2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/syscall.h>
43 #include <sys/param.h>
46 #if defined (SPARC) || defined (SPARC64)
47 # define fpq kernel_fpq
49 # define fpu kernel_fpu
52 #if defined (SPARC) || defined (SPARC64)
61 #ifndef PTRACE_PEEKUSR
62 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
64 #elif defined(HAVE_LINUX_PTRACE_H)
66 # ifdef HAVE_STRUCT_IA64_FPREG
67 # define ia64_fpreg XXX_ia64_fpreg
69 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
70 # define pt_all_user_regs XXX_pt_all_user_regs
72 #include <linux/ptrace.h>
74 # undef pt_all_user_regs
77 #if defined (LINUX) && defined (SPARC64)
79 # undef PTRACE_GETREGS
80 # define PTRACE_GETREGS PTRACE_GETREGS64
81 # undef PTRACE_SETREGS
82 # define PTRACE_SETREGS PTRACE_SETREGS64
83 #endif /* LINUX && SPARC64 */
85 #if defined(LINUX) && defined(IA64)
86 # include <asm/ptrace_offsets.h>
90 #define NR_SYSCALL_BASE 0
93 #define ERESTARTSYS 512
95 #ifndef ERESTARTNOINTR
96 #define ERESTARTNOINTR 513
98 #ifndef ERESTARTNOHAND
99 #define ERESTARTNOHAND 514 /* restart if no handler.. */
102 #define ENOIOCTLCMD 515 /* No ioctl command */
104 #ifndef ERESTART_RESTARTBLOCK
105 #define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
113 #undef NR_SYSCALL_BASE
114 #define NR_SYSCALL_BASE __NR_SYSCALL_BASE
120 /* Define these shorthand notations to simplify the syscallent files. */
121 #define TF TRACE_FILE
123 #define TN TRACE_NETWORK
124 #define TP TRACE_PROCESS
125 #define TS TRACE_SIGNAL
127 struct sysent sysent0[] = {
128 #include "syscallent.h"
130 int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
132 #if SUPPORTED_PERSONALITIES >= 2
133 struct sysent sysent1[] = {
134 #include "syscallent1.h"
136 int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
137 #endif /* SUPPORTED_PERSONALITIES >= 2 */
139 #if SUPPORTED_PERSONALITIES >= 3
140 struct sysent sysent2[] = {
141 #include "syscallent2.h"
143 int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
144 #endif /* SUPPORTED_PERSONALITIES >= 3 */
146 struct sysent *sysent;
149 /* Now undef them since short defines cause wicked namespace pollution. */
156 char *errnoent0[] = {
157 #include "errnoent.h"
159 int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
161 #if SUPPORTED_PERSONALITIES >= 2
162 char *errnoent1[] = {
163 #include "errnoent1.h"
165 int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
166 #endif /* SUPPORTED_PERSONALITIES >= 2 */
168 #if SUPPORTED_PERSONALITIES >= 3
169 char *errnoent2[] = {
170 #include "errnoent2.h"
172 int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
173 #endif /* SUPPORTED_PERSONALITIES >= 3 */
178 int current_personality;
181 set_personality(personality)
184 switch (personality) {
186 errnoent = errnoent0;
189 nsyscalls = nsyscalls0;
190 ioctlent = ioctlent0;
191 nioctlents = nioctlents0;
192 signalent = signalent0;
193 nsignals = nsignals0;
196 #if SUPPORTED_PERSONALITIES >= 2
198 errnoent = errnoent1;
201 nsyscalls = nsyscalls1;
202 ioctlent = ioctlent1;
203 nioctlents = nioctlents1;
204 signalent = signalent1;
205 nsignals = nsignals1;
207 #endif /* SUPPORTED_PERSONALITIES >= 2 */
209 #if SUPPORTED_PERSONALITIES >= 3
211 errnoent = errnoent2;
214 nsyscalls = nsyscalls2;
215 ioctlent = ioctlent2;
216 nioctlents = nioctlents2;
217 signalent = signalent2;
218 nsignals = nsignals2;
220 #endif /* SUPPORTED_PERSONALITIES >= 3 */
226 current_personality = personality;
230 int qual_flags[MAX_QUALS];
232 static int call_count[MAX_QUALS];
233 static int error_count[MAX_QUALS];
234 static struct timeval tv_count[MAX_QUALS];
235 static int sorted_count[MAX_QUALS];
237 static struct timeval shortest = { 1000000, 0 };
239 static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
241 static struct qual_options {
247 { QUAL_TRACE, "trace", qual_syscall, "system call" },
248 { QUAL_TRACE, "t", qual_syscall, "system call" },
249 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
250 { QUAL_ABBREV, "a", qual_syscall, "system call" },
251 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
252 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
253 { QUAL_RAW, "raw", qual_syscall, "system call" },
254 { QUAL_RAW, "x", qual_syscall, "system call" },
255 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
256 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
257 { QUAL_SIGNAL, "s", qual_signal, "signal" },
258 { QUAL_FAULT, "fault", qual_fault, "fault" },
259 { QUAL_FAULT, "faults", qual_fault, "fault" },
260 { QUAL_FAULT, "m", qual_fault, "fault" },
261 { QUAL_READ, "read", qual_desc, "descriptor" },
262 { QUAL_READ, "reads", qual_desc, "descriptor" },
263 { QUAL_READ, "r", qual_desc, "descriptor" },
264 { QUAL_WRITE, "write", qual_desc, "descriptor" },
265 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
266 { QUAL_WRITE, "w", qual_desc, "descriptor" },
267 { 0, NULL, NULL, NULL },
271 qualify_one(n, opt, not)
273 struct qual_options *opt;
277 qual_flags[n] &= ~opt->bitflag;
279 qual_flags[n] |= opt->bitflag;
283 qual_syscall(s, opt, not)
285 struct qual_options *opt;
291 for (i = 0; i < nsyscalls; i++) {
292 if (strcmp(s, sysent[i].sys_name) == 0) {
293 qualify_one(i, opt, not);
301 qual_signal(s, opt, not)
303 struct qual_options *opt;
309 if (s && *s && isdigit((unsigned char)*s)) {
310 qualify_one(atoi(s), opt, not);
313 if (strlen(s) >= sizeof buf)
317 for (i = 0; s[i]; i++)
318 s[i] = toupper((unsigned char)(s[i]));
319 if (strncmp(s, "SIG", 3) == 0)
321 for (i = 0; i <= NSIG; i++)
322 if (strcmp(s, signame(i) + 3) == 0) {
323 qualify_one(atoi(s), opt, not);
330 qual_fault(s, opt, not)
332 struct qual_options *opt;
339 qual_desc(s, opt, not)
341 struct qual_options *opt;
344 if (s && *s && isdigit((unsigned char)*s)) {
345 qualify_one(atoi(s), opt, not);
355 if (strcmp(s, "file") == 0)
357 if (strcmp(s, "ipc") == 0)
359 if (strcmp(s, "network") == 0)
360 return TRACE_NETWORK;
361 if (strcmp(s, "process") == 0)
362 return TRACE_PROCESS;
363 if (strcmp(s, "signal") == 0)
372 struct qual_options *opt;
377 opt = &qual_options[0];
378 for (i = 0; (p = qual_options[i].option_name); i++) {
380 if (strncmp(s, p, n) == 0 && s[n] == '=') {
381 opt = &qual_options[i];
391 if (strcmp(s, "none") == 0) {
395 if (strcmp(s, "all") == 0) {
396 for (i = 0; i < MAX_QUALS; i++) {
398 qual_flags[i] &= ~opt->bitflag;
400 qual_flags[i] |= opt->bitflag;
404 for (i = 0; i < MAX_QUALS; i++) {
406 qual_flags[i] |= opt->bitflag;
408 qual_flags[i] &= ~opt->bitflag;
410 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
411 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
412 for (i = 0; i < MAX_QUALS; i++) {
413 if (sysent[i].sys_flags & n) {
415 qual_flags[i] &= ~opt->bitflag;
417 qual_flags[i] |= opt->bitflag;
422 if (opt->qualify(p, opt, not)) {
423 fprintf(stderr, "strace: invalid %s `%s'\n",
424 opt->argument_name, p);
437 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
439 switch (tcp->scno + NR_SYSCALL_BASE) {
447 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
448 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
457 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
458 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
462 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
463 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
469 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
470 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
477 enum subcall_style { shift_style, deref_style, mask_style, door_style };
479 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
487 const struct subcall subcalls_table[] = {
488 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
490 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
492 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
494 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
498 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
500 const int socket_map [] = {
503 /* SYS_CONNECT */ 98,
504 /* SYS_LISTEN */ 106,
506 /* SYS_GETSOCKNAME */ 150,
507 /* SYS_GETPEERNAME */ 141,
508 /* SYS_SOCKETPAIR */ 135,
511 /* SYS_SENDTO */ 133,
512 /* SYS_RECVFROM */ 125,
513 /* SYS_SHUTDOWN */ 134,
514 /* SYS_SETSOCKOPT */ 105,
515 /* SYS_GETSOCKOPT */ 118,
516 /* SYS_SENDMSG */ 114,
517 /* SYS_RECVMSG */ 113
521 sparc_socket_decode (tcp)
527 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
530 tcp->scno = socket_map [tcp->u_arg [0]-1];
531 n = tcp->u_nargs = sysent [tcp->scno].nargs;
532 addr = tcp->u_arg [1];
533 for (i = 0; i < n; i++){
535 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
537 tcp->u_arg [i] = arg;
538 addr += sizeof (arg);
543 decode_subcall(tcp, subcall, nsubcalls, style)
547 enum subcall_style style;
549 long addr, mask, arg;
554 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
556 tcp->scno = subcall + tcp->u_arg[0];
557 if (sysent[tcp->scno].nargs != -1)
558 tcp->u_nargs = sysent[tcp->scno].nargs;
561 for (i = 0; i < tcp->u_nargs; i++)
562 tcp->u_arg[i] = tcp->u_arg[i + 1];
565 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
567 tcp->scno = subcall + tcp->u_arg[0];
568 addr = tcp->u_arg[1];
569 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
570 if (umove(tcp, addr, &arg) < 0)
575 tcp->u_nargs = sysent[tcp->scno].nargs;
578 mask = (tcp->u_arg[0] >> 8) & 0xff;
579 for (i = 0; mask; i++)
583 tcp->u_arg[0] &= 0xff;
584 tcp->scno = subcall + i;
585 if (sysent[tcp->scno].nargs != -1)
586 tcp->u_nargs = sysent[tcp->scno].nargs;
590 * Oh, yuck. The call code is the *sixth* argument.
591 * (don't you mean the *last* argument? - JH)
593 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
595 tcp->scno = subcall + tcp->u_arg[5];
596 if (sysent[tcp->scno].nargs != -1)
597 tcp->u_nargs = sysent[tcp->scno].nargs;
603 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
604 if (subcalls_table[i].call == tcp->scno) break;
605 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
606 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
607 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
608 for (i = 0; i < tcp->u_nargs; i++)
609 tcp->u_arg[i] = tcp->u_arg[i + 1];
617 struct tcb *tcp_last = NULL;
620 internal_syscall(tcp)
624 * We must always trace a few critical system calls in order to
625 * correctly support following forks in the presence of tracing
628 switch (tcp->scno + NR_SYSCALL_BASE) {
698 #ifdef __NR_exit_group
699 case __NR_exit_group:
702 case 252: /* IA-32 __NR_exit_group */
717 #elif defined (POWERPC)
718 static long result,flags;
722 static struct pt_regs regs;
723 #elif defined (ALPHA)
726 #elif defined (SPARC) || defined (SPARC64)
727 static struct regs regs;
728 static unsigned long trap;
732 #elif defined(S390) || defined(S390X)
735 static long syscall_mode;
742 #elif defined(X86_64)
760 #if defined(S390) || defined(S390X)
761 if (tcp->flags & TCB_WAITEXECVE) {
763 * When the execve system call completes successfully, the
764 * new process still has -ENOSYS (old style) or __NR_execve
765 * (new style) in gpr2. We cannot recover the scno again
766 * by disassembly, because the image that executed the
767 * syscall is gone now. Fortunately, we don't want it. We
768 * leave the flag set so that syscall_fixup can fake the
771 if (tcp->flags & TCB_INSYSCALL)
774 * This is the SIGTRAP after execve. We cannot try to read
775 * the system call here either.
777 tcp->flags &= ~TCB_WAITEXECVE;
781 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
784 if (syscall_mode != -ENOSYS) {
786 * Since kernel version 2.5.44 the scno gets passed in gpr2.
791 * Old style of "passing" the scno via the SVC instruction.
794 long opcode, offset_reg, tmp;
796 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
797 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
798 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
799 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
801 if (upeek(pid, PT_PSWADDR, &pc) < 0)
804 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
806 perror("peektext(pc-oneword)");
811 * We have to check if the SVC got executed directly or via an
812 * EXECUTE instruction. In case of EXECUTE it is necessary to do
813 * instruction decoding to derive the system call number.
814 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
815 * so that this doesn't work if a SVC opcode is part of an EXECUTE
816 * opcode. Since there is no way to find out the opcode size this
817 * is the best we can do...
820 if ((opcode & 0xff00) == 0x0a00) {
822 scno = opcode & 0xff;
825 /* SVC got executed by EXECUTE instruction */
828 * Do instruction decoding of EXECUTE. If you really want to
829 * understand this, read the Principles of Operations.
831 svc_addr = (void *) (opcode & 0xfff);
834 offset_reg = (opcode & 0x000f0000) >> 16;
835 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
840 offset_reg = (opcode & 0x0000f000) >> 12;
841 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
845 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
854 offset_reg = (opcode & 0x00f00000) >> 20;
855 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
858 scno = (scno | tmp) & 0xff;
861 #elif defined (POWERPC)
862 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
864 if (!(tcp->flags & TCB_INSYSCALL)) {
865 /* Check if we return from execve. */
866 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
867 tcp->flags &= ~TCB_WAITEXECVE;
872 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
874 #elif defined (X86_64)
875 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
878 if (!(tcp->flags & TCB_INSYSCALL)) {
879 static int currpers=-1;
882 /* Check CS register value. On x86-64 linux it is:
883 * 0x33 for long mode (64 bit)
884 * 0x23 for compatibility mode (32 bit)
885 * It takes only one ptrace and thus doesn't need
888 if (upeek(pid, 8*CS, &val) < 0)
892 case 0x23: currpers = 1; break;
893 case 0x33: currpers = 0; break;
895 fprintf(stderr, "Unknown value CS=0x%02X while "
896 "detecting personality of process "
897 "PID=%d\n", (int)val, pid);
898 currpers = current_personality;
902 /* This version analyzes the opcode of a syscall instruction.
903 * (int 0x80 on i386 vs. syscall on x86-64)
904 * It works, but is too complicated.
906 unsigned long val, rip, i;
908 if(upeek(pid, 8*RIP, &rip)<0)
909 perror("upeek(RIP)");
911 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
915 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
917 printf("ptrace_peektext failed: %s\n",
919 switch (call & 0xffff)
921 /* x86-64: syscall = 0x0f 0x05 */
922 case 0x050f: currpers = 0; break;
923 /* i386: int 0x80 = 0xcd 0x80 */
924 case 0x80cd: currpers = 1; break;
926 currpers = current_personality;
928 "Unknown syscall opcode (0x%04X) while "
929 "detecting personality of process "
930 "PID=%d\n", (int)call, pid);
934 if(currpers != current_personality)
936 char *names[]={"64 bit", "32 bit"};
937 set_personality(currpers);
938 printf("[ Process PID=%d runs in %s mode. ]\n",
939 pid, names[current_personality]);
943 # define IA64_PSR_IS ((long)1 << 34)
944 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
945 ia32 = (psr & IA64_PSR_IS) != 0;
946 if (!(tcp->flags & TCB_INSYSCALL)) {
948 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
951 if (upeek (pid, PT_R15, &scno) < 0)
954 /* Check if we return from execve. */
955 if (tcp->flags & TCB_WAITEXECVE) {
956 tcp->flags &= ~TCB_WAITEXECVE;
960 /* syscall in progress */
961 if (upeek (pid, PT_R8, &r8) < 0)
963 if (upeek (pid, PT_R10, &r10) < 0)
968 * Read complete register set in one go.
970 if (ptrace(PTRACE_GETREGS, pid, NULL, (void *)®s) == -1)
974 * We only need to grab the syscall number on syscall entry.
976 if (regs.ARM_ip == 0) {
978 * Note: we only deal with only 32-bit CPUs here.
980 if (regs.ARM_cpsr & 0x20) {
982 * Get the Thumb-mode system call number
987 * Get the ARM-mode system call number
990 scno = ptrace(PTRACE_PEEKTEXT, pid, (void *)(regs.ARM_pc - 4), NULL);
994 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
995 tcp->flags &= ~TCB_WAITEXECVE;
999 if ((scno & 0x0ff00000) != 0x0f900000) {
1000 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
1006 * Fixup the syscall number
1011 if (tcp->flags & TCB_INSYSCALL) {
1012 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
1013 tcp->flags &= ~TCB_INSYSCALL;
1016 if (!(tcp->flags & TCB_INSYSCALL)) {
1017 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1018 tcp->flags |= TCB_INSYSCALL;
1021 #elif defined (M68K)
1022 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
1024 #elif defined (MIPS)
1025 if (upeek(pid, REG_A3, &a3) < 0)
1028 if(!(tcp->flags & TCB_INSYSCALL)) {
1029 if (upeek(pid, REG_V0, &scno) < 0)
1032 if (scno < 0 || scno > nsyscalls) {
1033 if(a3 == 0 || a3 == -1) {
1035 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
1040 if (upeek(pid, REG_V0, &r2) < 0)
1043 #elif defined (ALPHA)
1044 if (upeek(pid, REG_A3, &a3) < 0)
1047 if (!(tcp->flags & TCB_INSYSCALL)) {
1048 if (upeek(pid, REG_R0, &scno) < 0)
1051 /* Check if we return from execve. */
1052 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1053 tcp->flags &= ~TCB_WAITEXECVE;
1058 * Do some sanity checks to figure out if it's
1059 * really a syscall entry
1061 if (scno < 0 || scno > nsyscalls) {
1062 if (a3 == 0 || a3 == -1) {
1064 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
1070 if (upeek(pid, REG_R0, &r0) < 0)
1073 #elif defined (SPARC) || defined (SPARC64)
1074 /* Everything we need is in the current register set. */
1075 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
1078 /* If we are entering, then disassemble the syscall trap. */
1079 if (!(tcp->flags & TCB_INSYSCALL)) {
1080 /* Retrieve the syscall trap instruction. */
1082 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
1083 #if defined(SPARC64)
1089 /* Disassemble the trap to see what personality to use. */
1092 /* Linux/SPARC syscall trap. */
1096 /* Linux/SPARC64 syscall trap. */
1100 /* SunOS syscall trap. (pers 1) */
1101 fprintf(stderr,"syscall: SunOS no support\n");
1104 /* Solaris 2.x syscall trap. (per 2) */
1108 /* NetBSD/FreeBSD syscall trap. */
1109 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1112 /* Solaris 2.x gettimeofday */
1116 /* Unknown syscall trap. */
1117 if(tcp->flags & TCB_WAITEXECVE) {
1118 tcp->flags &= ~TCB_WAITEXECVE;
1121 #if defined (SPARC64)
1122 fprintf(stderr,"syscall: unknown syscall trap %08lx %016lx\n", trap, regs.r_tpc);
1124 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
1129 /* Extract the system call number from the registers. */
1130 if (trap == 0x91d02027)
1136 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
1140 if (upeek(pid, PT_GR20, &scno) < 0)
1142 if (!(tcp->flags & TCB_INSYSCALL)) {
1143 /* Check if we return from execve. */
1144 if ((tcp->flags & TCB_WAITEXECVE)) {
1145 tcp->flags &= ~TCB_WAITEXECVE;
1151 * In the new syscall ABI, the system call number is in R3.
1153 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1157 /* Odd as it may seem, a glibc bug has been known to cause
1158 glibc to issue bogus negative syscall numbers. So for
1159 our purposes, make strace print what it *should* have been */
1160 long correct_scno = (scno & 0xff);
1163 "Detected glibc bug: bogus system call number = %ld, "
1164 "correcting to %ld\n",
1167 scno = correct_scno;
1171 if (!(tcp->flags & TCB_INSYSCALL)) {
1172 /* Check if we return from execve. */
1173 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1174 tcp->flags &= ~TCB_WAITEXECVE;
1179 if (upeek(pid, REG_SYSCALL, &scno) < 0)
1183 if (!(tcp->flags & TCB_INSYSCALL)) {
1184 /* Check if we return from execve. */
1185 if (tcp->flags & TCB_WAITEXECVE) {
1186 tcp->flags &= ~TCB_WAITEXECVE;
1193 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1196 /* new syscall ABI returns result in R0 */
1197 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1200 /* ABI defines result returned in r9 */
1201 if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
1206 #ifdef HAVE_PR_SYSCALL
1207 scno = tcp->status.PR_SYSCALL;
1208 #else /* !HAVE_PR_SYSCALL */
1210 scno = tcp->status.PR_WHAT;
1212 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1216 switch (regs.r_eax) {
1219 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1225 #endif /* FREEBSD */
1226 #endif /* !HAVE_PR_SYSCALL */
1227 #endif /* USE_PROCFS */
1228 if (!(tcp->flags & TCB_INSYSCALL))
1240 #else /* USE_PROCFS */
1241 int scno = tcp->scno;
1243 if (!(tcp->flags & TCB_INSYSCALL)) {
1244 if (tcp->status.PR_WHY != PR_SYSENTRY) {
1248 || scno == SYS_vfork
1249 #endif /* SYS_vfork */
1251 || scno == SYS_fork1
1252 #endif /* SYS_fork1 */
1254 || scno == SYS_forkall
1255 #endif /* SYS_forkall */
1257 || scno == SYS_rfork1
1258 #endif /* SYS_fork1 */
1260 || scno == SYS_rforkall
1261 #endif /* SYS_rforkall */
1263 /* We are returning in the child, fake it. */
1264 tcp->status.PR_WHY = PR_SYSENTRY;
1266 tcp->status.PR_WHY = PR_SYSEXIT;
1269 fprintf(stderr, "syscall: missing entry\n");
1270 tcp->flags |= TCB_INSYSCALL;
1275 if (tcp->status.PR_WHY != PR_SYSEXIT) {
1276 fprintf(stderr, "syscall: missing exit\n");
1277 tcp->flags &= ~TCB_INSYSCALL;
1280 #endif /* USE_PROCFS */
1282 if (!(tcp->flags & TCB_INSYSCALL)) {
1284 fprintf(stderr, "syscall: missing entry\n");
1285 tcp->flags |= TCB_INSYSCALL;
1292 * This happens when a signal handler
1293 * for a signal which interrupted a
1294 * a system call makes another system call.
1296 fprintf(stderr, "syscall: missing exit\n");
1298 tcp->flags &= ~TCB_INSYSCALL;
1304 if (upeek(pid, 4*EAX, &eax) < 0)
1306 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1308 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1311 #elif defined (X86_64)
1312 if (upeek(pid, 8*RAX, &rax) < 0)
1314 if (current_personality == 1)
1315 rax = (long int)(int)rax; /* sign extend from 32 bits */
1316 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1318 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1321 #elif defined (S390) || defined (S390X)
1322 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1324 if (syscall_mode != -ENOSYS)
1325 syscall_mode = tcp->scno;
1326 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
1328 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1331 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1332 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1333 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1335 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1336 * flag set for the post-execve SIGTRAP to see and reset.
1340 #elif defined (POWERPC)
1341 # define SO_MASK 0x10000000
1342 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1344 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
1346 if (flags & SO_MASK)
1348 #elif defined (M68K)
1349 if (upeek(pid, 4*PT_D0, &d0) < 0)
1351 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1353 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1360 #elif defined (HPPA)
1361 if (upeek(pid, PT_GR28, &r28) < 0)
1364 if (upeek(pid, PT_R10, &r10) < 0)
1366 if (upeek(pid, PT_R8, &r8) < 0)
1368 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1370 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1384 #if defined(S390) || defined(S390X)
1385 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1393 #else /* !S390 && !S390X */
1395 if (eax < 0 && -eax < nerrnos) {
1405 if (rax < 0 && -rax < nerrnos) {
1419 if (err < 0 && -err < nerrnos) {
1447 if (result && (unsigned long) -result < nerrnos) {
1452 tcp->u_rval = result;
1455 #else /* !POWERPC */
1457 if (d0 && (unsigned) -d0 < nerrnos) {
1467 if (regs.ARM_r0 && (unsigned) -regs.ARM_r0 < nerrnos) {
1469 u_error = -regs.ARM_r0;
1472 tcp->u_rval = regs.ARM_r0;
1487 if (regs.r_psr & PSR_C) {
1489 u_error = regs.r_o0;
1492 tcp->u_rval = regs.r_o0;
1497 if (regs.r_tstate & 0x1100000000UL) {
1499 u_error = regs.r_o0;
1502 tcp->u_rval = regs.r_o0;
1505 #else /* !SPARC64 */
1507 if (r28 && (unsigned) -r28 < nerrnos) {
1517 /* interpret R0 as return value or error number */
1518 if (r0 && (unsigned) -r0 < nerrnos) {
1528 /* interpret result as return value or error number */
1529 if (r9 && (unsigned) -r9 < nerrnos) {
1541 #endif /* SPARC64 */
1545 #endif /* POWERPC */
1550 #endif /* S390 || S390X */
1553 /* get error code from user struct */
1554 if (upeek(pid, uoff(u_error), &u_error) < 0)
1556 u_error >>= 24; /* u_error is a char */
1558 /* get system call return value */
1559 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1564 /* Judicious guessing goes a long way. */
1565 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1567 u_error = tcp->status.pr_reg[R_O0];
1570 tcp->u_rval = tcp->status.pr_reg[R_O0];
1575 /* Wanna know how to kill an hour single-stepping? */
1576 if (tcp->status.PR_REG[EFL] & 0x1) {
1578 u_error = tcp->status.PR_REG[EAX];
1581 tcp->u_rval = tcp->status.PR_REG[EAX];
1582 #ifdef HAVE_LONG_LONG
1584 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1585 tcp->status.PR_REG[EAX];
1591 /* Wanna know how to kill an hour single-stepping? */
1592 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1594 u_error = tcp->status.PR_REG[RAX];
1597 tcp->u_rval = tcp->status.PR_REG[RAX];
1602 if (tcp->status.pr_reg[CTX_A3]) {
1604 u_error = tcp->status.pr_reg[CTX_V0];
1607 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1613 if (regs.r_eflags & PSL_C) {
1615 u_error = regs.r_eax;
1617 tcp->u_rval = regs.r_eax;
1619 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1622 #endif /* FREEBSD */
1623 tcp->u_error = u_error;
1628 force_result(tcp, error, rval)
1634 #if defined(S390) || defined(S390X)
1635 gpr2 = error ? -error : rval;
1636 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1638 #else /* !S390 && !S390X */
1640 eax = error ? -error : rval;
1641 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1645 rax = error ? -error : rval;
1646 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 8), rax) < 0)
1651 r8 = error ? -error : rval;
1652 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1664 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1665 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1678 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1679 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1683 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1693 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1694 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
1696 #else /* !POWERPC */
1698 d0 = error ? -error : rval;
1699 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1703 regs.ARM_r0 = error ? -error : rval;
1704 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), regs.ARM_r0) < 0)
1716 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1717 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1721 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1724 regs.r_psr |= PSR_C;
1728 regs.r_psr &= ~PSR_C;
1731 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1735 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1738 regs.r_tstate |= 0x1100000000UL;
1742 regs.r_tstate &= ~0x1100000000UL;
1745 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1747 #else /* !SPARC64 */
1749 r28 = error ? -error : rval;
1750 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1754 r0 = error ? -error : rval;
1755 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1759 r9 = error ? -error : rval;
1760 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1766 #endif /* SPARC64 */
1770 #endif /* POWERPC */
1775 #endif /* S390 || S390X */
1778 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1780 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1788 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1793 regs.r_eflags |= PSL_C;
1797 regs.r_eflags &= ~PSL_C;
1800 if (pwrite(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1804 #endif /* FREEBSD */
1806 /* All branches reach here on success (only). */
1807 tcp->u_error = error;
1812 int syscall_enter(tcp)
1817 #endif /* !USE_PROCFS */
1819 #if defined(S390) || defined(S390X)
1822 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1823 tcp->u_nargs = sysent[tcp->scno].nargs;
1825 tcp->u_nargs = MAX_ARGS;
1826 for (i = 0; i < tcp->u_nargs; i++) {
1827 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
1831 #elif defined (ALPHA)
1834 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1835 tcp->u_nargs = sysent[tcp->scno].nargs;
1837 tcp->u_nargs = MAX_ARGS;
1838 for (i = 0; i < tcp->u_nargs; i++) {
1839 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1840 * for scno somewhere above here!
1842 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1846 #elif defined (IA64)
1849 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1850 /* be backwards compatible with kernel < 2.4.4... */
1852 # define PT_RBS_END PT_AR_BSP
1855 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
1857 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1860 sof = (cfm >> 0) & 0x7f;
1861 sol = (cfm >> 7) & 0x7f;
1862 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1864 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1865 && sysent[tcp->scno].nargs != -1)
1866 tcp->u_nargs = sysent[tcp->scno].nargs;
1868 tcp->u_nargs = MAX_ARGS;
1869 for (i = 0; i < tcp->u_nargs; ++i) {
1870 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1871 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1877 if (/* EBX = out0 */
1878 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1880 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1882 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1884 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1886 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1888 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1891 for (i = 0; i < 6; ++i)
1892 /* truncate away IVE sign-extension */
1893 tcp->u_arg[i] &= 0xffffffff;
1895 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1896 && sysent[tcp->scno].nargs != -1)
1897 tcp->u_nargs = sysent[tcp->scno].nargs;
1902 #elif defined (MIPS)
1907 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1908 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1910 nargs = tcp->u_nargs = MAX_ARGS;
1912 if(upeek(pid, REG_SP, &sp) < 0)
1914 for(i = 0; i < 4; i++) {
1915 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1918 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1919 (char *)(tcp->u_arg + 4));
1921 for(i = 0; i < nargs; i++) {
1922 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1927 #elif defined (POWERPC)
1929 #define PT_ORIG_R3 34
1933 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1934 tcp->u_nargs = sysent[tcp->scno].nargs;
1936 tcp->u_nargs = MAX_ARGS;
1937 for (i = 0; i < tcp->u_nargs; i++) {
1938 if (upeek(pid, (i==0) ?
1939 (sizeof(unsigned long)*PT_ORIG_R3) :
1940 ((i+PT_R3)*sizeof(unsigned long)),
1941 &tcp->u_arg[i]) < 0)
1945 #elif defined (SPARC) || defined (SPARC64)
1949 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1950 tcp->u_nargs = sysent[tcp->scno].nargs;
1952 tcp->u_nargs = MAX_ARGS;
1953 for (i = 0; i < tcp->u_nargs; i++)
1954 tcp->u_arg[i] = *((®s.r_o0) + i);
1956 #elif defined (HPPA)
1960 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1961 tcp->u_nargs = sysent[tcp->scno].nargs;
1963 tcp->u_nargs = MAX_ARGS;
1964 for (i = 0; i < tcp->u_nargs; i++) {
1965 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1973 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1974 tcp->u_nargs = sysent[tcp->scno].nargs;
1976 tcp->u_nargs = MAX_ARGS;
1977 for (i = 0; i < tcp->u_nargs; i++)
1978 tcp->u_arg[i] = regs.uregs[i];
1983 static int syscall_regs[] = {
1984 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
1985 REG_REG0, REG_REG0+1, REG_REG0+2
1988 tcp->u_nargs = sysent[tcp->scno].nargs;
1989 for (i = 0; i < tcp->u_nargs; i++) {
1990 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
1997 /* Registers used by SH5 Linux system calls for parameters */
1998 static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
2001 * TODO: should also check that the number of arguments encoded
2002 * in the trap number matches the number strace expects.
2005 assert(sysent[tcp->scno].nargs <
2006 sizeof(syscall_regs)/sizeof(syscall_regs[0]));
2009 tcp->u_nargs = sysent[tcp->scno].nargs;
2010 for (i = 0; i < tcp->u_nargs; i++) {
2011 if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
2016 #elif defined(X86_64)
2019 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
2020 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
2021 {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */
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 = MAX_ARGS;
2028 for (i = 0; i < tcp->u_nargs; i++) {
2029 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
2033 #else /* Other architecture (like i386) (32bits specific) */
2036 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2037 tcp->u_nargs = sysent[tcp->scno].nargs;
2039 tcp->u_nargs = MAX_ARGS;
2040 for (i = 0; i < tcp->u_nargs; i++) {
2041 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
2050 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2051 tcp->u_nargs = sysent[tcp->scno].nargs;
2053 tcp->u_nargs = MAX_ARGS;
2054 for (i = 0; i < tcp->u_nargs; i++) {
2057 if (upeek(pid, uoff(u_arg[0]) +
2058 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
2066 * SGI is broken: even though it has pr_sysarg, it doesn't
2067 * set them on system call entry. Get a clue.
2069 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2070 tcp->u_nargs = sysent[tcp->scno].nargs;
2072 tcp->u_nargs = tcp->status.pr_nsysarg;
2073 if (tcp->u_nargs > 4) {
2074 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2075 4*sizeof(tcp->u_arg[0]));
2076 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
2077 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
2080 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2081 tcp->u_nargs*sizeof(tcp->u_arg[0]));
2085 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
2087 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2088 tcp->u_nargs = sysent[tcp->scno].nargs;
2090 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
2091 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2092 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2093 #elif defined (HAVE_PR_SYSCALL)
2094 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2095 tcp->u_nargs = sysent[tcp->scno].nargs;
2097 tcp->u_nargs = tcp->status.pr_nsysarg;
2100 for (i = 0; i < tcp->u_nargs; i++)
2101 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
2103 #elif defined (I386)
2104 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2105 tcp->u_nargs = sysent[tcp->scno].nargs;
2108 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2109 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2111 I DONT KNOW WHAT TO DO
2112 #endif /* !HAVE_PR_SYSCALL */
2115 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2116 sysent[tcp->scno].nargs > tcp->status.val)
2117 tcp->u_nargs = sysent[tcp->scno].nargs;
2119 tcp->u_nargs = tcp->status.val;
2120 if (tcp->u_nargs < 0)
2122 if (tcp->u_nargs > MAX_ARGS)
2123 tcp->u_nargs = MAX_ARGS;
2124 switch(regs.r_eax) {
2126 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2127 regs.r_esp + sizeof(int) + sizeof(quad_t));
2130 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2131 regs.r_esp + 2 * sizeof(int));
2134 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2135 regs.r_esp + sizeof(int));
2138 #endif /* FREEBSD */
2150 /* Measure the exit time as early as possible to avoid errors. */
2151 if (dtime && (tcp->flags & TCB_INSYSCALL))
2152 gettimeofday(&tv, NULL);
2154 res = get_scno(tcp);
2158 res = syscall_fixup(tcp);
2162 if (tcp->flags & TCB_INSYSCALL) {
2164 res = get_error(tcp);
2168 internal_syscall(tcp);
2169 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2170 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2171 tcp->flags &= ~TCB_INSYSCALL;
2175 if (tcp->flags & TCB_REPRINT) {
2178 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2179 tprintf("syscall_%lu", tcp->scno);
2181 tprintf("%s", sysent[tcp->scno].sys_name);
2182 tprintf(" resumed> ");
2185 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
2186 call_count[tcp->scno]++;
2188 error_count[tcp->scno]++;
2189 tv_sub(&tv, &tv, &tcp->etime);
2191 if (tv_cmp(&tv, &tcp->dtime) > 0) {
2192 static struct timeval one_tick;
2193 if (one_tick.tv_usec == 0) {
2194 /* Initialize it. */
2195 struct itimerval it;
2196 memset(&it, 0, sizeof it);
2197 it.it_interval.tv_usec = 1;
2198 setitimer(ITIMER_REAL, &it, NULL);
2199 getitimer(ITIMER_REAL, &it);
2200 one_tick = it.it_interval;
2203 if (tv_nz(&tcp->dtime))
2205 else if (tv_cmp(&tv, &one_tick) > 0) {
2206 if (tv_cmp(&shortest, &one_tick) < 0)
2213 if (tv_cmp(&tv, &shortest) < 0)
2215 tv_add(&tv_count[tcp->scno],
2216 &tv_count[tcp->scno], &tv);
2217 tcp->flags &= ~TCB_INSYSCALL;
2221 if (tcp->scno >= nsyscalls || tcp->scno < 0
2222 || (qual_flags[tcp->scno] & QUAL_RAW))
2223 sys_res = printargs(tcp);
2225 if (not_failing_only && tcp->u_error)
2226 return 0; /* ignore failed syscalls */
2227 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2229 u_error = tcp->u_error;
2232 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2233 qual_flags[tcp->scno] & QUAL_RAW) {
2235 tprintf("= -1 (errno %ld)", u_error);
2237 tprintf("= %#lx", tcp->u_rval);
2239 else if (!(sys_res & RVAL_NONE) && u_error) {
2243 tprintf("= ? ERESTARTSYS (To be restarted)");
2245 case ERESTARTNOINTR:
2246 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2248 case ERESTARTNOHAND:
2249 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2251 case ERESTART_RESTARTBLOCK:
2252 tprintf("= ? ERESTART_RESTARTBLOCK (To be restarted)");
2258 tprintf("E??? (errno %ld)", u_error);
2259 else if (u_error < nerrnos)
2260 tprintf("%s (%s)", errnoent[u_error],
2263 tprintf("ERRNO_%ld (%s)", u_error,
2269 if (sys_res & RVAL_NONE)
2272 switch (sys_res & RVAL_MASK) {
2274 tprintf("= %#lx", tcp->u_rval);
2277 tprintf("= %#lo", tcp->u_rval);
2280 tprintf("= %lu", tcp->u_rval);
2283 tprintf("= %ld", tcp->u_rval);
2285 #ifdef HAVE_LONG_LONG
2287 tprintf("= %#llx", tcp->u_lrval);
2290 tprintf("= %#llo", tcp->u_lrval);
2292 case RVAL_LUDECIMAL:
2293 tprintf("= %llu", tcp->u_lrval);
2296 tprintf("= %lld", tcp->u_lrval);
2301 "invalid rval format\n");
2305 if ((sys_res & RVAL_STR) && tcp->auxstr)
2306 tprintf(" (%s)", tcp->auxstr);
2309 tv_sub(&tv, &tv, &tcp->etime);
2310 tprintf(" <%ld.%06ld>",
2311 (long) tv.tv_sec, (long) tv.tv_usec);
2316 if (fflush(tcp->outf) == EOF)
2318 tcp->flags &= ~TCB_INSYSCALL;
2322 /* Entering system call */
2323 res = syscall_enter(tcp);
2327 switch (tcp->scno + NR_SYSCALL_BASE) {
2329 #if !defined (ALPHA) && !defined(SPARC) && !defined(SPARC64) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
2330 case SYS_socketcall:
2331 decode_subcall(tcp, SYS_socket_subcall,
2332 SYS_socket_nsubcalls, deref_style);
2335 decode_subcall(tcp, SYS_ipc_subcall,
2336 SYS_ipc_nsubcalls, shift_style);
2338 #endif /* !ALPHA && !MIPS && !SPARC && !SPARC64 && !HPPA && !X86_64 */
2339 #if defined (SPARC) || defined (SPARC64)
2340 case SYS_socketcall:
2341 sparc_socket_decode (tcp);
2346 #ifdef SYS_pgrpsys_subcall
2348 decode_subcall(tcp, SYS_pgrpsys_subcall,
2349 SYS_pgrpsys_nsubcalls, shift_style);
2351 #endif /* SYS_pgrpsys_subcall */
2352 #ifdef SYS_sigcall_subcall
2354 decode_subcall(tcp, SYS_sigcall_subcall,
2355 SYS_sigcall_nsubcalls, mask_style);
2357 #endif /* SYS_sigcall_subcall */
2359 decode_subcall(tcp, SYS_msgsys_subcall,
2360 SYS_msgsys_nsubcalls, shift_style);
2363 decode_subcall(tcp, SYS_shmsys_subcall,
2364 SYS_shmsys_nsubcalls, shift_style);
2367 decode_subcall(tcp, SYS_semsys_subcall,
2368 SYS_semsys_nsubcalls, shift_style);
2372 decode_subcall(tcp, SYS_utssys_subcall,
2373 SYS_utssys_nsubcalls, shift_style);
2377 decode_subcall(tcp, SYS_sysfs_subcall,
2378 SYS_sysfs_nsubcalls, shift_style);
2381 decode_subcall(tcp, SYS_spcall_subcall,
2382 SYS_spcall_nsubcalls, shift_style);
2384 #ifdef SYS_context_subcall
2386 decode_subcall(tcp, SYS_context_subcall,
2387 SYS_context_nsubcalls, shift_style);
2389 #endif /* SYS_context_subcall */
2390 #ifdef SYS_door_subcall
2392 decode_subcall(tcp, SYS_door_subcall,
2393 SYS_door_nsubcalls, door_style);
2395 #endif /* SYS_door_subcall */
2396 #ifdef SYS_kaio_subcall
2398 decode_subcall(tcp, SYS_kaio_subcall,
2399 SYS_kaio_nsubcalls, shift_style);
2407 decode_subcall(tcp, 0, 0, table_style);
2412 decode_subcall(tcp, SYS_semsys_subcall,
2413 SYS_semsys_nsubcalls, shift_style);
2416 decode_subcall(tcp, SYS_msgsys_subcall,
2417 SYS_msgsys_nsubcalls, shift_style);
2420 decode_subcall(tcp, SYS_shmsys_subcall,
2421 SYS_shmsys_nsubcalls, shift_style);
2426 internal_syscall(tcp);
2427 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2428 tcp->flags |= TCB_INSYSCALL;
2433 gettimeofday(&tcp->etime, NULL);
2434 tcp->flags |= TCB_INSYSCALL;
2439 tcp->flags &= ~TCB_REPRINT;
2441 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2442 tprintf("syscall_%lu(", tcp->scno);
2444 tprintf("%s(", sysent[tcp->scno].sys_name);
2445 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2446 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2447 sys_res = printargs(tcp);
2449 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2450 if (fflush(tcp->outf) == EOF)
2452 tcp->flags |= TCB_INSYSCALL;
2453 /* Measure the entrance time as late as possible to avoid errors. */
2455 gettimeofday(&tcp->etime, NULL);
2463 if (entering(tcp)) {
2466 for (i = 0; i < tcp->u_nargs; i++)
2467 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2479 #if defined (SPARC) || defined (SPARC64)
2481 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
2485 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2487 #endif /* SPARC || SPARC64 */
2489 if (upeek(tcp->pid, PT_R9, &val) < 0)
2494 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2500 val = tcp->status.PR_REG[R_O1];
2503 val = tcp->status.PR_REG[EDX];
2506 val = tcp->status.PR_REG[RDX];
2509 val = tcp->status.PR_REG[CTX_V1];
2514 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
2521 * Apparently, indirect system calls have already be converted by ptrace(2),
2522 * so if you see "indir" this program has gone astray.
2530 if (entering(tcp)) {
2531 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2532 fprintf(stderr, "Bogus syscall: %u\n", scno);
2535 nargs = sysent[scno].nargs;
2536 tprintf("%s", sysent[scno].sys_name);
2537 for (i = 0; i < nargs; i++)
2538 tprintf(", %#lx", tcp->u_arg[i+1]);
2548 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2556 return strcmp(sysent[*((int *) a)].sys_name,
2557 sysent[*((int *) b)].sys_name);
2565 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2567 return (m < n) ? 1 : (m > n) ? -1 : 0;
2570 static int (*sortfun)();
2571 static struct timeval overhead = { -1, -1 };
2577 if (strcmp(sortby, "time") == 0)
2579 else if (strcmp(sortby, "calls") == 0)
2580 sortfun = count_cmp;
2581 else if (strcmp(sortby, "name") == 0)
2582 sortfun = syscall_cmp;
2583 else if (strcmp(sortby, "nothing") == 0)
2586 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2591 void set_overhead(n)
2594 overhead.tv_sec = n / 1000000;
2595 overhead.tv_usec = n % 1000000;
2603 int call_cum, error_cum;
2604 struct timeval tv_cum, dtv;
2606 char *dashes = "-------------------------";
2609 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2610 if (overhead.tv_sec == -1) {
2611 tv_mul(&overhead, &shortest, 8);
2612 tv_div(&overhead, &overhead, 10);
2614 for (i = 0; i < nsyscalls; i++) {
2615 sorted_count[i] = i;
2616 if (call_count[i] == 0)
2618 tv_mul(&dtv, &overhead, call_count[i]);
2619 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2620 call_cum += call_count[i];
2621 error_cum += error_count[i];
2622 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2625 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2626 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2627 "% time", "seconds", "usecs/call",
2628 "calls", "errors", "syscall");
2629 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2630 dashes, dashes, dashes, dashes, dashes, dashes);
2631 for (i = 0; i < nsyscalls; i++) {
2632 j = sorted_count[i];
2633 if (call_count[j] == 0)
2635 tv_div(&dtv, &tv_count[j], call_count[j]);
2637 sprintf(error_str, "%d", error_count[j]);
2639 error_str[0] = '\0';
2640 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2641 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2642 percent, (long) tv_count[j].tv_sec,
2643 (long) tv_count[j].tv_usec,
2644 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2645 call_count[j], error_str, sysent[j].sys_name);
2647 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2648 dashes, dashes, dashes, dashes, dashes, dashes);
2650 sprintf(error_str, "%d", error_cum);
2652 error_str[0] = '\0';
2653 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2654 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2655 call_cum, error_str, "total");