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();
}
58
1
2
3
4
5
6
7
8
9
void my_backtrace(void)
10
{
11
int j, nptrs;
12
void *buffer[BT_BUF_SIZE];
13
char **strings;
14
15
nptrs = backtrace(buffer, BT_BUF_SIZE);
16
printf("backtrace() returned %d addresses\n", nptrs);
17
18
/* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
19
would produce similar output to the following: */
20
21
strings = backtrace_symbols(buffer, nptrs);
22
if (strings == NULL) {
23
perror("backtrace_symbols");
24
exit(EXIT_FAILURE);
25
}
26
27
for (j = 0; j < nptrs; j++)
28
printf("%s\n", strings[j]);
29
30
free(strings);
31
}
32
33
unsigned long GetCurrentFp(void)
34
{
35
asm("ldr x0, [sp]");
36
}
37
38
void func()
39
{
40
int a=16;
41
42
my_backtrace();
43
44
printf("%s<%d> Fp=0x%lx \n",__FUNCTION__,__LINE__,GetCurrentFp());
45
printf("%s<%d> Fp-16=0x%lx \n",__FUNCTION__,__LINE__,(GetCurrentFp()+16));
46
}
47
48
void asmFunction()
49
{
50
int a = 10;
51
int b = 12;
52
func();
53
}
54
int main()
55
{
56
asmFunction();
57
}
58
2.编译
aarch64-linux-gnu-gcc test.c -funwind-tables -g -rdynamic
1
1
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
1
./a.out
2
backtrace() returned 5 addresses
3
./a.out(my_backtrace+0x2c) [0xaaaabb19ed50]
4
./a.out(func+0x14) [0xaaaabb19ee28]
5
./a.out(asmFunction+0x1c) [0xaaaabb19ee98]
6
./a.out(main+0xc) [0xaaaabb19eeb0]
7
/lib64/libc.so.6(__libc_start_main+0xe8) [0xffff913c927c]
8
func<44> Fp=0xfffff5deb4e0
9
func<45> Fp-16=0xfffff5deb4f0