How does GDB evaluate C++ expressions at runtime
which allows me to type things like print v.at(4);
gdb can call functions compiled into the binary. This is exactly what happens here. gdb calls std::vector
member function at()
and prints the result for you, see documentation.
Also note that this is possible because you are using v.at(0)
in your code. If you remove this part of code, v.at()
would not get instantiated and will not be available in the resulting binary so that gdb could not call it.
Short Answer: It does not compile code.
Long Answer:
- You call
print
command and the procedure occurs inprintcmd.c
- It calls
evaluate_expression
, defined ineval.c
, which evaluates an expression by reading target memory and calculating it inside gdb for standard operators, otherwise usecall_function_by_hand
. call_function_by_hand
is defined ininfcall.c
. When called, the procedure halts target execution (sometimes doesn't, so it is possible to crash a multithreaded program with this feature).- Inject code into the program being debug.
- Retrieve the result by reading memory and unpause it if necessary.
You may focus on the code of call_function_by_hand
for better understanding.
Note: compile
is a different thing from print
/call
.