日期:2014-05-16  浏览次数:20443 次

gdb积累学习
gdb积累学习
1、查看当前文件
通过gdb进行调试的时候,有的时候因为跳转的太多,所以不知道已经执行到了哪一个文件,这个时候,如果我们想看一下当前文件的名字,可以使用下面的命令:
(gdb) info source
Current source file is CDBStatement.cpp
Compilation directory is /account/work/ymm/CDB
Located in /account/work/ymm/CDB/CDBStatement.cpp
Contains 983 lines.
Source language is c++.
Compiled with DWARF 2 debugging format.
Does not include preprocessor macro info.
通过上面的方式,我们就不但可以查看到所在的文件,如果整个程序使用不同的文件话,我们还可以看到该文件所在的路径,这样在gdb的时候,就不会迷路了。
当然,我们进入gdb之后,我们首先应该进入文件,否则就会出现下面的情况:
[billing_dx@bmcs1 Account]$ gdb main
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-56.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /account/work/ymm/Account/main...done.
(gdb) info source
No current source file.
(gdb) 
扩展阅读: http://bbs.chinaunix.net/thread-1032298-1-1.html 


2、显示执行的行数(bt查看堆栈)
虽然我们通过next、setp的方式执行或者list的方式都可以显示行号,但是,有的时候因为我们通过list显示了过多的内容,这个时候显示的行数有可能都是执行行数下面很多行之后,如果这个时候我们不想继续执行(如果继续执行的话,可以肯定将要出错,而跟踪到目前为止很麻烦),我们就可以通过查看堆栈的方式查看当前行数。
gdb) bt
#0  CDProductImpl::GetGroupInfo (this=0x690b50, szTableNameS=0x7fffffffd160 "", szUri=0x7fffffffd7f0 "/Serv/ById", 
    iDec=@0x7fffffffd26c, iUni=@0x7fffffffd268) at CDProductImpl.cpp:1627
#1  0x00007ffff5907ad0 in CDProductImpl::Get (this=0x690b50, szUri=0x7fffffffd7f0 "/Serv/ById", 
    szParamList=0x7fffffffd6f0 "Serv_Id =1004", pBuf=0x7fffffffd950, iNum=@0x7fffffffd96c, szMsg=0x7fffffffd2f0 "")
    at CDProductImpl.cpp:24
#2  0x00007ffff59077bc in CDProduct::Get (this=0x677300, szUri=0x7fffffffd7f0 "/Serv/ById", 
    szParamList=0x7fffffffd6f0 "Serv_Id =1004", pBuf=0x7fffffffd950, iNum=@0x7fffffffd96c, szMsg=0x7fffffffd2f0 "")
    at CDProduct.cpp:24
#3  0x0000000000402a05 in CUserDataQry::GetUserOfferByServId (this=0x690d80) at CUserDataQry.cpp:127
#4  0x0000000000402367 in CUserDataQry::GetUserInfo (this=0x690d80, lServId=1004, szAccNbr=0x7fffffffda00 "1234", 
    lProductId=87654, pUserInfo=0x7fffffffda30) at CUserDataQry.cpp:37
#5  0x00000000004020c8 in main (argc=1, argv=0x7fffffffdb58) at main.cpp:42
通过上面的提示,我们可以很容易的看到程序的运行情况,而且可以明确当前的位置是CDProductImpl.cpp:1627。
当然,如果gdb调试都没有运行的话,就会因为堆栈内没有信息出现下面的提示:
(gdb) bt
No stack.
(gdb) 


我们还可以通过通过"info stack"命令,也可以查看堆栈的内容,显示的结果一样。
而"bt"命令应该就是"backtrace"简写,回溯,向后追踪的意思。


3、显示当前文件路径
和linux命令窗口一样,使用pwd就可以查看当前路径
(gdb) pwd   
Working directory /account/work/wangcq/ymm1.


4、设置断点时的警告
在调试的时候,我们或许会在动态库的地方设置断点,因为该文件的源文件不在当前工程中,所以在设置断点的时候会出现警告,如下:
(gdb) b CDProductImpl.cpp:24
No source file named CDProductImpl.cpp.
Make breakpoint pending on future shared library load? (y or [n]) 
这个时候,我们只需要设置一下"set breakpoint pending on"就可以在添加不能识别的断点的时候,也不出现警告了。如下
(gdb) set breakpoint pending on
(gdb) b a.cpp:10
No source file named a.cpp.
Breakpoint 1 (a.cpp:10) pending.
(gdb) 
虽然会提示该文件不存在,但是我们的断点依旧设置成功。
附:pending breakpoints:是指设置在程序开始调试后加载的动态库中的位置处的breakpoints
a.set breakpoint pending auto: GDB缺省设置,询问用户是否要设置pending breakpoint
b.set breakpoint pending on: GDB当前不能识别的breakpoint自动成为pending breakpoint
c.set breakpoint pending o