Skip to content

Latest commit

 

History

History
47 lines (46 loc) · 848 Bytes

gdb_related.md

File metadata and controls

47 lines (46 loc) · 848 Bytes

How to avoid optimized out at variable evaluation

  • Sample cpp code
#include <iostream>
#include <cmath>
int main()
{
    int x=1;
    std::cin >> x;
    int y=0;
    if (x==1 || x==2)
    {
	y = 3;
    } else
    {
	y = 4;
    }
    x = 2;
    int z = 3;
    if (x==1 || x==2)
    {
	z = 13;
    } else
    {
	z = 14;
    }
     std::cout << z << std::endl;
    return 0;
}
  • By the end of the code, -O3 -g will yield optimized out for variable y
gdb-peda$ p y
$1 = <optimized out>
  • -O1 yields same results
  • Applying -O0 will show the value of y but in -O3 state, we can use volatile int y=0;
    • This yields:
gdb-peda$ p y
$1 = 0x4

when std::bad_cast exception

  • bt may not work. Need to stop throw
  • catch throw then run the execuble within gdb, to stay in the stack memory
  • bt will work now