[英]View/Print function code from within 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
然后我嘗試指定亞麻布:
(gdb) list directory/directory_etc/sourcefile.c:941
936 in directory/directory_etc/sourcefile.c
所以行為類似於你所描述的,但“list filename:linenum”仍然無法正常工作
謝謝!
使用:
(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:No such file or directory”錯誤,您需要告訴GDB在哪里查找源代碼。 請參閱GDB手冊:源路徑 。 顯然,您需要實際擁有要在計算機上列出的功能的源代碼。
對於gcc :
使用源代碼的編譯添加調試選項標志-g:
gcc -g test.c
要測試,請使用:
gdb ./a.out
(gdb) list
有關更多功能,請查看手冊頁:
man gcc
man gdb
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.