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 __GLIBC__ == 2 && ((defined(I386) && __GLIBC_MINOR__ >= 0) || (defined(M68K) && __GLIBC_MINOR__ >= 1))
45 # include <linux/ptrace.h>
49 #ifndef SYS_ERRLIST_DECLARED
51 extern char *sys_errlist[];
52 #endif /* SYS_ERRLIST_DECLARED */
56 #define ERESTARTSYS 512
58 #ifndef ERESTARTNOINTR
59 #define ERESTARTNOINTR 513
61 #ifndef ERESTARTNOHAND
62 #define ERESTARTNOHAND 514 /* restart if no handler.. */
65 #define ENOIOCTLCMD 515 /* No ioctl command */
78 /* Define these shorthand notations to simplify the syscallent files. */
81 #define TN TRACE_NETWORK
82 #define TP TRACE_PROCESS
83 #define TS TRACE_SIGNAL
85 struct sysent sysent0[] = {
86 #include "syscallent.h"
88 int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
90 #if SUPPORTED_PERSONALITIES >= 2
91 struct sysent sysent1[] = {
92 #include "syscallent1.h"
94 int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
95 #endif /* SUPPORTED_PERSONALITIES >= 2 */
97 #if SUPPORTED_PERSONALITIES >= 3
98 struct sysent sysent2[] = {
99 #include "syscallent2.h"
101 int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
102 #endif /* SUPPORTED_PERSONALITIES >= 3 */
104 struct sysent *sysent;
107 /* Now undef them since short defines cause wicked namespace pollution. */
114 char *errnoent0[] = {
115 #include "errnoent.h"
117 int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
119 #if SUPPORTED_PERSONALITIES >= 2
120 char *errnoent1[] = {
121 #include "errnoent1.h"
123 int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
124 #endif /* SUPPORTED_PERSONALITIES >= 2 */
126 #if SUPPORTED_PERSONALITIES >= 3
127 char *errnoent2[] = {
128 #include "errnoent2.h"
130 int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
131 #endif /* SUPPORTED_PERSONALITIES >= 3 */
136 int current_personality;
139 set_personality(personality)
142 switch (personality) {
144 errnoent = errnoent0;
147 nsyscalls = nsyscalls0;
148 ioctlent = ioctlent0;
149 nioctlents = nioctlents0;
150 signalent = signalent0;
151 nsignals = nsignals0;
154 #if SUPPORTED_PERSONALITIES >= 2
156 errnoent = errnoent1;
159 nsyscalls = nsyscalls1;
160 ioctlent = ioctlent1;
161 nioctlents = nioctlents1;
162 signalent = signalent1;
163 nsignals = nsignals1;
165 #endif /* SUPPORTED_PERSONALITIES >= 2 */
167 #if SUPPORTED_PERSONALITIES >= 3
169 errnoent = errnoent2;
172 nsyscalls = nsyscalls2;
173 ioctlent = ioctlent2;
174 nioctlents = nioctlents2;
175 signalent = signalent2;
176 nsignals = nsignals2;
178 #endif /* SUPPORTED_PERSONALITIES >= 3 */
184 current_personality = personality;
188 int qual_flags[MAX_QUALS];
190 static int call_count[MAX_QUALS];
191 static int error_count[MAX_QUALS];
192 static struct timeval tv_count[MAX_QUALS];
193 static int sorted_count[MAX_QUALS];
195 static struct timeval shortest = { 1000000, 0 };
197 static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
199 static struct qual_options {
205 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
206 { QUAL_TRACE, "t", lookup_syscall, "system call" },
207 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
208 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
209 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
210 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
211 { QUAL_RAW, "raw", lookup_syscall, "system call" },
212 { QUAL_RAW, "x", lookup_syscall, "system call" },
213 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
214 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
215 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
216 { QUAL_FAULT, "fault", lookup_fault, "fault" },
217 { QUAL_FAULT, "faults", lookup_fault, "fault" },
218 { QUAL_FAULT, "m", lookup_fault, "fault" },
219 { QUAL_READ, "read", lookup_desc, "descriptor" },
220 { QUAL_READ, "reads", lookup_desc, "descriptor" },
221 { QUAL_READ, "r", lookup_desc, "descriptor" },
222 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
223 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
224 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
225 { 0, NULL, NULL, NULL },
234 for (i = 0; i < nsyscalls; i++) {
235 if (strcmp(s, sysent[i].sys_name) == 0)
248 if (s && *s && isdigit(*s))
252 for (i = 0; s[i]; i++)
253 s[i] = toupper(s[i]);
254 if (strncmp(s, "SIG", 3) == 0)
256 for (i = 0; i <= NSIG; i++) {
257 if (strcmp(s, signame(i) + 3) == 0)
274 if (s && *s && isdigit(*s))
283 if (strcmp(s, "file") == 0)
285 if (strcmp(s, "ipc") == 0)
287 if (strcmp(s, "network") == 0)
288 return TRACE_NETWORK;
289 if (strcmp(s, "process") == 0)
290 return TRACE_PROCESS;
291 if (strcmp(s, "signal") == 0)
300 struct qual_options *opt;
305 opt = &qual_options[0];
306 for (i = 0; (p = qual_options[i].option_name); i++) {
308 if (strncmp(s, p, n) == 0 && s[n] == '=') {
309 opt = &qual_options[i];
319 if (strcmp(s, "none") == 0) {
323 if (strcmp(s, "all") == 0) {
324 for (i = 0; i < MAX_QUALS; i++) {
326 qual_flags[i] &= ~opt->bitflag;
328 qual_flags[i] |= opt->bitflag;
332 for (i = 0; i < MAX_QUALS; i++) {
334 qual_flags[i] |= opt->bitflag;
336 qual_flags[i] &= ~opt->bitflag;
338 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
339 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
340 for (i = 0; i < MAX_QUALS; i++) {
341 if (sysent[i].sys_flags & n) {
343 qual_flags[i] &= ~opt->bitflag;
345 qual_flags[i] |= opt->bitflag;
350 if ((n = (*opt->lookup)(p)) < 0) {
351 fprintf(stderr, "strace: invalid %s `%s'\n",
352 opt->argument_name, p);
356 qual_flags[n] &= ~opt->bitflag;
358 qual_flags[n] |= opt->bitflag;
369 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
372 switch (tcp->scno + __NR_SYSCALL_BASE) {
383 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
384 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
393 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
394 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
399 enum subcall_style { shift_style, deref_style, mask_style, door_style };
401 #if !(defined(LINUX) && defined(ALPHA))
403 const int socket_map [] = {
406 /* SYS_CONNECT */ 98,
407 /* SYS_LISTEN */ 106,
409 /* SYS_GETSOCKNAME */ 150,
410 /* SYS_GETPEERNAME */ 141,
411 /* SYS_SOCKETPAIR */ 135,
414 /* SYS_SENDTO */ 133,
415 /* SYS_RECVFROM */ 125,
416 /* SYS_SHUTDOWN */ 134,
417 /* SYS_SETSOCKOPT */ 105,
418 /* SYS_GETSOCKOPT */ 118,
419 /* SYS_SENDMSG */ 114,
420 /* SYS_RECVMSG */ 113
424 sparc_socket_decode (tcp)
430 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
433 tcp->scno = socket_map [tcp->u_arg [0]-1];
434 n = tcp->u_nargs = sysent [tcp->scno].nargs;
435 addr = tcp->u_arg [1];
436 for (i = 0; i < n; i++){
438 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
440 tcp->u_arg [i] = arg;
441 addr += sizeof (arg);
446 decode_subcall(tcp, subcall, nsubcalls, style)
450 enum subcall_style style;
452 int i, addr, mask, arg;
454 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
458 tcp->scno = subcall + tcp->u_arg[0];
459 if (sysent[tcp->scno].nargs != -1)
460 tcp->u_nargs = sysent[tcp->scno].nargs;
463 for (i = 0; i < tcp->u_nargs; i++)
464 tcp->u_arg[i] = tcp->u_arg[i + 1];
467 tcp->scno = subcall + tcp->u_arg[0];
468 addr = tcp->u_arg[1];
469 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
470 if (umove(tcp, addr, &arg) < 0)
475 tcp->u_nargs = sysent[tcp->scno].nargs;
478 mask = (tcp->u_arg[0] >> 8) & 0xff;
479 tcp->u_arg[0] &= 0xff;
480 for (i = 0; mask; i++)
482 tcp->scno = subcall + i;
483 if (sysent[tcp->scno].nargs != -1)
484 tcp->u_nargs = sysent[tcp->scno].nargs;
488 * Oh, yuck. The call code is the *sixth* argument.
490 tcp->scno = subcall + tcp->u_arg[5];
491 if (sysent[tcp->scno].nargs != -1)
492 tcp->u_nargs = sysent[tcp->scno].nargs;
500 struct tcb *tcp_last = NULL;
503 internal_syscall(tcp)
507 * We must always trace a few critical system calls in order to
508 * correctly support following forks in the presence of tracing
512 switch (tcp->scno + __NR_SYSCALL_BASE) {
571 #elif defined (POWERPC)
577 #elif defined (ALPHA)
580 #elif defined (SPARC)
590 /* Measure the exit time as early as possible to avoid errors. */
591 if (dtime && (tcp->flags & TCB_INSYSCALL))
592 gettimeofday(&tv, NULL);
594 #if defined (POWERPC)
595 if (upeek(pid, 4*PT_R0, &scno) < 0)
597 if (!(tcp->flags & TCB_INSYSCALL)) {
598 /* Check if we return from execve. */
599 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
600 tcp->flags &= ~TCB_WAITEXECVE;
605 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
610 upeek(pid, 4*15, &pc);
611 umoven(tcp, pc-4, 4, (char *)&scno);
615 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
617 #elif defined (ALPHA)
618 if (upeek(pid, REG_A3, &a3) < 0)
621 if (!(tcp->flags & TCB_INSYSCALL)) {
622 if (upeek(pid, REG_R0, &scno) < 0)
625 /* Check if we return from execve. */
626 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
627 tcp->flags &= ~TCB_WAITEXECVE;
632 * Do some sanity checks to figure out if it's
633 * really a syscall entry
635 if (scno < 0 || scno > nsyscalls) {
636 if (a3 == 0 || a3 == -1) {
638 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
644 if (upeek(pid, REG_R0, &r0) < 0)
647 #elif defined (SPARC)
648 /* Everything we need is in the current register set. */
649 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
652 memmove (®s.u_regs [1], ®s.u_regs [0],
653 sizeof (regs.u_regs) - sizeof (regs.u_regs [0]));
655 /* If we are entering, then disassemble the syscall trap. */
656 if (!(tcp->flags & TCB_INSYSCALL)) {
657 /* Retrieve the syscall trap instruction. */
659 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.pc,0);
663 /* Disassemble the trap to see what personality to use. */
666 /* Linux/SPARC syscall trap. */
670 /* Linux/SPARC64 syscall trap. */
671 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
674 /* SunOS syscall trap. (pers 1) */
675 fprintf(stderr,"syscall: SunOS no support\n");
678 /* Solaris 2.x syscall trap. (per 2) */
682 /* NetBSD/FreeBSD syscall trap. */
683 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
686 /* Solaris 2.x gettimeofday */
690 /* Unknown syscall trap. */
691 if(tcp->flags & TCB_WAITEXECVE) {
692 tcp->flags &= ~TCB_WAITEXECVE;
695 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.pc);
699 /* Extract the system call number from the registers. */
700 if (trap == 0x91d02027)
703 scno = regs.u_regs[UREG_G1];
705 scno = regs.u_regs[UREG_I0];
706 memmove (®s.u_regs[UREG_I0], ®s.u_regs[UREG_I1], 7*sizeof(regs.u_regs[0]));
712 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
716 #ifdef HAVE_PR_SYSCALL
717 scno = tcp->status.pr_syscall;
718 #else /* !HAVE_PR_SYSCALL */
719 scno = tcp->status.pr_what;
720 #endif /* !HAVE_PR_SYSCALL */
721 if (!(tcp->flags & TCB_INSYSCALL)) {
722 if (tcp->status.pr_why != PR_SYSENTRY) {
727 #endif /* SYS_vfork */
729 /* We are returning in the child, fake it. */
730 tcp->status.pr_why = PR_SYSENTRY;
732 tcp->status.pr_why = PR_SYSEXIT;
735 fprintf(stderr, "syscall: missing entry\n");
736 tcp->flags |= TCB_INSYSCALL;
741 if (tcp->status.pr_why != PR_SYSEXIT) {
742 fprintf(stderr, "syscall: missing exit\n");
743 tcp->flags &= ~TCB_INSYSCALL;
748 if (!(tcp->flags & TCB_INSYSCALL)) {
750 fprintf(stderr, "syscall: missing entry\n");
751 tcp->flags |= TCB_INSYSCALL;
758 * This happens when a signal handler
759 * for a signal which interrupted a
760 * a system call makes another system call.
762 fprintf(stderr, "syscall: missing exit\n");
764 tcp->flags &= ~TCB_INSYSCALL;
770 if (upeek(pid, 4*EAX, &eax) < 0)
772 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
774 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
777 #elif defined (POWERPC)
778 # define SO_MASK 0x10000000
779 if (upeek(pid, 4*PT_CCR, &flags) < 0)
781 if (upeek(pid, 4*PT_R3, &result) < 0)
786 if (upeek(pid, 4*PT_D0, &d0) < 0)
788 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
790 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
794 if (upeek(pid, 4*0, (long *)&r0) < 0)
796 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
798 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
805 if (tcp->flags & TCB_INSYSCALL) {
810 if (eax < 0 && -eax < nerrnos) {
820 if (result && (unsigned) -result < nerrnos) {
825 tcp->u_rval = result;
830 if (d0 && (unsigned) -d0 < nerrnos) {
840 if (r0 && (unsigned) -r0 < nerrnos) {
860 if (regs.psr & PSR_C) {
862 u_error = regs.u_regs[UREG_I0];
865 tcp->u_rval = regs.u_regs[UREG_I0];
876 /* get error code from user struct */
877 if (upeek(pid, uoff(u_error), &u_error) < 0)
879 u_error >>= 24; /* u_error is a char */
881 /* get system call return value */
882 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
887 /* Judicious guessing goes a long way. */
888 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
890 u_error = tcp->status.pr_reg[R_O0];
893 tcp->u_rval = tcp->status.pr_reg[R_O0];
898 /* Wanna know how to kill an hour single-stepping? */
899 if (tcp->status.pr_reg[EFL] & 0x1) {
901 u_error = tcp->status.pr_reg[EAX];
904 tcp->u_rval = tcp->status.pr_reg[EAX];
909 if (tcp->status.pr_reg[CTX_A3]) {
911 u_error = tcp->status.pr_reg[CTX_V0];
914 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
919 tcp->u_error = u_error;
921 internal_syscall(tcp);
922 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
923 tcp->flags &= ~TCB_INSYSCALL;
927 if (tcp->flags & TCB_REPRINT) {
930 if (tcp->scno >= nsyscalls)
931 tprintf("syscall_%lu", tcp->scno);
933 tprintf("%s", sysent[tcp->scno].sys_name);
934 tprintf(" resumed> ");
938 call_count[tcp->scno]++;
940 error_count[tcp->scno]++;
941 tv_sub(&tv, &tv, &tcp->etime);
943 if (tv_cmp(&tv, &tcp->dtime) > 0) {
944 static struct timeval one_tick =
947 if (tv_nz(&tcp->dtime))
949 else if (tv_cmp(&tv, &one_tick) > 0) {
950 if (tv_cmp(&shortest, &one_tick) < 0)
957 if (tv_cmp(&tv, &shortest) < 0)
959 tv_add(&tv_count[tcp->scno],
960 &tv_count[tcp->scno], &tv);
961 tcp->flags &= ~TCB_INSYSCALL;
965 if (tcp->scno >= nsyscalls
966 || (qual_flags[tcp->scno] & QUAL_RAW))
967 sys_res = printargs(tcp);
969 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
970 u_error = tcp->u_error;
973 if (qual_flags[tcp->scno] & QUAL_RAW) {
975 tprintf("= -1 (errno %ld)", u_error);
977 tprintf("= %#lx", tcp->u_rval);
979 else if (!(sys_res & RVAL_NONE) && u_error) {
983 tprintf("= ? ERESTARTSYS (To be restarted)");
986 tprintf("= ? ERESTARTNOINTR (To be restarted)");
989 tprintf("= ? ERESTARTNOHAND (To be restarted)");
994 if (u_error < nerrnos && u_error < sys_nerr)
995 tprintf("%s (%s)", errnoent[u_error],
996 sys_errlist[u_error]);
997 else if (u_error < nerrnos)
998 tprintf("%s (errno %ld)",
999 errnoent[u_error], u_error);
1000 else if (u_error < sys_nerr)
1001 tprintf("ERRNO_%ld (%s)", u_error,
1002 sys_errlist[u_error]);
1004 tprintf("E??? (errno %ld)", u_error);
1011 if (sys_res & RVAL_NONE)
1014 switch (sys_res & RVAL_MASK) {
1016 tprintf("= %#lx", tcp->u_rval);
1019 tprintf("= %#lo", tcp->u_rval);
1022 tprintf("= %lu", tcp->u_rval);
1025 tprintf("= %ld", tcp->u_rval);
1029 "invalid rval format\n");
1033 if ((sys_res & RVAL_STR) && tcp->auxstr)
1034 tprintf(" (%s)", tcp->auxstr);
1037 tv_sub(&tv, &tv, &tcp->etime);
1038 tprintf(" <%ld.%06ld>",
1039 (long) tv.tv_sec, (long) tv.tv_usec);
1044 if (fflush(tcp->outf) == EOF)
1046 tcp->flags &= ~TCB_INSYSCALL;
1050 /* Entering system call */
1056 tcp->u_nargs = sysent[tcp->scno].nargs;
1057 for (i = 0; i < tcp->u_nargs; i++) {
1058 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1059 * for scno somewhere above here!
1061 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1065 #elif defined (POWERPC)
1068 tcp->u_nargs = sysent[tcp->scno].nargs;
1069 for (i = 0; i < tcp->u_nargs; i++) {
1070 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1074 #elif defined (SPARC)
1079 tcp->u_nargs = sysent[tcp->scno].nargs;
1080 for (i = 0; i < tcp->u_nargs; i++)
1081 tcp->u_arg[i] = regs.u_regs[offset + i];
1086 tcp->u_nargs = sysent[tcp->scno].nargs;
1087 for (i = 0; i < tcp->u_nargs; i++) {
1088 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1097 tcp->u_nargs = sysent[tcp->scno].nargs;
1098 for (i = 0; i < tcp->u_nargs; i++) {
1101 if (upeek(pid, uoff(u_arg[0]) +
1102 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1110 * SGI is broken: even though it has pr_sysarg, it doesn't
1111 * set them on system call entry. Get a clue.
1113 if (sysent[tcp->scno].nargs != -1)
1114 tcp->u_nargs = sysent[tcp->scno].nargs;
1116 tcp->u_nargs = tcp->status.pr_nsysarg;
1117 if (tcp->u_nargs > 4) {
1118 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1119 4*sizeof(tcp->u_arg[0]));
1120 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1121 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1124 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1125 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1128 #ifdef HAVE_PR_SYSCALL
1129 if (sysent[tcp->scno].nargs != -1)
1130 tcp->u_nargs = sysent[tcp->scno].nargs;
1132 tcp->u_nargs = tcp->status.pr_nsysarg;
1135 for (i = 0; i < tcp->u_nargs; i++)
1136 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1138 #else /* !HAVE_PR_SYSCALL */
1140 if (sysent[tcp->scno].nargs != -1)
1141 tcp->u_nargs = sysent[tcp->scno].nargs;
1144 umoven(tcp, tcp->status.pr_reg[UESP] + 4,
1145 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1147 #endif /* !HAVE_PR_SYSCALL */
1151 switch (tcp->scno + __NR_SYSCALL_BASE) {
1153 switch (tcp->scno) {
1156 #if !defined (ALPHA) && !defined(SPARC)
1157 case SYS_socketcall:
1158 decode_subcall(tcp, SYS_socket_subcall,
1159 SYS_socket_nsubcalls, deref_style);
1162 decode_subcall(tcp, SYS_ipc_subcall,
1163 SYS_ipc_nsubcalls, shift_style);
1165 #endif /* !ALPHA && !SPARC */
1167 case SYS_socketcall:
1168 sparc_socket_decode (tcp);
1173 #ifdef SYS_pgrpsys_subcall
1175 decode_subcall(tcp, SYS_pgrpsys_subcall,
1176 SYS_pgrpsys_nsubcalls, shift_style);
1178 #endif /* SYS_pgrpsys_subcall */
1179 #ifdef SYS_sigcall_subcall
1181 decode_subcall(tcp, SYS_sigcall_subcall,
1182 SYS_sigcall_nsubcalls, mask_style);
1184 #endif /* SYS_sigcall_subcall */
1186 decode_subcall(tcp, SYS_msgsys_subcall,
1187 SYS_msgsys_nsubcalls, shift_style);
1190 decode_subcall(tcp, SYS_shmsys_subcall,
1191 SYS_shmsys_nsubcalls, shift_style);
1194 decode_subcall(tcp, SYS_semsys_subcall,
1195 SYS_semsys_nsubcalls, shift_style);
1199 decode_subcall(tcp, SYS_utssys_subcall,
1200 SYS_utssys_nsubcalls, shift_style);
1204 decode_subcall(tcp, SYS_sysfs_subcall,
1205 SYS_sysfs_nsubcalls, shift_style);
1208 decode_subcall(tcp, SYS_spcall_subcall,
1209 SYS_spcall_nsubcalls, shift_style);
1211 #ifdef SYS_context_subcall
1213 decode_subcall(tcp, SYS_context_subcall,
1214 SYS_context_nsubcalls, shift_style);
1216 #endif /* SYS_context_subcall */
1217 #ifdef SYS_door_subcall
1219 decode_subcall(tcp, SYS_door_subcall,
1220 SYS_door_nsubcalls, door_style);
1222 #endif /* SYS_door_subcall */
1226 decode_subcall(tcp, SYS_semsys_subcall,
1227 SYS_semsys_nsubcalls, shift_style);
1230 decode_subcall(tcp, SYS_msgsys_subcall,
1231 SYS_msgsys_nsubcalls, shift_style);
1234 decode_subcall(tcp, SYS_shmsys_subcall,
1235 SYS_shmsys_nsubcalls, shift_style);
1240 internal_syscall(tcp);
1241 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1242 tcp->flags |= TCB_INSYSCALL;
1247 gettimeofday(&tcp->etime, NULL);
1248 tcp->flags |= TCB_INSYSCALL;
1253 tcp->flags &= ~TCB_REPRINT;
1255 if (tcp->scno >= nsyscalls)
1256 tprintf("syscall_%lu(", tcp->scno);
1258 tprintf("%s(", sysent[tcp->scno].sys_name);
1259 if (tcp->scno >= nsyscalls ||
1260 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1261 sys_res = printargs(tcp);
1263 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1264 if (fflush(tcp->outf) == EOF)
1266 tcp->flags |= TCB_INSYSCALL;
1267 /* Measure the entrance time as late as possible to avoid errors. */
1269 gettimeofday(&tcp->etime, NULL);
1277 if (entering(tcp)) {
1280 for (i = 0; i < tcp->u_nargs; i++)
1281 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1294 struct pt_regs regs;
1295 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
1297 val = regs.u_regs[UREG_I1];
1302 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1308 val = tcp->status.pr_reg[R_O1];
1311 val = tcp->status.pr_reg[EDX];
1314 val = tcp->status.pr_reg[CTX_V1];
1322 * Apparently, indirect system calls have already be converted by ptrace(2),
1323 * so if you see "indir" this program has gone astray.
1331 if (entering(tcp)) {
1332 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1333 fprintf(stderr, "Bogus syscall: %u\n", scno);
1336 nargs = sysent[scno].nargs;
1337 tprintf("%s", sysent[scno].sys_name);
1338 for (i = 0; i < nargs; i++)
1339 tprintf(", %#lx", tcp->u_arg[i+1]);
1349 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1357 return strcmp(sysent[*((int *) a)].sys_name,
1358 sysent[*((int *) b)].sys_name);
1366 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1368 return (m < n) ? 1 : (m > n) ? -1 : 0;
1371 static int (*sortfun)();
1372 static struct timeval overhead = { -1, -1 };
1378 if (strcmp(sortby, "time") == 0)
1380 else if (strcmp(sortby, "calls") == 0)
1381 sortfun = count_cmp;
1382 else if (strcmp(sortby, "name") == 0)
1383 sortfun = syscall_cmp;
1384 else if (strcmp(sortby, "nothing") == 0)
1387 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1392 void set_overhead(n)
1395 overhead.tv_sec = n / 1000000;
1396 overhead.tv_usec = n % 1000000;
1404 int call_cum, error_cum;
1405 struct timeval tv_cum, dtv;
1407 char *dashes = "-------------------------";
1410 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1411 if (overhead.tv_sec == -1) {
1412 tv_mul(&overhead, &shortest, 8);
1413 tv_div(&overhead, &overhead, 10);
1415 for (i = 0; i < nsyscalls; i++) {
1416 sorted_count[i] = i;
1417 if (call_count[i] == 0)
1419 tv_mul(&dtv, &overhead, call_count[i]);
1420 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1421 call_cum += call_count[i];
1422 error_cum += error_count[i];
1423 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1426 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1427 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1428 "% time", "seconds", "usecs/call",
1429 "calls", "errors", "syscall");
1430 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1431 dashes, dashes, dashes, dashes, dashes, dashes);
1432 for (i = 0; i < nsyscalls; i++) {
1433 j = sorted_count[i];
1434 if (call_count[j] == 0)
1436 tv_div(&dtv, &tv_count[j], call_count[j]);
1438 sprintf(error_str, "%d", error_count[j]);
1440 error_str[0] = '\0';
1441 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1442 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1443 percent, (long) tv_count[j].tv_sec,
1444 (long) tv_count[j].tv_usec,
1445 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1446 call_count[j], error_str, sysent[j].sys_name);
1448 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1449 dashes, dashes, dashes, dashes, dashes, dashes);
1451 sprintf(error_str, "%d", error_cum);
1453 error_str[0] = '\0';
1454 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1455 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1456 call_cum, error_str, "total");