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