2

我编写了一个小程序来了解 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 编译程序,为什么断点会挂起,直到库加载到内存中?

4

3 回答 3

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.

正如帮助所说, The*告诉 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).
于 2012-04-18T07:12:43.340 回答
4

看起来您正试图在名为0x080495a3. 而是尝试b *0x080495a3向 GDB 指示您要在特定地址处中断。

于 2012-04-18T07:12:29.583 回答
1

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

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

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

这样你就可以在线停止

0x080495a3 <+3>: 移动 0x20(%esp),%eax

于 2012-04-18T07:33:00.630 回答