Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDB backtrace :Find total number of frame

Tags:

gdb

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

like image 614
Tectrendz Avatar asked Sep 05 '12 16:09

Tectrendz


People also ask

How do you check backtrace?

To print a backtrace of the entire stack, use the backtrace command, or its alias bt . This command will print one line per frame for frames in the stack. By default, all stack frames are printed. You can stop the backtrace at any time by typing the system interrupt character, normally Ctrl-c .

How do you read a GDB backtrace?

To display the backtrace for several or all of the threads, use the command thread apply (see thread apply). For example, if you type thread apply all backtrace , gdb will display the backtrace for all the threads; this is handy when you debug a core dump of a multi-threaded program.

What does backtrace mean in GDB?

A backtrace is a summary of how your program got where it is. It shows one line per frame, for many frames, starting with the currently executing frame (frame zero), followed by its caller (frame one), and on up the stack.


1 Answers

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.

like image 50
Tom Tromey Avatar answered Sep 20 '22 17:09

Tom Tromey