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>
40 #if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 1 && (defined(I386) || defined(M68K))
45 #include <linux/ptrace.h>
48 #ifndef SYS_ERRLIST_DECLARED
50 extern char *sys_errlist[];
51 #endif /* SYS_ERRLIST_DECLARED */
55 #define ERESTARTSYS 512
57 #ifndef ERESTARTNOINTR
58 #define ERESTARTNOINTR 513
60 #ifndef ERESTARTNOHAND
61 #define ERESTARTNOHAND 514 /* restart if no handler.. */
64 #define ENOIOCTLCMD 515 /* No ioctl command */
77 /* Define these shorthand notations to simplify the syscallent files. */
80 #define TN TRACE_NETWORK
81 #define TP TRACE_PROCESS
82 #define TS TRACE_SIGNAL
84 struct sysent sysent0[] = {
85 #include "syscallent.h"
87 int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
89 #if SUPPORTED_PERSONALITIES >= 2
90 struct sysent sysent1[] = {
91 #include "syscallent1.h"
93 int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
94 #endif /* SUPPORTED_PERSONALITIES >= 2 */
96 #if SUPPORTED_PERSONALITIES >= 3
97 struct sysent sysent2[] = {
98 #include "syscallent2.h"
100 int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
101 #endif /* SUPPORTED_PERSONALITIES >= 3 */
103 struct sysent *sysent;
106 /* Now undef them since short defines cause wicked namespace pollution. */
113 char *errnoent0[] = {
114 #include "errnoent.h"
116 int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
118 #if SUPPORTED_PERSONALITIES >= 2
119 char *errnoent1[] = {
120 #include "errnoent1.h"
122 int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
123 #endif /* SUPPORTED_PERSONALITIES >= 2 */
125 #if SUPPORTED_PERSONALITIES >= 3
126 char *errnoent2[] = {
127 #include "errnoent2.h"
129 int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
130 #endif /* SUPPORTED_PERSONALITIES >= 3 */
135 int current_personality;
138 set_personality(int personality)
140 switch (personality) {
142 errnoent = errnoent0;
145 nsyscalls = nsyscalls0;
146 ioctlent = ioctlent0;
147 nioctlents = nioctlents0;
148 signalent = signalent0;
149 nsignals = nsignals0;
152 #if SUPPORTED_PERSONALITIES >= 2
154 errnoent = errnoent1;
157 nsyscalls = nsyscalls1;
158 ioctlent = ioctlent1;
159 nioctlents = nioctlents1;
160 signalent = signalent1;
161 nsignals = nsignals1;
163 #endif /* SUPPORTED_PERSONALITIES >= 2 */
165 #if SUPPORTED_PERSONALITIES >= 3
167 errnoent = errnoent2;
170 nsyscalls = nsyscalls2;
171 ioctlent = ioctlent2;
172 nioctlents = nioctlents2;
173 signalent = signalent2;
174 nsignals = nsignals2;
176 #endif /* SUPPORTED_PERSONALITIES >= 3 */
182 current_personality = personality;
186 int qual_flags[MAX_QUALS];
188 static int call_count[MAX_QUALS];
189 static int error_count[MAX_QUALS];
190 static struct timeval tv_count[MAX_QUALS];
191 static int sorted_count[MAX_QUALS];
193 static struct timeval shortest = { 1000000, 0 };
195 static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
197 static struct qual_options {
203 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
204 { QUAL_TRACE, "t", lookup_syscall, "system call" },
205 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
206 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
207 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
208 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
209 { QUAL_RAW, "raw", lookup_syscall, "system call" },
210 { QUAL_RAW, "x", lookup_syscall, "system call" },
211 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
212 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
213 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
214 { QUAL_FAULT, "fault", lookup_fault, "fault" },
215 { QUAL_FAULT, "faults", lookup_fault, "fault" },
216 { QUAL_FAULT, "m", lookup_fault, "fault" },
217 { QUAL_READ, "read", lookup_desc, "descriptor" },
218 { QUAL_READ, "reads", lookup_desc, "descriptor" },
219 { QUAL_READ, "r", lookup_desc, "descriptor" },
220 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
221 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
222 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
223 { 0, NULL, NULL, NULL },
232 for (i = 0; i < nsyscalls; i++) {
233 if (strcmp(s, sysent[i].sys_name) == 0)
246 if (s && *s && isdigit(*s))
250 for (i = 0; s[i]; i++)
251 s[i] = toupper(s[i]);
252 if (strncmp(s, "SIG", 3) == 0)
254 for (i = 0; i <= NSIG; i++) {
255 if (strcmp(s, signame(i) + 3) == 0)
272 if (s && *s && isdigit(*s))
281 if (strcmp(s, "file") == 0)
283 if (strcmp(s, "ipc") == 0)
285 if (strcmp(s, "network") == 0)
286 return TRACE_NETWORK;
287 if (strcmp(s, "process") == 0)
288 return TRACE_PROCESS;
289 if (strcmp(s, "signal") == 0)
298 struct qual_options *opt;
303 opt = &qual_options[0];
304 for (i = 0; (p = qual_options[i].option_name); i++) {
306 if (strncmp(s, p, n) == 0 && s[n] == '=') {
307 opt = &qual_options[i];
317 if (strcmp(s, "none") == 0) {
321 if (strcmp(s, "all") == 0) {
322 for (i = 0; i < MAX_QUALS; i++) {
324 qual_flags[i] &= ~opt->bitflag;
326 qual_flags[i] |= opt->bitflag;
330 for (i = 0; i < MAX_QUALS; i++) {
332 qual_flags[i] |= opt->bitflag;
334 qual_flags[i] &= ~opt->bitflag;
336 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
337 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
338 for (i = 0; i < MAX_QUALS; i++) {
339 if (sysent[i].sys_flags & n) {
341 qual_flags[i] &= ~opt->bitflag;
343 qual_flags[i] |= opt->bitflag;
348 if ((n = (*opt->lookup)(p)) < 0) {
349 fprintf(stderr, "strace: invalid %s `%s'\n",
350 opt->argument_name, p);
354 qual_flags[n] &= ~opt->bitflag;
356 qual_flags[n] |= opt->bitflag;
367 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
370 switch (tcp->scno + __NR_SYSCALL_BASE) {
381 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
382 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
391 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
392 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
397 enum subcall_style { shift_style, deref_style, mask_style };
399 #if !(defined(LINUX) && defined(ALPHA))
401 const int socket_map [] = {
404 /* SYS_CONNECT */ 98,
405 /* SYS_LISTEN */ 106,
407 /* SYS_GETSOCKNAME */ 150,
408 /* SYS_GETPEERNAME */ 141,
409 /* SYS_SOCKETPAIR */ 135,
412 /* SYS_SENDTO */ 133,
413 /* SYS_RECVFROM */ 125,
414 /* SYS_SHUTDOWN */ 134,
415 /* SYS_SETSOCKOPT */ 105,
416 /* SYS_GETSOCKOPT */ 118,
417 /* SYS_SENDMSG */ 114,
418 /* SYS_RECVMSG */ 113
422 sparc_socket_decode (struct tcb *tcp)
427 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
430 tcp->scno = socket_map [tcp->u_arg [0]-1];
431 n = tcp->u_nargs = sysent [tcp->scno].nargs;
432 addr = tcp->u_arg [1];
433 for (i = 0; i < n; i++){
435 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
437 tcp->u_arg [i] = arg;
438 addr += sizeof (arg);
443 decode_subcall(tcp, subcall, nsubcalls, style)
447 enum subcall_style style;
449 int i, addr, mask, arg;
451 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
455 tcp->scno = subcall + tcp->u_arg[0];
456 if (sysent[tcp->scno].nargs != -1)
457 tcp->u_nargs = sysent[tcp->scno].nargs;
460 for (i = 0; i < tcp->u_nargs; i++)
461 tcp->u_arg[i] = tcp->u_arg[i + 1];
464 tcp->scno = subcall + tcp->u_arg[0];
465 addr = tcp->u_arg[1];
466 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
467 if (umove(tcp, addr, &arg) < 0)
472 tcp->u_nargs = sysent[tcp->scno].nargs;
475 mask = (tcp->u_arg[0] >> 8) & 0xff;
476 tcp->u_arg[0] &= 0xff;
477 for (i = 0; mask; i++)
479 tcp->scno = subcall + i;
480 if (sysent[tcp->scno].nargs != -1)
481 tcp->u_nargs = sysent[tcp->scno].nargs;
487 struct tcb *tcp_last = NULL;
490 internal_syscall(tcp)
494 * We must always trace a few critical system calls in order to
495 * correctly support following forks in the presence of tracing
499 switch (tcp->scno + __NR_SYSCALL_BASE) {
558 #elif defined (POWERPC)
564 #elif defined (ALPHA)
567 #elif defined (SPARC)
577 /* Measure the exit time as early as possible to avoid errors. */
578 if (dtime && (tcp->flags & TCB_INSYSCALL))
579 gettimeofday(&tv, NULL);
581 #if defined (POWERPC)
582 if (upeek(pid, 4*PT_R0, &scno) < 0)
584 if (!(tcp->flags & TCB_INSYSCALL)) {
585 /* Check if we return from execve. */
586 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
587 tcp->flags &= ~TCB_WAITEXECVE;
592 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
597 upeek(pid, 4*15, &pc);
598 umoven(tcp, pc-4, 4, (char *)&scno);
602 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
604 #elif defined (ALPHA)
605 if (upeek(pid, REG_A3, &a3) < 0)
608 if (!(tcp->flags & TCB_INSYSCALL)) {
609 if (upeek(pid, REG_R0, &scno) < 0)
612 /* Check if we return from execve. */
613 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
614 tcp->flags &= ~TCB_WAITEXECVE;
619 * Do some sanity checks to figure out if it's
620 * really a syscall entry
622 if (scno < 0 || scno > nsyscalls) {
623 if (a3 == 0 || a3 == -1) {
625 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
631 if (upeek(pid, REG_R0, &r0) < 0)
634 #elif defined (SPARC)
635 /* Everything we need is in the current register set. */
636 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
639 memmove (®s.u_regs [1], ®s.u_regs [0],
640 sizeof (regs.u_regs) - sizeof (regs.u_regs [0]));
642 /* If we are entering, then disassemble the syscall trap. */
643 if (!(tcp->flags & TCB_INSYSCALL)) {
644 /* Retrieve the syscall trap instruction. */
646 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.pc,0);
650 /* Disassemble the trap to see what personality to use. */
653 /* Linux/SPARC syscall trap. */
657 /* SunOS syscall trap. (pers 1) */
658 fprintf(stderr,"syscall: SunOS no support\n");
661 /* Solaris 2.x syscall trap. (per 2) */
665 /* NetBSD/FreeBSD syscall trap. */
666 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
669 /* Solaris 2.x gettimeofday */
673 /* Unknown syscall trap. */
674 if(tcp->flags & TCB_WAITEXECVE) {
675 tcp->flags &= ~TCB_WAITEXECVE;
678 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.pc);
682 /* Extract the system call number from the registers. */
683 if (trap == 0x91d02027)
686 scno = regs.u_regs[UREG_G1];
688 scno = regs.u_regs[UREG_I0];
689 memmove (®s.u_regs[UREG_I0], ®s.u_regs[UREG_I1], 7*sizeof(regs.u_regs[0]));
695 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
699 #ifdef HAVE_PR_SYSCALL
700 scno = tcp->status.pr_syscall;
701 #else /* !HAVE_PR_SYSCALL */
702 scno = tcp->status.pr_what;
703 #endif /* !HAVE_PR_SYSCALL */
704 if (!(tcp->flags & TCB_INSYSCALL)) {
705 if (tcp->status.pr_why != PR_SYSENTRY) {
710 #endif /* SYS_vfork */
712 /* We are returning in the child, fake it. */
713 tcp->status.pr_why = PR_SYSENTRY;
715 tcp->status.pr_why = PR_SYSEXIT;
718 fprintf(stderr, "syscall: missing entry\n");
719 tcp->flags |= TCB_INSYSCALL;
724 if (tcp->status.pr_why != PR_SYSEXIT) {
725 fprintf(stderr, "syscall: missing exit\n");
726 tcp->flags &= ~TCB_INSYSCALL;
731 if (!(tcp->flags & TCB_INSYSCALL)) {
733 fprintf(stderr, "syscall: missing entry\n");
734 tcp->flags |= TCB_INSYSCALL;
741 * This happens when a signal handler
742 * for a signal which interrupted a
743 * a system call makes another system call.
745 fprintf(stderr, "syscall: missing exit\n");
747 tcp->flags &= ~TCB_INSYSCALL;
753 if (upeek(pid, 4*EAX, &eax) < 0)
755 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
757 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
760 #elif defined (POWERPC)
761 # define SO_MASK 0x10000000
762 if (upeek(pid, 4*PT_CCR, &flags) < 0)
764 if (upeek(pid, 4*PT_R3, &result) < 0)
769 if (upeek(pid, 4*PT_D0, &d0) < 0)
771 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
773 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
777 if (upeek(pid, 4*0, (long *)&r0) < 0)
779 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
781 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
788 if (tcp->flags & TCB_INSYSCALL) {
793 if (eax < 0 && -eax < nerrnos) {
803 if (result && (unsigned) -result < nerrnos) {
808 tcp->u_rval = result;
813 if (d0 && (unsigned) -d0 < nerrnos) {
823 if (r0 && (unsigned) -r0 < nerrnos) {
843 if (regs.psr & PSR_C) {
845 u_error = regs.u_regs[UREG_I0];
848 tcp->u_rval = regs.u_regs[UREG_I0];
859 /* get error code from user struct */
860 if (upeek(pid, uoff(u_error), &u_error) < 0)
862 u_error >>= 24; /* u_error is a char */
864 /* get system call return value */
865 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
870 /* Judicious guessing goes a long way. */
871 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
873 u_error = tcp->status.pr_reg[R_O0];
876 tcp->u_rval = tcp->status.pr_reg[R_O0];
881 /* Wanna know how to kill an hour single-stepping? */
882 if (tcp->status.pr_reg[EFL] & 0x1) {
884 u_error = tcp->status.pr_reg[EAX];
887 tcp->u_rval = tcp->status.pr_reg[EAX];
892 if (tcp->status.pr_reg[CTX_A3]) {
894 u_error = tcp->status.pr_reg[CTX_V0];
897 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
902 tcp->u_error = u_error;
904 internal_syscall(tcp);
905 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
906 tcp->flags &= ~TCB_INSYSCALL;
910 if (tcp->flags & TCB_REPRINT) {
913 if (tcp->scno >= nsyscalls)
914 tprintf("syscall_%lu", tcp->scno);
916 tprintf("%s", sysent[tcp->scno].sys_name);
917 tprintf(" resumed> ");
921 call_count[tcp->scno]++;
923 error_count[tcp->scno]++;
924 tv_sub(&tv, &tv, &tcp->etime);
926 if (tv_cmp(&tv, &tcp->dtime) > 0) {
927 static struct timeval one_tick =
930 if (tv_nz(&tcp->dtime))
932 else if (tv_cmp(&tv, &one_tick) > 0) {
933 if (tv_cmp(&shortest, &one_tick) < 0)
940 if (tv_cmp(&tv, &shortest) < 0)
942 tv_add(&tv_count[tcp->scno],
943 &tv_count[tcp->scno], &tv);
944 tcp->flags &= ~TCB_INSYSCALL;
948 if (tcp->scno >= nsyscalls
949 || (qual_flags[tcp->scno] & QUAL_RAW))
950 sys_res = printargs(tcp);
952 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
953 u_error = tcp->u_error;
956 if (qual_flags[tcp->scno] & QUAL_RAW) {
958 tprintf("= -1 (errno %ld)", u_error);
960 tprintf("= %#lx", tcp->u_rval);
962 else if (!(sys_res & RVAL_NONE) && u_error) {
966 tprintf("= ? ERESTARTSYS (To be restarted)");
969 tprintf("= ? ERESTARTNOINTR (To be restarted)");
972 tprintf("= ? ERESTARTNOHAND (To be restarted)");
977 if (u_error < nerrnos && u_error < sys_nerr)
978 tprintf("%s (%s)", errnoent[u_error],
979 sys_errlist[u_error]);
980 else if (u_error < nerrnos)
981 tprintf("%s (errno %ld)",
982 errnoent[u_error], u_error);
983 else if (u_error < sys_nerr)
984 tprintf("ERRNO_%ld (%s)", u_error,
985 sys_errlist[u_error]);
987 tprintf("E??? (errno %ld)", u_error);
994 if (sys_res & RVAL_NONE)
997 switch (sys_res & RVAL_MASK) {
999 tprintf("= %#lx", tcp->u_rval);
1002 tprintf("= %#lo", tcp->u_rval);
1005 tprintf("= %lu", tcp->u_rval);
1008 tprintf("= %ld", tcp->u_rval);
1012 "invalid rval format\n");
1016 if ((sys_res & RVAL_STR) && tcp->auxstr)
1017 tprintf(" (%s)", tcp->auxstr);
1020 tv_sub(&tv, &tv, &tcp->etime);
1021 tprintf(" <%ld.%06ld>",
1022 (long) tv.tv_sec, (long) tv.tv_usec);
1027 if (fflush(tcp->outf) == EOF)
1029 tcp->flags &= ~TCB_INSYSCALL;
1033 /* Entering system call */
1039 tcp->u_nargs = sysent[tcp->scno].nargs;
1040 for (i = 0; i < tcp->u_nargs; i++) {
1041 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1045 #elif defined (POWERPC)
1048 tcp->u_nargs = sysent[tcp->scno].nargs;
1049 for (i = 0; i < tcp->u_nargs; i++) {
1050 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1054 #elif defined (SPARC)
1059 tcp->u_nargs = sysent[tcp->scno].nargs;
1060 for (i = 0; i < tcp->u_nargs; i++)
1061 tcp->u_arg[i] = regs.u_regs[offset + i];
1066 tcp->u_nargs = sysent[tcp->scno].nargs;
1067 for (i = 0; i < tcp->u_nargs; i++) {
1068 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1077 tcp->u_nargs = sysent[tcp->scno].nargs;
1078 for (i = 0; i < tcp->u_nargs; i++) {
1081 if (upeek(pid, uoff(u_arg[0]) +
1082 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1090 * SGI is broken: even though it has pr_sysarg, it doesn't
1091 * set them on system call entry. Get a clue.
1093 if (sysent[tcp->scno].nargs != -1)
1094 tcp->u_nargs = sysent[tcp->scno].nargs;
1096 tcp->u_nargs = tcp->status.pr_nsysarg;
1097 if (tcp->u_nargs > 4) {
1098 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1099 4*sizeof(tcp->u_arg[0]));
1100 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1101 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1104 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1105 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1108 #ifdef HAVE_PR_SYSCALL
1109 if (sysent[tcp->scno].nargs != -1)
1110 tcp->u_nargs = sysent[tcp->scno].nargs;
1112 tcp->u_nargs = tcp->status.pr_nsysarg;
1115 for (i = 0; i < tcp->u_nargs; i++)
1116 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1118 #else /* !HAVE_PR_SYSCALL */
1120 if (sysent[tcp->scno].nargs != -1)
1121 tcp->u_nargs = sysent[tcp->scno].nargs;
1124 umoven(tcp, tcp->status.pr_reg[UESP] + 4,
1125 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1127 #endif /* !HAVE_PR_SYSCALL */
1131 switch (tcp->scno + __NR_SYSCALL_BASE) {
1133 switch (tcp->scno) {
1136 #if !defined (ALPHA) && !defined(SPARC)
1137 case SYS_socketcall:
1138 decode_subcall(tcp, SYS_socket_subcall,
1139 SYS_socket_nsubcalls, deref_style);
1142 decode_subcall(tcp, SYS_ipc_subcall,
1143 SYS_ipc_nsubcalls, shift_style);
1145 #endif /* !ALPHA && !SPARC */
1147 case SYS_socketcall:
1148 sparc_socket_decode (tcp);
1153 #ifdef SYS_pgrpsys_subcall
1155 decode_subcall(tcp, SYS_pgrpsys_subcall,
1156 SYS_pgrpsys_nsubcalls, shift_style);
1158 #endif /* SYS_pgrpsys_subcall */
1159 #ifdef SYS_sigcall_subcall
1161 decode_subcall(tcp, SYS_sigcall_subcall,
1162 SYS_sigcall_nsubcalls, mask_style);
1164 #endif /* SYS_sigcall_subcall */
1166 decode_subcall(tcp, SYS_msgsys_subcall,
1167 SYS_msgsys_nsubcalls, shift_style);
1170 decode_subcall(tcp, SYS_shmsys_subcall,
1171 SYS_shmsys_nsubcalls, shift_style);
1174 decode_subcall(tcp, SYS_semsys_subcall,
1175 SYS_semsys_nsubcalls, shift_style);
1179 decode_subcall(tcp, SYS_utssys_subcall,
1180 SYS_utssys_nsubcalls, shift_style);
1184 decode_subcall(tcp, SYS_sysfs_subcall,
1185 SYS_sysfs_nsubcalls, shift_style);
1188 decode_subcall(tcp, SYS_spcall_subcall,
1189 SYS_spcall_nsubcalls, shift_style);
1191 #ifdef SYS_context_subcall
1193 decode_subcall(tcp, SYS_context_subcall,
1194 SYS_context_nsubcalls, shift_style);
1196 #endif /* SYS_context_subcall */
1200 decode_subcall(tcp, SYS_semsys_subcall,
1201 SYS_semsys_nsubcalls, shift_style);
1204 decode_subcall(tcp, SYS_msgsys_subcall,
1205 SYS_msgsys_nsubcalls, shift_style);
1208 decode_subcall(tcp, SYS_shmsys_subcall,
1209 SYS_shmsys_nsubcalls, shift_style);
1214 internal_syscall(tcp);
1215 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1216 tcp->flags |= TCB_INSYSCALL;
1221 gettimeofday(&tcp->etime, NULL);
1222 tcp->flags |= TCB_INSYSCALL;
1227 tcp->flags &= ~TCB_REPRINT;
1229 if (tcp->scno >= nsyscalls)
1230 tprintf("syscall_%lu(", tcp->scno);
1232 tprintf("%s(", sysent[tcp->scno].sys_name);
1233 if (tcp->scno >= nsyscalls ||
1234 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1235 sys_res = printargs(tcp);
1237 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1238 if (fflush(tcp->outf) == EOF)
1240 tcp->flags |= TCB_INSYSCALL;
1241 /* Measure the entrance time as late as possible to avoid errors. */
1243 gettimeofday(&tcp->etime, NULL);
1251 if (entering(tcp)) {
1254 for (i = 0; i < tcp->u_nargs; i++)
1255 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1268 struct pt_regs regs;
1269 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
1271 val = regs.u_regs[UREG_I1];
1276 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1282 val = tcp->status.pr_reg[R_O1];
1285 val = tcp->status.pr_reg[EDX];
1288 val = tcp->status.pr_reg[CTX_V1];
1296 * Apparently, indirect system calls have already be converted by ptrace(2),
1297 * so if you see "indir" this program has gone astray.
1305 if (entering(tcp)) {
1306 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1307 fprintf(stderr, "Bogus syscall: %u\n", scno);
1310 nargs = sysent[scno].nargs;
1311 tprintf("%s", sysent[scno].sys_name);
1312 for (i = 0; i < nargs; i++)
1313 tprintf(", %#lx", tcp->u_arg[i+1]);
1323 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1331 return strcmp(sysent[*((int *) a)].sys_name,
1332 sysent[*((int *) b)].sys_name);
1340 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1342 return (m < n) ? 1 : (m > n) ? -1 : 0;
1345 static int (*sortfun)();
1346 static struct timeval overhead = { -1, -1 };
1352 if (strcmp(sortby, "time") == 0)
1354 else if (strcmp(sortby, "calls") == 0)
1355 sortfun = count_cmp;
1356 else if (strcmp(sortby, "name") == 0)
1357 sortfun = syscall_cmp;
1358 else if (strcmp(sortby, "nothing") == 0)
1361 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1366 void set_overhead(n)
1369 overhead.tv_sec = n / 1000000;
1370 overhead.tv_usec = n % 1000000;
1378 int call_cum, error_cum;
1379 struct timeval tv_cum, dtv;
1381 char *dashes = "-------------------------";
1384 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1385 if (overhead.tv_sec == -1) {
1386 tv_mul(&overhead, &shortest, 8);
1387 tv_div(&overhead, &overhead, 10);
1389 for (i = 0; i < nsyscalls; i++) {
1390 sorted_count[i] = i;
1391 if (call_count[i] == 0)
1393 tv_mul(&dtv, &overhead, call_count[i]);
1394 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1395 call_cum += call_count[i];
1396 error_cum += error_count[i];
1397 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1400 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1401 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1402 "% time", "seconds", "usecs/call",
1403 "calls", "errors", "syscall");
1404 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1405 dashes, dashes, dashes, dashes, dashes, dashes);
1406 for (i = 0; i < nsyscalls; i++) {
1407 j = sorted_count[i];
1408 if (call_count[j] == 0)
1410 tv_div(&dtv, &tv_count[j], call_count[j]);
1412 sprintf(error_str, "%d", error_count[j]);
1414 error_str[0] = '\0';
1415 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1416 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1417 percent, (long) tv_count[j].tv_sec,
1418 (long) tv_count[j].tv_usec,
1419 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1420 call_count[j], error_str, sysent[j].sys_name);
1422 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1423 dashes, dashes, dashes, dashes, dashes, dashes);
1425 sprintf(error_str, "%d", error_cum);
1427 error_str[0] = '\0';
1428 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1429 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1430 call_cum, error_str, "total");