2012-03-13 118 views
1

当使用gdb时,我经常会得到一个传递给函数的参数列表。然而,像bind某些功能,我没有得到的参数:如何从bind()获取函数参数?

(gdb) break bind 
Breakpoint 1 at 0x404b40 
(gdb) r 
... 
Breakpoint 1, bind() at ../sysdeps/unix/syscall-template.S:82 
82  in ../sysdeps/unix/syscall-template.S 
(gdb) bt 
#0 bind() at ../sysdeps/unix/syscall-template.S:82 
... 

如何我仍然得到参数传递给这些功能呢?

+0

你在使用什么操作系统? x64还是x86?答案取决于建筑。 – ks1322 2012-03-14 07:18:39

+0

x86_64,但我也对x86感兴趣。 – Lekensteyn 2012-03-14 11:30:30

回答

1

bind是套接字系统调用之一。有一种特殊的方法可以在gdb中加入系统调用断点 - catch syscall <syscall name>。在这种类型的断点之后,您可以根据kernel calling conventions来查看寄存器中的系统调用参数。对于x86_64,通过%rdi,%rsi,%rdx,%r10,%r8和%r9寄存器传递参数。对于x86-32 - 通过%ebx,%ecx,%edx,%esi,%edi,%ebp寄存器。

(gdb) catch syscall bind 
Catchpoint 3 (syscall 'bind' [49]) 
(gdb) r 
Starting program: /usr/bin/nmap google.com 

Starting Nmap 5.00 (http://nmap.org) at 2012-03-16 01:09 PDT 
Warning: Hostname google.com resolves to 6 IPs. Using 173.194.69.100. 

Catchpoint 3 (call to syscall 'bind'), 0x00007ffff6520307 in bind() 
    from /lib/libc.so.6 
(gdb) info registers 
rax   0xffffffffffffffda -38 
rbx   0xb35870 11753584 
rcx   0xffffffffffffffff -1 
rdx   0x14 20 
rsi   0x7fffffff7d90 140737488321936 
rdi   0x8 8 
rbp   0x8 0x8 
rsp   0x7fffffff7d58 0x7fffffff7d58 
r8    0xb 11 
r9    0x8000 32768 
r10   0x7fffffff7b00 140737488321280 
r11   0x202 514 
r12   0xb09630 11572784 
r13   0xb359f0 11753968 
r14   0x2 2 
r15   0xc8 200 
rip   0x7ffff6520307 0x7ffff6520307 <bind+7> 
eflags   0x202 [ IF ] 
cs    0x33 51 
ss    0x2b 43 
ds    0x0 0 
es    0x0 0 
fs    0x0 0 
---Type <return> to continue, or q <return> to quit--- 
gs    0x0 0 
(gdb) 

例如这里%rdi包含第一个bind调用参数 - 套接字文件描述符。

对于x86-32,由于套接字系统调用通过socketcall系统调用实现,所以事情更加复杂。这就是为什么不可能直接将0123点拨入bind。你可以找到更多关于它的信息here