4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
21 * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
27 #include <sys/cdefs.h>
28 #include <sys/param.h>
29 #include <sys/systm.h>
31 #include <sys/ctype.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
36 #include <sys/dtrace.h>
37 #include <sys/dtrace_bsd.h>
39 static d_open_t dtmalloc_open;
40 static int dtmalloc_unload(void);
41 static void dtmalloc_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
42 static void dtmalloc_provide(void *, dtrace_probedesc_t *);
43 static void dtmalloc_destroy(void *, dtrace_id_t, void *);
44 static void dtmalloc_enable(void *, dtrace_id_t, void *);
45 static void dtmalloc_disable(void *, dtrace_id_t, void *);
46 static void dtmalloc_load(void *);
48 static struct cdevsw dtmalloc_cdevsw = {
49 .d_version = D_VERSION,
50 .d_open = dtmalloc_open,
54 static dtrace_pattr_t dtmalloc_attr = {
55 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
56 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
57 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
58 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
59 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
62 static dtrace_pops_t dtmalloc_pops = {
75 static struct cdev *dtmalloc_cdev;
76 static dtrace_provider_id_t dtmalloc_id;
79 dtmalloc_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
83 switch (desc->dtargd_ndx) {
85 p = "struct malloc_type *";
88 p = "struct malloc_type_internal *";
91 p = "struct malloc_type_stats *";
100 desc->dtargd_ndx = DTRACE_ARGNONE;
105 strlcpy(desc->dtargd_native, p, sizeof(desc->dtargd_native));
111 dtmalloc_type_cb(struct malloc_type *mtp, void *arg __unused)
113 char name[DTRACE_FUNCNAMELEN];
114 struct malloc_type_internal *mtip = mtp->ks_handle;
118 * malloc_type descriptions are allowed to contain whitespace, but
119 * DTrace probe identifiers are not, so replace the whitespace with
122 strlcpy(name, mtp->ks_shortdesc, sizeof(name));
123 for (i = 0; name[i] != 0; i++)
124 if (isspace(name[i]))
127 if (dtrace_probe_lookup(dtmalloc_id, NULL, name, "malloc") != 0)
130 (void) dtrace_probe_create(dtmalloc_id, NULL, name, "malloc", 0,
131 &mtip->mti_probes[DTMALLOC_PROBE_MALLOC]);
132 (void) dtrace_probe_create(dtmalloc_id, NULL, name, "free", 0,
133 &mtip->mti_probes[DTMALLOC_PROBE_FREE]);
137 dtmalloc_provide(void *arg, dtrace_probedesc_t *desc)
142 malloc_type_list(dtmalloc_type_cb, desc);
146 dtmalloc_destroy(void *arg, dtrace_id_t id, void *parg)
151 dtmalloc_enable(void *arg, dtrace_id_t id, void *parg)
158 dtmalloc_disable(void *arg, dtrace_id_t id, void *parg)
165 dtmalloc_load(void *dummy)
167 /* Create the /dev/dtrace/dtmalloc entry. */
168 dtmalloc_cdev = make_dev(&dtmalloc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
171 if (dtrace_register("dtmalloc", &dtmalloc_attr, DTRACE_PRIV_USER,
172 NULL, &dtmalloc_pops, NULL, &dtmalloc_id) != 0)
175 dtrace_malloc_probe = dtrace_probe;
184 dtrace_malloc_probe = NULL;
186 if ((error = dtrace_unregister(dtmalloc_id)) != 0)
189 destroy_dev(dtmalloc_cdev);
195 dtmalloc_modevent(module_t mod __unused, int type, void *data __unused)
219 dtmalloc_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused)
224 SYSINIT(dtmalloc_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, dtmalloc_load, NULL);
225 SYSUNINIT(dtmalloc_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, dtmalloc_unload, NULL);
227 DEV_MODULE(dtmalloc, dtmalloc_modevent, NULL);
228 MODULE_VERSION(dtmalloc, 1);
229 MODULE_DEPEND(dtmalloc, dtrace, 1, 1, 1);
230 MODULE_DEPEND(dtmalloc, opensolaris, 1, 1, 1);