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/kld.c,v 1.11 2008/10/02 20:42:10 jhb Exp $
29 #include <sys/param.h>
37 #include <completer.h>
40 #include <frame-unwind.h>
46 #include <arch-utils.h>
48 #include <exceptions.h>
54 CORE_ADDR base_address;
57 /* Offsets of fields in linker_file structure. */
58 static CORE_ADDR off_address, off_filename, off_pathname, off_next;
60 /* KVA of 'linker_path' which corresponds to the kern.module_path sysctl .*/
61 static CORE_ADDR module_path_addr;
62 static CORE_ADDR linker_files_addr;
63 static CORE_ADDR kernel_file_addr;
65 static struct target_so_ops kld_so_ops;
72 if (stat(path, &sb) == 0 && S_ISREG(sb.st_mode))
78 * Look for a matching file checking for debug suffixes before the raw file:
79 * - filename + ".debug" (e.g. foo.ko.debug)
80 * - filename (e.g. foo.ko)
82 static const char *kld_suffixes[] = {
89 check_kld_path (char *path, size_t path_size)
94 ep = path + strlen(path);
95 suffix = kld_suffixes;
96 while (*suffix != NULL) {
97 if (strlcat(path, *suffix, path_size) < path_size) {
102 /* Restore original path to remove suffix. */
110 * Try to find the path for a kld by looking in the kernel's directory and
111 * in the various paths in the module path.
114 find_kld_path (char *filename, char *path, size_t path_size)
117 char *kernel_dir, *module_dir, *cp;
121 kernel_dir = dirname(bfd_get_filename(exec_bfd));
122 if (kernel_dir != NULL) {
123 snprintf(path, path_size, "%s/%s", kernel_dir,
125 if (check_kld_path(path, path_size))
129 if (module_path_addr != 0) {
130 target_read_string(module_path_addr, &module_path, PATH_MAX,
133 make_cleanup(xfree, module_path);
135 while ((module_dir = strsep(&cp, ";")) != NULL) {
136 snprintf(path, path_size, "%s/%s", module_dir,
138 if (check_kld_path(path, path_size))
147 * Read a kernel pointer given a KVA in 'address'.
150 read_pointer (CORE_ADDR address)
152 struct gdbarch *arch = get_current_arch();
153 enum bfd_endian byte_order = gdbarch_byte_order(arch);
154 gdb_byte buf[sizeof(ULONGEST)];
157 ptrsz = gdbarch_ptr_bit(arch) / 8;
159 if (target_read_memory(address, buf, ptrsz) != 0)
161 return (extract_unsigned_integer(buf, ptrsz, byte_order));
165 * Try to find this kld in the kernel linker's list of linker files.
168 find_kld_address (char *arg, CORE_ADDR *address)
175 if (linker_files_addr == 0 || off_address == 0 || off_filename == 0 ||
179 filename = basename(arg);
180 for (kld = read_pointer(linker_files_addr); kld != 0;
181 kld = read_pointer(kld + off_next)) {
182 /* Try to read this linker file's filename. */
183 target_read_string(read_pointer(kld + off_filename),
184 &kld_filename, PATH_MAX, &error);
188 /* Compare this kld's filename against our passed in name. */
189 if (strcmp(kld_filename, filename) != 0) {
196 * We found a match, use its address as the base
197 * address if we can read it.
199 *address = read_pointer(kld + off_address);
208 load_kld (char *path, CORE_ADDR base_addr, int from_tty)
210 struct section_addr_info *sap;
211 struct target_section *sections = NULL, *sections_end = NULL, *s;
212 struct cleanup *cleanup;
217 bfd = bfd_openr(path, gnutarget);
219 error("\"%s\": can't open: %s", path,
220 bfd_errmsg(bfd_get_error()));
221 cleanup = make_cleanup_bfd_close(bfd);
223 if (!bfd_check_format(bfd, bfd_object))
224 error("\%s\": not an object file", path);
226 /* Make sure we have a .text section. */
227 if (bfd_get_section_by_name (bfd, ".text") == NULL)
228 error("\"%s\": can't find text section", path);
230 /* Build a section table from the bfd and relocate the sections. */
231 if (build_section_table (bfd, §ions, §ions_end))
232 error("\"%s\": can't find file sections", path);
233 cleanup = make_cleanup(xfree, sections);
234 for (s = sections; s < sections_end; s++) {
235 s->addr += base_addr;
236 s->endaddr += base_addr;
239 /* Build a section addr info to pass to symbol_file_add(). */
240 sap = build_section_addr_info_from_section_table (sections,
242 cleanup = make_cleanup((make_cleanup_ftype *)free_section_addr_info,
245 printf_unfiltered("add symbol table from file \"%s\" at\n", path);
246 for (i = 0; i < sap->num_sections; i++)
247 printf_unfiltered("\t%s_addr = %s\n", sap->other[i].name,
248 hex_string(sap->other[i].addr));
250 if (from_tty && (!query("%s", "")))
251 error("Not confirmed.");
253 symbol_file_add(path, from_tty ? SYMFILE_VERBOSE : 0, sap, OBJF_USERLOADED);
255 do_cleanups(cleanup);
259 kgdb_add_kld_cmd (char *arg, int from_tty)
265 error("No kernel symbol file");
267 /* Try to open the raw path to handle absolute paths first. */
268 snprintf(path, sizeof(path), "%s", arg);
269 if (!check_kld_path(path, sizeof(path))) {
272 * If that didn't work, look in the various possible
273 * paths for the module.
275 if (!find_kld_path(arg, path, sizeof(path))) {
276 error("Unable to locate kld");
281 if (!find_kld_address(arg, &base_addr)) {
282 error("Unable to find kld in kernel");
286 load_kld(path, base_addr, from_tty);
288 reinit_frame_cache();
292 kld_relocate_section_addresses (struct so_list *so, struct target_section *sec)
295 sec->addr += so->lm_info->base_address;
296 sec->endaddr += so->lm_info->base_address;
300 kld_free_so (struct so_list *so)
307 kld_clear_solib (void)
312 kld_solib_create_inferior_hook (void)
317 kld_special_symbol_handling (void)
321 static struct so_list *
322 kld_current_sos (void)
324 struct so_list *head, **prev, *new;
325 CORE_ADDR kld, kernel;
329 if (linker_files_addr == 0 || kernel_file_addr == 0 ||
330 off_address == 0 || off_filename == 0 || off_next == 0)
337 * Walk the list of linker files creating so_list entries for
338 * each non-kernel file.
340 kernel = read_pointer(kernel_file_addr);
341 for (kld = read_pointer(linker_files_addr); kld != 0;
342 kld = read_pointer(kld + off_next)) {
343 /* Skip the main kernel file. */
347 new = xmalloc(sizeof(*new));
348 memset(new, 0, sizeof(*new));
350 new->lm_info = xmalloc(sizeof(*new->lm_info));
351 new->lm_info->base_address = 0;
353 /* Read the base filename and store it in so_original_name. */
354 target_read_string(read_pointer(kld + off_filename),
355 &path, sizeof(new->so_original_name), &error);
357 warning("kld_current_sos: Can't read filename: %s\n",
358 safe_strerror(error));
362 strlcpy(new->so_original_name, path,
363 sizeof(new->so_original_name));
367 * Try to read the pathname (if it exists) and store
370 if (off_pathname != 0) {
371 target_read_string(read_pointer(kld + off_pathname),
372 &path, sizeof(new->so_name), &error);
375 "kld_current_sos: Can't read pathname for \"%s\": %s\n",
376 new->so_original_name,
377 safe_strerror(error));
378 strlcpy(new->so_name, new->so_original_name,
379 sizeof(new->so_name));
381 strlcpy(new->so_name, path,
382 sizeof(new->so_name));
386 strlcpy(new->so_name, new->so_original_name,
387 sizeof(new->so_name));
389 /* Read this kld's base address. */
390 new->lm_info->base_address = read_pointer(kld + off_address);
391 if (new->lm_info->base_address == 0) {
393 "kld_current_sos: Invalid address for kld \"%s\"",
394 new->so_original_name);
399 /* Append to the list. */
408 kld_open_symbol_file_object (void *from_ttyp)
415 kld_in_dynsym_resolve_code (CORE_ADDR pc)
422 kld_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname)
427 *temp_pathname = NULL;
428 if (!find_kld_path(solib, path, sizeof(path))) {
432 fd = open(path, o_flags, 0);
434 *temp_pathname = xstrdup(path);
439 kld_new_objfile (struct objfile *objfile)
442 if (!have_partial_symbols())
446 * Compute offsets of relevant members in struct linker_file
447 * and the addresses of global variables. Don't warn about
448 * kernels that don't have 'pathname' in the linker_file
449 * struct since 6.x kernels don't have it.
451 off_address = kgdb_parse("&((struct linker_file *)0)->address");
452 off_filename = kgdb_parse("&((struct linker_file *)0)->filename");
453 off_pathname = kgdb_parse_quiet("&((struct linker_file *)0)->pathname");
454 off_next = kgdb_parse("&((struct linker_file *)0)->link.tqe_next");
455 module_path_addr = kgdb_parse("linker_path");
456 linker_files_addr = kgdb_parse("&linker_files.tqh_first");
457 kernel_file_addr = kgdb_parse("&linker_kernel_file");
461 load_klds_stub (void *arg)
464 solib_add(NULL, 1, ¤t_target, auto_solib_add);
471 /* XXX hack, needs to go into an abi init function */
472 set_solib_ops(get_current_arch(), &kld_so_ops);
474 kld_new_objfile(NULL);
475 catch_errors(load_klds_stub, NULL, NULL, RETURN_MASK_ALL);
476 observer_attach_new_objfile(kld_new_objfile);
480 initialize_kld_target(void)
482 struct cmd_list_element *c;
484 kld_so_ops.relocate_section_addresses = kld_relocate_section_addresses;
485 kld_so_ops.free_so = kld_free_so;
486 kld_so_ops.clear_solib = kld_clear_solib;
487 kld_so_ops.solib_create_inferior_hook = kld_solib_create_inferior_hook;
488 kld_so_ops.special_symbol_handling = kld_special_symbol_handling;
489 kld_so_ops.current_sos = kld_current_sos;
490 kld_so_ops.open_symbol_file_object = kld_open_symbol_file_object;
491 kld_so_ops.in_dynsym_resolve_code = kld_in_dynsym_resolve_code;
492 kld_so_ops.find_and_open_solib = kld_find_and_open_solib;
493 kld_so_ops.bfd_open = solib_bfd_open;
495 c = add_com("add-kld", class_files, kgdb_add_kld_cmd,
496 "Usage: add-kld FILE\n\
497 Load the symbols from the kernel loadable module FILE.");
498 set_cmd_completer(c, filename_completer);