以一个简单求阶乘的代码为例:
1 #include <stdio.h> 2 3 unsigned int fact(unsigned int n) 4 { 5 if (n == 0) 6 return 1; 7 return n * fact(n - 1); 8 } 9 10 int main(void) 11 { 12 int c = 0; 13 14 c = fact(5); 15 16 return c; 17 }
1 void mbacktrace(int fps[], int n) 2 { 3 int *reg_fp = 0, *reg_fp_next = 0; 4 int cnt = 0, i; 5 6 __asm__ __volatile__( 7 "mov %%ebp, %0\t\n" 8 : "=m"(reg_fp) 9 ); 10 11 reg_fp_next = reg_fp; 12 for (i = 0; i < n && reg_fp_next != 0; i++) 13 { 14 reg_fp_next = (int *)((int)*reg_fp); // get ebp 15 fps[i] = (int)(*(reg_fp + 1)); // get eip, or ret_addr 16 reg_fp = reg_fp_next; 17 printf("stack_fp[%d] = 0x%08x.\n", i, stack_fp[i]); 18 } 19 }
时间: 2024-10-19 04:19:37