Fri Jan 2 13:57:24 2009 UTC ()
Add minimal SI_LWP support.


(njoly)
diff -r1.99 -r1.100 src/usr.bin/kdump/kdump.c

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

--- src/usr.bin/kdump/kdump.c 2008/09/12 20:46:42 1.99
+++ src/usr.bin/kdump/kdump.c 2009/01/02 13:57:23 1.100
@@ -1,1034 +1,1037 @@ @@ -1,1034 +1,1037 @@
1/* $NetBSD: kdump.c,v 1.99 2008/09/12 20:46:42 christos Exp $ */ 1/* $NetBSD: kdump.c,v 1.100 2009/01/02 13:57:23 njoly 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.99 2008/09/12 20:46:42 christos Exp $"); 42__RCSID("$NetBSD: kdump.c,v 1.100 2009/01/02 13:57:23 njoly 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 while ((ch = getopt(argc, argv, "e:f:dlm:Nnp:RTt:xX:")) != -1) { 139 while ((ch = getopt(argc, argv, "e:f:dlm:Nnp:RTt:xX:")) != -1) {
140 switch (ch) { 140 switch (ch) {
141 case 'e': 141 case 'e':
142 emul_name = strdup(optarg); /* it's safer to copy it */ 142 emul_name = strdup(optarg); /* it's safer to copy it */
143 break; 143 break;
144 case 'f': 144 case 'f':
145 tracefile = optarg; 145 tracefile = optarg;
146 break; 146 break;
147 case 'd': 147 case 'd':
148 decimal = 1; 148 decimal = 1;
149 break; 149 break;
150 case 'l': 150 case 'l':
151 tail = 1; 151 tail = 1;
152 break; 152 break;
153 case 'p': 153 case 'p':
154 do_pid = strtoul(optarg, &cp, 0); 154 do_pid = strtoul(optarg, &cp, 0);
155 if (*cp != 0) 155 if (*cp != 0)
156 errx(1,"invalid number %s", optarg); 156 errx(1,"invalid number %s", optarg);
157 break; 157 break;
158 case 'm': 158 case 'm':
159 maxdata = strtoul(optarg, &cp, 0); 159 maxdata = strtoul(optarg, &cp, 0);
160 if (*cp != 0) 160 if (*cp != 0)
161 errx(1,"invalid number %s", optarg); 161 errx(1,"invalid number %s", optarg);
162 break; 162 break;
163 case 'N': 163 case 'N':
164 numeric++; 164 numeric++;
165 break; 165 break;
166 case 'n': 166 case 'n':
167 plain++; 167 plain++;
168 break; 168 break;
169 case 'R': 169 case 'R':
170 timestamp = 2; /* relative timestamp */ 170 timestamp = 2; /* relative timestamp */
171 break; 171 break;
172 case 'T': 172 case 'T':
173 timestamp = 1; 173 timestamp = 1;
174 break; 174 break;
175 case 't': 175 case 't':
176 trset = 1; 176 trset = 1;
177 trpoints = getpoints(trpoints, optarg); 177 trpoints = getpoints(trpoints, optarg);
178 if (trpoints < 0) 178 if (trpoints < 0)
179 errx(1, "unknown trace point in %s", optarg); 179 errx(1, "unknown trace point in %s", optarg);
180 break; 180 break;
181 case 'x': 181 case 'x':
182 word_size = 1; 182 word_size = 1;
183 break; 183 break;
184 case 'X': 184 case 'X':
185 word_size = strtoul(optarg, &cp, 0); 185 word_size = strtoul(optarg, &cp, 0);
186 if (*cp != 0 || word_size & (word_size - 1) || 186 if (*cp != 0 || word_size & (word_size - 1) ||
187 word_size > 16 || word_size <= 0) 187 word_size > 16 || word_size <= 0)
188 errx(1, "argument to -X must be " 188 errx(1, "argument to -X must be "
189 "1, 2, 4, 8 or 16"); 189 "1, 2, 4, 8 or 16");
190 break; 190 break;
191 default: 191 default:
192 usage(); 192 usage();
193 } 193 }
194 } 194 }
195 argv += optind; 195 argv += optind;
196 argc -= optind; 196 argc -= optind;
197 197
198 if (!trset) 198 if (!trset)
199 trpoints = ALL_POINTS; 199 trpoints = ALL_POINTS;
200 200
201 if (tracefile == NULL) { 201 if (tracefile == NULL) {
202 if (argc == 1) { 202 if (argc == 1) {
203 tracefile = argv[0]; 203 tracefile = argv[0];
204 argv++; 204 argv++;
205 argc--; 205 argc--;
206 } else 206 } else
207 tracefile = DEF_TRACEFILE; 207 tracefile = DEF_TRACEFILE;
208 } 208 }
209 209
210 if (argc > 0) 210 if (argc > 0)
211 usage(); 211 usage();
212 212
213 setemul(emul_name, 0, 0); 213 setemul(emul_name, 0, 0);
214 mach_lookup_emul(); 214 mach_lookup_emul();
215 215
216 m = malloc(size = 1024); 216 m = malloc(size = 1024);
217 if (m == NULL) 217 if (m == NULL)
218 errx(1, "malloc: %s", strerror(ENOMEM)); 218 errx(1, "malloc: %s", strerror(ENOMEM));
219 if (!freopen(tracefile, "r", stdin)) 219 if (!freopen(tracefile, "r", stdin))
220 err(1, "%s", tracefile); 220 err(1, "%s", tracefile);
221 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) { 221 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
222 if (trpoints & (1 << ktr_header.ktr_type) && 222 if (trpoints & (1 << ktr_header.ktr_type) &&
223 (do_pid == -1 || ktr_header.ktr_pid == do_pid)) 223 (do_pid == -1 || ktr_header.ktr_pid == do_pid))
224 col = dumpheader(&ktr_header); 224 col = dumpheader(&ktr_header);
225 else 225 else
226 col = -1; 226 col = -1;
227 if ((ktrlen = ktr_header.ktr_len) < 0) 227 if ((ktrlen = ktr_header.ktr_len) < 0)
228 errx(1, "bogus length 0x%x", ktrlen); 228 errx(1, "bogus length 0x%x", ktrlen);
229 if (ktrlen > size) { 229 if (ktrlen > size) {
230 while (ktrlen > size) 230 while (ktrlen > size)
231 size *= 2; 231 size *= 2;
232 m = realloc(m, size); 232 m = realloc(m, size);
233 if (m == NULL) 233 if (m == NULL)
234 errx(1, "realloc: %s", strerror(ENOMEM)); 234 errx(1, "realloc: %s", strerror(ENOMEM));
235 } 235 }
236 if (ktrlen && fread_tail(m, ktrlen, 1) == 0) 236 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
237 errx(1, "data too short"); 237 errx(1, "data too short");
238 if (col == -1) 238 if (col == -1)
239 continue; 239 continue;
240 240
241 /* update context to match currently processed record */ 241 /* update context to match currently processed record */
242 ectx_sanify(ktr_header.ktr_pid); 242 ectx_sanify(ktr_header.ktr_pid);
243 243
244 switch (ktr_header.ktr_type) { 244 switch (ktr_header.ktr_type) {
245 case KTR_SYSCALL: 245 case KTR_SYSCALL:
246 ktrsyscall(m); 246 ktrsyscall(m);
247 break; 247 break;
248 case KTR_SYSRET: 248 case KTR_SYSRET:
249 ktrsysret(m, ktrlen); 249 ktrsysret(m, ktrlen);
250 break; 250 break;
251 case KTR_NAMEI: 251 case KTR_NAMEI:
252 ktrnamei(m, ktrlen); 252 ktrnamei(m, ktrlen);
253 break; 253 break;
254 case KTR_GENIO: 254 case KTR_GENIO:
255 ktrgenio(m, ktrlen); 255 ktrgenio(m, ktrlen);
256 break; 256 break;
257 case KTR_PSIG: 257 case KTR_PSIG:
258 ktrpsig(m, ktrlen); 258 ktrpsig(m, ktrlen);
259 break; 259 break;
260 case KTR_CSW: 260 case KTR_CSW:
261 ktrcsw(m); 261 ktrcsw(m);
262 break; 262 break;
263 case KTR_EMUL: 263 case KTR_EMUL:
264 ktremul(m, ktrlen, size); 264 ktremul(m, ktrlen, size);
265 break; 265 break;
266 case KTR_USER: 266 case KTR_USER:
267 ktruser(m, ktrlen); 267 ktruser(m, ktrlen);
268 break; 268 break;
269 case KTR_MMSG: 269 case KTR_MMSG:
270 ktrmmsg(m, ktrlen); 270 ktrmmsg(m, ktrlen);
271 break; 271 break;
272 case KTR_MOOL: 272 case KTR_MOOL:
273 ktrmool(m, ktrlen); 273 ktrmool(m, ktrlen);
274 break; 274 break;
275 case KTR_EXEC_ARG: 275 case KTR_EXEC_ARG:
276 case KTR_EXEC_ENV: 276 case KTR_EXEC_ENV:
277 visdump_buf(m, ktrlen, col); 277 visdump_buf(m, ktrlen, col);
278 break; 278 break;
279 case KTR_MIB: 279 case KTR_MIB:
280 ktrmib(m, ktrlen); 280 ktrmib(m, ktrlen);
281 break; 281 break;
282 default: 282 default:
283 putchar('\n'); 283 putchar('\n');
284 hexdump_buf(m, ktrlen, word_size ? word_size : 1); 284 hexdump_buf(m, ktrlen, word_size ? word_size : 1);
285 } 285 }
286 if (tail) 286 if (tail)
287 (void)fflush(stdout); 287 (void)fflush(stdout);
288 } 288 }
289 return (0); 289 return (0);
290} 290}
291 291
292static int 292static int
293fread_tail(void *buf, size_t num, size_t size) 293fread_tail(void *buf, size_t num, size_t size)
294{ 294{
295 int i; 295 int i;
296 296
297 while ((i = fread(buf, size, num, stdin)) == 0 && tail) { 297 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
298 (void)sleep(1); 298 (void)sleep(1);
299 clearerr(stdin); 299 clearerr(stdin);
300 } 300 }
301 return (i); 301 return (i);
302} 302}
303 303
304static int 304static int
305dumpheader(struct ktr_header *kth) 305dumpheader(struct ktr_header *kth)
306{ 306{
307 char unknown[64]; 307 char unknown[64];
308 const char *type; 308 const char *type;
309 union holdtime { 309 union holdtime {
310 struct timeval tv; 310 struct timeval tv;
311 struct timespec ts; 311 struct timespec ts;
312 }; 312 };
313 static union holdtime prevtime; 313 static union holdtime prevtime;
314 union holdtime temp; 314 union holdtime temp;
315 int col; 315 int col;
316 316
317 switch (kth->ktr_type) { 317 switch (kth->ktr_type) {
318 case KTR_SYSCALL: 318 case KTR_SYSCALL:
319 type = "CALL"; 319 type = "CALL";
320 break; 320 break;
321 case KTR_SYSRET: 321 case KTR_SYSRET:
322 type = "RET "; 322 type = "RET ";
323 break; 323 break;
324 case KTR_NAMEI: 324 case KTR_NAMEI:
325 type = "NAMI"; 325 type = "NAMI";
326 break; 326 break;
327 case KTR_GENIO: 327 case KTR_GENIO:
328 type = "GIO "; 328 type = "GIO ";
329 break; 329 break;
330 case KTR_PSIG: 330 case KTR_PSIG:
331 type = "PSIG"; 331 type = "PSIG";
332 break; 332 break;
333 case KTR_CSW: 333 case KTR_CSW:
334 type = "CSW "; 334 type = "CSW ";
335 break; 335 break;
336 case KTR_EMUL: 336 case KTR_EMUL:
337 type = "EMUL"; 337 type = "EMUL";
338 break; 338 break;
339 case KTR_USER: 339 case KTR_USER:
340 type = "MISC"; 340 type = "MISC";
341 break; 341 break;
342 case KTR_MMSG: 342 case KTR_MMSG:
343 type = "MMSG"; 343 type = "MMSG";
344 break; 344 break;
345 case KTR_MOOL: 345 case KTR_MOOL:
346 type = "MOOL"; 346 type = "MOOL";
347 break; 347 break;
348 case KTR_EXEC_ENV: 348 case KTR_EXEC_ENV:
349 type = "ENV"; 349 type = "ENV";
350 break; 350 break;
351 case KTR_EXEC_ARG: 351 case KTR_EXEC_ARG:
352 type = "ARG"; 352 type = "ARG";
353 break; 353 break;
354 case KTR_SAUPCALL: 354 case KTR_SAUPCALL:
355 type = "SAU"; 355 type = "SAU";
356 break; 356 break;
357 case KTR_MIB: 357 case KTR_MIB:
358 type = "MIB"; 358 type = "MIB";
359 break; 359 break;
360 default: 360 default:
361 (void)snprintf(unknown, sizeof(unknown), "UNKNOWN(%d)", 361 (void)snprintf(unknown, sizeof(unknown), "UNKNOWN(%d)",
362 kth->ktr_type); 362 kth->ktr_type);
363 type = unknown; 363 type = unknown;
364 } 364 }
365 365
366 col = printf("%6d ", kth->ktr_pid); 366 col = printf("%6d ", kth->ktr_pid);
367 if (kth->ktr_version > KTRFACv0) 367 if (kth->ktr_version > KTRFACv0)
368 col += printf("%6d ", kth->ktr_lid); 368 col += printf("%6d ", kth->ktr_lid);
369 col += printf("%-8.*s ", MAXCOMLEN, kth->ktr_comm); 369 col += printf("%-8.*s ", MAXCOMLEN, kth->ktr_comm);
370 if (timestamp) { 370 if (timestamp) {
371 if (timestamp == 2) { 371 if (timestamp == 2) {
372 if (kth->ktr_version == KTRFACv0) { 372 if (kth->ktr_version == KTRFACv0) {
373 if (prevtime.tv.tv_sec == 0) 373 if (prevtime.tv.tv_sec == 0)
374 temp.tv.tv_sec = temp.tv.tv_usec = 0; 374 temp.tv.tv_sec = temp.tv.tv_usec = 0;
375 else 375 else
376 timersub(&kth->ktr_tv, 376 timersub(&kth->ktr_tv,
377 &prevtime.tv, &temp.tv); 377 &prevtime.tv, &temp.tv);
378 prevtime.tv = kth->ktr_tv; 378 prevtime.tv = kth->ktr_tv;
379 } else { 379 } else {
380 if (prevtime.ts.tv_sec == 0) 380 if (prevtime.ts.tv_sec == 0)
381 temp.ts.tv_sec = temp.ts.tv_nsec = 0; 381 temp.ts.tv_sec = temp.ts.tv_nsec = 0;
382 else 382 else
383 timespecsub(&kth->ktr_time, 383 timespecsub(&kth->ktr_time,
384 &prevtime.ts, &temp.ts); 384 &prevtime.ts, &temp.ts);
385 prevtime.ts = kth->ktr_time; 385 prevtime.ts = kth->ktr_time;
386 } 386 }
387 } else { 387 } else {
388 if (kth->ktr_version == KTRFACv0) 388 if (kth->ktr_version == KTRFACv0)
389 temp.tv = kth->ktr_tv; 389 temp.tv = kth->ktr_tv;
390 else 390 else
391 temp.ts = kth->ktr_time; 391 temp.ts = kth->ktr_time;
392 } 392 }
393 if (kth->ktr_version == KTRFACv0) 393 if (kth->ktr_version == KTRFACv0)
394 col += printf("%ld.%06ld ", 394 col += printf("%ld.%06ld ",
395 (long)temp.tv.tv_sec, (long)temp.tv.tv_usec); 395 (long)temp.tv.tv_sec, (long)temp.tv.tv_usec);
396 else 396 else
397 col += printf("%ld.%09ld ", 397 col += printf("%ld.%09ld ",
398 (long)temp.ts.tv_sec, (long)temp.ts.tv_nsec); 398 (long)temp.ts.tv_sec, (long)temp.ts.tv_nsec);
399 } 399 }
400 col += printf("%-4s ", type); 400 col += printf("%-4s ", type);
401 return col; 401 return col;
402} 402}
403 403
404static void 404static void
405output_long(u_long it, int as_x) 405output_long(u_long it, int as_x)
406{ 406{
407 if (cur_emul->flags & EMUL_FLAG_NETBSD32) 407 if (cur_emul->flags & EMUL_FLAG_NETBSD32)
408 printf(as_x ? "%#x" : "%d", (u_int)it); 408 printf(as_x ? "%#x" : "%d", (u_int)it);
409 else 409 else
410 printf(as_x ? "%#lx" : "%ld", it); 410 printf(as_x ? "%#lx" : "%ld", it);
411} 411}
412 412
413static void 413static void
414ioctldecode(u_long cmd) 414ioctldecode(u_long cmd)
415{ 415{
416 char dirbuf[4], *dir = dirbuf; 416 char dirbuf[4], *dir = dirbuf;
417 int c; 417 int c;
418 418
419 if (cmd & IOC_IN) 419 if (cmd & IOC_IN)
420 *dir++ = 'W'; 420 *dir++ = 'W';
421 if (cmd & IOC_OUT) 421 if (cmd & IOC_OUT)
422 *dir++ = 'R'; 422 *dir++ = 'R';
423 *dir = '\0'; 423 *dir = '\0';
424 424
425 c = (cmd >> 8) & 0xff; 425 c = (cmd >> 8) & 0xff;
426 if (isprint(c)) 426 if (isprint(c))
427 printf(",_IO%s('%c',", dirbuf, c); 427 printf(",_IO%s('%c',", dirbuf, c);
428 else 428 else
429 printf(",_IO%s(0x%02x,", dirbuf, c); 429 printf(",_IO%s(0x%02x,", dirbuf, c);
430 output_long(cmd & 0xff, decimal == 0); 430 output_long(cmd & 0xff, decimal == 0);
431 if ((cmd & IOC_VOID) == 0) { 431 if ((cmd & IOC_VOID) == 0) {
432 putchar(','); 432 putchar(',');
433 output_long(IOCPARM_LEN(cmd), decimal == 0); 433 output_long(IOCPARM_LEN(cmd), decimal == 0);
434 } 434 }
435 putchar(')'); 435 putchar(')');
436} 436}
437 437
438static void 438static void
439ktrsyscall(struct ktr_syscall *ktr) 439ktrsyscall(struct ktr_syscall *ktr)
440{ 440{
441 int argcount; 441 int argcount;
442 const struct emulation *emul = cur_emul; 442 const struct emulation *emul = cur_emul;
443 register_t *ap; 443 register_t *ap;
444 char c; 444 char c;
445 const char *cp; 445 const char *cp;
446 const char *sys_name; 446 const char *sys_name;
447 447
448 argcount = ktr->ktr_argsize / sizeof (*ap); 448 argcount = ktr->ktr_argsize / sizeof (*ap);
449 449
450 emul_changed = 0; 450 emul_changed = 0;
451 451
452 if (numeric || 452 if (numeric ||
453 ((ktr->ktr_code >= emul->nsysnames || ktr->ktr_code < 0) && 453 ((ktr->ktr_code >= emul->nsysnames || ktr->ktr_code < 0) &&
454 mach_traps_dispatch(&ktr->ktr_code, &emul) == 0)) { 454 mach_traps_dispatch(&ktr->ktr_code, &emul) == 0)) {
455 sys_name = "?"; 455 sys_name = "?";
456 (void)printf("[%d]", ktr->ktr_code); 456 (void)printf("[%d]", ktr->ktr_code);
457 } else { 457 } else {
458 sys_name = emul->sysnames[ktr->ktr_code]; 458 sys_name = emul->sysnames[ktr->ktr_code];
459 (void)printf("%s", sys_name); 459 (void)printf("%s", sys_name);
460 } 460 }
461#ifdef _LP64 461#ifdef _LP64
462#define NETBSD32_ "netbsd32_" 462#define NETBSD32_ "netbsd32_"
463 if (cur_emul->flags & EMUL_FLAG_NETBSD32) { 463 if (cur_emul->flags & EMUL_FLAG_NETBSD32) {
464 size_t len = strlen(NETBSD32_); 464 size_t len = strlen(NETBSD32_);
465 if (strncmp(sys_name, NETBSD32_, len) == 0) 465 if (strncmp(sys_name, NETBSD32_, len) == 0)
466 sys_name += len; 466 sys_name += len;
467 } 467 }
468#undef NETBSD32_ 468#undef NETBSD32_
469#endif 469#endif
470 470
471 ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall)); 471 ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
472 if (argcount) { 472 if (argcount) {
473 c = '('; 473 c = '(';
474 if (plain) { 474 if (plain) {
475 ; 475 ;
476 476
477 } else if (strcmp(sys_name, "exit") == 0) { 477 } else if (strcmp(sys_name, "exit") == 0) {
478 ectx_delete(); 478 ectx_delete();
479 479
480 } else if (strcmp(sys_name, "ioctl") == 0 && argcount >= 2) { 480 } else if (strcmp(sys_name, "ioctl") == 0 && argcount >= 2) {
481 (void)putchar('('); 481 (void)putchar('(');
482 output_long((long)*ap, !(decimal || small(*ap))); 482 output_long((long)*ap, !(decimal || small(*ap)));
483 ap++; 483 ap++;
484 argcount--; 484 argcount--;
485 if ((cp = ioctlname(*ap)) != NULL) 485 if ((cp = ioctlname(*ap)) != NULL)
486 (void)printf(",%s", cp); 486 (void)printf(",%s", cp);
487 else 487 else
488 ioctldecode(*ap); 488 ioctldecode(*ap);
489 ap++; 489 ap++;
490 argcount--; 490 argcount--;
491 c = ','; 491 c = ',';
492 492
493 } else if ((strstr(sys_name, "sigaction") != NULL || 493 } else if ((strstr(sys_name, "sigaction") != NULL ||
494 strstr(sys_name, "sigvec") != NULL) && argcount >= 1) { 494 strstr(sys_name, "sigvec") != NULL) && argcount >= 1) {
495 (void)printf("(SIG%s", signame(ap[0], 1)); 495 (void)printf("(SIG%s", signame(ap[0], 1));
496 ap += 1; 496 ap += 1;
497 argcount -= 1; 497 argcount -= 1;
498 c = ','; 498 c = ',';
499 499
500 } else if ((strcmp(sys_name, "kill") == 0 || 500 } else if ((strcmp(sys_name, "kill") == 0 ||
501 strcmp(sys_name, "killpg") == 0) && argcount >= 2) { 501 strcmp(sys_name, "killpg") == 0) && argcount >= 2) {
502 putchar('('); 502 putchar('(');
503 output_long((long)ap[0], !(decimal || small(*ap))); 503 output_long((long)ap[0], !(decimal || small(*ap)));
504 (void)printf(", SIG%s", signame(ap[1], 1)); 504 (void)printf(", SIG%s", signame(ap[1], 1));
505 ap += 2; 505 ap += 2;
506 argcount -= 2; 506 argcount -= 2;
507 c = ','; 507 c = ',';
508 508
509 } else if (strcmp(sys_name, "ptrace") == 0 && argcount >= 1) { 509 } else if (strcmp(sys_name, "ptrace") == 0 && argcount >= 1) {
510 putchar('('); 510 putchar('(');
511 if (strcmp(emul->name, "linux") == 0 || 511 if (strcmp(emul->name, "linux") == 0 ||
512 strcmp(emul->name, "linux32") == 0) { 512 strcmp(emul->name, "linux32") == 0) {
513 if ((long)*ap >= 0 && *ap < 513 if ((long)*ap >= 0 && *ap <
514 sizeof(linux_ptrace_ops) / 514 sizeof(linux_ptrace_ops) /
515 sizeof(linux_ptrace_ops[0])) 515 sizeof(linux_ptrace_ops[0]))
516 (void)printf("%s", 516 (void)printf("%s",
517 linux_ptrace_ops[*ap]); 517 linux_ptrace_ops[*ap]);
518 else 518 else
519 output_long((long)*ap, 1); 519 output_long((long)*ap, 1);
520 } else { 520 } else {
521 if ((long)*ap >= 0 && *ap < 521 if ((long)*ap >= 0 && *ap <
522 sizeof(ptrace_ops) / sizeof(ptrace_ops[0])) 522 sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
523 (void)printf("%s", ptrace_ops[*ap]); 523 (void)printf("%s", ptrace_ops[*ap]);
524#ifdef PT_MACHDEP_STRINGS 524#ifdef PT_MACHDEP_STRINGS
525 else if (*ap >= PT_FIRSTMACH && 525 else if (*ap >= PT_FIRSTMACH &&
526 *ap - PT_FIRSTMACH < 526 *ap - PT_FIRSTMACH <
527 sizeof(ptrace_machdep_ops) / 527 sizeof(ptrace_machdep_ops) /
528 sizeof(ptrace_machdep_ops[0])) 528 sizeof(ptrace_machdep_ops[0]))
529 (void)printf("%s", ptrace_machdep_ops[*ap - PT_FIRSTMACH]); 529 (void)printf("%s", ptrace_machdep_ops[*ap - PT_FIRSTMACH]);
530#endif 530#endif
531 else 531 else
532 output_long((long)*ap, 1); 532 output_long((long)*ap, 1);
533 } 533 }
534 ap++; 534 ap++;
535 argcount--; 535 argcount--;
536 c = ','; 536 c = ',';
537 537
538 } 538 }
539 while (argcount > 0) { 539 while (argcount > 0) {
540 putchar(c); 540 putchar(c);
541 output_long((long)*ap, !(decimal || small(*ap))); 541 output_long((long)*ap, !(decimal || small(*ap)));
542 ap++; 542 ap++;
543 argcount--; 543 argcount--;
544 c = ','; 544 c = ',';
545 } 545 }
546 (void)putchar(')'); 546 (void)putchar(')');
547 } 547 }
548 (void)putchar('\n'); 548 (void)putchar('\n');
549} 549}
550 550
551static void 551static void
552ktrsysret(struct ktr_sysret *ktr, int len) 552ktrsysret(struct ktr_sysret *ktr, int len)
553{ 553{
554 const struct emulation *emul; 554 const struct emulation *emul;
555 int error = ktr->ktr_error; 555 int error = ktr->ktr_error;
556 int code = ktr->ktr_code; 556 int code = ktr->ktr_code;
557 557
558 if (emul_changed) { 558 if (emul_changed) {
559 /* In order to get system call name right in execve return */ 559 /* In order to get system call name right in execve return */
560 emul = prev_emul; 560 emul = prev_emul;
561 emul_changed = 0; 561 emul_changed = 0;
562 } else 562 } else
563 emul = cur_emul; 563 emul = cur_emul;
564 564
565 if (numeric || ((code >= emul->nsysnames || code < 0 || plain > 1) && 565 if (numeric || ((code >= emul->nsysnames || code < 0 || plain > 1) &&
566 mach_traps_dispatch(&code, &emul) == 0)) 566 mach_traps_dispatch(&code, &emul) == 0))
567 (void)printf("[%d] ", code); 567 (void)printf("[%d] ", code);
568 else 568 else
569 (void)printf("%s ", emul->sysnames[code]); 569 (void)printf("%s ", emul->sysnames[code]);
570 570
571 switch (error) { 571 switch (error) {
572 case 0: 572 case 0:
573 rprint(ktr->ktr_retval); 573 rprint(ktr->ktr_retval);
574 if (len > offsetof(struct ktr_sysret, ktr_retval_1) && 574 if (len > offsetof(struct ktr_sysret, ktr_retval_1) &&
575 ktr->ktr_retval_1 != 0) { 575 ktr->ktr_retval_1 != 0) {
576 (void)printf(", "); 576 (void)printf(", ");
577 rprint(ktr->ktr_retval_1); 577 rprint(ktr->ktr_retval_1);
578 } 578 }
579 break; 579 break;
580 580
581 default: 581 default:
582 eprint(error); 582 eprint(error);
583 break; 583 break;
584 } 584 }
585 (void)putchar('\n'); 585 (void)putchar('\n');
586} 586}
587 587
588static void 588static void
589rprint(register_t ret) 589rprint(register_t ret)
590{ 590{
591 591
592 if (!plain) { 592 if (!plain) {
593 (void)printf("%ld", (long)ret); 593 (void)printf("%ld", (long)ret);
594 if (!small(ret)) 594 if (!small(ret))
595 (void)printf("/%#lx", (long)ret); 595 (void)printf("/%#lx", (long)ret);
596 } else { 596 } else {
597 if (decimal || small(ret)) 597 if (decimal || small(ret))
598 (void)printf("%ld", (long)ret); 598 (void)printf("%ld", (long)ret);
599 else 599 else
600 (void)printf("%#lx", (long)ret); 600 (void)printf("%#lx", (long)ret);
601 } 601 }
602} 602}
603 603
604/* 604/*
605 * We print the original emulation's error numerically, but we 605 * We print the original emulation's error numerically, but we
606 * translate it to netbsd to print it symbolically. 606 * translate it to netbsd to print it symbolically.
607 */ 607 */
608static void 608static void
609eprint(int e) 609eprint(int e)
610{ 610{
611 int i = e; 611 int i = e;
612 612
613 if (cur_emul->errnomap) { 613 if (cur_emul->errnomap) {
614 614
615 /* No remapping for ERESTART and EJUSTRETURN */ 615 /* No remapping for ERESTART and EJUSTRETURN */
616 /* Kludge for linux that has negative error numbers */ 616 /* Kludge for linux that has negative error numbers */
617 if (cur_emul->errnomap[2] > 0 && e < 0) 617 if (cur_emul->errnomap[2] > 0 && e < 0)
618 goto normal; 618 goto normal;
619 619
620 for (i = 0; i < cur_emul->nerrnomap; i++) 620 for (i = 0; i < cur_emul->nerrnomap; i++)
621 if (e == cur_emul->errnomap[i]) 621 if (e == cur_emul->errnomap[i])
622 break; 622 break;
623 623
624 if (i == cur_emul->nerrnomap) { 624 if (i == cur_emul->nerrnomap) {
625 printf("-1 unknown errno %d", e); 625 printf("-1 unknown errno %d", e);
626 return; 626 return;
627 } 627 }
628 } 628 }
629 629
630normal: 630normal:
631 switch (i) { 631 switch (i) {
632 case ERESTART: 632 case ERESTART:
633 (void)printf("RESTART"); 633 (void)printf("RESTART");
634 break; 634 break;
635 635
636 case EJUSTRETURN: 636 case EJUSTRETURN:
637 (void)printf("JUSTRETURN"); 637 (void)printf("JUSTRETURN");
638 break; 638 break;
639 639
640 default: 640 default:
641 (void)printf("-1 errno %d", e); 641 (void)printf("-1 errno %d", e);
642 if (!plain) 642 if (!plain)
643 (void)printf(" %s", strerror(i)); 643 (void)printf(" %s", strerror(i));
644 } 644 }
645} 645}
646 646
647static void 647static void
648ktrnamei(char *cp, int len) 648ktrnamei(char *cp, int len)
649{ 649{
650 650
651 (void)printf("\"%.*s\"\n", len, cp); 651 (void)printf("\"%.*s\"\n", len, cp);
652} 652}
653 653
654static void 654static void
655ktremul(char *name, int len, int bufsize) 655ktremul(char *name, int len, int bufsize)
656{ 656{
657 657
658 if (len >= bufsize) 658 if (len >= bufsize)
659 len = bufsize - 1; 659 len = bufsize - 1;
660 660
661 name[len] = '\0'; 661 name[len] = '\0';
662 setemul(name, ktr_header.ktr_pid, 1); 662 setemul(name, ktr_header.ktr_pid, 1);
663 emul_changed = 1; 663 emul_changed = 1;
664 664
665 (void)printf("\"%s\"\n", name); 665 (void)printf("\"%s\"\n", name);
666} 666}
667 667
668static void 668static void
669hexdump_buf(const void *vdp, int datalen, int word_sz) 669hexdump_buf(const void *vdp, int datalen, int word_sz)
670{ 670{
671 const char hex[] = "0123456789abcdef"; 671 const char hex[] = "0123456789abcdef";
672 char chars[16], prev[16]; 672 char chars[16], prev[16];
673 char bytes[16 * 3 + 4]; 673 char bytes[16 * 3 + 4];
674 const unsigned char *dp = vdp; 674 const unsigned char *dp = vdp;
675 const unsigned char *datalim = dp + datalen; 675 const unsigned char *datalim = dp + datalen;
676 const unsigned char *line_end; 676 const unsigned char *line_end;
677 int off, l = 0, c; 677 int off, l = 0, c;
678 char *cp, *bp; 678 char *cp, *bp;
679 int divmask = word_sz - 1; /* block size in bytes */ 679 int divmask = word_sz - 1; /* block size in bytes */
680 int gdelim = 3; /* gap between blocks */ 680 int gdelim = 3; /* gap between blocks */
681 int bsize = 2; /* increment for each byte */ 681 int bsize = 2; /* increment for each byte */
682 int width; 682 int width;
683 int dupl = 0; 683 int dupl = 0;
684#if _BYTE_ORDER == _LITTLE_ENDIAN 684#if _BYTE_ORDER == _LITTLE_ENDIAN
685 int bswap = word_sz - 1; 685 int bswap = word_sz - 1;
686#else 686#else
687#define bswap 0 687#define bswap 0
688#endif 688#endif
689 689
690 switch (word_sz) { 690 switch (word_sz) {
691 case 2: 691 case 2:
692 gdelim = 2; 692 gdelim = 2;
693 break; 693 break;
694 case 1: 694 case 1:
695 divmask = 7; 695 divmask = 7;
696 bsize = 3; 696 bsize = 3;
697 gdelim = 1; 697 gdelim = 1;
698 break; 698 break;
699 default: 699 default:
700 break; 700 break;
701 } 701 }
702 width = 16 * bsize + (16 / (divmask + 1)) * gdelim; 702 width = 16 * bsize + (16 / (divmask + 1)) * gdelim;
703 if (word_sz != 1) 703 if (word_sz != 1)
704 width += 2; 704 width += 2;
705 705
706 for (off = 0; dp < datalim; off += l) { 706 for (off = 0; dp < datalim; off += l) {
707 memset(bytes, ' ', sizeof bytes); 707 memset(bytes, ' ', sizeof bytes);
708 line_end = dp + 16; 708 line_end = dp + 16;
709 if (line_end >= datalim) { 709 if (line_end >= datalim) {
710 line_end = datalim; 710 line_end = datalim;
711 dupl |= 1; /* need to print */ 711 dupl |= 1; /* need to print */
712 } else { 712 } else {
713 if (dupl == 0 || memcmp(dp, prev, sizeof chars)) 713 if (dupl == 0 || memcmp(dp, prev, sizeof chars))
714 dupl |= 1; 714 dupl |= 1;
715 } 715 }
716 716
717 if (!(dupl & 1)) { 717 if (!(dupl & 1)) {
718 /* This is a duplicate of the line above, count 'em */ 718 /* This is a duplicate of the line above, count 'em */
719 dupl += 2; 719 dupl += 2;
720 dp = line_end; 720 dp = line_end;
721 continue; 721 continue;
722 } 722 }
723 723
724 if (dupl > 3) { 724 if (dupl > 3) {
725 /* previous line as a duplicate */ 725 /* previous line as a duplicate */
726 if (dupl == 5) 726 if (dupl == 5)
727 /* Only one duplicate, print line */ 727 /* Only one duplicate, print line */
728 printf("\t%-5.3x%.*s%.*s\n", 728 printf("\t%-5.3x%.*s%.*s\n",
729 off - l, width, bytes, l, chars); 729 off - l, width, bytes, l, chars);
730 else 730 else
731 printf("\t%.*s\n", 731 printf("\t%.*s\n",
732 snprintf(NULL, 0, "%3x", off), "*****"); 732 snprintf(NULL, 0, "%3x", off), "*****");
733 } 733 }
734 734
735 for (l = 0, bp = bytes, cp = chars; dp < line_end; l++) { 735 for (l = 0, bp = bytes, cp = chars; dp < line_end; l++) {
736 c = *dp++; 736 c = *dp++;
737 prev[l] = c; 737 prev[l] = c;
738 if ((l & divmask) == 0) 738 if ((l & divmask) == 0)
739 bp += gdelim; 739 bp += gdelim;
740 bp[(l ^ bswap) * bsize] = hex[c >> 4]; 740 bp[(l ^ bswap) * bsize] = hex[c >> 4];
741 bp[(l ^ bswap) * bsize + 1] = hex[c & 0xf]; 741 bp[(l ^ bswap) * bsize + 1] = hex[c & 0xf];
742 *cp++ = isgraph(c) ? c : '.'; 742 *cp++ = isgraph(c) ? c : '.';
743 } 743 }
744 744
745 printf("\t%-5.3x%.*s%.*s\n", off, width, bytes, l, chars); 745 printf("\t%-5.3x%.*s%.*s\n", off, width, bytes, l, chars);
746 dupl = 2; 746 dupl = 2;
747 } 747 }
748} 748}
749 749
750static void 750static void
751visdump_buf(const void *vdp, int datalen, int col) 751visdump_buf(const void *vdp, int datalen, int col)
752{ 752{
753 const unsigned char *dp = vdp; 753 const unsigned char *dp = vdp;
754 char *cp; 754 char *cp;
755 int width; 755 int width;
756 char visbuf[5]; 756 char visbuf[5];
757 static int screenwidth = 0; 757 static int screenwidth = 0;
758 758
759 if (screenwidth == 0) { 759 if (screenwidth == 0) {
760 struct winsize ws; 760 struct winsize ws;
761 761
762 if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 && 762 if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
763 ws.ws_col > 8) 763 ws.ws_col > 8)
764 screenwidth = ws.ws_col; 764 screenwidth = ws.ws_col;
765 else 765 else
766 screenwidth = 80; 766 screenwidth = 80;
767 } 767 }
768 768
769 (void)printf("\""); 769 (void)printf("\"");
770 col++; 770 col++;
771 for (; datalen > 0; datalen--, dp++) { 771 for (; datalen > 0; datalen--, dp++) {
772 (void)svis(visbuf, *dp, VIS_CSTYLE, 772 (void)svis(visbuf, *dp, VIS_CSTYLE,
773 datalen > 1 ? *(dp + 1) : 0, "\"\n"); 773 datalen > 1 ? *(dp + 1) : 0, "\"\n");
774 cp = visbuf; 774 cp = visbuf;
775 /* 775 /*
776 * Keep track of printables and 776 * Keep track of printables and
777 * space chars (like fold(1)). 777 * space chars (like fold(1)).
778 */ 778 */
779 if (col == 0) { 779 if (col == 0) {
780 (void)putchar('\t'); 780 (void)putchar('\t');
781 col = 8; 781 col = 8;
782 } 782 }
783 switch (*cp) { 783 switch (*cp) {
784 case '\n': 784 case '\n':
785 col = 0; 785 col = 0;
786 (void)putchar('\n'); 786 (void)putchar('\n');
787 continue; 787 continue;
788 case '\t': 788 case '\t':
789 width = 8 - (col & 07); 789 width = 8 - (col & 07);
790 break; 790 break;
791 default: 791 default:
792 width = strlen(cp); 792 width = strlen(cp);
793 } 793 }
794 if (col + width > (screenwidth - 2)) { 794 if (col + width > (screenwidth - 2)) {
795 (void)printf("\\\n\t"); 795 (void)printf("\\\n\t");
796 col = 8; 796 col = 8;
797 if (*cp == '\t') 797 if (*cp == '\t')
798 width = 8; 798 width = 8;
799 } 799 }
800 col += width; 800 col += width;
801 do { 801 do {
802 (void)putchar(*cp++); 802 (void)putchar(*cp++);
803 } while (*cp); 803 } while (*cp);
804 } 804 }
805 if (col == 0) 805 if (col == 0)
806 (void)printf(" "); 806 (void)printf(" ");
807 (void)printf("\"\n"); 807 (void)printf("\"\n");
808} 808}
809 809
810static void 810static void
811ktrgenio(struct ktr_genio *ktr, int len) 811ktrgenio(struct ktr_genio *ktr, int len)
812{ 812{
813 int datalen = len - sizeof (struct ktr_genio); 813 int datalen = len - sizeof (struct ktr_genio);
814 char *dp = (char *)ktr + sizeof (struct ktr_genio); 814 char *dp = (char *)ktr + sizeof (struct ktr_genio);
815 815
816 if (ktr->ktr_fd != -1) 816 if (ktr->ktr_fd != -1)
817 printf("fd %d ", ktr->ktr_fd); 817 printf("fd %d ", ktr->ktr_fd);
818 printf("%s %d bytes\n",  818 printf("%s %d bytes\n",
819 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen); 819 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
820 if (maxdata == 0) 820 if (maxdata == 0)
821 return; 821 return;
822 if (maxdata > 0 && datalen > maxdata) 822 if (maxdata > 0 && datalen > maxdata)
823 datalen = maxdata; 823 datalen = maxdata;
824 if (word_size) { 824 if (word_size) {
825 hexdump_buf(dp, datalen, word_size); 825 hexdump_buf(dp, datalen, word_size);
826 return; 826 return;
827 } 827 }
828 (void)printf(" "); 828 (void)printf(" ");
829 visdump_buf(dp, datalen, 7); 829 visdump_buf(dp, datalen, 7);
830} 830}
831 831
832static void 832static void
833ktrpsig(void *v, int len) 833ktrpsig(void *v, int len)
834{ 834{
835 int signo, first; 835 int signo, first;
836 struct { 836 struct {
837 struct ktr_psig ps; 837 struct ktr_psig ps;
838 siginfo_t si; 838 siginfo_t si;
839 } *psig = v; 839 } *psig = v;
840 siginfo_t *si = &psig->si; 840 siginfo_t *si = &psig->si;
841 const char *code; 841 const char *code;
842 842
843 (void)printf("SIG%s ", signame(psig->ps.signo, 0)); 843 (void)printf("SIG%s ", signame(psig->ps.signo, 0));
844 if (psig->ps.action == SIG_DFL) 844 if (psig->ps.action == SIG_DFL)
845 (void)printf("SIG_DFL"); 845 (void)printf("SIG_DFL");
846 else { 846 else {
847 (void)printf("caught handler=%p mask=(", psig->ps.action); 847 (void)printf("caught handler=%p mask=(", psig->ps.action);
848 first = 1; 848 first = 1;
849 for (signo = 1; signo < NSIG; signo++) { 849 for (signo = 1; signo < NSIG; signo++) {
850 if (sigismember(&psig->ps.mask, signo)) { 850 if (sigismember(&psig->ps.mask, signo)) {
851 if (first) 851 if (first)
852 first = 0; 852 first = 0;
853 else 853 else
854 (void)printf(","); 854 (void)printf(",");
855 (void)printf("%d", signo); 855 (void)printf("%d", signo);
856 } 856 }
857 } 857 }
858 (void)printf(")"); 858 (void)printf(")");
859 } 859 }
860 switch (len) { 860 switch (len) {
861 case sizeof(struct ktr_psig): 861 case sizeof(struct ktr_psig):
862 if (psig->ps.code) 862 if (psig->ps.code)
863 printf(" code=0x%x", psig->ps.code); 863 printf(" code=0x%x", psig->ps.code);
864 printf(psig->ps.action == SIG_DFL ? "\n" : ")\n"); 864 printf(psig->ps.action == SIG_DFL ? "\n" : ")\n");
865 return; 865 return;
866 case sizeof(*psig): 866 case sizeof(*psig):
867 if (si->si_code == 0) { 867 if (si->si_code == 0) {
868 printf(": code=SI_USER sent by pid=%d, uid=%d)\n", 868 printf(": code=SI_USER sent by pid=%d, uid=%d)\n",
869 si->si_pid, si->si_uid); 869 si->si_pid, si->si_uid);
870 return; 870 return;
871 } 871 }
872 872
873 if (si->si_code < 0) { 873 if (si->si_code < 0) {
874 switch (si->si_code) { 874 switch (si->si_code) {
875 case SI_TIMER: 875 case SI_TIMER:
876 printf(": code=SI_TIMER sigval %p)\n", 876 printf(": code=SI_TIMER sigval %p)\n",
877 si->si_value.sival_ptr); 877 si->si_value.sival_ptr);
878 return; 878 return;
879 case SI_QUEUE: 879 case SI_QUEUE:
880 code = "SI_QUEUE"; 880 code = "SI_QUEUE";
881 break; 881 break;
882 case SI_ASYNCIO: 882 case SI_ASYNCIO:
883 code = "SI_ASYNCIO"; 883 code = "SI_ASYNCIO";
884 break; 884 break;
885 case SI_MESGQ: 885 case SI_MESGQ:
886 code = "SI_MESGQ"; 886 code = "SI_MESGQ";
887 break; 887 break;
 888 case SI_LWP:
 889 code = "SI_LWP";
 890 break;
888 default: 891 default:
889 code = NULL; 892 code = NULL;
890 break; 893 break;
891 } 894 }
892 if (code) 895 if (code)
893 printf(": code=%s unimplemented)\n", code); 896 printf(": code=%s unimplemented)\n", code);
894 else 897 else
895 printf(": code=%d unimplemented)\n", 898 printf(": code=%d unimplemented)\n",
896 si->si_code); 899 si->si_code);
897 return; 900 return;
898 } 901 }
899 902
900 if (si->si_code == SI_NOINFO) { 903 if (si->si_code == SI_NOINFO) {
901 printf(": code=SI_NOINFO\n"); 904 printf(": code=SI_NOINFO\n");
902 return; 905 return;
903 } 906 }
904 907
905 code = siginfocodename(si->si_signo, si->si_code); 908 code = siginfocodename(si->si_signo, si->si_code);
906 switch (si->si_signo) { 909 switch (si->si_signo) {
907 case SIGCHLD: 910 case SIGCHLD:
908 printf(": code=%s child pid=%d, uid=%d, " 911 printf(": code=%s child pid=%d, uid=%d, "
909 " status=%u, utime=%lu, stime=%lu)\n", 912 " status=%u, utime=%lu, stime=%lu)\n",
910 code, si->si_pid, 913 code, si->si_pid,
911 si->si_uid, si->si_status, 914 si->si_uid, si->si_status,
912 (unsigned long) si->si_utime, 915 (unsigned long) si->si_utime,
913 (unsigned long) si->si_stime); 916 (unsigned long) si->si_stime);
914 return; 917 return;
915 case SIGILL: 918 case SIGILL:
916 case SIGFPE: 919 case SIGFPE:
917 case SIGSEGV: 920 case SIGSEGV:
918 case SIGBUS: 921 case SIGBUS:
919 case SIGTRAP: 922 case SIGTRAP:
920 printf(": code=%s, addr=%p, trap=%d)\n", 923 printf(": code=%s, addr=%p, trap=%d)\n",
921 code, si->si_addr, si->si_trap); 924 code, si->si_addr, si->si_trap);
922 return; 925 return;
923 case SIGIO: 926 case SIGIO:
924 printf(": code=%s, fd=%d, band=%lx)\n", 927 printf(": code=%s, fd=%d, band=%lx)\n",
925 code, si->si_fd, si->si_band); 928 code, si->si_fd, si->si_band);
926 return; 929 return;
927 default: 930 default:
928 printf(": code=%s, errno=%d)\n", 931 printf(": code=%s, errno=%d)\n",
929 code, si->si_errno); 932 code, si->si_errno);
930 return; 933 return;
931 } 934 }
932 /*NOTREACHED*/ 935 /*NOTREACHED*/
933 default: 936 default:
934 warnx("Unhandled size %d for ktrpsig\n", len); 937 warnx("Unhandled size %d for ktrpsig\n", len);
935 break; 938 break;
936 } 939 }
937} 940}
938 941
939static void 942static void
940ktrcsw(struct ktr_csw *cs) 943ktrcsw(struct ktr_csw *cs)
941{ 944{
942 945
943 (void)printf("%s %s\n", cs->out ? "stop" : "resume", 946 (void)printf("%s %s\n", cs->out ? "stop" : "resume",
944 cs->user ? "user" : "kernel"); 947 cs->user ? "user" : "kernel");
945} 948}
946 949
947static void 950static void
948ktruser(struct ktr_user *usr, int len) 951ktruser(struct ktr_user *usr, int len)
949{ 952{
950 int i; 953 int i;
951 unsigned char *dta; 954 unsigned char *dta;
952 955
953 len -= sizeof(struct ktr_user); 956 len -= sizeof(struct ktr_user);
954 printf("%.*s:", KTR_USER_MAXIDLEN, usr->ktr_id); 957 printf("%.*s:", KTR_USER_MAXIDLEN, usr->ktr_id);
955 dta = (unsigned char *)(usr + 1); 958 dta = (unsigned char *)(usr + 1);
956 if (word_size) { 959 if (word_size) {
957 printf("\n"); 960 printf("\n");
958 hexdump_buf(dta, len, word_size); 961 hexdump_buf(dta, len, word_size);
959 return; 962 return;
960 } 963 }
961 printf(" %d, ", len); 964 printf(" %d, ", len);
962 for (i = 0; i < len; i++) 965 for (i = 0; i < len; i++)
963 printf("%02x", (unsigned int) dta[i]); 966 printf("%02x", (unsigned int) dta[i]);
964 printf("\n"); 967 printf("\n");
965} 968}
966 969
967static void 970static void
968ktrmmsg(struct ktr_mmsg *mmsg, int len) 971ktrmmsg(struct ktr_mmsg *mmsg, int len)
969{ 972{
970 const char *service_name; 973 const char *service_name;
971 const char *reply; 974 const char *reply;
972 int id; 975 int id;
973 976
974 id = mmsg->ktr_id; 977 id = mmsg->ktr_id;
975 if ((id / 100) % 2) { /* Message reply */ 978 if ((id / 100) % 2) { /* Message reply */
976 reply = " reply"; 979 reply = " reply";
977 id -= 100; 980 id -= 100;
978 } else { 981 } else {
979 reply = ""; 982 reply = "";
980 } 983 }
981 984
982 if ((service_name = mach_service_name(id)) != NULL) 985 if ((service_name = mach_service_name(id)) != NULL)
983 printf("%s%s [%d]\n", service_name, reply, mmsg->ktr_id); 986 printf("%s%s [%d]\n", service_name, reply, mmsg->ktr_id);
984 else 987 else
985 printf("unknown service%s [%d]\n", reply, mmsg->ktr_id); 988 printf("unknown service%s [%d]\n", reply, mmsg->ktr_id);
986 989
987 hexdump_buf(mmsg, len, word_size ? word_size : 4); 990 hexdump_buf(mmsg, len, word_size ? word_size : 4);
988} 991}
989 992
990static void 993static void
991ktrmool(struct ktr_mool *mool, int len) 994ktrmool(struct ktr_mool *mool, int len)
992{ 995{
993 size_t size = mool->size; 996 size_t size = mool->size;
994 997
995 printf("%ld/0x%lx bytes at %p\n", 998 printf("%ld/0x%lx bytes at %p\n",
996 (u_long)size, (u_long)size, mool->uaddr); 999 (u_long)size, (u_long)size, mool->uaddr);
997 mool++; 1000 mool++;
998 hexdump_buf(mool, size, word_size ? word_size : 4); 1001 hexdump_buf(mool, size, word_size ? word_size : 4);
999} 1002}
1000 1003
1001static void 1004static void
1002ktrmib(int *namep, int len) 1005ktrmib(int *namep, int len)
1003{ 1006{
1004 int i; 1007 int i;
1005 1008
1006 for (i = 0; i < (len / sizeof(*namep)); i++) 1009 for (i = 0; i < (len / sizeof(*namep)); i++)
1007 printf("%s%d", (i == 0) ? "" : ".", namep[i]); 1010 printf("%s%d", (i == 0) ? "" : ".", namep[i]);
1008 printf("\n"); 1011 printf("\n");
1009} 1012}
1010 1013
1011static const char * 1014static const char *
1012signame(long sig, int xlat) 1015signame(long sig, int xlat)
1013{ 1016{
1014 static char buf[64]; 1017 static char buf[64];
1015 1018
1016 if (sig == 0) 1019 if (sig == 0)
1017 return " 0"; 1020 return " 0";
1018 else if (sig < 0 || sig >= NSIG) { 1021 else if (sig < 0 || sig >= NSIG) {
1019 (void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig); 1022 (void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig);
1020 return buf; 1023 return buf;
1021 } else 1024 } else
1022 return sys_signame[(xlat && cur_emul->signalmap != NULL) ? 1025 return sys_signame[(xlat && cur_emul->signalmap != NULL) ?
1023 cur_emul->signalmap[sig] : sig]; 1026 cur_emul->signalmap[sig] : sig];
1024} 1027}
1025 1028
1026static void 1029static void
1027usage(void) 1030usage(void)
1028{ 1031{
1029 1032
1030 (void)fprintf(stderr, "Usage: %s [-dlNnRT] [-e emulation] " 1033 (void)fprintf(stderr, "Usage: %s [-dlNnRT] [-e emulation] "
1031 "[-f file] [-m maxdata] [-p pid]\n [-t trstr] " 1034 "[-f file] [-m maxdata] [-p pid]\n [-t trstr] "
1032 "[-x | -X size] [file]\n", getprogname()); 1035 "[-x | -X size] [file]\n", getprogname());
1033 exit(1); 1036 exit(1);
1034} 1037}