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