kld.c revision 178634
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 178634 2008-04-28 15:26:11Z 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};
199
200static void
201add_section (bfd *bfd, asection *sect, void *arg)
202{
203	struct add_section_info *asi = arg;
204	CORE_ADDR address;
205	char *name;
206
207	/* Ignore non-resident sections. */
208	if ((bfd_get_section_flags(bfd, sect) & (SEC_ALLOC | SEC_LOAD)) == 0)
209		return;
210
211	name = xstrdup(bfd_get_section_name(bfd, sect));
212	make_cleanup(xfree, name);
213	address = asi->base_addr + bfd_get_section_vma(bfd, sect);
214	asi->section_addrs->other[asi->sect_index].name = name;
215	asi->section_addrs->other[asi->sect_index].addr = address;
216	asi->section_addrs->other[asi->sect_index].sectindex = sect->index;
217	printf_unfiltered("\t%s_addr = %s\n", name, local_hex_string(address));
218	asi->sect_index++;
219}
220
221static void
222load_kld (char *path, CORE_ADDR base_addr, int from_tty)
223{
224	struct add_section_info asi;
225	struct cleanup *cleanup;
226	bfd *bfd;
227
228	/* Open the kld. */
229	bfd = bfd_openr(path, gnutarget);
230	if (bfd == NULL)
231		error("\"%s\": can't open: %s", path,
232		    bfd_errmsg(bfd_get_error()));
233	cleanup = make_cleanup_bfd_close(bfd);
234
235	if (!bfd_check_format(bfd, bfd_object))
236		error("\%s\": not an object file", path);
237
238	/* Make sure we have a .text section. */
239	if (bfd_get_section_by_name (bfd, ".text") == NULL)
240		error("\"%s\": can't find text section", path);
241
242	printf_unfiltered("add symbol table from file \"%s\" at\n", path);
243
244	/* Build a section table for symbol_file_add() from the bfd sections. */
245	asi.section_addrs = alloc_section_addr_info(bfd_count_sections(bfd));
246	cleanup = make_cleanup(xfree, asi.section_addrs);
247	asi.sect_index = 0;
248	asi.base_addr = base_addr;
249	bfd_map_over_sections(bfd, add_section, &asi);
250
251	if (from_tty && (!query("%s", "")))
252		error("Not confirmed.");
253
254	symbol_file_add(path, from_tty, asi.section_addrs, 0, OBJF_USERLOADED);
255
256	do_cleanups(cleanup);
257}
258
259void
260kgdb_add_kld_cmd (char *arg, int from_tty)
261{
262	char path[PATH_MAX];
263	CORE_ADDR base_addr;
264
265	/* Try to open the raw path to handle absolute paths first. */
266	snprintf(path, sizeof(path), "%s", arg);
267	if (!check_kld_path(path, sizeof(path))) {
268
269		/*
270		 * If that didn't work, look in the various possible
271		 * paths for the module.
272		 */
273		if (!find_kld_path(arg, path, sizeof(path))) {
274			error("Unable to locate kld");
275			return;
276		}
277	}
278
279	if (!find_kld_address(arg, &base_addr)) {
280		error("Unable to find kld in kernel");
281		return;
282	}
283
284	load_kld(path, base_addr, from_tty);
285
286	reinit_frame_cache();
287}
288
289static void
290kld_relocate_section_addresses (struct so_list *so, struct section_table *sec)
291{
292
293	sec->addr += so->lm_info->base_address;
294	sec->endaddr += so->lm_info->base_address;
295}
296
297static void
298kld_free_so (struct so_list *so)
299{
300
301	xfree(so->lm_info);
302}
303
304static void
305kld_clear_solib (void)
306{
307}
308
309static void
310kld_solib_create_inferior_hook (void)
311{
312}
313
314static void
315kld_special_symbol_handling (void)
316{
317}
318
319static struct so_list *
320kld_current_sos (void)
321{
322	struct so_list *head, **prev, *new;
323	CORE_ADDR kld, kernel;
324	char *path;
325	int error;
326
327	head = NULL;
328	prev = &head;
329
330	/*
331	 * Walk the list of linker files creating so_list entries for
332	 * each non-kernel file.
333	 */
334	kernel = kgdb_parse("linker_kernel_file");
335	for (kld = kgdb_parse("linker_files.tqh_first"); kld != 0;
336	     kld = read_pointer(kld + off_next)) {
337		/* Skip the main kernel file. */
338		if (kld == kernel)
339			continue;
340
341		new = xmalloc(sizeof(*new));
342		memset(new, 0, sizeof(*new));
343
344		new->lm_info = xmalloc(sizeof(*new->lm_info));
345		new->lm_info->base_address = 0;
346
347		/* Read the base filename and store it in so_original_name. */
348		target_read_string(read_pointer(kld + off_filename),
349		    &path, sizeof(new->so_original_name), &error);
350		if (error != 0) {
351			warning("kld_current_sos: Can't read filename: %s\n",
352			    safe_strerror(error));
353			free_so(new);
354			continue;
355		}
356		strlcpy(new->so_original_name, path,
357		    sizeof(new->so_original_name));
358		xfree(path);
359
360		/*
361		 * Try to read the pathname (if it exists) and store
362		 * it in so_name.
363		 */
364		if (off_pathname != 0) {
365			target_read_string(read_pointer(kld + off_pathname),
366			    &path, sizeof(new->so_name), &error);
367			if (error != 0) {
368				warning(
369		    "kld_current_sos: Can't read pathname for \"%s\": %s\n",
370				    new->so_original_name,
371				    safe_strerror(error));
372				strlcpy(new->so_name, new->so_original_name,
373				    sizeof(new->so_name));
374			} else {
375				strlcpy(new->so_name, path,
376				    sizeof(new->so_name));
377				xfree(path);
378			}
379		} else
380			strlcpy(new->so_name, new->so_original_name,
381			    sizeof(new->so_name));
382
383		/* Read this kld's base address. */
384		new->lm_info->base_address = read_pointer(kld + off_address);
385		if (new->lm_info->base_address == 0) {
386			warning(
387			    "kld_current_sos: Invalid address for kld \"%s\"",
388			    new->so_original_name);
389			free_so(new);
390			continue;
391		}
392
393		/* Append to the list. */
394		*prev = new;
395		prev = &new->next;
396	}
397
398	return (head);
399}
400
401static int
402kld_open_symbol_file_object (void *from_ttyp)
403{
404
405	return (0);
406}
407
408static int
409kld_in_dynsym_resolve_code (CORE_ADDR pc)
410{
411
412	return (0);
413}
414
415static int
416kld_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname)
417{
418	char path[PATH_MAX];
419	int fd;
420
421	*temp_pathname = NULL;
422	if (!find_kld_path(solib, path, sizeof(path))) {
423		errno = ENOENT;
424		return (-1);
425	}
426	fd = open(path, o_flags, 0);
427	if (fd >= 0)
428		*temp_pathname = xstrdup(path);
429	return (fd);
430}
431
432static int
433load_klds_stub (void *arg)
434{
435
436	SOLIB_ADD(NULL, 1, &current_target, auto_solib_add);
437	return (0);
438}
439
440void
441kgdb_kld_init (void)
442{
443	struct cmd_list_element *c;
444
445	/* Compute offsets of relevant members in struct linker_file. */
446	off_address = kgdb_parse("&((struct linker_file *)0)->address");
447	off_filename = kgdb_parse("&((struct linker_file *)0)->filename");
448	off_pathname = kgdb_parse("&((struct linker_file *)0)->pathname");
449	off_next = kgdb_parse("&((struct linker_file *)0)->link.tqe_next");
450	if (off_address == 0 || off_filename == 0 || off_next == 0)
451		return;
452
453	module_path_addr = kgdb_parse("linker_path");
454
455	kld_so_ops.relocate_section_addresses = kld_relocate_section_addresses;
456	kld_so_ops.free_so = kld_free_so;
457	kld_so_ops.clear_solib = kld_clear_solib;
458	kld_so_ops.solib_create_inferior_hook = kld_solib_create_inferior_hook;
459	kld_so_ops.special_symbol_handling = kld_special_symbol_handling;
460	kld_so_ops.current_sos = kld_current_sos;
461	kld_so_ops.open_symbol_file_object = kld_open_symbol_file_object;
462	kld_so_ops.in_dynsym_resolve_code = kld_in_dynsym_resolve_code;
463	kld_so_ops.find_and_open_solib = kld_find_and_open_solib;
464
465	current_target_so_ops = &kld_so_ops;
466
467	catch_errors(load_klds_stub, NULL, NULL, RETURN_MASK_ALL);
468
469	c = add_com("add-kld", class_files, kgdb_add_kld_cmd,
470	   "Usage: add-kld FILE\n\
471Load the symbols from the kernel loadable module FILE.");
472	set_cmd_completer(c, filename_completer);
473}
474