Google Crashpad

Crashpad is an open-source multiplatform crash reporting system written in C++ by Google. It supports macOS, Windows and Linux (limited), and features an uploader to submit minidumps to a configured URL right when the process crashes.

Follow the Crashpad developer docs for instructions on how to build Crashpad from source. Make sure that crashpad’s header files are in your include path, then add a call to StartHandler() during your program startup:

Copied
#include <map>
#include <string>
#include <vector>

#include "client/crashpad_client.h"
#include "client/settings.h"

using namespace crashpad;

bool startCrashpad() {
  // Cache directory that will store crashpad information and minidumps
  base::FilePath database("path/to/crashpad/db");
  // Path to the out-of-process handler executable
  base::FilePath handler("path/to/crashpad_handler");
  // URL used to submit minidumps to
  std::string url("https://o0.ingest.sentry.io/api/0/minidump/?sentry_key=examplePublicKey");
  // Optional annotations passed via --annotations to the handler
  std::map<string, string> annotations;
  // Optional arguments to pass to the handler
  std::vector<string> arguments;

  CrashpadClient client;
  bool success = client.StartHandler(
    handler,
    database,
    database,
    url,
    annotations,
    arguments,
    /* restartable */ true,
    /* asynchronous_start */ false
  );

  return success;
}

This method directs crashes to the Crashpad handler. On macOS, this is applicable to this process and all subsequent child processes. On other platforms, child processes must also register by using SetHandlerIPCPipe(). For more information on configuring the crashpad handler, see crashpad_handler.

If you also want Crashpad to upload crashes to Sentry, additionally configure the Crashpad database for automatic uploads:

Copied
// #include "client/crash_report_database.h"

base::FilePath database("path/to/crashpad/db");
std::unique_ptr<CrashReportDatabase> db =
    crashpad::CrashReportDatabase::Initialize(database);

if (db != nullptr && db->GetSettings() != nullptr) {
  db->GetSettings()->SetUploadsEnabled(true);
}

By default, the crashpad handler will limit uploads to one per hour. To disable this limitation, pass the --no-rate-limit argument to the handler:

Copied
arguments.push_back("--no-rate-limit");