C语言 如何使用 gdb 来探索堆栈/堆?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3875055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:37:33  来源:igfitidea点击:

how to use gdb to explore the stack/heap?

cdebugginggdbstackheap

提问by Dervin Thunk

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.

任何人都可以给我一个快速概述/指向我检查 C 程序的堆栈(和堆?)的方法的文档吗?我认为这应该用 GDB 来完成,但如果有其他更直接的替代方案,那也应该没问题。

Thanks.

谢谢。

采纳答案by Srikar Appalaraju

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

我使用 GDB 进行调试的第一种方法是设置断点。这是这样做的:

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?

现在您的程序在程序的第 123 行停止。现在您可以使用print. 对于堆栈变量,只需使用print <varname>. 对于堆变量(指针)使用print <*varname>. 不确定检查堆栈/堆变量有什么特别的吗?

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.

当然,要调试多线程应用程序,您需要使其在单线程模式下运行,然后进行 dubug 否则很难预测正在发生的事情。

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

对于其他任何东西,都有大量的gdb文档,许多站点也提供了gdb 备忘单

回答by pm100

you can dump raw memory with the 'x' command

您可以使用“x”命令转储原始内存

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

最后一个将显示从 heapvar 之前的 20 个字节开始的 200 个字节。所以如果你只是 malloced 你也可以看到堆头

回答by Mahler

View stack: gdb> backtrace

查看堆栈: gdb> backtrace

View current stack frame: gdb> info frame

查看当前堆栈帧: gdb> info frame

View arguments of current stack frame: gdb> info args

查看当前堆栈帧的参数: gdb> info args

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

查看当前栈帧的局部变量: gdb> info locals

Navigate to parent stack frame: gdb> frame 1

导航到父堆栈框架: gdb> frame 1

Examining the Stack

检查堆栈

回答by Casual Coder

Try using ddd. ddd manual

尝试使用ddd. ddd手册

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

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

在gui中你可以做各种事情,查看机器码,查看内存等等。环视四周。在手册中还有一段检查堆栈。ddd提供良好的接口供您检查 C 程序。