sdt.c revision 288362
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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 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. 12 * 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] 18 * 19 * CDDL HEADER END 20 * 21 * Portions Copyright 2006-2008 John Birrell jb@freebsd.org 22 * 23 * $FreeBSD: head/sys/cddl/dev/sdt/sdt.c 288362 2015-09-29 11:58:21Z avg $ 24 * 25 */ 26 27/* 28 * This file contains a reimplementation of the statically-defined tracing (SDT) 29 * framework for DTrace. Probes and SDT providers are defined using the macros 30 * in sys/sdt.h, which append all the needed structures to linker sets. When 31 * this module is loaded, it iterates over all of the loaded modules and 32 * registers probes and providers with the DTrace framework based on the 33 * contents of these linker sets. 34 * 35 * A list of SDT providers is maintained here since a provider may span multiple 36 * modules. When a kernel module is unloaded, a provider defined in that module 37 * is unregistered only if no other modules refer to it. The DTrace framework is 38 * responsible for destroying individual probes when a kernel module is 39 * unloaded; in particular, probes may not span multiple kernel modules. 40 */ 41 42#include <sys/cdefs.h> 43#include <sys/param.h> 44#include <sys/systm.h> 45 46#include <sys/conf.h> 47#include <sys/eventhandler.h> 48#include <sys/kernel.h> 49#include <sys/limits.h> 50#include <sys/linker.h> 51#include <sys/linker_set.h> 52#include <sys/lock.h> 53#include <sys/lockstat.h> 54#include <sys/malloc.h> 55#include <sys/module.h> 56#include <sys/mutex.h> 57#include <sys/queue.h> 58#include <sys/sdt.h> 59 60#include <sys/dtrace.h> 61#include <sys/dtrace_bsd.h> 62 63/* DTrace methods. */ 64static void sdt_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *); 65static void sdt_provide_probes(void *, dtrace_probedesc_t *); 66static void sdt_destroy(void *, dtrace_id_t, void *); 67static void sdt_enable(void *, dtrace_id_t, void *); 68static void sdt_disable(void *, dtrace_id_t, void *); 69 70static void sdt_load(void); 71static int sdt_unload(void); 72static void sdt_create_provider(struct sdt_provider *); 73static void sdt_create_probe(struct sdt_probe *); 74static void sdt_kld_load(void *, struct linker_file *); 75static void sdt_kld_unload_try(void *, struct linker_file *, int *); 76 77static MALLOC_DEFINE(M_SDT, "SDT", "DTrace SDT providers"); 78 79static dtrace_pattr_t sdt_attr = { 80{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 81{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 82{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 83{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 84{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 85}; 86 87static dtrace_pops_t sdt_pops = { 88 sdt_provide_probes, 89 NULL, 90 sdt_enable, 91 sdt_disable, 92 NULL, 93 NULL, 94 sdt_getargdesc, 95 NULL, 96 NULL, 97 sdt_destroy, 98}; 99 100static TAILQ_HEAD(, sdt_provider) sdt_prov_list; 101 102eventhandler_tag sdt_kld_load_tag; 103eventhandler_tag sdt_kld_unload_try_tag; 104 105static void 106sdt_create_provider(struct sdt_provider *prov) 107{ 108 struct sdt_provider *curr, *newprov; 109 110 TAILQ_FOREACH(curr, &sdt_prov_list, prov_entry) 111 if (strcmp(prov->name, curr->name) == 0) { 112 /* The provider has already been defined. */ 113 curr->sdt_refs++; 114 return; 115 } 116 117 /* 118 * Make a copy of prov so that we don't lose fields if its module is 119 * unloaded but the provider isn't destroyed. This could happen with 120 * a provider that spans multiple modules. 121 */ 122 newprov = malloc(sizeof(*newprov), M_SDT, M_WAITOK | M_ZERO); 123 newprov->name = strdup(prov->name, M_SDT); 124 prov->sdt_refs = newprov->sdt_refs = 1; 125 126 TAILQ_INSERT_TAIL(&sdt_prov_list, newprov, prov_entry); 127 128 (void)dtrace_register(newprov->name, &sdt_attr, DTRACE_PRIV_USER, NULL, 129 &sdt_pops, NULL, (dtrace_provider_id_t *)&newprov->id); 130 prov->id = newprov->id; 131} 132 133static void 134sdt_create_probe(struct sdt_probe *probe) 135{ 136 struct sdt_provider *prov; 137 char mod[DTRACE_MODNAMELEN]; 138 char func[DTRACE_FUNCNAMELEN]; 139 char name[DTRACE_NAMELEN]; 140 const char *from; 141 char *to; 142 size_t len; 143 144 if (probe->version != (int)sizeof(*probe)) { 145 printf("ignoring probe %p, version %u expected %u\n", 146 probe, probe->version, (int)sizeof(*probe)); 147 return; 148 } 149 150 TAILQ_FOREACH(prov, &sdt_prov_list, prov_entry) 151 if (strcmp(prov->name, probe->prov->name) == 0) 152 break; 153 154 KASSERT(prov != NULL, ("probe defined without a provider")); 155 156 /* If no module name was specified, use the module filename. */ 157 if (*probe->mod == 0) { 158 len = strlcpy(mod, probe->sdtp_lf->filename, sizeof(mod)); 159 if (len > 3 && strcmp(mod + len - 3, ".ko") == 0) 160 mod[len - 3] = '\0'; 161 } else 162 strlcpy(mod, probe->mod, sizeof(mod)); 163 164 /* 165 * Unfortunately this is necessary because the Solaris DTrace 166 * code mixes consts and non-consts with casts to override 167 * the incompatibilies. On FreeBSD, we use strict warnings 168 * in the C compiler, so we have to respect const vs non-const. 169 */ 170 strlcpy(func, probe->func, sizeof(func)); 171 172 from = probe->name; 173 to = name; 174 for (len = 0; len < (sizeof(name) - 1) && *from != '\0'; 175 len++, from++, to++) { 176 if (from[0] == '_' && from[1] == '_') { 177 *to = '-'; 178 from++; 179 } else 180 *to = *from; 181 } 182 *to = '\0'; 183 184 if (dtrace_probe_lookup(prov->id, mod, func, name) != DTRACE_IDNONE) 185 return; 186 187 (void)dtrace_probe_create(prov->id, mod, func, name, 1, probe); 188} 189 190/* 191 * Probes are created through the SDT module load/unload hook, so this function 192 * has nothing to do. It only exists because the DTrace provider framework 193 * requires one of provide_probes and provide_module to be defined. 194 */ 195static void 196sdt_provide_probes(void *arg, dtrace_probedesc_t *desc) 197{ 198} 199 200static void 201sdt_enable(void *arg __unused, dtrace_id_t id, void *parg) 202{ 203 struct sdt_probe *probe = parg; 204 205 probe->id = id; 206 probe->sdtp_lf->nenabled++; 207 if (strcmp(probe->prov->name, "lockstat") == 0) 208 lockstat_enabled++; 209} 210 211static void 212sdt_disable(void *arg __unused, dtrace_id_t id, void *parg) 213{ 214 struct sdt_probe *probe = parg; 215 216 KASSERT(probe->sdtp_lf->nenabled > 0, ("no probes enabled")); 217 218 if (strcmp(probe->prov->name, "lockstat") == 0) 219 lockstat_enabled--; 220 probe->id = 0; 221 probe->sdtp_lf->nenabled--; 222} 223 224static void 225sdt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc) 226{ 227 struct sdt_argtype *argtype; 228 struct sdt_probe *probe = parg; 229 230 if (desc->dtargd_ndx >= probe->n_args) { 231 desc->dtargd_ndx = DTRACE_ARGNONE; 232 return; 233 } 234 235 TAILQ_FOREACH(argtype, &probe->argtype_list, argtype_entry) { 236 if (desc->dtargd_ndx == argtype->ndx) { 237 desc->dtargd_mapping = desc->dtargd_ndx; 238 if (argtype->type == NULL) { 239 desc->dtargd_native[0] = '\0'; 240 desc->dtargd_xlate[0] = '\0'; 241 continue; 242 } 243 strlcpy(desc->dtargd_native, argtype->type, 244 sizeof(desc->dtargd_native)); 245 if (argtype->xtype != NULL) 246 strlcpy(desc->dtargd_xlate, argtype->xtype, 247 sizeof(desc->dtargd_xlate)); 248 } 249 } 250} 251 252static void 253sdt_destroy(void *arg, dtrace_id_t id, void *parg) 254{ 255} 256 257/* 258 * Called from the kernel linker when a module is loaded, before 259 * dtrace_module_loaded() is called. This is done so that it's possible to 260 * register new providers when modules are loaded. The DTrace framework 261 * explicitly disallows calling into the framework from the provide_module 262 * provider method, so we cannot do this there. 263 */ 264static void 265sdt_kld_load(void *arg __unused, struct linker_file *lf) 266{ 267 struct sdt_provider **prov, **begin, **end; 268 struct sdt_probe **probe, **p_begin, **p_end; 269 struct sdt_argtype **argtype, **a_begin, **a_end; 270 271 if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end, 272 NULL) == 0) { 273 for (prov = begin; prov < end; prov++) 274 sdt_create_provider(*prov); 275 } 276 277 if (linker_file_lookup_set(lf, "sdt_probes_set", &p_begin, &p_end, 278 NULL) == 0) { 279 for (probe = p_begin; probe < p_end; probe++) { 280 (*probe)->sdtp_lf = lf; 281 sdt_create_probe(*probe); 282 TAILQ_INIT(&(*probe)->argtype_list); 283 } 284 } 285 286 if (linker_file_lookup_set(lf, "sdt_argtypes_set", &a_begin, &a_end, 287 NULL) == 0) { 288 for (argtype = a_begin; argtype < a_end; argtype++) { 289 (*argtype)->probe->n_args++; 290 TAILQ_INSERT_TAIL(&(*argtype)->probe->argtype_list, 291 *argtype, argtype_entry); 292 } 293 } 294} 295 296static void 297sdt_kld_unload_try(void *arg __unused, struct linker_file *lf, int *error) 298{ 299 struct sdt_provider *prov, **curr, **begin, **end, *tmp; 300 301 if (*error != 0) 302 /* We already have an error, so don't do anything. */ 303 return; 304 else if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end, 305 NULL)) 306 /* No DTrace providers are declared in this file. */ 307 return; 308 309 /* 310 * Go through all the providers declared in this linker file and 311 * unregister any that aren't declared in another loaded file. 312 */ 313 for (curr = begin; curr < end; curr++) { 314 TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) { 315 if (strcmp(prov->name, (*curr)->name) != 0) 316 continue; 317 318 if (prov->sdt_refs == 1) { 319 if (dtrace_unregister(prov->id) != 0) { 320 *error = 1; 321 return; 322 } 323 TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry); 324 free(prov->name, M_SDT); 325 free(prov, M_SDT); 326 } else 327 prov->sdt_refs--; 328 break; 329 } 330 } 331} 332 333static int 334sdt_linker_file_cb(linker_file_t lf, void *arg __unused) 335{ 336 337 sdt_kld_load(NULL, lf); 338 339 return (0); 340} 341 342static void 343sdt_load() 344{ 345 346 TAILQ_INIT(&sdt_prov_list); 347 348 sdt_probe_func = dtrace_probe; 349 350 sdt_kld_load_tag = EVENTHANDLER_REGISTER(kld_load, sdt_kld_load, NULL, 351 EVENTHANDLER_PRI_ANY); 352 sdt_kld_unload_try_tag = EVENTHANDLER_REGISTER(kld_unload_try, 353 sdt_kld_unload_try, NULL, EVENTHANDLER_PRI_ANY); 354 355 /* Pick up probes from the kernel and already-loaded linker files. */ 356 linker_file_foreach(sdt_linker_file_cb, NULL); 357} 358 359static int 360sdt_unload() 361{ 362 struct sdt_provider *prov, *tmp; 363 int ret; 364 365 EVENTHANDLER_DEREGISTER(kld_load, sdt_kld_load_tag); 366 EVENTHANDLER_DEREGISTER(kld_unload_try, sdt_kld_unload_try_tag); 367 368 sdt_probe_func = sdt_probe_stub; 369 370 TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) { 371 ret = dtrace_unregister(prov->id); 372 if (ret != 0) 373 return (ret); 374 TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry); 375 free(prov->name, M_SDT); 376 free(prov, M_SDT); 377 } 378 379 return (0); 380} 381 382static int 383sdt_modevent(module_t mod __unused, int type, void *data __unused) 384{ 385 int error = 0; 386 387 switch (type) { 388 case MOD_LOAD: 389 sdt_load(); 390 break; 391 392 case MOD_UNLOAD: 393 error = sdt_unload(); 394 break; 395 396 case MOD_SHUTDOWN: 397 break; 398 399 default: 400 error = EOPNOTSUPP; 401 break; 402 } 403 404 return (error); 405} 406 407DEV_MODULE(sdt, sdt_modevent, NULL); 408MODULE_VERSION(sdt, 1); 409MODULE_DEPEND(sdt, dtrace, 1, 1, 1); 410MODULE_DEPEND(sdt, opensolaris, 1, 1, 1); 411