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>
51 #ifndef PTRACE_PEEKUSR
52 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
54 #elif defined(HAVE_LINUX_PTRACE_H)
56 #include <linux/ptrace.h>
59 #if defined(LINUX) && defined(IA64)
60 # include <asm/ptrace_offsets.h>
64 #ifndef SYS_ERRLIST_DECLARED
66 extern char *sys_errlist[];
67 #endif /* SYS_ERRLIST_DECLARED */
69 #define NR_SYSCALL_BASE 0
72 #define ERESTARTSYS 512
74 #ifndef ERESTARTNOINTR
75 #define ERESTARTNOINTR 513
77 #ifndef ERESTARTNOHAND
78 #define ERESTARTNOHAND 514 /* restart if no handler.. */
81 #define ENOIOCTLCMD 515 /* No ioctl command */
89 #undef NR_SYSCALL_BASE
90 #define NR_SYSCALL_BASE __NR_SYSCALL_BASE
96 /* Define these shorthand notations to simplify the syscallent files. */
99 #define TN TRACE_NETWORK
100 #define TP TRACE_PROCESS
101 #define TS TRACE_SIGNAL
103 struct sysent sysent0[] = {
104 #include "syscallent.h"
106 int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
108 #if SUPPORTED_PERSONALITIES >= 2
109 struct sysent sysent1[] = {
110 #include "syscallent1.h"
112 int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
113 #endif /* SUPPORTED_PERSONALITIES >= 2 */
115 #if SUPPORTED_PERSONALITIES >= 3
116 struct sysent sysent2[] = {
117 #include "syscallent2.h"
119 int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
120 #endif /* SUPPORTED_PERSONALITIES >= 3 */
122 struct sysent *sysent;
125 /* Now undef them since short defines cause wicked namespace pollution. */
132 char *errnoent0[] = {
133 #include "errnoent.h"
135 int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
137 #if SUPPORTED_PERSONALITIES >= 2
138 char *errnoent1[] = {
139 #include "errnoent1.h"
141 int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
142 #endif /* SUPPORTED_PERSONALITIES >= 2 */
144 #if SUPPORTED_PERSONALITIES >= 3
145 char *errnoent2[] = {
146 #include "errnoent2.h"
148 int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
149 #endif /* SUPPORTED_PERSONALITIES >= 3 */
154 int current_personality;
157 set_personality(personality)
160 switch (personality) {
162 errnoent = errnoent0;
165 nsyscalls = nsyscalls0;
166 ioctlent = ioctlent0;
167 nioctlents = nioctlents0;
168 signalent = signalent0;
169 nsignals = nsignals0;
172 #if SUPPORTED_PERSONALITIES >= 2
174 errnoent = errnoent1;
177 nsyscalls = nsyscalls1;
178 ioctlent = ioctlent1;
179 nioctlents = nioctlents1;
180 signalent = signalent1;
181 nsignals = nsignals1;
183 #endif /* SUPPORTED_PERSONALITIES >= 2 */
185 #if SUPPORTED_PERSONALITIES >= 3
187 errnoent = errnoent2;
190 nsyscalls = nsyscalls2;
191 ioctlent = ioctlent2;
192 nioctlents = nioctlents2;
193 signalent = signalent2;
194 nsignals = nsignals2;
196 #endif /* SUPPORTED_PERSONALITIES >= 3 */
202 current_personality = personality;
206 int qual_flags[MAX_QUALS];
208 static int call_count[MAX_QUALS];
209 static int error_count[MAX_QUALS];
210 static struct timeval tv_count[MAX_QUALS];
211 static int sorted_count[MAX_QUALS];
213 static struct timeval shortest = { 1000000, 0 };
215 static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
217 static struct qual_options {
223 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
224 { QUAL_TRACE, "t", lookup_syscall, "system call" },
225 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
226 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
227 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
228 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
229 { QUAL_RAW, "raw", lookup_syscall, "system call" },
230 { QUAL_RAW, "x", lookup_syscall, "system call" },
231 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
232 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
233 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
234 { QUAL_FAULT, "fault", lookup_fault, "fault" },
235 { QUAL_FAULT, "faults", lookup_fault, "fault" },
236 { QUAL_FAULT, "m", lookup_fault, "fault" },
237 { QUAL_READ, "read", lookup_desc, "descriptor" },
238 { QUAL_READ, "reads", lookup_desc, "descriptor" },
239 { QUAL_READ, "r", lookup_desc, "descriptor" },
240 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
241 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
242 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
243 { 0, NULL, NULL, NULL },
252 for (i = 0; i < nsyscalls; i++) {
253 if (strcmp(s, sysent[i].sys_name) == 0)
266 if (s && *s && isdigit((unsigned char)*s))
270 for (i = 0; s[i]; i++)
271 s[i] = toupper((unsigned char)(s[i]));
272 if (strncmp(s, "SIG", 3) == 0)
274 for (i = 0; i <= NSIG; i++) {
275 if (strcmp(s, signame(i) + 3) == 0)
292 if (s && *s && isdigit((unsigned char)*s))
301 if (strcmp(s, "file") == 0)
303 if (strcmp(s, "ipc") == 0)
305 if (strcmp(s, "network") == 0)
306 return TRACE_NETWORK;
307 if (strcmp(s, "process") == 0)
308 return TRACE_PROCESS;
309 if (strcmp(s, "signal") == 0)
318 struct qual_options *opt;
323 opt = &qual_options[0];
324 for (i = 0; (p = qual_options[i].option_name); i++) {
326 if (strncmp(s, p, n) == 0 && s[n] == '=') {
327 opt = &qual_options[i];
337 if (strcmp(s, "none") == 0) {
341 if (strcmp(s, "all") == 0) {
342 for (i = 0; i < MAX_QUALS; i++) {
344 qual_flags[i] &= ~opt->bitflag;
346 qual_flags[i] |= opt->bitflag;
350 for (i = 0; i < MAX_QUALS; i++) {
352 qual_flags[i] |= opt->bitflag;
354 qual_flags[i] &= ~opt->bitflag;
356 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
357 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
358 for (i = 0; i < MAX_QUALS; i++) {
359 if (sysent[i].sys_flags & n) {
361 qual_flags[i] &= ~opt->bitflag;
363 qual_flags[i] |= opt->bitflag;
368 if ((n = (*opt->lookup)(p)) < 0) {
369 fprintf(stderr, "strace: invalid %s `%s'\n",
370 opt->argument_name, p);
374 qual_flags[n] &= ~opt->bitflag;
376 qual_flags[n] |= opt->bitflag;
387 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
389 switch (tcp->scno + NR_SYSCALL_BASE) {
397 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
398 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
407 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
408 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
414 enum subcall_style { shift_style, deref_style, mask_style, door_style };
416 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
424 const struct subcall subcalls_table[] = {
425 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
427 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
429 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
431 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
435 #if !(defined(LINUX) && ( defined(ALPHA) || defined(IA64) || defined(MIPS) ))
437 const int socket_map [] = {
440 /* SYS_CONNECT */ 98,
441 /* SYS_LISTEN */ 106,
443 /* SYS_GETSOCKNAME */ 150,
444 /* SYS_GETPEERNAME */ 141,
445 /* SYS_SOCKETPAIR */ 135,
448 /* SYS_SENDTO */ 133,
449 /* SYS_RECVFROM */ 125,
450 /* SYS_SHUTDOWN */ 134,
451 /* SYS_SETSOCKOPT */ 105,
452 /* SYS_GETSOCKOPT */ 118,
453 /* SYS_SENDMSG */ 114,
454 /* SYS_RECVMSG */ 113
458 sparc_socket_decode (tcp)
464 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
467 tcp->scno = socket_map [tcp->u_arg [0]-1];
468 n = tcp->u_nargs = sysent [tcp->scno].nargs;
469 addr = tcp->u_arg [1];
470 for (i = 0; i < n; i++){
472 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
474 tcp->u_arg [i] = arg;
475 addr += sizeof (arg);
480 decode_subcall(tcp, subcall, nsubcalls, style)
484 enum subcall_style style;
486 int i, addr, mask, arg;
490 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
492 tcp->scno = subcall + tcp->u_arg[0];
493 if (sysent[tcp->scno].nargs != -1)
494 tcp->u_nargs = sysent[tcp->scno].nargs;
497 for (i = 0; i < tcp->u_nargs; i++)
498 tcp->u_arg[i] = tcp->u_arg[i + 1];
501 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
503 tcp->scno = subcall + tcp->u_arg[0];
504 addr = tcp->u_arg[1];
505 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
506 if (umove(tcp, addr, &arg) < 0)
511 tcp->u_nargs = sysent[tcp->scno].nargs;
514 mask = (tcp->u_arg[0] >> 8) & 0xff;
515 for (i = 0; mask; i++)
519 tcp->u_arg[0] &= 0xff;
520 tcp->scno = subcall + i;
521 if (sysent[tcp->scno].nargs != -1)
522 tcp->u_nargs = sysent[tcp->scno].nargs;
526 * Oh, yuck. The call code is the *sixth* argument.
527 * (don't you mean the *last* argument? - JH)
529 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
531 tcp->scno = subcall + tcp->u_arg[5];
532 if (sysent[tcp->scno].nargs != -1)
533 tcp->u_nargs = sysent[tcp->scno].nargs;
539 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
540 if (subcalls_table[i].call == tcp->scno) break;
541 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
542 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
543 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
544 for (i = 0; i < tcp->u_nargs; i++)
545 tcp->u_arg[i] = tcp->u_arg[i + 1];
553 struct tcb *tcp_last = NULL;
556 internal_syscall(tcp)
560 * We must always trace a few critical system calls in order to
561 * correctly support following forks in the presence of tracing
564 switch (tcp->scno + NR_SYSCALL_BASE) {
618 #elif defined (POWERPC)
619 static long result,flags;
624 #elif defined (ALPHA)
627 #elif defined (SPARC)
628 static struct pt_regs regs;
629 static unsigned long trap;
653 if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
655 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
659 #elif defined (POWERPC)
660 if (upeek(pid, 4*PT_R0, &scno) < 0)
662 if (!(tcp->flags & TCB_INSYSCALL)) {
663 /* Check if we return from execve. */
664 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
665 tcp->flags &= ~TCB_WAITEXECVE;
670 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
673 #define IA64_PSR_IS ((long)1 << 34)
674 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
675 ia32 = (psr & IA64_PSR_IS);
676 if (!(tcp->flags & TCB_INSYSCALL)) {
678 if (upeek(pid, PT_R8, &scno) < 0)
681 if (upeek (pid, PT_R15, &scno) < 0)
685 /* syscall in progress */
686 if (upeek (pid, PT_R8, &r8) < 0)
688 if (upeek (pid, PT_R10, &r10) < 0)
694 upeek(pid, 4*15, &pc);
695 umoven(tcp, pc-4, 4, (char *)&scno);
699 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
702 if (upeek(pid, REG_A3, &a3) < 0)
705 if(!(tcp->flags & TCB_INSYSCALL)) {
706 if (upeek(pid, REG_V0, &scno) < 0)
709 if (scno < 0 || scno > nsyscalls) {
710 if(a3 == 0 || a3 == -1) {
712 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
717 if (upeek(pid, REG_V0, &r2) < 0)
720 #elif defined (ALPHA)
721 if (upeek(pid, REG_A3, &a3) < 0)
724 if (!(tcp->flags & TCB_INSYSCALL)) {
725 if (upeek(pid, REG_R0, &scno) < 0)
728 /* Check if we return from execve. */
729 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
730 tcp->flags &= ~TCB_WAITEXECVE;
735 * Do some sanity checks to figure out if it's
736 * really a syscall entry
738 if (scno < 0 || scno > nsyscalls) {
739 if (a3 == 0 || a3 == -1) {
741 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
747 if (upeek(pid, REG_R0, &r0) < 0)
750 #elif defined (SPARC)
751 /* Everything we need is in the current register set. */
752 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
755 /* If we are entering, then disassemble the syscall trap. */
756 if (!(tcp->flags & TCB_INSYSCALL)) {
757 /* Retrieve the syscall trap instruction. */
759 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
763 /* Disassemble the trap to see what personality to use. */
766 /* Linux/SPARC syscall trap. */
770 /* Linux/SPARC64 syscall trap. */
771 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
774 /* SunOS syscall trap. (pers 1) */
775 fprintf(stderr,"syscall: SunOS no support\n");
778 /* Solaris 2.x syscall trap. (per 2) */
782 /* NetBSD/FreeBSD syscall trap. */
783 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
786 /* Solaris 2.x gettimeofday */
790 /* Unknown syscall trap. */
791 if(tcp->flags & TCB_WAITEXECVE) {
792 tcp->flags &= ~TCB_WAITEXECVE;
795 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
799 /* Extract the system call number from the registers. */
800 if (trap == 0x91d02027)
806 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
812 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
816 #ifdef HAVE_PR_SYSCALL
817 scno = tcp->status.PR_SYSCALL;
818 #else /* !HAVE_PR_SYSCALL */
820 scno = tcp->status.PR_WHAT;
822 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
826 switch (regs.r_eax) {
829 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
836 #endif /* !HAVE_PR_SYSCALL */
837 #endif /* USE_PROCFS */
838 if (!(tcp->flags & TCB_INSYSCALL))
850 #else /* USE_PROCFS */
851 int scno = tcp->scno;
853 if (!(tcp->flags & TCB_INSYSCALL)) {
854 if (tcp->status.PR_WHY != PR_SYSENTRY) {
859 #endif /* SYS_vfork */
861 /* We are returning in the child, fake it. */
862 tcp->status.PR_WHY = PR_SYSENTRY;
864 tcp->status.PR_WHY = PR_SYSEXIT;
867 fprintf(stderr, "syscall: missing entry\n");
868 tcp->flags |= TCB_INSYSCALL;
873 if (tcp->status.PR_WHY != PR_SYSEXIT) {
874 fprintf(stderr, "syscall: missing exit\n");
875 tcp->flags &= ~TCB_INSYSCALL;
878 #endif /* USE_PROCFS */
880 if (!(tcp->flags & TCB_INSYSCALL)) {
882 fprintf(stderr, "syscall: missing entry\n");
883 tcp->flags |= TCB_INSYSCALL;
890 * This happens when a signal handler
891 * for a signal which interrupted a
892 * a system call makes another system call.
894 fprintf(stderr, "syscall: missing exit\n");
896 tcp->flags &= ~TCB_INSYSCALL;
902 if (upeek(pid, 4*EAX, &eax) < 0)
904 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
906 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
910 if (upeek(pid, PT_GPR2, &gpr2) < 0)
912 if (gpr2 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
914 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
917 #elif defined (POWERPC)
918 # define SO_MASK 0x10000000
919 if (upeek(pid, 4*PT_CCR, &flags) < 0)
921 if (upeek(pid, 4*PT_R3, &result) < 0)
926 if (upeek(pid, 4*PT_D0, &d0) < 0)
928 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
930 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
934 if (upeek(pid, 4*0, (long *)&r0) < 0)
936 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
938 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
954 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
964 if (eax < 0 && -eax < nerrnos) {
978 if (err < 0 && -err < nerrnos) {
1006 if (result && (unsigned) -result < nerrnos) {
1011 tcp->u_rval = result;
1014 #else /* !POWERPC */
1016 if (d0 && (unsigned) -d0 < nerrnos) {
1026 if (r0 && (unsigned) -r0 < nerrnos) {
1046 if (regs.r_psr & PSR_C) {
1048 u_error = regs.r_o0;
1051 tcp->u_rval = regs.r_o0;
1058 #endif /* POWERPC */
1065 /* get error code from user struct */
1066 if (upeek(pid, uoff(u_error), &u_error) < 0)
1068 u_error >>= 24; /* u_error is a char */
1070 /* get system call return value */
1071 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1076 /* Judicious guessing goes a long way. */
1077 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1079 u_error = tcp->status.pr_reg[R_O0];
1082 tcp->u_rval = tcp->status.pr_reg[R_O0];
1087 /* Wanna know how to kill an hour single-stepping? */
1088 if (tcp->status.PR_REG[EFL] & 0x1) {
1090 u_error = tcp->status.PR_REG[EAX];
1093 tcp->u_rval = tcp->status.PR_REG[EAX];
1094 #ifdef HAVE_LONG_LONG
1096 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1097 tcp->status.PR_REG[EAX];
1103 if (tcp->status.pr_reg[CTX_A3]) {
1105 u_error = tcp->status.pr_reg[CTX_V0];
1108 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1114 if (regs.r_eflags & PSL_C) {
1116 u_error = regs.r_eax;
1118 tcp->u_rval = regs.r_eax;
1120 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1123 #endif /* FREEBSD */
1124 tcp->u_error = u_error;
1128 int syscall_enter(tcp)
1133 #endif /* !USE_PROCFS */
1138 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1139 tcp->u_nargs = sysent[tcp->scno].nargs;
1141 tcp->u_nargs = MAX_ARGS;
1142 for (i = 0; i < tcp->u_nargs; i++) {
1143 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
1147 #elif defined (ALPHA)
1150 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1151 tcp->u_nargs = sysent[tcp->scno].nargs;
1153 tcp->u_nargs = MAX_ARGS;
1154 for (i = 0; i < tcp->u_nargs; i++) {
1155 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1156 * for scno somewhere above here!
1158 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1162 #elif defined (IA64)
1164 unsigned long *bsp, cfm, i;
1166 if (upeek(pid, PT_AR_BSP, (long *) &bsp) < 0)
1168 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1171 bsp = ia64_rse_skip_regs(bsp, -(cfm & 0x7f));
1173 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1174 tcp->u_nargs = sysent[tcp->scno].nargs;
1176 tcp->u_nargs = MAX_ARGS;
1177 for (i = 0; i < tcp->u_nargs; ++i) {
1178 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(bsp, i), sizeof(long),
1179 (char *) &tcp->u_arg[i])
1184 #elif defined (MIPS)
1189 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1190 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1192 nargs = tcp->u_nargs = MAX_ARGS;
1194 if(upeek(pid, REG_SP, &sp) < 0)
1196 for(i = 0; i < 4; i++) {
1197 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1200 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1201 (char *)(tcp->u_arg + 4));
1203 for(i = 0; i < nargs; i++) {
1204 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1209 #elif defined (POWERPC)
1212 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1213 tcp->u_nargs = sysent[tcp->scno].nargs;
1215 tcp->u_nargs = MAX_ARGS;
1216 for (i = 0; i < tcp->u_nargs; i++) {
1217 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1221 #elif defined (SPARC)
1225 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1226 tcp->u_nargs = sysent[tcp->scno].nargs;
1228 tcp->u_nargs = MAX_ARGS;
1229 for (i = 0; i < tcp->u_nargs; i++)
1230 tcp->u_arg[i] = *((®s.r_o0) + i);
1232 #else /* Other architecture (like i386) (32bits specific) */
1235 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1236 tcp->u_nargs = sysent[tcp->scno].nargs;
1238 tcp->u_nargs = MAX_ARGS;
1239 for (i = 0; i < tcp->u_nargs; i++) {
1240 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1249 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1250 tcp->u_nargs = sysent[tcp->scno].nargs;
1252 tcp->u_nargs = MAX_ARGS;
1253 for (i = 0; i < tcp->u_nargs; i++) {
1256 if (upeek(pid, uoff(u_arg[0]) +
1257 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1265 * SGI is broken: even though it has pr_sysarg, it doesn't
1266 * set them on system call entry. Get a clue.
1268 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1269 tcp->u_nargs = sysent[tcp->scno].nargs;
1271 tcp->u_nargs = tcp->status.pr_nsysarg;
1272 if (tcp->u_nargs > 4) {
1273 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1274 4*sizeof(tcp->u_arg[0]));
1275 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1276 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1279 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1280 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1284 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1286 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1287 tcp->u_nargs = sysent[tcp->scno].nargs;
1289 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1290 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1291 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1292 #elif defined (HAVE_PR_SYSCALL)
1293 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1294 tcp->u_nargs = sysent[tcp->scno].nargs;
1296 tcp->u_nargs = tcp->status.pr_nsysarg;
1299 for (i = 0; i < tcp->u_nargs; i++)
1300 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1302 #elif defined (I386)
1303 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1304 tcp->u_nargs = sysent[tcp->scno].nargs;
1307 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1308 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1310 I DONT KNOW WHAT TO DO
1311 #endif /* !HAVE_PR_SYSCALL */
1314 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1315 sysent[tcp->scno].nargs > tcp->status.val)
1316 tcp->u_nargs = sysent[tcp->scno].nargs;
1318 tcp->u_nargs = tcp->status.val;
1319 if (tcp->u_nargs < 0)
1321 if (tcp->u_nargs > MAX_ARGS)
1322 tcp->u_nargs = MAX_ARGS;
1323 switch(regs.r_eax) {
1325 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1326 regs.r_esp + sizeof(int) + sizeof(quad_t));
1329 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1330 regs.r_esp + 2 * sizeof(int));
1333 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1334 regs.r_esp + sizeof(int));
1337 #endif /* FREEBSD */
1349 /* Measure the exit time as early as possible to avoid errors. */
1350 if (dtime && (tcp->flags & TCB_INSYSCALL))
1351 gettimeofday(&tv, NULL);
1353 res = get_scno(tcp);
1357 res = syscall_fixup(tcp);
1361 if (tcp->flags & TCB_INSYSCALL) {
1363 res = get_error(tcp);
1366 u_error = tcp->u_error;
1369 internal_syscall(tcp);
1370 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1371 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
1372 tcp->flags &= ~TCB_INSYSCALL;
1376 if (tcp->flags & TCB_REPRINT) {
1379 if (tcp->scno >= nsyscalls || tcp->scno < 0)
1380 tprintf("syscall_%lu", tcp->scno);
1382 tprintf("%s", sysent[tcp->scno].sys_name);
1383 tprintf(" resumed> ");
1386 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
1387 call_count[tcp->scno]++;
1389 error_count[tcp->scno]++;
1390 tv_sub(&tv, &tv, &tcp->etime);
1392 if (tv_cmp(&tv, &tcp->dtime) > 0) {
1393 static struct timeval one_tick =
1394 { 0, 1000000 / HZ };
1396 if (tv_nz(&tcp->dtime))
1398 else if (tv_cmp(&tv, &one_tick) > 0) {
1399 if (tv_cmp(&shortest, &one_tick) < 0)
1406 if (tv_cmp(&tv, &shortest) < 0)
1408 tv_add(&tv_count[tcp->scno],
1409 &tv_count[tcp->scno], &tv);
1410 tcp->flags &= ~TCB_INSYSCALL;
1414 if (tcp->scno >= nsyscalls || tcp->scno < 0
1415 || (qual_flags[tcp->scno] & QUAL_RAW))
1416 sys_res = printargs(tcp);
1418 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1419 u_error = tcp->u_error;
1422 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
1423 qual_flags[tcp->scno] & QUAL_RAW) {
1425 tprintf("= -1 (errno %ld)", u_error);
1427 tprintf("= %#lx", tcp->u_rval);
1429 else if (!(sys_res & RVAL_NONE) && u_error) {
1433 tprintf("= ? ERESTARTSYS (To be restarted)");
1435 case ERESTARTNOINTR:
1436 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1438 case ERESTARTNOHAND:
1439 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1444 if (u_error < nerrnos && u_error < sys_nerr)
1445 tprintf("%s (%s)", errnoent[u_error],
1446 sys_errlist[u_error]);
1447 else if (u_error < nerrnos)
1448 tprintf("%s (errno %ld)",
1449 errnoent[u_error], u_error);
1450 else if (u_error < sys_nerr)
1451 tprintf("ERRNO_%ld (%s)", u_error,
1452 sys_errlist[u_error]);
1454 tprintf("E??? (errno %ld)", u_error);
1459 if (sys_res & RVAL_NONE)
1462 switch (sys_res & RVAL_MASK) {
1464 tprintf("= %#lx", tcp->u_rval);
1467 tprintf("= %#lo", tcp->u_rval);
1470 tprintf("= %lu", tcp->u_rval);
1473 tprintf("= %ld", tcp->u_rval);
1475 #ifdef HAVE_LONG_LONG
1477 tprintf("= %#llx", tcp->u_lrval);
1480 tprintf("= %#llo", tcp->u_lrval);
1482 case RVAL_LUDECIMAL:
1483 tprintf("= %llu", tcp->u_lrval);
1486 tprintf("= %lld", tcp->u_lrval);
1491 "invalid rval format\n");
1495 if ((sys_res & RVAL_STR) && tcp->auxstr)
1496 tprintf(" (%s)", tcp->auxstr);
1499 tv_sub(&tv, &tv, &tcp->etime);
1500 tprintf(" <%ld.%06ld>",
1501 (long) tv.tv_sec, (long) tv.tv_usec);
1506 if (fflush(tcp->outf) == EOF)
1508 tcp->flags &= ~TCB_INSYSCALL;
1512 /* Entering system call */
1513 res = syscall_enter(tcp);
1517 switch (tcp->scno + NR_SYSCALL_BASE) {
1519 #if !defined (ALPHA) && !defined(IA64) && !defined(SPARC) && !defined(MIPS)
1520 case SYS_socketcall:
1521 decode_subcall(tcp, SYS_socket_subcall,
1522 SYS_socket_nsubcalls, deref_style);
1525 decode_subcall(tcp, SYS_ipc_subcall,
1526 SYS_ipc_nsubcalls, shift_style);
1528 #endif /* !ALPHA && !IA64 && !MIPS && !SPARC */
1530 case SYS_socketcall:
1531 sparc_socket_decode (tcp);
1536 #ifdef SYS_pgrpsys_subcall
1538 decode_subcall(tcp, SYS_pgrpsys_subcall,
1539 SYS_pgrpsys_nsubcalls, shift_style);
1541 #endif /* SYS_pgrpsys_subcall */
1542 #ifdef SYS_sigcall_subcall
1544 decode_subcall(tcp, SYS_sigcall_subcall,
1545 SYS_sigcall_nsubcalls, mask_style);
1547 #endif /* SYS_sigcall_subcall */
1549 decode_subcall(tcp, SYS_msgsys_subcall,
1550 SYS_msgsys_nsubcalls, shift_style);
1553 decode_subcall(tcp, SYS_shmsys_subcall,
1554 SYS_shmsys_nsubcalls, shift_style);
1557 decode_subcall(tcp, SYS_semsys_subcall,
1558 SYS_semsys_nsubcalls, shift_style);
1562 decode_subcall(tcp, SYS_utssys_subcall,
1563 SYS_utssys_nsubcalls, shift_style);
1567 decode_subcall(tcp, SYS_sysfs_subcall,
1568 SYS_sysfs_nsubcalls, shift_style);
1571 decode_subcall(tcp, SYS_spcall_subcall,
1572 SYS_spcall_nsubcalls, shift_style);
1574 #ifdef SYS_context_subcall
1576 decode_subcall(tcp, SYS_context_subcall,
1577 SYS_context_nsubcalls, shift_style);
1579 #endif /* SYS_context_subcall */
1580 #ifdef SYS_door_subcall
1582 decode_subcall(tcp, SYS_door_subcall,
1583 SYS_door_nsubcalls, door_style);
1585 #endif /* SYS_door_subcall */
1586 #ifdef SYS_kaio_subcall
1588 decode_subcall(tcp, SYS_kaio_subcall,
1589 SYS_kaio_nsubcalls, shift_style);
1597 decode_subcall(tcp, 0, 0, table_style);
1602 decode_subcall(tcp, SYS_semsys_subcall,
1603 SYS_semsys_nsubcalls, shift_style);
1606 decode_subcall(tcp, SYS_msgsys_subcall,
1607 SYS_msgsys_nsubcalls, shift_style);
1610 decode_subcall(tcp, SYS_shmsys_subcall,
1611 SYS_shmsys_nsubcalls, shift_style);
1616 internal_syscall(tcp);
1617 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
1618 tcp->flags |= TCB_INSYSCALL;
1623 gettimeofday(&tcp->etime, NULL);
1624 tcp->flags |= TCB_INSYSCALL;
1629 tcp->flags &= ~TCB_REPRINT;
1631 if (tcp->scno >= nsyscalls || tcp->scno < 0)
1632 tprintf("syscall_%lu(", tcp->scno);
1634 tprintf("%s(", sysent[tcp->scno].sys_name);
1635 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
1636 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1637 sys_res = printargs(tcp);
1639 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1640 if (fflush(tcp->outf) == EOF)
1642 tcp->flags |= TCB_INSYSCALL;
1643 /* Measure the entrance time as late as possible to avoid errors. */
1645 gettimeofday(&tcp->etime, NULL);
1653 if (entering(tcp)) {
1656 for (i = 0; i < tcp->u_nargs; i++)
1657 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1671 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
1678 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1684 val = tcp->status.PR_REG[R_O1];
1687 val = tcp->status.PR_REG[EDX];
1690 val = tcp->status.PR_REG[CTX_V1];
1695 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
1702 * Apparently, indirect system calls have already be converted by ptrace(2),
1703 * so if you see "indir" this program has gone astray.
1711 if (entering(tcp)) {
1712 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1713 fprintf(stderr, "Bogus syscall: %u\n", scno);
1716 nargs = sysent[scno].nargs;
1717 tprintf("%s", sysent[scno].sys_name);
1718 for (i = 0; i < nargs; i++)
1719 tprintf(", %#lx", tcp->u_arg[i+1]);
1729 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1737 return strcmp(sysent[*((int *) a)].sys_name,
1738 sysent[*((int *) b)].sys_name);
1746 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1748 return (m < n) ? 1 : (m > n) ? -1 : 0;
1751 static int (*sortfun)();
1752 static struct timeval overhead = { -1, -1 };
1758 if (strcmp(sortby, "time") == 0)
1760 else if (strcmp(sortby, "calls") == 0)
1761 sortfun = count_cmp;
1762 else if (strcmp(sortby, "name") == 0)
1763 sortfun = syscall_cmp;
1764 else if (strcmp(sortby, "nothing") == 0)
1767 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1772 void set_overhead(n)
1775 overhead.tv_sec = n / 1000000;
1776 overhead.tv_usec = n % 1000000;
1784 int call_cum, error_cum;
1785 struct timeval tv_cum, dtv;
1787 char *dashes = "-------------------------";
1790 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1791 if (overhead.tv_sec == -1) {
1792 tv_mul(&overhead, &shortest, 8);
1793 tv_div(&overhead, &overhead, 10);
1795 for (i = 0; i < nsyscalls; i++) {
1796 sorted_count[i] = i;
1797 if (call_count[i] == 0)
1799 tv_mul(&dtv, &overhead, call_count[i]);
1800 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1801 call_cum += call_count[i];
1802 error_cum += error_count[i];
1803 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1806 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1807 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1808 "% time", "seconds", "usecs/call",
1809 "calls", "errors", "syscall");
1810 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1811 dashes, dashes, dashes, dashes, dashes, dashes);
1812 for (i = 0; i < nsyscalls; i++) {
1813 j = sorted_count[i];
1814 if (call_count[j] == 0)
1816 tv_div(&dtv, &tv_count[j], call_count[j]);
1818 sprintf(error_str, "%d", error_count[j]);
1820 error_str[0] = '\0';
1821 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1822 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1823 percent, (long) tv_count[j].tv_sec,
1824 (long) tv_count[j].tv_usec,
1825 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1826 call_count[j], error_str, sysent[j].sys_name);
1828 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1829 dashes, dashes, dashes, dashes, dashes, dashes);
1831 sprintf(error_str, "%d", error_cum);
1833 error_str[0] = '\0';
1834 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1835 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1836 call_cum, error_str, "total");