Annotation of src/usr.bin/kdump/ktrstruct.c, Revision 1.19
1.19 ! guenther 1: /* $OpenBSD: ktrstruct.c,v 1.18 2016/03/24 05:05:42 guenther Exp $ */
1.1 guenther 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. Neither the name of the University nor the names of its contributors
16: * may be used to endorse or promote products derived from this software
17: * without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: */
31:
32: #include <sys/types.h>
33: #include <sys/resource.h>
34: #include <sys/socket.h>
35: #include <sys/stat.h>
36: #include <sys/time.h>
1.14 tedu 37: #include <sys/event.h>
1.1 guenther 38: #include <sys/un.h>
1.6 guenther 39: #include <ufs/ufs/quota.h>
1.1 guenther 40: #include <netinet/in.h>
41: #include <arpa/inet.h>
42:
43: #include <ctype.h>
44: #include <err.h>
45: #include <limits.h>
1.10 guenther 46: #include <netdb.h>
1.1 guenther 47: #include <poll.h>
48: #include <signal.h>
1.13 guenther 49: #include <stddef.h>
1.1 guenther 50: #include <stdio.h>
51: #include <stdlib.h>
52: #include <stdint.h>
53: #include <string.h>
54: #include <grp.h>
55: #include <pwd.h>
56: #include <unistd.h>
1.18 guenther 57: #include <vis.h>
1.1 guenther 58:
59: #include "kdump.h"
60: #include "kdump_subr.h"
61:
62: #define TIME_FORMAT "%b %e %T %Y"
63:
64: static void
65: ktrsockaddr(struct sockaddr *sa)
66: {
1.18 guenther 67: /*
68: * TODO: Support additional address families
69: * #include <netmpls/mpls.h>
70: * struct sockaddr_mpls *mpls;
71: */
1.1 guenther 72:
73: /*
74: * note: ktrstruct() has already verified that sa points to a
75: * buffer at least sizeof(struct sockaddr) bytes long and exactly
76: * sa->sa_len bytes long.
77: */
78: printf("struct sockaddr { ");
79: sockfamilyname(sa->sa_family);
80: printf(", ");
81:
82: #define check_sockaddr_len(n) \
83: if (sa_##n->s##n##_len < sizeof(struct sockaddr_##n)) { \
84: printf("invalid"); \
85: break; \
86: }
87:
88: switch(sa->sa_family) {
89: case AF_INET: {
90: struct sockaddr_in *sa_in;
1.18 guenther 91: char addr[64];
1.1 guenther 92:
93: sa_in = (struct sockaddr_in *)sa;
94: check_sockaddr_len(in);
95: inet_ntop(AF_INET, &sa_in->sin_addr, addr, sizeof addr);
96: printf("%s:%u", addr, ntohs(sa_in->sin_port));
97: break;
98: }
99: case AF_INET6: {
100: struct sockaddr_in6 *sa_in6;
1.18 guenther 101: char addr[64];
1.1 guenther 102:
103: sa_in6 = (struct sockaddr_in6 *)sa;
104: check_sockaddr_len(in6);
105: inet_ntop(AF_INET6, &sa_in6->sin6_addr, addr, sizeof addr);
106: printf("[%s]:%u", addr, htons(sa_in6->sin6_port));
107: break;
108: }
109: case AF_UNIX: {
110: struct sockaddr_un *sa_un;
1.18 guenther 111: char path[4 * sizeof(sa_un->sun_path) + 1];
112: size_t len;
1.1 guenther 113:
114: sa_un = (struct sockaddr_un *)sa;
1.18 guenther 115: len = sa_un->sun_len;
116: if (len <= offsetof(struct sockaddr_un, sun_path)) {
1.1 guenther 117: printf("invalid");
118: break;
119: }
1.18 guenther 120: len -= offsetof(struct sockaddr_un, sun_path);
121: if (len > sizeof(sa_un->sun_path)) {
122: printf("too long");
123: break;
124: }
125: /* format, stopping at first NUL */
126: len = strnlen(sa_un->sun_path, len);
127: strvisx(path, sa_un->sun_path, len,
128: VIS_CSTYLE | VIS_DQ | VIS_TAB | VIS_NL);
129: printf("\"%s\"", path);
1.1 guenther 130: break;
131: }
132: default:
133: printf("unknown address family");
134: }
135: printf(" }\n");
136: }
137:
138: static void
1.8 guenther 139: print_time(time_t t, int relative, int have_subsec)
1.1 guenther 140: {
141: char timestr[PATH_MAX + 4];
142: struct tm *tm;
143:
1.8 guenther 144: if (t < 0 && have_subsec) {
145: /* negative times with non-zero subsecs require care */
146: printf("-%jd", -(intmax_t)(t + 1));
147: } else
1.1 guenther 148: printf("%jd", (intmax_t)t);
1.8 guenther 149:
150: if (!relative) {
1.1 guenther 151: tm = localtime(&t);
1.9 jsg 152: if (tm != NULL) {
153: (void)strftime(timestr, sizeof(timestr), TIME_FORMAT,
154: tm);
155: printf("<\"%s\">", timestr);
156: }
1.1 guenther 157: }
158: }
159:
160: static void
161: print_timespec(const struct timespec *tsp, int relative)
162: {
1.2 guenther 163: if (tsp->tv_nsec == UTIME_NOW)
164: printf("UTIME_NOW");
165: else if (tsp->tv_nsec == UTIME_OMIT)
166: printf("UTIME_OMIT");
1.8 guenther 167: else {
168: print_time(tsp->tv_sec, relative, tsp->tv_nsec);
1.2 guenther 169: if (tsp->tv_nsec != 0)
1.8 guenther 170: printf(".%09ld", tsp->tv_sec >= 0 ? tsp->tv_nsec :
171: 1000000000 - tsp->tv_nsec);
172: }
173: }
174:
175: void
176: uidname(int uid)
177: {
178: const char *name;
179:
180: if (uid == -1)
181: printf("-1");
182: else {
183: printf("%u<", (unsigned)uid);
184: if (uid > UID_MAX || (name = user_from_uid(uid, 1)) == NULL)
185: printf("unknown>");
186: else
187: printf("\"%s\">", name);
188: }
189: }
190:
191: void
192: gidname(int gid)
193: {
194: const char *name;
195:
196: if (gid == -1)
197: printf("-1");
198: else {
199: printf("%u<", (unsigned)gid);
200: if (gid > GID_MAX || (name = group_from_gid(gid, 1)) == NULL)
201: printf("unknown>");
202: else
203: printf("\"%s\">", name);
1.2 guenther 204: }
1.1 guenther 205: }
206:
207: static void
208: ktrstat(const struct stat *statp)
209: {
210: char mode[12];
211:
212: /*
213: * note: ktrstruct() has already verified that statp points to a
214: * buffer exactly sizeof(struct stat) bytes long.
215: */
216: printf("struct stat { ");
217: strmode(statp->st_mode, mode);
1.8 guenther 218: printf("dev=%d, ino=%llu, mode=%s, nlink=%u, uid=",
1.1 guenther 219: statp->st_dev, (unsigned long long)statp->st_ino,
220: mode, statp->st_nlink);
1.8 guenther 221: uidname(statp->st_uid);
222: printf(", gid=");
223: gidname(statp->st_gid);
224: printf(", rdev=%d, ", statp->st_rdev);
1.1 guenther 225: printf("atime=");
226: print_timespec(&statp->st_atim, 0);
227: printf(", mtime=");
228: print_timespec(&statp->st_mtim, 0);
229: printf(", ctime=");
230: print_timespec(&statp->st_ctim, 0);
1.7 krw 231: printf(", size=%lld, blocks=%lld, blksize=%d, flags=0x%x, gen=0x%x",
1.1 guenther 232: statp->st_size, statp->st_blocks, statp->st_blksize,
233: statp->st_flags, statp->st_gen);
234: printf(" }\n");
235: }
236:
237: static void
238: ktrtimespec(const struct timespec *tsp, int relative)
239: {
240: printf("struct timespec { ");
241: print_timespec(tsp, relative);
242: printf(" }\n");
243: }
244:
245: static void
1.3 guenther 246: print_timeval(const struct timeval *tvp, int relative)
1.1 guenther 247: {
1.8 guenther 248: print_time(tvp->tv_sec, relative, tvp->tv_usec);
249: if (tvp->tv_usec != 0)
250: printf(".%06ld", tvp->tv_sec >= 0 ? tvp->tv_usec :
1.4 guenther 251: 1000000 - tvp->tv_usec);
1.3 guenther 252: }
253:
254: static void
255: ktrtimeval(const struct timeval *tvp, int relative)
256: {
257: printf("struct timeval { ");
258: print_timeval(tvp, relative);
1.1 guenther 259: printf(" }\n");
260: }
261:
262: static void
263: ktrsigaction(const struct sigaction *sa)
264: {
265: /*
266: * note: ktrstruct() has already verified that sa points to a
267: * buffer exactly sizeof(struct sigaction) bytes long.
268: */
1.19 ! guenther 269: /*
! 270: * Fuck! Comparison of function pointers on hppa assumes you can
! 271: * dereference them if they're plabels! Cast everything to void *
! 272: * to suppress that extra logic; sorry folks, the address we report
! 273: * here might not match what you see in your executable...
! 274: */
1.1 guenther 275: printf("struct sigaction { ");
1.19 ! guenther 276: if ((void *)sa->sa_handler == (void *)SIG_DFL)
1.1 guenther 277: printf("handler=SIG_DFL");
1.19 ! guenther 278: else if ((void *)sa->sa_handler == (void *)SIG_IGN)
1.1 guenther 279: printf("handler=SIG_IGN");
280: else if (sa->sa_flags & SA_SIGINFO)
281: printf("sigaction=%p", (void *)sa->sa_sigaction);
282: else
283: printf("handler=%p", (void *)sa->sa_handler);
284: printf(", mask=");
285: sigset(sa->sa_mask);
286: printf(", flags=");
287: sigactionflagname(sa->sa_flags);
288: printf(" }\n");
289: }
290:
291: static void
292: print_rlim(rlim_t lim)
293: {
294: if (lim == RLIM_INFINITY)
295: printf("infinite");
296: else
297: printf("%llu", (unsigned long long)lim);
298: }
299:
300: static void
301: ktrrlimit(const struct rlimit *limp)
302: {
303: printf("struct rlimit { ");
304: printf("cur=");
305: print_rlim(limp->rlim_cur);
306: printf(", max=");
307: print_rlim(limp->rlim_max);
308: printf(" }\n");
309: }
310:
311: static void
312: ktrtfork(const struct __tfork *tf)
313: {
314: printf("struct __tfork { tcb=%p, tid=%p, stack=%p }\n",
315: tf->tf_tcb, (void *)tf->tf_tid, tf->tf_stack);
316: }
317:
318: static void
1.17 guenther 319: ktrfdset(struct fd_set *fds, int len)
1.1 guenther 320: {
321: int nfds, i, start = -1;
322: char sep = ' ';
323:
324: nfds = len * NBBY;
325: printf("struct fd_set {");
326: for (i = 0; i <= nfds; i++)
327: if (i != nfds && FD_ISSET(i, fds)) {
328: if (start == -1)
329: start = i;
330: } else if (start != -1) {
331: putchar(sep);
332: if (start == i - 1)
333: printf("%d", start);
334: else if (start == i - 2)
335: printf("%d,%d", start, i - 1);
336: else
337: printf("%d-%d", start, i - 1);
338: sep = ',';
339: start = -1;
340: }
341:
342: printf(" }\n");
343: }
344:
1.3 guenther 345: static void
346: ktrrusage(const struct rusage *rup)
347: {
348: printf("struct rusage { utime=");
349: print_timeval(&rup->ru_utime, 1);
350: printf(", stime=");
351: print_timeval(&rup->ru_stime, 1);
352: printf(", maxrss=%ld, ixrss=%ld, idrss=%ld, isrss=%ld,"
353: " minflt=%ld, majflt=%ld, nswap=%ld, inblock=%ld,"
354: " oublock=%ld, msgsnd=%ld, msgrcv=%ld, nsignals=%ld,"
355: " nvcsw=%ld, nivcsw=%ld }\n",
356: rup->ru_maxrss, rup->ru_ixrss, rup->ru_idrss, rup->ru_isrss,
357: rup->ru_minflt, rup->ru_majflt, rup->ru_nswap, rup->ru_inblock,
358: rup->ru_oublock, rup->ru_msgsnd, rup->ru_msgrcv, rup->ru_nsignals,
359: rup->ru_nvcsw, rup->ru_nivcsw);
360: }
361:
1.6 guenther 362: static void
363: ktrquota(const struct dqblk *quota)
364: {
365: printf("struct dqblk { bhardlimit=%u, bsoftlimit=%u, curblocks=%u,"
366: " ihardlimit=%u, isoftlimit=%u, curinodes=%u, btime=",
367: quota->dqb_bhardlimit, quota->dqb_bsoftlimit,
368: quota->dqb_curblocks, quota->dqb_ihardlimit,
369: quota->dqb_isoftlimit, quota->dqb_curinodes);
1.8 guenther 370: print_time(quota->dqb_btime, 0, 0);
1.6 guenther 371: printf(", itime=");
1.8 guenther 372: print_time(quota->dqb_itime, 0, 0);
1.6 guenther 373: printf(" }\n");
374: }
375:
1.10 guenther 376: static void
377: ktrmsghdr(const struct msghdr *msg)
378: {
379: printf("struct msghdr { name=%p, namelen=%u, iov=%p, iovlen=%u,"
1.16 guenther 380: " control=%p, controllen=%u, flags=",
1.10 guenther 381: msg->msg_name, msg->msg_namelen, msg->msg_iov, msg->msg_iovlen,
1.16 guenther 382: msg->msg_control, msg->msg_controllen);
383: sendrecvflagsname(msg->msg_flags);
384: printf(" }\n");
1.10 guenther 385: }
386:
387: static void
388: ktriovec(const char *data, int count)
389: {
390: struct iovec iov;
391: int i;
392:
393: printf("struct iovec");
394: if (count > 1)
395: printf(" [%d]", count);
396: for (i = 0; i < count; i++) {
397: memcpy(&iov, data, sizeof(iov));
398: data += sizeof(iov);
399: printf(" { base=%p, len=%lu }", iov.iov_base, iov.iov_len);
400: }
401: printf("\n");
402: }
403:
404: static void
1.14 tedu 405: ktrevent(const char *data, int count)
406: {
407: struct kevent kev;
408: int i;
409:
410: printf("struct kevent");
411: if (count > 1)
412: printf(" [%d]", count);
413: for (i = 0; i < count; i++) {
414: memcpy(&kev, data, sizeof(kev));
415: data += sizeof(kev);
1.16 guenther 416: printf(" { ident=%lu, filter=", kev.ident);
417: evfiltername(kev.filter);
418: printf(", flags=");
419: evflagsname(kev.flags);
420: printf(", fflags=");
421: evfflagsname(kev.filter, kev.fflags);
422: printf(", data=%llu", kev.data);
423: if ((kev.flags & EV_ERROR) && fancy) {
424: printf("<\"%s\">", strerror(kev.data));
1.15 tedu 425: }
1.16 guenther 426: printf(", udata=%p }", kev.udata);
1.14 tedu 427: }
428: printf("\n");
429: }
430:
431: static void
1.10 guenther 432: ktrcmsghdr(char *data, socklen_t len)
433: {
434: struct msghdr msg;
435: struct cmsghdr *cmsg;
436: int i, count, *fds;
437:
438: msg.msg_control = data;
439: msg.msg_controllen = len;
440:
441: /* count the control messages */
442: count = 0;
443: for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
444: cmsg = CMSG_NXTHDR(&msg, cmsg)) {
445: count++;
446: }
447:
448: printf("struct cmsghdr");
449: if (count > 1)
450: printf(" [%d]", count);
451:
452: for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
453: cmsg = CMSG_NXTHDR(&msg, cmsg)) {
454: printf(" { len=%u, level=", cmsg->cmsg_len);
455: if (cmsg->cmsg_level == SOL_SOCKET) {
456: printf("SOL_SOCKET, type=");
457: switch (cmsg->cmsg_type) {
458: case SCM_RIGHTS:
459: printf("SCM_RIGHTS, data=");
460: fds = (int *)CMSG_DATA(cmsg);
461: for (i = 0;
462: cmsg->cmsg_len > CMSG_LEN(sizeof(int) * i);
463: i++) {
464: printf("%s%d", i ? "," : "", fds[i]);
465: }
466: break;
467: case SCM_TIMESTAMP:
468: default:
469: printf("%d", cmsg->cmsg_type);
470: break;
471: }
472: } else {
473: struct protoent *p = getprotobynumber(cmsg->cmsg_level);
474:
475: printf("%u<%s>, type=%d", cmsg->cmsg_level,
476: p != NULL ? p->p_name : "unknown", cmsg->cmsg_type);
477: }
478: printf(" }");
479: }
480: printf("\n");
481: }
482:
1.1 guenther 483: void
484: ktrstruct(char *buf, size_t buflen)
485: {
486: char *name, *data;
487: size_t namelen, datalen;
488: int i;
489:
490: for (name = buf, namelen = 0; namelen < buflen && name[namelen] != '\0';
491: ++namelen)
492: /* nothing */;
493: if (namelen == buflen)
494: goto invalid;
495: if (name[namelen] != '\0')
496: goto invalid;
497: data = buf + namelen + 1;
498: datalen = buflen - namelen - 1;
499: if (datalen == 0)
500: goto invalid;
501: /* sanity check */
502: for (i = 0; i < namelen; ++i)
503: if (!isalpha((unsigned char)name[i]))
504: goto invalid;
505: if (strcmp(name, "stat") == 0) {
506: struct stat sb;
507:
508: if (datalen != sizeof(struct stat))
509: goto invalid;
510: memcpy(&sb, data, datalen);
511: ktrstat(&sb);
512: } else if (strcmp(name, "sockaddr") == 0) {
513: struct sockaddr_storage ss;
514:
515: if (datalen > sizeof(ss))
516: goto invalid;
517: memcpy(&ss, data, datalen);
518: if ((ss.ss_family != AF_UNIX &&
519: datalen < sizeof(struct sockaddr)) || datalen != ss.ss_len)
520: goto invalid;
521: ktrsockaddr((struct sockaddr *)&ss);
522: } else if (strcmp(name, "abstimespec") == 0 ||
523: strcmp(name, "reltimespec") == 0) {
524: struct timespec ts;
525:
526: if (datalen != sizeof(ts))
527: goto invalid;
528: memcpy(&ts, data, datalen);
529: ktrtimespec(&ts, name[0] == 'r');
530: } else if (strcmp(name, "abstimeval") == 0 ||
531: strcmp(name, "reltimeval") == 0) {
532: struct timeval tv;
533:
534: if (datalen != sizeof(tv))
535: goto invalid;
536: memcpy(&tv, data, datalen);
537: ktrtimeval(&tv, name[0] == 'r');
538: } else if (strcmp(name, "sigaction") == 0) {
539: struct sigaction sa;
540:
541: if (datalen != sizeof(sa))
542: goto invalid;
543: memcpy(&sa, data, datalen);
544: ktrsigaction(&sa);
545: } else if (strcmp(name, "rlimit") == 0) {
546: struct rlimit lim;
547:
548: if (datalen != sizeof(lim))
549: goto invalid;
550: memcpy(&lim, data, datalen);
551: ktrrlimit(&lim);
1.3 guenther 552: } else if (strcmp(name, "rusage") == 0) {
553: struct rusage ru;
554:
555: if (datalen != sizeof(ru))
556: goto invalid;
557: memcpy(&ru, data, datalen);
558: ktrrusage(&ru);
1.1 guenther 559: } else if (strcmp(name, "tfork") == 0) {
560: struct __tfork tf;
561:
562: if (datalen != sizeof(tf))
563: goto invalid;
564: memcpy(&tf, data, datalen);
565: ktrtfork(&tf);
566: } else if (strcmp(name, "fdset") == 0) {
567: struct fd_set *fds;
1.10 guenther 568:
1.1 guenther 569: if ((fds = malloc(datalen)) == NULL)
570: err(1, "malloc");
571: memcpy(fds, data, datalen);
572: ktrfdset(fds, datalen);
573: free(fds);
1.6 guenther 574: } else if (strcmp(name, "quota") == 0) {
575: struct dqblk quota;
576:
577: if (datalen != sizeof(quota))
578: goto invalid;
579: memcpy("a, data, datalen);
580: ktrquota("a);
1.10 guenther 581: } else if (strcmp(name, "msghdr") == 0) {
582: struct msghdr msg;
583:
584: if (datalen != sizeof(msg))
585: goto invalid;
586: memcpy(&msg, data, datalen);
587: ktrmsghdr(&msg);
588: } else if (strcmp(name, "iovec") == 0) {
589: if (datalen % sizeof(struct iovec))
590: goto invalid;
591: ktriovec(data, datalen / sizeof(struct iovec));
1.14 tedu 592: } else if (strcmp(name, "kevent") == 0) {
593: if (datalen % sizeof(struct kevent))
594: goto invalid;
595: ktrevent(data, datalen / sizeof(struct kevent));
1.10 guenther 596: } else if (strcmp(name, "cmsghdr") == 0) {
597: char *cmsg;
598:
599: if ((cmsg = malloc(datalen)) == NULL)
600: err(1, "malloc");
601: memcpy(cmsg, data, datalen);
602: ktrcmsghdr(cmsg, datalen);
603: free(cmsg);
1.12 deraadt 604: } else if (strcmp(name, "pledgereq") == 0) {
605: printf("pledge request=");
606: showbufc(basecol + sizeof("pledge request=") - 1,
1.18 guenther 607: (unsigned char *)data, datalen, VIS_DQ | VIS_TAB | VIS_NL);
1.12 deraadt 608: } else if (strcmp(name, "pledgepath") == 0) {
609: printf("pledge path=");
610: showbufc(basecol + sizeof("pledge path=") - 1,
1.18 guenther 611: (unsigned char *)data, datalen, VIS_DQ | VIS_TAB | VIS_NL);
1.1 guenther 612: } else {
613: printf("unknown structure %s\n", name);
614: }
615: return;
616: invalid:
617: printf("invalid record\n");
618: }