Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use gdb to explore the stack/heap?

Tags:

Could anyone please give me a quick overview/point me to documentation of a way to inspect the stack (and heap?) of a C program? I thought this should be done with GDB, but if there are other more straighforward alternatives, then that should be fine as well.

Thanks.

like image 700
Dervin Thunk Avatar asked Oct 06 '10 17:10

Dervin Thunk


People also ask

What is stack in GDB?

All the stack frames are allocated in a region of memory called the call stack . When your program stops, the GDB commands for examining the stack allow you to see all of this information. One of the stack frames is selected by GDB and many GDB commands refer implicitly to the selected frame.

How do I see all threads in GDB?

Use the "info threads" command to see the IDs of currently known threads. The GDB thread debugging facility allows you to observe all threads while your program runs--but whenever GDB takes control, one thread in particular is always the focus of debugging. This thread is called the current thread.


2 Answers

you can dump raw memory with the 'x' command

so if you want to look at bits of the stack or heap try things like

x/10b &stackvar
x/200b &heapvar-20

The last one will show you 200 bytes starting from 20 bytes before heapvar. So if you just malloced that you can see the heap header too

like image 151
pm100 Avatar answered Sep 20 '22 15:09

pm100


View stack: gdb> backtrace

View current stack frame: gdb> info frame

View arguments of current stack frame: gdb> info args

View local variable of current stack frame: gdb> info locals

Navigate to parent stack frame: gdb> frame 1

Examining the Stack

like image 36
Mahler Avatar answered Sep 20 '22 15:09

Mahler