I just ran on useful info for debugging code with <values optimized out> (for code generated by compiling with optimization, eg. with gcc -O3).

How to do it:

  • use TUI mode of GDB - either start gdb with gdbtui or type - (minus) and ENTER
  • enable split source / disassembly / command diplay: type C-x 2 (hold down Control and press X, then release both and press 2)
  • in the split mode - C-x o switches between the windows (like in Emacs)
  • using instructions nexti and stepi allows to execute one machine instruction

“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

Useful GDB commands:

  • info registers