summaryrefslogtreecommitdiff
path: root/usr/src/lib/libtnfctl/continue.c
blob: cc4ef319d34ad580ec212cf40c95bec20586091e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * interface to continue a target process (DIRECT_MODE) and helper
 * functions needed by this routine.
 */

#include "tnfctl_int.h"
#include "prb_proc.h"
#include "dbg.h"


#include <stdlib.h>
#include <errno.h>

static tnfctl_errcode_t _tnfctl_continue(tnfctl_handle_t *hndl,
    tnfctl_event_t *evt, sigset_t *oldmask, boolean_t watch_forks);
static tnfctl_errcode_t enable_target_state(tnfctl_handle_t *hndl,
    boolean_t watch_forks);
static tnfctl_errcode_t disable_target_state(tnfctl_handle_t *hndl);

/*
 * continue the target process and return the evt it stopped on.
 * If child_hndl is set and we see a fork, return a handle on child
 * process.
 */
tnfctl_errcode_t
tnfctl_continue(tnfctl_handle_t *hndl, tnfctl_event_t *evt,
		tnfctl_handle_t **child_hndl)
{
	tnfctl_errcode_t	prexstat;
	prb_status_t		prbstat;
	boolean_t		lmapok = B_FALSE;
	boolean_t		watch_forks;
	/* set my_evt to something other than TNFCTL_EVENT_TARGGONE */
	tnfctl_event_t		my_evt = TNFCTL_EVENT_EINTR;
	enum event_op_t		dl_evt;
	sigset_t		newmask, oldmask;
	prb_proc_ctl_t		*proc_p;
	prgreg_t		reg0, reg1;

	/* this interface only works for DIRECT_MODE clients */
	if (hndl->mode != DIRECT_MODE)
		return (TNFCTL_ERR_BADARG);

	proc_p = hndl->proc_p;

	if (sigfillset(&newmask) == -1)
		return (tnfctl_status_map(errno));

	watch_forks = (child_hndl != NULL);

	/*
	 * XXXX block all signals.  Synchronous signals like SEGV that
	 * the user could catch and handle will now result in a core dump.
	 * But, this is very unlikely for 2 reasons - most users don't try
	 * to handle synchronous signals - it usually just aborts the process.
	 * And, secondly, the code until we return the original mask is the
	 * place where this synchronous signal would be generated - and, it
	 * is not very much code.
	 */
	if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) == -1)
		return (tnfctl_status_map(errno));

	/*
	 * Target is stopped on entry because tnfctl_continue()
	 * only returns with a stopped target.
	 */

	/* target process shouldn't be stopped when link maps are incosistent */
	while (lmapok == B_FALSE) {
		prexstat = _tnfctl_continue(hndl, &my_evt, &oldmask,
		    watch_forks);
		if (prexstat) {
			if (my_evt == TNFCTL_EVENT_TARGGONE ||
			    my_evt == TNFCTL_EVENT_EXIT) {
				/*
				 * target exited - free obj list and probe
				 * list so that we keep our internal state
				 * correct, else probe control interfaces will
				 * have wrong information.
				 */
			    DBG(fprintf(stderr, "target is gone\n"));
				_tnfctl_free_objs_and_probes(hndl);
				*evt = my_evt;
				return (TNFCTL_ERR_NONE);
			} else if (my_evt == TNFCTL_EVENT_EXEC) {
				*evt = my_evt;
				return (TNFCTL_ERR_NONE);
			} else if (prexstat == TNFCTL_ERR_FILENOTFOUND) {
				return (TNFCTL_ERR_NOPROCESS);
			} else {
				return (prexstat);
			}
		}
		if (my_evt == TNFCTL_EVENT_FORK) {
	/*
	 * sanity check.  we should only get here if child_hndl is set
	 */
		    if (child_hndl) {
			    *evt = my_evt;
			    prbstat = prb_proc_get_r0_r1(proc_p,
				&reg0, &reg1);
			    if (prbstat) {
				prexstat = _tnfctl_map_to_errcode(prbstat);
				return (prexstat);
			    }
			    prexstat = tnfctl_pid_open((pid_t)reg0,
						child_hndl);
			    disable_target_state(*child_hndl);
			    return (prexstat);
			}
			return (TNFCTL_ERR_NONE);
		}

		/*
		 * update state in handle
		 * REMIND: Only need to call _tnfctl_refresh_process on
		 * dlopen or dlclose.  Need to take out other functionality
		 * of refresh_process into a separate function that should
		 * be called here.
		 */
		prexstat = _tnfctl_refresh_process(hndl, &lmapok, &dl_evt);
		if (prexstat && (lmapok == B_TRUE))
			return (prexstat);
		prexstat = TNFCTL_ERR_NONE;
	}
	*evt = my_evt;
	/* see if we have more detail about the event */
	if (dl_evt == EVT_OPEN)
		*evt = TNFCTL_EVENT_DLOPEN;
	else if (dl_evt == EVT_CLOSE)
		*evt = TNFCTL_EVENT_DLCLOSE;

	return (TNFCTL_ERR_NONE);
}

