-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSException.m
More file actions
1819 lines (1594 loc) · 47.4 KB
/
NSException.m
File metadata and controls
1819 lines (1594 loc) · 47.4 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
/** NSException - Object encapsulation of a general exception handler
Copyright (C) 1993-2013 Free Software Foundation, Inc.
Written by: Adam Fedor <fedor@boulder.colorado.edu>
Date: Mar 1995
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
$Date$ $Revision$
*/
#import "common.h"
#define EXPOSE_NSException_IVARS 1
#define EXPOSE_NSThread_IVARS 1
#import "GSPrivate.h"
#import "Foundation/NSEnumerator.h"
#import "Foundation/NSException.h"
#import "Foundation/NSArray.h"
#import "Foundation/NSCoder.h"
#import "Foundation/NSData.h"
#import "Foundation/NSLock.h"
#import "Foundation/NSNull.h"
#import "Foundation/NSThread.h"
#import "Foundation/NSLock.h"
#import "Foundation/NSDictionary.h"
#import "Foundation/NSValue.h"
#import "GNUstepBase/NSString+GNUstepBase.h"
#import "GSPThread.h"
#ifdef __GNUSTEP_RUNTIME__
#include <objc/hooks.h>
#endif
#ifdef HAVE_SET_UNCAUGHT_EXCEPTION_HANDLER
#include <objc/objc-exception.h>
#endif
#ifdef HAVE_MALLOC_H
#if !defined(__OpenBSD__)
#include <malloc.h>
#endif
#endif
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
#include <stdio.h>
#ifdef HAVE_BACKTRACE
#include <execinfo.h>
#endif
/*
* Turn off USE_BFD if we don't have bfd support for it.
*/
#if !(defined(HAVE_BFD_H) && defined(HAVE_LIBBFD) && defined(HAVE_LIBIBERTY))
# if defined(USE_BFD)
# undef USE_BFD
# endif
#endif
#if defined(_WIN32) && !defined(USE_BFD)
#include <windows.h>
#if defined(HAVE_DBGHELP_H)
#include <dbghelp.h>
#else
/* Supply the relevant bits from dbghelp.h if we could't find the header.
*/
#define SYMOPT_UNDNAME 0x00000002
#define SYMOPT_DEFERRED_LOADS 0x00000004
typedef struct _SYMBOL_INFO {
ULONG SizeOfStruct;
ULONG TypeIndex;
uint64_t Reserved[2];
ULONG Index;
ULONG Size;
uint64_t ModBase;
ULONG Flags;
uint64_t Value;
uint64_t Address;
ULONG Register;
ULONG Scope;
ULONG Tag;
ULONG NameLen;
ULONG MaxNameLen;
TCHAR Name[1];
} SYMBOL_INFO;
#endif
#endif
static NSUncaughtExceptionHandler *_NSUncaughtExceptionHandler = 0;
#define _e_info (((id*)_reserved)[0])
#define _e_stack (((id*)_reserved)[1])
@interface NSException (GSPrivate)
- (GSStackTrace*) _callStack;
@end
#if defined(_WIN32)
#if defined(USE_BFD)
static NSString *
GSPrivateBaseAddress(void *addr, void **base)
{
MEMORY_BASIC_INFORMATION info;
/* Found a note saying that according to Matt Pietrek's "Under the Hood"
* column for the April 1997 issue of Microsoft Systems Journal, the
* allocation base returned by VirtualQuery can be used as the handle
* to obtain module information for a loaded library.
*/
if (VirtualQuery (addr, &info, sizeof(info)) != 0)
{
HMODULE handle = (HMODULE) info.AllocationBase;
unichar path[MAX_PATH+1];
if (GetModuleFileNameW(handle, path, sizeof(path)-1) != 0)
{
path[sizeof(path)-1] = '\0';
*base = info.BaseAddress;
return [NSString stringWithCharacters: path length: wcslen(path)];
}
}
return nil;
}
#endif /* USE_BFD */
#else /* _WIN32 */
#include <dlfcn.h>
#if defined(USE_BFD)
static NSString *
GSPrivateBaseAddress(void *addr, void **base)
{
#ifdef HAVE_DLADDR
Dl_info info;
if (!dladdr(addr, &info))
return nil;
*base = info.dli_fbase;
return [NSString stringWithUTF8String: info.dli_fname];
#else
return nil;
#endif
}
#endif /* USE_BFD */
#endif /* _WIN32 */
#if defined(USE_BFD)
// GSStackTrace inspired by FYStackTrace.m
// created by Wim Oudshoorn on Mon 11-Apr-2006
// reworked by Lloyd Dupont @ NovaMind.com on 4-May-2006
#include <bfd.h>
@class GSBinaryFileInfo;
@interface GSFunctionInfo : NSObject
{
void *_address;
NSString *_fileName;
NSString *_functionName;
int _lineNo;
GSBinaryFileInfo *_module;
}
- (void*) address;
- (NSString *) fileName;
- (NSString *) function;
- (id) initWithModule: (GSBinaryFileInfo*)module
address: (void*)address
file: (NSString*)file
function: (NSString*)function
line: (int)lineNo;
- (int) lineNumber;
- (GSBinaryFileInfo*) module;
@end
@interface GSBinaryFileInfo : NSObject
{
NSString *_fileName;
bfd *_abfd;
asymbol **_symbols;
long _symbolCount;
}
- (NSString *) fileName;
- (GSFunctionInfo *) functionForAddress: (void*) address;
- (id) initWithBinaryFile: (NSString *)fileName;
- (id) init; // return info for the current executing process
@end
@implementation GSFunctionInfo
- (void*) address
{
return _address;
}
- (oneway void) dealloc
{
DESTROY(_module);
DESTROY(_fileName);
DESTROY(_functionName);
[super dealloc];
}
- (NSString *) description
{
return [NSString stringWithFormat: @"(%@: %p) %@ %@: %d",
[_module fileName], _address, _functionName, _fileName, _lineNo];
}
- (NSString *) fileName
{
return _fileName;
}
- (NSString *) function
{
return _functionName;
}
- (id) init
{
DESTROY(self);
return nil;
}
- (id) initWithModule: (GSBinaryFileInfo*)module
address: (void*)address
file: (NSString*)file
function: (NSString*)function
line: (int)lineNo
{
_module = RETAIN(module);
_address = address;
_fileName = [file copy];
_functionName = [function copy];
_lineNo = lineNo;
return self;
}
- (int) lineNumber
{
return _lineNo;
}
- (GSBinaryFileInfo *) module
{
return _module;
}
@end
@implementation GSBinaryFileInfo
+ (GSBinaryFileInfo*) infoWithBinaryFile: (NSString *)fileName
{
return [[[self alloc] initWithBinaryFile: fileName] autorelease];
}
+ (void) initialize
{
static BOOL first = YES;
if (first == NO)
{
return;
}
first = NO;
bfd_init ();
}
- (oneway void) dealloc
{
DESTROY(_fileName);
if (_abfd)
{
bfd_close (_abfd);
_abfd = NULL;
}
if (_symbols)
{
free(_symbols);
_symbols = NULL;
}
[super dealloc];
}
- (NSString *) fileName
{
return _fileName;
}
- (id) init
{
NSString *processName;
processName = [[[NSProcessInfo processInfo] arguments] objectAtIndex: 0];
return [self initWithBinaryFile: processName];
}
- (id) initWithBinaryFile: (NSString *)fileName
{
int neededSpace;
// 1st initialize the bfd
if ([fileName length] == 0)
{
//NSLog (@"GSBinaryFileInfo: No File");
DESTROY(self);
return nil;
}
_fileName = [fileName copy];
_abfd = bfd_openr ([fileName cString], NULL);
if (!_abfd)
{
//NSLog (@"GSBinaryFileInfo: No Binary Info");
DESTROY(self);
return nil;
}
if (!bfd_check_format_matches (_abfd, bfd_object, NULL))
{
//NSLog (@"GSBinaryFileInfo: BFD format object error");
DESTROY(self);
return nil;
}
// second read the symbols from it
if (!(bfd_get_file_flags (_abfd) & HAS_SYMS))
{
//NSLog (@"GSBinaryFileInfo: BFD does not contain any symbols");
DESTROY(self);
return nil;
}
neededSpace = bfd_get_symtab_upper_bound (_abfd);
if (neededSpace < 0)
{
//NSLog (@"GSBinaryFileInfo: BFD error while deducing needed space");
DESTROY(self);
return nil;
}
if (neededSpace == 0)
{
//NSLog (@"GSBinaryFileInfo: BFD no space for symbols needed");
DESTROY(self);
return nil;
}
_symbols = malloc (neededSpace);
if (!_symbols)
{
//NSLog (@"GSBinaryFileInfo: Can't allocate buffer");
DESTROY(self);
return nil;
}
_symbolCount = bfd_canonicalize_symtab (_abfd, _symbols);
if (_symbolCount < 0)
{
//NSLog (@"GSBinaryFileInfo: BFD error while reading symbols");
DESTROY(self);
return nil;
}
return self;
}
struct SearchAddressStruct
{
void *theAddress;
GSBinaryFileInfo *module;
asymbol **symbols;
GSFunctionInfo *theInfo;
};
static void find_address (bfd *abfd, asection *section,
struct SearchAddressStruct *info)
{
bfd_vma address;
bfd_vma vma;
unsigned size;
const char *fileName = 0;
const char *functionName = 0;
unsigned line = 0;
if (info->theInfo)
{
return;
}
address = (bfd_vma) (uintptr_t)info->theAddress;
/* bfd_get_section_vma() was changed to bfd_section_vma() together with
* changes to a couple of other inline functions.
*/
#if defined(HAVE_BFD_SECTION_VMA)
if (!(bfd_section_flags(section) & SEC_ALLOC))
{
return; // Only debug in this section
}
if (bfd_section_flags(section) & SEC_DATA)
{
return; // Only data in this section
}
vma = bfd_section_vma(section);
size = bfd_section_size(section);
#else
if (!(bfd_get_section_flags(abfd, section) & SEC_ALLOC))
{
return; // Only debug in this section
}
if (bfd_get_section_flags(abfd, section) & SEC_DATA)
{
return; // Only data in this section
}
vma = bfd_get_section_vma(abfd, section);
size = bfd_section_size(abfd, section);
#endif
if (address < vma || address >= vma + size)
{
return;
}
if (bfd_find_nearest_line (abfd, section, info->symbols,
address - vma, &fileName, &functionName, &line))
{
GSFunctionInfo *fi;
NSString *file = nil;
NSString *func = nil;
if (fileName != 0)
{
file = [NSString stringWithCString: fileName
encoding: [NSString defaultCStringEncoding]];
}
if (functionName != 0)
{
func = [NSString stringWithCString: functionName
encoding: [NSString defaultCStringEncoding]];
}
fi = [GSFunctionInfo alloc];
fi = [fi initWithModule: info->module
address: info->theAddress
file: file
function: func
line: line];
[fi autorelease];
info->theInfo = fi;
}
}
- (GSFunctionInfo *) functionForAddress: (void*) address
{
struct SearchAddressStruct searchInfo =
{ address, self, _symbols, nil };
bfd_map_over_sections (_abfd,
(void (*) (bfd *, asection *, void *)) find_address, &searchInfo);
return searchInfo.theInfo;
}
@end
static pthread_mutex_t modLock;
static NSMutableDictionary *stackModules = nil;
// initialize stack trace info
static id
GSLoadModule(NSString *fileName)
{
GSBinaryFileInfo *module = nil;
(void)pthread_mutex_lock(&modLock);
if (stackModules == nil)
{
NSEnumerator *enumerator;
NSBundle *bundle;
stackModules = [NSMutableDictionary new];
/*
* Try to ensure we have the main, base and gui library bundles.
*/
[NSBundle mainBundle];
[NSBundle bundleForClass: [NSObject class]];
[NSBundle bundleForClass: NSClassFromString(@"NSView")];
/*
* Add file info for all bundles with code.
*/
enumerator = [[NSBundle allBundles] objectEnumerator];
while ((bundle = [enumerator nextObject]) != nil)
{
if ([bundle load] == YES)
{
GSLoadModule([bundle executablePath]);
}
}
}
if ([fileName length] > 0)
{
module = [stackModules objectForKey: fileName];
if (module == nil)
{
module = [GSBinaryFileInfo infoWithBinaryFile: fileName];
if (module == nil)
{
module = (id)[NSNull null];
}
if ([stackModules objectForKey: fileName] == nil)
{
[stackModules setObject: module forKey: fileName];
}
else
{
module = [stackModules objectForKey: fileName];
}
}
}
(void)pthread_mutex_unlock(&modLock);
if (module == (id)[NSNull null])
{
module = nil;
}
return module;
}
static NSArray*
GSListModules()
{
NSArray *result;
GSLoadModule(nil); // initialise
(void)pthread_mutex_lock(&modLock);
result = [stackModules allValues];
(void)pthread_mutex_unlock(&modLock);
return result;
}
#endif /* USE_BFD */
#if defined(WITH_UNWIND) && !defined(HAVE_BACKTRACE)
#include <unwind.h>
#if !defined(_WIN32)
#include <dlfcn.h>
#endif
struct GSBacktraceState
{
void **current;
void **end;
};
static _Unwind_Reason_Code
GSUnwindCallback(struct _Unwind_Context* context, void* arg)
{
struct GSBacktraceState *state = (struct GSBacktraceState*)arg;
uintptr_t pc = _Unwind_GetIP(context);
if (pc) {
if (state->current == state->end) {
return _URC_END_OF_STACK;
} else {
*state->current++ = (void*)pc;
}
}
return 0; //_URC_OK/_URC_NO_REASON
}
#endif /* WITH_UNWIND && !HAVE_BACKTRACE */
#if defined(_WIN32) && !defined(USE_BFD)
typedef USHORT (WINAPI *CaptureStackBackTraceType)(ULONG,ULONG,PVOID*,PULONG);
typedef BOOL (WINAPI *SymInitializeType)(HANDLE,char*,BOOL);
typedef DWORD (WINAPI *SymSetOptionsType)(DWORD);
typedef BOOL (WINAPI *SymFromAddrType)(HANDLE,DWORD64,PDWORD64,SYMBOL_INFO*);
static CaptureStackBackTraceType capture = 0;
static SymInitializeType initSym = 0;
static SymSetOptionsType optSym = 0;
static SymFromAddrType fromSym = 0;
static HANDLE hProcess = 0;
static pthread_mutex_t traceLock;
#define MAXFRAMES 62 /* Limitation of windows-xp */
#else
#define MAXFRAMES 128 /* 1KB buffer on 64bit machine */
#endif
#if !defined(HAVE_BUILTIN_EXTRACT_RETURN_ADDRESS)
# define __builtin_extract_return_address(X) X
#endif
#define _NS_FRAME_HACK(a) \
case a: env->addr = __builtin_frame_address(a + 1); break;
#define _NS_RETURN_HACK(a) \
case a: env->addr = (__builtin_frame_address(a + 1) ? \
__builtin_extract_return_address(__builtin_return_address(a + 1)) : 0); break;
/*
* The following horrible signal handling code is a workaround for the fact
* that the __builtin_frame_address() and __builtin_return_address()
* functions are not reliable (at least not on my EM64T based system) and
* will sometimes walk off the stack and access illegal memory locations.
* In order to prevent such an occurrance from crashing the application,
* we use sigsetjmp() and siglongjmp() to ensure that we can recover, and
* we keep the jump buffer in thread-local memory to avoid possible thread
* safety issues.
* Of course this will fail horribly if an exception occurs in one of the
* few methods we use to manage the per-thread jump buffer.
*/
#if defined(HAVE_SYS_SIGNAL_H)
# include <sys/signal.h>
#elif defined(HAVE_SIGNAL_H)
# include <signal.h>
#endif
#if defined(_WIN32)
#ifndef SIGBUS
#define SIGBUS SIGILL
#endif
#endif
/* sigsetjmp may be a function or a macro. The test for the function is
* done at configure time so we can tell here if either is available.
*/
#if !defined(HAVE_SIGSETJMP) && !defined(sigsetjmp)
#define siglongjmp(A,B) longjmp(A,B)
#define sigsetjmp(A,B) setjmp(A)
#define sigjmp_buf jmp_buf
#endif
typedef struct {
sigjmp_buf buf;
void *addr;
void (*bus)(int);
void (*segv)(int);
} jbuf_type;
static jbuf_type *
jbuf()
{
NSMutableData *d;
NSMutableDictionary *dict;
dict = [[NSThread currentThread] threadDictionary];
d = [dict objectForKey: @"GSjbuf"];
if (d == nil)
{
d = [[NSMutableData alloc] initWithLength: sizeof(jbuf_type)];
[dict setObject: d forKey: @"GSjbuf"];
RELEASE(d);
}
return (jbuf_type*)[d mutableBytes];
}
static void
recover(int sig)
{
siglongjmp(jbuf()->buf, 1);
}
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wframe-address"
#else
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wframe-address"
#endif
void *
NSFrameAddress(NSUInteger offset)
{
jbuf_type *env;
env = jbuf();
if (sigsetjmp(env->buf, 1) == 0)
{
env->segv = signal(SIGSEGV, recover);
env->bus = signal(SIGBUS, recover);
switch (offset)
{
_NS_FRAME_HACK(0); _NS_FRAME_HACK(1); _NS_FRAME_HACK(2);
_NS_FRAME_HACK(3); _NS_FRAME_HACK(4); _NS_FRAME_HACK(5);
_NS_FRAME_HACK(6); _NS_FRAME_HACK(7); _NS_FRAME_HACK(8);
_NS_FRAME_HACK(9); _NS_FRAME_HACK(10); _NS_FRAME_HACK(11);
_NS_FRAME_HACK(12); _NS_FRAME_HACK(13); _NS_FRAME_HACK(14);
_NS_FRAME_HACK(15); _NS_FRAME_HACK(16); _NS_FRAME_HACK(17);
_NS_FRAME_HACK(18); _NS_FRAME_HACK(19); _NS_FRAME_HACK(20);
_NS_FRAME_HACK(21); _NS_FRAME_HACK(22); _NS_FRAME_HACK(23);
_NS_FRAME_HACK(24); _NS_FRAME_HACK(25); _NS_FRAME_HACK(26);
_NS_FRAME_HACK(27); _NS_FRAME_HACK(28); _NS_FRAME_HACK(29);
_NS_FRAME_HACK(30); _NS_FRAME_HACK(31); _NS_FRAME_HACK(32);
_NS_FRAME_HACK(33); _NS_FRAME_HACK(34); _NS_FRAME_HACK(35);
_NS_FRAME_HACK(36); _NS_FRAME_HACK(37); _NS_FRAME_HACK(38);
_NS_FRAME_HACK(39); _NS_FRAME_HACK(40); _NS_FRAME_HACK(41);
_NS_FRAME_HACK(42); _NS_FRAME_HACK(43); _NS_FRAME_HACK(44);
_NS_FRAME_HACK(45); _NS_FRAME_HACK(46); _NS_FRAME_HACK(47);
_NS_FRAME_HACK(48); _NS_FRAME_HACK(49); _NS_FRAME_HACK(50);
_NS_FRAME_HACK(51); _NS_FRAME_HACK(52); _NS_FRAME_HACK(53);
_NS_FRAME_HACK(54); _NS_FRAME_HACK(55); _NS_FRAME_HACK(56);
_NS_FRAME_HACK(57); _NS_FRAME_HACK(58); _NS_FRAME_HACK(59);
_NS_FRAME_HACK(60); _NS_FRAME_HACK(61); _NS_FRAME_HACK(62);
_NS_FRAME_HACK(63); _NS_FRAME_HACK(64); _NS_FRAME_HACK(65);
_NS_FRAME_HACK(66); _NS_FRAME_HACK(67); _NS_FRAME_HACK(68);
_NS_FRAME_HACK(69); _NS_FRAME_HACK(70); _NS_FRAME_HACK(71);
_NS_FRAME_HACK(72); _NS_FRAME_HACK(73); _NS_FRAME_HACK(74);
_NS_FRAME_HACK(75); _NS_FRAME_HACK(76); _NS_FRAME_HACK(77);
_NS_FRAME_HACK(78); _NS_FRAME_HACK(79); _NS_FRAME_HACK(80);
_NS_FRAME_HACK(81); _NS_FRAME_HACK(82); _NS_FRAME_HACK(83);
_NS_FRAME_HACK(84); _NS_FRAME_HACK(85); _NS_FRAME_HACK(86);
_NS_FRAME_HACK(87); _NS_FRAME_HACK(88); _NS_FRAME_HACK(89);
_NS_FRAME_HACK(90); _NS_FRAME_HACK(91); _NS_FRAME_HACK(92);
_NS_FRAME_HACK(93); _NS_FRAME_HACK(94); _NS_FRAME_HACK(95);
_NS_FRAME_HACK(96); _NS_FRAME_HACK(97); _NS_FRAME_HACK(98);
_NS_FRAME_HACK(99);
default: env->addr = NULL; break;
}
signal(SIGSEGV, env->segv);
signal(SIGBUS, env->bus);
}
else
{
env = jbuf();
signal(SIGSEGV, env->segv);
signal(SIGBUS, env->bus);
env->addr = NULL;
}
return env->addr;
}
NSUInteger NSCountFrames(void)
{
jbuf_type *env;
env = jbuf();
if (sigsetjmp(env->buf, 1) == 0)
{
env->segv = signal(SIGSEGV, recover);
env->bus = signal(SIGBUS, recover);
env->addr = 0;
#define _NS_COUNT_HACK(X) if (__builtin_frame_address(X + 1) == 0) \
goto done; else env->addr = (void*)(X + 1);
_NS_COUNT_HACK(0); _NS_COUNT_HACK(1); _NS_COUNT_HACK(2);
_NS_COUNT_HACK(3); _NS_COUNT_HACK(4); _NS_COUNT_HACK(5);
_NS_COUNT_HACK(6); _NS_COUNT_HACK(7); _NS_COUNT_HACK(8);
_NS_COUNT_HACK(9); _NS_COUNT_HACK(10); _NS_COUNT_HACK(11);
_NS_COUNT_HACK(12); _NS_COUNT_HACK(13); _NS_COUNT_HACK(14);
_NS_COUNT_HACK(15); _NS_COUNT_HACK(16); _NS_COUNT_HACK(17);
_NS_COUNT_HACK(18); _NS_COUNT_HACK(19); _NS_COUNT_HACK(20);
_NS_COUNT_HACK(21); _NS_COUNT_HACK(22); _NS_COUNT_HACK(23);
_NS_COUNT_HACK(24); _NS_COUNT_HACK(25); _NS_COUNT_HACK(26);
_NS_COUNT_HACK(27); _NS_COUNT_HACK(28); _NS_COUNT_HACK(29);
_NS_COUNT_HACK(30); _NS_COUNT_HACK(31); _NS_COUNT_HACK(32);
_NS_COUNT_HACK(33); _NS_COUNT_HACK(34); _NS_COUNT_HACK(35);
_NS_COUNT_HACK(36); _NS_COUNT_HACK(37); _NS_COUNT_HACK(38);
_NS_COUNT_HACK(39); _NS_COUNT_HACK(40); _NS_COUNT_HACK(41);
_NS_COUNT_HACK(42); _NS_COUNT_HACK(43); _NS_COUNT_HACK(44);
_NS_COUNT_HACK(45); _NS_COUNT_HACK(46); _NS_COUNT_HACK(47);
_NS_COUNT_HACK(48); _NS_COUNT_HACK(49); _NS_COUNT_HACK(50);
_NS_COUNT_HACK(51); _NS_COUNT_HACK(52); _NS_COUNT_HACK(53);
_NS_COUNT_HACK(54); _NS_COUNT_HACK(55); _NS_COUNT_HACK(56);
_NS_COUNT_HACK(57); _NS_COUNT_HACK(58); _NS_COUNT_HACK(59);
_NS_COUNT_HACK(60); _NS_COUNT_HACK(61); _NS_COUNT_HACK(62);
_NS_COUNT_HACK(63); _NS_COUNT_HACK(64); _NS_COUNT_HACK(65);
_NS_COUNT_HACK(66); _NS_COUNT_HACK(67); _NS_COUNT_HACK(68);
_NS_COUNT_HACK(69); _NS_COUNT_HACK(70); _NS_COUNT_HACK(71);
_NS_COUNT_HACK(72); _NS_COUNT_HACK(73); _NS_COUNT_HACK(74);
_NS_COUNT_HACK(75); _NS_COUNT_HACK(76); _NS_COUNT_HACK(77);
_NS_COUNT_HACK(78); _NS_COUNT_HACK(79); _NS_COUNT_HACK(80);
_NS_COUNT_HACK(81); _NS_COUNT_HACK(82); _NS_COUNT_HACK(83);
_NS_COUNT_HACK(84); _NS_COUNT_HACK(85); _NS_COUNT_HACK(86);
_NS_COUNT_HACK(87); _NS_COUNT_HACK(88); _NS_COUNT_HACK(89);
_NS_COUNT_HACK(90); _NS_COUNT_HACK(91); _NS_COUNT_HACK(92);
_NS_COUNT_HACK(93); _NS_COUNT_HACK(94); _NS_COUNT_HACK(95);
_NS_COUNT_HACK(96); _NS_COUNT_HACK(97); _NS_COUNT_HACK(98);
_NS_COUNT_HACK(99);
done:
signal(SIGSEGV, env->segv);
signal(SIGBUS, env->bus);
}
else
{
env = jbuf();
signal(SIGSEGV, env->segv);
signal(SIGBUS, env->bus);
}
return (uintptr_t)env->addr;
}
void *
NSReturnAddress(NSUInteger offset)
{
jbuf_type *env;
env = jbuf();
if (sigsetjmp(env->buf, 1) == 0)
{
env->segv = signal(SIGSEGV, recover);
env->bus = signal(SIGBUS, recover);
switch (offset)
{
_NS_RETURN_HACK(0); _NS_RETURN_HACK(1); _NS_RETURN_HACK(2);
_NS_RETURN_HACK(3); _NS_RETURN_HACK(4); _NS_RETURN_HACK(5);
_NS_RETURN_HACK(6); _NS_RETURN_HACK(7); _NS_RETURN_HACK(8);
_NS_RETURN_HACK(9); _NS_RETURN_HACK(10); _NS_RETURN_HACK(11);
_NS_RETURN_HACK(12); _NS_RETURN_HACK(13); _NS_RETURN_HACK(14);
_NS_RETURN_HACK(15); _NS_RETURN_HACK(16); _NS_RETURN_HACK(17);
_NS_RETURN_HACK(18); _NS_RETURN_HACK(19); _NS_RETURN_HACK(20);
_NS_RETURN_HACK(21); _NS_RETURN_HACK(22); _NS_RETURN_HACK(23);
_NS_RETURN_HACK(24); _NS_RETURN_HACK(25); _NS_RETURN_HACK(26);
_NS_RETURN_HACK(27); _NS_RETURN_HACK(28); _NS_RETURN_HACK(29);
_NS_RETURN_HACK(30); _NS_RETURN_HACK(31); _NS_RETURN_HACK(32);
_NS_RETURN_HACK(33); _NS_RETURN_HACK(34); _NS_RETURN_HACK(35);
_NS_RETURN_HACK(36); _NS_RETURN_HACK(37); _NS_RETURN_HACK(38);
_NS_RETURN_HACK(39); _NS_RETURN_HACK(40); _NS_RETURN_HACK(41);
_NS_RETURN_HACK(42); _NS_RETURN_HACK(43); _NS_RETURN_HACK(44);
_NS_RETURN_HACK(45); _NS_RETURN_HACK(46); _NS_RETURN_HACK(47);
_NS_RETURN_HACK(48); _NS_RETURN_HACK(49); _NS_RETURN_HACK(50);
_NS_RETURN_HACK(51); _NS_RETURN_HACK(52); _NS_RETURN_HACK(53);
_NS_RETURN_HACK(54); _NS_RETURN_HACK(55); _NS_RETURN_HACK(56);
_NS_RETURN_HACK(57); _NS_RETURN_HACK(58); _NS_RETURN_HACK(59);
_NS_RETURN_HACK(60); _NS_RETURN_HACK(61); _NS_RETURN_HACK(62);
_NS_RETURN_HACK(63); _NS_RETURN_HACK(64); _NS_RETURN_HACK(65);
_NS_RETURN_HACK(66); _NS_RETURN_HACK(67); _NS_RETURN_HACK(68);
_NS_RETURN_HACK(69); _NS_RETURN_HACK(70); _NS_RETURN_HACK(71);
_NS_RETURN_HACK(72); _NS_RETURN_HACK(73); _NS_RETURN_HACK(74);
_NS_RETURN_HACK(75); _NS_RETURN_HACK(76); _NS_RETURN_HACK(77);
_NS_RETURN_HACK(78); _NS_RETURN_HACK(79); _NS_RETURN_HACK(80);
_NS_RETURN_HACK(81); _NS_RETURN_HACK(82); _NS_RETURN_HACK(83);
_NS_RETURN_HACK(84); _NS_RETURN_HACK(85); _NS_RETURN_HACK(86);
_NS_RETURN_HACK(87); _NS_RETURN_HACK(88); _NS_RETURN_HACK(89);
_NS_RETURN_HACK(90); _NS_RETURN_HACK(91); _NS_RETURN_HACK(92);
_NS_RETURN_HACK(93); _NS_RETURN_HACK(94); _NS_RETURN_HACK(95);
_NS_RETURN_HACK(96); _NS_RETURN_HACK(97); _NS_RETURN_HACK(98);
_NS_RETURN_HACK(99);
default: env->addr = NULL; break;
}
signal(SIGSEGV, env->segv);
signal(SIGBUS, env->bus);
}
else
{
env = jbuf();
signal(SIGSEGV, env->segv);
signal(SIGBUS, env->bus);
env->addr = NULL;
}
return env->addr;
}
#ifdef __clang__
#pragma clang diagnostic pop
#else
#pragma GCC diagnostic pop
#endif
unsigned
GSPrivateReturnAddresses(NSUInteger **returns)
{
unsigned numReturns;
#if defined(_WIN32) && !defined(USE_BFD)
NSUInteger addr[MAXFRAMES];
(void)pthread_mutex_lock(&traceLock);
if (0 == hProcess)
{
hProcess = GetCurrentProcess();
if (0 == capture)
{
HANDLE hModule;
hModule = LoadLibrary("kernel32.dll");
if (0 == hModule)
{
fprintf(stderr, "Failed to load kernel32.dll with error: %d\n",
(int)GetLastError());
(void)pthread_mutex_unlock(&traceLock);
return 0;
}
capture = (CaptureStackBackTraceType)GetProcAddress(
hModule, "RtlCaptureStackBackTrace");
if (0 == capture)
{
fprintf(stderr, "Failed to find RtlCaptureStackBackTrace: %d\n",
(int)GetLastError());
(void)pthread_mutex_unlock(&traceLock);
return 0;
}
hModule = LoadLibrary("dbghelp.dll");
if (0 == hModule)
{
fprintf(stderr, "Failed to load dbghelp.dll with error: %d\n",
(int)GetLastError());
(void)pthread_mutex_unlock(&traceLock);
return 0;
}
optSym = (SymSetOptionsType)GetProcAddress(
hModule, "SymSetOptions");
if (0 == optSym)
{
fprintf(stderr, "Failed to find SymSetOptions: %d\n",
(int)GetLastError());
(void)pthread_mutex_unlock(&traceLock);
return 0;
}
initSym = (SymInitializeType)GetProcAddress(
hModule, "SymInitialize");
if (0 == initSym)
{
fprintf(stderr, "Failed to find SymInitialize: %d\n",
(int)GetLastError());
(void)pthread_mutex_unlock(&traceLock);
return 0;
}
fromSym = (SymFromAddrType)GetProcAddress(
hModule, "SymFromAddr");
if (0 == fromSym)
{
fprintf(stderr, "Failed to find SymFromAddr: %d\n",
(int)GetLastError());
(void)pthread_mutex_unlock(&traceLock);
return 0;
}
}
(optSym)(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS);
if (!(initSym)(hProcess, NULL, TRUE))
{
fprintf(stderr, "SymInitialize failed with error: %d\n",
(int)GetLastError());
fromSym = 0;
(void)pthread_mutex_unlock(&traceLock);
return 0;
}
}
if (0 == capture)
{
(void)pthread_mutex_unlock(&traceLock);
return 0;
}
numReturns = (capture)(0, MAXFRAMES, (void**)addr, NULL);
if (numReturns > 0)
{
*returns = malloc(numReturns * sizeof(void*));
memcpy(*returns, addr, numReturns * sizeof(void*));
}
(void)pthread_mutex_unlock(&traceLock);
#elif defined(HAVE_BACKTRACE)
void *addr[MAXFRAMES*sizeof(void*)];
numReturns = backtrace(addr, MAXFRAMES);
if (numReturns > 0)
{
*returns = malloc(numReturns * sizeof(void*));