-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrashpad.cpp
More file actions
176 lines (146 loc) · 4.04 KB
/
Crashpad.cpp
File metadata and controls
176 lines (146 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright © 2026 CCP ehf.
#include "StdAfx.h"
#if !_DEBUG
#include "Crashpad.h"
#include "FileSystem.h"
#include "client/annotation.h"
#include "client/crashpad_client.h"
#include "client/crash_report_database.h"
#include "client/settings.h"
#if _WIN32
auto executablePath = L"eve_crashmon.exe";
#elif __APPLE__
auto executablePath = "eve_crashmon";
#endif
// Data for an annotation isn't allowed to move around after created, so we use this wrapper class
class AnnotationData
{
public:
AnnotationData( const char* name, const char* value )
{
memset( m_name, 0, sizeof( m_name ) );
memset( m_value, 0, sizeof( m_value ) );
strncpy_s( m_name, sizeof( m_name ), name, strlen( name ) );
m_annotation = std::make_unique<crashpad::Annotation>( crashpad::Annotation::Type::kString, m_name, m_value );
SetValue( value );
}
AnnotationData( const AnnotationData& ) = delete;
AnnotationData& operator=( const AnnotationData& ) = delete;
~AnnotationData()
{
m_annotation->Clear();
}
void SetValue( const char* value )
{
size_t len = strlen( value );
memset( m_value, 0, strlen( m_value ) );
strncpy_s( m_value, sizeof( m_value ), value, len );
m_annotation->SetSize( static_cast<crashpad::Annotation::ValueSizeType>( len ) );
}
private:
char m_name[crashpad::Annotation::kNameMaxLength];
char m_value[crashpad::Annotation::kValueMaxSize];
std::unique_ptr<crashpad::Annotation> m_annotation;
};
bool CrashpadCrashInterface::InitializeCrashpad()
{
auto appdata = GetAppdataFolder();
if( appdata.empty() )
{
return false;
}
std::wstring logsFolder = GetLogsFolder();
if( logsFolder.empty() )
{
fprintf( stderr, "Failed to find folder for crashpad logs" );
return false;
}
if( !CreateDirectoryRec(logsFolder.c_str()) )
{
fprintf( stderr, "Failed to create folder for crashpad logs" );
return false;
}
#ifdef __APPLE__
std::string exePath = (const char*)CW2A( CcpExecutablePath().c_str() );
base::FilePath handler(exePath);
handler = handler.DirName().Append( executablePath );
std::string sLogsFolder = (const char*)CW2A( logsFolder.c_str() );
base::FilePath reportsDir( sLogsFolder );
#elif _WIN32
std::wstring exePath = CcpExecutablePath();
base::FilePath handler( exePath );
handler = handler.DirName().Append( executablePath );
base::FilePath reportsDir( logsFolder );
#else
#error Unsupported platform
#endif
base::FilePath metricsDir( reportsDir );
std::string url = "https://sentry.io/api/1434648/minidump/?sentry_key=0b0785270cff40ab88073f3429a81eb4";
std::map<std::string, std::string> annotations;
std::vector<std::string> arguments = { "--no-rate-limit" };
m_database = crashpad::CrashReportDatabase::Initialize( reportsDir );
if( !m_database )
{
return false;
}
// Enable automated crash uploads
m_settings = m_database->GetSettings();
if( !m_settings )
{
return false;
}
m_settings->SetUploadsEnabled( m_enabled );
m_client = std::make_unique<crashpad::CrashpadClient>();
return m_client->StartHandler( handler, reportsDir, metricsDir, url, annotations, arguments, true, false );
}
void CrashpadCrashInterface::EnableCrashReporting( bool enable )
{
m_enabled = enable;
if( m_settings )
{
m_settings->SetUploadsEnabled( enable );
}
}
void CrashpadCrashInterface::SetCrashKeyValue( const char* key, const char* val )
{
if( !m_settings )
{
return;
}
auto found = m_annotations.find( key );
if( found != m_annotations.end() )
{
found->second->SetValue( val );
}
else
{
m_annotations.emplace( key, std::make_unique<AnnotationData>( key, val ) );
}
}
bool CrashpadCrashInterface::IsCrashReportingEnabled()
{
return m_enabled;
}
void CrashpadCrashInterface::ProduceImmediateDump()
{
if( m_client )
{
#if _WIN32
LPCONTEXT lpContext{};
if( GetThreadContext( GetCurrentThread(), lpContext ) )
{
m_client->DumpWithoutCrash( *lpContext );
}
#endif
}
}
CrashpadCrashInterface* GetCrashReporter()
{
#if !_DEBUG
static CrashpadCrashInterface s_crashpadCrashInterface;
return &s_crashpadCrashInterface;
#else
return nullptr;
#endif
}
#endif