Article Directory
1. Background knowledge
- There are two ways to release the program, debug (debugging) mode and release (release) mode;
- The binary program from Linux gcc/g++ is in release mode by default;
- gdb is a debugger in Linux.
-g
It is used to debug the debug version code. If you want to use gdb to debug, you must add the option when generating the binary program from the source code
2. Install gdb
sudo yum install gdb //想直接安装就加上-y选项
Three. The usage of gdb
In order to analyze the various usages of gdb in detail, we first create two files: test.c and Makefile for testing gdb.
The following are the test codes in the two files:
//test.c:我们用一个从1加到100的程序来测试gdb
#include<stdio.h>
int addToTop(int top)
{
printf("enter addToTop\n");
int sum=0;
for(int i=1;i<=top;i++)
{
sum+=i;
}
printf("quit addToTop\n");
return sum;
}
int main()
{
int top=100;
int ret=addToTop(top);
printf("ret:%d\n",ret);
return 0;
}
//Makefile(自动化构建项目的工具)
mytest:test.c //依赖test.c文件生成mytest可执行程序
gcc test.c -g -o mytest -std=c99 //注意这里加上了-g选项,也用上了c99标准
.PHONY:
make clean:
rm -f mytest
Running result :
Terms and Conditions
There are two ways to publish programs:
- debug version: more debugging information will be added to the program itself for easy debugging.
- Release version: No debugging information will be added, and it is not debuggable.
In Linux, the executable program generated by gcc/g++ by default is the release version, which cannot be debugged. If you want to generate a debug version, you need to add options when using gcc/g++ to generate executable programs -g
.
We can use the readelf command to view the file information in ELF format:
generate the executable program of the release version and the debug version for the same source code, and the size of the executable program released by the debug version is larger than the size of the executable program released by the release version Bigger, from the figure above, we can see that there is debug information in the debug version. The reason is that the executable program released with the debug version contains more debugging information.
Common commands of gdb
1. Enter debugging
gdb [可执行程序] //在上述我们写的测试代码中,可执行程序命名为mytest
//所以接下来我们就以mytest为例
2. Exit the debugging operation
ctrl+d 或 quit
3. Show source code
list/l 行号:显示源代码,如果按换行键会接着上次显示的位置往下列,每次只显示10行
list/l 函数名:可以直接定位到函数名的位置
Operation example :
4. Set breakpoint breakPoint
beak(b) 行号:在某行设置一个断点
b 函数名:在某个函数开头设置断点
Operation example :
it means to set a breakpoint at line 19 of the source code
means to set a breakpoint at the beginning of the addToTop function of the source code, and it will automatically recognize line 5 (where the function is located)
5. View breakpoint information/disable breakpoint/enable breakpoint/delete breakpoint
info b(break)
The info b command will display all breakpoints. We have just set two breakpoints, so the information of two breakpoints is displayed. Num is the number of the breakpoint, Enb indicates the status of the breakpoint, and y indicates that the breakpoint is normal. Use n to indicate that the breakpoint is disabled.
Which breakpoint you want to operate is not the line number of the breakpoint in the code, but the number in the breakpoint list.
Disable breakpoints :
disable [断点编号Num]
Enable breakpoints :
enable [断点编号Num]
Remove breakpoint :
d(delete) [断点编号] //不加断点编号,则删除所有已设置的断点
6. Run the program and start debugging run
After running the program, it will stop at the breakpoint, just like we debug with the vs compiler under Windows. If you don't set a breakpoint, the program will run directly and output the result.
r或run
7. View variables
print variable value:
p 变量名
Trace view of a variable (long display):
display 变量名
Cancel tracking of variables (cancel long display):
undisplay 变量名
8. Other important commands
set var i=100
: set a variable to a specific valuecontinue(或c)
: From one breakpoint, run directly to the next breakpointuntil 行号
: In the function, jump to the specified position and execute the interval codebt(或breaktrace)
: view call stackinfo (i)locals
: View the value of the local variable of the current stack framen或next
: Execute code step by steps或stap
: Execute the code statement by statement, you can enter the function bodyfinish
: Enter a function and stop after executing the function. For example, the above-mentioned function adding from 1 to 100, if we are still executing the for loop at this time, we can directly input finish to point directly to the completion of addToTop function.print(p)
: Print the value of the expression, you can modify the value of the variable or call the function through the expressioninfo
: View the manual of gdb