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 = sysent0;
187 const char *const *errnoent = errnoent0;
188 const char *const *signalent = signalent0;
189 const struct ioctlent *ioctlent = ioctlent0;
190 unsigned nsyscalls = nsyscalls0;
191 unsigned nerrnos = nerrnos0;
192 unsigned nsignals = nsignals0;
193 unsigned nioctlents = nioctlents0;
194 int *qual_flags = qual_flags0;
196 #if SUPPORTED_PERSONALITIES > 1
197 int current_personality;
199 const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
200 PERSONALITY0_WORDSIZE,
201 PERSONALITY1_WORDSIZE,
202 # if SUPPORTED_PERSONALITIES > 2
203 PERSONALITY2_WORDSIZE,
208 set_personality(int personality)
210 switch (personality) {
212 errnoent = errnoent0;
215 nsyscalls = nsyscalls0;
216 ioctlent = ioctlent0;
217 nioctlents = nioctlents0;
218 signalent = signalent0;
219 nsignals = nsignals0;
220 qual_flags = qual_flags0;
224 errnoent = errnoent1;
227 nsyscalls = nsyscalls1;
228 ioctlent = ioctlent1;
229 nioctlents = nioctlents1;
230 signalent = signalent1;
231 nsignals = nsignals1;
232 qual_flags = qual_flags1;
235 # if SUPPORTED_PERSONALITIES >= 3
237 errnoent = errnoent2;
240 nsyscalls = nsyscalls2;
241 ioctlent = ioctlent2;
242 nioctlents = nioctlents2;
243 signalent = signalent2;
244 nsignals = nsignals2;
245 qual_flags = qual_flags2;
250 current_personality = personality;
254 update_personality(struct tcb *tcp, int personality)
256 if (personality == current_personality)
258 set_personality(personality);
260 if (personality == tcp->currpers)
262 tcp->currpers = personality;
264 # if defined(POWERPC64) || defined(X86_64)
266 static const char *const names[] = {"64 bit", "32 bit"};
267 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
268 tcp->pid, names[personality]);
274 static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
276 static const struct qual_options {
278 const char *option_name;
279 int (*qualify)(const char *, int, int);
280 const char *argument_name;
282 { QUAL_TRACE, "trace", qual_syscall, "system call" },
283 { QUAL_TRACE, "t", qual_syscall, "system call" },
284 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
285 { QUAL_ABBREV, "a", qual_syscall, "system call" },
286 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
287 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
288 { QUAL_RAW, "raw", qual_syscall, "system call" },
289 { QUAL_RAW, "x", qual_syscall, "system call" },
290 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
291 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
292 { QUAL_SIGNAL, "s", qual_signal, "signal" },
293 { QUAL_FAULT, "fault", qual_fault, "fault" },
294 { QUAL_FAULT, "faults", qual_fault, "fault" },
295 { QUAL_FAULT, "m", qual_fault, "fault" },
296 { QUAL_READ, "read", qual_desc, "descriptor" },
297 { QUAL_READ, "reads", qual_desc, "descriptor" },
298 { QUAL_READ, "r", qual_desc, "descriptor" },
299 { QUAL_WRITE, "write", qual_desc, "descriptor" },
300 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
301 { QUAL_WRITE, "w", qual_desc, "descriptor" },
302 { 0, NULL, NULL, NULL },
306 qualify_one(int n, int bitflag, int not, int pers)
308 if (pers == 0 || pers < 0) {
310 qual_flags0[n] &= ~bitflag;
312 qual_flags0[n] |= bitflag;
315 #if SUPPORTED_PERSONALITIES >= 2
316 if (pers == 1 || pers < 0) {
318 qual_flags1[n] &= ~bitflag;
320 qual_flags1[n] |= bitflag;
324 #if SUPPORTED_PERSONALITIES >= 3
325 if (pers == 2 || pers < 0) {
327 qual_flags2[n] &= ~bitflag;
329 qual_flags2[n] |= bitflag;
335 qual_syscall(const char *s, int bitflag, int not)
340 if (isdigit((unsigned char)*s)) {
342 if (i < 0 || i >= MAX_QUALS)
344 qualify_one(i, bitflag, not, -1);
347 for (i = 0; i < nsyscalls0; i++)
348 if (strcmp(s, sysent0[i].sys_name) == 0) {
349 qualify_one(i, bitflag, not, 0);
353 #if SUPPORTED_PERSONALITIES >= 2
354 for (i = 0; i < nsyscalls1; i++)
355 if (strcmp(s, sysent1[i].sys_name) == 0) {
356 qualify_one(i, bitflag, not, 1);
361 #if SUPPORTED_PERSONALITIES >= 3
362 for (i = 0; i < nsyscalls2; i++)
363 if (strcmp(s, sysent2[i].sys_name) == 0) {
364 qualify_one(i, bitflag, not, 2);
373 qual_signal(const char *s, int bitflag, int not)
378 if (isdigit((unsigned char)*s)) {
380 if (signo < 0 || signo >= MAX_QUALS)
382 qualify_one(signo, bitflag, not, -1);
385 if (strlen(s) >= sizeof buf)
389 if (strncasecmp(s, "SIG", 3) == 0)
391 for (i = 0; i <= NSIG; i++)
392 if (strcasecmp(s, signame(i) + 3) == 0) {
393 qualify_one(i, bitflag, not, -1);
400 qual_fault(const char *s, int bitflag, int not)
406 qual_desc(const char *s, int bitflag, int not)
408 if (isdigit((unsigned char)*s)) {
410 if (desc < 0 || desc >= MAX_QUALS)
412 qualify_one(desc, bitflag, not, -1);
419 lookup_class(const char *s)
421 if (strcmp(s, "file") == 0)
423 if (strcmp(s, "ipc") == 0)
425 if (strcmp(s, "network") == 0)
426 return TRACE_NETWORK;
427 if (strcmp(s, "process") == 0)
428 return TRACE_PROCESS;
429 if (strcmp(s, "signal") == 0)
431 if (strcmp(s, "desc") == 0)
437 qualify(const char *s)
439 const struct qual_options *opt;
445 opt = &qual_options[0];
446 for (i = 0; (p = qual_options[i].option_name); i++) {
448 if (strncmp(s, p, n) == 0 && s[n] == '=') {
449 opt = &qual_options[i];
459 if (strcmp(s, "none") == 0) {
463 if (strcmp(s, "all") == 0) {
464 for (i = 0; i < MAX_QUALS; i++) {
465 qualify_one(i, opt->bitflag, not, -1);
469 for (i = 0; i < MAX_QUALS; i++) {
470 qualify_one(i, opt->bitflag, !not, -1);
475 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
476 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
477 for (i = 0; i < nsyscalls0; i++)
478 if (sysent0[i].sys_flags & n)
479 qualify_one(i, opt->bitflag, not, 0);
481 #if SUPPORTED_PERSONALITIES >= 2
482 for (i = 0; i < nsyscalls1; i++)
483 if (sysent1[i].sys_flags & n)
484 qualify_one(i, opt->bitflag, not, 1);
487 #if SUPPORTED_PERSONALITIES >= 3
488 for (i = 0; i < nsyscalls2; i++)
489 if (sysent2[i].sys_flags & n)
490 qualify_one(i, opt->bitflag, not, 2);
495 if (opt->qualify(p, opt->bitflag, not)) {
496 error_msg_and_die("invalid %s '%s'",
497 opt->argument_name, p);
504 #ifdef SYS_socket_subcall
506 decode_socket_subcall(struct tcb *tcp)
509 unsigned int i, size;
511 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
514 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
515 addr = tcp->u_arg[1];
516 tcp->u_nargs = sysent[tcp->scno].nargs;
517 size = current_wordsize;
518 for (i = 0; i < tcp->u_nargs; ++i) {
519 if (size == sizeof(int)) {
521 if (umove(tcp, addr, &arg) < 0)
527 if (umove(tcp, addr, &arg) < 0)
536 #ifdef SYS_ipc_subcall
538 decode_ipc_subcall(struct tcb *tcp)
542 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
545 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
546 tcp->u_nargs = sysent[tcp->scno].nargs;
547 for (i = 0; i < tcp->u_nargs; i++)
548 tcp->u_arg[i] = tcp->u_arg[i + 1];
553 printargs(struct tcb *tcp)
558 for (i = 0; i < tcp->u_nargs; i++)
559 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
565 printargs_lu(struct tcb *tcp)
570 for (i = 0; i < tcp->u_nargs; i++)
571 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
577 printargs_ld(struct tcb *tcp)
582 for (i = 0; i < tcp->u_nargs; i++)
583 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
589 getrval2(struct tcb *tcp)
593 #if defined(SPARC) || defined(SPARC64)
595 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
597 val = regs.u_regs[U_REG_O1];
599 if (upeek(tcp, 4*(REG_REG0+1), &val) < 0)
602 if (upeek(tcp, PT_R9, &val) < 0)
610 is_restart_error(struct tcb *tcp)
612 switch (tcp->u_error) {
616 case ERESTART_RESTARTBLOCK:
625 struct pt_regs i386_regs;
626 #elif defined(X86_64)
628 * On 32 bits, pt_regs and user_regs_struct are the same,
629 * but on 64 bits, user_regs_struct has six more fields:
630 * fs_base, gs_base, ds, es, fs, gs.
631 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
633 static struct user_regs_struct x86_64_regs;
635 long r8, r10, psr; /* TODO: make static? */
636 long ia32 = 0; /* not static */
637 #elif defined(POWERPC)
644 static struct pt_regs regs;
649 static struct pt_regs regs;
650 #elif defined(SPARC) || defined(SPARC64)
651 static struct pt_regs regs;
652 static unsigned long trap;
653 #elif defined(LINUX_MIPSN32)
659 #elif defined(S390) || defined(S390X)
662 static long syscall_mode;
669 #elif defined(CRISV10) || defined(CRISV32)
671 #elif defined(MICROBLAZE)
676 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
677 * 1: ok, continue in trace_syscall().
678 * other: error, trace_syscall() should print error indicator
679 * ("????" etc) and bail out.
682 get_scno(struct tcb *tcp)
686 #if defined(S390) || defined(S390X)
687 if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
690 if (syscall_mode != -ENOSYS) {
692 * Since kernel version 2.5.44 the scno gets passed in gpr2.
697 * Old style of "passing" the scno via the SVC instruction.
699 long opcode, offset_reg, tmp;
701 static const int gpr_offset[16] = {
702 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
703 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
704 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
705 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
708 if (upeek(tcp, PT_PSWADDR, &pc) < 0)
711 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-sizeof(long)), 0);
713 perror("peektext(pc-oneword)");
718 * We have to check if the SVC got executed directly or via an
719 * EXECUTE instruction. In case of EXECUTE it is necessary to do
720 * instruction decoding to derive the system call number.
721 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
722 * so that this doesn't work if a SVC opcode is part of an EXECUTE
723 * opcode. Since there is no way to find out the opcode size this
724 * is the best we can do...
726 if ((opcode & 0xff00) == 0x0a00) {
728 scno = opcode & 0xff;
731 /* SVC got executed by EXECUTE instruction */
734 * Do instruction decoding of EXECUTE. If you really want to
735 * understand this, read the Principles of Operations.
737 svc_addr = (void *) (opcode & 0xfff);
740 offset_reg = (opcode & 0x000f0000) >> 16;
741 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
746 offset_reg = (opcode & 0x0000f000) >> 12;
747 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
751 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
760 offset_reg = (opcode & 0x00f00000) >> 20;
761 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
764 scno = (scno | tmp) & 0xff;
767 #elif defined(POWERPC)
768 if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
771 /* TODO: speed up strace by not doing this at every syscall.
772 * We only need to do it after execve.
777 /* Check for 64/32 bit mode. */
778 if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
780 /* SF is bit 0 of MSR */
785 update_personality(tcp, currpers);
788 /* Read complete register set in one go. */
789 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, ®s) < 0)
793 if (upeek(tcp, PT_ORIG_P0, &scno))
796 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &i386_regs) < 0)
798 scno = i386_regs.orig_eax;
799 #elif defined(X86_64)
801 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &x86_64_regs) < 0)
803 scno = x86_64_regs.orig_rax;
805 /* Check CS register value. On x86-64 linux it is:
806 * 0x33 for long mode (64 bit)
807 * 0x23 for compatibility mode (32 bit)
809 switch (x86_64_regs.cs) {
810 case 0x23: currpers = 1; break;
811 case 0x33: currpers = 0; break;
813 fprintf(stderr, "Unknown value CS=0x%08X while "
814 "detecting personality of process "
815 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
816 currpers = current_personality;
820 /* This version analyzes the opcode of a syscall instruction.
821 * (int 0x80 on i386 vs. syscall on x86-64)
822 * It works, but is too complicated.
824 unsigned long val, rip, i;
826 rip = x86_64_regs.rip;
828 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
832 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
834 fprintf(stderr, "ptrace_peektext failed: %s\n",
836 switch (call & 0xffff) {
837 /* x86-64: syscall = 0x0f 0x05 */
838 case 0x050f: currpers = 0; break;
839 /* i386: int 0x80 = 0xcd 0x80 */
840 case 0x80cd: currpers = 1; break;
842 currpers = current_personality;
844 "Unknown syscall opcode (0x%04X) while "
845 "detecting personality of process "
846 "PID=%d\n", (int)call, tcp->pid);
850 update_personality(tcp, currpers);
852 # define IA64_PSR_IS ((long)1 << 34)
853 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
854 ia32 = (psr & IA64_PSR_IS) != 0;
856 if (upeek(tcp, PT_R1, &scno) < 0)
859 if (upeek(tcp, PT_R15, &scno) < 0)
863 /* Read complete register set in one go. */
864 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (void *)®s) == -1)
868 * We only need to grab the syscall number on syscall entry.
870 if (regs.ARM_ip == 0) {
872 * Note: we only deal with only 32-bit CPUs here.
874 if (regs.ARM_cpsr & 0x20) {
876 * Get the Thumb-mode system call number
881 * Get the ARM-mode system call number
884 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(regs.ARM_pc - 4), NULL);
888 /* Handle the EABI syscall convention. We do not
889 bother converting structures between the two
890 ABIs, but basic functionality should work even
891 if strace and the traced program have different
893 if (scno == 0xef000000) {
896 if ((scno & 0x0ff00000) != 0x0f900000) {
897 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
903 * Fixup the syscall number
908 if (scno & 0x0f0000) {
910 * Handle ARM specific syscall
912 update_personality(tcp, 1);
915 update_personality(tcp, 0);
918 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
919 tcp->flags |= TCB_INSYSCALL;
922 if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
924 #elif defined(LINUX_MIPSN32)
925 unsigned long long regs[38];
927 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
933 if (!SCNO_IN_RANGE(scno)) {
934 if (a3 == 0 || a3 == -1) {
936 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
941 if (upeek(tcp, REG_A3, &a3) < 0)
943 if (upeek(tcp, REG_V0, &scno) < 0)
946 if (!SCNO_IN_RANGE(scno)) {
947 if (a3 == 0 || a3 == -1) {
949 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
954 if (upeek(tcp, REG_A3, &a3) < 0)
956 if (upeek(tcp, REG_R0, &scno) < 0)
960 * Do some sanity checks to figure out if it's
961 * really a syscall entry
963 if (!SCNO_IN_RANGE(scno)) {
964 if (a3 == 0 || a3 == -1) {
966 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
970 #elif defined(SPARC) || defined(SPARC64)
971 /* Everything we need is in the current register set. */
972 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
975 /* Disassemble the syscall trap. */
976 /* Retrieve the syscall trap instruction. */
978 # if defined(SPARC64)
979 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.tpc, 0);
982 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.pc, 0);
987 /* Disassemble the trap to see what personality to use. */
990 /* Linux/SPARC syscall trap. */
991 update_personality(tcp, 0);
994 /* Linux/SPARC64 syscall trap. */
995 update_personality(tcp, 2);
998 /* SunOS syscall trap. (pers 1) */
999 fprintf(stderr, "syscall: SunOS no support\n");
1002 /* Solaris 2.x syscall trap. (per 2) */
1003 update_personality(tcp, 1);
1006 /* NetBSD/FreeBSD syscall trap. */
1007 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1010 /* Solaris 2.x gettimeofday */
1011 update_personality(tcp, 1);
1014 # if defined(SPARC64)
1015 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, regs.tpc);
1017 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, regs.pc);
1022 /* Extract the system call number from the registers. */
1023 if (trap == 0x91d02027)
1026 scno = regs.u_regs[U_REG_G1];
1028 scno = regs.u_regs[U_REG_O0];
1029 memmove(®s.u_regs[U_REG_O0], ®s.u_regs[U_REG_O1], 7*sizeof(regs.u_regs[0]));
1032 if (upeek(tcp, PT_GR20, &scno) < 0)
1036 * In the new syscall ABI, the system call number is in R3.
1038 if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
1042 /* Odd as it may seem, a glibc bug has been known to cause
1043 glibc to issue bogus negative syscall numbers. So for
1044 our purposes, make strace print what it *should* have been */
1045 long correct_scno = (scno & 0xff);
1048 "Detected glibc bug: bogus system call"
1049 " number = %ld, correcting to %ld\n",
1052 scno = correct_scno;
1055 if (upeek(tcp, REG_SYSCALL, &scno) < 0)
1058 #elif defined(CRISV10) || defined(CRISV32)
1059 if (upeek(tcp, 4*PT_R9, &scno) < 0)
1062 if (upeek(tcp, PTREGS_OFFSET_REG(10), &scno) < 0)
1064 #elif defined(MICROBLAZE)
1065 if (upeek(tcp, 0, &scno) < 0)
1070 /* new syscall ABI returns result in R0 */
1071 if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0)
1074 /* ABI defines result returned in r9 */
1075 if (upeek(tcp, REG_GENERAL(9), (long *)&r9) < 0)
1083 /* Called at each syscall entry.
1085 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1086 * 1: ok, continue in trace_syscall().
1087 * other: error, trace_syscall() should print error indicator
1088 * ("????" etc) and bail out.
1091 syscall_fixup_on_sysenter(struct tcb *tcp)
1093 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
1095 if (i386_regs.eax != -ENOSYS) {
1097 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1100 #elif defined(X86_64)
1102 long rax = x86_64_regs.rax;
1103 if (current_personality == 1)
1104 rax = (int)rax; /* sign extend from 32 bits */
1105 if (rax != -ENOSYS) {
1107 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1111 #elif defined(S390) || defined(S390X)
1112 /* TODO: we already fetched PT_GPR2 in get_scno
1113 * and stored it in syscall_mode, reuse it here
1114 * instead of re-fetching?
1116 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1118 if (syscall_mode != -ENOSYS)
1119 syscall_mode = tcp->scno;
1120 if (gpr2 != syscall_mode) {
1122 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
1126 /* TODO? Eliminate upeek's in arches below like we did in x86 */
1127 if (upeek(tcp, 4*PT_D0, &d0) < 0)
1129 if (d0 != -ENOSYS) {
1131 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", d0);
1135 if (upeek(tcp, PT_R10, &r10) < 0)
1137 if (upeek(tcp, PT_R8, &r8) < 0)
1139 if (ia32 && r8 != -ENOSYS) {
1141 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", r8);
1144 #elif defined(CRISV10) || defined(CRISV32)
1145 if (upeek(tcp, 4*PT_R10, &r10) < 0)
1147 if (r10 != -ENOSYS) {
1149 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", r10);
1152 #elif defined(MICROBLAZE)
1153 if (upeek(tcp, 3 * 4, &r3) < 0)
1155 if (r3 != -ENOSYS) {
1157 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", r3);
1165 internal_fork(struct tcb *tcp)
1167 #if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1168 # define ARG_FLAGS 1
1170 # define ARG_FLAGS 0
1172 #ifndef CLONE_UNTRACED
1173 # define CLONE_UNTRACED 0x00800000
1175 if ((ptrace_setoptions
1176 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1177 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1183 if (entering(tcp)) {
1185 * We won't see the new child if clone is called with
1186 * CLONE_UNTRACED, so we keep the same logic with that option
1187 * and don't trace it.
1189 if ((sysent[tcp->scno].sys_func == sys_clone) &&
1190 (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED))
1194 if (tcp->flags & TCB_BPTSET)
1199 #if defined(TCB_WAITEXECVE)
1201 internal_exec(struct tcb *tcp)
1203 /* Maybe we have post-execve SIGTRAP suppressed? */
1204 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1205 return; /* yes, no need to do anything */
1207 if (exiting(tcp) && syserror(tcp))
1208 /* Error in execve, no post-execve SIGTRAP expected */
1209 tcp->flags &= ~TCB_WAITEXECVE;
1211 tcp->flags |= TCB_WAITEXECVE;
1216 internal_syscall(struct tcb *tcp)
1219 * We must always trace a few critical system calls in order to
1220 * correctly support following forks in the presence of tracing
1225 if (!SCNO_IN_RANGE(tcp->scno))
1228 func = sysent[tcp->scno].sys_func;
1230 if ( sys_fork == func
1231 || sys_vfork == func
1232 || sys_clone == func
1238 #if defined(TCB_WAITEXECVE)
1239 if ( sys_execve == func
1240 # if defined(SPARC) || defined(SPARC64)
1241 || sys_execv == func
1250 /* Return -1 on error or 1 on success (never 0!) */
1252 get_syscall_args(struct tcb *tcp)
1256 if (SCNO_IN_RANGE(tcp->scno))
1257 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1259 nargs = tcp->u_nargs = MAX_ARGS;
1261 #if defined(S390) || defined(S390X)
1262 for (i = 0; i < nargs; ++i)
1263 if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1265 #elif defined(ALPHA)
1266 for (i = 0; i < nargs; ++i)
1267 if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
1271 unsigned long *out0, cfm, sof, sol;
1273 /* be backwards compatible with kernel < 2.4.4... */
1275 # define PT_RBS_END PT_AR_BSP
1278 if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
1280 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1283 sof = (cfm >> 0) & 0x7f;
1284 sol = (cfm >> 7) & 0x7f;
1285 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1287 for (i = 0; i < nargs; ++i) {
1288 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1289 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1293 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1294 PT_R9 /* ECX = out1 */,
1295 PT_R10 /* EDX = out2 */,
1296 PT_R14 /* ESI = out3 */,
1297 PT_R15 /* EDI = out4 */,
1298 PT_R13 /* EBP = out5 */};
1300 for (i = 0; i < nargs; ++i) {
1301 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1303 /* truncate away IVE sign-extension */
1304 tcp->u_arg[i] &= 0xffffffff;
1307 #elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
1308 /* N32 and N64 both use up to six registers. */
1309 unsigned long long regs[38];
1311 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1314 for (i = 0; i < nargs; ++i) {
1315 tcp->u_arg[i] = regs[REG_A0 + i];
1316 # if defined(LINUX_MIPSN32)
1317 tcp->ext_arg[i] = regs[REG_A0 + i];
1324 if (upeek(tcp, REG_SP, &sp) < 0)
1326 for (i = 0; i < 4; ++i)
1327 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1329 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
1330 (char *)(tcp->u_arg + 4));
1332 for (i = 0; i < nargs; ++i)
1333 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1336 #elif defined(POWERPC)
1338 # define PT_ORIG_R3 34
1340 for (i = 0; i < nargs; ++i) {
1341 if (upeek(tcp, (i==0) ?
1342 (sizeof(unsigned long) * PT_ORIG_R3) :
1343 ((i+PT_R3) * sizeof(unsigned long)),
1344 &tcp->u_arg[i]) < 0)
1347 #elif defined(SPARC) || defined(SPARC64)
1348 for (i = 0; i < nargs; ++i)
1349 tcp->u_arg[i] = regs.u_regs[U_REG_O0 + i];
1351 for (i = 0; i < nargs; ++i)
1352 if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1355 for (i = 0; i < nargs; ++i)
1356 tcp->u_arg[i] = regs.uregs[i];
1357 #elif defined(AVR32)
1360 tcp->u_arg[0] = regs.r12;
1361 tcp->u_arg[1] = regs.r11;
1362 tcp->u_arg[2] = regs.r10;
1363 tcp->u_arg[3] = regs.r9;
1364 tcp->u_arg[4] = regs.r5;
1365 tcp->u_arg[5] = regs.r3;
1367 static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
1369 for (i = 0; i < nargs; ++i)
1370 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1373 static const int syscall_regs[MAX_ARGS] = {
1374 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1375 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
1378 for (i = 0; i < nargs; ++i)
1379 if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
1383 /* Registers used by SH5 Linux system calls for parameters */
1384 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
1386 for (i = 0; i < nargs; ++i)
1387 if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1389 #elif defined(X86_64)
1392 if (current_personality == 0) { /* x86-64 ABI */
1393 tcp->u_arg[0] = x86_64_regs.rdi;
1394 tcp->u_arg[1] = x86_64_regs.rsi;
1395 tcp->u_arg[2] = x86_64_regs.rdx;
1396 tcp->u_arg[3] = x86_64_regs.r10;
1397 tcp->u_arg[4] = x86_64_regs.r8;
1398 tcp->u_arg[5] = x86_64_regs.r9;
1399 } else { /* i386 ABI */
1400 /* Sign-extend lower 32 bits */
1401 tcp->u_arg[0] = (long)(int)x86_64_regs.rbx;
1402 tcp->u_arg[1] = (long)(int)x86_64_regs.rcx;
1403 tcp->u_arg[2] = (long)(int)x86_64_regs.rdx;
1404 tcp->u_arg[3] = (long)(int)x86_64_regs.rsi;
1405 tcp->u_arg[4] = (long)(int)x86_64_regs.rdi;
1406 tcp->u_arg[5] = (long)(int)x86_64_regs.rbp;
1408 #elif defined(MICROBLAZE)
1409 for (i = 0; i < nargs; ++i)
1410 if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1412 #elif defined(CRISV10) || defined(CRISV32)
1413 static const int crisregs[MAX_ARGS] = {
1414 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
1415 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
1418 for (i = 0; i < nargs; ++i)
1419 if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0)
1422 for (i = 0; i < nargs; ++i)
1423 if (upeek(tcp, PTREGS_OFFSET_REG(i), &tcp->u_arg[i]) < 0)
1426 for (i = 0; i < nargs; ++i)
1427 if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1432 tcp->u_arg[0] = i386_regs.ebx;
1433 tcp->u_arg[1] = i386_regs.ecx;
1434 tcp->u_arg[2] = i386_regs.edx;
1435 tcp->u_arg[3] = i386_regs.esi;
1436 tcp->u_arg[4] = i386_regs.edi;
1437 tcp->u_arg[5] = i386_regs.ebp;
1438 #else /* Other architecture (32bits specific) */
1439 for (i = 0; i < nargs; ++i)
1440 if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
1447 trace_syscall_entering(struct tcb *tcp)
1451 #if defined TCB_WAITEXECVE
1452 if (tcp->flags & TCB_WAITEXECVE) {
1453 /* This is the post-execve SIGTRAP. */
1454 tcp->flags &= ~TCB_WAITEXECVE;
1459 scno_good = res = get_scno(tcp);
1463 res = syscall_fixup_on_sysenter(tcp);
1467 res = get_syscall_args(tcp);
1471 tcp->flags &= ~TCB_REPRINT;
1473 tprints("????" /* anti-trigraph gap */ "(");
1474 else if (!SCNO_IN_RANGE(tcp->scno))
1475 tprintf("syscall_%lu(", tcp->scno);
1477 tprintf("%s(", sysent[tcp->scno].sys_name);
1479 * " <unavailable>" will be added later by the code which
1480 * detects ptrace errors.
1485 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
1486 while (SCNO_IN_RANGE(tcp->scno)) {
1487 # ifdef SYS_socket_subcall
1488 if (sysent[tcp->scno].sys_func == sys_socketcall) {
1489 decode_socket_subcall(tcp);
1493 # ifdef SYS_ipc_subcall
1494 if (sysent[tcp->scno].sys_func == sys_ipc) {
1495 decode_ipc_subcall(tcp);
1501 #endif /* SYS_socket_subcall || SYS_ipc_subcall */
1503 internal_syscall(tcp);
1505 if ((SCNO_IN_RANGE(tcp->scno) &&
1506 !(qual_flags[tcp->scno] & QUAL_TRACE)) ||
1507 (tracing_paths && !pathtrace_match(tcp))) {
1508 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1512 tcp->flags &= ~TCB_FILTERED;
1514 if (cflag == CFLAG_ONLY_STATS) {
1520 tcp->flags &= ~TCB_REPRINT;
1521 if (!SCNO_IN_RANGE(tcp->scno))
1522 tprintf("syscall_%lu(", tcp->scno);
1524 tprintf("%s(", sysent[tcp->scno].sys_name);
1525 if (!SCNO_IN_RANGE(tcp->scno) ||
1526 ((qual_flags[tcp->scno] & QUAL_RAW) &&
1527 sysent[tcp->scno].sys_func != sys_exit))
1528 res = printargs(tcp);
1530 res = (*sysent[tcp->scno].sys_func)(tcp);
1532 if (fflush(tcp->outf) == EOF)
1535 tcp->flags |= TCB_INSYSCALL;
1536 /* Measure the entrance time as late as possible to avoid errors. */
1538 gettimeofday(&tcp->etime, NULL);
1543 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1544 * 1: ok, continue in trace_syscall().
1545 * other: error, trace_syscall() should print error indicator
1546 * ("????" etc) and bail out.
1549 get_syscall_result(struct tcb *tcp)
1551 #if defined(S390) || defined(S390X)
1552 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1554 #elif defined(POWERPC)
1555 # define SO_MASK 0x10000000
1558 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1560 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &result) < 0)
1562 if (flags & SO_MASK)
1565 #elif defined(AVR32)
1566 /* Read complete register set in one go. */
1567 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, ®s) < 0)
1570 if (upeek(tcp, PT_R0, &r0) < 0)
1573 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &i386_regs) < 0)
1575 #elif defined(X86_64)
1576 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &x86_64_regs) < 0)
1579 # define IA64_PSR_IS ((long)1 << 34)
1580 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
1581 ia32 = (psr & IA64_PSR_IS) != 0;
1582 if (upeek(tcp, PT_R8, &r8) < 0)
1584 if (upeek(tcp, PT_R10, &r10) < 0)
1587 /* Read complete register set in one go. */
1588 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (void *)®s) == -1)
1591 if (upeek(tcp, 4*PT_D0, &d0) < 0)
1593 #elif defined(LINUX_MIPSN32)
1594 unsigned long long regs[38];
1596 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1601 if (upeek(tcp, REG_A3, &a3) < 0)
1603 if (upeek(tcp, REG_V0, &r2) < 0)
1605 #elif defined(ALPHA)
1606 if (upeek(tcp, REG_A3, &a3) < 0)
1608 if (upeek(tcp, REG_R0, &r0) < 0)
1610 #elif defined(SPARC) || defined(SPARC64)
1611 /* Everything we need is in the current register set. */
1612 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1615 if (upeek(tcp, PT_GR28, &r28) < 0)
1619 #elif defined(CRISV10) || defined(CRISV32)
1620 if (upeek(tcp, 4*PT_R10, &r10) < 0)
1623 #elif defined(MICROBLAZE)
1624 if (upeek(tcp, 3 * 4, &r3) < 0)
1629 /* new syscall ABI returns result in R0 */
1630 if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0)
1633 /* ABI defines result returned in r9 */
1634 if (upeek(tcp, REG_GENERAL(9), (long *)&r9) < 0)
1641 /* Called at each syscall exit */
1643 syscall_fixup_on_sysexit(struct tcb *tcp)
1645 #if defined(S390) || defined(S390X)
1646 if (syscall_mode != -ENOSYS)
1647 syscall_mode = tcp->scno;
1648 if ((tcp->flags & TCB_WAITEXECVE)
1649 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1651 * Return from execve.
1652 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1653 * flag set for the post-execve SIGTRAP to see and reset.
1661 * Check the syscall return value register value for whether it is
1662 * a negated errno code indicating an error, or a success return value.
1665 is_negated_errno(unsigned long int val)
1667 unsigned long int max = -(long int) nerrnos;
1668 #if SUPPORTED_PERSONALITIES > 1
1669 if (current_wordsize < sizeof(val)) {
1670 val = (unsigned int) val;
1671 max = (unsigned int) max;
1678 get_error(struct tcb *tcp)
1681 int check_errno = 1;
1682 if (SCNO_IN_RANGE(tcp->scno) &&
1683 sysent[tcp->scno].sys_flags & SYSCALL_NEVER_FAILS) {
1686 #if defined(S390) || defined(S390X)
1687 if (check_errno && is_negated_errno(gpr2)) {
1695 if (check_errno && is_negated_errno(i386_regs.eax)) {
1697 u_error = -i386_regs.eax;
1700 tcp->u_rval = i386_regs.eax;
1702 #elif defined(X86_64)
1703 if (check_errno && is_negated_errno(x86_64_regs.rax)) {
1705 u_error = -x86_64_regs.rax;
1708 tcp->u_rval = x86_64_regs.rax;
1715 if (check_errno && is_negated_errno(err)) {
1723 if (check_errno && r10) {
1731 if (check_errno && a3) {
1737 #elif defined(POWERPC)
1738 if (check_errno && is_negated_errno(result)) {
1743 tcp->u_rval = result;
1746 if (check_errno && is_negated_errno(d0)) {
1754 if (check_errno && is_negated_errno(regs.ARM_r0)) {
1756 u_error = -regs.ARM_r0;
1759 tcp->u_rval = regs.ARM_r0;
1761 #elif defined(AVR32)
1762 if (check_errno && regs.r12 && (unsigned) -regs.r12 < nerrnos) {
1764 u_error = -regs.r12;
1767 tcp->u_rval = regs.r12;
1770 if (check_errno && is_negated_errno(r0)) {
1776 #elif defined(ALPHA)
1777 if (check_errno && a3) {
1784 #elif defined(SPARC)
1785 if (check_errno && regs.psr & PSR_C) {
1787 u_error = regs.u_regs[U_REG_O0];
1790 tcp->u_rval = regs.u_regs[U_REG_O0];
1792 #elif defined(SPARC64)
1793 if (check_errno && regs.tstate & 0x1100000000UL) {
1795 u_error = regs.u_regs[U_REG_O0];
1798 tcp->u_rval = regs.u_regs[U_REG_O0];
1801 if (check_errno && is_negated_errno(r28)) {
1809 if (check_errno && is_negated_errno(r0)) {
1817 if (check_errno && is_negated_errno(r9)) {
1824 #elif defined(CRISV10) || defined(CRISV32)
1825 if (check_errno && r10 && (unsigned) -r10 < nerrnos) {
1834 if (upeek(tcp, PTREGS_OFFSET_REG(0), &rval) < 0)
1836 if (check_errno && rval < 0 && rval > -nerrnos) {
1843 #elif defined(MICROBLAZE)
1844 if (check_errno && is_negated_errno(r3)) {
1852 tcp->u_error = u_error;
1857 dumpio(struct tcb *tcp)
1861 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
1863 if (!SCNO_IN_RANGE(tcp->scno))
1865 if (sysent[tcp->scno].sys_func == printargs)
1867 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
1868 if (sysent[tcp->scno].sys_func == sys_read ||
1869 sysent[tcp->scno].sys_func == sys_pread ||
1870 sysent[tcp->scno].sys_func == sys_recv ||
1871 sysent[tcp->scno].sys_func == sys_recvfrom)
1872 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
1873 else if (sysent[tcp->scno].sys_func == sys_readv)
1874 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
1877 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
1878 if (sysent[tcp->scno].sys_func == sys_write ||
1879 sysent[tcp->scno].sys_func == sys_pwrite ||
1880 sysent[tcp->scno].sys_func == sys_send ||
1881 sysent[tcp->scno].sys_func == sys_sendto)
1882 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
1883 else if (sysent[tcp->scno].sys_func == sys_writev)
1884 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
1890 trace_syscall_exiting(struct tcb *tcp)
1897 /* Measure the exit time as early as possible to avoid errors. */
1899 gettimeofday(&tv, NULL);
1901 #if SUPPORTED_PERSONALITIES > 1
1902 update_personality(tcp, tcp->currpers);
1904 res = get_syscall_result(tcp);
1908 syscall_fixup_on_sysexit(tcp); /* never fails */
1909 res = get_error(tcp);
1914 internal_syscall(tcp);
1916 if (res == 1 && filtered(tcp)) {
1920 /* TODO: TCB_REPRINT is probably not necessary:
1921 * we can determine whether reprinting is needed
1922 * by examining printing_tcp. Something like:
1923 * if not in -ff mode, and printing_tcp != tcp,
1924 * then the log is not currenlty ends with *our*
1925 * syscall entry output, but with something else,
1926 * and we need to reprint.
1927 * If we'd implement this, printing_tcp = tcp
1928 * assignments in code below can be made more logical.
1931 if (tcp->flags & TCB_REPRINT) {
1933 if (!SCNO_IN_RANGE(tcp->scno))
1934 tprintf("<... syscall_%lu resumed> ", tcp->scno);
1936 tprintf("<... %s resumed> ", sysent[tcp->scno].sys_name);
1940 struct timeval t = tv;
1941 count_syscall(tcp, &t);
1942 if (cflag == CFLAG_ONLY_STATS) {
1951 tprints("= ? <unavailable>\n");
1953 tcp->flags &= ~TCB_INSYSCALL;
1957 if (!SCNO_IN_RANGE(tcp->scno)
1958 || (qual_flags[tcp->scno] & QUAL_RAW)) {
1960 sys_res = printargs(tcp);
1962 /* FIXME: not_failing_only (IOW, option -z) is broken:
1963 * failure of syscall is known only after syscall return.
1964 * Thus we end up with something like this on, say, ENOENT:
1965 * open("doesnt_exist", O_RDONLY <unfinished ...>
1966 * {next syscall decode}
1967 * whereas the intended result is that open(...) line
1968 * is not shown at all.
1970 if (not_failing_only && tcp->u_error)
1971 goto ret; /* ignore failed syscalls */
1973 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1978 u_error = tcp->u_error;
1979 if (!SCNO_IN_RANGE(tcp->scno) ||
1980 qual_flags[tcp->scno] & QUAL_RAW) {
1982 tprintf("= -1 (errno %ld)", u_error);
1984 tprintf("= %#lx", tcp->u_rval);
1986 else if (!(sys_res & RVAL_NONE) && u_error) {
1988 /* Blocked signals do not interrupt any syscalls.
1989 * In this case syscalls don't return ERESTARTfoo codes.
1991 * Deadly signals set to SIG_DFL interrupt syscalls
1992 * and kill the process regardless of which of the codes below
1993 * is returned by the interrupted syscall.
1994 * In some cases, kernel forces a kernel-generated deadly
1995 * signal to be unblocked and set to SIG_DFL (and thus cause
1996 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
1997 * or SIGILL. (The alternative is to leave process spinning
1998 * forever on the faulty instruction - not useful).
2000 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2001 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2002 * but kernel will always restart them.
2005 /* Most common type of signal-interrupted syscall exit code.
2006 * The system call will be restarted with the same arguments
2007 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2009 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
2011 case ERESTARTNOINTR:
2012 /* Rare. For example, fork() returns this if interrupted.
2013 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2015 tprints("= ? ERESTARTNOINTR (To be restarted)");
2017 case ERESTARTNOHAND:
2018 /* pause(), rt_sigsuspend() etc use this code.
2019 * SA_RESTART is ignored (assumed not set):
2020 * syscall won't restart (will return EINTR instead)
2021 * even after signal with SA_RESTART set.
2022 * However, after SIG_IGN or SIG_DFL signal it will.
2024 tprints("= ? ERESTARTNOHAND (Interrupted by signal)");
2026 case ERESTART_RESTARTBLOCK:
2027 /* Syscalls like nanosleep(), poll() which can't be
2028 * restarted with their original arguments use this
2029 * code. Kernel will execute restart_syscall() instead,
2030 * which changes arguments before restarting syscall.
2031 * SA_RESTART is ignored (assumed not set) similarly
2032 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2033 * since restart data is saved in "restart block"
2034 * in task struct, and if signal handler uses a syscall
2035 * which in turn saves another such restart block,
2036 * old data is lost and restart becomes impossible)
2038 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
2042 tprintf("= -1 E??? (errno %ld)", u_error);
2043 else if (u_error < nerrnos)
2044 tprintf("= -1 %s (%s)", errnoent[u_error],
2047 tprintf("= -1 ERRNO_%ld (%s)", u_error,
2051 if ((sys_res & RVAL_STR) && tcp->auxstr)
2052 tprintf(" (%s)", tcp->auxstr);
2055 if (sys_res & RVAL_NONE)
2058 switch (sys_res & RVAL_MASK) {
2060 tprintf("= %#lx", tcp->u_rval);
2063 tprintf("= %#lo", tcp->u_rval);
2066 tprintf("= %lu", tcp->u_rval);
2069 tprintf("= %ld", tcp->u_rval);
2073 "invalid rval format\n");
2077 if ((sys_res & RVAL_STR) && tcp->auxstr)
2078 tprintf(" (%s)", tcp->auxstr);
2081 tv_sub(&tv, &tv, &tcp->etime);
2082 tprintf(" <%ld.%06ld>",
2083 (long) tv.tv_sec, (long) tv.tv_usec);
2090 tcp->flags &= ~TCB_INSYSCALL;
2095 trace_syscall(struct tcb *tcp)
2097 return exiting(tcp) ?
2098 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);