Annotation of src/usr.bin/kdump/ktrstruct.c, Revision 1.3
1.3 ! guenther 1: /* $OpenBSD: ktrstruct.c,v 1.2 2013/09/09 05:10:32 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>
37: #include <sys/un.h>
38: #include <netinet/in.h>
39: #include <arpa/inet.h>
40:
41: #include <ctype.h>
42: #include <err.h>
43: #include <limits.h>
44: #include <poll.h>
45: #include <signal.h>
46: #include <stdio.h>
47: #include <stdlib.h>
48: #include <stdint.h>
49: #include <string.h>
50: #include <grp.h>
51: #include <pwd.h>
52: #include <unistd.h>
53:
54: #include "kdump.h"
55: #include "kdump_subr.h"
56:
57: #define TIME_FORMAT "%b %e %T %Y"
58:
59: static void
60: ktrsockaddr(struct sockaddr *sa)
61: {
62: /*
63: TODO: Support additional address families
64: #include <netnatm/natm.h>
65: struct sockaddr_natm *natm;
66: #include <netsmb/netbios.h>
67: struct sockaddr_nb *nb;
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
137: print_time(time_t t, int relative)
138: {
139: char timestr[PATH_MAX + 4];
140: struct tm *tm;
141:
142: if (resolv == 0 || relative)
143: printf("%jd", (intmax_t)t);
144: else {
145: tm = localtime(&t);
146: (void)strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
147: printf("\"%s\"", timestr);
148: }
149: }
150:
151: static void
152: print_timespec(const struct timespec *tsp, int relative)
153: {
1.2 guenther 154: if (tsp->tv_nsec == UTIME_NOW)
155: printf("UTIME_NOW");
156: else if (tsp->tv_nsec == UTIME_OMIT)
157: printf("UTIME_OMIT");
158: else {
159: print_time(tsp->tv_sec, relative);
160: if (tsp->tv_nsec != 0)
161: printf(".%09ld", tsp->tv_nsec);
162: }
1.1 guenther 163: }
164:
165: static void
166: ktrstat(const struct stat *statp)
167: {
168: char mode[12];
169: struct passwd *pwd;
170: struct group *grp;
171:
172: /*
173: * note: ktrstruct() has already verified that statp points to a
174: * buffer exactly sizeof(struct stat) bytes long.
175: */
176: printf("struct stat { ");
177: strmode(statp->st_mode, mode);
178: printf("dev=%d, ino=%llu, mode=%s, nlink=%u, ",
179: statp->st_dev, (unsigned long long)statp->st_ino,
180: mode, statp->st_nlink);
181: if (resolv == 0 || (pwd = getpwuid(statp->st_uid)) == NULL)
182: printf("uid=%u, ", statp->st_uid);
183: else
184: printf("uid=\"%s\", ", pwd->pw_name);
185: if (resolv == 0 || (grp = getgrgid(statp->st_gid)) == NULL)
186: printf("gid=%u, ", statp->st_gid);
187: else
188: printf("gid=\"%s\", ", grp->gr_name);
189: printf("rdev=%d, ", statp->st_rdev);
190: printf("atime=");
191: print_timespec(&statp->st_atim, 0);
192: printf(", mtime=");
193: print_timespec(&statp->st_mtim, 0);
194: printf(", ctime=");
195: print_timespec(&statp->st_ctim, 0);
196: printf(", size=%lld, blocks=%lld, blksize=%u, flags=0x%x, gen=0x%x",
197: statp->st_size, statp->st_blocks, statp->st_blksize,
198: statp->st_flags, statp->st_gen);
199: printf(" }\n");
200: }
201:
202: static void
203: ktrtimespec(const struct timespec *tsp, int relative)
204: {
205: printf("struct timespec { ");
206: print_timespec(tsp, relative);
207: printf(" }\n");
208: }
209:
210: static void
1.3 ! guenther 211: print_timeval(const struct timeval *tvp, int relative)
1.1 guenther 212: {
213: print_time(tvp->tv_sec, relative);
214: if (tvp->tv_usec != 0)
215: printf(".%06ld", tvp->tv_usec);
1.3 ! guenther 216: }
! 217:
! 218: static void
! 219: ktrtimeval(const struct timeval *tvp, int relative)
! 220: {
! 221: printf("struct timeval { ");
! 222: print_timeval(tvp, relative);
1.1 guenther 223: printf(" }\n");
224: }
225:
226: static void
227: ktrsigaction(const struct sigaction *sa)
228: {
229: /*
230: * note: ktrstruct() has already verified that sa points to a
231: * buffer exactly sizeof(struct sigaction) bytes long.
232: */
233: printf("struct sigaction { ");
234: if (sa->sa_handler == SIG_DFL)
235: printf("handler=SIG_DFL");
236: else if (sa->sa_handler == SIG_IGN)
237: printf("handler=SIG_IGN");
238: else if (sa->sa_flags & SA_SIGINFO)
239: printf("sigaction=%p", (void *)sa->sa_sigaction);
240: else
241: printf("handler=%p", (void *)sa->sa_handler);
242: printf(", mask=");
243: sigset(sa->sa_mask);
244: printf(", flags=");
245: sigactionflagname(sa->sa_flags);
246: printf(" }\n");
247: }
248:
249: static void
250: print_rlim(rlim_t lim)
251: {
252: if (lim == RLIM_INFINITY)
253: printf("infinite");
254: else
255: printf("%llu", (unsigned long long)lim);
256: }
257:
258: static void
259: ktrrlimit(const struct rlimit *limp)
260: {
261: printf("struct rlimit { ");
262: printf("cur=");
263: print_rlim(limp->rlim_cur);
264: printf(", max=");
265: print_rlim(limp->rlim_max);
266: printf(" }\n");
267: }
268:
269: static void
270: ktrtfork(const struct __tfork *tf)
271: {
272: printf("struct __tfork { tcb=%p, tid=%p, stack=%p }\n",
273: tf->tf_tcb, (void *)tf->tf_tid, tf->tf_stack);
274: }
275:
276: static void
277: ktrfdset(const struct fd_set *fds, int len)
278: {
279: int nfds, i, start = -1;
280: char sep = ' ';
281:
282: nfds = len * NBBY;
283: printf("struct fd_set {");
284: for (i = 0; i <= nfds; i++)
285: if (i != nfds && FD_ISSET(i, fds)) {
286: if (start == -1)
287: start = i;
288: } else if (start != -1) {
289: putchar(sep);
290: if (start == i - 1)
291: printf("%d", start);
292: else if (start == i - 2)
293: printf("%d,%d", start, i - 1);
294: else
295: printf("%d-%d", start, i - 1);
296: sep = ',';
297: start = -1;
298: }
299:
300: printf(" }\n");
301: }
302:
1.3 ! guenther 303: static void
! 304: ktrrusage(const struct rusage *rup)
! 305: {
! 306: printf("struct rusage { utime=");
! 307: print_timeval(&rup->ru_utime, 1);
! 308: printf(", stime=");
! 309: print_timeval(&rup->ru_stime, 1);
! 310: printf(", maxrss=%ld, ixrss=%ld, idrss=%ld, isrss=%ld,"
! 311: " minflt=%ld, majflt=%ld, nswap=%ld, inblock=%ld,"
! 312: " oublock=%ld, msgsnd=%ld, msgrcv=%ld, nsignals=%ld,"
! 313: " nvcsw=%ld, nivcsw=%ld }\n",
! 314: rup->ru_maxrss, rup->ru_ixrss, rup->ru_idrss, rup->ru_isrss,
! 315: rup->ru_minflt, rup->ru_majflt, rup->ru_nswap, rup->ru_inblock,
! 316: rup->ru_oublock, rup->ru_msgsnd, rup->ru_msgrcv, rup->ru_nsignals,
! 317: rup->ru_nvcsw, rup->ru_nivcsw);
! 318: }
! 319:
1.1 guenther 320: void
321: ktrstruct(char *buf, size_t buflen)
322: {
323: char *name, *data;
324: size_t namelen, datalen;
325: int i;
326:
327: for (name = buf, namelen = 0; namelen < buflen && name[namelen] != '\0';
328: ++namelen)
329: /* nothing */;
330: if (namelen == buflen)
331: goto invalid;
332: if (name[namelen] != '\0')
333: goto invalid;
334: data = buf + namelen + 1;
335: datalen = buflen - namelen - 1;
336: if (datalen == 0)
337: goto invalid;
338: /* sanity check */
339: for (i = 0; i < namelen; ++i)
340: if (!isalpha((unsigned char)name[i]))
341: goto invalid;
342: if (strcmp(name, "stat") == 0) {
343: struct stat sb;
344:
345: if (datalen != sizeof(struct stat))
346: goto invalid;
347: memcpy(&sb, data, datalen);
348: ktrstat(&sb);
349: } else if (strcmp(name, "sockaddr") == 0) {
350: struct sockaddr_storage ss;
351:
352: if (datalen > sizeof(ss))
353: goto invalid;
354: memcpy(&ss, data, datalen);
355: if ((ss.ss_family != AF_UNIX &&
356: datalen < sizeof(struct sockaddr)) || datalen != ss.ss_len)
357: goto invalid;
358: ktrsockaddr((struct sockaddr *)&ss);
359: } else if (strcmp(name, "abstimespec") == 0 ||
360: strcmp(name, "reltimespec") == 0) {
361: struct timespec ts;
362:
363: if (datalen != sizeof(ts))
364: goto invalid;
365: memcpy(&ts, data, datalen);
366: ktrtimespec(&ts, name[0] == 'r');
367: } else if (strcmp(name, "abstimeval") == 0 ||
368: strcmp(name, "reltimeval") == 0) {
369: struct timeval tv;
370:
371: if (datalen != sizeof(tv))
372: goto invalid;
373: memcpy(&tv, data, datalen);
374: ktrtimeval(&tv, name[0] == 'r');
375: } else if (strcmp(name, "sigaction") == 0) {
376: struct sigaction sa;
377:
378: if (datalen != sizeof(sa))
379: goto invalid;
380: memcpy(&sa, data, datalen);
381: ktrsigaction(&sa);
382: } else if (strcmp(name, "rlimit") == 0) {
383: struct rlimit lim;
384:
385: if (datalen != sizeof(lim))
386: goto invalid;
387: memcpy(&lim, data, datalen);
388: ktrrlimit(&lim);
1.3 ! guenther 389: } else if (strcmp(name, "rusage") == 0) {
! 390: struct rusage ru;
! 391:
! 392: if (datalen != sizeof(ru))
! 393: goto invalid;
! 394: memcpy(&ru, data, datalen);
! 395: ktrrusage(&ru);
1.1 guenther 396: } else if (strcmp(name, "tfork") == 0) {
397: struct __tfork tf;
398:
399: if (datalen != sizeof(tf))
400: goto invalid;
401: memcpy(&tf, data, datalen);
402: ktrtfork(&tf);
403: } else if (strcmp(name, "fdset") == 0) {
404: struct fd_set *fds;
405: if ((fds = malloc(datalen)) == NULL)
406: err(1, "malloc");
407: memcpy(fds, data, datalen);
408: ktrfdset(fds, datalen);
409: free(fds);
410: } else {
411: printf("unknown structure %s\n", name);
412: }
413: return;
414: invalid:
415: printf("invalid record\n");
416: }