syscall.c revision ee348c60735e35730c3e95b89a4dd89bbcb4e7b5
1/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 *                     Linux for s390 port by D.J. Barrow
8 *                    <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include "defs.h"
35#include <sys/user.h>
36#include <sys/param.h>
37
38#ifdef HAVE_SYS_REG_H
39# include <sys/reg.h>
40# ifndef PTRACE_PEEKUSR
41#  define PTRACE_PEEKUSR PTRACE_PEEKUSER
42# endif
43#elif defined(HAVE_LINUX_PTRACE_H)
44# undef PTRACE_SYSCALL
45# ifdef HAVE_STRUCT_IA64_FPREG
46#  define ia64_fpreg XXX_ia64_fpreg
47# endif
48# ifdef HAVE_STRUCT_PT_ALL_USER_REGS
49#  define pt_all_user_regs XXX_pt_all_user_regs
50# endif
51# include <linux/ptrace.h>
52# undef ia64_fpreg
53# undef pt_all_user_regs
54#endif
55
56#if defined(SPARC64)
57# undef PTRACE_GETREGS
58# define PTRACE_GETREGS PTRACE_GETREGS64
59# undef PTRACE_SETREGS
60# define PTRACE_SETREGS PTRACE_SETREGS64
61#endif
62
63#if defined(IA64)
64# include <asm/ptrace_offsets.h>
65# include <asm/rse.h>
66#endif
67
68#if defined(X86_64) || defined(X32)
69# include <sys/uio.h>
70# include <elf.h>
71#endif
72
73#if defined(AARCH64)
74# include <asm/ptrace.h>
75# include <sys/uio.h>
76# include <elf.h>
77#endif
78
79#if defined(OR1K)
80# include <sys/uio.h>
81# include <elf.h>
82#endif
83
84#if defined(METAG)
85# include <sys/uio.h>
86# include <elf.h>
87#endif
88
89#ifndef ERESTARTSYS
90# define ERESTARTSYS	512
91#endif
92#ifndef ERESTARTNOINTR
93# define ERESTARTNOINTR	513
94#endif
95#ifndef ERESTARTNOHAND
96# define ERESTARTNOHAND	514	/* restart if no handler */
97#endif
98#ifndef ERESTART_RESTARTBLOCK
99# define ERESTART_RESTARTBLOCK 516	/* restart by calling sys_restart_syscall */
100#endif
101
102#ifndef NSIG
103# warning: NSIG is not defined, using 32
104# define NSIG 32
105#endif
106#ifdef ARM
107/* Ugh. Is this really correct? ARM has no RT signals?! */
108# undef NSIG
109# define NSIG 32
110#endif
111
112#include "syscall.h"
113
114/* Define these shorthand notations to simplify the syscallent files. */
115#define TD TRACE_DESC
116#define TF TRACE_FILE
117#define TI TRACE_IPC
118#define TN TRACE_NETWORK
119#define TP TRACE_PROCESS
120#define TS TRACE_SIGNAL
121#define TM TRACE_MEMORY
122#define NF SYSCALL_NEVER_FAILS
123#define MA MAX_ARGS
124
125const struct_sysent sysent0[] = {
126#include "syscallent.h"
127};
128
129#if SUPPORTED_PERSONALITIES > 1
130static const struct_sysent sysent1[] = {
131# include "syscallent1.h"
132};
133#endif
134
135#if SUPPORTED_PERSONALITIES > 2
136static const struct_sysent sysent2[] = {
137# include "syscallent2.h"
138};
139#endif
140
141/* Now undef them since short defines cause wicked namespace pollution. */
142#undef TD
143#undef TF
144#undef TI
145#undef TN
146#undef TP
147#undef TS
148#undef TM
149#undef NF
150#undef MA
151
152/*
153 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
154 * program `ioctlsort', such that the list is sorted by the `code' field.
155 * This has the side-effect of resolving the _IO.. macros into
156 * plain integers, eliminating the need to include here everything
157 * in "/usr/include".
158 */
159
160const char *const errnoent0[] = {
161#include "errnoent.h"
162};
163const char *const signalent0[] = {
164#include "signalent.h"
165};
166const struct_ioctlent ioctlent0[] = {
167#include "ioctlent.h"
168};
169
170#if SUPPORTED_PERSONALITIES > 1
171static const char *const errnoent1[] = {
172# include "errnoent1.h"
173};
174static const char *const signalent1[] = {
175# include "signalent1.h"
176};
177static const struct_ioctlent ioctlent1[] = {
178# include "ioctlent1.h"
179};
180#endif
181
182#if SUPPORTED_PERSONALITIES > 2
183static const char *const errnoent2[] = {
184# include "errnoent2.h"
185};
186static const char *const signalent2[] = {
187# include "signalent2.h"
188};
189static const struct_ioctlent ioctlent2[] = {
190# include "ioctlent2.h"
191};
192#endif
193
194enum {
195	nsyscalls0 = ARRAY_SIZE(sysent0)
196#if SUPPORTED_PERSONALITIES > 1
197	, nsyscalls1 = ARRAY_SIZE(sysent1)
198# if SUPPORTED_PERSONALITIES > 2
199	, nsyscalls2 = ARRAY_SIZE(sysent2)
200# endif
201#endif
202};
203
204enum {
205	nerrnos0 = ARRAY_SIZE(errnoent0)
206#if SUPPORTED_PERSONALITIES > 1
207	, nerrnos1 = ARRAY_SIZE(errnoent1)
208# if SUPPORTED_PERSONALITIES > 2
209	, nerrnos2 = ARRAY_SIZE(errnoent2)
210# endif
211#endif
212};
213
214enum {
215	nsignals0 = ARRAY_SIZE(signalent0)
216#if SUPPORTED_PERSONALITIES > 1
217	, nsignals1 = ARRAY_SIZE(signalent1)
218# if SUPPORTED_PERSONALITIES > 2
219	, nsignals2 = ARRAY_SIZE(signalent2)
220# endif
221#endif
222};
223
224enum {
225	nioctlents0 = ARRAY_SIZE(ioctlent0)
226#if SUPPORTED_PERSONALITIES > 1
227	, nioctlents1 = ARRAY_SIZE(ioctlent1)
228# if SUPPORTED_PERSONALITIES > 2
229	, nioctlents2 = ARRAY_SIZE(ioctlent2)
230# endif
231#endif
232};
233
234#if SUPPORTED_PERSONALITIES > 1
235const struct_sysent *sysent = sysent0;
236const char *const *errnoent = errnoent0;
237const char *const *signalent = signalent0;
238const struct_ioctlent *ioctlent = ioctlent0;
239#endif
240unsigned nsyscalls = nsyscalls0;
241unsigned nerrnos = nerrnos0;
242unsigned nsignals = nsignals0;
243unsigned nioctlents = nioctlents0;
244
245unsigned num_quals;
246qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
247
248static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
249	nsyscalls0,
250#if SUPPORTED_PERSONALITIES > 1
251	nsyscalls1,
252#endif
253#if SUPPORTED_PERSONALITIES > 2
254	nsyscalls2,
255#endif
256};
257static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
258	sysent0,
259#if SUPPORTED_PERSONALITIES > 1
260	sysent1,
261#endif
262#if SUPPORTED_PERSONALITIES > 2
263	sysent2,
264#endif
265};
266
267enum {
268	MAX_NSYSCALLS1 = (nsyscalls0
269#if SUPPORTED_PERSONALITIES > 1
270			> nsyscalls1 ? nsyscalls0 : nsyscalls1
271#endif
272			),
273	MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
274#if SUPPORTED_PERSONALITIES > 2
275			> nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
276#endif
277			),
278	MAX_NSYSCALLS = MAX_NSYSCALLS2,
279	/* We are ready for arches with up to 255 signals,
280	 * even though the largest known signo is on MIPS and it is 128.
281	 * The number of existing syscalls on all arches is
282	 * larger that 255 anyway, so it is just a pedantic matter.
283	 */
284	MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
285};
286
287#if SUPPORTED_PERSONALITIES > 1
288unsigned current_personality;
289
290# ifndef current_wordsize
291unsigned current_wordsize;
292static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
293	PERSONALITY0_WORDSIZE,
294	PERSONALITY1_WORDSIZE,
295# if SUPPORTED_PERSONALITIES > 2
296	PERSONALITY2_WORDSIZE,
297# endif
298};
299# endif
300
301void
302set_personality(int personality)
303{
304	nsyscalls = nsyscall_vec[personality];
305	sysent = sysent_vec[personality];
306
307	switch (personality) {
308	case 0:
309		errnoent = errnoent0;
310		nerrnos = nerrnos0;
311		ioctlent = ioctlent0;
312		nioctlents = nioctlents0;
313		signalent = signalent0;
314		nsignals = nsignals0;
315		break;
316
317	case 1:
318		errnoent = errnoent1;
319		nerrnos = nerrnos1;
320		ioctlent = ioctlent1;
321		nioctlents = nioctlents1;
322		signalent = signalent1;
323		nsignals = nsignals1;
324		break;
325
326# if SUPPORTED_PERSONALITIES > 2
327	case 2:
328		errnoent = errnoent2;
329		nerrnos = nerrnos2;
330		ioctlent = ioctlent2;
331		nioctlents = nioctlents2;
332		signalent = signalent2;
333		nsignals = nsignals2;
334		break;
335# endif
336	}
337
338	current_personality = personality;
339# ifndef current_wordsize
340	current_wordsize = personality_wordsize[personality];
341# endif
342}
343
344static void
345update_personality(struct tcb *tcp, int personality)
346{
347	if (personality == current_personality)
348		return;
349	set_personality(personality);
350
351	if (personality == tcp->currpers)
352		return;
353	tcp->currpers = personality;
354
355# if defined(POWERPC64)
356	if (!qflag) {
357		static const char *const names[] = {"64 bit", "32 bit"};
358		fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
359			tcp->pid, names[personality]);
360	}
361# elif defined(X86_64)
362	if (!qflag) {
363		static const char *const names[] = {"64 bit", "32 bit", "x32"};
364		fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
365			tcp->pid, names[personality]);
366	}
367# elif defined(X32)
368	if (!qflag) {
369		static const char *const names[] = {"x32", "32 bit"};
370		fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
371			tcp->pid, names[personality]);
372	}
373# elif defined(AARCH64)
374	if (!qflag) {
375		static const char *const names[] = {"32-bit", "AArch64"};
376		fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
377			tcp->pid, names[personality]);
378	}
379# elif defined(TILE)
380	if (!qflag) {
381		static const char *const names[] = {"64-bit", "32-bit"};
382		fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
383			tcp->pid, names[personality]);
384	}
385# endif
386}
387#endif
388
389static int qual_syscall(), qual_signal(), qual_desc();
390
391static const struct qual_options {
392	int bitflag;
393	const char *option_name;
394	int (*qualify)(const char *, int, int);
395	const char *argument_name;
396} qual_options[] = {
397	{ QUAL_TRACE,	"trace",	qual_syscall,	"system call"	},
398	{ QUAL_TRACE,	"t",		qual_syscall,	"system call"	},
399	{ QUAL_ABBREV,	"abbrev",	qual_syscall,	"system call"	},
400	{ QUAL_ABBREV,	"a",		qual_syscall,	"system call"	},
401	{ QUAL_VERBOSE,	"verbose",	qual_syscall,	"system call"	},
402	{ QUAL_VERBOSE,	"v",		qual_syscall,	"system call"	},
403	{ QUAL_RAW,	"raw",		qual_syscall,	"system call"	},
404	{ QUAL_RAW,	"x",		qual_syscall,	"system call"	},
405	{ QUAL_SIGNAL,	"signal",	qual_signal,	"signal"	},
406	{ QUAL_SIGNAL,	"signals",	qual_signal,	"signal"	},
407	{ QUAL_SIGNAL,	"s",		qual_signal,	"signal"	},
408	{ QUAL_READ,	"read",		qual_desc,	"descriptor"	},
409	{ QUAL_READ,	"reads",	qual_desc,	"descriptor"	},
410	{ QUAL_READ,	"r",		qual_desc,	"descriptor"	},
411	{ QUAL_WRITE,	"write",	qual_desc,	"descriptor"	},
412	{ QUAL_WRITE,	"writes",	qual_desc,	"descriptor"	},
413	{ QUAL_WRITE,	"w",		qual_desc,	"descriptor"	},
414	{ 0,		NULL,		NULL,		NULL		},
415};
416
417static void
418reallocate_qual(int n)
419{
420	unsigned p;
421	qualbits_t *qp;
422	for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
423		qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
424		if (!qp)
425			die_out_of_memory();
426		memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
427	}
428	num_quals = n;
429}
430
431static void
432qualify_one(int n, int bitflag, int not, int pers)
433{
434	unsigned p;
435
436	if (num_quals <= n)
437		reallocate_qual(n + 1);
438
439	for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
440		if (pers == p || pers < 0) {
441			if (not)
442				qual_vec[p][n] &= ~bitflag;
443			else
444				qual_vec[p][n] |= bitflag;
445		}
446	}
447}
448
449static int
450qual_syscall(const char *s, int bitflag, int not)
451{
452	unsigned p;
453	unsigned i;
454	int rc = -1;
455
456	if (*s >= '0' && *s <= '9') {
457		i = string_to_uint(s);
458		if (i >= MAX_NSYSCALLS)
459			return -1;
460		qualify_one(i, bitflag, not, -1);
461		return 0;
462	}
463
464	for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
465		for (i = 0; i < nsyscall_vec[p]; i++) {
466			if (sysent_vec[p][i].sys_name
467			 && strcmp(s, sysent_vec[p][i].sys_name) == 0
468			) {
469				qualify_one(i, bitflag, not, p);
470				rc = 0;
471			}
472		}
473	}
474
475	return rc;
476}
477
478static int
479qual_signal(const char *s, int bitflag, int not)
480{
481	int i;
482
483	if (*s >= '0' && *s <= '9') {
484		int signo = string_to_uint(s);
485		if (signo < 0 || signo > 255)
486			return -1;
487		qualify_one(signo, bitflag, not, -1);
488		return 0;
489	}
490	if (strncasecmp(s, "SIG", 3) == 0)
491		s += 3;
492	for (i = 0; i <= NSIG; i++) {
493		if (strcasecmp(s, signame(i) + 3) == 0) {
494			qualify_one(i, bitflag, not, -1);
495			return 0;
496		}
497	}
498	return -1;
499}
500
501static int
502qual_desc(const char *s, int bitflag, int not)
503{
504	if (*s >= '0' && *s <= '9') {
505		int desc = string_to_uint(s);
506		if (desc < 0 || desc > 0x7fff) /* paranoia */
507			return -1;
508		qualify_one(desc, bitflag, not, -1);
509		return 0;
510	}
511	return -1;
512}
513
514static int
515lookup_class(const char *s)
516{
517	if (strcmp(s, "file") == 0)
518		return TRACE_FILE;
519	if (strcmp(s, "ipc") == 0)
520		return TRACE_IPC;
521	if (strcmp(s, "network") == 0)
522		return TRACE_NETWORK;
523	if (strcmp(s, "process") == 0)
524		return TRACE_PROCESS;
525	if (strcmp(s, "signal") == 0)
526		return TRACE_SIGNAL;
527	if (strcmp(s, "desc") == 0)
528		return TRACE_DESC;
529	if (strcmp(s, "memory") == 0)
530		return TRACE_MEMORY;
531	return -1;
532}
533
534void
535qualify(const char *s)
536{
537	const struct qual_options *opt;
538	int not;
539	char *copy;
540	const char *p;
541	int i, n;
542
543	if (num_quals == 0)
544		reallocate_qual(MIN_QUALS);
545
546	opt = &qual_options[0];
547	for (i = 0; (p = qual_options[i].option_name); i++) {
548		n = strlen(p);
549		if (strncmp(s, p, n) == 0 && s[n] == '=') {
550			opt = &qual_options[i];
551			s += n + 1;
552			break;
553		}
554	}
555	not = 0;
556	if (*s == '!') {
557		not = 1;
558		s++;
559	}
560	if (strcmp(s, "none") == 0) {
561		not = 1 - not;
562		s = "all";
563	}
564	if (strcmp(s, "all") == 0) {
565		for (i = 0; i < num_quals; i++) {
566			qualify_one(i, opt->bitflag, not, -1);
567		}
568		return;
569	}
570	for (i = 0; i < num_quals; i++) {
571		qualify_one(i, opt->bitflag, !not, -1);
572	}
573	copy = strdup(s);
574	if (!copy)
575		die_out_of_memory();
576	for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
577		if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
578			unsigned pers;
579			for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
580				for (i = 0; i < nsyscall_vec[pers]; i++)
581					if (sysent_vec[pers][i].sys_flags & n)
582						qualify_one(i, opt->bitflag, not, pers);
583			}
584			continue;
585		}
586		if (opt->qualify(p, opt->bitflag, not)) {
587			error_msg_and_die("invalid %s '%s'",
588				opt->argument_name, p);
589		}
590	}
591	free(copy);
592	return;
593}
594
595#ifdef SYS_socket_subcall
596static void
597decode_socket_subcall(struct tcb *tcp)
598{
599	unsigned long addr;
600	unsigned int i, n, size;
601
602	if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
603		return;
604
605	tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
606	tcp->qual_flg = qual_flags[tcp->scno];
607	tcp->s_ent = &sysent[tcp->scno];
608	addr = tcp->u_arg[1];
609	size = current_wordsize;
610	n = tcp->s_ent->nargs;
611	for (i = 0; i < n; ++i) {
612		if (size == sizeof(int)) {
613			unsigned int arg;
614			if (umove(tcp, addr, &arg) < 0)
615				arg = 0;
616			tcp->u_arg[i] = arg;
617		}
618		else {
619			unsigned long arg;
620			if (umove(tcp, addr, &arg) < 0)
621				arg = 0;
622			tcp->u_arg[i] = arg;
623		}
624		addr += size;
625	}
626}
627#endif
628
629#ifdef SYS_ipc_subcall
630static void
631decode_ipc_subcall(struct tcb *tcp)
632{
633	unsigned int i, n;
634
635	if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
636		return;
637
638	tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
639	tcp->qual_flg = qual_flags[tcp->scno];
640	tcp->s_ent = &sysent[tcp->scno];
641	n = tcp->s_ent->nargs;
642	for (i = 0; i < n; i++)
643		tcp->u_arg[i] = tcp->u_arg[i + 1];
644}
645#endif
646
647int
648printargs(struct tcb *tcp)
649{
650	if (entering(tcp)) {
651		int i;
652		int n = tcp->s_ent->nargs;
653		for (i = 0; i < n; i++)
654			tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
655	}
656	return 0;
657}
658
659int
660printargs_lu(struct tcb *tcp)
661{
662	if (entering(tcp)) {
663		int i;
664		int n = tcp->s_ent->nargs;
665		for (i = 0; i < n; i++)
666			tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
667	}
668	return 0;
669}
670
671int
672printargs_ld(struct tcb *tcp)
673{
674	if (entering(tcp)) {
675		int i;
676		int n = tcp->s_ent->nargs;
677		for (i = 0; i < n; i++)
678			tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
679	}
680	return 0;
681}
682
683#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
684long
685getrval2(struct tcb *tcp)
686{
687	long val;
688
689# if defined(SPARC) || defined(SPARC64)
690	val = sparc_regs.u_regs[U_REG_O1];
691# elif defined(SH)
692	if (upeek(tcp, 4*(REG_REG0+1), &val) < 0)
693		return -1;
694# elif defined(IA64)
695	if (upeek(tcp, PT_R9, &val) < 0)
696		return -1;
697# endif
698
699	return val;
700}
701#endif
702
703int
704is_restart_error(struct tcb *tcp)
705{
706	switch (tcp->u_error) {
707		case ERESTARTSYS:
708		case ERESTARTNOINTR:
709		case ERESTARTNOHAND:
710		case ERESTART_RESTARTBLOCK:
711			return 1;
712		default:
713			break;
714	}
715	return 0;
716}
717
718#if defined(I386)
719struct user_regs_struct i386_regs;
720#elif defined(X86_64) || defined(X32)
721/*
722 * On i386, pt_regs and user_regs_struct are the same,
723 * but on 64 bit x86, user_regs_struct has six more fields:
724 * fs_base, gs_base, ds, es, fs, gs.
725 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
726 */
727struct i386_user_regs_struct {
728	uint32_t ebx;
729	uint32_t ecx;
730	uint32_t edx;
731	uint32_t esi;
732	uint32_t edi;
733	uint32_t ebp;
734	uint32_t eax;
735	uint32_t xds;
736	uint32_t xes;
737	uint32_t xfs;
738	uint32_t xgs;
739	uint32_t orig_eax;
740	uint32_t eip;
741	uint32_t xcs;
742	uint32_t eflags;
743	uint32_t esp;
744	uint32_t xss;
745};
746static union {
747	struct user_regs_struct      x86_64_r;
748	struct i386_user_regs_struct i386_r;
749} x86_regs_union;
750# define x86_64_regs x86_regs_union.x86_64_r
751# define i386_regs   x86_regs_union.i386_r
752static struct iovec x86_io = {
753	.iov_base = &x86_regs_union
754};
755#elif defined(IA64)
756long ia32 = 0; /* not static */
757static long ia64_r8, ia64_r10;
758#elif defined(POWERPC)
759static long ppc_result;
760#elif defined(M68K)
761static long m68k_d0;
762#elif defined(BFIN)
763static long bfin_r0;
764#elif defined(ARM)
765struct pt_regs arm_regs; /* not static */
766#elif defined(AARCH64)
767static union {
768	struct user_pt_regs aarch64_r;
769	struct arm_pt_regs  arm_r;
770} arm_regs_union;
771# define aarch64_regs arm_regs_union.aarch64_r
772# define arm_regs     arm_regs_union.arm_r
773static struct iovec aarch64_io = {
774	.iov_base = &arm_regs_union
775};
776#elif defined(ALPHA)
777static long alpha_r0;
778static long alpha_a3;
779#elif defined(AVR32)
780static struct pt_regs avr32_regs;
781#elif defined(SPARC) || defined(SPARC64)
782struct pt_regs sparc_regs; /* not static */
783#elif defined(LINUX_MIPSN32)
784static long long mips_a3;
785static long long mips_r2;
786#elif defined(MIPS)
787static long mips_a3;
788static long mips_r2;
789#elif defined(S390) || defined(S390X)
790static long gpr2;
791static long syscall_mode;
792#elif defined(HPPA)
793static long hppa_r28;
794#elif defined(SH)
795static long sh_r0;
796#elif defined(SH64)
797static long sh64_r9;
798#elif defined(CRISV10) || defined(CRISV32)
799static long cris_r10;
800#elif defined(TILE)
801struct pt_regs tile_regs;
802#elif defined(MICROBLAZE)
803static long microblaze_r3;
804#elif defined(OR1K)
805static struct user_regs_struct or1k_regs;
806static struct iovec or1k_io = {
807	.iov_base = &or1k_regs
808};
809#elif defined(METAG)
810static struct user_gp_regs metag_regs;
811static struct iovec metag_io = {
812	.iov_base = &metag_regs
813};
814#endif
815
816void
817printcall(struct tcb *tcp)
818{
819#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
820			   sizeof(long) == 8 ? "[????????????????] " : \
821			   NULL /* crash */)
822	if (get_regs_error) {
823		PRINTBADPC;
824		return;
825	}
826#if defined(I386)
827	tprintf("[%08lx] ", i386_regs.eip);
828#elif defined(S390) || defined(S390X)
829	long psw;
830	if (upeek(tcp, PT_PSWADDR, &psw) < 0) {
831		PRINTBADPC;
832		return;
833	}
834# ifdef S390
835	tprintf("[%08lx] ", psw);
836# elif S390X
837	tprintf("[%016lx] ", psw);
838# endif
839#elif defined(X86_64) || defined(X32)
840	if (x86_io.iov_len == sizeof(i386_regs)) {
841		tprintf("[%08x] ", (unsigned) i386_regs.eip);
842	} else {
843# if defined(X86_64)
844		tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
845# elif defined(X32)
846		/* Note: this truncates 64-bit rip to 32 bits */
847		tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
848# endif
849	}
850#elif defined(IA64)
851	long ip;
852	if (upeek(tcp, PT_B0, &ip) < 0) {
853		PRINTBADPC;
854		return;
855	}
856	tprintf("[%08lx] ", ip);
857#elif defined(POWERPC)
858	long pc;
859	if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
860		PRINTBADPC;
861		return;
862	}
863# ifdef POWERPC64
864	tprintf("[%016lx] ", pc);
865# else
866	tprintf("[%08lx] ", pc);
867# endif
868#elif defined(M68K)
869	long pc;
870	if (upeek(tcp, 4*PT_PC, &pc) < 0) {
871		tprints("[????????] ");
872		return;
873	}
874	tprintf("[%08lx] ", pc);
875#elif defined(ALPHA)
876	long pc;
877	if (upeek(tcp, REG_PC, &pc) < 0) {
878		tprints("[????????????????] ");
879		return;
880	}
881	tprintf("[%08lx] ", pc);
882#elif defined(SPARC)
883	tprintf("[%08lx] ", sparc_regs.pc);
884#elif defined(SPARC64)
885	tprintf("[%08lx] ", sparc_regs.tpc);
886#elif defined(HPPA)
887	long pc;
888	if (upeek(tcp, PT_IAOQ0, &pc) < 0) {
889		tprints("[????????] ");
890		return;
891	}
892	tprintf("[%08lx] ", pc);
893#elif defined(MIPS)
894	long pc;
895	if (upeek(tcp, REG_EPC, &pc) < 0) {
896		tprints("[????????] ");
897		return;
898	}
899	tprintf("[%08lx] ", pc);
900#elif defined(SH)
901	long pc;
902	if (upeek(tcp, 4*REG_PC, &pc) < 0) {
903		tprints("[????????] ");
904		return;
905	}
906	tprintf("[%08lx] ", pc);
907#elif defined(SH64)
908	long pc;
909	if (upeek(tcp, REG_PC, &pc) < 0) {
910		tprints("[????????????????] ");
911		return;
912	}
913	tprintf("[%08lx] ", pc);
914#elif defined(ARM)
915	tprintf("[%08lx] ", arm_regs.ARM_pc);
916#elif defined(AARCH64)
917	/* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
918#elif defined(AVR32)
919	tprintf("[%08lx] ", avr32_regs.pc);
920#elif defined(BFIN)
921	long pc;
922	if (upeek(tcp, PT_PC, &pc) < 0) {
923		PRINTBADPC;
924		return;
925	}
926	tprintf("[%08lx] ", pc);
927#elif defined(CRISV10)
928	long pc;
929	if (upeek(tcp, 4*PT_IRP, &pc) < 0) {
930		PRINTBADPC;
931		return;
932	}
933	tprintf("[%08lx] ", pc);
934#elif defined(CRISV32)
935	long pc;
936	if (upeek(tcp, 4*PT_ERP, &pc) < 0) {
937		PRINTBADPC;
938		return;
939	}
940	tprintf("[%08lx] ", pc);
941#elif defined(TILE)
942# ifdef _LP64
943	tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
944# else
945	tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
946# endif
947#elif defined(OR1K)
948	tprintf("[%08lx] ", or1k_regs.pc);
949#elif defined(METAG)
950	tprintf("[%08lx] ", metag_regs.pc);
951#endif /* architecture */
952}
953
954/* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
955 * The shuffling should be reversible: shuffle_scno(shuffle_scno(n)) == n.
956 */
957#if defined(ARM) /* So far only ARM needs this */
958static long
959shuffle_scno(unsigned long scno)
960{
961	if (scno <= ARM_LAST_ORDINARY_SYSCALL)
962		return scno;
963
964	/* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
965	if (scno == 0x000ffff0)
966		return ARM_LAST_ORDINARY_SYSCALL+1;
967	if (scno == ARM_LAST_ORDINARY_SYSCALL+1)
968		return 0x000ffff0;
969
970	/* Is it ARM specific syscall?
971	 * Swap with [LAST_ORDINARY+2, LAST_ORDINARY+2 + LAST_SPECIAL] range.
972	 */
973	if (scno >= 0x000f0000
974	 && scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL
975	) {
976		return scno - 0x000f0000 + (ARM_LAST_ORDINARY_SYSCALL+2);
977	}
978	if (/* scno >= ARM_LAST_ORDINARY_SYSCALL+2 - always true */ 1
979	 && scno <= (ARM_LAST_ORDINARY_SYSCALL+2) + ARM_LAST_SPECIAL_SYSCALL
980	) {
981		return scno + 0x000f0000 - (ARM_LAST_ORDINARY_SYSCALL+2);
982	}
983
984	return scno;
985}
986#else
987# define shuffle_scno(scno) ((long)(scno))
988#endif
989
990static char*
991undefined_scno_name(struct tcb *tcp)
992{
993	static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
994
995	sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
996	return buf;
997}
998
999#ifndef get_regs
1000long get_regs_error;
1001void
1002get_regs(pid_t pid)
1003{
1004# if defined(AVR32)
1005	get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
1006# elif defined(I386)
1007	get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &i386_regs);
1008# elif defined(X32)
1009	/* x86_io.iov_base = &x86_regs_union; - already is */
1010	x86_io.iov_len = sizeof(x86_regs_union);
1011	get_regs_error = ptrace(PTRACE_GETREGSET, pid,
1012				NT_PRSTATUS, (long) &x86_io);
1013# elif defined(X86_64)
1014	/*
1015	 * PTRACE_GETREGSET was introduced in 2.6.33.
1016	 * Let's be paranoid and require a bit later kernel.
1017	 */
1018	if (os_release >= KERNEL_VERSION(2,6,35)) {
1019		/*x86_io.iov_base = &x86_regs_union; - already is */
1020		x86_io.iov_len = sizeof(x86_regs_union);
1021		get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (long) &x86_io);
1022	} else {
1023		/* Use old method, with heuristical detection of 32-bitness */
1024		x86_io.iov_len = sizeof(x86_64_regs);
1025		get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &x86_64_regs);
1026		if (!get_regs_error && x86_64_regs.cs == 0x23) {
1027			x86_io.iov_len = sizeof(i386_regs);
1028			/*
1029			 * The order is important: i386_regs and x86_64_regs
1030			 * are overlaid in memory!
1031			 */
1032			i386_regs.ebx = x86_64_regs.rbx;
1033			i386_regs.ecx = x86_64_regs.rcx;
1034			i386_regs.edx = x86_64_regs.rdx;
1035			i386_regs.esi = x86_64_regs.rsi;
1036			i386_regs.edi = x86_64_regs.rdi;
1037			i386_regs.ebp = x86_64_regs.rbp;
1038			i386_regs.eax = x86_64_regs.rax;
1039			/*i386_regs.xds = x86_64_regs.ds; unused by strace */
1040			/*i386_regs.xes = x86_64_regs.es; ditto... */
1041			/*i386_regs.xfs = x86_64_regs.fs;*/
1042			/*i386_regs.xgs = x86_64_regs.gs;*/
1043			i386_regs.orig_eax = x86_64_regs.orig_rax;
1044			i386_regs.eip = x86_64_regs.rip;
1045			/*i386_regs.xcs = x86_64_regs.cs;*/
1046			/*i386_regs.eflags = x86_64_regs.eflags;*/
1047			i386_regs.esp = x86_64_regs.rsp;
1048			/*i386_regs.xss = x86_64_regs.ss;*/
1049		}
1050	}
1051# elif defined(ARM)
1052	get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (void *)&arm_regs);
1053# elif defined(AARCH64)
1054	/*aarch64_io.iov_base = &arm_regs_union; - already is */
1055	aarch64_io.iov_len = sizeof(arm_regs_union);
1056	get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (void *)&aarch64_io);
1057#  if 0
1058	/* Paranoia checks */
1059	if (get_regs_error)
1060		return;
1061	switch (aarch64_io.iov_len) {
1062		case sizeof(aarch64_regs):
1063			/* We are in 64-bit mode */
1064			break;
1065		case sizeof(arm_regs):
1066			/* We are in 32-bit mode */
1067			break;
1068		default:
1069			get_regs_error = -1;
1070			break;
1071	}
1072#  endif
1073# elif defined(SPARC) || defined(SPARC64)
1074	get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
1075# elif defined(TILE)
1076	get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &tile_regs);
1077# elif defined(OR1K)
1078	or1k_io.iov_len = sizeof(or1k_regs);
1079	get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &or1k_io);
1080# elif defined(METAG)
1081	metag_io.iov_len = sizeof(metag_regs);
1082	get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &metag_io);
1083# endif
1084}
1085#endif
1086
1087/* Returns:
1088 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1089 * 1: ok, continue in trace_syscall_entering().
1090 * other: error, trace_syscall_entering() should print error indicator
1091 *    ("????" etc) and bail out.
1092 */
1093static int
1094get_scno(struct tcb *tcp)
1095{
1096	long scno = 0;
1097
1098#if defined(S390) || defined(S390X)
1099	if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
1100		return -1;
1101
1102	if (syscall_mode != -ENOSYS) {
1103		/*
1104		 * Since kernel version 2.5.44 the scno gets passed in gpr2.
1105		 */
1106		scno = syscall_mode;
1107	} else {
1108		/*
1109		 * Old style of "passing" the scno via the SVC instruction.
1110		 */
1111		long psw;
1112		long opcode, offset_reg, tmp;
1113		void *svc_addr;
1114		static const int gpr_offset[16] = {
1115				PT_GPR0,  PT_GPR1,  PT_ORIGGPR2, PT_GPR3,
1116				PT_GPR4,  PT_GPR5,  PT_GPR6,     PT_GPR7,
1117				PT_GPR8,  PT_GPR9,  PT_GPR10,    PT_GPR11,
1118				PT_GPR12, PT_GPR13, PT_GPR14,    PT_GPR15
1119		};
1120
1121		if (upeek(tcp, PT_PSWADDR, &psw) < 0)
1122			return -1;
1123		errno = 0;
1124		opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
1125		if (errno) {
1126			perror_msg("peektext(psw-oneword)");
1127			return -1;
1128		}
1129
1130		/*
1131		 *  We have to check if the SVC got executed directly or via an
1132		 *  EXECUTE instruction. In case of EXECUTE it is necessary to do
1133		 *  instruction decoding to derive the system call number.
1134		 *  Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1135		 *  so that this doesn't work if a SVC opcode is part of an EXECUTE
1136		 *  opcode. Since there is no way to find out the opcode size this
1137		 *  is the best we can do...
1138		 */
1139		if ((opcode & 0xff00) == 0x0a00) {
1140			/* SVC opcode */
1141			scno = opcode & 0xff;
1142		}
1143		else {
1144			/* SVC got executed by EXECUTE instruction */
1145
1146			/*
1147			 *  Do instruction decoding of EXECUTE. If you really want to
1148			 *  understand this, read the Principles of Operations.
1149			 */
1150			svc_addr = (void *) (opcode & 0xfff);
1151
1152			tmp = 0;
1153			offset_reg = (opcode & 0x000f0000) >> 16;
1154			if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
1155				return -1;
1156			svc_addr += tmp;
1157
1158			tmp = 0;
1159			offset_reg = (opcode & 0x0000f000) >> 12;
1160			if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
1161				return -1;
1162			svc_addr += tmp;
1163
1164			scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
1165			if (errno)
1166				return -1;
1167# if defined(S390X)
1168			scno >>= 48;
1169# else
1170			scno >>= 16;
1171# endif
1172			tmp = 0;
1173			offset_reg = (opcode & 0x00f00000) >> 20;
1174			if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
1175				return -1;
1176
1177			scno = (scno | tmp) & 0xff;
1178		}
1179	}
1180#elif defined(POWERPC)
1181	if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
1182		return -1;
1183# ifdef POWERPC64
1184	/* TODO: speed up strace by not doing this at every syscall.
1185	 * We only need to do it after execve.
1186	 */
1187	int currpers;
1188	long val;
1189
1190	/* Check for 64/32 bit mode. */
1191	if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
1192		return -1;
1193	/* SF is bit 0 of MSR */
1194	if (val < 0)
1195		currpers = 0;
1196	else
1197		currpers = 1;
1198	update_personality(tcp, currpers);
1199# endif
1200#elif defined(AVR32)
1201	scno = avr32_regs.r8;
1202#elif defined(BFIN)
1203	if (upeek(tcp, PT_ORIG_P0, &scno))
1204		return -1;
1205#elif defined(I386)
1206	scno = i386_regs.orig_eax;
1207#elif defined(X86_64) || defined(X32)
1208# ifndef __X32_SYSCALL_BIT
1209#  define __X32_SYSCALL_BIT	0x40000000
1210# endif
1211	int currpers;
1212# if 1
1213	/* GETREGSET of NT_PRSTATUS tells us regset size,
1214	 * which unambiguously detects i386.
1215	 *
1216	 * Linux kernel distinguishes x86-64 and x32 processes
1217	 * solely by looking at __X32_SYSCALL_BIT:
1218	 * arch/x86/include/asm/compat.h::is_x32_task():
1219	 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1220	 *         return true;
1221	 */
1222	if (x86_io.iov_len == sizeof(i386_regs)) {
1223		scno = i386_regs.orig_eax;
1224		currpers = 1;
1225	} else {
1226		scno = x86_64_regs.orig_rax;
1227		currpers = 0;
1228		if (scno & __X32_SYSCALL_BIT) {
1229			scno -= __X32_SYSCALL_BIT;
1230			currpers = 2;
1231		}
1232	}
1233# elif 0
1234	/* cs = 0x33 for long mode (native 64 bit and x32)
1235	 * cs = 0x23 for compatibility mode (32 bit)
1236	 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1237	 */
1238	scno = x86_64_regs.orig_rax;
1239	switch (x86_64_regs.cs) {
1240		case 0x23: currpers = 1; break;
1241		case 0x33:
1242			if (x86_64_regs.ds == 0x2b) {
1243				currpers = 2;
1244				scno &= ~__X32_SYSCALL_BIT;
1245			} else
1246				currpers = 0;
1247			break;
1248		default:
1249			fprintf(stderr, "Unknown value CS=0x%08X while "
1250				 "detecting personality of process "
1251				 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
1252			currpers = current_personality;
1253			break;
1254	}
1255# elif 0
1256	/* This version analyzes the opcode of a syscall instruction.
1257	 * (int 0x80 on i386 vs. syscall on x86-64)
1258	 * It works, but is too complicated, and strictly speaking, unreliable.
1259	 */
1260	unsigned long call, rip = x86_64_regs.rip;
1261	/* sizeof(syscall) == sizeof(int 0x80) == 2 */
1262	rip -= 2;
1263	errno = 0;
1264	call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
1265	if (errno)
1266		fprintf(stderr, "ptrace_peektext failed: %s\n",
1267				strerror(errno));
1268	switch (call & 0xffff) {
1269		/* x86-64: syscall = 0x0f 0x05 */
1270		case 0x050f: currpers = 0; break;
1271		/* i386: int 0x80 = 0xcd 0x80 */
1272		case 0x80cd: currpers = 1; break;
1273		default:
1274			currpers = current_personality;
1275			fprintf(stderr,
1276				"Unknown syscall opcode (0x%04X) while "
1277				"detecting personality of process "
1278				"PID=%d\n", (int)call, tcp->pid);
1279			break;
1280	}
1281# endif
1282
1283# ifdef X32
1284	/* If we are built for a x32 system, then personality 0 is x32
1285	 * (not x86_64), and stracing of x86_64 apps is not supported.
1286	 * Stracing of i386 apps is still supported.
1287	 */
1288	if (currpers == 0) {
1289		fprintf(stderr, "syscall_%lu(...) in unsupported "
1290				"64-bit mode of process PID=%d\n",
1291			scno, tcp->pid);
1292		return 0;
1293	}
1294	currpers &= ~2; /* map 2,1 to 0,1 */
1295# endif
1296	update_personality(tcp, currpers);
1297#elif defined(IA64)
1298#	define IA64_PSR_IS	((long)1 << 34)
1299	long psr;
1300	if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
1301		ia32 = (psr & IA64_PSR_IS) != 0;
1302	if (ia32) {
1303		if (upeek(tcp, PT_R1, &scno) < 0)
1304			return -1;
1305	} else {
1306		if (upeek(tcp, PT_R15, &scno) < 0)
1307			return -1;
1308	}
1309#elif defined(AARCH64)
1310	switch (aarch64_io.iov_len) {
1311		case sizeof(aarch64_regs):
1312			/* We are in 64-bit mode */
1313			scno = aarch64_regs.regs[8];
1314			update_personality(tcp, 1);
1315			break;
1316		case sizeof(arm_regs):
1317			/* We are in 32-bit mode */
1318			scno = arm_regs.ARM_r7;
1319			update_personality(tcp, 0);
1320			break;
1321	}
1322#elif defined(ARM)
1323	if (arm_regs.ARM_ip != 0) {
1324		/* It is not a syscall entry */
1325		fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1326		tcp->flags |= TCB_INSYSCALL;
1327		return 0;
1328	}
1329	/* Note: we support only 32-bit CPUs, not 26-bit */
1330
1331	if (arm_regs.ARM_cpsr & 0x20) {
1332		/* Thumb mode */
1333		scno = arm_regs.ARM_r7;
1334	} else {
1335		/* ARM mode */
1336		errno = 0;
1337		scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1338		if (errno)
1339			return -1;
1340
1341		/* EABI syscall convention? */
1342		if (scno == 0xef000000) {
1343			scno = arm_regs.ARM_r7; /* yes */
1344		} else {
1345			if ((scno & 0x0ff00000) != 0x0f900000) {
1346				fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1347					tcp->pid, scno);
1348				return -1;
1349			}
1350			/* Fixup the syscall number */
1351			scno &= 0x000fffff;
1352		}
1353	}
1354
1355	scno = shuffle_scno(scno);
1356#elif defined(M68K)
1357	if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
1358		return -1;
1359#elif defined(LINUX_MIPSN32)
1360	unsigned long long regs[38];
1361
1362	if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1363		return -1;
1364	mips_a3 = regs[REG_A3];
1365	mips_r2 = regs[REG_V0];
1366
1367	scno = mips_r2;
1368	if (!SCNO_IN_RANGE(scno)) {
1369		if (mips_a3 == 0 || mips_a3 == -1) {
1370			if (debug_flag)
1371				fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1372			return 0;
1373		}
1374	}
1375#elif defined(MIPS)
1376	if (upeek(tcp, REG_A3, &mips_a3) < 0)
1377		return -1;
1378	if (upeek(tcp, REG_V0, &scno) < 0)
1379		return -1;
1380
1381	if (!SCNO_IN_RANGE(scno)) {
1382		if (mips_a3 == 0 || mips_a3 == -1) {
1383			if (debug_flag)
1384				fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1385			return 0;
1386		}
1387	}
1388#elif defined(ALPHA)
1389	if (upeek(tcp, REG_A3, &alpha_a3) < 0)
1390		return -1;
1391	if (upeek(tcp, REG_R0, &scno) < 0)
1392		return -1;
1393
1394	/*
1395	 * Do some sanity checks to figure out if it's
1396	 * really a syscall entry
1397	 */
1398	if (!SCNO_IN_RANGE(scno)) {
1399		if (alpha_a3 == 0 || alpha_a3 == -1) {
1400			if (debug_flag)
1401				fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
1402			return 0;
1403		}
1404	}
1405#elif defined(SPARC) || defined(SPARC64)
1406	/* Disassemble the syscall trap. */
1407	/* Retrieve the syscall trap instruction. */
1408	unsigned long trap;
1409	errno = 0;
1410# if defined(SPARC64)
1411	trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
1412	trap >>= 32;
1413# else
1414	trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
1415# endif
1416	if (errno)
1417		return -1;
1418
1419	/* Disassemble the trap to see what personality to use. */
1420	switch (trap) {
1421	case 0x91d02010:
1422		/* Linux/SPARC syscall trap. */
1423		update_personality(tcp, 0);
1424		break;
1425	case 0x91d0206d:
1426		/* Linux/SPARC64 syscall trap. */
1427		update_personality(tcp, 2);
1428		break;
1429	case 0x91d02000:
1430		/* SunOS syscall trap. (pers 1) */
1431		fprintf(stderr, "syscall: SunOS no support\n");
1432		return -1;
1433	case 0x91d02008:
1434		/* Solaris 2.x syscall trap. (per 2) */
1435		update_personality(tcp, 1);
1436		break;
1437	case 0x91d02009:
1438		/* NetBSD/FreeBSD syscall trap. */
1439		fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1440		return -1;
1441	case 0x91d02027:
1442		/* Solaris 2.x gettimeofday */
1443		update_personality(tcp, 1);
1444		break;
1445	default:
1446# if defined(SPARC64)
1447		fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
1448# else
1449		fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
1450# endif
1451		return -1;
1452	}
1453
1454	/* Extract the system call number from the registers. */
1455	if (trap == 0x91d02027)
1456		scno = 156;
1457	else
1458		scno = sparc_regs.u_regs[U_REG_G1];
1459	if (scno == 0) {
1460		scno = sparc_regs.u_regs[U_REG_O0];
1461		memmove(&sparc_regs.u_regs[U_REG_O0], &sparc_regs.u_regs[U_REG_O1], 7*sizeof(sparc_regs.u_regs[0]));
1462	}
1463#elif defined(HPPA)
1464	if (upeek(tcp, PT_GR20, &scno) < 0)
1465		return -1;
1466#elif defined(SH)
1467	/*
1468	 * In the new syscall ABI, the system call number is in R3.
1469	 */
1470	if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
1471		return -1;
1472
1473	if (scno < 0) {
1474		/* Odd as it may seem, a glibc bug has been known to cause
1475		   glibc to issue bogus negative syscall numbers.  So for
1476		   our purposes, make strace print what it *should* have been */
1477		long correct_scno = (scno & 0xff);
1478		if (debug_flag)
1479			fprintf(stderr,
1480				"Detected glibc bug: bogus system call"
1481				" number = %ld, correcting to %ld\n",
1482				scno,
1483				correct_scno);
1484		scno = correct_scno;
1485	}
1486#elif defined(SH64)
1487	if (upeek(tcp, REG_SYSCALL, &scno) < 0)
1488		return -1;
1489	scno &= 0xFFFF;
1490#elif defined(CRISV10) || defined(CRISV32)
1491	if (upeek(tcp, 4*PT_R9, &scno) < 0)
1492		return -1;
1493#elif defined(TILE)
1494	int currpers;
1495	scno = tile_regs.regs[10];
1496# ifdef __tilepro__
1497	currpers = 1;
1498# else
1499#  ifndef PT_FLAGS_COMPAT
1500#   define PT_FLAGS_COMPAT 0x10000  /* from Linux 3.8 on */
1501#  endif
1502	if (tile_regs.flags & PT_FLAGS_COMPAT)
1503		currpers = 1;
1504	else
1505		currpers = 0;
1506# endif
1507	update_personality(tcp, currpers);
1508#elif defined(MICROBLAZE)
1509	if (upeek(tcp, 0, &scno) < 0)
1510		return -1;
1511#elif defined(OR1K)
1512	scno = or1k_regs.gpr[11];
1513#elif defined(METAG)
1514	scno = metag_regs.dx[0][1];	/* syscall number in D1Re0 (D1.0) */
1515#endif
1516
1517	tcp->scno = scno;
1518	if (SCNO_IS_VALID(tcp->scno)) {
1519		tcp->s_ent = &sysent[scno];
1520		tcp->qual_flg = qual_flags[scno];
1521	} else {
1522		static const struct_sysent unknown = {
1523			.nargs = MAX_ARGS,
1524			.sys_flags = 0,
1525			.sys_func = printargs,
1526			.sys_name = "unknown", /* not used */
1527		};
1528		tcp->s_ent = &unknown;
1529		tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1530	}
1531	return 1;
1532}
1533
1534/* Called at each syscall entry.
1535 * Returns:
1536 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1537 * 1: ok, continue in trace_syscall_entering().
1538 * other: error, trace_syscall_entering() should print error indicator
1539 *    ("????" etc) and bail out.
1540 */
1541static int
1542syscall_fixup_on_sysenter(struct tcb *tcp)
1543{
1544	/* A common case of "not a syscall entry" is post-execve SIGTRAP */
1545#if defined(I386)
1546	if (i386_regs.eax != -ENOSYS) {
1547		if (debug_flag)
1548			fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1549		return 0;
1550	}
1551#elif defined(X86_64) || defined(X32)
1552	{
1553		long rax;
1554		if (x86_io.iov_len == sizeof(i386_regs)) {
1555			/* Sign extend from 32 bits */
1556			rax = (int32_t)i386_regs.eax;
1557		} else {
1558			/* Note: in X32 build, this truncates 64 to 32 bits */
1559			rax = x86_64_regs.rax;
1560		}
1561		if (rax != -ENOSYS) {
1562			if (debug_flag)
1563				fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1564			return 0;
1565		}
1566	}
1567#elif defined(S390) || defined(S390X)
1568	/* TODO: we already fetched PT_GPR2 in get_scno
1569	 * and stored it in syscall_mode, reuse it here
1570	 * instead of re-fetching?
1571	 */
1572	if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1573		return -1;
1574	if (syscall_mode != -ENOSYS)
1575		syscall_mode = tcp->scno;
1576	if (gpr2 != syscall_mode) {
1577		if (debug_flag)
1578			fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
1579		return 0;
1580	}
1581#elif defined(M68K)
1582	/* TODO? Eliminate upeek's in arches below like we did in x86 */
1583	if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
1584		return -1;
1585	if (m68k_d0 != -ENOSYS) {
1586		if (debug_flag)
1587			fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
1588		return 0;
1589	}
1590#elif defined(IA64)
1591	if (upeek(tcp, PT_R10, &ia64_r10) < 0)
1592		return -1;
1593	if (upeek(tcp, PT_R8, &ia64_r8) < 0)
1594		return -1;
1595	if (ia32 && ia64_r8 != -ENOSYS) {
1596		if (debug_flag)
1597			fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
1598		return 0;
1599	}
1600#elif defined(CRISV10) || defined(CRISV32)
1601	if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
1602		return -1;
1603	if (cris_r10 != -ENOSYS) {
1604		if (debug_flag)
1605			fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
1606		return 0;
1607	}
1608#elif defined(MICROBLAZE)
1609	if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
1610		return -1;
1611	if (microblaze_r3 != -ENOSYS) {
1612		if (debug_flag)
1613			fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
1614		return 0;
1615	}
1616#endif
1617	return 1;
1618}
1619
1620static void
1621internal_fork(struct tcb *tcp)
1622{
1623#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1624# define ARG_FLAGS	1
1625#else
1626# define ARG_FLAGS	0
1627#endif
1628#ifndef CLONE_UNTRACED
1629# define CLONE_UNTRACED	0x00800000
1630#endif
1631	if ((ptrace_setoptions
1632	    & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1633	   == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1634		return;
1635
1636	if (!followfork)
1637		return;
1638
1639	if (entering(tcp)) {
1640		/*
1641		 * We won't see the new child if clone is called with
1642		 * CLONE_UNTRACED, so we keep the same logic with that option
1643		 * and don't trace it.
1644		 */
1645		if ((tcp->s_ent->sys_func == sys_clone)
1646		 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1647		)
1648			return;
1649		setbpt(tcp);
1650	} else {
1651		if (tcp->flags & TCB_BPTSET)
1652			clearbpt(tcp);
1653	}
1654}
1655
1656#if defined(TCB_WAITEXECVE)
1657static void
1658internal_exec(struct tcb *tcp)
1659{
1660	/* Maybe we have post-execve SIGTRAP suppressed? */
1661	if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1662		return; /* yes, no need to do anything */
1663
1664	if (exiting(tcp) && syserror(tcp))
1665		/* Error in execve, no post-execve SIGTRAP expected */
1666		tcp->flags &= ~TCB_WAITEXECVE;
1667	else
1668		tcp->flags |= TCB_WAITEXECVE;
1669}
1670#endif
1671
1672static void
1673syscall_fixup_for_fork_exec(struct tcb *tcp)
1674{
1675	/*
1676	 * We must always trace a few critical system calls in order to
1677	 * correctly support following forks in the presence of tracing
1678	 * qualifiers.
1679	 */
1680	int (*func)();
1681
1682	func = tcp->s_ent->sys_func;
1683
1684	if (   sys_fork == func
1685	    || sys_vfork == func
1686	    || sys_clone == func
1687	   ) {
1688		internal_fork(tcp);
1689		return;
1690	}
1691
1692#if defined(TCB_WAITEXECVE)
1693	if (   sys_execve == func
1694# if defined(SPARC) || defined(SPARC64)
1695	    || sys_execv == func
1696# endif
1697	   ) {
1698		internal_exec(tcp);
1699		return;
1700	}
1701#endif
1702}
1703
1704/* Return -1 on error or 1 on success (never 0!) */
1705static int
1706get_syscall_args(struct tcb *tcp)
1707{
1708	int i, nargs;
1709
1710	nargs = tcp->s_ent->nargs;
1711
1712#if defined(S390) || defined(S390X)
1713	for (i = 0; i < nargs; ++i)
1714		if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1715			return -1;
1716#elif defined(ALPHA)
1717	for (i = 0; i < nargs; ++i)
1718		if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
1719			return -1;
1720#elif defined(IA64)
1721	if (!ia32) {
1722		unsigned long *out0, cfm, sof, sol;
1723		long rbs_end;
1724		/* be backwards compatible with kernel < 2.4.4... */
1725#		ifndef PT_RBS_END
1726#		  define PT_RBS_END	PT_AR_BSP
1727#		endif
1728
1729		if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
1730			return -1;
1731		if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1732			return -1;
1733
1734		sof = (cfm >> 0) & 0x7f;
1735		sol = (cfm >> 7) & 0x7f;
1736		out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1737
1738		for (i = 0; i < nargs; ++i) {
1739			if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1740				   sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1741				return -1;
1742		}
1743	} else {
1744		static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1745						      PT_R9  /* ECX = out1 */,
1746						      PT_R10 /* EDX = out2 */,
1747						      PT_R14 /* ESI = out3 */,
1748						      PT_R15 /* EDI = out4 */,
1749						      PT_R13 /* EBP = out5 */};
1750
1751		for (i = 0; i < nargs; ++i) {
1752			if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1753				return -1;
1754			/* truncate away IVE sign-extension */
1755			tcp->u_arg[i] &= 0xffffffff;
1756		}
1757	}
1758#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
1759	/* N32 and N64 both use up to six registers.  */
1760	unsigned long long regs[38];
1761
1762	if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1763		return -1;
1764
1765	for (i = 0; i < nargs; ++i) {
1766		tcp->u_arg[i] = regs[REG_A0 + i];
1767# if defined(LINUX_MIPSN32)
1768		tcp->ext_arg[i] = regs[REG_A0 + i];
1769# endif
1770	}
1771#elif defined(MIPS)
1772	if (nargs > 4) {
1773		long sp;
1774
1775		if (upeek(tcp, REG_SP, &sp) < 0)
1776			return -1;
1777		for (i = 0; i < 4; ++i)
1778			if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1779				return -1;
1780		umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
1781		       (char *)(tcp->u_arg + 4));
1782	} else {
1783		for (i = 0; i < nargs; ++i)
1784			if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1785				return -1;
1786	}
1787#elif defined(POWERPC)
1788# ifndef PT_ORIG_R3
1789#  define PT_ORIG_R3 34
1790# endif
1791	for (i = 0; i < nargs; ++i) {
1792		if (upeek(tcp, (i==0) ?
1793			(sizeof(unsigned long) * PT_ORIG_R3) :
1794			((i+PT_R3) * sizeof(unsigned long)),
1795				&tcp->u_arg[i]) < 0)
1796			return -1;
1797	}
1798#elif defined(SPARC) || defined(SPARC64)
1799	for (i = 0; i < nargs; ++i)
1800		tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
1801#elif defined(HPPA)
1802	for (i = 0; i < nargs; ++i)
1803		if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1804			return -1;
1805#elif defined(ARM) || defined(AARCH64)
1806# if defined(AARCH64)
1807	if (tcp->currpers == 1)
1808		for (i = 0; i < nargs; ++i)
1809			tcp->u_arg[i] = aarch64_regs.regs[i];
1810	else
1811# endif
1812	for (i = 0; i < nargs; ++i)
1813		tcp->u_arg[i] = arm_regs.uregs[i];
1814#elif defined(AVR32)
1815	(void)i;
1816	(void)nargs;
1817	tcp->u_arg[0] = avr32_regs.r12;
1818	tcp->u_arg[1] = avr32_regs.r11;
1819	tcp->u_arg[2] = avr32_regs.r10;
1820	tcp->u_arg[3] = avr32_regs.r9;
1821	tcp->u_arg[4] = avr32_regs.r5;
1822	tcp->u_arg[5] = avr32_regs.r3;
1823#elif defined(BFIN)
1824	static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
1825
1826	for (i = 0; i < nargs; ++i)
1827		if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1828			return -1;
1829#elif defined(SH)
1830	static const int syscall_regs[MAX_ARGS] = {
1831		4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1832		4 * (REG_REG0+7), 4 * (REG_REG0  ), 4 * (REG_REG0+1)
1833	};
1834
1835	for (i = 0; i < nargs; ++i)
1836		if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
1837			return -1;
1838#elif defined(SH64)
1839	int i;
1840	/* Registers used by SH5 Linux system calls for parameters */
1841	static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
1842
1843	for (i = 0; i < nargs; ++i)
1844		if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1845			return -1;
1846#elif defined(I386)
1847	(void)i;
1848	(void)nargs;
1849	tcp->u_arg[0] = i386_regs.ebx;
1850	tcp->u_arg[1] = i386_regs.ecx;
1851	tcp->u_arg[2] = i386_regs.edx;
1852	tcp->u_arg[3] = i386_regs.esi;
1853	tcp->u_arg[4] = i386_regs.edi;
1854	tcp->u_arg[5] = i386_regs.ebp;
1855#elif defined(X86_64) || defined(X32)
1856	(void)i;
1857	(void)nargs;
1858	if (x86_io.iov_len != sizeof(i386_regs)) {
1859		/* x86-64 or x32 ABI */
1860		tcp->u_arg[0] = x86_64_regs.rdi;
1861		tcp->u_arg[1] = x86_64_regs.rsi;
1862		tcp->u_arg[2] = x86_64_regs.rdx;
1863		tcp->u_arg[3] = x86_64_regs.r10;
1864		tcp->u_arg[4] = x86_64_regs.r8;
1865		tcp->u_arg[5] = x86_64_regs.r9;
1866#  ifdef X32
1867		tcp->ext_arg[0] = x86_64_regs.rdi;
1868		tcp->ext_arg[1] = x86_64_regs.rsi;
1869		tcp->ext_arg[2] = x86_64_regs.rdx;
1870		tcp->ext_arg[3] = x86_64_regs.r10;
1871		tcp->ext_arg[4] = x86_64_regs.r8;
1872		tcp->ext_arg[5] = x86_64_regs.r9;
1873#  endif
1874	} else {
1875		/* i386 ABI */
1876		/* Zero-extend from 32 bits */
1877		/* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1878		 * if you need to use *sign-extended* parameter.
1879		 */
1880		tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1881		tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1882		tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1883		tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1884		tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1885		tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
1886	}
1887#elif defined(MICROBLAZE)
1888	for (i = 0; i < nargs; ++i)
1889		if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1890			return -1;
1891#elif defined(CRISV10) || defined(CRISV32)
1892	static const int crisregs[MAX_ARGS] = {
1893		4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
1894		4*PT_R13     , 4*PT_MOF, 4*PT_SRP
1895	};
1896
1897	for (i = 0; i < nargs; ++i)
1898		if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0)
1899			return -1;
1900#elif defined(TILE)
1901	for (i = 0; i < nargs; ++i)
1902		tcp->u_arg[i] = tile_regs.regs[i];
1903#elif defined(M68K)
1904	for (i = 0; i < nargs; ++i)
1905		if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1906			return -1;
1907#elif defined(OR1K)
1908	(void)nargs;
1909	for (i = 0; i < 6; ++i)
1910		tcp->u_arg[i] = or1k_regs.gpr[3 + i];
1911#elif defined(METAG)
1912	for (i = 0; i < nargs; i++)
1913		/* arguments go backwards from D1Ar1 (D1.3) */
1914		tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
1915#else /* Other architecture (32bits specific) */
1916	for (i = 0; i < nargs; ++i)
1917		if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
1918			return -1;
1919#endif
1920	return 1;
1921}
1922
1923static int
1924trace_syscall_entering(struct tcb *tcp)
1925{
1926	int res, scno_good;
1927
1928#if defined TCB_WAITEXECVE
1929	if (tcp->flags & TCB_WAITEXECVE) {
1930		/* This is the post-execve SIGTRAP. */
1931		tcp->flags &= ~TCB_WAITEXECVE;
1932		return 0;
1933	}
1934#endif
1935
1936	scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
1937	if (res == 0)
1938		return res;
1939	if (res == 1) {
1940		res = syscall_fixup_on_sysenter(tcp);
1941		if (res == 0)
1942			return res;
1943		if (res == 1)
1944			res = get_syscall_args(tcp);
1945	}
1946
1947	if (res != 1) {
1948		printleader(tcp);
1949		if (scno_good != 1)
1950			tprints("????" /* anti-trigraph gap */ "(");
1951		else if (tcp->qual_flg & UNDEFINED_SCNO)
1952			tprintf("%s(", undefined_scno_name(tcp));
1953		else
1954			tprintf("%s(", tcp->s_ent->sys_name);
1955		/*
1956		 * " <unavailable>" will be added later by the code which
1957		 * detects ptrace errors.
1958		 */
1959		goto ret;
1960	}
1961
1962#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
1963	while (1) {
1964# ifdef SYS_socket_subcall
1965		if (tcp->s_ent->sys_func == sys_socketcall) {
1966			decode_socket_subcall(tcp);
1967			break;
1968		}
1969# endif
1970# ifdef SYS_ipc_subcall
1971		if (tcp->s_ent->sys_func == sys_ipc) {
1972			decode_ipc_subcall(tcp);
1973			break;
1974		}
1975# endif
1976		break;
1977	}
1978#endif
1979
1980	if (need_fork_exec_workarounds)
1981		syscall_fixup_for_fork_exec(tcp);
1982
1983	if (!(tcp->qual_flg & QUAL_TRACE)
1984	 || (tracing_paths && !pathtrace_match(tcp))
1985	) {
1986		tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1987		return 0;
1988	}
1989
1990	tcp->flags &= ~TCB_FILTERED;
1991
1992	if (cflag == CFLAG_ONLY_STATS) {
1993		res = 0;
1994		goto ret;
1995	}
1996
1997	printleader(tcp);
1998	if (tcp->qual_flg & UNDEFINED_SCNO)
1999		tprintf("%s(", undefined_scno_name(tcp));
2000	else
2001		tprintf("%s(", tcp->s_ent->sys_name);
2002	if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
2003		res = printargs(tcp);
2004	else
2005		res = tcp->s_ent->sys_func(tcp);
2006
2007	fflush(tcp->outf);
2008 ret:
2009	tcp->flags |= TCB_INSYSCALL;
2010	/* Measure the entrance time as late as possible to avoid errors. */
2011	if (Tflag || cflag)
2012		gettimeofday(&tcp->etime, NULL);
2013	return res;
2014}
2015
2016/* Returns:
2017 * 1: ok, continue in trace_syscall_exiting().
2018 * -1: error, trace_syscall_exiting() should print error indicator
2019 *    ("????" etc) and bail out.
2020 */
2021static int
2022get_syscall_result(struct tcb *tcp)
2023{
2024#if defined(S390) || defined(S390X)
2025	if (upeek(tcp, PT_GPR2, &gpr2) < 0)
2026		return -1;
2027#elif defined(POWERPC)
2028# define SO_MASK 0x10000000
2029	{
2030		long flags;
2031		if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
2032			return -1;
2033		if (upeek(tcp, sizeof(unsigned long)*PT_R3, &ppc_result) < 0)
2034			return -1;
2035		if (flags & SO_MASK)
2036			ppc_result = -ppc_result;
2037	}
2038#elif defined(AVR32)
2039	/* already done by get_regs */
2040#elif defined(BFIN)
2041	if (upeek(tcp, PT_R0, &bfin_r0) < 0)
2042		return -1;
2043#elif defined(I386)
2044	/* already done by get_regs */
2045#elif defined(X86_64) || defined(X32)
2046	/* already done by get_regs */
2047#elif defined(IA64)
2048#	define IA64_PSR_IS	((long)1 << 34)
2049	long psr;
2050	if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
2051		ia32 = (psr & IA64_PSR_IS) != 0;
2052	if (upeek(tcp, PT_R8, &ia64_r8) < 0)
2053		return -1;
2054	if (upeek(tcp, PT_R10, &ia64_r10) < 0)
2055		return -1;
2056#elif defined(ARM)
2057	/* already done by get_regs */
2058#elif defined(AARCH64)
2059	/* register reading already done by get_regs */
2060
2061	/* Used to do this, but we did it on syscall entry already: */
2062	/* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2063	 * else it's personality 0.
2064	 */
2065	/*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
2066#elif defined(M68K)
2067	if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
2068		return -1;
2069#elif defined(LINUX_MIPSN32)
2070	unsigned long long regs[38];
2071
2072	if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2073		return -1;
2074	mips_a3 = regs[REG_A3];
2075	mips_r2 = regs[REG_V0];
2076#elif defined(MIPS)
2077	if (upeek(tcp, REG_A3, &mips_a3) < 0)
2078		return -1;
2079	if (upeek(tcp, REG_V0, &mips_r2) < 0)
2080		return -1;
2081#elif defined(ALPHA)
2082	if (upeek(tcp, REG_A3, &alpha_a3) < 0)
2083		return -1;
2084	if (upeek(tcp, REG_R0, &alpha_r0) < 0)
2085		return -1;
2086#elif defined(SPARC) || defined(SPARC64)
2087	/* already done by get_regs */
2088#elif defined(HPPA)
2089	if (upeek(tcp, PT_GR28, &hppa_r28) < 0)
2090		return -1;
2091#elif defined(SH)
2092	/* new syscall ABI returns result in R0 */
2093	if (upeek(tcp, 4*REG_REG0, (long *)&sh_r0) < 0)
2094		return -1;
2095#elif defined(SH64)
2096	/* ABI defines result returned in r9 */
2097	if (upeek(tcp, REG_GENERAL(9), (long *)&sh64_r9) < 0)
2098		return -1;
2099#elif defined(CRISV10) || defined(CRISV32)
2100	if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
2101		return -1;
2102#elif defined(TILE)
2103	/* already done by get_regs */
2104#elif defined(MICROBLAZE)
2105	if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
2106		return -1;
2107#elif defined(OR1K)
2108	/* already done by get_regs */
2109#elif defined(METAG)
2110	/* already done by get_regs */
2111#endif
2112	return 1;
2113}
2114
2115/* Called at each syscall exit */
2116static void
2117syscall_fixup_on_sysexit(struct tcb *tcp)
2118{
2119#if defined(S390) || defined(S390X)
2120	if (syscall_mode != -ENOSYS)
2121		syscall_mode = tcp->scno;
2122	if ((tcp->flags & TCB_WAITEXECVE)
2123		 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
2124		/*
2125		 * Return from execve.
2126		 * Fake a return value of zero.  We leave the TCB_WAITEXECVE
2127		 * flag set for the post-execve SIGTRAP to see and reset.
2128		 */
2129		gpr2 = 0;
2130	}
2131#endif
2132}
2133
2134/*
2135 * Check the syscall return value register value for whether it is
2136 * a negated errno code indicating an error, or a success return value.
2137 */
2138static inline int
2139is_negated_errno(unsigned long int val)
2140{
2141	unsigned long int max = -(long int) nerrnos;
2142#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
2143	if (current_wordsize < sizeof(val)) {
2144		val = (unsigned int) val;
2145		max = (unsigned int) max;
2146	}
2147#endif
2148	return val > max;
2149}
2150
2151#if defined(X32)
2152static inline int
2153is_negated_errno_x32(unsigned long long val)
2154{
2155	unsigned long long max = -(long long) nerrnos;
2156	/*
2157	 * current_wordsize is 4 even in personality 0 (native X32)
2158	 * but truncation _must not_ be done in it.
2159	 * can't check current_wordsize here!
2160	 */
2161	if (current_personality != 0) {
2162		val = (uint32_t) val;
2163		max = (uint32_t) max;
2164	}
2165	return val > max;
2166}
2167#endif
2168
2169/* Returns:
2170 * 1: ok, continue in trace_syscall_exiting().
2171 * -1: error, trace_syscall_exiting() should print error indicator
2172 *    ("????" etc) and bail out.
2173 */
2174static void
2175get_error(struct tcb *tcp)
2176{
2177	int u_error = 0;
2178	int check_errno = 1;
2179	if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
2180		check_errno = 0;
2181	}
2182#if defined(S390) || defined(S390X)
2183	if (check_errno && is_negated_errno(gpr2)) {
2184		tcp->u_rval = -1;
2185		u_error = -gpr2;
2186	}
2187	else {
2188		tcp->u_rval = gpr2;
2189	}
2190#elif defined(I386)
2191	if (check_errno && is_negated_errno(i386_regs.eax)) {
2192		tcp->u_rval = -1;
2193		u_error = -i386_regs.eax;
2194	}
2195	else {
2196		tcp->u_rval = i386_regs.eax;
2197	}
2198#elif defined(X86_64)
2199	long rax;
2200	if (x86_io.iov_len == sizeof(i386_regs)) {
2201		/* Sign extend from 32 bits */
2202		rax = (int32_t)i386_regs.eax;
2203	} else {
2204		rax = x86_64_regs.rax;
2205	}
2206	if (check_errno && is_negated_errno(rax)) {
2207		tcp->u_rval = -1;
2208		u_error = -rax;
2209	}
2210	else {
2211		tcp->u_rval = rax;
2212	}
2213#elif defined(X32)
2214	/* In X32, return value is 64-bit (llseek uses one).
2215	 * Using merely "long rax" would not work.
2216	 */
2217	long long rax;
2218	if (x86_io.iov_len == sizeof(i386_regs)) {
2219		/* Sign extend from 32 bits */
2220		rax = (int32_t)i386_regs.eax;
2221	} else {
2222		rax = x86_64_regs.rax;
2223	}
2224	/* Careful: is_negated_errno() works only on longs */
2225	if (check_errno && is_negated_errno_x32(rax)) {
2226		tcp->u_rval = -1;
2227		u_error = -rax;
2228	}
2229	else {
2230		tcp->u_rval = rax; /* truncating */
2231		tcp->u_lrval = rax;
2232	}
2233#elif defined(IA64)
2234	if (ia32) {
2235		int err;
2236
2237		err = (int)ia64_r8;
2238		if (check_errno && is_negated_errno(err)) {
2239			tcp->u_rval = -1;
2240			u_error = -err;
2241		}
2242		else {
2243			tcp->u_rval = err;
2244		}
2245	} else {
2246		if (check_errno && ia64_r10) {
2247			tcp->u_rval = -1;
2248			u_error = ia64_r8;
2249		} else {
2250			tcp->u_rval = ia64_r8;
2251		}
2252	}
2253#elif defined(MIPS)
2254	if (check_errno && mips_a3) {
2255		tcp->u_rval = -1;
2256		u_error = mips_r2;
2257	} else {
2258		tcp->u_rval = mips_r2;
2259# if defined(LINUX_MIPSN32)
2260		tcp->u_lrval = mips_r2;
2261# endif
2262	}
2263#elif defined(POWERPC)
2264	if (check_errno && is_negated_errno(ppc_result)) {
2265		tcp->u_rval = -1;
2266		u_error = -ppc_result;
2267	}
2268	else {
2269		tcp->u_rval = ppc_result;
2270	}
2271#elif defined(M68K)
2272	if (check_errno && is_negated_errno(m68k_d0)) {
2273		tcp->u_rval = -1;
2274		u_error = -m68k_d0;
2275	}
2276	else {
2277		tcp->u_rval = m68k_d0;
2278	}
2279#elif defined(ARM) || defined(AARCH64)
2280# if defined(AARCH64)
2281	if (tcp->currpers == 1) {
2282		if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2283			tcp->u_rval = -1;
2284			u_error = -aarch64_regs.regs[0];
2285		}
2286		else {
2287			tcp->u_rval = aarch64_regs.regs[0];
2288		}
2289	}
2290	else
2291# endif
2292	{
2293		if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
2294			tcp->u_rval = -1;
2295			u_error = -arm_regs.ARM_r0;
2296		}
2297		else {
2298			tcp->u_rval = arm_regs.ARM_r0;
2299		}
2300	}
2301#elif defined(AVR32)
2302	if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
2303		tcp->u_rval = -1;
2304		u_error = -avr32_regs.r12;
2305	}
2306	else {
2307		tcp->u_rval = avr32_regs.r12;
2308	}
2309#elif defined(BFIN)
2310	if (check_errno && is_negated_errno(bfin_r0)) {
2311		tcp->u_rval = -1;
2312		u_error = -bfin_r0;
2313	} else {
2314		tcp->u_rval = bfin_r0;
2315	}
2316#elif defined(ALPHA)
2317	if (check_errno && alpha_a3) {
2318		tcp->u_rval = -1;
2319		u_error = alpha_r0;
2320	}
2321	else {
2322		tcp->u_rval = alpha_r0;
2323	}
2324#elif defined(SPARC)
2325	if (check_errno && sparc_regs.psr & PSR_C) {
2326		tcp->u_rval = -1;
2327		u_error = sparc_regs.u_regs[U_REG_O0];
2328	}
2329	else {
2330		tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
2331	}
2332#elif defined(SPARC64)
2333	if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
2334		tcp->u_rval = -1;
2335		u_error = sparc_regs.u_regs[U_REG_O0];
2336	}
2337	else {
2338		tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
2339	}
2340#elif defined(HPPA)
2341	if (check_errno && is_negated_errno(hppa_r28)) {
2342		tcp->u_rval = -1;
2343		u_error = -hppa_r28;
2344	}
2345	else {
2346		tcp->u_rval = hppa_r28;
2347	}
2348#elif defined(SH)
2349	if (check_errno && is_negated_errno(sh_r0)) {
2350		tcp->u_rval = -1;
2351		u_error = -sh_r0;
2352	}
2353	else {
2354		tcp->u_rval = sh_r0;
2355	}
2356#elif defined(SH64)
2357	if (check_errno && is_negated_errno(sh64_r9)) {
2358		tcp->u_rval = -1;
2359		u_error = -sh64_r9;
2360	}
2361	else {
2362		tcp->u_rval = sh64_r9;
2363	}
2364#elif defined(METAG)
2365	/* result pointer in D0Re0 (D0.0) */
2366	if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2367		tcp->u_rval = -1;
2368		u_error = -metag_regs.dx[0][0];
2369	}
2370	else {
2371		tcp->u_rval = metag_regs.dx[0][0];
2372	}
2373#elif defined(CRISV10) || defined(CRISV32)
2374	if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
2375		tcp->u_rval = -1;
2376		u_error = -cris_r10;
2377	}
2378	else {
2379		tcp->u_rval = cris_r10;
2380	}
2381#elif defined(TILE)
2382	/*
2383	 * The standard tile calling convention returns the value (or negative
2384	 * errno) in r0, and zero (or positive errno) in r1.
2385	 * Until at least kernel 3.8, however, the r1 value is not reflected
2386	 * in ptregs at this point, so we use r0 here.
2387	 */
2388	if (check_errno && is_negated_errno(tile_regs.regs[0])) {
2389		tcp->u_rval = -1;
2390		u_error = -tile_regs.regs[0];
2391	} else {
2392		tcp->u_rval = tile_regs.regs[0];
2393	}
2394#elif defined(MICROBLAZE)
2395	if (check_errno && is_negated_errno(microblaze_r3)) {
2396		tcp->u_rval = -1;
2397		u_error = -microblaze_r3;
2398	}
2399	else {
2400		tcp->u_rval = microblaze_r3;
2401	}
2402#elif defined(OR1K)
2403	if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2404		tcp->u_rval = -1;
2405		u_error = -or1k_regs.gpr[11];
2406	}
2407	else {
2408		tcp->u_rval = or1k_regs.gpr[11];
2409	}
2410#endif
2411	tcp->u_error = u_error;
2412}
2413
2414static void
2415dumpio(struct tcb *tcp)
2416{
2417	int (*func)();
2418
2419	if (syserror(tcp))
2420		return;
2421	if ((unsigned long) tcp->u_arg[0] >= num_quals)
2422		return;
2423	func = tcp->s_ent->sys_func;
2424	if (func == printargs)
2425		return;
2426	if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
2427		if (func == sys_read ||
2428		    func == sys_pread ||
2429		    func == sys_recv ||
2430		    func == sys_recvfrom)
2431			dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
2432		else if (func == sys_readv)
2433			dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2434		return;
2435	}
2436	if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
2437		if (func == sys_write ||
2438		    func == sys_pwrite ||
2439		    func == sys_send ||
2440		    func == sys_sendto)
2441			dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
2442		else if (func == sys_writev)
2443			dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2444		return;
2445	}
2446}
2447
2448static int
2449trace_syscall_exiting(struct tcb *tcp)
2450{
2451	int sys_res;
2452	struct timeval tv;
2453	int res;
2454	long u_error;
2455
2456	/* Measure the exit time as early as possible to avoid errors. */
2457	if (Tflag || cflag)
2458		gettimeofday(&tv, NULL);
2459
2460#if SUPPORTED_PERSONALITIES > 1
2461	update_personality(tcp, tcp->currpers);
2462#endif
2463	res = (get_regs_error ? -1 : get_syscall_result(tcp));
2464	if (res == 1) {
2465		syscall_fixup_on_sysexit(tcp); /* never fails */
2466		get_error(tcp); /* never fails */
2467		if (need_fork_exec_workarounds)
2468			syscall_fixup_for_fork_exec(tcp);
2469		if (filtered(tcp))
2470			goto ret;
2471	}
2472
2473	if (cflag) {
2474		struct timeval t = tv;
2475		count_syscall(tcp, &t);
2476		if (cflag == CFLAG_ONLY_STATS) {
2477			goto ret;
2478		}
2479	}
2480
2481	/* If not in -ff mode, and printing_tcp != tcp,
2482	 * then the log currently does not end with output
2483	 * of _our syscall entry_, but with something else.
2484	 * We need to say which syscall's return is this.
2485	 *
2486	 * Forced reprinting via TCB_REPRINT is used only by
2487	 * "strace -ff -oLOG test/threaded_execve" corner case.
2488	 * It's the only case when -ff mode needs reprinting.
2489	 */
2490	if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2491		tcp->flags &= ~TCB_REPRINT;
2492		printleader(tcp);
2493		if (tcp->qual_flg & UNDEFINED_SCNO)
2494			tprintf("<... %s resumed> ", undefined_scno_name(tcp));
2495		else
2496			tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
2497	}
2498	printing_tcp = tcp;
2499
2500	if (res != 1) {
2501		/* There was error in one of prior ptrace ops */
2502		tprints(") ");
2503		tabto();
2504		tprints("= ? <unavailable>\n");
2505		line_ended();
2506		tcp->flags &= ~TCB_INSYSCALL;
2507		return res;
2508	}
2509
2510	sys_res = 0;
2511	if (tcp->qual_flg & QUAL_RAW) {
2512		/* sys_res = printargs(tcp); - but it's nop on sysexit */
2513	} else {
2514	/* FIXME: not_failing_only (IOW, option -z) is broken:
2515	 * failure of syscall is known only after syscall return.
2516	 * Thus we end up with something like this on, say, ENOENT:
2517	 *     open("doesnt_exist", O_RDONLY <unfinished ...>
2518	 *     {next syscall decode}
2519	 * whereas the intended result is that open(...) line
2520	 * is not shown at all.
2521	 */
2522		if (not_failing_only && tcp->u_error)
2523			goto ret;	/* ignore failed syscalls */
2524		sys_res = tcp->s_ent->sys_func(tcp);
2525	}
2526
2527	tprints(") ");
2528	tabto();
2529	u_error = tcp->u_error;
2530	if (tcp->qual_flg & QUAL_RAW) {
2531		if (u_error)
2532			tprintf("= -1 (errno %ld)", u_error);
2533		else
2534			tprintf("= %#lx", tcp->u_rval);
2535	}
2536	else if (!(sys_res & RVAL_NONE) && u_error) {
2537		switch (u_error) {
2538		/* Blocked signals do not interrupt any syscalls.
2539		 * In this case syscalls don't return ERESTARTfoo codes.
2540		 *
2541		 * Deadly signals set to SIG_DFL interrupt syscalls
2542		 * and kill the process regardless of which of the codes below
2543		 * is returned by the interrupted syscall.
2544		 * In some cases, kernel forces a kernel-generated deadly
2545		 * signal to be unblocked and set to SIG_DFL (and thus cause
2546		 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2547		 * or SIGILL. (The alternative is to leave process spinning
2548		 * forever on the faulty instruction - not useful).
2549		 *
2550		 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2551		 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2552		 * but kernel will always restart them.
2553		 */
2554		case ERESTARTSYS:
2555			/* Most common type of signal-interrupted syscall exit code.
2556			 * The system call will be restarted with the same arguments
2557			 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2558			 */
2559			tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
2560			break;
2561		case ERESTARTNOINTR:
2562			/* Rare. For example, fork() returns this if interrupted.
2563			 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2564			 */
2565			tprints("= ? ERESTARTNOINTR (To be restarted)");
2566			break;
2567		case ERESTARTNOHAND:
2568			/* pause(), rt_sigsuspend() etc use this code.
2569			 * SA_RESTART is ignored (assumed not set):
2570			 * syscall won't restart (will return EINTR instead)
2571			 * even after signal with SA_RESTART set. However,
2572			 * after SIG_IGN or SIG_DFL signal it will restart
2573			 * (thus the name "restart only if has no handler").
2574			 */
2575			tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
2576			break;
2577		case ERESTART_RESTARTBLOCK:
2578			/* Syscalls like nanosleep(), poll() which can't be
2579			 * restarted with their original arguments use this
2580			 * code. Kernel will execute restart_syscall() instead,
2581			 * which changes arguments before restarting syscall.
2582			 * SA_RESTART is ignored (assumed not set) similarly
2583			 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2584			 * since restart data is saved in "restart block"
2585			 * in task struct, and if signal handler uses a syscall
2586			 * which in turn saves another such restart block,
2587			 * old data is lost and restart becomes impossible)
2588			 */
2589			tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
2590			break;
2591		default:
2592			if (u_error < 0)
2593				tprintf("= -1 E??? (errno %ld)", u_error);
2594			else if (u_error < nerrnos)
2595				tprintf("= -1 %s (%s)", errnoent[u_error],
2596					strerror(u_error));
2597			else
2598				tprintf("= -1 ERRNO_%ld (%s)", u_error,
2599					strerror(u_error));
2600			break;
2601		}
2602		if ((sys_res & RVAL_STR) && tcp->auxstr)
2603			tprintf(" (%s)", tcp->auxstr);
2604	}
2605	else {
2606		if (sys_res & RVAL_NONE)
2607			tprints("= ?");
2608		else {
2609			switch (sys_res & RVAL_MASK) {
2610			case RVAL_HEX:
2611				tprintf("= %#lx", tcp->u_rval);
2612				break;
2613			case RVAL_OCTAL:
2614				tprintf("= %#lo", tcp->u_rval);
2615				break;
2616			case RVAL_UDECIMAL:
2617				tprintf("= %lu", tcp->u_rval);
2618				break;
2619			case RVAL_DECIMAL:
2620				tprintf("= %ld", tcp->u_rval);
2621				break;
2622#if defined(LINUX_MIPSN32) || defined(X32)
2623			/*
2624			case RVAL_LHEX:
2625				tprintf("= %#llx", tcp->u_lrval);
2626				break;
2627			case RVAL_LOCTAL:
2628				tprintf("= %#llo", tcp->u_lrval);
2629				break;
2630			*/
2631			case RVAL_LUDECIMAL:
2632				tprintf("= %llu", tcp->u_lrval);
2633				break;
2634			/*
2635			case RVAL_LDECIMAL:
2636				tprintf("= %lld", tcp->u_lrval);
2637				break;
2638			*/
2639#endif
2640			default:
2641				fprintf(stderr,
2642					"invalid rval format\n");
2643				break;
2644			}
2645		}
2646		if ((sys_res & RVAL_STR) && tcp->auxstr)
2647			tprintf(" (%s)", tcp->auxstr);
2648	}
2649	if (Tflag) {
2650		tv_sub(&tv, &tv, &tcp->etime);
2651		tprintf(" <%ld.%06ld>",
2652			(long) tv.tv_sec, (long) tv.tv_usec);
2653	}
2654	tprints("\n");
2655	dumpio(tcp);
2656	line_ended();
2657
2658 ret:
2659	tcp->flags &= ~TCB_INSYSCALL;
2660	return 0;
2661}
2662
2663int
2664trace_syscall(struct tcb *tcp)
2665{
2666	return exiting(tcp) ?
2667		trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2668}
2669