kld.c revision 177701
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 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/gnu/usr.bin/gdb/kgdb/kld.c 177701 2008-03-29 03:48:06Z jhb $"); 29 30#include <sys/param.h> 31#include <sys/stat.h> 32#include <fcntl.h> 33#include <kvm.h> 34#include <libgen.h> 35 36#include <defs.h> 37#include <command.h> 38#include <completer.h> 39#include <environ.h> 40#include <frame-unwind.h> 41#include <inferior.h> 42#include <objfiles.h> 43#include <gdbcore.h> 44#include <language.h> 45#include <solist.h> 46 47#include "kgdb.h" 48 49struct lm_info { 50 CORE_ADDR base_address; 51}; 52 53/* Offsets of fields in linker_file structure. */ 54static CORE_ADDR off_address, off_filename, off_pathname, off_next; 55 56/* KVA of 'linker_path' which corresponds to the kern.module_path sysctl .*/ 57static CORE_ADDR module_path_addr; 58 59static struct target_so_ops kld_so_ops; 60 61static int 62kld_ok (char *path) 63{ 64 struct stat sb; 65 66 if (stat(path, &sb) == 0 && S_ISREG(sb.st_mode)) 67 return (1); 68 return (0); 69} 70 71/* 72 * Look for a matching file checking for debug suffixes before the raw file: 73 * - filename + ".symbols" (e.g. foo.ko.symbols) 74 * - filename + ".debug" (e.g. foo.ko.debug) 75 * - filename (e.g. foo.ko) 76 */ 77static const char *kld_suffixes[] = { 78 ".debug", 79 "", 80 NULL 81}; 82 83static int 84check_kld_path (char *path, size_t path_size) 85{ 86 const char **suffix; 87 char *ep; 88 89 ep = path + strlen(path); 90 suffix = kld_suffixes; 91 while (*suffix != NULL) { 92 if (strlcat(path, *suffix, path_size) < path_size) { 93 if (kld_ok(path)) 94 return (1); 95 } 96 97 /* Restore original path to remove suffix. */ 98 *ep = '\0'; 99 suffix++; 100 } 101 return (0); 102} 103 104/* 105 * Try to find the path for a kld by looking in the kernel's directory and 106 * in the various paths in the module path. 107 */ 108static int 109find_kld_path (char *filename, char *path, size_t path_size) 110{ 111 char *module_path; 112 char *kernel_dir, *module_dir, *cp; 113 int error; 114 115 kernel_dir = dirname(kernel); 116 if (kernel_dir != NULL) { 117 snprintf(path, path_size, "%s/%s", kernel_dir, filename); 118 if (check_kld_path(path, path_size)) 119 return (1); 120 } 121 if (module_path_addr != 0) { 122 target_read_string(module_path_addr, &module_path, PATH_MAX, 123 &error); 124 if (error == 0) { 125 make_cleanup(xfree, module_path); 126 cp = module_path; 127 while ((module_dir = strsep(&cp, ";")) != NULL) { 128 snprintf(path, path_size, "%s/%s", module_dir, 129 filename); 130 if (check_kld_path(path, path_size)) 131 return (1); 132 } 133 } 134 } 135 return (0); 136} 137 138/* 139 * Read a kernel pointer given a KVA in 'address'. 140 */ 141static CORE_ADDR 142read_pointer (CORE_ADDR address) 143{ 144 CORE_ADDR value; 145 146 if (target_read_memory(address, (char *)&value, TARGET_PTR_BIT / 8) != 147 0) 148 return (0); 149 return (extract_unsigned_integer(&value, TARGET_PTR_BIT / 8)); 150} 151 152/* 153 * Try to find this kld in the kernel linker's list of linker files. 154 */ 155static int 156find_kld_address (char *arg, CORE_ADDR *address) 157{ 158 CORE_ADDR kld; 159 char *kld_filename; 160 char *filename; 161 int error; 162 163 if (off_address == 0 || off_filename == 0 || off_next == 0) 164 return (0); 165 166 filename = basename(arg); 167 for (kld = kgdb_parse("linker_files.tqh_first"); kld != 0; 168 kld = read_pointer(kld + off_next)) { 169 /* Try to read this linker file's filename. */ 170 target_read_string(read_pointer(kld + off_filename), 171 &kld_filename, PATH_MAX, &error); 172 if (error) 173 continue; 174 175 /* Compare this kld's filename against our passed in name. */ 176 if (strcmp(kld_filename, filename) != 0) { 177 xfree(kld_filename); 178 continue; 179 } 180 xfree(kld_filename); 181 182 /* 183 * We found a match, use its address as the base 184 * address if we can read it. 185 */ 186 *address = read_pointer(kld + off_address); 187 if (*address == 0) 188 return (0); 189 return (1); 190 } 191 return (0); 192} 193 194struct add_section_info { 195 struct section_addr_info *section_addrs; 196 int sect_index; 197 CORE_ADDR base_addr; 198 int add_kld_command; 199}; 200 201static void 202add_section (bfd *bfd, asection *sect, void *arg) 203{ 204 struct add_section_info *asi = arg; 205 CORE_ADDR address; 206 char *name; 207 208 /* Ignore non-resident sections. */ 209 if ((bfd_get_section_flags(bfd, sect) & (SEC_ALLOC | SEC_LOAD)) == 0) 210 return; 211 212 name = xstrdup(bfd_get_section_name(bfd, sect)); 213 make_cleanup(xfree, name); 214 address = asi->base_addr + bfd_get_section_vma(bfd, sect); 215 asi->section_addrs->other[asi->sect_index].name = name; 216 asi->section_addrs->other[asi->sect_index].addr = address; 217 asi->section_addrs->other[asi->sect_index].sectindex = sect->index; 218 if (asi->add_kld_command) 219 printf_unfiltered("\t%s_addr = %s\n", name, 220 local_hex_string(address)); 221 asi->sect_index++; 222} 223 224static void 225load_kld (char *path, CORE_ADDR base_addr, int from_tty, int add_kld_command) 226{ 227 struct add_section_info asi; 228 struct cleanup *cleanup; 229 bfd *bfd; 230 231 /* Open the kld. */ 232 bfd = bfd_openr(path, gnutarget); 233 if (bfd == NULL) 234 error("\"%s\": can't open: %s", path, 235 bfd_errmsg(bfd_get_error())); 236 cleanup = make_cleanup_bfd_close(bfd); 237 238 if (!bfd_check_format(bfd, bfd_object)) 239 error("\%s\": not an object file", path); 240 241 /* Make sure we have a .text section. */ 242 if (bfd_get_section_by_name (bfd, ".text") == NULL) 243 error("\"%s\": can't find text section", path); 244 245 if (add_kld_command) 246 printf_unfiltered("add symbol table from file \"%s\" at\n", 247 path); 248 249 /* Build a section table for symbol_file_add() from the bfd sections. */ 250 asi.section_addrs = alloc_section_addr_info(bfd_count_sections(bfd)); 251 cleanup = make_cleanup(xfree, asi.section_addrs); 252 asi.sect_index = 0; 253 asi.base_addr = base_addr; 254 asi.add_kld_command = add_kld_command; 255 bfd_map_over_sections(bfd, add_section, &asi); 256 257 if (from_tty && (!query("%s", ""))) 258 error("Not confirmed."); 259 260 symbol_file_add(path, from_tty, asi.section_addrs, 0, 261 add_kld_command ? OBJF_USERLOADED : 0); 262 263 do_cleanups(cleanup); 264} 265 266void 267kgdb_add_kld_cmd (char *arg, int from_tty) 268{ 269 char path[PATH_MAX]; 270 CORE_ADDR base_addr; 271 272 /* Try to open the raw path to handle absolute paths first. */ 273 snprintf(path, sizeof(path), "%s", arg); 274 if (!check_kld_path(path, sizeof(path))) { 275 276 /* 277 * If that didn't work, look in the various possible 278 * paths for the module. 279 */ 280 if (!find_kld_path(arg, path, sizeof(path))) { 281 error("Unable to locate kld"); 282 return; 283 } 284 } 285 286 if (!find_kld_address(arg, &base_addr)) { 287 error("Unable to find kld in kernel"); 288 return; 289 } 290 291 load_kld(path, base_addr, from_tty, 1); 292 293 reinit_frame_cache(); 294} 295 296static void 297kld_relocate_section_addresses (struct so_list *so, struct section_table *sec) 298{ 299 300 sec->addr += so->lm_info->base_address; 301 sec->endaddr += so->lm_info->base_address; 302} 303 304static void 305kld_free_so (struct so_list *so) 306{ 307 308 xfree(so->lm_info); 309} 310 311static void 312kld_clear_solib (void) 313{ 314} 315 316static void 317kld_solib_create_inferior_hook (void) 318{ 319} 320 321static void 322kld_special_symbol_handling (void) 323{ 324} 325 326static struct so_list * 327kld_current_sos (void) 328{ 329 struct so_list *head, **prev, *new; 330 CORE_ADDR kld, kernel; 331 char *path; 332 int error; 333 334 head = NULL; 335 prev = &head; 336 337 /* 338 * Walk the list of linker files creating so_list entries for 339 * each non-kernel file. 340 */ 341 kernel = kgdb_parse("linker_kernel_file"); 342 for (kld = kgdb_parse("linker_files.tqh_first"); kld != 0; 343 kld = read_pointer(kld + off_next)) { 344 /* Skip the main kernel file. */ 345 if (kld == kernel) 346 continue; 347 348 new = xmalloc(sizeof(*new)); 349 memset(new, 0, sizeof(*new)); 350 351 new->lm_info = xmalloc(sizeof(*new->lm_info)); 352 new->lm_info->base_address = 0; 353 354 /* Read the base filename and store it in so_original_name. */ 355 target_read_string(read_pointer(kld + off_filename), 356 &path, sizeof(new->so_original_name), &error); 357 if (error != 0) { 358 warning("kld_current_sos: Can't read filename: %s\n", 359 safe_strerror(error)); 360 free_so(new); 361 continue; 362 } 363 strlcpy(new->so_original_name, path, 364 sizeof(new->so_original_name)); 365 xfree(path); 366 367 /* 368 * Try to read the pathname (if it exists) and store 369 * it in so_name. 370 */ 371 if (off_pathname != 0) { 372 target_read_string(read_pointer(kld + off_pathname), 373 &path, sizeof(new->so_name), &error); 374 if (error != 0) { 375 warning( 376 "kld_current_sos: Can't read pathname for \"%s\": %s\n", 377 new->so_original_name, 378 safe_strerror(error)); 379 strlcpy(new->so_name, new->so_original_name, 380 sizeof(new->so_name)); 381 } else { 382 strlcpy(new->so_name, path, 383 sizeof(new->so_name)); 384 xfree(path); 385 } 386 } else 387 strlcpy(new->so_name, new->so_original_name, 388 sizeof(new->so_name)); 389 390 /* Read this kld's base address. */ 391 new->lm_info->base_address = read_pointer(kld + off_address); 392 if (new->lm_info->base_address == 0) { 393 warning( 394 "kld_current_sos: Invalid address for kld \"%s\"", 395 new->so_original_name); 396 free_so(new); 397 continue; 398 } 399 400 /* Append to the list. */ 401 *prev = new; 402 prev = &new->next; 403 } 404 405 return (head); 406} 407 408static int 409kld_open_symbol_file_object (void *from_ttyp) 410{ 411 412 return (0); 413} 414 415static int 416kld_in_dynsym_resolve_code (CORE_ADDR pc) 417{ 418 419 return (0); 420} 421 422static int 423kld_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname) 424{ 425 char path[PATH_MAX]; 426 int fd; 427 428 *temp_pathname = NULL; 429 if (!find_kld_path(solib, path, sizeof(path))) { 430 errno = ENOENT; 431 return (-1); 432 } 433 fd = open(path, o_flags, 0); 434 if (fd >= 0) 435 *temp_pathname = xstrdup(path); 436 return (fd); 437} 438 439static int 440load_klds_stub (void *arg) 441{ 442 443 SOLIB_ADD(NULL, 1, ¤t_target, auto_solib_add); 444 return (0); 445} 446 447void 448kgdb_kld_init (void) 449{ 450 struct cmd_list_element *c; 451 452 /* Compute offsets of relevant members in struct linker_file. */ 453 off_address = kgdb_parse("&((struct linker_file *)0)->address"); 454 off_filename = kgdb_parse("&((struct linker_file *)0)->filename"); 455 off_pathname = kgdb_parse("&((struct linker_file *)0)->pathname"); 456 off_next = kgdb_parse("&((struct linker_file *)0)->link.tqe_next"); 457 if (off_address == 0 || off_filename == 0 || off_next == 0) 458 return; 459 460 module_path_addr = kgdb_parse("linker_path"); 461 462 kld_so_ops.relocate_section_addresses = kld_relocate_section_addresses; 463 kld_so_ops.free_so = kld_free_so; 464 kld_so_ops.clear_solib = kld_clear_solib; 465 kld_so_ops.solib_create_inferior_hook = kld_solib_create_inferior_hook; 466 kld_so_ops.special_symbol_handling = kld_special_symbol_handling; 467 kld_so_ops.current_sos = kld_current_sos; 468 kld_so_ops.open_symbol_file_object = kld_open_symbol_file_object; 469 kld_so_ops.in_dynsym_resolve_code = kld_in_dynsym_resolve_code; 470 kld_so_ops.find_and_open_solib = kld_find_and_open_solib; 471 472 current_target_so_ops = &kld_so_ops; 473 474 catch_errors(load_klds_stub, NULL, NULL, RETURN_MASK_ALL); 475 476 c = add_com("add-kld", class_files, kgdb_add_kld_cmd, 477 "Usage: add-kld FILE\n\ 478Load the symbols from the kernel loadable module FILE."); 479 set_cmd_completer(c, filename_completer); 480} 481