1 /* Copyright (c) 2008 Johannes Fortmann
3 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:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 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. */
9 #import <Foundation/NSMutableArray.h>
10 #import <Foundation/NSValue.h>
11 #import <Foundation/NSString.h>
16 static void *_objc_returnAddress(unsigned frame)
19 #define ADDR(x) case x: ret=__builtin_return_address(x); break;
70 static jmp_buf handleBadAccess;
72 static void _objc_badAccessHandler()
74 longjmp(handleBadAccess, 1);
81 NSMutableArray *ret=[NSMutableArray array];
84 void *oldSegv=signal(SIGSEGV, _objc_badAccessHandler);
87 void *oldBus=signal(SIGBUS, _objc_badAccessHandler);
90 // only _objc_guardedReturnAddress may fail in the below code (because of a corrupted/unexpected stack)
91 // since we're not in a library function at that time, we can longjmp to the following error handling routine
92 if(setjmp(handleBadAccess))
94 NSLog(@"Protection fault during creation of stack trace.");
95 [ret addObject:@"<invalid frame>"];
99 int frame=2; // Skip _objc_returnAddress and _NSStackTrace - they are always there
100 void *addr=_objc_returnAddress(frame);
104 [ret addObject:[NSValue valueWithPointer:addr]];
106 addr=_objc_returnAddress(frame);
111 signal(SIGSEGV, oldSegv);
114 signal(SIGBUS, oldBus);
119 return [NSArray arrayWithObject:@"Stack trace unavailable in Release builds"];