打印C中所有局部变量的地址 [英] Print addresses of all local variables in C

查看:382
本文介绍了打印C中所有局部变量的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在程序的不同执行点打印函数中使用的所有本地和全局变量的地址,并将其存储在文件中。



/ p>

info local命令打印所有局部变量的值。我需要一些类似的方式打印地址。有没有内置命令?



编辑1



我正在使用一个gcc插件,它生成在编译时一个点对图。



我想验证生成的图是否正确,即如果指针实际上指向了插件告诉的变量他们应该指向。



我们想通过数千行代码验证这个关于大型程序的信息。我们将使用程序验证此信息,而不是手动验证。每个函数中都有几个本地和全局变量,因此在每一行代码之后添加printf语句是不可能的。

解决方案

没有内置的命令来做到这一点。在gdb bugzilla中有一个开放的功能要求来显示当前堆栈框架中所有已知插槽的含义,但没有人实现这一点。



这可以用一点gdb脚本来完成。最简单的方法是使用Python遍历所选的框架。然后在每个这样的中,您可以迭代所有变量,并在变量上调用 info addr p>

请注意,使用 print& var 打印地址并不总是奏效。一个变量并不总是有一个地址 - 但是,如果该变量存在,它将有一个位置,这就是 info addr 将显示的位置。



这些想法可以不同的一个简单方法是编译器决定将变量放入寄存器。还有一些比较复杂的例子,比如说编译器可以把变量放在不同点的函数中,或者可以将本地 struct 分割成其组成部分并移动它们。



默认情况下 info addr 尝试打印一些模糊的人类可读的东西。如果您需要这个详细程度,您也可以要求它转储DWARF位置表达式。


I want to print the addresses of all the local and global variables which are being used in a function, at different points of execution of a program and store them in a file.

I am trying to use gdb for this same.

The "info local" command prints the values of all local variables. I need something to print the addresses in a similar way. Is there any built in command for it?

Edit 1

I am working on a gcc plugin which generates a points-to graph at compile time.

I want to verify if the graph generated is correct, i.e. if the pointers do actually point to the variables, which the plugin tells they should be pointing to.

We want to validate this points-to information on large programs with over thousands of lines of code. We will be validating this information using a program and not manually. There are several local and global variables in each function, therefore adding printf statements after every line of code is not possible.

解决方案

There is no built-in command to do this. There is an open feature request in gdb bugzilla to have a way to show the meaning of all the known slots in the current stack frame, but nobody has ever implemented this.

This can be done with a bit of gdb scripting. The simplest way is to use Python to iterate over the Blocks of the selected Frame. Then in each such Block, you can iterate over all the variables, and invoke info addr on the variable.

Note that printing the address with print &var will not always work. A variable does not always have an address -- but, if the variable exists, it will have a location, which is what info addr will show.

One simple way these ideas can differ is if the compiler decides to put the variable into a register. There are more complicated cases as well, though, for example the compiler can put the variable into different spots at different points in the function; or can split a local struct into its constituent parts and move them around.

By default info addr tries to print something vaguely human-readable. You can also ask it to just dump the DWARF location expressions if you need that level of detail.

这篇关于打印C中所有局部变量的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