18b6a428fSSimon Schubert /*
28b6a428fSSimon Schubert * Copyright (c) 2004 Marcel Moolenaar
38b6a428fSSimon Schubert * All rights reserved.
48b6a428fSSimon Schubert *
58b6a428fSSimon Schubert * Redistribution and use in source and binary forms, with or without
68b6a428fSSimon Schubert * modification, are permitted provided that the following conditions
78b6a428fSSimon Schubert * are met:
88b6a428fSSimon Schubert *
98b6a428fSSimon Schubert * 1. Redistributions of source code must retain the above copyright
108b6a428fSSimon Schubert * notice, this list of conditions and the following disclaimer.
118b6a428fSSimon Schubert * 2. Redistributions in binary form must reproduce the above copyright
128b6a428fSSimon Schubert * notice, this list of conditions and the following disclaimer in the
138b6a428fSSimon Schubert * documentation and/or other materials provided with the distribution.
148b6a428fSSimon Schubert *
158b6a428fSSimon Schubert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
168b6a428fSSimon Schubert * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
178b6a428fSSimon Schubert * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
188b6a428fSSimon Schubert * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
198b6a428fSSimon Schubert * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
208b6a428fSSimon Schubert * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
218b6a428fSSimon Schubert * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
228b6a428fSSimon Schubert * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
238b6a428fSSimon Schubert * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
248b6a428fSSimon Schubert * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
258b6a428fSSimon Schubert *
26f17ea84bSSimon Schubert * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/main.c,v 1.16 2008/04/29 20:32:45 jhb Exp $
278b6a428fSSimon Schubert */
288b6a428fSSimon Schubert
298b6a428fSSimon Schubert #include <sys/cdefs.h>
308b6a428fSSimon Schubert
318b6a428fSSimon Schubert #include <sys/param.h>
328b6a428fSSimon Schubert #include <sys/stat.h>
338b6a428fSSimon Schubert #include <sys/types.h>
348b6a428fSSimon Schubert #include <sys/ioctl.h>
358b6a428fSSimon Schubert #include <sys/resource.h>
368b6a428fSSimon Schubert #include <sys/select.h>
378b6a428fSSimon Schubert #include <sys/time.h>
388b6a428fSSimon Schubert #include <sys/wait.h>
398b6a428fSSimon Schubert #include <errno.h>
408b6a428fSSimon Schubert #include <err.h>
418b6a428fSSimon Schubert #include <inttypes.h>
428b6a428fSSimon Schubert #include <kvm.h>
438b6a428fSSimon Schubert #include <limits.h>
448b6a428fSSimon Schubert #include <paths.h>
458b6a428fSSimon Schubert #include <stdio.h>
468b6a428fSSimon Schubert #include <stdlib.h>
478b6a428fSSimon Schubert #include <string.h>
488b6a428fSSimon Schubert #include <unistd.h>
498b6a428fSSimon Schubert
508b6a428fSSimon Schubert /* libgdb stuff. */
518b6a428fSSimon Schubert #include <defs.h>
528b6a428fSSimon Schubert #include <frame.h>
538b6a428fSSimon Schubert #include <frame-unwind.h>
548b6a428fSSimon Schubert #include <inferior.h>
558b6a428fSSimon Schubert #include <interps.h>
568b6a428fSSimon Schubert #include <cli-out.h>
578b6a428fSSimon Schubert #include <main.h>
58f17ea84bSSimon Schubert #include <gdbcmd.h>
59f17ea84bSSimon Schubert #include <objfiles.h>
608b6a428fSSimon Schubert #include <target.h>
618b6a428fSSimon Schubert #include <top.h>
62f17ea84bSSimon Schubert #include <ui-file.h>
638b6a428fSSimon Schubert #include <bfd.h>
648b6a428fSSimon Schubert #include <gdbcore.h>
65c33252afSJohn Marino #include <exceptions.h>
66f17ea84bSSimon Schubert #include <observer.h>
67939fa31eSMatthew Dillon #include <arch-utils.h>
688b6a428fSSimon Schubert
698b6a428fSSimon Schubert #include "kgdb.h"
708b6a428fSSimon Schubert
718b6a428fSSimon Schubert static int dumpnr;
72f17ea84bSSimon Schubert static int quiet;
738b6a428fSSimon Schubert static int verbose;
748b6a428fSSimon Schubert
75*f4a9d1afSzrj static char crashdir[PATH_MAX - 24]; /* allow for appending filenames */
768b6a428fSSimon Schubert static char *kernel;
778b6a428fSSimon Schubert static char *remote;
788b6a428fSSimon Schubert static char *vmcore;
79f17ea84bSSimon Schubert static struct ui_file *parse_gdberr;
808b6a428fSSimon Schubert
818b6a428fSSimon Schubert static void
usage(void)828b6a428fSSimon Schubert usage(void)
838b6a428fSSimon Schubert {
848b6a428fSSimon Schubert
858b6a428fSSimon Schubert fprintf(stderr,
865cd406e0SSascha Wildner "usage: %s [-afqtvw] [-d crashdir] [-c core | -n dumpnr | -r device]\n"
878b6a428fSSimon Schubert "\t[kernel [core]]\n", getprogname());
888b6a428fSSimon Schubert exit(1);
898b6a428fSSimon Schubert }
908b6a428fSSimon Schubert
918b6a428fSSimon Schubert static void
kernel_from_dumpnr(int nr)928b6a428fSSimon Schubert kernel_from_dumpnr(int nr)
938b6a428fSSimon Schubert {
948b6a428fSSimon Schubert char path[PATH_MAX];
958b6a428fSSimon Schubert FILE *info;
968b6a428fSSimon Schubert char *s;
978b6a428fSSimon Schubert struct stat st;
988b6a428fSSimon Schubert int l;
998b6a428fSSimon Schubert
1008b6a428fSSimon Schubert /*
1018b6a428fSSimon Schubert * If there's a kernel image right here in the crash directory, then
1029a3588c6SSascha Wildner * use it. The kernel image is either called kern.<nr> or is in a
1039a3588c6SSascha Wildner * subdirectory kern.<nr> and called kernel. The latter allows us
1048b6a428fSSimon Schubert * to collect the modules in the same place.
1058b6a428fSSimon Schubert */
106c0a6a6f9SSascha Wildner snprintf(path, sizeof(path), "%s/kern.%d", crashdir, nr);
1078b6a428fSSimon Schubert if (stat(path, &st) == 0) {
1088b6a428fSSimon Schubert if (S_ISREG(st.st_mode)) {
1098b6a428fSSimon Schubert kernel = strdup(path);
1108b6a428fSSimon Schubert return;
1118b6a428fSSimon Schubert }
1128b6a428fSSimon Schubert if (S_ISDIR(st.st_mode)) {
113c0a6a6f9SSascha Wildner snprintf(path, sizeof(path), "%s/kern.%d/kernel",
1148b6a428fSSimon Schubert crashdir, nr);
1158b6a428fSSimon Schubert if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) {
1168b6a428fSSimon Schubert kernel = strdup(path);
1178b6a428fSSimon Schubert return;
1188b6a428fSSimon Schubert }
1198b6a428fSSimon Schubert }
1208b6a428fSSimon Schubert }
1218b6a428fSSimon Schubert
1228b6a428fSSimon Schubert /*
1238b6a428fSSimon Schubert * No kernel image here. Parse the dump header. The kernel object
1248b6a428fSSimon Schubert * directory can be found there and we probably have the kernel
1258b6a428fSSimon Schubert * image still in it. The object directory may also have a kernel
1268b6a428fSSimon Schubert * with debugging info (called kernel.debug). If we have a debug
1278b6a428fSSimon Schubert * kernel, use it.
1288b6a428fSSimon Schubert */
1298b6a428fSSimon Schubert snprintf(path, sizeof(path), "%s/info.%d", crashdir, nr);
1308b6a428fSSimon Schubert info = fopen(path, "r");
1318b6a428fSSimon Schubert if (info == NULL) {
132a938cc15SJohn Marino warn("%s", path);
1338b6a428fSSimon Schubert return;
1348b6a428fSSimon Schubert }
1358b6a428fSSimon Schubert while (fgets(path, sizeof(path), info) != NULL) {
1368b6a428fSSimon Schubert l = strlen(path);
1378b6a428fSSimon Schubert if (l > 0 && path[l - 1] == '\n')
1388b6a428fSSimon Schubert path[--l] = '\0';
1398b6a428fSSimon Schubert if (strncmp(path, " ", 4) == 0) {
1408b6a428fSSimon Schubert s = strchr(path, ':');
1418b6a428fSSimon Schubert s = (s == NULL) ? path + 4 : s + 1;
1428b6a428fSSimon Schubert l = snprintf(path, sizeof(path), "%s/kernel.debug", s);
1438b6a428fSSimon Schubert if (stat(path, &st) == -1 || !S_ISREG(st.st_mode)) {
1448b6a428fSSimon Schubert path[l - 6] = '\0';
1458b6a428fSSimon Schubert if (stat(path, &st) == -1 ||
1468b6a428fSSimon Schubert !S_ISREG(st.st_mode))
1478b6a428fSSimon Schubert break;
1488b6a428fSSimon Schubert }
1498b6a428fSSimon Schubert kernel = strdup(path);
1508b6a428fSSimon Schubert break;
1518b6a428fSSimon Schubert }
1528b6a428fSSimon Schubert }
1538b6a428fSSimon Schubert fclose(info);
1548b6a428fSSimon Schubert }
1558b6a428fSSimon Schubert
1568b6a428fSSimon Schubert static void
kgdb_new_objfile(struct objfile * objfile)1578b6a428fSSimon Schubert kgdb_new_objfile(struct objfile *objfile)
1588b6a428fSSimon Schubert {
159f17ea84bSSimon Schubert static int once = 1;
160f17ea84bSSimon Schubert
161f17ea84bSSimon Schubert if (once && objfile != NULL && objfile == symfile_objfile) {
162f17ea84bSSimon Schubert char *buf;
163f17ea84bSSimon Schubert
164f17ea84bSSimon Schubert /*
165f17ea84bSSimon Schubert * The initial kernel has just been loaded. Start the
166f17ea84bSSimon Schubert * remote target if we have one or attach to the core.
167f17ea84bSSimon Schubert */
168f17ea84bSSimon Schubert once = 0;
169f17ea84bSSimon Schubert
170f17ea84bSSimon Schubert if (remote != NULL)
171f17ea84bSSimon Schubert asprintf(&buf, "target remote %s", remote);
172f17ea84bSSimon Schubert else if (vmcore != NULL)
173f17ea84bSSimon Schubert asprintf(&buf, "target kernel %s", vmcore);
174f17ea84bSSimon Schubert
175f17ea84bSSimon Schubert if (buf != NULL) {
176f17ea84bSSimon Schubert execute_command(buf, 0);
177f17ea84bSSimon Schubert free(buf);
178f17ea84bSSimon Schubert }
179f17ea84bSSimon Schubert }
1808b6a428fSSimon Schubert }
1818b6a428fSSimon Schubert
182c33252afSJohn Marino int
gdb_parse_exp_1(const char ** stringptr,struct block * block,int comma,struct expression ** expression)183c33252afSJohn Marino gdb_parse_exp_1 (const char **stringptr, struct block *block, int comma,
184c33252afSJohn Marino struct expression **expression)
185c33252afSJohn Marino {
186c33252afSJohn Marino volatile struct gdb_exception except;
187c33252afSJohn Marino CORE_ADDR pc = 0;
188c33252afSJohn Marino
189c33252afSJohn Marino TRY_CATCH (except, RETURN_MASK_ERROR)
190c33252afSJohn Marino {
191c33252afSJohn Marino *expression = parse_exp_1 (stringptr, pc ,block, comma);
192c33252afSJohn Marino }
193c33252afSJohn Marino
194c33252afSJohn Marino if (except.reason < 0)
195c33252afSJohn Marino return 0;
196c33252afSJohn Marino return 1;
197c33252afSJohn Marino }
198c33252afSJohn Marino
199c33252afSJohn Marino int
gdb_evaluate_expression(struct expression * exp,struct value ** value)200c33252afSJohn Marino gdb_evaluate_expression (struct expression *exp, struct value **value)
201c33252afSJohn Marino {
202c33252afSJohn Marino volatile struct gdb_exception except;
203c33252afSJohn Marino
204c33252afSJohn Marino TRY_CATCH (except, RETURN_MASK_ERROR)
205c33252afSJohn Marino {
206c33252afSJohn Marino *value = evaluate_expression(exp);
207c33252afSJohn Marino }
208c33252afSJohn Marino
209c33252afSJohn Marino if (except.reason < 0)
210c33252afSJohn Marino return 0;
211c33252afSJohn Marino return 1;
212c33252afSJohn Marino }
213c33252afSJohn Marino
214f17ea84bSSimon Schubert /*
215f17ea84bSSimon Schubert * Parse an expression and return its value. If 'quiet' is true, then
216f17ea84bSSimon Schubert * any error messages from the parser are masked.
217f17ea84bSSimon Schubert */
218f17ea84bSSimon Schubert CORE_ADDR
kgdb_parse_1(const char * exp,int quiet)219f17ea84bSSimon Schubert kgdb_parse_1(const char *exp, int quiet)
2208b6a428fSSimon Schubert {
221f17ea84bSSimon Schubert struct ui_file *old_stderr;
2228b6a428fSSimon Schubert struct cleanup *old_chain;
2238b6a428fSSimon Schubert struct expression *expr;
2248b6a428fSSimon Schubert struct value *val;
225c33252afSJohn Marino const char *s;
2268b6a428fSSimon Schubert CORE_ADDR n;
2278b6a428fSSimon Schubert
228f17ea84bSSimon Schubert old_stderr = gdb_stderr;
229f17ea84bSSimon Schubert if (quiet)
230f17ea84bSSimon Schubert gdb_stderr = parse_gdberr;
231f17ea84bSSimon Schubert n = 0;
232f17ea84bSSimon Schubert s = xstrdup(exp);
233c33252afSJohn Marino old_chain = make_cleanup(xfree, (char*)s);
234f17ea84bSSimon Schubert if (gdb_parse_exp_1(&s, NULL, 0, &expr) && *s == '\0') {
235f17ea84bSSimon Schubert make_cleanup(free_current_contents, &expr);
236f17ea84bSSimon Schubert if (gdb_evaluate_expression(expr, &val))
237f17ea84bSSimon Schubert n = value_as_address(val);
238f17ea84bSSimon Schubert }
2398b6a428fSSimon Schubert do_cleanups(old_chain);
240f17ea84bSSimon Schubert gdb_stderr = old_stderr;
2418b6a428fSSimon Schubert return (n);
2428b6a428fSSimon Schubert }
2438b6a428fSSimon Schubert
2448b6a428fSSimon Schubert #define MSGBUF_SEQ_TO_POS(size, seq) ((seq) % (size))
2458b6a428fSSimon Schubert
246f17ea84bSSimon Schubert void
kgdb_dmesg(void)247f17ea84bSSimon Schubert kgdb_dmesg(void)
2483cdd3f79SMatthew Dillon {
249f17ea84bSSimon Schubert CORE_ADDR bufp;
250f17ea84bSSimon Schubert int size, rseq, wseq;
2518b6a428fSSimon Schubert char c;
2528b6a428fSSimon Schubert
2538b6a428fSSimon Schubert /*
2548b6a428fSSimon Schubert * Display the unread portion of the message buffer. This gives the
2558b6a428fSSimon Schubert * user a some initial data to work from.
2568b6a428fSSimon Schubert */
257f17ea84bSSimon Schubert if (quiet)
2588b6a428fSSimon Schubert return;
259f17ea84bSSimon Schubert bufp = kgdb_parse("msgbufp->msg_ptr");
260f17ea84bSSimon Schubert size = (int)kgdb_parse("msgbufp->msg_size");
261a938cc15SJohn Marino if (bufp == 0 || size == 0)
262a938cc15SJohn Marino return;
263f17ea84bSSimon Schubert rseq = (int)kgdb_parse("msgbufp->msg_bufr");
264f17ea84bSSimon Schubert wseq = (int)kgdb_parse("msgbufp->msg_bufx");
265f17ea84bSSimon Schubert rseq = MSGBUF_SEQ_TO_POS(size, rseq);
266f17ea84bSSimon Schubert wseq = MSGBUF_SEQ_TO_POS(size, wseq);
267a938cc15SJohn Marino if (rseq == wseq)
268c499d304SSimon Schubert return;
2698b6a428fSSimon Schubert
2708b6a428fSSimon Schubert printf("\nUnread portion of the kernel message buffer:\n");
2718b6a428fSSimon Schubert while (rseq < wseq) {
272f17ea84bSSimon Schubert read_memory(bufp + rseq, &c, 1);
2738b6a428fSSimon Schubert putchar(c);
2748b6a428fSSimon Schubert rseq++;
275f17ea84bSSimon Schubert if (rseq == size)
2768b6a428fSSimon Schubert rseq = 0;
2778b6a428fSSimon Schubert }
2788b6a428fSSimon Schubert if (c != '\n')
2798b6a428fSSimon Schubert putchar('\n');
2808b6a428fSSimon Schubert putchar('\n');
2818b6a428fSSimon Schubert }
2828b6a428fSSimon Schubert
2838b6a428fSSimon Schubert static void
kgdb_init(char * argv0 __unused)2848b6a428fSSimon Schubert kgdb_init(char *argv0 __unused)
2858b6a428fSSimon Schubert {
2868b6a428fSSimon Schubert
287f17ea84bSSimon Schubert parse_gdberr = mem_fileopen();
2888b6a428fSSimon Schubert set_prompt("(kgdb) ");
289f17ea84bSSimon Schubert initialize_kgdb_target();
290f17ea84bSSimon Schubert initialize_kld_target();
291f17ea84bSSimon Schubert observer_attach_new_objfile(kgdb_new_objfile);
292f17ea84bSSimon Schubert }
293f17ea84bSSimon Schubert
294f17ea84bSSimon Schubert /*
295f17ea84bSSimon Schubert * Remote targets can support any number of syntaxes and we want to
296f17ea84bSSimon Schubert * support them all with one addition: we support specifying a device
297f17ea84bSSimon Schubert * node for a serial device without the "/dev/" prefix.
298f17ea84bSSimon Schubert *
299f17ea84bSSimon Schubert * What we do is to stat(2) the existing remote target first. If that
300f17ea84bSSimon Schubert * fails, we try it with "/dev/" prepended. If that succeeds we use
301f17ea84bSSimon Schubert * the resulting path, otherwise we use the original target. If
302f17ea84bSSimon Schubert * either stat(2) succeeds make sure the file is either a character
303f17ea84bSSimon Schubert * device or a FIFO.
304f17ea84bSSimon Schubert */
305f17ea84bSSimon Schubert static void
verify_remote(void)306f17ea84bSSimon Schubert verify_remote(void)
307f17ea84bSSimon Schubert {
308f17ea84bSSimon Schubert char path[PATH_MAX];
309f17ea84bSSimon Schubert struct stat st;
310f17ea84bSSimon Schubert
311f17ea84bSSimon Schubert if (stat(remote, &st) != 0) {
312f17ea84bSSimon Schubert snprintf(path, sizeof(path), "/dev/%s", remote);
313f17ea84bSSimon Schubert if (stat(path, &st) != 0)
314f17ea84bSSimon Schubert return;
315f17ea84bSSimon Schubert free(remote);
316f17ea84bSSimon Schubert remote = strdup(path);
317f17ea84bSSimon Schubert }
318f17ea84bSSimon Schubert if (!S_ISCHR(st.st_mode) && !S_ISFIFO(st.st_mode))
319f17ea84bSSimon Schubert errx(1, "%s: not a special file, FIFO or socket", remote);
320f17ea84bSSimon Schubert }
321f17ea84bSSimon Schubert
322f17ea84bSSimon Schubert static void
add_arg(struct captured_main_args * args,char * arg)323f17ea84bSSimon Schubert add_arg(struct captured_main_args *args, char *arg)
324f17ea84bSSimon Schubert {
325f17ea84bSSimon Schubert
326f17ea84bSSimon Schubert args->argc++;
327f17ea84bSSimon Schubert args->argv = reallocf(args->argv, (args->argc + 1) * sizeof(char *));
328f17ea84bSSimon Schubert if (args->argv == NULL)
329f17ea84bSSimon Schubert err(1, "Out of memory building argument list");
330f17ea84bSSimon Schubert args->argv[args->argc] = arg;
3318b6a428fSSimon Schubert }
3328b6a428fSSimon Schubert
3338b6a428fSSimon Schubert int
main(int argc,char * argv[])3348b6a428fSSimon Schubert main(int argc, char *argv[])
3358b6a428fSSimon Schubert {
3368b6a428fSSimon Schubert char path[PATH_MAX];
3378b6a428fSSimon Schubert struct stat st;
3388b6a428fSSimon Schubert struct captured_main_args args;
3398b6a428fSSimon Schubert char *s;
340f17ea84bSSimon Schubert int a, ch;
3418b6a428fSSimon Schubert
3428b6a428fSSimon Schubert dumpnr = -1;
3438b6a428fSSimon Schubert
3448b6a428fSSimon Schubert strlcpy(crashdir, "/var/crash", sizeof(crashdir));
3458b6a428fSSimon Schubert s = getenv("KGDB_CRASH_DIR");
3468b6a428fSSimon Schubert if (s != NULL)
3478b6a428fSSimon Schubert strlcpy(crashdir, s, sizeof(crashdir));
3488b6a428fSSimon Schubert
3498b6a428fSSimon Schubert /* Convert long options into short options. */
3508b6a428fSSimon Schubert for (a = 1; a < argc; a++) {
3518b6a428fSSimon Schubert s = argv[a];
3528b6a428fSSimon Schubert if (s[0] == '-') {
3538b6a428fSSimon Schubert s++;
3548b6a428fSSimon Schubert /* Long options take either 1 or 2 dashes. */
3558b6a428fSSimon Schubert if (s[0] == '-')
3568b6a428fSSimon Schubert s++;
3578b6a428fSSimon Schubert if (strcmp(s, "quiet") == 0)
3588b6a428fSSimon Schubert argv[a] = "-q";
3598b6a428fSSimon Schubert else if (strcmp(s, "fullname") == 0)
3608b6a428fSSimon Schubert argv[a] = "-f";
3614c0200b9SJohn Marino else if (strcmp(s, "tui-mode") == 0)
3624c0200b9SJohn Marino argv[a] = "-t";
3638b6a428fSSimon Schubert }
3648b6a428fSSimon Schubert }
3658b6a428fSSimon Schubert
3668b6a428fSSimon Schubert quiet = 0;
367f17ea84bSSimon Schubert memset (&args, 0, sizeof args);
368f17ea84bSSimon Schubert args.use_windows = 0;
369f17ea84bSSimon Schubert args.interpreter_p = INTERP_CONSOLE;
370f17ea84bSSimon Schubert args.argv = malloc(sizeof(char *));
371f17ea84bSSimon Schubert args.argv[0] = argv[0];
372a938cc15SJohn Marino add_arg(&args, "--kernel");
3738b6a428fSSimon Schubert
3744c0200b9SJohn Marino while ((ch = getopt(argc, argv, "ac:d:fn:qr:tvw")) != -1) {
3758b6a428fSSimon Schubert switch (ch) {
3768b6a428fSSimon Schubert case 'a':
3778b6a428fSSimon Schubert annotation_level++;
3788b6a428fSSimon Schubert break;
3798b6a428fSSimon Schubert case 'c': /* use given core file. */
3808b6a428fSSimon Schubert if (vmcore != NULL) {
3818b6a428fSSimon Schubert warnx("option %c: can only be specified once",
3828b6a428fSSimon Schubert optopt);
3838b6a428fSSimon Schubert usage();
3848b6a428fSSimon Schubert /* NOTREACHED */
3858b6a428fSSimon Schubert }
3868b6a428fSSimon Schubert vmcore = strdup(optarg);
3878b6a428fSSimon Schubert break;
3888b6a428fSSimon Schubert case 'd': /* lookup dumps in given directory. */
3898b6a428fSSimon Schubert strlcpy(crashdir, optarg, sizeof(crashdir));
3908b6a428fSSimon Schubert break;
3918b6a428fSSimon Schubert case 'f':
3928b6a428fSSimon Schubert annotation_level = 1;
3938b6a428fSSimon Schubert break;
3948b6a428fSSimon Schubert case 'n': /* use dump with given number. */
3958b6a428fSSimon Schubert dumpnr = strtol(optarg, &s, 0);
3968b6a428fSSimon Schubert if (dumpnr < 0 || *s != '\0') {
3978b6a428fSSimon Schubert warnx("option %c: invalid kernel dump number",
3988b6a428fSSimon Schubert optopt);
3998b6a428fSSimon Schubert usage();
4008b6a428fSSimon Schubert /* NOTREACHED */
4018b6a428fSSimon Schubert }
4028b6a428fSSimon Schubert break;
4038b6a428fSSimon Schubert case 'q':
4048b6a428fSSimon Schubert quiet = 1;
405f17ea84bSSimon Schubert add_arg(&args, "-q");
4068b6a428fSSimon Schubert break;
4078b6a428fSSimon Schubert case 'r': /* use given device for remote session. */
4088b6a428fSSimon Schubert if (remote != NULL) {
4098b6a428fSSimon Schubert warnx("option %c: can only be specified once",
4108b6a428fSSimon Schubert optopt);
4118b6a428fSSimon Schubert usage();
4128b6a428fSSimon Schubert /* NOTREACHED */
4138b6a428fSSimon Schubert }
4148b6a428fSSimon Schubert remote = strdup(optarg);
4158b6a428fSSimon Schubert break;
4164c0200b9SJohn Marino case 't':
4174c0200b9SJohn Marino args.interpreter_p = INTERP_TUI;
4184c0200b9SJohn Marino add_arg(&args, "-tui");
4194c0200b9SJohn Marino quiet = 1;
4204c0200b9SJohn Marino add_arg(&args, "-q");
4214c0200b9SJohn Marino break;
4228b6a428fSSimon Schubert case 'v': /* increase verbosity. */
4238b6a428fSSimon Schubert verbose++;
4248b6a428fSSimon Schubert break;
4258b6a428fSSimon Schubert case 'w': /* core file is writeable. */
426f17ea84bSSimon Schubert add_arg(&args, "--write");
4278b6a428fSSimon Schubert break;
4288b6a428fSSimon Schubert case '?':
4298b6a428fSSimon Schubert default:
4308b6a428fSSimon Schubert usage();
4318b6a428fSSimon Schubert }
4328b6a428fSSimon Schubert }
4338b6a428fSSimon Schubert
4348b6a428fSSimon Schubert if (((vmcore != NULL) ? 1 : 0) + ((dumpnr >= 0) ? 1 : 0) +
4358b6a428fSSimon Schubert ((remote != NULL) ? 1 : 0) > 1) {
4368b6a428fSSimon Schubert warnx("options -c, -n and -r are mutually exclusive");
4378b6a428fSSimon Schubert usage();
4388b6a428fSSimon Schubert /* NOTREACHED */
4398b6a428fSSimon Schubert }
4408b6a428fSSimon Schubert
4418b6a428fSSimon Schubert if (verbose > 1)
4428b6a428fSSimon Schubert warnx("using %s as the crash directory", crashdir);
4438b6a428fSSimon Schubert
4448b6a428fSSimon Schubert if (argc > optind)
4458b6a428fSSimon Schubert kernel = strdup(argv[optind++]);
4468b6a428fSSimon Schubert
4478b6a428fSSimon Schubert if (argc > optind && (dumpnr >= 0 || remote != NULL)) {
4488b6a428fSSimon Schubert warnx("options -n and -r do not take a core file. Ignored");
4498b6a428fSSimon Schubert optind = argc;
4508b6a428fSSimon Schubert }
4518b6a428fSSimon Schubert
4528b6a428fSSimon Schubert if (dumpnr >= 0) {
4538b6a428fSSimon Schubert snprintf(path, sizeof(path), "%s/vmcore.%d", crashdir, dumpnr);
4548b6a428fSSimon Schubert if (stat(path, &st) == -1)
455a938cc15SJohn Marino err(1, "%s", path);
4568b6a428fSSimon Schubert if (!S_ISREG(st.st_mode))
4578b6a428fSSimon Schubert errx(1, "%s: not a regular file", path);
4588b6a428fSSimon Schubert vmcore = strdup(path);
459f17ea84bSSimon Schubert } else if (remote != NULL) {
460f17ea84bSSimon Schubert verify_remote();
4618b6a428fSSimon Schubert } else if (argc > optind) {
4628b6a428fSSimon Schubert if (vmcore == NULL)
4638b6a428fSSimon Schubert vmcore = strdup(argv[optind++]);
4648b6a428fSSimon Schubert if (argc > optind)
4658b6a428fSSimon Schubert warnx("multiple core files specified. Ignored");
4668b6a428fSSimon Schubert } else if (vmcore == NULL && kernel == NULL) {
4678b6a428fSSimon Schubert vmcore = strdup(_PATH_MEM);
4688b6a428fSSimon Schubert kernel = strdup(getbootfile());
4698b6a428fSSimon Schubert }
4708b6a428fSSimon Schubert
4718b6a428fSSimon Schubert if (verbose) {
4728b6a428fSSimon Schubert if (vmcore != NULL)
4738b6a428fSSimon Schubert warnx("core file: %s", vmcore);
4748b6a428fSSimon Schubert if (remote != NULL)
4758b6a428fSSimon Schubert warnx("device file: %s", remote);
4768b6a428fSSimon Schubert if (kernel != NULL)
4778b6a428fSSimon Schubert warnx("kernel image: %s", kernel);
4788b6a428fSSimon Schubert }
4798b6a428fSSimon Schubert
480f17ea84bSSimon Schubert /* A remote target requires an explicit kernel argument. */
4818b6a428fSSimon Schubert if (remote != NULL && kernel == NULL) {
4828b6a428fSSimon Schubert warnx("remote debugging requires a kernel");
4838b6a428fSSimon Schubert usage();
4848b6a428fSSimon Schubert /* NOTREACHED */
4858b6a428fSSimon Schubert }
4868b6a428fSSimon Schubert
4878b6a428fSSimon Schubert /* If we don't have a kernel image yet, try to find one. */
4888b6a428fSSimon Schubert if (kernel == NULL) {
4898b6a428fSSimon Schubert if (dumpnr >= 0)
4908b6a428fSSimon Schubert kernel_from_dumpnr(dumpnr);
4918b6a428fSSimon Schubert
4928b6a428fSSimon Schubert if (kernel == NULL)
4938b6a428fSSimon Schubert errx(1, "couldn't find a suitable kernel image");
4948b6a428fSSimon Schubert if (verbose)
4958b6a428fSSimon Schubert warnx("kernel image: %s", kernel);
4968b6a428fSSimon Schubert }
497f17ea84bSSimon Schubert add_arg(&args, kernel);
4988b6a428fSSimon Schubert
499f17ea84bSSimon Schubert /*
500f17ea84bSSimon Schubert if (vmcore != NULL)
501f17ea84bSSimon Schubert add_arg(&args, vmcore);
502f17ea84bSSimon Schubert */
5038b6a428fSSimon Schubert
5048b6a428fSSimon Schubert /* The libgdb code uses optind too. Reset it... */
5058b6a428fSSimon Schubert optind = 0;
5068b6a428fSSimon Schubert
507f17ea84bSSimon Schubert /* Terminate argv list. */
508f17ea84bSSimon Schubert add_arg(&args, NULL);
5098b6a428fSSimon Schubert
5108b6a428fSSimon Schubert deprecated_init_ui_hook = kgdb_init;
5118b6a428fSSimon Schubert
5128b6a428fSSimon Schubert return (gdb_main(&args));
5138b6a428fSSimon Schubert }
514