/*
 * Continues target and waits for it to stop.
 *	warning: This routine returns TNFCTL_EVENT_DLOPEN for any kind of
 *	dl activity.  Up to the caller to determine the actual DL event.
 */
static tnfctl_errcode_t
_tnfctl_continue(tnfctl_handle_t *hndl, tnfctl_event_t *evt, sigset_t *oldmask,
    boolean_t watch_forks)
{
	tnfctl_errcode_t	prexstat;
	tnfctl_errcode_t	ret_prexstat = TNFCTL_ERR_NONE;
	prb_status_t		prbstat, prbstat2;
	prb_proc_ctl_t		*proc_p;
	prb_proc_state_t 	state;

	proc_p = hndl->proc_p;

	/* set up state before we run process */
	prexstat = enable_target_state(hndl, watch_forks);
	if (prexstat)
		return (prexstat);

again:

	/* resume target */
	prbstat = prb_proc_cont(proc_p);
	if (prbstat) {
		ret_prexstat = _tnfctl_map_to_errcode(prbstat);
		goto end_of_func;
	}

	/* wait on target to stop (standby) */
	prbstat = prb_proc_wait(proc_p, B_TRUE, oldmask);
	if (prbstat) {
		if (prbstat == EINTR) {
			*evt = TNFCTL_EVENT_EINTR;
			prbstat2 = prb_proc_stop(proc_p);
			if (prbstat2) {
				ret_prexstat = _tnfctl_map_to_errcode(prbstat2);
				goto end_of_func;
			}
		} else if (prbstat == ENOENT) {
			/* target process finished */
			if (hndl->called_exit)
				*evt = TNFCTL_EVENT_EXIT;
			else
				*evt = TNFCTL_EVENT_TARGGONE;
			/* return directly - process no longer around */
			return (TNFCTL_ERR_INTERNAL);
		} else {
			ret_prexstat = _tnfctl_map_to_errcode(prbstat);
			goto end_of_func;
		}
	}

	prbstat = prb_proc_state(proc_p, &state);
	if (prbstat) {
		ret_prexstat = _tnfctl_map_to_errcode(prbstat);
		goto end_of_func;
	}
	if (state.ps_isbptfault) {
		/* dlopen or dlclose */
		prbstat = prb_rtld_advance(proc_p);
		if (prbstat) {
			ret_prexstat = _tnfctl_map_to_errcode(prbstat);
			goto end_of_func;
		}
		/*
		 * actually don't know if it is a dlopen or dlclose yet.
		 * But, we return dlopen here.  Up to the caller to determine
		 * which one it actually is.
		 */
		*evt = TNFCTL_EVENT_DLOPEN;
	} else
	    if (state.ps_issysentry) {
		switch (state.ps_syscallnum) {
		case SYS_execve:
		    *evt = TNFCTL_EVENT_EXEC;
		    ret_prexstat = TNFCTL_ERR_INTERNAL;
		    break;
		case SYS_exit:
		    hndl->called_exit = B_TRUE;
		    goto again;
		default:
		    break;
		}
	    } else if (state.ps_issysexit) {
		    switch (state.ps_syscallnum) {
		    case SYS_vfork:
		    case SYS_forksys:
			*evt = TNFCTL_EVENT_FORK;
			break;
		    default:
			break;
		    }
		}
end_of_func:
	/*
	 * disable all our sycall tracing and bpt setup in process when it
	 * is stopped, so that even if the controlling process aborts,
	 * the target could continue running
	 */
	prexstat = disable_target_state(hndl);
	if (prexstat)
		return (prexstat);
	return (ret_prexstat);
}

