GDB命令使用详解

下面是man gdb手册,把常用的加了中文注释

gdb(1)                                                            GNU Tools                                                           gdb(1)




NAME  
       gdb - The GNU Debugger

SYNOPSIS
       gdb    [-help] [-nx] [-q] [-batch] [-cd=dir] [-f] [-b bps] [-tty=dev] [-s symfile] [-e prog] [-se prog] [-c core] [-x cmds] [-d dir]
              [prog [core|procID]]

       gdb    [options] --args prog [arguments]

       gdbtui [options]

DESCRIPTION
       The purpose of a debugger such as GDB is to allow you to see what is going on ``inside'' another program while  it  executes—or  what
       another program was doing at the moment it crashed.

       GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:


          ·   Start your program, specifying anything that might affect its behavior.


          ·   Make your program stop on specified conditions.


          ·   Examine what has happened, when your program has stopped.


          ·   Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.

       You  can  use  GDB  to  debug programs written in C, C++, and Modula-2.  Fortran support will be added when a GNU Fortran compiler is
       ready.

       GDB is invoked with the shell command gdb.  Once started, it reads commands from the terminal until you tell it to exit with the  GDB
       command quit.  You can get online help from gdb itself by using the command help.

       You  can  run  gdb  with no arguments or options; but the most usual way to start GDB is with one argument or two, specifying an exe‐
       cutable program as the argument:

       gdb program


       You can also start with both an executable program and a core file specified:

       gdb program core


       You can, instead, specify a process ID as a second argument, if you want to debug a running process:

       gdb program 1234


       would attach GDB to process 1234 (unless you also have a file named `1234'; GDB does check for a core file first).

       Here are some of the most frequently needed GDB commands:
        //在函数处放置断点
       break [file:]function  
               Set a breakpoint at function (in file).
             //运行程序,如果有变量的话使用变量
       run [arglist]
              Start your program (with arglist, if specified).
        //    显示函数的栈调用情况
       bt     Backtrace: display the program stack.
        //打印变量的值
       print expr
               Display the value of an expression.
    //继续运行
       c      Continue running your program (after stopping, e.g. at a breakpoint).
    //下一步,跟VS里面的next step很像,这样,执行一行代码,如果是函数也会跳过函数
       next   Execute next program line (after stopping); step over any function calls in the line.
    
       edit [file:]function
              look at the program line where it is presently stopped.

       list [file:]function
              type the text of the program in the vicinity of where it is presently stopped.
        //进入函数,这样,也会执行一行代码,不过如果遇到函数的话就会进入函数的内部,再一行一行的执行。
       step   Execute next program line (after stopping); step into any function calls in the line.

       help [name]
              Show information about GDB command name, or general information about using GDB.
    //退出
       quit   Exit from GDB.

       For full details on GDB, see Using GDB: A Guide to the GNU Source-Level Debugger, by Richard M. Stallman and Roland  H.  Pesch.   The
       same text is available online as the gdb entry in the info program.

OPTIONS
       Any  arguments  other  than options specify an executable file and core file (or process ID); that is, the first argument encountered
       with no associated option flag is equivalent to a `-se' option, and the second, if any, is equivalent to a `-c' option  if  it's  the
       name  of  a file.  Many options have both long and short forms; both are shown here.  The long forms are also recognized if you trun‐
       cate them, so long as enough of the option is present to be unambiguous.  (If you prefer, you can  flag  option  arguments  with  `+'
       rather than `-', though we illustrate the more usual convention.)

       All  the  options  and command line arguments you give are processed in sequential order.  The order makes a difference when the `-x'
       option is used.

       -b bps  Set the line speed (baud rate or bits per second) of any serial interface used by GDB for remote debugging.

    
       -batch Run in batch mode.  Exit with status 0 after processing all the command files specified with  `-x'  (and  `.gdbinit',  if  not
              inhibited).  Exit with nonzero status if an error occurs in executing the GDB commands in the command files.

              Batch  mode may be useful for running GDB as a filter, for example to download and run a program on another computer; in order
              to make this more useful, the message

              Program exited normally.


              (which is ordinarily issued whenever a program running under GDB control terminates) is not issued when running in batch mode.
    

       -c FILE, -core=FILE
              Use file file as a core dump to examine.
    //指定工作目录

       -cd=directory
               Run GDB using directory as its working directory, instead of the current directory.
    //增加查找源文件的目录

       -d DIRECTORY, -directory=DIRECTORY
              Add directory to the path to search for source files.
    

       -e FILE, -exec=FILE
              Use file file as the executable file to execute when appropriate, and for examining pure data in conjunction with a core dump.


       -f, -fullname
              Emacs sets this option when it runs GDB as a subprocess.  It tells GDB to output the full file name and line number in a stan‐
              dard,  recognizable fashion each time a stack frame is displayed (which includes each time the program stops).  This recogniz‐
              able format looks like two ` 32' characters, followed by the file name,  line  number  and  character  position  separated  by
              colons,  and  a  newline.   The Emacs-to-GDB interface program uses the two ` 32' characters as a signal to display the source
              code for the frame.


       -h, -help
              List all options, with brief explanations.


       -n, -nx
              Do not execute commands from any `.gdbinit' initialization files.  Normally, the commands in these files  are  executed  after
              all the command options and arguments have been processed.

    //从指定文件读出符号列表
       -s FILE, -symbols=FILE
              Read symbol table from file file.


       -se=file
              Read symbol table from file file and use it as the executable file.


       q, -quiet
              ``Quiet''.  Do not print the introductory and copyright messages.  These messages are also suppressed in batch mode.

    
       -tty=device
               Run using device for your program's standard input and output.


       --args Pass arguments after the program name to the program when it is run.


       -tui   Run GDB using a text (console) user interface.

       -write Enable writing into executable and core files.

    //从文件中读取内容作为命令
       -x FILE, -command=FILE
              Execute GDB commands from file file.

SEE ALSO
       `gdb' entry in info; Using GDB: A Guide to the GNU Source-Level Debugger, Richard M. Stallman and Roland H. Pesch, July 1991.

COPYING
       Copyright (c) 1991, 2010 Free Software Foundation, Inc.

       Permission  is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice
       are preserved on all copies.

       Permission is granted to copy and distribute modified versions of this manual under the conditions  for  verbatim  copying,  provided
       that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.

       Permission  is granted to copy and distribute translations of this manual into another language, under the above conditions for modi‐
       fied versions, except that this permission notice may be included in translations approved by the Free Software Foundation instead of
       in the original English.



GNU Tools                                                         22may2002                                                           gdb(1)



在使用gdb前,必须先载入可执行文件,因为要进行调试,文件中就必须包含调试信息,所以在用gcc或cc编译时就需要用-g参数来打开程序的调试选项。

 

调试开始时,必须先载入要进行调试的程序,可以用以下两种方式:

 * 在启动gdb后执行以下命令:

   file 可执行文件路径

 * 在gdb启动时就载入程序:

   gdb 可执行文件路径

 

载入程序后,接下来就是要进行断点的设置,要监视的变量的添加等工作,下面对在这个过程中常会用到的命令逐一进行介绍:

 * list :显示程序中的代码,常用使用格式有:

    list

      输出从上次调用list命令开始往后的10行程序代码。

    list -

      输出从上次调用list命令开始往前的10行程序代码。

    list n

      输出第n行附近的10行程序代码。

    list function

      输出函数function前后的10行程序代码。

 * forward/search :从当前行向后查找匹配某个字符串的程序行。使用格式:

    forward/search 字符串

  查找到的行号将保存在$_变量中,可以用print $_命令来查看。

 * reverse-search :和forward/search相反,向前查找字符串。使用格式同上。

 * break :在程序中设置断点,当程序运行到指定行上时,会暂停执行。使用格式:

    break 要设置断点的行号

 * tbreak :设置临时断点,在设置之后只起作用一次。使用格式:

    tbreak 要设置临时断点的行号

 * clear :和break相反,clear用于清除断点。使用格式:

    clear 要清除的断点所在的行号

 * run :启动程序,在run后面带上参数可以传递给正在调试的程序。

 * awatch :用来增加一个观察点(add watch),使用格式:

    awatch 变量或表达式

  当表达式的值发生改变或表达式的值被读取时,程序就会停止运行。

 * watch :与awatch类似用来设置观察点,但程序只有当表达式的值发生改变时才会停止运行。使用格 式:

    watch 变量或表达式

  需要注意的是,awatch和watch都必须在程序运行的过程中设置观察点,即可运行run之后才能设置。

 * commands :设置在遇到断点后执行特定的指令。使用格式有:

    commands

      设置遇到最后一个遇到的断点时要执行的命令

    commands n

      设置遇到断点号n时要执行的命令

  注意,commands后面跟的是断点号,而不是断点所在的行号。

  在输入命令后,就可以输入遇到断点后要执行的命令,每行一条命令,在输入最后一条命令后输入end就可以结束输入。

 * delete :清除断点或自动显示的表达式。使用格式:

    delete 断点号

 * disable :让指定断点失效。使用格式:

    disable 断点号列表

  断点号之间用空格间隔开。

 * enable :和disable相反,恢复失效的断点。使用格式:

    enable 断点编号列表

 * ignore :忽略断点。使用格式:

    ignore 断点号 忽略次数

 * condition :设置断点在一定条件下才能生效。使用格式:

    condition 断点号 条件表达式

 * cont/continue :使程序在暂停在断点之后继续运行。使用格式:

    cont

      跳过当前断点继续运行。

    cont n

      跳过n次断点,继续运行。

  当n为1时,cont 1即为cont。

 * jump :让程序跳到指定行开始调试。使用格式:

    jump 行号

 * next :继续执行语句,但是跳过子程序的调用。使用格式:

    next

      执行一条语句

    next n

      执行n条语句

 * nexti :单步执行语句,但和next不同的是,它会跟踪到子程序的内部,但不打印出子程序内部的语句。使用格式同上。

 * step :与next类似,但是它会跟踪到子程序的内部,而且会显示子程序内部的执行情况。使用格式同上。

 * stepi :与step类似,但是比step更详细,是nexti和step的结合。使用格式同上。

 * whatis :显示某个变量或表达式的数据类型。使用格式:

    whatis 变量或表达式

 * ptype :和whatis类似,用于显示数据类型,但是它还可以显示typedef定义的类型等。使用格式:

    ptype 变量或表达式

 * set :设置程序中变量的值。使用格式:

    set 变量=表达式

    set 变量:=表达式

 * display :增加要显示值的表达式。使用格式:

    display 表达式

 * info display :显示当前所有的要显示值的表达式。

 * delete display/undisplay :删除要显示值的表达式。使用格式:

    delete display/undisplay 表达式编号

 * disable display :暂时不显示一个要表达式的值。使用格式:

    disable display 表达式编号

 * enable display :与disable display相反,使用表达式恢复显示。使用格式:

    enable display 表达式编号

 * print :打印变量或表达式的值。使用格式:

    print 变量或表达式

  表达式中有两个符号有特殊含义:$和$$。

  $表示给定序号的前一个序号,$$表示给定序号的前两个序号。

  如果$和$$后面不带数字,则给定序号为当前序号。

 * backtrace :打印指定个数的栈帧(stack frame)。使用格式:

    backtrace 栈帧个数

 * frame :打印栈帧。使用格式:

    frame 栈帧号

 * info frame :显示当前栈帧的详细信息。

 * select-frame :选择栈帧,选择后可以用info frame来显示栈帧信息。使用格式:

    select-frame 栈帧号

 * kill :结束当前程序的调试。

 * quit :退出gdb。

参考:http://www.cnblogs.com/evasnowind/archive/2010/11/07/1871324.html

你可能感兴趣的:(linux,unix,debug,ubuntu,gdb)