Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: printing a stack trace to the console



On 31. Mar 2004, at 10:44, Jonathan Sand wrote:

I've looked into "defaults write NSGlobalDomain NSExceptionHandlingMask
63" but defaults apparently doesn't work quite that way anymore.

I do this in code:
[[NSExceptionHandler defaultExceptionHandler]
setExceptionHandlingMask:NSLogAndHandleEveryExceptionMask];

If it did, I could write the following objective-C method:

- (void)printStackTrace {
NS_DURING
[NSException raise:0 format:@"stack trace requested!"];
NS_HANDLER
NSLog(@"%@", [localException userInfo]);
NS_ENDHANDLER
}

That's pretty much what I do, except that I use 'atos' to convert the address to symbols and 'c++filt' to demangle the C++ symbols (and then 'tail' to cut the first few lines and 'cat' to enumerate the listing), all in all my NS_HANDLER ends up being:

fprintf(stderr, "------ STACK TRACE ------\n");
if(id trace = [[localException userInfo] objectForKey:NSStackTraceKey])
{
NSString* str = [NSString stringWithFormat:@"/usr/bin/atos -p %d %@ | tail -n +4 | c++filt3 | cat -n", getpid(), trace];
if(FILE* fp = popen([str UTF8String], "r"))
{
unsigned char resBuf[512];
while(size_t len = fread(resBuf, 1, sizeof(resBuf), fp))
fwrite(resBuf, 1, len, stderr);
pclose(fp);
}
}
fprintf(stderr, "-------------------------\n");




** Cocoa FAQ: <http://www.alastairs-place.net/cocoa/faq.txt> **
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >printing a stack trace to the console (From: Jonathan Sand <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.