10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 53235Sraf * Common Development and Distribution License (the "License"). 63235Sraf * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211399Sahl 220Sstevel@tonic-gate /* 23*11237SJonathan.Haslam@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* 280Sstevel@tonic-gate * DTrace Process Control 290Sstevel@tonic-gate * 300Sstevel@tonic-gate * This file provides a set of routines that permit libdtrace and its clients 310Sstevel@tonic-gate * to create and grab process handles using libproc, and to share these handles 320Sstevel@tonic-gate * between library mechanisms that need libproc access, such as ustack(), and 330Sstevel@tonic-gate * client mechanisms that need libproc access, such as dtrace(1M) -c and -p. 340Sstevel@tonic-gate * The library provides several mechanisms in the libproc control layer: 350Sstevel@tonic-gate * 360Sstevel@tonic-gate * Reference Counting: The library code and client code can independently grab 370Sstevel@tonic-gate * the same process handles without interfering with one another. Only when 380Sstevel@tonic-gate * the reference count drops to zero and the handle is not being cached (see 390Sstevel@tonic-gate * below for more information on caching) will Prelease() be called on it. 400Sstevel@tonic-gate * 410Sstevel@tonic-gate * Handle Caching: If a handle is grabbed PGRAB_RDONLY (e.g. by ustack()) and 420Sstevel@tonic-gate * the reference count drops to zero, the handle is not immediately released. 430Sstevel@tonic-gate * Instead, libproc handles are maintained on dph_lrulist in order from most- 440Sstevel@tonic-gate * recently accessed to least-recently accessed. Idle handles are maintained 450Sstevel@tonic-gate * until a pre-defined LRU cache limit is exceeded, permitting repeated calls 460Sstevel@tonic-gate * to ustack() to avoid the overhead of releasing and re-grabbing processes. 470Sstevel@tonic-gate * 480Sstevel@tonic-gate * Process Control: For processes that are grabbed for control (~PGRAB_RDONLY) 490Sstevel@tonic-gate * or created by dt_proc_create(), a control thread is created to provide 500Sstevel@tonic-gate * callbacks on process exit and symbol table caching on dlopen()s. 510Sstevel@tonic-gate * 520Sstevel@tonic-gate * MT-Safety: Libproc is not MT-Safe, so dt_proc_lock() and dt_proc_unlock() 530Sstevel@tonic-gate * are provided to synchronize access to the libproc handle between libdtrace 540Sstevel@tonic-gate * code and client code and the control thread's use of the ps_prochandle. 550Sstevel@tonic-gate * 560Sstevel@tonic-gate * NOTE: MT-Safety is NOT provided for libdtrace itself, or for use of the 570Sstevel@tonic-gate * dtrace_proc_grab/dtrace_proc_create mechanisms. Like all exported libdtrace 580Sstevel@tonic-gate * calls, these are assumed to be MT-Unsafe. MT-Safety is ONLY provided for 590Sstevel@tonic-gate * synchronization between libdtrace control threads and the client thread. 600Sstevel@tonic-gate * 610Sstevel@tonic-gate * The ps_prochandles themselves are maintained along with a dt_proc_t struct 620Sstevel@tonic-gate * in a hash table indexed by PID. This provides basic locking and reference 630Sstevel@tonic-gate * counting. The dt_proc_t is also maintained in LRU order on dph_lrulist. 640Sstevel@tonic-gate * The dph_lrucnt and dph_lrulim count the number of cacheable processes and 650Sstevel@tonic-gate * the current limit on the number of actively cached entries. 660Sstevel@tonic-gate * 670Sstevel@tonic-gate * The control thread for a process establishes breakpoints at the rtld_db 680Sstevel@tonic-gate * locations of interest, updates mappings and symbol tables at these points, 690Sstevel@tonic-gate * and handles exec and fork (by always following the parent). The control 700Sstevel@tonic-gate * thread automatically exits when the process dies or control is lost. 710Sstevel@tonic-gate * 720Sstevel@tonic-gate * A simple notification mechanism is provided for libdtrace clients using 730Sstevel@tonic-gate * dtrace_handle_proc() for notification of PS_UNDEAD or PS_LOST events. If 740Sstevel@tonic-gate * such an event occurs, the dt_proc_t itself is enqueued on a notification 750Sstevel@tonic-gate * list and the control thread broadcasts to dph_cv. dtrace_sleep() will wake 760Sstevel@tonic-gate * up using this condition and will then call the client handler as necessary. 770Sstevel@tonic-gate */ 780Sstevel@tonic-gate 790Sstevel@tonic-gate #include <sys/wait.h> 800Sstevel@tonic-gate #include <sys/lwp.h> 810Sstevel@tonic-gate #include <strings.h> 820Sstevel@tonic-gate #include <signal.h> 830Sstevel@tonic-gate #include <assert.h> 840Sstevel@tonic-gate #include <errno.h> 850Sstevel@tonic-gate 860Sstevel@tonic-gate #include <dt_proc.h> 870Sstevel@tonic-gate #include <dt_pid.h> 880Sstevel@tonic-gate #include <dt_impl.h> 890Sstevel@tonic-gate 900Sstevel@tonic-gate #define IS_SYS_EXEC(w) (w == SYS_exec || w == SYS_execve) 913235Sraf #define IS_SYS_FORK(w) (w == SYS_vfork || w == SYS_fork1 || \ 923235Sraf w == SYS_forkall || w == SYS_forksys) 930Sstevel@tonic-gate 940Sstevel@tonic-gate static dt_bkpt_t * 950Sstevel@tonic-gate dt_proc_bpcreate(dt_proc_t *dpr, uintptr_t addr, dt_bkpt_f *func, void *data) 960Sstevel@tonic-gate { 970Sstevel@tonic-gate struct ps_prochandle *P = dpr->dpr_proc; 980Sstevel@tonic-gate dt_bkpt_t *dbp; 990Sstevel@tonic-gate 1000Sstevel@tonic-gate assert(DT_MUTEX_HELD(&dpr->dpr_lock)); 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate if ((dbp = dt_zalloc(dpr->dpr_hdl, sizeof (dt_bkpt_t))) != NULL) { 1030Sstevel@tonic-gate dbp->dbp_func = func; 1040Sstevel@tonic-gate dbp->dbp_data = data; 1050Sstevel@tonic-gate dbp->dbp_addr = addr; 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate if (Psetbkpt(P, dbp->dbp_addr, &dbp->dbp_instr) == 0) 1080Sstevel@tonic-gate dbp->dbp_active = B_TRUE; 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate dt_list_append(&dpr->dpr_bps, dbp); 1110Sstevel@tonic-gate } 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate return (dbp); 1140Sstevel@tonic-gate } 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate static void 1170Sstevel@tonic-gate dt_proc_bpdestroy(dt_proc_t *dpr, int delbkpts) 1180Sstevel@tonic-gate { 1190Sstevel@tonic-gate int state = Pstate(dpr->dpr_proc); 1200Sstevel@tonic-gate dt_bkpt_t *dbp, *nbp; 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate assert(DT_MUTEX_HELD(&dpr->dpr_lock)); 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate for (dbp = dt_list_next(&dpr->dpr_bps); dbp != NULL; dbp = nbp) { 1250Sstevel@tonic-gate if (delbkpts && dbp->dbp_active && 1260Sstevel@tonic-gate state != PS_LOST && state != PS_UNDEAD) { 1270Sstevel@tonic-gate (void) Pdelbkpt(dpr->dpr_proc, 1280Sstevel@tonic-gate dbp->dbp_addr, dbp->dbp_instr); 1290Sstevel@tonic-gate } 1300Sstevel@tonic-gate nbp = dt_list_next(dbp); 1310Sstevel@tonic-gate dt_list_delete(&dpr->dpr_bps, dbp); 1320Sstevel@tonic-gate dt_free(dpr->dpr_hdl, dbp); 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate } 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate static void 1370Sstevel@tonic-gate dt_proc_bpmatch(dtrace_hdl_t *dtp, dt_proc_t *dpr) 1380Sstevel@tonic-gate { 1390Sstevel@tonic-gate const lwpstatus_t *psp = &Pstatus(dpr->dpr_proc)->pr_lwp; 1400Sstevel@tonic-gate dt_bkpt_t *dbp; 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate assert(DT_MUTEX_HELD(&dpr->dpr_lock)); 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate for (dbp = dt_list_next(&dpr->dpr_bps); 1450Sstevel@tonic-gate dbp != NULL; dbp = dt_list_next(dbp)) { 1460Sstevel@tonic-gate if (psp->pr_reg[R_PC] == dbp->dbp_addr) 1470Sstevel@tonic-gate break; 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate if (dbp == NULL) { 1510Sstevel@tonic-gate dt_dprintf("pid %d: spurious breakpoint wakeup for %lx\n", 1520Sstevel@tonic-gate (int)dpr->dpr_pid, (ulong_t)psp->pr_reg[R_PC]); 1530Sstevel@tonic-gate return; 1540Sstevel@tonic-gate } 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate dt_dprintf("pid %d: hit breakpoint at %lx (%lu)\n", 1570Sstevel@tonic-gate (int)dpr->dpr_pid, (ulong_t)dbp->dbp_addr, ++dbp->dbp_hits); 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate dbp->dbp_func(dtp, dpr, dbp->dbp_data); 1600Sstevel@tonic-gate (void) Pxecbkpt(dpr->dpr_proc, dbp->dbp_instr); 1610Sstevel@tonic-gate } 1620Sstevel@tonic-gate 1634273Sahl static void 1640Sstevel@tonic-gate dt_proc_bpenable(dt_proc_t *dpr) 1650Sstevel@tonic-gate { 1660Sstevel@tonic-gate dt_bkpt_t *dbp; 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate assert(DT_MUTEX_HELD(&dpr->dpr_lock)); 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate for (dbp = dt_list_next(&dpr->dpr_bps); 1710Sstevel@tonic-gate dbp != NULL; dbp = dt_list_next(dbp)) { 1720Sstevel@tonic-gate if (!dbp->dbp_active && Psetbkpt(dpr->dpr_proc, 1730Sstevel@tonic-gate dbp->dbp_addr, &dbp->dbp_instr) == 0) 1740Sstevel@tonic-gate dbp->dbp_active = B_TRUE; 1750Sstevel@tonic-gate } 1761399Sahl 1771399Sahl dt_dprintf("breakpoints enabled\n"); 1780Sstevel@tonic-gate } 1790Sstevel@tonic-gate 1804273Sahl static void 1810Sstevel@tonic-gate dt_proc_bpdisable(dt_proc_t *dpr) 1820Sstevel@tonic-gate { 1830Sstevel@tonic-gate dt_bkpt_t *dbp; 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate assert(DT_MUTEX_HELD(&dpr->dpr_lock)); 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate for (dbp = dt_list_next(&dpr->dpr_bps); 1880Sstevel@tonic-gate dbp != NULL; dbp = dt_list_next(dbp)) { 1890Sstevel@tonic-gate if (dbp->dbp_active && Pdelbkpt(dpr->dpr_proc, 1900Sstevel@tonic-gate dbp->dbp_addr, dbp->dbp_instr) == 0) 1910Sstevel@tonic-gate dbp->dbp_active = B_FALSE; 1920Sstevel@tonic-gate } 1931399Sahl 1941399Sahl dt_dprintf("breakpoints disabled\n"); 1951399Sahl } 1961399Sahl 1971399Sahl static void 1981399Sahl dt_proc_notify(dtrace_hdl_t *dtp, dt_proc_hash_t *dph, dt_proc_t *dpr, 1991399Sahl const char *msg) 2001399Sahl { 2011399Sahl dt_proc_notify_t *dprn = dt_alloc(dtp, sizeof (dt_proc_notify_t)); 2021399Sahl 2031399Sahl if (dprn == NULL) { 2041399Sahl dt_dprintf("failed to allocate notification for %d %s\n", 2051399Sahl (int)dpr->dpr_pid, msg); 2061399Sahl } else { 2071399Sahl dprn->dprn_dpr = dpr; 2081399Sahl if (msg == NULL) 2091399Sahl dprn->dprn_errmsg[0] = '\0'; 2101399Sahl else 2111399Sahl (void) strlcpy(dprn->dprn_errmsg, msg, 2121399Sahl sizeof (dprn->dprn_errmsg)); 2131399Sahl 2141399Sahl (void) pthread_mutex_lock(&dph->dph_lock); 2151399Sahl 2161399Sahl dprn->dprn_next = dph->dph_notify; 2171399Sahl dph->dph_notify = dprn; 2181399Sahl 2191399Sahl (void) pthread_cond_broadcast(&dph->dph_cv); 2201399Sahl (void) pthread_mutex_unlock(&dph->dph_lock); 2211399Sahl } 2220Sstevel@tonic-gate } 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate /* 2250Sstevel@tonic-gate * Check to see if the control thread was requested to stop when the victim 2260Sstevel@tonic-gate * process reached a particular event (why) rather than continuing the victim. 2270Sstevel@tonic-gate * If 'why' is set in the stop mask, we wait on dpr_cv for dt_proc_continue(). 2280Sstevel@tonic-gate * If 'why' is not set, this function returns immediately and does nothing. 2290Sstevel@tonic-gate */ 2300Sstevel@tonic-gate static void 2310Sstevel@tonic-gate dt_proc_stop(dt_proc_t *dpr, uint8_t why) 2320Sstevel@tonic-gate { 2330Sstevel@tonic-gate assert(DT_MUTEX_HELD(&dpr->dpr_lock)); 2340Sstevel@tonic-gate assert(why != DT_PROC_STOP_IDLE); 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate if (dpr->dpr_stop & why) { 2370Sstevel@tonic-gate dpr->dpr_stop |= DT_PROC_STOP_IDLE; 2380Sstevel@tonic-gate dpr->dpr_stop &= ~why; 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate (void) pthread_cond_broadcast(&dpr->dpr_cv); 2410Sstevel@tonic-gate 2424273Sahl /* 2434273Sahl * We disable breakpoints while stopped to preserve the 2444273Sahl * integrity of the program text for both our own disassembly 2454273Sahl * and that of the kernel. 2464273Sahl */ 2474273Sahl dt_proc_bpdisable(dpr); 2484273Sahl 2490Sstevel@tonic-gate while (dpr->dpr_stop & DT_PROC_STOP_IDLE) 2500Sstevel@tonic-gate (void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock); 2514273Sahl 2524273Sahl dt_proc_bpenable(dpr); 2530Sstevel@tonic-gate } 2540Sstevel@tonic-gate } 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate /*ARGSUSED*/ 2570Sstevel@tonic-gate static void 2580Sstevel@tonic-gate dt_proc_bpmain(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *fname) 2590Sstevel@tonic-gate { 2600Sstevel@tonic-gate dt_dprintf("pid %d: breakpoint at %s()\n", (int)dpr->dpr_pid, fname); 2610Sstevel@tonic-gate dt_proc_stop(dpr, DT_PROC_STOP_MAIN); 2620Sstevel@tonic-gate } 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate static void 2650Sstevel@tonic-gate dt_proc_rdevent(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *evname) 2660Sstevel@tonic-gate { 2670Sstevel@tonic-gate rd_event_msg_t rdm; 2680Sstevel@tonic-gate rd_err_e err; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate if ((err = rd_event_getmsg(dpr->dpr_rtld, &rdm)) != RD_OK) { 2710Sstevel@tonic-gate dt_dprintf("pid %d: failed to get %s event message: %s\n", 2720Sstevel@tonic-gate (int)dpr->dpr_pid, evname, rd_errstr(err)); 2730Sstevel@tonic-gate return; 2740Sstevel@tonic-gate } 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate dt_dprintf("pid %d: rtld event %s type=%d state %d\n", 2770Sstevel@tonic-gate (int)dpr->dpr_pid, evname, rdm.type, rdm.u.state); 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate switch (rdm.type) { 2800Sstevel@tonic-gate case RD_DLACTIVITY: 2811399Sahl if (rdm.u.state != RD_CONSISTENT) 2821399Sahl break; 2831399Sahl 2841399Sahl Pupdate_syms(dpr->dpr_proc); 2851399Sahl if (dt_pid_create_probes_module(dtp, dpr) != 0) 2861399Sahl dt_proc_notify(dtp, dtp->dt_procs, dpr, 2871399Sahl dpr->dpr_errmsg); 2881399Sahl 2890Sstevel@tonic-gate break; 2900Sstevel@tonic-gate case RD_PREINIT: 2910Sstevel@tonic-gate Pupdate_syms(dpr->dpr_proc); 2920Sstevel@tonic-gate dt_proc_stop(dpr, DT_PROC_STOP_PREINIT); 2930Sstevel@tonic-gate break; 2940Sstevel@tonic-gate case RD_POSTINIT: 2950Sstevel@tonic-gate Pupdate_syms(dpr->dpr_proc); 2960Sstevel@tonic-gate dt_proc_stop(dpr, DT_PROC_STOP_POSTINIT); 2970Sstevel@tonic-gate break; 2980Sstevel@tonic-gate } 2990Sstevel@tonic-gate } 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate static void 3020Sstevel@tonic-gate dt_proc_rdwatch(dt_proc_t *dpr, rd_event_e event, const char *evname) 3030Sstevel@tonic-gate { 3040Sstevel@tonic-gate rd_notify_t rdn; 3050Sstevel@tonic-gate rd_err_e err; 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate if ((err = rd_event_addr(dpr->dpr_rtld, event, &rdn)) != RD_OK) { 3080Sstevel@tonic-gate dt_dprintf("pid %d: failed to get event address for %s: %s\n", 3090Sstevel@tonic-gate (int)dpr->dpr_pid, evname, rd_errstr(err)); 3100Sstevel@tonic-gate return; 3110Sstevel@tonic-gate } 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate if (rdn.type != RD_NOTIFY_BPT) { 3140Sstevel@tonic-gate dt_dprintf("pid %d: event %s has unexpected type %d\n", 3150Sstevel@tonic-gate (int)dpr->dpr_pid, evname, rdn.type); 3160Sstevel@tonic-gate return; 3170Sstevel@tonic-gate } 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate (void) dt_proc_bpcreate(dpr, rdn.u.bptaddr, 3200Sstevel@tonic-gate (dt_bkpt_f *)dt_proc_rdevent, (void *)evname); 3210Sstevel@tonic-gate } 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate /* 3240Sstevel@tonic-gate * Common code for enabling events associated with the run-time linker after 3250Sstevel@tonic-gate * attaching to a process or after a victim process completes an exec(2). 3260Sstevel@tonic-gate */ 3270Sstevel@tonic-gate static void 3280Sstevel@tonic-gate dt_proc_attach(dt_proc_t *dpr, int exec) 3290Sstevel@tonic-gate { 3300Sstevel@tonic-gate const pstatus_t *psp = Pstatus(dpr->dpr_proc); 3310Sstevel@tonic-gate rd_err_e err; 3320Sstevel@tonic-gate GElf_Sym sym; 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate assert(DT_MUTEX_HELD(&dpr->dpr_lock)); 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate if (exec) { 3370Sstevel@tonic-gate if (psp->pr_lwp.pr_errno != 0) 3380Sstevel@tonic-gate return; /* exec failed: nothing needs to be done */ 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate dt_proc_bpdestroy(dpr, B_FALSE); 3410Sstevel@tonic-gate Preset_maps(dpr->dpr_proc); 3420Sstevel@tonic-gate } 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate if ((dpr->dpr_rtld = Prd_agent(dpr->dpr_proc)) != NULL && 3450Sstevel@tonic-gate (err = rd_event_enable(dpr->dpr_rtld, B_TRUE)) == RD_OK) { 3460Sstevel@tonic-gate dt_proc_rdwatch(dpr, RD_PREINIT, "RD_PREINIT"); 3470Sstevel@tonic-gate dt_proc_rdwatch(dpr, RD_POSTINIT, "RD_POSTINIT"); 3480Sstevel@tonic-gate dt_proc_rdwatch(dpr, RD_DLACTIVITY, "RD_DLACTIVITY"); 3490Sstevel@tonic-gate } else { 3500Sstevel@tonic-gate dt_dprintf("pid %d: failed to enable rtld events: %s\n", 3510Sstevel@tonic-gate (int)dpr->dpr_pid, dpr->dpr_rtld ? rd_errstr(err) : 3520Sstevel@tonic-gate "rtld_db agent initialization failed"); 3530Sstevel@tonic-gate } 3540Sstevel@tonic-gate 3550Sstevel@tonic-gate Pupdate_maps(dpr->dpr_proc); 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate if (Pxlookup_by_name(dpr->dpr_proc, LM_ID_BASE, 3580Sstevel@tonic-gate "a.out", "main", &sym, NULL) == 0) { 3590Sstevel@tonic-gate (void) dt_proc_bpcreate(dpr, (uintptr_t)sym.st_value, 3600Sstevel@tonic-gate (dt_bkpt_f *)dt_proc_bpmain, "a.out`main"); 3610Sstevel@tonic-gate } else { 3620Sstevel@tonic-gate dt_dprintf("pid %d: failed to find a.out`main: %s\n", 3630Sstevel@tonic-gate (int)dpr->dpr_pid, strerror(errno)); 3640Sstevel@tonic-gate } 3650Sstevel@tonic-gate } 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate /* 3680Sstevel@tonic-gate * Wait for a stopped process to be set running again by some other debugger. 3690Sstevel@tonic-gate * This is typically not required by /proc-based debuggers, since the usual 3700Sstevel@tonic-gate * model is that one debugger controls one victim. But DTrace, as usual, has 3710Sstevel@tonic-gate * its own needs: the stop() action assumes that prun(1) or some other tool 3720Sstevel@tonic-gate * will be applied to resume the victim process. This could be solved by 3730Sstevel@tonic-gate * adding a PCWRUN directive to /proc, but that seems like overkill unless 3740Sstevel@tonic-gate * other debuggers end up needing this functionality, so we implement a cheap 3750Sstevel@tonic-gate * equivalent to PCWRUN using the set of existing kernel mechanisms. 3760Sstevel@tonic-gate * 3770Sstevel@tonic-gate * Our intent is really not just to wait for the victim to run, but rather to 3780Sstevel@tonic-gate * wait for it to run and then stop again for a reason other than the current 3790Sstevel@tonic-gate * PR_REQUESTED stop. Since PCWSTOP/Pstopstatus() can be applied repeatedly 3800Sstevel@tonic-gate * to a stopped process and will return the same result without affecting the 3810Sstevel@tonic-gate * victim, we can just perform these operations repeatedly until Pstate() 3820Sstevel@tonic-gate * changes, the representative LWP ID changes, or the stop timestamp advances. 3830Sstevel@tonic-gate * dt_proc_control() will then rediscover the new state and continue as usual. 3840Sstevel@tonic-gate * When the process is still stopped in the same exact state, we sleep for a 3850Sstevel@tonic-gate * brief interval before waiting again so as not to spin consuming CPU cycles. 3860Sstevel@tonic-gate */ 3870Sstevel@tonic-gate static void 3880Sstevel@tonic-gate dt_proc_waitrun(dt_proc_t *dpr) 3890Sstevel@tonic-gate { 3900Sstevel@tonic-gate struct ps_prochandle *P = dpr->dpr_proc; 3910Sstevel@tonic-gate const lwpstatus_t *psp = &Pstatus(P)->pr_lwp; 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate int krflag = psp->pr_flags & (PR_KLC | PR_RLC); 3940Sstevel@tonic-gate timestruc_t tstamp = psp->pr_tstamp; 3950Sstevel@tonic-gate lwpid_t lwpid = psp->pr_lwpid; 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate const long wstop = PCWSTOP; 3980Sstevel@tonic-gate int pfd = Pctlfd(P); 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate assert(DT_MUTEX_HELD(&dpr->dpr_lock)); 4010Sstevel@tonic-gate assert(psp->pr_flags & PR_STOPPED); 4020Sstevel@tonic-gate assert(Pstate(P) == PS_STOP); 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate /* 4050Sstevel@tonic-gate * While we are waiting for the victim to run, clear PR_KLC and PR_RLC 4060Sstevel@tonic-gate * so that if the libdtrace client is killed, the victim stays stopped. 4070Sstevel@tonic-gate * dt_proc_destroy() will also observe this and perform PRELEASE_HANG. 4080Sstevel@tonic-gate */ 4090Sstevel@tonic-gate (void) Punsetflags(P, krflag); 4100Sstevel@tonic-gate Psync(P); 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate while (!dpr->dpr_quit) { 4150Sstevel@tonic-gate if (write(pfd, &wstop, sizeof (wstop)) == -1 && errno == EINTR) 4160Sstevel@tonic-gate continue; /* check dpr_quit and continue waiting */ 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate (void) pthread_mutex_lock(&dpr->dpr_lock); 4190Sstevel@tonic-gate (void) Pstopstatus(P, PCNULL, 0); 4200Sstevel@tonic-gate psp = &Pstatus(P)->pr_lwp; 4210Sstevel@tonic-gate 4220Sstevel@tonic-gate /* 4230Sstevel@tonic-gate * If we've reached a new state, found a new representative, or 4240Sstevel@tonic-gate * the stop timestamp has changed, restore PR_KLC/PR_RLC to its 4250Sstevel@tonic-gate * original setting and then return with dpr_lock held. 4260Sstevel@tonic-gate */ 4270Sstevel@tonic-gate if (Pstate(P) != PS_STOP || psp->pr_lwpid != lwpid || 4280Sstevel@tonic-gate bcmp(&psp->pr_tstamp, &tstamp, sizeof (tstamp)) != 0) { 4290Sstevel@tonic-gate (void) Psetflags(P, krflag); 4300Sstevel@tonic-gate Psync(P); 4310Sstevel@tonic-gate return; 4320Sstevel@tonic-gate } 4330Sstevel@tonic-gate 4340Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 4350Sstevel@tonic-gate (void) poll(NULL, 0, MILLISEC / 2); 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate (void) pthread_mutex_lock(&dpr->dpr_lock); 4390Sstevel@tonic-gate } 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate typedef struct dt_proc_control_data { 4420Sstevel@tonic-gate dtrace_hdl_t *dpcd_hdl; /* DTrace handle */ 4430Sstevel@tonic-gate dt_proc_t *dpcd_proc; /* proccess to control */ 4440Sstevel@tonic-gate } dt_proc_control_data_t; 4450Sstevel@tonic-gate 4460Sstevel@tonic-gate /* 4470Sstevel@tonic-gate * Main loop for all victim process control threads. We initialize all the 4480Sstevel@tonic-gate * appropriate /proc control mechanisms, and then enter a loop waiting for 4490Sstevel@tonic-gate * the process to stop on an event or die. We process any events by calling 4500Sstevel@tonic-gate * appropriate subroutines, and exit when the victim dies or we lose control. 4510Sstevel@tonic-gate * 4520Sstevel@tonic-gate * The control thread synchronizes the use of dpr_proc with other libdtrace 4530Sstevel@tonic-gate * threads using dpr_lock. We hold the lock for all of our operations except 4540Sstevel@tonic-gate * waiting while the process is running: this is accomplished by writing a 4550Sstevel@tonic-gate * PCWSTOP directive directly to the underlying /proc/<pid>/ctl file. If the 4560Sstevel@tonic-gate * libdtrace client wishes to exit or abort our wait, SIGCANCEL can be used. 4570Sstevel@tonic-gate */ 4580Sstevel@tonic-gate static void * 4590Sstevel@tonic-gate dt_proc_control(void *arg) 4600Sstevel@tonic-gate { 4610Sstevel@tonic-gate dt_proc_control_data_t *datap = arg; 4620Sstevel@tonic-gate dtrace_hdl_t *dtp = datap->dpcd_hdl; 4630Sstevel@tonic-gate dt_proc_t *dpr = datap->dpcd_proc; 4640Sstevel@tonic-gate dt_proc_hash_t *dph = dpr->dpr_hdl->dt_procs; 4650Sstevel@tonic-gate struct ps_prochandle *P = dpr->dpr_proc; 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate int pfd = Pctlfd(P); 4680Sstevel@tonic-gate int pid = dpr->dpr_pid; 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate const long wstop = PCWSTOP; 4710Sstevel@tonic-gate int notify = B_FALSE; 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate /* 4740Sstevel@tonic-gate * We disable the POSIX thread cancellation mechanism so that the 4750Sstevel@tonic-gate * client program using libdtrace can't accidentally cancel our thread. 4760Sstevel@tonic-gate * dt_proc_destroy() uses SIGCANCEL explicitly to simply poke us out 4770Sstevel@tonic-gate * of PCWSTOP with EINTR, at which point we will see dpr_quit and exit. 4780Sstevel@tonic-gate */ 4790Sstevel@tonic-gate (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); 4800Sstevel@tonic-gate 4810Sstevel@tonic-gate /* 4820Sstevel@tonic-gate * Set up the corresponding process for tracing by libdtrace. We want 4830Sstevel@tonic-gate * to be able to catch breakpoints and efficiently single-step over 4840Sstevel@tonic-gate * them, and we need to enable librtld_db to watch libdl activity. 4850Sstevel@tonic-gate */ 4860Sstevel@tonic-gate (void) pthread_mutex_lock(&dpr->dpr_lock); 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate (void) Punsetflags(P, PR_ASYNC); /* require synchronous mode */ 4890Sstevel@tonic-gate (void) Psetflags(P, PR_BPTADJ); /* always adjust eip on x86 */ 4900Sstevel@tonic-gate (void) Punsetflags(P, PR_FORK); /* do not inherit on fork */ 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate (void) Pfault(P, FLTBPT, B_TRUE); /* always trace breakpoints */ 4930Sstevel@tonic-gate (void) Pfault(P, FLTTRACE, B_TRUE); /* always trace single-step */ 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate /* 4960Sstevel@tonic-gate * We must trace exit from exec() system calls so that if the exec is 4970Sstevel@tonic-gate * successful, we can reset our breakpoints and re-initialize libproc. 4980Sstevel@tonic-gate */ 4990Sstevel@tonic-gate (void) Psysexit(P, SYS_exec, B_TRUE); 5000Sstevel@tonic-gate (void) Psysexit(P, SYS_execve, B_TRUE); 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate /* 5030Sstevel@tonic-gate * We must trace entry and exit for fork() system calls in order to 5040Sstevel@tonic-gate * disable our breakpoints temporarily during the fork. We do not set 5050Sstevel@tonic-gate * the PR_FORK flag, so if fork succeeds the child begins executing and 5060Sstevel@tonic-gate * does not inherit any other tracing behaviors or a control thread. 5070Sstevel@tonic-gate */ 5080Sstevel@tonic-gate (void) Psysentry(P, SYS_vfork, B_TRUE); 5090Sstevel@tonic-gate (void) Psysexit(P, SYS_vfork, B_TRUE); 5100Sstevel@tonic-gate (void) Psysentry(P, SYS_fork1, B_TRUE); 5110Sstevel@tonic-gate (void) Psysexit(P, SYS_fork1, B_TRUE); 5120Sstevel@tonic-gate (void) Psysentry(P, SYS_forkall, B_TRUE); 5130Sstevel@tonic-gate (void) Psysexit(P, SYS_forkall, B_TRUE); 5143235Sraf (void) Psysentry(P, SYS_forksys, B_TRUE); 5153235Sraf (void) Psysexit(P, SYS_forksys, B_TRUE); 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate Psync(P); /* enable all /proc changes */ 5180Sstevel@tonic-gate dt_proc_attach(dpr, B_FALSE); /* enable rtld breakpoints */ 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate /* 5210Sstevel@tonic-gate * If PR_KLC is set, we created the process; otherwise we grabbed it. 5220Sstevel@tonic-gate * Check for an appropriate stop request and wait for dt_proc_continue. 5230Sstevel@tonic-gate */ 5240Sstevel@tonic-gate if (Pstatus(P)->pr_flags & PR_KLC) 5250Sstevel@tonic-gate dt_proc_stop(dpr, DT_PROC_STOP_CREATE); 5260Sstevel@tonic-gate else 5270Sstevel@tonic-gate dt_proc_stop(dpr, DT_PROC_STOP_GRAB); 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate if (Psetrun(P, 0, 0) == -1) { 5300Sstevel@tonic-gate dt_dprintf("pid %d: failed to set running: %s\n", 5310Sstevel@tonic-gate (int)dpr->dpr_pid, strerror(errno)); 5320Sstevel@tonic-gate } 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate /* 5370Sstevel@tonic-gate * Wait for the process corresponding to this control thread to stop, 5380Sstevel@tonic-gate * process the event, and then set it running again. We want to sleep 5390Sstevel@tonic-gate * with dpr_lock *unheld* so that other parts of libdtrace can use the 5400Sstevel@tonic-gate * ps_prochandle in the meantime (e.g. ustack()). To do this, we write 5410Sstevel@tonic-gate * a PCWSTOP directive directly to the underlying /proc/<pid>/ctl file. 5420Sstevel@tonic-gate * Once the process stops, we wake up, grab dpr_lock, and then call 5430Sstevel@tonic-gate * Pwait() (which will return immediately) and do our processing. 5440Sstevel@tonic-gate */ 5450Sstevel@tonic-gate while (!dpr->dpr_quit) { 5460Sstevel@tonic-gate const lwpstatus_t *psp; 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate if (write(pfd, &wstop, sizeof (wstop)) == -1 && errno == EINTR) 5490Sstevel@tonic-gate continue; /* check dpr_quit and continue waiting */ 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate (void) pthread_mutex_lock(&dpr->dpr_lock); 5520Sstevel@tonic-gate pwait_locked: 5530Sstevel@tonic-gate if (Pstopstatus(P, PCNULL, 0) == -1 && errno == EINTR) { 5540Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 5550Sstevel@tonic-gate continue; /* check dpr_quit and continue waiting */ 5560Sstevel@tonic-gate } 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate switch (Pstate(P)) { 5590Sstevel@tonic-gate case PS_STOP: 5600Sstevel@tonic-gate psp = &Pstatus(P)->pr_lwp; 5610Sstevel@tonic-gate 5620Sstevel@tonic-gate dt_dprintf("pid %d: proc stopped showing %d/%d\n", 5630Sstevel@tonic-gate pid, psp->pr_why, psp->pr_what); 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate /* 5660Sstevel@tonic-gate * If the process stops showing PR_REQUESTED, then the 5670Sstevel@tonic-gate * DTrace stop() action was applied to it or another 5680Sstevel@tonic-gate * debugging utility (e.g. pstop(1)) asked it to stop. 5690Sstevel@tonic-gate * In either case, the user's intention is for the 5700Sstevel@tonic-gate * process to remain stopped until another external 5710Sstevel@tonic-gate * mechanism (e.g. prun(1)) is applied. So instead of 5720Sstevel@tonic-gate * setting the process running ourself, we wait for 5730Sstevel@tonic-gate * someone else to do so. Once that happens, we return 5740Sstevel@tonic-gate * to our normal loop waiting for an event of interest. 5750Sstevel@tonic-gate */ 5760Sstevel@tonic-gate if (psp->pr_why == PR_REQUESTED) { 5770Sstevel@tonic-gate dt_proc_waitrun(dpr); 5780Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 5790Sstevel@tonic-gate continue; 5800Sstevel@tonic-gate } 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate /* 5830Sstevel@tonic-gate * If the process stops showing one of the events that 5840Sstevel@tonic-gate * we are tracing, perform the appropriate response. 5850Sstevel@tonic-gate * Note that we ignore PR_SUSPENDED, PR_CHECKPOINT, and 5860Sstevel@tonic-gate * PR_JOBCONTROL by design: if one of these conditions 5870Sstevel@tonic-gate * occurs, we will fall through to Psetrun() but the 5880Sstevel@tonic-gate * process will remain stopped in the kernel by the 5890Sstevel@tonic-gate * corresponding mechanism (e.g. job control stop). 5900Sstevel@tonic-gate */ 5910Sstevel@tonic-gate if (psp->pr_why == PR_FAULTED && psp->pr_what == FLTBPT) 5920Sstevel@tonic-gate dt_proc_bpmatch(dtp, dpr); 5930Sstevel@tonic-gate else if (psp->pr_why == PR_SYSENTRY && 5940Sstevel@tonic-gate IS_SYS_FORK(psp->pr_what)) 5950Sstevel@tonic-gate dt_proc_bpdisable(dpr); 5960Sstevel@tonic-gate else if (psp->pr_why == PR_SYSEXIT && 5970Sstevel@tonic-gate IS_SYS_FORK(psp->pr_what)) 5980Sstevel@tonic-gate dt_proc_bpenable(dpr); 5990Sstevel@tonic-gate else if (psp->pr_why == PR_SYSEXIT && 6000Sstevel@tonic-gate IS_SYS_EXEC(psp->pr_what)) 6010Sstevel@tonic-gate dt_proc_attach(dpr, B_TRUE); 6020Sstevel@tonic-gate break; 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate case PS_LOST: 6050Sstevel@tonic-gate if (Preopen(P) == 0) 6060Sstevel@tonic-gate goto pwait_locked; 6070Sstevel@tonic-gate 6080Sstevel@tonic-gate dt_dprintf("pid %d: proc lost: %s\n", 6090Sstevel@tonic-gate pid, strerror(errno)); 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate dpr->dpr_quit = B_TRUE; 6120Sstevel@tonic-gate notify = B_TRUE; 6130Sstevel@tonic-gate break; 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate case PS_UNDEAD: 6160Sstevel@tonic-gate dt_dprintf("pid %d: proc died\n", pid); 6170Sstevel@tonic-gate dpr->dpr_quit = B_TRUE; 6180Sstevel@tonic-gate notify = B_TRUE; 6190Sstevel@tonic-gate break; 6200Sstevel@tonic-gate } 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate if (Pstate(P) != PS_UNDEAD && Psetrun(P, 0, 0) == -1) { 6230Sstevel@tonic-gate dt_dprintf("pid %d: failed to set running: %s\n", 6240Sstevel@tonic-gate (int)dpr->dpr_pid, strerror(errno)); 6250Sstevel@tonic-gate } 6260Sstevel@tonic-gate 6270Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 6280Sstevel@tonic-gate } 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate /* 6310Sstevel@tonic-gate * If the control thread detected PS_UNDEAD or PS_LOST, then enqueue 6320Sstevel@tonic-gate * the dt_proc_t structure on the dt_proc_hash_t notification list. 6330Sstevel@tonic-gate */ 6341399Sahl if (notify) 6351399Sahl dt_proc_notify(dtp, dph, dpr, NULL); 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate /* 6380Sstevel@tonic-gate * Destroy and remove any remaining breakpoints, set dpr_done and clear 6390Sstevel@tonic-gate * dpr_tid to indicate the control thread has exited, and notify any 6400Sstevel@tonic-gate * waiting thread in dt_proc_destroy() that we have succesfully exited. 6410Sstevel@tonic-gate */ 6420Sstevel@tonic-gate (void) pthread_mutex_lock(&dpr->dpr_lock); 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate dt_proc_bpdestroy(dpr, B_TRUE); 6450Sstevel@tonic-gate dpr->dpr_done = B_TRUE; 6460Sstevel@tonic-gate dpr->dpr_tid = 0; 6470Sstevel@tonic-gate 6481399Sahl (void) pthread_cond_broadcast(&dpr->dpr_cv); 6490Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 6500Sstevel@tonic-gate 6510Sstevel@tonic-gate return (NULL); 6520Sstevel@tonic-gate } 6530Sstevel@tonic-gate 6540Sstevel@tonic-gate /*PRINTFLIKE3*/ 6550Sstevel@tonic-gate static struct ps_prochandle * 6560Sstevel@tonic-gate dt_proc_error(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *format, ...) 6570Sstevel@tonic-gate { 6580Sstevel@tonic-gate va_list ap; 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate va_start(ap, format); 6610Sstevel@tonic-gate dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap); 6620Sstevel@tonic-gate va_end(ap); 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate if (dpr->dpr_proc != NULL) 6650Sstevel@tonic-gate Prelease(dpr->dpr_proc, 0); 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate dt_free(dtp, dpr); 6680Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_COMPILER); 6690Sstevel@tonic-gate return (NULL); 6700Sstevel@tonic-gate } 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate dt_proc_t * 6730Sstevel@tonic-gate dt_proc_lookup(dtrace_hdl_t *dtp, struct ps_prochandle *P, int remove) 6740Sstevel@tonic-gate { 6750Sstevel@tonic-gate dt_proc_hash_t *dph = dtp->dt_procs; 6760Sstevel@tonic-gate pid_t pid = Pstatus(P)->pr_pid; 6770Sstevel@tonic-gate dt_proc_t *dpr, **dpp = &dph->dph_hash[pid & (dph->dph_hashlen - 1)]; 6780Sstevel@tonic-gate 6790Sstevel@tonic-gate for (dpr = *dpp; dpr != NULL; dpr = dpr->dpr_hash) { 6800Sstevel@tonic-gate if (dpr->dpr_pid == pid) 6810Sstevel@tonic-gate break; 6820Sstevel@tonic-gate else 6830Sstevel@tonic-gate dpp = &dpr->dpr_hash; 6840Sstevel@tonic-gate } 6850Sstevel@tonic-gate 6860Sstevel@tonic-gate assert(dpr != NULL); 6870Sstevel@tonic-gate assert(dpr->dpr_proc == P); 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate if (remove) 6900Sstevel@tonic-gate *dpp = dpr->dpr_hash; /* remove from pid hash chain */ 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate return (dpr); 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate static void 6960Sstevel@tonic-gate dt_proc_destroy(dtrace_hdl_t *dtp, struct ps_prochandle *P) 6970Sstevel@tonic-gate { 698576Sbmc dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE); 6990Sstevel@tonic-gate dt_proc_hash_t *dph = dtp->dt_procs; 7001399Sahl dt_proc_notify_t *npr, **npp; 7010Sstevel@tonic-gate int rflag; 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate assert(dpr != NULL); 7040Sstevel@tonic-gate 7050Sstevel@tonic-gate /* 7060Sstevel@tonic-gate * If neither PR_KLC nor PR_RLC is set, then the process is stopped by 7070Sstevel@tonic-gate * an external debugger and we were waiting in dt_proc_waitrun(). 7080Sstevel@tonic-gate * Leave the process in this condition using PRELEASE_HANG. 7090Sstevel@tonic-gate */ 7100Sstevel@tonic-gate if (!(Pstatus(dpr->dpr_proc)->pr_flags & (PR_KLC | PR_RLC))) { 7110Sstevel@tonic-gate dt_dprintf("abandoning pid %d\n", (int)dpr->dpr_pid); 7120Sstevel@tonic-gate rflag = PRELEASE_HANG; 713*11237SJonathan.Haslam@Sun.COM } else if (Pstatus(dpr->dpr_proc)->pr_flags & PR_KLC) { 714*11237SJonathan.Haslam@Sun.COM dt_dprintf("killing pid %d\n", (int)dpr->dpr_pid); 715*11237SJonathan.Haslam@Sun.COM rflag = PRELEASE_KILL; /* apply kill-on-last-close */ 7160Sstevel@tonic-gate } else { 7170Sstevel@tonic-gate dt_dprintf("releasing pid %d\n", (int)dpr->dpr_pid); 718*11237SJonathan.Haslam@Sun.COM rflag = 0; /* apply run-on-last-close */ 7190Sstevel@tonic-gate } 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate if (dpr->dpr_tid) { 7220Sstevel@tonic-gate /* 7230Sstevel@tonic-gate * Set the dpr_quit flag to tell the daemon thread to exit. We 7240Sstevel@tonic-gate * send it a SIGCANCEL to poke it out of PCWSTOP or any other 7250Sstevel@tonic-gate * long-term /proc system call. Our daemon threads have POSIX 7260Sstevel@tonic-gate * cancellation disabled, so EINTR will be the only effect. We 7270Sstevel@tonic-gate * then wait for dpr_done to indicate the thread has exited. 7280Sstevel@tonic-gate * 7290Sstevel@tonic-gate * We can't use pthread_kill() to send SIGCANCEL because the 7300Sstevel@tonic-gate * interface forbids it and we can't use pthread_cancel() 7310Sstevel@tonic-gate * because with cancellation disabled it won't actually 7320Sstevel@tonic-gate * send SIGCANCEL to the target thread, so we use _lwp_kill() 7330Sstevel@tonic-gate * to do the job. This is all built on evil knowledge of 7340Sstevel@tonic-gate * the details of the cancellation mechanism in libc. 7350Sstevel@tonic-gate */ 7360Sstevel@tonic-gate (void) pthread_mutex_lock(&dpr->dpr_lock); 7370Sstevel@tonic-gate dpr->dpr_quit = B_TRUE; 7380Sstevel@tonic-gate (void) _lwp_kill(dpr->dpr_tid, SIGCANCEL); 7390Sstevel@tonic-gate 740265Smws /* 741265Smws * If the process is currently idling in dt_proc_stop(), re- 742265Smws * enable breakpoints and poke it into running again. 743265Smws */ 744265Smws if (dpr->dpr_stop & DT_PROC_STOP_IDLE) { 745265Smws dt_proc_bpenable(dpr); 746265Smws dpr->dpr_stop &= ~DT_PROC_STOP_IDLE; 747265Smws (void) pthread_cond_broadcast(&dpr->dpr_cv); 748265Smws } 749265Smws 7500Sstevel@tonic-gate while (!dpr->dpr_done) 7510Sstevel@tonic-gate (void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock); 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 7540Sstevel@tonic-gate } 7550Sstevel@tonic-gate 7560Sstevel@tonic-gate /* 757576Sbmc * Before we free the process structure, remove this dt_proc_t from the 758576Sbmc * lookup hash, and then walk the dt_proc_hash_t's notification list 759576Sbmc * and remove this dt_proc_t if it is enqueued. 7600Sstevel@tonic-gate */ 7610Sstevel@tonic-gate (void) pthread_mutex_lock(&dph->dph_lock); 762576Sbmc (void) dt_proc_lookup(dtp, P, B_TRUE); 7630Sstevel@tonic-gate npp = &dph->dph_notify; 7640Sstevel@tonic-gate 7651399Sahl while ((npr = *npp) != NULL) { 7661399Sahl if (npr->dprn_dpr == dpr) { 7671399Sahl *npp = npr->dprn_next; 7681399Sahl dt_free(dtp, npr); 7691399Sahl } else { 7701399Sahl npp = &npr->dprn_next; 7711399Sahl } 7720Sstevel@tonic-gate } 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate (void) pthread_mutex_unlock(&dph->dph_lock); 7750Sstevel@tonic-gate 7760Sstevel@tonic-gate /* 7770Sstevel@tonic-gate * Remove the dt_proc_list from the LRU list, release the underlying 7780Sstevel@tonic-gate * libproc handle, and free our dt_proc_t data structure. 7790Sstevel@tonic-gate */ 7800Sstevel@tonic-gate if (dpr->dpr_cacheable) { 7810Sstevel@tonic-gate assert(dph->dph_lrucnt != 0); 7820Sstevel@tonic-gate dph->dph_lrucnt--; 7830Sstevel@tonic-gate } 7840Sstevel@tonic-gate 7850Sstevel@tonic-gate dt_list_delete(&dph->dph_lrulist, dpr); 7860Sstevel@tonic-gate Prelease(dpr->dpr_proc, rflag); 7870Sstevel@tonic-gate dt_free(dtp, dpr); 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate static int 7910Sstevel@tonic-gate dt_proc_create_thread(dtrace_hdl_t *dtp, dt_proc_t *dpr, uint_t stop) 7920Sstevel@tonic-gate { 7930Sstevel@tonic-gate dt_proc_control_data_t data; 7940Sstevel@tonic-gate sigset_t nset, oset; 7950Sstevel@tonic-gate pthread_attr_t a; 7960Sstevel@tonic-gate int err; 7970Sstevel@tonic-gate 7980Sstevel@tonic-gate (void) pthread_mutex_lock(&dpr->dpr_lock); 7990Sstevel@tonic-gate dpr->dpr_stop |= stop; /* set bit for initial rendezvous */ 8000Sstevel@tonic-gate 8010Sstevel@tonic-gate (void) pthread_attr_init(&a); 8020Sstevel@tonic-gate (void) pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED); 8030Sstevel@tonic-gate 8040Sstevel@tonic-gate (void) sigfillset(&nset); 8050Sstevel@tonic-gate (void) sigdelset(&nset, SIGABRT); /* unblocked for assert() */ 8060Sstevel@tonic-gate (void) sigdelset(&nset, SIGCANCEL); /* see dt_proc_destroy() */ 8070Sstevel@tonic-gate 8080Sstevel@tonic-gate data.dpcd_hdl = dtp; 8090Sstevel@tonic-gate data.dpcd_proc = dpr; 8100Sstevel@tonic-gate 8110Sstevel@tonic-gate (void) pthread_sigmask(SIG_SETMASK, &nset, &oset); 8120Sstevel@tonic-gate err = pthread_create(&dpr->dpr_tid, &a, dt_proc_control, &data); 8130Sstevel@tonic-gate (void) pthread_sigmask(SIG_SETMASK, &oset, NULL); 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate /* 8160Sstevel@tonic-gate * If the control thread was created, then wait on dpr_cv for either 8170Sstevel@tonic-gate * dpr_done to be set (the victim died or the control thread failed) 8180Sstevel@tonic-gate * or DT_PROC_STOP_IDLE to be set, indicating that the victim is now 8190Sstevel@tonic-gate * stopped by /proc and the control thread is at the rendezvous event. 8200Sstevel@tonic-gate * On success, we return with the process and control thread stopped: 8210Sstevel@tonic-gate * the caller can then apply dt_proc_continue() to resume both. 8220Sstevel@tonic-gate */ 8230Sstevel@tonic-gate if (err == 0) { 8240Sstevel@tonic-gate while (!dpr->dpr_done && !(dpr->dpr_stop & DT_PROC_STOP_IDLE)) 8250Sstevel@tonic-gate (void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock); 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate /* 8280Sstevel@tonic-gate * If dpr_done is set, the control thread aborted before it 8290Sstevel@tonic-gate * reached the rendezvous event. This is either due to PS_LOST 8300Sstevel@tonic-gate * or PS_UNDEAD (i.e. the process died). We try to provide a 8310Sstevel@tonic-gate * small amount of useful information to help figure it out. 8320Sstevel@tonic-gate */ 8330Sstevel@tonic-gate if (dpr->dpr_done) { 8340Sstevel@tonic-gate const psinfo_t *prp = Ppsinfo(dpr->dpr_proc); 8350Sstevel@tonic-gate int stat = prp ? prp->pr_wstat : 0; 8360Sstevel@tonic-gate int pid = dpr->dpr_pid; 8370Sstevel@tonic-gate 8380Sstevel@tonic-gate if (Pstate(dpr->dpr_proc) == PS_LOST) { 8390Sstevel@tonic-gate (void) dt_proc_error(dpr->dpr_hdl, dpr, 8400Sstevel@tonic-gate "failed to control pid %d: process exec'd " 8410Sstevel@tonic-gate "set-id or unobservable program\n", pid); 8420Sstevel@tonic-gate } else if (WIFSIGNALED(stat)) { 8430Sstevel@tonic-gate (void) dt_proc_error(dpr->dpr_hdl, dpr, 8440Sstevel@tonic-gate "failed to control pid %d: process died " 8450Sstevel@tonic-gate "from signal %d\n", pid, WTERMSIG(stat)); 8460Sstevel@tonic-gate } else { 8470Sstevel@tonic-gate (void) dt_proc_error(dpr->dpr_hdl, dpr, 8480Sstevel@tonic-gate "failed to control pid %d: process exited " 8490Sstevel@tonic-gate "with status %d\n", pid, WEXITSTATUS(stat)); 8500Sstevel@tonic-gate } 8510Sstevel@tonic-gate 8520Sstevel@tonic-gate err = ESRCH; /* cause grab() or create() to fail */ 8530Sstevel@tonic-gate } 8540Sstevel@tonic-gate } else { 8550Sstevel@tonic-gate (void) dt_proc_error(dpr->dpr_hdl, dpr, 8560Sstevel@tonic-gate "failed to create control thread for process-id %d: %s\n", 8570Sstevel@tonic-gate (int)dpr->dpr_pid, strerror(err)); 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate 8600Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 8610Sstevel@tonic-gate (void) pthread_attr_destroy(&a); 8620Sstevel@tonic-gate 8630Sstevel@tonic-gate return (err); 8640Sstevel@tonic-gate } 8650Sstevel@tonic-gate 8660Sstevel@tonic-gate struct ps_prochandle * 8670Sstevel@tonic-gate dt_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv) 8680Sstevel@tonic-gate { 8690Sstevel@tonic-gate dt_proc_hash_t *dph = dtp->dt_procs; 8700Sstevel@tonic-gate dt_proc_t *dpr; 8710Sstevel@tonic-gate int err; 8720Sstevel@tonic-gate 8730Sstevel@tonic-gate if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL) 8740Sstevel@tonic-gate return (NULL); /* errno is set for us */ 8750Sstevel@tonic-gate 8760Sstevel@tonic-gate (void) pthread_mutex_init(&dpr->dpr_lock, NULL); 8770Sstevel@tonic-gate (void) pthread_cond_init(&dpr->dpr_cv, NULL); 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate if ((dpr->dpr_proc = Pcreate(file, argv, &err, NULL, 0)) == NULL) { 8800Sstevel@tonic-gate return (dt_proc_error(dtp, dpr, 8810Sstevel@tonic-gate "failed to execute %s: %s\n", file, Pcreate_error(err))); 8820Sstevel@tonic-gate } 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate dpr->dpr_hdl = dtp; 8850Sstevel@tonic-gate dpr->dpr_pid = Pstatus(dpr->dpr_proc)->pr_pid; 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate (void) Punsetflags(dpr->dpr_proc, PR_RLC); 8880Sstevel@tonic-gate (void) Psetflags(dpr->dpr_proc, PR_KLC); 8890Sstevel@tonic-gate 8900Sstevel@tonic-gate if (dt_proc_create_thread(dtp, dpr, dtp->dt_prcmode) != 0) 8910Sstevel@tonic-gate return (NULL); /* dt_proc_error() has been called for us */ 8920Sstevel@tonic-gate 8930Sstevel@tonic-gate dpr->dpr_hash = dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)]; 8940Sstevel@tonic-gate dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)] = dpr; 8950Sstevel@tonic-gate dt_list_prepend(&dph->dph_lrulist, dpr); 8960Sstevel@tonic-gate 8970Sstevel@tonic-gate dt_dprintf("created pid %d\n", (int)dpr->dpr_pid); 8980Sstevel@tonic-gate dpr->dpr_refs++; 8990Sstevel@tonic-gate 9000Sstevel@tonic-gate return (dpr->dpr_proc); 9010Sstevel@tonic-gate } 9020Sstevel@tonic-gate 9030Sstevel@tonic-gate struct ps_prochandle * 9040Sstevel@tonic-gate dt_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags, int nomonitor) 9050Sstevel@tonic-gate { 9060Sstevel@tonic-gate dt_proc_hash_t *dph = dtp->dt_procs; 9070Sstevel@tonic-gate uint_t h = pid & (dph->dph_hashlen - 1); 9080Sstevel@tonic-gate dt_proc_t *dpr, *opr; 9090Sstevel@tonic-gate int err; 9100Sstevel@tonic-gate 9110Sstevel@tonic-gate /* 9120Sstevel@tonic-gate * Search the hash table for the pid. If it is already grabbed or 9130Sstevel@tonic-gate * created, move the handle to the front of the lrulist, increment 9140Sstevel@tonic-gate * the reference count, and return the existing ps_prochandle. 9150Sstevel@tonic-gate */ 9160Sstevel@tonic-gate for (dpr = dph->dph_hash[h]; dpr != NULL; dpr = dpr->dpr_hash) { 9170Sstevel@tonic-gate if (dpr->dpr_pid == pid && !dpr->dpr_stale) { 9180Sstevel@tonic-gate /* 9190Sstevel@tonic-gate * If the cached handle was opened read-only and 9200Sstevel@tonic-gate * this request is for a writeable handle, mark 9210Sstevel@tonic-gate * the cached handle as stale and open a new handle. 9220Sstevel@tonic-gate * Since it's stale, unmark it as cacheable. 9230Sstevel@tonic-gate */ 9240Sstevel@tonic-gate if (dpr->dpr_rdonly && !(flags & PGRAB_RDONLY)) { 9250Sstevel@tonic-gate dt_dprintf("upgrading pid %d\n", (int)pid); 9260Sstevel@tonic-gate dpr->dpr_stale = B_TRUE; 9270Sstevel@tonic-gate dpr->dpr_cacheable = B_FALSE; 9280Sstevel@tonic-gate dph->dph_lrucnt--; 9290Sstevel@tonic-gate break; 9300Sstevel@tonic-gate } 9310Sstevel@tonic-gate 9320Sstevel@tonic-gate dt_dprintf("grabbed pid %d (cached)\n", (int)pid); 9330Sstevel@tonic-gate dt_list_delete(&dph->dph_lrulist, dpr); 9340Sstevel@tonic-gate dt_list_prepend(&dph->dph_lrulist, dpr); 9350Sstevel@tonic-gate dpr->dpr_refs++; 9360Sstevel@tonic-gate return (dpr->dpr_proc); 9370Sstevel@tonic-gate } 9380Sstevel@tonic-gate } 9390Sstevel@tonic-gate 9400Sstevel@tonic-gate if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL) 9410Sstevel@tonic-gate return (NULL); /* errno is set for us */ 9420Sstevel@tonic-gate 9430Sstevel@tonic-gate (void) pthread_mutex_init(&dpr->dpr_lock, NULL); 9440Sstevel@tonic-gate (void) pthread_cond_init(&dpr->dpr_cv, NULL); 9450Sstevel@tonic-gate 9460Sstevel@tonic-gate if ((dpr->dpr_proc = Pgrab(pid, flags, &err)) == NULL) { 9470Sstevel@tonic-gate return (dt_proc_error(dtp, dpr, 9480Sstevel@tonic-gate "failed to grab pid %d: %s\n", (int)pid, Pgrab_error(err))); 9490Sstevel@tonic-gate } 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate dpr->dpr_hdl = dtp; 9520Sstevel@tonic-gate dpr->dpr_pid = pid; 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate (void) Punsetflags(dpr->dpr_proc, PR_KLC); 9550Sstevel@tonic-gate (void) Psetflags(dpr->dpr_proc, PR_RLC); 9560Sstevel@tonic-gate 9570Sstevel@tonic-gate /* 9580Sstevel@tonic-gate * If we are attempting to grab the process without a monitor 9590Sstevel@tonic-gate * thread, then mark the process cacheable only if it's being 9600Sstevel@tonic-gate * grabbed read-only. If we're currently caching more process 9610Sstevel@tonic-gate * handles than dph_lrulim permits, attempt to find the 9620Sstevel@tonic-gate * least-recently-used handle that is currently unreferenced and 9630Sstevel@tonic-gate * release it from the cache. Otherwise we are grabbing the process 9640Sstevel@tonic-gate * for control: create a control thread for this process and store 9650Sstevel@tonic-gate * its ID in dpr->dpr_tid. 9660Sstevel@tonic-gate */ 9670Sstevel@tonic-gate if (nomonitor || (flags & PGRAB_RDONLY)) { 9680Sstevel@tonic-gate if (dph->dph_lrucnt >= dph->dph_lrulim) { 9690Sstevel@tonic-gate for (opr = dt_list_prev(&dph->dph_lrulist); 9700Sstevel@tonic-gate opr != NULL; opr = dt_list_prev(opr)) { 9710Sstevel@tonic-gate if (opr->dpr_cacheable && opr->dpr_refs == 0) { 9720Sstevel@tonic-gate dt_proc_destroy(dtp, opr->dpr_proc); 9730Sstevel@tonic-gate break; 9740Sstevel@tonic-gate } 9750Sstevel@tonic-gate } 9760Sstevel@tonic-gate } 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate if (flags & PGRAB_RDONLY) { 9790Sstevel@tonic-gate dpr->dpr_cacheable = B_TRUE; 9800Sstevel@tonic-gate dpr->dpr_rdonly = B_TRUE; 9810Sstevel@tonic-gate dph->dph_lrucnt++; 9820Sstevel@tonic-gate } 9830Sstevel@tonic-gate 9840Sstevel@tonic-gate } else if (dt_proc_create_thread(dtp, dpr, DT_PROC_STOP_GRAB) != 0) 9850Sstevel@tonic-gate return (NULL); /* dt_proc_error() has been called for us */ 9860Sstevel@tonic-gate 9870Sstevel@tonic-gate dpr->dpr_hash = dph->dph_hash[h]; 9880Sstevel@tonic-gate dph->dph_hash[h] = dpr; 9890Sstevel@tonic-gate dt_list_prepend(&dph->dph_lrulist, dpr); 9900Sstevel@tonic-gate 9910Sstevel@tonic-gate dt_dprintf("grabbed pid %d\n", (int)pid); 9920Sstevel@tonic-gate dpr->dpr_refs++; 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate return (dpr->dpr_proc); 9950Sstevel@tonic-gate } 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate void 9980Sstevel@tonic-gate dt_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P) 9990Sstevel@tonic-gate { 10000Sstevel@tonic-gate dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE); 10010Sstevel@tonic-gate dt_proc_hash_t *dph = dtp->dt_procs; 10020Sstevel@tonic-gate 10030Sstevel@tonic-gate assert(dpr != NULL); 10040Sstevel@tonic-gate assert(dpr->dpr_refs != 0); 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate if (--dpr->dpr_refs == 0 && 10070Sstevel@tonic-gate (!dpr->dpr_cacheable || dph->dph_lrucnt > dph->dph_lrulim)) 10080Sstevel@tonic-gate dt_proc_destroy(dtp, P); 10090Sstevel@tonic-gate } 10100Sstevel@tonic-gate 10110Sstevel@tonic-gate void 10120Sstevel@tonic-gate dt_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P) 10130Sstevel@tonic-gate { 10140Sstevel@tonic-gate dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE); 10150Sstevel@tonic-gate 10160Sstevel@tonic-gate (void) pthread_mutex_lock(&dpr->dpr_lock); 10170Sstevel@tonic-gate 10180Sstevel@tonic-gate if (dpr->dpr_stop & DT_PROC_STOP_IDLE) { 10190Sstevel@tonic-gate dpr->dpr_stop &= ~DT_PROC_STOP_IDLE; 10200Sstevel@tonic-gate (void) pthread_cond_broadcast(&dpr->dpr_cv); 10210Sstevel@tonic-gate } 10220Sstevel@tonic-gate 10230Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 10240Sstevel@tonic-gate } 10250Sstevel@tonic-gate 10260Sstevel@tonic-gate void 10270Sstevel@tonic-gate dt_proc_lock(dtrace_hdl_t *dtp, struct ps_prochandle *P) 10280Sstevel@tonic-gate { 10290Sstevel@tonic-gate dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE); 10300Sstevel@tonic-gate int err = pthread_mutex_lock(&dpr->dpr_lock); 10310Sstevel@tonic-gate assert(err == 0); /* check for recursion */ 10320Sstevel@tonic-gate } 10330Sstevel@tonic-gate 10340Sstevel@tonic-gate void 10350Sstevel@tonic-gate dt_proc_unlock(dtrace_hdl_t *dtp, struct ps_prochandle *P) 10360Sstevel@tonic-gate { 10370Sstevel@tonic-gate dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE); 10380Sstevel@tonic-gate int err = pthread_mutex_unlock(&dpr->dpr_lock); 10390Sstevel@tonic-gate assert(err == 0); /* check for unheld lock */ 10400Sstevel@tonic-gate } 10410Sstevel@tonic-gate 10420Sstevel@tonic-gate void 10430Sstevel@tonic-gate dt_proc_hash_create(dtrace_hdl_t *dtp) 10440Sstevel@tonic-gate { 10450Sstevel@tonic-gate if ((dtp->dt_procs = dt_zalloc(dtp, sizeof (dt_proc_hash_t) + 10460Sstevel@tonic-gate sizeof (dt_proc_t *) * _dtrace_pidbuckets - 1)) != NULL) { 10470Sstevel@tonic-gate 10480Sstevel@tonic-gate (void) pthread_mutex_init(&dtp->dt_procs->dph_lock, NULL); 10490Sstevel@tonic-gate (void) pthread_cond_init(&dtp->dt_procs->dph_cv, NULL); 10500Sstevel@tonic-gate 10510Sstevel@tonic-gate dtp->dt_procs->dph_hashlen = _dtrace_pidbuckets; 10520Sstevel@tonic-gate dtp->dt_procs->dph_lrulim = _dtrace_pidlrulim; 10530Sstevel@tonic-gate } 10540Sstevel@tonic-gate } 10550Sstevel@tonic-gate 10560Sstevel@tonic-gate void 10570Sstevel@tonic-gate dt_proc_hash_destroy(dtrace_hdl_t *dtp) 10580Sstevel@tonic-gate { 10590Sstevel@tonic-gate dt_proc_hash_t *dph = dtp->dt_procs; 10600Sstevel@tonic-gate dt_proc_t *dpr; 10610Sstevel@tonic-gate 10620Sstevel@tonic-gate while ((dpr = dt_list_next(&dph->dph_lrulist)) != NULL) 10630Sstevel@tonic-gate dt_proc_destroy(dtp, dpr->dpr_proc); 10640Sstevel@tonic-gate 10650Sstevel@tonic-gate dtp->dt_procs = NULL; 10660Sstevel@tonic-gate dt_free(dtp, dph); 10670Sstevel@tonic-gate } 10680Sstevel@tonic-gate 10690Sstevel@tonic-gate struct ps_prochandle * 10700Sstevel@tonic-gate dtrace_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv) 10710Sstevel@tonic-gate { 10720Sstevel@tonic-gate dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target"); 10730Sstevel@tonic-gate struct ps_prochandle *P = dt_proc_create(dtp, file, argv); 10740Sstevel@tonic-gate 10750Sstevel@tonic-gate if (P != NULL && idp != NULL && idp->di_id == 0) 10760Sstevel@tonic-gate idp->di_id = Pstatus(P)->pr_pid; /* $target = created pid */ 10770Sstevel@tonic-gate 10780Sstevel@tonic-gate return (P); 10790Sstevel@tonic-gate } 10800Sstevel@tonic-gate 10810Sstevel@tonic-gate struct ps_prochandle * 10820Sstevel@tonic-gate dtrace_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags) 10830Sstevel@tonic-gate { 10840Sstevel@tonic-gate dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target"); 10850Sstevel@tonic-gate struct ps_prochandle *P = dt_proc_grab(dtp, pid, flags, 0); 10860Sstevel@tonic-gate 10870Sstevel@tonic-gate if (P != NULL && idp != NULL && idp->di_id == 0) 10880Sstevel@tonic-gate idp->di_id = pid; /* $target = grabbed pid */ 10890Sstevel@tonic-gate 10900Sstevel@tonic-gate return (P); 10910Sstevel@tonic-gate } 10920Sstevel@tonic-gate 10930Sstevel@tonic-gate void 10940Sstevel@tonic-gate dtrace_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P) 10950Sstevel@tonic-gate { 10960Sstevel@tonic-gate dt_proc_release(dtp, P); 10970Sstevel@tonic-gate } 10980Sstevel@tonic-gate 10990Sstevel@tonic-gate void 11000Sstevel@tonic-gate dtrace_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P) 11010Sstevel@tonic-gate { 11020Sstevel@tonic-gate dt_proc_continue(dtp, P); 11030Sstevel@tonic-gate } 1104