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.
39 #include <sys/param.h>
43 # ifndef PTRACE_PEEKUSR
44 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
46 #elif defined(HAVE_LINUX_PTRACE_H)
47 # undef PTRACE_SYSCALL
48 # ifdef HAVE_STRUCT_IA64_FPREG
49 # define ia64_fpreg XXX_ia64_fpreg
51 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
52 # define pt_all_user_regs XXX_pt_all_user_regs
54 # include <linux/ptrace.h>
56 # undef pt_all_user_regs
60 # undef PTRACE_GETREGS
61 # define PTRACE_GETREGS PTRACE_GETREGS64
62 # undef PTRACE_SETREGS
63 # define PTRACE_SETREGS PTRACE_SETREGS64
67 # include <asm/ptrace_offsets.h>
72 # define ERESTARTSYS 512
74 # ifndef ERESTARTNOINTR
75 # define ERESTARTNOINTR 513
77 # ifndef ERESTARTNOHAND
78 # define ERESTARTNOHAND 514 /* restart if no handler.. */
81 # define ENOIOCTLCMD 515 /* No ioctl command */
83 # ifndef ERESTART_RESTARTBLOCK
84 # define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
88 # warning: NSIG is not defined, using 32
92 /* Ugh. Is this really correct? ARM has no RT signals?! */
99 /* Define these shorthand notations to simplify the syscallent files. */
100 #define TD TRACE_DESC
101 #define TF TRACE_FILE
103 #define TN TRACE_NETWORK
104 #define TP TRACE_PROCESS
105 #define TS TRACE_SIGNAL
106 #define NF SYSCALL_NEVER_FAILS
109 static const struct sysent sysent0[] = {
110 #include "syscallent.h"
113 #if SUPPORTED_PERSONALITIES >= 2
114 static const struct sysent sysent1[] = {
115 # include "syscallent1.h"
119 #if SUPPORTED_PERSONALITIES >= 3
120 static const struct sysent sysent2[] = {
121 # include "syscallent2.h"
125 /* Now undef them since short defines cause wicked namespace pollution. */
136 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
137 * program `ioctlsort', such that the list is sorted by the `code' field.
138 * This has the side-effect of resolving the _IO.. macros into
139 * plain integers, eliminating the need to include here everything
143 static const char *const errnoent0[] = {
144 #include "errnoent.h"
146 static const char *const signalent0[] = {
147 #include "signalent.h"
149 static const struct ioctlent ioctlent0[] = {
150 #include "ioctlent.h"
152 enum { nsyscalls0 = ARRAY_SIZE(sysent0) };
153 enum { nerrnos0 = ARRAY_SIZE(errnoent0) };
154 enum { nsignals0 = ARRAY_SIZE(signalent0) };
155 enum { nioctlents0 = ARRAY_SIZE(ioctlent0) };
156 int qual_flags0[MAX_QUALS];
158 #if SUPPORTED_PERSONALITIES >= 2
159 static const char *const errnoent1[] = {
160 # include "errnoent1.h"
162 static const char *const signalent1[] = {
163 # include "signalent1.h"
165 static const struct ioctlent ioctlent1[] = {
166 # include "ioctlent1.h"
168 enum { nsyscalls1 = ARRAY_SIZE(sysent1) };
169 enum { nerrnos1 = ARRAY_SIZE(errnoent1) };
170 enum { nsignals1 = ARRAY_SIZE(signalent1) };
171 enum { nioctlents1 = ARRAY_SIZE(ioctlent1) };
172 int qual_flags1[MAX_QUALS];
175 #if SUPPORTED_PERSONALITIES >= 3
176 static const char *const errnoent2[] = {
177 # include "errnoent2.h"
179 static const char *const signalent2[] = {
180 # include "signalent2.h"
182 static const struct ioctlent ioctlent2[] = {
183 # include "ioctlent2.h"
185 enum { nsyscalls2 = ARRAY_SIZE(sysent2) };
186 enum { nerrnos2 = ARRAY_SIZE(errnoent2) };
187 enum { nsignals2 = ARRAY_SIZE(signalent2) };
188 enum { nioctlents2 = ARRAY_SIZE(ioctlent2) };
189 int qual_flags2[MAX_QUALS];
192 const struct sysent *sysent;
193 const char *const *errnoent;
194 const char *const *signalent;
195 const struct ioctlent *ioctlent;
202 int current_personality;
204 #ifndef PERSONALITY0_WORDSIZE
205 # define PERSONALITY0_WORDSIZE sizeof(long)
207 const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
208 PERSONALITY0_WORDSIZE,
209 #if SUPPORTED_PERSONALITIES > 1
210 PERSONALITY1_WORDSIZE,
212 #if SUPPORTED_PERSONALITIES > 2
213 PERSONALITY2_WORDSIZE,
218 set_personality(int personality)
220 switch (personality) {
222 errnoent = errnoent0;
225 nsyscalls = nsyscalls0;
226 ioctlent = ioctlent0;
227 nioctlents = nioctlents0;
228 signalent = signalent0;
229 nsignals = nsignals0;
230 qual_flags = qual_flags0;
233 #if SUPPORTED_PERSONALITIES >= 2
235 errnoent = errnoent1;
238 nsyscalls = nsyscalls1;
239 ioctlent = ioctlent1;
240 nioctlents = nioctlents1;
241 signalent = signalent1;
242 nsignals = nsignals1;
243 qual_flags = qual_flags1;
247 #if SUPPORTED_PERSONALITIES >= 3
249 errnoent = errnoent2;
252 nsyscalls = nsyscalls2;
253 ioctlent = ioctlent2;
254 nioctlents = nioctlents2;
255 signalent = signalent2;
256 nsignals = nsignals2;
257 qual_flags = qual_flags2;
262 current_personality = personality;
265 #if SUPPORTED_PERSONALITIES > 1
267 update_personality(struct tcb *tcp, int personality)
269 if (personality == current_personality)
271 set_personality(personality);
273 if (personality == tcp->currpers)
275 tcp->currpers = personality;
277 # if defined(POWERPC64) || defined(X86_64)
279 static const char *const names[] = {"64 bit", "32 bit"};
280 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
281 tcp->pid, names[personality]);
287 static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
289 static const struct qual_options {
291 const char *option_name;
292 int (*qualify)(const char *, int, int);
293 const char *argument_name;
295 { QUAL_TRACE, "trace", qual_syscall, "system call" },
296 { QUAL_TRACE, "t", qual_syscall, "system call" },
297 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
298 { QUAL_ABBREV, "a", qual_syscall, "system call" },
299 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
300 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
301 { QUAL_RAW, "raw", qual_syscall, "system call" },
302 { QUAL_RAW, "x", qual_syscall, "system call" },
303 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
304 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
305 { QUAL_SIGNAL, "s", qual_signal, "signal" },
306 { QUAL_FAULT, "fault", qual_fault, "fault" },
307 { QUAL_FAULT, "faults", qual_fault, "fault" },
308 { QUAL_FAULT, "m", qual_fault, "fault" },
309 { QUAL_READ, "read", qual_desc, "descriptor" },
310 { QUAL_READ, "reads", qual_desc, "descriptor" },
311 { QUAL_READ, "r", qual_desc, "descriptor" },
312 { QUAL_WRITE, "write", qual_desc, "descriptor" },
313 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
314 { QUAL_WRITE, "w", qual_desc, "descriptor" },
315 { 0, NULL, NULL, NULL },
319 qualify_one(int n, int bitflag, int not, int pers)
321 if (pers == 0 || pers < 0) {
323 qual_flags0[n] &= ~bitflag;
325 qual_flags0[n] |= bitflag;
328 #if SUPPORTED_PERSONALITIES >= 2
329 if (pers == 1 || pers < 0) {
331 qual_flags1[n] &= ~bitflag;
333 qual_flags1[n] |= bitflag;
337 #if SUPPORTED_PERSONALITIES >= 3
338 if (pers == 2 || pers < 0) {
340 qual_flags2[n] &= ~bitflag;
342 qual_flags2[n] |= bitflag;
348 qual_syscall(const char *s, int bitflag, int not)
353 if (isdigit((unsigned char)*s)) {
355 if (i < 0 || i >= MAX_QUALS)
357 qualify_one(i, bitflag, not, -1);
360 for (i = 0; i < nsyscalls0; i++)
361 if (strcmp(s, sysent0[i].sys_name) == 0) {
362 qualify_one(i, bitflag, not, 0);
366 #if SUPPORTED_PERSONALITIES >= 2
367 for (i = 0; i < nsyscalls1; i++)
368 if (strcmp(s, sysent1[i].sys_name) == 0) {
369 qualify_one(i, bitflag, not, 1);
374 #if SUPPORTED_PERSONALITIES >= 3
375 for (i = 0; i < nsyscalls2; i++)
376 if (strcmp(s, sysent2[i].sys_name) == 0) {
377 qualify_one(i, bitflag, not, 2);
386 qual_signal(const char *s, int bitflag, int not)
391 if (isdigit((unsigned char)*s)) {
393 if (signo < 0 || signo >= MAX_QUALS)
395 qualify_one(signo, bitflag, not, -1);
398 if (strlen(s) >= sizeof buf)
402 if (strncasecmp(s, "SIG", 3) == 0)
404 for (i = 0; i <= NSIG; i++)
405 if (strcasecmp(s, signame(i) + 3) == 0) {
406 qualify_one(i, bitflag, not, -1);
413 qual_fault(const char *s, int bitflag, int not)
419 qual_desc(const char *s, int bitflag, int not)
421 if (isdigit((unsigned char)*s)) {
423 if (desc < 0 || desc >= MAX_QUALS)
425 qualify_one(desc, bitflag, not, -1);
432 lookup_class(const char *s)
434 if (strcmp(s, "file") == 0)
436 if (strcmp(s, "ipc") == 0)
438 if (strcmp(s, "network") == 0)
439 return TRACE_NETWORK;
440 if (strcmp(s, "process") == 0)
441 return TRACE_PROCESS;
442 if (strcmp(s, "signal") == 0)
444 if (strcmp(s, "desc") == 0)
450 qualify(const char *s)
452 const struct qual_options *opt;
458 opt = &qual_options[0];
459 for (i = 0; (p = qual_options[i].option_name); i++) {
461 if (strncmp(s, p, n) == 0 && s[n] == '=') {
462 opt = &qual_options[i];
472 if (strcmp(s, "none") == 0) {
476 if (strcmp(s, "all") == 0) {
477 for (i = 0; i < MAX_QUALS; i++) {
478 qualify_one(i, opt->bitflag, not, -1);
482 for (i = 0; i < MAX_QUALS; i++) {
483 qualify_one(i, opt->bitflag, !not, -1);
488 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
489 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
490 for (i = 0; i < nsyscalls0; i++)
491 if (sysent0[i].sys_flags & n)
492 qualify_one(i, opt->bitflag, not, 0);
494 #if SUPPORTED_PERSONALITIES >= 2
495 for (i = 0; i < nsyscalls1; i++)
496 if (sysent1[i].sys_flags & n)
497 qualify_one(i, opt->bitflag, not, 1);
500 #if SUPPORTED_PERSONALITIES >= 3
501 for (i = 0; i < nsyscalls2; i++)
502 if (sysent2[i].sys_flags & n)
503 qualify_one(i, opt->bitflag, not, 2);
508 if (opt->qualify(p, opt->bitflag, not)) {
509 error_msg_and_die("invalid %s '%s'",
510 opt->argument_name, p);
517 enum subcall_style { shift_style, deref_style, mask_style, door_style };
519 #if !(defined(ALPHA) || defined(MIPS) || defined(__ARM_EABI__))
522 decode_subcall(struct tcb *tcp, int subcall, int nsubcalls, enum subcall_style style)
524 unsigned long addr, mask;
526 int size = personality_wordsize[current_personality];
530 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
532 tcp->scno = subcall + tcp->u_arg[0];
533 tcp->u_nargs = n = sysent[tcp->scno].nargs;
534 for (i = 0; i < n; i++)
535 tcp->u_arg[i] = tcp->u_arg[i + 1];
538 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
540 tcp->scno = subcall + tcp->u_arg[0];
541 addr = tcp->u_arg[1];
542 tcp->u_nargs = n = sysent[tcp->scno].nargs;
543 for (i = 0; i < n; i++) {
544 if (size == sizeof(int)) {
546 if (umove(tcp, addr, &arg) < 0)
550 else if (size == sizeof(long)) {
552 if (umove(tcp, addr, &arg) < 0)
562 mask = (tcp->u_arg[0] >> 8) & 0xff;
563 for (i = 0; mask; i++)
567 tcp->u_arg[0] &= 0xff;
568 tcp->scno = subcall + i;
569 tcp->u_nargs = sysent[tcp->scno].nargs;
573 * Oh, yuck. The call code is the *sixth* argument.
574 * (don't you mean the *last* argument? - JH)
576 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
578 tcp->scno = subcall + tcp->u_arg[5];
579 tcp->u_nargs = sysent[tcp->scno].nargs;
586 printargs(struct tcb *tcp)
591 for (i = 0; i < tcp->u_nargs; i++)
592 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
598 printargs_lu(struct tcb *tcp)
603 for (i = 0; i < tcp->u_nargs; i++)
604 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
610 printargs_ld(struct tcb *tcp)
615 for (i = 0; i < tcp->u_nargs; i++)
616 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
622 getrval2(struct tcb *tcp)
626 #if defined(SPARC) || defined(SPARC64)
628 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
630 val = regs.u_regs[U_REG_O1];
632 if (upeek(tcp, 4*(REG_REG0+1), &val) < 0)
635 if (upeek(tcp, PT_R9, &val) < 0)
643 is_restart_error(struct tcb *tcp)
645 switch (tcp->u_error) {
649 case ERESTART_RESTARTBLOCK:
658 struct pt_regs i386_regs;
659 #elif defined(X86_64)
661 * On 32 bits, pt_regs and user_regs_struct are the same,
662 * but on 64 bits, user_regs_struct has six more fields:
663 * fs_base, gs_base, ds, es, fs, gs.
664 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
666 static struct user_regs_struct x86_64_regs;
668 long r8, r10, psr; /* TODO: make static? */
669 long ia32 = 0; /* not static */
670 #elif defined(POWERPC)
677 static struct pt_regs regs;
682 static struct pt_regs regs;
683 #elif defined(SPARC) || defined(SPARC64)
684 static struct pt_regs regs;
685 static unsigned long trap;
686 #elif defined(LINUX_MIPSN32)
692 #elif defined(S390) || defined(S390X)
695 static long syscall_mode;
702 #elif defined(CRISV10) || defined(CRISV32)
704 #elif defined(MICROBLAZE)
709 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
710 * 1: ok, continue in trace_syscall().
711 * other: error, trace_syscall() should print error indicator
712 * ("????" etc) and bail out.
716 get_scno(struct tcb *tcp)
720 #if defined(S390) || defined(S390X)
721 if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
724 if (syscall_mode != -ENOSYS) {
726 * Since kernel version 2.5.44 the scno gets passed in gpr2.
731 * Old style of "passing" the scno via the SVC instruction.
733 long opcode, offset_reg, tmp;
735 static const int gpr_offset[16] = {
736 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
737 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
738 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
739 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
742 if (upeek(tcp, PT_PSWADDR, &pc) < 0)
745 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-sizeof(long)), 0);
747 perror("peektext(pc-oneword)");
752 * We have to check if the SVC got executed directly or via an
753 * EXECUTE instruction. In case of EXECUTE it is necessary to do
754 * instruction decoding to derive the system call number.
755 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
756 * so that this doesn't work if a SVC opcode is part of an EXECUTE
757 * opcode. Since there is no way to find out the opcode size this
758 * is the best we can do...
760 if ((opcode & 0xff00) == 0x0a00) {
762 scno = opcode & 0xff;
765 /* SVC got executed by EXECUTE instruction */
768 * Do instruction decoding of EXECUTE. If you really want to
769 * understand this, read the Principles of Operations.
771 svc_addr = (void *) (opcode & 0xfff);
774 offset_reg = (opcode & 0x000f0000) >> 16;
775 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
780 offset_reg = (opcode & 0x0000f000) >> 12;
781 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
785 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
794 offset_reg = (opcode & 0x00f00000) >> 20;
795 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
798 scno = (scno | tmp) & 0xff;
801 #elif defined(POWERPC)
802 if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
805 /* TODO: speed up strace by not doing this at every syscall.
806 * We only need to do it after execve.
811 /* Check for 64/32 bit mode. */
812 if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
814 /* SF is bit 0 of MSR */
819 update_personality(tcp, currpers);
822 /* Read complete register set in one go. */
823 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, ®s) < 0)
827 if (upeek(tcp, PT_ORIG_P0, &scno))
830 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &i386_regs) < 0)
832 scno = i386_regs.orig_eax;
833 #elif defined(X86_64)
835 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &x86_64_regs) < 0)
837 scno = x86_64_regs.orig_rax;
839 /* Check CS register value. On x86-64 linux it is:
840 * 0x33 for long mode (64 bit)
841 * 0x23 for compatibility mode (32 bit)
843 switch (x86_64_regs.cs) {
844 case 0x23: currpers = 1; break;
845 case 0x33: currpers = 0; break;
847 fprintf(stderr, "Unknown value CS=0x%08X while "
848 "detecting personality of process "
849 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
850 currpers = current_personality;
854 /* This version analyzes the opcode of a syscall instruction.
855 * (int 0x80 on i386 vs. syscall on x86-64)
856 * It works, but is too complicated.
858 unsigned long val, rip, i;
860 rip = x86_64_regs.rip;
862 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
866 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
868 fprintf(stderr, "ptrace_peektext failed: %s\n",
870 switch (call & 0xffff) {
871 /* x86-64: syscall = 0x0f 0x05 */
872 case 0x050f: currpers = 0; break;
873 /* i386: int 0x80 = 0xcd 0x80 */
874 case 0x80cd: currpers = 1; break;
876 currpers = current_personality;
878 "Unknown syscall opcode (0x%04X) while "
879 "detecting personality of process "
880 "PID=%d\n", (int)call, tcp->pid);
884 update_personality(tcp, currpers);
886 # define IA64_PSR_IS ((long)1 << 34)
887 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
888 ia32 = (psr & IA64_PSR_IS) != 0;
890 if (upeek(tcp, PT_R1, &scno) < 0)
893 if (upeek(tcp, PT_R15, &scno) < 0)
897 /* Read complete register set in one go. */
898 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (void *)®s) == -1)
902 * We only need to grab the syscall number on syscall entry.
904 if (regs.ARM_ip == 0) {
906 * Note: we only deal with only 32-bit CPUs here.
908 if (regs.ARM_cpsr & 0x20) {
910 * Get the Thumb-mode system call number
915 * Get the ARM-mode system call number
918 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(regs.ARM_pc - 4), NULL);
922 /* Handle the EABI syscall convention. We do not
923 bother converting structures between the two
924 ABIs, but basic functionality should work even
925 if strace and the traced program have different
927 if (scno == 0xef000000) {
930 if ((scno & 0x0ff00000) != 0x0f900000) {
931 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
937 * Fixup the syscall number
942 if (scno & 0x0f0000) {
944 * Handle ARM specific syscall
946 update_personality(tcp, 1);
949 update_personality(tcp, 0);
952 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
953 tcp->flags |= TCB_INSYSCALL;
956 if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
958 #elif defined(LINUX_MIPSN32)
959 unsigned long long regs[38];
961 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
967 if (!SCNO_IN_RANGE(scno)) {
968 if (a3 == 0 || a3 == -1) {
970 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
975 if (upeek(tcp, REG_A3, &a3) < 0)
977 if (upeek(tcp, REG_V0, &scno) < 0)
980 if (!SCNO_IN_RANGE(scno)) {
981 if (a3 == 0 || a3 == -1) {
983 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
988 if (upeek(tcp, REG_A3, &a3) < 0)
990 if (upeek(tcp, REG_R0, &scno) < 0)
994 * Do some sanity checks to figure out if it's
995 * really a syscall entry
997 if (!SCNO_IN_RANGE(scno)) {
998 if (a3 == 0 || a3 == -1) {
1000 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
1004 #elif defined(SPARC) || defined(SPARC64)
1005 /* Everything we need is in the current register set. */
1006 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1009 /* Disassemble the syscall trap. */
1010 /* Retrieve the syscall trap instruction. */
1012 # if defined(SPARC64)
1013 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.tpc, 0);
1016 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.pc, 0);
1021 /* Disassemble the trap to see what personality to use. */
1024 /* Linux/SPARC syscall trap. */
1025 update_personality(tcp, 0);
1028 /* Linux/SPARC64 syscall trap. */
1029 update_personality(tcp, 2);
1032 /* SunOS syscall trap. (pers 1) */
1033 fprintf(stderr, "syscall: SunOS no support\n");
1036 /* Solaris 2.x syscall trap. (per 2) */
1037 update_personality(tcp, 1);
1040 /* NetBSD/FreeBSD syscall trap. */
1041 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1044 /* Solaris 2.x gettimeofday */
1045 update_personality(tcp, 1);
1048 # if defined(SPARC64)
1049 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, regs.tpc);
1051 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, regs.pc);
1056 /* Extract the system call number from the registers. */
1057 if (trap == 0x91d02027)
1060 scno = regs.u_regs[U_REG_G1];
1062 scno = regs.u_regs[U_REG_O0];
1063 memmove(®s.u_regs[U_REG_O0], ®s.u_regs[U_REG_O1], 7*sizeof(regs.u_regs[0]));
1066 if (upeek(tcp, PT_GR20, &scno) < 0)
1070 * In the new syscall ABI, the system call number is in R3.
1072 if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
1076 /* Odd as it may seem, a glibc bug has been known to cause
1077 glibc to issue bogus negative syscall numbers. So for
1078 our purposes, make strace print what it *should* have been */
1079 long correct_scno = (scno & 0xff);
1082 "Detected glibc bug: bogus system call"
1083 " number = %ld, correcting to %ld\n",
1086 scno = correct_scno;
1089 if (upeek(tcp, REG_SYSCALL, &scno) < 0)
1092 #elif defined(CRISV10) || defined(CRISV32)
1093 if (upeek(tcp, 4*PT_R9, &scno) < 0)
1096 if (upeek(tcp, PTREGS_OFFSET_REG(10), &scno) < 0)
1098 #elif defined(MICROBLAZE)
1099 if (upeek(tcp, 0, &scno) < 0)
1104 /* new syscall ABI returns result in R0 */
1105 if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0)
1108 /* ABI defines result returned in r9 */
1109 if (upeek(tcp, REG_GENERAL(9), (long *)&r9) < 0)
1117 /* Called at each syscall entry.
1119 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1120 * 1: ok, continue in trace_syscall().
1121 * other: error, trace_syscall() should print error indicator
1122 * ("????" etc) and bail out.
1125 syscall_fixup_on_sysenter(struct tcb *tcp)
1127 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
1129 if (i386_regs.eax != -ENOSYS) {
1131 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1134 #elif defined(X86_64)
1136 long rax = x86_64_regs.rax;
1137 if (current_personality == 1)
1138 rax = (int)rax; /* sign extend from 32 bits */
1139 if (rax != -ENOSYS) {
1141 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1145 #elif defined(S390) || defined(S390X)
1146 /* TODO: we already fetched PT_GPR2 in get_scno
1147 * and stored it in syscall_mode, reuse it here
1148 * instead of re-fetching?
1150 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1152 if (syscall_mode != -ENOSYS)
1153 syscall_mode = tcp->scno;
1154 if (gpr2 != syscall_mode) {
1156 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
1160 /* TODO? Eliminate upeek's in arches below like we did in x86 */
1161 if (upeek(tcp, 4*PT_D0, &d0) < 0)
1163 if (d0 != -ENOSYS) {
1165 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", d0);
1169 if (upeek(tcp, PT_R10, &r10) < 0)
1171 if (upeek(tcp, PT_R8, &r8) < 0)
1173 if (ia32 && r8 != -ENOSYS) {
1175 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", r8);
1178 #elif defined(CRISV10) || defined(CRISV32)
1179 if (upeek(tcp, 4*PT_R10, &r10) < 0)
1181 if (r10 != -ENOSYS) {
1183 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", r10);
1186 #elif defined(MICROBLAZE)
1187 if (upeek(tcp, 3 * 4, &r3) < 0)
1189 if (r3 != -ENOSYS) {
1191 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", r3);
1199 internal_syscall(struct tcb *tcp)
1202 * We must always trace a few critical system calls in order to
1203 * correctly support following forks in the presence of tracing
1208 if (!SCNO_IN_RANGE(tcp->scno))
1211 func = sysent[tcp->scno].sys_func;
1213 if ( sys_fork == func
1214 || sys_vfork == func
1215 || sys_clone == func
1217 return internal_fork(tcp);
1219 #if defined(TCB_WAITEXECVE)
1220 if ( sys_execve == func
1221 # if defined(SPARC) || defined(SPARC64)
1222 || sys_execv == func
1225 return internal_exec(tcp);
1232 syscall_enter(struct tcb *tcp)
1236 if (SCNO_IN_RANGE(tcp->scno))
1237 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1239 nargs = tcp->u_nargs = MAX_ARGS;
1241 #if defined(S390) || defined(S390X)
1242 for (i = 0; i < nargs; ++i)
1243 if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1245 #elif defined(ALPHA)
1246 for (i = 0; i < nargs; ++i)
1247 if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
1251 unsigned long *out0, cfm, sof, sol;
1253 /* be backwards compatible with kernel < 2.4.4... */
1255 # define PT_RBS_END PT_AR_BSP
1258 if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
1260 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1263 sof = (cfm >> 0) & 0x7f;
1264 sol = (cfm >> 7) & 0x7f;
1265 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1267 for (i = 0; i < nargs; ++i) {
1268 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1269 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1273 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1274 PT_R9 /* ECX = out1 */,
1275 PT_R10 /* EDX = out2 */,
1276 PT_R14 /* ESI = out3 */,
1277 PT_R15 /* EDI = out4 */,
1278 PT_R13 /* EBP = out5 */};
1280 for (i = 0; i < nargs; ++i) {
1281 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1283 /* truncate away IVE sign-extension */
1284 tcp->u_arg[i] &= 0xffffffff;
1287 #elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
1288 /* N32 and N64 both use up to six registers. */
1289 unsigned long long regs[38];
1291 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1294 for (i = 0; i < nargs; ++i) {
1295 tcp->u_arg[i] = regs[REG_A0 + i];
1296 # if defined(LINUX_MIPSN32)
1297 tcp->ext_arg[i] = regs[REG_A0 + i];
1304 if (upeek(tcp, REG_SP, &sp) < 0)
1306 for (i = 0; i < 4; ++i)
1307 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1309 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
1310 (char *)(tcp->u_arg + 4));
1312 for (i = 0; i < nargs; ++i)
1313 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1316 #elif defined(POWERPC)
1318 # define PT_ORIG_R3 34
1320 for (i = 0; i < nargs; ++i) {
1321 if (upeek(tcp, (i==0) ?
1322 (sizeof(unsigned long) * PT_ORIG_R3) :
1323 ((i+PT_R3) * sizeof(unsigned long)),
1324 &tcp->u_arg[i]) < 0)
1327 #elif defined(SPARC) || defined(SPARC64)
1328 for (i = 0; i < nargs; ++i)
1329 tcp->u_arg[i] = regs.u_regs[U_REG_O0 + i];
1331 for (i = 0; i < nargs; ++i)
1332 if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1335 for (i = 0; i < nargs; ++i)
1336 tcp->u_arg[i] = regs.uregs[i];
1337 #elif defined(AVR32)
1340 tcp->u_arg[0] = regs.r12;
1341 tcp->u_arg[1] = regs.r11;
1342 tcp->u_arg[2] = regs.r10;
1343 tcp->u_arg[3] = regs.r9;
1344 tcp->u_arg[4] = regs.r5;
1345 tcp->u_arg[5] = regs.r3;
1347 static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
1349 for (i = 0; i < nargs; ++i)
1350 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1353 static const int syscall_regs[MAX_ARGS] = {
1354 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1355 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
1358 for (i = 0; i < nargs; ++i)
1359 if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
1363 /* Registers used by SH5 Linux system calls for parameters */
1364 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
1366 for (i = 0; i < nargs; ++i)
1367 if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1369 #elif defined(X86_64)
1372 if (current_personality == 0) { /* x86-64 ABI */
1373 tcp->u_arg[0] = x86_64_regs.rdi;
1374 tcp->u_arg[1] = x86_64_regs.rsi;
1375 tcp->u_arg[2] = x86_64_regs.rdx;
1376 tcp->u_arg[3] = x86_64_regs.r10;
1377 tcp->u_arg[4] = x86_64_regs.r8;
1378 tcp->u_arg[5] = x86_64_regs.r9;
1379 } else { /* i386 ABI */
1380 /* Sign-extend lower 32 bits */
1381 tcp->u_arg[0] = (long)(int)x86_64_regs.rbx;
1382 tcp->u_arg[1] = (long)(int)x86_64_regs.rcx;
1383 tcp->u_arg[2] = (long)(int)x86_64_regs.rdx;
1384 tcp->u_arg[3] = (long)(int)x86_64_regs.rsi;
1385 tcp->u_arg[4] = (long)(int)x86_64_regs.rdi;
1386 tcp->u_arg[5] = (long)(int)x86_64_regs.rbp;
1388 #elif defined(MICROBLAZE)
1389 for (i = 0; i < nargs; ++i)
1390 if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1392 #elif defined(CRISV10) || defined(CRISV32)
1393 static const int crisregs[MAX_ARGS] = {
1394 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
1395 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
1398 for (i = 0; i < nargs; ++i)
1399 if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0)
1402 for (i = 0; i < nargs; ++i)
1403 if (upeek(tcp, PTREGS_OFFSET_REG(i), &tcp->u_arg[i]) < 0)
1406 for (i = 0; i < nargs; ++i)
1407 if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1412 tcp->u_arg[0] = i386_regs.ebx;
1413 tcp->u_arg[1] = i386_regs.ecx;
1414 tcp->u_arg[2] = i386_regs.edx;
1415 tcp->u_arg[3] = i386_regs.esi;
1416 tcp->u_arg[4] = i386_regs.edi;
1417 tcp->u_arg[5] = i386_regs.ebp;
1418 #else /* Other architecture (32bits specific) */
1419 for (i = 0; i < nargs; ++i)
1420 if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
1427 trace_syscall_entering(struct tcb *tcp)
1431 #if defined TCB_WAITEXECVE
1432 if (tcp->flags & TCB_WAITEXECVE) {
1433 /* This is the post-execve SIGTRAP. */
1434 tcp->flags &= ~TCB_WAITEXECVE;
1439 scno_good = res = get_scno(tcp);
1443 res = syscall_fixup_on_sysenter(tcp);
1447 res = syscall_enter(tcp);
1453 tcp->flags &= ~TCB_REPRINT;
1455 tprintf("????" /* anti-trigraph gap */ "(");
1456 else if (!SCNO_IN_RANGE(tcp->scno))
1457 tprintf("syscall_%lu(", tcp->scno);
1459 tprintf("%s(", sysent[tcp->scno].sys_name);
1461 * " <unavailable>" will be added later by the code which
1462 * detects ptrace errors.
1467 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
1468 while (SCNO_IN_RANGE(tcp->scno)) {
1469 # ifdef SYS_socket_subcall
1470 if (sysent[tcp->scno].sys_func == sys_socketcall) {
1471 decode_subcall(tcp, SYS_socket_subcall,
1472 SYS_socket_nsubcalls, deref_style);
1476 # ifdef SYS_ipc_subcall
1477 if (sysent[tcp->scno].sys_func == sys_ipc) {
1478 decode_subcall(tcp, SYS_ipc_subcall,
1479 SYS_ipc_nsubcalls, shift_style);
1485 #endif /* SYS_socket_subcall || SYS_ipc_subcall */
1487 internal_syscall(tcp);
1489 if ((SCNO_IN_RANGE(tcp->scno) &&
1490 !(qual_flags[tcp->scno] & QUAL_TRACE)) ||
1491 (tracing_paths && !pathtrace_match(tcp))) {
1492 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1496 tcp->flags &= ~TCB_FILTERED;
1498 if (cflag == CFLAG_ONLY_STATS) {
1504 tcp->flags &= ~TCB_REPRINT;
1505 if (!SCNO_IN_RANGE(tcp->scno))
1506 tprintf("syscall_%lu(", tcp->scno);
1508 tprintf("%s(", sysent[tcp->scno].sys_name);
1509 if (!SCNO_IN_RANGE(tcp->scno) ||
1510 ((qual_flags[tcp->scno] & QUAL_RAW) &&
1511 sysent[tcp->scno].sys_func != sys_exit))
1512 res = printargs(tcp);
1514 res = (*sysent[tcp->scno].sys_func)(tcp);
1516 if (fflush(tcp->outf) == EOF)
1519 tcp->flags |= TCB_INSYSCALL;
1520 /* Measure the entrance time as late as possible to avoid errors. */
1522 gettimeofday(&tcp->etime, NULL);
1527 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1528 * 1: ok, continue in trace_syscall().
1529 * other: error, trace_syscall() should print error indicator
1530 * ("????" etc) and bail out.
1533 get_syscall_result(struct tcb *tcp)
1535 #if defined(S390) || defined(S390X)
1536 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1538 #elif defined(POWERPC)
1539 # define SO_MASK 0x10000000
1542 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1544 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &result) < 0)
1546 if (flags & SO_MASK)
1549 #elif defined(AVR32)
1550 /* Read complete register set in one go. */
1551 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, ®s) < 0)
1554 if (upeek(tcp, PT_R0, &r0) < 0)
1557 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &i386_regs) < 0)
1559 #elif defined(X86_64)
1560 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &x86_64_regs) < 0)
1563 # define IA64_PSR_IS ((long)1 << 34)
1564 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
1565 ia32 = (psr & IA64_PSR_IS) != 0;
1566 if (upeek(tcp, PT_R8, &r8) < 0)
1568 if (upeek(tcp, PT_R10, &r10) < 0)
1571 /* Read complete register set in one go. */
1572 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (void *)®s) == -1)
1575 if (upeek(tcp, 4*PT_D0, &d0) < 0)
1577 #elif defined(LINUX_MIPSN32)
1578 unsigned long long regs[38];
1580 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1585 if (upeek(tcp, REG_A3, &a3) < 0)
1587 if (upeek(tcp, REG_V0, &r2) < 0)
1589 #elif defined(ALPHA)
1590 if (upeek(tcp, REG_A3, &a3) < 0)
1592 if (upeek(tcp, REG_R0, &r0) < 0)
1594 #elif defined(SPARC) || defined(SPARC64)
1595 /* Everything we need is in the current register set. */
1596 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1599 if (upeek(tcp, PT_GR28, &r28) < 0)
1603 #elif defined(CRISV10) || defined(CRISV32)
1604 if (upeek(tcp, 4*PT_R10, &r10) < 0)
1607 #elif defined(MICROBLAZE)
1608 if (upeek(tcp, 3 * 4, &r3) < 0)
1613 /* new syscall ABI returns result in R0 */
1614 if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0)
1617 /* ABI defines result returned in r9 */
1618 if (upeek(tcp, REG_GENERAL(9), (long *)&r9) < 0)
1625 /* Called at each syscall exit.
1627 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1628 * 1: ok, continue in trace_syscall().
1629 * other: error, trace_syscall() should print error indicator
1630 * ("????" etc) and bail out.
1633 syscall_fixup_on_sysexit(struct tcb *tcp)
1635 #if defined(S390) || defined(S390X)
1636 if (syscall_mode != -ENOSYS)
1637 syscall_mode = tcp->scno;
1638 if ((tcp->flags & TCB_WAITEXECVE)
1639 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1641 * Return from execve.
1642 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1643 * flag set for the post-execve SIGTRAP to see and reset.
1652 * Check the syscall return value register value for whether it is
1653 * a negated errno code indicating an error, or a success return value.
1656 is_negated_errno(unsigned long int val)
1658 unsigned long int max = -(long int) nerrnos;
1659 #if SUPPORTED_PERSONALITIES > 1
1660 if (personality_wordsize[current_personality] < sizeof(val)) {
1661 val = (unsigned int) val;
1662 max = (unsigned int) max;
1669 get_error(struct tcb *tcp)
1672 int check_errno = 1;
1673 if (SCNO_IN_RANGE(tcp->scno) &&
1674 sysent[tcp->scno].sys_flags & SYSCALL_NEVER_FAILS) {
1677 #if defined(S390) || defined(S390X)
1678 if (check_errno && is_negated_errno(gpr2)) {
1686 if (check_errno && is_negated_errno(i386_regs.eax)) {
1688 u_error = -i386_regs.eax;
1691 tcp->u_rval = i386_regs.eax;
1693 #elif defined(X86_64)
1694 if (check_errno && is_negated_errno(x86_64_regs.rax)) {
1696 u_error = -x86_64_regs.rax;
1699 tcp->u_rval = x86_64_regs.rax;
1706 if (check_errno && is_negated_errno(err)) {
1714 if (check_errno && r10) {
1722 if (check_errno && a3) {
1728 #elif defined(POWERPC)
1729 if (check_errno && is_negated_errno(result)) {
1734 tcp->u_rval = result;
1737 if (check_errno && is_negated_errno(d0)) {
1745 if (check_errno && is_negated_errno(regs.ARM_r0)) {
1747 u_error = -regs.ARM_r0;
1750 tcp->u_rval = regs.ARM_r0;
1752 #elif defined(AVR32)
1753 if (check_errno && regs.r12 && (unsigned) -regs.r12 < nerrnos) {
1755 u_error = -regs.r12;
1758 tcp->u_rval = regs.r12;
1761 if (check_errno && is_negated_errno(r0)) {
1767 #elif defined(ALPHA)
1768 if (check_errno && a3) {
1775 #elif defined(SPARC)
1776 if (check_errno && regs.psr & PSR_C) {
1778 u_error = regs.u_regs[U_REG_O0];
1781 tcp->u_rval = regs.u_regs[U_REG_O0];
1783 #elif defined(SPARC64)
1784 if (check_errno && regs.tstate & 0x1100000000UL) {
1786 u_error = regs.u_regs[U_REG_O0];
1789 tcp->u_rval = regs.u_regs[U_REG_O0];
1792 if (check_errno && is_negated_errno(r28)) {
1800 if (check_errno && is_negated_errno(r0)) {
1808 if (check_errno && is_negated_errno(r9)) {
1815 #elif defined(CRISV10) || defined(CRISV32)
1816 if (check_errno && r10 && (unsigned) -r10 < nerrnos) {
1825 if (upeek(tcp, PTREGS_OFFSET_REG(0), &rval) < 0)
1827 if (check_errno && rval < 0 && rval > -nerrnos) {
1834 #elif defined(MICROBLAZE)
1835 if (check_errno && is_negated_errno(r3)) {
1843 tcp->u_error = u_error;
1848 dumpio(struct tcb *tcp)
1852 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
1854 if (!SCNO_IN_RANGE(tcp->scno))
1856 if (sysent[tcp->scno].sys_func == printargs)
1858 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
1859 if (sysent[tcp->scno].sys_func == sys_read ||
1860 sysent[tcp->scno].sys_func == sys_pread ||
1861 sysent[tcp->scno].sys_func == sys_recv ||
1862 sysent[tcp->scno].sys_func == sys_recvfrom)
1863 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
1864 else if (sysent[tcp->scno].sys_func == sys_readv)
1865 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
1868 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
1869 if (sysent[tcp->scno].sys_func == sys_write ||
1870 sysent[tcp->scno].sys_func == sys_pwrite ||
1871 sysent[tcp->scno].sys_func == sys_send ||
1872 sysent[tcp->scno].sys_func == sys_sendto)
1873 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
1874 else if (sysent[tcp->scno].sys_func == sys_writev)
1875 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
1881 trace_syscall_exiting(struct tcb *tcp)
1888 /* Measure the exit time as early as possible to avoid errors. */
1890 gettimeofday(&tv, NULL);
1892 #if SUPPORTED_PERSONALITIES > 1
1893 update_personality(tcp, tcp->currpers);
1895 res = get_syscall_result(tcp);
1899 res = syscall_fixup_on_sysexit(tcp);
1903 res = get_error(tcp);
1907 internal_syscall(tcp);
1909 if (res == 1 && filtered(tcp)) {
1913 /* TODO: TCB_REPRINT is probably not necessary:
1914 * we can determine whether reprinting is needed
1915 * by examining printing_tcp. Something like:
1916 * if not in -ff mode, and printing_tcp != tcp,
1917 * then the log is not currenlty ends with *our*
1918 * syscall entry output, but with something else,
1919 * and we need to reprint.
1920 * If we'd implement this, printing_tcp = tcp
1921 * assignments in code below can be made more logical.
1924 if (tcp->flags & TCB_REPRINT) {
1926 if (!SCNO_IN_RANGE(tcp->scno))
1927 tprintf("<... syscall_%lu resumed> ", tcp->scno);
1929 tprintf("<... %s resumed> ", sysent[tcp->scno].sys_name);
1933 struct timeval t = tv;
1934 count_syscall(tcp, &t);
1935 if (cflag == CFLAG_ONLY_STATS) {
1944 tprints("= ? <unavailable>\n");
1946 tcp->flags &= ~TCB_INSYSCALL;
1950 if (!SCNO_IN_RANGE(tcp->scno)
1951 || (qual_flags[tcp->scno] & QUAL_RAW)) {
1953 sys_res = printargs(tcp);
1955 /* FIXME: not_failing_only (IOW, option -z) is broken:
1956 * failure of syscall is known only after syscall return.
1957 * Thus we end up with something like this on, say, ENOENT:
1958 * open("doesnt_exist", O_RDONLY <unfinished ...>
1959 * {next syscall decode}
1960 * whereas the intended result is that open(...) line
1961 * is not shown at all.
1963 if (not_failing_only && tcp->u_error)
1964 goto ret; /* ignore failed syscalls */
1966 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1971 u_error = tcp->u_error;
1972 if (!SCNO_IN_RANGE(tcp->scno) ||
1973 qual_flags[tcp->scno] & QUAL_RAW) {
1975 tprintf("= -1 (errno %ld)", u_error);
1977 tprintf("= %#lx", tcp->u_rval);
1979 else if (!(sys_res & RVAL_NONE) && u_error) {
1981 /* Blocked signals do not interrupt any syscalls.
1982 * In this case syscalls don't return ERESTARTfoo codes.
1984 * Deadly signals set to SIG_DFL interrupt syscalls
1985 * and kill the process regardless of which of the codes below
1986 * is returned by the interrupted syscall.
1987 * In some cases, kernel forces a kernel-generated deadly
1988 * signal to be unblocked and set to SIG_DFL (and thus cause
1989 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
1990 * or SIGILL. (The alternative is to leave process spinning
1991 * forever on the faulty instruction - not useful).
1993 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
1994 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
1995 * but kernel will always restart them.
1998 /* Most common type of signal-interrupted syscall exit code.
1999 * The system call will be restarted with the same arguments
2000 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2002 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
2004 case ERESTARTNOINTR:
2005 /* Rare. For example, fork() returns this if interrupted.
2006 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2008 tprints("= ? ERESTARTNOINTR (To be restarted)");
2010 case ERESTARTNOHAND:
2011 /* pause(), rt_sigsuspend() etc use this code.
2012 * SA_RESTART is ignored (assumed not set):
2013 * syscall won't restart (will return EINTR instead)
2014 * even after signal with SA_RESTART set.
2015 * However, after SIG_IGN or SIG_DFL signal it will.
2017 tprints("= ? ERESTARTNOHAND (Interrupted by signal)");
2019 case ERESTART_RESTARTBLOCK:
2020 /* Syscalls like nanosleep(), poll() which can't be
2021 * restarted with their original arguments use this
2022 * code. Kernel will execute restart_syscall() instead,
2023 * which changes arguments before restarting syscall.
2024 * SA_RESTART is ignored (assumed not set) similarly
2025 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2026 * since restart data is saved in "restart block"
2027 * in task struct, and if signal handler uses a syscall
2028 * which in turn saves another such restart block,
2029 * old data is lost and restart becomes impossible)
2031 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
2035 tprintf("= -1 E??? (errno %ld)", u_error);
2036 else if (u_error < nerrnos)
2037 tprintf("= -1 %s (%s)", errnoent[u_error],
2040 tprintf("= -1 ERRNO_%ld (%s)", u_error,
2044 if ((sys_res & RVAL_STR) && tcp->auxstr)
2045 tprintf(" (%s)", tcp->auxstr);
2048 if (sys_res & RVAL_NONE)
2051 switch (sys_res & RVAL_MASK) {
2053 tprintf("= %#lx", tcp->u_rval);
2056 tprintf("= %#lo", tcp->u_rval);
2059 tprintf("= %lu", tcp->u_rval);
2062 tprintf("= %ld", tcp->u_rval);
2064 #ifdef HAVE_LONG_LONG
2066 tprintf("= %#llx", tcp->u_lrval);
2069 tprintf("= %#llo", tcp->u_lrval);
2071 case RVAL_LUDECIMAL:
2072 tprintf("= %llu", tcp->u_lrval);
2075 tprintf("= %lld", tcp->u_lrval);
2080 "invalid rval format\n");
2084 if ((sys_res & RVAL_STR) && tcp->auxstr)
2085 tprintf(" (%s)", tcp->auxstr);
2088 tv_sub(&tv, &tv, &tcp->etime);
2089 tprintf(" <%ld.%06ld>",
2090 (long) tv.tv_sec, (long) tv.tv_usec);
2097 tcp->flags &= ~TCB_INSYSCALL;
2102 trace_syscall(struct tcb *tcp)
2104 return exiting(tcp) ?
2105 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);