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>
46 #if defined (SPARC) || defined (SPARC64)
47 # define fpq kernel_fpq
49 # define fpu kernel_fpu
52 #if defined (SPARC) || defined (SPARC64)
61 #ifndef PTRACE_PEEKUSR
62 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
64 #elif defined(HAVE_LINUX_PTRACE_H)
66 # ifdef HAVE_STRUCT_IA64_FPREG
67 # define ia64_fpreg XXX_ia64_fpreg
69 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
70 # define pt_all_user_regs XXX_pt_all_user_regs
72 #include <linux/ptrace.h>
74 # undef pt_all_user_regs
77 #if defined (LINUX) && defined (SPARC64)
79 # undef PTRACE_GETREGS
80 # define PTRACE_GETREGS PTRACE_GETREGS64
81 # undef PTRACE_SETREGS
82 # define PTRACE_SETREGS PTRACE_SETREGS64
83 #endif /* LINUX && SPARC64 */
85 #if defined(LINUX) && defined(IA64)
86 # include <asm/ptrace_offsets.h>
90 #define NR_SYSCALL_BASE 0
93 #define ERESTARTSYS 512
95 #ifndef ERESTARTNOINTR
96 #define ERESTARTNOINTR 513
98 #ifndef ERESTARTNOHAND
99 #define ERESTARTNOHAND 514 /* restart if no handler.. */
102 #define ENOIOCTLCMD 515 /* No ioctl command */
104 #ifndef ERESTART_RESTARTBLOCK
105 #define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
113 #undef NR_SYSCALL_BASE
114 #define NR_SYSCALL_BASE __NR_SYSCALL_BASE
120 /* Define these shorthand notations to simplify the syscallent files. */
121 #define TF TRACE_FILE
123 #define TN TRACE_NETWORK
124 #define TP TRACE_PROCESS
125 #define TS TRACE_SIGNAL
127 static const struct sysent sysent0[] = {
128 #include "syscallent.h"
130 static const int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
132 #if SUPPORTED_PERSONALITIES >= 2
133 static const struct sysent sysent1[] = {
134 #include "syscallent1.h"
136 static const int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
137 #endif /* SUPPORTED_PERSONALITIES >= 2 */
139 #if SUPPORTED_PERSONALITIES >= 3
140 static const struct sysent sysent2[] = {
141 #include "syscallent2.h"
143 static const int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
144 #endif /* SUPPORTED_PERSONALITIES >= 3 */
146 const struct sysent *sysent;
149 /* Now undef them since short defines cause wicked namespace pollution. */
156 static const char *const errnoent0[] = {
157 #include "errnoent.h"
159 static const int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
161 #if SUPPORTED_PERSONALITIES >= 2
162 static const char *const errnoent1[] = {
163 #include "errnoent1.h"
165 static const int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
166 #endif /* SUPPORTED_PERSONALITIES >= 2 */
168 #if SUPPORTED_PERSONALITIES >= 3
169 static const char *const errnoent2[] = {
170 #include "errnoent2.h"
172 static const int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
173 #endif /* SUPPORTED_PERSONALITIES >= 3 */
175 const char *const *errnoent;
178 int current_personality;
181 set_personality(personality)
184 switch (personality) {
186 errnoent = errnoent0;
189 nsyscalls = nsyscalls0;
190 ioctlent = ioctlent0;
191 nioctlents = nioctlents0;
192 signalent = signalent0;
193 nsignals = nsignals0;
196 #if SUPPORTED_PERSONALITIES >= 2
198 errnoent = errnoent1;
201 nsyscalls = nsyscalls1;
202 ioctlent = ioctlent1;
203 nioctlents = nioctlents1;
204 signalent = signalent1;
205 nsignals = nsignals1;
207 #endif /* SUPPORTED_PERSONALITIES >= 2 */
209 #if SUPPORTED_PERSONALITIES >= 3
211 errnoent = errnoent2;
214 nsyscalls = nsyscalls2;
215 ioctlent = ioctlent2;
216 nioctlents = nioctlents2;
217 signalent = signalent2;
218 nsignals = nsignals2;
220 #endif /* SUPPORTED_PERSONALITIES >= 3 */
226 current_personality = personality;
230 int qual_flags[MAX_QUALS];
238 static struct call_counts *counts;
240 static struct timeval shortest = { 1000000, 0 };
242 static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
244 static const struct qual_options {
250 { QUAL_TRACE, "trace", qual_syscall, "system call" },
251 { QUAL_TRACE, "t", qual_syscall, "system call" },
252 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
253 { QUAL_ABBREV, "a", qual_syscall, "system call" },
254 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
255 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
256 { QUAL_RAW, "raw", qual_syscall, "system call" },
257 { QUAL_RAW, "x", qual_syscall, "system call" },
258 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
259 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
260 { QUAL_SIGNAL, "s", qual_signal, "signal" },
261 { QUAL_FAULT, "fault", qual_fault, "fault" },
262 { QUAL_FAULT, "faults", qual_fault, "fault" },
263 { QUAL_FAULT, "m", qual_fault, "fault" },
264 { QUAL_READ, "read", qual_desc, "descriptor" },
265 { QUAL_READ, "reads", qual_desc, "descriptor" },
266 { QUAL_READ, "r", qual_desc, "descriptor" },
267 { QUAL_WRITE, "write", qual_desc, "descriptor" },
268 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
269 { QUAL_WRITE, "w", qual_desc, "descriptor" },
270 { 0, NULL, NULL, NULL },
274 qualify_one(n, opt, not)
276 const struct qual_options *opt;
280 qual_flags[n] &= ~opt->bitflag;
282 qual_flags[n] |= opt->bitflag;
286 qual_syscall(s, opt, not)
288 const struct qual_options *opt;
294 for (i = 0; i < nsyscalls; i++) {
295 if (strcmp(s, sysent[i].sys_name) == 0) {
296 qualify_one(i, opt, not);
304 qual_signal(s, opt, not)
306 const struct qual_options *opt;
312 if (s && *s && isdigit((unsigned char)*s)) {
314 if (signo < 0 || signo >= MAX_QUALS)
316 qualify_one(signo, opt, not);
319 if (strlen(s) >= sizeof buf)
323 for (i = 0; s[i]; i++)
324 s[i] = toupper((unsigned char)(s[i]));
325 if (strncmp(s, "SIG", 3) == 0)
327 for (i = 0; i <= NSIG; i++)
328 if (strcmp(s, signame(i) + 3) == 0) {
329 qualify_one(i, opt, not);
336 qual_fault(s, opt, not)
338 const struct qual_options *opt;
345 qual_desc(s, opt, not)
347 const struct qual_options *opt;
350 if (s && *s && isdigit((unsigned char)*s)) {
352 if (desc < 0 || desc >= MAX_QUALS)
354 qualify_one(desc, opt, not);
364 if (strcmp(s, "file") == 0)
366 if (strcmp(s, "ipc") == 0)
368 if (strcmp(s, "network") == 0)
369 return TRACE_NETWORK;
370 if (strcmp(s, "process") == 0)
371 return TRACE_PROCESS;
372 if (strcmp(s, "signal") == 0)
381 const struct qual_options *opt;
386 opt = &qual_options[0];
387 for (i = 0; (p = qual_options[i].option_name); i++) {
389 if (strncmp(s, p, n) == 0 && s[n] == '=') {
390 opt = &qual_options[i];
400 if (strcmp(s, "none") == 0) {
404 if (strcmp(s, "all") == 0) {
405 for (i = 0; i < MAX_QUALS; i++) {
407 qual_flags[i] &= ~opt->bitflag;
409 qual_flags[i] |= opt->bitflag;
413 for (i = 0; i < MAX_QUALS; i++) {
415 qual_flags[i] |= opt->bitflag;
417 qual_flags[i] &= ~opt->bitflag;
419 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
420 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
421 for (i = 0; i < MAX_QUALS; i++) {
422 if (sysent[i].sys_flags & n) {
424 qual_flags[i] &= ~opt->bitflag;
426 qual_flags[i] |= opt->bitflag;
431 if (opt->qualify(p, opt, not)) {
432 fprintf(stderr, "strace: invalid %s `%s'\n",
433 opt->argument_name, p);
446 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
448 switch (tcp->scno + NR_SYSCALL_BASE) {
453 #if defined SYS_pread && SYS_pread64 != SYS_pread
462 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
463 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
469 #if defined SYS_pwrite && SYS_pwrite64 != SYS_pwrite
478 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
479 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
483 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
484 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
490 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
491 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
498 enum subcall_style { shift_style, deref_style, mask_style, door_style };
500 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
508 const struct subcall subcalls_table[] = {
509 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
511 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
513 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
515 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
519 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
521 const int socket_map [] = {
524 /* SYS_CONNECT */ 98,
525 /* SYS_LISTEN */ 106,
527 /* SYS_GETSOCKNAME */ 150,
528 /* SYS_GETPEERNAME */ 141,
529 /* SYS_SOCKETPAIR */ 135,
532 /* SYS_SENDTO */ 133,
533 /* SYS_RECVFROM */ 125,
534 /* SYS_SHUTDOWN */ 134,
535 /* SYS_SETSOCKOPT */ 105,
536 /* SYS_GETSOCKOPT */ 118,
537 /* SYS_SENDMSG */ 114,
538 /* SYS_RECVMSG */ 113
542 sparc_socket_decode (tcp)
548 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
551 tcp->scno = socket_map [tcp->u_arg [0]-1];
552 n = tcp->u_nargs = sysent [tcp->scno].nargs;
553 addr = tcp->u_arg [1];
554 for (i = 0; i < n; i++){
556 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
558 tcp->u_arg [i] = arg;
559 addr += sizeof (arg);
564 decode_subcall(tcp, subcall, nsubcalls, style)
568 enum subcall_style style;
570 long addr, mask, arg;
575 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
577 tcp->scno = subcall + tcp->u_arg[0];
578 if (sysent[tcp->scno].nargs != -1)
579 tcp->u_nargs = sysent[tcp->scno].nargs;
582 for (i = 0; i < tcp->u_nargs; i++)
583 tcp->u_arg[i] = tcp->u_arg[i + 1];
586 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
588 tcp->scno = subcall + tcp->u_arg[0];
589 addr = tcp->u_arg[1];
590 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
591 if (umove(tcp, addr, &arg) < 0)
596 tcp->u_nargs = sysent[tcp->scno].nargs;
599 mask = (tcp->u_arg[0] >> 8) & 0xff;
600 for (i = 0; mask; i++)
604 tcp->u_arg[0] &= 0xff;
605 tcp->scno = subcall + i;
606 if (sysent[tcp->scno].nargs != -1)
607 tcp->u_nargs = sysent[tcp->scno].nargs;
611 * Oh, yuck. The call code is the *sixth* argument.
612 * (don't you mean the *last* argument? - JH)
614 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
616 tcp->scno = subcall + tcp->u_arg[5];
617 if (sysent[tcp->scno].nargs != -1)
618 tcp->u_nargs = sysent[tcp->scno].nargs;
624 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
625 if (subcalls_table[i].call == tcp->scno) break;
626 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
627 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
628 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
629 for (i = 0; i < tcp->u_nargs; i++)
630 tcp->u_arg[i] = tcp->u_arg[i + 1];
638 struct tcb *tcp_last = NULL;
641 internal_syscall(tcp)
645 * We must always trace a few critical system calls in order to
646 * correctly support following forks in the presence of tracing
649 switch (tcp->scno + NR_SYSCALL_BASE) {
710 internal_wait(tcp, 2);
714 internal_wait(tcp, 3);
724 #ifdef __NR_exit_group
725 case __NR_exit_group:
728 case 252: /* IA-32 __NR_exit_group */
743 #elif defined (POWERPC)
744 static long result,flags;
748 static struct pt_regs regs;
749 #elif defined (ALPHA)
752 #elif defined (SPARC) || defined (SPARC64)
753 static struct regs regs;
754 static unsigned long trap;
758 #elif defined(S390) || defined(S390X)
761 static long syscall_mode;
768 #elif defined(X86_64)
786 #if defined(S390) || defined(S390X)
787 if (tcp->flags & TCB_WAITEXECVE) {
789 * When the execve system call completes successfully, the
790 * new process still has -ENOSYS (old style) or __NR_execve
791 * (new style) in gpr2. We cannot recover the scno again
792 * by disassembly, because the image that executed the
793 * syscall is gone now. Fortunately, we don't want it. We
794 * leave the flag set so that syscall_fixup can fake the
797 if (tcp->flags & TCB_INSYSCALL)
800 * This is the SIGTRAP after execve. We cannot try to read
801 * the system call here either.
803 tcp->flags &= ~TCB_WAITEXECVE;
807 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
810 if (syscall_mode != -ENOSYS) {
812 * Since kernel version 2.5.44 the scno gets passed in gpr2.
817 * Old style of "passing" the scno via the SVC instruction.
820 long opcode, offset_reg, tmp;
822 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
823 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
824 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
825 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
827 if (upeek(pid, PT_PSWADDR, &pc) < 0)
830 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
832 perror("peektext(pc-oneword)");
837 * We have to check if the SVC got executed directly or via an
838 * EXECUTE instruction. In case of EXECUTE it is necessary to do
839 * instruction decoding to derive the system call number.
840 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
841 * so that this doesn't work if a SVC opcode is part of an EXECUTE
842 * opcode. Since there is no way to find out the opcode size this
843 * is the best we can do...
846 if ((opcode & 0xff00) == 0x0a00) {
848 scno = opcode & 0xff;
851 /* SVC got executed by EXECUTE instruction */
854 * Do instruction decoding of EXECUTE. If you really want to
855 * understand this, read the Principles of Operations.
857 svc_addr = (void *) (opcode & 0xfff);
860 offset_reg = (opcode & 0x000f0000) >> 16;
861 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
866 offset_reg = (opcode & 0x0000f000) >> 12;
867 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
871 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
880 offset_reg = (opcode & 0x00f00000) >> 20;
881 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
884 scno = (scno | tmp) & 0xff;
887 #elif defined (POWERPC)
888 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
890 if (!(tcp->flags & TCB_INSYSCALL)) {
891 /* Check if we return from execve. */
892 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
893 tcp->flags &= ~TCB_WAITEXECVE;
898 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
900 #elif defined (X86_64)
901 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
904 if (!(tcp->flags & TCB_INSYSCALL)) {
905 static int currpers=-1;
908 /* Check CS register value. On x86-64 linux it is:
909 * 0x33 for long mode (64 bit)
910 * 0x23 for compatibility mode (32 bit)
911 * It takes only one ptrace and thus doesn't need
914 if (upeek(pid, 8*CS, &val) < 0)
918 case 0x23: currpers = 1; break;
919 case 0x33: currpers = 0; break;
921 fprintf(stderr, "Unknown value CS=0x%02X while "
922 "detecting personality of process "
923 "PID=%d\n", (int)val, pid);
924 currpers = current_personality;
928 /* This version analyzes the opcode of a syscall instruction.
929 * (int 0x80 on i386 vs. syscall on x86-64)
930 * It works, but is too complicated.
932 unsigned long val, rip, i;
934 if(upeek(pid, 8*RIP, &rip)<0)
935 perror("upeek(RIP)");
937 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
941 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
943 printf("ptrace_peektext failed: %s\n",
945 switch (call & 0xffff)
947 /* x86-64: syscall = 0x0f 0x05 */
948 case 0x050f: currpers = 0; break;
949 /* i386: int 0x80 = 0xcd 0x80 */
950 case 0x80cd: currpers = 1; break;
952 currpers = current_personality;
954 "Unknown syscall opcode (0x%04X) while "
955 "detecting personality of process "
956 "PID=%d\n", (int)call, pid);
960 if(currpers != current_personality)
962 char *names[]={"64 bit", "32 bit"};
963 set_personality(currpers);
964 printf("[ Process PID=%d runs in %s mode. ]\n",
965 pid, names[current_personality]);
969 # define IA64_PSR_IS ((long)1 << 34)
970 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
971 ia32 = (psr & IA64_PSR_IS) != 0;
972 if (!(tcp->flags & TCB_INSYSCALL)) {
974 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
977 if (upeek (pid, PT_R15, &scno) < 0)
980 /* Check if we return from execve. */
981 if (tcp->flags & TCB_WAITEXECVE) {
982 tcp->flags &= ~TCB_WAITEXECVE;
986 /* syscall in progress */
987 if (upeek (pid, PT_R8, &r8) < 0)
989 if (upeek (pid, PT_R10, &r10) < 0)
994 * Read complete register set in one go.
996 if (ptrace(PTRACE_GETREGS, pid, NULL, (void *)®s) == -1)
1000 * We only need to grab the syscall number on syscall entry.
1002 if (regs.ARM_ip == 0) {
1004 * Note: we only deal with only 32-bit CPUs here.
1006 if (regs.ARM_cpsr & 0x20) {
1008 * Get the Thumb-mode system call number
1013 * Get the ARM-mode system call number
1016 scno = ptrace(PTRACE_PEEKTEXT, pid, (void *)(regs.ARM_pc - 4), NULL);
1020 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
1021 tcp->flags &= ~TCB_WAITEXECVE;
1025 if ((scno & 0x0ff00000) != 0x0f900000) {
1026 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
1032 * Fixup the syscall number
1037 if (tcp->flags & TCB_INSYSCALL) {
1038 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
1039 tcp->flags &= ~TCB_INSYSCALL;
1042 if (!(tcp->flags & TCB_INSYSCALL)) {
1043 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1044 tcp->flags |= TCB_INSYSCALL;
1047 #elif defined (M68K)
1048 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
1050 #elif defined (MIPS)
1051 if (upeek(pid, REG_A3, &a3) < 0)
1054 if(!(tcp->flags & TCB_INSYSCALL)) {
1055 if (upeek(pid, REG_V0, &scno) < 0)
1058 if (scno < 0 || scno > nsyscalls) {
1059 if(a3 == 0 || a3 == -1) {
1061 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
1066 if (upeek(pid, REG_V0, &r2) < 0)
1069 #elif defined (ALPHA)
1070 if (upeek(pid, REG_A3, &a3) < 0)
1073 if (!(tcp->flags & TCB_INSYSCALL)) {
1074 if (upeek(pid, REG_R0, &scno) < 0)
1077 /* Check if we return from execve. */
1078 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1079 tcp->flags &= ~TCB_WAITEXECVE;
1084 * Do some sanity checks to figure out if it's
1085 * really a syscall entry
1087 if (scno < 0 || scno > nsyscalls) {
1088 if (a3 == 0 || a3 == -1) {
1090 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
1096 if (upeek(pid, REG_R0, &r0) < 0)
1099 #elif defined (SPARC) || defined (SPARC64)
1100 /* Everything we need is in the current register set. */
1101 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
1104 /* If we are entering, then disassemble the syscall trap. */
1105 if (!(tcp->flags & TCB_INSYSCALL)) {
1106 /* Retrieve the syscall trap instruction. */
1108 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
1109 #if defined(SPARC64)
1115 /* Disassemble the trap to see what personality to use. */
1118 /* Linux/SPARC syscall trap. */
1122 /* Linux/SPARC64 syscall trap. */
1126 /* SunOS syscall trap. (pers 1) */
1127 fprintf(stderr,"syscall: SunOS no support\n");
1130 /* Solaris 2.x syscall trap. (per 2) */
1134 /* NetBSD/FreeBSD syscall trap. */
1135 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1138 /* Solaris 2.x gettimeofday */
1142 /* Unknown syscall trap. */
1143 if(tcp->flags & TCB_WAITEXECVE) {
1144 tcp->flags &= ~TCB_WAITEXECVE;
1147 #if defined (SPARC64)
1148 fprintf(stderr,"syscall: unknown syscall trap %08lx %016lx\n", trap, regs.r_tpc);
1150 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
1155 /* Extract the system call number from the registers. */
1156 if (trap == 0x91d02027)
1162 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
1166 if (upeek(pid, PT_GR20, &scno) < 0)
1168 if (!(tcp->flags & TCB_INSYSCALL)) {
1169 /* Check if we return from execve. */
1170 if ((tcp->flags & TCB_WAITEXECVE)) {
1171 tcp->flags &= ~TCB_WAITEXECVE;
1177 * In the new syscall ABI, the system call number is in R3.
1179 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1183 /* Odd as it may seem, a glibc bug has been known to cause
1184 glibc to issue bogus negative syscall numbers. So for
1185 our purposes, make strace print what it *should* have been */
1186 long correct_scno = (scno & 0xff);
1189 "Detected glibc bug: bogus system call number = %ld, "
1190 "correcting to %ld\n",
1193 scno = correct_scno;
1197 if (!(tcp->flags & TCB_INSYSCALL)) {
1198 /* Check if we return from execve. */
1199 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1200 tcp->flags &= ~TCB_WAITEXECVE;
1205 if (upeek(pid, REG_SYSCALL, &scno) < 0)
1209 if (!(tcp->flags & TCB_INSYSCALL)) {
1210 /* Check if we return from execve. */
1211 if (tcp->flags & TCB_WAITEXECVE) {
1212 tcp->flags &= ~TCB_WAITEXECVE;
1219 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1222 /* new syscall ABI returns result in R0 */
1223 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1226 /* ABI defines result returned in r9 */
1227 if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
1232 #ifdef HAVE_PR_SYSCALL
1233 scno = tcp->status.PR_SYSCALL;
1234 #else /* !HAVE_PR_SYSCALL */
1236 scno = tcp->status.PR_WHAT;
1238 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1242 switch (regs.r_eax) {
1245 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1251 #endif /* FREEBSD */
1252 #endif /* !HAVE_PR_SYSCALL */
1253 #endif /* USE_PROCFS */
1254 if (!(tcp->flags & TCB_INSYSCALL))
1266 #else /* USE_PROCFS */
1267 int scno = tcp->scno;
1269 if (!(tcp->flags & TCB_INSYSCALL)) {
1270 if (tcp->status.PR_WHY != PR_SYSENTRY) {
1274 || scno == SYS_vfork
1275 #endif /* SYS_vfork */
1277 || scno == SYS_fork1
1278 #endif /* SYS_fork1 */
1280 || scno == SYS_forkall
1281 #endif /* SYS_forkall */
1283 || scno == SYS_rfork1
1284 #endif /* SYS_fork1 */
1286 || scno == SYS_rforkall
1287 #endif /* SYS_rforkall */
1289 /* We are returning in the child, fake it. */
1290 tcp->status.PR_WHY = PR_SYSENTRY;
1292 tcp->status.PR_WHY = PR_SYSEXIT;
1295 fprintf(stderr, "syscall: missing entry\n");
1296 tcp->flags |= TCB_INSYSCALL;
1301 if (tcp->status.PR_WHY != PR_SYSEXIT) {
1302 fprintf(stderr, "syscall: missing exit\n");
1303 tcp->flags &= ~TCB_INSYSCALL;
1306 #endif /* USE_PROCFS */
1308 if (!(tcp->flags & TCB_INSYSCALL)) {
1310 fprintf(stderr, "syscall: missing entry\n");
1311 tcp->flags |= TCB_INSYSCALL;
1318 * This happens when a signal handler
1319 * for a signal which interrupted a
1320 * a system call makes another system call.
1322 fprintf(stderr, "syscall: missing exit\n");
1324 tcp->flags &= ~TCB_INSYSCALL;
1330 if (upeek(pid, 4*EAX, &eax) < 0)
1332 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1334 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1337 #elif defined (X86_64)
1338 if (upeek(pid, 8*RAX, &rax) < 0)
1340 if (current_personality == 1)
1341 rax = (long int)(int)rax; /* sign extend from 32 bits */
1342 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1344 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1347 #elif defined (S390) || defined (S390X)
1348 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1350 if (syscall_mode != -ENOSYS)
1351 syscall_mode = tcp->scno;
1352 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
1354 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1357 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1358 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1359 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1361 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1362 * flag set for the post-execve SIGTRAP to see and reset.
1366 #elif defined (POWERPC)
1367 # define SO_MASK 0x10000000
1368 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1370 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
1372 if (flags & SO_MASK)
1374 #elif defined (M68K)
1375 if (upeek(pid, 4*PT_D0, &d0) < 0)
1377 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1379 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1386 #elif defined (HPPA)
1387 if (upeek(pid, PT_GR28, &r28) < 0)
1390 if (upeek(pid, PT_R10, &r10) < 0)
1392 if (upeek(pid, PT_R8, &r8) < 0)
1394 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1396 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1410 #if defined(S390) || defined(S390X)
1411 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1419 #else /* !S390 && !S390X */
1421 if (eax < 0 && -eax < nerrnos) {
1431 if (rax < 0 && -rax < nerrnos) {
1445 if (err < 0 && -err < nerrnos) {
1473 if (result && (unsigned long) -result < nerrnos) {
1478 tcp->u_rval = result;
1481 #else /* !POWERPC */
1483 if (d0 && (unsigned) -d0 < nerrnos) {
1493 if (regs.ARM_r0 && (unsigned) -regs.ARM_r0 < nerrnos) {
1495 u_error = -regs.ARM_r0;
1498 tcp->u_rval = regs.ARM_r0;
1513 if (regs.r_psr & PSR_C) {
1515 u_error = regs.r_o0;
1518 tcp->u_rval = regs.r_o0;
1523 if (regs.r_tstate & 0x1100000000UL) {
1525 u_error = regs.r_o0;
1528 tcp->u_rval = regs.r_o0;
1531 #else /* !SPARC64 */
1533 if (r28 && (unsigned) -r28 < nerrnos) {
1543 /* interpret R0 as return value or error number */
1544 if (r0 && (unsigned) -r0 < nerrnos) {
1554 /* interpret result as return value or error number */
1555 if (r9 && (unsigned) -r9 < nerrnos) {
1567 #endif /* SPARC64 */
1571 #endif /* POWERPC */
1576 #endif /* S390 || S390X */
1579 /* get error code from user struct */
1580 if (upeek(pid, uoff(u_error), &u_error) < 0)
1582 u_error >>= 24; /* u_error is a char */
1584 /* get system call return value */
1585 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1590 /* Judicious guessing goes a long way. */
1591 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1593 u_error = tcp->status.pr_reg[R_O0];
1596 tcp->u_rval = tcp->status.pr_reg[R_O0];
1601 /* Wanna know how to kill an hour single-stepping? */
1602 if (tcp->status.PR_REG[EFL] & 0x1) {
1604 u_error = tcp->status.PR_REG[EAX];
1607 tcp->u_rval = tcp->status.PR_REG[EAX];
1608 #ifdef HAVE_LONG_LONG
1610 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1611 tcp->status.PR_REG[EAX];
1617 /* Wanna know how to kill an hour single-stepping? */
1618 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1620 u_error = tcp->status.PR_REG[RAX];
1623 tcp->u_rval = tcp->status.PR_REG[RAX];
1628 if (tcp->status.pr_reg[CTX_A3]) {
1630 u_error = tcp->status.pr_reg[CTX_V0];
1633 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1639 if (regs.r_eflags & PSL_C) {
1641 u_error = regs.r_eax;
1643 tcp->u_rval = regs.r_eax;
1645 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1648 #endif /* FREEBSD */
1649 tcp->u_error = u_error;
1654 force_result(tcp, error, rval)
1660 #if defined(S390) || defined(S390X)
1661 gpr2 = error ? -error : rval;
1662 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1664 #else /* !S390 && !S390X */
1666 eax = error ? -error : rval;
1667 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1671 rax = error ? -error : rval;
1672 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 8), rax) < 0)
1677 r8 = error ? -error : rval;
1678 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1690 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1691 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1704 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1705 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1709 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1719 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1720 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
1722 #else /* !POWERPC */
1724 d0 = error ? -error : rval;
1725 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1729 regs.ARM_r0 = error ? -error : rval;
1730 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), regs.ARM_r0) < 0)
1742 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1743 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1747 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1750 regs.r_psr |= PSR_C;
1754 regs.r_psr &= ~PSR_C;
1757 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1761 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1764 regs.r_tstate |= 0x1100000000UL;
1768 regs.r_tstate &= ~0x1100000000UL;
1771 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1773 #else /* !SPARC64 */
1775 r28 = error ? -error : rval;
1776 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1780 r0 = error ? -error : rval;
1781 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1785 r9 = error ? -error : rval;
1786 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1792 #endif /* SPARC64 */
1796 #endif /* POWERPC */
1801 #endif /* S390 || S390X */
1804 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1806 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1814 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1819 regs.r_eflags |= PSL_C;
1823 regs.r_eflags &= ~PSL_C;
1826 if (pwrite(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1830 #endif /* FREEBSD */
1832 /* All branches reach here on success (only). */
1833 tcp->u_error = error;
1838 int syscall_enter(tcp)
1843 #endif /* !USE_PROCFS */
1845 #if defined(S390) || defined(S390X)
1848 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1849 tcp->u_nargs = sysent[tcp->scno].nargs;
1851 tcp->u_nargs = MAX_ARGS;
1852 for (i = 0; i < tcp->u_nargs; i++) {
1853 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
1857 #elif defined (ALPHA)
1860 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1861 tcp->u_nargs = sysent[tcp->scno].nargs;
1863 tcp->u_nargs = MAX_ARGS;
1864 for (i = 0; i < tcp->u_nargs; i++) {
1865 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1866 * for scno somewhere above here!
1868 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1872 #elif defined (IA64)
1875 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1876 /* be backwards compatible with kernel < 2.4.4... */
1878 # define PT_RBS_END PT_AR_BSP
1881 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
1883 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1886 sof = (cfm >> 0) & 0x7f;
1887 sol = (cfm >> 7) & 0x7f;
1888 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1890 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1891 && sysent[tcp->scno].nargs != -1)
1892 tcp->u_nargs = sysent[tcp->scno].nargs;
1894 tcp->u_nargs = MAX_ARGS;
1895 for (i = 0; i < tcp->u_nargs; ++i) {
1896 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1897 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1903 if (/* EBX = out0 */
1904 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1906 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1908 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1910 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1912 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1914 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1917 for (i = 0; i < 6; ++i)
1918 /* truncate away IVE sign-extension */
1919 tcp->u_arg[i] &= 0xffffffff;
1921 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1922 && sysent[tcp->scno].nargs != -1)
1923 tcp->u_nargs = sysent[tcp->scno].nargs;
1928 #elif defined (MIPS)
1933 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1934 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1936 nargs = tcp->u_nargs = MAX_ARGS;
1938 if(upeek(pid, REG_SP, &sp) < 0)
1940 for(i = 0; i < 4; i++) {
1941 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1944 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1945 (char *)(tcp->u_arg + 4));
1947 for(i = 0; i < nargs; i++) {
1948 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1953 #elif defined (POWERPC)
1955 #define PT_ORIG_R3 34
1959 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1960 tcp->u_nargs = sysent[tcp->scno].nargs;
1962 tcp->u_nargs = MAX_ARGS;
1963 for (i = 0; i < tcp->u_nargs; i++) {
1964 if (upeek(pid, (i==0) ?
1965 (sizeof(unsigned long)*PT_ORIG_R3) :
1966 ((i+PT_R3)*sizeof(unsigned long)),
1967 &tcp->u_arg[i]) < 0)
1971 #elif defined (SPARC) || defined (SPARC64)
1975 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1976 tcp->u_nargs = sysent[tcp->scno].nargs;
1978 tcp->u_nargs = MAX_ARGS;
1979 for (i = 0; i < tcp->u_nargs; i++)
1980 tcp->u_arg[i] = *((®s.r_o0) + i);
1982 #elif defined (HPPA)
1986 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1987 tcp->u_nargs = sysent[tcp->scno].nargs;
1989 tcp->u_nargs = MAX_ARGS;
1990 for (i = 0; i < tcp->u_nargs; i++) {
1991 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1999 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2000 tcp->u_nargs = sysent[tcp->scno].nargs;
2002 tcp->u_nargs = MAX_ARGS;
2003 for (i = 0; i < tcp->u_nargs; i++)
2004 tcp->u_arg[i] = regs.uregs[i];
2009 static int syscall_regs[] = {
2010 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
2011 REG_REG0, REG_REG0+1, REG_REG0+2
2014 tcp->u_nargs = sysent[tcp->scno].nargs;
2015 for (i = 0; i < tcp->u_nargs; i++) {
2016 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
2023 /* Registers used by SH5 Linux system calls for parameters */
2024 static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
2027 * TODO: should also check that the number of arguments encoded
2028 * in the trap number matches the number strace expects.
2031 assert(sysent[tcp->scno].nargs <
2032 sizeof(syscall_regs)/sizeof(syscall_regs[0]));
2035 tcp->u_nargs = sysent[tcp->scno].nargs;
2036 for (i = 0; i < tcp->u_nargs; i++) {
2037 if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
2042 #elif defined(X86_64)
2045 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
2046 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
2047 {RBX,RCX,RDX,RSI,RDI,RBP} /* i386 ABI */
2050 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2051 tcp->u_nargs = sysent[tcp->scno].nargs;
2053 tcp->u_nargs = MAX_ARGS;
2054 for (i = 0; i < tcp->u_nargs; i++) {
2055 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
2059 #else /* Other architecture (like i386) (32bits specific) */
2062 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2063 tcp->u_nargs = sysent[tcp->scno].nargs;
2065 tcp->u_nargs = MAX_ARGS;
2066 for (i = 0; i < tcp->u_nargs; i++) {
2067 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
2076 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2077 tcp->u_nargs = sysent[tcp->scno].nargs;
2079 tcp->u_nargs = MAX_ARGS;
2080 for (i = 0; i < tcp->u_nargs; i++) {
2083 if (upeek(pid, uoff(u_arg[0]) +
2084 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
2092 * SGI is broken: even though it has pr_sysarg, it doesn't
2093 * set them on system call entry. Get a clue.
2095 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2096 tcp->u_nargs = sysent[tcp->scno].nargs;
2098 tcp->u_nargs = tcp->status.pr_nsysarg;
2099 if (tcp->u_nargs > 4) {
2100 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2101 4*sizeof(tcp->u_arg[0]));
2102 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
2103 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
2106 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2107 tcp->u_nargs*sizeof(tcp->u_arg[0]));
2111 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
2113 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2114 tcp->u_nargs = sysent[tcp->scno].nargs;
2116 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
2117 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2118 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2119 #elif defined (HAVE_PR_SYSCALL)
2120 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2121 tcp->u_nargs = sysent[tcp->scno].nargs;
2123 tcp->u_nargs = tcp->status.pr_nsysarg;
2126 for (i = 0; i < tcp->u_nargs; i++)
2127 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
2129 #elif defined (I386)
2130 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2131 tcp->u_nargs = sysent[tcp->scno].nargs;
2134 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2135 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2137 I DONT KNOW WHAT TO DO
2138 #endif /* !HAVE_PR_SYSCALL */
2141 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2142 sysent[tcp->scno].nargs > tcp->status.val)
2143 tcp->u_nargs = sysent[tcp->scno].nargs;
2145 tcp->u_nargs = tcp->status.val;
2146 if (tcp->u_nargs < 0)
2148 if (tcp->u_nargs > MAX_ARGS)
2149 tcp->u_nargs = MAX_ARGS;
2150 switch(regs.r_eax) {
2152 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2153 regs.r_esp + sizeof(int) + sizeof(quad_t));
2156 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2157 regs.r_esp + 2 * sizeof(int));
2160 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2161 regs.r_esp + sizeof(int));
2164 #endif /* FREEBSD */
2176 /* Measure the exit time as early as possible to avoid errors. */
2177 if (dtime && (tcp->flags & TCB_INSYSCALL))
2178 gettimeofday(&tv, NULL);
2180 res = get_scno(tcp);
2184 res = syscall_fixup(tcp);
2188 if (tcp->flags & TCB_INSYSCALL) {
2190 res = get_error(tcp);
2194 internal_syscall(tcp);
2195 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2196 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2197 tcp->flags &= ~TCB_INSYSCALL;
2201 if (tcp->flags & TCB_REPRINT) {
2204 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2205 tprintf("syscall_%lu", tcp->scno);
2207 tprintf("%s", sysent[tcp->scno].sys_name);
2208 tprintf(" resumed> ");
2211 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
2212 if (counts == NULL) {
2213 counts = calloc(sizeof *counts, nsyscalls);
2214 if (counts == NULL) {
2216 strace: out of memory for call counts\n");
2221 counts[tcp->scno].calls++;
2223 counts[tcp->scno].errors++;
2224 tv_sub(&tv, &tv, &tcp->etime);
2226 if (tv_cmp(&tv, &tcp->dtime) > 0) {
2227 static struct timeval one_tick;
2228 if (one_tick.tv_usec == 0) {
2229 /* Initialize it. */
2230 struct itimerval it;
2231 memset(&it, 0, sizeof it);
2232 it.it_interval.tv_usec = 1;
2233 setitimer(ITIMER_REAL, &it, NULL);
2234 getitimer(ITIMER_REAL, &it);
2235 one_tick = it.it_interval;
2238 if (tv_nz(&tcp->dtime))
2240 else if (tv_cmp(&tv, &one_tick) > 0) {
2241 if (tv_cmp(&shortest, &one_tick) < 0)
2248 if (tv_cmp(&tv, &shortest) < 0)
2250 tv_add(&counts[tcp->scno].time,
2251 &counts[tcp->scno].time, &tv);
2252 tcp->flags &= ~TCB_INSYSCALL;
2256 if (tcp->scno >= nsyscalls || tcp->scno < 0
2257 || (qual_flags[tcp->scno] & QUAL_RAW))
2258 sys_res = printargs(tcp);
2260 if (not_failing_only && tcp->u_error)
2261 return 0; /* ignore failed syscalls */
2262 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2264 u_error = tcp->u_error;
2267 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2268 qual_flags[tcp->scno] & QUAL_RAW) {
2270 tprintf("= -1 (errno %ld)", u_error);
2272 tprintf("= %#lx", tcp->u_rval);
2274 else if (!(sys_res & RVAL_NONE) && u_error) {
2278 tprintf("= ? ERESTARTSYS (To be restarted)");
2280 case ERESTARTNOINTR:
2281 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2283 case ERESTARTNOHAND:
2284 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2286 case ERESTART_RESTARTBLOCK:
2287 tprintf("= ? ERESTART_RESTARTBLOCK (To be restarted)");
2293 tprintf("E??? (errno %ld)", u_error);
2294 else if (u_error < nerrnos)
2295 tprintf("%s (%s)", errnoent[u_error],
2298 tprintf("ERRNO_%ld (%s)", u_error,
2304 if (sys_res & RVAL_NONE)
2307 switch (sys_res & RVAL_MASK) {
2309 tprintf("= %#lx", tcp->u_rval);
2312 tprintf("= %#lo", tcp->u_rval);
2315 tprintf("= %lu", tcp->u_rval);
2318 tprintf("= %ld", tcp->u_rval);
2320 #ifdef HAVE_LONG_LONG
2322 tprintf("= %#llx", tcp->u_lrval);
2325 tprintf("= %#llo", tcp->u_lrval);
2327 case RVAL_LUDECIMAL:
2328 tprintf("= %llu", tcp->u_lrval);
2331 tprintf("= %lld", tcp->u_lrval);
2336 "invalid rval format\n");
2340 if ((sys_res & RVAL_STR) && tcp->auxstr)
2341 tprintf(" (%s)", tcp->auxstr);
2344 tv_sub(&tv, &tv, &tcp->etime);
2345 tprintf(" <%ld.%06ld>",
2346 (long) tv.tv_sec, (long) tv.tv_usec);
2351 if (fflush(tcp->outf) == EOF)
2353 tcp->flags &= ~TCB_INSYSCALL;
2357 /* Entering system call */
2358 res = syscall_enter(tcp);
2362 switch (tcp->scno + NR_SYSCALL_BASE) {
2364 #if !defined (ALPHA) && !defined(SPARC) && !defined(SPARC64) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
2365 case SYS_socketcall:
2366 decode_subcall(tcp, SYS_socket_subcall,
2367 SYS_socket_nsubcalls, deref_style);
2370 decode_subcall(tcp, SYS_ipc_subcall,
2371 SYS_ipc_nsubcalls, shift_style);
2373 #endif /* !ALPHA && !MIPS && !SPARC && !SPARC64 && !HPPA && !X86_64 */
2374 #if defined (SPARC) || defined (SPARC64)
2375 case SYS_socketcall:
2376 sparc_socket_decode (tcp);
2381 #ifdef SYS_pgrpsys_subcall
2383 decode_subcall(tcp, SYS_pgrpsys_subcall,
2384 SYS_pgrpsys_nsubcalls, shift_style);
2386 #endif /* SYS_pgrpsys_subcall */
2387 #ifdef SYS_sigcall_subcall
2389 decode_subcall(tcp, SYS_sigcall_subcall,
2390 SYS_sigcall_nsubcalls, mask_style);
2392 #endif /* SYS_sigcall_subcall */
2394 decode_subcall(tcp, SYS_msgsys_subcall,
2395 SYS_msgsys_nsubcalls, shift_style);
2398 decode_subcall(tcp, SYS_shmsys_subcall,
2399 SYS_shmsys_nsubcalls, shift_style);
2402 decode_subcall(tcp, SYS_semsys_subcall,
2403 SYS_semsys_nsubcalls, shift_style);
2407 decode_subcall(tcp, SYS_utssys_subcall,
2408 SYS_utssys_nsubcalls, shift_style);
2412 decode_subcall(tcp, SYS_sysfs_subcall,
2413 SYS_sysfs_nsubcalls, shift_style);
2416 decode_subcall(tcp, SYS_spcall_subcall,
2417 SYS_spcall_nsubcalls, shift_style);
2419 #ifdef SYS_context_subcall
2421 decode_subcall(tcp, SYS_context_subcall,
2422 SYS_context_nsubcalls, shift_style);
2424 #endif /* SYS_context_subcall */
2425 #ifdef SYS_door_subcall
2427 decode_subcall(tcp, SYS_door_subcall,
2428 SYS_door_nsubcalls, door_style);
2430 #endif /* SYS_door_subcall */
2431 #ifdef SYS_kaio_subcall
2433 decode_subcall(tcp, SYS_kaio_subcall,
2434 SYS_kaio_nsubcalls, shift_style);
2442 decode_subcall(tcp, 0, 0, table_style);
2447 decode_subcall(tcp, SYS_semsys_subcall,
2448 SYS_semsys_nsubcalls, shift_style);
2451 decode_subcall(tcp, SYS_msgsys_subcall,
2452 SYS_msgsys_nsubcalls, shift_style);
2455 decode_subcall(tcp, SYS_shmsys_subcall,
2456 SYS_shmsys_nsubcalls, shift_style);
2461 internal_syscall(tcp);
2462 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2463 tcp->flags |= TCB_INSYSCALL;
2468 gettimeofday(&tcp->etime, NULL);
2469 tcp->flags |= TCB_INSYSCALL;
2474 tcp->flags &= ~TCB_REPRINT;
2476 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2477 tprintf("syscall_%lu(", tcp->scno);
2479 tprintf("%s(", sysent[tcp->scno].sys_name);
2480 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2481 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2482 sys_res = printargs(tcp);
2484 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2485 if (fflush(tcp->outf) == EOF)
2487 tcp->flags |= TCB_INSYSCALL;
2488 /* Measure the entrance time as late as possible to avoid errors. */
2490 gettimeofday(&tcp->etime, NULL);
2498 if (entering(tcp)) {
2501 for (i = 0; i < tcp->u_nargs; i++)
2502 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2514 #if defined (SPARC) || defined (SPARC64)
2516 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
2520 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2522 #endif /* SPARC || SPARC64 */
2524 if (upeek(tcp->pid, PT_R9, &val) < 0)
2529 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2535 val = tcp->status.PR_REG[R_O1];
2538 val = tcp->status.PR_REG[EDX];
2541 val = tcp->status.PR_REG[RDX];
2544 val = tcp->status.PR_REG[CTX_V1];
2549 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
2556 * Apparently, indirect system calls have already be converted by ptrace(2),
2557 * so if you see "indir" this program has gone astray.
2565 if (entering(tcp)) {
2566 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2567 fprintf(stderr, "Bogus syscall: %u\n", scno);
2570 nargs = sysent[scno].nargs;
2571 tprintf("%s", sysent[scno].sys_name);
2572 for (i = 0; i < nargs; i++)
2573 tprintf(", %#lx", tcp->u_arg[i+1]);
2583 return -tv_cmp(&counts[*((int *) a)].time, &counts[*((int *) b)].time);
2591 return strcmp(sysent[*((int *) a)].sys_name,
2592 sysent[*((int *) b)].sys_name);
2600 int m = counts[*((int *) a)].calls, n = counts[*((int *) b)].calls;
2602 return (m < n) ? 1 : (m > n) ? -1 : 0;
2605 static int (*sortfun)();
2606 static struct timeval overhead = { -1, -1 };
2612 if (strcmp(sortby, "time") == 0)
2614 else if (strcmp(sortby, "calls") == 0)
2615 sortfun = count_cmp;
2616 else if (strcmp(sortby, "name") == 0)
2617 sortfun = syscall_cmp;
2618 else if (strcmp(sortby, "nothing") == 0)
2621 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2626 void set_overhead(n)
2629 overhead.tv_sec = n / 1000000;
2630 overhead.tv_usec = n % 1000000;
2638 int call_cum, error_cum;
2639 struct timeval tv_cum, dtv;
2641 char *dashes = "-------------------------";
2644 int *sorted_count = malloc(nsyscalls * sizeof(int));
2646 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2647 if (overhead.tv_sec == -1) {
2648 tv_mul(&overhead, &shortest, 8);
2649 tv_div(&overhead, &overhead, 10);
2651 for (i = 0; i < nsyscalls; i++) {
2652 sorted_count[i] = i;
2653 if (counts == NULL || counts[i].calls == 0)
2655 tv_mul(&dtv, &overhead, counts[i].calls);
2656 tv_sub(&counts[i].time, &counts[i].time, &dtv);
2657 call_cum += counts[i].calls;
2658 error_cum += counts[i].errors;
2659 tv_add(&tv_cum, &tv_cum, &counts[i].time);
2661 if (counts && sortfun)
2662 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2663 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2664 "% time", "seconds", "usecs/call",
2665 "calls", "errors", "syscall");
2666 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2667 dashes, dashes, dashes, dashes, dashes, dashes);
2669 for (i = 0; i < nsyscalls; i++) {
2670 j = sorted_count[i];
2671 if (counts[j].calls == 0)
2673 tv_div(&dtv, &counts[j].time, counts[j].calls);
2674 if (counts[j].errors)
2675 sprintf(error_str, "%d", counts[j].errors);
2677 error_str[0] = '\0';
2678 percent = (100.0 * tv_float(&counts[j].time)
2679 / tv_float(&tv_cum));
2680 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2681 percent, (long) counts[j].time.tv_sec,
2682 (long) counts[j].time.tv_usec,
2683 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2685 error_str, sysent[j].sys_name);
2690 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2691 dashes, dashes, dashes, dashes, dashes, dashes);
2693 sprintf(error_str, "%d", error_cum);
2695 error_str[0] = '\0';
2696 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2697 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2698 call_cum, error_str, "total");