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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <mach.h>
#include <ctype.h>
static int rtrace(uvlong, uvlong, uvlong);
static int ctrace(uvlong, uvlong, uvlong);
static int i386trace(uvlong, uvlong, uvlong);
static int amd64trace(uvlong, uvlong, uvlong);
static uvlong getval(uvlong);
static void inithdr(int);
static void fatal(char*, ...);
static void readstack(void);
static Fhdr fhdr;
static int interactive;
#define FRAMENAME ".frame"
static void
usage(void)
{
fprint(2, "usage: ktrace [-i] kernel pc sp [link]\n");
exits("usage");
}
static void
printaddr(char *addr, uvlong pc)
{
int i;
char *p;
/*
* reformat the following.
*
* foo+1a1 -> src(foo+0x1a1);
* 10101010 -> src(0x10101010);
*/
if(strlen(addr) == 8 && strchr(addr, '+') == nil){
for(i=0; i<8; i++)
if(!isxdigit(addr[i]))
break;
if(i == 8){
print("src(%#.8llux); // 0x%s\n", pc, addr);
return;
}
}
if(p=strchr(addr, '+')){
*p++ = 0;
print("src(%#.8llux); // %s+0x%s\n", pc, addr, p);
}else
print("src(%#.8llux); // %s\n", pc, addr);
}
static void (*fmt)(char*, uvlong) = printaddr;
void
main(int argc, char *argv[])
{
int (*t)(uvlong, uvlong, uvlong);
uvlong pc, sp, link;
int fd;
ARGBEGIN{
case 'i':
interactive++;
break;
default:
usage();
}ARGEND
link = 0;
t = ctrace;
switch(argc){
case 4:
t = rtrace;
link = strtoull(argv[3], 0, 16);
break;
case 3:
break;
default:
usage();
}
pc = strtoull(argv[1], 0, 16);
sp = strtoull(argv[2], 0, 16);
if(!interactive)
readstack();
fd = open(argv[0], OREAD);
if(fd < 0)
fatal("can't open %s: %r", argv[0]);
inithdr(fd);
switch(fhdr.magic){
case I_MAGIC: /* intel 386 */
t = i386trace;
break;
case S_MAGIC: /* amd64 */
t = amd64trace;
break;
case A_MAGIC: /* 68020 */
case J_MAGIC: /* intel 960 */
t = ctrace;
break;
case K_MAGIC: /* sparc */
case D_MAGIC: /* amd 29000 */
case V_MAGIC: /* mips 3000 */
case M_MAGIC: /* mips 4000 */
case E_MAGIC: /* arm 7-something */
case Q_MAGIC: /* powerpc */
case N_MAGIC: /* mips 4000 LE */
case L_MAGIC: /* dec alpha */
t = rtrace;
break;
case X_MAGIC: /* att dsp 3210 */
sysfatal("can't ktrace %s", argv[0]);
break;
default:
fprint(2, "%s: warning: can't tell what type of stack %s uses; assuming it's %s\n",
argv0, argv[0], argc == 4 ? "risc" : "cisc");
break;
}
(*t)(pc, sp, link);
exits(0);
}
static void
inithdr(int fd)
{
seek(fd, 0, 0);
if(!crackhdr(fd, &fhdr))
fatal("read text header");
if(syminit(fd, &fhdr) < 0)
fatal("%r\n");
}
static int
rtrace(uvlong pc, uvlong sp, uvlong link)
{
Symbol s, f;
char buf[128];
uvlong oldpc;
int i;
i = 0;
while(findsym(pc, CTEXT, &s)) {
if(pc == s.value) /* at first instruction */
f.value = 0;
else if(findlocal(&s, FRAMENAME, &f) == 0)
break;
symoff(buf, sizeof buf, pc, CANY);
fmt(buf, pc);
oldpc = pc;
if(s.type == 'L' || s.type == 'l' || pc <= s.value+mach->pcquant){
if(link == 0)
fprint(2, "%s: need to supply a valid link register\n", argv0);
pc = link;
}else{
pc = getval(sp);
if(pc == 0)
break;
}
if(pc == 0 || (pc == oldpc && f.value == 0))
break;
sp += f.value;
if(++i > 40)
break;
}
return i;
}
static int
ctrace(uvlong pc, uvlong sp, uvlong link)
{
Symbol s;
char buf[128];
int found;
uvlong opc, moved;
long j;
USED(link);
j = 0;
opc = 0;
while(pc && opc != pc) {
moved = pc2sp(pc);
if (moved == ~0){
print("pc2sp(%#.8llux) = -1 %r\n", pc);
break;
}
found = findsym(pc, CTEXT, &s);
if (!found){
print("findsym fails\n");
break;
}
symoff(buf, sizeof buf, pc, CANY);
fmt(buf, pc);
sp += moved;
opc = pc;
pc = getval(sp);
if(pc == 0)
break;
sp += mach->szaddr; /*assumes address size = stack width*/
if(++j > 40)
break;
}
return j;
}
static int
i386trace(uvlong pc, uvlong sp, uvlong link)
{
int i;
uvlong osp;
Symbol s, f;
char buf[128];
USED(link);
i = 0;
osp = 0;
while(findsym(pc, CTEXT, &s)) {
symoff(buf, sizeof buf, pc, CANY);
fmt(buf, pc);
//XXX s.value &= ~(uintptr)0;
if(pc != s.value) { /* not at first instruction */
if(findlocal(&s, FRAMENAME, &f) == 0)
break;
sp += f.value-mach->szaddr;
}else if(strcmp(s.name, "forkret") == 0){
//XXX
print("//passing interrupt frame; last pc found at sp=%#llux\n", osp);
sp += 15 * mach->szaddr; /* pop interrupt frame */
}
pc = getval(sp);
//XXX
if(pc == 0 && strcmp(s.name, "forkret") == 0){
sp += 3 * mach->szaddr; /* pop iret eip, cs, eflags */
print("//guessing call through invalid pointer, try again at sp=%#llux\n", sp);
s.name = "";
pc = getval(sp);
}
if(pc == 0) {
print("//didn't find pc at sp=%#llux, last pc found at sp=%#llux\n", sp, osp);
break;
}
osp = sp;
sp += mach->szaddr;
//XXX
if(strcmp(s.name, "forkret") == 0)
sp += 2 * mach->szaddr; /* pop iret cs, eflags */
if(++i > 40)
break;
}
return i;
}
static int
amd64trace(uvlong pc, uvlong sp, uvlong link)
{
int i, isintrr;
uvlong osp;
Symbol s, f;
char buf[128];
USED(link);
i = 0;
osp = 0;
while(findsym(pc, CTEXT, &s)) {
symoff(buf, sizeof buf, pc, CANY);
fmt(buf, pc);
if(strcmp(s.name, "_intrr") == 0)
isintrr = 1;
else
isintrr = 0;
if(pc != s.value) { /* not at first instruction */
if(findlocal(&s, FRAMENAME, &f) == 0)
break;
sp += f.value-mach->szaddr;
}
else if(isintrr){
print("//passing interrupt frame; last pc found at sp=%#llux\n", osp);
/*
* Pop interrupt frame (ureg.h) up to the IP value.
*/
sp += 19 * mach->szaddr;
}
pc = getval(sp);
if(pc == 0 && isintrr){
/*
* Pop IP, CS and FLAGS to get to the SP.
* The AMD64 aligns the interrupt stack on
* a 16-byte boundary so have the get the
* SP from the saved frame.
*/
sp += 3 * mach->szaddr;
print("//guessing call through invalid pointer; try again at sp=%#llux\n", sp);
s.name = "";
sp = getval(sp);
pc = getval(sp);
}
if(pc == 0) {
print("//didn't find pc at sp=%#llux, last pc found at sp=%#llux\n", sp, osp);
break;
}
osp = sp;
if(!isintrr)
sp += mach->szaddr;
if(++i > 40)
break;
}
return i;
}
int naddr;
uvlong addr[1024];
uvlong val[1024];
static void
putval(uvlong a, uvlong v)
{
if(naddr < nelem(addr)){
addr[naddr] = a;
val[naddr] = v;
naddr++;
}
}
static void
readstack(void)
{
Biobuf b;
char *p;
char *f[64];
int nf, i;
Binit(&b, 0, OREAD);
while(p=Brdline(&b, '\n')){
p[Blinelen(&b)-1] = 0;
nf = tokenize(p, f, nelem(f));
for(i=0; i<nf; i++){
if(p=strchr(f[i], '=')){
*p++ = 0;
putval(strtoull(f[i], 0, 16), strtoull(p, 0, 16));
}
}
}
}
static uvlong
getval(uvlong a)
{
char buf[256];
int i, n;
uvlong r;
if(interactive){
print("// data at %#8.8llux? ", a);
n = read(0, buf, sizeof(buf)-1);
if(n <= 0)
return 0;
buf[n] = '\0';
r = strtoull(buf, 0, 16);
}else{
r = 0;
for(i=0; i<naddr; i++)
if(addr[i] == a)
r = val[i];
}
return r;
}
static void
fatal(char *fmt, ...)
{
char buf[4096];
va_list arg;
va_start(arg, fmt);
vseprint(buf, buf+sizeof(buf), fmt, arg);
va_end(arg);
fprint(2, "ktrace: %s\n", buf);
exits(buf);
}
|