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