2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/syscall.h>
43 #include <sys/param.h>
49 #if HAVE_LINUX_PTRACE_H
51 #include <linux/ptrace.h>
56 #ifndef PTRACE_PEEKUSR
57 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
61 #ifndef SYS_ERRLIST_DECLARED
63 extern char *sys_errlist[];
64 #endif /* SYS_ERRLIST_DECLARED */
68 #define ERESTARTSYS 512
70 #ifndef ERESTARTNOINTR
71 #define ERESTARTNOINTR 513
73 #ifndef ERESTARTNOHAND
74 #define ERESTARTNOHAND 514 /* restart if no handler.. */
77 #define ENOIOCTLCMD 515 /* No ioctl command */
90 /* Define these shorthand notations to simplify the syscallent files. */
93 #define TN TRACE_NETWORK
94 #define TP TRACE_PROCESS
95 #define TS TRACE_SIGNAL
97 struct sysent sysent0[] = {
98 #include "syscallent.h"
100 int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
102 #if SUPPORTED_PERSONALITIES >= 2
103 struct sysent sysent1[] = {
104 #include "syscallent1.h"
106 int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
107 #endif /* SUPPORTED_PERSONALITIES >= 2 */
109 #if SUPPORTED_PERSONALITIES >= 3
110 struct sysent sysent2[] = {
111 #include "syscallent2.h"
113 int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
114 #endif /* SUPPORTED_PERSONALITIES >= 3 */
116 struct sysent *sysent;
119 /* Now undef them since short defines cause wicked namespace pollution. */
126 char *errnoent0[] = {
127 #include "errnoent.h"
129 int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
131 #if SUPPORTED_PERSONALITIES >= 2
132 char *errnoent1[] = {
133 #include "errnoent1.h"
135 int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
136 #endif /* SUPPORTED_PERSONALITIES >= 2 */
138 #if SUPPORTED_PERSONALITIES >= 3
139 char *errnoent2[] = {
140 #include "errnoent2.h"
142 int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
143 #endif /* SUPPORTED_PERSONALITIES >= 3 */
148 int current_personality;
151 set_personality(personality)
154 switch (personality) {
156 errnoent = errnoent0;
159 nsyscalls = nsyscalls0;
160 ioctlent = ioctlent0;
161 nioctlents = nioctlents0;
162 signalent = signalent0;
163 nsignals = nsignals0;
166 #if SUPPORTED_PERSONALITIES >= 2
168 errnoent = errnoent1;
171 nsyscalls = nsyscalls1;
172 ioctlent = ioctlent1;
173 nioctlents = nioctlents1;
174 signalent = signalent1;
175 nsignals = nsignals1;
177 #endif /* SUPPORTED_PERSONALITIES >= 2 */
179 #if SUPPORTED_PERSONALITIES >= 3
181 errnoent = errnoent2;
184 nsyscalls = nsyscalls2;
185 ioctlent = ioctlent2;
186 nioctlents = nioctlents2;
187 signalent = signalent2;
188 nsignals = nsignals2;
190 #endif /* SUPPORTED_PERSONALITIES >= 3 */
196 current_personality = personality;
200 int qual_flags[MAX_QUALS];
202 static int call_count[MAX_QUALS];
203 static int error_count[MAX_QUALS];
204 static struct timeval tv_count[MAX_QUALS];
205 static int sorted_count[MAX_QUALS];
207 static struct timeval shortest = { 1000000, 0 };
209 static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
211 static struct qual_options {
217 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
218 { QUAL_TRACE, "t", lookup_syscall, "system call" },
219 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
220 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
221 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
222 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
223 { QUAL_RAW, "raw", lookup_syscall, "system call" },
224 { QUAL_RAW, "x", lookup_syscall, "system call" },
225 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
226 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
227 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
228 { QUAL_FAULT, "fault", lookup_fault, "fault" },
229 { QUAL_FAULT, "faults", lookup_fault, "fault" },
230 { QUAL_FAULT, "m", lookup_fault, "fault" },
231 { QUAL_READ, "read", lookup_desc, "descriptor" },
232 { QUAL_READ, "reads", lookup_desc, "descriptor" },
233 { QUAL_READ, "r", lookup_desc, "descriptor" },
234 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
235 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
236 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
237 { 0, NULL, NULL, NULL },
246 for (i = 0; i < nsyscalls; i++) {
247 if (strcmp(s, sysent[i].sys_name) == 0)
260 if (s && *s && isdigit(*s))
264 for (i = 0; s[i]; i++)
265 s[i] = toupper(s[i]);
266 if (strncmp(s, "SIG", 3) == 0)
268 for (i = 0; i <= NSIG; i++) {
269 if (strcmp(s, signame(i) + 3) == 0)
286 if (s && *s && isdigit(*s))
295 if (strcmp(s, "file") == 0)
297 if (strcmp(s, "ipc") == 0)
299 if (strcmp(s, "network") == 0)
300 return TRACE_NETWORK;
301 if (strcmp(s, "process") == 0)
302 return TRACE_PROCESS;
303 if (strcmp(s, "signal") == 0)
312 struct qual_options *opt;
317 opt = &qual_options[0];
318 for (i = 0; (p = qual_options[i].option_name); i++) {
320 if (strncmp(s, p, n) == 0 && s[n] == '=') {
321 opt = &qual_options[i];
331 if (strcmp(s, "none") == 0) {
335 if (strcmp(s, "all") == 0) {
336 for (i = 0; i < MAX_QUALS; i++) {
338 qual_flags[i] &= ~opt->bitflag;
340 qual_flags[i] |= opt->bitflag;
344 for (i = 0; i < MAX_QUALS; i++) {
346 qual_flags[i] |= opt->bitflag;
348 qual_flags[i] &= ~opt->bitflag;
350 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
351 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
352 for (i = 0; i < MAX_QUALS; i++) {
353 if (sysent[i].sys_flags & n) {
355 qual_flags[i] &= ~opt->bitflag;
357 qual_flags[i] |= opt->bitflag;
362 if ((n = (*opt->lookup)(p)) < 0) {
363 fprintf(stderr, "strace: invalid %s `%s'\n",
364 opt->argument_name, p);
368 qual_flags[n] &= ~opt->bitflag;
370 qual_flags[n] |= opt->bitflag;
381 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
384 switch (tcp->scno + __NR_SYSCALL_BASE) {
395 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
396 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
405 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
406 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
411 enum subcall_style { shift_style, deref_style, mask_style, door_style };
413 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
415 const int socket_map [] = {
418 /* SYS_CONNECT */ 98,
419 /* SYS_LISTEN */ 106,
421 /* SYS_GETSOCKNAME */ 150,
422 /* SYS_GETPEERNAME */ 141,
423 /* SYS_SOCKETPAIR */ 135,
426 /* SYS_SENDTO */ 133,
427 /* SYS_RECVFROM */ 125,
428 /* SYS_SHUTDOWN */ 134,
429 /* SYS_SETSOCKOPT */ 105,
430 /* SYS_GETSOCKOPT */ 118,
431 /* SYS_SENDMSG */ 114,
432 /* SYS_RECVMSG */ 113
436 sparc_socket_decode (tcp)
442 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
445 tcp->scno = socket_map [tcp->u_arg [0]-1];
446 n = tcp->u_nargs = sysent [tcp->scno].nargs;
447 addr = tcp->u_arg [1];
448 for (i = 0; i < n; i++){
450 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
452 tcp->u_arg [i] = arg;
453 addr += sizeof (arg);
458 decode_subcall(tcp, subcall, nsubcalls, style)
462 enum subcall_style style;
464 int i, addr, mask, arg;
466 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
470 tcp->scno = subcall + tcp->u_arg[0];
471 if (sysent[tcp->scno].nargs != -1)
472 tcp->u_nargs = sysent[tcp->scno].nargs;
475 for (i = 0; i < tcp->u_nargs; i++)
476 tcp->u_arg[i] = tcp->u_arg[i + 1];
479 tcp->scno = subcall + tcp->u_arg[0];
480 addr = tcp->u_arg[1];
481 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
482 if (umove(tcp, addr, &arg) < 0)
487 tcp->u_nargs = sysent[tcp->scno].nargs;
490 mask = (tcp->u_arg[0] >> 8) & 0xff;
491 tcp->u_arg[0] &= 0xff;
492 for (i = 0; mask; i++)
494 tcp->scno = subcall + i;
495 if (sysent[tcp->scno].nargs != -1)
496 tcp->u_nargs = sysent[tcp->scno].nargs;
500 * Oh, yuck. The call code is the *sixth* argument.
502 tcp->scno = subcall + tcp->u_arg[5];
503 if (sysent[tcp->scno].nargs != -1)
504 tcp->u_nargs = sysent[tcp->scno].nargs;
512 struct tcb *tcp_last = NULL;
515 internal_syscall(tcp)
519 * We must always trace a few critical system calls in order to
520 * correctly support following forks in the presence of tracing
524 switch (tcp->scno + __NR_SYSCALL_BASE) {
583 #elif defined (POWERPC)
589 #elif defined (ALPHA)
594 #elif defined (SPARC)
607 /* Measure the exit time as early as possible to avoid errors. */
608 if (dtime && (tcp->flags & TCB_INSYSCALL))
609 gettimeofday(&tv, NULL);
612 if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
614 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
618 #elif defined (POWERPC)
619 if (upeek(pid, 4*PT_R0, &scno) < 0)
621 if (!(tcp->flags & TCB_INSYSCALL)) {
622 /* Check if we return from execve. */
623 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
624 tcp->flags &= ~TCB_WAITEXECVE;
629 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
634 upeek(pid, 4*15, &pc);
635 umoven(tcp, pc-4, 4, (char *)&scno);
639 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
642 if (upeek(pid, REG_A3, &a3) < 0)
645 if(!(tcp->flags & TCB_INSYSCALL)) {
646 if (upeek(pid, REG_V0, &scno) < 0)
649 if (scno < 0 || scno > nsyscalls) {
650 if(a3 == 0 || a3 == -1) {
652 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
657 if (upeek(pid, REG_V0, &r2) < 0)
660 #elif defined (ALPHA)
661 if (upeek(pid, REG_A3, &a3) < 0)
664 if (!(tcp->flags & TCB_INSYSCALL)) {
665 if (upeek(pid, REG_R0, &scno) < 0)
668 /* Check if we return from execve. */
669 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
670 tcp->flags &= ~TCB_WAITEXECVE;
675 * Do some sanity checks to figure out if it's
676 * really a syscall entry
678 if (scno < 0 || scno > nsyscalls) {
679 if (a3 == 0 || a3 == -1) {
681 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
687 if (upeek(pid, REG_R0, &r0) < 0)
690 #elif defined (SPARC)
691 /* Everything we need is in the current register set. */
692 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
695 /* If we are entering, then disassemble the syscall trap. */
696 if (!(tcp->flags & TCB_INSYSCALL)) {
697 /* Retrieve the syscall trap instruction. */
699 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
703 /* Disassemble the trap to see what personality to use. */
706 /* Linux/SPARC syscall trap. */
710 /* Linux/SPARC64 syscall trap. */
711 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
714 /* SunOS syscall trap. (pers 1) */
715 fprintf(stderr,"syscall: SunOS no support\n");
718 /* Solaris 2.x syscall trap. (per 2) */
722 /* NetBSD/FreeBSD syscall trap. */
723 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
726 /* Solaris 2.x gettimeofday */
730 /* Unknown syscall trap. */
731 if(tcp->flags & TCB_WAITEXECVE) {
732 tcp->flags &= ~TCB_WAITEXECVE;
735 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
739 /* Extract the system call number from the registers. */
740 if (trap == 0x91d02027)
746 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
752 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
756 #ifdef HAVE_PR_SYSCALL
757 scno = tcp->status.pr_syscall;
758 #else /* !HAVE_PR_SYSCALL */
759 scno = tcp->status.PR_WHAT;
760 #endif /* !HAVE_PR_SYSCALL */
761 if (!(tcp->flags & TCB_INSYSCALL)) {
762 if (tcp->status.PR_WHY != PR_SYSENTRY) {
767 #endif /* SYS_vfork */
769 /* We are returning in the child, fake it. */
770 tcp->status.PR_WHY = PR_SYSENTRY;
772 tcp->status.PR_WHY = PR_SYSEXIT;
775 fprintf(stderr, "syscall: missing entry\n");
776 tcp->flags |= TCB_INSYSCALL;
781 if (tcp->status.PR_WHY != PR_SYSEXIT) {
782 fprintf(stderr, "syscall: missing exit\n");
783 tcp->flags &= ~TCB_INSYSCALL;
788 if (!(tcp->flags & TCB_INSYSCALL)) {
790 fprintf(stderr, "syscall: missing entry\n");
791 tcp->flags |= TCB_INSYSCALL;
798 * This happens when a signal handler
799 * for a signal which interrupted a
800 * a system call makes another system call.
802 fprintf(stderr, "syscall: missing exit\n");
804 tcp->flags &= ~TCB_INSYSCALL;
810 if (upeek(pid, 4*EAX, &eax) < 0)
812 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
814 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
817 #elif defined (POWERPC)
818 # define SO_MASK 0x10000000
819 if (upeek(pid, 4*PT_CCR, &flags) < 0)
821 if (upeek(pid, 4*PT_R3, &result) < 0)
826 if (upeek(pid, 4*PT_D0, &d0) < 0)
828 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
830 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
834 if (upeek(pid, 4*0, (long *)&r0) < 0)
836 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
838 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
845 if (tcp->flags & TCB_INSYSCALL) {
850 if (eax < 0 && -eax < nerrnos) {
869 if (result && (unsigned) -result < nerrnos) {
874 tcp->u_rval = result;
879 if (d0 && (unsigned) -d0 < nerrnos) {
889 if (r0 && (unsigned) -r0 < nerrnos) {
909 if (regs.r_psr & PSR_C) {
914 tcp->u_rval = regs.r_o0;
926 /* get error code from user struct */
927 if (upeek(pid, uoff(u_error), &u_error) < 0)
929 u_error >>= 24; /* u_error is a char */
931 /* get system call return value */
932 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
937 /* Judicious guessing goes a long way. */
938 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
940 u_error = tcp->status.pr_reg[R_O0];
943 tcp->u_rval = tcp->status.pr_reg[R_O0];
948 /* Wanna know how to kill an hour single-stepping? */
949 if (tcp->status.PR_REG[EFL] & 0x1) {
951 u_error = tcp->status.PR_REG[EAX];
954 tcp->u_rval = tcp->status.PR_REG[EAX];
959 if (tcp->status.pr_reg[CTX_A3]) {
961 u_error = tcp->status.pr_reg[CTX_V0];
964 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
969 tcp->u_error = u_error;
971 internal_syscall(tcp);
972 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
973 tcp->flags &= ~TCB_INSYSCALL;
977 if (tcp->flags & TCB_REPRINT) {
980 if (tcp->scno >= nsyscalls)
981 tprintf("syscall_%lu", tcp->scno);
983 tprintf("%s", sysent[tcp->scno].sys_name);
984 tprintf(" resumed> ");
988 call_count[tcp->scno]++;
990 error_count[tcp->scno]++;
991 tv_sub(&tv, &tv, &tcp->etime);
993 if (tv_cmp(&tv, &tcp->dtime) > 0) {
994 static struct timeval one_tick =
997 if (tv_nz(&tcp->dtime))
999 else if (tv_cmp(&tv, &one_tick) > 0) {
1000 if (tv_cmp(&shortest, &one_tick) < 0)
1007 if (tv_cmp(&tv, &shortest) < 0)
1009 tv_add(&tv_count[tcp->scno],
1010 &tv_count[tcp->scno], &tv);
1011 tcp->flags &= ~TCB_INSYSCALL;
1015 if (tcp->scno >= nsyscalls
1016 || (qual_flags[tcp->scno] & QUAL_RAW))
1017 sys_res = printargs(tcp);
1019 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1020 u_error = tcp->u_error;
1023 if (qual_flags[tcp->scno] & QUAL_RAW) {
1025 tprintf("= -1 (errno %ld)", u_error);
1027 tprintf("= %#lx", tcp->u_rval);
1029 else if (!(sys_res & RVAL_NONE) && u_error) {
1033 tprintf("= ? ERESTARTSYS (To be restarted)");
1035 case ERESTARTNOINTR:
1036 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1038 case ERESTARTNOHAND:
1039 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1044 if (u_error < nerrnos && u_error < sys_nerr)
1045 tprintf("%s (%s)", errnoent[u_error],
1046 sys_errlist[u_error]);
1047 else if (u_error < nerrnos)
1048 tprintf("%s (errno %ld)",
1049 errnoent[u_error], u_error);
1050 else if (u_error < sys_nerr)
1051 tprintf("ERRNO_%ld (%s)", u_error,
1052 sys_errlist[u_error]);
1054 tprintf("E??? (errno %ld)", u_error);
1061 if (sys_res & RVAL_NONE)
1064 switch (sys_res & RVAL_MASK) {
1066 tprintf("= %#lx", tcp->u_rval);
1069 tprintf("= %#lo", tcp->u_rval);
1072 tprintf("= %lu", tcp->u_rval);
1075 tprintf("= %ld", tcp->u_rval);
1079 "invalid rval format\n");
1083 if ((sys_res & RVAL_STR) && tcp->auxstr)
1084 tprintf(" (%s)", tcp->auxstr);
1087 tv_sub(&tv, &tv, &tcp->etime);
1088 tprintf(" <%ld.%06ld>",
1089 (long) tv.tv_sec, (long) tv.tv_usec);
1094 if (fflush(tcp->outf) == EOF)
1096 tcp->flags &= ~TCB_INSYSCALL;
1100 /* Entering system call */
1106 tcp->u_nargs = sysent[tcp->scno].nargs;
1107 for (i = 0; i < tcp->u_nargs; i++) {
1108 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
1112 #elif defined (ALPHA)
1115 tcp->u_nargs = sysent[tcp->scno].nargs;
1116 for (i = 0; i < tcp->u_nargs; i++) {
1117 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1118 * for scno somewhere above here!
1120 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1124 #elif defined (MIPS)
1129 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1131 if(upeek(pid, REG_SP, &sp) < 0)
1133 for(i = 0; i < 4; i++) {
1134 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1137 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1138 (char *)(tcp->u_arg + 4));
1140 for(i = 0; i < nargs; i++) {
1141 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1146 #elif defined (POWERPC)
1149 tcp->u_nargs = sysent[tcp->scno].nargs;
1150 for (i = 0; i < tcp->u_nargs; i++) {
1151 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1155 #elif defined (SPARC)
1159 tcp->u_nargs = sysent[tcp->scno].nargs;
1160 for (i = 0; i < tcp->u_nargs; i++)
1161 tcp->u_arg[i] = *((®s.r_o0) + i);
1166 tcp->u_nargs = sysent[tcp->scno].nargs;
1167 for (i = 0; i < tcp->u_nargs; i++) {
1168 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1177 tcp->u_nargs = sysent[tcp->scno].nargs;
1178 for (i = 0; i < tcp->u_nargs; i++) {
1181 if (upeek(pid, uoff(u_arg[0]) +
1182 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1190 * SGI is broken: even though it has pr_sysarg, it doesn't
1191 * set them on system call entry. Get a clue.
1193 if (sysent[tcp->scno].nargs != -1)
1194 tcp->u_nargs = sysent[tcp->scno].nargs;
1196 tcp->u_nargs = tcp->status.pr_nsysarg;
1197 if (tcp->u_nargs > 4) {
1198 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1199 4*sizeof(tcp->u_arg[0]));
1200 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1201 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1204 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1205 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1208 #ifdef HAVE_PR_SYSCALL
1209 if (sysent[tcp->scno].nargs != -1)
1210 tcp->u_nargs = sysent[tcp->scno].nargs;
1212 tcp->u_nargs = tcp->status.pr_nsysarg;
1215 for (i = 0; i < tcp->u_nargs; i++)
1216 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1218 #else /* !HAVE_PR_SYSCALL */
1220 if (sysent[tcp->scno].nargs != -1)
1221 tcp->u_nargs = sysent[tcp->scno].nargs;
1224 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1228 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1229 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1231 #endif /* !HAVE_PR_SYSCALL */
1235 switch (tcp->scno + __NR_SYSCALL_BASE) {
1237 switch (tcp->scno) {
1240 #if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS)
1241 case SYS_socketcall:
1242 decode_subcall(tcp, SYS_socket_subcall,
1243 SYS_socket_nsubcalls, deref_style);
1246 decode_subcall(tcp, SYS_ipc_subcall,
1247 SYS_ipc_nsubcalls, shift_style);
1249 #endif /* !ALPHA && !SPARC */
1251 case SYS_socketcall:
1252 sparc_socket_decode (tcp);
1257 #ifdef SYS_pgrpsys_subcall
1259 decode_subcall(tcp, SYS_pgrpsys_subcall,
1260 SYS_pgrpsys_nsubcalls, shift_style);
1262 #endif /* SYS_pgrpsys_subcall */
1263 #ifdef SYS_sigcall_subcall
1265 decode_subcall(tcp, SYS_sigcall_subcall,
1266 SYS_sigcall_nsubcalls, mask_style);
1268 #endif /* SYS_sigcall_subcall */
1270 decode_subcall(tcp, SYS_msgsys_subcall,
1271 SYS_msgsys_nsubcalls, shift_style);
1274 decode_subcall(tcp, SYS_shmsys_subcall,
1275 SYS_shmsys_nsubcalls, shift_style);
1278 decode_subcall(tcp, SYS_semsys_subcall,
1279 SYS_semsys_nsubcalls, shift_style);
1283 decode_subcall(tcp, SYS_utssys_subcall,
1284 SYS_utssys_nsubcalls, shift_style);
1288 decode_subcall(tcp, SYS_sysfs_subcall,
1289 SYS_sysfs_nsubcalls, shift_style);
1292 decode_subcall(tcp, SYS_spcall_subcall,
1293 SYS_spcall_nsubcalls, shift_style);
1295 #ifdef SYS_context_subcall
1297 decode_subcall(tcp, SYS_context_subcall,
1298 SYS_context_nsubcalls, shift_style);
1300 #endif /* SYS_context_subcall */
1301 #ifdef SYS_door_subcall
1303 decode_subcall(tcp, SYS_door_subcall,
1304 SYS_door_nsubcalls, door_style);
1306 #endif /* SYS_door_subcall */
1310 decode_subcall(tcp, SYS_semsys_subcall,
1311 SYS_semsys_nsubcalls, shift_style);
1314 decode_subcall(tcp, SYS_msgsys_subcall,
1315 SYS_msgsys_nsubcalls, shift_style);
1318 decode_subcall(tcp, SYS_shmsys_subcall,
1319 SYS_shmsys_nsubcalls, shift_style);
1324 internal_syscall(tcp);
1325 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1326 tcp->flags |= TCB_INSYSCALL;
1331 gettimeofday(&tcp->etime, NULL);
1332 tcp->flags |= TCB_INSYSCALL;
1337 tcp->flags &= ~TCB_REPRINT;
1339 if (tcp->scno >= nsyscalls)
1340 tprintf("syscall_%lu(", tcp->scno);
1342 tprintf("%s(", sysent[tcp->scno].sys_name);
1343 if (tcp->scno >= nsyscalls ||
1344 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1345 sys_res = printargs(tcp);
1347 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1348 if (fflush(tcp->outf) == EOF)
1350 tcp->flags |= TCB_INSYSCALL;
1351 /* Measure the entrance time as late as possible to avoid errors. */
1353 gettimeofday(&tcp->etime, NULL);
1361 if (entering(tcp)) {
1364 for (i = 0; i < tcp->u_nargs; i++)
1365 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1379 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
1386 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1392 val = tcp->status.PR_REG[R_O1];
1395 val = tcp->status.PR_REG[EDX];
1398 val = tcp->status.PR_REG[CTX_V1];
1406 * Apparently, indirect system calls have already be converted by ptrace(2),
1407 * so if you see "indir" this program has gone astray.
1415 if (entering(tcp)) {
1416 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1417 fprintf(stderr, "Bogus syscall: %u\n", scno);
1420 nargs = sysent[scno].nargs;
1421 tprintf("%s", sysent[scno].sys_name);
1422 for (i = 0; i < nargs; i++)
1423 tprintf(", %#lx", tcp->u_arg[i+1]);
1433 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1441 return strcmp(sysent[*((int *) a)].sys_name,
1442 sysent[*((int *) b)].sys_name);
1450 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1452 return (m < n) ? 1 : (m > n) ? -1 : 0;
1455 static int (*sortfun)();
1456 static struct timeval overhead = { -1, -1 };
1462 if (strcmp(sortby, "time") == 0)
1464 else if (strcmp(sortby, "calls") == 0)
1465 sortfun = count_cmp;
1466 else if (strcmp(sortby, "name") == 0)
1467 sortfun = syscall_cmp;
1468 else if (strcmp(sortby, "nothing") == 0)
1471 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1476 void set_overhead(n)
1479 overhead.tv_sec = n / 1000000;
1480 overhead.tv_usec = n % 1000000;
1488 int call_cum, error_cum;
1489 struct timeval tv_cum, dtv;
1491 char *dashes = "-------------------------";
1494 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1495 if (overhead.tv_sec == -1) {
1496 tv_mul(&overhead, &shortest, 8);
1497 tv_div(&overhead, &overhead, 10);
1499 for (i = 0; i < nsyscalls; i++) {
1500 sorted_count[i] = i;
1501 if (call_count[i] == 0)
1503 tv_mul(&dtv, &overhead, call_count[i]);
1504 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1505 call_cum += call_count[i];
1506 error_cum += error_count[i];
1507 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1510 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1511 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1512 "% time", "seconds", "usecs/call",
1513 "calls", "errors", "syscall");
1514 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1515 dashes, dashes, dashes, dashes, dashes, dashes);
1516 for (i = 0; i < nsyscalls; i++) {
1517 j = sorted_count[i];
1518 if (call_count[j] == 0)
1520 tv_div(&dtv, &tv_count[j], call_count[j]);
1522 sprintf(error_str, "%d", error_count[j]);
1524 error_str[0] = '\0';
1525 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1526 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1527 percent, (long) tv_count[j].tv_sec,
1528 (long) tv_count[j].tv_usec,
1529 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1530 call_count[j], error_str, sysent[j].sys_name);
1532 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1533 dashes, dashes, dashes, dashes, dashes, dashes);
1535 sprintf(error_str, "%d", error_cum);
1537 error_str[0] = '\0';
1538 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1539 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1540 call_cum, error_str, "total");