-
-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathsentry_crash_daemon.c
More file actions
4195 lines (3755 loc) · 154 KB
/
sentry_crash_daemon.c
File metadata and controls
4195 lines (3755 loc) · 154 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_crash_daemon.h"
#include "minidump/sentry_minidump_writer.h"
#include "sentry_alloc.h"
#include "sentry_attachment.h"
#include "sentry_core.h"
#include "sentry_crash_ipc.h"
#include "sentry_database.h"
#include "sentry_envelope.h"
#include "sentry_json.h"
#include "sentry_logger.h"
#include "sentry_options.h"
#include "sentry_path.h"
#include "sentry_process.h"
#include "sentry_screenshot.h"
#include "sentry_session_replay.h"
#include "sentry_string.h"
#include "sentry_symbolizer.h"
#include "sentry_sync.h"
#include "sentry_transport.h"
#include "sentry_utils.h"
#include "sentry_uuid.h"
#include "sentry_value.h"
#include "transports/sentry_disk_transport.h"
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#if defined(SENTRY_PLATFORM_UNIX)
# include <dirent.h>
# include <dlfcn.h>
# include <errno.h>
# include <fcntl.h>
# include <inttypes.h>
# include <signal.h>
# include <sys/stat.h>
# include <sys/types.h>
# include <sys/uio.h>
# include <sys/wait.h>
# include <unistd.h>
# if defined(SENTRY_PLATFORM_MACOS)
# include <crt_externs.h>
# include <mach-o/dyld.h>
# include <spawn.h>
# endif
# if defined(SENTRY_PLATFORM_LINUX)
# include "unwinder/sentry_unwinder.h"
# endif
#elif defined(SENTRY_PLATFORM_WINDOWS)
# include <dbghelp.h>
# include <fcntl.h>
# include <io.h>
# include <sys/stat.h>
# include <windows.h>
// Forward declaration for StackWalk64-based stack unwinding (defined later)
static size_t walk_stack_with_dbghelp(HANDLE hProcess, DWORD crashed_tid,
const CONTEXT *ctx_record, sentry_frame_info_t *frames, size_t max_frames);
#endif
// Provide default ASAN options for sentry-crash daemon executable
// This suppresses false positives from fork() which ASAN doesn't handle well
#if defined(__has_feature)
# if __has_feature(address_sanitizer)
const char *
__asan_default_options(void)
{
// Disable stack-use-after-return detection which causes false positives
// with fork+exec since ASAN's shadow memory gets confused about ownership
return "detect_stack_use_after_return=0:halt_on_error=0";
}
# endif
#endif
/**
* Helper to write a file as an attachment to an envelope
* Returns true on success, false on failure
*/
static bool
write_attachment_to_envelope(int fd, const char *file_path,
const char *filename, const char *attachment_type, const char *content_type)
{
#if defined(SENTRY_PLATFORM_UNIX)
int attach_fd = open(file_path, O_RDONLY);
#elif defined(SENTRY_PLATFORM_WINDOWS)
// Use wide-char API for proper UTF-8 path support
wchar_t *wpath = sentry__string_to_wstr(file_path);
int attach_fd = wpath ? _wopen(wpath, _O_RDONLY | _O_BINARY) : -1;
sentry_free(wpath);
#endif
if (attach_fd < 0) {
SENTRY_WARNF("Failed to open attachment file: %s", file_path);
return false;
}
#if defined(SENTRY_PLATFORM_UNIX)
struct stat st;
if (fstat(attach_fd, &st) != 0) {
SENTRY_WARNF("Failed to stat attachment file: %s", file_path);
close(attach_fd);
return false;
}
long long file_size = (long long)st.st_size;
#elif defined(SENTRY_PLATFORM_WINDOWS)
struct __stat64 st;
if (_fstat64(attach_fd, &st) != 0) {
SENTRY_WARNF("Failed to stat attachment file: %s", file_path);
_close(attach_fd);
return false;
}
long long file_size = (long long)st.st_size;
#endif
// Write attachment item header
char header[SENTRY_CRASH_ENVELOPE_HEADER_SIZE];
int header_written;
if (content_type) {
header_written = snprintf(header, sizeof(header),
"{\"type\":\"attachment\",\"length\":%lld,"
"\"attachment_type\":\"%s\","
"\"content_type\":\"%s\","
"\"filename\":\"%s\"}\n",
file_size,
sentry__string_empty(attachment_type)
? SENTRY_ATTACHMENT_TYPE_GENERIC
: attachment_type,
content_type, filename ? filename : "attachment");
} else {
header_written = snprintf(header, sizeof(header),
"{\"type\":\"attachment\",\"length\":%lld,"
"\"attachment_type\":\"%s\","
"\"filename\":\"%s\"}\n",
file_size,
sentry__string_empty(attachment_type)
? SENTRY_ATTACHMENT_TYPE_GENERIC
: attachment_type,
filename ? filename : "attachment");
}
if (header_written < 0 || header_written >= (int)sizeof(header)) {
SENTRY_WARN("Failed to write attachment header");
#if defined(SENTRY_PLATFORM_UNIX)
close(attach_fd);
#elif defined(SENTRY_PLATFORM_WINDOWS)
_close(attach_fd);
#endif
return false;
}
#if defined(SENTRY_PLATFORM_UNIX)
if (write(fd, header, header_written) != (ssize_t)header_written) {
SENTRY_WARN("Failed to write attachment header to envelope");
}
#elif defined(SENTRY_PLATFORM_WINDOWS)
_write(fd, header, (unsigned int)header_written);
#endif
// Copy attachment content
char buf[SENTRY_CRASH_FILE_BUFFER_SIZE];
#if defined(SENTRY_PLATFORM_UNIX)
ssize_t n;
while ((n = read(attach_fd, buf, sizeof(buf))) > 0) {
ssize_t written = write(fd, buf, n);
if (written != n) {
SENTRY_WARNF(
"Failed to write attachment content for: %s", file_path);
close(attach_fd);
return false;
}
}
if (n < 0) {
SENTRY_WARNF("Failed to read attachment file: %s", file_path);
close(attach_fd);
return false;
}
if (write(fd, "\n", 1) != 1) {
SENTRY_WARN("Failed to write newline to envelope");
}
close(attach_fd);
#elif defined(SENTRY_PLATFORM_WINDOWS)
int n;
while ((n = _read(attach_fd, buf, sizeof(buf))) > 0) {
int written = _write(fd, buf, (unsigned int)n);
if (written != n) {
SENTRY_WARNF(
"Failed to write attachment content for: %s", file_path);
_close(attach_fd);
return false;
}
}
if (n < 0) {
SENTRY_WARNF("Failed to read attachment file: %s", file_path);
_close(attach_fd);
return false;
}
_write(fd, "\n", 1);
_close(attach_fd);
#endif
return true;
}
static bool
attachment_is_placeholder(const sentry_options_t *options, const char *path)
{
sentry_attachment_t attachment = { 0 };
attachment.path = sentry__path_from_str(path);
if (!attachment.path) {
return false;
}
bool is_placeholder
= sentry__attachment_is_placeholder(&attachment, options);
sentry__path_free(attachment.path);
return is_placeholder;
}
// For each large attachment listed in `<run_folder>/__sentry-attachments`,
// cache it as an attachment-ref item. Small attachments were already inlined
// during envelope writing.
static void
add_attachment_refs(sentry_envelope_t *envelope,
const sentry_options_t *options, const sentry_path_t *run_folder)
{
if (!envelope || !options || !options->run
|| !options->enable_large_attachments || !run_folder) {
return;
}
sentry_path_t *attach_list_path
= sentry__path_join_str(run_folder, "__sentry-attachments");
if (!attach_list_path) {
SENTRY_WARN("Failed to resolve attachment manifest path");
return;
}
size_t attach_json_len = 0;
char *attach_json
= sentry__path_read_to_buffer(attach_list_path, &attach_json_len);
sentry__path_free(attach_list_path);
if (!attach_json) {
return;
}
sentry_value_t list = attach_json_len > 0
? sentry__value_from_json(attach_json, attach_json_len)
: sentry_value_new_null();
sentry_free(attach_json);
if (sentry_value_is_null(list)) {
SENTRY_WARN("Failed to parse attachment manifest");
return;
}
bool materialized = false;
size_t len = sentry_value_get_length(list);
for (size_t i = 0; i < len; i++) {
sentry_value_t info = sentry_value_get_by_index(list, i);
const char *path
= sentry_value_as_string(sentry_value_get_by_key(info, "path"));
const char *filename
= sentry_value_as_string(sentry_value_get_by_key(info, "filename"));
const char *attachment_type = sentry_value_as_string(
sentry_value_get_by_key(info, "attachment_type"));
const char *content_type = sentry_value_as_string(
sentry_value_get_by_key(info, "content_type"));
if (sentry__string_empty(path) || sentry__string_empty(filename)) {
SENTRY_WARN("Skipping malformed attachment manifest entry");
continue;
}
sentry_attachment_t attachment = { 0 };
attachment.path = sentry__path_from_str(path);
attachment.filename = sentry__path_from_str(filename);
if (!attachment.path || !attachment.filename) {
SENTRY_WARNF("Failed to allocate attachment paths for: %s", path);
sentry__path_free(attachment.path);
sentry__path_free(attachment.filename);
continue;
}
attachment.type
= (char *)((attachment_type && *attachment_type) ? attachment_type
: NULL);
attachment.content_type
= sentry__string_empty(content_type) ? NULL : (char *)content_type;
if (!sentry__attachment_is_placeholder(&attachment, options)) {
sentry__path_free(attachment.path);
sentry__path_free(attachment.filename);
continue;
}
if (!materialized && !sentry__envelope_materialize(envelope)) {
SENTRY_WARN("Failed to materialize envelope for attachment-refs");
sentry__path_free(attachment.path);
sentry__path_free(attachment.filename);
break;
}
materialized = true;
if (!sentry__cache_attachment_ref(
envelope, &attachment, options->run->cache_path, NULL)) {
SENTRY_WARN("failed to cache attachment-ref");
}
sentry__path_free(attachment.path);
sentry__path_free(attachment.filename);
}
sentry_value_decref(list);
}
#if defined(SENTRY_PLATFORM_UNIX)
/**
* Get signal name from signal number (Unix platforms only)
*/
static const char *
get_signal_name(int signum)
{
switch (signum) {
case SIGABRT:
return "SIGABRT";
case SIGBUS:
return "SIGBUS";
case SIGFPE:
return "SIGFPE";
case SIGILL:
return "SIGILL";
case SIGSEGV:
return "SIGSEGV";
case SIGSYS:
return "SIGSYS";
case SIGTRAP:
return "SIGTRAP";
default:
return "UNKNOWN";
}
}
#endif
/**
* Build registers value from crash context for a specific thread.
*
* @param ctx The crash context
* @param thread_idx Index of the thread in ctx->platform.threads[]
* Pass SIZE_MAX to use the crashed thread context
* @return Registers value object
*/
static sentry_value_t
build_registers_from_ctx(const sentry_crash_context_t *ctx, size_t thread_idx)
{
sentry_value_t registers = sentry_value_new_object();
#if defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID)
// Use thread-specific context, defaulting to crashed thread
const ucontext_t *uctx = &ctx->platform.context;
if (thread_idx != SIZE_MAX && ctx->platform.num_threads > 0
&& thread_idx < ctx->platform.num_threads) {
uctx = &ctx->platform.threads[thread_idx].context;
}
# if defined(__x86_64__)
uintptr_t *mctx = (uintptr_t *)&uctx->uc_mcontext;
sentry_value_set_by_key(
registers, "r8", sentry__value_new_addr((uint64_t)mctx[0]));
sentry_value_set_by_key(
registers, "r9", sentry__value_new_addr((uint64_t)mctx[1]));
sentry_value_set_by_key(
registers, "r10", sentry__value_new_addr((uint64_t)mctx[2]));
sentry_value_set_by_key(
registers, "r11", sentry__value_new_addr((uint64_t)mctx[3]));
sentry_value_set_by_key(
registers, "r12", sentry__value_new_addr((uint64_t)mctx[4]));
sentry_value_set_by_key(
registers, "r13", sentry__value_new_addr((uint64_t)mctx[5]));
sentry_value_set_by_key(
registers, "r14", sentry__value_new_addr((uint64_t)mctx[6]));
sentry_value_set_by_key(
registers, "r15", sentry__value_new_addr((uint64_t)mctx[7]));
sentry_value_set_by_key(
registers, "rdi", sentry__value_new_addr((uint64_t)mctx[8]));
sentry_value_set_by_key(
registers, "rsi", sentry__value_new_addr((uint64_t)mctx[9]));
sentry_value_set_by_key(
registers, "rbp", sentry__value_new_addr((uint64_t)mctx[10]));
sentry_value_set_by_key(
registers, "rbx", sentry__value_new_addr((uint64_t)mctx[11]));
sentry_value_set_by_key(
registers, "rdx", sentry__value_new_addr((uint64_t)mctx[12]));
sentry_value_set_by_key(
registers, "rax", sentry__value_new_addr((uint64_t)mctx[13]));
sentry_value_set_by_key(
registers, "rcx", sentry__value_new_addr((uint64_t)mctx[14]));
sentry_value_set_by_key(
registers, "rsp", sentry__value_new_addr((uint64_t)mctx[15]));
sentry_value_set_by_key(
registers, "rip", sentry__value_new_addr((uint64_t)mctx[16]));
# elif defined(__aarch64__)
// Use struct field access instead of raw pointer indexing because
// struct sigcontext has fault_address before regs[31] on aarch64.
for (int i = 0; i < 29; i++) {
char name[4];
snprintf(name, sizeof(name), "x%d", i);
sentry_value_set_by_key(
registers, name, sentry__value_new_addr(uctx->uc_mcontext.regs[i]));
}
sentry_value_set_by_key(
registers, "fp", sentry__value_new_addr(uctx->uc_mcontext.regs[29]));
sentry_value_set_by_key(
registers, "lr", sentry__value_new_addr(uctx->uc_mcontext.regs[30]));
sentry_value_set_by_key(
registers, "sp", sentry__value_new_addr(uctx->uc_mcontext.sp));
sentry_value_set_by_key(
registers, "pc", sentry__value_new_addr(uctx->uc_mcontext.pc));
# endif
#elif defined(SENTRY_PLATFORM_MACOS)
// Use thread-specific context, defaulting to crashed thread
const _STRUCT_MCONTEXT *mctx = &ctx->platform.mcontext;
if (thread_idx != SIZE_MAX && ctx->platform.num_threads > 0
&& thread_idx < ctx->platform.num_threads) {
mctx = &ctx->platform.threads[thread_idx].state;
}
# if defined(__x86_64__)
sentry_value_set_by_key(
registers, "rax", sentry__value_new_addr(mctx->__ss.__rax));
sentry_value_set_by_key(
registers, "rbx", sentry__value_new_addr(mctx->__ss.__rbx));
sentry_value_set_by_key(
registers, "rcx", sentry__value_new_addr(mctx->__ss.__rcx));
sentry_value_set_by_key(
registers, "rdx", sentry__value_new_addr(mctx->__ss.__rdx));
sentry_value_set_by_key(
registers, "rdi", sentry__value_new_addr(mctx->__ss.__rdi));
sentry_value_set_by_key(
registers, "rsi", sentry__value_new_addr(mctx->__ss.__rsi));
sentry_value_set_by_key(
registers, "rbp", sentry__value_new_addr(mctx->__ss.__rbp));
sentry_value_set_by_key(
registers, "rsp", sentry__value_new_addr(mctx->__ss.__rsp));
sentry_value_set_by_key(
registers, "r8", sentry__value_new_addr(mctx->__ss.__r8));
sentry_value_set_by_key(
registers, "r9", sentry__value_new_addr(mctx->__ss.__r9));
sentry_value_set_by_key(
registers, "r10", sentry__value_new_addr(mctx->__ss.__r10));
sentry_value_set_by_key(
registers, "r11", sentry__value_new_addr(mctx->__ss.__r11));
sentry_value_set_by_key(
registers, "r12", sentry__value_new_addr(mctx->__ss.__r12));
sentry_value_set_by_key(
registers, "r13", sentry__value_new_addr(mctx->__ss.__r13));
sentry_value_set_by_key(
registers, "r14", sentry__value_new_addr(mctx->__ss.__r14));
sentry_value_set_by_key(
registers, "r15", sentry__value_new_addr(mctx->__ss.__r15));
sentry_value_set_by_key(
registers, "rip", sentry__value_new_addr(mctx->__ss.__rip));
# elif defined(__aarch64__)
for (int i = 0; i < 29; i++) {
char name[4];
snprintf(name, sizeof(name), "x%d", i);
sentry_value_set_by_key(
registers, name, sentry__value_new_addr(mctx->__ss.__x[i]));
}
sentry_value_set_by_key(registers, "fp",
sentry__value_new_addr(SENTRY__ARM64_GET_FP(mctx->__ss)));
sentry_value_set_by_key(registers, "lr",
sentry__value_new_addr(SENTRY__ARM64_GET_LR(mctx->__ss)));
sentry_value_set_by_key(registers, "sp",
sentry__value_new_addr(SENTRY__ARM64_GET_SP(mctx->__ss)));
sentry_value_set_by_key(registers, "pc",
sentry__value_new_addr(SENTRY__ARM64_GET_PC(mctx->__ss)));
# endif
#elif defined(SENTRY_PLATFORM_WINDOWS)
// Use thread-specific context, defaulting to crashed thread
const CONTEXT *wctx = &ctx->platform.context;
if (thread_idx != SIZE_MAX && ctx->platform.num_threads > 0
&& thread_idx < ctx->platform.num_threads) {
wctx = &ctx->platform.threads[thread_idx].context;
}
# if defined(_M_AMD64)
sentry_value_set_by_key(
registers, "rax", sentry__value_new_addr(wctx->Rax));
sentry_value_set_by_key(
registers, "rbx", sentry__value_new_addr(wctx->Rbx));
sentry_value_set_by_key(
registers, "rcx", sentry__value_new_addr(wctx->Rcx));
sentry_value_set_by_key(
registers, "rdx", sentry__value_new_addr(wctx->Rdx));
sentry_value_set_by_key(
registers, "rdi", sentry__value_new_addr(wctx->Rdi));
sentry_value_set_by_key(
registers, "rsi", sentry__value_new_addr(wctx->Rsi));
sentry_value_set_by_key(
registers, "rbp", sentry__value_new_addr(wctx->Rbp));
sentry_value_set_by_key(
registers, "rsp", sentry__value_new_addr(wctx->Rsp));
sentry_value_set_by_key(registers, "r8", sentry__value_new_addr(wctx->R8));
sentry_value_set_by_key(registers, "r9", sentry__value_new_addr(wctx->R9));
sentry_value_set_by_key(
registers, "r10", sentry__value_new_addr(wctx->R10));
sentry_value_set_by_key(
registers, "r11", sentry__value_new_addr(wctx->R11));
sentry_value_set_by_key(
registers, "r12", sentry__value_new_addr(wctx->R12));
sentry_value_set_by_key(
registers, "r13", sentry__value_new_addr(wctx->R13));
sentry_value_set_by_key(
registers, "r14", sentry__value_new_addr(wctx->R14));
sentry_value_set_by_key(
registers, "r15", sentry__value_new_addr(wctx->R15));
sentry_value_set_by_key(
registers, "rip", sentry__value_new_addr(wctx->Rip));
# elif defined(_M_IX86)
sentry_value_set_by_key(
registers, "eax", sentry__value_new_addr(wctx->Eax));
sentry_value_set_by_key(
registers, "ebx", sentry__value_new_addr(wctx->Ebx));
sentry_value_set_by_key(
registers, "ecx", sentry__value_new_addr(wctx->Ecx));
sentry_value_set_by_key(
registers, "edx", sentry__value_new_addr(wctx->Edx));
sentry_value_set_by_key(
registers, "edi", sentry__value_new_addr(wctx->Edi));
sentry_value_set_by_key(
registers, "esi", sentry__value_new_addr(wctx->Esi));
sentry_value_set_by_key(
registers, "ebp", sentry__value_new_addr(wctx->Ebp));
sentry_value_set_by_key(
registers, "esp", sentry__value_new_addr(wctx->Esp));
sentry_value_set_by_key(
registers, "eip", sentry__value_new_addr(wctx->Eip));
# elif defined(_M_ARM64)
for (int i = 0; i < 29; i++) {
char name[4];
snprintf(name, sizeof(name), "x%d", i);
sentry_value_set_by_key(
registers, name, sentry__value_new_addr(wctx->X[i]));
}
sentry_value_set_by_key(registers, "fp", sentry__value_new_addr(wctx->Fp));
sentry_value_set_by_key(registers, "lr", sentry__value_new_addr(wctx->Lr));
sentry_value_set_by_key(registers, "sp", sentry__value_new_addr(wctx->Sp));
sentry_value_set_by_key(registers, "pc", sentry__value_new_addr(wctx->Pc));
# endif
#endif
return registers;
}
#if defined(SENTRY_PLATFORM_LINUX)
static sentry_value_t
build_registers_from_remote_registers(
const sentry_remote_registers_t *remote_registers)
{
sentry_value_t registers = sentry_value_new_object();
for (size_t i = 0; i < remote_registers->count; i++) {
const sentry_remote_register_t *remote_register
= &remote_registers->values[i];
sentry_value_set_by_key(registers, remote_register->name,
sentry__value_new_addr(remote_register->value));
}
return registers;
}
#endif
/**
* Maximum number of frames to unwind
*/
#define MAX_STACK_FRAMES 128
/**
* Read a pointer-sized value from the stack buffer.
* Returns true if successful, false if address is outside the buffer.
*/
static bool
read_stack_value(const uint8_t *stack_buf, uint64_t stack_start,
uint64_t stack_size, uint64_t addr, uint64_t *out_value)
{
if (addr < stack_start
|| addr + sizeof(uint64_t) > stack_start + stack_size) {
return false;
}
uint64_t offset = addr - stack_start;
memcpy(out_value, stack_buf + offset, sizeof(uint64_t));
return true;
}
/**
* Check if an address looks like a valid code pointer.
* Basic sanity check to avoid garbage in the stacktrace.
*/
static bool
is_valid_code_addr(uint64_t addr)
{
// Must be non-null and in typical code range
if (addr == 0 || addr < 0x1000) {
return false;
}
#if defined(__x86_64__) || defined(_M_AMD64)
// On x86_64, user space is below the canonical address boundary
if (addr > 0x00007FFFFFFFFFFF) {
return false;
}
#elif defined(__aarch64__) || defined(_M_ARM64)
// On ARM64 with 48-bit VA, user space is typically 0x0 to 0xFFFF_FFFF_FFFF
// Kernel space starts at 0xFFFF_0000_0000_0000
// Addresses like 0xAAAA_xxxx are valid user space addresses with ASLR
if (addr >= 0xFFFF000000000000ULL) {
return false; // Kernel space
}
#endif
return true;
}
/**
* Find the module containing the given address and add module info to frame.
* Sets 'package' (module name) and 'image_addr' on the frame if found.
*
* @param ctx The crash context containing module list
* @param frame The frame value to enrich
* @param addr The instruction address to look up
*/
static void
enrich_frame_with_module_info(
const sentry_crash_context_t *ctx, sentry_value_t frame, uint64_t addr)
{
for (uint32_t i = 0; i < ctx->module_count; i++) {
const sentry_module_info_t *mod = &ctx->modules[i];
if (addr >= mod->base_address && addr < mod->base_address + mod->size) {
// Set package to full module path (matches minidump format)
sentry_value_set_by_key(
frame, "package", sentry_value_new_string(mod->name));
// Note: Do NOT set image_addr on frames - it's not present in
// minidump-derived events and may cause symbolicator issues
SENTRY_DEBUGF("Frame 0x%llx -> module %s", (unsigned long long)addr,
mod->name);
return;
}
}
// No matching module found - log for debugging
SENTRY_DEBUGF("Frame 0x%llx NOT matched to any module (module_count=%u)",
(unsigned long long)addr, ctx->module_count);
}
#if defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID)
static void enrich_frame_with_symbol(
const sentry_crash_context_t *ctx, sentry_value_t frame, uint64_t addr);
#endif
/**
* Build stacktrace frames for a specific thread using frame pointer-based
* unwinding. Reads the captured stack memory and walks the frame chain.
*
* @param ctx The crash context
* @param thread_idx Index of the thread in ctx->platform.threads[]
* Pass SIZE_MAX to use the crashed thread from mcontext
* @return Stacktrace value with frames array
*/
static sentry_value_t
build_stacktrace_for_thread(
const sentry_crash_context_t *ctx, size_t thread_idx)
{
sentry_value_t stacktrace = sentry_value_new_object();
sentry_value_t frames = sentry_value_new_list();
// Suppress unused parameter warning on platforms where thread_idx isn't
// used
(void)thread_idx;
// Get instruction pointer and frame pointer from crash context
uint64_t ip = 0;
uint64_t fp = 0;
uint64_t sp = 0;
#if defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID)
// Use thread-specific context, defaulting to crashed thread
const ucontext_t *thread_context = &ctx->platform.context;
if (thread_idx != SIZE_MAX && ctx->platform.num_threads > 0
&& thread_idx < ctx->platform.num_threads) {
thread_context = &ctx->platform.threads[thread_idx].context;
}
# if defined(__x86_64__)
ip = (uint64_t)thread_context->uc_mcontext.gregs[REG_RIP];
fp = (uint64_t)thread_context->uc_mcontext.gregs[REG_RBP];
sp = (uint64_t)thread_context->uc_mcontext.gregs[REG_RSP];
# elif defined(__aarch64__)
ip = (uint64_t)thread_context->uc_mcontext.pc;
fp = (uint64_t)thread_context->uc_mcontext.regs[29]; // x29 is FP
sp = (uint64_t)thread_context->uc_mcontext.sp;
# elif defined(__i386__)
ip = (uint64_t)thread_context->uc_mcontext.gregs[REG_EIP];
fp = (uint64_t)thread_context->uc_mcontext.gregs[REG_EBP];
sp = (uint64_t)thread_context->uc_mcontext.gregs[REG_ESP];
# elif defined(__arm__)
ip = (uint64_t)thread_context->uc_mcontext.arm_pc;
fp = (uint64_t)thread_context->uc_mcontext.arm_fp;
sp = (uint64_t)thread_context->uc_mcontext.arm_sp;
# endif
#elif defined(SENTRY_PLATFORM_MACOS)
# if defined(__x86_64__)
ip = ctx->platform.mcontext.__ss.__rip;
fp = ctx->platform.mcontext.__ss.__rbp;
sp = ctx->platform.mcontext.__ss.__rsp;
# elif defined(__aarch64__)
ip = SENTRY__ARM64_GET_PC(ctx->platform.mcontext.__ss);
fp = SENTRY__ARM64_GET_FP(ctx->platform.mcontext.__ss);
sp = SENTRY__ARM64_GET_SP(ctx->platform.mcontext.__ss);
# endif
#elif defined(SENTRY_PLATFORM_WINDOWS)
// Use thread-specific context, defaulting to crashed thread
const CONTEXT *thread_context = &ctx->platform.context;
if (thread_idx != SIZE_MAX && ctx->platform.num_threads > 0
&& thread_idx < ctx->platform.num_threads) {
thread_context = &ctx->platform.threads[thread_idx].context;
}
# if defined(_M_AMD64)
ip = thread_context->Rip;
fp = thread_context->Rbp;
sp = thread_context->Rsp;
# elif defined(_M_IX86)
ip = thread_context->Eip;
fp = thread_context->Ebp;
sp = thread_context->Esp;
# elif defined(_M_ARM64)
ip = thread_context->Pc;
fp = thread_context->Fp;
sp = thread_context->Sp;
# endif
#endif
(void)sp; // May be unused depending on platform
// Try to read stack memory from the captured stack file or process memory
uint8_t *stack_buf = NULL;
uint64_t stack_start = 0;
uint64_t stack_size = 0;
#if defined(SENTRY_PLATFORM_MACOS)
// On macOS, stack is saved to a file by the signal handler.
// Use the specified thread index, or find the crashed thread if SIZE_MAX.
if (ctx->platform.num_threads > 0) {
size_t idx = thread_idx;
// If SIZE_MAX, find the crashed thread
if (idx == SIZE_MAX) {
idx = 0;
for (size_t i = 0; i < ctx->platform.num_threads; i++) {
if (ctx->platform.threads[i].tid
== (uint64_t)ctx->crashed_tid) {
idx = i;
break;
}
}
}
// Validate index
if (idx >= ctx->platform.num_threads) {
SENTRY_WARNF("Invalid thread index %zu (max %zu)", idx,
ctx->platform.num_threads);
sentry_value_set_by_key(stacktrace, "frames", frames);
return stacktrace;
}
const sentry_thread_context_darwin_t *thread
= &ctx->platform.threads[idx];
// Use IP/FP/SP from the thread state (matches saved stack)
# if defined(__x86_64__)
ip = thread->state.__ss.__rip;
fp = thread->state.__ss.__rbp;
sp = thread->state.__ss.__rsp;
# elif defined(__aarch64__)
ip = SENTRY__ARM64_GET_PC(thread->state.__ss);
fp = SENTRY__ARM64_GET_FP(thread->state.__ss);
sp = SENTRY__ARM64_GET_SP(thread->state.__ss);
# endif
SENTRY_DEBUGF("Thread %zu: IP=0x%llx FP=0x%llx SP=0x%llx", idx,
(unsigned long long)ip, (unsigned long long)fp,
(unsigned long long)sp);
const char *stack_path = thread->stack_path;
stack_size = thread->stack_size;
if (stack_path[0] != '\0' && stack_size > 0) {
int stack_fd = open(stack_path, O_RDONLY);
if (stack_fd >= 0) {
stack_buf = sentry_malloc(stack_size);
if (stack_buf) {
ssize_t bytes_read = read(stack_fd, stack_buf, stack_size);
if (bytes_read == (ssize_t)stack_size) {
// Stack was captured from SP upward
stack_start = sp;
SENTRY_DEBUGF(
"Loaded stack: start=0x%llx size=%llu, FP offset "
"from SP=%lld",
(unsigned long long)stack_start,
(unsigned long long)stack_size,
(long long)(fp - sp));
} else {
SENTRY_WARNF(
"Stack read failed: got %zd, expected %llu",
bytes_read, (unsigned long long)stack_size);
sentry_free(stack_buf);
stack_buf = NULL;
}
}
close(stack_fd);
} else {
SENTRY_WARNF("Failed to open stack file: %s", stack_path);
}
} else {
SENTRY_DEBUGF("No stack file for thread %zu", idx);
}
}
#elif defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID)
// On Linux, use process_vm_readv to read stack memory from crashed process
if (ctx->platform.num_threads > 0) {
pid_t pid = ctx->crashed_pid;
stack_size = SENTRY_CRASH_MAX_STACK_CAPTURE;
stack_start = sp;
stack_buf = sentry_malloc(stack_size);
if (stack_buf) {
struct iovec local_iov
= { .iov_base = stack_buf, .iov_len = stack_size };
struct iovec remote_iov
= { .iov_base = (void *)stack_start, .iov_len = stack_size };
ssize_t bytes_read
= process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0);
if (bytes_read <= 0) {
SENTRY_DEBUG(
"process_vm_readv failed, falling back to single frame");
sentry_free(stack_buf);
stack_buf = NULL;
} else {
stack_size = (uint64_t)bytes_read;
SENTRY_DEBUGF(
"Read %zd bytes of stack from process %d", bytes_read, pid);
}
}
}
#elif defined(SENTRY_PLATFORM_WINDOWS)
// On Windows, use StackWalk64 for proper stack unwinding
// This uses PE unwind info and works reliably on x64/ARM64
// Get thread-specific context for stack walking
const CONTEXT *walk_context = &ctx->platform.context;
DWORD walk_thread_id = (DWORD)ctx->crashed_tid;
if (thread_idx != SIZE_MAX && ctx->platform.num_threads > 0
&& thread_idx < ctx->platform.num_threads) {
const sentry_thread_context_windows_t *tctx
= &ctx->platform.threads[thread_idx];
walk_context = &tctx->context;
walk_thread_id = tctx->thread_id;
}
HANDLE hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION,
FALSE, (DWORD)ctx->crashed_pid);
if (hProcess) {
sentry_frame_info_t stack_frames[MAX_STACK_FRAMES];
// Make a copy since StackWalk64 may modify the context
CONTEXT ctx_copy = *walk_context;
size_t dbghelp_frame_count = walk_stack_with_dbghelp(hProcess,
walk_thread_id, &ctx_copy, stack_frames, MAX_STACK_FRAMES);
if (dbghelp_frame_count > 0) {
// Build sentry frames from StackWalk64 results
sentry_value_t temp_frames[MAX_STACK_FRAMES];
int frame_count = 0;
for (size_t i = 0;
i < dbghelp_frame_count && frame_count < MAX_STACK_FRAMES;
i++) {
uint64_t frame_addr
= (uint64_t)(uintptr_t)stack_frames[i].instruction_addr;
temp_frames[frame_count] = sentry_value_new_object();
sentry_value_set_by_key(temp_frames[frame_count],
"instruction_addr", sentry__value_new_addr(frame_addr));
// First frame is from context, rest are from CFI unwinding
sentry_value_set_by_key(temp_frames[frame_count], "trust",
sentry_value_new_string(i == 0 ? "context" : "cfi"));
enrich_frame_with_module_info(
ctx, temp_frames[frame_count], frame_addr);
if (stack_frames[i].symbol) {
sentry_value_set_by_key(temp_frames[frame_count],
"function",
sentry_value_new_string(stack_frames[i].symbol));
}
if (stack_frames[i].symbol_addr) {
sentry_value_set_by_key(temp_frames[frame_count],
"symbol_addr",
sentry__value_new_addr(
(uint64_t)(uintptr_t)stack_frames[i].symbol_addr));
}
frame_count++;
}
for (size_t i = 0; i < dbghelp_frame_count; i++) {
sentry_free((char *)stack_frames[i].symbol);
}
// Sentry expects frames in reverse order (outermost caller first)
for (int i = frame_count - 1; i >= 0; i--) {
sentry_value_append(frames, temp_frames[i]);
}
sentry_value_set_by_key(stacktrace, "frames", frames);
sentry_value_set_by_key(stacktrace, "registers",
build_registers_from_ctx(ctx, thread_idx));
CloseHandle(hProcess);
return stacktrace;
}
CloseHandle(hProcess);
} else {
SENTRY_WARNF("Failed to open process %d for stack walk (error %lu)",
ctx->crashed_pid, GetLastError());
}
// Fall through to add at least the IP frame below
#endif
// Build frame list - collect in callee-first order, then reverse for Sentry
sentry_value_t temp_frames[MAX_STACK_FRAMES];
int frame_count = 0;
#if defined(SENTRY_PLATFORM_LINUX)
// Remote DWARF unwinding via libunwind ptrace accessors. Do not attach to
// the crashed thread from the daemon; use the saved fault context below.
{
pid_t tid = 0;
if (thread_idx == SIZE_MAX) {
tid = ctx->crashed_tid;
} else if (thread_idx < ctx->platform.num_threads) {
tid = ctx->platform.threads[thread_idx].tid;
}
bool is_crashed_thread
= thread_idx == SIZE_MAX || tid == ctx->crashed_tid;
if (tid > 0 && !is_crashed_thread) {
sentry_remote_registers_t registers = { 0 };
sentry_remote_frame_t *remote_frames
= sentry_malloc(sizeof(*remote_frames) * MAX_STACK_FRAMES);
size_t remote_count = remote_frames
? sentry__unwind_stack_from_thread(
tid, remote_frames, MAX_STACK_FRAMES, ®isters)
: 0;
if (remote_count > 0) {
SENTRY_DEBUGF("Remote unwound %zu frames for thread %d",
remote_count, tid);
for (size_t i = 0;
i < remote_count && frame_count < MAX_STACK_FRAMES; i++) {
if (remote_frames[i].ip == 0
|| !is_valid_code_addr(remote_frames[i].ip)) {
continue;
}
temp_frames[frame_count] = sentry_value_new_object();
sentry_value_set_by_key(temp_frames[frame_count],
"instruction_addr",
sentry__value_new_addr(remote_frames[i].ip));
// Trust describes the unwind source, not the emitted
// frame index. If the initial cursor frame is filtered
// out, the next emitted frame was still reached via CFI.
sentry_value_set_by_key(temp_frames[frame_count], "trust",
sentry_value_new_string(i == 0 ? "context" : "cfi"));
enrich_frame_with_module_info(
ctx, temp_frames[frame_count], remote_frames[i].ip);
if (remote_frames[i].symbol[0]) {
sentry_value_set_by_key(temp_frames[frame_count],
"function",
sentry_value_new_string(remote_frames[i].symbol));
}
frame_count++;
}
if (frame_count > 0) {
if (stack_buf) {
sentry_free(stack_buf);
}
for (int i = frame_count - 1; i >= 0; i--) {
sentry_value_append(frames, temp_frames[i]);
}
sentry_value_set_by_key(stacktrace, "frames", frames);
if (registers.count > 0) {
sentry_value_set_by_key(stacktrace, "registers",
build_registers_from_remote_registers(®isters));
}
sentry_free(remote_frames);
return stacktrace;
}
}
if (remote_frames) {
sentry_free(remote_frames);