xref: /onnv-gate/usr/src/cmd/mdb/common/kmdb/kctl/kctl_mod.c (revision 11053:f33a1c7f3155)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*11053SSurya.Prakki@Sun.COM  * Common Development and Distribution License (the "License").
6*11053SSurya.Prakki@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11053SSurya.Prakki@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Tracking of kernel module loads and unloads
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <kmdb/kmdb_kdi.h>
310Sstevel@tonic-gate #include <kmdb/kctl/kctl.h>
320Sstevel@tonic-gate #include <sys/kobj.h>
330Sstevel@tonic-gate #include <sys/kobj_impl.h>
340Sstevel@tonic-gate #include <sys/ctf_api.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate static kobj_notify_list_t kctl_mod_notifiers[] = {
370Sstevel@tonic-gate 	{ kctl_mod_changed, KOBJ_NOTIFY_MODLOADED },
380Sstevel@tonic-gate 	{ kctl_mod_changed, KOBJ_NOTIFY_MODUNLOADING },
390Sstevel@tonic-gate 	{ NULL, 0 }
400Sstevel@tonic-gate };
410Sstevel@tonic-gate 
420Sstevel@tonic-gate int
kctl_mod_decompress(struct modctl * modp)430Sstevel@tonic-gate kctl_mod_decompress(struct modctl *modp)
440Sstevel@tonic-gate {
450Sstevel@tonic-gate 	ctf_file_t *fp;
460Sstevel@tonic-gate 	struct module *mp = modp->mod_mp;
470Sstevel@tonic-gate 	int rc;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 	if ((kmdb_kdi_get_flags() & KMDB_KDI_FL_NOCTF) || mp->ctfdata == NULL)
500Sstevel@tonic-gate 		return (0);
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 	if ((fp = ctf_modopen(mp, &rc)) == NULL)
530Sstevel@tonic-gate 		return (rc);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	ctf_close(fp);
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	return (0);
580Sstevel@tonic-gate }
590Sstevel@tonic-gate 
600Sstevel@tonic-gate void
kctl_mod_loaded(struct modctl * modp)610Sstevel@tonic-gate kctl_mod_loaded(struct modctl *modp)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	int rc;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	mutex_enter(&mod_lock);
660Sstevel@tonic-gate 	if (modp->mod_mp == NULL) {
670Sstevel@tonic-gate 		mutex_exit(&mod_lock);
680Sstevel@tonic-gate 		return;
690Sstevel@tonic-gate 	}
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	if ((rc = kctl_mod_decompress(modp)) != 0) {
720Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to decompress CTF data for %s: %s\n",
730Sstevel@tonic-gate 		    modp->mod_modname, ctf_errmsg(rc));
740Sstevel@tonic-gate 	}
750Sstevel@tonic-gate 	mutex_exit(&mod_lock);
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	if (!(kmdb_kdi_get_flags() & KMDB_KDI_FL_NOMODS))
780Sstevel@tonic-gate 		kctl_dmod_autoload(modp->mod_modname);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*ARGSUSED*/
820Sstevel@tonic-gate void
kctl_mod_changed(uint_t why,struct modctl * what)830Sstevel@tonic-gate kctl_mod_changed(uint_t why, struct modctl *what)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate 	if (why == KOBJ_NOTIFY_MODLOADED)
860Sstevel@tonic-gate 		kctl_mod_loaded(what);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * Tell krtld to notify kmdb when modules have been loaded and unloaded
910Sstevel@tonic-gate  */
920Sstevel@tonic-gate void
kctl_mod_notify_reg(void)930Sstevel@tonic-gate kctl_mod_notify_reg(void)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate 	kobj_notify_list_t *kn;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	for (kn = kctl_mod_notifiers; kn->kn_func != NULL; kn++)
98*11053SSurya.Prakki@Sun.COM 		(void) kobj_notify_add(kn);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate void
kctl_mod_notify_unreg(void)1020Sstevel@tonic-gate kctl_mod_notify_unreg(void)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	kobj_notify_list_t *kn;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	for (kn = kctl_mod_notifiers; kn->kn_func != NULL; kn++)
107*11053SSurya.Prakki@Sun.COM 		(void) kobj_notify_remove(kn);
1080Sstevel@tonic-gate }
109