C语言 如何监视GDB中的变量并在满足某些条件时对其进行记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3099537/
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
How to monitor variables in GDB and log it if it meets certain conditions?
提问by kp11
I would like to know if there is any way in which I can monitor a value of a variable until for example a counter reaches a value and then log the output of variable value during each counter value?
我想知道是否有任何方法可以监视变量的值,直到例如计数器达到某个值,然后在每个计数器值期间记录变量值的输出?
回答by ninjalj
Set a watch point on the counter:
在计数器上设置一个观察点:
(gdb) watch var
And make that watchpoint conditional:
并使该观察点有条件:
(gdb) cond <watchpoint_number> var>=value
If you want to log to a file:
如果要登录到文件:
(gdb) set logging file <filename>
(gdb) set logging on
By default gdb logs to gdb.txt
默认情况下,gdb 会记录到 gdb.txt
回答by unwind
You can use watchpointsto make gdb monitor the value of a variable, and break execution of the program when the value changes. Once execution is stopped, you can use gdb's command set to inspect and print the value. I'm not sure if you can script gdb to do this automatically each time it breaks.
您可以使用观察点让 gdb 监视变量的值,并在值更改时中断程序的执行。一旦执行停止,您可以使用 gdb 的命令集来检查和打印值。我不确定您是否可以编写 gdb 脚本以在每次中断时自动执行此操作。

