我有一个应用程序。我有源代码(C)。无论如何,我可以编译它。添加我想要的任何工具。等等。但是,我不想用一堆printf来哄骗源代码。我希望能够生成某种类型的日志,以显示特定值(例如全局结构的某些成员)是否写入(其值发生更改)。我希望能够显示源文件和行号,以及新旧值。帮助gdb跟踪(或类似)
我也想能够通过名称而不是地址来指定值。但地址确定。如果我可以指定一个函数本地的值,则为奖励点。
我还在摸索着想弄清楚gdb的跟踪命令。任何帮助是极大的赞赏。谢谢。
我有一个应用程序。我有源代码(C)。无论如何,我可以编译它。添加我想要的任何工具。等等。但是,我不想用一堆printf来哄骗源代码。我希望能够生成某种类型的日志,以显示特定值(例如全局结构的某些成员)是否写入(其值发生更改)。我希望能够显示源文件和行号,以及新旧值。帮助gdb跟踪(或类似)
我也想能够通过名称而不是地址来指定值。但地址确定。如果我可以指定一个函数本地的值,则为奖励点。
我还在摸索着想弄清楚gdb的跟踪命令。任何帮助是极大的赞赏。谢谢。
谢谢@derobert和@peter!我终于回到了这个,这个:
break main
commands
watch somevar
commands
cont
end
cont
end
run
有窍门。这适用于“somevar”是全局的,或本地的“main”。如果“somevar”是另一个函数的本地代码,只需用上面的函数名替换“main”。
在文件中把这些命令(例如“gdbscript”),并运行GDB,如:
gdb -x gdbscript a.out
首先,您需要确保使用调试符号编译您的程序,并且可能不进行优化以使gdb最有用。对于gcc,这将是-g -O0
。
其次,您要查找的功能不是跟踪,它的观察点。
(gdb) help watch
Set a watchpoint for an expression.
A watchpoint stops execution of your program whenever the value of
an expression changes.
所以,对于一些示例代码:
int main() {
int a;
a = 1;
a = 2;
return 0;
}
那么你就可以在其上运行GDB,并且:
(gdb) b main
Breakpoint 1 at 0x80483a5: file test.c, line 4.
(gdb) run
Starting program: /tmp/test
Breakpoint 1, main() at test.c:4
4 a = 1;
(gdb) watch a
Hardware watchpoint 2: a
(gdb) c
Continuing.
Hardware watchpoint 2: a
Old value = -1207552288
New value = 2
main() at test.c:8
8 return 0;
它的工作稍微好笑由于是在栈上,不是记忆。如果优化开始,它会更少工作:a会被优化。
如前所述,您需要为变量设置一个观察点。
的使用“命令”命令
(gdb) help commands
Set commands to be executed when a breakpoint is hit.
Give breakpoint number as argument after "commands".
With no argument, the targeted breakpoint is the last one set.
The commands themselves follow starting on the next line.
Type a line containing "end" to indicate the end of them.
Give "silent" as the first line to make the breakpoint silent;
then no output is printed when it is hit, except what the commands print.
所以,找到watch命令观察点数量,并做到这一点(假设你的手表是在第二个破发)
(gdp) commands 2
> print a
> cont
> end
假设a是你想要的变量。如果您对gdb给出的输出感到满意,您可以省略打印行。
您还可以使用原始断点中的命令来设置观察点并继续。
尝试宣告了'''如挥发性int',这可能使例子更好地工作。 – caf 2009-08-26 00:29:30
谢谢德罗伯特。根据我对手表的了解,他们导致程序执行停止,直到用户“继续”。我敢肯定,我可以写一个期望脚本来做到这一点,但有没有办法从gdb内部自动完成此操作? – tvaughan 2009-08-26 18:16:44
@tvaughan:我不知道在gdb中使用自动方法。 – derobert 2009-08-27 04:28:15