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>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
36 #include <sys/param.h>
40 # ifndef PTRACE_PEEKUSR
41 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
43 #elif defined(HAVE_LINUX_PTRACE_H)
44 # undef PTRACE_SYSCALL
45 # ifdef HAVE_STRUCT_IA64_FPREG
46 # define ia64_fpreg XXX_ia64_fpreg
48 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
49 # define pt_all_user_regs XXX_pt_all_user_regs
51 # include <linux/ptrace.h>
53 # undef pt_all_user_regs
57 # undef PTRACE_GETREGS
58 # define PTRACE_GETREGS PTRACE_GETREGS64
59 # undef PTRACE_SETREGS
60 # define PTRACE_SETREGS PTRACE_SETREGS64
64 # include <asm/ptrace_offsets.h>
69 # define ERESTARTSYS 512
71 #ifndef ERESTARTNOINTR
72 # define ERESTARTNOINTR 513
74 #ifndef ERESTARTNOHAND
75 # define ERESTARTNOHAND 514 /* restart if no handler */
77 #ifndef ERESTART_RESTARTBLOCK
78 # define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
82 # warning: NSIG is not defined, using 32
86 /* Ugh. Is this really correct? ARM has no RT signals?! */
93 /* Define these shorthand notations to simplify the syscallent files. */
97 #define TN TRACE_NETWORK
98 #define TP TRACE_PROCESS
99 #define TS TRACE_SIGNAL
100 #define NF SYSCALL_NEVER_FAILS
103 static const struct sysent sysent0[] = {
104 #include "syscallent.h"
107 #if SUPPORTED_PERSONALITIES >= 2
108 static const struct sysent sysent1[] = {
109 # include "syscallent1.h"
113 #if SUPPORTED_PERSONALITIES >= 3
114 static const struct sysent sysent2[] = {
115 # include "syscallent2.h"
119 /* Now undef them since short defines cause wicked namespace pollution. */
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
137 static const char *const errnoent0[] = {
138 #include "errnoent.h"
140 static const char *const signalent0[] = {
141 #include "signalent.h"
143 static const struct ioctlent ioctlent0[] = {
144 #include "ioctlent.h"
146 enum { nsyscalls0 = ARRAY_SIZE(sysent0) };
147 enum { nerrnos0 = ARRAY_SIZE(errnoent0) };
148 enum { nsignals0 = ARRAY_SIZE(signalent0) };
149 enum { nioctlents0 = ARRAY_SIZE(ioctlent0) };
150 int qual_flags0[MAX_QUALS];
152 #if SUPPORTED_PERSONALITIES >= 2
153 static const char *const errnoent1[] = {
154 # include "errnoent1.h"
156 static const char *const signalent1[] = {
157 # include "signalent1.h"
159 static const struct ioctlent ioctlent1[] = {
160 # include "ioctlent1.h"
162 enum { nsyscalls1 = ARRAY_SIZE(sysent1) };
163 enum { nerrnos1 = ARRAY_SIZE(errnoent1) };
164 enum { nsignals1 = ARRAY_SIZE(signalent1) };
165 enum { nioctlents1 = ARRAY_SIZE(ioctlent1) };
166 int qual_flags1[MAX_QUALS];
169 #if SUPPORTED_PERSONALITIES >= 3
170 static const char *const errnoent2[] = {
171 # include "errnoent2.h"
173 static const char *const signalent2[] = {
174 # include "signalent2.h"
176 static const struct ioctlent ioctlent2[] = {
177 # include "ioctlent2.h"
179 enum { nsyscalls2 = ARRAY_SIZE(sysent2) };
180 enum { nerrnos2 = ARRAY_SIZE(errnoent2) };
181 enum { nsignals2 = ARRAY_SIZE(signalent2) };
182 enum { nioctlents2 = ARRAY_SIZE(ioctlent2) };
183 int qual_flags2[MAX_QUALS];
186 const struct sysent *sysent;
187 const char *const *errnoent;
188 const char *const *signalent;
189 const struct ioctlent *ioctlent;
196 int current_personality;
198 #ifndef PERSONALITY0_WORDSIZE
199 # define PERSONALITY0_WORDSIZE sizeof(long)
201 const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
202 PERSONALITY0_WORDSIZE,
203 #if SUPPORTED_PERSONALITIES > 1
204 PERSONALITY1_WORDSIZE,
206 #if SUPPORTED_PERSONALITIES > 2
207 PERSONALITY2_WORDSIZE,
212 set_personality(int personality)
214 switch (personality) {
216 errnoent = errnoent0;
219 nsyscalls = nsyscalls0;
220 ioctlent = ioctlent0;
221 nioctlents = nioctlents0;
222 signalent = signalent0;
223 nsignals = nsignals0;
224 qual_flags = qual_flags0;
227 #if SUPPORTED_PERSONALITIES >= 2
229 errnoent = errnoent1;
232 nsyscalls = nsyscalls1;
233 ioctlent = ioctlent1;
234 nioctlents = nioctlents1;
235 signalent = signalent1;
236 nsignals = nsignals1;
237 qual_flags = qual_flags1;
241 #if SUPPORTED_PERSONALITIES >= 3
243 errnoent = errnoent2;
246 nsyscalls = nsyscalls2;
247 ioctlent = ioctlent2;
248 nioctlents = nioctlents2;
249 signalent = signalent2;
250 nsignals = nsignals2;
251 qual_flags = qual_flags2;
256 current_personality = personality;
259 #if SUPPORTED_PERSONALITIES > 1
261 update_personality(struct tcb *tcp, int personality)
263 if (personality == current_personality)
265 set_personality(personality);
267 if (personality == tcp->currpers)
269 tcp->currpers = personality;
271 # if defined(POWERPC64) || defined(X86_64)
273 static const char *const names[] = {"64 bit", "32 bit"};
274 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
275 tcp->pid, names[personality]);
281 static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
283 static const struct qual_options {
285 const char *option_name;
286 int (*qualify)(const char *, int, int);
287 const char *argument_name;
289 { QUAL_TRACE, "trace", qual_syscall, "system call" },
290 { QUAL_TRACE, "t", qual_syscall, "system call" },
291 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
292 { QUAL_ABBREV, "a", qual_syscall, "system call" },
293 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
294 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
295 { QUAL_RAW, "raw", qual_syscall, "system call" },
296 { QUAL_RAW, "x", qual_syscall, "system call" },
297 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
298 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
299 { QUAL_SIGNAL, "s", qual_signal, "signal" },
300 { QUAL_FAULT, "fault", qual_fault, "fault" },
301 { QUAL_FAULT, "faults", qual_fault, "fault" },
302 { QUAL_FAULT, "m", qual_fault, "fault" },
303 { QUAL_READ, "read", qual_desc, "descriptor" },
304 { QUAL_READ, "reads", qual_desc, "descriptor" },
305 { QUAL_READ, "r", qual_desc, "descriptor" },
306 { QUAL_WRITE, "write", qual_desc, "descriptor" },
307 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
308 { QUAL_WRITE, "w", qual_desc, "descriptor" },
309 { 0, NULL, NULL, NULL },
313 qualify_one(int n, int bitflag, int not, int pers)
315 if (pers == 0 || pers < 0) {
317 qual_flags0[n] &= ~bitflag;
319 qual_flags0[n] |= bitflag;
322 #if SUPPORTED_PERSONALITIES >= 2
323 if (pers == 1 || pers < 0) {
325 qual_flags1[n] &= ~bitflag;
327 qual_flags1[n] |= bitflag;
331 #if SUPPORTED_PERSONALITIES >= 3
332 if (pers == 2 || pers < 0) {
334 qual_flags2[n] &= ~bitflag;
336 qual_flags2[n] |= bitflag;
342 qual_syscall(const char *s, int bitflag, int not)
347 if (isdigit((unsigned char)*s)) {
349 if (i < 0 || i >= MAX_QUALS)
351 qualify_one(i, bitflag, not, -1);
354 for (i = 0; i < nsyscalls0; i++)
355 if (strcmp(s, sysent0[i].sys_name) == 0) {
356 qualify_one(i, bitflag, not, 0);
360 #if SUPPORTED_PERSONALITIES >= 2
361 for (i = 0; i < nsyscalls1; i++)
362 if (strcmp(s, sysent1[i].sys_name) == 0) {
363 qualify_one(i, bitflag, not, 1);
368 #if SUPPORTED_PERSONALITIES >= 3
369 for (i = 0; i < nsyscalls2; i++)
370 if (strcmp(s, sysent2[i].sys_name) == 0) {
371 qualify_one(i, bitflag, not, 2);
380 qual_signal(const char *s, int bitflag, int not)
385 if (isdigit((unsigned char)*s)) {
387 if (signo < 0 || signo >= MAX_QUALS)
389 qualify_one(signo, bitflag, not, -1);
392 if (strlen(s) >= sizeof buf)
396 if (strncasecmp(s, "SIG", 3) == 0)
398 for (i = 0; i <= NSIG; i++)
399 if (strcasecmp(s, signame(i) + 3) == 0) {
400 qualify_one(i, bitflag, not, -1);
407 qual_fault(const char *s, int bitflag, int not)
413 qual_desc(const char *s, int bitflag, int not)
415 if (isdigit((unsigned char)*s)) {
417 if (desc < 0 || desc >= MAX_QUALS)
419 qualify_one(desc, bitflag, not, -1);
426 lookup_class(const char *s)
428 if (strcmp(s, "file") == 0)
430 if (strcmp(s, "ipc") == 0)
432 if (strcmp(s, "network") == 0)
433 return TRACE_NETWORK;
434 if (strcmp(s, "process") == 0)
435 return TRACE_PROCESS;
436 if (strcmp(s, "signal") == 0)
438 if (strcmp(s, "desc") == 0)
444 qualify(const char *s)
446 const struct qual_options *opt;
452 opt = &qual_options[0];
453 for (i = 0; (p = qual_options[i].option_name); i++) {
455 if (strncmp(s, p, n) == 0 && s[n] == '=') {
456 opt = &qual_options[i];
466 if (strcmp(s, "none") == 0) {
470 if (strcmp(s, "all") == 0) {
471 for (i = 0; i < MAX_QUALS; i++) {
472 qualify_one(i, opt->bitflag, not, -1);
476 for (i = 0; i < MAX_QUALS; i++) {
477 qualify_one(i, opt->bitflag, !not, -1);
482 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
483 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
484 for (i = 0; i < nsyscalls0; i++)
485 if (sysent0[i].sys_flags & n)
486 qualify_one(i, opt->bitflag, not, 0);
488 #if SUPPORTED_PERSONALITIES >= 2
489 for (i = 0; i < nsyscalls1; i++)
490 if (sysent1[i].sys_flags & n)
491 qualify_one(i, opt->bitflag, not, 1);
494 #if SUPPORTED_PERSONALITIES >= 3
495 for (i = 0; i < nsyscalls2; i++)
496 if (sysent2[i].sys_flags & n)
497 qualify_one(i, opt->bitflag, not, 2);
502 if (opt->qualify(p, opt->bitflag, not)) {
503 error_msg_and_die("invalid %s '%s'",
504 opt->argument_name, p);
511 #ifdef SYS_socket_subcall
513 decode_socket_subcall(struct tcb *tcp)
516 unsigned int i, size;
518 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
521 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
522 addr = tcp->u_arg[1];
523 tcp->u_nargs = sysent[tcp->scno].nargs;
524 size = personality_wordsize[current_personality];
525 for (i = 0; i < tcp->u_nargs; ++i) {
526 if (size == sizeof(int)) {
528 if (umove(tcp, addr, &arg) < 0)
534 if (umove(tcp, addr, &arg) < 0)
543 #ifdef SYS_ipc_subcall
545 decode_ipc_subcall(struct tcb *tcp)
549 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
552 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
553 tcp->u_nargs = sysent[tcp->scno].nargs;
554 for (i = 0; i < tcp->u_nargs; i++)
555 tcp->u_arg[i] = tcp->u_arg[i + 1];
560 printargs(struct tcb *tcp)
565 for (i = 0; i < tcp->u_nargs; i++)
566 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
572 printargs_lu(struct tcb *tcp)
577 for (i = 0; i < tcp->u_nargs; i++)
578 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
584 printargs_ld(struct tcb *tcp)
589 for (i = 0; i < tcp->u_nargs; i++)
590 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
596 getrval2(struct tcb *tcp)
600 #if defined(SPARC) || defined(SPARC64)
602 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
604 val = regs.u_regs[U_REG_O1];
606 if (upeek(tcp, 4*(REG_REG0+1), &val) < 0)
609 if (upeek(tcp, PT_R9, &val) < 0)
617 is_restart_error(struct tcb *tcp)
619 switch (tcp->u_error) {
623 case ERESTART_RESTARTBLOCK:
632 struct pt_regs i386_regs;
633 #elif defined(X86_64)
635 * On 32 bits, pt_regs and user_regs_struct are the same,
636 * but on 64 bits, user_regs_struct has six more fields:
637 * fs_base, gs_base, ds, es, fs, gs.
638 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
640 static struct user_regs_struct x86_64_regs;
642 long r8, r10, psr; /* TODO: make static? */
643 long ia32 = 0; /* not static */
644 #elif defined(POWERPC)
651 static struct pt_regs regs;
656 static struct pt_regs regs;
657 #elif defined(SPARC) || defined(SPARC64)
658 static struct pt_regs regs;
659 static unsigned long trap;
660 #elif defined(LINUX_MIPSN32)
666 #elif defined(S390) || defined(S390X)
669 static long syscall_mode;
676 #elif defined(CRISV10) || defined(CRISV32)
678 #elif defined(MICROBLAZE)
683 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
684 * 1: ok, continue in trace_syscall().
685 * other: error, trace_syscall() should print error indicator
686 * ("????" etc) and bail out.
690 get_scno(struct tcb *tcp)
694 #if defined(S390) || defined(S390X)
695 if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
698 if (syscall_mode != -ENOSYS) {
700 * Since kernel version 2.5.44 the scno gets passed in gpr2.
705 * Old style of "passing" the scno via the SVC instruction.
707 long opcode, offset_reg, tmp;
709 static const int gpr_offset[16] = {
710 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
711 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
712 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
713 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
716 if (upeek(tcp, PT_PSWADDR, &pc) < 0)
719 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-sizeof(long)), 0);
721 perror("peektext(pc-oneword)");
726 * We have to check if the SVC got executed directly or via an
727 * EXECUTE instruction. In case of EXECUTE it is necessary to do
728 * instruction decoding to derive the system call number.
729 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
730 * so that this doesn't work if a SVC opcode is part of an EXECUTE
731 * opcode. Since there is no way to find out the opcode size this
732 * is the best we can do...
734 if ((opcode & 0xff00) == 0x0a00) {
736 scno = opcode & 0xff;
739 /* SVC got executed by EXECUTE instruction */
742 * Do instruction decoding of EXECUTE. If you really want to
743 * understand this, read the Principles of Operations.
745 svc_addr = (void *) (opcode & 0xfff);
748 offset_reg = (opcode & 0x000f0000) >> 16;
749 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
754 offset_reg = (opcode & 0x0000f000) >> 12;
755 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
759 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
768 offset_reg = (opcode & 0x00f00000) >> 20;
769 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
772 scno = (scno | tmp) & 0xff;
775 #elif defined(POWERPC)
776 if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
779 /* TODO: speed up strace by not doing this at every syscall.
780 * We only need to do it after execve.
785 /* Check for 64/32 bit mode. */
786 if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
788 /* SF is bit 0 of MSR */
793 update_personality(tcp, currpers);
796 /* Read complete register set in one go. */
797 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, ®s) < 0)
801 if (upeek(tcp, PT_ORIG_P0, &scno))
804 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &i386_regs) < 0)
806 scno = i386_regs.orig_eax;
807 #elif defined(X86_64)
809 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &x86_64_regs) < 0)
811 scno = x86_64_regs.orig_rax;
813 /* Check CS register value. On x86-64 linux it is:
814 * 0x33 for long mode (64 bit)
815 * 0x23 for compatibility mode (32 bit)
817 switch (x86_64_regs.cs) {
818 case 0x23: currpers = 1; break;
819 case 0x33: currpers = 0; break;
821 fprintf(stderr, "Unknown value CS=0x%08X while "
822 "detecting personality of process "
823 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
824 currpers = current_personality;
828 /* This version analyzes the opcode of a syscall instruction.
829 * (int 0x80 on i386 vs. syscall on x86-64)
830 * It works, but is too complicated.
832 unsigned long val, rip, i;
834 rip = x86_64_regs.rip;
836 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
840 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
842 fprintf(stderr, "ptrace_peektext failed: %s\n",
844 switch (call & 0xffff) {
845 /* x86-64: syscall = 0x0f 0x05 */
846 case 0x050f: currpers = 0; break;
847 /* i386: int 0x80 = 0xcd 0x80 */
848 case 0x80cd: currpers = 1; break;
850 currpers = current_personality;
852 "Unknown syscall opcode (0x%04X) while "
853 "detecting personality of process "
854 "PID=%d\n", (int)call, tcp->pid);
858 update_personality(tcp, currpers);
860 # define IA64_PSR_IS ((long)1 << 34)
861 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
862 ia32 = (psr & IA64_PSR_IS) != 0;
864 if (upeek(tcp, PT_R1, &scno) < 0)
867 if (upeek(tcp, PT_R15, &scno) < 0)
871 /* Read complete register set in one go. */
872 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (void *)®s) == -1)
876 * We only need to grab the syscall number on syscall entry.
878 if (regs.ARM_ip == 0) {
880 * Note: we only deal with only 32-bit CPUs here.
882 if (regs.ARM_cpsr & 0x20) {
884 * Get the Thumb-mode system call number
889 * Get the ARM-mode system call number
892 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(regs.ARM_pc - 4), NULL);
896 /* Handle the EABI syscall convention. We do not
897 bother converting structures between the two
898 ABIs, but basic functionality should work even
899 if strace and the traced program have different
901 if (scno == 0xef000000) {
904 if ((scno & 0x0ff00000) != 0x0f900000) {
905 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
911 * Fixup the syscall number
916 if (scno & 0x0f0000) {
918 * Handle ARM specific syscall
920 update_personality(tcp, 1);
923 update_personality(tcp, 0);
926 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
927 tcp->flags |= TCB_INSYSCALL;
930 if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
932 #elif defined(LINUX_MIPSN32)
933 unsigned long long regs[38];
935 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
941 if (!SCNO_IN_RANGE(scno)) {
942 if (a3 == 0 || a3 == -1) {
944 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
949 if (upeek(tcp, REG_A3, &a3) < 0)
951 if (upeek(tcp, REG_V0, &scno) < 0)
954 if (!SCNO_IN_RANGE(scno)) {
955 if (a3 == 0 || a3 == -1) {
957 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
962 if (upeek(tcp, REG_A3, &a3) < 0)
964 if (upeek(tcp, REG_R0, &scno) < 0)
968 * Do some sanity checks to figure out if it's
969 * really a syscall entry
971 if (!SCNO_IN_RANGE(scno)) {
972 if (a3 == 0 || a3 == -1) {
974 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
978 #elif defined(SPARC) || defined(SPARC64)
979 /* Everything we need is in the current register set. */
980 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
983 /* Disassemble the syscall trap. */
984 /* Retrieve the syscall trap instruction. */
986 # if defined(SPARC64)
987 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.tpc, 0);
990 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.pc, 0);
995 /* Disassemble the trap to see what personality to use. */
998 /* Linux/SPARC syscall trap. */
999 update_personality(tcp, 0);
1002 /* Linux/SPARC64 syscall trap. */
1003 update_personality(tcp, 2);
1006 /* SunOS syscall trap. (pers 1) */
1007 fprintf(stderr, "syscall: SunOS no support\n");
1010 /* Solaris 2.x syscall trap. (per 2) */
1011 update_personality(tcp, 1);
1014 /* NetBSD/FreeBSD syscall trap. */
1015 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1018 /* Solaris 2.x gettimeofday */
1019 update_personality(tcp, 1);
1022 # if defined(SPARC64)
1023 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, regs.tpc);
1025 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, regs.pc);
1030 /* Extract the system call number from the registers. */
1031 if (trap == 0x91d02027)
1034 scno = regs.u_regs[U_REG_G1];
1036 scno = regs.u_regs[U_REG_O0];
1037 memmove(®s.u_regs[U_REG_O0], ®s.u_regs[U_REG_O1], 7*sizeof(regs.u_regs[0]));
1040 if (upeek(tcp, PT_GR20, &scno) < 0)
1044 * In the new syscall ABI, the system call number is in R3.
1046 if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
1050 /* Odd as it may seem, a glibc bug has been known to cause
1051 glibc to issue bogus negative syscall numbers. So for
1052 our purposes, make strace print what it *should* have been */
1053 long correct_scno = (scno & 0xff);
1056 "Detected glibc bug: bogus system call"
1057 " number = %ld, correcting to %ld\n",
1060 scno = correct_scno;
1063 if (upeek(tcp, REG_SYSCALL, &scno) < 0)
1066 #elif defined(CRISV10) || defined(CRISV32)
1067 if (upeek(tcp, 4*PT_R9, &scno) < 0)
1070 if (upeek(tcp, PTREGS_OFFSET_REG(10), &scno) < 0)
1072 #elif defined(MICROBLAZE)
1073 if (upeek(tcp, 0, &scno) < 0)
1078 /* new syscall ABI returns result in R0 */
1079 if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0)
1082 /* ABI defines result returned in r9 */
1083 if (upeek(tcp, REG_GENERAL(9), (long *)&r9) < 0)
1091 /* Called at each syscall entry.
1093 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1094 * 1: ok, continue in trace_syscall().
1095 * other: error, trace_syscall() should print error indicator
1096 * ("????" etc) and bail out.
1099 syscall_fixup_on_sysenter(struct tcb *tcp)
1101 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
1103 if (i386_regs.eax != -ENOSYS) {
1105 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1108 #elif defined(X86_64)
1110 long rax = x86_64_regs.rax;
1111 if (current_personality == 1)
1112 rax = (int)rax; /* sign extend from 32 bits */
1113 if (rax != -ENOSYS) {
1115 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1119 #elif defined(S390) || defined(S390X)
1120 /* TODO: we already fetched PT_GPR2 in get_scno
1121 * and stored it in syscall_mode, reuse it here
1122 * instead of re-fetching?
1124 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1126 if (syscall_mode != -ENOSYS)
1127 syscall_mode = tcp->scno;
1128 if (gpr2 != syscall_mode) {
1130 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
1134 /* TODO? Eliminate upeek's in arches below like we did in x86 */
1135 if (upeek(tcp, 4*PT_D0, &d0) < 0)
1137 if (d0 != -ENOSYS) {
1139 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", d0);
1143 if (upeek(tcp, PT_R10, &r10) < 0)
1145 if (upeek(tcp, PT_R8, &r8) < 0)
1147 if (ia32 && r8 != -ENOSYS) {
1149 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", r8);
1152 #elif defined(CRISV10) || defined(CRISV32)
1153 if (upeek(tcp, 4*PT_R10, &r10) < 0)
1155 if (r10 != -ENOSYS) {
1157 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", r10);
1160 #elif defined(MICROBLAZE)
1161 if (upeek(tcp, 3 * 4, &r3) < 0)
1163 if (r3 != -ENOSYS) {
1165 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", r3);
1173 internal_fork(struct tcb *tcp)
1175 #if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1176 # define ARG_FLAGS 1
1178 # define ARG_FLAGS 0
1180 #ifndef CLONE_UNTRACED
1181 # define CLONE_UNTRACED 0x00800000
1183 if ((ptrace_setoptions
1184 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1185 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1191 if (entering(tcp)) {
1193 * We won't see the new child if clone is called with
1194 * CLONE_UNTRACED, so we keep the same logic with that option
1195 * and don't trace it.
1197 if ((sysent[tcp->scno].sys_func == sys_clone) &&
1198 (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED))
1202 if (tcp->flags & TCB_BPTSET)
1207 #if defined(TCB_WAITEXECVE)
1209 internal_exec(struct tcb *tcp)
1211 /* Maybe we have post-execve SIGTRAP suppressed? */
1212 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1213 return; /* yes, no need to do anything */
1215 if (exiting(tcp) && syserror(tcp))
1216 /* Error in execve, no post-execve SIGTRAP expected */
1217 tcp->flags &= ~TCB_WAITEXECVE;
1219 tcp->flags |= TCB_WAITEXECVE;
1224 internal_syscall(struct tcb *tcp)
1227 * We must always trace a few critical system calls in order to
1228 * correctly support following forks in the presence of tracing
1233 if (!SCNO_IN_RANGE(tcp->scno))
1236 func = sysent[tcp->scno].sys_func;
1238 if ( sys_fork == func
1239 || sys_vfork == func
1240 || sys_clone == func
1246 #if defined(TCB_WAITEXECVE)
1247 if ( sys_execve == func
1248 # if defined(SPARC) || defined(SPARC64)
1249 || sys_execv == func
1259 syscall_enter(struct tcb *tcp)
1263 if (SCNO_IN_RANGE(tcp->scno))
1264 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1266 nargs = tcp->u_nargs = MAX_ARGS;
1268 #if defined(S390) || defined(S390X)
1269 for (i = 0; i < nargs; ++i)
1270 if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1272 #elif defined(ALPHA)
1273 for (i = 0; i < nargs; ++i)
1274 if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
1278 unsigned long *out0, cfm, sof, sol;
1280 /* be backwards compatible with kernel < 2.4.4... */
1282 # define PT_RBS_END PT_AR_BSP
1285 if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
1287 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1290 sof = (cfm >> 0) & 0x7f;
1291 sol = (cfm >> 7) & 0x7f;
1292 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1294 for (i = 0; i < nargs; ++i) {
1295 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1296 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1300 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1301 PT_R9 /* ECX = out1 */,
1302 PT_R10 /* EDX = out2 */,
1303 PT_R14 /* ESI = out3 */,
1304 PT_R15 /* EDI = out4 */,
1305 PT_R13 /* EBP = out5 */};
1307 for (i = 0; i < nargs; ++i) {
1308 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1310 /* truncate away IVE sign-extension */
1311 tcp->u_arg[i] &= 0xffffffff;
1314 #elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
1315 /* N32 and N64 both use up to six registers. */
1316 unsigned long long regs[38];
1318 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1321 for (i = 0; i < nargs; ++i) {
1322 tcp->u_arg[i] = regs[REG_A0 + i];
1323 # if defined(LINUX_MIPSN32)
1324 tcp->ext_arg[i] = regs[REG_A0 + i];
1331 if (upeek(tcp, REG_SP, &sp) < 0)
1333 for (i = 0; i < 4; ++i)
1334 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1336 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
1337 (char *)(tcp->u_arg + 4));
1339 for (i = 0; i < nargs; ++i)
1340 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1343 #elif defined(POWERPC)
1345 # define PT_ORIG_R3 34
1347 for (i = 0; i < nargs; ++i) {
1348 if (upeek(tcp, (i==0) ?
1349 (sizeof(unsigned long) * PT_ORIG_R3) :
1350 ((i+PT_R3) * sizeof(unsigned long)),
1351 &tcp->u_arg[i]) < 0)
1354 #elif defined(SPARC) || defined(SPARC64)
1355 for (i = 0; i < nargs; ++i)
1356 tcp->u_arg[i] = regs.u_regs[U_REG_O0 + i];
1358 for (i = 0; i < nargs; ++i)
1359 if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1362 for (i = 0; i < nargs; ++i)
1363 tcp->u_arg[i] = regs.uregs[i];
1364 #elif defined(AVR32)
1367 tcp->u_arg[0] = regs.r12;
1368 tcp->u_arg[1] = regs.r11;
1369 tcp->u_arg[2] = regs.r10;
1370 tcp->u_arg[3] = regs.r9;
1371 tcp->u_arg[4] = regs.r5;
1372 tcp->u_arg[5] = regs.r3;
1374 static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
1376 for (i = 0; i < nargs; ++i)
1377 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1380 static const int syscall_regs[MAX_ARGS] = {
1381 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1382 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
1385 for (i = 0; i < nargs; ++i)
1386 if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
1390 /* Registers used by SH5 Linux system calls for parameters */
1391 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
1393 for (i = 0; i < nargs; ++i)
1394 if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1396 #elif defined(X86_64)
1399 if (current_personality == 0) { /* x86-64 ABI */
1400 tcp->u_arg[0] = x86_64_regs.rdi;
1401 tcp->u_arg[1] = x86_64_regs.rsi;
1402 tcp->u_arg[2] = x86_64_regs.rdx;
1403 tcp->u_arg[3] = x86_64_regs.r10;
1404 tcp->u_arg[4] = x86_64_regs.r8;
1405 tcp->u_arg[5] = x86_64_regs.r9;
1406 } else { /* i386 ABI */
1407 /* Sign-extend lower 32 bits */
1408 tcp->u_arg[0] = (long)(int)x86_64_regs.rbx;
1409 tcp->u_arg[1] = (long)(int)x86_64_regs.rcx;
1410 tcp->u_arg[2] = (long)(int)x86_64_regs.rdx;
1411 tcp->u_arg[3] = (long)(int)x86_64_regs.rsi;
1412 tcp->u_arg[4] = (long)(int)x86_64_regs.rdi;
1413 tcp->u_arg[5] = (long)(int)x86_64_regs.rbp;
1415 #elif defined(MICROBLAZE)
1416 for (i = 0; i < nargs; ++i)
1417 if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1419 #elif defined(CRISV10) || defined(CRISV32)
1420 static const int crisregs[MAX_ARGS] = {
1421 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
1422 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
1425 for (i = 0; i < nargs; ++i)
1426 if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0)
1429 for (i = 0; i < nargs; ++i)
1430 if (upeek(tcp, PTREGS_OFFSET_REG(i), &tcp->u_arg[i]) < 0)
1433 for (i = 0; i < nargs; ++i)
1434 if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1439 tcp->u_arg[0] = i386_regs.ebx;
1440 tcp->u_arg[1] = i386_regs.ecx;
1441 tcp->u_arg[2] = i386_regs.edx;
1442 tcp->u_arg[3] = i386_regs.esi;
1443 tcp->u_arg[4] = i386_regs.edi;
1444 tcp->u_arg[5] = i386_regs.ebp;
1445 #else /* Other architecture (32bits specific) */
1446 for (i = 0; i < nargs; ++i)
1447 if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
1454 trace_syscall_entering(struct tcb *tcp)
1458 #if defined TCB_WAITEXECVE
1459 if (tcp->flags & TCB_WAITEXECVE) {
1460 /* This is the post-execve SIGTRAP. */
1461 tcp->flags &= ~TCB_WAITEXECVE;
1466 scno_good = res = get_scno(tcp);
1470 res = syscall_fixup_on_sysenter(tcp);
1474 res = syscall_enter(tcp);
1480 tcp->flags &= ~TCB_REPRINT;
1482 tprintf("????" /* anti-trigraph gap */ "(");
1483 else if (!SCNO_IN_RANGE(tcp->scno))
1484 tprintf("syscall_%lu(", tcp->scno);
1486 tprintf("%s(", sysent[tcp->scno].sys_name);
1488 * " <unavailable>" will be added later by the code which
1489 * detects ptrace errors.
1494 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
1495 while (SCNO_IN_RANGE(tcp->scno)) {
1496 # ifdef SYS_socket_subcall
1497 if (sysent[tcp->scno].sys_func == sys_socketcall) {
1498 decode_socket_subcall(tcp);
1502 # ifdef SYS_ipc_subcall
1503 if (sysent[tcp->scno].sys_func == sys_ipc) {
1504 decode_ipc_subcall(tcp);
1510 #endif /* SYS_socket_subcall || SYS_ipc_subcall */
1512 internal_syscall(tcp);
1514 if ((SCNO_IN_RANGE(tcp->scno) &&
1515 !(qual_flags[tcp->scno] & QUAL_TRACE)) ||
1516 (tracing_paths && !pathtrace_match(tcp))) {
1517 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1521 tcp->flags &= ~TCB_FILTERED;
1523 if (cflag == CFLAG_ONLY_STATS) {
1529 tcp->flags &= ~TCB_REPRINT;
1530 if (!SCNO_IN_RANGE(tcp->scno))
1531 tprintf("syscall_%lu(", tcp->scno);
1533 tprintf("%s(", sysent[tcp->scno].sys_name);
1534 if (!SCNO_IN_RANGE(tcp->scno) ||
1535 ((qual_flags[tcp->scno] & QUAL_RAW) &&
1536 sysent[tcp->scno].sys_func != sys_exit))
1537 res = printargs(tcp);
1539 res = (*sysent[tcp->scno].sys_func)(tcp);
1541 if (fflush(tcp->outf) == EOF)
1544 tcp->flags |= TCB_INSYSCALL;
1545 /* Measure the entrance time as late as possible to avoid errors. */
1547 gettimeofday(&tcp->etime, NULL);
1552 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1553 * 1: ok, continue in trace_syscall().
1554 * other: error, trace_syscall() should print error indicator
1555 * ("????" etc) and bail out.
1558 get_syscall_result(struct tcb *tcp)
1560 #if defined(S390) || defined(S390X)
1561 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1563 #elif defined(POWERPC)
1564 # define SO_MASK 0x10000000
1567 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1569 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &result) < 0)
1571 if (flags & SO_MASK)
1574 #elif defined(AVR32)
1575 /* Read complete register set in one go. */
1576 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, ®s) < 0)
1579 if (upeek(tcp, PT_R0, &r0) < 0)
1582 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &i386_regs) < 0)
1584 #elif defined(X86_64)
1585 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &x86_64_regs) < 0)
1588 # define IA64_PSR_IS ((long)1 << 34)
1589 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
1590 ia32 = (psr & IA64_PSR_IS) != 0;
1591 if (upeek(tcp, PT_R8, &r8) < 0)
1593 if (upeek(tcp, PT_R10, &r10) < 0)
1596 /* Read complete register set in one go. */
1597 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (void *)®s) == -1)
1600 if (upeek(tcp, 4*PT_D0, &d0) < 0)
1602 #elif defined(LINUX_MIPSN32)
1603 unsigned long long regs[38];
1605 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1610 if (upeek(tcp, REG_A3, &a3) < 0)
1612 if (upeek(tcp, REG_V0, &r2) < 0)
1614 #elif defined(ALPHA)
1615 if (upeek(tcp, REG_A3, &a3) < 0)
1617 if (upeek(tcp, REG_R0, &r0) < 0)
1619 #elif defined(SPARC) || defined(SPARC64)
1620 /* Everything we need is in the current register set. */
1621 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1624 if (upeek(tcp, PT_GR28, &r28) < 0)
1628 #elif defined(CRISV10) || defined(CRISV32)
1629 if (upeek(tcp, 4*PT_R10, &r10) < 0)
1632 #elif defined(MICROBLAZE)
1633 if (upeek(tcp, 3 * 4, &r3) < 0)
1638 /* new syscall ABI returns result in R0 */
1639 if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0)
1642 /* ABI defines result returned in r9 */
1643 if (upeek(tcp, REG_GENERAL(9), (long *)&r9) < 0)
1650 /* Called at each syscall exit.
1652 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1653 * 1: ok, continue in trace_syscall().
1654 * other: error, trace_syscall() should print error indicator
1655 * ("????" etc) and bail out.
1658 syscall_fixup_on_sysexit(struct tcb *tcp)
1660 #if defined(S390) || defined(S390X)
1661 if (syscall_mode != -ENOSYS)
1662 syscall_mode = tcp->scno;
1663 if ((tcp->flags & TCB_WAITEXECVE)
1664 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1666 * Return from execve.
1667 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1668 * flag set for the post-execve SIGTRAP to see and reset.
1677 * Check the syscall return value register value for whether it is
1678 * a negated errno code indicating an error, or a success return value.
1681 is_negated_errno(unsigned long int val)
1683 unsigned long int max = -(long int) nerrnos;
1684 #if SUPPORTED_PERSONALITIES > 1
1685 if (personality_wordsize[current_personality] < sizeof(val)) {
1686 val = (unsigned int) val;
1687 max = (unsigned int) max;
1694 get_error(struct tcb *tcp)
1697 int check_errno = 1;
1698 if (SCNO_IN_RANGE(tcp->scno) &&
1699 sysent[tcp->scno].sys_flags & SYSCALL_NEVER_FAILS) {
1702 #if defined(S390) || defined(S390X)
1703 if (check_errno && is_negated_errno(gpr2)) {
1711 if (check_errno && is_negated_errno(i386_regs.eax)) {
1713 u_error = -i386_regs.eax;
1716 tcp->u_rval = i386_regs.eax;
1718 #elif defined(X86_64)
1719 if (check_errno && is_negated_errno(x86_64_regs.rax)) {
1721 u_error = -x86_64_regs.rax;
1724 tcp->u_rval = x86_64_regs.rax;
1731 if (check_errno && is_negated_errno(err)) {
1739 if (check_errno && r10) {
1747 if (check_errno && a3) {
1753 #elif defined(POWERPC)
1754 if (check_errno && is_negated_errno(result)) {
1759 tcp->u_rval = result;
1762 if (check_errno && is_negated_errno(d0)) {
1770 if (check_errno && is_negated_errno(regs.ARM_r0)) {
1772 u_error = -regs.ARM_r0;
1775 tcp->u_rval = regs.ARM_r0;
1777 #elif defined(AVR32)
1778 if (check_errno && regs.r12 && (unsigned) -regs.r12 < nerrnos) {
1780 u_error = -regs.r12;
1783 tcp->u_rval = regs.r12;
1786 if (check_errno && is_negated_errno(r0)) {
1792 #elif defined(ALPHA)
1793 if (check_errno && a3) {
1800 #elif defined(SPARC)
1801 if (check_errno && regs.psr & PSR_C) {
1803 u_error = regs.u_regs[U_REG_O0];
1806 tcp->u_rval = regs.u_regs[U_REG_O0];
1808 #elif defined(SPARC64)
1809 if (check_errno && regs.tstate & 0x1100000000UL) {
1811 u_error = regs.u_regs[U_REG_O0];
1814 tcp->u_rval = regs.u_regs[U_REG_O0];
1817 if (check_errno && is_negated_errno(r28)) {
1825 if (check_errno && is_negated_errno(r0)) {
1833 if (check_errno && is_negated_errno(r9)) {
1840 #elif defined(CRISV10) || defined(CRISV32)
1841 if (check_errno && r10 && (unsigned) -r10 < nerrnos) {
1850 if (upeek(tcp, PTREGS_OFFSET_REG(0), &rval) < 0)
1852 if (check_errno && rval < 0 && rval > -nerrnos) {
1859 #elif defined(MICROBLAZE)
1860 if (check_errno && is_negated_errno(r3)) {
1868 tcp->u_error = u_error;
1873 dumpio(struct tcb *tcp)
1877 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
1879 if (!SCNO_IN_RANGE(tcp->scno))
1881 if (sysent[tcp->scno].sys_func == printargs)
1883 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
1884 if (sysent[tcp->scno].sys_func == sys_read ||
1885 sysent[tcp->scno].sys_func == sys_pread ||
1886 sysent[tcp->scno].sys_func == sys_recv ||
1887 sysent[tcp->scno].sys_func == sys_recvfrom)
1888 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
1889 else if (sysent[tcp->scno].sys_func == sys_readv)
1890 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
1893 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
1894 if (sysent[tcp->scno].sys_func == sys_write ||
1895 sysent[tcp->scno].sys_func == sys_pwrite ||
1896 sysent[tcp->scno].sys_func == sys_send ||
1897 sysent[tcp->scno].sys_func == sys_sendto)
1898 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
1899 else if (sysent[tcp->scno].sys_func == sys_writev)
1900 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
1906 trace_syscall_exiting(struct tcb *tcp)
1913 /* Measure the exit time as early as possible to avoid errors. */
1915 gettimeofday(&tv, NULL);
1917 #if SUPPORTED_PERSONALITIES > 1
1918 update_personality(tcp, tcp->currpers);
1920 res = get_syscall_result(tcp);
1924 res = syscall_fixup_on_sysexit(tcp);
1928 res = get_error(tcp);
1932 internal_syscall(tcp);
1934 if (res == 1 && filtered(tcp)) {
1938 /* TODO: TCB_REPRINT is probably not necessary:
1939 * we can determine whether reprinting is needed
1940 * by examining printing_tcp. Something like:
1941 * if not in -ff mode, and printing_tcp != tcp,
1942 * then the log is not currenlty ends with *our*
1943 * syscall entry output, but with something else,
1944 * and we need to reprint.
1945 * If we'd implement this, printing_tcp = tcp
1946 * assignments in code below can be made more logical.
1949 if (tcp->flags & TCB_REPRINT) {
1951 if (!SCNO_IN_RANGE(tcp->scno))
1952 tprintf("<... syscall_%lu resumed> ", tcp->scno);
1954 tprintf("<... %s resumed> ", sysent[tcp->scno].sys_name);
1958 struct timeval t = tv;
1959 count_syscall(tcp, &t);
1960 if (cflag == CFLAG_ONLY_STATS) {
1969 tprints("= ? <unavailable>\n");
1971 tcp->flags &= ~TCB_INSYSCALL;
1975 if (!SCNO_IN_RANGE(tcp->scno)
1976 || (qual_flags[tcp->scno] & QUAL_RAW)) {
1978 sys_res = printargs(tcp);
1980 /* FIXME: not_failing_only (IOW, option -z) is broken:
1981 * failure of syscall is known only after syscall return.
1982 * Thus we end up with something like this on, say, ENOENT:
1983 * open("doesnt_exist", O_RDONLY <unfinished ...>
1984 * {next syscall decode}
1985 * whereas the intended result is that open(...) line
1986 * is not shown at all.
1988 if (not_failing_only && tcp->u_error)
1989 goto ret; /* ignore failed syscalls */
1991 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1996 u_error = tcp->u_error;
1997 if (!SCNO_IN_RANGE(tcp->scno) ||
1998 qual_flags[tcp->scno] & QUAL_RAW) {
2000 tprintf("= -1 (errno %ld)", u_error);
2002 tprintf("= %#lx", tcp->u_rval);
2004 else if (!(sys_res & RVAL_NONE) && u_error) {
2006 /* Blocked signals do not interrupt any syscalls.
2007 * In this case syscalls don't return ERESTARTfoo codes.
2009 * Deadly signals set to SIG_DFL interrupt syscalls
2010 * and kill the process regardless of which of the codes below
2011 * is returned by the interrupted syscall.
2012 * In some cases, kernel forces a kernel-generated deadly
2013 * signal to be unblocked and set to SIG_DFL (and thus cause
2014 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2015 * or SIGILL. (The alternative is to leave process spinning
2016 * forever on the faulty instruction - not useful).
2018 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2019 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2020 * but kernel will always restart them.
2023 /* Most common type of signal-interrupted syscall exit code.
2024 * The system call will be restarted with the same arguments
2025 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2027 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
2029 case ERESTARTNOINTR:
2030 /* Rare. For example, fork() returns this if interrupted.
2031 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2033 tprints("= ? ERESTARTNOINTR (To be restarted)");
2035 case ERESTARTNOHAND:
2036 /* pause(), rt_sigsuspend() etc use this code.
2037 * SA_RESTART is ignored (assumed not set):
2038 * syscall won't restart (will return EINTR instead)
2039 * even after signal with SA_RESTART set.
2040 * However, after SIG_IGN or SIG_DFL signal it will.
2042 tprints("= ? ERESTARTNOHAND (Interrupted by signal)");
2044 case ERESTART_RESTARTBLOCK:
2045 /* Syscalls like nanosleep(), poll() which can't be
2046 * restarted with their original arguments use this
2047 * code. Kernel will execute restart_syscall() instead,
2048 * which changes arguments before restarting syscall.
2049 * SA_RESTART is ignored (assumed not set) similarly
2050 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2051 * since restart data is saved in "restart block"
2052 * in task struct, and if signal handler uses a syscall
2053 * which in turn saves another such restart block,
2054 * old data is lost and restart becomes impossible)
2056 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
2060 tprintf("= -1 E??? (errno %ld)", u_error);
2061 else if (u_error < nerrnos)
2062 tprintf("= -1 %s (%s)", errnoent[u_error],
2065 tprintf("= -1 ERRNO_%ld (%s)", u_error,
2069 if ((sys_res & RVAL_STR) && tcp->auxstr)
2070 tprintf(" (%s)", tcp->auxstr);
2073 if (sys_res & RVAL_NONE)
2076 switch (sys_res & RVAL_MASK) {
2078 tprintf("= %#lx", tcp->u_rval);
2081 tprintf("= %#lo", tcp->u_rval);
2084 tprintf("= %lu", tcp->u_rval);
2087 tprintf("= %ld", tcp->u_rval);
2091 "invalid rval format\n");
2095 if ((sys_res & RVAL_STR) && tcp->auxstr)
2096 tprintf(" (%s)", tcp->auxstr);
2099 tv_sub(&tv, &tv, &tcp->etime);
2100 tprintf(" <%ld.%06ld>",
2101 (long) tv.tv_sec, (long) tv.tv_usec);
2108 tcp->flags &= ~TCB_INSYSCALL;
2113 trace_syscall(struct tcb *tcp)
2115 return exiting(tcp) ?
2116 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);