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 #include <linux/ptrace.h>
69 #if defined(LINUX) && defined(IA64)
70 # include <asm/ptrace_offsets.h>
74 #define NR_SYSCALL_BASE 0
77 #define ERESTARTSYS 512
79 #ifndef ERESTARTNOINTR
80 #define ERESTARTNOINTR 513
82 #ifndef ERESTARTNOHAND
83 #define ERESTARTNOHAND 514 /* restart if no handler.. */
86 #define ENOIOCTLCMD 515 /* No ioctl command */
94 #undef NR_SYSCALL_BASE
95 #define NR_SYSCALL_BASE __NR_SYSCALL_BASE
101 /* Define these shorthand notations to simplify the syscallent files. */
102 #define TF TRACE_FILE
104 #define TN TRACE_NETWORK
105 #define TP TRACE_PROCESS
106 #define TS TRACE_SIGNAL
108 struct sysent sysent0[] = {
109 #include "syscallent.h"
111 int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
113 #if SUPPORTED_PERSONALITIES >= 2
114 struct sysent sysent1[] = {
115 #include "syscallent1.h"
117 int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
118 #endif /* SUPPORTED_PERSONALITIES >= 2 */
120 #if SUPPORTED_PERSONALITIES >= 3
121 struct sysent sysent2[] = {
122 #include "syscallent2.h"
124 int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
125 #endif /* SUPPORTED_PERSONALITIES >= 3 */
127 struct sysent *sysent;
130 /* Now undef them since short defines cause wicked namespace pollution. */
137 char *errnoent0[] = {
138 #include "errnoent.h"
140 int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
142 #if SUPPORTED_PERSONALITIES >= 2
143 char *errnoent1[] = {
144 #include "errnoent1.h"
146 int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
147 #endif /* SUPPORTED_PERSONALITIES >= 2 */
149 #if SUPPORTED_PERSONALITIES >= 3
150 char *errnoent2[] = {
151 #include "errnoent2.h"
153 int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
154 #endif /* SUPPORTED_PERSONALITIES >= 3 */
159 int current_personality;
162 set_personality(personality)
165 switch (personality) {
167 errnoent = errnoent0;
170 nsyscalls = nsyscalls0;
171 ioctlent = ioctlent0;
172 nioctlents = nioctlents0;
173 signalent = signalent0;
174 nsignals = nsignals0;
177 #if SUPPORTED_PERSONALITIES >= 2
179 errnoent = errnoent1;
182 nsyscalls = nsyscalls1;
183 ioctlent = ioctlent1;
184 nioctlents = nioctlents1;
185 signalent = signalent1;
186 nsignals = nsignals1;
188 #endif /* SUPPORTED_PERSONALITIES >= 2 */
190 #if SUPPORTED_PERSONALITIES >= 3
192 errnoent = errnoent2;
195 nsyscalls = nsyscalls2;
196 ioctlent = ioctlent2;
197 nioctlents = nioctlents2;
198 signalent = signalent2;
199 nsignals = nsignals2;
201 #endif /* SUPPORTED_PERSONALITIES >= 3 */
207 current_personality = personality;
211 int qual_flags[MAX_QUALS];
213 static int call_count[MAX_QUALS];
214 static int error_count[MAX_QUALS];
215 static struct timeval tv_count[MAX_QUALS];
216 static int sorted_count[MAX_QUALS];
218 static struct timeval shortest = { 1000000, 0 };
220 static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
222 static struct qual_options {
228 { QUAL_TRACE, "trace", qual_syscall, "system call" },
229 { QUAL_TRACE, "t", qual_syscall, "system call" },
230 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
231 { QUAL_ABBREV, "a", qual_syscall, "system call" },
232 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
233 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
234 { QUAL_RAW, "raw", qual_syscall, "system call" },
235 { QUAL_RAW, "x", qual_syscall, "system call" },
236 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
237 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
238 { QUAL_SIGNAL, "s", qual_signal, "signal" },
239 { QUAL_FAULT, "fault", qual_fault, "fault" },
240 { QUAL_FAULT, "faults", qual_fault, "fault" },
241 { QUAL_FAULT, "m", qual_fault, "fault" },
242 { QUAL_READ, "read", qual_desc, "descriptor" },
243 { QUAL_READ, "reads", qual_desc, "descriptor" },
244 { QUAL_READ, "r", qual_desc, "descriptor" },
245 { QUAL_WRITE, "write", qual_desc, "descriptor" },
246 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
247 { QUAL_WRITE, "w", qual_desc, "descriptor" },
248 { 0, NULL, NULL, NULL },
252 qualify_one(n, opt, not)
254 struct qual_options *opt;
258 qual_flags[n] &= ~opt->bitflag;
260 qual_flags[n] |= opt->bitflag;
264 qual_syscall(s, opt, not)
266 struct qual_options *opt;
272 for (i = 0; i < nsyscalls; i++) {
273 if (strcmp(s, sysent[i].sys_name) == 0) {
274 qualify_one(i, opt, not);
282 qual_signal(s, opt, not)
284 struct qual_options *opt;
290 if (s && *s && isdigit((unsigned char)*s)) {
291 qualify_one(atoi(s), opt, not);
296 for (i = 0; s[i]; i++)
297 s[i] = toupper((unsigned char)(s[i]));
298 if (strncmp(s, "SIG", 3) == 0)
300 for (i = 0; i <= NSIG; i++)
301 if (strcmp(s, signame(i) + 3) == 0) {
302 qualify_one(atoi(s), opt, not);
309 qual_fault(s, opt, not)
311 struct qual_options *opt;
318 qual_desc(s, opt, not)
320 struct qual_options *opt;
323 if (s && *s && isdigit((unsigned char)*s)) {
324 qualify_one(atoi(s), opt, not);
333 if (strcmp(s, "file") == 0)
335 if (strcmp(s, "ipc") == 0)
337 if (strcmp(s, "network") == 0)
338 return TRACE_NETWORK;
339 if (strcmp(s, "process") == 0)
340 return TRACE_PROCESS;
341 if (strcmp(s, "signal") == 0)
350 struct qual_options *opt;
355 opt = &qual_options[0];
356 for (i = 0; (p = qual_options[i].option_name); i++) {
358 if (strncmp(s, p, n) == 0 && s[n] == '=') {
359 opt = &qual_options[i];
369 if (strcmp(s, "none") == 0) {
373 if (strcmp(s, "all") == 0) {
374 for (i = 0; i < MAX_QUALS; i++) {
376 qual_flags[i] &= ~opt->bitflag;
378 qual_flags[i] |= opt->bitflag;
382 for (i = 0; i < MAX_QUALS; i++) {
384 qual_flags[i] |= opt->bitflag;
386 qual_flags[i] &= ~opt->bitflag;
388 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
389 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
390 for (i = 0; i < MAX_QUALS; i++) {
391 if (sysent[i].sys_flags & n) {
393 qual_flags[i] &= ~opt->bitflag;
395 qual_flags[i] |= opt->bitflag;
400 if (opt->qualify(p, opt, not)) {
401 fprintf(stderr, "strace: invalid %s `%s'\n",
402 opt->argument_name, p);
415 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
417 switch (tcp->scno + NR_SYSCALL_BASE) {
425 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
426 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
435 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
436 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
440 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
441 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
447 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
448 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
455 enum subcall_style { shift_style, deref_style, mask_style, door_style };
457 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
465 const struct subcall subcalls_table[] = {
466 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
468 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
470 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
472 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
476 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
478 const int socket_map [] = {
481 /* SYS_CONNECT */ 98,
482 /* SYS_LISTEN */ 106,
484 /* SYS_GETSOCKNAME */ 150,
485 /* SYS_GETPEERNAME */ 141,
486 /* SYS_SOCKETPAIR */ 135,
489 /* SYS_SENDTO */ 133,
490 /* SYS_RECVFROM */ 125,
491 /* SYS_SHUTDOWN */ 134,
492 /* SYS_SETSOCKOPT */ 105,
493 /* SYS_GETSOCKOPT */ 118,
494 /* SYS_SENDMSG */ 114,
495 /* SYS_RECVMSG */ 113
499 sparc_socket_decode (tcp)
505 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
508 tcp->scno = socket_map [tcp->u_arg [0]-1];
509 n = tcp->u_nargs = sysent [tcp->scno].nargs;
510 addr = tcp->u_arg [1];
511 for (i = 0; i < n; i++){
513 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
515 tcp->u_arg [i] = arg;
516 addr += sizeof (arg);
521 decode_subcall(tcp, subcall, nsubcalls, style)
525 enum subcall_style style;
527 long addr, mask, arg;
532 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
534 tcp->scno = subcall + tcp->u_arg[0];
535 if (sysent[tcp->scno].nargs != -1)
536 tcp->u_nargs = sysent[tcp->scno].nargs;
539 for (i = 0; i < tcp->u_nargs; i++)
540 tcp->u_arg[i] = tcp->u_arg[i + 1];
543 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
545 tcp->scno = subcall + tcp->u_arg[0];
546 addr = tcp->u_arg[1];
547 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
548 if (umove(tcp, addr, &arg) < 0)
553 tcp->u_nargs = sysent[tcp->scno].nargs;
556 mask = (tcp->u_arg[0] >> 8) & 0xff;
557 for (i = 0; mask; i++)
561 tcp->u_arg[0] &= 0xff;
562 tcp->scno = subcall + i;
563 if (sysent[tcp->scno].nargs != -1)
564 tcp->u_nargs = sysent[tcp->scno].nargs;
568 * Oh, yuck. The call code is the *sixth* argument.
569 * (don't you mean the *last* argument? - JH)
571 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
573 tcp->scno = subcall + tcp->u_arg[5];
574 if (sysent[tcp->scno].nargs != -1)
575 tcp->u_nargs = sysent[tcp->scno].nargs;
581 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
582 if (subcalls_table[i].call == tcp->scno) break;
583 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
584 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
585 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
586 for (i = 0; i < tcp->u_nargs; i++)
587 tcp->u_arg[i] = tcp->u_arg[i + 1];
595 struct tcb *tcp_last = NULL;
598 internal_syscall(tcp)
602 * We must always trace a few critical system calls in order to
603 * correctly support following forks in the presence of tracing
606 switch (tcp->scno + NR_SYSCALL_BASE) {
676 #ifdef __NR_exit_group
677 case __NR_exit_group:
692 #elif defined (POWERPC)
693 static long result,flags;
698 #elif defined (ALPHA)
701 #elif defined (SPARC)
702 static struct regs regs;
703 static unsigned long trap;
707 #elif defined(S390) || defined(S390X)
710 static long syscall_mode;
715 #elif defined(X86_64)
733 #if defined(S390) || defined(S390X)
734 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
736 if (syscall_mode != -ENOSYS) {
738 * Since kernel version 2.5.44 the scno gets passed in gpr2.
742 if (tcp->flags & TCB_WAITEXECVE) {
744 * When the execve system call completes successfully, the
745 * new process still has -ENOSYS (old style) or __NR_execve
746 * (new style) in gpr2. We cannot recover the scno again
747 * by disassembly, because the image that executed the
748 * syscall is gone now. Fortunately, we don't want it. We
749 * leave the flag set so that syscall_fixup can fake the
752 if (tcp->flags & TCB_INSYSCALL)
755 * This is the SIGTRAP after execve. We cannot try to read
756 * the system call here either.
758 tcp->flags &= ~TCB_WAITEXECVE;
763 * Old style of "passing" the scno via the SVC instruction.
766 long opcode, offset_reg, tmp;
768 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
769 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
770 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
771 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
773 if (upeek(pid, PT_PSWADDR, &pc) < 0)
776 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
778 perror("peektext(pc-oneword)");
783 * We have to check if the SVC got executed directly or via an
784 * EXECUTE instruction. In case of EXECUTE it is necessary to do
785 * instruction decoding to derive the system call number.
786 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
787 * so that this doesn't work if a SVC opcode is part of an EXECUTE
788 * opcode. Since there is no way to find out the opcode size this
789 * is the best we can do...
792 if ((opcode & 0xff00) == 0x0a00) {
794 scno = opcode & 0xff;
797 /* SVC got executed by EXECUTE instruction */
800 * Do instruction decoding of EXECUTE. If you really want to
801 * understand this, read the Principles of Operations.
803 svc_addr = (void *) (opcode & 0xfff);
806 offset_reg = (opcode & 0x000f0000) >> 16;
807 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
812 offset_reg = (opcode & 0x0000f000) >> 12;
813 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
817 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
826 offset_reg = (opcode & 0x00f00000) >> 20;
827 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
830 scno = (scno | tmp) & 0xff;
833 #elif defined (POWERPC)
834 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
836 if (!(tcp->flags & TCB_INSYSCALL)) {
837 /* Check if we return from execve. */
838 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
839 tcp->flags &= ~TCB_WAITEXECVE;
844 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
846 #elif defined (X86_64)
847 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
850 if (!(tcp->flags & TCB_INSYSCALL)) {
851 static int currpers=-1;
854 /* Check CS register value. On x86-64 linux it is:
855 * 0x33 for long mode (64 bit)
856 * 0x23 for compatibility mode (32 bit)
857 * It takes only one ptrace and thus doesn't need
860 if (upeek(pid, 8*CS, &val) < 0)
864 case 0x23: currpers = 1; break;
865 case 0x33: currpers = 0; break;
867 fprintf(stderr, "Unknown value CS=0x%02X while "
868 "detecting personality of process "
869 "PID=%d\n", (int)val, pid);
870 currpers = current_personality;
874 /* This version analyzes the opcode of a syscall instruction.
875 * (int 0x80 on i386 vs. syscall on x86-64)
876 * It works, but is too complicated.
878 unsigned long val, rip, i;
880 if(upeek(pid, 8*RIP, &rip)<0)
881 perror("upeek(RIP)");
883 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
887 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
889 printf("ptrace_peektext failed: %s\n",
891 switch (call & 0xffff)
893 /* x86-64: syscall = 0x0f 0x05 */
894 case 0x050f: currpers = 0; break;
895 /* i386: int 0x80 = 0xcd 0x80 */
896 case 0x80cd: currpers = 1; break;
898 currpers = current_personality;
900 "Unknown syscall opcode (0x%04X) while "
901 "detecting personality of process "
902 "PID=%d\n", (int)call, pid);
906 if(currpers != current_personality)
908 char *names[]={"64 bit", "32 bit"};
909 set_personality(currpers);
910 printf("[ Process PID=%d runs in %s mode. ]\n",
911 pid, names[current_personality]);
915 # define IA64_PSR_IS ((long)1 << 34)
916 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
917 ia32 = (psr & IA64_PSR_IS) != 0;
918 if (!(tcp->flags & TCB_INSYSCALL)) {
920 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
923 if (upeek (pid, PT_R15, &scno) < 0)
926 /* Check if we return from execve. */
927 if (tcp->flags & TCB_WAITEXECVE) {
928 tcp->flags &= ~TCB_WAITEXECVE;
932 /* syscall in progress */
933 if (upeek (pid, PT_R8, &r8) < 0)
935 if (upeek (pid, PT_R10, &r10) < 0)
941 upeek(pid, 4*15, &pc);
942 umoven(tcp, pc-4, 4, (char *)&scno);
946 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
949 if (upeek(pid, REG_A3, &a3) < 0)
952 if(!(tcp->flags & TCB_INSYSCALL)) {
953 if (upeek(pid, REG_V0, &scno) < 0)
956 if (scno < 0 || scno > nsyscalls) {
957 if(a3 == 0 || a3 == -1) {
959 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
964 if (upeek(pid, REG_V0, &r2) < 0)
967 #elif defined (ALPHA)
968 if (upeek(pid, REG_A3, &a3) < 0)
971 if (!(tcp->flags & TCB_INSYSCALL)) {
972 if (upeek(pid, REG_R0, &scno) < 0)
975 /* Check if we return from execve. */
976 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
977 tcp->flags &= ~TCB_WAITEXECVE;
982 * Do some sanity checks to figure out if it's
983 * really a syscall entry
985 if (scno < 0 || scno > nsyscalls) {
986 if (a3 == 0 || a3 == -1) {
988 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
994 if (upeek(pid, REG_R0, &r0) < 0)
997 #elif defined (SPARC)
998 /* Everything we need is in the current register set. */
999 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
1002 /* If we are entering, then disassemble the syscall trap. */
1003 if (!(tcp->flags & TCB_INSYSCALL)) {
1004 /* Retrieve the syscall trap instruction. */
1006 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
1010 /* Disassemble the trap to see what personality to use. */
1013 /* Linux/SPARC syscall trap. */
1017 /* Linux/SPARC64 syscall trap. */
1018 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
1021 /* SunOS syscall trap. (pers 1) */
1022 fprintf(stderr,"syscall: SunOS no support\n");
1025 /* Solaris 2.x syscall trap. (per 2) */
1029 /* NetBSD/FreeBSD syscall trap. */
1030 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1033 /* Solaris 2.x gettimeofday */
1037 /* Unknown syscall trap. */
1038 if(tcp->flags & TCB_WAITEXECVE) {
1039 tcp->flags &= ~TCB_WAITEXECVE;
1042 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
1046 /* Extract the system call number from the registers. */
1047 if (trap == 0x91d02027)
1053 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
1057 if (upeek(pid, PT_GR20, &scno) < 0)
1059 if (!(tcp->flags & TCB_INSYSCALL)) {
1060 /* Check if we return from execve. */
1061 if ((tcp->flags & TCB_WAITEXECVE)) {
1062 tcp->flags &= ~TCB_WAITEXECVE;
1068 * In the new syscall ABI, the system call number is in R3.
1070 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1074 /* Odd as it may seem, a glibc bug has been known to cause
1075 glibc to issue bogus negative syscall numbers. So for
1076 our purposes, make strace print what it *should* have been */
1077 long correct_scno = (scno & 0xff);
1080 "Detected glibc bug: bogus system call number = %ld, "
1081 "correcting to %ld\n",
1084 scno = correct_scno;
1088 if (!(tcp->flags & TCB_INSYSCALL)) {
1089 /* Check if we return from execve. */
1090 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1091 tcp->flags &= ~TCB_WAITEXECVE;
1098 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1101 /* new syscall ABI returns result in R0 */
1102 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1106 #ifdef HAVE_PR_SYSCALL
1107 scno = tcp->status.PR_SYSCALL;
1108 #else /* !HAVE_PR_SYSCALL */
1110 scno = tcp->status.PR_WHAT;
1112 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1116 switch (regs.r_eax) {
1119 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1125 #endif /* FREEBSD */
1126 #endif /* !HAVE_PR_SYSCALL */
1127 #endif /* USE_PROCFS */
1128 if (!(tcp->flags & TCB_INSYSCALL))
1140 #else /* USE_PROCFS */
1141 int scno = tcp->scno;
1143 if (!(tcp->flags & TCB_INSYSCALL)) {
1144 if (tcp->status.PR_WHY != PR_SYSENTRY) {
1148 || scno == SYS_vfork
1149 #endif /* SYS_vfork */
1151 || scno == SYS_fork1
1152 #endif /* SYS_fork1 */
1154 || scno == SYS_forkall
1155 #endif /* SYS_forkall */
1157 || scno == SYS_rfork1
1158 #endif /* SYS_fork1 */
1160 || scno == SYS_rforkall
1161 #endif /* SYS_rforkall */
1163 /* We are returning in the child, fake it. */
1164 tcp->status.PR_WHY = PR_SYSENTRY;
1166 tcp->status.PR_WHY = PR_SYSEXIT;
1169 fprintf(stderr, "syscall: missing entry\n");
1170 tcp->flags |= TCB_INSYSCALL;
1175 if (tcp->status.PR_WHY != PR_SYSEXIT) {
1176 fprintf(stderr, "syscall: missing exit\n");
1177 tcp->flags &= ~TCB_INSYSCALL;
1180 #endif /* USE_PROCFS */
1182 if (!(tcp->flags & TCB_INSYSCALL)) {
1184 fprintf(stderr, "syscall: missing entry\n");
1185 tcp->flags |= TCB_INSYSCALL;
1192 * This happens when a signal handler
1193 * for a signal which interrupted a
1194 * a system call makes another system call.
1196 fprintf(stderr, "syscall: missing exit\n");
1198 tcp->flags &= ~TCB_INSYSCALL;
1204 if (upeek(pid, 4*EAX, &eax) < 0)
1206 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1208 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1211 #elif defined (X86_64)
1212 if (upeek(pid, 8*RAX, &rax) < 0)
1214 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1216 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1219 #elif defined (S390) || defined (S390X)
1220 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1222 if (syscall_mode != -ENOSYS)
1223 syscall_mode = tcp->scno;
1224 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
1226 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1229 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1230 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1231 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1233 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1234 * flag set for the post-execve SIGTRAP to see and reset.
1238 #elif defined (POWERPC)
1239 # define SO_MASK 0x10000000
1240 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1242 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
1244 if (flags & SO_MASK)
1246 #elif defined (M68K)
1247 if (upeek(pid, 4*PT_D0, &d0) < 0)
1249 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1251 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1255 if (upeek(pid, 4*0, (long *)&r0) < 0)
1257 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1259 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
1262 #elif defined (HPPA)
1263 if (upeek(pid, PT_GR28, &r28) < 0)
1266 if (upeek(pid, PT_R10, &r10) < 0)
1268 if (upeek(pid, PT_R8, &r8) < 0)
1270 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1272 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1286 #if defined(S390) || defined(S390X)
1287 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1295 #else /* !S390 && !S390X */
1297 if (eax < 0 && -eax < nerrnos) {
1307 if (rax < 0 && -rax < nerrnos) {
1321 if (err < 0 && -err < nerrnos) {
1349 if (result && (unsigned) -result < nerrnos) {
1354 tcp->u_rval = result;
1357 #else /* !POWERPC */
1359 if (d0 && (unsigned) -d0 < nerrnos) {
1369 if (r0 && (unsigned) -r0 < nerrnos) {
1389 if (regs.r_psr & PSR_C) {
1391 u_error = regs.r_o0;
1394 tcp->u_rval = regs.r_o0;
1399 if (r28 && (unsigned) -r28 < nerrnos) {
1409 /* interpret R0 as return value or error number */
1410 if (r0 && (unsigned) -r0 < nerrnos) {
1424 #endif /* POWERPC */
1429 #endif /* S390 || S390X */
1432 /* get error code from user struct */
1433 if (upeek(pid, uoff(u_error), &u_error) < 0)
1435 u_error >>= 24; /* u_error is a char */
1437 /* get system call return value */
1438 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1443 /* Judicious guessing goes a long way. */
1444 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1446 u_error = tcp->status.pr_reg[R_O0];
1449 tcp->u_rval = tcp->status.pr_reg[R_O0];
1454 /* Wanna know how to kill an hour single-stepping? */
1455 if (tcp->status.PR_REG[EFL] & 0x1) {
1457 u_error = tcp->status.PR_REG[EAX];
1460 tcp->u_rval = tcp->status.PR_REG[EAX];
1461 #ifdef HAVE_LONG_LONG
1463 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1464 tcp->status.PR_REG[EAX];
1470 /* Wanna know how to kill an hour single-stepping? */
1471 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1473 u_error = tcp->status.PR_REG[RAX];
1476 tcp->u_rval = tcp->status.PR_REG[RAX];
1481 if (tcp->status.pr_reg[CTX_A3]) {
1483 u_error = tcp->status.pr_reg[CTX_V0];
1486 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1492 if (regs.r_eflags & PSL_C) {
1494 u_error = regs.r_eax;
1496 tcp->u_rval = regs.r_eax;
1498 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1501 #endif /* FREEBSD */
1502 tcp->u_error = u_error;
1507 force_result(tcp, error, rval)
1513 #if defined(S390) || defined(S390X)
1514 gpr2 = error ? -error : rval;
1515 if (upeek(tcp->pid, PT_GPR2, &gpr2) < 0)
1517 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1519 #else /* !S390 && !S390X */
1521 eax = error ? -error : rval;
1522 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1526 rax = error ? -error : rval;
1527 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 4), rax) < 0)
1532 r8 = error ? -error : rval;
1533 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1545 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1546 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1559 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1560 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1564 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1574 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1575 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
1577 #else /* !POWERPC */
1579 d0 = error ? -error : rval;
1580 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1584 r0 = error ? -error : rval;
1585 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), r0) < 0)
1597 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1598 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1602 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1605 regs.r_psr |= PSR_C;
1609 regs.r_psr &= ~PSR_C;
1612 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1616 r28 = error ? -error : rval;
1617 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1621 r0 = error ? -error : rval;
1622 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1630 #endif /* POWERPC */
1635 #endif /* S390 || S390X */
1638 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1640 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1648 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1653 regs.r_eflags |= PSL_C;
1657 regs.r_eflags &= ~PSL_C;
1660 if (pwrite(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1664 #endif /* FREEBSD */
1666 /* All branches reach here on success (only). */
1667 tcp->u_error = error;
1672 int syscall_enter(tcp)
1677 #endif /* !USE_PROCFS */
1679 #if defined(S390) || defined(S390X)
1682 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1683 tcp->u_nargs = sysent[tcp->scno].nargs;
1685 tcp->u_nargs = MAX_ARGS;
1686 for (i = 0; i < tcp->u_nargs; i++) {
1687 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
1691 #elif defined (ALPHA)
1694 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1695 tcp->u_nargs = sysent[tcp->scno].nargs;
1697 tcp->u_nargs = MAX_ARGS;
1698 for (i = 0; i < tcp->u_nargs; i++) {
1699 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1700 * for scno somewhere above here!
1702 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1706 #elif defined (IA64)
1709 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1710 /* be backwards compatible with kernel < 2.4.4... */
1712 # define PT_RBS_END PT_AR_BSP
1715 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
1717 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1720 sof = (cfm >> 0) & 0x7f;
1721 sol = (cfm >> 7) & 0x7f;
1722 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1724 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1725 && sysent[tcp->scno].nargs != -1)
1726 tcp->u_nargs = sysent[tcp->scno].nargs;
1728 tcp->u_nargs = MAX_ARGS;
1729 for (i = 0; i < tcp->u_nargs; ++i) {
1730 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1731 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1737 if (/* EBX = out0 */
1738 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1740 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1742 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1744 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1746 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1748 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1751 for (i = 0; i < 6; ++i)
1752 /* truncate away IVE sign-extension */
1753 tcp->u_arg[i] &= 0xffffffff;
1755 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1756 && sysent[tcp->scno].nargs != -1)
1757 tcp->u_nargs = sysent[tcp->scno].nargs;
1762 #elif defined (MIPS)
1767 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1768 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1770 nargs = tcp->u_nargs = MAX_ARGS;
1772 if(upeek(pid, REG_SP, &sp) < 0)
1774 for(i = 0; i < 4; i++) {
1775 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1778 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1779 (char *)(tcp->u_arg + 4));
1781 for(i = 0; i < nargs; i++) {
1782 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1787 #elif defined (POWERPC)
1789 #define PT_ORIG_R3 34
1793 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1794 tcp->u_nargs = sysent[tcp->scno].nargs;
1796 tcp->u_nargs = MAX_ARGS;
1797 for (i = 0; i < tcp->u_nargs; i++) {
1798 if (upeek(pid, (i==0) ?
1799 (sizeof(unsigned long)*PT_ORIG_R3) :
1800 ((i+PT_R3)*sizeof(unsigned long)),
1801 &tcp->u_arg[i]) < 0)
1805 #elif defined (SPARC)
1809 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1810 tcp->u_nargs = sysent[tcp->scno].nargs;
1812 tcp->u_nargs = MAX_ARGS;
1813 for (i = 0; i < tcp->u_nargs; i++)
1814 tcp->u_arg[i] = *((®s.r_o0) + i);
1816 #elif defined (HPPA)
1820 if (tcp->scno >= 0 && tcp->scno < nsyscalls && 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 (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1832 static int syscall_regs[] = {
1833 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
1834 REG_REG0, REG_REG0+1, REG_REG0+2
1837 tcp->u_nargs = sysent[tcp->scno].nargs;
1838 for (i = 0; i < tcp->u_nargs; i++) {
1839 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
1843 #elif defined(X86_64)
1846 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
1847 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
1848 {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */
1851 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1852 tcp->u_nargs = sysent[tcp->scno].nargs;
1854 tcp->u_nargs = MAX_ARGS;
1855 for (i = 0; i < tcp->u_nargs; i++) {
1856 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
1860 #else /* Other architecture (like i386) (32bits specific) */
1863 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1864 tcp->u_nargs = sysent[tcp->scno].nargs;
1866 tcp->u_nargs = MAX_ARGS;
1867 for (i = 0; i < tcp->u_nargs; i++) {
1868 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1877 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1878 tcp->u_nargs = sysent[tcp->scno].nargs;
1880 tcp->u_nargs = MAX_ARGS;
1881 for (i = 0; i < tcp->u_nargs; i++) {
1884 if (upeek(pid, uoff(u_arg[0]) +
1885 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1893 * SGI is broken: even though it has pr_sysarg, it doesn't
1894 * set them on system call entry. Get a clue.
1896 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1897 tcp->u_nargs = sysent[tcp->scno].nargs;
1899 tcp->u_nargs = tcp->status.pr_nsysarg;
1900 if (tcp->u_nargs > 4) {
1901 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1902 4*sizeof(tcp->u_arg[0]));
1903 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1904 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1907 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1908 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1912 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1914 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1915 tcp->u_nargs = sysent[tcp->scno].nargs;
1917 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1918 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1919 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1920 #elif defined (HAVE_PR_SYSCALL)
1921 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1922 tcp->u_nargs = sysent[tcp->scno].nargs;
1924 tcp->u_nargs = tcp->status.pr_nsysarg;
1927 for (i = 0; i < tcp->u_nargs; i++)
1928 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1930 #elif defined (I386)
1931 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1932 tcp->u_nargs = sysent[tcp->scno].nargs;
1935 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1936 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1938 I DONT KNOW WHAT TO DO
1939 #endif /* !HAVE_PR_SYSCALL */
1942 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1943 sysent[tcp->scno].nargs > tcp->status.val)
1944 tcp->u_nargs = sysent[tcp->scno].nargs;
1946 tcp->u_nargs = tcp->status.val;
1947 if (tcp->u_nargs < 0)
1949 if (tcp->u_nargs > MAX_ARGS)
1950 tcp->u_nargs = MAX_ARGS;
1951 switch(regs.r_eax) {
1953 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1954 regs.r_esp + sizeof(int) + sizeof(quad_t));
1957 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1958 regs.r_esp + 2 * sizeof(int));
1961 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1962 regs.r_esp + sizeof(int));
1965 #endif /* FREEBSD */
1977 /* Measure the exit time as early as possible to avoid errors. */
1978 if (dtime && (tcp->flags & TCB_INSYSCALL))
1979 gettimeofday(&tv, NULL);
1981 res = get_scno(tcp);
1985 res = syscall_fixup(tcp);
1989 if (tcp->flags & TCB_INSYSCALL) {
1991 res = get_error(tcp);
1995 internal_syscall(tcp);
1996 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1997 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
1998 tcp->flags &= ~TCB_INSYSCALL;
2002 if (tcp->flags & TCB_REPRINT) {
2005 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2006 tprintf("syscall_%lu", tcp->scno);
2008 tprintf("%s", sysent[tcp->scno].sys_name);
2009 tprintf(" resumed> ");
2012 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
2013 call_count[tcp->scno]++;
2015 error_count[tcp->scno]++;
2016 tv_sub(&tv, &tv, &tcp->etime);
2018 if (tv_cmp(&tv, &tcp->dtime) > 0) {
2019 static struct timeval one_tick =
2020 { 0, 1000000 / HZ };
2022 if (tv_nz(&tcp->dtime))
2024 else if (tv_cmp(&tv, &one_tick) > 0) {
2025 if (tv_cmp(&shortest, &one_tick) < 0)
2032 if (tv_cmp(&tv, &shortest) < 0)
2034 tv_add(&tv_count[tcp->scno],
2035 &tv_count[tcp->scno], &tv);
2036 tcp->flags &= ~TCB_INSYSCALL;
2040 if (tcp->scno >= nsyscalls || tcp->scno < 0
2041 || (qual_flags[tcp->scno] & QUAL_RAW))
2042 sys_res = printargs(tcp);
2044 if (not_failing_only && tcp->u_error)
2045 return 0; /* ignore failed syscalls */
2046 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2048 u_error = tcp->u_error;
2051 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2052 qual_flags[tcp->scno] & QUAL_RAW) {
2054 tprintf("= -1 (errno %ld)", u_error);
2056 tprintf("= %#lx", tcp->u_rval);
2058 else if (!(sys_res & RVAL_NONE) && u_error) {
2062 tprintf("= ? ERESTARTSYS (To be restarted)");
2064 case ERESTARTNOINTR:
2065 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2067 case ERESTARTNOHAND:
2068 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2074 tprintf("E??? (errno %ld)", u_error);
2075 else if (u_error < nerrnos)
2076 tprintf("%s (%s)", errnoent[u_error],
2079 tprintf("ERRNO_%ld (%s)", u_error,
2085 if (sys_res & RVAL_NONE)
2088 switch (sys_res & RVAL_MASK) {
2090 tprintf("= %#lx", tcp->u_rval);
2093 tprintf("= %#lo", tcp->u_rval);
2096 tprintf("= %lu", tcp->u_rval);
2099 tprintf("= %ld", tcp->u_rval);
2101 #ifdef HAVE_LONG_LONG
2103 tprintf("= %#llx", tcp->u_lrval);
2106 tprintf("= %#llo", tcp->u_lrval);
2108 case RVAL_LUDECIMAL:
2109 tprintf("= %llu", tcp->u_lrval);
2112 tprintf("= %lld", tcp->u_lrval);
2117 "invalid rval format\n");
2121 if ((sys_res & RVAL_STR) && tcp->auxstr)
2122 tprintf(" (%s)", tcp->auxstr);
2125 tv_sub(&tv, &tv, &tcp->etime);
2126 tprintf(" <%ld.%06ld>",
2127 (long) tv.tv_sec, (long) tv.tv_usec);
2132 if (fflush(tcp->outf) == EOF)
2134 tcp->flags &= ~TCB_INSYSCALL;
2138 /* Entering system call */
2139 res = syscall_enter(tcp);
2143 switch (tcp->scno + NR_SYSCALL_BASE) {
2145 #if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
2146 case SYS_socketcall:
2147 decode_subcall(tcp, SYS_socket_subcall,
2148 SYS_socket_nsubcalls, deref_style);
2151 decode_subcall(tcp, SYS_ipc_subcall,
2152 SYS_ipc_nsubcalls, shift_style);
2154 #endif /* !ALPHA && !MIPS && !SPARC && !HPPA && !X86_64 */
2156 case SYS_socketcall:
2157 sparc_socket_decode (tcp);
2162 #ifdef SYS_pgrpsys_subcall
2164 decode_subcall(tcp, SYS_pgrpsys_subcall,
2165 SYS_pgrpsys_nsubcalls, shift_style);
2167 #endif /* SYS_pgrpsys_subcall */
2168 #ifdef SYS_sigcall_subcall
2170 decode_subcall(tcp, SYS_sigcall_subcall,
2171 SYS_sigcall_nsubcalls, mask_style);
2173 #endif /* SYS_sigcall_subcall */
2175 decode_subcall(tcp, SYS_msgsys_subcall,
2176 SYS_msgsys_nsubcalls, shift_style);
2179 decode_subcall(tcp, SYS_shmsys_subcall,
2180 SYS_shmsys_nsubcalls, shift_style);
2183 decode_subcall(tcp, SYS_semsys_subcall,
2184 SYS_semsys_nsubcalls, shift_style);
2188 decode_subcall(tcp, SYS_utssys_subcall,
2189 SYS_utssys_nsubcalls, shift_style);
2193 decode_subcall(tcp, SYS_sysfs_subcall,
2194 SYS_sysfs_nsubcalls, shift_style);
2197 decode_subcall(tcp, SYS_spcall_subcall,
2198 SYS_spcall_nsubcalls, shift_style);
2200 #ifdef SYS_context_subcall
2202 decode_subcall(tcp, SYS_context_subcall,
2203 SYS_context_nsubcalls, shift_style);
2205 #endif /* SYS_context_subcall */
2206 #ifdef SYS_door_subcall
2208 decode_subcall(tcp, SYS_door_subcall,
2209 SYS_door_nsubcalls, door_style);
2211 #endif /* SYS_door_subcall */
2212 #ifdef SYS_kaio_subcall
2214 decode_subcall(tcp, SYS_kaio_subcall,
2215 SYS_kaio_nsubcalls, shift_style);
2223 decode_subcall(tcp, 0, 0, table_style);
2228 decode_subcall(tcp, SYS_semsys_subcall,
2229 SYS_semsys_nsubcalls, shift_style);
2232 decode_subcall(tcp, SYS_msgsys_subcall,
2233 SYS_msgsys_nsubcalls, shift_style);
2236 decode_subcall(tcp, SYS_shmsys_subcall,
2237 SYS_shmsys_nsubcalls, shift_style);
2242 internal_syscall(tcp);
2243 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2244 tcp->flags |= TCB_INSYSCALL;
2249 gettimeofday(&tcp->etime, NULL);
2250 tcp->flags |= TCB_INSYSCALL;
2255 tcp->flags &= ~TCB_REPRINT;
2257 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2258 tprintf("syscall_%lu(", tcp->scno);
2260 tprintf("%s(", sysent[tcp->scno].sys_name);
2261 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2262 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2263 sys_res = printargs(tcp);
2265 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2266 if (fflush(tcp->outf) == EOF)
2268 tcp->flags |= TCB_INSYSCALL;
2269 /* Measure the entrance time as late as possible to avoid errors. */
2271 gettimeofday(&tcp->etime, NULL);
2279 if (entering(tcp)) {
2282 for (i = 0; i < tcp->u_nargs; i++)
2283 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2297 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
2301 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2307 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2313 val = tcp->status.PR_REG[R_O1];
2316 val = tcp->status.PR_REG[EDX];
2319 val = tcp->status.PR_REG[RDX];
2322 val = tcp->status.PR_REG[CTX_V1];
2327 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
2334 * Apparently, indirect system calls have already be converted by ptrace(2),
2335 * so if you see "indir" this program has gone astray.
2343 if (entering(tcp)) {
2344 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2345 fprintf(stderr, "Bogus syscall: %u\n", scno);
2348 nargs = sysent[scno].nargs;
2349 tprintf("%s", sysent[scno].sys_name);
2350 for (i = 0; i < nargs; i++)
2351 tprintf(", %#lx", tcp->u_arg[i+1]);
2361 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2369 return strcmp(sysent[*((int *) a)].sys_name,
2370 sysent[*((int *) b)].sys_name);
2378 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2380 return (m < n) ? 1 : (m > n) ? -1 : 0;
2383 static int (*sortfun)();
2384 static struct timeval overhead = { -1, -1 };
2390 if (strcmp(sortby, "time") == 0)
2392 else if (strcmp(sortby, "calls") == 0)
2393 sortfun = count_cmp;
2394 else if (strcmp(sortby, "name") == 0)
2395 sortfun = syscall_cmp;
2396 else if (strcmp(sortby, "nothing") == 0)
2399 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2404 void set_overhead(n)
2407 overhead.tv_sec = n / 1000000;
2408 overhead.tv_usec = n % 1000000;
2416 int call_cum, error_cum;
2417 struct timeval tv_cum, dtv;
2419 char *dashes = "-------------------------";
2422 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2423 if (overhead.tv_sec == -1) {
2424 tv_mul(&overhead, &shortest, 8);
2425 tv_div(&overhead, &overhead, 10);
2427 for (i = 0; i < nsyscalls; i++) {
2428 sorted_count[i] = i;
2429 if (call_count[i] == 0)
2431 tv_mul(&dtv, &overhead, call_count[i]);
2432 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2433 call_cum += call_count[i];
2434 error_cum += error_count[i];
2435 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2438 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2439 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2440 "% time", "seconds", "usecs/call",
2441 "calls", "errors", "syscall");
2442 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2443 dashes, dashes, dashes, dashes, dashes, dashes);
2444 for (i = 0; i < nsyscalls; i++) {
2445 j = sorted_count[i];
2446 if (call_count[j] == 0)
2448 tv_div(&dtv, &tv_count[j], call_count[j]);
2450 sprintf(error_str, "%d", error_count[j]);
2452 error_str[0] = '\0';
2453 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2454 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2455 percent, (long) tv_count[j].tv_sec,
2456 (long) tv_count[j].tv_usec,
2457 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2458 call_count[j], error_str, sysent[j].sys_name);
2460 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2461 dashes, dashes, dashes, dashes, dashes, dashes);
2463 sprintf(error_str, "%d", error_cum);
2465 error_str[0] = '\0';
2466 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2467 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2468 call_cum, error_str, "total");