2012-04-18 53 views
2

我寫了一個小程序來查找exit()函數在Linux中的工作方式。如何知道exit()函數的工作方式?

#include <unistd.h> 

int main() 

{ 
    exit(0); 
} 

然後我用gcc編譯程序。

gcc -o example -g -static example.c 

在gdb中,當我設置一個斷點時,我得到了這些行。

Dump of assembler code for function exit: 
0x080495a0 <+0>: sub $0x1c,%esp 
0x080495a3 <+3>: mov 0x20(%esp),%eax 
0x080495a7 <+7>: movl $0x1,0x8(%esp) 
0x080495af <+15>: movl $0x80d602c,0x4(%esp) 
0x080495b7 <+23>: mov %eax,(%esp) 
0x080495ba <+26>: call 0x80494b0 <__run_exit_handlers> 
End of assembler dump. 

(gdb) b 0x080495a3 
Function "0x080495a3" not defined. 
Make breakpoint pending on future shared library load? (y or [n]) y 
Breakpoint 1 (0x080495a3) pending. 

(gdb) run 
Starting program: /home/jack/Documents/overflow/example 
[Inferior 1 (process 2299) exited normally] 

程序不停在斷點處。爲什麼?我使用-static來編譯程序,爲什麼斷點會一直等到庫加載到內存中?

+2

嘗試添加標誌-O0(大O零)到編輯。這會將優化設置爲最低,因此彙編代碼可能更易於閱讀。 – Mads 2012-04-18 07:14:19

回答

5

你在要求gdb打破一個名爲0x080495a3的函數。您需要改用b *0x080495a3

(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. 

由於幫助說,該*告訴GDB這是你想要打破上的地址。

從你的例子:

Function "0x080495a3" not defined. 
Make breakpoint pending on future shared library load? (y or [n]) y 
Breakpoint 1 (0x080495a3) pending. 

「待定」是指該斷點等待直到被叫0x080495a3函數是從共享庫加載。


您可能也有興趣break-range

(gdb) help break-range 
Set a breakpoint for an address range. 
break-range START-LOCATION, END-LOCATION 
where START-LOCATION and END-LOCATION can be one of the following: 
LINENUM, for that line in the current file, 
FILE:LINENUM, for that line in that file, 
+OFFSET, for that number of lines after the current line 
     or the start of the range 
FUNCTION, for the first line in that function, 
FILE:FUNCTION, to distinguish among like-named static functions. 
*ADDRESS, for the instruction at that address. 

The breakpoint will stop execution of the inferior whenever it executes 
an instruction at any address within the [START-LOCATION, END-LOCATION] 
range (including START-LOCATION and END-LOCATION). 
4

看起來您正試圖在名爲0x080495a3的函數中設置斷點。請嘗試b *0x080495a3向GDB表明您希望在特定地址處中斷。

1

0x080495a3是指您願意申請破發點線的地址。但gdb的格式是b(函數名稱或行號)。所以你有兩種方式來做到這一點。

1)在gdb會話啓動後執行l。它會在C中列出你的代碼。然後使用行號申請一箇中斷點。其他

2)如果你想使用地址,使用b * 0x080495a3方法來設置一箇中斷點。

這樣,您就能夠在線路停止

0x080495a3 < +3>:MOV 0×20(%ESP),%eax中