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);
477 fprintf(stderr, "out of memory\n");
480 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
481 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
482 for (i = 0; i < nsyscalls0; i++)
483 if (sysent0[i].sys_flags & n)
484 qualify_one(i, opt->bitflag, not, 0);
486 #if SUPPORTED_PERSONALITIES >= 2
487 for (i = 0; i < nsyscalls1; i++)
488 if (sysent1[i].sys_flags & n)
489 qualify_one(i, opt->bitflag, not, 1);
490 #endif /* SUPPORTED_PERSONALITIES >= 2 */
492 #if SUPPORTED_PERSONALITIES >= 3
493 for (i = 0; i < nsyscalls2; i++)
494 if (sysent2[i].sys_flags & n)
495 qualify_one(i, opt->bitflag, not, 2);
496 #endif /* SUPPORTED_PERSONALITIES >= 3 */
500 if (opt->qualify(p, opt->bitflag, not)) {
501 fprintf(stderr, "strace: invalid %s `%s'\n",
502 opt->argument_name, p);
511 enum subcall_style { shift_style, deref_style, mask_style, door_style };
513 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
521 static const struct subcall subcalls_table[] = {
522 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
524 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
526 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
528 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
532 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) || defined(__ARM_EABI__) ))
535 decode_subcall(struct tcb *tcp, int subcall, int nsubcalls, enum subcall_style style)
537 unsigned long addr, mask;
539 int size = personality_wordsize[current_personality];
543 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
545 tcp->scno = subcall + tcp->u_arg[0];
546 tcp->u_nargs = n = sysent[tcp->scno].nargs;
547 for (i = 0; i < n; i++)
548 tcp->u_arg[i] = tcp->u_arg[i + 1];
551 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
553 tcp->scno = subcall + tcp->u_arg[0];
554 addr = tcp->u_arg[1];
555 tcp->u_nargs = n = sysent[tcp->scno].nargs;
556 for (i = 0; i < n; i++) {
557 if (size == sizeof(int)) {
559 if (umove(tcp, addr, &arg) < 0)
563 else if (size == sizeof(long)) {
565 if (umove(tcp, addr, &arg) < 0)
575 mask = (tcp->u_arg[0] >> 8) & 0xff;
576 for (i = 0; mask; i++)
580 tcp->u_arg[0] &= 0xff;
581 tcp->scno = subcall + i;
582 tcp->u_nargs = sysent[tcp->scno].nargs;
586 * Oh, yuck. The call code is the *sixth* argument.
587 * (don't you mean the *last* argument? - JH)
589 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
591 tcp->scno = subcall + tcp->u_arg[5];
592 tcp->u_nargs = sysent[tcp->scno].nargs;
596 for (i = 0; i < ARRAY_SIZE(subcalls_table); i++)
597 if (subcalls_table[i].call == tcp->scno) break;
598 if (i < ARRAY_SIZE(subcalls_table) &&
599 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
600 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
601 for (i = 0; i < tcp->u_nargs; i++)
602 tcp->u_arg[i] = tcp->u_arg[i + 1];
611 printargs(struct tcb *tcp)
616 for (i = 0; i < tcp->u_nargs; i++)
617 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
623 getrval2(struct tcb *tcp)
628 #if defined (SPARC) || defined (SPARC64)
630 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
632 val = regs.u_regs[U_REG_O1];
634 if (upeek(tcp, 4*(REG_REG0+1), &val) < 0)
637 if (upeek(tcp, PT_R9, &val) < 0)
643 if (upeek(tcp, uoff(u_rval2), &val) < 0)
649 val = tcp->status.PR_REG[R_O1];
652 val = tcp->status.PR_REG[EDX];
655 val = tcp->status.PR_REG[RDX];
658 val = tcp->status.PR_REG[CTX_V1];
664 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
672 * Apparently, indirect system calls have already be converted by ptrace(2),
673 * so if you see "indir" this program has gone astray.
676 sys_indir(struct tcb *tcp)
682 scno = tcp->u_arg[0];
683 if (!SCNO_IN_RANGE(scno)) {
684 fprintf(stderr, "Bogus syscall: %ld\n", scno);
687 nargs = sysent[scno].nargs;
688 tprintf("%s", sysent[scno].sys_name);
689 for (i = 0; i < nargs; i++)
690 tprintf(", %#lx", tcp->u_arg[i+1]);
697 is_restart_error(struct tcb *tcp)
702 switch (tcp->u_error) {
706 case ERESTART_RESTARTBLOCK:
715 struct tcb *tcp_last = NULL;
720 # elif defined (IA64)
721 long r8, r10, psr; /* TODO: make static? */
722 long ia32 = 0; /* not static */
723 # elif defined (POWERPC)
724 static long result, flags;
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(X86_64)
757 # elif defined(CRISV10) || defined(CRISV32)
759 # elif defined(MICROBLAZE)
764 struct reg regs; /* TODO: make static? */
768 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
769 * 1: ok, continue in trace_syscall().
770 * other: error, trace_syscall() should print error indicator
771 * ("????" etc) and bail out.
777 get_scno(struct tcb *tcp)
782 # if defined(S390) || defined(S390X)
783 if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
786 if (syscall_mode != -ENOSYS) {
788 * Since kernel version 2.5.44 the scno gets passed in gpr2.
793 * Old style of "passing" the scno via the SVC instruction.
795 long opcode, offset_reg, tmp;
797 static const int gpr_offset[16] = {
798 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
799 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
800 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
801 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
804 if (upeek(tcp, PT_PSWADDR, &pc) < 0)
807 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-sizeof(long)), 0);
809 perror("peektext(pc-oneword)");
814 * We have to check if the SVC got executed directly or via an
815 * EXECUTE instruction. In case of EXECUTE it is necessary to do
816 * instruction decoding to derive the system call number.
817 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
818 * so that this doesn't work if a SVC opcode is part of an EXECUTE
819 * opcode. Since there is no way to find out the opcode size this
820 * is the best we can do...
822 if ((opcode & 0xff00) == 0x0a00) {
824 scno = opcode & 0xff;
827 /* SVC got executed by EXECUTE instruction */
830 * Do instruction decoding of EXECUTE. If you really want to
831 * understand this, read the Principles of Operations.
833 svc_addr = (void *) (opcode & 0xfff);
836 offset_reg = (opcode & 0x000f0000) >> 16;
837 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
842 offset_reg = (opcode & 0x0000f000) >> 12;
843 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
847 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
856 offset_reg = (opcode & 0x00f00000) >> 20;
857 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
860 scno = (scno | tmp) & 0xff;
863 # elif defined (POWERPC)
864 if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
867 /* TODO: speed up strace by not doing this at every syscall.
868 * We only need to do it after execve.
874 /* Check for 64/32 bit mode. */
875 if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
877 /* SF is bit 0 of MSR */
882 if (currpers != current_personality) {
883 static const char *const names[] = {"64 bit", "32 bit"};
884 set_personality(currpers);
885 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
886 pid, names[current_personality]);
889 # elif defined(AVR32)
890 /* Read complete register set in one go. */
891 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, ®s) < 0)
895 if (upeek(tcp, PT_ORIG_P0, &scno))
897 # elif defined (I386)
898 if (upeek(tcp, 4*ORIG_EAX, &scno) < 0)
900 # elif defined (X86_64)
901 if (upeek(tcp, 8*ORIG_RAX, &scno) < 0)
904 /* TODO: speed up strace by not doing this at every syscall.
905 * We only need to do it after execve.
911 /* Check CS register value. On x86-64 linux it is:
912 * 0x33 for long mode (64 bit)
913 * 0x23 for compatibility mode (32 bit)
914 * It takes only one ptrace and thus doesn't need
917 if (upeek(tcp, 8*CS, &val) < 0)
920 case 0x23: currpers = 1; break;
921 case 0x33: currpers = 0; break;
923 fprintf(stderr, "Unknown value CS=0x%02X while "
924 "detecting personality of process "
925 "PID=%d\n", (int)val, pid);
926 currpers = current_personality;
930 /* This version analyzes the opcode of a syscall instruction.
931 * (int 0x80 on i386 vs. syscall on x86-64)
932 * It works, but is too complicated.
934 unsigned long val, rip, i;
936 if (upeek(tcp, 8*RIP, &rip) < 0)
937 perror("upeek(RIP)");
939 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
943 call = ptrace(PTRACE_PEEKTEXT, pid, (char *)rip, (char *)0);
945 fprintf(stderr, "ptrace_peektext failed: %s\n",
947 switch (call & 0xffff) {
948 /* x86-64: syscall = 0x0f 0x05 */
949 case 0x050f: currpers = 0; break;
950 /* i386: int 0x80 = 0xcd 0x80 */
951 case 0x80cd: currpers = 1; break;
953 currpers = current_personality;
955 "Unknown syscall opcode (0x%04X) while "
956 "detecting personality of process "
957 "PID=%d\n", (int)call, pid);
961 if (currpers != current_personality) {
962 static const char *const names[] = {"64 bit", "32 bit"};
963 set_personality(currpers);
964 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
965 pid, names[current_personality]);
968 # define IA64_PSR_IS ((long)1 << 34)
969 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
970 ia32 = (psr & IA64_PSR_IS) != 0;
972 if (upeek(tcp, PT_R1, &scno) < 0) /* orig eax */
975 if (upeek(tcp, PT_R15, &scno) < 0)
979 /* Read complete register set in one go. */
980 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (void *)®s) == -1)
984 * We only need to grab the syscall number on syscall entry.
986 if (regs.ARM_ip == 0) {
988 * Note: we only deal with only 32-bit CPUs here.
990 if (regs.ARM_cpsr & 0x20) {
992 * Get the Thumb-mode system call number
997 * Get the ARM-mode system call number
1000 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(regs.ARM_pc - 4), NULL);
1004 /* Handle the EABI syscall convention. We do not
1005 bother converting structures between the two
1006 ABIs, but basic functionality should work even
1007 if strace and the traced program have different
1009 if (scno == 0xef000000) {
1012 if ((scno & 0x0ff00000) != 0x0f900000) {
1013 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
1019 * Fixup the syscall number
1024 if (scno & 0x0f0000) {
1026 * Handle ARM specific syscall
1034 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
1035 tcp->flags |= TCB_INSYSCALL;
1037 # elif defined (M68K)
1038 if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
1040 # elif defined (LINUX_MIPSN32)
1041 unsigned long long regs[38];
1043 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1049 if (!SCNO_IN_RANGE(scno)) {
1050 if (a3 == 0 || a3 == -1) {
1052 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1056 # elif defined (MIPS)
1057 if (upeek(tcp, REG_A3, &a3) < 0)
1059 if (upeek(tcp, REG_V0, &scno) < 0)
1062 if (!SCNO_IN_RANGE(scno)) {
1063 if (a3 == 0 || a3 == -1) {
1065 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1069 # elif defined (ALPHA)
1070 if (upeek(tcp, REG_A3, &a3) < 0)
1072 if (upeek(tcp, REG_R0, &scno) < 0)
1076 * Do some sanity checks to figure out if it's
1077 * really a syscall entry
1079 if (!SCNO_IN_RANGE(scno)) {
1080 if (a3 == 0 || a3 == -1) {
1082 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
1086 # elif defined (SPARC) || defined (SPARC64)
1087 /* Everything we need is in the current register set. */
1088 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1091 /* Disassemble the syscall trap. */
1092 /* Retrieve the syscall trap instruction. */
1094 # if defined(SPARC64)
1095 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.tpc, 0);
1098 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.pc, 0);
1103 /* Disassemble the trap to see what personality to use. */
1106 /* Linux/SPARC syscall trap. */
1110 /* Linux/SPARC64 syscall trap. */
1114 /* SunOS syscall trap. (pers 1) */
1115 fprintf(stderr, "syscall: SunOS no support\n");
1118 /* Solaris 2.x syscall trap. (per 2) */
1122 /* NetBSD/FreeBSD syscall trap. */
1123 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1126 /* Solaris 2.x gettimeofday */
1130 # if defined (SPARC64)
1131 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, regs.tpc);
1133 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, regs.pc);
1138 /* Extract the system call number from the registers. */
1139 if (trap == 0x91d02027)
1142 scno = regs.u_regs[U_REG_G1];
1144 scno = regs.u_regs[U_REG_O0];
1145 memmove(®s.u_regs[U_REG_O0], ®s.u_regs[U_REG_O1], 7*sizeof(regs.u_regs[0]));
1147 # elif defined(HPPA)
1148 if (upeek(tcp, PT_GR20, &scno) < 0)
1152 * In the new syscall ABI, the system call number is in R3.
1154 if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
1158 /* Odd as it may seem, a glibc bug has been known to cause
1159 glibc to issue bogus negative syscall numbers. So for
1160 our purposes, make strace print what it *should* have been */
1161 long correct_scno = (scno & 0xff);
1164 "Detected glibc bug: bogus system call"
1165 " number = %ld, correcting to %ld\n",
1168 scno = correct_scno;
1170 # elif defined(SH64)
1171 if (upeek(tcp, REG_SYSCALL, &scno) < 0)
1174 # elif defined(CRISV10) || defined(CRISV32)
1175 if (upeek(tcp, 4*PT_R9, &scno) < 0)
1177 # elif defined(TILE)
1178 if (upeek(tcp, PTREGS_OFFSET_REG(10), &scno) < 0)
1180 # elif defined(MICROBLAZE)
1181 if (upeek(tcp, 0, &scno) < 0)
1187 if (upeek(tcp, uoff(u_arg[7]), &scno) < 0)
1190 /* new syscall ABI returns result in R0 */
1191 if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0)
1194 /* ABI defines result returned in r9 */
1195 if (upeek(tcp, REG_GENERAL(9), (long *)&r9) < 0)
1200 # ifdef HAVE_PR_SYSCALL
1201 scno = tcp->status.PR_SYSCALL;
1204 scno = tcp->status.PR_WHAT;
1206 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1210 switch (regs.r_eax) {
1213 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1219 # endif /* FREEBSD */
1220 # endif /* !HAVE_PR_SYSCALL */
1221 #endif /* USE_PROCFS */
1228 known_scno(struct tcb *tcp)
1230 long scno = tcp->scno;
1231 #if SUPPORTED_PERSONALITIES > 1
1232 if (SCNO_IN_RANGE(scno) && sysent[scno].native_scno != 0)
1233 scno = sysent[scno].native_scno;
1236 scno += NR_SYSCALL_BASE;
1240 /* Called at each syscall entry.
1242 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1243 * 1: ok, continue in trace_syscall().
1244 * other: error, trace_syscall() should print error indicator
1245 * ("????" etc) and bail out.
1248 syscall_fixup(struct tcb *tcp)
1251 int scno = known_scno(tcp);
1253 if (tcp->status.PR_WHY != PR_SYSENTRY) {
1257 || scno == SYS_vfork
1260 || scno == SYS_fork1
1263 || scno == SYS_forkall
1266 || scno == SYS_rfork1
1269 || scno == SYS_rforkall
1272 /* We are returning in the child, fake it. */
1273 tcp->status.PR_WHY = PR_SYSENTRY;
1275 tcp->status.PR_WHY = PR_SYSEXIT;
1278 fprintf(stderr, "syscall: missing entry\n");
1279 tcp->flags |= TCB_INSYSCALL;
1282 #endif /* USE_PROCFS */
1286 fprintf(stderr, "syscall: missing entry\n");
1287 tcp->flags |= TCB_INSYSCALL;
1292 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
1294 /* With PTRACE_O_TRACEEXEC, post-execve SIGTRAP is disabled.
1295 * Every extra ptrace call is expensive, so check EAX
1296 * on syscall entry only if PTRACE_O_TRACEEXEC is not enabled:
1298 if (!(ptrace_setoptions & PTRACE_O_TRACEEXEC)) {
1299 if (upeek(tcp, 4*EAX, &eax) < 0)
1301 if (eax != -ENOSYS) {
1303 fprintf(stderr, "not a syscall entry (eax = %ld)\n", eax);
1307 #elif defined (X86_64)
1308 if (!(ptrace_setoptions & PTRACE_O_TRACEEXEC)) {
1309 if (upeek(tcp, 8*RAX, &rax) < 0)
1311 if (current_personality == 1)
1312 rax = (long int)(int)rax; /* sign extend from 32 bits */
1313 if (rax != -ENOSYS && entering(tcp)) {
1315 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1319 #elif defined (S390) || defined (S390X)
1320 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1322 if (syscall_mode != -ENOSYS)
1323 syscall_mode = tcp->scno;
1324 if (gpr2 != syscall_mode) {
1326 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
1329 #elif defined (POWERPC)
1330 # define SO_MASK 0x10000000
1331 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1333 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &result) < 0)
1335 if (flags & SO_MASK)
1337 #elif defined (M68K)
1338 if (upeek(tcp, 4*PT_D0, &d0) < 0)
1340 if (d0 != -ENOSYS) {
1342 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", d0);
1346 /* Nothing required */
1348 if (upeek(tcp, PT_R0, &r0) < 0)
1350 #elif defined (HPPA)
1351 if (upeek(tcp, PT_GR28, &r28) < 0)
1354 if (upeek(tcp, PT_R10, &r10) < 0)
1356 if (upeek(tcp, PT_R8, &r8) < 0)
1358 if (ia32 && r8 != -ENOSYS) {
1360 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", r8);
1363 #elif defined(CRISV10) || defined(CRISV32)
1364 if (upeek(tcp, 4*PT_R10, &r10) < 0)
1366 if (r10 != -ENOSYS) {
1368 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", r10);
1371 #elif defined(MICROBLAZE)
1372 if (upeek(tcp, 3 * 4, &r3) < 0)
1374 if (r3 != -ENOSYS) {
1376 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", r3);
1385 internal_syscall(struct tcb *tcp)
1388 * We must always trace a few critical system calls in order to
1389 * correctly support following forks in the presence of tracing
1394 if (!SCNO_IN_RANGE(tcp->scno))
1397 func = sysent[tcp->scno].sys_func;
1399 if ( sys_fork == func
1400 #if defined(FREEBSD) || defined(LINUX) || defined(SUNOS4)
1401 || sys_vfork == func
1404 || sys_clone == func
1407 || sys_rfork == func
1410 return internal_fork(tcp);
1412 #if defined SUNOS4 || (defined LINUX && defined TCB_WAITEXECVE)
1413 if ( sys_execve == func
1414 # if defined(SPARC) || defined(SPARC64) || defined(SUNOS4)
1415 || sys_execv == func
1418 || sys_rexecve == func
1421 return internal_exec(tcp);
1428 syscall_enter(struct tcb *tcp)
1433 if (SCNO_IN_RANGE(tcp->scno))
1434 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1436 nargs = tcp->u_nargs = MAX_ARGS;
1438 # if defined(S390) || defined(S390X)
1439 for (i = 0; i < nargs; ++i)
1440 if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1442 # elif defined(ALPHA)
1443 for (i = 0; i < nargs; ++i)
1444 if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
1446 # elif defined(IA64)
1448 unsigned long *out0, cfm, sof, sol;
1450 /* be backwards compatible with kernel < 2.4.4... */
1452 # define PT_RBS_END PT_AR_BSP
1455 if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
1457 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1460 sof = (cfm >> 0) & 0x7f;
1461 sol = (cfm >> 7) & 0x7f;
1462 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1464 for (i = 0; i < nargs; ++i) {
1465 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1466 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1470 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1471 PT_R9 /* ECX = out1 */,
1472 PT_R10 /* EDX = out2 */,
1473 PT_R14 /* ESI = out3 */,
1474 PT_R15 /* EDI = out4 */,
1475 PT_R13 /* EBP = out5 */};
1477 for (i = 0; i < nargs; ++i) {
1478 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1480 /* truncate away IVE sign-extension */
1481 tcp->u_arg[i] &= 0xffffffff;
1484 # elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
1485 /* N32 and N64 both use up to six registers. */
1486 unsigned long long regs[38];
1488 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1491 for (i = 0; i < nargs; ++i) {
1492 tcp->u_arg[i] = regs[REG_A0 + i];
1493 # if defined(LINUX_MIPSN32)
1494 tcp->ext_arg[i] = regs[REG_A0 + i];
1497 # elif defined(MIPS)
1501 if (upeek(tcp, REG_SP, &sp) < 0)
1503 for (i = 0; i < 4; ++i)
1504 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1506 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
1507 (char *)(tcp->u_arg + 4));
1509 for (i = 0; i < nargs; ++i)
1510 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1513 # elif defined(POWERPC)
1515 # define PT_ORIG_R3 34
1517 for (i = 0; i < nargs; ++i) {
1518 if (upeek(tcp, (i==0) ?
1519 (sizeof(unsigned long) * PT_ORIG_R3) :
1520 ((i+PT_R3) * sizeof(unsigned long)),
1521 &tcp->u_arg[i]) < 0)
1524 # elif defined(SPARC) || defined(SPARC64)
1525 for (i = 0; i < nargs; ++i)
1526 tcp->u_arg[i] = regs.u_regs[U_REG_O0 + i];
1527 # elif defined(HPPA)
1528 for (i = 0; i < nargs; ++i)
1529 if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1532 for (i = 0; i < nargs; ++i)
1533 tcp->u_arg[i] = regs.uregs[i];
1534 # elif defined(AVR32)
1535 static const unsigned long *argregp[MAX_ARGS] = { ®s.r12,
1541 for (i = 0; i < nargs; ++i)
1542 tcp->u_arg[i] = *argregp[i];
1543 # elif defined(BFIN)
1544 static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
1546 for (i = 0; i < nargs; ++i)
1547 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1550 static const int syscall_regs[MAX_ARGS] = {
1551 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1552 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
1555 for (i = 0; i < nargs; ++i)
1556 if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
1558 # elif defined(SH64)
1560 /* Registers used by SH5 Linux system calls for parameters */
1561 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
1563 for (i = 0; i < nargs; ++i)
1564 if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1566 # elif defined(X86_64)
1567 static const int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
1568 { 8 * RDI, 8 * RSI, 8 * RDX, 8 * R10, 8 * R8 , 8 * R9 }, /* x86-64 ABI */
1569 { 8 * RBX, 8 * RCX, 8 * RDX, 8 * RSI, 8 * RDI, 8 * RBP } /* i386 ABI */
1572 for (i = 0; i < nargs; ++i)
1573 if (upeek(tcp, argreg[current_personality][i], &tcp->u_arg[i]) < 0)
1575 # elif defined(MICROBLAZE)
1576 for (i = 0; i < nargs; ++i)
1577 if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1579 # elif defined(CRISV10) || defined(CRISV32)
1580 static const int crisregs[MAX_ARGS] = {
1581 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
1582 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
1585 for (i = 0; i < nargs; ++i)
1586 if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0)
1588 # elif defined(TILE)
1589 for (i = 0; i < nargs; ++i)
1590 if (upeek(tcp, PTREGS_OFFSET_REG(i), &tcp->u_arg[i]) < 0)
1592 # elif defined(M68K)
1593 for (i = 0; i < nargs; ++i)
1594 if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1596 # else /* Other architecture (like i386) (32bits specific) */
1597 for (i = 0; i < nargs; ++i)
1598 if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
1604 if (SCNO_IN_RANGE(tcp->scno))
1605 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1607 nargs = tcp->u_nargs = MAX_ARGS;
1608 for (i = 0; i < nargs; i++) {
1611 if (upeek(tcp, uoff(u_arg[0]) +
1612 (i * sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1619 * SGI is broken: even though it has pr_sysarg, it doesn't
1620 * set them on system call entry. Get a clue.
1622 if (SCNO_IN_RANGE(tcp->scno))
1623 tcp->u_nargs = sysent[tcp->scno].nargs;
1625 tcp->u_nargs = tcp->status.pr_nsysarg;
1626 if (tcp->u_nargs > 4) {
1627 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1628 4 * sizeof(tcp->u_arg[0]));
1629 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1630 (tcp->u_nargs - 4) * sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1633 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1634 tcp->u_nargs * sizeof(tcp->u_arg[0]));
1636 # elif UNIXWARE >= 2
1638 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1640 if (SCNO_IN_RANGE(tcp->scno))
1641 tcp->u_nargs = sysent[tcp->scno].nargs;
1643 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1644 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1645 tcp->u_nargs * sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1646 # elif defined(HAVE_PR_SYSCALL)
1648 if (SCNO_IN_RANGE(tcp->scno))
1649 tcp->u_nargs = sysent[tcp->scno].nargs;
1651 tcp->u_nargs = tcp->status.pr_nsysarg;
1652 for (i = 0; i < tcp->u_nargs; i++)
1653 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1654 # elif defined(I386)
1655 if (SCNO_IN_RANGE(tcp->scno))
1656 tcp->u_nargs = sysent[tcp->scno].nargs;
1659 if (tcp->u_nargs > 0)
1660 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1661 tcp->u_nargs * sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1663 I DONT KNOW WHAT TO DO
1667 if (SCNO_IN_RANGE(tcp->scno) &&
1668 sysent[tcp->scno].nargs > tcp->status.val)
1669 tcp->u_nargs = sysent[tcp->scno].nargs;
1671 tcp->u_nargs = tcp->status.val;
1672 if (tcp->u_nargs < 0)
1674 if (tcp->u_nargs > MAX_ARGS)
1675 tcp->u_nargs = MAX_ARGS;
1676 switch (regs.r_eax) {
1678 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1679 regs.r_esp + sizeof(int) + sizeof(quad_t));
1682 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1683 regs.r_esp + 2 * sizeof(int));
1686 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1687 regs.r_esp + sizeof(int));
1690 #endif /* FREEBSD */
1695 trace_syscall_entering(struct tcb *tcp)
1699 #if defined TCB_WAITEXECVE
1700 if (tcp->flags & TCB_WAITEXECVE) {
1701 /* This is the post-execve SIGTRAP. */
1702 tcp->flags &= ~TCB_WAITEXECVE;
1707 scno_good = res = get_scno(tcp);
1711 res = syscall_fixup(tcp);
1715 res = syscall_enter(tcp);
1721 tcp->flags &= ~TCB_REPRINT;
1724 tprintf("????" /* anti-trigraph gap */ "(");
1725 else if (!SCNO_IN_RANGE(tcp->scno))
1726 tprintf("syscall_%lu(", tcp->scno);
1728 tprintf("%s(", sysent[tcp->scno].sys_name);
1730 * " <unavailable>" will be added later by the code which
1731 * detects ptrace errors.
1736 switch (known_scno(tcp)) {
1737 #ifdef SYS_socket_subcall
1738 case SYS_socketcall:
1739 decode_subcall(tcp, SYS_socket_subcall,
1740 SYS_socket_nsubcalls, deref_style);
1743 #ifdef SYS_ipc_subcall
1745 decode_subcall(tcp, SYS_ipc_subcall,
1746 SYS_ipc_nsubcalls, shift_style);
1750 #ifdef SYS_pgrpsys_subcall
1752 decode_subcall(tcp, SYS_pgrpsys_subcall,
1753 SYS_pgrpsys_nsubcalls, shift_style);
1755 #endif /* SYS_pgrpsys_subcall */
1756 #ifdef SYS_sigcall_subcall
1758 decode_subcall(tcp, SYS_sigcall_subcall,
1759 SYS_sigcall_nsubcalls, mask_style);
1761 #endif /* SYS_sigcall_subcall */
1763 decode_subcall(tcp, SYS_msgsys_subcall,
1764 SYS_msgsys_nsubcalls, shift_style);
1767 decode_subcall(tcp, SYS_shmsys_subcall,
1768 SYS_shmsys_nsubcalls, shift_style);
1771 decode_subcall(tcp, SYS_semsys_subcall,
1772 SYS_semsys_nsubcalls, shift_style);
1775 decode_subcall(tcp, SYS_sysfs_subcall,
1776 SYS_sysfs_nsubcalls, shift_style);
1779 decode_subcall(tcp, SYS_spcall_subcall,
1780 SYS_spcall_nsubcalls, shift_style);
1782 #ifdef SYS_context_subcall
1784 decode_subcall(tcp, SYS_context_subcall,
1785 SYS_context_nsubcalls, shift_style);
1787 #endif /* SYS_context_subcall */
1788 #ifdef SYS_door_subcall
1790 decode_subcall(tcp, SYS_door_subcall,
1791 SYS_door_nsubcalls, door_style);
1793 #endif /* SYS_door_subcall */
1794 #ifdef SYS_kaio_subcall
1796 decode_subcall(tcp, SYS_kaio_subcall,
1797 SYS_kaio_nsubcalls, shift_style);
1805 decode_subcall(tcp, 0, 0, table_style);
1810 decode_subcall(tcp, SYS_semsys_subcall,
1811 SYS_semsys_nsubcalls, shift_style);
1814 decode_subcall(tcp, SYS_msgsys_subcall,
1815 SYS_msgsys_nsubcalls, shift_style);
1818 decode_subcall(tcp, SYS_shmsys_subcall,
1819 SYS_shmsys_nsubcalls, shift_style);
1824 internal_syscall(tcp);
1826 if ((SCNO_IN_RANGE(tcp->scno) &&
1827 !(qual_flags[tcp->scno] & QUAL_TRACE)) ||
1828 (tracing_paths && !pathtrace_match(tcp))) {
1829 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1833 tcp->flags &= ~TCB_FILTERED;
1835 if (cflag == CFLAG_ONLY_STATS) {
1841 tcp->flags &= ~TCB_REPRINT;
1843 if (!SCNO_IN_RANGE(tcp->scno))
1844 tprintf("syscall_%lu(", tcp->scno);
1846 tprintf("%s(", sysent[tcp->scno].sys_name);
1847 if (!SCNO_IN_RANGE(tcp->scno) ||
1848 ((qual_flags[tcp->scno] & QUAL_RAW) &&
1849 sysent[tcp->scno].sys_func != sys_exit))
1850 res = printargs(tcp);
1852 res = (*sysent[tcp->scno].sys_func)(tcp);
1854 if (fflush(tcp->outf) == EOF)
1857 tcp->flags |= TCB_INSYSCALL;
1858 /* Measure the entrance time as late as possible to avoid errors. */
1860 gettimeofday(&tcp->etime, NULL);
1865 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1866 * 1: ok, continue in trace_syscall().
1867 * other: error, trace_syscall() should print error indicator
1868 * ("????" etc) and bail out.
1871 get_syscall_result(struct tcb *tcp)
1874 # if defined(S390) || defined(S390X)
1875 # elif defined (POWERPC)
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 # elif defined (I386)
1882 if (upeek(tcp, 4*EAX, &eax) < 0)
1884 # elif defined (X86_64)
1885 if (upeek(tcp, 8*RAX, &rax) < 0)
1887 # elif defined(IA64)
1888 # define IA64_PSR_IS ((long)1 << 34)
1889 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
1890 ia32 = (psr & IA64_PSR_IS) != 0;
1891 if (upeek(tcp, PT_R8, &r8) < 0)
1893 if (upeek(tcp, PT_R10, &r10) < 0)
1895 # elif defined (ARM)
1896 /* Read complete register set in one go. */
1897 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (void *)®s) == -1)
1899 # elif defined (M68K)
1900 # elif defined (LINUX_MIPSN32)
1901 unsigned long long regs[38];
1903 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1907 # elif defined (MIPS)
1908 if (upeek(tcp, REG_A3, &a3) < 0)
1910 if (upeek(tcp, REG_V0, &r2) < 0)
1912 # elif defined (ALPHA)
1913 if (upeek(tcp, REG_A3, &a3) < 0)
1915 if (upeek(tcp, REG_R0, &r0) < 0)
1917 # elif defined (SPARC) || defined (SPARC64)
1918 /* Everything we need is in the current register set. */
1919 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1921 # elif defined(HPPA)
1923 # elif defined(SH64)
1924 # elif defined(CRISV10) || defined(CRISV32)
1925 # elif defined(TILE)
1926 # elif defined(MICROBLAZE)
1932 /* new syscall ABI returns result in R0 */
1933 if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0)
1936 /* ABI defines result returned in r9 */
1937 if (upeek(tcp, REG_GENERAL(9), (long *)&r9) < 0)
1942 # ifndef HAVE_PR_SYSCALL
1944 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1948 # endif /* FREEBSD */
1949 # endif /* !HAVE_PR_SYSCALL */
1950 #endif /* USE_PROCFS */
1955 /* Called at each syscall exit.
1957 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1958 * 1: ok, continue in trace_syscall().
1959 * other: error, trace_syscall() should print error indicator
1960 * ("????" etc) and bail out.
1963 syscall_fixup_on_sysexit(struct tcb *tcp)
1966 if (tcp->status.PR_WHY != PR_SYSEXIT) {
1967 fprintf(stderr, "syscall: missing exit\n");
1968 tcp->flags &= ~TCB_INSYSCALL;
1970 #endif /* USE_PROCFS */
1974 int scno = known_scno(tcp);
1978 * This happens when a signal handler
1979 * for a signal which interrupted a
1980 * a system call makes another system call.
1982 fprintf(stderr, "syscall: missing exit\n");
1984 tcp->flags &= ~TCB_INSYSCALL;
1990 #if defined (S390) || defined (S390X)
1991 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1993 if (syscall_mode != -ENOSYS)
1994 syscall_mode = tcp->scno;
1995 if ((tcp->flags & TCB_WAITEXECVE)
1996 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1998 * Return from execve.
1999 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2000 * flag set for the post-execve SIGTRAP to see and reset.
2004 #elif defined (POWERPC)
2005 # define SO_MASK 0x10000000
2006 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
2008 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &result) < 0)
2010 if (flags & SO_MASK)
2012 #elif defined (M68K)
2013 if (upeek(tcp, 4*PT_D0, &d0) < 0)
2016 /* Nothing required */
2018 if (upeek(tcp, PT_R0, &r0) < 0)
2020 #elif defined (HPPA)
2021 if (upeek(tcp, PT_GR28, &r28) < 0)
2024 if (upeek(tcp, PT_R10, &r10) < 0)
2026 if (upeek(tcp, PT_R8, &r8) < 0)
2028 #elif defined(CRISV10) || defined(CRISV32)
2029 if (upeek(tcp, 4*PT_R10, &r10) < 0)
2031 #elif defined(MICROBLAZE)
2032 if (upeek(tcp, 3 * 4, &r3) < 0)
2041 * Check the syscall return value register value for whether it is
2042 * a negated errno code indicating an error, or a success return value.
2045 is_negated_errno(unsigned long int val)
2047 unsigned long int max = -(long int) nerrnos;
2048 # if SUPPORTED_PERSONALITIES > 1
2049 if (personality_wordsize[current_personality] < sizeof(val)) {
2050 val = (unsigned int) val;
2051 max = (unsigned int) max;
2059 get_error(struct tcb *tcp)
2063 int check_errno = 1;
2064 if (SCNO_IN_RANGE(tcp->scno) &&
2065 sysent[tcp->scno].sys_flags & SYSCALL_NEVER_FAILS) {
2068 # if defined(S390) || defined(S390X)
2069 if (check_errno && is_negated_errno(gpr2)) {
2076 # elif defined(I386)
2077 if (check_errno && is_negated_errno(eax)) {
2084 # elif defined(X86_64)
2085 if (check_errno && is_negated_errno(rax)) {
2092 # elif defined(IA64)
2097 if (check_errno && is_negated_errno(err)) {
2105 if (check_errno && r10) {
2112 # elif defined(MIPS)
2113 if (check_errno && a3) {
2119 # elif defined(POWERPC)
2120 if (check_errno && is_negated_errno(result)) {
2125 tcp->u_rval = result;
2127 # elif defined(M68K)
2128 if (check_errno && is_negated_errno(d0)) {
2136 if (check_errno && is_negated_errno(regs.ARM_r0)) {
2138 u_error = -regs.ARM_r0;
2141 tcp->u_rval = regs.ARM_r0;
2143 # elif defined(AVR32)
2144 if (check_errno && regs.r12 && (unsigned) -regs.r12 < nerrnos) {
2146 u_error = -regs.r12;
2149 tcp->u_rval = regs.r12;
2151 # elif defined(BFIN)
2152 if (check_errno && is_negated_errno(r0)) {
2158 # elif defined(ALPHA)
2159 if (check_errno && a3) {
2166 # elif defined(SPARC)
2167 if (check_errno && regs.psr & PSR_C) {
2169 u_error = regs.u_regs[U_REG_O0];
2172 tcp->u_rval = regs.u_regs[U_REG_O0];
2174 # elif defined(SPARC64)
2175 if (check_errno && regs.tstate & 0x1100000000UL) {
2177 u_error = regs.u_regs[U_REG_O0];
2180 tcp->u_rval = regs.u_regs[U_REG_O0];
2182 # elif defined(HPPA)
2183 if (check_errno && is_negated_errno(r28)) {
2191 if (check_errno && is_negated_errno(r0)) {
2198 # elif defined(SH64)
2199 if (check_errno && is_negated_errno(r9)) {
2206 # elif defined(CRISV10) || defined(CRISV32)
2207 if (check_errno && r10 && (unsigned) -r10 < nerrnos) {
2214 # elif defined(TILE)
2216 if (upeek(tcp, PTREGS_OFFSET_REG(0), &rval) < 0)
2218 if (check_errno && rval < 0 && rval > -nerrnos) {
2225 # elif defined(MICROBLAZE)
2226 if (check_errno && is_negated_errno(r3)) {
2236 /* get error code from user struct */
2237 if (upeek(tcp, uoff(u_error), &u_error) < 0)
2239 u_error >>= 24; /* u_error is a char */
2241 /* get system call return value */
2242 if (upeek(tcp, uoff(u_rval1), &tcp->u_rval) < 0)
2247 /* Judicious guessing goes a long way. */
2248 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
2250 u_error = tcp->status.pr_reg[R_O0];
2253 tcp->u_rval = tcp->status.pr_reg[R_O0];
2257 if (tcp->status.PR_REG[EFL] & 0x1) {
2259 u_error = tcp->status.PR_REG[EAX];
2262 tcp->u_rval = tcp->status.PR_REG[EAX];
2263 # ifdef HAVE_LONG_LONG
2265 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
2266 tcp->status.PR_REG[EAX];
2271 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
2273 u_error = tcp->status.PR_REG[RAX];
2276 tcp->u_rval = tcp->status.PR_REG[RAX];
2278 # endif /* X86_64 */
2280 if (tcp->status.pr_reg[CTX_A3]) {
2282 u_error = tcp->status.pr_reg[CTX_V0];
2285 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
2290 if (regs.r_eflags & PSL_C) {
2292 u_error = regs.r_eax;
2294 tcp->u_rval = regs.r_eax;
2296 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
2298 #endif /* FREEBSD */
2299 tcp->u_error = u_error;
2304 dumpio(struct tcb *tcp)
2308 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
2310 if (!SCNO_IN_RANGE(tcp->scno))
2312 if (sysent[tcp->scno].sys_func == printargs)
2314 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
2315 if (sysent[tcp->scno].sys_func == sys_read ||
2316 sysent[tcp->scno].sys_func == sys_pread ||
2317 sysent[tcp->scno].sys_func == sys_pread64 ||
2318 sysent[tcp->scno].sys_func == sys_recv ||
2319 sysent[tcp->scno].sys_func == sys_recvfrom)
2320 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
2321 else if (sysent[tcp->scno].sys_func == sys_readv)
2322 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2325 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
2326 if (sysent[tcp->scno].sys_func == sys_write ||
2327 sysent[tcp->scno].sys_func == sys_pwrite ||
2328 sysent[tcp->scno].sys_func == sys_pwrite64 ||
2329 sysent[tcp->scno].sys_func == sys_send ||
2330 sysent[tcp->scno].sys_func == sys_sendto)
2331 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
2332 else if (sysent[tcp->scno].sys_func == sys_writev)
2333 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2339 trace_syscall_exiting(struct tcb *tcp)
2346 /* Measure the exit time as early as possible to avoid errors. */
2348 gettimeofday(&tv, NULL);
2350 res = get_syscall_result(tcp);
2354 res = syscall_fixup_on_sysexit(tcp);
2358 res = get_error(tcp);
2362 internal_syscall(tcp);
2364 if (res == 1 && filtered(tcp)) {
2368 if (tcp->flags & TCB_REPRINT) {
2370 if (!SCNO_IN_RANGE(tcp->scno))
2371 tprintf("<... syscall_%lu resumed> ", tcp->scno);
2373 tprintf("<... %s resumed> ", sysent[tcp->scno].sys_name);
2377 struct timeval t = tv;
2378 count_syscall(tcp, &t);
2379 if (cflag == CFLAG_ONLY_STATS) {
2387 tprintf("= ? <unavailable>");
2389 tcp->flags &= ~TCB_INSYSCALL;
2393 if (!SCNO_IN_RANGE(tcp->scno)
2394 || (qual_flags[tcp->scno] & QUAL_RAW))
2395 sys_res = printargs(tcp);
2397 /* FIXME: not_failing_only (IOW, option -z) is broken:
2398 * failure of syscall is known only after syscall return.
2399 * Thus we end up with something like this on, say, ENOENT:
2400 * open("doesnt_exist", O_RDONLY <unfinished ...>
2401 * {next syscall decode}
2402 * whereas the intended result is that open(...) line
2403 * is not shown at all.
2405 if (not_failing_only && tcp->u_error)
2406 goto ret; /* ignore failed syscalls */
2407 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2412 u_error = tcp->u_error;
2413 if (!SCNO_IN_RANGE(tcp->scno) ||
2414 qual_flags[tcp->scno] & QUAL_RAW) {
2416 tprintf("= -1 (errno %ld)", u_error);
2418 tprintf("= %#lx", tcp->u_rval);
2420 else if (!(sys_res & RVAL_NONE) && u_error) {
2424 tprintf("= ? ERESTARTSYS (To be restarted)");
2426 case ERESTARTNOINTR:
2427 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2429 case ERESTARTNOHAND:
2430 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2432 case ERESTART_RESTARTBLOCK:
2433 tprintf("= ? ERESTART_RESTARTBLOCK (To be restarted)");
2438 tprintf("= -1 E??? (errno %ld)", u_error);
2439 else if (u_error < nerrnos)
2440 tprintf("= -1 %s (%s)", errnoent[u_error],
2443 tprintf("= -1 ERRNO_%ld (%s)", u_error,
2447 if ((sys_res & RVAL_STR) && tcp->auxstr)
2448 tprintf(" (%s)", tcp->auxstr);
2451 if (sys_res & RVAL_NONE)
2454 switch (sys_res & RVAL_MASK) {
2456 tprintf("= %#lx", tcp->u_rval);
2459 tprintf("= %#lo", tcp->u_rval);
2462 tprintf("= %lu", tcp->u_rval);
2465 tprintf("= %ld", tcp->u_rval);
2467 #ifdef HAVE_LONG_LONG
2469 tprintf("= %#llx", tcp->u_lrval);
2472 tprintf("= %#llo", tcp->u_lrval);
2474 case RVAL_LUDECIMAL:
2475 tprintf("= %llu", tcp->u_lrval);
2478 tprintf("= %lld", tcp->u_lrval);
2483 "invalid rval format\n");
2487 if ((sys_res & RVAL_STR) && tcp->auxstr)
2488 tprintf(" (%s)", tcp->auxstr);
2491 tv_sub(&tv, &tv, &tcp->etime);
2492 tprintf(" <%ld.%06ld>",
2493 (long) tv.tv_sec, (long) tv.tv_usec);
2498 if (fflush(tcp->outf) == EOF)
2501 tcp->flags &= ~TCB_INSYSCALL;
2506 trace_syscall(struct tcb *tcp)
2508 return exiting(tcp) ?
2509 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);