1 /* Handle FreeBSD kernel modules as shared libraries.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
25 #include <sys/param.h>
26 #include <sys/queue.h>
29 #include <sys/linker.h>
32 /* XXX, kludge to avoid duplicate definitions while sys/linker.h is used. */
51 static int try_modpath (char *buf, int buflen, char *fmt, ...);
52 static char *guess_modpath (char *modname);
55 kgdb_relocate_section_addresses (struct so_list *so,
56 struct section_table *sec)
58 sec->addr += so->lm_info->address;
59 sec->endaddr += so->lm_info->address;
63 kgdb_open_symbol_file_object (void *from_ttyp)
65 warning ("kgdb_open_symbol_file_object called\n");
69 static struct so_list *
70 kgdb_current_sos (void)
72 linker_file_list_t linker_files;
73 struct linker_file lfile;
74 struct minimal_symbol *msymbol;
75 struct linker_file *lfilek;
76 struct so_list *head = NULL;
77 struct so_list **link_ptr = &head;
79 CORE_ADDR lfiles_addr;
81 msymbol = lookup_minimal_symbol ("linker_files", NULL, symfile_objfile);
82 if (msymbol == NULL || SYMBOL_VALUE_ADDRESS (msymbol) == 0)
84 warning ("failed to find linker_files symbol\n");
87 lfiles_addr = SYMBOL_VALUE_ADDRESS (msymbol);
88 if (target_read_memory (lfiles_addr, (char *)&linker_files,
89 sizeof (linker_files)))
91 warning ("failed to read linker_files data\n");
94 for (lfilek = TAILQ_FIRST (&linker_files); lfilek != NULL;
95 lfilek = TAILQ_NEXT (&lfile, link))
98 struct cleanup *old_chain;
102 if (target_read_memory ((CORE_ADDR) lfilek, (char *) &lfile,
105 warning ("failed to read linker file data at %p\n", lfilek);
108 target_read_string ((CORE_ADDR) lfile.filename, &buf,
109 SO_NAME_MAX_PATH_SIZE - 1, &errcode);
112 warning ("cannot read linker file pathname: %s\n",
113 safe_strerror (errcode));
116 if (strlen (buf) < 3 || strcmp (&buf[strlen (buf) - 3], ".ko") != 0)
122 new = (struct so_list *) xmalloc (sizeof (struct so_list));
123 old_chain = make_cleanup (xfree, new);
125 memset (new, 0, sizeof (*new));
127 new->lm_info = xmalloc (sizeof (struct lm_info));
128 make_cleanup (xfree, new->lm_info);
130 new->lm_info->address = (CORE_ADDR) lfile.address;
132 strncpy (new->so_original_name, buf, SO_NAME_MAX_PATH_SIZE - 1);
133 new->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
135 snprintf (new->so_name, SO_NAME_MAX_PATH_SIZE, "%s",
136 guess_modpath (new->so_original_name));
140 link_ptr = &new->next;
142 discard_cleanups (old_chain);
148 kgdb_in_dynsym_resolve_code (CORE_ADDR pc)
150 warning ("kgdb_in_dynsym_resolve_code called\n");
155 kgdb_special_symbol_handling (void)
160 kgdb_solib_create_inferior_hook (void)
162 struct so_list *inferior_sos;
164 inferior_sos = kgdb_current_sos ();
167 solib_add (NULL, /*from_tty*/0, NULL, auto_solib_add);
172 kgdb_clear_solib (void)
177 kgdb_free_so (struct so_list *so)
183 try_modpath (char *buf, int buflen, char *fmt, ...)
189 vsnprintf (buf, buflen, fmt, ap);
192 return (stat (buf, &sb) == 0);
196 guess_modpath (char *modname)
198 static char buf[2048], moddir[128], syspath[1024];
199 struct minimal_symbol *msymbol;
200 char *kernpath, *objpath, *p, *version;
201 int errcode, n, syspathlen;
203 /* Set default module location */
204 snprintf (buf, sizeof (buf), "/boot/kernel/%s", modname);
206 /* Guess at the subdirectory off sys/modules. XXX, only sometimes correct */
207 n = strlen (modname);
208 if (n > 3 && strcmp (&modname[n - 3], ".ko") == 0)
210 snprintf (moddir, sizeof (moddir), "%.*s", n, modname);
212 /* Try to locate the kernel compile location from version[] */
213 msymbol = lookup_minimal_symbol ("version", NULL, symfile_objfile);
214 if (msymbol == NULL || SYMBOL_VALUE_ADDRESS (msymbol) == 0)
216 warning("cannot find `version' symbol; using default module path\n");
219 target_read_string (SYMBOL_VALUE_ADDRESS (msymbol), &version, 2048, &errcode);
222 warning ("cannot read `version' string; using default module path: %s\n",
223 safe_strerror (errcode));
227 /* Find the kernel build path after user@host: on the second line. */
228 if ((p = strchr (version, '\n')) == NULL ||
229 (kernpath = strchr (p, ':')) == NULL ||
230 (p = strchr (kernpath, '\n')) == NULL)
232 warning ("cannot parse version[]; using default module path\n");
240 * Find the absolute path to src/sys by skipping back over path
241 * components until we find a "/sys/".
244 while (p > kernpath && syspathlen == 0)
246 while (p > kernpath && *p != '/')
248 if (strncmp (p, "/sys/", 5) == 0)
249 syspathlen = p - kernpath + 4;
250 else if (p > kernpath)
255 warning ("cannot find /sys/ in `%s'; using default module path\n",
261 * For kernels compiled with buildkernel, the object path will have
262 * been prepended to the /sys/ path in `kernpath'.
264 objpath = getenv ("MAKEOBJDIRPREFIX");
266 objpath = "/usr/obj";
267 n = strlen (objpath);
268 if (syspathlen > n + 1 && strncmp (kernpath, objpath, n) == 0 &&
270 snprintf (syspath, sizeof (syspath), "%.*s", syspathlen - n, kernpath + n);
272 snprintf (syspath, sizeof (syspath), "%.*s", syspathlen, kernpath);
274 /* Now try to find the module file */
275 if (!try_modpath (buf, sizeof (buf), "./%s.debug", modname) &&
276 !try_modpath (buf, sizeof (buf), "./%s", modname) && !try_modpath (buf,
277 sizeof (buf), "%s/modules%s/modules/%s/%s.debug", kernpath, syspath,
278 moddir, modname) && !try_modpath (buf, sizeof (buf),
279 "%s/modules%s/modules/%s/%s", kernpath, syspath, moddir, modname) &&
280 !try_modpath (buf, sizeof (buf), "/boot/kernel/%s.debug", modname) &&
281 !try_modpath (buf, sizeof (buf), "/boot/kernel/%s", modname))
283 warning ("cannot find file for module %s\n", modname);
284 snprintf (buf, sizeof (buf), "%s", modname);
291 struct target_so_ops kgdb_so_ops;
294 _initialize_kgdb_solib (void)
296 kgdb_so_ops.relocate_section_addresses = kgdb_relocate_section_addresses;
297 kgdb_so_ops.free_so = kgdb_free_so;
298 kgdb_so_ops.clear_solib = kgdb_clear_solib;
299 kgdb_so_ops.solib_create_inferior_hook = kgdb_solib_create_inferior_hook;
300 kgdb_so_ops.special_symbol_handling = kgdb_special_symbol_handling;
301 kgdb_so_ops.current_sos = kgdb_current_sos;
302 kgdb_so_ops.open_symbol_file_object = kgdb_open_symbol_file_object;
303 kgdb_so_ops.in_dynsym_resolve_code = kgdb_in_dynsym_resolve_code;