how to use gdb to explore the stack/heap?

Dervin Thunk picture Dervin Thunk · Oct 6, 2010 · Viewed 38.7k times · Source

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.

Answer

pm100 picture pm100 · Oct 6, 2010

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