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 */ 21*657b1f3dSraf 227c478bd9Sstevel@tonic-gate /* 237c3666b4Skk * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * This file contains a set of generic routines for periodically 317c478bd9Sstevel@tonic-gate * sampling the state of another process, or tree of processes. 327c478bd9Sstevel@tonic-gate * 337c478bd9Sstevel@tonic-gate * It is built upon the infrastructure provided by libproc. 347c478bd9Sstevel@tonic-gate */ 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include <sys/wait.h> 377c478bd9Sstevel@tonic-gate #include <sys/syscall.h> 387c478bd9Sstevel@tonic-gate #include <sys/time.h> 397c478bd9Sstevel@tonic-gate #include <libproc.h> 407c478bd9Sstevel@tonic-gate #include <stdio.h> 417c478bd9Sstevel@tonic-gate #include <stdlib.h> 427c478bd9Sstevel@tonic-gate #include <errno.h> 437c478bd9Sstevel@tonic-gate #include <unistd.h> 447c478bd9Sstevel@tonic-gate #include <signal.h> 457c478bd9Sstevel@tonic-gate #include <string.h> 467c478bd9Sstevel@tonic-gate #include <strings.h> 477c478bd9Sstevel@tonic-gate #include <limits.h> 487c478bd9Sstevel@tonic-gate #include <ctype.h> 497c478bd9Sstevel@tonic-gate #include <libintl.h> 507c478bd9Sstevel@tonic-gate #include <libcpc.h> 517c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h> 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #include "libpctx.h" 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate struct __pctx { 567c478bd9Sstevel@tonic-gate pctx_errfn_t *errfn; 577c478bd9Sstevel@tonic-gate struct ps_prochandle *Pr; 587c478bd9Sstevel@tonic-gate void *uarg; 597c478bd9Sstevel@tonic-gate pctx_sysc_execfn_t *exec; 607c478bd9Sstevel@tonic-gate pctx_sysc_forkfn_t *fork; 617c478bd9Sstevel@tonic-gate pctx_sysc_exitfn_t *exit; 627c478bd9Sstevel@tonic-gate pctx_sysc_lwp_createfn_t *lwp_create; 637c478bd9Sstevel@tonic-gate pctx_init_lwpfn_t *init_lwp; 647c478bd9Sstevel@tonic-gate pctx_fini_lwpfn_t *fini_lwp; 657c478bd9Sstevel@tonic-gate pctx_sysc_lwp_exitfn_t *lwp_exit; 667c478bd9Sstevel@tonic-gate int verbose; 677c478bd9Sstevel@tonic-gate int created; 687c478bd9Sstevel@tonic-gate int sigblocked; 697c478bd9Sstevel@tonic-gate sigset_t savedset; 707c478bd9Sstevel@tonic-gate cpc_t *cpc; 717c478bd9Sstevel@tonic-gate }; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate static void (*pctx_cpc_callback)(cpc_t *cpc, struct __pctx *pctx); 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate static void 767c478bd9Sstevel@tonic-gate pctx_default_errfn(const char *fn, const char *fmt, va_list ap) 777c478bd9Sstevel@tonic-gate { 787c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "libpctx: pctx_%s: ", fn); 797c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap); 807c478bd9Sstevel@tonic-gate } 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate /*PRINTFLIKE3*/ 837c478bd9Sstevel@tonic-gate static void 847c478bd9Sstevel@tonic-gate pctx_error(pctx_t *pctx, const char *fn, const char *fmt, ...) 857c478bd9Sstevel@tonic-gate { 867c478bd9Sstevel@tonic-gate va_list ap; 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate va_start(ap, fmt); 897c478bd9Sstevel@tonic-gate pctx->errfn(fn, fmt, ap); 907c478bd9Sstevel@tonic-gate va_end(ap); 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate /* 947c478bd9Sstevel@tonic-gate * Create a new process and bind the user args for it 957c478bd9Sstevel@tonic-gate */ 967c478bd9Sstevel@tonic-gate pctx_t * 977c478bd9Sstevel@tonic-gate pctx_create( 987c478bd9Sstevel@tonic-gate const char *filename, 997c478bd9Sstevel@tonic-gate char *const *argv, 1007c478bd9Sstevel@tonic-gate void *arg, 1017c478bd9Sstevel@tonic-gate int verbose, 1027c478bd9Sstevel@tonic-gate pctx_errfn_t *errfn) 1037c478bd9Sstevel@tonic-gate { 1047c478bd9Sstevel@tonic-gate static const char fn[] = "create"; 1057c478bd9Sstevel@tonic-gate int err; 1067c478bd9Sstevel@tonic-gate pctx_t *pctx; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate pctx = calloc(1, sizeof (*pctx)); 1097c478bd9Sstevel@tonic-gate pctx->uarg = arg; 1107c478bd9Sstevel@tonic-gate pctx->verbose = verbose; 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 * 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 2407c478bd9Sstevel@tonic-gate default_void(pctx_t *pctx) 2417c478bd9Sstevel@tonic-gate {} 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2447c478bd9Sstevel@tonic-gate static int 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 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) 3047c478bd9Sstevel@tonic-gate pctx->exec = (pctx_sysc_execfn_t *)default_int; 3057c478bd9Sstevel@tonic-gate if (pctx->fork == NULL) 3067c478bd9Sstevel@tonic-gate pctx->fork = (pctx_sysc_forkfn_t *)default_void; 3077c478bd9Sstevel@tonic-gate if (pctx->exit == NULL) 3087c478bd9Sstevel@tonic-gate pctx->exit = (pctx_sysc_exitfn_t *)default_void; 3097c478bd9Sstevel@tonic-gate if (pctx->lwp_create == NULL) 3107c478bd9Sstevel@tonic-gate pctx->lwp_create = (pctx_sysc_lwp_createfn_t *)default_int; 3117c478bd9Sstevel@tonic-gate if (pctx->init_lwp == NULL) 3127c478bd9Sstevel@tonic-gate pctx->init_lwp = (pctx_init_lwpfn_t *)default_int; 3137c478bd9Sstevel@tonic-gate if (pctx->fini_lwp == NULL) 3147c478bd9Sstevel@tonic-gate pctx->fini_lwp = (pctx_fini_lwpfn_t *)default_int; 3157c478bd9Sstevel@tonic-gate if (pctx->lwp_exit == NULL) 3167c478bd9Sstevel@tonic-gate pctx->lwp_exit = (pctx_sysc_lwp_exitfn_t *)default_int; 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate if (pctx->fork != (pctx_sysc_forkfn_t *)default_void) { 3197c478bd9Sstevel@tonic-gate (void) Psysexit(pctx->Pr, SYS_forkall, 1); 3207c478bd9Sstevel@tonic-gate (void) Psysexit(pctx->Pr, SYS_vfork, 1); 3217c478bd9Sstevel@tonic-gate (void) Psysexit(pctx->Pr, SYS_fork1, 1); 322*657b1f3dSraf (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_forkall, 0); 3277c478bd9Sstevel@tonic-gate (void) Psysexit(pctx->Pr, SYS_vfork, 0); 3287c478bd9Sstevel@tonic-gate (void) Psysexit(pctx->Pr, SYS_fork1, 0); 329*657b1f3dSraf (void) Psysexit(pctx->Pr, SYS_forksys, 0); 3307c478bd9Sstevel@tonic-gate if (Punsetflags(pctx->Pr, PR_FORK) == -1) 3317c478bd9Sstevel@tonic-gate error = -1; 3327c478bd9Sstevel@tonic-gate } 3337c478bd9Sstevel@tonic-gate 3347c478bd9Sstevel@tonic-gate /* 3357c478bd9Sstevel@tonic-gate * exec causes termination of all but the exec-ing lwp, 3367c478bd9Sstevel@tonic-gate * and resets the lwpid to one in the new address space. 3377c478bd9Sstevel@tonic-gate */ 3387c478bd9Sstevel@tonic-gate if (pctx->exec != (pctx_sysc_execfn_t *)default_int || 3397c478bd9Sstevel@tonic-gate pctx->fini_lwp != (pctx_fini_lwpfn_t *)default_int || 3407c478bd9Sstevel@tonic-gate pctx->init_lwp != (pctx_init_lwpfn_t *)default_int) { 3417c478bd9Sstevel@tonic-gate (void) Psysexit(pctx->Pr, SYS_exec, 1); 3427c478bd9Sstevel@tonic-gate (void) Psysexit(pctx->Pr, SYS_execve, 1); 3437c478bd9Sstevel@tonic-gate (void) Psysentry(pctx->Pr, SYS_exec, 1); 3447c478bd9Sstevel@tonic-gate (void) Psysentry(pctx->Pr, SYS_execve, 1); 3457c478bd9Sstevel@tonic-gate } else { 3467c478bd9Sstevel@tonic-gate (void) Psysexit(pctx->Pr, SYS_exec, 0); 3477c478bd9Sstevel@tonic-gate (void) Psysexit(pctx->Pr, SYS_execve, 0); 3487c478bd9Sstevel@tonic-gate (void) Psysentry(pctx->Pr, SYS_exec, 0); 3497c478bd9Sstevel@tonic-gate (void) Psysentry(pctx->Pr, SYS_execve, 0); 3507c478bd9Sstevel@tonic-gate } 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate (void) Psysexit(pctx->Pr, SYS_lwp_create, 3537c478bd9Sstevel@tonic-gate pctx->lwp_create != (pctx_sysc_lwp_createfn_t *)default_int || 3547c478bd9Sstevel@tonic-gate pctx->init_lwp != (pctx_init_lwpfn_t *)default_int); 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate (void) Psysentry(pctx->Pr, SYS_lwp_exit, 3577c478bd9Sstevel@tonic-gate pctx->lwp_exit != (pctx_sysc_lwp_exitfn_t *)default_int || 3587c478bd9Sstevel@tonic-gate pctx->fini_lwp != (pctx_fini_lwpfn_t *)default_int); 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate return (0); 3617c478bd9Sstevel@tonic-gate } 3627c478bd9Sstevel@tonic-gate 3637c478bd9Sstevel@tonic-gate static sigset_t termsig; 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate static void 3667c478bd9Sstevel@tonic-gate __libpctx_init(void) 3677c478bd9Sstevel@tonic-gate { 3687c478bd9Sstevel@tonic-gate /* 3697c478bd9Sstevel@tonic-gate * Initialize the signal set used to shield ourselves from 3707c478bd9Sstevel@tonic-gate * death-by-terminal-signal while the agent lwp is running. 3717c478bd9Sstevel@tonic-gate */ 3727c478bd9Sstevel@tonic-gate (void) sigemptyset(&termsig); 3737c478bd9Sstevel@tonic-gate (void) sigaddset(&termsig, SIGHUP); 3747c478bd9Sstevel@tonic-gate (void) sigaddset(&termsig, SIGTERM); 3757c478bd9Sstevel@tonic-gate (void) sigaddset(&termsig, SIGINT); 3767c478bd9Sstevel@tonic-gate (void) sigaddset(&termsig, SIGQUIT); 3777c478bd9Sstevel@tonic-gate } 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate #pragma init(__libpctx_init) 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate static void 3827c478bd9Sstevel@tonic-gate pctx_begin_syscalls(pctx_t *pctx) 3837c478bd9Sstevel@tonic-gate { 3847c478bd9Sstevel@tonic-gate if (pctx->Pr == NULL) 3857c478bd9Sstevel@tonic-gate return; 3867c478bd9Sstevel@tonic-gate if (pctx->sigblocked++ == 0) { 3877c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &termsig, &pctx->savedset); 3887c478bd9Sstevel@tonic-gate (void) Pcreate_agent(pctx->Pr); 3897c478bd9Sstevel@tonic-gate } 3907c478bd9Sstevel@tonic-gate } 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate static void 3937c478bd9Sstevel@tonic-gate pctx_end_syscalls(pctx_t *pctx) 3947c478bd9Sstevel@tonic-gate { 3957c478bd9Sstevel@tonic-gate if (pctx->Pr == NULL) 3967c478bd9Sstevel@tonic-gate return; 3977c478bd9Sstevel@tonic-gate if (--pctx->sigblocked == 0) { 3987c478bd9Sstevel@tonic-gate (void) Pdestroy_agent(pctx->Pr); 3997c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &pctx->savedset, NULL); 4007c478bd9Sstevel@tonic-gate } 4017c478bd9Sstevel@tonic-gate } 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate /* 4047c478bd9Sstevel@tonic-gate * Iterate over the valid lwpids in the process, invoking the 4057c478bd9Sstevel@tonic-gate * action function on each one. 4067c478bd9Sstevel@tonic-gate */ 4077c478bd9Sstevel@tonic-gate static int 4087c478bd9Sstevel@tonic-gate pctx_lwpiterate(pctx_t *pctx, int (*action)(pctx_t *, pid_t, id_t, void *)) 4097c478bd9Sstevel@tonic-gate { 4107c478bd9Sstevel@tonic-gate const pstatus_t *pstatus; 4117c478bd9Sstevel@tonic-gate char lstatus[64]; 4127c478bd9Sstevel@tonic-gate struct stat statb; 4137c478bd9Sstevel@tonic-gate lwpstatus_t *lwps; 4147c478bd9Sstevel@tonic-gate prheader_t *prh; 4157c478bd9Sstevel@tonic-gate int fd, nlwp; 4167c478bd9Sstevel@tonic-gate int ret = 0; 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate if (action == (int (*)(pctx_t *, pid_t, id_t, void *))default_int) 4197c478bd9Sstevel@tonic-gate return (0); 4207c478bd9Sstevel@tonic-gate 4217c478bd9Sstevel@tonic-gate pstatus = Pstatus(pctx->Pr); 4227c478bd9Sstevel@tonic-gate if (pstatus->pr_nlwp <= 1) { 4237c478bd9Sstevel@tonic-gate pctx_begin_syscalls(pctx); 4247c478bd9Sstevel@tonic-gate ret = action(pctx, pstatus->pr_pid, 1, pctx->uarg); 4257c478bd9Sstevel@tonic-gate pctx_end_syscalls(pctx); 4267c478bd9Sstevel@tonic-gate return (ret); 4277c478bd9Sstevel@tonic-gate } 4287c478bd9Sstevel@tonic-gate 4297c478bd9Sstevel@tonic-gate (void) snprintf(lstatus, sizeof (lstatus), 4307c478bd9Sstevel@tonic-gate "/proc/%d/lstatus", (int)pstatus->pr_pid); 4317c478bd9Sstevel@tonic-gate 4327c478bd9Sstevel@tonic-gate if ((fd = open(lstatus, O_RDONLY)) < 0 || 4337c478bd9Sstevel@tonic-gate fstat(fd, &statb) != 0) { 4347c478bd9Sstevel@tonic-gate if (fd >= 0) 4357c478bd9Sstevel@tonic-gate (void) close(fd); 4367c478bd9Sstevel@tonic-gate return (-1); 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate prh = malloc(statb.st_size); 4407c478bd9Sstevel@tonic-gate if (read(fd, prh, statb.st_size) < 4417c478bd9Sstevel@tonic-gate sizeof (prheader_t) + sizeof (lwpstatus_t)) { 4427c478bd9Sstevel@tonic-gate (void) close(fd); 4437c478bd9Sstevel@tonic-gate free(prh); 4447c478bd9Sstevel@tonic-gate return (-1); 4457c478bd9Sstevel@tonic-gate } 4467c478bd9Sstevel@tonic-gate (void) close(fd); 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */ 4497c478bd9Sstevel@tonic-gate lwps = (lwpstatus_t *)(prh + 1); 4507c478bd9Sstevel@tonic-gate pctx_begin_syscalls(pctx); 4517c478bd9Sstevel@tonic-gate for (nlwp = prh->pr_nent; nlwp > 0; nlwp--) { 4527c478bd9Sstevel@tonic-gate if (action(pctx, 4537c478bd9Sstevel@tonic-gate pstatus->pr_pid, lwps->pr_lwpid, pctx->uarg) != 0) 4547c478bd9Sstevel@tonic-gate ret = -1; 4557c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */ 4567c478bd9Sstevel@tonic-gate lwps = (lwpstatus_t *)((char *)lwps + prh->pr_entsize); 4577c478bd9Sstevel@tonic-gate } 4587c478bd9Sstevel@tonic-gate pctx_end_syscalls(pctx); 4597c478bd9Sstevel@tonic-gate free(prh); 4607c478bd9Sstevel@tonic-gate return (ret); 4617c478bd9Sstevel@tonic-gate } 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate /* 4647c478bd9Sstevel@tonic-gate * Free any associated state, but leave the process stopped if it 4657c478bd9Sstevel@tonic-gate * is still under our control. (If it isn't under our control, 4667c478bd9Sstevel@tonic-gate * it should just run to completion when we do our last close) 4677c478bd9Sstevel@tonic-gate */ 4687c478bd9Sstevel@tonic-gate static void 4697c478bd9Sstevel@tonic-gate pctx_free(pctx_t *pctx) 4707c478bd9Sstevel@tonic-gate { 4717c478bd9Sstevel@tonic-gate if (pctx->cpc != NULL && pctx_cpc_callback != NULL) 4727c478bd9Sstevel@tonic-gate (*pctx_cpc_callback)(pctx->cpc, pctx); 4737c478bd9Sstevel@tonic-gate if (pctx->Pr) { 4747c478bd9Sstevel@tonic-gate Pfree(pctx->Pr); 4757c478bd9Sstevel@tonic-gate pctx->Pr = NULL; 4767c478bd9Sstevel@tonic-gate } 4777c478bd9Sstevel@tonic-gate pctx->errfn = pctx_default_errfn; 4787c478bd9Sstevel@tonic-gate } 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate /* 4817c478bd9Sstevel@tonic-gate * Completely release the process from our control and discard all our state 4827c478bd9Sstevel@tonic-gate */ 4837c478bd9Sstevel@tonic-gate void 4847c478bd9Sstevel@tonic-gate pctx_release(pctx_t *pctx) 4857c478bd9Sstevel@tonic-gate { 4867c478bd9Sstevel@tonic-gate if (pctx->Pr) { 4877c478bd9Sstevel@tonic-gate Prelease(pctx->Pr, PRELEASE_CLEAR); 4887c478bd9Sstevel@tonic-gate pctx->Pr = NULL; 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate pctx_free(pctx); 4917c478bd9Sstevel@tonic-gate bzero(pctx, sizeof (*pctx)); 4927c478bd9Sstevel@tonic-gate free(pctx); 4937c478bd9Sstevel@tonic-gate } 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate static void 4967c478bd9Sstevel@tonic-gate msincr(struct timeval *tv, uint_t msec) 4977c478bd9Sstevel@tonic-gate { 4987c478bd9Sstevel@tonic-gate tv->tv_sec += msec / MILLISEC; 4997c478bd9Sstevel@tonic-gate tv->tv_usec += (msec % MILLISEC) * MILLISEC; 5007c478bd9Sstevel@tonic-gate if (tv->tv_usec > MICROSEC) { 5017c478bd9Sstevel@tonic-gate tv->tv_sec++; 5027c478bd9Sstevel@tonic-gate tv->tv_usec -= MICROSEC; 5037c478bd9Sstevel@tonic-gate } 5047c478bd9Sstevel@tonic-gate } 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate static uint_t 5077c478bd9Sstevel@tonic-gate msdiff(struct timeval *tva, struct timeval *tvb) 5087c478bd9Sstevel@tonic-gate { 5097c478bd9Sstevel@tonic-gate time_t sdiff = tva->tv_sec - tvb->tv_sec; 5107c478bd9Sstevel@tonic-gate suseconds_t udiff = tva->tv_usec - tvb->tv_usec; 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate if (sdiff < 0) 5137c478bd9Sstevel@tonic-gate return (0); 5147c478bd9Sstevel@tonic-gate if (udiff < 0) { 5157c478bd9Sstevel@tonic-gate udiff += MICROSEC; 5167c478bd9Sstevel@tonic-gate sdiff--; 5177c478bd9Sstevel@tonic-gate } 5187c478bd9Sstevel@tonic-gate if (sdiff < 0) 5197c478bd9Sstevel@tonic-gate return (0); 5207c478bd9Sstevel@tonic-gate if (sdiff >= (INT_MAX / MILLISEC)) 5217c478bd9Sstevel@tonic-gate return ((uint_t)INT_MAX); 5227c478bd9Sstevel@tonic-gate return ((uint_t)(sdiff * MILLISEC + udiff / MILLISEC)); 5237c478bd9Sstevel@tonic-gate } 5247c478bd9Sstevel@tonic-gate 5257c478bd9Sstevel@tonic-gate int 5267c478bd9Sstevel@tonic-gate pctx_run( 5277c478bd9Sstevel@tonic-gate pctx_t *pctx, 5287c478bd9Sstevel@tonic-gate uint_t msec, 5297c478bd9Sstevel@tonic-gate uint_t nsamples, 5307c478bd9Sstevel@tonic-gate int (*tick)(pctx_t *, pid_t, id_t, void *)) 5317c478bd9Sstevel@tonic-gate { 5327c478bd9Sstevel@tonic-gate static const char fn[] = "run"; 5337c478bd9Sstevel@tonic-gate struct timeval tvgoal, tvnow; 5347c478bd9Sstevel@tonic-gate uint_t mswait = 0; 5357c478bd9Sstevel@tonic-gate int running = 1; 5367c478bd9Sstevel@tonic-gate const pstatus_t *pstatus; 5377c478bd9Sstevel@tonic-gate psinfo_t psinfo; 5387c478bd9Sstevel@tonic-gate void (*sigsaved)(); 5397c478bd9Sstevel@tonic-gate id_t lwpid; 5407c478bd9Sstevel@tonic-gate pid_t pid = Pstatus(pctx->Pr)->pr_pid; 5417c478bd9Sstevel@tonic-gate int pstate; 5427c478bd9Sstevel@tonic-gate 5437c478bd9Sstevel@tonic-gate if (msec == 0) 5447c478bd9Sstevel@tonic-gate nsamples = 0; 5457c478bd9Sstevel@tonic-gate if (nsamples == 0) 5467c478bd9Sstevel@tonic-gate nsamples = UINT_MAX; 5477c478bd9Sstevel@tonic-gate 5487c478bd9Sstevel@tonic-gate /* 5497c478bd9Sstevel@tonic-gate * Casually discard any knowledge of the children we create 5507c478bd9Sstevel@tonic-gate */ 5517c478bd9Sstevel@tonic-gate sigsaved = signal(SIGCHLD, SIG_IGN); 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate /* 5547c478bd9Sstevel@tonic-gate * Since we've just "discovered" this process which might have 5557c478bd9Sstevel@tonic-gate * been running for weeks, deliver some init_lwp events so 5567c478bd9Sstevel@tonic-gate * that our caller gets a handle on the process. 5577c478bd9Sstevel@tonic-gate */ 5587c478bd9Sstevel@tonic-gate if (pctx_lwpiterate(pctx, pctx->init_lwp) != 0) { 5597c478bd9Sstevel@tonic-gate if (pctx->verbose) 5607c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, 5617c478bd9Sstevel@tonic-gate gettext("%d: lwp discovery failed\n"), (int)pid); 5627c478bd9Sstevel@tonic-gate goto bailout; 5637c478bd9Sstevel@tonic-gate } 5647c478bd9Sstevel@tonic-gate 5657c478bd9Sstevel@tonic-gate if (msec != 0) { 5667c478bd9Sstevel@tonic-gate /* 5677c478bd9Sstevel@tonic-gate * tvgoal represents the time at which the sample 5687c478bd9Sstevel@tonic-gate * should next be taken. 5697c478bd9Sstevel@tonic-gate */ 5707c478bd9Sstevel@tonic-gate (void) gettimeofday(&tvgoal, 0); 5717c478bd9Sstevel@tonic-gate msincr(&tvgoal, msec); 5727c478bd9Sstevel@tonic-gate } 5737c478bd9Sstevel@tonic-gate 5747c3666b4Skk /* 5757c3666b4Skk * The event handling loop continues while running is 1. 5767c3666b4Skk * running becomes 0 when either the controlled process has 5777c3666b4Skk * exited successfully or the number of time samples has expired. 5787c3666b4Skk * Otherwise, if an error has occurred, running becomes -1. 5797c3666b4Skk */ 5807c3666b4Skk while (running == 1) { 5817c478bd9Sstevel@tonic-gate 5827c478bd9Sstevel@tonic-gate if (Psetrun(pctx->Pr, 0, 0) != 0) { 5837c478bd9Sstevel@tonic-gate if (pctx->verbose) 5847c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, 5857c478bd9Sstevel@tonic-gate gettext("%d: Psetrun\n"), (int)pid); 5867c478bd9Sstevel@tonic-gate break; 5877c478bd9Sstevel@tonic-gate } 5887c478bd9Sstevel@tonic-gate 5897c478bd9Sstevel@tonic-gate if (msec != 0) { 5907c478bd9Sstevel@tonic-gate /* 5917c478bd9Sstevel@tonic-gate * This timing loop attempts to estimate the number 5927c478bd9Sstevel@tonic-gate * of milliseconds between our "goal" time (when 5937c478bd9Sstevel@tonic-gate * we should stop the process and run the tick 5947c478bd9Sstevel@tonic-gate * routine) and the current time. 5957c478bd9Sstevel@tonic-gate * 5967c478bd9Sstevel@tonic-gate * If we ever find ourselves running behind i.e. we 5977c478bd9Sstevel@tonic-gate * missed our goal, then we skip ahead to the next 5987c478bd9Sstevel@tonic-gate * goal instead. 5997c478bd9Sstevel@tonic-gate */ 6007c478bd9Sstevel@tonic-gate do { 6017c478bd9Sstevel@tonic-gate (void) gettimeofday(&tvnow, 0); 6027c478bd9Sstevel@tonic-gate if ((mswait = msdiff(&tvgoal, &tvnow)) == 0) { 6037c478bd9Sstevel@tonic-gate msincr(&tvgoal, msec); 6047c478bd9Sstevel@tonic-gate /* 6057c478bd9Sstevel@tonic-gate * Skip ahead to the next goal, unless 6067c478bd9Sstevel@tonic-gate * there is only one more sample left 6077c478bd9Sstevel@tonic-gate * to take. 6087c478bd9Sstevel@tonic-gate */ 6097c478bd9Sstevel@tonic-gate if (nsamples != 1) 6107c478bd9Sstevel@tonic-gate nsamples--; 6117c478bd9Sstevel@tonic-gate } 6127c478bd9Sstevel@tonic-gate } while (mswait == 0); 6137c478bd9Sstevel@tonic-gate } 6147c478bd9Sstevel@tonic-gate 6157c478bd9Sstevel@tonic-gate (void) Pwait(pctx->Pr, mswait); 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate checkstate: 6187c478bd9Sstevel@tonic-gate switch (pstate = Pstate(pctx->Pr)) { 6197c478bd9Sstevel@tonic-gate case PS_RUN: 6207c478bd9Sstevel@tonic-gate /* 6217c478bd9Sstevel@tonic-gate * Try again, but wait for up to 5 seconds. 6227c478bd9Sstevel@tonic-gate */ 6237c478bd9Sstevel@tonic-gate if (Pstop(pctx->Pr, 5 * MILLISEC) == -1 || 6247c478bd9Sstevel@tonic-gate (pstate = Pstate(pctx->Pr)) != PS_STOP) { 6257c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, 6267c478bd9Sstevel@tonic-gate gettext("%d: won't stop\n"), (int)pid); 6277c478bd9Sstevel@tonic-gate } 6287c478bd9Sstevel@tonic-gate break; 6297c478bd9Sstevel@tonic-gate case PS_STOP: 6307c478bd9Sstevel@tonic-gate break; 6317c478bd9Sstevel@tonic-gate case PS_LOST: 6327c478bd9Sstevel@tonic-gate /* 6337c478bd9Sstevel@tonic-gate * Lost control - probably execed a setuid/setgid 6347c478bd9Sstevel@tonic-gate * executable. Try and get control back again, 6357c478bd9Sstevel@tonic-gate * else bail .. 6367c478bd9Sstevel@tonic-gate */ 6377c478bd9Sstevel@tonic-gate (void) Preopen(pctx->Pr); 6387c478bd9Sstevel@tonic-gate if ((pstate = Pstate(pctx->Pr)) != PS_LOST) 6397c478bd9Sstevel@tonic-gate goto checkstate; 6407c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, 6417c478bd9Sstevel@tonic-gate gettext("%d: execed a program that cannot " 6427c478bd9Sstevel@tonic-gate "be tracked\n"), (int)pid); 6437c3666b4Skk running = -1; 6447c478bd9Sstevel@tonic-gate break; 6457c478bd9Sstevel@tonic-gate case PS_UNDEAD: 6467c478bd9Sstevel@tonic-gate case PS_DEAD: 6477c478bd9Sstevel@tonic-gate if (pctx->verbose) 6487c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, 6497c478bd9Sstevel@tonic-gate gettext("%d: process terminated\n"), 6507c478bd9Sstevel@tonic-gate (int)pid); 6517c3666b4Skk running = -1; 6527c478bd9Sstevel@tonic-gate break; 6537c478bd9Sstevel@tonic-gate default: 6547c478bd9Sstevel@tonic-gate if (pctx->verbose) 6557c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, 6567c478bd9Sstevel@tonic-gate gettext("%d: process state 0x%x?\n"), 6577c478bd9Sstevel@tonic-gate (int)pid, pstate); 6587c478bd9Sstevel@tonic-gate break; 6597c478bd9Sstevel@tonic-gate } 6607c478bd9Sstevel@tonic-gate 6617c478bd9Sstevel@tonic-gate if (pstate != PS_STOP) 6627c478bd9Sstevel@tonic-gate break; 6637c478bd9Sstevel@tonic-gate 6647c478bd9Sstevel@tonic-gate pstatus = Pstatus(pctx->Pr); 6657c478bd9Sstevel@tonic-gate lwpid = pstatus->pr_lwp.pr_lwpid; 6667c478bd9Sstevel@tonic-gate switch (pstatus->pr_lwp.pr_why) { 6677c478bd9Sstevel@tonic-gate case PR_REQUESTED: 6687c478bd9Sstevel@tonic-gate msincr(&tvgoal, msec); 6697c478bd9Sstevel@tonic-gate if (pstatus->pr_flags & PR_VFORKP) { 6707c478bd9Sstevel@tonic-gate /* 6717c478bd9Sstevel@tonic-gate * The process is in a vfork stupor until 6727c478bd9Sstevel@tonic-gate * its child releases it via an exec. 6737c478bd9Sstevel@tonic-gate * Don't sample it while it's in this state 6747c478bd9Sstevel@tonic-gate * - we won't be able to create the agent. 6757c478bd9Sstevel@tonic-gate */ 6767c478bd9Sstevel@tonic-gate break; 6777c478bd9Sstevel@tonic-gate } 6787c478bd9Sstevel@tonic-gate if (pctx_lwpiterate(pctx, tick) != 0) 6797c3666b4Skk running = -1; 6807c3666b4Skk if (running == 1 && --nsamples == 0) 6817c478bd9Sstevel@tonic-gate running = 0; 6827c478bd9Sstevel@tonic-gate break; 6837c478bd9Sstevel@tonic-gate case PR_SYSENTRY: 6847c478bd9Sstevel@tonic-gate switch (pstatus->pr_lwp.pr_what) { 6857c478bd9Sstevel@tonic-gate case SYS_lwp_exit: 6867c478bd9Sstevel@tonic-gate pctx_begin_syscalls(pctx); 6877c478bd9Sstevel@tonic-gate (void) pctx->fini_lwp(pctx, 6887c478bd9Sstevel@tonic-gate pid, lwpid, pctx->uarg); 6897c478bd9Sstevel@tonic-gate (void) pctx->lwp_exit(pctx, 6907c478bd9Sstevel@tonic-gate pid, lwpid, pctx->uarg); 6917c478bd9Sstevel@tonic-gate pctx_end_syscalls(pctx); 6927c478bd9Sstevel@tonic-gate break; 6937c478bd9Sstevel@tonic-gate case SYS_exit: 6947c3666b4Skk if (pctx_lwpiterate(pctx, pctx->fini_lwp) 6957c3666b4Skk != 0) 6967c3666b4Skk running = -1; 6977c478bd9Sstevel@tonic-gate pctx->exit(pctx, pid, lwpid, 6987c478bd9Sstevel@tonic-gate (int)pstatus->pr_lwp.pr_sysarg[0], 6997c478bd9Sstevel@tonic-gate pctx->uarg); 7007c3666b4Skk if (running == 1) 7017c3666b4Skk running = 0; 7027c478bd9Sstevel@tonic-gate break; 7037c478bd9Sstevel@tonic-gate case SYS_exec: 7047c478bd9Sstevel@tonic-gate case SYS_execve: 7057c478bd9Sstevel@tonic-gate (void) pctx_lwpiterate(pctx, pctx->fini_lwp); 7067c478bd9Sstevel@tonic-gate break; 7077c478bd9Sstevel@tonic-gate default: 7087c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, 7097c478bd9Sstevel@tonic-gate "warning - pid %d sysentry(%d)\n", 7107c478bd9Sstevel@tonic-gate (int)pid, pstatus->pr_lwp.pr_what); 7117c478bd9Sstevel@tonic-gate break; 7127c478bd9Sstevel@tonic-gate } 7137c478bd9Sstevel@tonic-gate break; 7147c478bd9Sstevel@tonic-gate case PR_SYSEXIT: 7157c478bd9Sstevel@tonic-gate switch (pstatus->pr_lwp.pr_what) { 7167c478bd9Sstevel@tonic-gate case SYS_exec: 7177c478bd9Sstevel@tonic-gate case SYS_execve: 7187c478bd9Sstevel@tonic-gate if (pstatus->pr_lwp.pr_errno) { 7197c478bd9Sstevel@tonic-gate /* 7207c478bd9Sstevel@tonic-gate * The exec failed completely. 7217c478bd9Sstevel@tonic-gate * Reinstate the lwps we fini'd 7227c478bd9Sstevel@tonic-gate * at exec entrance 7237c478bd9Sstevel@tonic-gate */ 7247c3666b4Skk if (pctx_lwpiterate(pctx, 7257c3666b4Skk pctx->init_lwp) == 0) 7267c3666b4Skk running = 1; 7277c3666b4Skk else 7287c3666b4Skk running = -1; 7297c478bd9Sstevel@tonic-gate break; 7307c478bd9Sstevel@tonic-gate } 7317c478bd9Sstevel@tonic-gate if (pctx->exec == (pctx_sysc_execfn_t *) 7327c478bd9Sstevel@tonic-gate default_int) { 7337c478bd9Sstevel@tonic-gate running = 0; 7347c478bd9Sstevel@tonic-gate break; 7357c478bd9Sstevel@tonic-gate } 7367c478bd9Sstevel@tonic-gate (void) memcpy(&psinfo, 7377c478bd9Sstevel@tonic-gate Ppsinfo(pctx->Pr), sizeof (psinfo)); 7387c478bd9Sstevel@tonic-gate proc_unctrl_psinfo(&psinfo); 7397c478bd9Sstevel@tonic-gate pctx_begin_syscalls(pctx); 7407c3666b4Skk if (pctx->exec(pctx, pid, lwpid, 7417c3666b4Skk psinfo.pr_psargs, pctx->uarg) != 0) 7427c3666b4Skk running = -1; 7437c3666b4Skk if (running == 1 && pctx->init_lwp(pctx, 7447c3666b4Skk pid, 1, pctx->uarg) != 0) 7457c3666b4Skk running = -1; 7467c478bd9Sstevel@tonic-gate pctx_end_syscalls(pctx); 7477c478bd9Sstevel@tonic-gate break; 7487c478bd9Sstevel@tonic-gate case SYS_lwp_create: 7497c478bd9Sstevel@tonic-gate if (pstatus->pr_lwp.pr_errno || 7507c478bd9Sstevel@tonic-gate pstatus->pr_lwp.pr_rval1) 7517c478bd9Sstevel@tonic-gate break; 7527c478bd9Sstevel@tonic-gate pctx_begin_syscalls(pctx); 7537c3666b4Skk if (pctx->init_lwp(pctx, pid, lwpid, 7547c3666b4Skk pctx->uarg) != 0) 7557c3666b4Skk running = -1; 7567c3666b4Skk if (running == 1 && pctx->lwp_create(pctx, 7577c3666b4Skk pid, lwpid, pctx->uarg) != 0) 7587c3666b4Skk running = -1; 7597c478bd9Sstevel@tonic-gate pctx_end_syscalls(pctx); 7607c478bd9Sstevel@tonic-gate break; 7617c478bd9Sstevel@tonic-gate case SYS_forkall: 7627c478bd9Sstevel@tonic-gate case SYS_vfork: 7637c478bd9Sstevel@tonic-gate case SYS_fork1: 764*657b1f3dSraf case SYS_forksys: 7657c478bd9Sstevel@tonic-gate if (pstatus->pr_lwp.pr_errno) 7667c478bd9Sstevel@tonic-gate break; 7677c478bd9Sstevel@tonic-gate (void) fflush(NULL); 7687c478bd9Sstevel@tonic-gate switch (fork1()) { 7697c478bd9Sstevel@tonic-gate pid_t ppid; 7707c478bd9Sstevel@tonic-gate int wascreated; 7717c478bd9Sstevel@tonic-gate pctx_sysc_forkfn_t *forkfn; 7727c478bd9Sstevel@tonic-gate case 0: 7737c478bd9Sstevel@tonic-gate ppid = pid; 7747c478bd9Sstevel@tonic-gate pid = pstatus->pr_lwp.pr_rval1; 7757c478bd9Sstevel@tonic-gate wascreated = pctx->created; 7767c478bd9Sstevel@tonic-gate forkfn = pctx->fork; 7777c478bd9Sstevel@tonic-gate pctx_free(pctx); 7787c478bd9Sstevel@tonic-gate pctx = pctx_capture(pid, pctx->uarg, 7797c478bd9Sstevel@tonic-gate pctx->verbose, pctx->errfn); 7807c478bd9Sstevel@tonic-gate if (pctx != NULL) { 7817c478bd9Sstevel@tonic-gate if (wascreated) { 7827c478bd9Sstevel@tonic-gate /* 7837c478bd9Sstevel@tonic-gate * Set kill on last 7847c478bd9Sstevel@tonic-gate * close so -all- 7857c478bd9Sstevel@tonic-gate * children die. 7867c478bd9Sstevel@tonic-gate */ 7877c478bd9Sstevel@tonic-gate pctx->created = 1; 7887c478bd9Sstevel@tonic-gate (void) Psetflags( 7897c478bd9Sstevel@tonic-gate pctx->Pr, PR_KLC); 7907c478bd9Sstevel@tonic-gate } 7917c478bd9Sstevel@tonic-gate (*forkfn)(pctx, ppid, pid, 7927c478bd9Sstevel@tonic-gate lwpid, pctx->uarg); 7937c478bd9Sstevel@tonic-gate pctx_release(pctx); 7947c3666b4Skk _exit(0); 7957c3666b4Skk } else { 7967c3666b4Skk _exit(1); 7977c478bd9Sstevel@tonic-gate } 7987c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 7997c478bd9Sstevel@tonic-gate case -1: 8007c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, 8017c478bd9Sstevel@tonic-gate "cannot follow pid %d: %s\n", 8027c478bd9Sstevel@tonic-gate (int)pstatus->pr_lwp.pr_rval1, 8037c478bd9Sstevel@tonic-gate strerror(errno)); 8047c478bd9Sstevel@tonic-gate break; 8057c478bd9Sstevel@tonic-gate default: 8067c478bd9Sstevel@tonic-gate break; 8077c478bd9Sstevel@tonic-gate } 8087c478bd9Sstevel@tonic-gate break; 8097c478bd9Sstevel@tonic-gate default: 8107c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, gettext( 8117c478bd9Sstevel@tonic-gate "warning - pid %d sysexit(%d)\n"), 8127c478bd9Sstevel@tonic-gate (int)pid, pstatus->pr_lwp.pr_what); 8137c478bd9Sstevel@tonic-gate break; 8147c478bd9Sstevel@tonic-gate } 8157c478bd9Sstevel@tonic-gate break; 8167c478bd9Sstevel@tonic-gate case PR_SIGNALLED: 8177c478bd9Sstevel@tonic-gate if (pctx->verbose) 8187c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, 8197c478bd9Sstevel@tonic-gate gettext("pid %d - signalled\n"), (int)pid); 8207c478bd9Sstevel@tonic-gate break; 8217c478bd9Sstevel@tonic-gate case PR_JOBCONTROL: 8227c478bd9Sstevel@tonic-gate if (pctx->verbose) 8237c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, 8247c478bd9Sstevel@tonic-gate gettext("pid %d - job control stop\n"), 8257c478bd9Sstevel@tonic-gate (int)pid); 8267c3666b4Skk running = -1; 8277c478bd9Sstevel@tonic-gate break; 8287c478bd9Sstevel@tonic-gate case PR_FAULTED: 8297c478bd9Sstevel@tonic-gate if (pctx->verbose) 8307c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, 8317c478bd9Sstevel@tonic-gate gettext("pid %d - faulted\n"), (int)pid); 8327c478bd9Sstevel@tonic-gate break; 8337c478bd9Sstevel@tonic-gate case PR_SUSPENDED: 8347c478bd9Sstevel@tonic-gate if (pctx->verbose) 8357c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, 8367c478bd9Sstevel@tonic-gate gettext("pid %d - suspended\n"), (int)pid); 8377c478bd9Sstevel@tonic-gate break; 8387c478bd9Sstevel@tonic-gate case PR_CHECKPOINT: 8397c478bd9Sstevel@tonic-gate if (pctx->verbose) 8407c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, 8417c478bd9Sstevel@tonic-gate gettext("pid %d - checkpoint\n"), 8427c478bd9Sstevel@tonic-gate (int)pid); 8437c478bd9Sstevel@tonic-gate break; 8447c478bd9Sstevel@tonic-gate default: 8457c478bd9Sstevel@tonic-gate if (pctx->verbose) 8467c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, 8477c478bd9Sstevel@tonic-gate gettext("pid %d - reason %d\n"), 8487c478bd9Sstevel@tonic-gate (int)pid, pstatus->pr_lwp.pr_why); 8497c3666b4Skk running = -1; 8507c478bd9Sstevel@tonic-gate break; 8517c478bd9Sstevel@tonic-gate } 8527c478bd9Sstevel@tonic-gate } 8537c478bd9Sstevel@tonic-gate 8547c478bd9Sstevel@tonic-gate bailout: 8557c478bd9Sstevel@tonic-gate (void) signal(SIGCHLD, sigsaved); 8567c478bd9Sstevel@tonic-gate 8577c3666b4Skk switch (running) { 8587c3666b4Skk case 0: 8597c3666b4Skk return (0); 8607c3666b4Skk case -1: 8617c3666b4Skk return (-1); 8627c3666b4Skk default: 8637c3666b4Skk pctx_error(pctx, fn, gettext("lost control of pid %d\n"), 8647c3666b4Skk (int)pid); 8657c3666b4Skk pctx_free(pctx); 8667c3666b4Skk return (-1); 8677c3666b4Skk } 8687c478bd9Sstevel@tonic-gate } 8697c478bd9Sstevel@tonic-gate 8707c478bd9Sstevel@tonic-gate /* 8717c478bd9Sstevel@tonic-gate * Execute the private 'cpc' system call in the context of the 8727c478bd9Sstevel@tonic-gate * controlled process. 8737c478bd9Sstevel@tonic-gate */ 8747c478bd9Sstevel@tonic-gate int 8757c478bd9Sstevel@tonic-gate __pctx_cpc(pctx_t *pctx, cpc_t *cpc, 8767c478bd9Sstevel@tonic-gate int cmd, id_t lwpid, void *data1, void *data2, void *data3, int bufsize) 8777c478bd9Sstevel@tonic-gate { 8787c478bd9Sstevel@tonic-gate sysret_t rval; 8797c478bd9Sstevel@tonic-gate argdes_t argd[5]; 8807c478bd9Sstevel@tonic-gate argdes_t *adp = &argd[0]; 8817c478bd9Sstevel@tonic-gate int error; 8827c478bd9Sstevel@tonic-gate 8837c478bd9Sstevel@tonic-gate /* 8847c478bd9Sstevel@tonic-gate * Keep track of the relationship between cpc_t and pctx_t here. 8857c478bd9Sstevel@tonic-gate * We store the last cpc_t used by libpctx, so that when this pctx is 8867c478bd9Sstevel@tonic-gate * destroyed, libpctx can notify libcpc. 8877c478bd9Sstevel@tonic-gate */ 8887c478bd9Sstevel@tonic-gate if (pctx->cpc != NULL && pctx->cpc != cpc && pctx_cpc_callback != NULL) 8897c478bd9Sstevel@tonic-gate (*pctx_cpc_callback)(pctx->cpc, pctx); 8907c478bd9Sstevel@tonic-gate pctx->cpc = cpc; 8917c478bd9Sstevel@tonic-gate 8927c478bd9Sstevel@tonic-gate /* 8937c478bd9Sstevel@tonic-gate * cmd and lwpid are passed in by value no matter what the command is. 8947c478bd9Sstevel@tonic-gate */ 8957c478bd9Sstevel@tonic-gate adp->arg_value = cmd; 8967c478bd9Sstevel@tonic-gate adp->arg_object = NULL; 8977c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL; 8987c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT; 8997c478bd9Sstevel@tonic-gate adp->arg_size = 0; 9007c478bd9Sstevel@tonic-gate adp++; 9017c478bd9Sstevel@tonic-gate 9027c478bd9Sstevel@tonic-gate adp->arg_value = lwpid; 9037c478bd9Sstevel@tonic-gate adp->arg_object = NULL; 9047c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL; 9057c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT; 9067c478bd9Sstevel@tonic-gate adp->arg_size = 0; 9077c478bd9Sstevel@tonic-gate adp++; 9087c478bd9Sstevel@tonic-gate 9097c478bd9Sstevel@tonic-gate switch (cmd) { 9107c478bd9Sstevel@tonic-gate case CPC_BIND: 9117c478bd9Sstevel@tonic-gate adp->arg_value = 0; 9127c478bd9Sstevel@tonic-gate adp->arg_object = data1; 9137c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYREF; 9147c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT; 9157c478bd9Sstevel@tonic-gate adp->arg_size = (size_t)data2; 9167c478bd9Sstevel@tonic-gate adp++; 9177c478bd9Sstevel@tonic-gate 9187c478bd9Sstevel@tonic-gate adp->arg_value = (size_t)data2; 9197c478bd9Sstevel@tonic-gate adp->arg_object = NULL; 9207c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL; 9217c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT; 9227c478bd9Sstevel@tonic-gate adp->arg_size = 0; 9237c478bd9Sstevel@tonic-gate adp++; 9247c478bd9Sstevel@tonic-gate 9257c478bd9Sstevel@tonic-gate adp->arg_value = 0; 9267c478bd9Sstevel@tonic-gate adp->arg_object = data3; 9277c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYREF; 9287c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INOUT; 9297c478bd9Sstevel@tonic-gate adp->arg_size = sizeof (int); 9307c478bd9Sstevel@tonic-gate 9317c478bd9Sstevel@tonic-gate break; 9327c478bd9Sstevel@tonic-gate case CPC_SAMPLE: 9337c478bd9Sstevel@tonic-gate adp->arg_value = 0; 9347c478bd9Sstevel@tonic-gate adp->arg_object = data1; 9357c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYREF; 9367c478bd9Sstevel@tonic-gate adp->arg_inout = AI_OUTPUT; 9377c478bd9Sstevel@tonic-gate adp->arg_size = bufsize; 9387c478bd9Sstevel@tonic-gate adp++; 9397c478bd9Sstevel@tonic-gate 9407c478bd9Sstevel@tonic-gate adp->arg_value = 0; 9417c478bd9Sstevel@tonic-gate adp->arg_object = data2; 9427c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYREF; 9437c478bd9Sstevel@tonic-gate adp->arg_inout = AI_OUTPUT; 9447c478bd9Sstevel@tonic-gate adp->arg_size = sizeof (hrtime_t); 9457c478bd9Sstevel@tonic-gate adp++; 9467c478bd9Sstevel@tonic-gate 9477c478bd9Sstevel@tonic-gate adp->arg_value = 0; 9487c478bd9Sstevel@tonic-gate adp->arg_object = data3; 9497c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYREF; 9507c478bd9Sstevel@tonic-gate adp->arg_inout = AI_OUTPUT; 9517c478bd9Sstevel@tonic-gate adp->arg_size = sizeof (uint64_t); 9527c478bd9Sstevel@tonic-gate 9537c478bd9Sstevel@tonic-gate break; 9547c478bd9Sstevel@tonic-gate default: 9557c478bd9Sstevel@tonic-gate adp->arg_value = 0; 9567c478bd9Sstevel@tonic-gate adp->arg_object = 0; 9577c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL; 9587c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT; 9597c478bd9Sstevel@tonic-gate adp->arg_size = 0; 9607c478bd9Sstevel@tonic-gate adp++; 9617c478bd9Sstevel@tonic-gate 9627c478bd9Sstevel@tonic-gate adp->arg_value = 0; 9637c478bd9Sstevel@tonic-gate adp->arg_object = 0; 9647c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL; 9657c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT; 9667c478bd9Sstevel@tonic-gate adp->arg_size = 0; 9677c478bd9Sstevel@tonic-gate adp++; 9687c478bd9Sstevel@tonic-gate 9697c478bd9Sstevel@tonic-gate adp->arg_value = 0; 9707c478bd9Sstevel@tonic-gate adp->arg_object = 0; 9717c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL; 9727c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT; 9737c478bd9Sstevel@tonic-gate adp->arg_size = 0; 9747c478bd9Sstevel@tonic-gate 9757c478bd9Sstevel@tonic-gate break; 9767c478bd9Sstevel@tonic-gate } 9777c478bd9Sstevel@tonic-gate 9787c478bd9Sstevel@tonic-gate error = Psyscall(pctx->Pr, &rval, SYS_cpc, 5, &argd[0]); 9797c478bd9Sstevel@tonic-gate 9807c478bd9Sstevel@tonic-gate if (error) { 9817c478bd9Sstevel@tonic-gate errno = error > 0 ? error : ENOSYS; 9827c478bd9Sstevel@tonic-gate return (-1); 9837c478bd9Sstevel@tonic-gate } 9847c478bd9Sstevel@tonic-gate return (rval.sys_rval1); 9857c478bd9Sstevel@tonic-gate } 9867c478bd9Sstevel@tonic-gate 9877c478bd9Sstevel@tonic-gate /* 9887c478bd9Sstevel@tonic-gate * libcpc-private hook used to register a callback. The callback is used to 9897c478bd9Sstevel@tonic-gate * notify libcpc when a pctx handle is invalidated. 9907c478bd9Sstevel@tonic-gate */ 9917c478bd9Sstevel@tonic-gate void 9927c478bd9Sstevel@tonic-gate __pctx_cpc_register_callback(void (*arg)(struct __cpc *, struct __pctx *)) 9937c478bd9Sstevel@tonic-gate { 9947c478bd9Sstevel@tonic-gate pctx_cpc_callback = arg; 9957c478bd9Sstevel@tonic-gate } 996