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