gcc编译需要添加-g
1.gdb运行程序
gdb ./Test
2.设置断点
1.设置断点在某一行
b xxx.cpp:100
2.设置断点在某个函数
b xxx.cpp:fun
如果是成员函数则需要类名
b xxx.cpp:A::fun
这样运行到这里就中断。
如果想要某种条件才中断
b xxx.cpp:A::fun if a = 10
条件很多上面这个不方便 可以使用 command命令
info b 查看当前断点信息
例如
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000006d58b5 in GamePlayer::TurnOutCard() at src/GamePlayer.cpp:88
2 breakpoint keep y 0x00000000006e2f20 in LoadGameConfig(Ini*) at src/GameStructs.cpp:12
我想在第2个断点处使用,怎么做呢?
command 2
命令
...
end //结束标记
3.r //运行
4.c //继续 相当于F5
5.s //进入函数里面 相当于F11
6.n //下一行 相当于F10
7.finish //结束当前函数
8.enter键 //重复上一命令
9.ctrl + c //立即中断
10.bt //打印当前堆栈
#0 0x0000003b86aacc4d in nanosleep () from /lib64/libc.so.6
#1 0x0000003b86ae1e94 in usleep () from /lib64/libc.so.6
#2 0x0000000000412f98 in MySleep (millionseconds=9) at ../../Common/src/GameUtil.cpp:119
#3 0x000000000061d2fe in Server::Loop (this=0xc6f640) at ../../GameServer/src/Server.cpp:308
#4 0x000000000061eb56 in Server::Run (this=0xc6f640) at ../../GameServer/src/Server.cpp:675
#5 0x000000000040a250 in main (argc=1, argv=0x7fffffffe428) at Main.cpp:9
跳转堆栈(#后面的数组 就是堆栈号(我也不知道就啥,随便吧))
f 2
11.打印值
p a //打印变量
p *(b+2)@10 //打印数组 从b+2 开始10个长度的值
p/d a //十进制打印
12.有时候程序可能不是gdb下运行的 现在想调试怎么办呢?
ps aux|grep Test //查找需要调试程序的进程号
gdb -p 进程号 //附加到进程 这样就可以用上面的命令了
也可以直接将命令写在文件中
gdb -p 进程号 -x 调试命名文件
13.有时候程序 coredump 怎么办
gdb ./Test core文件名
bt 查看当前堆栈
没有core文件怎么呢
dmesg
PDKServer[2543]: segfault at 48 ip 000000000071c58d sp 00007ffedf37a9e0 error 4 in PDKServer[400000+785000]
addr2line 000000000071c58d -e PDKServer -f //显示程序core在哪一行
14.q 退出gdb
15.
把下面加到登录脚本的最后
/etc/profile
ulimit -c unlimited
ulimit -u 16384
ulimit -n 102400
ulimit -d unlimited
ulimit -m unlimited
ulimit -s unlimited
ulimit -t unlimited
ulimit -v unlimited
差不多就这些
gdb基本调试命令
猜你喜欢
转载自blog.csdn.net/seanbill/article/details/88597948
今日推荐
周排行