-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathksyms_subr.c
189 lines (154 loc) · 3.42 KB
/
ksyms_subr.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
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define _PATH_CAT "/bin/cat"
#define _PATH_NM "/usr/bin/nm"
#define _PATH_SORT "/usr/bin/sort"
struct symtbl {
const char *symbol;
uintptr_t addr;
};
static struct symtbl *symtbls;
static unsigned long symtbl_cur = 0;
static unsigned long symtbl_max = 0;
static struct symtbl *
symtbl_alloc(void)
{
struct symtbl *symtbl, *newtbls;
if (symtbl_cur >= symtbl_max) {
#define SYMTBL_NGROW (1024 * 512)
if (symtbl_max == 0) {
symtbl_max += SYMTBL_NGROW;
newtbls = malloc(sizeof(struct symtbl) * SYMTBL_NGROW);
} else {
symtbl_max += SYMTBL_NGROW;
newtbls = realloc(symtbls, sizeof(struct symtbl) * symtbl_max);
}
if (newtbls == NULL)
return NULL;
symtbls = newtbls;
}
symtbl = symtbls + symtbl_cur++;
return symtbl;
}
static int
symtbl_add(const char *symbol, const char *addrstr)
{
struct symtbl *symtbl;
const char *sym;
uintptr_t addr;
addr = strtoull(addrstr, NULL, 16);
symtbl = symtbl_alloc();
if (symtbl == NULL)
return 1;
symtbl->symbol = strdup(symbol);
symtbl->addr = addr;
return 0;
}
void
ksyms_dump(void)
{
struct symtbl *symtbl;
unsigned long i;
for (i = 0, symtbl = symtbls; i < symtbl_cur; i++, symtbl++) {
printf("%u: %lx %s\n", i, symtbl->addr, symtbl->symbol);
}
}
const char *
ksyms_lookupsym(uintptr_t addr)
{
struct symtbl *base, *sym;
unsigned long lim;
static char buf[1024];
if (addr == 0)
return "NULL";
base = symtbls;
for (lim = symtbl_cur; lim != 0; lim >>= 1) {
sym = base + (lim >> 1);
if (sym->addr == addr)
return sym->symbol;
if (sym->addr < addr) {
base = sym + 1;
lim--;
}
}
if ((sym > symtbls) && (sym->addr > addr))
sym--;
snprintf(buf, sizeof(buf), "%s+0x%llx", sym->symbol, addr - sym->addr);
return buf;
}
uintptr_t
ksyms_lookup(const char *name)
{
struct symtbl *tbl;
for (tbl = symtbls; tbl < symtbls + symtbl_cur; tbl++) {
if (strcmp(tbl->symbol, name) == 0) {
return tbl->addr;
}
}
return -1;
}
void
ksyms_destroy(void)
{
if (symtbls != NULL) {
free(symtbls);
symtbls = NULL;
}
}
int
ksyms_load(void)
{
FILE *fh;
char tmpfile1[512];
char tmpfile2[512];
char cmdline[512]; /* "/bin/cat /dev/ksyms > /tmp/$$.2; /usr/bin/nm /tmp/$$.2 | sort > $$.2" */
char linebuf[1024];
pid_t pid = getpid();
snprintf(tmpfile1, sizeof(tmpfile1), "/tmp/softintdump.tmp1.%u", pid);
snprintf(tmpfile2, sizeof(tmpfile2), "/tmp/softintdump.tmp2.%u", pid);
snprintf(cmdline, sizeof(cmdline), "%s %s > %s; %s %s | %s > %s", _PATH_CAT, _PATH_KSYMS, tmpfile1, _PATH_NM, tmpfile1, _PATH_SORT, tmpfile2);
/* XXX */
system(cmdline);
unlink(tmpfile1);
fh = fopen(tmpfile2, "r");
unlink(tmpfile2);
if (fh == NULL)
return -1;
while (fgets(linebuf, sizeof(linebuf), fh) != NULL) {
#define MAXPARAMS 8
char *p, *tokens[MAXPARAMS], *last;
int i;
/* chop */
i = strlen(linebuf);
if (i > 0)
linebuf[i - 1] = '\0';
for (i = 0, p = strtok_r(linebuf, " ", &last);
p != NULL;
(p = strtok_r(NULL, " ", &last)), i++) {
if (i < MAXPARAMS - 1)
tokens[i] = p;
}
tokens[i] = NULL;
if (tokens[2] != NULL) {
if (symtbl_add(tokens[2], tokens[0]) != 0) {
pclose(fh);
ksyms_destroy();
return -1;
}
}
}
pclose(fh);
return 0;
}
#if 0
int
main(int argc, char *argv[])
{
ksyms_load();
printf("%s\n", ksyms_lookupsym(0xffffffff80e38440ULL));
printf("0x%llx\n", ksyms_lookup("sigill_debug"));
}
#endif