Find offset of block instruction in function

Martin Sebor msebor@gmail.com
Mon Mar 21 21:21:00 GMT 2016


On 03/21/2016 12:59 AM, crasypantz wrote:
> Hello,
>
> Given the result of 'DECL_SAVED_TREE' for a FUNCTION_DECL, is there a way to
> determine the offset into the function for a given block instruction?
> Perhaps the question is not worded correctly, or maybe what I'm asking
> doesn't make sense. But essentially, say there is a function:
>
> void something() {
>      int blah;
>
>      blah = 2;
>
>      if (1) {
>          printf("blah: %d\n, blah);
>      }
> }
>
> In this case is there a way to determine the offset of 'if (1)' or 'printf'?
> By 'offset' I mean in the same sense 'backtrace_symbols' includes "a
> hexadecimal offset into the function." If so does it depend on optimization
> or anything else?

I'm not sure if you're asking about how to do this within GCC
itself as it's compiling a function or in general (outside GCC).
I don't know the answer to the first question though I would
expect it to be possible (if perhaps not easy) since the latter
is both (possible and easy).  If you are asking about GCC
internals you might have better luck in the gcc development
list.  gcc-help is for questions about building or using GCC.

The DWARF debugging information emitted by compilers contains
what's called a line number program.  The program is a sequence
of DWARF opcodes and a mapping between instruction addresses and
source file names and line numbers that let the debugger follow
the execution of the the program as the user steps through it
line by line.  The line number program can be viewed by Binutils
(see objdump --dwarf=rawline) and Elfutils (readelf -wl).

This is independent of optimization but as you may have seen
while debugging an optimized program, it's not always 100%
accurate due to code being moved around or even eliminated
by the optimizer.  It does (obviously) depend on enabling
debugging with -g.

Martin



More information about the Gcc-help mailing list