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);
334 if (strcmp(s, "file") == 0)
336 if (strcmp(s, "ipc") == 0)
338 if (strcmp(s, "network") == 0)
339 return TRACE_NETWORK;
340 if (strcmp(s, "process") == 0)
341 return TRACE_PROCESS;
342 if (strcmp(s, "signal") == 0)
351 struct qual_options *opt;
356 opt = &qual_options[0];
357 for (i = 0; (p = qual_options[i].option_name); i++) {
359 if (strncmp(s, p, n) == 0 && s[n] == '=') {
360 opt = &qual_options[i];
370 if (strcmp(s, "none") == 0) {
374 if (strcmp(s, "all") == 0) {
375 for (i = 0; i < MAX_QUALS; i++) {
377 qual_flags[i] &= ~opt->bitflag;
379 qual_flags[i] |= opt->bitflag;
383 for (i = 0; i < MAX_QUALS; i++) {
385 qual_flags[i] |= opt->bitflag;
387 qual_flags[i] &= ~opt->bitflag;
389 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
390 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
391 for (i = 0; i < MAX_QUALS; i++) {
392 if (sysent[i].sys_flags & n) {
394 qual_flags[i] &= ~opt->bitflag;
396 qual_flags[i] |= opt->bitflag;
401 if (opt->qualify(p, opt, not)) {
402 fprintf(stderr, "strace: invalid %s `%s'\n",
403 opt->argument_name, p);
416 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
418 switch (tcp->scno + NR_SYSCALL_BASE) {
426 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
427 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
436 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
437 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
441 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
442 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
448 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
449 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
456 enum subcall_style { shift_style, deref_style, mask_style, door_style };
458 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
466 const struct subcall subcalls_table[] = {
467 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
469 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
471 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
473 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
477 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
479 const int socket_map [] = {
482 /* SYS_CONNECT */ 98,
483 /* SYS_LISTEN */ 106,
485 /* SYS_GETSOCKNAME */ 150,
486 /* SYS_GETPEERNAME */ 141,
487 /* SYS_SOCKETPAIR */ 135,
490 /* SYS_SENDTO */ 133,
491 /* SYS_RECVFROM */ 125,
492 /* SYS_SHUTDOWN */ 134,
493 /* SYS_SETSOCKOPT */ 105,
494 /* SYS_GETSOCKOPT */ 118,
495 /* SYS_SENDMSG */ 114,
496 /* SYS_RECVMSG */ 113
500 sparc_socket_decode (tcp)
506 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
509 tcp->scno = socket_map [tcp->u_arg [0]-1];
510 n = tcp->u_nargs = sysent [tcp->scno].nargs;
511 addr = tcp->u_arg [1];
512 for (i = 0; i < n; i++){
514 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
516 tcp->u_arg [i] = arg;
517 addr += sizeof (arg);
522 decode_subcall(tcp, subcall, nsubcalls, style)
526 enum subcall_style style;
528 long addr, mask, arg;
533 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
535 tcp->scno = subcall + tcp->u_arg[0];
536 if (sysent[tcp->scno].nargs != -1)
537 tcp->u_nargs = sysent[tcp->scno].nargs;
540 for (i = 0; i < tcp->u_nargs; i++)
541 tcp->u_arg[i] = tcp->u_arg[i + 1];
544 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
546 tcp->scno = subcall + tcp->u_arg[0];
547 addr = tcp->u_arg[1];
548 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
549 if (umove(tcp, addr, &arg) < 0)
554 tcp->u_nargs = sysent[tcp->scno].nargs;
557 mask = (tcp->u_arg[0] >> 8) & 0xff;
558 for (i = 0; mask; i++)
562 tcp->u_arg[0] &= 0xff;
563 tcp->scno = subcall + i;
564 if (sysent[tcp->scno].nargs != -1)
565 tcp->u_nargs = sysent[tcp->scno].nargs;
569 * Oh, yuck. The call code is the *sixth* argument.
570 * (don't you mean the *last* argument? - JH)
572 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
574 tcp->scno = subcall + tcp->u_arg[5];
575 if (sysent[tcp->scno].nargs != -1)
576 tcp->u_nargs = sysent[tcp->scno].nargs;
582 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
583 if (subcalls_table[i].call == tcp->scno) break;
584 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
585 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
586 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
587 for (i = 0; i < tcp->u_nargs; i++)
588 tcp->u_arg[i] = tcp->u_arg[i + 1];
596 struct tcb *tcp_last = NULL;
599 internal_syscall(tcp)
603 * We must always trace a few critical system calls in order to
604 * correctly support following forks in the presence of tracing
607 switch (tcp->scno + NR_SYSCALL_BASE) {
677 #ifdef __NR_exit_group
678 case __NR_exit_group:
693 #elif defined (POWERPC)
694 static long result,flags;
699 #elif defined (ALPHA)
702 #elif defined (SPARC)
703 static struct regs regs;
704 static unsigned long trap;
708 #elif defined(S390) || defined(S390X)
711 static long syscall_mode;
716 #elif defined(SHMEDIA)
718 #elif defined(X86_64)
736 #if defined(S390) || defined(S390X)
737 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
739 if (syscall_mode != -ENOSYS) {
741 * Since kernel version 2.5.44 the scno gets passed in gpr2.
745 if (tcp->flags & TCB_WAITEXECVE) {
747 * When the execve system call completes successfully, the
748 * new process still has -ENOSYS (old style) or __NR_execve
749 * (new style) in gpr2. We cannot recover the scno again
750 * by disassembly, because the image that executed the
751 * syscall is gone now. Fortunately, we don't want it. We
752 * leave the flag set so that syscall_fixup can fake the
755 if (tcp->flags & TCB_INSYSCALL)
758 * This is the SIGTRAP after execve. We cannot try to read
759 * the system call here either.
761 tcp->flags &= ~TCB_WAITEXECVE;
766 * Old style of "passing" the scno via the SVC instruction.
769 long opcode, offset_reg, tmp;
771 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
772 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
773 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
774 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
776 if (upeek(pid, PT_PSWADDR, &pc) < 0)
779 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
781 perror("peektext(pc-oneword)");
786 * We have to check if the SVC got executed directly or via an
787 * EXECUTE instruction. In case of EXECUTE it is necessary to do
788 * instruction decoding to derive the system call number.
789 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
790 * so that this doesn't work if a SVC opcode is part of an EXECUTE
791 * opcode. Since there is no way to find out the opcode size this
792 * is the best we can do...
795 if ((opcode & 0xff00) == 0x0a00) {
797 scno = opcode & 0xff;
800 /* SVC got executed by EXECUTE instruction */
803 * Do instruction decoding of EXECUTE. If you really want to
804 * understand this, read the Principles of Operations.
806 svc_addr = (void *) (opcode & 0xfff);
809 offset_reg = (opcode & 0x000f0000) >> 16;
810 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
815 offset_reg = (opcode & 0x0000f000) >> 12;
816 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
820 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
829 offset_reg = (opcode & 0x00f00000) >> 20;
830 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
833 scno = (scno | tmp) & 0xff;
836 #elif defined (POWERPC)
837 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
839 if (!(tcp->flags & TCB_INSYSCALL)) {
840 /* Check if we return from execve. */
841 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
842 tcp->flags &= ~TCB_WAITEXECVE;
847 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
849 #elif defined (X86_64)
850 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
853 if (!(tcp->flags & TCB_INSYSCALL)) {
854 static int currpers=-1;
857 /* Check CS register value. On x86-64 linux it is:
858 * 0x33 for long mode (64 bit)
859 * 0x23 for compatibility mode (32 bit)
860 * It takes only one ptrace and thus doesn't need
863 if (upeek(pid, 8*CS, &val) < 0)
867 case 0x23: currpers = 1; break;
868 case 0x33: currpers = 0; break;
870 fprintf(stderr, "Unknown value CS=0x%02X while "
871 "detecting personality of process "
872 "PID=%d\n", (int)val, pid);
873 currpers = current_personality;
877 /* This version analyzes the opcode of a syscall instruction.
878 * (int 0x80 on i386 vs. syscall on x86-64)
879 * It works, but is too complicated.
881 unsigned long val, rip, i;
883 if(upeek(pid, 8*RIP, &rip)<0)
884 perror("upeek(RIP)");
886 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
890 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
892 printf("ptrace_peektext failed: %s\n",
894 switch (call & 0xffff)
896 /* x86-64: syscall = 0x0f 0x05 */
897 case 0x050f: currpers = 0; break;
898 /* i386: int 0x80 = 0xcd 0x80 */
899 case 0x80cd: currpers = 1; break;
901 currpers = current_personality;
903 "Unknown syscall opcode (0x%04X) while "
904 "detecting personality of process "
905 "PID=%d\n", (int)call, pid);
909 if(currpers != current_personality)
911 char *names[]={"64 bit", "32 bit"};
912 set_personality(currpers);
913 printf("[ Process PID=%d runs in %s mode. ]\n",
914 pid, names[current_personality]);
918 # define IA64_PSR_IS ((long)1 << 34)
919 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
920 ia32 = (psr & IA64_PSR_IS) != 0;
921 if (!(tcp->flags & TCB_INSYSCALL)) {
923 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
926 if (upeek (pid, PT_R15, &scno) < 0)
929 /* Check if we return from execve. */
930 if (tcp->flags & TCB_WAITEXECVE) {
931 tcp->flags &= ~TCB_WAITEXECVE;
935 /* syscall in progress */
936 if (upeek (pid, PT_R8, &r8) < 0)
938 if (upeek (pid, PT_R10, &r10) < 0)
944 upeek(pid, 4*15, &pc);
945 umoven(tcp, pc-4, 4, (char *)&scno);
949 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
952 if (upeek(pid, REG_A3, &a3) < 0)
955 if(!(tcp->flags & TCB_INSYSCALL)) {
956 if (upeek(pid, REG_V0, &scno) < 0)
959 if (scno < 0 || scno > nsyscalls) {
960 if(a3 == 0 || a3 == -1) {
962 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
967 if (upeek(pid, REG_V0, &r2) < 0)
970 #elif defined (ALPHA)
971 if (upeek(pid, REG_A3, &a3) < 0)
974 if (!(tcp->flags & TCB_INSYSCALL)) {
975 if (upeek(pid, REG_R0, &scno) < 0)
978 /* Check if we return from execve. */
979 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
980 tcp->flags &= ~TCB_WAITEXECVE;
985 * Do some sanity checks to figure out if it's
986 * really a syscall entry
988 if (scno < 0 || scno > nsyscalls) {
989 if (a3 == 0 || a3 == -1) {
991 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
997 if (upeek(pid, REG_R0, &r0) < 0)
1000 #elif defined (SPARC)
1001 /* Everything we need is in the current register set. */
1002 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
1005 /* If we are entering, then disassemble the syscall trap. */
1006 if (!(tcp->flags & TCB_INSYSCALL)) {
1007 /* Retrieve the syscall trap instruction. */
1009 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
1013 /* Disassemble the trap to see what personality to use. */
1016 /* Linux/SPARC syscall trap. */
1020 /* Linux/SPARC64 syscall trap. */
1021 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
1024 /* SunOS syscall trap. (pers 1) */
1025 fprintf(stderr,"syscall: SunOS no support\n");
1028 /* Solaris 2.x syscall trap. (per 2) */
1032 /* NetBSD/FreeBSD syscall trap. */
1033 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1036 /* Solaris 2.x gettimeofday */
1040 /* Unknown syscall trap. */
1041 if(tcp->flags & TCB_WAITEXECVE) {
1042 tcp->flags &= ~TCB_WAITEXECVE;
1045 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
1049 /* Extract the system call number from the registers. */
1050 if (trap == 0x91d02027)
1056 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
1060 if (upeek(pid, PT_GR20, &scno) < 0)
1062 if (!(tcp->flags & TCB_INSYSCALL)) {
1063 /* Check if we return from execve. */
1064 if ((tcp->flags & TCB_WAITEXECVE)) {
1065 tcp->flags &= ~TCB_WAITEXECVE;
1071 * In the new syscall ABI, the system call number is in R3.
1073 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1077 /* Odd as it may seem, a glibc bug has been known to cause
1078 glibc to issue bogus negative syscall numbers. So for
1079 our purposes, make strace print what it *should* have been */
1080 long correct_scno = (scno & 0xff);
1083 "Detected glibc bug: bogus system call number = %ld, "
1084 "correcting to %ld\n",
1087 scno = correct_scno;
1091 if (!(tcp->flags & TCB_INSYSCALL)) {
1092 /* Check if we return from execve. */
1093 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1094 tcp->flags &= ~TCB_WAITEXECVE;
1098 #elif defined(SHMEDIA)
1099 if (upeek(pid, REG_SYSCALL, &scno) < 0)
1103 if (!(tcp->flags & TCB_INSYSCALL)) {
1104 /* Check if we return from execve. */
1105 if (tcp->flags & TCB_WAITEXECVE) {
1106 tcp->flags &= ~TCB_WAITEXECVE;
1110 #endif /* SHMEDIA */
1113 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1116 /* new syscall ABI returns result in R0 */
1117 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1119 #elif defined(SHMEDIA)
1120 /* ABI defines result returned in r9 */
1121 if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
1126 #ifdef HAVE_PR_SYSCALL
1127 scno = tcp->status.PR_SYSCALL;
1128 #else /* !HAVE_PR_SYSCALL */
1130 scno = tcp->status.PR_WHAT;
1132 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1136 switch (regs.r_eax) {
1139 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1145 #endif /* FREEBSD */
1146 #endif /* !HAVE_PR_SYSCALL */
1147 #endif /* USE_PROCFS */
1148 if (!(tcp->flags & TCB_INSYSCALL))
1160 #else /* USE_PROCFS */
1161 int scno = tcp->scno;
1163 if (!(tcp->flags & TCB_INSYSCALL)) {
1164 if (tcp->status.PR_WHY != PR_SYSENTRY) {
1168 || scno == SYS_vfork
1169 #endif /* SYS_vfork */
1171 || scno == SYS_fork1
1172 #endif /* SYS_fork1 */
1174 || scno == SYS_forkall
1175 #endif /* SYS_forkall */
1177 || scno == SYS_rfork1
1178 #endif /* SYS_fork1 */
1180 || scno == SYS_rforkall
1181 #endif /* SYS_rforkall */
1183 /* We are returning in the child, fake it. */
1184 tcp->status.PR_WHY = PR_SYSENTRY;
1186 tcp->status.PR_WHY = PR_SYSEXIT;
1189 fprintf(stderr, "syscall: missing entry\n");
1190 tcp->flags |= TCB_INSYSCALL;
1195 if (tcp->status.PR_WHY != PR_SYSEXIT) {
1196 fprintf(stderr, "syscall: missing exit\n");
1197 tcp->flags &= ~TCB_INSYSCALL;
1200 #endif /* USE_PROCFS */
1202 if (!(tcp->flags & TCB_INSYSCALL)) {
1204 fprintf(stderr, "syscall: missing entry\n");
1205 tcp->flags |= TCB_INSYSCALL;
1212 * This happens when a signal handler
1213 * for a signal which interrupted a
1214 * a system call makes another system call.
1216 fprintf(stderr, "syscall: missing exit\n");
1218 tcp->flags &= ~TCB_INSYSCALL;
1224 if (upeek(pid, 4*EAX, &eax) < 0)
1226 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1228 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1231 #elif defined (X86_64)
1232 if (upeek(pid, 8*RAX, &rax) < 0)
1234 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1236 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1239 #elif defined (S390) || defined (S390X)
1240 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1242 if (syscall_mode != -ENOSYS)
1243 syscall_mode = tcp->scno;
1244 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
1246 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1249 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1250 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1251 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1253 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1254 * flag set for the post-execve SIGTRAP to see and reset.
1258 #elif defined (POWERPC)
1259 # define SO_MASK 0x10000000
1260 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1262 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
1264 if (flags & SO_MASK)
1266 #elif defined (M68K)
1267 if (upeek(pid, 4*PT_D0, &d0) < 0)
1269 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1271 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1275 if (upeek(pid, 4*0, (long *)&r0) < 0)
1277 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1279 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
1282 #elif defined (HPPA)
1283 if (upeek(pid, PT_GR28, &r28) < 0)
1286 if (upeek(pid, PT_R10, &r10) < 0)
1288 if (upeek(pid, PT_R8, &r8) < 0)
1290 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1292 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1306 #if defined(S390) || defined(S390X)
1307 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1315 #else /* !S390 && !S390X */
1317 if (eax < 0 && -eax < nerrnos) {
1327 if (rax < 0 && -rax < nerrnos) {
1341 if (err < 0 && -err < nerrnos) {
1369 if (result && (unsigned) -result < nerrnos) {
1374 tcp->u_rval = result;
1377 #else /* !POWERPC */
1379 if (d0 && (unsigned) -d0 < nerrnos) {
1389 if (r0 && (unsigned) -r0 < nerrnos) {
1409 if (regs.r_psr & PSR_C) {
1411 u_error = regs.r_o0;
1414 tcp->u_rval = regs.r_o0;
1419 if (r28 && (unsigned) -r28 < nerrnos) {
1429 /* interpret R0 as return value or error number */
1430 if (r0 && (unsigned) -r0 < nerrnos) {
1440 /* interpret result as return value or error number */
1441 if (r9 && (unsigned) -r9 < nerrnos) {
1449 #endif /* SHMEDIA */
1456 #endif /* POWERPC */
1461 #endif /* S390 || S390X */
1464 /* get error code from user struct */
1465 if (upeek(pid, uoff(u_error), &u_error) < 0)
1467 u_error >>= 24; /* u_error is a char */
1469 /* get system call return value */
1470 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1475 /* Judicious guessing goes a long way. */
1476 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1478 u_error = tcp->status.pr_reg[R_O0];
1481 tcp->u_rval = tcp->status.pr_reg[R_O0];
1486 /* Wanna know how to kill an hour single-stepping? */
1487 if (tcp->status.PR_REG[EFL] & 0x1) {
1489 u_error = tcp->status.PR_REG[EAX];
1492 tcp->u_rval = tcp->status.PR_REG[EAX];
1493 #ifdef HAVE_LONG_LONG
1495 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1496 tcp->status.PR_REG[EAX];
1502 /* Wanna know how to kill an hour single-stepping? */
1503 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1505 u_error = tcp->status.PR_REG[RAX];
1508 tcp->u_rval = tcp->status.PR_REG[RAX];
1513 if (tcp->status.pr_reg[CTX_A3]) {
1515 u_error = tcp->status.pr_reg[CTX_V0];
1518 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1524 if (regs.r_eflags & PSL_C) {
1526 u_error = regs.r_eax;
1528 tcp->u_rval = regs.r_eax;
1530 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1533 #endif /* FREEBSD */
1534 tcp->u_error = u_error;
1539 force_result(tcp, error, rval)
1545 #if defined(S390) || defined(S390X)
1546 gpr2 = error ? -error : rval;
1547 if (upeek(tcp->pid, PT_GPR2, &gpr2) < 0)
1549 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1551 #else /* !S390 && !S390X */
1553 eax = error ? -error : rval;
1554 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1558 rax = error ? -error : rval;
1559 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 4), rax) < 0)
1564 r8 = error ? -error : rval;
1565 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1577 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1578 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1591 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1592 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1596 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1606 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1607 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
1609 #else /* !POWERPC */
1611 d0 = error ? -error : rval;
1612 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1616 r0 = error ? -error : rval;
1617 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), r0) < 0)
1629 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1630 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1634 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1637 regs.r_psr |= PSR_C;
1641 regs.r_psr &= ~PSR_C;
1644 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1648 r28 = error ? -error : rval;
1649 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1653 r0 = error ? -error : rval;
1654 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1658 r9 = error ? -error : rval;
1659 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1661 #endif /* SHMEDIA */
1668 #endif /* POWERPC */
1673 #endif /* S390 || S390X */
1676 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1678 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1686 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1691 regs.r_eflags |= PSL_C;
1695 regs.r_eflags &= ~PSL_C;
1698 if (pwrite(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1702 #endif /* FREEBSD */
1704 /* All branches reach here on success (only). */
1705 tcp->u_error = error;
1710 int syscall_enter(tcp)
1715 #endif /* !USE_PROCFS */
1717 #if defined(S390) || defined(S390X)
1720 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1721 tcp->u_nargs = sysent[tcp->scno].nargs;
1723 tcp->u_nargs = MAX_ARGS;
1724 for (i = 0; i < tcp->u_nargs; i++) {
1725 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
1729 #elif defined (ALPHA)
1732 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1733 tcp->u_nargs = sysent[tcp->scno].nargs;
1735 tcp->u_nargs = MAX_ARGS;
1736 for (i = 0; i < tcp->u_nargs; i++) {
1737 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1738 * for scno somewhere above here!
1740 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1744 #elif defined (IA64)
1747 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1748 /* be backwards compatible with kernel < 2.4.4... */
1750 # define PT_RBS_END PT_AR_BSP
1753 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
1755 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1758 sof = (cfm >> 0) & 0x7f;
1759 sol = (cfm >> 7) & 0x7f;
1760 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1762 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1763 && sysent[tcp->scno].nargs != -1)
1764 tcp->u_nargs = sysent[tcp->scno].nargs;
1766 tcp->u_nargs = MAX_ARGS;
1767 for (i = 0; i < tcp->u_nargs; ++i) {
1768 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1769 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1775 if (/* EBX = out0 */
1776 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1778 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1780 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1782 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1784 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1786 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1789 for (i = 0; i < 6; ++i)
1790 /* truncate away IVE sign-extension */
1791 tcp->u_arg[i] &= 0xffffffff;
1793 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1794 && sysent[tcp->scno].nargs != -1)
1795 tcp->u_nargs = sysent[tcp->scno].nargs;
1800 #elif defined (MIPS)
1805 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1806 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1808 nargs = tcp->u_nargs = MAX_ARGS;
1810 if(upeek(pid, REG_SP, &sp) < 0)
1812 for(i = 0; i < 4; i++) {
1813 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1816 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1817 (char *)(tcp->u_arg + 4));
1819 for(i = 0; i < nargs; i++) {
1820 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1825 #elif defined (POWERPC)
1827 #define PT_ORIG_R3 34
1831 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1832 tcp->u_nargs = sysent[tcp->scno].nargs;
1834 tcp->u_nargs = MAX_ARGS;
1835 for (i = 0; i < tcp->u_nargs; i++) {
1836 if (upeek(pid, (i==0) ?
1837 (sizeof(unsigned long)*PT_ORIG_R3) :
1838 ((i+PT_R3)*sizeof(unsigned long)),
1839 &tcp->u_arg[i]) < 0)
1843 #elif defined (SPARC)
1847 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1848 tcp->u_nargs = sysent[tcp->scno].nargs;
1850 tcp->u_nargs = MAX_ARGS;
1851 for (i = 0; i < tcp->u_nargs; i++)
1852 tcp->u_arg[i] = *((®s.r_o0) + i);
1854 #elif defined (HPPA)
1858 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1859 tcp->u_nargs = sysent[tcp->scno].nargs;
1861 tcp->u_nargs = MAX_ARGS;
1862 for (i = 0; i < tcp->u_nargs; i++) {
1863 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1870 static int syscall_regs[] = {
1871 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
1872 REG_REG0, REG_REG0+1, REG_REG0+2
1875 tcp->u_nargs = sysent[tcp->scno].nargs;
1876 for (i = 0; i < tcp->u_nargs; i++) {
1877 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
1881 #elif defined(SHMEDIA)
1884 /* Registers used by SH5 Linux system calls for parameters */
1885 static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
1888 * TODO: should also check that the number of arguments encoded
1889 * in the trap number matches the number strace expects.
1892 assert(sysent[tcp->scno].nargs <
1893 sizeof(syscall_regs)/sizeof(syscall_regs[0]));
1896 tcp->u_nargs = sysent[tcp->scno].nargs;
1897 for (i = 0; i < tcp->u_nargs; i++) {
1898 if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1903 #elif defined(X86_64)
1906 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
1907 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
1908 {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */
1911 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1912 tcp->u_nargs = sysent[tcp->scno].nargs;
1914 tcp->u_nargs = MAX_ARGS;
1915 for (i = 0; i < tcp->u_nargs; i++) {
1916 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
1920 #else /* Other architecture (like i386) (32bits specific) */
1923 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1924 tcp->u_nargs = sysent[tcp->scno].nargs;
1926 tcp->u_nargs = MAX_ARGS;
1927 for (i = 0; i < tcp->u_nargs; i++) {
1928 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1937 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1938 tcp->u_nargs = sysent[tcp->scno].nargs;
1940 tcp->u_nargs = MAX_ARGS;
1941 for (i = 0; i < tcp->u_nargs; i++) {
1944 if (upeek(pid, uoff(u_arg[0]) +
1945 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1953 * SGI is broken: even though it has pr_sysarg, it doesn't
1954 * set them on system call entry. Get a clue.
1956 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1957 tcp->u_nargs = sysent[tcp->scno].nargs;
1959 tcp->u_nargs = tcp->status.pr_nsysarg;
1960 if (tcp->u_nargs > 4) {
1961 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1962 4*sizeof(tcp->u_arg[0]));
1963 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1964 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1967 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1968 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1972 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1974 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1975 tcp->u_nargs = sysent[tcp->scno].nargs;
1977 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1978 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1979 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1980 #elif defined (HAVE_PR_SYSCALL)
1981 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1982 tcp->u_nargs = sysent[tcp->scno].nargs;
1984 tcp->u_nargs = tcp->status.pr_nsysarg;
1987 for (i = 0; i < tcp->u_nargs; i++)
1988 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1990 #elif defined (I386)
1991 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1992 tcp->u_nargs = sysent[tcp->scno].nargs;
1995 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1996 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1998 I DONT KNOW WHAT TO DO
1999 #endif /* !HAVE_PR_SYSCALL */
2002 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2003 sysent[tcp->scno].nargs > tcp->status.val)
2004 tcp->u_nargs = sysent[tcp->scno].nargs;
2006 tcp->u_nargs = tcp->status.val;
2007 if (tcp->u_nargs < 0)
2009 if (tcp->u_nargs > MAX_ARGS)
2010 tcp->u_nargs = MAX_ARGS;
2011 switch(regs.r_eax) {
2013 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2014 regs.r_esp + sizeof(int) + sizeof(quad_t));
2017 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2018 regs.r_esp + 2 * sizeof(int));
2021 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2022 regs.r_esp + sizeof(int));
2025 #endif /* FREEBSD */
2037 /* Measure the exit time as early as possible to avoid errors. */
2038 if (dtime && (tcp->flags & TCB_INSYSCALL))
2039 gettimeofday(&tv, NULL);
2041 res = get_scno(tcp);
2045 res = syscall_fixup(tcp);
2049 if (tcp->flags & TCB_INSYSCALL) {
2051 res = get_error(tcp);
2055 internal_syscall(tcp);
2056 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2057 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2058 tcp->flags &= ~TCB_INSYSCALL;
2062 if (tcp->flags & TCB_REPRINT) {
2065 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2066 tprintf("syscall_%lu", tcp->scno);
2068 tprintf("%s", sysent[tcp->scno].sys_name);
2069 tprintf(" resumed> ");
2072 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
2073 call_count[tcp->scno]++;
2075 error_count[tcp->scno]++;
2076 tv_sub(&tv, &tv, &tcp->etime);
2078 if (tv_cmp(&tv, &tcp->dtime) > 0) {
2079 static struct timeval one_tick =
2080 { 0, 1000000 / HZ };
2082 if (tv_nz(&tcp->dtime))
2084 else if (tv_cmp(&tv, &one_tick) > 0) {
2085 if (tv_cmp(&shortest, &one_tick) < 0)
2092 if (tv_cmp(&tv, &shortest) < 0)
2094 tv_add(&tv_count[tcp->scno],
2095 &tv_count[tcp->scno], &tv);
2096 tcp->flags &= ~TCB_INSYSCALL;
2100 if (tcp->scno >= nsyscalls || tcp->scno < 0
2101 || (qual_flags[tcp->scno] & QUAL_RAW))
2102 sys_res = printargs(tcp);
2104 if (not_failing_only && tcp->u_error)
2105 return 0; /* ignore failed syscalls */
2106 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2108 u_error = tcp->u_error;
2111 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2112 qual_flags[tcp->scno] & QUAL_RAW) {
2114 tprintf("= -1 (errno %ld)", u_error);
2116 tprintf("= %#lx", tcp->u_rval);
2118 else if (!(sys_res & RVAL_NONE) && u_error) {
2122 tprintf("= ? ERESTARTSYS (To be restarted)");
2124 case ERESTARTNOINTR:
2125 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2127 case ERESTARTNOHAND:
2128 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2134 tprintf("E??? (errno %ld)", u_error);
2135 else if (u_error < nerrnos)
2136 tprintf("%s (%s)", errnoent[u_error],
2139 tprintf("ERRNO_%ld (%s)", u_error,
2145 if (sys_res & RVAL_NONE)
2148 switch (sys_res & RVAL_MASK) {
2150 tprintf("= %#lx", tcp->u_rval);
2153 tprintf("= %#lo", tcp->u_rval);
2156 tprintf("= %lu", tcp->u_rval);
2159 tprintf("= %ld", tcp->u_rval);
2161 #ifdef HAVE_LONG_LONG
2163 tprintf("= %#llx", tcp->u_lrval);
2166 tprintf("= %#llo", tcp->u_lrval);
2168 case RVAL_LUDECIMAL:
2169 tprintf("= %llu", tcp->u_lrval);
2172 tprintf("= %lld", tcp->u_lrval);
2177 "invalid rval format\n");
2181 if ((sys_res & RVAL_STR) && tcp->auxstr)
2182 tprintf(" (%s)", tcp->auxstr);
2185 tv_sub(&tv, &tv, &tcp->etime);
2186 tprintf(" <%ld.%06ld>",
2187 (long) tv.tv_sec, (long) tv.tv_usec);
2192 if (fflush(tcp->outf) == EOF)
2194 tcp->flags &= ~TCB_INSYSCALL;
2198 /* Entering system call */
2199 res = syscall_enter(tcp);
2203 switch (tcp->scno + NR_SYSCALL_BASE) {
2205 #if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
2206 case SYS_socketcall:
2207 decode_subcall(tcp, SYS_socket_subcall,
2208 SYS_socket_nsubcalls, deref_style);
2211 decode_subcall(tcp, SYS_ipc_subcall,
2212 SYS_ipc_nsubcalls, shift_style);
2214 #endif /* !ALPHA && !MIPS && !SPARC && !HPPA && !X86_64 */
2216 case SYS_socketcall:
2217 sparc_socket_decode (tcp);
2222 #ifdef SYS_pgrpsys_subcall
2224 decode_subcall(tcp, SYS_pgrpsys_subcall,
2225 SYS_pgrpsys_nsubcalls, shift_style);
2227 #endif /* SYS_pgrpsys_subcall */
2228 #ifdef SYS_sigcall_subcall
2230 decode_subcall(tcp, SYS_sigcall_subcall,
2231 SYS_sigcall_nsubcalls, mask_style);
2233 #endif /* SYS_sigcall_subcall */
2235 decode_subcall(tcp, SYS_msgsys_subcall,
2236 SYS_msgsys_nsubcalls, shift_style);
2239 decode_subcall(tcp, SYS_shmsys_subcall,
2240 SYS_shmsys_nsubcalls, shift_style);
2243 decode_subcall(tcp, SYS_semsys_subcall,
2244 SYS_semsys_nsubcalls, shift_style);
2248 decode_subcall(tcp, SYS_utssys_subcall,
2249 SYS_utssys_nsubcalls, shift_style);
2253 decode_subcall(tcp, SYS_sysfs_subcall,
2254 SYS_sysfs_nsubcalls, shift_style);
2257 decode_subcall(tcp, SYS_spcall_subcall,
2258 SYS_spcall_nsubcalls, shift_style);
2260 #ifdef SYS_context_subcall
2262 decode_subcall(tcp, SYS_context_subcall,
2263 SYS_context_nsubcalls, shift_style);
2265 #endif /* SYS_context_subcall */
2266 #ifdef SYS_door_subcall
2268 decode_subcall(tcp, SYS_door_subcall,
2269 SYS_door_nsubcalls, door_style);
2271 #endif /* SYS_door_subcall */
2272 #ifdef SYS_kaio_subcall
2274 decode_subcall(tcp, SYS_kaio_subcall,
2275 SYS_kaio_nsubcalls, shift_style);
2283 decode_subcall(tcp, 0, 0, table_style);
2288 decode_subcall(tcp, SYS_semsys_subcall,
2289 SYS_semsys_nsubcalls, shift_style);
2292 decode_subcall(tcp, SYS_msgsys_subcall,
2293 SYS_msgsys_nsubcalls, shift_style);
2296 decode_subcall(tcp, SYS_shmsys_subcall,
2297 SYS_shmsys_nsubcalls, shift_style);
2302 internal_syscall(tcp);
2303 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2304 tcp->flags |= TCB_INSYSCALL;
2309 gettimeofday(&tcp->etime, NULL);
2310 tcp->flags |= TCB_INSYSCALL;
2315 tcp->flags &= ~TCB_REPRINT;
2317 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2318 tprintf("syscall_%lu(", tcp->scno);
2320 tprintf("%s(", sysent[tcp->scno].sys_name);
2321 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2322 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2323 sys_res = printargs(tcp);
2325 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2326 if (fflush(tcp->outf) == EOF)
2328 tcp->flags |= TCB_INSYSCALL;
2329 /* Measure the entrance time as late as possible to avoid errors. */
2331 gettimeofday(&tcp->etime, NULL);
2339 if (entering(tcp)) {
2342 for (i = 0; i < tcp->u_nargs; i++)
2343 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2357 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
2361 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2367 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2373 val = tcp->status.PR_REG[R_O1];
2376 val = tcp->status.PR_REG[EDX];
2379 val = tcp->status.PR_REG[RDX];
2382 val = tcp->status.PR_REG[CTX_V1];
2387 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
2394 * Apparently, indirect system calls have already be converted by ptrace(2),
2395 * so if you see "indir" this program has gone astray.
2403 if (entering(tcp)) {
2404 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2405 fprintf(stderr, "Bogus syscall: %u\n", scno);
2408 nargs = sysent[scno].nargs;
2409 tprintf("%s", sysent[scno].sys_name);
2410 for (i = 0; i < nargs; i++)
2411 tprintf(", %#lx", tcp->u_arg[i+1]);
2421 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2429 return strcmp(sysent[*((int *) a)].sys_name,
2430 sysent[*((int *) b)].sys_name);
2438 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2440 return (m < n) ? 1 : (m > n) ? -1 : 0;
2443 static int (*sortfun)();
2444 static struct timeval overhead = { -1, -1 };
2450 if (strcmp(sortby, "time") == 0)
2452 else if (strcmp(sortby, "calls") == 0)
2453 sortfun = count_cmp;
2454 else if (strcmp(sortby, "name") == 0)
2455 sortfun = syscall_cmp;
2456 else if (strcmp(sortby, "nothing") == 0)
2459 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2464 void set_overhead(n)
2467 overhead.tv_sec = n / 1000000;
2468 overhead.tv_usec = n % 1000000;
2476 int call_cum, error_cum;
2477 struct timeval tv_cum, dtv;
2479 char *dashes = "-------------------------";
2482 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2483 if (overhead.tv_sec == -1) {
2484 tv_mul(&overhead, &shortest, 8);
2485 tv_div(&overhead, &overhead, 10);
2487 for (i = 0; i < nsyscalls; i++) {
2488 sorted_count[i] = i;
2489 if (call_count[i] == 0)
2491 tv_mul(&dtv, &overhead, call_count[i]);
2492 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2493 call_cum += call_count[i];
2494 error_cum += error_count[i];
2495 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2498 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2499 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2500 "% time", "seconds", "usecs/call",
2501 "calls", "errors", "syscall");
2502 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2503 dashes, dashes, dashes, dashes, dashes, dashes);
2504 for (i = 0; i < nsyscalls; i++) {
2505 j = sorted_count[i];
2506 if (call_count[j] == 0)
2508 tv_div(&dtv, &tv_count[j], call_count[j]);
2510 sprintf(error_str, "%d", error_count[j]);
2512 error_str[0] = '\0';
2513 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2514 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2515 percent, (long) tv_count[j].tv_sec,
2516 (long) tv_count[j].tv_usec,
2517 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2518 call_count[j], error_str, sysent[j].sys_name);
2520 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2521 dashes, dashes, dashes, dashes, dashes, dashes);
2523 sprintf(error_str, "%d", error_cum);
2525 error_str[0] = '\0';
2526 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2527 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2528 call_cum, error_str, "total");