1. 程式人生 > 實用技巧 >GDB編輯、搜尋原始碼以及線上幫助

GDB編輯、搜尋原始碼以及線上幫助

GDB編輯、搜尋原始碼以及線上幫助

本節主要講解的是在 GDB 內對原始檔中的程式碼進行修改和查詢,分別對應 GDB 中的 edit 命令和 search 命令,下面是對這兩個命令的詳細介紹。

GDB edit命令:編輯檔案

在 GDB 中編輯原始檔中使用 edit 命令,該命令的語法格式如下:

(gdb) edit [location]
(gdb) edit [filename] : [location]

location 表示程式中的位置。這個命令表示啟用檔案的指定位置,然後進行編輯。示例:

(gdb) edit 16            //表示啟用檔案中的第16 行的程式碼(游標定位到第 16 行程式碼的開頭位置)
(gdb) edit func //表示啟用檔案中的 func 處的程式碼(游標定位到 func 函式所在行的開頭位置) (gdb) edit test.c : 16 //表示啟用 test.c 檔案的第16 行。

GDB edit 命令編輯檔案預設使用的是 ex 編譯器,使用的時候可能會遇見下面的情況:

(gdb) edit
bash: /bin/ex: 沒有那個檔案或目錄

遇到這種問題時,我們可以指定任意的編輯器(例如 Vim)去編輯檔案。進入 GDB 偵錯程式前,執行如下命令設定 EDITOR 環境變數:

export EDITOR=/usr/bin/vim

由此,當在 GDB 偵錯程式中執行 edit 命令時,就會自動進入 Vim 編輯器,從而對當前除錯的程式進行修改。
注意,上面修改編輯器的方法只是臨時生效,當退出 shell 終端後配置就會被還原,下次使用的時候還要再次使用這條命令。想要永久的實現配置,需要去修改配置檔案,具體做法是:修改當前 home 目錄下的”~/.bashrc”檔案,在檔案的最後新增上述的命令就可以實現檔案的永久配置(修改的是當前使用者的配置檔案,一般不建議修改系統的配置檔案,出現問題不容易恢復)。配置完成重啟 shell 終端,就可以完成配置。

GDB search命令:搜尋檔案

在除錯檔案時,某些時候可能會去找尋找某一行或者是某一部分的程式碼。可以使用 list 顯示全部的原始碼,然後進行檢視。當原始檔的程式碼量較少時,我們可以使用這種方式搜尋。如果原始檔的程式碼量很大,使用這種方式尋找效率會很低。所以 GDB 中提供了相關的原始碼搜尋的的 search 命令。
search 命令的語法格式為:

search <regexp>
reverse-search <regexp>

第一項命令格式表示從當前行的開始向前搜尋,後一項表示從當前行開始向後搜尋。其中 regexp 就是正則表示式,正則表示式描述了一種字串匹配的模式,可以用來檢查一個串中是否含有某種子串、將匹配的子串替換或者從某個串中取出符合某個條件的子串。很多的程式語言都支援使用正則表示式。
使用命令時可能會下面的情況:

(gdb) search func
Expression not found

表示搜尋的範圍沒有出現要尋找的字串或者定位到了程式碼行的末尾。

GDB help命令

為了降低使用者使用 GDB 偵錯程式的學習成本,GDB 提供了 help 命令,它可以幫使用者打印出目標命令的功能和具體用法。首先,為了方便使用者能夠快速地從眾多 GDB 命令中查詢到目標命令,help 命令根據不同 GDB 命令的功能對它們做了分類:

(gdb) help
List of classes of commands:

aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands

可以看到,根據各個 GDB 命令的不同功能,help 命令將它們分成了 12 大類,每一類中都包含多個功能類似的 GDB 命令。
以 breakpoints 類為例,該類中包含了 GDB 所有的斷點命令。藉助 help 命令,我們可以檢視某一類中具體包含的 GDB 命令:

(gdb) help breakpoints
Making program stop at certain points.

List of commands:

awatch -- Set a watchpoint for an expression
break -- Set breakpoint at specified location
break-range -- Set a breakpoint for an address range
catch -- Set catchpoints to catch events
catch assert -- Catch failed Ada assertions
catch catch -- Catch an exception
……

在此基礎上,通過 help 命令,我們可以檢視指定命令的功能和用法。例如:

(gdb) help break
Set breakpoint at specified line or function.
break [LOCATION] [thread THREADNUM] [if CONDITION]
LOCATION may be a line number, function name, or "*" and an address.
If a line number is specified, break at start of code for that line.
If a function is specified, break at start of code for that function.
If an address is specified, break at that exact address.
With no LOCATION, uses current execution address of the selected
stack frame.  This is useful for breaking on return to a stack frame.

THREADNUM is the number from "info threads".
CONDITION is a boolean expression.

Multiple breakpoints at one place are permitted, and useful if their
conditions are different.

Do "help breakpoints" for info on other commands dealing with breakpoints.

可以看到,我們嘗試藉助 help 命令檢視 break 命令,其列印資訊中包括 break 命令的具體功能、完整語法格式以及各個引數的具體含義。由此,只要系統學習過目標命令的讀者,藉助 help 命令打印出的提示資訊,一定可以回憶起來並切實用目標命令開始對程式進行除錯。

其他:GDB 還支援命令的自動補全。所謂自動補全,即在 (gdb) 右側輸入 GDB 命令時,對於特別長的命令,我們只需要輸入命令的前幾個字元,然後按下 Tab 鍵,GDB 就會自動幫我們補全整個命令。