Home > System Tutorial > LINUX > The gospel of debugging C programs in Linux - gdb

The gospel of debugging C programs in Linux - gdb

王林
Release: 2024-02-11 20:12:02
forward
970 people have browsed it

Every programmer, no matter how experienced, cannot completely avoid bugs in software. Therefore, troubleshooting and fixing bugs has become one of the most crucial tasks in the software development cycle. While there are many ways to troubleshoot bugs, such as testing and code self-review, specialized debugger software can also help pinpoint the problem so it can be fixed.

For programmers who use the C/C programming language or Fortran and Modula-2 to develop software, there is an excellent debugger tool-GDB, which will be good news to them. GDB not only helps programmers debug bugs and other problems in their code more easily, but it also provides many useful features and options. In this article, we will discuss some basics of the GDB debugger, as well as some of the useful features and options it provides.

在 Linux 中调试 C 程序的福音——gdb

Before we begin, it’s worth mentioning that all instructions and examples in the article have been tested on Ubuntu 14.04 LTS. The sample codes in the tutorial are all written in C language; 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 help 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 cover some basic points that will help you later.

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

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

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

sudo apt-get install gdb
Copy after login

For installation methods on other distributions, please see here.

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

gdb [可执行程序的名称]
Copy after login

Doing this will initialize the GDB debugger, but your executable program will not be started at this time. At this point you can define debugging related settings. For example, you can set a breakpoint at a specific line or function to have GDB pause program execution at that line.

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

run
Copy after login
Copy after login

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

run [参数]
Copy after login

GDB provides many useful commands, which can always come in handy when debugging. We will discuss some of these commands in the next section.

GDB debugger use case

Now we have a basic idea of ​​GDB and its usage. So let’s apply what we’ve learned with an example. This is a sample code:

#include 
int main()
{
    int out = 0, tot = 0, cnt = 0;
    int val[] = {5, 54, 76, 91, 35, 27, 45, 15, 99, 0};

    while(cnt printf("/n Total = [%d]/n", tot);
    return 0;
}
Copy after login

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 the executable program compiled by this code is executed, the following error is generated:

$ ./gdb-testFloating point exception (core dumped)
Copy after login

So, to debug this code, the first step is to compile the program with the -g option. The command is as follows:

gcc -g -Wall gdb-test.c -o gdb-test
Copy after login

Next, let's run the GDB debugger and specify the executable program to debug. The command is as follows:

gdb ./gdb-test
Copy after login

Now, the error I just got was a Floating point exception, which most of you probably already know, is caused by n % x when x is 0. So, with that in mind, I added a breakpoint at line 11 where the division operation occurs. as follows:

(gdb)&;break 11
Copy after login

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

Now, let GDB start running the program:

run
Copy after login
Copy after login

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)
Copy after login

As you can see, the debugger displays the line of code where the breakpoint is located. Now, let's print out the value of out at this time. as follows:

(gdb) print out
$1 = 5
(gdb)
Copy after login

如上所示,值 5被打印出来了。这个时候一切都还是正常的。让调试器继续执行程序直到命中下一个断点,可以通过使用 c 命令来完成:

c
Copy after login

重复上述操作,直到 out值变为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)
Copy after login

现在,为了进一步确认问题,我使用 GDB 的s (或 step) 命令代替 c 命令。因为,我只想让当前程序在第 11 行之后暂停,再一步步执行,看看这个时候是否会发生崩溃。

以下是执行之后输出信息:

(gdb) s

Program received signal SIGFPE, Arithmetic exception.
0x080484aa in main () at gdb-test.c:11
11 tot = tot + 0xffffffff/out;
Copy after login

是的,如上输出的第一行内容所示,这就是抛出异常的地方。当我再次尝试运行s 命令时,问题最终也得到了确认:

(gdb) s

Program terminated with signal SIGFPE, Arithmetic exception.
The program no longer exists.
Copy after login

通过这种方式,你就可以使用 GDB 调试你的程序。

总结

GDB 提供了很多功能供用户研究和使用,在这里,我们仅仅只介绍了很少一部分内容。通过 GDB 的手册页可以进一步了解这个工具,当你在调试代码的时候,尝试使用一下它。GDB 调试器有一定的学习难度,但是它很值得你下功夫学习。

The above is the detailed content of The gospel of debugging C programs in Linux - gdb. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template