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