ARM 64打印调用栈

1.代码
#include <stdio.h>
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define BT_BUF_SIZE 100

void my_backtrace(void)
{
   int j, nptrs;
   void *buffer[BT_BUF_SIZE];
   char **strings;

   nptrs = backtrace(buffer, BT_BUF_SIZE);
   printf("backtrace() returned %d addresses\n", nptrs);

   /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
	  would produce similar output to the following: */

   strings = backtrace_symbols(buffer, nptrs);
   if (strings == NULL) {
	   perror("backtrace_symbols");
	   exit(EXIT_FAILURE);
   }

   for (j = 0; j < nptrs; j++)
	   printf("%s\n", strings[j]);

   free(strings);
}

unsigned long GetCurrentFp(void) 
{
	asm("ldr x0, [sp]");
}

void func()
{
	int a=16;
	
	my_backtrace();
	
	printf("%s<%d> Fp=0x%lx \n",__FUNCTION__,__LINE__,GetCurrentFp());
	printf("%s<%d> Fp-16=0x%lx \n",__FUNCTION__,__LINE__,(GetCurrentFp()+16));
}

void asmFunction()
{
    int a = 10;
    int b = 12;
    func();
}
int main() 
{
    asmFunction();
}
2.编译
aarch64-linux-gnu-gcc test.c -funwind-tables -g -rdynamic
3.执行
./a.out 
backtrace() returned 5 addresses
./a.out(my_backtrace+0x2c) [0xaaaabb19ed50]
./a.out(func+0x14) [0xaaaabb19ee28]
./a.out(asmFunction+0x1c) [0xaaaabb19ee98]
./a.out(main+0xc) [0xaaaabb19eeb0]
/lib64/libc.so.6(__libc_start_main+0xe8) [0xffff913c927c]
func<44> Fp=0xfffff5deb4e0 
func<45> Fp-16=0xfffff5deb4f0 

发表评论

邮箱地址不会被公开。 必填项已用*标注