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