2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/syscall.h>
43 #include <sys/param.h>
47 # define fpq kernel_fpq
49 # define fpu kernel_fpu
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(IA64)
78 # include <asm/ptrace_offsets.h>
82 #define NR_SYSCALL_BASE 0
85 #define ERESTARTSYS 512
87 #ifndef ERESTARTNOINTR
88 #define ERESTARTNOINTR 513
90 #ifndef ERESTARTNOHAND
91 #define ERESTARTNOHAND 514 /* restart if no handler.. */
94 #define ENOIOCTLCMD 515 /* No ioctl command */
96 #ifndef ERESTART_RESTARTBLOCK
97 #define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
105 #undef NR_SYSCALL_BASE
106 #define NR_SYSCALL_BASE __NR_SYSCALL_BASE
112 /* Define these shorthand notations to simplify the syscallent files. */
113 #define TF TRACE_FILE
115 #define TN TRACE_NETWORK
116 #define TP TRACE_PROCESS
117 #define TS TRACE_SIGNAL
119 struct sysent sysent0[] = {
120 #include "syscallent.h"
122 int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
124 #if SUPPORTED_PERSONALITIES >= 2
125 struct sysent sysent1[] = {
126 #include "syscallent1.h"
128 int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
129 #endif /* SUPPORTED_PERSONALITIES >= 2 */
131 #if SUPPORTED_PERSONALITIES >= 3
132 struct sysent sysent2[] = {
133 #include "syscallent2.h"
135 int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
136 #endif /* SUPPORTED_PERSONALITIES >= 3 */
138 struct sysent *sysent;
141 /* Now undef them since short defines cause wicked namespace pollution. */
148 char *errnoent0[] = {
149 #include "errnoent.h"
151 int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
153 #if SUPPORTED_PERSONALITIES >= 2
154 char *errnoent1[] = {
155 #include "errnoent1.h"
157 int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
158 #endif /* SUPPORTED_PERSONALITIES >= 2 */
160 #if SUPPORTED_PERSONALITIES >= 3
161 char *errnoent2[] = {
162 #include "errnoent2.h"
164 int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
165 #endif /* SUPPORTED_PERSONALITIES >= 3 */
170 int current_personality;
173 set_personality(personality)
176 switch (personality) {
178 errnoent = errnoent0;
181 nsyscalls = nsyscalls0;
182 ioctlent = ioctlent0;
183 nioctlents = nioctlents0;
184 signalent = signalent0;
185 nsignals = nsignals0;
188 #if SUPPORTED_PERSONALITIES >= 2
190 errnoent = errnoent1;
193 nsyscalls = nsyscalls1;
194 ioctlent = ioctlent1;
195 nioctlents = nioctlents1;
196 signalent = signalent1;
197 nsignals = nsignals1;
199 #endif /* SUPPORTED_PERSONALITIES >= 2 */
201 #if SUPPORTED_PERSONALITIES >= 3
203 errnoent = errnoent2;
206 nsyscalls = nsyscalls2;
207 ioctlent = ioctlent2;
208 nioctlents = nioctlents2;
209 signalent = signalent2;
210 nsignals = nsignals2;
212 #endif /* SUPPORTED_PERSONALITIES >= 3 */
218 current_personality = personality;
222 int qual_flags[MAX_QUALS];
224 static int call_count[MAX_QUALS];
225 static int error_count[MAX_QUALS];
226 static struct timeval tv_count[MAX_QUALS];
227 static int sorted_count[MAX_QUALS];
229 static struct timeval shortest = { 1000000, 0 };
231 static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
233 static struct qual_options {
239 { QUAL_TRACE, "trace", qual_syscall, "system call" },
240 { QUAL_TRACE, "t", qual_syscall, "system call" },
241 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
242 { QUAL_ABBREV, "a", qual_syscall, "system call" },
243 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
244 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
245 { QUAL_RAW, "raw", qual_syscall, "system call" },
246 { QUAL_RAW, "x", qual_syscall, "system call" },
247 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
248 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
249 { QUAL_SIGNAL, "s", qual_signal, "signal" },
250 { QUAL_FAULT, "fault", qual_fault, "fault" },
251 { QUAL_FAULT, "faults", qual_fault, "fault" },
252 { QUAL_FAULT, "m", qual_fault, "fault" },
253 { QUAL_READ, "read", qual_desc, "descriptor" },
254 { QUAL_READ, "reads", qual_desc, "descriptor" },
255 { QUAL_READ, "r", qual_desc, "descriptor" },
256 { QUAL_WRITE, "write", qual_desc, "descriptor" },
257 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
258 { QUAL_WRITE, "w", qual_desc, "descriptor" },
259 { 0, NULL, NULL, NULL },
263 qualify_one(n, opt, not)
265 struct qual_options *opt;
269 qual_flags[n] &= ~opt->bitflag;
271 qual_flags[n] |= opt->bitflag;
275 qual_syscall(s, opt, not)
277 struct qual_options *opt;
283 for (i = 0; i < nsyscalls; i++) {
284 if (strcmp(s, sysent[i].sys_name) == 0) {
285 qualify_one(i, opt, not);
293 qual_signal(s, opt, not)
295 struct qual_options *opt;
301 if (s && *s && isdigit((unsigned char)*s)) {
302 qualify_one(atoi(s), opt, not);
305 if (strlen(s) >= sizeof buf)
309 for (i = 0; s[i]; i++)
310 s[i] = toupper((unsigned char)(s[i]));
311 if (strncmp(s, "SIG", 3) == 0)
313 for (i = 0; i <= NSIG; i++)
314 if (strcmp(s, signame(i) + 3) == 0) {
315 qualify_one(atoi(s), opt, not);
322 qual_fault(s, opt, not)
324 struct qual_options *opt;
331 qual_desc(s, opt, not)
333 struct qual_options *opt;
336 if (s && *s && isdigit((unsigned char)*s)) {
337 qualify_one(atoi(s), opt, not);
347 if (strcmp(s, "file") == 0)
349 if (strcmp(s, "ipc") == 0)
351 if (strcmp(s, "network") == 0)
352 return TRACE_NETWORK;
353 if (strcmp(s, "process") == 0)
354 return TRACE_PROCESS;
355 if (strcmp(s, "signal") == 0)
364 struct qual_options *opt;
369 opt = &qual_options[0];
370 for (i = 0; (p = qual_options[i].option_name); i++) {
372 if (strncmp(s, p, n) == 0 && s[n] == '=') {
373 opt = &qual_options[i];
383 if (strcmp(s, "none") == 0) {
387 if (strcmp(s, "all") == 0) {
388 for (i = 0; i < MAX_QUALS; i++) {
390 qual_flags[i] &= ~opt->bitflag;
392 qual_flags[i] |= opt->bitflag;
396 for (i = 0; i < MAX_QUALS; i++) {
398 qual_flags[i] |= opt->bitflag;
400 qual_flags[i] &= ~opt->bitflag;
402 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
403 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
404 for (i = 0; i < MAX_QUALS; i++) {
405 if (sysent[i].sys_flags & n) {
407 qual_flags[i] &= ~opt->bitflag;
409 qual_flags[i] |= opt->bitflag;
414 if (opt->qualify(p, opt, not)) {
415 fprintf(stderr, "strace: invalid %s `%s'\n",
416 opt->argument_name, p);
429 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
431 switch (tcp->scno + NR_SYSCALL_BASE) {
439 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
440 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
449 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
450 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
454 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
455 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
461 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
462 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
469 enum subcall_style { shift_style, deref_style, mask_style, door_style };
471 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
479 const struct subcall subcalls_table[] = {
480 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
482 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
484 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
486 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
490 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
492 const int socket_map [] = {
495 /* SYS_CONNECT */ 98,
496 /* SYS_LISTEN */ 106,
498 /* SYS_GETSOCKNAME */ 150,
499 /* SYS_GETPEERNAME */ 141,
500 /* SYS_SOCKETPAIR */ 135,
503 /* SYS_SENDTO */ 133,
504 /* SYS_RECVFROM */ 125,
505 /* SYS_SHUTDOWN */ 134,
506 /* SYS_SETSOCKOPT */ 105,
507 /* SYS_GETSOCKOPT */ 118,
508 /* SYS_SENDMSG */ 114,
509 /* SYS_RECVMSG */ 113
513 sparc_socket_decode (tcp)
519 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
522 tcp->scno = socket_map [tcp->u_arg [0]-1];
523 n = tcp->u_nargs = sysent [tcp->scno].nargs;
524 addr = tcp->u_arg [1];
525 for (i = 0; i < n; i++){
527 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
529 tcp->u_arg [i] = arg;
530 addr += sizeof (arg);
535 decode_subcall(tcp, subcall, nsubcalls, style)
539 enum subcall_style style;
541 long addr, mask, arg;
546 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
548 tcp->scno = subcall + tcp->u_arg[0];
549 if (sysent[tcp->scno].nargs != -1)
550 tcp->u_nargs = sysent[tcp->scno].nargs;
553 for (i = 0; i < tcp->u_nargs; i++)
554 tcp->u_arg[i] = tcp->u_arg[i + 1];
557 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
559 tcp->scno = subcall + tcp->u_arg[0];
560 addr = tcp->u_arg[1];
561 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
562 if (umove(tcp, addr, &arg) < 0)
567 tcp->u_nargs = sysent[tcp->scno].nargs;
570 mask = (tcp->u_arg[0] >> 8) & 0xff;
571 for (i = 0; mask; i++)
575 tcp->u_arg[0] &= 0xff;
576 tcp->scno = subcall + i;
577 if (sysent[tcp->scno].nargs != -1)
578 tcp->u_nargs = sysent[tcp->scno].nargs;
582 * Oh, yuck. The call code is the *sixth* argument.
583 * (don't you mean the *last* argument? - JH)
585 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
587 tcp->scno = subcall + tcp->u_arg[5];
588 if (sysent[tcp->scno].nargs != -1)
589 tcp->u_nargs = sysent[tcp->scno].nargs;
595 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
596 if (subcalls_table[i].call == tcp->scno) break;
597 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
598 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
599 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
600 for (i = 0; i < tcp->u_nargs; i++)
601 tcp->u_arg[i] = tcp->u_arg[i + 1];
609 struct tcb *tcp_last = NULL;
612 internal_syscall(tcp)
616 * We must always trace a few critical system calls in order to
617 * correctly support following forks in the presence of tracing
620 switch (tcp->scno + NR_SYSCALL_BASE) {
690 #ifdef __NR_exit_group
691 case __NR_exit_group:
694 case 252: /* IA-32 __NR_exit_group */
709 #elif defined (POWERPC)
710 static long result,flags;
714 static struct pt_regs regs;
715 #elif defined (ALPHA)
718 #elif defined (SPARC)
719 static struct regs regs;
720 static unsigned long trap;
724 #elif defined(S390) || defined(S390X)
727 static long syscall_mode;
734 #elif defined(X86_64)
752 #if defined(S390) || defined(S390X)
753 if (tcp->flags & TCB_WAITEXECVE) {
755 * When the execve system call completes successfully, the
756 * new process still has -ENOSYS (old style) or __NR_execve
757 * (new style) in gpr2. We cannot recover the scno again
758 * by disassembly, because the image that executed the
759 * syscall is gone now. Fortunately, we don't want it. We
760 * leave the flag set so that syscall_fixup can fake the
763 if (tcp->flags & TCB_INSYSCALL)
766 * This is the SIGTRAP after execve. We cannot try to read
767 * the system call here either.
769 tcp->flags &= ~TCB_WAITEXECVE;
773 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
776 if (syscall_mode != -ENOSYS) {
778 * Since kernel version 2.5.44 the scno gets passed in gpr2.
783 * Old style of "passing" the scno via the SVC instruction.
786 long opcode, offset_reg, tmp;
788 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
789 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
790 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
791 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
793 if (upeek(pid, PT_PSWADDR, &pc) < 0)
796 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
798 perror("peektext(pc-oneword)");
803 * We have to check if the SVC got executed directly or via an
804 * EXECUTE instruction. In case of EXECUTE it is necessary to do
805 * instruction decoding to derive the system call number.
806 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
807 * so that this doesn't work if a SVC opcode is part of an EXECUTE
808 * opcode. Since there is no way to find out the opcode size this
809 * is the best we can do...
812 if ((opcode & 0xff00) == 0x0a00) {
814 scno = opcode & 0xff;
817 /* SVC got executed by EXECUTE instruction */
820 * Do instruction decoding of EXECUTE. If you really want to
821 * understand this, read the Principles of Operations.
823 svc_addr = (void *) (opcode & 0xfff);
826 offset_reg = (opcode & 0x000f0000) >> 16;
827 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
832 offset_reg = (opcode & 0x0000f000) >> 12;
833 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
837 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
846 offset_reg = (opcode & 0x00f00000) >> 20;
847 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
850 scno = (scno | tmp) & 0xff;
853 #elif defined (POWERPC)
854 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
856 if (!(tcp->flags & TCB_INSYSCALL)) {
857 /* Check if we return from execve. */
858 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
859 tcp->flags &= ~TCB_WAITEXECVE;
864 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
866 #elif defined (X86_64)
867 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
870 if (!(tcp->flags & TCB_INSYSCALL)) {
871 static int currpers=-1;
874 /* Check CS register value. On x86-64 linux it is:
875 * 0x33 for long mode (64 bit)
876 * 0x23 for compatibility mode (32 bit)
877 * It takes only one ptrace and thus doesn't need
880 if (upeek(pid, 8*CS, &val) < 0)
884 case 0x23: currpers = 1; break;
885 case 0x33: currpers = 0; break;
887 fprintf(stderr, "Unknown value CS=0x%02X while "
888 "detecting personality of process "
889 "PID=%d\n", (int)val, pid);
890 currpers = current_personality;
894 /* This version analyzes the opcode of a syscall instruction.
895 * (int 0x80 on i386 vs. syscall on x86-64)
896 * It works, but is too complicated.
898 unsigned long val, rip, i;
900 if(upeek(pid, 8*RIP, &rip)<0)
901 perror("upeek(RIP)");
903 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
907 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
909 printf("ptrace_peektext failed: %s\n",
911 switch (call & 0xffff)
913 /* x86-64: syscall = 0x0f 0x05 */
914 case 0x050f: currpers = 0; break;
915 /* i386: int 0x80 = 0xcd 0x80 */
916 case 0x80cd: currpers = 1; break;
918 currpers = current_personality;
920 "Unknown syscall opcode (0x%04X) while "
921 "detecting personality of process "
922 "PID=%d\n", (int)call, pid);
926 if(currpers != current_personality)
928 char *names[]={"64 bit", "32 bit"};
929 set_personality(currpers);
930 printf("[ Process PID=%d runs in %s mode. ]\n",
931 pid, names[current_personality]);
935 # define IA64_PSR_IS ((long)1 << 34)
936 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
937 ia32 = (psr & IA64_PSR_IS) != 0;
938 if (!(tcp->flags & TCB_INSYSCALL)) {
940 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
943 if (upeek (pid, PT_R15, &scno) < 0)
946 /* Check if we return from execve. */
947 if (tcp->flags & TCB_WAITEXECVE) {
948 tcp->flags &= ~TCB_WAITEXECVE;
952 /* syscall in progress */
953 if (upeek (pid, PT_R8, &r8) < 0)
955 if (upeek (pid, PT_R10, &r10) < 0)
960 * Read complete register set in one go.
962 if (ptrace(PTRACE_GETREGS, pid, NULL, (void *)®s) == -1)
966 * We only need to grab the syscall number on syscall entry.
968 if (regs.ARM_ip == 0) {
970 * Note: we only deal with only 32-bit CPUs here.
972 if (regs.ARM_cpsr & 0x20) {
974 * Get the Thumb-mode system call number
979 * Get the ARM-mode system call number
982 scno = ptrace(PTRACE_PEEKTEXT, pid, (void *)(regs.ARM_pc - 4), NULL);
986 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
987 tcp->flags &= ~TCB_WAITEXECVE;
991 if ((scno & 0x0ff00000) != 0x0f900000) {
992 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
998 * Fixup the syscall number
1003 if (tcp->flags & TCB_INSYSCALL) {
1004 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
1005 tcp->flags &= ~TCB_INSYSCALL;
1008 if (!(tcp->flags & TCB_INSYSCALL)) {
1009 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1010 tcp->flags |= TCB_INSYSCALL;
1013 #elif defined (M68K)
1014 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
1016 #elif defined (MIPS)
1017 if (upeek(pid, REG_A3, &a3) < 0)
1020 if(!(tcp->flags & TCB_INSYSCALL)) {
1021 if (upeek(pid, REG_V0, &scno) < 0)
1024 if (scno < 0 || scno > nsyscalls) {
1025 if(a3 == 0 || a3 == -1) {
1027 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
1032 if (upeek(pid, REG_V0, &r2) < 0)
1035 #elif defined (ALPHA)
1036 if (upeek(pid, REG_A3, &a3) < 0)
1039 if (!(tcp->flags & TCB_INSYSCALL)) {
1040 if (upeek(pid, REG_R0, &scno) < 0)
1043 /* Check if we return from execve. */
1044 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1045 tcp->flags &= ~TCB_WAITEXECVE;
1050 * Do some sanity checks to figure out if it's
1051 * really a syscall entry
1053 if (scno < 0 || scno > nsyscalls) {
1054 if (a3 == 0 || a3 == -1) {
1056 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
1062 if (upeek(pid, REG_R0, &r0) < 0)
1065 #elif defined (SPARC)
1066 /* Everything we need is in the current register set. */
1067 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
1070 /* If we are entering, then disassemble the syscall trap. */
1071 if (!(tcp->flags & TCB_INSYSCALL)) {
1072 /* Retrieve the syscall trap instruction. */
1074 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
1078 /* Disassemble the trap to see what personality to use. */
1081 /* Linux/SPARC syscall trap. */
1085 /* Linux/SPARC64 syscall trap. */
1086 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
1089 /* SunOS syscall trap. (pers 1) */
1090 fprintf(stderr,"syscall: SunOS no support\n");
1093 /* Solaris 2.x syscall trap. (per 2) */
1097 /* NetBSD/FreeBSD syscall trap. */
1098 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1101 /* Solaris 2.x gettimeofday */
1105 /* Unknown syscall trap. */
1106 if(tcp->flags & TCB_WAITEXECVE) {
1107 tcp->flags &= ~TCB_WAITEXECVE;
1110 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
1114 /* Extract the system call number from the registers. */
1115 if (trap == 0x91d02027)
1121 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
1125 if (upeek(pid, PT_GR20, &scno) < 0)
1127 if (!(tcp->flags & TCB_INSYSCALL)) {
1128 /* Check if we return from execve. */
1129 if ((tcp->flags & TCB_WAITEXECVE)) {
1130 tcp->flags &= ~TCB_WAITEXECVE;
1136 * In the new syscall ABI, the system call number is in R3.
1138 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1142 /* Odd as it may seem, a glibc bug has been known to cause
1143 glibc to issue bogus negative syscall numbers. So for
1144 our purposes, make strace print what it *should* have been */
1145 long correct_scno = (scno & 0xff);
1148 "Detected glibc bug: bogus system call number = %ld, "
1149 "correcting to %ld\n",
1152 scno = correct_scno;
1156 if (!(tcp->flags & TCB_INSYSCALL)) {
1157 /* Check if we return from execve. */
1158 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1159 tcp->flags &= ~TCB_WAITEXECVE;
1164 if (upeek(pid, REG_SYSCALL, &scno) < 0)
1168 if (!(tcp->flags & TCB_INSYSCALL)) {
1169 /* Check if we return from execve. */
1170 if (tcp->flags & TCB_WAITEXECVE) {
1171 tcp->flags &= ~TCB_WAITEXECVE;
1178 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1181 /* new syscall ABI returns result in R0 */
1182 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1185 /* ABI defines result returned in r9 */
1186 if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
1191 #ifdef HAVE_PR_SYSCALL
1192 scno = tcp->status.PR_SYSCALL;
1193 #else /* !HAVE_PR_SYSCALL */
1195 scno = tcp->status.PR_WHAT;
1197 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1201 switch (regs.r_eax) {
1204 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1210 #endif /* FREEBSD */
1211 #endif /* !HAVE_PR_SYSCALL */
1212 #endif /* USE_PROCFS */
1213 if (!(tcp->flags & TCB_INSYSCALL))
1225 #else /* USE_PROCFS */
1226 int scno = tcp->scno;
1228 if (!(tcp->flags & TCB_INSYSCALL)) {
1229 if (tcp->status.PR_WHY != PR_SYSENTRY) {
1233 || scno == SYS_vfork
1234 #endif /* SYS_vfork */
1236 || scno == SYS_fork1
1237 #endif /* SYS_fork1 */
1239 || scno == SYS_forkall
1240 #endif /* SYS_forkall */
1242 || scno == SYS_rfork1
1243 #endif /* SYS_fork1 */
1245 || scno == SYS_rforkall
1246 #endif /* SYS_rforkall */
1248 /* We are returning in the child, fake it. */
1249 tcp->status.PR_WHY = PR_SYSENTRY;
1251 tcp->status.PR_WHY = PR_SYSEXIT;
1254 fprintf(stderr, "syscall: missing entry\n");
1255 tcp->flags |= TCB_INSYSCALL;
1260 if (tcp->status.PR_WHY != PR_SYSEXIT) {
1261 fprintf(stderr, "syscall: missing exit\n");
1262 tcp->flags &= ~TCB_INSYSCALL;
1265 #endif /* USE_PROCFS */
1267 if (!(tcp->flags & TCB_INSYSCALL)) {
1269 fprintf(stderr, "syscall: missing entry\n");
1270 tcp->flags |= TCB_INSYSCALL;
1277 * This happens when a signal handler
1278 * for a signal which interrupted a
1279 * a system call makes another system call.
1281 fprintf(stderr, "syscall: missing exit\n");
1283 tcp->flags &= ~TCB_INSYSCALL;
1289 if (upeek(pid, 4*EAX, &eax) < 0)
1291 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1293 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1296 #elif defined (X86_64)
1297 if (upeek(pid, 8*RAX, &rax) < 0)
1299 if (current_personality == 1)
1300 rax = (long int)(int)rax; /* sign extend from 32 bits */
1301 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1303 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1306 #elif defined (S390) || defined (S390X)
1307 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1309 if (syscall_mode != -ENOSYS)
1310 syscall_mode = tcp->scno;
1311 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
1313 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1316 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1317 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1318 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1320 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1321 * flag set for the post-execve SIGTRAP to see and reset.
1325 #elif defined (POWERPC)
1326 # define SO_MASK 0x10000000
1327 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1329 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
1331 if (flags & SO_MASK)
1333 #elif defined (M68K)
1334 if (upeek(pid, 4*PT_D0, &d0) < 0)
1336 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1338 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1345 #elif defined (HPPA)
1346 if (upeek(pid, PT_GR28, &r28) < 0)
1349 if (upeek(pid, PT_R10, &r10) < 0)
1351 if (upeek(pid, PT_R8, &r8) < 0)
1353 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1355 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1369 #if defined(S390) || defined(S390X)
1370 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1378 #else /* !S390 && !S390X */
1380 if (eax < 0 && -eax < nerrnos) {
1390 if (rax < 0 && -rax < nerrnos) {
1404 if (err < 0 && -err < nerrnos) {
1432 if (result && (unsigned long) -result < nerrnos) {
1437 tcp->u_rval = result;
1440 #else /* !POWERPC */
1442 if (d0 && (unsigned) -d0 < nerrnos) {
1452 if (regs.ARM_r0 && (unsigned) -regs.ARM_r0 < nerrnos) {
1454 u_error = -regs.ARM_r0;
1457 tcp->u_rval = regs.ARM_r0;
1472 if (regs.r_psr & PSR_C) {
1474 u_error = regs.r_o0;
1477 tcp->u_rval = regs.r_o0;
1482 if (r28 && (unsigned) -r28 < nerrnos) {
1492 /* interpret R0 as return value or error number */
1493 if (r0 && (unsigned) -r0 < nerrnos) {
1503 /* interpret result as return value or error number */
1504 if (r9 && (unsigned) -r9 < nerrnos) {
1519 #endif /* POWERPC */
1524 #endif /* S390 || S390X */
1527 /* get error code from user struct */
1528 if (upeek(pid, uoff(u_error), &u_error) < 0)
1530 u_error >>= 24; /* u_error is a char */
1532 /* get system call return value */
1533 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1538 /* Judicious guessing goes a long way. */
1539 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1541 u_error = tcp->status.pr_reg[R_O0];
1544 tcp->u_rval = tcp->status.pr_reg[R_O0];
1549 /* Wanna know how to kill an hour single-stepping? */
1550 if (tcp->status.PR_REG[EFL] & 0x1) {
1552 u_error = tcp->status.PR_REG[EAX];
1555 tcp->u_rval = tcp->status.PR_REG[EAX];
1556 #ifdef HAVE_LONG_LONG
1558 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1559 tcp->status.PR_REG[EAX];
1565 /* Wanna know how to kill an hour single-stepping? */
1566 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1568 u_error = tcp->status.PR_REG[RAX];
1571 tcp->u_rval = tcp->status.PR_REG[RAX];
1576 if (tcp->status.pr_reg[CTX_A3]) {
1578 u_error = tcp->status.pr_reg[CTX_V0];
1581 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1587 if (regs.r_eflags & PSL_C) {
1589 u_error = regs.r_eax;
1591 tcp->u_rval = regs.r_eax;
1593 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1596 #endif /* FREEBSD */
1597 tcp->u_error = u_error;
1602 force_result(tcp, error, rval)
1608 #if defined(S390) || defined(S390X)
1609 gpr2 = error ? -error : rval;
1610 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1612 #else /* !S390 && !S390X */
1614 eax = error ? -error : rval;
1615 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1619 rax = error ? -error : rval;
1620 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 8), rax) < 0)
1625 r8 = error ? -error : rval;
1626 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1638 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1639 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1652 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1653 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1657 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1667 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1668 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
1670 #else /* !POWERPC */
1672 d0 = error ? -error : rval;
1673 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1677 regs.ARM_r0 = error ? -error : rval;
1678 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), regs.ARM_r0) < 0)
1690 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1691 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1695 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1698 regs.r_psr |= PSR_C;
1702 regs.r_psr &= ~PSR_C;
1705 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1709 r28 = error ? -error : rval;
1710 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1714 r0 = error ? -error : rval;
1715 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1719 r9 = error ? -error : rval;
1720 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1729 #endif /* POWERPC */
1734 #endif /* S390 || S390X */
1737 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1739 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1747 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1752 regs.r_eflags |= PSL_C;
1756 regs.r_eflags &= ~PSL_C;
1759 if (pwrite(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1763 #endif /* FREEBSD */
1765 /* All branches reach here on success (only). */
1766 tcp->u_error = error;
1771 int syscall_enter(tcp)
1776 #endif /* !USE_PROCFS */
1778 #if defined(S390) || defined(S390X)
1781 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1782 tcp->u_nargs = sysent[tcp->scno].nargs;
1784 tcp->u_nargs = MAX_ARGS;
1785 for (i = 0; i < tcp->u_nargs; i++) {
1786 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
1790 #elif defined (ALPHA)
1793 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1794 tcp->u_nargs = sysent[tcp->scno].nargs;
1796 tcp->u_nargs = MAX_ARGS;
1797 for (i = 0; i < tcp->u_nargs; i++) {
1798 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1799 * for scno somewhere above here!
1801 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1805 #elif defined (IA64)
1808 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1809 /* be backwards compatible with kernel < 2.4.4... */
1811 # define PT_RBS_END PT_AR_BSP
1814 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
1816 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1819 sof = (cfm >> 0) & 0x7f;
1820 sol = (cfm >> 7) & 0x7f;
1821 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1823 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1824 && sysent[tcp->scno].nargs != -1)
1825 tcp->u_nargs = sysent[tcp->scno].nargs;
1827 tcp->u_nargs = MAX_ARGS;
1828 for (i = 0; i < tcp->u_nargs; ++i) {
1829 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1830 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1836 if (/* EBX = out0 */
1837 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1839 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1841 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1843 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1845 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1847 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1850 for (i = 0; i < 6; ++i)
1851 /* truncate away IVE sign-extension */
1852 tcp->u_arg[i] &= 0xffffffff;
1854 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1855 && sysent[tcp->scno].nargs != -1)
1856 tcp->u_nargs = sysent[tcp->scno].nargs;
1861 #elif defined (MIPS)
1866 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1867 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1869 nargs = tcp->u_nargs = MAX_ARGS;
1871 if(upeek(pid, REG_SP, &sp) < 0)
1873 for(i = 0; i < 4; i++) {
1874 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1877 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1878 (char *)(tcp->u_arg + 4));
1880 for(i = 0; i < nargs; i++) {
1881 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1886 #elif defined (POWERPC)
1888 #define PT_ORIG_R3 34
1892 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1893 tcp->u_nargs = sysent[tcp->scno].nargs;
1895 tcp->u_nargs = MAX_ARGS;
1896 for (i = 0; i < tcp->u_nargs; i++) {
1897 if (upeek(pid, (i==0) ?
1898 (sizeof(unsigned long)*PT_ORIG_R3) :
1899 ((i+PT_R3)*sizeof(unsigned long)),
1900 &tcp->u_arg[i]) < 0)
1904 #elif defined (SPARC)
1908 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1909 tcp->u_nargs = sysent[tcp->scno].nargs;
1911 tcp->u_nargs = MAX_ARGS;
1912 for (i = 0; i < tcp->u_nargs; i++)
1913 tcp->u_arg[i] = *((®s.r_o0) + i);
1915 #elif defined (HPPA)
1919 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1920 tcp->u_nargs = sysent[tcp->scno].nargs;
1922 tcp->u_nargs = MAX_ARGS;
1923 for (i = 0; i < tcp->u_nargs; i++) {
1924 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1932 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1933 tcp->u_nargs = sysent[tcp->scno].nargs;
1935 tcp->u_nargs = MAX_ARGS;
1936 for (i = 0; i < tcp->u_nargs; i++)
1937 tcp->u_arg[i] = regs.uregs[i];
1942 static int syscall_regs[] = {
1943 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
1944 REG_REG0, REG_REG0+1, REG_REG0+2
1947 tcp->u_nargs = sysent[tcp->scno].nargs;
1948 for (i = 0; i < tcp->u_nargs; i++) {
1949 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
1956 /* Registers used by SH5 Linux system calls for parameters */
1957 static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
1960 * TODO: should also check that the number of arguments encoded
1961 * in the trap number matches the number strace expects.
1964 assert(sysent[tcp->scno].nargs <
1965 sizeof(syscall_regs)/sizeof(syscall_regs[0]));
1968 tcp->u_nargs = sysent[tcp->scno].nargs;
1969 for (i = 0; i < tcp->u_nargs; i++) {
1970 if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1975 #elif defined(X86_64)
1978 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
1979 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
1980 {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */
1983 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1984 tcp->u_nargs = sysent[tcp->scno].nargs;
1986 tcp->u_nargs = MAX_ARGS;
1987 for (i = 0; i < tcp->u_nargs; i++) {
1988 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
1992 #else /* Other architecture (like i386) (32bits specific) */
1995 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1996 tcp->u_nargs = sysent[tcp->scno].nargs;
1998 tcp->u_nargs = MAX_ARGS;
1999 for (i = 0; i < tcp->u_nargs; i++) {
2000 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
2009 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2010 tcp->u_nargs = sysent[tcp->scno].nargs;
2012 tcp->u_nargs = MAX_ARGS;
2013 for (i = 0; i < tcp->u_nargs; i++) {
2016 if (upeek(pid, uoff(u_arg[0]) +
2017 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
2025 * SGI is broken: even though it has pr_sysarg, it doesn't
2026 * set them on system call entry. Get a clue.
2028 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2029 tcp->u_nargs = sysent[tcp->scno].nargs;
2031 tcp->u_nargs = tcp->status.pr_nsysarg;
2032 if (tcp->u_nargs > 4) {
2033 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2034 4*sizeof(tcp->u_arg[0]));
2035 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
2036 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
2039 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2040 tcp->u_nargs*sizeof(tcp->u_arg[0]));
2044 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
2046 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2047 tcp->u_nargs = sysent[tcp->scno].nargs;
2049 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
2050 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2051 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2052 #elif defined (HAVE_PR_SYSCALL)
2053 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2054 tcp->u_nargs = sysent[tcp->scno].nargs;
2056 tcp->u_nargs = tcp->status.pr_nsysarg;
2059 for (i = 0; i < tcp->u_nargs; i++)
2060 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
2062 #elif defined (I386)
2063 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2064 tcp->u_nargs = sysent[tcp->scno].nargs;
2067 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2068 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2070 I DONT KNOW WHAT TO DO
2071 #endif /* !HAVE_PR_SYSCALL */
2074 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2075 sysent[tcp->scno].nargs > tcp->status.val)
2076 tcp->u_nargs = sysent[tcp->scno].nargs;
2078 tcp->u_nargs = tcp->status.val;
2079 if (tcp->u_nargs < 0)
2081 if (tcp->u_nargs > MAX_ARGS)
2082 tcp->u_nargs = MAX_ARGS;
2083 switch(regs.r_eax) {
2085 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2086 regs.r_esp + sizeof(int) + sizeof(quad_t));
2089 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2090 regs.r_esp + 2 * sizeof(int));
2093 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2094 regs.r_esp + sizeof(int));
2097 #endif /* FREEBSD */
2109 /* Measure the exit time as early as possible to avoid errors. */
2110 if (dtime && (tcp->flags & TCB_INSYSCALL))
2111 gettimeofday(&tv, NULL);
2113 res = get_scno(tcp);
2117 res = syscall_fixup(tcp);
2121 if (tcp->flags & TCB_INSYSCALL) {
2123 res = get_error(tcp);
2127 internal_syscall(tcp);
2128 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2129 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2130 tcp->flags &= ~TCB_INSYSCALL;
2134 if (tcp->flags & TCB_REPRINT) {
2137 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2138 tprintf("syscall_%lu", tcp->scno);
2140 tprintf("%s", sysent[tcp->scno].sys_name);
2141 tprintf(" resumed> ");
2144 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
2145 call_count[tcp->scno]++;
2147 error_count[tcp->scno]++;
2148 tv_sub(&tv, &tv, &tcp->etime);
2150 if (tv_cmp(&tv, &tcp->dtime) > 0) {
2151 static struct timeval one_tick;
2152 if (one_tick.tv_usec == 0) {
2153 /* Initialize it. */
2154 struct itimerval it;
2155 memset(&it, 0, sizeof it);
2156 it.it_interval.tv_usec = 1;
2157 setitimer(ITIMER_REAL, &it, NULL);
2158 getitimer(ITIMER_REAL, &it);
2159 one_tick = it.it_interval;
2162 if (tv_nz(&tcp->dtime))
2164 else if (tv_cmp(&tv, &one_tick) > 0) {
2165 if (tv_cmp(&shortest, &one_tick) < 0)
2172 if (tv_cmp(&tv, &shortest) < 0)
2174 tv_add(&tv_count[tcp->scno],
2175 &tv_count[tcp->scno], &tv);
2176 tcp->flags &= ~TCB_INSYSCALL;
2180 if (tcp->scno >= nsyscalls || tcp->scno < 0
2181 || (qual_flags[tcp->scno] & QUAL_RAW))
2182 sys_res = printargs(tcp);
2184 if (not_failing_only && tcp->u_error)
2185 return 0; /* ignore failed syscalls */
2186 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2188 u_error = tcp->u_error;
2191 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2192 qual_flags[tcp->scno] & QUAL_RAW) {
2194 tprintf("= -1 (errno %ld)", u_error);
2196 tprintf("= %#lx", tcp->u_rval);
2198 else if (!(sys_res & RVAL_NONE) && u_error) {
2202 tprintf("= ? ERESTARTSYS (To be restarted)");
2204 case ERESTARTNOINTR:
2205 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2207 case ERESTARTNOHAND:
2208 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2210 case ERESTART_RESTARTBLOCK:
2211 tprintf("= ? ERESTART_RESTARTBLOCK (To be restarted)");
2217 tprintf("E??? (errno %ld)", u_error);
2218 else if (u_error < nerrnos)
2219 tprintf("%s (%s)", errnoent[u_error],
2222 tprintf("ERRNO_%ld (%s)", u_error,
2228 if (sys_res & RVAL_NONE)
2231 switch (sys_res & RVAL_MASK) {
2233 tprintf("= %#lx", tcp->u_rval);
2236 tprintf("= %#lo", tcp->u_rval);
2239 tprintf("= %lu", tcp->u_rval);
2242 tprintf("= %ld", tcp->u_rval);
2244 #ifdef HAVE_LONG_LONG
2246 tprintf("= %#llx", tcp->u_lrval);
2249 tprintf("= %#llo", tcp->u_lrval);
2251 case RVAL_LUDECIMAL:
2252 tprintf("= %llu", tcp->u_lrval);
2255 tprintf("= %lld", tcp->u_lrval);
2260 "invalid rval format\n");
2264 if ((sys_res & RVAL_STR) && tcp->auxstr)
2265 tprintf(" (%s)", tcp->auxstr);
2268 tv_sub(&tv, &tv, &tcp->etime);
2269 tprintf(" <%ld.%06ld>",
2270 (long) tv.tv_sec, (long) tv.tv_usec);
2275 if (fflush(tcp->outf) == EOF)
2277 tcp->flags &= ~TCB_INSYSCALL;
2281 /* Entering system call */
2282 res = syscall_enter(tcp);
2286 switch (tcp->scno + NR_SYSCALL_BASE) {
2288 #if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
2289 case SYS_socketcall:
2290 decode_subcall(tcp, SYS_socket_subcall,
2291 SYS_socket_nsubcalls, deref_style);
2294 decode_subcall(tcp, SYS_ipc_subcall,
2295 SYS_ipc_nsubcalls, shift_style);
2297 #endif /* !ALPHA && !MIPS && !SPARC && !HPPA && !X86_64 */
2299 case SYS_socketcall:
2300 sparc_socket_decode (tcp);
2305 #ifdef SYS_pgrpsys_subcall
2307 decode_subcall(tcp, SYS_pgrpsys_subcall,
2308 SYS_pgrpsys_nsubcalls, shift_style);
2310 #endif /* SYS_pgrpsys_subcall */
2311 #ifdef SYS_sigcall_subcall
2313 decode_subcall(tcp, SYS_sigcall_subcall,
2314 SYS_sigcall_nsubcalls, mask_style);
2316 #endif /* SYS_sigcall_subcall */
2318 decode_subcall(tcp, SYS_msgsys_subcall,
2319 SYS_msgsys_nsubcalls, shift_style);
2322 decode_subcall(tcp, SYS_shmsys_subcall,
2323 SYS_shmsys_nsubcalls, shift_style);
2326 decode_subcall(tcp, SYS_semsys_subcall,
2327 SYS_semsys_nsubcalls, shift_style);
2331 decode_subcall(tcp, SYS_utssys_subcall,
2332 SYS_utssys_nsubcalls, shift_style);
2336 decode_subcall(tcp, SYS_sysfs_subcall,
2337 SYS_sysfs_nsubcalls, shift_style);
2340 decode_subcall(tcp, SYS_spcall_subcall,
2341 SYS_spcall_nsubcalls, shift_style);
2343 #ifdef SYS_context_subcall
2345 decode_subcall(tcp, SYS_context_subcall,
2346 SYS_context_nsubcalls, shift_style);
2348 #endif /* SYS_context_subcall */
2349 #ifdef SYS_door_subcall
2351 decode_subcall(tcp, SYS_door_subcall,
2352 SYS_door_nsubcalls, door_style);
2354 #endif /* SYS_door_subcall */
2355 #ifdef SYS_kaio_subcall
2357 decode_subcall(tcp, SYS_kaio_subcall,
2358 SYS_kaio_nsubcalls, shift_style);
2366 decode_subcall(tcp, 0, 0, table_style);
2371 decode_subcall(tcp, SYS_semsys_subcall,
2372 SYS_semsys_nsubcalls, shift_style);
2375 decode_subcall(tcp, SYS_msgsys_subcall,
2376 SYS_msgsys_nsubcalls, shift_style);
2379 decode_subcall(tcp, SYS_shmsys_subcall,
2380 SYS_shmsys_nsubcalls, shift_style);
2385 internal_syscall(tcp);
2386 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2387 tcp->flags |= TCB_INSYSCALL;
2392 gettimeofday(&tcp->etime, NULL);
2393 tcp->flags |= TCB_INSYSCALL;
2398 tcp->flags &= ~TCB_REPRINT;
2400 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2401 tprintf("syscall_%lu(", tcp->scno);
2403 tprintf("%s(", sysent[tcp->scno].sys_name);
2404 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2405 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2406 sys_res = printargs(tcp);
2408 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2409 if (fflush(tcp->outf) == EOF)
2411 tcp->flags |= TCB_INSYSCALL;
2412 /* Measure the entrance time as late as possible to avoid errors. */
2414 gettimeofday(&tcp->etime, NULL);
2422 if (entering(tcp)) {
2425 for (i = 0; i < tcp->u_nargs; i++)
2426 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2440 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
2444 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2448 if (upeek(tcp->pid, PT_R9, &val) < 0)
2453 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2459 val = tcp->status.PR_REG[R_O1];
2462 val = tcp->status.PR_REG[EDX];
2465 val = tcp->status.PR_REG[RDX];
2468 val = tcp->status.PR_REG[CTX_V1];
2473 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
2480 * Apparently, indirect system calls have already be converted by ptrace(2),
2481 * so if you see "indir" this program has gone astray.
2489 if (entering(tcp)) {
2490 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2491 fprintf(stderr, "Bogus syscall: %u\n", scno);
2494 nargs = sysent[scno].nargs;
2495 tprintf("%s", sysent[scno].sys_name);
2496 for (i = 0; i < nargs; i++)
2497 tprintf(", %#lx", tcp->u_arg[i+1]);
2507 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2515 return strcmp(sysent[*((int *) a)].sys_name,
2516 sysent[*((int *) b)].sys_name);
2524 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2526 return (m < n) ? 1 : (m > n) ? -1 : 0;
2529 static int (*sortfun)();
2530 static struct timeval overhead = { -1, -1 };
2536 if (strcmp(sortby, "time") == 0)
2538 else if (strcmp(sortby, "calls") == 0)
2539 sortfun = count_cmp;
2540 else if (strcmp(sortby, "name") == 0)
2541 sortfun = syscall_cmp;
2542 else if (strcmp(sortby, "nothing") == 0)
2545 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2550 void set_overhead(n)
2553 overhead.tv_sec = n / 1000000;
2554 overhead.tv_usec = n % 1000000;
2562 int call_cum, error_cum;
2563 struct timeval tv_cum, dtv;
2565 char *dashes = "-------------------------";
2568 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2569 if (overhead.tv_sec == -1) {
2570 tv_mul(&overhead, &shortest, 8);
2571 tv_div(&overhead, &overhead, 10);
2573 for (i = 0; i < nsyscalls; i++) {
2574 sorted_count[i] = i;
2575 if (call_count[i] == 0)
2577 tv_mul(&dtv, &overhead, call_count[i]);
2578 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2579 call_cum += call_count[i];
2580 error_cum += error_count[i];
2581 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2584 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2585 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2586 "% time", "seconds", "usecs/call",
2587 "calls", "errors", "syscall");
2588 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2589 dashes, dashes, dashes, dashes, dashes, dashes);
2590 for (i = 0; i < nsyscalls; i++) {
2591 j = sorted_count[i];
2592 if (call_count[j] == 0)
2594 tv_div(&dtv, &tv_count[j], call_count[j]);
2596 sprintf(error_str, "%d", error_count[j]);
2598 error_str[0] = '\0';
2599 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2600 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2601 percent, (long) tv_count[j].tv_sec,
2602 (long) tv_count[j].tv_usec,
2603 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2604 call_count[j], error_str, sysent[j].sys_name);
2606 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2607 dashes, dashes, dashes, dashes, dashes, dashes);
2609 sprintf(error_str, "%d", error_cum);
2611 error_str[0] = '\0';
2612 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2613 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2614 call_cum, error_str, "total");