dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / mdb / common / kmdb / kmdb_module_load.c
blob38ba3ffe486d3e0c182ee839ae57520786956a77
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/param.h>
30 #include <sys/modctl.h>
31 #include <sys/kobj.h>
32 #include <sys/kobj_impl.h>
33 #include <unistd.h>
34 #include <strings.h>
35 #include <dlfcn.h>
36 #include <link.h>
38 #include <kmdb/kmdb_module.h>
39 #include <kmdb/kmdb_wr_impl.h>
40 #include <kmdb/kmdb_kdi.h>
41 #include <mdb/mdb_modapi.h>
42 #include <mdb/mdb_debug.h>
43 #include <mdb/mdb_string.h>
44 #include <mdb/mdb_ctf.h>
45 #include <mdb/mdb_err.h>
46 #include <mdb/mdb_io.h>
47 #include <mdb/mdb_frame.h>
48 #include <mdb/mdb.h>
50 static void kmdb_module_request_unload(kmdb_modctl_t *, const char *, int);
52 static void
53 kmc_free(kmdb_modctl_t *kmc)
55 if (kmc->kmc_modname != NULL)
56 strfree(kmc->kmc_modname);
57 mdb_free(kmc, sizeof (kmdb_modctl_t));
61 * Sends a request to the driver to load the module. If/when the load has
62 * completed successfully, kmdb_module_loaded is called.
64 int
65 mdb_module_load(const char *fname, int mode)
67 const char *modname = strbasename(fname);
68 kmdb_wr_load_t *dlr;
69 kmdb_modctl_t *kmc = NULL;
70 const char *wformat = NULL;
71 mdb_var_t *v;
73 if (!mdb_module_validate_name(modname, &wformat))
74 goto module_load_err;
76 if ((v = mdb_nv_lookup(&mdb.m_dmodctl, modname)) != NULL) {
77 kmc = MDB_NV_COOKIE(v);
79 if (kmc->kmc_state == KMDB_MC_STATE_LOADING)
80 wformat = "module %s is already being loaded\n";
81 else
82 wformat = "module %s is being unloaded\n";
83 goto module_load_err;
86 kmc = mdb_zalloc(sizeof (kmdb_modctl_t), UM_SLEEP);
87 kmc->kmc_loadmode = mode;
88 kmc->kmc_modname = strdup(modname);
89 kmc->kmc_state = KMDB_MC_STATE_LOADING;
91 if (mdb_nv_insert(&mdb.m_dmodctl, modname, NULL, (uintptr_t)kmc, 0) ==
92 NULL) {
93 wformat = "module %s can't be registered for load\n";
94 kmc_free(kmc);
95 goto module_load_err;
98 dlr = mdb_zalloc(sizeof (kmdb_wr_load_t), UM_SLEEP);
99 dlr->dlr_node.wn_task = WNTASK_DMOD_LOAD;
100 dlr->dlr_fname = strdup(fname);
102 kmdb_wr_driver_notify(dlr);
104 if (!(mode & MDB_MOD_DEFER) &&
105 mdb_tgt_continue(mdb.m_target, NULL) == 0)
106 return (0);
108 if (!(mode & MDB_MOD_SILENT))
109 mdb_printf("%s load pending (:c to complete)\n", modname);
111 return (0);
113 module_load_err:
114 if (!(mode & MDB_MOD_SILENT))
115 warn(wformat, modname);
117 return (-1);
121 kmdb_module_loaded(kmdb_wr_load_t *dlr)
123 struct modctl *modp = dlr->dlr_modctl;
124 const char *modname = strbasename(dlr->dlr_fname);
125 struct module *mp;
126 kmdb_modctl_t *kmc;
127 mdb_var_t *v;
129 v = mdb_nv_lookup(&mdb.m_dmodctl, modname);
131 if (dlr->dlr_errno != 0) {
133 * We're somewhat limited in the diagnostics that we can
134 * provide in the event of a failed load. In most load-failure
135 * cases, the driver can only send up a generic errno. We use
136 * EMDB_ENOMOD to signal generic errors, and supply our own
137 * message. This twists the meaning of EMDB_NOMOD somewhat, but
138 * it's better than defining a new one.
140 if (dlr->dlr_errno == EMDB_NOMOD) {
141 mdb_warn("%s does not appear to be a kmdb dmod\n",
142 modname);
143 } else {
144 (void) set_errno(dlr->dlr_errno);
145 mdb_warn("dmod %s failed to load", modname);
148 if (v != NULL)
149 mdb_nv_remove(&mdb.m_dmodctl, v);
150 return (0);
153 if ((mp = modp->mod_mp) == NULL || mp->symhdr == NULL ||
154 mp->strhdr == NULL || mp->symtbl == NULL || mp->strings == NULL) {
155 mdb_warn("dmod %s did not load properly\n");
156 goto module_loaded_err;
159 if ((v = mdb_nv_lookup(&mdb.m_dmodctl, modname)) == NULL) {
160 kmc = mdb_zalloc(sizeof (kmdb_modctl_t), UM_SLEEP);
161 kmc->kmc_loadmode = MDB_MOD_LOCAL;
162 kmc->kmc_modname = strdup(modname);
163 kmc->kmc_state = KMDB_MC_STATE_LOADING;
165 (void) mdb_nv_insert(&mdb.m_dmodctl, modname, NULL,
166 (uintptr_t)kmc, 0);
167 } else {
168 kmc = MDB_NV_COOKIE(v);
169 ASSERT(kmc->kmc_symtab == NULL);
172 kmc->kmc_modctl = modp;
173 kmc->kmc_exported = (mp->flags & KOBJ_EXPORTED) != 0;
174 mdb_gelf_ehdr_to_gehdr(&mp->hdr, &kmc->kmc_ehdr);
176 kmc->kmc_symtab = mdb_gelf_symtab_create_raw(&kmc->kmc_ehdr, mp->symhdr,
177 mp->symtbl, mp->strhdr, mp->strings,
178 MDB_TGT_SYMTAB);
180 if (mp->flags & KOBJ_PRIM)
181 kmc->kmc_flags |= KMDB_MC_FL_NOUNLOAD;
183 if (mdb_module_create(modname, modp->mod_filename,
184 kmc->kmc_loadmode, &kmc->kmc_mod) < 0)
185 goto module_loaded_err;
187 kmc->kmc_state = KMDB_MC_STATE_LOADED;
189 return (1);
191 module_loaded_err:
192 if (kmc->kmc_symtab != NULL)
193 mdb_gelf_symtab_destroy(kmc->kmc_symtab);
195 kmdb_module_request_unload(kmc, kmc->kmc_modname, MDB_MOD_DEFER);
196 return (0);
199 void
200 kmdb_module_load_ack(kmdb_wr_load_t *dlr)
202 strfree(dlr->dlr_fname);
203 mdb_free(dlr, sizeof (kmdb_wr_load_t));
206 void
207 mdb_module_load_all(int mode)
209 kmdb_wr_t *wn;
211 ASSERT(mode & MDB_MOD_DEFER);
213 wn = mdb_zalloc(sizeof (kmdb_wr_t), UM_SLEEP);
214 wn->wn_task = WNTASK_DMOD_LOAD_ALL;
216 kmdb_wr_driver_notify(wn);
219 void
220 kmdb_module_load_all_ack(kmdb_wr_t *wn)
222 mdb_free(wn, sizeof (kmdb_wr_t));
225 static void
226 kmdb_module_request_unload(kmdb_modctl_t *kmc, const char *modname, int mode)
228 kmdb_wr_unload_t *dur = mdb_zalloc(sizeof (kmdb_wr_unload_t), UM_SLEEP);
229 dur->dur_node.wn_task = WNTASK_DMOD_UNLOAD;
230 dur->dur_modname = strdup(modname);
231 dur->dur_modctl = kmc->kmc_modctl;
233 kmdb_wr_driver_notify(dur);
235 kmc->kmc_state = KMDB_MC_STATE_UNLOADING;
237 if (!(mode & MDB_MOD_DEFER) &&
238 mdb_tgt_continue(mdb.m_target, NULL) == 0)
239 return;
241 if (!(mode & MDB_MOD_SILENT))
242 mdb_printf("%s unload pending (:c to complete)\n", modname);
245 /*ARGSUSED*/
247 mdb_module_unload(const char *name, int mode)
249 kmdb_modctl_t *kmc = NULL;
250 const char *basename;
251 mdb_var_t *v;
254 * We may have been called with the name from the module itself
255 * if the caller is iterating through the module list, so we need
256 * to make a copy of the name. If we don't, we can't use it after
257 * the call to unload_common(), which frees the module.
259 name = strdup(name);
260 basename = strbasename(name);
263 * Make sure the module is in the proper state for unloading. Modules
264 * may only be unloaded if they have properly completed loading.
266 if ((v = mdb_nv_lookup(&mdb.m_dmodctl, basename)) != NULL) {
267 kmc = MDB_NV_COOKIE(v);
268 switch (kmc->kmc_state) {
269 case KMDB_MC_STATE_LOADING:
270 warn("%s is in the process of loading\n", basename);
271 return (set_errno(EMDB_NOMOD));
272 case KMDB_MC_STATE_UNLOADING:
273 warn("%s is already being unloaded\n", basename);
274 return (set_errno(EMDB_NOMOD));
275 default:
276 ASSERT(kmc->kmc_state == KMDB_MC_STATE_LOADED);
279 if (kmc->kmc_flags & KMDB_MC_FL_NOUNLOAD)
280 return (set_errno(EMDB_KMODNOUNLOAD));
283 if (mdb_module_unload_common(name) < 0) {
284 if (!(mode & MDB_MOD_SILENT)) {
285 mdb_dprintf(MDB_DBG_MODULE, "unload of %s failed\n",
286 name);
288 return (-1); /* errno is set for us */
292 * Any modules legitimately not listed in dmodctl (builtins, for
293 * example) will be handled by mdb_module_unload_common. If any of
294 * them get here, we've got a problem.
296 if (v == NULL) {
297 warn("unload of unregistered module %s\n", basename);
298 return (set_errno(EMDB_NOMOD));
301 ASSERT(kmc->kmc_dlrefcnt == 0);
303 mdb_gelf_symtab_destroy(kmc->kmc_symtab);
305 kmdb_module_request_unload(kmc, basename, mode);
306 return (0);
310 kmdb_module_unloaded(kmdb_wr_unload_t *dur)
312 mdb_var_t *v;
314 if ((v = mdb_nv_lookup(&mdb.m_dmodctl, dur->dur_modname)) == NULL) {
315 mdb_warn("unload for unrequested module %s\n",
316 dur->dur_modname);
317 return (0);
320 if (dur->dur_errno != 0) {
321 mdb_warn("dmod %s failed to unload", dur->dur_modname);
322 return (0);
325 kmc_free(MDB_NV_COOKIE(v));
326 mdb_nv_remove(&mdb.m_dmodctl, v);
328 return (1);
331 void
332 kmdb_module_unload_ack(kmdb_wr_unload_t *dur)
334 if (dur->dur_modname != NULL)
335 strfree(dur->dur_modname);
336 mdb_free(dur, sizeof (kmdb_wr_unload_t));
340 * Called by the kmdb_kvm target upon debugger reentry, this routine checks
341 * to see if the loaded dmods have changed. Of particular interest is the
342 * exportation of dmod symbol tables, which will happen during the boot
343 * process for dmods that were loaded prior to kernel startup. If this
344 * has occurred, we'll need to reconstruct our view of the symbol tables for
345 * the affected dmods, since the old symbol tables lived in bootmem
346 * and have been moved during the kobj_export_module().
348 * Also, any ctf_file_t we might have opened is now invalid, since it
349 * has internal pointers to the old data as well.
351 void
352 kmdb_module_sync(void)
354 mdb_var_t *v;
356 mdb_nv_rewind(&mdb.m_dmodctl);
357 while ((v = mdb_nv_advance(&mdb.m_dmodctl)) != NULL) {
358 kmdb_modctl_t *kmc = MDB_NV_COOKIE(v);
359 struct module *mp;
361 if (kmc->kmc_state != KMDB_MC_STATE_LOADED)
362 continue;
364 mp = kmc->kmc_modctl->mod_mp;
366 if ((mp->flags & (KOBJ_PRIM | KOBJ_EXPORTED)) &&
367 !kmc->kmc_exported) {
369 * The exporting process moves the symtab from boot
370 * scratch memory to vmem.
372 if (kmc->kmc_symtab != NULL)
373 mdb_gelf_symtab_destroy(kmc->kmc_symtab);
375 kmc->kmc_symtab = mdb_gelf_symtab_create_raw(
376 &kmc->kmc_ehdr, mp->symhdr, mp->symtbl, mp->strhdr,
377 mp->strings, MDB_TGT_SYMTAB);
379 if (kmc->kmc_mod->mod_ctfp != NULL) {
380 ctf_close(kmc->kmc_mod->mod_ctfp);
381 kmc->kmc_mod->mod_ctfp =
382 mdb_ctf_open(kmc->kmc_modname, NULL);
384 kmc->kmc_exported = TRUE;