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>
41 #if defined(LINUX) && defined(SPARC)
47 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
50 #if defined(linux) && !defined(__GLIBC__)
51 #include <linux/ptrace.h>
54 #ifndef SYS_ERRLIST_DECLARED
56 extern char *sys_errlist[];
57 #endif /* SYS_ERRLIST_DECLARED */
61 #define ERESTARTSYS 512
63 #ifndef ERESTARTNOINTR
64 #define ERESTARTNOINTR 513
66 #ifndef ERESTARTNOHAND
67 #define ERESTARTNOHAND 514 /* restart if no handler.. */
70 #define ENOIOCTLCMD 515 /* No ioctl command */
83 /* Define these shorthand notations to simplify the syscallent files. */
86 #define TN TRACE_NETWORK
87 #define TP TRACE_PROCESS
88 #define TS TRACE_SIGNAL
90 struct sysent sysent0[] = {
91 #include "syscallent.h"
93 int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
95 #if SUPPORTED_PERSONALITIES >= 2
96 struct sysent sysent1[] = {
97 #include "syscallent1.h"
99 int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
100 #endif /* SUPPORTED_PERSONALITIES >= 2 */
102 #if SUPPORTED_PERSONALITIES >= 3
103 struct sysent sysent2[] = {
104 #include "syscallent2.h"
106 int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
107 #endif /* SUPPORTED_PERSONALITIES >= 3 */
109 struct sysent *sysent;
112 /* Now undef them since short defines cause wicked namespace pollution. */
119 char *errnoent0[] = {
120 #include "errnoent.h"
122 int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
124 #if SUPPORTED_PERSONALITIES >= 2
125 char *errnoent1[] = {
126 #include "errnoent1.h"
128 int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
129 #endif /* SUPPORTED_PERSONALITIES >= 2 */
131 #if SUPPORTED_PERSONALITIES >= 3
132 char *errnoent2[] = {
133 #include "errnoent2.h"
135 int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
136 #endif /* SUPPORTED_PERSONALITIES >= 3 */
141 int current_personality;
144 set_personality(personality)
147 switch (personality) {
149 errnoent = errnoent0;
152 nsyscalls = nsyscalls0;
153 ioctlent = ioctlent0;
154 nioctlents = nioctlents0;
155 signalent = signalent0;
156 nsignals = nsignals0;
159 #if SUPPORTED_PERSONALITIES >= 2
161 errnoent = errnoent1;
164 nsyscalls = nsyscalls1;
165 ioctlent = ioctlent1;
166 nioctlents = nioctlents1;
167 signalent = signalent1;
168 nsignals = nsignals1;
170 #endif /* SUPPORTED_PERSONALITIES >= 2 */
172 #if SUPPORTED_PERSONALITIES >= 3
174 errnoent = errnoent2;
177 nsyscalls = nsyscalls2;
178 ioctlent = ioctlent2;
179 nioctlents = nioctlents2;
180 signalent = signalent2;
181 nsignals = nsignals2;
183 #endif /* SUPPORTED_PERSONALITIES >= 3 */
189 current_personality = personality;
193 int qual_flags[MAX_QUALS];
195 static int call_count[MAX_QUALS];
196 static int error_count[MAX_QUALS];
197 static struct timeval tv_count[MAX_QUALS];
198 static int sorted_count[MAX_QUALS];
200 static struct timeval shortest = { 1000000, 0 };
202 static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
204 static struct qual_options {
210 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
211 { QUAL_TRACE, "t", lookup_syscall, "system call" },
212 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
213 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
214 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
215 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
216 { QUAL_RAW, "raw", lookup_syscall, "system call" },
217 { QUAL_RAW, "x", lookup_syscall, "system call" },
218 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
219 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
220 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
221 { QUAL_FAULT, "fault", lookup_fault, "fault" },
222 { QUAL_FAULT, "faults", lookup_fault, "fault" },
223 { QUAL_FAULT, "m", lookup_fault, "fault" },
224 { QUAL_READ, "read", lookup_desc, "descriptor" },
225 { QUAL_READ, "reads", lookup_desc, "descriptor" },
226 { QUAL_READ, "r", lookup_desc, "descriptor" },
227 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
228 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
229 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
230 { 0, NULL, NULL, NULL },
239 for (i = 0; i < nsyscalls; i++) {
240 if (strcmp(s, sysent[i].sys_name) == 0)
253 if (s && *s && isdigit(*s))
257 for (i = 0; s[i]; i++)
258 s[i] = toupper(s[i]);
259 if (strncmp(s, "SIG", 3) == 0)
261 for (i = 0; i <= NSIG; i++) {
262 if (strcmp(s, signame(i) + 3) == 0)
279 if (s && *s && isdigit(*s))
288 if (strcmp(s, "file") == 0)
290 if (strcmp(s, "ipc") == 0)
292 if (strcmp(s, "network") == 0)
293 return TRACE_NETWORK;
294 if (strcmp(s, "process") == 0)
295 return TRACE_PROCESS;
296 if (strcmp(s, "signal") == 0)
305 struct qual_options *opt;
310 opt = &qual_options[0];
311 for (i = 0; (p = qual_options[i].option_name); i++) {
313 if (strncmp(s, p, n) == 0 && s[n] == '=') {
314 opt = &qual_options[i];
324 if (strcmp(s, "none") == 0) {
328 if (strcmp(s, "all") == 0) {
329 for (i = 0; i < MAX_QUALS; i++) {
331 qual_flags[i] &= ~opt->bitflag;
333 qual_flags[i] |= opt->bitflag;
337 for (i = 0; i < MAX_QUALS; i++) {
339 qual_flags[i] |= opt->bitflag;
341 qual_flags[i] &= ~opt->bitflag;
343 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
344 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
345 for (i = 0; i < MAX_QUALS; i++) {
346 if (sysent[i].sys_flags & n) {
348 qual_flags[i] &= ~opt->bitflag;
350 qual_flags[i] |= opt->bitflag;
355 if ((n = (*opt->lookup)(p)) < 0) {
356 fprintf(stderr, "strace: invalid %s `%s'\n",
357 opt->argument_name, p);
361 qual_flags[n] &= ~opt->bitflag;
363 qual_flags[n] |= opt->bitflag;
374 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
377 switch (tcp->scno + __NR_SYSCALL_BASE) {
388 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
389 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
398 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
399 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
404 enum subcall_style { shift_style, deref_style, mask_style, door_style };
406 #if !(defined(LINUX) && defined(ALPHA))
408 const int socket_map [] = {
411 /* SYS_CONNECT */ 98,
412 /* SYS_LISTEN */ 106,
414 /* SYS_GETSOCKNAME */ 150,
415 /* SYS_GETPEERNAME */ 141,
416 /* SYS_SOCKETPAIR */ 135,
419 /* SYS_SENDTO */ 133,
420 /* SYS_RECVFROM */ 125,
421 /* SYS_SHUTDOWN */ 134,
422 /* SYS_SETSOCKOPT */ 105,
423 /* SYS_GETSOCKOPT */ 118,
424 /* SYS_SENDMSG */ 114,
425 /* SYS_RECVMSG */ 113
429 sparc_socket_decode (tcp)
435 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
438 tcp->scno = socket_map [tcp->u_arg [0]-1];
439 n = tcp->u_nargs = sysent [tcp->scno].nargs;
440 addr = tcp->u_arg [1];
441 for (i = 0; i < n; i++){
443 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
445 tcp->u_arg [i] = arg;
446 addr += sizeof (arg);
451 decode_subcall(tcp, subcall, nsubcalls, style)
455 enum subcall_style style;
457 int i, addr, mask, arg;
459 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
463 tcp->scno = subcall + tcp->u_arg[0];
464 if (sysent[tcp->scno].nargs != -1)
465 tcp->u_nargs = sysent[tcp->scno].nargs;
468 for (i = 0; i < tcp->u_nargs; i++)
469 tcp->u_arg[i] = tcp->u_arg[i + 1];
472 tcp->scno = subcall + tcp->u_arg[0];
473 addr = tcp->u_arg[1];
474 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
475 if (umove(tcp, addr, &arg) < 0)
480 tcp->u_nargs = sysent[tcp->scno].nargs;
483 mask = (tcp->u_arg[0] >> 8) & 0xff;
484 tcp->u_arg[0] &= 0xff;
485 for (i = 0; mask; i++)
487 tcp->scno = subcall + i;
488 if (sysent[tcp->scno].nargs != -1)
489 tcp->u_nargs = sysent[tcp->scno].nargs;
493 * Oh, yuck. The call code is the *sixth* argument.
495 tcp->scno = subcall + tcp->u_arg[5];
496 if (sysent[tcp->scno].nargs != -1)
497 tcp->u_nargs = sysent[tcp->scno].nargs;
505 struct tcb *tcp_last = NULL;
508 internal_syscall(tcp)
512 * We must always trace a few critical system calls in order to
513 * correctly support following forks in the presence of tracing
517 switch (tcp->scno + __NR_SYSCALL_BASE) {
576 #elif defined (POWERPC)
582 #elif defined (ALPHA)
585 #elif defined (SPARC)
595 /* Measure the exit time as early as possible to avoid errors. */
596 if (dtime && (tcp->flags & TCB_INSYSCALL))
597 gettimeofday(&tv, NULL);
599 #if defined (POWERPC)
600 if (upeek(pid, 4*PT_R0, &scno) < 0)
602 if (!(tcp->flags & TCB_INSYSCALL)) {
603 /* Check if we return from execve. */
604 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
605 tcp->flags &= ~TCB_WAITEXECVE;
610 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
615 upeek(pid, 4*15, &pc);
616 umoven(tcp, pc-4, 4, (char *)&scno);
620 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
622 #elif defined (ALPHA)
623 if (upeek(pid, REG_A3, &a3) < 0)
626 if (!(tcp->flags & TCB_INSYSCALL)) {
627 if (upeek(pid, REG_R0, &scno) < 0)
630 /* Check if we return from execve. */
631 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
632 tcp->flags &= ~TCB_WAITEXECVE;
637 * Do some sanity checks to figure out if it's
638 * really a syscall entry
640 if (scno < 0 || scno > nsyscalls) {
641 if (a3 == 0 || a3 == -1) {
643 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
649 if (upeek(pid, REG_R0, &r0) < 0)
652 #elif defined (SPARC)
653 /* Everything we need is in the current register set. */
654 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
657 /* If we are entering, then disassemble the syscall trap. */
658 if (!(tcp->flags & TCB_INSYSCALL)) {
659 /* Retrieve the syscall trap instruction. */
661 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
665 /* Disassemble the trap to see what personality to use. */
668 /* Linux/SPARC syscall trap. */
672 /* Linux/SPARC64 syscall trap. */
673 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
676 /* SunOS syscall trap. (pers 1) */
677 fprintf(stderr,"syscall: SunOS no support\n");
680 /* Solaris 2.x syscall trap. (per 2) */
684 /* NetBSD/FreeBSD syscall trap. */
685 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
688 /* Solaris 2.x gettimeofday */
692 /* Unknown syscall trap. */
693 if(tcp->flags & TCB_WAITEXECVE) {
694 tcp->flags &= ~TCB_WAITEXECVE;
697 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
701 /* Extract the system call number from the registers. */
702 if (trap == 0x91d02027)
708 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
714 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
718 #ifdef HAVE_PR_SYSCALL
719 scno = tcp->status.pr_syscall;
720 #else /* !HAVE_PR_SYSCALL */
721 scno = tcp->status.PR_WHAT;
722 #endif /* !HAVE_PR_SYSCALL */
723 if (!(tcp->flags & TCB_INSYSCALL)) {
724 if (tcp->status.PR_WHY != PR_SYSENTRY) {
729 #endif /* SYS_vfork */
731 /* We are returning in the child, fake it. */
732 tcp->status.PR_WHY = PR_SYSENTRY;
734 tcp->status.PR_WHY = PR_SYSEXIT;
737 fprintf(stderr, "syscall: missing entry\n");
738 tcp->flags |= TCB_INSYSCALL;
743 if (tcp->status.PR_WHY != PR_SYSEXIT) {
744 fprintf(stderr, "syscall: missing exit\n");
745 tcp->flags &= ~TCB_INSYSCALL;
750 if (!(tcp->flags & TCB_INSYSCALL)) {
752 fprintf(stderr, "syscall: missing entry\n");
753 tcp->flags |= TCB_INSYSCALL;
760 * This happens when a signal handler
761 * for a signal which interrupted a
762 * a system call makes another system call.
764 fprintf(stderr, "syscall: missing exit\n");
766 tcp->flags &= ~TCB_INSYSCALL;
772 if (upeek(pid, 4*EAX, &eax) < 0)
774 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
776 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
779 #elif defined (POWERPC)
780 # define SO_MASK 0x10000000
781 if (upeek(pid, 4*PT_CCR, &flags) < 0)
783 if (upeek(pid, 4*PT_R3, &result) < 0)
788 if (upeek(pid, 4*PT_D0, &d0) < 0)
790 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
792 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
796 if (upeek(pid, 4*0, (long *)&r0) < 0)
798 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
800 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
807 if (tcp->flags & TCB_INSYSCALL) {
812 if (eax < 0 && -eax < nerrnos) {
822 if (result && (unsigned) -result < nerrnos) {
827 tcp->u_rval = result;
832 if (d0 && (unsigned) -d0 < nerrnos) {
842 if (r0 && (unsigned) -r0 < nerrnos) {
862 if (regs.r_psr & PSR_C) {
867 tcp->u_rval = regs.r_o0;
878 /* get error code from user struct */
879 if (upeek(pid, uoff(u_error), &u_error) < 0)
881 u_error >>= 24; /* u_error is a char */
883 /* get system call return value */
884 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
889 /* Judicious guessing goes a long way. */
890 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
892 u_error = tcp->status.pr_reg[R_O0];
895 tcp->u_rval = tcp->status.pr_reg[R_O0];
900 /* Wanna know how to kill an hour single-stepping? */
901 if (tcp->status.PR_REG[EFL] & 0x1) {
903 u_error = tcp->status.PR_REG[EAX];
906 tcp->u_rval = tcp->status.PR_REG[EAX];
911 if (tcp->status.pr_reg[CTX_A3]) {
913 u_error = tcp->status.pr_reg[CTX_V0];
916 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
921 tcp->u_error = u_error;
923 internal_syscall(tcp);
924 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
925 tcp->flags &= ~TCB_INSYSCALL;
929 if (tcp->flags & TCB_REPRINT) {
932 if (tcp->scno >= nsyscalls)
933 tprintf("syscall_%lu", tcp->scno);
935 tprintf("%s", sysent[tcp->scno].sys_name);
936 tprintf(" resumed> ");
940 call_count[tcp->scno]++;
942 error_count[tcp->scno]++;
943 tv_sub(&tv, &tv, &tcp->etime);
945 if (tv_cmp(&tv, &tcp->dtime) > 0) {
946 static struct timeval one_tick =
949 if (tv_nz(&tcp->dtime))
951 else if (tv_cmp(&tv, &one_tick) > 0) {
952 if (tv_cmp(&shortest, &one_tick) < 0)
959 if (tv_cmp(&tv, &shortest) < 0)
961 tv_add(&tv_count[tcp->scno],
962 &tv_count[tcp->scno], &tv);
963 tcp->flags &= ~TCB_INSYSCALL;
967 if (tcp->scno >= nsyscalls
968 || (qual_flags[tcp->scno] & QUAL_RAW))
969 sys_res = printargs(tcp);
971 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
972 u_error = tcp->u_error;
975 if (qual_flags[tcp->scno] & QUAL_RAW) {
977 tprintf("= -1 (errno %ld)", u_error);
979 tprintf("= %#lx", tcp->u_rval);
981 else if (!(sys_res & RVAL_NONE) && u_error) {
985 tprintf("= ? ERESTARTSYS (To be restarted)");
988 tprintf("= ? ERESTARTNOINTR (To be restarted)");
991 tprintf("= ? ERESTARTNOHAND (To be restarted)");
996 if (u_error < nerrnos && u_error < sys_nerr)
997 tprintf("%s (%s)", errnoent[u_error],
998 sys_errlist[u_error]);
999 else if (u_error < nerrnos)
1000 tprintf("%s (errno %ld)",
1001 errnoent[u_error], u_error);
1002 else if (u_error < sys_nerr)
1003 tprintf("ERRNO_%ld (%s)", u_error,
1004 sys_errlist[u_error]);
1006 tprintf("E??? (errno %ld)", u_error);
1013 if (sys_res & RVAL_NONE)
1016 switch (sys_res & RVAL_MASK) {
1018 tprintf("= %#lx", tcp->u_rval);
1021 tprintf("= %#lo", tcp->u_rval);
1024 tprintf("= %lu", tcp->u_rval);
1027 tprintf("= %ld", tcp->u_rval);
1031 "invalid rval format\n");
1035 if ((sys_res & RVAL_STR) && tcp->auxstr)
1036 tprintf(" (%s)", tcp->auxstr);
1039 tv_sub(&tv, &tv, &tcp->etime);
1040 tprintf(" <%ld.%06ld>",
1041 (long) tv.tv_sec, (long) tv.tv_usec);
1046 if (fflush(tcp->outf) == EOF)
1048 tcp->flags &= ~TCB_INSYSCALL;
1052 /* Entering system call */
1058 tcp->u_nargs = sysent[tcp->scno].nargs;
1059 for (i = 0; i < tcp->u_nargs; i++) {
1060 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1061 * for scno somewhere above here!
1063 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1067 #elif defined (POWERPC)
1070 tcp->u_nargs = sysent[tcp->scno].nargs;
1071 for (i = 0; i < tcp->u_nargs; i++) {
1072 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1076 #elif defined (SPARC)
1080 tcp->u_nargs = sysent[tcp->scno].nargs;
1081 for (i = 0; i < tcp->u_nargs; i++)
1082 tcp->u_arg[i] = *((®s.r_o0) + i);
1087 tcp->u_nargs = sysent[tcp->scno].nargs;
1088 for (i = 0; i < tcp->u_nargs; i++) {
1089 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1098 tcp->u_nargs = sysent[tcp->scno].nargs;
1099 for (i = 0; i < tcp->u_nargs; i++) {
1102 if (upeek(pid, uoff(u_arg[0]) +
1103 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1111 * SGI is broken: even though it has pr_sysarg, it doesn't
1112 * set them on system call entry. Get a clue.
1114 if (sysent[tcp->scno].nargs != -1)
1115 tcp->u_nargs = sysent[tcp->scno].nargs;
1117 tcp->u_nargs = tcp->status.pr_nsysarg;
1118 if (tcp->u_nargs > 4) {
1119 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1120 4*sizeof(tcp->u_arg[0]));
1121 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1122 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1125 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1126 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1129 #ifdef HAVE_PR_SYSCALL
1130 if (sysent[tcp->scno].nargs != -1)
1131 tcp->u_nargs = sysent[tcp->scno].nargs;
1133 tcp->u_nargs = tcp->status.pr_nsysarg;
1136 for (i = 0; i < tcp->u_nargs; i++)
1137 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1139 #else /* !HAVE_PR_SYSCALL */
1141 if (sysent[tcp->scno].nargs != -1)
1142 tcp->u_nargs = sysent[tcp->scno].nargs;
1145 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1149 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1150 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1152 #endif /* !HAVE_PR_SYSCALL */
1156 switch (tcp->scno + __NR_SYSCALL_BASE) {
1158 switch (tcp->scno) {
1161 #if !defined (ALPHA) && !defined(SPARC)
1162 case SYS_socketcall:
1163 decode_subcall(tcp, SYS_socket_subcall,
1164 SYS_socket_nsubcalls, deref_style);
1167 decode_subcall(tcp, SYS_ipc_subcall,
1168 SYS_ipc_nsubcalls, shift_style);
1170 #endif /* !ALPHA && !SPARC */
1172 case SYS_socketcall:
1173 sparc_socket_decode (tcp);
1178 #ifdef SYS_pgrpsys_subcall
1180 decode_subcall(tcp, SYS_pgrpsys_subcall,
1181 SYS_pgrpsys_nsubcalls, shift_style);
1183 #endif /* SYS_pgrpsys_subcall */
1184 #ifdef SYS_sigcall_subcall
1186 decode_subcall(tcp, SYS_sigcall_subcall,
1187 SYS_sigcall_nsubcalls, mask_style);
1189 #endif /* SYS_sigcall_subcall */
1191 decode_subcall(tcp, SYS_msgsys_subcall,
1192 SYS_msgsys_nsubcalls, shift_style);
1195 decode_subcall(tcp, SYS_shmsys_subcall,
1196 SYS_shmsys_nsubcalls, shift_style);
1199 decode_subcall(tcp, SYS_semsys_subcall,
1200 SYS_semsys_nsubcalls, shift_style);
1204 decode_subcall(tcp, SYS_utssys_subcall,
1205 SYS_utssys_nsubcalls, shift_style);
1209 decode_subcall(tcp, SYS_sysfs_subcall,
1210 SYS_sysfs_nsubcalls, shift_style);
1213 decode_subcall(tcp, SYS_spcall_subcall,
1214 SYS_spcall_nsubcalls, shift_style);
1216 #ifdef SYS_context_subcall
1218 decode_subcall(tcp, SYS_context_subcall,
1219 SYS_context_nsubcalls, shift_style);
1221 #endif /* SYS_context_subcall */
1222 #ifdef SYS_door_subcall
1224 decode_subcall(tcp, SYS_door_subcall,
1225 SYS_door_nsubcalls, door_style);
1227 #endif /* SYS_door_subcall */
1231 decode_subcall(tcp, SYS_semsys_subcall,
1232 SYS_semsys_nsubcalls, shift_style);
1235 decode_subcall(tcp, SYS_msgsys_subcall,
1236 SYS_msgsys_nsubcalls, shift_style);
1239 decode_subcall(tcp, SYS_shmsys_subcall,
1240 SYS_shmsys_nsubcalls, shift_style);
1245 internal_syscall(tcp);
1246 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1247 tcp->flags |= TCB_INSYSCALL;
1252 gettimeofday(&tcp->etime, NULL);
1253 tcp->flags |= TCB_INSYSCALL;
1258 tcp->flags &= ~TCB_REPRINT;
1260 if (tcp->scno >= nsyscalls)
1261 tprintf("syscall_%lu(", tcp->scno);
1263 tprintf("%s(", sysent[tcp->scno].sys_name);
1264 if (tcp->scno >= nsyscalls ||
1265 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1266 sys_res = printargs(tcp);
1268 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1269 if (fflush(tcp->outf) == EOF)
1271 tcp->flags |= TCB_INSYSCALL;
1272 /* Measure the entrance time as late as possible to avoid errors. */
1274 gettimeofday(&tcp->etime, NULL);
1282 if (entering(tcp)) {
1285 for (i = 0; i < tcp->u_nargs; i++)
1286 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1300 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
1307 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1313 val = tcp->status.PR_REG[R_O1];
1316 val = tcp->status.PR_REG[EDX];
1319 val = tcp->status.PR_REG[CTX_V1];
1327 * Apparently, indirect system calls have already be converted by ptrace(2),
1328 * so if you see "indir" this program has gone astray.
1336 if (entering(tcp)) {
1337 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1338 fprintf(stderr, "Bogus syscall: %u\n", scno);
1341 nargs = sysent[scno].nargs;
1342 tprintf("%s", sysent[scno].sys_name);
1343 for (i = 0; i < nargs; i++)
1344 tprintf(", %#lx", tcp->u_arg[i+1]);
1354 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1362 return strcmp(sysent[*((int *) a)].sys_name,
1363 sysent[*((int *) b)].sys_name);
1371 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1373 return (m < n) ? 1 : (m > n) ? -1 : 0;
1376 static int (*sortfun)();
1377 static struct timeval overhead = { -1, -1 };
1383 if (strcmp(sortby, "time") == 0)
1385 else if (strcmp(sortby, "calls") == 0)
1386 sortfun = count_cmp;
1387 else if (strcmp(sortby, "name") == 0)
1388 sortfun = syscall_cmp;
1389 else if (strcmp(sortby, "nothing") == 0)
1392 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1397 void set_overhead(n)
1400 overhead.tv_sec = n / 1000000;
1401 overhead.tv_usec = n % 1000000;
1409 int call_cum, error_cum;
1410 struct timeval tv_cum, dtv;
1412 char *dashes = "-------------------------";
1415 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1416 if (overhead.tv_sec == -1) {
1417 tv_mul(&overhead, &shortest, 8);
1418 tv_div(&overhead, &overhead, 10);
1420 for (i = 0; i < nsyscalls; i++) {
1421 sorted_count[i] = i;
1422 if (call_count[i] == 0)
1424 tv_mul(&dtv, &overhead, call_count[i]);
1425 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1426 call_cum += call_count[i];
1427 error_cum += error_count[i];
1428 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1431 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1432 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1433 "% time", "seconds", "usecs/call",
1434 "calls", "errors", "syscall");
1435 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1436 dashes, dashes, dashes, dashes, dashes, dashes);
1437 for (i = 0; i < nsyscalls; i++) {
1438 j = sorted_count[i];
1439 if (call_count[j] == 0)
1441 tv_div(&dtv, &tv_count[j], call_count[j]);
1443 sprintf(error_str, "%d", error_count[j]);
1445 error_str[0] = '\0';
1446 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1447 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1448 percent, (long) tv_count[j].tv_sec,
1449 (long) tv_count[j].tv_usec,
1450 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1451 call_count[j], error_str, sysent[j].sys_name);
1453 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1454 dashes, dashes, dashes, dashes, dashes, dashes);
1456 sprintf(error_str, "%d", error_cum);
1458 error_str[0] = '\0';
1459 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1460 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1461 call_cum, error_str, "total");