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);
307 for (i = 0; s[i]; i++)
308 s[i] = toupper((unsigned char)(s[i]));
309 if (strncmp(s, "SIG", 3) == 0)
311 for (i = 0; i <= NSIG; i++)
312 if (strcmp(s, signame(i) + 3) == 0) {
313 qualify_one(atoi(s), opt, not);
320 qual_fault(s, opt, not)
322 struct qual_options *opt;
329 qual_desc(s, opt, not)
331 struct qual_options *opt;
334 if (s && *s && isdigit((unsigned char)*s)) {
335 qualify_one(atoi(s), opt, not);
345 if (strcmp(s, "file") == 0)
347 if (strcmp(s, "ipc") == 0)
349 if (strcmp(s, "network") == 0)
350 return TRACE_NETWORK;
351 if (strcmp(s, "process") == 0)
352 return TRACE_PROCESS;
353 if (strcmp(s, "signal") == 0)
362 struct qual_options *opt;
367 opt = &qual_options[0];
368 for (i = 0; (p = qual_options[i].option_name); i++) {
370 if (strncmp(s, p, n) == 0 && s[n] == '=') {
371 opt = &qual_options[i];
381 if (strcmp(s, "none") == 0) {
385 if (strcmp(s, "all") == 0) {
386 for (i = 0; i < MAX_QUALS; i++) {
388 qual_flags[i] &= ~opt->bitflag;
390 qual_flags[i] |= opt->bitflag;
394 for (i = 0; i < MAX_QUALS; i++) {
396 qual_flags[i] |= opt->bitflag;
398 qual_flags[i] &= ~opt->bitflag;
400 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
401 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
402 for (i = 0; i < MAX_QUALS; i++) {
403 if (sysent[i].sys_flags & n) {
405 qual_flags[i] &= ~opt->bitflag;
407 qual_flags[i] |= opt->bitflag;
412 if (opt->qualify(p, opt, not)) {
413 fprintf(stderr, "strace: invalid %s `%s'\n",
414 opt->argument_name, p);
427 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
429 switch (tcp->scno + NR_SYSCALL_BASE) {
437 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
438 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
447 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
448 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
452 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
453 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
459 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
460 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
467 enum subcall_style { shift_style, deref_style, mask_style, door_style };
469 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
477 const struct subcall subcalls_table[] = {
478 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
480 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
482 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
484 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
488 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
490 const int socket_map [] = {
493 /* SYS_CONNECT */ 98,
494 /* SYS_LISTEN */ 106,
496 /* SYS_GETSOCKNAME */ 150,
497 /* SYS_GETPEERNAME */ 141,
498 /* SYS_SOCKETPAIR */ 135,
501 /* SYS_SENDTO */ 133,
502 /* SYS_RECVFROM */ 125,
503 /* SYS_SHUTDOWN */ 134,
504 /* SYS_SETSOCKOPT */ 105,
505 /* SYS_GETSOCKOPT */ 118,
506 /* SYS_SENDMSG */ 114,
507 /* SYS_RECVMSG */ 113
511 sparc_socket_decode (tcp)
517 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
520 tcp->scno = socket_map [tcp->u_arg [0]-1];
521 n = tcp->u_nargs = sysent [tcp->scno].nargs;
522 addr = tcp->u_arg [1];
523 for (i = 0; i < n; i++){
525 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
527 tcp->u_arg [i] = arg;
528 addr += sizeof (arg);
533 decode_subcall(tcp, subcall, nsubcalls, style)
537 enum subcall_style style;
539 long addr, mask, arg;
544 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
546 tcp->scno = subcall + tcp->u_arg[0];
547 if (sysent[tcp->scno].nargs != -1)
548 tcp->u_nargs = sysent[tcp->scno].nargs;
551 for (i = 0; i < tcp->u_nargs; i++)
552 tcp->u_arg[i] = tcp->u_arg[i + 1];
555 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
557 tcp->scno = subcall + tcp->u_arg[0];
558 addr = tcp->u_arg[1];
559 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
560 if (umove(tcp, addr, &arg) < 0)
565 tcp->u_nargs = sysent[tcp->scno].nargs;
568 mask = (tcp->u_arg[0] >> 8) & 0xff;
569 for (i = 0; mask; i++)
573 tcp->u_arg[0] &= 0xff;
574 tcp->scno = subcall + i;
575 if (sysent[tcp->scno].nargs != -1)
576 tcp->u_nargs = sysent[tcp->scno].nargs;
580 * Oh, yuck. The call code is the *sixth* argument.
581 * (don't you mean the *last* argument? - JH)
583 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
585 tcp->scno = subcall + tcp->u_arg[5];
586 if (sysent[tcp->scno].nargs != -1)
587 tcp->u_nargs = sysent[tcp->scno].nargs;
593 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
594 if (subcalls_table[i].call == tcp->scno) break;
595 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
596 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
597 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
598 for (i = 0; i < tcp->u_nargs; i++)
599 tcp->u_arg[i] = tcp->u_arg[i + 1];
607 struct tcb *tcp_last = NULL;
610 internal_syscall(tcp)
614 * We must always trace a few critical system calls in order to
615 * correctly support following forks in the presence of tracing
618 switch (tcp->scno + NR_SYSCALL_BASE) {
688 #ifdef __NR_exit_group
689 case __NR_exit_group:
692 case 252: /* IA-32 __NR_exit_group */
707 #elif defined (POWERPC)
708 static long result,flags;
712 static struct pt_regs regs;
713 #elif defined (ALPHA)
716 #elif defined (SPARC)
717 static struct regs regs;
718 static unsigned long trap;
722 #elif defined(S390) || defined(S390X)
725 static long syscall_mode;
732 #elif defined(X86_64)
750 #if defined(S390) || defined(S390X)
751 if (tcp->flags & TCB_WAITEXECVE) {
753 * When the execve system call completes successfully, the
754 * new process still has -ENOSYS (old style) or __NR_execve
755 * (new style) in gpr2. We cannot recover the scno again
756 * by disassembly, because the image that executed the
757 * syscall is gone now. Fortunately, we don't want it. We
758 * leave the flag set so that syscall_fixup can fake the
761 if (tcp->flags & TCB_INSYSCALL)
764 * This is the SIGTRAP after execve. We cannot try to read
765 * the system call here either.
767 tcp->flags &= ~TCB_WAITEXECVE;
771 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
774 if (syscall_mode != -ENOSYS) {
776 * Since kernel version 2.5.44 the scno gets passed in gpr2.
781 * Old style of "passing" the scno via the SVC instruction.
784 long opcode, offset_reg, tmp;
786 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
787 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
788 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
789 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
791 if (upeek(pid, PT_PSWADDR, &pc) < 0)
794 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
796 perror("peektext(pc-oneword)");
801 * We have to check if the SVC got executed directly or via an
802 * EXECUTE instruction. In case of EXECUTE it is necessary to do
803 * instruction decoding to derive the system call number.
804 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
805 * so that this doesn't work if a SVC opcode is part of an EXECUTE
806 * opcode. Since there is no way to find out the opcode size this
807 * is the best we can do...
810 if ((opcode & 0xff00) == 0x0a00) {
812 scno = opcode & 0xff;
815 /* SVC got executed by EXECUTE instruction */
818 * Do instruction decoding of EXECUTE. If you really want to
819 * understand this, read the Principles of Operations.
821 svc_addr = (void *) (opcode & 0xfff);
824 offset_reg = (opcode & 0x000f0000) >> 16;
825 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
830 offset_reg = (opcode & 0x0000f000) >> 12;
831 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
835 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
844 offset_reg = (opcode & 0x00f00000) >> 20;
845 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
848 scno = (scno | tmp) & 0xff;
851 #elif defined (POWERPC)
852 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
854 if (!(tcp->flags & TCB_INSYSCALL)) {
855 /* Check if we return from execve. */
856 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
857 tcp->flags &= ~TCB_WAITEXECVE;
862 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
864 #elif defined (X86_64)
865 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
868 if (!(tcp->flags & TCB_INSYSCALL)) {
869 static int currpers=-1;
872 /* Check CS register value. On x86-64 linux it is:
873 * 0x33 for long mode (64 bit)
874 * 0x23 for compatibility mode (32 bit)
875 * It takes only one ptrace and thus doesn't need
878 if (upeek(pid, 8*CS, &val) < 0)
882 case 0x23: currpers = 1; break;
883 case 0x33: currpers = 0; break;
885 fprintf(stderr, "Unknown value CS=0x%02X while "
886 "detecting personality of process "
887 "PID=%d\n", (int)val, pid);
888 currpers = current_personality;
892 /* This version analyzes the opcode of a syscall instruction.
893 * (int 0x80 on i386 vs. syscall on x86-64)
894 * It works, but is too complicated.
896 unsigned long val, rip, i;
898 if(upeek(pid, 8*RIP, &rip)<0)
899 perror("upeek(RIP)");
901 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
905 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
907 printf("ptrace_peektext failed: %s\n",
909 switch (call & 0xffff)
911 /* x86-64: syscall = 0x0f 0x05 */
912 case 0x050f: currpers = 0; break;
913 /* i386: int 0x80 = 0xcd 0x80 */
914 case 0x80cd: currpers = 1; break;
916 currpers = current_personality;
918 "Unknown syscall opcode (0x%04X) while "
919 "detecting personality of process "
920 "PID=%d\n", (int)call, pid);
924 if(currpers != current_personality)
926 char *names[]={"64 bit", "32 bit"};
927 set_personality(currpers);
928 printf("[ Process PID=%d runs in %s mode. ]\n",
929 pid, names[current_personality]);
933 # define IA64_PSR_IS ((long)1 << 34)
934 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
935 ia32 = (psr & IA64_PSR_IS) != 0;
936 if (!(tcp->flags & TCB_INSYSCALL)) {
938 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
941 if (upeek (pid, PT_R15, &scno) < 0)
944 /* Check if we return from execve. */
945 if (tcp->flags & TCB_WAITEXECVE) {
946 tcp->flags &= ~TCB_WAITEXECVE;
950 /* syscall in progress */
951 if (upeek (pid, PT_R8, &r8) < 0)
953 if (upeek (pid, PT_R10, &r10) < 0)
958 * Read complete register set in one go.
960 if (ptrace(PTRACE_GETREGS, pid, NULL, (void *)®s) == -1)
964 * We only need to grab the syscall number on syscall entry.
966 if (regs.ARM_ip == 0) {
968 * Note: we only deal with only 32-bit CPUs here.
970 if (regs.ARM_cpsr & 0x20) {
972 * Get the Thumb-mode system call number
977 * Get the ARM-mode system call number
980 scno = ptrace(PTRACE_PEEKTEXT, pid, (void *)(regs.ARM_pc - 4), NULL);
984 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
985 tcp->flags &= ~TCB_WAITEXECVE;
989 if ((scno & 0x0ff00000) != 0x0f900000) {
990 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
996 * Fixup the syscall number
1001 if (tcp->flags & TCB_INSYSCALL) {
1002 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
1003 tcp->flags &= ~TCB_INSYSCALL;
1006 if (!(tcp->flags & TCB_INSYSCALL)) {
1007 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1008 tcp->flags |= TCB_INSYSCALL;
1011 #elif defined (M68K)
1012 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
1014 #elif defined (MIPS)
1015 if (upeek(pid, REG_A3, &a3) < 0)
1018 if(!(tcp->flags & TCB_INSYSCALL)) {
1019 if (upeek(pid, REG_V0, &scno) < 0)
1022 if (scno < 0 || scno > nsyscalls) {
1023 if(a3 == 0 || a3 == -1) {
1025 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
1030 if (upeek(pid, REG_V0, &r2) < 0)
1033 #elif defined (ALPHA)
1034 if (upeek(pid, REG_A3, &a3) < 0)
1037 if (!(tcp->flags & TCB_INSYSCALL)) {
1038 if (upeek(pid, REG_R0, &scno) < 0)
1041 /* Check if we return from execve. */
1042 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1043 tcp->flags &= ~TCB_WAITEXECVE;
1048 * Do some sanity checks to figure out if it's
1049 * really a syscall entry
1051 if (scno < 0 || scno > nsyscalls) {
1052 if (a3 == 0 || a3 == -1) {
1054 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
1060 if (upeek(pid, REG_R0, &r0) < 0)
1063 #elif defined (SPARC)
1064 /* Everything we need is in the current register set. */
1065 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
1068 /* If we are entering, then disassemble the syscall trap. */
1069 if (!(tcp->flags & TCB_INSYSCALL)) {
1070 /* Retrieve the syscall trap instruction. */
1072 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
1076 /* Disassemble the trap to see what personality to use. */
1079 /* Linux/SPARC syscall trap. */
1083 /* Linux/SPARC64 syscall trap. */
1084 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
1087 /* SunOS syscall trap. (pers 1) */
1088 fprintf(stderr,"syscall: SunOS no support\n");
1091 /* Solaris 2.x syscall trap. (per 2) */
1095 /* NetBSD/FreeBSD syscall trap. */
1096 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1099 /* Solaris 2.x gettimeofday */
1103 /* Unknown syscall trap. */
1104 if(tcp->flags & TCB_WAITEXECVE) {
1105 tcp->flags &= ~TCB_WAITEXECVE;
1108 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
1112 /* Extract the system call number from the registers. */
1113 if (trap == 0x91d02027)
1119 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
1123 if (upeek(pid, PT_GR20, &scno) < 0)
1125 if (!(tcp->flags & TCB_INSYSCALL)) {
1126 /* Check if we return from execve. */
1127 if ((tcp->flags & TCB_WAITEXECVE)) {
1128 tcp->flags &= ~TCB_WAITEXECVE;
1134 * In the new syscall ABI, the system call number is in R3.
1136 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1140 /* Odd as it may seem, a glibc bug has been known to cause
1141 glibc to issue bogus negative syscall numbers. So for
1142 our purposes, make strace print what it *should* have been */
1143 long correct_scno = (scno & 0xff);
1146 "Detected glibc bug: bogus system call number = %ld, "
1147 "correcting to %ld\n",
1150 scno = correct_scno;
1154 if (!(tcp->flags & TCB_INSYSCALL)) {
1155 /* Check if we return from execve. */
1156 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1157 tcp->flags &= ~TCB_WAITEXECVE;
1162 if (upeek(pid, REG_SYSCALL, &scno) < 0)
1166 if (!(tcp->flags & TCB_INSYSCALL)) {
1167 /* Check if we return from execve. */
1168 if (tcp->flags & TCB_WAITEXECVE) {
1169 tcp->flags &= ~TCB_WAITEXECVE;
1176 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1179 /* new syscall ABI returns result in R0 */
1180 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1183 /* ABI defines result returned in r9 */
1184 if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
1189 #ifdef HAVE_PR_SYSCALL
1190 scno = tcp->status.PR_SYSCALL;
1191 #else /* !HAVE_PR_SYSCALL */
1193 scno = tcp->status.PR_WHAT;
1195 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1199 switch (regs.r_eax) {
1202 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1208 #endif /* FREEBSD */
1209 #endif /* !HAVE_PR_SYSCALL */
1210 #endif /* USE_PROCFS */
1211 if (!(tcp->flags & TCB_INSYSCALL))
1223 #else /* USE_PROCFS */
1224 int scno = tcp->scno;
1226 if (!(tcp->flags & TCB_INSYSCALL)) {
1227 if (tcp->status.PR_WHY != PR_SYSENTRY) {
1231 || scno == SYS_vfork
1232 #endif /* SYS_vfork */
1234 || scno == SYS_fork1
1235 #endif /* SYS_fork1 */
1237 || scno == SYS_forkall
1238 #endif /* SYS_forkall */
1240 || scno == SYS_rfork1
1241 #endif /* SYS_fork1 */
1243 || scno == SYS_rforkall
1244 #endif /* SYS_rforkall */
1246 /* We are returning in the child, fake it. */
1247 tcp->status.PR_WHY = PR_SYSENTRY;
1249 tcp->status.PR_WHY = PR_SYSEXIT;
1252 fprintf(stderr, "syscall: missing entry\n");
1253 tcp->flags |= TCB_INSYSCALL;
1258 if (tcp->status.PR_WHY != PR_SYSEXIT) {
1259 fprintf(stderr, "syscall: missing exit\n");
1260 tcp->flags &= ~TCB_INSYSCALL;
1263 #endif /* USE_PROCFS */
1265 if (!(tcp->flags & TCB_INSYSCALL)) {
1267 fprintf(stderr, "syscall: missing entry\n");
1268 tcp->flags |= TCB_INSYSCALL;
1275 * This happens when a signal handler
1276 * for a signal which interrupted a
1277 * a system call makes another system call.
1279 fprintf(stderr, "syscall: missing exit\n");
1281 tcp->flags &= ~TCB_INSYSCALL;
1287 if (upeek(pid, 4*EAX, &eax) < 0)
1289 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1291 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1294 #elif defined (X86_64)
1295 if (upeek(pid, 8*RAX, &rax) < 0)
1297 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1299 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1302 #elif defined (S390) || defined (S390X)
1303 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1305 if (syscall_mode != -ENOSYS)
1306 syscall_mode = tcp->scno;
1307 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
1309 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1312 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1313 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1314 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1316 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1317 * flag set for the post-execve SIGTRAP to see and reset.
1321 #elif defined (POWERPC)
1322 # define SO_MASK 0x10000000
1323 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1325 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
1327 if (flags & SO_MASK)
1329 #elif defined (M68K)
1330 if (upeek(pid, 4*PT_D0, &d0) < 0)
1332 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1334 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1341 #elif defined (HPPA)
1342 if (upeek(pid, PT_GR28, &r28) < 0)
1345 if (upeek(pid, PT_R10, &r10) < 0)
1347 if (upeek(pid, PT_R8, &r8) < 0)
1349 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1351 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1365 #if defined(S390) || defined(S390X)
1366 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1374 #else /* !S390 && !S390X */
1376 if (eax < 0 && -eax < nerrnos) {
1386 if (rax < 0 && -rax < nerrnos) {
1400 if (err < 0 && -err < nerrnos) {
1428 if (result && (unsigned long) -result < nerrnos) {
1433 tcp->u_rval = result;
1436 #else /* !POWERPC */
1438 if (d0 && (unsigned) -d0 < nerrnos) {
1448 if (regs.ARM_r0 && (unsigned) -regs.ARM_r0 < nerrnos) {
1450 u_error = -regs.ARM_r0;
1453 tcp->u_rval = regs.ARM_r0;
1468 if (regs.r_psr & PSR_C) {
1470 u_error = regs.r_o0;
1473 tcp->u_rval = regs.r_o0;
1478 if (r28 && (unsigned) -r28 < nerrnos) {
1488 /* interpret R0 as return value or error number */
1489 if (r0 && (unsigned) -r0 < nerrnos) {
1499 /* interpret result as return value or error number */
1500 if (r9 && (unsigned) -r9 < nerrnos) {
1515 #endif /* POWERPC */
1520 #endif /* S390 || S390X */
1523 /* get error code from user struct */
1524 if (upeek(pid, uoff(u_error), &u_error) < 0)
1526 u_error >>= 24; /* u_error is a char */
1528 /* get system call return value */
1529 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1534 /* Judicious guessing goes a long way. */
1535 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1537 u_error = tcp->status.pr_reg[R_O0];
1540 tcp->u_rval = tcp->status.pr_reg[R_O0];
1545 /* Wanna know how to kill an hour single-stepping? */
1546 if (tcp->status.PR_REG[EFL] & 0x1) {
1548 u_error = tcp->status.PR_REG[EAX];
1551 tcp->u_rval = tcp->status.PR_REG[EAX];
1552 #ifdef HAVE_LONG_LONG
1554 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1555 tcp->status.PR_REG[EAX];
1561 /* Wanna know how to kill an hour single-stepping? */
1562 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1564 u_error = tcp->status.PR_REG[RAX];
1567 tcp->u_rval = tcp->status.PR_REG[RAX];
1572 if (tcp->status.pr_reg[CTX_A3]) {
1574 u_error = tcp->status.pr_reg[CTX_V0];
1577 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1583 if (regs.r_eflags & PSL_C) {
1585 u_error = regs.r_eax;
1587 tcp->u_rval = regs.r_eax;
1589 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1592 #endif /* FREEBSD */
1593 tcp->u_error = u_error;
1598 force_result(tcp, error, rval)
1604 #if defined(S390) || defined(S390X)
1605 gpr2 = error ? -error : rval;
1606 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1608 #else /* !S390 && !S390X */
1610 eax = error ? -error : rval;
1611 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1615 rax = error ? -error : rval;
1616 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 4), rax) < 0)
1621 r8 = error ? -error : rval;
1622 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1634 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1635 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1648 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1649 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1653 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1663 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1664 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
1666 #else /* !POWERPC */
1668 d0 = error ? -error : rval;
1669 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1673 regs.ARM_r0 = error ? -error : rval;
1674 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), regs.ARM_r0) < 0)
1686 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1687 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1691 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1694 regs.r_psr |= PSR_C;
1698 regs.r_psr &= ~PSR_C;
1701 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1705 r28 = error ? -error : rval;
1706 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1710 r0 = error ? -error : rval;
1711 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1715 r9 = error ? -error : rval;
1716 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1725 #endif /* POWERPC */
1730 #endif /* S390 || S390X */
1733 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1735 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1743 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1748 regs.r_eflags |= PSL_C;
1752 regs.r_eflags &= ~PSL_C;
1755 if (pwrite(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1759 #endif /* FREEBSD */
1761 /* All branches reach here on success (only). */
1762 tcp->u_error = error;
1767 int syscall_enter(tcp)
1772 #endif /* !USE_PROCFS */
1774 #if defined(S390) || defined(S390X)
1777 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1778 tcp->u_nargs = sysent[tcp->scno].nargs;
1780 tcp->u_nargs = MAX_ARGS;
1781 for (i = 0; i < tcp->u_nargs; i++) {
1782 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
1786 #elif defined (ALPHA)
1789 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1790 tcp->u_nargs = sysent[tcp->scno].nargs;
1792 tcp->u_nargs = MAX_ARGS;
1793 for (i = 0; i < tcp->u_nargs; i++) {
1794 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1795 * for scno somewhere above here!
1797 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1801 #elif defined (IA64)
1804 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1805 /* be backwards compatible with kernel < 2.4.4... */
1807 # define PT_RBS_END PT_AR_BSP
1810 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
1812 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1815 sof = (cfm >> 0) & 0x7f;
1816 sol = (cfm >> 7) & 0x7f;
1817 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1819 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1820 && sysent[tcp->scno].nargs != -1)
1821 tcp->u_nargs = sysent[tcp->scno].nargs;
1823 tcp->u_nargs = MAX_ARGS;
1824 for (i = 0; i < tcp->u_nargs; ++i) {
1825 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1826 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1832 if (/* EBX = out0 */
1833 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1835 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1837 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1839 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1841 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1843 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1846 for (i = 0; i < 6; ++i)
1847 /* truncate away IVE sign-extension */
1848 tcp->u_arg[i] &= 0xffffffff;
1850 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1851 && sysent[tcp->scno].nargs != -1)
1852 tcp->u_nargs = sysent[tcp->scno].nargs;
1857 #elif defined (MIPS)
1862 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1863 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1865 nargs = tcp->u_nargs = MAX_ARGS;
1867 if(upeek(pid, REG_SP, &sp) < 0)
1869 for(i = 0; i < 4; i++) {
1870 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1873 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1874 (char *)(tcp->u_arg + 4));
1876 for(i = 0; i < nargs; i++) {
1877 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1882 #elif defined (POWERPC)
1884 #define PT_ORIG_R3 34
1888 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1889 tcp->u_nargs = sysent[tcp->scno].nargs;
1891 tcp->u_nargs = MAX_ARGS;
1892 for (i = 0; i < tcp->u_nargs; i++) {
1893 if (upeek(pid, (i==0) ?
1894 (sizeof(unsigned long)*PT_ORIG_R3) :
1895 ((i+PT_R3)*sizeof(unsigned long)),
1896 &tcp->u_arg[i]) < 0)
1900 #elif defined (SPARC)
1904 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1905 tcp->u_nargs = sysent[tcp->scno].nargs;
1907 tcp->u_nargs = MAX_ARGS;
1908 for (i = 0; i < tcp->u_nargs; i++)
1909 tcp->u_arg[i] = *((®s.r_o0) + i);
1911 #elif defined (HPPA)
1915 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1916 tcp->u_nargs = sysent[tcp->scno].nargs;
1918 tcp->u_nargs = MAX_ARGS;
1919 for (i = 0; i < tcp->u_nargs; i++) {
1920 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1928 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1929 tcp->u_nargs = sysent[tcp->scno].nargs;
1931 tcp->u_nargs = MAX_ARGS;
1932 for (i = 0; i < tcp->u_nargs; i++)
1933 tcp->u_arg[i] = regs.uregs[i];
1938 static int syscall_regs[] = {
1939 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
1940 REG_REG0, REG_REG0+1, REG_REG0+2
1943 tcp->u_nargs = sysent[tcp->scno].nargs;
1944 for (i = 0; i < tcp->u_nargs; i++) {
1945 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
1952 /* Registers used by SH5 Linux system calls for parameters */
1953 static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
1956 * TODO: should also check that the number of arguments encoded
1957 * in the trap number matches the number strace expects.
1960 assert(sysent[tcp->scno].nargs <
1961 sizeof(syscall_regs)/sizeof(syscall_regs[0]));
1964 tcp->u_nargs = sysent[tcp->scno].nargs;
1965 for (i = 0; i < tcp->u_nargs; i++) {
1966 if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1971 #elif defined(X86_64)
1974 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
1975 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
1976 {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */
1979 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1980 tcp->u_nargs = sysent[tcp->scno].nargs;
1982 tcp->u_nargs = MAX_ARGS;
1983 for (i = 0; i < tcp->u_nargs; i++) {
1984 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
1988 #else /* Other architecture (like i386) (32bits specific) */
1991 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1992 tcp->u_nargs = sysent[tcp->scno].nargs;
1994 tcp->u_nargs = MAX_ARGS;
1995 for (i = 0; i < tcp->u_nargs; i++) {
1996 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
2005 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2006 tcp->u_nargs = sysent[tcp->scno].nargs;
2008 tcp->u_nargs = MAX_ARGS;
2009 for (i = 0; i < tcp->u_nargs; i++) {
2012 if (upeek(pid, uoff(u_arg[0]) +
2013 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
2021 * SGI is broken: even though it has pr_sysarg, it doesn't
2022 * set them on system call entry. Get a clue.
2024 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2025 tcp->u_nargs = sysent[tcp->scno].nargs;
2027 tcp->u_nargs = tcp->status.pr_nsysarg;
2028 if (tcp->u_nargs > 4) {
2029 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2030 4*sizeof(tcp->u_arg[0]));
2031 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
2032 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
2035 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2036 tcp->u_nargs*sizeof(tcp->u_arg[0]));
2040 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
2042 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2043 tcp->u_nargs = sysent[tcp->scno].nargs;
2045 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
2046 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2047 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2048 #elif defined (HAVE_PR_SYSCALL)
2049 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2050 tcp->u_nargs = sysent[tcp->scno].nargs;
2052 tcp->u_nargs = tcp->status.pr_nsysarg;
2055 for (i = 0; i < tcp->u_nargs; i++)
2056 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
2058 #elif defined (I386)
2059 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2060 tcp->u_nargs = sysent[tcp->scno].nargs;
2063 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2064 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2066 I DONT KNOW WHAT TO DO
2067 #endif /* !HAVE_PR_SYSCALL */
2070 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2071 sysent[tcp->scno].nargs > tcp->status.val)
2072 tcp->u_nargs = sysent[tcp->scno].nargs;
2074 tcp->u_nargs = tcp->status.val;
2075 if (tcp->u_nargs < 0)
2077 if (tcp->u_nargs > MAX_ARGS)
2078 tcp->u_nargs = MAX_ARGS;
2079 switch(regs.r_eax) {
2081 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2082 regs.r_esp + sizeof(int) + sizeof(quad_t));
2085 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2086 regs.r_esp + 2 * sizeof(int));
2089 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2090 regs.r_esp + sizeof(int));
2093 #endif /* FREEBSD */
2105 /* Measure the exit time as early as possible to avoid errors. */
2106 if (dtime && (tcp->flags & TCB_INSYSCALL))
2107 gettimeofday(&tv, NULL);
2109 res = get_scno(tcp);
2113 res = syscall_fixup(tcp);
2117 if (tcp->flags & TCB_INSYSCALL) {
2119 res = get_error(tcp);
2123 internal_syscall(tcp);
2124 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2125 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2126 tcp->flags &= ~TCB_INSYSCALL;
2130 if (tcp->flags & TCB_REPRINT) {
2133 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2134 tprintf("syscall_%lu", tcp->scno);
2136 tprintf("%s", sysent[tcp->scno].sys_name);
2137 tprintf(" resumed> ");
2140 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
2141 call_count[tcp->scno]++;
2143 error_count[tcp->scno]++;
2144 tv_sub(&tv, &tv, &tcp->etime);
2146 if (tv_cmp(&tv, &tcp->dtime) > 0) {
2147 static struct timeval one_tick;
2148 if (one_tick.tv_usec == 0) {
2149 /* Initialize it. */
2150 struct itimerval it;
2151 memset(&it, 0, sizeof it);
2152 it.it_interval.tv_usec = 1;
2153 setitimer(ITIMER_REAL, &it, NULL);
2154 getitimer(ITIMER_REAL, &it);
2155 one_tick = it.it_interval;
2158 if (tv_nz(&tcp->dtime))
2160 else if (tv_cmp(&tv, &one_tick) > 0) {
2161 if (tv_cmp(&shortest, &one_tick) < 0)
2168 if (tv_cmp(&tv, &shortest) < 0)
2170 tv_add(&tv_count[tcp->scno],
2171 &tv_count[tcp->scno], &tv);
2172 tcp->flags &= ~TCB_INSYSCALL;
2176 if (tcp->scno >= nsyscalls || tcp->scno < 0
2177 || (qual_flags[tcp->scno] & QUAL_RAW))
2178 sys_res = printargs(tcp);
2180 if (not_failing_only && tcp->u_error)
2181 return 0; /* ignore failed syscalls */
2182 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2184 u_error = tcp->u_error;
2187 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2188 qual_flags[tcp->scno] & QUAL_RAW) {
2190 tprintf("= -1 (errno %ld)", u_error);
2192 tprintf("= %#lx", tcp->u_rval);
2194 else if (!(sys_res & RVAL_NONE) && u_error) {
2198 tprintf("= ? ERESTARTSYS (To be restarted)");
2200 case ERESTARTNOINTR:
2201 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2203 case ERESTARTNOHAND:
2204 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2206 case ERESTART_RESTARTBLOCK:
2207 tprintf("= ? ERESTART_RESTARTBLOCK (To be restarted)");
2213 tprintf("E??? (errno %ld)", u_error);
2214 else if (u_error < nerrnos)
2215 tprintf("%s (%s)", errnoent[u_error],
2218 tprintf("ERRNO_%ld (%s)", u_error,
2224 if (sys_res & RVAL_NONE)
2227 switch (sys_res & RVAL_MASK) {
2229 tprintf("= %#lx", tcp->u_rval);
2232 tprintf("= %#lo", tcp->u_rval);
2235 tprintf("= %lu", tcp->u_rval);
2238 tprintf("= %ld", tcp->u_rval);
2240 #ifdef HAVE_LONG_LONG
2242 tprintf("= %#llx", tcp->u_lrval);
2245 tprintf("= %#llo", tcp->u_lrval);
2247 case RVAL_LUDECIMAL:
2248 tprintf("= %llu", tcp->u_lrval);
2251 tprintf("= %lld", tcp->u_lrval);
2256 "invalid rval format\n");
2260 if ((sys_res & RVAL_STR) && tcp->auxstr)
2261 tprintf(" (%s)", tcp->auxstr);
2264 tv_sub(&tv, &tv, &tcp->etime);
2265 tprintf(" <%ld.%06ld>",
2266 (long) tv.tv_sec, (long) tv.tv_usec);
2271 if (fflush(tcp->outf) == EOF)
2273 tcp->flags &= ~TCB_INSYSCALL;
2277 /* Entering system call */
2278 res = syscall_enter(tcp);
2282 switch (tcp->scno + NR_SYSCALL_BASE) {
2284 #if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
2285 case SYS_socketcall:
2286 decode_subcall(tcp, SYS_socket_subcall,
2287 SYS_socket_nsubcalls, deref_style);
2290 decode_subcall(tcp, SYS_ipc_subcall,
2291 SYS_ipc_nsubcalls, shift_style);
2293 #endif /* !ALPHA && !MIPS && !SPARC && !HPPA && !X86_64 */
2295 case SYS_socketcall:
2296 sparc_socket_decode (tcp);
2301 #ifdef SYS_pgrpsys_subcall
2303 decode_subcall(tcp, SYS_pgrpsys_subcall,
2304 SYS_pgrpsys_nsubcalls, shift_style);
2306 #endif /* SYS_pgrpsys_subcall */
2307 #ifdef SYS_sigcall_subcall
2309 decode_subcall(tcp, SYS_sigcall_subcall,
2310 SYS_sigcall_nsubcalls, mask_style);
2312 #endif /* SYS_sigcall_subcall */
2314 decode_subcall(tcp, SYS_msgsys_subcall,
2315 SYS_msgsys_nsubcalls, shift_style);
2318 decode_subcall(tcp, SYS_shmsys_subcall,
2319 SYS_shmsys_nsubcalls, shift_style);
2322 decode_subcall(tcp, SYS_semsys_subcall,
2323 SYS_semsys_nsubcalls, shift_style);
2327 decode_subcall(tcp, SYS_utssys_subcall,
2328 SYS_utssys_nsubcalls, shift_style);
2332 decode_subcall(tcp, SYS_sysfs_subcall,
2333 SYS_sysfs_nsubcalls, shift_style);
2336 decode_subcall(tcp, SYS_spcall_subcall,
2337 SYS_spcall_nsubcalls, shift_style);
2339 #ifdef SYS_context_subcall
2341 decode_subcall(tcp, SYS_context_subcall,
2342 SYS_context_nsubcalls, shift_style);
2344 #endif /* SYS_context_subcall */
2345 #ifdef SYS_door_subcall
2347 decode_subcall(tcp, SYS_door_subcall,
2348 SYS_door_nsubcalls, door_style);
2350 #endif /* SYS_door_subcall */
2351 #ifdef SYS_kaio_subcall
2353 decode_subcall(tcp, SYS_kaio_subcall,
2354 SYS_kaio_nsubcalls, shift_style);
2362 decode_subcall(tcp, 0, 0, table_style);
2367 decode_subcall(tcp, SYS_semsys_subcall,
2368 SYS_semsys_nsubcalls, shift_style);
2371 decode_subcall(tcp, SYS_msgsys_subcall,
2372 SYS_msgsys_nsubcalls, shift_style);
2375 decode_subcall(tcp, SYS_shmsys_subcall,
2376 SYS_shmsys_nsubcalls, shift_style);
2381 internal_syscall(tcp);
2382 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2383 tcp->flags |= TCB_INSYSCALL;
2388 gettimeofday(&tcp->etime, NULL);
2389 tcp->flags |= TCB_INSYSCALL;
2394 tcp->flags &= ~TCB_REPRINT;
2396 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2397 tprintf("syscall_%lu(", tcp->scno);
2399 tprintf("%s(", sysent[tcp->scno].sys_name);
2400 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2401 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2402 sys_res = printargs(tcp);
2404 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2405 if (fflush(tcp->outf) == EOF)
2407 tcp->flags |= TCB_INSYSCALL;
2408 /* Measure the entrance time as late as possible to avoid errors. */
2410 gettimeofday(&tcp->etime, NULL);
2418 if (entering(tcp)) {
2421 for (i = 0; i < tcp->u_nargs; i++)
2422 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2436 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
2440 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2444 if (upeek(tcp->pid, PT_R9, &val) < 0)
2449 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2455 val = tcp->status.PR_REG[R_O1];
2458 val = tcp->status.PR_REG[EDX];
2461 val = tcp->status.PR_REG[RDX];
2464 val = tcp->status.PR_REG[CTX_V1];
2469 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
2476 * Apparently, indirect system calls have already be converted by ptrace(2),
2477 * so if you see "indir" this program has gone astray.
2485 if (entering(tcp)) {
2486 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2487 fprintf(stderr, "Bogus syscall: %u\n", scno);
2490 nargs = sysent[scno].nargs;
2491 tprintf("%s", sysent[scno].sys_name);
2492 for (i = 0; i < nargs; i++)
2493 tprintf(", %#lx", tcp->u_arg[i+1]);
2503 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2511 return strcmp(sysent[*((int *) a)].sys_name,
2512 sysent[*((int *) b)].sys_name);
2520 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2522 return (m < n) ? 1 : (m > n) ? -1 : 0;
2525 static int (*sortfun)();
2526 static struct timeval overhead = { -1, -1 };
2532 if (strcmp(sortby, "time") == 0)
2534 else if (strcmp(sortby, "calls") == 0)
2535 sortfun = count_cmp;
2536 else if (strcmp(sortby, "name") == 0)
2537 sortfun = syscall_cmp;
2538 else if (strcmp(sortby, "nothing") == 0)
2541 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2546 void set_overhead(n)
2549 overhead.tv_sec = n / 1000000;
2550 overhead.tv_usec = n % 1000000;
2558 int call_cum, error_cum;
2559 struct timeval tv_cum, dtv;
2561 char *dashes = "-------------------------";
2564 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2565 if (overhead.tv_sec == -1) {
2566 tv_mul(&overhead, &shortest, 8);
2567 tv_div(&overhead, &overhead, 10);
2569 for (i = 0; i < nsyscalls; i++) {
2570 sorted_count[i] = i;
2571 if (call_count[i] == 0)
2573 tv_mul(&dtv, &overhead, call_count[i]);
2574 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2575 call_cum += call_count[i];
2576 error_cum += error_count[i];
2577 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2580 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2581 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2582 "% time", "seconds", "usecs/call",
2583 "calls", "errors", "syscall");
2584 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2585 dashes, dashes, dashes, dashes, dashes, dashes);
2586 for (i = 0; i < nsyscalls; i++) {
2587 j = sorted_count[i];
2588 if (call_count[j] == 0)
2590 tv_div(&dtv, &tv_count[j], call_count[j]);
2592 sprintf(error_str, "%d", error_count[j]);
2594 error_str[0] = '\0';
2595 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2596 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2597 percent, (long) tv_count[j].tv_sec,
2598 (long) tv_count[j].tv_usec,
2599 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2600 call_count[j], error_str, sysent[j].sys_name);
2602 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2603 dashes, dashes, dashes, dashes, dashes, dashes);
2605 sprintf(error_str, "%d", error_cum);
2607 error_str[0] = '\0';
2608 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2609 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2610 call_cum, error_str, "total");