syscall.c revision 7211dbcb2f8a43eb5d36bd15c7ba1267ba21e223
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;
698static struct iovec x86_io = {
699	.iov_base = &x86_regs_union
700};
701# define ARCH_REGS_FOR_GETREGSET x86_regs_union
702# define ARCH_IOVEC_FOR_GETREGSET x86_io
703#elif defined(IA64)
704static bool ia64_ia32mode;
705static long ia64_r8, ia64_r10;
706#elif defined(POWERPC)
707struct pt_regs ppc_regs; /* not static */
708# define ARCH_REGS_FOR_GETREGS ppc_regs
709#elif defined(M68K)
710static long m68k_d0;
711#elif defined(BFIN)
712static long bfin_r0;
713#elif defined(ARM)
714struct pt_regs arm_regs; /* not static */
715# define ARCH_REGS_FOR_GETREGS arm_regs
716#elif defined(AARCH64)
717struct arm_pt_regs {
718        int uregs[18];
719};
720# define ARM_cpsr       uregs[16]
721# define ARM_pc         uregs[15]
722# define ARM_lr         uregs[14]
723# define ARM_sp         uregs[13]
724# define ARM_ip         uregs[12]
725# define ARM_fp         uregs[11]
726# define ARM_r10        uregs[10]
727# define ARM_r9         uregs[9]
728# define ARM_r8         uregs[8]
729# define ARM_r7         uregs[7]
730# define ARM_r6         uregs[6]
731# define ARM_r5         uregs[5]
732# define ARM_r4         uregs[4]
733# define ARM_r3         uregs[3]
734# define ARM_r2         uregs[2]
735# define ARM_r1         uregs[1]
736# define ARM_r0         uregs[0]
737# define ARM_ORIG_r0    uregs[17]
738static union {
739	struct user_pt_regs aarch64_r;
740	struct arm_pt_regs  arm_r;
741} arm_regs_union;
742# define aarch64_regs arm_regs_union.aarch64_r
743# define arm_regs     arm_regs_union.arm_r
744static struct iovec aarch64_io = {
745	.iov_base = &arm_regs_union
746};
747# define ARCH_REGS_FOR_GETREGSET arm_regs_union
748# define ARCH_IOVEC_FOR_GETREGSET aarch64_io
749#elif defined(ALPHA)
750static long alpha_r0;
751static long alpha_a3;
752#elif defined(AVR32)
753static struct pt_regs avr32_regs;
754# define ARCH_REGS_FOR_GETREGS avr32_regs
755#elif defined(SPARC) || defined(SPARC64)
756struct pt_regs sparc_regs; /* not static */
757# define ARCH_REGS_FOR_GETREGS sparc_regs
758#elif defined(MIPS)
759struct mips_regs mips_regs; /* not static */
760/* PTRACE_GETREGS on MIPS is available since linux v2.6.15. */
761# define ARCH_REGS_FOR_GETREGS mips_regs
762#elif defined(S390) || defined(S390X)
763static long s390_gpr2;
764#elif defined(HPPA)
765static long hppa_r28;
766#elif defined(SH)
767static long sh_r0;
768#elif defined(SH64)
769static long sh64_r9;
770#elif defined(CRISV10) || defined(CRISV32)
771static long cris_r10;
772#elif defined(TILE)
773struct pt_regs tile_regs; /* not static */
774# define ARCH_REGS_FOR_GETREGS tile_regs
775#elif defined(MICROBLAZE)
776static long microblaze_r3;
777#elif defined(OR1K)
778static struct user_regs_struct or1k_regs;
779# define ARCH_REGS_FOR_GETREGSET or1k_regs
780#elif defined(METAG)
781static struct user_gp_regs metag_regs;
782# define ARCH_REGS_FOR_GETREGSET metag_regs
783#elif defined(XTENSA)
784static long xtensa_a2;
785# elif defined(ARC)
786static struct user_regs_struct arc_regs;
787# define ARCH_REGS_FOR_GETREGSET arc_regs
788#endif
789
790static long get_regs_error;
791
792void
793print_pc(struct tcb *tcp)
794{
795	const char *fmt;
796	const char *bad;
797
798#ifdef current_wordsize
799# define pc_wordsize current_wordsize
800#else
801# define pc_wordsize personality_wordsize[tcp->currpers]
802#endif
803
804	if (pc_wordsize == 4) {
805		fmt = "[%08lx] ";
806		bad = "[????????] ";
807	} else {
808		fmt = "[%016lx] ";
809		bad = "[????????????????] ";
810	}
811
812#undef pc_wordsize
813#define PRINTBADPC tprints(bad)
814
815	if (get_regs_error) {
816		PRINTBADPC;
817		return;
818	}
819
820#if defined(I386)
821	tprintf(fmt, i386_regs.eip);
822#elif defined(X86_64) || defined(X32)
823	if (x86_io.iov_len == sizeof(i386_regs))
824		tprintf(fmt, (unsigned long) i386_regs.eip);
825	else
826		tprintf(fmt, (unsigned long) x86_64_regs.rip);
827#elif defined(S390) || defined(S390X)
828	long psw;
829	if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0) {
830		PRINTBADPC;
831		return;
832	}
833	tprintf(fmt, psw);
834#elif defined(IA64)
835	long ip;
836	if (upeek(tcp->pid, PT_B0, &ip) < 0) {
837		PRINTBADPC;
838		return;
839	}
840	tprintf(fmt, ip);
841#elif defined(POWERPC)
842	tprintf(fmt, ppc_regs.nip);
843#elif defined(M68K)
844	long pc;
845	if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
846		PRINTBADPC;
847		return;
848	}
849	tprintf(fmt, pc);
850#elif defined(ALPHA)
851	long pc;
852	if (upeek(tcp->pid, REG_PC, &pc) < 0) {
853		PRINTBADPC;
854		return;
855	}
856	tprintf(fmt, pc);
857#elif defined(SPARC)
858	tprintf(fmt, sparc_regs.pc);
859#elif defined(SPARC64)
860	tprintf(fmt, sparc_regs.tpc);
861#elif defined(HPPA)
862	long pc;
863	if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
864		PRINTBADPC;
865		return;
866	}
867	tprintf(fmt, pc);
868#elif defined MIPS
869	tprintf(fmt, (unsigned long) mips_REG_EPC);
870#elif defined(SH)
871	long pc;
872	if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
873		PRINTBADPC;
874		return;
875	}
876	tprintf(fmt, pc);
877#elif defined(SH64)
878	long pc;
879	if (upeek(tcp->pid, REG_PC, &pc) < 0) {
880		PRINTBADPC;
881		return;
882	}
883	tprintf(fmt, pc);
884#elif defined(ARM)
885	tprintf(fmt, arm_regs.ARM_pc);
886#elif defined(AVR32)
887	tprintf(fmt, avr32_regs.pc);
888#elif defined(BFIN)
889	long pc;
890	if (upeek(tcp->pid, PT_PC, &pc) < 0) {
891		PRINTBADPC;
892		return;
893	}
894	tprintf(fmt, pc);
895#elif defined(CRISV10)
896	long pc;
897	if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
898		PRINTBADPC;
899		return;
900	}
901	tprintf(fmt, pc);
902#elif defined(CRISV32)
903	long pc;
904	if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
905		PRINTBADPC;
906		return;
907	}
908	tprintf(fmt, pc);
909#elif defined(TILE)
910	tprintf(fmt, (unsigned long) tile_regs.pc);
911#elif defined(OR1K)
912	tprintf(fmt, or1k_regs.pc);
913#elif defined(METAG)
914	tprintf(fmt, metag_regs.pc);
915#elif defined(XTENSA)
916	long pc;
917	if (upeek(tcp->pid, REG_PC, &pc) < 0) {
918		PRINTBADPC;
919		return;
920	}
921	tprintf(fmt, pc);
922#elif defined(ARC)
923	tprintf(fmt, arc_regs.efa);
924#else
925# warning print_pc is not implemented for this architecture
926	PRINTBADPC;
927#endif /* architecture */
928}
929
930/*
931 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
932 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
933 */
934#if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
935static long
936shuffle_scno(unsigned long scno)
937{
938	if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
939		return scno;
940
941	/* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
942	if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
943		return 0x000ffff0;
944	if (scno == 0x000ffff0)
945		return ARM_FIRST_SHUFFLED_SYSCALL;
946
947#define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
948	/*
949	 * Is it ARM specific syscall?
950	 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
951	 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
952	 */
953	if (scno >= 0x000f0000 &&
954	    scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
955		return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
956	}
957	if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
958		return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
959	}
960
961	return scno;
962}
963#else
964# define shuffle_scno(scno) ((long)(scno))
965#endif
966
967static char*
968undefined_scno_name(struct tcb *tcp)
969{
970	static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
971
972	sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
973	return buf;
974}
975
976#ifdef POWERPC
977/*
978 * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
979 * we provide a slow fallback for old kernels.
980 */
981static int powerpc_getregs_old(pid_t pid)
982{
983	int i;
984	long r;
985
986	if (iflag) {
987		r = upeek(pid, sizeof(long) * PT_NIP, (long *)&ppc_regs.nip);
988		if (r)
989			goto out;
990	}
991#ifdef POWERPC64 /* else we never use it */
992	r = upeek(pid, sizeof(long) * PT_MSR, (long *)&ppc_regs.msr);
993	if (r)
994		goto out;
995#endif
996	r = upeek(pid, sizeof(long) * PT_CCR, (long *)&ppc_regs.ccr);
997	if (r)
998		goto out;
999	r = upeek(pid, sizeof(long) * PT_ORIG_R3, (long *)&ppc_regs.orig_gpr3);
1000	if (r)
1001		goto out;
1002	for (i = 0; i <= 8; i++) {
1003		r = upeek(pid, sizeof(long) * (PT_R0 + i),
1004			  (long *)&ppc_regs.gpr[i]);
1005		if (r)
1006			goto out;
1007	}
1008 out:
1009	return r;
1010}
1011#endif
1012
1013void
1014clear_regs(void)
1015{
1016	get_regs_error = -1;
1017}
1018
1019#if defined ARCH_REGS_FOR_GETREGSET
1020static long
1021get_regset(pid_t pid)
1022{
1023# ifdef ARCH_IOVEC_FOR_GETREGSET
1024	/* variable iovec */
1025	ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1026	return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1027		      &ARCH_IOVEC_FOR_GETREGSET);
1028# else
1029	/* constant iovec */
1030	static struct iovec io = {
1031		.iov_base = &ARCH_REGS_FOR_GETREGSET,
1032		.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1033	};
1034	return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1035
1036# endif
1037}
1038#endif /* ARCH_REGS_FOR_GETREGSET */
1039
1040void
1041get_regs(pid_t pid)
1042{
1043#ifdef ARCH_REGS_FOR_GETREGSET
1044# ifdef X86_64
1045	/* Try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS. */
1046	static int getregset_support;
1047
1048	if (getregset_support >= 0) {
1049		get_regs_error = get_regset(pid);
1050		if (getregset_support > 0)
1051			return;
1052		if (get_regs_error >= 0) {
1053			getregset_support = 1;
1054			return;
1055		}
1056		if (errno == EPERM || errno == ESRCH)
1057			return;
1058		getregset_support = -1;
1059	}
1060	/* Use old method, with unreliable heuristical detection of 32-bitness. */
1061	x86_io.iov_len = sizeof(x86_64_regs);
1062	get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
1063	if (!get_regs_error && x86_64_regs.cs == 0x23) {
1064		x86_io.iov_len = sizeof(i386_regs);
1065		/*
1066		 * The order is important: i386_regs and x86_64_regs
1067		 * are overlaid in memory!
1068		 */
1069		i386_regs.ebx = x86_64_regs.rbx;
1070		i386_regs.ecx = x86_64_regs.rcx;
1071		i386_regs.edx = x86_64_regs.rdx;
1072		i386_regs.esi = x86_64_regs.rsi;
1073		i386_regs.edi = x86_64_regs.rdi;
1074		i386_regs.ebp = x86_64_regs.rbp;
1075		i386_regs.eax = x86_64_regs.rax;
1076		/* i386_regs.xds = x86_64_regs.ds; unused by strace */
1077		/* i386_regs.xes = x86_64_regs.es; ditto... */
1078		/* i386_regs.xfs = x86_64_regs.fs; */
1079		/* i386_regs.xgs = x86_64_regs.gs; */
1080		i386_regs.orig_eax = x86_64_regs.orig_rax;
1081		i386_regs.eip = x86_64_regs.rip;
1082		/* i386_regs.xcs = x86_64_regs.cs; */
1083		/* i386_regs.eflags = x86_64_regs.eflags; */
1084		i386_regs.esp = x86_64_regs.rsp;
1085		/* i386_regs.xss = x86_64_regs.ss; */
1086	}
1087# else /* !X86_64 */
1088	/* Assume that PTRACE_GETREGSET works. */
1089	get_regs_error = get_regset(pid);
1090# endif
1091#elif defined ARCH_REGS_FOR_GETREGS
1092# if defined SPARC || defined SPARC64
1093	/* SPARC systems have the meaning of data and addr reversed */
1094	get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&ARCH_REGS_FOR_GETREGS, 0);
1095# elif defined POWERPC
1096	static bool old_kernel = 0;
1097	if (old_kernel)
1098		goto old;
1099	get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1100	if (get_regs_error && errno == EIO) {
1101		old_kernel = 1;
1102 old:
1103		get_regs_error = powerpc_getregs_old(pid);
1104	}
1105# else
1106	/* Assume that PTRACE_GETREGS works. */
1107	get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1108# endif
1109
1110#else /* !ARCH_REGS_FOR_GETREGSET && !ARCH_REGS_FOR_GETREGS */
1111#  warning get_regs is not implemented for this architecture yet
1112	get_regs_error = 0;
1113#endif
1114}
1115
1116/* Returns:
1117 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1118 * 1: ok, continue in trace_syscall_entering().
1119 * other: error, trace_syscall_entering() should print error indicator
1120 *    ("????" etc) and bail out.
1121 */
1122static int
1123get_scno(struct tcb *tcp)
1124{
1125	long scno = 0;
1126
1127#if defined(S390) || defined(S390X)
1128	if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
1129		return -1;
1130
1131	if (s390_gpr2 != -ENOSYS) {
1132		/*
1133		 * Since kernel version 2.5.44 the scno gets passed in gpr2.
1134		 */
1135		scno = s390_gpr2;
1136	} else {
1137		/*
1138		 * Old style of "passing" the scno via the SVC instruction.
1139		 */
1140		long psw;
1141		long opcode, offset_reg, tmp;
1142		void *svc_addr;
1143		static const int gpr_offset[16] = {
1144				PT_GPR0,  PT_GPR1,  PT_ORIGGPR2, PT_GPR3,
1145				PT_GPR4,  PT_GPR5,  PT_GPR6,     PT_GPR7,
1146				PT_GPR8,  PT_GPR9,  PT_GPR10,    PT_GPR11,
1147				PT_GPR12, PT_GPR13, PT_GPR14,    PT_GPR15
1148		};
1149
1150		if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0)
1151			return -1;
1152		errno = 0;
1153		opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
1154		if (errno) {
1155			perror_msg("peektext(psw-oneword)");
1156			return -1;
1157		}
1158
1159		/*
1160		 *  We have to check if the SVC got executed directly or via an
1161		 *  EXECUTE instruction. In case of EXECUTE it is necessary to do
1162		 *  instruction decoding to derive the system call number.
1163		 *  Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1164		 *  so that this doesn't work if a SVC opcode is part of an EXECUTE
1165		 *  opcode. Since there is no way to find out the opcode size this
1166		 *  is the best we can do...
1167		 */
1168		if ((opcode & 0xff00) == 0x0a00) {
1169			/* SVC opcode */
1170			scno = opcode & 0xff;
1171		}
1172		else {
1173			/* SVC got executed by EXECUTE instruction */
1174
1175			/*
1176			 *  Do instruction decoding of EXECUTE. If you really want to
1177			 *  understand this, read the Principles of Operations.
1178			 */
1179			svc_addr = (void *) (opcode & 0xfff);
1180
1181			tmp = 0;
1182			offset_reg = (opcode & 0x000f0000) >> 16;
1183			if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
1184				return -1;
1185			svc_addr += tmp;
1186
1187			tmp = 0;
1188			offset_reg = (opcode & 0x0000f000) >> 12;
1189			if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
1190				return -1;
1191			svc_addr += tmp;
1192
1193			scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
1194			if (errno)
1195				return -1;
1196# if defined(S390X)
1197			scno >>= 48;
1198# else
1199			scno >>= 16;
1200# endif
1201			tmp = 0;
1202			offset_reg = (opcode & 0x00f00000) >> 20;
1203			if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
1204				return -1;
1205
1206			scno = (scno | tmp) & 0xff;
1207		}
1208	}
1209#elif defined(POWERPC)
1210	scno = ppc_regs.gpr[0];
1211# ifdef POWERPC64
1212	unsigned int currpers;
1213
1214	/*
1215	 * Check for 64/32 bit mode.
1216	 * Embedded implementations covered by Book E extension of PPC use
1217	 * bit 0 (CM) of 32-bit Machine state register (MSR).
1218	 * Other implementations use bit 0 (SF) of 64-bit MSR.
1219	 */
1220	currpers = (ppc_regs.msr & 0x8000000080000000) ? 0 : 1;
1221	update_personality(tcp, currpers);
1222# endif
1223#elif defined(AVR32)
1224	scno = avr32_regs.r8;
1225#elif defined(BFIN)
1226	if (upeek(tcp->pid, PT_ORIG_P0, &scno))
1227		return -1;
1228#elif defined(I386)
1229	scno = i386_regs.orig_eax;
1230#elif defined(X86_64) || defined(X32)
1231# ifndef __X32_SYSCALL_BIT
1232#  define __X32_SYSCALL_BIT	0x40000000
1233# endif
1234	unsigned int currpers;
1235# if 1
1236	/* GETREGSET of NT_PRSTATUS tells us regset size,
1237	 * which unambiguously detects i386.
1238	 *
1239	 * Linux kernel distinguishes x86-64 and x32 processes
1240	 * solely by looking at __X32_SYSCALL_BIT:
1241	 * arch/x86/include/asm/compat.h::is_x32_task():
1242	 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1243	 *         return true;
1244	 */
1245	if (x86_io.iov_len == sizeof(i386_regs)) {
1246		scno = i386_regs.orig_eax;
1247		currpers = 1;
1248	} else {
1249		scno = x86_64_regs.orig_rax;
1250		currpers = 0;
1251		if (scno & __X32_SYSCALL_BIT) {
1252			/*
1253			 * Syscall number -1 requires special treatment:
1254			 * it might be a side effect of SECCOMP_RET_ERRNO
1255			 * filtering that sets orig_rax to -1
1256			 * in some versions of linux kernel.
1257			 * If that is the case, then
1258			 * __X32_SYSCALL_BIT logic does not apply.
1259			 */
1260			if ((long long) x86_64_regs.orig_rax != -1) {
1261				scno -= __X32_SYSCALL_BIT;
1262				currpers = 2;
1263			} else {
1264#  ifdef X32
1265				currpers = 2;
1266#  endif
1267			}
1268		}
1269	}
1270# elif 0
1271	/* cs = 0x33 for long mode (native 64 bit and x32)
1272	 * cs = 0x23 for compatibility mode (32 bit)
1273	 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1274	 */
1275	scno = x86_64_regs.orig_rax;
1276	switch (x86_64_regs.cs) {
1277		case 0x23: currpers = 1; break;
1278		case 0x33:
1279			if (x86_64_regs.ds == 0x2b) {
1280				currpers = 2;
1281				scno &= ~__X32_SYSCALL_BIT;
1282			} else
1283				currpers = 0;
1284			break;
1285		default:
1286			fprintf(stderr, "Unknown value CS=0x%08X while "
1287				 "detecting personality of process "
1288				 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
1289			currpers = current_personality;
1290			break;
1291	}
1292# elif 0
1293	/* This version analyzes the opcode of a syscall instruction.
1294	 * (int 0x80 on i386 vs. syscall on x86-64)
1295	 * It works, but is too complicated, and strictly speaking, unreliable.
1296	 */
1297	unsigned long call, rip = x86_64_regs.rip;
1298	/* sizeof(syscall) == sizeof(int 0x80) == 2 */
1299	rip -= 2;
1300	errno = 0;
1301	call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
1302	if (errno)
1303		fprintf(stderr, "ptrace_peektext failed: %s\n",
1304				strerror(errno));
1305	switch (call & 0xffff) {
1306		/* x86-64: syscall = 0x0f 0x05 */
1307		case 0x050f: currpers = 0; break;
1308		/* i386: int 0x80 = 0xcd 0x80 */
1309		case 0x80cd: currpers = 1; break;
1310		default:
1311			currpers = current_personality;
1312			fprintf(stderr,
1313				"Unknown syscall opcode (0x%04X) while "
1314				"detecting personality of process "
1315				"PID=%d\n", (int)call, tcp->pid);
1316			break;
1317	}
1318# endif
1319
1320# ifdef X32
1321	/* If we are built for a x32 system, then personality 0 is x32
1322	 * (not x86_64), and stracing of x86_64 apps is not supported.
1323	 * Stracing of i386 apps is still supported.
1324	 */
1325	if (currpers == 0) {
1326		fprintf(stderr, "syscall_%lu(...) in unsupported "
1327				"64-bit mode of process PID=%d\n",
1328			scno, tcp->pid);
1329		return 0;
1330	}
1331	currpers &= ~2; /* map 2,1 to 0,1 */
1332# endif
1333	update_personality(tcp, currpers);
1334#elif defined(IA64)
1335#	define IA64_PSR_IS	((long)1 << 34)
1336	long psr;
1337	if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
1338		ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
1339	if (ia64_ia32mode) {
1340		if (upeek(tcp->pid, PT_R1, &scno) < 0)
1341			return -1;
1342	} else {
1343		if (upeek(tcp->pid, PT_R15, &scno) < 0)
1344			return -1;
1345	}
1346#elif defined(AARCH64)
1347	switch (aarch64_io.iov_len) {
1348		case sizeof(aarch64_regs):
1349			/* We are in 64-bit mode */
1350			scno = aarch64_regs.regs[8];
1351			update_personality(tcp, 1);
1352			break;
1353		case sizeof(arm_regs):
1354			/* We are in 32-bit mode */
1355			/* Note: we don't support OABI, unlike 32-bit ARM build */
1356			scno = arm_regs.ARM_r7;
1357			scno = shuffle_scno(scno);
1358			update_personality(tcp, 0);
1359			break;
1360	}
1361#elif defined(ARM)
1362	if (arm_regs.ARM_ip != 0) {
1363		/* It is not a syscall entry */
1364		fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1365		tcp->flags |= TCB_INSYSCALL;
1366		return 0;
1367	}
1368	/* Note: we support only 32-bit CPUs, not 26-bit */
1369
1370# if !defined(__ARM_EABI__) || ENABLE_ARM_OABI
1371	if (arm_regs.ARM_cpsr & 0x20)
1372		/* Thumb mode */
1373		goto scno_in_r7;
1374	/* ARM mode */
1375	/* Check EABI/OABI by examining SVC insn's low 24 bits */
1376	errno = 0;
1377	scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1378	if (errno)
1379		return -1;
1380	/* EABI syscall convention? */
1381	if ((unsigned long) scno != 0xef000000) {
1382		/* No, it's OABI */
1383		if ((scno & 0x0ff00000) != 0x0f900000) {
1384			fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1385				tcp->pid, scno);
1386			return -1;
1387		}
1388		/* Fixup the syscall number */
1389		scno &= 0x000fffff;
1390	} else {
1391 scno_in_r7:
1392		scno = arm_regs.ARM_r7;
1393	}
1394# else /* __ARM_EABI__ || !ENABLE_ARM_OABI */
1395	scno = arm_regs.ARM_r7;
1396# endif
1397	scno = shuffle_scno(scno);
1398#elif defined(M68K)
1399	if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
1400		return -1;
1401#elif defined(MIPS)
1402	scno = mips_REG_V0;
1403
1404	if (!SCNO_IN_RANGE(scno)) {
1405		if (mips_REG_A3 == 0 || mips_REG_A3 == -1) {
1406			if (debug_flag)
1407				fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1408			return 0;
1409		}
1410	}
1411#elif defined(ALPHA)
1412	if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
1413		return -1;
1414	if (upeek(tcp->pid, REG_R0, &scno) < 0)
1415		return -1;
1416
1417	/*
1418	 * Do some sanity checks to figure out if it's
1419	 * really a syscall entry
1420	 */
1421	if (!SCNO_IN_RANGE(scno)) {
1422		if (alpha_a3 == 0 || alpha_a3 == -1) {
1423			if (debug_flag)
1424				fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
1425			return 0;
1426		}
1427	}
1428#elif defined(SPARC) || defined(SPARC64)
1429	/* Disassemble the syscall trap. */
1430	/* Retrieve the syscall trap instruction. */
1431	unsigned long trap;
1432	errno = 0;
1433# if defined(SPARC64)
1434	trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
1435	trap >>= 32;
1436# else
1437	trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
1438# endif
1439	if (errno)
1440		return -1;
1441
1442	/* Disassemble the trap to see what personality to use. */
1443	switch (trap) {
1444	case 0x91d02010:
1445		/* Linux/SPARC syscall trap. */
1446		update_personality(tcp, 0);
1447		break;
1448	case 0x91d0206d:
1449		/* Linux/SPARC64 syscall trap. */
1450		update_personality(tcp, 2);
1451		break;
1452	case 0x91d02000:
1453		/* SunOS syscall trap. (pers 1) */
1454		fprintf(stderr, "syscall: SunOS no support\n");
1455		return -1;
1456	case 0x91d02008:
1457		/* Solaris 2.x syscall trap. (per 2) */
1458		update_personality(tcp, 1);
1459		break;
1460	case 0x91d02009:
1461		/* NetBSD/FreeBSD syscall trap. */
1462		fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1463		return -1;
1464	case 0x91d02027:
1465		/* Solaris 2.x gettimeofday */
1466		update_personality(tcp, 1);
1467		break;
1468	default:
1469# if defined(SPARC64)
1470		fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
1471# else
1472		fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
1473# endif
1474		return -1;
1475	}
1476
1477	/* Extract the system call number from the registers. */
1478	if (trap == 0x91d02027)
1479		scno = 156;
1480	else
1481		scno = sparc_regs.u_regs[U_REG_G1];
1482	if (scno == 0) {
1483		scno = sparc_regs.u_regs[U_REG_O0];
1484		memmove(&sparc_regs.u_regs[U_REG_O0], &sparc_regs.u_regs[U_REG_O1], 7*sizeof(sparc_regs.u_regs[0]));
1485	}
1486#elif defined(HPPA)
1487	if (upeek(tcp->pid, PT_GR20, &scno) < 0)
1488		return -1;
1489#elif defined(SH)
1490	/*
1491	 * In the new syscall ABI, the system call number is in R3.
1492	 */
1493	if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
1494		return -1;
1495
1496	if (scno < 0) {
1497		/* Odd as it may seem, a glibc bug has been known to cause
1498		   glibc to issue bogus negative syscall numbers.  So for
1499		   our purposes, make strace print what it *should* have been */
1500		long correct_scno = (scno & 0xff);
1501		if (debug_flag)
1502			fprintf(stderr,
1503				"Detected glibc bug: bogus system call"
1504				" number = %ld, correcting to %ld\n",
1505				scno,
1506				correct_scno);
1507		scno = correct_scno;
1508	}
1509#elif defined(SH64)
1510	if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
1511		return -1;
1512	scno &= 0xFFFF;
1513#elif defined(CRISV10) || defined(CRISV32)
1514	if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
1515		return -1;
1516#elif defined(TILE)
1517	unsigned int currpers;
1518	scno = tile_regs.regs[10];
1519# ifdef __tilepro__
1520	currpers = 1;
1521# else
1522#  ifndef PT_FLAGS_COMPAT
1523#   define PT_FLAGS_COMPAT 0x10000  /* from Linux 3.8 on */
1524#  endif
1525	if (tile_regs.flags & PT_FLAGS_COMPAT)
1526		currpers = 1;
1527	else
1528		currpers = 0;
1529# endif
1530	update_personality(tcp, currpers);
1531#elif defined(MICROBLAZE)
1532	if (upeek(tcp->pid, 0, &scno) < 0)
1533		return -1;
1534#elif defined(OR1K)
1535	scno = or1k_regs.gpr[11];
1536#elif defined(METAG)
1537	scno = metag_regs.dx[0][1];	/* syscall number in D1Re0 (D1.0) */
1538#elif defined(XTENSA)
1539	if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
1540		return -1;
1541# elif defined(ARC)
1542	scno = arc_regs.scratch.r8;
1543#endif
1544
1545	tcp->scno = scno;
1546	if (SCNO_IS_VALID(tcp->scno)) {
1547		tcp->s_ent = &sysent[scno];
1548		tcp->qual_flg = qual_flags[scno];
1549	} else {
1550		static const struct_sysent unknown = {
1551			.nargs = MAX_ARGS,
1552			.sys_flags = 0,
1553			.sys_func = printargs,
1554			.sys_name = "unknown", /* not used */
1555		};
1556		tcp->s_ent = &unknown;
1557		tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1558	}
1559	return 1;
1560}
1561
1562/*
1563 * Cannot rely on __kernel_[u]long_t being defined,
1564 * it is quite a recent feature of <asm/posix_types.h>.
1565 */
1566#ifdef __kernel_long_t
1567typedef __kernel_long_t kernel_long_t;
1568typedef __kernel_ulong_t kernel_ulong_t;
1569#else
1570# ifdef X32
1571typedef long long kernel_long_t;
1572typedef unsigned long long kernel_ulong_t;
1573# else
1574typedef long kernel_long_t;
1575typedef unsigned long kernel_ulong_t;
1576# endif
1577#endif
1578
1579/*
1580 * Check the syscall return value register value for whether it is
1581 * a negated errno code indicating an error, or a success return value.
1582 */
1583static inline bool
1584is_negated_errno(kernel_ulong_t val)
1585{
1586	/* Linux kernel defines MAX_ERRNO to 4095. */
1587	kernel_ulong_t max = -(kernel_long_t) 4095;
1588
1589#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1590	if (current_wordsize < sizeof(val)) {
1591		val = (uint32_t) val;
1592		max = (uint32_t) max;
1593	}
1594#elif defined X32
1595	/*
1596	 * current_wordsize is 4 even in personality 0 (native X32)
1597	 * but truncation _must not_ be done in it.
1598	 * can't check current_wordsize here!
1599	 */
1600	if (current_personality != 0) {
1601		val = (uint32_t) val;
1602		max = (uint32_t) max;
1603	}
1604#endif
1605
1606	return val >= max;
1607}
1608
1609/* Return -1 on error or 1 on success (never 0!) */
1610static int
1611get_syscall_args(struct tcb *tcp)
1612{
1613	int i, nargs;
1614
1615	nargs = tcp->s_ent->nargs;
1616
1617#if defined(S390) || defined(S390X)
1618	for (i = 0; i < nargs; ++i)
1619		if (upeek(tcp->pid, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1620			return -1;
1621#elif defined(ALPHA)
1622	for (i = 0; i < nargs; ++i)
1623		if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1624			return -1;
1625#elif defined(IA64)
1626	if (!ia64_ia32mode) {
1627		unsigned long *out0, cfm, sof, sol;
1628		long rbs_end;
1629		/* be backwards compatible with kernel < 2.4.4... */
1630#		ifndef PT_RBS_END
1631#		  define PT_RBS_END	PT_AR_BSP
1632#		endif
1633
1634		if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
1635			return -1;
1636		if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
1637			return -1;
1638
1639		sof = (cfm >> 0) & 0x7f;
1640		sol = (cfm >> 7) & 0x7f;
1641		out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1642
1643		for (i = 0; i < nargs; ++i) {
1644			if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1645				   sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1646				return -1;
1647		}
1648	} else {
1649		static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1650						      PT_R9  /* ECX = out1 */,
1651						      PT_R10 /* EDX = out2 */,
1652						      PT_R14 /* ESI = out3 */,
1653						      PT_R15 /* EDI = out4 */,
1654						      PT_R13 /* EBP = out5 */};
1655
1656		for (i = 0; i < nargs; ++i) {
1657			if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
1658				return -1;
1659			/* truncate away IVE sign-extension */
1660			tcp->u_arg[i] &= 0xffffffff;
1661		}
1662	}
1663#elif defined LINUX_MIPSN64
1664	(void)i;
1665	(void)nargs;
1666	tcp->u_arg[0] = mips_REG_A0;
1667	tcp->u_arg[1] = mips_REG_A1;
1668	tcp->u_arg[2] = mips_REG_A2;
1669	tcp->u_arg[3] = mips_REG_A3;
1670	tcp->u_arg[4] = mips_REG_A4;
1671	tcp->u_arg[5] = mips_REG_A5;
1672#elif defined LINUX_MIPSN32
1673	(void)i;
1674	(void)nargs;
1675	tcp->u_arg[0] = tcp->ext_arg[0] = mips_REG_A0;
1676	tcp->u_arg[1] = tcp->ext_arg[1] = mips_REG_A1;
1677	tcp->u_arg[2] = tcp->ext_arg[2] = mips_REG_A2;
1678	tcp->u_arg[3] = tcp->ext_arg[3] = mips_REG_A3;
1679	tcp->u_arg[4] = tcp->ext_arg[4] = mips_REG_A4;
1680	tcp->u_arg[5] = tcp->ext_arg[5] = mips_REG_A5;
1681#elif defined LINUX_MIPSO32
1682	(void)i;
1683	(void)nargs;
1684	tcp->u_arg[0] = mips_REG_A0;
1685	tcp->u_arg[1] = mips_REG_A1;
1686	tcp->u_arg[2] = mips_REG_A2;
1687	tcp->u_arg[3] = mips_REG_A3;
1688	if (nargs > 4) {
1689		umoven(tcp, mips_REG_SP + 4 * 4,
1690		       (nargs - 4) * sizeof(tcp->u_arg[0]),
1691		       (char *)(tcp->u_arg + 4));
1692	}
1693#elif defined(POWERPC)
1694	(void)i;
1695	(void)nargs;
1696	tcp->u_arg[0] = ppc_regs.orig_gpr3;
1697	tcp->u_arg[1] = ppc_regs.gpr[4];
1698	tcp->u_arg[2] = ppc_regs.gpr[5];
1699	tcp->u_arg[3] = ppc_regs.gpr[6];
1700	tcp->u_arg[4] = ppc_regs.gpr[7];
1701	tcp->u_arg[5] = ppc_regs.gpr[8];
1702#elif defined(SPARC) || defined(SPARC64)
1703	for (i = 0; i < nargs; ++i)
1704		tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
1705#elif defined(HPPA)
1706	for (i = 0; i < nargs; ++i)
1707		if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1708			return -1;
1709#elif defined(ARM) || defined(AARCH64)
1710# if defined(AARCH64)
1711	if (tcp->currpers == 1)
1712		for (i = 0; i < nargs; ++i)
1713			tcp->u_arg[i] = aarch64_regs.regs[i];
1714	else
1715# endif
1716	for (i = 0; i < nargs; ++i)
1717		tcp->u_arg[i] = arm_regs.uregs[i];
1718#elif defined(AVR32)
1719	(void)i;
1720	(void)nargs;
1721	tcp->u_arg[0] = avr32_regs.r12;
1722	tcp->u_arg[1] = avr32_regs.r11;
1723	tcp->u_arg[2] = avr32_regs.r10;
1724	tcp->u_arg[3] = avr32_regs.r9;
1725	tcp->u_arg[4] = avr32_regs.r5;
1726	tcp->u_arg[5] = avr32_regs.r3;
1727#elif defined(BFIN)
1728	static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
1729
1730	for (i = 0; i < nargs; ++i)
1731		if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
1732			return -1;
1733#elif defined(SH)
1734	static const int syscall_regs[MAX_ARGS] = {
1735		4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1736		4 * (REG_REG0+7), 4 * (REG_REG0  ), 4 * (REG_REG0+1)
1737	};
1738
1739	for (i = 0; i < nargs; ++i)
1740		if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
1741			return -1;
1742#elif defined(SH64)
1743	int i;
1744	/* Registers used by SH5 Linux system calls for parameters */
1745	static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
1746
1747	for (i = 0; i < nargs; ++i)
1748		if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1749			return -1;
1750#elif defined(I386)
1751	(void)i;
1752	(void)nargs;
1753	tcp->u_arg[0] = i386_regs.ebx;
1754	tcp->u_arg[1] = i386_regs.ecx;
1755	tcp->u_arg[2] = i386_regs.edx;
1756	tcp->u_arg[3] = i386_regs.esi;
1757	tcp->u_arg[4] = i386_regs.edi;
1758	tcp->u_arg[5] = i386_regs.ebp;
1759#elif defined(X86_64) || defined(X32)
1760	(void)i;
1761	(void)nargs;
1762	if (x86_io.iov_len != sizeof(i386_regs)) {
1763		/* x86-64 or x32 ABI */
1764		tcp->u_arg[0] = x86_64_regs.rdi;
1765		tcp->u_arg[1] = x86_64_regs.rsi;
1766		tcp->u_arg[2] = x86_64_regs.rdx;
1767		tcp->u_arg[3] = x86_64_regs.r10;
1768		tcp->u_arg[4] = x86_64_regs.r8;
1769		tcp->u_arg[5] = x86_64_regs.r9;
1770#  ifdef X32
1771		tcp->ext_arg[0] = x86_64_regs.rdi;
1772		tcp->ext_arg[1] = x86_64_regs.rsi;
1773		tcp->ext_arg[2] = x86_64_regs.rdx;
1774		tcp->ext_arg[3] = x86_64_regs.r10;
1775		tcp->ext_arg[4] = x86_64_regs.r8;
1776		tcp->ext_arg[5] = x86_64_regs.r9;
1777#  endif
1778	} else {
1779		/* i386 ABI */
1780		/* Zero-extend from 32 bits */
1781		/* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1782		 * if you need to use *sign-extended* parameter.
1783		 */
1784		tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1785		tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1786		tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1787		tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1788		tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1789		tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
1790	}
1791#elif defined(MICROBLAZE)
1792	for (i = 0; i < nargs; ++i)
1793		if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1794			return -1;
1795#elif defined(CRISV10) || defined(CRISV32)
1796	static const int crisregs[MAX_ARGS] = {
1797		4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
1798		4*PT_R13     , 4*PT_MOF, 4*PT_SRP
1799	};
1800
1801	for (i = 0; i < nargs; ++i)
1802		if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
1803			return -1;
1804#elif defined(TILE)
1805	for (i = 0; i < nargs; ++i)
1806		tcp->u_arg[i] = tile_regs.regs[i];
1807#elif defined(M68K)
1808	for (i = 0; i < nargs; ++i)
1809		if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1810			return -1;
1811#elif defined(OR1K)
1812	(void)nargs;
1813	for (i = 0; i < 6; ++i)
1814		tcp->u_arg[i] = or1k_regs.gpr[3 + i];
1815#elif defined(METAG)
1816	for (i = 0; i < nargs; i++)
1817		/* arguments go backwards from D1Ar1 (D1.3) */
1818		tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
1819#elif defined(XTENSA)
1820	/* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
1821	static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
1822	for (i = 0; i < nargs; ++i)
1823		if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
1824			return -1;
1825# elif defined(ARC)
1826	long *arc_args = &arc_regs.scratch.r0;
1827	for (i = 0; i < nargs; ++i)
1828		tcp->u_arg[i] = *arc_args--;
1829
1830#else /* Other architecture (32bits specific) */
1831	for (i = 0; i < nargs; ++i)
1832		if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
1833			return -1;
1834#endif
1835	return 1;
1836}
1837
1838static int
1839trace_syscall_entering(struct tcb *tcp)
1840{
1841	int res, scno_good;
1842
1843	scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
1844	if (res == 0)
1845		return res;
1846	if (res == 1)
1847		res = get_syscall_args(tcp);
1848
1849	if (res != 1) {
1850		printleader(tcp);
1851		if (scno_good != 1)
1852			tprints("????" /* anti-trigraph gap */ "(");
1853		else if (tcp->qual_flg & UNDEFINED_SCNO)
1854			tprintf("%s(", undefined_scno_name(tcp));
1855		else
1856			tprintf("%s(", tcp->s_ent->sys_name);
1857		/*
1858		 * " <unavailable>" will be added later by the code which
1859		 * detects ptrace errors.
1860		 */
1861		goto ret;
1862	}
1863
1864	if (   sys_execve == tcp->s_ent->sys_func
1865# if defined(SPARC) || defined(SPARC64)
1866	    || sys_execv == tcp->s_ent->sys_func
1867# endif
1868	   ) {
1869		hide_log_until_execve = 0;
1870	}
1871
1872#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
1873	while (1) {
1874# ifdef SYS_socket_subcall
1875		if (tcp->s_ent->sys_func == sys_socketcall) {
1876			decode_socket_subcall(tcp);
1877			break;
1878		}
1879# endif
1880# ifdef SYS_ipc_subcall
1881		if (tcp->s_ent->sys_func == sys_ipc) {
1882			decode_ipc_subcall(tcp);
1883			break;
1884		}
1885# endif
1886		break;
1887	}
1888#endif
1889
1890	if (!(tcp->qual_flg & QUAL_TRACE)
1891	 || (tracing_paths && !pathtrace_match(tcp))
1892	) {
1893		tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1894		return 0;
1895	}
1896
1897	tcp->flags &= ~TCB_FILTERED;
1898
1899	if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
1900		res = 0;
1901		goto ret;
1902	}
1903
1904#ifdef USE_LIBUNWIND
1905	if (stack_trace_enabled) {
1906		if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
1907			unwind_capture_stacktrace(tcp);
1908	}
1909#endif
1910
1911	printleader(tcp);
1912	if (tcp->qual_flg & UNDEFINED_SCNO)
1913		tprintf("%s(", undefined_scno_name(tcp));
1914	else
1915		tprintf("%s(", tcp->s_ent->sys_name);
1916	if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
1917		res = printargs(tcp);
1918	else
1919		res = tcp->s_ent->sys_func(tcp);
1920
1921	fflush(tcp->outf);
1922 ret:
1923	tcp->flags |= TCB_INSYSCALL;
1924	/* Measure the entrance time as late as possible to avoid errors. */
1925	if (Tflag || cflag)
1926		gettimeofday(&tcp->etime, NULL);
1927	return res;
1928}
1929
1930/* Returns:
1931 * 1: ok, continue in trace_syscall_exiting().
1932 * -1: error, trace_syscall_exiting() should print error indicator
1933 *    ("????" etc) and bail out.
1934 */
1935static int
1936get_syscall_result(struct tcb *tcp)
1937{
1938#if defined ARCH_REGS_FOR_GETREGSET || defined ARCH_REGS_FOR_GETREGS
1939	/* already done by get_regs */
1940#elif defined(S390) || defined(S390X)
1941	if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
1942		return -1;
1943#elif defined(BFIN)
1944	if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
1945		return -1;
1946#elif defined(IA64)
1947#	define IA64_PSR_IS	((long)1 << 34)
1948	long psr;
1949	if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
1950		ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
1951	if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
1952		return -1;
1953	if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
1954		return -1;
1955#elif defined(M68K)
1956	if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
1957		return -1;
1958#elif defined(ALPHA)
1959	if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
1960		return -1;
1961	if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
1962		return -1;
1963#elif defined(HPPA)
1964	if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
1965		return -1;
1966#elif defined(SH)
1967	/* new syscall ABI returns result in R0 */
1968	if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
1969		return -1;
1970#elif defined(SH64)
1971	/* ABI defines result returned in r9 */
1972	if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
1973		return -1;
1974#elif defined(CRISV10) || defined(CRISV32)
1975	if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
1976		return -1;
1977#elif defined(MICROBLAZE)
1978	if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
1979		return -1;
1980#elif defined(XTENSA)
1981	if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
1982		return -1;
1983#else
1984# error get_syscall_result is not implemented for this architecture
1985#endif
1986	return 1;
1987}
1988
1989/* Returns:
1990 * 1: ok, continue in trace_syscall_exiting().
1991 * -1: error, trace_syscall_exiting() should print error indicator
1992 *    ("????" etc) and bail out.
1993 */
1994static void
1995get_error(struct tcb *tcp)
1996{
1997	int u_error = 0;
1998	int check_errno = 1;
1999	if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
2000		check_errno = 0;
2001	}
2002#if defined(S390) || defined(S390X)
2003	if (check_errno && is_negated_errno(s390_gpr2)) {
2004		tcp->u_rval = -1;
2005		u_error = -s390_gpr2;
2006	}
2007	else {
2008		tcp->u_rval = s390_gpr2;
2009	}
2010#elif defined(I386)
2011	if (check_errno && is_negated_errno(i386_regs.eax)) {
2012		tcp->u_rval = -1;
2013		u_error = -i386_regs.eax;
2014	}
2015	else {
2016		tcp->u_rval = i386_regs.eax;
2017	}
2018#elif defined(X86_64) || defined(X32)
2019	/*
2020	 * In X32, return value is 64-bit (llseek uses one).
2021	 * Using merely "long rax" would not work.
2022	 */
2023	kernel_long_t rax;
2024
2025	if (x86_io.iov_len == sizeof(i386_regs)) {
2026		/* Sign extend from 32 bits */
2027		rax = (int32_t) i386_regs.eax;
2028	} else {
2029		rax = x86_64_regs.rax;
2030	}
2031	if (check_errno && is_negated_errno(rax)) {
2032		tcp->u_rval = -1;
2033		u_error = -rax;
2034	}
2035	else {
2036		tcp->u_rval = rax;
2037# ifdef X32
2038		/* tcp->u_rval contains a truncated value */
2039		tcp->u_lrval = rax;
2040# endif
2041	}
2042#elif defined(IA64)
2043	if (ia64_ia32mode) {
2044		int err;
2045
2046		err = (int)ia64_r8;
2047		if (check_errno && is_negated_errno(err)) {
2048			tcp->u_rval = -1;
2049			u_error = -err;
2050		}
2051		else {
2052			tcp->u_rval = err;
2053		}
2054	} else {
2055		if (check_errno && ia64_r10) {
2056			tcp->u_rval = -1;
2057			u_error = ia64_r8;
2058		} else {
2059			tcp->u_rval = ia64_r8;
2060		}
2061	}
2062#elif defined(MIPS)
2063	if (check_errno && mips_REG_A3) {
2064		tcp->u_rval = -1;
2065		u_error = mips_REG_V0;
2066	} else {
2067# if defined LINUX_MIPSN32
2068		tcp->u_lrval = mips_REG_V0;
2069# endif
2070		tcp->u_rval = mips_REG_V0;
2071	}
2072#elif defined(POWERPC)
2073	if (check_errno && (ppc_regs.ccr & 0x10000000)) {
2074		tcp->u_rval = -1;
2075		u_error = ppc_regs.gpr[3];
2076	}
2077	else {
2078		tcp->u_rval = ppc_regs.gpr[3];
2079	}
2080#elif defined(M68K)
2081	if (check_errno && is_negated_errno(m68k_d0)) {
2082		tcp->u_rval = -1;
2083		u_error = -m68k_d0;
2084	}
2085	else {
2086		tcp->u_rval = m68k_d0;
2087	}
2088#elif defined(ARM) || defined(AARCH64)
2089# if defined(AARCH64)
2090	if (tcp->currpers == 1) {
2091		if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2092			tcp->u_rval = -1;
2093			u_error = -aarch64_regs.regs[0];
2094		}
2095		else {
2096			tcp->u_rval = aarch64_regs.regs[0];
2097		}
2098	}
2099	else
2100# endif
2101	{
2102		if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
2103			tcp->u_rval = -1;
2104			u_error = -arm_regs.ARM_r0;
2105		}
2106		else {
2107			tcp->u_rval = arm_regs.ARM_r0;
2108		}
2109	}
2110#elif defined(AVR32)
2111	if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
2112		tcp->u_rval = -1;
2113		u_error = -avr32_regs.r12;
2114	}
2115	else {
2116		tcp->u_rval = avr32_regs.r12;
2117	}
2118#elif defined(BFIN)
2119	if (check_errno && is_negated_errno(bfin_r0)) {
2120		tcp->u_rval = -1;
2121		u_error = -bfin_r0;
2122	} else {
2123		tcp->u_rval = bfin_r0;
2124	}
2125#elif defined(ALPHA)
2126	if (check_errno && alpha_a3) {
2127		tcp->u_rval = -1;
2128		u_error = alpha_r0;
2129	}
2130	else {
2131		tcp->u_rval = alpha_r0;
2132	}
2133#elif defined(SPARC)
2134	if (check_errno && sparc_regs.psr & PSR_C) {
2135		tcp->u_rval = -1;
2136		u_error = sparc_regs.u_regs[U_REG_O0];
2137	}
2138	else {
2139		tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
2140	}
2141#elif defined(SPARC64)
2142	if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
2143		tcp->u_rval = -1;
2144		u_error = sparc_regs.u_regs[U_REG_O0];
2145	}
2146	else {
2147		tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
2148	}
2149#elif defined(HPPA)
2150	if (check_errno && is_negated_errno(hppa_r28)) {
2151		tcp->u_rval = -1;
2152		u_error = -hppa_r28;
2153	}
2154	else {
2155		tcp->u_rval = hppa_r28;
2156	}
2157#elif defined(SH)
2158	if (check_errno && is_negated_errno(sh_r0)) {
2159		tcp->u_rval = -1;
2160		u_error = -sh_r0;
2161	}
2162	else {
2163		tcp->u_rval = sh_r0;
2164	}
2165#elif defined(SH64)
2166	if (check_errno && is_negated_errno(sh64_r9)) {
2167		tcp->u_rval = -1;
2168		u_error = -sh64_r9;
2169	}
2170	else {
2171		tcp->u_rval = sh64_r9;
2172	}
2173#elif defined(METAG)
2174	/* result pointer in D0Re0 (D0.0) */
2175	if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2176		tcp->u_rval = -1;
2177		u_error = -metag_regs.dx[0][0];
2178	}
2179	else {
2180		tcp->u_rval = metag_regs.dx[0][0];
2181	}
2182#elif defined(CRISV10) || defined(CRISV32)
2183	if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
2184		tcp->u_rval = -1;
2185		u_error = -cris_r10;
2186	}
2187	else {
2188		tcp->u_rval = cris_r10;
2189	}
2190#elif defined(TILE)
2191	/*
2192	 * The standard tile calling convention returns the value (or negative
2193	 * errno) in r0, and zero (or positive errno) in r1.
2194	 * Until at least kernel 3.8, however, the r1 value is not reflected
2195	 * in ptregs at this point, so we use r0 here.
2196	 */
2197	if (check_errno && is_negated_errno(tile_regs.regs[0])) {
2198		tcp->u_rval = -1;
2199		u_error = -tile_regs.regs[0];
2200	} else {
2201		tcp->u_rval = tile_regs.regs[0];
2202	}
2203#elif defined(MICROBLAZE)
2204	if (check_errno && is_negated_errno(microblaze_r3)) {
2205		tcp->u_rval = -1;
2206		u_error = -microblaze_r3;
2207	}
2208	else {
2209		tcp->u_rval = microblaze_r3;
2210	}
2211#elif defined(OR1K)
2212	if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2213		tcp->u_rval = -1;
2214		u_error = -or1k_regs.gpr[11];
2215	}
2216	else {
2217		tcp->u_rval = or1k_regs.gpr[11];
2218	}
2219#elif defined(XTENSA)
2220	if (check_errno && is_negated_errno(xtensa_a2)) {
2221		tcp->u_rval = -1;
2222		u_error = -xtensa_a2;
2223	}
2224	else {
2225		tcp->u_rval = xtensa_a2;
2226	}
2227#elif defined(ARC)
2228	if (check_errno && is_negated_errno(arc_regs.scratch.r0)) {
2229		tcp->u_rval = -1;
2230		u_error = -arc_regs.scratch.r0;
2231	}
2232	else {
2233		tcp->u_rval = arc_regs.scratch.r0;
2234	}
2235#endif
2236	tcp->u_error = u_error;
2237}
2238
2239static void
2240dumpio(struct tcb *tcp)
2241{
2242	int (*func)();
2243
2244	if (syserror(tcp))
2245		return;
2246	if ((unsigned long) tcp->u_arg[0] >= num_quals)
2247		return;
2248	func = tcp->s_ent->sys_func;
2249	if (func == printargs)
2250		return;
2251	if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
2252		if (func == sys_read ||
2253		    func == sys_pread ||
2254		    func == sys_recv ||
2255		    func == sys_recvfrom) {
2256			dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
2257			return;
2258		} else if (func == sys_readv) {
2259			dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2260			return;
2261#if HAVE_SENDMSG
2262		} else if (func == sys_recvmsg) {
2263			dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
2264			return;
2265		} else if (func == sys_recvmmsg) {
2266			dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
2267			return;
2268#endif
2269		}
2270	}
2271	if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
2272		if (func == sys_write ||
2273		    func == sys_pwrite ||
2274		    func == sys_send ||
2275		    func == sys_sendto)
2276			dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
2277		else if (func == sys_writev)
2278			dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2279#if HAVE_SENDMSG
2280		else if (func == sys_sendmsg)
2281			dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
2282		else if (func == sys_sendmmsg)
2283			dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
2284#endif
2285	}
2286}
2287
2288static int
2289trace_syscall_exiting(struct tcb *tcp)
2290{
2291	int sys_res;
2292	struct timeval tv;
2293	int res;
2294	long u_error;
2295
2296	/* Measure the exit time as early as possible to avoid errors. */
2297	if (Tflag || cflag)
2298		gettimeofday(&tv, NULL);
2299
2300#ifdef USE_LIBUNWIND
2301	if (stack_trace_enabled) {
2302		if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
2303			unwind_cache_invalidate(tcp);
2304	}
2305#endif
2306
2307#if SUPPORTED_PERSONALITIES > 1
2308	update_personality(tcp, tcp->currpers);
2309#endif
2310	res = (get_regs_error ? -1 : get_syscall_result(tcp));
2311	if (res == 1) {
2312		get_error(tcp); /* never fails */
2313		if (filtered(tcp) || hide_log_until_execve)
2314			goto ret;
2315	}
2316
2317	if (cflag) {
2318		count_syscall(tcp, &tv);
2319		if (cflag == CFLAG_ONLY_STATS) {
2320			goto ret;
2321		}
2322	}
2323
2324	/* If not in -ff mode, and printing_tcp != tcp,
2325	 * then the log currently does not end with output
2326	 * of _our syscall entry_, but with something else.
2327	 * We need to say which syscall's return is this.
2328	 *
2329	 * Forced reprinting via TCB_REPRINT is used only by
2330	 * "strace -ff -oLOG test/threaded_execve" corner case.
2331	 * It's the only case when -ff mode needs reprinting.
2332	 */
2333	if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2334		tcp->flags &= ~TCB_REPRINT;
2335		printleader(tcp);
2336		if (tcp->qual_flg & UNDEFINED_SCNO)
2337			tprintf("<... %s resumed> ", undefined_scno_name(tcp));
2338		else
2339			tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
2340	}
2341	printing_tcp = tcp;
2342
2343	if (res != 1) {
2344		/* There was error in one of prior ptrace ops */
2345		tprints(") ");
2346		tabto();
2347		tprints("= ? <unavailable>\n");
2348		line_ended();
2349		tcp->flags &= ~TCB_INSYSCALL;
2350		return res;
2351	}
2352
2353	sys_res = 0;
2354	if (tcp->qual_flg & QUAL_RAW) {
2355		/* sys_res = printargs(tcp); - but it's nop on sysexit */
2356	} else {
2357	/* FIXME: not_failing_only (IOW, option -z) is broken:
2358	 * failure of syscall is known only after syscall return.
2359	 * Thus we end up with something like this on, say, ENOENT:
2360	 *     open("doesnt_exist", O_RDONLY <unfinished ...>
2361	 *     {next syscall decode}
2362	 * whereas the intended result is that open(...) line
2363	 * is not shown at all.
2364	 */
2365		if (not_failing_only && tcp->u_error)
2366			goto ret;	/* ignore failed syscalls */
2367		sys_res = tcp->s_ent->sys_func(tcp);
2368	}
2369
2370	tprints(") ");
2371	tabto();
2372	u_error = tcp->u_error;
2373	if (tcp->qual_flg & QUAL_RAW) {
2374		if (u_error)
2375			tprintf("= -1 (errno %ld)", u_error);
2376		else
2377			tprintf("= %#lx", tcp->u_rval);
2378	}
2379	else if (!(sys_res & RVAL_NONE) && u_error) {
2380		switch (u_error) {
2381		/* Blocked signals do not interrupt any syscalls.
2382		 * In this case syscalls don't return ERESTARTfoo codes.
2383		 *
2384		 * Deadly signals set to SIG_DFL interrupt syscalls
2385		 * and kill the process regardless of which of the codes below
2386		 * is returned by the interrupted syscall.
2387		 * In some cases, kernel forces a kernel-generated deadly
2388		 * signal to be unblocked and set to SIG_DFL (and thus cause
2389		 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2390		 * or SIGILL. (The alternative is to leave process spinning
2391		 * forever on the faulty instruction - not useful).
2392		 *
2393		 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2394		 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2395		 * but kernel will always restart them.
2396		 */
2397		case ERESTARTSYS:
2398			/* Most common type of signal-interrupted syscall exit code.
2399			 * The system call will be restarted with the same arguments
2400			 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2401			 */
2402			tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
2403			break;
2404		case ERESTARTNOINTR:
2405			/* Rare. For example, fork() returns this if interrupted.
2406			 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2407			 */
2408			tprints("= ? ERESTARTNOINTR (To be restarted)");
2409			break;
2410		case ERESTARTNOHAND:
2411			/* pause(), rt_sigsuspend() etc use this code.
2412			 * SA_RESTART is ignored (assumed not set):
2413			 * syscall won't restart (will return EINTR instead)
2414			 * even after signal with SA_RESTART set. However,
2415			 * after SIG_IGN or SIG_DFL signal it will restart
2416			 * (thus the name "restart only if has no handler").
2417			 */
2418			tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
2419			break;
2420		case ERESTART_RESTARTBLOCK:
2421			/* Syscalls like nanosleep(), poll() which can't be
2422			 * restarted with their original arguments use this
2423			 * code. Kernel will execute restart_syscall() instead,
2424			 * which changes arguments before restarting syscall.
2425			 * SA_RESTART is ignored (assumed not set) similarly
2426			 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2427			 * since restart data is saved in "restart block"
2428			 * in task struct, and if signal handler uses a syscall
2429			 * which in turn saves another such restart block,
2430			 * old data is lost and restart becomes impossible)
2431			 */
2432			tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
2433			break;
2434		default:
2435			if ((unsigned long) u_error < nerrnos
2436			    && errnoent[u_error])
2437				tprintf("= -1 %s (%s)", errnoent[u_error],
2438					strerror(u_error));
2439			else
2440				tprintf("= -1 ERRNO_%lu (%s)", u_error,
2441					strerror(u_error));
2442			break;
2443		}
2444		if ((sys_res & RVAL_STR) && tcp->auxstr)
2445			tprintf(" (%s)", tcp->auxstr);
2446	}
2447	else {
2448		if (sys_res & RVAL_NONE)
2449			tprints("= ?");
2450		else {
2451			switch (sys_res & RVAL_MASK) {
2452			case RVAL_HEX:
2453				tprintf("= %#lx", tcp->u_rval);
2454				break;
2455			case RVAL_OCTAL:
2456				tprintf("= %#lo", tcp->u_rval);
2457				break;
2458			case RVAL_UDECIMAL:
2459				tprintf("= %lu", tcp->u_rval);
2460				break;
2461			case RVAL_DECIMAL:
2462				tprintf("= %ld", tcp->u_rval);
2463				break;
2464			case RVAL_FD:
2465				if (show_fd_path) {
2466					tprints("= ");
2467					printfd(tcp, tcp->u_rval);
2468				}
2469				else
2470					tprintf("= %ld", tcp->u_rval);
2471				break;
2472#if defined(LINUX_MIPSN32) || defined(X32)
2473			/*
2474			case RVAL_LHEX:
2475				tprintf("= %#llx", tcp->u_lrval);
2476				break;
2477			case RVAL_LOCTAL:
2478				tprintf("= %#llo", tcp->u_lrval);
2479				break;
2480			*/
2481			case RVAL_LUDECIMAL:
2482				tprintf("= %llu", tcp->u_lrval);
2483				break;
2484			/*
2485			case RVAL_LDECIMAL:
2486				tprintf("= %lld", tcp->u_lrval);
2487				break;
2488			*/
2489#endif
2490			default:
2491				fprintf(stderr,
2492					"invalid rval format\n");
2493				break;
2494			}
2495		}
2496		if ((sys_res & RVAL_STR) && tcp->auxstr)
2497			tprintf(" (%s)", tcp->auxstr);
2498	}
2499	if (Tflag) {
2500		tv_sub(&tv, &tv, &tcp->etime);
2501		tprintf(" <%ld.%06ld>",
2502			(long) tv.tv_sec, (long) tv.tv_usec);
2503	}
2504	tprints("\n");
2505	dumpio(tcp);
2506	line_ended();
2507
2508#ifdef USE_LIBUNWIND
2509	if (stack_trace_enabled)
2510		unwind_print_stacktrace(tcp);
2511#endif
2512
2513 ret:
2514	tcp->flags &= ~TCB_INSYSCALL;
2515	return 0;
2516}
2517
2518int
2519trace_syscall(struct tcb *tcp)
2520{
2521	return exiting(tcp) ?
2522		trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2523}
2524