]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - gnu/usr.bin/gdb/kgdb/kld.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / gnu / usr.bin / gdb / kgdb / kld.c
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$");
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
50 struct lm_info {
51         CORE_ADDR base_address;
52 };
53
54 /* Offsets of fields in linker_file structure. */
55 static CORE_ADDR off_address, off_filename, off_pathname, off_next;
56
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;
61
62 static struct target_so_ops kld_so_ops;
63
64 static int
65 kld_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  */
79 static const char *kld_suffixes[] = {
80         ".debug",
81         "",
82         NULL
83 };
84
85 static int
86 check_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  */
110 static int
111 find_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  */
146 static CORE_ADDR
147 read_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  */
160 static int
161 find_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
200 static void
201 adjust_section_address (struct section_table *sec, CORE_ADDR *curr_base)
202 {
203         struct bfd_section *asect = sec->the_bfd_section;
204         bfd *abfd = sec->bfd;
205
206         if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) {
207                 sec->addr += *curr_base;
208                 sec->endaddr += *curr_base;
209                 return;
210         }
211
212         *curr_base = align_power(*curr_base,
213             bfd_get_section_alignment(abfd, asect));
214         sec->addr = *curr_base;
215         sec->endaddr = sec->addr + bfd_section_size(abfd, asect);
216         *curr_base = sec->endaddr;
217 }
218
219 static void
220 load_kld (char *path, CORE_ADDR base_addr, int from_tty)
221 {
222         struct section_addr_info *sap;
223         struct section_table *sections = NULL, *sections_end = NULL, *s;
224         struct cleanup *cleanup;
225         bfd *bfd;
226         CORE_ADDR curr_addr;
227         int i;
228
229         /* Open the kld. */
230         bfd = bfd_openr(path, gnutarget);
231         if (bfd == NULL)
232                 error("\"%s\": can't open: %s", path,
233                     bfd_errmsg(bfd_get_error()));
234         cleanup = make_cleanup_bfd_close(bfd);
235
236         if (!bfd_check_format(bfd, bfd_object))
237                 error("\%s\": not an object file", path);
238
239         /* Make sure we have a .text section. */
240         if (bfd_get_section_by_name (bfd, ".text") == NULL)
241                 error("\"%s\": can't find text section", path);
242
243         /* Build a section table from the bfd and relocate the sections. */
244         if (build_section_table (bfd, &sections, &sections_end))
245                 error("\"%s\": can't find file sections", path);
246         cleanup = make_cleanup(xfree, sections);
247         curr_addr = base_addr;
248         for (s = sections; s < sections_end; s++)
249                 adjust_section_address(s, &curr_addr);
250
251         /* Build a section addr info to pass to symbol_file_add(). */
252         sap = build_section_addr_info_from_section_table (sections,
253             sections_end);
254         cleanup = make_cleanup((make_cleanup_ftype *)free_section_addr_info,
255             sap);
256
257         printf_unfiltered("add symbol table from file \"%s\" at\n", path);
258         for (i = 0; i < sap->num_sections; i++)
259                 printf_unfiltered("\t%s_addr = %s\n", sap->other[i].name,
260                     local_hex_string(sap->other[i].addr));              
261
262         if (from_tty && (!query("%s", "")))
263                 error("Not confirmed.");
264
265         symbol_file_add(path, from_tty, sap, 0, OBJF_USERLOADED);
266
267         do_cleanups(cleanup);
268 }
269
270 static void
271 kgdb_add_kld_cmd (char *arg, int from_tty)
272 {
273         char path[PATH_MAX];
274         CORE_ADDR base_addr;
275
276         if (!exec_bfd)
277                 error("No kernel symbol file");
278
279         /* Try to open the raw path to handle absolute paths first. */
280         snprintf(path, sizeof(path), "%s", arg);
281         if (!check_kld_path(path, sizeof(path))) {
282
283                 /*
284                  * If that didn't work, look in the various possible
285                  * paths for the module.
286                  */
287                 if (!find_kld_path(arg, path, sizeof(path))) {
288                         error("Unable to locate kld");
289                         return;
290                 }
291         }
292
293         if (!find_kld_address(arg, &base_addr)) {
294                 error("Unable to find kld in kernel");
295                 return;
296         }
297
298         load_kld(path, base_addr, from_tty);
299
300         reinit_frame_cache();
301 }
302
303 static void
304 kld_relocate_section_addresses (struct so_list *so, struct section_table *sec)
305 {
306         static CORE_ADDR curr_addr;
307
308         if (sec == so->sections)
309                 curr_addr = so->lm_info->base_address;
310
311         adjust_section_address(sec, &curr_addr);
312 }
313
314 static void
315 kld_free_so (struct so_list *so)
316 {
317
318         xfree(so->lm_info);
319 }
320
321 static void
322 kld_clear_solib (void)
323 {
324 }
325
326 static void
327 kld_solib_create_inferior_hook (void)
328 {
329 }
330
331 static void
332 kld_special_symbol_handling (void)
333 {
334 }
335
336 static struct so_list *
337 kld_current_sos (void)
338 {
339         struct so_list *head, **prev, *new;
340         CORE_ADDR kld, kernel;
341         char *path;
342         int error;
343
344         if (linker_files_addr == 0 || kernel_file_addr == 0 ||
345             off_address == 0 || off_filename == 0 || off_next == 0)
346                 return (NULL);
347
348         head = NULL;
349         prev = &head;
350
351         /*
352          * Walk the list of linker files creating so_list entries for
353          * each non-kernel file.
354          */
355         kernel = read_pointer(kernel_file_addr);
356         for (kld = read_pointer(linker_files_addr); kld != 0;
357              kld = read_pointer(kld + off_next)) {
358                 /* Skip the main kernel file. */
359                 if (kld == kernel)
360                         continue;
361
362                 new = xmalloc(sizeof(*new));
363                 memset(new, 0, sizeof(*new));
364
365                 new->lm_info = xmalloc(sizeof(*new->lm_info));
366                 new->lm_info->base_address = 0;
367
368                 /* Read the base filename and store it in so_original_name. */
369                 target_read_string(read_pointer(kld + off_filename),
370                     &path, sizeof(new->so_original_name), &error);
371                 if (error != 0) {
372                         warning("kld_current_sos: Can't read filename: %s\n",
373                             safe_strerror(error));
374                         free_so(new);
375                         continue;
376                 }
377                 strlcpy(new->so_original_name, path,
378                     sizeof(new->so_original_name));
379                 xfree(path);
380
381                 /*
382                  * Try to read the pathname (if it exists) and store
383                  * it in so_name.
384                  */
385                 if (off_pathname != 0) {
386                         target_read_string(read_pointer(kld + off_pathname),
387                             &path, sizeof(new->so_name), &error);
388                         if (error != 0) {
389                                 warning(
390                     "kld_current_sos: Can't read pathname for \"%s\": %s\n",
391                                     new->so_original_name,
392                                     safe_strerror(error));
393                                 strlcpy(new->so_name, new->so_original_name,
394                                     sizeof(new->so_name));
395                         } else {
396                                 strlcpy(new->so_name, path,
397                                     sizeof(new->so_name));
398                                 xfree(path);
399                         }
400                 } else
401                         strlcpy(new->so_name, new->so_original_name,
402                             sizeof(new->so_name));
403
404                 /* Read this kld's base address. */
405                 new->lm_info->base_address = read_pointer(kld + off_address);
406                 if (new->lm_info->base_address == 0) {
407                         warning(
408                             "kld_current_sos: Invalid address for kld \"%s\"",
409                             new->so_original_name);
410                         free_so(new);
411                         continue;
412                 }
413
414                 /* Append to the list. */
415                 *prev = new;
416                 prev = &new->next;
417         }
418
419         return (head);
420 }
421
422 static int
423 kld_open_symbol_file_object (void *from_ttyp)
424 {
425
426         return (0);
427 }
428
429 static int
430 kld_in_dynsym_resolve_code (CORE_ADDR pc)
431 {
432
433         return (0);
434 }
435
436 static int
437 kld_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname)
438 {
439         char path[PATH_MAX];
440         int fd;
441
442         *temp_pathname = NULL;
443         if (!find_kld_path(solib, path, sizeof(path))) {
444                 errno = ENOENT;
445                 return (-1);
446         }
447         fd = open(path, o_flags, 0);
448         if (fd >= 0)
449                 *temp_pathname = xstrdup(path);
450         return (fd);
451 }
452
453 void
454 kld_new_objfile (struct objfile *objfile)
455 {
456
457         if (!have_partial_symbols())
458                 return;
459
460         /*
461          * Compute offsets of relevant members in struct linker_file
462          * and the addresses of global variables.  Don't warn about
463          * kernels that don't have 'pathname' in the linker_file
464          * struct since 6.x kernels don't have it.
465          */
466         off_address = kgdb_parse("&((struct linker_file *)0)->address");
467         off_filename = kgdb_parse("&((struct linker_file *)0)->filename");
468         off_pathname = kgdb_parse_quiet("&((struct linker_file *)0)->pathname");
469         off_next = kgdb_parse("&((struct linker_file *)0)->link.tqe_next");
470         module_path_addr = kgdb_parse("linker_path");
471         linker_files_addr = kgdb_parse("&linker_files.tqh_first");
472         kernel_file_addr = kgdb_parse("&linker_kernel_file");
473 }
474
475 static int
476 load_klds_stub (void *arg)
477 {
478
479         SOLIB_ADD(NULL, 1, &current_target, auto_solib_add);
480         return (0);
481 }
482
483 void
484 kld_init (void)
485 {
486
487         catch_errors(load_klds_stub, NULL, NULL, RETURN_MASK_ALL);
488 }
489
490 void
491 initialize_kld_target(void)
492 {
493         struct cmd_list_element *c;
494
495         kld_so_ops.relocate_section_addresses = kld_relocate_section_addresses;
496         kld_so_ops.free_so = kld_free_so;
497         kld_so_ops.clear_solib = kld_clear_solib;
498         kld_so_ops.solib_create_inferior_hook = kld_solib_create_inferior_hook;
499         kld_so_ops.special_symbol_handling = kld_special_symbol_handling;
500         kld_so_ops.current_sos = kld_current_sos;
501         kld_so_ops.open_symbol_file_object = kld_open_symbol_file_object;
502         kld_so_ops.in_dynsym_resolve_code = kld_in_dynsym_resolve_code;
503         kld_so_ops.find_and_open_solib = kld_find_and_open_solib;
504
505         current_target_so_ops = &kld_so_ops;
506
507         c = add_com("add-kld", class_files, kgdb_add_kld_cmd,
508            "Usage: add-kld FILE\n\
509 Load the symbols from the kernel loadable module FILE.");
510         set_cmd_completer(c, filename_completer);
511 }