summaryrefslogtreecommitdiff
path: root/usr/src/cmd/mdb/common/kmdb/kctl/kctl_mod.c
blob: 8a66af6a7ff8f4c5a2a73dcac6f7fffb71b24160 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Tracking of kernel module loads and unloads
 */

#include <kmdb/kmdb_kdi.h>
#include <kmdb/kctl/kctl.h>
#include <sys/kobj.h>
#include <sys/kobj_impl.h>
#include <sys/ctf_api.h>

static kobj_notify_list_t kctl_mod_notifiers[] = {
	{ kctl_mod_changed, KOBJ_NOTIFY_MODLOADED },
	{ kctl_mod_changed, KOBJ_NOTIFY_MODUNLOADING },
	{ NULL, 0 }
};

int
kctl_mod_decompress(struct modctl *modp)
{
	ctf_file_t *fp;
	struct module *mp = modp->mod_mp;
	int rc;

	if ((kmdb_kdi_get_flags() & KMDB_KDI_FL_NOCTF) || mp->ctfdata == NULL)
		return (0);

	if ((fp = ctf_modopen(mp, &rc)) == NULL)
		return (rc);

	ctf_close(fp);

	return (0);
}

void
kctl_mod_loaded(struct modctl *modp)
{
	int rc;

	mutex_enter(&mod_lock);
	if (modp->mod_mp == NULL) {
		mutex_exit(&mod_lock);
		return;
	}

	if ((rc = kctl_mod_decompress(modp)) != 0) {
		cmn_err(CE_WARN, "failed to decompress CTF data for %s: %s\n",
		    modp->mod_modname, ctf_errmsg(rc));
	}
	mutex_exit(&mod_lock);

	if (!(kmdb_kdi_get_flags() & KMDB_KDI_FL_NOMODS))
		kctl_dmod_autoload(modp->mod_modname);
}

/*ARGSUSED*/
void
kctl_mod_changed(uint_t why, struct modctl *what)
{
	if (why == KOBJ_NOTIFY_MODLOADED)
		kctl_mod_loaded(what);
}

/*
 * Tell krtld to notify kmdb when modules have been loaded and unloaded
 */
void
kctl_mod_notify_reg(void)
{
	kobj_notify_list_t *kn;

	for (kn = kctl_mod_notifiers; kn->kn_func != NULL; kn++)
		(void) kobj_notify_add(kn);
}

void
kctl_mod_notify_unreg(void)
{
	kobj_notify_list_t *kn;

	for (kn = kctl_mod_notifiers; kn->kn_func != NULL; kn++)
		(void) kobj_notify_remove(kn);
}