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 (known_scno(tcp)) {
453 #if defined SYS_pread && SYS_pread64 != SYS_pread
458 #elif defined SYS_sub_recv
463 #elif defined SYS_sub_recvfrom
464 case SYS_sub_recvfrom:
466 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
467 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
473 #if defined SYS_pwrite && SYS_pwrite64 != SYS_pwrite
478 #elif defined SYS_sub_send
483 #elif defined SYS_sub_sendto
486 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
487 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
491 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
492 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
498 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
499 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
506 enum subcall_style { shift_style, deref_style, mask_style, door_style };
508 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
516 static const struct subcall subcalls_table[] = {
517 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
519 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
521 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
523 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
527 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
529 static const int socket_map [] = {
532 /* SYS_CONNECT */ 98,
533 /* SYS_LISTEN */ 106,
535 /* SYS_GETSOCKNAME */ 150,
536 /* SYS_GETPEERNAME */ 141,
537 /* SYS_SOCKETPAIR */ 135,
540 /* SYS_SENDTO */ 133,
541 /* SYS_RECVFROM */ 125,
542 /* SYS_SHUTDOWN */ 134,
543 /* SYS_SETSOCKOPT */ 105,
544 /* SYS_GETSOCKOPT */ 118,
545 /* SYS_SENDMSG */ 114,
546 /* SYS_RECVMSG */ 113
549 #if defined (SPARC) || defined (SPARC64)
551 sparc_socket_decode (tcp)
557 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
560 tcp->scno = socket_map [tcp->u_arg [0]-1];
561 n = tcp->u_nargs = sysent [tcp->scno].nargs;
562 addr = tcp->u_arg [1];
563 for (i = 0; i < n; i++){
565 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
567 tcp->u_arg [i] = arg;
568 addr += sizeof (arg);
574 decode_subcall(tcp, subcall, nsubcalls, style)
578 enum subcall_style style;
580 long addr, mask, arg;
585 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
587 tcp->scno = subcall + tcp->u_arg[0];
588 if (sysent[tcp->scno].nargs != -1)
589 tcp->u_nargs = sysent[tcp->scno].nargs;
592 for (i = 0; i < tcp->u_nargs; i++)
593 tcp->u_arg[i] = tcp->u_arg[i + 1];
596 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
598 tcp->scno = subcall + tcp->u_arg[0];
599 addr = tcp->u_arg[1];
600 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
601 if (umove(tcp, addr, &arg) < 0)
606 tcp->u_nargs = sysent[tcp->scno].nargs;
609 mask = (tcp->u_arg[0] >> 8) & 0xff;
610 for (i = 0; mask; i++)
614 tcp->u_arg[0] &= 0xff;
615 tcp->scno = subcall + i;
616 if (sysent[tcp->scno].nargs != -1)
617 tcp->u_nargs = sysent[tcp->scno].nargs;
621 * Oh, yuck. The call code is the *sixth* argument.
622 * (don't you mean the *last* argument? - JH)
624 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
626 tcp->scno = subcall + tcp->u_arg[5];
627 if (sysent[tcp->scno].nargs != -1)
628 tcp->u_nargs = sysent[tcp->scno].nargs;
634 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
635 if (subcalls_table[i].call == tcp->scno) break;
636 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
637 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
638 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
639 for (i = 0; i < tcp->u_nargs; i++)
640 tcp->u_arg[i] = tcp->u_arg[i + 1];
648 struct tcb *tcp_last = NULL;
651 internal_syscall(tcp)
655 * We must always trace a few critical system calls in order to
656 * correctly support following forks in the presence of tracing
659 switch (known_scno(tcp)) {
720 internal_wait(tcp, 2);
724 internal_wait(tcp, 3);
734 #ifdef __NR_exit_group
735 case __NR_exit_group:
738 case 252: /* IA-32 __NR_exit_group */
753 #elif defined (POWERPC)
754 static long result,flags;
758 static struct pt_regs regs;
759 #elif defined (ALPHA)
762 #elif defined (SPARC) || defined (SPARC64)
763 static struct regs regs;
764 static unsigned long trap;
768 #elif defined(S390) || defined(S390X)
771 static long syscall_mode;
778 #elif defined(X86_64)
796 #if defined(S390) || defined(S390X)
797 if (tcp->flags & TCB_WAITEXECVE) {
799 * When the execve system call completes successfully, the
800 * new process still has -ENOSYS (old style) or __NR_execve
801 * (new style) in gpr2. We cannot recover the scno again
802 * by disassembly, because the image that executed the
803 * syscall is gone now. Fortunately, we don't want it. We
804 * leave the flag set so that syscall_fixup can fake the
807 if (tcp->flags & TCB_INSYSCALL)
810 * This is the SIGTRAP after execve. We cannot try to read
811 * the system call here either.
813 tcp->flags &= ~TCB_WAITEXECVE;
817 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
820 if (syscall_mode != -ENOSYS) {
822 * Since kernel version 2.5.44 the scno gets passed in gpr2.
827 * Old style of "passing" the scno via the SVC instruction.
830 long opcode, offset_reg, tmp;
832 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
833 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
834 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
835 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
837 if (upeek(pid, PT_PSWADDR, &pc) < 0)
840 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
842 perror("peektext(pc-oneword)");
847 * We have to check if the SVC got executed directly or via an
848 * EXECUTE instruction. In case of EXECUTE it is necessary to do
849 * instruction decoding to derive the system call number.
850 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
851 * so that this doesn't work if a SVC opcode is part of an EXECUTE
852 * opcode. Since there is no way to find out the opcode size this
853 * is the best we can do...
856 if ((opcode & 0xff00) == 0x0a00) {
858 scno = opcode & 0xff;
861 /* SVC got executed by EXECUTE instruction */
864 * Do instruction decoding of EXECUTE. If you really want to
865 * understand this, read the Principles of Operations.
867 svc_addr = (void *) (opcode & 0xfff);
870 offset_reg = (opcode & 0x000f0000) >> 16;
871 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
876 offset_reg = (opcode & 0x0000f000) >> 12;
877 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
881 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
890 offset_reg = (opcode & 0x00f00000) >> 20;
891 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
894 scno = (scno | tmp) & 0xff;
897 #elif defined (POWERPC)
898 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
900 if (!(tcp->flags & TCB_INSYSCALL)) {
901 /* Check if we return from execve. */
902 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
903 tcp->flags &= ~TCB_WAITEXECVE;
908 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
910 #elif defined (X86_64)
911 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
914 if (!(tcp->flags & TCB_INSYSCALL)) {
915 static int currpers=-1;
918 /* Check CS register value. On x86-64 linux it is:
919 * 0x33 for long mode (64 bit)
920 * 0x23 for compatibility mode (32 bit)
921 * It takes only one ptrace and thus doesn't need
924 if (upeek(pid, 8*CS, &val) < 0)
928 case 0x23: currpers = 1; break;
929 case 0x33: currpers = 0; break;
931 fprintf(stderr, "Unknown value CS=0x%02X while "
932 "detecting personality of process "
933 "PID=%d\n", (int)val, pid);
934 currpers = current_personality;
938 /* This version analyzes the opcode of a syscall instruction.
939 * (int 0x80 on i386 vs. syscall on x86-64)
940 * It works, but is too complicated.
942 unsigned long val, rip, i;
944 if(upeek(pid, 8*RIP, &rip)<0)
945 perror("upeek(RIP)");
947 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
951 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
953 printf("ptrace_peektext failed: %s\n",
955 switch (call & 0xffff)
957 /* x86-64: syscall = 0x0f 0x05 */
958 case 0x050f: currpers = 0; break;
959 /* i386: int 0x80 = 0xcd 0x80 */
960 case 0x80cd: currpers = 1; break;
962 currpers = current_personality;
964 "Unknown syscall opcode (0x%04X) while "
965 "detecting personality of process "
966 "PID=%d\n", (int)call, pid);
970 if(currpers != current_personality)
972 char *names[]={"64 bit", "32 bit"};
973 set_personality(currpers);
974 printf("[ Process PID=%d runs in %s mode. ]\n",
975 pid, names[current_personality]);
979 # define IA64_PSR_IS ((long)1 << 34)
980 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
981 ia32 = (psr & IA64_PSR_IS) != 0;
982 if (!(tcp->flags & TCB_INSYSCALL)) {
984 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
987 if (upeek (pid, PT_R15, &scno) < 0)
990 /* Check if we return from execve. */
991 if (tcp->flags & TCB_WAITEXECVE) {
992 tcp->flags &= ~TCB_WAITEXECVE;
996 /* syscall in progress */
997 if (upeek (pid, PT_R8, &r8) < 0)
999 if (upeek (pid, PT_R10, &r10) < 0)
1004 * Read complete register set in one go.
1006 if (ptrace(PTRACE_GETREGS, pid, NULL, (void *)®s) == -1)
1010 * We only need to grab the syscall number on syscall entry.
1012 if (regs.ARM_ip == 0) {
1014 * Note: we only deal with only 32-bit CPUs here.
1016 if (regs.ARM_cpsr & 0x20) {
1018 * Get the Thumb-mode system call number
1023 * Get the ARM-mode system call number
1026 scno = ptrace(PTRACE_PEEKTEXT, pid, (void *)(regs.ARM_pc - 4), NULL);
1030 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
1031 tcp->flags &= ~TCB_WAITEXECVE;
1035 if ((scno & 0x0ff00000) != 0x0f900000) {
1036 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
1042 * Fixup the syscall number
1047 if (tcp->flags & TCB_INSYSCALL) {
1048 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
1049 tcp->flags &= ~TCB_INSYSCALL;
1052 if (!(tcp->flags & TCB_INSYSCALL)) {
1053 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1054 tcp->flags |= TCB_INSYSCALL;
1057 #elif defined (M68K)
1058 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
1060 #elif defined (MIPS)
1061 if (upeek(pid, REG_A3, &a3) < 0)
1064 if(!(tcp->flags & TCB_INSYSCALL)) {
1065 if (upeek(pid, REG_V0, &scno) < 0)
1068 if (scno < 0 || scno > nsyscalls) {
1069 if(a3 == 0 || a3 == -1) {
1071 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
1076 if (upeek(pid, REG_V0, &r2) < 0)
1079 #elif defined (ALPHA)
1080 if (upeek(pid, REG_A3, &a3) < 0)
1083 if (!(tcp->flags & TCB_INSYSCALL)) {
1084 if (upeek(pid, REG_R0, &scno) < 0)
1087 /* Check if we return from execve. */
1088 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1089 tcp->flags &= ~TCB_WAITEXECVE;
1094 * Do some sanity checks to figure out if it's
1095 * really a syscall entry
1097 if (scno < 0 || scno > nsyscalls) {
1098 if (a3 == 0 || a3 == -1) {
1100 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
1106 if (upeek(pid, REG_R0, &r0) < 0)
1109 #elif defined (SPARC) || defined (SPARC64)
1110 /* Everything we need is in the current register set. */
1111 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
1114 /* If we are entering, then disassemble the syscall trap. */
1115 if (!(tcp->flags & TCB_INSYSCALL)) {
1116 /* Retrieve the syscall trap instruction. */
1118 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
1119 #if defined(SPARC64)
1125 /* Disassemble the trap to see what personality to use. */
1128 /* Linux/SPARC syscall trap. */
1132 /* Linux/SPARC64 syscall trap. */
1136 /* SunOS syscall trap. (pers 1) */
1137 fprintf(stderr,"syscall: SunOS no support\n");
1140 /* Solaris 2.x syscall trap. (per 2) */
1144 /* NetBSD/FreeBSD syscall trap. */
1145 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1148 /* Solaris 2.x gettimeofday */
1152 /* Unknown syscall trap. */
1153 if(tcp->flags & TCB_WAITEXECVE) {
1154 tcp->flags &= ~TCB_WAITEXECVE;
1157 #if defined (SPARC64)
1158 fprintf(stderr,"syscall: unknown syscall trap %08lx %016lx\n", trap, regs.r_tpc);
1160 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
1165 /* Extract the system call number from the registers. */
1166 if (trap == 0x91d02027)
1172 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
1176 if (upeek(pid, PT_GR20, &scno) < 0)
1178 if (!(tcp->flags & TCB_INSYSCALL)) {
1179 /* Check if we return from execve. */
1180 if ((tcp->flags & TCB_WAITEXECVE)) {
1181 tcp->flags &= ~TCB_WAITEXECVE;
1187 * In the new syscall ABI, the system call number is in R3.
1189 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1193 /* Odd as it may seem, a glibc bug has been known to cause
1194 glibc to issue bogus negative syscall numbers. So for
1195 our purposes, make strace print what it *should* have been */
1196 long correct_scno = (scno & 0xff);
1199 "Detected glibc bug: bogus system call number = %ld, "
1200 "correcting to %ld\n",
1203 scno = correct_scno;
1207 if (!(tcp->flags & TCB_INSYSCALL)) {
1208 /* Check if we return from execve. */
1209 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1210 tcp->flags &= ~TCB_WAITEXECVE;
1215 if (upeek(pid, REG_SYSCALL, &scno) < 0)
1219 if (!(tcp->flags & TCB_INSYSCALL)) {
1220 /* Check if we return from execve. */
1221 if (tcp->flags & TCB_WAITEXECVE) {
1222 tcp->flags &= ~TCB_WAITEXECVE;
1229 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1232 /* new syscall ABI returns result in R0 */
1233 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1236 /* ABI defines result returned in r9 */
1237 if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
1242 #ifdef HAVE_PR_SYSCALL
1243 scno = tcp->status.PR_SYSCALL;
1244 #else /* !HAVE_PR_SYSCALL */
1246 scno = tcp->status.PR_WHAT;
1248 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1252 switch (regs.r_eax) {
1255 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1261 #endif /* FREEBSD */
1262 #endif /* !HAVE_PR_SYSCALL */
1263 #endif /* USE_PROCFS */
1264 if (!(tcp->flags & TCB_INSYSCALL))
1274 long scno = tcp->scno;
1275 if (scno >= 0 && scno < nsyscalls && sysent[scno].native_scno != 0)
1276 scno = sysent[scno].native_scno;
1278 scno += NR_SYSCALL_BASE;
1288 #else /* USE_PROCFS */
1289 int scno = known_scno(tcp);
1291 if (!(tcp->flags & TCB_INSYSCALL)) {
1292 if (tcp->status.PR_WHY != PR_SYSENTRY) {
1296 || scno == SYS_vfork
1297 #endif /* SYS_vfork */
1299 || scno == SYS_fork1
1300 #endif /* SYS_fork1 */
1302 || scno == SYS_forkall
1303 #endif /* SYS_forkall */
1305 || scno == SYS_rfork1
1306 #endif /* SYS_fork1 */
1308 || scno == SYS_rforkall
1309 #endif /* SYS_rforkall */
1311 /* We are returning in the child, fake it. */
1312 tcp->status.PR_WHY = PR_SYSENTRY;
1314 tcp->status.PR_WHY = PR_SYSEXIT;
1317 fprintf(stderr, "syscall: missing entry\n");
1318 tcp->flags |= TCB_INSYSCALL;
1323 if (tcp->status.PR_WHY != PR_SYSEXIT) {
1324 fprintf(stderr, "syscall: missing exit\n");
1325 tcp->flags &= ~TCB_INSYSCALL;
1328 #endif /* USE_PROCFS */
1330 if (!(tcp->flags & TCB_INSYSCALL)) {
1332 fprintf(stderr, "syscall: missing entry\n");
1333 tcp->flags |= TCB_INSYSCALL;
1340 * This happens when a signal handler
1341 * for a signal which interrupted a
1342 * a system call makes another system call.
1344 fprintf(stderr, "syscall: missing exit\n");
1346 tcp->flags &= ~TCB_INSYSCALL;
1352 if (upeek(pid, 4*EAX, &eax) < 0)
1354 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1356 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1359 #elif defined (X86_64)
1360 if (upeek(pid, 8*RAX, &rax) < 0)
1362 if (current_personality == 1)
1363 rax = (long int)(int)rax; /* sign extend from 32 bits */
1364 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1366 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1369 #elif defined (S390) || defined (S390X)
1370 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1372 if (syscall_mode != -ENOSYS)
1373 syscall_mode = tcp->scno;
1374 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
1376 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1379 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1380 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1381 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1383 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1384 * flag set for the post-execve SIGTRAP to see and reset.
1388 #elif defined (POWERPC)
1389 # define SO_MASK 0x10000000
1390 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1392 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
1394 if (flags & SO_MASK)
1396 #elif defined (M68K)
1397 if (upeek(pid, 4*PT_D0, &d0) < 0)
1399 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1401 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1408 #elif defined (HPPA)
1409 if (upeek(pid, PT_GR28, &r28) < 0)
1412 if (upeek(pid, PT_R10, &r10) < 0)
1414 if (upeek(pid, PT_R8, &r8) < 0)
1416 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1418 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1432 #if defined(S390) || defined(S390X)
1433 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1441 #else /* !S390 && !S390X */
1443 if (eax < 0 && -eax < nerrnos) {
1453 if (rax < 0 && -rax < nerrnos) {
1467 if (err < 0 && -err < nerrnos) {
1495 if (result && (unsigned long) -result < nerrnos) {
1500 tcp->u_rval = result;
1503 #else /* !POWERPC */
1505 if (d0 && (unsigned) -d0 < nerrnos) {
1515 if (regs.ARM_r0 && (unsigned) -regs.ARM_r0 < nerrnos) {
1517 u_error = -regs.ARM_r0;
1520 tcp->u_rval = regs.ARM_r0;
1535 if (regs.r_psr & PSR_C) {
1537 u_error = regs.r_o0;
1540 tcp->u_rval = regs.r_o0;
1545 if (regs.r_tstate & 0x1100000000UL) {
1547 u_error = regs.r_o0;
1550 tcp->u_rval = regs.r_o0;
1553 #else /* !SPARC64 */
1555 if (r28 && (unsigned) -r28 < nerrnos) {
1565 /* interpret R0 as return value or error number */
1566 if (r0 && (unsigned) -r0 < nerrnos) {
1576 /* interpret result as return value or error number */
1577 if (r9 && (unsigned) -r9 < nerrnos) {
1589 #endif /* SPARC64 */
1593 #endif /* POWERPC */
1598 #endif /* S390 || S390X */
1601 /* get error code from user struct */
1602 if (upeek(pid, uoff(u_error), &u_error) < 0)
1604 u_error >>= 24; /* u_error is a char */
1606 /* get system call return value */
1607 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1612 /* Judicious guessing goes a long way. */
1613 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1615 u_error = tcp->status.pr_reg[R_O0];
1618 tcp->u_rval = tcp->status.pr_reg[R_O0];
1623 /* Wanna know how to kill an hour single-stepping? */
1624 if (tcp->status.PR_REG[EFL] & 0x1) {
1626 u_error = tcp->status.PR_REG[EAX];
1629 tcp->u_rval = tcp->status.PR_REG[EAX];
1630 #ifdef HAVE_LONG_LONG
1632 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1633 tcp->status.PR_REG[EAX];
1639 /* Wanna know how to kill an hour single-stepping? */
1640 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1642 u_error = tcp->status.PR_REG[RAX];
1645 tcp->u_rval = tcp->status.PR_REG[RAX];
1650 if (tcp->status.pr_reg[CTX_A3]) {
1652 u_error = tcp->status.pr_reg[CTX_V0];
1655 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1661 if (regs.r_eflags & PSL_C) {
1663 u_error = regs.r_eax;
1665 tcp->u_rval = regs.r_eax;
1667 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1670 #endif /* FREEBSD */
1671 tcp->u_error = u_error;
1676 force_result(tcp, error, rval)
1682 #if defined(S390) || defined(S390X)
1683 gpr2 = error ? -error : rval;
1684 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1686 #else /* !S390 && !S390X */
1688 eax = error ? -error : rval;
1689 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1693 rax = error ? -error : rval;
1694 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 8), rax) < 0)
1699 r8 = error ? -error : rval;
1700 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1712 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1713 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1726 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1727 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1731 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1741 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1742 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
1744 #else /* !POWERPC */
1746 d0 = error ? -error : rval;
1747 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1751 regs.ARM_r0 = error ? -error : rval;
1752 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), regs.ARM_r0) < 0)
1764 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1765 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1769 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1772 regs.r_psr |= PSR_C;
1776 regs.r_psr &= ~PSR_C;
1779 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1783 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1786 regs.r_tstate |= 0x1100000000UL;
1790 regs.r_tstate &= ~0x1100000000UL;
1793 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1795 #else /* !SPARC64 */
1797 r28 = error ? -error : rval;
1798 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1802 r0 = error ? -error : rval;
1803 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1807 r9 = error ? -error : rval;
1808 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1814 #endif /* SPARC64 */
1818 #endif /* POWERPC */
1823 #endif /* S390 || S390X */
1826 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1828 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1836 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1841 regs.r_eflags |= PSL_C;
1845 regs.r_eflags &= ~PSL_C;
1848 if (pwrite(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1852 #endif /* FREEBSD */
1854 /* All branches reach here on success (only). */
1855 tcp->u_error = error;
1866 #endif /* !USE_PROCFS */
1868 #if defined(S390) || defined(S390X)
1871 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1872 tcp->u_nargs = sysent[tcp->scno].nargs;
1874 tcp->u_nargs = MAX_ARGS;
1875 for (i = 0; i < tcp->u_nargs; i++) {
1876 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
1880 #elif defined (ALPHA)
1883 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1884 tcp->u_nargs = sysent[tcp->scno].nargs;
1886 tcp->u_nargs = MAX_ARGS;
1887 for (i = 0; i < tcp->u_nargs; i++) {
1888 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1889 * for scno somewhere above here!
1891 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1895 #elif defined (IA64)
1898 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1899 /* be backwards compatible with kernel < 2.4.4... */
1901 # define PT_RBS_END PT_AR_BSP
1904 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
1906 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1909 sof = (cfm >> 0) & 0x7f;
1910 sol = (cfm >> 7) & 0x7f;
1911 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1913 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1914 && sysent[tcp->scno].nargs != -1)
1915 tcp->u_nargs = sysent[tcp->scno].nargs;
1917 tcp->u_nargs = MAX_ARGS;
1918 for (i = 0; i < tcp->u_nargs; ++i) {
1919 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1920 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1926 if (/* EBX = out0 */
1927 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1929 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1931 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1933 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1935 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1937 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1940 for (i = 0; i < 6; ++i)
1941 /* truncate away IVE sign-extension */
1942 tcp->u_arg[i] &= 0xffffffff;
1944 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1945 && sysent[tcp->scno].nargs != -1)
1946 tcp->u_nargs = sysent[tcp->scno].nargs;
1951 #elif defined (MIPS)
1956 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1957 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1959 nargs = tcp->u_nargs = MAX_ARGS;
1961 if(upeek(pid, REG_SP, &sp) < 0)
1963 for(i = 0; i < 4; i++) {
1964 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1967 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1968 (char *)(tcp->u_arg + 4));
1970 for(i = 0; i < nargs; i++) {
1971 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1976 #elif defined (POWERPC)
1978 #define PT_ORIG_R3 34
1982 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1983 tcp->u_nargs = sysent[tcp->scno].nargs;
1985 tcp->u_nargs = MAX_ARGS;
1986 for (i = 0; i < tcp->u_nargs; i++) {
1987 if (upeek(pid, (i==0) ?
1988 (sizeof(unsigned long)*PT_ORIG_R3) :
1989 ((i+PT_R3)*sizeof(unsigned long)),
1990 &tcp->u_arg[i]) < 0)
1994 #elif defined (SPARC) || defined (SPARC64)
1998 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1999 tcp->u_nargs = sysent[tcp->scno].nargs;
2001 tcp->u_nargs = MAX_ARGS;
2002 for (i = 0; i < tcp->u_nargs; i++)
2003 tcp->u_arg[i] = *((®s.r_o0) + i);
2005 #elif defined (HPPA)
2009 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2010 tcp->u_nargs = sysent[tcp->scno].nargs;
2012 tcp->u_nargs = MAX_ARGS;
2013 for (i = 0; i < tcp->u_nargs; i++) {
2014 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
2022 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2023 tcp->u_nargs = sysent[tcp->scno].nargs;
2025 tcp->u_nargs = MAX_ARGS;
2026 for (i = 0; i < tcp->u_nargs; i++)
2027 tcp->u_arg[i] = regs.uregs[i];
2032 static int syscall_regs[] = {
2033 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
2034 REG_REG0, REG_REG0+1, REG_REG0+2
2037 tcp->u_nargs = sysent[tcp->scno].nargs;
2038 for (i = 0; i < tcp->u_nargs; i++) {
2039 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
2046 /* Registers used by SH5 Linux system calls for parameters */
2047 static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
2050 * TODO: should also check that the number of arguments encoded
2051 * in the trap number matches the number strace expects.
2054 assert(sysent[tcp->scno].nargs <
2055 sizeof(syscall_regs)/sizeof(syscall_regs[0]));
2058 tcp->u_nargs = sysent[tcp->scno].nargs;
2059 for (i = 0; i < tcp->u_nargs; i++) {
2060 if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
2065 #elif defined(X86_64)
2068 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
2069 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
2070 {RBX,RCX,RDX,RSI,RDI,RBP} /* i386 ABI */
2073 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2074 tcp->u_nargs = sysent[tcp->scno].nargs;
2076 tcp->u_nargs = MAX_ARGS;
2077 for (i = 0; i < tcp->u_nargs; i++) {
2078 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
2082 #else /* Other architecture (like i386) (32bits specific) */
2085 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2086 tcp->u_nargs = sysent[tcp->scno].nargs;
2088 tcp->u_nargs = MAX_ARGS;
2089 for (i = 0; i < tcp->u_nargs; i++) {
2090 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
2099 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2100 tcp->u_nargs = sysent[tcp->scno].nargs;
2102 tcp->u_nargs = MAX_ARGS;
2103 for (i = 0; i < tcp->u_nargs; i++) {
2106 if (upeek(pid, uoff(u_arg[0]) +
2107 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
2115 * SGI is broken: even though it has pr_sysarg, it doesn't
2116 * set them on system call entry. Get a clue.
2118 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2119 tcp->u_nargs = sysent[tcp->scno].nargs;
2121 tcp->u_nargs = tcp->status.pr_nsysarg;
2122 if (tcp->u_nargs > 4) {
2123 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2124 4*sizeof(tcp->u_arg[0]));
2125 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
2126 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
2129 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2130 tcp->u_nargs*sizeof(tcp->u_arg[0]));
2134 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
2136 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2137 tcp->u_nargs = sysent[tcp->scno].nargs;
2139 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
2140 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2141 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2142 #elif defined (HAVE_PR_SYSCALL)
2143 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2144 tcp->u_nargs = sysent[tcp->scno].nargs;
2146 tcp->u_nargs = tcp->status.pr_nsysarg;
2149 for (i = 0; i < tcp->u_nargs; i++)
2150 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
2152 #elif defined (I386)
2153 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2154 tcp->u_nargs = sysent[tcp->scno].nargs;
2157 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2158 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2160 I DONT KNOW WHAT TO DO
2161 #endif /* !HAVE_PR_SYSCALL */
2164 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2165 sysent[tcp->scno].nargs > tcp->status.val)
2166 tcp->u_nargs = sysent[tcp->scno].nargs;
2168 tcp->u_nargs = tcp->status.val;
2169 if (tcp->u_nargs < 0)
2171 if (tcp->u_nargs > MAX_ARGS)
2172 tcp->u_nargs = MAX_ARGS;
2173 switch(regs.r_eax) {
2175 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2176 regs.r_esp + sizeof(int) + sizeof(quad_t));
2179 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2180 regs.r_esp + 2 * sizeof(int));
2183 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2184 regs.r_esp + sizeof(int));
2187 #endif /* FREEBSD */
2199 /* Measure the exit time as early as possible to avoid errors. */
2200 if (dtime && (tcp->flags & TCB_INSYSCALL))
2201 gettimeofday(&tv, NULL);
2203 res = get_scno(tcp);
2207 res = syscall_fixup(tcp);
2211 if (tcp->flags & TCB_INSYSCALL) {
2213 res = get_error(tcp);
2217 internal_syscall(tcp);
2218 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2219 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2220 tcp->flags &= ~TCB_INSYSCALL;
2224 if (tcp->flags & TCB_REPRINT) {
2227 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2228 tprintf("syscall_%lu", tcp->scno);
2230 tprintf("%s", sysent[tcp->scno].sys_name);
2231 tprintf(" resumed> ");
2234 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
2235 if (counts == NULL) {
2236 counts = calloc(sizeof *counts, nsyscalls);
2237 if (counts == NULL) {
2239 strace: out of memory for call counts\n");
2244 counts[tcp->scno].calls++;
2246 counts[tcp->scno].errors++;
2247 tv_sub(&tv, &tv, &tcp->etime);
2249 if (tv_cmp(&tv, &tcp->dtime) > 0) {
2250 static struct timeval one_tick;
2251 if (one_tick.tv_usec == 0) {
2252 /* Initialize it. */
2253 struct itimerval it;
2254 memset(&it, 0, sizeof it);
2255 it.it_interval.tv_usec = 1;
2256 setitimer(ITIMER_REAL, &it, NULL);
2257 getitimer(ITIMER_REAL, &it);
2258 one_tick = it.it_interval;
2261 if (tv_nz(&tcp->dtime))
2263 else if (tv_cmp(&tv, &one_tick) > 0) {
2264 if (tv_cmp(&shortest, &one_tick) < 0)
2271 if (tv_cmp(&tv, &shortest) < 0)
2273 tv_add(&counts[tcp->scno].time,
2274 &counts[tcp->scno].time, &tv);
2275 tcp->flags &= ~TCB_INSYSCALL;
2279 if (tcp->scno >= nsyscalls || tcp->scno < 0
2280 || (qual_flags[tcp->scno] & QUAL_RAW))
2281 sys_res = printargs(tcp);
2283 if (not_failing_only && tcp->u_error)
2284 return 0; /* ignore failed syscalls */
2285 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2287 u_error = tcp->u_error;
2290 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2291 qual_flags[tcp->scno] & QUAL_RAW) {
2293 tprintf("= -1 (errno %ld)", u_error);
2295 tprintf("= %#lx", tcp->u_rval);
2297 else if (!(sys_res & RVAL_NONE) && u_error) {
2301 tprintf("= ? ERESTARTSYS (To be restarted)");
2303 case ERESTARTNOINTR:
2304 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2306 case ERESTARTNOHAND:
2307 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2309 case ERESTART_RESTARTBLOCK:
2310 tprintf("= ? ERESTART_RESTARTBLOCK (To be restarted)");
2316 tprintf("E??? (errno %ld)", u_error);
2317 else if (u_error < nerrnos)
2318 tprintf("%s (%s)", errnoent[u_error],
2321 tprintf("ERRNO_%ld (%s)", u_error,
2327 if (sys_res & RVAL_NONE)
2330 switch (sys_res & RVAL_MASK) {
2332 tprintf("= %#lx", tcp->u_rval);
2335 tprintf("= %#lo", tcp->u_rval);
2338 tprintf("= %lu", tcp->u_rval);
2341 tprintf("= %ld", tcp->u_rval);
2343 #ifdef HAVE_LONG_LONG
2345 tprintf("= %#llx", tcp->u_lrval);
2348 tprintf("= %#llo", tcp->u_lrval);
2350 case RVAL_LUDECIMAL:
2351 tprintf("= %llu", tcp->u_lrval);
2354 tprintf("= %lld", tcp->u_lrval);
2359 "invalid rval format\n");
2363 if ((sys_res & RVAL_STR) && tcp->auxstr)
2364 tprintf(" (%s)", tcp->auxstr);
2367 tv_sub(&tv, &tv, &tcp->etime);
2368 tprintf(" <%ld.%06ld>",
2369 (long) tv.tv_sec, (long) tv.tv_usec);
2374 if (fflush(tcp->outf) == EOF)
2376 tcp->flags &= ~TCB_INSYSCALL;
2380 /* Entering system call */
2381 res = syscall_enter(tcp);
2385 switch (known_scno(tcp)) {
2387 #if !defined (ALPHA) && !defined(SPARC) && !defined(SPARC64) && !defined(MIPS) && !defined(HPPA)
2388 case SYS_socketcall:
2389 decode_subcall(tcp, SYS_socket_subcall,
2390 SYS_socket_nsubcalls, deref_style);
2393 decode_subcall(tcp, SYS_ipc_subcall,
2394 SYS_ipc_nsubcalls, shift_style);
2396 #endif /* !ALPHA && !MIPS && !SPARC && !SPARC64 && !HPPA */
2397 #if defined (SPARC) || defined (SPARC64)
2398 case SYS_socketcall:
2399 sparc_socket_decode (tcp);
2404 #ifdef SYS_pgrpsys_subcall
2406 decode_subcall(tcp, SYS_pgrpsys_subcall,
2407 SYS_pgrpsys_nsubcalls, shift_style);
2409 #endif /* SYS_pgrpsys_subcall */
2410 #ifdef SYS_sigcall_subcall
2412 decode_subcall(tcp, SYS_sigcall_subcall,
2413 SYS_sigcall_nsubcalls, mask_style);
2415 #endif /* SYS_sigcall_subcall */
2417 decode_subcall(tcp, SYS_msgsys_subcall,
2418 SYS_msgsys_nsubcalls, shift_style);
2421 decode_subcall(tcp, SYS_shmsys_subcall,
2422 SYS_shmsys_nsubcalls, shift_style);
2425 decode_subcall(tcp, SYS_semsys_subcall,
2426 SYS_semsys_nsubcalls, shift_style);
2430 decode_subcall(tcp, SYS_utssys_subcall,
2431 SYS_utssys_nsubcalls, shift_style);
2435 decode_subcall(tcp, SYS_sysfs_subcall,
2436 SYS_sysfs_nsubcalls, shift_style);
2439 decode_subcall(tcp, SYS_spcall_subcall,
2440 SYS_spcall_nsubcalls, shift_style);
2442 #ifdef SYS_context_subcall
2444 decode_subcall(tcp, SYS_context_subcall,
2445 SYS_context_nsubcalls, shift_style);
2447 #endif /* SYS_context_subcall */
2448 #ifdef SYS_door_subcall
2450 decode_subcall(tcp, SYS_door_subcall,
2451 SYS_door_nsubcalls, door_style);
2453 #endif /* SYS_door_subcall */
2454 #ifdef SYS_kaio_subcall
2456 decode_subcall(tcp, SYS_kaio_subcall,
2457 SYS_kaio_nsubcalls, shift_style);
2465 decode_subcall(tcp, 0, 0, table_style);
2470 decode_subcall(tcp, SYS_semsys_subcall,
2471 SYS_semsys_nsubcalls, shift_style);
2474 decode_subcall(tcp, SYS_msgsys_subcall,
2475 SYS_msgsys_nsubcalls, shift_style);
2478 decode_subcall(tcp, SYS_shmsys_subcall,
2479 SYS_shmsys_nsubcalls, shift_style);
2484 internal_syscall(tcp);
2485 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2486 tcp->flags |= TCB_INSYSCALL;
2491 gettimeofday(&tcp->etime, NULL);
2492 tcp->flags |= TCB_INSYSCALL;
2497 tcp->flags &= ~TCB_REPRINT;
2499 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2500 tprintf("syscall_%lu(", tcp->scno);
2502 tprintf("%s(", sysent[tcp->scno].sys_name);
2503 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2504 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2505 sys_res = printargs(tcp);
2507 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2508 if (fflush(tcp->outf) == EOF)
2510 tcp->flags |= TCB_INSYSCALL;
2511 /* Measure the entrance time as late as possible to avoid errors. */
2513 gettimeofday(&tcp->etime, NULL);
2521 if (entering(tcp)) {
2524 for (i = 0; i < tcp->u_nargs; i++)
2525 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2537 #if defined (SPARC) || defined (SPARC64)
2539 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
2543 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2546 if (upeek(tcp->pid, PT_R9, &val) < 0)
2548 #endif /* SPARC || SPARC64 */
2552 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2558 val = tcp->status.PR_REG[R_O1];
2561 val = tcp->status.PR_REG[EDX];
2564 val = tcp->status.PR_REG[RDX];
2567 val = tcp->status.PR_REG[CTX_V1];
2572 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
2579 * Apparently, indirect system calls have already be converted by ptrace(2),
2580 * so if you see "indir" this program has gone astray.
2588 if (entering(tcp)) {
2589 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2590 fprintf(stderr, "Bogus syscall: %u\n", scno);
2593 nargs = sysent[scno].nargs;
2594 tprintf("%s", sysent[scno].sys_name);
2595 for (i = 0; i < nargs; i++)
2596 tprintf(", %#lx", tcp->u_arg[i+1]);
2606 return -tv_cmp(&counts[*((int *) a)].time, &counts[*((int *) b)].time);
2614 return strcmp(sysent[*((int *) a)].sys_name,
2615 sysent[*((int *) b)].sys_name);
2623 int m = counts[*((int *) a)].calls, n = counts[*((int *) b)].calls;
2625 return (m < n) ? 1 : (m > n) ? -1 : 0;
2628 static int (*sortfun)();
2629 static struct timeval overhead = { -1, -1 };
2635 if (strcmp(sortby, "time") == 0)
2637 else if (strcmp(sortby, "calls") == 0)
2638 sortfun = count_cmp;
2639 else if (strcmp(sortby, "name") == 0)
2640 sortfun = syscall_cmp;
2641 else if (strcmp(sortby, "nothing") == 0)
2644 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2649 void set_overhead(n)
2652 overhead.tv_sec = n / 1000000;
2653 overhead.tv_usec = n % 1000000;
2661 int call_cum, error_cum;
2662 struct timeval tv_cum, dtv;
2664 char *dashes = "-------------------------";
2667 int *sorted_count = malloc(nsyscalls * sizeof(int));
2669 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2670 if (overhead.tv_sec == -1) {
2671 tv_mul(&overhead, &shortest, 8);
2672 tv_div(&overhead, &overhead, 10);
2674 for (i = 0; i < nsyscalls; i++) {
2675 sorted_count[i] = i;
2676 if (counts == NULL || counts[i].calls == 0)
2678 tv_mul(&dtv, &overhead, counts[i].calls);
2679 tv_sub(&counts[i].time, &counts[i].time, &dtv);
2680 call_cum += counts[i].calls;
2681 error_cum += counts[i].errors;
2682 tv_add(&tv_cum, &tv_cum, &counts[i].time);
2684 if (counts && sortfun)
2685 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2686 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2687 "% time", "seconds", "usecs/call",
2688 "calls", "errors", "syscall");
2689 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2690 dashes, dashes, dashes, dashes, dashes, dashes);
2692 for (i = 0; i < nsyscalls; i++) {
2693 j = sorted_count[i];
2694 if (counts[j].calls == 0)
2696 tv_div(&dtv, &counts[j].time, counts[j].calls);
2697 if (counts[j].errors)
2698 sprintf(error_str, "%d", counts[j].errors);
2700 error_str[0] = '\0';
2701 percent = (100.0 * tv_float(&counts[j].time)
2702 / tv_float(&tv_cum));
2703 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2704 percent, (long) counts[j].time.tv_sec,
2705 (long) counts[j].time.tv_usec,
2706 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2708 error_str, sysent[j].sys_name);
2713 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2714 dashes, dashes, dashes, dashes, dashes, dashes);
2716 sprintf(error_str, "%d", error_cum);
2718 error_str[0] = '\0';
2719 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2720 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2721 call_cum, error_str, "total");