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;
655 if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
657 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
661 #elif defined (POWERPC)
662 if (upeek(pid, 4*PT_R0, &scno) < 0)
664 if (!(tcp->flags & TCB_INSYSCALL)) {
665 /* Check if we return from execve. */
666 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
667 tcp->flags &= ~TCB_WAITEXECVE;
672 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
675 #define IA64_PSR_IS ((long)1 << 34)
676 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
677 ia32 = (psr & IA64_PSR_IS);
678 if (!(tcp->flags & TCB_INSYSCALL)) {
680 if (upeek(pid, PT_R8, &scno) < 0)
683 if (upeek (pid, PT_R15, &scno) < 0)
687 /* syscall in progress */
688 if (upeek (pid, PT_R8, &r8) < 0)
690 if (upeek (pid, PT_R10, &r10) < 0)
696 upeek(pid, 4*15, &pc);
697 umoven(tcp, pc-4, 4, (char *)&scno);
701 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
704 if (upeek(pid, REG_A3, &a3) < 0)
707 if(!(tcp->flags & TCB_INSYSCALL)) {
708 if (upeek(pid, REG_V0, &scno) < 0)
711 if (scno < 0 || scno > nsyscalls) {
712 if(a3 == 0 || a3 == -1) {
714 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
719 if (upeek(pid, REG_V0, &r2) < 0)
722 #elif defined (ALPHA)
723 if (upeek(pid, REG_A3, &a3) < 0)
726 if (!(tcp->flags & TCB_INSYSCALL)) {
727 if (upeek(pid, REG_R0, &scno) < 0)
730 /* Check if we return from execve. */
731 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
732 tcp->flags &= ~TCB_WAITEXECVE;
737 * Do some sanity checks to figure out if it's
738 * really a syscall entry
740 if (scno < 0 || scno > nsyscalls) {
741 if (a3 == 0 || a3 == -1) {
743 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
749 if (upeek(pid, REG_R0, &r0) < 0)
752 #elif defined (SPARC)
753 /* Everything we need is in the current register set. */
754 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
757 /* If we are entering, then disassemble the syscall trap. */
758 if (!(tcp->flags & TCB_INSYSCALL)) {
759 /* Retrieve the syscall trap instruction. */
761 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
765 /* Disassemble the trap to see what personality to use. */
768 /* Linux/SPARC syscall trap. */
772 /* Linux/SPARC64 syscall trap. */
773 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
776 /* SunOS syscall trap. (pers 1) */
777 fprintf(stderr,"syscall: SunOS no support\n");
780 /* Solaris 2.x syscall trap. (per 2) */
784 /* NetBSD/FreeBSD syscall trap. */
785 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
788 /* Solaris 2.x gettimeofday */
792 /* Unknown syscall trap. */
793 if(tcp->flags & TCB_WAITEXECVE) {
794 tcp->flags &= ~TCB_WAITEXECVE;
797 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
801 /* Extract the system call number from the registers. */
802 if (trap == 0x91d02027)
808 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
812 if (upeek(pid, PT_GR20, &scno) < 0)
814 if (!(tcp->flags & TCB_INSYSCALL)) {
815 /* Check if we return from execve. */
816 if ((tcp->flags & TCB_WAITEXECVE)) {
817 tcp->flags &= ~TCB_WAITEXECVE;
824 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
828 #ifdef HAVE_PR_SYSCALL
829 scno = tcp->status.PR_SYSCALL;
830 #else /* !HAVE_PR_SYSCALL */
832 scno = tcp->status.PR_WHAT;
834 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
838 switch (regs.r_eax) {
841 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
848 #endif /* !HAVE_PR_SYSCALL */
849 #endif /* USE_PROCFS */
850 if (!(tcp->flags & TCB_INSYSCALL))
862 #else /* USE_PROCFS */
863 int scno = tcp->scno;
865 if (!(tcp->flags & TCB_INSYSCALL)) {
866 if (tcp->status.PR_WHY != PR_SYSENTRY) {
871 #endif /* SYS_vfork */
873 /* We are returning in the child, fake it. */
874 tcp->status.PR_WHY = PR_SYSENTRY;
876 tcp->status.PR_WHY = PR_SYSEXIT;
879 fprintf(stderr, "syscall: missing entry\n");
880 tcp->flags |= TCB_INSYSCALL;
885 if (tcp->status.PR_WHY != PR_SYSEXIT) {
886 fprintf(stderr, "syscall: missing exit\n");
887 tcp->flags &= ~TCB_INSYSCALL;
890 #endif /* USE_PROCFS */
892 if (!(tcp->flags & TCB_INSYSCALL)) {
894 fprintf(stderr, "syscall: missing entry\n");
895 tcp->flags |= TCB_INSYSCALL;
902 * This happens when a signal handler
903 * for a signal which interrupted a
904 * a system call makes another system call.
906 fprintf(stderr, "syscall: missing exit\n");
908 tcp->flags &= ~TCB_INSYSCALL;
914 if (upeek(pid, 4*EAX, &eax) < 0)
916 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
918 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
922 if (upeek(pid, PT_GPR2, &gpr2) < 0)
924 if (gpr2 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
926 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
929 #elif defined (POWERPC)
930 # define SO_MASK 0x10000000
931 if (upeek(pid, 4*PT_CCR, &flags) < 0)
933 if (upeek(pid, 4*PT_R3, &result) < 0)
938 if (upeek(pid, 4*PT_D0, &d0) < 0)
940 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
942 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
946 if (upeek(pid, 4*0, (long *)&r0) < 0)
948 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
950 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
954 if (upeek(pid, PT_GR28, &r28) < 0)
968 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
978 if (eax < 0 && -eax < nerrnos) {
992 if (err < 0 && -err < nerrnos) {
1020 if (result && (unsigned) -result < nerrnos) {
1025 tcp->u_rval = result;
1028 #else /* !POWERPC */
1030 if (d0 && (unsigned) -d0 < nerrnos) {
1040 if (r0 && (unsigned) -r0 < nerrnos) {
1060 if (regs.r_psr & PSR_C) {
1062 u_error = regs.r_o0;
1065 tcp->u_rval = regs.r_o0;
1070 if (r28 && (unsigned) -r28 < nerrnos) {
1083 #endif /* POWERPC */
1090 /* get error code from user struct */
1091 if (upeek(pid, uoff(u_error), &u_error) < 0)
1093 u_error >>= 24; /* u_error is a char */
1095 /* get system call return value */
1096 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1101 /* Judicious guessing goes a long way. */
1102 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1104 u_error = tcp->status.pr_reg[R_O0];
1107 tcp->u_rval = tcp->status.pr_reg[R_O0];
1112 /* Wanna know how to kill an hour single-stepping? */
1113 if (tcp->status.PR_REG[EFL] & 0x1) {
1115 u_error = tcp->status.PR_REG[EAX];
1118 tcp->u_rval = tcp->status.PR_REG[EAX];
1119 #ifdef HAVE_LONG_LONG
1121 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1122 tcp->status.PR_REG[EAX];
1128 if (tcp->status.pr_reg[CTX_A3]) {
1130 u_error = tcp->status.pr_reg[CTX_V0];
1133 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1139 if (regs.r_eflags & PSL_C) {
1141 u_error = regs.r_eax;
1143 tcp->u_rval = regs.r_eax;
1145 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1148 #endif /* FREEBSD */
1149 tcp->u_error = u_error;
1153 int syscall_enter(tcp)
1158 #endif /* !USE_PROCFS */
1163 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1164 tcp->u_nargs = sysent[tcp->scno].nargs;
1166 tcp->u_nargs = MAX_ARGS;
1167 for (i = 0; i < tcp->u_nargs; i++) {
1168 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
1172 #elif defined (ALPHA)
1175 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1176 tcp->u_nargs = sysent[tcp->scno].nargs;
1178 tcp->u_nargs = MAX_ARGS;
1179 for (i = 0; i < tcp->u_nargs; i++) {
1180 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1181 * for scno somewhere above here!
1183 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1187 #elif defined (IA64)
1189 unsigned long *bsp, cfm, i;
1191 if (upeek(pid, PT_AR_BSP, (long *) &bsp) < 0)
1193 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1196 bsp = ia64_rse_skip_regs(bsp, -(cfm & 0x7f));
1198 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1199 tcp->u_nargs = sysent[tcp->scno].nargs;
1201 tcp->u_nargs = MAX_ARGS;
1202 for (i = 0; i < tcp->u_nargs; ++i) {
1203 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(bsp, i), sizeof(long),
1204 (char *) &tcp->u_arg[i])
1209 #elif defined (MIPS)
1214 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1215 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1217 nargs = tcp->u_nargs = MAX_ARGS;
1219 if(upeek(pid, REG_SP, &sp) < 0)
1221 for(i = 0; i < 4; i++) {
1222 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1225 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1226 (char *)(tcp->u_arg + 4));
1228 for(i = 0; i < nargs; i++) {
1229 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1234 #elif defined (POWERPC)
1237 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1238 tcp->u_nargs = sysent[tcp->scno].nargs;
1240 tcp->u_nargs = MAX_ARGS;
1241 for (i = 0; i < tcp->u_nargs; i++) {
1242 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1246 #elif defined (SPARC)
1250 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1251 tcp->u_nargs = sysent[tcp->scno].nargs;
1253 tcp->u_nargs = MAX_ARGS;
1254 for (i = 0; i < tcp->u_nargs; i++)
1255 tcp->u_arg[i] = *((®s.r_o0) + i);
1257 #elif defined (HPPA)
1261 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1262 tcp->u_nargs = sysent[tcp->scno].nargs;
1264 tcp->u_nargs = MAX_ARGS;
1265 for (i = 0; i < tcp->u_nargs; i++) {
1266 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1270 #else /* Other architecture (like i386) (32bits specific) */
1273 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1274 tcp->u_nargs = sysent[tcp->scno].nargs;
1276 tcp->u_nargs = MAX_ARGS;
1277 for (i = 0; i < tcp->u_nargs; i++) {
1278 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1287 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1288 tcp->u_nargs = sysent[tcp->scno].nargs;
1290 tcp->u_nargs = MAX_ARGS;
1291 for (i = 0; i < tcp->u_nargs; i++) {
1294 if (upeek(pid, uoff(u_arg[0]) +
1295 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1303 * SGI is broken: even though it has pr_sysarg, it doesn't
1304 * set them on system call entry. Get a clue.
1306 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1307 tcp->u_nargs = sysent[tcp->scno].nargs;
1309 tcp->u_nargs = tcp->status.pr_nsysarg;
1310 if (tcp->u_nargs > 4) {
1311 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1312 4*sizeof(tcp->u_arg[0]));
1313 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1314 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1317 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1318 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1322 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1324 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1325 tcp->u_nargs = sysent[tcp->scno].nargs;
1327 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1328 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1329 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1330 #elif defined (HAVE_PR_SYSCALL)
1331 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1332 tcp->u_nargs = sysent[tcp->scno].nargs;
1334 tcp->u_nargs = tcp->status.pr_nsysarg;
1337 for (i = 0; i < tcp->u_nargs; i++)
1338 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1340 #elif defined (I386)
1341 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1342 tcp->u_nargs = sysent[tcp->scno].nargs;
1345 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1346 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1348 I DONT KNOW WHAT TO DO
1349 #endif /* !HAVE_PR_SYSCALL */
1352 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1353 sysent[tcp->scno].nargs > tcp->status.val)
1354 tcp->u_nargs = sysent[tcp->scno].nargs;
1356 tcp->u_nargs = tcp->status.val;
1357 if (tcp->u_nargs < 0)
1359 if (tcp->u_nargs > MAX_ARGS)
1360 tcp->u_nargs = MAX_ARGS;
1361 switch(regs.r_eax) {
1363 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1364 regs.r_esp + sizeof(int) + sizeof(quad_t));
1367 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1368 regs.r_esp + 2 * sizeof(int));
1371 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1372 regs.r_esp + sizeof(int));
1375 #endif /* FREEBSD */
1387 /* Measure the exit time as early as possible to avoid errors. */
1388 if (dtime && (tcp->flags & TCB_INSYSCALL))
1389 gettimeofday(&tv, NULL);
1391 res = get_scno(tcp);
1395 res = syscall_fixup(tcp);
1399 if (tcp->flags & TCB_INSYSCALL) {
1401 res = get_error(tcp);
1404 u_error = tcp->u_error;
1407 internal_syscall(tcp);
1408 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1409 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
1410 tcp->flags &= ~TCB_INSYSCALL;
1414 if (tcp->flags & TCB_REPRINT) {
1417 if (tcp->scno >= nsyscalls || tcp->scno < 0)
1418 tprintf("syscall_%lu", tcp->scno);
1420 tprintf("%s", sysent[tcp->scno].sys_name);
1421 tprintf(" resumed> ");
1424 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
1425 call_count[tcp->scno]++;
1427 error_count[tcp->scno]++;
1428 tv_sub(&tv, &tv, &tcp->etime);
1430 if (tv_cmp(&tv, &tcp->dtime) > 0) {
1431 static struct timeval one_tick =
1432 { 0, 1000000 / HZ };
1434 if (tv_nz(&tcp->dtime))
1436 else if (tv_cmp(&tv, &one_tick) > 0) {
1437 if (tv_cmp(&shortest, &one_tick) < 0)
1444 if (tv_cmp(&tv, &shortest) < 0)
1446 tv_add(&tv_count[tcp->scno],
1447 &tv_count[tcp->scno], &tv);
1448 tcp->flags &= ~TCB_INSYSCALL;
1452 if (tcp->scno >= nsyscalls || tcp->scno < 0
1453 || (qual_flags[tcp->scno] & QUAL_RAW))
1454 sys_res = printargs(tcp);
1456 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1457 u_error = tcp->u_error;
1460 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
1461 qual_flags[tcp->scno] & QUAL_RAW) {
1463 tprintf("= -1 (errno %ld)", u_error);
1465 tprintf("= %#lx", tcp->u_rval);
1467 else if (!(sys_res & RVAL_NONE) && u_error) {
1471 tprintf("= ? ERESTARTSYS (To be restarted)");
1473 case ERESTARTNOINTR:
1474 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1476 case ERESTARTNOHAND:
1477 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1482 if (u_error < nerrnos && u_error < sys_nerr)
1483 tprintf("%s (%s)", errnoent[u_error],
1484 sys_errlist[u_error]);
1485 else if (u_error < nerrnos)
1486 tprintf("%s (errno %ld)",
1487 errnoent[u_error], u_error);
1488 else if (u_error < sys_nerr)
1489 tprintf("ERRNO_%ld (%s)", u_error,
1490 sys_errlist[u_error]);
1492 tprintf("E??? (errno %ld)", u_error);
1497 if (sys_res & RVAL_NONE)
1500 switch (sys_res & RVAL_MASK) {
1502 tprintf("= %#lx", tcp->u_rval);
1505 tprintf("= %#lo", tcp->u_rval);
1508 tprintf("= %lu", tcp->u_rval);
1511 tprintf("= %ld", tcp->u_rval);
1513 #ifdef HAVE_LONG_LONG
1515 tprintf("= %#llx", tcp->u_lrval);
1518 tprintf("= %#llo", tcp->u_lrval);
1520 case RVAL_LUDECIMAL:
1521 tprintf("= %llu", tcp->u_lrval);
1524 tprintf("= %lld", tcp->u_lrval);
1529 "invalid rval format\n");
1533 if ((sys_res & RVAL_STR) && tcp->auxstr)
1534 tprintf(" (%s)", tcp->auxstr);
1537 tv_sub(&tv, &tv, &tcp->etime);
1538 tprintf(" <%ld.%06ld>",
1539 (long) tv.tv_sec, (long) tv.tv_usec);
1544 if (fflush(tcp->outf) == EOF)
1546 tcp->flags &= ~TCB_INSYSCALL;
1550 /* Entering system call */
1551 res = syscall_enter(tcp);
1555 switch (tcp->scno + NR_SYSCALL_BASE) {
1557 #if !defined (ALPHA) && !defined(IA64) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA)
1558 case SYS_socketcall:
1559 decode_subcall(tcp, SYS_socket_subcall,
1560 SYS_socket_nsubcalls, deref_style);
1563 decode_subcall(tcp, SYS_ipc_subcall,
1564 SYS_ipc_nsubcalls, shift_style);
1566 #endif /* !ALPHA && !IA64 && !MIPS && !SPARC */
1568 case SYS_socketcall:
1569 sparc_socket_decode (tcp);
1574 #ifdef SYS_pgrpsys_subcall
1576 decode_subcall(tcp, SYS_pgrpsys_subcall,
1577 SYS_pgrpsys_nsubcalls, shift_style);
1579 #endif /* SYS_pgrpsys_subcall */
1580 #ifdef SYS_sigcall_subcall
1582 decode_subcall(tcp, SYS_sigcall_subcall,
1583 SYS_sigcall_nsubcalls, mask_style);
1585 #endif /* SYS_sigcall_subcall */
1587 decode_subcall(tcp, SYS_msgsys_subcall,
1588 SYS_msgsys_nsubcalls, shift_style);
1591 decode_subcall(tcp, SYS_shmsys_subcall,
1592 SYS_shmsys_nsubcalls, shift_style);
1595 decode_subcall(tcp, SYS_semsys_subcall,
1596 SYS_semsys_nsubcalls, shift_style);
1600 decode_subcall(tcp, SYS_utssys_subcall,
1601 SYS_utssys_nsubcalls, shift_style);
1605 decode_subcall(tcp, SYS_sysfs_subcall,
1606 SYS_sysfs_nsubcalls, shift_style);
1609 decode_subcall(tcp, SYS_spcall_subcall,
1610 SYS_spcall_nsubcalls, shift_style);
1612 #ifdef SYS_context_subcall
1614 decode_subcall(tcp, SYS_context_subcall,
1615 SYS_context_nsubcalls, shift_style);
1617 #endif /* SYS_context_subcall */
1618 #ifdef SYS_door_subcall
1620 decode_subcall(tcp, SYS_door_subcall,
1621 SYS_door_nsubcalls, door_style);
1623 #endif /* SYS_door_subcall */
1624 #ifdef SYS_kaio_subcall
1626 decode_subcall(tcp, SYS_kaio_subcall,
1627 SYS_kaio_nsubcalls, shift_style);
1635 decode_subcall(tcp, 0, 0, table_style);
1640 decode_subcall(tcp, SYS_semsys_subcall,
1641 SYS_semsys_nsubcalls, shift_style);
1644 decode_subcall(tcp, SYS_msgsys_subcall,
1645 SYS_msgsys_nsubcalls, shift_style);
1648 decode_subcall(tcp, SYS_shmsys_subcall,
1649 SYS_shmsys_nsubcalls, shift_style);
1654 internal_syscall(tcp);
1655 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
1656 tcp->flags |= TCB_INSYSCALL;
1661 gettimeofday(&tcp->etime, NULL);
1662 tcp->flags |= TCB_INSYSCALL;
1667 tcp->flags &= ~TCB_REPRINT;
1669 if (tcp->scno >= nsyscalls || tcp->scno < 0)
1670 tprintf("syscall_%lu(", tcp->scno);
1672 tprintf("%s(", sysent[tcp->scno].sys_name);
1673 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
1674 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1675 sys_res = printargs(tcp);
1677 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1678 if (fflush(tcp->outf) == EOF)
1680 tcp->flags |= TCB_INSYSCALL;
1681 /* Measure the entrance time as late as possible to avoid errors. */
1683 gettimeofday(&tcp->etime, NULL);
1691 if (entering(tcp)) {
1694 for (i = 0; i < tcp->u_nargs; i++)
1695 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1709 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
1716 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1722 val = tcp->status.PR_REG[R_O1];
1725 val = tcp->status.PR_REG[EDX];
1728 val = tcp->status.PR_REG[CTX_V1];
1733 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
1740 * Apparently, indirect system calls have already be converted by ptrace(2),
1741 * so if you see "indir" this program has gone astray.
1749 if (entering(tcp)) {
1750 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1751 fprintf(stderr, "Bogus syscall: %u\n", scno);
1754 nargs = sysent[scno].nargs;
1755 tprintf("%s", sysent[scno].sys_name);
1756 for (i = 0; i < nargs; i++)
1757 tprintf(", %#lx", tcp->u_arg[i+1]);
1767 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1775 return strcmp(sysent[*((int *) a)].sys_name,
1776 sysent[*((int *) b)].sys_name);
1784 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1786 return (m < n) ? 1 : (m > n) ? -1 : 0;
1789 static int (*sortfun)();
1790 static struct timeval overhead = { -1, -1 };
1796 if (strcmp(sortby, "time") == 0)
1798 else if (strcmp(sortby, "calls") == 0)
1799 sortfun = count_cmp;
1800 else if (strcmp(sortby, "name") == 0)
1801 sortfun = syscall_cmp;
1802 else if (strcmp(sortby, "nothing") == 0)
1805 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1810 void set_overhead(n)
1813 overhead.tv_sec = n / 1000000;
1814 overhead.tv_usec = n % 1000000;
1822 int call_cum, error_cum;
1823 struct timeval tv_cum, dtv;
1825 char *dashes = "-------------------------";
1828 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1829 if (overhead.tv_sec == -1) {
1830 tv_mul(&overhead, &shortest, 8);
1831 tv_div(&overhead, &overhead, 10);
1833 for (i = 0; i < nsyscalls; i++) {
1834 sorted_count[i] = i;
1835 if (call_count[i] == 0)
1837 tv_mul(&dtv, &overhead, call_count[i]);
1838 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1839 call_cum += call_count[i];
1840 error_cum += error_count[i];
1841 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1844 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1845 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1846 "% time", "seconds", "usecs/call",
1847 "calls", "errors", "syscall");
1848 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1849 dashes, dashes, dashes, dashes, dashes, dashes);
1850 for (i = 0; i < nsyscalls; i++) {
1851 j = sorted_count[i];
1852 if (call_count[j] == 0)
1854 tv_div(&dtv, &tv_count[j], call_count[j]);
1856 sprintf(error_str, "%d", error_count[j]);
1858 error_str[0] = '\0';
1859 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1860 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1861 percent, (long) tv_count[j].tv_sec,
1862 (long) tv_count[j].tv_usec,
1863 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1864 call_count[j], error_str, sysent[j].sys_name);
1866 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1867 dashes, dashes, dashes, dashes, dashes, dashes);
1869 sprintf(error_str, "%d", error_cum);
1871 error_str[0] = '\0';
1872 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1873 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1874 call_cum, error_str, "total");