-
-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathsentry_backend_native.c
More file actions
1181 lines (1053 loc) · 41.3 KB
/
Copy pathsentry_backend_native.c
File metadata and controls
1181 lines (1053 loc) · 41.3 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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "sentry_boot.h"
#if defined(SENTRY_PLATFORM_UNIX)
# include <errno.h>
# include <fcntl.h>
# include <pthread.h>
# include <signal.h>
# include <sys/types.h>
# include <sys/wait.h>
# include <unistd.h>
# if defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID)
# include <sys/prctl.h>
# endif
#elif defined(SENTRY_PLATFORM_WINDOWS) && !defined(SENTRY_PLATFORM_XBOX)
# include <werapi.h>
#endif
#include <string.h>
#include "sentry_alloc.h"
#include "sentry_backend.h"
#include "sentry_core.h"
#include "sentry_crash_context.h"
#include "sentry_crash_daemon.h"
#include "sentry_crash_handler.h"
#include "sentry_crash_ipc.h"
#include "sentry_database.h"
#include "sentry_envelope.h"
#include "sentry_json.h"
#include "sentry_logger.h"
#include "sentry_logs.h"
#include "sentry_metrics.h"
#include "sentry_options.h"
#include "sentry_os.h"
#include "sentry_path.h"
#include "sentry_scope.h"
#include "sentry_session.h"
#include "sentry_sync.h"
#include "sentry_tracing.h"
#include "sentry_transport.h"
#include "sentry_value.h"
#include "transports/sentry_disk_transport.h"
// Global process-wide synchronization for IPC and shared memory access
// This lives for the entire backend lifetime and is shared across all threads
#if defined(SENTRY_PLATFORM_WINDOWS)
static HANDLE g_ipc_mutex = NULL;
#elif defined(SENTRY_PLATFORM_MACOS)
// macOS uses a plain pthread mutex instead of named semaphores (sem_open)
// because App Sandbox blocks POSIX named semaphores.
static sentry_mutex_t g_ipc_sync_mutex = SENTRY__MUTEX_INIT;
#else
# include <semaphore.h>
static sem_t *g_ipc_init_sem = SEM_FAILED;
static char g_ipc_sem_name[64] = { 0 };
#endif
// Mutex to protect IPC initialization (Windows and Linux only, not macOS/iOS)
// macOS uses g_ipc_sync_mutex directly; iOS has no out-of-process daemon.
#if defined(SENTRY_PLATFORM_WINDOWS) \
|| (!defined(SENTRY_PLATFORM_MACOS) && !defined(SENTRY_PLATFORM_IOS))
# ifdef SENTRY__MUTEX_INIT_DYN
SENTRY__MUTEX_INIT_DYN(g_ipc_init_mutex)
# else
static sentry_mutex_t g_ipc_init_mutex = SENTRY__MUTEX_INIT;
# endif
#endif
#if defined(SENTRY_PLATFORM_WINDOWS) && !defined(SENTRY_PLATFORM_XBOX)
static sentry_wer_registration_t g_wer_registration = { 0 };
static sentry_path_t *g_wer_path = NULL;
static LSTATUS
wer_set_registry_value(const sentry_path_t *wer_path, DWORD value)
{
return RegSetKeyValueW(HKEY_CURRENT_USER,
L"Software\\Microsoft\\Windows\\Windows Error Reporting\\"
L"RuntimeExceptionHelperModules",
wer_path->path_w, REG_DWORD, &value, sizeof(value));
}
static LSTATUS
wer_delete_registry_value(const sentry_path_t *wer_path)
{
return RegDeleteKeyValueW(HKEY_CURRENT_USER,
L"Software\\Microsoft\\Windows\\Windows Error Reporting\\"
L"RuntimeExceptionHelperModules",
wer_path->path_w);
}
static sentry_path_t *
wer_default_path(void)
{
sentry_path_t *current_exe = sentry__path_current_exe();
if (!current_exe) {
return NULL;
}
sentry_path_t *exe_dir = sentry__path_dir(current_exe);
sentry__path_free(current_exe);
if (!exe_dir) {
return NULL;
}
sentry_path_t *wer_path = sentry__path_join_str(exe_dir, "sentry-wer.dll");
sentry__path_free(exe_dir);
return wer_path;
}
static void
wer_unregister_module(void)
{
if (!g_wer_path) {
return;
}
WerUnregisterRuntimeExceptionModule(
g_wer_path->path_w, &g_wer_registration);
wer_delete_registry_value(g_wer_path);
sentry__path_free(g_wer_path);
g_wer_path = NULL;
memset(&g_wer_registration, 0, sizeof(g_wer_registration));
}
static bool
wer_register_module(uint64_t app_tid)
{
windows_version_t win_ver;
if (!sentry__get_windows_version(&win_ver) || win_ver.build < 19041) {
SENTRY_WARN("Native WER module not registered, because Windows "
"doesn't meet version requirements (build >= 19041).");
return false;
}
sentry_path_t *wer_path = wer_default_path();
if (!wer_path || !sentry__path_is_file(wer_path)) {
SENTRY_WARN("Native WER module not found");
sentry__path_free(wer_path);
return false;
}
const DWORD one = 1;
LSTATUS reg_res = wer_set_registry_value(wer_path, one);
if (reg_res != ERROR_SUCCESS) {
SENTRY_WARN("registering native WER module in registry failed");
sentry__path_free(wer_path);
return false;
}
g_wer_registration.version = 1;
g_wer_registration.app_pid = GetCurrentProcessId();
g_wer_registration.app_tid = app_tid;
HRESULT hr = WerRegisterRuntimeExceptionModule(
wer_path->path_w, &g_wer_registration);
if (FAILED(hr)) {
SENTRY_WARN("registering native WER module failed");
wer_delete_registry_value(wer_path);
sentry__path_free(wer_path);
memset(&g_wer_registration, 0, sizeof(g_wer_registration));
return false;
}
SENTRY_DEBUGF("registered native WER module \"%s\"", wer_path->path);
g_wer_path = wer_path;
return true;
}
#endif
/**
* Native backend state
*/
typedef struct {
sentry_crash_ipc_t *ipc;
pid_t daemon_pid;
sentry_path_t *event_path;
sentry_path_t *breadcrumb1_path;
sentry_path_t *breadcrumb2_path;
sentry_path_t *envelope_path;
size_t num_breadcrumbs;
volatile long crashed;
} native_backend_state_t;
static int
native_backend_startup(
sentry_backend_t *backend, const sentry_options_t *options)
{
SENTRY_WARN("The native backend is experimental and under active "
"development.");
SENTRY_DEBUG("starting native backend");
#if defined(SENTRY_PLATFORM_WINDOWS)
// Create process-wide mutex for IPC synchronization (Windows)
// Use portable mutex to protect Windows mutex creation
SENTRY__MUTEX_INIT_DYN_ONCE(g_ipc_init_mutex);
sentry__mutex_lock(&g_ipc_init_mutex);
if (!g_ipc_mutex) {
wchar_t mutex_name[64];
swprintf(
mutex_name, 64, L"Local\\SentryIPC-%lu", GetCurrentProcessId());
g_ipc_mutex = CreateMutexW(NULL, FALSE, mutex_name);
if (!g_ipc_mutex) {
sentry__mutex_unlock(&g_ipc_init_mutex);
SENTRY_WARNF("failed to create IPC mutex: %lu", GetLastError());
return 1;
}
}
sentry__mutex_unlock(&g_ipc_init_mutex);
#elif defined(SENTRY_PLATFORM_MACOS)
// macOS uses a plain pthread mutex (no sem_open which is blocked by App
// Sandbox). The mutex is statically initialized - no setup needed.
(void)0;
#elif !defined(SENTRY_PLATFORM_IOS)
// Create process-wide IPC initialization semaphore (singleton pattern)
// Protected by mutex to handle concurrent backend startups
SENTRY__MUTEX_INIT_DYN_ONCE(g_ipc_init_mutex);
sentry__mutex_lock(&g_ipc_init_mutex);
if (g_ipc_init_sem == SEM_FAILED) {
snprintf(g_ipc_sem_name, sizeof(g_ipc_sem_name), "/sentry-init-%d",
(int)getpid());
// Unlink any stale semaphore from previous runs
sem_unlink(g_ipc_sem_name);
// Create fresh semaphore with initial value 1
g_ipc_init_sem = sem_open(g_ipc_sem_name, O_CREAT | O_EXCL, 0600, 1);
if (g_ipc_init_sem == SEM_FAILED) {
sentry__mutex_unlock(&g_ipc_init_mutex);
SENTRY_WARNF("failed to create IPC semaphore: %s", strerror(errno));
return 1;
}
}
sentry__mutex_unlock(&g_ipc_init_mutex);
#endif
native_backend_state_t *state = SENTRY_MAKE(native_backend_state_t);
if (!state) {
return 1;
}
backend->data = state;
// Initialize IPC (protected by global synchronization for concurrent
// access)
#if defined(SENTRY_PLATFORM_WINDOWS)
state->ipc = sentry__crash_ipc_init_app(g_ipc_mutex);
#elif defined(SENTRY_PLATFORM_IOS)
state->ipc = sentry__crash_ipc_init_app(NULL);
#elif defined(SENTRY_PLATFORM_MACOS)
state->ipc = sentry__crash_ipc_init_app(&g_ipc_sync_mutex);
#else
state->ipc = sentry__crash_ipc_init_app(g_ipc_init_sem);
#endif
if (!state->ipc) {
SENTRY_WARN("failed to initialize crash IPC");
sentry_free(state);
backend->data = NULL;
return 1;
}
// Configure crash context (protected by synchronization for concurrent
// access)
#if defined(SENTRY_PLATFORM_WINDOWS)
if (g_ipc_mutex) {
DWORD wait_result = WaitForSingleObject(g_ipc_mutex, INFINITE);
if (wait_result != WAIT_OBJECT_0) {
SENTRY_WARNF("failed to acquire mutex for context setup: %lu",
GetLastError());
sentry__crash_ipc_free(state->ipc);
sentry_free(state);
backend->data = NULL;
return 1;
}
}
#elif defined(SENTRY_PLATFORM_MACOS)
sentry__mutex_lock(&g_ipc_sync_mutex);
#elif !defined(SENTRY_PLATFORM_IOS)
if (g_ipc_init_sem && sem_wait(g_ipc_init_sem) < 0) {
SENTRY_WARNF("failed to acquire semaphore for context setup: %s",
strerror(errno));
sentry__crash_ipc_free(state->ipc);
sentry_free(state);
backend->data = NULL;
return 1;
}
#endif
sentry_crash_context_t *ctx = state->ipc->shmem;
// Set minidump mode from options
ctx->minidump_mode = (sentry_minidump_mode_t)options->minidump_mode;
// Set crash reporting mode from options
ctx->crash_reporting_mode = options->crash_reporting_mode;
ctx->system_crash_reporter_enabled = options->system_crash_reporter_enabled;
ctx->crash_upload_mode = options->crash_upload_mode;
// Pass debug logging setting to daemon
ctx->debug_enabled = options->debug;
ctx->attach_screenshot = options->attach_screenshot;
ctx->attach_session_replay = options->attach_session_replay;
ctx->session_replay_duration = options->session_replay_duration;
ctx->cache_keep = (int)options->cache_keep;
ctx->require_user_consent = options->require_user_consent;
ctx->enable_large_attachments = options->enable_large_attachments;
ctx->http_retry = options->http_retry;
ctx->shutdown_timeout = options->shutdown_timeout;
ctx->transfer_timeout = options->transfer_timeout;
sentry__atomic_store(
&ctx->user_consent, sentry__atomic_fetch(&options->run->user_consent));
// Set up event and breadcrumb paths
sentry_path_t *run_path = options->run->run_path;
sentry_path_t *db_path = options->database_path;
// Store database path for daemon use
if (db_path) {
#ifdef _WIN32
strncpy_s(ctx->database_path, sizeof(ctx->database_path), db_path->path,
_TRUNCATE);
#else
strncpy(
ctx->database_path, db_path->path, sizeof(ctx->database_path) - 1);
ctx->database_path[sizeof(ctx->database_path) - 1] = '\0';
#endif
}
// Store DSN for daemon to send crashes
if (options->dsn && options->dsn->raw) {
#ifdef _WIN32
strncpy_s(ctx->dsn, sizeof(ctx->dsn), options->dsn->raw, _TRUNCATE);
#else
strncpy(ctx->dsn, options->dsn->raw, sizeof(ctx->dsn) - 1);
ctx->dsn[sizeof(ctx->dsn) - 1] = '\0';
#endif
}
// Store transport configuration for daemon's curl transport
if (options->ca_certs) {
#ifdef _WIN32
strncpy_s(
ctx->ca_certs, sizeof(ctx->ca_certs), options->ca_certs, _TRUNCATE);
#else
strncpy(ctx->ca_certs, options->ca_certs, sizeof(ctx->ca_certs) - 1);
ctx->ca_certs[sizeof(ctx->ca_certs) - 1] = '\0';
#endif
}
if (options->proxy) {
#ifdef _WIN32
strncpy_s(ctx->proxy, sizeof(ctx->proxy), options->proxy, _TRUNCATE);
#else
strncpy(ctx->proxy, options->proxy, sizeof(ctx->proxy) - 1);
ctx->proxy[sizeof(ctx->proxy) - 1] = '\0';
#endif
}
if (options->user_agent) {
#ifdef _WIN32
strncpy_s(ctx->user_agent, sizeof(ctx->user_agent), options->user_agent,
_TRUNCATE);
#else
strncpy(
ctx->user_agent, options->user_agent, sizeof(ctx->user_agent) - 1);
ctx->user_agent[sizeof(ctx->user_agent) - 1] = '\0';
#endif
}
state->event_path = sentry__path_join_str(run_path, "__sentry-event");
state->breadcrumb1_path
= sentry__path_join_str(run_path, "__sentry-breadcrumb1");
state->breadcrumb2_path
= sentry__path_join_str(run_path, "__sentry-breadcrumb2");
sentry__path_touch(state->event_path);
sentry__path_touch(state->breadcrumb1_path);
sentry__path_touch(state->breadcrumb2_path);
// Copy paths to crash context
#ifdef _WIN32
strncpy_s(ctx->event_path, sizeof(ctx->event_path), state->event_path->path,
_TRUNCATE);
strncpy_s(ctx->breadcrumb1_path, sizeof(ctx->breadcrumb1_path),
state->breadcrumb1_path->path, _TRUNCATE);
strncpy_s(ctx->breadcrumb2_path, sizeof(ctx->breadcrumb2_path),
state->breadcrumb2_path->path, _TRUNCATE);
#else
strncpy(
ctx->event_path, state->event_path->path, sizeof(ctx->event_path) - 1);
ctx->event_path[sizeof(ctx->event_path) - 1] = '\0';
strncpy(ctx->breadcrumb1_path, state->breadcrumb1_path->path,
sizeof(ctx->breadcrumb1_path) - 1);
ctx->breadcrumb1_path[sizeof(ctx->breadcrumb1_path) - 1] = '\0';
strncpy(ctx->breadcrumb2_path, state->breadcrumb2_path->path,
sizeof(ctx->breadcrumb2_path) - 1);
ctx->breadcrumb2_path[sizeof(ctx->breadcrumb2_path) - 1] = '\0';
#endif
// Set up crash envelope path
state->envelope_path = sentry__path_join_str(
options->run->run_path, "__sentry-crash.envelope");
if (state->envelope_path) {
#ifdef _WIN32
strncpy_s(ctx->envelope_path, sizeof(ctx->envelope_path),
state->envelope_path->path, _TRUNCATE);
#else
strncpy(ctx->envelope_path, state->envelope_path->path,
sizeof(ctx->envelope_path) - 1);
ctx->envelope_path[sizeof(ctx->envelope_path) - 1] = '\0';
#endif
}
// Set up external crash reporter if configured
if (options->external_crash_reporter) {
#ifdef _WIN32
strncpy_s(ctx->external_reporter_path,
sizeof(ctx->external_reporter_path),
options->external_crash_reporter->path, _TRUNCATE);
#else
strncpy(ctx->external_reporter_path,
options->external_crash_reporter->path,
sizeof(ctx->external_reporter_path) - 1);
ctx->external_reporter_path[sizeof(ctx->external_reporter_path) - 1]
= '\0';
#endif
}
#if defined(SENTRY_PLATFORM_WINDOWS)
// Release mutex after context configuration
if (g_ipc_mutex) {
ReleaseMutex(g_ipc_mutex);
}
#elif defined(SENTRY_PLATFORM_MACOS)
sentry__mutex_unlock(&g_ipc_sync_mutex);
#elif !defined(SENTRY_PLATFORM_IOS)
// Release semaphore after context configuration
if (g_ipc_init_sem) {
sem_post(g_ipc_init_sem);
}
#endif
// Install crash handlers (signal handlers on Linux/macOS, Mach exception
// handler on iOS)
#if defined(SENTRY_PLATFORM_IOS)
if (sentry__crash_handler_init(state->ipc) < 0) {
SENTRY_WARN("failed to initialize crash handler");
sentry__crash_ipc_free(state->ipc);
sentry_free(state);
backend->data = NULL;
return 1;
}
#else
// Other platforms: Use out-of-process daemon
// Pass the notification handles (eventfd/pipe on Unix, events on Windows)
const char *daemon_handler_path
= options->handler_path ? options->handler_path->path : NULL;
# if defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID)
uint64_t tid = (uint64_t)pthread_self();
state->daemon_pid = sentry__crash_daemon_start(getpid(), tid,
state->ipc->notify_fd, state->ipc->ready_fd, daemon_handler_path);
# elif defined(SENTRY_PLATFORM_MACOS)
uint64_t tid = (uint64_t)pthread_self();
state->daemon_pid
= sentry__crash_daemon_start(getpid(), tid, state->ipc->notify_pipe[0],
state->ipc->ready_pipe[1], state->ipc->shm_fd, daemon_handler_path);
# elif defined(SENTRY_PLATFORM_WINDOWS)
uint64_t tid = (uint64_t)GetCurrentThreadId();
state->daemon_pid = sentry__crash_daemon_start(GetCurrentProcessId(), tid,
state->ipc->event_handle, state->ipc->ready_event_handle,
daemon_handler_path);
# endif
// On Windows, pid_t is DWORD (unsigned), so (pid_t)-1 == 0xFFFFFFFF.
// On Unix, pid_t is signed and fork returns -1 on failure.
# if defined(SENTRY_PLATFORM_WINDOWS)
if (state->daemon_pid == (pid_t)-1) {
# else
if (state->daemon_pid < 0) {
# endif
SENTRY_WARN("failed to start crash daemon");
sentry__crash_ipc_free(state->ipc);
sentry_free(state);
backend->data = NULL;
return 1;
}
SENTRY_DEBUGF("crash daemon started with PID %d", state->daemon_pid);
# if defined(SENTRY_PLATFORM_MACOS)
// Close unused pipe ends in parent process
close(state->ipc->notify_pipe[0]); // Daemon reads from this
close(state->ipc->ready_pipe[1]); // Daemon writes to this
state->ipc->notify_pipe[0] = -1;
state->ipc->ready_pipe[1] = -1;
# endif
# if defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID)
// Close unused eventfd ends in parent process
// (eventfds are bidirectional, but we only use one direction per fd)
// Parent writes to notify_fd, daemon reads from it - parent can close for
// reading Daemon writes to ready_fd, parent reads from it - parent can
// close for writing Actually, eventfds can't be closed for one direction,
// so keep them open
// On Linux, allow the daemon to ptrace this process
// This is required when Yama LSM ptrace_scope is enabled
if (prctl(PR_SET_PTRACER, state->daemon_pid, 0, 0, 0) != 0) {
SENTRY_WARNF(
"prctl(PR_SET_PTRACER) failed: %s - daemon may not be able to "
"read process memory",
strerror(errno));
} else {
SENTRY_DEBUGF("Set daemon PID %d as ptracer", state->daemon_pid);
}
# endif
// Wait for daemon to signal it's ready
if (!sentry__crash_ipc_wait_for_ready(
state->ipc, SENTRY_CRASH_DAEMON_READY_TIMEOUT_MS)) {
SENTRY_WARN("Daemon did not signal ready in time, proceeding anyway");
} else {
SENTRY_DEBUG("Daemon signaled ready");
}
# if defined(SENTRY_PLATFORM_WINDOWS) && !defined(SENTRY_PLATFORM_XBOX)
state->ipc->shmem->platform.wer_enabled = wer_register_module(tid);
# endif
if (sentry__crash_handler_init(state->ipc) < 0) {
SENTRY_WARN("failed to initialize crash handler");
# if defined(SENTRY_PLATFORM_UNIX)
kill(state->daemon_pid, SIGTERM);
# elif defined(SENTRY_PLATFORM_WINDOWS)
# if !defined(SENTRY_PLATFORM_XBOX)
wer_unregister_module();
# endif
// On Windows, terminate the daemon process
HANDLE hDaemon
= OpenProcess(PROCESS_TERMINATE, FALSE, state->daemon_pid);
if (hDaemon) {
TerminateProcess(hDaemon, 1);
CloseHandle(hDaemon);
}
# endif
sentry__crash_ipc_free(state->ipc);
sentry_free(state);
backend->data = NULL;
return 1;
}
#endif
SENTRY_DEBUG("native backend started successfully");
return 0;
}
static void
native_backend_shutdown(sentry_backend_t *backend)
{
SENTRY_DEBUG("shutting down native backend");
native_backend_state_t *state = (native_backend_state_t *)backend->data;
if (!state) {
return;
}
#if defined(SENTRY_PLATFORM_WINDOWS) && !defined(SENTRY_PLATFORM_XBOX)
wer_unregister_module();
#endif
// Shutdown crash handlers (signal handlers on Linux/macOS, Mach exception
// handler on iOS)
sentry__crash_handler_shutdown();
#if defined(SENTRY_PLATFORM_UNIX) && !defined(SENTRY_PLATFORM_IOS)
// Terminate daemon (Unix)
if (state->daemon_pid > 0) {
kill(state->daemon_pid, SIGTERM);
// Wait for daemon to exit
waitpid(state->daemon_pid, NULL, 0);
}
#elif defined(SENTRY_PLATFORM_WINDOWS)
// Terminate daemon (Windows)
if (state->daemon_pid > 0) {
HANDLE hDaemon = OpenProcess(
PROCESS_TERMINATE | SYNCHRONIZE, FALSE, state->daemon_pid);
if (hDaemon) {
TerminateProcess(hDaemon, 0);
// Wait for daemon to exit (with timeout)
WaitForSingleObject(hDaemon, 5000); // 5 second timeout
CloseHandle(hDaemon);
}
}
#endif
// Dump daemon log file for debugging (especially useful in CI)
// Use same naming as shared memory to find the correct log file
if (state->ipc && state->ipc->shmem) {
char log_path[SENTRY_CRASH_MAX_PATH];
int log_path_len = -1;
// Extract the unique ID from the shm name/path to find the daemon log
// Platform-specific: shm_name on Linux/Windows, shm_path on macOS
#if defined(SENTRY_PLATFORM_WINDOWS)
const wchar_t *shm_id_w = wcsrchr(state->ipc->shm_name, L'-');
if (shm_id_w) {
shm_id_w++; // Skip the '-'
char *shm_id = sentry__string_from_wstr(shm_id_w);
if (shm_id) {
log_path_len = _snprintf(log_path, sizeof(log_path),
"%s\\sentry-daemon-%s.log",
state->ipc->shmem->database_path, shm_id);
if (log_path_len > 0 && log_path_len < (int)sizeof(log_path)) {
wchar_t *wpath = sentry__string_to_wstr(log_path);
FILE *log_file = wpath ? _wfopen(wpath, L"r") : NULL;
sentry_free(wpath);
if (log_file) {
fprintf(stderr,
"\n========== Daemon Log (%s) ==========\n",
shm_id);
char line[1024];
while (fgets(line, sizeof(line), log_file)) {
fprintf(stderr, "%s", line);
}
fprintf(stderr,
"=========================================\n\n");
fclose(log_file);
}
}
sentry_free(shm_id);
}
}
#else
// On macOS: shm_path = "{tmpdir}/.sentry-shm-{id}"
// On Linux: shm_name = "/s-{id}"
// In both cases, the ID follows the last '-'
# if defined(SENTRY_PLATFORM_MACOS)
const char *shm_id_src = state->ipc->shm_path;
# else
const char *shm_id_src = state->ipc->shm_name;
# endif
const char *shm_id = shm_id_src[0] ? strrchr(shm_id_src, '-') : NULL;
if (shm_id) {
shm_id++; // Skip the '-'
log_path_len = snprintf(log_path, sizeof(log_path),
"%s/sentry-daemon-%s.log", state->ipc->shmem->database_path,
shm_id);
if (log_path_len > 0 && log_path_len < (int)sizeof(log_path)) {
FILE *log_file = fopen(log_path, "r");
if (log_file) {
fprintf(stderr, "\n========== Daemon Log (%s) ==========\n",
shm_id);
char line[1024];
while (fgets(line, sizeof(line), log_file)) {
fprintf(stderr, "%s", line);
}
fprintf(stderr,
"=========================================\n\n");
fclose(log_file);
}
}
}
#endif
}
// Cleanup IPC
if (state->ipc) {
sentry__crash_ipc_free(state->ipc);
state->ipc = NULL; // Prevent use-after-free
}
#if !defined(SENTRY_PLATFORM_WINDOWS) && !defined(SENTRY_PLATFORM_IOS)
// Don't clean up semaphore here - it persists for the process lifetime
// and may be reused if backend is restarted within same process
#endif
SENTRY_DEBUG("native backend shutdown complete");
}
static void
native_backend_user_consent_changed(sentry_backend_t *backend)
{
native_backend_state_t *state = (native_backend_state_t *)backend->data;
if (!state || !state->ipc || !state->ipc->shmem) {
return;
}
SENTRY_WITH_OPTIONS (options) {
if (options->run) {
sentry__atomic_store(&state->ipc->shmem->user_consent,
sentry__atomic_fetch(&options->run->user_consent));
}
}
}
static void
native_backend_free(sentry_backend_t *backend)
{
native_backend_state_t *state = (native_backend_state_t *)backend->data;
if (!state) {
return;
}
sentry__path_free(state->event_path);
sentry__path_free(state->breadcrumb1_path);
sentry__path_free(state->breadcrumb2_path);
sentry__path_free(state->envelope_path);
sentry_free(state);
}
// Writes the scope's attachment list to <run>/__sentry-attachments so the
// crash daemon can locate and append them to the crash envelope.
static void
native_backend_write_attachments(const sentry_path_t *event_path)
{
if (!event_path) {
return;
}
SENTRY_WITH_SCOPE (scope) {
if (!scope->attachments) {
continue;
}
sentry_path_t *run_path = sentry__path_dir(event_path);
if (!run_path) {
continue;
}
sentry_path_t *attach_list_path
= sentry__path_join_str(run_path, "__sentry-attachments");
if (attach_list_path) {
sentry_value_t attach_list = sentry_value_new_list();
for (sentry_attachment_t *it = scope->attachments; it;
it = it->next) {
if (!it->path) {
continue;
}
sentry_value_t attach_info = sentry_value_new_object();
sentry_value_set_by_key(attach_info, "path",
sentry_value_new_string(it->path->path));
const char *filename = sentry__path_filename(
it->filename ? it->filename : it->path);
sentry_value_set_by_key(
attach_info, "filename", sentry_value_new_string(filename));
if (it->type && *it->type) {
sentry_value_set_by_key(attach_info, "attachment_type",
sentry_value_new_string(it->type));
}
if (it->content_type) {
sentry_value_set_by_key(attach_info, "content_type",
sentry_value_new_string(it->content_type));
}
sentry_value_append(attach_list, attach_info);
}
size_t attach_json_len = 0;
char *attach_json
= sentry__value_to_json(attach_list, &attach_json_len);
sentry_value_decref(attach_list);
if (attach_json) {
sentry__path_write_buffer(
attach_list_path, attach_json, attach_json_len);
sentry_free(attach_json);
}
sentry__path_free(attach_list_path);
}
sentry__path_free(run_path);
}
}
static void
native_backend_flush_scope(
sentry_backend_t *backend, const sentry_options_t *UNUSED(options))
{
native_backend_state_t *state = (native_backend_state_t *)backend->data;
if (!state || !state->event_path) {
return;
}
// Manifest writes must continue post-crash so attachments registered
// from on_crash/before_send reach the daemon
native_backend_write_attachments(state->event_path);
if (sentry__atomic_fetch(&state->crashed)) {
return;
}
// Create event with current scope
sentry_value_t event = sentry_value_new_object();
sentry_value_set_by_key(
event, "level", sentry__value_new_level(SENTRY_LEVEL_FATAL));
// Apply scope with contexts (includes OS, device info from Sentry)
SENTRY_WITH_SCOPE (scope) {
// Get contexts from scope (includes OS info)
sentry_value_t os_context
= sentry_value_get_by_key(scope->contexts, "os");
if (!sentry_value_is_null(os_context)) {
sentry_value_t event_contexts = sentry_value_new_object();
sentry_value_set_by_key(event_contexts, "os", os_context);
sentry_value_incref(os_context);
#if defined(SENTRY_PLATFORM_WINDOWS)
// Add device context with arch for Windows native events
// This is required for Sentry's symbolicator to process PE modules
sentry_value_t device_context = sentry_value_new_object();
sentry_value_set_by_key(
device_context, "type", sentry_value_new_string("device"));
# if defined(_M_AMD64)
sentry_value_set_by_key(
device_context, "arch", sentry_value_new_string("x86_64"));
# elif defined(_M_IX86)
sentry_value_set_by_key(
device_context, "arch", sentry_value_new_string("x86"));
# elif defined(_M_ARM64)
sentry_value_set_by_key(
device_context, "arch", sentry_value_new_string("arm64"));
# endif
sentry_value_set_by_key(event_contexts, "device", device_context);
#endif
sentry_value_set_by_key(event, "contexts", event_contexts);
}
// Also copy other scope data (user, tags, extra, etc.)
sentry_value_t user = scope->user;
if (sentry_value_get_type(user) == SENTRY_VALUE_TYPE_OBJECT
&& sentry_value_get_length(user) > 0) {
sentry_value_set_by_key(event, "user", user);
sentry_value_incref(user);
}
sentry_value_t tags = scope->tags;
if (!sentry_value_is_null(tags)) {
sentry_value_set_by_key(event, "tags", tags);
sentry_value_incref(tags);
}
sentry_value_t extra = scope->extra;
if (!sentry_value_is_null(extra)) {
sentry_value_set_by_key(event, "extra", extra);
sentry_value_incref(extra);
}
}
// Serialize to JSON (so it can be deserialized on next start)
size_t json_len = 0;
char *json_str = sentry__value_to_json(event, &json_len);
sentry_value_decref(event);
if (json_str) {
sentry__path_write_buffer(state->event_path, json_str, json_len);
sentry_free(json_str);
}
}
static void
native_backend_add_breadcrumb(sentry_backend_t *backend,
sentry_value_t breadcrumb, const sentry_options_t *options)
{
native_backend_state_t *state = (native_backend_state_t *)backend->data;
if (!state) {
return;
}
size_t max_breadcrumbs = options->max_breadcrumbs;
if (!max_breadcrumbs) {
return;
}
bool first_breadcrumb = state->num_breadcrumbs % max_breadcrumbs == 0;
const sentry_path_t *breadcrumb_file
= state->num_breadcrumbs % (max_breadcrumbs * 2) < max_breadcrumbs
? state->breadcrumb1_path
: state->breadcrumb2_path;
state->num_breadcrumbs++;
if (!breadcrumb_file) {
return;
}
// Serialize to JSON (so it can be deserialized on next start)
size_t json_len = 0;
char *json_str = sentry__value_to_json(breadcrumb, &json_len);
if (!json_str) {
return;
}
int rv = first_breadcrumb
? sentry__path_write_buffer(breadcrumb_file, json_str, json_len)
: sentry__path_append_buffer(breadcrumb_file, json_str, json_len);
sentry_free(json_str);
if (rv != 0) {
SENTRY_WARN("failed to write breadcrumb");
}
}
/**
* Ensures that buffer attachments have a unique path in the run directory.
* Similar to Crashpad's ensure_unique_path function.
*/
static bool
ensure_attachment_path(sentry_attachment_t *attachment)
{
if (!attachment || !attachment->filename) {
return false;
}
// Generate UUID for unique path
sentry_uuid_t uuid = sentry_uuid_new_v4();
char uuid_str[37];
sentry_uuid_as_string(&uuid, uuid_str);
sentry_path_t *base_path = NULL;
SENTRY_WITH_OPTIONS (options) {
if (options->run && options->run->run_path) {
base_path = sentry__path_join_str(options->run->run_path, uuid_str);
}
}
if (!base_path || sentry__path_create_dir_all(base_path) != 0) {
sentry__path_free(base_path);
return false;
}
sentry_path_t *old_path = attachment->path;
attachment->path = sentry__path_join_str(
base_path, sentry__path_filename(attachment->filename));
sentry__path_free(base_path);
sentry__path_free(old_path);
return attachment->path != NULL;
}
static void
native_backend_add_attachment(
sentry_backend_t *backend, sentry_attachment_t *attachment)
{
(void)backend; // Unused
// For buffer attachments, assign a path in the run directory and write to
// disk
if (attachment->buf) {
if (!attachment->path) {
if (!ensure_attachment_path(attachment)) {
SENTRY_WARN("failed to assign path for buffer attachment");
return;
}
}
// Write buffer to disk
if (sentry__path_write_buffer(
attachment->path, attachment->buf, attachment->buf_len)
!= 0) {
SENTRY_WARNF("failed to write native backend attachment \"%s\"",
attachment->path->path);
}
}
// For file attachments, the path is already set and points to the actual
// file. The crash daemon will read these files from their original
// locations.
}
/**
* Handle exception - called from signal handler via sentry_handle_exception
* This processes the event with on_crash/before_send hooks and ends the session
*/
static void
native_backend_except(sentry_backend_t *backend, const sentry_ucontext_t *uctx)
{
native_backend_state_t *state = (native_backend_state_t *)backend->data;
if (state) {
sentry__atomic_store(&state->crashed, 1);
}
SENTRY_WITH_OPTIONS (options) {
// Disable logging during crash handling if configured
if (!options->enable_logging_when_crashed) {
sentry__logger_disable();
}
SENTRY_DEBUG("handling native backend exception");
// Flush logs and metrics in a crash-safe manner before crash handling
if (options->enable_logs) {
sentry__logs_flush_crash_safe();
}
if (options->enable_metrics) {
sentry__metrics_flush_crash_safe();
}
// Write crash marker
sentry__write_crash_marker(options);
sentry_value_t transaction
= sentry__trace_finish(SENTRY_SPAN_STATUS_ABORTED);