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 285703 2015-07-19 22:14:09Z markj $ 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 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: sdt.c,v 1.17 2016/07/17 02:09:10 pgoyette Exp $"); 43 44 #include <sys/cdefs.h> 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 48 #include <sys/conf.h> 49 #ifdef __FreeBSD__ 50 #include <sys/eventhandler.h> 51 #endif 52 #include <sys/kernel.h> 53 #include <sys/limits.h> 54 #ifdef __FreeBSD__ 55 #include <sys/linker.h> 56 #include <sys/linker_set.h> 57 #endif 58 #include <sys/lock.h> 59 #ifdef __FreeBSD__ 60 #include <sys/lockstat.h> 61 #endif 62 #include <sys/malloc.h> 63 #include <sys/kmem.h> 64 #include <sys/module.h> 65 #include <sys/mutex.h> 66 #include <sys/queue.h> 67 #define KDTRACE_HOOKS 68 #include <sys/sdt.h> 69 70 #include <sys/dtrace.h> 71 #include <sys/dtrace_bsd.h> 72 73 /* DTrace methods. */ 74 static void sdt_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *); 75 static void sdt_provide_probes(void *, const dtrace_probedesc_t *); 76 static void sdt_destroy(void *, dtrace_id_t, void *); 77 static int sdt_enable(void *, dtrace_id_t, void *); 78 static void sdt_disable(void *, dtrace_id_t, void *); 79 80 static void sdt_load(void); 81 static int sdt_unload(void); 82 static void sdt_create_provider(struct sdt_provider *); 83 static void sdt_create_probe(struct sdt_probe *); 84 #ifdef __FreeBSD__ 85 static void sdt_kld_load(void *, struct linker_file *); 86 static void sdt_kld_unload_try(void *, struct linker_file *, int *); 87 #endif 88 89 MALLOC_DECLARE(M_SDT); 90 MALLOC_DEFINE(M_SDT, "SDT", "DTrace SDT providers"); 91 #define SDT_KASSERT(cond, msg) KASSERT(cond) 92 93 static dtrace_pattr_t sdt_attr = { 94 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 95 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 96 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 97 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 98 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 99 }; 100 101 static dtrace_pops_t sdt_pops = { 102 sdt_provide_probes, 103 NULL, 104 sdt_enable, 105 sdt_disable, 106 NULL, 107 NULL, 108 sdt_getargdesc, 109 NULL, 110 NULL, 111 sdt_destroy, 112 }; 113 114 #ifdef __NetBSD__ 115 static int 116 sdt_open(dev_t dev, int flags, int mode, struct lwp *l) 117 { 118 return (0); 119 } 120 121 static const struct cdevsw sdt_cdevsw = { 122 .d_open = sdt_open, 123 .d_close = noclose, 124 .d_read = noread, 125 .d_write = nowrite, 126 .d_ioctl = noioctl, 127 .d_stop = nostop, 128 .d_tty = notty, 129 .d_poll = nopoll, 130 .d_mmap = nommap, 131 .d_kqfilter = nokqfilter, 132 .d_discard = nodiscard, 133 .d_flag = D_OTHER 134 }; 135 #endif 136 137 static TAILQ_HEAD(, sdt_provider) sdt_prov_list; 138 139 #ifdef __FreeBSD__ 140 eventhandler_tag sdt_kld_load_tag; 141 eventhandler_tag sdt_kld_unload_try_tag; 142 #endif 143 144 #ifdef __NetBSD__ 145 static char * 146 strdup(const char *s, const struct malloc_type *m) 147 { 148 size_t l = strlen(s) + 1; 149 char *d = malloc(l, m, M_WAITOK); 150 memcpy(d, s, l); 151 return d; 152 } 153 #endif 154 155 static void 156 sdt_create_provider(struct sdt_provider *prov) 157 { 158 struct sdt_provider *curr, *newprov; 159 160 TAILQ_FOREACH(curr, &sdt_prov_list, prov_entry) 161 if (strcmp(prov->name, curr->name) == 0) { 162 /* The provider has already been defined. */ 163 curr->sdt_refs++; 164 return; 165 } 166 167 /* 168 * Make a copy of prov so that we don't lose fields if its module is 169 * unloaded but the provider isn't destroyed. This could happen with 170 * a provider that spans multiple modules. 171 */ 172 newprov = malloc(sizeof(*newprov), M_SDT, M_WAITOK | M_ZERO); 173 newprov->name = strdup(prov->name, M_SDT); 174 prov->sdt_refs = newprov->sdt_refs = 1; 175 176 TAILQ_INSERT_TAIL(&sdt_prov_list, newprov, prov_entry); 177 178 (void)dtrace_register(newprov->name, &sdt_attr, DTRACE_PRIV_USER, NULL, 179 &sdt_pops, NULL, (dtrace_provider_id_t *)&newprov->id); 180 prov->id = newprov->id; 181 } 182 183 static void 184 sdt_create_probe(struct sdt_probe *probe) 185 { 186 struct sdt_provider *prov; 187 char mod[DTRACE_MODNAMELEN]; 188 char func[DTRACE_FUNCNAMELEN]; 189 char name[DTRACE_NAMELEN]; 190 const char *from; 191 char *to; 192 size_t len; 193 194 TAILQ_FOREACH(prov, &sdt_prov_list, prov_entry) 195 if (strcmp(prov->name, probe->prov->name) == 0) 196 break; 197 198 SDT_KASSERT(prov != NULL, ("probe defined without a provider")); 199 200 #ifdef __FreeBSD__ 201 /* If no module name was specified, use the module filename. */ 202 if (*probe->mod == 0) { 203 len = strlcpy(mod, probe->sdtp_lf->filename, sizeof(mod)); 204 if (len > 3 && strcmp(mod + len - 3, ".ko") == 0) 205 mod[len - 3] = '\0'; 206 } else 207 #endif 208 strlcpy(mod, probe->mod, sizeof(mod)); 209 210 /* 211 * Unfortunately this is necessary because the Solaris DTrace 212 * code mixes consts and non-consts with casts to override 213 * the incompatibilies. On FreeBSD, we use strict warnings 214 * in the C compiler, so we have to respect const vs non-const. 215 */ 216 strlcpy(func, probe->func, sizeof(func)); 217 218 from = probe->name; 219 to = name; 220 for (len = 0; len < (sizeof(name) - 1) && *from != '\0'; 221 len++, from++, to++) { 222 if (from[0] == '_' && from[1] == '_') { 223 *to = '-'; 224 from++; 225 } else 226 *to = *from; 227 } 228 *to = '\0'; 229 230 if (dtrace_probe_lookup(prov->id, mod, func, name) != DTRACE_IDNONE) 231 return; 232 233 (void)dtrace_probe_create(prov->id, mod, func, name, 1, probe); 234 } 235 236 /* 237 * Probes are created through the SDT module load/unload hook, so this function 238 * has nothing to do. It only exists because the DTrace provider framework 239 * requires one of provide_probes and provide_module to be defined. 240 */ 241 static void 242 sdt_provide_probes(void *arg, const dtrace_probedesc_t *desc) 243 { 244 } 245 246 static int 247 sdt_enable(void *arg __unused, dtrace_id_t id, void *parg) 248 { 249 struct sdt_probe *probe = parg; 250 251 #ifdef SDT_DEBUG 252 printf("sdt: %s\n", __func__); 253 #endif 254 255 probe->id = id; 256 #ifdef __FreeBSD__ 257 probe->sdtp_lf->nenabled++; 258 if (strcmp(probe->prov->name, "lockstat") == 0) 259 lockstat_enabled++; 260 #endif 261 return 1; 262 } 263 264 static void 265 sdt_disable(void *arg __unused, dtrace_id_t id, void *parg) 266 { 267 struct sdt_probe *probe = parg; 268 269 #ifdef __FreeBSD__ 270 SDT_KASSERT(probe->sdtp_lf->nenabled > 0, ("no probes enabled")); 271 #endif 272 273 #ifdef SDT_DEBUG 274 printf("sdt: %s\n", __func__); 275 #endif 276 277 #ifdef __FreeBSD__ 278 if (strcmp(probe->prov->name, "lockstat") == 0) 279 lockstat_enabled--; 280 probe->sdtp_lf->nenabled--; 281 #endif 282 probe->id = 0; 283 } 284 285 static void 286 sdt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc) 287 { 288 struct sdt_argtype *argtype; 289 struct sdt_probe *probe = parg; 290 291 #ifdef SDT_DEBUG 292 printf("sdt: %s probe %d\n", __func__, id); 293 printf("%s: probe %d (%s:%s:%s:%s).%d\n", 294 __func__, id, 295 probe->provider, 296 probe->module, 297 probe->function, 298 probe->name, 299 desc->dtargd_ndx); 300 #endif 301 if (desc->dtargd_ndx >= probe->n_args) { 302 desc->dtargd_ndx = DTRACE_ARGNONE; 303 return; 304 } 305 306 TAILQ_FOREACH(argtype, &probe->argtype_list, argtype_entry) { 307 if (desc->dtargd_ndx == argtype->ndx) { 308 desc->dtargd_mapping = desc->dtargd_ndx; 309 if (argtype->type == NULL) { 310 desc->dtargd_native[0] = '\0'; 311 desc->dtargd_xlate[0] = '\0'; 312 continue; 313 } 314 strlcpy(desc->dtargd_native, argtype->type, 315 sizeof(desc->dtargd_native)); 316 if (argtype->xtype != NULL) 317 strlcpy(desc->dtargd_xlate, argtype->xtype, 318 sizeof(desc->dtargd_xlate)); 319 } 320 } 321 } 322 323 static void 324 sdt_destroy(void *arg, dtrace_id_t id, void *parg) 325 { 326 } 327 328 #ifdef __FreeBSD__ 329 /* 330 * Called from the kernel linker when a module is loaded, before 331 * dtrace_module_loaded() is called. This is done so that it's possible to 332 * register new providers when modules are loaded. The DTrace framework 333 * explicitly disallows calling into the framework from the provide_module 334 * provider method, so we cannot do this there. 335 */ 336 static void 337 sdt_kld_load(void *arg __unused, struct linker_file *lf) 338 { 339 struct sdt_provider **prov, **begin, **end; 340 struct sdt_probe **probe, **p_begin, **p_end; 341 struct sdt_argtype **argtype, **a_begin, **a_end; 342 343 if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end, 344 NULL) == 0) { 345 for (prov = begin; prov < end; prov++) 346 sdt_create_provider(*prov); 347 } 348 349 if (linker_file_lookup_set(lf, "sdt_probes_set", &p_begin, &p_end, 350 NULL) == 0) { 351 for (probe = p_begin; probe < p_end; probe++) { 352 (*probe)->sdtp_lf = lf; 353 sdt_create_probe(*probe); 354 TAILQ_INIT(&(*probe)->argtype_list); 355 } 356 } 357 358 if (linker_file_lookup_set(lf, "sdt_argtypes_set", &a_begin, &a_end, 359 NULL) == 0) { 360 for (argtype = a_begin; argtype < a_end; argtype++) { 361 (*argtype)->probe->n_args++; 362 TAILQ_INSERT_TAIL(&(*argtype)->probe->argtype_list, 363 *argtype, argtype_entry); 364 } 365 } 366 } 367 368 static void 369 sdt_kld_unload_try(void *arg __unused, struct linker_file *lf, int *error) 370 { 371 struct sdt_provider *prov, **curr, **begin, **end, *tmp; 372 373 if (*error != 0) 374 /* We already have an error, so don't do anything. */ 375 return; 376 else if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end, 377 NULL)) 378 /* No DTrace providers are declared in this file. */ 379 return; 380 381 /* 382 * Go through all the providers declared in this linker file and 383 * unregister any that aren't declared in another loaded file. 384 */ 385 for (curr = begin; curr < end; curr++) { 386 TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) { 387 if (strcmp(prov->name, (*curr)->name) != 0) 388 continue; 389 390 if (prov->sdt_refs == 1) { 391 if (dtrace_unregister(prov->id) != 0) { 392 *error = 1; 393 return; 394 } 395 TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry); 396 free(prov->name, M_SDT); 397 free(prov, M_SDT); 398 } else 399 prov->sdt_refs--; 400 break; 401 } 402 } 403 } 404 405 static int 406 sdt_linker_file_cb(linker_file_t lf, void *arg __unused) 407 { 408 409 sdt_kld_load(NULL, lf); 410 411 return (0); 412 } 413 #endif 414 415 #ifdef __NetBSD__ 416 /* 417 * weak symbols don't work in kernel modules; link set end symbols are 418 * weak by default, so we kill that. 419 */ 420 #undef __weak 421 #define __weak 422 __link_set_decl(sdt_providers_set, struct sdt_provider); 423 __link_set_decl(sdt_probes_set, struct sdt_probe); 424 __link_set_decl(sdt_argtypes_set, struct sdt_argtype); 425 426 /* 427 * Unfortunately we don't have linker set functions and event handlers 428 * to support loading and unloading probes in modules... Currently if 429 * modules have probes, if the modules are loaded when sdt is loaded 430 * they will work, but they will crash unloading. 431 */ 432 static void 433 sdt_link_set_load(void) 434 { 435 struct sdt_provider * const *provider; 436 struct sdt_probe * const *probe; 437 struct sdt_argtype * const *argtype; 438 439 __link_set_foreach(provider, sdt_providers_set) { 440 sdt_create_provider(*provider); 441 } 442 443 __link_set_foreach(probe, sdt_probes_set) { 444 (*probe)->sdtp_lf = NULL; // XXX: we don't support it 445 sdt_create_probe(*probe); 446 TAILQ_INIT(&(*probe)->argtype_list); 447 } 448 449 __link_set_foreach(argtype, sdt_argtypes_set) { 450 (*argtype)->probe->n_args++; 451 TAILQ_INSERT_TAIL(&(*argtype)->probe->argtype_list, 452 *argtype, argtype_entry); 453 } 454 } 455 456 static void 457 sdt_link_set_unload(void) 458 { 459 struct sdt_provider * const *curr, *prov, *tmp; 460 461 /* 462 * Go through all the providers declared in this linker file and 463 * unregister any that aren't declared in another loaded file. 464 */ 465 __link_set_foreach(curr, sdt_providers_set) { 466 TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) { 467 if (strcmp(prov->name, (*curr)->name) != 0) 468 continue; 469 470 if (prov->sdt_refs == 1) { 471 if (dtrace_unregister(prov->id) != 0) { 472 return; 473 } 474 TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry); 475 free(__UNCONST(prov->name), M_SDT); 476 free(prov, M_SDT); 477 } else 478 prov->sdt_refs--; 479 break; 480 } 481 } 482 } 483 #endif 484 485 486 static void 487 sdt_load(void) 488 { 489 TAILQ_INIT(&sdt_prov_list); 490 491 sdt_init(dtrace_probe); 492 #ifdef __FreeBSD__ 493 /* Pick up probes from the kernel and already-loaded linker files. */ 494 linker_file_foreach(sdt_linker_file_cb, NULL); 495 #endif 496 #ifdef __NetBSD__ 497 sdt_link_set_load(); 498 #endif 499 } 500 501 static int 502 sdt_unload(void) 503 { 504 struct sdt_provider *prov, *tmp; 505 int ret; 506 507 sdt_exit(); 508 509 #ifdef __NetBSD__ 510 sdt_link_set_unload(); 511 #endif 512 TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) { 513 ret = dtrace_unregister(prov->id); 514 if (ret != 0) 515 return ret; 516 TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry); 517 free(__UNCONST(prov->name), M_SDT); 518 free(prov, M_SDT); 519 } 520 521 return 0; 522 } 523 524 #ifdef __FreeBSD__ 525 static int 526 sdt_modevent(module_t mod __unused, int type, void *data __unused) 527 { 528 int error = 0; 529 530 switch (type) { 531 case MOD_LOAD: 532 sdt_load(); 533 break; 534 535 case MOD_UNLOAD: 536 error = sdt_unload(); 537 break; 538 539 case MOD_SHUTDOWN: 540 break; 541 542 default: 543 error = EOPNOTSUPP; 544 break; 545 } 546 547 return (error); 548 } 549 550 DEV_MODULE(sdt, sdt_modevent, NULL); 551 MODULE_VERSION(sdt, 1); 552 MODULE_DEPEND(sdt, dtrace, 1, 1, 1); 553 MODULE_DEPEND(sdt, opensolaris, 1, 1, 1); 554 #endif 555 556 #ifdef __NetBSD__ 557 static int 558 dtrace_sdt_modcmd(modcmd_t cmd, void *data) 559 { 560 int bmajor = -1, cmajor = -1; 561 int error; 562 563 switch (cmd) { 564 case MODULE_CMD_INIT: 565 sdt_load(); 566 return devsw_attach("sdt", NULL, &bmajor, 567 &sdt_cdevsw, &cmajor); 568 case MODULE_CMD_FINI: 569 error = sdt_unload(); 570 if (error != 0) 571 return error; 572 return devsw_detach(NULL, &sdt_cdevsw); 573 case MODULE_CMD_AUTOUNLOAD: 574 return EBUSY; 575 default: 576 error = EOPNOTSUPP; 577 break; 578 } 579 return error; 580 } 581 582 MODULE(MODULE_CLASS_MISC, dtrace_sdt, "dtrace"); 583 #endif 584