Tectrendz
Tectrendz

Reputation: 1454

GDB backtrace :Find total number of frame

I am trying to find the start and end of stack using macro from a core file. To accomplish the same I am trying to find the esp value from last and first frame . Difference of the same will give me the stack size in use.

Do we have a way to find number of frames in the stack? bt give me all the frame . from frame 0 i can find the top of the stack? do we have a way to find the last frame number ?

Do do we have another way to find the start of the stack and end of the stack ? Thanks

Upvotes: 7

Views: 2922

Answers (1)

Tom Tromey
Tom Tromey

Reputation: 22559

You can find the frame number of the outermost frame using bt -1:

(gdb) bt -1
#9  0x0000000000464c45 in main (argc=<optimized out>, argv=<optimized out>)
    at ../../binutils-gdb/gdb/gdb.c:32

You can see that the outermost frame is #9.

However, scripting this is a bit of a pain. For example, you can't use frame -1 to select that frame.

At this point you have two options.

One option is to use the gdb Python API to iterate over frames and do whatever you like. This is the simplest route, if it is available to you. The Python API is documented and easy to use; in this case you will mostly be interested in the gdb.Frame code.

If you can't use Python, you can use the traditional, horrible gdb hack of using set logging to write the output of bt -1 to a file; then shell to rewrite the contents of this file to be a valid gdb command (like frame 9); and finally source the resulting transformed file.

Upvotes: 10

Related Questions