2 * Copyright (c) 2004 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/main.c,v 1.16 2008/04/29 20:32:45 jhb Exp $
29 #include <sys/cdefs.h>
31 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #include <sys/resource.h>
36 #include <sys/select.h>
53 #include <frame-unwind.h>
65 #include <exceptions.h>
67 #include <arch-utils.h>
75 static char crashdir[PATH_MAX - 24]; /* allow for appending filenames */
79 static struct ui_file *parse_gdberr;
86 "usage: %s [-afqtvw] [-d crashdir] [-c core | -n dumpnr | -r device]\n"
87 "\t[kernel [core]]\n", getprogname());
92 kernel_from_dumpnr(int nr)
101 * If there's a kernel image right here in the crash directory, then
102 * use it. The kernel image is either called kern.<nr> or is in a
103 * subdirectory kern.<nr> and called kernel. The latter allows us
104 * to collect the modules in the same place.
106 snprintf(path, sizeof(path), "%s/kern.%d", crashdir, nr);
107 if (stat(path, &st) == 0) {
108 if (S_ISREG(st.st_mode)) {
109 kernel = strdup(path);
112 if (S_ISDIR(st.st_mode)) {
113 snprintf(path, sizeof(path), "%s/kern.%d/kernel",
115 if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) {
116 kernel = strdup(path);
123 * No kernel image here. Parse the dump header. The kernel object
124 * directory can be found there and we probably have the kernel
125 * image still in it. The object directory may also have a kernel
126 * with debugging info (called kernel.debug). If we have a debug
129 snprintf(path, sizeof(path), "%s/info.%d", crashdir, nr);
130 info = fopen(path, "r");
135 while (fgets(path, sizeof(path), info) != NULL) {
137 if (l > 0 && path[l - 1] == '\n')
139 if (strncmp(path, " ", 4) == 0) {
140 s = strchr(path, ':');
141 s = (s == NULL) ? path + 4 : s + 1;
142 l = snprintf(path, sizeof(path), "%s/kernel.debug", s);
143 if (stat(path, &st) == -1 || !S_ISREG(st.st_mode)) {
145 if (stat(path, &st) == -1 ||
146 !S_ISREG(st.st_mode))
149 kernel = strdup(path);
157 kgdb_new_objfile(struct objfile *objfile)
161 if (once && objfile != NULL && objfile == symfile_objfile) {
165 * The initial kernel has just been loaded. Start the
166 * remote target if we have one or attach to the core.
171 asprintf(&buf, "target remote %s", remote);
172 else if (vmcore != NULL)
173 asprintf(&buf, "target kernel %s", vmcore);
176 execute_command(buf, 0);
183 gdb_parse_exp_1 (const char **stringptr, struct block *block, int comma,
184 struct expression **expression)
186 volatile struct gdb_exception except;
189 TRY_CATCH (except, RETURN_MASK_ERROR)
191 *expression = parse_exp_1 (stringptr, pc ,block, comma);
194 if (except.reason < 0)
200 gdb_evaluate_expression (struct expression *exp, struct value **value)
202 volatile struct gdb_exception except;
204 TRY_CATCH (except, RETURN_MASK_ERROR)
206 *value = evaluate_expression(exp);
209 if (except.reason < 0)
215 * Parse an expression and return its value. If 'quiet' is true, then
216 * any error messages from the parser are masked.
219 kgdb_parse_1(const char *exp, int quiet)
221 struct ui_file *old_stderr;
222 struct cleanup *old_chain;
223 struct expression *expr;
228 old_stderr = gdb_stderr;
230 gdb_stderr = parse_gdberr;
233 old_chain = make_cleanup(xfree, (char*)s);
234 if (gdb_parse_exp_1(&s, NULL, 0, &expr) && *s == '\0') {
235 make_cleanup(free_current_contents, &expr);
236 if (gdb_evaluate_expression(expr, &val))
237 n = value_as_address(val);
239 do_cleanups(old_chain);
240 gdb_stderr = old_stderr;
244 #define MSGBUF_SEQ_TO_POS(size, seq) ((seq) % (size))
250 int size, rseq, wseq;
254 * Display the unread portion of the message buffer. This gives the
255 * user a some initial data to work from.
259 bufp = kgdb_parse("msgbufp->msg_ptr");
260 size = (int)kgdb_parse("msgbufp->msg_size");
261 if (bufp == 0 || size == 0)
263 rseq = (int)kgdb_parse("msgbufp->msg_bufr");
264 wseq = (int)kgdb_parse("msgbufp->msg_bufx");
265 rseq = MSGBUF_SEQ_TO_POS(size, rseq);
266 wseq = MSGBUF_SEQ_TO_POS(size, wseq);
270 printf("\nUnread portion of the kernel message buffer:\n");
271 while (rseq < wseq) {
272 read_memory(bufp + rseq, &c, 1);
284 kgdb_init(char *argv0 __unused)
287 parse_gdberr = mem_fileopen();
288 set_prompt("(kgdb) ");
289 initialize_kgdb_target();
290 initialize_kld_target();
291 observer_attach_new_objfile(kgdb_new_objfile);
295 * Remote targets can support any number of syntaxes and we want to
296 * support them all with one addition: we support specifying a device
297 * node for a serial device without the "/dev/" prefix.
299 * What we do is to stat(2) the existing remote target first. If that
300 * fails, we try it with "/dev/" prepended. If that succeeds we use
301 * the resulting path, otherwise we use the original target. If
302 * either stat(2) succeeds make sure the file is either a character
311 if (stat(remote, &st) != 0) {
312 snprintf(path, sizeof(path), "/dev/%s", remote);
313 if (stat(path, &st) != 0)
316 remote = strdup(path);
318 if (!S_ISCHR(st.st_mode) && !S_ISFIFO(st.st_mode))
319 errx(1, "%s: not a special file, FIFO or socket", remote);
323 add_arg(struct captured_main_args *args, char *arg)
327 args->argv = reallocf(args->argv, (args->argc + 1) * sizeof(char *));
328 if (args->argv == NULL)
329 err(1, "Out of memory building argument list");
330 args->argv[args->argc] = arg;
334 main(int argc, char *argv[])
338 struct captured_main_args args;
344 strlcpy(crashdir, "/var/crash", sizeof(crashdir));
345 s = getenv("KGDB_CRASH_DIR");
347 strlcpy(crashdir, s, sizeof(crashdir));
349 /* Convert long options into short options. */
350 for (a = 1; a < argc; a++) {
354 /* Long options take either 1 or 2 dashes. */
357 if (strcmp(s, "quiet") == 0)
359 else if (strcmp(s, "fullname") == 0)
361 else if (strcmp(s, "tui-mode") == 0)
367 memset (&args, 0, sizeof args);
368 args.use_windows = 0;
369 args.interpreter_p = INTERP_CONSOLE;
370 args.argv = malloc(sizeof(char *));
371 args.argv[0] = argv[0];
372 add_arg(&args, "--kernel");
374 while ((ch = getopt(argc, argv, "ac:d:fn:qr:tvw")) != -1) {
379 case 'c': /* use given core file. */
380 if (vmcore != NULL) {
381 warnx("option %c: can only be specified once",
386 vmcore = strdup(optarg);
388 case 'd': /* lookup dumps in given directory. */
389 strlcpy(crashdir, optarg, sizeof(crashdir));
392 annotation_level = 1;
394 case 'n': /* use dump with given number. */
395 dumpnr = strtol(optarg, &s, 0);
396 if (dumpnr < 0 || *s != '\0') {
397 warnx("option %c: invalid kernel dump number",
405 add_arg(&args, "-q");
407 case 'r': /* use given device for remote session. */
408 if (remote != NULL) {
409 warnx("option %c: can only be specified once",
414 remote = strdup(optarg);
417 args.interpreter_p = INTERP_TUI;
418 add_arg(&args, "-tui");
420 add_arg(&args, "-q");
422 case 'v': /* increase verbosity. */
425 case 'w': /* core file is writeable. */
426 add_arg(&args, "--write");
434 if (((vmcore != NULL) ? 1 : 0) + ((dumpnr >= 0) ? 1 : 0) +
435 ((remote != NULL) ? 1 : 0) > 1) {
436 warnx("options -c, -n and -r are mutually exclusive");
442 warnx("using %s as the crash directory", crashdir);
445 kernel = strdup(argv[optind++]);
447 if (argc > optind && (dumpnr >= 0 || remote != NULL)) {
448 warnx("options -n and -r do not take a core file. Ignored");
453 snprintf(path, sizeof(path), "%s/vmcore.%d", crashdir, dumpnr);
454 if (stat(path, &st) == -1)
456 if (!S_ISREG(st.st_mode))
457 errx(1, "%s: not a regular file", path);
458 vmcore = strdup(path);
459 } else if (remote != NULL) {
461 } else if (argc > optind) {
463 vmcore = strdup(argv[optind++]);
465 warnx("multiple core files specified. Ignored");
466 } else if (vmcore == NULL && kernel == NULL) {
467 vmcore = strdup(_PATH_MEM);
468 kernel = strdup(getbootfile());
473 warnx("core file: %s", vmcore);
475 warnx("device file: %s", remote);
477 warnx("kernel image: %s", kernel);
480 /* A remote target requires an explicit kernel argument. */
481 if (remote != NULL && kernel == NULL) {
482 warnx("remote debugging requires a kernel");
487 /* If we don't have a kernel image yet, try to find one. */
488 if (kernel == NULL) {
490 kernel_from_dumpnr(dumpnr);
493 errx(1, "couldn't find a suitable kernel image");
495 warnx("kernel image: %s", kernel);
497 add_arg(&args, kernel);
501 add_arg(&args, vmcore);
504 /* The libgdb code uses optind too. Reset it... */
507 /* Terminate argv list. */
508 add_arg(&args, NULL);
510 deprecated_init_ui_hook = kgdb_init;
512 return (gdb_main(&args));