xref: /freebsd-14.2/sys/cddl/dev/dtmalloc/dtmalloc.c (revision 95ee2897)
191eaf3e1SJohn Birrell /*
291eaf3e1SJohn Birrell  * CDDL HEADER START
391eaf3e1SJohn Birrell  *
491eaf3e1SJohn Birrell  * The contents of this file are subject to the terms of the
591eaf3e1SJohn Birrell  * Common Development and Distribution License (the "License").
691eaf3e1SJohn Birrell  * You may not use this file except in compliance with the License.
791eaf3e1SJohn Birrell  *
891eaf3e1SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
991eaf3e1SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
1091eaf3e1SJohn Birrell  * See the License for the specific language governing permissions
1191eaf3e1SJohn Birrell  * and limitations under the License.
1291eaf3e1SJohn Birrell  *
1391eaf3e1SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
1491eaf3e1SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1591eaf3e1SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
1691eaf3e1SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
1791eaf3e1SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
1891eaf3e1SJohn Birrell  *
1991eaf3e1SJohn Birrell  * CDDL HEADER END
2091eaf3e1SJohn Birrell  *
2191eaf3e1SJohn Birrell  * Portions Copyright 2006-2008 John Birrell [email protected]
2291eaf3e1SJohn Birrell  *
2391eaf3e1SJohn Birrell  */
2491eaf3e1SJohn Birrell 
2591eaf3e1SJohn Birrell #include <sys/cdefs.h>
2691eaf3e1SJohn Birrell #include <sys/param.h>
2791eaf3e1SJohn Birrell #include <sys/systm.h>
2891eaf3e1SJohn Birrell #include <sys/conf.h>
29837610ebSMark Johnston #include <sys/ctype.h>
3091eaf3e1SJohn Birrell #include <sys/kernel.h>
3191eaf3e1SJohn Birrell #include <sys/malloc.h>
3291eaf3e1SJohn Birrell #include <sys/module.h>
3391eaf3e1SJohn Birrell 
3491eaf3e1SJohn Birrell #include <sys/dtrace.h>
3591eaf3e1SJohn Birrell #include <sys/dtrace_bsd.h>
3691eaf3e1SJohn Birrell 
377cd79421SMateusz Guzik extern bool dtrace_malloc_enabled;
387cd79421SMateusz Guzik static uint32_t dtrace_malloc_enabled_count;
397cd79421SMateusz Guzik 
4091eaf3e1SJohn Birrell static int	dtmalloc_unload(void);
4191eaf3e1SJohn Birrell static void	dtmalloc_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
4291eaf3e1SJohn Birrell static void	dtmalloc_provide(void *, dtrace_probedesc_t *);
4391eaf3e1SJohn Birrell static void	dtmalloc_destroy(void *, dtrace_id_t, void *);
4491eaf3e1SJohn Birrell static void	dtmalloc_enable(void *, dtrace_id_t, void *);
4591eaf3e1SJohn Birrell static void	dtmalloc_disable(void *, dtrace_id_t, void *);
4691eaf3e1SJohn Birrell static void	dtmalloc_load(void *);
4791eaf3e1SJohn Birrell 
4891eaf3e1SJohn Birrell static dtrace_pattr_t dtmalloc_attr = {
4991eaf3e1SJohn Birrell { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
5091eaf3e1SJohn Birrell { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
5191eaf3e1SJohn Birrell { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
5291eaf3e1SJohn Birrell { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
5391eaf3e1SJohn Birrell { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
5491eaf3e1SJohn Birrell };
5591eaf3e1SJohn Birrell 
5691eaf3e1SJohn Birrell static dtrace_pops_t dtmalloc_pops = {
5747f11baaSMark Johnston 	.dtps_provide =		dtmalloc_provide,
5847f11baaSMark Johnston 	.dtps_provide_module =	NULL,
5947f11baaSMark Johnston 	.dtps_enable =		dtmalloc_enable,
6047f11baaSMark Johnston 	.dtps_disable =		dtmalloc_disable,
6147f11baaSMark Johnston 	.dtps_suspend =		NULL,
6247f11baaSMark Johnston 	.dtps_resume =		NULL,
6347f11baaSMark Johnston 	.dtps_getargdesc =	dtmalloc_getargdesc,
6447f11baaSMark Johnston 	.dtps_getargval =	NULL,
6547f11baaSMark Johnston 	.dtps_usermode =	NULL,
6647f11baaSMark Johnston 	.dtps_destroy =		dtmalloc_destroy
6791eaf3e1SJohn Birrell };
6891eaf3e1SJohn Birrell 
6991eaf3e1SJohn Birrell static dtrace_provider_id_t	dtmalloc_id;
7091eaf3e1SJohn Birrell 
7191eaf3e1SJohn Birrell static void
dtmalloc_getargdesc(void * arg,dtrace_id_t id,void * parg,dtrace_argdesc_t * desc)7291eaf3e1SJohn Birrell dtmalloc_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
7391eaf3e1SJohn Birrell {
7491eaf3e1SJohn Birrell 	const char *p = NULL;
7591eaf3e1SJohn Birrell 
7691eaf3e1SJohn Birrell 	switch (desc->dtargd_ndx) {
7791eaf3e1SJohn Birrell 	case 0:
7891eaf3e1SJohn Birrell 		p = "struct malloc_type *";
7991eaf3e1SJohn Birrell 		break;
8091eaf3e1SJohn Birrell 	case 1:
8191eaf3e1SJohn Birrell 		p = "struct malloc_type_internal *";
8291eaf3e1SJohn Birrell 		break;
8391eaf3e1SJohn Birrell 	case 2:
8491eaf3e1SJohn Birrell 		p = "struct malloc_type_stats *";
8591eaf3e1SJohn Birrell 		break;
8691eaf3e1SJohn Birrell 	case 3:
8791eaf3e1SJohn Birrell 		p = "unsigned long";
8891eaf3e1SJohn Birrell 		break;
8991eaf3e1SJohn Birrell 	case 4:
9091eaf3e1SJohn Birrell 		p = "int";
9191eaf3e1SJohn Birrell 		break;
9291eaf3e1SJohn Birrell 	default:
9391eaf3e1SJohn Birrell 		desc->dtargd_ndx = DTRACE_ARGNONE;
9491eaf3e1SJohn Birrell 		break;
9591eaf3e1SJohn Birrell 	}
9691eaf3e1SJohn Birrell 
9791eaf3e1SJohn Birrell 	if (p != NULL)
9891eaf3e1SJohn Birrell 		strlcpy(desc->dtargd_native, p, sizeof(desc->dtargd_native));
9991eaf3e1SJohn Birrell 
10091eaf3e1SJohn Birrell 	return;
10191eaf3e1SJohn Birrell }
10291eaf3e1SJohn Birrell 
10391eaf3e1SJohn Birrell static void
dtmalloc_type_cb(struct malloc_type * mtp,void * arg __unused)10491eaf3e1SJohn Birrell dtmalloc_type_cb(struct malloc_type *mtp, void *arg __unused)
10591eaf3e1SJohn Birrell {
10691eaf3e1SJohn Birrell 	char name[DTRACE_FUNCNAMELEN];
107bdcc2226SMateusz Guzik 	struct malloc_type_internal *mtip = &mtp->ks_mti;
108837610ebSMark Johnston 	int i;
10991eaf3e1SJohn Birrell 
110837610ebSMark Johnston 	/*
111837610ebSMark Johnston 	 * malloc_type descriptions are allowed to contain whitespace, but
112837610ebSMark Johnston 	 * DTrace probe identifiers are not, so replace the whitespace with
113837610ebSMark Johnston 	 * underscores.
114837610ebSMark Johnston 	 */
11591eaf3e1SJohn Birrell 	strlcpy(name, mtp->ks_shortdesc, sizeof(name));
116837610ebSMark Johnston 	for (i = 0; name[i] != 0; i++)
117837610ebSMark Johnston 		if (isspace(name[i]))
118837610ebSMark Johnston 			name[i] = '_';
11991eaf3e1SJohn Birrell 
12091eaf3e1SJohn Birrell 	if (dtrace_probe_lookup(dtmalloc_id, NULL, name, "malloc") != 0)
12191eaf3e1SJohn Birrell 		return;
12291eaf3e1SJohn Birrell 
12391eaf3e1SJohn Birrell 	(void) dtrace_probe_create(dtmalloc_id, NULL, name, "malloc", 0,
12491eaf3e1SJohn Birrell 	    &mtip->mti_probes[DTMALLOC_PROBE_MALLOC]);
12591eaf3e1SJohn Birrell 	(void) dtrace_probe_create(dtmalloc_id, NULL, name, "free", 0,
12691eaf3e1SJohn Birrell 	    &mtip->mti_probes[DTMALLOC_PROBE_FREE]);
12791eaf3e1SJohn Birrell }
12891eaf3e1SJohn Birrell 
12991eaf3e1SJohn Birrell static void
dtmalloc_provide(void * arg,dtrace_probedesc_t * desc)13091eaf3e1SJohn Birrell dtmalloc_provide(void *arg, dtrace_probedesc_t *desc)
13191eaf3e1SJohn Birrell {
13291eaf3e1SJohn Birrell 	if (desc != NULL)
13391eaf3e1SJohn Birrell 		return;
13491eaf3e1SJohn Birrell 
13591eaf3e1SJohn Birrell 	malloc_type_list(dtmalloc_type_cb, desc);
13691eaf3e1SJohn Birrell }
13791eaf3e1SJohn Birrell 
13891eaf3e1SJohn Birrell static void
dtmalloc_destroy(void * arg,dtrace_id_t id,void * parg)13991eaf3e1SJohn Birrell dtmalloc_destroy(void *arg, dtrace_id_t id, void *parg)
14091eaf3e1SJohn Birrell {
14191eaf3e1SJohn Birrell }
14291eaf3e1SJohn Birrell 
14391eaf3e1SJohn Birrell static void
dtmalloc_enable(void * arg,dtrace_id_t id,void * parg)14491eaf3e1SJohn Birrell dtmalloc_enable(void *arg, dtrace_id_t id, void *parg)
14591eaf3e1SJohn Birrell {
14691eaf3e1SJohn Birrell 	uint32_t *p = parg;
14791eaf3e1SJohn Birrell 	*p = id;
1487cd79421SMateusz Guzik 	dtrace_malloc_enabled_count++;
1497cd79421SMateusz Guzik 	if (dtrace_malloc_enabled_count == 1)
1507cd79421SMateusz Guzik 		dtrace_malloc_enabled = true;
15191eaf3e1SJohn Birrell }
15291eaf3e1SJohn Birrell 
15391eaf3e1SJohn Birrell static void
dtmalloc_disable(void * arg,dtrace_id_t id,void * parg)15491eaf3e1SJohn Birrell dtmalloc_disable(void *arg, dtrace_id_t id, void *parg)
15591eaf3e1SJohn Birrell {
15691eaf3e1SJohn Birrell 	uint32_t *p = parg;
15791eaf3e1SJohn Birrell 	*p = 0;
1587cd79421SMateusz Guzik 	dtrace_malloc_enabled_count--;
1597cd79421SMateusz Guzik 	if (dtrace_malloc_enabled_count == 0)
1607cd79421SMateusz Guzik 		dtrace_malloc_enabled = false;
16191eaf3e1SJohn Birrell }
16291eaf3e1SJohn Birrell 
16391eaf3e1SJohn Birrell static void
dtmalloc_load(void * dummy)16491eaf3e1SJohn Birrell dtmalloc_load(void *dummy)
16591eaf3e1SJohn Birrell {
16691eaf3e1SJohn Birrell 	if (dtrace_register("dtmalloc", &dtmalloc_attr, DTRACE_PRIV_USER,
16791eaf3e1SJohn Birrell 	    NULL, &dtmalloc_pops, NULL, &dtmalloc_id) != 0)
16891eaf3e1SJohn Birrell 		return;
16991eaf3e1SJohn Birrell 
17091eaf3e1SJohn Birrell 	dtrace_malloc_probe = dtrace_probe;
17191eaf3e1SJohn Birrell }
17291eaf3e1SJohn Birrell 
17391eaf3e1SJohn Birrell 
17491eaf3e1SJohn Birrell static int
dtmalloc_unload(void)175*a0c55bacSDimitry Andric dtmalloc_unload(void)
17691eaf3e1SJohn Birrell {
17791eaf3e1SJohn Birrell 	int error = 0;
17891eaf3e1SJohn Birrell 
17991eaf3e1SJohn Birrell 	dtrace_malloc_probe = NULL;
18091eaf3e1SJohn Birrell 
18191eaf3e1SJohn Birrell 	if ((error = dtrace_unregister(dtmalloc_id)) != 0)
18291eaf3e1SJohn Birrell 		return (error);
18391eaf3e1SJohn Birrell 
18491eaf3e1SJohn Birrell 	return (error);
18591eaf3e1SJohn Birrell }
18691eaf3e1SJohn Birrell 
18791eaf3e1SJohn Birrell static int
dtmalloc_modevent(module_t mod __unused,int type,void * data __unused)18891eaf3e1SJohn Birrell dtmalloc_modevent(module_t mod __unused, int type, void *data __unused)
18991eaf3e1SJohn Birrell {
19091eaf3e1SJohn Birrell 	int error = 0;
19191eaf3e1SJohn Birrell 
19291eaf3e1SJohn Birrell 	switch (type) {
19391eaf3e1SJohn Birrell 	case MOD_LOAD:
19491eaf3e1SJohn Birrell 		break;
19591eaf3e1SJohn Birrell 
19691eaf3e1SJohn Birrell 	case MOD_UNLOAD:
19791eaf3e1SJohn Birrell 		break;
19891eaf3e1SJohn Birrell 
19991eaf3e1SJohn Birrell 	case MOD_SHUTDOWN:
20091eaf3e1SJohn Birrell 		break;
20191eaf3e1SJohn Birrell 
20291eaf3e1SJohn Birrell 	default:
20391eaf3e1SJohn Birrell 		error = EOPNOTSUPP;
20491eaf3e1SJohn Birrell 		break;
20591eaf3e1SJohn Birrell 
20691eaf3e1SJohn Birrell 	}
20791eaf3e1SJohn Birrell 
20891eaf3e1SJohn Birrell 	return (error);
20991eaf3e1SJohn Birrell }
21091eaf3e1SJohn Birrell 
21191eaf3e1SJohn Birrell SYSINIT(dtmalloc_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, dtmalloc_load, NULL);
21291eaf3e1SJohn Birrell SYSUNINIT(dtmalloc_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, dtmalloc_unload, NULL);
21391eaf3e1SJohn Birrell 
21491eaf3e1SJohn Birrell DEV_MODULE(dtmalloc, dtmalloc_modevent, NULL);
21591eaf3e1SJohn Birrell MODULE_VERSION(dtmalloc, 1);
21691eaf3e1SJohn Birrell MODULE_DEPEND(dtmalloc, dtrace, 1, 1, 1);
21791eaf3e1SJohn Birrell MODULE_DEPEND(dtmalloc, opensolaris, 1, 1, 1);
218