The run and quit commands
What you'll learn: How to start your program inside the debugger and how to safely exit the GDB environment.
Stepping into the Cockpit
Think of GDB as a flight simulator for your code. When you open your program with GDB, you aren’t actually running the code yet; you are just sitting in the cockpit, looking at the controls. To make the "engines" start and the code actually execute, you need to tell GDB to begin.
The run command
Once you have started GDB in your terminal (usually by typing gdb ./my_program), you will see a prompt that looks like (gdb). To start execution, simply type:
run
You can also use the shorthand r. When you hit enter, your program will behave exactly as it does normally—it will ask for user input or print text to the screen—until one of three things happens:
- The program finishes naturally.
- The program crashes (a "Segfault").
- The program hits a "breakpoint" (a stop sign you’ve set).
If your program requires command-line arguments (like filenames or flags), you provide them directly after the command, like run input.txt.
The quit command
Once you are finished debugging, you need to step out of the simulator and return to your standard terminal. To do this, type:
quit
You can also use the shorthand q. If your program is still currently running or paused in the background, GDB will politely ask: "A debugging session is active. Quit anyway? (y or n)". Type y and hit enter to shut everything down.
A simple workflow
Imagine you have a basic program like this:
#include <stdio.h>
int main() {
printf("Hello, Debugger!\n");
return 0;
}
After compiling with the -g flag, your interaction would look like this:
- Start GDB:
gdb ./hello - Start the code:
run - View output:
Hello, Debugger! - Exit GDB:
quit
Key takeaway
Use run (or r) to start your program's execution and quit (or q) to exit the GDB environment.