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