xref: /onnv-gate/usr/src/cmd/truss/actions.c (revision 10927)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53235Sraf  * Common Development and Distribution License (the "License").
63235Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
213235Sraf 
220Sstevel@tonic-gate /*
23*10927SRoger.Faulkner@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <ctype.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <memory.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <limits.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/stack.h>
400Sstevel@tonic-gate #include <signal.h>
410Sstevel@tonic-gate #include <sys/isa_defs.h>
420Sstevel@tonic-gate #include <libproc.h>
430Sstevel@tonic-gate #include <priv.h>
440Sstevel@tonic-gate #include "ramdata.h"
450Sstevel@tonic-gate #include "systable.h"
460Sstevel@tonic-gate #include "print.h"
470Sstevel@tonic-gate #include "proto.h"
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * Actions to take when process stops.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * Function prototypes for static routines in this module.
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate int	stopsig(private_t *);
570Sstevel@tonic-gate void	showpaths(private_t *, const struct systable *);
580Sstevel@tonic-gate void	showargs(private_t *, int);
590Sstevel@tonic-gate void	dumpargs(private_t *, long, const char *);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * Report an lwp to be sleeping (if true).
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate void
650Sstevel@tonic-gate report_sleeping(private_t *pri, int dotrace)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
680Sstevel@tonic-gate 	int sys = Lsp->pr_syscall;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	if (!prismember(&trace, sys) || !dotrace ||
710Sstevel@tonic-gate 	    !(Lsp->pr_flags & (PR_ASLEEP|PR_VFORKP))) {
720Sstevel@tonic-gate 		/* Make sure we catch sysexit even if we're not tracing it. */
730Sstevel@tonic-gate 		(void) Psysexit(Proc, sys, TRUE);
740Sstevel@tonic-gate 		return;
750Sstevel@tonic-gate 	}
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	pri->length = 0;
780Sstevel@tonic-gate 	pri->Errno = 0;
790Sstevel@tonic-gate 	pri->ErrPriv = PRIV_NONE;
800Sstevel@tonic-gate 	pri->Rval1 = pri->Rval2 = 0;
810Sstevel@tonic-gate 	(void) sysentry(pri, dotrace);
820Sstevel@tonic-gate 	make_pname(pri, 0);
830Sstevel@tonic-gate 	putpname(pri);
840Sstevel@tonic-gate 	timestamp(pri);
850Sstevel@tonic-gate 	pri->length += printf("%s", pri->sys_string);
860Sstevel@tonic-gate 	pri->sys_leng = 0;
870Sstevel@tonic-gate 	*pri->sys_string = '\0';
880Sstevel@tonic-gate 	pri->length >>= 3;
890Sstevel@tonic-gate 	if (Lsp->pr_flags & PR_VFORKP)
900Sstevel@tonic-gate 		pri->length += 2;
910Sstevel@tonic-gate 	if (pri->length >= 4)
920Sstevel@tonic-gate 		(void) fputc(' ', stdout);
930Sstevel@tonic-gate 	for (; pri->length < 4; pri->length++)
940Sstevel@tonic-gate 		(void) fputc('\t', stdout);
950Sstevel@tonic-gate 	if (Lsp->pr_flags & PR_VFORKP)
960Sstevel@tonic-gate 		(void) fputs("(waiting for child to exit()/exec()...)\n",
97*10927SRoger.Faulkner@Sun.COM 		    stdout);
980Sstevel@tonic-gate 	else
990Sstevel@tonic-gate 		(void) fputs("(sleeping...)\n", stdout);
1000Sstevel@tonic-gate 	pri->length = 0;
1010Sstevel@tonic-gate 	if (prismember(&verbose, sys)) {
1020Sstevel@tonic-gate 		int raw = prismember(&rawout, sys);
1030Sstevel@tonic-gate 		pri->Errno = 1;
1040Sstevel@tonic-gate 		expound(pri, 0, raw);
1050Sstevel@tonic-gate 		pri->Errno = 0;
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 	Flush();
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate /*
1110Sstevel@tonic-gate  * requested() gets called for these reasons:
1120Sstevel@tonic-gate  *	flag == JOBSIG:		report nothing; change state to JOBSTOP
1130Sstevel@tonic-gate  *	flag == JOBSTOP:	report "Continued ..."
1140Sstevel@tonic-gate  *	default:		report sleeping system call
1150Sstevel@tonic-gate  *
1160Sstevel@tonic-gate  * It returns a new flag:  JOBSTOP or SLEEPING or 0.
1170Sstevel@tonic-gate  */
1180Sstevel@tonic-gate int
1190Sstevel@tonic-gate requested(private_t *pri, int flag, int dotrace)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
1220Sstevel@tonic-gate 	int sig = Lsp->pr_cursig;
1230Sstevel@tonic-gate 	int newflag = 0;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	switch (flag) {
1260Sstevel@tonic-gate 	case JOBSIG:
1270Sstevel@tonic-gate 		return (JOBSTOP);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	case JOBSTOP:
1300Sstevel@tonic-gate 		if (dotrace && !cflag && prismember(&signals, sig)) {
1310Sstevel@tonic-gate 			pri->length = 0;
1320Sstevel@tonic-gate 			putpname(pri);
1330Sstevel@tonic-gate 			timestamp(pri);
1340Sstevel@tonic-gate 			(void) printf("    Continued with signal #%d, %s",
135*10927SRoger.Faulkner@Sun.COM 			    sig, signame(pri, sig));
1360Sstevel@tonic-gate 			if (Lsp->pr_action.sa_handler == SIG_DFL)
1370Sstevel@tonic-gate 				(void) printf(" [default]");
1380Sstevel@tonic-gate 			else if (Lsp->pr_action.sa_handler == SIG_IGN)
1390Sstevel@tonic-gate 				(void) printf(" [ignored]");
1400Sstevel@tonic-gate 			else
1410Sstevel@tonic-gate 				(void) printf(" [caught]");
1420Sstevel@tonic-gate 			(void) fputc('\n', stdout);
1430Sstevel@tonic-gate 			Flush();
1440Sstevel@tonic-gate 		}
1450Sstevel@tonic-gate 		newflag = 0;
1460Sstevel@tonic-gate 		break;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	default:
1490Sstevel@tonic-gate 		newflag = SLEEPING;
1500Sstevel@tonic-gate 		if (!cflag)
1510Sstevel@tonic-gate 			report_sleeping(pri, dotrace);
1520Sstevel@tonic-gate 		break;
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	return (newflag);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate int
1590Sstevel@tonic-gate jobcontrol(private_t *pri, int dotrace)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
1620Sstevel@tonic-gate 	int sig = stopsig(pri);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (sig == 0)
1650Sstevel@tonic-gate 		return (0);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if (dotrace && !cflag &&		/* not just counting */
1680Sstevel@tonic-gate 	    prismember(&signals, sig)) {	/* tracing this signal */
1690Sstevel@tonic-gate 		int sys;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 		pri->length = 0;
1720Sstevel@tonic-gate 		putpname(pri);
1730Sstevel@tonic-gate 		timestamp(pri);
1740Sstevel@tonic-gate 		(void) printf("    Stopped by signal #%d, %s",
175*10927SRoger.Faulkner@Sun.COM 		    sig, signame(pri, sig));
1760Sstevel@tonic-gate 		if ((Lsp->pr_flags & PR_ASLEEP) &&
1770Sstevel@tonic-gate 		    (sys = Lsp->pr_syscall) > 0 && sys <= PRMAXSYS)
1780Sstevel@tonic-gate 			(void) printf(", in %s()",
179*10927SRoger.Faulkner@Sun.COM 			    sysname(pri, sys, getsubcode(pri)));
1800Sstevel@tonic-gate 		(void) fputc('\n', stdout);
1810Sstevel@tonic-gate 		Flush();
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	return (JOBSTOP);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate /*
1880Sstevel@tonic-gate  * Return the signal the process stopped on iff process is already stopped on
1890Sstevel@tonic-gate  * PR_JOBCONTROL or is stopped on PR_SIGNALLED or PR_REQUESTED with a current
1900Sstevel@tonic-gate  * signal that will cause a JOBCONTROL stop when the process is set running.
1910Sstevel@tonic-gate  */
1920Sstevel@tonic-gate int
1930Sstevel@tonic-gate stopsig(private_t *pri)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
1960Sstevel@tonic-gate 	int sig = 0;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	if (Lsp->pr_flags & PR_STOPPED) {
1990Sstevel@tonic-gate 		switch (Lsp->pr_why) {
2000Sstevel@tonic-gate 		case PR_JOBCONTROL:
2010Sstevel@tonic-gate 			sig = Lsp->pr_what;
2020Sstevel@tonic-gate 			if (sig < 0 || sig > PRMAXSIG)
2030Sstevel@tonic-gate 				sig = 0;
2040Sstevel@tonic-gate 			break;
2050Sstevel@tonic-gate 		case PR_SIGNALLED:
2060Sstevel@tonic-gate 		case PR_REQUESTED:
2070Sstevel@tonic-gate 			if (Lsp->pr_action.sa_handler == SIG_DFL) {
2080Sstevel@tonic-gate 				switch (Lsp->pr_cursig) {
2090Sstevel@tonic-gate 				case SIGSTOP:
2100Sstevel@tonic-gate 					sig = SIGSTOP;
2110Sstevel@tonic-gate 					break;
2120Sstevel@tonic-gate 				case SIGTSTP:
2130Sstevel@tonic-gate 				case SIGTTIN:
2140Sstevel@tonic-gate 				case SIGTTOU:
2150Sstevel@tonic-gate 					if (!(Lsp->pr_flags & PR_ORPHAN))
2160Sstevel@tonic-gate 						sig = Lsp->pr_cursig;
2170Sstevel@tonic-gate 					break;
2180Sstevel@tonic-gate 				}
2190Sstevel@tonic-gate 			}
2200Sstevel@tonic-gate 			break;
2210Sstevel@tonic-gate 		}
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	return (sig);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate int
2280Sstevel@tonic-gate signalled(private_t *pri, int flag, int dotrace)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
2310Sstevel@tonic-gate 	int sig = Lsp->pr_what;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	if (sig <= 0 || sig > PRMAXSIG)	/* check bounds */
2340Sstevel@tonic-gate 		return (0);
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	if (dotrace && cflag) {			/* just counting */
2370Sstevel@tonic-gate 		(void) mutex_lock(&count_lock);
2380Sstevel@tonic-gate 		Cp->sigcount[sig]++;
2390Sstevel@tonic-gate 		(void) mutex_unlock(&count_lock);
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	if (sig == SIGCONT && (flag == JOBSIG || flag == JOBSTOP))
2430Sstevel@tonic-gate 		flag = requested(pri, JOBSTOP, dotrace);
2440Sstevel@tonic-gate 	else if ((flag = jobcontrol(pri, dotrace)) == 0 &&
2450Sstevel@tonic-gate 	    !cflag && dotrace &&
2460Sstevel@tonic-gate 	    prismember(&signals, sig)) {
2470Sstevel@tonic-gate 		int sys;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 		pri->length = 0;
2500Sstevel@tonic-gate 		putpname(pri);
2510Sstevel@tonic-gate 		timestamp(pri);
2520Sstevel@tonic-gate 		(void) printf("    Received signal #%d, %s",
253*10927SRoger.Faulkner@Sun.COM 		    sig, signame(pri, sig));
2540Sstevel@tonic-gate 		if ((Lsp->pr_flags & PR_ASLEEP) &&
2550Sstevel@tonic-gate 		    (sys = Lsp->pr_syscall) > 0 && sys <= PRMAXSYS)
2560Sstevel@tonic-gate 			(void) printf(", in %s()",
257*10927SRoger.Faulkner@Sun.COM 			    sysname(pri, sys, getsubcode(pri)));
2580Sstevel@tonic-gate 		if (Lsp->pr_action.sa_handler == SIG_DFL)
2590Sstevel@tonic-gate 			(void) printf(" [default]");
2600Sstevel@tonic-gate 		else if (Lsp->pr_action.sa_handler == SIG_IGN)
2610Sstevel@tonic-gate 			(void) printf(" [ignored]");
2620Sstevel@tonic-gate 		else
2630Sstevel@tonic-gate 			(void) printf(" [caught]");
2640Sstevel@tonic-gate 		(void) fputc('\n', stdout);
2650Sstevel@tonic-gate 		if (Lsp->pr_info.si_code != 0 ||
2660Sstevel@tonic-gate 		    Lsp->pr_info.si_pid != 0)
2670Sstevel@tonic-gate 			print_siginfo(pri, &Lsp->pr_info);
2680Sstevel@tonic-gate 		Flush();
2690Sstevel@tonic-gate 	}
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	if (flag == JOBSTOP)
2720Sstevel@tonic-gate 		flag = JOBSIG;
2730Sstevel@tonic-gate 	return (flag);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate int
2770Sstevel@tonic-gate faulted(private_t *pri, int dotrace)
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
2800Sstevel@tonic-gate 	int flt = Lsp->pr_what;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	if ((uint_t)flt > PRMAXFAULT || !prismember(&faults, flt) || !dotrace)
2830Sstevel@tonic-gate 		return (0);
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	(void) mutex_lock(&count_lock);
2860Sstevel@tonic-gate 	Cp->fltcount[flt]++;
2870Sstevel@tonic-gate 	(void) mutex_unlock(&count_lock);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	if (cflag)		/* just counting */
2900Sstevel@tonic-gate 		return (1);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	pri->length = 0;
2930Sstevel@tonic-gate 	putpname(pri);
2940Sstevel@tonic-gate 	timestamp(pri);
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	(void) printf("    Incurred fault #%d, %s  %%pc = 0x%.8lX",
297*10927SRoger.Faulkner@Sun.COM 	    flt, proc_fltname(flt, pri->flt_name, sizeof (pri->flt_name)),
298*10927SRoger.Faulkner@Sun.COM 	    (long)Lsp->pr_reg[R_PC]);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	if (flt == FLTPAGE)
3010Sstevel@tonic-gate 		(void) printf("  addr = 0x%.8lX",
302*10927SRoger.Faulkner@Sun.COM 		    (long)Lsp->pr_info.si_addr);
3030Sstevel@tonic-gate 	(void) fputc('\n', stdout);
3040Sstevel@tonic-gate 	if (Lsp->pr_info.si_signo != 0)
3050Sstevel@tonic-gate 		print_siginfo(pri, &Lsp->pr_info);
3060Sstevel@tonic-gate 	Flush();
3070Sstevel@tonic-gate 	return (1);
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate  * Set up pri->sys_nargs and pri->sys_args[] (syscall args).
3120Sstevel@tonic-gate  */
3130Sstevel@tonic-gate void
3140Sstevel@tonic-gate setupsysargs(private_t *pri, int what)
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
3170Sstevel@tonic-gate 	int nargs;
3180Sstevel@tonic-gate 	int i;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate #if sparc
3210Sstevel@tonic-gate 	/* determine whether syscall is indirect */
3220Sstevel@tonic-gate 	pri->sys_indirect = (Lsp->pr_reg[R_G1] == SYS_syscall)? 1 : 0;
3230Sstevel@tonic-gate #else
3240Sstevel@tonic-gate 	pri->sys_indirect = 0;
3250Sstevel@tonic-gate #endif
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	(void) memset(pri->sys_args, 0, sizeof (pri->sys_args));
3280Sstevel@tonic-gate 	if (what != Lsp->pr_syscall) {	/* assertion */
3290Sstevel@tonic-gate 		(void) printf("%s\t*** Inconsistent syscall: %d vs %d ***\n",
330*10927SRoger.Faulkner@Sun.COM 		    pri->pname, what, Lsp->pr_syscall);
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate 	nargs = Lsp->pr_nsysarg;
3330Sstevel@tonic-gate 	for (i = 0;
3340Sstevel@tonic-gate 	    i < nargs && i < sizeof (pri->sys_args) / sizeof (pri->sys_args[0]);
3350Sstevel@tonic-gate 	    i++)
3360Sstevel@tonic-gate 		pri->sys_args[i] = Lsp->pr_sysarg[i];
3370Sstevel@tonic-gate 	pri->sys_nargs = nargs;
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate #define	ISREAD(code) \
3410Sstevel@tonic-gate 	((code) == SYS_read || (code) == SYS_pread || \
3420Sstevel@tonic-gate 	(code) == SYS_pread64 || (code) == SYS_readv || \
3430Sstevel@tonic-gate 	(code) == SYS_recv || (code) == SYS_recvfrom)
3440Sstevel@tonic-gate #define	ISWRITE(code) \
3450Sstevel@tonic-gate 	((code) == SYS_write || (code) == SYS_pwrite || \
3460Sstevel@tonic-gate 	(code) == SYS_pwrite64 || (code) == SYS_writev || \
3470Sstevel@tonic-gate 	(code) == SYS_send || (code) == SYS_sendto)
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate /*
3500Sstevel@tonic-gate  * Return TRUE iff syscall is being traced.
3510Sstevel@tonic-gate  */
3520Sstevel@tonic-gate int
3530Sstevel@tonic-gate sysentry(private_t *pri, int dotrace)
3540Sstevel@tonic-gate {
3550Sstevel@tonic-gate 	pid_t pid = Pstatus(Proc)->pr_pid;
3560Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
3570Sstevel@tonic-gate 	long arg;
3580Sstevel@tonic-gate 	int nargs;
3590Sstevel@tonic-gate 	int i;
3600Sstevel@tonic-gate 	int x;
3610Sstevel@tonic-gate 	int len;
3620Sstevel@tonic-gate 	char *s;
3630Sstevel@tonic-gate 	const struct systable *stp;
3640Sstevel@tonic-gate 	int what = Lsp->pr_what;
3650Sstevel@tonic-gate 	int subcode;
3660Sstevel@tonic-gate 	int istraced;
3670Sstevel@tonic-gate 	int raw;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	/* for reporting sleeping system calls */
3700Sstevel@tonic-gate 	if (what == 0 && (Lsp->pr_flags & (PR_ASLEEP|PR_VFORKP)))
3710Sstevel@tonic-gate 		what = Lsp->pr_syscall;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	/* protect ourself from operating system error */
3740Sstevel@tonic-gate 	if (what <= 0 || what > PRMAXSYS)
3750Sstevel@tonic-gate 		what = 0;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	/*
3780Sstevel@tonic-gate 	 * Set up the system call arguments (pri->sys_nargs & pri->sys_args[]).
3790Sstevel@tonic-gate 	 */
3800Sstevel@tonic-gate 	setupsysargs(pri, what);
3810Sstevel@tonic-gate 	nargs = pri->sys_nargs;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	/* get systable entry for this syscall */
3840Sstevel@tonic-gate 	subcode = getsubcode(pri);
3850Sstevel@tonic-gate 	stp = subsys(what, subcode);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	if (nargs > stp->nargs)
3880Sstevel@tonic-gate 		nargs = stp->nargs;
3890Sstevel@tonic-gate 	pri->sys_nargs = nargs;
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	/* fetch and remember first argument if it's a string */
3920Sstevel@tonic-gate 	pri->sys_valid = FALSE;
3930Sstevel@tonic-gate 	if (nargs > 0 && stp->arg[0] == STG) {
3940Sstevel@tonic-gate 		long offset;
3950Sstevel@tonic-gate 		uint32_t offset32;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 		/*
3980Sstevel@tonic-gate 		 * Special case for exit from exec().
3990Sstevel@tonic-gate 		 * The address in pri->sys_args[0] refers to the old process
4000Sstevel@tonic-gate 		 * image.  We must fetch the string from the new image.
4010Sstevel@tonic-gate 		 */
4020Sstevel@tonic-gate 		if (Lsp->pr_why == PR_SYSEXIT &&
4030Sstevel@tonic-gate 		    (Lsp->pr_what == SYS_execve ||
4040Sstevel@tonic-gate 		    Lsp->pr_what == SYS_exec)) {
4050Sstevel@tonic-gate 			psinfo_t psinfo;
4060Sstevel@tonic-gate 			long argv;
4070Sstevel@tonic-gate 			auxv_t auxv[32];
4080Sstevel@tonic-gate 			int naux;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 			offset = 0;
4110Sstevel@tonic-gate 			naux = proc_get_auxv(pid, auxv, 32);
4120Sstevel@tonic-gate 			for (i = 0; i < naux; i++) {
4130Sstevel@tonic-gate 				if (auxv[i].a_type == AT_SUN_EXECNAME) {
4140Sstevel@tonic-gate 					offset = (long)auxv[i].a_un.a_ptr;
4150Sstevel@tonic-gate 					break;
4160Sstevel@tonic-gate 				}
4170Sstevel@tonic-gate 			}
4180Sstevel@tonic-gate 			if (offset == 0 &&
4190Sstevel@tonic-gate 			    proc_get_psinfo(pid, &psinfo) == 0) {
4200Sstevel@tonic-gate 				argv = (long)psinfo.pr_argv;
4210Sstevel@tonic-gate 				if (data_model == PR_MODEL_LP64)
4220Sstevel@tonic-gate 					(void) Pread(Proc, &offset,
423*10927SRoger.Faulkner@Sun.COM 					    sizeof (offset), argv);
4240Sstevel@tonic-gate 				else {
4250Sstevel@tonic-gate 					offset32 = 0;
4260Sstevel@tonic-gate 					(void) Pread(Proc, &offset32,
427*10927SRoger.Faulkner@Sun.COM 					    sizeof (offset32), argv);
4280Sstevel@tonic-gate 					offset = offset32;
4290Sstevel@tonic-gate 				}
4300Sstevel@tonic-gate 			}
4310Sstevel@tonic-gate 		} else {
4320Sstevel@tonic-gate 			offset = pri->sys_args[0];
4330Sstevel@tonic-gate 		}
4340Sstevel@tonic-gate 		if ((s = fetchstring(pri, offset, PATH_MAX)) != NULL) {
4350Sstevel@tonic-gate 			pri->sys_valid = TRUE;
4360Sstevel@tonic-gate 			len = strlen(s);
4370Sstevel@tonic-gate 			/* reallocate if necessary */
4380Sstevel@tonic-gate 			while (len >= pri->sys_psize) {
4390Sstevel@tonic-gate 				free(pri->sys_path);
4400Sstevel@tonic-gate 				pri->sys_path = my_malloc(pri->sys_psize *= 2,
441*10927SRoger.Faulkner@Sun.COM 				    "pathname buffer");
4420Sstevel@tonic-gate 			}
4430Sstevel@tonic-gate 			(void) strcpy(pri->sys_path, s); /* remember pathname */
4440Sstevel@tonic-gate 		}
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	istraced = dotrace && prismember(&trace, what);
4480Sstevel@tonic-gate 	raw = prismember(&rawout, what);
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	/* force tracing of read/write buffer dump syscalls */
4510Sstevel@tonic-gate 	if (!istraced && nargs > 2) {
4520Sstevel@tonic-gate 		int fdp1 = (int)pri->sys_args[0] + 1;
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 		if (ISREAD(what)) {
4550Sstevel@tonic-gate 			if (prismember(&readfd, fdp1))
4560Sstevel@tonic-gate 				istraced = TRUE;
4570Sstevel@tonic-gate 		} else if (ISWRITE(what)) {
4580Sstevel@tonic-gate 			if (prismember(&writefd, fdp1))
4590Sstevel@tonic-gate 				istraced = TRUE;
4600Sstevel@tonic-gate 		}
4610Sstevel@tonic-gate 	}
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	pri->sys_leng = 0;
4640Sstevel@tonic-gate 	if (cflag || !istraced)		/* just counting */
4650Sstevel@tonic-gate 		*pri->sys_string = 0;
4660Sstevel@tonic-gate 	else {
4670Sstevel@tonic-gate 		int argprinted = FALSE;
4680Sstevel@tonic-gate 		const char *name;
4690Sstevel@tonic-gate 
4703235Sraf 		name = sysname(pri, what, raw? -1 : subcode);
4710Sstevel@tonic-gate 		grow(pri, strlen(name) + 1);
4720Sstevel@tonic-gate 		pri->sys_leng = snprintf(pri->sys_string, pri->sys_ssize,
473*10927SRoger.Faulkner@Sun.COM 		    "%s(", name);
4740Sstevel@tonic-gate 		for (i = 0; i < nargs; i++) {
4750Sstevel@tonic-gate 			arg = pri->sys_args[i];
4760Sstevel@tonic-gate 			x = stp->arg[i];
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 			if (x == STG && !raw &&
4790Sstevel@tonic-gate 			    i == 0 && pri->sys_valid) {	/* already fetched */
4800Sstevel@tonic-gate 				escape_string(pri, pri->sys_path);
4810Sstevel@tonic-gate 				argprinted = TRUE;
4820Sstevel@tonic-gate 			} else if (x != HID || raw) {
4830Sstevel@tonic-gate 				if (argprinted)
4840Sstevel@tonic-gate 					outstring(pri, ", ");
4850Sstevel@tonic-gate 				if (x == LLO)
4860Sstevel@tonic-gate 					(*Print[x])(pri, raw, arg,
487*10927SRoger.Faulkner@Sun.COM 					    pri->sys_args[++i]);
4880Sstevel@tonic-gate 				else
4890Sstevel@tonic-gate 					(*Print[x])(pri, raw, arg);
4900Sstevel@tonic-gate 				/*
4910Sstevel@tonic-gate 				 * if nothing printed, then don't print ", "
4920Sstevel@tonic-gate 				 */
4930Sstevel@tonic-gate 				if (x == NOV)
4940Sstevel@tonic-gate 					argprinted = FALSE;
4950Sstevel@tonic-gate 				else
4960Sstevel@tonic-gate 					argprinted = TRUE;
4970Sstevel@tonic-gate 			}
4980Sstevel@tonic-gate 		}
4990Sstevel@tonic-gate 		outstring(pri, ")");
5000Sstevel@tonic-gate 	}
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	return (istraced);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate #undef	ISREAD
5050Sstevel@tonic-gate #undef	ISWRITE
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate /*
5080Sstevel@tonic-gate  * sysexit() returns non-zero if anything was printed.
5090Sstevel@tonic-gate  */
5100Sstevel@tonic-gate int
5110Sstevel@tonic-gate sysexit(private_t *pri, int dotrace)
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
5140Sstevel@tonic-gate 	int what = Lsp->pr_what;
5150Sstevel@tonic-gate 	struct syscount *scp;
5160Sstevel@tonic-gate 	const struct systable *stp;
5170Sstevel@tonic-gate 	int subcode;
5180Sstevel@tonic-gate 	int istraced;
5190Sstevel@tonic-gate 	int raw;
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	/* protect ourself from operating system error */
5220Sstevel@tonic-gate 	if (what <= 0 || what > PRMAXSYS)
5230Sstevel@tonic-gate 		return (0);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	/*
5260Sstevel@tonic-gate 	 * If we aren't supposed to be tracing this one, then
5270Sstevel@tonic-gate 	 * delete it from the traced signal set.  We got here
5280Sstevel@tonic-gate 	 * because the process was sleeping in an untraced syscall.
5290Sstevel@tonic-gate 	 */
5300Sstevel@tonic-gate 	if (!prismember(&traceeven, what)) {
5310Sstevel@tonic-gate 		(void) Psysexit(Proc, what, FALSE);
5320Sstevel@tonic-gate 		return (0);
5330Sstevel@tonic-gate 	}
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	/* pick up registers & set pri->Errno before anything else */
5360Sstevel@tonic-gate 	pri->Errno = Lsp->pr_errno;
5370Sstevel@tonic-gate 	pri->ErrPriv = Lsp->pr_errpriv;
5380Sstevel@tonic-gate 	pri->Rval1 = Lsp->pr_rval1;
5390Sstevel@tonic-gate 	pri->Rval2 = Lsp->pr_rval2;
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	switch (what) {
5420Sstevel@tonic-gate 	case SYS_exit:		/* these are traced on entry */
5430Sstevel@tonic-gate 	case SYS_lwp_exit:
5440Sstevel@tonic-gate 	case SYS_evtrapret:
5450Sstevel@tonic-gate 	case SYS_context:
5460Sstevel@tonic-gate 		istraced = dotrace && prismember(&trace, what);
5470Sstevel@tonic-gate 		break;
5480Sstevel@tonic-gate 	case SYS_exec:		/* these are normally traced on entry */
5490Sstevel@tonic-gate 	case SYS_execve:
5500Sstevel@tonic-gate 		istraced = dotrace && prismember(&trace, what);
5510Sstevel@tonic-gate 		if (pri->exec_string && *pri->exec_string) {
5520Sstevel@tonic-gate 			if (!cflag && istraced) { /* print exec() string now */
5530Sstevel@tonic-gate 				if (pri->exec_pname[0] != '\0')
5540Sstevel@tonic-gate 					(void) fputs(pri->exec_pname, stdout);
5550Sstevel@tonic-gate 				timestamp(pri);
5560Sstevel@tonic-gate 				(void) fputs(pri->exec_string, stdout);
5570Sstevel@tonic-gate 			}
5580Sstevel@tonic-gate 			pri->exec_pname[0] = '\0';
5590Sstevel@tonic-gate 			pri->exec_string[0] = '\0';
5600Sstevel@tonic-gate 			break;
5610Sstevel@tonic-gate 		}
5620Sstevel@tonic-gate 		/* FALLTHROUGH */
5630Sstevel@tonic-gate 	default:
5640Sstevel@tonic-gate 		/* we called sysentry() in main() for these */
5650Sstevel@tonic-gate 		if (what == SYS_open || what == SYS_open64)
5660Sstevel@tonic-gate 			istraced = dotrace && prismember(&trace, what);
5670Sstevel@tonic-gate 		else
5680Sstevel@tonic-gate 			istraced = sysentry(pri, dotrace) && dotrace;
5690Sstevel@tonic-gate 		pri->length = 0;
5700Sstevel@tonic-gate 		if (!cflag && istraced) {
5710Sstevel@tonic-gate 			putpname(pri);
5720Sstevel@tonic-gate 			timestamp(pri);
5730Sstevel@tonic-gate 			pri->length += printf("%s", pri->sys_string);
5740Sstevel@tonic-gate 		}
5750Sstevel@tonic-gate 		pri->sys_leng = 0;
5760Sstevel@tonic-gate 		*pri->sys_string = '\0';
5770Sstevel@tonic-gate 		break;
5780Sstevel@tonic-gate 	}
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	/* get systable entry for this syscall */
5810Sstevel@tonic-gate 	subcode = getsubcode(pri);
5820Sstevel@tonic-gate 	stp = subsys(what, subcode);
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	if (cflag && istraced) {
5850Sstevel@tonic-gate 		(void) mutex_lock(&count_lock);
5860Sstevel@tonic-gate 		scp = Cp->syscount[what];
5873235Sraf 		if (what == SYS_forksys && subcode >= 3)
5883235Sraf 			scp += subcode - 3;
5893235Sraf 		else if (subcode != -1 &&
5900Sstevel@tonic-gate 		    (what != SYS_open && what != SYS_open64 &&
5910Sstevel@tonic-gate 		    what != SYS_lwp_create))
5920Sstevel@tonic-gate 			scp += subcode;
5930Sstevel@tonic-gate 		scp->count++;
5940Sstevel@tonic-gate 		accumulate(&scp->stime, &Lsp->pr_stime, &pri->syslast);
5950Sstevel@tonic-gate 		accumulate(&Cp->usrtotal, &Lsp->pr_utime, &pri->usrlast);
5960Sstevel@tonic-gate 		pri->syslast = Lsp->pr_stime;
5970Sstevel@tonic-gate 		pri->usrlast = Lsp->pr_utime;
5980Sstevel@tonic-gate 		(void) mutex_unlock(&count_lock);
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 
6013235Sraf 	raw = prismember(&rawout, what);
6023235Sraf 
6030Sstevel@tonic-gate 	if (!cflag && istraced) {
6040Sstevel@tonic-gate 		if ((what == SYS_forkall ||
6050Sstevel@tonic-gate 		    what == SYS_vfork ||
6063235Sraf 		    what == SYS_fork1 ||
6073235Sraf 		    what == SYS_forksys) &&
6080Sstevel@tonic-gate 		    pri->Errno == 0 && pri->Rval2 != 0) {
6090Sstevel@tonic-gate 			pri->length &= ~07;
6103235Sraf 			if (strlen(sysname(pri, what, raw? -1 : subcode)) < 6) {
6110Sstevel@tonic-gate 				(void) fputc('\t', stdout);
6123235Sraf 				pri->length += 8;
6133235Sraf 			}
6140Sstevel@tonic-gate 			pri->length +=
615*10927SRoger.Faulkner@Sun.COM 			    7 + printf("\t(returning as child ...)");
6160Sstevel@tonic-gate 		}
6170Sstevel@tonic-gate 		if (what == SYS_lwp_create &&
6180Sstevel@tonic-gate 		    pri->Errno == 0 && pri->Rval1 == 0) {
6190Sstevel@tonic-gate 			pri->length &= ~07;
6200Sstevel@tonic-gate 			pri->length +=
621*10927SRoger.Faulkner@Sun.COM 			    7 + printf("\t(returning as new lwp ...)");
6220Sstevel@tonic-gate 		}
6230Sstevel@tonic-gate 		if (pri->Errno != 0 ||
6240Sstevel@tonic-gate 		    (what != SYS_exec && what != SYS_execve)) {
6250Sstevel@tonic-gate 			/* prepare to print the return code */
6260Sstevel@tonic-gate 			pri->length >>= 3;
6270Sstevel@tonic-gate 			if (pri->length >= 6)
6280Sstevel@tonic-gate 				(void) fputc(' ', stdout);
6290Sstevel@tonic-gate 			for (; pri->length < 6; pri->length++)
6300Sstevel@tonic-gate 				(void) fputc('\t', stdout);
6310Sstevel@tonic-gate 		}
6320Sstevel@tonic-gate 	}
6330Sstevel@tonic-gate 	pri->length = 0;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	if (pri->Errno != 0) {		/* error in syscall */
6360Sstevel@tonic-gate 		if (istraced) {
6370Sstevel@tonic-gate 			if (cflag)
6380Sstevel@tonic-gate 				scp->error++;
6390Sstevel@tonic-gate 			else {
6400Sstevel@tonic-gate 				const char *ename = errname(pri->Errno);
6410Sstevel@tonic-gate 				const char *privname;
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 				(void) printf("Err#%d", pri->Errno);
6440Sstevel@tonic-gate 				if (ename != NULL) {
6450Sstevel@tonic-gate 					(void) fputc(' ', stdout);
6460Sstevel@tonic-gate 					(void) fputs(ename, stdout);
6470Sstevel@tonic-gate 				}
6480Sstevel@tonic-gate 				switch (pri->ErrPriv) {
6490Sstevel@tonic-gate 				case PRIV_NONE:
6500Sstevel@tonic-gate 					privname = NULL;
6510Sstevel@tonic-gate 					break;
6520Sstevel@tonic-gate 				case PRIV_ALL:
6530Sstevel@tonic-gate 					privname = "ALL";
6540Sstevel@tonic-gate 					break;
6550Sstevel@tonic-gate 				case PRIV_MULTIPLE:
6560Sstevel@tonic-gate 					privname = "MULTIPLE";
6570Sstevel@tonic-gate 					break;
6580Sstevel@tonic-gate 				case PRIV_ALLZONE:
6590Sstevel@tonic-gate 					privname = "ZONE";
6600Sstevel@tonic-gate 					break;
6610Sstevel@tonic-gate 				default:
6620Sstevel@tonic-gate 					privname = priv_getbynum(pri->ErrPriv);
6630Sstevel@tonic-gate 					break;
6640Sstevel@tonic-gate 				}
6650Sstevel@tonic-gate 				if (privname != NULL)
6660Sstevel@tonic-gate 					(void) printf(" [%s]", privname);
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 				(void) fputc('\n', stdout);
6690Sstevel@tonic-gate 			}
6700Sstevel@tonic-gate 		}
6710Sstevel@tonic-gate 	} else {
6720Sstevel@tonic-gate 		/* show arguments on successful exec */
6730Sstevel@tonic-gate 		if (what == SYS_exec || what == SYS_execve) {
6740Sstevel@tonic-gate 			if (!cflag && istraced)
6750Sstevel@tonic-gate 				showargs(pri, raw);
6760Sstevel@tonic-gate 		} else if (!cflag && istraced) {
6770Sstevel@tonic-gate 			const char *fmt = NULL;
6780Sstevel@tonic-gate 			long rv1 = pri->Rval1;
6790Sstevel@tonic-gate 			long rv2 = pri->Rval2;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate #ifdef _LP64
6820Sstevel@tonic-gate 			/*
6830Sstevel@tonic-gate 			 * 32-bit system calls return 32-bit values. We
6840Sstevel@tonic-gate 			 * later mask out the upper bits if we want to
6850Sstevel@tonic-gate 			 * print these as unsigned values.
6860Sstevel@tonic-gate 			 */
6870Sstevel@tonic-gate 			if (data_model == PR_MODEL_ILP32) {
6880Sstevel@tonic-gate 				rv1 = (int)rv1;
6890Sstevel@tonic-gate 				rv2 = (int)rv2;
6900Sstevel@tonic-gate 			}
6910Sstevel@tonic-gate #endif
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 			switch (what) {
6940Sstevel@tonic-gate 			case SYS_llseek:
6950Sstevel@tonic-gate 				rv1 &= 0xffffffff;
6960Sstevel@tonic-gate 				rv2 &= 0xffffffff;
6970Sstevel@tonic-gate #ifdef _LONG_LONG_LTOH	/* first long of a longlong is the low order */
6980Sstevel@tonic-gate 				if (rv2 != 0) {
6990Sstevel@tonic-gate 					long temp = rv1;
7000Sstevel@tonic-gate 					fmt = "= 0x%lX%.8lX";
7010Sstevel@tonic-gate 					rv1 = rv2;
7020Sstevel@tonic-gate 					rv2 = temp;
7030Sstevel@tonic-gate 					break;
7040Sstevel@tonic-gate 				}
7050Sstevel@tonic-gate #else	/* the other way around */
7060Sstevel@tonic-gate 				if (rv1 != 0) {
7070Sstevel@tonic-gate 					fmt = "= 0x%lX%.8lX";
7080Sstevel@tonic-gate 					break;
7090Sstevel@tonic-gate 				}
7100Sstevel@tonic-gate 				rv1 = rv2;	/* ugly */
7110Sstevel@tonic-gate #endif
7120Sstevel@tonic-gate 				/* FALLTHROUGH */
7130Sstevel@tonic-gate 			case SYS_lseek:
7140Sstevel@tonic-gate 			case SYS_ulimit:
7150Sstevel@tonic-gate 				if (rv1 & 0xff000000) {
7160Sstevel@tonic-gate #ifdef _LP64
7170Sstevel@tonic-gate 					if (data_model == PR_MODEL_ILP32)
7180Sstevel@tonic-gate 						rv1 &= 0xffffffff;
7190Sstevel@tonic-gate #endif
7200Sstevel@tonic-gate 					fmt = "= 0x%.8lX";
7210Sstevel@tonic-gate 				}
7220Sstevel@tonic-gate 				break;
7230Sstevel@tonic-gate 			case SYS_sigtimedwait:
7240Sstevel@tonic-gate 				if (raw)
7250Sstevel@tonic-gate 					/* EMPTY */;
7260Sstevel@tonic-gate 				else if ((fmt = rawsigname(pri, rv1)) != NULL) {
7270Sstevel@tonic-gate 					rv1 = (long)fmt;	/* filthy */
7280Sstevel@tonic-gate 					fmt = "= %s";
7290Sstevel@tonic-gate 				}
7300Sstevel@tonic-gate 				break;
7310Sstevel@tonic-gate 			case SYS_port:
7320Sstevel@tonic-gate #ifdef _LP64
7330Sstevel@tonic-gate 				if (data_model == PR_MODEL_LP64) {
7340Sstevel@tonic-gate 					rv2 = rv1 & 0xffffffff;
7350Sstevel@tonic-gate 					rv1 = rv1 >> 32;
7360Sstevel@tonic-gate 				}
7370Sstevel@tonic-gate #endif
7380Sstevel@tonic-gate 				break;
7390Sstevel@tonic-gate 			}
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 			if (fmt == NULL) {
7420Sstevel@tonic-gate 				switch (stp->rval[0]) {
7430Sstevel@tonic-gate 				case HEX:
7440Sstevel@tonic-gate #ifdef _LP64
7450Sstevel@tonic-gate 					if (data_model == PR_MODEL_ILP32)
7460Sstevel@tonic-gate 						rv1 &= 0xffffffff;
7470Sstevel@tonic-gate #endif
7480Sstevel@tonic-gate 					fmt = "= 0x%.8lX";
7490Sstevel@tonic-gate 					break;
7500Sstevel@tonic-gate 				case HHX:
7510Sstevel@tonic-gate #ifdef _LP64
7520Sstevel@tonic-gate 					if (data_model == PR_MODEL_ILP32)
7530Sstevel@tonic-gate 						rv1 &= 0xffffffff;
7540Sstevel@tonic-gate #endif
7550Sstevel@tonic-gate 					fmt = "= 0x%.4lX";
7560Sstevel@tonic-gate 					break;
7570Sstevel@tonic-gate 				case OCT:
7580Sstevel@tonic-gate #ifdef _LP64
7590Sstevel@tonic-gate 					if (data_model == PR_MODEL_ILP32)
7600Sstevel@tonic-gate 						rv1 &= 0xffffffff;
7610Sstevel@tonic-gate #endif
7620Sstevel@tonic-gate 					fmt = "= %#lo";
7630Sstevel@tonic-gate 					break;
7644321Scasper 				case UNS:
7654321Scasper #ifdef _LP64
7664321Scasper 					if (data_model == PR_MODEL_ILP32)
7674321Scasper 						rv1 &= 0xffffffff;
7684321Scasper #endif
7694321Scasper 					fmt = "= %lu";
7704321Scasper 					break;
7710Sstevel@tonic-gate 				default:
7720Sstevel@tonic-gate 					fmt = "= %ld";
7730Sstevel@tonic-gate 					break;
7740Sstevel@tonic-gate 				}
7750Sstevel@tonic-gate 			}
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 			(void) printf(fmt, rv1, rv2);
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 			switch (stp->rval[1]) {
7800Sstevel@tonic-gate 			case NOV:
7810Sstevel@tonic-gate 				fmt = NULL;
7820Sstevel@tonic-gate 				break;
7830Sstevel@tonic-gate 			case HEX:
7840Sstevel@tonic-gate #ifdef _LP64
7850Sstevel@tonic-gate 				if (data_model == PR_MODEL_ILP32)
7860Sstevel@tonic-gate 					rv2 &= 0xffffffff;
7870Sstevel@tonic-gate #endif
7880Sstevel@tonic-gate 				fmt = " [0x%.8lX]";
7890Sstevel@tonic-gate 				break;
7900Sstevel@tonic-gate 			case HHX:
7910Sstevel@tonic-gate #ifdef _LP64
7920Sstevel@tonic-gate 				if (data_model == PR_MODEL_ILP32)
7930Sstevel@tonic-gate 					rv2 &= 0xffffffff;
7940Sstevel@tonic-gate #endif
7950Sstevel@tonic-gate 				fmt = " [0x%.4lX]";
7960Sstevel@tonic-gate 				break;
7970Sstevel@tonic-gate 			case OCT:
7980Sstevel@tonic-gate #ifdef _LP64
7990Sstevel@tonic-gate 				if (data_model == PR_MODEL_ILP32)
8000Sstevel@tonic-gate 					rv2 &= 0xffffffff;
8010Sstevel@tonic-gate #endif
8020Sstevel@tonic-gate 				fmt = " [%#lo]";
8030Sstevel@tonic-gate 				break;
8044321Scasper 			case UNS:
8054321Scasper #ifdef _LP64
8064321Scasper 				if (data_model == PR_MODEL_ILP32)
8074321Scasper 					rv2 &= 0xffffffff;
8084321Scasper #endif
8094321Scasper 				fmt = " [%lu]";
8104321Scasper 				break;
8110Sstevel@tonic-gate 			default:
8120Sstevel@tonic-gate 				fmt = " [%ld]";
8130Sstevel@tonic-gate 				break;
8140Sstevel@tonic-gate 			}
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 			if (fmt != NULL)
8170Sstevel@tonic-gate 				(void) printf(fmt, rv2);
8180Sstevel@tonic-gate 			(void) fputc('\n', stdout);
8190Sstevel@tonic-gate 		}
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 		if (what == SYS_forkall ||
8220Sstevel@tonic-gate 		    what == SYS_vfork ||
8233235Sraf 		    what == SYS_fork1 ||
8243235Sraf 		    what == SYS_forksys) {
8250Sstevel@tonic-gate 			if (pri->Rval2 == 0)		/* child was created */
8260Sstevel@tonic-gate 				pri->child = pri->Rval1;
8270Sstevel@tonic-gate 			else if (cflag && istraced)	/* this is the child */
8280Sstevel@tonic-gate 				scp->count--;
8290Sstevel@tonic-gate 		}
8300Sstevel@tonic-gate 		if (what == SYS_lwp_create && pri->Rval1 == 0 &&
8310Sstevel@tonic-gate 		    cflag && istraced)		/* this is the created lwp */
8320Sstevel@tonic-gate 			scp->count--;
8330Sstevel@tonic-gate 	}
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate #define	ISREAD(code) \
8360Sstevel@tonic-gate 	((code) == SYS_read || (code) == SYS_pread || (code) == SYS_pread64 || \
8370Sstevel@tonic-gate 	(code) == SYS_recv || (code) == SYS_recvfrom)
8380Sstevel@tonic-gate #define	ISWRITE(code) \
8390Sstevel@tonic-gate 	((code) == SYS_write || (code) == SYS_pwrite || \
8400Sstevel@tonic-gate 	(code) == SYS_pwrite64 || (code) == SYS_send || (code) == SYS_sendto)
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	if (!cflag && istraced) {
8430Sstevel@tonic-gate 		int fdp1 = (int)pri->sys_args[0] + 1; /* filedescriptor + 1 */
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 		if (raw) {
8460Sstevel@tonic-gate 			if (what != SYS_exec && what != SYS_execve)
8470Sstevel@tonic-gate 				showpaths(pri, stp);
8480Sstevel@tonic-gate 			if (ISREAD(what) || ISWRITE(what)) {
8490Sstevel@tonic-gate 				if (pri->iob_buf[0] != '\0')
8500Sstevel@tonic-gate 					(void) printf("%s     0x%.8lX: %s\n",
851*10927SRoger.Faulkner@Sun.COM 					    pri->pname, pri->sys_args[1],
852*10927SRoger.Faulkner@Sun.COM 					    pri->iob_buf);
8530Sstevel@tonic-gate 			}
8540Sstevel@tonic-gate 		}
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 		/*
8570Sstevel@tonic-gate 		 * Show buffer contents for read()/pread() or write()/pwrite().
8580Sstevel@tonic-gate 		 * IOBSIZE bytes have already been shown;
8590Sstevel@tonic-gate 		 * don't show them again unless there's more.
8600Sstevel@tonic-gate 		 */
8610Sstevel@tonic-gate 		if ((ISREAD(what) && pri->Errno == 0 &&
8620Sstevel@tonic-gate 		    prismember(&readfd, fdp1)) ||
8630Sstevel@tonic-gate 		    (ISWRITE(what) && prismember(&writefd, fdp1))) {
8640Sstevel@tonic-gate 			long nb = ISWRITE(what) ? pri->sys_args[2] : pri->Rval1;
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 			if (nb > IOBSIZE) {
8670Sstevel@tonic-gate 				/* enter region of lengthy output */
8680Sstevel@tonic-gate 				if (nb > MYBUFSIZ / 4)
8690Sstevel@tonic-gate 					Eserialize();
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 				showbuffer(pri, pri->sys_args[1], nb);
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 				/* exit region of lengthy output */
8740Sstevel@tonic-gate 				if (nb > MYBUFSIZ / 4)
8750Sstevel@tonic-gate 					Xserialize();
8760Sstevel@tonic-gate 			}
8770Sstevel@tonic-gate 		}
8780Sstevel@tonic-gate #undef	ISREAD
8790Sstevel@tonic-gate #undef	ISWRITE
8800Sstevel@tonic-gate 		/*
8810Sstevel@tonic-gate 		 * Do verbose interpretation if requested.
8820Sstevel@tonic-gate 		 * If buffer contents for read or write have been requested and
8830Sstevel@tonic-gate 		 * this is a readv() or writev(), force verbose interpretation.
8840Sstevel@tonic-gate 		 */
8850Sstevel@tonic-gate 		if (prismember(&verbose, what) ||
886328Sja97890 		    ((what == SYS_readv || what == SYS_recvmsg) &&
887328Sja97890 		    pri->Errno == 0 && prismember(&readfd, fdp1)) ||
888328Sja97890 		    ((what == SYS_writev || what == SYS_sendfilev ||
889328Sja97890 		    what == SYS_sendmsg) &&
8900Sstevel@tonic-gate 		    prismember(&writefd, fdp1)))
8910Sstevel@tonic-gate 			expound(pri, pri->Rval1, raw);
8920Sstevel@tonic-gate 	}
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	return (!cflag && istraced);
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate void
8980Sstevel@tonic-gate showpaths(private_t *pri, const struct systable *stp)
8990Sstevel@tonic-gate {
9000Sstevel@tonic-gate 	int i;
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	for (i = 0; i < pri->sys_nargs; i++) {
9030Sstevel@tonic-gate 		if ((stp->arg[i] == STG) ||
9040Sstevel@tonic-gate 		    (stp->arg[i] == RST && !pri->Errno) ||
9050Sstevel@tonic-gate 		    (stp->arg[i] == RLK && !pri->Errno && pri->Rval1 > 0)) {
9060Sstevel@tonic-gate 			long addr = pri->sys_args[i];
9070Sstevel@tonic-gate 			int maxleng =
9080Sstevel@tonic-gate 			    (stp->arg[i] == RLK)? (int)pri->Rval1 : PATH_MAX;
9090Sstevel@tonic-gate 			char *s;
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 			if (i == 0 && pri->sys_valid)	/* already fetched */
9120Sstevel@tonic-gate 				s = pri->sys_path;
9130Sstevel@tonic-gate 			else
9140Sstevel@tonic-gate 				s = fetchstring(pri, addr,
9150Sstevel@tonic-gate 				    maxleng > PATH_MAX ? PATH_MAX : maxleng);
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 			if (s != (char *)NULL)
9180Sstevel@tonic-gate 				(void) printf("%s     0x%.8lX: \"%s\"\n",
919*10927SRoger.Faulkner@Sun.COM 				    pri->pname, addr, s);
9200Sstevel@tonic-gate 		}
9210Sstevel@tonic-gate 	}
9220Sstevel@tonic-gate }
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate /*
9250Sstevel@tonic-gate  * Display arguments to successful exec().
9260Sstevel@tonic-gate  */
9270Sstevel@tonic-gate void
9280Sstevel@tonic-gate showargs(private_t *pri, int raw)
9290Sstevel@tonic-gate {
9300Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
9310Sstevel@tonic-gate 	int nargs;
9320Sstevel@tonic-gate 	long ap;
9330Sstevel@tonic-gate 	int ptrsize;
9340Sstevel@tonic-gate 	int fail;
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	pri->length = 0;
9370Sstevel@tonic-gate 	ptrsize = (data_model == PR_MODEL_LP64)? 8 : 4;
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)	/* XX64 */
9400Sstevel@tonic-gate 	ap = (long)Lsp->pr_reg[R_SP];
9410Sstevel@tonic-gate 	fail = (Pread(Proc, &nargs, sizeof (nargs), ap) != sizeof (nargs));
9420Sstevel@tonic-gate 	ap += ptrsize;
9430Sstevel@tonic-gate #endif /* i386 */
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate #if sparc
9460Sstevel@tonic-gate 	if (data_model == PR_MODEL_LP64) {
9470Sstevel@tonic-gate 		int64_t xnargs;
9480Sstevel@tonic-gate 		ap = (long)(Lsp->pr_reg[R_SP]) + 16 * sizeof (int64_t)
949*10927SRoger.Faulkner@Sun.COM 		    + STACK_BIAS;
9500Sstevel@tonic-gate 		fail = (Pread(Proc, &xnargs, sizeof (xnargs), ap) !=
951*10927SRoger.Faulkner@Sun.COM 		    sizeof (xnargs));
9520Sstevel@tonic-gate 		nargs = (int)xnargs;
9530Sstevel@tonic-gate 	} else {
9540Sstevel@tonic-gate 		ap = (long)(Lsp->pr_reg[R_SP]) + 16 * sizeof (int32_t);
9550Sstevel@tonic-gate 		fail = (Pread(Proc, &nargs, sizeof (nargs), ap) !=
956*10927SRoger.Faulkner@Sun.COM 		    sizeof (nargs));
9570Sstevel@tonic-gate 	}
9580Sstevel@tonic-gate 	ap += ptrsize;
9590Sstevel@tonic-gate #endif /* sparc */
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	if (fail) {
9620Sstevel@tonic-gate 		(void) printf("\n%s\t*** Bad argument list? ***\n", pri->pname);
9630Sstevel@tonic-gate 		return;
9640Sstevel@tonic-gate 	}
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	(void) printf("  argc = %d\n", nargs);
9670Sstevel@tonic-gate 	if (raw)
9680Sstevel@tonic-gate 		showpaths(pri, &systable[SYS_exec]);
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	show_cred(pri, FALSE);
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	if (aflag || eflag) {		/* dump args or environment */
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 		/* enter region of (potentially) lengthy output */
9750Sstevel@tonic-gate 		Eserialize();
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 		if (aflag)		/* dump the argument list */
9780Sstevel@tonic-gate 			dumpargs(pri, ap, "argv:");
9790Sstevel@tonic-gate 		ap += (nargs+1) * ptrsize;
9800Sstevel@tonic-gate 		if (eflag)		/* dump the environment */
9810Sstevel@tonic-gate 			dumpargs(pri, ap, "envp:");
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 		/* exit region of lengthy output */
9840Sstevel@tonic-gate 		Xserialize();
9850Sstevel@tonic-gate 	}
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate void
9890Sstevel@tonic-gate dumpargs(private_t *pri, long ap, const char *str)
9900Sstevel@tonic-gate {
9910Sstevel@tonic-gate 	char *string;
9920Sstevel@tonic-gate 	unsigned int leng = 0;
9930Sstevel@tonic-gate 	int ptrsize;
9940Sstevel@tonic-gate 	long arg = 0;
9950Sstevel@tonic-gate 	char *argaddr;
9960Sstevel@tonic-gate 	char badaddr[32];
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	if (interrupt)
9990Sstevel@tonic-gate 		return;
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate #ifdef _LP64
10020Sstevel@tonic-gate 	if (data_model == PR_MODEL_LP64) {
10030Sstevel@tonic-gate 		argaddr = (char *)&arg;
10040Sstevel@tonic-gate 		ptrsize = 8;
10050Sstevel@tonic-gate 	} else {
10060Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
10070Sstevel@tonic-gate 		argaddr = (char *)&arg;
10080Sstevel@tonic-gate #else
10090Sstevel@tonic-gate 		argaddr = (char *)&arg + 4;
10100Sstevel@tonic-gate #endif
10110Sstevel@tonic-gate 		ptrsize = 4;
10120Sstevel@tonic-gate 	}
10130Sstevel@tonic-gate #else
10140Sstevel@tonic-gate 	argaddr = (char *)&arg;
10150Sstevel@tonic-gate 	ptrsize = 4;
10160Sstevel@tonic-gate #endif
10170Sstevel@tonic-gate 	putpname(pri);
10180Sstevel@tonic-gate 	(void) fputc(' ', stdout);
10190Sstevel@tonic-gate 	(void) fputs(str, stdout);
10200Sstevel@tonic-gate 	leng += 1 + strlen(str);
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	while (!interrupt) {
10230Sstevel@tonic-gate 		if (Pread(Proc, argaddr, ptrsize, ap) != ptrsize) {
10240Sstevel@tonic-gate 			(void) printf("\n%s\t*** Bad argument list? ***\n",
1025*10927SRoger.Faulkner@Sun.COM 			    pri->pname);
10260Sstevel@tonic-gate 			return;
10270Sstevel@tonic-gate 		}
10280Sstevel@tonic-gate 		ap += ptrsize;
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 		if (arg == 0)
10310Sstevel@tonic-gate 			break;
10320Sstevel@tonic-gate 		string = fetchstring(pri, arg, PATH_MAX);
10330Sstevel@tonic-gate 		if (string == NULL) {
10340Sstevel@tonic-gate 			(void) sprintf(badaddr, "BadAddress:0x%.8lX", arg);
10350Sstevel@tonic-gate 			string = badaddr;
10360Sstevel@tonic-gate 		}
10370Sstevel@tonic-gate 		if ((leng += strlen(string)) < 63) {
10380Sstevel@tonic-gate 			(void) fputc(' ', stdout);
10390Sstevel@tonic-gate 			leng++;
10400Sstevel@tonic-gate 		} else {
10410Sstevel@tonic-gate 			(void) fputc('\n', stdout);
10420Sstevel@tonic-gate 			leng = 0;
10430Sstevel@tonic-gate 			putpname(pri);
10440Sstevel@tonic-gate 			(void) fputs("  ", stdout);
10450Sstevel@tonic-gate 			leng += 2 + strlen(string);
10460Sstevel@tonic-gate 		}
10470Sstevel@tonic-gate 		(void) fputs(string, stdout);
10480Sstevel@tonic-gate 	}
10490Sstevel@tonic-gate 	(void) fputc('\n', stdout);
10500Sstevel@tonic-gate }
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate /*
10530Sstevel@tonic-gate  * Display contents of read() or write() buffer.
10540Sstevel@tonic-gate  */
10550Sstevel@tonic-gate void
10560Sstevel@tonic-gate showbuffer(private_t *pri, long offset, long count)
10570Sstevel@tonic-gate {
10580Sstevel@tonic-gate 	char buffer[320];
10590Sstevel@tonic-gate 	int nbytes;
10600Sstevel@tonic-gate 	char *buf;
10610Sstevel@tonic-gate 	int n;
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	while (count > 0 && !interrupt) {
10640Sstevel@tonic-gate 		nbytes = (count < sizeof (buffer))? count : sizeof (buffer);
10650Sstevel@tonic-gate 		if ((nbytes = Pread(Proc, buffer, nbytes, offset)) <= 0)
10660Sstevel@tonic-gate 			break;
10670Sstevel@tonic-gate 		count -= nbytes;
10680Sstevel@tonic-gate 		offset += nbytes;
10690Sstevel@tonic-gate 		buf = buffer;
10700Sstevel@tonic-gate 		while (nbytes > 0 && !interrupt) {
10710Sstevel@tonic-gate 			char obuf[65];
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 			n = (nbytes < 32)? nbytes : 32;
10740Sstevel@tonic-gate 			showbytes(buf, n, obuf);
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 			putpname(pri);
10770Sstevel@tonic-gate 			(void) fputs("  ", stdout);
10780Sstevel@tonic-gate 			(void) fputs(obuf, stdout);
10790Sstevel@tonic-gate 			(void) fputc('\n', stdout);
10800Sstevel@tonic-gate 			nbytes -= n;
10810Sstevel@tonic-gate 			buf += n;
10820Sstevel@tonic-gate 		}
10830Sstevel@tonic-gate 	}
10840Sstevel@tonic-gate }
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate void
10870Sstevel@tonic-gate showbytes(const char *buf, int n, char *obuf)
10880Sstevel@tonic-gate {
10890Sstevel@tonic-gate 	int c;
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	while (--n >= 0) {
10920Sstevel@tonic-gate 		int c1 = '\\';
10930Sstevel@tonic-gate 		int c2;
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 		switch (c = (*buf++ & 0xff)) {
10960Sstevel@tonic-gate 		case '\0':
10970Sstevel@tonic-gate 			c2 = '0';
10980Sstevel@tonic-gate 			break;
10990Sstevel@tonic-gate 		case '\b':
11000Sstevel@tonic-gate 			c2 = 'b';
11010Sstevel@tonic-gate 			break;
11020Sstevel@tonic-gate 		case '\t':
11030Sstevel@tonic-gate 			c2 = 't';
11040Sstevel@tonic-gate 			break;
11050Sstevel@tonic-gate 		case '\n':
11060Sstevel@tonic-gate 			c2 = 'n';
11070Sstevel@tonic-gate 			break;
11080Sstevel@tonic-gate 		case '\v':
11090Sstevel@tonic-gate 			c2 = 'v';
11100Sstevel@tonic-gate 			break;
11110Sstevel@tonic-gate 		case '\f':
11120Sstevel@tonic-gate 			c2 = 'f';
11130Sstevel@tonic-gate 			break;
11140Sstevel@tonic-gate 		case '\r':
11150Sstevel@tonic-gate 			c2 = 'r';
11160Sstevel@tonic-gate 			break;
11170Sstevel@tonic-gate 		default:
11180Sstevel@tonic-gate 			if (isprint(c)) {
11190Sstevel@tonic-gate 				c1 = ' ';
11200Sstevel@tonic-gate 				c2 = c;
11210Sstevel@tonic-gate 			} else {
11220Sstevel@tonic-gate 				c1 = c>>4;
11230Sstevel@tonic-gate 				c1 += (c1 < 10)? '0' : 'A'-10;
11240Sstevel@tonic-gate 				c2 = c&0xf;
11250Sstevel@tonic-gate 				c2 += (c2 < 10)? '0' : 'A'-10;
11260Sstevel@tonic-gate 			}
11270Sstevel@tonic-gate 			break;
11280Sstevel@tonic-gate 		}
11290Sstevel@tonic-gate 		*obuf++ = (char)c1;
11300Sstevel@tonic-gate 		*obuf++ = (char)c2;
11310Sstevel@tonic-gate 	}
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	*obuf = '\0';
11340Sstevel@tonic-gate }
1135