DEV Community

Cover image for Getting Started With GDB!
Sushant Gupta
Sushant Gupta

Posted on

Getting Started With GDB!

This is the Part 2 of Debugging Series

If you don't know what is GDB, i highly recommend to check the part 1 of this series!

Nowadays, i am learning & practicing coding questions of Data Structures & Algorithms over various coding platforms such as CodeChef, Hackerearth etc.


Most of the time when i stuck in some question or if i am not able to get the problem statement properly!
What i usually do is firstly i give time to the problem & if found that i missing somewhere due to which i'm not able to solve the problem!
In that case i refer to the problem's editorial section in which author has curated the explanation well & the prefix solution hint!


What i faced sometimes, that after seeing the solution if i am not to able to understand the code, what that code does & how code is executing?

In this case i debug the code, which plays a vital role!


So, Let's Get Started!

Let's Start

In my first part, I've explained what is debugging? & how to install GDB debugger! Go check it out if you haven't!

I follow some certain commands while debugging in almost every code!

Let's take a simple code for finding sum of first n natural numbers!

#include<iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    int i, sum = 0;
    for(i = 1; i <= n; i++){
        sum += i;
    }
    cout<<sum<<"\n";
    return 0;
}

GDB offers a big list of commands, however the following commands are the ones used most frequently:

b main - Puts the breakpoint at the beginning of the program i.e., from main() , we can set breakpoint anywhere in the program!

breakpoint

r - Runs the program until a breakpoint or error
After setting breakpoint, i usually start the program for execution by writing r , it starts from the next line after the main()

run

s - Runs the next line of program or if u want to go inside any function then just hit s !

n - Like s, u can use this command also, but it does not step into functions!

I use n most of the time instead of s ,here i've taken n = 20

next

p var - Prints the current value of the variable "var", here variable are sum, so write p sum to check the final sum = 210 !

print

q - Quits GDB, after finishing the execution, quit the GDB Debugger by hitting y !

quit


Now, U Can Debug Any Program!
So, Lets Start Debugging Codes!

Here, are some more commands, in case u stuck somewhere!

b - Puts a breakpoint at the current line
b N - Puts a breakpoint at line N
b +N - Puts a breakpoint N lines down from the current line
b fn - Puts a breakpoint at the beginning of function "fn"
d N - Deletes breakpoint number N
info break - list breakpoints
c - Continues running the program until the next breakpoint or error
f - Runs until the current function is finished
s N - Runs the next N lines of the program
u N - Runs until you get N lines in front of the current line
bt - Prints a stack trace
u - Goes up a level in the stack
d - Goes down a level in the stack


Thanks for giving this a read! XD

HAPPY CODING

Top comments (0)