-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
SentryCrashReport.c
1788 lines (1641 loc) · 64.4 KB
/
SentryCrashReport.c
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
// Adapted from: https://github.com/kstenerud/KSCrash
//
// SentryCrashReport.m
//
// Created by Karl Stenerud on 2012-01-28.
//
// Copyright (c) 2012 Karl Stenerud. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall remain in place
// in this source code.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include "SentryCrashReport.h"
#include "SentryCrashBinaryImageCache.h"
#include "SentryCrashCPU.h"
#include "SentryCrashCachedData.h"
#include "SentryCrashDynamicLinker.h"
#include "SentryCrashFileUtils.h"
#include "SentryCrashJSONCodec.h"
#include "SentryCrashMach.h"
#include "SentryCrashMemory.h"
#include "SentryCrashObjC.h"
#include "SentryCrashReportFields.h"
#include "SentryCrashReportVersion.h"
#include "SentryCrashReportWriter.h"
#include "SentryCrashSignalInfo.h"
#include "SentryCrashStackCursor_Backtrace.h"
#include "SentryCrashStackCursor_MachineContext.h"
#include "SentryCrashString.h"
#include "SentryCrashThread.h"
#include "SentryCrashUUIDConversion.h"
#include "SentryInternalCDefines.h"
#include "SentryScopeSyncC.h"
#include "SentryAsyncSafeLog.h"
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// ============================================================================
#pragma mark - Constants -
// ============================================================================
/** Default number of objects, subobjects, and ivars to record from a memory loc
*/
#define kDefaultMemorySearchDepth 15
/** How far to search the stack (in pointer sized jumps) for notable data. */
#define kStackNotableSearchBackDistance 20
#define kStackNotableSearchForwardDistance 10
/** How much of the stack to dump (in pointer sized jumps). */
#define kStackContentsPushedDistance 20
#define kStackContentsPoppedDistance 10
#define kStackContentsTotalDistance (kStackContentsPushedDistance + kStackContentsPoppedDistance)
/** The minimum length for a valid string. */
#define kMinStringLength 4
// ============================================================================
#pragma mark - JSON Encoding -
// ============================================================================
#define getJsonContext(REPORT_WRITER) ((SentryCrashJSONEncodeContext *)((REPORT_WRITER)->context))
// ============================================================================
#pragma mark - Runtime Config -
// ============================================================================
typedef struct {
/** If YES, introspect memory contents during a crash.
* Any Objective-C objects or C strings near the stack pointer or referenced
* by cpu registers or exceptions will be recorded in the crash report,
* along with their contents.
*/
bool enabled;
/** List of classes that should never be introspected.
* Whenever a class in this list is encountered, only the class name will be
* recorded.
*/
const char **restrictedClasses;
int restrictedClassesCount;
} SentryCrash_IntrospectionRules;
static const char *g_userInfoJSON;
static SentryCrash_IntrospectionRules g_introspectionRules;
#pragma mark Callbacks
static void
addBooleanElement(
const SentryCrashReportWriter *const writer, const char *const key, const bool value)
{
sentrycrashjson_addBooleanElement(getJsonContext(writer), key, value);
}
static void
addFloatingPointElement(
const SentryCrashReportWriter *const writer, const char *const key, const double value)
{
sentrycrashjson_addFloatingPointElement(getJsonContext(writer), key, value);
}
static void
addIntegerElement(
const SentryCrashReportWriter *const writer, const char *const key, const int64_t value)
{
sentrycrashjson_addIntegerElement(getJsonContext(writer), key, value);
}
static void
addUIntegerElement(
const SentryCrashReportWriter *const writer, const char *const key, const uint64_t value)
{
sentrycrashjson_addUIntegerElement(getJsonContext(writer), key, value);
}
static void
addStringElement(
const SentryCrashReportWriter *const writer, const char *const key, const char *const value)
{
sentrycrashjson_addStringElement(
getJsonContext(writer), key, value, SentryCrashJSON_SIZE_AUTOMATIC);
}
static void
addTextFileElement(
const SentryCrashReportWriter *const writer, const char *const key, const char *const filePath)
{
const int fd = open(filePath, O_RDONLY);
if (fd < 0) {
SENTRY_ASYNC_SAFE_LOG_ERROR("Could not open file %s: %s", filePath, strerror(errno));
return;
}
if (sentrycrashjson_beginStringElement(getJsonContext(writer), key) != SentryCrashJSON_OK) {
SENTRY_ASYNC_SAFE_LOG_ERROR("Could not start string element");
goto done;
}
char buffer[512];
int bytesRead;
for (bytesRead = (int)read(fd, buffer, sizeof(buffer)); bytesRead > 0;
bytesRead = (int)read(fd, buffer, sizeof(buffer))) {
if (sentrycrashjson_appendStringElement(getJsonContext(writer), buffer, bytesRead)
!= SentryCrashJSON_OK) {
SENTRY_ASYNC_SAFE_LOG_ERROR("Could not append string element");
goto done;
}
}
done:
sentrycrashjson_endStringElement(getJsonContext(writer));
close(fd);
}
static void
addDataElement(const SentryCrashReportWriter *const writer, const char *const key,
const char *const value, const int length)
{
sentrycrashjson_addDataElement(getJsonContext(writer), key, value, length);
}
static void
beginDataElement(const SentryCrashReportWriter *const writer, const char *const key)
{
sentrycrashjson_beginDataElement(getJsonContext(writer), key);
}
static void
appendDataElement(
const SentryCrashReportWriter *const writer, const char *const value, const int length)
{
sentrycrashjson_appendDataElement(getJsonContext(writer), value, length);
}
static void
endDataElement(const SentryCrashReportWriter *const writer)
{
sentrycrashjson_endDataElement(getJsonContext(writer));
}
static void
addUUIDElement(const SentryCrashReportWriter *const writer, const char *const key,
const unsigned char *const value)
{
if (value == NULL) {
sentrycrashjson_addNullElement(getJsonContext(writer), key);
} else {
int uuidLength = 36;
char uuidBuffer[uuidLength + 1]; // one for the null terminator
const unsigned char *src = value;
char *dst = uuidBuffer;
sentrycrashdl_convertBinaryImageUUID(src, dst);
sentrycrashjson_addStringElement(getJsonContext(writer), key, uuidBuffer, uuidLength);
}
}
static void
addJSONElement(const SentryCrashReportWriter *const writer, const char *const key,
const char *const jsonElement, bool closeLastContainer)
{
int jsonResult = sentrycrashjson_addJSONElement(
getJsonContext(writer), key, jsonElement, (int)strlen(jsonElement), closeLastContainer);
if (jsonResult != SentryCrashJSON_OK) {
char errorBuff[100];
snprintf(errorBuff, sizeof(errorBuff), "Invalid JSON data: %s",
sentrycrashjson_stringForError(jsonResult));
sentrycrashjson_beginObject(getJsonContext(writer), key);
sentrycrashjson_addStringElement(getJsonContext(writer), SentryCrashField_Error, errorBuff,
SentryCrashJSON_SIZE_AUTOMATIC);
sentrycrashjson_addStringElement(getJsonContext(writer), SentryCrashField_JSONData,
jsonElement, SentryCrashJSON_SIZE_AUTOMATIC);
sentrycrashjson_endContainer(getJsonContext(writer));
}
}
static void
addJSONElementFromFile(const SentryCrashReportWriter *const writer, const char *const key,
const char *const filePath, bool closeLastContainer)
{
sentrycrashjson_addJSONFromFile(getJsonContext(writer), key, filePath, closeLastContainer);
}
static void
beginObject(const SentryCrashReportWriter *const writer, const char *const key)
{
sentrycrashjson_beginObject(getJsonContext(writer), key);
}
static void
beginArray(const SentryCrashReportWriter *const writer, const char *const key)
{
sentrycrashjson_beginArray(getJsonContext(writer), key);
}
static void
endContainer(const SentryCrashReportWriter *const writer)
{
sentrycrashjson_endContainer(getJsonContext(writer));
}
static void
addTextLinesFromFile(
const SentryCrashReportWriter *const writer, const char *const key, const char *const filePath)
{
char readBuffer[1024];
SentryCrashBufferedReader reader;
if (!sentrycrashfu_openBufferedReader(&reader, filePath, readBuffer, sizeof(readBuffer))) {
return;
}
char buffer[1024];
beginArray(writer, key);
{
for (;;) {
int length = sizeof(buffer);
sentrycrashfu_readBufferedReaderUntilChar(&reader, '\n', buffer, &length);
if (length <= 0) {
break;
}
buffer[length - 1] = '\0';
sentrycrashjson_addStringElement(
getJsonContext(writer), NULL, buffer, SentryCrashJSON_SIZE_AUTOMATIC);
}
}
endContainer(writer);
sentrycrashfu_closeBufferedReader(&reader);
}
static int
addJSONData(const char *restrict const data, const int length, void *restrict userData)
{
SentryCrashBufferedWriter *writer = (SentryCrashBufferedWriter *)userData;
const bool success = sentrycrashfu_writeBufferedWriter(writer, data, length);
return success ? SentryCrashJSON_OK : SentryCrashJSON_ERROR_CANNOT_ADD_DATA;
}
// ============================================================================
#pragma mark - Utility -
// ============================================================================
/** Check if a memory address points to a valid null terminated UTF-8 string.
*
* @param address The address to check.
*
* @return true if the address points to a string.
*/
static bool
isValidString(const void *const address)
{
if ((void *)address == NULL) {
return false;
}
char buffer[500];
if ((uintptr_t)address + sizeof(buffer) < (uintptr_t)address) {
// Wrapped around the address range.
return false;
}
if (!sentrycrashmem_copySafely(address, buffer, sizeof(buffer))) {
return false;
}
return sentrycrashstring_isNullTerminatedUTF8String(buffer, kMinStringLength, sizeof(buffer));
}
/** Get the backtrace for the specified machine context.
*
* This function will choose how to fetch the backtrace based on the crash and
* machine context. It may store the backtrace in backtraceBuffer unless it can
* be fetched directly from memory. Do not count on backtraceBuffer containing
* anything. Always use the return value.
*
* @param crash The crash handler context.
*
* @param machineContext The machine context.
*
* @param cursor The stack cursor to fill.
*
* @return True if the cursor was filled.
*/
static bool
getStackCursor(const SentryCrash_MonitorContext *const crash,
const struct SentryCrashMachineContext *const machineContext, SentryCrashStackCursor *cursor)
{
if (sentrycrashmc_getThreadFromContext(machineContext)
== sentrycrashmc_getThreadFromContext(crash->offendingMachineContext)) {
*cursor = *((SentryCrashStackCursor *)crash->stackCursor);
return true;
}
sentrycrashsc_initWithMachineContext(
cursor, SentryCrashSC_STACK_OVERFLOW_THRESHOLD, machineContext);
return true;
}
// ============================================================================
#pragma mark - Report Writing -
// ============================================================================
/** Write the contents of a memory location.
* Also writes meta information about the data.
*
* @param writer The writer.
*
* @param key The object key, if needed.
*
* @param address The memory address.
*
* @param limit How many more subreferenced objects to write, if any.
*/
static void writeMemoryContents(const SentryCrashReportWriter *const writer, const char *const key,
const uintptr_t address, int *limit);
/** Write a string to the report.
* This will only print the first child of the array.
*
* @param writer The writer.
*
* @param key The object key, if needed.
*
* @param objectAddress The object's address.
*
* @param limit How many more subreferenced objects to write, if any.
*/
static void
writeNSStringContents(const SentryCrashReportWriter *const writer, const char *const key,
const uintptr_t objectAddress, __unused int *limit)
{
const void *object = (const void *)objectAddress;
char buffer[200];
if (sentrycrashobjc_copyStringContents(object, buffer, sizeof(buffer))) {
writer->addStringElement(writer, key, buffer);
}
}
/** Write a URL to the report.
* This will only print the first child of the array.
*
* @param writer The writer.
*
* @param key The object key, if needed.
*
* @param objectAddress The object's address.
*
* @param limit How many more subreferenced objects to write, if any.
*/
static void
writeURLContents(const SentryCrashReportWriter *const writer, const char *const key,
const uintptr_t objectAddress, __unused int *limit)
{
const void *object = (const void *)objectAddress;
char buffer[200];
if (sentrycrashobjc_copyStringContents(object, buffer, sizeof(buffer))) {
writer->addStringElement(writer, key, buffer);
}
}
/** Write a date to the report.
* This will only print the first child of the array.
*
* @param writer The writer.
*
* @param key The object key, if needed.
*
* @param objectAddress The object's address.
*
* @param limit How many more subreferenced objects to write, if any.
*/
static void
writeDateContents(const SentryCrashReportWriter *const writer, const char *const key,
const uintptr_t objectAddress, __unused int *limit)
{
const void *object = (const void *)objectAddress;
writer->addFloatingPointElement(writer, key, sentrycrashobjc_dateContents(object));
}
/** Write a number to the report.
* This will only print the first child of the array.
*
* @param writer The writer.
*
* @param key The object key, if needed.
*
* @param objectAddress The object's address.
*
* @param limit How many more subreferenced objects to write, if any.
*/
static void
writeNumberContents(const SentryCrashReportWriter *const writer, const char *const key,
const uintptr_t objectAddress, __unused int *limit)
{
const void *object = (const void *)objectAddress;
writer->addFloatingPointElement(writer, key, sentrycrashobjc_numberAsFloat(object));
}
/** Write an array to the report.
* This will only print the first child of the array.
*
* @param writer The writer.
*
* @param key The object key, if needed.
*
* @param objectAddress The object's address.
*
* @param limit How many more subreferenced objects to write, if any.
*/
static void
writeArrayContents(const SentryCrashReportWriter *const writer, const char *const key,
const uintptr_t objectAddress, int *limit)
{
const void *object = (const void *)objectAddress;
uintptr_t firstObject;
if (sentrycrashobjc_arrayContents(object, &firstObject, 1) == 1) {
writeMemoryContents(writer, key, firstObject, limit);
}
}
/** Write out ivar information about an unknown object.
*
* @param writer The writer.
*
* @param key The object key, if needed.
*
* @param objectAddress The object's address.
*
* @param limit How many more subreferenced objects to write, if any.
*/
static void
writeUnknownObjectContents(const SentryCrashReportWriter *const writer, const char *const key,
const uintptr_t objectAddress, int *limit)
{
(*limit)--;
const void *object = (const void *)objectAddress;
SentryCrashObjCIvar ivars[10];
int8_t s8;
int16_t s16;
int sInt;
int32_t s32;
int64_t s64;
uint8_t u8;
uint16_t u16;
unsigned int uInt;
uint32_t u32;
uint64_t u64;
float f32;
double f64;
bool b;
void *pointer;
writer->beginObject(writer, key);
{
if (sentrycrashobjc_isTaggedPointer(object)) {
writer->addIntegerElement(
writer, "tagged_payload", (int64_t)sentrycrashobjc_taggedPointerPayload(object));
} else {
const void *class = sentrycrashobjc_isaPointer(object);
int ivarCount = sentrycrashobjc_ivarList(class, ivars, sizeof(ivars) / sizeof(*ivars));
*limit -= ivarCount;
for (int i = 0; i < ivarCount; i++) {
SentryCrashObjCIvar *ivar = &ivars[i];
switch (ivar->type[0]) {
case 'c':
sentrycrashobjc_ivarValue(object, ivar->index, &s8);
writer->addIntegerElement(writer, ivar->name, s8);
break;
case 'i':
sentrycrashobjc_ivarValue(object, ivar->index, &sInt);
writer->addIntegerElement(writer, ivar->name, sInt);
break;
case 's':
sentrycrashobjc_ivarValue(object, ivar->index, &s16);
writer->addIntegerElement(writer, ivar->name, s16);
break;
case 'l':
sentrycrashobjc_ivarValue(object, ivar->index, &s32);
writer->addIntegerElement(writer, ivar->name, s32);
break;
case 'q':
sentrycrashobjc_ivarValue(object, ivar->index, &s64);
writer->addIntegerElement(writer, ivar->name, s64);
break;
case 'C':
sentrycrashobjc_ivarValue(object, ivar->index, &u8);
writer->addUIntegerElement(writer, ivar->name, u8);
break;
case 'I':
sentrycrashobjc_ivarValue(object, ivar->index, &uInt);
writer->addUIntegerElement(writer, ivar->name, uInt);
break;
case 'S':
sentrycrashobjc_ivarValue(object, ivar->index, &u16);
writer->addUIntegerElement(writer, ivar->name, u16);
break;
case 'L':
sentrycrashobjc_ivarValue(object, ivar->index, &u32);
writer->addUIntegerElement(writer, ivar->name, u32);
break;
case 'Q':
sentrycrashobjc_ivarValue(object, ivar->index, &u64);
writer->addUIntegerElement(writer, ivar->name, u64);
break;
case 'f':
sentrycrashobjc_ivarValue(object, ivar->index, &f32);
writer->addFloatingPointElement(writer, ivar->name, f32);
break;
case 'd':
sentrycrashobjc_ivarValue(object, ivar->index, &f64);
writer->addFloatingPointElement(writer, ivar->name, f64);
break;
case 'B':
sentrycrashobjc_ivarValue(object, ivar->index, &b);
writer->addBooleanElement(writer, ivar->name, b);
break;
case '*':
case '@':
case '#':
case ':':
sentrycrashobjc_ivarValue(object, ivar->index, &pointer);
writeMemoryContents(writer, ivar->name, (uintptr_t)pointer, limit);
break;
default:
SENTRY_ASYNC_SAFE_LOG_DEBUG(
"%s: Unknown ivar type [%s]", ivar->name, ivar->type);
}
}
}
}
writer->endContainer(writer);
}
static bool
isRestrictedClass(const char *name)
{
if (g_introspectionRules.restrictedClasses != NULL) {
for (int i = 0; i < g_introspectionRules.restrictedClassesCount; i++) {
const char *className = g_introspectionRules.restrictedClasses[i];
if (className != NULL && strcmp(name, className) == 0) {
return true;
}
}
}
return false;
}
static bool
writeObjCObject(const SentryCrashReportWriter *const writer, const uintptr_t address, int *limit)
{
const void *object = (const void *)address;
switch (sentrycrashobjc_objectType(object)) {
case SentryCrashObjCTypeClass:
writer->addStringElement(writer, SentryCrashField_Type, SentryCrashMemType_Class);
writer->addStringElement(writer, SentryCrashField_Class, sentrycrashobjc_className(object));
return true;
case SentryCrashObjCTypeObject: {
writer->addStringElement(writer, SentryCrashField_Type, SentryCrashMemType_Object);
const char *className = sentrycrashobjc_objectClassName(object);
writer->addStringElement(writer, SentryCrashField_Class, className);
if (!isRestrictedClass(className)) {
switch (sentrycrashobjc_objectClassType(object)) {
case SentryCrashObjCClassTypeString:
writeNSStringContents(writer, SentryCrashField_Value, address, limit);
return true;
case SentryCrashObjCClassTypeURL:
writeURLContents(writer, SentryCrashField_Value, address, limit);
return true;
case SentryCrashObjCClassTypeDate:
writeDateContents(writer, SentryCrashField_Value, address, limit);
return true;
case SentryCrashObjCClassTypeArray:
if (*limit > 0) {
writeArrayContents(writer, SentryCrashField_FirstObject, address, limit);
}
return true;
case SentryCrashObjCClassTypeNumber:
writeNumberContents(writer, SentryCrashField_Value, address, limit);
return true;
case SentryCrashObjCClassTypeDictionary:
case SentryCrashObjCClassTypeException:
// TODO: Implement these.
if (*limit > 0) {
writeUnknownObjectContents(writer, SentryCrashField_Ivars, address, limit);
}
return true;
case SentryCrashObjCClassTypeUnknown:
if (*limit > 0) {
writeUnknownObjectContents(writer, SentryCrashField_Ivars, address, limit);
}
return true;
}
}
break;
}
case SentryCrashObjCTypeBlock:
writer->addStringElement(writer, SentryCrashField_Type, SentryCrashMemType_Block);
const char *className = sentrycrashobjc_objectClassName(object);
writer->addStringElement(writer, SentryCrashField_Class, className);
return true;
case SentryCrashObjCTypeUnknown:
break;
}
return false;
}
/** Write the contents of a memory location.
* Also writes meta information about the data.
*
* @param writer The writer.
*
* @param key The object key, if needed.
*
* @param address The memory address.
*
* @param limit How many more subreferenced objects to write, if any.
*/
static void
writeMemoryContents(const SentryCrashReportWriter *const writer, const char *const key,
const uintptr_t address, int *limit)
{
(*limit)--;
const void *object = (const void *)address;
writer->beginObject(writer, key);
{
writer->addUIntegerElement(writer, SentryCrashField_Address, address);
if (!writeObjCObject(writer, address, limit)) {
if (object == NULL) {
writer->addStringElement(
writer, SentryCrashField_Type, SentryCrashMemType_NullPointer);
} else if (isValidString(object)) {
writer->addStringElement(writer, SentryCrashField_Type, SentryCrashMemType_String);
writer->addStringElement(writer, SentryCrashField_Value, (const char *)object);
} else {
writer->addStringElement(writer, SentryCrashField_Type, SentryCrashMemType_Unknown);
}
}
}
writer->endContainer(writer);
}
static bool
isValidPointer(const uintptr_t address)
{
if (address == (uintptr_t)NULL) {
return false;
}
if (sentrycrashobjc_isTaggedPointer((const void *)address)) {
if (!sentrycrashobjc_isValidTaggedPointer((const void *)address)) {
return false;
}
}
return true;
}
static bool
isNotableAddress(const uintptr_t address)
{
if (!isValidPointer(address)) {
return false;
}
const void *object = (const void *)address;
if (sentrycrashobjc_objectType(object) != SentryCrashObjCTypeUnknown) {
return true;
}
if (isValidString(object)) {
return true;
}
return false;
}
/** Write the contents of a memory location only if it contains notable data.
* Also writes meta information about the data.
*
* @param writer The writer.
*
* @param key The object key, if needed.
*
* @param address The memory address.
*/
static void
writeMemoryContentsIfNotable(
const SentryCrashReportWriter *const writer, const char *const key, const uintptr_t address)
{
if (isNotableAddress(address)) {
int limit = kDefaultMemorySearchDepth;
writeMemoryContents(writer, key, address, &limit);
}
}
/** Look for a hex value in a string and try to write whatever it references.
*
* @param writer The writer.
*
* @param key The object key, if needed.
*
* @param string The string to search.
*/
static void
writeAddressReferencedByString(
const SentryCrashReportWriter *const writer, const char *const key, const char *string)
{
uint64_t address = 0;
if (string == NULL
|| !sentrycrashstring_extractHexValue(string, (int)strlen(string), &address)) {
return;
}
int limit = kDefaultMemorySearchDepth;
writeMemoryContents(writer, key, (uintptr_t)address, &limit);
}
#pragma mark Backtrace
/** Write a backtrace to the report.
*
* @param writer The writer to write the backtrace to.
*
* @param key The object key, if needed.
*
* @param stackCursor The stack cursor to read from.
*/
static void
writeBacktrace(const SentryCrashReportWriter *const writer, const char *const key,
SentryCrashStackCursor *stackCursor)
{
writer->beginObject(writer, key);
{
writer->beginArray(writer, SentryCrashField_Contents);
{
while (stackCursor->advanceCursor(stackCursor)) {
writer->beginObject(writer, NULL);
{
if (stackCursor->symbolicate(stackCursor)) {
if (stackCursor->stackEntry.imageName != NULL) {
writer->addStringElement(writer, SentryCrashField_ObjectName,
sentrycrashfu_lastPathEntry(stackCursor->stackEntry.imageName));
}
writer->addUIntegerElement(writer, SentryCrashField_ObjectAddr,
stackCursor->stackEntry.imageAddress);
if (stackCursor->stackEntry.symbolName != NULL) {
writer->addStringElement(writer, SentryCrashField_SymbolName,
stackCursor->stackEntry.symbolName);
}
writer->addUIntegerElement(writer, SentryCrashField_SymbolAddr,
stackCursor->stackEntry.symbolAddress);
}
writer->addUIntegerElement(
writer, SentryCrashField_InstructionAddr, stackCursor->stackEntry.address);
}
writer->endContainer(writer);
}
}
writer->endContainer(writer);
writer->addIntegerElement(writer, SentryCrashField_Skipped, 0);
}
writer->endContainer(writer);
}
#pragma mark Stack
/** Write a dump of the stack contents to the report.
*
* @param writer The writer.
*
* @param key The object key, if needed.
*
* @param machineContext The context to retrieve the stack from.
*
* @param isStackOverflow If true, the stack has overflowed.
*/
static void
writeStackContents(const SentryCrashReportWriter *const writer, const char *const key,
const struct SentryCrashMachineContext *const machineContext, const bool isStackOverflow)
{
uintptr_t sp = sentrycrashcpu_stackPointer(machineContext);
if ((void *)sp == NULL) {
return;
}
uintptr_t lowAddress = sp
+ (uintptr_t)(kStackContentsPushedDistance * (int)sizeof(sp)
* sentrycrashcpu_stackGrowDirection() * -1);
uintptr_t highAddress = sp
+ (uintptr_t)(kStackContentsPoppedDistance * (int)sizeof(sp)
* sentrycrashcpu_stackGrowDirection());
if (highAddress < lowAddress) {
uintptr_t tmp = lowAddress;
lowAddress = highAddress;
highAddress = tmp;
}
writer->beginObject(writer, key);
{
writer->addStringElement(writer, SentryCrashField_GrowDirection,
sentrycrashcpu_stackGrowDirection() > 0 ? "+" : "-");
writer->addUIntegerElement(writer, SentryCrashField_DumpStart, lowAddress);
writer->addUIntegerElement(writer, SentryCrashField_DumpEnd, highAddress);
writer->addUIntegerElement(writer, SentryCrashField_StackPtr, sp);
writer->addBooleanElement(writer, SentryCrashField_Overflow, isStackOverflow);
uint8_t stackBuffer[kStackContentsTotalDistance * sizeof(sp)];
int copyLength = (int)(highAddress - lowAddress);
if (sentrycrashmem_copySafely((void *)lowAddress, stackBuffer, copyLength)) {
writer->addDataElement(
writer, SentryCrashField_Contents, (void *)stackBuffer, copyLength);
} else {
writer->addStringElement(
writer, SentryCrashField_Error, "Stack contents not accessible");
}
}
writer->endContainer(writer);
}
/** Write any notable addresses near the stack pointer (above and below).
*
* @param writer The writer.
*
* @param machineContext The context to retrieve the stack from.
*
* @param backDistance The distance towards the beginning of the stack to check.
*
* @param forwardDistance The distance past the end of the stack to check.
*/
static void
writeNotableStackContents(const SentryCrashReportWriter *const writer,
const struct SentryCrashMachineContext *const machineContext, const int backDistance,
const int forwardDistance)
{
uintptr_t sp = sentrycrashcpu_stackPointer(machineContext);
if ((void *)sp == NULL) {
return;
}
uintptr_t lowAddress = sp
+ (uintptr_t)(backDistance * (int)sizeof(sp) * sentrycrashcpu_stackGrowDirection() * -1);
uintptr_t highAddress
= sp + (uintptr_t)(forwardDistance * (int)sizeof(sp) * sentrycrashcpu_stackGrowDirection());
if (highAddress < lowAddress) {
uintptr_t tmp = lowAddress;
lowAddress = highAddress;
highAddress = tmp;
}
uintptr_t contentsAsPointer;
char nameBuffer[40];
for (uintptr_t address = lowAddress; address < highAddress; address += sizeof(address)) {
if (sentrycrashmem_copySafely(
(void *)address, &contentsAsPointer, sizeof(contentsAsPointer))) {
snprintf(nameBuffer, sizeof(nameBuffer), "stack@%p", (void *)address);
writeMemoryContentsIfNotable(writer, nameBuffer, contentsAsPointer);
}
}
}
#pragma mark Registers
/** Write the contents of all regular registers to the report.
*
* @param writer The writer.
*
* @param key The object key, if needed.
*
* @param machineContext The context to retrieve the registers from.
*/
static void
writeBasicRegisters(const SentryCrashReportWriter *const writer, const char *const key,
const struct SentryCrashMachineContext *const machineContext)
{
char registerNameBuff[30];
const char *registerName;
writer->beginObject(writer, key);
{
const int numRegisters = sentrycrashcpu_numRegisters();
for (int reg = 0; reg < numRegisters; reg++) {
registerName = sentrycrashcpu_registerName(reg);
if (registerName == NULL) {
snprintf(registerNameBuff, sizeof(registerNameBuff), "r%d", reg);
registerName = registerNameBuff;
}
writer->addUIntegerElement(
writer, registerName, sentrycrashcpu_registerValue(machineContext, reg));
}
}
writer->endContainer(writer);
}
/** Write the contents of all exception registers to the report.
*
* @param writer The writer.
*
* @param key The object key, if needed.
*
* @param machineContext The context to retrieve the registers from.
*/
static void
writeExceptionRegisters(const SentryCrashReportWriter *const writer, const char *const key,
const struct SentryCrashMachineContext *const machineContext)
{
char registerNameBuff[30];
const char *registerName;
writer->beginObject(writer, key);
{
const int numRegisters = sentrycrashcpu_numExceptionRegisters();
for (int reg = 0; reg < numRegisters; reg++) {
registerName = sentrycrashcpu_exceptionRegisterName(reg);
if (registerName == NULL) {
snprintf(registerNameBuff, sizeof(registerNameBuff), "r%d", reg);
registerName = registerNameBuff;
}
writer->addUIntegerElement(
writer, registerName, sentrycrashcpu_exceptionRegisterValue(machineContext, reg));
}
}
writer->endContainer(writer);
}
/** Write all applicable registers.
*
* @param writer The writer.
*
* @param key The object key, if needed.
*
* @param machineContext The context to retrieve the registers from.
*/
static void
writeRegisters(const SentryCrashReportWriter *const writer, const char *const key,
const struct SentryCrashMachineContext *const machineContext)
{
writer->beginObject(writer, key);
{
writeBasicRegisters(writer, SentryCrashField_Basic, machineContext);
if (sentrycrashmc_hasValidExceptionRegisters(machineContext)) {
writeExceptionRegisters(writer, SentryCrashField_Exception, machineContext);
}