Chapter 6 linux debugger - the use of gdb

1. Premise

1、debug与release

The final release method of the program is divided into two types, one is debug mode and the other is release mode. Usually, the executable files compiled by g++/gcc are in release mode, that is, in release mode. What is the difference between the two? The debug mode allows debugging with gdb, so there will be some debugging information in the debug mode, and this mode is usually used by programmers. Another release mode is called the release mode. This mode does not contain debugging information, so it cannot be debugged with gdb, so the release mode is usually used by users.

So in order for us to use gdb to debug the code, we need to add it to the compilation instructions of the compiler -g. As shown below:
insert image description here

2. Installation of gdb

Some linux servers do not have gdb, so you need to install it yourself. Because the author is using Ubuntu's Linux, so here is the installation command of Ubuntu.

 sudo apt install gdb

2. Common debugging instructions

1. Start debugging

We need to enter the command on the command line:

gdb 二进制可执行文件

As shown in the figure below:
insert image description here
When we start debugging, we can enter some commands in gdb to debug.

2. Code display

l 行号                  //从第几行开始显示代码(往后显示10行),显示结束后,再按l,可以继续往后显示。
l 函数名                //列出某个函数代码

insert image description here

3. Breakpoint setting

b 行号                                           //在某一行设置断点
b 函数名                                         //在某个函数的开头设置断点
info break                                      //查看断点信息
delete breakpoints                              //删除所有断点
delete breakpoints                              //n删除序号为n的断点
disable breakpoints  断点编号                     //禁用断点
enable breakpoints   断点编号                     //启用断点

Set breakpoints
insert image description here
View or modify breakpoints
insert image description here

4. Code execution

r                        // 运行程序。
n                        // 逐过程执行。
s                      	 // 逐语句执行。
until x                  //跳至x行
finish                   // 执行到当前函数返回,然后停下来等待后续的命令。

5. Information monitoring

p 变量                   // 打印变量的数值
set var                 // 修改变量的数值
c                       // 从当前位置开始连续而非单步执行程序
display 变量             // 跟踪查看变量,每次停下来都显示该变量的数值 
undisplay                //取消跟踪某个变量

6. Exit debugging

quit                        // 退出调试

Guess you like

Origin blog.csdn.net/weixin_72060925/article/details/131501768