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