kld.c revision 183359
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 183359 2008-09-25 19:32:03Z 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 <exec.h> 41#include <frame-unwind.h> 42#include <inferior.h> 43#include <objfiles.h> 44#include <gdbcore.h> 45#include <language.h> 46#include <solist.h> 47 48#include "kgdb.h" 49 50struct lm_info { 51 CORE_ADDR base_address; 52}; 53 54/* Offsets of fields in linker_file structure. */ 55static CORE_ADDR off_address, off_filename, off_pathname, off_next; 56 57/* KVA of 'linker_path' which corresponds to the kern.module_path sysctl .*/ 58static CORE_ADDR module_path_addr; 59static CORE_ADDR linker_files_addr; 60static CORE_ADDR kernel_file_addr; 61 62static struct target_so_ops kld_so_ops; 63 64static int 65kld_ok (char *path) 66{ 67 struct stat sb; 68 69 if (stat(path, &sb) == 0 && S_ISREG(sb.st_mode)) 70 return (1); 71 return (0); 72} 73 74/* 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) 78 */ 79static const char *kld_suffixes[] = { 80 ".debug", 81 "", 82 NULL 83}; 84 85static int 86check_kld_path (char *path, size_t path_size) 87{ 88 const char **suffix; 89 char *ep; 90 91 ep = path + strlen(path); 92 suffix = kld_suffixes; 93 while (*suffix != NULL) { 94 if (strlcat(path, *suffix, path_size) < path_size) { 95 if (kld_ok(path)) 96 return (1); 97 } 98 99 /* Restore original path to remove suffix. */ 100 *ep = '\0'; 101 suffix++; 102 } 103 return (0); 104} 105 106/* 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. 109 */ 110static int 111find_kld_path (char *filename, char *path, size_t path_size) 112{ 113 char *module_path; 114 char *kernel_dir, *module_dir, *cp; 115 int error; 116 117 if (exec_bfd) { 118 kernel_dir = dirname(bfd_get_filename(exec_bfd)); 119 if (kernel_dir != NULL) { 120 snprintf(path, path_size, "%s/%s", kernel_dir, 121 filename); 122 if (check_kld_path(path, path_size)) 123 return (1); 124 } 125 } 126 if (module_path_addr != 0) { 127 target_read_string(module_path_addr, &module_path, PATH_MAX, 128 &error); 129 if (error == 0) { 130 make_cleanup(xfree, module_path); 131 cp = module_path; 132 while ((module_dir = strsep(&cp, ";")) != NULL) { 133 snprintf(path, path_size, "%s/%s", module_dir, 134 filename); 135 if (check_kld_path(path, path_size)) 136 return (1); 137 } 138 } 139 } 140 return (0); 141} 142 143/* 144 * Read a kernel pointer given a KVA in 'address'. 145 */ 146static CORE_ADDR 147read_pointer (CORE_ADDR address) 148{ 149 CORE_ADDR value; 150 151 if (target_read_memory(address, (char *)&value, TARGET_PTR_BIT / 8) != 152 0) 153 return (0); 154 return (extract_unsigned_integer(&value, TARGET_PTR_BIT / 8)); 155} 156 157/* 158 * Try to find this kld in the kernel linker's list of linker files. 159 */ 160static int 161find_kld_address (char *arg, CORE_ADDR *address) 162{ 163 CORE_ADDR kld; 164 char *kld_filename; 165 char *filename; 166 int error; 167 168 if (linker_files_addr == 0 || off_address == 0 || off_filename == 0 || 169 off_next == 0) 170 return (0); 171 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); 178 if (error) 179 continue; 180 181 /* Compare this kld's filename against our passed in name. */ 182 if (strcmp(kld_filename, filename) != 0) { 183 xfree(kld_filename); 184 continue; 185 } 186 xfree(kld_filename); 187 188 /* 189 * We found a match, use its address as the base 190 * address if we can read it. 191 */ 192 *address = read_pointer(kld + off_address); 193 if (*address == 0) 194 return (0); 195 return (1); 196 } 197 return (0); 198} 199 200static void 201load_kld (char *path, CORE_ADDR base_addr, int from_tty) 202{ 203 struct section_addr_info *sap; 204 struct section_table *sections, *sections_end, *s; 205 struct cleanup *cleanup; 206 bfd *bfd; 207 int i; 208 209 /* Open the kld. */ 210 bfd = bfd_openr(path, gnutarget); 211 if (bfd == NULL) 212 error("\"%s\": can't open: %s", path, 213 bfd_errmsg(bfd_get_error())); 214 cleanup = make_cleanup_bfd_close(bfd); 215 216 if (!bfd_check_format(bfd, bfd_object)) 217 error("\%s\": not an object file", path); 218 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); 222 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; 230 } 231 232 /* Build a section addr info to pass to symbol_file_add(). */ 233 sap = build_section_addr_info_from_section_table (sections, 234 sections_end); 235 cleanup = make_cleanup((make_cleanup_ftype *)free_section_addr_info, 236 sap); 237 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)); 242 243 if (from_tty && (!query("%s", ""))) 244 error("Not confirmed."); 245 246 symbol_file_add(path, from_tty, sap, 0, OBJF_USERLOADED); 247 248 do_cleanups(cleanup); 249} 250 251static void 252kgdb_add_kld_cmd (char *arg, int from_tty) 253{ 254 char path[PATH_MAX]; 255 CORE_ADDR base_addr; 256 257 if (!exec_bfd) 258 error("No kernel symbol file"); 259 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))) { 263 264 /* 265 * If that didn't work, look in the various possible 266 * paths for the module. 267 */ 268 if (!find_kld_path(arg, path, sizeof(path))) { 269 error("Unable to locate kld"); 270 return; 271 } 272 } 273 274 if (!find_kld_address(arg, &base_addr)) { 275 error("Unable to find kld in kernel"); 276 return; 277 } 278 279 load_kld(path, base_addr, from_tty); 280 281 reinit_frame_cache(); 282} 283 284static void 285kld_relocate_section_addresses (struct so_list *so, struct section_table *sec) 286{ 287 288 sec->addr += so->lm_info->base_address; 289 sec->endaddr += so->lm_info->base_address; 290} 291 292static void 293kld_free_so (struct so_list *so) 294{ 295 296 xfree(so->lm_info); 297} 298 299static void 300kld_clear_solib (void) 301{ 302} 303 304static void 305kld_solib_create_inferior_hook (void) 306{ 307} 308 309static void 310kld_special_symbol_handling (void) 311{ 312} 313 314static struct so_list * 315kld_current_sos (void) 316{ 317 struct so_list *head, **prev, *new; 318 CORE_ADDR kld, kernel; 319 char *path; 320 int error; 321 322 if (linker_files_addr == 0 || kernel_file_addr == 0 || 323 off_address == 0 || off_filename == 0 || off_next == 0) 324 return (NULL); 325 326 head = NULL; 327 prev = &head; 328 329 /* 330 * Walk the list of linker files creating so_list entries for 331 * each non-kernel file. 332 */ 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. */ 337 if (kld == kernel) 338 continue; 339 340 new = xmalloc(sizeof(*new)); 341 memset(new, 0, sizeof(*new)); 342 343 new->lm_info = xmalloc(sizeof(*new->lm_info)); 344 new->lm_info->base_address = 0; 345 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); 349 if (error != 0) { 350 warning("kld_current_sos: Can't read filename: %s\n", 351 safe_strerror(error)); 352 free_so(new); 353 continue; 354 } 355 strlcpy(new->so_original_name, path, 356 sizeof(new->so_original_name)); 357 xfree(path); 358 359 /* 360 * Try to read the pathname (if it exists) and store 361 * it in so_name. 362 */ 363 if (off_pathname != 0) { 364 target_read_string(read_pointer(kld + off_pathname), 365 &path, sizeof(new->so_name), &error); 366 if (error != 0) { 367 warning( 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)); 373 } else { 374 strlcpy(new->so_name, path, 375 sizeof(new->so_name)); 376 xfree(path); 377 } 378 } else 379 strlcpy(new->so_name, new->so_original_name, 380 sizeof(new->so_name)); 381 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) { 385 warning( 386 "kld_current_sos: Invalid address for kld \"%s\"", 387 new->so_original_name); 388 free_so(new); 389 continue; 390 } 391 392 /* Append to the list. */ 393 *prev = new; 394 prev = &new->next; 395 } 396 397 return (head); 398} 399 400static int 401kld_open_symbol_file_object (void *from_ttyp) 402{ 403 404 return (0); 405} 406 407static int 408kld_in_dynsym_resolve_code (CORE_ADDR pc) 409{ 410 411 return (0); 412} 413 414static int 415kld_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname) 416{ 417 char path[PATH_MAX]; 418 int fd; 419 420 *temp_pathname = NULL; 421 if (!find_kld_path(solib, path, sizeof(path))) { 422 errno = ENOENT; 423 return (-1); 424 } 425 fd = open(path, o_flags, 0); 426 if (fd >= 0) 427 *temp_pathname = xstrdup(path); 428 return (fd); 429} 430 431void 432kld_new_objfile (struct objfile *objfile) 433{ 434 435 if (!have_partial_symbols()) 436 return; 437 438 /* 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. 443 */ 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"); 451} 452 453static int 454load_klds_stub (void *arg) 455{ 456 457 SOLIB_ADD(NULL, 1, ¤t_target, auto_solib_add); 458 return (0); 459} 460 461void 462kld_init (void) 463{ 464 465 catch_errors(load_klds_stub, NULL, NULL, RETURN_MASK_ALL); 466} 467 468void 469initialize_kld_target(void) 470{ 471 struct cmd_list_element *c; 472 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; 482 483 current_target_so_ops = &kld_so_ops; 484 485 c = add_com("add-kld", class_files, kgdb_add_kld_cmd, 486 "Usage: add-kld FILE\n\ 487Load the symbols from the kernel loadable module FILE."); 488 set_cmd_completer(c, filename_completer); 489} 490