17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
235ae68c69Sjohnlev  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
297c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
307c478bd9Sstevel@tonic-gate #include <sys/kobj_impl.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <strings.h>
337c478bd9Sstevel@tonic-gate #include <dlfcn.h>
347c478bd9Sstevel@tonic-gate #include <link.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_module.h>
377c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_wr_impl.h>
387c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_kdi.h>
397c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
407c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
417c478bd9Sstevel@tonic-gate #include <mdb/mdb_string.h>
425ae68c69Sjohnlev #include <mdb/mdb_ctf.h>
437c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
447c478bd9Sstevel@tonic-gate #include <mdb/mdb_io.h>
457c478bd9Sstevel@tonic-gate #include <mdb/mdb_frame.h>
467c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static void kmdb_module_request_unload(kmdb_modctl_t *, const char *, int);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static void
kmc_free(kmdb_modctl_t * kmc)517c478bd9Sstevel@tonic-gate kmc_free(kmdb_modctl_t *kmc)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	if (kmc->kmc_modname != NULL)
547c478bd9Sstevel@tonic-gate 		strfree(kmc->kmc_modname);
557c478bd9Sstevel@tonic-gate 	mdb_free(kmc, sizeof (kmdb_modctl_t));
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * Sends a request to the driver to load the module.  If/when the load has
607c478bd9Sstevel@tonic-gate  * completed successfully, kmdb_module_loaded is called.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate int
mdb_module_load(const char * fname,int mode)637c478bd9Sstevel@tonic-gate mdb_module_load(const char *fname, int mode)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate 	const char *modname = strbasename(fname);
667c478bd9Sstevel@tonic-gate 	kmdb_wr_load_t *dlr;
677c478bd9Sstevel@tonic-gate 	kmdb_modctl_t *kmc = NULL;
687c478bd9Sstevel@tonic-gate 	const char *wformat = NULL;
697c478bd9Sstevel@tonic-gate 	mdb_var_t *v;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	if (!mdb_module_validate_name(modname, &wformat))
727c478bd9Sstevel@tonic-gate 		goto module_load_err;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if ((v = mdb_nv_lookup(&mdb.m_dmodctl, modname)) != NULL) {
757c478bd9Sstevel@tonic-gate 		kmc = MDB_NV_COOKIE(v);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 		if (kmc->kmc_state == KMDB_MC_STATE_LOADING)
787c478bd9Sstevel@tonic-gate 			wformat = "module %s is already being loaded\n";
797c478bd9Sstevel@tonic-gate 		else
807c478bd9Sstevel@tonic-gate 			wformat = "module %s is being unloaded\n";
817c478bd9Sstevel@tonic-gate 		goto module_load_err;
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	kmc = mdb_zalloc(sizeof (kmdb_modctl_t), UM_SLEEP);
857c478bd9Sstevel@tonic-gate 	kmc->kmc_loadmode = mode;
867c478bd9Sstevel@tonic-gate 	kmc->kmc_modname = strdup(modname);
877c478bd9Sstevel@tonic-gate 	kmc->kmc_state = KMDB_MC_STATE_LOADING;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (mdb_nv_insert(&mdb.m_dmodctl, modname, NULL, (uintptr_t)kmc, 0) ==
907c478bd9Sstevel@tonic-gate 	    NULL) {
917c478bd9Sstevel@tonic-gate 		wformat = "module %s can't be registered for load\n";
927c478bd9Sstevel@tonic-gate 		kmc_free(kmc);
937c478bd9Sstevel@tonic-gate 		goto module_load_err;
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	dlr = mdb_zalloc(sizeof (kmdb_wr_load_t), UM_SLEEP);
977c478bd9Sstevel@tonic-gate 	dlr->dlr_node.wn_task = WNTASK_DMOD_LOAD;
987c478bd9Sstevel@tonic-gate 	dlr->dlr_fname = strdup(fname);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	kmdb_wr_driver_notify(dlr);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if (!(mode & MDB_MOD_DEFER) &&
1037c478bd9Sstevel@tonic-gate 	    mdb_tgt_continue(mdb.m_target, NULL) == 0)
1047c478bd9Sstevel@tonic-gate 		return (0);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if (!(mode & MDB_MOD_SILENT))
1077c478bd9Sstevel@tonic-gate 		mdb_printf("%s load pending (:c to complete)\n", modname);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	return (0);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate module_load_err:
1127c478bd9Sstevel@tonic-gate 	if (!(mode & MDB_MOD_SILENT))
1137c478bd9Sstevel@tonic-gate 		warn(wformat, modname);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	return (-1);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate int
kmdb_module_loaded(kmdb_wr_load_t * dlr)1197c478bd9Sstevel@tonic-gate kmdb_module_loaded(kmdb_wr_load_t *dlr)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	struct modctl *modp = dlr->dlr_modctl;
1227c478bd9Sstevel@tonic-gate 	const char *modname = strbasename(dlr->dlr_fname);
1237c478bd9Sstevel@tonic-gate 	struct module *mp;
124*24537d3eSToomas Soome 	kmdb_modctl_t *kmc = NULL;
1257c478bd9Sstevel@tonic-gate 	mdb_var_t *v;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	v = mdb_nv_lookup(&mdb.m_dmodctl, modname);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if (dlr->dlr_errno != 0) {
1307c478bd9Sstevel@tonic-gate 		/*
1317c478bd9Sstevel@tonic-gate 		 * We're somewhat limited in the diagnostics that we can
1327c478bd9Sstevel@tonic-gate 		 * provide in the event of a failed load.  In most load-failure
1337c478bd9Sstevel@tonic-gate 		 * cases, the driver can only send up a generic errno.  We use
1347c478bd9Sstevel@tonic-gate 		 * EMDB_ENOMOD to signal generic errors, and supply our own
1357c478bd9Sstevel@tonic-gate 		 * message.  This twists the meaning of EMDB_NOMOD somewhat, but
1367c478bd9Sstevel@tonic-gate 		 * it's better than defining a new one.
1377c478bd9Sstevel@tonic-gate 		 */
1387c478bd9Sstevel@tonic-gate 		if (dlr->dlr_errno == EMDB_NOMOD) {
1397c478bd9Sstevel@tonic-gate 			mdb_warn("%s does not appear to be a kmdb dmod\n",
1407c478bd9Sstevel@tonic-gate 			    modname);
1417c478bd9Sstevel@tonic-gate 		} else {
1427c478bd9Sstevel@tonic-gate 			(void) set_errno(dlr->dlr_errno);
1437c478bd9Sstevel@tonic-gate 			mdb_warn("dmod %s failed to load", modname);
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 		if (v != NULL)
1477c478bd9Sstevel@tonic-gate 			mdb_nv_remove(&mdb.m_dmodctl, v);
1487c478bd9Sstevel@tonic-gate 		return (0);
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if ((mp = modp->mod_mp) == NULL || mp->symhdr == NULL ||
1527c478bd9Sstevel@tonic-gate 	    mp->strhdr == NULL || mp->symtbl == NULL || mp->strings == NULL) {
1537c478bd9Sstevel@tonic-gate 		mdb_warn("dmod %s did not load properly\n");
1547c478bd9Sstevel@tonic-gate 		goto module_loaded_err;
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	if ((v = mdb_nv_lookup(&mdb.m_dmodctl, modname)) == NULL) {
1587c478bd9Sstevel@tonic-gate 		kmc = mdb_zalloc(sizeof (kmdb_modctl_t), UM_SLEEP);
1597c478bd9Sstevel@tonic-gate 		kmc->kmc_loadmode = MDB_MOD_LOCAL;
1607c478bd9Sstevel@tonic-gate 		kmc->kmc_modname = strdup(modname);
1617c478bd9Sstevel@tonic-gate 		kmc->kmc_state = KMDB_MC_STATE_LOADING;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 		(void) mdb_nv_insert(&mdb.m_dmodctl, modname, NULL,
1647c478bd9Sstevel@tonic-gate 		    (uintptr_t)kmc, 0);
1657c478bd9Sstevel@tonic-gate 	} else {
1667c478bd9Sstevel@tonic-gate 		kmc = MDB_NV_COOKIE(v);
1677c478bd9Sstevel@tonic-gate 		ASSERT(kmc->kmc_symtab == NULL);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	kmc->kmc_modctl = modp;
1717c478bd9Sstevel@tonic-gate 	kmc->kmc_exported = (mp->flags & KOBJ_EXPORTED) != 0;
1727c478bd9Sstevel@tonic-gate 	mdb_gelf_ehdr_to_gehdr(&mp->hdr, &kmc->kmc_ehdr);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	kmc->kmc_symtab = mdb_gelf_symtab_create_raw(&kmc->kmc_ehdr, mp->symhdr,
1757c478bd9Sstevel@tonic-gate 	    mp->symtbl, mp->strhdr, mp->strings,
1767c478bd9Sstevel@tonic-gate 	    MDB_TGT_SYMTAB);
1777c478bd9Sstevel@tonic-gate 
1785ae68c69Sjohnlev 	if (mp->flags & KOBJ_PRIM)
1797c478bd9Sstevel@tonic-gate 		kmc->kmc_flags |= KMDB_MC_FL_NOUNLOAD;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if (mdb_module_create(modname, modp->mod_filename,
1827c478bd9Sstevel@tonic-gate 	    kmc->kmc_loadmode, &kmc->kmc_mod) < 0)
1837c478bd9Sstevel@tonic-gate 		goto module_loaded_err;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	kmc->kmc_state = KMDB_MC_STATE_LOADED;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	return (1);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate module_loaded_err:
1907c478bd9Sstevel@tonic-gate 	if (kmc->kmc_symtab != NULL)
1917c478bd9Sstevel@tonic-gate 		mdb_gelf_symtab_destroy(kmc->kmc_symtab);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	kmdb_module_request_unload(kmc, kmc->kmc_modname, MDB_MOD_DEFER);
1947c478bd9Sstevel@tonic-gate 	return (0);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate void
kmdb_module_load_ack(kmdb_wr_load_t * dlr)1987c478bd9Sstevel@tonic-gate kmdb_module_load_ack(kmdb_wr_load_t *dlr)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	strfree(dlr->dlr_fname);
2017c478bd9Sstevel@tonic-gate 	mdb_free(dlr, sizeof (kmdb_wr_load_t));
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate void
mdb_module_load_all(int mode)2057c478bd9Sstevel@tonic-gate mdb_module_load_all(int mode)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	kmdb_wr_t *wn;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	ASSERT(mode & MDB_MOD_DEFER);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	wn = mdb_zalloc(sizeof (kmdb_wr_t), UM_SLEEP);
2127c478bd9Sstevel@tonic-gate 	wn->wn_task = WNTASK_DMOD_LOAD_ALL;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	kmdb_wr_driver_notify(wn);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate void
kmdb_module_load_all_ack(kmdb_wr_t * wn)2187c478bd9Sstevel@tonic-gate kmdb_module_load_all_ack(kmdb_wr_t *wn)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	mdb_free(wn, sizeof (kmdb_wr_t));
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate static void
kmdb_module_request_unload(kmdb_modctl_t * kmc,const char * modname,int mode)2247c478bd9Sstevel@tonic-gate kmdb_module_request_unload(kmdb_modctl_t *kmc, const char *modname, int mode)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	kmdb_wr_unload_t *dur = mdb_zalloc(sizeof (kmdb_wr_unload_t), UM_SLEEP);
2277c478bd9Sstevel@tonic-gate 	dur->dur_node.wn_task = WNTASK_DMOD_UNLOAD;
2287c478bd9Sstevel@tonic-gate 	dur->dur_modname = strdup(modname);
2297c478bd9Sstevel@tonic-gate 	dur->dur_modctl = kmc->kmc_modctl;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	kmdb_wr_driver_notify(dur);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	kmc->kmc_state = KMDB_MC_STATE_UNLOADING;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (!(mode & MDB_MOD_DEFER) &&
2367c478bd9Sstevel@tonic-gate 	    mdb_tgt_continue(mdb.m_target, NULL) == 0)
2377c478bd9Sstevel@tonic-gate 		return;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	if (!(mode & MDB_MOD_SILENT))
2407c478bd9Sstevel@tonic-gate 		mdb_printf("%s unload pending (:c to complete)\n", modname);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2447c478bd9Sstevel@tonic-gate int
mdb_module_unload(const char * name,int mode)2457c478bd9Sstevel@tonic-gate mdb_module_unload(const char *name, int mode)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 	kmdb_modctl_t *kmc = NULL;
2487c478bd9Sstevel@tonic-gate 	const char *basename;
2497c478bd9Sstevel@tonic-gate 	mdb_var_t *v;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	/*
2527c478bd9Sstevel@tonic-gate 	 * We may have been called with the name from the module itself
2537c478bd9Sstevel@tonic-gate 	 * if the caller is iterating through the module list, so we need
2547c478bd9Sstevel@tonic-gate 	 * to make a copy of the name.  If we don't, we can't use it after
2557c478bd9Sstevel@tonic-gate 	 * the call to unload_common(), which frees the module.
2567c478bd9Sstevel@tonic-gate 	 */
2577c478bd9Sstevel@tonic-gate 	name = strdup(name);
2587c478bd9Sstevel@tonic-gate 	basename = strbasename(name);
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	/*
2617c478bd9Sstevel@tonic-gate 	 * Make sure the module is in the proper state for unloading.  Modules
2627c478bd9Sstevel@tonic-gate 	 * may only be unloaded if they have properly completed loading.
2637c478bd9Sstevel@tonic-gate 	 */
2647c478bd9Sstevel@tonic-gate 	if ((v = mdb_nv_lookup(&mdb.m_dmodctl, basename)) != NULL) {
2657c478bd9Sstevel@tonic-gate 		kmc = MDB_NV_COOKIE(v);
2667c478bd9Sstevel@tonic-gate 		switch (kmc->kmc_state) {
2677c478bd9Sstevel@tonic-gate 		case KMDB_MC_STATE_LOADING:
2687c478bd9Sstevel@tonic-gate 			warn("%s is in the process of loading\n", basename);
2697c478bd9Sstevel@tonic-gate 			return (set_errno(EMDB_NOMOD));
2707c478bd9Sstevel@tonic-gate 		case KMDB_MC_STATE_UNLOADING:
2717c478bd9Sstevel@tonic-gate 			warn("%s is already being unloaded\n", basename);
2727c478bd9Sstevel@tonic-gate 			return (set_errno(EMDB_NOMOD));
2737c478bd9Sstevel@tonic-gate 		default:
2747c478bd9Sstevel@tonic-gate 			ASSERT(kmc->kmc_state == KMDB_MC_STATE_LOADED);
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		if (kmc->kmc_flags & KMDB_MC_FL_NOUNLOAD)
2787c478bd9Sstevel@tonic-gate 			return (set_errno(EMDB_KMODNOUNLOAD));
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	if (mdb_module_unload_common(name) < 0) {
2827c478bd9Sstevel@tonic-gate 		if (!(mode & MDB_MOD_SILENT)) {
2837c478bd9Sstevel@tonic-gate 			mdb_dprintf(MDB_DBG_MODULE, "unload of %s failed\n",
2847c478bd9Sstevel@tonic-gate 			    name);
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 		return (-1); /* errno is set for us */
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/*
2907c478bd9Sstevel@tonic-gate 	 * Any modules legitimately not listed in dmodctl (builtins, for
2917c478bd9Sstevel@tonic-gate 	 * example) will be handled by mdb_module_unload_common.  If any of
2927c478bd9Sstevel@tonic-gate 	 * them get here, we've got a problem.
2937c478bd9Sstevel@tonic-gate 	 */
2947c478bd9Sstevel@tonic-gate 	if (v == NULL) {
2957c478bd9Sstevel@tonic-gate 		warn("unload of unregistered module %s\n", basename);
2967c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOMOD));
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	ASSERT(kmc->kmc_dlrefcnt == 0);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	mdb_gelf_symtab_destroy(kmc->kmc_symtab);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	kmdb_module_request_unload(kmc, basename, mode);
3047c478bd9Sstevel@tonic-gate 	return (0);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate int
kmdb_module_unloaded(kmdb_wr_unload_t * dur)3087c478bd9Sstevel@tonic-gate kmdb_module_unloaded(kmdb_wr_unload_t *dur)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	mdb_var_t *v;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	if ((v = mdb_nv_lookup(&mdb.m_dmodctl, dur->dur_modname)) == NULL) {
3137c478bd9Sstevel@tonic-gate 		mdb_warn("unload for unrequested module %s\n",
3147c478bd9Sstevel@tonic-gate 		    dur->dur_modname);
3157c478bd9Sstevel@tonic-gate 		return (0);
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	if (dur->dur_errno != 0) {
3197c478bd9Sstevel@tonic-gate 		mdb_warn("dmod %s failed to unload", dur->dur_modname);
3207c478bd9Sstevel@tonic-gate 		return (0);
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	kmc_free(MDB_NV_COOKIE(v));
3247c478bd9Sstevel@tonic-gate 	mdb_nv_remove(&mdb.m_dmodctl, v);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	return (1);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate void
kmdb_module_unload_ack(kmdb_wr_unload_t * dur)3307c478bd9Sstevel@tonic-gate kmdb_module_unload_ack(kmdb_wr_unload_t *dur)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	if (dur->dur_modname != NULL)
3337c478bd9Sstevel@tonic-gate 		strfree(dur->dur_modname);
3347c478bd9Sstevel@tonic-gate 	mdb_free(dur, sizeof (kmdb_wr_unload_t));
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate /*
3387c478bd9Sstevel@tonic-gate  * Called by the kmdb_kvm target upon debugger reentry, this routine checks
3397c478bd9Sstevel@tonic-gate  * to see if the loaded dmods have changed.  Of particular interest is the
3407c478bd9Sstevel@tonic-gate  * exportation of dmod symbol tables, which will happen during the boot
3417c478bd9Sstevel@tonic-gate  * process for dmods that were loaded prior to kernel startup.  If this
3427c478bd9Sstevel@tonic-gate  * has occurred, we'll need to reconstruct our view of the symbol tables for
3435ae68c69Sjohnlev  * the affected dmods, since the old symbol tables lived in bootmem
3445ae68c69Sjohnlev  * and have been moved during the kobj_export_module().
3455ae68c69Sjohnlev  *
3465ae68c69Sjohnlev  * Also, any ctf_file_t we might have opened is now invalid, since it
3475ae68c69Sjohnlev  * has internal pointers to the old data as well.
3487c478bd9Sstevel@tonic-gate  */
3497c478bd9Sstevel@tonic-gate void
kmdb_module_sync(void)3507c478bd9Sstevel@tonic-gate kmdb_module_sync(void)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	mdb_var_t *v;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	mdb_nv_rewind(&mdb.m_dmodctl);
3557c478bd9Sstevel@tonic-gate 	while ((v = mdb_nv_advance(&mdb.m_dmodctl)) != NULL) {
3567c478bd9Sstevel@tonic-gate 		kmdb_modctl_t *kmc = MDB_NV_COOKIE(v);
3577c478bd9Sstevel@tonic-gate 		struct module *mp;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 		if (kmc->kmc_state != KMDB_MC_STATE_LOADED)
3607c478bd9Sstevel@tonic-gate 			continue;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 		mp = kmc->kmc_modctl->mod_mp;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		if ((mp->flags & (KOBJ_PRIM | KOBJ_EXPORTED)) &&
3657c478bd9Sstevel@tonic-gate 		    !kmc->kmc_exported) {
3667c478bd9Sstevel@tonic-gate 			/*
3677c478bd9Sstevel@tonic-gate 			 * The exporting process moves the symtab from boot
3687c478bd9Sstevel@tonic-gate 			 * scratch memory to vmem.
3697c478bd9Sstevel@tonic-gate 			 */
3707c478bd9Sstevel@tonic-gate 			if (kmc->kmc_symtab != NULL)
3717c478bd9Sstevel@tonic-gate 				mdb_gelf_symtab_destroy(kmc->kmc_symtab);
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 			kmc->kmc_symtab = mdb_gelf_symtab_create_raw(
3747c478bd9Sstevel@tonic-gate 			    &kmc->kmc_ehdr, mp->symhdr, mp->symtbl, mp->strhdr,
3757c478bd9Sstevel@tonic-gate 			    mp->strings, MDB_TGT_SYMTAB);
3767c478bd9Sstevel@tonic-gate 
3775ae68c69Sjohnlev 			if (kmc->kmc_mod->mod_ctfp != NULL) {
3785ae68c69Sjohnlev 				ctf_close(kmc->kmc_mod->mod_ctfp);
3795ae68c69Sjohnlev 				kmc->kmc_mod->mod_ctfp =
3805ae68c69Sjohnlev 				    mdb_ctf_open(kmc->kmc_modname, NULL);
3815ae68c69Sjohnlev 			}
3827c478bd9Sstevel@tonic-gate 			kmc->kmc_exported = TRUE;
3837c478bd9Sstevel@tonic-gate 		}
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate }
386