多文件程序的调试,例子:
文件结构:
/demo
Makefile
/src
demo.cpp
util.cpp
/include
util.h
截图:
----------------------------------------------------------------------
-----------------------------------------------------------
注意detach和kill的区别,仔细看文档的说明:
detach
When you have fiished debugging the attached process, you can use the detach
command to release it from gdb control. Detaching the process continues its
execution. After the detach command, that process and gdb become completely
independent once more, and you are ready to attach another process
or start one with run. detach does not repeat if you press RET again after
executing the command.
kill
Kill the child process in which your program is running under gdb.
This command is useful if you wish to debug a core dump instead of a running process.
gdb ignores any core dump fie while your program is running.
On some operating systems, a program cannot be executed outside gdb while you have
breakpoints set on it inside gdb. You can use the kill command in this situation to permit
running your program outside the debugger.
The kill command is also useful if you wish to recompile and relink your program,
since on many systems it is impossible to modify an executable fie while it is running in a
process. In this case, when you next type run, gdb notices that the fie has changed, and
reads the symbol table again (while trying to preserve your current breakpoint settings).
GDB文档下载地址:https://www.gnu.org/software/gdb/documentation/
-----------------------------------------------
如何给被调试的程序设置命令行参数:
gdb demo_exe
set args
show args // 查看已设置的命令行参数
或者
gdb demo_exe
run
参考资料:http://www.cnblogs.com/rosesmall/archive/2012/04/10/2440514.html
-----------------------------------------------------------
专题-GDB设置断点,变量观察:http://wangjinxin7.blog.163.com/blog/static/1752150162012175255611/
专题-GDB打印变量、指针、结构:http://blog.chinaunix.net/uid-26822401-id-3155297.html
---------------------------------------------------------
这里着重讲一下GDB的TUI模式,尽管没有GUI方便,不过还是比命令行要直观一些的,相比命令行界面最大的好处就是你能直观的看到你当前程序跑到哪个位置了,也不用在GDB和EDITOR之间来回切换,并且你可以在一个窗口中同时查看源代码、汇编代码、寄存器、命令行4个区域(如果你都打开的话)。
其他方面,TUI模式与命令行模式基本没有区别
TUI - Text User Interface,说白了,就是在command-line的窗口上画一个类似GUI的界面,效果图如下
这里主要说一下最基本的也是最常用的使用方法(相关的命令),完整的文档可以去http://www.gnu.org/software/gdb/documentation/下载一个PDF来看,目前TUI的内容在Chapter 25
1.打开/关闭TUI模式
gdb -tui
其中
开关次模式的快捷键是CTRL+X A,意思就是你按着CTRL不放的同时,依次按X,A
2.如果你不需要查看寄存器和汇编指令的话,只打开源代码窗口(src)和命令窗口(cmd)即可
layout src
3.另外,如果你需要在源代码窗口按KEY_UP,KEY_DOWN,KEY_LEFT,KEY_RIGHT,KEY_PGUP,KEY_PGDN,KEY_HOME,KEY_END进行代码查看的话,使用如下命令可以切换到带源代码窗口
focus src
类似的如果你需要在命令窗口使用KEY_UP,KEY_DOWN查看命令的话,使用如下命令切换到命令窗口
focus cmd
------------------------------------------------------------
命令的话,这里大多数只写缩写的格式。可以打开GDB后输入help查看帮助,或者直接去看documentation或网上的教程
n
单步(step over, next)
break
给某个函数打断点
break
在某一行打断点
l
ENTER
执行上一次命令
info break
查看断点信息
r
运行程序(run)
p
打印变量的值(print)
bt
查看调用栈(backtrace)
finish
退出函数
c
继续运行直到遇到下一个断点(continue)
q
退出GDB(quit)
info locals
查看局部变量
info watch
查看监视
break
满足
condition
给已存在的断点设置中断条件
delete/disable/enable
删除/启用/禁用 断点/监视
watch
添加监视,满足
frame <#frame-number>
切换frame&#xff08;其实一个frame就是调用栈的一层&#xff09;&#xff0c;可以结合info locals查看调用栈各层的local变量&#xff0c;利用bt命令可以查看当前调用栈的所有frame(#0为栈顶frame)
set
给变量设置新值&#xff0c;比如你有一个char buf[255]你可以在GDB调试的时候输入set *buf&#61;&#39;M&#39;从而把buf[0]修改为‘M’&#xff0c;你可以通过p buf来查看
----------------------------------------------------------------------
我的GDB学习路线&#xff1a;
// 介绍了最最最基础的命令
http://blog.csdn.net/haoel/article/details/2879
// 下面这一篇非常重要&#xff0c;必看
http://www.ibm.com/developerworks/cn/linux/sdk/gdb/index.html
上面那篇讲到core dump file的时候&#xff0c;如果发现无法产生core dump file&#xff0c;则参考这篇文章&#xff1a;http://www.cnblogs.com/qrlozte/p/4435065.html
另外&#xff0c;上面那篇讲到连接进程的内容的时候&#xff0c;一般要用sudo命令提供root密码才行&#xff0c;比如
sudo gdb demo_exe attach 28688
或者&#xff0c;拆开为3条命令
sudo gdb
// 输入root密码
file demo_exe
attach 28688 // ./demo_exe & 可以查看到process id
http://www.programlife.net/gdb-manual.html
&#64;