程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 用gdb跟蹤函數棧桢的變化情況

用gdb跟蹤函數棧桢的變化情況

編輯:關於C語言
 

代碼如下:
#include <stdio.h>

void hello()
{
int i = 0;

printf("i = %d, hello world\n", i);
}

int main()
{
hello();

return 0;
}
gdb生成調試信息,跟進去看看。

在調用hello之前,在main函數內查看寄存器情況時,打印如下:
(gdb) info registers
eax 0xbff644a4 -1074379612
ecx 0xbff64420 -1074379744
edx 0x1 1
ebx 0xb7f2fff4 -1208811532
esp 0xbff64400 0xbff64400
ebp 0xbff64408 0xbff64408
esi 0x8048420 134513696
edi 0x8048310 134513424
eip 0x80483f7 0x80483f7 <main+17>
eflags 0x286 [ PF SF IF ]
cs 0x73 115
ss 0x7b 123
ds 0x7b 123
es 0x7b 123
fs 0x0 0
gs 0x33 51

在進入hello函數之後,查看寄存器情況,打印如下:
(gdb) info registers
eax 0xbff644a4 -1074379612
ecx 0xbff64420 -1074379744
edx 0x1 1
ebx 0xb7f2fff4 -1208811532
esp 0xbff643e0 0xbff643e0
ebp 0xbff643f8 0xbff643f8
esi 0x8048420 134513696
edi 0x8048310 134513424
eip 0x80483ca 0x80483ca <hello+6>
eflags 0x282 [ SF IF ]
cs 0x73 115
ss 0x7b 123
ds 0x7b 123
es 0x7b 123
fs 0x0 0
gs 0x33 51

根據兩個棧桢中寄存器的數據,看看變化前的esp - 變化後的ebp,得到以下結果:
(gdb) print 0xbff64400 - 0xbff643f8
$1 = 8

再看看 在hello之中*(ebp), *(ebp + 4)的數據:
(gdb) x 0xbff643f8
0xbff643f8: 0xbff64408
(gdb) x 0xbff643f8+4
0xbff643fc: 0x080483fc
其中, 第一次打印的結果0xbff64408是main棧桢中ebp寄存器的數據。
而反匯編main函數的結果如下:
(gdb) disassemble main
Dump of assembler code for function main:
0x080483e6 <main+0>: lea 0x4(%esp),%ecx
0x080483ea <main+4>: and $0xfffffff0,%esp
0x080483ed <main+7>: pushl -0x4(%ecx)
0x080483f0 <main+10>: push %ebp
0x080483f1 <main+11>: mov %esp,%ebp
0x080483f3 <main+13>: push %ecx
0x080483f4 <main+14>: sub $0x4,%esp
0x080483f7 <main+17>: call 0x80483c4 <hello>
0x080483fc <main+22>: mov $0x0,%eax
0x08048401 <main+27>: add $0x4,%esp
0x08048404 <main+30>: pop %ecx
0x08048405 <main+31>: pop %ebp
0x08048406 <main+32>: lea -0x4(%ecx),%esp
0x08048409 <main+35>: ret
End of assembler dump.  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved