Home | History | Annotate | Line # | Download | only in crash
crash.c revision 1.10.2.1
      1  1.10.2.1  pgoyette /*	$NetBSD: crash.c,v 1.10.2.1 2017/03/20 06:58:07 pgoyette Exp $	*/
      2       1.1        ad 
      3       1.1        ad /*-
      4       1.1        ad  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5       1.1        ad  * All rights reserved.
      6       1.1        ad  *
      7       1.1        ad  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1        ad  * by Andrew Doran.
      9       1.1        ad  *
     10       1.1        ad  * Redistribution and use in source and binary forms, with or without
     11       1.1        ad  * modification, are permitted provided that the following conditions
     12       1.1        ad  * are met:
     13       1.1        ad  * 1. Redistributions of source code must retain the above copyright
     14       1.1        ad  *    notice, this list of conditions and the following disclaimer.
     15       1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     17       1.1        ad  *    documentation and/or other materials provided with the distribution.
     18       1.1        ad  *
     19       1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1        ad  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1        ad  */
     31       1.1        ad 
     32       1.1        ad #include <sys/cdefs.h>
     33       1.1        ad #ifndef lint
     34  1.10.2.1  pgoyette __RCSID("$NetBSD: crash.c,v 1.10.2.1 2017/03/20 06:58:07 pgoyette Exp $");
     35       1.1        ad #endif /* not lint */
     36       1.1        ad 
     37       1.1        ad #include <ddb/ddb.h>
     38       1.1        ad 
     39       1.1        ad #include <sys/fcntl.h>
     40       1.1        ad #include <sys/mman.h>
     41  1.10.2.1  pgoyette #include <sys/stat.h>
     42       1.1        ad #include <sys/ioctl.h>
     43       1.1        ad 
     44       1.1        ad #include <machine/frame.h>
     45       1.1        ad 
     46       1.1        ad #include <stdarg.h>
     47       1.1        ad #include <stdlib.h>
     48       1.1        ad #include <unistd.h>
     49       1.1        ad #include <getopt.h>
     50       1.1        ad #include <errno.h>
     51       1.1        ad #include <histedit.h>
     52       1.1        ad #include <paths.h>
     53       1.1        ad #include <kvm.h>
     54       1.1        ad #include <err.h>
     55       1.1        ad #include <ctype.h>
     56       1.3  christos #include <util.h>
     57       1.1        ad 
     58       1.1        ad #include "extern.h"
     59       1.1        ad 
     60       1.1        ad #define	MAXSTAB	(16 * 1024 * 1024)
     61       1.1        ad 
     62       1.1        ad db_regs_t	ddb_regs;
     63       1.3  christos 
     64       1.3  christos static kvm_t		*kd;
     65       1.3  christos static History		*hist;
     66       1.3  christos static HistEvent	he;
     67       1.3  christos static EditLine		*elptr;
     68       1.3  christos static char		imgrelease[16];
     69       1.3  christos static FILE		*ofp;
     70       1.1        ad 
     71       1.1        ad static struct nlist nl[] = {
     72       1.1        ad #define	X_OSRELEASE	0
     73       1.1        ad 	{ .n_name = "_osrelease" },
     74       1.1        ad #define	X_PANICSTR	1
     75       1.1        ad 	{ .n_name = "_panicstr" },
     76       1.1        ad 	{ .n_name = NULL },
     77       1.1        ad };
     78       1.1        ad 
     79       1.3  christos static void
     80       1.3  christos cleanup(void)
     81       1.3  christos {
     82       1.3  christos 	if (ofp != stdout) {
     83       1.3  christos 		(void)fflush(ofp);
     84       1.3  christos 		(void)pclose(ofp);
     85       1.3  christos 		ofp = stdout;
     86       1.3  christos 	}
     87       1.3  christos 	el_end(elptr);
     88       1.3  christos 	history_end(hist);
     89       1.3  christos }
     90       1.3  christos 
     91       1.1        ad void
     92       1.1        ad db_vprintf(const char *fmt, va_list ap)
     93       1.1        ad {
     94       1.1        ad 	char buf[1024];
     95       1.1        ad 	int b, c;
     96       1.1        ad 
     97       1.1        ad 	c = vsnprintf(buf, sizeof(buf), fmt, ap);
     98       1.1        ad 	for (b = 0; b < c; b++) {
     99       1.1        ad 		db_putchar(buf[b]);
    100       1.1        ad 	}
    101       1.1        ad }
    102       1.1        ad 
    103       1.1        ad void
    104       1.1        ad db_printf(const char *fmt, ...)
    105       1.1        ad {
    106       1.1        ad 	va_list ap;
    107       1.1        ad 
    108       1.1        ad 	va_start(ap, fmt);
    109       1.1        ad 	db_vprintf(fmt, ap);
    110       1.1        ad 	va_end(ap);
    111       1.1        ad }
    112       1.1        ad 
    113       1.1        ad void
    114       1.1        ad db_write_bytes(db_addr_t addr, size_t size, const char *str)
    115       1.1        ad {
    116       1.1        ad 
    117       1.2     lukem 	if ((size_t)kvm_write(kd, addr, str, size) != size) {
    118       1.1        ad 		warnx("kvm_write(%p, %zd): %s", (void *)addr, size,
    119       1.1        ad 		    kvm_geterr(kd));
    120       1.1        ad 		longjmp(db_recover);
    121       1.1        ad 	}
    122       1.1        ad }
    123       1.1        ad 
    124       1.1        ad void
    125       1.1        ad db_read_bytes(db_addr_t addr, size_t size, char *str)
    126       1.1        ad {
    127       1.1        ad 
    128       1.2     lukem 	if ((size_t)kvm_read(kd, addr, str, size) != size) {
    129       1.1        ad 		warnx("kvm_read(%p, %zd): %s", (void *)addr, size,
    130       1.1        ad 		    kvm_geterr(kd));
    131       1.1        ad 		longjmp(db_recover);
    132       1.1        ad 	}
    133       1.1        ad }
    134       1.1        ad 
    135       1.1        ad void *
    136       1.1        ad db_alloc(size_t sz)
    137       1.1        ad {
    138       1.1        ad 
    139       1.3  christos 	return emalloc(sz);
    140       1.1        ad }
    141       1.1        ad 
    142       1.1        ad void *
    143       1.1        ad db_zalloc(size_t sz)
    144       1.1        ad {
    145       1.1        ad 
    146       1.3  christos 	return ecalloc(1, sz);
    147       1.1        ad }
    148       1.1        ad 
    149       1.1        ad void
    150       1.1        ad db_free(void *p, size_t sz)
    151       1.1        ad {
    152       1.1        ad 
    153       1.1        ad 	free(p);
    154       1.1        ad }
    155       1.1        ad 
    156       1.1        ad static void
    157       1.1        ad punt(void)
    158       1.1        ad {
    159       1.1        ad 
    160       1.1        ad 	db_printf("This command can only be used in-kernel.\n");
    161       1.1        ad }
    162       1.1        ad 
    163       1.1        ad void
    164       1.1        ad db_breakpoint_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    165       1.1        ad     const char *modif)
    166       1.1        ad {
    167       1.1        ad 
    168       1.1        ad 	punt();
    169       1.1        ad }
    170       1.1        ad 
    171       1.1        ad void
    172       1.1        ad db_continue_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    173       1.1        ad     const char *modif)
    174       1.1        ad {
    175       1.1        ad 
    176       1.1        ad 	db_cmd_loop_done = true;
    177       1.1        ad }
    178       1.1        ad 
    179       1.1        ad void
    180       1.1        ad db_delete_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    181       1.1        ad     const char *modif)
    182       1.1        ad {
    183       1.1        ad 
    184       1.1        ad 	punt();
    185       1.1        ad }
    186       1.1        ad 
    187       1.1        ad void
    188       1.1        ad db_deletewatch_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    189       1.1        ad     const char *modif)
    190       1.1        ad {
    191       1.1        ad 
    192       1.1        ad 	punt();
    193       1.1        ad }
    194       1.1        ad 
    195       1.1        ad 
    196       1.1        ad void
    197       1.1        ad db_trace_until_matching_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    198       1.1        ad     const char *modif)
    199       1.1        ad {
    200       1.1        ad 
    201       1.1        ad 	punt();
    202       1.1        ad }
    203       1.1        ad 
    204       1.1        ad 
    205       1.1        ad void
    206       1.1        ad db_single_step_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    207       1.1        ad     const char *modif)
    208       1.1        ad {
    209       1.1        ad 
    210       1.1        ad 	punt();
    211       1.1        ad }
    212       1.1        ad 
    213       1.1        ad 
    214       1.1        ad void
    215       1.1        ad db_trace_until_call_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    216       1.1        ad     const char *modif)
    217       1.1        ad {
    218       1.1        ad 
    219       1.1        ad 	punt();
    220       1.1        ad }
    221       1.1        ad 
    222       1.1        ad 
    223       1.1        ad void
    224       1.1        ad db_watchpoint_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    225       1.1        ad     const char *modif)
    226       1.1        ad {
    227       1.1        ad 
    228       1.1        ad 	punt();
    229       1.1        ad }
    230       1.1        ad 
    231       1.1        ad int
    232       1.1        ad db_readline(char *lstart, int lsize)
    233       1.1        ad {
    234       1.1        ad 	const char *el;
    235       1.1        ad 	char *pcmd;
    236       1.1        ad 	int cnt;
    237       1.1        ad 
    238       1.1        ad 	db_force_whitespace();
    239       1.1        ad 
    240       1.1        ad 	/* Close any open pipe. */
    241       1.1        ad 	if (ofp != stdout) {
    242       1.1        ad 		(void)fflush(ofp);
    243       1.1        ad 		(void)pclose(ofp);
    244       1.1        ad 		ofp = stdout;
    245       1.1        ad 	}
    246       1.1        ad 
    247       1.1        ad 	/* Read next command. */
    248       1.1        ad 	el = el_gets(elptr, &cnt);
    249       1.3  christos 	if (el == NULL) {	/* EOF */
    250       1.3  christos 		exit(EXIT_SUCCESS);
    251       1.1        ad 	}
    252       1.1        ad 
    253       1.1        ad 	/* Save to history, and copy to caller's buffer. */
    254       1.1        ad 	history(hist, &he, H_ENTER, el);
    255       1.1        ad 	strlcpy(lstart, el, lsize);
    256       1.1        ad 	if (cnt >= lsize) {
    257       1.1        ad 		cnt = lsize - 1;
    258       1.1        ad 	}
    259       1.1        ad 	lstart[cnt] = '\0';
    260       1.1        ad 	if (cnt > 0 && lstart[cnt - 1] == '\n') {
    261       1.1        ad 		lstart[cnt - 1] = '\0';
    262       1.1        ad 	}
    263       1.1        ad 
    264       1.1        ad 	/* Need to open a pipe? If not, return now. */
    265       1.1        ad 	pcmd = strchr(lstart, '|');
    266       1.1        ad 	if (pcmd == NULL) {
    267       1.1        ad 		return strlen(lstart);
    268       1.1        ad 	}
    269       1.1        ad 
    270       1.1        ad 	/* Open a pipe to specified command, redirect output. */
    271       1.1        ad 	assert(ofp == stdout);
    272       1.3  christos 	for (*pcmd++ = '\0'; isspace((unsigned char)*pcmd); pcmd++) {
    273       1.1        ad 		/* nothing */
    274       1.1        ad 	}
    275       1.1        ad 	errno = 0;
    276       1.1        ad 	ofp = popen(pcmd, "w");
    277       1.1        ad 	if (ofp == NULL) {
    278       1.1        ad 		warn("opening pipe for command `%s'", pcmd);
    279       1.1        ad 		*lstart = '\0';
    280       1.1        ad 	}
    281       1.1        ad 	return strlen(lstart);
    282       1.1        ad }
    283       1.1        ad 
    284       1.1        ad void
    285       1.1        ad db_check_interrupt(void)
    286       1.1        ad {
    287       1.1        ad 
    288       1.1        ad }
    289       1.1        ad 
    290       1.1        ad int
    291       1.1        ad cngetc(void)
    292       1.1        ad {
    293       1.1        ad 
    294       1.1        ad 	fprintf(stderr, "cngetc\n");
    295       1.1        ad 	abort();
    296       1.1        ad }
    297       1.1        ad 
    298       1.1        ad void
    299       1.1        ad cnputc(int c)
    300       1.1        ad {
    301       1.1        ad 
    302       1.1        ad 	putc(c, ofp);
    303       1.1        ad }
    304       1.1        ad 
    305       1.4     joerg __dead static void
    306       1.1        ad usage(void)
    307       1.1        ad {
    308       1.1        ad 
    309       1.1        ad 	fprintf(stderr,
    310       1.7       wiz 	    "usage: %s [-w] [-M core] [-N kernel]\n\n"
    311       1.8  christos 	    "-M core\tspecify memory file (default /dev/mem)\n"
    312       1.7       wiz 	    "-N kernel\tspecify name list file (default /dev/ksyms)\n",
    313       1.1        ad 	    getprogname());
    314       1.1        ad 	exit(EXIT_FAILURE);
    315       1.1        ad }
    316       1.1        ad 
    317       1.1        ad static const char *
    318       1.1        ad prompt(void)
    319       1.1        ad {
    320       1.1        ad 
    321       1.1        ad 	return "crash> ";
    322       1.1        ad }
    323       1.1        ad 
    324       1.1        ad int
    325       1.1        ad main(int argc, char **argv)
    326       1.1        ad {
    327       1.1        ad 	const char *nlistf, *memf;
    328       1.1        ad 	uintptr_t panicstr;
    329       1.1        ad 	struct winsize ws;
    330       1.1        ad 	struct stat sb;
    331       1.1        ad 	size_t sz;
    332       1.1        ad 	void *elf;
    333       1.6  christos 	int fd, ch, flags;
    334       1.1        ad 	char c;
    335       1.1        ad 
    336       1.1        ad 	nlistf = _PATH_KSYMS;
    337       1.1        ad 	memf = _PATH_MEM;
    338       1.1        ad 	ofp = stdout;
    339       1.6  christos 	flags = O_RDONLY;
    340       1.1        ad 
    341       1.3  christos 	setprogname(argv[0]);
    342       1.3  christos 
    343       1.1        ad 	/*
    344       1.1        ad 	 * Parse options.
    345       1.1        ad 	 */
    346       1.6  christos 	while ((ch = getopt(argc, argv, "M:N:w")) != -1) {
    347       1.1        ad 		switch (ch) {
    348       1.1        ad 		case 'M':
    349       1.1        ad 			memf = optarg;
    350       1.1        ad 			break;
    351       1.1        ad 		case 'N':
    352       1.1        ad 			nlistf = optarg;
    353       1.1        ad 			break;
    354       1.6  christos 		case 'w':
    355       1.6  christos 			flags = O_RDWR;
    356       1.6  christos 			break;
    357       1.1        ad 		default:
    358       1.1        ad 			usage();
    359       1.1        ad 		}
    360       1.1        ad 	}
    361       1.1        ad 	argc -= optind;
    362       1.1        ad 	argv += optind;
    363       1.1        ad 
    364       1.1        ad 	/*
    365       1.1        ad 	 * Print a list of images, and allow user to select.
    366       1.1        ad 	 */
    367       1.1        ad 	/* XXX */
    368       1.1        ad 
    369       1.1        ad 	/*
    370       1.1        ad 	 * Open the images (crash dump and symbol table).
    371       1.1        ad 	 */
    372       1.6  christos 	kd = kvm_open(nlistf, memf, NULL, flags, getprogname());
    373       1.1        ad 	if (kd == NULL) {
    374       1.1        ad 		return EXIT_FAILURE;
    375       1.1        ad 	}
    376       1.1        ad 	fd = open(nlistf, O_RDONLY);
    377       1.3  christos 	if (fd == -1)  {
    378       1.3  christos 		err(EXIT_FAILURE, "open `%s'", nlistf);
    379       1.1        ad 	}
    380       1.3  christos 	if (fstat(fd, &sb) == -1) {
    381       1.3  christos 		err(EXIT_FAILURE, "stat `%s'", nlistf);
    382       1.1        ad 	}
    383       1.1        ad 	if ((sb.st_mode & S_IFMT) != S_IFREG) {	/* XXX ksyms */
    384       1.1        ad 		sz = MAXSTAB;
    385       1.1        ad 		elf = malloc(sz);
    386       1.1        ad 		if (elf == NULL) {
    387       1.3  christos 			err(EXIT_FAILURE, "malloc(%zu)", sz);
    388       1.1        ad 		}
    389       1.1        ad 		sz = read(fd, elf, sz);
    390       1.3  christos 		if ((ssize_t)sz == -1) {
    391       1.3  christos 			err(EXIT_FAILURE, "read `%s'", nlistf);
    392       1.1        ad 		}
    393       1.1        ad 		if (sz == MAXSTAB) {
    394       1.3  christos 			errx(EXIT_FAILURE, "symbol table > %d bytes", MAXSTAB);
    395       1.1        ad 		}
    396       1.1        ad 	} else {
    397       1.1        ad 		sz = sb.st_size;
    398       1.5  christos 		elf = mmap(NULL, sz, PROT_READ, MAP_PRIVATE|MAP_FILE, fd, 0);
    399       1.1        ad 		if (elf == MAP_FAILED) {
    400       1.3  christos 			err(EXIT_FAILURE, "mmap `%s'", nlistf);
    401       1.1        ad 		}
    402       1.1        ad 	}
    403       1.1        ad 
    404       1.1        ad 	/*
    405       1.1        ad 	 * Print kernel & crash versions.
    406       1.1        ad 	 */
    407       1.1        ad 	if (kvm_nlist(kd, nl) == -1) {
    408       1.1        ad 		errx(EXIT_FAILURE, "kvm_nlist: %s", kvm_geterr(kd));
    409       1.1        ad 	}
    410       1.1        ad 	if ((size_t)kvm_read(kd, nl[X_OSRELEASE].n_value, imgrelease,
    411       1.1        ad 	    sizeof(imgrelease)) != sizeof(imgrelease)) {
    412       1.1        ad 		errx(EXIT_FAILURE, "cannot read osrelease: %s",
    413       1.1        ad 		    kvm_geterr(kd));
    414       1.1        ad 	}
    415       1.1        ad 	printf("Crash version %s, image version %s.\n", osrelease, imgrelease);
    416       1.1        ad 	if (strcmp(osrelease, imgrelease) != 0) {
    417       1.1        ad 		printf("WARNING: versions differ, you may not be able to "
    418       1.1        ad 		    "examine this image.\n");
    419       1.1        ad 	}
    420       1.1        ad 
    421       1.1        ad 	/*
    422       1.1        ad 	 * Print the panic string, if any.
    423       1.1        ad 	 */
    424       1.1        ad 	if ((size_t)kvm_read(kd, nl[X_PANICSTR].n_value, &panicstr,
    425       1.1        ad 	    sizeof(panicstr)) != sizeof(panicstr)) {
    426       1.1        ad 		errx(EXIT_FAILURE, "cannot read panicstr: %s",
    427       1.1        ad 		    kvm_geterr(kd));
    428       1.1        ad 	}
    429       1.1        ad 	if (strcmp(memf, _PATH_MEM) == 0) {
    430       1.1        ad 		printf("Output from a running system is unreliable.\n");
    431       1.1        ad 	} else if (panicstr == 0) {
    432       1.1        ad 		printf("System does not appear to have panicked.\n");
    433       1.1        ad 	} else {
    434       1.1        ad 		printf("System panicked: ");
    435       1.1        ad 		for (;;) {
    436       1.1        ad 			if ((size_t)kvm_read(kd, panicstr, &c, sizeof(c)) !=
    437       1.1        ad 			    sizeof(c)) {
    438       1.1        ad 				errx(EXIT_FAILURE, "cannot read *panicstr: %s",
    439       1.1        ad 				    kvm_geterr(kd));
    440       1.1        ad 			}
    441       1.1        ad 			if (c == '\0') {
    442       1.1        ad 				break;
    443       1.1        ad 			}
    444       1.1        ad 			putchar(c);
    445       1.1        ad 			panicstr++;
    446       1.1        ad 		}
    447       1.1        ad 		putchar('\n');
    448       1.1        ad 	}
    449       1.1        ad 
    450       1.1        ad 	/*
    451       1.1        ad 	 * Initialize line editing.
    452       1.1        ad 	 */
    453       1.1        ad 	hist = history_init();
    454       1.1        ad 	history(hist, &he, H_SETSIZE, 100);
    455       1.1        ad 	elptr = el_init(getprogname(), stdin, stdout, stderr);
    456       1.1        ad 	el_set(elptr, EL_EDITOR, "emacs");
    457       1.1        ad 	el_set(elptr, EL_SIGNAL, 1);
    458       1.1        ad 	el_set(elptr, EL_HIST, history, hist);
    459       1.1        ad 	el_set(elptr, EL_PROMPT, prompt);
    460       1.1        ad 	el_source(elptr, NULL);
    461       1.1        ad 
    462       1.3  christos 	atexit(cleanup);
    463       1.3  christos 
    464       1.1        ad 	/*
    465       1.1        ad 	 * Initialize ddb.
    466       1.1        ad 	 */
    467       1.1        ad 	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1) {
    468       1.1        ad 		db_max_width = ws.ws_col;
    469       1.1        ad 	}
    470       1.1        ad 	db_mach_init(kd);
    471       1.1        ad 	ddb_init(sz, elf, (char *)elf + sz);
    472       1.1        ad 
    473       1.1        ad 	/*
    474       1.1        ad 	 * Debug it!
    475       1.1        ad 	 */
    476       1.1        ad 	db_command_loop();
    477       1.1        ad 
    478       1.1        ad 	/*
    479       1.3  christos 	 * Finish.
    480       1.1        ad 	 */
    481       1.3  christos 	return EXIT_SUCCESS;
    482       1.1        ad }
    483