Mon Jan 10 05:15:17 2011 UTC ()
format SI_QUEUE properly.


(christos)
diff -r1.105 -r1.106 src/usr.bin/kdump/kdump.c

cvs diff -r1.105 -r1.106 src/usr.bin/kdump/kdump.c (switch to unified diff)

--- src/usr.bin/kdump/kdump.c 2010/08/08 18:31:50 1.105
+++ src/usr.bin/kdump/kdump.c 2011/01/10 05:15:17 1.106
@@ -1,1104 +1,1104 @@ @@ -1,1104 +1,1104 @@
1/* $NetBSD: kdump.c,v 1.105 2010/08/08 18:31:50 chs Exp $ */ 1/* $NetBSD: kdump.c,v 1.106 2011/01/10 05:15:17 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1988, 1993 4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors 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 16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission. 17 * without specific prior written permission.
18 * 18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 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 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 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 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 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 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE. 29 * SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33#ifndef lint 33#ifndef lint
34__COPYRIGHT("@(#) Copyright (c) 1988, 1993\ 34__COPYRIGHT("@(#) Copyright (c) 1988, 1993\
35 The Regents of the University of California. All rights reserved."); 35 The Regents of the University of California. All rights reserved.");
36#endif /* not lint */ 36#endif /* not lint */
37 37
38#ifndef lint 38#ifndef lint
39#if 0 39#if 0
40static char sccsid[] = "@(#)kdump.c 8.4 (Berkeley) 4/28/95"; 40static char sccsid[] = "@(#)kdump.c 8.4 (Berkeley) 4/28/95";
41#else 41#else
42__RCSID("$NetBSD: kdump.c,v 1.105 2010/08/08 18:31:50 chs Exp $"); 42__RCSID("$NetBSD: kdump.c,v 1.106 2011/01/10 05:15:17 christos Exp $");
43#endif 43#endif
44#endif /* not lint */ 44#endif /* not lint */
45 45
46#include <sys/param.h> 46#include <sys/param.h>
47#define _KERNEL 47#define _KERNEL
48#include <sys/errno.h> 48#include <sys/errno.h>
49#undef _KERNEL 49#undef _KERNEL
50#include <sys/time.h> 50#include <sys/time.h>
51#include <sys/uio.h> 51#include <sys/uio.h>
52#include <sys/ktrace.h> 52#include <sys/ktrace.h>
53#include <sys/ioctl.h> 53#include <sys/ioctl.h>
54#include <sys/ptrace.h> 54#include <sys/ptrace.h>
55 55
56#include <ctype.h> 56#include <ctype.h>
57#include <err.h> 57#include <err.h>
58#include <signal.h> 58#include <signal.h>
59#include <stddef.h> 59#include <stddef.h>
60#include <stdio.h> 60#include <stdio.h>
61#include <stdlib.h> 61#include <stdlib.h>
62#include <string.h> 62#include <string.h>
63#include <unistd.h> 63#include <unistd.h>
64#include <vis.h> 64#include <vis.h>
65 65
66#include "ktrace.h" 66#include "ktrace.h"
67#include "setemul.h" 67#include "setemul.h"
68 68
69#include <sys/syscall.h> 69#include <sys/syscall.h>
70 70
71static int timestamp, decimal, plain, tail, maxdata = -1, numeric; 71static int timestamp, decimal, plain, tail, maxdata = -1, numeric;
72static int word_size = 0; 72static int word_size = 0;
73static pid_t do_pid = -1; 73static pid_t do_pid = -1;
74static const char *tracefile = NULL; 74static const char *tracefile = NULL;
75static struct ktr_header ktr_header; 75static struct ktr_header ktr_header;
76static int emul_changed = 0; 76static int emul_changed = 0;
77 77
78#define eqs(s1, s2) (strcmp((s1), (s2)) == 0) 78#define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
79#define small(v) (((long)(v) >= 0) && ((long)(v) < 10)) 79#define small(v) (((long)(v) >= 0) && ((long)(v) < 10))
80 80
81static const char * const ptrace_ops[] = { 81static const char * const ptrace_ops[] = {
82 "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U", 82 "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U",
83 "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE", 83 "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE",
84 "PT_KILL", "PT_ATTACH", "PT_DETACH", "PT_IO", 84 "PT_KILL", "PT_ATTACH", "PT_DETACH", "PT_IO",
85 "PT_DUMPCORE", "PT_LWPINFO", "PT_SYSCALL", 85 "PT_DUMPCORE", "PT_LWPINFO", "PT_SYSCALL",
86}; 86};
87 87
88#ifdef PT_MACHDEP_STRINGS 88#ifdef PT_MACHDEP_STRINGS
89static const char * const ptrace_machdep_ops[] = { PT_MACHDEP_STRINGS }; 89static const char * const ptrace_machdep_ops[] = { PT_MACHDEP_STRINGS };
90#endif 90#endif
91 91
92static const char * const linux_ptrace_ops[] = { 92static const char * const linux_ptrace_ops[] = {
93 "PTRACE_TRACEME", 93 "PTRACE_TRACEME",
94 "PTRACE_PEEKTEXT", "PTRACE_PEEKDATA", "PTRACE_PEEKUSER", 94 "PTRACE_PEEKTEXT", "PTRACE_PEEKDATA", "PTRACE_PEEKUSER",
95 "PTRACE_POKETEXT", "PTRACE_POKEDATA", "PTRACE_POKEUSER", 95 "PTRACE_POKETEXT", "PTRACE_POKEDATA", "PTRACE_POKEUSER",
96 "PTRACE_CONT", "PTRACE_KILL", "PTRACE_SINGLESTEP", 96 "PTRACE_CONT", "PTRACE_KILL", "PTRACE_SINGLESTEP",
97 NULL, NULL, 97 NULL, NULL,
98 "PTRACE_GETREGS", "PTRACE_SETREGS", "PTRACE_GETFPREGS", 98 "PTRACE_GETREGS", "PTRACE_SETREGS", "PTRACE_GETFPREGS",
99 "PTRACE_SETFPREGS", "PTRACE_ATTACH", "PTRACE_DETACH", 99 "PTRACE_SETFPREGS", "PTRACE_ATTACH", "PTRACE_DETACH",
100 NULL, NULL, NULL, NULL, NULL, NULL, 100 NULL, NULL, NULL, NULL, NULL, NULL,
101 "PTRACE_SYSCALL", 101 "PTRACE_SYSCALL",
102}; 102};
103 103
104int main(int, char **); 104int main(int, char **);
105static int fread_tail(void *, size_t, size_t); 105static int fread_tail(void *, size_t, size_t);
106static int dumpheader(struct ktr_header *); 106static int dumpheader(struct ktr_header *);
107static void output_long(u_long, int); 107static void output_long(u_long, int);
108static void ioctldecode(u_long); 108static void ioctldecode(u_long);
109static void ktrsyscall(struct ktr_syscall *); 109static void ktrsyscall(struct ktr_syscall *);
110static void ktrsysret(struct ktr_sysret *, int); 110static void ktrsysret(struct ktr_sysret *, int);
111static void ktrnamei(char *, int); 111static void ktrnamei(char *, int);
112static void ktremul(char *, int, int); 112static void ktremul(char *, int, int);
113static void ktrgenio(struct ktr_genio *, int); 113static void ktrgenio(struct ktr_genio *, int);
114static void ktrpsig(void *, int); 114static void ktrpsig(void *, int);
115static void ktrcsw(struct ktr_csw *); 115static void ktrcsw(struct ktr_csw *);
116static void ktruser(struct ktr_user *, int); 116static void ktruser(struct ktr_user *, int);
117static void ktrmmsg(struct ktr_mmsg *, int); 117static void ktrmmsg(struct ktr_mmsg *, int);
118static void ktrmool(struct ktr_mool *, int); 118static void ktrmool(struct ktr_mool *, int);
119static void ktrmib(int *, int); 119static void ktrmib(int *, int);
120static void usage(void) __dead; 120static void usage(void) __dead;
121static void eprint(int); 121static void eprint(int);
122static void rprint(register_t); 122static void rprint(register_t);
123static const char *signame(long, int); 123static const char *signame(long, int);
124static void hexdump_buf(const void *, int, int); 124static void hexdump_buf(const void *, int, int);
125static void visdump_buf(const void *, int, int); 125static void visdump_buf(const void *, int, int);
126 126
127int 127int
128main(int argc, char **argv) 128main(int argc, char **argv)
129{ 129{
130 int ch, ktrlen, size; 130 int ch, ktrlen, size;
131 void *m; 131 void *m;
132 int trpoints = 0; 132 int trpoints = 0;
133 int trset = 0; 133 int trset = 0;
134 const char *emul_name = "netbsd"; 134 const char *emul_name = "netbsd";
135 int col; 135 int col;
136 char *cp; 136 char *cp;
137 137
138 setprogname(argv[0]); 138 setprogname(argv[0]);
139 139
140 if (strcmp(getprogname(), "ioctlname") == 0) { 140 if (strcmp(getprogname(), "ioctlname") == 0) {
141 int i; 141 int i;
142 142
143 while ((ch = getopt(argc, argv, "e:")) != -1) 143 while ((ch = getopt(argc, argv, "e:")) != -1)
144 switch (ch) { 144 switch (ch) {
145 case 'e': 145 case 'e':
146 emul_name = optarg; 146 emul_name = optarg;
147 break; 147 break;
148 default: 148 default:
149 usage(); 149 usage();
150 break; 150 break;
151 } 151 }
152 setemul(emul_name, 0, 0); 152 setemul(emul_name, 0, 0);
153 argv += optind; 153 argv += optind;
154 argc -= optind; 154 argc -= optind;
155 155
156 if (argc < 1) 156 if (argc < 1)
157 usage(); 157 usage();
158 158
159 for (i = 0; i < argc; i++) { 159 for (i = 0; i < argc; i++) {
160 ioctldecode(strtoul(argv[i], NULL, 0)); 160 ioctldecode(strtoul(argv[i], NULL, 0));
161 (void)putchar('\n'); 161 (void)putchar('\n');
162 } 162 }
163 return 0; 163 return 0;
164 } 164 }
165  165
166 while ((ch = getopt(argc, argv, "e:f:dlm:Nnp:RTt:xX:")) != -1) { 166 while ((ch = getopt(argc, argv, "e:f:dlm:Nnp:RTt:xX:")) != -1) {
167 switch (ch) { 167 switch (ch) {
168 case 'e': 168 case 'e':
169 emul_name = strdup(optarg); /* it's safer to copy it */ 169 emul_name = strdup(optarg); /* it's safer to copy it */
170 break; 170 break;
171 case 'f': 171 case 'f':
172 tracefile = optarg; 172 tracefile = optarg;
173 break; 173 break;
174 case 'd': 174 case 'd':
175 decimal = 1; 175 decimal = 1;
176 break; 176 break;
177 case 'l': 177 case 'l':
178 tail = 1; 178 tail = 1;
179 break; 179 break;
180 case 'p': 180 case 'p':
181 do_pid = strtoul(optarg, &cp, 0); 181 do_pid = strtoul(optarg, &cp, 0);
182 if (*cp != 0) 182 if (*cp != 0)
183 errx(1,"invalid number %s", optarg); 183 errx(1,"invalid number %s", optarg);
184 break; 184 break;
185 case 'm': 185 case 'm':
186 maxdata = strtoul(optarg, &cp, 0); 186 maxdata = strtoul(optarg, &cp, 0);
187 if (*cp != 0) 187 if (*cp != 0)
188 errx(1,"invalid number %s", optarg); 188 errx(1,"invalid number %s", optarg);
189 break; 189 break;
190 case 'N': 190 case 'N':
191 numeric++; 191 numeric++;
192 break; 192 break;
193 case 'n': 193 case 'n':
194 plain++; 194 plain++;
195 break; 195 break;
196 case 'R': 196 case 'R':
197 timestamp = 2; /* relative timestamp */ 197 timestamp = 2; /* relative timestamp */
198 break; 198 break;
199 case 'T': 199 case 'T':
200 timestamp = 1; 200 timestamp = 1;
201 break; 201 break;
202 case 't': 202 case 't':
203 trset = 1; 203 trset = 1;
204 trpoints = getpoints(trpoints, optarg); 204 trpoints = getpoints(trpoints, optarg);
205 if (trpoints < 0) 205 if (trpoints < 0)
206 errx(1, "unknown trace point in %s", optarg); 206 errx(1, "unknown trace point in %s", optarg);
207 break; 207 break;
208 case 'x': 208 case 'x':
209 word_size = 1; 209 word_size = 1;
210 break; 210 break;
211 case 'X': 211 case 'X':
212 word_size = strtoul(optarg, &cp, 0); 212 word_size = strtoul(optarg, &cp, 0);
213 if (*cp != 0 || word_size & (word_size - 1) || 213 if (*cp != 0 || word_size & (word_size - 1) ||
214 word_size > 16 || word_size <= 0) 214 word_size > 16 || word_size <= 0)
215 errx(1, "argument to -X must be " 215 errx(1, "argument to -X must be "
216 "1, 2, 4, 8 or 16"); 216 "1, 2, 4, 8 or 16");
217 break; 217 break;
218 default: 218 default:
219 usage(); 219 usage();
220 } 220 }
221 } 221 }
222 argv += optind; 222 argv += optind;
223 argc -= optind; 223 argc -= optind;
224 224
225 if (!trset) 225 if (!trset)
226 trpoints = ALL_POINTS; 226 trpoints = ALL_POINTS;
227 227
228 if (tracefile == NULL) { 228 if (tracefile == NULL) {
229 if (argc == 1) { 229 if (argc == 1) {
230 tracefile = argv[0]; 230 tracefile = argv[0];
231 argv++; 231 argv++;
232 argc--; 232 argc--;
233 } else 233 } else
234 tracefile = DEF_TRACEFILE; 234 tracefile = DEF_TRACEFILE;
235 } 235 }
236 236
237 if (argc > 0) 237 if (argc > 0)
238 usage(); 238 usage();
239 239
240 setemul(emul_name, 0, 0); 240 setemul(emul_name, 0, 0);
241 mach_lookup_emul(); 241 mach_lookup_emul();
242 242
243 m = malloc(size = 1024); 243 m = malloc(size = 1024);
244 if (m == NULL) 244 if (m == NULL)
245 errx(1, "malloc: %s", strerror(ENOMEM)); 245 errx(1, "malloc: %s", strerror(ENOMEM));
246 if (!freopen(tracefile, "r", stdin)) 246 if (!freopen(tracefile, "r", stdin))
247 err(1, "%s", tracefile); 247 err(1, "%s", tracefile);
248 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) { 248 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
249 if (trpoints & (1 << ktr_header.ktr_type) && 249 if (trpoints & (1 << ktr_header.ktr_type) &&
250 (do_pid == -1 || ktr_header.ktr_pid == do_pid)) 250 (do_pid == -1 || ktr_header.ktr_pid == do_pid))
251 col = dumpheader(&ktr_header); 251 col = dumpheader(&ktr_header);
252 else 252 else
253 col = -1; 253 col = -1;
254 if ((ktrlen = ktr_header.ktr_len) < 0) 254 if ((ktrlen = ktr_header.ktr_len) < 0)
255 errx(1, "bogus length 0x%x", ktrlen); 255 errx(1, "bogus length 0x%x", ktrlen);
256 if (ktrlen > size) { 256 if (ktrlen > size) {
257 while (ktrlen > size) 257 while (ktrlen > size)
258 size *= 2; 258 size *= 2;
259 m = realloc(m, size); 259 m = realloc(m, size);
260 if (m == NULL) 260 if (m == NULL)
261 errx(1, "realloc: %s", strerror(ENOMEM)); 261 errx(1, "realloc: %s", strerror(ENOMEM));
262 } 262 }
263 if (ktrlen && fread_tail(m, ktrlen, 1) == 0) 263 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
264 errx(1, "data too short"); 264 errx(1, "data too short");
265 if (col == -1) 265 if (col == -1)
266 continue; 266 continue;
267 267
268 /* update context to match currently processed record */ 268 /* update context to match currently processed record */
269 ectx_sanify(ktr_header.ktr_pid); 269 ectx_sanify(ktr_header.ktr_pid);
270 270
271 switch (ktr_header.ktr_type) { 271 switch (ktr_header.ktr_type) {
272 case KTR_SYSCALL: 272 case KTR_SYSCALL:
273 ktrsyscall(m); 273 ktrsyscall(m);
274 break; 274 break;
275 case KTR_SYSRET: 275 case KTR_SYSRET:
276 ktrsysret(m, ktrlen); 276 ktrsysret(m, ktrlen);
277 break; 277 break;
278 case KTR_NAMEI: 278 case KTR_NAMEI:
279 ktrnamei(m, ktrlen); 279 ktrnamei(m, ktrlen);
280 break; 280 break;
281 case KTR_GENIO: 281 case KTR_GENIO:
282 ktrgenio(m, ktrlen); 282 ktrgenio(m, ktrlen);
283 break; 283 break;
284 case KTR_PSIG: 284 case KTR_PSIG:
285 ktrpsig(m, ktrlen); 285 ktrpsig(m, ktrlen);
286 break; 286 break;
287 case KTR_CSW: 287 case KTR_CSW:
288 ktrcsw(m); 288 ktrcsw(m);
289 break; 289 break;
290 case KTR_EMUL: 290 case KTR_EMUL:
291 ktremul(m, ktrlen, size); 291 ktremul(m, ktrlen, size);
292 break; 292 break;
293 case KTR_USER: 293 case KTR_USER:
294 ktruser(m, ktrlen); 294 ktruser(m, ktrlen);
295 break; 295 break;
296 case KTR_MMSG: 296 case KTR_MMSG:
297 ktrmmsg(m, ktrlen); 297 ktrmmsg(m, ktrlen);
298 break; 298 break;
299 case KTR_MOOL: 299 case KTR_MOOL:
300 ktrmool(m, ktrlen); 300 ktrmool(m, ktrlen);
301 break; 301 break;
302 case KTR_EXEC_ARG: 302 case KTR_EXEC_ARG:
303 case KTR_EXEC_ENV: 303 case KTR_EXEC_ENV:
304 visdump_buf(m, ktrlen, col); 304 visdump_buf(m, ktrlen, col);
305 break; 305 break;
306 case KTR_MIB: 306 case KTR_MIB:
307 ktrmib(m, ktrlen); 307 ktrmib(m, ktrlen);
308 break; 308 break;
309 default: 309 default:
310 putchar('\n'); 310 putchar('\n');
311 hexdump_buf(m, ktrlen, word_size ? word_size : 1); 311 hexdump_buf(m, ktrlen, word_size ? word_size : 1);
312 } 312 }
313 if (tail) 313 if (tail)
314 (void)fflush(stdout); 314 (void)fflush(stdout);
315 } 315 }
316 return (0); 316 return (0);
317} 317}
318 318
319static int 319static int
320fread_tail(void *buf, size_t num, size_t size) 320fread_tail(void *buf, size_t num, size_t size)
321{ 321{
322 int i; 322 int i;
323 323
324 while ((i = fread(buf, size, num, stdin)) == 0 && tail) { 324 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
325 (void)sleep(1); 325 (void)sleep(1);
326 clearerr(stdin); 326 clearerr(stdin);
327 } 327 }
328 return (i); 328 return (i);
329} 329}
330 330
331static int 331static int
332dumpheader(struct ktr_header *kth) 332dumpheader(struct ktr_header *kth)
333{ 333{
334 char unknown[64]; 334 char unknown[64];
335 const char *type; 335 const char *type;
336 union holdtime { 336 union holdtime {
337 struct timeval tv; 337 struct timeval tv;
338 struct timespec ts; 338 struct timespec ts;
339 }; 339 };
340 static union holdtime prevtime; 340 static union holdtime prevtime;
341 union holdtime temp; 341 union holdtime temp;
342 int col; 342 int col;
343 343
344 switch (kth->ktr_type) { 344 switch (kth->ktr_type) {
345 case KTR_SYSCALL: 345 case KTR_SYSCALL:
346 type = "CALL"; 346 type = "CALL";
347 break; 347 break;
348 case KTR_SYSRET: 348 case KTR_SYSRET:
349 type = "RET "; 349 type = "RET ";
350 break; 350 break;
351 case KTR_NAMEI: 351 case KTR_NAMEI:
352 type = "NAMI"; 352 type = "NAMI";
353 break; 353 break;
354 case KTR_GENIO: 354 case KTR_GENIO:
355 type = "GIO "; 355 type = "GIO ";
356 break; 356 break;
357 case KTR_PSIG: 357 case KTR_PSIG:
358 type = "PSIG"; 358 type = "PSIG";
359 break; 359 break;
360 case KTR_CSW: 360 case KTR_CSW:
361 type = "CSW "; 361 type = "CSW ";
362 break; 362 break;
363 case KTR_EMUL: 363 case KTR_EMUL:
364 type = "EMUL"; 364 type = "EMUL";
365 break; 365 break;
366 case KTR_USER: 366 case KTR_USER:
367 type = "MISC"; 367 type = "MISC";
368 break; 368 break;
369 case KTR_MMSG: 369 case KTR_MMSG:
370 type = "MMSG"; 370 type = "MMSG";
371 break; 371 break;
372 case KTR_MOOL: 372 case KTR_MOOL:
373 type = "MOOL"; 373 type = "MOOL";
374 break; 374 break;
375 case KTR_EXEC_ENV: 375 case KTR_EXEC_ENV:
376 type = "ENV"; 376 type = "ENV";
377 break; 377 break;
378 case KTR_EXEC_ARG: 378 case KTR_EXEC_ARG:
379 type = "ARG"; 379 type = "ARG";
380 break; 380 break;
381 case KTR_SAUPCALL: 381 case KTR_SAUPCALL:
382 type = "SAU"; 382 type = "SAU";
383 break; 383 break;
384 case KTR_MIB: 384 case KTR_MIB:
385 type = "MIB"; 385 type = "MIB";
386 break; 386 break;
387 default: 387 default:
388 (void)snprintf(unknown, sizeof(unknown), "UNKNOWN(%d)", 388 (void)snprintf(unknown, sizeof(unknown), "UNKNOWN(%d)",
389 kth->ktr_type); 389 kth->ktr_type);
390 type = unknown; 390 type = unknown;
391 } 391 }
392 392
393 col = printf("%6d ", kth->ktr_pid); 393 col = printf("%6d ", kth->ktr_pid);
394 if (kth->ktr_version > KTRFACv0) 394 if (kth->ktr_version > KTRFACv0)
395 col += printf("%6d ", kth->ktr_lid); 395 col += printf("%6d ", kth->ktr_lid);
396 col += printf("%-8.*s ", MAXCOMLEN, kth->ktr_comm); 396 col += printf("%-8.*s ", MAXCOMLEN, kth->ktr_comm);
397 if (timestamp) { 397 if (timestamp) {
398 (void)&prevtime; 398 (void)&prevtime;
399 if (timestamp == 2) { 399 if (timestamp == 2) {
400 switch (kth->ktr_version) { 400 switch (kth->ktr_version) {
401 case KTRFAC_VERSION(KTRFACv0): 401 case KTRFAC_VERSION(KTRFACv0):
402 if (prevtime.tv.tv_sec == 0) 402 if (prevtime.tv.tv_sec == 0)
403 temp.tv.tv_sec = temp.tv.tv_usec = 0; 403 temp.tv.tv_sec = temp.tv.tv_usec = 0;
404 else 404 else
405 timersub(&kth->ktr_otv, 405 timersub(&kth->ktr_otv,
406 &prevtime.tv, &temp.tv); 406 &prevtime.tv, &temp.tv);
407 prevtime.tv.tv_sec = kth->ktr_otv.tv_sec; 407 prevtime.tv.tv_sec = kth->ktr_otv.tv_sec;
408 prevtime.tv.tv_usec = kth->ktr_otv.tv_usec; 408 prevtime.tv.tv_usec = kth->ktr_otv.tv_usec;
409 break; 409 break;
410 case KTRFAC_VERSION(KTRFACv1): 410 case KTRFAC_VERSION(KTRFACv1):
411 if (prevtime.ts.tv_sec == 0) 411 if (prevtime.ts.tv_sec == 0)
412 temp.ts.tv_sec = temp.ts.tv_nsec = 0; 412 temp.ts.tv_sec = temp.ts.tv_nsec = 0;
413 else 413 else
414 timespecsub(&kth->ktr_ots, 414 timespecsub(&kth->ktr_ots,
415 &prevtime.ts, &temp.ts); 415 &prevtime.ts, &temp.ts);
416 prevtime.ts.tv_sec = kth->ktr_ots.tv_sec; 416 prevtime.ts.tv_sec = kth->ktr_ots.tv_sec;
417 prevtime.ts.tv_nsec = kth->ktr_ots.tv_nsec; 417 prevtime.ts.tv_nsec = kth->ktr_ots.tv_nsec;
418 break; 418 break;
419 case KTRFAC_VERSION(KTRFACv2): 419 case KTRFAC_VERSION(KTRFACv2):
420 if (prevtime.ts.tv_sec == 0) 420 if (prevtime.ts.tv_sec == 0)
421 temp.ts.tv_sec = temp.ts.tv_nsec = 0; 421 temp.ts.tv_sec = temp.ts.tv_nsec = 0;
422 else 422 else
423 timespecsub(&kth->ktr_ts, 423 timespecsub(&kth->ktr_ts,
424 &prevtime.ts, &temp.ts); 424 &prevtime.ts, &temp.ts);
425 prevtime.ts.tv_sec = kth->ktr_ts.tv_sec; 425 prevtime.ts.tv_sec = kth->ktr_ts.tv_sec;
426 prevtime.ts.tv_nsec = kth->ktr_ts.tv_nsec; 426 prevtime.ts.tv_nsec = kth->ktr_ts.tv_nsec;
427 break; 427 break;
428 default: 428 default:
429 goto badversion; 429 goto badversion;
430 } 430 }
431 } else { 431 } else {
432 switch (kth->ktr_version) { 432 switch (kth->ktr_version) {
433 case KTRFAC_VERSION(KTRFACv0): 433 case KTRFAC_VERSION(KTRFACv0):
434 temp.tv.tv_sec = kth->ktr_otv.tv_sec; 434 temp.tv.tv_sec = kth->ktr_otv.tv_sec;
435 temp.tv.tv_usec = kth->ktr_otv.tv_usec; 435 temp.tv.tv_usec = kth->ktr_otv.tv_usec;
436 break; 436 break;
437 case KTRFAC_VERSION(KTRFACv1): 437 case KTRFAC_VERSION(KTRFACv1):
438 temp.ts.tv_sec = kth->ktr_ots.tv_sec; 438 temp.ts.tv_sec = kth->ktr_ots.tv_sec;
439 temp.ts.tv_nsec = kth->ktr_ots.tv_nsec; 439 temp.ts.tv_nsec = kth->ktr_ots.tv_nsec;
440 break; 440 break;
441 case KTRFAC_VERSION(KTRFACv2): 441 case KTRFAC_VERSION(KTRFACv2):
442 temp.ts.tv_sec = kth->ktr_ts.tv_sec; 442 temp.ts.tv_sec = kth->ktr_ts.tv_sec;
443 temp.ts.tv_nsec = kth->ktr_ts.tv_nsec; 443 temp.ts.tv_nsec = kth->ktr_ts.tv_nsec;
444 break; 444 break;
445 default: 445 default:
446 badversion: 446 badversion:
447 err(1, "Unsupported ktrace version %x\n", 447 err(1, "Unsupported ktrace version %x\n",
448 kth->ktr_version); 448 kth->ktr_version);
449 } 449 }
450 } 450 }
451 if (kth->ktr_version == KTRFACv0) 451 if (kth->ktr_version == KTRFACv0)
452 col += printf("%lld.%06ld ", 452 col += printf("%lld.%06ld ",
453 (long long)temp.tv.tv_sec, (long)temp.tv.tv_usec); 453 (long long)temp.tv.tv_sec, (long)temp.tv.tv_usec);
454 else 454 else
455 col += printf("%lld.%09ld ", 455 col += printf("%lld.%09ld ",
456 (long long)temp.ts.tv_sec, (long)temp.ts.tv_nsec); 456 (long long)temp.ts.tv_sec, (long)temp.ts.tv_nsec);
457 } 457 }
458 col += printf("%-4s ", type); 458 col += printf("%-4s ", type);
459 return col; 459 return col;
460} 460}
461 461
462static void 462static void
463output_long(u_long it, int as_x) 463output_long(u_long it, int as_x)
464{ 464{
465 if (cur_emul->flags & EMUL_FLAG_NETBSD32) 465 if (cur_emul->flags & EMUL_FLAG_NETBSD32)
466 printf(as_x ? "%#x" : "%d", (u_int)it); 466 printf(as_x ? "%#x" : "%d", (u_int)it);
467 else 467 else
468 printf(as_x ? "%#lx" : "%ld", it); 468 printf(as_x ? "%#lx" : "%ld", it);
469} 469}
470 470
471static void 471static void
472ioctldecode(u_long cmd) 472ioctldecode(u_long cmd)
473{ 473{
474 char dirbuf[4], *dir = dirbuf; 474 char dirbuf[4], *dir = dirbuf;
475 int c; 475 int c;
476 476
477 if (cmd & IOC_IN) 477 if (cmd & IOC_IN)
478 *dir++ = 'W'; 478 *dir++ = 'W';
479 if (cmd & IOC_OUT) 479 if (cmd & IOC_OUT)
480 *dir++ = 'R'; 480 *dir++ = 'R';
481 *dir = '\0'; 481 *dir = '\0';
482 482
483 c = (cmd >> 8) & 0xff; 483 c = (cmd >> 8) & 0xff;
484 if (isprint(c)) 484 if (isprint(c))
485 printf("_IO%s('%c',", dirbuf, c); 485 printf("_IO%s('%c',", dirbuf, c);
486 else 486 else
487 printf("_IO%s(0x%02x,", dirbuf, c); 487 printf("_IO%s(0x%02x,", dirbuf, c);
488 output_long(cmd & 0xff, decimal == 0); 488 output_long(cmd & 0xff, decimal == 0);
489 if ((cmd & IOC_VOID) == 0) { 489 if ((cmd & IOC_VOID) == 0) {
490 putchar(','); 490 putchar(',');
491 output_long(IOCPARM_LEN(cmd), decimal == 0); 491 output_long(IOCPARM_LEN(cmd), decimal == 0);
492 } 492 }
493 putchar(')'); 493 putchar(')');
494} 494}
495 495
496static void 496static void
497ktrsyscall(struct ktr_syscall *ktr) 497ktrsyscall(struct ktr_syscall *ktr)
498{ 498{
499 int argcount; 499 int argcount;
500 const struct emulation *emul = cur_emul; 500 const struct emulation *emul = cur_emul;
501 register_t *ap; 501 register_t *ap;
502 char c; 502 char c;
503 const char *cp; 503 const char *cp;
504 const char *sys_name; 504 const char *sys_name;
505 505
506 argcount = ktr->ktr_argsize / sizeof (*ap); 506 argcount = ktr->ktr_argsize / sizeof (*ap);
507 507
508 emul_changed = 0; 508 emul_changed = 0;
509 509
510 if (numeric || 510 if (numeric ||
511 ((ktr->ktr_code >= emul->nsysnames || ktr->ktr_code < 0) && 511 ((ktr->ktr_code >= emul->nsysnames || ktr->ktr_code < 0) &&
512 mach_traps_dispatch(&ktr->ktr_code, &emul) == 0)) { 512 mach_traps_dispatch(&ktr->ktr_code, &emul) == 0)) {
513 sys_name = "?"; 513 sys_name = "?";
514 (void)printf("[%d]", ktr->ktr_code); 514 (void)printf("[%d]", ktr->ktr_code);
515 } else { 515 } else {
516 sys_name = emul->sysnames[ktr->ktr_code]; 516 sys_name = emul->sysnames[ktr->ktr_code];
517 (void)printf("%s", sys_name); 517 (void)printf("%s", sys_name);
518 } 518 }
519#ifdef _LP64 519#ifdef _LP64
520#define NETBSD32_ "netbsd32_" 520#define NETBSD32_ "netbsd32_"
521 if (cur_emul->flags & EMUL_FLAG_NETBSD32) { 521 if (cur_emul->flags & EMUL_FLAG_NETBSD32) {
522 size_t len = strlen(NETBSD32_); 522 size_t len = strlen(NETBSD32_);
523 if (strncmp(sys_name, NETBSD32_, len) == 0) 523 if (strncmp(sys_name, NETBSD32_, len) == 0)
524 sys_name += len; 524 sys_name += len;
525 } 525 }
526#undef NETBSD32_ 526#undef NETBSD32_
527#endif 527#endif
528 528
529 ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall)); 529 ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
530 if (argcount) { 530 if (argcount) {
531 c = '('; 531 c = '(';
532 if (plain) { 532 if (plain) {
533 ; 533 ;
534 534
535 } else if (strcmp(sys_name, "exit_group") == 0 || 535 } else if (strcmp(sys_name, "exit_group") == 0 ||
536 (strcmp(emul->name, "linux") != 0 && 536 (strcmp(emul->name, "linux") != 0 &&
537 strcmp(emul->name, "linux32") != 0 && 537 strcmp(emul->name, "linux32") != 0 &&
538 strcmp(sys_name, "exit") == 0)) { 538 strcmp(sys_name, "exit") == 0)) {
539 ectx_delete(); 539 ectx_delete();
540 540
541 } else if (strcmp(sys_name, "ioctl") == 0 && argcount >= 2) { 541 } else if (strcmp(sys_name, "ioctl") == 0 && argcount >= 2) {
542 (void)putchar('('); 542 (void)putchar('(');
543 output_long((long)*ap, !(decimal || small(*ap))); 543 output_long((long)*ap, !(decimal || small(*ap)));
544 ap++; 544 ap++;
545 argcount--; 545 argcount--;
546 if ((cp = ioctlname(*ap)) != NULL) 546 if ((cp = ioctlname(*ap)) != NULL)
547 (void)printf(",%s", cp); 547 (void)printf(",%s", cp);
548 else { 548 else {
549 (void)putchar(','); 549 (void)putchar(',');
550 ioctldecode(*ap); 550 ioctldecode(*ap);
551 } 551 }
552 ap++; 552 ap++;
553 argcount--; 553 argcount--;
554 c = ','; 554 c = ',';
555 555
556 } else if ((strstr(sys_name, "sigaction") != NULL || 556 } else if ((strstr(sys_name, "sigaction") != NULL ||
557 strstr(sys_name, "sigvec") != NULL) && argcount >= 1) { 557 strstr(sys_name, "sigvec") != NULL) && argcount >= 1) {
558 (void)printf("(SIG%s", signame(ap[0], 1)); 558 (void)printf("(SIG%s", signame(ap[0], 1));
559 ap += 1; 559 ap += 1;
560 argcount -= 1; 560 argcount -= 1;
561 c = ','; 561 c = ',';
562 562
563 } else if ((strcmp(sys_name, "kill") == 0 || 563 } else if ((strcmp(sys_name, "kill") == 0 ||
564 strcmp(sys_name, "killpg") == 0) && argcount >= 2) { 564 strcmp(sys_name, "killpg") == 0) && argcount >= 2) {
565 putchar('('); 565 putchar('(');
566 output_long((long)ap[0], !(decimal || small(*ap))); 566 output_long((long)ap[0], !(decimal || small(*ap)));
567 (void)printf(", SIG%s", signame(ap[1], 1)); 567 (void)printf(", SIG%s", signame(ap[1], 1));
568 ap += 2; 568 ap += 2;
569 argcount -= 2; 569 argcount -= 2;
570 c = ','; 570 c = ',';
571 571
572 } else if (strcmp(sys_name, "ptrace") == 0 && argcount >= 1) { 572 } else if (strcmp(sys_name, "ptrace") == 0 && argcount >= 1) {
573 putchar('('); 573 putchar('(');
574 if (strcmp(emul->name, "linux") == 0 || 574 if (strcmp(emul->name, "linux") == 0 ||
575 strcmp(emul->name, "linux32") == 0) { 575 strcmp(emul->name, "linux32") == 0) {
576 if ((long)*ap >= 0 && *ap < 576 if ((long)*ap >= 0 && *ap <
577 (register_t)(sizeof(linux_ptrace_ops) / 577 (register_t)(sizeof(linux_ptrace_ops) /
578 sizeof(linux_ptrace_ops[0]))) 578 sizeof(linux_ptrace_ops[0])))
579 (void)printf("%s", 579 (void)printf("%s",
580 linux_ptrace_ops[*ap]); 580 linux_ptrace_ops[*ap]);
581 else 581 else
582 output_long((long)*ap, 1); 582 output_long((long)*ap, 1);
583 } else { 583 } else {
584 if ((long)*ap >= 0 && *ap < 584 if ((long)*ap >= 0 && *ap <
585 (register_t)(sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))) 585 (register_t)(sizeof(ptrace_ops) / sizeof(ptrace_ops[0])))
586 (void)printf("%s", ptrace_ops[*ap]); 586 (void)printf("%s", ptrace_ops[*ap]);
587#ifdef PT_MACHDEP_STRINGS 587#ifdef PT_MACHDEP_STRINGS
588 else if (*ap >= PT_FIRSTMACH && 588 else if (*ap >= PT_FIRSTMACH &&
589 *ap - PT_FIRSTMACH < 589 *ap - PT_FIRSTMACH <
590 (register_t)(sizeof(ptrace_machdep_ops) / 590 (register_t)(sizeof(ptrace_machdep_ops) /
591 sizeof(ptrace_machdep_ops[0]))) 591 sizeof(ptrace_machdep_ops[0])))
592 (void)printf("%s", ptrace_machdep_ops[*ap - PT_FIRSTMACH]); 592 (void)printf("%s", ptrace_machdep_ops[*ap - PT_FIRSTMACH]);
593#endif 593#endif
594 else 594 else
595 output_long((long)*ap, 1); 595 output_long((long)*ap, 1);
596 } 596 }
597 ap++; 597 ap++;
598 argcount--; 598 argcount--;
599 c = ','; 599 c = ',';
600 600
601 } 601 }
602 while (argcount > 0) { 602 while (argcount > 0) {
603 putchar(c); 603 putchar(c);
604 output_long((long)*ap, !(decimal || small(*ap))); 604 output_long((long)*ap, !(decimal || small(*ap)));
605 ap++; 605 ap++;
606 argcount--; 606 argcount--;
607 c = ','; 607 c = ',';
608 } 608 }
609 (void)putchar(')'); 609 (void)putchar(')');
610 } 610 }
611 (void)putchar('\n'); 611 (void)putchar('\n');
612} 612}
613 613
614static void 614static void
615ktrsysret(struct ktr_sysret *ktr, int len) 615ktrsysret(struct ktr_sysret *ktr, int len)
616{ 616{
617 const struct emulation *emul; 617 const struct emulation *emul;
618 int error = ktr->ktr_error; 618 int error = ktr->ktr_error;
619 int code = ktr->ktr_code; 619 int code = ktr->ktr_code;
620 620
621 if (emul_changed) { 621 if (emul_changed) {
622 /* In order to get system call name right in execve return */ 622 /* In order to get system call name right in execve return */
623 emul = prev_emul; 623 emul = prev_emul;
624 emul_changed = 0; 624 emul_changed = 0;
625 } else 625 } else
626 emul = cur_emul; 626 emul = cur_emul;
627 627
628 if (numeric || ((code >= emul->nsysnames || code < 0 || plain > 1) && 628 if (numeric || ((code >= emul->nsysnames || code < 0 || plain > 1) &&
629 mach_traps_dispatch(&code, &emul) == 0)) 629 mach_traps_dispatch(&code, &emul) == 0))
630 (void)printf("[%d] ", code); 630 (void)printf("[%d] ", code);
631 else 631 else
632 (void)printf("%s ", emul->sysnames[code]); 632 (void)printf("%s ", emul->sysnames[code]);
633 633
634 switch (error) { 634 switch (error) {
635 case 0: 635 case 0:
636 rprint(ktr->ktr_retval); 636 rprint(ktr->ktr_retval);
637 if (len > (int)offsetof(struct ktr_sysret, ktr_retval_1) && 637 if (len > (int)offsetof(struct ktr_sysret, ktr_retval_1) &&
638 ktr->ktr_retval_1 != 0) { 638 ktr->ktr_retval_1 != 0) {
639 (void)printf(", "); 639 (void)printf(", ");
640 rprint(ktr->ktr_retval_1); 640 rprint(ktr->ktr_retval_1);
641 } 641 }
642 break; 642 break;
643 643
644 default: 644 default:
645 eprint(error); 645 eprint(error);
646 break; 646 break;
647 } 647 }
648 (void)putchar('\n'); 648 (void)putchar('\n');
649} 649}
650 650
651static void 651static void
652rprint(register_t ret) 652rprint(register_t ret)
653{ 653{
654 654
655 if (!plain) { 655 if (!plain) {
656 (void)printf("%ld", (long)ret); 656 (void)printf("%ld", (long)ret);
657 if (!small(ret)) 657 if (!small(ret))
658 (void)printf("/%#lx", (long)ret); 658 (void)printf("/%#lx", (long)ret);
659 } else { 659 } else {
660 if (decimal || small(ret)) 660 if (decimal || small(ret))
661 (void)printf("%ld", (long)ret); 661 (void)printf("%ld", (long)ret);
662 else 662 else
663 (void)printf("%#lx", (long)ret); 663 (void)printf("%#lx", (long)ret);
664 } 664 }
665} 665}
666 666
667/* 667/*
668 * We print the original emulation's error numerically, but we 668 * We print the original emulation's error numerically, but we
669 * translate it to netbsd to print it symbolically. 669 * translate it to netbsd to print it symbolically.
670 */ 670 */
671static void 671static void
672eprint(int e) 672eprint(int e)
673{ 673{
674 int i = e; 674 int i = e;
675 675
676 if (cur_emul->errnomap) { 676 if (cur_emul->errnomap) {
677 677
678 /* No remapping for ERESTART and EJUSTRETURN */ 678 /* No remapping for ERESTART and EJUSTRETURN */
679 /* Kludge for linux that has negative error numbers */ 679 /* Kludge for linux that has negative error numbers */
680 if (cur_emul->errnomap[2] > 0 && e < 0) 680 if (cur_emul->errnomap[2] > 0 && e < 0)
681 goto normal; 681 goto normal;
682 682
683 for (i = 0; i < cur_emul->nerrnomap; i++) 683 for (i = 0; i < cur_emul->nerrnomap; i++)
684 if (e == cur_emul->errnomap[i]) 684 if (e == cur_emul->errnomap[i])
685 break; 685 break;
686 686
687 if (i == cur_emul->nerrnomap) { 687 if (i == cur_emul->nerrnomap) {
688 printf("-1 unknown errno %d", e); 688 printf("-1 unknown errno %d", e);
689 return; 689 return;
690 } 690 }
691 } 691 }
692 692
693normal: 693normal:
694 switch (i) { 694 switch (i) {
695 case ERESTART: 695 case ERESTART:
696 (void)printf("RESTART"); 696 (void)printf("RESTART");
697 break; 697 break;
698 698
699 case EJUSTRETURN: 699 case EJUSTRETURN:
700 (void)printf("JUSTRETURN"); 700 (void)printf("JUSTRETURN");
701 break; 701 break;
702 702
703 default: 703 default:
704 (void)printf("-1 errno %d", e); 704 (void)printf("-1 errno %d", e);
705 if (!plain) 705 if (!plain)
706 (void)printf(" %s", strerror(i)); 706 (void)printf(" %s", strerror(i));
707 } 707 }
708} 708}
709 709
710static void 710static void
711ktrnamei(char *cp, int len) 711ktrnamei(char *cp, int len)
712{ 712{
713 713
714 (void)printf("\"%.*s\"\n", len, cp); 714 (void)printf("\"%.*s\"\n", len, cp);
715} 715}
716 716
717static void 717static void
718ktremul(char *name, int len, int bufsize) 718ktremul(char *name, int len, int bufsize)
719{ 719{
720 720
721 if (len >= bufsize) 721 if (len >= bufsize)
722 len = bufsize - 1; 722 len = bufsize - 1;
723 723
724 name[len] = '\0'; 724 name[len] = '\0';
725 setemul(name, ktr_header.ktr_pid, 1); 725 setemul(name, ktr_header.ktr_pid, 1);
726 emul_changed = 1; 726 emul_changed = 1;
727 727
728 (void)printf("\"%s\"\n", name); 728 (void)printf("\"%s\"\n", name);
729} 729}
730 730
731static void 731static void
732hexdump_buf(const void *vdp, int datalen, int word_sz) 732hexdump_buf(const void *vdp, int datalen, int word_sz)
733{ 733{
734 const char hex[] = "0123456789abcdef"; 734 const char hex[] = "0123456789abcdef";
735 char chars[16], prev[16]; 735 char chars[16], prev[16];
736 char bytes[16 * 3 + 4]; 736 char bytes[16 * 3 + 4];
737 const unsigned char *dp = vdp; 737 const unsigned char *dp = vdp;
738 const unsigned char *datalim = dp + datalen; 738 const unsigned char *datalim = dp + datalen;
739 const unsigned char *line_end; 739 const unsigned char *line_end;
740 int off, l = 0, c; 740 int off, l = 0, c;
741 char *cp, *bp; 741 char *cp, *bp;
742 int divmask = word_sz - 1; /* block size in bytes */ 742 int divmask = word_sz - 1; /* block size in bytes */
743 int gdelim = 3; /* gap between blocks */ 743 int gdelim = 3; /* gap between blocks */
744 int bsize = 2; /* increment for each byte */ 744 int bsize = 2; /* increment for each byte */
745 int width; 745 int width;
746 int dupl = 0; 746 int dupl = 0;
747#if _BYTE_ORDER == _LITTLE_ENDIAN 747#if _BYTE_ORDER == _LITTLE_ENDIAN
748 int bswap = word_sz - 1; 748 int bswap = word_sz - 1;
749#else 749#else
750#define bswap 0 750#define bswap 0
751#endif 751#endif
752 752
753 switch (word_sz) { 753 switch (word_sz) {
754 case 2: 754 case 2:
755 gdelim = 2; 755 gdelim = 2;
756 break; 756 break;
757 case 1: 757 case 1:
758 divmask = 7; 758 divmask = 7;
759 bsize = 3; 759 bsize = 3;
760 gdelim = 1; 760 gdelim = 1;
761 break; 761 break;
762 default: 762 default:
763 break; 763 break;
764 } 764 }
765 width = 16 * bsize + (16 / (divmask + 1)) * gdelim; 765 width = 16 * bsize + (16 / (divmask + 1)) * gdelim;
766 if (word_sz != 1) 766 if (word_sz != 1)
767 width += 2; 767 width += 2;
768 768
769 for (off = 0; dp < datalim; off += l) { 769 for (off = 0; dp < datalim; off += l) {
770 memset(bytes, ' ', sizeof bytes); 770 memset(bytes, ' ', sizeof bytes);
771 line_end = dp + 16; 771 line_end = dp + 16;
772 if (line_end >= datalim) { 772 if (line_end >= datalim) {
773 line_end = datalim; 773 line_end = datalim;
774 dupl |= 1; /* need to print */ 774 dupl |= 1; /* need to print */
775 } else { 775 } else {
776 if (dupl == 0 || memcmp(dp, prev, sizeof chars)) 776 if (dupl == 0 || memcmp(dp, prev, sizeof chars))
777 dupl |= 1; 777 dupl |= 1;
778 } 778 }
779 779
780 if (!(dupl & 1)) { 780 if (!(dupl & 1)) {
781 /* This is a duplicate of the line above, count 'em */ 781 /* This is a duplicate of the line above, count 'em */
782 dupl += 2; 782 dupl += 2;
783 dp = line_end; 783 dp = line_end;
784 continue; 784 continue;
785 } 785 }
786 786
787 if (dupl > 3) { 787 if (dupl > 3) {
788 /* previous line as a duplicate */ 788 /* previous line as a duplicate */
789 if (dupl == 5) 789 if (dupl == 5)
790 /* Only one duplicate, print line */ 790 /* Only one duplicate, print line */
791 printf("\t%-5.3x%.*s%.*s\n", 791 printf("\t%-5.3x%.*s%.*s\n",
792 off - l, width, bytes, l, chars); 792 off - l, width, bytes, l, chars);
793 else 793 else
794 printf("\t%.*s\n", 794 printf("\t%.*s\n",
795 snprintf(NULL, 0, "%3x", off), "*****"); 795 snprintf(NULL, 0, "%3x", off), "*****");
796 } 796 }
797 797
798 for (l = 0, bp = bytes, cp = chars; dp < line_end; l++) { 798 for (l = 0, bp = bytes, cp = chars; dp < line_end; l++) {
799 c = *dp++; 799 c = *dp++;
800 prev[l] = c; 800 prev[l] = c;
801 if ((l & divmask) == 0) 801 if ((l & divmask) == 0)
802 bp += gdelim; 802 bp += gdelim;
803 bp[(l ^ bswap) * bsize] = hex[c >> 4]; 803 bp[(l ^ bswap) * bsize] = hex[c >> 4];
804 bp[(l ^ bswap) * bsize + 1] = hex[c & 0xf]; 804 bp[(l ^ bswap) * bsize + 1] = hex[c & 0xf];
805 *cp++ = isgraph(c) ? c : '.'; 805 *cp++ = isgraph(c) ? c : '.';
806 } 806 }
807 807
808 printf("\t%-5.3x%.*s%.*s\n", off, width, bytes, l, chars); 808 printf("\t%-5.3x%.*s%.*s\n", off, width, bytes, l, chars);
809 dupl = 2; 809 dupl = 2;
810 } 810 }
811} 811}
812 812
813static void 813static void
814visdump_buf(const void *vdp, int datalen, int col) 814visdump_buf(const void *vdp, int datalen, int col)
815{ 815{
816 const unsigned char *dp = vdp; 816 const unsigned char *dp = vdp;
817 char *cp; 817 char *cp;
818 int width; 818 int width;
819 char visbuf[5]; 819 char visbuf[5];
820 static int screenwidth = 0; 820 static int screenwidth = 0;
821 821
822 if (screenwidth == 0) { 822 if (screenwidth == 0) {
823 struct winsize ws; 823 struct winsize ws;
824 824
825 if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 && 825 if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
826 ws.ws_col > 8) 826 ws.ws_col > 8)
827 screenwidth = ws.ws_col; 827 screenwidth = ws.ws_col;
828 else 828 else
829 screenwidth = 80; 829 screenwidth = 80;
830 } 830 }
831 831
832 (void)printf("\""); 832 (void)printf("\"");
833 col++; 833 col++;
834 for (; datalen > 0; datalen--, dp++) { 834 for (; datalen > 0; datalen--, dp++) {
835 (void)svis(visbuf, *dp, VIS_CSTYLE, 835 (void)svis(visbuf, *dp, VIS_CSTYLE,
836 datalen > 1 ? *(dp + 1) : 0, "\"\n"); 836 datalen > 1 ? *(dp + 1) : 0, "\"\n");
837 cp = visbuf; 837 cp = visbuf;
838 /* 838 /*
839 * Keep track of printables and 839 * Keep track of printables and
840 * space chars (like fold(1)). 840 * space chars (like fold(1)).
841 */ 841 */
842 if (col == 0) { 842 if (col == 0) {
843 (void)putchar('\t'); 843 (void)putchar('\t');
844 col = 8; 844 col = 8;
845 } 845 }
846 switch (*cp) { 846 switch (*cp) {
847 case '\n': 847 case '\n':
848 col = 0; 848 col = 0;
849 (void)putchar('\n'); 849 (void)putchar('\n');
850 continue; 850 continue;
851 case '\t': 851 case '\t':
852 width = 8 - (col & 07); 852 width = 8 - (col & 07);
853 break; 853 break;
854 default: 854 default:
855 width = strlen(cp); 855 width = strlen(cp);
856 } 856 }
857 if (col + width > (screenwidth - 2)) { 857 if (col + width > (screenwidth - 2)) {
858 (void)printf("\\\n\t"); 858 (void)printf("\\\n\t");
859 col = 8; 859 col = 8;
860 if (*cp == '\t') 860 if (*cp == '\t')
861 width = 8; 861 width = 8;
862 } 862 }
863 col += width; 863 col += width;
864 do { 864 do {
865 (void)putchar(*cp++); 865 (void)putchar(*cp++);
866 } while (*cp); 866 } while (*cp);
867 } 867 }
868 if (col == 0) 868 if (col == 0)
869 (void)printf(" "); 869 (void)printf(" ");
870 (void)printf("\"\n"); 870 (void)printf("\"\n");
871} 871}
872 872
873static void 873static void
874ktrgenio(struct ktr_genio *ktr, int len) 874ktrgenio(struct ktr_genio *ktr, int len)
875{ 875{
876 int datalen = len - sizeof (struct ktr_genio); 876 int datalen = len - sizeof (struct ktr_genio);
877 char *dp = (char *)ktr + sizeof (struct ktr_genio); 877 char *dp = (char *)ktr + sizeof (struct ktr_genio);
878 878
879 if (ktr->ktr_fd != -1) 879 if (ktr->ktr_fd != -1)
880 printf("fd %d ", ktr->ktr_fd); 880 printf("fd %d ", ktr->ktr_fd);
881 printf("%s %d bytes\n",  881 printf("%s %d bytes\n",
882 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen); 882 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
883 if (maxdata == 0) 883 if (maxdata == 0)
884 return; 884 return;
885 if (maxdata > 0 && datalen > maxdata) 885 if (maxdata > 0 && datalen > maxdata)
886 datalen = maxdata; 886 datalen = maxdata;
887 if (word_size) { 887 if (word_size) {
888 hexdump_buf(dp, datalen, word_size); 888 hexdump_buf(dp, datalen, word_size);
889 return; 889 return;
890 } 890 }
891 (void)printf(" "); 891 (void)printf(" ");
892 visdump_buf(dp, datalen, 7); 892 visdump_buf(dp, datalen, 7);
893} 893}
894 894
895static void 895static void
896ktrpsig(void *v, int len) 896ktrpsig(void *v, int len)
897{ 897{
898 int signo, first; 898 int signo, first;
899 struct { 899 struct {
900 struct ktr_psig ps; 900 struct ktr_psig ps;
901 siginfo_t si; 901 siginfo_t si;
902 } *psig = v; 902 } *psig = v;
903 siginfo_t *si = &psig->si; 903 siginfo_t *si = &psig->si;
904 const char *code; 904 const char *code;
905 905
906 (void)printf("SIG%s ", signame(psig->ps.signo, 0)); 906 (void)printf("SIG%s ", signame(psig->ps.signo, 0));
907 if (psig->ps.action == SIG_DFL) 907 if (psig->ps.action == SIG_DFL)
908 (void)printf("SIG_DFL"); 908 (void)printf("SIG_DFL");
909 else { 909 else {
910 (void)printf("caught handler=%p mask=(", psig->ps.action); 910 (void)printf("caught handler=%p mask=(", psig->ps.action);
911 first = 1; 911 first = 1;
912 for (signo = 1; signo < NSIG; signo++) { 912 for (signo = 1; signo < NSIG; signo++) {
913 if (sigismember(&psig->ps.mask, signo)) { 913 if (sigismember(&psig->ps.mask, signo)) {
914 if (first) 914 if (first)
915 first = 0; 915 first = 0;
916 else 916 else
917 (void)printf(","); 917 (void)printf(",");
918 (void)printf("%d", signo); 918 (void)printf("%d", signo);
919 } 919 }
920 } 920 }
921 (void)printf(")"); 921 (void)printf(")");
922 } 922 }
923 switch (len) { 923 switch (len) {
924 case sizeof(struct ktr_psig): 924 case sizeof(struct ktr_psig):
925 if (psig->ps.code) 925 if (psig->ps.code)
926 printf(" code=0x%x", psig->ps.code); 926 printf(" code=0x%x", psig->ps.code);
927 printf(psig->ps.action == SIG_DFL ? "\n" : ")\n"); 927 printf(psig->ps.action == SIG_DFL ? "\n" : ")\n");
928 return; 928 return;
929 case sizeof(*psig): 929 case sizeof(*psig):
930 if (si->si_code == 0) { 930 if (si->si_code == 0) {
931 printf(": code=SI_USER sent by pid=%d, uid=%d)\n", 931 printf(": code=SI_USER sent by pid=%d, uid=%d)\n",
932 si->si_pid, si->si_uid); 932 si->si_pid, si->si_uid);
933 return; 933 return;
934 } 934 }
935 935
936 if (si->si_code < 0) { 936 if (si->si_code < 0) {
937 switch (si->si_code) { 937 switch (si->si_code) {
938 case SI_TIMER: 938 case SI_TIMER:
939 printf(": code=SI_TIMER sigval %p)\n", 
940 si->si_value.sival_ptr); 
941 return; 
942 case SI_QUEUE: 939 case SI_QUEUE:
943 code = "SI_QUEUE"; 940 printf(": code=%s sent by pid=%d, uid=%d with "
944 break; 941 "sigval %p)\n", si->si_code == SI_TIMER ?
 942 "SI_TIMER" : "SI_QUEUE", si->si_pid,
 943 si->si_uid, si->si_value.sival_ptr);
 944 return;
945 case SI_ASYNCIO: 945 case SI_ASYNCIO:
946 code = "SI_ASYNCIO"; 946 code = "SI_ASYNCIO";
947 break; 947 break;
948 case SI_MESGQ: 948 case SI_MESGQ:
949 code = "SI_MESGQ"; 949 code = "SI_MESGQ";
950 break; 950 break;
951 case SI_LWP: 951 case SI_LWP:
952 code = "SI_LWP"; 952 code = "SI_LWP";
953 break; 953 break;
954 default: 954 default:
955 code = NULL; 955 code = NULL;
956 break; 956 break;
957 } 957 }
958 if (code) 958 if (code)
959 printf(": code=%s unimplemented)\n", code); 959 printf(": code=%s unimplemented)\n", code);
960 else 960 else
961 printf(": code=%d unimplemented)\n", 961 printf(": code=%d unimplemented)\n",
962 si->si_code); 962 si->si_code);
963 return; 963 return;
964 } 964 }
965 965
966 if (si->si_code == SI_NOINFO) { 966 if (si->si_code == SI_NOINFO) {
967 printf(": code=SI_NOINFO\n"); 967 printf(": code=SI_NOINFO\n");
968 return; 968 return;
969 } 969 }
970 970
971 code = siginfocodename(si->si_signo, si->si_code); 971 code = siginfocodename(si->si_signo, si->si_code);
972 switch (si->si_signo) { 972 switch (si->si_signo) {
973 case SIGCHLD: 973 case SIGCHLD:
974 printf(": code=%s child pid=%d, uid=%d, " 974 printf(": code=%s child pid=%d, uid=%d, "
975 " status=%u, utime=%lu, stime=%lu)\n", 975 " status=%u, utime=%lu, stime=%lu)\n",
976 code, si->si_pid, 976 code, si->si_pid,
977 si->si_uid, si->si_status, 977 si->si_uid, si->si_status,
978 (unsigned long) si->si_utime, 978 (unsigned long) si->si_utime,
979 (unsigned long) si->si_stime); 979 (unsigned long) si->si_stime);
980 return; 980 return;
981 case SIGILL: 981 case SIGILL:
982 case SIGFPE: 982 case SIGFPE:
983 case SIGSEGV: 983 case SIGSEGV:
984 case SIGBUS: 984 case SIGBUS:
985 case SIGTRAP: 985 case SIGTRAP:
986 printf(": code=%s, addr=%p, trap=%d)\n", 986 printf(": code=%s, addr=%p, trap=%d)\n",
987 code, si->si_addr, si->si_trap); 987 code, si->si_addr, si->si_trap);
988 return; 988 return;
989 case SIGIO: 989 case SIGIO:
990 printf(": code=%s, fd=%d, band=%lx)\n", 990 printf(": code=%s, fd=%d, band=%lx)\n",
991 code, si->si_fd, si->si_band); 991 code, si->si_fd, si->si_band);
992 return; 992 return;
993 default: 993 default:
994 printf(": code=%s, errno=%d)\n", 994 printf(": code=%s, errno=%d)\n",
995 code, si->si_errno); 995 code, si->si_errno);
996 return; 996 return;
997 } 997 }
998 /*NOTREACHED*/ 998 /*NOTREACHED*/
999 default: 999 default:
1000 warnx("Unhandled size %d for ktrpsig\n", len); 1000 warnx("Unhandled size %d for ktrpsig\n", len);
1001 break; 1001 break;
1002 } 1002 }
1003} 1003}
1004 1004
1005static void 1005static void
1006ktrcsw(struct ktr_csw *cs) 1006ktrcsw(struct ktr_csw *cs)
1007{ 1007{
1008 1008
1009 (void)printf("%s %s\n", cs->out ? "stop" : "resume", 1009 (void)printf("%s %s\n", cs->out ? "stop" : "resume",
1010 cs->user ? "user" : "kernel"); 1010 cs->user ? "user" : "kernel");
1011} 1011}
1012 1012
1013static void 1013static void
1014ktruser(struct ktr_user *usr, int len) 1014ktruser(struct ktr_user *usr, int len)
1015{ 1015{
1016 int i; 1016 int i;
1017 unsigned char *dta; 1017 unsigned char *dta;
1018 1018
1019 len -= sizeof(struct ktr_user); 1019 len -= sizeof(struct ktr_user);
1020 printf("%.*s:", KTR_USER_MAXIDLEN, usr->ktr_id); 1020 printf("%.*s:", KTR_USER_MAXIDLEN, usr->ktr_id);
1021 dta = (unsigned char *)(usr + 1); 1021 dta = (unsigned char *)(usr + 1);
1022 if (word_size) { 1022 if (word_size) {
1023 printf("\n"); 1023 printf("\n");
1024 hexdump_buf(dta, len, word_size); 1024 hexdump_buf(dta, len, word_size);
1025 return; 1025 return;
1026 } 1026 }
1027 printf(" %d, ", len); 1027 printf(" %d, ", len);
1028 for (i = 0; i < len; i++) 1028 for (i = 0; i < len; i++)
1029 printf("%02x", (unsigned int) dta[i]); 1029 printf("%02x", (unsigned int) dta[i]);
1030 printf("\n"); 1030 printf("\n");
1031} 1031}
1032 1032
1033static void 1033static void
1034ktrmmsg(struct ktr_mmsg *mmsg, int len) 1034ktrmmsg(struct ktr_mmsg *mmsg, int len)
1035{ 1035{
1036 const char *service_name; 1036 const char *service_name;
1037 const char *reply; 1037 const char *reply;
1038 int id; 1038 int id;
1039 1039
1040 id = mmsg->ktr_id; 1040 id = mmsg->ktr_id;
1041 if ((id / 100) % 2) { /* Message reply */ 1041 if ((id / 100) % 2) { /* Message reply */
1042 reply = " reply"; 1042 reply = " reply";
1043 id -= 100; 1043 id -= 100;
1044 } else { 1044 } else {
1045 reply = ""; 1045 reply = "";
1046 } 1046 }
1047 1047
1048 if ((service_name = mach_service_name(id)) != NULL) 1048 if ((service_name = mach_service_name(id)) != NULL)
1049 printf("%s%s [%d]\n", service_name, reply, mmsg->ktr_id); 1049 printf("%s%s [%d]\n", service_name, reply, mmsg->ktr_id);
1050 else 1050 else
1051 printf("unknown service%s [%d]\n", reply, mmsg->ktr_id); 1051 printf("unknown service%s [%d]\n", reply, mmsg->ktr_id);
1052 1052
1053 hexdump_buf(mmsg, len, word_size ? word_size : 4); 1053 hexdump_buf(mmsg, len, word_size ? word_size : 4);
1054} 1054}
1055 1055
1056static void 1056static void
1057ktrmool(struct ktr_mool *mool, int len) 1057ktrmool(struct ktr_mool *mool, int len)
1058{ 1058{
1059 size_t size = mool->size; 1059 size_t size = mool->size;
1060 1060
1061 printf("%ld/0x%lx bytes at %p\n", 1061 printf("%ld/0x%lx bytes at %p\n",
1062 (u_long)size, (u_long)size, mool->uaddr); 1062 (u_long)size, (u_long)size, mool->uaddr);
1063 mool++; 1063 mool++;
1064 hexdump_buf(mool, size, word_size ? word_size : 4); 1064 hexdump_buf(mool, size, word_size ? word_size : 4);
1065} 1065}
1066 1066
1067static void 1067static void
1068ktrmib(int *namep, int len) 1068ktrmib(int *namep, int len)
1069{ 1069{
1070 size_t i; 1070 size_t i;
1071 1071
1072 for (i = 0; i < (len / sizeof(*namep)); i++) 1072 for (i = 0; i < (len / sizeof(*namep)); i++)
1073 printf("%s%d", (i == 0) ? "" : ".", namep[i]); 1073 printf("%s%d", (i == 0) ? "" : ".", namep[i]);
1074 printf("\n"); 1074 printf("\n");
1075} 1075}
1076 1076
1077static const char * 1077static const char *
1078signame(long sig, int xlat) 1078signame(long sig, int xlat)
1079{ 1079{
1080 static char buf[64]; 1080 static char buf[64];
1081 1081
1082 if (sig == 0) 1082 if (sig == 0)
1083 return " 0"; 1083 return " 0";
1084 else if (sig < 0 || sig >= NSIG) { 1084 else if (sig < 0 || sig >= NSIG) {
1085 (void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig); 1085 (void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig);
1086 return buf; 1086 return buf;
1087 } else 1087 } else
1088 return sys_signame[(xlat && cur_emul->signalmap != NULL) ? 1088 return sys_signame[(xlat && cur_emul->signalmap != NULL) ?
1089 cur_emul->signalmap[sig] : sig]; 1089 cur_emul->signalmap[sig] : sig];
1090} 1090}
1091 1091
1092static void 1092static void
1093usage(void) 1093usage(void)
1094{ 1094{
1095 if (strcmp(getprogname(), "ioctlname") == 0) { 1095 if (strcmp(getprogname(), "ioctlname") == 0) {
1096 (void)fprintf(stderr, "Usage: %s [-e emulation] <ioctl> ...\n", 1096 (void)fprintf(stderr, "Usage: %s [-e emulation] <ioctl> ...\n",
1097 getprogname()); 1097 getprogname());
1098 } else { 1098 } else {
1099 (void)fprintf(stderr, "Usage: %s [-dlNnRT] [-e emulation] " 1099 (void)fprintf(stderr, "Usage: %s [-dlNnRT] [-e emulation] "
1100 "[-f file] [-m maxdata] [-p pid]\n [-t trstr] " 1100 "[-f file] [-m maxdata] [-p pid]\n [-t trstr] "
1101 "[-x | -X size] [file]\n", getprogname()); 1101 "[-x | -X size] [file]\n", getprogname());
1102 } 1102 }
1103 exit(1); 1103 exit(1);
1104} 1104}