首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Google Crashpad与Linux应用程序集成

将Google Crashpad与Linux应用程序集成
EN

Stack Overflow用户
提问于 2021-07-14 20:27:15
回答 1查看 221关注 0票数 1

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

因为它是overview design

我按照这个link在ubuntu上创建了一个处理程序进程

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

代码语言:javascript
复制
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相关的信息。是否有人可以提供任何工作示例的链接

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-14 22:57:08

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

以下是BugSplat的myUbuntuCrasher示例中的一段代码:

代码语言:javascript
复制
// Start crash handler
CrashpadClient *client = new CrashpadClient();
bool status = client->StartHandler(handler, reportsDir, metricsDir, url, annotations, arguments, true, false, attachments);

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

代码语言:javascript
复制
#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, reportsDir, metricsDir, url, annotations, arguments, true, false, 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中找到。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68378087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档