Index: stable/6/gnu/usr.bin/gdb/kgdb/kld.c =================================================================== --- stable/6/gnu/usr.bin/gdb/kgdb/kld.c (revision 177888) +++ stable/6/gnu/usr.bin/gdb/kgdb/kld.c (revision 177889) @@ -1,479 +1,480 @@ /* * Copyright (c) 2004 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "kgdb.h" struct lm_info { CORE_ADDR base_address; }; /* Offsets of fields in linker_file structure. */ static CORE_ADDR off_address, off_filename, off_pathname, off_next; /* KVA of 'linker_path' which corresponds to the kern.module_path sysctl .*/ static CORE_ADDR module_path_addr; static struct target_so_ops kld_so_ops; static int kld_ok (char *path) { struct stat sb; if (stat(path, &sb) == 0 && S_ISREG(sb.st_mode)) return (1); return (0); } /* * Look for a matching file checking for debug suffixes before the raw file: * - filename + ".symbols" (e.g. foo.ko.symbols) * - filename + ".debug" (e.g. foo.ko.debug) * - filename (e.g. foo.ko) */ static const char *kld_suffixes[] = { ".debug", "", NULL }; static int check_kld_path (char *path, size_t path_size) { const char **suffix; char *ep; ep = path + strlen(path); suffix = kld_suffixes; while (*suffix != NULL) { if (strlcat(path, *suffix, path_size) < path_size) { if (kld_ok(path)) return (1); } /* Restore original path to remove suffix. */ *ep = '\0'; suffix++; } return (0); } /* * Try to find the path for a kld by looking in the kernel's directory and * in the various paths in the module path. */ static int find_kld_path (char *filename, char *path, size_t path_size) { char *module_path; char *kernel_dir, *module_dir, *cp; int error; kernel_dir = dirname(kernel); if (kernel_dir != NULL) { snprintf(path, path_size, "%s/%s", kernel_dir, filename); if (check_kld_path(path, path_size)) return (1); } if (module_path_addr != 0) { target_read_string(module_path_addr, &module_path, PATH_MAX, &error); if (error == 0) { make_cleanup(xfree, module_path); cp = module_path; while ((module_dir = strsep(&cp, ";")) != NULL) { snprintf(path, path_size, "%s/%s", module_dir, filename); if (check_kld_path(path, path_size)) return (1); } } } return (0); } /* * Read a kernel pointer given a KVA in 'address'. */ static CORE_ADDR read_pointer (CORE_ADDR address) { CORE_ADDR value; if (target_read_memory(address, (char *)&value, TARGET_PTR_BIT / 8) != 0) return (0); return (extract_unsigned_integer(&value, TARGET_PTR_BIT / 8)); } /* * Try to find this kld in the kernel linker's list of linker files. */ static int find_kld_address (char *arg, CORE_ADDR *address) { CORE_ADDR kld; char *kld_filename; char *filename; int error; if (off_address == 0 || off_filename == 0 || off_next == 0) return (0); filename = basename(arg); for (kld = kgdb_parse("linker_files.tqh_first"); kld != 0; kld = read_pointer(kld + off_next)) { /* Try to read this linker file's filename. */ target_read_string(read_pointer(kld + off_filename), &kld_filename, PATH_MAX, &error); if (error) continue; /* Compare this kld's filename against our passed in name. */ if (strcmp(kld_filename, filename) != 0) { xfree(kld_filename); continue; } xfree(kld_filename); /* * We found a match, use its address as the base * address if we can read it. */ *address = read_pointer(kld + off_address); if (*address == 0) return (0); return (1); } return (0); } struct add_section_info { struct section_addr_info *section_addrs; int sect_index; CORE_ADDR base_addr; int add_kld_command; }; static void add_section (bfd *bfd, asection *sect, void *arg) { struct add_section_info *asi = arg; CORE_ADDR address; char *name; /* Ignore non-resident sections. */ if ((bfd_get_section_flags(bfd, sect) & (SEC_ALLOC | SEC_LOAD)) == 0) return; name = xstrdup(bfd_get_section_name(bfd, sect)); make_cleanup(xfree, name); address = asi->base_addr + bfd_get_section_vma(bfd, sect); asi->section_addrs->other[asi->sect_index].name = name; asi->section_addrs->other[asi->sect_index].addr = address; asi->section_addrs->other[asi->sect_index].sectindex = sect->index; if (asi->add_kld_command) printf_unfiltered("\t%s_addr = %s\n", name, local_hex_string(address)); asi->sect_index++; } static void load_kld (char *path, CORE_ADDR base_addr, int from_tty, int add_kld_command) { struct add_section_info asi; struct cleanup *cleanup; bfd *bfd; /* Open the kld. */ bfd = bfd_openr(path, gnutarget); if (bfd == NULL) error("\"%s\": can't open: %s", path, bfd_errmsg(bfd_get_error())); cleanup = make_cleanup_bfd_close(bfd); if (!bfd_check_format(bfd, bfd_object)) error("\%s\": not an object file", path); /* Make sure we have a .text section. */ if (bfd_get_section_by_name (bfd, ".text") == NULL) error("\"%s\": can't find text section", path); if (add_kld_command) printf_unfiltered("add symbol table from file \"%s\" at\n", path); /* Build a section table for symbol_file_add() from the bfd sections. */ asi.section_addrs = alloc_section_addr_info(bfd_count_sections(bfd)); cleanup = make_cleanup(xfree, asi.section_addrs); asi.sect_index = 0; asi.base_addr = base_addr; asi.add_kld_command = add_kld_command; bfd_map_over_sections(bfd, add_section, &asi); if (from_tty && (!query("%s", ""))) error("Not confirmed."); symbol_file_add(path, from_tty, asi.section_addrs, 0, add_kld_command ? OBJF_USERLOADED : 0); do_cleanups(cleanup); } void kgdb_add_kld_cmd (char *arg, int from_tty) { char path[PATH_MAX]; CORE_ADDR base_addr; /* Try to open the raw path to handle absolute paths first. */ snprintf(path, sizeof(path), "%s", arg); if (!check_kld_path(path, sizeof(path))) { /* * If that didn't work, look in the various possible * paths for the module. */ if (!find_kld_path(arg, path, sizeof(path))) { error("Unable to locate kld"); return; } } if (!find_kld_address(arg, &base_addr)) { error("Unable to find kld in kernel"); return; } load_kld(path, base_addr, from_tty, 1); reinit_frame_cache(); } static void kld_relocate_section_addresses (struct so_list *so, struct section_table *sec) { sec->addr += so->lm_info->base_address; sec->endaddr += so->lm_info->base_address; } static void kld_free_so (struct so_list *so) { xfree(so->lm_info); } static void kld_clear_solib (void) { } static void kld_solib_create_inferior_hook (void) { } static void kld_special_symbol_handling (void) { } static struct so_list * kld_current_sos (void) { struct so_list *head, **prev, *new; CORE_ADDR kld, kernel; char *path; int error; + head = NULL; prev = &head; /* * Walk the list of linker files creating so_list entries for * each non-kernel file. */ kernel = kgdb_parse("linker_kernel_file"); for (kld = kgdb_parse("linker_files.tqh_first"); kld != 0; kld = read_pointer(kld + off_next)) { /* Skip the main kernel file. */ if (kld == kernel) continue; new = xmalloc(sizeof(*new)); memset(new, 0, sizeof(*new)); new->lm_info = xmalloc(sizeof(*new->lm_info)); new->lm_info->base_address = 0; /* Read the base filename and store it in so_original_name. */ target_read_string(read_pointer(kld + off_filename), &path, sizeof(new->so_original_name), &error); if (error != 0) { warning("kld_current_sos: Can't read filename: %s\n", safe_strerror(error)); free_so(new); continue; } strlcpy(new->so_original_name, path, sizeof(new->so_original_name)); xfree(path); /* * Try to read the pathname (if it exists) and store * it in so_name. */ if (off_pathname != 0) { target_read_string(read_pointer(kld + off_pathname), &path, sizeof(new->so_name), &error); if (error != 0) { warning( "kld_current_sos: Can't read pathname for \"%s\": %s\n", new->so_original_name, safe_strerror(error)); strlcpy(new->so_name, new->so_original_name, sizeof(new->so_name)); } else { strlcpy(new->so_name, path, sizeof(new->so_name)); xfree(path); } } else strlcpy(new->so_name, new->so_original_name, sizeof(new->so_name)); /* Read this kld's base address. */ new->lm_info->base_address = read_pointer(kld + off_address); if (new->lm_info->base_address == 0) { warning( "kld_current_sos: Invalid address for kld \"%s\"", new->so_original_name); free_so(new); continue; } /* Append to the list. */ *prev = new; prev = &new->next; } return (head); } static int kld_open_symbol_file_object (void *from_ttyp) { return (0); } static int kld_in_dynsym_resolve_code (CORE_ADDR pc) { return (0); } static int kld_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname) { char path[PATH_MAX]; int fd; *temp_pathname = NULL; if (!find_kld_path(solib, path, sizeof(path))) { errno = ENOENT; return (-1); } fd = open(path, o_flags, 0); if (fd >= 0) *temp_pathname = xstrdup(path); return (fd); } static int load_klds_stub (void *arg) { SOLIB_ADD(NULL, 1, ¤t_target, auto_solib_add); return (0); } void kgdb_kld_init (void) { struct cmd_list_element *c; /* Compute offsets of relevant members in struct linker_file. */ off_address = kgdb_parse("&((struct linker_file *)0)->address"); off_filename = kgdb_parse("&((struct linker_file *)0)->filename"); off_pathname = kgdb_parse("&((struct linker_file *)0)->pathname"); off_next = kgdb_parse("&((struct linker_file *)0)->link.tqe_next"); if (off_address == 0 || off_filename == 0 || off_next == 0) return; module_path_addr = kgdb_parse("linker_path"); kld_so_ops.relocate_section_addresses = kld_relocate_section_addresses; kld_so_ops.free_so = kld_free_so; kld_so_ops.clear_solib = kld_clear_solib; kld_so_ops.solib_create_inferior_hook = kld_solib_create_inferior_hook; kld_so_ops.special_symbol_handling = kld_special_symbol_handling; kld_so_ops.current_sos = kld_current_sos; kld_so_ops.open_symbol_file_object = kld_open_symbol_file_object; kld_so_ops.in_dynsym_resolve_code = kld_in_dynsym_resolve_code; kld_so_ops.find_and_open_solib = kld_find_and_open_solib; current_target_so_ops = &kld_so_ops; catch_errors(load_klds_stub, NULL, NULL, RETURN_MASK_ALL); c = add_com("add-kld", class_files, kgdb_add_kld_cmd, "Usage: add-kld FILE\n\ Load the symbols from the kernel loadable module FILE."); set_cmd_completer(c, filename_completer); } Index: stable/6/gnu/usr.bin/gdb/kgdb/main.c =================================================================== --- stable/6/gnu/usr.bin/gdb/kgdb/main.c (revision 177888) +++ stable/6/gnu/usr.bin/gdb/kgdb/main.c (revision 177889) @@ -1,497 +1,500 @@ /* * Copyright (c) 2004 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* libgdb stuff. */ #include #include #include #include #include #include #include #include #include #include #include +#include extern void (*init_ui_hook)(char *); extern frame_unwind_sniffer_ftype *kgdb_sniffer_kluge; extern void symbol_file_add_main (char *args, int from_tty); #include "kgdb.h" kvm_t *kvm; static char kvm_err[_POSIX2_LINE_MAX]; static int dumpnr; static int quiet; static int verbose; static char crashdir[PATH_MAX]; char *kernel; static char *remote; static char *vmcore; static void (*kgdb_new_objfile_chain)(struct objfile * objfile); static void kgdb_atexit(void) { if (kvm != NULL) kvm_close(kvm); } static void usage(void) { fprintf(stderr, "usage: %s [-afqv] [-d crashdir] [-c core | -n dumpnr | -r device]\n" "\t[kernel [core]]\n", getprogname()); exit(1); } static void kernel_from_dumpnr(int nr) { char path[PATH_MAX]; FILE *info; char *s; struct stat st; int l; /* * If there's a kernel image right here in the crash directory, then * use it. The kernel image is either called kernel. or is in a * subdirectory kernel. and called kernel. The latter allows us * to collect the modules in the same place. */ snprintf(path, sizeof(path), "%s/kernel.%d", crashdir, nr); if (stat(path, &st) == 0) { if (S_ISREG(st.st_mode)) { kernel = strdup(path); return; } if (S_ISDIR(st.st_mode)) { snprintf(path, sizeof(path), "%s/kernel.%d/kernel", crashdir, nr); if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) { kernel = strdup(path); return; } } } /* * No kernel image here. Parse the dump header. The kernel object * directory can be found there and we probably have the kernel * image still in it. The object directory may also have a kernel * with debugging info (called kernel.debug). If we have a debug * kernel, use it. */ snprintf(path, sizeof(path), "%s/info.%d", crashdir, nr); info = fopen(path, "r"); if (info == NULL) { warn(path); return; } while (fgets(path, sizeof(path), info) != NULL) { l = strlen(path); if (l > 0 && path[l - 1] == '\n') path[--l] = '\0'; if (strncmp(path, " ", 4) == 0) { s = strchr(path, ':'); s = (s == NULL) ? path + 4 : s + 1; l = snprintf(path, sizeof(path), "%s/kernel.debug", s); if (stat(path, &st) == -1 || !S_ISREG(st.st_mode)) { path[l - 6] = '\0'; if (stat(path, &st) == -1 || !S_ISREG(st.st_mode)) break; } kernel = strdup(path); break; } } fclose(info); } static void kgdb_new_objfile(struct objfile *objfile) { #if 0 printf("XXX: %s(%p)\n", __func__, objfile); if (objfile != NULL) { goto out; } out: #endif if (kgdb_new_objfile_chain != NULL) kgdb_new_objfile_chain(objfile); } CORE_ADDR kgdb_parse(const char *exp) { struct cleanup *old_chain; struct expression *expr; struct value *val; char *s; CORE_ADDR n; - s = strdup(exp); - old_chain = make_cleanup(free_current_contents, &expr); - expr = parse_expression(s); - val = (expr != NULL) ? evaluate_expression(expr) : NULL; - n = (val != NULL) ? value_as_address(val) : 0; + n = 0; + s = xstrdup(exp); + old_chain = make_cleanup(xfree, s); + if (gdb_parse_exp_1(&s, NULL, 0, &expr) && *s == '\0') { + make_cleanup(free_current_contents, &expr); + if (gdb_evaluate_expression(expr, &val)) + n = value_as_address(val); + } do_cleanups(old_chain); - free(s); return (n); } #define MSGBUF_SEQ_TO_POS(size, seq) ((seq) % (size)) static void kgdb_init_target(void) { CORE_ADDR bufp; int size, rseq, wseq; int kern_desc; char c; kern_desc = open(kernel, O_RDONLY); if (kern_desc == -1) errx(1, "couldn't open a kernel image"); kern_bfd = bfd_fdopenr(kernel, gnutarget, kern_desc); if (kern_bfd == NULL) { close(kern_desc); errx(1, "\"%s\": can't open to probe ABI: %s.", kernel, bfd_errmsg (bfd_get_error ())); } bfd_set_cacheable(kern_bfd, 1); if (!bfd_check_format (kern_bfd, bfd_object)) { bfd_close(kern_bfd); errx(1, "\"%s\": not in executable format: %s", kernel, bfd_errmsg(bfd_get_error())); } set_gdbarch_from_file (kern_bfd); symbol_file_add_main (kernel, 0); if (remote) push_remote_target (remote, 0); else kgdb_target(); /* * Display the unread portion of the message buffer. This gives the * user a some initial data to work from. */ if (quiet) return; bufp = kgdb_parse("msgbufp->msg_ptr"); size = (int)kgdb_parse("msgbufp->msg_size"); rseq = (int)kgdb_parse("msgbufp->msg_rseq"); wseq = (int)kgdb_parse("msgbufp->msg_wseq"); rseq = MSGBUF_SEQ_TO_POS(size, rseq); wseq = MSGBUF_SEQ_TO_POS(size, wseq); if (bufp == 0 || size == 0 || rseq == wseq) return; printf("\nUnread portion of the kernel message buffer:\n"); while (rseq < wseq) { read_memory(bufp + rseq, &c, 1); putchar(c); rseq++; if (rseq == size) rseq = 0; } if (c != '\n') putchar('\n'); putchar('\n'); } static void kgdb_interp_command_loop(void *data) { static int once = 0; if (!once) { once = 1; kgdb_init_target(); print_stack_frame(get_selected_frame(), frame_relative_level(get_selected_frame()), 1); } command_loop(); } static void kgdb_init(char *argv0 __unused) { static struct interp_procs procs = { NULL, NULL, NULL, NULL, NULL, kgdb_interp_command_loop }; struct interp *kgdb; kgdb = interp_new("kgdb", NULL, cli_out_new(gdb_stdout), &procs); interp_add(kgdb); set_prompt("(kgdb) "); kgdb_new_objfile_chain = target_new_objfile_hook; target_new_objfile_hook = kgdb_new_objfile; } int main(int argc, char *argv[]) { char path[PATH_MAX]; struct stat st; struct captured_main_args args; char *s; int a, ch, writecore; dumpnr = -1; strlcpy(crashdir, "/var/crash", sizeof(crashdir)); s = getenv("KGDB_CRASH_DIR"); if (s != NULL) strlcpy(crashdir, s, sizeof(crashdir)); /* Convert long options into short options. */ for (a = 1; a < argc; a++) { s = argv[a]; if (s[0] == '-') { s++; /* Long options take either 1 or 2 dashes. */ if (s[0] == '-') s++; if (strcmp(s, "quiet") == 0) argv[a] = "-q"; else if (strcmp(s, "fullname") == 0) argv[a] = "-f"; } } quiet = 0; writecore = 0; while ((ch = getopt(argc, argv, "ac:d:fn:qr:vw")) != -1) { switch (ch) { case 'a': annotation_level++; break; case 'c': /* use given core file. */ if (vmcore != NULL) { warnx("option %c: can only be specified once", optopt); usage(); /* NOTREACHED */ } vmcore = strdup(optarg); break; case 'd': /* lookup dumps in given directory. */ strlcpy(crashdir, optarg, sizeof(crashdir)); break; case 'f': annotation_level = 1; break; case 'n': /* use dump with given number. */ dumpnr = strtol(optarg, &s, 0); if (dumpnr < 0 || *s != '\0') { warnx("option %c: invalid kernel dump number", optopt); usage(); /* NOTREACHED */ } break; case 'q': quiet = 1; break; case 'r': /* use given device for remote session. */ if (remote != NULL) { warnx("option %c: can only be specified once", optopt); usage(); /* NOTREACHED */ } remote = strdup(optarg); break; case 'v': /* increase verbosity. */ verbose++; break; case 'w': /* core file is writeable. */ writecore = 1; break; case '?': default: usage(); } } if (((vmcore != NULL) ? 1 : 0) + ((dumpnr >= 0) ? 1 : 0) + ((remote != NULL) ? 1 : 0) > 1) { warnx("options -c, -n and -r are mutually exclusive"); usage(); /* NOTREACHED */ } if (verbose > 1) warnx("using %s as the crash directory", crashdir); if (argc > optind) kernel = strdup(argv[optind++]); if (argc > optind && (dumpnr >= 0 || remote != NULL)) { warnx("options -n and -r do not take a core file. Ignored"); optind = argc; } if (dumpnr >= 0) { snprintf(path, sizeof(path), "%s/vmcore.%d", crashdir, dumpnr); if (stat(path, &st) == -1) err(1, path); if (!S_ISREG(st.st_mode)) errx(1, "%s: not a regular file", path); vmcore = strdup(path); } else if (remote != NULL && remote[0] != ':' && remote[0] != '|') { if (stat(remote, &st) != 0) { snprintf(path, sizeof(path), "/dev/%s", remote); if (stat(path, &st) != 0) { err(1, "%s", remote); /* NOTREACHED */ } free(remote); remote = strdup(path); } if (!S_ISCHR(st.st_mode) && !S_ISFIFO(st.st_mode)) { errx(1, "%s: not a special file, FIFO or socket", remote); /* NOTREACHED */ } } else if (argc > optind) { if (vmcore == NULL) vmcore = strdup(argv[optind++]); if (argc > optind) warnx("multiple core files specified. Ignored"); } else if (vmcore == NULL && kernel == NULL) { vmcore = strdup(_PATH_MEM); kernel = strdup(getbootfile()); } if (verbose) { if (vmcore != NULL) warnx("core file: %s", vmcore); if (remote != NULL) warnx("device file: %s", remote); if (kernel != NULL) warnx("kernel image: %s", kernel); } /* * At this point we must either have a core file or have a kernel * with a remote target. */ if (remote != NULL && kernel == NULL) { warnx("remote debugging requires a kernel"); usage(); /* NOTREACHED */ } if (vmcore == NULL && remote == NULL) { warnx("need a core file or a device for remote debugging"); usage(); /* NOTREACHED */ } /* If we don't have a kernel image yet, try to find one. */ if (kernel == NULL) { if (dumpnr >= 0) kernel_from_dumpnr(dumpnr); if (kernel == NULL) errx(1, "couldn't find a suitable kernel image"); if (verbose) warnx("kernel image: %s", kernel); } if (remote == NULL) { kvm = kvm_openfiles(kernel, vmcore, NULL, writecore ? O_RDWR : O_RDONLY, kvm_err); if (kvm == NULL) errx(1, kvm_err); atexit(kgdb_atexit); kgdb_thr_init(); } /* The libgdb code uses optind too. Reset it... */ optind = 0; memset (&args, 0, sizeof args); args.argv = argv; args.argc = 1 + quiet; if (quiet) argv[1] = "-q"; argv[args.argc] = NULL; args.use_windows = 0; args.interpreter_p = "kgdb"; init_ui_hook = kgdb_init; kgdb_sniffer_kluge = kgdb_trgt_trapframe_sniffer; return (gdb_main(&args)); } Index: stable/7/gnu/usr.bin/gdb/kgdb/kld.c =================================================================== --- stable/7/gnu/usr.bin/gdb/kgdb/kld.c (revision 177888) +++ stable/7/gnu/usr.bin/gdb/kgdb/kld.c (revision 177889) @@ -1,479 +1,480 @@ /* * Copyright (c) 2004 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "kgdb.h" struct lm_info { CORE_ADDR base_address; }; /* Offsets of fields in linker_file structure. */ static CORE_ADDR off_address, off_filename, off_pathname, off_next; /* KVA of 'linker_path' which corresponds to the kern.module_path sysctl .*/ static CORE_ADDR module_path_addr; static struct target_so_ops kld_so_ops; static int kld_ok (char *path) { struct stat sb; if (stat(path, &sb) == 0 && S_ISREG(sb.st_mode)) return (1); return (0); } /* * Look for a matching file checking for debug suffixes before the raw file: * - filename + ".symbols" (e.g. foo.ko.symbols) * - filename + ".debug" (e.g. foo.ko.debug) * - filename (e.g. foo.ko) */ static const char *kld_suffixes[] = { ".debug", "", NULL }; static int check_kld_path (char *path, size_t path_size) { const char **suffix; char *ep; ep = path + strlen(path); suffix = kld_suffixes; while (*suffix != NULL) { if (strlcat(path, *suffix, path_size) < path_size) { if (kld_ok(path)) return (1); } /* Restore original path to remove suffix. */ *ep = '\0'; suffix++; } return (0); } /* * Try to find the path for a kld by looking in the kernel's directory and * in the various paths in the module path. */ static int find_kld_path (char *filename, char *path, size_t path_size) { char *module_path; char *kernel_dir, *module_dir, *cp; int error; kernel_dir = dirname(kernel); if (kernel_dir != NULL) { snprintf(path, path_size, "%s/%s", kernel_dir, filename); if (check_kld_path(path, path_size)) return (1); } if (module_path_addr != 0) { target_read_string(module_path_addr, &module_path, PATH_MAX, &error); if (error == 0) { make_cleanup(xfree, module_path); cp = module_path; while ((module_dir = strsep(&cp, ";")) != NULL) { snprintf(path, path_size, "%s/%s", module_dir, filename); if (check_kld_path(path, path_size)) return (1); } } } return (0); } /* * Read a kernel pointer given a KVA in 'address'. */ static CORE_ADDR read_pointer (CORE_ADDR address) { CORE_ADDR value; if (target_read_memory(address, (char *)&value, TARGET_PTR_BIT / 8) != 0) return (0); return (extract_unsigned_integer(&value, TARGET_PTR_BIT / 8)); } /* * Try to find this kld in the kernel linker's list of linker files. */ static int find_kld_address (char *arg, CORE_ADDR *address) { CORE_ADDR kld; char *kld_filename; char *filename; int error; if (off_address == 0 || off_filename == 0 || off_next == 0) return (0); filename = basename(arg); for (kld = kgdb_parse("linker_files.tqh_first"); kld != 0; kld = read_pointer(kld + off_next)) { /* Try to read this linker file's filename. */ target_read_string(read_pointer(kld + off_filename), &kld_filename, PATH_MAX, &error); if (error) continue; /* Compare this kld's filename against our passed in name. */ if (strcmp(kld_filename, filename) != 0) { xfree(kld_filename); continue; } xfree(kld_filename); /* * We found a match, use its address as the base * address if we can read it. */ *address = read_pointer(kld + off_address); if (*address == 0) return (0); return (1); } return (0); } struct add_section_info { struct section_addr_info *section_addrs; int sect_index; CORE_ADDR base_addr; int add_kld_command; }; static void add_section (bfd *bfd, asection *sect, void *arg) { struct add_section_info *asi = arg; CORE_ADDR address; char *name; /* Ignore non-resident sections. */ if ((bfd_get_section_flags(bfd, sect) & (SEC_ALLOC | SEC_LOAD)) == 0) return; name = xstrdup(bfd_get_section_name(bfd, sect)); make_cleanup(xfree, name); address = asi->base_addr + bfd_get_section_vma(bfd, sect); asi->section_addrs->other[asi->sect_index].name = name; asi->section_addrs->other[asi->sect_index].addr = address; asi->section_addrs->other[asi->sect_index].sectindex = sect->index; if (asi->add_kld_command) printf_unfiltered("\t%s_addr = %s\n", name, local_hex_string(address)); asi->sect_index++; } static void load_kld (char *path, CORE_ADDR base_addr, int from_tty, int add_kld_command) { struct add_section_info asi; struct cleanup *cleanup; bfd *bfd; /* Open the kld. */ bfd = bfd_openr(path, gnutarget); if (bfd == NULL) error("\"%s\": can't open: %s", path, bfd_errmsg(bfd_get_error())); cleanup = make_cleanup_bfd_close(bfd); if (!bfd_check_format(bfd, bfd_object)) error("\%s\": not an object file", path); /* Make sure we have a .text section. */ if (bfd_get_section_by_name (bfd, ".text") == NULL) error("\"%s\": can't find text section", path); if (add_kld_command) printf_unfiltered("add symbol table from file \"%s\" at\n", path); /* Build a section table for symbol_file_add() from the bfd sections. */ asi.section_addrs = alloc_section_addr_info(bfd_count_sections(bfd)); cleanup = make_cleanup(xfree, asi.section_addrs); asi.sect_index = 0; asi.base_addr = base_addr; asi.add_kld_command = add_kld_command; bfd_map_over_sections(bfd, add_section, &asi); if (from_tty && (!query("%s", ""))) error("Not confirmed."); symbol_file_add(path, from_tty, asi.section_addrs, 0, add_kld_command ? OBJF_USERLOADED : 0); do_cleanups(cleanup); } void kgdb_add_kld_cmd (char *arg, int from_tty) { char path[PATH_MAX]; CORE_ADDR base_addr; /* Try to open the raw path to handle absolute paths first. */ snprintf(path, sizeof(path), "%s", arg); if (!check_kld_path(path, sizeof(path))) { /* * If that didn't work, look in the various possible * paths for the module. */ if (!find_kld_path(arg, path, sizeof(path))) { error("Unable to locate kld"); return; } } if (!find_kld_address(arg, &base_addr)) { error("Unable to find kld in kernel"); return; } load_kld(path, base_addr, from_tty, 1); reinit_frame_cache(); } static void kld_relocate_section_addresses (struct so_list *so, struct section_table *sec) { sec->addr += so->lm_info->base_address; sec->endaddr += so->lm_info->base_address; } static void kld_free_so (struct so_list *so) { xfree(so->lm_info); } static void kld_clear_solib (void) { } static void kld_solib_create_inferior_hook (void) { } static void kld_special_symbol_handling (void) { } static struct so_list * kld_current_sos (void) { struct so_list *head, **prev, *new; CORE_ADDR kld, kernel; char *path; int error; + head = NULL; prev = &head; /* * Walk the list of linker files creating so_list entries for * each non-kernel file. */ kernel = kgdb_parse("linker_kernel_file"); for (kld = kgdb_parse("linker_files.tqh_first"); kld != 0; kld = read_pointer(kld + off_next)) { /* Skip the main kernel file. */ if (kld == kernel) continue; new = xmalloc(sizeof(*new)); memset(new, 0, sizeof(*new)); new->lm_info = xmalloc(sizeof(*new->lm_info)); new->lm_info->base_address = 0; /* Read the base filename and store it in so_original_name. */ target_read_string(read_pointer(kld + off_filename), &path, sizeof(new->so_original_name), &error); if (error != 0) { warning("kld_current_sos: Can't read filename: %s\n", safe_strerror(error)); free_so(new); continue; } strlcpy(new->so_original_name, path, sizeof(new->so_original_name)); xfree(path); /* * Try to read the pathname (if it exists) and store * it in so_name. */ if (off_pathname != 0) { target_read_string(read_pointer(kld + off_pathname), &path, sizeof(new->so_name), &error); if (error != 0) { warning( "kld_current_sos: Can't read pathname for \"%s\": %s\n", new->so_original_name, safe_strerror(error)); strlcpy(new->so_name, new->so_original_name, sizeof(new->so_name)); } else { strlcpy(new->so_name, path, sizeof(new->so_name)); xfree(path); } } else strlcpy(new->so_name, new->so_original_name, sizeof(new->so_name)); /* Read this kld's base address. */ new->lm_info->base_address = read_pointer(kld + off_address); if (new->lm_info->base_address == 0) { warning( "kld_current_sos: Invalid address for kld \"%s\"", new->so_original_name); free_so(new); continue; } /* Append to the list. */ *prev = new; prev = &new->next; } return (head); } static int kld_open_symbol_file_object (void *from_ttyp) { return (0); } static int kld_in_dynsym_resolve_code (CORE_ADDR pc) { return (0); } static int kld_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname) { char path[PATH_MAX]; int fd; *temp_pathname = NULL; if (!find_kld_path(solib, path, sizeof(path))) { errno = ENOENT; return (-1); } fd = open(path, o_flags, 0); if (fd >= 0) *temp_pathname = xstrdup(path); return (fd); } static int load_klds_stub (void *arg) { SOLIB_ADD(NULL, 1, ¤t_target, auto_solib_add); return (0); } void kgdb_kld_init (void) { struct cmd_list_element *c; /* Compute offsets of relevant members in struct linker_file. */ off_address = kgdb_parse("&((struct linker_file *)0)->address"); off_filename = kgdb_parse("&((struct linker_file *)0)->filename"); off_pathname = kgdb_parse("&((struct linker_file *)0)->pathname"); off_next = kgdb_parse("&((struct linker_file *)0)->link.tqe_next"); if (off_address == 0 || off_filename == 0 || off_next == 0) return; module_path_addr = kgdb_parse("linker_path"); kld_so_ops.relocate_section_addresses = kld_relocate_section_addresses; kld_so_ops.free_so = kld_free_so; kld_so_ops.clear_solib = kld_clear_solib; kld_so_ops.solib_create_inferior_hook = kld_solib_create_inferior_hook; kld_so_ops.special_symbol_handling = kld_special_symbol_handling; kld_so_ops.current_sos = kld_current_sos; kld_so_ops.open_symbol_file_object = kld_open_symbol_file_object; kld_so_ops.in_dynsym_resolve_code = kld_in_dynsym_resolve_code; kld_so_ops.find_and_open_solib = kld_find_and_open_solib; current_target_so_ops = &kld_so_ops; catch_errors(load_klds_stub, NULL, NULL, RETURN_MASK_ALL); c = add_com("add-kld", class_files, kgdb_add_kld_cmd, "Usage: add-kld FILE\n\ Load the symbols from the kernel loadable module FILE."); set_cmd_completer(c, filename_completer); } Index: stable/7/gnu/usr.bin/gdb/kgdb/main.c =================================================================== --- stable/7/gnu/usr.bin/gdb/kgdb/main.c (revision 177888) +++ stable/7/gnu/usr.bin/gdb/kgdb/main.c (revision 177889) @@ -1,497 +1,500 @@ /* * Copyright (c) 2004 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* libgdb stuff. */ #include #include #include #include #include #include #include #include #include #include #include +#include extern void (*init_ui_hook)(char *); extern frame_unwind_sniffer_ftype *kgdb_sniffer_kluge; extern void symbol_file_add_main (char *args, int from_tty); #include "kgdb.h" kvm_t *kvm; static char kvm_err[_POSIX2_LINE_MAX]; static int dumpnr; static int quiet; static int verbose; static char crashdir[PATH_MAX]; char *kernel; static char *remote; static char *vmcore; static void (*kgdb_new_objfile_chain)(struct objfile * objfile); static void kgdb_atexit(void) { if (kvm != NULL) kvm_close(kvm); } static void usage(void) { fprintf(stderr, "usage: %s [-afqv] [-d crashdir] [-c core | -n dumpnr | -r device]\n" "\t[kernel [core]]\n", getprogname()); exit(1); } static void kernel_from_dumpnr(int nr) { char path[PATH_MAX]; FILE *info; char *s; struct stat st; int l; /* * If there's a kernel image right here in the crash directory, then * use it. The kernel image is either called kernel. or is in a * subdirectory kernel. and called kernel. The latter allows us * to collect the modules in the same place. */ snprintf(path, sizeof(path), "%s/kernel.%d", crashdir, nr); if (stat(path, &st) == 0) { if (S_ISREG(st.st_mode)) { kernel = strdup(path); return; } if (S_ISDIR(st.st_mode)) { snprintf(path, sizeof(path), "%s/kernel.%d/kernel", crashdir, nr); if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) { kernel = strdup(path); return; } } } /* * No kernel image here. Parse the dump header. The kernel object * directory can be found there and we probably have the kernel * image still in it. The object directory may also have a kernel * with debugging info (called kernel.debug). If we have a debug * kernel, use it. */ snprintf(path, sizeof(path), "%s/info.%d", crashdir, nr); info = fopen(path, "r"); if (info == NULL) { warn(path); return; } while (fgets(path, sizeof(path), info) != NULL) { l = strlen(path); if (l > 0 && path[l - 1] == '\n') path[--l] = '\0'; if (strncmp(path, " ", 4) == 0) { s = strchr(path, ':'); s = (s == NULL) ? path + 4 : s + 1; l = snprintf(path, sizeof(path), "%s/kernel.debug", s); if (stat(path, &st) == -1 || !S_ISREG(st.st_mode)) { path[l - 6] = '\0'; if (stat(path, &st) == -1 || !S_ISREG(st.st_mode)) break; } kernel = strdup(path); break; } } fclose(info); } static void kgdb_new_objfile(struct objfile *objfile) { #if 0 printf("XXX: %s(%p)\n", __func__, objfile); if (objfile != NULL) { goto out; } out: #endif if (kgdb_new_objfile_chain != NULL) kgdb_new_objfile_chain(objfile); } CORE_ADDR kgdb_parse(const char *exp) { struct cleanup *old_chain; struct expression *expr; struct value *val; char *s; CORE_ADDR n; - s = strdup(exp); - old_chain = make_cleanup(free_current_contents, &expr); - expr = parse_expression(s); - val = (expr != NULL) ? evaluate_expression(expr) : NULL; - n = (val != NULL) ? value_as_address(val) : 0; + n = 0; + s = xstrdup(exp); + old_chain = make_cleanup(xfree, s); + if (gdb_parse_exp_1(&s, NULL, 0, &expr) && *s == '\0') { + make_cleanup(free_current_contents, &expr); + if (gdb_evaluate_expression(expr, &val)) + n = value_as_address(val); + } do_cleanups(old_chain); - free(s); return (n); } #define MSGBUF_SEQ_TO_POS(size, seq) ((seq) % (size)) static void kgdb_init_target(void) { CORE_ADDR bufp; int size, rseq, wseq; int kern_desc; char c; kern_desc = open(kernel, O_RDONLY); if (kern_desc == -1) errx(1, "couldn't open a kernel image"); kern_bfd = bfd_fdopenr(kernel, gnutarget, kern_desc); if (kern_bfd == NULL) { close(kern_desc); errx(1, "\"%s\": can't open to probe ABI: %s.", kernel, bfd_errmsg (bfd_get_error ())); } bfd_set_cacheable(kern_bfd, 1); if (!bfd_check_format (kern_bfd, bfd_object)) { bfd_close(kern_bfd); errx(1, "\"%s\": not in executable format: %s", kernel, bfd_errmsg(bfd_get_error())); } set_gdbarch_from_file (kern_bfd); symbol_file_add_main (kernel, 0); if (remote) push_remote_target (remote, 0); else kgdb_target(); /* * Display the unread portion of the message buffer. This gives the * user a some initial data to work from. */ if (quiet) return; bufp = kgdb_parse("msgbufp->msg_ptr"); size = (int)kgdb_parse("msgbufp->msg_size"); rseq = (int)kgdb_parse("msgbufp->msg_rseq"); wseq = (int)kgdb_parse("msgbufp->msg_wseq"); rseq = MSGBUF_SEQ_TO_POS(size, rseq); wseq = MSGBUF_SEQ_TO_POS(size, wseq); if (bufp == 0 || size == 0 || rseq == wseq) return; printf("\nUnread portion of the kernel message buffer:\n"); while (rseq < wseq) { read_memory(bufp + rseq, &c, 1); putchar(c); rseq++; if (rseq == size) rseq = 0; } if (c != '\n') putchar('\n'); putchar('\n'); } static void kgdb_interp_command_loop(void *data) { static int once = 0; if (!once) { once = 1; kgdb_init_target(); print_stack_frame(get_selected_frame(), frame_relative_level(get_selected_frame()), 1); } command_loop(); } static void kgdb_init(char *argv0 __unused) { static struct interp_procs procs = { NULL, NULL, NULL, NULL, NULL, kgdb_interp_command_loop }; struct interp *kgdb; kgdb = interp_new("kgdb", NULL, cli_out_new(gdb_stdout), &procs); interp_add(kgdb); set_prompt("(kgdb) "); kgdb_new_objfile_chain = target_new_objfile_hook; target_new_objfile_hook = kgdb_new_objfile; } int main(int argc, char *argv[]) { char path[PATH_MAX]; struct stat st; struct captured_main_args args; char *s; int a, ch, writecore; dumpnr = -1; strlcpy(crashdir, "/var/crash", sizeof(crashdir)); s = getenv("KGDB_CRASH_DIR"); if (s != NULL) strlcpy(crashdir, s, sizeof(crashdir)); /* Convert long options into short options. */ for (a = 1; a < argc; a++) { s = argv[a]; if (s[0] == '-') { s++; /* Long options take either 1 or 2 dashes. */ if (s[0] == '-') s++; if (strcmp(s, "quiet") == 0) argv[a] = "-q"; else if (strcmp(s, "fullname") == 0) argv[a] = "-f"; } } quiet = 0; writecore = 0; while ((ch = getopt(argc, argv, "ac:d:fn:qr:vw")) != -1) { switch (ch) { case 'a': annotation_level++; break; case 'c': /* use given core file. */ if (vmcore != NULL) { warnx("option %c: can only be specified once", optopt); usage(); /* NOTREACHED */ } vmcore = strdup(optarg); break; case 'd': /* lookup dumps in given directory. */ strlcpy(crashdir, optarg, sizeof(crashdir)); break; case 'f': annotation_level = 1; break; case 'n': /* use dump with given number. */ dumpnr = strtol(optarg, &s, 0); if (dumpnr < 0 || *s != '\0') { warnx("option %c: invalid kernel dump number", optopt); usage(); /* NOTREACHED */ } break; case 'q': quiet = 1; break; case 'r': /* use given device for remote session. */ if (remote != NULL) { warnx("option %c: can only be specified once", optopt); usage(); /* NOTREACHED */ } remote = strdup(optarg); break; case 'v': /* increase verbosity. */ verbose++; break; case 'w': /* core file is writeable. */ writecore = 1; break; case '?': default: usage(); } } if (((vmcore != NULL) ? 1 : 0) + ((dumpnr >= 0) ? 1 : 0) + ((remote != NULL) ? 1 : 0) > 1) { warnx("options -c, -n and -r are mutually exclusive"); usage(); /* NOTREACHED */ } if (verbose > 1) warnx("using %s as the crash directory", crashdir); if (argc > optind) kernel = strdup(argv[optind++]); if (argc > optind && (dumpnr >= 0 || remote != NULL)) { warnx("options -n and -r do not take a core file. Ignored"); optind = argc; } if (dumpnr >= 0) { snprintf(path, sizeof(path), "%s/vmcore.%d", crashdir, dumpnr); if (stat(path, &st) == -1) err(1, path); if (!S_ISREG(st.st_mode)) errx(1, "%s: not a regular file", path); vmcore = strdup(path); } else if (remote != NULL && remote[0] != ':' && remote[0] != '|') { if (stat(remote, &st) != 0) { snprintf(path, sizeof(path), "/dev/%s", remote); if (stat(path, &st) != 0) { err(1, "%s", remote); /* NOTREACHED */ } free(remote); remote = strdup(path); } if (!S_ISCHR(st.st_mode) && !S_ISFIFO(st.st_mode)) { errx(1, "%s: not a special file, FIFO or socket", remote); /* NOTREACHED */ } } else if (argc > optind) { if (vmcore == NULL) vmcore = strdup(argv[optind++]); if (argc > optind) warnx("multiple core files specified. Ignored"); } else if (vmcore == NULL && kernel == NULL) { vmcore = strdup(_PATH_MEM); kernel = strdup(getbootfile()); } if (verbose) { if (vmcore != NULL) warnx("core file: %s", vmcore); if (remote != NULL) warnx("device file: %s", remote); if (kernel != NULL) warnx("kernel image: %s", kernel); } /* * At this point we must either have a core file or have a kernel * with a remote target. */ if (remote != NULL && kernel == NULL) { warnx("remote debugging requires a kernel"); usage(); /* NOTREACHED */ } if (vmcore == NULL && remote == NULL) { warnx("need a core file or a device for remote debugging"); usage(); /* NOTREACHED */ } /* If we don't have a kernel image yet, try to find one. */ if (kernel == NULL) { if (dumpnr >= 0) kernel_from_dumpnr(dumpnr); if (kernel == NULL) errx(1, "couldn't find a suitable kernel image"); if (verbose) warnx("kernel image: %s", kernel); } if (remote == NULL) { kvm = kvm_openfiles(kernel, vmcore, NULL, writecore ? O_RDWR : O_RDONLY, kvm_err); if (kvm == NULL) errx(1, kvm_err); atexit(kgdb_atexit); kgdb_thr_init(); } /* The libgdb code uses optind too. Reset it... */ optind = 0; memset (&args, 0, sizeof args); args.argv = argv; args.argc = 1 + quiet; if (quiet) argv[1] = "-q"; argv[args.argc] = NULL; args.use_windows = 0; args.interpreter_p = "kgdb"; init_ui_hook = kgdb_init; kgdb_sniffer_kluge = kgdb_trgt_trapframe_sniffer; return (gdb_main(&args)); }