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