how to use gdb to explore the stack/heap?

后端 未结 4 1210
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-04 03:31

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 t

相关标签:
4条回答
  • 2021-02-04 03:54

    Try using ffffd. ffffd 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 ffffd:

    ffffd 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. ffffd provides good interface for you to examine C program.

    0 讨论(0)
  • 2021-02-04 03:58

    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

    0 讨论(0)
  • 2021-02-04 04:14

    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

    0 讨论(0)
  • 2021-02-04 04:15

    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.

    0 讨论(0)
提交回复
热议问题