GDB can do more than look. set var armor = 2 changes a variable in the paused program.
Changing variables and calling functions from the debugger
Level 11 of 13 · interactive GDB exercise · runs in your browser
Change a variable in a paused program and call one of its functions, to test an idea without editing or recompiling.
print damage(4) calls a function of the program, right now, and shows what it returns.
Left alone, this program prints health = 60, lives = 2. Make it print something better, without touching the code.
Give the player armor before the hit lands, and let the program finish with all 3 lives.
You work in a real GDB session on the program below. A breakpoint on main was set and the program was started; it is paused at the first line of main.
#include <stdio.h>
int lives = 3;
int damage(int armor) {
return 40 - armor * 10;
}
int main(void) {
int health = 100;
int armor = 0;
health -= damage(armor);
if (health < 70) {
lives--;
}
printf("health = %d, lives = %d\n", health, lives);
return 0;
}
main.c, compiled with gcc -g -O0 -std=c11 -Wall -fno-omit-frame-pointer -fno-stack-protector -no-pie -fno-pie for 32-bit x86 Linux.
set var <name> = <value>print <var> = <value>call <func>(<args>)return [value]nextprint <expr>continuerunarmor after line 12 has already run. Too late for this run: the damage is done. Type run to start over, or fix things up with set var health = 80.armor while still on line 11. That line has not run yet and will overwrite your value with 0. Step past it first.set armor = 2 and getting a confusing error for some names. Plain set also changes GDB's own settings, so names can clash. set var always means a program variable.