0Day Forums
Why GDB jumps unpredictably between lines and prints variables as "<value optimized out>"? - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: C# (https://0day.red/Forum-C)
+--- Thread: Why GDB jumps unpredictably between lines and prints variables as "<value optimized out>"? (/Thread-Why-GDB-jumps-unpredictably-between-lines-and-prints-variables-as-quot-lt-value-optimized-out-gt-quot)



Why GDB jumps unpredictably between lines and prints variables as "<value optimized out>"? - hymie884 - 07-24-2023

Can anyone explain this behavior of gdb?

900 memset(&new_ckpt_info,'\0',sizeof(CKPT_INFO));
(gdb)
**903 prev_offset = cp_node->offset;**
(gdb)
**905 m_CPND_CKPTINFO_READ(ckpt_info,(char *)cb->shm_addr.ckpt_addr+sizeof(CKPT_** HDR),i_offset);
(gdb)
**903 prev_offset = cp_node->offset;**
(gdb)
**905 m_CPND_CKPTINFO_READ(ckpt_info,(char *)cb->shm_addr.ckpt_addr+sizeof(CKPT_ HDR),i_offset);**
(gdb)
**908 bitmap_offset = client_hdl/32;**
(gdb)
**910 bitmap_value = cpnd_client_bitmap_set(client_hdl%32);**
(gdb)
**908 bitmap_offset = client_hdl/32;**
(gdb)
**910 bitmap_value = cpnd_client_bitmap_set(client_hdl%32);**
(gdb)
**908 bitmap_offset = client_hdl/32;**
(gdb)
**910 bitmap_value = cpnd_client_bitmap_set(client_hdl%32);**
(gdb)
913 found = cpnd_find_exact_ckptinfo(cb , &ckpt_info , bitmap_offset , &offset , &prev_offset);
(gdb)
916 if(!found)
(gdb) p found
$1 = <value optimized out>
(gdb) set found=0
Left operand of assignment is not an lvalue.

Why after executing line 903 it again executes the same for 905 908 910?

Another things is `found` is a `bool`-type variable, so why it is showing `value optimized out`?
I am not able to set the value of `found` as well.

This seems to be a compiler optimization (in this case its `-O2`); how can I still set the value of `found`?






RE: Why GDB jumps unpredictably between lines and prints variables as "<value optimized out>"? - yttria113 - 07-24-2023

The compiler will start doing very clever things with optimisations turned on. The debugger will show the code jumping forward and backwards alot due to the optimized way variables are stored in registers. This is probably the reason why you can't set your variable (or in some cases see its value) as it has been cleverly distributed between registers for speed, rather than having a direct memory location that the debugger can access.

Compile without optimisations?


RE: Why GDB jumps unpredictably between lines and prints variables as "<value optimized out>"? - sukeyxzxg - 07-24-2023

You pretty much can't set the value of found. Debugging optimized programs is rarely worth the trouble, the compiler can rearrange the code in ways that it'll in no way correspond to the source code (other than producing the same result), thus confusing debuggers to no end.



RE: Why GDB jumps unpredictably between lines and prints variables as "<value optimized out>"? - americanism726 - 07-24-2023

Declare **found** as "volatile". This should tell the compiler to NOT optimize it out.

volatile int found = 0;


RE: Why GDB jumps unpredictably between lines and prints variables as "<value optimized out>"? - basheercqsikqfji - 07-24-2023

Typically, boolean values that are used in branches immediately after they're calculated like this are never actually stored in variables. Instead, the compiler just branches directly off the [condition codes][1] that were set from the preceding comparison. For example,

int a = SomeFunction();
bool result = --a >= 0; // use subtraction as example computation
if ( result )
{
foo();
}
else
{
bar();
}
return;

Usually compiles to something like:

call .SomeFunction ; calls to SomeFunction(), which stores its return value in eax
sub eax, 1 ; subtract 1 from eax and store in eax, set S (sign) flag if result is negative
jl ELSEBLOCK ; GOTO label "ELSEBLOCK" if S flag is set
call .foo ; this is the "if" black, call foo()
j FINISH ; GOTO FINISH; skip over the "else" block
ELSEBLOCK: ; label this location to the assembler
call .bar
FINISH: ; both paths end up here
ret ; return

Notice how the "bool" is never actually stored anywhere.



[1]:

[To see links please register here]




RE: Why GDB jumps unpredictably between lines and prints variables as "<value optimized out>"? - Sircarlynmrus - 07-24-2023

When debugging optimized programs (which may be necessary if the bug doesn't show up in debug builds), you often have to understand assembly compiler generated.

In your particular case, return value of `cpnd_find_exact_ckptinfo` will be stored in the register which is used on your platform for return values. On `ix86`, that would be `%eax`. On `x86_64`: `%rax`, etc. You may need to google for '[your processor] procedure calling convention' if it's none of the above.

You can examine that register in `GDB` and you can set it. E.g. on `ix86`:

(gdb) p $eax
(gdb) set $eax = 0