Use gdb to debug the program

Use gdb to debug the program

background

First of all, we need to understand the following:
1. There are two ways to release the program, debug mode and release mode
2. The binary program out of Linux gcc/g++, the default is release mode
3. To use gdb to debug, it must be in the source code Add the -g option when generating the binary program

What is gdb

gdb is a command line-based, powerful program debugging tool released by the gnu open source organization under the UNIX/LINUX operating system.

manual

1. Enter gdb binFile to enter debugging
2. Debug command explanation

list 行号:显示binFile源代码,接着上次的位置往下列,每次列10行
list 函数名:列出某个函数的源代码
r或run:运行程序
n或next:单条执行
s或step:进入函数调用
break(b)行号:在某一行开头设置断点
info break:查看断断点信息
finsh:执行到当前函数返回,然后停下来等待命令
print(p):打印表达式的值,通过表达式可以修改变量的值或者调用函数
p 变量:打印变量的值
set var:修改变量的值
continue(c):从当前位置开始连续而非单步执行程序
run(r):从开始连续而非单步执行程序
delete breakpoints:删除所有断点
delete breakpoints n:删除序号为n的断点
disable breakpoints:禁用断点
info(或i) breakopints:参看当前设置了那些断点
display 变量名:跟踪查看一个变量,每次停下来都显示它的值
undisplay:取消对先前设置的那些变量的跟踪
until X行号:跳至X行
breaktrace(bt):查看各级函数调用及参数
info(i) local:查看当前栈局部变量的值
quit:退出gdb
CTRL + d:退出gdb

Note: You must use -g when compiling a program debugged with gdb, otherwise the problem of No symbol table is loaded. Use the "file" command. will appear.

Guess you like

Origin blog.csdn.net/qq_43825377/article/details/112689133