Getting Started With GDB!
Tags: tutorial, codenewbie, computerscience
Published: 2020-06-08 05:59:39

> This is the Part 2 of [Debugging Series](https://medium.com/@er.sushant.2/what-is-debugging-how-to-do-it-162f5e31ecc1)

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](https://media.giphy.com/media/l1Ku7AZCOifXfL0pq/giphy.gif "By Giphy")


In my [first part](https://medium.com/@er.sushant.2/what-is-debugging-how-to-do-it-162f5e31ecc1), 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!

```javascript
#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](https://dev-to-uploads.s3.amazonaws.com/i/yun5bd71s214edk8zfic.PNG)

**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](https://dev-to-uploads.s3.amazonaws.com/i/zs68imidkdvwjpem2wl9.PNG)

**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](https://dev-to-uploads.s3.amazonaws.com/i/ho2857ohz0rquhxijwkf.PNG)

**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](https://dev-to-uploads.s3.amazonaws.com/i/ah9x0x477yornpwgyy12.PNG)

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

![quit](https://dev-to-uploads.s3.amazonaws.com/i/wsim4i36oas0m109p5if.PNG)

___

```
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_**