(gdb) bt
#0 0x080486da in func_3 ()
#1 0x08048766 in func_int ()
#2 0x080487ae in func_str ()
#3 0x080487ff in main ()
前面數字式層次關係,#0表示最上面,即當前函數。除了第0層前面的地址表示是當前pc值,其他地址信息都表示函數調用的返回地址,例如上面:func_int() -->func_3() ,func_3執行完成後,接着會執行0x08048766地址的指令。
上面簡單介紹了一下Linux下面通過調用棧來定位問題,但調用棧的獲取原理,以及如何獲取,估計還是有些人會不知道的。之所以要介紹這個,因爲對於一些大型系統,完善的日誌功能是必不可少的,否則系統出了問題,沒有相關日誌,是非常痛苦的。尤其是在某些環境下,如電信領域,大多數是服務器或應用程序都是跑在單板上,出現問題了,不會像我們調試小程序那樣直接用gdb進行調試。雖然某些情況下可以使用gdb attach上出問題的進程,但大多數服務器單板沒有相關調試工具。所以要定位問題,基本上都是通過分析日誌。還有一種情況,就是那種隨機性問題,如果沒有日誌,那就更加痛苦了,就算你能夠使用gdb也無能爲力。所以log重要,但是log中通常需要記錄哪些信息呢?通常情況會保護函數調用出錯時,把傳入該函數的參數信息,或者一些關鍵全局變量信息,有些時候會記錄日期,對於服務器程序,日期一般都會記錄。另外還有一個也相對重要的就是調用棧信息。
所以下面來介紹一下獲取調用棧的原理和方法:
在Linux+x86環境,c語言函數調用時,下面介紹一下c函數是怎麼壓棧的:棧是從高地址向下低地址移動。通常一個函數中會有參數,局部變量等相關信息,這些信息是通過下面原則分配棧的:
1、棧的信息排布爲:先是局部變量存放,調用函數返回值存放,然後是調用其它函數參數函數,
- <pre name="code" class="cpp">如下面程序:
- int B(int c, int d)
- {
- return c+d;
- }
- int A(int a, int b)
- {
- int c = 0xff, d = 0xffff;
- return B(c, d);
- }
- 通過objdump -d 命令可以查看反彙編指令
- 反匯編出來後如下:
- 00000079 <B>:
- 79: 55 push %ebp
- 7a: 89 e5 mov %esp,%ebp
- 7c: 8b 45 0c mov 0xc(%ebp),%eax
- 7f: 03 45 08 add 0x8(%ebp),%eax
- 82: 5d pop %ebp
- 83: c3 ret
- 00000084 <A>:
- 84: 55 push %ebp
- 85: 89 e5 mov %esp,%ebp
- 87: 83 ec 18 sub $0x18,%esp
- 8a: c7 45 fc ff 00 00 00 movl $0xff,-0x4(%ebp)
- 91: c7 45 f8 ff ff 00 00 movl $0xffff,-0x8(%ebp)
- 98: 8b 45 f8 mov -0x8(%ebp),%eax
- 9b: 89 44 24 04 mov %eax,0x4(%esp)
- 9f: 8b 45 fc mov -0x4(%ebp),%eax
- a2: 89 04 24 mov %eax,(%esp)
- a5: e8 fc ff ff ff call a6 <A+0x22>
- aa: c9 leave
- ab: c3 ret
- 從上面反彙編可以看出,在A調用B時,A的調用棧佈局信息如下,
- 高地址: |---------|
- | ebp |<--| push %ebp -------------A-----------------
- |---------| |
- | c | | movl $0xff,-0x4(%ebp) ;A函數局部變量 c
- |---------| |
- | d | | movl $0xffff,-0x8(%ebp) ;A函數局部變量 d
- |---------| |
- | | |
- |---------| |
- | | |
- |---------| |
- c+%ebp| d | | mov %eax,0x4(%esp) ;A調用B函數時,準備好參數d
- |---------| |
- 8+%ebp| c | | mov %eax,(%esp) ;A調用B函數時,準備好參數c
- |---------| |<----%esp -------------A----------------
- 4+%ebp| retaddr | | A 調用B的返回地址,在執行call指令時,指令自動把call指令下一條壓入這個地方。
- |---------| |
- %ebp->| ebp |--- 對應於執行B函數 :push %ebp時,把在A函數運行時的ebp保存到該位置中。
- |---------|
- 低地址:</pre><br>
- <pre></pre>
- 後面B在執行mov 0xc(%ebp),%eax時,簡單用語言描述一下函數調用過程,就那上A調用B來說,首先A函數準備好參數,即把局部變量c,d放到棧上,然後執行call B(call a6 <A+0x22>)指令,call指令執行時默認會把當前指令的下一條指令壓入棧中,然後執行B函數第一條指令即(push %ebp),所以當執行到B函數push %ebp時,棧的信息就是上面那種樣子了。 知道一般程序是怎麼壓棧的,並且A函數調用B函數會把A函數中調用B函數的那條call指令的下一條指令壓棧棧中,通常情況一個函數第一條指令都是push
- %ebp, 功能是保存調用函數棧幀,第2條指令時mov %esp , %ebp,即把esp賦值給ebp,即初始化當前函數棧幀。 在執行過程中,函數調用首先指向call執行,然後執行被調用者第一條指令(push %ebp),c語言函數調用通常都是這樣情況的,而call指令又一個隱藏動作就是把下一指令(返回地址)壓棧。所以在棧裏面排布就是<pre name="code" class="cpp"> ---------
- | ret_addr|
- |---------|
- | ebp |
- |---------|
- 我們再看一下第二條指令,mov %esp , %ebp , 初始化當前函數棧幀。最終結果如下
- ---------
- | ret_addr| |
- |---------| |
- | ebp |---/
- |---------|<--|
- | ... | |
- |---------| |
- | ret_addr| |
- |---------| |
- | ebp |---/
- |---------|<--|
- | ... | |
- |---------| |
- | ret_addr| |
- |---------| |
- | ebp |---/
- |---------|---| </pre><br>
- <br>
- <br>
- 所以我們只要知道當前%epb的值,就可以通過上面那種圖示方法進行調用棧分析了。有人會問爲什麼libc有函數實現了,自己就沒有必要了,但libc只提供獲取當前線程的調用棧信息,有些時候需要獲取其他線程的調用棧信息,這個時候就需要自己分析實現了,總體思路一樣,只需要獲取到其它線程的%ebp信息即可,但通常情況在用戶態是不能夠獲取%ebp寄存器的,可以藉助內存模塊來實現。<br>
- <p>下面寫的一個小程序,一種方法使用libc庫裏面backtrace函數實現,還有一種就是自己通過分析調用棧信息來實現。</p>
- <p></p>
- <pre name="code" class="cpp">#include <stdio.h>
- #include <string.h>
- #include <execinfo.h>
- /* 獲取ebp寄存器值 */
- void get_ebp(unsigned long *ebp)
- {
- __asm__ __volatile__("mov %%ebp, %0 \r\n"
- :"=m"(*ebp)
- ::"memory");
- }
- int my_backtrace(void **stack, int size, unsigned long ebp)
- {
- int layer = 0;
- while(layer < size && ebp != 0 && *(unsigned long*)ebp != 0 && *(unsigned long *)ebp != ebp)
- {
- stack[layer++] = *(unsigned long *)(ebp+4);
- ebp = *(unsigned long*)ebp;
- }
- return layer;
- }
- int func_3(int a, int b, int c)
- {
- void *stack_addr[10];
- int layer;
- int i;
- char **ppstack_funcs;
- /* 通過調用libc函數實現 */
- layer = backtrace(stack_addr, 10);
- ppstack_funcs = backtrace_symbols(stack_addr, layer);
- for(i = 0; i < layer; i++)
- printf("\n%s:%p\n", ppstack_funcs[i], stack_addr[i]);
- /* 自己實現 */
- unsigned long ebp = 0;
- get_ebp(&ebp);
- memset(stack_addr, 0, sizeof(stack_addr));
- layer = my_backtrace(stack_addr, 10, ebp);
- for(i = 0; i < layer; i++)
- printf("\nmy: %p\n", stack_addr[i]);
- free(ppstack_funcs);
- return 3;
- }
- int func_int(int a, int b, int c, int d)
- {
- int aa,bb,cc;
- int ret= func_3(aa,bb,cc);
- return (a+ b+ c+ d + ret);
- }
- int func_str()
- {
- int a = 1, b = 2;
- int ret;
- ret = func_int(a, a, b, b);
- return ret;
- }
- int B(int c, int d)
- {
- return c+d;
- }
- int A(int a, int b)
- {
- int c = 0xff, d = 0xffff;
- return B(c, d);
- }
- int main(int argc, char *argv[])
- {
- int ret = func_str();
- return 0;
- }
- </pre><pre name="code" class="cpp">程序編譯加上-rdynaminc</pre><pre name="code" class="cpp">否則獲取調用棧只有地址,沒有函數名信息。</pre><pre name="code" class="cpp">運行結果:</pre><pre name="code" class="cpp"><pre name="code" class="cpp">./exe() [0x80484dd]:0x80484dd
- ./exe() [0x80485ea]:0x80485ea
- ./exe() [0x8048632]:0x8048632
- ./exe() [0x8048683]:0x8048683
- /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6) [0xb7dd5bd6]:0xb7dd5bd6
- ./exe() [0x8048401]:0x8048401
- my: 0x804858a
- my: 0x80485ea
- my: 0x8048632
- my: 0x8048683
- my: 0xb7dd5bd6
- </pre><br>
- <pre></pre>
- <p></p>
- <p></p>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
-
</pre>