Getting Started with GDB
Learn to execute the code using the GNU Debugger.
In this lesson, we’ll learn to execute our code step by step using a debugger. This way, we’ll be able to see the output each line produces and how different variables change during the life of a program.
Motivation
Once we learn to use the debugger, we will be able to understand the code by setting breakpoints and analyzing the values of different variables (state of the program).
Our main focus at this point is to familiarize ourselves with the debugger tool. We’ll look at the animations and then practice each step in the debugger tool given below.
Later in the course, we’ll focus on ways to make the most of the debugger tool by learning how to look for logical (semantic and runtime) errors. This way, we’ll come to appreciate the advantages and uses of the debugger even more.
Let’s dive into our
Executing code in one go
Before we learn about line-by-line code execution, let’s see how we can execute the code directly.
To execute a program in one go, we will follow the steps shown below:
To execute the code directly, we first need to remove the default breakpoint and then click the start ( ) button.
Executing code step-by-step
Let’s revisit the table printing program and see how every instruction of the program executes.
Look at the following animation to understand how to execute the program in step-by-step mode.
The illustration above shows how we can execute our code line by line.
A walkthrough of the debugger
Let’s walk through the steps one by one.
Once we click “Run”, our source code is loaded into the top left section. This is where we can see the table printing source code in the illustration above.
On line 5, we can see a breakpoint, which is always marked by default. To remove or add a breakpoint, we can click the respective line number.
We start the execution by clicking the start/restart ( ) button, followed by the next ( ) button. To view what each line does, we can keep clicking the next button and its respective output will be visible at the bottom (as shown in the animation above).
Run the code following the same steps in the editor below. First, execute the code directly, and then execute it step by step.
Note: To execute the code again, click the start/restart ( ) button. Alternatively, click the “Load Binary” button on the top-left section of the debugger.
Change a variable’s state during execution
You can viewslide 16 to see how we changed the Table
variable by adding an assignment statement, Table=3
, resulting in the remaining instruction to be printed with Table=3
. This means that during the execution of the program, we can change the variable value with whatever value we would like to have for testing purposes.
#include<iostream> using namespace std; int main() { int Table = 73; cout << "Table: "; cin>>Table; cout << Table << " x 1 = " << Table*1 << endl; cout << Table << " x 2 = " << Table*2 << endl; cout << Table << " x 3 = " << Table*3 << endl; cout << Table << " x 4 = " << Table*4 << endl; cout << Table << " x 5 = " << Table*5 << endl; cout << Table << " x 6 = " << Table*6 << endl; cout << Table << " x 7 = " << Table*7 << endl; cout << Table << " x 8 = " << Table*8 << endl; cout << Table << " x 9 = " << Table*9 << endl; cout << Table << " x 10 = " << Table*10<< endl; return 0; }
If you are on the last
return 0;
line and you press the next ( ) button, the debugger will show the assembly code. Please ignore it. To view or execute the code again, click the “Load Binary” button on the top-left section of the debugger.
Executing till the breakpoint
To execute the code directly till a certain point, we can do the following:
- Click the start/restart ( ) button
- Add the breakpoint on whichever line(s) you want
- Click the continue ( ) button and it will execute all lines till the breakpoint
Look at the animation below.
Try running the code by adding a breakpoint or several breakpoints in the code editor given above.