2013-05-08 36 views
4

我正在嘗試開發一個簡單的基於文本的用戶界面,該界面運行一些gdb命令。 我希望用戶能夠在代碼的某個區域設置和斷開/跟蹤點並運行一些調試命令。查看/打印GDB中的功能代碼

我想讓用戶輸入需要調試的功能。然後我使用該函數名稱並打印該函數的源代碼,然後要求用戶選擇哪一行代碼來設置中斷/跟蹤點。目前,使用反彙編命令我可以打印出用戶的內存地址,但我想打印實際的源代碼。

這可以在gdb中完成嗎?

目前:

Dump of assembler code for function test_function: 
    0x03800f70 <test_function+0>: push %ebp 
    0x03800f71 <test_function+1>: mov %esp,%ebp 
    0x03800f73 <test_function+3>: sub $0x48,%esp 

我想要什麼:

void main() 
{ 
    printf("Hello World\n"); 
} 

謝謝!

編輯: 我得到這個:

(gdb) list myFunction 
941  directory/directory_etc/sourcefile.c: No such file or directory. 
     in directory/directory_etc/sourcefile.c 

然後我試圖指定LINENUM:

(gdb) list directory/directory_etc/sourcefile.c:941 
936  in directory/directory_etc/sourcefile.c 

所以行爲類似於你所描述的,但「清單文件名:LINENUM 「仍然沒有工作

謝謝!

+0

當前功能:http://stackoverflow.com/questions/12824251/gdb-list-source-of-current-function-without-typing-its-name – 2015-07-27 11:52:22

回答

6

用途:

(gdb) list FUNCTION 

list命令的詳細信息,在線幫助:

(gdb) help list 
List specified function or line. 
With no argument, lists ten more lines after or around previous listing. 
"list -" lists the ten lines before a previous ten-line listing. 
One argument specifies a line, and ten lines are listed around that line. 
Two arguments with comma between specify starting and ending lines to list. 
Lines can be specified in these ways: 
    LINENUM, to list around that line in current file, 
    FILE:LINENUM, to list around that line in that file, 
    FUNCTION, to list around beginning of that function, 
    FILE:FUNCTION, to distinguish among like-named static functions. 
    *ADDRESS, to list around the line containing that address. 
With two args if one is empty it stands for ten lines away from the other arg. 

對於任何非玩具項目,你可能會打的情況下,像這樣:

$ gdb /bin/true 
<...> 
(gdb) start 
<...> 
(gdb) list printf 
file: "/usr/include/bits/stdio2.h", line number: 104 
file: "printf.c", line number: 29 

其中列出了代碼庫中函數的多個定義。在上面的printf()的情況下,非重載純C函數有兩個定義。一個在stdio2.h中定義。然後,您可以使用list FILE:LINENUM形式來指定要列出哪一個:

(gdb) list printf.c:29 
24 
25 /* Write formatted output to stdout from the format string FORMAT. */ 
26 /* VARARGS1 */ 
27 int 
28 __printf (const char *format, ...) 
29 { 
30 va_list arg; 
31 int done; 
32 
33 va_start (arg, format); 

對於「sourcefile.c:沒有這樣的文件或目錄」你看到的錯誤,你需要告訴GDB哪裏找爲源代碼。見GDB Manual: Source Path。顯然你需要真正擁有你想要在你的機器上列出的函數的源代碼。

+0

謝謝,當我在一個簡單的文件上運行此命令( HelloWorld.exe)它沒有問題。但是,我正在嘗試使用gdb來調試大型實時系統。我要查看該功能在許多C文件,這些文件編譯成.elf文件之一,當我在那裏運行它,我得到:(GDB)列表功能 941目錄/ directory_etc/code.c – user2342775 2013-05-09 11:26:19

+0

@ user2342775 ,當gdb的list命令顯示一個函數名的幾個定義時,我已經修改了我的答案。這是你所看到的嗎? – scottt 2013-05-09 15:23:34

+0

我編輯了我原來的問題,試圖讓它更具可讀性,謝謝! – user2342775 2013-05-09 16:08:22