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