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.
42 #include <sys/syscall.h>
43 #include <sys/param.h>
47 #ifndef PTRACE_PEEKUSR
48 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
50 #elif defined(HAVE_LINUX_PTRACE_H)
52 # ifdef HAVE_STRUCT_IA64_FPREG
53 # define ia64_fpreg XXX_ia64_fpreg
55 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
56 # define pt_all_user_regs XXX_pt_all_user_regs
58 #include <linux/ptrace.h>
60 # undef pt_all_user_regs
63 #if defined (LINUX) && defined (SPARC64)
64 # undef PTRACE_GETREGS
65 # define PTRACE_GETREGS PTRACE_GETREGS64
66 # undef PTRACE_SETREGS
67 # define PTRACE_SETREGS PTRACE_SETREGS64
68 #endif /* LINUX && SPARC64 */
70 #if defined(LINUX) && defined(IA64)
71 # include <asm/ptrace_offsets.h>
75 #define NR_SYSCALL_BASE 0
78 #define ERESTARTSYS 512
80 #ifndef ERESTARTNOINTR
81 #define ERESTARTNOINTR 513
83 #ifndef ERESTARTNOHAND
84 #define ERESTARTNOHAND 514 /* restart if no handler.. */
87 #define ENOIOCTLCMD 515 /* No ioctl command */
89 #ifndef ERESTART_RESTARTBLOCK
90 #define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
93 #warning: NSIG is not defined, using 32
97 /* Ugh. Is this really correct? ARM has no RT signals?! */
100 #undef NR_SYSCALL_BASE
101 #define NR_SYSCALL_BASE __NR_SYSCALL_BASE
107 /* Define these shorthand notations to simplify the syscallent files. */
108 #define TD TRACE_DESC
109 #define TF TRACE_FILE
111 #define TN TRACE_NETWORK
112 #define TP TRACE_PROCESS
113 #define TS TRACE_SIGNAL
114 #define NF SYSCALL_NEVER_FAILS
117 static const struct sysent sysent0[] = {
118 #include "syscallent.h"
121 #if SUPPORTED_PERSONALITIES >= 2
122 static const struct sysent sysent1[] = {
123 #include "syscallent1.h"
127 #if SUPPORTED_PERSONALITIES >= 3
128 static const struct sysent sysent2[] = {
129 #include "syscallent2.h"
133 /* Now undef them since short defines cause wicked namespace pollution. */
145 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
146 * program `ioctlsort', such that the list is sorted by the `code' field.
147 * This has the side-effect of resolving the _IO.. macros into
148 * plain integers, eliminating the need to include here everything
153 static const char *const errnoent0[] = {
154 #include "errnoent.h"
156 static const char *const signalent0[] = {
157 #include "signalent.h"
159 static const struct ioctlent ioctlent0[] = {
160 #include "ioctlent.h"
162 enum { nsyscalls0 = ARRAY_SIZE(sysent0) };
163 enum { nerrnos0 = ARRAY_SIZE(errnoent0) };
164 enum { nsignals0 = ARRAY_SIZE(signalent0) };
165 enum { nioctlents0 = ARRAY_SIZE(ioctlent0) };
166 int qual_flags0[MAX_QUALS];
168 #if SUPPORTED_PERSONALITIES >= 2
169 static const char *const errnoent1[] = {
170 #include "errnoent1.h"
172 static const char *const signalent1[] = {
173 #include "signalent1.h"
175 static const struct ioctlent ioctlent1[] = {
176 #include "ioctlent1.h"
178 enum { nsyscalls1 = ARRAY_SIZE(sysent1) };
179 enum { nerrnos1 = ARRAY_SIZE(errnoent1) };
180 enum { nsignals1 = ARRAY_SIZE(signalent1) };
181 enum { nioctlents1 = ARRAY_SIZE(ioctlent1) };
182 int qual_flags1[MAX_QUALS];
185 #if SUPPORTED_PERSONALITIES >= 3
186 static const char *const errnoent2[] = {
187 #include "errnoent2.h"
189 static const char *const signalent2[] = {
190 #include "signalent2.h"
192 static const struct ioctlent ioctlent2[] = {
193 #include "ioctlent2.h"
195 enum { nsyscalls2 = ARRAY_SIZE(sysent2) };
196 enum { nerrnos2 = ARRAY_SIZE(errnoent2) };
197 enum { nsignals2 = ARRAY_SIZE(signalent2) };
198 enum { nioctlents2 = ARRAY_SIZE(ioctlent2) };
199 int qual_flags2[MAX_QUALS];
203 const struct sysent *sysent;
204 const char *const *errnoent;
205 const char *const *signalent;
206 const struct ioctlent *ioctlent;
213 int current_personality;
215 #ifndef PERSONALITY0_WORDSIZE
216 # define PERSONALITY0_WORDSIZE sizeof(long)
218 const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
219 PERSONALITY0_WORDSIZE,
220 #if SUPPORTED_PERSONALITIES > 1
221 PERSONALITY1_WORDSIZE,
223 #if SUPPORTED_PERSONALITIES > 2
224 PERSONALITY2_WORDSIZE,
229 set_personality(int personality)
231 switch (personality) {
233 errnoent = errnoent0;
236 nsyscalls = nsyscalls0;
237 ioctlent = ioctlent0;
238 nioctlents = nioctlents0;
239 signalent = signalent0;
240 nsignals = nsignals0;
241 qual_flags = qual_flags0;
244 #if SUPPORTED_PERSONALITIES >= 2
246 errnoent = errnoent1;
249 nsyscalls = nsyscalls1;
250 ioctlent = ioctlent1;
251 nioctlents = nioctlents1;
252 signalent = signalent1;
253 nsignals = nsignals1;
254 qual_flags = qual_flags1;
258 #if SUPPORTED_PERSONALITIES >= 3
260 errnoent = errnoent2;
263 nsyscalls = nsyscalls2;
264 ioctlent = ioctlent2;
265 nioctlents = nioctlents2;
266 signalent = signalent2;
267 nsignals = nsignals2;
268 qual_flags = qual_flags2;
273 current_personality = personality;
277 static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
279 static const struct qual_options {
281 const char *option_name;
282 int (*qualify)(const char *, int, int);
283 const char *argument_name;
285 { QUAL_TRACE, "trace", qual_syscall, "system call" },
286 { QUAL_TRACE, "t", qual_syscall, "system call" },
287 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
288 { QUAL_ABBREV, "a", qual_syscall, "system call" },
289 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
290 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
291 { QUAL_RAW, "raw", qual_syscall, "system call" },
292 { QUAL_RAW, "x", qual_syscall, "system call" },
293 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
294 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
295 { QUAL_SIGNAL, "s", qual_signal, "signal" },
296 { QUAL_FAULT, "fault", qual_fault, "fault" },
297 { QUAL_FAULT, "faults", qual_fault, "fault" },
298 { QUAL_FAULT, "m", qual_fault, "fault" },
299 { QUAL_READ, "read", qual_desc, "descriptor" },
300 { QUAL_READ, "reads", qual_desc, "descriptor" },
301 { QUAL_READ, "r", qual_desc, "descriptor" },
302 { QUAL_WRITE, "write", qual_desc, "descriptor" },
303 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
304 { QUAL_WRITE, "w", qual_desc, "descriptor" },
305 { 0, NULL, NULL, NULL },
309 qualify_one(int n, int bitflag, int not, int pers)
311 if (pers == 0 || pers < 0) {
313 qual_flags0[n] &= ~bitflag;
315 qual_flags0[n] |= bitflag;
318 #if SUPPORTED_PERSONALITIES >= 2
319 if (pers == 1 || pers < 0) {
321 qual_flags1[n] &= ~bitflag;
323 qual_flags1[n] |= bitflag;
325 #endif /* SUPPORTED_PERSONALITIES >= 2 */
327 #if SUPPORTED_PERSONALITIES >= 3
328 if (pers == 2 || pers < 0) {
330 qual_flags2[n] &= ~bitflag;
332 qual_flags2[n] |= bitflag;
334 #endif /* SUPPORTED_PERSONALITIES >= 3 */
338 qual_syscall(const char *s, int bitflag, int not)
343 if (isdigit((unsigned char)*s)) {
345 if (i < 0 || i >= MAX_QUALS)
347 qualify_one(i, bitflag, not, -1);
350 for (i = 0; i < nsyscalls0; i++)
351 if (strcmp(s, sysent0[i].sys_name) == 0) {
352 qualify_one(i, bitflag, not, 0);
356 #if SUPPORTED_PERSONALITIES >= 2
357 for (i = 0; i < nsyscalls1; i++)
358 if (strcmp(s, sysent1[i].sys_name) == 0) {
359 qualify_one(i, bitflag, not, 1);
362 #endif /* SUPPORTED_PERSONALITIES >= 2 */
364 #if SUPPORTED_PERSONALITIES >= 3
365 for (i = 0; i < nsyscalls2; i++)
366 if (strcmp(s, sysent2[i].sys_name) == 0) {
367 qualify_one(i, bitflag, not, 2);
370 #endif /* SUPPORTED_PERSONALITIES >= 3 */
376 qual_signal(const char *s, int bitflag, int not)
381 if (isdigit((unsigned char)*s)) {
383 if (signo < 0 || signo >= MAX_QUALS)
385 qualify_one(signo, bitflag, not, -1);
388 if (strlen(s) >= sizeof buf)
392 if (strncasecmp(s, "SIG", 3) == 0)
394 for (i = 0; i <= NSIG; i++)
395 if (strcasecmp(s, signame(i) + 3) == 0) {
396 qualify_one(i, bitflag, not, -1);
403 qual_fault(const char *s, int bitflag, int not)
409 qual_desc(const char *s, int bitflag, int not)
411 if (isdigit((unsigned char)*s)) {
413 if (desc < 0 || desc >= MAX_QUALS)
415 qualify_one(desc, bitflag, not, -1);
422 lookup_class(const char *s)
424 if (strcmp(s, "file") == 0)
426 if (strcmp(s, "ipc") == 0)
428 if (strcmp(s, "network") == 0)
429 return TRACE_NETWORK;
430 if (strcmp(s, "process") == 0)
431 return TRACE_PROCESS;
432 if (strcmp(s, "signal") == 0)
434 if (strcmp(s, "desc") == 0)
440 qualify(const char *s)
442 const struct qual_options *opt;
448 opt = &qual_options[0];
449 for (i = 0; (p = qual_options[i].option_name); i++) {
451 if (strncmp(s, p, n) == 0 && s[n] == '=') {
452 opt = &qual_options[i];
462 if (strcmp(s, "none") == 0) {
466 if (strcmp(s, "all") == 0) {
467 for (i = 0; i < MAX_QUALS; i++) {
468 qualify_one(i, opt->bitflag, not, -1);
472 for (i = 0; i < MAX_QUALS; i++) {
473 qualify_one(i, opt->bitflag, !not, -1);
478 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
479 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
480 for (i = 0; i < nsyscalls0; i++)
481 if (sysent0[i].sys_flags & n)
482 qualify_one(i, opt->bitflag, not, 0);
484 #if SUPPORTED_PERSONALITIES >= 2
485 for (i = 0; i < nsyscalls1; i++)
486 if (sysent1[i].sys_flags & n)
487 qualify_one(i, opt->bitflag, not, 1);
488 #endif /* SUPPORTED_PERSONALITIES >= 2 */
490 #if SUPPORTED_PERSONALITIES >= 3
491 for (i = 0; i < nsyscalls2; i++)
492 if (sysent2[i].sys_flags & n)
493 qualify_one(i, opt->bitflag, not, 2);
494 #endif /* SUPPORTED_PERSONALITIES >= 3 */
498 if (opt->qualify(p, opt->bitflag, not)) {
499 fprintf(stderr, "strace: invalid %s `%s'\n",
500 opt->argument_name, p);
509 enum subcall_style { shift_style, deref_style, mask_style, door_style };
511 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
519 static const struct subcall subcalls_table[] = {
520 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
522 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
524 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
526 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
530 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) || defined(__ARM_EABI__) ))
533 decode_subcall(struct tcb *tcp, int subcall, int nsubcalls, enum subcall_style style)
535 unsigned long addr, mask;
537 int size = personality_wordsize[current_personality];
541 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
543 tcp->scno = subcall + tcp->u_arg[0];
544 tcp->u_nargs = n = sysent[tcp->scno].nargs;
545 for (i = 0; i < n; i++)
546 tcp->u_arg[i] = tcp->u_arg[i + 1];
549 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
551 tcp->scno = subcall + tcp->u_arg[0];
552 addr = tcp->u_arg[1];
553 tcp->u_nargs = n = sysent[tcp->scno].nargs;
554 for (i = 0; i < n; i++) {
555 if (size == sizeof(int)) {
557 if (umove(tcp, addr, &arg) < 0)
561 else if (size == sizeof(long)) {
563 if (umove(tcp, addr, &arg) < 0)
573 mask = (tcp->u_arg[0] >> 8) & 0xff;
574 for (i = 0; mask; i++)
578 tcp->u_arg[0] &= 0xff;
579 tcp->scno = subcall + i;
580 tcp->u_nargs = sysent[tcp->scno].nargs;
584 * Oh, yuck. The call code is the *sixth* argument.
585 * (don't you mean the *last* argument? - JH)
587 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
589 tcp->scno = subcall + tcp->u_arg[5];
590 tcp->u_nargs = sysent[tcp->scno].nargs;
594 for (i = 0; i < ARRAY_SIZE(subcalls_table); i++)
595 if (subcalls_table[i].call == tcp->scno) break;
596 if (i < ARRAY_SIZE(subcalls_table) &&
597 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
598 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
599 for (i = 0; i < tcp->u_nargs; i++)
600 tcp->u_arg[i] = tcp->u_arg[i + 1];
609 printargs(struct tcb *tcp)
614 for (i = 0; i < tcp->u_nargs; i++)
615 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
621 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)
641 if (upeek(tcp, uoff(u_rval2), &val) < 0)
647 val = tcp->status.PR_REG[R_O1];
650 val = tcp->status.PR_REG[EDX];
653 val = tcp->status.PR_REG[RDX];
656 val = tcp->status.PR_REG[CTX_V1];
662 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
670 * Apparently, indirect system calls have already be converted by ptrace(2),
671 * so if you see "indir" this program has gone astray.
674 sys_indir(struct tcb *tcp)
680 scno = tcp->u_arg[0];
681 if (!SCNO_IN_RANGE(scno)) {
682 fprintf(stderr, "Bogus syscall: %ld\n", scno);
685 nargs = sysent[scno].nargs;
686 tprints(sysent[scno].sys_name);
687 for (i = 0; i < nargs; i++)
688 tprintf(", %#lx", tcp->u_arg[i+1]);
695 is_restart_error(struct tcb *tcp)
700 switch (tcp->u_error) {
704 case ERESTART_RESTARTBLOCK:
713 struct tcb *tcp_last = NULL;
717 static struct pt_regs i386_regs;
718 # elif defined(X86_64)
719 static struct pt_regs x86_64_regs;
720 # elif defined (IA64)
721 long r8, r10, psr; /* TODO: make static? */
722 long ia32 = 0; /* not static */
723 # elif defined (POWERPC)
725 # elif defined (M68K)
730 static struct pt_regs regs;
731 # elif defined (ALPHA)
734 # elif defined(AVR32)
735 static struct pt_regs regs;
736 # elif defined (SPARC) || defined (SPARC64)
737 static struct pt_regs regs;
738 static unsigned long trap;
739 # elif defined(LINUX_MIPSN32)
745 # elif defined(S390) || defined(S390X)
748 static long syscall_mode;
755 # elif defined(CRISV10) || defined(CRISV32)
757 # elif defined(MICROBLAZE)
762 struct reg regs; /* TODO: make static? */
766 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
767 * 1: ok, continue in trace_syscall().
768 * other: error, trace_syscall() should print error indicator
769 * ("????" etc) and bail out.
775 get_scno(struct tcb *tcp)
780 # if defined(S390) || defined(S390X)
781 if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
784 if (syscall_mode != -ENOSYS) {
786 * Since kernel version 2.5.44 the scno gets passed in gpr2.
791 * Old style of "passing" the scno via the SVC instruction.
793 long opcode, offset_reg, tmp;
795 static const int gpr_offset[16] = {
796 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
797 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
798 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
799 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
802 if (upeek(tcp, PT_PSWADDR, &pc) < 0)
805 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-sizeof(long)), 0);
807 perror("peektext(pc-oneword)");
812 * We have to check if the SVC got executed directly or via an
813 * EXECUTE instruction. In case of EXECUTE it is necessary to do
814 * instruction decoding to derive the system call number.
815 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
816 * so that this doesn't work if a SVC opcode is part of an EXECUTE
817 * opcode. Since there is no way to find out the opcode size this
818 * is the best we can do...
820 if ((opcode & 0xff00) == 0x0a00) {
822 scno = opcode & 0xff;
825 /* SVC got executed by EXECUTE instruction */
828 * Do instruction decoding of EXECUTE. If you really want to
829 * understand this, read the Principles of Operations.
831 svc_addr = (void *) (opcode & 0xfff);
834 offset_reg = (opcode & 0x000f0000) >> 16;
835 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
840 offset_reg = (opcode & 0x0000f000) >> 12;
841 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
845 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
854 offset_reg = (opcode & 0x00f00000) >> 20;
855 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
858 scno = (scno | tmp) & 0xff;
861 # elif defined (POWERPC)
862 if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
865 /* TODO: speed up strace by not doing this at every syscall.
866 * We only need to do it after execve.
872 /* Check for 64/32 bit mode. */
873 if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
875 /* SF is bit 0 of MSR */
880 if (currpers != current_personality) {
881 static const char *const names[] = {"64 bit", "32 bit"};
882 set_personality(currpers);
883 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
884 pid, names[current_personality]);
887 # elif defined(AVR32)
888 /* Read complete register set in one go. */
889 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, ®s) < 0)
893 if (upeek(tcp, PT_ORIG_P0, &scno))
895 # elif defined (I386)
896 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &i386_regs) < 0)
898 scno = i386_regs.orig_eax;
899 # elif defined (X86_64)
901 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &x86_64_regs) < 0)
903 scno = x86_64_regs.orig_rax;
905 /* Check CS register value. On x86-64 linux it is:
906 * 0x33 for long mode (64 bit)
907 * 0x23 for compatibility mode (32 bit)
909 switch (x86_64_regs.cs) {
910 case 0x23: currpers = 1; break;
911 case 0x33: currpers = 0; break;
913 fprintf(stderr, "Unknown value CS=0x%08X while "
914 "detecting personality of process "
915 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
916 currpers = current_personality;
920 /* This version analyzes the opcode of a syscall instruction.
921 * (int 0x80 on i386 vs. syscall on x86-64)
922 * It works, but is too complicated.
924 unsigned long val, rip, i;
926 rip = x86_64_regs.rip;
928 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
932 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
934 fprintf(stderr, "ptrace_peektext failed: %s\n",
936 switch (call & 0xffff) {
937 /* x86-64: syscall = 0x0f 0x05 */
938 case 0x050f: currpers = 0; break;
939 /* i386: int 0x80 = 0xcd 0x80 */
940 case 0x80cd: currpers = 1; break;
942 currpers = current_personality;
944 "Unknown syscall opcode (0x%04X) while "
945 "detecting personality of process "
946 "PID=%d\n", (int)call, tcp->pid);
950 if (currpers != current_personality) {
951 static const char *const names[] = {"64 bit", "32 bit"};
952 set_personality(currpers);
953 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
954 tcp->pid, names[current_personality]);
957 # define IA64_PSR_IS ((long)1 << 34)
958 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
959 ia32 = (psr & IA64_PSR_IS) != 0;
961 if (upeek(tcp, PT_R1, &scno) < 0)
964 if (upeek(tcp, PT_R15, &scno) < 0)
968 /* Read complete register set in one go. */
969 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (void *)®s) == -1)
973 * We only need to grab the syscall number on syscall entry.
975 if (regs.ARM_ip == 0) {
977 * Note: we only deal with only 32-bit CPUs here.
979 if (regs.ARM_cpsr & 0x20) {
981 * Get the Thumb-mode system call number
986 * Get the ARM-mode system call number
989 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(regs.ARM_pc - 4), NULL);
993 /* Handle the EABI syscall convention. We do not
994 bother converting structures between the two
995 ABIs, but basic functionality should work even
996 if strace and the traced program have different
998 if (scno == 0xef000000) {
1001 if ((scno & 0x0ff00000) != 0x0f900000) {
1002 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
1008 * Fixup the syscall number
1013 if (scno & 0x0f0000) {
1015 * Handle ARM specific syscall
1023 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
1024 tcp->flags |= TCB_INSYSCALL;
1026 # elif defined (M68K)
1027 if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
1029 # elif defined (LINUX_MIPSN32)
1030 unsigned long long regs[38];
1032 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1038 if (!SCNO_IN_RANGE(scno)) {
1039 if (a3 == 0 || a3 == -1) {
1041 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1045 # elif defined (MIPS)
1046 if (upeek(tcp, REG_A3, &a3) < 0)
1048 if (upeek(tcp, REG_V0, &scno) < 0)
1051 if (!SCNO_IN_RANGE(scno)) {
1052 if (a3 == 0 || a3 == -1) {
1054 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1058 # elif defined (ALPHA)
1059 if (upeek(tcp, REG_A3, &a3) < 0)
1061 if (upeek(tcp, REG_R0, &scno) < 0)
1065 * Do some sanity checks to figure out if it's
1066 * really a syscall entry
1068 if (!SCNO_IN_RANGE(scno)) {
1069 if (a3 == 0 || a3 == -1) {
1071 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
1075 # elif defined (SPARC) || defined (SPARC64)
1076 /* Everything we need is in the current register set. */
1077 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1080 /* Disassemble the syscall trap. */
1081 /* Retrieve the syscall trap instruction. */
1083 # if defined(SPARC64)
1084 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.tpc, 0);
1087 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.pc, 0);
1092 /* Disassemble the trap to see what personality to use. */
1095 /* Linux/SPARC syscall trap. */
1099 /* Linux/SPARC64 syscall trap. */
1103 /* SunOS syscall trap. (pers 1) */
1104 fprintf(stderr, "syscall: SunOS no support\n");
1107 /* Solaris 2.x syscall trap. (per 2) */
1111 /* NetBSD/FreeBSD syscall trap. */
1112 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1115 /* Solaris 2.x gettimeofday */
1119 # if defined (SPARC64)
1120 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, regs.tpc);
1122 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, regs.pc);
1127 /* Extract the system call number from the registers. */
1128 if (trap == 0x91d02027)
1131 scno = regs.u_regs[U_REG_G1];
1133 scno = regs.u_regs[U_REG_O0];
1134 memmove(®s.u_regs[U_REG_O0], ®s.u_regs[U_REG_O1], 7*sizeof(regs.u_regs[0]));
1136 # elif defined(HPPA)
1137 if (upeek(tcp, PT_GR20, &scno) < 0)
1141 * In the new syscall ABI, the system call number is in R3.
1143 if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
1147 /* Odd as it may seem, a glibc bug has been known to cause
1148 glibc to issue bogus negative syscall numbers. So for
1149 our purposes, make strace print what it *should* have been */
1150 long correct_scno = (scno & 0xff);
1153 "Detected glibc bug: bogus system call"
1154 " number = %ld, correcting to %ld\n",
1157 scno = correct_scno;
1159 # elif defined(SH64)
1160 if (upeek(tcp, REG_SYSCALL, &scno) < 0)
1163 # elif defined(CRISV10) || defined(CRISV32)
1164 if (upeek(tcp, 4*PT_R9, &scno) < 0)
1166 # elif defined(TILE)
1167 if (upeek(tcp, PTREGS_OFFSET_REG(10), &scno) < 0)
1169 # elif defined(MICROBLAZE)
1170 if (upeek(tcp, 0, &scno) < 0)
1176 if (upeek(tcp, uoff(u_arg[7]), &scno) < 0)
1179 /* new syscall ABI returns result in R0 */
1180 if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0)
1183 /* ABI defines result returned in r9 */
1184 if (upeek(tcp, REG_GENERAL(9), (long *)&r9) < 0)
1189 # ifdef HAVE_PR_SYSCALL
1190 scno = tcp->status.PR_SYSCALL;
1193 scno = tcp->status.PR_WHAT;
1195 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1199 switch (regs.r_eax) {
1202 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1208 # endif /* FREEBSD */
1209 # endif /* !HAVE_PR_SYSCALL */
1210 #endif /* USE_PROCFS */
1217 known_scno(struct tcb *tcp)
1219 long scno = tcp->scno;
1220 #if SUPPORTED_PERSONALITIES > 1
1221 if (SCNO_IN_RANGE(scno) && sysent[scno].native_scno != 0)
1222 scno = sysent[scno].native_scno;
1225 scno += NR_SYSCALL_BASE;
1229 /* Called at each syscall entry.
1231 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1232 * 1: ok, continue in trace_syscall().
1233 * other: error, trace_syscall() should print error indicator
1234 * ("????" etc) and bail out.
1237 syscall_fixup_on_sysenter(struct tcb *tcp)
1240 int scno = known_scno(tcp);
1242 if (tcp->status.PR_WHY != PR_SYSENTRY) {
1246 || scno == SYS_vfork
1249 || scno == SYS_fork1
1252 || scno == SYS_forkall
1255 || scno == SYS_rfork1
1258 || scno == SYS_rforkall
1261 /* We are returning in the child, fake it. */
1262 tcp->status.PR_WHY = PR_SYSENTRY;
1264 tcp->status.PR_WHY = PR_SYSEXIT;
1267 fprintf(stderr, "syscall: missing entry\n");
1268 tcp->flags |= TCB_INSYSCALL;
1271 #endif /* USE_PROCFS */
1275 fprintf(stderr, "syscall: missing entry\n");
1276 tcp->flags |= TCB_INSYSCALL;
1281 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
1283 if (i386_regs.eax != -ENOSYS) {
1285 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1288 #elif defined (X86_64)
1290 long rax = x86_64_regs.rax;
1291 if (current_personality == 1)
1292 rax = (int)rax; /* sign extend from 32 bits */
1293 if (rax != -ENOSYS) {
1295 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1299 #elif defined (S390) || defined (S390X)
1300 /* TODO: we already fetched PT_GPR2 in get_scno
1301 * and stored it in syscall_mode, reuse it here
1302 * instead of re-fetching?
1304 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1306 if (syscall_mode != -ENOSYS)
1307 syscall_mode = tcp->scno;
1308 if (gpr2 != syscall_mode) {
1310 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
1313 #elif defined (M68K)
1314 /* TODO? Eliminate upeek's in arches below like we did in x86 */
1315 if (upeek(tcp, 4*PT_D0, &d0) < 0)
1317 if (d0 != -ENOSYS) {
1319 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", d0);
1323 if (upeek(tcp, PT_R10, &r10) < 0)
1325 if (upeek(tcp, PT_R8, &r8) < 0)
1327 if (ia32 && r8 != -ENOSYS) {
1329 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", r8);
1332 #elif defined(CRISV10) || defined(CRISV32)
1333 if (upeek(tcp, 4*PT_R10, &r10) < 0)
1335 if (r10 != -ENOSYS) {
1337 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", r10);
1340 #elif defined(MICROBLAZE)
1341 if (upeek(tcp, 3 * 4, &r3) < 0)
1343 if (r3 != -ENOSYS) {
1345 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", r3);
1354 internal_syscall(struct tcb *tcp)
1357 * We must always trace a few critical system calls in order to
1358 * correctly support following forks in the presence of tracing
1363 if (!SCNO_IN_RANGE(tcp->scno))
1366 func = sysent[tcp->scno].sys_func;
1368 if ( sys_fork == func
1369 #if defined(FREEBSD) || defined(LINUX) || defined(SUNOS4)
1370 || sys_vfork == func
1373 || sys_clone == func
1376 || sys_rfork == func
1379 return internal_fork(tcp);
1381 #if defined SUNOS4 || (defined LINUX && defined TCB_WAITEXECVE)
1382 if ( sys_execve == func
1383 # if defined(SPARC) || defined(SPARC64) || defined(SUNOS4)
1384 || sys_execv == func
1387 || sys_rexecve == func
1390 return internal_exec(tcp);
1397 syscall_enter(struct tcb *tcp)
1402 if (SCNO_IN_RANGE(tcp->scno))
1403 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1405 nargs = tcp->u_nargs = MAX_ARGS;
1407 # if defined(S390) || defined(S390X)
1408 for (i = 0; i < nargs; ++i)
1409 if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1411 # elif defined(ALPHA)
1412 for (i = 0; i < nargs; ++i)
1413 if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
1415 # elif defined(IA64)
1417 unsigned long *out0, cfm, sof, sol;
1419 /* be backwards compatible with kernel < 2.4.4... */
1421 # define PT_RBS_END PT_AR_BSP
1424 if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
1426 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1429 sof = (cfm >> 0) & 0x7f;
1430 sol = (cfm >> 7) & 0x7f;
1431 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1433 for (i = 0; i < nargs; ++i) {
1434 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1435 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1439 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1440 PT_R9 /* ECX = out1 */,
1441 PT_R10 /* EDX = out2 */,
1442 PT_R14 /* ESI = out3 */,
1443 PT_R15 /* EDI = out4 */,
1444 PT_R13 /* EBP = out5 */};
1446 for (i = 0; i < nargs; ++i) {
1447 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1449 /* truncate away IVE sign-extension */
1450 tcp->u_arg[i] &= 0xffffffff;
1453 # elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
1454 /* N32 and N64 both use up to six registers. */
1455 unsigned long long regs[38];
1457 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1460 for (i = 0; i < nargs; ++i) {
1461 tcp->u_arg[i] = regs[REG_A0 + i];
1462 # if defined(LINUX_MIPSN32)
1463 tcp->ext_arg[i] = regs[REG_A0 + i];
1466 # elif defined(MIPS)
1470 if (upeek(tcp, REG_SP, &sp) < 0)
1472 for (i = 0; i < 4; ++i)
1473 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1475 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
1476 (char *)(tcp->u_arg + 4));
1478 for (i = 0; i < nargs; ++i)
1479 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1482 # elif defined(POWERPC)
1484 # define PT_ORIG_R3 34
1486 for (i = 0; i < nargs; ++i) {
1487 if (upeek(tcp, (i==0) ?
1488 (sizeof(unsigned long) * PT_ORIG_R3) :
1489 ((i+PT_R3) * sizeof(unsigned long)),
1490 &tcp->u_arg[i]) < 0)
1493 # elif defined(SPARC) || defined(SPARC64)
1494 for (i = 0; i < nargs; ++i)
1495 tcp->u_arg[i] = regs.u_regs[U_REG_O0 + i];
1496 # elif defined(HPPA)
1497 for (i = 0; i < nargs; ++i)
1498 if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1501 for (i = 0; i < nargs; ++i)
1502 tcp->u_arg[i] = regs.uregs[i];
1503 # elif defined(AVR32)
1506 tcp->u_arg[0] = regs.r12;
1507 tcp->u_arg[1] = regs.r11;
1508 tcp->u_arg[2] = regs.r10;
1509 tcp->u_arg[3] = regs.r9;
1510 tcp->u_arg[4] = regs.r5;
1511 tcp->u_arg[5] = regs.r3;
1512 # elif defined(BFIN)
1513 static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
1515 for (i = 0; i < nargs; ++i)
1516 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1519 static const int syscall_regs[MAX_ARGS] = {
1520 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1521 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
1524 for (i = 0; i < nargs; ++i)
1525 if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
1527 # elif defined(SH64)
1529 /* Registers used by SH5 Linux system calls for parameters */
1530 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
1532 for (i = 0; i < nargs; ++i)
1533 if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1535 # elif defined(X86_64)
1538 if (current_personality == 0) { /* x86-64 ABI */
1539 tcp->u_arg[0] = x86_64_regs.rdi;
1540 tcp->u_arg[1] = x86_64_regs.rsi;
1541 tcp->u_arg[2] = x86_64_regs.rdx;
1542 tcp->u_arg[3] = x86_64_regs.r10;
1543 tcp->u_arg[4] = x86_64_regs.r8;
1544 tcp->u_arg[5] = x86_64_regs.r9;
1545 } else { /* i386 ABI */
1546 /* Sign-extend lower 32 bits */
1547 tcp->u_arg[0] = (long)(int)x86_64_regs.rbx;
1548 tcp->u_arg[1] = (long)(int)x86_64_regs.rcx;
1549 tcp->u_arg[2] = (long)(int)x86_64_regs.rdx;
1550 tcp->u_arg[3] = (long)(int)x86_64_regs.rsi;
1551 tcp->u_arg[4] = (long)(int)x86_64_regs.rdi;
1552 tcp->u_arg[5] = (long)(int)x86_64_regs.rbp;
1554 # elif defined(MICROBLAZE)
1555 for (i = 0; i < nargs; ++i)
1556 if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1558 # elif defined(CRISV10) || defined(CRISV32)
1559 static const int crisregs[MAX_ARGS] = {
1560 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
1561 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
1564 for (i = 0; i < nargs; ++i)
1565 if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0)
1567 # elif defined(TILE)
1568 for (i = 0; i < nargs; ++i)
1569 if (upeek(tcp, PTREGS_OFFSET_REG(i), &tcp->u_arg[i]) < 0)
1571 # elif defined(M68K)
1572 for (i = 0; i < nargs; ++i)
1573 if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1575 # elif defined(I386)
1578 tcp->u_arg[0] = i386_regs.ebx;
1579 tcp->u_arg[1] = i386_regs.ecx;
1580 tcp->u_arg[2] = i386_regs.edx;
1581 tcp->u_arg[3] = i386_regs.esi;
1582 tcp->u_arg[4] = i386_regs.edi;
1583 tcp->u_arg[5] = i386_regs.ebp;
1584 # else /* Other architecture (32bits specific) */
1585 for (i = 0; i < nargs; ++i)
1586 if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
1592 if (SCNO_IN_RANGE(tcp->scno))
1593 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1595 nargs = tcp->u_nargs = MAX_ARGS;
1596 for (i = 0; i < nargs; i++) {
1599 if (upeek(tcp, uoff(u_arg[0]) +
1600 (i * sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1607 * SGI is broken: even though it has pr_sysarg, it doesn't
1608 * set them on system call entry. Get a clue.
1610 if (SCNO_IN_RANGE(tcp->scno))
1611 tcp->u_nargs = sysent[tcp->scno].nargs;
1613 tcp->u_nargs = tcp->status.pr_nsysarg;
1614 if (tcp->u_nargs > 4) {
1615 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1616 4 * sizeof(tcp->u_arg[0]));
1617 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1618 (tcp->u_nargs - 4) * sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1621 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1622 tcp->u_nargs * sizeof(tcp->u_arg[0]));
1624 # elif UNIXWARE >= 2
1626 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1628 if (SCNO_IN_RANGE(tcp->scno))
1629 tcp->u_nargs = sysent[tcp->scno].nargs;
1631 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1632 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1633 tcp->u_nargs * sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1634 # elif defined(HAVE_PR_SYSCALL)
1636 if (SCNO_IN_RANGE(tcp->scno))
1637 tcp->u_nargs = sysent[tcp->scno].nargs;
1639 tcp->u_nargs = tcp->status.pr_nsysarg;
1640 for (i = 0; i < tcp->u_nargs; i++)
1641 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1642 # elif defined(I386)
1643 if (SCNO_IN_RANGE(tcp->scno))
1644 tcp->u_nargs = sysent[tcp->scno].nargs;
1647 if (tcp->u_nargs > 0)
1648 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1649 tcp->u_nargs * sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1651 I DONT KNOW WHAT TO DO
1655 if (SCNO_IN_RANGE(tcp->scno) &&
1656 sysent[tcp->scno].nargs > tcp->status.val)
1657 tcp->u_nargs = sysent[tcp->scno].nargs;
1659 tcp->u_nargs = tcp->status.val;
1660 if (tcp->u_nargs < 0)
1662 if (tcp->u_nargs > MAX_ARGS)
1663 tcp->u_nargs = MAX_ARGS;
1664 switch (regs.r_eax) {
1666 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1667 regs.r_esp + sizeof(int) + sizeof(quad_t));
1670 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1671 regs.r_esp + 2 * sizeof(int));
1674 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1675 regs.r_esp + sizeof(int));
1678 #endif /* FREEBSD */
1683 trace_syscall_entering(struct tcb *tcp)
1687 #if defined TCB_WAITEXECVE
1688 if (tcp->flags & TCB_WAITEXECVE) {
1689 /* This is the post-execve SIGTRAP. */
1690 tcp->flags &= ~TCB_WAITEXECVE;
1695 scno_good = res = get_scno(tcp);
1699 res = syscall_fixup_on_sysenter(tcp);
1703 res = syscall_enter(tcp);
1709 tcp->flags &= ~TCB_REPRINT;
1712 tprintf("????" /* anti-trigraph gap */ "(");
1713 else if (!SCNO_IN_RANGE(tcp->scno))
1714 tprintf("syscall_%lu(", tcp->scno);
1716 tprintf("%s(", sysent[tcp->scno].sys_name);
1718 * " <unavailable>" will be added later by the code which
1719 * detects ptrace errors.
1724 switch (known_scno(tcp)) {
1725 #ifdef SYS_socket_subcall
1726 case SYS_socketcall:
1727 decode_subcall(tcp, SYS_socket_subcall,
1728 SYS_socket_nsubcalls, deref_style);
1731 #ifdef SYS_ipc_subcall
1733 decode_subcall(tcp, SYS_ipc_subcall,
1734 SYS_ipc_nsubcalls, shift_style);
1738 #ifdef SYS_pgrpsys_subcall
1740 decode_subcall(tcp, SYS_pgrpsys_subcall,
1741 SYS_pgrpsys_nsubcalls, shift_style);
1743 #endif /* SYS_pgrpsys_subcall */
1744 #ifdef SYS_sigcall_subcall
1746 decode_subcall(tcp, SYS_sigcall_subcall,
1747 SYS_sigcall_nsubcalls, mask_style);
1749 #endif /* SYS_sigcall_subcall */
1751 decode_subcall(tcp, SYS_msgsys_subcall,
1752 SYS_msgsys_nsubcalls, shift_style);
1755 decode_subcall(tcp, SYS_shmsys_subcall,
1756 SYS_shmsys_nsubcalls, shift_style);
1759 decode_subcall(tcp, SYS_semsys_subcall,
1760 SYS_semsys_nsubcalls, shift_style);
1763 decode_subcall(tcp, SYS_sysfs_subcall,
1764 SYS_sysfs_nsubcalls, shift_style);
1767 decode_subcall(tcp, SYS_spcall_subcall,
1768 SYS_spcall_nsubcalls, shift_style);
1770 #ifdef SYS_context_subcall
1772 decode_subcall(tcp, SYS_context_subcall,
1773 SYS_context_nsubcalls, shift_style);
1775 #endif /* SYS_context_subcall */
1776 #ifdef SYS_door_subcall
1778 decode_subcall(tcp, SYS_door_subcall,
1779 SYS_door_nsubcalls, door_style);
1781 #endif /* SYS_door_subcall */
1782 #ifdef SYS_kaio_subcall
1784 decode_subcall(tcp, SYS_kaio_subcall,
1785 SYS_kaio_nsubcalls, shift_style);
1793 decode_subcall(tcp, 0, 0, table_style);
1798 decode_subcall(tcp, SYS_semsys_subcall,
1799 SYS_semsys_nsubcalls, shift_style);
1802 decode_subcall(tcp, SYS_msgsys_subcall,
1803 SYS_msgsys_nsubcalls, shift_style);
1806 decode_subcall(tcp, SYS_shmsys_subcall,
1807 SYS_shmsys_nsubcalls, shift_style);
1812 internal_syscall(tcp);
1814 if ((SCNO_IN_RANGE(tcp->scno) &&
1815 !(qual_flags[tcp->scno] & QUAL_TRACE)) ||
1816 (tracing_paths && !pathtrace_match(tcp))) {
1817 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1821 tcp->flags &= ~TCB_FILTERED;
1823 if (cflag == CFLAG_ONLY_STATS) {
1829 tcp->flags &= ~TCB_REPRINT;
1831 if (!SCNO_IN_RANGE(tcp->scno))
1832 tprintf("syscall_%lu(", tcp->scno);
1834 tprintf("%s(", sysent[tcp->scno].sys_name);
1835 if (!SCNO_IN_RANGE(tcp->scno) ||
1836 ((qual_flags[tcp->scno] & QUAL_RAW) &&
1837 sysent[tcp->scno].sys_func != sys_exit))
1838 res = printargs(tcp);
1840 res = (*sysent[tcp->scno].sys_func)(tcp);
1842 if (fflush(tcp->outf) == EOF)
1845 tcp->flags |= TCB_INSYSCALL;
1846 /* Measure the entrance time as late as possible to avoid errors. */
1848 gettimeofday(&tcp->etime, NULL);
1853 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1854 * 1: ok, continue in trace_syscall().
1855 * other: error, trace_syscall() should print error indicator
1856 * ("????" etc) and bail out.
1859 get_syscall_result(struct tcb *tcp)
1862 # if defined(S390) || defined(S390X)
1863 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1865 # elif defined (POWERPC)
1866 # define SO_MASK 0x10000000
1869 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1871 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &result) < 0)
1873 if (flags & SO_MASK)
1876 # elif defined(AVR32)
1877 /* Read complete register set in one go. */
1878 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, ®s) < 0)
1880 # elif defined(BFIN)
1881 if (upeek(tcp, PT_R0, &r0) < 0)
1883 # elif defined (I386)
1884 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &i386_regs) < 0)
1886 # elif defined (X86_64)
1887 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &x86_64_regs) < 0)
1889 # elif defined(IA64)
1890 # define IA64_PSR_IS ((long)1 << 34)
1891 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
1892 ia32 = (psr & IA64_PSR_IS) != 0;
1893 if (upeek(tcp, PT_R8, &r8) < 0)
1895 if (upeek(tcp, PT_R10, &r10) < 0)
1897 # elif defined (ARM)
1898 /* Read complete register set in one go. */
1899 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (void *)®s) == -1)
1901 # elif defined (M68K)
1902 if (upeek(tcp, 4*PT_D0, &d0) < 0)
1904 # elif defined (LINUX_MIPSN32)
1905 unsigned long long regs[38];
1907 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1911 # elif defined (MIPS)
1912 if (upeek(tcp, REG_A3, &a3) < 0)
1914 if (upeek(tcp, REG_V0, &r2) < 0)
1916 # elif defined (ALPHA)
1917 if (upeek(tcp, REG_A3, &a3) < 0)
1919 if (upeek(tcp, REG_R0, &r0) < 0)
1921 # elif defined (SPARC) || defined (SPARC64)
1922 /* Everything we need is in the current register set. */
1923 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1925 # elif defined(HPPA)
1926 if (upeek(tcp, PT_GR28, &r28) < 0)
1929 # elif defined(SH64)
1930 # elif defined(CRISV10) || defined(CRISV32)
1931 if (upeek(tcp, 4*PT_R10, &r10) < 0)
1933 # elif defined(TILE)
1934 # elif defined(MICROBLAZE)
1935 if (upeek(tcp, 3 * 4, &r3) < 0)
1942 /* new syscall ABI returns result in R0 */
1943 if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0)
1946 /* ABI defines result returned in r9 */
1947 if (upeek(tcp, REG_GENERAL(9), (long *)&r9) < 0)
1952 # ifndef HAVE_PR_SYSCALL
1954 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1958 # endif /* FREEBSD */
1959 # endif /* !HAVE_PR_SYSCALL */
1960 #endif /* USE_PROCFS */
1965 /* Called at each syscall exit.
1967 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1968 * 1: ok, continue in trace_syscall().
1969 * other: error, trace_syscall() should print error indicator
1970 * ("????" etc) and bail out.
1973 syscall_fixup_on_sysexit(struct tcb *tcp)
1976 if (tcp->status.PR_WHY != PR_SYSEXIT) {
1977 fprintf(stderr, "syscall: missing exit\n");
1978 tcp->flags &= ~TCB_INSYSCALL;
1980 #endif /* USE_PROCFS */
1984 int scno = known_scno(tcp);
1988 * This happens when a signal handler
1989 * for a signal which interrupted a
1990 * a system call makes another system call.
1992 fprintf(stderr, "syscall: missing exit\n");
1994 tcp->flags &= ~TCB_INSYSCALL;
2000 # if defined (S390) || defined (S390X)
2001 if (syscall_mode != -ENOSYS)
2002 syscall_mode = tcp->scno;
2003 if ((tcp->flags & TCB_WAITEXECVE)
2004 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
2006 * Return from execve.
2007 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2008 * flag set for the post-execve SIGTRAP to see and reset.
2019 * Check the syscall return value register value for whether it is
2020 * a negated errno code indicating an error, or a success return value.
2023 is_negated_errno(unsigned long int val)
2025 unsigned long int max = -(long int) nerrnos;
2026 # if SUPPORTED_PERSONALITIES > 1
2027 if (personality_wordsize[current_personality] < sizeof(val)) {
2028 val = (unsigned int) val;
2029 max = (unsigned int) max;
2037 get_error(struct tcb *tcp)
2041 int check_errno = 1;
2042 if (SCNO_IN_RANGE(tcp->scno) &&
2043 sysent[tcp->scno].sys_flags & SYSCALL_NEVER_FAILS) {
2046 # if defined(S390) || defined(S390X)
2047 if (check_errno && is_negated_errno(gpr2)) {
2054 # elif defined(I386)
2055 if (check_errno && is_negated_errno(i386_regs.eax)) {
2057 u_error = -i386_regs.eax;
2060 tcp->u_rval = i386_regs.eax;
2062 # elif defined(X86_64)
2063 if (check_errno && is_negated_errno(x86_64_regs.rax)) {
2065 u_error = -x86_64_regs.rax;
2068 tcp->u_rval = x86_64_regs.rax;
2070 # elif defined(IA64)
2075 if (check_errno && is_negated_errno(err)) {
2083 if (check_errno && r10) {
2090 # elif defined(MIPS)
2091 if (check_errno && a3) {
2097 # elif defined(POWERPC)
2098 if (check_errno && is_negated_errno(result)) {
2103 tcp->u_rval = result;
2105 # elif defined(M68K)
2106 if (check_errno && is_negated_errno(d0)) {
2114 if (check_errno && is_negated_errno(regs.ARM_r0)) {
2116 u_error = -regs.ARM_r0;
2119 tcp->u_rval = regs.ARM_r0;
2121 # elif defined(AVR32)
2122 if (check_errno && regs.r12 && (unsigned) -regs.r12 < nerrnos) {
2124 u_error = -regs.r12;
2127 tcp->u_rval = regs.r12;
2129 # elif defined(BFIN)
2130 if (check_errno && is_negated_errno(r0)) {
2136 # elif defined(ALPHA)
2137 if (check_errno && a3) {
2144 # elif defined(SPARC)
2145 if (check_errno && regs.psr & PSR_C) {
2147 u_error = regs.u_regs[U_REG_O0];
2150 tcp->u_rval = regs.u_regs[U_REG_O0];
2152 # elif defined(SPARC64)
2153 if (check_errno && regs.tstate & 0x1100000000UL) {
2155 u_error = regs.u_regs[U_REG_O0];
2158 tcp->u_rval = regs.u_regs[U_REG_O0];
2160 # elif defined(HPPA)
2161 if (check_errno && is_negated_errno(r28)) {
2169 if (check_errno && is_negated_errno(r0)) {
2176 # elif defined(SH64)
2177 if (check_errno && is_negated_errno(r9)) {
2184 # elif defined(CRISV10) || defined(CRISV32)
2185 if (check_errno && r10 && (unsigned) -r10 < nerrnos) {
2192 # elif defined(TILE)
2194 if (upeek(tcp, PTREGS_OFFSET_REG(0), &rval) < 0)
2196 if (check_errno && rval < 0 && rval > -nerrnos) {
2203 # elif defined(MICROBLAZE)
2204 if (check_errno && is_negated_errno(r3)) {
2214 /* get error code from user struct */
2215 if (upeek(tcp, uoff(u_error), &u_error) < 0)
2217 u_error >>= 24; /* u_error is a char */
2219 /* get system call return value */
2220 if (upeek(tcp, uoff(u_rval1), &tcp->u_rval) < 0)
2225 /* Judicious guessing goes a long way. */
2226 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
2228 u_error = tcp->status.pr_reg[R_O0];
2231 tcp->u_rval = tcp->status.pr_reg[R_O0];
2235 if (tcp->status.PR_REG[EFL] & 0x1) {
2237 u_error = tcp->status.PR_REG[EAX];
2240 tcp->u_rval = tcp->status.PR_REG[EAX];
2241 # ifdef HAVE_LONG_LONG
2243 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
2244 tcp->status.PR_REG[EAX];
2249 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
2251 u_error = tcp->status.PR_REG[RAX];
2254 tcp->u_rval = tcp->status.PR_REG[RAX];
2256 # endif /* X86_64 */
2258 if (tcp->status.pr_reg[CTX_A3]) {
2260 u_error = tcp->status.pr_reg[CTX_V0];
2263 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
2268 if (regs.r_eflags & PSL_C) {
2270 u_error = regs.r_eax;
2272 tcp->u_rval = regs.r_eax;
2274 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
2276 #endif /* FREEBSD */
2277 tcp->u_error = u_error;
2282 dumpio(struct tcb *tcp)
2286 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
2288 if (!SCNO_IN_RANGE(tcp->scno))
2290 if (sysent[tcp->scno].sys_func == printargs)
2292 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
2293 if (sysent[tcp->scno].sys_func == sys_read ||
2294 sysent[tcp->scno].sys_func == sys_pread ||
2295 sysent[tcp->scno].sys_func == sys_pread64 ||
2296 sysent[tcp->scno].sys_func == sys_recv ||
2297 sysent[tcp->scno].sys_func == sys_recvfrom)
2298 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
2299 else if (sysent[tcp->scno].sys_func == sys_readv)
2300 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2303 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
2304 if (sysent[tcp->scno].sys_func == sys_write ||
2305 sysent[tcp->scno].sys_func == sys_pwrite ||
2306 sysent[tcp->scno].sys_func == sys_pwrite64 ||
2307 sysent[tcp->scno].sys_func == sys_send ||
2308 sysent[tcp->scno].sys_func == sys_sendto)
2309 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
2310 else if (sysent[tcp->scno].sys_func == sys_writev)
2311 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2317 trace_syscall_exiting(struct tcb *tcp)
2324 /* Measure the exit time as early as possible to avoid errors. */
2326 gettimeofday(&tv, NULL);
2328 res = get_syscall_result(tcp);
2332 res = syscall_fixup_on_sysexit(tcp);
2336 res = get_error(tcp);
2340 internal_syscall(tcp);
2342 if (res == 1 && filtered(tcp)) {
2346 if (tcp->flags & TCB_REPRINT) {
2348 if (!SCNO_IN_RANGE(tcp->scno))
2349 tprintf("<... syscall_%lu resumed> ", tcp->scno);
2351 tprintf("<... %s resumed> ", sysent[tcp->scno].sys_name);
2355 struct timeval t = tv;
2356 count_syscall(tcp, &t);
2357 if (cflag == CFLAG_ONLY_STATS) {
2365 tprints("= ? <unavailable>");
2367 tcp->flags &= ~TCB_INSYSCALL;
2371 if (!SCNO_IN_RANGE(tcp->scno)
2372 || (qual_flags[tcp->scno] & QUAL_RAW))
2373 sys_res = printargs(tcp);
2375 /* FIXME: not_failing_only (IOW, option -z) is broken:
2376 * failure of syscall is known only after syscall return.
2377 * Thus we end up with something like this on, say, ENOENT:
2378 * open("doesnt_exist", O_RDONLY <unfinished ...>
2379 * {next syscall decode}
2380 * whereas the intended result is that open(...) line
2381 * is not shown at all.
2383 if (not_failing_only && tcp->u_error)
2384 goto ret; /* ignore failed syscalls */
2385 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2390 u_error = tcp->u_error;
2391 if (!SCNO_IN_RANGE(tcp->scno) ||
2392 qual_flags[tcp->scno] & QUAL_RAW) {
2394 tprintf("= -1 (errno %ld)", u_error);
2396 tprintf("= %#lx", tcp->u_rval);
2398 else if (!(sys_res & RVAL_NONE) && u_error) {
2402 tprints("= ? ERESTARTSYS (To be restarted)");
2404 case ERESTARTNOINTR:
2405 tprints("= ? ERESTARTNOINTR (To be restarted)");
2407 case ERESTARTNOHAND:
2408 tprints("= ? ERESTARTNOHAND (To be restarted)");
2410 case ERESTART_RESTARTBLOCK:
2411 tprints("= ? ERESTART_RESTARTBLOCK (To be restarted)");
2416 tprintf("= -1 E??? (errno %ld)", u_error);
2417 else if (u_error < nerrnos)
2418 tprintf("= -1 %s (%s)", errnoent[u_error],
2421 tprintf("= -1 ERRNO_%ld (%s)", u_error,
2425 if ((sys_res & RVAL_STR) && tcp->auxstr)
2426 tprintf(" (%s)", tcp->auxstr);
2429 if (sys_res & RVAL_NONE)
2432 switch (sys_res & RVAL_MASK) {
2434 tprintf("= %#lx", tcp->u_rval);
2437 tprintf("= %#lo", tcp->u_rval);
2440 tprintf("= %lu", tcp->u_rval);
2443 tprintf("= %ld", tcp->u_rval);
2445 #ifdef HAVE_LONG_LONG
2447 tprintf("= %#llx", tcp->u_lrval);
2450 tprintf("= %#llo", tcp->u_lrval);
2452 case RVAL_LUDECIMAL:
2453 tprintf("= %llu", tcp->u_lrval);
2456 tprintf("= %lld", tcp->u_lrval);
2461 "invalid rval format\n");
2465 if ((sys_res & RVAL_STR) && tcp->auxstr)
2466 tprintf(" (%s)", tcp->auxstr);
2469 tv_sub(&tv, &tv, &tcp->etime);
2470 tprintf(" <%ld.%06ld>",
2471 (long) tv.tv_sec, (long) tv.tv_usec);
2476 if (fflush(tcp->outf) == EOF)
2479 tcp->flags &= ~TCB_INSYSCALL;
2484 trace_syscall(struct tcb *tcp)
2486 return exiting(tcp) ?
2487 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);