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 2010 The FreeBSD Foundation 22 * 23 * $FreeBSD$ 24 */ 25 26 /* 27 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 28 * Use is subject to license terms. 29 */ 30 31 /* 32 * Copyright (c) 2015, Joyent, Inc. All rights reserved. 33 */ 34 35 #include <sys/atomic.h> 36 #include <sys/errno.h> 37 #include <sys/stat.h> 38 #include <sys/modctl.h> 39 #include <sys/conf.h> 40 #include <sys/systm.h> 41 #ifdef illumos 42 #include <sys/ddi.h> 43 #endif 44 #include <sys/sunddi.h> 45 #include <sys/cpuvar.h> 46 #include <sys/kmem.h> 47 #ifdef illumos 48 #include <sys/strsubr.h> 49 #endif 50 #include <sys/fasttrap.h> 51 #include <sys/fasttrap_impl.h> 52 #include <sys/fasttrap_isa.h> 53 #include <sys/dtrace.h> 54 #include <sys/dtrace_impl.h> 55 #include <sys/sysmacros.h> 56 #include <sys/proc.h> 57 #include <sys/policy.h> 58 #ifdef illumos 59 #include <util/qsort.h> 60 #endif 61 #include <sys/mutex.h> 62 #include <sys/kernel.h> 63 #ifndef illumos 64 #include <sys/dtrace_bsd.h> 65 #include <sys/eventhandler.h> 66 #include <sys/rmlock.h> 67 #include <sys/sysent.h> 68 #include <sys/sysctl.h> 69 #include <sys/u8_textprep.h> 70 #include <sys/user.h> 71 72 #include <vm/vm.h> 73 #include <vm/pmap.h> 74 #include <vm/vm_map.h> 75 #include <vm/vm_param.h> 76 77 #include <cddl/dev/dtrace/dtrace_cddl.h> 78 #endif 79 80 /* 81 * User-Land Trap-Based Tracing 82 * ---------------------------- 83 * 84 * The fasttrap provider allows DTrace consumers to instrument any user-level 85 * instruction to gather data; this includes probes with semantic 86 * signifigance like entry and return as well as simple offsets into the 87 * function. While the specific techniques used are very ISA specific, the 88 * methodology is generalizable to any architecture. 89 * 90 * 91 * The General Methodology 92 * ----------------------- 93 * 94 * With the primary goal of tracing every user-land instruction and the 95 * limitation that we can't trust user space so don't want to rely on much 96 * information there, we begin by replacing the instructions we want to trace 97 * with trap instructions. Each instruction we overwrite is saved into a hash 98 * table keyed by process ID and pc address. When we enter the kernel due to 99 * this trap instruction, we need the effects of the replaced instruction to 100 * appear to have occurred before we proceed with the user thread's 101 * execution. 102 * 103 * Each user level thread is represented by a ulwp_t structure which is 104 * always easily accessible through a register. The most basic way to produce 105 * the effects of the instruction we replaced is to copy that instruction out 106 * to a bit of scratch space reserved in the user thread's ulwp_t structure 107 * (a sort of kernel-private thread local storage), set the PC to that 108 * scratch space and single step. When we reenter the kernel after single 109 * stepping the instruction we must then adjust the PC to point to what would 110 * normally be the next instruction. Of course, special care must be taken 111 * for branches and jumps, but these represent such a small fraction of any 112 * instruction set that writing the code to emulate these in the kernel is 113 * not too difficult. 114 * 115 * Return probes may require several tracepoints to trace every return site, 116 * and, conversely, each tracepoint may activate several probes (the entry 117 * and offset 0 probes, for example). To solve this muliplexing problem, 118 * tracepoints contain lists of probes to activate and probes contain lists 119 * of tracepoints to enable. If a probe is activated, it adds its ID to 120 * existing tracepoints or creates new ones as necessary. 121 * 122 * Most probes are activated _before_ the instruction is executed, but return 123 * probes are activated _after_ the effects of the last instruction of the 124 * function are visible. Return probes must be fired _after_ we have 125 * single-stepped the instruction whereas all other probes are fired 126 * beforehand. 127 * 128 * 129 * Lock Ordering 130 * ------------- 131 * 132 * The lock ordering below -- both internally and with respect to the DTrace 133 * framework -- is a little tricky and bears some explanation. Each provider 134 * has a lock (ftp_mtx) that protects its members including reference counts 135 * for enabled probes (ftp_rcount), consumers actively creating probes 136 * (ftp_ccount) and USDT consumers (ftp_mcount); all three prevent a provider 137 * from being freed. A provider is looked up by taking the bucket lock for the 138 * provider hash table, and is returned with its lock held. The provider lock 139 * may be taken in functions invoked by the DTrace framework, but may not be 140 * held while calling functions in the DTrace framework. 141 * 142 * To ensure consistency over multiple calls to the DTrace framework, the 143 * creation lock (ftp_cmtx) should be held. Naturally, the creation lock may 144 * not be taken when holding the provider lock as that would create a cyclic 145 * lock ordering. In situations where one would naturally take the provider 146 * lock and then the creation lock, we instead up a reference count to prevent 147 * the provider from disappearing, drop the provider lock, and acquire the 148 * creation lock. 149 * 150 * Briefly: 151 * bucket lock before provider lock 152 * DTrace before provider lock 153 * creation lock before DTrace 154 * never hold the provider lock and creation lock simultaneously 155 */ 156 157 static d_open_t fasttrap_open; 158 static d_ioctl_t fasttrap_ioctl; 159 160 static struct cdevsw fasttrap_cdevsw = { 161 .d_version = D_VERSION, 162 .d_open = fasttrap_open, 163 .d_ioctl = fasttrap_ioctl, 164 .d_name = "fasttrap", 165 }; 166 static struct cdev *fasttrap_cdev; 167 static dtrace_meta_provider_id_t fasttrap_meta_id; 168 169 static struct proc *fasttrap_cleanup_proc; 170 static struct mtx fasttrap_cleanup_mtx; 171 static uint_t fasttrap_cleanup_work, fasttrap_cleanup_drain, fasttrap_cleanup_cv; 172 173 /* 174 * Generation count on modifications to the global tracepoint lookup table. 175 */ 176 static volatile uint64_t fasttrap_mod_gen; 177 178 /* 179 * When the fasttrap provider is loaded, fasttrap_max is set to either 180 * FASTTRAP_MAX_DEFAULT, or the value for fasttrap-max-probes in the 181 * fasttrap.conf file (Illumos), or the value provied in the loader.conf (FreeBSD). 182 * Each time a probe is created, fasttrap_total is incremented by the number 183 * of tracepoints that may be associated with that probe; fasttrap_total is capped 184 * at fasttrap_max. 185 */ 186 #define FASTTRAP_MAX_DEFAULT 250000 187 static uint32_t fasttrap_max = FASTTRAP_MAX_DEFAULT; 188 static uint32_t fasttrap_total; 189 190 /* 191 * Copyright (c) 2011, Joyent, Inc. All rights reserved. 192 */ 193 194 #define FASTTRAP_TPOINTS_DEFAULT_SIZE 0x4000 195 #define FASTTRAP_PROVIDERS_DEFAULT_SIZE 0x100 196 #define FASTTRAP_PROCS_DEFAULT_SIZE 0x100 197 198 #define FASTTRAP_PID_NAME "pid" 199 200 fasttrap_hash_t fasttrap_tpoints; 201 static fasttrap_hash_t fasttrap_provs; 202 static fasttrap_hash_t fasttrap_procs; 203 204 static uint64_t fasttrap_pid_count; /* pid ref count */ 205 static kmutex_t fasttrap_count_mtx; /* lock on ref count */ 206 207 #define FASTTRAP_ENABLE_FAIL 1 208 #define FASTTRAP_ENABLE_PARTIAL 2 209 210 static int fasttrap_tracepoint_enable(proc_t *, fasttrap_probe_t *, uint_t); 211 static void fasttrap_tracepoint_disable(proc_t *, fasttrap_probe_t *, uint_t); 212 213 static fasttrap_provider_t *fasttrap_provider_lookup(pid_t, const char *, 214 const dtrace_pattr_t *); 215 static void fasttrap_provider_retire(pid_t, const char *, int); 216 static void fasttrap_provider_free(fasttrap_provider_t *); 217 218 static fasttrap_proc_t *fasttrap_proc_lookup(pid_t); 219 static void fasttrap_proc_release(fasttrap_proc_t *); 220 221 #ifndef illumos 222 static void fasttrap_thread_dtor(void *, struct thread *); 223 #endif 224 225 #define FASTTRAP_PROVS_INDEX(pid, name) \ 226 ((fasttrap_hash_str(name) + (pid)) & fasttrap_provs.fth_mask) 227 228 #define FASTTRAP_PROCS_INDEX(pid) ((pid) & fasttrap_procs.fth_mask) 229 230 #ifndef illumos 231 struct rmlock fasttrap_tp_lock; 232 static eventhandler_tag fasttrap_thread_dtor_tag; 233 #endif 234 235 static unsigned long tpoints_hash_size = FASTTRAP_TPOINTS_DEFAULT_SIZE; 236 237 #ifdef __FreeBSD__ 238 SYSCTL_DECL(_kern_dtrace); 239 SYSCTL_NODE(_kern_dtrace, OID_AUTO, fasttrap, CTLFLAG_RD, 0, "DTrace fasttrap parameters"); 240 SYSCTL_UINT(_kern_dtrace_fasttrap, OID_AUTO, max_probes, CTLFLAG_RWTUN, &fasttrap_max, 241 FASTTRAP_MAX_DEFAULT, "Maximum number of fasttrap probes"); 242 SYSCTL_ULONG(_kern_dtrace_fasttrap, OID_AUTO, tpoints_hash_size, CTLFLAG_RDTUN, &tpoints_hash_size, 243 FASTTRAP_TPOINTS_DEFAULT_SIZE, "Size of the tracepoint hash table"); 244 #endif 245 246 static int 247 fasttrap_highbit(ulong_t i) 248 { 249 int h = 1; 250 251 if (i == 0) 252 return (0); 253 #ifdef _LP64 254 if (i & 0xffffffff00000000ul) { 255 h += 32; i >>= 32; 256 } 257 #endif 258 if (i & 0xffff0000) { 259 h += 16; i >>= 16; 260 } 261 if (i & 0xff00) { 262 h += 8; i >>= 8; 263 } 264 if (i & 0xf0) { 265 h += 4; i >>= 4; 266 } 267 if (i & 0xc) { 268 h += 2; i >>= 2; 269 } 270 if (i & 0x2) { 271 h += 1; 272 } 273 return (h); 274 } 275 276 static uint_t 277 fasttrap_hash_str(const char *p) 278 { 279 unsigned int g; 280 uint_t hval = 0; 281 282 while (*p) { 283 hval = (hval << 4) + *p++; 284 if ((g = (hval & 0xf0000000)) != 0) 285 hval ^= g >> 24; 286 hval &= ~g; 287 } 288 return (hval); 289 } 290 291 void 292 fasttrap_sigtrap(proc_t *p, kthread_t *t, uintptr_t pc) 293 { 294 ksiginfo_t ksi; 295 296 ksiginfo_init(&ksi); 297 ksi.ksi_signo = SIGTRAP; 298 ksi.ksi_code = TRAP_DTRACE; 299 ksi.ksi_addr = (caddr_t)pc; 300 PROC_LOCK(p); 301 (void)tdsendsignal(p, t, SIGTRAP, &ksi); 302 PROC_UNLOCK(p); 303 } 304 305 #ifndef illumos 306 /* 307 * Obtain a chunk of scratch space in the address space of the target process. 308 */ 309 fasttrap_scrspace_t * 310 fasttrap_scraddr(struct thread *td, fasttrap_proc_t *fprc) 311 { 312 fasttrap_scrblock_t *scrblk; 313 fasttrap_scrspace_t *scrspc; 314 struct proc *p; 315 vm_offset_t addr; 316 int error, i; 317 318 scrspc = NULL; 319 if (td->t_dtrace_sscr != NULL) { 320 /* If the thread already has scratch space, we're done. */ 321 scrspc = (fasttrap_scrspace_t *)td->t_dtrace_sscr; 322 return (scrspc); 323 } 324 325 p = td->td_proc; 326 327 mutex_enter(&fprc->ftpc_mtx); 328 if (LIST_EMPTY(&fprc->ftpc_fscr)) { 329 /* 330 * No scratch space is available, so we'll map a new scratch 331 * space block into the traced process' address space. 332 */ 333 addr = 0; 334 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, 335 FASTTRAP_SCRBLOCK_SIZE, 0, VMFS_ANY_SPACE, VM_PROT_ALL, 336 VM_PROT_ALL, 0); 337 if (error != KERN_SUCCESS) 338 goto done; 339 340 scrblk = malloc(sizeof(*scrblk), M_SOLARIS, M_WAITOK); 341 scrblk->ftsb_addr = addr; 342 LIST_INSERT_HEAD(&fprc->ftpc_scrblks, scrblk, ftsb_next); 343 344 /* 345 * Carve the block up into chunks and put them on the free list. 346 */ 347 for (i = 0; 348 i < FASTTRAP_SCRBLOCK_SIZE / FASTTRAP_SCRSPACE_SIZE; i++) { 349 scrspc = malloc(sizeof(*scrspc), M_SOLARIS, M_WAITOK); 350 scrspc->ftss_addr = addr + 351 i * FASTTRAP_SCRSPACE_SIZE; 352 LIST_INSERT_HEAD(&fprc->ftpc_fscr, scrspc, 353 ftss_next); 354 } 355 } 356 357 /* 358 * Take the first scratch chunk off the free list, put it on the 359 * allocated list, and return its address. 360 */ 361 scrspc = LIST_FIRST(&fprc->ftpc_fscr); 362 LIST_REMOVE(scrspc, ftss_next); 363 LIST_INSERT_HEAD(&fprc->ftpc_ascr, scrspc, ftss_next); 364 365 /* 366 * This scratch space is reserved for use by td until the thread exits. 367 */ 368 td->t_dtrace_sscr = scrspc; 369 370 done: 371 mutex_exit(&fprc->ftpc_mtx); 372 373 return (scrspc); 374 } 375 376 /* 377 * Return any allocated per-thread scratch space chunks back to the process' 378 * free list. 379 */ 380 static void 381 fasttrap_thread_dtor(void *arg __unused, struct thread *td) 382 { 383 fasttrap_bucket_t *bucket; 384 fasttrap_proc_t *fprc; 385 fasttrap_scrspace_t *scrspc; 386 pid_t pid; 387 388 if (td->t_dtrace_sscr == NULL) 389 return; 390 391 pid = td->td_proc->p_pid; 392 bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)]; 393 fprc = NULL; 394 395 /* Look up the fasttrap process handle for this process. */ 396 mutex_enter(&bucket->ftb_mtx); 397 for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) { 398 if (fprc->ftpc_pid == pid) { 399 mutex_enter(&fprc->ftpc_mtx); 400 mutex_exit(&bucket->ftb_mtx); 401 break; 402 } 403 } 404 if (fprc == NULL) { 405 mutex_exit(&bucket->ftb_mtx); 406 return; 407 } 408 409 scrspc = (fasttrap_scrspace_t *)td->t_dtrace_sscr; 410 LIST_REMOVE(scrspc, ftss_next); 411 LIST_INSERT_HEAD(&fprc->ftpc_fscr, scrspc, ftss_next); 412 413 mutex_exit(&fprc->ftpc_mtx); 414 } 415 #endif 416 417 /* 418 * This function ensures that no threads are actively using the memory 419 * associated with probes that were formerly live. 420 */ 421 static void 422 fasttrap_mod_barrier(uint64_t gen) 423 { 424 int i; 425 426 if (gen < fasttrap_mod_gen) 427 return; 428 429 fasttrap_mod_gen++; 430 431 #ifdef illumos 432 CPU_FOREACH(i) { 433 mutex_enter(&fasttrap_cpuc_pid_lock[i]); 434 mutex_exit(&fasttrap_cpuc_pid_lock[i]); 435 } 436 #else 437 rm_wlock(&fasttrap_tp_lock); 438 rm_wunlock(&fasttrap_tp_lock); 439 #endif 440 } 441 442 /* 443 * This function performs asynchronous cleanup of fasttrap providers. The 444 * Solaris implementation of this mechanism use a timeout that's activated in 445 * fasttrap_pid_cleanup(), but this doesn't work in FreeBSD: one may sleep while 446 * holding the DTrace mutexes, but it is unsafe to sleep in a callout handler. 447 * Thus we use a dedicated process to perform the cleanup when requested. 448 */ 449 /*ARGSUSED*/ 450 static void 451 fasttrap_pid_cleanup_cb(void *data) 452 { 453 fasttrap_provider_t **fpp, *fp; 454 fasttrap_bucket_t *bucket; 455 dtrace_provider_id_t provid; 456 int i, later = 0, rval; 457 458 mtx_lock(&fasttrap_cleanup_mtx); 459 while (!fasttrap_cleanup_drain || later > 0) { 460 fasttrap_cleanup_work = 0; 461 mtx_unlock(&fasttrap_cleanup_mtx); 462 463 later = 0; 464 465 /* 466 * Iterate over all the providers trying to remove the marked 467 * ones. If a provider is marked but not retired, we just 468 * have to take a crack at removing it -- it's no big deal if 469 * we can't. 470 */ 471 for (i = 0; i < fasttrap_provs.fth_nent; i++) { 472 bucket = &fasttrap_provs.fth_table[i]; 473 mutex_enter(&bucket->ftb_mtx); 474 fpp = (fasttrap_provider_t **)&bucket->ftb_data; 475 476 while ((fp = *fpp) != NULL) { 477 if (!fp->ftp_marked) { 478 fpp = &fp->ftp_next; 479 continue; 480 } 481 482 mutex_enter(&fp->ftp_mtx); 483 484 /* 485 * If this provider has consumers actively 486 * creating probes (ftp_ccount) or is a USDT 487 * provider (ftp_mcount), we can't unregister 488 * or even condense. 489 */ 490 if (fp->ftp_ccount != 0 || 491 fp->ftp_mcount != 0) { 492 mutex_exit(&fp->ftp_mtx); 493 fp->ftp_marked = 0; 494 continue; 495 } 496 497 if (!fp->ftp_retired || fp->ftp_rcount != 0) 498 fp->ftp_marked = 0; 499 500 mutex_exit(&fp->ftp_mtx); 501 502 /* 503 * If we successfully unregister this 504 * provider we can remove it from the hash 505 * chain and free the memory. If our attempt 506 * to unregister fails and this is a retired 507 * provider, increment our flag to try again 508 * pretty soon. If we've consumed more than 509 * half of our total permitted number of 510 * probes call dtrace_condense() to try to 511 * clean out the unenabled probes. 512 */ 513 provid = fp->ftp_provid; 514 if ((rval = dtrace_unregister(provid)) != 0) { 515 if (fasttrap_total > fasttrap_max / 2) 516 (void) dtrace_condense(provid); 517 518 if (rval == EAGAIN) 519 fp->ftp_marked = 1; 520 521 later += fp->ftp_marked; 522 fpp = &fp->ftp_next; 523 } else { 524 *fpp = fp->ftp_next; 525 fasttrap_provider_free(fp); 526 } 527 } 528 mutex_exit(&bucket->ftb_mtx); 529 } 530 mtx_lock(&fasttrap_cleanup_mtx); 531 532 /* 533 * If we were unable to retire a provider, try again after a 534 * second. This situation can occur in certain circumstances 535 * where providers cannot be unregistered even though they have 536 * no probes enabled because of an execution of dtrace -l or 537 * something similar. 538 */ 539 if (later > 0 || fasttrap_cleanup_work || 540 fasttrap_cleanup_drain) { 541 mtx_unlock(&fasttrap_cleanup_mtx); 542 pause("ftclean", hz); 543 mtx_lock(&fasttrap_cleanup_mtx); 544 } else 545 mtx_sleep(&fasttrap_cleanup_cv, &fasttrap_cleanup_mtx, 546 0, "ftcl", 0); 547 } 548 549 /* 550 * Wake up the thread in fasttrap_unload() now that we're done. 551 */ 552 wakeup(&fasttrap_cleanup_drain); 553 mtx_unlock(&fasttrap_cleanup_mtx); 554 555 kthread_exit(); 556 } 557 558 /* 559 * Activates the asynchronous cleanup mechanism. 560 */ 561 static void 562 fasttrap_pid_cleanup(void) 563 { 564 565 mtx_lock(&fasttrap_cleanup_mtx); 566 if (!fasttrap_cleanup_work) { 567 fasttrap_cleanup_work = 1; 568 wakeup(&fasttrap_cleanup_cv); 569 } 570 mtx_unlock(&fasttrap_cleanup_mtx); 571 } 572 573 /* 574 * This is called from cfork() via dtrace_fasttrap_fork(). The child 575 * process's address space is (roughly) a copy of the parent process's so 576 * we have to remove all the instrumentation we had previously enabled in the 577 * parent. 578 */ 579 static void 580 fasttrap_fork(proc_t *p, proc_t *cp) 581 { 582 #ifndef illumos 583 fasttrap_scrblock_t *scrblk; 584 fasttrap_proc_t *fprc = NULL; 585 #endif 586 pid_t ppid = p->p_pid; 587 int i; 588 589 ASSERT(curproc == p); 590 #ifdef illumos 591 ASSERT(p->p_proc_flag & P_PR_LOCK); 592 #else 593 PROC_LOCK_ASSERT(p, MA_OWNED); 594 #endif 595 #ifdef illumos 596 ASSERT(p->p_dtrace_count > 0); 597 #else 598 /* 599 * This check is purposely here instead of in kern_fork.c because, 600 * for legal resons, we cannot include the dtrace_cddl.h header 601 * inside kern_fork.c and insert if-clause there. 602 */ 603 if (p->p_dtrace_count == 0 && p->p_dtrace_helpers == NULL) 604 return; 605 #endif 606 607 ASSERT(cp->p_dtrace_count == 0); 608 609 /* 610 * This would be simpler and faster if we maintained per-process 611 * hash tables of enabled tracepoints. It could, however, potentially 612 * slow down execution of a tracepoint since we'd need to go 613 * through two levels of indirection. In the future, we should 614 * consider either maintaining per-process ancillary lists of 615 * enabled tracepoints or hanging a pointer to a per-process hash 616 * table of enabled tracepoints off the proc structure. 617 */ 618 619 /* 620 * We don't have to worry about the child process disappearing 621 * because we're in fork(). 622 */ 623 #ifdef illumos 624 mtx_lock_spin(&cp->p_slock); 625 sprlock_proc(cp); 626 mtx_unlock_spin(&cp->p_slock); 627 #else 628 /* 629 * fasttrap_tracepoint_remove() expects the child process to be 630 * unlocked and the VM then expects curproc to be unlocked. 631 */ 632 _PHOLD(cp); 633 PROC_UNLOCK(cp); 634 PROC_UNLOCK(p); 635 if (p->p_dtrace_count == 0) 636 goto dup_helpers; 637 #endif 638 639 /* 640 * Iterate over every tracepoint looking for ones that belong to the 641 * parent process, and remove each from the child process. 642 */ 643 for (i = 0; i < fasttrap_tpoints.fth_nent; i++) { 644 fasttrap_tracepoint_t *tp; 645 fasttrap_bucket_t *bucket = &fasttrap_tpoints.fth_table[i]; 646 647 mutex_enter(&bucket->ftb_mtx); 648 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 649 if (tp->ftt_pid == ppid && 650 tp->ftt_proc->ftpc_acount != 0) { 651 int ret = fasttrap_tracepoint_remove(cp, tp); 652 ASSERT(ret == 0); 653 654 /* 655 * The count of active providers can only be 656 * decremented (i.e. to zero) during exec, 657 * exit, and removal of a meta provider so it 658 * should be impossible to drop the count 659 * mid-fork. 660 */ 661 ASSERT(tp->ftt_proc->ftpc_acount != 0); 662 #ifndef illumos 663 fprc = tp->ftt_proc; 664 #endif 665 } 666 } 667 mutex_exit(&bucket->ftb_mtx); 668 669 #ifndef illumos 670 /* 671 * Unmap any scratch space inherited from the parent's address 672 * space. 673 */ 674 if (fprc != NULL) { 675 mutex_enter(&fprc->ftpc_mtx); 676 LIST_FOREACH(scrblk, &fprc->ftpc_scrblks, ftsb_next) { 677 vm_map_remove(&cp->p_vmspace->vm_map, 678 scrblk->ftsb_addr, 679 scrblk->ftsb_addr + FASTTRAP_SCRBLOCK_SIZE); 680 } 681 mutex_exit(&fprc->ftpc_mtx); 682 } 683 #endif 684 } 685 686 #ifdef illumos 687 mutex_enter(&cp->p_lock); 688 sprunlock(cp); 689 #else 690 dup_helpers: 691 if (p->p_dtrace_helpers != NULL) 692 dtrace_helpers_duplicate(p, cp); 693 PROC_LOCK(p); 694 PROC_LOCK(cp); 695 _PRELE(cp); 696 #endif 697 } 698 699 /* 700 * This is called from proc_exit() or from exec_common() if p_dtrace_probes 701 * is set on the proc structure to indicate that there is a pid provider 702 * associated with this process. 703 */ 704 static void 705 fasttrap_exec_exit(proc_t *p) 706 { 707 #ifndef illumos 708 struct thread *td; 709 #endif 710 711 #ifdef illumos 712 ASSERT(p == curproc); 713 #else 714 PROC_LOCK_ASSERT(p, MA_OWNED); 715 _PHOLD(p); 716 /* 717 * Since struct threads may be recycled, we cannot rely on t_dtrace_sscr 718 * fields to be zeroed by kdtrace_thread_ctor. Thus we must zero it 719 * ourselves when a process exits. 720 */ 721 FOREACH_THREAD_IN_PROC(p, td) 722 td->t_dtrace_sscr = NULL; 723 PROC_UNLOCK(p); 724 #endif 725 726 /* 727 * We clean up the pid provider for this process here; user-land 728 * static probes are handled by the meta-provider remove entry point. 729 */ 730 fasttrap_provider_retire(p->p_pid, FASTTRAP_PID_NAME, 0); 731 #ifndef illumos 732 if (p->p_dtrace_helpers) 733 dtrace_helpers_destroy(p); 734 PROC_LOCK(p); 735 _PRELE(p); 736 #endif 737 } 738 739 740 /*ARGSUSED*/ 741 static void 742 fasttrap_pid_provide(void *arg, dtrace_probedesc_t *desc) 743 { 744 /* 745 * There are no "default" pid probes. 746 */ 747 } 748 749 static int 750 fasttrap_tracepoint_enable(proc_t *p, fasttrap_probe_t *probe, uint_t index) 751 { 752 fasttrap_tracepoint_t *tp, *new_tp = NULL; 753 fasttrap_bucket_t *bucket; 754 fasttrap_id_t *id; 755 pid_t pid; 756 uintptr_t pc; 757 758 ASSERT(index < probe->ftp_ntps); 759 760 pid = probe->ftp_pid; 761 pc = probe->ftp_tps[index].fit_tp->ftt_pc; 762 id = &probe->ftp_tps[index].fit_id; 763 764 ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid); 765 766 #ifdef illumos 767 ASSERT(!(p->p_flag & SVFORK)); 768 #endif 769 770 /* 771 * Before we make any modifications, make sure we've imposed a barrier 772 * on the generation in which this probe was last modified. 773 */ 774 fasttrap_mod_barrier(probe->ftp_gen); 775 776 bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)]; 777 778 /* 779 * If the tracepoint has already been enabled, just add our id to the 780 * list of interested probes. This may be our second time through 781 * this path in which case we'll have constructed the tracepoint we'd 782 * like to install. If we can't find a match, and have an allocated 783 * tracepoint ready to go, enable that one now. 784 * 785 * A tracepoint whose process is defunct is also considered defunct. 786 */ 787 again: 788 mutex_enter(&bucket->ftb_mtx); 789 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 790 /* 791 * Note that it's safe to access the active count on the 792 * associated proc structure because we know that at least one 793 * provider (this one) will still be around throughout this 794 * operation. 795 */ 796 if (tp->ftt_pid != pid || tp->ftt_pc != pc || 797 tp->ftt_proc->ftpc_acount == 0) 798 continue; 799 800 /* 801 * Now that we've found a matching tracepoint, it would be 802 * a decent idea to confirm that the tracepoint is still 803 * enabled and the trap instruction hasn't been overwritten. 804 * Since this is a little hairy, we'll punt for now. 805 */ 806 807 /* 808 * This can't be the first interested probe. We don't have 809 * to worry about another thread being in the midst of 810 * deleting this tracepoint (which would be the only valid 811 * reason for a tracepoint to have no interested probes) 812 * since we're holding P_PR_LOCK for this process. 813 */ 814 ASSERT(tp->ftt_ids != NULL || tp->ftt_retids != NULL); 815 816 switch (id->fti_ptype) { 817 case DTFTP_ENTRY: 818 case DTFTP_OFFSETS: 819 case DTFTP_IS_ENABLED: 820 id->fti_next = tp->ftt_ids; 821 membar_producer(); 822 tp->ftt_ids = id; 823 membar_producer(); 824 break; 825 826 case DTFTP_RETURN: 827 case DTFTP_POST_OFFSETS: 828 id->fti_next = tp->ftt_retids; 829 membar_producer(); 830 tp->ftt_retids = id; 831 membar_producer(); 832 break; 833 834 default: 835 ASSERT(0); 836 } 837 838 mutex_exit(&bucket->ftb_mtx); 839 840 if (new_tp != NULL) { 841 new_tp->ftt_ids = NULL; 842 new_tp->ftt_retids = NULL; 843 } 844 845 return (0); 846 } 847 848 /* 849 * If we have a good tracepoint ready to go, install it now while 850 * we have the lock held and no one can screw with us. 851 */ 852 if (new_tp != NULL) { 853 int rc = 0; 854 855 new_tp->ftt_next = bucket->ftb_data; 856 membar_producer(); 857 bucket->ftb_data = new_tp; 858 membar_producer(); 859 mutex_exit(&bucket->ftb_mtx); 860 861 /* 862 * Activate the tracepoint in the ISA-specific manner. 863 * If this fails, we need to report the failure, but 864 * indicate that this tracepoint must still be disabled 865 * by calling fasttrap_tracepoint_disable(). 866 */ 867 if (fasttrap_tracepoint_install(p, new_tp) != 0) 868 rc = FASTTRAP_ENABLE_PARTIAL; 869 870 /* 871 * Increment the count of the number of tracepoints active in 872 * the victim process. 873 */ 874 #ifdef illumos 875 ASSERT(p->p_proc_flag & P_PR_LOCK); 876 #endif 877 p->p_dtrace_count++; 878 879 return (rc); 880 } 881 882 mutex_exit(&bucket->ftb_mtx); 883 884 /* 885 * Initialize the tracepoint that's been preallocated with the probe. 886 */ 887 new_tp = probe->ftp_tps[index].fit_tp; 888 889 ASSERT(new_tp->ftt_pid == pid); 890 ASSERT(new_tp->ftt_pc == pc); 891 ASSERT(new_tp->ftt_proc == probe->ftp_prov->ftp_proc); 892 ASSERT(new_tp->ftt_ids == NULL); 893 ASSERT(new_tp->ftt_retids == NULL); 894 895 switch (id->fti_ptype) { 896 case DTFTP_ENTRY: 897 case DTFTP_OFFSETS: 898 case DTFTP_IS_ENABLED: 899 id->fti_next = NULL; 900 new_tp->ftt_ids = id; 901 break; 902 903 case DTFTP_RETURN: 904 case DTFTP_POST_OFFSETS: 905 id->fti_next = NULL; 906 new_tp->ftt_retids = id; 907 break; 908 909 default: 910 ASSERT(0); 911 } 912 913 #ifdef __FreeBSD__ 914 if (SV_PROC_FLAG(p, SV_LP64)) 915 p->p_model = DATAMODEL_LP64; 916 else 917 p->p_model = DATAMODEL_ILP32; 918 #endif 919 920 /* 921 * If the ISA-dependent initialization goes to plan, go back to the 922 * beginning and try to install this freshly made tracepoint. 923 */ 924 if (fasttrap_tracepoint_init(p, new_tp, pc, id->fti_ptype) == 0) 925 goto again; 926 927 new_tp->ftt_ids = NULL; 928 new_tp->ftt_retids = NULL; 929 930 return (FASTTRAP_ENABLE_FAIL); 931 } 932 933 static void 934 fasttrap_tracepoint_disable(proc_t *p, fasttrap_probe_t *probe, uint_t index) 935 { 936 fasttrap_bucket_t *bucket; 937 fasttrap_provider_t *provider = probe->ftp_prov; 938 fasttrap_tracepoint_t **pp, *tp; 939 fasttrap_id_t *id, **idp = NULL; 940 pid_t pid; 941 uintptr_t pc; 942 943 ASSERT(index < probe->ftp_ntps); 944 945 pid = probe->ftp_pid; 946 pc = probe->ftp_tps[index].fit_tp->ftt_pc; 947 id = &probe->ftp_tps[index].fit_id; 948 949 ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid); 950 951 /* 952 * Find the tracepoint and make sure that our id is one of the 953 * ones registered with it. 954 */ 955 bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)]; 956 mutex_enter(&bucket->ftb_mtx); 957 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 958 if (tp->ftt_pid == pid && tp->ftt_pc == pc && 959 tp->ftt_proc == provider->ftp_proc) 960 break; 961 } 962 963 /* 964 * If we somehow lost this tracepoint, we're in a world of hurt. 965 */ 966 ASSERT(tp != NULL); 967 968 switch (id->fti_ptype) { 969 case DTFTP_ENTRY: 970 case DTFTP_OFFSETS: 971 case DTFTP_IS_ENABLED: 972 ASSERT(tp->ftt_ids != NULL); 973 idp = &tp->ftt_ids; 974 break; 975 976 case DTFTP_RETURN: 977 case DTFTP_POST_OFFSETS: 978 ASSERT(tp->ftt_retids != NULL); 979 idp = &tp->ftt_retids; 980 break; 981 982 default: 983 ASSERT(0); 984 } 985 986 while ((*idp)->fti_probe != probe) { 987 idp = &(*idp)->fti_next; 988 ASSERT(*idp != NULL); 989 } 990 991 id = *idp; 992 *idp = id->fti_next; 993 membar_producer(); 994 995 ASSERT(id->fti_probe == probe); 996 997 /* 998 * If there are other registered enablings of this tracepoint, we're 999 * all done, but if this was the last probe assocated with this 1000 * this tracepoint, we need to remove and free it. 1001 */ 1002 if (tp->ftt_ids != NULL || tp->ftt_retids != NULL) { 1003 1004 /* 1005 * If the current probe's tracepoint is in use, swap it 1006 * for an unused tracepoint. 1007 */ 1008 if (tp == probe->ftp_tps[index].fit_tp) { 1009 fasttrap_probe_t *tmp_probe; 1010 fasttrap_tracepoint_t **tmp_tp; 1011 uint_t tmp_index; 1012 1013 if (tp->ftt_ids != NULL) { 1014 tmp_probe = tp->ftt_ids->fti_probe; 1015 /* LINTED - alignment */ 1016 tmp_index = FASTTRAP_ID_INDEX(tp->ftt_ids); 1017 tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp; 1018 } else { 1019 tmp_probe = tp->ftt_retids->fti_probe; 1020 /* LINTED - alignment */ 1021 tmp_index = FASTTRAP_ID_INDEX(tp->ftt_retids); 1022 tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp; 1023 } 1024 1025 ASSERT(*tmp_tp != NULL); 1026 ASSERT(*tmp_tp != probe->ftp_tps[index].fit_tp); 1027 ASSERT((*tmp_tp)->ftt_ids == NULL); 1028 ASSERT((*tmp_tp)->ftt_retids == NULL); 1029 1030 probe->ftp_tps[index].fit_tp = *tmp_tp; 1031 *tmp_tp = tp; 1032 } 1033 1034 mutex_exit(&bucket->ftb_mtx); 1035 1036 /* 1037 * Tag the modified probe with the generation in which it was 1038 * changed. 1039 */ 1040 probe->ftp_gen = fasttrap_mod_gen; 1041 return; 1042 } 1043 1044 mutex_exit(&bucket->ftb_mtx); 1045 1046 /* 1047 * We can't safely remove the tracepoint from the set of active 1048 * tracepoints until we've actually removed the fasttrap instruction 1049 * from the process's text. We can, however, operate on this 1050 * tracepoint secure in the knowledge that no other thread is going to 1051 * be looking at it since we hold P_PR_LOCK on the process if it's 1052 * live or we hold the provider lock on the process if it's dead and 1053 * gone. 1054 */ 1055 1056 /* 1057 * We only need to remove the actual instruction if we're looking 1058 * at an existing process 1059 */ 1060 if (p != NULL) { 1061 /* 1062 * If we fail to restore the instruction we need to kill 1063 * this process since it's in a completely unrecoverable 1064 * state. 1065 */ 1066 if (fasttrap_tracepoint_remove(p, tp) != 0) 1067 fasttrap_sigtrap(p, NULL, pc); 1068 1069 /* 1070 * Decrement the count of the number of tracepoints active 1071 * in the victim process. 1072 */ 1073 #ifdef illumos 1074 ASSERT(p->p_proc_flag & P_PR_LOCK); 1075 #endif 1076 p->p_dtrace_count--; 1077 1078 atomic_add_rel_64(&p->p_fasttrap_tp_gen, 1); 1079 } 1080 1081 /* 1082 * Remove the probe from the hash table of active tracepoints. 1083 */ 1084 mutex_enter(&bucket->ftb_mtx); 1085 pp = (fasttrap_tracepoint_t **)&bucket->ftb_data; 1086 ASSERT(*pp != NULL); 1087 while (*pp != tp) { 1088 pp = &(*pp)->ftt_next; 1089 ASSERT(*pp != NULL); 1090 } 1091 1092 *pp = tp->ftt_next; 1093 membar_producer(); 1094 1095 mutex_exit(&bucket->ftb_mtx); 1096 1097 /* 1098 * Tag the modified probe with the generation in which it was changed. 1099 */ 1100 probe->ftp_gen = fasttrap_mod_gen; 1101 } 1102 1103 static void 1104 fasttrap_enable_callbacks(void) 1105 { 1106 /* 1107 * We don't have to play the rw lock game here because we're 1108 * providing something rather than taking something away -- 1109 * we can be sure that no threads have tried to follow this 1110 * function pointer yet. 1111 */ 1112 mutex_enter(&fasttrap_count_mtx); 1113 if (fasttrap_pid_count == 0) { 1114 ASSERT(dtrace_pid_probe_ptr == NULL); 1115 ASSERT(dtrace_return_probe_ptr == NULL); 1116 dtrace_pid_probe_ptr = &fasttrap_pid_probe; 1117 dtrace_return_probe_ptr = &fasttrap_return_probe; 1118 } 1119 ASSERT(dtrace_pid_probe_ptr == &fasttrap_pid_probe); 1120 ASSERT(dtrace_return_probe_ptr == &fasttrap_return_probe); 1121 fasttrap_pid_count++; 1122 mutex_exit(&fasttrap_count_mtx); 1123 } 1124 1125 static void 1126 fasttrap_disable_callbacks(void) 1127 { 1128 #ifdef illumos 1129 ASSERT(MUTEX_HELD(&cpu_lock)); 1130 #endif 1131 1132 1133 mutex_enter(&fasttrap_count_mtx); 1134 ASSERT(fasttrap_pid_count > 0); 1135 fasttrap_pid_count--; 1136 if (fasttrap_pid_count == 0) { 1137 #ifdef illumos 1138 cpu_t *cur, *cpu = CPU; 1139 1140 for (cur = cpu->cpu_next_onln; cur != cpu; 1141 cur = cur->cpu_next_onln) { 1142 rw_enter(&cur->cpu_ft_lock, RW_WRITER); 1143 } 1144 #endif 1145 dtrace_pid_probe_ptr = NULL; 1146 dtrace_return_probe_ptr = NULL; 1147 #ifdef illumos 1148 for (cur = cpu->cpu_next_onln; cur != cpu; 1149 cur = cur->cpu_next_onln) { 1150 rw_exit(&cur->cpu_ft_lock); 1151 } 1152 #endif 1153 } 1154 mutex_exit(&fasttrap_count_mtx); 1155 } 1156 1157 /*ARGSUSED*/ 1158 static void 1159 fasttrap_pid_enable(void *arg, dtrace_id_t id, void *parg) 1160 { 1161 fasttrap_probe_t *probe = parg; 1162 proc_t *p = NULL; 1163 int i, rc; 1164 1165 ASSERT(probe != NULL); 1166 ASSERT(!probe->ftp_enabled); 1167 ASSERT(id == probe->ftp_id); 1168 #ifdef illumos 1169 ASSERT(MUTEX_HELD(&cpu_lock)); 1170 #endif 1171 1172 /* 1173 * Increment the count of enabled probes on this probe's provider; 1174 * the provider can't go away while the probe still exists. We 1175 * must increment this even if we aren't able to properly enable 1176 * this probe. 1177 */ 1178 mutex_enter(&probe->ftp_prov->ftp_mtx); 1179 probe->ftp_prov->ftp_rcount++; 1180 mutex_exit(&probe->ftp_prov->ftp_mtx); 1181 1182 /* 1183 * If this probe's provider is retired (meaning it was valid in a 1184 * previously exec'ed incarnation of this address space), bail out. The 1185 * provider can't go away while we're in this code path. 1186 */ 1187 if (probe->ftp_prov->ftp_retired) 1188 return; 1189 1190 /* 1191 * If we can't find the process, it may be that we're in the context of 1192 * a fork in which the traced process is being born and we're copying 1193 * USDT probes. Otherwise, the process is gone so bail. 1194 */ 1195 #ifdef illumos 1196 if ((p = sprlock(probe->ftp_pid)) == NULL) { 1197 if ((curproc->p_flag & SFORKING) == 0) 1198 return; 1199 1200 mutex_enter(&pidlock); 1201 p = prfind(probe->ftp_pid); 1202 1203 if (p == NULL) { 1204 /* 1205 * So it's not that the target process is being born, 1206 * it's that it isn't there at all (and we simply 1207 * happen to be forking). Anyway, we know that the 1208 * target is definitely gone, so bail out. 1209 */ 1210 mutex_exit(&pidlock); 1211 return (0); 1212 } 1213 1214 /* 1215 * Confirm that curproc is indeed forking the process in which 1216 * we're trying to enable probes. 1217 */ 1218 ASSERT(p->p_parent == curproc); 1219 ASSERT(p->p_stat == SIDL); 1220 1221 mutex_enter(&p->p_lock); 1222 mutex_exit(&pidlock); 1223 1224 sprlock_proc(p); 1225 } 1226 1227 ASSERT(!(p->p_flag & SVFORK)); 1228 mutex_exit(&p->p_lock); 1229 #else 1230 if (pget(probe->ftp_pid, PGET_HOLD | PGET_NOTWEXIT, &p) != 0) 1231 return; 1232 #endif 1233 1234 /* 1235 * We have to enable the trap entry point before any user threads have 1236 * the chance to execute the trap instruction we're about to place 1237 * in their process's text. 1238 */ 1239 fasttrap_enable_callbacks(); 1240 1241 /* 1242 * Enable all the tracepoints and add this probe's id to each 1243 * tracepoint's list of active probes. 1244 */ 1245 for (i = 0; i < probe->ftp_ntps; i++) { 1246 if ((rc = fasttrap_tracepoint_enable(p, probe, i)) != 0) { 1247 /* 1248 * If enabling the tracepoint failed completely, 1249 * we don't have to disable it; if the failure 1250 * was only partial we must disable it. 1251 */ 1252 if (rc == FASTTRAP_ENABLE_FAIL) 1253 i--; 1254 else 1255 ASSERT(rc == FASTTRAP_ENABLE_PARTIAL); 1256 1257 /* 1258 * Back up and pull out all the tracepoints we've 1259 * created so far for this probe. 1260 */ 1261 while (i >= 0) { 1262 fasttrap_tracepoint_disable(p, probe, i); 1263 i--; 1264 } 1265 1266 #ifdef illumos 1267 mutex_enter(&p->p_lock); 1268 sprunlock(p); 1269 #else 1270 PRELE(p); 1271 #endif 1272 1273 /* 1274 * Since we're not actually enabling this probe, 1275 * drop our reference on the trap table entry. 1276 */ 1277 fasttrap_disable_callbacks(); 1278 return; 1279 } 1280 } 1281 #ifdef illumos 1282 mutex_enter(&p->p_lock); 1283 sprunlock(p); 1284 #else 1285 PRELE(p); 1286 #endif 1287 1288 probe->ftp_enabled = 1; 1289 } 1290 1291 /*ARGSUSED*/ 1292 static void 1293 fasttrap_pid_disable(void *arg, dtrace_id_t id, void *parg) 1294 { 1295 fasttrap_probe_t *probe = parg; 1296 fasttrap_provider_t *provider = probe->ftp_prov; 1297 proc_t *p; 1298 int i, whack = 0; 1299 1300 ASSERT(id == probe->ftp_id); 1301 1302 mutex_enter(&provider->ftp_mtx); 1303 1304 /* 1305 * We won't be able to acquire a /proc-esque lock on the process 1306 * iff the process is dead and gone. In this case, we rely on the 1307 * provider lock as a point of mutual exclusion to prevent other 1308 * DTrace consumers from disabling this probe. 1309 */ 1310 if (pget(probe->ftp_pid, PGET_HOLD | PGET_NOTWEXIT, &p) != 0) 1311 p = NULL; 1312 1313 /* 1314 * Disable all the associated tracepoints (for fully enabled probes). 1315 */ 1316 if (probe->ftp_enabled) { 1317 for (i = 0; i < probe->ftp_ntps; i++) { 1318 fasttrap_tracepoint_disable(p, probe, i); 1319 } 1320 } 1321 1322 ASSERT(provider->ftp_rcount > 0); 1323 provider->ftp_rcount--; 1324 1325 if (p != NULL) { 1326 /* 1327 * Even though we may not be able to remove it entirely, we 1328 * mark this retired provider to get a chance to remove some 1329 * of the associated probes. 1330 */ 1331 if (provider->ftp_retired && !provider->ftp_marked) 1332 whack = provider->ftp_marked = 1; 1333 mutex_exit(&provider->ftp_mtx); 1334 } else { 1335 /* 1336 * If the process is dead, we're just waiting for the 1337 * last probe to be disabled to be able to free it. 1338 */ 1339 if (provider->ftp_rcount == 0 && !provider->ftp_marked) 1340 whack = provider->ftp_marked = 1; 1341 mutex_exit(&provider->ftp_mtx); 1342 } 1343 1344 if (whack) 1345 fasttrap_pid_cleanup(); 1346 1347 #ifdef __FreeBSD__ 1348 if (p != NULL) 1349 PRELE(p); 1350 #endif 1351 if (!probe->ftp_enabled) 1352 return; 1353 1354 probe->ftp_enabled = 0; 1355 1356 #ifdef illumos 1357 ASSERT(MUTEX_HELD(&cpu_lock)); 1358 #endif 1359 fasttrap_disable_callbacks(); 1360 } 1361 1362 /*ARGSUSED*/ 1363 static void 1364 fasttrap_pid_getargdesc(void *arg, dtrace_id_t id, void *parg, 1365 dtrace_argdesc_t *desc) 1366 { 1367 fasttrap_probe_t *probe = parg; 1368 char *str; 1369 int i, ndx; 1370 1371 desc->dtargd_native[0] = '\0'; 1372 desc->dtargd_xlate[0] = '\0'; 1373 1374 if (probe->ftp_prov->ftp_retired != 0 || 1375 desc->dtargd_ndx >= probe->ftp_nargs) { 1376 desc->dtargd_ndx = DTRACE_ARGNONE; 1377 return; 1378 } 1379 1380 ndx = (probe->ftp_argmap != NULL) ? 1381 probe->ftp_argmap[desc->dtargd_ndx] : desc->dtargd_ndx; 1382 1383 str = probe->ftp_ntypes; 1384 for (i = 0; i < ndx; i++) { 1385 str += strlen(str) + 1; 1386 } 1387 1388 ASSERT(strlen(str + 1) < sizeof (desc->dtargd_native)); 1389 (void) strcpy(desc->dtargd_native, str); 1390 1391 if (probe->ftp_xtypes == NULL) 1392 return; 1393 1394 str = probe->ftp_xtypes; 1395 for (i = 0; i < desc->dtargd_ndx; i++) { 1396 str += strlen(str) + 1; 1397 } 1398 1399 ASSERT(strlen(str + 1) < sizeof (desc->dtargd_xlate)); 1400 (void) strcpy(desc->dtargd_xlate, str); 1401 } 1402 1403 /*ARGSUSED*/ 1404 static void 1405 fasttrap_pid_destroy(void *arg, dtrace_id_t id, void *parg) 1406 { 1407 fasttrap_probe_t *probe = parg; 1408 int i; 1409 size_t size; 1410 1411 ASSERT(probe != NULL); 1412 ASSERT(!probe->ftp_enabled); 1413 ASSERT(fasttrap_total >= probe->ftp_ntps); 1414 1415 atomic_add_32(&fasttrap_total, -probe->ftp_ntps); 1416 size = offsetof(fasttrap_probe_t, ftp_tps[probe->ftp_ntps]); 1417 1418 if (probe->ftp_gen + 1 >= fasttrap_mod_gen) 1419 fasttrap_mod_barrier(probe->ftp_gen); 1420 1421 for (i = 0; i < probe->ftp_ntps; i++) { 1422 kmem_free(probe->ftp_tps[i].fit_tp, 1423 sizeof (fasttrap_tracepoint_t)); 1424 } 1425 1426 kmem_free(probe, size); 1427 } 1428 1429 1430 static const dtrace_pattr_t pid_attr = { 1431 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 1432 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 1433 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 1434 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 1435 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 1436 }; 1437 1438 static dtrace_pops_t pid_pops = { 1439 .dtps_provide = fasttrap_pid_provide, 1440 .dtps_provide_module = NULL, 1441 .dtps_enable = fasttrap_pid_enable, 1442 .dtps_disable = fasttrap_pid_disable, 1443 .dtps_suspend = NULL, 1444 .dtps_resume = NULL, 1445 .dtps_getargdesc = fasttrap_pid_getargdesc, 1446 .dtps_getargval = fasttrap_pid_getarg, 1447 .dtps_usermode = NULL, 1448 .dtps_destroy = fasttrap_pid_destroy 1449 }; 1450 1451 static dtrace_pops_t usdt_pops = { 1452 .dtps_provide = fasttrap_pid_provide, 1453 .dtps_provide_module = NULL, 1454 .dtps_enable = fasttrap_pid_enable, 1455 .dtps_disable = fasttrap_pid_disable, 1456 .dtps_suspend = NULL, 1457 .dtps_resume = NULL, 1458 .dtps_getargdesc = fasttrap_pid_getargdesc, 1459 .dtps_getargval = fasttrap_usdt_getarg, 1460 .dtps_usermode = NULL, 1461 .dtps_destroy = fasttrap_pid_destroy 1462 }; 1463 1464 static fasttrap_proc_t * 1465 fasttrap_proc_lookup(pid_t pid) 1466 { 1467 fasttrap_bucket_t *bucket; 1468 fasttrap_proc_t *fprc, *new_fprc; 1469 1470 1471 bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)]; 1472 mutex_enter(&bucket->ftb_mtx); 1473 1474 for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) { 1475 if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) { 1476 mutex_enter(&fprc->ftpc_mtx); 1477 mutex_exit(&bucket->ftb_mtx); 1478 fprc->ftpc_rcount++; 1479 atomic_inc_64(&fprc->ftpc_acount); 1480 ASSERT(fprc->ftpc_acount <= fprc->ftpc_rcount); 1481 mutex_exit(&fprc->ftpc_mtx); 1482 1483 return (fprc); 1484 } 1485 } 1486 1487 /* 1488 * Drop the bucket lock so we don't try to perform a sleeping 1489 * allocation under it. 1490 */ 1491 mutex_exit(&bucket->ftb_mtx); 1492 1493 new_fprc = kmem_zalloc(sizeof (fasttrap_proc_t), KM_SLEEP); 1494 new_fprc->ftpc_pid = pid; 1495 new_fprc->ftpc_rcount = 1; 1496 new_fprc->ftpc_acount = 1; 1497 #ifndef illumos 1498 mutex_init(&new_fprc->ftpc_mtx, "fasttrap proc mtx", MUTEX_DEFAULT, 1499 NULL); 1500 #endif 1501 1502 mutex_enter(&bucket->ftb_mtx); 1503 1504 /* 1505 * Take another lap through the list to make sure a proc hasn't 1506 * been created for this pid while we weren't under the bucket lock. 1507 */ 1508 for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) { 1509 if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) { 1510 mutex_enter(&fprc->ftpc_mtx); 1511 mutex_exit(&bucket->ftb_mtx); 1512 fprc->ftpc_rcount++; 1513 atomic_inc_64(&fprc->ftpc_acount); 1514 ASSERT(fprc->ftpc_acount <= fprc->ftpc_rcount); 1515 mutex_exit(&fprc->ftpc_mtx); 1516 1517 kmem_free(new_fprc, sizeof (fasttrap_proc_t)); 1518 1519 return (fprc); 1520 } 1521 } 1522 1523 new_fprc->ftpc_next = bucket->ftb_data; 1524 bucket->ftb_data = new_fprc; 1525 1526 mutex_exit(&bucket->ftb_mtx); 1527 1528 return (new_fprc); 1529 } 1530 1531 static void 1532 fasttrap_proc_release(fasttrap_proc_t *proc) 1533 { 1534 fasttrap_bucket_t *bucket; 1535 fasttrap_proc_t *fprc, **fprcp; 1536 pid_t pid = proc->ftpc_pid; 1537 #ifndef illumos 1538 fasttrap_scrblock_t *scrblk, *scrblktmp; 1539 fasttrap_scrspace_t *scrspc, *scrspctmp; 1540 struct proc *p; 1541 struct thread *td; 1542 #endif 1543 1544 mutex_enter(&proc->ftpc_mtx); 1545 1546 ASSERT(proc->ftpc_rcount != 0); 1547 ASSERT(proc->ftpc_acount <= proc->ftpc_rcount); 1548 1549 if (--proc->ftpc_rcount != 0) { 1550 mutex_exit(&proc->ftpc_mtx); 1551 return; 1552 } 1553 1554 #ifndef illumos 1555 /* 1556 * Free all structures used to manage per-thread scratch space. 1557 */ 1558 LIST_FOREACH_SAFE(scrblk, &proc->ftpc_scrblks, ftsb_next, 1559 scrblktmp) { 1560 LIST_REMOVE(scrblk, ftsb_next); 1561 free(scrblk, M_SOLARIS); 1562 } 1563 LIST_FOREACH_SAFE(scrspc, &proc->ftpc_fscr, ftss_next, scrspctmp) { 1564 LIST_REMOVE(scrspc, ftss_next); 1565 free(scrspc, M_SOLARIS); 1566 } 1567 LIST_FOREACH_SAFE(scrspc, &proc->ftpc_ascr, ftss_next, scrspctmp) { 1568 LIST_REMOVE(scrspc, ftss_next); 1569 free(scrspc, M_SOLARIS); 1570 } 1571 1572 if ((p = pfind(pid)) != NULL) { 1573 FOREACH_THREAD_IN_PROC(p, td) 1574 td->t_dtrace_sscr = NULL; 1575 PROC_UNLOCK(p); 1576 } 1577 #endif 1578 1579 mutex_exit(&proc->ftpc_mtx); 1580 1581 /* 1582 * There should definitely be no live providers associated with this 1583 * process at this point. 1584 */ 1585 ASSERT(proc->ftpc_acount == 0); 1586 1587 bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)]; 1588 mutex_enter(&bucket->ftb_mtx); 1589 1590 fprcp = (fasttrap_proc_t **)&bucket->ftb_data; 1591 while ((fprc = *fprcp) != NULL) { 1592 if (fprc == proc) 1593 break; 1594 1595 fprcp = &fprc->ftpc_next; 1596 } 1597 1598 /* 1599 * Something strange has happened if we can't find the proc. 1600 */ 1601 ASSERT(fprc != NULL); 1602 1603 *fprcp = fprc->ftpc_next; 1604 1605 mutex_exit(&bucket->ftb_mtx); 1606 1607 kmem_free(fprc, sizeof (fasttrap_proc_t)); 1608 } 1609 1610 /* 1611 * Lookup a fasttrap-managed provider based on its name and associated pid. 1612 * If the pattr argument is non-NULL, this function instantiates the provider 1613 * if it doesn't exist otherwise it returns NULL. The provider is returned 1614 * with its lock held. 1615 */ 1616 static fasttrap_provider_t * 1617 fasttrap_provider_lookup(pid_t pid, const char *name, 1618 const dtrace_pattr_t *pattr) 1619 { 1620 fasttrap_provider_t *fp, *new_fp = NULL; 1621 fasttrap_bucket_t *bucket; 1622 char provname[DTRACE_PROVNAMELEN]; 1623 proc_t *p; 1624 cred_t *cred; 1625 1626 ASSERT(strlen(name) < sizeof (fp->ftp_name)); 1627 ASSERT(pattr != NULL); 1628 1629 bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)]; 1630 mutex_enter(&bucket->ftb_mtx); 1631 1632 /* 1633 * Take a lap through the list and return the match if we find it. 1634 */ 1635 for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 1636 if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 1637 !fp->ftp_retired) { 1638 mutex_enter(&fp->ftp_mtx); 1639 mutex_exit(&bucket->ftb_mtx); 1640 return (fp); 1641 } 1642 } 1643 1644 /* 1645 * Drop the bucket lock so we don't try to perform a sleeping 1646 * allocation under it. 1647 */ 1648 mutex_exit(&bucket->ftb_mtx); 1649 1650 /* 1651 * Make sure the process exists, isn't a child created as the result 1652 * of a vfork(2), and isn't a zombie (but may be in fork). 1653 */ 1654 if ((p = pfind(pid)) == NULL) 1655 return (NULL); 1656 1657 /* 1658 * Increment p_dtrace_probes so that the process knows to inform us 1659 * when it exits or execs. fasttrap_provider_free() decrements this 1660 * when we're done with this provider. 1661 */ 1662 p->p_dtrace_probes++; 1663 1664 /* 1665 * Grab the credentials for this process so we have 1666 * something to pass to dtrace_register(). 1667 */ 1668 PROC_LOCK_ASSERT(p, MA_OWNED); 1669 crhold(p->p_ucred); 1670 cred = p->p_ucred; 1671 PROC_UNLOCK(p); 1672 1673 new_fp = kmem_zalloc(sizeof (fasttrap_provider_t), KM_SLEEP); 1674 new_fp->ftp_pid = pid; 1675 new_fp->ftp_proc = fasttrap_proc_lookup(pid); 1676 #ifndef illumos 1677 mutex_init(&new_fp->ftp_mtx, "provider mtx", MUTEX_DEFAULT, NULL); 1678 mutex_init(&new_fp->ftp_cmtx, "lock on creating", MUTEX_DEFAULT, NULL); 1679 #endif 1680 1681 ASSERT(new_fp->ftp_proc != NULL); 1682 1683 mutex_enter(&bucket->ftb_mtx); 1684 1685 /* 1686 * Take another lap through the list to make sure a provider hasn't 1687 * been created for this pid while we weren't under the bucket lock. 1688 */ 1689 for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 1690 if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 1691 !fp->ftp_retired) { 1692 mutex_enter(&fp->ftp_mtx); 1693 mutex_exit(&bucket->ftb_mtx); 1694 fasttrap_provider_free(new_fp); 1695 crfree(cred); 1696 return (fp); 1697 } 1698 } 1699 1700 (void) strcpy(new_fp->ftp_name, name); 1701 1702 /* 1703 * Fail and return NULL if either the provider name is too long 1704 * or we fail to register this new provider with the DTrace 1705 * framework. Note that this is the only place we ever construct 1706 * the full provider name -- we keep it in pieces in the provider 1707 * structure. 1708 */ 1709 if (snprintf(provname, sizeof (provname), "%s%u", name, (uint_t)pid) >= 1710 sizeof (provname) || 1711 dtrace_register(provname, pattr, 1712 DTRACE_PRIV_PROC | DTRACE_PRIV_OWNER | DTRACE_PRIV_ZONEOWNER, cred, 1713 pattr == &pid_attr ? &pid_pops : &usdt_pops, new_fp, 1714 &new_fp->ftp_provid) != 0) { 1715 mutex_exit(&bucket->ftb_mtx); 1716 fasttrap_provider_free(new_fp); 1717 crfree(cred); 1718 return (NULL); 1719 } 1720 1721 new_fp->ftp_next = bucket->ftb_data; 1722 bucket->ftb_data = new_fp; 1723 1724 mutex_enter(&new_fp->ftp_mtx); 1725 mutex_exit(&bucket->ftb_mtx); 1726 1727 crfree(cred); 1728 return (new_fp); 1729 } 1730 1731 static void 1732 fasttrap_provider_free(fasttrap_provider_t *provider) 1733 { 1734 pid_t pid = provider->ftp_pid; 1735 proc_t *p; 1736 1737 /* 1738 * There need to be no associated enabled probes, no consumers 1739 * creating probes, and no meta providers referencing this provider. 1740 */ 1741 ASSERT(provider->ftp_rcount == 0); 1742 ASSERT(provider->ftp_ccount == 0); 1743 ASSERT(provider->ftp_mcount == 0); 1744 1745 /* 1746 * If this provider hasn't been retired, we need to explicitly drop the 1747 * count of active providers on the associated process structure. 1748 */ 1749 if (!provider->ftp_retired) { 1750 atomic_dec_64(&provider->ftp_proc->ftpc_acount); 1751 ASSERT(provider->ftp_proc->ftpc_acount < 1752 provider->ftp_proc->ftpc_rcount); 1753 } 1754 1755 fasttrap_proc_release(provider->ftp_proc); 1756 1757 #ifndef illumos 1758 mutex_destroy(&provider->ftp_mtx); 1759 mutex_destroy(&provider->ftp_cmtx); 1760 #endif 1761 kmem_free(provider, sizeof (fasttrap_provider_t)); 1762 1763 /* 1764 * Decrement p_dtrace_probes on the process whose provider we're 1765 * freeing. We don't have to worry about clobbering somone else's 1766 * modifications to it because we have locked the bucket that 1767 * corresponds to this process's hash chain in the provider hash 1768 * table. Don't sweat it if we can't find the process. 1769 */ 1770 if ((p = pfind(pid)) == NULL) { 1771 return; 1772 } 1773 1774 p->p_dtrace_probes--; 1775 #ifndef illumos 1776 PROC_UNLOCK(p); 1777 #endif 1778 } 1779 1780 static void 1781 fasttrap_provider_retire(pid_t pid, const char *name, int mprov) 1782 { 1783 fasttrap_provider_t *fp; 1784 fasttrap_bucket_t *bucket; 1785 dtrace_provider_id_t provid; 1786 1787 ASSERT(strlen(name) < sizeof (fp->ftp_name)); 1788 1789 bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)]; 1790 mutex_enter(&bucket->ftb_mtx); 1791 1792 for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 1793 if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 1794 !fp->ftp_retired) 1795 break; 1796 } 1797 1798 if (fp == NULL) { 1799 mutex_exit(&bucket->ftb_mtx); 1800 return; 1801 } 1802 1803 mutex_enter(&fp->ftp_mtx); 1804 ASSERT(!mprov || fp->ftp_mcount > 0); 1805 if (mprov && --fp->ftp_mcount != 0) { 1806 mutex_exit(&fp->ftp_mtx); 1807 mutex_exit(&bucket->ftb_mtx); 1808 return; 1809 } 1810 1811 /* 1812 * Mark the provider to be removed in our post-processing step, mark it 1813 * retired, and drop the active count on its proc. Marking it indicates 1814 * that we should try to remove it; setting the retired flag indicates 1815 * that we're done with this provider; dropping the active the proc 1816 * releases our hold, and when this reaches zero (as it will during 1817 * exit or exec) the proc and associated providers become defunct. 1818 * 1819 * We obviously need to take the bucket lock before the provider lock 1820 * to perform the lookup, but we need to drop the provider lock 1821 * before calling into the DTrace framework since we acquire the 1822 * provider lock in callbacks invoked from the DTrace framework. The 1823 * bucket lock therefore protects the integrity of the provider hash 1824 * table. 1825 */ 1826 atomic_dec_64(&fp->ftp_proc->ftpc_acount); 1827 ASSERT(fp->ftp_proc->ftpc_acount < fp->ftp_proc->ftpc_rcount); 1828 1829 fp->ftp_retired = 1; 1830 fp->ftp_marked = 1; 1831 provid = fp->ftp_provid; 1832 mutex_exit(&fp->ftp_mtx); 1833 1834 /* 1835 * We don't have to worry about invalidating the same provider twice 1836 * since fasttrap_provider_lookup() will ignore provider that have 1837 * been marked as retired. 1838 */ 1839 dtrace_invalidate(provid); 1840 1841 mutex_exit(&bucket->ftb_mtx); 1842 1843 fasttrap_pid_cleanup(); 1844 } 1845 1846 static int 1847 fasttrap_uint32_cmp(const void *ap, const void *bp) 1848 { 1849 return (*(const uint32_t *)ap - *(const uint32_t *)bp); 1850 } 1851 1852 static int 1853 fasttrap_uint64_cmp(const void *ap, const void *bp) 1854 { 1855 return (*(const uint64_t *)ap - *(const uint64_t *)bp); 1856 } 1857 1858 static int 1859 fasttrap_add_probe(fasttrap_probe_spec_t *pdata) 1860 { 1861 fasttrap_provider_t *provider; 1862 fasttrap_probe_t *pp; 1863 fasttrap_tracepoint_t *tp; 1864 char *name; 1865 int i, aframes = 0, whack; 1866 1867 /* 1868 * There needs to be at least one desired trace point. 1869 */ 1870 if (pdata->ftps_noffs == 0) 1871 return (EINVAL); 1872 1873 switch (pdata->ftps_type) { 1874 case DTFTP_ENTRY: 1875 name = "entry"; 1876 aframes = FASTTRAP_ENTRY_AFRAMES; 1877 break; 1878 case DTFTP_RETURN: 1879 name = "return"; 1880 aframes = FASTTRAP_RETURN_AFRAMES; 1881 break; 1882 case DTFTP_OFFSETS: 1883 name = NULL; 1884 break; 1885 default: 1886 return (EINVAL); 1887 } 1888 1889 if ((provider = fasttrap_provider_lookup(pdata->ftps_pid, 1890 FASTTRAP_PID_NAME, &pid_attr)) == NULL) 1891 return (ESRCH); 1892 1893 /* 1894 * Increment this reference count to indicate that a consumer is 1895 * actively adding a new probe associated with this provider. This 1896 * prevents the provider from being deleted -- we'll need to check 1897 * for pending deletions when we drop this reference count. 1898 */ 1899 provider->ftp_ccount++; 1900 mutex_exit(&provider->ftp_mtx); 1901 1902 /* 1903 * Grab the creation lock to ensure consistency between calls to 1904 * dtrace_probe_lookup() and dtrace_probe_create() in the face of 1905 * other threads creating probes. We must drop the provider lock 1906 * before taking this lock to avoid a three-way deadlock with the 1907 * DTrace framework. 1908 */ 1909 mutex_enter(&provider->ftp_cmtx); 1910 1911 if (name == NULL) { 1912 for (i = 0; i < pdata->ftps_noffs; i++) { 1913 char name_str[17]; 1914 1915 (void) sprintf(name_str, "%llx", 1916 (unsigned long long)pdata->ftps_offs[i]); 1917 1918 if (dtrace_probe_lookup(provider->ftp_provid, 1919 pdata->ftps_mod, pdata->ftps_func, name_str) != 0) 1920 continue; 1921 1922 atomic_inc_32(&fasttrap_total); 1923 1924 if (fasttrap_total > fasttrap_max) { 1925 atomic_dec_32(&fasttrap_total); 1926 goto no_mem; 1927 } 1928 1929 pp = kmem_zalloc(sizeof (fasttrap_probe_t), KM_SLEEP); 1930 1931 pp->ftp_prov = provider; 1932 pp->ftp_faddr = pdata->ftps_pc; 1933 pp->ftp_fsize = pdata->ftps_size; 1934 pp->ftp_pid = pdata->ftps_pid; 1935 pp->ftp_ntps = 1; 1936 1937 tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), 1938 KM_SLEEP); 1939 1940 tp->ftt_proc = provider->ftp_proc; 1941 tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc; 1942 tp->ftt_pid = pdata->ftps_pid; 1943 1944 pp->ftp_tps[0].fit_tp = tp; 1945 pp->ftp_tps[0].fit_id.fti_probe = pp; 1946 pp->ftp_tps[0].fit_id.fti_ptype = pdata->ftps_type; 1947 1948 pp->ftp_id = dtrace_probe_create(provider->ftp_provid, 1949 pdata->ftps_mod, pdata->ftps_func, name_str, 1950 FASTTRAP_OFFSET_AFRAMES, pp); 1951 } 1952 1953 } else if (dtrace_probe_lookup(provider->ftp_provid, pdata->ftps_mod, 1954 pdata->ftps_func, name) == 0) { 1955 atomic_add_32(&fasttrap_total, pdata->ftps_noffs); 1956 1957 if (fasttrap_total > fasttrap_max) { 1958 atomic_add_32(&fasttrap_total, -pdata->ftps_noffs); 1959 goto no_mem; 1960 } 1961 1962 /* 1963 * Make sure all tracepoint program counter values are unique. 1964 * We later assume that each probe has exactly one tracepoint 1965 * for a given pc. 1966 */ 1967 qsort(pdata->ftps_offs, pdata->ftps_noffs, 1968 sizeof (uint64_t), fasttrap_uint64_cmp); 1969 for (i = 1; i < pdata->ftps_noffs; i++) { 1970 if (pdata->ftps_offs[i] > pdata->ftps_offs[i - 1]) 1971 continue; 1972 1973 atomic_add_32(&fasttrap_total, -pdata->ftps_noffs); 1974 goto no_mem; 1975 } 1976 1977 ASSERT(pdata->ftps_noffs > 0); 1978 pp = kmem_zalloc(offsetof(fasttrap_probe_t, 1979 ftp_tps[pdata->ftps_noffs]), KM_SLEEP); 1980 1981 pp->ftp_prov = provider; 1982 pp->ftp_faddr = pdata->ftps_pc; 1983 pp->ftp_fsize = pdata->ftps_size; 1984 pp->ftp_pid = pdata->ftps_pid; 1985 pp->ftp_ntps = pdata->ftps_noffs; 1986 1987 for (i = 0; i < pdata->ftps_noffs; i++) { 1988 tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), 1989 KM_SLEEP); 1990 1991 tp->ftt_proc = provider->ftp_proc; 1992 tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc; 1993 tp->ftt_pid = pdata->ftps_pid; 1994 1995 pp->ftp_tps[i].fit_tp = tp; 1996 pp->ftp_tps[i].fit_id.fti_probe = pp; 1997 pp->ftp_tps[i].fit_id.fti_ptype = pdata->ftps_type; 1998 } 1999 2000 pp->ftp_id = dtrace_probe_create(provider->ftp_provid, 2001 pdata->ftps_mod, pdata->ftps_func, name, aframes, pp); 2002 } 2003 2004 mutex_exit(&provider->ftp_cmtx); 2005 2006 /* 2007 * We know that the provider is still valid since we incremented the 2008 * creation reference count. If someone tried to clean up this provider 2009 * while we were using it (e.g. because the process called exec(2) or 2010 * exit(2)), take note of that and try to clean it up now. 2011 */ 2012 mutex_enter(&provider->ftp_mtx); 2013 provider->ftp_ccount--; 2014 whack = provider->ftp_retired; 2015 mutex_exit(&provider->ftp_mtx); 2016 2017 if (whack) 2018 fasttrap_pid_cleanup(); 2019 2020 return (0); 2021 2022 no_mem: 2023 /* 2024 * If we've exhausted the allowable resources, we'll try to remove 2025 * this provider to free some up. This is to cover the case where 2026 * the user has accidentally created many more probes than was 2027 * intended (e.g. pid123:::). 2028 */ 2029 mutex_exit(&provider->ftp_cmtx); 2030 mutex_enter(&provider->ftp_mtx); 2031 provider->ftp_ccount--; 2032 provider->ftp_marked = 1; 2033 mutex_exit(&provider->ftp_mtx); 2034 2035 fasttrap_pid_cleanup(); 2036 2037 return (ENOMEM); 2038 } 2039 2040 /*ARGSUSED*/ 2041 static void * 2042 fasttrap_meta_provide(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid) 2043 { 2044 fasttrap_provider_t *provider; 2045 2046 /* 2047 * A 32-bit unsigned integer (like a pid for example) can be 2048 * expressed in 10 or fewer decimal digits. Make sure that we'll 2049 * have enough space for the provider name. 2050 */ 2051 if (strlen(dhpv->dthpv_provname) + 10 >= 2052 sizeof (provider->ftp_name)) { 2053 printf("failed to instantiate provider %s: " 2054 "name too long to accomodate pid", dhpv->dthpv_provname); 2055 return (NULL); 2056 } 2057 2058 /* 2059 * Don't let folks spoof the true pid provider. 2060 */ 2061 if (strcmp(dhpv->dthpv_provname, FASTTRAP_PID_NAME) == 0) { 2062 printf("failed to instantiate provider %s: " 2063 "%s is an invalid name", dhpv->dthpv_provname, 2064 FASTTRAP_PID_NAME); 2065 return (NULL); 2066 } 2067 2068 /* 2069 * The highest stability class that fasttrap supports is ISA; cap 2070 * the stability of the new provider accordingly. 2071 */ 2072 if (dhpv->dthpv_pattr.dtpa_provider.dtat_class > DTRACE_CLASS_ISA) 2073 dhpv->dthpv_pattr.dtpa_provider.dtat_class = DTRACE_CLASS_ISA; 2074 if (dhpv->dthpv_pattr.dtpa_mod.dtat_class > DTRACE_CLASS_ISA) 2075 dhpv->dthpv_pattr.dtpa_mod.dtat_class = DTRACE_CLASS_ISA; 2076 if (dhpv->dthpv_pattr.dtpa_func.dtat_class > DTRACE_CLASS_ISA) 2077 dhpv->dthpv_pattr.dtpa_func.dtat_class = DTRACE_CLASS_ISA; 2078 if (dhpv->dthpv_pattr.dtpa_name.dtat_class > DTRACE_CLASS_ISA) 2079 dhpv->dthpv_pattr.dtpa_name.dtat_class = DTRACE_CLASS_ISA; 2080 if (dhpv->dthpv_pattr.dtpa_args.dtat_class > DTRACE_CLASS_ISA) 2081 dhpv->dthpv_pattr.dtpa_args.dtat_class = DTRACE_CLASS_ISA; 2082 2083 if ((provider = fasttrap_provider_lookup(pid, dhpv->dthpv_provname, 2084 &dhpv->dthpv_pattr)) == NULL) { 2085 printf("failed to instantiate provider %s for " 2086 "process %u", dhpv->dthpv_provname, (uint_t)pid); 2087 return (NULL); 2088 } 2089 2090 /* 2091 * Up the meta provider count so this provider isn't removed until 2092 * the meta provider has been told to remove it. 2093 */ 2094 provider->ftp_mcount++; 2095 2096 mutex_exit(&provider->ftp_mtx); 2097 2098 return (provider); 2099 } 2100 2101 /* 2102 * We know a few things about our context here: we know that the probe being 2103 * created doesn't already exist (DTrace won't load DOF at the same address 2104 * twice, even if explicitly told to do so) and we know that we are 2105 * single-threaded with respect to the meta provider machinery. Knowing that 2106 * this is a new probe and that there is no way for us to race with another 2107 * operation on this provider allows us an important optimization: we need not 2108 * lookup a probe before adding it. Saving this lookup is important because 2109 * this code is in the fork path for processes with USDT probes, and lookups 2110 * here are potentially very expensive because of long hash conflicts on 2111 * module, function and name (DTrace doesn't hash on provider name). 2112 */ 2113 /*ARGSUSED*/ 2114 static void 2115 fasttrap_meta_create_probe(void *arg, void *parg, 2116 dtrace_helper_probedesc_t *dhpb) 2117 { 2118 fasttrap_provider_t *provider = parg; 2119 fasttrap_probe_t *pp; 2120 fasttrap_tracepoint_t *tp; 2121 int i, j; 2122 uint32_t ntps; 2123 2124 /* 2125 * Since the meta provider count is non-zero we don't have to worry 2126 * about this provider disappearing. 2127 */ 2128 ASSERT(provider->ftp_mcount > 0); 2129 2130 /* 2131 * The offsets must be unique. 2132 */ 2133 qsort(dhpb->dthpb_offs, dhpb->dthpb_noffs, sizeof (uint32_t), 2134 fasttrap_uint32_cmp); 2135 for (i = 1; i < dhpb->dthpb_noffs; i++) { 2136 if (dhpb->dthpb_base + dhpb->dthpb_offs[i] <= 2137 dhpb->dthpb_base + dhpb->dthpb_offs[i - 1]) 2138 return; 2139 } 2140 2141 qsort(dhpb->dthpb_enoffs, dhpb->dthpb_nenoffs, sizeof (uint32_t), 2142 fasttrap_uint32_cmp); 2143 for (i = 1; i < dhpb->dthpb_nenoffs; i++) { 2144 if (dhpb->dthpb_base + dhpb->dthpb_enoffs[i] <= 2145 dhpb->dthpb_base + dhpb->dthpb_enoffs[i - 1]) 2146 return; 2147 } 2148 2149 ntps = dhpb->dthpb_noffs + dhpb->dthpb_nenoffs; 2150 ASSERT(ntps > 0); 2151 2152 atomic_add_32(&fasttrap_total, ntps); 2153 2154 if (fasttrap_total > fasttrap_max) { 2155 atomic_add_32(&fasttrap_total, -ntps); 2156 return; 2157 } 2158 2159 pp = kmem_zalloc(offsetof(fasttrap_probe_t, ftp_tps[ntps]), KM_SLEEP); 2160 2161 pp->ftp_prov = provider; 2162 pp->ftp_pid = provider->ftp_pid; 2163 pp->ftp_ntps = ntps; 2164 pp->ftp_nargs = dhpb->dthpb_xargc; 2165 pp->ftp_xtypes = dhpb->dthpb_xtypes; 2166 pp->ftp_ntypes = dhpb->dthpb_ntypes; 2167 2168 /* 2169 * First create a tracepoint for each actual point of interest. 2170 */ 2171 for (i = 0; i < dhpb->dthpb_noffs; i++) { 2172 tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP); 2173 2174 tp->ftt_proc = provider->ftp_proc; 2175 tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_offs[i]; 2176 tp->ftt_pid = provider->ftp_pid; 2177 2178 pp->ftp_tps[i].fit_tp = tp; 2179 pp->ftp_tps[i].fit_id.fti_probe = pp; 2180 #ifdef __sparc 2181 pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_POST_OFFSETS; 2182 #else 2183 pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_OFFSETS; 2184 #endif 2185 } 2186 2187 /* 2188 * Then create a tracepoint for each is-enabled point. 2189 */ 2190 for (j = 0; i < ntps; i++, j++) { 2191 tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP); 2192 2193 tp->ftt_proc = provider->ftp_proc; 2194 tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_enoffs[j]; 2195 tp->ftt_pid = provider->ftp_pid; 2196 2197 pp->ftp_tps[i].fit_tp = tp; 2198 pp->ftp_tps[i].fit_id.fti_probe = pp; 2199 pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_IS_ENABLED; 2200 } 2201 2202 /* 2203 * If the arguments are shuffled around we set the argument remapping 2204 * table. Later, when the probe fires, we only remap the arguments 2205 * if the table is non-NULL. 2206 */ 2207 for (i = 0; i < dhpb->dthpb_xargc; i++) { 2208 if (dhpb->dthpb_args[i] != i) { 2209 pp->ftp_argmap = dhpb->dthpb_args; 2210 break; 2211 } 2212 } 2213 2214 /* 2215 * The probe is fully constructed -- register it with DTrace. 2216 */ 2217 pp->ftp_id = dtrace_probe_create(provider->ftp_provid, dhpb->dthpb_mod, 2218 dhpb->dthpb_func, dhpb->dthpb_name, FASTTRAP_OFFSET_AFRAMES, pp); 2219 } 2220 2221 /*ARGSUSED*/ 2222 static void 2223 fasttrap_meta_remove(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid) 2224 { 2225 /* 2226 * Clean up the USDT provider. There may be active consumers of the 2227 * provider busy adding probes, no damage will actually befall the 2228 * provider until that count has dropped to zero. This just puts 2229 * the provider on death row. 2230 */ 2231 fasttrap_provider_retire(pid, dhpv->dthpv_provname, 1); 2232 } 2233 2234 static dtrace_mops_t fasttrap_mops = { 2235 .dtms_create_probe = fasttrap_meta_create_probe, 2236 .dtms_provide_pid = fasttrap_meta_provide, 2237 .dtms_remove_pid = fasttrap_meta_remove 2238 }; 2239 2240 /*ARGSUSED*/ 2241 static int 2242 fasttrap_open(struct cdev *dev __unused, int oflags __unused, 2243 int devtype __unused, struct thread *td __unused) 2244 { 2245 return (0); 2246 } 2247 2248 /*ARGSUSED*/ 2249 static int 2250 fasttrap_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int fflag, 2251 struct thread *td) 2252 { 2253 if (!dtrace_attached()) 2254 return (EAGAIN); 2255 2256 if (cmd == FASTTRAPIOC_MAKEPROBE) { 2257 fasttrap_probe_spec_t *uprobe = *(fasttrap_probe_spec_t **)arg; 2258 fasttrap_probe_spec_t *probe; 2259 uint64_t noffs; 2260 size_t size; 2261 int ret, err; 2262 2263 if (copyin(&uprobe->ftps_noffs, &noffs, 2264 sizeof (uprobe->ftps_noffs))) 2265 return (EFAULT); 2266 2267 /* 2268 * Probes must have at least one tracepoint. 2269 */ 2270 if (noffs == 0) 2271 return (EINVAL); 2272 2273 size = sizeof (fasttrap_probe_spec_t) + 2274 sizeof (probe->ftps_offs[0]) * (noffs - 1); 2275 2276 if (size > 1024 * 1024) 2277 return (ENOMEM); 2278 2279 probe = kmem_alloc(size, KM_SLEEP); 2280 2281 if (copyin(uprobe, probe, size) != 0 || 2282 probe->ftps_noffs != noffs) { 2283 kmem_free(probe, size); 2284 return (EFAULT); 2285 } 2286 2287 /* 2288 * Verify that the function and module strings contain no 2289 * funny characters. 2290 */ 2291 if (u8_validate(probe->ftps_func, strlen(probe->ftps_func), 2292 NULL, U8_VALIDATE_ENTIRE, &err) < 0) { 2293 ret = EINVAL; 2294 goto err; 2295 } 2296 2297 if (u8_validate(probe->ftps_mod, strlen(probe->ftps_mod), 2298 NULL, U8_VALIDATE_ENTIRE, &err) < 0) { 2299 ret = EINVAL; 2300 goto err; 2301 } 2302 2303 #ifdef notyet 2304 if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) { 2305 proc_t *p; 2306 pid_t pid = probe->ftps_pid; 2307 2308 mutex_enter(&pidlock); 2309 /* 2310 * Report an error if the process doesn't exist 2311 * or is actively being birthed. 2312 */ 2313 if ((p = pfind(pid)) == NULL || p->p_stat == SIDL) { 2314 mutex_exit(&pidlock); 2315 return (ESRCH); 2316 } 2317 mutex_enter(&p->p_lock); 2318 mutex_exit(&pidlock); 2319 2320 if ((ret = priv_proc_cred_perm(cr, p, NULL, 2321 VREAD | VWRITE)) != 0) { 2322 mutex_exit(&p->p_lock); 2323 return (ret); 2324 } 2325 mutex_exit(&p->p_lock); 2326 } 2327 #endif /* notyet */ 2328 2329 ret = fasttrap_add_probe(probe); 2330 err: 2331 kmem_free(probe, size); 2332 2333 return (ret); 2334 2335 } else if (cmd == FASTTRAPIOC_GETINSTR) { 2336 fasttrap_instr_query_t instr; 2337 fasttrap_tracepoint_t *tp; 2338 uint_t index; 2339 #ifdef notyet 2340 int ret; 2341 #endif 2342 2343 #ifdef illumos 2344 if (copyin((void *)arg, &instr, sizeof (instr)) != 0) 2345 return (EFAULT); 2346 #endif 2347 2348 #ifdef notyet 2349 if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) { 2350 proc_t *p; 2351 pid_t pid = instr.ftiq_pid; 2352 2353 mutex_enter(&pidlock); 2354 /* 2355 * Report an error if the process doesn't exist 2356 * or is actively being birthed. 2357 */ 2358 if ((p == pfind(pid)) == NULL || p->p_stat == SIDL) { 2359 mutex_exit(&pidlock); 2360 return (ESRCH); 2361 } 2362 mutex_enter(&p->p_lock); 2363 mutex_exit(&pidlock); 2364 2365 if ((ret = priv_proc_cred_perm(cr, p, NULL, 2366 VREAD)) != 0) { 2367 mutex_exit(&p->p_lock); 2368 return (ret); 2369 } 2370 2371 mutex_exit(&p->p_lock); 2372 } 2373 #endif /* notyet */ 2374 2375 index = FASTTRAP_TPOINTS_INDEX(instr.ftiq_pid, instr.ftiq_pc); 2376 2377 mutex_enter(&fasttrap_tpoints.fth_table[index].ftb_mtx); 2378 tp = fasttrap_tpoints.fth_table[index].ftb_data; 2379 while (tp != NULL) { 2380 if (instr.ftiq_pid == tp->ftt_pid && 2381 instr.ftiq_pc == tp->ftt_pc && 2382 tp->ftt_proc->ftpc_acount != 0) 2383 break; 2384 2385 tp = tp->ftt_next; 2386 } 2387 2388 if (tp == NULL) { 2389 mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx); 2390 return (ENOENT); 2391 } 2392 2393 bcopy(&tp->ftt_instr, &instr.ftiq_instr, 2394 sizeof (instr.ftiq_instr)); 2395 mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx); 2396 2397 if (copyout(&instr, (void *)arg, sizeof (instr)) != 0) 2398 return (EFAULT); 2399 2400 return (0); 2401 } 2402 2403 return (EINVAL); 2404 } 2405 2406 static int 2407 fasttrap_load(void) 2408 { 2409 ulong_t nent; 2410 int i, ret; 2411 2412 /* Create the /dev/dtrace/fasttrap entry. */ 2413 fasttrap_cdev = make_dev(&fasttrap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, 2414 "dtrace/fasttrap"); 2415 2416 mtx_init(&fasttrap_cleanup_mtx, "fasttrap clean", "dtrace", MTX_DEF); 2417 mutex_init(&fasttrap_count_mtx, "fasttrap count mtx", MUTEX_DEFAULT, 2418 NULL); 2419 2420 #ifdef illumos 2421 fasttrap_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 2422 "fasttrap-max-probes", FASTTRAP_MAX_DEFAULT); 2423 #endif 2424 fasttrap_total = 0; 2425 2426 /* 2427 * Conjure up the tracepoints hashtable... 2428 */ 2429 #ifdef illumos 2430 nent = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 2431 "fasttrap-hash-size", FASTTRAP_TPOINTS_DEFAULT_SIZE); 2432 #else 2433 nent = tpoints_hash_size; 2434 #endif 2435 2436 if (nent == 0 || nent > 0x1000000) 2437 nent = FASTTRAP_TPOINTS_DEFAULT_SIZE; 2438 2439 tpoints_hash_size = nent; 2440 2441 if (ISP2(nent)) 2442 fasttrap_tpoints.fth_nent = nent; 2443 else 2444 fasttrap_tpoints.fth_nent = 1 << fasttrap_highbit(nent); 2445 ASSERT(fasttrap_tpoints.fth_nent > 0); 2446 fasttrap_tpoints.fth_mask = fasttrap_tpoints.fth_nent - 1; 2447 fasttrap_tpoints.fth_table = kmem_zalloc(fasttrap_tpoints.fth_nent * 2448 sizeof (fasttrap_bucket_t), KM_SLEEP); 2449 #ifndef illumos 2450 for (i = 0; i < fasttrap_tpoints.fth_nent; i++) 2451 mutex_init(&fasttrap_tpoints.fth_table[i].ftb_mtx, 2452 "tracepoints bucket mtx", MUTEX_DEFAULT, NULL); 2453 #endif 2454 2455 /* 2456 * ... and the providers hash table... 2457 */ 2458 nent = FASTTRAP_PROVIDERS_DEFAULT_SIZE; 2459 if (ISP2(nent)) 2460 fasttrap_provs.fth_nent = nent; 2461 else 2462 fasttrap_provs.fth_nent = 1 << fasttrap_highbit(nent); 2463 ASSERT(fasttrap_provs.fth_nent > 0); 2464 fasttrap_provs.fth_mask = fasttrap_provs.fth_nent - 1; 2465 fasttrap_provs.fth_table = kmem_zalloc(fasttrap_provs.fth_nent * 2466 sizeof (fasttrap_bucket_t), KM_SLEEP); 2467 #ifndef illumos 2468 for (i = 0; i < fasttrap_provs.fth_nent; i++) 2469 mutex_init(&fasttrap_provs.fth_table[i].ftb_mtx, 2470 "providers bucket mtx", MUTEX_DEFAULT, NULL); 2471 #endif 2472 2473 ret = kproc_create(fasttrap_pid_cleanup_cb, NULL, 2474 &fasttrap_cleanup_proc, 0, 0, "ftcleanup"); 2475 if (ret != 0) { 2476 destroy_dev(fasttrap_cdev); 2477 #ifndef illumos 2478 for (i = 0; i < fasttrap_provs.fth_nent; i++) 2479 mutex_destroy(&fasttrap_provs.fth_table[i].ftb_mtx); 2480 for (i = 0; i < fasttrap_tpoints.fth_nent; i++) 2481 mutex_destroy(&fasttrap_tpoints.fth_table[i].ftb_mtx); 2482 #endif 2483 kmem_free(fasttrap_provs.fth_table, fasttrap_provs.fth_nent * 2484 sizeof (fasttrap_bucket_t)); 2485 mtx_destroy(&fasttrap_cleanup_mtx); 2486 mutex_destroy(&fasttrap_count_mtx); 2487 return (ret); 2488 } 2489 2490 2491 /* 2492 * ... and the procs hash table. 2493 */ 2494 nent = FASTTRAP_PROCS_DEFAULT_SIZE; 2495 if (ISP2(nent)) 2496 fasttrap_procs.fth_nent = nent; 2497 else 2498 fasttrap_procs.fth_nent = 1 << fasttrap_highbit(nent); 2499 ASSERT(fasttrap_procs.fth_nent > 0); 2500 fasttrap_procs.fth_mask = fasttrap_procs.fth_nent - 1; 2501 fasttrap_procs.fth_table = kmem_zalloc(fasttrap_procs.fth_nent * 2502 sizeof (fasttrap_bucket_t), KM_SLEEP); 2503 #ifndef illumos 2504 for (i = 0; i < fasttrap_procs.fth_nent; i++) 2505 mutex_init(&fasttrap_procs.fth_table[i].ftb_mtx, 2506 "processes bucket mtx", MUTEX_DEFAULT, NULL); 2507 2508 rm_init(&fasttrap_tp_lock, "fasttrap tracepoint"); 2509 2510 /* 2511 * This event handler must run before kdtrace_thread_dtor() since it 2512 * accesses the thread's struct kdtrace_thread. 2513 */ 2514 fasttrap_thread_dtor_tag = EVENTHANDLER_REGISTER(thread_dtor, 2515 fasttrap_thread_dtor, NULL, EVENTHANDLER_PRI_FIRST); 2516 #endif 2517 2518 /* 2519 * Install our hooks into fork(2), exec(2), and exit(2). 2520 */ 2521 dtrace_fasttrap_fork = &fasttrap_fork; 2522 dtrace_fasttrap_exit = &fasttrap_exec_exit; 2523 dtrace_fasttrap_exec = &fasttrap_exec_exit; 2524 2525 (void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL, 2526 &fasttrap_meta_id); 2527 2528 return (0); 2529 } 2530 2531 static int 2532 fasttrap_unload(void) 2533 { 2534 int i, fail = 0; 2535 2536 /* 2537 * Unregister the meta-provider to make sure no new fasttrap- 2538 * managed providers come along while we're trying to close up 2539 * shop. If we fail to detach, we'll need to re-register as a 2540 * meta-provider. We can fail to unregister as a meta-provider 2541 * if providers we manage still exist. 2542 */ 2543 if (fasttrap_meta_id != DTRACE_METAPROVNONE && 2544 dtrace_meta_unregister(fasttrap_meta_id) != 0) 2545 return (-1); 2546 2547 /* 2548 * Iterate over all of our providers. If there's still a process 2549 * that corresponds to that pid, fail to detach. 2550 */ 2551 for (i = 0; i < fasttrap_provs.fth_nent; i++) { 2552 fasttrap_provider_t **fpp, *fp; 2553 fasttrap_bucket_t *bucket = &fasttrap_provs.fth_table[i]; 2554 2555 mutex_enter(&bucket->ftb_mtx); 2556 fpp = (fasttrap_provider_t **)&bucket->ftb_data; 2557 while ((fp = *fpp) != NULL) { 2558 /* 2559 * Acquire and release the lock as a simple way of 2560 * waiting for any other consumer to finish with 2561 * this provider. A thread must first acquire the 2562 * bucket lock so there's no chance of another thread 2563 * blocking on the provider's lock. 2564 */ 2565 mutex_enter(&fp->ftp_mtx); 2566 mutex_exit(&fp->ftp_mtx); 2567 2568 if (dtrace_unregister(fp->ftp_provid) != 0) { 2569 fail = 1; 2570 fpp = &fp->ftp_next; 2571 } else { 2572 *fpp = fp->ftp_next; 2573 fasttrap_provider_free(fp); 2574 } 2575 } 2576 2577 mutex_exit(&bucket->ftb_mtx); 2578 } 2579 2580 if (fail) { 2581 (void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL, 2582 &fasttrap_meta_id); 2583 2584 return (-1); 2585 } 2586 2587 /* 2588 * Stop new processes from entering these hooks now, before the 2589 * fasttrap_cleanup thread runs. That way all processes will hopefully 2590 * be out of these hooks before we free fasttrap_provs.fth_table 2591 */ 2592 ASSERT(dtrace_fasttrap_fork == &fasttrap_fork); 2593 dtrace_fasttrap_fork = NULL; 2594 2595 ASSERT(dtrace_fasttrap_exec == &fasttrap_exec_exit); 2596 dtrace_fasttrap_exec = NULL; 2597 2598 ASSERT(dtrace_fasttrap_exit == &fasttrap_exec_exit); 2599 dtrace_fasttrap_exit = NULL; 2600 2601 mtx_lock(&fasttrap_cleanup_mtx); 2602 fasttrap_cleanup_drain = 1; 2603 /* Wait for the cleanup thread to finish up and signal us. */ 2604 wakeup(&fasttrap_cleanup_cv); 2605 mtx_sleep(&fasttrap_cleanup_drain, &fasttrap_cleanup_mtx, 0, "ftcld", 2606 0); 2607 fasttrap_cleanup_proc = NULL; 2608 mtx_destroy(&fasttrap_cleanup_mtx); 2609 2610 #ifdef DEBUG 2611 mutex_enter(&fasttrap_count_mtx); 2612 ASSERT(fasttrap_pid_count == 0); 2613 mutex_exit(&fasttrap_count_mtx); 2614 #endif 2615 2616 #ifndef illumos 2617 EVENTHANDLER_DEREGISTER(thread_dtor, fasttrap_thread_dtor_tag); 2618 2619 for (i = 0; i < fasttrap_tpoints.fth_nent; i++) 2620 mutex_destroy(&fasttrap_tpoints.fth_table[i].ftb_mtx); 2621 for (i = 0; i < fasttrap_provs.fth_nent; i++) 2622 mutex_destroy(&fasttrap_provs.fth_table[i].ftb_mtx); 2623 for (i = 0; i < fasttrap_procs.fth_nent; i++) 2624 mutex_destroy(&fasttrap_procs.fth_table[i].ftb_mtx); 2625 #endif 2626 kmem_free(fasttrap_tpoints.fth_table, 2627 fasttrap_tpoints.fth_nent * sizeof (fasttrap_bucket_t)); 2628 fasttrap_tpoints.fth_nent = 0; 2629 2630 kmem_free(fasttrap_provs.fth_table, 2631 fasttrap_provs.fth_nent * sizeof (fasttrap_bucket_t)); 2632 fasttrap_provs.fth_nent = 0; 2633 2634 kmem_free(fasttrap_procs.fth_table, 2635 fasttrap_procs.fth_nent * sizeof (fasttrap_bucket_t)); 2636 fasttrap_procs.fth_nent = 0; 2637 2638 #ifndef illumos 2639 destroy_dev(fasttrap_cdev); 2640 mutex_destroy(&fasttrap_count_mtx); 2641 rm_destroy(&fasttrap_tp_lock); 2642 #endif 2643 2644 return (0); 2645 } 2646 2647 /* ARGSUSED */ 2648 static int 2649 fasttrap_modevent(module_t mod __unused, int type, void *data __unused) 2650 { 2651 int error = 0; 2652 2653 switch (type) { 2654 case MOD_LOAD: 2655 break; 2656 2657 case MOD_UNLOAD: 2658 break; 2659 2660 case MOD_SHUTDOWN: 2661 break; 2662 2663 default: 2664 error = EOPNOTSUPP; 2665 break; 2666 } 2667 return (error); 2668 } 2669 2670 SYSINIT(fasttrap_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, fasttrap_load, 2671 NULL); 2672 SYSUNINIT(fasttrap_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, 2673 fasttrap_unload, NULL); 2674 2675 DEV_MODULE(fasttrap, fasttrap_modevent, NULL); 2676 MODULE_VERSION(fasttrap, 1); 2677 MODULE_DEPEND(fasttrap, dtrace, 1, 1, 1); 2678 MODULE_DEPEND(fasttrap, opensolaris, 1, 1, 1); 2679