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>
49 #if HAVE_LINUX_PTRACE_H
51 #include <linux/ptrace.h>
56 #ifndef PTRACE_PEEKUSR
57 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
61 #if defined(LINUX) && defined(IA64)
62 # include <asm/ptrace_offsets.h>
66 #ifndef SYS_ERRLIST_DECLARED
68 extern char *sys_errlist[];
69 #endif /* SYS_ERRLIST_DECLARED */
71 #define NR_SYSCALL_BASE 0
74 #define ERESTARTSYS 512
76 #ifndef ERESTARTNOINTR
77 #define ERESTARTNOINTR 513
79 #ifndef ERESTARTNOHAND
80 #define ERESTARTNOHAND 514 /* restart if no handler.. */
83 #define ENOIOCTLCMD 515 /* No ioctl command */
91 #undef NR_SYSCALL_BASE
92 #define NR_SYSCALL_BASE __NR_SYSCALL_BASE
98 /* Define these shorthand notations to simplify the syscallent files. */
101 #define TN TRACE_NETWORK
102 #define TP TRACE_PROCESS
103 #define TS TRACE_SIGNAL
105 struct sysent sysent0[] = {
106 #include "syscallent.h"
108 int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
110 #if SUPPORTED_PERSONALITIES >= 2
111 struct sysent sysent1[] = {
112 #include "syscallent1.h"
114 int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
115 #endif /* SUPPORTED_PERSONALITIES >= 2 */
117 #if SUPPORTED_PERSONALITIES >= 3
118 struct sysent sysent2[] = {
119 #include "syscallent2.h"
121 int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
122 #endif /* SUPPORTED_PERSONALITIES >= 3 */
124 struct sysent *sysent;
127 /* Now undef them since short defines cause wicked namespace pollution. */
134 char *errnoent0[] = {
135 #include "errnoent.h"
137 int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
139 #if SUPPORTED_PERSONALITIES >= 2
140 char *errnoent1[] = {
141 #include "errnoent1.h"
143 int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
144 #endif /* SUPPORTED_PERSONALITIES >= 2 */
146 #if SUPPORTED_PERSONALITIES >= 3
147 char *errnoent2[] = {
148 #include "errnoent2.h"
150 int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
151 #endif /* SUPPORTED_PERSONALITIES >= 3 */
156 int current_personality;
159 set_personality(personality)
162 switch (personality) {
164 errnoent = errnoent0;
167 nsyscalls = nsyscalls0;
168 ioctlent = ioctlent0;
169 nioctlents = nioctlents0;
170 signalent = signalent0;
171 nsignals = nsignals0;
174 #if SUPPORTED_PERSONALITIES >= 2
176 errnoent = errnoent1;
179 nsyscalls = nsyscalls1;
180 ioctlent = ioctlent1;
181 nioctlents = nioctlents1;
182 signalent = signalent1;
183 nsignals = nsignals1;
185 #endif /* SUPPORTED_PERSONALITIES >= 2 */
187 #if SUPPORTED_PERSONALITIES >= 3
189 errnoent = errnoent2;
192 nsyscalls = nsyscalls2;
193 ioctlent = ioctlent2;
194 nioctlents = nioctlents2;
195 signalent = signalent2;
196 nsignals = nsignals2;
198 #endif /* SUPPORTED_PERSONALITIES >= 3 */
204 current_personality = personality;
208 int qual_flags[MAX_QUALS];
210 static int call_count[MAX_QUALS];
211 static int error_count[MAX_QUALS];
212 static struct timeval tv_count[MAX_QUALS];
213 static int sorted_count[MAX_QUALS];
215 static struct timeval shortest = { 1000000, 0 };
217 static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
219 static struct qual_options {
225 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
226 { QUAL_TRACE, "t", lookup_syscall, "system call" },
227 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
228 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
229 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
230 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
231 { QUAL_RAW, "raw", lookup_syscall, "system call" },
232 { QUAL_RAW, "x", lookup_syscall, "system call" },
233 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
234 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
235 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
236 { QUAL_FAULT, "fault", lookup_fault, "fault" },
237 { QUAL_FAULT, "faults", lookup_fault, "fault" },
238 { QUAL_FAULT, "m", lookup_fault, "fault" },
239 { QUAL_READ, "read", lookup_desc, "descriptor" },
240 { QUAL_READ, "reads", lookup_desc, "descriptor" },
241 { QUAL_READ, "r", lookup_desc, "descriptor" },
242 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
243 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
244 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
245 { 0, NULL, NULL, NULL },
254 for (i = 0; i < nsyscalls; i++) {
255 if (strcmp(s, sysent[i].sys_name) == 0)
268 if (s && *s && isdigit(*s))
272 for (i = 0; s[i]; i++)
273 s[i] = toupper(s[i]);
274 if (strncmp(s, "SIG", 3) == 0)
276 for (i = 0; i <= NSIG; i++) {
277 if (strcmp(s, signame(i) + 3) == 0)
294 if (s && *s && isdigit(*s))
303 if (strcmp(s, "file") == 0)
305 if (strcmp(s, "ipc") == 0)
307 if (strcmp(s, "network") == 0)
308 return TRACE_NETWORK;
309 if (strcmp(s, "process") == 0)
310 return TRACE_PROCESS;
311 if (strcmp(s, "signal") == 0)
320 struct qual_options *opt;
325 opt = &qual_options[0];
326 for (i = 0; (p = qual_options[i].option_name); i++) {
328 if (strncmp(s, p, n) == 0 && s[n] == '=') {
329 opt = &qual_options[i];
339 if (strcmp(s, "none") == 0) {
343 if (strcmp(s, "all") == 0) {
344 for (i = 0; i < MAX_QUALS; i++) {
346 qual_flags[i] &= ~opt->bitflag;
348 qual_flags[i] |= opt->bitflag;
352 for (i = 0; i < MAX_QUALS; i++) {
354 qual_flags[i] |= opt->bitflag;
356 qual_flags[i] &= ~opt->bitflag;
358 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
359 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
360 for (i = 0; i < MAX_QUALS; i++) {
361 if (sysent[i].sys_flags & n) {
363 qual_flags[i] &= ~opt->bitflag;
365 qual_flags[i] |= opt->bitflag;
370 if ((n = (*opt->lookup)(p)) < 0) {
371 fprintf(stderr, "strace: invalid %s `%s'\n",
372 opt->argument_name, p);
376 qual_flags[n] &= ~opt->bitflag;
378 qual_flags[n] |= opt->bitflag;
389 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
391 switch (tcp->scno + NR_SYSCALL_BASE) {
399 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
400 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
409 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
410 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
415 enum subcall_style { shift_style, deref_style, mask_style, door_style };
417 #if !(defined(LINUX) && ( defined(ALPHA) || defined(IA64) || defined(MIPS) ))
419 const int socket_map [] = {
422 /* SYS_CONNECT */ 98,
423 /* SYS_LISTEN */ 106,
425 /* SYS_GETSOCKNAME */ 150,
426 /* SYS_GETPEERNAME */ 141,
427 /* SYS_SOCKETPAIR */ 135,
430 /* SYS_SENDTO */ 133,
431 /* SYS_RECVFROM */ 125,
432 /* SYS_SHUTDOWN */ 134,
433 /* SYS_SETSOCKOPT */ 105,
434 /* SYS_GETSOCKOPT */ 118,
435 /* SYS_SENDMSG */ 114,
436 /* SYS_RECVMSG */ 113
440 sparc_socket_decode (tcp)
446 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
449 tcp->scno = socket_map [tcp->u_arg [0]-1];
450 n = tcp->u_nargs = sysent [tcp->scno].nargs;
451 addr = tcp->u_arg [1];
452 for (i = 0; i < n; i++){
454 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
456 tcp->u_arg [i] = arg;
457 addr += sizeof (arg);
462 decode_subcall(tcp, subcall, nsubcalls, style)
466 enum subcall_style style;
468 int i, addr, mask, arg;
470 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
474 tcp->scno = subcall + tcp->u_arg[0];
475 if (sysent[tcp->scno].nargs != -1)
476 tcp->u_nargs = sysent[tcp->scno].nargs;
479 for (i = 0; i < tcp->u_nargs; i++)
480 tcp->u_arg[i] = tcp->u_arg[i + 1];
483 tcp->scno = subcall + tcp->u_arg[0];
484 addr = tcp->u_arg[1];
485 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
486 if (umove(tcp, addr, &arg) < 0)
491 tcp->u_nargs = sysent[tcp->scno].nargs;
494 mask = (tcp->u_arg[0] >> 8) & 0xff;
495 tcp->u_arg[0] &= 0xff;
496 for (i = 0; mask; i++)
498 tcp->scno = subcall + i;
499 if (sysent[tcp->scno].nargs != -1)
500 tcp->u_nargs = sysent[tcp->scno].nargs;
504 * Oh, yuck. The call code is the *sixth* argument.
506 tcp->scno = subcall + tcp->u_arg[5];
507 if (sysent[tcp->scno].nargs != -1)
508 tcp->u_nargs = sysent[tcp->scno].nargs;
516 struct tcb *tcp_last = NULL;
519 internal_syscall(tcp)
523 * We must always trace a few critical system calls in order to
524 * correctly support following forks in the presence of tracing
527 switch (tcp->scno + NR_SYSCALL_BASE) {
583 if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
585 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
589 #elif defined (POWERPC)
590 if (upeek(pid, 4*PT_R0, &scno) < 0)
592 if (!(tcp->flags & TCB_INSYSCALL)) {
593 /* Check if we return from execve. */
594 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
595 tcp->flags &= ~TCB_WAITEXECVE;
600 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
603 #define IA64_PSR_IS ((long)1 << 34)
604 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
605 ia32 = (psr & IA64_PSR_IS);
606 if (!(tcp->flags & TCB_INSYSCALL)) {
608 if (upeek(pid, PT_R8, &scno) < 0)
611 if (upeek (pid, PT_R15, &scno) < 0)
615 /* syscall in progress */
616 if (upeek (pid, PT_R8, &r8) < 0)
618 if (upeek (pid, PT_R10, &r10) < 0)
624 upeek(pid, 4*15, &pc);
625 umoven(tcp, pc-4, 4, (char *)&scno);
629 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
632 if (upeek(pid, REG_A3, &a3) < 0)
635 if(!(tcp->flags & TCB_INSYSCALL)) {
636 if (upeek(pid, REG_V0, &scno) < 0)
639 if (scno < 0 || scno > nsyscalls) {
640 if(a3 == 0 || a3 == -1) {
642 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
647 if (upeek(pid, REG_V0, &r2) < 0)
650 #elif defined (ALPHA)
651 if (upeek(pid, REG_A3, &a3) < 0)
654 if (!(tcp->flags & TCB_INSYSCALL)) {
655 if (upeek(pid, REG_R0, &scno) < 0)
658 /* Check if we return from execve. */
659 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
660 tcp->flags &= ~TCB_WAITEXECVE;
665 * Do some sanity checks to figure out if it's
666 * really a syscall entry
668 if (scno < 0 || scno > nsyscalls) {
669 if (a3 == 0 || a3 == -1) {
671 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
677 if (upeek(pid, REG_R0, &r0) < 0)
680 #elif defined (SPARC)
681 /* Everything we need is in the current register set. */
682 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
685 /* If we are entering, then disassemble the syscall trap. */
686 if (!(tcp->flags & TCB_INSYSCALL)) {
687 /* Retrieve the syscall trap instruction. */
689 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
693 /* Disassemble the trap to see what personality to use. */
696 /* Linux/SPARC syscall trap. */
700 /* Linux/SPARC64 syscall trap. */
701 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
704 /* SunOS syscall trap. (pers 1) */
705 fprintf(stderr,"syscall: SunOS no support\n");
708 /* Solaris 2.x syscall trap. (per 2) */
712 /* NetBSD/FreeBSD syscall trap. */
713 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
716 /* Solaris 2.x gettimeofday */
720 /* Unknown syscall trap. */
721 if(tcp->flags & TCB_WAITEXECVE) {
722 tcp->flags &= ~TCB_WAITEXECVE;
725 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
729 /* Extract the system call number from the registers. */
730 if (trap == 0x91d02027)
736 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
742 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
746 #ifdef HAVE_PR_SYSCALL
747 scno = tcp->status.pr_syscall;
748 #else /* !HAVE_PR_SYSCALL */
749 scno = tcp->status.PR_WHAT;
750 #endif /* !HAVE_PR_SYSCALL */
762 #elif defined (POWERPC)
763 static long result,flags;
768 #elif defined (ALPHA)
771 #elif defined (SPARC)
772 static struct pt_regs regs;
773 static unsigned long trap;
787 if (!(tcp->flags & TCB_INSYSCALL)) {
788 if (tcp->status.PR_WHY != PR_SYSENTRY) {
793 #endif /* SYS_vfork */
795 /* We are returning in the child, fake it. */
796 tcp->status.PR_WHY = PR_SYSENTRY;
798 tcp->status.PR_WHY = PR_SYSEXIT;
801 fprintf(stderr, "syscall: missing entry\n");
802 tcp->flags |= TCB_INSYSCALL;
807 if (tcp->status.PR_WHY != PR_SYSEXIT) {
808 fprintf(stderr, "syscall: missing exit\n");
809 tcp->flags &= ~TCB_INSYSCALL;
814 if (!(tcp->flags & TCB_INSYSCALL)) {
816 fprintf(stderr, "syscall: missing entry\n");
817 tcp->flags |= TCB_INSYSCALL;
824 * This happens when a signal handler
825 * for a signal which interrupted a
826 * a system call makes another system call.
828 fprintf(stderr, "syscall: missing exit\n");
830 tcp->flags &= ~TCB_INSYSCALL;
836 if (upeek(pid, 4*EAX, &eax) < 0)
838 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
840 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
843 #elif defined (POWERPC)
844 # define SO_MASK 0x10000000
845 if (upeek(pid, 4*PT_CCR, &flags) < 0)
847 if (upeek(pid, 4*PT_R3, &result) < 0)
852 if (upeek(pid, 4*PT_D0, &d0) < 0)
854 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
856 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
860 if (upeek(pid, 4*0, (long *)&r0) < 0)
862 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
864 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
880 if (eax < 0 && -eax < nerrnos) {
894 if (err < 0 && -err < nerrnos) {
922 if (result && (unsigned) -result < nerrnos) {
927 tcp->u_rval = result;
932 if (d0 && (unsigned) -d0 < nerrnos) {
942 if (r0 && (unsigned) -r0 < nerrnos) {
962 if (regs.r_psr & PSR_C) {
967 tcp->u_rval = regs.r_o0;
980 /* get error code from user struct */
981 if (upeek(pid, uoff(u_error), &u_error) < 0)
983 u_error >>= 24; /* u_error is a char */
985 /* get system call return value */
986 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
991 /* Judicious guessing goes a long way. */
992 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
994 u_error = tcp->status.pr_reg[R_O0];
997 tcp->u_rval = tcp->status.pr_reg[R_O0];
1002 /* Wanna know how to kill an hour single-stepping? */
1003 if (tcp->status.PR_REG[EFL] & 0x1) {
1005 u_error = tcp->status.PR_REG[EAX];
1008 tcp->u_rval = tcp->status.PR_REG[EAX];
1013 if (tcp->status.pr_reg[CTX_A3]) {
1015 u_error = tcp->status.pr_reg[CTX_V0];
1018 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1023 tcp->u_error = u_error;
1027 int syscall_enter(tcp)
1035 tcp->u_nargs = sysent[tcp->scno].nargs;
1036 for (i = 0; i < tcp->u_nargs; i++) {
1037 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
1041 #elif defined (ALPHA)
1044 tcp->u_nargs = sysent[tcp->scno].nargs;
1045 for (i = 0; i < tcp->u_nargs; i++) {
1046 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1047 * for scno somewhere above here!
1049 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1053 #elif defined (IA64)
1055 unsigned long *bsp, i;
1057 if (upeek(pid, PT_AR_BSP, (long *) &bsp) < 0)
1060 tcp->u_nargs = sysent[tcp->scno].nargs;
1061 for (i = 0; i < tcp->u_nargs; ++i) {
1062 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(bsp, i), sizeof(long),
1063 (char *) &tcp->u_arg[i])
1068 #elif defined (MIPS)
1073 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1075 if(upeek(pid, REG_SP, &sp) < 0)
1077 for(i = 0; i < 4; i++) {
1078 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1081 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1082 (char *)(tcp->u_arg + 4));
1084 for(i = 0; i < nargs; i++) {
1085 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1090 #elif defined (POWERPC)
1093 tcp->u_nargs = sysent[tcp->scno].nargs;
1094 for (i = 0; i < tcp->u_nargs; i++) {
1095 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1099 #elif defined (SPARC)
1103 tcp->u_nargs = sysent[tcp->scno].nargs;
1104 for (i = 0; i < tcp->u_nargs; i++)
1105 tcp->u_arg[i] = *((®s.r_o0) + i);
1110 tcp->u_nargs = sysent[tcp->scno].nargs;
1111 for (i = 0; i < tcp->u_nargs; i++) {
1112 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1121 tcp->u_nargs = sysent[tcp->scno].nargs;
1122 for (i = 0; i < tcp->u_nargs; i++) {
1125 if (upeek(pid, uoff(u_arg[0]) +
1126 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1134 * SGI is broken: even though it has pr_sysarg, it doesn't
1135 * set them on system call entry. Get a clue.
1137 if (sysent[tcp->scno].nargs != -1)
1138 tcp->u_nargs = sysent[tcp->scno].nargs;
1140 tcp->u_nargs = tcp->status.pr_nsysarg;
1141 if (tcp->u_nargs > 4) {
1142 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1143 4*sizeof(tcp->u_arg[0]));
1144 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1145 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1148 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1149 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1152 #ifdef HAVE_PR_SYSCALL
1153 if (sysent[tcp->scno].nargs != -1)
1154 tcp->u_nargs = sysent[tcp->scno].nargs;
1156 tcp->u_nargs = tcp->status.pr_nsysarg;
1159 for (i = 0; i < tcp->u_nargs; i++)
1160 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1162 #else /* !HAVE_PR_SYSCALL */
1164 if (sysent[tcp->scno].nargs != -1)
1165 tcp->u_nargs = sysent[tcp->scno].nargs;
1168 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1172 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1173 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1175 #endif /* !HAVE_PR_SYSCALL */
1189 /* Measure the exit time as early as possible to avoid errors. */
1190 if (dtime && (tcp->flags & TCB_INSYSCALL))
1191 gettimeofday(&tv, NULL);
1193 res = get_scno(tcp);
1197 res = syscall_fixup(tcp);
1201 if (tcp->flags & TCB_INSYSCALL) {
1203 res = get_error(tcp);
1206 u_error = tcp->u_error;
1209 internal_syscall(tcp);
1210 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1211 tcp->flags &= ~TCB_INSYSCALL;
1215 if (tcp->flags & TCB_REPRINT) {
1218 if (tcp->scno >= nsyscalls)
1219 tprintf("syscall_%lu", tcp->scno);
1221 tprintf("%s", sysent[tcp->scno].sys_name);
1222 tprintf(" resumed> ");
1226 call_count[tcp->scno]++;
1228 error_count[tcp->scno]++;
1229 tv_sub(&tv, &tv, &tcp->etime);
1231 if (tv_cmp(&tv, &tcp->dtime) > 0) {
1232 static struct timeval one_tick =
1233 { 0, 1000000 / HZ };
1235 if (tv_nz(&tcp->dtime))
1237 else if (tv_cmp(&tv, &one_tick) > 0) {
1238 if (tv_cmp(&shortest, &one_tick) < 0)
1245 if (tv_cmp(&tv, &shortest) < 0)
1247 tv_add(&tv_count[tcp->scno],
1248 &tv_count[tcp->scno], &tv);
1249 tcp->flags &= ~TCB_INSYSCALL;
1253 if (tcp->scno >= nsyscalls
1254 || (qual_flags[tcp->scno] & QUAL_RAW))
1255 sys_res = printargs(tcp);
1257 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1258 u_error = tcp->u_error;
1261 if (qual_flags[tcp->scno] & QUAL_RAW) {
1263 tprintf("= -1 (errno %ld)", u_error);
1265 tprintf("= %#lx", tcp->u_rval);
1267 else if (!(sys_res & RVAL_NONE) && u_error) {
1271 tprintf("= ? ERESTARTSYS (To be restarted)");
1273 case ERESTARTNOINTR:
1274 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1276 case ERESTARTNOHAND:
1277 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1282 if (u_error < nerrnos && u_error < sys_nerr)
1283 tprintf("%s (%s)", errnoent[u_error],
1284 sys_errlist[u_error]);
1285 else if (u_error < nerrnos)
1286 tprintf("%s (errno %ld)",
1287 errnoent[u_error], u_error);
1288 else if (u_error < sys_nerr)
1289 tprintf("ERRNO_%ld (%s)", u_error,
1290 sys_errlist[u_error]);
1292 tprintf("E??? (errno %ld)", u_error);
1297 if (sys_res & RVAL_NONE)
1300 switch (sys_res & RVAL_MASK) {
1302 tprintf("= %#lx", tcp->u_rval);
1305 tprintf("= %#lo", tcp->u_rval);
1308 tprintf("= %lu", tcp->u_rval);
1311 tprintf("= %ld", tcp->u_rval);
1315 "invalid rval format\n");
1319 if ((sys_res & RVAL_STR) && tcp->auxstr)
1320 tprintf(" (%s)", tcp->auxstr);
1323 tv_sub(&tv, &tv, &tcp->etime);
1324 tprintf(" <%ld.%06ld>",
1325 (long) tv.tv_sec, (long) tv.tv_usec);
1330 if (fflush(tcp->outf) == EOF)
1332 tcp->flags &= ~TCB_INSYSCALL;
1336 /* Entering system call */
1337 res = syscall_enter(tcp);
1341 switch (tcp->scno + NR_SYSCALL_BASE) {
1343 #if !defined (ALPHA) && !defined(IA64) && !defined(SPARC) && !defined(MIPS)
1344 case SYS_socketcall:
1345 decode_subcall(tcp, SYS_socket_subcall,
1346 SYS_socket_nsubcalls, deref_style);
1349 decode_subcall(tcp, SYS_ipc_subcall,
1350 SYS_ipc_nsubcalls, shift_style);
1352 #endif /* !ALPHA && !SPARC */
1354 case SYS_socketcall:
1355 sparc_socket_decode (tcp);
1360 #ifdef SYS_pgrpsys_subcall
1362 decode_subcall(tcp, SYS_pgrpsys_subcall,
1363 SYS_pgrpsys_nsubcalls, shift_style);
1365 #endif /* SYS_pgrpsys_subcall */
1366 #ifdef SYS_sigcall_subcall
1368 decode_subcall(tcp, SYS_sigcall_subcall,
1369 SYS_sigcall_nsubcalls, mask_style);
1371 #endif /* SYS_sigcall_subcall */
1373 decode_subcall(tcp, SYS_msgsys_subcall,
1374 SYS_msgsys_nsubcalls, shift_style);
1377 decode_subcall(tcp, SYS_shmsys_subcall,
1378 SYS_shmsys_nsubcalls, shift_style);
1381 decode_subcall(tcp, SYS_semsys_subcall,
1382 SYS_semsys_nsubcalls, shift_style);
1386 decode_subcall(tcp, SYS_utssys_subcall,
1387 SYS_utssys_nsubcalls, shift_style);
1391 decode_subcall(tcp, SYS_sysfs_subcall,
1392 SYS_sysfs_nsubcalls, shift_style);
1395 decode_subcall(tcp, SYS_spcall_subcall,
1396 SYS_spcall_nsubcalls, shift_style);
1398 #ifdef SYS_context_subcall
1400 decode_subcall(tcp, SYS_context_subcall,
1401 SYS_context_nsubcalls, shift_style);
1403 #endif /* SYS_context_subcall */
1404 #ifdef SYS_door_subcall
1406 decode_subcall(tcp, SYS_door_subcall,
1407 SYS_door_nsubcalls, door_style);
1409 #endif /* SYS_door_subcall */
1413 decode_subcall(tcp, SYS_semsys_subcall,
1414 SYS_semsys_nsubcalls, shift_style);
1417 decode_subcall(tcp, SYS_msgsys_subcall,
1418 SYS_msgsys_nsubcalls, shift_style);
1421 decode_subcall(tcp, SYS_shmsys_subcall,
1422 SYS_shmsys_nsubcalls, shift_style);
1427 internal_syscall(tcp);
1428 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1429 tcp->flags |= TCB_INSYSCALL;
1434 gettimeofday(&tcp->etime, NULL);
1435 tcp->flags |= TCB_INSYSCALL;
1440 tcp->flags &= ~TCB_REPRINT;
1442 if (tcp->scno >= nsyscalls)
1443 tprintf("syscall_%lu(", tcp->scno);
1445 tprintf("%s(", sysent[tcp->scno].sys_name);
1446 if (tcp->scno >= nsyscalls ||
1447 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1448 sys_res = printargs(tcp);
1450 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1451 if (fflush(tcp->outf) == EOF)
1453 tcp->flags |= TCB_INSYSCALL;
1454 /* Measure the entrance time as late as possible to avoid errors. */
1456 gettimeofday(&tcp->etime, NULL);
1464 if (entering(tcp)) {
1467 for (i = 0; i < tcp->u_nargs; i++)
1468 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1482 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
1489 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1495 val = tcp->status.PR_REG[R_O1];
1498 val = tcp->status.PR_REG[EDX];
1501 val = tcp->status.PR_REG[CTX_V1];
1509 * Apparently, indirect system calls have already be converted by ptrace(2),
1510 * so if you see "indir" this program has gone astray.
1518 if (entering(tcp)) {
1519 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1520 fprintf(stderr, "Bogus syscall: %u\n", scno);
1523 nargs = sysent[scno].nargs;
1524 tprintf("%s", sysent[scno].sys_name);
1525 for (i = 0; i < nargs; i++)
1526 tprintf(", %#lx", tcp->u_arg[i+1]);
1536 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1544 return strcmp(sysent[*((int *) a)].sys_name,
1545 sysent[*((int *) b)].sys_name);
1553 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1555 return (m < n) ? 1 : (m > n) ? -1 : 0;
1558 static int (*sortfun)();
1559 static struct timeval overhead = { -1, -1 };
1565 if (strcmp(sortby, "time") == 0)
1567 else if (strcmp(sortby, "calls") == 0)
1568 sortfun = count_cmp;
1569 else if (strcmp(sortby, "name") == 0)
1570 sortfun = syscall_cmp;
1571 else if (strcmp(sortby, "nothing") == 0)
1574 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1579 void set_overhead(n)
1582 overhead.tv_sec = n / 1000000;
1583 overhead.tv_usec = n % 1000000;
1591 int call_cum, error_cum;
1592 struct timeval tv_cum, dtv;
1594 char *dashes = "-------------------------";
1597 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1598 if (overhead.tv_sec == -1) {
1599 tv_mul(&overhead, &shortest, 8);
1600 tv_div(&overhead, &overhead, 10);
1602 for (i = 0; i < nsyscalls; i++) {
1603 sorted_count[i] = i;
1604 if (call_count[i] == 0)
1606 tv_mul(&dtv, &overhead, call_count[i]);
1607 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1608 call_cum += call_count[i];
1609 error_cum += error_count[i];
1610 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1613 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1614 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1615 "% time", "seconds", "usecs/call",
1616 "calls", "errors", "syscall");
1617 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1618 dashes, dashes, dashes, dashes, dashes, dashes);
1619 for (i = 0; i < nsyscalls; i++) {
1620 j = sorted_count[i];
1621 if (call_count[j] == 0)
1623 tv_div(&dtv, &tv_count[j], call_count[j]);
1625 sprintf(error_str, "%d", error_count[j]);
1627 error_str[0] = '\0';
1628 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1629 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1630 percent, (long) tv_count[j].tv_sec,
1631 (long) tv_count[j].tv_usec,
1632 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1633 call_count[j], error_str, sysent[j].sys_name);
1635 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1636 dashes, dashes, dashes, dashes, dashes, dashes);
1638 sprintf(error_str, "%d", error_cum);
1640 error_str[0] = '\0';
1641 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1642 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1643 call_cum, error_str, "total");