fasttrap.c revision 212357
1179193Sjb/* 2179193Sjb * CDDL HEADER START 3179193Sjb * 4179193Sjb * The contents of this file are subject to the terms of the 5179193Sjb * Common Development and Distribution License (the "License"). 6179193Sjb * You may not use this file except in compliance with the License. 7179193Sjb * 8179193Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9179193Sjb * or http://www.opensolaris.org/os/licensing. 10179193Sjb * See the License for the specific language governing permissions 11179193Sjb * and limitations under the License. 12179193Sjb * 13179193Sjb * When distributing Covered Code, include this CDDL HEADER in each 14179193Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15179193Sjb * If applicable, add the following below this CDDL HEADER, with the 16179193Sjb * fields enclosed by brackets "[]" replaced with your own identifying 17179193Sjb * information: Portions Copyright [yyyy] [name of copyright owner] 18179193Sjb * 19179193Sjb * CDDL HEADER END 20211738Srpaulo * 21211738Srpaulo * Portions Copyright 2010 The FreeBSD Foundation 22211738Srpaulo * 23211738Srpaulo * $FreeBSD: head/sys/cddl/contrib/opensolaris/uts/common/dtrace/fasttrap.c 212357 2010-09-09 09:58:05Z rpaulo $ 24179193Sjb */ 25179193Sjb 26179193Sjb/* 27179198Sjb * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 28179193Sjb * Use is subject to license terms. 29179193Sjb */ 30179193Sjb 31211738Srpaulo#if defined(sun) 32179193Sjb#pragma ident "%Z%%M% %I% %E% SMI" 33211738Srpaulo#endif 34179193Sjb 35179193Sjb#include <sys/atomic.h> 36179193Sjb#include <sys/errno.h> 37179193Sjb#include <sys/stat.h> 38179193Sjb#include <sys/modctl.h> 39179193Sjb#include <sys/conf.h> 40179193Sjb#include <sys/systm.h> 41211738Srpaulo#if defined(sun) 42179193Sjb#include <sys/ddi.h> 43211738Srpaulo#endif 44179193Sjb#include <sys/sunddi.h> 45179193Sjb#include <sys/cpuvar.h> 46179193Sjb#include <sys/kmem.h> 47211738Srpaulo#if defined(sun) 48179193Sjb#include <sys/strsubr.h> 49211738Srpaulo#endif 50179193Sjb#include <sys/fasttrap.h> 51179193Sjb#include <sys/fasttrap_impl.h> 52179193Sjb#include <sys/fasttrap_isa.h> 53179193Sjb#include <sys/dtrace.h> 54179193Sjb#include <sys/dtrace_impl.h> 55179193Sjb#include <sys/sysmacros.h> 56179193Sjb#include <sys/proc.h> 57179193Sjb#include <sys/policy.h> 58211738Srpaulo#if defined(sun) 59179193Sjb#include <util/qsort.h> 60211738Srpaulo#endif 61211738Srpaulo#include <sys/mutex.h> 62211738Srpaulo#include <sys/kernel.h> 63211738Srpaulo#if !defined(sun) 64211738Srpaulo#include <sys/user.h> 65211738Srpaulo#include <sys/dtrace_bsd.h> 66211738Srpaulo#include <cddl/dev/dtrace/dtrace_cddl.h> 67211738Srpaulo#endif 68179193Sjb 69179193Sjb/* 70179193Sjb * User-Land Trap-Based Tracing 71179193Sjb * ---------------------------- 72179193Sjb * 73179193Sjb * The fasttrap provider allows DTrace consumers to instrument any user-level 74179193Sjb * instruction to gather data; this includes probes with semantic 75179193Sjb * signifigance like entry and return as well as simple offsets into the 76179193Sjb * function. While the specific techniques used are very ISA specific, the 77179193Sjb * methodology is generalizable to any architecture. 78179193Sjb * 79179193Sjb * 80179193Sjb * The General Methodology 81179193Sjb * ----------------------- 82179193Sjb * 83179193Sjb * With the primary goal of tracing every user-land instruction and the 84179193Sjb * limitation that we can't trust user space so don't want to rely on much 85179193Sjb * information there, we begin by replacing the instructions we want to trace 86179193Sjb * with trap instructions. Each instruction we overwrite is saved into a hash 87179193Sjb * table keyed by process ID and pc address. When we enter the kernel due to 88179193Sjb * this trap instruction, we need the effects of the replaced instruction to 89179193Sjb * appear to have occurred before we proceed with the user thread's 90179193Sjb * execution. 91179193Sjb * 92179193Sjb * Each user level thread is represented by a ulwp_t structure which is 93179193Sjb * always easily accessible through a register. The most basic way to produce 94179193Sjb * the effects of the instruction we replaced is to copy that instruction out 95179193Sjb * to a bit of scratch space reserved in the user thread's ulwp_t structure 96179193Sjb * (a sort of kernel-private thread local storage), set the PC to that 97179193Sjb * scratch space and single step. When we reenter the kernel after single 98179193Sjb * stepping the instruction we must then adjust the PC to point to what would 99179193Sjb * normally be the next instruction. Of course, special care must be taken 100179193Sjb * for branches and jumps, but these represent such a small fraction of any 101179193Sjb * instruction set that writing the code to emulate these in the kernel is 102179193Sjb * not too difficult. 103179193Sjb * 104179193Sjb * Return probes may require several tracepoints to trace every return site, 105179193Sjb * and, conversely, each tracepoint may activate several probes (the entry 106179193Sjb * and offset 0 probes, for example). To solve this muliplexing problem, 107179193Sjb * tracepoints contain lists of probes to activate and probes contain lists 108179193Sjb * of tracepoints to enable. If a probe is activated, it adds its ID to 109179193Sjb * existing tracepoints or creates new ones as necessary. 110179193Sjb * 111179193Sjb * Most probes are activated _before_ the instruction is executed, but return 112179193Sjb * probes are activated _after_ the effects of the last instruction of the 113179193Sjb * function are visible. Return probes must be fired _after_ we have 114179193Sjb * single-stepped the instruction whereas all other probes are fired 115179193Sjb * beforehand. 116179193Sjb * 117179193Sjb * 118179193Sjb * Lock Ordering 119179193Sjb * ------------- 120179193Sjb * 121179193Sjb * The lock ordering below -- both internally and with respect to the DTrace 122179193Sjb * framework -- is a little tricky and bears some explanation. Each provider 123179193Sjb * has a lock (ftp_mtx) that protects its members including reference counts 124179193Sjb * for enabled probes (ftp_rcount), consumers actively creating probes 125179193Sjb * (ftp_ccount) and USDT consumers (ftp_mcount); all three prevent a provider 126179193Sjb * from being freed. A provider is looked up by taking the bucket lock for the 127179193Sjb * provider hash table, and is returned with its lock held. The provider lock 128179193Sjb * may be taken in functions invoked by the DTrace framework, but may not be 129179193Sjb * held while calling functions in the DTrace framework. 130179193Sjb * 131179193Sjb * To ensure consistency over multiple calls to the DTrace framework, the 132179193Sjb * creation lock (ftp_cmtx) should be held. Naturally, the creation lock may 133179193Sjb * not be taken when holding the provider lock as that would create a cyclic 134179193Sjb * lock ordering. In situations where one would naturally take the provider 135179193Sjb * lock and then the creation lock, we instead up a reference count to prevent 136179193Sjb * the provider from disappearing, drop the provider lock, and acquire the 137179193Sjb * creation lock. 138179193Sjb * 139179193Sjb * Briefly: 140179193Sjb * bucket lock before provider lock 141179193Sjb * DTrace before provider lock 142179193Sjb * creation lock before DTrace 143179193Sjb * never hold the provider lock and creation lock simultaneously 144179193Sjb */ 145179193Sjb 146211738Srpaulostatic d_open_t fasttrap_open; 147211738Srpaulostatic d_ioctl_t fasttrap_ioctl; 148211738Srpaulo 149211738Srpaulostatic struct cdevsw fasttrap_cdevsw = { 150211738Srpaulo .d_version = D_VERSION, 151211738Srpaulo .d_open = fasttrap_open, 152211738Srpaulo .d_ioctl = fasttrap_ioctl, 153211738Srpaulo .d_name = "fasttrap", 154211738Srpaulo}; 155211738Srpaulostatic struct cdev *fasttrap_cdev; 156179193Sjbstatic dtrace_meta_provider_id_t fasttrap_meta_id; 157179193Sjb 158211738Srpaulostatic struct callout fasttrap_timeout; 159211738Srpaulostatic struct mtx fasttrap_cleanup_mtx; 160179193Sjbstatic uint_t fasttrap_cleanup_work; 161179193Sjb 162179193Sjb/* 163179193Sjb * Generation count on modifications to the global tracepoint lookup table. 164179193Sjb */ 165179193Sjbstatic volatile uint64_t fasttrap_mod_gen; 166179193Sjb 167179193Sjb/* 168179193Sjb * When the fasttrap provider is loaded, fasttrap_max is set to either 169179193Sjb * FASTTRAP_MAX_DEFAULT or the value for fasttrap-max-probes in the 170179193Sjb * fasttrap.conf file. Each time a probe is created, fasttrap_total is 171179193Sjb * incremented by the number of tracepoints that may be associated with that 172179193Sjb * probe; fasttrap_total is capped at fasttrap_max. 173179193Sjb */ 174179193Sjb#define FASTTRAP_MAX_DEFAULT 250000 175179193Sjbstatic uint32_t fasttrap_max; 176179193Sjbstatic uint32_t fasttrap_total; 177179193Sjb 178179193Sjb 179179193Sjb#define FASTTRAP_TPOINTS_DEFAULT_SIZE 0x4000 180179193Sjb#define FASTTRAP_PROVIDERS_DEFAULT_SIZE 0x100 181179193Sjb#define FASTTRAP_PROCS_DEFAULT_SIZE 0x100 182179193Sjb 183179193Sjb#define FASTTRAP_PID_NAME "pid" 184179193Sjb 185179193Sjbfasttrap_hash_t fasttrap_tpoints; 186179193Sjbstatic fasttrap_hash_t fasttrap_provs; 187179193Sjbstatic fasttrap_hash_t fasttrap_procs; 188179193Sjb 189179193Sjbstatic uint64_t fasttrap_pid_count; /* pid ref count */ 190179193Sjbstatic kmutex_t fasttrap_count_mtx; /* lock on ref count */ 191179193Sjb 192179193Sjb#define FASTTRAP_ENABLE_FAIL 1 193179193Sjb#define FASTTRAP_ENABLE_PARTIAL 2 194179193Sjb 195179193Sjbstatic int fasttrap_tracepoint_enable(proc_t *, fasttrap_probe_t *, uint_t); 196179193Sjbstatic void fasttrap_tracepoint_disable(proc_t *, fasttrap_probe_t *, uint_t); 197179193Sjb 198179193Sjbstatic fasttrap_provider_t *fasttrap_provider_lookup(pid_t, const char *, 199179193Sjb const dtrace_pattr_t *); 200179193Sjbstatic void fasttrap_provider_retire(pid_t, const char *, int); 201179193Sjbstatic void fasttrap_provider_free(fasttrap_provider_t *); 202179193Sjb 203179193Sjbstatic fasttrap_proc_t *fasttrap_proc_lookup(pid_t); 204179193Sjbstatic void fasttrap_proc_release(fasttrap_proc_t *); 205179193Sjb 206179193Sjb#define FASTTRAP_PROVS_INDEX(pid, name) \ 207179193Sjb ((fasttrap_hash_str(name) + (pid)) & fasttrap_provs.fth_mask) 208179193Sjb 209179193Sjb#define FASTTRAP_PROCS_INDEX(pid) ((pid) & fasttrap_procs.fth_mask) 210179193Sjb 211211925Srpaulo#if !defined(sun) 212211925Srpaulostatic kmutex_t fasttrap_cpuc_pid_lock[MAXCPU]; 213211925Srpaulo#endif 214211925Srpaulo 215179193Sjbstatic int 216179193Sjbfasttrap_highbit(ulong_t i) 217179193Sjb{ 218179193Sjb int h = 1; 219179193Sjb 220179193Sjb if (i == 0) 221179193Sjb return (0); 222179193Sjb#ifdef _LP64 223179193Sjb if (i & 0xffffffff00000000ul) { 224179193Sjb h += 32; i >>= 32; 225179193Sjb } 226179193Sjb#endif 227179193Sjb if (i & 0xffff0000) { 228179193Sjb h += 16; i >>= 16; 229179193Sjb } 230179193Sjb if (i & 0xff00) { 231179193Sjb h += 8; i >>= 8; 232179193Sjb } 233179193Sjb if (i & 0xf0) { 234179193Sjb h += 4; i >>= 4; 235179193Sjb } 236179193Sjb if (i & 0xc) { 237179193Sjb h += 2; i >>= 2; 238179193Sjb } 239179193Sjb if (i & 0x2) { 240179193Sjb h += 1; 241179193Sjb } 242179193Sjb return (h); 243179193Sjb} 244179193Sjb 245179193Sjbstatic uint_t 246179193Sjbfasttrap_hash_str(const char *p) 247179193Sjb{ 248179193Sjb unsigned int g; 249179193Sjb uint_t hval = 0; 250179193Sjb 251179193Sjb while (*p) { 252179193Sjb hval = (hval << 4) + *p++; 253179193Sjb if ((g = (hval & 0xf0000000)) != 0) 254179193Sjb hval ^= g >> 24; 255179193Sjb hval &= ~g; 256179193Sjb } 257179193Sjb return (hval); 258179193Sjb} 259179193Sjb 260179193Sjbvoid 261179193Sjbfasttrap_sigtrap(proc_t *p, kthread_t *t, uintptr_t pc) 262179193Sjb{ 263211738Srpaulo#if defined(sun) 264179193Sjb sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP); 265179193Sjb 266179193Sjb sqp->sq_info.si_signo = SIGTRAP; 267179193Sjb sqp->sq_info.si_code = TRAP_DTRACE; 268179193Sjb sqp->sq_info.si_addr = (caddr_t)pc; 269179193Sjb 270179193Sjb mutex_enter(&p->p_lock); 271179193Sjb sigaddqa(p, t, sqp); 272179193Sjb mutex_exit(&p->p_lock); 273179193Sjb 274179193Sjb if (t != NULL) 275179193Sjb aston(t); 276211738Srpaulo#else 277211738Srpaulo ksiginfo_t *ksi = kmem_zalloc(sizeof (ksiginfo_t), KM_SLEEP); 278211738Srpaulo 279211738Srpaulo ksiginfo_init(ksi); 280211738Srpaulo ksi->ksi_signo = SIGTRAP; 281211738Srpaulo ksi->ksi_code = TRAP_DTRACE; 282211738Srpaulo ksi->ksi_addr = (caddr_t)pc; 283211738Srpaulo PROC_LOCK(p); 284211745Srpaulo (void) tdksignal(t, SIGTRAP, ksi); 285211738Srpaulo PROC_UNLOCK(p); 286211738Srpaulo#endif 287179193Sjb} 288179193Sjb 289179193Sjb/* 290179193Sjb * This function ensures that no threads are actively using the memory 291179193Sjb * associated with probes that were formerly live. 292179193Sjb */ 293179193Sjbstatic void 294179193Sjbfasttrap_mod_barrier(uint64_t gen) 295179193Sjb{ 296179193Sjb int i; 297179193Sjb 298179193Sjb if (gen < fasttrap_mod_gen) 299179193Sjb return; 300179193Sjb 301179193Sjb fasttrap_mod_gen++; 302179193Sjb 303211925Srpaulo CPU_FOREACH(i) { 304211925Srpaulo mutex_enter(&fasttrap_cpuc_pid_lock[i]); 305211925Srpaulo mutex_exit(&fasttrap_cpuc_pid_lock[i]); 306179193Sjb } 307179193Sjb} 308179193Sjb 309179193Sjb/* 310179193Sjb * This is the timeout's callback for cleaning up the providers and their 311179193Sjb * probes. 312179193Sjb */ 313179193Sjb/*ARGSUSED*/ 314179193Sjbstatic void 315179193Sjbfasttrap_pid_cleanup_cb(void *data) 316179193Sjb{ 317179193Sjb fasttrap_provider_t **fpp, *fp; 318179193Sjb fasttrap_bucket_t *bucket; 319179193Sjb dtrace_provider_id_t provid; 320211738Srpaulo int i, later = 0; 321179193Sjb 322179193Sjb static volatile int in = 0; 323179193Sjb ASSERT(in == 0); 324179193Sjb in = 1; 325179193Sjb 326179193Sjb while (fasttrap_cleanup_work) { 327179193Sjb fasttrap_cleanup_work = 0; 328211738Srpaulo mtx_unlock(&fasttrap_cleanup_mtx); 329179193Sjb 330179193Sjb later = 0; 331179193Sjb 332179193Sjb /* 333179193Sjb * Iterate over all the providers trying to remove the marked 334179193Sjb * ones. If a provider is marked but not retired, we just 335179193Sjb * have to take a crack at removing it -- it's no big deal if 336179193Sjb * we can't. 337179193Sjb */ 338179193Sjb for (i = 0; i < fasttrap_provs.fth_nent; i++) { 339179193Sjb bucket = &fasttrap_provs.fth_table[i]; 340179193Sjb mutex_enter(&bucket->ftb_mtx); 341179193Sjb fpp = (fasttrap_provider_t **)&bucket->ftb_data; 342179193Sjb 343179193Sjb while ((fp = *fpp) != NULL) { 344179193Sjb if (!fp->ftp_marked) { 345179193Sjb fpp = &fp->ftp_next; 346179193Sjb continue; 347179193Sjb } 348179193Sjb 349179193Sjb mutex_enter(&fp->ftp_mtx); 350179193Sjb 351179193Sjb /* 352179193Sjb * If this provider has consumers actively 353179193Sjb * creating probes (ftp_ccount) or is a USDT 354179193Sjb * provider (ftp_mcount), we can't unregister 355179193Sjb * or even condense. 356179193Sjb */ 357179193Sjb if (fp->ftp_ccount != 0 || 358179193Sjb fp->ftp_mcount != 0) { 359179193Sjb mutex_exit(&fp->ftp_mtx); 360179193Sjb fp->ftp_marked = 0; 361179193Sjb continue; 362179193Sjb } 363179193Sjb 364179193Sjb if (!fp->ftp_retired || fp->ftp_rcount != 0) 365179193Sjb fp->ftp_marked = 0; 366179193Sjb 367179193Sjb mutex_exit(&fp->ftp_mtx); 368179193Sjb 369179193Sjb /* 370179193Sjb * If we successfully unregister this 371179193Sjb * provider we can remove it from the hash 372179193Sjb * chain and free the memory. If our attempt 373179193Sjb * to unregister fails and this is a retired 374179193Sjb * provider, increment our flag to try again 375179193Sjb * pretty soon. If we've consumed more than 376179193Sjb * half of our total permitted number of 377179193Sjb * probes call dtrace_condense() to try to 378179193Sjb * clean out the unenabled probes. 379179193Sjb */ 380179193Sjb provid = fp->ftp_provid; 381179193Sjb if (dtrace_unregister(provid) != 0) { 382179193Sjb if (fasttrap_total > fasttrap_max / 2) 383179193Sjb (void) dtrace_condense(provid); 384179193Sjb later += fp->ftp_marked; 385179193Sjb fpp = &fp->ftp_next; 386179193Sjb } else { 387179193Sjb *fpp = fp->ftp_next; 388179193Sjb fasttrap_provider_free(fp); 389179193Sjb } 390179193Sjb } 391179193Sjb mutex_exit(&bucket->ftb_mtx); 392179193Sjb } 393179193Sjb 394211738Srpaulo mtx_lock(&fasttrap_cleanup_mtx); 395179193Sjb } 396179193Sjb 397211738Srpaulo#if 0 398179193Sjb ASSERT(fasttrap_timeout != 0); 399211738Srpaulo#endif 400179193Sjb 401179193Sjb /* 402179193Sjb * If we were unable to remove a retired provider, try again after 403179193Sjb * a second. This situation can occur in certain circumstances where 404179193Sjb * providers cannot be unregistered even though they have no probes 405179193Sjb * enabled because of an execution of dtrace -l or something similar. 406179193Sjb * If the timeout has been disabled (set to 1 because we're trying 407179193Sjb * to detach), we set fasttrap_cleanup_work to ensure that we'll 408179193Sjb * get a chance to do that work if and when the timeout is reenabled 409179193Sjb * (if detach fails). 410179193Sjb */ 411211738Srpaulo if (later > 0 && callout_active(&fasttrap_timeout)) 412211738Srpaulo callout_reset(&fasttrap_timeout, hz, &fasttrap_pid_cleanup_cb, 413211738Srpaulo NULL); 414179193Sjb else if (later > 0) 415179193Sjb fasttrap_cleanup_work = 1; 416211738Srpaulo else { 417211738Srpaulo#if !defined(sun) 418211738Srpaulo /* Nothing to be done for FreeBSD */ 419211738Srpaulo#endif 420211738Srpaulo } 421179193Sjb 422179193Sjb in = 0; 423179193Sjb} 424179193Sjb 425179193Sjb/* 426179193Sjb * Activates the asynchronous cleanup mechanism. 427179193Sjb */ 428179193Sjbstatic void 429179193Sjbfasttrap_pid_cleanup(void) 430179193Sjb{ 431211738Srpaulo 432211738Srpaulo mtx_lock(&fasttrap_cleanup_mtx); 433179193Sjb fasttrap_cleanup_work = 1; 434211738Srpaulo callout_reset(&fasttrap_timeout, 1, &fasttrap_pid_cleanup_cb, NULL); 435211738Srpaulo mtx_unlock(&fasttrap_cleanup_mtx); 436179193Sjb} 437179193Sjb 438179193Sjb/* 439179193Sjb * This is called from cfork() via dtrace_fasttrap_fork(). The child 440179198Sjb * process's address space is (roughly) a copy of the parent process's so 441179193Sjb * we have to remove all the instrumentation we had previously enabled in the 442179193Sjb * parent. 443179193Sjb */ 444179193Sjbstatic void 445179193Sjbfasttrap_fork(proc_t *p, proc_t *cp) 446179193Sjb{ 447179193Sjb pid_t ppid = p->p_pid; 448179193Sjb int i; 449179193Sjb 450211738Srpaulo#if defined(sun) 451179193Sjb ASSERT(curproc == p); 452179193Sjb ASSERT(p->p_proc_flag & P_PR_LOCK); 453211738Srpaulo#else 454211738Srpaulo PROC_LOCK_ASSERT(p, MA_OWNED); 455211738Srpaulo#endif 456211738Srpaulo#if defined(sun) 457179193Sjb ASSERT(p->p_dtrace_count > 0); 458211738Srpaulo#else 459212357Srpaulo if (p->p_dtrace_helpers) { 460212357Srpaulo /* 461212357Srpaulo * dtrace_helpers_duplicate() allocates memory. 462212357Srpaulo */ 463212357Srpaulo PROC_UNLOCK(p); 464212357Srpaulo PROC_UNLOCK(cp); 465212357Srpaulo dtrace_helpers_duplicate(p, cp); 466212357Srpaulo PROC_LOCK(cp); 467212357Srpaulo PROC_LOCK(p); 468212357Srpaulo } 469211738Srpaulo /* 470211738Srpaulo * This check is purposely here instead of in kern_fork.c because, 471211738Srpaulo * for legal resons, we cannot include the dtrace_cddl.h header 472211738Srpaulo * inside kern_fork.c and insert if-clause there. 473211738Srpaulo */ 474211738Srpaulo if (p->p_dtrace_count == 0) 475211738Srpaulo return; 476211738Srpaulo#endif 477179193Sjb ASSERT(cp->p_dtrace_count == 0); 478179193Sjb 479179193Sjb /* 480179193Sjb * This would be simpler and faster if we maintained per-process 481179193Sjb * hash tables of enabled tracepoints. It could, however, potentially 482179193Sjb * slow down execution of a tracepoint since we'd need to go 483179193Sjb * through two levels of indirection. In the future, we should 484179193Sjb * consider either maintaining per-process ancillary lists of 485179193Sjb * enabled tracepoints or hanging a pointer to a per-process hash 486179193Sjb * table of enabled tracepoints off the proc structure. 487179193Sjb */ 488179193Sjb 489179193Sjb /* 490179193Sjb * We don't have to worry about the child process disappearing 491179193Sjb * because we're in fork(). 492179193Sjb */ 493211738Srpaulo#if defined(sun) 494211738Srpaulo mtx_lock_spin(&cp->p_slock); 495179193Sjb sprlock_proc(cp); 496211738Srpaulo mtx_unlock_spin(&cp->p_slock); 497211738Srpaulo#endif 498179193Sjb 499179193Sjb /* 500179193Sjb * Iterate over every tracepoint looking for ones that belong to the 501179193Sjb * parent process, and remove each from the child process. 502179193Sjb */ 503179193Sjb for (i = 0; i < fasttrap_tpoints.fth_nent; i++) { 504179193Sjb fasttrap_tracepoint_t *tp; 505179193Sjb fasttrap_bucket_t *bucket = &fasttrap_tpoints.fth_table[i]; 506179193Sjb 507179193Sjb mutex_enter(&bucket->ftb_mtx); 508179193Sjb for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 509179193Sjb if (tp->ftt_pid == ppid && 510179193Sjb tp->ftt_proc->ftpc_acount != 0) { 511179193Sjb int ret = fasttrap_tracepoint_remove(cp, tp); 512179193Sjb ASSERT(ret == 0); 513179198Sjb 514179198Sjb /* 515179198Sjb * The count of active providers can only be 516179198Sjb * decremented (i.e. to zero) during exec, 517179198Sjb * exit, and removal of a meta provider so it 518179198Sjb * should be impossible to drop the count 519179198Sjb * mid-fork. 520179198Sjb */ 521179198Sjb ASSERT(tp->ftt_proc->ftpc_acount != 0); 522179193Sjb } 523179193Sjb } 524179193Sjb mutex_exit(&bucket->ftb_mtx); 525179193Sjb } 526179193Sjb 527211738Srpaulo#if defined(sun) 528179193Sjb mutex_enter(&cp->p_lock); 529179193Sjb sprunlock(cp); 530211738Srpaulo#endif 531179193Sjb} 532179193Sjb 533179193Sjb/* 534179193Sjb * This is called from proc_exit() or from exec_common() if p_dtrace_probes 535179193Sjb * is set on the proc structure to indicate that there is a pid provider 536179193Sjb * associated with this process. 537179193Sjb */ 538179193Sjbstatic void 539179193Sjbfasttrap_exec_exit(proc_t *p) 540179193Sjb{ 541211738Srpaulo#if defined(sun) 542179193Sjb ASSERT(p == curproc); 543211738Srpaulo#endif 544211738Srpaulo PROC_LOCK_ASSERT(p, MA_OWNED); 545211738Srpaulo PROC_UNLOCK(p); 546179193Sjb 547179193Sjb /* 548179193Sjb * We clean up the pid provider for this process here; user-land 549179193Sjb * static probes are handled by the meta-provider remove entry point. 550179193Sjb */ 551179193Sjb fasttrap_provider_retire(p->p_pid, FASTTRAP_PID_NAME, 0); 552212357Srpaulo#if !defined(sun) 553212357Srpaulo if (p->p_dtrace_helpers) 554212357Srpaulo dtrace_helpers_destroy(p); 555212357Srpaulo#endif 556211738Srpaulo PROC_LOCK(p); 557179193Sjb} 558179193Sjb 559179193Sjb 560179193Sjb/*ARGSUSED*/ 561179193Sjbstatic void 562211738Srpaulofasttrap_pid_provide(void *arg, dtrace_probedesc_t *desc) 563179193Sjb{ 564179193Sjb /* 565179193Sjb * There are no "default" pid probes. 566179193Sjb */ 567179193Sjb} 568179193Sjb 569179193Sjbstatic int 570179193Sjbfasttrap_tracepoint_enable(proc_t *p, fasttrap_probe_t *probe, uint_t index) 571179193Sjb{ 572179193Sjb fasttrap_tracepoint_t *tp, *new_tp = NULL; 573179193Sjb fasttrap_bucket_t *bucket; 574179193Sjb fasttrap_id_t *id; 575179193Sjb pid_t pid; 576179193Sjb uintptr_t pc; 577179193Sjb 578179193Sjb ASSERT(index < probe->ftp_ntps); 579179193Sjb 580179193Sjb pid = probe->ftp_pid; 581179193Sjb pc = probe->ftp_tps[index].fit_tp->ftt_pc; 582179193Sjb id = &probe->ftp_tps[index].fit_id; 583179193Sjb 584179193Sjb ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid); 585179193Sjb 586211738Srpaulo#if defined(sun) 587179193Sjb ASSERT(!(p->p_flag & SVFORK)); 588211738Srpaulo#endif 589179193Sjb 590179193Sjb /* 591179193Sjb * Before we make any modifications, make sure we've imposed a barrier 592179193Sjb * on the generation in which this probe was last modified. 593179193Sjb */ 594179193Sjb fasttrap_mod_barrier(probe->ftp_gen); 595179193Sjb 596179193Sjb bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)]; 597179193Sjb 598179193Sjb /* 599179193Sjb * If the tracepoint has already been enabled, just add our id to the 600179193Sjb * list of interested probes. This may be our second time through 601179193Sjb * this path in which case we'll have constructed the tracepoint we'd 602179193Sjb * like to install. If we can't find a match, and have an allocated 603179193Sjb * tracepoint ready to go, enable that one now. 604179193Sjb * 605179193Sjb * A tracepoint whose process is defunct is also considered defunct. 606179193Sjb */ 607179193Sjbagain: 608179193Sjb mutex_enter(&bucket->ftb_mtx); 609179193Sjb for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 610179198Sjb /* 611179198Sjb * Note that it's safe to access the active count on the 612179198Sjb * associated proc structure because we know that at least one 613179198Sjb * provider (this one) will still be around throughout this 614179198Sjb * operation. 615179198Sjb */ 616179193Sjb if (tp->ftt_pid != pid || tp->ftt_pc != pc || 617179193Sjb tp->ftt_proc->ftpc_acount == 0) 618179193Sjb continue; 619179193Sjb 620179193Sjb /* 621179193Sjb * Now that we've found a matching tracepoint, it would be 622179193Sjb * a decent idea to confirm that the tracepoint is still 623179193Sjb * enabled and the trap instruction hasn't been overwritten. 624179193Sjb * Since this is a little hairy, we'll punt for now. 625179193Sjb */ 626179193Sjb 627179193Sjb /* 628179193Sjb * This can't be the first interested probe. We don't have 629179193Sjb * to worry about another thread being in the midst of 630179193Sjb * deleting this tracepoint (which would be the only valid 631179193Sjb * reason for a tracepoint to have no interested probes) 632179193Sjb * since we're holding P_PR_LOCK for this process. 633179193Sjb */ 634179193Sjb ASSERT(tp->ftt_ids != NULL || tp->ftt_retids != NULL); 635179193Sjb 636179193Sjb switch (id->fti_ptype) { 637179193Sjb case DTFTP_ENTRY: 638179193Sjb case DTFTP_OFFSETS: 639179193Sjb case DTFTP_IS_ENABLED: 640179193Sjb id->fti_next = tp->ftt_ids; 641179193Sjb membar_producer(); 642179193Sjb tp->ftt_ids = id; 643179193Sjb membar_producer(); 644179193Sjb break; 645179193Sjb 646179193Sjb case DTFTP_RETURN: 647179193Sjb case DTFTP_POST_OFFSETS: 648179193Sjb id->fti_next = tp->ftt_retids; 649179193Sjb membar_producer(); 650179193Sjb tp->ftt_retids = id; 651179193Sjb membar_producer(); 652179193Sjb break; 653179193Sjb 654179193Sjb default: 655179193Sjb ASSERT(0); 656179193Sjb } 657179193Sjb 658179193Sjb mutex_exit(&bucket->ftb_mtx); 659179193Sjb 660179193Sjb if (new_tp != NULL) { 661179193Sjb new_tp->ftt_ids = NULL; 662179193Sjb new_tp->ftt_retids = NULL; 663179193Sjb } 664179193Sjb 665179193Sjb return (0); 666179193Sjb } 667179193Sjb 668179193Sjb /* 669179193Sjb * If we have a good tracepoint ready to go, install it now while 670179193Sjb * we have the lock held and no one can screw with us. 671179193Sjb */ 672179193Sjb if (new_tp != NULL) { 673179193Sjb int rc = 0; 674179193Sjb 675179193Sjb new_tp->ftt_next = bucket->ftb_data; 676179193Sjb membar_producer(); 677179193Sjb bucket->ftb_data = new_tp; 678179193Sjb membar_producer(); 679179193Sjb mutex_exit(&bucket->ftb_mtx); 680179193Sjb 681179193Sjb /* 682179193Sjb * Activate the tracepoint in the ISA-specific manner. 683179193Sjb * If this fails, we need to report the failure, but 684179193Sjb * indicate that this tracepoint must still be disabled 685179193Sjb * by calling fasttrap_tracepoint_disable(). 686179193Sjb */ 687179193Sjb if (fasttrap_tracepoint_install(p, new_tp) != 0) 688179193Sjb rc = FASTTRAP_ENABLE_PARTIAL; 689179193Sjb 690179193Sjb /* 691179193Sjb * Increment the count of the number of tracepoints active in 692179193Sjb * the victim process. 693179193Sjb */ 694211738Srpaulo#if defined(sun) 695179193Sjb ASSERT(p->p_proc_flag & P_PR_LOCK); 696211738Srpaulo#else 697211738Srpaulo PROC_LOCK_ASSERT(p, MA_OWNED); 698211738Srpaulo#endif 699179193Sjb p->p_dtrace_count++; 700179193Sjb 701179193Sjb return (rc); 702179193Sjb } 703179193Sjb 704179193Sjb mutex_exit(&bucket->ftb_mtx); 705179193Sjb 706179193Sjb /* 707179193Sjb * Initialize the tracepoint that's been preallocated with the probe. 708179193Sjb */ 709179193Sjb new_tp = probe->ftp_tps[index].fit_tp; 710179193Sjb 711179193Sjb ASSERT(new_tp->ftt_pid == pid); 712179193Sjb ASSERT(new_tp->ftt_pc == pc); 713179193Sjb ASSERT(new_tp->ftt_proc == probe->ftp_prov->ftp_proc); 714179193Sjb ASSERT(new_tp->ftt_ids == NULL); 715179193Sjb ASSERT(new_tp->ftt_retids == NULL); 716179193Sjb 717179193Sjb switch (id->fti_ptype) { 718179193Sjb case DTFTP_ENTRY: 719179193Sjb case DTFTP_OFFSETS: 720179193Sjb case DTFTP_IS_ENABLED: 721179193Sjb id->fti_next = NULL; 722179193Sjb new_tp->ftt_ids = id; 723179193Sjb break; 724179193Sjb 725179193Sjb case DTFTP_RETURN: 726179193Sjb case DTFTP_POST_OFFSETS: 727179193Sjb id->fti_next = NULL; 728179193Sjb new_tp->ftt_retids = id; 729179193Sjb break; 730179193Sjb 731179193Sjb default: 732179193Sjb ASSERT(0); 733179193Sjb } 734179193Sjb 735179193Sjb /* 736179193Sjb * If the ISA-dependent initialization goes to plan, go back to the 737179193Sjb * beginning and try to install this freshly made tracepoint. 738179193Sjb */ 739179193Sjb if (fasttrap_tracepoint_init(p, new_tp, pc, id->fti_ptype) == 0) 740179193Sjb goto again; 741179193Sjb 742179193Sjb new_tp->ftt_ids = NULL; 743179193Sjb new_tp->ftt_retids = NULL; 744179193Sjb 745179193Sjb return (FASTTRAP_ENABLE_FAIL); 746179193Sjb} 747179193Sjb 748179193Sjbstatic void 749179193Sjbfasttrap_tracepoint_disable(proc_t *p, fasttrap_probe_t *probe, uint_t index) 750179193Sjb{ 751179193Sjb fasttrap_bucket_t *bucket; 752179193Sjb fasttrap_provider_t *provider = probe->ftp_prov; 753179193Sjb fasttrap_tracepoint_t **pp, *tp; 754211738Srpaulo fasttrap_id_t *id, **idp = NULL; 755179193Sjb pid_t pid; 756179193Sjb uintptr_t pc; 757179193Sjb 758179193Sjb ASSERT(index < probe->ftp_ntps); 759179193Sjb 760179193Sjb pid = probe->ftp_pid; 761179193Sjb pc = probe->ftp_tps[index].fit_tp->ftt_pc; 762179193Sjb id = &probe->ftp_tps[index].fit_id; 763179193Sjb 764179193Sjb ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid); 765179193Sjb 766179193Sjb /* 767179193Sjb * Find the tracepoint and make sure that our id is one of the 768179193Sjb * ones registered with it. 769179193Sjb */ 770179193Sjb bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)]; 771179193Sjb mutex_enter(&bucket->ftb_mtx); 772179193Sjb for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 773179193Sjb if (tp->ftt_pid == pid && tp->ftt_pc == pc && 774179193Sjb tp->ftt_proc == provider->ftp_proc) 775179193Sjb break; 776179193Sjb } 777179193Sjb 778179193Sjb /* 779179193Sjb * If we somehow lost this tracepoint, we're in a world of hurt. 780179193Sjb */ 781179193Sjb ASSERT(tp != NULL); 782179193Sjb 783179193Sjb switch (id->fti_ptype) { 784179193Sjb case DTFTP_ENTRY: 785179193Sjb case DTFTP_OFFSETS: 786179193Sjb case DTFTP_IS_ENABLED: 787179193Sjb ASSERT(tp->ftt_ids != NULL); 788179193Sjb idp = &tp->ftt_ids; 789179193Sjb break; 790179193Sjb 791179193Sjb case DTFTP_RETURN: 792179193Sjb case DTFTP_POST_OFFSETS: 793179193Sjb ASSERT(tp->ftt_retids != NULL); 794179193Sjb idp = &tp->ftt_retids; 795179193Sjb break; 796179193Sjb 797179193Sjb default: 798179193Sjb ASSERT(0); 799179193Sjb } 800179193Sjb 801179193Sjb while ((*idp)->fti_probe != probe) { 802179193Sjb idp = &(*idp)->fti_next; 803179193Sjb ASSERT(*idp != NULL); 804179193Sjb } 805179193Sjb 806179193Sjb id = *idp; 807179193Sjb *idp = id->fti_next; 808179193Sjb membar_producer(); 809179193Sjb 810179193Sjb ASSERT(id->fti_probe == probe); 811179193Sjb 812179193Sjb /* 813179193Sjb * If there are other registered enablings of this tracepoint, we're 814179193Sjb * all done, but if this was the last probe assocated with this 815179193Sjb * this tracepoint, we need to remove and free it. 816179193Sjb */ 817179193Sjb if (tp->ftt_ids != NULL || tp->ftt_retids != NULL) { 818179193Sjb 819179193Sjb /* 820179193Sjb * If the current probe's tracepoint is in use, swap it 821179193Sjb * for an unused tracepoint. 822179193Sjb */ 823179193Sjb if (tp == probe->ftp_tps[index].fit_tp) { 824179193Sjb fasttrap_probe_t *tmp_probe; 825179193Sjb fasttrap_tracepoint_t **tmp_tp; 826179193Sjb uint_t tmp_index; 827179193Sjb 828179193Sjb if (tp->ftt_ids != NULL) { 829179193Sjb tmp_probe = tp->ftt_ids->fti_probe; 830179193Sjb /* LINTED - alignment */ 831179193Sjb tmp_index = FASTTRAP_ID_INDEX(tp->ftt_ids); 832179193Sjb tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp; 833179193Sjb } else { 834179193Sjb tmp_probe = tp->ftt_retids->fti_probe; 835179193Sjb /* LINTED - alignment */ 836179193Sjb tmp_index = FASTTRAP_ID_INDEX(tp->ftt_retids); 837179193Sjb tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp; 838179193Sjb } 839179193Sjb 840179193Sjb ASSERT(*tmp_tp != NULL); 841179193Sjb ASSERT(*tmp_tp != probe->ftp_tps[index].fit_tp); 842179193Sjb ASSERT((*tmp_tp)->ftt_ids == NULL); 843179193Sjb ASSERT((*tmp_tp)->ftt_retids == NULL); 844179193Sjb 845179193Sjb probe->ftp_tps[index].fit_tp = *tmp_tp; 846179193Sjb *tmp_tp = tp; 847179193Sjb } 848179193Sjb 849179193Sjb mutex_exit(&bucket->ftb_mtx); 850179193Sjb 851179193Sjb /* 852179193Sjb * Tag the modified probe with the generation in which it was 853179193Sjb * changed. 854179193Sjb */ 855179193Sjb probe->ftp_gen = fasttrap_mod_gen; 856179193Sjb return; 857179193Sjb } 858179193Sjb 859179193Sjb mutex_exit(&bucket->ftb_mtx); 860179193Sjb 861179193Sjb /* 862179193Sjb * We can't safely remove the tracepoint from the set of active 863179193Sjb * tracepoints until we've actually removed the fasttrap instruction 864179193Sjb * from the process's text. We can, however, operate on this 865179193Sjb * tracepoint secure in the knowledge that no other thread is going to 866179193Sjb * be looking at it since we hold P_PR_LOCK on the process if it's 867179193Sjb * live or we hold the provider lock on the process if it's dead and 868179193Sjb * gone. 869179193Sjb */ 870179193Sjb 871179193Sjb /* 872179193Sjb * We only need to remove the actual instruction if we're looking 873179193Sjb * at an existing process 874179193Sjb */ 875179193Sjb if (p != NULL) { 876179193Sjb /* 877179193Sjb * If we fail to restore the instruction we need to kill 878179193Sjb * this process since it's in a completely unrecoverable 879179193Sjb * state. 880179193Sjb */ 881179193Sjb if (fasttrap_tracepoint_remove(p, tp) != 0) 882179193Sjb fasttrap_sigtrap(p, NULL, pc); 883179193Sjb 884179193Sjb /* 885179193Sjb * Decrement the count of the number of tracepoints active 886179193Sjb * in the victim process. 887179193Sjb */ 888211738Srpaulo#if defined(sun) 889179193Sjb ASSERT(p->p_proc_flag & P_PR_LOCK); 890211738Srpaulo#else 891211738Srpaulo PROC_LOCK_ASSERT(p, MA_OWNED); 892211738Srpaulo#endif 893179193Sjb p->p_dtrace_count--; 894179193Sjb } 895179193Sjb 896179193Sjb /* 897179193Sjb * Remove the probe from the hash table of active tracepoints. 898179193Sjb */ 899179193Sjb mutex_enter(&bucket->ftb_mtx); 900179193Sjb pp = (fasttrap_tracepoint_t **)&bucket->ftb_data; 901179193Sjb ASSERT(*pp != NULL); 902179193Sjb while (*pp != tp) { 903179193Sjb pp = &(*pp)->ftt_next; 904179193Sjb ASSERT(*pp != NULL); 905179193Sjb } 906179193Sjb 907179193Sjb *pp = tp->ftt_next; 908179193Sjb membar_producer(); 909179193Sjb 910179193Sjb mutex_exit(&bucket->ftb_mtx); 911179193Sjb 912179193Sjb /* 913179193Sjb * Tag the modified probe with the generation in which it was changed. 914179193Sjb */ 915179193Sjb probe->ftp_gen = fasttrap_mod_gen; 916179193Sjb} 917179193Sjb 918179193Sjbstatic void 919179193Sjbfasttrap_enable_callbacks(void) 920179193Sjb{ 921179193Sjb /* 922179193Sjb * We don't have to play the rw lock game here because we're 923179193Sjb * providing something rather than taking something away -- 924179193Sjb * we can be sure that no threads have tried to follow this 925179193Sjb * function pointer yet. 926179193Sjb */ 927179193Sjb mutex_enter(&fasttrap_count_mtx); 928179193Sjb if (fasttrap_pid_count == 0) { 929179193Sjb ASSERT(dtrace_pid_probe_ptr == NULL); 930179193Sjb ASSERT(dtrace_return_probe_ptr == NULL); 931179193Sjb dtrace_pid_probe_ptr = &fasttrap_pid_probe; 932179193Sjb dtrace_return_probe_ptr = &fasttrap_return_probe; 933179193Sjb } 934179193Sjb ASSERT(dtrace_pid_probe_ptr == &fasttrap_pid_probe); 935179193Sjb ASSERT(dtrace_return_probe_ptr == &fasttrap_return_probe); 936179193Sjb fasttrap_pid_count++; 937179193Sjb mutex_exit(&fasttrap_count_mtx); 938179193Sjb} 939179193Sjb 940179193Sjbstatic void 941179193Sjbfasttrap_disable_callbacks(void) 942179193Sjb{ 943211738Srpaulo#if defined(sun) 944179193Sjb ASSERT(MUTEX_HELD(&cpu_lock)); 945211738Srpaulo#endif 946179193Sjb 947211738Srpaulo 948179193Sjb mutex_enter(&fasttrap_count_mtx); 949179193Sjb ASSERT(fasttrap_pid_count > 0); 950179193Sjb fasttrap_pid_count--; 951179193Sjb if (fasttrap_pid_count == 0) { 952211738Srpaulo#if defined(sun) 953179193Sjb cpu_t *cur, *cpu = CPU; 954179193Sjb 955179193Sjb for (cur = cpu->cpu_next_onln; cur != cpu; 956179193Sjb cur = cur->cpu_next_onln) { 957179193Sjb rw_enter(&cur->cpu_ft_lock, RW_WRITER); 958179193Sjb } 959211738Srpaulo#endif 960179193Sjb dtrace_pid_probe_ptr = NULL; 961179193Sjb dtrace_return_probe_ptr = NULL; 962211738Srpaulo#if defined(sun) 963179193Sjb for (cur = cpu->cpu_next_onln; cur != cpu; 964179193Sjb cur = cur->cpu_next_onln) { 965179193Sjb rw_exit(&cur->cpu_ft_lock); 966179193Sjb } 967211738Srpaulo#endif 968179193Sjb } 969179193Sjb mutex_exit(&fasttrap_count_mtx); 970179193Sjb} 971179193Sjb 972179193Sjb/*ARGSUSED*/ 973179193Sjbstatic void 974179193Sjbfasttrap_pid_enable(void *arg, dtrace_id_t id, void *parg) 975179193Sjb{ 976179193Sjb fasttrap_probe_t *probe = parg; 977211738Srpaulo proc_t *p = NULL; 978179193Sjb int i, rc; 979179193Sjb 980211738Srpaulo 981179193Sjb ASSERT(probe != NULL); 982179193Sjb ASSERT(!probe->ftp_enabled); 983179193Sjb ASSERT(id == probe->ftp_id); 984211738Srpaulo#if defined(sun) 985179193Sjb ASSERT(MUTEX_HELD(&cpu_lock)); 986211738Srpaulo#endif 987179193Sjb 988179193Sjb /* 989179193Sjb * Increment the count of enabled probes on this probe's provider; 990179193Sjb * the provider can't go away while the probe still exists. We 991179193Sjb * must increment this even if we aren't able to properly enable 992179193Sjb * this probe. 993179193Sjb */ 994179193Sjb mutex_enter(&probe->ftp_prov->ftp_mtx); 995179193Sjb probe->ftp_prov->ftp_rcount++; 996179193Sjb mutex_exit(&probe->ftp_prov->ftp_mtx); 997179193Sjb 998179193Sjb /* 999179193Sjb * If this probe's provider is retired (meaning it was valid in a 1000179193Sjb * previously exec'ed incarnation of this address space), bail out. The 1001179193Sjb * provider can't go away while we're in this code path. 1002179193Sjb */ 1003179193Sjb if (probe->ftp_prov->ftp_retired) 1004179193Sjb return; 1005179193Sjb 1006179193Sjb /* 1007179193Sjb * If we can't find the process, it may be that we're in the context of 1008179193Sjb * a fork in which the traced process is being born and we're copying 1009179193Sjb * USDT probes. Otherwise, the process is gone so bail. 1010179193Sjb */ 1011211738Srpaulo#if defined(sun) 1012179193Sjb if ((p = sprlock(probe->ftp_pid)) == NULL) { 1013179193Sjb if ((curproc->p_flag & SFORKING) == 0) 1014179193Sjb return; 1015179193Sjb 1016179193Sjb mutex_enter(&pidlock); 1017179193Sjb p = prfind(probe->ftp_pid); 1018179193Sjb 1019179193Sjb /* 1020179193Sjb * Confirm that curproc is indeed forking the process in which 1021179193Sjb * we're trying to enable probes. 1022179193Sjb */ 1023179193Sjb ASSERT(p != NULL); 1024179193Sjb ASSERT(p->p_parent == curproc); 1025179193Sjb ASSERT(p->p_stat == SIDL); 1026179193Sjb 1027179193Sjb mutex_enter(&p->p_lock); 1028179193Sjb mutex_exit(&pidlock); 1029179193Sjb 1030179193Sjb sprlock_proc(p); 1031179193Sjb } 1032179193Sjb 1033179193Sjb ASSERT(!(p->p_flag & SVFORK)); 1034179193Sjb mutex_exit(&p->p_lock); 1035211738Srpaulo#else 1036211738Srpaulo if ((p = pfind(probe->ftp_pid)) == NULL) 1037211738Srpaulo return; 1038211738Srpaulo#endif 1039179193Sjb 1040179193Sjb /* 1041179193Sjb * We have to enable the trap entry point before any user threads have 1042179193Sjb * the chance to execute the trap instruction we're about to place 1043179193Sjb * in their process's text. 1044179193Sjb */ 1045211738Srpaulo PROC_UNLOCK(p); 1046179193Sjb fasttrap_enable_callbacks(); 1047211738Srpaulo PROC_LOCK(p); 1048179193Sjb 1049179193Sjb /* 1050179193Sjb * Enable all the tracepoints and add this probe's id to each 1051179193Sjb * tracepoint's list of active probes. 1052179193Sjb */ 1053179193Sjb for (i = 0; i < probe->ftp_ntps; i++) { 1054179193Sjb if ((rc = fasttrap_tracepoint_enable(p, probe, i)) != 0) { 1055179193Sjb /* 1056179193Sjb * If enabling the tracepoint failed completely, 1057179193Sjb * we don't have to disable it; if the failure 1058179193Sjb * was only partial we must disable it. 1059179193Sjb */ 1060179193Sjb if (rc == FASTTRAP_ENABLE_FAIL) 1061179193Sjb i--; 1062179193Sjb else 1063179193Sjb ASSERT(rc == FASTTRAP_ENABLE_PARTIAL); 1064179193Sjb 1065179193Sjb /* 1066179193Sjb * Back up and pull out all the tracepoints we've 1067179193Sjb * created so far for this probe. 1068179193Sjb */ 1069179193Sjb while (i >= 0) { 1070179193Sjb fasttrap_tracepoint_disable(p, probe, i); 1071179193Sjb i--; 1072179193Sjb } 1073179193Sjb 1074211738Srpaulo#if defined(sun) 1075179193Sjb mutex_enter(&p->p_lock); 1076179193Sjb sprunlock(p); 1077211738Srpaulo#else 1078211738Srpaulo PROC_UNLOCK(p); 1079211738Srpaulo#endif 1080179193Sjb 1081179193Sjb /* 1082179193Sjb * Since we're not actually enabling this probe, 1083179193Sjb * drop our reference on the trap table entry. 1084179193Sjb */ 1085179193Sjb fasttrap_disable_callbacks(); 1086179193Sjb return; 1087179193Sjb } 1088179193Sjb } 1089211738Srpaulo#if defined(sun) 1090179193Sjb mutex_enter(&p->p_lock); 1091179193Sjb sprunlock(p); 1092211738Srpaulo#else 1093211738Srpaulo PROC_UNLOCK(p); 1094211738Srpaulo#endif 1095179193Sjb 1096179193Sjb probe->ftp_enabled = 1; 1097179193Sjb} 1098179193Sjb 1099179193Sjb/*ARGSUSED*/ 1100179193Sjbstatic void 1101179193Sjbfasttrap_pid_disable(void *arg, dtrace_id_t id, void *parg) 1102179193Sjb{ 1103179193Sjb fasttrap_probe_t *probe = parg; 1104179193Sjb fasttrap_provider_t *provider = probe->ftp_prov; 1105179193Sjb proc_t *p; 1106179193Sjb int i, whack = 0; 1107179193Sjb 1108179193Sjb ASSERT(id == probe->ftp_id); 1109179193Sjb 1110211738Srpaulo mutex_enter(&provider->ftp_mtx); 1111211738Srpaulo 1112179193Sjb /* 1113179193Sjb * We won't be able to acquire a /proc-esque lock on the process 1114179193Sjb * iff the process is dead and gone. In this case, we rely on the 1115179193Sjb * provider lock as a point of mutual exclusion to prevent other 1116179193Sjb * DTrace consumers from disabling this probe. 1117179193Sjb */ 1118211738Srpaulo if ((p = pfind(probe->ftp_pid)) == NULL) { 1119211738Srpaulo mutex_exit(&provider->ftp_mtx); 1120211738Srpaulo return; 1121179193Sjb } 1122179193Sjb 1123179193Sjb /* 1124179193Sjb * Disable all the associated tracepoints (for fully enabled probes). 1125179193Sjb */ 1126179193Sjb if (probe->ftp_enabled) { 1127179193Sjb for (i = 0; i < probe->ftp_ntps; i++) { 1128179193Sjb fasttrap_tracepoint_disable(p, probe, i); 1129179193Sjb } 1130179193Sjb } 1131179193Sjb 1132179193Sjb ASSERT(provider->ftp_rcount > 0); 1133179193Sjb provider->ftp_rcount--; 1134179193Sjb 1135179193Sjb if (p != NULL) { 1136179193Sjb /* 1137179193Sjb * Even though we may not be able to remove it entirely, we 1138179193Sjb * mark this retired provider to get a chance to remove some 1139179193Sjb * of the associated probes. 1140179193Sjb */ 1141179193Sjb if (provider->ftp_retired && !provider->ftp_marked) 1142179193Sjb whack = provider->ftp_marked = 1; 1143179193Sjb mutex_exit(&provider->ftp_mtx); 1144179193Sjb } else { 1145179193Sjb /* 1146179193Sjb * If the process is dead, we're just waiting for the 1147179193Sjb * last probe to be disabled to be able to free it. 1148179193Sjb */ 1149179193Sjb if (provider->ftp_rcount == 0 && !provider->ftp_marked) 1150179193Sjb whack = provider->ftp_marked = 1; 1151179193Sjb mutex_exit(&provider->ftp_mtx); 1152179193Sjb } 1153211738Srpaulo#if !defined(sun) 1154211738Srpaulo PROC_UNLOCK(p); 1155211738Srpaulo#endif 1156179193Sjb 1157179193Sjb if (whack) 1158179193Sjb fasttrap_pid_cleanup(); 1159179193Sjb 1160179193Sjb if (!probe->ftp_enabled) 1161179193Sjb return; 1162179193Sjb 1163179193Sjb probe->ftp_enabled = 0; 1164179193Sjb 1165211738Srpaulo#if defined(sun) 1166179193Sjb ASSERT(MUTEX_HELD(&cpu_lock)); 1167211738Srpaulo#endif 1168179193Sjb fasttrap_disable_callbacks(); 1169179193Sjb} 1170179193Sjb 1171179193Sjb/*ARGSUSED*/ 1172179193Sjbstatic void 1173179193Sjbfasttrap_pid_getargdesc(void *arg, dtrace_id_t id, void *parg, 1174179193Sjb dtrace_argdesc_t *desc) 1175179193Sjb{ 1176179193Sjb fasttrap_probe_t *probe = parg; 1177179193Sjb char *str; 1178179193Sjb int i, ndx; 1179179193Sjb 1180179193Sjb desc->dtargd_native[0] = '\0'; 1181179193Sjb desc->dtargd_xlate[0] = '\0'; 1182179193Sjb 1183179193Sjb if (probe->ftp_prov->ftp_retired != 0 || 1184179193Sjb desc->dtargd_ndx >= probe->ftp_nargs) { 1185179193Sjb desc->dtargd_ndx = DTRACE_ARGNONE; 1186179193Sjb return; 1187179193Sjb } 1188179193Sjb 1189179193Sjb ndx = (probe->ftp_argmap != NULL) ? 1190179193Sjb probe->ftp_argmap[desc->dtargd_ndx] : desc->dtargd_ndx; 1191179193Sjb 1192179193Sjb str = probe->ftp_ntypes; 1193179193Sjb for (i = 0; i < ndx; i++) { 1194179193Sjb str += strlen(str) + 1; 1195179193Sjb } 1196179193Sjb 1197179193Sjb ASSERT(strlen(str + 1) < sizeof (desc->dtargd_native)); 1198179193Sjb (void) strcpy(desc->dtargd_native, str); 1199179193Sjb 1200179193Sjb if (probe->ftp_xtypes == NULL) 1201179193Sjb return; 1202179193Sjb 1203179193Sjb str = probe->ftp_xtypes; 1204179193Sjb for (i = 0; i < desc->dtargd_ndx; i++) { 1205179193Sjb str += strlen(str) + 1; 1206179193Sjb } 1207179193Sjb 1208179193Sjb ASSERT(strlen(str + 1) < sizeof (desc->dtargd_xlate)); 1209179193Sjb (void) strcpy(desc->dtargd_xlate, str); 1210179193Sjb} 1211179193Sjb 1212179193Sjb/*ARGSUSED*/ 1213179193Sjbstatic void 1214179193Sjbfasttrap_pid_destroy(void *arg, dtrace_id_t id, void *parg) 1215179193Sjb{ 1216179193Sjb fasttrap_probe_t *probe = parg; 1217179193Sjb int i; 1218179193Sjb size_t size; 1219179193Sjb 1220179193Sjb ASSERT(probe != NULL); 1221179193Sjb ASSERT(!probe->ftp_enabled); 1222179193Sjb ASSERT(fasttrap_total >= probe->ftp_ntps); 1223179193Sjb 1224179193Sjb atomic_add_32(&fasttrap_total, -probe->ftp_ntps); 1225179193Sjb size = offsetof(fasttrap_probe_t, ftp_tps[probe->ftp_ntps]); 1226179193Sjb 1227179193Sjb if (probe->ftp_gen + 1 >= fasttrap_mod_gen) 1228179193Sjb fasttrap_mod_barrier(probe->ftp_gen); 1229179193Sjb 1230179193Sjb for (i = 0; i < probe->ftp_ntps; i++) { 1231179193Sjb kmem_free(probe->ftp_tps[i].fit_tp, 1232179193Sjb sizeof (fasttrap_tracepoint_t)); 1233179193Sjb } 1234179193Sjb 1235179193Sjb kmem_free(probe, size); 1236179193Sjb} 1237179193Sjb 1238179193Sjb 1239179193Sjbstatic const dtrace_pattr_t pid_attr = { 1240179193Sjb{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 1241179193Sjb{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 1242179193Sjb{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 1243179193Sjb{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 1244179193Sjb{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 1245179193Sjb}; 1246179193Sjb 1247179193Sjbstatic dtrace_pops_t pid_pops = { 1248179193Sjb fasttrap_pid_provide, 1249179193Sjb NULL, 1250179193Sjb fasttrap_pid_enable, 1251179193Sjb fasttrap_pid_disable, 1252179193Sjb NULL, 1253179193Sjb NULL, 1254179193Sjb fasttrap_pid_getargdesc, 1255179193Sjb fasttrap_pid_getarg, 1256179193Sjb NULL, 1257179193Sjb fasttrap_pid_destroy 1258179193Sjb}; 1259179193Sjb 1260179193Sjbstatic dtrace_pops_t usdt_pops = { 1261179193Sjb fasttrap_pid_provide, 1262179193Sjb NULL, 1263179193Sjb fasttrap_pid_enable, 1264179193Sjb fasttrap_pid_disable, 1265179193Sjb NULL, 1266179193Sjb NULL, 1267179193Sjb fasttrap_pid_getargdesc, 1268179193Sjb fasttrap_usdt_getarg, 1269179193Sjb NULL, 1270179193Sjb fasttrap_pid_destroy 1271179193Sjb}; 1272179193Sjb 1273179193Sjbstatic fasttrap_proc_t * 1274179193Sjbfasttrap_proc_lookup(pid_t pid) 1275179193Sjb{ 1276179193Sjb fasttrap_bucket_t *bucket; 1277179193Sjb fasttrap_proc_t *fprc, *new_fprc; 1278179193Sjb 1279211738Srpaulo 1280179193Sjb bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)]; 1281179193Sjb mutex_enter(&bucket->ftb_mtx); 1282179193Sjb 1283179193Sjb for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) { 1284179193Sjb if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) { 1285179193Sjb mutex_enter(&fprc->ftpc_mtx); 1286179193Sjb mutex_exit(&bucket->ftb_mtx); 1287179193Sjb fprc->ftpc_rcount++; 1288179193Sjb atomic_add_64(&fprc->ftpc_acount, 1); 1289179198Sjb ASSERT(fprc->ftpc_acount <= fprc->ftpc_rcount); 1290179193Sjb mutex_exit(&fprc->ftpc_mtx); 1291179193Sjb 1292179193Sjb return (fprc); 1293179193Sjb } 1294179193Sjb } 1295179193Sjb 1296179193Sjb /* 1297179193Sjb * Drop the bucket lock so we don't try to perform a sleeping 1298179193Sjb * allocation under it. 1299179193Sjb */ 1300179193Sjb mutex_exit(&bucket->ftb_mtx); 1301179193Sjb 1302179193Sjb new_fprc = kmem_zalloc(sizeof (fasttrap_proc_t), KM_SLEEP); 1303179193Sjb new_fprc->ftpc_pid = pid; 1304179193Sjb new_fprc->ftpc_rcount = 1; 1305179193Sjb new_fprc->ftpc_acount = 1; 1306211738Srpaulo#if !defined(sun) 1307211738Srpaulo mutex_init(&new_fprc->ftpc_mtx, "fasttrap proc mtx", MUTEX_DEFAULT, 1308211738Srpaulo NULL); 1309211738Srpaulo#endif 1310179193Sjb 1311179193Sjb mutex_enter(&bucket->ftb_mtx); 1312179193Sjb 1313179193Sjb /* 1314179193Sjb * Take another lap through the list to make sure a proc hasn't 1315179193Sjb * been created for this pid while we weren't under the bucket lock. 1316179193Sjb */ 1317179193Sjb for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) { 1318179193Sjb if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) { 1319179193Sjb mutex_enter(&fprc->ftpc_mtx); 1320179193Sjb mutex_exit(&bucket->ftb_mtx); 1321179193Sjb fprc->ftpc_rcount++; 1322179193Sjb atomic_add_64(&fprc->ftpc_acount, 1); 1323179198Sjb ASSERT(fprc->ftpc_acount <= fprc->ftpc_rcount); 1324179193Sjb mutex_exit(&fprc->ftpc_mtx); 1325179193Sjb 1326179193Sjb kmem_free(new_fprc, sizeof (fasttrap_proc_t)); 1327179193Sjb 1328179193Sjb return (fprc); 1329179193Sjb } 1330179193Sjb } 1331179193Sjb 1332179193Sjb new_fprc->ftpc_next = bucket->ftb_data; 1333179193Sjb bucket->ftb_data = new_fprc; 1334179193Sjb 1335179193Sjb mutex_exit(&bucket->ftb_mtx); 1336179193Sjb 1337179193Sjb return (new_fprc); 1338179193Sjb} 1339179193Sjb 1340179193Sjbstatic void 1341179193Sjbfasttrap_proc_release(fasttrap_proc_t *proc) 1342179193Sjb{ 1343179193Sjb fasttrap_bucket_t *bucket; 1344179193Sjb fasttrap_proc_t *fprc, **fprcp; 1345179193Sjb pid_t pid = proc->ftpc_pid; 1346179193Sjb 1347179193Sjb mutex_enter(&proc->ftpc_mtx); 1348179193Sjb 1349179193Sjb ASSERT(proc->ftpc_rcount != 0); 1350179198Sjb ASSERT(proc->ftpc_acount <= proc->ftpc_rcount); 1351179193Sjb 1352179193Sjb if (--proc->ftpc_rcount != 0) { 1353179193Sjb mutex_exit(&proc->ftpc_mtx); 1354179193Sjb return; 1355179193Sjb } 1356179193Sjb 1357179193Sjb mutex_exit(&proc->ftpc_mtx); 1358179193Sjb 1359179193Sjb /* 1360179193Sjb * There should definitely be no live providers associated with this 1361179193Sjb * process at this point. 1362179193Sjb */ 1363179193Sjb ASSERT(proc->ftpc_acount == 0); 1364179193Sjb 1365179193Sjb bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)]; 1366179193Sjb mutex_enter(&bucket->ftb_mtx); 1367179193Sjb 1368179193Sjb fprcp = (fasttrap_proc_t **)&bucket->ftb_data; 1369179193Sjb while ((fprc = *fprcp) != NULL) { 1370179193Sjb if (fprc == proc) 1371179193Sjb break; 1372179193Sjb 1373179193Sjb fprcp = &fprc->ftpc_next; 1374179193Sjb } 1375179193Sjb 1376179193Sjb /* 1377179193Sjb * Something strange has happened if we can't find the proc. 1378179193Sjb */ 1379179193Sjb ASSERT(fprc != NULL); 1380179193Sjb 1381179193Sjb *fprcp = fprc->ftpc_next; 1382179193Sjb 1383179193Sjb mutex_exit(&bucket->ftb_mtx); 1384179193Sjb 1385179193Sjb kmem_free(fprc, sizeof (fasttrap_proc_t)); 1386179193Sjb} 1387179193Sjb 1388179193Sjb/* 1389179193Sjb * Lookup a fasttrap-managed provider based on its name and associated pid. 1390179193Sjb * If the pattr argument is non-NULL, this function instantiates the provider 1391179193Sjb * if it doesn't exist otherwise it returns NULL. The provider is returned 1392179193Sjb * with its lock held. 1393179193Sjb */ 1394179193Sjbstatic fasttrap_provider_t * 1395179193Sjbfasttrap_provider_lookup(pid_t pid, const char *name, 1396179193Sjb const dtrace_pattr_t *pattr) 1397179193Sjb{ 1398179193Sjb fasttrap_provider_t *fp, *new_fp = NULL; 1399179193Sjb fasttrap_bucket_t *bucket; 1400179193Sjb char provname[DTRACE_PROVNAMELEN]; 1401179193Sjb proc_t *p; 1402179193Sjb cred_t *cred; 1403179193Sjb 1404179193Sjb ASSERT(strlen(name) < sizeof (fp->ftp_name)); 1405179193Sjb ASSERT(pattr != NULL); 1406179193Sjb 1407179193Sjb bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)]; 1408179193Sjb mutex_enter(&bucket->ftb_mtx); 1409179193Sjb 1410179193Sjb /* 1411179193Sjb * Take a lap through the list and return the match if we find it. 1412179193Sjb */ 1413179193Sjb for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 1414179193Sjb if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 1415179193Sjb !fp->ftp_retired) { 1416179193Sjb mutex_enter(&fp->ftp_mtx); 1417179193Sjb mutex_exit(&bucket->ftb_mtx); 1418179193Sjb return (fp); 1419179193Sjb } 1420179193Sjb } 1421179193Sjb 1422179193Sjb /* 1423179193Sjb * Drop the bucket lock so we don't try to perform a sleeping 1424179193Sjb * allocation under it. 1425179193Sjb */ 1426179193Sjb mutex_exit(&bucket->ftb_mtx); 1427179193Sjb 1428179193Sjb /* 1429179193Sjb * Make sure the process exists, isn't a child created as the result 1430179193Sjb * of a vfork(2), and isn't a zombie (but may be in fork). 1431179193Sjb */ 1432211738Srpaulo if ((p = pfind(pid)) == NULL) 1433179193Sjb return (NULL); 1434179193Sjb 1435179193Sjb /* 1436179193Sjb * Increment p_dtrace_probes so that the process knows to inform us 1437179193Sjb * when it exits or execs. fasttrap_provider_free() decrements this 1438179193Sjb * when we're done with this provider. 1439179193Sjb */ 1440179193Sjb p->p_dtrace_probes++; 1441179193Sjb 1442179193Sjb /* 1443179193Sjb * Grab the credentials for this process so we have 1444179193Sjb * something to pass to dtrace_register(). 1445179193Sjb */ 1446211738Srpaulo PROC_LOCK_ASSERT(p, MA_OWNED); 1447211738Srpaulo crhold(p->p_ucred); 1448211738Srpaulo cred = p->p_ucred; 1449211738Srpaulo PROC_UNLOCK(p); 1450179193Sjb 1451179193Sjb new_fp = kmem_zalloc(sizeof (fasttrap_provider_t), KM_SLEEP); 1452179193Sjb new_fp->ftp_pid = pid; 1453179193Sjb new_fp->ftp_proc = fasttrap_proc_lookup(pid); 1454211738Srpaulo#if !defined(sun) 1455211738Srpaulo mutex_init(&new_fp->ftp_mtx, "provider mtx", MUTEX_DEFAULT, NULL); 1456211738Srpaulo mutex_init(&new_fp->ftp_cmtx, "lock on creating", MUTEX_DEFAULT, NULL); 1457211738Srpaulo#endif 1458179193Sjb 1459179193Sjb ASSERT(new_fp->ftp_proc != NULL); 1460179193Sjb 1461179193Sjb mutex_enter(&bucket->ftb_mtx); 1462179193Sjb 1463179193Sjb /* 1464179193Sjb * Take another lap through the list to make sure a provider hasn't 1465179193Sjb * been created for this pid while we weren't under the bucket lock. 1466179193Sjb */ 1467179193Sjb for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 1468179193Sjb if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 1469179193Sjb !fp->ftp_retired) { 1470179193Sjb mutex_enter(&fp->ftp_mtx); 1471179193Sjb mutex_exit(&bucket->ftb_mtx); 1472179193Sjb fasttrap_provider_free(new_fp); 1473179193Sjb crfree(cred); 1474179193Sjb return (fp); 1475179193Sjb } 1476179193Sjb } 1477179193Sjb 1478179193Sjb (void) strcpy(new_fp->ftp_name, name); 1479179193Sjb 1480179193Sjb /* 1481179193Sjb * Fail and return NULL if either the provider name is too long 1482179193Sjb * or we fail to register this new provider with the DTrace 1483179193Sjb * framework. Note that this is the only place we ever construct 1484179193Sjb * the full provider name -- we keep it in pieces in the provider 1485179193Sjb * structure. 1486179193Sjb */ 1487179193Sjb if (snprintf(provname, sizeof (provname), "%s%u", name, (uint_t)pid) >= 1488179193Sjb sizeof (provname) || 1489179193Sjb dtrace_register(provname, pattr, 1490179193Sjb DTRACE_PRIV_PROC | DTRACE_PRIV_OWNER | DTRACE_PRIV_ZONEOWNER, cred, 1491179193Sjb pattr == &pid_attr ? &pid_pops : &usdt_pops, new_fp, 1492179193Sjb &new_fp->ftp_provid) != 0) { 1493179193Sjb mutex_exit(&bucket->ftb_mtx); 1494179193Sjb fasttrap_provider_free(new_fp); 1495179193Sjb crfree(cred); 1496179193Sjb return (NULL); 1497179193Sjb } 1498179193Sjb 1499179193Sjb new_fp->ftp_next = bucket->ftb_data; 1500179193Sjb bucket->ftb_data = new_fp; 1501179193Sjb 1502179193Sjb mutex_enter(&new_fp->ftp_mtx); 1503179193Sjb mutex_exit(&bucket->ftb_mtx); 1504179193Sjb 1505179193Sjb crfree(cred); 1506179193Sjb return (new_fp); 1507179193Sjb} 1508179193Sjb 1509179193Sjbstatic void 1510179193Sjbfasttrap_provider_free(fasttrap_provider_t *provider) 1511179193Sjb{ 1512179193Sjb pid_t pid = provider->ftp_pid; 1513179193Sjb proc_t *p; 1514179193Sjb 1515179193Sjb /* 1516179193Sjb * There need to be no associated enabled probes, no consumers 1517179193Sjb * creating probes, and no meta providers referencing this provider. 1518179193Sjb */ 1519179193Sjb ASSERT(provider->ftp_rcount == 0); 1520179193Sjb ASSERT(provider->ftp_ccount == 0); 1521179193Sjb ASSERT(provider->ftp_mcount == 0); 1522179193Sjb 1523179198Sjb /* 1524179198Sjb * If this provider hasn't been retired, we need to explicitly drop the 1525179198Sjb * count of active providers on the associated process structure. 1526179198Sjb */ 1527179198Sjb if (!provider->ftp_retired) { 1528179198Sjb atomic_add_64(&provider->ftp_proc->ftpc_acount, -1); 1529179198Sjb ASSERT(provider->ftp_proc->ftpc_acount < 1530179198Sjb provider->ftp_proc->ftpc_rcount); 1531179198Sjb } 1532179198Sjb 1533179193Sjb fasttrap_proc_release(provider->ftp_proc); 1534179193Sjb 1535211738Srpaulo#if !defined(sun) 1536211738Srpaulo mutex_destroy(&provider->ftp_mtx); 1537211738Srpaulo mutex_destroy(&provider->ftp_cmtx); 1538211738Srpaulo#endif 1539179193Sjb kmem_free(provider, sizeof (fasttrap_provider_t)); 1540179193Sjb 1541179193Sjb /* 1542179193Sjb * Decrement p_dtrace_probes on the process whose provider we're 1543179193Sjb * freeing. We don't have to worry about clobbering somone else's 1544179193Sjb * modifications to it because we have locked the bucket that 1545179193Sjb * corresponds to this process's hash chain in the provider hash 1546179193Sjb * table. Don't sweat it if we can't find the process. 1547179193Sjb */ 1548211738Srpaulo if ((p = pfind(pid)) == NULL) { 1549179193Sjb return; 1550179193Sjb } 1551179193Sjb 1552179193Sjb p->p_dtrace_probes--; 1553211738Srpaulo#if !defined(sun) 1554211738Srpaulo PROC_UNLOCK(p); 1555211738Srpaulo#endif 1556179193Sjb} 1557179193Sjb 1558179193Sjbstatic void 1559179193Sjbfasttrap_provider_retire(pid_t pid, const char *name, int mprov) 1560179193Sjb{ 1561179193Sjb fasttrap_provider_t *fp; 1562179193Sjb fasttrap_bucket_t *bucket; 1563179193Sjb dtrace_provider_id_t provid; 1564179193Sjb 1565179193Sjb ASSERT(strlen(name) < sizeof (fp->ftp_name)); 1566179193Sjb 1567179193Sjb bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)]; 1568179193Sjb mutex_enter(&bucket->ftb_mtx); 1569179193Sjb 1570179193Sjb for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 1571179193Sjb if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 1572179193Sjb !fp->ftp_retired) 1573179193Sjb break; 1574179193Sjb } 1575179193Sjb 1576179193Sjb if (fp == NULL) { 1577179193Sjb mutex_exit(&bucket->ftb_mtx); 1578179193Sjb return; 1579179193Sjb } 1580179193Sjb 1581179193Sjb mutex_enter(&fp->ftp_mtx); 1582179193Sjb ASSERT(!mprov || fp->ftp_mcount > 0); 1583179193Sjb if (mprov && --fp->ftp_mcount != 0) { 1584179193Sjb mutex_exit(&fp->ftp_mtx); 1585179193Sjb mutex_exit(&bucket->ftb_mtx); 1586179193Sjb return; 1587179193Sjb } 1588179193Sjb 1589179193Sjb /* 1590179193Sjb * Mark the provider to be removed in our post-processing step, mark it 1591179193Sjb * retired, and drop the active count on its proc. Marking it indicates 1592179193Sjb * that we should try to remove it; setting the retired flag indicates 1593179193Sjb * that we're done with this provider; dropping the active the proc 1594179193Sjb * releases our hold, and when this reaches zero (as it will during 1595179193Sjb * exit or exec) the proc and associated providers become defunct. 1596179193Sjb * 1597179193Sjb * We obviously need to take the bucket lock before the provider lock 1598179193Sjb * to perform the lookup, but we need to drop the provider lock 1599179193Sjb * before calling into the DTrace framework since we acquire the 1600179193Sjb * provider lock in callbacks invoked from the DTrace framework. The 1601179193Sjb * bucket lock therefore protects the integrity of the provider hash 1602179193Sjb * table. 1603179193Sjb */ 1604179193Sjb atomic_add_64(&fp->ftp_proc->ftpc_acount, -1); 1605179198Sjb ASSERT(fp->ftp_proc->ftpc_acount < fp->ftp_proc->ftpc_rcount); 1606179198Sjb 1607179193Sjb fp->ftp_retired = 1; 1608179193Sjb fp->ftp_marked = 1; 1609179193Sjb provid = fp->ftp_provid; 1610179193Sjb mutex_exit(&fp->ftp_mtx); 1611179193Sjb 1612179193Sjb /* 1613179193Sjb * We don't have to worry about invalidating the same provider twice 1614179193Sjb * since fasttrap_provider_lookup() will ignore provider that have 1615179193Sjb * been marked as retired. 1616179193Sjb */ 1617179193Sjb dtrace_invalidate(provid); 1618179193Sjb 1619179193Sjb mutex_exit(&bucket->ftb_mtx); 1620179193Sjb 1621179193Sjb fasttrap_pid_cleanup(); 1622179193Sjb} 1623179193Sjb 1624179193Sjbstatic int 1625179193Sjbfasttrap_uint32_cmp(const void *ap, const void *bp) 1626179193Sjb{ 1627179193Sjb return (*(const uint32_t *)ap - *(const uint32_t *)bp); 1628179193Sjb} 1629179193Sjb 1630179193Sjbstatic int 1631179193Sjbfasttrap_uint64_cmp(const void *ap, const void *bp) 1632179193Sjb{ 1633179193Sjb return (*(const uint64_t *)ap - *(const uint64_t *)bp); 1634179193Sjb} 1635179193Sjb 1636179193Sjbstatic int 1637179193Sjbfasttrap_add_probe(fasttrap_probe_spec_t *pdata) 1638179193Sjb{ 1639179193Sjb fasttrap_provider_t *provider; 1640179193Sjb fasttrap_probe_t *pp; 1641179193Sjb fasttrap_tracepoint_t *tp; 1642179193Sjb char *name; 1643211738Srpaulo int i, aframes = 0, whack; 1644179193Sjb 1645179193Sjb /* 1646179193Sjb * There needs to be at least one desired trace point. 1647179193Sjb */ 1648179193Sjb if (pdata->ftps_noffs == 0) 1649179193Sjb return (EINVAL); 1650179193Sjb 1651179193Sjb switch (pdata->ftps_type) { 1652179193Sjb case DTFTP_ENTRY: 1653179193Sjb name = "entry"; 1654179193Sjb aframes = FASTTRAP_ENTRY_AFRAMES; 1655179193Sjb break; 1656179193Sjb case DTFTP_RETURN: 1657179193Sjb name = "return"; 1658179193Sjb aframes = FASTTRAP_RETURN_AFRAMES; 1659179193Sjb break; 1660179193Sjb case DTFTP_OFFSETS: 1661179193Sjb name = NULL; 1662179193Sjb break; 1663179193Sjb default: 1664179193Sjb return (EINVAL); 1665179193Sjb } 1666179193Sjb 1667179193Sjb if ((provider = fasttrap_provider_lookup(pdata->ftps_pid, 1668179193Sjb FASTTRAP_PID_NAME, &pid_attr)) == NULL) 1669179193Sjb return (ESRCH); 1670179193Sjb 1671179193Sjb /* 1672179193Sjb * Increment this reference count to indicate that a consumer is 1673179193Sjb * actively adding a new probe associated with this provider. This 1674179193Sjb * prevents the provider from being deleted -- we'll need to check 1675179193Sjb * for pending deletions when we drop this reference count. 1676179193Sjb */ 1677179193Sjb provider->ftp_ccount++; 1678179193Sjb mutex_exit(&provider->ftp_mtx); 1679179193Sjb 1680179193Sjb /* 1681179193Sjb * Grab the creation lock to ensure consistency between calls to 1682179193Sjb * dtrace_probe_lookup() and dtrace_probe_create() in the face of 1683179193Sjb * other threads creating probes. We must drop the provider lock 1684179193Sjb * before taking this lock to avoid a three-way deadlock with the 1685179193Sjb * DTrace framework. 1686179193Sjb */ 1687179193Sjb mutex_enter(&provider->ftp_cmtx); 1688179193Sjb 1689179193Sjb if (name == NULL) { 1690179193Sjb for (i = 0; i < pdata->ftps_noffs; i++) { 1691179193Sjb char name_str[17]; 1692179193Sjb 1693179193Sjb (void) sprintf(name_str, "%llx", 1694179193Sjb (unsigned long long)pdata->ftps_offs[i]); 1695179193Sjb 1696179193Sjb if (dtrace_probe_lookup(provider->ftp_provid, 1697179193Sjb pdata->ftps_mod, pdata->ftps_func, name_str) != 0) 1698179193Sjb continue; 1699179193Sjb 1700179193Sjb atomic_add_32(&fasttrap_total, 1); 1701179193Sjb 1702179193Sjb if (fasttrap_total > fasttrap_max) { 1703179193Sjb atomic_add_32(&fasttrap_total, -1); 1704179193Sjb goto no_mem; 1705179193Sjb } 1706179193Sjb 1707179193Sjb pp = kmem_zalloc(sizeof (fasttrap_probe_t), KM_SLEEP); 1708179193Sjb 1709179193Sjb pp->ftp_prov = provider; 1710179193Sjb pp->ftp_faddr = pdata->ftps_pc; 1711179193Sjb pp->ftp_fsize = pdata->ftps_size; 1712179193Sjb pp->ftp_pid = pdata->ftps_pid; 1713179193Sjb pp->ftp_ntps = 1; 1714179193Sjb 1715179193Sjb tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), 1716179193Sjb KM_SLEEP); 1717179193Sjb 1718179193Sjb tp->ftt_proc = provider->ftp_proc; 1719179193Sjb tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc; 1720179193Sjb tp->ftt_pid = pdata->ftps_pid; 1721179193Sjb 1722179193Sjb pp->ftp_tps[0].fit_tp = tp; 1723179193Sjb pp->ftp_tps[0].fit_id.fti_probe = pp; 1724179193Sjb pp->ftp_tps[0].fit_id.fti_ptype = pdata->ftps_type; 1725179193Sjb 1726179193Sjb pp->ftp_id = dtrace_probe_create(provider->ftp_provid, 1727179193Sjb pdata->ftps_mod, pdata->ftps_func, name_str, 1728179193Sjb FASTTRAP_OFFSET_AFRAMES, pp); 1729179193Sjb } 1730179193Sjb 1731179193Sjb } else if (dtrace_probe_lookup(provider->ftp_provid, pdata->ftps_mod, 1732179193Sjb pdata->ftps_func, name) == 0) { 1733179193Sjb atomic_add_32(&fasttrap_total, pdata->ftps_noffs); 1734179193Sjb 1735179193Sjb if (fasttrap_total > fasttrap_max) { 1736179193Sjb atomic_add_32(&fasttrap_total, -pdata->ftps_noffs); 1737179193Sjb goto no_mem; 1738179193Sjb } 1739179193Sjb 1740179193Sjb /* 1741179193Sjb * Make sure all tracepoint program counter values are unique. 1742179193Sjb * We later assume that each probe has exactly one tracepoint 1743179193Sjb * for a given pc. 1744179193Sjb */ 1745179193Sjb qsort(pdata->ftps_offs, pdata->ftps_noffs, 1746179193Sjb sizeof (uint64_t), fasttrap_uint64_cmp); 1747179193Sjb for (i = 1; i < pdata->ftps_noffs; i++) { 1748179193Sjb if (pdata->ftps_offs[i] > pdata->ftps_offs[i - 1]) 1749179193Sjb continue; 1750179193Sjb 1751179193Sjb atomic_add_32(&fasttrap_total, -pdata->ftps_noffs); 1752179193Sjb goto no_mem; 1753179193Sjb } 1754179193Sjb 1755179193Sjb ASSERT(pdata->ftps_noffs > 0); 1756179193Sjb pp = kmem_zalloc(offsetof(fasttrap_probe_t, 1757179193Sjb ftp_tps[pdata->ftps_noffs]), KM_SLEEP); 1758179193Sjb 1759179193Sjb pp->ftp_prov = provider; 1760179193Sjb pp->ftp_faddr = pdata->ftps_pc; 1761179193Sjb pp->ftp_fsize = pdata->ftps_size; 1762179193Sjb pp->ftp_pid = pdata->ftps_pid; 1763179193Sjb pp->ftp_ntps = pdata->ftps_noffs; 1764179193Sjb 1765179193Sjb for (i = 0; i < pdata->ftps_noffs; i++) { 1766179193Sjb tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), 1767179193Sjb KM_SLEEP); 1768179193Sjb 1769179193Sjb tp->ftt_proc = provider->ftp_proc; 1770179193Sjb tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc; 1771179193Sjb tp->ftt_pid = pdata->ftps_pid; 1772179193Sjb 1773179193Sjb pp->ftp_tps[i].fit_tp = tp; 1774179193Sjb pp->ftp_tps[i].fit_id.fti_probe = pp; 1775179193Sjb pp->ftp_tps[i].fit_id.fti_ptype = pdata->ftps_type; 1776179193Sjb } 1777179193Sjb 1778179193Sjb pp->ftp_id = dtrace_probe_create(provider->ftp_provid, 1779179193Sjb pdata->ftps_mod, pdata->ftps_func, name, aframes, pp); 1780179193Sjb } 1781179193Sjb 1782179193Sjb mutex_exit(&provider->ftp_cmtx); 1783179193Sjb 1784179193Sjb /* 1785179193Sjb * We know that the provider is still valid since we incremented the 1786179193Sjb * creation reference count. If someone tried to clean up this provider 1787179193Sjb * while we were using it (e.g. because the process called exec(2) or 1788179193Sjb * exit(2)), take note of that and try to clean it up now. 1789179193Sjb */ 1790179193Sjb mutex_enter(&provider->ftp_mtx); 1791179193Sjb provider->ftp_ccount--; 1792179193Sjb whack = provider->ftp_retired; 1793179193Sjb mutex_exit(&provider->ftp_mtx); 1794179193Sjb 1795179193Sjb if (whack) 1796179193Sjb fasttrap_pid_cleanup(); 1797179193Sjb 1798179193Sjb return (0); 1799179193Sjb 1800179193Sjbno_mem: 1801179193Sjb /* 1802179193Sjb * If we've exhausted the allowable resources, we'll try to remove 1803179193Sjb * this provider to free some up. This is to cover the case where 1804179193Sjb * the user has accidentally created many more probes than was 1805179193Sjb * intended (e.g. pid123:::). 1806179193Sjb */ 1807179193Sjb mutex_exit(&provider->ftp_cmtx); 1808179193Sjb mutex_enter(&provider->ftp_mtx); 1809179193Sjb provider->ftp_ccount--; 1810179193Sjb provider->ftp_marked = 1; 1811179193Sjb mutex_exit(&provider->ftp_mtx); 1812179193Sjb 1813179193Sjb fasttrap_pid_cleanup(); 1814179193Sjb 1815179193Sjb return (ENOMEM); 1816179193Sjb} 1817179193Sjb 1818179193Sjb/*ARGSUSED*/ 1819179193Sjbstatic void * 1820179193Sjbfasttrap_meta_provide(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid) 1821179193Sjb{ 1822179193Sjb fasttrap_provider_t *provider; 1823179193Sjb 1824179193Sjb /* 1825179193Sjb * A 32-bit unsigned integer (like a pid for example) can be 1826179193Sjb * expressed in 10 or fewer decimal digits. Make sure that we'll 1827179193Sjb * have enough space for the provider name. 1828179193Sjb */ 1829179193Sjb if (strlen(dhpv->dthpv_provname) + 10 >= 1830179193Sjb sizeof (provider->ftp_name)) { 1831211738Srpaulo printf("failed to instantiate provider %s: " 1832179193Sjb "name too long to accomodate pid", dhpv->dthpv_provname); 1833179193Sjb return (NULL); 1834179193Sjb } 1835179193Sjb 1836179193Sjb /* 1837179193Sjb * Don't let folks spoof the true pid provider. 1838179193Sjb */ 1839179193Sjb if (strcmp(dhpv->dthpv_provname, FASTTRAP_PID_NAME) == 0) { 1840211738Srpaulo printf("failed to instantiate provider %s: " 1841179193Sjb "%s is an invalid name", dhpv->dthpv_provname, 1842179193Sjb FASTTRAP_PID_NAME); 1843179193Sjb return (NULL); 1844179193Sjb } 1845179193Sjb 1846179193Sjb /* 1847179193Sjb * The highest stability class that fasttrap supports is ISA; cap 1848179193Sjb * the stability of the new provider accordingly. 1849179193Sjb */ 1850179193Sjb if (dhpv->dthpv_pattr.dtpa_provider.dtat_class > DTRACE_CLASS_ISA) 1851179193Sjb dhpv->dthpv_pattr.dtpa_provider.dtat_class = DTRACE_CLASS_ISA; 1852179193Sjb if (dhpv->dthpv_pattr.dtpa_mod.dtat_class > DTRACE_CLASS_ISA) 1853179193Sjb dhpv->dthpv_pattr.dtpa_mod.dtat_class = DTRACE_CLASS_ISA; 1854179193Sjb if (dhpv->dthpv_pattr.dtpa_func.dtat_class > DTRACE_CLASS_ISA) 1855179193Sjb dhpv->dthpv_pattr.dtpa_func.dtat_class = DTRACE_CLASS_ISA; 1856179193Sjb if (dhpv->dthpv_pattr.dtpa_name.dtat_class > DTRACE_CLASS_ISA) 1857179193Sjb dhpv->dthpv_pattr.dtpa_name.dtat_class = DTRACE_CLASS_ISA; 1858179193Sjb if (dhpv->dthpv_pattr.dtpa_args.dtat_class > DTRACE_CLASS_ISA) 1859179193Sjb dhpv->dthpv_pattr.dtpa_args.dtat_class = DTRACE_CLASS_ISA; 1860179193Sjb 1861179193Sjb if ((provider = fasttrap_provider_lookup(pid, dhpv->dthpv_provname, 1862179193Sjb &dhpv->dthpv_pattr)) == NULL) { 1863211738Srpaulo printf("failed to instantiate provider %s for " 1864179193Sjb "process %u", dhpv->dthpv_provname, (uint_t)pid); 1865179193Sjb return (NULL); 1866179193Sjb } 1867179193Sjb 1868179193Sjb /* 1869179193Sjb * Up the meta provider count so this provider isn't removed until 1870179193Sjb * the meta provider has been told to remove it. 1871179193Sjb */ 1872179193Sjb provider->ftp_mcount++; 1873179193Sjb 1874179193Sjb mutex_exit(&provider->ftp_mtx); 1875179193Sjb 1876179193Sjb return (provider); 1877179193Sjb} 1878179193Sjb 1879179193Sjb/*ARGSUSED*/ 1880179193Sjbstatic void 1881179193Sjbfasttrap_meta_create_probe(void *arg, void *parg, 1882179193Sjb dtrace_helper_probedesc_t *dhpb) 1883179193Sjb{ 1884179193Sjb fasttrap_provider_t *provider = parg; 1885179193Sjb fasttrap_probe_t *pp; 1886179193Sjb fasttrap_tracepoint_t *tp; 1887179193Sjb int i, j; 1888179193Sjb uint32_t ntps; 1889179193Sjb 1890179193Sjb /* 1891179193Sjb * Since the meta provider count is non-zero we don't have to worry 1892179193Sjb * about this provider disappearing. 1893179193Sjb */ 1894179193Sjb ASSERT(provider->ftp_mcount > 0); 1895179193Sjb 1896179193Sjb /* 1897179193Sjb * The offsets must be unique. 1898179193Sjb */ 1899179193Sjb qsort(dhpb->dthpb_offs, dhpb->dthpb_noffs, sizeof (uint32_t), 1900179193Sjb fasttrap_uint32_cmp); 1901179193Sjb for (i = 1; i < dhpb->dthpb_noffs; i++) { 1902179193Sjb if (dhpb->dthpb_base + dhpb->dthpb_offs[i] <= 1903179193Sjb dhpb->dthpb_base + dhpb->dthpb_offs[i - 1]) 1904179193Sjb return; 1905179193Sjb } 1906179193Sjb 1907179193Sjb qsort(dhpb->dthpb_enoffs, dhpb->dthpb_nenoffs, sizeof (uint32_t), 1908179193Sjb fasttrap_uint32_cmp); 1909179193Sjb for (i = 1; i < dhpb->dthpb_nenoffs; i++) { 1910179193Sjb if (dhpb->dthpb_base + dhpb->dthpb_enoffs[i] <= 1911179193Sjb dhpb->dthpb_base + dhpb->dthpb_enoffs[i - 1]) 1912179193Sjb return; 1913179193Sjb } 1914179193Sjb 1915179193Sjb /* 1916179193Sjb * Grab the creation lock to ensure consistency between calls to 1917179193Sjb * dtrace_probe_lookup() and dtrace_probe_create() in the face of 1918179193Sjb * other threads creating probes. 1919179193Sjb */ 1920179193Sjb mutex_enter(&provider->ftp_cmtx); 1921179193Sjb 1922179193Sjb if (dtrace_probe_lookup(provider->ftp_provid, dhpb->dthpb_mod, 1923179193Sjb dhpb->dthpb_func, dhpb->dthpb_name) != 0) { 1924179193Sjb mutex_exit(&provider->ftp_cmtx); 1925179193Sjb return; 1926179193Sjb } 1927179193Sjb 1928179193Sjb ntps = dhpb->dthpb_noffs + dhpb->dthpb_nenoffs; 1929179193Sjb ASSERT(ntps > 0); 1930179193Sjb 1931179193Sjb atomic_add_32(&fasttrap_total, ntps); 1932179193Sjb 1933179193Sjb if (fasttrap_total > fasttrap_max) { 1934179193Sjb atomic_add_32(&fasttrap_total, -ntps); 1935179193Sjb mutex_exit(&provider->ftp_cmtx); 1936179193Sjb return; 1937179193Sjb } 1938179193Sjb 1939179193Sjb pp = kmem_zalloc(offsetof(fasttrap_probe_t, ftp_tps[ntps]), KM_SLEEP); 1940179193Sjb 1941179193Sjb pp->ftp_prov = provider; 1942179193Sjb pp->ftp_pid = provider->ftp_pid; 1943179193Sjb pp->ftp_ntps = ntps; 1944179193Sjb pp->ftp_nargs = dhpb->dthpb_xargc; 1945179193Sjb pp->ftp_xtypes = dhpb->dthpb_xtypes; 1946179193Sjb pp->ftp_ntypes = dhpb->dthpb_ntypes; 1947179193Sjb 1948179193Sjb /* 1949179193Sjb * First create a tracepoint for each actual point of interest. 1950179193Sjb */ 1951179193Sjb for (i = 0; i < dhpb->dthpb_noffs; i++) { 1952179193Sjb tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP); 1953179193Sjb 1954179193Sjb tp->ftt_proc = provider->ftp_proc; 1955179193Sjb tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_offs[i]; 1956179193Sjb tp->ftt_pid = provider->ftp_pid; 1957179193Sjb 1958179193Sjb pp->ftp_tps[i].fit_tp = tp; 1959179193Sjb pp->ftp_tps[i].fit_id.fti_probe = pp; 1960179193Sjb#ifdef __sparc 1961179193Sjb pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_POST_OFFSETS; 1962179193Sjb#else 1963179193Sjb pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_OFFSETS; 1964179193Sjb#endif 1965179193Sjb } 1966179193Sjb 1967179193Sjb /* 1968179193Sjb * Then create a tracepoint for each is-enabled point. 1969179193Sjb */ 1970179193Sjb for (j = 0; i < ntps; i++, j++) { 1971179193Sjb tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP); 1972179193Sjb 1973179193Sjb tp->ftt_proc = provider->ftp_proc; 1974179193Sjb tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_enoffs[j]; 1975179193Sjb tp->ftt_pid = provider->ftp_pid; 1976179193Sjb 1977179193Sjb pp->ftp_tps[i].fit_tp = tp; 1978179193Sjb pp->ftp_tps[i].fit_id.fti_probe = pp; 1979179193Sjb pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_IS_ENABLED; 1980179193Sjb } 1981179193Sjb 1982179193Sjb /* 1983179193Sjb * If the arguments are shuffled around we set the argument remapping 1984179193Sjb * table. Later, when the probe fires, we only remap the arguments 1985179193Sjb * if the table is non-NULL. 1986179193Sjb */ 1987179193Sjb for (i = 0; i < dhpb->dthpb_xargc; i++) { 1988179193Sjb if (dhpb->dthpb_args[i] != i) { 1989179193Sjb pp->ftp_argmap = dhpb->dthpb_args; 1990179193Sjb break; 1991179193Sjb } 1992179193Sjb } 1993179193Sjb 1994179193Sjb /* 1995179193Sjb * The probe is fully constructed -- register it with DTrace. 1996179193Sjb */ 1997179193Sjb pp->ftp_id = dtrace_probe_create(provider->ftp_provid, dhpb->dthpb_mod, 1998179193Sjb dhpb->dthpb_func, dhpb->dthpb_name, FASTTRAP_OFFSET_AFRAMES, pp); 1999179193Sjb 2000179193Sjb mutex_exit(&provider->ftp_cmtx); 2001179193Sjb} 2002179193Sjb 2003179193Sjb/*ARGSUSED*/ 2004179193Sjbstatic void 2005179193Sjbfasttrap_meta_remove(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid) 2006179193Sjb{ 2007179193Sjb /* 2008179193Sjb * Clean up the USDT provider. There may be active consumers of the 2009179193Sjb * provider busy adding probes, no damage will actually befall the 2010179193Sjb * provider until that count has dropped to zero. This just puts 2011179193Sjb * the provider on death row. 2012179193Sjb */ 2013179193Sjb fasttrap_provider_retire(pid, dhpv->dthpv_provname, 1); 2014179193Sjb} 2015179193Sjb 2016179193Sjbstatic dtrace_mops_t fasttrap_mops = { 2017179193Sjb fasttrap_meta_create_probe, 2018179193Sjb fasttrap_meta_provide, 2019179193Sjb fasttrap_meta_remove 2020179193Sjb}; 2021179193Sjb 2022179193Sjb/*ARGSUSED*/ 2023179193Sjbstatic int 2024211738Srpaulofasttrap_open(struct cdev *dev __unused, int oflags __unused, 2025211738Srpaulo int devtype __unused, struct thread *td __unused) 2026179193Sjb{ 2027179193Sjb return (0); 2028179193Sjb} 2029179193Sjb 2030179193Sjb/*ARGSUSED*/ 2031179193Sjbstatic int 2032211738Srpaulofasttrap_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int fflag, 2033211738Srpaulo struct thread *td) 2034179193Sjb{ 2035211738Srpaulo#ifdef notyet 2036211738Srpaulo struct kinfo_proc kp; 2037211738Srpaulo const cred_t *cr = td->td_ucred; 2038211738Srpaulo#endif 2039179193Sjb if (!dtrace_attached()) 2040179193Sjb return (EAGAIN); 2041179193Sjb 2042179193Sjb if (cmd == FASTTRAPIOC_MAKEPROBE) { 2043179193Sjb fasttrap_probe_spec_t *uprobe = (void *)arg; 2044179193Sjb fasttrap_probe_spec_t *probe; 2045179193Sjb uint64_t noffs; 2046179193Sjb size_t size; 2047179193Sjb int ret; 2048179193Sjb char *c; 2049179193Sjb 2050211738Srpaulo#if defined(sun) 2051179193Sjb if (copyin(&uprobe->ftps_noffs, &noffs, 2052179193Sjb sizeof (uprobe->ftps_noffs))) 2053179193Sjb return (EFAULT); 2054211738Srpaulo#else 2055211738Srpaulo noffs = uprobe->ftps_noffs; 2056211738Srpaulo#endif 2057179193Sjb 2058179193Sjb /* 2059179193Sjb * Probes must have at least one tracepoint. 2060179193Sjb */ 2061179193Sjb if (noffs == 0) 2062179193Sjb return (EINVAL); 2063179193Sjb 2064179193Sjb size = sizeof (fasttrap_probe_spec_t) + 2065179193Sjb sizeof (probe->ftps_offs[0]) * (noffs - 1); 2066179193Sjb 2067179193Sjb if (size > 1024 * 1024) 2068179193Sjb return (ENOMEM); 2069179193Sjb 2070179193Sjb probe = kmem_alloc(size, KM_SLEEP); 2071179193Sjb 2072211738Srpaulo#if defined(sun) 2073179193Sjb if (copyin(uprobe, probe, size) != 0) { 2074179193Sjb kmem_free(probe, size); 2075179193Sjb return (EFAULT); 2076179193Sjb } 2077211738Srpaulo#else 2078211738Srpaulo memcpy(probe, uprobe, sizeof(*probe)); 2079211738Srpaulo if (noffs > 1 && copyin(uprobe + 1, probe + 1, size) != 0) { 2080211738Srpaulo kmem_free(probe, size); 2081211738Srpaulo return (EFAULT); 2082211738Srpaulo } 2083211738Srpaulo#endif 2084179193Sjb 2085211738Srpaulo 2086179193Sjb /* 2087179193Sjb * Verify that the function and module strings contain no 2088179193Sjb * funny characters. 2089179193Sjb */ 2090179193Sjb for (c = &probe->ftps_func[0]; *c != '\0'; c++) { 2091179193Sjb if (*c < 0x20 || 0x7f <= *c) { 2092179193Sjb ret = EINVAL; 2093179193Sjb goto err; 2094179193Sjb } 2095179193Sjb } 2096179193Sjb 2097179193Sjb for (c = &probe->ftps_mod[0]; *c != '\0'; c++) { 2098179193Sjb if (*c < 0x20 || 0x7f <= *c) { 2099179193Sjb ret = EINVAL; 2100179193Sjb goto err; 2101179193Sjb } 2102179193Sjb } 2103179193Sjb 2104211738Srpaulo#ifdef notyet 2105179193Sjb if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) { 2106179193Sjb proc_t *p; 2107179193Sjb pid_t pid = probe->ftps_pid; 2108179193Sjb 2109211738Srpaulo#if defined(sun) 2110179193Sjb mutex_enter(&pidlock); 2111211738Srpaulo#endif 2112179193Sjb /* 2113179193Sjb * Report an error if the process doesn't exist 2114179193Sjb * or is actively being birthed. 2115179193Sjb */ 2116211738Srpaulo p = pfind(pid); 2117211738Srpaulo if (p) 2118211738Srpaulo fill_kinfo_proc(p, &kp); 2119211738Srpaulo if (p == NULL || kp.ki_stat == SIDL) { 2120211738Srpaulo#if defined(sun) 2121179193Sjb mutex_exit(&pidlock); 2122211738Srpaulo#endif 2123179193Sjb return (ESRCH); 2124179193Sjb } 2125211738Srpaulo#if defined(sun) 2126179193Sjb mutex_enter(&p->p_lock); 2127179193Sjb mutex_exit(&pidlock); 2128211738Srpaulo#else 2129211738Srpaulo PROC_LOCK_ASSERT(p, MA_OWNED); 2130211738Srpaulo#endif 2131179193Sjb 2132211738Srpaulo#ifdef notyet 2133179193Sjb if ((ret = priv_proc_cred_perm(cr, p, NULL, 2134179193Sjb VREAD | VWRITE)) != 0) { 2135211738Srpaulo#if defined(sun) 2136179193Sjb mutex_exit(&p->p_lock); 2137211738Srpaulo#else 2138211738Srpaulo PROC_UNLOCK(p); 2139211738Srpaulo#endif 2140179193Sjb return (ret); 2141179193Sjb } 2142211738Srpaulo#endif /* notyet */ 2143211738Srpaulo#if defined(sun) 2144179193Sjb mutex_exit(&p->p_lock); 2145211738Srpaulo#else 2146211738Srpaulo PROC_UNLOCK(p); 2147211738Srpaulo#endif 2148179193Sjb } 2149211738Srpaulo#endif /* notyet */ 2150179193Sjb 2151179193Sjb ret = fasttrap_add_probe(probe); 2152179193Sjberr: 2153179193Sjb kmem_free(probe, size); 2154179193Sjb 2155179193Sjb return (ret); 2156179193Sjb 2157179193Sjb } else if (cmd == FASTTRAPIOC_GETINSTR) { 2158179193Sjb fasttrap_instr_query_t instr; 2159179193Sjb fasttrap_tracepoint_t *tp; 2160179193Sjb uint_t index; 2161211738Srpaulo#if defined(sun) 2162179193Sjb int ret; 2163211738Srpaulo#endif 2164179193Sjb 2165211738Srpaulo#if defined(sun) 2166179193Sjb if (copyin((void *)arg, &instr, sizeof (instr)) != 0) 2167179193Sjb return (EFAULT); 2168211738Srpaulo#endif 2169179193Sjb 2170211738Srpaulo#ifdef notyet 2171179193Sjb if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) { 2172179193Sjb proc_t *p; 2173179193Sjb pid_t pid = instr.ftiq_pid; 2174179193Sjb 2175211738Srpaulo#if defined(sun) 2176179193Sjb mutex_enter(&pidlock); 2177211738Srpaulo#endif 2178179193Sjb /* 2179179193Sjb * Report an error if the process doesn't exist 2180179193Sjb * or is actively being birthed. 2181179193Sjb */ 2182211738Srpaulo p = pfind(pid); 2183211738Srpaulo if (p) 2184211738Srpaulo fill_kinfo_proc(p, &kp); 2185211738Srpaulo if (p == NULL || kp.ki_stat == SIDL) { 2186211738Srpaulo#if defined(sun) 2187179193Sjb mutex_exit(&pidlock); 2188211738Srpaulo#endif 2189179193Sjb return (ESRCH); 2190179193Sjb } 2191211738Srpaulo#if defined(sun) 2192179193Sjb mutex_enter(&p->p_lock); 2193179193Sjb mutex_exit(&pidlock); 2194211738Srpaulo#else 2195211738Srpaulo PROC_LOCK_ASSERT(p, MA_OWNED); 2196211738Srpaulo#endif 2197179193Sjb 2198211738Srpaulo#ifdef notyet 2199179193Sjb if ((ret = priv_proc_cred_perm(cr, p, NULL, 2200179193Sjb VREAD)) != 0) { 2201211738Srpaulo#if defined(sun) 2202179193Sjb mutex_exit(&p->p_lock); 2203211738Srpaulo#else 2204211738Srpaulo PROC_UNLOCK(p); 2205211738Srpaulo#endif 2206179193Sjb return (ret); 2207179193Sjb } 2208211738Srpaulo#endif /* notyet */ 2209179193Sjb 2210211738Srpaulo#if defined(sun) 2211179193Sjb mutex_exit(&p->p_lock); 2212211738Srpaulo#else 2213211738Srpaulo PROC_UNLOCK(p); 2214211738Srpaulo#endif 2215179193Sjb } 2216211738Srpaulo#endif /* notyet */ 2217179193Sjb 2218179193Sjb index = FASTTRAP_TPOINTS_INDEX(instr.ftiq_pid, instr.ftiq_pc); 2219179193Sjb 2220179193Sjb mutex_enter(&fasttrap_tpoints.fth_table[index].ftb_mtx); 2221179193Sjb tp = fasttrap_tpoints.fth_table[index].ftb_data; 2222179193Sjb while (tp != NULL) { 2223179193Sjb if (instr.ftiq_pid == tp->ftt_pid && 2224179193Sjb instr.ftiq_pc == tp->ftt_pc && 2225179193Sjb tp->ftt_proc->ftpc_acount != 0) 2226179193Sjb break; 2227179193Sjb 2228179193Sjb tp = tp->ftt_next; 2229179193Sjb } 2230179193Sjb 2231179193Sjb if (tp == NULL) { 2232179193Sjb mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx); 2233179193Sjb return (ENOENT); 2234179193Sjb } 2235179193Sjb 2236179193Sjb bcopy(&tp->ftt_instr, &instr.ftiq_instr, 2237179193Sjb sizeof (instr.ftiq_instr)); 2238179193Sjb mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx); 2239179193Sjb 2240179193Sjb if (copyout(&instr, (void *)arg, sizeof (instr)) != 0) 2241179193Sjb return (EFAULT); 2242179193Sjb 2243179193Sjb return (0); 2244179193Sjb } 2245179193Sjb 2246179193Sjb return (EINVAL); 2247179193Sjb} 2248179193Sjb 2249179193Sjbstatic int 2250211738Srpaulofasttrap_load(void) 2251179193Sjb{ 2252179193Sjb ulong_t nent; 2253211738Srpaulo int i; 2254179193Sjb 2255211738Srpaulo /* Create the /dev/dtrace/fasttrap entry. */ 2256211738Srpaulo fasttrap_cdev = make_dev(&fasttrap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, 2257211738Srpaulo "dtrace/fasttrap"); 2258179193Sjb 2259211738Srpaulo mtx_init(&fasttrap_cleanup_mtx, "fasttrap clean", "dtrace", MTX_DEF); 2260211738Srpaulo callout_init_mtx(&fasttrap_timeout, &fasttrap_cleanup_mtx, 0); 2261211738Srpaulo mutex_init(&fasttrap_count_mtx, "fasttrap count mtx", MUTEX_DEFAULT, 2262211738Srpaulo NULL); 2263179193Sjb 2264179193Sjb /* 2265179193Sjb * Install our hooks into fork(2), exec(2), and exit(2). 2266179193Sjb */ 2267211738Srpaulo dtrace_fasttrap_fork = &fasttrap_fork; 2268211738Srpaulo dtrace_fasttrap_exit = &fasttrap_exec_exit; 2269211738Srpaulo dtrace_fasttrap_exec = &fasttrap_exec_exit; 2270179193Sjb 2271211738Srpaulo#if defined(sun) 2272179193Sjb fasttrap_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 2273179193Sjb "fasttrap-max-probes", FASTTRAP_MAX_DEFAULT); 2274211738Srpaulo#else 2275211738Srpaulo fasttrap_max = FASTTRAP_MAX_DEFAULT; 2276211738Srpaulo#endif 2277179193Sjb fasttrap_total = 0; 2278179193Sjb 2279179193Sjb /* 2280179193Sjb * Conjure up the tracepoints hashtable... 2281179193Sjb */ 2282211738Srpaulo#if defined(sun) 2283179193Sjb nent = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 2284179193Sjb "fasttrap-hash-size", FASTTRAP_TPOINTS_DEFAULT_SIZE); 2285211738Srpaulo#else 2286211738Srpaulo nent = FASTTRAP_TPOINTS_DEFAULT_SIZE; 2287211738Srpaulo#endif 2288179193Sjb 2289179193Sjb if (nent == 0 || nent > 0x1000000) 2290179193Sjb nent = FASTTRAP_TPOINTS_DEFAULT_SIZE; 2291179193Sjb 2292179193Sjb if ((nent & (nent - 1)) == 0) 2293179193Sjb fasttrap_tpoints.fth_nent = nent; 2294179193Sjb else 2295179193Sjb fasttrap_tpoints.fth_nent = 1 << fasttrap_highbit(nent); 2296179193Sjb ASSERT(fasttrap_tpoints.fth_nent > 0); 2297179193Sjb fasttrap_tpoints.fth_mask = fasttrap_tpoints.fth_nent - 1; 2298179193Sjb fasttrap_tpoints.fth_table = kmem_zalloc(fasttrap_tpoints.fth_nent * 2299179193Sjb sizeof (fasttrap_bucket_t), KM_SLEEP); 2300211738Srpaulo#if !defined(sun) 2301211738Srpaulo for (i = 0; i < fasttrap_tpoints.fth_nent; i++) 2302211738Srpaulo mutex_init(&fasttrap_tpoints.fth_table[i].ftb_mtx, 2303211738Srpaulo "tracepoints bucket mtx", MUTEX_DEFAULT, NULL); 2304211738Srpaulo#endif 2305179193Sjb 2306179193Sjb /* 2307179193Sjb * ... and the providers hash table... 2308179193Sjb */ 2309179193Sjb nent = FASTTRAP_PROVIDERS_DEFAULT_SIZE; 2310179193Sjb if ((nent & (nent - 1)) == 0) 2311179193Sjb fasttrap_provs.fth_nent = nent; 2312179193Sjb else 2313179193Sjb fasttrap_provs.fth_nent = 1 << fasttrap_highbit(nent); 2314179193Sjb ASSERT(fasttrap_provs.fth_nent > 0); 2315179193Sjb fasttrap_provs.fth_mask = fasttrap_provs.fth_nent - 1; 2316179193Sjb fasttrap_provs.fth_table = kmem_zalloc(fasttrap_provs.fth_nent * 2317179193Sjb sizeof (fasttrap_bucket_t), KM_SLEEP); 2318211738Srpaulo#if !defined(sun) 2319211738Srpaulo for (i = 0; i < fasttrap_provs.fth_nent; i++) 2320211738Srpaulo mutex_init(&fasttrap_provs.fth_table[i].ftb_mtx, 2321211738Srpaulo "providers bucket mtx", MUTEX_DEFAULT, NULL); 2322211738Srpaulo#endif 2323179193Sjb 2324179193Sjb /* 2325179193Sjb * ... and the procs hash table. 2326179193Sjb */ 2327179193Sjb nent = FASTTRAP_PROCS_DEFAULT_SIZE; 2328179193Sjb if ((nent & (nent - 1)) == 0) 2329179193Sjb fasttrap_procs.fth_nent = nent; 2330179193Sjb else 2331179193Sjb fasttrap_procs.fth_nent = 1 << fasttrap_highbit(nent); 2332179193Sjb ASSERT(fasttrap_procs.fth_nent > 0); 2333179193Sjb fasttrap_procs.fth_mask = fasttrap_procs.fth_nent - 1; 2334179193Sjb fasttrap_procs.fth_table = kmem_zalloc(fasttrap_procs.fth_nent * 2335179193Sjb sizeof (fasttrap_bucket_t), KM_SLEEP); 2336211738Srpaulo#if !defined(sun) 2337211738Srpaulo for (i = 0; i < fasttrap_procs.fth_nent; i++) 2338211738Srpaulo mutex_init(&fasttrap_procs.fth_table[i].ftb_mtx, 2339211738Srpaulo "processes bucket mtx", MUTEX_DEFAULT, NULL); 2340211925Srpaulo 2341211925Srpaulo CPU_FOREACH(i) { 2342211925Srpaulo mutex_init(&fasttrap_cpuc_pid_lock[i], "fasttrap barrier", 2343211925Srpaulo MUTEX_DEFAULT, NULL); 2344211925Srpaulo } 2345211738Srpaulo#endif 2346179193Sjb 2347179193Sjb (void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL, 2348179193Sjb &fasttrap_meta_id); 2349179193Sjb 2350211738Srpaulo return (0); 2351179193Sjb} 2352179193Sjb 2353179193Sjbstatic int 2354211738Srpaulofasttrap_unload(void) 2355179193Sjb{ 2356179193Sjb int i, fail = 0; 2357179193Sjb 2358179193Sjb /* 2359179193Sjb * Unregister the meta-provider to make sure no new fasttrap- 2360179193Sjb * managed providers come along while we're trying to close up 2361179193Sjb * shop. If we fail to detach, we'll need to re-register as a 2362179193Sjb * meta-provider. We can fail to unregister as a meta-provider 2363179193Sjb * if providers we manage still exist. 2364179193Sjb */ 2365179193Sjb if (fasttrap_meta_id != DTRACE_METAPROVNONE && 2366179193Sjb dtrace_meta_unregister(fasttrap_meta_id) != 0) 2367211738Srpaulo return (-1); 2368179193Sjb 2369179193Sjb /* 2370179193Sjb * Prevent any new timeouts from running by setting fasttrap_timeout 2371179193Sjb * to a non-zero value, and wait for the current timeout to complete. 2372179193Sjb */ 2373211738Srpaulo mtx_lock(&fasttrap_cleanup_mtx); 2374179193Sjb fasttrap_cleanup_work = 0; 2375211738Srpaulo callout_drain(&fasttrap_timeout); 2376211738Srpaulo mtx_unlock(&fasttrap_cleanup_mtx); 2377179193Sjb 2378179193Sjb /* 2379179193Sjb * Iterate over all of our providers. If there's still a process 2380179193Sjb * that corresponds to that pid, fail to detach. 2381179193Sjb */ 2382179193Sjb for (i = 0; i < fasttrap_provs.fth_nent; i++) { 2383179193Sjb fasttrap_provider_t **fpp, *fp; 2384179193Sjb fasttrap_bucket_t *bucket = &fasttrap_provs.fth_table[i]; 2385179193Sjb 2386179193Sjb mutex_enter(&bucket->ftb_mtx); 2387179193Sjb fpp = (fasttrap_provider_t **)&bucket->ftb_data; 2388179193Sjb while ((fp = *fpp) != NULL) { 2389179193Sjb /* 2390179193Sjb * Acquire and release the lock as a simple way of 2391179193Sjb * waiting for any other consumer to finish with 2392179193Sjb * this provider. A thread must first acquire the 2393179193Sjb * bucket lock so there's no chance of another thread 2394179193Sjb * blocking on the provider's lock. 2395179193Sjb */ 2396179193Sjb mutex_enter(&fp->ftp_mtx); 2397179193Sjb mutex_exit(&fp->ftp_mtx); 2398179193Sjb 2399179193Sjb if (dtrace_unregister(fp->ftp_provid) != 0) { 2400179193Sjb fail = 1; 2401179193Sjb fpp = &fp->ftp_next; 2402179193Sjb } else { 2403179193Sjb *fpp = fp->ftp_next; 2404179193Sjb fasttrap_provider_free(fp); 2405179193Sjb } 2406179193Sjb } 2407179193Sjb 2408179193Sjb mutex_exit(&bucket->ftb_mtx); 2409179193Sjb } 2410179193Sjb 2411179193Sjb if (fail) { 2412179193Sjb uint_t work; 2413179193Sjb /* 2414179193Sjb * If we're failing to detach, we need to unblock timeouts 2415179193Sjb * and start a new timeout if any work has accumulated while 2416179193Sjb * we've been unsuccessfully trying to detach. 2417179193Sjb */ 2418211738Srpaulo mtx_lock(&fasttrap_cleanup_mtx); 2419179193Sjb work = fasttrap_cleanup_work; 2420211738Srpaulo callout_drain(&fasttrap_timeout); 2421211738Srpaulo mtx_unlock(&fasttrap_cleanup_mtx); 2422179193Sjb 2423179193Sjb if (work) 2424179193Sjb fasttrap_pid_cleanup(); 2425179193Sjb 2426179193Sjb (void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL, 2427179193Sjb &fasttrap_meta_id); 2428179193Sjb 2429211738Srpaulo return (-1); 2430179193Sjb } 2431179193Sjb 2432179193Sjb#ifdef DEBUG 2433179193Sjb mutex_enter(&fasttrap_count_mtx); 2434179193Sjb ASSERT(fasttrap_pid_count == 0); 2435179193Sjb mutex_exit(&fasttrap_count_mtx); 2436179193Sjb#endif 2437179193Sjb 2438179193Sjb kmem_free(fasttrap_tpoints.fth_table, 2439179193Sjb fasttrap_tpoints.fth_nent * sizeof (fasttrap_bucket_t)); 2440179193Sjb fasttrap_tpoints.fth_nent = 0; 2441179193Sjb 2442179193Sjb kmem_free(fasttrap_provs.fth_table, 2443179193Sjb fasttrap_provs.fth_nent * sizeof (fasttrap_bucket_t)); 2444179193Sjb fasttrap_provs.fth_nent = 0; 2445179193Sjb 2446179193Sjb kmem_free(fasttrap_procs.fth_table, 2447179193Sjb fasttrap_procs.fth_nent * sizeof (fasttrap_bucket_t)); 2448179193Sjb fasttrap_procs.fth_nent = 0; 2449179193Sjb 2450179193Sjb /* 2451179193Sjb * We know there are no tracepoints in any process anywhere in 2452179193Sjb * the system so there is no process which has its p_dtrace_count 2453179193Sjb * greater than zero, therefore we know that no thread can actively 2454179193Sjb * be executing code in fasttrap_fork(). Similarly for p_dtrace_probes 2455179193Sjb * and fasttrap_exec() and fasttrap_exit(). 2456179193Sjb */ 2457211738Srpaulo ASSERT(dtrace_fasttrap_fork == &fasttrap_fork); 2458211738Srpaulo dtrace_fasttrap_fork = NULL; 2459179193Sjb 2460211738Srpaulo ASSERT(dtrace_fasttrap_exec == &fasttrap_exec_exit); 2461211738Srpaulo dtrace_fasttrap_exec = NULL; 2462179193Sjb 2463211738Srpaulo ASSERT(dtrace_fasttrap_exit == &fasttrap_exec_exit); 2464211738Srpaulo dtrace_fasttrap_exit = NULL; 2465179193Sjb 2466211738Srpaulo#if !defined(sun) 2467211738Srpaulo destroy_dev(fasttrap_cdev); 2468211738Srpaulo mutex_destroy(&fasttrap_count_mtx); 2469211925Srpaulo CPU_FOREACH(i) { 2470211925Srpaulo mutex_destroy(&fasttrap_cpuc_pid_lock[i]); 2471211925Srpaulo } 2472211738Srpaulo#endif 2473179193Sjb 2474211738Srpaulo return (0); 2475179193Sjb} 2476179193Sjb 2477211738Srpaulo/* ARGSUSED */ 2478211738Srpaulostatic int 2479211738Srpaulofasttrap_modevent(module_t mod __unused, int type, void *data __unused) 2480211738Srpaulo{ 2481211738Srpaulo int error = 0; 2482179193Sjb 2483211738Srpaulo switch (type) { 2484211738Srpaulo case MOD_LOAD: 2485211738Srpaulo break; 2486179193Sjb 2487211738Srpaulo case MOD_UNLOAD: 2488211738Srpaulo break; 2489179193Sjb 2490211738Srpaulo case MOD_SHUTDOWN: 2491211738Srpaulo break; 2492179193Sjb 2493211738Srpaulo default: 2494211738Srpaulo error = EOPNOTSUPP; 2495211738Srpaulo break; 2496211738Srpaulo } 2497211738Srpaulo return (error); 2498179193Sjb} 2499179193Sjb 2500211738SrpauloSYSINIT(fasttrap_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, fasttrap_load, 2501211738Srpaulo NULL); 2502211738SrpauloSYSUNINIT(fasttrap_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, 2503211738Srpaulo fasttrap_unload, NULL); 2504211738Srpaulo 2505211738SrpauloDEV_MODULE(fasttrap, fasttrap_modevent, NULL); 2506211738SrpauloMODULE_VERSION(fasttrap, 1); 2507211738SrpauloMODULE_DEPEND(fasttrap, dtrace, 1, 1, 1); 2508211738SrpauloMODULE_DEPEND(fasttrap, opensolaris, 1, 1, 1); 2509