-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest.c
More file actions
121 lines (108 loc) · 2.51 KB
/
Copy pathtest.c
File metadata and controls
121 lines (108 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include "addr2line.h"
int FuncA();
void FuncB(int argA, char argB);
int FuncC(int argA, float argB, double argC);
void dump()
{
int size = 0;
void *buf[256] = {0};
char **cbuf = NULL;
char **addresses = NULL;
size = backtrace(buf, sizeof(buf) / (sizeof(void *)));
if (!size)
{
return;
}
addresses = backtrace_symbols(buf, size);
if (addresses == NULL)
{
return;
}
char *file_name = (char *)malloc(sizeof(char) * 30);
get_this_file_name(file_name);
if (file_bfd_open(file_name, NULL) == error)
{
printf("bfd file open failed.");
free(addresses);
free(file_name);
return error;
}
unsigned int result = nonsense;
cbuf = (char **)malloc(size * sizeof(char *));
for (int i = 0; i < size; i++)
{
cbuf[i] = (char *)malloc(sizeof(char[8]));
sprintf(cbuf[i], "%x", (unsigned int)buf[i]);
result = translate_address(cbuf[i]);
printf("%s\n", addresses[i]);
if (result == error)
{
printf("translate address occur error!\n");
}
else if (result == no_found)
{
printf("Can't find the address.\n");
}
else if (result == nonsense)
{
printf("The address have found,but everything is null.So it's nonsense.");
}
else
{
if (result & file_flag)
{
printf("file name is %s\n", get_file_name());
}
if (result & function_flag)
{
printf("function name is %s\n", get_function_name());
}
if (result & line_flag)
{
printf("line is %u\n", get_line());
}
if (result & discriminator_flag)
{
printf("discriminator is %u\n", get_discriminator());
}
result = ok;
}
printf("\n");
}
for (int i = 0; i < size; i++)
{
free(cbuf[i]);
}
free(cbuf);
free(addresses);
free(file_name);
file_bfd_close();
addresses = NULL;
return;
}
int main()
{
FuncA();
printf("FuncA end.\n");
return 0;
}
int FuncA()
{
FuncB(0, 'c');
printf("FuncB end.\n");
return 0;
}
void FuncB(int argA, char argB)
{
FuncC(1, 3.14, 3.14);
printf("FuncC end.\n");
}
int FuncC(int argA, float argB, double argC)
{
// backtrace function
dump();
return 0;
}