17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5657b1f3dSraf * Common Development and Distribution License (the "License"). 6657b1f3dSraf * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21900524f3Sahl 227c478bd9Sstevel@tonic-gate /* 2353f3aea0SRoger A. Faulkner * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* 287c478bd9Sstevel@tonic-gate * DTrace Process Control 297c478bd9Sstevel@tonic-gate * 307c478bd9Sstevel@tonic-gate * This file provides a set of routines that permit libdtrace and its clients 317c478bd9Sstevel@tonic-gate * to create and grab process handles using libproc, and to share these handles 327c478bd9Sstevel@tonic-gate * between library mechanisms that need libproc access, such as ustack(), and 337c478bd9Sstevel@tonic-gate * client mechanisms that need libproc access, such as dtrace(1M) -c and -p. 347c478bd9Sstevel@tonic-gate * The library provides several mechanisms in the libproc control layer: 357c478bd9Sstevel@tonic-gate * 367c478bd9Sstevel@tonic-gate * Reference Counting: The library code and client code can independently grab 377c478bd9Sstevel@tonic-gate * the same process handles without interfering with one another. Only when 387c478bd9Sstevel@tonic-gate * the reference count drops to zero and the handle is not being cached (see 397c478bd9Sstevel@tonic-gate * below for more information on caching) will Prelease() be called on it. 407c478bd9Sstevel@tonic-gate * 417c478bd9Sstevel@tonic-gate * Handle Caching: If a handle is grabbed PGRAB_RDONLY (e.g. by ustack()) and 427c478bd9Sstevel@tonic-gate * the reference count drops to zero, the handle is not immediately released. 437c478bd9Sstevel@tonic-gate * Instead, libproc handles are maintained on dph_lrulist in order from most- 447c478bd9Sstevel@tonic-gate * recently accessed to least-recently accessed. Idle handles are maintained 457c478bd9Sstevel@tonic-gate * until a pre-defined LRU cache limit is exceeded, permitting repeated calls 467c478bd9Sstevel@tonic-gate * to ustack() to avoid the overhead of releasing and re-grabbing processes. 477c478bd9Sstevel@tonic-gate * 487c478bd9Sstevel@tonic-gate * Process Control: For processes that are grabbed for control (~PGRAB_RDONLY) 497c478bd9Sstevel@tonic-gate * or created by dt_proc_create(), a control thread is created to provide 507c478bd9Sstevel@tonic-gate * callbacks on process exit and symbol table caching on dlopen()s. 517c478bd9Sstevel@tonic-gate * 527c478bd9Sstevel@tonic-gate * MT-Safety: Libproc is not MT-Safe, so dt_proc_lock() and dt_proc_unlock() 537c478bd9Sstevel@tonic-gate * are provided to synchronize access to the libproc handle between libdtrace 547c478bd9Sstevel@tonic-gate * code and client code and the control thread's use of the ps_prochandle. 557c478bd9Sstevel@tonic-gate * 567c478bd9Sstevel@tonic-gate * NOTE: MT-Safety is NOT provided for libdtrace itself, or for use of the 577c478bd9Sstevel@tonic-gate * dtrace_proc_grab/dtrace_proc_create mechanisms. Like all exported libdtrace 587c478bd9Sstevel@tonic-gate * calls, these are assumed to be MT-Unsafe. MT-Safety is ONLY provided for 597c478bd9Sstevel@tonic-gate * synchronization between libdtrace control threads and the client thread. 607c478bd9Sstevel@tonic-gate * 617c478bd9Sstevel@tonic-gate * The ps_prochandles themselves are maintained along with a dt_proc_t struct 627c478bd9Sstevel@tonic-gate * in a hash table indexed by PID. This provides basic locking and reference 637c478bd9Sstevel@tonic-gate * counting. The dt_proc_t is also maintained in LRU order on dph_lrulist. 647c478bd9Sstevel@tonic-gate * The dph_lrucnt and dph_lrulim count the number of cacheable processes and 657c478bd9Sstevel@tonic-gate * the current limit on the number of actively cached entries. 667c478bd9Sstevel@tonic-gate * 677c478bd9Sstevel@tonic-gate * The control thread for a process establishes breakpoints at the rtld_db 687c478bd9Sstevel@tonic-gate * locations of interest, updates mappings and symbol tables at these points, 697c478bd9Sstevel@tonic-gate * and handles exec and fork (by always following the parent). The control 707c478bd9Sstevel@tonic-gate * thread automatically exits when the process dies or control is lost. 717c478bd9Sstevel@tonic-gate * 727c478bd9Sstevel@tonic-gate * A simple notification mechanism is provided for libdtrace clients using 737c478bd9Sstevel@tonic-gate * dtrace_handle_proc() for notification of PS_UNDEAD or PS_LOST events. If 747c478bd9Sstevel@tonic-gate * such an event occurs, the dt_proc_t itself is enqueued on a notification 757c478bd9Sstevel@tonic-gate * list and the control thread broadcasts to dph_cv. dtrace_sleep() will wake 767c478bd9Sstevel@tonic-gate * up using this condition and will then call the client handler as necessary. 777c478bd9Sstevel@tonic-gate */ 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate #include <sys/wait.h> 807c478bd9Sstevel@tonic-gate #include <sys/lwp.h> 817c478bd9Sstevel@tonic-gate #include <strings.h> 827c478bd9Sstevel@tonic-gate #include <signal.h> 837c478bd9Sstevel@tonic-gate #include <assert.h> 847c478bd9Sstevel@tonic-gate #include <errno.h> 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate #include <dt_proc.h> 877c478bd9Sstevel@tonic-gate #include <dt_pid.h> 887c478bd9Sstevel@tonic-gate #include <dt_impl.h> 897c478bd9Sstevel@tonic-gate 90*8fd04b83SRoger A. Faulkner #define IS_SYS_EXEC(w) (w == SYS_execve) 91*8fd04b83SRoger A. Faulkner #define IS_SYS_FORK(w) (w == SYS_vfork || w == SYS_forksys) 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate static dt_bkpt_t * 947c478bd9Sstevel@tonic-gate dt_proc_bpcreate(dt_proc_t *dpr, uintptr_t addr, dt_bkpt_f *func, void *data) 957c478bd9Sstevel@tonic-gate { 967c478bd9Sstevel@tonic-gate struct ps_prochandle *P = dpr->dpr_proc; 977c478bd9Sstevel@tonic-gate dt_bkpt_t *dbp; 987c478bd9Sstevel@tonic-gate 9953f3aea0SRoger A. Faulkner assert(MUTEX_HELD(&dpr->dpr_lock)); 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate if ((dbp = dt_zalloc(dpr->dpr_hdl, sizeof (dt_bkpt_t))) != NULL) { 1027c478bd9Sstevel@tonic-gate dbp->dbp_func = func; 1037c478bd9Sstevel@tonic-gate dbp->dbp_data = data; 1047c478bd9Sstevel@tonic-gate dbp->dbp_addr = addr; 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate if (Psetbkpt(P, dbp->dbp_addr, &dbp->dbp_instr) == 0) 1077c478bd9Sstevel@tonic-gate dbp->dbp_active = B_TRUE; 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate dt_list_append(&dpr->dpr_bps, dbp); 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate return (dbp); 1137c478bd9Sstevel@tonic-gate } 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate static void 1167c478bd9Sstevel@tonic-gate dt_proc_bpdestroy(dt_proc_t *dpr, int delbkpts) 1177c478bd9Sstevel@tonic-gate { 1187c478bd9Sstevel@tonic-gate int state = Pstate(dpr->dpr_proc); 1197c478bd9Sstevel@tonic-gate dt_bkpt_t *dbp, *nbp; 1207c478bd9Sstevel@tonic-gate 12153f3aea0SRoger A. Faulkner assert(MUTEX_HELD(&dpr->dpr_lock)); 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate for (dbp = dt_list_next(&dpr->dpr_bps); dbp != NULL; dbp = nbp) { 1247c478bd9Sstevel@tonic-gate if (delbkpts && dbp->dbp_active && 1257c478bd9Sstevel@tonic-gate state != PS_LOST && state != PS_UNDEAD) { 1267c478bd9Sstevel@tonic-gate (void) Pdelbkpt(dpr->dpr_proc, 1277c478bd9Sstevel@tonic-gate dbp->dbp_addr, dbp->dbp_instr); 1287c478bd9Sstevel@tonic-gate } 1297c478bd9Sstevel@tonic-gate nbp = dt_list_next(dbp); 1307c478bd9Sstevel@tonic-gate dt_list_delete(&dpr->dpr_bps, dbp); 1317c478bd9Sstevel@tonic-gate dt_free(dpr->dpr_hdl, dbp); 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate } 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate static void 1367c478bd9Sstevel@tonic-gate dt_proc_bpmatch(dtrace_hdl_t *dtp, dt_proc_t *dpr) 1377c478bd9Sstevel@tonic-gate { 1387c478bd9Sstevel@tonic-gate const lwpstatus_t *psp = &Pstatus(dpr->dpr_proc)->pr_lwp; 1397c478bd9Sstevel@tonic-gate dt_bkpt_t *dbp; 1407c478bd9Sstevel@tonic-gate 14153f3aea0SRoger A. Faulkner assert(MUTEX_HELD(&dpr->dpr_lock)); 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate for (dbp = dt_list_next(&dpr->dpr_bps); 1447c478bd9Sstevel@tonic-gate dbp != NULL; dbp = dt_list_next(dbp)) { 1457c478bd9Sstevel@tonic-gate if (psp->pr_reg[R_PC] == dbp->dbp_addr) 1467c478bd9Sstevel@tonic-gate break; 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate if (dbp == NULL) { 1507c478bd9Sstevel@tonic-gate dt_dprintf("pid %d: spurious breakpoint wakeup for %lx\n", 1517c478bd9Sstevel@tonic-gate (int)dpr->dpr_pid, (ulong_t)psp->pr_reg[R_PC]); 1527c478bd9Sstevel@tonic-gate return; 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate dt_dprintf("pid %d: hit breakpoint at %lx (%lu)\n", 1567c478bd9Sstevel@tonic-gate (int)dpr->dpr_pid, (ulong_t)dbp->dbp_addr, ++dbp->dbp_hits); 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate dbp->dbp_func(dtp, dpr, dbp->dbp_data); 1597c478bd9Sstevel@tonic-gate (void) Pxecbkpt(dpr->dpr_proc, dbp->dbp_instr); 1607c478bd9Sstevel@tonic-gate } 1617c478bd9Sstevel@tonic-gate 162dcafa303Sahl static void 1637c478bd9Sstevel@tonic-gate dt_proc_bpenable(dt_proc_t *dpr) 1647c478bd9Sstevel@tonic-gate { 1657c478bd9Sstevel@tonic-gate dt_bkpt_t *dbp; 1667c478bd9Sstevel@tonic-gate 16753f3aea0SRoger A. Faulkner assert(MUTEX_HELD(&dpr->dpr_lock)); 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate for (dbp = dt_list_next(&dpr->dpr_bps); 1707c478bd9Sstevel@tonic-gate dbp != NULL; dbp = dt_list_next(dbp)) { 1717c478bd9Sstevel@tonic-gate if (!dbp->dbp_active && Psetbkpt(dpr->dpr_proc, 1727c478bd9Sstevel@tonic-gate dbp->dbp_addr, &dbp->dbp_instr) == 0) 1737c478bd9Sstevel@tonic-gate dbp->dbp_active = B_TRUE; 1747c478bd9Sstevel@tonic-gate } 175900524f3Sahl 176900524f3Sahl dt_dprintf("breakpoints enabled\n"); 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate 179dcafa303Sahl static void 1807c478bd9Sstevel@tonic-gate dt_proc_bpdisable(dt_proc_t *dpr) 1817c478bd9Sstevel@tonic-gate { 1827c478bd9Sstevel@tonic-gate dt_bkpt_t *dbp; 1837c478bd9Sstevel@tonic-gate 18453f3aea0SRoger A. Faulkner assert(MUTEX_HELD(&dpr->dpr_lock)); 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate for (dbp = dt_list_next(&dpr->dpr_bps); 1877c478bd9Sstevel@tonic-gate dbp != NULL; dbp = dt_list_next(dbp)) { 1887c478bd9Sstevel@tonic-gate if (dbp->dbp_active && Pdelbkpt(dpr->dpr_proc, 1897c478bd9Sstevel@tonic-gate dbp->dbp_addr, dbp->dbp_instr) == 0) 1907c478bd9Sstevel@tonic-gate dbp->dbp_active = B_FALSE; 1917c478bd9Sstevel@tonic-gate } 192900524f3Sahl 193900524f3Sahl dt_dprintf("breakpoints disabled\n"); 194900524f3Sahl } 195900524f3Sahl 196900524f3Sahl static void 197900524f3Sahl dt_proc_notify(dtrace_hdl_t *dtp, dt_proc_hash_t *dph, dt_proc_t *dpr, 198900524f3Sahl const char *msg) 199900524f3Sahl { 200900524f3Sahl dt_proc_notify_t *dprn = dt_alloc(dtp, sizeof (dt_proc_notify_t)); 201900524f3Sahl 202900524f3Sahl if (dprn == NULL) { 203900524f3Sahl dt_dprintf("failed to allocate notification for %d %s\n", 204900524f3Sahl (int)dpr->dpr_pid, msg); 205900524f3Sahl } else { 206900524f3Sahl dprn->dprn_dpr = dpr; 207900524f3Sahl if (msg == NULL) 208900524f3Sahl dprn->dprn_errmsg[0] = '\0'; 209900524f3Sahl else 210900524f3Sahl (void) strlcpy(dprn->dprn_errmsg, msg, 211900524f3Sahl sizeof (dprn->dprn_errmsg)); 212900524f3Sahl 213900524f3Sahl (void) pthread_mutex_lock(&dph->dph_lock); 214900524f3Sahl 215900524f3Sahl dprn->dprn_next = dph->dph_notify; 216900524f3Sahl dph->dph_notify = dprn; 217900524f3Sahl 218900524f3Sahl (void) pthread_cond_broadcast(&dph->dph_cv); 219900524f3Sahl (void) pthread_mutex_unlock(&dph->dph_lock); 220900524f3Sahl } 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate /* 2247c478bd9Sstevel@tonic-gate * Check to see if the control thread was requested to stop when the victim 2257c478bd9Sstevel@tonic-gate * process reached a particular event (why) rather than continuing the victim. 2267c478bd9Sstevel@tonic-gate * If 'why' is set in the stop mask, we wait on dpr_cv for dt_proc_continue(). 2277c478bd9Sstevel@tonic-gate * If 'why' is not set, this function returns immediately and does nothing. 2287c478bd9Sstevel@tonic-gate */ 2297c478bd9Sstevel@tonic-gate static void 2307c478bd9Sstevel@tonic-gate dt_proc_stop(dt_proc_t *dpr, uint8_t why) 2317c478bd9Sstevel@tonic-gate { 23253f3aea0SRoger A. Faulkner assert(MUTEX_HELD(&dpr->dpr_lock)); 2337c478bd9Sstevel@tonic-gate assert(why != DT_PROC_STOP_IDLE); 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate if (dpr->dpr_stop & why) { 2367c478bd9Sstevel@tonic-gate dpr->dpr_stop |= DT_PROC_STOP_IDLE; 2377c478bd9Sstevel@tonic-gate dpr->dpr_stop &= ~why; 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate (void) pthread_cond_broadcast(&dpr->dpr_cv); 2407c478bd9Sstevel@tonic-gate 241dcafa303Sahl /* 242dcafa303Sahl * We disable breakpoints while stopped to preserve the 243dcafa303Sahl * integrity of the program text for both our own disassembly 244dcafa303Sahl * and that of the kernel. 245dcafa303Sahl */ 246dcafa303Sahl dt_proc_bpdisable(dpr); 247dcafa303Sahl 2487c478bd9Sstevel@tonic-gate while (dpr->dpr_stop & DT_PROC_STOP_IDLE) 2497c478bd9Sstevel@tonic-gate (void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock); 250dcafa303Sahl 251dcafa303Sahl dt_proc_bpenable(dpr); 2527c478bd9Sstevel@tonic-gate } 2537c478bd9Sstevel@tonic-gate } 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2567c478bd9Sstevel@tonic-gate static void 2577c478bd9Sstevel@tonic-gate dt_proc_bpmain(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *fname) 2587c478bd9Sstevel@tonic-gate { 2597c478bd9Sstevel@tonic-gate dt_dprintf("pid %d: breakpoint at %s()\n", (int)dpr->dpr_pid, fname); 2607c478bd9Sstevel@tonic-gate dt_proc_stop(dpr, DT_PROC_STOP_MAIN); 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate static void 2647c478bd9Sstevel@tonic-gate dt_proc_rdevent(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *evname) 2657c478bd9Sstevel@tonic-gate { 2667c478bd9Sstevel@tonic-gate rd_event_msg_t rdm; 2677c478bd9Sstevel@tonic-gate rd_err_e err; 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate if ((err = rd_event_getmsg(dpr->dpr_rtld, &rdm)) != RD_OK) { 2707c478bd9Sstevel@tonic-gate dt_dprintf("pid %d: failed to get %s event message: %s\n", 2717c478bd9Sstevel@tonic-gate (int)dpr->dpr_pid, evname, rd_errstr(err)); 2727c478bd9Sstevel@tonic-gate return; 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate dt_dprintf("pid %d: rtld event %s type=%d state %d\n", 2767c478bd9Sstevel@tonic-gate (int)dpr->dpr_pid, evname, rdm.type, rdm.u.state); 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate switch (rdm.type) { 2797c478bd9Sstevel@tonic-gate case RD_DLACTIVITY: 280900524f3Sahl if (rdm.u.state != RD_CONSISTENT) 281900524f3Sahl break; 282900524f3Sahl 2837c478bd9Sstevel@tonic-gate Pupdate_syms(dpr->dpr_proc); 284900524f3Sahl if (dt_pid_create_probes_module(dtp, dpr) != 0) 285900524f3Sahl dt_proc_notify(dtp, dtp->dt_procs, dpr, 286900524f3Sahl dpr->dpr_errmsg); 287900524f3Sahl 2887c478bd9Sstevel@tonic-gate break; 2897c478bd9Sstevel@tonic-gate case RD_PREINIT: 2907c478bd9Sstevel@tonic-gate Pupdate_syms(dpr->dpr_proc); 2917c478bd9Sstevel@tonic-gate dt_proc_stop(dpr, DT_PROC_STOP_PREINIT); 2927c478bd9Sstevel@tonic-gate break; 2937c478bd9Sstevel@tonic-gate case RD_POSTINIT: 2947c478bd9Sstevel@tonic-gate Pupdate_syms(dpr->dpr_proc); 2957c478bd9Sstevel@tonic-gate dt_proc_stop(dpr, DT_PROC_STOP_POSTINIT); 2967c478bd9Sstevel@tonic-gate break; 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate static void 3017c478bd9Sstevel@tonic-gate dt_proc_rdwatch(dt_proc_t *dpr, rd_event_e event, const char *evname) 3027c478bd9Sstevel@tonic-gate { 3037c478bd9Sstevel@tonic-gate rd_notify_t rdn; 3047c478bd9Sstevel@tonic-gate rd_err_e err; 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate if ((err = rd_event_addr(dpr->dpr_rtld, event, &rdn)) != RD_OK) { 3077c478bd9Sstevel@tonic-gate dt_dprintf("pid %d: failed to get event address for %s: %s\n", 3087c478bd9Sstevel@tonic-gate (int)dpr->dpr_pid, evname, rd_errstr(err)); 3097c478bd9Sstevel@tonic-gate return; 3107c478bd9Sstevel@tonic-gate } 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate if (rdn.type != RD_NOTIFY_BPT) { 3137c478bd9Sstevel@tonic-gate dt_dprintf("pid %d: event %s has unexpected type %d\n", 3147c478bd9Sstevel@tonic-gate (int)dpr->dpr_pid, evname, rdn.type); 3157c478bd9Sstevel@tonic-gate return; 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate (void) dt_proc_bpcreate(dpr, rdn.u.bptaddr, 3197c478bd9Sstevel@tonic-gate (dt_bkpt_f *)dt_proc_rdevent, (void *)evname); 3207c478bd9Sstevel@tonic-gate } 3217c478bd9Sstevel@tonic-gate 3227c478bd9Sstevel@tonic-gate /* 3237c478bd9Sstevel@tonic-gate * Common code for enabling events associated with the run-time linker after 3247c478bd9Sstevel@tonic-gate * attaching to a process or after a victim process completes an exec(2). 3257c478bd9Sstevel@tonic-gate */ 3267c478bd9Sstevel@tonic-gate static void 3277c478bd9Sstevel@tonic-gate dt_proc_attach(dt_proc_t *dpr, int exec) 3287c478bd9Sstevel@tonic-gate { 3297c478bd9Sstevel@tonic-gate const pstatus_t *psp = Pstatus(dpr->dpr_proc); 3307c478bd9Sstevel@tonic-gate rd_err_e err; 3317c478bd9Sstevel@tonic-gate GElf_Sym sym; 3327c478bd9Sstevel@tonic-gate 33353f3aea0SRoger A. Faulkner assert(MUTEX_HELD(&dpr->dpr_lock)); 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate if (exec) { 3367c478bd9Sstevel@tonic-gate if (psp->pr_lwp.pr_errno != 0) 3377c478bd9Sstevel@tonic-gate return; /* exec failed: nothing needs to be done */ 3387c478bd9Sstevel@tonic-gate 3397c478bd9Sstevel@tonic-gate dt_proc_bpdestroy(dpr, B_FALSE); 3407c478bd9Sstevel@tonic-gate Preset_maps(dpr->dpr_proc); 3417c478bd9Sstevel@tonic-gate } 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate if ((dpr->dpr_rtld = Prd_agent(dpr->dpr_proc)) != NULL && 3447c478bd9Sstevel@tonic-gate (err = rd_event_enable(dpr->dpr_rtld, B_TRUE)) == RD_OK) { 3457c478bd9Sstevel@tonic-gate dt_proc_rdwatch(dpr, RD_PREINIT, "RD_PREINIT"); 3467c478bd9Sstevel@tonic-gate dt_proc_rdwatch(dpr, RD_POSTINIT, "RD_POSTINIT"); 3477c478bd9Sstevel@tonic-gate dt_proc_rdwatch(dpr, RD_DLACTIVITY, "RD_DLACTIVITY"); 3487c478bd9Sstevel@tonic-gate } else { 3497c478bd9Sstevel@tonic-gate dt_dprintf("pid %d: failed to enable rtld events: %s\n", 3507c478bd9Sstevel@tonic-gate (int)dpr->dpr_pid, dpr->dpr_rtld ? rd_errstr(err) : 3517c478bd9Sstevel@tonic-gate "rtld_db agent initialization failed"); 3527c478bd9Sstevel@tonic-gate } 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate Pupdate_maps(dpr->dpr_proc); 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate if (Pxlookup_by_name(dpr->dpr_proc, LM_ID_BASE, 3577c478bd9Sstevel@tonic-gate "a.out", "main", &sym, NULL) == 0) { 3587c478bd9Sstevel@tonic-gate (void) dt_proc_bpcreate(dpr, (uintptr_t)sym.st_value, 3597c478bd9Sstevel@tonic-gate (dt_bkpt_f *)dt_proc_bpmain, "a.out`main"); 3607c478bd9Sstevel@tonic-gate } else { 3617c478bd9Sstevel@tonic-gate dt_dprintf("pid %d: failed to find a.out`main: %s\n", 3627c478bd9Sstevel@tonic-gate (int)dpr->dpr_pid, strerror(errno)); 3637c478bd9Sstevel@tonic-gate } 3647c478bd9Sstevel@tonic-gate } 3657c478bd9Sstevel@tonic-gate 3667c478bd9Sstevel@tonic-gate /* 3677c478bd9Sstevel@tonic-gate * Wait for a stopped process to be set running again by some other debugger. 3687c478bd9Sstevel@tonic-gate * This is typically not required by /proc-based debuggers, since the usual 3697c478bd9Sstevel@tonic-gate * model is that one debugger controls one victim. But DTrace, as usual, has 3707c478bd9Sstevel@tonic-gate * its own needs: the stop() action assumes that prun(1) or some other tool 3717c478bd9Sstevel@tonic-gate * will be applied to resume the victim process. This could be solved by 3727c478bd9Sstevel@tonic-gate * adding a PCWRUN directive to /proc, but that seems like overkill unless 3737c478bd9Sstevel@tonic-gate * other debuggers end up needing this functionality, so we implement a cheap 3747c478bd9Sstevel@tonic-gate * equivalent to PCWRUN using the set of existing kernel mechanisms. 3757c478bd9Sstevel@tonic-gate * 3767c478bd9Sstevel@tonic-gate * Our intent is really not just to wait for the victim to run, but rather to 3777c478bd9Sstevel@tonic-gate * wait for it to run and then stop again for a reason other than the current 3787c478bd9Sstevel@tonic-gate * PR_REQUESTED stop. Since PCWSTOP/Pstopstatus() can be applied repeatedly 3797c478bd9Sstevel@tonic-gate * to a stopped process and will return the same result without affecting the 3807c478bd9Sstevel@tonic-gate * victim, we can just perform these operations repeatedly until Pstate() 3817c478bd9Sstevel@tonic-gate * changes, the representative LWP ID changes, or the stop timestamp advances. 3827c478bd9Sstevel@tonic-gate * dt_proc_control() will then rediscover the new state and continue as usual. 3837c478bd9Sstevel@tonic-gate * When the process is still stopped in the same exact state, we sleep for a 3847c478bd9Sstevel@tonic-gate * brief interval before waiting again so as not to spin consuming CPU cycles. 3857c478bd9Sstevel@tonic-gate */ 3867c478bd9Sstevel@tonic-gate static void 3877c478bd9Sstevel@tonic-gate dt_proc_waitrun(dt_proc_t *dpr) 3887c478bd9Sstevel@tonic-gate { 3897c478bd9Sstevel@tonic-gate struct ps_prochandle *P = dpr->dpr_proc; 3907c478bd9Sstevel@tonic-gate const lwpstatus_t *psp = &Pstatus(P)->pr_lwp; 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate int krflag = psp->pr_flags & (PR_KLC | PR_RLC); 3937c478bd9Sstevel@tonic-gate timestruc_t tstamp = psp->pr_tstamp; 3947c478bd9Sstevel@tonic-gate lwpid_t lwpid = psp->pr_lwpid; 3957c478bd9Sstevel@tonic-gate 3967c478bd9Sstevel@tonic-gate const long wstop = PCWSTOP; 3977c478bd9Sstevel@tonic-gate int pfd = Pctlfd(P); 3987c478bd9Sstevel@tonic-gate 39953f3aea0SRoger A. Faulkner assert(MUTEX_HELD(&dpr->dpr_lock)); 4007c478bd9Sstevel@tonic-gate assert(psp->pr_flags & PR_STOPPED); 4017c478bd9Sstevel@tonic-gate assert(Pstate(P) == PS_STOP); 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate /* 4047c478bd9Sstevel@tonic-gate * While we are waiting for the victim to run, clear PR_KLC and PR_RLC 4057c478bd9Sstevel@tonic-gate * so that if the libdtrace client is killed, the victim stays stopped. 4067c478bd9Sstevel@tonic-gate * dt_proc_destroy() will also observe this and perform PRELEASE_HANG. 4077c478bd9Sstevel@tonic-gate */ 4087c478bd9Sstevel@tonic-gate (void) Punsetflags(P, krflag); 4097c478bd9Sstevel@tonic-gate Psync(P); 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 4127c478bd9Sstevel@tonic-gate 4137c478bd9Sstevel@tonic-gate while (!dpr->dpr_quit) { 4147c478bd9Sstevel@tonic-gate if (write(pfd, &wstop, sizeof (wstop)) == -1 && errno == EINTR) 4157c478bd9Sstevel@tonic-gate continue; /* check dpr_quit and continue waiting */ 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&dpr->dpr_lock); 4187c478bd9Sstevel@tonic-gate (void) Pstopstatus(P, PCNULL, 0); 4197c478bd9Sstevel@tonic-gate psp = &Pstatus(P)->pr_lwp; 4207c478bd9Sstevel@tonic-gate 4217c478bd9Sstevel@tonic-gate /* 4227c478bd9Sstevel@tonic-gate * If we've reached a new state, found a new representative, or 4237c478bd9Sstevel@tonic-gate * the stop timestamp has changed, restore PR_KLC/PR_RLC to its 4247c478bd9Sstevel@tonic-gate * original setting and then return with dpr_lock held. 4257c478bd9Sstevel@tonic-gate */ 4267c478bd9Sstevel@tonic-gate if (Pstate(P) != PS_STOP || psp->pr_lwpid != lwpid || 4277c478bd9Sstevel@tonic-gate bcmp(&psp->pr_tstamp, &tstamp, sizeof (tstamp)) != 0) { 4287c478bd9Sstevel@tonic-gate (void) Psetflags(P, krflag); 4297c478bd9Sstevel@tonic-gate Psync(P); 4307c478bd9Sstevel@tonic-gate return; 4317c478bd9Sstevel@tonic-gate } 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 4347c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, MILLISEC / 2); 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&dpr->dpr_lock); 4387c478bd9Sstevel@tonic-gate } 4397c478bd9Sstevel@tonic-gate 4407c478bd9Sstevel@tonic-gate typedef struct dt_proc_control_data { 4417c478bd9Sstevel@tonic-gate dtrace_hdl_t *dpcd_hdl; /* DTrace handle */ 4427c478bd9Sstevel@tonic-gate dt_proc_t *dpcd_proc; /* proccess to control */ 4437c478bd9Sstevel@tonic-gate } dt_proc_control_data_t; 4447c478bd9Sstevel@tonic-gate 4457c478bd9Sstevel@tonic-gate /* 4467c478bd9Sstevel@tonic-gate * Main loop for all victim process control threads. We initialize all the 4477c478bd9Sstevel@tonic-gate * appropriate /proc control mechanisms, and then enter a loop waiting for 4487c478bd9Sstevel@tonic-gate * the process to stop on an event or die. We process any events by calling 4497c478bd9Sstevel@tonic-gate * appropriate subroutines, and exit when the victim dies or we lose control. 4507c478bd9Sstevel@tonic-gate * 4517c478bd9Sstevel@tonic-gate * The control thread synchronizes the use of dpr_proc with other libdtrace 4527c478bd9Sstevel@tonic-gate * threads using dpr_lock. We hold the lock for all of our operations except 4537c478bd9Sstevel@tonic-gate * waiting while the process is running: this is accomplished by writing a 4547c478bd9Sstevel@tonic-gate * PCWSTOP directive directly to the underlying /proc/<pid>/ctl file. If the 4557c478bd9Sstevel@tonic-gate * libdtrace client wishes to exit or abort our wait, SIGCANCEL can be used. 4567c478bd9Sstevel@tonic-gate */ 4577c478bd9Sstevel@tonic-gate static void * 4587c478bd9Sstevel@tonic-gate dt_proc_control(void *arg) 4597c478bd9Sstevel@tonic-gate { 4607c478bd9Sstevel@tonic-gate dt_proc_control_data_t *datap = arg; 4617c478bd9Sstevel@tonic-gate dtrace_hdl_t *dtp = datap->dpcd_hdl; 4627c478bd9Sstevel@tonic-gate dt_proc_t *dpr = datap->dpcd_proc; 4637c478bd9Sstevel@tonic-gate dt_proc_hash_t *dph = dpr->dpr_hdl->dt_procs; 4647c478bd9Sstevel@tonic-gate struct ps_prochandle *P = dpr->dpr_proc; 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate int pfd = Pctlfd(P); 4677c478bd9Sstevel@tonic-gate int pid = dpr->dpr_pid; 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate const long wstop = PCWSTOP; 4707c478bd9Sstevel@tonic-gate int notify = B_FALSE; 4717c478bd9Sstevel@tonic-gate 4727c478bd9Sstevel@tonic-gate /* 4737c478bd9Sstevel@tonic-gate * We disable the POSIX thread cancellation mechanism so that the 4747c478bd9Sstevel@tonic-gate * client program using libdtrace can't accidentally cancel our thread. 4757c478bd9Sstevel@tonic-gate * dt_proc_destroy() uses SIGCANCEL explicitly to simply poke us out 4767c478bd9Sstevel@tonic-gate * of PCWSTOP with EINTR, at which point we will see dpr_quit and exit. 4777c478bd9Sstevel@tonic-gate */ 4787c478bd9Sstevel@tonic-gate (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate /* 4817c478bd9Sstevel@tonic-gate * Set up the corresponding process for tracing by libdtrace. We want 4827c478bd9Sstevel@tonic-gate * to be able to catch breakpoints and efficiently single-step over 4837c478bd9Sstevel@tonic-gate * them, and we need to enable librtld_db to watch libdl activity. 4847c478bd9Sstevel@tonic-gate */ 4857c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&dpr->dpr_lock); 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate (void) Punsetflags(P, PR_ASYNC); /* require synchronous mode */ 4887c478bd9Sstevel@tonic-gate (void) Psetflags(P, PR_BPTADJ); /* always adjust eip on x86 */ 4897c478bd9Sstevel@tonic-gate (void) Punsetflags(P, PR_FORK); /* do not inherit on fork */ 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate (void) Pfault(P, FLTBPT, B_TRUE); /* always trace breakpoints */ 4927c478bd9Sstevel@tonic-gate (void) Pfault(P, FLTTRACE, B_TRUE); /* always trace single-step */ 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate /* 4957c478bd9Sstevel@tonic-gate * We must trace exit from exec() system calls so that if the exec is 4967c478bd9Sstevel@tonic-gate * successful, we can reset our breakpoints and re-initialize libproc. 4977c478bd9Sstevel@tonic-gate */ 4987c478bd9Sstevel@tonic-gate (void) Psysexit(P, SYS_execve, B_TRUE); 4997c478bd9Sstevel@tonic-gate 5007c478bd9Sstevel@tonic-gate /* 5017c478bd9Sstevel@tonic-gate * We must trace entry and exit for fork() system calls in order to 5027c478bd9Sstevel@tonic-gate * disable our breakpoints temporarily during the fork. We do not set 5037c478bd9Sstevel@tonic-gate * the PR_FORK flag, so if fork succeeds the child begins executing and 5047c478bd9Sstevel@tonic-gate * does not inherit any other tracing behaviors or a control thread. 5057c478bd9Sstevel@tonic-gate */ 5067c478bd9Sstevel@tonic-gate (void) Psysentry(P, SYS_vfork, B_TRUE); 5077c478bd9Sstevel@tonic-gate (void) Psysexit(P, SYS_vfork, B_TRUE); 508657b1f3dSraf (void) Psysentry(P, SYS_forksys, B_TRUE); 509657b1f3dSraf (void) Psysexit(P, SYS_forksys, B_TRUE); 5107c478bd9Sstevel@tonic-gate 5117c478bd9Sstevel@tonic-gate Psync(P); /* enable all /proc changes */ 5127c478bd9Sstevel@tonic-gate dt_proc_attach(dpr, B_FALSE); /* enable rtld breakpoints */ 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate /* 5157c478bd9Sstevel@tonic-gate * If PR_KLC is set, we created the process; otherwise we grabbed it. 5167c478bd9Sstevel@tonic-gate * Check for an appropriate stop request and wait for dt_proc_continue. 5177c478bd9Sstevel@tonic-gate */ 5187c478bd9Sstevel@tonic-gate if (Pstatus(P)->pr_flags & PR_KLC) 5197c478bd9Sstevel@tonic-gate dt_proc_stop(dpr, DT_PROC_STOP_CREATE); 5207c478bd9Sstevel@tonic-gate else 5217c478bd9Sstevel@tonic-gate dt_proc_stop(dpr, DT_PROC_STOP_GRAB); 5227c478bd9Sstevel@tonic-gate 5237c478bd9Sstevel@tonic-gate if (Psetrun(P, 0, 0) == -1) { 5247c478bd9Sstevel@tonic-gate dt_dprintf("pid %d: failed to set running: %s\n", 5257c478bd9Sstevel@tonic-gate (int)dpr->dpr_pid, strerror(errno)); 5267c478bd9Sstevel@tonic-gate } 5277c478bd9Sstevel@tonic-gate 5287c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate /* 5317c478bd9Sstevel@tonic-gate * Wait for the process corresponding to this control thread to stop, 5327c478bd9Sstevel@tonic-gate * process the event, and then set it running again. We want to sleep 5337c478bd9Sstevel@tonic-gate * with dpr_lock *unheld* so that other parts of libdtrace can use the 5347c478bd9Sstevel@tonic-gate * ps_prochandle in the meantime (e.g. ustack()). To do this, we write 5357c478bd9Sstevel@tonic-gate * a PCWSTOP directive directly to the underlying /proc/<pid>/ctl file. 5367c478bd9Sstevel@tonic-gate * Once the process stops, we wake up, grab dpr_lock, and then call 5377c478bd9Sstevel@tonic-gate * Pwait() (which will return immediately) and do our processing. 5387c478bd9Sstevel@tonic-gate */ 5397c478bd9Sstevel@tonic-gate while (!dpr->dpr_quit) { 5407c478bd9Sstevel@tonic-gate const lwpstatus_t *psp; 5417c478bd9Sstevel@tonic-gate 5427c478bd9Sstevel@tonic-gate if (write(pfd, &wstop, sizeof (wstop)) == -1 && errno == EINTR) 5437c478bd9Sstevel@tonic-gate continue; /* check dpr_quit and continue waiting */ 5447c478bd9Sstevel@tonic-gate 5457c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&dpr->dpr_lock); 5467c478bd9Sstevel@tonic-gate pwait_locked: 5477c478bd9Sstevel@tonic-gate if (Pstopstatus(P, PCNULL, 0) == -1 && errno == EINTR) { 5487c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 5497c478bd9Sstevel@tonic-gate continue; /* check dpr_quit and continue waiting */ 5507c478bd9Sstevel@tonic-gate } 5517c478bd9Sstevel@tonic-gate 5527c478bd9Sstevel@tonic-gate switch (Pstate(P)) { 5537c478bd9Sstevel@tonic-gate case PS_STOP: 5547c478bd9Sstevel@tonic-gate psp = &Pstatus(P)->pr_lwp; 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate dt_dprintf("pid %d: proc stopped showing %d/%d\n", 5577c478bd9Sstevel@tonic-gate pid, psp->pr_why, psp->pr_what); 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate /* 5607c478bd9Sstevel@tonic-gate * If the process stops showing PR_REQUESTED, then the 5617c478bd9Sstevel@tonic-gate * DTrace stop() action was applied to it or another 5627c478bd9Sstevel@tonic-gate * debugging utility (e.g. pstop(1)) asked it to stop. 5637c478bd9Sstevel@tonic-gate * In either case, the user's intention is for the 5647c478bd9Sstevel@tonic-gate * process to remain stopped until another external 5657c478bd9Sstevel@tonic-gate * mechanism (e.g. prun(1)) is applied. So instead of 5667c478bd9Sstevel@tonic-gate * setting the process running ourself, we wait for 5677c478bd9Sstevel@tonic-gate * someone else to do so. Once that happens, we return 5687c478bd9Sstevel@tonic-gate * to our normal loop waiting for an event of interest. 5697c478bd9Sstevel@tonic-gate */ 5707c478bd9Sstevel@tonic-gate if (psp->pr_why == PR_REQUESTED) { 5717c478bd9Sstevel@tonic-gate dt_proc_waitrun(dpr); 5727c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 5737c478bd9Sstevel@tonic-gate continue; 5747c478bd9Sstevel@tonic-gate } 5757c478bd9Sstevel@tonic-gate 5767c478bd9Sstevel@tonic-gate /* 5777c478bd9Sstevel@tonic-gate * If the process stops showing one of the events that 5787c478bd9Sstevel@tonic-gate * we are tracing, perform the appropriate response. 5797c478bd9Sstevel@tonic-gate * Note that we ignore PR_SUSPENDED, PR_CHECKPOINT, and 5807c478bd9Sstevel@tonic-gate * PR_JOBCONTROL by design: if one of these conditions 5817c478bd9Sstevel@tonic-gate * occurs, we will fall through to Psetrun() but the 5827c478bd9Sstevel@tonic-gate * process will remain stopped in the kernel by the 5837c478bd9Sstevel@tonic-gate * corresponding mechanism (e.g. job control stop). 5847c478bd9Sstevel@tonic-gate */ 5857c478bd9Sstevel@tonic-gate if (psp->pr_why == PR_FAULTED && psp->pr_what == FLTBPT) 5867c478bd9Sstevel@tonic-gate dt_proc_bpmatch(dtp, dpr); 5877c478bd9Sstevel@tonic-gate else if (psp->pr_why == PR_SYSENTRY && 5887c478bd9Sstevel@tonic-gate IS_SYS_FORK(psp->pr_what)) 5897c478bd9Sstevel@tonic-gate dt_proc_bpdisable(dpr); 5907c478bd9Sstevel@tonic-gate else if (psp->pr_why == PR_SYSEXIT && 5917c478bd9Sstevel@tonic-gate IS_SYS_FORK(psp->pr_what)) 5927c478bd9Sstevel@tonic-gate dt_proc_bpenable(dpr); 5937c478bd9Sstevel@tonic-gate else if (psp->pr_why == PR_SYSEXIT && 5947c478bd9Sstevel@tonic-gate IS_SYS_EXEC(psp->pr_what)) 5957c478bd9Sstevel@tonic-gate dt_proc_attach(dpr, B_TRUE); 5967c478bd9Sstevel@tonic-gate break; 5977c478bd9Sstevel@tonic-gate 5987c478bd9Sstevel@tonic-gate case PS_LOST: 5997c478bd9Sstevel@tonic-gate if (Preopen(P) == 0) 6007c478bd9Sstevel@tonic-gate goto pwait_locked; 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate dt_dprintf("pid %d: proc lost: %s\n", 6037c478bd9Sstevel@tonic-gate pid, strerror(errno)); 6047c478bd9Sstevel@tonic-gate 6057c478bd9Sstevel@tonic-gate dpr->dpr_quit = B_TRUE; 6067c478bd9Sstevel@tonic-gate notify = B_TRUE; 6077c478bd9Sstevel@tonic-gate break; 6087c478bd9Sstevel@tonic-gate 6097c478bd9Sstevel@tonic-gate case PS_UNDEAD: 6107c478bd9Sstevel@tonic-gate dt_dprintf("pid %d: proc died\n", pid); 6117c478bd9Sstevel@tonic-gate dpr->dpr_quit = B_TRUE; 6127c478bd9Sstevel@tonic-gate notify = B_TRUE; 6137c478bd9Sstevel@tonic-gate break; 6147c478bd9Sstevel@tonic-gate } 6157c478bd9Sstevel@tonic-gate 6167c478bd9Sstevel@tonic-gate if (Pstate(P) != PS_UNDEAD && Psetrun(P, 0, 0) == -1) { 6177c478bd9Sstevel@tonic-gate dt_dprintf("pid %d: failed to set running: %s\n", 6187c478bd9Sstevel@tonic-gate (int)dpr->dpr_pid, strerror(errno)); 6197c478bd9Sstevel@tonic-gate } 6207c478bd9Sstevel@tonic-gate 6217c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 6227c478bd9Sstevel@tonic-gate } 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate /* 6257c478bd9Sstevel@tonic-gate * If the control thread detected PS_UNDEAD or PS_LOST, then enqueue 6267c478bd9Sstevel@tonic-gate * the dt_proc_t structure on the dt_proc_hash_t notification list. 6277c478bd9Sstevel@tonic-gate */ 628900524f3Sahl if (notify) 629900524f3Sahl dt_proc_notify(dtp, dph, dpr, NULL); 6307c478bd9Sstevel@tonic-gate 6317c478bd9Sstevel@tonic-gate /* 6327c478bd9Sstevel@tonic-gate * Destroy and remove any remaining breakpoints, set dpr_done and clear 6337c478bd9Sstevel@tonic-gate * dpr_tid to indicate the control thread has exited, and notify any 6347c478bd9Sstevel@tonic-gate * waiting thread in dt_proc_destroy() that we have succesfully exited. 6357c478bd9Sstevel@tonic-gate */ 6367c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&dpr->dpr_lock); 6377c478bd9Sstevel@tonic-gate 6387c478bd9Sstevel@tonic-gate dt_proc_bpdestroy(dpr, B_TRUE); 6397c478bd9Sstevel@tonic-gate dpr->dpr_done = B_TRUE; 6407c478bd9Sstevel@tonic-gate dpr->dpr_tid = 0; 6417c478bd9Sstevel@tonic-gate 6427c478bd9Sstevel@tonic-gate (void) pthread_cond_broadcast(&dpr->dpr_cv); 643900524f3Sahl (void) pthread_mutex_unlock(&dpr->dpr_lock); 6447c478bd9Sstevel@tonic-gate 6457c478bd9Sstevel@tonic-gate return (NULL); 6467c478bd9Sstevel@tonic-gate } 6477c478bd9Sstevel@tonic-gate 6487c478bd9Sstevel@tonic-gate /*PRINTFLIKE3*/ 6497c478bd9Sstevel@tonic-gate static struct ps_prochandle * 6507c478bd9Sstevel@tonic-gate dt_proc_error(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *format, ...) 6517c478bd9Sstevel@tonic-gate { 6527c478bd9Sstevel@tonic-gate va_list ap; 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate va_start(ap, format); 6557c478bd9Sstevel@tonic-gate dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap); 6567c478bd9Sstevel@tonic-gate va_end(ap); 6577c478bd9Sstevel@tonic-gate 6587c478bd9Sstevel@tonic-gate if (dpr->dpr_proc != NULL) 6597c478bd9Sstevel@tonic-gate Prelease(dpr->dpr_proc, 0); 6607c478bd9Sstevel@tonic-gate 6617c478bd9Sstevel@tonic-gate dt_free(dtp, dpr); 6627c478bd9Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_COMPILER); 6637c478bd9Sstevel@tonic-gate return (NULL); 6647c478bd9Sstevel@tonic-gate } 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate dt_proc_t * 6677c478bd9Sstevel@tonic-gate dt_proc_lookup(dtrace_hdl_t *dtp, struct ps_prochandle *P, int remove) 6687c478bd9Sstevel@tonic-gate { 6697c478bd9Sstevel@tonic-gate dt_proc_hash_t *dph = dtp->dt_procs; 6707c478bd9Sstevel@tonic-gate pid_t pid = Pstatus(P)->pr_pid; 6717c478bd9Sstevel@tonic-gate dt_proc_t *dpr, **dpp = &dph->dph_hash[pid & (dph->dph_hashlen - 1)]; 6727c478bd9Sstevel@tonic-gate 6737c478bd9Sstevel@tonic-gate for (dpr = *dpp; dpr != NULL; dpr = dpr->dpr_hash) { 6747c478bd9Sstevel@tonic-gate if (dpr->dpr_pid == pid) 6757c478bd9Sstevel@tonic-gate break; 6767c478bd9Sstevel@tonic-gate else 6777c478bd9Sstevel@tonic-gate dpp = &dpr->dpr_hash; 6787c478bd9Sstevel@tonic-gate } 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate assert(dpr != NULL); 6817c478bd9Sstevel@tonic-gate assert(dpr->dpr_proc == P); 6827c478bd9Sstevel@tonic-gate 6837c478bd9Sstevel@tonic-gate if (remove) 6847c478bd9Sstevel@tonic-gate *dpp = dpr->dpr_hash; /* remove from pid hash chain */ 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate return (dpr); 6877c478bd9Sstevel@tonic-gate } 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate static void 6907c478bd9Sstevel@tonic-gate dt_proc_destroy(dtrace_hdl_t *dtp, struct ps_prochandle *P) 6917c478bd9Sstevel@tonic-gate { 692b365acd0Sbmc dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE); 6937c478bd9Sstevel@tonic-gate dt_proc_hash_t *dph = dtp->dt_procs; 694900524f3Sahl dt_proc_notify_t *npr, **npp; 6957c478bd9Sstevel@tonic-gate int rflag; 6967c478bd9Sstevel@tonic-gate 6977c478bd9Sstevel@tonic-gate assert(dpr != NULL); 6987c478bd9Sstevel@tonic-gate 6997c478bd9Sstevel@tonic-gate /* 7007c478bd9Sstevel@tonic-gate * If neither PR_KLC nor PR_RLC is set, then the process is stopped by 7017c478bd9Sstevel@tonic-gate * an external debugger and we were waiting in dt_proc_waitrun(). 7027c478bd9Sstevel@tonic-gate * Leave the process in this condition using PRELEASE_HANG. 7037c478bd9Sstevel@tonic-gate */ 7047c478bd9Sstevel@tonic-gate if (!(Pstatus(dpr->dpr_proc)->pr_flags & (PR_KLC | PR_RLC))) { 7057c478bd9Sstevel@tonic-gate dt_dprintf("abandoning pid %d\n", (int)dpr->dpr_pid); 7067c478bd9Sstevel@tonic-gate rflag = PRELEASE_HANG; 70758dbc507SJonathan Haslam } else if (Pstatus(dpr->dpr_proc)->pr_flags & PR_KLC) { 70858dbc507SJonathan Haslam dt_dprintf("killing pid %d\n", (int)dpr->dpr_pid); 70958dbc507SJonathan Haslam rflag = PRELEASE_KILL; /* apply kill-on-last-close */ 7107c478bd9Sstevel@tonic-gate } else { 7117c478bd9Sstevel@tonic-gate dt_dprintf("releasing pid %d\n", (int)dpr->dpr_pid); 71258dbc507SJonathan Haslam rflag = 0; /* apply run-on-last-close */ 7137c478bd9Sstevel@tonic-gate } 7147c478bd9Sstevel@tonic-gate 7157c478bd9Sstevel@tonic-gate if (dpr->dpr_tid) { 7167c478bd9Sstevel@tonic-gate /* 7177c478bd9Sstevel@tonic-gate * Set the dpr_quit flag to tell the daemon thread to exit. We 7187c478bd9Sstevel@tonic-gate * send it a SIGCANCEL to poke it out of PCWSTOP or any other 7197c478bd9Sstevel@tonic-gate * long-term /proc system call. Our daemon threads have POSIX 7207c478bd9Sstevel@tonic-gate * cancellation disabled, so EINTR will be the only effect. We 7217c478bd9Sstevel@tonic-gate * then wait for dpr_done to indicate the thread has exited. 7227c478bd9Sstevel@tonic-gate * 7237c478bd9Sstevel@tonic-gate * We can't use pthread_kill() to send SIGCANCEL because the 7247c478bd9Sstevel@tonic-gate * interface forbids it and we can't use pthread_cancel() 7257c478bd9Sstevel@tonic-gate * because with cancellation disabled it won't actually 7267c478bd9Sstevel@tonic-gate * send SIGCANCEL to the target thread, so we use _lwp_kill() 7277c478bd9Sstevel@tonic-gate * to do the job. This is all built on evil knowledge of 7287c478bd9Sstevel@tonic-gate * the details of the cancellation mechanism in libc. 7297c478bd9Sstevel@tonic-gate */ 7307c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&dpr->dpr_lock); 7317c478bd9Sstevel@tonic-gate dpr->dpr_quit = B_TRUE; 7327c478bd9Sstevel@tonic-gate (void) _lwp_kill(dpr->dpr_tid, SIGCANCEL); 7337c478bd9Sstevel@tonic-gate 7341a7c1b72Smws /* 7351a7c1b72Smws * If the process is currently idling in dt_proc_stop(), re- 7361a7c1b72Smws * enable breakpoints and poke it into running again. 7371a7c1b72Smws */ 7381a7c1b72Smws if (dpr->dpr_stop & DT_PROC_STOP_IDLE) { 7391a7c1b72Smws dt_proc_bpenable(dpr); 7401a7c1b72Smws dpr->dpr_stop &= ~DT_PROC_STOP_IDLE; 7411a7c1b72Smws (void) pthread_cond_broadcast(&dpr->dpr_cv); 7421a7c1b72Smws } 7431a7c1b72Smws 7447c478bd9Sstevel@tonic-gate while (!dpr->dpr_done) 7457c478bd9Sstevel@tonic-gate (void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock); 7467c478bd9Sstevel@tonic-gate 7477c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 7487c478bd9Sstevel@tonic-gate } 7497c478bd9Sstevel@tonic-gate 7507c478bd9Sstevel@tonic-gate /* 751b365acd0Sbmc * Before we free the process structure, remove this dt_proc_t from the 752b365acd0Sbmc * lookup hash, and then walk the dt_proc_hash_t's notification list 753b365acd0Sbmc * and remove this dt_proc_t if it is enqueued. 7547c478bd9Sstevel@tonic-gate */ 7557c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&dph->dph_lock); 756b365acd0Sbmc (void) dt_proc_lookup(dtp, P, B_TRUE); 7577c478bd9Sstevel@tonic-gate npp = &dph->dph_notify; 7587c478bd9Sstevel@tonic-gate 759900524f3Sahl while ((npr = *npp) != NULL) { 760900524f3Sahl if (npr->dprn_dpr == dpr) { 761900524f3Sahl *npp = npr->dprn_next; 762900524f3Sahl dt_free(dtp, npr); 763900524f3Sahl } else { 764900524f3Sahl npp = &npr->dprn_next; 7657c478bd9Sstevel@tonic-gate } 7667c478bd9Sstevel@tonic-gate } 7677c478bd9Sstevel@tonic-gate 7687c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&dph->dph_lock); 7697c478bd9Sstevel@tonic-gate 7707c478bd9Sstevel@tonic-gate /* 7717c478bd9Sstevel@tonic-gate * Remove the dt_proc_list from the LRU list, release the underlying 7727c478bd9Sstevel@tonic-gate * libproc handle, and free our dt_proc_t data structure. 7737c478bd9Sstevel@tonic-gate */ 7747c478bd9Sstevel@tonic-gate if (dpr->dpr_cacheable) { 7757c478bd9Sstevel@tonic-gate assert(dph->dph_lrucnt != 0); 7767c478bd9Sstevel@tonic-gate dph->dph_lrucnt--; 7777c478bd9Sstevel@tonic-gate } 7787c478bd9Sstevel@tonic-gate 7797c478bd9Sstevel@tonic-gate dt_list_delete(&dph->dph_lrulist, dpr); 7807c478bd9Sstevel@tonic-gate Prelease(dpr->dpr_proc, rflag); 7817c478bd9Sstevel@tonic-gate dt_free(dtp, dpr); 7827c478bd9Sstevel@tonic-gate } 7837c478bd9Sstevel@tonic-gate 7847c478bd9Sstevel@tonic-gate static int 7857c478bd9Sstevel@tonic-gate dt_proc_create_thread(dtrace_hdl_t *dtp, dt_proc_t *dpr, uint_t stop) 7867c478bd9Sstevel@tonic-gate { 7877c478bd9Sstevel@tonic-gate dt_proc_control_data_t data; 7887c478bd9Sstevel@tonic-gate sigset_t nset, oset; 7897c478bd9Sstevel@tonic-gate pthread_attr_t a; 7907c478bd9Sstevel@tonic-gate int err; 7917c478bd9Sstevel@tonic-gate 7927c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&dpr->dpr_lock); 7937c478bd9Sstevel@tonic-gate dpr->dpr_stop |= stop; /* set bit for initial rendezvous */ 7947c478bd9Sstevel@tonic-gate 7957c478bd9Sstevel@tonic-gate (void) pthread_attr_init(&a); 7967c478bd9Sstevel@tonic-gate (void) pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED); 7977c478bd9Sstevel@tonic-gate 7987c478bd9Sstevel@tonic-gate (void) sigfillset(&nset); 7997c478bd9Sstevel@tonic-gate (void) sigdelset(&nset, SIGABRT); /* unblocked for assert() */ 8007c478bd9Sstevel@tonic-gate (void) sigdelset(&nset, SIGCANCEL); /* see dt_proc_destroy() */ 8017c478bd9Sstevel@tonic-gate 8027c478bd9Sstevel@tonic-gate data.dpcd_hdl = dtp; 8037c478bd9Sstevel@tonic-gate data.dpcd_proc = dpr; 8047c478bd9Sstevel@tonic-gate 8057c478bd9Sstevel@tonic-gate (void) pthread_sigmask(SIG_SETMASK, &nset, &oset); 8067c478bd9Sstevel@tonic-gate err = pthread_create(&dpr->dpr_tid, &a, dt_proc_control, &data); 8077c478bd9Sstevel@tonic-gate (void) pthread_sigmask(SIG_SETMASK, &oset, NULL); 8087c478bd9Sstevel@tonic-gate 8097c478bd9Sstevel@tonic-gate /* 8107c478bd9Sstevel@tonic-gate * If the control thread was created, then wait on dpr_cv for either 8117c478bd9Sstevel@tonic-gate * dpr_done to be set (the victim died or the control thread failed) 8127c478bd9Sstevel@tonic-gate * or DT_PROC_STOP_IDLE to be set, indicating that the victim is now 8137c478bd9Sstevel@tonic-gate * stopped by /proc and the control thread is at the rendezvous event. 8147c478bd9Sstevel@tonic-gate * On success, we return with the process and control thread stopped: 8157c478bd9Sstevel@tonic-gate * the caller can then apply dt_proc_continue() to resume both. 8167c478bd9Sstevel@tonic-gate */ 8177c478bd9Sstevel@tonic-gate if (err == 0) { 8187c478bd9Sstevel@tonic-gate while (!dpr->dpr_done && !(dpr->dpr_stop & DT_PROC_STOP_IDLE)) 8197c478bd9Sstevel@tonic-gate (void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock); 8207c478bd9Sstevel@tonic-gate 8217c478bd9Sstevel@tonic-gate /* 8227c478bd9Sstevel@tonic-gate * If dpr_done is set, the control thread aborted before it 8237c478bd9Sstevel@tonic-gate * reached the rendezvous event. This is either due to PS_LOST 8247c478bd9Sstevel@tonic-gate * or PS_UNDEAD (i.e. the process died). We try to provide a 8257c478bd9Sstevel@tonic-gate * small amount of useful information to help figure it out. 8267c478bd9Sstevel@tonic-gate */ 8277c478bd9Sstevel@tonic-gate if (dpr->dpr_done) { 8287c478bd9Sstevel@tonic-gate const psinfo_t *prp = Ppsinfo(dpr->dpr_proc); 8297c478bd9Sstevel@tonic-gate int stat = prp ? prp->pr_wstat : 0; 8307c478bd9Sstevel@tonic-gate int pid = dpr->dpr_pid; 8317c478bd9Sstevel@tonic-gate 8327c478bd9Sstevel@tonic-gate if (Pstate(dpr->dpr_proc) == PS_LOST) { 8337c478bd9Sstevel@tonic-gate (void) dt_proc_error(dpr->dpr_hdl, dpr, 8347c478bd9Sstevel@tonic-gate "failed to control pid %d: process exec'd " 8357c478bd9Sstevel@tonic-gate "set-id or unobservable program\n", pid); 8367c478bd9Sstevel@tonic-gate } else if (WIFSIGNALED(stat)) { 8377c478bd9Sstevel@tonic-gate (void) dt_proc_error(dpr->dpr_hdl, dpr, 8387c478bd9Sstevel@tonic-gate "failed to control pid %d: process died " 8397c478bd9Sstevel@tonic-gate "from signal %d\n", pid, WTERMSIG(stat)); 8407c478bd9Sstevel@tonic-gate } else { 8417c478bd9Sstevel@tonic-gate (void) dt_proc_error(dpr->dpr_hdl, dpr, 8427c478bd9Sstevel@tonic-gate "failed to control pid %d: process exited " 8437c478bd9Sstevel@tonic-gate "with status %d\n", pid, WEXITSTATUS(stat)); 8447c478bd9Sstevel@tonic-gate } 8457c478bd9Sstevel@tonic-gate 8467c478bd9Sstevel@tonic-gate err = ESRCH; /* cause grab() or create() to fail */ 8477c478bd9Sstevel@tonic-gate } 8487c478bd9Sstevel@tonic-gate } else { 8497c478bd9Sstevel@tonic-gate (void) dt_proc_error(dpr->dpr_hdl, dpr, 8507c478bd9Sstevel@tonic-gate "failed to create control thread for process-id %d: %s\n", 8517c478bd9Sstevel@tonic-gate (int)dpr->dpr_pid, strerror(err)); 8527c478bd9Sstevel@tonic-gate } 8537c478bd9Sstevel@tonic-gate 8547c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 8557c478bd9Sstevel@tonic-gate (void) pthread_attr_destroy(&a); 8567c478bd9Sstevel@tonic-gate 8577c478bd9Sstevel@tonic-gate return (err); 8587c478bd9Sstevel@tonic-gate } 8597c478bd9Sstevel@tonic-gate 8607c478bd9Sstevel@tonic-gate struct ps_prochandle * 8617c478bd9Sstevel@tonic-gate dt_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv) 8627c478bd9Sstevel@tonic-gate { 8637c478bd9Sstevel@tonic-gate dt_proc_hash_t *dph = dtp->dt_procs; 8647c478bd9Sstevel@tonic-gate dt_proc_t *dpr; 8657c478bd9Sstevel@tonic-gate int err; 8667c478bd9Sstevel@tonic-gate 8677c478bd9Sstevel@tonic-gate if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL) 8687c478bd9Sstevel@tonic-gate return (NULL); /* errno is set for us */ 8697c478bd9Sstevel@tonic-gate 8707c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&dpr->dpr_lock, NULL); 8717c478bd9Sstevel@tonic-gate (void) pthread_cond_init(&dpr->dpr_cv, NULL); 8727c478bd9Sstevel@tonic-gate 8737c478bd9Sstevel@tonic-gate if ((dpr->dpr_proc = Pcreate(file, argv, &err, NULL, 0)) == NULL) { 8747c478bd9Sstevel@tonic-gate return (dt_proc_error(dtp, dpr, 8757c478bd9Sstevel@tonic-gate "failed to execute %s: %s\n", file, Pcreate_error(err))); 8767c478bd9Sstevel@tonic-gate } 8777c478bd9Sstevel@tonic-gate 8787c478bd9Sstevel@tonic-gate dpr->dpr_hdl = dtp; 8797c478bd9Sstevel@tonic-gate dpr->dpr_pid = Pstatus(dpr->dpr_proc)->pr_pid; 8807c478bd9Sstevel@tonic-gate 8817c478bd9Sstevel@tonic-gate (void) Punsetflags(dpr->dpr_proc, PR_RLC); 8827c478bd9Sstevel@tonic-gate (void) Psetflags(dpr->dpr_proc, PR_KLC); 8837c478bd9Sstevel@tonic-gate 8847c478bd9Sstevel@tonic-gate if (dt_proc_create_thread(dtp, dpr, dtp->dt_prcmode) != 0) 8857c478bd9Sstevel@tonic-gate return (NULL); /* dt_proc_error() has been called for us */ 8867c478bd9Sstevel@tonic-gate 8877c478bd9Sstevel@tonic-gate dpr->dpr_hash = dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)]; 8887c478bd9Sstevel@tonic-gate dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)] = dpr; 8897c478bd9Sstevel@tonic-gate dt_list_prepend(&dph->dph_lrulist, dpr); 8907c478bd9Sstevel@tonic-gate 8917c478bd9Sstevel@tonic-gate dt_dprintf("created pid %d\n", (int)dpr->dpr_pid); 8927c478bd9Sstevel@tonic-gate dpr->dpr_refs++; 8937c478bd9Sstevel@tonic-gate 8947c478bd9Sstevel@tonic-gate return (dpr->dpr_proc); 8957c478bd9Sstevel@tonic-gate } 8967c478bd9Sstevel@tonic-gate 8977c478bd9Sstevel@tonic-gate struct ps_prochandle * 8987c478bd9Sstevel@tonic-gate dt_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags, int nomonitor) 8997c478bd9Sstevel@tonic-gate { 9007c478bd9Sstevel@tonic-gate dt_proc_hash_t *dph = dtp->dt_procs; 9017c478bd9Sstevel@tonic-gate uint_t h = pid & (dph->dph_hashlen - 1); 9027c478bd9Sstevel@tonic-gate dt_proc_t *dpr, *opr; 9037c478bd9Sstevel@tonic-gate int err; 9047c478bd9Sstevel@tonic-gate 9057c478bd9Sstevel@tonic-gate /* 9067c478bd9Sstevel@tonic-gate * Search the hash table for the pid. If it is already grabbed or 9077c478bd9Sstevel@tonic-gate * created, move the handle to the front of the lrulist, increment 9087c478bd9Sstevel@tonic-gate * the reference count, and return the existing ps_prochandle. 9097c478bd9Sstevel@tonic-gate */ 9107c478bd9Sstevel@tonic-gate for (dpr = dph->dph_hash[h]; dpr != NULL; dpr = dpr->dpr_hash) { 9117c478bd9Sstevel@tonic-gate if (dpr->dpr_pid == pid && !dpr->dpr_stale) { 9127c478bd9Sstevel@tonic-gate /* 9137c478bd9Sstevel@tonic-gate * If the cached handle was opened read-only and 9147c478bd9Sstevel@tonic-gate * this request is for a writeable handle, mark 9157c478bd9Sstevel@tonic-gate * the cached handle as stale and open a new handle. 9167c478bd9Sstevel@tonic-gate * Since it's stale, unmark it as cacheable. 9177c478bd9Sstevel@tonic-gate */ 9187c478bd9Sstevel@tonic-gate if (dpr->dpr_rdonly && !(flags & PGRAB_RDONLY)) { 9197c478bd9Sstevel@tonic-gate dt_dprintf("upgrading pid %d\n", (int)pid); 9207c478bd9Sstevel@tonic-gate dpr->dpr_stale = B_TRUE; 9217c478bd9Sstevel@tonic-gate dpr->dpr_cacheable = B_FALSE; 9227c478bd9Sstevel@tonic-gate dph->dph_lrucnt--; 9237c478bd9Sstevel@tonic-gate break; 9247c478bd9Sstevel@tonic-gate } 9257c478bd9Sstevel@tonic-gate 9267c478bd9Sstevel@tonic-gate dt_dprintf("grabbed pid %d (cached)\n", (int)pid); 9277c478bd9Sstevel@tonic-gate dt_list_delete(&dph->dph_lrulist, dpr); 9287c478bd9Sstevel@tonic-gate dt_list_prepend(&dph->dph_lrulist, dpr); 9297c478bd9Sstevel@tonic-gate dpr->dpr_refs++; 9307c478bd9Sstevel@tonic-gate return (dpr->dpr_proc); 9317c478bd9Sstevel@tonic-gate } 9327c478bd9Sstevel@tonic-gate } 9337c478bd9Sstevel@tonic-gate 9347c478bd9Sstevel@tonic-gate if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL) 9357c478bd9Sstevel@tonic-gate return (NULL); /* errno is set for us */ 9367c478bd9Sstevel@tonic-gate 9377c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&dpr->dpr_lock, NULL); 9387c478bd9Sstevel@tonic-gate (void) pthread_cond_init(&dpr->dpr_cv, NULL); 9397c478bd9Sstevel@tonic-gate 9407c478bd9Sstevel@tonic-gate if ((dpr->dpr_proc = Pgrab(pid, flags, &err)) == NULL) { 9417c478bd9Sstevel@tonic-gate return (dt_proc_error(dtp, dpr, 9427c478bd9Sstevel@tonic-gate "failed to grab pid %d: %s\n", (int)pid, Pgrab_error(err))); 9437c478bd9Sstevel@tonic-gate } 9447c478bd9Sstevel@tonic-gate 9457c478bd9Sstevel@tonic-gate dpr->dpr_hdl = dtp; 9467c478bd9Sstevel@tonic-gate dpr->dpr_pid = pid; 9477c478bd9Sstevel@tonic-gate 9487c478bd9Sstevel@tonic-gate (void) Punsetflags(dpr->dpr_proc, PR_KLC); 9497c478bd9Sstevel@tonic-gate (void) Psetflags(dpr->dpr_proc, PR_RLC); 9507c478bd9Sstevel@tonic-gate 9517c478bd9Sstevel@tonic-gate /* 9527c478bd9Sstevel@tonic-gate * If we are attempting to grab the process without a monitor 9537c478bd9Sstevel@tonic-gate * thread, then mark the process cacheable only if it's being 9547c478bd9Sstevel@tonic-gate * grabbed read-only. If we're currently caching more process 9557c478bd9Sstevel@tonic-gate * handles than dph_lrulim permits, attempt to find the 9567c478bd9Sstevel@tonic-gate * least-recently-used handle that is currently unreferenced and 9577c478bd9Sstevel@tonic-gate * release it from the cache. Otherwise we are grabbing the process 9587c478bd9Sstevel@tonic-gate * for control: create a control thread for this process and store 9597c478bd9Sstevel@tonic-gate * its ID in dpr->dpr_tid. 9607c478bd9Sstevel@tonic-gate */ 9617c478bd9Sstevel@tonic-gate if (nomonitor || (flags & PGRAB_RDONLY)) { 9627c478bd9Sstevel@tonic-gate if (dph->dph_lrucnt >= dph->dph_lrulim) { 9637c478bd9Sstevel@tonic-gate for (opr = dt_list_prev(&dph->dph_lrulist); 9647c478bd9Sstevel@tonic-gate opr != NULL; opr = dt_list_prev(opr)) { 9657c478bd9Sstevel@tonic-gate if (opr->dpr_cacheable && opr->dpr_refs == 0) { 9667c478bd9Sstevel@tonic-gate dt_proc_destroy(dtp, opr->dpr_proc); 9677c478bd9Sstevel@tonic-gate break; 9687c478bd9Sstevel@tonic-gate } 9697c478bd9Sstevel@tonic-gate } 9707c478bd9Sstevel@tonic-gate } 9717c478bd9Sstevel@tonic-gate 9727c478bd9Sstevel@tonic-gate if (flags & PGRAB_RDONLY) { 9737c478bd9Sstevel@tonic-gate dpr->dpr_cacheable = B_TRUE; 9747c478bd9Sstevel@tonic-gate dpr->dpr_rdonly = B_TRUE; 9757c478bd9Sstevel@tonic-gate dph->dph_lrucnt++; 9767c478bd9Sstevel@tonic-gate } 9777c478bd9Sstevel@tonic-gate 9787c478bd9Sstevel@tonic-gate } else if (dt_proc_create_thread(dtp, dpr, DT_PROC_STOP_GRAB) != 0) 9797c478bd9Sstevel@tonic-gate return (NULL); /* dt_proc_error() has been called for us */ 9807c478bd9Sstevel@tonic-gate 9817c478bd9Sstevel@tonic-gate dpr->dpr_hash = dph->dph_hash[h]; 9827c478bd9Sstevel@tonic-gate dph->dph_hash[h] = dpr; 9837c478bd9Sstevel@tonic-gate dt_list_prepend(&dph->dph_lrulist, dpr); 9847c478bd9Sstevel@tonic-gate 9857c478bd9Sstevel@tonic-gate dt_dprintf("grabbed pid %d\n", (int)pid); 9867c478bd9Sstevel@tonic-gate dpr->dpr_refs++; 9877c478bd9Sstevel@tonic-gate 9887c478bd9Sstevel@tonic-gate return (dpr->dpr_proc); 9897c478bd9Sstevel@tonic-gate } 9907c478bd9Sstevel@tonic-gate 9917c478bd9Sstevel@tonic-gate void 9927c478bd9Sstevel@tonic-gate dt_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P) 9937c478bd9Sstevel@tonic-gate { 9947c478bd9Sstevel@tonic-gate dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE); 9957c478bd9Sstevel@tonic-gate dt_proc_hash_t *dph = dtp->dt_procs; 9967c478bd9Sstevel@tonic-gate 9977c478bd9Sstevel@tonic-gate assert(dpr != NULL); 9987c478bd9Sstevel@tonic-gate assert(dpr->dpr_refs != 0); 9997c478bd9Sstevel@tonic-gate 10007c478bd9Sstevel@tonic-gate if (--dpr->dpr_refs == 0 && 10017c478bd9Sstevel@tonic-gate (!dpr->dpr_cacheable || dph->dph_lrucnt > dph->dph_lrulim)) 10027c478bd9Sstevel@tonic-gate dt_proc_destroy(dtp, P); 10037c478bd9Sstevel@tonic-gate } 10047c478bd9Sstevel@tonic-gate 10057c478bd9Sstevel@tonic-gate void 10067c478bd9Sstevel@tonic-gate dt_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P) 10077c478bd9Sstevel@tonic-gate { 10087c478bd9Sstevel@tonic-gate dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE); 10097c478bd9Sstevel@tonic-gate 10107c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&dpr->dpr_lock); 10117c478bd9Sstevel@tonic-gate 10127c478bd9Sstevel@tonic-gate if (dpr->dpr_stop & DT_PROC_STOP_IDLE) { 10137c478bd9Sstevel@tonic-gate dpr->dpr_stop &= ~DT_PROC_STOP_IDLE; 10147c478bd9Sstevel@tonic-gate (void) pthread_cond_broadcast(&dpr->dpr_cv); 10157c478bd9Sstevel@tonic-gate } 10167c478bd9Sstevel@tonic-gate 10177c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 10187c478bd9Sstevel@tonic-gate } 10197c478bd9Sstevel@tonic-gate 10207c478bd9Sstevel@tonic-gate void 10217c478bd9Sstevel@tonic-gate dt_proc_lock(dtrace_hdl_t *dtp, struct ps_prochandle *P) 10227c478bd9Sstevel@tonic-gate { 10237c478bd9Sstevel@tonic-gate dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE); 10247c478bd9Sstevel@tonic-gate int err = pthread_mutex_lock(&dpr->dpr_lock); 10257c478bd9Sstevel@tonic-gate assert(err == 0); /* check for recursion */ 10267c478bd9Sstevel@tonic-gate } 10277c478bd9Sstevel@tonic-gate 10287c478bd9Sstevel@tonic-gate void 10297c478bd9Sstevel@tonic-gate dt_proc_unlock(dtrace_hdl_t *dtp, struct ps_prochandle *P) 10307c478bd9Sstevel@tonic-gate { 10317c478bd9Sstevel@tonic-gate dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE); 10327c478bd9Sstevel@tonic-gate int err = pthread_mutex_unlock(&dpr->dpr_lock); 10337c478bd9Sstevel@tonic-gate assert(err == 0); /* check for unheld lock */ 10347c478bd9Sstevel@tonic-gate } 10357c478bd9Sstevel@tonic-gate 10367c478bd9Sstevel@tonic-gate void 10377c478bd9Sstevel@tonic-gate dt_proc_hash_create(dtrace_hdl_t *dtp) 10387c478bd9Sstevel@tonic-gate { 10397c478bd9Sstevel@tonic-gate if ((dtp->dt_procs = dt_zalloc(dtp, sizeof (dt_proc_hash_t) + 10407c478bd9Sstevel@tonic-gate sizeof (dt_proc_t *) * _dtrace_pidbuckets - 1)) != NULL) { 10417c478bd9Sstevel@tonic-gate 10427c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&dtp->dt_procs->dph_lock, NULL); 10437c478bd9Sstevel@tonic-gate (void) pthread_cond_init(&dtp->dt_procs->dph_cv, NULL); 10447c478bd9Sstevel@tonic-gate 10457c478bd9Sstevel@tonic-gate dtp->dt_procs->dph_hashlen = _dtrace_pidbuckets; 10467c478bd9Sstevel@tonic-gate dtp->dt_procs->dph_lrulim = _dtrace_pidlrulim; 10477c478bd9Sstevel@tonic-gate } 10487c478bd9Sstevel@tonic-gate } 10497c478bd9Sstevel@tonic-gate 10507c478bd9Sstevel@tonic-gate void 10517c478bd9Sstevel@tonic-gate dt_proc_hash_destroy(dtrace_hdl_t *dtp) 10527c478bd9Sstevel@tonic-gate { 10537c478bd9Sstevel@tonic-gate dt_proc_hash_t *dph = dtp->dt_procs; 10547c478bd9Sstevel@tonic-gate dt_proc_t *dpr; 10557c478bd9Sstevel@tonic-gate 10567c478bd9Sstevel@tonic-gate while ((dpr = dt_list_next(&dph->dph_lrulist)) != NULL) 10577c478bd9Sstevel@tonic-gate dt_proc_destroy(dtp, dpr->dpr_proc); 10587c478bd9Sstevel@tonic-gate 10597c478bd9Sstevel@tonic-gate dtp->dt_procs = NULL; 10607c478bd9Sstevel@tonic-gate dt_free(dtp, dph); 10617c478bd9Sstevel@tonic-gate } 10627c478bd9Sstevel@tonic-gate 10637c478bd9Sstevel@tonic-gate struct ps_prochandle * 10647c478bd9Sstevel@tonic-gate dtrace_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv) 10657c478bd9Sstevel@tonic-gate { 10667c478bd9Sstevel@tonic-gate dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target"); 10677c478bd9Sstevel@tonic-gate struct ps_prochandle *P = dt_proc_create(dtp, file, argv); 10687c478bd9Sstevel@tonic-gate 10697c478bd9Sstevel@tonic-gate if (P != NULL && idp != NULL && idp->di_id == 0) 10707c478bd9Sstevel@tonic-gate idp->di_id = Pstatus(P)->pr_pid; /* $target = created pid */ 10717c478bd9Sstevel@tonic-gate 10727c478bd9Sstevel@tonic-gate return (P); 10737c478bd9Sstevel@tonic-gate } 10747c478bd9Sstevel@tonic-gate 10757c478bd9Sstevel@tonic-gate struct ps_prochandle * 10767c478bd9Sstevel@tonic-gate dtrace_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags) 10777c478bd9Sstevel@tonic-gate { 10787c478bd9Sstevel@tonic-gate dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target"); 10797c478bd9Sstevel@tonic-gate struct ps_prochandle *P = dt_proc_grab(dtp, pid, flags, 0); 10807c478bd9Sstevel@tonic-gate 10817c478bd9Sstevel@tonic-gate if (P != NULL && idp != NULL && idp->di_id == 0) 10827c478bd9Sstevel@tonic-gate idp->di_id = pid; /* $target = grabbed pid */ 10837c478bd9Sstevel@tonic-gate 10847c478bd9Sstevel@tonic-gate return (P); 10857c478bd9Sstevel@tonic-gate } 10867c478bd9Sstevel@tonic-gate 10877c478bd9Sstevel@tonic-gate void 10887c478bd9Sstevel@tonic-gate dtrace_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P) 10897c478bd9Sstevel@tonic-gate { 10907c478bd9Sstevel@tonic-gate dt_proc_release(dtp, P); 10917c478bd9Sstevel@tonic-gate } 10927c478bd9Sstevel@tonic-gate 10937c478bd9Sstevel@tonic-gate void 10947c478bd9Sstevel@tonic-gate dtrace_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P) 10957c478bd9Sstevel@tonic-gate { 10967c478bd9Sstevel@tonic-gate dt_proc_continue(dtp, P); 10977c478bd9Sstevel@tonic-gate } 1098