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;
698 static struct pt_regs regs;
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)
943 * Read complete register set in one go.
945 if (ptrace(PTRACE_GETREGS, pid, NULL, (void *)®s) == -1)
949 * We only need to grab the syscall number on syscall entry.
951 if (regs.ARM_ip == 0) {
953 * Note: we only deal with only 32-bit CPUs here.
955 if (regs.ARM_cpsr & 0x20) {
957 * Get the Thumb-mode system call number
962 * Get the ARM-mode system call number
965 scno = ptrace(PTRACE_PEEKTEXT, pid, (void *)(regs.ARM_pc - 4), NULL);
969 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
970 tcp->flags &= ~TCB_WAITEXECVE;
974 if ((scno & 0x0ff00000) != 0x0f900000) {
975 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
981 * Fixup the syscall number
986 if (tcp->flags & TCB_INSYSCALL) {
987 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
988 tcp->flags &= ~TCB_INSYSCALL;
991 if (!(tcp->flags & TCB_INSYSCALL)) {
992 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
993 tcp->flags |= TCB_INSYSCALL;
997 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
1000 if (upeek(pid, REG_A3, &a3) < 0)
1003 if(!(tcp->flags & TCB_INSYSCALL)) {
1004 if (upeek(pid, REG_V0, &scno) < 0)
1007 if (scno < 0 || scno > nsyscalls) {
1008 if(a3 == 0 || a3 == -1) {
1010 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
1015 if (upeek(pid, REG_V0, &r2) < 0)
1018 #elif defined (ALPHA)
1019 if (upeek(pid, REG_A3, &a3) < 0)
1022 if (!(tcp->flags & TCB_INSYSCALL)) {
1023 if (upeek(pid, REG_R0, &scno) < 0)
1026 /* Check if we return from execve. */
1027 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1028 tcp->flags &= ~TCB_WAITEXECVE;
1033 * Do some sanity checks to figure out if it's
1034 * really a syscall entry
1036 if (scno < 0 || scno > nsyscalls) {
1037 if (a3 == 0 || a3 == -1) {
1039 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
1045 if (upeek(pid, REG_R0, &r0) < 0)
1048 #elif defined (SPARC)
1049 /* Everything we need is in the current register set. */
1050 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
1053 /* If we are entering, then disassemble the syscall trap. */
1054 if (!(tcp->flags & TCB_INSYSCALL)) {
1055 /* Retrieve the syscall trap instruction. */
1057 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
1061 /* Disassemble the trap to see what personality to use. */
1064 /* Linux/SPARC syscall trap. */
1068 /* Linux/SPARC64 syscall trap. */
1069 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
1072 /* SunOS syscall trap. (pers 1) */
1073 fprintf(stderr,"syscall: SunOS no support\n");
1076 /* Solaris 2.x syscall trap. (per 2) */
1080 /* NetBSD/FreeBSD syscall trap. */
1081 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1084 /* Solaris 2.x gettimeofday */
1088 /* Unknown syscall trap. */
1089 if(tcp->flags & TCB_WAITEXECVE) {
1090 tcp->flags &= ~TCB_WAITEXECVE;
1093 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
1097 /* Extract the system call number from the registers. */
1098 if (trap == 0x91d02027)
1104 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
1108 if (upeek(pid, PT_GR20, &scno) < 0)
1110 if (!(tcp->flags & TCB_INSYSCALL)) {
1111 /* Check if we return from execve. */
1112 if ((tcp->flags & TCB_WAITEXECVE)) {
1113 tcp->flags &= ~TCB_WAITEXECVE;
1119 * In the new syscall ABI, the system call number is in R3.
1121 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1125 /* Odd as it may seem, a glibc bug has been known to cause
1126 glibc to issue bogus negative syscall numbers. So for
1127 our purposes, make strace print what it *should* have been */
1128 long correct_scno = (scno & 0xff);
1131 "Detected glibc bug: bogus system call number = %ld, "
1132 "correcting to %ld\n",
1135 scno = correct_scno;
1139 if (!(tcp->flags & TCB_INSYSCALL)) {
1140 /* Check if we return from execve. */
1141 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1142 tcp->flags &= ~TCB_WAITEXECVE;
1146 #elif defined(SHMEDIA)
1147 if (upeek(pid, REG_SYSCALL, &scno) < 0)
1151 if (!(tcp->flags & TCB_INSYSCALL)) {
1152 /* Check if we return from execve. */
1153 if (tcp->flags & TCB_WAITEXECVE) {
1154 tcp->flags &= ~TCB_WAITEXECVE;
1158 #endif /* SHMEDIA */
1161 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1164 /* new syscall ABI returns result in R0 */
1165 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1167 #elif defined(SHMEDIA)
1168 /* ABI defines result returned in r9 */
1169 if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
1174 #ifdef HAVE_PR_SYSCALL
1175 scno = tcp->status.PR_SYSCALL;
1176 #else /* !HAVE_PR_SYSCALL */
1178 scno = tcp->status.PR_WHAT;
1180 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1184 switch (regs.r_eax) {
1187 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1193 #endif /* FREEBSD */
1194 #endif /* !HAVE_PR_SYSCALL */
1195 #endif /* USE_PROCFS */
1196 if (!(tcp->flags & TCB_INSYSCALL))
1208 #else /* USE_PROCFS */
1209 int scno = tcp->scno;
1211 if (!(tcp->flags & TCB_INSYSCALL)) {
1212 if (tcp->status.PR_WHY != PR_SYSENTRY) {
1216 || scno == SYS_vfork
1217 #endif /* SYS_vfork */
1219 || scno == SYS_fork1
1220 #endif /* SYS_fork1 */
1222 || scno == SYS_forkall
1223 #endif /* SYS_forkall */
1225 || scno == SYS_rfork1
1226 #endif /* SYS_fork1 */
1228 || scno == SYS_rforkall
1229 #endif /* SYS_rforkall */
1231 /* We are returning in the child, fake it. */
1232 tcp->status.PR_WHY = PR_SYSENTRY;
1234 tcp->status.PR_WHY = PR_SYSEXIT;
1237 fprintf(stderr, "syscall: missing entry\n");
1238 tcp->flags |= TCB_INSYSCALL;
1243 if (tcp->status.PR_WHY != PR_SYSEXIT) {
1244 fprintf(stderr, "syscall: missing exit\n");
1245 tcp->flags &= ~TCB_INSYSCALL;
1248 #endif /* USE_PROCFS */
1250 if (!(tcp->flags & TCB_INSYSCALL)) {
1252 fprintf(stderr, "syscall: missing entry\n");
1253 tcp->flags |= TCB_INSYSCALL;
1260 * This happens when a signal handler
1261 * for a signal which interrupted a
1262 * a system call makes another system call.
1264 fprintf(stderr, "syscall: missing exit\n");
1266 tcp->flags &= ~TCB_INSYSCALL;
1272 if (upeek(pid, 4*EAX, &eax) < 0)
1274 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1276 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1279 #elif defined (X86_64)
1280 if (upeek(pid, 8*RAX, &rax) < 0)
1282 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1284 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1287 #elif defined (S390) || defined (S390X)
1288 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1290 if (syscall_mode != -ENOSYS)
1291 syscall_mode = tcp->scno;
1292 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
1294 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1297 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1298 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1299 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1301 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1302 * flag set for the post-execve SIGTRAP to see and reset.
1306 #elif defined (POWERPC)
1307 # define SO_MASK 0x10000000
1308 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1310 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
1312 if (flags & SO_MASK)
1314 #elif defined (M68K)
1315 if (upeek(pid, 4*PT_D0, &d0) < 0)
1317 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1319 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1326 #elif defined (HPPA)
1327 if (upeek(pid, PT_GR28, &r28) < 0)
1330 if (upeek(pid, PT_R10, &r10) < 0)
1332 if (upeek(pid, PT_R8, &r8) < 0)
1334 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1336 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1350 #if defined(S390) || defined(S390X)
1351 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1359 #else /* !S390 && !S390X */
1361 if (eax < 0 && -eax < nerrnos) {
1371 if (rax < 0 && -rax < nerrnos) {
1385 if (err < 0 && -err < nerrnos) {
1413 if (result && (unsigned) -result < nerrnos) {
1418 tcp->u_rval = result;
1421 #else /* !POWERPC */
1423 if (d0 && (unsigned) -d0 < nerrnos) {
1433 if (regs.ARM_r0 && (unsigned) -regs.ARM_r0 < nerrnos) {
1435 u_error = -regs.ARM_r0;
1438 tcp->u_rval = regs.ARM_r0;
1453 if (regs.r_psr & PSR_C) {
1455 u_error = regs.r_o0;
1458 tcp->u_rval = regs.r_o0;
1463 if (r28 && (unsigned) -r28 < nerrnos) {
1473 /* interpret R0 as return value or error number */
1474 if (r0 && (unsigned) -r0 < nerrnos) {
1484 /* interpret result as return value or error number */
1485 if (r9 && (unsigned) -r9 < nerrnos) {
1493 #endif /* SHMEDIA */
1500 #endif /* POWERPC */
1505 #endif /* S390 || S390X */
1508 /* get error code from user struct */
1509 if (upeek(pid, uoff(u_error), &u_error) < 0)
1511 u_error >>= 24; /* u_error is a char */
1513 /* get system call return value */
1514 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1519 /* Judicious guessing goes a long way. */
1520 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1522 u_error = tcp->status.pr_reg[R_O0];
1525 tcp->u_rval = tcp->status.pr_reg[R_O0];
1530 /* Wanna know how to kill an hour single-stepping? */
1531 if (tcp->status.PR_REG[EFL] & 0x1) {
1533 u_error = tcp->status.PR_REG[EAX];
1536 tcp->u_rval = tcp->status.PR_REG[EAX];
1537 #ifdef HAVE_LONG_LONG
1539 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1540 tcp->status.PR_REG[EAX];
1546 /* Wanna know how to kill an hour single-stepping? */
1547 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1549 u_error = tcp->status.PR_REG[RAX];
1552 tcp->u_rval = tcp->status.PR_REG[RAX];
1557 if (tcp->status.pr_reg[CTX_A3]) {
1559 u_error = tcp->status.pr_reg[CTX_V0];
1562 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1568 if (regs.r_eflags & PSL_C) {
1570 u_error = regs.r_eax;
1572 tcp->u_rval = regs.r_eax;
1574 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1577 #endif /* FREEBSD */
1578 tcp->u_error = u_error;
1583 force_result(tcp, error, rval)
1589 #if defined(S390) || defined(S390X)
1590 gpr2 = error ? -error : rval;
1591 if (upeek(tcp->pid, PT_GPR2, &gpr2) < 0)
1593 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1595 #else /* !S390 && !S390X */
1597 eax = error ? -error : rval;
1598 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1602 rax = error ? -error : rval;
1603 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 4), rax) < 0)
1608 r8 = error ? -error : rval;
1609 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1621 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1622 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1635 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1636 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1640 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1650 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1651 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
1653 #else /* !POWERPC */
1655 d0 = error ? -error : rval;
1656 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1660 r0 = error ? -error : rval;
1661 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), r0) < 0)
1673 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1674 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1678 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1681 regs.r_psr |= PSR_C;
1685 regs.r_psr &= ~PSR_C;
1688 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1692 r28 = error ? -error : rval;
1693 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1697 r0 = error ? -error : rval;
1698 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1702 r9 = error ? -error : rval;
1703 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1705 #endif /* SHMEDIA */
1712 #endif /* POWERPC */
1717 #endif /* S390 || S390X */
1720 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1722 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1730 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1735 regs.r_eflags |= PSL_C;
1739 regs.r_eflags &= ~PSL_C;
1742 if (pwrite(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1746 #endif /* FREEBSD */
1748 /* All branches reach here on success (only). */
1749 tcp->u_error = error;
1754 int syscall_enter(tcp)
1759 #endif /* !USE_PROCFS */
1761 #if defined(S390) || defined(S390X)
1764 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1765 tcp->u_nargs = sysent[tcp->scno].nargs;
1767 tcp->u_nargs = MAX_ARGS;
1768 for (i = 0; i < tcp->u_nargs; i++) {
1769 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
1773 #elif defined (ALPHA)
1776 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1777 tcp->u_nargs = sysent[tcp->scno].nargs;
1779 tcp->u_nargs = MAX_ARGS;
1780 for (i = 0; i < tcp->u_nargs; i++) {
1781 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1782 * for scno somewhere above here!
1784 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1788 #elif defined (IA64)
1791 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1792 /* be backwards compatible with kernel < 2.4.4... */
1794 # define PT_RBS_END PT_AR_BSP
1797 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
1799 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1802 sof = (cfm >> 0) & 0x7f;
1803 sol = (cfm >> 7) & 0x7f;
1804 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1806 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1807 && sysent[tcp->scno].nargs != -1)
1808 tcp->u_nargs = sysent[tcp->scno].nargs;
1810 tcp->u_nargs = MAX_ARGS;
1811 for (i = 0; i < tcp->u_nargs; ++i) {
1812 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1813 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1819 if (/* EBX = out0 */
1820 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1822 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1824 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1826 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1828 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1830 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1833 for (i = 0; i < 6; ++i)
1834 /* truncate away IVE sign-extension */
1835 tcp->u_arg[i] &= 0xffffffff;
1837 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1838 && sysent[tcp->scno].nargs != -1)
1839 tcp->u_nargs = sysent[tcp->scno].nargs;
1844 #elif defined (MIPS)
1849 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1850 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1852 nargs = tcp->u_nargs = MAX_ARGS;
1854 if(upeek(pid, REG_SP, &sp) < 0)
1856 for(i = 0; i < 4; i++) {
1857 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1860 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1861 (char *)(tcp->u_arg + 4));
1863 for(i = 0; i < nargs; i++) {
1864 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1869 #elif defined (POWERPC)
1871 #define PT_ORIG_R3 34
1875 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1876 tcp->u_nargs = sysent[tcp->scno].nargs;
1878 tcp->u_nargs = MAX_ARGS;
1879 for (i = 0; i < tcp->u_nargs; i++) {
1880 if (upeek(pid, (i==0) ?
1881 (sizeof(unsigned long)*PT_ORIG_R3) :
1882 ((i+PT_R3)*sizeof(unsigned long)),
1883 &tcp->u_arg[i]) < 0)
1887 #elif defined (SPARC)
1891 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1892 tcp->u_nargs = sysent[tcp->scno].nargs;
1894 tcp->u_nargs = MAX_ARGS;
1895 for (i = 0; i < tcp->u_nargs; i++)
1896 tcp->u_arg[i] = *((®s.r_o0) + i);
1898 #elif defined (HPPA)
1902 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1903 tcp->u_nargs = sysent[tcp->scno].nargs;
1905 tcp->u_nargs = MAX_ARGS;
1906 for (i = 0; i < tcp->u_nargs; i++) {
1907 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1915 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1916 tcp->u_nargs = sysent[tcp->scno].nargs;
1918 tcp->u_nargs = MAX_ARGS;
1919 for (i = 0; i < tcp->u_nargs; i++)
1920 tcp->u_arg[i] = regs.uregs[i];
1925 static int syscall_regs[] = {
1926 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
1927 REG_REG0, REG_REG0+1, REG_REG0+2
1930 tcp->u_nargs = sysent[tcp->scno].nargs;
1931 for (i = 0; i < tcp->u_nargs; i++) {
1932 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
1936 #elif defined(SHMEDIA)
1939 /* Registers used by SH5 Linux system calls for parameters */
1940 static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
1943 * TODO: should also check that the number of arguments encoded
1944 * in the trap number matches the number strace expects.
1947 assert(sysent[tcp->scno].nargs <
1948 sizeof(syscall_regs)/sizeof(syscall_regs[0]));
1951 tcp->u_nargs = sysent[tcp->scno].nargs;
1952 for (i = 0; i < tcp->u_nargs; i++) {
1953 if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1958 #elif defined(X86_64)
1961 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
1962 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
1963 {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */
1966 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1967 tcp->u_nargs = sysent[tcp->scno].nargs;
1969 tcp->u_nargs = MAX_ARGS;
1970 for (i = 0; i < tcp->u_nargs; i++) {
1971 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
1975 #else /* Other architecture (like i386) (32bits specific) */
1978 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1979 tcp->u_nargs = sysent[tcp->scno].nargs;
1981 tcp->u_nargs = MAX_ARGS;
1982 for (i = 0; i < tcp->u_nargs; i++) {
1983 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1992 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1993 tcp->u_nargs = sysent[tcp->scno].nargs;
1995 tcp->u_nargs = MAX_ARGS;
1996 for (i = 0; i < tcp->u_nargs; i++) {
1999 if (upeek(pid, uoff(u_arg[0]) +
2000 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
2008 * SGI is broken: even though it has pr_sysarg, it doesn't
2009 * set them on system call entry. Get a clue.
2011 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2012 tcp->u_nargs = sysent[tcp->scno].nargs;
2014 tcp->u_nargs = tcp->status.pr_nsysarg;
2015 if (tcp->u_nargs > 4) {
2016 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2017 4*sizeof(tcp->u_arg[0]));
2018 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
2019 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
2022 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2023 tcp->u_nargs*sizeof(tcp->u_arg[0]));
2027 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
2029 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2030 tcp->u_nargs = sysent[tcp->scno].nargs;
2032 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
2033 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2034 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2035 #elif defined (HAVE_PR_SYSCALL)
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 = tcp->status.pr_nsysarg;
2042 for (i = 0; i < tcp->u_nargs; i++)
2043 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
2045 #elif defined (I386)
2046 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2047 tcp->u_nargs = sysent[tcp->scno].nargs;
2050 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2051 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2053 I DONT KNOW WHAT TO DO
2054 #endif /* !HAVE_PR_SYSCALL */
2057 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2058 sysent[tcp->scno].nargs > tcp->status.val)
2059 tcp->u_nargs = sysent[tcp->scno].nargs;
2061 tcp->u_nargs = tcp->status.val;
2062 if (tcp->u_nargs < 0)
2064 if (tcp->u_nargs > MAX_ARGS)
2065 tcp->u_nargs = MAX_ARGS;
2066 switch(regs.r_eax) {
2068 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2069 regs.r_esp + sizeof(int) + sizeof(quad_t));
2072 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2073 regs.r_esp + 2 * sizeof(int));
2076 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2077 regs.r_esp + sizeof(int));
2080 #endif /* FREEBSD */
2092 /* Measure the exit time as early as possible to avoid errors. */
2093 if (dtime && (tcp->flags & TCB_INSYSCALL))
2094 gettimeofday(&tv, NULL);
2096 res = get_scno(tcp);
2100 res = syscall_fixup(tcp);
2104 if (tcp->flags & TCB_INSYSCALL) {
2106 res = get_error(tcp);
2110 internal_syscall(tcp);
2111 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2112 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2113 tcp->flags &= ~TCB_INSYSCALL;
2117 if (tcp->flags & TCB_REPRINT) {
2120 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2121 tprintf("syscall_%lu", tcp->scno);
2123 tprintf("%s", sysent[tcp->scno].sys_name);
2124 tprintf(" resumed> ");
2127 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
2128 call_count[tcp->scno]++;
2130 error_count[tcp->scno]++;
2131 tv_sub(&tv, &tv, &tcp->etime);
2133 if (tv_cmp(&tv, &tcp->dtime) > 0) {
2134 static struct timeval one_tick =
2135 { 0, 1000000 / HZ };
2137 if (tv_nz(&tcp->dtime))
2139 else if (tv_cmp(&tv, &one_tick) > 0) {
2140 if (tv_cmp(&shortest, &one_tick) < 0)
2147 if (tv_cmp(&tv, &shortest) < 0)
2149 tv_add(&tv_count[tcp->scno],
2150 &tv_count[tcp->scno], &tv);
2151 tcp->flags &= ~TCB_INSYSCALL;
2155 if (tcp->scno >= nsyscalls || tcp->scno < 0
2156 || (qual_flags[tcp->scno] & QUAL_RAW))
2157 sys_res = printargs(tcp);
2159 if (not_failing_only && tcp->u_error)
2160 return 0; /* ignore failed syscalls */
2161 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2163 u_error = tcp->u_error;
2166 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2167 qual_flags[tcp->scno] & QUAL_RAW) {
2169 tprintf("= -1 (errno %ld)", u_error);
2171 tprintf("= %#lx", tcp->u_rval);
2173 else if (!(sys_res & RVAL_NONE) && u_error) {
2177 tprintf("= ? ERESTARTSYS (To be restarted)");
2179 case ERESTARTNOINTR:
2180 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2182 case ERESTARTNOHAND:
2183 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2189 tprintf("E??? (errno %ld)", u_error);
2190 else if (u_error < nerrnos)
2191 tprintf("%s (%s)", errnoent[u_error],
2194 tprintf("ERRNO_%ld (%s)", u_error,
2200 if (sys_res & RVAL_NONE)
2203 switch (sys_res & RVAL_MASK) {
2205 tprintf("= %#lx", tcp->u_rval);
2208 tprintf("= %#lo", tcp->u_rval);
2211 tprintf("= %lu", tcp->u_rval);
2214 tprintf("= %ld", tcp->u_rval);
2216 #ifdef HAVE_LONG_LONG
2218 tprintf("= %#llx", tcp->u_lrval);
2221 tprintf("= %#llo", tcp->u_lrval);
2223 case RVAL_LUDECIMAL:
2224 tprintf("= %llu", tcp->u_lrval);
2227 tprintf("= %lld", tcp->u_lrval);
2232 "invalid rval format\n");
2236 if ((sys_res & RVAL_STR) && tcp->auxstr)
2237 tprintf(" (%s)", tcp->auxstr);
2240 tv_sub(&tv, &tv, &tcp->etime);
2241 tprintf(" <%ld.%06ld>",
2242 (long) tv.tv_sec, (long) tv.tv_usec);
2247 if (fflush(tcp->outf) == EOF)
2249 tcp->flags &= ~TCB_INSYSCALL;
2253 /* Entering system call */
2254 res = syscall_enter(tcp);
2258 switch (tcp->scno + NR_SYSCALL_BASE) {
2260 #if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
2261 case SYS_socketcall:
2262 decode_subcall(tcp, SYS_socket_subcall,
2263 SYS_socket_nsubcalls, deref_style);
2266 decode_subcall(tcp, SYS_ipc_subcall,
2267 SYS_ipc_nsubcalls, shift_style);
2269 #endif /* !ALPHA && !MIPS && !SPARC && !HPPA && !X86_64 */
2271 case SYS_socketcall:
2272 sparc_socket_decode (tcp);
2277 #ifdef SYS_pgrpsys_subcall
2279 decode_subcall(tcp, SYS_pgrpsys_subcall,
2280 SYS_pgrpsys_nsubcalls, shift_style);
2282 #endif /* SYS_pgrpsys_subcall */
2283 #ifdef SYS_sigcall_subcall
2285 decode_subcall(tcp, SYS_sigcall_subcall,
2286 SYS_sigcall_nsubcalls, mask_style);
2288 #endif /* SYS_sigcall_subcall */
2290 decode_subcall(tcp, SYS_msgsys_subcall,
2291 SYS_msgsys_nsubcalls, shift_style);
2294 decode_subcall(tcp, SYS_shmsys_subcall,
2295 SYS_shmsys_nsubcalls, shift_style);
2298 decode_subcall(tcp, SYS_semsys_subcall,
2299 SYS_semsys_nsubcalls, shift_style);
2303 decode_subcall(tcp, SYS_utssys_subcall,
2304 SYS_utssys_nsubcalls, shift_style);
2308 decode_subcall(tcp, SYS_sysfs_subcall,
2309 SYS_sysfs_nsubcalls, shift_style);
2312 decode_subcall(tcp, SYS_spcall_subcall,
2313 SYS_spcall_nsubcalls, shift_style);
2315 #ifdef SYS_context_subcall
2317 decode_subcall(tcp, SYS_context_subcall,
2318 SYS_context_nsubcalls, shift_style);
2320 #endif /* SYS_context_subcall */
2321 #ifdef SYS_door_subcall
2323 decode_subcall(tcp, SYS_door_subcall,
2324 SYS_door_nsubcalls, door_style);
2326 #endif /* SYS_door_subcall */
2327 #ifdef SYS_kaio_subcall
2329 decode_subcall(tcp, SYS_kaio_subcall,
2330 SYS_kaio_nsubcalls, shift_style);
2338 decode_subcall(tcp, 0, 0, table_style);
2343 decode_subcall(tcp, SYS_semsys_subcall,
2344 SYS_semsys_nsubcalls, shift_style);
2347 decode_subcall(tcp, SYS_msgsys_subcall,
2348 SYS_msgsys_nsubcalls, shift_style);
2351 decode_subcall(tcp, SYS_shmsys_subcall,
2352 SYS_shmsys_nsubcalls, shift_style);
2357 internal_syscall(tcp);
2358 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2359 tcp->flags |= TCB_INSYSCALL;
2364 gettimeofday(&tcp->etime, NULL);
2365 tcp->flags |= TCB_INSYSCALL;
2370 tcp->flags &= ~TCB_REPRINT;
2372 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2373 tprintf("syscall_%lu(", tcp->scno);
2375 tprintf("%s(", sysent[tcp->scno].sys_name);
2376 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2377 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2378 sys_res = printargs(tcp);
2380 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2381 if (fflush(tcp->outf) == EOF)
2383 tcp->flags |= TCB_INSYSCALL;
2384 /* Measure the entrance time as late as possible to avoid errors. */
2386 gettimeofday(&tcp->etime, NULL);
2394 if (entering(tcp)) {
2397 for (i = 0; i < tcp->u_nargs; i++)
2398 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2412 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
2416 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2422 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2428 val = tcp->status.PR_REG[R_O1];
2431 val = tcp->status.PR_REG[EDX];
2434 val = tcp->status.PR_REG[RDX];
2437 val = tcp->status.PR_REG[CTX_V1];
2442 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
2449 * Apparently, indirect system calls have already be converted by ptrace(2),
2450 * so if you see "indir" this program has gone astray.
2458 if (entering(tcp)) {
2459 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2460 fprintf(stderr, "Bogus syscall: %u\n", scno);
2463 nargs = sysent[scno].nargs;
2464 tprintf("%s", sysent[scno].sys_name);
2465 for (i = 0; i < nargs; i++)
2466 tprintf(", %#lx", tcp->u_arg[i+1]);
2476 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2484 return strcmp(sysent[*((int *) a)].sys_name,
2485 sysent[*((int *) b)].sys_name);
2493 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2495 return (m < n) ? 1 : (m > n) ? -1 : 0;
2498 static int (*sortfun)();
2499 static struct timeval overhead = { -1, -1 };
2505 if (strcmp(sortby, "time") == 0)
2507 else if (strcmp(sortby, "calls") == 0)
2508 sortfun = count_cmp;
2509 else if (strcmp(sortby, "name") == 0)
2510 sortfun = syscall_cmp;
2511 else if (strcmp(sortby, "nothing") == 0)
2514 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2519 void set_overhead(n)
2522 overhead.tv_sec = n / 1000000;
2523 overhead.tv_usec = n % 1000000;
2531 int call_cum, error_cum;
2532 struct timeval tv_cum, dtv;
2534 char *dashes = "-------------------------";
2537 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2538 if (overhead.tv_sec == -1) {
2539 tv_mul(&overhead, &shortest, 8);
2540 tv_div(&overhead, &overhead, 10);
2542 for (i = 0; i < nsyscalls; i++) {
2543 sorted_count[i] = i;
2544 if (call_count[i] == 0)
2546 tv_mul(&dtv, &overhead, call_count[i]);
2547 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2548 call_cum += call_count[i];
2549 error_cum += error_count[i];
2550 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2553 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2554 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2555 "% time", "seconds", "usecs/call",
2556 "calls", "errors", "syscall");
2557 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2558 dashes, dashes, dashes, dashes, dashes, dashes);
2559 for (i = 0; i < nsyscalls; i++) {
2560 j = sorted_count[i];
2561 if (call_count[j] == 0)
2563 tv_div(&dtv, &tv_count[j], call_count[j]);
2565 sprintf(error_str, "%d", error_count[j]);
2567 error_str[0] = '\0';
2568 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2569 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2570 percent, (long) tv_count[j].tv_sec,
2571 (long) tv_count[j].tv_usec,
2572 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2573 call_count[j], error_str, sysent[j].sys_name);
2575 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2576 dashes, dashes, dashes, dashes, dashes, dashes);
2578 sprintf(error_str, "%d", error_cum);
2580 error_str[0] = '\0';
2581 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2582 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2583 call_cum, error_str, "total");