xref: /illumos-gate/usr/src/cmd/truss/actions.c (revision f48205be)
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
5657b1f3dSraf  * Common Development and Distribution License (the "License").
6657b1f3dSraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21657b1f3dSraf 
227c478bd9Sstevel@tonic-gate /*
23*f48205beScasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <ctype.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <memory.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <limits.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/stack.h>
427c478bd9Sstevel@tonic-gate #include <signal.h>
437c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
447c478bd9Sstevel@tonic-gate #include <libproc.h>
457c478bd9Sstevel@tonic-gate #include <priv.h>
467c478bd9Sstevel@tonic-gate #include "ramdata.h"
477c478bd9Sstevel@tonic-gate #include "systable.h"
487c478bd9Sstevel@tonic-gate #include "print.h"
497c478bd9Sstevel@tonic-gate #include "proto.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * Actions to take when process stops.
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * Function prototypes for static routines in this module.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate int	stopsig(private_t *);
597c478bd9Sstevel@tonic-gate void	showpaths(private_t *, const struct systable *);
607c478bd9Sstevel@tonic-gate void	showargs(private_t *, int);
617c478bd9Sstevel@tonic-gate void	dumpargs(private_t *, long, const char *);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * Report an lwp to be sleeping (if true).
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate void
677c478bd9Sstevel@tonic-gate report_sleeping(private_t *pri, int dotrace)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
707c478bd9Sstevel@tonic-gate 	int sys = Lsp->pr_syscall;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	if (!prismember(&trace, sys) || !dotrace ||
737c478bd9Sstevel@tonic-gate 	    !(Lsp->pr_flags & (PR_ASLEEP|PR_VFORKP))) {
747c478bd9Sstevel@tonic-gate 		/* Make sure we catch sysexit even if we're not tracing it. */
757c478bd9Sstevel@tonic-gate 		(void) Psysexit(Proc, sys, TRUE);
767c478bd9Sstevel@tonic-gate 		return;
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	pri->length = 0;
807c478bd9Sstevel@tonic-gate 	pri->Errno = 0;
817c478bd9Sstevel@tonic-gate 	pri->ErrPriv = PRIV_NONE;
827c478bd9Sstevel@tonic-gate 	pri->Rval1 = pri->Rval2 = 0;
837c478bd9Sstevel@tonic-gate 	(void) sysentry(pri, dotrace);
847c478bd9Sstevel@tonic-gate 	make_pname(pri, 0);
857c478bd9Sstevel@tonic-gate 	putpname(pri);
867c478bd9Sstevel@tonic-gate 	timestamp(pri);
877c478bd9Sstevel@tonic-gate 	pri->length += printf("%s", pri->sys_string);
887c478bd9Sstevel@tonic-gate 	pri->sys_leng = 0;
897c478bd9Sstevel@tonic-gate 	*pri->sys_string = '\0';
907c478bd9Sstevel@tonic-gate 	pri->length >>= 3;
917c478bd9Sstevel@tonic-gate 	if (Lsp->pr_flags & PR_VFORKP)
927c478bd9Sstevel@tonic-gate 		pri->length += 2;
937c478bd9Sstevel@tonic-gate 	if (pri->length >= 4)
947c478bd9Sstevel@tonic-gate 		(void) fputc(' ', stdout);
957c478bd9Sstevel@tonic-gate 	for (; pri->length < 4; pri->length++)
967c478bd9Sstevel@tonic-gate 		(void) fputc('\t', stdout);
977c478bd9Sstevel@tonic-gate 	if (Lsp->pr_flags & PR_VFORKP)
987c478bd9Sstevel@tonic-gate 		(void) fputs("(waiting for child to exit()/exec()...)\n",
997c478bd9Sstevel@tonic-gate 			stdout);
1007c478bd9Sstevel@tonic-gate 	else
1017c478bd9Sstevel@tonic-gate 		(void) fputs("(sleeping...)\n", stdout);
1027c478bd9Sstevel@tonic-gate 	pri->length = 0;
1037c478bd9Sstevel@tonic-gate 	if (prismember(&verbose, sys)) {
1047c478bd9Sstevel@tonic-gate 		int raw = prismember(&rawout, sys);
1057c478bd9Sstevel@tonic-gate 		pri->Errno = 1;
1067c478bd9Sstevel@tonic-gate 		expound(pri, 0, raw);
1077c478bd9Sstevel@tonic-gate 		pri->Errno = 0;
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 	Flush();
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * requested() gets called for these reasons:
1147c478bd9Sstevel@tonic-gate  *	flag == JOBSIG:		report nothing; change state to JOBSTOP
1157c478bd9Sstevel@tonic-gate  *	flag == JOBSTOP:	report "Continued ..."
1167c478bd9Sstevel@tonic-gate  *	default:		report sleeping system call
1177c478bd9Sstevel@tonic-gate  *
1187c478bd9Sstevel@tonic-gate  * It returns a new flag:  JOBSTOP or SLEEPING or 0.
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate int
1217c478bd9Sstevel@tonic-gate requested(private_t *pri, int flag, int dotrace)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
1247c478bd9Sstevel@tonic-gate 	int sig = Lsp->pr_cursig;
1257c478bd9Sstevel@tonic-gate 	int newflag = 0;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	switch (flag) {
1287c478bd9Sstevel@tonic-gate 	case JOBSIG:
1297c478bd9Sstevel@tonic-gate 		return (JOBSTOP);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	case JOBSTOP:
1327c478bd9Sstevel@tonic-gate 		if (dotrace && !cflag && prismember(&signals, sig)) {
1337c478bd9Sstevel@tonic-gate 			pri->length = 0;
1347c478bd9Sstevel@tonic-gate 			putpname(pri);
1357c478bd9Sstevel@tonic-gate 			timestamp(pri);
1367c478bd9Sstevel@tonic-gate 			(void) printf("    Continued with signal #%d, %s",
1377c478bd9Sstevel@tonic-gate 				sig, signame(pri, sig));
1387c478bd9Sstevel@tonic-gate 			if (Lsp->pr_action.sa_handler == SIG_DFL)
1397c478bd9Sstevel@tonic-gate 				(void) printf(" [default]");
1407c478bd9Sstevel@tonic-gate 			else if (Lsp->pr_action.sa_handler == SIG_IGN)
1417c478bd9Sstevel@tonic-gate 				(void) printf(" [ignored]");
1427c478bd9Sstevel@tonic-gate 			else
1437c478bd9Sstevel@tonic-gate 				(void) printf(" [caught]");
1447c478bd9Sstevel@tonic-gate 			(void) fputc('\n', stdout);
1457c478bd9Sstevel@tonic-gate 			Flush();
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 		newflag = 0;
1487c478bd9Sstevel@tonic-gate 		break;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	default:
1517c478bd9Sstevel@tonic-gate 		newflag = SLEEPING;
1527c478bd9Sstevel@tonic-gate 		if (!cflag)
1537c478bd9Sstevel@tonic-gate 			report_sleeping(pri, dotrace);
1547c478bd9Sstevel@tonic-gate 		break;
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	return (newflag);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate int
1617c478bd9Sstevel@tonic-gate jobcontrol(private_t *pri, int dotrace)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
1647c478bd9Sstevel@tonic-gate 	int sig = stopsig(pri);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if (sig == 0)
1677c478bd9Sstevel@tonic-gate 		return (0);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if (dotrace && !cflag &&		/* not just counting */
1707c478bd9Sstevel@tonic-gate 	    prismember(&signals, sig)) {	/* tracing this signal */
1717c478bd9Sstevel@tonic-gate 		int sys;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 		pri->length = 0;
1747c478bd9Sstevel@tonic-gate 		putpname(pri);
1757c478bd9Sstevel@tonic-gate 		timestamp(pri);
1767c478bd9Sstevel@tonic-gate 		(void) printf("    Stopped by signal #%d, %s",
1777c478bd9Sstevel@tonic-gate 			sig, signame(pri, sig));
1787c478bd9Sstevel@tonic-gate 		if ((Lsp->pr_flags & PR_ASLEEP) &&
1797c478bd9Sstevel@tonic-gate 		    (sys = Lsp->pr_syscall) > 0 && sys <= PRMAXSYS)
1807c478bd9Sstevel@tonic-gate 			(void) printf(", in %s()",
1817c478bd9Sstevel@tonic-gate 				sysname(pri, sys, getsubcode(pri)));
1827c478bd9Sstevel@tonic-gate 		(void) fputc('\n', stdout);
1837c478bd9Sstevel@tonic-gate 		Flush();
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	return (JOBSTOP);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * Return the signal the process stopped on iff process is already stopped on
1917c478bd9Sstevel@tonic-gate  * PR_JOBCONTROL or is stopped on PR_SIGNALLED or PR_REQUESTED with a current
1927c478bd9Sstevel@tonic-gate  * signal that will cause a JOBCONTROL stop when the process is set running.
1937c478bd9Sstevel@tonic-gate  */
1947c478bd9Sstevel@tonic-gate int
1957c478bd9Sstevel@tonic-gate stopsig(private_t *pri)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
1987c478bd9Sstevel@tonic-gate 	int sig = 0;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (Lsp->pr_flags & PR_STOPPED) {
2017c478bd9Sstevel@tonic-gate 		switch (Lsp->pr_why) {
2027c478bd9Sstevel@tonic-gate 		case PR_JOBCONTROL:
2037c478bd9Sstevel@tonic-gate 			sig = Lsp->pr_what;
2047c478bd9Sstevel@tonic-gate 			if (sig < 0 || sig > PRMAXSIG)
2057c478bd9Sstevel@tonic-gate 				sig = 0;
2067c478bd9Sstevel@tonic-gate 			break;
2077c478bd9Sstevel@tonic-gate 		case PR_SIGNALLED:
2087c478bd9Sstevel@tonic-gate 		case PR_REQUESTED:
2097c478bd9Sstevel@tonic-gate 			if (Lsp->pr_action.sa_handler == SIG_DFL) {
2107c478bd9Sstevel@tonic-gate 				switch (Lsp->pr_cursig) {
2117c478bd9Sstevel@tonic-gate 				case SIGSTOP:
2127c478bd9Sstevel@tonic-gate 					sig = SIGSTOP;
2137c478bd9Sstevel@tonic-gate 					break;
2147c478bd9Sstevel@tonic-gate 				case SIGTSTP:
2157c478bd9Sstevel@tonic-gate 				case SIGTTIN:
2167c478bd9Sstevel@tonic-gate 				case SIGTTOU:
2177c478bd9Sstevel@tonic-gate 					if (!(Lsp->pr_flags & PR_ORPHAN))
2187c478bd9Sstevel@tonic-gate 						sig = Lsp->pr_cursig;
2197c478bd9Sstevel@tonic-gate 					break;
2207c478bd9Sstevel@tonic-gate 				}
2217c478bd9Sstevel@tonic-gate 			}
2227c478bd9Sstevel@tonic-gate 			break;
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	return (sig);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate int
2307c478bd9Sstevel@tonic-gate signalled(private_t *pri, int flag, int dotrace)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
2337c478bd9Sstevel@tonic-gate 	int sig = Lsp->pr_what;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (sig <= 0 || sig > PRMAXSIG)	/* check bounds */
2367c478bd9Sstevel@tonic-gate 		return (0);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if (dotrace && cflag) {			/* just counting */
2397c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&count_lock);
2407c478bd9Sstevel@tonic-gate 		Cp->sigcount[sig]++;
2417c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&count_lock);
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if (sig == SIGCONT && (flag == JOBSIG || flag == JOBSTOP))
2457c478bd9Sstevel@tonic-gate 		flag = requested(pri, JOBSTOP, dotrace);
2467c478bd9Sstevel@tonic-gate 	else if ((flag = jobcontrol(pri, dotrace)) == 0 &&
2477c478bd9Sstevel@tonic-gate 	    !cflag && dotrace &&
2487c478bd9Sstevel@tonic-gate 	    prismember(&signals, sig)) {
2497c478bd9Sstevel@tonic-gate 		int sys;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 		pri->length = 0;
2527c478bd9Sstevel@tonic-gate 		putpname(pri);
2537c478bd9Sstevel@tonic-gate 		timestamp(pri);
2547c478bd9Sstevel@tonic-gate 		(void) printf("    Received signal #%d, %s",
2557c478bd9Sstevel@tonic-gate 			sig, signame(pri, sig));
2567c478bd9Sstevel@tonic-gate 		if ((Lsp->pr_flags & PR_ASLEEP) &&
2577c478bd9Sstevel@tonic-gate 		    (sys = Lsp->pr_syscall) > 0 && sys <= PRMAXSYS)
2587c478bd9Sstevel@tonic-gate 			(void) printf(", in %s()",
2597c478bd9Sstevel@tonic-gate 				sysname(pri, sys, getsubcode(pri)));
2607c478bd9Sstevel@tonic-gate 		if (Lsp->pr_action.sa_handler == SIG_DFL)
2617c478bd9Sstevel@tonic-gate 			(void) printf(" [default]");
2627c478bd9Sstevel@tonic-gate 		else if (Lsp->pr_action.sa_handler == SIG_IGN)
2637c478bd9Sstevel@tonic-gate 			(void) printf(" [ignored]");
2647c478bd9Sstevel@tonic-gate 		else
2657c478bd9Sstevel@tonic-gate 			(void) printf(" [caught]");
2667c478bd9Sstevel@tonic-gate 		(void) fputc('\n', stdout);
2677c478bd9Sstevel@tonic-gate 		if (Lsp->pr_info.si_code != 0 ||
2687c478bd9Sstevel@tonic-gate 		    Lsp->pr_info.si_pid != 0)
2697c478bd9Sstevel@tonic-gate 			print_siginfo(pri, &Lsp->pr_info);
2707c478bd9Sstevel@tonic-gate 		Flush();
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (flag == JOBSTOP)
2747c478bd9Sstevel@tonic-gate 		flag = JOBSIG;
2757c478bd9Sstevel@tonic-gate 	return (flag);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate int
2797c478bd9Sstevel@tonic-gate faulted(private_t *pri, int dotrace)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
2827c478bd9Sstevel@tonic-gate 	int flt = Lsp->pr_what;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	if ((uint_t)flt > PRMAXFAULT || !prismember(&faults, flt) || !dotrace)
2857c478bd9Sstevel@tonic-gate 		return (0);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&count_lock);
2887c478bd9Sstevel@tonic-gate 	Cp->fltcount[flt]++;
2897c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&count_lock);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if (cflag)		/* just counting */
2927c478bd9Sstevel@tonic-gate 		return (1);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	pri->length = 0;
2957c478bd9Sstevel@tonic-gate 	putpname(pri);
2967c478bd9Sstevel@tonic-gate 	timestamp(pri);
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	(void) printf("    Incurred fault #%d, %s  %%pc = 0x%.8lX",
2997c478bd9Sstevel@tonic-gate 		flt, proc_fltname(flt, pri->flt_name, sizeof (pri->flt_name)),
3007c478bd9Sstevel@tonic-gate 		(long)Lsp->pr_reg[R_PC]);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	if (flt == FLTPAGE)
3037c478bd9Sstevel@tonic-gate 		(void) printf("  addr = 0x%.8lX",
3047c478bd9Sstevel@tonic-gate 			(long)Lsp->pr_info.si_addr);
3057c478bd9Sstevel@tonic-gate 	(void) fputc('\n', stdout);
3067c478bd9Sstevel@tonic-gate 	if (Lsp->pr_info.si_signo != 0)
3077c478bd9Sstevel@tonic-gate 		print_siginfo(pri, &Lsp->pr_info);
3087c478bd9Sstevel@tonic-gate 	Flush();
3097c478bd9Sstevel@tonic-gate 	return (1);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate  * Set up pri->sys_nargs and pri->sys_args[] (syscall args).
3147c478bd9Sstevel@tonic-gate  */
3157c478bd9Sstevel@tonic-gate void
3167c478bd9Sstevel@tonic-gate setupsysargs(private_t *pri, int what)
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
3197c478bd9Sstevel@tonic-gate 	int nargs;
3207c478bd9Sstevel@tonic-gate 	int i;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate #if sparc
3237c478bd9Sstevel@tonic-gate 	/* determine whether syscall is indirect */
3247c478bd9Sstevel@tonic-gate 	pri->sys_indirect = (Lsp->pr_reg[R_G1] == SYS_syscall)? 1 : 0;
3257c478bd9Sstevel@tonic-gate #else
3267c478bd9Sstevel@tonic-gate 	pri->sys_indirect = 0;
3277c478bd9Sstevel@tonic-gate #endif
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	(void) memset(pri->sys_args, 0, sizeof (pri->sys_args));
3307c478bd9Sstevel@tonic-gate 	if (what != Lsp->pr_syscall) {	/* assertion */
3317c478bd9Sstevel@tonic-gate 		(void) printf("%s\t*** Inconsistent syscall: %d vs %d ***\n",
3327c478bd9Sstevel@tonic-gate 			pri->pname, what, Lsp->pr_syscall);
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 	nargs = Lsp->pr_nsysarg;
3357c478bd9Sstevel@tonic-gate 	for (i = 0;
3367c478bd9Sstevel@tonic-gate 	    i < nargs && i < sizeof (pri->sys_args) / sizeof (pri->sys_args[0]);
3377c478bd9Sstevel@tonic-gate 	    i++)
3387c478bd9Sstevel@tonic-gate 		pri->sys_args[i] = Lsp->pr_sysarg[i];
3397c478bd9Sstevel@tonic-gate 	pri->sys_nargs = nargs;
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate #define	ISREAD(code) \
3437c478bd9Sstevel@tonic-gate 	((code) == SYS_read || (code) == SYS_pread || \
3447c478bd9Sstevel@tonic-gate 	(code) == SYS_pread64 || (code) == SYS_readv || \
3457c478bd9Sstevel@tonic-gate 	(code) == SYS_recv || (code) == SYS_recvfrom)
3467c478bd9Sstevel@tonic-gate #define	ISWRITE(code) \
3477c478bd9Sstevel@tonic-gate 	((code) == SYS_write || (code) == SYS_pwrite || \
3487c478bd9Sstevel@tonic-gate 	(code) == SYS_pwrite64 || (code) == SYS_writev || \
3497c478bd9Sstevel@tonic-gate 	(code) == SYS_send || (code) == SYS_sendto)
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate /*
3527c478bd9Sstevel@tonic-gate  * Return TRUE iff syscall is being traced.
3537c478bd9Sstevel@tonic-gate  */
3547c478bd9Sstevel@tonic-gate int
3557c478bd9Sstevel@tonic-gate sysentry(private_t *pri, int dotrace)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 	pid_t pid = Pstatus(Proc)->pr_pid;
3587c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
3597c478bd9Sstevel@tonic-gate 	long arg;
3607c478bd9Sstevel@tonic-gate 	int nargs;
3617c478bd9Sstevel@tonic-gate 	int i;
3627c478bd9Sstevel@tonic-gate 	int x;
3637c478bd9Sstevel@tonic-gate 	int len;
3647c478bd9Sstevel@tonic-gate 	char *s;
3657c478bd9Sstevel@tonic-gate 	const struct systable *stp;
3667c478bd9Sstevel@tonic-gate 	int what = Lsp->pr_what;
3677c478bd9Sstevel@tonic-gate 	int subcode;
3687c478bd9Sstevel@tonic-gate 	int istraced;
3697c478bd9Sstevel@tonic-gate 	int raw;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	/* for reporting sleeping system calls */
3727c478bd9Sstevel@tonic-gate 	if (what == 0 && (Lsp->pr_flags & (PR_ASLEEP|PR_VFORKP)))
3737c478bd9Sstevel@tonic-gate 		what = Lsp->pr_syscall;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	/* protect ourself from operating system error */
3767c478bd9Sstevel@tonic-gate 	if (what <= 0 || what > PRMAXSYS)
3777c478bd9Sstevel@tonic-gate 		what = 0;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/*
3807c478bd9Sstevel@tonic-gate 	 * Set up the system call arguments (pri->sys_nargs & pri->sys_args[]).
3817c478bd9Sstevel@tonic-gate 	 */
3827c478bd9Sstevel@tonic-gate 	setupsysargs(pri, what);
3837c478bd9Sstevel@tonic-gate 	nargs = pri->sys_nargs;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	/* get systable entry for this syscall */
3867c478bd9Sstevel@tonic-gate 	subcode = getsubcode(pri);
3877c478bd9Sstevel@tonic-gate 	stp = subsys(what, subcode);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	if (nargs > stp->nargs)
3907c478bd9Sstevel@tonic-gate 		nargs = stp->nargs;
3917c478bd9Sstevel@tonic-gate 	pri->sys_nargs = nargs;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	/* fetch and remember first argument if it's a string */
3947c478bd9Sstevel@tonic-gate 	pri->sys_valid = FALSE;
3957c478bd9Sstevel@tonic-gate 	if (nargs > 0 && stp->arg[0] == STG) {
3967c478bd9Sstevel@tonic-gate 		long offset;
3977c478bd9Sstevel@tonic-gate 		uint32_t offset32;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 		/*
4007c478bd9Sstevel@tonic-gate 		 * Special case for exit from exec().
4017c478bd9Sstevel@tonic-gate 		 * The address in pri->sys_args[0] refers to the old process
4027c478bd9Sstevel@tonic-gate 		 * image.  We must fetch the string from the new image.
4037c478bd9Sstevel@tonic-gate 		 */
4047c478bd9Sstevel@tonic-gate 		if (Lsp->pr_why == PR_SYSEXIT &&
4057c478bd9Sstevel@tonic-gate 		    (Lsp->pr_what == SYS_execve ||
4067c478bd9Sstevel@tonic-gate 		    Lsp->pr_what == SYS_exec)) {
4077c478bd9Sstevel@tonic-gate 			psinfo_t psinfo;
4087c478bd9Sstevel@tonic-gate 			long argv;
4097c478bd9Sstevel@tonic-gate 			auxv_t auxv[32];
4107c478bd9Sstevel@tonic-gate 			int naux;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 			offset = 0;
4137c478bd9Sstevel@tonic-gate 			naux = proc_get_auxv(pid, auxv, 32);
4147c478bd9Sstevel@tonic-gate 			for (i = 0; i < naux; i++) {
4157c478bd9Sstevel@tonic-gate 				if (auxv[i].a_type == AT_SUN_EXECNAME) {
4167c478bd9Sstevel@tonic-gate 					offset = (long)auxv[i].a_un.a_ptr;
4177c478bd9Sstevel@tonic-gate 					break;
4187c478bd9Sstevel@tonic-gate 				}
4197c478bd9Sstevel@tonic-gate 			}
4207c478bd9Sstevel@tonic-gate 			if (offset == 0 &&
4217c478bd9Sstevel@tonic-gate 			    proc_get_psinfo(pid, &psinfo) == 0) {
4227c478bd9Sstevel@tonic-gate 				argv = (long)psinfo.pr_argv;
4237c478bd9Sstevel@tonic-gate 				if (data_model == PR_MODEL_LP64)
4247c478bd9Sstevel@tonic-gate 					(void) Pread(Proc, &offset,
4257c478bd9Sstevel@tonic-gate 						sizeof (offset), argv);
4267c478bd9Sstevel@tonic-gate 				else {
4277c478bd9Sstevel@tonic-gate 					offset32 = 0;
4287c478bd9Sstevel@tonic-gate 					(void) Pread(Proc, &offset32,
4297c478bd9Sstevel@tonic-gate 						sizeof (offset32), argv);
4307c478bd9Sstevel@tonic-gate 					offset = offset32;
4317c478bd9Sstevel@tonic-gate 				}
4327c478bd9Sstevel@tonic-gate 			}
4337c478bd9Sstevel@tonic-gate 		} else {
4347c478bd9Sstevel@tonic-gate 			offset = pri->sys_args[0];
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 		if ((s = fetchstring(pri, offset, PATH_MAX)) != NULL) {
4377c478bd9Sstevel@tonic-gate 			pri->sys_valid = TRUE;
4387c478bd9Sstevel@tonic-gate 			len = strlen(s);
4397c478bd9Sstevel@tonic-gate 			/* reallocate if necessary */
4407c478bd9Sstevel@tonic-gate 			while (len >= pri->sys_psize) {
4417c478bd9Sstevel@tonic-gate 				free(pri->sys_path);
4427c478bd9Sstevel@tonic-gate 				pri->sys_path = my_malloc(pri->sys_psize *= 2,
4437c478bd9Sstevel@tonic-gate 					"pathname buffer");
4447c478bd9Sstevel@tonic-gate 			}
4457c478bd9Sstevel@tonic-gate 			(void) strcpy(pri->sys_path, s); /* remember pathname */
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	istraced = dotrace && prismember(&trace, what);
4507c478bd9Sstevel@tonic-gate 	raw = prismember(&rawout, what);
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	/* force tracing of read/write buffer dump syscalls */
4537c478bd9Sstevel@tonic-gate 	if (!istraced && nargs > 2) {
4547c478bd9Sstevel@tonic-gate 		int fdp1 = (int)pri->sys_args[0] + 1;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 		if (ISREAD(what)) {
4577c478bd9Sstevel@tonic-gate 			if (prismember(&readfd, fdp1))
4587c478bd9Sstevel@tonic-gate 				istraced = TRUE;
4597c478bd9Sstevel@tonic-gate 		} else if (ISWRITE(what)) {
4607c478bd9Sstevel@tonic-gate 			if (prismember(&writefd, fdp1))
4617c478bd9Sstevel@tonic-gate 				istraced = TRUE;
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	pri->sys_leng = 0;
4667c478bd9Sstevel@tonic-gate 	if (cflag || !istraced)		/* just counting */
4677c478bd9Sstevel@tonic-gate 		*pri->sys_string = 0;
4687c478bd9Sstevel@tonic-gate 	else {
4697c478bd9Sstevel@tonic-gate 		int argprinted = FALSE;
4707c478bd9Sstevel@tonic-gate 		const char *name;
4717c478bd9Sstevel@tonic-gate 
472657b1f3dSraf 		name = sysname(pri, what, raw? -1 : subcode);
4737c478bd9Sstevel@tonic-gate 		grow(pri, strlen(name) + 1);
4747c478bd9Sstevel@tonic-gate 		pri->sys_leng = snprintf(pri->sys_string, pri->sys_ssize,
4757c478bd9Sstevel@tonic-gate 			"%s(", name);
4767c478bd9Sstevel@tonic-gate 		for (i = 0; i < nargs; i++) {
4777c478bd9Sstevel@tonic-gate 			arg = pri->sys_args[i];
4787c478bd9Sstevel@tonic-gate 			x = stp->arg[i];
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 			if (x == STG && !raw &&
4817c478bd9Sstevel@tonic-gate 			    i == 0 && pri->sys_valid) {	/* already fetched */
4827c478bd9Sstevel@tonic-gate 				escape_string(pri, pri->sys_path);
4837c478bd9Sstevel@tonic-gate 				argprinted = TRUE;
4847c478bd9Sstevel@tonic-gate 			} else if (x != HID || raw) {
4857c478bd9Sstevel@tonic-gate 				if (argprinted)
4867c478bd9Sstevel@tonic-gate 					outstring(pri, ", ");
4877c478bd9Sstevel@tonic-gate 				if (x == LLO)
4887c478bd9Sstevel@tonic-gate 					(*Print[x])(pri, raw, arg,
4897c478bd9Sstevel@tonic-gate 						pri->sys_args[++i]);
4907c478bd9Sstevel@tonic-gate 				else
4917c478bd9Sstevel@tonic-gate 					(*Print[x])(pri, raw, arg);
4927c478bd9Sstevel@tonic-gate 				/*
4937c478bd9Sstevel@tonic-gate 				 * if nothing printed, then don't print ", "
4947c478bd9Sstevel@tonic-gate 				 */
4957c478bd9Sstevel@tonic-gate 				if (x == NOV)
4967c478bd9Sstevel@tonic-gate 					argprinted = FALSE;
4977c478bd9Sstevel@tonic-gate 				else
4987c478bd9Sstevel@tonic-gate 					argprinted = TRUE;
4997c478bd9Sstevel@tonic-gate 			}
5007c478bd9Sstevel@tonic-gate 		}
5017c478bd9Sstevel@tonic-gate 		outstring(pri, ")");
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	return (istraced);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate #undef	ISREAD
5077c478bd9Sstevel@tonic-gate #undef	ISWRITE
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate /*
5107c478bd9Sstevel@tonic-gate  * sysexit() returns non-zero if anything was printed.
5117c478bd9Sstevel@tonic-gate  */
5127c478bd9Sstevel@tonic-gate int
5137c478bd9Sstevel@tonic-gate sysexit(private_t *pri, int dotrace)
5147c478bd9Sstevel@tonic-gate {
5157c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
5167c478bd9Sstevel@tonic-gate 	int what = Lsp->pr_what;
5177c478bd9Sstevel@tonic-gate 	struct syscount *scp;
5187c478bd9Sstevel@tonic-gate 	const struct systable *stp;
5197c478bd9Sstevel@tonic-gate 	int subcode;
5207c478bd9Sstevel@tonic-gate 	int istraced;
5217c478bd9Sstevel@tonic-gate 	int raw;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	/* protect ourself from operating system error */
5247c478bd9Sstevel@tonic-gate 	if (what <= 0 || what > PRMAXSYS)
5257c478bd9Sstevel@tonic-gate 		return (0);
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	/*
5287c478bd9Sstevel@tonic-gate 	 * If we aren't supposed to be tracing this one, then
5297c478bd9Sstevel@tonic-gate 	 * delete it from the traced signal set.  We got here
5307c478bd9Sstevel@tonic-gate 	 * because the process was sleeping in an untraced syscall.
5317c478bd9Sstevel@tonic-gate 	 */
5327c478bd9Sstevel@tonic-gate 	if (!prismember(&traceeven, what)) {
5337c478bd9Sstevel@tonic-gate 		(void) Psysexit(Proc, what, FALSE);
5347c478bd9Sstevel@tonic-gate 		return (0);
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	/* pick up registers & set pri->Errno before anything else */
5387c478bd9Sstevel@tonic-gate 	pri->Errno = Lsp->pr_errno;
5397c478bd9Sstevel@tonic-gate 	pri->ErrPriv = Lsp->pr_errpriv;
5407c478bd9Sstevel@tonic-gate 	pri->Rval1 = Lsp->pr_rval1;
5417c478bd9Sstevel@tonic-gate 	pri->Rval2 = Lsp->pr_rval2;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	switch (what) {
5447c478bd9Sstevel@tonic-gate 	case SYS_exit:		/* these are traced on entry */
5457c478bd9Sstevel@tonic-gate 	case SYS_lwp_exit:
5467c478bd9Sstevel@tonic-gate 	case SYS_evtrapret:
5477c478bd9Sstevel@tonic-gate 	case SYS_context:
5487c478bd9Sstevel@tonic-gate 		istraced = dotrace && prismember(&trace, what);
5497c478bd9Sstevel@tonic-gate 		break;
5507c478bd9Sstevel@tonic-gate 	case SYS_exec:		/* these are normally traced on entry */
5517c478bd9Sstevel@tonic-gate 	case SYS_execve:
5527c478bd9Sstevel@tonic-gate 		istraced = dotrace && prismember(&trace, what);
5537c478bd9Sstevel@tonic-gate 		if (pri->exec_string && *pri->exec_string) {
5547c478bd9Sstevel@tonic-gate 			if (!cflag && istraced) { /* print exec() string now */
5557c478bd9Sstevel@tonic-gate 				if (pri->exec_pname[0] != '\0')
5567c478bd9Sstevel@tonic-gate 					(void) fputs(pri->exec_pname, stdout);
5577c478bd9Sstevel@tonic-gate 				timestamp(pri);
5587c478bd9Sstevel@tonic-gate 				(void) fputs(pri->exec_string, stdout);
5597c478bd9Sstevel@tonic-gate 			}
5607c478bd9Sstevel@tonic-gate 			pri->exec_pname[0] = '\0';
5617c478bd9Sstevel@tonic-gate 			pri->exec_string[0] = '\0';
5627c478bd9Sstevel@tonic-gate 			break;
5637c478bd9Sstevel@tonic-gate 		}
5647c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
5657c478bd9Sstevel@tonic-gate 	default:
5667c478bd9Sstevel@tonic-gate 		/* we called sysentry() in main() for these */
5677c478bd9Sstevel@tonic-gate 		if (what == SYS_open || what == SYS_open64)
5687c478bd9Sstevel@tonic-gate 			istraced = dotrace && prismember(&trace, what);
5697c478bd9Sstevel@tonic-gate 		else
5707c478bd9Sstevel@tonic-gate 			istraced = sysentry(pri, dotrace) && dotrace;
5717c478bd9Sstevel@tonic-gate 		pri->length = 0;
5727c478bd9Sstevel@tonic-gate 		if (!cflag && istraced) {
5737c478bd9Sstevel@tonic-gate 			putpname(pri);
5747c478bd9Sstevel@tonic-gate 			timestamp(pri);
5757c478bd9Sstevel@tonic-gate 			pri->length += printf("%s", pri->sys_string);
5767c478bd9Sstevel@tonic-gate 		}
5777c478bd9Sstevel@tonic-gate 		pri->sys_leng = 0;
5787c478bd9Sstevel@tonic-gate 		*pri->sys_string = '\0';
5797c478bd9Sstevel@tonic-gate 		break;
5807c478bd9Sstevel@tonic-gate 	}
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	/* get systable entry for this syscall */
5837c478bd9Sstevel@tonic-gate 	subcode = getsubcode(pri);
5847c478bd9Sstevel@tonic-gate 	stp = subsys(what, subcode);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	if (cflag && istraced) {
5877c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&count_lock);
5887c478bd9Sstevel@tonic-gate 		scp = Cp->syscount[what];
589657b1f3dSraf 		if (what == SYS_forksys && subcode >= 3)
590657b1f3dSraf 			scp += subcode - 3;
591657b1f3dSraf 		else if (subcode != -1 &&
5927c478bd9Sstevel@tonic-gate 		    (what != SYS_open && what != SYS_open64 &&
5937c478bd9Sstevel@tonic-gate 		    what != SYS_lwp_create))
5947c478bd9Sstevel@tonic-gate 			scp += subcode;
5957c478bd9Sstevel@tonic-gate 		scp->count++;
5967c478bd9Sstevel@tonic-gate 		accumulate(&scp->stime, &Lsp->pr_stime, &pri->syslast);
5977c478bd9Sstevel@tonic-gate 		accumulate(&Cp->usrtotal, &Lsp->pr_utime, &pri->usrlast);
5987c478bd9Sstevel@tonic-gate 		pri->syslast = Lsp->pr_stime;
5997c478bd9Sstevel@tonic-gate 		pri->usrlast = Lsp->pr_utime;
6007c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&count_lock);
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 
603657b1f3dSraf 	raw = prismember(&rawout, what);
604657b1f3dSraf 
6057c478bd9Sstevel@tonic-gate 	if (!cflag && istraced) {
6067c478bd9Sstevel@tonic-gate 		if ((what == SYS_forkall ||
6077c478bd9Sstevel@tonic-gate 		    what == SYS_vfork ||
608657b1f3dSraf 		    what == SYS_fork1 ||
609657b1f3dSraf 		    what == SYS_forksys) &&
6107c478bd9Sstevel@tonic-gate 		    pri->Errno == 0 && pri->Rval2 != 0) {
6117c478bd9Sstevel@tonic-gate 			pri->length &= ~07;
612657b1f3dSraf 			if (strlen(sysname(pri, what, raw? -1 : subcode)) < 6) {
6137c478bd9Sstevel@tonic-gate 				(void) fputc('\t', stdout);
614657b1f3dSraf 				pri->length += 8;
615657b1f3dSraf 			}
6167c478bd9Sstevel@tonic-gate 			pri->length +=
617657b1f3dSraf 				7 + printf("\t(returning as child ...)");
6187c478bd9Sstevel@tonic-gate 		}
6197c478bd9Sstevel@tonic-gate 		if (what == SYS_lwp_create &&
6207c478bd9Sstevel@tonic-gate 		    pri->Errno == 0 && pri->Rval1 == 0) {
6217c478bd9Sstevel@tonic-gate 			pri->length &= ~07;
6227c478bd9Sstevel@tonic-gate 			pri->length +=
6237c478bd9Sstevel@tonic-gate 				7 + printf("\t(returning as new lwp ...)");
6247c478bd9Sstevel@tonic-gate 		}
6257c478bd9Sstevel@tonic-gate 		if (pri->Errno != 0 ||
6267c478bd9Sstevel@tonic-gate 		    (what != SYS_exec && what != SYS_execve)) {
6277c478bd9Sstevel@tonic-gate 			/* prepare to print the return code */
6287c478bd9Sstevel@tonic-gate 			pri->length >>= 3;
6297c478bd9Sstevel@tonic-gate 			if (pri->length >= 6)
6307c478bd9Sstevel@tonic-gate 				(void) fputc(' ', stdout);
6317c478bd9Sstevel@tonic-gate 			for (; pri->length < 6; pri->length++)
6327c478bd9Sstevel@tonic-gate 				(void) fputc('\t', stdout);
6337c478bd9Sstevel@tonic-gate 		}
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 	pri->length = 0;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	if (pri->Errno != 0) {		/* error in syscall */
6387c478bd9Sstevel@tonic-gate 		if (istraced) {
6397c478bd9Sstevel@tonic-gate 			if (cflag)
6407c478bd9Sstevel@tonic-gate 				scp->error++;
6417c478bd9Sstevel@tonic-gate 			else {
6427c478bd9Sstevel@tonic-gate 				const char *ename = errname(pri->Errno);
6437c478bd9Sstevel@tonic-gate 				const char *privname;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 				(void) printf("Err#%d", pri->Errno);
6467c478bd9Sstevel@tonic-gate 				if (ename != NULL) {
6477c478bd9Sstevel@tonic-gate 					(void) fputc(' ', stdout);
6487c478bd9Sstevel@tonic-gate 					(void) fputs(ename, stdout);
6497c478bd9Sstevel@tonic-gate 				}
6507c478bd9Sstevel@tonic-gate 				switch (pri->ErrPriv) {
6517c478bd9Sstevel@tonic-gate 				case PRIV_NONE:
6527c478bd9Sstevel@tonic-gate 					privname = NULL;
6537c478bd9Sstevel@tonic-gate 					break;
6547c478bd9Sstevel@tonic-gate 				case PRIV_ALL:
6557c478bd9Sstevel@tonic-gate 					privname = "ALL";
6567c478bd9Sstevel@tonic-gate 					break;
6577c478bd9Sstevel@tonic-gate 				case PRIV_MULTIPLE:
6587c478bd9Sstevel@tonic-gate 					privname = "MULTIPLE";
6597c478bd9Sstevel@tonic-gate 					break;
6607c478bd9Sstevel@tonic-gate 				case PRIV_ALLZONE:
6617c478bd9Sstevel@tonic-gate 					privname = "ZONE";
6627c478bd9Sstevel@tonic-gate 					break;
6637c478bd9Sstevel@tonic-gate 				default:
6647c478bd9Sstevel@tonic-gate 					privname = priv_getbynum(pri->ErrPriv);
6657c478bd9Sstevel@tonic-gate 					break;
6667c478bd9Sstevel@tonic-gate 				}
6677c478bd9Sstevel@tonic-gate 				if (privname != NULL)
6687c478bd9Sstevel@tonic-gate 					(void) printf(" [%s]", privname);
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 				(void) fputc('\n', stdout);
6717c478bd9Sstevel@tonic-gate 			}
6727c478bd9Sstevel@tonic-gate 		}
6737c478bd9Sstevel@tonic-gate 	} else {
6747c478bd9Sstevel@tonic-gate 		/* show arguments on successful exec */
6757c478bd9Sstevel@tonic-gate 		if (what == SYS_exec || what == SYS_execve) {
6767c478bd9Sstevel@tonic-gate 			if (!cflag && istraced)
6777c478bd9Sstevel@tonic-gate 				showargs(pri, raw);
6787c478bd9Sstevel@tonic-gate 		} else if (!cflag && istraced) {
6797c478bd9Sstevel@tonic-gate 			const char *fmt = NULL;
6807c478bd9Sstevel@tonic-gate 			long rv1 = pri->Rval1;
6817c478bd9Sstevel@tonic-gate 			long rv2 = pri->Rval2;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate #ifdef _LP64
6847c478bd9Sstevel@tonic-gate 			/*
6857c478bd9Sstevel@tonic-gate 			 * 32-bit system calls return 32-bit values. We
6867c478bd9Sstevel@tonic-gate 			 * later mask out the upper bits if we want to
6877c478bd9Sstevel@tonic-gate 			 * print these as unsigned values.
6887c478bd9Sstevel@tonic-gate 			 */
6897c478bd9Sstevel@tonic-gate 			if (data_model == PR_MODEL_ILP32) {
6907c478bd9Sstevel@tonic-gate 				rv1 = (int)rv1;
6917c478bd9Sstevel@tonic-gate 				rv2 = (int)rv2;
6927c478bd9Sstevel@tonic-gate 			}
6937c478bd9Sstevel@tonic-gate #endif
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 			switch (what) {
6967c478bd9Sstevel@tonic-gate 			case SYS_llseek:
6977c478bd9Sstevel@tonic-gate 				rv1 &= 0xffffffff;
6987c478bd9Sstevel@tonic-gate 				rv2 &= 0xffffffff;
6997c478bd9Sstevel@tonic-gate #ifdef _LONG_LONG_LTOH	/* first long of a longlong is the low order */
7007c478bd9Sstevel@tonic-gate 				if (rv2 != 0) {
7017c478bd9Sstevel@tonic-gate 					long temp = rv1;
7027c478bd9Sstevel@tonic-gate 					fmt = "= 0x%lX%.8lX";
7037c478bd9Sstevel@tonic-gate 					rv1 = rv2;
7047c478bd9Sstevel@tonic-gate 					rv2 = temp;
7057c478bd9Sstevel@tonic-gate 					break;
7067c478bd9Sstevel@tonic-gate 				}
7077c478bd9Sstevel@tonic-gate #else	/* the other way around */
7087c478bd9Sstevel@tonic-gate 				if (rv1 != 0) {
7097c478bd9Sstevel@tonic-gate 					fmt = "= 0x%lX%.8lX";
7107c478bd9Sstevel@tonic-gate 					break;
7117c478bd9Sstevel@tonic-gate 				}
7127c478bd9Sstevel@tonic-gate 				rv1 = rv2;	/* ugly */
7137c478bd9Sstevel@tonic-gate #endif
7147c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
7157c478bd9Sstevel@tonic-gate 			case SYS_lseek:
7167c478bd9Sstevel@tonic-gate 			case SYS_ulimit:
7177c478bd9Sstevel@tonic-gate 				if (rv1 & 0xff000000) {
7187c478bd9Sstevel@tonic-gate #ifdef _LP64
7197c478bd9Sstevel@tonic-gate 					if (data_model == PR_MODEL_ILP32)
7207c478bd9Sstevel@tonic-gate 						rv1 &= 0xffffffff;
7217c478bd9Sstevel@tonic-gate #endif
7227c478bd9Sstevel@tonic-gate 					fmt = "= 0x%.8lX";
7237c478bd9Sstevel@tonic-gate 				}
7247c478bd9Sstevel@tonic-gate 				break;
7257c478bd9Sstevel@tonic-gate 			case SYS_signal:
7267c478bd9Sstevel@tonic-gate 				if (raw)
7277c478bd9Sstevel@tonic-gate 					/* EMPTY */;
7287c478bd9Sstevel@tonic-gate 				else if (rv1 == (int)SIG_DFL)
7297c478bd9Sstevel@tonic-gate 					fmt = "= SIG_DFL";
7307c478bd9Sstevel@tonic-gate 				else if (rv1 == (int)SIG_IGN)
7317c478bd9Sstevel@tonic-gate 					fmt = "= SIG_IGN";
7327c478bd9Sstevel@tonic-gate 				else if (rv1 == (int)SIG_HOLD)
7337c478bd9Sstevel@tonic-gate 					fmt = "= SIG_HOLD";
7347c478bd9Sstevel@tonic-gate 				break;
7357c478bd9Sstevel@tonic-gate 			case SYS_sigtimedwait:
7367c478bd9Sstevel@tonic-gate 				if (raw)
7377c478bd9Sstevel@tonic-gate 					/* EMPTY */;
7387c478bd9Sstevel@tonic-gate 				else if ((fmt = rawsigname(pri, rv1)) != NULL) {
7397c478bd9Sstevel@tonic-gate 					rv1 = (long)fmt;	/* filthy */
7407c478bd9Sstevel@tonic-gate 					fmt = "= %s";
7417c478bd9Sstevel@tonic-gate 				}
7427c478bd9Sstevel@tonic-gate 				break;
7437c478bd9Sstevel@tonic-gate 			case SYS_port:
7447c478bd9Sstevel@tonic-gate #ifdef _LP64
7457c478bd9Sstevel@tonic-gate 				if (data_model == PR_MODEL_LP64) {
7467c478bd9Sstevel@tonic-gate 					rv2 = rv1 & 0xffffffff;
7477c478bd9Sstevel@tonic-gate 					rv1 = rv1 >> 32;
7487c478bd9Sstevel@tonic-gate 				}
7497c478bd9Sstevel@tonic-gate #endif
7507c478bd9Sstevel@tonic-gate 				break;
7517c478bd9Sstevel@tonic-gate 			}
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 			if (fmt == NULL) {
7547c478bd9Sstevel@tonic-gate 				switch (stp->rval[0]) {
7557c478bd9Sstevel@tonic-gate 				case HEX:
7567c478bd9Sstevel@tonic-gate #ifdef _LP64
7577c478bd9Sstevel@tonic-gate 					if (data_model == PR_MODEL_ILP32)
7587c478bd9Sstevel@tonic-gate 						rv1 &= 0xffffffff;
7597c478bd9Sstevel@tonic-gate #endif
7607c478bd9Sstevel@tonic-gate 					fmt = "= 0x%.8lX";
7617c478bd9Sstevel@tonic-gate 					break;
7627c478bd9Sstevel@tonic-gate 				case HHX:
7637c478bd9Sstevel@tonic-gate #ifdef _LP64
7647c478bd9Sstevel@tonic-gate 					if (data_model == PR_MODEL_ILP32)
7657c478bd9Sstevel@tonic-gate 						rv1 &= 0xffffffff;
7667c478bd9Sstevel@tonic-gate #endif
7677c478bd9Sstevel@tonic-gate 					fmt = "= 0x%.4lX";
7687c478bd9Sstevel@tonic-gate 					break;
7697c478bd9Sstevel@tonic-gate 				case OCT:
7707c478bd9Sstevel@tonic-gate #ifdef _LP64
7717c478bd9Sstevel@tonic-gate 					if (data_model == PR_MODEL_ILP32)
7727c478bd9Sstevel@tonic-gate 						rv1 &= 0xffffffff;
7737c478bd9Sstevel@tonic-gate #endif
7747c478bd9Sstevel@tonic-gate 					fmt = "= %#lo";
7757c478bd9Sstevel@tonic-gate 					break;
776*f48205beScasper 				case UNS:
777*f48205beScasper #ifdef _LP64
778*f48205beScasper 					if (data_model == PR_MODEL_ILP32)
779*f48205beScasper 						rv1 &= 0xffffffff;
780*f48205beScasper #endif
781*f48205beScasper 					fmt = "= %lu";
782*f48205beScasper 					break;
7837c478bd9Sstevel@tonic-gate 				default:
7847c478bd9Sstevel@tonic-gate 					fmt = "= %ld";
7857c478bd9Sstevel@tonic-gate 					break;
7867c478bd9Sstevel@tonic-gate 				}
7877c478bd9Sstevel@tonic-gate 			}
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 			(void) printf(fmt, rv1, rv2);
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 			switch (stp->rval[1]) {
7927c478bd9Sstevel@tonic-gate 			case NOV:
7937c478bd9Sstevel@tonic-gate 				fmt = NULL;
7947c478bd9Sstevel@tonic-gate 				break;
7957c478bd9Sstevel@tonic-gate 			case HEX:
7967c478bd9Sstevel@tonic-gate #ifdef _LP64
7977c478bd9Sstevel@tonic-gate 				if (data_model == PR_MODEL_ILP32)
7987c478bd9Sstevel@tonic-gate 					rv2 &= 0xffffffff;
7997c478bd9Sstevel@tonic-gate #endif
8007c478bd9Sstevel@tonic-gate 				fmt = " [0x%.8lX]";
8017c478bd9Sstevel@tonic-gate 				break;
8027c478bd9Sstevel@tonic-gate 			case HHX:
8037c478bd9Sstevel@tonic-gate #ifdef _LP64
8047c478bd9Sstevel@tonic-gate 				if (data_model == PR_MODEL_ILP32)
8057c478bd9Sstevel@tonic-gate 					rv2 &= 0xffffffff;
8067c478bd9Sstevel@tonic-gate #endif
8077c478bd9Sstevel@tonic-gate 				fmt = " [0x%.4lX]";
8087c478bd9Sstevel@tonic-gate 				break;
8097c478bd9Sstevel@tonic-gate 			case OCT:
8107c478bd9Sstevel@tonic-gate #ifdef _LP64
8117c478bd9Sstevel@tonic-gate 				if (data_model == PR_MODEL_ILP32)
8127c478bd9Sstevel@tonic-gate 					rv2 &= 0xffffffff;
8137c478bd9Sstevel@tonic-gate #endif
8147c478bd9Sstevel@tonic-gate 				fmt = " [%#lo]";
8157c478bd9Sstevel@tonic-gate 				break;
816*f48205beScasper 			case UNS:
817*f48205beScasper #ifdef _LP64
818*f48205beScasper 				if (data_model == PR_MODEL_ILP32)
819*f48205beScasper 					rv2 &= 0xffffffff;
820*f48205beScasper #endif
821*f48205beScasper 				fmt = " [%lu]";
822*f48205beScasper 				break;
8237c478bd9Sstevel@tonic-gate 			default:
8247c478bd9Sstevel@tonic-gate 				fmt = " [%ld]";
8257c478bd9Sstevel@tonic-gate 				break;
8267c478bd9Sstevel@tonic-gate 			}
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 			if (fmt != NULL)
8297c478bd9Sstevel@tonic-gate 				(void) printf(fmt, rv2);
8307c478bd9Sstevel@tonic-gate 			(void) fputc('\n', stdout);
8317c478bd9Sstevel@tonic-gate 		}
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 		if (what == SYS_forkall ||
8347c478bd9Sstevel@tonic-gate 		    what == SYS_vfork ||
835657b1f3dSraf 		    what == SYS_fork1 ||
836657b1f3dSraf 		    what == SYS_forksys) {
8377c478bd9Sstevel@tonic-gate 			if (pri->Rval2 == 0)		/* child was created */
8387c478bd9Sstevel@tonic-gate 				pri->child = pri->Rval1;
8397c478bd9Sstevel@tonic-gate 			else if (cflag && istraced)	/* this is the child */
8407c478bd9Sstevel@tonic-gate 				scp->count--;
8417c478bd9Sstevel@tonic-gate 		}
8427c478bd9Sstevel@tonic-gate 		if (what == SYS_lwp_create && pri->Rval1 == 0 &&
8437c478bd9Sstevel@tonic-gate 		    cflag && istraced)		/* this is the created lwp */
8447c478bd9Sstevel@tonic-gate 			scp->count--;
8457c478bd9Sstevel@tonic-gate 	}
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate #define	ISREAD(code) \
8487c478bd9Sstevel@tonic-gate 	((code) == SYS_read || (code) == SYS_pread || (code) == SYS_pread64 || \
8497c478bd9Sstevel@tonic-gate 	(code) == SYS_recv || (code) == SYS_recvfrom)
8507c478bd9Sstevel@tonic-gate #define	ISWRITE(code) \
8517c478bd9Sstevel@tonic-gate 	((code) == SYS_write || (code) == SYS_pwrite || \
8527c478bd9Sstevel@tonic-gate 	(code) == SYS_pwrite64 || (code) == SYS_send || (code) == SYS_sendto)
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	if (!cflag && istraced) {
8557c478bd9Sstevel@tonic-gate 		int fdp1 = (int)pri->sys_args[0] + 1; /* filedescriptor + 1 */
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 		if (raw) {
8587c478bd9Sstevel@tonic-gate 			if (what != SYS_exec && what != SYS_execve)
8597c478bd9Sstevel@tonic-gate 				showpaths(pri, stp);
8607c478bd9Sstevel@tonic-gate 			if (ISREAD(what) || ISWRITE(what)) {
8617c478bd9Sstevel@tonic-gate 				if (pri->iob_buf[0] != '\0')
8627c478bd9Sstevel@tonic-gate 					(void) printf("%s     0x%.8lX: %s\n",
8637c478bd9Sstevel@tonic-gate 						pri->pname, pri->sys_args[1],
8647c478bd9Sstevel@tonic-gate 						pri->iob_buf);
8657c478bd9Sstevel@tonic-gate 			}
8667c478bd9Sstevel@tonic-gate 		}
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 		/*
8697c478bd9Sstevel@tonic-gate 		 * Show buffer contents for read()/pread() or write()/pwrite().
8707c478bd9Sstevel@tonic-gate 		 * IOBSIZE bytes have already been shown;
8717c478bd9Sstevel@tonic-gate 		 * don't show them again unless there's more.
8727c478bd9Sstevel@tonic-gate 		 */
8737c478bd9Sstevel@tonic-gate 		if ((ISREAD(what) && pri->Errno == 0 &&
8747c478bd9Sstevel@tonic-gate 		    prismember(&readfd, fdp1)) ||
8757c478bd9Sstevel@tonic-gate 		    (ISWRITE(what) && prismember(&writefd, fdp1))) {
8767c478bd9Sstevel@tonic-gate 			long nb = ISWRITE(what) ? pri->sys_args[2] : pri->Rval1;
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 			if (nb > IOBSIZE) {
8797c478bd9Sstevel@tonic-gate 				/* enter region of lengthy output */
8807c478bd9Sstevel@tonic-gate 				if (nb > MYBUFSIZ / 4)
8817c478bd9Sstevel@tonic-gate 					Eserialize();
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 				showbuffer(pri, pri->sys_args[1], nb);
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 				/* exit region of lengthy output */
8867c478bd9Sstevel@tonic-gate 				if (nb > MYBUFSIZ / 4)
8877c478bd9Sstevel@tonic-gate 					Xserialize();
8887c478bd9Sstevel@tonic-gate 			}
8897c478bd9Sstevel@tonic-gate 		}
8907c478bd9Sstevel@tonic-gate #undef	ISREAD
8917c478bd9Sstevel@tonic-gate #undef	ISWRITE
8927c478bd9Sstevel@tonic-gate 		/*
8937c478bd9Sstevel@tonic-gate 		 * Do verbose interpretation if requested.
8947c478bd9Sstevel@tonic-gate 		 * If buffer contents for read or write have been requested and
8957c478bd9Sstevel@tonic-gate 		 * this is a readv() or writev(), force verbose interpretation.
8967c478bd9Sstevel@tonic-gate 		 */
8977c478bd9Sstevel@tonic-gate 		if (prismember(&verbose, what) ||
89881006e0fSja 		    ((what == SYS_readv || what == SYS_recvmsg) &&
89981006e0fSja 		    pri->Errno == 0 && prismember(&readfd, fdp1)) ||
90081006e0fSja 		    ((what == SYS_writev || what == SYS_sendfilev ||
90181006e0fSja 		    what == SYS_sendmsg) &&
9027c478bd9Sstevel@tonic-gate 		    prismember(&writefd, fdp1)))
9037c478bd9Sstevel@tonic-gate 			expound(pri, pri->Rval1, raw);
9047c478bd9Sstevel@tonic-gate 	}
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	return (!cflag && istraced);
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate void
9107c478bd9Sstevel@tonic-gate showpaths(private_t *pri, const struct systable *stp)
9117c478bd9Sstevel@tonic-gate {
9127c478bd9Sstevel@tonic-gate 	int i;
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	for (i = 0; i < pri->sys_nargs; i++) {
9157c478bd9Sstevel@tonic-gate 		if ((stp->arg[i] == STG) ||
9167c478bd9Sstevel@tonic-gate 		    (stp->arg[i] == RST && !pri->Errno) ||
9177c478bd9Sstevel@tonic-gate 		    (stp->arg[i] == RLK && !pri->Errno && pri->Rval1 > 0)) {
9187c478bd9Sstevel@tonic-gate 			long addr = pri->sys_args[i];
9197c478bd9Sstevel@tonic-gate 			int maxleng =
9207c478bd9Sstevel@tonic-gate 			    (stp->arg[i] == RLK)? (int)pri->Rval1 : PATH_MAX;
9217c478bd9Sstevel@tonic-gate 			char *s;
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 			if (i == 0 && pri->sys_valid)	/* already fetched */
9247c478bd9Sstevel@tonic-gate 				s = pri->sys_path;
9257c478bd9Sstevel@tonic-gate 			else
9267c478bd9Sstevel@tonic-gate 				s = fetchstring(pri, addr,
9277c478bd9Sstevel@tonic-gate 				    maxleng > PATH_MAX ? PATH_MAX : maxleng);
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 			if (s != (char *)NULL)
9307c478bd9Sstevel@tonic-gate 				(void) printf("%s     0x%.8lX: \"%s\"\n",
9317c478bd9Sstevel@tonic-gate 					pri->pname, addr, s);
9327c478bd9Sstevel@tonic-gate 		}
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate }
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate /*
9377c478bd9Sstevel@tonic-gate  * Display arguments to successful exec().
9387c478bd9Sstevel@tonic-gate  */
9397c478bd9Sstevel@tonic-gate void
9407c478bd9Sstevel@tonic-gate showargs(private_t *pri, int raw)
9417c478bd9Sstevel@tonic-gate {
9427c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
9437c478bd9Sstevel@tonic-gate 	int nargs;
9447c478bd9Sstevel@tonic-gate 	long ap;
9457c478bd9Sstevel@tonic-gate 	int ptrsize;
9467c478bd9Sstevel@tonic-gate 	int fail;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	pri->length = 0;
9497c478bd9Sstevel@tonic-gate 	ptrsize = (data_model == PR_MODEL_LP64)? 8 : 4;
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)	/* XX64 */
9527c478bd9Sstevel@tonic-gate 	ap = (long)Lsp->pr_reg[R_SP];
9537c478bd9Sstevel@tonic-gate 	fail = (Pread(Proc, &nargs, sizeof (nargs), ap) != sizeof (nargs));
9547c478bd9Sstevel@tonic-gate 	ap += ptrsize;
9557c478bd9Sstevel@tonic-gate #endif /* i386 */
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate #if sparc
9587c478bd9Sstevel@tonic-gate 	if (data_model == PR_MODEL_LP64) {
9597c478bd9Sstevel@tonic-gate 		int64_t xnargs;
9607c478bd9Sstevel@tonic-gate 		ap = (long)(Lsp->pr_reg[R_SP]) + 16 * sizeof (int64_t)
9617c478bd9Sstevel@tonic-gate 			+ STACK_BIAS;
9627c478bd9Sstevel@tonic-gate 		fail = (Pread(Proc, &xnargs, sizeof (xnargs), ap) !=
9637c478bd9Sstevel@tonic-gate 			sizeof (xnargs));
9647c478bd9Sstevel@tonic-gate 		nargs = (int)xnargs;
9657c478bd9Sstevel@tonic-gate 	} else {
9667c478bd9Sstevel@tonic-gate 		ap = (long)(Lsp->pr_reg[R_SP]) + 16 * sizeof (int32_t);
9677c478bd9Sstevel@tonic-gate 		fail = (Pread(Proc, &nargs, sizeof (nargs), ap) !=
9687c478bd9Sstevel@tonic-gate 			sizeof (nargs));
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 	ap += ptrsize;
9717c478bd9Sstevel@tonic-gate #endif /* sparc */
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	if (fail) {
9747c478bd9Sstevel@tonic-gate 		(void) printf("\n%s\t*** Bad argument list? ***\n", pri->pname);
9757c478bd9Sstevel@tonic-gate 		return;
9767c478bd9Sstevel@tonic-gate 	}
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	(void) printf("  argc = %d\n", nargs);
9797c478bd9Sstevel@tonic-gate 	if (raw)
9807c478bd9Sstevel@tonic-gate 		showpaths(pri, &systable[SYS_exec]);
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	show_cred(pri, FALSE);
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	if (aflag || eflag) {		/* dump args or environment */
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 		/* enter region of (potentially) lengthy output */
9877c478bd9Sstevel@tonic-gate 		Eserialize();
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 		if (aflag)		/* dump the argument list */
9907c478bd9Sstevel@tonic-gate 			dumpargs(pri, ap, "argv:");
9917c478bd9Sstevel@tonic-gate 		ap += (nargs+1) * ptrsize;
9927c478bd9Sstevel@tonic-gate 		if (eflag)		/* dump the environment */
9937c478bd9Sstevel@tonic-gate 			dumpargs(pri, ap, "envp:");
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 		/* exit region of lengthy output */
9967c478bd9Sstevel@tonic-gate 		Xserialize();
9977c478bd9Sstevel@tonic-gate 	}
9987c478bd9Sstevel@tonic-gate }
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate void
10017c478bd9Sstevel@tonic-gate dumpargs(private_t *pri, long ap, const char *str)
10027c478bd9Sstevel@tonic-gate {
10037c478bd9Sstevel@tonic-gate 	char *string;
10047c478bd9Sstevel@tonic-gate 	unsigned int leng = 0;
10057c478bd9Sstevel@tonic-gate 	int ptrsize;
10067c478bd9Sstevel@tonic-gate 	long arg = 0;
10077c478bd9Sstevel@tonic-gate 	char *argaddr;
10087c478bd9Sstevel@tonic-gate 	char badaddr[32];
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	if (interrupt)
10117c478bd9Sstevel@tonic-gate 		return;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate #ifdef _LP64
10147c478bd9Sstevel@tonic-gate 	if (data_model == PR_MODEL_LP64) {
10157c478bd9Sstevel@tonic-gate 		argaddr = (char *)&arg;
10167c478bd9Sstevel@tonic-gate 		ptrsize = 8;
10177c478bd9Sstevel@tonic-gate 	} else {
10187c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
10197c478bd9Sstevel@tonic-gate 		argaddr = (char *)&arg;
10207c478bd9Sstevel@tonic-gate #else
10217c478bd9Sstevel@tonic-gate 		argaddr = (char *)&arg + 4;
10227c478bd9Sstevel@tonic-gate #endif
10237c478bd9Sstevel@tonic-gate 		ptrsize = 4;
10247c478bd9Sstevel@tonic-gate 	}
10257c478bd9Sstevel@tonic-gate #else
10267c478bd9Sstevel@tonic-gate 	argaddr = (char *)&arg;
10277c478bd9Sstevel@tonic-gate 	ptrsize = 4;
10287c478bd9Sstevel@tonic-gate #endif
10297c478bd9Sstevel@tonic-gate 	putpname(pri);
10307c478bd9Sstevel@tonic-gate 	(void) fputc(' ', stdout);
10317c478bd9Sstevel@tonic-gate 	(void) fputs(str, stdout);
10327c478bd9Sstevel@tonic-gate 	leng += 1 + strlen(str);
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	while (!interrupt) {
10357c478bd9Sstevel@tonic-gate 		if (Pread(Proc, argaddr, ptrsize, ap) != ptrsize) {
10367c478bd9Sstevel@tonic-gate 			(void) printf("\n%s\t*** Bad argument list? ***\n",
10377c478bd9Sstevel@tonic-gate 				pri->pname);
10387c478bd9Sstevel@tonic-gate 			return;
10397c478bd9Sstevel@tonic-gate 		}
10407c478bd9Sstevel@tonic-gate 		ap += ptrsize;
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 		if (arg == 0)
10437c478bd9Sstevel@tonic-gate 			break;
10447c478bd9Sstevel@tonic-gate 		string = fetchstring(pri, arg, PATH_MAX);
10457c478bd9Sstevel@tonic-gate 		if (string == NULL) {
10467c478bd9Sstevel@tonic-gate 			(void) sprintf(badaddr, "BadAddress:0x%.8lX", arg);
10477c478bd9Sstevel@tonic-gate 			string = badaddr;
10487c478bd9Sstevel@tonic-gate 		}
10497c478bd9Sstevel@tonic-gate 		if ((leng += strlen(string)) < 63) {
10507c478bd9Sstevel@tonic-gate 			(void) fputc(' ', stdout);
10517c478bd9Sstevel@tonic-gate 			leng++;
10527c478bd9Sstevel@tonic-gate 		} else {
10537c478bd9Sstevel@tonic-gate 			(void) fputc('\n', stdout);
10547c478bd9Sstevel@tonic-gate 			leng = 0;
10557c478bd9Sstevel@tonic-gate 			putpname(pri);
10567c478bd9Sstevel@tonic-gate 			(void) fputs("  ", stdout);
10577c478bd9Sstevel@tonic-gate 			leng += 2 + strlen(string);
10587c478bd9Sstevel@tonic-gate 		}
10597c478bd9Sstevel@tonic-gate 		(void) fputs(string, stdout);
10607c478bd9Sstevel@tonic-gate 	}
10617c478bd9Sstevel@tonic-gate 	(void) fputc('\n', stdout);
10627c478bd9Sstevel@tonic-gate }
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate /*
10657c478bd9Sstevel@tonic-gate  * Display contents of read() or write() buffer.
10667c478bd9Sstevel@tonic-gate  */
10677c478bd9Sstevel@tonic-gate void
10687c478bd9Sstevel@tonic-gate showbuffer(private_t *pri, long offset, long count)
10697c478bd9Sstevel@tonic-gate {
10707c478bd9Sstevel@tonic-gate 	char buffer[320];
10717c478bd9Sstevel@tonic-gate 	int nbytes;
10727c478bd9Sstevel@tonic-gate 	char *buf;
10737c478bd9Sstevel@tonic-gate 	int n;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	while (count > 0 && !interrupt) {
10767c478bd9Sstevel@tonic-gate 		nbytes = (count < sizeof (buffer))? count : sizeof (buffer);
10777c478bd9Sstevel@tonic-gate 		if ((nbytes = Pread(Proc, buffer, nbytes, offset)) <= 0)
10787c478bd9Sstevel@tonic-gate 			break;
10797c478bd9Sstevel@tonic-gate 		count -= nbytes;
10807c478bd9Sstevel@tonic-gate 		offset += nbytes;
10817c478bd9Sstevel@tonic-gate 		buf = buffer;
10827c478bd9Sstevel@tonic-gate 		while (nbytes > 0 && !interrupt) {
10837c478bd9Sstevel@tonic-gate 			char obuf[65];
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 			n = (nbytes < 32)? nbytes : 32;
10867c478bd9Sstevel@tonic-gate 			showbytes(buf, n, obuf);
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 			putpname(pri);
10897c478bd9Sstevel@tonic-gate 			(void) fputs("  ", stdout);
10907c478bd9Sstevel@tonic-gate 			(void) fputs(obuf, stdout);
10917c478bd9Sstevel@tonic-gate 			(void) fputc('\n', stdout);
10927c478bd9Sstevel@tonic-gate 			nbytes -= n;
10937c478bd9Sstevel@tonic-gate 			buf += n;
10947c478bd9Sstevel@tonic-gate 		}
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate }
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate void
10997c478bd9Sstevel@tonic-gate showbytes(const char *buf, int n, char *obuf)
11007c478bd9Sstevel@tonic-gate {
11017c478bd9Sstevel@tonic-gate 	int c;
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	while (--n >= 0) {
11047c478bd9Sstevel@tonic-gate 		int c1 = '\\';
11057c478bd9Sstevel@tonic-gate 		int c2;
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 		switch (c = (*buf++ & 0xff)) {
11087c478bd9Sstevel@tonic-gate 		case '\0':
11097c478bd9Sstevel@tonic-gate 			c2 = '0';
11107c478bd9Sstevel@tonic-gate 			break;
11117c478bd9Sstevel@tonic-gate 		case '\b':
11127c478bd9Sstevel@tonic-gate 			c2 = 'b';
11137c478bd9Sstevel@tonic-gate 			break;
11147c478bd9Sstevel@tonic-gate 		case '\t':
11157c478bd9Sstevel@tonic-gate 			c2 = 't';
11167c478bd9Sstevel@tonic-gate 			break;
11177c478bd9Sstevel@tonic-gate 		case '\n':
11187c478bd9Sstevel@tonic-gate 			c2 = 'n';
11197c478bd9Sstevel@tonic-gate 			break;
11207c478bd9Sstevel@tonic-gate 		case '\v':
11217c478bd9Sstevel@tonic-gate 			c2 = 'v';
11227c478bd9Sstevel@tonic-gate 			break;
11237c478bd9Sstevel@tonic-gate 		case '\f':
11247c478bd9Sstevel@tonic-gate 			c2 = 'f';
11257c478bd9Sstevel@tonic-gate 			break;
11267c478bd9Sstevel@tonic-gate 		case '\r':
11277c478bd9Sstevel@tonic-gate 			c2 = 'r';
11287c478bd9Sstevel@tonic-gate 			break;
11297c478bd9Sstevel@tonic-gate 		default:
11307c478bd9Sstevel@tonic-gate 			if (isprint(c)) {
11317c478bd9Sstevel@tonic-gate 				c1 = ' ';
11327c478bd9Sstevel@tonic-gate 				c2 = c;
11337c478bd9Sstevel@tonic-gate 			} else {
11347c478bd9Sstevel@tonic-gate 				c1 = c>>4;
11357c478bd9Sstevel@tonic-gate 				c1 += (c1 < 10)? '0' : 'A'-10;
11367c478bd9Sstevel@tonic-gate 				c2 = c&0xf;
11377c478bd9Sstevel@tonic-gate 				c2 += (c2 < 10)? '0' : 'A'-10;
11387c478bd9Sstevel@tonic-gate 			}
11397c478bd9Sstevel@tonic-gate 			break;
11407c478bd9Sstevel@tonic-gate 		}
11417c478bd9Sstevel@tonic-gate 		*obuf++ = (char)c1;
11427c478bd9Sstevel@tonic-gate 		*obuf++ = (char)c2;
11437c478bd9Sstevel@tonic-gate 	}
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	*obuf = '\0';
11467c478bd9Sstevel@tonic-gate }
1147