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>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/syscall.h>
39 #include <sys/param.h>
45 #if HAVE_LINUX_PTRACE_H
47 #include <linux/ptrace.h>
52 #ifndef PTRACE_PEEKUSR
53 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
57 #ifndef SYS_ERRLIST_DECLARED
59 extern char *sys_errlist[];
60 #endif /* SYS_ERRLIST_DECLARED */
64 #define ERESTARTSYS 512
66 #ifndef ERESTARTNOINTR
67 #define ERESTARTNOINTR 513
69 #ifndef ERESTARTNOHAND
70 #define ERESTARTNOHAND 514 /* restart if no handler.. */
73 #define ENOIOCTLCMD 515 /* No ioctl command */
86 /* Define these shorthand notations to simplify the syscallent files. */
89 #define TN TRACE_NETWORK
90 #define TP TRACE_PROCESS
91 #define TS TRACE_SIGNAL
93 struct sysent sysent0[] = {
94 #include "syscallent.h"
96 int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
98 #if SUPPORTED_PERSONALITIES >= 2
99 struct sysent sysent1[] = {
100 #include "syscallent1.h"
102 int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
103 #endif /* SUPPORTED_PERSONALITIES >= 2 */
105 #if SUPPORTED_PERSONALITIES >= 3
106 struct sysent sysent2[] = {
107 #include "syscallent2.h"
109 int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
110 #endif /* SUPPORTED_PERSONALITIES >= 3 */
112 struct sysent *sysent;
115 /* Now undef them since short defines cause wicked namespace pollution. */
122 char *errnoent0[] = {
123 #include "errnoent.h"
125 int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
127 #if SUPPORTED_PERSONALITIES >= 2
128 char *errnoent1[] = {
129 #include "errnoent1.h"
131 int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
132 #endif /* SUPPORTED_PERSONALITIES >= 2 */
134 #if SUPPORTED_PERSONALITIES >= 3
135 char *errnoent2[] = {
136 #include "errnoent2.h"
138 int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
139 #endif /* SUPPORTED_PERSONALITIES >= 3 */
144 int current_personality;
147 set_personality(personality)
150 switch (personality) {
152 errnoent = errnoent0;
155 nsyscalls = nsyscalls0;
156 ioctlent = ioctlent0;
157 nioctlents = nioctlents0;
158 signalent = signalent0;
159 nsignals = nsignals0;
162 #if SUPPORTED_PERSONALITIES >= 2
164 errnoent = errnoent1;
167 nsyscalls = nsyscalls1;
168 ioctlent = ioctlent1;
169 nioctlents = nioctlents1;
170 signalent = signalent1;
171 nsignals = nsignals1;
173 #endif /* SUPPORTED_PERSONALITIES >= 2 */
175 #if SUPPORTED_PERSONALITIES >= 3
177 errnoent = errnoent2;
180 nsyscalls = nsyscalls2;
181 ioctlent = ioctlent2;
182 nioctlents = nioctlents2;
183 signalent = signalent2;
184 nsignals = nsignals2;
186 #endif /* SUPPORTED_PERSONALITIES >= 3 */
192 current_personality = personality;
196 int qual_flags[MAX_QUALS];
198 static int call_count[MAX_QUALS];
199 static int error_count[MAX_QUALS];
200 static struct timeval tv_count[MAX_QUALS];
201 static int sorted_count[MAX_QUALS];
203 static struct timeval shortest = { 1000000, 0 };
205 static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
207 static struct qual_options {
213 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
214 { QUAL_TRACE, "t", lookup_syscall, "system call" },
215 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
216 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
217 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
218 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
219 { QUAL_RAW, "raw", lookup_syscall, "system call" },
220 { QUAL_RAW, "x", lookup_syscall, "system call" },
221 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
222 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
223 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
224 { QUAL_FAULT, "fault", lookup_fault, "fault" },
225 { QUAL_FAULT, "faults", lookup_fault, "fault" },
226 { QUAL_FAULT, "m", lookup_fault, "fault" },
227 { QUAL_READ, "read", lookup_desc, "descriptor" },
228 { QUAL_READ, "reads", lookup_desc, "descriptor" },
229 { QUAL_READ, "r", lookup_desc, "descriptor" },
230 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
231 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
232 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
233 { 0, NULL, NULL, NULL },
242 for (i = 0; i < nsyscalls; i++) {
243 if (strcmp(s, sysent[i].sys_name) == 0)
256 if (s && *s && isdigit(*s))
260 for (i = 0; s[i]; i++)
261 s[i] = toupper(s[i]);
262 if (strncmp(s, "SIG", 3) == 0)
264 for (i = 0; i <= NSIG; i++) {
265 if (strcmp(s, signame(i) + 3) == 0)
282 if (s && *s && isdigit(*s))
291 if (strcmp(s, "file") == 0)
293 if (strcmp(s, "ipc") == 0)
295 if (strcmp(s, "network") == 0)
296 return TRACE_NETWORK;
297 if (strcmp(s, "process") == 0)
298 return TRACE_PROCESS;
299 if (strcmp(s, "signal") == 0)
308 struct qual_options *opt;
313 opt = &qual_options[0];
314 for (i = 0; (p = qual_options[i].option_name); i++) {
316 if (strncmp(s, p, n) == 0 && s[n] == '=') {
317 opt = &qual_options[i];
327 if (strcmp(s, "none") == 0) {
331 if (strcmp(s, "all") == 0) {
332 for (i = 0; i < MAX_QUALS; i++) {
334 qual_flags[i] &= ~opt->bitflag;
336 qual_flags[i] |= opt->bitflag;
340 for (i = 0; i < MAX_QUALS; i++) {
342 qual_flags[i] |= opt->bitflag;
344 qual_flags[i] &= ~opt->bitflag;
346 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
347 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
348 for (i = 0; i < MAX_QUALS; i++) {
349 if (sysent[i].sys_flags & n) {
351 qual_flags[i] &= ~opt->bitflag;
353 qual_flags[i] |= opt->bitflag;
358 if ((n = (*opt->lookup)(p)) < 0) {
359 fprintf(stderr, "strace: invalid %s `%s'\n",
360 opt->argument_name, p);
364 qual_flags[n] &= ~opt->bitflag;
366 qual_flags[n] |= opt->bitflag;
377 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
380 switch (tcp->scno + __NR_SYSCALL_BASE) {
391 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
392 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
401 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
402 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
407 enum subcall_style { shift_style, deref_style, mask_style, door_style };
409 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
411 const int socket_map [] = {
414 /* SYS_CONNECT */ 98,
415 /* SYS_LISTEN */ 106,
417 /* SYS_GETSOCKNAME */ 150,
418 /* SYS_GETPEERNAME */ 141,
419 /* SYS_SOCKETPAIR */ 135,
422 /* SYS_SENDTO */ 133,
423 /* SYS_RECVFROM */ 125,
424 /* SYS_SHUTDOWN */ 134,
425 /* SYS_SETSOCKOPT */ 105,
426 /* SYS_GETSOCKOPT */ 118,
427 /* SYS_SENDMSG */ 114,
428 /* SYS_RECVMSG */ 113
432 sparc_socket_decode (tcp)
438 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
441 tcp->scno = socket_map [tcp->u_arg [0]-1];
442 n = tcp->u_nargs = sysent [tcp->scno].nargs;
443 addr = tcp->u_arg [1];
444 for (i = 0; i < n; i++){
446 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
448 tcp->u_arg [i] = arg;
449 addr += sizeof (arg);
454 decode_subcall(tcp, subcall, nsubcalls, style)
458 enum subcall_style style;
460 int i, addr, mask, arg;
462 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
466 tcp->scno = subcall + tcp->u_arg[0];
467 if (sysent[tcp->scno].nargs != -1)
468 tcp->u_nargs = sysent[tcp->scno].nargs;
471 for (i = 0; i < tcp->u_nargs; i++)
472 tcp->u_arg[i] = tcp->u_arg[i + 1];
475 tcp->scno = subcall + tcp->u_arg[0];
476 addr = tcp->u_arg[1];
477 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
478 if (umove(tcp, addr, &arg) < 0)
483 tcp->u_nargs = sysent[tcp->scno].nargs;
486 mask = (tcp->u_arg[0] >> 8) & 0xff;
487 tcp->u_arg[0] &= 0xff;
488 for (i = 0; mask; i++)
490 tcp->scno = subcall + i;
491 if (sysent[tcp->scno].nargs != -1)
492 tcp->u_nargs = sysent[tcp->scno].nargs;
496 * Oh, yuck. The call code is the *sixth* argument.
498 tcp->scno = subcall + tcp->u_arg[5];
499 if (sysent[tcp->scno].nargs != -1)
500 tcp->u_nargs = sysent[tcp->scno].nargs;
508 struct tcb *tcp_last = NULL;
511 internal_syscall(tcp)
515 * We must always trace a few critical system calls in order to
516 * correctly support following forks in the presence of tracing
520 switch (tcp->scno + __NR_SYSCALL_BASE) {
579 #elif defined (POWERPC)
585 #elif defined (ALPHA)
590 #elif defined (SPARC)
600 /* Measure the exit time as early as possible to avoid errors. */
601 if (dtime && (tcp->flags & TCB_INSYSCALL))
602 gettimeofday(&tv, NULL);
604 #if defined (POWERPC)
605 if (upeek(pid, 4*PT_R0, &scno) < 0)
607 if (!(tcp->flags & TCB_INSYSCALL)) {
608 /* Check if we return from execve. */
609 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
610 tcp->flags &= ~TCB_WAITEXECVE;
615 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
620 upeek(pid, 4*15, &pc);
621 umoven(tcp, pc-4, 4, (char *)&scno);
625 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
628 if (upeek(pid, REG_A3, &a3) < 0)
631 if(!(tcp->flags & TCB_INSYSCALL)) {
632 if (upeek(pid, REG_V0, &scno) < 0)
635 if (scno < 0 || scno > nsyscalls) {
636 if(a3 == 0 || a3 == -1) {
638 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
643 if (upeek(pid, REG_V0, &r2) < 0)
646 #elif defined (ALPHA)
647 if (upeek(pid, REG_A3, &a3) < 0)
650 if (!(tcp->flags & TCB_INSYSCALL)) {
651 if (upeek(pid, REG_R0, &scno) < 0)
654 /* Check if we return from execve. */
655 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
656 tcp->flags &= ~TCB_WAITEXECVE;
661 * Do some sanity checks to figure out if it's
662 * really a syscall entry
664 if (scno < 0 || scno > nsyscalls) {
665 if (a3 == 0 || a3 == -1) {
667 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
673 if (upeek(pid, REG_R0, &r0) < 0)
676 #elif defined (SPARC)
677 /* Everything we need is in the current register set. */
678 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
681 /* If we are entering, then disassemble the syscall trap. */
682 if (!(tcp->flags & TCB_INSYSCALL)) {
683 /* Retrieve the syscall trap instruction. */
685 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
689 /* Disassemble the trap to see what personality to use. */
692 /* Linux/SPARC syscall trap. */
696 /* Linux/SPARC64 syscall trap. */
697 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
700 /* SunOS syscall trap. (pers 1) */
701 fprintf(stderr,"syscall: SunOS no support\n");
704 /* Solaris 2.x syscall trap. (per 2) */
708 /* NetBSD/FreeBSD syscall trap. */
709 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
712 /* Solaris 2.x gettimeofday */
716 /* Unknown syscall trap. */
717 if(tcp->flags & TCB_WAITEXECVE) {
718 tcp->flags &= ~TCB_WAITEXECVE;
721 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
725 /* Extract the system call number from the registers. */
726 if (trap == 0x91d02027)
732 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
738 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
742 #ifdef HAVE_PR_SYSCALL
743 scno = tcp->status.pr_syscall;
744 #else /* !HAVE_PR_SYSCALL */
745 scno = tcp->status.PR_WHAT;
746 #endif /* !HAVE_PR_SYSCALL */
747 if (!(tcp->flags & TCB_INSYSCALL)) {
748 if (tcp->status.PR_WHY != PR_SYSENTRY) {
753 #endif /* SYS_vfork */
755 /* We are returning in the child, fake it. */
756 tcp->status.PR_WHY = PR_SYSENTRY;
758 tcp->status.PR_WHY = PR_SYSEXIT;
761 fprintf(stderr, "syscall: missing entry\n");
762 tcp->flags |= TCB_INSYSCALL;
767 if (tcp->status.PR_WHY != PR_SYSEXIT) {
768 fprintf(stderr, "syscall: missing exit\n");
769 tcp->flags &= ~TCB_INSYSCALL;
774 if (!(tcp->flags & TCB_INSYSCALL)) {
776 fprintf(stderr, "syscall: missing entry\n");
777 tcp->flags |= TCB_INSYSCALL;
784 * This happens when a signal handler
785 * for a signal which interrupted a
786 * a system call makes another system call.
788 fprintf(stderr, "syscall: missing exit\n");
790 tcp->flags &= ~TCB_INSYSCALL;
796 if (upeek(pid, 4*EAX, &eax) < 0)
798 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
800 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
803 #elif defined (POWERPC)
804 # define SO_MASK 0x10000000
805 if (upeek(pid, 4*PT_CCR, &flags) < 0)
807 if (upeek(pid, 4*PT_R3, &result) < 0)
812 if (upeek(pid, 4*PT_D0, &d0) < 0)
814 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
816 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
820 if (upeek(pid, 4*0, (long *)&r0) < 0)
822 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
824 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
831 if (tcp->flags & TCB_INSYSCALL) {
836 if (eax < 0 && -eax < nerrnos) {
855 if (result && (unsigned) -result < nerrnos) {
860 tcp->u_rval = result;
865 if (d0 && (unsigned) -d0 < nerrnos) {
875 if (r0 && (unsigned) -r0 < nerrnos) {
895 if (regs.r_psr & PSR_C) {
900 tcp->u_rval = regs.r_o0;
912 /* get error code from user struct */
913 if (upeek(pid, uoff(u_error), &u_error) < 0)
915 u_error >>= 24; /* u_error is a char */
917 /* get system call return value */
918 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
923 /* Judicious guessing goes a long way. */
924 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
926 u_error = tcp->status.pr_reg[R_O0];
929 tcp->u_rval = tcp->status.pr_reg[R_O0];
934 /* Wanna know how to kill an hour single-stepping? */
935 if (tcp->status.PR_REG[EFL] & 0x1) {
937 u_error = tcp->status.PR_REG[EAX];
940 tcp->u_rval = tcp->status.PR_REG[EAX];
945 if (tcp->status.pr_reg[CTX_A3]) {
947 u_error = tcp->status.pr_reg[CTX_V0];
950 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
955 tcp->u_error = u_error;
957 internal_syscall(tcp);
958 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
959 tcp->flags &= ~TCB_INSYSCALL;
963 if (tcp->flags & TCB_REPRINT) {
966 if (tcp->scno >= nsyscalls)
967 tprintf("syscall_%lu", tcp->scno);
969 tprintf("%s", sysent[tcp->scno].sys_name);
970 tprintf(" resumed> ");
974 call_count[tcp->scno]++;
976 error_count[tcp->scno]++;
977 tv_sub(&tv, &tv, &tcp->etime);
979 if (tv_cmp(&tv, &tcp->dtime) > 0) {
980 static struct timeval one_tick =
983 if (tv_nz(&tcp->dtime))
985 else if (tv_cmp(&tv, &one_tick) > 0) {
986 if (tv_cmp(&shortest, &one_tick) < 0)
993 if (tv_cmp(&tv, &shortest) < 0)
995 tv_add(&tv_count[tcp->scno],
996 &tv_count[tcp->scno], &tv);
997 tcp->flags &= ~TCB_INSYSCALL;
1001 if (tcp->scno >= nsyscalls
1002 || (qual_flags[tcp->scno] & QUAL_RAW))
1003 sys_res = printargs(tcp);
1005 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1006 u_error = tcp->u_error;
1009 if (qual_flags[tcp->scno] & QUAL_RAW) {
1011 tprintf("= -1 (errno %ld)", u_error);
1013 tprintf("= %#lx", tcp->u_rval);
1015 else if (!(sys_res & RVAL_NONE) && u_error) {
1019 tprintf("= ? ERESTARTSYS (To be restarted)");
1021 case ERESTARTNOINTR:
1022 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1024 case ERESTARTNOHAND:
1025 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1030 if (u_error < nerrnos && u_error < sys_nerr)
1031 tprintf("%s (%s)", errnoent[u_error],
1032 sys_errlist[u_error]);
1033 else if (u_error < nerrnos)
1034 tprintf("%s (errno %ld)",
1035 errnoent[u_error], u_error);
1036 else if (u_error < sys_nerr)
1037 tprintf("ERRNO_%ld (%s)", u_error,
1038 sys_errlist[u_error]);
1040 tprintf("E??? (errno %ld)", u_error);
1047 if (sys_res & RVAL_NONE)
1050 switch (sys_res & RVAL_MASK) {
1052 tprintf("= %#lx", tcp->u_rval);
1055 tprintf("= %#lo", tcp->u_rval);
1058 tprintf("= %lu", tcp->u_rval);
1061 tprintf("= %ld", tcp->u_rval);
1065 "invalid rval format\n");
1069 if ((sys_res & RVAL_STR) && tcp->auxstr)
1070 tprintf(" (%s)", tcp->auxstr);
1073 tv_sub(&tv, &tv, &tcp->etime);
1074 tprintf(" <%ld.%06ld>",
1075 (long) tv.tv_sec, (long) tv.tv_usec);
1080 if (fflush(tcp->outf) == EOF)
1082 tcp->flags &= ~TCB_INSYSCALL;
1086 /* Entering system call */
1092 tcp->u_nargs = sysent[tcp->scno].nargs;
1093 for (i = 0; i < tcp->u_nargs; i++) {
1094 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1095 * for scno somewhere above here!
1097 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1101 #elif defined (MIPS)
1106 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1108 if(upeek(pid, REG_SP, &sp) < 0)
1110 for(i = 0; i < 4; i++) {
1111 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1114 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1115 (char *)(tcp->u_arg + 4));
1117 for(i = 0; i < nargs; i++) {
1118 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1123 #elif defined (POWERPC)
1126 tcp->u_nargs = sysent[tcp->scno].nargs;
1127 for (i = 0; i < tcp->u_nargs; i++) {
1128 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1132 #elif defined (SPARC)
1136 tcp->u_nargs = sysent[tcp->scno].nargs;
1137 for (i = 0; i < tcp->u_nargs; i++)
1138 tcp->u_arg[i] = *((®s.r_o0) + i);
1143 tcp->u_nargs = sysent[tcp->scno].nargs;
1144 for (i = 0; i < tcp->u_nargs; i++) {
1145 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1154 tcp->u_nargs = sysent[tcp->scno].nargs;
1155 for (i = 0; i < tcp->u_nargs; i++) {
1158 if (upeek(pid, uoff(u_arg[0]) +
1159 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1167 * SGI is broken: even though it has pr_sysarg, it doesn't
1168 * set them on system call entry. Get a clue.
1170 if (sysent[tcp->scno].nargs != -1)
1171 tcp->u_nargs = sysent[tcp->scno].nargs;
1173 tcp->u_nargs = tcp->status.pr_nsysarg;
1174 if (tcp->u_nargs > 4) {
1175 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1176 4*sizeof(tcp->u_arg[0]));
1177 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1178 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1181 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1182 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1185 #ifdef HAVE_PR_SYSCALL
1186 if (sysent[tcp->scno].nargs != -1)
1187 tcp->u_nargs = sysent[tcp->scno].nargs;
1189 tcp->u_nargs = tcp->status.pr_nsysarg;
1192 for (i = 0; i < tcp->u_nargs; i++)
1193 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1195 #else /* !HAVE_PR_SYSCALL */
1197 if (sysent[tcp->scno].nargs != -1)
1198 tcp->u_nargs = sysent[tcp->scno].nargs;
1201 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1205 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1206 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1208 #endif /* !HAVE_PR_SYSCALL */
1212 switch (tcp->scno + __NR_SYSCALL_BASE) {
1214 switch (tcp->scno) {
1217 #if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS)
1218 case SYS_socketcall:
1219 decode_subcall(tcp, SYS_socket_subcall,
1220 SYS_socket_nsubcalls, deref_style);
1223 decode_subcall(tcp, SYS_ipc_subcall,
1224 SYS_ipc_nsubcalls, shift_style);
1226 #endif /* !ALPHA && !SPARC */
1228 case SYS_socketcall:
1229 sparc_socket_decode (tcp);
1234 #ifdef SYS_pgrpsys_subcall
1236 decode_subcall(tcp, SYS_pgrpsys_subcall,
1237 SYS_pgrpsys_nsubcalls, shift_style);
1239 #endif /* SYS_pgrpsys_subcall */
1240 #ifdef SYS_sigcall_subcall
1242 decode_subcall(tcp, SYS_sigcall_subcall,
1243 SYS_sigcall_nsubcalls, mask_style);
1245 #endif /* SYS_sigcall_subcall */
1247 decode_subcall(tcp, SYS_msgsys_subcall,
1248 SYS_msgsys_nsubcalls, shift_style);
1251 decode_subcall(tcp, SYS_shmsys_subcall,
1252 SYS_shmsys_nsubcalls, shift_style);
1255 decode_subcall(tcp, SYS_semsys_subcall,
1256 SYS_semsys_nsubcalls, shift_style);
1260 decode_subcall(tcp, SYS_utssys_subcall,
1261 SYS_utssys_nsubcalls, shift_style);
1265 decode_subcall(tcp, SYS_sysfs_subcall,
1266 SYS_sysfs_nsubcalls, shift_style);
1269 decode_subcall(tcp, SYS_spcall_subcall,
1270 SYS_spcall_nsubcalls, shift_style);
1272 #ifdef SYS_context_subcall
1274 decode_subcall(tcp, SYS_context_subcall,
1275 SYS_context_nsubcalls, shift_style);
1277 #endif /* SYS_context_subcall */
1278 #ifdef SYS_door_subcall
1280 decode_subcall(tcp, SYS_door_subcall,
1281 SYS_door_nsubcalls, door_style);
1283 #endif /* SYS_door_subcall */
1287 decode_subcall(tcp, SYS_semsys_subcall,
1288 SYS_semsys_nsubcalls, shift_style);
1291 decode_subcall(tcp, SYS_msgsys_subcall,
1292 SYS_msgsys_nsubcalls, shift_style);
1295 decode_subcall(tcp, SYS_shmsys_subcall,
1296 SYS_shmsys_nsubcalls, shift_style);
1301 internal_syscall(tcp);
1302 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1303 tcp->flags |= TCB_INSYSCALL;
1308 gettimeofday(&tcp->etime, NULL);
1309 tcp->flags |= TCB_INSYSCALL;
1314 tcp->flags &= ~TCB_REPRINT;
1316 if (tcp->scno >= nsyscalls)
1317 tprintf("syscall_%lu(", tcp->scno);
1319 tprintf("%s(", sysent[tcp->scno].sys_name);
1320 if (tcp->scno >= nsyscalls ||
1321 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1322 sys_res = printargs(tcp);
1324 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1325 if (fflush(tcp->outf) == EOF)
1327 tcp->flags |= TCB_INSYSCALL;
1328 /* Measure the entrance time as late as possible to avoid errors. */
1330 gettimeofday(&tcp->etime, NULL);
1338 if (entering(tcp)) {
1341 for (i = 0; i < tcp->u_nargs; i++)
1342 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1356 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
1363 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1369 val = tcp->status.PR_REG[R_O1];
1372 val = tcp->status.PR_REG[EDX];
1375 val = tcp->status.PR_REG[CTX_V1];
1383 * Apparently, indirect system calls have already be converted by ptrace(2),
1384 * so if you see "indir" this program has gone astray.
1392 if (entering(tcp)) {
1393 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1394 fprintf(stderr, "Bogus syscall: %u\n", scno);
1397 nargs = sysent[scno].nargs;
1398 tprintf("%s", sysent[scno].sys_name);
1399 for (i = 0; i < nargs; i++)
1400 tprintf(", %#lx", tcp->u_arg[i+1]);
1410 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1418 return strcmp(sysent[*((int *) a)].sys_name,
1419 sysent[*((int *) b)].sys_name);
1427 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1429 return (m < n) ? 1 : (m > n) ? -1 : 0;
1432 static int (*sortfun)();
1433 static struct timeval overhead = { -1, -1 };
1439 if (strcmp(sortby, "time") == 0)
1441 else if (strcmp(sortby, "calls") == 0)
1442 sortfun = count_cmp;
1443 else if (strcmp(sortby, "name") == 0)
1444 sortfun = syscall_cmp;
1445 else if (strcmp(sortby, "nothing") == 0)
1448 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1453 void set_overhead(n)
1456 overhead.tv_sec = n / 1000000;
1457 overhead.tv_usec = n % 1000000;
1465 int call_cum, error_cum;
1466 struct timeval tv_cum, dtv;
1468 char *dashes = "-------------------------";
1471 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1472 if (overhead.tv_sec == -1) {
1473 tv_mul(&overhead, &shortest, 8);
1474 tv_div(&overhead, &overhead, 10);
1476 for (i = 0; i < nsyscalls; i++) {
1477 sorted_count[i] = i;
1478 if (call_count[i] == 0)
1480 tv_mul(&dtv, &overhead, call_count[i]);
1481 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1482 call_cum += call_count[i];
1483 error_cum += error_count[i];
1484 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1487 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1488 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1489 "% time", "seconds", "usecs/call",
1490 "calls", "errors", "syscall");
1491 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1492 dashes, dashes, dashes, dashes, dashes, dashes);
1493 for (i = 0; i < nsyscalls; i++) {
1494 j = sorted_count[i];
1495 if (call_count[j] == 0)
1497 tv_div(&dtv, &tv_count[j], call_count[j]);
1499 sprintf(error_str, "%d", error_count[j]);
1501 error_str[0] = '\0';
1502 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1503 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1504 percent, (long) tv_count[j].tv_sec,
1505 (long) tv_count[j].tv_usec,
1506 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1507 call_count[j], error_str, sysent[j].sys_name);
1509 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1510 dashes, dashes, dashes, dashes, dashes, dashes);
1512 sprintf(error_str, "%d", error_cum);
1514 error_str[0] = '\0';
1515 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1516 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1517 call_cum, error_str, "total");