Integrating Crashpad

102 views Asked by At

Cmake applaction is running fine after integrating crashpad. But no crash dump gets generated on application crash.

#include "cn/Paths.h"


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

#if defined(_MSC_VER)
#define NOMINMAX
#include <windows.h>

#elif defined(__APPLE__)
#include <mach-o/dyld.h>
#include <vector>

#elif defined(__linux__)
#include <stdio.h>
#include <unistd.h>
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif

bool initializeCrashpad( std::string dbName, std::string appName, std::string appVersion );
wxString getExecutableDir( void );
void crash( void );

int main( int argc, char** argv ) {
    std::string dbName = "wxtestdb";
    std::string appName = "wxtestapp";
    std::string appVersion = "1.0";

    initializeCrashpad( dbName, appName, appVersion );
    crash();
}

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

bool initializeCrashpad( std::string dbName, std::string appName, std::string appVersion )
{
    // Get directory where the exe lives so we can pass a full path to handler, reportsDir, metricsDir and attachments
    wxString exeDir = getExecutableDir();

    if ( exeDir == wxEmptyString )
        return false;

    cn::Paths crashpadPaths( exeDir );

    base::FilePath handler( cn::Paths::getPlatformString( crashpadPaths.getHandlerPath() ) );
    base::FilePath reportsDir( cn::Paths::getPlatformString( crashpadPaths.getReportsPath() ) );
    base::FilePath metricsDir( cn::Paths::getPlatformString( crashpadPaths.getMetricsPath() ) );
    std::string url = "https://fred.bugsplat.com/post/bp/crash/crashpad.php";

    // Metadata that will be posted to the server with the crash report map
    std::map<std::string, std::string> annotations;
    annotations["format"] = "minidump";
    annotations["database"] = dbName;
    annotations["product"] = appName;
    annotations["version"] = appVersion;
    annotations["key"] = "Sample key";
    annotations["user"] = "[email protected]";
    annotations["list_annotations"] = "Sample comment";

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

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

    // File paths of attachments to uploaded with minidump file at crash time - default upload limit is 2MB
    std::vector<base::FilePath> attachments;
    base::FilePath attachment( cn::Paths::getPlatformString( crashpadPaths.getAttachmentPath() ) );
    attachments.push_back( attachment );

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

    // Start crash handler
    crashpad::CrashpadClient* client = new crashpad::CrashpadClient();
    auto a = client->GetHandlerIPCPipe();
    bool status = client->StartHandler( handler, reportsDir, metricsDir, url, annotations, arguments, true, true );
    //bool a = client->WaitForHandlerStart( 50000 );
    return status;
}

wxString getExecutableDir( void )
{
#if defined(_MSC_VER)
    HMODULE hModule = GetModuleHandleW( NULL );
    WCHAR path[MAX_PATH];
    DWORD retVal = GetModuleFileNameW( hModule, path, MAX_PATH );
    if ( retVal == 0 ) return wxEmptyString;

    wchar_t* lastBackslash = wcsrchr( path, '\\' );
    if ( lastBackslash == NULL ) return wxEmptyString;
    *lastBackslash = 0;

    return path;

#elif defined(__APPLE__)
    unsigned int bufferSize = 512;
    vector<char> buffer( bufferSize + 1 );

    if ( _NSGetExecutablePath( &buffer[0], &bufferSize ) ) {
        buffer.resize( bufferSize );
        _NSGetExecutablePath( &buffer[0], &bufferSize );
    }

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

    return &buffer[0];

#elif defined(__linux__)
    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 wxEmptyString;
    *lastForwardSlash = '\0';

    return pBuf;
#endif
}

Running application thorugh cmd found that ipc_pipe was somehow empty. and getting this error msg FATAL crashpad_client_win.cc:707] Check failed: !ipc_pipe_.empty().

Please help me to find out if I am missing anything to set.

System config: Windows 10 Visual Studio 17 2022 x64 Windows SDK 10.1.22621.1778

0

There are 0 answers