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>
74 static char crashdir[PATH_MAX];
78 static struct ui_file *parse_gdberr;
85 "usage: %s [-afqv] [-d crashdir] [-c core | -n dumpnr | -r device]\n"
86 "\t[kernel [core]]\n", getprogname());
91 kernel_from_dumpnr(int nr)
100 * If there's a kernel image right here in the crash directory, then
101 * use it. The kernel image is either called kernel.<nr> or is in a
102 * subdirectory kernel.<nr> and called kernel. The latter allows us
103 * to collect the modules in the same place.
105 snprintf(path, sizeof(path), "%s/kernel.%d", crashdir, nr);
106 if (stat(path, &st) == 0) {
107 if (S_ISREG(st.st_mode)) {
108 kernel = strdup(path);
111 if (S_ISDIR(st.st_mode)) {
112 snprintf(path, sizeof(path), "%s/kernel.%d/kernel",
114 if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) {
115 kernel = strdup(path);
122 * No kernel image here. Parse the dump header. The kernel object
123 * directory can be found there and we probably have the kernel
124 * image still in it. The object directory may also have a kernel
125 * with debugging info (called kernel.debug). If we have a debug
128 snprintf(path, sizeof(path), "%s/info.%d", crashdir, nr);
129 info = fopen(path, "r");
134 while (fgets(path, sizeof(path), info) != NULL) {
136 if (l > 0 && path[l - 1] == '\n')
138 if (strncmp(path, " ", 4) == 0) {
139 s = strchr(path, ':');
140 s = (s == NULL) ? path + 4 : s + 1;
141 l = snprintf(path, sizeof(path), "%s/kernel.debug", s);
142 if (stat(path, &st) == -1 || !S_ISREG(st.st_mode)) {
144 if (stat(path, &st) == -1 ||
145 !S_ISREG(st.st_mode))
148 kernel = strdup(path);
156 kgdb_new_objfile(struct objfile *objfile)
160 if (once && objfile != NULL && objfile == symfile_objfile) {
164 * The initial kernel has just been loaded. Start the
165 * remote target if we have one or attach to the core.
170 asprintf(&buf, "target remote %s", remote);
171 else if (vmcore != NULL)
172 asprintf(&buf, "target kernel %s", vmcore);
175 execute_command(buf, 0);
182 * Parse an expression and return its value. If 'quiet' is true, then
183 * any error messages from the parser are masked.
186 kgdb_parse_1(const char *exp, int quiet)
188 struct ui_file *old_stderr;
189 struct cleanup *old_chain;
190 struct expression *expr;
195 old_stderr = gdb_stderr;
197 gdb_stderr = parse_gdberr;
200 old_chain = make_cleanup(xfree, s);
201 if (gdb_parse_exp_1(&s, NULL, 0, &expr) && *s == '\0') {
202 make_cleanup(free_current_contents, &expr);
203 if (gdb_evaluate_expression(expr, &val))
204 n = value_as_address(val);
206 do_cleanups(old_chain);
207 gdb_stderr = old_stderr;
211 #define MSGBUF_SEQ_TO_POS(size, seq) ((seq) % (size))
217 int size, rseq, wseq;
221 * Display the unread portion of the message buffer. This gives the
222 * user a some initial data to work from.
226 bufp = kgdb_parse("msgbufp->msg_ptr");
227 size = (int)kgdb_parse("msgbufp->msg_size");
228 rseq = (int)kgdb_parse("msgbufp->msg_bufr");
229 wseq = (int)kgdb_parse("msgbufp->msg_bufx");
230 rseq = MSGBUF_SEQ_TO_POS(size, rseq);
231 wseq = MSGBUF_SEQ_TO_POS(size, wseq);
232 if (bufp == 0 || size == 0 || rseq == wseq)
235 printf("\nUnread portion of the kernel message buffer:\n");
236 while (rseq < wseq) {
237 read_memory(bufp + rseq, &c, 1);
249 kgdb_init(char *argv0 __unused)
252 parse_gdberr = mem_fileopen();
253 set_prompt("(kgdb) ");
254 initialize_kgdb_target();
255 initialize_kld_target();
256 observer_attach_new_objfile(kgdb_new_objfile);
260 * Remote targets can support any number of syntaxes and we want to
261 * support them all with one addition: we support specifying a device
262 * node for a serial device without the "/dev/" prefix.
264 * What we do is to stat(2) the existing remote target first. If that
265 * fails, we try it with "/dev/" prepended. If that succeeds we use
266 * the resulting path, otherwise we use the original target. If
267 * either stat(2) succeeds make sure the file is either a character
276 if (stat(remote, &st) != 0) {
277 snprintf(path, sizeof(path), "/dev/%s", remote);
278 if (stat(path, &st) != 0)
281 remote = strdup(path);
283 if (!S_ISCHR(st.st_mode) && !S_ISFIFO(st.st_mode))
284 errx(1, "%s: not a special file, FIFO or socket", remote);
288 add_arg(struct captured_main_args *args, char *arg)
292 args->argv = reallocf(args->argv, (args->argc + 1) * sizeof(char *));
293 if (args->argv == NULL)
294 err(1, "Out of memory building argument list");
295 args->argv[args->argc] = arg;
299 main(int argc, char *argv[])
303 struct captured_main_args args;
309 strlcpy(crashdir, "/var/crash", sizeof(crashdir));
310 s = getenv("KGDB_CRASH_DIR");
312 strlcpy(crashdir, s, sizeof(crashdir));
314 /* Convert long options into short options. */
315 for (a = 1; a < argc; a++) {
319 /* Long options take either 1 or 2 dashes. */
322 if (strcmp(s, "quiet") == 0)
324 else if (strcmp(s, "fullname") == 0)
330 memset (&args, 0, sizeof args);
331 args.use_windows = 0;
332 args.interpreter_p = INTERP_CONSOLE;
333 args.argv = malloc(sizeof(char *));
334 args.argv[0] = argv[0];
336 while ((ch = getopt(argc, argv, "ac:d:fn:qr:vw")) != -1) {
341 case 'c': /* use given core file. */
342 if (vmcore != NULL) {
343 warnx("option %c: can only be specified once",
348 vmcore = strdup(optarg);
350 case 'd': /* lookup dumps in given directory. */
351 strlcpy(crashdir, optarg, sizeof(crashdir));
354 annotation_level = 1;
356 case 'n': /* use dump with given number. */
357 dumpnr = strtol(optarg, &s, 0);
358 if (dumpnr < 0 || *s != '\0') {
359 warnx("option %c: invalid kernel dump number",
367 add_arg(&args, "-q");
369 case 'r': /* use given device for remote session. */
370 if (remote != NULL) {
371 warnx("option %c: can only be specified once",
376 remote = strdup(optarg);
378 case 'v': /* increase verbosity. */
381 case 'w': /* core file is writeable. */
382 add_arg(&args, "--write");
390 if (((vmcore != NULL) ? 1 : 0) + ((dumpnr >= 0) ? 1 : 0) +
391 ((remote != NULL) ? 1 : 0) > 1) {
392 warnx("options -c, -n and -r are mutually exclusive");
398 warnx("using %s as the crash directory", crashdir);
401 kernel = strdup(argv[optind++]);
403 if (argc > optind && (dumpnr >= 0 || remote != NULL)) {
404 warnx("options -n and -r do not take a core file. Ignored");
409 snprintf(path, sizeof(path), "%s/vmcore.%d", crashdir, dumpnr);
410 if (stat(path, &st) == -1)
412 if (!S_ISREG(st.st_mode))
413 errx(1, "%s: not a regular file", path);
414 vmcore = strdup(path);
415 } else if (remote != NULL) {
417 } else if (argc > optind) {
419 vmcore = strdup(argv[optind++]);
421 warnx("multiple core files specified. Ignored");
422 } else if (vmcore == NULL && kernel == NULL) {
423 vmcore = strdup(_PATH_MEM);
424 kernel = strdup(getbootfile());
429 warnx("core file: %s", vmcore);
431 warnx("device file: %s", remote);
433 warnx("kernel image: %s", kernel);
436 /* A remote target requires an explicit kernel argument. */
437 if (remote != NULL && kernel == NULL) {
438 warnx("remote debugging requires a kernel");
443 /* If we don't have a kernel image yet, try to find one. */
444 if (kernel == NULL) {
446 kernel_from_dumpnr(dumpnr);
449 errx(1, "couldn't find a suitable kernel image");
451 warnx("kernel image: %s", kernel);
453 add_arg(&args, kernel);
457 add_arg(&args, vmcore);
460 /* The libgdb code uses optind too. Reset it... */
463 /* Terminate argv list. */
464 add_arg(&args, NULL);
466 deprecated_init_ui_hook = kgdb_init;
468 return (gdb_main(&args));