how to use gdb to explore the stack/heap?
Asked Answered
R

5

23

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.

Rogue answered 6/10, 2010 at 17:3 Comment(2)
What operating system do you use?Counterpane
Ah, sorry, yes: OS: Ubuntu Linux, Compiler: GCC.Rogue
H
3

My first approach to using GDB for debugging is to setup breakpoints. This is done like so:

prompt> gdb ./x_bstree.c
(gdb) #prompt
(gdb) b 123 #break at line 123
(gdb) r #start program

Now your program halts at line 123 of your program. Now you can examine variables in stack or heap using print. For stack variables just use print <varname>. For heap variables (pointers) use print <*varname>. Not sure there is anything special to do for examining stack/heap variables?

Of course to debug multi-threaded applications you would need to make it run in single-threaded mode & then dubug Otherwise it becomes difficult to predict what's happening.

For anything else there is extensive documentation of gdb & many sites also provide gdb cheat sheets.

Hokanson answered 6/10, 2010 at 17:41 Comment(0)
H
20

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

Heiduc answered 6/10, 2010 at 17:56 Comment(1)
I like your approach! Is there any good gdb plugin that allows to examine the heap showing it's chunk data, headers, in-use bits and the like? I just found gdb-heap. Unfortunately it seems pretty outdated and unmaintained.Punak
D
6

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

Dalesman answered 9/2, 2018 at 8:13 Comment(1)
None of that displays raw memory content.Finalism
H
3

My first approach to using GDB for debugging is to setup breakpoints. This is done like so:

prompt> gdb ./x_bstree.c
(gdb) #prompt
(gdb) b 123 #break at line 123
(gdb) r #start program

Now your program halts at line 123 of your program. Now you can examine variables in stack or heap using print. For stack variables just use print <varname>. For heap variables (pointers) use print <*varname>. Not sure there is anything special to do for examining stack/heap variables?

Of course to debug multi-threaded applications you would need to make it run in single-threaded mode & then dubug Otherwise it becomes difficult to predict what's happening.

For anything else there is extensive documentation of gdb & many sites also provide gdb cheat sheets.

Hokanson answered 6/10, 2010 at 17:41 Comment(0)
B
2

Try using ddd. ddd manual

Ok. Maybe I elaborate a little. I use it like this.

compile my program with debug symbols:

gcc -g program.c -o program

run ddd:

ddd program

In gui you can do all sorts of things, view machine code, view memory, etc. . Look around. In manual there is also a section of examining stack. ddd provides good interface for you to examine C program.

Basilius answered 6/10, 2010 at 17:42 Comment(3)
i thought ddd was just a front end for gdb, no?Rogue
I've found it more convenient than gdb. In gui you can look at several things at once. In gdb I am scrolling back all the time.Basilius
It IS just a front end for gdb, see updated manual location. It can, however, be a lot more straightforward for certain types of workflow than a console-only tool.Vladamar
H
0

core_analyzer has gdb plugin and stand alone tool.
Latest version is only gdb plugin.

gdb plugin command shows memory statistic.

(gdb)
heap  [/verbose or /v]  [/leak or /l]
heap  [/block or /b]  [/cluster or /c]  <addr_exp>
heap  [/usage or /u]  <var_exp>
heap  [/topblock or /tb]  [/topuser or /tu]  <num>

stand alone tool has horizontal search and vertical search and it might be near what you expect.

[0] Print General Core Information
[1] Find References to an Object (horizontal search)
[2] What Is This Address and Underlying Object Type (vertical search)
[3] Objects Shared Between Threads
[4] Memory Pattern Analysis
[5] Query Heap Memory Block
[6] Page Walk (check the integrity of surrounding memory blocks)
[7] Heap Walk (check the whole heap for corruption and memory usage)
[8] Biggest heap memory blocks
[9] Biggest Heap Memory Owners (variables)
[10] Heap Memory Leak Candidates
[11] Quit

In my case, I could not get exprected result from core_analyzer.

Harvey answered 1/4, 2022 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.