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_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 = sdt_getargval, 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 int aframes; 148 149 if (probe->version != (int)sizeof(*probe)) { 150 printf("ignoring probe %p, version %u expected %u\n", 151 probe, probe->version, (int)sizeof(*probe)); 152 return; 153 } 154 155 TAILQ_FOREACH(prov, &sdt_prov_list, prov_entry) 156 if (strcmp(prov->name, probe->prov->name) == 0) 157 break; 158 159 KASSERT(prov != NULL, ("probe defined without a provider")); 160 161 /* If no module name was specified, use the module filename. */ 162 if (*probe->mod == 0) { 163 len = strlcpy(mod, probe->sdtp_lf->filename, sizeof(mod)); 164 if (len > 3 && strcmp(mod + len - 3, ".ko") == 0) 165 mod[len - 3] = '\0'; 166 } else 167 strlcpy(mod, probe->mod, sizeof(mod)); 168 169 /* 170 * Unfortunately this is necessary because the Solaris DTrace 171 * code mixes consts and non-consts with casts to override 172 * the incompatibilies. On FreeBSD, we use strict warnings 173 * in the C compiler, so we have to respect const vs non-const. 174 */ 175 strlcpy(func, probe->func, sizeof(func)); 176 if (func[0] == '\0') 177 strcpy(func, "none"); 178 179 from = probe->name; 180 to = name; 181 for (len = 0; len < (sizeof(name) - 1) && *from != '\0'; 182 len++, from++, to++) { 183 if (from[0] == '_' && from[1] == '_') { 184 *to = '-'; 185 from++; 186 } else 187 *to = *from; 188 } 189 *to = '\0'; 190 191 if (dtrace_probe_lookup(prov->id, mod, func, name) != DTRACE_IDNONE) 192 return; 193 194 aframes = 1; /* unwind past sdt_probe() */ 195 if (strcmp(prov->name, "lockstat") == 0) { 196 /* 197 * Locking primitives instrumented by lockstat automatically 198 * disable inlining. Step forward an extra frame so that DTrace 199 * variables like "caller" provide the function trying to 200 * acquire or release the lock rather than an internal function. 201 */ 202 aframes++; 203 } 204 (void)dtrace_probe_create(prov->id, mod, func, name, aframes, probe); 205 } 206 207 /* 208 * Probes are created through the SDT module load/unload hook, so this function 209 * has nothing to do. It only exists because the DTrace provider framework 210 * requires one of provide_probes and provide_module to be defined. 211 */ 212 static void 213 sdt_provide_probes(void *arg, dtrace_probedesc_t *desc) 214 { 215 } 216 217 struct sdt_enable_cb_arg { 218 struct sdt_probe *probe; 219 int cpu; 220 int arrived; 221 int done; 222 bool enable; 223 }; 224 225 static void 226 sdt_probe_update_cb(void *_arg) 227 { 228 struct sdt_enable_cb_arg *arg; 229 struct sdt_tracepoint *tp; 230 231 arg = _arg; 232 if (arg->cpu != curcpu) { 233 atomic_add_rel_int(&arg->arrived, 1); 234 while (atomic_load_acq_int(&arg->done) == 0) 235 cpu_spinwait(); 236 return; 237 } else { 238 while (atomic_load_acq_int(&arg->arrived) != mp_ncpus - 1) 239 cpu_spinwait(); 240 } 241 242 STAILQ_FOREACH(tp, &arg->probe->tracepoint_list, tracepoint_entry) { 243 if (arg->enable) 244 sdt_tracepoint_patch(tp->patchpoint, tp->target); 245 else 246 sdt_tracepoint_restore(tp->patchpoint); 247 } 248 249 atomic_store_rel_int(&arg->done, 1); 250 } 251 252 static void 253 sdt_probe_update(struct sdt_probe *probe, bool enable) 254 { 255 struct sdt_enable_cb_arg cbarg; 256 257 sched_pin(); 258 cbarg.probe = probe; 259 cbarg.cpu = curcpu; 260 atomic_store_rel_int(&cbarg.arrived, 0); 261 atomic_store_rel_int(&cbarg.done, 0); 262 cbarg.enable = enable; 263 smp_rendezvous(NULL, sdt_probe_update_cb, NULL, &cbarg); 264 sched_unpin(); 265 } 266 267 static void 268 sdt_enable(void *arg __unused, dtrace_id_t id, void *parg) 269 { 270 struct sdt_probe *probe; 271 272 probe = parg; 273 274 probe->id = id; 275 probe->sdtp_lf->nenabled++; 276 if (strcmp(probe->prov->name, "lockstat") == 0) { 277 lockstat_enabled_count++; 278 if (lockstat_enabled_count == 1) 279 lockstat_enabled = true; 280 } 281 sdt_probes_enabled_count++; 282 if (sdt_probes_enabled_count == 1) 283 sdt_probes_enabled = true; 284 285 sdt_probe_update(probe, true); 286 } 287 288 static void 289 sdt_disable(void *arg __unused, dtrace_id_t id, void *parg) 290 { 291 struct sdt_probe *probe; 292 293 probe = parg; 294 KASSERT(probe->sdtp_lf->nenabled > 0, ("no probes enabled")); 295 296 sdt_probe_update(probe, false); 297 298 sdt_probes_enabled_count--; 299 if (sdt_probes_enabled_count == 0) 300 sdt_probes_enabled = false; 301 if (strcmp(probe->prov->name, "lockstat") == 0) { 302 lockstat_enabled_count--; 303 if (lockstat_enabled_count == 0) 304 lockstat_enabled = false; 305 } 306 probe->id = 0; 307 probe->sdtp_lf->nenabled--; 308 } 309 310 static void 311 sdt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc) 312 { 313 struct sdt_argtype *argtype; 314 struct sdt_probe *probe = parg; 315 316 if (desc->dtargd_ndx >= probe->n_args) { 317 desc->dtargd_ndx = DTRACE_ARGNONE; 318 return; 319 } 320 321 TAILQ_FOREACH(argtype, &probe->argtype_list, argtype_entry) { 322 if (desc->dtargd_ndx == argtype->ndx) { 323 desc->dtargd_mapping = desc->dtargd_ndx; 324 if (argtype->type == NULL) { 325 desc->dtargd_native[0] = '\0'; 326 desc->dtargd_xlate[0] = '\0'; 327 continue; 328 } 329 strlcpy(desc->dtargd_native, argtype->type, 330 sizeof(desc->dtargd_native)); 331 if (argtype->xtype != NULL) 332 strlcpy(desc->dtargd_xlate, argtype->xtype, 333 sizeof(desc->dtargd_xlate)); 334 } 335 } 336 } 337 338 /* 339 * Fetch arguments beyond the first five passed directly to dtrace_probe(). 340 * FreeBSD's SDT implement currently only supports up to 6 arguments, so we just 341 * need to handle arg5 here. 342 */ 343 static uint64_t 344 sdt_getargval(void *arg __unused, dtrace_id_t id __unused, 345 void *parg __unused, int argno, int aframes __unused) 346 { 347 if (argno != 5) { 348 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 349 return (0); 350 } else { 351 return (curthread->t_dtrace_sdt_arg[argno - 5]); 352 } 353 } 354 355 static void 356 sdt_destroy(void *arg, dtrace_id_t id, void *parg) 357 { 358 } 359 360 static void 361 sdt_kld_load_providers(struct linker_file *lf) 362 { 363 struct sdt_provider **prov, **begin, **end; 364 365 if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end, 366 NULL) == 0) { 367 for (prov = begin; prov < end; prov++) 368 sdt_create_provider(*prov); 369 } 370 } 371 372 static void 373 sdt_kld_load_probes(struct linker_file *lf) 374 { 375 struct sdt_probe **p_begin, **p_end; 376 struct sdt_argtype **a_begin, **a_end; 377 struct sdt_tracepoint *tp_begin, *tp_end; 378 379 if (linker_file_lookup_set(lf, "sdt_probes_set", &p_begin, &p_end, 380 NULL) == 0) { 381 for (struct sdt_probe **probe = p_begin; probe < p_end; 382 probe++) { 383 (*probe)->sdtp_lf = lf; 384 sdt_create_probe(*probe); 385 TAILQ_INIT(&(*probe)->argtype_list); 386 STAILQ_INIT(&(*probe)->tracepoint_list); 387 } 388 } 389 390 if (linker_file_lookup_set(lf, "sdt_argtypes_set", &a_begin, &a_end, 391 NULL) == 0) { 392 for (struct sdt_argtype **argtype = a_begin; argtype < a_end; 393 argtype++) { 394 (*argtype)->probe->n_args++; 395 TAILQ_INSERT_TAIL(&(*argtype)->probe->argtype_list, 396 *argtype, argtype_entry); 397 } 398 } 399 400 if (linker_file_lookup_set(lf, __XSTRING(_SDT_TRACEPOINT_SET), 401 &tp_begin, &tp_end, NULL) == 0) { 402 for (struct sdt_tracepoint *tp = tp_begin; tp < tp_end; tp++) { 403 if (!sdt_tracepoint_valid(tp->patchpoint, tp->target)) { 404 printf( 405 "invalid tracepoint %#jx->%#jx for %s:%s:%s:%s\n", 406 (uintmax_t)tp->patchpoint, 407 (uintmax_t)tp->target, 408 tp->probe->prov->name, tp->probe->mod, 409 tp->probe->func, tp->probe->name); 410 continue; 411 } 412 STAILQ_INSERT_TAIL(&tp->probe->tracepoint_list, tp, 413 tracepoint_entry); 414 } 415 } 416 } 417 418 /* 419 * Called from the kernel linker when a module is loaded, before 420 * dtrace_module_loaded() is called. This is done so that it's possible to 421 * register new providers when modules are loaded. The DTrace framework 422 * explicitly disallows calling into the framework from the provide_module 423 * provider method, so we cannot do this there. 424 */ 425 static void 426 sdt_kld_load(void *arg __unused, struct linker_file *lf) 427 { 428 sdt_kld_load_providers(lf); 429 sdt_kld_load_probes(lf); 430 } 431 432 static void 433 sdt_kld_unload_try(void *arg __unused, struct linker_file *lf, int *error) 434 { 435 struct sdt_provider *prov, **curr, **begin, **end, *tmp; 436 437 if (*error != 0) 438 /* We already have an error, so don't do anything. */ 439 return; 440 else if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end, 441 NULL)) 442 /* No DTrace providers are declared in this file. */ 443 return; 444 445 /* 446 * Go through all the providers declared in this linker file and 447 * unregister any that aren't declared in another loaded file. 448 */ 449 for (curr = begin; curr < end; curr++) { 450 TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) { 451 if (strcmp(prov->name, (*curr)->name) != 0) 452 continue; 453 454 if (prov->sdt_refs == 1) { 455 if (dtrace_unregister(prov->id) != 0) { 456 *error = 1; 457 return; 458 } 459 TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry); 460 free(prov->name, M_SDT); 461 free(prov, M_SDT); 462 } else 463 prov->sdt_refs--; 464 break; 465 } 466 } 467 } 468 469 static int 470 sdt_load_providers_cb(linker_file_t lf, void *arg __unused) 471 { 472 sdt_kld_load_providers(lf); 473 return (0); 474 } 475 476 static int 477 sdt_load_probes_cb(linker_file_t lf, void *arg __unused) 478 { 479 sdt_kld_load_probes(lf); 480 return (0); 481 } 482 483 static void 484 sdt_dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 485 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4, uintptr_t arg5) 486 { 487 curthread->t_dtrace_sdt_arg[0] = arg5; 488 dtrace_probe(id, arg0, arg1, arg2, arg3, arg4); 489 } 490 491 static void 492 sdt_load(void) 493 { 494 495 TAILQ_INIT(&sdt_prov_list); 496 497 sdt_probe_func = sdt_dtrace_probe; 498 499 sdt_kld_load_tag = EVENTHANDLER_REGISTER(kld_load, sdt_kld_load, NULL, 500 EVENTHANDLER_PRI_ANY); 501 sdt_kld_unload_try_tag = EVENTHANDLER_REGISTER(kld_unload_try, 502 sdt_kld_unload_try, NULL, EVENTHANDLER_PRI_ANY); 503 504 /* 505 * Pick up probes from the kernel and already-loaded linker files. 506 * Define providers in a separate pass since a linker file may be using 507 * providers defined in a file that appears later in the list. 508 */ 509 linker_file_foreach(sdt_load_providers_cb, NULL); 510 linker_file_foreach(sdt_load_probes_cb, NULL); 511 } 512 513 static int 514 sdt_unload(void) 515 { 516 struct sdt_provider *prov, *tmp; 517 int ret; 518 519 EVENTHANDLER_DEREGISTER(kld_load, sdt_kld_load_tag); 520 EVENTHANDLER_DEREGISTER(kld_unload_try, sdt_kld_unload_try_tag); 521 522 sdt_probe_func = sdt_probe_stub; 523 524 TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) { 525 ret = dtrace_unregister(prov->id); 526 if (ret != 0) 527 return (ret); 528 TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry); 529 free(prov->name, M_SDT); 530 free(prov, M_SDT); 531 } 532 533 return (0); 534 } 535 536 static int 537 sdt_modevent(module_t mod __unused, int type, void *data __unused) 538 { 539 switch (type) { 540 case MOD_LOAD: 541 case MOD_UNLOAD: 542 case MOD_SHUTDOWN: 543 return (0); 544 default: 545 return (EOPNOTSUPP); 546 } 547 } 548 549 SYSINIT(sdt_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_load, NULL); 550 SYSUNINIT(sdt_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_unload, NULL); 551 552 DEV_MODULE(sdt, sdt_modevent, NULL); 553 MODULE_VERSION(sdt, 1); 554 MODULE_DEPEND(sdt, dtrace, 1, 1, 1); 555 MODULE_DEPEND(sdt, opensolaris, 1, 1, 1); 556