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