Merge pull request #9 from gunyarakun/fix-typo
[cocotron.git] / Foundation / NSDebug.m
blobd6ddcb865a7ae33b90a9210675a5d2199bac7f1e
1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
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/NSDebug.h>
10 #import <Foundation/NSString.h>
11 #ifdef WINDOWS
12 #include <windows.h>
13 #endif
15 #if defined(LINUX) ||  defined(__APPLE__)  ||  defined(FREEBSD)
16 #include <execinfo.h>
17 #endif
19 BOOL NSZombieEnabled=NO;
20 BOOL NSDebugEnabled=NO;
21 BOOL NSCooperativeThreadsEnabled=NO;
23 const char* _NSPrintForDebugger(id object) {
24         if(object && [object respondsToSelector:@selector(description)]) {
25                 return [[object description] UTF8String];
26         }
27         return NULL;
30 #ifndef WINDOWS
31 void NSCooperativeThreadBlocking() {
34 void NSCooperativeThreadWaiting() {
36 #else
39 static HANDLE NSCooperativeEvent(){
40    static HANDLE handle=NULL;
42    if(handle==NULL)
43     handle=CreateEvent(NULL,FALSE,FALSE,NULL);
45    return handle;
48 void NSCooperativeThreadBlocking() {
49    if(NSCooperativeThreadsEnabled){
50     SetEvent(NSCooperativeEvent());
51    }
54 void NSCooperativeThreadWaiting() {
55    if(NSCooperativeThreadsEnabled){
56     WaitForSingleObject(NSCooperativeEvent(),0);
57    }
59 #endif
61 #define _NS_RETURN_ADDRESS(x) case x: return __builtin_return_address(x + 1)
63 void *NSFrameAddress(NSUInteger level)
65     void* callstack[128];
67     if (level > 128) {
68         return NULL;
69     }
70     
71     int i, frameCount = backtrace(callstack, level + 1);
72     if (frameCount < level + 1) {
73         return NULL;
74     }
75     
76     return callstack[level + 1];
79 unsigned NSCountFrames(void)
81     unsigned    x = 0;
83     while (NSFrameAddress(x + 1) != NULL){
84      x++;
85     }
87     return x;
90 void *NSReturnAddress(int level)
92     switch (level) {
93             _NS_RETURN_ADDRESS(0);
94             _NS_RETURN_ADDRESS(1);
95             _NS_RETURN_ADDRESS(2);
96             _NS_RETURN_ADDRESS(3);
97             _NS_RETURN_ADDRESS(4);
98             _NS_RETURN_ADDRESS(5);
99             _NS_RETURN_ADDRESS(6);
100             _NS_RETURN_ADDRESS(7);
101             _NS_RETURN_ADDRESS(8);
102             _NS_RETURN_ADDRESS(9);
103             _NS_RETURN_ADDRESS(10);
104             _NS_RETURN_ADDRESS(11);
105             _NS_RETURN_ADDRESS(12);
106             _NS_RETURN_ADDRESS(13);
107             _NS_RETURN_ADDRESS(14);
108             _NS_RETURN_ADDRESS(15);
109             _NS_RETURN_ADDRESS(16);
110             _NS_RETURN_ADDRESS(17);
111             _NS_RETURN_ADDRESS(18);
112             _NS_RETURN_ADDRESS(19);
113             _NS_RETURN_ADDRESS(20);
114             _NS_RETURN_ADDRESS(21);
115             _NS_RETURN_ADDRESS(22);
116             _NS_RETURN_ADDRESS(23);
117             _NS_RETURN_ADDRESS(24);
118             _NS_RETURN_ADDRESS(25);
119             _NS_RETURN_ADDRESS(26);
120             _NS_RETURN_ADDRESS(27);
121             _NS_RETURN_ADDRESS(28);
122             _NS_RETURN_ADDRESS(29);
123             _NS_RETURN_ADDRESS(30);
124             _NS_RETURN_ADDRESS(31);
125             _NS_RETURN_ADDRESS(32);
126             _NS_RETURN_ADDRESS(33);
127             _NS_RETURN_ADDRESS(34);
128             _NS_RETURN_ADDRESS(35);
129             _NS_RETURN_ADDRESS(36);
130             _NS_RETURN_ADDRESS(37);
131             _NS_RETURN_ADDRESS(38);
132             _NS_RETURN_ADDRESS(39);
133             _NS_RETURN_ADDRESS(40);
134             _NS_RETURN_ADDRESS(41);
135             _NS_RETURN_ADDRESS(42);
136             _NS_RETURN_ADDRESS(43);
137             _NS_RETURN_ADDRESS(44);
138             _NS_RETURN_ADDRESS(45);
139             _NS_RETURN_ADDRESS(46);
140             _NS_RETURN_ADDRESS(47);
141             _NS_RETURN_ADDRESS(48);
142             _NS_RETURN_ADDRESS(49);
143             _NS_RETURN_ADDRESS(50);
144         default: return NULL;
145     }
147     return NULL;