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