GDB未使用的变量
是否可以使用GDB获取未使用变量的值? GCC 是否有一些配置,以便未使用变量的垃圾值不会显示为“优化”?
c 文件:
#include<stdio.h>
void main()
{
int x;
int y;
printf("value of x: %d",x);
}
在 gdb 中我想获取变量 y 的垃圾值。
(gdb) run
Starting program: /home/charmae/workspace/AVT/a.out
Breakpoint 1, main () at file4.c:7
7 printf("value of x: %d",x);
(gdb) info locals
x = 2789364
(gdb) p y
$1 = <optimized out>
(gdb) p x
$2 = 2789364
Is it possible to get the value of unused variable using GDB? Is there some configuration for GCC so that the garbage value of the unused variable will be shown not 'optimized out'?
c file:
#include<stdio.h>
void main()
{
int x;
int y;
printf("value of x: %d",x);
}
In the gdb I want to get the garbage value of variable y.
(gdb) run
Starting program: /home/charmae/workspace/AVT/a.out
Breakpoint 1, main () at file4.c:7
7 printf("value of x: %d",x);
(gdb) info locals
x = 2789364
(gdb) p y
$1 = <optimized out>
(gdb) p x
$2 = 2789364
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与GDB无关。优化该变量的实体是编译器(在您的情况下可能是 GCC)。您可以通过将变量声明为易失性来强制它保留它
更好的问题是 - 为什么您要尝试这样做?
It has nothing to do with GDB. The entity that optimized that variable out is the compiler (probably GCC in your case). You might force it to keep it by declaring the variable as volatile
A better question is - why are you trying to do?
与gcc无关。编译器要么已编译代码来维护该值,要么没有。
It's nothing to do with gcc. Either the compiler has compiled code to maintain the value, or it hasn't.
您可以添加
y=y;
语句。这将强制使用y
,并使用gcc -O0 -g
跟踪它(至少在我的 Linux/Debian/Sid/AMD64 上使用gcc 4.6.2
和gdb 7.3.50
)You might add an
y=y;
statement. That would forcey
to be used, and withgcc -O0 -g
keep track of it (at least on my Linux/Debian/Sid/AMD64 withgcc 4.6.2
andgdb 7.3.50
)