1.使用gdb编译必须在编译选项中加上-g: g++ -g hello.cpp -o hello
2.使用gdb调试的方法:
(1)gdb ./hello
(2)gdb ./hello hello.core
(3)gdb ./hello PID
3.gdb帮助:
(1)help:打出命令的类别
(2)help command:打出具体某命令的使用方法
(3)在gdb执行shell命令:shell command
4.gdb常用命令:
l(ist),b(reak),n(ext),s(tep),r(un),c(ontinue),p(rint),bt,finish,q(uit)
回车:重复上一个命令
set args, show args:设置、查看运行时参数
5.设置断点:
break function
break function(type, type, ...)
break class::function
break filename:function
break linenum
break filename:linenum
break +/- offset
break address(代码运行的内存地址)
break nothing:在下一条指令处停止
break ... if condition
info break, info breakpoints, info break[i], info breakpoints[i]:查看断点
6.设置观察点:
watch expr
rwatch expr:当expr被读时,停住程序
awatch expr:当expr被读或被写时,停住
info watchpoints
7.设置捕捉点:
catch event
throw:C++抛出异常
catch:C++捕获异常
load, unload:载入,卸载共享库(动态链接库)
exec,fork,vfork
8.管理断点
clear,delete,enable,disable
9.设置变量值
set i = 1;
set var i = 1;