dump.c revision 1.7 1 /* $NetBSD: dump.c,v 1.7 2000/01/26 14:23:41 sommerfeld Exp $ */
2
3 /*-
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)kdump.c 8.4 (Berkeley) 4/28/95";
45 #endif
46 __RCSID("$NetBSD: dump.c,v 1.7 2000/01/26 14:23:41 sommerfeld Exp $");
47 #endif /* not lint */
48
49 #include <sys/param.h>
50 #define _KERNEL
51 #include <sys/errno.h>
52 #undef _KERNEL
53 #include <sys/time.h>
54 #include <sys/uio.h>
55 #include <sys/ktrace.h>
56 #include <sys/ioctl.h>
57 #include <sys/ptrace.h>
58 #define _KERNEL
59 #include <sys/errno.h>
60 #undef _KERNEL
61
62 #include <err.h>
63 #include <signal.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68 #include <vis.h>
69
70 #include "ktrace.h"
71 #include "misc.h"
72
73 int timestamp, decimal, fancy = 1, tail, maxdata;
74
75 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
76
77 #include <sys/syscall.h>
78
79 #include "../../sys/compat/hpux/hpux_syscall.h"
80 #include "../../sys/compat/ibcs2/ibcs2_syscall.h"
81 #include "../../sys/compat/linux/linux_syscall.h"
82 #include "../../sys/compat/osf1/osf1_syscall.h"
83 #include "../../sys/compat/sunos/sunos_syscall.h"
84 #include "../../sys/compat/svr4/svr4_syscall.h"
85 #include "../../sys/compat/ultrix/ultrix_syscall.h"
86
87 #define KTRACE
88 #include "../../sys/kern/syscalls.c"
89
90 #include "../../sys/compat/hpux/hpux_syscalls.c"
91 #include "../../sys/compat/ibcs2/ibcs2_syscalls.c"
92 #include "../../sys/compat/linux/linux_syscalls.c"
93 #include "../../sys/compat/osf1/osf1_syscalls.c"
94 #include "../../sys/compat/sunos/sunos_syscalls.c"
95 #include "../../sys/compat/svr4/svr4_syscalls.c"
96 #include "../../sys/compat/ultrix/ultrix_syscalls.c"
97 #undef KTRACE
98
99 struct emulation {
100 char *name; /* Emulation name */
101 char **sysnames; /* Array of system call names */
102 int nsysnames; /* Number of */
103 };
104
105 static struct emulation emulations[] = {
106 { "netbsd", syscallnames, SYS_MAXSYSCALL },
107 { "hpux", hpux_syscallnames, HPUX_SYS_MAXSYSCALL },
108 { "ibcs2", ibcs2_syscallnames, IBCS2_SYS_MAXSYSCALL },
109 { "linux", linux_syscallnames, LINUX_SYS_MAXSYSCALL },
110 { "osf1", osf1_syscallnames, OSF1_SYS_MAXSYSCALL },
111 { "sunos", sunos_syscallnames, SUNOS_SYS_MAXSYSCALL },
112 { "svr4", svr4_syscallnames, SVR4_SYS_MAXSYSCALL },
113 { "ultrix", ultrix_syscallnames, ULTRIX_SYS_MAXSYSCALL },
114 { NULL, NULL, NULL }
115 };
116
117 struct emulation *current = &emulations[0]; /* NetBSD */
118
119
120 static char *ptrace_ops[] = {
121 "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U",
122 "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE",
123 "PT_KILL", "PT_ATTACH", "PT_DETACH",
124 };
125
126
127 void dumprecord __P((struct ktr_header *, int, int *, void **, FILE *));
128 void dumpheader __P((struct ktr_header *, char *, int, int *));
129 void dumpfile __P((char *, int, int));
130 int fread_tail __P((char *, int, int, FILE *));
131 void ioctldecode __P((u_long));
132 int ktrsyscall __P((struct ktr_syscall *, int, char *, int, int *));
133 void ktrsysret __P((struct ktr_sysret *, char *, int, int *));
134 void ktrnamei __P((char *, int, char *, int, int *));
135 void ktremul __P((char *, int, char *, int, int *));
136 void ktrgenio __P((struct ktr_genio *, int, char *, int, int *));
137 void ktrpsig __P((struct ktr_psig *));
138 void ktrcsw __P((struct ktr_csw *));
139 void setemul __P((char *));
140
141 #define KTR_BUFSZ 512
142 #define BLEFT (bufsz - (bp - buff))
143
144
145 void
146 dumprecord(ktr, trpoints, sizep, mp, fp)
147 register struct ktr_header *ktr;
148 int trpoints;
149 int *sizep;
150 void **mp;
151 FILE *fp;
152 {
153 static void *mcopy = NULL;
154 static int linelen = 0, iolinelen = 0;
155 char buff[KTR_BUFSZ], iobuff[KTR_BUFSZ], *bp;
156 int ktrlen, *lenp;
157 void *m;
158
159 if (!ktr) {
160 printf("%s\n", buff);
161 if (*iobuff)
162 printf("%s\n", iobuff);
163 return;
164 }
165
166 if (ktr->ktr_type == KTR_GENIO || ktr->ktr_type == KTR_EMUL) {
167 bp = iobuff;
168 lenp = &iolinelen;
169 } else {
170 bp = buff;
171 lenp = &linelen;
172 }
173 if (!mcopy && (trpoints & (1<<ktr->ktr_type)))
174 dumpheader(ktr, bp, KTR_BUFSZ, lenp);
175
176 if ((ktrlen = ktr->ktr_len) < 0)
177 errx(1, "bogus length 0x%x", ktrlen);
178 m = *mp;
179 if (ktrlen >= *sizep) {
180 *mp = m = (void *)realloc(m, *sizep = ktrlen+1);
181 if (m == NULL)
182 errx(1, "%s", strerror(ENOMEM));
183 }
184 if (ktrlen && fread_tail(m, ktrlen, 1, fp) == 0)
185 errx(1, "data too short");
186 if ((trpoints & (1<<ktr->ktr_type)) == 0)
187 return;
188 switch (ktr->ktr_type)
189 {
190 case KTR_SYSCALL:
191 if (ktrsyscall((struct ktr_syscall *)m, 0, bp, KTR_BUFSZ,
192 lenp) == 0) {
193 mcopy = (void *)malloc(ktrlen + 1);
194 bcopy(m, mcopy, ktrlen);
195 return;
196 }
197 break;
198 case KTR_SYSRET:
199 ktrsysret((struct ktr_sysret *)m, bp, KTR_BUFSZ, lenp);
200 if (*iobuff || iolinelen) {
201 fputs(iobuff, stdout);
202 *iobuff = '\0';
203 iolinelen = 0;
204 }
205 break;
206 case KTR_NAMEI:
207 ktrnamei(m, ktrlen, bp, sizeof(buff), lenp);
208 if (mcopy) {
209 (void) ktrsyscall((struct ktr_syscall *)mcopy, 1, bp,
210 KTR_BUFSZ, lenp);
211 free(mcopy);
212 mcopy = NULL;
213 }
214 break;
215 case KTR_GENIO:
216 ktrgenio((struct ktr_genio *)m, ktrlen, bp, KTR_BUFSZ, lenp);
217 break;
218 case KTR_PSIG:
219 ktrpsig((struct ktr_psig *)m);
220 break;
221 case KTR_CSW:
222 ktrcsw((struct ktr_csw *)m);
223 break;
224 case KTR_EMUL:
225 ktremul(m, ktrlen, bp, sizeof(buff), lenp);
226 break;
227 }
228
229 if (mcopy) {
230 free(mcopy);
231 mcopy = NULL;
232 }
233 }
234
235 void
236 dumpfile(file, fd, trpoints)
237 char *file;
238 int fd;
239 int trpoints;
240 {
241 struct ktr_header ktr_header;
242 void *m;
243 FILE *fp;
244 int size;
245
246 m = (void *)malloc(size = 1025);
247 if (m == NULL)
248 errx(1, "%s", strerror(ENOMEM));
249 if (!file || !*file) {
250 if (!(fp = fdopen(fd, "r")))
251 err(1, "fdopen(%d)", fd);
252 } else if (!strcmp(file, "-"))
253 fp = stdin;
254 else if (!(fp = fopen(file, "r")))
255 err(1, "%s", file);
256
257 while (fread_tail((char *)&ktr_header,sizeof(struct ktr_header),1,fp)) {
258 dumprecord(&ktr_header, trpoints, &size, &m, fp);
259 if (tail)
260 (void)fflush(stdout);
261 }
262 dumprecord(NULL, 0, NULL, NULL, fp);
263 }
264
265
266 int
267 fread_tail(buf, size, num, fp)
268 char *buf;
269 int num, size;
270 FILE *fp;
271 {
272 int i;
273
274 while ((i = fread(buf, size, num, fp)) == 0 && tail) {
275 (void)sleep(1);
276 clearerr(fp);
277 }
278 return (i);
279 }
280
281 void
282 dumpheader(kth, buff, buffsz, lenp)
283 struct ktr_header *kth;
284 char *buff;
285 int buffsz, *lenp;
286 {
287 static struct timeval prevtime;
288 char *bp = buff + *lenp;
289 struct timeval temp;
290
291 if (kth->ktr_type == KTR_SYSRET || kth->ktr_type == KTR_GENIO)
292 return;
293 *lenp = 0;
294 (void)snprintf(bp, buffsz - *lenp, "%6d %-8.*s ",
295 kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
296 *lenp += strlen(bp);
297 bp = buff + *lenp;
298
299 if (timestamp) {
300 if (timestamp == 2) {
301 timersub(&kth->ktr_time, &prevtime, &temp);
302 prevtime = kth->ktr_time;
303 } else
304 temp = kth->ktr_time;
305 (void)snprintf(bp, buffsz - *lenp, "%ld.%06ld ", temp.tv_sec,
306 temp.tv_usec);
307 *lenp += strlen(bp);
308 }
309 }
310
311 void
312 ioctldecode(cmd)
313 u_long cmd;
314 {
315 char dirbuf[4], *dir = dirbuf;
316
317 if (cmd & IOC_OUT)
318 *dir++ = 'W';
319 if (cmd & IOC_IN)
320 *dir++ = 'R';
321 *dir = '\0';
322
323 printf(decimal ? ",_IO%s('%c',%ld" : ",_IO%s('%c',%#lx",
324 dirbuf, (cmd >> 8) & 0xff, cmd & 0xff);
325 if ((cmd & IOC_VOID) == 0)
326 printf(decimal ? ",%ld)" : ",%#lx)", (cmd >> 16) & 0xff);
327 else
328 printf(")");
329 }
330
331 int
332 ktrsyscall(ktr, nohdr, buff, bufsz, lenp)
333 register struct ktr_syscall *ktr;
334 int nohdr, bufsz, *lenp;
335 char *buff;
336 {
337 register int argsize = ktr->ktr_argsize;
338 register register_t *ap;
339 char *bp = buff;
340 int eol = 1;
341
342 if (*lenp < bufsz) {
343 bp += *lenp;
344 bzero(bp, BLEFT);
345 }
346 if (!nohdr) {
347 if (ktr->ktr_code >= current->nsysnames || ktr->ktr_code < 0)
348 (void)snprintf(bp, BLEFT, "[%d]", ktr->ktr_code);
349 else
350 (void)snprintf(bp, BLEFT,
351 "%s", current->sysnames[ktr->ktr_code]);
352 bp += strlen(bp);
353 }
354 ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
355 if (argsize) {
356 char *s = "(";
357 if (fancy && !nohdr) {
358 switch (ktr->ktr_code) {
359 /*
360 * All these have a path as the first param.
361 */
362 case SYS_open : case SYS_chdir :
363 case SYS___stat13 : case SYS_chroot :
364 case SYS_execve : case SYS_pathconf :
365 case SYS_rmdir : case SYS_rename :
366 case SYS_symlink : case SYS_chflags :
367 case SYS_link : case SYS_mkdir :
368 case SYS_mknod : case SYS_mkfifo :
369 if (BLEFT > 1)
370 *bp++ = '(';
371 eol = 0;
372 break;
373 case SYS___sigaction14 :
374 (void)snprintf(bp, BLEFT, "(%s",
375 signals[(int)*ap].name);
376 s = ", ";
377 argsize -= sizeof(register_t);
378 ap++;
379 break;
380 case SYS_ioctl :
381 if (decimal)
382 (void)snprintf(bp, BLEFT, "(%ld",
383 (long)*ap);
384 else
385 (void)snprintf(bp, BLEFT, "(%#lx",
386 (long)*ap);
387 bp += strlen(bp);
388 ap++;
389 argsize -= sizeof(register_t);
390 if ((s = ioctlname(*ap)) != NULL)
391 (void)snprintf(bp, BLEFT, ", %s", s);
392 else
393 ioctldecode(*ap);
394 s = ", ";
395 ap++;
396 argsize -= sizeof(register_t);
397 break;
398 case SYS_ptrace :
399 if (*ap >= 0 && *ap <=
400 sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
401 (void)snprintf(bp, BLEFT, "(%s",
402 ptrace_ops[*ap]);
403 else
404 (void)snprintf(bp, BLEFT, "(%ld",
405 (long)*ap);
406 s = ", ";
407 ap++;
408 argsize -= sizeof(register_t);
409 break;
410 default :
411 break;
412 }
413 bp += strlen(bp);
414 }
415 if (eol) {
416 while (argsize) {
417 if (!nohdr || strcmp(s, "(")) {
418 if (decimal)
419 (void)snprintf(bp, BLEFT,
420 "%s%ld", s,
421 (long)*ap);
422 else
423 (void)snprintf(bp, BLEFT,
424 "%s%#lx", s,
425 (long)*ap);
426 bp += strlen(bp);
427 }
428 s = ", ";
429 ap++;
430 argsize -= sizeof(register_t);
431 }
432 if (BLEFT > 1)
433 *bp++ = ')';
434 }
435 }
436 *bp = '\0';
437
438 *lenp = bp - buff;
439 return eol;
440 }
441
442 void
443 ktrsysret(ktr, buff, buffsz, lenp)
444 struct ktr_sysret *ktr;
445 int buffsz, *lenp;
446 char *buff;
447 {
448 register register_t ret = ktr->ktr_retval;
449 register int error = ktr->ktr_error;
450
451 while (*lenp < 50)
452 buff[(*lenp)++] = ' ';
453 if (error == EJUSTRETURN)
454 strcpy(buff + *lenp, " JUSTRETURN");
455 else if (error == ERESTART)
456 strcpy(buff + *lenp, " RESTART");
457 else if (error) {
458 sprintf(buff + *lenp, " Err#%d", error);
459 if (error < MAXERRNOS && error >= -2)
460 sprintf(buff + strlen(buff), " %s",errnos[error].name);
461 } else
462 sprintf(buff + *lenp, " = %ld", (long)ret);
463 strcat(buff + *lenp, "\n");
464 *lenp = 0;
465 fputs(buff, stdout);
466 *buff = '\0';
467 }
468
469 void
470 ktrnamei(cp, len, buff, buffsz, lenp)
471 int buffsz, *lenp;
472 char *cp, *buff;
473 {
474 snprintf(buff + *lenp, buffsz - *lenp, "\"%.*s\"", len, cp);
475 *lenp += strlen(buff + *lenp);
476 }
477
478 void
479 ktremul(cp, len, buff, buffsz, lenp)
480 int buffsz, *lenp;
481 char *cp, *buff;
482 {
483 bzero(buff + *lenp, buffsz - *lenp);
484 cp[len] = '\0';
485 snprintf(buff + *lenp, buffsz - *lenp, "emul(%s)\n", cp);
486 *lenp += strlen(buff + *lenp);
487
488 setemul(cp);
489 }
490
491 void
492 ktrgenio(ktr, len, buff, bufsz, lenp)
493 struct ktr_genio *ktr;
494 int len;
495 char *buff;
496 int bufsz, *lenp;
497 {
498 static int screenwidth = 0;
499 register int datalen = len - sizeof (struct ktr_genio);
500 register char *dp = (char *)ktr + sizeof (struct ktr_genio);
501 register int col = 0;
502 register int width;
503 char visbuf[5], *bp = buff;
504
505 if (*lenp < bufsz) {
506 bp += *lenp;
507 bzero(buff, BLEFT);
508 } else
509 *lenp = 0;
510 if (screenwidth == 0) {
511 struct winsize ws;
512
513 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
514 ws.ws_col > 8)
515 screenwidth = ws.ws_col;
516 else
517 screenwidth = 80;
518 }
519
520 if (maxdata && datalen > maxdata)
521 datalen = maxdata;
522 strcpy(bp, " \"");
523 col = *lenp;
524 col += 8;
525 bp += 8;
526 for (; datalen > 0; datalen--, dp++) {
527 (void) vis(visbuf, *dp, VIS_NL|VIS_TAB|VIS_CSTYLE, *(dp+1));
528 width = strlen(visbuf);
529 visbuf[4] = '\0';
530 if (col + width + 2 >= screenwidth)
531 break;
532 col += width;
533 strncpy(bp, visbuf, width);
534 bp += width;
535 if (col + 2 >= screenwidth)
536 break;
537 }
538 strcpy(bp, "\"\n");
539 *lenp = col + 2;
540 }
541
542 void
543 ktrpsig(psig)
544 struct ktr_psig *psig;
545 {
546 (void)printf("SIG%s ", sys_signame[psig->signo]);
547 if (psig->action == SIG_DFL)
548 (void)printf("SIG_DFL\n");
549 else {
550 (void)printf("caught handler=0x%lx mask=0x%lx code=0x%x\n",
551 (u_long)psig->action, (unsigned long)psig->mask.__bits[0],
552 psig->code);
553 }
554 }
555
556 void
557 ktrcsw(cs)
558 struct ktr_csw *cs;
559 {
560 (void)printf("%s %s\n", cs->out ? "stop" : "resume",
561 cs->user ? "user" : "kernel");
562 }
563
564 void
565 setemul(name)
566 char *name;
567 {
568 int i;
569 for (i = 0; emulations[i].name != NULL; i++)
570 if (strcmp(emulations[i].name, name) == 0) {
571 current = &emulations[i];
572 return;
573 }
574 warnx("Emulation `%s' unknown", name);
575 }
576