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 TD TRACE_DESC
122 #define TF TRACE_FILE
124 #define TN TRACE_NETWORK
125 #define TP TRACE_PROCESS
126 #define TS TRACE_SIGNAL
128 static const struct sysent sysent0[] = {
129 #include "syscallent.h"
131 static const int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
133 #if SUPPORTED_PERSONALITIES >= 2
134 static const struct sysent sysent1[] = {
135 #include "syscallent1.h"
137 static const int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
138 #endif /* SUPPORTED_PERSONALITIES >= 2 */
140 #if SUPPORTED_PERSONALITIES >= 3
141 static const struct sysent sysent2[] = {
142 #include "syscallent2.h"
144 static const int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
145 #endif /* SUPPORTED_PERSONALITIES >= 3 */
147 const struct sysent *sysent;
150 /* Now undef them since short defines cause wicked namespace pollution. */
158 static const char *const errnoent0[] = {
159 #include "errnoent.h"
161 static const int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
163 #if SUPPORTED_PERSONALITIES >= 2
164 static const char *const errnoent1[] = {
165 #include "errnoent1.h"
167 static const int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
168 #endif /* SUPPORTED_PERSONALITIES >= 2 */
170 #if SUPPORTED_PERSONALITIES >= 3
171 static const char *const errnoent2[] = {
172 #include "errnoent2.h"
174 static const int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
175 #endif /* SUPPORTED_PERSONALITIES >= 3 */
177 const char *const *errnoent;
180 int current_personality;
183 set_personality(personality)
186 switch (personality) {
188 errnoent = errnoent0;
191 nsyscalls = nsyscalls0;
192 ioctlent = ioctlent0;
193 nioctlents = nioctlents0;
194 signalent = signalent0;
195 nsignals = nsignals0;
198 #if SUPPORTED_PERSONALITIES >= 2
200 errnoent = errnoent1;
203 nsyscalls = nsyscalls1;
204 ioctlent = ioctlent1;
205 nioctlents = nioctlents1;
206 signalent = signalent1;
207 nsignals = nsignals1;
209 #endif /* SUPPORTED_PERSONALITIES >= 2 */
211 #if SUPPORTED_PERSONALITIES >= 3
213 errnoent = errnoent2;
216 nsyscalls = nsyscalls2;
217 ioctlent = ioctlent2;
218 nioctlents = nioctlents2;
219 signalent = signalent2;
220 nsignals = nsignals2;
222 #endif /* SUPPORTED_PERSONALITIES >= 3 */
228 current_personality = personality;
232 int qual_flags[MAX_QUALS];
240 static struct call_counts *counts;
242 static struct timeval shortest = { 1000000, 0 };
244 static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
246 static const struct qual_options {
252 { QUAL_TRACE, "trace", qual_syscall, "system call" },
253 { QUAL_TRACE, "t", qual_syscall, "system call" },
254 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
255 { QUAL_ABBREV, "a", qual_syscall, "system call" },
256 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
257 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
258 { QUAL_RAW, "raw", qual_syscall, "system call" },
259 { QUAL_RAW, "x", qual_syscall, "system call" },
260 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
261 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
262 { QUAL_SIGNAL, "s", qual_signal, "signal" },
263 { QUAL_FAULT, "fault", qual_fault, "fault" },
264 { QUAL_FAULT, "faults", qual_fault, "fault" },
265 { QUAL_FAULT, "m", qual_fault, "fault" },
266 { QUAL_READ, "read", qual_desc, "descriptor" },
267 { QUAL_READ, "reads", qual_desc, "descriptor" },
268 { QUAL_READ, "r", qual_desc, "descriptor" },
269 { QUAL_WRITE, "write", qual_desc, "descriptor" },
270 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
271 { QUAL_WRITE, "w", qual_desc, "descriptor" },
272 { 0, NULL, NULL, NULL },
276 qualify_one(n, opt, not)
278 const struct qual_options *opt;
282 qual_flags[n] &= ~opt->bitflag;
284 qual_flags[n] |= opt->bitflag;
288 qual_syscall(s, opt, not)
290 const struct qual_options *opt;
296 for (i = 0; i < nsyscalls; i++) {
297 if (strcmp(s, sysent[i].sys_name) == 0) {
298 qualify_one(i, opt, not);
306 qual_signal(s, opt, not)
308 const struct qual_options *opt;
314 if (s && *s && isdigit((unsigned char)*s)) {
316 if (signo < 0 || signo >= MAX_QUALS)
318 qualify_one(signo, opt, not);
321 if (strlen(s) >= sizeof buf)
325 for (i = 0; s[i]; i++)
326 s[i] = toupper((unsigned char)(s[i]));
327 if (strncmp(s, "SIG", 3) == 0)
329 for (i = 0; i <= NSIG; i++)
330 if (strcmp(s, signame(i) + 3) == 0) {
331 qualify_one(i, opt, not);
338 qual_fault(s, opt, not)
340 const struct qual_options *opt;
347 qual_desc(s, opt, not)
349 const struct qual_options *opt;
352 if (s && *s && isdigit((unsigned char)*s)) {
354 if (desc < 0 || desc >= MAX_QUALS)
356 qualify_one(desc, opt, not);
366 if (strcmp(s, "file") == 0)
368 if (strcmp(s, "ipc") == 0)
370 if (strcmp(s, "network") == 0)
371 return TRACE_NETWORK;
372 if (strcmp(s, "process") == 0)
373 return TRACE_PROCESS;
374 if (strcmp(s, "signal") == 0)
376 if (strcmp(s, "desc") == 0)
385 const struct qual_options *opt;
390 opt = &qual_options[0];
391 for (i = 0; (p = qual_options[i].option_name); i++) {
393 if (strncmp(s, p, n) == 0 && s[n] == '=') {
394 opt = &qual_options[i];
404 if (strcmp(s, "none") == 0) {
408 if (strcmp(s, "all") == 0) {
409 for (i = 0; i < MAX_QUALS; i++) {
411 qual_flags[i] &= ~opt->bitflag;
413 qual_flags[i] |= opt->bitflag;
417 for (i = 0; i < MAX_QUALS; i++) {
419 qual_flags[i] |= opt->bitflag;
421 qual_flags[i] &= ~opt->bitflag;
423 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
424 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
425 for (i = 0; i < MAX_QUALS; i++) {
426 if (sysent[i].sys_flags & n) {
428 qual_flags[i] &= ~opt->bitflag;
430 qual_flags[i] |= opt->bitflag;
435 if (opt->qualify(p, opt, not)) {
436 fprintf(stderr, "strace: invalid %s `%s'\n",
437 opt->argument_name, p);
450 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
452 switch (known_scno(tcp)) {
457 #if defined SYS_pread && SYS_pread64 != SYS_pread
462 #elif defined SYS_sub_recv
467 #elif defined SYS_sub_recvfrom
468 case SYS_sub_recvfrom:
470 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
471 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
477 #if defined SYS_pwrite && SYS_pwrite64 != SYS_pwrite
482 #elif defined SYS_sub_send
487 #elif defined SYS_sub_sendto
490 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
491 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
495 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
496 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
502 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
503 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
510 enum subcall_style { shift_style, deref_style, mask_style, door_style };
512 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
520 static const struct subcall subcalls_table[] = {
521 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
523 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
525 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
527 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
531 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
533 static const int socket_map [] = {
536 /* SYS_CONNECT */ 98,
537 /* SYS_LISTEN */ 106,
539 /* SYS_GETSOCKNAME */ 150,
540 /* SYS_GETPEERNAME */ 141,
541 /* SYS_SOCKETPAIR */ 135,
544 /* SYS_SENDTO */ 133,
545 /* SYS_RECVFROM */ 125,
546 /* SYS_SHUTDOWN */ 134,
547 /* SYS_SETSOCKOPT */ 105,
548 /* SYS_GETSOCKOPT */ 118,
549 /* SYS_SENDMSG */ 114,
550 /* SYS_RECVMSG */ 113
553 #if defined (SPARC) || defined (SPARC64)
555 sparc_socket_decode (tcp)
561 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
564 tcp->scno = socket_map [tcp->u_arg [0]-1];
565 n = tcp->u_nargs = sysent [tcp->scno].nargs;
566 addr = tcp->u_arg [1];
567 for (i = 0; i < n; i++){
569 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
571 tcp->u_arg [i] = arg;
572 addr += sizeof (arg);
578 decode_subcall(tcp, subcall, nsubcalls, style)
582 enum subcall_style style;
584 long addr, mask, arg;
589 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
591 tcp->scno = subcall + tcp->u_arg[0];
592 if (sysent[tcp->scno].nargs != -1)
593 tcp->u_nargs = sysent[tcp->scno].nargs;
596 for (i = 0; i < tcp->u_nargs; i++)
597 tcp->u_arg[i] = tcp->u_arg[i + 1];
600 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
602 tcp->scno = subcall + tcp->u_arg[0];
603 addr = tcp->u_arg[1];
604 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
605 if (umove(tcp, addr, &arg) < 0)
610 tcp->u_nargs = sysent[tcp->scno].nargs;
613 mask = (tcp->u_arg[0] >> 8) & 0xff;
614 for (i = 0; mask; i++)
618 tcp->u_arg[0] &= 0xff;
619 tcp->scno = subcall + i;
620 if (sysent[tcp->scno].nargs != -1)
621 tcp->u_nargs = sysent[tcp->scno].nargs;
625 * Oh, yuck. The call code is the *sixth* argument.
626 * (don't you mean the *last* argument? - JH)
628 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
630 tcp->scno = subcall + tcp->u_arg[5];
631 if (sysent[tcp->scno].nargs != -1)
632 tcp->u_nargs = sysent[tcp->scno].nargs;
638 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
639 if (subcalls_table[i].call == tcp->scno) break;
640 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
641 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
642 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
643 for (i = 0; i < tcp->u_nargs; i++)
644 tcp->u_arg[i] = tcp->u_arg[i + 1];
652 struct tcb *tcp_last = NULL;
655 internal_syscall(tcp)
659 * We must always trace a few critical system calls in order to
660 * correctly support following forks in the presence of tracing
663 switch (known_scno(tcp)) {
724 internal_wait(tcp, 2);
728 internal_wait(tcp, 3);
738 #ifdef __NR_exit_group
739 case __NR_exit_group:
742 case 252: /* IA-32 __NR_exit_group */
757 #elif defined (POWERPC)
758 static long result,flags;
762 static struct pt_regs regs;
763 #elif defined (ALPHA)
766 #elif defined (SPARC) || defined (SPARC64)
767 static struct regs regs;
768 static unsigned long trap;
772 #elif defined(S390) || defined(S390X)
775 static long syscall_mode;
782 #elif defined(X86_64)
800 #if defined(S390) || defined(S390X)
801 if (tcp->flags & TCB_WAITEXECVE) {
803 * When the execve system call completes successfully, the
804 * new process still has -ENOSYS (old style) or __NR_execve
805 * (new style) in gpr2. We cannot recover the scno again
806 * by disassembly, because the image that executed the
807 * syscall is gone now. Fortunately, we don't want it. We
808 * leave the flag set so that syscall_fixup can fake the
811 if (tcp->flags & TCB_INSYSCALL)
814 * This is the SIGTRAP after execve. We cannot try to read
815 * the system call here either.
817 tcp->flags &= ~TCB_WAITEXECVE;
821 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
824 if (syscall_mode != -ENOSYS) {
826 * Since kernel version 2.5.44 the scno gets passed in gpr2.
831 * Old style of "passing" the scno via the SVC instruction.
834 long opcode, offset_reg, tmp;
836 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
837 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
838 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
839 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
841 if (upeek(pid, PT_PSWADDR, &pc) < 0)
844 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
846 perror("peektext(pc-oneword)");
851 * We have to check if the SVC got executed directly or via an
852 * EXECUTE instruction. In case of EXECUTE it is necessary to do
853 * instruction decoding to derive the system call number.
854 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
855 * so that this doesn't work if a SVC opcode is part of an EXECUTE
856 * opcode. Since there is no way to find out the opcode size this
857 * is the best we can do...
860 if ((opcode & 0xff00) == 0x0a00) {
862 scno = opcode & 0xff;
865 /* SVC got executed by EXECUTE instruction */
868 * Do instruction decoding of EXECUTE. If you really want to
869 * understand this, read the Principles of Operations.
871 svc_addr = (void *) (opcode & 0xfff);
874 offset_reg = (opcode & 0x000f0000) >> 16;
875 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
880 offset_reg = (opcode & 0x0000f000) >> 12;
881 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
885 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
894 offset_reg = (opcode & 0x00f00000) >> 20;
895 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
898 scno = (scno | tmp) & 0xff;
901 #elif defined (POWERPC)
902 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
904 if (!(tcp->flags & TCB_INSYSCALL)) {
905 /* Check if we return from execve. */
906 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
907 tcp->flags &= ~TCB_WAITEXECVE;
912 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
914 #elif defined (X86_64)
915 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
918 if (!(tcp->flags & TCB_INSYSCALL)) {
919 static int currpers=-1;
922 /* Check CS register value. On x86-64 linux it is:
923 * 0x33 for long mode (64 bit)
924 * 0x23 for compatibility mode (32 bit)
925 * It takes only one ptrace and thus doesn't need
928 if (upeek(pid, 8*CS, &val) < 0)
932 case 0x23: currpers = 1; break;
933 case 0x33: currpers = 0; break;
935 fprintf(stderr, "Unknown value CS=0x%02X while "
936 "detecting personality of process "
937 "PID=%d\n", (int)val, pid);
938 currpers = current_personality;
942 /* This version analyzes the opcode of a syscall instruction.
943 * (int 0x80 on i386 vs. syscall on x86-64)
944 * It works, but is too complicated.
946 unsigned long val, rip, i;
948 if(upeek(pid, 8*RIP, &rip)<0)
949 perror("upeek(RIP)");
951 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
955 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
957 printf("ptrace_peektext failed: %s\n",
959 switch (call & 0xffff)
961 /* x86-64: syscall = 0x0f 0x05 */
962 case 0x050f: currpers = 0; break;
963 /* i386: int 0x80 = 0xcd 0x80 */
964 case 0x80cd: currpers = 1; break;
966 currpers = current_personality;
968 "Unknown syscall opcode (0x%04X) while "
969 "detecting personality of process "
970 "PID=%d\n", (int)call, pid);
974 if(currpers != current_personality)
976 char *names[]={"64 bit", "32 bit"};
977 set_personality(currpers);
978 printf("[ Process PID=%d runs in %s mode. ]\n",
979 pid, names[current_personality]);
983 # define IA64_PSR_IS ((long)1 << 34)
984 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
985 ia32 = (psr & IA64_PSR_IS) != 0;
986 if (!(tcp->flags & TCB_INSYSCALL)) {
988 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
991 if (upeek (pid, PT_R15, &scno) < 0)
994 /* Check if we return from execve. */
995 if (tcp->flags & TCB_WAITEXECVE) {
996 tcp->flags &= ~TCB_WAITEXECVE;
1000 /* syscall in progress */
1001 if (upeek (pid, PT_R8, &r8) < 0)
1003 if (upeek (pid, PT_R10, &r10) < 0)
1008 * Read complete register set in one go.
1010 if (ptrace(PTRACE_GETREGS, pid, NULL, (void *)®s) == -1)
1014 * We only need to grab the syscall number on syscall entry.
1016 if (regs.ARM_ip == 0) {
1018 * Note: we only deal with only 32-bit CPUs here.
1020 if (regs.ARM_cpsr & 0x20) {
1022 * Get the Thumb-mode system call number
1027 * Get the ARM-mode system call number
1030 scno = ptrace(PTRACE_PEEKTEXT, pid, (void *)(regs.ARM_pc - 4), NULL);
1034 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
1035 tcp->flags &= ~TCB_WAITEXECVE;
1039 if ((scno & 0x0ff00000) != 0x0f900000) {
1040 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
1046 * Fixup the syscall number
1051 if (tcp->flags & TCB_INSYSCALL) {
1052 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
1053 tcp->flags &= ~TCB_INSYSCALL;
1056 if (!(tcp->flags & TCB_INSYSCALL)) {
1057 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1058 tcp->flags |= TCB_INSYSCALL;
1061 #elif defined (M68K)
1062 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
1064 #elif defined (MIPS)
1065 if (upeek(pid, REG_A3, &a3) < 0)
1068 if(!(tcp->flags & TCB_INSYSCALL)) {
1069 if (upeek(pid, REG_V0, &scno) < 0)
1072 if (scno < 0 || scno > nsyscalls) {
1073 if(a3 == 0 || a3 == -1) {
1075 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
1080 if (upeek(pid, REG_V0, &r2) < 0)
1083 #elif defined (ALPHA)
1084 if (upeek(pid, REG_A3, &a3) < 0)
1087 if (!(tcp->flags & TCB_INSYSCALL)) {
1088 if (upeek(pid, REG_R0, &scno) < 0)
1091 /* Check if we return from execve. */
1092 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1093 tcp->flags &= ~TCB_WAITEXECVE;
1098 * Do some sanity checks to figure out if it's
1099 * really a syscall entry
1101 if (scno < 0 || scno > nsyscalls) {
1102 if (a3 == 0 || a3 == -1) {
1104 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
1110 if (upeek(pid, REG_R0, &r0) < 0)
1113 #elif defined (SPARC) || defined (SPARC64)
1114 /* Everything we need is in the current register set. */
1115 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
1118 /* If we are entering, then disassemble the syscall trap. */
1119 if (!(tcp->flags & TCB_INSYSCALL)) {
1120 /* Retrieve the syscall trap instruction. */
1122 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
1123 #if defined(SPARC64)
1129 /* Disassemble the trap to see what personality to use. */
1132 /* Linux/SPARC syscall trap. */
1136 /* Linux/SPARC64 syscall trap. */
1140 /* SunOS syscall trap. (pers 1) */
1141 fprintf(stderr,"syscall: SunOS no support\n");
1144 /* Solaris 2.x syscall trap. (per 2) */
1148 /* NetBSD/FreeBSD syscall trap. */
1149 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1152 /* Solaris 2.x gettimeofday */
1156 /* Unknown syscall trap. */
1157 if(tcp->flags & TCB_WAITEXECVE) {
1158 tcp->flags &= ~TCB_WAITEXECVE;
1161 #if defined (SPARC64)
1162 fprintf(stderr,"syscall: unknown syscall trap %08lx %016lx\n", trap, regs.r_tpc);
1164 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
1169 /* Extract the system call number from the registers. */
1170 if (trap == 0x91d02027)
1176 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
1180 if (upeek(pid, PT_GR20, &scno) < 0)
1182 if (!(tcp->flags & TCB_INSYSCALL)) {
1183 /* Check if we return from execve. */
1184 if ((tcp->flags & TCB_WAITEXECVE)) {
1185 tcp->flags &= ~TCB_WAITEXECVE;
1191 * In the new syscall ABI, the system call number is in R3.
1193 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1197 /* Odd as it may seem, a glibc bug has been known to cause
1198 glibc to issue bogus negative syscall numbers. So for
1199 our purposes, make strace print what it *should* have been */
1200 long correct_scno = (scno & 0xff);
1203 "Detected glibc bug: bogus system call number = %ld, "
1204 "correcting to %ld\n",
1207 scno = correct_scno;
1211 if (!(tcp->flags & TCB_INSYSCALL)) {
1212 /* Check if we return from execve. */
1213 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1214 tcp->flags &= ~TCB_WAITEXECVE;
1219 if (upeek(pid, REG_SYSCALL, &scno) < 0)
1223 if (!(tcp->flags & TCB_INSYSCALL)) {
1224 /* Check if we return from execve. */
1225 if (tcp->flags & TCB_WAITEXECVE) {
1226 tcp->flags &= ~TCB_WAITEXECVE;
1233 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1236 /* new syscall ABI returns result in R0 */
1237 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1240 /* ABI defines result returned in r9 */
1241 if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
1246 #ifdef HAVE_PR_SYSCALL
1247 scno = tcp->status.PR_SYSCALL;
1248 #else /* !HAVE_PR_SYSCALL */
1250 scno = tcp->status.PR_WHAT;
1252 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1256 switch (regs.r_eax) {
1259 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1265 #endif /* FREEBSD */
1266 #endif /* !HAVE_PR_SYSCALL */
1267 #endif /* USE_PROCFS */
1268 if (!(tcp->flags & TCB_INSYSCALL))
1278 long scno = tcp->scno;
1279 if (scno >= 0 && scno < nsyscalls && sysent[scno].native_scno != 0)
1280 scno = sysent[scno].native_scno;
1282 scno += NR_SYSCALL_BASE;
1292 #else /* USE_PROCFS */
1293 int scno = known_scno(tcp);
1295 if (!(tcp->flags & TCB_INSYSCALL)) {
1296 if (tcp->status.PR_WHY != PR_SYSENTRY) {
1300 || scno == SYS_vfork
1301 #endif /* SYS_vfork */
1303 || scno == SYS_fork1
1304 #endif /* SYS_fork1 */
1306 || scno == SYS_forkall
1307 #endif /* SYS_forkall */
1309 || scno == SYS_rfork1
1310 #endif /* SYS_fork1 */
1312 || scno == SYS_rforkall
1313 #endif /* SYS_rforkall */
1315 /* We are returning in the child, fake it. */
1316 tcp->status.PR_WHY = PR_SYSENTRY;
1318 tcp->status.PR_WHY = PR_SYSEXIT;
1321 fprintf(stderr, "syscall: missing entry\n");
1322 tcp->flags |= TCB_INSYSCALL;
1327 if (tcp->status.PR_WHY != PR_SYSEXIT) {
1328 fprintf(stderr, "syscall: missing exit\n");
1329 tcp->flags &= ~TCB_INSYSCALL;
1332 #endif /* USE_PROCFS */
1334 if (!(tcp->flags & TCB_INSYSCALL)) {
1336 fprintf(stderr, "syscall: missing entry\n");
1337 tcp->flags |= TCB_INSYSCALL;
1344 * This happens when a signal handler
1345 * for a signal which interrupted a
1346 * a system call makes another system call.
1348 fprintf(stderr, "syscall: missing exit\n");
1350 tcp->flags &= ~TCB_INSYSCALL;
1356 if (upeek(pid, 4*EAX, &eax) < 0)
1358 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1360 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1363 #elif defined (X86_64)
1364 if (upeek(pid, 8*RAX, &rax) < 0)
1366 if (current_personality == 1)
1367 rax = (long int)(int)rax; /* sign extend from 32 bits */
1368 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1370 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1373 #elif defined (S390) || defined (S390X)
1374 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1376 if (syscall_mode != -ENOSYS)
1377 syscall_mode = tcp->scno;
1378 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
1380 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1383 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1384 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1385 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1387 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1388 * flag set for the post-execve SIGTRAP to see and reset.
1392 #elif defined (POWERPC)
1393 # define SO_MASK 0x10000000
1394 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1396 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
1398 if (flags & SO_MASK)
1400 #elif defined (M68K)
1401 if (upeek(pid, 4*PT_D0, &d0) < 0)
1403 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1405 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1412 #elif defined (HPPA)
1413 if (upeek(pid, PT_GR28, &r28) < 0)
1416 if (upeek(pid, PT_R10, &r10) < 0)
1418 if (upeek(pid, PT_R8, &r8) < 0)
1420 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1422 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1436 #if defined(S390) || defined(S390X)
1437 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1445 #else /* !S390 && !S390X */
1447 if (eax < 0 && -eax < nerrnos) {
1457 if (rax < 0 && -rax < nerrnos) {
1471 if (err < 0 && -err < nerrnos) {
1499 if (result && (unsigned long) -result < nerrnos) {
1504 tcp->u_rval = result;
1507 #else /* !POWERPC */
1509 if (d0 && (unsigned) -d0 < nerrnos) {
1519 if (regs.ARM_r0 && (unsigned) -regs.ARM_r0 < nerrnos) {
1521 u_error = -regs.ARM_r0;
1524 tcp->u_rval = regs.ARM_r0;
1539 if (regs.r_psr & PSR_C) {
1541 u_error = regs.r_o0;
1544 tcp->u_rval = regs.r_o0;
1549 if (regs.r_tstate & 0x1100000000UL) {
1551 u_error = regs.r_o0;
1554 tcp->u_rval = regs.r_o0;
1557 #else /* !SPARC64 */
1559 if (r28 && (unsigned) -r28 < nerrnos) {
1569 /* interpret R0 as return value or error number */
1570 if (r0 && (unsigned) -r0 < nerrnos) {
1580 /* interpret result as return value or error number */
1581 if (r9 && (unsigned) -r9 < nerrnos) {
1593 #endif /* SPARC64 */
1597 #endif /* POWERPC */
1602 #endif /* S390 || S390X */
1605 /* get error code from user struct */
1606 if (upeek(pid, uoff(u_error), &u_error) < 0)
1608 u_error >>= 24; /* u_error is a char */
1610 /* get system call return value */
1611 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1616 /* Judicious guessing goes a long way. */
1617 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1619 u_error = tcp->status.pr_reg[R_O0];
1622 tcp->u_rval = tcp->status.pr_reg[R_O0];
1627 /* Wanna know how to kill an hour single-stepping? */
1628 if (tcp->status.PR_REG[EFL] & 0x1) {
1630 u_error = tcp->status.PR_REG[EAX];
1633 tcp->u_rval = tcp->status.PR_REG[EAX];
1634 #ifdef HAVE_LONG_LONG
1636 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1637 tcp->status.PR_REG[EAX];
1643 /* Wanna know how to kill an hour single-stepping? */
1644 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1646 u_error = tcp->status.PR_REG[RAX];
1649 tcp->u_rval = tcp->status.PR_REG[RAX];
1654 if (tcp->status.pr_reg[CTX_A3]) {
1656 u_error = tcp->status.pr_reg[CTX_V0];
1659 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1665 if (regs.r_eflags & PSL_C) {
1667 u_error = regs.r_eax;
1669 tcp->u_rval = regs.r_eax;
1671 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1674 #endif /* FREEBSD */
1675 tcp->u_error = u_error;
1680 force_result(tcp, error, rval)
1686 #if defined(S390) || defined(S390X)
1687 gpr2 = error ? -error : rval;
1688 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1690 #else /* !S390 && !S390X */
1692 eax = error ? -error : rval;
1693 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1697 rax = error ? -error : rval;
1698 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 8), rax) < 0)
1703 r8 = error ? -error : rval;
1704 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1716 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1717 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1730 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1731 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1735 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1745 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1746 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
1748 #else /* !POWERPC */
1750 d0 = error ? -error : rval;
1751 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1755 regs.ARM_r0 = error ? -error : rval;
1756 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), regs.ARM_r0) < 0)
1768 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1769 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1773 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1776 regs.r_psr |= PSR_C;
1780 regs.r_psr &= ~PSR_C;
1783 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1787 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1790 regs.r_tstate |= 0x1100000000UL;
1794 regs.r_tstate &= ~0x1100000000UL;
1797 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1799 #else /* !SPARC64 */
1801 r28 = error ? -error : rval;
1802 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1806 r0 = error ? -error : rval;
1807 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1811 r9 = error ? -error : rval;
1812 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1818 #endif /* SPARC64 */
1822 #endif /* POWERPC */
1827 #endif /* S390 || S390X */
1830 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1832 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1840 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1845 regs.r_eflags |= PSL_C;
1849 regs.r_eflags &= ~PSL_C;
1852 if (pwrite(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1856 #endif /* FREEBSD */
1858 /* All branches reach here on success (only). */
1859 tcp->u_error = error;
1870 #endif /* !USE_PROCFS */
1872 #if defined(S390) || defined(S390X)
1875 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1876 tcp->u_nargs = sysent[tcp->scno].nargs;
1878 tcp->u_nargs = MAX_ARGS;
1879 for (i = 0; i < tcp->u_nargs; i++) {
1880 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
1884 #elif defined (ALPHA)
1887 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1888 tcp->u_nargs = sysent[tcp->scno].nargs;
1890 tcp->u_nargs = MAX_ARGS;
1891 for (i = 0; i < tcp->u_nargs; i++) {
1892 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1893 * for scno somewhere above here!
1895 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1899 #elif defined (IA64)
1902 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1903 /* be backwards compatible with kernel < 2.4.4... */
1905 # define PT_RBS_END PT_AR_BSP
1908 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
1910 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1913 sof = (cfm >> 0) & 0x7f;
1914 sol = (cfm >> 7) & 0x7f;
1915 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1917 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1918 && sysent[tcp->scno].nargs != -1)
1919 tcp->u_nargs = sysent[tcp->scno].nargs;
1921 tcp->u_nargs = MAX_ARGS;
1922 for (i = 0; i < tcp->u_nargs; ++i) {
1923 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1924 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1930 if (/* EBX = out0 */
1931 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1933 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1935 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1937 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1939 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1941 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1944 for (i = 0; i < 6; ++i)
1945 /* truncate away IVE sign-extension */
1946 tcp->u_arg[i] &= 0xffffffff;
1948 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1949 && sysent[tcp->scno].nargs != -1)
1950 tcp->u_nargs = sysent[tcp->scno].nargs;
1955 #elif defined (MIPS)
1960 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1961 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1963 nargs = tcp->u_nargs = MAX_ARGS;
1965 if(upeek(pid, REG_SP, &sp) < 0)
1967 for(i = 0; i < 4; i++) {
1968 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1971 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1972 (char *)(tcp->u_arg + 4));
1974 for(i = 0; i < nargs; i++) {
1975 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1980 #elif defined (POWERPC)
1982 #define PT_ORIG_R3 34
1986 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1987 tcp->u_nargs = sysent[tcp->scno].nargs;
1989 tcp->u_nargs = MAX_ARGS;
1990 for (i = 0; i < tcp->u_nargs; i++) {
1991 if (upeek(pid, (i==0) ?
1992 (sizeof(unsigned long)*PT_ORIG_R3) :
1993 ((i+PT_R3)*sizeof(unsigned long)),
1994 &tcp->u_arg[i]) < 0)
1998 #elif defined (SPARC) || defined (SPARC64)
2002 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2003 tcp->u_nargs = sysent[tcp->scno].nargs;
2005 tcp->u_nargs = MAX_ARGS;
2006 for (i = 0; i < tcp->u_nargs; i++)
2007 tcp->u_arg[i] = *((®s.r_o0) + i);
2009 #elif defined (HPPA)
2013 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2014 tcp->u_nargs = sysent[tcp->scno].nargs;
2016 tcp->u_nargs = MAX_ARGS;
2017 for (i = 0; i < tcp->u_nargs; i++) {
2018 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
2026 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2027 tcp->u_nargs = sysent[tcp->scno].nargs;
2029 tcp->u_nargs = MAX_ARGS;
2030 for (i = 0; i < tcp->u_nargs; i++)
2031 tcp->u_arg[i] = regs.uregs[i];
2036 static int syscall_regs[] = {
2037 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
2038 REG_REG0, REG_REG0+1, REG_REG0+2
2041 tcp->u_nargs = sysent[tcp->scno].nargs;
2042 for (i = 0; i < tcp->u_nargs; i++) {
2043 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
2050 /* Registers used by SH5 Linux system calls for parameters */
2051 static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
2054 * TODO: should also check that the number of arguments encoded
2055 * in the trap number matches the number strace expects.
2058 assert(sysent[tcp->scno].nargs <
2059 sizeof(syscall_regs)/sizeof(syscall_regs[0]));
2062 tcp->u_nargs = sysent[tcp->scno].nargs;
2063 for (i = 0; i < tcp->u_nargs; i++) {
2064 if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
2069 #elif defined(X86_64)
2072 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
2073 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
2074 {RBX,RCX,RDX,RSI,RDI,RBP} /* i386 ABI */
2077 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2078 tcp->u_nargs = sysent[tcp->scno].nargs;
2080 tcp->u_nargs = MAX_ARGS;
2081 for (i = 0; i < tcp->u_nargs; i++) {
2082 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
2086 #else /* Other architecture (like i386) (32bits specific) */
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 = MAX_ARGS;
2093 for (i = 0; i < tcp->u_nargs; i++) {
2094 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
2103 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2104 tcp->u_nargs = sysent[tcp->scno].nargs;
2106 tcp->u_nargs = MAX_ARGS;
2107 for (i = 0; i < tcp->u_nargs; i++) {
2110 if (upeek(pid, uoff(u_arg[0]) +
2111 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
2119 * SGI is broken: even though it has pr_sysarg, it doesn't
2120 * set them on system call entry. Get a clue.
2122 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2123 tcp->u_nargs = sysent[tcp->scno].nargs;
2125 tcp->u_nargs = tcp->status.pr_nsysarg;
2126 if (tcp->u_nargs > 4) {
2127 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2128 4*sizeof(tcp->u_arg[0]));
2129 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
2130 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
2133 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2134 tcp->u_nargs*sizeof(tcp->u_arg[0]));
2138 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
2140 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2141 tcp->u_nargs = sysent[tcp->scno].nargs;
2143 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
2144 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2145 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2146 #elif defined (HAVE_PR_SYSCALL)
2147 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2148 tcp->u_nargs = sysent[tcp->scno].nargs;
2150 tcp->u_nargs = tcp->status.pr_nsysarg;
2153 for (i = 0; i < tcp->u_nargs; i++)
2154 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
2156 #elif defined (I386)
2157 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2158 tcp->u_nargs = sysent[tcp->scno].nargs;
2161 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2162 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2164 I DONT KNOW WHAT TO DO
2165 #endif /* !HAVE_PR_SYSCALL */
2168 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2169 sysent[tcp->scno].nargs > tcp->status.val)
2170 tcp->u_nargs = sysent[tcp->scno].nargs;
2172 tcp->u_nargs = tcp->status.val;
2173 if (tcp->u_nargs < 0)
2175 if (tcp->u_nargs > MAX_ARGS)
2176 tcp->u_nargs = MAX_ARGS;
2177 switch(regs.r_eax) {
2179 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2180 regs.r_esp + sizeof(int) + sizeof(quad_t));
2183 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2184 regs.r_esp + 2 * sizeof(int));
2187 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2188 regs.r_esp + sizeof(int));
2191 #endif /* FREEBSD */
2203 /* Measure the exit time as early as possible to avoid errors. */
2204 if (dtime && (tcp->flags & TCB_INSYSCALL))
2205 gettimeofday(&tv, NULL);
2207 res = get_scno(tcp);
2211 res = syscall_fixup(tcp);
2215 if (tcp->flags & TCB_INSYSCALL) {
2217 res = get_error(tcp);
2221 internal_syscall(tcp);
2222 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2223 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2224 tcp->flags &= ~TCB_INSYSCALL;
2228 if (tcp->flags & TCB_REPRINT) {
2231 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2232 tprintf("syscall_%lu", tcp->scno);
2234 tprintf("%s", sysent[tcp->scno].sys_name);
2235 tprintf(" resumed> ");
2238 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
2239 if (counts == NULL) {
2240 counts = calloc(sizeof *counts, nsyscalls);
2241 if (counts == NULL) {
2243 strace: out of memory for call counts\n");
2248 counts[tcp->scno].calls++;
2250 counts[tcp->scno].errors++;
2251 tv_sub(&tv, &tv, &tcp->etime);
2253 if (tv_cmp(&tv, &tcp->dtime) > 0) {
2254 static struct timeval one_tick;
2255 if (one_tick.tv_usec == 0) {
2256 /* Initialize it. */
2257 struct itimerval it;
2258 memset(&it, 0, sizeof it);
2259 it.it_interval.tv_usec = 1;
2260 setitimer(ITIMER_REAL, &it, NULL);
2261 getitimer(ITIMER_REAL, &it);
2262 one_tick = it.it_interval;
2265 if (tv_nz(&tcp->dtime))
2267 else if (tv_cmp(&tv, &one_tick) > 0) {
2268 if (tv_cmp(&shortest, &one_tick) < 0)
2275 if (tv_cmp(&tv, &shortest) < 0)
2277 tv_add(&counts[tcp->scno].time,
2278 &counts[tcp->scno].time, &tv);
2279 tcp->flags &= ~TCB_INSYSCALL;
2283 if (tcp->scno >= nsyscalls || tcp->scno < 0
2284 || (qual_flags[tcp->scno] & QUAL_RAW))
2285 sys_res = printargs(tcp);
2287 if (not_failing_only && tcp->u_error)
2288 return 0; /* ignore failed syscalls */
2289 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2291 u_error = tcp->u_error;
2294 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2295 qual_flags[tcp->scno] & QUAL_RAW) {
2297 tprintf("= -1 (errno %ld)", u_error);
2299 tprintf("= %#lx", tcp->u_rval);
2301 else if (!(sys_res & RVAL_NONE) && u_error) {
2305 tprintf("= ? ERESTARTSYS (To be restarted)");
2307 case ERESTARTNOINTR:
2308 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2310 case ERESTARTNOHAND:
2311 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2313 case ERESTART_RESTARTBLOCK:
2314 tprintf("= ? ERESTART_RESTARTBLOCK (To be restarted)");
2320 tprintf("E??? (errno %ld)", u_error);
2321 else if (u_error < nerrnos)
2322 tprintf("%s (%s)", errnoent[u_error],
2325 tprintf("ERRNO_%ld (%s)", u_error,
2331 if (sys_res & RVAL_NONE)
2334 switch (sys_res & RVAL_MASK) {
2336 tprintf("= %#lx", tcp->u_rval);
2339 tprintf("= %#lo", tcp->u_rval);
2342 tprintf("= %lu", tcp->u_rval);
2345 tprintf("= %ld", tcp->u_rval);
2347 #ifdef HAVE_LONG_LONG
2349 tprintf("= %#llx", tcp->u_lrval);
2352 tprintf("= %#llo", tcp->u_lrval);
2354 case RVAL_LUDECIMAL:
2355 tprintf("= %llu", tcp->u_lrval);
2358 tprintf("= %lld", tcp->u_lrval);
2363 "invalid rval format\n");
2367 if ((sys_res & RVAL_STR) && tcp->auxstr)
2368 tprintf(" (%s)", tcp->auxstr);
2371 tv_sub(&tv, &tv, &tcp->etime);
2372 tprintf(" <%ld.%06ld>",
2373 (long) tv.tv_sec, (long) tv.tv_usec);
2378 if (fflush(tcp->outf) == EOF)
2380 tcp->flags &= ~TCB_INSYSCALL;
2384 /* Entering system call */
2385 res = syscall_enter(tcp);
2389 switch (known_scno(tcp)) {
2391 #if !defined (ALPHA) && !defined(SPARC) && !defined(SPARC64) && !defined(MIPS) && !defined(HPPA)
2392 case SYS_socketcall:
2393 decode_subcall(tcp, SYS_socket_subcall,
2394 SYS_socket_nsubcalls, deref_style);
2397 decode_subcall(tcp, SYS_ipc_subcall,
2398 SYS_ipc_nsubcalls, shift_style);
2400 #endif /* !ALPHA && !MIPS && !SPARC && !SPARC64 && !HPPA */
2401 #if defined (SPARC) || defined (SPARC64)
2402 case SYS_socketcall:
2403 sparc_socket_decode (tcp);
2408 #ifdef SYS_pgrpsys_subcall
2410 decode_subcall(tcp, SYS_pgrpsys_subcall,
2411 SYS_pgrpsys_nsubcalls, shift_style);
2413 #endif /* SYS_pgrpsys_subcall */
2414 #ifdef SYS_sigcall_subcall
2416 decode_subcall(tcp, SYS_sigcall_subcall,
2417 SYS_sigcall_nsubcalls, mask_style);
2419 #endif /* SYS_sigcall_subcall */
2421 decode_subcall(tcp, SYS_msgsys_subcall,
2422 SYS_msgsys_nsubcalls, shift_style);
2425 decode_subcall(tcp, SYS_shmsys_subcall,
2426 SYS_shmsys_nsubcalls, shift_style);
2429 decode_subcall(tcp, SYS_semsys_subcall,
2430 SYS_semsys_nsubcalls, shift_style);
2434 decode_subcall(tcp, SYS_utssys_subcall,
2435 SYS_utssys_nsubcalls, shift_style);
2439 decode_subcall(tcp, SYS_sysfs_subcall,
2440 SYS_sysfs_nsubcalls, shift_style);
2443 decode_subcall(tcp, SYS_spcall_subcall,
2444 SYS_spcall_nsubcalls, shift_style);
2446 #ifdef SYS_context_subcall
2448 decode_subcall(tcp, SYS_context_subcall,
2449 SYS_context_nsubcalls, shift_style);
2451 #endif /* SYS_context_subcall */
2452 #ifdef SYS_door_subcall
2454 decode_subcall(tcp, SYS_door_subcall,
2455 SYS_door_nsubcalls, door_style);
2457 #endif /* SYS_door_subcall */
2458 #ifdef SYS_kaio_subcall
2460 decode_subcall(tcp, SYS_kaio_subcall,
2461 SYS_kaio_nsubcalls, shift_style);
2469 decode_subcall(tcp, 0, 0, table_style);
2474 decode_subcall(tcp, SYS_semsys_subcall,
2475 SYS_semsys_nsubcalls, shift_style);
2478 decode_subcall(tcp, SYS_msgsys_subcall,
2479 SYS_msgsys_nsubcalls, shift_style);
2482 decode_subcall(tcp, SYS_shmsys_subcall,
2483 SYS_shmsys_nsubcalls, shift_style);
2488 internal_syscall(tcp);
2489 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2490 tcp->flags |= TCB_INSYSCALL;
2495 gettimeofday(&tcp->etime, NULL);
2496 tcp->flags |= TCB_INSYSCALL;
2501 tcp->flags &= ~TCB_REPRINT;
2503 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2504 tprintf("syscall_%lu(", tcp->scno);
2506 tprintf("%s(", sysent[tcp->scno].sys_name);
2507 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2508 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2509 sys_res = printargs(tcp);
2511 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2512 if (fflush(tcp->outf) == EOF)
2514 tcp->flags |= TCB_INSYSCALL;
2515 /* Measure the entrance time as late as possible to avoid errors. */
2517 gettimeofday(&tcp->etime, NULL);
2525 if (entering(tcp)) {
2528 for (i = 0; i < tcp->u_nargs; i++)
2529 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2541 #if defined (SPARC) || defined (SPARC64)
2543 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
2547 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2550 if (upeek(tcp->pid, PT_R9, &val) < 0)
2552 #endif /* SPARC || SPARC64 */
2556 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2562 val = tcp->status.PR_REG[R_O1];
2565 val = tcp->status.PR_REG[EDX];
2568 val = tcp->status.PR_REG[RDX];
2571 val = tcp->status.PR_REG[CTX_V1];
2576 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
2583 * Apparently, indirect system calls have already be converted by ptrace(2),
2584 * so if you see "indir" this program has gone astray.
2592 if (entering(tcp)) {
2593 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2594 fprintf(stderr, "Bogus syscall: %u\n", scno);
2597 nargs = sysent[scno].nargs;
2598 tprintf("%s", sysent[scno].sys_name);
2599 for (i = 0; i < nargs; i++)
2600 tprintf(", %#lx", tcp->u_arg[i+1]);
2610 return -tv_cmp(&counts[*((int *) a)].time, &counts[*((int *) b)].time);
2618 return strcmp(sysent[*((int *) a)].sys_name,
2619 sysent[*((int *) b)].sys_name);
2627 int m = counts[*((int *) a)].calls, n = counts[*((int *) b)].calls;
2629 return (m < n) ? 1 : (m > n) ? -1 : 0;
2632 static int (*sortfun)();
2633 static struct timeval overhead = { -1, -1 };
2639 if (strcmp(sortby, "time") == 0)
2641 else if (strcmp(sortby, "calls") == 0)
2642 sortfun = count_cmp;
2643 else if (strcmp(sortby, "name") == 0)
2644 sortfun = syscall_cmp;
2645 else if (strcmp(sortby, "nothing") == 0)
2648 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2653 void set_overhead(n)
2656 overhead.tv_sec = n / 1000000;
2657 overhead.tv_usec = n % 1000000;
2665 int call_cum, error_cum;
2666 struct timeval tv_cum, dtv;
2668 char *dashes = "-------------------------";
2671 int *sorted_count = malloc(nsyscalls * sizeof(int));
2673 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2674 if (overhead.tv_sec == -1) {
2675 tv_mul(&overhead, &shortest, 8);
2676 tv_div(&overhead, &overhead, 10);
2678 for (i = 0; i < nsyscalls; i++) {
2679 sorted_count[i] = i;
2680 if (counts == NULL || counts[i].calls == 0)
2682 tv_mul(&dtv, &overhead, counts[i].calls);
2683 tv_sub(&counts[i].time, &counts[i].time, &dtv);
2684 call_cum += counts[i].calls;
2685 error_cum += counts[i].errors;
2686 tv_add(&tv_cum, &tv_cum, &counts[i].time);
2688 if (counts && sortfun)
2689 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2690 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2691 "% time", "seconds", "usecs/call",
2692 "calls", "errors", "syscall");
2693 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2694 dashes, dashes, dashes, dashes, dashes, dashes);
2696 for (i = 0; i < nsyscalls; i++) {
2697 j = sorted_count[i];
2698 if (counts[j].calls == 0)
2700 tv_div(&dtv, &counts[j].time, counts[j].calls);
2701 if (counts[j].errors)
2702 sprintf(error_str, "%d", counts[j].errors);
2704 error_str[0] = '\0';
2705 percent = (100.0 * tv_float(&counts[j].time)
2706 / tv_float(&tv_cum));
2707 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2708 percent, (long) counts[j].time.tv_sec,
2709 (long) counts[j].time.tv_usec,
2710 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2712 error_str, sysent[j].sys_name);
2717 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2718 dashes, dashes, dashes, dashes, dashes, dashes);
2720 sprintf(error_str, "%d", error_cum);
2722 error_str[0] = '\0';
2723 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2724 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2725 call_cum, error_str, "total");