Re: Help on Stack trace
Re: Help on Stack trace
- Subject: Re: Help on Stack trace
- From: Rick Altherr <email@hidden>
- Date: Mon, 9 Feb 2009 10:15:29 -0800
On Feb 8, 2009, at 3:35 PM, Steve Checkoway wrote:
On Feb 6, 2009, at 9:18 AM, Peter O'Gorman wrote:
dladdr will give you the name and address of "the nearest run-time
symbol with a value less than or equal to addr". Where run-time
symbol,
means exported global symbol.
Interestingly enough, using some of your code, you can do a lot
better.
Given
static void foo( int *p ) { ++*p; }
int bar() { return 0; }
the code snippet (full code listing attached, assuming the list
allows it)
Dl_info info;
if( dladdr(foo, &info) )
printf( "foo: %s %s %p\n", info.dli_fname, info.dli_sname,
info.dli_saddr );
if( dladdr(bar, &info) )
printf( "bar: %s %s %p\n", info.dli_fname, info.dli_sname,
info.dli_saddr );
FromAddr( foo );
if( Symbol )
printf( "foo: %s %s %#tx\n", File, Symbol, (ptrdiff_t)Address );
Symbol = NULL;
FromAddr( bar );
if( Symbol )
printf( "bar: %s %s %#tx\n", File, Symbol, (ptrdiff_t)Address );
prints out
foo: /Users/steve/temp/./a.out FromAddr 0x19fc
bar: /Users/steve/temp/./a.out bar 0x1d70
foo: /Users/steve/temp/./a.out _foo 0x1d40
bar: /Users/steve/temp/./a.out _bar 0x1d70
In particular, the FromAddr() function finds foo even though it is
not an exported global symbol.
In this case, since foo() was referenced by address (the calls to
FromAddr and dladdr), a symbol was generated for it. If your
application just called the function instead of referencing it, the
compiler will most likely inline it and has no obligation to generate
a symbol for it. The debugging information will correctly show that
that address within the calling function came from the lines of source
where foo() is defined, but the symbol table will show it came from
the calling function.
This also assumes the binary being read hasn't been stripped. If the
binary has the debugging info stripped, this should still work, but if
all non-global symbols have been stripped, you will get the same
results as dladdr(). Worse, a production application typically has
every symbol except for __start() stripped.
The attached file is very slightly adapted from <http://svn.stepmania.com/viewvc/bin/cgi/viewvc.cgi/trunk/stepmania/src/archutils/Unix/BacktraceNames.cpp?view=markup
>. In particular converted from c++ to c, hence the global variables.
<a.c>
There are a few potential bugs in this implementation. The main one
being that in Leopard, the segments of a binary can be slid
independently. This implementation assumes that the LINKEDIT segment
will have the same slide as the __TEXT segment. While this is likely
(assuming minimal address space fragmentation), it isn't guaranteed.
--
Steve Checkoway
"Anyone who says that the solution is to educate the users
hasn't ever met an actual user." -- Bruce Schneier
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
@apple.com
This email sent to email@hidden
--
Rick Altherr
Architecture and Performance Group
email@hidden
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden