xref: /illumos-gate/usr/src/cmd/mdb/common/kmdb/kctl/kctl_mod.c (revision e7cbe64f7a72dae5cb44f100db60ca88f3313c65)
1 /*
2  * CDDL HEADER START
3  *
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.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Tracking of kernel module loads and unloads
31  */
32 
33 #include <kmdb/kmdb_kdi.h>
34 #include <kmdb/kctl/kctl.h>
35 #include <sys/kobj.h>
36 #include <sys/kobj_impl.h>
37 #include <sys/ctf_api.h>
38 
39 static kobj_notify_list_t kctl_mod_notifiers[] = {
40 	{ kctl_mod_changed, KOBJ_NOTIFY_MODLOADED },
41 	{ kctl_mod_changed, KOBJ_NOTIFY_MODUNLOADING },
42 	{ NULL, 0 }
43 };
44 
45 int
46 kctl_mod_decompress(struct modctl *modp)
47 {
48 	ctf_file_t *fp;
49 	struct module *mp = modp->mod_mp;
50 	int rc;
51 
52 	if ((kmdb_kdi_get_flags() & KMDB_KDI_FL_NOCTF) || mp->ctfdata == NULL)
53 		return (0);
54 
55 	if ((fp = ctf_modopen(mp, &rc)) == NULL)
56 		return (rc);
57 
58 	ctf_close(fp);
59 
60 	return (0);
61 }
62 
63 void
64 kctl_mod_loaded(struct modctl *modp)
65 {
66 	int rc;
67 
68 	mutex_enter(&mod_lock);
69 	if (modp->mod_mp == NULL) {
70 		mutex_exit(&mod_lock);
71 		return;
72 	}
73 
74 	if ((rc = kctl_mod_decompress(modp)) != 0) {
75 		cmn_err(CE_WARN, "failed to decompress CTF data for %s: %s\n",
76 		    modp->mod_modname, ctf_errmsg(rc));
77 	}
78 	mutex_exit(&mod_lock);
79 
80 	if (!(kmdb_kdi_get_flags() & KMDB_KDI_FL_NOMODS))
81 		kctl_dmod_autoload(modp->mod_modname);
82 }
83 
84 /*ARGSUSED*/
85 void
86 kctl_mod_changed(uint_t why, struct modctl *what)
87 {
88 	if (why == KOBJ_NOTIFY_MODLOADED)
89 		kctl_mod_loaded(what);
90 }
91 
92 /*
93  * Tell krtld to notify kmdb when modules have been loaded and unloaded
94  */
95 void
96 kctl_mod_notify_reg(void)
97 {
98 	kobj_notify_list_t *kn;
99 
100 	for (kn = kctl_mod_notifiers; kn->kn_func != NULL; kn++)
101 		kobj_notify_add(kn);
102 }
103 
104 void
105 kctl_mod_notify_unreg(void)
106 {
107 	kobj_notify_list_t *kn;
108 
109 	for (kn = kctl_mod_notifiers; kn->kn_func != NULL; kn++)
110 		kobj_notify_remove(kn);
111 }
112