2012-03-13 122 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