-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem_coord.c
210 lines (195 loc) · 4.06 KB
/
mem_coord.c
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include <limits.h>
#include <errno.h>
#ifdef HP_UX
#include <dl.h>
#endif
#include "mem_coord.h"
static int i_have_addr2line = 0;
static char prog_path[PATH_MAX];
static void print_not_found(void)
{
fprintf(stderr, "Could not find addr2line.\n"
"Check http://www.gnu.org/software/binutils/\n"
"Coordinate(file, line) will not printed.\n");
return;
}
static void get_coord_line(char *line, char **coord)
{
char *p = line;
while (*line) {
if (*line == '/')
p = line;
line++;
}
p++;
*coord = malloc(strlen(p) + 1);
if (*coord == NULL) {
fprintf(stderr, "malloc() failed. %d %s\n",
errno, strerror(errno));
exit(1);
}
strcpy(*coord, p);
return;
}
static void get_func_line(char *line, char **func)
{
*func = malloc(strlen(line) + 1);
if (*func == NULL) {
fprintf(stderr, "malloc() failed. %d %s\n",
errno, strerror(errno));
exit(1);
}
strcpy(*func, line);
return;
}
int get_exec_path(char *exec_path, int len)
{
int ret;
#ifdef LINUX
ret = readlink("/proc/self/exe", exec_path, len);
if (ret < 0) {
fprintf(stderr, "readlink() failed. %d %s\n",
errno, strerror(errno));
return -1;
}
exec_path[len - 1] = '\0';
return 0;
#elif SUNOS
const char *cmdp;
char cwd[BUFSIZ], *cwdp;
char *homep = getenv("HOME");
cmdp = getexecname();
if (cmdp != NULL) {
if (homep != NULL) {
if (strstr(cmdp, homep) == NULL) {
cwdp = getcwd(cwd, sizeof(cwd));
if (cwdp == NULL) {
fprintf(stderr,
"getcwd() failed. %d %s\n",
errno, strerror(errno));
return -1;
}
cwd[sizeof(cwd) - 1] = '\0';
snprintf(exec_path, len,
"%s/%s", cwd, cmdp);
} else {
strncpy(exec_path, cmdp, len);
exec_path[len - 1] = '\0';
}
} else {
fprintf(stderr, "warning: couldn't get $HOME\n");
snprintf(exec_path, len,
"./%s\n", cmdp);
}
} else {
fprintf(stderr, "getexecname() failed. %d %s\n",
errno, strerror(errno));
return -1;
}
return 0;
#elif HP_UX
struct shl_descriptor desc;
ret = shl_get_r(0, &desc);
if (ret < 0) {
fprintf(stderr, "shl_get_r() failed. %d %s\n",
errno, strerror(errno));
return -1;
}
strncpy(exec_path, desc.filename, len);
exec_path[len - 1] = '\0';
return 0;
#else
return -1;
#endif
}
void check_addr2line(void)
{
static const char *cmd = "which addr2line";
FILE *pp;
char line[BUFSIZ];
prog_path[0] = '\0';
pp = popen(cmd, "r");
if (pp == NULL) {
print_not_found();
} else {
while (fgets(line, sizeof(line), pp)) {
line[strlen(line) - 1] = '\0';
if (strstr(line, "addr2line")) {
strncpy(prog_path, line, sizeof(prog_path));
prog_path[sizeof(prog_path) - 1] = '\0';
}
}
if (prog_path[0] != '\0') {
i_have_addr2line = 1;
fprintf(stderr, "> %s ..ok\n", prog_path);
} else {
print_not_found();
}
}
pclose(pp);
return;
}
/*
* IN@addr: address
* IN@exec: executable path
* OUT@func: function
* OUT@coord: coordnate (file:line)
*/
#define DEFAULT_LEN 8
void get_coordinate(unsigned long addr, const char *exec,
char **func, char **coord)
{
char cmd[BUFSIZ];
char line[BUFSIZ];
FILE *pp;
int len;
*func = NULL;
*coord = NULL;
if (i_have_addr2line) {
snprintf(cmd, sizeof(cmd),
"%s %#lx -f -e %s", prog_path, addr, exec);
pp = popen(cmd, "r");
if (pp != NULL) {
while (fgets(line, sizeof(line), pp)) {
line[strlen(line) - 1] = '\0';
if ((strstr(line, "addr2line") != NULL) ||
(strstr(line, "??") != NULL)) {
pclose(pp);
goto dontknow;
}
if (strstr(line, ":") != NULL) {
get_coord_line(line, coord);
} else {
get_func_line(line, func);
}
}
pclose(pp);
return;
} else {
goto dontknow;
}
} else {
dontknow:
*func = malloc(DEFAULT_LEN);
if (*func != NULL) {
strcpy(*func, "??");
} else {
fprintf(stderr, "malloc() failed. %d %s\n",
errno, strerror(errno));
exit(1);
}
*coord = malloc(DEFAULT_LEN);
if (*coord != NULL) {
strcpy(*coord, "??:0");
} else {
fprintf(stderr, "malloc() failed. %d %s\n",
errno, strerror(errno));
exit(1);
}
return;
}
}