Intro to GDB

The GNU Debugger is a power terminal debugger that can help you trouble shoot bugs in your code.
First here is a simple short C program that I’ll use in gdb. This code has an error which is the return value for the createABC function should not be commented out. This is wrong: // return (abc);
#include <stdio.h>
#define LEN 26 //NOL = Number of Letters
char * createABC ()
{
static char abc[LEN];
for (int i = 0; i < LEN; i++)
abc[i] = i + 65;
// return (abc);
}
int main()
{
char * abc;
printf("Creating ABC list \n");
abc = createABC();
for(int i = 0; i < LEN; i++)
printf("%c ", abc[i] ) ;
printf("\n");
}
First when using gdb we have to compile the program with debugging information. The compiler that I use is gcc which just requires the added -g flag to add the debugger information. To compile the program above I’ll use:
gcc -g abc.c -o abc
When starting gdb there is a couple of ways to run it. This program can be run by entering in gdb and the program name:
gdb abc
Other programs with more then one argument you might want to start it with:
gdb --args argument 1 argument 2 and so on
Once the program starts all you have to do is type run. As you can see below where I highlighted the word run. After that we can see that we get a Segmentation fault on line 25 of abc.c

At this point you can use the command backtrace to trace the function calls that lead to the segmentation fault. This example is unimpressive since it stopped on the line with the segmentation fault already. Here is an image of running backtrace.

At this point you have a couple of option on how to see the code in more detail. One option is to type tui enable which gives you the terminal user interface (tui). You can actually start with the tui by starting gdb with the -tui flag. Than of course you can turn it off with tui disable. Here is what it looks like after I type tui enable.

Another way to see the lines of code is with the command list 25 which is the command list and 25 is the line of code. Here is an image of when I use this command and you can see that the line 25 is the 6th line out of 10 lines. This way it’s in the middle and you can see a little above and below line 25. Note: I added the yellow highlights to make it easier to see.

Now we need to enter in breaks in the program to stop it at specific stops. There are a different ways to enter in a break such as break main or break 25 or break abc.c:25. The break 25 and break abc.c:25 will give us the same break since there is only one file that is being used, but if you are using more than one file you can use the break abc.c:25 style to say which file and line to break at.

Above you can see the command break main starts at line 16 since main actually starts at line 16. Once you have your break point in place you can see your break points by typing info break which you can see above.
We can disable or delete break points by simply typing disable or delete and the number associated with break point. Below I highlighted the area I disabled the first break point then deleted it.

A quick side note:
The command help or help all will list commands inside of gdb if you can remember the command. You can also use man gdb or info gdb in the terminal to get more information on how to use gdb.
Now when we type run the program will break at our break points which is line 24 and line 25 since the line 16 (main) has been deleted. Once the program stops the main three commands to use are step, next, and continue.
The commands step and next are similar, but step will give you more details where it will go into the #include files to show what a function is doing. For example the printf() function on line 20. When using step it will go into the #include <stdio.h> file to show what is happening. The next command will run the line of code without going into the function.
Note: Below you can see using step at line 20 the gdb program steps into the function that is included in the stdio.h file. Also notice the 2nd (gdb), which is highlighted in green, is blank afterwards. This is because instead of typing step or next each time you can just press enter with no command the the previous command will be repeated.

Remember that sometimes you will want to step into the function like on line 22 there is the function createABC(). When you use next it will just run the function, but if you use step it will step into the function. See below

Another cool trick if you like to have a clean screen is to use the command shell clear. This will clear your screen similar to clear in the terminal.
The command continue will continue to the next break point. And again we can see that we can press the enter key with no input and the continue command will be executed again. see below.

Finally we get to the print command. This command will print out the value for your variable. We can also use the print/x command to print out the hex value for the variable. We can see below that we cannot access the memory location for abc[0] when we try to print it out. This is where we start looking at where the value of abc[] comes from and look at our function createABC().

Here at the createABC() function we see that we have the return value commented out in error and we can fix the error. A great thing with gdb is after we correct the error in another screen and compile it again the new compile will be available in gdb without restarting the program.

Some other things to remember:
Notice that in the function createABC() there is a variable called abc[]. If you want to print that variable you need to use this command if you are not in scope. print createABC::abc[0]. If you stepped into that function and you’re currently in scope then you can use the normal print abc[0] to get the value.