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 * Copyright 2024 Mark Johnston <markj@FreeBSD.org> 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 #include <cddl/dev/dtrace/dtrace_cddl.h> 62 63 /* DTrace methods. */ 64 static void sdt_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *); 65 static uint64_t sdt_getargval(void *, dtrace_id_t, void *, int, int); 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_init_probe(struct sdt_probe *, linker_file_t); 76 static void sdt_kld_load(void *, struct linker_file *); 77 static void sdt_kld_unload_try(void *, struct linker_file *, int *); 78 79 static MALLOC_DEFINE(M_SDT, "SDT", "DTrace SDT providers"); 80 81 static int sdt_probes_enabled_count; 82 static int lockstat_enabled_count; 83 84 static dtrace_pattr_t sdt_attr = { 85 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 86 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 87 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 88 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 89 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 90 }; 91 92 static dtrace_pops_t sdt_pops = { 93 .dtps_provide = sdt_provide_probes, 94 .dtps_provide_module = NULL, 95 .dtps_enable = sdt_enable, 96 .dtps_disable = sdt_disable, 97 .dtps_suspend = NULL, 98 .dtps_resume = NULL, 99 .dtps_getargdesc = sdt_getargdesc, 100 .dtps_getargval = sdt_getargval, 101 .dtps_usermode = NULL, 102 .dtps_destroy = sdt_destroy, 103 }; 104 105 static TAILQ_HEAD(, sdt_provider) sdt_prov_list; 106 107 static eventhandler_tag sdt_kld_load_tag; 108 static eventhandler_tag sdt_kld_unload_try_tag; 109 110 static void 111 sdt_create_provider(struct sdt_provider *prov) 112 { 113 struct sdt_provider *curr, *newprov; 114 115 TAILQ_FOREACH(curr, &sdt_prov_list, prov_entry) 116 if (strcmp(prov->name, curr->name) == 0) { 117 /* The provider has already been defined. */ 118 curr->sdt_refs++; 119 return; 120 } 121 122 /* 123 * Make a copy of prov so that we don't lose fields if its module is 124 * unloaded but the provider isn't destroyed. This could happen with 125 * a provider that spans multiple modules. 126 */ 127 newprov = malloc(sizeof(*newprov), M_SDT, M_WAITOK | M_ZERO); 128 newprov->name = strdup(prov->name, M_SDT); 129 prov->sdt_refs = newprov->sdt_refs = 1; 130 131 TAILQ_INSERT_TAIL(&sdt_prov_list, newprov, prov_entry); 132 133 (void)dtrace_register(newprov->name, &sdt_attr, DTRACE_PRIV_USER, NULL, 134 &sdt_pops, NULL, (dtrace_provider_id_t *)&newprov->id); 135 prov->id = newprov->id; 136 } 137 138 static void 139 sdt_create_probe(struct sdt_probe *probe) 140 { 141 struct sdt_provider *prov; 142 char mod[DTRACE_MODNAMELEN]; 143 char func[DTRACE_FUNCNAMELEN]; 144 char name[DTRACE_NAMELEN]; 145 const char *from; 146 char *to; 147 size_t len; 148 int aframes; 149 150 if (probe->version != (int)sizeof(*probe)) { 151 printf("ignoring probe %p, version %u expected %u\n", 152 probe, probe->version, (int)sizeof(*probe)); 153 return; 154 } 155 156 TAILQ_FOREACH(prov, &sdt_prov_list, prov_entry) 157 if (strcmp(prov->name, probe->prov->name) == 0) 158 break; 159 160 KASSERT(prov != NULL, ("probe defined without a provider")); 161 162 /* If no module name was specified, use the module filename. */ 163 if (*probe->mod == 0) { 164 len = strlcpy(mod, probe->sdtp_lf->filename, sizeof(mod)); 165 if (len > 3 && strcmp(mod + len - 3, ".ko") == 0) 166 mod[len - 3] = '\0'; 167 } else 168 strlcpy(mod, probe->mod, sizeof(mod)); 169 170 /* 171 * Unfortunately this is necessary because the Solaris DTrace 172 * code mixes consts and non-consts with casts to override 173 * the incompatibilies. On FreeBSD, we use strict warnings 174 * in the C compiler, so we have to respect const vs non-const. 175 */ 176 strlcpy(func, probe->func, sizeof(func)); 177 if (func[0] == '\0') 178 strcpy(func, "none"); 179 180 from = probe->name; 181 to = name; 182 for (len = 0; len < (sizeof(name) - 1) && *from != '\0'; 183 len++, from++, to++) { 184 if (from[0] == '_' && from[1] == '_') { 185 *to = '-'; 186 from++; 187 } else 188 *to = *from; 189 } 190 *to = '\0'; 191 192 if (dtrace_probe_lookup(prov->id, mod, func, name) != DTRACE_IDNONE) 193 return; 194 195 aframes = 1; /* unwind past sdt_probe() */ 196 if (strcmp(prov->name, "lockstat") == 0) { 197 /* 198 * Locking primitives instrumented by lockstat automatically 199 * disable inlining. Step forward an extra frame so that DTrace 200 * variables like "caller" provide the function trying to 201 * acquire or release the lock rather than an internal function. 202 */ 203 aframes++; 204 } 205 (void)dtrace_probe_create(prov->id, mod, func, name, aframes, probe); 206 } 207 208 static void 209 sdt_init_probe(struct sdt_probe *probe, linker_file_t lf) 210 { 211 probe->sdtp_lf = lf; 212 TAILQ_INIT(&probe->argtype_list); 213 STAILQ_INIT(&probe->tracepoint_list); 214 } 215 216 /* 217 * Probes are created through the SDT module load/unload hook, so this function 218 * has nothing to do. It only exists because the DTrace provider framework 219 * requires one of provide_probes and provide_module to be defined. 220 */ 221 static void 222 sdt_provide_probes(void *arg, dtrace_probedesc_t *desc) 223 { 224 } 225 226 struct sdt_enable_cb_arg { 227 struct sdt_probe *probe; 228 int cpu; 229 int arrived; 230 int done; 231 bool enable; 232 }; 233 234 static void 235 sdt_probe_update_cb(void *_arg) 236 { 237 struct sdt_enable_cb_arg *arg; 238 struct sdt_tracepoint *tp; 239 240 arg = _arg; 241 if (arg->cpu != curcpu) { 242 atomic_add_rel_int(&arg->arrived, 1); 243 while (atomic_load_acq_int(&arg->done) == 0) 244 cpu_spinwait(); 245 return; 246 } else { 247 while (atomic_load_acq_int(&arg->arrived) != mp_ncpus - 1) 248 cpu_spinwait(); 249 } 250 251 STAILQ_FOREACH(tp, &arg->probe->tracepoint_list, tracepoint_entry) { 252 if (arg->enable) 253 sdt_tracepoint_patch(tp->patchpoint, tp->target); 254 else 255 sdt_tracepoint_restore(tp->patchpoint); 256 } 257 258 atomic_store_rel_int(&arg->done, 1); 259 } 260 261 static void 262 sdt_probe_update(struct sdt_probe *probe, bool enable) 263 { 264 struct sdt_enable_cb_arg cbarg; 265 266 sched_pin(); 267 cbarg.probe = probe; 268 cbarg.cpu = curcpu; 269 atomic_store_rel_int(&cbarg.arrived, 0); 270 atomic_store_rel_int(&cbarg.done, 0); 271 cbarg.enable = enable; 272 smp_rendezvous(NULL, sdt_probe_update_cb, NULL, &cbarg); 273 sched_unpin(); 274 } 275 276 static void 277 sdt_enable(void *arg __unused, dtrace_id_t id, void *parg) 278 { 279 struct sdt_probe *probe; 280 281 probe = parg; 282 283 probe->id = id; 284 probe->sdtp_lf->nenabled++; 285 if (strcmp(probe->prov->name, "lockstat") == 0) { 286 lockstat_enabled_count++; 287 if (lockstat_enabled_count == 1) 288 lockstat_enabled = true; 289 } 290 sdt_probes_enabled_count++; 291 if (sdt_probes_enabled_count == 1) 292 sdt_probes_enabled = true; 293 294 sdt_probe_update(probe, true); 295 } 296 297 static void 298 sdt_disable(void *arg __unused, dtrace_id_t id, void *parg) 299 { 300 struct sdt_probe *probe; 301 302 probe = parg; 303 KASSERT(probe->sdtp_lf->nenabled > 0, ("no probes enabled")); 304 305 sdt_probe_update(probe, false); 306 307 sdt_probes_enabled_count--; 308 if (sdt_probes_enabled_count == 0) 309 sdt_probes_enabled = false; 310 if (strcmp(probe->prov->name, "lockstat") == 0) { 311 lockstat_enabled_count--; 312 if (lockstat_enabled_count == 0) 313 lockstat_enabled = false; 314 } 315 probe->id = 0; 316 probe->sdtp_lf->nenabled--; 317 } 318 319 static void 320 sdt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc) 321 { 322 struct sdt_argtype *argtype; 323 struct sdt_probe *probe = parg; 324 325 if (desc->dtargd_ndx >= probe->n_args) { 326 desc->dtargd_ndx = DTRACE_ARGNONE; 327 return; 328 } 329 330 TAILQ_FOREACH(argtype, &probe->argtype_list, argtype_entry) { 331 if (desc->dtargd_ndx == argtype->ndx) { 332 desc->dtargd_mapping = desc->dtargd_ndx; 333 if (argtype->type == NULL) { 334 desc->dtargd_native[0] = '\0'; 335 desc->dtargd_xlate[0] = '\0'; 336 continue; 337 } 338 strlcpy(desc->dtargd_native, argtype->type, 339 sizeof(desc->dtargd_native)); 340 if (argtype->xtype != NULL) 341 strlcpy(desc->dtargd_xlate, argtype->xtype, 342 sizeof(desc->dtargd_xlate)); 343 } 344 } 345 } 346 347 /* 348 * Fetch arguments beyond the first five passed directly to dtrace_probe(). 349 * FreeBSD's SDT implement currently only supports up to 6 arguments, so we just 350 * need to handle arg5 here. 351 */ 352 static uint64_t 353 sdt_getargval(void *arg __unused, dtrace_id_t id __unused, 354 void *parg __unused, int argno, int aframes __unused) 355 { 356 if (argno != 5) { 357 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 358 return (0); 359 } else { 360 return (curthread->t_dtrace_sdt_arg[argno - 5]); 361 } 362 } 363 364 static void 365 sdt_destroy(void *arg, dtrace_id_t id, void *parg) 366 { 367 } 368 369 static void 370 sdt_kld_load_providers(struct linker_file *lf) 371 { 372 struct sdt_provider **prov, **begin, **end; 373 struct sdt_probe **p_begin, **p_end; 374 375 if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end, 376 NULL) == 0) { 377 for (prov = begin; prov < end; prov++) 378 sdt_create_provider(*prov); 379 } 380 381 if (linker_file_lookup_set(lf, "sdt_probes_set", &p_begin, &p_end, 382 NULL) == 0) { 383 for (struct sdt_probe **probe = p_begin; probe < p_end; probe++) 384 sdt_init_probe(*probe, lf); 385 } 386 } 387 388 static void 389 sdt_kld_load_probes(struct linker_file *lf) 390 { 391 struct sdt_probe **p_begin, **p_end; 392 struct sdt_argtype **a_begin, **a_end; 393 struct sdt_tracepoint *tp_begin, *tp_end; 394 395 if (linker_file_lookup_set(lf, "sdt_probes_set", &p_begin, &p_end, 396 NULL) == 0) { 397 for (struct sdt_probe **probe = p_begin; probe < p_end; probe++) 398 sdt_create_probe(*probe); 399 } 400 401 if (linker_file_lookup_set(lf, "sdt_argtypes_set", &a_begin, &a_end, 402 NULL) == 0) { 403 for (struct sdt_argtype **argtype = a_begin; argtype < a_end; 404 argtype++) { 405 (*argtype)->probe->n_args++; 406 TAILQ_INSERT_TAIL(&(*argtype)->probe->argtype_list, 407 *argtype, argtype_entry); 408 } 409 } 410 411 if (linker_file_lookup_set(lf, __XSTRING(_SDT_TRACEPOINT_SET), 412 &tp_begin, &tp_end, NULL) == 0) { 413 for (struct sdt_tracepoint *tp = tp_begin; tp < tp_end; tp++) { 414 if (!sdt_tracepoint_valid(tp->patchpoint, tp->target)) { 415 printf( 416 "invalid tracepoint %#jx->%#jx for %s:%s:%s:%s\n", 417 (uintmax_t)tp->patchpoint, 418 (uintmax_t)tp->target, 419 tp->probe->prov->name, tp->probe->mod, 420 tp->probe->func, tp->probe->name); 421 continue; 422 } 423 STAILQ_INSERT_TAIL(&tp->probe->tracepoint_list, tp, 424 tracepoint_entry); 425 } 426 } 427 } 428 429 /* 430 * Called from the kernel linker when a module is loaded, before 431 * dtrace_module_loaded() is called. This is done so that it's possible to 432 * register new providers when modules are loaded. The DTrace framework 433 * explicitly disallows calling into the framework from the provide_module 434 * provider method, so we cannot do this there. 435 */ 436 static void 437 sdt_kld_load(void *arg __unused, struct linker_file *lf) 438 { 439 sdt_kld_load_providers(lf); 440 sdt_kld_load_probes(lf); 441 } 442 443 static bool 444 sdt_kld_unload_providers(struct linker_file *lf) 445 { 446 struct sdt_provider *prov, **curr, **begin, **end, *tmp; 447 448 if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end, 449 NULL)) 450 /* No DTrace providers are declared in this file. */ 451 return (true); 452 453 /* 454 * Go through all the providers declared in this linker file and 455 * unregister any that aren't declared in another loaded file. 456 */ 457 for (curr = begin; curr < end; curr++) { 458 TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) { 459 if (strcmp(prov->name, (*curr)->name) != 0) 460 continue; 461 462 if (prov->sdt_refs == 1) { 463 if (dtrace_unregister(prov->id) != 0) { 464 return (false); 465 } 466 TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry); 467 free(prov->name, M_SDT); 468 free(prov, M_SDT); 469 } else 470 prov->sdt_refs--; 471 break; 472 } 473 } 474 475 return (true); 476 } 477 478 static bool 479 sdt_kld_unload_probes(struct linker_file *lf) 480 { 481 struct sdt_probe **p_begin, **p_end; 482 struct sdt_argtype **a_begin, **a_end; 483 struct sdt_tracepoint *tp_begin, *tp_end; 484 485 if (linker_file_lookup_set(lf, __XSTRING(_SDT_TRACEPOINT_SET), 486 &tp_begin, &tp_end, NULL) == 0) { 487 for (struct sdt_tracepoint *tp = tp_begin; tp < tp_end; tp++) { 488 struct sdt_tracepoint *tp2; 489 490 if (!sdt_tracepoint_valid(tp->patchpoint, tp->target)) 491 continue; 492 493 /* Only remove the entry if it is in the list. */ 494 tp2 = STAILQ_FIRST(&tp->probe->tracepoint_list); 495 if (tp2 == tp) { 496 STAILQ_REMOVE_HEAD(&tp->probe->tracepoint_list, 497 tracepoint_entry); 498 } else if (tp2 != NULL) { 499 struct sdt_tracepoint *tp3; 500 501 for (;;) { 502 tp3 = STAILQ_NEXT(tp2, 503 tracepoint_entry); 504 if (tp3 == NULL) 505 break; 506 if (tp3 == tp) { 507 STAILQ_REMOVE_AFTER( 508 &tp->probe->tracepoint_list, 509 tp2, tracepoint_entry); 510 break; 511 } 512 tp2 = tp3; 513 } 514 } 515 } 516 } 517 518 if (linker_file_lookup_set(lf, "sdt_argtypes_set", &a_begin, &a_end, 519 NULL) == 0) { 520 for (struct sdt_argtype **argtype = a_begin; argtype < a_end; 521 argtype++) { 522 struct sdt_argtype *argtype2; 523 524 /* Only remove the entry if it is in the list. */ 525 TAILQ_FOREACH(argtype2, 526 &(*argtype)->probe->argtype_list, argtype_entry) { 527 if (argtype2 == *argtype) { 528 (*argtype)->probe->n_args--; 529 TAILQ_REMOVE( 530 &(*argtype)->probe->argtype_list, 531 *argtype, argtype_entry); 532 break; 533 } 534 } 535 } 536 } 537 538 if (linker_file_lookup_set(lf, "sdt_probes_set", &p_begin, &p_end, 539 NULL) == 0) { 540 for (struct sdt_probe **probe = p_begin; probe < p_end; 541 probe++) { 542 if ((*probe)->sdtp_lf == lf) { 543 if (!TAILQ_EMPTY(&(*probe)->argtype_list)) 544 return (false); 545 if (!STAILQ_EMPTY(&(*probe)->tracepoint_list)) 546 return (false); 547 548 /* 549 * Don't destroy the probe as there 550 * might be multiple instances of the 551 * same probe in different modules. 552 */ 553 } 554 } 555 } 556 557 return (true); 558 } 559 560 static void 561 sdt_kld_unload_try(void *arg __unused, struct linker_file *lf, int *error) 562 { 563 if (*error != 0) 564 /* We already have an error, so don't do anything. */ 565 return; 566 567 if (!sdt_kld_unload_probes(lf)) 568 *error = 1; 569 else if (!sdt_kld_unload_providers(lf)) 570 *error = 1; 571 } 572 573 static int 574 sdt_load_providers_cb(linker_file_t lf, void *arg __unused) 575 { 576 sdt_kld_load_providers(lf); 577 return (0); 578 } 579 580 static int 581 sdt_load_probes_cb(linker_file_t lf, void *arg __unused) 582 { 583 sdt_kld_load_probes(lf); 584 return (0); 585 } 586 587 static void 588 sdt_dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 589 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4, uintptr_t arg5) 590 { 591 curthread->t_dtrace_sdt_arg[0] = arg5; 592 dtrace_probe(id, arg0, arg1, arg2, arg3, arg4); 593 } 594 595 static void 596 sdt_load(void) 597 { 598 599 TAILQ_INIT(&sdt_prov_list); 600 601 sdt_probe_func = sdt_dtrace_probe; 602 603 sdt_kld_load_tag = EVENTHANDLER_REGISTER(kld_load, sdt_kld_load, NULL, 604 EVENTHANDLER_PRI_ANY); 605 sdt_kld_unload_try_tag = EVENTHANDLER_REGISTER(kld_unload_try, 606 sdt_kld_unload_try, NULL, EVENTHANDLER_PRI_ANY); 607 608 /* 609 * Pick up probes from the kernel and already-loaded linker files. 610 * Define providers in a separate pass since a linker file may be using 611 * providers defined in a file that appears later in the list. 612 */ 613 linker_file_foreach(sdt_load_providers_cb, NULL); 614 linker_file_foreach(sdt_load_probes_cb, NULL); 615 } 616 617 static int 618 sdt_unload(void) 619 { 620 struct sdt_provider *prov, *tmp; 621 int ret; 622 623 EVENTHANDLER_DEREGISTER(kld_load, sdt_kld_load_tag); 624 EVENTHANDLER_DEREGISTER(kld_unload_try, sdt_kld_unload_try_tag); 625 626 sdt_probe_func = sdt_probe_stub; 627 628 TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) { 629 ret = dtrace_unregister(prov->id); 630 if (ret != 0) 631 return (ret); 632 TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry); 633 free(prov->name, M_SDT); 634 free(prov, M_SDT); 635 } 636 637 return (0); 638 } 639 640 static int 641 sdt_modevent(module_t mod __unused, int type, void *data __unused) 642 { 643 switch (type) { 644 case MOD_LOAD: 645 case MOD_UNLOAD: 646 case MOD_SHUTDOWN: 647 return (0); 648 default: 649 return (EOPNOTSUPP); 650 } 651 } 652 653 SYSINIT(sdt_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_load, NULL); 654 SYSUNINIT(sdt_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_unload, NULL); 655 656 DEV_MODULE(sdt, sdt_modevent, NULL); 657 MODULE_VERSION(sdt, 1); 658 MODULE_DEPEND(sdt, dtrace, 1, 1, 1); 659 MODULE_DEPEND(sdt, opensolaris, 1, 1, 1); 660