Unused gdb variable

Is it possible to get the value of an unused variable using gdb? Is there any configuration for GCC so that the garbage value for an unused variable is not displayed "optimized"?

c file:

#include<stdio.h> void main() { int x; int y; printf("value of x: %d",x); } 

In gdb, I want to get the garbage value of the 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) py $1 = <optimized out> (gdb) px $2 = 2789364 
+6
source share
3 answers

This has nothing to do with GDB. The object that optimized this variable is the compiler (maybe GCC in your case). You can force it to save it by declaring the variable as mutable

Best question: why are you trying to do?

+1
source

This is not related to gcc. Either the compiler has compiled code to maintain the value, or it does not.

0
source

You can add the operator y=y; . This would force the use of y and gcc -O0 -g track it (at least on my Linux / Debian / Sid / AMD64 with gcc 4.6.2 and gdb 7.3.50 )

0
source

Source: https://habr.com/ru/post/901965/


All Articles