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 #ifndef SYS_ERRLIST_DECLARED
63 extern char *sys_errlist[];
64 #endif /* SYS_ERRLIST_DECLARED */
66 #define NR_SYSCALL_BASE 0
69 #define ERESTARTSYS 512
71 #ifndef ERESTARTNOINTR
72 #define ERESTARTNOINTR 513
74 #ifndef ERESTARTNOHAND
75 #define ERESTARTNOHAND 514 /* restart if no handler.. */
78 #define ENOIOCTLCMD 515 /* No ioctl command */
86 #undef NR_SYSCALL_BASE
87 #define NR_SYSCALL_BASE __NR_SYSCALL_BASE
93 /* Define these shorthand notations to simplify the syscallent files. */
96 #define TN TRACE_NETWORK
97 #define TP TRACE_PROCESS
98 #define TS TRACE_SIGNAL
100 struct sysent sysent0[] = {
101 #include "syscallent.h"
103 int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
105 #if SUPPORTED_PERSONALITIES >= 2
106 struct sysent sysent1[] = {
107 #include "syscallent1.h"
109 int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
110 #endif /* SUPPORTED_PERSONALITIES >= 2 */
112 #if SUPPORTED_PERSONALITIES >= 3
113 struct sysent sysent2[] = {
114 #include "syscallent2.h"
116 int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
117 #endif /* SUPPORTED_PERSONALITIES >= 3 */
119 struct sysent *sysent;
122 /* Now undef them since short defines cause wicked namespace pollution. */
129 char *errnoent0[] = {
130 #include "errnoent.h"
132 int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
134 #if SUPPORTED_PERSONALITIES >= 2
135 char *errnoent1[] = {
136 #include "errnoent1.h"
138 int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
139 #endif /* SUPPORTED_PERSONALITIES >= 2 */
141 #if SUPPORTED_PERSONALITIES >= 3
142 char *errnoent2[] = {
143 #include "errnoent2.h"
145 int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
146 #endif /* SUPPORTED_PERSONALITIES >= 3 */
151 int current_personality;
154 set_personality(personality)
157 switch (personality) {
159 errnoent = errnoent0;
162 nsyscalls = nsyscalls0;
163 ioctlent = ioctlent0;
164 nioctlents = nioctlents0;
165 signalent = signalent0;
166 nsignals = nsignals0;
169 #if SUPPORTED_PERSONALITIES >= 2
171 errnoent = errnoent1;
174 nsyscalls = nsyscalls1;
175 ioctlent = ioctlent1;
176 nioctlents = nioctlents1;
177 signalent = signalent1;
178 nsignals = nsignals1;
180 #endif /* SUPPORTED_PERSONALITIES >= 2 */
182 #if SUPPORTED_PERSONALITIES >= 3
184 errnoent = errnoent2;
187 nsyscalls = nsyscalls2;
188 ioctlent = ioctlent2;
189 nioctlents = nioctlents2;
190 signalent = signalent2;
191 nsignals = nsignals2;
193 #endif /* SUPPORTED_PERSONALITIES >= 3 */
199 current_personality = personality;
203 int qual_flags[MAX_QUALS];
205 static int call_count[MAX_QUALS];
206 static int error_count[MAX_QUALS];
207 static struct timeval tv_count[MAX_QUALS];
208 static int sorted_count[MAX_QUALS];
210 static struct timeval shortest = { 1000000, 0 };
212 static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
214 static struct qual_options {
220 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
221 { QUAL_TRACE, "t", lookup_syscall, "system call" },
222 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
223 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
224 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
225 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
226 { QUAL_RAW, "raw", lookup_syscall, "system call" },
227 { QUAL_RAW, "x", lookup_syscall, "system call" },
228 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
229 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
230 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
231 { QUAL_FAULT, "fault", lookup_fault, "fault" },
232 { QUAL_FAULT, "faults", lookup_fault, "fault" },
233 { QUAL_FAULT, "m", lookup_fault, "fault" },
234 { QUAL_READ, "read", lookup_desc, "descriptor" },
235 { QUAL_READ, "reads", lookup_desc, "descriptor" },
236 { QUAL_READ, "r", lookup_desc, "descriptor" },
237 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
238 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
239 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
240 { 0, NULL, NULL, NULL },
249 for (i = 0; i < nsyscalls; i++) {
250 if (strcmp(s, sysent[i].sys_name) == 0)
263 if (s && *s && isdigit(*s))
267 for (i = 0; s[i]; i++)
268 s[i] = toupper(s[i]);
269 if (strncmp(s, "SIG", 3) == 0)
271 for (i = 0; i <= NSIG; i++) {
272 if (strcmp(s, signame(i) + 3) == 0)
289 if (s && *s && isdigit(*s))
298 if (strcmp(s, "file") == 0)
300 if (strcmp(s, "ipc") == 0)
302 if (strcmp(s, "network") == 0)
303 return TRACE_NETWORK;
304 if (strcmp(s, "process") == 0)
305 return TRACE_PROCESS;
306 if (strcmp(s, "signal") == 0)
315 struct qual_options *opt;
320 opt = &qual_options[0];
321 for (i = 0; (p = qual_options[i].option_name); i++) {
323 if (strncmp(s, p, n) == 0 && s[n] == '=') {
324 opt = &qual_options[i];
334 if (strcmp(s, "none") == 0) {
338 if (strcmp(s, "all") == 0) {
339 for (i = 0; i < MAX_QUALS; i++) {
341 qual_flags[i] &= ~opt->bitflag;
343 qual_flags[i] |= opt->bitflag;
347 for (i = 0; i < MAX_QUALS; i++) {
349 qual_flags[i] |= opt->bitflag;
351 qual_flags[i] &= ~opt->bitflag;
353 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
354 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
355 for (i = 0; i < MAX_QUALS; i++) {
356 if (sysent[i].sys_flags & n) {
358 qual_flags[i] &= ~opt->bitflag;
360 qual_flags[i] |= opt->bitflag;
365 if ((n = (*opt->lookup)(p)) < 0) {
366 fprintf(stderr, "strace: invalid %s `%s'\n",
367 opt->argument_name, p);
371 qual_flags[n] &= ~opt->bitflag;
373 qual_flags[n] |= opt->bitflag;
384 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
386 switch (tcp->scno + NR_SYSCALL_BASE) {
394 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
395 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
404 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
405 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
410 enum subcall_style { shift_style, deref_style, mask_style, door_style };
412 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
414 const int socket_map [] = {
417 /* SYS_CONNECT */ 98,
418 /* SYS_LISTEN */ 106,
420 /* SYS_GETSOCKNAME */ 150,
421 /* SYS_GETPEERNAME */ 141,
422 /* SYS_SOCKETPAIR */ 135,
425 /* SYS_SENDTO */ 133,
426 /* SYS_RECVFROM */ 125,
427 /* SYS_SHUTDOWN */ 134,
428 /* SYS_SETSOCKOPT */ 105,
429 /* SYS_GETSOCKOPT */ 118,
430 /* SYS_SENDMSG */ 114,
431 /* SYS_RECVMSG */ 113
435 sparc_socket_decode (tcp)
441 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
444 tcp->scno = socket_map [tcp->u_arg [0]-1];
445 n = tcp->u_nargs = sysent [tcp->scno].nargs;
446 addr = tcp->u_arg [1];
447 for (i = 0; i < n; i++){
449 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
451 tcp->u_arg [i] = arg;
452 addr += sizeof (arg);
457 decode_subcall(tcp, subcall, nsubcalls, style)
461 enum subcall_style style;
463 int i, addr, mask, arg;
465 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
469 tcp->scno = subcall + tcp->u_arg[0];
470 if (sysent[tcp->scno].nargs != -1)
471 tcp->u_nargs = sysent[tcp->scno].nargs;
474 for (i = 0; i < tcp->u_nargs; i++)
475 tcp->u_arg[i] = tcp->u_arg[i + 1];
478 tcp->scno = subcall + tcp->u_arg[0];
479 addr = tcp->u_arg[1];
480 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
481 if (umove(tcp, addr, &arg) < 0)
486 tcp->u_nargs = sysent[tcp->scno].nargs;
489 mask = (tcp->u_arg[0] >> 8) & 0xff;
490 tcp->u_arg[0] &= 0xff;
491 for (i = 0; mask; i++)
493 tcp->scno = subcall + i;
494 if (sysent[tcp->scno].nargs != -1)
495 tcp->u_nargs = sysent[tcp->scno].nargs;
499 * Oh, yuck. The call code is the *sixth* argument.
501 tcp->scno = subcall + tcp->u_arg[5];
502 if (sysent[tcp->scno].nargs != -1)
503 tcp->u_nargs = sysent[tcp->scno].nargs;
511 struct tcb *tcp_last = NULL;
514 internal_syscall(tcp)
518 * We must always trace a few critical system calls in order to
519 * correctly support following forks in the presence of tracing
522 switch (tcp->scno + NR_SYSCALL_BASE) {
579 #elif defined (POWERPC)
585 #elif defined (ALPHA)
590 #elif defined (SPARC)
603 /* Measure the exit time as early as possible to avoid errors. */
604 if (dtime && (tcp->flags & TCB_INSYSCALL))
605 gettimeofday(&tv, NULL);
608 if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
610 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
614 #elif defined (POWERPC)
615 if (upeek(pid, 4*PT_R0, &scno) < 0)
617 if (!(tcp->flags & TCB_INSYSCALL)) {
618 /* Check if we return from execve. */
619 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
620 tcp->flags &= ~TCB_WAITEXECVE;
625 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
630 upeek(pid, 4*15, &pc);
631 umoven(tcp, pc-4, 4, (char *)&scno);
635 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
638 if (upeek(pid, REG_A3, &a3) < 0)
641 if(!(tcp->flags & TCB_INSYSCALL)) {
642 if (upeek(pid, REG_V0, &scno) < 0)
645 if (scno < 0 || scno > nsyscalls) {
646 if(a3 == 0 || a3 == -1) {
648 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
653 if (upeek(pid, REG_V0, &r2) < 0)
656 #elif defined (ALPHA)
657 if (upeek(pid, REG_A3, &a3) < 0)
660 if (!(tcp->flags & TCB_INSYSCALL)) {
661 if (upeek(pid, REG_R0, &scno) < 0)
664 /* Check if we return from execve. */
665 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
666 tcp->flags &= ~TCB_WAITEXECVE;
671 * Do some sanity checks to figure out if it's
672 * really a syscall entry
674 if (scno < 0 || scno > nsyscalls) {
675 if (a3 == 0 || a3 == -1) {
677 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
683 if (upeek(pid, REG_R0, &r0) < 0)
686 #elif defined (SPARC)
687 /* Everything we need is in the current register set. */
688 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
691 /* If we are entering, then disassemble the syscall trap. */
692 if (!(tcp->flags & TCB_INSYSCALL)) {
693 /* Retrieve the syscall trap instruction. */
695 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
699 /* Disassemble the trap to see what personality to use. */
702 /* Linux/SPARC syscall trap. */
706 /* Linux/SPARC64 syscall trap. */
707 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
710 /* SunOS syscall trap. (pers 1) */
711 fprintf(stderr,"syscall: SunOS no support\n");
714 /* Solaris 2.x syscall trap. (per 2) */
718 /* NetBSD/FreeBSD syscall trap. */
719 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
722 /* Solaris 2.x gettimeofday */
726 /* Unknown syscall trap. */
727 if(tcp->flags & TCB_WAITEXECVE) {
728 tcp->flags &= ~TCB_WAITEXECVE;
731 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
735 /* Extract the system call number from the registers. */
736 if (trap == 0x91d02027)
742 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
748 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
752 #ifdef HAVE_PR_SYSCALL
753 scno = tcp->status.pr_syscall;
754 #else /* !HAVE_PR_SYSCALL */
755 scno = tcp->status.PR_WHAT;
756 #endif /* !HAVE_PR_SYSCALL */
757 if (!(tcp->flags & TCB_INSYSCALL)) {
758 if (tcp->status.PR_WHY != PR_SYSENTRY) {
763 #endif /* SYS_vfork */
765 /* We are returning in the child, fake it. */
766 tcp->status.PR_WHY = PR_SYSENTRY;
768 tcp->status.PR_WHY = PR_SYSEXIT;
771 fprintf(stderr, "syscall: missing entry\n");
772 tcp->flags |= TCB_INSYSCALL;
777 if (tcp->status.PR_WHY != PR_SYSEXIT) {
778 fprintf(stderr, "syscall: missing exit\n");
779 tcp->flags &= ~TCB_INSYSCALL;
784 if (!(tcp->flags & TCB_INSYSCALL)) {
786 fprintf(stderr, "syscall: missing entry\n");
787 tcp->flags |= TCB_INSYSCALL;
794 * This happens when a signal handler
795 * for a signal which interrupted a
796 * a system call makes another system call.
798 fprintf(stderr, "syscall: missing exit\n");
800 tcp->flags &= ~TCB_INSYSCALL;
806 if (upeek(pid, 4*EAX, &eax) < 0)
808 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
810 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
813 #elif defined (POWERPC)
814 # define SO_MASK 0x10000000
815 if (upeek(pid, 4*PT_CCR, &flags) < 0)
817 if (upeek(pid, 4*PT_R3, &result) < 0)
822 if (upeek(pid, 4*PT_D0, &d0) < 0)
824 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
826 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
830 if (upeek(pid, 4*0, (long *)&r0) < 0)
832 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
834 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
841 if (tcp->flags & TCB_INSYSCALL) {
846 if (eax < 0 && -eax < nerrnos) {
865 if (result && (unsigned) -result < nerrnos) {
870 tcp->u_rval = result;
875 if (d0 && (unsigned) -d0 < nerrnos) {
885 if (r0 && (unsigned) -r0 < nerrnos) {
905 if (regs.r_psr & PSR_C) {
910 tcp->u_rval = regs.r_o0;
922 /* get error code from user struct */
923 if (upeek(pid, uoff(u_error), &u_error) < 0)
925 u_error >>= 24; /* u_error is a char */
927 /* get system call return value */
928 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
933 /* Judicious guessing goes a long way. */
934 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
936 u_error = tcp->status.pr_reg[R_O0];
939 tcp->u_rval = tcp->status.pr_reg[R_O0];
944 /* Wanna know how to kill an hour single-stepping? */
945 if (tcp->status.PR_REG[EFL] & 0x1) {
947 u_error = tcp->status.PR_REG[EAX];
950 tcp->u_rval = tcp->status.PR_REG[EAX];
955 if (tcp->status.pr_reg[CTX_A3]) {
957 u_error = tcp->status.pr_reg[CTX_V0];
960 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
965 tcp->u_error = u_error;
967 internal_syscall(tcp);
968 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
969 tcp->flags &= ~TCB_INSYSCALL;
973 if (tcp->flags & TCB_REPRINT) {
976 if (tcp->scno >= nsyscalls)
977 tprintf("syscall_%lu", tcp->scno);
979 tprintf("%s", sysent[tcp->scno].sys_name);
980 tprintf(" resumed> ");
984 call_count[tcp->scno]++;
986 error_count[tcp->scno]++;
987 tv_sub(&tv, &tv, &tcp->etime);
989 if (tv_cmp(&tv, &tcp->dtime) > 0) {
990 static struct timeval one_tick =
993 if (tv_nz(&tcp->dtime))
995 else if (tv_cmp(&tv, &one_tick) > 0) {
996 if (tv_cmp(&shortest, &one_tick) < 0)
1003 if (tv_cmp(&tv, &shortest) < 0)
1005 tv_add(&tv_count[tcp->scno],
1006 &tv_count[tcp->scno], &tv);
1007 tcp->flags &= ~TCB_INSYSCALL;
1011 if (tcp->scno >= nsyscalls
1012 || (qual_flags[tcp->scno] & QUAL_RAW))
1013 sys_res = printargs(tcp);
1015 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1016 u_error = tcp->u_error;
1019 if (qual_flags[tcp->scno] & QUAL_RAW) {
1021 tprintf("= -1 (errno %ld)", u_error);
1023 tprintf("= %#lx", tcp->u_rval);
1025 else if (!(sys_res & RVAL_NONE) && u_error) {
1029 tprintf("= ? ERESTARTSYS (To be restarted)");
1031 case ERESTARTNOINTR:
1032 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1034 case ERESTARTNOHAND:
1035 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1040 if (u_error < nerrnos && u_error < sys_nerr)
1041 tprintf("%s (%s)", errnoent[u_error],
1042 sys_errlist[u_error]);
1043 else if (u_error < nerrnos)
1044 tprintf("%s (errno %ld)",
1045 errnoent[u_error], u_error);
1046 else if (u_error < sys_nerr)
1047 tprintf("ERRNO_%ld (%s)", u_error,
1048 sys_errlist[u_error]);
1050 tprintf("E??? (errno %ld)", u_error);
1057 if (sys_res & RVAL_NONE)
1060 switch (sys_res & RVAL_MASK) {
1062 tprintf("= %#lx", tcp->u_rval);
1065 tprintf("= %#lo", tcp->u_rval);
1068 tprintf("= %lu", tcp->u_rval);
1071 tprintf("= %ld", tcp->u_rval);
1075 "invalid rval format\n");
1079 if ((sys_res & RVAL_STR) && tcp->auxstr)
1080 tprintf(" (%s)", tcp->auxstr);
1083 tv_sub(&tv, &tv, &tcp->etime);
1084 tprintf(" <%ld.%06ld>",
1085 (long) tv.tv_sec, (long) tv.tv_usec);
1090 if (fflush(tcp->outf) == EOF)
1092 tcp->flags &= ~TCB_INSYSCALL;
1096 /* Entering system call */
1102 tcp->u_nargs = sysent[tcp->scno].nargs;
1103 for (i = 0; i < tcp->u_nargs; i++) {
1104 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
1108 #elif defined (ALPHA)
1111 tcp->u_nargs = sysent[tcp->scno].nargs;
1112 for (i = 0; i < tcp->u_nargs; i++) {
1113 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1114 * for scno somewhere above here!
1116 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1120 #elif defined (MIPS)
1125 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1127 if(upeek(pid, REG_SP, &sp) < 0)
1129 for(i = 0; i < 4; i++) {
1130 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1133 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1134 (char *)(tcp->u_arg + 4));
1136 for(i = 0; i < nargs; i++) {
1137 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1142 #elif defined (POWERPC)
1145 tcp->u_nargs = sysent[tcp->scno].nargs;
1146 for (i = 0; i < tcp->u_nargs; i++) {
1147 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1151 #elif defined (SPARC)
1155 tcp->u_nargs = sysent[tcp->scno].nargs;
1156 for (i = 0; i < tcp->u_nargs; i++)
1157 tcp->u_arg[i] = *((®s.r_o0) + i);
1162 tcp->u_nargs = sysent[tcp->scno].nargs;
1163 for (i = 0; i < tcp->u_nargs; i++) {
1164 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1173 tcp->u_nargs = sysent[tcp->scno].nargs;
1174 for (i = 0; i < tcp->u_nargs; i++) {
1177 if (upeek(pid, uoff(u_arg[0]) +
1178 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1186 * SGI is broken: even though it has pr_sysarg, it doesn't
1187 * set them on system call entry. Get a clue.
1189 if (sysent[tcp->scno].nargs != -1)
1190 tcp->u_nargs = sysent[tcp->scno].nargs;
1192 tcp->u_nargs = tcp->status.pr_nsysarg;
1193 if (tcp->u_nargs > 4) {
1194 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1195 4*sizeof(tcp->u_arg[0]));
1196 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1197 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1200 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1201 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1204 #ifdef HAVE_PR_SYSCALL
1205 if (sysent[tcp->scno].nargs != -1)
1206 tcp->u_nargs = sysent[tcp->scno].nargs;
1208 tcp->u_nargs = tcp->status.pr_nsysarg;
1211 for (i = 0; i < tcp->u_nargs; i++)
1212 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1214 #else /* !HAVE_PR_SYSCALL */
1216 if (sysent[tcp->scno].nargs != -1)
1217 tcp->u_nargs = sysent[tcp->scno].nargs;
1220 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1224 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1225 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1227 #endif /* !HAVE_PR_SYSCALL */
1230 switch (tcp->scno + NR_SYSCALL_BASE) {
1232 #if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS)
1233 case SYS_socketcall:
1234 decode_subcall(tcp, SYS_socket_subcall,
1235 SYS_socket_nsubcalls, deref_style);
1238 decode_subcall(tcp, SYS_ipc_subcall,
1239 SYS_ipc_nsubcalls, shift_style);
1241 #endif /* !ALPHA && !SPARC */
1243 case SYS_socketcall:
1244 sparc_socket_decode (tcp);
1249 #ifdef SYS_pgrpsys_subcall
1251 decode_subcall(tcp, SYS_pgrpsys_subcall,
1252 SYS_pgrpsys_nsubcalls, shift_style);
1254 #endif /* SYS_pgrpsys_subcall */
1255 #ifdef SYS_sigcall_subcall
1257 decode_subcall(tcp, SYS_sigcall_subcall,
1258 SYS_sigcall_nsubcalls, mask_style);
1260 #endif /* SYS_sigcall_subcall */
1262 decode_subcall(tcp, SYS_msgsys_subcall,
1263 SYS_msgsys_nsubcalls, shift_style);
1266 decode_subcall(tcp, SYS_shmsys_subcall,
1267 SYS_shmsys_nsubcalls, shift_style);
1270 decode_subcall(tcp, SYS_semsys_subcall,
1271 SYS_semsys_nsubcalls, shift_style);
1275 decode_subcall(tcp, SYS_utssys_subcall,
1276 SYS_utssys_nsubcalls, shift_style);
1280 decode_subcall(tcp, SYS_sysfs_subcall,
1281 SYS_sysfs_nsubcalls, shift_style);
1284 decode_subcall(tcp, SYS_spcall_subcall,
1285 SYS_spcall_nsubcalls, shift_style);
1287 #ifdef SYS_context_subcall
1289 decode_subcall(tcp, SYS_context_subcall,
1290 SYS_context_nsubcalls, shift_style);
1292 #endif /* SYS_context_subcall */
1293 #ifdef SYS_door_subcall
1295 decode_subcall(tcp, SYS_door_subcall,
1296 SYS_door_nsubcalls, door_style);
1298 #endif /* SYS_door_subcall */
1302 decode_subcall(tcp, SYS_semsys_subcall,
1303 SYS_semsys_nsubcalls, shift_style);
1306 decode_subcall(tcp, SYS_msgsys_subcall,
1307 SYS_msgsys_nsubcalls, shift_style);
1310 decode_subcall(tcp, SYS_shmsys_subcall,
1311 SYS_shmsys_nsubcalls, shift_style);
1316 internal_syscall(tcp);
1317 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1318 tcp->flags |= TCB_INSYSCALL;
1323 gettimeofday(&tcp->etime, NULL);
1324 tcp->flags |= TCB_INSYSCALL;
1329 tcp->flags &= ~TCB_REPRINT;
1331 if (tcp->scno >= nsyscalls)
1332 tprintf("syscall_%lu(", tcp->scno);
1334 tprintf("%s(", sysent[tcp->scno].sys_name);
1335 if (tcp->scno >= nsyscalls ||
1336 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1337 sys_res = printargs(tcp);
1339 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1340 if (fflush(tcp->outf) == EOF)
1342 tcp->flags |= TCB_INSYSCALL;
1343 /* Measure the entrance time as late as possible to avoid errors. */
1345 gettimeofday(&tcp->etime, NULL);
1353 if (entering(tcp)) {
1356 for (i = 0; i < tcp->u_nargs; i++)
1357 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1371 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
1378 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1384 val = tcp->status.PR_REG[R_O1];
1387 val = tcp->status.PR_REG[EDX];
1390 val = tcp->status.PR_REG[CTX_V1];
1398 * Apparently, indirect system calls have already be converted by ptrace(2),
1399 * so if you see "indir" this program has gone astray.
1407 if (entering(tcp)) {
1408 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1409 fprintf(stderr, "Bogus syscall: %u\n", scno);
1412 nargs = sysent[scno].nargs;
1413 tprintf("%s", sysent[scno].sys_name);
1414 for (i = 0; i < nargs; i++)
1415 tprintf(", %#lx", tcp->u_arg[i+1]);
1425 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1433 return strcmp(sysent[*((int *) a)].sys_name,
1434 sysent[*((int *) b)].sys_name);
1442 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1444 return (m < n) ? 1 : (m > n) ? -1 : 0;
1447 static int (*sortfun)();
1448 static struct timeval overhead = { -1, -1 };
1454 if (strcmp(sortby, "time") == 0)
1456 else if (strcmp(sortby, "calls") == 0)
1457 sortfun = count_cmp;
1458 else if (strcmp(sortby, "name") == 0)
1459 sortfun = syscall_cmp;
1460 else if (strcmp(sortby, "nothing") == 0)
1463 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1468 void set_overhead(n)
1471 overhead.tv_sec = n / 1000000;
1472 overhead.tv_usec = n % 1000000;
1480 int call_cum, error_cum;
1481 struct timeval tv_cum, dtv;
1483 char *dashes = "-------------------------";
1486 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1487 if (overhead.tv_sec == -1) {
1488 tv_mul(&overhead, &shortest, 8);
1489 tv_div(&overhead, &overhead, 10);
1491 for (i = 0; i < nsyscalls; i++) {
1492 sorted_count[i] = i;
1493 if (call_count[i] == 0)
1495 tv_mul(&dtv, &overhead, call_count[i]);
1496 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1497 call_cum += call_count[i];
1498 error_cum += error_count[i];
1499 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1502 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1503 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1504 "% time", "seconds", "usecs/call",
1505 "calls", "errors", "syscall");
1506 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1507 dashes, dashes, dashes, dashes, dashes, dashes);
1508 for (i = 0; i < nsyscalls; i++) {
1509 j = sorted_count[i];
1510 if (call_count[j] == 0)
1512 tv_div(&dtv, &tv_count[j], call_count[j]);
1514 sprintf(error_str, "%d", error_count[j]);
1516 error_str[0] = '\0';
1517 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1518 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1519 percent, (long) tv_count[j].tv_sec,
1520 (long) tv_count[j].tv_usec,
1521 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1522 call_count[j], error_str, sysent[j].sys_name);
1524 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1525 dashes, dashes, dashes, dashes, dashes, dashes);
1527 sprintf(error_str, "%d", error_cum);
1529 error_str[0] = '\0';
1530 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1531 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1532 call_cum, error_str, "total");