Crashpad is an open-source library initially developed by Google as a successor to the Google Breakpad library. It is used in popular software such as Google Chrome, and by companies such as Slack and Spotify.
For existing users of Crashpad, Backtrace has plug-and-play support. It has a robust architecture designed to allow for a high degree of customizability and stability even in the face of most obscure of software crashes.
For new users, Backtrace has prepared an enhanced fork of Crashpad, allowing easier integration with the service.
Initial Integration
If you are a Windows and Visual Studio user, please review the Backtrace Visual Studio Extension Guide to simplify the integration of Crashpad into your new application.
If you are a Windows and Visual Studio user with an existing application, you can use manual instructions in the Visual Studio section of this guide for a step-by-step guide to integrating with Visual Studio.
Advanced instructions are available at the Crashpad home page if you can't find what you're looking for or prefer to build Crashpad from source. Backtrace's fork of Crashpad is available on Github. If you would like additional assistance, please don't hesitate to contact support@backtrace.io.
Update your application
Once Crashpad has been integrated into your application, you'll need to specify the right options to crashpad::CrashpadClient::StartHandler
. For more advanced information on StartHandler
, please refer to the documentation linked here.
bool crashpad::CrashpadClient::StartHandler(
const base::FilePath& handler,
const base::FilePath& database,
const base::FilePath& metrics_dir,
const std::string& url,
const std::map<std::string, std::string>& annotations,
const std::vector<std::string>& arguments,
bool restartable,
bool asynchronous_start
)
Windows users can also use Backtrace-specific convenience function crashpad::CrashpadClient::StartHandlerForBacktrace
:
bool StartHandlerForBacktrace(
const base::FilePath& handler,
const base::FilePath& database,
const base::FilePath& metrics_dir,
const std::string& url,
const std::map<std::string, std::string>& annotations,
const std::vector<std::string>& arguments,
const std::map<std::string, std::string>& file_attachments,
bool restartable,
bool asynchronous_start
)
It's very similar to StartHandler
, with the exception of the file_attachments
parameter. file_attachments
is a map of attachment name ⟶ path, allowing convenient inclusion of file attachments (i.e. the log file) with the crash dump.
Set the URL parameter
Change the url
parameter to StartHandler
or StartHandlerForBacktrace
to point to your server dump submission port (labeled as http/writer
in the listener configuration pane). Preferrably, the SSL enabled port should be used. If Backtrace is hosting your instance, the default port is 6098.
For example, if Backtrace is hosting your instance at team.sp.backtrace.io
, then set the URL argument to https://team.sp.backtrace.io:6098/
. The URL needs to have a proper canonical form (the trailing slash shall not be omitted). The reason for this is the WinHttpCrackUrl
function used by the Windows Crashpad uploader internally - it fails to parse the URL without the trailing slash.
Set optional annotations
The annotations
map can also be used to set additional attributes. Attributes allow you to embed relevant context into each crash report and can be any data point of your choosing. Examples include version
, directx.version
, graphics.card
, etc. More information on attributes can be found in the product guide.
File attachments
On Linux and (only with the Backtrace fork) Windows, you can attach files to crash data (i.e. log files). To do so, add string formatted as such to the arguments
parameter of the StartHandler
function:
arguments.push_back(
"--attachment=attachment_UPLOAD_NAME=FILE_PATH"
);
For example:
arguments.push_back(
"--attachment=attachment_2018-02-30.log=C:/my_app/app_2018-02-30.log"
);
If you are using the Backtrace fork on Windows, you can use the convenience function StartHandlerForBacktrace
. Then, you should use the file_attachments
parameter instead, and the above code may be simplified to the following:
std::map<std::string, std::string> files = {
{ "2018-02-30.log", "C:/my_app/app_2018-02-30.log" }
};
Crash handler parameter
handler
is a path to an external program responsible for generating and uploading dumps. This is the recommended for uploading crashes. On Windows, look for bin/crashpad_handler.exe
, which is suitable as a default crash handler. See below for a complete example.
handler
is executed once the application crashes, so it should be available under the specified path during its execution. That means that the handler should be bundled with the application if it's delivered to the end-users, and that its path should be relative or dynamically generated.
Example code
See below for self-contained example code. The following code is also applicable to macOS applications, just ensure the various file paths are valid.
[...]
// On Windows, define NOMINMAX prior to including Crashpad to avoid
// unwanted definitions of macros MIN and MAX from WinAPI
#define NOMINMAX
#include <client/crash_report_database.h>
#include <client/settings.h>
#include <client/crashpad_client.h>
[...]
static bool
startCrashHandler()
{
using namespace crashpad;
std::map<std::string, std::string> annotations;
std::vector<std::string> arguments;
CrashpadClient client;
bool rc;
/*
* ENSURE THIS VALUE IS CORRECT.
*
* This is the directory you will use to store and
* queue crash data.
*/
std::wstring db_path(L"my_app_dir\\Crashpad\\db");
/*
* ENSURE THIS VALUE IS CORRECT.
*
* Crashpad has the ability to support crashes both in-process
* and out-of-process. The out-of-process handler is
* significantly more robust than traditional in-process crash
* handlers. This path may be relative.
*/
std::wstring handler_path(L"my_app_dir\\bin\\crashpad_handler.exe");
/*
* YOU MUST CHANGE THIS VALUE.
*
* Update the <Universe> and <Token> in the below
*/
std::string url(""https://submit.backtrace.io/<Universe>/<Token>/minidump");
/*
* REMOVE THIS FOR ACTUAL BUILD.
*
* To disable crashpad’s default limitation of
* 1 upload per hour, pass the --no-rate-limit
* argument to the handler
*
*/
arguments.push_back("--no-rate-limit");
base::FilePath db(db_path);
base::FilePath handler(handler_path);
std::unique_ptr<CrashReportDatabase> database =
crashpad::CrashReportDatabase::Initialize(db);
if (database == nullptr || database->GetSettings() == NULL)
return false;
/* Enable automated uploads. */
database->GetSettings()->SetUploadsEnabled(true);
rc = client.StartHandler(handler,
db,
db,
url,
annotations,
arguments,
true,
true);
if (rc == false)
return false;
/* Optional, wait for Crashpad to initialize. */
rc = client.WaitForHandlerStart(INFINITE);
if (rc == false)
return false;
return true;
}
Manage Symbols
Symbols must be uploaded to have Backtrace determine source-code mapping of incoming crashes, including source file and line number. In order for Backtrace to effectively group and analyze your incoming crashes, you must provide application debug symbols.
To learn more about how to upload and manage symbols with Backtrace, please see the symbolification guide.
Visual Studio
If you are a Windows and Visual Studio user, please review the Backtrace Visual Studio Extension Guide to simplify the integration of Crashpad into your application!
The instructions below are accurate, but please consider the extension to minimize the chance of configuration errors.
Obtain Crashpad
You can build Crashpad from source or download Backtrace-provided versions of the library.
Build from source
It is always possible to build Crashpad from source. In order to do this, please see the build guide on the Crashpad website. Backtrace's branch is available here.
Download built libraries
Backtrace provides updated builds of Crashpad. It is recommended to use the stable version. Nightly builds are provided if you want to take advantage of the latest improvements in Crashpad.
The latest release of Crashpad is available at http://get.backtrace.io/crashpad/builds/.
Integrate Crashpad
Once you have built Crashpad or download the pre-built libraries, you will now need to integrate the library into your application. The library is statically linked into your project, so you will not need to distribute any additional projects.
There are two versions of Crashpad
provided in the Backtrace archive.
lib_mt
contains the crashpad libraries built with the /MT
switch for static linking. lib_md
contains the crashpad libraries built with the /MD
swtich for dynamic linking.
Use the appropriate version for your application.
Integrate into your build process
This section outlines how to import the Crashpad
library into your Visual Studio project.
Add the header files
First, you'll need to add the Crashpad directory in the include path for your application. The header files for Crashpad in the provided .zip
file are in the include directory.
Go to the project configuration menu (Project
> Properties
> VC++ Directories
) and set Include Directories
to point to the include
and include\mini_chromium
folders of the extracted archive.
Add the libraries
Static Linking
Now, you'll need to add the relevant release path (see the table above) to your Library Directories
(Project
> Properties
> VC++ Directories
). For example, if I am deploying a 64-bit Windows application, then after downloading the appropriate crashpad build, the lib_mt
sub-directory is added to Library Directories
.
Once that is done, you'll need to add the actual set of static libraries for Crashpad
. Navigate to your linker input settings (Project
> Properties
> Linker
> Input
), and add client.lib;util.lib;base.lib
as an additional dependencies. See the screenshot below for an example.
Remember to use the build of the Crashpad
library that corresponds with your build configuration. For example, if you are building a 32-bit Debug build, then ensure that you are referencing the .lib
files from the 32-bit debug build. If you encounter errors involving ITERATOR_DEBUG_LEVEL
, then there is likely a mismatch between your build configuration and the build of Crashpad
. In order to change the build settings, go to Build
> Configuration Manager
then change your Active solution configuration
.
Dynamic Linking
If you are using dynamic linking (the \MD
flag), then make sure to use the lib_md
sub-directory rather than lib_mt
. For example, crashpad\lib_md
rather than crashpad\lib_mt
.
Verify Linker Settings
Static Linking
Last but not least, ensure that you have code generation runtime settings set to a mode compatible with static libraries, such as Multi-threaded (/MT)
. Go to Project
> Properties
> C/C++
> Code Generation
and update the Runtime Library
setting.
Dynamic Linking
If you are using dynamic linking, then ensure that you use the /MD
option instead of /MT
.
Debug builds
If you are building in debug mode, ensure that you use the debug version of the linking compiler switches (/MDd
and /MTd
):
Ensure symbol generation
It is required to upload symbols into Backtrace for intelligent deduplication and classification. This section explains how to enable debug symbols for your application.
Go to Project
> Properties
> Linker
and update the Generate Debug Info
setting. You'll want to set it to either Generate Debug Information (/DEBUG)
or Generate Debug Information optimized for sharing and publishing (/DEBUG:FULL)
. The /DEBUG
option is recommended if you would like to avoid the possibility of a performance impact.
With this setting, a .pdb
file will be generated for your application in the build output directory. You are able to upload .sym
, .pdb
and archive files containing .pdb
or .sym
files into Backtrace manually or through the command line. It is also possible to hook up Visual Studio to automatically upload symbols as they are generated. You should be good to send crash reports at this point. Simply ensure that you've uploaded your symbols (click on Symbols
tab under the Project Configuration
page).
For more details, please refer to the symbolification guide.
Send crash reports
Congratulations, now you're ready to integrate Crashpad into your application. In order to do this, you'll need to add the code from the section Example code
above.
For Windows applications, consider using the convenience function StartHandlerForBacktrace
.
Finish
At this point, crashes should be automatically submitted into Backtrace. As crashes generate, refresh the Project
page of the associated project to see faults in real-time.
Additional features of Backtrace fork
Convenient file attachment upload
The fork provided by Backtrace additionally offers file upload support for Windows applications. To make use of it, use Backtrace-specific function StartHandlerForBacktrace
, as discussed above.
Send reports using EXCEPTION_POINTERS in Windows
A new function has been added to the CrashpadClient
class. It's useful, for example, when dealing with vectored exceptions. Additionally, it does not require that
the process must end (the exception may be handled).
static void DumpWithoutCrashWithException(EXCEPTION_POINTERS* pointer);
Windows 7, Windows Server 2008 R2 and Windows Server 2012 support (adding TLS 1.1/1.2 support)
The default Crashpad crash handler binary uses WinHttp to upload crashes. On those systems, TLS 1.1 and TLS 1.2 are in the default protocol set, and only TLS 1.0, and SSL 3.0 are available by default, which are not accepted by Backtrace.
This should be fixed by KB 3140245, but you may want to add them to the default list manually via the registry. We suggest doing it in your application installer.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\WinHttp]
"DefaultSecureProtocols"=dword:00000a00
Additional Documentation
Additional documentation is available at the Crashpad Website. For more information on the crashpad_handler
, please see crashpad_handler.md.
If you're still encountering issues, contact us at support@backtrace.io.