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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
38 #include <completer.h>
41 #include <frame-unwind.h>
51 CORE_ADDR base_address;
54 /* Offsets of fields in linker_file structure. */
55 static CORE_ADDR off_address, off_filename, off_pathname, off_next;
57 /* KVA of 'linker_path' which corresponds to the kern.module_path sysctl .*/
58 static CORE_ADDR module_path_addr;
59 static CORE_ADDR linker_files_addr;
60 static CORE_ADDR kernel_file_addr;
62 static struct target_so_ops kld_so_ops;
69 if (stat(path, &sb) == 0 && S_ISREG(sb.st_mode))
75 * Look for a matching file checking for debug suffixes before the raw file:
76 * - filename + ".debug" (e.g. foo.ko.debug)
77 * - filename (e.g. foo.ko)
79 static const char *kld_suffixes[] = {
86 check_kld_path (char *path, size_t path_size)
91 ep = path + strlen(path);
92 suffix = kld_suffixes;
93 while (*suffix != NULL) {
94 if (strlcat(path, *suffix, path_size) < path_size) {
99 /* Restore original path to remove suffix. */
107 * Try to find the path for a kld by looking in the kernel's directory and
108 * in the various paths in the module path.
111 find_kld_path (char *filename, char *path, size_t path_size)
114 char *kernel_dir, *module_dir, *cp;
118 kernel_dir = dirname(bfd_get_filename(exec_bfd));
119 if (kernel_dir != NULL) {
120 snprintf(path, path_size, "%s/%s", kernel_dir,
122 if (check_kld_path(path, path_size))
126 if (module_path_addr != 0) {
127 target_read_string(module_path_addr, &module_path, PATH_MAX,
130 make_cleanup(xfree, module_path);
132 while ((module_dir = strsep(&cp, ";")) != NULL) {
133 snprintf(path, path_size, "%s/%s", module_dir,
135 if (check_kld_path(path, path_size))
144 * Read a kernel pointer given a KVA in 'address'.
147 read_pointer (CORE_ADDR address)
151 if (target_read_memory(address, (char *)&value, TARGET_PTR_BIT / 8) !=
154 return (extract_unsigned_integer(&value, TARGET_PTR_BIT / 8));
158 * Try to find this kld in the kernel linker's list of linker files.
161 find_kld_address (char *arg, CORE_ADDR *address)
168 if (linker_files_addr == 0 || off_address == 0 || off_filename == 0 ||
172 filename = basename(arg);
173 for (kld = read_pointer(linker_files_addr); kld != 0;
174 kld = read_pointer(kld + off_next)) {
175 /* Try to read this linker file's filename. */
176 target_read_string(read_pointer(kld + off_filename),
177 &kld_filename, PATH_MAX, &error);
181 /* Compare this kld's filename against our passed in name. */
182 if (strcmp(kld_filename, filename) != 0) {
189 * We found a match, use its address as the base
190 * address if we can read it.
192 *address = read_pointer(kld + off_address);
201 load_kld (char *path, CORE_ADDR base_addr, int from_tty)
203 struct section_addr_info *sap;
204 struct section_table *sections = NULL, *sections_end = NULL, *s;
205 struct cleanup *cleanup;
210 bfd = bfd_openr(path, gnutarget);
212 error("\"%s\": can't open: %s", path,
213 bfd_errmsg(bfd_get_error()));
214 cleanup = make_cleanup_bfd_close(bfd);
216 if (!bfd_check_format(bfd, bfd_object))
217 error("\%s\": not an object file", path);
219 /* Make sure we have a .text section. */
220 if (bfd_get_section_by_name (bfd, ".text") == NULL)
221 error("\"%s\": can't find text section", path);
223 /* Build a section table from the bfd and relocate the sections. */
224 if (build_section_table (bfd, §ions, §ions_end))
225 error("\"%s\": can't find file sections", path);
226 cleanup = make_cleanup(xfree, sections);
227 for (s = sections; s < sections_end; s++) {
228 s->addr += base_addr;
229 s->endaddr += base_addr;
232 /* Build a section addr info to pass to symbol_file_add(). */
233 sap = build_section_addr_info_from_section_table (sections,
235 cleanup = make_cleanup((make_cleanup_ftype *)free_section_addr_info,
238 printf_unfiltered("add symbol table from file \"%s\" at\n", path);
239 for (i = 0; i < sap->num_sections; i++)
240 printf_unfiltered("\t%s_addr = %s\n", sap->other[i].name,
241 local_hex_string(sap->other[i].addr));
243 if (from_tty && (!query("%s", "")))
244 error("Not confirmed.");
246 symbol_file_add(path, from_tty, sap, 0, OBJF_USERLOADED);
248 do_cleanups(cleanup);
252 kgdb_add_kld_cmd (char *arg, int from_tty)
258 error("No kernel symbol file");
260 /* Try to open the raw path to handle absolute paths first. */
261 snprintf(path, sizeof(path), "%s", arg);
262 if (!check_kld_path(path, sizeof(path))) {
265 * If that didn't work, look in the various possible
266 * paths for the module.
268 if (!find_kld_path(arg, path, sizeof(path))) {
269 error("Unable to locate kld");
274 if (!find_kld_address(arg, &base_addr)) {
275 error("Unable to find kld in kernel");
279 load_kld(path, base_addr, from_tty);
281 reinit_frame_cache();
285 kld_relocate_section_addresses (struct so_list *so, struct section_table *sec)
288 sec->addr += so->lm_info->base_address;
289 sec->endaddr += so->lm_info->base_address;
293 kld_free_so (struct so_list *so)
300 kld_clear_solib (void)
305 kld_solib_create_inferior_hook (void)
310 kld_special_symbol_handling (void)
314 static struct so_list *
315 kld_current_sos (void)
317 struct so_list *head, **prev, *new;
318 CORE_ADDR kld, kernel;
322 if (linker_files_addr == 0 || kernel_file_addr == 0 ||
323 off_address == 0 || off_filename == 0 || off_next == 0)
330 * Walk the list of linker files creating so_list entries for
331 * each non-kernel file.
333 kernel = read_pointer(kernel_file_addr);
334 for (kld = read_pointer(linker_files_addr); kld != 0;
335 kld = read_pointer(kld + off_next)) {
336 /* Skip the main kernel file. */
340 new = xmalloc(sizeof(*new));
341 memset(new, 0, sizeof(*new));
343 new->lm_info = xmalloc(sizeof(*new->lm_info));
344 new->lm_info->base_address = 0;
346 /* Read the base filename and store it in so_original_name. */
347 target_read_string(read_pointer(kld + off_filename),
348 &path, sizeof(new->so_original_name), &error);
350 warning("kld_current_sos: Can't read filename: %s\n",
351 safe_strerror(error));
355 strlcpy(new->so_original_name, path,
356 sizeof(new->so_original_name));
360 * Try to read the pathname (if it exists) and store
363 if (off_pathname != 0) {
364 target_read_string(read_pointer(kld + off_pathname),
365 &path, sizeof(new->so_name), &error);
368 "kld_current_sos: Can't read pathname for \"%s\": %s\n",
369 new->so_original_name,
370 safe_strerror(error));
371 strlcpy(new->so_name, new->so_original_name,
372 sizeof(new->so_name));
374 strlcpy(new->so_name, path,
375 sizeof(new->so_name));
379 strlcpy(new->so_name, new->so_original_name,
380 sizeof(new->so_name));
382 /* Read this kld's base address. */
383 new->lm_info->base_address = read_pointer(kld + off_address);
384 if (new->lm_info->base_address == 0) {
386 "kld_current_sos: Invalid address for kld \"%s\"",
387 new->so_original_name);
392 /* Append to the list. */
401 kld_open_symbol_file_object (void *from_ttyp)
408 kld_in_dynsym_resolve_code (CORE_ADDR pc)
415 kld_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname)
420 *temp_pathname = NULL;
421 if (!find_kld_path(solib, path, sizeof(path))) {
425 fd = open(path, o_flags, 0);
427 *temp_pathname = xstrdup(path);
432 kld_new_objfile (struct objfile *objfile)
435 if (!have_partial_symbols())
439 * Compute offsets of relevant members in struct linker_file
440 * and the addresses of global variables. Don't warn about
441 * kernels that don't have 'pathname' in the linker_file
442 * struct since 6.x kernels don't have it.
444 off_address = kgdb_parse("&((struct linker_file *)0)->address");
445 off_filename = kgdb_parse("&((struct linker_file *)0)->filename");
446 off_pathname = kgdb_parse_quiet("&((struct linker_file *)0)->pathname");
447 off_next = kgdb_parse("&((struct linker_file *)0)->link.tqe_next");
448 module_path_addr = kgdb_parse("linker_path");
449 linker_files_addr = kgdb_parse("&linker_files.tqh_first");
450 kernel_file_addr = kgdb_parse("&linker_kernel_file");
454 load_klds_stub (void *arg)
457 SOLIB_ADD(NULL, 1, ¤t_target, auto_solib_add);
465 catch_errors(load_klds_stub, NULL, NULL, RETURN_MASK_ALL);
469 initialize_kld_target(void)
471 struct cmd_list_element *c;
473 kld_so_ops.relocate_section_addresses = kld_relocate_section_addresses;
474 kld_so_ops.free_so = kld_free_so;
475 kld_so_ops.clear_solib = kld_clear_solib;
476 kld_so_ops.solib_create_inferior_hook = kld_solib_create_inferior_hook;
477 kld_so_ops.special_symbol_handling = kld_special_symbol_handling;
478 kld_so_ops.current_sos = kld_current_sos;
479 kld_so_ops.open_symbol_file_object = kld_open_symbol_file_object;
480 kld_so_ops.in_dynsym_resolve_code = kld_in_dynsym_resolve_code;
481 kld_so_ops.find_and_open_solib = kld_find_and_open_solib;
483 current_target_so_ops = &kld_so_ops;
485 c = add_com("add-kld", class_files, kgdb_add_kld_cmd,
486 "Usage: add-kld FILE\n\
487 Load the symbols from the kernel loadable module FILE.");
488 set_cmd_completer(c, filename_completer);