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
57c3666b4Skk  * Common Development and Distribution License (the "License").
67c3666b4Skk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21657b1f3dSraf 
227c478bd9Sstevel@tonic-gate /*
238fd04b83SRoger A. Faulkner  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * This file contains a set of generic routines for periodically
297c478bd9Sstevel@tonic-gate  * sampling the state of another process, or tree of processes.
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * It is built upon the infrastructure provided by libproc.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/wait.h>
357c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
367c478bd9Sstevel@tonic-gate #include <sys/time.h>
377c478bd9Sstevel@tonic-gate #include <libproc.h>
387c478bd9Sstevel@tonic-gate #include <stdio.h>
397c478bd9Sstevel@tonic-gate #include <stdlib.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <unistd.h>
427c478bd9Sstevel@tonic-gate #include <signal.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <strings.h>
457c478bd9Sstevel@tonic-gate #include <limits.h>
467c478bd9Sstevel@tonic-gate #include <ctype.h>
477c478bd9Sstevel@tonic-gate #include <libintl.h>
487c478bd9Sstevel@tonic-gate #include <libcpc.h>
497c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include "libpctx.h"
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate struct __pctx {
547c478bd9Sstevel@tonic-gate 	pctx_errfn_t *errfn;
557c478bd9Sstevel@tonic-gate 	struct ps_prochandle *Pr;
567c478bd9Sstevel@tonic-gate 	void *uarg;
577c478bd9Sstevel@tonic-gate 	pctx_sysc_execfn_t *exec;
587c478bd9Sstevel@tonic-gate 	pctx_sysc_forkfn_t *fork;
597c478bd9Sstevel@tonic-gate 	pctx_sysc_exitfn_t *exit;
607c478bd9Sstevel@tonic-gate 	pctx_sysc_lwp_createfn_t *lwp_create;
617c478bd9Sstevel@tonic-gate 	pctx_init_lwpfn_t *init_lwp;
627c478bd9Sstevel@tonic-gate 	pctx_fini_lwpfn_t *fini_lwp;
637c478bd9Sstevel@tonic-gate 	pctx_sysc_lwp_exitfn_t *lwp_exit;
647c478bd9Sstevel@tonic-gate 	int verbose;
657c478bd9Sstevel@tonic-gate 	int created;
667c478bd9Sstevel@tonic-gate 	int sigblocked;
67b885580bSAlexander Kolbasov 	int terminate;
687c478bd9Sstevel@tonic-gate 	sigset_t savedset;
697c478bd9Sstevel@tonic-gate 	cpc_t *cpc;
707c478bd9Sstevel@tonic-gate };
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static void (*pctx_cpc_callback)(cpc_t *cpc, struct __pctx *pctx);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static void
pctx_default_errfn(const char * fn,const char * fmt,va_list ap)757c478bd9Sstevel@tonic-gate pctx_default_errfn(const char *fn, const char *fmt, va_list ap)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "libpctx: pctx_%s: ", fn);
787c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*PRINTFLIKE3*/
827c478bd9Sstevel@tonic-gate static void
pctx_error(pctx_t * pctx,const char * fn,const char * fmt,...)837c478bd9Sstevel@tonic-gate pctx_error(pctx_t *pctx, const char *fn, const char *fmt, ...)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	va_list ap;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
887c478bd9Sstevel@tonic-gate 	pctx->errfn(fn, fmt, ap);
897c478bd9Sstevel@tonic-gate 	va_end(ap);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * Create a new process and bind the user args for it
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate pctx_t *
pctx_create(const char * filename,char * const * argv,void * arg,int verbose,pctx_errfn_t * errfn)967c478bd9Sstevel@tonic-gate pctx_create(
977c478bd9Sstevel@tonic-gate     const char *filename,
987c478bd9Sstevel@tonic-gate     char *const *argv,
997c478bd9Sstevel@tonic-gate     void *arg,
1007c478bd9Sstevel@tonic-gate     int verbose,
1017c478bd9Sstevel@tonic-gate     pctx_errfn_t *errfn)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	static const char fn[] = "create";
1047c478bd9Sstevel@tonic-gate 	int err;
1057c478bd9Sstevel@tonic-gate 	pctx_t *pctx;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	pctx = calloc(1, sizeof (*pctx));
1087c478bd9Sstevel@tonic-gate 	pctx->uarg = arg;
1097c478bd9Sstevel@tonic-gate 	pctx->verbose = verbose;
110b885580bSAlexander Kolbasov 	pctx->terminate = 0;
1117c478bd9Sstevel@tonic-gate 	pctx->errfn = errfn ? errfn : pctx_default_errfn;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if ((pctx->Pr = Pcreate(filename, argv, &err, 0, 0)) == NULL) {
1147c478bd9Sstevel@tonic-gate 		switch (err) {
1157c478bd9Sstevel@tonic-gate 		case C_PERM:
1167c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn, gettext("cannot trace set-id or "
1177c478bd9Sstevel@tonic-gate 			    "unreadable program '%s'\n"), filename);
1187c478bd9Sstevel@tonic-gate 			break;
1197c478bd9Sstevel@tonic-gate 		case C_LP64:
1207c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn, gettext("cannot control LP64 "
1217c478bd9Sstevel@tonic-gate 			    "program '%s'\n"), filename);
1227c478bd9Sstevel@tonic-gate 			break;
1237c478bd9Sstevel@tonic-gate 		case C_NOEXEC:
1247c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn, gettext("cannot execute "
1257c478bd9Sstevel@tonic-gate 			    "program '%s'\n"), filename);
1267c478bd9Sstevel@tonic-gate 			break;
1277c478bd9Sstevel@tonic-gate 		case C_NOENT:
1287c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn, gettext("cannot find"
1297c478bd9Sstevel@tonic-gate 			    "program '%s'\n"), filename);
1307c478bd9Sstevel@tonic-gate 			break;
1317c478bd9Sstevel@tonic-gate 		case C_FORK:
1327c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn, gettext("cannot fork, "
1337c478bd9Sstevel@tonic-gate 			    "program '%s'\n"), filename);
1347c478bd9Sstevel@tonic-gate 			break;
1357c478bd9Sstevel@tonic-gate 		default:
1367c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn, gettext("%s, program '%s'\n"),
1377c478bd9Sstevel@tonic-gate 			    Pcreate_error(err), filename);
1387c478bd9Sstevel@tonic-gate 			break;
1397c478bd9Sstevel@tonic-gate 		}
1407c478bd9Sstevel@tonic-gate 		free(pctx);
1417c478bd9Sstevel@tonic-gate 		return (NULL);
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (Psysentry(pctx->Pr, SYS_exit, 1) == -1) {
1457c478bd9Sstevel@tonic-gate 		pctx_error(pctx, fn,
1467c478bd9Sstevel@tonic-gate 		    gettext("can't stop-on-exit() program '%s'\n"), filename);
1477c478bd9Sstevel@tonic-gate 		Prelease(pctx->Pr, PRELEASE_KILL);
1487c478bd9Sstevel@tonic-gate 		free(pctx);
1497c478bd9Sstevel@tonic-gate 		return (NULL);
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 	/*
1527c478bd9Sstevel@tonic-gate 	 * Set kill-on-last-close so the controlled process
1537c478bd9Sstevel@tonic-gate 	 * dies if we die.
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 	pctx->created = 1;
1567c478bd9Sstevel@tonic-gate 	(void) Psetflags(pctx->Pr, PR_KLC);
1577c478bd9Sstevel@tonic-gate 	(void) pctx_set_events(pctx, PCTX_NULL_EVENT);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	return (pctx);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * Capture an existing process and bind the user args for it
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate pctx_t *
pctx_capture(pid_t pid,void * arg,int verbose,pctx_errfn_t * errfn)1667c478bd9Sstevel@tonic-gate pctx_capture(pid_t pid, void *arg, int verbose, pctx_errfn_t *errfn)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	static const char fn[] = "capture";
1697c478bd9Sstevel@tonic-gate 	int err;
1707c478bd9Sstevel@tonic-gate 	pctx_t *pctx;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	pctx = calloc(1, sizeof (*pctx));
1737c478bd9Sstevel@tonic-gate 	pctx->uarg = arg;
1747c478bd9Sstevel@tonic-gate 	pctx->verbose = verbose;
1757c478bd9Sstevel@tonic-gate 	pctx->errfn = errfn ? errfn : pctx_default_errfn;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if ((pctx->Pr = Pgrab(pid, 0, &err)) == NULL) {
1787c478bd9Sstevel@tonic-gate 		switch (err) {
1797c478bd9Sstevel@tonic-gate 		case G_NOPROC:
1807c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn,
1817c478bd9Sstevel@tonic-gate 			    gettext("pid %d doesn't exist\n"), (int)pid);
1827c478bd9Sstevel@tonic-gate 			break;
1837c478bd9Sstevel@tonic-gate 		case G_ZOMB:
1847c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn,
1857c478bd9Sstevel@tonic-gate 			    gettext("pid %d is a zombie\n"), (int)pid);
1867c478bd9Sstevel@tonic-gate 			break;
1877c478bd9Sstevel@tonic-gate 		case G_PERM:
1887c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn,
1897c478bd9Sstevel@tonic-gate 			    gettext("pid %d: permission denied\n"), (int)pid);
1907c478bd9Sstevel@tonic-gate 			break;
1917c478bd9Sstevel@tonic-gate 		case G_BUSY:
1927c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn,
1937c478bd9Sstevel@tonic-gate 			    gettext("pid %d is already being traced\n"),
1947c478bd9Sstevel@tonic-gate 			    (int)pid);
1957c478bd9Sstevel@tonic-gate 			break;
1967c478bd9Sstevel@tonic-gate 		case G_SYS:
1977c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn,
1987c478bd9Sstevel@tonic-gate 			    gettext("pid %d is a system process\n"), (int)pid);
1997c478bd9Sstevel@tonic-gate 			break;
2007c478bd9Sstevel@tonic-gate 		case G_SELF:
2017c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn,
2027c478bd9Sstevel@tonic-gate 			    gettext("cannot capture self!\n"));
2037c478bd9Sstevel@tonic-gate 			break;
2047c478bd9Sstevel@tonic-gate 		case G_LP64:
2057c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn, gettext("cannot control LP64 "
2067c478bd9Sstevel@tonic-gate 			    "process, pid %d\n"), (int)pid);
2077c478bd9Sstevel@tonic-gate 			break;
2087c478bd9Sstevel@tonic-gate 		default:
2097c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn, gettext("%s: pid %d\n"),
2107c478bd9Sstevel@tonic-gate 			    Pgrab_error(err), (int)pid);
2117c478bd9Sstevel@tonic-gate 			break;
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 		free(pctx);
2147c478bd9Sstevel@tonic-gate 		return (NULL);
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	if (Psysentry(pctx->Pr, SYS_exit, 1) == -1) {
2187c478bd9Sstevel@tonic-gate 		pctx_error(pctx, fn,
2197c478bd9Sstevel@tonic-gate 		    gettext("can't stop-on-exit() pid %d\n"), (int)pid);
2207c478bd9Sstevel@tonic-gate 		Prelease(pctx->Pr, PRELEASE_CLEAR);
2217c478bd9Sstevel@tonic-gate 		free(pctx);
2227c478bd9Sstevel@tonic-gate 		return (NULL);
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/*
2267c478bd9Sstevel@tonic-gate 	 * Set run-on-last-close so the controlled process
2277c478bd9Sstevel@tonic-gate 	 * runs even if we die on a signal.  This is because
2287c478bd9Sstevel@tonic-gate 	 * we grabbed an existing process - it would be impolite
2297c478bd9Sstevel@tonic-gate 	 * to cause it to die if we exit prematurely.
2307c478bd9Sstevel@tonic-gate 	 */
2317c478bd9Sstevel@tonic-gate 	pctx->created = 0;
2327c478bd9Sstevel@tonic-gate 	(void) Psetflags(pctx->Pr, PR_RLC);
2337c478bd9Sstevel@tonic-gate 	(void) pctx_set_events(pctx, PCTX_NULL_EVENT);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	return (pctx);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2397c478bd9Sstevel@tonic-gate static void
default_void(pctx_t * pctx)2407c478bd9Sstevel@tonic-gate default_void(pctx_t *pctx)
2417c478bd9Sstevel@tonic-gate {}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2447c478bd9Sstevel@tonic-gate static int
default_int(pctx_t * pctx)2457c478bd9Sstevel@tonic-gate default_int(pctx_t *pctx)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 	return (0);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate int
pctx_set_events(pctx_t * pctx,...)2517c478bd9Sstevel@tonic-gate pctx_set_events(pctx_t *pctx, ...)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	static const char fn[] = "set_events";
2547c478bd9Sstevel@tonic-gate 	va_list pvar;
2557c478bd9Sstevel@tonic-gate 	int error = 0;
2567c478bd9Sstevel@tonic-gate 	pctx_event_t event;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	va_start(pvar, pctx);
2597c478bd9Sstevel@tonic-gate 	do {
2607c478bd9Sstevel@tonic-gate 		switch (event = (pctx_event_t)va_arg(pvar, pctx_event_t)) {
2617c478bd9Sstevel@tonic-gate 		case PCTX_NULL_EVENT:
2627c478bd9Sstevel@tonic-gate 			break;
2637c478bd9Sstevel@tonic-gate 		case PCTX_SYSC_EXEC_EVENT:
2647c478bd9Sstevel@tonic-gate 			pctx->exec = (pctx_sysc_execfn_t *)
2657c478bd9Sstevel@tonic-gate 			    va_arg(pvar, pctx_sysc_execfn_t *);
2667c478bd9Sstevel@tonic-gate 			break;
2677c478bd9Sstevel@tonic-gate 		case PCTX_SYSC_FORK_EVENT:
2687c478bd9Sstevel@tonic-gate 			pctx->fork = (pctx_sysc_forkfn_t *)
2697c478bd9Sstevel@tonic-gate 			    va_arg(pvar, pctx_sysc_forkfn_t *);
2707c478bd9Sstevel@tonic-gate 			break;
2717c478bd9Sstevel@tonic-gate 		case PCTX_SYSC_EXIT_EVENT:	/* always intercepted */
2727c478bd9Sstevel@tonic-gate 			pctx->exit = (pctx_sysc_exitfn_t *)
2737c478bd9Sstevel@tonic-gate 			    va_arg(pvar, pctx_sysc_exitfn_t *);
2747c478bd9Sstevel@tonic-gate 			break;
2757c478bd9Sstevel@tonic-gate 		case PCTX_SYSC_LWP_CREATE_EVENT:
2767c478bd9Sstevel@tonic-gate 			pctx->lwp_create = (pctx_sysc_lwp_createfn_t *)
2777c478bd9Sstevel@tonic-gate 			    va_arg(pvar, pctx_sysc_lwp_createfn_t *);
2787c478bd9Sstevel@tonic-gate 			break;
2797c478bd9Sstevel@tonic-gate 		case PCTX_INIT_LWP_EVENT:
2807c478bd9Sstevel@tonic-gate 			pctx->init_lwp = (pctx_init_lwpfn_t *)
2817c478bd9Sstevel@tonic-gate 			    va_arg(pvar, pctx_init_lwpfn_t *);
2827c478bd9Sstevel@tonic-gate 			break;
2837c478bd9Sstevel@tonic-gate 		case PCTX_FINI_LWP_EVENT:
2847c478bd9Sstevel@tonic-gate 			pctx->fini_lwp = (pctx_fini_lwpfn_t *)
2857c478bd9Sstevel@tonic-gate 			    va_arg(pvar, pctx_fini_lwpfn_t *);
2867c478bd9Sstevel@tonic-gate 			break;
2877c478bd9Sstevel@tonic-gate 		case PCTX_SYSC_LWP_EXIT_EVENT:
2887c478bd9Sstevel@tonic-gate 			pctx->lwp_exit = (pctx_sysc_lwp_exitfn_t *)
2897c478bd9Sstevel@tonic-gate 			    va_arg(pvar, pctx_sysc_lwp_exitfn_t *);
2907c478bd9Sstevel@tonic-gate 			break;
2917c478bd9Sstevel@tonic-gate 		default:
2927c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn,
2937c478bd9Sstevel@tonic-gate 			    gettext("unknown event type %x\n"), event);
2947c478bd9Sstevel@tonic-gate 			error = -1;
2957c478bd9Sstevel@tonic-gate 			break;
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 	} while (event != PCTX_NULL_EVENT && error == 0);
2987c478bd9Sstevel@tonic-gate 	va_end(pvar);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if (error != 0)
3017c478bd9Sstevel@tonic-gate 		return (error);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (pctx->exec == NULL)
304*bf6cb86eSToomas Soome 		pctx->exec = (pctx_sysc_execfn_t *)(uintptr_t)default_int;
3057c478bd9Sstevel@tonic-gate 	if (pctx->fork == NULL)
306*bf6cb86eSToomas Soome 		pctx->fork = (pctx_sysc_forkfn_t *)(uintptr_t)default_void;
3077c478bd9Sstevel@tonic-gate 	if (pctx->exit == NULL)
308*bf6cb86eSToomas Soome 		pctx->exit = (pctx_sysc_exitfn_t *)(uintptr_t)default_void;
3097c478bd9Sstevel@tonic-gate 	if (pctx->lwp_create == NULL)
310*bf6cb86eSToomas Soome 		pctx->lwp_create = (pctx_sysc_lwp_createfn_t *)
311*bf6cb86eSToomas Soome 		    (uintptr_t)default_int;
3127c478bd9Sstevel@tonic-gate 	if (pctx->init_lwp == NULL)
313*bf6cb86eSToomas Soome 		pctx->init_lwp = (pctx_init_lwpfn_t *)(uintptr_t)default_int;
3147c478bd9Sstevel@tonic-gate 	if (pctx->fini_lwp == NULL)
315*bf6cb86eSToomas Soome 		pctx->fini_lwp = (pctx_fini_lwpfn_t *)(uintptr_t)default_int;
3167c478bd9Sstevel@tonic-gate 	if (pctx->lwp_exit == NULL)
317*bf6cb86eSToomas Soome 		pctx->lwp_exit = (pctx_sysc_lwp_exitfn_t *)
318*bf6cb86eSToomas Soome 		    (uintptr_t)default_int;
3197c478bd9Sstevel@tonic-gate 
320*bf6cb86eSToomas Soome 	if ((uintptr_t)pctx->fork != (uintptr_t)default_void) {
3217c478bd9Sstevel@tonic-gate 		(void) Psysexit(pctx->Pr, SYS_vfork, 1);
322657b1f3dSraf 		(void) Psysexit(pctx->Pr, SYS_forksys, 1);
3237c478bd9Sstevel@tonic-gate 		if (Psetflags(pctx->Pr, PR_FORK) == -1)
3247c478bd9Sstevel@tonic-gate 			error = -1;
3257c478bd9Sstevel@tonic-gate 	} else {
3267c478bd9Sstevel@tonic-gate 		(void) Psysexit(pctx->Pr, SYS_vfork, 0);
327657b1f3dSraf 		(void) Psysexit(pctx->Pr, SYS_forksys, 0);
3287c478bd9Sstevel@tonic-gate 		if (Punsetflags(pctx->Pr, PR_FORK) == -1)
3297c478bd9Sstevel@tonic-gate 			error = -1;
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * exec causes termination of all but the exec-ing lwp,
3347c478bd9Sstevel@tonic-gate 	 * and resets the lwpid to one in the new address space.
3357c478bd9Sstevel@tonic-gate 	 */
336*bf6cb86eSToomas Soome 	if ((uintptr_t)pctx->exec != (uintptr_t)default_int ||
337*bf6cb86eSToomas Soome 	    (uintptr_t)pctx->fini_lwp != (uintptr_t)default_int ||
338*bf6cb86eSToomas Soome 	    (uintptr_t)pctx->init_lwp != (uintptr_t)default_int) {
3397c478bd9Sstevel@tonic-gate 		(void) Psysexit(pctx->Pr, SYS_execve, 1);
3407c478bd9Sstevel@tonic-gate 		(void) Psysentry(pctx->Pr, SYS_execve, 1);
3417c478bd9Sstevel@tonic-gate 	} else {
3427c478bd9Sstevel@tonic-gate 		(void) Psysexit(pctx->Pr, SYS_execve, 0);
3437c478bd9Sstevel@tonic-gate 		(void) Psysentry(pctx->Pr, SYS_execve, 0);
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	(void) Psysexit(pctx->Pr, SYS_lwp_create,
347*bf6cb86eSToomas Soome 	    (uintptr_t)pctx->lwp_create != (uintptr_t)default_int ||
348*bf6cb86eSToomas Soome 	    (uintptr_t)pctx->init_lwp != (uintptr_t)default_int);
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	(void) Psysentry(pctx->Pr, SYS_lwp_exit,
351*bf6cb86eSToomas Soome 	    (uintptr_t)pctx->lwp_exit != (uintptr_t)default_int ||
352*bf6cb86eSToomas Soome 	    (uintptr_t)pctx->fini_lwp != (uintptr_t)default_int);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	return (0);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate static sigset_t termsig;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate static void
__libpctx_init(void)3607c478bd9Sstevel@tonic-gate __libpctx_init(void)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate 	/*
3637c478bd9Sstevel@tonic-gate 	 * Initialize the signal set used to shield ourselves from
3647c478bd9Sstevel@tonic-gate 	 * death-by-terminal-signal while the agent lwp is running.
3657c478bd9Sstevel@tonic-gate 	 */
3667c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&termsig);
3677c478bd9Sstevel@tonic-gate 	(void) sigaddset(&termsig, SIGHUP);
3687c478bd9Sstevel@tonic-gate 	(void) sigaddset(&termsig, SIGTERM);
3697c478bd9Sstevel@tonic-gate 	(void) sigaddset(&termsig, SIGINT);
3707c478bd9Sstevel@tonic-gate 	(void) sigaddset(&termsig, SIGQUIT);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate #pragma init(__libpctx_init)
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate static void
pctx_begin_syscalls(pctx_t * pctx)3767c478bd9Sstevel@tonic-gate pctx_begin_syscalls(pctx_t *pctx)
3777c478bd9Sstevel@tonic-gate {
3787c478bd9Sstevel@tonic-gate 	if (pctx->Pr == NULL)
3797c478bd9Sstevel@tonic-gate 		return;
3807c478bd9Sstevel@tonic-gate 	if (pctx->sigblocked++ == 0) {
3817c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_BLOCK, &termsig, &pctx->savedset);
3827c478bd9Sstevel@tonic-gate 		(void) Pcreate_agent(pctx->Pr);
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate static void
pctx_end_syscalls(pctx_t * pctx)3877c478bd9Sstevel@tonic-gate pctx_end_syscalls(pctx_t *pctx)
3887c478bd9Sstevel@tonic-gate {
3897c478bd9Sstevel@tonic-gate 	if (pctx->Pr == NULL)
3907c478bd9Sstevel@tonic-gate 		return;
3917c478bd9Sstevel@tonic-gate 	if (--pctx->sigblocked == 0) {
3927c478bd9Sstevel@tonic-gate 		(void) Pdestroy_agent(pctx->Pr);
3937c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_SETMASK, &pctx->savedset, NULL);
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate /*
3987c478bd9Sstevel@tonic-gate  * Iterate over the valid lwpids in the process, invoking the
3997c478bd9Sstevel@tonic-gate  * action function on each one.
4007c478bd9Sstevel@tonic-gate  */
4017c478bd9Sstevel@tonic-gate static int
pctx_lwpiterate(pctx_t * pctx,int (* action)(pctx_t *,pid_t,id_t,void *))4027c478bd9Sstevel@tonic-gate pctx_lwpiterate(pctx_t *pctx, int (*action)(pctx_t *, pid_t, id_t, void *))
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	const pstatus_t *pstatus;
4057c478bd9Sstevel@tonic-gate 	char lstatus[64];
4067c478bd9Sstevel@tonic-gate 	struct stat statb;
4077c478bd9Sstevel@tonic-gate 	lwpstatus_t *lwps;
4087c478bd9Sstevel@tonic-gate 	prheader_t *prh;
4097c478bd9Sstevel@tonic-gate 	int fd, nlwp;
4107c478bd9Sstevel@tonic-gate 	int ret = 0;
4117c478bd9Sstevel@tonic-gate 
412*bf6cb86eSToomas Soome 	if ((uintptr_t)action == (uintptr_t)default_int)
4137c478bd9Sstevel@tonic-gate 		return (0);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	pstatus = Pstatus(pctx->Pr);
4167c478bd9Sstevel@tonic-gate 	if (pstatus->pr_nlwp <= 1) {
4177c478bd9Sstevel@tonic-gate 		pctx_begin_syscalls(pctx);
4187c478bd9Sstevel@tonic-gate 		ret = action(pctx, pstatus->pr_pid, 1, pctx->uarg);
4197c478bd9Sstevel@tonic-gate 		pctx_end_syscalls(pctx);
4207c478bd9Sstevel@tonic-gate 		return (ret);
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	(void) snprintf(lstatus, sizeof (lstatus),
4247c478bd9Sstevel@tonic-gate 	    "/proc/%d/lstatus", (int)pstatus->pr_pid);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if ((fd = open(lstatus, O_RDONLY)) < 0 ||
4277c478bd9Sstevel@tonic-gate 	    fstat(fd, &statb) != 0) {
4287c478bd9Sstevel@tonic-gate 		if (fd >= 0)
4297c478bd9Sstevel@tonic-gate 			(void) close(fd);
4307c478bd9Sstevel@tonic-gate 		return (-1);
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	prh = malloc(statb.st_size);
4347c478bd9Sstevel@tonic-gate 	if (read(fd, prh, statb.st_size) <
4357c478bd9Sstevel@tonic-gate 	    sizeof (prheader_t) + sizeof (lwpstatus_t)) {
4367c478bd9Sstevel@tonic-gate 		(void) close(fd);
4377c478bd9Sstevel@tonic-gate 		free(prh);
4387c478bd9Sstevel@tonic-gate 		return (-1);
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 	(void) close(fd);
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
4437c478bd9Sstevel@tonic-gate 	lwps = (lwpstatus_t *)(prh + 1);
4447c478bd9Sstevel@tonic-gate 	pctx_begin_syscalls(pctx);
4457c478bd9Sstevel@tonic-gate 	for (nlwp = prh->pr_nent; nlwp > 0; nlwp--) {
4467c478bd9Sstevel@tonic-gate 		if (action(pctx,
4477c478bd9Sstevel@tonic-gate 		    pstatus->pr_pid, lwps->pr_lwpid, pctx->uarg) != 0)
4487c478bd9Sstevel@tonic-gate 			ret = -1;
4497c478bd9Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
4507c478bd9Sstevel@tonic-gate 		lwps = (lwpstatus_t *)((char *)lwps + prh->pr_entsize);
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 	pctx_end_syscalls(pctx);
4537c478bd9Sstevel@tonic-gate 	free(prh);
4547c478bd9Sstevel@tonic-gate 	return (ret);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate /*
4587c478bd9Sstevel@tonic-gate  * Free any associated state, but leave the process stopped if it
4597c478bd9Sstevel@tonic-gate  * is still under our control.  (If it isn't under our control,
4607c478bd9Sstevel@tonic-gate  * it should just run to completion when we do our last close)
4617c478bd9Sstevel@tonic-gate  */
4627c478bd9Sstevel@tonic-gate static void
pctx_free(pctx_t * pctx)4637c478bd9Sstevel@tonic-gate pctx_free(pctx_t *pctx)
4647c478bd9Sstevel@tonic-gate {
4657c478bd9Sstevel@tonic-gate 	if (pctx->cpc != NULL && pctx_cpc_callback != NULL)
4667c478bd9Sstevel@tonic-gate 		(*pctx_cpc_callback)(pctx->cpc, pctx);
4677c478bd9Sstevel@tonic-gate 	if (pctx->Pr) {
4687c478bd9Sstevel@tonic-gate 		Pfree(pctx->Pr);
4697c478bd9Sstevel@tonic-gate 		pctx->Pr = NULL;
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 	pctx->errfn = pctx_default_errfn;
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate /*
4757c478bd9Sstevel@tonic-gate  * Completely release the process from our control and discard all our state
4767c478bd9Sstevel@tonic-gate  */
4777c478bd9Sstevel@tonic-gate void
pctx_release(pctx_t * pctx)4787c478bd9Sstevel@tonic-gate pctx_release(pctx_t *pctx)
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate 	if (pctx->Pr) {
4817c478bd9Sstevel@tonic-gate 		Prelease(pctx->Pr, PRELEASE_CLEAR);
4827c478bd9Sstevel@tonic-gate 		pctx->Pr = NULL;
4837c478bd9Sstevel@tonic-gate 	}
484b885580bSAlexander Kolbasov 
4857c478bd9Sstevel@tonic-gate 	pctx_free(pctx);
4867c478bd9Sstevel@tonic-gate 	bzero(pctx, sizeof (*pctx));
4877c478bd9Sstevel@tonic-gate 	free(pctx);
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate static void
msincr(struct timeval * tv,uint_t msec)4917c478bd9Sstevel@tonic-gate msincr(struct timeval *tv, uint_t msec)
4927c478bd9Sstevel@tonic-gate {
4937c478bd9Sstevel@tonic-gate 	tv->tv_sec += msec / MILLISEC;
4947c478bd9Sstevel@tonic-gate 	tv->tv_usec += (msec % MILLISEC) * MILLISEC;
4957c478bd9Sstevel@tonic-gate 	if (tv->tv_usec > MICROSEC) {
4967c478bd9Sstevel@tonic-gate 		tv->tv_sec++;
4977c478bd9Sstevel@tonic-gate 		tv->tv_usec -= MICROSEC;
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate static uint_t
msdiff(struct timeval * tva,struct timeval * tvb)5027c478bd9Sstevel@tonic-gate msdiff(struct timeval *tva, struct timeval *tvb)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	time_t sdiff = tva->tv_sec - tvb->tv_sec;
5057c478bd9Sstevel@tonic-gate 	suseconds_t udiff = tva->tv_usec - tvb->tv_usec;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	if (sdiff < 0)
5087c478bd9Sstevel@tonic-gate 		return (0);
5097c478bd9Sstevel@tonic-gate 	if (udiff < 0) {
5107c478bd9Sstevel@tonic-gate 		udiff += MICROSEC;
5117c478bd9Sstevel@tonic-gate 		sdiff--;
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 	if (sdiff < 0)
5147c478bd9Sstevel@tonic-gate 		return (0);
5157c478bd9Sstevel@tonic-gate 	if (sdiff >= (INT_MAX / MILLISEC))
5167c478bd9Sstevel@tonic-gate 		return ((uint_t)INT_MAX);
5177c478bd9Sstevel@tonic-gate 	return ((uint_t)(sdiff * MILLISEC + udiff / MILLISEC));
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate int
pctx_run(pctx_t * pctx,uint_t msec,uint_t nsamples,int (* tick)(pctx_t *,pid_t,id_t,void *))5217c478bd9Sstevel@tonic-gate pctx_run(
5227c478bd9Sstevel@tonic-gate 	pctx_t *pctx,
5237c478bd9Sstevel@tonic-gate 	uint_t msec,
5247c478bd9Sstevel@tonic-gate 	uint_t nsamples,
5257c478bd9Sstevel@tonic-gate 	int (*tick)(pctx_t *, pid_t, id_t, void *))
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate 	static const char fn[] = "run";
5287c478bd9Sstevel@tonic-gate 	struct timeval tvgoal, tvnow;
5297c478bd9Sstevel@tonic-gate 	uint_t mswait = 0;
5307c478bd9Sstevel@tonic-gate 	int running = 1;
5317c478bd9Sstevel@tonic-gate 	const pstatus_t *pstatus;
5327c478bd9Sstevel@tonic-gate 	psinfo_t psinfo;
5337c478bd9Sstevel@tonic-gate 	void (*sigsaved)();
5347c478bd9Sstevel@tonic-gate 	id_t lwpid;
5357c478bd9Sstevel@tonic-gate 	pid_t pid = Pstatus(pctx->Pr)->pr_pid;
5367c478bd9Sstevel@tonic-gate 	int pstate;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	if (msec == 0)
5397c478bd9Sstevel@tonic-gate 		nsamples = 0;
5407c478bd9Sstevel@tonic-gate 	if (nsamples == 0)
5417c478bd9Sstevel@tonic-gate 		nsamples = UINT_MAX;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	/*
5447c478bd9Sstevel@tonic-gate 	 * Casually discard any knowledge of the children we create
5457c478bd9Sstevel@tonic-gate 	 */
5467c478bd9Sstevel@tonic-gate 	sigsaved = signal(SIGCHLD, SIG_IGN);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/*
5497c478bd9Sstevel@tonic-gate 	 * Since we've just "discovered" this process which might have
5507c478bd9Sstevel@tonic-gate 	 * been running for weeks, deliver some init_lwp events so
5517c478bd9Sstevel@tonic-gate 	 * that our caller gets a handle on the process.
5527c478bd9Sstevel@tonic-gate 	 */
5537c478bd9Sstevel@tonic-gate 	if (pctx_lwpiterate(pctx, pctx->init_lwp) != 0) {
5547c478bd9Sstevel@tonic-gate 		if (pctx->verbose)
5557c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn,
5567c478bd9Sstevel@tonic-gate 			    gettext("%d: lwp discovery failed\n"), (int)pid);
5577c478bd9Sstevel@tonic-gate 		goto bailout;
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	if (msec != 0) {
5617c478bd9Sstevel@tonic-gate 		/*
5627c478bd9Sstevel@tonic-gate 		 * tvgoal represents the time at which the sample
5637c478bd9Sstevel@tonic-gate 		 * should next be taken.
5647c478bd9Sstevel@tonic-gate 		 */
5657c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&tvgoal, 0);
5667c478bd9Sstevel@tonic-gate 		msincr(&tvgoal, msec);
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 
5697c3666b4Skk 	/*
5707c3666b4Skk 	 * The event handling loop continues while running is 1.
5717c3666b4Skk 	 * running becomes 0 when either the controlled process has
5727c3666b4Skk 	 * exited successfully or the number of time samples has expired.
5737c3666b4Skk 	 * Otherwise, if an error has occurred, running becomes -1.
5747c3666b4Skk 	 */
575b885580bSAlexander Kolbasov 	while (running == 1 && !pctx->terminate) {
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 		if (Psetrun(pctx->Pr, 0, 0) != 0) {
5787c478bd9Sstevel@tonic-gate 			if (pctx->verbose)
5797c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
5807c478bd9Sstevel@tonic-gate 				    gettext("%d: Psetrun\n"), (int)pid);
5817c478bd9Sstevel@tonic-gate 			break;
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 		if (msec != 0) {
5857c478bd9Sstevel@tonic-gate 			/*
5867c478bd9Sstevel@tonic-gate 			 * This timing loop attempts to estimate the number
5877c478bd9Sstevel@tonic-gate 			 * of milliseconds between our "goal" time (when
5887c478bd9Sstevel@tonic-gate 			 * we should stop the process and run the tick
5897c478bd9Sstevel@tonic-gate 			 * routine) and the current time.
5907c478bd9Sstevel@tonic-gate 			 *
5917c478bd9Sstevel@tonic-gate 			 * If we ever find ourselves running behind i.e. we
5927c478bd9Sstevel@tonic-gate 			 * missed our goal, then we skip ahead to the next
5937c478bd9Sstevel@tonic-gate 			 * goal instead.
5947c478bd9Sstevel@tonic-gate 			 */
5957c478bd9Sstevel@tonic-gate 			do {
5967c478bd9Sstevel@tonic-gate 				(void) gettimeofday(&tvnow, 0);
5977c478bd9Sstevel@tonic-gate 				if ((mswait = msdiff(&tvgoal, &tvnow)) == 0) {
5987c478bd9Sstevel@tonic-gate 					msincr(&tvgoal, msec);
5997c478bd9Sstevel@tonic-gate 					/*
6007c478bd9Sstevel@tonic-gate 					 * Skip ahead to the next goal, unless
6017c478bd9Sstevel@tonic-gate 					 * there is only one more sample left
6027c478bd9Sstevel@tonic-gate 					 * to take.
6037c478bd9Sstevel@tonic-gate 					 */
6047c478bd9Sstevel@tonic-gate 					if (nsamples != 1)
6057c478bd9Sstevel@tonic-gate 						nsamples--;
6067c478bd9Sstevel@tonic-gate 				}
607b885580bSAlexander Kolbasov 			} while (mswait == 0 && !pctx->terminate);
6087c478bd9Sstevel@tonic-gate 		}
6097c478bd9Sstevel@tonic-gate 
610b885580bSAlexander Kolbasov 		if (pctx->terminate)
611b885580bSAlexander Kolbasov 			goto bailout;
612b885580bSAlexander Kolbasov 		else
613b885580bSAlexander Kolbasov 			(void) Pwait(pctx->Pr, mswait);
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate checkstate:
6167c478bd9Sstevel@tonic-gate 		switch (pstate = Pstate(pctx->Pr)) {
6177c478bd9Sstevel@tonic-gate 		case PS_RUN:
6187c478bd9Sstevel@tonic-gate 			/*
6197c478bd9Sstevel@tonic-gate 			 * Try again, but wait for up to 5 seconds.
6207c478bd9Sstevel@tonic-gate 			 */
6217c478bd9Sstevel@tonic-gate 			if (Pstop(pctx->Pr, 5 * MILLISEC) == -1 ||
6227c478bd9Sstevel@tonic-gate 			    (pstate = Pstate(pctx->Pr)) != PS_STOP) {
6237c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
6247c478bd9Sstevel@tonic-gate 				    gettext("%d: won't stop\n"), (int)pid);
6257c478bd9Sstevel@tonic-gate 			}
6267c478bd9Sstevel@tonic-gate 			break;
6277c478bd9Sstevel@tonic-gate 		case PS_STOP:
6287c478bd9Sstevel@tonic-gate 			break;
6297c478bd9Sstevel@tonic-gate 		case PS_LOST:
6307c478bd9Sstevel@tonic-gate 			/*
6317c478bd9Sstevel@tonic-gate 			 * Lost control - probably execed a setuid/setgid
6327c478bd9Sstevel@tonic-gate 			 * executable.  Try and get control back again,
6337c478bd9Sstevel@tonic-gate 			 * else bail ..
6347c478bd9Sstevel@tonic-gate 			 */
6357c478bd9Sstevel@tonic-gate 			(void) Preopen(pctx->Pr);
6367c478bd9Sstevel@tonic-gate 			if ((pstate = Pstate(pctx->Pr)) != PS_LOST)
6377c478bd9Sstevel@tonic-gate 				goto checkstate;
6387c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn,
6397c478bd9Sstevel@tonic-gate 			    gettext("%d: execed a program that cannot "
6407c478bd9Sstevel@tonic-gate 			    "be tracked\n"), (int)pid);
6417c3666b4Skk 			running = -1;
6427c478bd9Sstevel@tonic-gate 			break;
6437c478bd9Sstevel@tonic-gate 		case PS_UNDEAD:
6447c478bd9Sstevel@tonic-gate 		case PS_DEAD:
6457c478bd9Sstevel@tonic-gate 			if (pctx->verbose)
6467c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
6477c478bd9Sstevel@tonic-gate 				    gettext("%d: process terminated\n"),
6487c478bd9Sstevel@tonic-gate 				    (int)pid);
6497c3666b4Skk 			running = -1;
6507c478bd9Sstevel@tonic-gate 			break;
6517c478bd9Sstevel@tonic-gate 		default:
6527c478bd9Sstevel@tonic-gate 			if (pctx->verbose)
6537c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
6547c478bd9Sstevel@tonic-gate 				    gettext("%d: process state 0x%x?\n"),
6557c478bd9Sstevel@tonic-gate 				    (int)pid, pstate);
6567c478bd9Sstevel@tonic-gate 			break;
6577c478bd9Sstevel@tonic-gate 		}
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 		if (pstate != PS_STOP)
6607c478bd9Sstevel@tonic-gate 			break;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 		pstatus = Pstatus(pctx->Pr);
6637c478bd9Sstevel@tonic-gate 		lwpid = pstatus->pr_lwp.pr_lwpid;
6647c478bd9Sstevel@tonic-gate 		switch (pstatus->pr_lwp.pr_why) {
6657c478bd9Sstevel@tonic-gate 		case PR_REQUESTED:
6667c478bd9Sstevel@tonic-gate 			msincr(&tvgoal, msec);
6677c478bd9Sstevel@tonic-gate 			if (pstatus->pr_flags & PR_VFORKP) {
6687c478bd9Sstevel@tonic-gate 				/*
6697c478bd9Sstevel@tonic-gate 				 * The process is in a vfork stupor until
6707c478bd9Sstevel@tonic-gate 				 * its child releases it via an exec.
6717c478bd9Sstevel@tonic-gate 				 * Don't sample it while it's in this state
6727c478bd9Sstevel@tonic-gate 				 * - we won't be able to create the agent.
6737c478bd9Sstevel@tonic-gate 				 */
6747c478bd9Sstevel@tonic-gate 				break;
6757c478bd9Sstevel@tonic-gate 			}
6767c478bd9Sstevel@tonic-gate 			if (pctx_lwpiterate(pctx, tick) != 0)
6777c3666b4Skk 				running = -1;
6787c3666b4Skk 			if (running == 1 && --nsamples == 0)
6797c478bd9Sstevel@tonic-gate 				running = 0;
6807c478bd9Sstevel@tonic-gate 			break;
6817c478bd9Sstevel@tonic-gate 		case PR_SYSENTRY:
6827c478bd9Sstevel@tonic-gate 			switch (pstatus->pr_lwp.pr_what) {
6837c478bd9Sstevel@tonic-gate 			case SYS_lwp_exit:
6847c478bd9Sstevel@tonic-gate 				pctx_begin_syscalls(pctx);
6857c478bd9Sstevel@tonic-gate 				(void) pctx->fini_lwp(pctx,
6867c478bd9Sstevel@tonic-gate 				    pid, lwpid, pctx->uarg);
6877c478bd9Sstevel@tonic-gate 				(void) pctx->lwp_exit(pctx,
6887c478bd9Sstevel@tonic-gate 				    pid, lwpid, pctx->uarg);
6897c478bd9Sstevel@tonic-gate 				pctx_end_syscalls(pctx);
6907c478bd9Sstevel@tonic-gate 				break;
6917c478bd9Sstevel@tonic-gate 			case SYS_exit:
6927c3666b4Skk 				if (pctx_lwpiterate(pctx, pctx->fini_lwp)
6937c3666b4Skk 				    != 0)
6947c3666b4Skk 					running = -1;
6957c478bd9Sstevel@tonic-gate 				pctx->exit(pctx, pid, lwpid,
6967c478bd9Sstevel@tonic-gate 				    (int)pstatus->pr_lwp.pr_sysarg[0],
6977c478bd9Sstevel@tonic-gate 				    pctx->uarg);
6987c3666b4Skk 				if (running == 1)
6997c3666b4Skk 					running = 0;
7007c478bd9Sstevel@tonic-gate 				break;
7017c478bd9Sstevel@tonic-gate 			case SYS_execve:
7027c478bd9Sstevel@tonic-gate 				(void) pctx_lwpiterate(pctx, pctx->fini_lwp);
7037c478bd9Sstevel@tonic-gate 				break;
7047c478bd9Sstevel@tonic-gate 			default:
7057c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
7067c478bd9Sstevel@tonic-gate 				    "warning - pid %d sysentry(%d)\n",
7077c478bd9Sstevel@tonic-gate 				    (int)pid, pstatus->pr_lwp.pr_what);
7087c478bd9Sstevel@tonic-gate 				break;
7097c478bd9Sstevel@tonic-gate 			}
7107c478bd9Sstevel@tonic-gate 			break;
7117c478bd9Sstevel@tonic-gate 		case PR_SYSEXIT:
7127c478bd9Sstevel@tonic-gate 			switch (pstatus->pr_lwp.pr_what) {
7137c478bd9Sstevel@tonic-gate 			case SYS_execve:
7147c478bd9Sstevel@tonic-gate 				if (pstatus->pr_lwp.pr_errno) {
7157c478bd9Sstevel@tonic-gate 					/*
7167c478bd9Sstevel@tonic-gate 					 * The exec failed completely.
7177c478bd9Sstevel@tonic-gate 					 * Reinstate the lwps we fini'd
7187c478bd9Sstevel@tonic-gate 					 * at exec entrance
7197c478bd9Sstevel@tonic-gate 					 */
7207c3666b4Skk 					if (pctx_lwpiterate(pctx,
7217c3666b4Skk 					    pctx->init_lwp) == 0)
7227c3666b4Skk 						running = 1;
7237c3666b4Skk 					else
7247c3666b4Skk 						running = -1;
7257c478bd9Sstevel@tonic-gate 					break;
7267c478bd9Sstevel@tonic-gate 				}
727*bf6cb86eSToomas Soome 				if ((uintptr_t)pctx->exec ==
728*bf6cb86eSToomas Soome 				    (uintptr_t)default_int) {
7297c478bd9Sstevel@tonic-gate 					running = 0;
7307c478bd9Sstevel@tonic-gate 					break;
7317c478bd9Sstevel@tonic-gate 				}
7327c478bd9Sstevel@tonic-gate 				(void) memcpy(&psinfo,
7337c478bd9Sstevel@tonic-gate 				    Ppsinfo(pctx->Pr), sizeof (psinfo));
7347c478bd9Sstevel@tonic-gate 				proc_unctrl_psinfo(&psinfo);
7357c478bd9Sstevel@tonic-gate 				pctx_begin_syscalls(pctx);
7367c3666b4Skk 				if (pctx->exec(pctx, pid, lwpid,
7377c3666b4Skk 				    psinfo.pr_psargs, pctx->uarg) != 0)
7387c3666b4Skk 					running = -1;
7397c3666b4Skk 				if (running == 1 && pctx->init_lwp(pctx,
7407c3666b4Skk 				    pid, 1, pctx->uarg) != 0)
7417c3666b4Skk 					running = -1;
7427c478bd9Sstevel@tonic-gate 				pctx_end_syscalls(pctx);
7437c478bd9Sstevel@tonic-gate 				break;
7447c478bd9Sstevel@tonic-gate 			case SYS_lwp_create:
7457c478bd9Sstevel@tonic-gate 				if (pstatus->pr_lwp.pr_errno ||
7467c478bd9Sstevel@tonic-gate 				    pstatus->pr_lwp.pr_rval1)
7477c478bd9Sstevel@tonic-gate 					break;
7487c478bd9Sstevel@tonic-gate 				pctx_begin_syscalls(pctx);
7497c3666b4Skk 				if (pctx->init_lwp(pctx, pid, lwpid,
7507c3666b4Skk 				    pctx->uarg) != 0)
7517c3666b4Skk 					running = -1;
7527c3666b4Skk 				if (running == 1 && pctx->lwp_create(pctx,
7537c3666b4Skk 				    pid, lwpid, pctx->uarg) != 0)
7547c3666b4Skk 					running = -1;
7557c478bd9Sstevel@tonic-gate 				pctx_end_syscalls(pctx);
7567c478bd9Sstevel@tonic-gate 				break;
7577c478bd9Sstevel@tonic-gate 			case SYS_vfork:
758657b1f3dSraf 			case SYS_forksys:
7597c478bd9Sstevel@tonic-gate 				if (pstatus->pr_lwp.pr_errno)
7607c478bd9Sstevel@tonic-gate 					break;
7617c478bd9Sstevel@tonic-gate 				(void) fflush(NULL);
7627c478bd9Sstevel@tonic-gate 				switch (fork1()) {
7637c478bd9Sstevel@tonic-gate 					pid_t ppid;
7647c478bd9Sstevel@tonic-gate 					int wascreated;
7657c478bd9Sstevel@tonic-gate 					pctx_sysc_forkfn_t *forkfn;
7667c478bd9Sstevel@tonic-gate 				case 0:
7677c478bd9Sstevel@tonic-gate 					ppid = pid;
7687c478bd9Sstevel@tonic-gate 					pid = pstatus->pr_lwp.pr_rval1;
7697c478bd9Sstevel@tonic-gate 					wascreated = pctx->created;
7707c478bd9Sstevel@tonic-gate 					forkfn = pctx->fork;
7717c478bd9Sstevel@tonic-gate 					pctx_free(pctx);
7727c478bd9Sstevel@tonic-gate 					pctx = pctx_capture(pid, pctx->uarg,
7737c478bd9Sstevel@tonic-gate 					    pctx->verbose, pctx->errfn);
7747c478bd9Sstevel@tonic-gate 					if (pctx != NULL) {
7757c478bd9Sstevel@tonic-gate 						if (wascreated) {
7767c478bd9Sstevel@tonic-gate 							/*
7777c478bd9Sstevel@tonic-gate 							 * Set kill on last
7787c478bd9Sstevel@tonic-gate 							 * close so -all-
7797c478bd9Sstevel@tonic-gate 							 * children die.
7807c478bd9Sstevel@tonic-gate 							 */
7817c478bd9Sstevel@tonic-gate 							pctx->created = 1;
7827c478bd9Sstevel@tonic-gate 							(void) Psetflags(
7837c478bd9Sstevel@tonic-gate 							    pctx->Pr, PR_KLC);
7847c478bd9Sstevel@tonic-gate 						}
7857c478bd9Sstevel@tonic-gate 						(*forkfn)(pctx, ppid, pid,
7867c478bd9Sstevel@tonic-gate 						    lwpid, pctx->uarg);
7877c478bd9Sstevel@tonic-gate 						pctx_release(pctx);
7887c3666b4Skk 						_exit(0);
7897c3666b4Skk 					} else {
7907c3666b4Skk 						_exit(1);
7917c478bd9Sstevel@tonic-gate 					}
7927c478bd9Sstevel@tonic-gate 					/*NOTREACHED*/
7937c478bd9Sstevel@tonic-gate 				case -1:
7947c478bd9Sstevel@tonic-gate 					pctx_error(pctx, fn,
7957c478bd9Sstevel@tonic-gate 					    "cannot follow pid %d: %s\n",
7967c478bd9Sstevel@tonic-gate 					    (int)pstatus->pr_lwp.pr_rval1,
7977c478bd9Sstevel@tonic-gate 					    strerror(errno));
7987c478bd9Sstevel@tonic-gate 					break;
7997c478bd9Sstevel@tonic-gate 				default:
8007c478bd9Sstevel@tonic-gate 					break;
8017c478bd9Sstevel@tonic-gate 				}
8027c478bd9Sstevel@tonic-gate 				break;
8037c478bd9Sstevel@tonic-gate 			default:
8047c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn, gettext(
8057c478bd9Sstevel@tonic-gate 				    "warning - pid %d sysexit(%d)\n"),
8067c478bd9Sstevel@tonic-gate 				    (int)pid, pstatus->pr_lwp.pr_what);
8077c478bd9Sstevel@tonic-gate 				break;
8087c478bd9Sstevel@tonic-gate 			}
8097c478bd9Sstevel@tonic-gate 			break;
8107c478bd9Sstevel@tonic-gate 		case PR_SIGNALLED:
8117c478bd9Sstevel@tonic-gate 			if (pctx->verbose)
8127c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
8137c478bd9Sstevel@tonic-gate 				    gettext("pid %d - signalled\n"), (int)pid);
8147c478bd9Sstevel@tonic-gate 			break;
8157c478bd9Sstevel@tonic-gate 		case PR_JOBCONTROL:
8167c478bd9Sstevel@tonic-gate 			if (pctx->verbose)
8177c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
8187c478bd9Sstevel@tonic-gate 				    gettext("pid %d - job control stop\n"),
8197c478bd9Sstevel@tonic-gate 				    (int)pid);
8207c3666b4Skk 			running = -1;
8217c478bd9Sstevel@tonic-gate 			break;
8227c478bd9Sstevel@tonic-gate 		case PR_FAULTED:
8237c478bd9Sstevel@tonic-gate 			if (pctx->verbose)
8247c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
8257c478bd9Sstevel@tonic-gate 				    gettext("pid %d - faulted\n"), (int)pid);
8267c478bd9Sstevel@tonic-gate 			break;
8277c478bd9Sstevel@tonic-gate 		case PR_SUSPENDED:
8287c478bd9Sstevel@tonic-gate 			if (pctx->verbose)
8297c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
8307c478bd9Sstevel@tonic-gate 				    gettext("pid %d - suspended\n"), (int)pid);
8317c478bd9Sstevel@tonic-gate 			break;
8327c478bd9Sstevel@tonic-gate 		case PR_CHECKPOINT:
8337c478bd9Sstevel@tonic-gate 			if (pctx->verbose)
8347c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
8357c478bd9Sstevel@tonic-gate 				    gettext("pid %d - checkpoint\n"),
8367c478bd9Sstevel@tonic-gate 				    (int)pid);
8377c478bd9Sstevel@tonic-gate 			break;
8387c478bd9Sstevel@tonic-gate 		default:
8397c478bd9Sstevel@tonic-gate 			if (pctx->verbose)
8407c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
8417c478bd9Sstevel@tonic-gate 				    gettext("pid %d - reason %d\n"),
8427c478bd9Sstevel@tonic-gate 				    (int)pid, pstatus->pr_lwp.pr_why);
8437c3666b4Skk 			running = -1;
8447c478bd9Sstevel@tonic-gate 			break;
8457c478bd9Sstevel@tonic-gate 		}
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate bailout:
8497c478bd9Sstevel@tonic-gate 	(void) signal(SIGCHLD, sigsaved);
8507c478bd9Sstevel@tonic-gate 
851b885580bSAlexander Kolbasov 	if (pctx->terminate)
852b885580bSAlexander Kolbasov 		return (0);
853b885580bSAlexander Kolbasov 
8547c3666b4Skk 	switch (running) {
8557c3666b4Skk 	case 0:
8567c3666b4Skk 		return (0);
8577c3666b4Skk 	case -1:
8587c3666b4Skk 		return (-1);
8597c3666b4Skk 	default:
8607c3666b4Skk 		pctx_error(pctx, fn, gettext("lost control of pid %d\n"),
8617c3666b4Skk 		    (int)pid);
8627c3666b4Skk 		pctx_free(pctx);
8637c3666b4Skk 		return (-1);
8647c3666b4Skk 	}
8657c478bd9Sstevel@tonic-gate }
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate /*
8687c478bd9Sstevel@tonic-gate  * Execute the private 'cpc' system call in the context of the
8697c478bd9Sstevel@tonic-gate  * controlled process.
8707c478bd9Sstevel@tonic-gate  */
8717c478bd9Sstevel@tonic-gate int
__pctx_cpc(pctx_t * pctx,cpc_t * cpc,int cmd,id_t lwpid,void * data1,void * data2,void * data3,int bufsize)8727c478bd9Sstevel@tonic-gate __pctx_cpc(pctx_t *pctx, cpc_t *cpc,
8737c478bd9Sstevel@tonic-gate     int cmd, id_t lwpid, void *data1, void *data2, void *data3, int bufsize)
8747c478bd9Sstevel@tonic-gate {
8757c478bd9Sstevel@tonic-gate 	sysret_t rval;
8767c478bd9Sstevel@tonic-gate 	argdes_t argd[5];
8777c478bd9Sstevel@tonic-gate 	argdes_t *adp = &argd[0];
8787c478bd9Sstevel@tonic-gate 	int error;
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	/*
8817c478bd9Sstevel@tonic-gate 	 * Keep track of the relationship between cpc_t and pctx_t here.
8827c478bd9Sstevel@tonic-gate 	 * We store the last cpc_t used by libpctx, so that when this pctx is
8837c478bd9Sstevel@tonic-gate 	 * destroyed, libpctx can notify libcpc.
8847c478bd9Sstevel@tonic-gate 	 */
885b885580bSAlexander Kolbasov 
8867c478bd9Sstevel@tonic-gate 	if (pctx->cpc != NULL && pctx->cpc != cpc && pctx_cpc_callback != NULL)
8877c478bd9Sstevel@tonic-gate 		(*pctx_cpc_callback)(pctx->cpc, pctx);
8887c478bd9Sstevel@tonic-gate 	pctx->cpc = cpc;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	/*
8917c478bd9Sstevel@tonic-gate 	 * cmd and lwpid are passed in by value no matter what the command is.
8927c478bd9Sstevel@tonic-gate 	 */
8937c478bd9Sstevel@tonic-gate 	adp->arg_value = cmd;
8947c478bd9Sstevel@tonic-gate 	adp->arg_object = NULL;
8957c478bd9Sstevel@tonic-gate 	adp->arg_type = AT_BYVAL;
8967c478bd9Sstevel@tonic-gate 	adp->arg_inout = AI_INPUT;
8977c478bd9Sstevel@tonic-gate 	adp->arg_size = 0;
8987c478bd9Sstevel@tonic-gate 	adp++;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	adp->arg_value = lwpid;
9017c478bd9Sstevel@tonic-gate 	adp->arg_object = NULL;
9027c478bd9Sstevel@tonic-gate 	adp->arg_type = AT_BYVAL;
9037c478bd9Sstevel@tonic-gate 	adp->arg_inout = AI_INPUT;
9047c478bd9Sstevel@tonic-gate 	adp->arg_size = 0;
9057c478bd9Sstevel@tonic-gate 	adp++;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	switch (cmd) {
9087c478bd9Sstevel@tonic-gate 	case CPC_BIND:
9097c478bd9Sstevel@tonic-gate 		adp->arg_value = 0;
9107c478bd9Sstevel@tonic-gate 		adp->arg_object = data1;
9117c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYREF;
9127c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_INPUT;
9137c478bd9Sstevel@tonic-gate 		adp->arg_size = (size_t)data2;
9147c478bd9Sstevel@tonic-gate 		adp++;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 		adp->arg_value = (size_t)data2;
9177c478bd9Sstevel@tonic-gate 		adp->arg_object = NULL;
9187c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYVAL;
9197c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_INPUT;
9207c478bd9Sstevel@tonic-gate 		adp->arg_size = 0;
9217c478bd9Sstevel@tonic-gate 		adp++;
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 		adp->arg_value = 0;
9247c478bd9Sstevel@tonic-gate 		adp->arg_object = data3;
9257c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYREF;
9267c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_INOUT;
9277c478bd9Sstevel@tonic-gate 		adp->arg_size = sizeof (int);
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 		break;
9307c478bd9Sstevel@tonic-gate 	case CPC_SAMPLE:
9317c478bd9Sstevel@tonic-gate 		adp->arg_value = 0;
9327c478bd9Sstevel@tonic-gate 		adp->arg_object = data1;
9337c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYREF;
9347c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_OUTPUT;
9357c478bd9Sstevel@tonic-gate 		adp->arg_size = bufsize;
9367c478bd9Sstevel@tonic-gate 		adp++;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 		adp->arg_value = 0;
9397c478bd9Sstevel@tonic-gate 		adp->arg_object = data2;
9407c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYREF;
9417c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_OUTPUT;
9427c478bd9Sstevel@tonic-gate 		adp->arg_size = sizeof (hrtime_t);
9437c478bd9Sstevel@tonic-gate 		adp++;
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 		adp->arg_value = 0;
9467c478bd9Sstevel@tonic-gate 		adp->arg_object = data3;
9477c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYREF;
9487c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_OUTPUT;
9497c478bd9Sstevel@tonic-gate 		adp->arg_size = sizeof (uint64_t);
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 		break;
9527c478bd9Sstevel@tonic-gate 	default:
9537c478bd9Sstevel@tonic-gate 		adp->arg_value = 0;
9547c478bd9Sstevel@tonic-gate 		adp->arg_object = 0;
9557c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYVAL;
9567c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_INPUT;
9577c478bd9Sstevel@tonic-gate 		adp->arg_size = 0;
9587c478bd9Sstevel@tonic-gate 		adp++;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 		adp->arg_value = 0;
9617c478bd9Sstevel@tonic-gate 		adp->arg_object = 0;
9627c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYVAL;
9637c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_INPUT;
9647c478bd9Sstevel@tonic-gate 		adp->arg_size = 0;
9657c478bd9Sstevel@tonic-gate 		adp++;
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 		adp->arg_value = 0;
9687c478bd9Sstevel@tonic-gate 		adp->arg_object = 0;
9697c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYVAL;
9707c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_INPUT;
9717c478bd9Sstevel@tonic-gate 		adp->arg_size = 0;
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 		break;
9747c478bd9Sstevel@tonic-gate 	}
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	error = Psyscall(pctx->Pr, &rval, SYS_cpc, 5, &argd[0]);
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	if (error) {
9797c478bd9Sstevel@tonic-gate 		errno = error > 0 ? error : ENOSYS;
9807c478bd9Sstevel@tonic-gate 		return (-1);
9817c478bd9Sstevel@tonic-gate 	}
9827c478bd9Sstevel@tonic-gate 	return (rval.sys_rval1);
9837c478bd9Sstevel@tonic-gate }
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate /*
9867c478bd9Sstevel@tonic-gate  * libcpc-private hook used to register a callback. The callback is used to
9877c478bd9Sstevel@tonic-gate  * notify libcpc when a pctx handle is invalidated.
9887c478bd9Sstevel@tonic-gate  */
9897c478bd9Sstevel@tonic-gate void
__pctx_cpc_register_callback(void (* arg)(struct __cpc *,struct __pctx *))9907c478bd9Sstevel@tonic-gate __pctx_cpc_register_callback(void (*arg)(struct __cpc *, struct __pctx *))
9917c478bd9Sstevel@tonic-gate {
9927c478bd9Sstevel@tonic-gate 	pctx_cpc_callback = arg;
9937c478bd9Sstevel@tonic-gate }
994b885580bSAlexander Kolbasov 
995b885580bSAlexander Kolbasov /*
996b885580bSAlexander Kolbasov  * Tell pctx_run to bail out immediately
997b885580bSAlexander Kolbasov  */
998b885580bSAlexander Kolbasov void
pctx_terminate(struct __pctx * pctx)999b885580bSAlexander Kolbasov pctx_terminate(struct __pctx *pctx)
1000b885580bSAlexander Kolbasov {
1001b885580bSAlexander Kolbasov 	pctx->terminate = 1;
1002b885580bSAlexander Kolbasov }
1003