backport stackframe bugfixes

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/branches/stable@24557 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2007-02-14 12:44:22 +00:00
parent 7f71c718b3
commit 6c08f6b082
3 changed files with 423 additions and 214 deletions

View file

@ -1,3 +1,9 @@
2007-02-14 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSDebug.m:
* Source/NSException.m:
Backport stackframe/stacktrace generation bugfixes from trunk
2007-02-06 Richard Frith-Macdonald <rfm@gnu.org>
* Source/GSHTTPURLHandle.m:

View file

@ -20,7 +20,8 @@
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA.
<title>NSDebug utilities reference</title>
$Date$ $Revision$
@ -29,10 +30,12 @@
#include "config.h"
#include <stdio.h>
#include "GNUstepBase/GSLock.h"
#include "Foundation/NSArray.h"
#include "Foundation/NSData.h"
#include "Foundation/NSDebug.h"
#include "Foundation/NSString.h"
#include "Foundation/NSLock.h"
#include "Foundation/NSNotification.h"
#include "Foundation/NSNotificationQueue.h"
#include "Foundation/NSThread.h"
@ -63,11 +66,13 @@ static NSLock *uniqueLock = nil;
static const char* _GSDebugAllocationList(BOOL difference);
static const char* _GSDebugAllocationListAll(void);
void _GSDebugAllocationAdd(Class c, id o);
void _GSDebugAllocationRemove(Class c, id o);
static void _GSDebugAllocationAdd(Class c, id o);
static void _GSDebugAllocationRemove(Class c, id o);
void (*_GSDebugAllocationAddFunc)(Class c, id o) = _GSDebugAllocationAdd;
void (*_GSDebugAllocationRemoveFunc)(Class c, id o) = _GSDebugAllocationRemove;
static void (*_GSDebugAllocationAddFunc)(Class c, id o)
= _GSDebugAllocationAdd;
static void (*_GSDebugAllocationRemoveFunc)(Class c, id o)
= _GSDebugAllocationRemove;
@interface GSDebugAlloc : NSObject
+ (void) initialize;
@ -849,53 +854,6 @@ GSDebugMethodMsg(id obj, SEL sel, const char *file, int line, NSString *fmt)
return message;
}
// static void *_frameOffsets[100];
#define _NS_FRAME_HACK(a) case a: return __builtin_frame_address(a + 1)
#define _NS_RETURN_HACK(a) case a: return __builtin_return_address(a + 1)
void *NSFrameAddress(int offset)
{
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);
}
return NULL;
}
unsigned NSCountFrames(void)
{
unsigned x = 0;
@ -905,48 +863,178 @@ unsigned NSCountFrames(void)
return x;
}
void *NSReturnAddress(int offset)
{
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);
}
#define _NS_FRAME_HACK(a) case a: val = __builtin_frame_address(a + 1); break;
#define _NS_RETURN_HACK(a) case a: val = __builtin_return_address(a + 1); break;
return NULL;
/*
* 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 setjmp() and longjmp() 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.
*/
#include <signal.h>
#if defined(__MINGW32__)
#include <setjmp.h>
#endif
static jmp_buf *
jbuf()
{
NSMutableData *d;
d = [[[NSThread currentThread] threadDictionary] objectForKey: @"GSjbuf"];
if (d == nil)
{
d = [[NSMutableData alloc] initWithLength:
sizeof(jmp_buf) + sizeof(void(*)(int))];
[[[NSThread currentThread] threadDictionary] setObject: d
forKey: @"GSjbuf"];
RELEASE(d);
}
return (jmp_buf*)[d mutableBytes];
}
static void
recover(int sig)
{
jmp_buf *env = jbuf();
longjmp(*env, 1);
}
void *
NSFrameAddress(int offset)
{
jmp_buf *env;
void (*old)(int);
void *val;
env = jbuf();
if (setjmp(*env) == 0)
{
old = signal(SIGSEGV, recover);
memcpy(env + 1, &old, sizeof(old));
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: val = NULL; break;
}
signal(SIGSEGV, old);
}
else
{
env = jbuf();
memcpy(&old, env + 1, sizeof(old));
signal(SIGSEGV, old);
val = NULL;
}
return val;
}
void *
NSReturnAddress(int offset)
{
jmp_buf *env;
void (*old)(int);
void *val;
env = jbuf();
if (setjmp(*env) == 0)
{
old = signal(SIGSEGV, recover);
memcpy(env + 1, &old, sizeof(old));
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: val = NULL; break;
}
signal(SIGSEGV, old);
}
else
{
env = jbuf();
memcpy(&old, env + 1, sizeof(old));
signal(SIGSEGV, old);
val = NULL;
}
return val;
}
const char *_NSPrintForDebugger(id object)
{
if (object && [object respondsToSelector: @selector(description)])
@ -957,6 +1045,7 @@ const char *_NSPrintForDebugger(id object)
NSString *_NSNewStringFromCString(const char *cstring)
{
return [NSString stringWithCString: cstring];
return [NSString stringWithCString: cstring
encoding: [NSString defaultCStringEncoding]];
}

View file

@ -34,29 +34,83 @@
#include "Foundation/NSCoder.h"
#include "Foundation/NSNull.h"
#include "Foundation/NSThread.h"
#include "Foundation/NSLock.h"
#include "Foundation/NSDictionary.h"
#include <stdio.h>
#if defined(__MINGW32__)
static NSString *
GSPrivateBaseAddress(void *addr, void **base)
{
return nil;
}
#else /* __MINGW32__ */
#ifndef GNU_SOURCE
#define GNU_SOURCE
#endif
#ifndef __USE_GNU
#define __USE_GNU
#endif
#include <dlfcn.h>
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 /* __MINGW32__ */
/* This is the GNU name for the CTOR list */
@interface GSStackTrace : NSObject
{
NSMutableArray *frames;
}
+ (GSStackTrace*) currentStack;
- (NSString*) description;
- (NSEnumerator*) enumerator;
- (id) frameAt: (unsigned)index;
- (unsigned) frameCount;
- (NSEnumerator*) reverseEnumerator;
@end
#define STACKSYMBOLS 1
/*
* Turn off STACKTRACE if we don't have bfd support for it.
* Turn off STACKSYMBOLS if we don't have bfd support for it.
*/
#if !(defined(HAVE_BFD_H) && defined(HAVE_LIBBFD) && defined(HAVE_LIBIBERTY))
#if defined(STACKTRACE)
#undef STACKTRACE
#if defined(STACKSYMBOLS)
#undef STACKSYMBOLS
#endif
#endif
/*
* Turn off STACKTRACE if we don't have DEBUG defined ... if we are not built
* Turn off STACKSYMBOLS if we don't have DEBUG defined ... if we are not built
* with DEBUG then we are probably missing stackframe information etc.
*/
#if !(defined(DEBUG))
#if defined(STACKTRACE)
#undef STACKTRACE
#if defined(STACKSYMBOLS)
#undef STACKSYMBOLS
#endif
#endif
#if defined(STACKTRACE)
#if defined(STACKSYMBOLS)
// GSStackTrace inspired by FYStackTrace.m
// created by Wim Oudshoorn on Mon 11-Apr-2006
@ -90,40 +144,18 @@
@interface GSBinaryFileInfo : NSObject
{
NSString *_filename;
NSString *_fileName;
bfd *_abfd;
asymbol **_symbols;
long _symbolCount;
}
- (NSString *) filename;
- (NSString *) fileName;
- (GSFunctionInfo *) functionForAddress: (void*) address;
- (id) initWithBinaryFile: (NSString *)filename;
- (id) initWithBinaryFile: (NSString *)fileName;
- (id) init; // return info for the current executing process
@end
@interface GSStackTrace : NSObject
{
NSMutableArray *frames;
}
+ (GSStackTrace*) currentStack;
/*
* Add some module information to the stack trace information
* only symbols from the current process's file, GNUstep base library,
* GNUstep gui library, and any bundles containing code are loaded.
* All other symbols should be manually added
*/
+ (BOOL) loadModule: (NSString *)filename;
- (NSString*) description;
- (NSEnumerator*) enumerator;
- (GSFunctionInfo*) frameAt: (unsigned)index;
- (unsigned) frameCount;
- (NSEnumerator*) reverseEnumerator;
@end
@implementation GSFunctionInfo
@ -147,7 +179,7 @@
- (NSString *) description
{
return [NSString stringWithFormat: @"(%@: %p) %@ %@: %d",
[_module filename], _address, _functionName, _fileName, _lineNo];
[_module fileName], _address, _functionName, _fileName, _lineNo];
}
- (NSString *) fileName
@ -197,9 +229,9 @@
@implementation GSBinaryFileInfo
+ (GSBinaryFileInfo*) infoWithBinaryFile: (NSString *)filename
+ (GSBinaryFileInfo*) infoWithBinaryFile: (NSString *)fileName
{
return [[[self alloc] initWithBinaryFile: filename] autorelease];
return [[[self alloc] initWithBinaryFile: fileName] autorelease];
}
+ (void) initialize
@ -216,8 +248,8 @@
- (oneway void) dealloc
{
[_filename release];
_filename = nil;
[_fileName release];
_fileName = nil;
if (_abfd)
{
bfd_close (_abfd);
@ -231,9 +263,9 @@
[super dealloc];
}
- (NSString *) filename
- (NSString *) fileName
{
return _filename;
return _fileName;
}
- (id) init
@ -244,28 +276,28 @@
return [self initWithBinaryFile: processName];
}
- (id) initWithBinaryFile: (NSString *)filename
- (id) initWithBinaryFile: (NSString *)fileName
{
int neededSpace;
// 1st initialize the bfd
if ([filename length] == 0)
if ([fileName length] == 0)
{
NSLog (@"GSBinaryFileInfo: No File");
//NSLog (@"GSBinaryFileInfo: No File");
[self release];
return nil;
}
_filename = [filename copy];
_abfd = bfd_openr ([filename cString], NULL);
_fileName = [fileName copy];
_abfd = bfd_openr ([fileName cString], NULL);
if (!_abfd)
{
NSLog (@"GSBinaryFileInfo: No Binary Info");
//NSLog (@"GSBinaryFileInfo: No Binary Info");
[self release];
return nil;
}
if (!bfd_check_format_matches (_abfd, bfd_object, NULL))
{
NSLog (@"GSBinaryFileInfo: BFD format object error");
//NSLog (@"GSBinaryFileInfo: BFD format object error");
[self release];
return nil;
}
@ -273,7 +305,7 @@
// second read the symbols from it
if (!(bfd_get_file_flags (_abfd) & HAS_SYMS))
{
NSLog (@"GSBinaryFileInfo: BFD does not contain any symbols");
//NSLog (@"GSBinaryFileInfo: BFD does not contain any symbols");
[self release];
return nil;
}
@ -281,27 +313,27 @@
neededSpace = bfd_get_symtab_upper_bound (_abfd);
if (neededSpace < 0)
{
NSLog (@"GSBinaryFileInfo: BFD error while deducing needed space");
//NSLog (@"GSBinaryFileInfo: BFD error while deducing needed space");
[self release];
return nil;
}
if (neededSpace == 0)
{
NSLog (@"GSBinaryFileInfo: BFD no space for symbols needed");
//NSLog (@"GSBinaryFileInfo: BFD no space for symbols needed");
[self release];
return nil;
}
_symbols = objc_malloc (neededSpace);
if (!_symbols)
{
NSLog (@"GSBinaryFileInfo: Can't allocate buffer");
//NSLog (@"GSBinaryFileInfo: Can't allocate buffer");
[self release];
return nil;
}
_symbolCount = bfd_canonicalize_symtab (_abfd, _symbols);
if (_symbolCount < 0)
{
NSLog (@"GSBinaryFileInfo: BFD error while reading symbols");
//NSLog (@"GSBinaryFileInfo: BFD error while reading symbols");
[self release];
return nil;
}
@ -336,10 +368,16 @@ static void find_address (bfd *abfd, asection *section,
return;
}
address = (bfd_vma) info->theAddress;
address = (bfd_vma) (intptr_t)info->theAddress;
vma = bfd_get_section_vma (abfd, section);
size = bfd_get_section_size (section);
#if defined(bfd_get_section_size)
size = bfd_get_section_size (section); // recent
#else
size = bfd_section_size (abfd, section); // older version
#endif
if (address < vma || address >= vma + size)
{
return;
@ -348,13 +386,25 @@ static void find_address (bfd *abfd, asection *section,
if (bfd_find_nearest_line (abfd, section, info->symbols,
address - vma, &fileName, &functionName, &line))
{
GSFunctionInfo *fi;
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: [NSString stringWithCString: fileName]
function: [NSString stringWithCString: functionName]
file: file
function: func
line: line];
[fi autorelease];
info->theInfo = fi;
@ -372,10 +422,16 @@ static void find_address (bfd *abfd, asection *section,
@end
// this method automatically load the current process + GNUstep base & gui.
static NSMutableDictionary *GetStackModules()
static NSRecursiveLock *modLock = nil;
static NSMutableDictionary *stackModules = nil;
// initialize stack trace info
static id
GSLoadModule(NSString *fileName)
{
static NSMutableDictionary *stackModules = nil;
GSBinaryFileInfo *module = nil;
[modLock lock];
if (stackModules == nil)
{
@ -399,56 +455,62 @@ static NSMutableDictionary *GetStackModules()
{
if ([bundle load] == YES)
{
[GSStackTrace loadModule: [bundle executablePath]];
GSLoadModule([bundle executablePath]);
}
}
}
return stackModules;
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];
}
}
}
[modLock unlock];
if (module == (id)[NSNull null])
{
module = nil;
}
return module;
}
@implementation GSStackTrace : NSObject
static NSArray*
GSListModules()
{
NSArray *result;
static NSNull *null = nil;
GSLoadModule(nil); // initialise
[modLock lock];
result = [stackModules allValues];
[modLock unlock];
return result;
}
#endif /* STACKSYMBOLS */
@implementation GSStackTrace : NSObject
+ (GSStackTrace*) currentStack
{
return [[[GSStackTrace alloc] init] autorelease];
}
+ (void) initialize
{
null = RETAIN([NSNull null]);
}
// initialize stack trace info
+ (BOOL) loadModule: (NSString *)filename
{
if ([filename length] > 0)
{
NSMutableDictionary *modules = GetStackModules();
if ([modules objectForKey: filename] == nil)
{
GSBinaryFileInfo *module;
module = [GSBinaryFileInfo infoWithBinaryFile: filename];
if (module != nil)
{
[modules setObject: module forKey: filename];
}
else
{
[modules setObject: null forKey: filename];
}
}
if ([modules objectForKey: filename] != null)
{
return YES;
}
}
return NO;
}
- (oneway void) dealloc
{
[frames release];
@ -466,7 +528,7 @@ static NSNull *null = nil;
n = [frames count];
for (i = 0; i < n; i++)
{
GSFunctionInfo *line = [frames objectAtIndex: i];
id line = [frames objectAtIndex: i];
[result appendFormat: @"%3d: %@\n", i, line];
}
@ -478,7 +540,7 @@ static NSNull *null = nil;
return [frames objectEnumerator];
}
- (GSFunctionInfo*) frameAt: (unsigned)index
- (id) frameAt: (unsigned)index
{
return [frames objectAtIndex: index];
}
@ -489,42 +551,63 @@ static NSNull *null = nil;
}
// grab the current stack
// this MAX_FRAME comes from NSDebug.h which warn only 100 frames are available
#define MAX_FRAME 100
- (id) init
{
NSArray *modules;
#if defined(STACKSYMBOLS)
int i;
int j;
int n;
int m;
frames = [[NSMutableArray alloc] init];
modules = [GetStackModules() allValues];
n = NSCountFrames();
m = [modules count];
for (i = 0; i < n && i < MAX_FRAME; i++)
for (i = 0; i < n; i++)
{
GSFunctionInfo *aFrame = nil;
void *address = NSReturnAddress(i);
void *base;
NSString *modulePath = GSPrivateBaseAddress(address, &base);
GSBinaryFileInfo *bfi;
for (j = 0; j < m; j++)
{
GSBinaryFileInfo *bfi = [modules objectAtIndex: j];
if ((id)bfi != (id)null)
if (modulePath != nil && (bfi = GSLoadModule(modulePath)) != nil)
{
aFrame = [bfi functionForAddress: (void*)(address - base)];
if (aFrame == nil)
{
/* We know we have the right module be function lookup
* failed ... perhaps we need to use the absolute
* address rather than offest by 'base' in this case.
*/
aFrame = [bfi functionForAddress: address];
if (aFrame)
}
//if (aFrame == nil) NSLog(@"BFI base for %@ (%p) is %p", modulePath, address, base);
}
else
{
NSArray *modules;
int j;
int m;
//if (modulePath != nil) NSLog(@"BFI not found for %@ (%p)", modulePath, address);
modules = GSListModules();
m = [modules count];
for (j = 0; j < m; j++)
{
bfi = [modules objectAtIndex: j];
if ((id)bfi != (id)[NSNull null])
{
[frames addObject: aFrame];
break;
aFrame = [bfi functionForAddress: address];
if (aFrame != nil)
{
break;
}
}
}
}
}
// not found (?!), add an 'unknown' function
if (!aFrame)
if (aFrame == nil)
{
aFrame = [GSFunctionInfo alloc];
[aFrame initWithModule: nil
@ -533,9 +616,23 @@ static NSNull *null = nil;
function: nil
line: 0];
[aFrame autorelease];
[frames addObject: aFrame];
}
[frames addObject: aFrame];
}
#else
int i;
int n;
frames = [[NSMutableArray alloc] init];
n = NSCountFrames();
for (i = 0; i < n; i++)
{
void *address = NSReturnAddress(i);
[frames addObject: [NSString stringWithFormat: @"%p", address]];
}
#endif
return self;
}
@ -547,7 +644,6 @@ static NSNull *null = nil;
@end
#endif /* STACKTRACE */
@ -598,17 +694,34 @@ static void _terminate()
static void
_NSFoundationUncaughtExceptionHandler (NSException *exception)
{
extern const char* GSArgZero(void);
extern const char *GSArgZero(void);
NSString *stack;
fprintf(stderr, "%s: Uncaught exception %s, reason: %s\n", GSArgZero(),
[[exception name] lossyCString], [[exception reason] lossyCString]);
fflush(stderr); /* NEEDED UNDER MINGW */
stack = [[[exception userInfo] objectForKey: @"GSStackTraceKey"] description];
if (stack != nil)
{
fprintf(stderr, "Stack\n%s\n", [stack lossyCString]);
}
fflush(stderr); /* NEEDED UNDER MINGW */
_terminate();
}
@implementation NSException
#if defined(STACKSYMBOLS)
+ (void) initialize
{
if (modLock == nil)
{
modLock = [NSRecursiveLock new];
}
}
#endif /* STACKSYMBOLS */
+ (NSException*) exceptionWithName: (NSString*)name
reason: (NSString*)reason
userInfo: (NSDictionary*)userInfo
@ -665,8 +778,9 @@ _NSFoundationUncaughtExceptionHandler (NSException *exception)
NSHandler *handler;
#endif
#if defined(STACKTRACE)
if ([_e_info objectForKey: @"GSStackTraceKey"] == nil)
#if defined(DEBUG)
if (GSEnvironmentFlag("GNUSTEP_STACK_TRACE", NO) == YES
&& [_e_info objectForKey: @"GSStackTraceKey"] == nil)
{
NSMutableDictionary *m;