Annotation of src/usr.bin/kdump/ktrstruct.c, Revision 1.10
1.10 ! guenther 1: /* $OpenBSD: ktrstruct.c,v 1.9 2014/12/16 03:19:23 jsg 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>
37: #include <sys/un.h>
1.6 guenther 38: #include <ufs/ufs/quota.h>
1.1 guenther 39: #include <netinet/in.h>
40: #include <arpa/inet.h>
41:
42: #include <ctype.h>
43: #include <err.h>
44: #include <limits.h>
1.10 ! guenther 45: #include <netdb.h>
1.1 guenther 46: #include <poll.h>
47: #include <signal.h>
48: #include <stdio.h>
49: #include <stdlib.h>
50: #include <stdint.h>
51: #include <string.h>
52: #include <grp.h>
53: #include <pwd.h>
54: #include <unistd.h>
55:
56: #include "kdump.h"
57: #include "kdump_subr.h"
58:
59: #define TIME_FORMAT "%b %e %T %Y"
60:
61: static void
62: ktrsockaddr(struct sockaddr *sa)
63: {
64: /*
65: TODO: Support additional address families
1.4 guenther 66: #include <netmpls/mpls.h>
67: struct sockaddr_mpls *mpls;
1.1 guenther 68: */
69: char addr[64];
70:
71: /*
72: * note: ktrstruct() has already verified that sa points to a
73: * buffer at least sizeof(struct sockaddr) bytes long and exactly
74: * sa->sa_len bytes long.
75: */
76: printf("struct sockaddr { ");
77: sockfamilyname(sa->sa_family);
78: printf(", ");
79:
80: #define check_sockaddr_len(n) \
81: if (sa_##n->s##n##_len < sizeof(struct sockaddr_##n)) { \
82: printf("invalid"); \
83: break; \
84: }
85:
86: switch(sa->sa_family) {
87: case AF_INET: {
88: struct sockaddr_in *sa_in;
89:
90: sa_in = (struct sockaddr_in *)sa;
91: check_sockaddr_len(in);
92: inet_ntop(AF_INET, &sa_in->sin_addr, addr, sizeof addr);
93: printf("%s:%u", addr, ntohs(sa_in->sin_port));
94: break;
95: }
96: case AF_INET6: {
97: struct sockaddr_in6 *sa_in6;
98:
99: sa_in6 = (struct sockaddr_in6 *)sa;
100: check_sockaddr_len(in6);
101: inet_ntop(AF_INET6, &sa_in6->sin6_addr, addr, sizeof addr);
102: printf("[%s]:%u", addr, htons(sa_in6->sin6_port));
103: break;
104: }
105: #ifdef IPX
106: case AF_IPX: {
107: struct sockaddr_ipx *sa_ipx;
108:
109: sa_ipx = (struct sockaddr_ipx *)sa;
110: check_sockaddr_len(ipx);
111: /* XXX wish we had ipx_ntop */
112: printf("%s", ipx_ntoa(sa_ipx->sipx_addr));
113: break;
114: }
115: #endif
116: case AF_UNIX: {
117: struct sockaddr_un *sa_un;
118:
119: sa_un = (struct sockaddr_un *)sa;
120: if (sa_un->sun_len <= sizeof(sa_un->sun_len) +
121: sizeof(sa_un->sun_family)) {
122: printf("invalid");
123: break;
124: }
125: printf("\"%.*s\"", (int)(sa_un->sun_len -
126: sizeof(sa_un->sun_len) - sizeof(sa_un->sun_family)),
127: sa_un->sun_path);
128: break;
129: }
130: default:
131: printf("unknown address family");
132: }
133: printf(" }\n");
134: }
135:
136: static void
1.8 guenther 137: print_time(time_t t, int relative, int have_subsec)
1.1 guenther 138: {
139: char timestr[PATH_MAX + 4];
140: struct tm *tm;
141:
1.8 guenther 142: if (t < 0 && have_subsec) {
143: /* negative times with non-zero subsecs require care */
144: printf("-%jd", -(intmax_t)(t + 1));
145: } else
1.1 guenther 146: printf("%jd", (intmax_t)t);
1.8 guenther 147:
148: if (!relative) {
1.1 guenther 149: tm = localtime(&t);
1.9 jsg 150: if (tm != NULL) {
151: (void)strftime(timestr, sizeof(timestr), TIME_FORMAT,
152: tm);
153: printf("<\"%s\">", timestr);
154: }
1.1 guenther 155: }
156: }
157:
158: static void
159: print_timespec(const struct timespec *tsp, int relative)
160: {
1.2 guenther 161: if (tsp->tv_nsec == UTIME_NOW)
162: printf("UTIME_NOW");
163: else if (tsp->tv_nsec == UTIME_OMIT)
164: printf("UTIME_OMIT");
1.8 guenther 165: else {
166: print_time(tsp->tv_sec, relative, tsp->tv_nsec);
1.2 guenther 167: if (tsp->tv_nsec != 0)
1.8 guenther 168: printf(".%09ld", tsp->tv_sec >= 0 ? tsp->tv_nsec :
169: 1000000000 - tsp->tv_nsec);
170: }
171: }
172:
173: void
174: uidname(int uid)
175: {
176: const char *name;
177:
178: if (uid == -1)
179: printf("-1");
180: else {
181: printf("%u<", (unsigned)uid);
182: if (uid > UID_MAX || (name = user_from_uid(uid, 1)) == NULL)
183: printf("unknown>");
184: else
185: printf("\"%s\">", name);
186: }
187: }
188:
189: void
190: gidname(int gid)
191: {
192: const char *name;
193:
194: if (gid == -1)
195: printf("-1");
196: else {
197: printf("%u<", (unsigned)gid);
198: if (gid > GID_MAX || (name = group_from_gid(gid, 1)) == NULL)
199: printf("unknown>");
200: else
201: printf("\"%s\">", name);
1.2 guenther 202: }
1.1 guenther 203: }
204:
205: static void
206: ktrstat(const struct stat *statp)
207: {
208: char mode[12];
209:
210: /*
211: * note: ktrstruct() has already verified that statp points to a
212: * buffer exactly sizeof(struct stat) bytes long.
213: */
214: printf("struct stat { ");
215: strmode(statp->st_mode, mode);
1.8 guenther 216: printf("dev=%d, ino=%llu, mode=%s, nlink=%u, uid=",
1.1 guenther 217: statp->st_dev, (unsigned long long)statp->st_ino,
218: mode, statp->st_nlink);
1.8 guenther 219: uidname(statp->st_uid);
220: printf(", gid=");
221: gidname(statp->st_gid);
222: printf(", rdev=%d, ", statp->st_rdev);
1.1 guenther 223: printf("atime=");
224: print_timespec(&statp->st_atim, 0);
225: printf(", mtime=");
226: print_timespec(&statp->st_mtim, 0);
227: printf(", ctime=");
228: print_timespec(&statp->st_ctim, 0);
1.7 krw 229: printf(", size=%lld, blocks=%lld, blksize=%d, flags=0x%x, gen=0x%x",
1.1 guenther 230: statp->st_size, statp->st_blocks, statp->st_blksize,
231: statp->st_flags, statp->st_gen);
232: printf(" }\n");
233: }
234:
235: static void
236: ktrtimespec(const struct timespec *tsp, int relative)
237: {
238: printf("struct timespec { ");
239: print_timespec(tsp, relative);
240: printf(" }\n");
241: }
242:
243: static void
1.3 guenther 244: print_timeval(const struct timeval *tvp, int relative)
1.1 guenther 245: {
1.8 guenther 246: print_time(tvp->tv_sec, relative, tvp->tv_usec);
247: if (tvp->tv_usec != 0)
248: printf(".%06ld", tvp->tv_sec >= 0 ? tvp->tv_usec :
1.4 guenther 249: 1000000 - tvp->tv_usec);
1.3 guenther 250: }
251:
252: static void
253: ktrtimeval(const struct timeval *tvp, int relative)
254: {
255: printf("struct timeval { ");
256: print_timeval(tvp, relative);
1.1 guenther 257: printf(" }\n");
258: }
259:
260: static void
261: ktrsigaction(const struct sigaction *sa)
262: {
263: /*
264: * note: ktrstruct() has already verified that sa points to a
265: * buffer exactly sizeof(struct sigaction) bytes long.
266: */
267: printf("struct sigaction { ");
268: if (sa->sa_handler == SIG_DFL)
269: printf("handler=SIG_DFL");
270: else if (sa->sa_handler == SIG_IGN)
271: printf("handler=SIG_IGN");
272: else if (sa->sa_flags & SA_SIGINFO)
273: printf("sigaction=%p", (void *)sa->sa_sigaction);
274: else
275: printf("handler=%p", (void *)sa->sa_handler);
276: printf(", mask=");
277: sigset(sa->sa_mask);
278: printf(", flags=");
279: sigactionflagname(sa->sa_flags);
280: printf(" }\n");
281: }
282:
283: static void
284: print_rlim(rlim_t lim)
285: {
286: if (lim == RLIM_INFINITY)
287: printf("infinite");
288: else
289: printf("%llu", (unsigned long long)lim);
290: }
291:
292: static void
293: ktrrlimit(const struct rlimit *limp)
294: {
295: printf("struct rlimit { ");
296: printf("cur=");
297: print_rlim(limp->rlim_cur);
298: printf(", max=");
299: print_rlim(limp->rlim_max);
300: printf(" }\n");
301: }
302:
303: static void
304: ktrtfork(const struct __tfork *tf)
305: {
306: printf("struct __tfork { tcb=%p, tid=%p, stack=%p }\n",
307: tf->tf_tcb, (void *)tf->tf_tid, tf->tf_stack);
308: }
309:
310: static void
311: ktrfdset(const struct fd_set *fds, int len)
312: {
313: int nfds, i, start = -1;
314: char sep = ' ';
315:
316: nfds = len * NBBY;
317: printf("struct fd_set {");
318: for (i = 0; i <= nfds; i++)
319: if (i != nfds && FD_ISSET(i, fds)) {
320: if (start == -1)
321: start = i;
322: } else if (start != -1) {
323: putchar(sep);
324: if (start == i - 1)
325: printf("%d", start);
326: else if (start == i - 2)
327: printf("%d,%d", start, i - 1);
328: else
329: printf("%d-%d", start, i - 1);
330: sep = ',';
331: start = -1;
332: }
333:
334: printf(" }\n");
335: }
336:
1.3 guenther 337: static void
338: ktrrusage(const struct rusage *rup)
339: {
340: printf("struct rusage { utime=");
341: print_timeval(&rup->ru_utime, 1);
342: printf(", stime=");
343: print_timeval(&rup->ru_stime, 1);
344: printf(", maxrss=%ld, ixrss=%ld, idrss=%ld, isrss=%ld,"
345: " minflt=%ld, majflt=%ld, nswap=%ld, inblock=%ld,"
346: " oublock=%ld, msgsnd=%ld, msgrcv=%ld, nsignals=%ld,"
347: " nvcsw=%ld, nivcsw=%ld }\n",
348: rup->ru_maxrss, rup->ru_ixrss, rup->ru_idrss, rup->ru_isrss,
349: rup->ru_minflt, rup->ru_majflt, rup->ru_nswap, rup->ru_inblock,
350: rup->ru_oublock, rup->ru_msgsnd, rup->ru_msgrcv, rup->ru_nsignals,
351: rup->ru_nvcsw, rup->ru_nivcsw);
352: }
353:
1.6 guenther 354: static void
355: ktrquota(const struct dqblk *quota)
356: {
357: printf("struct dqblk { bhardlimit=%u, bsoftlimit=%u, curblocks=%u,"
358: " ihardlimit=%u, isoftlimit=%u, curinodes=%u, btime=",
359: quota->dqb_bhardlimit, quota->dqb_bsoftlimit,
360: quota->dqb_curblocks, quota->dqb_ihardlimit,
361: quota->dqb_isoftlimit, quota->dqb_curinodes);
1.8 guenther 362: print_time(quota->dqb_btime, 0, 0);
1.6 guenther 363: printf(", itime=");
1.8 guenther 364: print_time(quota->dqb_itime, 0, 0);
1.6 guenther 365: printf(" }\n");
366: }
367:
1.10 ! guenther 368: static void
! 369: ktrmsghdr(const struct msghdr *msg)
! 370: {
! 371: printf("struct msghdr { name=%p, namelen=%u, iov=%p, iovlen=%u,"
! 372: " control=%p, controllen=%u, flags=%d }\n",
! 373: msg->msg_name, msg->msg_namelen, msg->msg_iov, msg->msg_iovlen,
! 374: msg->msg_control, msg->msg_controllen, msg->msg_flags);
! 375: }
! 376:
! 377: static void
! 378: ktriovec(const char *data, int count)
! 379: {
! 380: struct iovec iov;
! 381: int i;
! 382:
! 383: printf("struct iovec");
! 384: if (count > 1)
! 385: printf(" [%d]", count);
! 386: for (i = 0; i < count; i++) {
! 387: memcpy(&iov, data, sizeof(iov));
! 388: data += sizeof(iov);
! 389: printf(" { base=%p, len=%lu }", iov.iov_base, iov.iov_len);
! 390: }
! 391: printf("\n");
! 392: }
! 393:
! 394: static void
! 395: ktrcmsghdr(char *data, socklen_t len)
! 396: {
! 397: struct msghdr msg;
! 398: struct cmsghdr *cmsg;
! 399: int i, count, *fds;
! 400:
! 401: msg.msg_control = data;
! 402: msg.msg_controllen = len;
! 403:
! 404: /* count the control messages */
! 405: count = 0;
! 406: for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
! 407: cmsg = CMSG_NXTHDR(&msg, cmsg)) {
! 408: count++;
! 409: }
! 410:
! 411: printf("struct cmsghdr");
! 412: if (count > 1)
! 413: printf(" [%d]", count);
! 414:
! 415: for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
! 416: cmsg = CMSG_NXTHDR(&msg, cmsg)) {
! 417: printf(" { len=%u, level=", cmsg->cmsg_len);
! 418: if (cmsg->cmsg_level == SOL_SOCKET) {
! 419: printf("SOL_SOCKET, type=");
! 420: switch (cmsg->cmsg_type) {
! 421: case SCM_RIGHTS:
! 422: printf("SCM_RIGHTS, data=");
! 423: fds = (int *)CMSG_DATA(cmsg);
! 424: for (i = 0;
! 425: cmsg->cmsg_len > CMSG_LEN(sizeof(int) * i);
! 426: i++) {
! 427: printf("%s%d", i ? "," : "", fds[i]);
! 428: }
! 429: break;
! 430: case SCM_TIMESTAMP:
! 431: default:
! 432: printf("%d", cmsg->cmsg_type);
! 433: break;
! 434: }
! 435: } else {
! 436: struct protoent *p = getprotobynumber(cmsg->cmsg_level);
! 437:
! 438: printf("%u<%s>, type=%d", cmsg->cmsg_level,
! 439: p != NULL ? p->p_name : "unknown", cmsg->cmsg_type);
! 440: }
! 441: printf(" }");
! 442: }
! 443: printf("\n");
! 444: }
! 445:
1.1 guenther 446: void
447: ktrstruct(char *buf, size_t buflen)
448: {
449: char *name, *data;
450: size_t namelen, datalen;
451: int i;
452:
453: for (name = buf, namelen = 0; namelen < buflen && name[namelen] != '\0';
454: ++namelen)
455: /* nothing */;
456: if (namelen == buflen)
457: goto invalid;
458: if (name[namelen] != '\0')
459: goto invalid;
460: data = buf + namelen + 1;
461: datalen = buflen - namelen - 1;
462: if (datalen == 0)
463: goto invalid;
464: /* sanity check */
465: for (i = 0; i < namelen; ++i)
466: if (!isalpha((unsigned char)name[i]))
467: goto invalid;
468: if (strcmp(name, "stat") == 0) {
469: struct stat sb;
470:
471: if (datalen != sizeof(struct stat))
472: goto invalid;
473: memcpy(&sb, data, datalen);
474: ktrstat(&sb);
475: } else if (strcmp(name, "sockaddr") == 0) {
476: struct sockaddr_storage ss;
477:
478: if (datalen > sizeof(ss))
479: goto invalid;
480: memcpy(&ss, data, datalen);
481: if ((ss.ss_family != AF_UNIX &&
482: datalen < sizeof(struct sockaddr)) || datalen != ss.ss_len)
483: goto invalid;
484: ktrsockaddr((struct sockaddr *)&ss);
485: } else if (strcmp(name, "abstimespec") == 0 ||
486: strcmp(name, "reltimespec") == 0) {
487: struct timespec ts;
488:
489: if (datalen != sizeof(ts))
490: goto invalid;
491: memcpy(&ts, data, datalen);
492: ktrtimespec(&ts, name[0] == 'r');
493: } else if (strcmp(name, "abstimeval") == 0 ||
494: strcmp(name, "reltimeval") == 0) {
495: struct timeval tv;
496:
497: if (datalen != sizeof(tv))
498: goto invalid;
499: memcpy(&tv, data, datalen);
500: ktrtimeval(&tv, name[0] == 'r');
501: } else if (strcmp(name, "sigaction") == 0) {
502: struct sigaction sa;
503:
504: if (datalen != sizeof(sa))
505: goto invalid;
506: memcpy(&sa, data, datalen);
507: ktrsigaction(&sa);
508: } else if (strcmp(name, "rlimit") == 0) {
509: struct rlimit lim;
510:
511: if (datalen != sizeof(lim))
512: goto invalid;
513: memcpy(&lim, data, datalen);
514: ktrrlimit(&lim);
1.3 guenther 515: } else if (strcmp(name, "rusage") == 0) {
516: struct rusage ru;
517:
518: if (datalen != sizeof(ru))
519: goto invalid;
520: memcpy(&ru, data, datalen);
521: ktrrusage(&ru);
1.1 guenther 522: } else if (strcmp(name, "tfork") == 0) {
523: struct __tfork tf;
524:
525: if (datalen != sizeof(tf))
526: goto invalid;
527: memcpy(&tf, data, datalen);
528: ktrtfork(&tf);
529: } else if (strcmp(name, "fdset") == 0) {
530: struct fd_set *fds;
1.10 ! guenther 531:
1.1 guenther 532: if ((fds = malloc(datalen)) == NULL)
533: err(1, "malloc");
534: memcpy(fds, data, datalen);
535: ktrfdset(fds, datalen);
536: free(fds);
1.6 guenther 537: } else if (strcmp(name, "quota") == 0) {
538: struct dqblk quota;
539:
540: if (datalen != sizeof(quota))
541: goto invalid;
542: memcpy("a, data, datalen);
543: ktrquota("a);
1.10 ! guenther 544: } else if (strcmp(name, "msghdr") == 0) {
! 545: struct msghdr msg;
! 546:
! 547: if (datalen != sizeof(msg))
! 548: goto invalid;
! 549: memcpy(&msg, data, datalen);
! 550: ktrmsghdr(&msg);
! 551: } else if (strcmp(name, "iovec") == 0) {
! 552: if (datalen % sizeof(struct iovec))
! 553: goto invalid;
! 554: ktriovec(data, datalen / sizeof(struct iovec));
! 555: } else if (strcmp(name, "cmsghdr") == 0) {
! 556: char *cmsg;
! 557:
! 558: if ((cmsg = malloc(datalen)) == NULL)
! 559: err(1, "malloc");
! 560: memcpy(cmsg, data, datalen);
! 561: ktrcmsghdr(cmsg, datalen);
! 562: free(cmsg);
1.1 guenther 563: } else {
564: printf("unknown structure %s\n", name);
565: }
566: return;
567: invalid:
568: printf("invalid record\n");
569: }