将 Google Crashpad 与 Linux 应用程序集成

问题描述

我正在尝试将 Google 的 Crashpad 集成到我在 Ubuntu 上运行的应用程序中。

按照overview design

crashpad design

我按照此 link

在 ubuntu 上创建了一个处理程序进程

现在对于客户端进程,我应该通过套接字连接向处理程序注册它。

Linux/Android
On Linux,a registration is a connected socket pair between a client process and the Crashpad handler. This socket pair may be private or shared among many client processes.

我该怎么做?

互联网上没有太多与 crashpad 相关的信息。有人可以提供任何工作示例的链接

解决方法

除非您有一个未在此处列出的特殊用例,否则您不必手动对套接字执行任何操作。只需在程序入口点创建一个新的 CrashpadClient 实例并调用 StartHandler

以下是来自 BugSplat 的 myUbuntuCrasher 示例的片段:

// Start crash handler
CrashpadClient *client = new CrashpadClient();
bool status = client->StartHandler(handler,reportsDir,metricsDir,url,annotations,arguments,true,false,attachments);

以下是来自 main.cpp 的完整示例:

#include <stdio.h>
#include <unistd.h>
#include "client/crashpad_client.h"
#include "client/crash_report_database.h"
#include "client/settings.h"

#define MIN(x,y) (((x) < (y)) ? (x) : (y))

#if defined(OS_POSIX)
typedef std::string StringType;
#elif defined(OS_WIN)
typedef std::wstring StringType;
#endif

using namespace base;
using namespace crashpad;
using namespace std;

bool initializeCrashpad(void);
StringType getExecutableDir(void);
void crash(void);

int main(int argc,char **argv) {
    initializeCrashpad();
    crash();
}

void crash() {
    *(volatile int *)0 = 0;
}

bool initializeCrashpad() {
    // Get directory where the exe lives so we can pass a full path to handler,reportsDir and metricsDir
    StringType exeDir = getExecutableDir();

    // Ensure that handler is shipped with your application
    FilePath handler(exeDir + "/../crashpad/bin/crashpad_handler");

    // Directory where reports will be saved. Important! Must be writable or crashpad_handler will crash.
    FilePath reportsDir(exeDir);

    // Directory where metrics will be saved. Important! Must be writable or crashpad_handler will crash.
    FilePath metricsDir(exeDir);

    // Configure url with BugSplat’s public fred database. Replace 'fred' with the name of your BugSplat database.
    StringType url = "http://fred.bugsplat.com/post/bp/crash/crashpad.php";

    // Metadata that will be posted to the server with the crash report map
    map<StringType,StringType> annotations;
    annotations["format"] = "minidump";           // Required: Crashpad setting to save crash as a minidump
    annotations["database"] = "fred";             // Required: BugSplat database
    annotations["product"] = "myUbuntuCrasher";   // Required: BugSplat appName
    annotations["version"] = "1.0.0";             // Required: BugSplat appVersion
    annotations["key"] = "Sample key";            // Optional: BugSplat key field
    annotations["user"] = "fred@bugsplat.com";    // Optional: BugSplat user email
    annotations["list_annotations"] = "Sample comment"; // Optional: BugSplat crash description

    // Disable crashpad rate limiting so that all crashes have dmp files
    vector<StringType> arguments; 
    arguments.push_back("--no-rate-limit");

    // File paths of attachments to be uploaded with the minidump file at crash time - default bundle limit is 2MB
    vector<FilePath> attachments;
    FilePath attachment(exeDir + "/attachment.txt");
    attachments.push_back(attachment);

    // Initialize Crashpad database
    unique_ptr<CrashReportDatabase> database = CrashReportDatabase::Initialize(reportsDir);
    if (database == NULL) return false;

    // Enable automated crash uploads
    Settings *settings = database->GetSettings();
    if (settings == NULL) return false;
    settings->SetUploadsEnabled(true);

    // Start crash handler
    CrashpadClient *client = new CrashpadClient();
    bool status = client->StartHandler(handler,attachments);
    return status;
}

StringType getExecutableDir() {
    char pBuf[FILENAME_MAX];
    int len = sizeof(pBuf);
    int bytes = MIN(readlink("/proc/self/exe",pBuf,len),len - 1);
    if (bytes >= 0) {
        pBuf[bytes] = '\0';
    }

    char* lastForwardSlash = strrchr(&pBuf[0],'/');
    if (lastForwardSlash == NULL) return NULL;
    *lastForwardSlash = '\0';

    return pBuf;
}

有关在 Ubuntu 中配置 Crashpad 的更多信息,请参见 here