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
5*80148899SSurya Prakki  * Common Development and Distribution License (the "License").
6*80148899SSurya Prakki  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*80148899SSurya Prakki  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Tracking of kernel module loads and unloads
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_kdi.h>
317c478bd9Sstevel@tonic-gate #include <kmdb/kctl/kctl.h>
327c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
337c478bd9Sstevel@tonic-gate #include <sys/kobj_impl.h>
347c478bd9Sstevel@tonic-gate #include <sys/ctf_api.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate static kobj_notify_list_t kctl_mod_notifiers[] = {
377c478bd9Sstevel@tonic-gate 	{ kctl_mod_changed, KOBJ_NOTIFY_MODLOADED },
387c478bd9Sstevel@tonic-gate 	{ kctl_mod_changed, KOBJ_NOTIFY_MODUNLOADING },
397c478bd9Sstevel@tonic-gate 	{ NULL, 0 }
407c478bd9Sstevel@tonic-gate };
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate int
kctl_mod_decompress(struct modctl * modp)437c478bd9Sstevel@tonic-gate kctl_mod_decompress(struct modctl *modp)
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 	ctf_file_t *fp;
467c478bd9Sstevel@tonic-gate 	struct module *mp = modp->mod_mp;
477c478bd9Sstevel@tonic-gate 	int rc;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	if ((kmdb_kdi_get_flags() & KMDB_KDI_FL_NOCTF) || mp->ctfdata == NULL)
507c478bd9Sstevel@tonic-gate 		return (0);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	if ((fp = ctf_modopen(mp, &rc)) == NULL)
537c478bd9Sstevel@tonic-gate 		return (rc);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	ctf_close(fp);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	return (0);
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate void
kctl_mod_loaded(struct modctl * modp)617c478bd9Sstevel@tonic-gate kctl_mod_loaded(struct modctl *modp)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	int rc;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
667c478bd9Sstevel@tonic-gate 	if (modp->mod_mp == NULL) {
677c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_lock);
687c478bd9Sstevel@tonic-gate 		return;
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	if ((rc = kctl_mod_decompress(modp)) != 0) {
727c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to decompress CTF data for %s: %s\n",
737c478bd9Sstevel@tonic-gate 		    modp->mod_modname, ctf_errmsg(rc));
747c478bd9Sstevel@tonic-gate 	}
757c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	if (!(kmdb_kdi_get_flags() & KMDB_KDI_FL_NOMODS))
787c478bd9Sstevel@tonic-gate 		kctl_dmod_autoload(modp->mod_modname);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*ARGSUSED*/
827c478bd9Sstevel@tonic-gate void
kctl_mod_changed(uint_t why,struct modctl * what)837c478bd9Sstevel@tonic-gate kctl_mod_changed(uint_t why, struct modctl *what)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	if (why == KOBJ_NOTIFY_MODLOADED)
867c478bd9Sstevel@tonic-gate 		kctl_mod_loaded(what);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * Tell krtld to notify kmdb when modules have been loaded and unloaded
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate void
kctl_mod_notify_reg(void)937c478bd9Sstevel@tonic-gate kctl_mod_notify_reg(void)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	kobj_notify_list_t *kn;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	for (kn = kctl_mod_notifiers; kn->kn_func != NULL; kn++)
98*80148899SSurya Prakki 		(void) kobj_notify_add(kn);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate void
kctl_mod_notify_unreg(void)1027c478bd9Sstevel@tonic-gate kctl_mod_notify_unreg(void)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	kobj_notify_list_t *kn;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	for (kn = kctl_mod_notifiers; kn->kn_func != NULL; kn++)
107*80148899SSurya Prakki 		(void) kobj_notify_remove(kn);
1087c478bd9Sstevel@tonic-gate }
109