| Commit | Line | Data |
|---|---|---|
| 8b6a428f SS |
1 | /* |
| 2 | * Copyright (c) 2004 Marcel Moolenaar | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: | |
| 8 | * | |
| 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. | |
| 14 | * | |
| 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. | |
| 25 | * | |
| f17ea84b | 26 | * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/main.c,v 1.16 2008/04/29 20:32:45 jhb Exp $ |
| 8b6a428f SS |
27 | */ |
| 28 | ||
| 29 | #include <sys/cdefs.h> | |
| 30 | ||
| 31 | #include <sys/param.h> | |
| 32 | #include <sys/stat.h> | |
| 33 | #include <sys/types.h> | |
| 34 | #include <sys/ioctl.h> | |
| 35 | #include <sys/resource.h> | |
| 36 | #include <sys/select.h> | |
| 37 | #include <sys/time.h> | |
| 38 | #include <sys/wait.h> | |
| 39 | #include <errno.h> | |
| 40 | #include <err.h> | |
| 8b6a428f SS |
41 | #include <inttypes.h> |
| 42 | #include <kvm.h> | |
| 43 | #include <limits.h> | |
| 44 | #include <paths.h> | |
| 45 | #include <stdio.h> | |
| 46 | #include <stdlib.h> | |
| 47 | #include <string.h> | |
| 48 | #include <unistd.h> | |
| 8b6a428f SS |
49 | |
| 50 | /* libgdb stuff. */ | |
| 51 | #include <defs.h> | |
| 52 | #include <frame.h> | |
| 53 | #include <frame-unwind.h> | |
| 54 | #include <inferior.h> | |
| 55 | #include <interps.h> | |
| 56 | #include <cli-out.h> | |
| 57 | #include <main.h> | |
| f17ea84b SS |
58 | #include <gdbcmd.h> |
| 59 | #include <objfiles.h> | |
| 8b6a428f SS |
60 | #include <target.h> |
| 61 | #include <top.h> | |
| f17ea84b | 62 | #include <ui-file.h> |
| 8b6a428f SS |
63 | #include <bfd.h> |
| 64 | #include <gdbcore.h> | |
| f17ea84b SS |
65 | #include <wrapper.h> |
| 66 | #include <observer.h> | |
| 939fa31e | 67 | #include <arch-utils.h> |
| 8b6a428f SS |
68 | |
| 69 | #include "kgdb.h" | |
| 70 | ||
| 8b6a428f | 71 | static int dumpnr; |
| f17ea84b | 72 | static int quiet; |
| 8b6a428f SS |
73 | static int verbose; |
| 74 | ||
| 75 | static char crashdir[PATH_MAX]; | |
| 76 | static char *kernel; | |
| 77 | static char *remote; | |
| 78 | static char *vmcore; | |
| f17ea84b | 79 | static struct ui_file *parse_gdberr; |
| 8b6a428f SS |
80 | |
| 81 | static void | |
| 82 | usage(void) | |
| 83 | { | |
| 84 | ||
| 85 | fprintf(stderr, | |
| 4c0200b9 | 86 | "usage: %s [-afqtv] [-d crashdir] [-c core | -n dumpnr | -r device]\n" |
| 8b6a428f SS |
87 | "\t[kernel [core]]\n", getprogname()); |
| 88 | exit(1); | |
| 89 | } | |
| 90 | ||
| 91 | static void | |
| 92 | kernel_from_dumpnr(int nr) | |
| 93 | { | |
| 94 | char path[PATH_MAX]; | |
| 95 | FILE *info; | |
| 96 | char *s; | |
| 97 | struct stat st; | |
| 98 | int l; | |
| 99 | ||
| 100 | /* | |
| 101 | * If there's a kernel image right here in the crash directory, then | |
| 9a3588c6 SW |
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 | |
| 8b6a428f SS |
104 | * to collect the modules in the same place. |
| 105 | */ | |
| c0a6a6f9 | 106 | snprintf(path, sizeof(path), "%s/kern.%d", crashdir, nr); |
| 8b6a428f SS |
107 | if (stat(path, &st) == 0) { |
| 108 | if (S_ISREG(st.st_mode)) { | |
| 109 | kernel = strdup(path); | |
| 110 | return; | |
| 111 | } | |
| 112 | if (S_ISDIR(st.st_mode)) { | |
| c0a6a6f9 | 113 | snprintf(path, sizeof(path), "%s/kern.%d/kernel", |
| 8b6a428f SS |
114 | crashdir, nr); |
| 115 | if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) { | |
| 116 | kernel = strdup(path); | |
| 117 | return; | |
| 118 | } | |
| 119 | } | |
| 120 | } | |
| 121 | ||
| 122 | /* | |
| 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 | |
| 127 | * kernel, use it. | |
| 128 | */ | |
| 129 | snprintf(path, sizeof(path), "%s/info.%d", crashdir, nr); | |
| 130 | info = fopen(path, "r"); | |
| 131 | if (info == NULL) { | |
| a938cc15 | 132 | warn("%s", path); |
| 8b6a428f SS |
133 | return; |
| 134 | } | |
| 135 | while (fgets(path, sizeof(path), info) != NULL) { | |
| 136 | l = strlen(path); | |
| 137 | if (l > 0 && path[l - 1] == '\n') | |
| 138 | path[--l] = '\0'; | |
| 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)) { | |
| 144 | path[l - 6] = '\0'; | |
| 145 | if (stat(path, &st) == -1 || | |
| 146 | !S_ISREG(st.st_mode)) | |
| 147 | break; | |
| 148 | } | |
| 149 | kernel = strdup(path); | |
| 150 | break; | |
| 151 | } | |
| 152 | } | |
| 153 | fclose(info); | |
| 154 | } | |
| 155 | ||
| 156 | static void | |
| 157 | kgdb_new_objfile(struct objfile *objfile) | |
| 158 | { | |
| f17ea84b SS |
159 | static int once = 1; |
| 160 | ||
| 161 | if (once && objfile != NULL && objfile == symfile_objfile) { | |
| 162 | char *buf; | |
| 8b6a428f | 163 | |
| f17ea84b SS |
164 | /* |
| 165 | * The initial kernel has just been loaded. Start the | |
| 166 | * remote target if we have one or attach to the core. | |
| 167 | */ | |
| 168 | once = 0; | |
| 169 | ||
| 170 | if (remote != NULL) | |
| 171 | asprintf(&buf, "target remote %s", remote); | |
| 172 | else if (vmcore != NULL) | |
| 173 | asprintf(&buf, "target kernel %s", vmcore); | |
| 174 | ||
| 175 | if (buf != NULL) { | |
| 176 | execute_command(buf, 0); | |
| 177 | free(buf); | |
| 178 | } | |
| 179 | } | |
| 8b6a428f SS |
180 | } |
| 181 | ||
| f17ea84b SS |
182 | /* |
| 183 | * Parse an expression and return its value. If 'quiet' is true, then | |
| 184 | * any error messages from the parser are masked. | |
| 185 | */ | |
| 186 | CORE_ADDR | |
| 187 | kgdb_parse_1(const char *exp, int quiet) | |
| 8b6a428f | 188 | { |
| f17ea84b | 189 | struct ui_file *old_stderr; |
| 8b6a428f SS |
190 | struct cleanup *old_chain; |
| 191 | struct expression *expr; | |
| 192 | struct value *val; | |
| 193 | char *s; | |
| 194 | CORE_ADDR n; | |
| 195 | ||
| f17ea84b SS |
196 | old_stderr = gdb_stderr; |
| 197 | if (quiet) | |
| 198 | gdb_stderr = parse_gdberr; | |
| 199 | n = 0; | |
| 200 | s = xstrdup(exp); | |
| 201 | old_chain = make_cleanup(xfree, s); | |
| 202 | if (gdb_parse_exp_1(&s, NULL, 0, &expr) && *s == '\0') { | |
| 203 | make_cleanup(free_current_contents, &expr); | |
| 204 | if (gdb_evaluate_expression(expr, &val)) | |
| 205 | n = value_as_address(val); | |
| 206 | } | |
| 8b6a428f | 207 | do_cleanups(old_chain); |
| f17ea84b | 208 | gdb_stderr = old_stderr; |
| 8b6a428f SS |
209 | return (n); |
| 210 | } | |
| 211 | ||
| 212 | #define MSGBUF_SEQ_TO_POS(size, seq) ((seq) % (size)) | |
| 213 | ||
| f17ea84b SS |
214 | void |
| 215 | kgdb_dmesg(void) | |
| 8b6a428f | 216 | { |
| f17ea84b SS |
217 | CORE_ADDR bufp; |
| 218 | int size, rseq, wseq; | |
| 8b6a428f SS |
219 | char c; |
| 220 | ||
| 221 | /* | |
| 222 | * Display the unread portion of the message buffer. This gives the | |
| 223 | * user a some initial data to work from. | |
| 224 | */ | |
| f17ea84b | 225 | if (quiet) |
| c499d304 | 226 | return; |
| f17ea84b SS |
227 | bufp = kgdb_parse("msgbufp->msg_ptr"); |
| 228 | size = (int)kgdb_parse("msgbufp->msg_size"); | |
| a938cc15 JM |
229 | if (bufp == 0 || size == 0) |
| 230 | return; | |
| f17ea84b SS |
231 | rseq = (int)kgdb_parse("msgbufp->msg_bufr"); |
| 232 | wseq = (int)kgdb_parse("msgbufp->msg_bufx"); | |
| 233 | rseq = MSGBUF_SEQ_TO_POS(size, rseq); | |
| 234 | wseq = MSGBUF_SEQ_TO_POS(size, wseq); | |
| a938cc15 | 235 | if (rseq == wseq) |
| 8b6a428f SS |
236 | return; |
| 237 | ||
| 238 | printf("\nUnread portion of the kernel message buffer:\n"); | |
| 239 | while (rseq < wseq) { | |
| f17ea84b | 240 | read_memory(bufp + rseq, &c, 1); |
| 8b6a428f SS |
241 | putchar(c); |
| 242 | rseq++; | |
| f17ea84b | 243 | if (rseq == size) |
| 8b6a428f SS |
244 | rseq = 0; |
| 245 | } | |
| 246 | if (c != '\n') | |
| 247 | putchar('\n'); | |
| 248 | putchar('\n'); | |
| 249 | } | |
| 250 | ||
| 251 | static void | |
| 252 | kgdb_init(char *argv0 __unused) | |
| 253 | { | |
| 8b6a428f | 254 | |
| f17ea84b | 255 | parse_gdberr = mem_fileopen(); |
| 8b6a428f | 256 | set_prompt("(kgdb) "); |
| f17ea84b SS |
257 | initialize_kgdb_target(); |
| 258 | initialize_kld_target(); | |
| 259 | observer_attach_new_objfile(kgdb_new_objfile); | |
| 260 | } | |
| 261 | ||
| 262 | /* | |
| 263 | * Remote targets can support any number of syntaxes and we want to | |
| 264 | * support them all with one addition: we support specifying a device | |
| 265 | * node for a serial device without the "/dev/" prefix. | |
| 266 | * | |
| 267 | * What we do is to stat(2) the existing remote target first. If that | |
| 268 | * fails, we try it with "/dev/" prepended. If that succeeds we use | |
| 269 | * the resulting path, otherwise we use the original target. If | |
| 270 | * either stat(2) succeeds make sure the file is either a character | |
| 271 | * device or a FIFO. | |
| 272 | */ | |
| 273 | static void | |
| 274 | verify_remote(void) | |
| 275 | { | |
| 276 | char path[PATH_MAX]; | |
| 277 | struct stat st; | |
| 278 | ||
| 279 | if (stat(remote, &st) != 0) { | |
| 280 | snprintf(path, sizeof(path), "/dev/%s", remote); | |
| 281 | if (stat(path, &st) != 0) | |
| 282 | return; | |
| 283 | free(remote); | |
| 284 | remote = strdup(path); | |
| 285 | } | |
| 286 | if (!S_ISCHR(st.st_mode) && !S_ISFIFO(st.st_mode)) | |
| 287 | errx(1, "%s: not a special file, FIFO or socket", remote); | |
| 288 | } | |
| 289 | ||
| 290 | static void | |
| 291 | add_arg(struct captured_main_args *args, char *arg) | |
| 292 | { | |
| 293 | ||
| 294 | args->argc++; | |
| 295 | args->argv = reallocf(args->argv, (args->argc + 1) * sizeof(char *)); | |
| 296 | if (args->argv == NULL) | |
| 297 | err(1, "Out of memory building argument list"); | |
| 298 | args->argv[args->argc] = arg; | |
| 8b6a428f SS |
299 | } |
| 300 | ||
| 301 | int | |
| 302 | main(int argc, char *argv[]) | |
| 303 | { | |
| 304 | char path[PATH_MAX]; | |
| 305 | struct stat st; | |
| 306 | struct captured_main_args args; | |
| 307 | char *s; | |
| f17ea84b | 308 | int a, ch; |
| 8b6a428f SS |
309 | |
| 310 | dumpnr = -1; | |
| 311 | ||
| 312 | strlcpy(crashdir, "/var/crash", sizeof(crashdir)); | |
| 313 | s = getenv("KGDB_CRASH_DIR"); | |
| 314 | if (s != NULL) | |
| 315 | strlcpy(crashdir, s, sizeof(crashdir)); | |
| 316 | ||
| 317 | /* Convert long options into short options. */ | |
| 318 | for (a = 1; a < argc; a++) { | |
| 319 | s = argv[a]; | |
| 320 | if (s[0] == '-') { | |
| 321 | s++; | |
| 322 | /* Long options take either 1 or 2 dashes. */ | |
| 323 | if (s[0] == '-') | |
| 324 | s++; | |
| 325 | if (strcmp(s, "quiet") == 0) | |
| 326 | argv[a] = "-q"; | |
| 327 | else if (strcmp(s, "fullname") == 0) | |
| 328 | argv[a] = "-f"; | |
| 4c0200b9 JM |
329 | else if (strcmp(s, "tui-mode") == 0) |
| 330 | argv[a] = "-t"; | |
| 8b6a428f SS |
331 | } |
| 332 | } | |
| 333 | ||
| 334 | quiet = 0; | |
| f17ea84b SS |
335 | memset (&args, 0, sizeof args); |
| 336 | args.use_windows = 0; | |
| 337 | args.interpreter_p = INTERP_CONSOLE; | |
| 338 | args.argv = malloc(sizeof(char *)); | |
| 339 | args.argv[0] = argv[0]; | |
| a938cc15 | 340 | add_arg(&args, "--kernel"); |
| 8b6a428f | 341 | |
| 4c0200b9 | 342 | while ((ch = getopt(argc, argv, "ac:d:fn:qr:tvw")) != -1) { |
| 8b6a428f SS |
343 | switch (ch) { |
| 344 | case 'a': | |
| 345 | annotation_level++; | |
| 346 | break; | |
| 347 | case 'c': /* use given core file. */ | |
| 348 | if (vmcore != NULL) { | |
| 349 | warnx("option %c: can only be specified once", | |
| 350 | optopt); | |
| 351 | usage(); | |
| 352 | /* NOTREACHED */ | |
| 353 | } | |
| 354 | vmcore = strdup(optarg); | |
| 355 | break; | |
| 356 | case 'd': /* lookup dumps in given directory. */ | |
| 357 | strlcpy(crashdir, optarg, sizeof(crashdir)); | |
| 358 | break; | |
| 359 | case 'f': | |
| 360 | annotation_level = 1; | |
| 361 | break; | |
| 362 | case 'n': /* use dump with given number. */ | |
| 363 | dumpnr = strtol(optarg, &s, 0); | |
| 364 | if (dumpnr < 0 || *s != '\0') { | |
| 365 | warnx("option %c: invalid kernel dump number", | |
| 366 | optopt); | |
| 367 | usage(); | |
| 368 | /* NOTREACHED */ | |
| 369 | } | |
| 370 | break; | |
| 371 | case 'q': | |
| 372 | quiet = 1; | |
| f17ea84b | 373 | add_arg(&args, "-q"); |
| 8b6a428f SS |
374 | break; |
| 375 | case 'r': /* use given device for remote session. */ | |
| 376 | if (remote != NULL) { | |
| 377 | warnx("option %c: can only be specified once", | |
| 378 | optopt); | |
| 379 | usage(); | |
| 380 | /* NOTREACHED */ | |
| 381 | } | |
| 382 | remote = strdup(optarg); | |
| 383 | break; | |
| 4c0200b9 JM |
384 | case 't': |
| 385 | args.interpreter_p = INTERP_TUI; | |
| 386 | add_arg(&args, "-tui"); | |
| 387 | quiet = 1; | |
| 388 | add_arg(&args, "-q"); | |
| 389 | break; | |
| 8b6a428f SS |
390 | case 'v': /* increase verbosity. */ |
| 391 | verbose++; | |
| 392 | break; | |
| 393 | case 'w': /* core file is writeable. */ | |
| f17ea84b | 394 | add_arg(&args, "--write"); |
| 8b6a428f SS |
395 | break; |
| 396 | case '?': | |
| 397 | default: | |
| 398 | usage(); | |
| 399 | } | |
| 400 | } | |
| 401 | ||
| 402 | if (((vmcore != NULL) ? 1 : 0) + ((dumpnr >= 0) ? 1 : 0) + | |
| 403 | ((remote != NULL) ? 1 : 0) > 1) { | |
| 404 | warnx("options -c, -n and -r are mutually exclusive"); | |
| 405 | usage(); | |
| 406 | /* NOTREACHED */ | |
| 407 | } | |
| 408 | ||
| 409 | if (verbose > 1) | |
| 410 | warnx("using %s as the crash directory", crashdir); | |
| 411 | ||
| 412 | if (argc > optind) | |
| 413 | kernel = strdup(argv[optind++]); | |
| 414 | ||
| 415 | if (argc > optind && (dumpnr >= 0 || remote != NULL)) { | |
| 416 | warnx("options -n and -r do not take a core file. Ignored"); | |
| 417 | optind = argc; | |
| 418 | } | |
| 419 | ||
| 420 | if (dumpnr >= 0) { | |
| 421 | snprintf(path, sizeof(path), "%s/vmcore.%d", crashdir, dumpnr); | |
| 422 | if (stat(path, &st) == -1) | |
| a938cc15 | 423 | err(1, "%s", path); |
| 8b6a428f SS |
424 | if (!S_ISREG(st.st_mode)) |
| 425 | errx(1, "%s: not a regular file", path); | |
| 426 | vmcore = strdup(path); | |
| f17ea84b SS |
427 | } else if (remote != NULL) { |
| 428 | verify_remote(); | |
| 8b6a428f SS |
429 | } else if (argc > optind) { |
| 430 | if (vmcore == NULL) | |
| 431 | vmcore = strdup(argv[optind++]); | |
| 432 | if (argc > optind) | |
| 433 | warnx("multiple core files specified. Ignored"); | |
| 434 | } else if (vmcore == NULL && kernel == NULL) { | |
| 435 | vmcore = strdup(_PATH_MEM); | |
| 436 | kernel = strdup(getbootfile()); | |
| 437 | } | |
| 438 | ||
| 439 | if (verbose) { | |
| 440 | if (vmcore != NULL) | |
| 441 | warnx("core file: %s", vmcore); | |
| 442 | if (remote != NULL) | |
| 443 | warnx("device file: %s", remote); | |
| 444 | if (kernel != NULL) | |
| 445 | warnx("kernel image: %s", kernel); | |
| 446 | } | |
| 447 | ||
| f17ea84b | 448 | /* A remote target requires an explicit kernel argument. */ |
| 8b6a428f SS |
449 | if (remote != NULL && kernel == NULL) { |
| 450 | warnx("remote debugging requires a kernel"); | |
| 451 | usage(); | |
| 452 | /* NOTREACHED */ | |
| 453 | } | |
| 8b6a428f SS |
454 | |
| 455 | /* If we don't have a kernel image yet, try to find one. */ | |
| 456 | if (kernel == NULL) { | |
| 457 | if (dumpnr >= 0) | |
| 458 | kernel_from_dumpnr(dumpnr); | |
| 459 | ||
| 460 | if (kernel == NULL) | |
| 461 | errx(1, "couldn't find a suitable kernel image"); | |
| 462 | if (verbose) | |
| 463 | warnx("kernel image: %s", kernel); | |
| 464 | } | |
| f17ea84b | 465 | add_arg(&args, kernel); |
| 8b6a428f | 466 | |
| f17ea84b SS |
467 | /* |
| 468 | if (vmcore != NULL) | |
| 469 | add_arg(&args, vmcore); | |
| 470 | */ | |
| 8b6a428f SS |
471 | |
| 472 | /* The libgdb code uses optind too. Reset it... */ | |
| 473 | optind = 0; | |
| 474 | ||
| f17ea84b SS |
475 | /* Terminate argv list. */ |
| 476 | add_arg(&args, NULL); | |
| 8b6a428f SS |
477 | |
| 478 | deprecated_init_ui_hook = kgdb_init; | |
| 479 | ||
| 480 | return (gdb_main(&args)); | |
| 481 | } |