Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why GDB jumps unpredictably between lines and prints variables as "<value optimized out>"?

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?

like image 673
Arpit Avatar asked Aug 28 '09 06:08

Arpit


People also ask

Why does GDB say optimized out?

It means you compiled with e.g. gcc -O3 and the gcc optimiser found that some of your variables were redundant in some way that allowed them to be optimised away.

How do I disable optimized in GDB?

Another option to see all <optimized out> variables in gdb is of course disabling gccoptimization altogether. Look for compilation flags (e.g., in CFLAGS) in your Makefile. You will find something like '-O1', '-O2' or '-O3', which defines various levels of gcc optimization. Remove this flag, or change it to '-O0'.


1 Answers

To debug optimized code, learn assembly/machine language.

Use the GDB TUI mode. My copy of GDB enables it when I type the minus and Enter. Then type C-x 2 (that is hold down Control and press X, release both and then press 2). That will put it into split source and disassembly display. Then use stepi and nexti to move one machine instruction at a time. Use C-x o to switch between the TUI windows.

Download a PDF about your CPU's machine language and the function calling conventions. You will quickly learn to recognize what is being done with function arguments and return values.

You can display the value of a register by using a GDB command like p $eax

like image 54
Zan Lynx Avatar answered Oct 24 '22 08:10

Zan Lynx