Moving between frames with up and down
What you'll learn: How to navigate the function call stack to inspect variables in parent functions during a debugging session.
The stack is a ladder
When your program is running and you hit a breakpoint, GDB stops inside the current function. But how did you get there? Programs work like a stack of cafeteria trays: every time a function calls another, a new "frame" is placed on top. The frame contains that function's local variables.
By default, GDB shows you the very top tray (the function currently executing). However, the bug you are hunting might be caused by a bad value passed from a function further down the stack. To see those variables, you need to move your perspective.
Climbing with up and down
In GDB, the "top" of the stack (the current function) is Level 0. The function that called it is Level 1, and so on.
up: Moves your focus one level higher in the call stack (towardmain). Think of this as moving "up" the chain of command to the caller.down: Moves your focus back toward the current function (toward the crash or breakpoint).
#include <stdio.h>
void process_data(int value) {
// GDB stops here!
printf("Processing: %d\n", value);
}
void initialize() {
int secret_key = 42;
process_data(secret_key);
}
int main() {
initialize();
return 0;
}
Seeing the invisible
If you are paused inside process_data, typing print secret_key will result in an error because that variable doesn't exist in the current frame.
If you type up, GDB will inform you that you are now in initialize. Now, if you type print secret_key, GDB will show you the value 42. You haven't restarted the program or moved the execution pointer; you have simply shifted your "eyes" to a different frame on the stack.
You can move multiple steps by typing up 2 or go back to the start by typing down. This is essential for tracing how a NULL pointer or a weird integer was passed through several layers of your code before finally causing a crash.
Key takeaway
Use up to inspect the variables of the function that called your current position, and down to return toward the active line of code.