Deleted Added
full compact
dt_proc.c (210775) dt_proc.c (211554)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27/*
28 * DTrace Process Control
29 *
30 * This file provides a set of routines that permit libdtrace and its clients
31 * to create and grab process handles using libproc, and to share these handles
32 * between library mechanisms that need libproc access, such as ustack(), and
33 * client mechanisms that need libproc access, such as dtrace(1M) -c and -p.
34 * The library provides several mechanisms in the libproc control layer:
35 *
36 * Reference Counting: The library code and client code can independently grab
37 * the same process handles without interfering with one another. Only when
38 * the reference count drops to zero and the handle is not being cached (see
39 * below for more information on caching) will Prelease() be called on it.
40 *
41 * Handle Caching: If a handle is grabbed PGRAB_RDONLY (e.g. by ustack()) and
42 * the reference count drops to zero, the handle is not immediately released.
43 * Instead, libproc handles are maintained on dph_lrulist in order from most-
44 * recently accessed to least-recently accessed. Idle handles are maintained
45 * until a pre-defined LRU cache limit is exceeded, permitting repeated calls
46 * to ustack() to avoid the overhead of releasing and re-grabbing processes.
47 *
48 * Process Control: For processes that are grabbed for control (~PGRAB_RDONLY)
49 * or created by dt_proc_create(), a control thread is created to provide
50 * callbacks on process exit and symbol table caching on dlopen()s.
51 *
52 * MT-Safety: Libproc is not MT-Safe, so dt_proc_lock() and dt_proc_unlock()
53 * are provided to synchronize access to the libproc handle between libdtrace
54 * code and client code and the control thread's use of the ps_prochandle.
55 *
56 * NOTE: MT-Safety is NOT provided for libdtrace itself, or for use of the
57 * dtrace_proc_grab/dtrace_proc_create mechanisms. Like all exported libdtrace
58 * calls, these are assumed to be MT-Unsafe. MT-Safety is ONLY provided for
59 * synchronization between libdtrace control threads and the client thread.
60 *
61 * The ps_prochandles themselves are maintained along with a dt_proc_t struct
62 * in a hash table indexed by PID. This provides basic locking and reference
63 * counting. The dt_proc_t is also maintained in LRU order on dph_lrulist.
64 * The dph_lrucnt and dph_lrulim count the number of cacheable processes and
65 * the current limit on the number of actively cached entries.
66 *
67 * The control thread for a process establishes breakpoints at the rtld_db
68 * locations of interest, updates mappings and symbol tables at these points,
69 * and handles exec and fork (by always following the parent). The control
70 * thread automatically exits when the process dies or control is lost.
71 *
72 * A simple notification mechanism is provided for libdtrace clients using
73 * dtrace_handle_proc() for notification of PS_UNDEAD or PS_LOST events. If
74 * such an event occurs, the dt_proc_t itself is enqueued on a notification
75 * list and the control thread broadcasts to dph_cv. dtrace_sleep() will wake
76 * up using this condition and will then call the client handler as necessary.
77 */
78
79#include <sys/wait.h>
80#if defined(sun)
81#include <sys/lwp.h>
82#endif
83#include <strings.h>
84#include <signal.h>
85#include <assert.h>
86#include <errno.h>
87
88#include <dt_proc.h>
89#include <dt_pid.h>
90#include <dt_impl.h>
91
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27/*
28 * DTrace Process Control
29 *
30 * This file provides a set of routines that permit libdtrace and its clients
31 * to create and grab process handles using libproc, and to share these handles
32 * between library mechanisms that need libproc access, such as ustack(), and
33 * client mechanisms that need libproc access, such as dtrace(1M) -c and -p.
34 * The library provides several mechanisms in the libproc control layer:
35 *
36 * Reference Counting: The library code and client code can independently grab
37 * the same process handles without interfering with one another. Only when
38 * the reference count drops to zero and the handle is not being cached (see
39 * below for more information on caching) will Prelease() be called on it.
40 *
41 * Handle Caching: If a handle is grabbed PGRAB_RDONLY (e.g. by ustack()) and
42 * the reference count drops to zero, the handle is not immediately released.
43 * Instead, libproc handles are maintained on dph_lrulist in order from most-
44 * recently accessed to least-recently accessed. Idle handles are maintained
45 * until a pre-defined LRU cache limit is exceeded, permitting repeated calls
46 * to ustack() to avoid the overhead of releasing and re-grabbing processes.
47 *
48 * Process Control: For processes that are grabbed for control (~PGRAB_RDONLY)
49 * or created by dt_proc_create(), a control thread is created to provide
50 * callbacks on process exit and symbol table caching on dlopen()s.
51 *
52 * MT-Safety: Libproc is not MT-Safe, so dt_proc_lock() and dt_proc_unlock()
53 * are provided to synchronize access to the libproc handle between libdtrace
54 * code and client code and the control thread's use of the ps_prochandle.
55 *
56 * NOTE: MT-Safety is NOT provided for libdtrace itself, or for use of the
57 * dtrace_proc_grab/dtrace_proc_create mechanisms. Like all exported libdtrace
58 * calls, these are assumed to be MT-Unsafe. MT-Safety is ONLY provided for
59 * synchronization between libdtrace control threads and the client thread.
60 *
61 * The ps_prochandles themselves are maintained along with a dt_proc_t struct
62 * in a hash table indexed by PID. This provides basic locking and reference
63 * counting. The dt_proc_t is also maintained in LRU order on dph_lrulist.
64 * The dph_lrucnt and dph_lrulim count the number of cacheable processes and
65 * the current limit on the number of actively cached entries.
66 *
67 * The control thread for a process establishes breakpoints at the rtld_db
68 * locations of interest, updates mappings and symbol tables at these points,
69 * and handles exec and fork (by always following the parent). The control
70 * thread automatically exits when the process dies or control is lost.
71 *
72 * A simple notification mechanism is provided for libdtrace clients using
73 * dtrace_handle_proc() for notification of PS_UNDEAD or PS_LOST events. If
74 * such an event occurs, the dt_proc_t itself is enqueued on a notification
75 * list and the control thread broadcasts to dph_cv. dtrace_sleep() will wake
76 * up using this condition and will then call the client handler as necessary.
77 */
78
79#include <sys/wait.h>
80#if defined(sun)
81#include <sys/lwp.h>
82#endif
83#include <strings.h>
84#include <signal.h>
85#include <assert.h>
86#include <errno.h>
87
88#include <dt_proc.h>
89#include <dt_pid.h>
90#include <dt_impl.h>
91
92#if !defined(sun)
93#include <sys/syscall.h>
94#include <libproc_compat.h>
95#define SYS_forksys SYS_fork
96#endif
97
92#define IS_SYS_EXEC(w) (w == SYS_execve)
93#define IS_SYS_FORK(w) (w == SYS_vfork || w == SYS_forksys)
94
98#define IS_SYS_EXEC(w) (w == SYS_execve)
99#define IS_SYS_FORK(w) (w == SYS_vfork || w == SYS_forksys)
100
95#ifdef DOODAD
96static dt_bkpt_t *
97dt_proc_bpcreate(dt_proc_t *dpr, uintptr_t addr, dt_bkpt_f *func, void *data)
98{
99 struct ps_prochandle *P = dpr->dpr_proc;
100 dt_bkpt_t *dbp;
101
102 assert(DT_MUTEX_HELD(&dpr->dpr_lock));
103
104 if ((dbp = dt_zalloc(dpr->dpr_hdl, sizeof (dt_bkpt_t))) != NULL) {
105 dbp->dbp_func = func;
106 dbp->dbp_data = data;
107 dbp->dbp_addr = addr;
108
109 if (Psetbkpt(P, dbp->dbp_addr, &dbp->dbp_instr) == 0)
110 dbp->dbp_active = B_TRUE;
111
112 dt_list_append(&dpr->dpr_bps, dbp);
113 }
114
115 return (dbp);
116}
101static dt_bkpt_t *
102dt_proc_bpcreate(dt_proc_t *dpr, uintptr_t addr, dt_bkpt_f *func, void *data)
103{
104 struct ps_prochandle *P = dpr->dpr_proc;
105 dt_bkpt_t *dbp;
106
107 assert(DT_MUTEX_HELD(&dpr->dpr_lock));
108
109 if ((dbp = dt_zalloc(dpr->dpr_hdl, sizeof (dt_bkpt_t))) != NULL) {
110 dbp->dbp_func = func;
111 dbp->dbp_data = data;
112 dbp->dbp_addr = addr;
113
114 if (Psetbkpt(P, dbp->dbp_addr, &dbp->dbp_instr) == 0)
115 dbp->dbp_active = B_TRUE;
116
117 dt_list_append(&dpr->dpr_bps, dbp);
118 }
119
120 return (dbp);
121}
117#endif
118
119static void
120dt_proc_bpdestroy(dt_proc_t *dpr, int delbkpts)
121{
122
123static void
124dt_proc_bpdestroy(dt_proc_t *dpr, int delbkpts)
125{
122#if defined(sun)
123 int state = Pstate(dpr->dpr_proc);
126 int state = Pstate(dpr->dpr_proc);
124#else
125 int state = proc_state(dpr->dpr_proc);
126#endif
127 dt_bkpt_t *dbp, *nbp;
128
129 assert(DT_MUTEX_HELD(&dpr->dpr_lock));
130
131 for (dbp = dt_list_next(&dpr->dpr_bps); dbp != NULL; dbp = nbp) {
127 dt_bkpt_t *dbp, *nbp;
128
129 assert(DT_MUTEX_HELD(&dpr->dpr_lock));
130
131 for (dbp = dt_list_next(&dpr->dpr_bps); dbp != NULL; dbp = nbp) {
132printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
133#ifdef DOODAD
134 if (delbkpts && dbp->dbp_active &&
135 state != PS_LOST && state != PS_UNDEAD) {
136 (void) Pdelbkpt(dpr->dpr_proc,
137 dbp->dbp_addr, dbp->dbp_instr);
138 }
132 if (delbkpts && dbp->dbp_active &&
133 state != PS_LOST && state != PS_UNDEAD) {
134 (void) Pdelbkpt(dpr->dpr_proc,
135 dbp->dbp_addr, dbp->dbp_instr);
136 }
139#endif
140 nbp = dt_list_next(dbp);
141 dt_list_delete(&dpr->dpr_bps, dbp);
142 dt_free(dpr->dpr_hdl, dbp);
143 }
144}
145
137 nbp = dt_list_next(dbp);
138 dt_list_delete(&dpr->dpr_bps, dbp);
139 dt_free(dpr->dpr_hdl, dbp);
140 }
141}
142
146#ifdef DOODAD
147static void
148dt_proc_bpmatch(dtrace_hdl_t *dtp, dt_proc_t *dpr)
149{
143static void
144dt_proc_bpmatch(dtrace_hdl_t *dtp, dt_proc_t *dpr)
145{
146#if defined(sun)
150 const lwpstatus_t *psp = &Pstatus(dpr->dpr_proc)->pr_lwp;
147 const lwpstatus_t *psp = &Pstatus(dpr->dpr_proc)->pr_lwp;
148#else
149 unsigned long pc;
150#endif
151 dt_bkpt_t *dbp;
152
153 assert(DT_MUTEX_HELD(&dpr->dpr_lock));
154
151 dt_bkpt_t *dbp;
152
153 assert(DT_MUTEX_HELD(&dpr->dpr_lock));
154
155#if !defined(sun)
156 proc_regget(dpr->dpr_proc, REG_PC, &pc);
157 proc_bkptregadj(&pc);
158#endif
159
155 for (dbp = dt_list_next(&dpr->dpr_bps);
156 dbp != NULL; dbp = dt_list_next(dbp)) {
160 for (dbp = dt_list_next(&dpr->dpr_bps);
161 dbp != NULL; dbp = dt_list_next(dbp)) {
162#if defined(sun)
157 if (psp->pr_reg[R_PC] == dbp->dbp_addr)
158 break;
163 if (psp->pr_reg[R_PC] == dbp->dbp_addr)
164 break;
165#else
166 if (pc == dbp->dbp_addr)
167 break;
168#endif
159 }
160
161 if (dbp == NULL) {
162 dt_dprintf("pid %d: spurious breakpoint wakeup for %lx\n",
169 }
170
171 if (dbp == NULL) {
172 dt_dprintf("pid %d: spurious breakpoint wakeup for %lx\n",
173#if defined(sun)
163 (int)dpr->dpr_pid, (ulong_t)psp->pr_reg[R_PC]);
174 (int)dpr->dpr_pid, (ulong_t)psp->pr_reg[R_PC]);
175#else
176 (int)dpr->dpr_pid, pc);
177#endif
164 return;
165 }
166
167 dt_dprintf("pid %d: hit breakpoint at %lx (%lu)\n",
168 (int)dpr->dpr_pid, (ulong_t)dbp->dbp_addr, ++dbp->dbp_hits);
169
170 dbp->dbp_func(dtp, dpr, dbp->dbp_data);
171 (void) Pxecbkpt(dpr->dpr_proc, dbp->dbp_instr);
172}
178 return;
179 }
180
181 dt_dprintf("pid %d: hit breakpoint at %lx (%lu)\n",
182 (int)dpr->dpr_pid, (ulong_t)dbp->dbp_addr, ++dbp->dbp_hits);
183
184 dbp->dbp_func(dtp, dpr, dbp->dbp_data);
185 (void) Pxecbkpt(dpr->dpr_proc, dbp->dbp_instr);
186}
173#endif
174
175static void
176dt_proc_bpenable(dt_proc_t *dpr)
177{
178 dt_bkpt_t *dbp;
179
180 assert(DT_MUTEX_HELD(&dpr->dpr_lock));
181
182 for (dbp = dt_list_next(&dpr->dpr_bps);
183 dbp != NULL; dbp = dt_list_next(dbp)) {
187
188static void
189dt_proc_bpenable(dt_proc_t *dpr)
190{
191 dt_bkpt_t *dbp;
192
193 assert(DT_MUTEX_HELD(&dpr->dpr_lock));
194
195 for (dbp = dt_list_next(&dpr->dpr_bps);
196 dbp != NULL; dbp = dt_list_next(dbp)) {
184printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
185#ifdef DOODAD
186 if (!dbp->dbp_active && Psetbkpt(dpr->dpr_proc,
187 dbp->dbp_addr, &dbp->dbp_instr) == 0)
188 dbp->dbp_active = B_TRUE;
197 if (!dbp->dbp_active && Psetbkpt(dpr->dpr_proc,
198 dbp->dbp_addr, &dbp->dbp_instr) == 0)
199 dbp->dbp_active = B_TRUE;
189#endif
190 }
191
192 dt_dprintf("breakpoints enabled\n");
193}
194
195static void
196dt_proc_bpdisable(dt_proc_t *dpr)
197{
198 dt_bkpt_t *dbp;
199
200 assert(DT_MUTEX_HELD(&dpr->dpr_lock));
201
202 for (dbp = dt_list_next(&dpr->dpr_bps);
203 dbp != NULL; dbp = dt_list_next(dbp)) {
200 }
201
202 dt_dprintf("breakpoints enabled\n");
203}
204
205static void
206dt_proc_bpdisable(dt_proc_t *dpr)
207{
208 dt_bkpt_t *dbp;
209
210 assert(DT_MUTEX_HELD(&dpr->dpr_lock));
211
212 for (dbp = dt_list_next(&dpr->dpr_bps);
213 dbp != NULL; dbp = dt_list_next(dbp)) {
204printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
205#ifdef DOODAD
206 if (dbp->dbp_active && Pdelbkpt(dpr->dpr_proc,
207 dbp->dbp_addr, dbp->dbp_instr) == 0)
208 dbp->dbp_active = B_FALSE;
214 if (dbp->dbp_active && Pdelbkpt(dpr->dpr_proc,
215 dbp->dbp_addr, dbp->dbp_instr) == 0)
216 dbp->dbp_active = B_FALSE;
209#endif
210 }
211
212 dt_dprintf("breakpoints disabled\n");
213}
214
215static void
216dt_proc_notify(dtrace_hdl_t *dtp, dt_proc_hash_t *dph, dt_proc_t *dpr,
217 const char *msg)
218{
219 dt_proc_notify_t *dprn = dt_alloc(dtp, sizeof (dt_proc_notify_t));
220
221 if (dprn == NULL) {
222 dt_dprintf("failed to allocate notification for %d %s\n",
223 (int)dpr->dpr_pid, msg);
224 } else {
225 dprn->dprn_dpr = dpr;
226 if (msg == NULL)
227 dprn->dprn_errmsg[0] = '\0';
228 else
229 (void) strlcpy(dprn->dprn_errmsg, msg,
230 sizeof (dprn->dprn_errmsg));
231
232 (void) pthread_mutex_lock(&dph->dph_lock);
233
234 dprn->dprn_next = dph->dph_notify;
235 dph->dph_notify = dprn;
236
237 (void) pthread_cond_broadcast(&dph->dph_cv);
238 (void) pthread_mutex_unlock(&dph->dph_lock);
239 }
240}
241
242/*
243 * Check to see if the control thread was requested to stop when the victim
244 * process reached a particular event (why) rather than continuing the victim.
245 * If 'why' is set in the stop mask, we wait on dpr_cv for dt_proc_continue().
246 * If 'why' is not set, this function returns immediately and does nothing.
247 */
248static void
249dt_proc_stop(dt_proc_t *dpr, uint8_t why)
250{
251 assert(DT_MUTEX_HELD(&dpr->dpr_lock));
252 assert(why != DT_PROC_STOP_IDLE);
253
254 if (dpr->dpr_stop & why) {
255 dpr->dpr_stop |= DT_PROC_STOP_IDLE;
256 dpr->dpr_stop &= ~why;
257
258 (void) pthread_cond_broadcast(&dpr->dpr_cv);
259
260 /*
261 * We disable breakpoints while stopped to preserve the
262 * integrity of the program text for both our own disassembly
263 * and that of the kernel.
264 */
265 dt_proc_bpdisable(dpr);
266
267 while (dpr->dpr_stop & DT_PROC_STOP_IDLE)
268 (void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);
269
270 dt_proc_bpenable(dpr);
271 }
272}
273
274/*ARGSUSED*/
275static void
276dt_proc_bpmain(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *fname)
277{
278 dt_dprintf("pid %d: breakpoint at %s()\n", (int)dpr->dpr_pid, fname);
279 dt_proc_stop(dpr, DT_PROC_STOP_MAIN);
280}
281
217 }
218
219 dt_dprintf("breakpoints disabled\n");
220}
221
222static void
223dt_proc_notify(dtrace_hdl_t *dtp, dt_proc_hash_t *dph, dt_proc_t *dpr,
224 const char *msg)
225{
226 dt_proc_notify_t *dprn = dt_alloc(dtp, sizeof (dt_proc_notify_t));
227
228 if (dprn == NULL) {
229 dt_dprintf("failed to allocate notification for %d %s\n",
230 (int)dpr->dpr_pid, msg);
231 } else {
232 dprn->dprn_dpr = dpr;
233 if (msg == NULL)
234 dprn->dprn_errmsg[0] = '\0';
235 else
236 (void) strlcpy(dprn->dprn_errmsg, msg,
237 sizeof (dprn->dprn_errmsg));
238
239 (void) pthread_mutex_lock(&dph->dph_lock);
240
241 dprn->dprn_next = dph->dph_notify;
242 dph->dph_notify = dprn;
243
244 (void) pthread_cond_broadcast(&dph->dph_cv);
245 (void) pthread_mutex_unlock(&dph->dph_lock);
246 }
247}
248
249/*
250 * Check to see if the control thread was requested to stop when the victim
251 * process reached a particular event (why) rather than continuing the victim.
252 * If 'why' is set in the stop mask, we wait on dpr_cv for dt_proc_continue().
253 * If 'why' is not set, this function returns immediately and does nothing.
254 */
255static void
256dt_proc_stop(dt_proc_t *dpr, uint8_t why)
257{
258 assert(DT_MUTEX_HELD(&dpr->dpr_lock));
259 assert(why != DT_PROC_STOP_IDLE);
260
261 if (dpr->dpr_stop & why) {
262 dpr->dpr_stop |= DT_PROC_STOP_IDLE;
263 dpr->dpr_stop &= ~why;
264
265 (void) pthread_cond_broadcast(&dpr->dpr_cv);
266
267 /*
268 * We disable breakpoints while stopped to preserve the
269 * integrity of the program text for both our own disassembly
270 * and that of the kernel.
271 */
272 dt_proc_bpdisable(dpr);
273
274 while (dpr->dpr_stop & DT_PROC_STOP_IDLE)
275 (void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);
276
277 dt_proc_bpenable(dpr);
278 }
279}
280
281/*ARGSUSED*/
282static void
283dt_proc_bpmain(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *fname)
284{
285 dt_dprintf("pid %d: breakpoint at %s()\n", (int)dpr->dpr_pid, fname);
286 dt_proc_stop(dpr, DT_PROC_STOP_MAIN);
287}
288
282#if defined(sun)
283static void
284dt_proc_rdevent(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *evname)
285{
286 rd_event_msg_t rdm;
287 rd_err_e err;
288
289 if ((err = rd_event_getmsg(dpr->dpr_rtld, &rdm)) != RD_OK) {
290 dt_dprintf("pid %d: failed to get %s event message: %s\n",
291 (int)dpr->dpr_pid, evname, rd_errstr(err));
292 return;
293 }
294
295 dt_dprintf("pid %d: rtld event %s type=%d state %d\n",
296 (int)dpr->dpr_pid, evname, rdm.type, rdm.u.state);
297
298 switch (rdm.type) {
299 case RD_DLACTIVITY:
300 if (rdm.u.state != RD_CONSISTENT)
301 break;
302
303 Pupdate_syms(dpr->dpr_proc);
304 if (dt_pid_create_probes_module(dtp, dpr) != 0)
305 dt_proc_notify(dtp, dtp->dt_procs, dpr,
306 dpr->dpr_errmsg);
307
308 break;
309 case RD_PREINIT:
310 Pupdate_syms(dpr->dpr_proc);
311 dt_proc_stop(dpr, DT_PROC_STOP_PREINIT);
312 break;
313 case RD_POSTINIT:
314 Pupdate_syms(dpr->dpr_proc);
315 dt_proc_stop(dpr, DT_PROC_STOP_POSTINIT);
316 break;
317 }
318}
319
320static void
321dt_proc_rdwatch(dt_proc_t *dpr, rd_event_e event, const char *evname)
322{
323 rd_notify_t rdn;
324 rd_err_e err;
325
326 if ((err = rd_event_addr(dpr->dpr_rtld, event, &rdn)) != RD_OK) {
327 dt_dprintf("pid %d: failed to get event address for %s: %s\n",
328 (int)dpr->dpr_pid, evname, rd_errstr(err));
329 return;
330 }
331
332 if (rdn.type != RD_NOTIFY_BPT) {
333 dt_dprintf("pid %d: event %s has unexpected type %d\n",
334 (int)dpr->dpr_pid, evname, rdn.type);
335 return;
336 }
337
338 (void) dt_proc_bpcreate(dpr, rdn.u.bptaddr,
289static void
290dt_proc_rdevent(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *evname)
291{
292 rd_event_msg_t rdm;
293 rd_err_e err;
294
295 if ((err = rd_event_getmsg(dpr->dpr_rtld, &rdm)) != RD_OK) {
296 dt_dprintf("pid %d: failed to get %s event message: %s\n",
297 (int)dpr->dpr_pid, evname, rd_errstr(err));
298 return;
299 }
300
301 dt_dprintf("pid %d: rtld event %s type=%d state %d\n",
302 (int)dpr->dpr_pid, evname, rdm.type, rdm.u.state);
303
304 switch (rdm.type) {
305 case RD_DLACTIVITY:
306 if (rdm.u.state != RD_CONSISTENT)
307 break;
308
309 Pupdate_syms(dpr->dpr_proc);
310 if (dt_pid_create_probes_module(dtp, dpr) != 0)
311 dt_proc_notify(dtp, dtp->dt_procs, dpr,
312 dpr->dpr_errmsg);
313
314 break;
315 case RD_PREINIT:
316 Pupdate_syms(dpr->dpr_proc);
317 dt_proc_stop(dpr, DT_PROC_STOP_PREINIT);
318 break;
319 case RD_POSTINIT:
320 Pupdate_syms(dpr->dpr_proc);
321 dt_proc_stop(dpr, DT_PROC_STOP_POSTINIT);
322 break;
323 }
324}
325
326static void
327dt_proc_rdwatch(dt_proc_t *dpr, rd_event_e event, const char *evname)
328{
329 rd_notify_t rdn;
330 rd_err_e err;
331
332 if ((err = rd_event_addr(dpr->dpr_rtld, event, &rdn)) != RD_OK) {
333 dt_dprintf("pid %d: failed to get event address for %s: %s\n",
334 (int)dpr->dpr_pid, evname, rd_errstr(err));
335 return;
336 }
337
338 if (rdn.type != RD_NOTIFY_BPT) {
339 dt_dprintf("pid %d: event %s has unexpected type %d\n",
340 (int)dpr->dpr_pid, evname, rdn.type);
341 return;
342 }
343
344 (void) dt_proc_bpcreate(dpr, rdn.u.bptaddr,
345#if defined(sun)
339 (dt_bkpt_f *)dt_proc_rdevent, (void *)evname);
346 (dt_bkpt_f *)dt_proc_rdevent, (void *)evname);
347#else
348 /* XXX ugly */
349 (dt_bkpt_f *)dt_proc_rdevent, __DECONST(void *, evname));
350#endif
340}
341
342/*
343 * Common code for enabling events associated with the run-time linker after
344 * attaching to a process or after a victim process completes an exec(2).
345 */
346static void
347dt_proc_attach(dt_proc_t *dpr, int exec)
348{
351}
352
353/*
354 * Common code for enabling events associated with the run-time linker after
355 * attaching to a process or after a victim process completes an exec(2).
356 */
357static void
358dt_proc_attach(dt_proc_t *dpr, int exec)
359{
360#if defined(sun)
349 const pstatus_t *psp = Pstatus(dpr->dpr_proc);
361 const pstatus_t *psp = Pstatus(dpr->dpr_proc);
362#endif
350 rd_err_e err;
351 GElf_Sym sym;
352
353 assert(DT_MUTEX_HELD(&dpr->dpr_lock));
354
355 if (exec) {
363 rd_err_e err;
364 GElf_Sym sym;
365
366 assert(DT_MUTEX_HELD(&dpr->dpr_lock));
367
368 if (exec) {
369#if defined(sun)
356 if (psp->pr_lwp.pr_errno != 0)
357 return; /* exec failed: nothing needs to be done */
370 if (psp->pr_lwp.pr_errno != 0)
371 return; /* exec failed: nothing needs to be done */
372#endif
358
359 dt_proc_bpdestroy(dpr, B_FALSE);
373
374 dt_proc_bpdestroy(dpr, B_FALSE);
375#if defined(sun)
360 Preset_maps(dpr->dpr_proc);
376 Preset_maps(dpr->dpr_proc);
377#endif
361 }
378 }
362
363 if ((dpr->dpr_rtld = Prd_agent(dpr->dpr_proc)) != NULL &&
364 (err = rd_event_enable(dpr->dpr_rtld, B_TRUE)) == RD_OK) {
379 if ((dpr->dpr_rtld = Prd_agent(dpr->dpr_proc)) != NULL &&
380 (err = rd_event_enable(dpr->dpr_rtld, B_TRUE)) == RD_OK) {
381#if defined(sun)
365 dt_proc_rdwatch(dpr, RD_PREINIT, "RD_PREINIT");
382 dt_proc_rdwatch(dpr, RD_PREINIT, "RD_PREINIT");
383#endif
366 dt_proc_rdwatch(dpr, RD_POSTINIT, "RD_POSTINIT");
384 dt_proc_rdwatch(dpr, RD_POSTINIT, "RD_POSTINIT");
385#if defined(sun)
367 dt_proc_rdwatch(dpr, RD_DLACTIVITY, "RD_DLACTIVITY");
386 dt_proc_rdwatch(dpr, RD_DLACTIVITY, "RD_DLACTIVITY");
387#endif
368 } else {
369 dt_dprintf("pid %d: failed to enable rtld events: %s\n",
370 (int)dpr->dpr_pid, dpr->dpr_rtld ? rd_errstr(err) :
371 "rtld_db agent initialization failed");
372 }
373
374 Pupdate_maps(dpr->dpr_proc);
375
376 if (Pxlookup_by_name(dpr->dpr_proc, LM_ID_BASE,
377 "a.out", "main", &sym, NULL) == 0) {
378 (void) dt_proc_bpcreate(dpr, (uintptr_t)sym.st_value,
379 (dt_bkpt_f *)dt_proc_bpmain, "a.out`main");
380 } else {
381 dt_dprintf("pid %d: failed to find a.out`main: %s\n",
382 (int)dpr->dpr_pid, strerror(errno));
383 }
384}
385
386/*
387 * Wait for a stopped process to be set running again by some other debugger.
388 * This is typically not required by /proc-based debuggers, since the usual
389 * model is that one debugger controls one victim. But DTrace, as usual, has
390 * its own needs: the stop() action assumes that prun(1) or some other tool
391 * will be applied to resume the victim process. This could be solved by
392 * adding a PCWRUN directive to /proc, but that seems like overkill unless
393 * other debuggers end up needing this functionality, so we implement a cheap
394 * equivalent to PCWRUN using the set of existing kernel mechanisms.
395 *
396 * Our intent is really not just to wait for the victim to run, but rather to
397 * wait for it to run and then stop again for a reason other than the current
398 * PR_REQUESTED stop. Since PCWSTOP/Pstopstatus() can be applied repeatedly
399 * to a stopped process and will return the same result without affecting the
400 * victim, we can just perform these operations repeatedly until Pstate()
401 * changes, the representative LWP ID changes, or the stop timestamp advances.
402 * dt_proc_control() will then rediscover the new state and continue as usual.
403 * When the process is still stopped in the same exact state, we sleep for a
404 * brief interval before waiting again so as not to spin consuming CPU cycles.
405 */
406static void
407dt_proc_waitrun(dt_proc_t *dpr)
408{
388 } else {
389 dt_dprintf("pid %d: failed to enable rtld events: %s\n",
390 (int)dpr->dpr_pid, dpr->dpr_rtld ? rd_errstr(err) :
391 "rtld_db agent initialization failed");
392 }
393
394 Pupdate_maps(dpr->dpr_proc);
395
396 if (Pxlookup_by_name(dpr->dpr_proc, LM_ID_BASE,
397 "a.out", "main", &sym, NULL) == 0) {
398 (void) dt_proc_bpcreate(dpr, (uintptr_t)sym.st_value,
399 (dt_bkpt_f *)dt_proc_bpmain, "a.out`main");
400 } else {
401 dt_dprintf("pid %d: failed to find a.out`main: %s\n",
402 (int)dpr->dpr_pid, strerror(errno));
403 }
404}
405
406/*
407 * Wait for a stopped process to be set running again by some other debugger.
408 * This is typically not required by /proc-based debuggers, since the usual
409 * model is that one debugger controls one victim. But DTrace, as usual, has
410 * its own needs: the stop() action assumes that prun(1) or some other tool
411 * will be applied to resume the victim process. This could be solved by
412 * adding a PCWRUN directive to /proc, but that seems like overkill unless
413 * other debuggers end up needing this functionality, so we implement a cheap
414 * equivalent to PCWRUN using the set of existing kernel mechanisms.
415 *
416 * Our intent is really not just to wait for the victim to run, but rather to
417 * wait for it to run and then stop again for a reason other than the current
418 * PR_REQUESTED stop. Since PCWSTOP/Pstopstatus() can be applied repeatedly
419 * to a stopped process and will return the same result without affecting the
420 * victim, we can just perform these operations repeatedly until Pstate()
421 * changes, the representative LWP ID changes, or the stop timestamp advances.
422 * dt_proc_control() will then rediscover the new state and continue as usual.
423 * When the process is still stopped in the same exact state, we sleep for a
424 * brief interval before waiting again so as not to spin consuming CPU cycles.
425 */
426static void
427dt_proc_waitrun(dt_proc_t *dpr)
428{
429printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
430#ifdef DOODAD
409 struct ps_prochandle *P = dpr->dpr_proc;
410 const lwpstatus_t *psp = &Pstatus(P)->pr_lwp;
411
412 int krflag = psp->pr_flags & (PR_KLC | PR_RLC);
413 timestruc_t tstamp = psp->pr_tstamp;
414 lwpid_t lwpid = psp->pr_lwpid;
415
416 const long wstop = PCWSTOP;
417 int pfd = Pctlfd(P);
418
419 assert(DT_MUTEX_HELD(&dpr->dpr_lock));
420 assert(psp->pr_flags & PR_STOPPED);
421 assert(Pstate(P) == PS_STOP);
422
423 /*
424 * While we are waiting for the victim to run, clear PR_KLC and PR_RLC
425 * so that if the libdtrace client is killed, the victim stays stopped.
426 * dt_proc_destroy() will also observe this and perform PRELEASE_HANG.
427 */
428 (void) Punsetflags(P, krflag);
429 Psync(P);
430
431 (void) pthread_mutex_unlock(&dpr->dpr_lock);
432
433 while (!dpr->dpr_quit) {
434 if (write(pfd, &wstop, sizeof (wstop)) == -1 && errno == EINTR)
435 continue; /* check dpr_quit and continue waiting */
436
437 (void) pthread_mutex_lock(&dpr->dpr_lock);
438 (void) Pstopstatus(P, PCNULL, 0);
439 psp = &Pstatus(P)->pr_lwp;
440
441 /*
442 * If we've reached a new state, found a new representative, or
443 * the stop timestamp has changed, restore PR_KLC/PR_RLC to its
444 * original setting and then return with dpr_lock held.
445 */
446 if (Pstate(P) != PS_STOP || psp->pr_lwpid != lwpid ||
447 bcmp(&psp->pr_tstamp, &tstamp, sizeof (tstamp)) != 0) {
448 (void) Psetflags(P, krflag);
449 Psync(P);
450 return;
451 }
452
453 (void) pthread_mutex_unlock(&dpr->dpr_lock);
454 (void) poll(NULL, 0, MILLISEC / 2);
455 }
456
457 (void) pthread_mutex_lock(&dpr->dpr_lock);
431 struct ps_prochandle *P = dpr->dpr_proc;
432 const lwpstatus_t *psp = &Pstatus(P)->pr_lwp;
433
434 int krflag = psp->pr_flags & (PR_KLC | PR_RLC);
435 timestruc_t tstamp = psp->pr_tstamp;
436 lwpid_t lwpid = psp->pr_lwpid;
437
438 const long wstop = PCWSTOP;
439 int pfd = Pctlfd(P);
440
441 assert(DT_MUTEX_HELD(&dpr->dpr_lock));
442 assert(psp->pr_flags & PR_STOPPED);
443 assert(Pstate(P) == PS_STOP);
444
445 /*
446 * While we are waiting for the victim to run, clear PR_KLC and PR_RLC
447 * so that if the libdtrace client is killed, the victim stays stopped.
448 * dt_proc_destroy() will also observe this and perform PRELEASE_HANG.
449 */
450 (void) Punsetflags(P, krflag);
451 Psync(P);
452
453 (void) pthread_mutex_unlock(&dpr->dpr_lock);
454
455 while (!dpr->dpr_quit) {
456 if (write(pfd, &wstop, sizeof (wstop)) == -1 && errno == EINTR)
457 continue; /* check dpr_quit and continue waiting */
458
459 (void) pthread_mutex_lock(&dpr->dpr_lock);
460 (void) Pstopstatus(P, PCNULL, 0);
461 psp = &Pstatus(P)->pr_lwp;
462
463 /*
464 * If we've reached a new state, found a new representative, or
465 * the stop timestamp has changed, restore PR_KLC/PR_RLC to its
466 * original setting and then return with dpr_lock held.
467 */
468 if (Pstate(P) != PS_STOP || psp->pr_lwpid != lwpid ||
469 bcmp(&psp->pr_tstamp, &tstamp, sizeof (tstamp)) != 0) {
470 (void) Psetflags(P, krflag);
471 Psync(P);
472 return;
473 }
474
475 (void) pthread_mutex_unlock(&dpr->dpr_lock);
476 (void) poll(NULL, 0, MILLISEC / 2);
477 }
478
479 (void) pthread_mutex_lock(&dpr->dpr_lock);
458}
459#endif
480#endif
481}
460
461typedef struct dt_proc_control_data {
462 dtrace_hdl_t *dpcd_hdl; /* DTrace handle */
463 dt_proc_t *dpcd_proc; /* proccess to control */
464} dt_proc_control_data_t;
465
466/*
467 * Main loop for all victim process control threads. We initialize all the
468 * appropriate /proc control mechanisms, and then enter a loop waiting for
469 * the process to stop on an event or die. We process any events by calling
470 * appropriate subroutines, and exit when the victim dies or we lose control.
471 *
472 * The control thread synchronizes the use of dpr_proc with other libdtrace
473 * threads using dpr_lock. We hold the lock for all of our operations except
474 * waiting while the process is running: this is accomplished by writing a
475 * PCWSTOP directive directly to the underlying /proc/<pid>/ctl file. If the
476 * libdtrace client wishes to exit or abort our wait, SIGCANCEL can be used.
477 */
478static void *
479dt_proc_control(void *arg)
480{
481 dt_proc_control_data_t *datap = arg;
482 dtrace_hdl_t *dtp = datap->dpcd_hdl;
483 dt_proc_t *dpr = datap->dpcd_proc;
484 dt_proc_hash_t *dph = dpr->dpr_hdl->dt_procs;
485 struct ps_prochandle *P = dpr->dpr_proc;
486 int pid = dpr->dpr_pid;
487
488#if defined(sun)
489 int pfd = Pctlfd(P);
490
491 const long wstop = PCWSTOP;
492#endif
493 int notify = B_FALSE;
494
495 /*
496 * We disable the POSIX thread cancellation mechanism so that the
497 * client program using libdtrace can't accidentally cancel our thread.
498 * dt_proc_destroy() uses SIGCANCEL explicitly to simply poke us out
499 * of PCWSTOP with EINTR, at which point we will see dpr_quit and exit.
500 */
501 (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
502
503 /*
504 * Set up the corresponding process for tracing by libdtrace. We want
505 * to be able to catch breakpoints and efficiently single-step over
506 * them, and we need to enable librtld_db to watch libdl activity.
507 */
508 (void) pthread_mutex_lock(&dpr->dpr_lock);
509
510#if defined(sun)
511 (void) Punsetflags(P, PR_ASYNC); /* require synchronous mode */
512 (void) Psetflags(P, PR_BPTADJ); /* always adjust eip on x86 */
513 (void) Punsetflags(P, PR_FORK); /* do not inherit on fork */
514
515 (void) Pfault(P, FLTBPT, B_TRUE); /* always trace breakpoints */
516 (void) Pfault(P, FLTTRACE, B_TRUE); /* always trace single-step */
517
518 /*
519 * We must trace exit from exec() system calls so that if the exec is
520 * successful, we can reset our breakpoints and re-initialize libproc.
521 */
522 (void) Psysexit(P, SYS_execve, B_TRUE);
523
524 /*
525 * We must trace entry and exit for fork() system calls in order to
526 * disable our breakpoints temporarily during the fork. We do not set
527 * the PR_FORK flag, so if fork succeeds the child begins executing and
528 * does not inherit any other tracing behaviors or a control thread.
529 */
530 (void) Psysentry(P, SYS_vfork, B_TRUE);
531 (void) Psysexit(P, SYS_vfork, B_TRUE);
532 (void) Psysentry(P, SYS_forksys, B_TRUE);
533 (void) Psysexit(P, SYS_forksys, B_TRUE);
534
535 Psync(P); /* enable all /proc changes */
482
483typedef struct dt_proc_control_data {
484 dtrace_hdl_t *dpcd_hdl; /* DTrace handle */
485 dt_proc_t *dpcd_proc; /* proccess to control */
486} dt_proc_control_data_t;
487
488/*
489 * Main loop for all victim process control threads. We initialize all the
490 * appropriate /proc control mechanisms, and then enter a loop waiting for
491 * the process to stop on an event or die. We process any events by calling
492 * appropriate subroutines, and exit when the victim dies or we lose control.
493 *
494 * The control thread synchronizes the use of dpr_proc with other libdtrace
495 * threads using dpr_lock. We hold the lock for all of our operations except
496 * waiting while the process is running: this is accomplished by writing a
497 * PCWSTOP directive directly to the underlying /proc/<pid>/ctl file. If the
498 * libdtrace client wishes to exit or abort our wait, SIGCANCEL can be used.
499 */
500static void *
501dt_proc_control(void *arg)
502{
503 dt_proc_control_data_t *datap = arg;
504 dtrace_hdl_t *dtp = datap->dpcd_hdl;
505 dt_proc_t *dpr = datap->dpcd_proc;
506 dt_proc_hash_t *dph = dpr->dpr_hdl->dt_procs;
507 struct ps_prochandle *P = dpr->dpr_proc;
508 int pid = dpr->dpr_pid;
509
510#if defined(sun)
511 int pfd = Pctlfd(P);
512
513 const long wstop = PCWSTOP;
514#endif
515 int notify = B_FALSE;
516
517 /*
518 * We disable the POSIX thread cancellation mechanism so that the
519 * client program using libdtrace can't accidentally cancel our thread.
520 * dt_proc_destroy() uses SIGCANCEL explicitly to simply poke us out
521 * of PCWSTOP with EINTR, at which point we will see dpr_quit and exit.
522 */
523 (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
524
525 /*
526 * Set up the corresponding process for tracing by libdtrace. We want
527 * to be able to catch breakpoints and efficiently single-step over
528 * them, and we need to enable librtld_db to watch libdl activity.
529 */
530 (void) pthread_mutex_lock(&dpr->dpr_lock);
531
532#if defined(sun)
533 (void) Punsetflags(P, PR_ASYNC); /* require synchronous mode */
534 (void) Psetflags(P, PR_BPTADJ); /* always adjust eip on x86 */
535 (void) Punsetflags(P, PR_FORK); /* do not inherit on fork */
536
537 (void) Pfault(P, FLTBPT, B_TRUE); /* always trace breakpoints */
538 (void) Pfault(P, FLTTRACE, B_TRUE); /* always trace single-step */
539
540 /*
541 * We must trace exit from exec() system calls so that if the exec is
542 * successful, we can reset our breakpoints and re-initialize libproc.
543 */
544 (void) Psysexit(P, SYS_execve, B_TRUE);
545
546 /*
547 * We must trace entry and exit for fork() system calls in order to
548 * disable our breakpoints temporarily during the fork. We do not set
549 * the PR_FORK flag, so if fork succeeds the child begins executing and
550 * does not inherit any other tracing behaviors or a control thread.
551 */
552 (void) Psysentry(P, SYS_vfork, B_TRUE);
553 (void) Psysexit(P, SYS_vfork, B_TRUE);
554 (void) Psysentry(P, SYS_forksys, B_TRUE);
555 (void) Psysexit(P, SYS_forksys, B_TRUE);
556
557 Psync(P); /* enable all /proc changes */
558#endif
536 dt_proc_attach(dpr, B_FALSE); /* enable rtld breakpoints */
537
538 /*
539 * If PR_KLC is set, we created the process; otherwise we grabbed it.
540 * Check for an appropriate stop request and wait for dt_proc_continue.
541 */
559 dt_proc_attach(dpr, B_FALSE); /* enable rtld breakpoints */
560
561 /*
562 * If PR_KLC is set, we created the process; otherwise we grabbed it.
563 * Check for an appropriate stop request and wait for dt_proc_continue.
564 */
565#if defined(sun)
542 if (Pstatus(P)->pr_flags & PR_KLC)
566 if (Pstatus(P)->pr_flags & PR_KLC)
567#else
568 if (proc_getflags(P) & PR_KLC)
569#endif
543 dt_proc_stop(dpr, DT_PROC_STOP_CREATE);
544 else
545 dt_proc_stop(dpr, DT_PROC_STOP_GRAB);
546
547 if (Psetrun(P, 0, 0) == -1) {
548 dt_dprintf("pid %d: failed to set running: %s\n",
549 (int)dpr->dpr_pid, strerror(errno));
550 }
570 dt_proc_stop(dpr, DT_PROC_STOP_CREATE);
571 else
572 dt_proc_stop(dpr, DT_PROC_STOP_GRAB);
573
574 if (Psetrun(P, 0, 0) == -1) {
575 dt_dprintf("pid %d: failed to set running: %s\n",
576 (int)dpr->dpr_pid, strerror(errno));
577 }
551#else
552 /*
553 * If PR_KLC is set, we created the process; otherwise we grabbed it.
554 * Check for an appropriate stop request and wait for dt_proc_continue.
555 */
556 if (proc_getflags(P) & PR_KLC)
557 dt_proc_stop(dpr, DT_PROC_STOP_CREATE);
558 else
559 dt_proc_stop(dpr, DT_PROC_STOP_GRAB);
560
578
561 if (proc_continue(P) != 0)
562 dt_dprintf("pid %d: failed to set running: %s\n",
563 (int)dpr->dpr_pid, strerror(errno));
564#endif
565
566 (void) pthread_mutex_unlock(&dpr->dpr_lock);
567
568 /*
569 * Wait for the process corresponding to this control thread to stop,
570 * process the event, and then set it running again. We want to sleep
571 * with dpr_lock *unheld* so that other parts of libdtrace can use the
572 * ps_prochandle in the meantime (e.g. ustack()). To do this, we write
573 * a PCWSTOP directive directly to the underlying /proc/<pid>/ctl file.
574 * Once the process stops, we wake up, grab dpr_lock, and then call
575 * Pwait() (which will return immediately) and do our processing.
576 */
577 while (!dpr->dpr_quit) {
579 (void) pthread_mutex_unlock(&dpr->dpr_lock);
580
581 /*
582 * Wait for the process corresponding to this control thread to stop,
583 * process the event, and then set it running again. We want to sleep
584 * with dpr_lock *unheld* so that other parts of libdtrace can use the
585 * ps_prochandle in the meantime (e.g. ustack()). To do this, we write
586 * a PCWSTOP directive directly to the underlying /proc/<pid>/ctl file.
587 * Once the process stops, we wake up, grab dpr_lock, and then call
588 * Pwait() (which will return immediately) and do our processing.
589 */
590 while (!dpr->dpr_quit) {
578#if defined(sun)
579 const lwpstatus_t *psp;
580
591 const lwpstatus_t *psp;
592
593#if defined(sun)
581 if (write(pfd, &wstop, sizeof (wstop)) == -1 && errno == EINTR)
582 continue; /* check dpr_quit and continue waiting */
583#else
584 /* Wait for the process to report status. */
585 proc_wstatus(P);
594 if (write(pfd, &wstop, sizeof (wstop)) == -1 && errno == EINTR)
595 continue; /* check dpr_quit and continue waiting */
596#else
597 /* Wait for the process to report status. */
598 proc_wstatus(P);
599 if (errno == EINTR)
600 continue; /* check dpr_quit and continue waiting */
586#endif
587
588 (void) pthread_mutex_lock(&dpr->dpr_lock);
589
590#if defined(sun)
591pwait_locked:
592 if (Pstopstatus(P, PCNULL, 0) == -1 && errno == EINTR) {
593 (void) pthread_mutex_unlock(&dpr->dpr_lock);
594 continue; /* check dpr_quit and continue waiting */
595 }
596#endif
597
601#endif
602
603 (void) pthread_mutex_lock(&dpr->dpr_lock);
604
605#if defined(sun)
606pwait_locked:
607 if (Pstopstatus(P, PCNULL, 0) == -1 && errno == EINTR) {
608 (void) pthread_mutex_unlock(&dpr->dpr_lock);
609 continue; /* check dpr_quit and continue waiting */
610 }
611#endif
612
598#if defined(sun)
599 switch (Pstate(P)) {
613 switch (Pstate(P)) {
600#else
601 switch (proc_state(P)) {
602#endif
603 case PS_STOP:
614 case PS_STOP:
604#ifdef DOODAD
615#if defined(sun)
605 psp = &Pstatus(P)->pr_lwp;
616 psp = &Pstatus(P)->pr_lwp;
617#else
618 psp = proc_getlwpstatus(P);
619#endif
606
607 dt_dprintf("pid %d: proc stopped showing %d/%d\n",
608 pid, psp->pr_why, psp->pr_what);
609
610 /*
611 * If the process stops showing PR_REQUESTED, then the
612 * DTrace stop() action was applied to it or another
613 * debugging utility (e.g. pstop(1)) asked it to stop.
614 * In either case, the user's intention is for the
615 * process to remain stopped until another external
616 * mechanism (e.g. prun(1)) is applied. So instead of
617 * setting the process running ourself, we wait for
618 * someone else to do so. Once that happens, we return
619 * to our normal loop waiting for an event of interest.
620 */
621 if (psp->pr_why == PR_REQUESTED) {
622 dt_proc_waitrun(dpr);
623 (void) pthread_mutex_unlock(&dpr->dpr_lock);
624 continue;
625 }
626
627 /*
628 * If the process stops showing one of the events that
629 * we are tracing, perform the appropriate response.
630 * Note that we ignore PR_SUSPENDED, PR_CHECKPOINT, and
631 * PR_JOBCONTROL by design: if one of these conditions
632 * occurs, we will fall through to Psetrun() but the
633 * process will remain stopped in the kernel by the
634 * corresponding mechanism (e.g. job control stop).
635 */
636 if (psp->pr_why == PR_FAULTED && psp->pr_what == FLTBPT)
637 dt_proc_bpmatch(dtp, dpr);
638 else if (psp->pr_why == PR_SYSENTRY &&
639 IS_SYS_FORK(psp->pr_what))
640 dt_proc_bpdisable(dpr);
641 else if (psp->pr_why == PR_SYSEXIT &&
642 IS_SYS_FORK(psp->pr_what))
643 dt_proc_bpenable(dpr);
644 else if (psp->pr_why == PR_SYSEXIT &&
645 IS_SYS_EXEC(psp->pr_what))
646 dt_proc_attach(dpr, B_TRUE);
620
621 dt_dprintf("pid %d: proc stopped showing %d/%d\n",
622 pid, psp->pr_why, psp->pr_what);
623
624 /*
625 * If the process stops showing PR_REQUESTED, then the
626 * DTrace stop() action was applied to it or another
627 * debugging utility (e.g. pstop(1)) asked it to stop.
628 * In either case, the user's intention is for the
629 * process to remain stopped until another external
630 * mechanism (e.g. prun(1)) is applied. So instead of
631 * setting the process running ourself, we wait for
632 * someone else to do so. Once that happens, we return
633 * to our normal loop waiting for an event of interest.
634 */
635 if (psp->pr_why == PR_REQUESTED) {
636 dt_proc_waitrun(dpr);
637 (void) pthread_mutex_unlock(&dpr->dpr_lock);
638 continue;
639 }
640
641 /*
642 * If the process stops showing one of the events that
643 * we are tracing, perform the appropriate response.
644 * Note that we ignore PR_SUSPENDED, PR_CHECKPOINT, and
645 * PR_JOBCONTROL by design: if one of these conditions
646 * occurs, we will fall through to Psetrun() but the
647 * process will remain stopped in the kernel by the
648 * corresponding mechanism (e.g. job control stop).
649 */
650 if (psp->pr_why == PR_FAULTED && psp->pr_what == FLTBPT)
651 dt_proc_bpmatch(dtp, dpr);
652 else if (psp->pr_why == PR_SYSENTRY &&
653 IS_SYS_FORK(psp->pr_what))
654 dt_proc_bpdisable(dpr);
655 else if (psp->pr_why == PR_SYSEXIT &&
656 IS_SYS_FORK(psp->pr_what))
657 dt_proc_bpenable(dpr);
658 else if (psp->pr_why == PR_SYSEXIT &&
659 IS_SYS_EXEC(psp->pr_what))
660 dt_proc_attach(dpr, B_TRUE);
647#endif
648 break;
649
650 case PS_LOST:
651#if defined(sun)
652 if (Preopen(P) == 0)
653 goto pwait_locked;
654#endif
655
656 dt_dprintf("pid %d: proc lost: %s\n",
657 pid, strerror(errno));
658
659 dpr->dpr_quit = B_TRUE;
660 notify = B_TRUE;
661 break;
662
663 case PS_UNDEAD:
664 dt_dprintf("pid %d: proc died\n", pid);
665 dpr->dpr_quit = B_TRUE;
666 notify = B_TRUE;
667 break;
668 }
669
661 break;
662
663 case PS_LOST:
664#if defined(sun)
665 if (Preopen(P) == 0)
666 goto pwait_locked;
667#endif
668
669 dt_dprintf("pid %d: proc lost: %s\n",
670 pid, strerror(errno));
671
672 dpr->dpr_quit = B_TRUE;
673 notify = B_TRUE;
674 break;
675
676 case PS_UNDEAD:
677 dt_dprintf("pid %d: proc died\n", pid);
678 dpr->dpr_quit = B_TRUE;
679 notify = B_TRUE;
680 break;
681 }
682
670#if defined(sun)
671 if (Pstate(P) != PS_UNDEAD && Psetrun(P, 0, 0) == -1) {
672 dt_dprintf("pid %d: failed to set running: %s\n",
673 (int)dpr->dpr_pid, strerror(errno));
674 }
683 if (Pstate(P) != PS_UNDEAD && Psetrun(P, 0, 0) == -1) {
684 dt_dprintf("pid %d: failed to set running: %s\n",
685 (int)dpr->dpr_pid, strerror(errno));
686 }
675#endif
676
677 (void) pthread_mutex_unlock(&dpr->dpr_lock);
678 }
679
680 /*
681 * If the control thread detected PS_UNDEAD or PS_LOST, then enqueue
682 * the dt_proc_t structure on the dt_proc_hash_t notification list.
683 */
684 if (notify)
685 dt_proc_notify(dtp, dph, dpr, NULL);
686
687 /*
688 * Destroy and remove any remaining breakpoints, set dpr_done and clear
689 * dpr_tid to indicate the control thread has exited, and notify any
690 * waiting thread in dt_proc_destroy() that we have succesfully exited.
691 */
692 (void) pthread_mutex_lock(&dpr->dpr_lock);
693
694 dt_proc_bpdestroy(dpr, B_TRUE);
695 dpr->dpr_done = B_TRUE;
696 dpr->dpr_tid = 0;
697
698 (void) pthread_cond_broadcast(&dpr->dpr_cv);
699 (void) pthread_mutex_unlock(&dpr->dpr_lock);
700
701 return (NULL);
702}
703
704/*PRINTFLIKE3*/
705static struct ps_prochandle *
706dt_proc_error(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *format, ...)
707{
708 va_list ap;
709
710 va_start(ap, format);
711 dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
712 va_end(ap);
713
714 if (dpr->dpr_proc != NULL)
687
688 (void) pthread_mutex_unlock(&dpr->dpr_lock);
689 }
690
691 /*
692 * If the control thread detected PS_UNDEAD or PS_LOST, then enqueue
693 * the dt_proc_t structure on the dt_proc_hash_t notification list.
694 */
695 if (notify)
696 dt_proc_notify(dtp, dph, dpr, NULL);
697
698 /*
699 * Destroy and remove any remaining breakpoints, set dpr_done and clear
700 * dpr_tid to indicate the control thread has exited, and notify any
701 * waiting thread in dt_proc_destroy() that we have succesfully exited.
702 */
703 (void) pthread_mutex_lock(&dpr->dpr_lock);
704
705 dt_proc_bpdestroy(dpr, B_TRUE);
706 dpr->dpr_done = B_TRUE;
707 dpr->dpr_tid = 0;
708
709 (void) pthread_cond_broadcast(&dpr->dpr_cv);
710 (void) pthread_mutex_unlock(&dpr->dpr_lock);
711
712 return (NULL);
713}
714
715/*PRINTFLIKE3*/
716static struct ps_prochandle *
717dt_proc_error(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *format, ...)
718{
719 va_list ap;
720
721 va_start(ap, format);
722 dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
723 va_end(ap);
724
725 if (dpr->dpr_proc != NULL)
715#if defined(sun)
716 Prelease(dpr->dpr_proc, 0);
726 Prelease(dpr->dpr_proc, 0);
717#else
718 proc_detach(dpr->dpr_proc, 0);
719#endif
720
721 dt_free(dtp, dpr);
722 (void) dt_set_errno(dtp, EDT_COMPILER);
723 return (NULL);
724}
725
726dt_proc_t *
727dt_proc_lookup(dtrace_hdl_t *dtp, struct ps_prochandle *P, int remove)
728{
729 dt_proc_hash_t *dph = dtp->dt_procs;
730#if defined(sun)
731 pid_t pid = Pstatus(P)->pr_pid;
732#else
733 pid_t pid = proc_getpid(P);
734#endif
735 dt_proc_t *dpr, **dpp = &dph->dph_hash[pid & (dph->dph_hashlen - 1)];
736
737 for (dpr = *dpp; dpr != NULL; dpr = dpr->dpr_hash) {
738 if (dpr->dpr_pid == pid)
739 break;
740 else
741 dpp = &dpr->dpr_hash;
742 }
743
744 assert(dpr != NULL);
745 assert(dpr->dpr_proc == P);
746
747 if (remove)
748 *dpp = dpr->dpr_hash; /* remove from pid hash chain */
749
750 return (dpr);
751}
752
753static void
754dt_proc_destroy(dtrace_hdl_t *dtp, struct ps_prochandle *P)
755{
756 dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
757 dt_proc_hash_t *dph = dtp->dt_procs;
758 dt_proc_notify_t *npr, **npp;
759 int rflag;
760
761 assert(dpr != NULL);
762
763 /*
764 * If neither PR_KLC nor PR_RLC is set, then the process is stopped by
765 * an external debugger and we were waiting in dt_proc_waitrun().
766 * Leave the process in this condition using PRELEASE_HANG.
767 */
768#if defined(sun)
769 if (!(Pstatus(dpr->dpr_proc)->pr_flags & (PR_KLC | PR_RLC))) {
770#else
771 if (!(proc_getflags(dpr->dpr_proc) & (PR_KLC | PR_RLC))) {
772#endif
773 dt_dprintf("abandoning pid %d\n", (int)dpr->dpr_pid);
774 rflag = PRELEASE_HANG;
775#if defined(sun)
776 } else if (Pstatus(dpr->dpr_proc)->pr_flags & PR_KLC) {
777#else
778 } else if (proc_getflags(dpr->dpr_proc) & PR_KLC) {
779#endif
780 dt_dprintf("killing pid %d\n", (int)dpr->dpr_pid);
781 rflag = PRELEASE_KILL; /* apply kill-on-last-close */
782 } else {
783 dt_dprintf("releasing pid %d\n", (int)dpr->dpr_pid);
784 rflag = 0; /* apply run-on-last-close */
785 }
786
787 if (dpr->dpr_tid) {
788 /*
789 * Set the dpr_quit flag to tell the daemon thread to exit. We
790 * send it a SIGCANCEL to poke it out of PCWSTOP or any other
791 * long-term /proc system call. Our daemon threads have POSIX
792 * cancellation disabled, so EINTR will be the only effect. We
793 * then wait for dpr_done to indicate the thread has exited.
794 *
795 * We can't use pthread_kill() to send SIGCANCEL because the
796 * interface forbids it and we can't use pthread_cancel()
797 * because with cancellation disabled it won't actually
798 * send SIGCANCEL to the target thread, so we use _lwp_kill()
799 * to do the job. This is all built on evil knowledge of
800 * the details of the cancellation mechanism in libc.
801 */
802 (void) pthread_mutex_lock(&dpr->dpr_lock);
803 dpr->dpr_quit = B_TRUE;
804#if defined(sun)
805 (void) _lwp_kill(dpr->dpr_tid, SIGCANCEL);
806#else
727
728 dt_free(dtp, dpr);
729 (void) dt_set_errno(dtp, EDT_COMPILER);
730 return (NULL);
731}
732
733dt_proc_t *
734dt_proc_lookup(dtrace_hdl_t *dtp, struct ps_prochandle *P, int remove)
735{
736 dt_proc_hash_t *dph = dtp->dt_procs;
737#if defined(sun)
738 pid_t pid = Pstatus(P)->pr_pid;
739#else
740 pid_t pid = proc_getpid(P);
741#endif
742 dt_proc_t *dpr, **dpp = &dph->dph_hash[pid & (dph->dph_hashlen - 1)];
743
744 for (dpr = *dpp; dpr != NULL; dpr = dpr->dpr_hash) {
745 if (dpr->dpr_pid == pid)
746 break;
747 else
748 dpp = &dpr->dpr_hash;
749 }
750
751 assert(dpr != NULL);
752 assert(dpr->dpr_proc == P);
753
754 if (remove)
755 *dpp = dpr->dpr_hash; /* remove from pid hash chain */
756
757 return (dpr);
758}
759
760static void
761dt_proc_destroy(dtrace_hdl_t *dtp, struct ps_prochandle *P)
762{
763 dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
764 dt_proc_hash_t *dph = dtp->dt_procs;
765 dt_proc_notify_t *npr, **npp;
766 int rflag;
767
768 assert(dpr != NULL);
769
770 /*
771 * If neither PR_KLC nor PR_RLC is set, then the process is stopped by
772 * an external debugger and we were waiting in dt_proc_waitrun().
773 * Leave the process in this condition using PRELEASE_HANG.
774 */
775#if defined(sun)
776 if (!(Pstatus(dpr->dpr_proc)->pr_flags & (PR_KLC | PR_RLC))) {
777#else
778 if (!(proc_getflags(dpr->dpr_proc) & (PR_KLC | PR_RLC))) {
779#endif
780 dt_dprintf("abandoning pid %d\n", (int)dpr->dpr_pid);
781 rflag = PRELEASE_HANG;
782#if defined(sun)
783 } else if (Pstatus(dpr->dpr_proc)->pr_flags & PR_KLC) {
784#else
785 } else if (proc_getflags(dpr->dpr_proc) & PR_KLC) {
786#endif
787 dt_dprintf("killing pid %d\n", (int)dpr->dpr_pid);
788 rflag = PRELEASE_KILL; /* apply kill-on-last-close */
789 } else {
790 dt_dprintf("releasing pid %d\n", (int)dpr->dpr_pid);
791 rflag = 0; /* apply run-on-last-close */
792 }
793
794 if (dpr->dpr_tid) {
795 /*
796 * Set the dpr_quit flag to tell the daemon thread to exit. We
797 * send it a SIGCANCEL to poke it out of PCWSTOP or any other
798 * long-term /proc system call. Our daemon threads have POSIX
799 * cancellation disabled, so EINTR will be the only effect. We
800 * then wait for dpr_done to indicate the thread has exited.
801 *
802 * We can't use pthread_kill() to send SIGCANCEL because the
803 * interface forbids it and we can't use pthread_cancel()
804 * because with cancellation disabled it won't actually
805 * send SIGCANCEL to the target thread, so we use _lwp_kill()
806 * to do the job. This is all built on evil knowledge of
807 * the details of the cancellation mechanism in libc.
808 */
809 (void) pthread_mutex_lock(&dpr->dpr_lock);
810 dpr->dpr_quit = B_TRUE;
811#if defined(sun)
812 (void) _lwp_kill(dpr->dpr_tid, SIGCANCEL);
813#else
807 (void) pthread_kill(dpr->dpr_tid, SIGUSR1);
814 pthread_kill(dpr->dpr_tid, SIGUSR1);
808#endif
809
810 /*
811 * If the process is currently idling in dt_proc_stop(), re-
812 * enable breakpoints and poke it into running again.
813 */
814 if (dpr->dpr_stop & DT_PROC_STOP_IDLE) {
815 dt_proc_bpenable(dpr);
816 dpr->dpr_stop &= ~DT_PROC_STOP_IDLE;
817 (void) pthread_cond_broadcast(&dpr->dpr_cv);
818 }
819
820 while (!dpr->dpr_done)
821 (void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);
822
823 (void) pthread_mutex_unlock(&dpr->dpr_lock);
824 }
825
826 /*
827 * Before we free the process structure, remove this dt_proc_t from the
828 * lookup hash, and then walk the dt_proc_hash_t's notification list
829 * and remove this dt_proc_t if it is enqueued.
830 */
831 (void) pthread_mutex_lock(&dph->dph_lock);
832 (void) dt_proc_lookup(dtp, P, B_TRUE);
833 npp = &dph->dph_notify;
834
835 while ((npr = *npp) != NULL) {
836 if (npr->dprn_dpr == dpr) {
837 *npp = npr->dprn_next;
838 dt_free(dtp, npr);
839 } else {
840 npp = &npr->dprn_next;
841 }
842 }
843
844 (void) pthread_mutex_unlock(&dph->dph_lock);
845
846 /*
847 * Remove the dt_proc_list from the LRU list, release the underlying
848 * libproc handle, and free our dt_proc_t data structure.
849 */
850 if (dpr->dpr_cacheable) {
851 assert(dph->dph_lrucnt != 0);
852 dph->dph_lrucnt--;
853 }
854
855 dt_list_delete(&dph->dph_lrulist, dpr);
815#endif
816
817 /*
818 * If the process is currently idling in dt_proc_stop(), re-
819 * enable breakpoints and poke it into running again.
820 */
821 if (dpr->dpr_stop & DT_PROC_STOP_IDLE) {
822 dt_proc_bpenable(dpr);
823 dpr->dpr_stop &= ~DT_PROC_STOP_IDLE;
824 (void) pthread_cond_broadcast(&dpr->dpr_cv);
825 }
826
827 while (!dpr->dpr_done)
828 (void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);
829
830 (void) pthread_mutex_unlock(&dpr->dpr_lock);
831 }
832
833 /*
834 * Before we free the process structure, remove this dt_proc_t from the
835 * lookup hash, and then walk the dt_proc_hash_t's notification list
836 * and remove this dt_proc_t if it is enqueued.
837 */
838 (void) pthread_mutex_lock(&dph->dph_lock);
839 (void) dt_proc_lookup(dtp, P, B_TRUE);
840 npp = &dph->dph_notify;
841
842 while ((npr = *npp) != NULL) {
843 if (npr->dprn_dpr == dpr) {
844 *npp = npr->dprn_next;
845 dt_free(dtp, npr);
846 } else {
847 npp = &npr->dprn_next;
848 }
849 }
850
851 (void) pthread_mutex_unlock(&dph->dph_lock);
852
853 /*
854 * Remove the dt_proc_list from the LRU list, release the underlying
855 * libproc handle, and free our dt_proc_t data structure.
856 */
857 if (dpr->dpr_cacheable) {
858 assert(dph->dph_lrucnt != 0);
859 dph->dph_lrucnt--;
860 }
861
862 dt_list_delete(&dph->dph_lrulist, dpr);
856#if defined(sun)
857 Prelease(dpr->dpr_proc, rflag);
863 Prelease(dpr->dpr_proc, rflag);
858#else
859 proc_detach(dpr->dpr_proc, rflag);
860#endif
861 dt_free(dtp, dpr);
862}
863
864static int
865dt_proc_create_thread(dtrace_hdl_t *dtp, dt_proc_t *dpr, uint_t stop)
866{
867 dt_proc_control_data_t data;
868 sigset_t nset, oset;
869 pthread_attr_t a;
870 int err;
871
872 (void) pthread_mutex_lock(&dpr->dpr_lock);
873 dpr->dpr_stop |= stop; /* set bit for initial rendezvous */
874
875 (void) pthread_attr_init(&a);
876 (void) pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
877
878 (void) sigfillset(&nset);
879 (void) sigdelset(&nset, SIGABRT); /* unblocked for assert() */
880#if defined(sun)
881 (void) sigdelset(&nset, SIGCANCEL); /* see dt_proc_destroy() */
882#else
883 (void) sigdelset(&nset, SIGUSR1); /* see dt_proc_destroy() */
884#endif
885
886 data.dpcd_hdl = dtp;
887 data.dpcd_proc = dpr;
888
889 (void) pthread_sigmask(SIG_SETMASK, &nset, &oset);
890 err = pthread_create(&dpr->dpr_tid, &a, dt_proc_control, &data);
891 (void) pthread_sigmask(SIG_SETMASK, &oset, NULL);
892
893 /*
894 * If the control thread was created, then wait on dpr_cv for either
895 * dpr_done to be set (the victim died or the control thread failed)
896 * or DT_PROC_STOP_IDLE to be set, indicating that the victim is now
897 * stopped by /proc and the control thread is at the rendezvous event.
898 * On success, we return with the process and control thread stopped:
899 * the caller can then apply dt_proc_continue() to resume both.
900 */
901 if (err == 0) {
902 while (!dpr->dpr_done && !(dpr->dpr_stop & DT_PROC_STOP_IDLE))
903 (void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);
904
905 /*
906 * If dpr_done is set, the control thread aborted before it
907 * reached the rendezvous event. This is either due to PS_LOST
908 * or PS_UNDEAD (i.e. the process died). We try to provide a
909 * small amount of useful information to help figure it out.
910 */
911 if (dpr->dpr_done) {
912#if defined(sun)
913 const psinfo_t *prp = Ppsinfo(dpr->dpr_proc);
914 int stat = prp ? prp->pr_wstat : 0;
864 dt_free(dtp, dpr);
865}
866
867static int
868dt_proc_create_thread(dtrace_hdl_t *dtp, dt_proc_t *dpr, uint_t stop)
869{
870 dt_proc_control_data_t data;
871 sigset_t nset, oset;
872 pthread_attr_t a;
873 int err;
874
875 (void) pthread_mutex_lock(&dpr->dpr_lock);
876 dpr->dpr_stop |= stop; /* set bit for initial rendezvous */
877
878 (void) pthread_attr_init(&a);
879 (void) pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
880
881 (void) sigfillset(&nset);
882 (void) sigdelset(&nset, SIGABRT); /* unblocked for assert() */
883#if defined(sun)
884 (void) sigdelset(&nset, SIGCANCEL); /* see dt_proc_destroy() */
885#else
886 (void) sigdelset(&nset, SIGUSR1); /* see dt_proc_destroy() */
887#endif
888
889 data.dpcd_hdl = dtp;
890 data.dpcd_proc = dpr;
891
892 (void) pthread_sigmask(SIG_SETMASK, &nset, &oset);
893 err = pthread_create(&dpr->dpr_tid, &a, dt_proc_control, &data);
894 (void) pthread_sigmask(SIG_SETMASK, &oset, NULL);
895
896 /*
897 * If the control thread was created, then wait on dpr_cv for either
898 * dpr_done to be set (the victim died or the control thread failed)
899 * or DT_PROC_STOP_IDLE to be set, indicating that the victim is now
900 * stopped by /proc and the control thread is at the rendezvous event.
901 * On success, we return with the process and control thread stopped:
902 * the caller can then apply dt_proc_continue() to resume both.
903 */
904 if (err == 0) {
905 while (!dpr->dpr_done && !(dpr->dpr_stop & DT_PROC_STOP_IDLE))
906 (void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);
907
908 /*
909 * If dpr_done is set, the control thread aborted before it
910 * reached the rendezvous event. This is either due to PS_LOST
911 * or PS_UNDEAD (i.e. the process died). We try to provide a
912 * small amount of useful information to help figure it out.
913 */
914 if (dpr->dpr_done) {
915#if defined(sun)
916 const psinfo_t *prp = Ppsinfo(dpr->dpr_proc);
917 int stat = prp ? prp->pr_wstat : 0;
915#endif
916 int pid = dpr->dpr_pid;
918 int pid = dpr->dpr_pid;
917
918#if defined(sun)
919 if (Pstate(dpr->dpr_proc) == PS_LOST) {
920#else
919#else
921 if (proc_state(dpr->dpr_proc) == PS_LOST) {
920 int stat = proc_getwstat(dpr->dpr_proc);
921 int pid = proc_getpid(dpr->dpr_proc);
922#endif
922#endif
923 if (proc_state(dpr->dpr_proc) == PS_LOST) {
923 (void) dt_proc_error(dpr->dpr_hdl, dpr,
924 "failed to control pid %d: process exec'd "
925 "set-id or unobservable program\n", pid);
924 (void) dt_proc_error(dpr->dpr_hdl, dpr,
925 "failed to control pid %d: process exec'd "
926 "set-id or unobservable program\n", pid);
926#if defined(sun)
927 } else if (WIFSIGNALED(stat)) {
928 (void) dt_proc_error(dpr->dpr_hdl, dpr,
929 "failed to control pid %d: process died "
930 "from signal %d\n", pid, WTERMSIG(stat));
931 } else {
932 (void) dt_proc_error(dpr->dpr_hdl, dpr,
933 "failed to control pid %d: process exited "
934 "with status %d\n", pid, WEXITSTATUS(stat));
927 } else if (WIFSIGNALED(stat)) {
928 (void) dt_proc_error(dpr->dpr_hdl, dpr,
929 "failed to control pid %d: process died "
930 "from signal %d\n", pid, WTERMSIG(stat));
931 } else {
932 (void) dt_proc_error(dpr->dpr_hdl, dpr,
933 "failed to control pid %d: process exited "
934 "with status %d\n", pid, WEXITSTATUS(stat));
935#endif
936 }
937
938 err = ESRCH; /* cause grab() or create() to fail */
939 }
940 } else {
941 (void) dt_proc_error(dpr->dpr_hdl, dpr,
942 "failed to create control thread for process-id %d: %s\n",
943 (int)dpr->dpr_pid, strerror(err));
944 }
945
946 (void) pthread_mutex_unlock(&dpr->dpr_lock);
947 (void) pthread_attr_destroy(&a);
948
949 return (err);
950}
951
952struct ps_prochandle *
953dt_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv,
954 proc_child_func *pcf, void *child_arg)
955{
956 dt_proc_hash_t *dph = dtp->dt_procs;
957 dt_proc_t *dpr;
958 int err;
959
960 if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL)
961 return (NULL); /* errno is set for us */
962
963 (void) pthread_mutex_init(&dpr->dpr_lock, NULL);
964 (void) pthread_cond_init(&dpr->dpr_cv, NULL);
965
966#if defined(sun)
967 if ((dpr->dpr_proc = Pcreate(file, argv, &err, NULL, 0)) == NULL) {
935 }
936
937 err = ESRCH; /* cause grab() or create() to fail */
938 }
939 } else {
940 (void) dt_proc_error(dpr->dpr_hdl, dpr,
941 "failed to create control thread for process-id %d: %s\n",
942 (int)dpr->dpr_pid, strerror(err));
943 }
944
945 (void) pthread_mutex_unlock(&dpr->dpr_lock);
946 (void) pthread_attr_destroy(&a);
947
948 return (err);
949}
950
951struct ps_prochandle *
952dt_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv,
953 proc_child_func *pcf, void *child_arg)
954{
955 dt_proc_hash_t *dph = dtp->dt_procs;
956 dt_proc_t *dpr;
957 int err;
958
959 if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL)
960 return (NULL); /* errno is set for us */
961
962 (void) pthread_mutex_init(&dpr->dpr_lock, NULL);
963 (void) pthread_cond_init(&dpr->dpr_cv, NULL);
964
965#if defined(sun)
966 if ((dpr->dpr_proc = Pcreate(file, argv, &err, NULL, 0)) == NULL) {
967#else
968 if ((err = proc_create(file, argv, pcf, child_arg,
969 &dpr->dpr_proc)) != 0) {
970#endif
968 return (dt_proc_error(dtp, dpr,
969 "failed to execute %s: %s\n", file, Pcreate_error(err)));
970 }
971
972 dpr->dpr_hdl = dtp;
971 return (dt_proc_error(dtp, dpr,
972 "failed to execute %s: %s\n", file, Pcreate_error(err)));
973 }
974
975 dpr->dpr_hdl = dtp;
976#if defined(sun)
973 dpr->dpr_pid = Pstatus(dpr->dpr_proc)->pr_pid;
977 dpr->dpr_pid = Pstatus(dpr->dpr_proc)->pr_pid;
974
975 (void) Punsetflags(dpr->dpr_proc, PR_RLC);
976 (void) Psetflags(dpr->dpr_proc, PR_KLC);
977#else
978#else
978 (void) proc_clearflags(dpr->dpr_proc, PR_RLC);
979 (void) proc_setflags(dpr->dpr_proc, PR_KLC);
980 if ((err = proc_create(file, argv, pcf, child_arg, &dpr->dpr_proc)) != 0)
981 return (dt_proc_error(dtp, dpr,
982 "failed to execute %s: %s\n", file, strerror(err)));
983 dpr->dpr_hdl = dtp;
984 dpr->dpr_pid = proc_getpid(dpr->dpr_proc);
985#endif
986
979 dpr->dpr_pid = proc_getpid(dpr->dpr_proc);
980#endif
981
987#if defined(sun)
982 (void) Punsetflags(dpr->dpr_proc, PR_RLC);
983 (void) Psetflags(dpr->dpr_proc, PR_KLC);
984
988 if (dt_proc_create_thread(dtp, dpr, dtp->dt_prcmode) != 0)
985 if (dt_proc_create_thread(dtp, dpr, dtp->dt_prcmode) != 0)
989#else
990 if (dt_proc_create_thread(dtp, dpr, DT_PROC_STOP_IDLE) != 0)
991#endif
992 return (NULL); /* dt_proc_error() has been called for us */
993
994 dpr->dpr_hash = dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)];
995 dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)] = dpr;
996 dt_list_prepend(&dph->dph_lrulist, dpr);
997
998 dt_dprintf("created pid %d\n", (int)dpr->dpr_pid);
999 dpr->dpr_refs++;
1000
1001 return (dpr->dpr_proc);
1002}
1003
1004struct ps_prochandle *
1005dt_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags, int nomonitor)
1006{
1007 dt_proc_hash_t *dph = dtp->dt_procs;
1008 uint_t h = pid & (dph->dph_hashlen - 1);
1009 dt_proc_t *dpr, *opr;
1010 int err;
1011
1012 /*
1013 * Search the hash table for the pid. If it is already grabbed or
1014 * created, move the handle to the front of the lrulist, increment
1015 * the reference count, and return the existing ps_prochandle.
1016 */
1017 for (dpr = dph->dph_hash[h]; dpr != NULL; dpr = dpr->dpr_hash) {
1018 if (dpr->dpr_pid == pid && !dpr->dpr_stale) {
1019 /*
1020 * If the cached handle was opened read-only and
1021 * this request is for a writeable handle, mark
1022 * the cached handle as stale and open a new handle.
1023 * Since it's stale, unmark it as cacheable.
1024 */
1025 if (dpr->dpr_rdonly && !(flags & PGRAB_RDONLY)) {
1026 dt_dprintf("upgrading pid %d\n", (int)pid);
1027 dpr->dpr_stale = B_TRUE;
1028 dpr->dpr_cacheable = B_FALSE;
1029 dph->dph_lrucnt--;
1030 break;
1031 }
1032
1033 dt_dprintf("grabbed pid %d (cached)\n", (int)pid);
1034 dt_list_delete(&dph->dph_lrulist, dpr);
1035 dt_list_prepend(&dph->dph_lrulist, dpr);
1036 dpr->dpr_refs++;
1037 return (dpr->dpr_proc);
1038 }
1039 }
1040
1041 if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL)
1042 return (NULL); /* errno is set for us */
1043
1044 (void) pthread_mutex_init(&dpr->dpr_lock, NULL);
1045 (void) pthread_cond_init(&dpr->dpr_cv, NULL);
1046
1047#if defined(sun)
1048 if ((dpr->dpr_proc = Pgrab(pid, flags, &err)) == NULL) {
986 return (NULL); /* dt_proc_error() has been called for us */
987
988 dpr->dpr_hash = dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)];
989 dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)] = dpr;
990 dt_list_prepend(&dph->dph_lrulist, dpr);
991
992 dt_dprintf("created pid %d\n", (int)dpr->dpr_pid);
993 dpr->dpr_refs++;
994
995 return (dpr->dpr_proc);
996}
997
998struct ps_prochandle *
999dt_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags, int nomonitor)
1000{
1001 dt_proc_hash_t *dph = dtp->dt_procs;
1002 uint_t h = pid & (dph->dph_hashlen - 1);
1003 dt_proc_t *dpr, *opr;
1004 int err;
1005
1006 /*
1007 * Search the hash table for the pid. If it is already grabbed or
1008 * created, move the handle to the front of the lrulist, increment
1009 * the reference count, and return the existing ps_prochandle.
1010 */
1011 for (dpr = dph->dph_hash[h]; dpr != NULL; dpr = dpr->dpr_hash) {
1012 if (dpr->dpr_pid == pid && !dpr->dpr_stale) {
1013 /*
1014 * If the cached handle was opened read-only and
1015 * this request is for a writeable handle, mark
1016 * the cached handle as stale and open a new handle.
1017 * Since it's stale, unmark it as cacheable.
1018 */
1019 if (dpr->dpr_rdonly && !(flags & PGRAB_RDONLY)) {
1020 dt_dprintf("upgrading pid %d\n", (int)pid);
1021 dpr->dpr_stale = B_TRUE;
1022 dpr->dpr_cacheable = B_FALSE;
1023 dph->dph_lrucnt--;
1024 break;
1025 }
1026
1027 dt_dprintf("grabbed pid %d (cached)\n", (int)pid);
1028 dt_list_delete(&dph->dph_lrulist, dpr);
1029 dt_list_prepend(&dph->dph_lrulist, dpr);
1030 dpr->dpr_refs++;
1031 return (dpr->dpr_proc);
1032 }
1033 }
1034
1035 if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL)
1036 return (NULL); /* errno is set for us */
1037
1038 (void) pthread_mutex_init(&dpr->dpr_lock, NULL);
1039 (void) pthread_cond_init(&dpr->dpr_cv, NULL);
1040
1041#if defined(sun)
1042 if ((dpr->dpr_proc = Pgrab(pid, flags, &err)) == NULL) {
1043#else
1044 if ((err = proc_attach(pid, flags, &dpr->dpr_proc)) != 0) {
1045#endif
1049 return (dt_proc_error(dtp, dpr,
1050 "failed to grab pid %d: %s\n", (int)pid, Pgrab_error(err)));
1051 }
1046 return (dt_proc_error(dtp, dpr,
1047 "failed to grab pid %d: %s\n", (int)pid, Pgrab_error(err)));
1048 }
1052#else
1053 if ((err = proc_attach(pid, flags, &dpr->dpr_proc)) != 0)
1054 return (dt_proc_error(dtp, dpr,
1055 "failed to grab pid %d: %s\n", (int) pid, strerror(err)));
1056#endif
1057
1058 dpr->dpr_hdl = dtp;
1059 dpr->dpr_pid = pid;
1060
1049
1050 dpr->dpr_hdl = dtp;
1051 dpr->dpr_pid = pid;
1052
1061#if defined(sun)
1062 (void) Punsetflags(dpr->dpr_proc, PR_KLC);
1063 (void) Psetflags(dpr->dpr_proc, PR_RLC);
1053 (void) Punsetflags(dpr->dpr_proc, PR_KLC);
1054 (void) Psetflags(dpr->dpr_proc, PR_RLC);
1064#else
1065 (void) proc_clearflags(dpr->dpr_proc, PR_KLC);
1066 (void) proc_setflags(dpr->dpr_proc, PR_RLC);
1067#endif
1068
1069 /*
1070 * If we are attempting to grab the process without a monitor
1071 * thread, then mark the process cacheable only if it's being
1072 * grabbed read-only. If we're currently caching more process
1073 * handles than dph_lrulim permits, attempt to find the
1074 * least-recently-used handle that is currently unreferenced and
1075 * release it from the cache. Otherwise we are grabbing the process
1076 * for control: create a control thread for this process and store
1077 * its ID in dpr->dpr_tid.
1078 */
1079 if (nomonitor || (flags & PGRAB_RDONLY)) {
1080 if (dph->dph_lrucnt >= dph->dph_lrulim) {
1081 for (opr = dt_list_prev(&dph->dph_lrulist);
1082 opr != NULL; opr = dt_list_prev(opr)) {
1083 if (opr->dpr_cacheable && opr->dpr_refs == 0) {
1084 dt_proc_destroy(dtp, opr->dpr_proc);
1085 break;
1086 }
1087 }
1088 }
1089
1090 if (flags & PGRAB_RDONLY) {
1091 dpr->dpr_cacheable = B_TRUE;
1092 dpr->dpr_rdonly = B_TRUE;
1093 dph->dph_lrucnt++;
1094 }
1095
1096 } else if (dt_proc_create_thread(dtp, dpr, DT_PROC_STOP_GRAB) != 0)
1097 return (NULL); /* dt_proc_error() has been called for us */
1098
1099 dpr->dpr_hash = dph->dph_hash[h];
1100 dph->dph_hash[h] = dpr;
1101 dt_list_prepend(&dph->dph_lrulist, dpr);
1102
1103 dt_dprintf("grabbed pid %d\n", (int)pid);
1104 dpr->dpr_refs++;
1105
1106 return (dpr->dpr_proc);
1107}
1108
1109void
1110dt_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P)
1111{
1112 dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
1113 dt_proc_hash_t *dph = dtp->dt_procs;
1114
1115 assert(dpr != NULL);
1116 assert(dpr->dpr_refs != 0);
1117
1118 if (--dpr->dpr_refs == 0 &&
1119 (!dpr->dpr_cacheable || dph->dph_lrucnt > dph->dph_lrulim))
1120 dt_proc_destroy(dtp, P);
1121}
1122
1123void
1124dt_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P)
1125{
1126 dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
1127
1128 (void) pthread_mutex_lock(&dpr->dpr_lock);
1129
1130 if (dpr->dpr_stop & DT_PROC_STOP_IDLE) {
1131 dpr->dpr_stop &= ~DT_PROC_STOP_IDLE;
1132 (void) pthread_cond_broadcast(&dpr->dpr_cv);
1133 }
1134
1135 (void) pthread_mutex_unlock(&dpr->dpr_lock);
1136}
1137
1138void
1139dt_proc_lock(dtrace_hdl_t *dtp, struct ps_prochandle *P)
1140{
1141 dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
1142 int err = pthread_mutex_lock(&dpr->dpr_lock);
1143 assert(err == 0); /* check for recursion */
1144}
1145
1146void
1147dt_proc_unlock(dtrace_hdl_t *dtp, struct ps_prochandle *P)
1148{
1149 dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
1150 int err = pthread_mutex_unlock(&dpr->dpr_lock);
1151 assert(err == 0); /* check for unheld lock */
1152}
1153
1154void
1155dt_proc_hash_create(dtrace_hdl_t *dtp)
1156{
1157 if ((dtp->dt_procs = dt_zalloc(dtp, sizeof (dt_proc_hash_t) +
1158 sizeof (dt_proc_t *) * _dtrace_pidbuckets - 1)) != NULL) {
1159
1160 (void) pthread_mutex_init(&dtp->dt_procs->dph_lock, NULL);
1161 (void) pthread_cond_init(&dtp->dt_procs->dph_cv, NULL);
1162
1163 dtp->dt_procs->dph_hashlen = _dtrace_pidbuckets;
1164 dtp->dt_procs->dph_lrulim = _dtrace_pidlrulim;
1165 }
1166}
1167
1168void
1169dt_proc_hash_destroy(dtrace_hdl_t *dtp)
1170{
1171 dt_proc_hash_t *dph = dtp->dt_procs;
1172 dt_proc_t *dpr;
1173
1174 while ((dpr = dt_list_next(&dph->dph_lrulist)) != NULL)
1175 dt_proc_destroy(dtp, dpr->dpr_proc);
1176
1177 dtp->dt_procs = NULL;
1178 dt_free(dtp, dph);
1179}
1180
1181struct ps_prochandle *
1182dtrace_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv,
1183 proc_child_func *pcf, void *child_arg)
1184{
1185 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
1186 struct ps_prochandle *P = dt_proc_create(dtp, file, argv, pcf, child_arg);
1187
1055
1056 /*
1057 * If we are attempting to grab the process without a monitor
1058 * thread, then mark the process cacheable only if it's being
1059 * grabbed read-only. If we're currently caching more process
1060 * handles than dph_lrulim permits, attempt to find the
1061 * least-recently-used handle that is currently unreferenced and
1062 * release it from the cache. Otherwise we are grabbing the process
1063 * for control: create a control thread for this process and store
1064 * its ID in dpr->dpr_tid.
1065 */
1066 if (nomonitor || (flags & PGRAB_RDONLY)) {
1067 if (dph->dph_lrucnt >= dph->dph_lrulim) {
1068 for (opr = dt_list_prev(&dph->dph_lrulist);
1069 opr != NULL; opr = dt_list_prev(opr)) {
1070 if (opr->dpr_cacheable && opr->dpr_refs == 0) {
1071 dt_proc_destroy(dtp, opr->dpr_proc);
1072 break;
1073 }
1074 }
1075 }
1076
1077 if (flags & PGRAB_RDONLY) {
1078 dpr->dpr_cacheable = B_TRUE;
1079 dpr->dpr_rdonly = B_TRUE;
1080 dph->dph_lrucnt++;
1081 }
1082
1083 } else if (dt_proc_create_thread(dtp, dpr, DT_PROC_STOP_GRAB) != 0)
1084 return (NULL); /* dt_proc_error() has been called for us */
1085
1086 dpr->dpr_hash = dph->dph_hash[h];
1087 dph->dph_hash[h] = dpr;
1088 dt_list_prepend(&dph->dph_lrulist, dpr);
1089
1090 dt_dprintf("grabbed pid %d\n", (int)pid);
1091 dpr->dpr_refs++;
1092
1093 return (dpr->dpr_proc);
1094}
1095
1096void
1097dt_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P)
1098{
1099 dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
1100 dt_proc_hash_t *dph = dtp->dt_procs;
1101
1102 assert(dpr != NULL);
1103 assert(dpr->dpr_refs != 0);
1104
1105 if (--dpr->dpr_refs == 0 &&
1106 (!dpr->dpr_cacheable || dph->dph_lrucnt > dph->dph_lrulim))
1107 dt_proc_destroy(dtp, P);
1108}
1109
1110void
1111dt_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P)
1112{
1113 dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
1114
1115 (void) pthread_mutex_lock(&dpr->dpr_lock);
1116
1117 if (dpr->dpr_stop & DT_PROC_STOP_IDLE) {
1118 dpr->dpr_stop &= ~DT_PROC_STOP_IDLE;
1119 (void) pthread_cond_broadcast(&dpr->dpr_cv);
1120 }
1121
1122 (void) pthread_mutex_unlock(&dpr->dpr_lock);
1123}
1124
1125void
1126dt_proc_lock(dtrace_hdl_t *dtp, struct ps_prochandle *P)
1127{
1128 dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
1129 int err = pthread_mutex_lock(&dpr->dpr_lock);
1130 assert(err == 0); /* check for recursion */
1131}
1132
1133void
1134dt_proc_unlock(dtrace_hdl_t *dtp, struct ps_prochandle *P)
1135{
1136 dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
1137 int err = pthread_mutex_unlock(&dpr->dpr_lock);
1138 assert(err == 0); /* check for unheld lock */
1139}
1140
1141void
1142dt_proc_hash_create(dtrace_hdl_t *dtp)
1143{
1144 if ((dtp->dt_procs = dt_zalloc(dtp, sizeof (dt_proc_hash_t) +
1145 sizeof (dt_proc_t *) * _dtrace_pidbuckets - 1)) != NULL) {
1146
1147 (void) pthread_mutex_init(&dtp->dt_procs->dph_lock, NULL);
1148 (void) pthread_cond_init(&dtp->dt_procs->dph_cv, NULL);
1149
1150 dtp->dt_procs->dph_hashlen = _dtrace_pidbuckets;
1151 dtp->dt_procs->dph_lrulim = _dtrace_pidlrulim;
1152 }
1153}
1154
1155void
1156dt_proc_hash_destroy(dtrace_hdl_t *dtp)
1157{
1158 dt_proc_hash_t *dph = dtp->dt_procs;
1159 dt_proc_t *dpr;
1160
1161 while ((dpr = dt_list_next(&dph->dph_lrulist)) != NULL)
1162 dt_proc_destroy(dtp, dpr->dpr_proc);
1163
1164 dtp->dt_procs = NULL;
1165 dt_free(dtp, dph);
1166}
1167
1168struct ps_prochandle *
1169dtrace_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv,
1170 proc_child_func *pcf, void *child_arg)
1171{
1172 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
1173 struct ps_prochandle *P = dt_proc_create(dtp, file, argv, pcf, child_arg);
1174
1188 if (P != NULL && idp != NULL && idp->di_id == 0)
1175 if (P != NULL && idp != NULL && idp->di_id == 0) {
1189#if defined(sun)
1190 idp->di_id = Pstatus(P)->pr_pid; /* $target = created pid */
1191#else
1192 idp->di_id = proc_getpid(P); /* $target = created pid */
1193#endif
1176#if defined(sun)
1177 idp->di_id = Pstatus(P)->pr_pid; /* $target = created pid */
1178#else
1179 idp->di_id = proc_getpid(P); /* $target = created pid */
1180#endif
1181 }
1194
1195 return (P);
1196}
1197
1198struct ps_prochandle *
1199dtrace_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags)
1200{
1201 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
1202 struct ps_prochandle *P = dt_proc_grab(dtp, pid, flags, 0);
1203
1204 if (P != NULL && idp != NULL && idp->di_id == 0)
1205 idp->di_id = pid; /* $target = grabbed pid */
1206
1207 return (P);
1208}
1209
1210void
1211dtrace_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P)
1212{
1213 dt_proc_release(dtp, P);
1214}
1215
1216void
1217dtrace_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P)
1218{
1219 dt_proc_continue(dtp, P);
1220}
1182
1183 return (P);
1184}
1185
1186struct ps_prochandle *
1187dtrace_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags)
1188{
1189 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
1190 struct ps_prochandle *P = dt_proc_grab(dtp, pid, flags, 0);
1191
1192 if (P != NULL && idp != NULL && idp->di_id == 0)
1193 idp->di_id = pid; /* $target = grabbed pid */
1194
1195 return (P);
1196}
1197
1198void
1199dtrace_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P)
1200{
1201 dt_proc_release(dtp, P);
1202}
1203
1204void
1205dtrace_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P)
1206{
1207 dt_proc_continue(dtp, P);
1208}