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)) {
313 qualify_one(atoi(s), opt, not);
316 if (strlen(s) >= sizeof buf)
320 for (i = 0; s[i]; i++)
321 s[i] = toupper((unsigned char)(s[i]));
322 if (strncmp(s, "SIG", 3) == 0)
324 for (i = 0; i <= NSIG; i++)
325 if (strcmp(s, signame(i) + 3) == 0) {
326 qualify_one(atoi(s), opt, not);
333 qual_fault(s, opt, not)
335 const struct qual_options *opt;
342 qual_desc(s, opt, not)
344 const struct qual_options *opt;
347 if (s && *s && isdigit((unsigned char)*s)) {
348 qualify_one(atoi(s), opt, not);
358 if (strcmp(s, "file") == 0)
360 if (strcmp(s, "ipc") == 0)
362 if (strcmp(s, "network") == 0)
363 return TRACE_NETWORK;
364 if (strcmp(s, "process") == 0)
365 return TRACE_PROCESS;
366 if (strcmp(s, "signal") == 0)
375 const struct qual_options *opt;
380 opt = &qual_options[0];
381 for (i = 0; (p = qual_options[i].option_name); i++) {
383 if (strncmp(s, p, n) == 0 && s[n] == '=') {
384 opt = &qual_options[i];
394 if (strcmp(s, "none") == 0) {
398 if (strcmp(s, "all") == 0) {
399 for (i = 0; i < MAX_QUALS; i++) {
401 qual_flags[i] &= ~opt->bitflag;
403 qual_flags[i] |= opt->bitflag;
407 for (i = 0; i < MAX_QUALS; i++) {
409 qual_flags[i] |= opt->bitflag;
411 qual_flags[i] &= ~opt->bitflag;
413 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
414 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
415 for (i = 0; i < MAX_QUALS; i++) {
416 if (sysent[i].sys_flags & n) {
418 qual_flags[i] &= ~opt->bitflag;
420 qual_flags[i] |= opt->bitflag;
425 if (opt->qualify(p, opt, not)) {
426 fprintf(stderr, "strace: invalid %s `%s'\n",
427 opt->argument_name, p);
440 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
442 switch (tcp->scno + NR_SYSCALL_BASE) {
447 #if defined SYS_pread && SYS_pread64 != SYS_pread
456 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
457 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
463 #if defined SYS_pwrite && SYS_pwrite64 != SYS_pwrite
472 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
473 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
477 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
478 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
484 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
485 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
492 enum subcall_style { shift_style, deref_style, mask_style, door_style };
494 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
502 const struct subcall subcalls_table[] = {
503 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
505 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
507 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
509 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
513 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
515 const int socket_map [] = {
518 /* SYS_CONNECT */ 98,
519 /* SYS_LISTEN */ 106,
521 /* SYS_GETSOCKNAME */ 150,
522 /* SYS_GETPEERNAME */ 141,
523 /* SYS_SOCKETPAIR */ 135,
526 /* SYS_SENDTO */ 133,
527 /* SYS_RECVFROM */ 125,
528 /* SYS_SHUTDOWN */ 134,
529 /* SYS_SETSOCKOPT */ 105,
530 /* SYS_GETSOCKOPT */ 118,
531 /* SYS_SENDMSG */ 114,
532 /* SYS_RECVMSG */ 113
536 sparc_socket_decode (tcp)
542 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
545 tcp->scno = socket_map [tcp->u_arg [0]-1];
546 n = tcp->u_nargs = sysent [tcp->scno].nargs;
547 addr = tcp->u_arg [1];
548 for (i = 0; i < n; i++){
550 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
552 tcp->u_arg [i] = arg;
553 addr += sizeof (arg);
558 decode_subcall(tcp, subcall, nsubcalls, style)
562 enum subcall_style style;
564 long addr, mask, arg;
569 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
571 tcp->scno = subcall + tcp->u_arg[0];
572 if (sysent[tcp->scno].nargs != -1)
573 tcp->u_nargs = sysent[tcp->scno].nargs;
576 for (i = 0; i < tcp->u_nargs; i++)
577 tcp->u_arg[i] = tcp->u_arg[i + 1];
580 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
582 tcp->scno = subcall + tcp->u_arg[0];
583 addr = tcp->u_arg[1];
584 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
585 if (umove(tcp, addr, &arg) < 0)
590 tcp->u_nargs = sysent[tcp->scno].nargs;
593 mask = (tcp->u_arg[0] >> 8) & 0xff;
594 for (i = 0; mask; i++)
598 tcp->u_arg[0] &= 0xff;
599 tcp->scno = subcall + i;
600 if (sysent[tcp->scno].nargs != -1)
601 tcp->u_nargs = sysent[tcp->scno].nargs;
605 * Oh, yuck. The call code is the *sixth* argument.
606 * (don't you mean the *last* argument? - JH)
608 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
610 tcp->scno = subcall + tcp->u_arg[5];
611 if (sysent[tcp->scno].nargs != -1)
612 tcp->u_nargs = sysent[tcp->scno].nargs;
618 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
619 if (subcalls_table[i].call == tcp->scno) break;
620 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
621 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
622 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
623 for (i = 0; i < tcp->u_nargs; i++)
624 tcp->u_arg[i] = tcp->u_arg[i + 1];
632 struct tcb *tcp_last = NULL;
635 internal_syscall(tcp)
639 * We must always trace a few critical system calls in order to
640 * correctly support following forks in the presence of tracing
643 switch (tcp->scno + NR_SYSCALL_BASE) {
704 internal_wait(tcp, 2);
708 internal_wait(tcp, 3);
718 #ifdef __NR_exit_group
719 case __NR_exit_group:
722 case 252: /* IA-32 __NR_exit_group */
737 #elif defined (POWERPC)
738 static long result,flags;
742 static struct pt_regs regs;
743 #elif defined (ALPHA)
746 #elif defined (SPARC) || defined (SPARC64)
747 static struct regs regs;
748 static unsigned long trap;
752 #elif defined(S390) || defined(S390X)
755 static long syscall_mode;
762 #elif defined(X86_64)
780 #if defined(S390) || defined(S390X)
781 if (tcp->flags & TCB_WAITEXECVE) {
783 * When the execve system call completes successfully, the
784 * new process still has -ENOSYS (old style) or __NR_execve
785 * (new style) in gpr2. We cannot recover the scno again
786 * by disassembly, because the image that executed the
787 * syscall is gone now. Fortunately, we don't want it. We
788 * leave the flag set so that syscall_fixup can fake the
791 if (tcp->flags & TCB_INSYSCALL)
794 * This is the SIGTRAP after execve. We cannot try to read
795 * the system call here either.
797 tcp->flags &= ~TCB_WAITEXECVE;
801 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
804 if (syscall_mode != -ENOSYS) {
806 * Since kernel version 2.5.44 the scno gets passed in gpr2.
811 * Old style of "passing" the scno via the SVC instruction.
814 long opcode, offset_reg, tmp;
816 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
817 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
818 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
819 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
821 if (upeek(pid, PT_PSWADDR, &pc) < 0)
824 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
826 perror("peektext(pc-oneword)");
831 * We have to check if the SVC got executed directly or via an
832 * EXECUTE instruction. In case of EXECUTE it is necessary to do
833 * instruction decoding to derive the system call number.
834 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
835 * so that this doesn't work if a SVC opcode is part of an EXECUTE
836 * opcode. Since there is no way to find out the opcode size this
837 * is the best we can do...
840 if ((opcode & 0xff00) == 0x0a00) {
842 scno = opcode & 0xff;
845 /* SVC got executed by EXECUTE instruction */
848 * Do instruction decoding of EXECUTE. If you really want to
849 * understand this, read the Principles of Operations.
851 svc_addr = (void *) (opcode & 0xfff);
854 offset_reg = (opcode & 0x000f0000) >> 16;
855 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
860 offset_reg = (opcode & 0x0000f000) >> 12;
861 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
865 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
874 offset_reg = (opcode & 0x00f00000) >> 20;
875 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
878 scno = (scno | tmp) & 0xff;
881 #elif defined (POWERPC)
882 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
884 if (!(tcp->flags & TCB_INSYSCALL)) {
885 /* Check if we return from execve. */
886 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
887 tcp->flags &= ~TCB_WAITEXECVE;
892 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
894 #elif defined (X86_64)
895 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
898 if (!(tcp->flags & TCB_INSYSCALL)) {
899 static int currpers=-1;
902 /* Check CS register value. On x86-64 linux it is:
903 * 0x33 for long mode (64 bit)
904 * 0x23 for compatibility mode (32 bit)
905 * It takes only one ptrace and thus doesn't need
908 if (upeek(pid, 8*CS, &val) < 0)
912 case 0x23: currpers = 1; break;
913 case 0x33: currpers = 0; break;
915 fprintf(stderr, "Unknown value CS=0x%02X while "
916 "detecting personality of process "
917 "PID=%d\n", (int)val, pid);
918 currpers = current_personality;
922 /* This version analyzes the opcode of a syscall instruction.
923 * (int 0x80 on i386 vs. syscall on x86-64)
924 * It works, but is too complicated.
926 unsigned long val, rip, i;
928 if(upeek(pid, 8*RIP, &rip)<0)
929 perror("upeek(RIP)");
931 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
935 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
937 printf("ptrace_peektext failed: %s\n",
939 switch (call & 0xffff)
941 /* x86-64: syscall = 0x0f 0x05 */
942 case 0x050f: currpers = 0; break;
943 /* i386: int 0x80 = 0xcd 0x80 */
944 case 0x80cd: currpers = 1; break;
946 currpers = current_personality;
948 "Unknown syscall opcode (0x%04X) while "
949 "detecting personality of process "
950 "PID=%d\n", (int)call, pid);
954 if(currpers != current_personality)
956 char *names[]={"64 bit", "32 bit"};
957 set_personality(currpers);
958 printf("[ Process PID=%d runs in %s mode. ]\n",
959 pid, names[current_personality]);
963 # define IA64_PSR_IS ((long)1 << 34)
964 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
965 ia32 = (psr & IA64_PSR_IS) != 0;
966 if (!(tcp->flags & TCB_INSYSCALL)) {
968 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
971 if (upeek (pid, PT_R15, &scno) < 0)
974 /* Check if we return from execve. */
975 if (tcp->flags & TCB_WAITEXECVE) {
976 tcp->flags &= ~TCB_WAITEXECVE;
980 /* syscall in progress */
981 if (upeek (pid, PT_R8, &r8) < 0)
983 if (upeek (pid, PT_R10, &r10) < 0)
988 * Read complete register set in one go.
990 if (ptrace(PTRACE_GETREGS, pid, NULL, (void *)®s) == -1)
994 * We only need to grab the syscall number on syscall entry.
996 if (regs.ARM_ip == 0) {
998 * Note: we only deal with only 32-bit CPUs here.
1000 if (regs.ARM_cpsr & 0x20) {
1002 * Get the Thumb-mode system call number
1007 * Get the ARM-mode system call number
1010 scno = ptrace(PTRACE_PEEKTEXT, pid, (void *)(regs.ARM_pc - 4), NULL);
1014 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
1015 tcp->flags &= ~TCB_WAITEXECVE;
1019 if ((scno & 0x0ff00000) != 0x0f900000) {
1020 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
1026 * Fixup the syscall number
1031 if (tcp->flags & TCB_INSYSCALL) {
1032 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
1033 tcp->flags &= ~TCB_INSYSCALL;
1036 if (!(tcp->flags & TCB_INSYSCALL)) {
1037 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1038 tcp->flags |= TCB_INSYSCALL;
1041 #elif defined (M68K)
1042 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
1044 #elif defined (MIPS)
1045 if (upeek(pid, REG_A3, &a3) < 0)
1048 if(!(tcp->flags & TCB_INSYSCALL)) {
1049 if (upeek(pid, REG_V0, &scno) < 0)
1052 if (scno < 0 || scno > nsyscalls) {
1053 if(a3 == 0 || a3 == -1) {
1055 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
1060 if (upeek(pid, REG_V0, &r2) < 0)
1063 #elif defined (ALPHA)
1064 if (upeek(pid, REG_A3, &a3) < 0)
1067 if (!(tcp->flags & TCB_INSYSCALL)) {
1068 if (upeek(pid, REG_R0, &scno) < 0)
1071 /* Check if we return from execve. */
1072 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1073 tcp->flags &= ~TCB_WAITEXECVE;
1078 * Do some sanity checks to figure out if it's
1079 * really a syscall entry
1081 if (scno < 0 || scno > nsyscalls) {
1082 if (a3 == 0 || a3 == -1) {
1084 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
1090 if (upeek(pid, REG_R0, &r0) < 0)
1093 #elif defined (SPARC) || defined (SPARC64)
1094 /* Everything we need is in the current register set. */
1095 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
1098 /* If we are entering, then disassemble the syscall trap. */
1099 if (!(tcp->flags & TCB_INSYSCALL)) {
1100 /* Retrieve the syscall trap instruction. */
1102 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
1103 #if defined(SPARC64)
1109 /* Disassemble the trap to see what personality to use. */
1112 /* Linux/SPARC syscall trap. */
1116 /* Linux/SPARC64 syscall trap. */
1120 /* SunOS syscall trap. (pers 1) */
1121 fprintf(stderr,"syscall: SunOS no support\n");
1124 /* Solaris 2.x syscall trap. (per 2) */
1128 /* NetBSD/FreeBSD syscall trap. */
1129 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1132 /* Solaris 2.x gettimeofday */
1136 /* Unknown syscall trap. */
1137 if(tcp->flags & TCB_WAITEXECVE) {
1138 tcp->flags &= ~TCB_WAITEXECVE;
1141 #if defined (SPARC64)
1142 fprintf(stderr,"syscall: unknown syscall trap %08lx %016lx\n", trap, regs.r_tpc);
1144 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
1149 /* Extract the system call number from the registers. */
1150 if (trap == 0x91d02027)
1156 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
1160 if (upeek(pid, PT_GR20, &scno) < 0)
1162 if (!(tcp->flags & TCB_INSYSCALL)) {
1163 /* Check if we return from execve. */
1164 if ((tcp->flags & TCB_WAITEXECVE)) {
1165 tcp->flags &= ~TCB_WAITEXECVE;
1171 * In the new syscall ABI, the system call number is in R3.
1173 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1177 /* Odd as it may seem, a glibc bug has been known to cause
1178 glibc to issue bogus negative syscall numbers. So for
1179 our purposes, make strace print what it *should* have been */
1180 long correct_scno = (scno & 0xff);
1183 "Detected glibc bug: bogus system call number = %ld, "
1184 "correcting to %ld\n",
1187 scno = correct_scno;
1191 if (!(tcp->flags & TCB_INSYSCALL)) {
1192 /* Check if we return from execve. */
1193 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1194 tcp->flags &= ~TCB_WAITEXECVE;
1199 if (upeek(pid, REG_SYSCALL, &scno) < 0)
1203 if (!(tcp->flags & TCB_INSYSCALL)) {
1204 /* Check if we return from execve. */
1205 if (tcp->flags & TCB_WAITEXECVE) {
1206 tcp->flags &= ~TCB_WAITEXECVE;
1213 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1216 /* new syscall ABI returns result in R0 */
1217 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1220 /* ABI defines result returned in r9 */
1221 if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
1226 #ifdef HAVE_PR_SYSCALL
1227 scno = tcp->status.PR_SYSCALL;
1228 #else /* !HAVE_PR_SYSCALL */
1230 scno = tcp->status.PR_WHAT;
1232 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1236 switch (regs.r_eax) {
1239 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1245 #endif /* FREEBSD */
1246 #endif /* !HAVE_PR_SYSCALL */
1247 #endif /* USE_PROCFS */
1248 if (!(tcp->flags & TCB_INSYSCALL))
1260 #else /* USE_PROCFS */
1261 int scno = tcp->scno;
1263 if (!(tcp->flags & TCB_INSYSCALL)) {
1264 if (tcp->status.PR_WHY != PR_SYSENTRY) {
1268 || scno == SYS_vfork
1269 #endif /* SYS_vfork */
1271 || scno == SYS_fork1
1272 #endif /* SYS_fork1 */
1274 || scno == SYS_forkall
1275 #endif /* SYS_forkall */
1277 || scno == SYS_rfork1
1278 #endif /* SYS_fork1 */
1280 || scno == SYS_rforkall
1281 #endif /* SYS_rforkall */
1283 /* We are returning in the child, fake it. */
1284 tcp->status.PR_WHY = PR_SYSENTRY;
1286 tcp->status.PR_WHY = PR_SYSEXIT;
1289 fprintf(stderr, "syscall: missing entry\n");
1290 tcp->flags |= TCB_INSYSCALL;
1295 if (tcp->status.PR_WHY != PR_SYSEXIT) {
1296 fprintf(stderr, "syscall: missing exit\n");
1297 tcp->flags &= ~TCB_INSYSCALL;
1300 #endif /* USE_PROCFS */
1302 if (!(tcp->flags & TCB_INSYSCALL)) {
1304 fprintf(stderr, "syscall: missing entry\n");
1305 tcp->flags |= TCB_INSYSCALL;
1312 * This happens when a signal handler
1313 * for a signal which interrupted a
1314 * a system call makes another system call.
1316 fprintf(stderr, "syscall: missing exit\n");
1318 tcp->flags &= ~TCB_INSYSCALL;
1324 if (upeek(pid, 4*EAX, &eax) < 0)
1326 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1328 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1331 #elif defined (X86_64)
1332 if (upeek(pid, 8*RAX, &rax) < 0)
1334 if (current_personality == 1)
1335 rax = (long int)(int)rax; /* sign extend from 32 bits */
1336 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1338 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1341 #elif defined (S390) || defined (S390X)
1342 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1344 if (syscall_mode != -ENOSYS)
1345 syscall_mode = tcp->scno;
1346 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
1348 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1351 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1352 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1353 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1355 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1356 * flag set for the post-execve SIGTRAP to see and reset.
1360 #elif defined (POWERPC)
1361 # define SO_MASK 0x10000000
1362 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1364 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
1366 if (flags & SO_MASK)
1368 #elif defined (M68K)
1369 if (upeek(pid, 4*PT_D0, &d0) < 0)
1371 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1373 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1380 #elif defined (HPPA)
1381 if (upeek(pid, PT_GR28, &r28) < 0)
1384 if (upeek(pid, PT_R10, &r10) < 0)
1386 if (upeek(pid, PT_R8, &r8) < 0)
1388 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1390 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1404 #if defined(S390) || defined(S390X)
1405 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1413 #else /* !S390 && !S390X */
1415 if (eax < 0 && -eax < nerrnos) {
1425 if (rax < 0 && -rax < nerrnos) {
1439 if (err < 0 && -err < nerrnos) {
1467 if (result && (unsigned long) -result < nerrnos) {
1472 tcp->u_rval = result;
1475 #else /* !POWERPC */
1477 if (d0 && (unsigned) -d0 < nerrnos) {
1487 if (regs.ARM_r0 && (unsigned) -regs.ARM_r0 < nerrnos) {
1489 u_error = -regs.ARM_r0;
1492 tcp->u_rval = regs.ARM_r0;
1507 if (regs.r_psr & PSR_C) {
1509 u_error = regs.r_o0;
1512 tcp->u_rval = regs.r_o0;
1517 if (regs.r_tstate & 0x1100000000UL) {
1519 u_error = regs.r_o0;
1522 tcp->u_rval = regs.r_o0;
1525 #else /* !SPARC64 */
1527 if (r28 && (unsigned) -r28 < nerrnos) {
1537 /* interpret R0 as return value or error number */
1538 if (r0 && (unsigned) -r0 < nerrnos) {
1548 /* interpret result as return value or error number */
1549 if (r9 && (unsigned) -r9 < nerrnos) {
1561 #endif /* SPARC64 */
1565 #endif /* POWERPC */
1570 #endif /* S390 || S390X */
1573 /* get error code from user struct */
1574 if (upeek(pid, uoff(u_error), &u_error) < 0)
1576 u_error >>= 24; /* u_error is a char */
1578 /* get system call return value */
1579 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1584 /* Judicious guessing goes a long way. */
1585 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1587 u_error = tcp->status.pr_reg[R_O0];
1590 tcp->u_rval = tcp->status.pr_reg[R_O0];
1595 /* Wanna know how to kill an hour single-stepping? */
1596 if (tcp->status.PR_REG[EFL] & 0x1) {
1598 u_error = tcp->status.PR_REG[EAX];
1601 tcp->u_rval = tcp->status.PR_REG[EAX];
1602 #ifdef HAVE_LONG_LONG
1604 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1605 tcp->status.PR_REG[EAX];
1611 /* Wanna know how to kill an hour single-stepping? */
1612 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1614 u_error = tcp->status.PR_REG[RAX];
1617 tcp->u_rval = tcp->status.PR_REG[RAX];
1622 if (tcp->status.pr_reg[CTX_A3]) {
1624 u_error = tcp->status.pr_reg[CTX_V0];
1627 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1633 if (regs.r_eflags & PSL_C) {
1635 u_error = regs.r_eax;
1637 tcp->u_rval = regs.r_eax;
1639 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1642 #endif /* FREEBSD */
1643 tcp->u_error = u_error;
1648 force_result(tcp, error, rval)
1654 #if defined(S390) || defined(S390X)
1655 gpr2 = error ? -error : rval;
1656 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1658 #else /* !S390 && !S390X */
1660 eax = error ? -error : rval;
1661 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1665 rax = error ? -error : rval;
1666 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 8), rax) < 0)
1671 r8 = error ? -error : rval;
1672 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1684 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1685 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1698 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1699 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1703 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1713 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1714 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
1716 #else /* !POWERPC */
1718 d0 = error ? -error : rval;
1719 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1723 regs.ARM_r0 = error ? -error : rval;
1724 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), regs.ARM_r0) < 0)
1736 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1737 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1741 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1744 regs.r_psr |= PSR_C;
1748 regs.r_psr &= ~PSR_C;
1751 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1755 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1758 regs.r_tstate |= 0x1100000000UL;
1762 regs.r_tstate &= ~0x1100000000UL;
1765 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1767 #else /* !SPARC64 */
1769 r28 = error ? -error : rval;
1770 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1774 r0 = error ? -error : rval;
1775 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1779 r9 = error ? -error : rval;
1780 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1786 #endif /* SPARC64 */
1790 #endif /* POWERPC */
1795 #endif /* S390 || S390X */
1798 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1800 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1808 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1813 regs.r_eflags |= PSL_C;
1817 regs.r_eflags &= ~PSL_C;
1820 if (pwrite(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1824 #endif /* FREEBSD */
1826 /* All branches reach here on success (only). */
1827 tcp->u_error = error;
1832 int syscall_enter(tcp)
1837 #endif /* !USE_PROCFS */
1839 #if defined(S390) || defined(S390X)
1842 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1843 tcp->u_nargs = sysent[tcp->scno].nargs;
1845 tcp->u_nargs = MAX_ARGS;
1846 for (i = 0; i < tcp->u_nargs; i++) {
1847 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
1851 #elif defined (ALPHA)
1854 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1855 tcp->u_nargs = sysent[tcp->scno].nargs;
1857 tcp->u_nargs = MAX_ARGS;
1858 for (i = 0; i < tcp->u_nargs; i++) {
1859 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1860 * for scno somewhere above here!
1862 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1866 #elif defined (IA64)
1869 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1870 /* be backwards compatible with kernel < 2.4.4... */
1872 # define PT_RBS_END PT_AR_BSP
1875 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
1877 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1880 sof = (cfm >> 0) & 0x7f;
1881 sol = (cfm >> 7) & 0x7f;
1882 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1884 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1885 && sysent[tcp->scno].nargs != -1)
1886 tcp->u_nargs = sysent[tcp->scno].nargs;
1888 tcp->u_nargs = MAX_ARGS;
1889 for (i = 0; i < tcp->u_nargs; ++i) {
1890 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1891 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1897 if (/* EBX = out0 */
1898 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1900 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1902 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1904 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1906 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1908 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1911 for (i = 0; i < 6; ++i)
1912 /* truncate away IVE sign-extension */
1913 tcp->u_arg[i] &= 0xffffffff;
1915 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1916 && sysent[tcp->scno].nargs != -1)
1917 tcp->u_nargs = sysent[tcp->scno].nargs;
1922 #elif defined (MIPS)
1927 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1928 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1930 nargs = tcp->u_nargs = MAX_ARGS;
1932 if(upeek(pid, REG_SP, &sp) < 0)
1934 for(i = 0; i < 4; i++) {
1935 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1938 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1939 (char *)(tcp->u_arg + 4));
1941 for(i = 0; i < nargs; i++) {
1942 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1947 #elif defined (POWERPC)
1949 #define PT_ORIG_R3 34
1953 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1954 tcp->u_nargs = sysent[tcp->scno].nargs;
1956 tcp->u_nargs = MAX_ARGS;
1957 for (i = 0; i < tcp->u_nargs; i++) {
1958 if (upeek(pid, (i==0) ?
1959 (sizeof(unsigned long)*PT_ORIG_R3) :
1960 ((i+PT_R3)*sizeof(unsigned long)),
1961 &tcp->u_arg[i]) < 0)
1965 #elif defined (SPARC) || defined (SPARC64)
1969 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1970 tcp->u_nargs = sysent[tcp->scno].nargs;
1972 tcp->u_nargs = MAX_ARGS;
1973 for (i = 0; i < tcp->u_nargs; i++)
1974 tcp->u_arg[i] = *((®s.r_o0) + i);
1976 #elif defined (HPPA)
1980 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1981 tcp->u_nargs = sysent[tcp->scno].nargs;
1983 tcp->u_nargs = MAX_ARGS;
1984 for (i = 0; i < tcp->u_nargs; i++) {
1985 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1993 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1994 tcp->u_nargs = sysent[tcp->scno].nargs;
1996 tcp->u_nargs = MAX_ARGS;
1997 for (i = 0; i < tcp->u_nargs; i++)
1998 tcp->u_arg[i] = regs.uregs[i];
2003 static int syscall_regs[] = {
2004 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
2005 REG_REG0, REG_REG0+1, REG_REG0+2
2008 tcp->u_nargs = sysent[tcp->scno].nargs;
2009 for (i = 0; i < tcp->u_nargs; i++) {
2010 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
2017 /* Registers used by SH5 Linux system calls for parameters */
2018 static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
2021 * TODO: should also check that the number of arguments encoded
2022 * in the trap number matches the number strace expects.
2025 assert(sysent[tcp->scno].nargs <
2026 sizeof(syscall_regs)/sizeof(syscall_regs[0]));
2029 tcp->u_nargs = sysent[tcp->scno].nargs;
2030 for (i = 0; i < tcp->u_nargs; i++) {
2031 if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
2036 #elif defined(X86_64)
2039 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
2040 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
2041 {RBX,RCX,RDX,RSI,RDI,RBP} /* i386 ABI */
2044 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2045 tcp->u_nargs = sysent[tcp->scno].nargs;
2047 tcp->u_nargs = MAX_ARGS;
2048 for (i = 0; i < tcp->u_nargs; i++) {
2049 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
2053 #else /* Other architecture (like i386) (32bits specific) */
2056 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2057 tcp->u_nargs = sysent[tcp->scno].nargs;
2059 tcp->u_nargs = MAX_ARGS;
2060 for (i = 0; i < tcp->u_nargs; i++) {
2061 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
2070 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2071 tcp->u_nargs = sysent[tcp->scno].nargs;
2073 tcp->u_nargs = MAX_ARGS;
2074 for (i = 0; i < tcp->u_nargs; i++) {
2077 if (upeek(pid, uoff(u_arg[0]) +
2078 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
2086 * SGI is broken: even though it has pr_sysarg, it doesn't
2087 * set them on system call entry. Get a clue.
2089 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2090 tcp->u_nargs = sysent[tcp->scno].nargs;
2092 tcp->u_nargs = tcp->status.pr_nsysarg;
2093 if (tcp->u_nargs > 4) {
2094 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2095 4*sizeof(tcp->u_arg[0]));
2096 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
2097 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
2100 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2101 tcp->u_nargs*sizeof(tcp->u_arg[0]));
2105 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
2107 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2108 tcp->u_nargs = sysent[tcp->scno].nargs;
2110 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
2111 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2112 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2113 #elif defined (HAVE_PR_SYSCALL)
2114 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2115 tcp->u_nargs = sysent[tcp->scno].nargs;
2117 tcp->u_nargs = tcp->status.pr_nsysarg;
2120 for (i = 0; i < tcp->u_nargs; i++)
2121 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
2123 #elif defined (I386)
2124 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2125 tcp->u_nargs = sysent[tcp->scno].nargs;
2128 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2129 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2131 I DONT KNOW WHAT TO DO
2132 #endif /* !HAVE_PR_SYSCALL */
2135 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2136 sysent[tcp->scno].nargs > tcp->status.val)
2137 tcp->u_nargs = sysent[tcp->scno].nargs;
2139 tcp->u_nargs = tcp->status.val;
2140 if (tcp->u_nargs < 0)
2142 if (tcp->u_nargs > MAX_ARGS)
2143 tcp->u_nargs = MAX_ARGS;
2144 switch(regs.r_eax) {
2146 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2147 regs.r_esp + sizeof(int) + sizeof(quad_t));
2150 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2151 regs.r_esp + 2 * sizeof(int));
2154 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2155 regs.r_esp + sizeof(int));
2158 #endif /* FREEBSD */
2170 /* Measure the exit time as early as possible to avoid errors. */
2171 if (dtime && (tcp->flags & TCB_INSYSCALL))
2172 gettimeofday(&tv, NULL);
2174 res = get_scno(tcp);
2178 res = syscall_fixup(tcp);
2182 if (tcp->flags & TCB_INSYSCALL) {
2184 res = get_error(tcp);
2188 internal_syscall(tcp);
2189 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2190 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2191 tcp->flags &= ~TCB_INSYSCALL;
2195 if (tcp->flags & TCB_REPRINT) {
2198 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2199 tprintf("syscall_%lu", tcp->scno);
2201 tprintf("%s", sysent[tcp->scno].sys_name);
2202 tprintf(" resumed> ");
2205 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
2206 if (counts == NULL) {
2207 counts = calloc(sizeof *counts, nsyscalls);
2208 if (counts == NULL) {
2210 strace: out of memory for call counts\n");
2215 counts[tcp->scno].calls++;
2217 counts[tcp->scno].errors++;
2218 tv_sub(&tv, &tv, &tcp->etime);
2220 if (tv_cmp(&tv, &tcp->dtime) > 0) {
2221 static struct timeval one_tick;
2222 if (one_tick.tv_usec == 0) {
2223 /* Initialize it. */
2224 struct itimerval it;
2225 memset(&it, 0, sizeof it);
2226 it.it_interval.tv_usec = 1;
2227 setitimer(ITIMER_REAL, &it, NULL);
2228 getitimer(ITIMER_REAL, &it);
2229 one_tick = it.it_interval;
2232 if (tv_nz(&tcp->dtime))
2234 else if (tv_cmp(&tv, &one_tick) > 0) {
2235 if (tv_cmp(&shortest, &one_tick) < 0)
2242 if (tv_cmp(&tv, &shortest) < 0)
2244 tv_add(&counts[tcp->scno].time,
2245 &counts[tcp->scno].time, &tv);
2246 tcp->flags &= ~TCB_INSYSCALL;
2250 if (tcp->scno >= nsyscalls || tcp->scno < 0
2251 || (qual_flags[tcp->scno] & QUAL_RAW))
2252 sys_res = printargs(tcp);
2254 if (not_failing_only && tcp->u_error)
2255 return 0; /* ignore failed syscalls */
2256 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2258 u_error = tcp->u_error;
2261 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2262 qual_flags[tcp->scno] & QUAL_RAW) {
2264 tprintf("= -1 (errno %ld)", u_error);
2266 tprintf("= %#lx", tcp->u_rval);
2268 else if (!(sys_res & RVAL_NONE) && u_error) {
2272 tprintf("= ? ERESTARTSYS (To be restarted)");
2274 case ERESTARTNOINTR:
2275 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2277 case ERESTARTNOHAND:
2278 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2280 case ERESTART_RESTARTBLOCK:
2281 tprintf("= ? ERESTART_RESTARTBLOCK (To be restarted)");
2287 tprintf("E??? (errno %ld)", u_error);
2288 else if (u_error < nerrnos)
2289 tprintf("%s (%s)", errnoent[u_error],
2292 tprintf("ERRNO_%ld (%s)", u_error,
2298 if (sys_res & RVAL_NONE)
2301 switch (sys_res & RVAL_MASK) {
2303 tprintf("= %#lx", tcp->u_rval);
2306 tprintf("= %#lo", tcp->u_rval);
2309 tprintf("= %lu", tcp->u_rval);
2312 tprintf("= %ld", tcp->u_rval);
2314 #ifdef HAVE_LONG_LONG
2316 tprintf("= %#llx", tcp->u_lrval);
2319 tprintf("= %#llo", tcp->u_lrval);
2321 case RVAL_LUDECIMAL:
2322 tprintf("= %llu", tcp->u_lrval);
2325 tprintf("= %lld", tcp->u_lrval);
2330 "invalid rval format\n");
2334 if ((sys_res & RVAL_STR) && tcp->auxstr)
2335 tprintf(" (%s)", tcp->auxstr);
2338 tv_sub(&tv, &tv, &tcp->etime);
2339 tprintf(" <%ld.%06ld>",
2340 (long) tv.tv_sec, (long) tv.tv_usec);
2345 if (fflush(tcp->outf) == EOF)
2347 tcp->flags &= ~TCB_INSYSCALL;
2351 /* Entering system call */
2352 res = syscall_enter(tcp);
2356 switch (tcp->scno + NR_SYSCALL_BASE) {
2358 #if !defined (ALPHA) && !defined(SPARC) && !defined(SPARC64) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
2359 case SYS_socketcall:
2360 decode_subcall(tcp, SYS_socket_subcall,
2361 SYS_socket_nsubcalls, deref_style);
2364 decode_subcall(tcp, SYS_ipc_subcall,
2365 SYS_ipc_nsubcalls, shift_style);
2367 #endif /* !ALPHA && !MIPS && !SPARC && !SPARC64 && !HPPA && !X86_64 */
2368 #if defined (SPARC) || defined (SPARC64)
2369 case SYS_socketcall:
2370 sparc_socket_decode (tcp);
2375 #ifdef SYS_pgrpsys_subcall
2377 decode_subcall(tcp, SYS_pgrpsys_subcall,
2378 SYS_pgrpsys_nsubcalls, shift_style);
2380 #endif /* SYS_pgrpsys_subcall */
2381 #ifdef SYS_sigcall_subcall
2383 decode_subcall(tcp, SYS_sigcall_subcall,
2384 SYS_sigcall_nsubcalls, mask_style);
2386 #endif /* SYS_sigcall_subcall */
2388 decode_subcall(tcp, SYS_msgsys_subcall,
2389 SYS_msgsys_nsubcalls, shift_style);
2392 decode_subcall(tcp, SYS_shmsys_subcall,
2393 SYS_shmsys_nsubcalls, shift_style);
2396 decode_subcall(tcp, SYS_semsys_subcall,
2397 SYS_semsys_nsubcalls, shift_style);
2401 decode_subcall(tcp, SYS_utssys_subcall,
2402 SYS_utssys_nsubcalls, shift_style);
2406 decode_subcall(tcp, SYS_sysfs_subcall,
2407 SYS_sysfs_nsubcalls, shift_style);
2410 decode_subcall(tcp, SYS_spcall_subcall,
2411 SYS_spcall_nsubcalls, shift_style);
2413 #ifdef SYS_context_subcall
2415 decode_subcall(tcp, SYS_context_subcall,
2416 SYS_context_nsubcalls, shift_style);
2418 #endif /* SYS_context_subcall */
2419 #ifdef SYS_door_subcall
2421 decode_subcall(tcp, SYS_door_subcall,
2422 SYS_door_nsubcalls, door_style);
2424 #endif /* SYS_door_subcall */
2425 #ifdef SYS_kaio_subcall
2427 decode_subcall(tcp, SYS_kaio_subcall,
2428 SYS_kaio_nsubcalls, shift_style);
2436 decode_subcall(tcp, 0, 0, table_style);
2441 decode_subcall(tcp, SYS_semsys_subcall,
2442 SYS_semsys_nsubcalls, shift_style);
2445 decode_subcall(tcp, SYS_msgsys_subcall,
2446 SYS_msgsys_nsubcalls, shift_style);
2449 decode_subcall(tcp, SYS_shmsys_subcall,
2450 SYS_shmsys_nsubcalls, shift_style);
2455 internal_syscall(tcp);
2456 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2457 tcp->flags |= TCB_INSYSCALL;
2462 gettimeofday(&tcp->etime, NULL);
2463 tcp->flags |= TCB_INSYSCALL;
2468 tcp->flags &= ~TCB_REPRINT;
2470 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2471 tprintf("syscall_%lu(", tcp->scno);
2473 tprintf("%s(", sysent[tcp->scno].sys_name);
2474 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2475 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2476 sys_res = printargs(tcp);
2478 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2479 if (fflush(tcp->outf) == EOF)
2481 tcp->flags |= TCB_INSYSCALL;
2482 /* Measure the entrance time as late as possible to avoid errors. */
2484 gettimeofday(&tcp->etime, NULL);
2492 if (entering(tcp)) {
2495 for (i = 0; i < tcp->u_nargs; i++)
2496 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2508 #if defined (SPARC) || defined (SPARC64)
2510 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
2514 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2516 #endif /* SPARC || SPARC64 */
2518 if (upeek(tcp->pid, PT_R9, &val) < 0)
2523 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2529 val = tcp->status.PR_REG[R_O1];
2532 val = tcp->status.PR_REG[EDX];
2535 val = tcp->status.PR_REG[RDX];
2538 val = tcp->status.PR_REG[CTX_V1];
2543 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
2550 * Apparently, indirect system calls have already be converted by ptrace(2),
2551 * so if you see "indir" this program has gone astray.
2559 if (entering(tcp)) {
2560 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2561 fprintf(stderr, "Bogus syscall: %u\n", scno);
2564 nargs = sysent[scno].nargs;
2565 tprintf("%s", sysent[scno].sys_name);
2566 for (i = 0; i < nargs; i++)
2567 tprintf(", %#lx", tcp->u_arg[i+1]);
2577 return -tv_cmp(&counts[*((int *) a)].time, &counts[*((int *) b)].time);
2585 return strcmp(sysent[*((int *) a)].sys_name,
2586 sysent[*((int *) b)].sys_name);
2594 int m = counts[*((int *) a)].calls, n = counts[*((int *) b)].calls;
2596 return (m < n) ? 1 : (m > n) ? -1 : 0;
2599 static int (*sortfun)();
2600 static struct timeval overhead = { -1, -1 };
2606 if (strcmp(sortby, "time") == 0)
2608 else if (strcmp(sortby, "calls") == 0)
2609 sortfun = count_cmp;
2610 else if (strcmp(sortby, "name") == 0)
2611 sortfun = syscall_cmp;
2612 else if (strcmp(sortby, "nothing") == 0)
2615 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2620 void set_overhead(n)
2623 overhead.tv_sec = n / 1000000;
2624 overhead.tv_usec = n % 1000000;
2632 int call_cum, error_cum;
2633 struct timeval tv_cum, dtv;
2635 char *dashes = "-------------------------";
2638 int *sorted_count = malloc(nsyscalls * sizeof(int));
2640 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2641 if (overhead.tv_sec == -1) {
2642 tv_mul(&overhead, &shortest, 8);
2643 tv_div(&overhead, &overhead, 10);
2645 for (i = 0; i < nsyscalls; i++) {
2646 sorted_count[i] = i;
2647 if (counts == NULL || counts[i].calls == 0)
2649 tv_mul(&dtv, &overhead, counts[i].calls);
2650 tv_sub(&counts[i].time, &counts[i].time, &dtv);
2651 call_cum += counts[i].calls;
2652 error_cum += counts[i].errors;
2653 tv_add(&tv_cum, &tv_cum, &counts[i].time);
2656 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2657 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2658 "% time", "seconds", "usecs/call",
2659 "calls", "errors", "syscall");
2660 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2661 dashes, dashes, dashes, dashes, dashes, dashes);
2663 for (i = 0; i < nsyscalls; i++) {
2664 j = sorted_count[i];
2665 if (counts[j].calls == 0)
2667 tv_div(&dtv, &counts[j].time, counts[j].calls);
2668 if (counts[j].errors)
2669 sprintf(error_str, "%d", counts[j].errors);
2671 error_str[0] = '\0';
2672 percent = (100.0 * tv_float(&counts[j].time)
2673 / tv_float(&tv_cum));
2674 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2675 percent, (long) counts[j].time.tv_sec,
2676 (long) counts[j].time.tv_usec,
2677 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2679 error_str, sysent[j].sys_name);
2684 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2685 dashes, dashes, dashes, dashes, dashes, dashes);
2687 sprintf(error_str, "%d", error_cum);
2689 error_str[0] = '\0';
2690 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2691 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2692 call_cum, error_str, "total");