用gdb獲得正確的調用棧

Sometimes GDB cannot parse the call stack correctly, and the call stack in gdb shows as:

(gdb) bt
0 0xb7f33410 in ?? ()
1 0xa89793a8 in ?? ()
2 0x00000002 in ?? ()
3 0x00000000 in ?? ()

 

since the call stack can be re-calculated by the stack frame, so if we can get the correct %ebp(frame pointer) reigster values, then we can get the call stack easily.

The ebp saves the current bottom of stack frame, and we know, when a function is invoked, the return address will be pushed into stack, and then the current ebp will be pushed, typical code as:

The code:

func(params);
means:
push eip;
jmp @func
push %ebp // save previous frame pointer
mov %esp, %ebp // save current frame pointer
...

 

So we can say, the pointer of %ebp is the last frame pointer, and the %ebp + 1 is the return address, then we can recursively get the address of each function call, and with "info symbol" to get each function's symbol information.

An example:

(gdb) bt

0 0xb7f33410 in ?? ()
1 0xa89793a8 in ?? ()
2 0x00000002 in ?? ()
3 0x00000000 in ?? ()
(gdb) x/10 $ebp
0xa89793a8:    0xa89793c8    0x08078734    0x0827e9e4    0x00000000
0xa89793b8:    0x00000000    0x00000000    0x00000000    0x00000000
0xa89793c8:    0xa89793e8    0x0806d1b1

(gdb) info symbol 0x08078734
IMSS::PsMutex::lock() + 18 in section .text

(gdb) x/10 0xa89793c8
0xa89793c8:    0xa89793e8    0x0806d1b1    0x0827e9e0    0x00000000
0xa89793d8:    0x00000000    0x00000000    0x00000000    0x0827e9e0
0xa89793e8:    0xa8979418    0x08077f3f

(gdb) info symbol 0x0806d1b1
IMSS::PsMutexLock::privateLock() + 35 in section .text

(gdb) x/10 0xa89793e8
0xa89793e8:    0xa8979418    0x08077f3f    0xa8979400    0x080a338c
0xa89793f8:    0xa8979418    0x08075d59    0x0827e9e0    0x00000001
0xa8979408:    0x00000001    0x00000000

(gdb) info symbol 0x08077f3f
SOAPRequestSocket::Accept(soap*&) + 41 in section .text

(gdb) x/10 0xa8979418
0xa8979418:    0xa8979438    0x08075c1c    0x0827e9e0    0x0829e8c8
0xa8979428:    0x00000019    0x00000000    0x00000000    0x0829e8b8
0xa8979438:    0xa8979458    0xb73dd797

(gdb) info symbol 0x08075c1c
PolicyRequestHandler::main() + 146 in section .text

 

Then we can get the call stack is:

IMSS::PsMutex::lock() + 18 in section .text

IMSS::PsMutexLock::privateLock() + 35 in section .text
SOAPRequestSocket::Accept(soap*&) + 41 in section .text
PolicyRequestHandler::main() + 146 in section .text

 

- END -

 

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/zoufeiyy/archive/2007/01/22/1490241.aspx

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章