简体   繁体   English

GDB:我怎样才能获得所有局部变量的名称?

[英]GDB : how can I get the name of all local variables?

Is there a way in gdb to get the name of all local variables ? 有没有办法在gdb中获取所有局部变量的名称? I know that 我知道

info local 信息本地

prints names and values of local variables, but I would like to automatically compare the value of all the locals at several points of execution of a function (and there is a huge number of local variables in this function, so doing it manually one by one would be tedious ...) 打印局部变量的名称和值,但我想在函数的多个执行点自动比较所有本地的值(并且在这个函数中有大量的局部变量,所以一个接一个地手动执行会很乏味...)

Thanks ! 谢谢 !

It sounds like you really want a way to compare some locals at two points in time. 听起来你真的想要一种方法来比较一些当地人在两个时间点。 There are a couple of ways to do this. 有几种方法可以做到这一点。

One low-tech way is to dump the variables with info locals . 一种低技术方式是使用info locals转储变量。 You can dump them to a file using the set logging facility, and you can completely automate this by setting a breakpoint whose commands do the logging, the dumping, disable the logging, and finally end with continue . 您可以使用set logging工具将它们转储到文件中,并且可以通过设置断点来完全自动执行此操作,该断点的commands执行日志记录,转储,禁用日志记录,最后以continue结束。

In this approach you would write a separate script to compare the results. 在这种方法中,您将编写一个单独的脚本来比较结果。

Another approach is to use gdb's Python scripting facility. 另一种方法是使用gdb的Python脚本工具。 It's easy to get the local variables here: get the selected frame with gdb.selected_frame() ; 在这里获取局部变量很容易:使用gdb.selected_frame()获取所选框架; then get the frame's block ( frame.block() ); 然后得到帧的块( frame.block() ); then iterate over the locals of the block to get that block's variables, and walk up the blocks (see gdb.Block.superblock ) until you reach the function boundary. 然后遍历块的gdb.Block.superblock区域以获取该块的变量,并向上走动块(请参阅gdb.Block.superblock ),直到到达函数边界。

You can either evaluate or ignore symbols you find (if, say, you only want to compare some subset). 您可以评估或忽略您找到的符号(例如,您只想比较某些子集)。 And, you can decide how you want to compare the values you collect. 并且,您可以决定如何比较收集的值。

In this approach all the work is done in gdb, without any separate comparison script. 在这种方法中,所有工作都在gdb中完成,没有任何单独的比较脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM