This is the mail archive of the mailing list for the binutils project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: addr2line ends up off by 1 line numbers when -O0 is used

> 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

(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.)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]