xref: /illumos-gate/usr/src/cmd/truss/actions.c (revision e8d3efa1c56e5f2b5368600a2baeb7b1d54a07f8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2026 Oxide Computer Company
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <ctype.h>
34 #include <string.h>
35 #include <memory.h>
36 #include <errno.h>
37 #include <limits.h>
38 #include <sys/types.h>
39 #include <sys/stack.h>
40 #include <signal.h>
41 #include <sys/isa_defs.h>
42 #include <libproc.h>
43 #include <priv.h>
44 #include "ramdata.h"
45 #include "systable.h"
46 #include "print.h"
47 #include "proto.h"
48 
49 /*
50  * Actions to take when process stops.
51  */
52 
53 /*
54  * Function prototypes for static routines in this module.
55  */
56 int	stopsig(private_t *);
57 void	showpaths(private_t *, const struct systable *);
58 void	showargs(private_t *, int);
59 void	dumpargs(private_t *, long, const char *);
60 
61 /*
62  * Report an lwp to be sleeping (if true).
63  */
64 void
65 report_sleeping(private_t *pri, int dotrace)
66 {
67 	const lwpstatus_t *Lsp = pri->lwpstat;
68 	int sys = Lsp->pr_syscall;
69 
70 	if (!prismember(&trace, sys) || !dotrace ||
71 	    !(Lsp->pr_flags & (PR_ASLEEP|PR_VFORKP))) {
72 		/* Make sure we catch sysexit even if we're not tracing it. */
73 		(void) Psysexit(Proc, sys, TRUE);
74 		return;
75 	}
76 
77 	pri->length = 0;
78 	pri->Errno = 0;
79 	pri->ErrPriv = PRIV_NONE;
80 	pri->Rval1 = pri->Rval2 = 0;
81 	(void) sysentry(pri, dotrace);
82 	make_pname(pri, 0);
83 	putpname(pri);
84 	timestamp(pri);
85 	pri->length += printf("%s", pri->sys_string);
86 	pri->sys_leng = 0;
87 	*pri->sys_string = '\0';
88 	pri->length >>= 3;
89 	if (Lsp->pr_flags & PR_VFORKP)
90 		pri->length += 2;
91 	if (pri->length >= 4)
92 		(void) fputc(' ', stdout);
93 	for (; pri->length < 4; pri->length++)
94 		(void) fputc('\t', stdout);
95 	if (Lsp->pr_flags & PR_VFORKP)
96 		(void) fputs("(waiting for child to exit()/exec()...)\n",
97 		    stdout);
98 	else
99 		(void) fputs("(sleeping...)\n", stdout);
100 	pri->length = 0;
101 	if (prismember(&verbose, sys)) {
102 		int raw = prismember(&rawout, sys);
103 		pri->Errno = 1;
104 		expound(pri, 0, raw);
105 		pri->Errno = 0;
106 	}
107 	Flush();
108 }
109 
110 /*
111  * requested() gets called for these reasons:
112  *	flag == JOBSIG:		report nothing; change state to JOBSTOP
113  *	flag == JOBSTOP:	report "Continued ..."
114  *	default:		report sleeping system call
115  *
116  * It returns a new flag:  JOBSTOP or SLEEPING or 0.
117  */
118 int
119 requested(private_t *pri, int flag, int dotrace)
120 {
121 	const lwpstatus_t *Lsp = pri->lwpstat;
122 	int sig = Lsp->pr_cursig;
123 	int newflag = 0;
124 
125 	switch (flag) {
126 	case JOBSIG:
127 		return (JOBSTOP);
128 
129 	case JOBSTOP:
130 		if (dotrace && !cflag && prismember(&signals, sig)) {
131 			pri->length = 0;
132 			putpname(pri);
133 			timestamp(pri);
134 			(void) printf("    Continued with signal #%d, %s",
135 			    sig, signame(pri, sig));
136 			if (Lsp->pr_action.sa_handler == SIG_DFL)
137 				(void) printf(" [default]");
138 			else if (Lsp->pr_action.sa_handler == SIG_IGN)
139 				(void) printf(" [ignored]");
140 			else
141 				(void) printf(" [caught]");
142 			(void) fputc('\n', stdout);
143 			Flush();
144 		}
145 		newflag = 0;
146 		break;
147 
148 	default:
149 		newflag = SLEEPING;
150 		if (!cflag)
151 			report_sleeping(pri, dotrace);
152 		break;
153 	}
154 
155 	return (newflag);
156 }
157 
158 int
159 jobcontrol(private_t *pri, int dotrace)
160 {
161 	const lwpstatus_t *Lsp = pri->lwpstat;
162 	int sig = stopsig(pri);
163 
164 	if (sig == 0)
165 		return (0);
166 
167 	if (dotrace && !cflag &&		/* not just counting */
168 	    prismember(&signals, sig)) {	/* tracing this signal */
169 		int sys;
170 
171 		pri->length = 0;
172 		putpname(pri);
173 		timestamp(pri);
174 		(void) printf("    Stopped by signal #%d, %s",
175 		    sig, signame(pri, sig));
176 		if ((Lsp->pr_flags & PR_ASLEEP) &&
177 		    (sys = Lsp->pr_syscall) > 0 && sys <= PRMAXSYS)
178 			(void) printf(", in %s()",
179 			    sysname(pri, sys, getsubcode(pri)));
180 		(void) fputc('\n', stdout);
181 		Flush();
182 	}
183 
184 	return (JOBSTOP);
185 }
186 
187 /*
188  * Return the signal the process stopped on iff process is already stopped on
189  * PR_JOBCONTROL or is stopped on PR_SIGNALLED or PR_REQUESTED with a current
190  * signal that will cause a JOBCONTROL stop when the process is set running.
191  */
192 int
193 stopsig(private_t *pri)
194 {
195 	const lwpstatus_t *Lsp = pri->lwpstat;
196 	int sig = 0;
197 
198 	if (Lsp->pr_flags & PR_STOPPED) {
199 		switch (Lsp->pr_why) {
200 		case PR_JOBCONTROL:
201 			sig = Lsp->pr_what;
202 			if (sig < 0 || sig > PRMAXSIG)
203 				sig = 0;
204 			break;
205 		case PR_SIGNALLED:
206 		case PR_REQUESTED:
207 			if (Lsp->pr_action.sa_handler == SIG_DFL) {
208 				switch (Lsp->pr_cursig) {
209 				case SIGSTOP:
210 					sig = SIGSTOP;
211 					break;
212 				case SIGTSTP:
213 				case SIGTTIN:
214 				case SIGTTOU:
215 					if (!(Lsp->pr_flags & PR_ORPHAN))
216 						sig = Lsp->pr_cursig;
217 					break;
218 				}
219 			}
220 			break;
221 		}
222 	}
223 
224 	return (sig);
225 }
226 
227 int
228 signalled(private_t *pri, int flag, int dotrace)
229 {
230 	const lwpstatus_t *Lsp = pri->lwpstat;
231 	int sig = Lsp->pr_what;
232 
233 	if (sig <= 0 || sig > PRMAXSIG)	/* check bounds */
234 		return (0);
235 
236 	if (dotrace && cflag) {			/* just counting */
237 		(void) mutex_lock(&count_lock);
238 		Cp->sigcount[sig]++;
239 		(void) mutex_unlock(&count_lock);
240 	}
241 
242 	if (sig == SIGCONT && (flag == JOBSIG || flag == JOBSTOP))
243 		flag = requested(pri, JOBSTOP, dotrace);
244 	else if ((flag = jobcontrol(pri, dotrace)) == 0 &&
245 	    !cflag && dotrace &&
246 	    prismember(&signals, sig)) {
247 		int sys;
248 
249 		pri->length = 0;
250 		putpname(pri);
251 		timestamp(pri);
252 		(void) printf("    Received signal #%d, %s",
253 		    sig, signame(pri, sig));
254 		if ((Lsp->pr_flags & PR_ASLEEP) &&
255 		    (sys = Lsp->pr_syscall) > 0 && sys <= PRMAXSYS)
256 			(void) printf(", in %s()",
257 			    sysname(pri, sys, getsubcode(pri)));
258 		if (Lsp->pr_action.sa_handler == SIG_DFL)
259 			(void) printf(" [default]");
260 		else if (Lsp->pr_action.sa_handler == SIG_IGN)
261 			(void) printf(" [ignored]");
262 		else
263 			(void) printf(" [caught]");
264 		(void) fputc('\n', stdout);
265 		if (Lsp->pr_info.si_code != 0 ||
266 		    Lsp->pr_info.si_pid != 0)
267 			print_siginfo(pri, &Lsp->pr_info);
268 		Flush();
269 	}
270 
271 	if (flag == JOBSTOP)
272 		flag = JOBSIG;
273 	return (flag);
274 }
275 
276 int
277 faulted(private_t *pri, int dotrace)
278 {
279 	const lwpstatus_t *Lsp = pri->lwpstat;
280 	int flt = Lsp->pr_what;
281 
282 	if ((uint_t)flt > PRMAXFAULT || !prismember(&faults, flt) || !dotrace)
283 		return (0);
284 
285 	(void) mutex_lock(&count_lock);
286 	Cp->fltcount[flt]++;
287 	(void) mutex_unlock(&count_lock);
288 
289 	if (cflag)		/* just counting */
290 		return (1);
291 
292 	pri->length = 0;
293 	putpname(pri);
294 	timestamp(pri);
295 
296 	(void) printf("    Incurred fault #%d, %s  %%pc = 0x%.8lX",
297 	    flt, proc_fltname(flt, pri->flt_name, sizeof (pri->flt_name)),
298 	    (long)Lsp->pr_reg[R_PC]);
299 
300 	if (flt == FLTPAGE)
301 		(void) printf("  addr = 0x%.8lX",
302 		    (long)Lsp->pr_info.si_addr);
303 	(void) fputc('\n', stdout);
304 	if (Lsp->pr_info.si_signo != 0)
305 		print_siginfo(pri, &Lsp->pr_info);
306 	Flush();
307 	return (1);
308 }
309 
310 /*
311  * Set up pri->sys_nargs and pri->sys_args[] (syscall args).
312  */
313 void
314 setupsysargs(private_t *pri, int what)
315 {
316 	const lwpstatus_t *Lsp = pri->lwpstat;
317 	int nargs;
318 	int i;
319 
320 #if sparc
321 	/* determine whether syscall is indirect */
322 	pri->sys_indirect = (Lsp->pr_reg[R_G1] == SYS_syscall)? 1 : 0;
323 #else
324 	pri->sys_indirect = 0;
325 #endif
326 
327 	(void) memset(pri->sys_args, 0, sizeof (pri->sys_args));
328 	if (what != Lsp->pr_syscall) {	/* assertion */
329 		(void) printf("%s\t*** Inconsistent syscall: %d vs %d ***\n",
330 		    pri->pname, what, Lsp->pr_syscall);
331 	}
332 	nargs = Lsp->pr_nsysarg;
333 	for (i = 0;
334 	    i < nargs && i < sizeof (pri->sys_args) / sizeof (pri->sys_args[0]);
335 	    i++)
336 		pri->sys_args[i] = Lsp->pr_sysarg[i];
337 	pri->sys_nargs = nargs;
338 }
339 
340 #define	ISREAD(code) \
341 	((code) == SYS_read || (code) == SYS_pread || \
342 	(code) == SYS_pread64 || (code) == SYS_readv || \
343 	(code) == SYS_recv || (code) == SYS_recvfrom)
344 #define	ISWRITE(code) \
345 	((code) == SYS_write || (code) == SYS_pwrite || \
346 	(code) == SYS_pwrite64 || (code) == SYS_writev || \
347 	(code) == SYS_send || (code) == SYS_sendto)
348 
349 /*
350  * Return TRUE iff syscall is being traced.
351  */
352 int
353 sysentry(private_t *pri, int dotrace)
354 {
355 	pid_t pid = Pstatus(Proc)->pr_pid;
356 	const lwpstatus_t *Lsp = pri->lwpstat;
357 	long arg;
358 	int nargs;
359 	int i;
360 	int x;
361 	int len;
362 	char *s;
363 	const struct systable *stp;
364 	int what = Lsp->pr_what;
365 	int subcode;
366 	int istraced;
367 	int raw;
368 
369 	/* for reporting sleeping system calls */
370 	if (what == 0 && (Lsp->pr_flags & (PR_ASLEEP|PR_VFORKP)))
371 		what = Lsp->pr_syscall;
372 
373 	/* protect ourself from operating system error */
374 	if (what <= 0 || what > PRMAXSYS)
375 		what = 0;
376 
377 	/*
378 	 * Set up the system call arguments (pri->sys_nargs & pri->sys_args[]).
379 	 */
380 	setupsysargs(pri, what);
381 	nargs = pri->sys_nargs;
382 
383 	/* get systable entry for this syscall */
384 	subcode = getsubcode(pri);
385 	stp = subsys(what, subcode);
386 
387 	if (nargs > stp->nargs)
388 		nargs = stp->nargs;
389 	pri->sys_nargs = nargs;
390 
391 	/*
392 	 * Fetch and remember first argument if it's a string,
393 	 * or second argument if SYS_openat or SYS_openat64.
394 	 */
395 	pri->sys_valid = FALSE;
396 	if ((nargs > 0 && stp->arg[0] == STG) ||
397 	    (nargs > 1 && (what == SYS_openat || what == SYS_openat64))) {
398 		long offset;
399 		uint32_t offset32;
400 
401 		/*
402 		 * Special case for exit from exec().
403 		 * The address in pri->sys_args[0] refers to the old process
404 		 * image.  We must fetch the string from the new image.
405 		 */
406 		if (Lsp->pr_why == PR_SYSEXIT && what == SYS_execve) {
407 			psinfo_t psinfo;
408 			long argv;
409 			auxv_t auxv[32];
410 			int naux;
411 
412 			offset = 0;
413 			naux = proc_get_auxv(pid, auxv, 32);
414 			for (i = 0; i < naux; i++) {
415 				if (auxv[i].a_type == AT_SUN_EXECNAME) {
416 					offset = (long)auxv[i].a_un.a_ptr;
417 					break;
418 				}
419 			}
420 			if (offset == 0 &&
421 			    proc_get_psinfo(pid, &psinfo) == 0) {
422 				argv = (long)psinfo.pr_argv;
423 				if (data_model == PR_MODEL_LP64)
424 					(void) Pread(Proc, &offset,
425 					    sizeof (offset), argv);
426 				else {
427 					offset32 = 0;
428 					(void) Pread(Proc, &offset32,
429 					    sizeof (offset32), argv);
430 					offset = offset32;
431 				}
432 			}
433 		} else if (stp->arg[0] == STG) {
434 			offset = pri->sys_args[0];
435 		} else {
436 			offset = pri->sys_args[1];
437 		}
438 		if ((s = fetchstring(pri, offset, PATH_MAX)) != NULL) {
439 			pri->sys_valid = TRUE;
440 			len = strlen(s);
441 			/* reallocate if necessary */
442 			while (len >= pri->sys_psize) {
443 				free(pri->sys_path);
444 				pri->sys_path = my_malloc(pri->sys_psize *= 2,
445 				    "pathname buffer");
446 			}
447 			(void) strcpy(pri->sys_path, s); /* remember pathname */
448 		}
449 	}
450 
451 	istraced = dotrace && prismember(&trace, what);
452 	raw = prismember(&rawout, what);
453 
454 	/* force tracing of read/write buffer dump syscalls */
455 	if (!istraced && nargs > 2) {
456 		int fdp1 = (int)pri->sys_args[0] + 1;
457 
458 		if (ISREAD(what)) {
459 			if (prismember(&readfd, fdp1))
460 				istraced = TRUE;
461 		} else if (ISWRITE(what)) {
462 			if (prismember(&writefd, fdp1))
463 				istraced = TRUE;
464 		}
465 	}
466 
467 	pri->sys_leng = 0;
468 	if (cflag || !istraced)		/* just counting */
469 		*pri->sys_string = 0;
470 	else {
471 		int argprinted = FALSE;
472 		const char *name;
473 
474 		name = sysname(pri, what, raw? -1 : subcode);
475 		grow(pri, strlen(name) + 1);
476 		pri->sys_leng = snprintf(pri->sys_string, pri->sys_ssize,
477 		    "%s(", name);
478 		for (i = 0; i < nargs; i++) {
479 			arg = pri->sys_args[i];
480 			x = stp->arg[i];
481 
482 			if (!raw && pri->sys_valid &&
483 			    ((i == 0 && x == STG) ||
484 			    (i == 1 && (what == SYS_openat ||
485 			    what == SYS_openat64)))) {	/* already fetched */
486 				if (argprinted)
487 					outstring(pri, ", ");
488 				escape_string(pri, pri->sys_path);
489 				argprinted = TRUE;
490 			} else if (x != NOV && (x != HID || raw)) {
491 				if (argprinted)
492 					outstring(pri, ", ");
493 				if (x == LLO)
494 					(*Print[x])(pri, raw, arg,
495 					    pri->sys_args[++i]);
496 				else
497 					(*Print[x])(pri, raw, arg);
498 				argprinted = TRUE;
499 			}
500 		}
501 		outstring(pri, ")");
502 	}
503 
504 	return (istraced);
505 }
506 #undef	ISREAD
507 #undef	ISWRITE
508 
509 /*
510  * sysexit() returns non-zero if anything was printed.
511  */
512 int
513 sysexit(private_t *pri, int dotrace)
514 {
515 	const lwpstatus_t *Lsp = pri->lwpstat;
516 	int what = Lsp->pr_what;
517 	struct syscount *scp;
518 	const struct systable *stp;
519 	int subcode;
520 	int istraced;
521 	int raw;
522 
523 	/* protect ourself from operating system error */
524 	if (what <= 0 || what > PRMAXSYS)
525 		return (0);
526 
527 	/*
528 	 * If we aren't supposed to be tracing this one, then
529 	 * delete it from the traced signal set.  We got here
530 	 * because the process was sleeping in an untraced syscall.
531 	 */
532 	if (!prismember(&traceeven, what)) {
533 		(void) Psysexit(Proc, what, FALSE);
534 		return (0);
535 	}
536 
537 	/* pick up registers & set pri->Errno before anything else */
538 	pri->Errno = Lsp->pr_errno;
539 	pri->ErrPriv = Lsp->pr_errpriv;
540 	pri->Rval1 = Lsp->pr_rval1;
541 	pri->Rval2 = Lsp->pr_rval2;
542 
543 	switch (what) {
544 	case SYS_exit:		/* these are traced on entry */
545 	case SYS_lwp_exit:
546 	case SYS_context:
547 		istraced = dotrace && prismember(&trace, what);
548 		break;
549 	case SYS_execve:	/* this is normally traced on entry */
550 		istraced = dotrace && prismember(&trace, what);
551 		if (pri->exec_string && *pri->exec_string) {
552 			if (!cflag && istraced) { /* print exec() string now */
553 				if (pri->exec_pname[0] != '\0')
554 					(void) fputs(pri->exec_pname, stdout);
555 				timestamp(pri);
556 				(void) fputs(pri->exec_string, stdout);
557 			}
558 			pri->exec_pname[0] = '\0';
559 			pri->exec_string[0] = '\0';
560 			break;
561 		}
562 		/* FALLTHROUGH */
563 	default:
564 		/* we called sysentry() in main() for these */
565 		if (what == SYS_openat || what == SYS_openat64 ||
566 		    what == SYS_open || what == SYS_open64)
567 			istraced = dotrace && prismember(&trace, what);
568 		else
569 			istraced = sysentry(pri, dotrace) && dotrace;
570 		pri->length = 0;
571 		if (!cflag && istraced) {
572 			putpname(pri);
573 			timestamp(pri);
574 			pri->length += printf("%s", pri->sys_string);
575 		}
576 		pri->sys_leng = 0;
577 		*pri->sys_string = '\0';
578 		break;
579 	}
580 
581 	/* get systable entry for this syscall */
582 	subcode = getsubcode(pri);
583 	stp = subsys(what, subcode);
584 
585 	if (cflag && istraced) {
586 		(void) mutex_lock(&count_lock);
587 		scp = Cp->syscount[what];
588 		if (what == SYS_forksys && subcode >= 3)
589 			scp += subcode - 3;
590 		else if (subcode != -1 &&
591 		    (what != SYS_openat && what != SYS_openat64 &&
592 		    what != SYS_open && what != SYS_open64 &&
593 		    what != SYS_lwp_create))
594 			scp += subcode;
595 		scp->count++;
596 		accumulate(&scp->stime, &Lsp->pr_stime, &pri->syslast);
597 		accumulate(&Cp->usrtotal, &Lsp->pr_utime, &pri->usrlast);
598 		pri->syslast = Lsp->pr_stime;
599 		pri->usrlast = Lsp->pr_utime;
600 		(void) mutex_unlock(&count_lock);
601 	}
602 
603 	raw = prismember(&rawout, what);
604 
605 	if (!cflag && istraced) {
606 		if ((what == SYS_vfork || what == SYS_forksys) &&
607 		    pri->Errno == 0 && pri->Rval2 != 0) {
608 			pri->length &= ~07;
609 			if (strlen(sysname(pri, what, raw? -1 : subcode)) < 6) {
610 				(void) fputc('\t', stdout);
611 				pri->length += 8;
612 			}
613 			pri->length +=
614 			    7 + printf("\t(returning as child ...)");
615 		}
616 		if (what == SYS_lwp_create &&
617 		    pri->Errno == 0 && pri->Rval1 == 0) {
618 			pri->length &= ~07;
619 			pri->length +=
620 			    7 + printf("\t(returning as new lwp ...)");
621 		}
622 		if (pri->Errno != 0 || what != SYS_execve) {
623 			/* prepare to print the return code */
624 			pri->length >>= 3;
625 			if (pri->length >= 6)
626 				(void) fputc(' ', stdout);
627 			for (; pri->length < 6; pri->length++)
628 				(void) fputc('\t', stdout);
629 		}
630 	}
631 	pri->length = 0;
632 
633 	if (pri->Errno != 0) {		/* error in syscall */
634 		if (istraced) {
635 			if (cflag)
636 				scp->error++;
637 			else {
638 				const char *ename = errname(pri->Errno);
639 				const char *privname;
640 
641 				(void) printf("Err#%d", pri->Errno);
642 				if (ename != NULL) {
643 					(void) fputc(' ', stdout);
644 					(void) fputs(ename, stdout);
645 				}
646 				switch (pri->ErrPriv) {
647 				case PRIV_NONE:
648 					privname = NULL;
649 					break;
650 				case PRIV_ALL:
651 					privname = "ALL";
652 					break;
653 				case PRIV_MULTIPLE:
654 					privname = "MULTIPLE";
655 					break;
656 				case PRIV_ALLZONE:
657 					privname = "ZONE";
658 					break;
659 				default:
660 					privname = priv_getbynum(pri->ErrPriv);
661 					break;
662 				}
663 				if (privname != NULL)
664 					(void) printf(" [%s]", privname);
665 
666 				(void) fputc('\n', stdout);
667 			}
668 		}
669 	} else {
670 		/* show arguments on successful exec */
671 		if (what == SYS_execve) {
672 			if (!cflag && istraced)
673 				showargs(pri, raw);
674 		} else if (!cflag && istraced) {
675 			const char *fmt = NULL;
676 			long rv1 = pri->Rval1;
677 			long rv2 = pri->Rval2;
678 
679 			/*
680 			 * 32-bit system calls return 32-bit values. We
681 			 * later mask out the upper bits if we want to
682 			 * print these as unsigned values.
683 			 */
684 			if (data_model == PR_MODEL_ILP32) {
685 				rv1 = (int)rv1;
686 				rv2 = (int)rv2;
687 			}
688 
689 			switch (what) {
690 			case SYS_llseek:
691 				rv1 &= 0xffffffff;
692 				rv2 &= 0xffffffff;
693 #ifdef _LONG_LONG_LTOH	/* first long of a longlong is the low order */
694 				if (rv2 != 0) {
695 					long temp = rv1;
696 					fmt = "= 0x%lX%.8lX";
697 					rv1 = rv2;
698 					rv2 = temp;
699 					break;
700 				}
701 #else	/* the other way around */
702 				if (rv1 != 0) {
703 					fmt = "= 0x%lX%.8lX";
704 					break;
705 				}
706 				rv1 = rv2;	/* ugly */
707 #endif
708 				/* FALLTHROUGH */
709 			case SYS_lseek:
710 			case SYS_ulimit:
711 				if (rv1 & 0xff000000) {
712 					if (data_model == PR_MODEL_ILP32)
713 						rv1 &= 0xffffffff;
714 					fmt = "= 0x%.8lX";
715 				}
716 				break;
717 			case SYS_sigtimedwait:
718 				if (raw)
719 					/* EMPTY */;
720 				else if ((fmt = rawsigname(pri, rv1)) != NULL) {
721 					rv1 = (long)fmt;	/* filthy */
722 					fmt = "= %s";
723 				}
724 				break;
725 			case SYS_port:
726 				if (data_model == PR_MODEL_LP64) {
727 					rv2 = rv1 & 0xffffffff;
728 					rv1 = rv1 >> 32;
729 				}
730 				break;
731 			}
732 
733 			if (fmt == NULL) {
734 				switch (stp->rval[0]) {
735 				case HEX:
736 					if (data_model == PR_MODEL_ILP32)
737 						rv1 &= 0xffffffff;
738 					fmt = "= 0x%.8lX";
739 					break;
740 				case HHX:
741 					if (data_model == PR_MODEL_ILP32)
742 						rv1 &= 0xffffffff;
743 					fmt = "= 0x%.4lX";
744 					break;
745 				case OCT:
746 					if (data_model == PR_MODEL_ILP32)
747 						rv1 &= 0xffffffff;
748 					fmt = "= %#lo";
749 					break;
750 				case UNS:
751 					if (data_model == PR_MODEL_ILP32)
752 						rv1 &= 0xffffffff;
753 					fmt = "= %lu";
754 					break;
755 				default:
756 					fmt = "= %ld";
757 					break;
758 				}
759 			}
760 
761 			(void) printf(fmt, rv1, rv2);
762 
763 			switch (stp->rval[1]) {
764 			case NOV:
765 				fmt = NULL;
766 				break;
767 			case HEX:
768 				if (data_model == PR_MODEL_ILP32)
769 					rv2 &= 0xffffffff;
770 				fmt = " [0x%.8lX]";
771 				break;
772 			case HHX:
773 				if (data_model == PR_MODEL_ILP32)
774 					rv2 &= 0xffffffff;
775 				fmt = " [0x%.4lX]";
776 				break;
777 			case OCT:
778 				if (data_model == PR_MODEL_ILP32)
779 					rv2 &= 0xffffffff;
780 				fmt = " [%#lo]";
781 				break;
782 			case UNS:
783 				if (data_model == PR_MODEL_ILP32)
784 					rv2 &= 0xffffffff;
785 				fmt = " [%lu]";
786 				break;
787 			default:
788 				fmt = " [%ld]";
789 				break;
790 			}
791 
792 			if (fmt != NULL)
793 				(void) printf(fmt, rv2);
794 			(void) fputc('\n', stdout);
795 		}
796 
797 		if (what == SYS_vfork || what == SYS_forksys ||
798 		    what == SYS_spawn) {
799 			if (pri->Rval2 == 0)		/* child was created */
800 				pri->child = pri->Rval1;
801 			else if (cflag && istraced)	/* this is the child */
802 				scp->count--;
803 		}
804 		if (what == SYS_lwp_create && pri->Rval1 == 0 &&
805 		    cflag && istraced)		/* this is the created lwp */
806 			scp->count--;
807 	}
808 
809 #define	ISREAD(code) \
810 	((code) == SYS_read || (code) == SYS_pread || (code) == SYS_pread64 || \
811 	(code) == SYS_recv || (code) == SYS_recvfrom)
812 #define	ISWRITE(code) \
813 	((code) == SYS_write || (code) == SYS_pwrite || \
814 	(code) == SYS_pwrite64 || (code) == SYS_send || (code) == SYS_sendto)
815 
816 	if (!cflag && istraced) {
817 		int fdp1 = (int)pri->sys_args[0] + 1; /* filedescriptor + 1 */
818 
819 		if (raw) {
820 			if (what != SYS_execve)
821 				showpaths(pri, stp);
822 			if (ISREAD(what) || ISWRITE(what)) {
823 				if (pri->iob_buf[0] != '\0')
824 					(void) printf("%s     0x%.8lX: %s\n",
825 					    pri->pname, pri->sys_args[1],
826 					    pri->iob_buf);
827 			}
828 		}
829 
830 		/*
831 		 * Show buffer contents for read()/pread() or write()/pwrite().
832 		 * IOBSIZE bytes have already been shown;
833 		 * don't show them again unless there's more.
834 		 */
835 		if ((ISREAD(what) && pri->Errno == 0 &&
836 		    prismember(&readfd, fdp1)) ||
837 		    (ISWRITE(what) && prismember(&writefd, fdp1))) {
838 			long nb = ISWRITE(what) ? pri->sys_args[2] : pri->Rval1;
839 
840 			if (nb > IOBSIZE) {
841 				/* enter region of lengthy output */
842 				if (nb > MYBUFSIZ / 4)
843 					Eserialize();
844 
845 				showbuffer(pri, pri->sys_args[1], nb);
846 
847 				/* exit region of lengthy output */
848 				if (nb > MYBUFSIZ / 4)
849 					Xserialize();
850 			}
851 		}
852 #undef	ISREAD
853 #undef	ISWRITE
854 		/*
855 		 * Do verbose interpretation if requested.
856 		 * If buffer contents for read or write have been requested and
857 		 * this is a readv() or writev(), force verbose interpretation.
858 		 */
859 		if (prismember(&verbose, what) ||
860 		    ((what == SYS_readv || what == SYS_recvmsg) &&
861 		    pri->Errno == 0 && prismember(&readfd, fdp1)) ||
862 		    ((what == SYS_writev || what == SYS_sendfilev ||
863 		    what == SYS_sendmsg) &&
864 		    prismember(&writefd, fdp1)))
865 			expound(pri, pri->Rval1, raw);
866 	}
867 
868 	return (!cflag && istraced);
869 }
870 
871 void
872 showpaths(private_t *pri, const struct systable *stp)
873 {
874 	int what = pri->lwpstat->pr_what;
875 	int i;
876 
877 	for (i = 0; i < pri->sys_nargs; i++) {
878 		if (stp->arg[i] == ATC && (int)pri->sys_args[i] == AT_FDCWD) {
879 			(void) printf("%s     0x%.8X: AT_FDCWD\n",
880 			    pri->pname, AT_FDCWD);
881 		} else if ((stp->arg[i] == STG) ||
882 		    (stp->arg[i] == RST && !pri->Errno) ||
883 		    (stp->arg[i] == RLK && !pri->Errno && pri->Rval1 > 0)) {
884 			long addr = pri->sys_args[i];
885 			int maxleng =
886 			    (stp->arg[i] == RLK)? (int)pri->Rval1 : PATH_MAX;
887 			char *s;
888 
889 			if (pri->sys_valid &&
890 			    ((i == 0 && stp->arg[0] == STG) ||
891 			    (i == 1 && (what == SYS_openat ||
892 			    what == SYS_openat64))))	/* already fetched */
893 				s = pri->sys_path;
894 			else
895 				s = fetchstring(pri, addr,
896 				    maxleng > PATH_MAX ? PATH_MAX : maxleng);
897 
898 			if (s != (char *)NULL)
899 				(void) printf("%s     0x%.8lX: \"%s\"\n",
900 				    pri->pname, addr, s);
901 		}
902 	}
903 }
904 
905 /*
906  * Display arguments to successful exec().
907  */
908 void
909 showargs(private_t *pri, int raw)
910 {
911 	const lwpstatus_t *Lsp = pri->lwpstat;
912 	int nargs;
913 	long ap;
914 	int ptrsize;
915 	int fail;
916 
917 	pri->length = 0;
918 	ptrsize = (data_model == PR_MODEL_LP64)? 8 : 4;
919 
920 #if defined(__i386) || defined(__amd64)	/* XX64 */
921 	ap = (long)Lsp->pr_reg[R_SP];
922 	fail = (Pread(Proc, &nargs, sizeof (nargs), ap) != sizeof (nargs));
923 	ap += ptrsize;
924 #endif /* i386 */
925 
926 #if sparc
927 	if (data_model == PR_MODEL_LP64) {
928 		int64_t xnargs;
929 		ap = (long)(Lsp->pr_reg[R_SP]) + 16 * sizeof (int64_t)
930 		    + STACK_BIAS;
931 		fail = (Pread(Proc, &xnargs, sizeof (xnargs), ap) !=
932 		    sizeof (xnargs));
933 		nargs = (int)xnargs;
934 	} else {
935 		ap = (long)(Lsp->pr_reg[R_SP]) + 16 * sizeof (int32_t);
936 		fail = (Pread(Proc, &nargs, sizeof (nargs), ap) !=
937 		    sizeof (nargs));
938 	}
939 	ap += ptrsize;
940 #endif /* sparc */
941 
942 	if (fail) {
943 		(void) printf("\n%s\t*** Bad argument list? ***\n", pri->pname);
944 		return;
945 	}
946 
947 	(void) printf("  argc = %d\n", nargs);
948 	if (raw)
949 		showpaths(pri, &systable[SYS_execve]);
950 
951 	show_cred(pri, FALSE, FALSE);
952 
953 	if (aflag || eflag) {		/* dump args or environment */
954 
955 		/* enter region of (potentially) lengthy output */
956 		Eserialize();
957 
958 		if (aflag)		/* dump the argument list */
959 			dumpargs(pri, ap, "argv:");
960 		ap += (nargs+1) * ptrsize;
961 		if (eflag)		/* dump the environment */
962 			dumpargs(pri, ap, "envp:");
963 
964 		/* exit region of lengthy output */
965 		Xserialize();
966 	}
967 }
968 
969 void
970 dumpargs(private_t *pri, long ap, const char *str)
971 {
972 	char *string;
973 	unsigned int leng = 0;
974 	int ptrsize;
975 	long arg = 0;
976 	char *argaddr;
977 	char badaddr[32];
978 
979 	if (interrupt)
980 		return;
981 
982 	if (data_model == PR_MODEL_LP64) {
983 		argaddr = (char *)&arg;
984 		ptrsize = 8;
985 	} else {
986 #if defined(_LITTLE_ENDIAN)
987 		argaddr = (char *)&arg;
988 #else
989 		argaddr = (char *)&arg + 4;
990 #endif
991 		ptrsize = 4;
992 	}
993 	putpname(pri);
994 	(void) fputc(' ', stdout);
995 	(void) fputs(str, stdout);
996 	leng += 1 + strlen(str);
997 
998 	while (!interrupt) {
999 		if (Pread(Proc, argaddr, ptrsize, ap) != ptrsize) {
1000 			(void) printf("\n%s\t*** Bad argument list? ***\n",
1001 			    pri->pname);
1002 			return;
1003 		}
1004 		ap += ptrsize;
1005 
1006 		if (arg == 0)
1007 			break;
1008 		string = fetchstring(pri, arg, PATH_MAX);
1009 		if (string == NULL) {
1010 			(void) sprintf(badaddr, "BadAddress:0x%.8lX", arg);
1011 			string = badaddr;
1012 		}
1013 		if ((leng += strlen(string)) < 63) {
1014 			(void) fputc(' ', stdout);
1015 			leng++;
1016 		} else {
1017 			(void) fputc('\n', stdout);
1018 			leng = 0;
1019 			putpname(pri);
1020 			(void) fputs("  ", stdout);
1021 			leng += 2 + strlen(string);
1022 		}
1023 		(void) fputs(string, stdout);
1024 	}
1025 	(void) fputc('\n', stdout);
1026 }
1027 
1028 /*
1029  * Display contents of read() or write() buffer.
1030  */
1031 void
1032 showbuffer(private_t *pri, long offset, long count)
1033 {
1034 	char buffer[320];
1035 	int nbytes;
1036 	char *buf;
1037 	int n;
1038 
1039 	while (count > 0 && !interrupt) {
1040 		nbytes = (count < sizeof (buffer))? count : sizeof (buffer);
1041 		if ((nbytes = Pread(Proc, buffer, nbytes, offset)) <= 0)
1042 			break;
1043 		count -= nbytes;
1044 		offset += nbytes;
1045 		buf = buffer;
1046 		while (nbytes > 0 && !interrupt) {
1047 			char obuf[65];
1048 
1049 			n = (nbytes < 32)? nbytes : 32;
1050 			showbytes(buf, n, obuf);
1051 
1052 			putpname(pri);
1053 			(void) fputs("  ", stdout);
1054 			(void) fputs(obuf, stdout);
1055 			(void) fputc('\n', stdout);
1056 			nbytes -= n;
1057 			buf += n;
1058 		}
1059 	}
1060 }
1061 
1062 void
1063 showbytes(const char *buf, int n, char *obuf)
1064 {
1065 	int c;
1066 
1067 	while (--n >= 0) {
1068 		int c1 = '\\';
1069 		int c2;
1070 
1071 		switch (c = (*buf++ & 0xff)) {
1072 		case '\0':
1073 			c2 = '0';
1074 			break;
1075 		case '\b':
1076 			c2 = 'b';
1077 			break;
1078 		case '\t':
1079 			c2 = 't';
1080 			break;
1081 		case '\n':
1082 			c2 = 'n';
1083 			break;
1084 		case '\v':
1085 			c2 = 'v';
1086 			break;
1087 		case '\f':
1088 			c2 = 'f';
1089 			break;
1090 		case '\r':
1091 			c2 = 'r';
1092 			break;
1093 		default:
1094 			if (isprint(c)) {
1095 				c1 = ' ';
1096 				c2 = c;
1097 			} else {
1098 				c1 = c>>4;
1099 				c1 += (c1 < 10)? '0' : 'A'-10;
1100 				c2 = c&0xf;
1101 				c2 += (c2 < 10)? '0' : 'A'-10;
1102 			}
1103 			break;
1104 		}
1105 		*obuf++ = (char)c1;
1106 		*obuf++ = (char)c2;
1107 	}
1108 
1109 	*obuf = '\0';
1110 }
1111