xref: /dragonfly/gnu/usr.bin/gdb/kgdb/kld.c (revision c89a6c1b)
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  * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/kld.c,v 1.11 2008/10/02 20:42:10 jhb Exp $
27  */
28 
29 #include <sys/param.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <kvm.h>
33 #include <libgen.h>
34 
35 #include <defs.h>
36 #include <command.h>
37 #include <completer.h>
38 #include <environ.h>
39 #include <exec.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 #include <arch-utils.h>
47 #include <solib.h>
48 #include <exceptions.h>
49 #include <observer.h>
50 
51 #include "kgdb.h"
52 
53 struct lm_info {
54 	CORE_ADDR base_address;
55 };
56 
57 /* Offsets of fields in linker_file structure. */
58 static CORE_ADDR off_address, off_filename, off_pathname, off_next;
59 
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;
64 
65 static struct target_so_ops kld_so_ops;
66 
67 static int
68 kld_ok (char *path)
69 {
70 	struct stat sb;
71 
72 	if (stat(path, &sb) == 0 && S_ISREG(sb.st_mode))
73 		return (1);
74 	return (0);
75 }
76 
77 /*
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)
81  */
82 static const char *kld_suffixes[] = {
83 	".debug",
84 	"",
85 	NULL
86 };
87 
88 static int
89 check_kld_path (char *path, size_t path_size)
90 {
91 	const char **suffix;
92 	char *ep;
93 
94 	ep = path + strlen(path);
95 	suffix = kld_suffixes;
96 	while (*suffix != NULL) {
97 		if (strlcat(path, *suffix, path_size) < path_size) {
98 			if (kld_ok(path))
99 				return (1);
100 		}
101 
102 		/* Restore original path to remove suffix. */
103 		*ep = '\0';
104 		suffix++;
105 	}
106 	return (0);
107 }
108 
109 /*
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.
112  */
113 static int
114 find_kld_path (char *filename, char *path, size_t path_size)
115 {
116 	char *module_path;
117 	char *kernel_dir, *module_dir, *cp;
118 	int error;
119 
120 	if (exec_bfd) {
121 		kernel_dir = dirname(bfd_get_filename(exec_bfd));
122 		if (kernel_dir != NULL) {
123 			snprintf(path, path_size, "%s/%s", kernel_dir,
124 			    filename);
125 			if (check_kld_path(path, path_size))
126 				return (1);
127 		}
128 	}
129 	if (module_path_addr != 0) {
130 		target_read_string(module_path_addr, &module_path, PATH_MAX,
131 		    &error);
132 		if (error == 0) {
133 			make_cleanup(xfree, module_path);
134 			cp = module_path;
135 			while ((module_dir = strsep(&cp, ";")) != NULL) {
136 				snprintf(path, path_size, "%s/%s", module_dir,
137 				    filename);
138 				if (check_kld_path(path, path_size))
139 					return (1);
140 			}
141 		}
142 	}
143 	return (0);
144 }
145 
146 /*
147  * Read a kernel pointer given a KVA in 'address'.
148  */
149 static CORE_ADDR
150 read_pointer (CORE_ADDR address)
151 {
152 	struct gdbarch *arch = get_current_arch();
153 	enum bfd_endian byte_order = gdbarch_byte_order(arch);
154 	gdb_byte buf[sizeof(ULONGEST)];
155 	int ptrsz;
156 
157 	ptrsz = gdbarch_ptr_bit(arch) / 8;
158 
159 	if (target_read_memory(address, buf, ptrsz) != 0)
160 		return (0);
161 	return (extract_unsigned_integer(buf, ptrsz, byte_order));
162 }
163 
164 /*
165  * Try to find this kld in the kernel linker's list of linker files.
166  */
167 static int
168 find_kld_address (char *arg, CORE_ADDR *address)
169 {
170 	CORE_ADDR kld;
171 	char *kld_filename;
172 	char *filename;
173 	int error;
174 
175 	if (linker_files_addr == 0 || off_address == 0 || off_filename == 0 ||
176 	    off_next == 0)
177 		return (0);
178 
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);
185 		if (error)
186 			continue;
187 
188 		/* Compare this kld's filename against our passed in name. */
189 		if (strcmp(kld_filename, filename) != 0) {
190 			xfree(kld_filename);
191 			continue;
192 		}
193 		xfree(kld_filename);
194 
195 		/*
196 		 * We found a match, use its address as the base
197 		 * address if we can read it.
198 		 */
199 		*address = read_pointer(kld + off_address);
200 		if (*address == 0)
201 			return (0);
202 		return (1);
203 	}
204 	return (0);
205 }
206 
207 static void
208 load_kld (char *path, CORE_ADDR base_addr, int from_tty)
209 {
210 	struct section_addr_info *sap;
211 	struct target_section *sections = NULL, *sections_end = NULL, *s;
212 	struct cleanup *cleanup;
213 	bfd *bfd;
214 	int i;
215 
216 	/* Open the kld. */
217 	bfd = bfd_openr(path, gnutarget);
218 	if (bfd == NULL)
219 		error("\"%s\": can't open: %s", path,
220 		    bfd_errmsg(bfd_get_error()));
221 	cleanup = make_cleanup_bfd_close(bfd);
222 
223 	if (!bfd_check_format(bfd, bfd_object))
224 		error("\%s\": not an object file", path);
225 
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);
229 
230 	/* Build a section table from the bfd and relocate the sections. */
231 	if (build_section_table (bfd, &sections, &sections_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;
237 	}
238 
239 	/* Build a section addr info to pass to symbol_file_add(). */
240 	sap = build_section_addr_info_from_section_table (sections,
241 	    sections_end);
242 	cleanup = make_cleanup((make_cleanup_ftype *)free_section_addr_info,
243 	    sap);
244 
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));
249 
250 	if (from_tty && (!query("%s", "")))
251 		error("Not confirmed.");
252 
253 	symbol_file_add(path, from_tty ? SYMFILE_VERBOSE : 0, sap, OBJF_USERLOADED);
254 
255 	do_cleanups(cleanup);
256 }
257 
258 static void
259 kgdb_add_kld_cmd (char *arg, int from_tty)
260 {
261 	char path[PATH_MAX];
262 	CORE_ADDR base_addr;
263 
264 	if (!exec_bfd)
265 		error("No kernel symbol file");
266 
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))) {
270 
271 		/*
272 		 * If that didn't work, look in the various possible
273 		 * paths for the module.
274 		 */
275 		if (!find_kld_path(arg, path, sizeof(path))) {
276 			error("Unable to locate kld");
277 			return;
278 		}
279 	}
280 
281 	if (!find_kld_address(arg, &base_addr)) {
282 		error("Unable to find kld in kernel");
283 		return;
284 	}
285 
286 	load_kld(path, base_addr, from_tty);
287 
288 	reinit_frame_cache();
289 }
290 
291 static void
292 kld_relocate_section_addresses (struct so_list *so, struct target_section *sec)
293 {
294 
295 	sec->addr += so->lm_info->base_address;
296 	sec->endaddr += so->lm_info->base_address;
297 }
298 
299 static void
300 kld_free_so (struct so_list *so)
301 {
302 
303 	xfree(so->lm_info);
304 }
305 
306 static void
307 kld_clear_solib (void)
308 {
309 }
310 
311 static void
312 kld_solib_create_inferior_hook (void)
313 {
314 }
315 
316 static void
317 kld_special_symbol_handling (void)
318 {
319 }
320 
321 static struct so_list *
322 kld_current_sos (void)
323 {
324 	struct so_list *head, **prev, *new;
325 	CORE_ADDR kld, kernel;
326 	char *path;
327 	int error;
328 
329 	if (linker_files_addr == 0 || kernel_file_addr == 0 ||
330 	    off_address == 0 || off_filename == 0 || off_next == 0)
331 		return (NULL);
332 
333 	head = NULL;
334 	prev = &head;
335 
336 	/*
337 	 * Walk the list of linker files creating so_list entries for
338 	 * each non-kernel file.
339 	 */
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. */
344 		if (kld == kernel)
345 			continue;
346 
347 		new = xmalloc(sizeof(*new));
348 		memset(new, 0, sizeof(*new));
349 
350 		new->lm_info = xmalloc(sizeof(*new->lm_info));
351 		new->lm_info->base_address = 0;
352 
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);
356 		if (error != 0) {
357 			warning("kld_current_sos: Can't read filename: %s\n",
358 			    safe_strerror(error));
359 			free_so(new);
360 			continue;
361 		}
362 		strlcpy(new->so_original_name, path,
363 		    sizeof(new->so_original_name));
364 		xfree(path);
365 
366 		/*
367 		 * Try to read the pathname (if it exists) and store
368 		 * it in so_name.
369 		 */
370 		if (off_pathname != 0) {
371 			target_read_string(read_pointer(kld + off_pathname),
372 			    &path, sizeof(new->so_name), &error);
373 			if (error != 0) {
374 				warning(
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));
380 			} else {
381 				strlcpy(new->so_name, path,
382 				    sizeof(new->so_name));
383 				xfree(path);
384 			}
385 		} else
386 			strlcpy(new->so_name, new->so_original_name,
387 			    sizeof(new->so_name));
388 
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) {
392 			warning(
393 			    "kld_current_sos: Invalid address for kld \"%s\"",
394 			    new->so_original_name);
395 			free_so(new);
396 			continue;
397 		}
398 
399 		/* Append to the list. */
400 		*prev = new;
401 		prev = &new->next;
402 	}
403 
404 	return (head);
405 }
406 
407 static int
408 kld_open_symbol_file_object (void *from_ttyp)
409 {
410 
411 	return (0);
412 }
413 
414 static int
415 kld_in_dynsym_resolve_code (CORE_ADDR pc)
416 {
417 
418 	return (0);
419 }
420 
421 static int
422 kld_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname)
423 {
424 	char path[PATH_MAX];
425 	int fd;
426 
427 	*temp_pathname = NULL;
428 	if (!find_kld_path(solib, path, sizeof(path))) {
429 		errno = ENOENT;
430 		return (-1);
431 	}
432 	fd = open(path, o_flags, 0);
433 	if (fd >= 0)
434 		*temp_pathname = xstrdup(path);
435 	return (fd);
436 }
437 
438 void
439 kld_new_objfile (struct objfile *objfile)
440 {
441 
442 	if (!have_partial_symbols())
443 		return;
444 
445 	/*
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.
450 	 */
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");
458 }
459 
460 static int
461 load_klds_stub (void *arg)
462 {
463 
464 	solib_add(NULL, 1, &current_target, auto_solib_add);
465 	return (0);
466 }
467 
468 void
469 kld_init (void)
470 {
471 	/* XXX hack, needs to go into an abi init function */
472 	set_solib_ops(get_current_arch(), &kld_so_ops);
473 
474 	kld_new_objfile(NULL);
475 	catch_errors(load_klds_stub, NULL, NULL, RETURN_MASK_ALL);
476 	observer_attach_new_objfile(kld_new_objfile);
477 }
478 
479 void
480 initialize_kld_target(void)
481 {
482 	struct cmd_list_element *c;
483 
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;
494 
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);
499 }
500