我怎么知道exit()函数是如何工作的?

时间:2012-04-18 07:09:56

标签: c linux

我写了一个小程序来查找exit()函数在Linux中是如何工作的。

#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编译程序,为什么断点会挂起,直到库加载到内存中?

3 个答案:

答案 0 :(得分: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.

“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).

答案 1 :(得分:4)

看起来你正试图在名为0x080495a3的函数上设置断点。而是尝试b *0x080495a3向GDB表明您想要在特定地址中断。

答案 2 :(得分:1)

0x080495a3是您愿意应用断点的行的地址。但是gdb的格式是b(函数名或行号)。所以你有2种方法可以做到这一点。

1)在gdb会话开始后执行l。它将列出C中的代码。然后使用行号

应用断点

2)如果要使用该地址,请使用b * 0x080495a3方式设置断点。

这样你就可以在行

停止了

0x080495a3&lt; + 3&gt;:mov 0x20(%esp),%eax