How to use gdb to debug C programs in Linux

Abstract: No matter how experienced a programmer is, it is impossible to develop any software without bugs. Therefore, troubleshooting and fixing bugs becomes one of the most important tasks in the software development cycle. There are many ways to troubleshoot bugs (testing, code self-review, etc.), but there are also specialized software (called debuggers) that can help pinpoint the problem so that it can be fixed.

No software can be developed without bugs, no matter how experienced a programmer is. Therefore, troubleshooting and fixing bugs becomes one of the most important tasks in the software development cycle. There are many ways to troubleshoot bugs (testing, code self-review, etc.), but there are also specialized software (called debuggers) that can help pinpoint the problem so that it can be fixed.

If you are a C/C++ programmer, or develop software using Fortran and Modula-2 programming languages, then you will be happy to know that there is such an excellent debugger - GDB - which can help you debug code bugs and other more easily question. In this post, we'll discuss the basics of the GDB debugger, including some of the useful features/options it provides.

Before we start, it's worth mentioning that all the instructions and examples in the article have been tested on Ubuntu 14.04 LTS. The sample code in the tutorial is written in C; the shell used is bash (4.3.11); the GDB version is 7.7.1.

GDB debugger basics In
layman 's terms, GDB allows you to see the internal flow of the program during execution and helps you identify the problem. We'll discuss the use of the GDB debugger with a working example in the next section, but before that, let's discuss some basic points that will help you later.

First of all, in order to use a debugger like GDB smoothly, you must compile the program in a specified way, so that the compiler generates the debugging information required by the debugger. For example, you need to use the -g command line option when compiling code using the gcc compiler (which we will use to compile sample C programs in later chapters of this tutorial).

To read about the -g command line option in the gcc compiler man page, see here.

Next, make sure the GDB debugger is installed on your system. If it is not installed, and you are using a Debian-based system (eg Ubuntu), then you can easily install the tool with the following command:

sudo apt-get install gdb
On other distributions, see here.

Now, when you have compiled the program as above (gcc -g command line option), and have installed the GDB debugger, then you can run the program in debug mode with the following command:

gdb [executable program's name]
Doing so will initialize the GDB debugger, but your executable will not be started at this point. At this point you can define debug related settings. For example, you can set a breakpoint on a specific line or function to have GDB pause program execution at that line.

Next, in order to start your program, you must execute the following gdb command:

run
Here, it is worth mentioning that if your program requires some command line parameters, then you can specify these parameters here. For example:

run [parameters]
GDB provides many useful commands that are always useful when debugging. We will discuss some of these commands in the next section.

GDB debugger use cases
Now we have a basic idea of ​​GDB and its usage. So let's apply what we've learned with an example. Here is a sample code:

#include <stdio.h>
int main()
{
int out = 0, tot = 0, cnt = 0;
int val[] = {5, 54, 76, 91, 35, 27, 45, 15, 99, 0};
while(cnt < 10)
{
out = val[cnt];
tot = tot + 0xffffffff/out;
cnt++ ;
}
printf("\n Total = [%d]\n", tot);
return 0;
}
Briefly explain what this code does. Get each value in the val array, assign it to the out variable, then add the value before tot and the result value of 0xffffffff/out, and assign it to the tot variable.

The problem encountered here is that when executing the compiled executable of this code, the following error is generated:

$ ./gdb-test
Floating point exception (core dumped)
So, to debug this code, the first step is to use The -g option compiles the program. The command is as follows:

gcc -g -Wall gdb-test.c -o gdb-test
Next, let's run the GDB debugger and specify the executable to debug. The command is as follows:

gdb ./gdb-test
Now, the error I just got is Floating point exception, most of you may already know, this is because n % x, when x is 0, the error is caused. So, with that in mind, I added a breakpoint at line 11 where the division operation occurs. as follows:

(gdb)&;break 11
Note that (gdb) is the prompt information of the debugger, I only entered the break 11 command.

Now, let GDB start running the program:

run
When the breakpoint is hit for the first time, GDB displays the following output:

Breakpoint 1, main () at gdb-test.c:11
11 tot = tot + 0xffffffff/out;
(gdb)
As you can see, the debugger shows the line of code where the breakpoint is located. Now, let's print out the value of out at this point. As follows:

(gdb) print out
$1 = 5
(gdb)
As shown above, the value 5 is printed. At this time everything is normal. Let the debugger continue executing the program until the next breakpoint is hit, which can be done by using the c command:

c
Repeat the above operation until the value of out becomes 0.

...
...
...
Breakpoint 1, main () at gdb-test.c:11
11 tot = tot + 0xffffffff/out;
(gdb) print out
$2 = 99
(gdb) c
Continuing.
Breakpoint 1, main () at gdb-test.c:11
11 tot = tot + 0xffffffff/out;
(gdb) print out
$3 = 0
(gdb)
Now, to further confirm the problem, I use GDB's s (or step) command instead of the c command. Because, I just want the current program to pause after line 11 and execute step by step to see if a crash occurs at this time.

The following is the output information after execution:

(gdb) s
Program received signal SIGFPE, Arithmetic exception.
0x080484aa in main () at gdb-test.c:11
11 tot = tot + 0xffffffff/out;
Yes, the first line of the above output As the content shows, this is where the exception is thrown. When I tried running the s command again, the problem was finally confirmed:

(gdb) s
Program terminated with signal SIGFPE, Arithmetic exception.
The program no longer exists.
In this way, you can use GDB to debug your program.

Summary
GDB provides many features for users to research and use, here, we only introduce a small part of the content. Learn more about this tool through the GDB man page, and try it out when you're debugging code. The GDB debugger is a bit of a learning curve, but it's well worth the effort.

The original release time is: 2017-02-04

This article is from Yunqi Community Partner "Linux China"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326199298&siteId=291194637