]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/kdump/kdump.c
This commit was generated by cvs2svn to compensate for changes in r175261,
[FreeBSD/FreeBSD.git] / usr.bin / kdump / kdump.c
1 /*-
2  * Copyright (c) 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1988, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)kdump.c     8.1 (Berkeley) 6/6/93";
43 #endif
44 #endif /* not lint */
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47
48 #define _KERNEL
49 extern int errno;
50 #include <sys/errno.h>
51 #undef _KERNEL
52 #include <sys/param.h>
53 #include <sys/errno.h>
54 #define _KERNEL
55 #include <sys/time.h>
56 #undef _KERNEL
57 #include <sys/uio.h>
58 #include <sys/ktrace.h>
59 #include <sys/ioctl.h>
60 #include <sys/socket.h>
61 #include <dlfcn.h>
62 #include <err.h>
63 #include <locale.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68 #include <vis.h>
69 #include "ktrace.h"
70 #include "kdump_subr.h"
71
72 int fread_tail(void *, int, int);
73 void dumpheader(struct ktr_header *);
74 void ktrsyscall(struct ktr_syscall *);
75 void ktrsysret(struct ktr_sysret *);
76 void ktrnamei(char *, int);
77 void hexdump(char *, int, int);
78 void visdump(char *, int, int);
79 void ktrgenio(struct ktr_genio *, int);
80 void ktrpsig(struct ktr_psig *);
81 void ktrcsw(struct ktr_csw *);
82 void ktruser(int, unsigned char *);
83 void usage(void);
84 const char *ioctlname(u_long);
85
86 int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata;
87 const char *tracefile = DEF_TRACEFILE;
88 struct ktr_header ktr_header;
89
90 #define eqs(s1, s2)     (strcmp((s1), (s2)) == 0)
91
92 int
93 main(int argc, char *argv[])
94 {
95         int ch, ktrlen, size;
96         void *m;
97         int trpoints = ALL_POINTS;
98         int drop_logged;
99         pid_t pid = 0;
100
101         (void) setlocale(LC_CTYPE, "");
102
103         while ((ch = getopt(argc,argv,"f:dElm:np:HRsTt:")) != -1)
104                 switch((char)ch) {
105                 case 'f':
106                         tracefile = optarg;
107                         break;
108                 case 'd':
109                         decimal = 1;
110                         break;
111                 case 'l':
112                         tail = 1;
113                         break;
114                 case 'm':
115                         maxdata = atoi(optarg);
116                         break;
117                 case 'n':
118                         fancy = 0;
119                         break;
120                 case 'p':
121                         pid = atoi(optarg);
122                         break;
123                 case 's':
124                         suppressdata = 1;
125                         break;
126                 case 'E':
127                         timestamp = 3;  /* elapsed timestamp */
128                         break;
129                 case 'H':
130                         threads = 1;
131                         break;
132                 case 'R':
133                         timestamp = 2;  /* relative timestamp */
134                         break;
135                 case 'T':
136                         timestamp = 1;
137                         break;
138                 case 't':
139                         trpoints = getpoints(optarg);
140                         if (trpoints < 0)
141                                 errx(1, "unknown trace point in %s", optarg);
142                         break;
143                 default:
144                         usage();
145                 }
146
147         if (argc > optind)
148                 usage();
149
150         m = (void *)malloc(size = 1025);
151         if (m == NULL)
152                 errx(1, "%s", strerror(ENOMEM));
153         if (!freopen(tracefile, "r", stdin))
154                 err(1, "%s", tracefile);
155         drop_logged = 0;
156         while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
157                 if (ktr_header.ktr_type & KTR_DROP) {
158                         ktr_header.ktr_type &= ~KTR_DROP;
159                         if (!drop_logged && threads) {
160                                 (void)printf("%6d %6d %-8.*s Events dropped.\n",
161                                     ktr_header.ktr_pid, ktr_header.ktr_tid >
162                                     0 ? ktr_header.ktr_tid : 0, MAXCOMLEN,
163                                     ktr_header.ktr_comm);
164                                 drop_logged = 1;
165                         } else if (!drop_logged) {
166                                 (void)printf("%6d %-8.*s Events dropped.\n",
167                                     ktr_header.ktr_pid, MAXCOMLEN,
168                                     ktr_header.ktr_comm);
169                                 drop_logged = 1;
170                         }
171                 }
172                 if (trpoints & (1<<ktr_header.ktr_type))
173                         if (pid == 0 || ktr_header.ktr_pid == pid)
174                                 dumpheader(&ktr_header);
175                 if ((ktrlen = ktr_header.ktr_len) < 0)
176                         errx(1, "bogus length 0x%x", ktrlen);
177                 if (ktrlen > size) {
178                         m = (void *)realloc(m, ktrlen+1);
179                         if (m == NULL)
180                                 errx(1, "%s", strerror(ENOMEM));
181                         size = ktrlen;
182                 }
183                 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
184                         errx(1, "data too short");
185                 if (pid && ktr_header.ktr_pid != pid)
186                         continue;
187                 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
188                         continue;
189                 drop_logged = 0;
190                 switch (ktr_header.ktr_type) {
191                 case KTR_SYSCALL:
192                         ktrsyscall((struct ktr_syscall *)m);
193                         break;
194                 case KTR_SYSRET:
195                         ktrsysret((struct ktr_sysret *)m);
196                         break;
197                 case KTR_NAMEI:
198                         ktrnamei(m, ktrlen);
199                         break;
200                 case KTR_GENIO:
201                         ktrgenio((struct ktr_genio *)m, ktrlen);
202                         break;
203                 case KTR_PSIG:
204                         ktrpsig((struct ktr_psig *)m);
205                         break;
206                 case KTR_CSW:
207                         ktrcsw((struct ktr_csw *)m);
208                         break;
209                 case KTR_USER:
210                         ktruser(ktrlen, m);
211                         break;
212                 default:
213                         printf("\n");
214                         break;
215                 }
216                 if (tail)
217                         (void)fflush(stdout);
218         }
219         return 0;
220 }
221
222 int
223 fread_tail(void *buf, int size, int num)
224 {
225         int i;
226
227         while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
228                 (void)sleep(1);
229                 clearerr(stdin);
230         }
231         return (i);
232 }
233
234 void
235 dumpheader(struct ktr_header *kth)
236 {
237         static char unknown[64];
238         static struct timeval prevtime, temp;
239         const char *type;
240
241         switch (kth->ktr_type) {
242         case KTR_SYSCALL:
243                 type = "CALL";
244                 break;
245         case KTR_SYSRET:
246                 type = "RET ";
247                 break;
248         case KTR_NAMEI:
249                 type = "NAMI";
250                 break;
251         case KTR_GENIO:
252                 type = "GIO ";
253                 break;
254         case KTR_PSIG:
255                 type = "PSIG";
256                 break;
257         case KTR_CSW:
258                 type = "CSW ";
259                 break;
260         case KTR_USER:
261                 type = "USER";
262                 break;
263         default:
264                 (void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
265                 type = unknown;
266         }
267
268         /*
269          * The ktr_tid field was previously the ktr_buffer field, which held
270          * the kernel pointer value for the buffer associated with data
271          * following the record header.  It now holds a threadid, but only
272          * for trace files after the change.  Older trace files still contain
273          * kernel pointers.  Detect this and suppress the results by printing
274          * negative tid's as 0.
275          */
276         if (threads)
277                 (void)printf("%6d %6d %-8.*s ", kth->ktr_pid, kth->ktr_tid >
278                     0 ? kth->ktr_tid : 0, MAXCOMLEN, kth->ktr_comm);
279         else
280                 (void)printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN,
281                     kth->ktr_comm);
282         if (timestamp) {
283                 if (timestamp == 3) {
284                         if (prevtime.tv_sec == 0)
285                                 prevtime = kth->ktr_time;
286                         timevalsub(&kth->ktr_time, &prevtime);
287                 }
288                 if (timestamp == 2) {
289                         temp = kth->ktr_time;
290                         timevalsub(&kth->ktr_time, &prevtime);
291                         prevtime = temp;
292                 }
293                 (void)printf("%ld.%06ld ",
294                     kth->ktr_time.tv_sec, kth->ktr_time.tv_usec);
295         }
296         (void)printf("%s  ", type);
297 }
298
299 #include <sys/syscall.h>
300 #define KTRACE
301 #include <sys/kern/syscalls.c>
302 #undef KTRACE
303 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
304
305 void
306 ktrsyscall(struct ktr_syscall *ktr)
307 {
308         int narg = ktr->ktr_narg;
309         register_t *ip;
310
311         if (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0)
312                 (void)printf("[%d]", ktr->ktr_code);
313         else
314                 (void)printf("%s", syscallnames[ktr->ktr_code]);
315         ip = &ktr->ktr_args[0];
316         if (narg) {
317                 char c = '(';
318                 if (fancy) {
319
320 #define print_number(i,n,c) do {                      \
321         if (decimal)                                  \
322                 (void)printf("%c%ld", c, (long)*i);   \
323         else                                          \
324                 (void)printf("%c%#lx", c, (long)*i);  \
325         i++;                                          \
326         n--;                                          \
327         c = ',';                                      \
328         } while (0);
329
330                         if (ktr->ktr_code == SYS_ioctl) {
331                                 const char *cp;
332                                 print_number(ip,narg,c);
333                                 if ((cp = ioctlname(*ip)) != NULL)
334                                         (void)printf(",%s", cp);
335                                 else {
336                                         if (decimal)
337                                                 (void)printf(",%ld", (long)*ip);
338                                         else
339                                                 (void)printf(",%#lx ", (long)*ip);
340                                 }
341                                 c = ',';
342                                 ip++;
343                                 narg--;
344                         } else if (ktr->ktr_code == SYS_ptrace) {
345                                 (void)putchar('(');
346                                 ptraceopname ((int)*ip);
347                                 c = ',';
348                                 ip++;
349                                 narg--;
350                         } else if (ktr->ktr_code == SYS_access ||
351                                    ktr->ktr_code == SYS_eaccess) {
352                                 print_number(ip,narg,c);
353                                 (void)putchar(',');
354                                 accessmodename ((int)*ip);
355                                 ip++;
356                                 narg--;
357                         } else if (ktr->ktr_code == SYS_open) {
358                                 int     flags;
359                                 int     mode;
360                                 print_number(ip,narg,c);
361                                 flags = *ip;
362                                 mode = *++ip;
363                                 (void)putchar(',');
364                                 flagsandmodename (flags, mode, decimal);
365                                 ip++;
366                                 narg-=2;
367                         } else if (ktr->ktr_code == SYS_wait4) {
368                                 print_number(ip,narg,c);
369                                 print_number(ip,narg,c);
370                                 (void)putchar(',');
371                                 wait4optname ((int)*ip);
372                                 ip++;
373                                 narg--;
374                         } else if (ktr->ktr_code == SYS_chmod ||
375                                    ktr->ktr_code == SYS_fchmod ||
376                                    ktr->ktr_code == SYS_lchmod) {
377                                 print_number(ip,narg,c);
378                                 (void)putchar(',');
379                                 modename ((int)*ip);
380                                 ip++;
381                                 narg--;
382                         } else if (ktr->ktr_code == SYS_mknod) {
383                                 print_number(ip,narg,c);
384                                 (void)putchar(',');
385                                 modename ((int)*ip);
386                                 ip++;
387                                 narg--;
388                         } else if (ktr->ktr_code == SYS_getfsstat) {
389                                 print_number(ip,narg,c);
390                                 print_number(ip,narg,c);
391                                 (void)putchar(',');
392                                 getfsstatflagsname ((int)*ip);
393                                 ip++;
394                                 narg--;
395                         } else if (ktr->ktr_code == SYS_mount) {
396                                 print_number(ip,narg,c);
397                                 print_number(ip,narg,c);
398                                 (void)putchar(',');
399                                 mountflagsname ((int)*ip);
400                                 ip++;
401                                 narg--;
402                         } else if (ktr->ktr_code == SYS_unmount) {
403                                 print_number(ip,narg,c);
404                                 (void)putchar(',');
405                                 mountflagsname ((int)*ip);
406                                 ip++;
407                                 narg--;
408                         } else if (ktr->ktr_code == SYS_recvmsg ||
409                                    ktr->ktr_code == SYS_sendmsg) {
410                                 print_number(ip,narg,c);
411                                 print_number(ip,narg,c);
412                                 (void)putchar(',');
413                                 sendrecvflagsname ((int)*ip);
414                                 ip++;
415                                 narg--;
416                         } else if (ktr->ktr_code == SYS_recvfrom ||
417                                    ktr->ktr_code == SYS_sendto) {
418                                 print_number(ip,narg,c);
419                                 print_number(ip,narg,c);
420                                 print_number(ip,narg,c);
421                                 (void)putchar(',');
422                                 sendrecvflagsname ((int)*ip);
423                                 ip++;
424                                 narg--;
425                         } else if (ktr->ktr_code == SYS_chflags ||
426                                    ktr->ktr_code == SYS_fchflags ||
427                                    ktr->ktr_code == SYS_lchflags) {
428                                 print_number(ip,narg,c);
429                                 (void)putchar(',');
430                                 modename((int)*ip);
431                                 ip++;
432                                 narg--;
433                         } else if (ktr->ktr_code == SYS_kill) {
434                                 print_number(ip,narg,c);
435                                 (void)putchar(',');
436                                 signame((int)*ip);
437                                 ip++;
438                                 narg--;
439                         } else if (ktr->ktr_code == SYS_reboot) {
440                                 (void)putchar('(');
441                                 rebootoptname((int)*ip);
442                                 ip++;
443                                 narg--;
444                         } else if (ktr->ktr_code == SYS_umask) {
445                                 (void)putchar('(');
446                                 modename((int)*ip);
447                                 ip++;
448                                 narg--;
449                         } else if (ktr->ktr_code == SYS_msync) {
450                                 print_number(ip,narg,c);
451                                 print_number(ip,narg,c);
452                                 (void)putchar(',');
453                                 msyncflagsname((int)*ip);
454                                 ip++;
455                                 narg--;
456 #ifdef SYS_freebsd6_mmap
457                         } else if (ktr->ktr_code == SYS_freebsd6_mmap) {
458                                 print_number(ip,narg,c);
459                                 print_number(ip,narg,c);
460                                 (void)putchar(',');
461                                 mmapprotname ((int)*ip);
462                                 (void)putchar(',');
463                                 ip++;
464                                 narg--;
465                                 mmapflagsname ((int)*ip);
466                                 ip++;
467                                 narg--;
468 #endif
469                         } else if (ktr->ktr_code == SYS_mmap) {
470                                 print_number(ip,narg,c);
471                                 print_number(ip,narg,c);
472                                 (void)putchar(',');
473                                 mmapprotname ((int)*ip);
474                                 (void)putchar(',');
475                                 ip++;
476                                 narg--;
477                                 mmapflagsname ((int)*ip);
478                                 ip++;
479                                 narg--;
480                         } else if (ktr->ktr_code == SYS_mprotect) {
481                                 print_number(ip,narg,c);
482                                 print_number(ip,narg,c);
483                                 (void)putchar(',');
484                                 mmapprotname ((int)*ip);
485                                 ip++;
486                                 narg--;
487                         } else if (ktr->ktr_code == SYS_madvise) {
488                                 print_number(ip,narg,c);
489                                 print_number(ip,narg,c);
490                                 (void)putchar(',');
491                                 madvisebehavname((int)*ip);
492                                 ip++;
493                                 narg--;
494                         } else if (ktr->ktr_code == SYS_setpriority) {
495                                 print_number(ip,narg,c);
496                                 print_number(ip,narg,c);
497                                 (void)putchar(',');
498                                 prioname((int)*ip);
499                                 ip++;
500                                 narg--;
501                         } else if (ktr->ktr_code == SYS_fcntl) {
502                                 int cmd;
503                                 int arg;
504                                 print_number(ip,narg,c);
505                                 cmd = *ip;
506                                 arg = *++ip;
507                                 (void)putchar(',');
508                                 fcntlcmdname(cmd, arg, decimal);
509                                 ip++;
510                                 narg-=2;
511                         } else if (ktr->ktr_code == SYS_socket) {
512                                 int sockdomain;
513                                 (void)putchar('(');
514                                 sockdomain=(int)*ip;
515                                 sockdomainname(sockdomain);
516                                 ip++;
517                                 narg--;
518                                 (void)putchar(',');
519                                 socktypename((int)*ip);
520                                 ip++;
521                                 narg--;
522                                 if (sockdomain == PF_INET ||
523                                     sockdomain == PF_INET6) {
524                                         (void)putchar(',');
525                                         sockipprotoname((int)*ip);
526                                         ip++;
527                                         narg--;
528                                 }
529                                 c = ',';
530                         } else if (ktr->ktr_code == SYS_setsockopt ||
531                                    ktr->ktr_code == SYS_getsockopt) {
532                                 print_number(ip,narg,c);
533                                 (void)putchar(',');
534                                 sockoptlevelname((int)*ip, decimal);
535                                 if ((int)*ip == SOL_SOCKET) {
536                                         ip++;
537                                         narg--;
538                                         (void)putchar(',');
539                                         sockoptname((int)*ip);
540                                 }
541                                 ip++;
542                                 narg--;
543 #ifdef SYS_freebsd6_lseek
544                         } else if (ktr->ktr_code == SYS_freebsd6_lseek) {
545                                 print_number(ip,narg,c);
546                                 /* Hidden 'pad' argument, not in lseek(2) */
547                                 print_number(ip,narg,c);
548                                 print_number(ip,narg,c);
549                                 (void)putchar(',');
550                                 whencename ((int)*ip);
551                                 ip++;
552                                 narg--;
553 #endif
554                         } else if (ktr->ktr_code == SYS_lseek) {
555                                 print_number(ip,narg,c);
556                                 /* Hidden 'pad' argument, not in lseek(2) */
557                                 print_number(ip,narg,c);
558                                 (void)putchar(',');
559                                 whencename ((int)*ip);
560                                 ip++;
561                                 narg--;
562
563                         } else if (ktr->ktr_code == SYS_flock) {
564                                 print_number(ip,narg,c);
565                                 (void)putchar(',');
566                                 flockname((int)*ip);
567                                 ip++;
568                                 narg--;
569                         } else if (ktr->ktr_code == SYS_mkfifo ||
570                                    ktr->ktr_code == SYS_mkdir) {
571                                 print_number(ip,narg,c);
572                                 (void)putchar(',');
573                                 modename((int)*ip);
574                                 ip++;
575                                 narg--;
576                         } else if (ktr->ktr_code == SYS_shutdown) {
577                                 print_number(ip,narg,c);
578                                 (void)putchar(',');
579                                 shutdownhowname((int)*ip);
580                                 ip++;
581                                 narg--;
582                         } else if (ktr->ktr_code == SYS_socketpair) {
583                                 (void)putchar('(');
584                                 sockdomainname((int)*ip);
585                                 ip++;
586                                 narg--;
587                                 (void)putchar(',');
588                                 socktypename((int)*ip);
589                                 ip++;
590                                 narg--;
591                                 c = ',';
592                         } else if (ktr->ktr_code == SYS_getrlimit ||
593                                    ktr->ktr_code == SYS_setrlimit) {
594                                 (void)putchar('(');
595                                 rlimitname((int)*ip);
596                                 ip++;
597                                 narg--;
598                                 c = ',';
599                         } else if (ktr->ktr_code == SYS_quotactl) {
600                                 print_number(ip,narg,c);
601                                 (void)putchar(',');
602                                 quotactlname((int)*ip);
603                                 ip++;
604                                 narg--;
605                                 c = ',';
606                         } else if (ktr->ktr_code == SYS_nfssvc) {
607                                 (void)putchar('(');
608                                 nfssvcname((int)*ip);
609                                 ip++;
610                                 narg--;
611                                 c = ',';
612                         } else if (ktr->ktr_code == SYS_rtprio) {
613                                 (void)putchar('(');
614                                 rtprioname((int)*ip);
615                                 ip++;
616                                 narg--;
617                                 c = ',';
618                         } else if (ktr->ktr_code == SYS___semctl) {
619                                 print_number(ip,narg,c);
620                                 print_number(ip,narg,c);
621                                 (void)putchar(',');
622                                 semctlname((int)*ip);
623                                 ip++;
624                                 narg--;
625                         } else if (ktr->ktr_code == SYS_semget) {
626                                 print_number(ip,narg,c);
627                                 print_number(ip,narg,c);
628                                 (void)putchar(',');
629                                 semgetname((int)*ip);
630                                 ip++;
631                                 narg--;
632                         } else if (ktr->ktr_code == SYS_msgctl) {
633                                 print_number(ip,narg,c);
634                                 (void)putchar(',');
635                                 shmctlname((int)*ip);
636                                 ip++;
637                                 narg--;
638                         } else if (ktr->ktr_code == SYS_shmat) {
639                                 print_number(ip,narg,c);
640                                 print_number(ip,narg,c);
641                                 (void)putchar(',');
642                                 shmatname((int)*ip);
643                                 ip++;
644                                 narg--;
645                         } else if (ktr->ktr_code == SYS_shmctl) {
646                                 print_number(ip,narg,c);
647                                 (void)putchar(',');
648                                 shmctlname((int)*ip);
649                                 ip++;
650                                 narg--;
651                         } else if (ktr->ktr_code == SYS_minherit) {
652                                 print_number(ip,narg,c);
653                                 print_number(ip,narg,c);
654                                 (void)putchar(',');
655                                 minheritname((int)*ip);
656                                 ip++;
657                                 narg--;
658                         } else if (ktr->ktr_code == SYS_rfork) {
659                                 (void)putchar('(');
660                                 rforkname((int)*ip);
661                                 ip++;
662                                 narg--;
663                                 c = ',';
664                         } else if (ktr->ktr_code == SYS_lio_listio) {
665                                 (void)putchar('(');
666                                 lio_listioname((int)*ip);
667                                 ip++;
668                                 narg--;
669                                 c = ',';
670                         } else if (ktr->ktr_code == SYS_mlockall) {
671                                 (void)putchar('(');
672                                 mlockallname((int)*ip);
673                                 ip++;
674                                 narg--;
675                         } else if (ktr->ktr_code == SYS_sched_setscheduler) {
676                                 print_number(ip,narg,c);
677                                 (void)putchar(',');
678                                 schedpolicyname((int)*ip);
679                                 ip++;
680                                 narg--;
681                         } else if (ktr->ktr_code == SYS_sched_get_priority_max ||
682                                    ktr->ktr_code == SYS_sched_get_priority_min) {
683                                 (void)putchar('(');
684                                 schedpolicyname((int)*ip);
685                                 ip++;
686                                 narg--;
687                         } else if (ktr->ktr_code == SYS_sendfile) {
688                                 print_number(ip,narg,c);
689                                 print_number(ip,narg,c);
690                                 print_number(ip,narg,c);
691                                 print_number(ip,narg,c);
692                                 print_number(ip,narg,c);
693                                 print_number(ip,narg,c);
694                                 (void)putchar(',');
695                                 sendfileflagsname((int)*ip);
696                                 ip++;
697                                 narg--;
698                         } else if (ktr->ktr_code == SYS_kldsym) {
699                                 print_number(ip,narg,c);
700                                 (void)putchar(',');
701                                 kldsymcmdname((int)*ip);
702                                 ip++;
703                                 narg--;
704                         } else if (ktr->ktr_code == SYS_sigprocmask) {
705                                 (void)putchar('(');
706                                 sigprocmaskhowname((int)*ip);
707                                 ip++;
708                                 narg--;
709                                 c = ',';
710                         } else if (ktr->ktr_code == SYS___acl_get_file ||
711                                    ktr->ktr_code == SYS___acl_set_file ||
712                                    ktr->ktr_code == SYS___acl_get_fd ||
713                                    ktr->ktr_code == SYS___acl_set_fd ||
714                                    ktr->ktr_code == SYS___acl_delete_file ||
715                                    ktr->ktr_code == SYS___acl_delete_fd ||
716                                    ktr->ktr_code == SYS___acl_aclcheck_file ||
717                                    ktr->ktr_code == SYS___acl_aclcheck_fd ||
718                                    ktr->ktr_code == SYS___acl_get_link ||
719                                    ktr->ktr_code == SYS___acl_set_link ||
720                                    ktr->ktr_code == SYS___acl_delete_link ||
721                                    ktr->ktr_code == SYS___acl_aclcheck_link) {
722                                 print_number(ip,narg,c);
723                                 (void)putchar(',');
724                                 acltypename((int)*ip);
725                                 ip++;
726                                 narg--;
727                         } else if (ktr->ktr_code == SYS_sigaction) {
728                                 (void)putchar('(');
729                                 signame((int)*ip);
730                                 ip++;
731                                 narg--;
732                                 c = ',';
733                         } else if (ktr->ktr_code == SYS_extattrctl) {
734                                 print_number(ip,narg,c);
735                                 (void)putchar(',');
736                                 extattrctlname((int)*ip);
737                                 ip++;
738                                 narg--;
739                         } else if (ktr->ktr_code == SYS_nmount) {
740                                 print_number(ip,narg,c);
741                                 print_number(ip,narg,c);
742                                 (void)putchar(',');
743                                 mountflagsname ((int)*ip);
744                                 ip++;
745                                 narg--;
746                         } else if (ktr->ktr_code == SYS_kse_thr_interrupt) {
747                                 print_number(ip,narg,c);
748                                 (void)putchar(',');
749                                 ksethrcmdname ((int)*ip);
750                                 ip++;
751                                 narg--;
752                         } else if (ktr->ktr_code == SYS_thr_create) {
753                                 print_number(ip,narg,c);
754                                 print_number(ip,narg,c);
755                                 (void)putchar(',');
756                                 thrcreateflagsname ((int)*ip);
757                                 ip++;
758                                 narg--;
759                         } else if (ktr->ktr_code == SYS_thr_kill) {
760                                 print_number(ip,narg,c);
761                                 (void)putchar(',');
762                                 signame ((int)*ip);
763                                 ip++;
764                                 narg--;
765                         } else if (ktr->ktr_code == SYS_kldunloadf) {
766                                 print_number(ip,narg,c);
767                                 (void)putchar(',');
768                                 kldunloadfflagsname ((int)*ip);
769                                 ip++;
770                                 narg--;
771                         }
772                 }
773                 while (narg) {
774                         print_number(ip,narg,c);
775                 }
776                 (void)putchar(')');
777         }
778         (void)putchar('\n');
779 }
780
781 void
782 ktrsysret(struct ktr_sysret *ktr)
783 {
784         register_t ret = ktr->ktr_retval;
785         int error = ktr->ktr_error;
786         int code = ktr->ktr_code;
787
788         if (code >= nsyscalls || code < 0)
789                 (void)printf("[%d] ", code);
790         else
791                 (void)printf("%s ", syscallnames[code]);
792
793         if (error == 0) {
794                 if (fancy) {
795                         (void)printf("%d", ret);
796                         if (ret < 0 || ret > 9)
797                                 (void)printf("/%#lx", (long)ret);
798                 } else {
799                         if (decimal)
800                                 (void)printf("%ld", (long)ret);
801                         else
802                                 (void)printf("%#lx", (long)ret);
803                 }
804         } else if (error == ERESTART)
805                 (void)printf("RESTART");
806         else if (error == EJUSTRETURN)
807                 (void)printf("JUSTRETURN");
808         else {
809                 (void)printf("-1 errno %d", ktr->ktr_error);
810                 if (fancy)
811                         (void)printf(" %s", strerror(ktr->ktr_error));
812         }
813         (void)putchar('\n');
814 }
815
816 void
817 ktrnamei(char *cp, int len)
818 {
819         (void)printf("\"%.*s\"\n", len, cp);
820 }
821
822 void
823 hexdump(char *p, int len, int screenwidth)
824 {
825         int n, i;
826         int width;
827
828         width = 0;
829         do {
830                 width += 2;
831                 i = 13;                 /* base offset */
832                 i += (width / 2) + 1;   /* spaces every second byte */
833                 i += (width * 2);       /* width of bytes */
834                 i += 3;                 /* "  |" */
835                 i += width;             /* each byte */
836                 i += 1;                 /* "|" */
837         } while (i < screenwidth);
838         width -= 2;
839
840         for (n = 0; n < len; n += width) {
841                 for (i = n; i < n + width; i++) {
842                         if ((i % width) == 0) { /* beginning of line */
843                                 printf("       0x%04x", i);
844                         }
845                         if ((i % 2) == 0) {
846                                 printf(" ");
847                         }
848                         if (i < len)
849                                 printf("%02x", p[i] & 0xff);
850                         else
851                                 printf("  ");
852                 }
853                 printf("  |");
854                 for (i = n; i < n + width; i++) {
855                         if (i >= len)
856                                 break;
857                         if (p[i] >= ' ' && p[i] <= '~')
858                                 printf("%c", p[i]);
859                         else
860                                 printf(".");
861                 }
862                 printf("|\n");
863         }
864         if ((i % width) != 0)
865                 printf("\n");
866 }
867
868 void
869 visdump(char *dp, int datalen, int screenwidth)
870 {
871         int col = 0;
872         char *cp;
873         int width;
874         char visbuf[5];
875
876         (void)printf("       \"");
877         col = 8;
878         for (;datalen > 0; datalen--, dp++) {
879                 (void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
880                 cp = visbuf;
881                 /*
882                  * Keep track of printables and
883                  * space chars (like fold(1)).
884                  */
885                 if (col == 0) {
886                         (void)putchar('\t');
887                         col = 8;
888                 }
889                 switch(*cp) {
890                 case '\n':
891                         col = 0;
892                         (void)putchar('\n');
893                         continue;
894                 case '\t':
895                         width = 8 - (col&07);
896                         break;
897                 default:
898                         width = strlen(cp);
899                 }
900                 if (col + width > (screenwidth-2)) {
901                         (void)printf("\\\n\t");
902                         col = 8;
903                 }
904                 col += width;
905                 do {
906                         (void)putchar(*cp++);
907                 } while (*cp);
908         }
909         if (col == 0)
910                 (void)printf("       ");
911         (void)printf("\"\n");
912 }
913
914 void
915 ktrgenio(struct ktr_genio *ktr, int len)
916 {
917         int datalen = len - sizeof (struct ktr_genio);
918         char *dp = (char *)ktr + sizeof (struct ktr_genio);
919         static int screenwidth = 0;
920         int i, binary;
921
922         if (screenwidth == 0) {
923                 struct winsize ws;
924
925                 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
926                     ws.ws_col > 8)
927                         screenwidth = ws.ws_col;
928                 else
929                         screenwidth = 80;
930         }
931         printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
932                 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
933                 datalen == 1 ? "" : "s");
934         if (suppressdata)
935                 return;
936         if (maxdata && datalen > maxdata)
937                 datalen = maxdata;
938
939         for (i = 0, binary = 0; i < datalen && binary == 0; i++)  {
940                 if (dp[i] >= 32 && dp[i] < 127)
941                         continue;
942                 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
943                         continue;
944                 binary = 1;
945         }
946         if (binary)
947                 hexdump(dp, datalen, screenwidth);
948         else
949                 visdump(dp, datalen, screenwidth);
950 }
951
952 const char *signames[] = {
953         "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT",     /*  1 - 6  */
954         "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS",             /*  7 - 12 */
955         "PIPE", "ALRM",  "TERM", "URG", "STOP", "TSTP",         /* 13 - 18 */
956         "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU",           /* 19 - 24 */
957         "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1",        /* 25 - 30 */
958         "USR2", NULL,                                           /* 31 - 32 */
959 };
960
961 void
962 ktrpsig(struct ktr_psig *psig)
963 {
964         if (psig->signo > 0 && psig->signo < NSIG)
965                 (void)printf("SIG%s ", signames[psig->signo]);
966         else
967                 (void)printf("SIG %d ", psig->signo);
968         if (psig->action == SIG_DFL)
969                 (void)printf("SIG_DFL\n");
970         else {
971                 (void)printf("caught handler=0x%lx mask=0x%x code=0x%x\n",
972                     (u_long)psig->action, psig->mask.__bits[0], psig->code);
973         }
974 }
975
976 void
977 ktrcsw(struct ktr_csw *cs)
978 {
979         (void)printf("%s %s\n", cs->out ? "stop" : "resume",
980                 cs->user ? "user" : "kernel");
981 }
982
983 #define UTRACE_DLOPEN_START             1
984 #define UTRACE_DLOPEN_STOP              2
985 #define UTRACE_DLCLOSE_START            3
986 #define UTRACE_DLCLOSE_STOP             4
987 #define UTRACE_LOAD_OBJECT              5
988 #define UTRACE_UNLOAD_OBJECT            6
989 #define UTRACE_ADD_RUNDEP               7
990 #define UTRACE_PRELOAD_FINISHED         8
991 #define UTRACE_INIT_CALL                9
992 #define UTRACE_FINI_CALL                10
993
994 struct utrace_rtld {
995         char sig[4];                            /* 'RTLD' */
996         int event;
997         void *handle;
998         void *mapbase;
999         size_t mapsize;
1000         int refcnt;
1001         char name[MAXPATHLEN];
1002 };
1003
1004 void
1005 ktruser_rtld(int len, unsigned char *p)
1006 {
1007         struct utrace_rtld *ut = (struct utrace_rtld *)p;
1008         void *parent;
1009         int mode;
1010
1011         switch (ut->event) {
1012         case UTRACE_DLOPEN_START:
1013                 mode = ut->refcnt;
1014                 printf("dlopen(%s, ", ut->name);
1015                 switch (mode & RTLD_MODEMASK) {
1016                 case RTLD_NOW:
1017                         printf("RTLD_NOW");
1018                         break;
1019                 case RTLD_LAZY:
1020                         printf("RTLD_LAZY");
1021                         break;
1022                 default:
1023                         printf("%#x", mode & RTLD_MODEMASK);
1024                 }
1025                 if (mode & RTLD_GLOBAL)
1026                         printf(" | RTLD_GLOBAL");
1027                 if (mode & RTLD_TRACE)
1028                         printf(" | RTLD_TRACE");
1029                 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE))
1030                         printf(" | %#x", mode &
1031                             ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE));
1032                 printf(")\n");
1033                 break;
1034         case UTRACE_DLOPEN_STOP:
1035                 printf("%p = dlopen(%s) ref %d\n", ut->handle, ut->name,
1036                     ut->refcnt);
1037                 break;
1038         case UTRACE_DLCLOSE_START:
1039                 printf("dlclose(%p) (%s, %d)\n", ut->handle, ut->name,
1040                     ut->refcnt);
1041                 break;
1042         case UTRACE_DLCLOSE_STOP:
1043                 printf("dlclose(%p) finished\n", ut->handle);
1044                 break;
1045         case UTRACE_LOAD_OBJECT:
1046                 printf("RTLD: loaded   %p @ %p - %p (%s)\n", ut->handle,
1047                     ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1048                     ut->name);
1049                 break;
1050         case UTRACE_UNLOAD_OBJECT:
1051                 printf("RTLD: unloaded %p @ %p - %p (%s)\n", ut->handle,
1052                     ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1053                     ut->name);
1054                 break;
1055         case UTRACE_ADD_RUNDEP:
1056                 parent = ut->mapbase;
1057                 printf("RTLD: %p now depends on %p (%s, %d)\n", parent,
1058                     ut->handle, ut->name, ut->refcnt);
1059                 break;
1060         case UTRACE_PRELOAD_FINISHED:
1061                 printf("RTLD: LD_PRELOAD finished\n");
1062                 break;
1063         case UTRACE_INIT_CALL:
1064                 printf("RTLD: init %p for %p (%s)\n", ut->mapbase, ut->handle,
1065                     ut->name);
1066                 break;
1067         case UTRACE_FINI_CALL:
1068                 printf("RTLD: fini %p for %p (%s)\n", ut->mapbase, ut->handle,
1069                     ut->name);
1070                 break;
1071         default:
1072                 p += 4;
1073                 len -= 4;
1074                 printf("RTLD: %d ", len);
1075                 while (len--)
1076                         if (decimal)
1077                                 printf(" %d", *p++);
1078                         else
1079                                 printf(" %02x", *p++);
1080                 printf("\n");
1081         }
1082 }
1083
1084 struct utrace_malloc {
1085         void *p;
1086         size_t s;
1087         void *r;
1088 };
1089
1090 void
1091 ktruser_malloc(int len, unsigned char *p)
1092 {
1093         struct utrace_malloc *ut = (struct utrace_malloc *)p;
1094
1095         if (ut->p == NULL) {
1096                 if (ut->s == 0 && ut->r == NULL)
1097                         printf("malloc_init()\n");
1098                 else
1099                         printf("%p = malloc(%zu)\n", ut->r, ut->s);
1100         } else {
1101                 if (ut->s == 0)
1102                         printf("free(%p)\n", ut->p);
1103                 else
1104                         printf("%p = realloc(%p, %zu)\n", ut->r, ut->p, ut->s);
1105         }
1106 }
1107
1108 void
1109 ktruser(int len, unsigned char *p)
1110 {
1111
1112         if (len >= 8 && bcmp(p, "RTLD", 4) == 0) {
1113                 ktruser_rtld(len, p);
1114                 return;
1115         }
1116
1117         if (len == sizeof(struct utrace_malloc)) {
1118                 ktruser_malloc(len, p);
1119                 return;
1120         }
1121
1122         (void)printf("%d ", len);
1123         while (len--)
1124                 if (decimal)
1125                         (void)printf(" %d", *p++);
1126                 else
1127                         (void)printf(" %02x", *p++);
1128         (void)printf("\n");
1129 }
1130
1131 void
1132 usage(void)
1133 {
1134         (void)fprintf(stderr,
1135   "usage: kdump [-dEnlHRsT] [-f trfile] [-m maxdata] [-p pid] [-t [cnisuw]]\n");
1136         exit(1);
1137 }