我写了一个小程序来查找exit()函数在Linux中的工作原理.
#include <unistd.h>
int main()
{
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
然后我用gcc编译了程序.
gcc -o example -g -static example.c
Run Code Online (Sandbox Code Playgroud)
在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]
Run Code Online (Sandbox Code Playgroud)
该程序不会在断点处停止.为什么?我使用-static编译程序,为什么断点会挂起,直到库加载到内存中?
你要求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.
Run Code Online (Sandbox Code Playgroud)
正如帮助所说,*告诉gdb它是你要打破的地址.
从你的例子:
Function "0x080495a3" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (0x080495a3) pending.
Run Code Online (Sandbox Code Playgroud)
"挂起"意味着断点正在等待,直到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).
Run Code Online (Sandbox Code Playgroud)