-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathSentrySubsystemDesktop.cpp
460 lines (352 loc) · 14.2 KB
/
SentrySubsystemDesktop.cpp
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Copyright (c) 2022 Sentry. All Rights Reserved.
#include "SentrySubsystemDesktop.h"
#include "SentryEventDesktop.h"
#include "SentryBreadcrumbDesktop.h"
#include "SentryUserDesktop.h"
#include "SentryScopeDesktop.h"
#include "SentryTransactionDesktop.h"
#include "SentryTransactionContextDesktop.h"
#include "SentryDefines.h"
#include "SentrySettings.h"
#include "SentryEvent.h"
#include "SentryBreadcrumb.h"
#include "SentryUserFeedback.h"
#include "SentryUser.h"
#include "SentryTransaction.h"
#include "SentryModule.h"
#include "SentryBeforeSendHandler.h"
#include "SentryTraceSampler.h"
#include "SentryTransactionContext.h"
#include "SentryUserFeedbackDesktop.h"
#include "Infrastructure/SentryConvertorsDesktop.h"
#include "CrashReporter/SentryCrashReporter.h"
#include "CrashReporter/SentryCrashContext.h"
#include "Transport/SentryTransport.h"
#include "Misc/Paths.h"
#include "Misc/ScopeLock.h"
#include "HAL/FileManager.h"
#include "Runtime/Launch/Resources/Version.h"
#include "GenericPlatform/GenericPlatformOutputDevices.h"
#include "GenericPlatform/GenericPlatformCrashContext.h"
#if PLATFORM_WINDOWS
#include "Windows/WindowsPlatformMisc.h"
#endif
#if USE_SENTRY_NATIVE
void PrintVerboseLog(sentry_level_t level, const char *message, va_list args, void *userdata)
{
char buffer[512];
vsnprintf(buffer, 512, message, args);
#if !NO_LOGGING
const FName SentryCategoryName(LogSentrySdk.GetCategoryName());
#else
const FName SentryCategoryName(TEXT("LogSentrySdk"));
#endif
GLog->CategorizedLogf(SentryCategoryName, SentryConvertorsDesktop::SentryLevelToLogVerbosity(level), TEXT("%s"), *FString(buffer));
}
sentry_value_t HandleBeforeSend(sentry_value_t event, void *hint, void *closure)
{
SentrySubsystemDesktop* SentrySubsystem = static_cast<SentrySubsystemDesktop*>(closure);
USentryEvent* EventToProcess = NewObject<USentryEvent>();
EventToProcess->InitWithNativeImpl(MakeShareable(new SentryEventDesktop(event)));
SentrySubsystem->GetCurrentScope()->Apply(EventToProcess);
return SentrySubsystem->GetBeforeSendHandler()->HandleBeforeSend(EventToProcess, nullptr) ? event : sentry_value_new_null();
}
sentry_value_t HandleBeforeCrash(const sentry_ucontext_t *uctx, sentry_value_t event, void *closure)
{
SentrySubsystemDesktop* SentrySubsystem = static_cast<SentrySubsystemDesktop*>(closure);
FSentryCrashContext::Get()->Apply(SentrySubsystem->GetCurrentScope());
return HandleBeforeSend(event, nullptr, closure);
}
SentrySubsystemDesktop::SentrySubsystemDesktop()
: beforeSend(nullptr)
, crashReporter(MakeShareable(new SentryCrashReporter))
, isEnabled(false)
, isStackTraceEnabled(true)
{
}
void SentrySubsystemDesktop::InitWithSettings(const USentrySettings* settings, USentryBeforeSendHandler* beforeSendHandler, USentryTraceSampler* traceSampler)
{
beforeSend = beforeSendHandler;
scopeStack.Push(MakeShareable(new SentryScopeDesktop()));
sentry_options_t* options = sentry_options_new();
if(settings->EnableAutoLogAttachment)
{
const FString LogFilePath = FGenericPlatformOutputDevices::GetAbsoluteLogFilename();
#if PLATFORM_WINDOWS
sentry_options_add_attachmentw(options, *FPaths::ConvertRelativePathToFull(LogFilePath));
#elif PLATFORM_LINUX
sentry_options_add_attachment(options, TCHAR_TO_UTF8(*FPaths::ConvertRelativePathToFull(LogFilePath)));
#endif
}
if(settings->UseProxy)
{
sentry_options_set_http_proxy(options, TCHAR_TO_ANSI(*settings->ProxyUrl));
}
if(settings->EnableTracing && settings->SamplingType == ESentryTracesSamplingType::UniformSampleRate)
{
sentry_options_set_traces_sample_rate(options, settings->TracesSampleRate);
}
if(settings->EnableTracing && settings->SamplingType == ESentryTracesSamplingType::TracesSampler)
{
UE_LOG(LogSentrySdk, Warning, TEXT("The Native SDK doesn't currently support sampling functions"));
}
#if PLATFORM_WINDOWS
if(!FSentryModule::Get().IsMarketplaceVersion())
{
const FString HandlerPath = GetHandlerPath();
if(!FPaths::FileExists(HandlerPath))
{
UE_LOG(LogSentrySdk, Log, TEXT("Crashpad executable couldn't be found so Breakpad will be used instead. "
"Please make sure that the plugin was rebuilt to avoid initialization failure."));
}
sentry_options_set_handler_pathw(options, *HandlerPath);
}
#elif PLATFORM_LINUX
sentry_options_set_handler_path(options, TCHAR_TO_ANSI(*GetHandlerPath()));
#endif
#if PLATFORM_WINDOWS
sentry_options_set_database_pathw(options, *GetDatabasePath());
#elif PLATFORM_LINUX
sentry_options_set_database_path(options, TCHAR_TO_ANSI(*GetDatabasePath()));
#endif
sentry_options_set_release(options, TCHAR_TO_ANSI(settings->OverrideReleaseName
? *settings->Release
: *settings->GetFormattedReleaseName()));
sentry_options_set_dsn(options, TCHAR_TO_ANSI(*settings->Dsn));
sentry_options_set_environment(options, TCHAR_TO_ANSI(*settings->Environment));
sentry_options_set_logger(options, PrintVerboseLog, nullptr);
sentry_options_set_debug(options, settings->Debug);
sentry_options_set_auto_session_tracking(options, settings->EnableAutoSessionTracking);
sentry_options_set_sample_rate(options, settings->SampleRate);
sentry_options_set_max_breadcrumbs(options, settings->MaxBreadcrumbs);
sentry_options_set_before_send(options, HandleBeforeSend, this);
sentry_options_set_on_crash(options, HandleBeforeCrash, this);
sentry_options_set_shutdown_timeout(options, 3000);
#if PLATFORM_LINUX
sentry_options_set_transport(options, FSentryTransport::Create());
#endif
int initResult = sentry_init(options);
UE_LOG(LogSentrySdk, Log, TEXT("Sentry initialization completed with result %d (0 on success)."), initResult);
isEnabled = initResult == 0 ? true : false;
sentry_clear_crashed_last_run();
#if PLATFORM_WINDOWS && ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION >= 2
FPlatformMisc::SetCrashHandlingType(settings->EnableAutoCrashCapturing
? ECrashHandlingType::Disabled
: ECrashHandlingType::Default);
#endif
isStackTraceEnabled = settings->AttachStacktrace;
crashReporter->SetRelease(settings->Release);
crashReporter->SetEnvironment(settings->Environment);
}
void SentrySubsystemDesktop::Close()
{
isEnabled = false;
sentry_close();
scopeStack.Empty();
}
bool SentrySubsystemDesktop::IsEnabled()
{
return isEnabled;
}
ESentryCrashedLastRun SentrySubsystemDesktop::IsCrashedLastRun()
{
ESentryCrashedLastRun unrealIsCrashed = ESentryCrashedLastRun::NotEvaluated;
switch (sentry_get_crashed_last_run())
{
case -1:
unrealIsCrashed = ESentryCrashedLastRun::NotEvaluated;
break;
case 0:
unrealIsCrashed = ESentryCrashedLastRun::NotCrashed;
break;
case 1:
unrealIsCrashed = ESentryCrashedLastRun::Crashed;
break;
default:
UE_LOG(LogSentrySdk, Warning, TEXT("Unknown IsCrashedLastRun result. NotEvaluated will be returned."));
}
return unrealIsCrashed;
}
void SentrySubsystemDesktop::AddBreadcrumb(USentryBreadcrumb* breadcrumb)
{
GetCurrentScope()->AddBreadcrumb(breadcrumb);
}
void SentrySubsystemDesktop::AddBreadcrumbWithParams(const FString& Message, const FString& Category, const FString& Type, const TMap<FString, FString>& Data, ESentryLevel Level)
{
TSharedPtr<SentryBreadcrumbDesktop> breadcrumbDesktop = MakeShareable(new SentryBreadcrumbDesktop());
breadcrumbDesktop->SetMessage(Message);
breadcrumbDesktop->SetCategory(Category);
breadcrumbDesktop->SetType(Type);
breadcrumbDesktop->SetData(Data);
breadcrumbDesktop->SetLevel(Level);
GetCurrentScope()->AddBreadcrumb(breadcrumbDesktop);
}
void SentrySubsystemDesktop::ClearBreadcrumbs()
{
GetCurrentScope()->ClearBreadcrumbs();
}
USentryId* SentrySubsystemDesktop::CaptureMessage(const FString& message, ESentryLevel level)
{
sentry_value_t sentryEvent = sentry_value_new_message_event(SentryConvertorsDesktop::SentryLevelToNative(level), nullptr, TCHAR_TO_ANSI(*message));
if(isStackTraceEnabled)
{
sentry_value_set_stacktrace(sentryEvent, nullptr, 0);
}
sentry_uuid_t id = sentry_capture_event(sentryEvent);
return SentryConvertorsDesktop::SentryIdToUnreal(id);
}
USentryId* SentrySubsystemDesktop::CaptureMessageWithScope(const FString& message, const FConfigureScopeNativeDelegate& onScopeConfigure, ESentryLevel level)
{
FScopeLock Lock(&CriticalSection);
TSharedPtr<SentryScopeDesktop> NewLocalScope = MakeShareable(new SentryScopeDesktop(*GetCurrentScope()));
USentryScope* Scope = NewObject<USentryScope>();
Scope->InitWithNativeImpl(NewLocalScope);
onScopeConfigure.ExecuteIfBound(Scope);
scopeStack.Push(NewLocalScope);
USentryId* Id = CaptureMessage(message, level);
scopeStack.Pop();
return Id;
}
USentryId* SentrySubsystemDesktop::CaptureEvent(USentryEvent* event)
{
TSharedPtr<SentryEventDesktop> eventDesktop = StaticCastSharedPtr<SentryEventDesktop>(event->GetNativeImpl());
sentry_value_t nativeEvent = eventDesktop->GetNativeObject();
if(isStackTraceEnabled)
{
sentry_value_set_stacktrace(nativeEvent, nullptr, 0);
}
sentry_uuid_t id = sentry_capture_event(nativeEvent);
return SentryConvertorsDesktop::SentryIdToUnreal(id);
}
USentryId* SentrySubsystemDesktop::CaptureEventWithScope(USentryEvent* event, const FConfigureScopeNativeDelegate& onScopeConfigure)
{
FScopeLock Lock(&CriticalSection);
TSharedPtr<SentryScopeDesktop> NewLocalScope = MakeShareable(new SentryScopeDesktop(*GetCurrentScope()));
USentryScope* Scope = NewObject<USentryScope>();
Scope->InitWithNativeImpl(NewLocalScope);
onScopeConfigure.ExecuteIfBound(Scope);
scopeStack.Push(NewLocalScope);
USentryId* Id = CaptureEvent(event);
scopeStack.Pop();
return Id;
}
USentryId* SentrySubsystemDesktop::CaptureException(const FString& type, const FString& message, int32 framesToSkip)
{
sentry_value_t exceptionEvent = sentry_value_new_event();
auto StackFrames = FGenericPlatformStackWalk::GetStack(framesToSkip);
sentry_value_set_by_key(exceptionEvent, "stacktrace", SentryConvertorsDesktop::CallstackToNative(StackFrames));
sentry_value_t nativeException = sentry_value_new_exception(TCHAR_TO_ANSI(*type), TCHAR_TO_ANSI(*message));
sentry_event_add_exception(exceptionEvent, nativeException);
sentry_uuid_t id = sentry_capture_event(exceptionEvent);
return SentryConvertorsDesktop::SentryIdToUnreal(id);
}
USentryId* SentrySubsystemDesktop::CaptureAssertion(const FString& type, const FString& message)
{
return CaptureException(type, message, 7);
}
USentryId* SentrySubsystemDesktop::CaptureEnsure(const FString& type, const FString& message)
{
#if PLATFORM_WINDOWS && ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION >= 3
int32 framesToSkip = 8;
#else
int32 framesToSkip = 7;
#endif
return CaptureException(type, message, framesToSkip);
}
void SentrySubsystemDesktop::CaptureUserFeedback(USentryUserFeedback* userFeedback)
{
TSharedPtr<SentryUserFeedbackDesktop> userFeedbackDesktop = StaticCastSharedPtr<SentryUserFeedbackDesktop>(userFeedback->GetNativeImpl());
sentry_capture_user_feedback(userFeedbackDesktop->GetNativeObject());
}
void SentrySubsystemDesktop::SetUser(USentryUser* user)
{
TSharedPtr<SentryUserDesktop> userDesktop = StaticCastSharedPtr<SentryUserDesktop>(user->GetNativeImpl());
sentry_set_user(userDesktop->GetNativeObject());
crashReporter->SetUser(user);
}
void SentrySubsystemDesktop::RemoveUser()
{
sentry_remove_user();
crashReporter->RemoveUser();
}
void SentrySubsystemDesktop::ConfigureScope(const FConfigureScopeNativeDelegate& onConfigureScope)
{
USentryScope* Scope = NewObject<USentryScope>();
Scope->InitWithNativeImpl(GetCurrentScope());
onConfigureScope.ExecuteIfBound(Scope);
}
void SentrySubsystemDesktop::SetContext(const FString& key, const TMap<FString, FString>& values)
{
GetCurrentScope()->SetContext(key, values);
crashReporter->SetContext(key, values);
}
void SentrySubsystemDesktop::SetTag(const FString& key, const FString& value)
{
GetCurrentScope()->SetTagValue(key, value);
crashReporter->SetTag(key, value);
}
void SentrySubsystemDesktop::RemoveTag(const FString& key)
{
GetCurrentScope()->RemoveTag(key);
crashReporter->RemoveTag(key);
}
void SentrySubsystemDesktop::SetLevel(ESentryLevel level)
{
GetCurrentScope()->SetLevel(level);
}
void SentrySubsystemDesktop::StartSession()
{
sentry_start_session();
}
void SentrySubsystemDesktop::EndSession()
{
sentry_end_session();
}
USentryTransaction* SentrySubsystemDesktop::StartTransaction(const FString& name, const FString& operation)
{
sentry_transaction_context_t* transactionContext = sentry_transaction_context_new(TCHAR_TO_ANSI(*name), TCHAR_TO_ANSI(*operation));
sentry_transaction_t* nativeTransaction = sentry_transaction_start(transactionContext, sentry_value_new_null());
return SentryConvertorsDesktop::SentryTransactionToUnreal(nativeTransaction);
}
USentryTransaction* SentrySubsystemDesktop::StartTransactionWithContext(USentryTransactionContext* context)
{
TSharedPtr<SentryTransactionContextDesktop> transactionContextDesktop = StaticCastSharedPtr<SentryTransactionContextDesktop>(context->GetNativeImpl());
sentry_transaction_t* nativeTransaction = sentry_transaction_start(transactionContextDesktop->GetNativeObject(), sentry_value_new_null());
return SentryConvertorsDesktop::SentryTransactionToUnreal(nativeTransaction);
}
USentryTransaction* SentrySubsystemDesktop::StartTransactionWithContextAndOptions(USentryTransactionContext* context, const TMap<FString, FString>& options)
{
UE_LOG(LogSentrySdk, Log, TEXT("Transaction options currently not supported on desktop."));
return StartTransactionWithContext(context);
}
USentryBeforeSendHandler* SentrySubsystemDesktop::GetBeforeSendHandler()
{
return beforeSend;
}
TSharedPtr<SentryScopeDesktop> SentrySubsystemDesktop::GetCurrentScope()
{
if(scopeStack.Num() == 0)
{
UE_LOG(LogSentrySdk, Warning, TEXT("Scope stack is empty."));
return nullptr;
}
return scopeStack.Top();
}
FString SentrySubsystemDesktop::GetHandlerPath() const
{
#if PLATFORM_WINDOWS
const FString HandlerExecutableName = TEXT("crashpad_handler.exe");
#elif PLATFORM_LINUX
const FString HandlerExecutableName = TEXT("crashpad_handler");
#endif
const FString HandlerPath = FPaths::Combine(FSentryModule::Get().GetBinariesPath(), HandlerExecutableName);
const FString HandlerFullPath = FPaths::ConvertRelativePathToFull(HandlerPath);
return HandlerFullPath;
}
FString SentrySubsystemDesktop::GetDatabasePath() const
{
const FString DatabasePath = FPaths::Combine(FPaths::ProjectDir(), TEXT(".sentry-native"));
const FString DatabaseFullPath = FPaths::ConvertRelativePathToFull(DatabasePath);
return DatabaseFullPath;
}
#endif