This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: addr2line ends up off by 1 line numbers when -O0 is used
- From: Cary Coutant <ccoutant at gmail dot com>
- To: Britton Kerin <britton dot kerin at gmail dot com>
- Cc: Binutils <binutils at sourceware dot org>
- Date: Thu, 17 Mar 2016 16:17:37 -0700
- Subject: Re: addr2line ends up off by 1 line numbers when -O0 is used
- Authentication-results: sourceware.org; auth=none
- References: <CAC4O8c-=Zs4AXSP1ZM8DnABCuwrgQXGJuvsb0sefVo-ZPqt3Pw at mail dot gmail dot com>
> I'm not sure whether to blame libc (backtrace() function) or gcc or
> addr2line, but if I dump the pointers from backtrace() to
> address_file then use
>
> addr2line -e my_exe -f -i <address_file >call_locations
>
> to recover the line numbers, the line numbers are off by 1 statement
> for some stack frames when -O0 has been used, but not if -O2 has been
> used at compile time.
Try subtracting 1 from each of the return addresses in the backtrace
(i.e., all but the first). The return address from a call is usually
the first instruction of the statement following the call, so if you
lookup that address, you'll get the line you're returning to, not the
line the call came from. Most unwinders will subtract 1 from the
return addresses when looking up line numbers on the fly, but if
you're trying to the annotation offline, that's not happening
automatically.
(This is a common trick, but it's only a heuristic -- it may fail if
the caller does something weird like synthesizing an out-of-line
return address.)
-cary