GDB命令使用詳解

來源:互聯網
上載者:User

下面是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。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.