2012-04-18 36 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中