continue/c 继续运行
next/n 单步运行
step/s 如果有函数则进入函数执行
finish 跳出当前的函数
jump/j 跳转到指定行/地址后继续执行,因此如果在跳转的目标行上如果没有设置断点,会继续往下执行
stop 停止运行
until xxx 可用于跳出循环
quit/ctrl+d 退出GDB
print/p var 打印变量的值
print/p &var 打印变量地址
printf/p *addr 打印地址的值
printf/p /x var 用16进制显示数据
x十六进制/d十进制/u十六进制无符号/t二进制/c字符/f浮点
break/b xxx 在某行打断点
break/b fun 在某个函数处加断点
break/b 30 if n==100 //当变量n等于100的时候在30行处加断点
break fileName:N 在某个文件的N行加断点
info break/b [n] 查看断点,(注:n表示断点号,可选。info b列出所有断点信息)
clear N 删除N行断点
delete N 删除N号断点
delete 删除所有断点
disable xxx 失能断点
enable xxx 使能断点
info b 查看断点
info source 查看当前程序
info stack 查看堆栈信息
info args 查看当前参数值
info threads 查看线程
display args 查看当前参数值
bt 查看函数堆栈
pwd查看程序路径
ctrl+p 前一条命令
ctrl+n 下一条命令
watch xxx 设置监控点,在变量改变的时候停下来。(不可直接设置,先加断点在监测)
ctrl+l可能layout会造成控制台花屏,使用ctrl+L清屏
list linenum:以linenum指定的行号为中心,显示10行
list function:以指定的函数为中心,显示10行
list:重复上一次的list指令,也可以直接按回车键,重复上次指令。
set listsize count:设置每次显示的行数。
show listsize:显示已设置的显示行数。
list first,last:显示指定起始行到结束结束行的源文件。
list ,last:显示以指定的last为结束行,显示10行。
list first,:以first为第一行,显示10行。
list +:以上次显示的结束行为起始行显示后10行
list –:以上次显示的起始行为结束行,显示前10行
show scheduler-locking: 查看gdb工作模式,默认为step, 即在调试的时候可以暂停其他线程的执行,以免线程混乱或者其他线程超时。
set scheduler-locking mode: 设置gdb工作模式,具体参考:
https://sourceware.org/gdb/onlinedocs/gdb/All_002dStop-Mode.html
set variable variable = [expression] 设置变量的值,可以缩写为 set var ......
Parameters
variable |
The variable to set. If variable starts with a dollar sign ($) then it is either a predefined register name or a debugger variable, either a predefined variable or a user variable. If variable does not start with a dollar sign ($), it is a variable in the program. |
expression |
The value for the variable. |
(gdb) print node->_data 2 (gdb) set variable node->_data = 5 (gdb) print node->_data 5
常见问题:
1. print变量的时候出现<value optimized out>
gdb调试程序的时候打印变量值会出现<value optimized out> 情况,可以在gcc编译的时候加上 -O0参数项,意思是不进行编译优,发布项目的时候不要使用 -O0参数项,gcc 默认编译或加上-O2优化编译会提高程序运行速度.
参考文献:
https://blog.csdn.net/u014015972/article/details/51620694
http://scc.ustc.edu.cn/zlsc/chinagrid/intel/debugger/cl/GUID-BD46F547-7EFA-4066-96BB-7CB9EF415140.htm
https://sourceware.org/gdb/onlinedocs/gdb/All_002dStop-Mode.html
https://sourceware.org/gdb/onlinedocs/gdb/Non_002dStop-Mode.html
https://www-zeuthen.desy.de/unix/unixguide/infohtml/gdb/All_002dStop-Mode.html