Merge pull request #10 from gunyarakun/fix-invalid-return
[cocotron.git] / Foundation / NSException / debugHelpers.m
blob9566b2a6c01160b3ea0f5f3c0024345e07f2baaa
1 /* Copyright (c) 2008 Johannes Fortmann
2  
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:
4  
5  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6  
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>
13 #include <setjmp.h>
15 #ifdef DEBUG
16 static void *_objc_returnAddress(unsigned frame)
18    void *ret=0;
19 #define ADDR(x) case x: ret=__builtin_return_address(x); break;
20    switch(frame)
21    {
22          ADDR(0)
23          ADDR(1)
24          ADDR(2)
25          ADDR(3)
26          ADDR(4)
27          ADDR(5)
28          ADDR(6)
29          ADDR(7)
30          ADDR(8)
31          ADDR(9)
32          ADDR(10)
33          ADDR(11)
34          ADDR(12)
35          ADDR(13)
36          ADDR(14)
37          ADDR(15)
38          ADDR(16)
39          ADDR(17)
40          ADDR(18)
41          ADDR(19)
42            ADDR(20)
43            ADDR(21)
44            ADDR(22)
45            ADDR(23)
46            ADDR(24)
47            ADDR(25)
48            ADDR(26)
49            ADDR(27)
50            ADDR(28)
51            ADDR(29)
52            ADDR(30)
53            ADDR(31)
54            ADDR(32)
55            ADDR(33)
56            ADDR(34)
57            ADDR(35)
58            ADDR(36)
59            ADDR(37)
60            ADDR(38)
61            ADDR(39)
62       default:
63          ;
64    }
65 #undef ADDR
66    
67    return ret;
70 static jmp_buf handleBadAccess;
72 static void _objc_badAccessHandler()
74    longjmp(handleBadAccess, 1);
76 #endif
78 id _NSStackTrace()
80 #ifdef DEBUG
81    NSMutableArray *ret=[NSMutableArray array];
83 #ifdef SIGSEGV
84    void *oldSegv=signal(SIGSEGV, _objc_badAccessHandler);
85 #endif
86 #ifdef SIGBUS
87    void *oldBus=signal(SIGBUS, _objc_badAccessHandler);
88 #endif
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))
93    {
94       NSLog(@"Protection fault during creation of stack trace.");
95       [ret addObject:@"<invalid frame>"];
96       goto restore;
97    }
98    
99    int frame=2; // Skip _objc_returnAddress and _NSStackTrace - they are always there
100    void *addr=_objc_returnAddress(frame);
101    
102    while(addr)
103    {
104       [ret addObject:[NSValue valueWithPointer:addr]];
105       frame++;
106       addr=_objc_returnAddress(frame);
107    }
108    
109 restore:
110 #ifdef SIGSEGV
111    signal(SIGSEGV, oldSegv);
112 #endif
113 #ifdef SIGBUS
114    signal(SIGBUS, oldBus);
115 #endif
117    return ret;
118 #else
119    return [NSArray arrayWithObject:@"Stack trace unavailable in Release builds"];
120 #endif