GDB未使用的变量

发布于 2024-12-17 07:04:28 字数 478 浏览 3 评论 0原文

是否可以使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄荷港 2024-12-24 07:04:28

与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?

小苏打饼 2024-12-24 07:04:28

与gcc无关。编译器要么已编译代码来维护该值,要么没有。

It's nothing to do with gcc. Either the compiler has compiled code to maintain the value, or it hasn't.

飘过的浮云 2024-12-24 07:04:28

您可以添加 y=y; 语句。这将强制使用 y,并使用 gcc -O0 -g 跟踪它(至少在我的 Linux/Debian/Sid/AMD64 上使用 gcc 4.6.2gdb 7.3.50

You might add an y=y; statement. That would force y to be used, and with gcc -O0 -g keep track of it (at least on my Linux/Debian/Sid/AMD64 with gcc 4.6.2 and gdb 7.3.50)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文