/*
 * enable the system call tracing and dl activity tracing
 */
static tnfctl_errcode_t
enable_target_state(tnfctl_handle_t *hndl, boolean_t watch_forks)
{
	prb_status_t	prbstat;
	prb_proc_ctl_t	*proc_p;

	proc_p = hndl->proc_p;

	/* trace exec */
	prbstat = prb_proc_entry(proc_p, SYS_execve, PRB_SYS_ADD);
	if (prbstat)
		return (_tnfctl_map_to_errcode(prbstat));
	/* trace exit */
	prbstat = prb_proc_entry(proc_p, SYS_exit, PRB_SYS_ADD);
	if (prbstat)
		return (_tnfctl_map_to_errcode(prbstat));
	/* trace fork if the caller requests */
	if (watch_forks) {
		prbstat = prb_proc_exit(proc_p, SYS_vfork, PRB_SYS_ADD);
		if (prbstat)
			return (_tnfctl_map_to_errcode(prbstat));

		prbstat = prb_proc_exit(proc_p, SYS_forksys, PRB_SYS_ADD);
		if (prbstat)
			return (_tnfctl_map_to_errcode(prbstat));

		prbstat = prb_proc_setfork(proc_p, B_TRUE);
		if (prbstat)
			return (_tnfctl_map_to_errcode(prbstat));
	}
	/*
	 * tracing flags for fork and exec will get unset when
	 * process stops. see disable_target_state()
	 */

	/* setup process to stop during dlopen() or dlclose() */
	prbstat = prb_rtld_stalk(proc_p);
	return (_tnfctl_map_to_errcode(prbstat));
}

/*
 * disable the system call tracing and dl activity tracing
 */
static tnfctl_errcode_t
disable_target_state(tnfctl_handle_t *hndl)
{
	prb_status_t	prbstat;
	prb_proc_ctl_t	*proc_p;

	proc_p = hndl->proc_p;

	/* remove the stalking breakpoint while the process is stopped */
	prbstat = prb_rtld_unstalk(proc_p);
	if (prbstat)
		return (_tnfctl_map_to_errcode(prbstat));

	/* remove the exec, exit and fork tracing while stopped */
	prbstat = prb_proc_entry(proc_p, SYS_execve, PRB_SYS_DEL);
	if (prbstat)
		return (_tnfctl_map_to_errcode(prbstat));
	prbstat = prb_proc_entry(proc_p, SYS_exit, PRB_SYS_DEL);
	if (prbstat)
		return (_tnfctl_map_to_errcode(prbstat));
	prbstat = prb_proc_exit(proc_p, SYS_vfork, PRB_SYS_DEL);
	if (prbstat)
	    return (_tnfctl_map_to_errcode(prbstat));
	prbstat = prb_proc_exit(proc_p, SYS_forksys, PRB_SYS_DEL);
	if (prbstat)
	    return (_tnfctl_map_to_errcode(prbstat));
	prbstat = prb_proc_setfork(proc_p, B_FALSE);
	if (prbstat)
	    return (_tnfctl_map_to_errcode(prbstat));

	return (TNFCTL_ERR_NONE);
}