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) {
584 #elif defined (POWERPC)
590 #elif defined (ALPHA)
595 #elif defined (SPARC)
608 /* Measure the exit time as early as possible to avoid errors. */
609 if (dtime && (tcp->flags & TCB_INSYSCALL))
610 gettimeofday(&tv, NULL);
613 if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
615 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
619 #elif defined (POWERPC)
620 if (upeek(pid, 4*PT_R0, &scno) < 0)
622 if (!(tcp->flags & TCB_INSYSCALL)) {
623 /* Check if we return from execve. */
624 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
625 tcp->flags &= ~TCB_WAITEXECVE;
630 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
635 upeek(pid, 4*15, &pc);
636 umoven(tcp, pc-4, 4, (char *)&scno);
640 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
643 if (upeek(pid, REG_A3, &a3) < 0)
646 if(!(tcp->flags & TCB_INSYSCALL)) {
647 if (upeek(pid, REG_V0, &scno) < 0)
650 if (scno < 0 || scno > nsyscalls) {
651 if(a3 == 0 || a3 == -1) {
653 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
658 if (upeek(pid, REG_V0, &r2) < 0)
661 #elif defined (ALPHA)
662 if (upeek(pid, REG_A3, &a3) < 0)
665 if (!(tcp->flags & TCB_INSYSCALL)) {
666 if (upeek(pid, REG_R0, &scno) < 0)
669 /* Check if we return from execve. */
670 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
671 tcp->flags &= ~TCB_WAITEXECVE;
676 * Do some sanity checks to figure out if it's
677 * really a syscall entry
679 if (scno < 0 || scno > nsyscalls) {
680 if (a3 == 0 || a3 == -1) {
682 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
688 if (upeek(pid, REG_R0, &r0) < 0)
691 #elif defined (SPARC)
692 /* Everything we need is in the current register set. */
693 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
696 /* If we are entering, then disassemble the syscall trap. */
697 if (!(tcp->flags & TCB_INSYSCALL)) {
698 /* Retrieve the syscall trap instruction. */
700 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
704 /* Disassemble the trap to see what personality to use. */
707 /* Linux/SPARC syscall trap. */
711 /* Linux/SPARC64 syscall trap. */
712 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
715 /* SunOS syscall trap. (pers 1) */
716 fprintf(stderr,"syscall: SunOS no support\n");
719 /* Solaris 2.x syscall trap. (per 2) */
723 /* NetBSD/FreeBSD syscall trap. */
724 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
727 /* Solaris 2.x gettimeofday */
731 /* Unknown syscall trap. */
732 if(tcp->flags & TCB_WAITEXECVE) {
733 tcp->flags &= ~TCB_WAITEXECVE;
736 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
740 /* Extract the system call number from the registers. */
741 if (trap == 0x91d02027)
747 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
753 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
757 #ifdef HAVE_PR_SYSCALL
758 scno = tcp->status.pr_syscall;
759 #else /* !HAVE_PR_SYSCALL */
760 scno = tcp->status.PR_WHAT;
761 #endif /* !HAVE_PR_SYSCALL */
762 if (!(tcp->flags & TCB_INSYSCALL)) {
763 if (tcp->status.PR_WHY != PR_SYSENTRY) {
768 #endif /* SYS_vfork */
770 /* We are returning in the child, fake it. */
771 tcp->status.PR_WHY = PR_SYSENTRY;
773 tcp->status.PR_WHY = PR_SYSEXIT;
776 fprintf(stderr, "syscall: missing entry\n");
777 tcp->flags |= TCB_INSYSCALL;
782 if (tcp->status.PR_WHY != PR_SYSEXIT) {
783 fprintf(stderr, "syscall: missing exit\n");
784 tcp->flags &= ~TCB_INSYSCALL;
789 if (!(tcp->flags & TCB_INSYSCALL)) {
791 fprintf(stderr, "syscall: missing entry\n");
792 tcp->flags |= TCB_INSYSCALL;
799 * This happens when a signal handler
800 * for a signal which interrupted a
801 * a system call makes another system call.
803 fprintf(stderr, "syscall: missing exit\n");
805 tcp->flags &= ~TCB_INSYSCALL;
811 if (upeek(pid, 4*EAX, &eax) < 0)
813 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
815 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
818 #elif defined (POWERPC)
819 # define SO_MASK 0x10000000
820 if (upeek(pid, 4*PT_CCR, &flags) < 0)
822 if (upeek(pid, 4*PT_R3, &result) < 0)
827 if (upeek(pid, 4*PT_D0, &d0) < 0)
829 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
831 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
835 if (upeek(pid, 4*0, (long *)&r0) < 0)
837 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
839 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
846 if (tcp->flags & TCB_INSYSCALL) {
851 if (eax < 0 && -eax < nerrnos) {
870 if (result && (unsigned) -result < nerrnos) {
875 tcp->u_rval = result;
880 if (d0 && (unsigned) -d0 < nerrnos) {
890 if (r0 && (unsigned) -r0 < nerrnos) {
910 if (regs.r_psr & PSR_C) {
915 tcp->u_rval = regs.r_o0;
927 /* get error code from user struct */
928 if (upeek(pid, uoff(u_error), &u_error) < 0)
930 u_error >>= 24; /* u_error is a char */
932 /* get system call return value */
933 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
938 /* Judicious guessing goes a long way. */
939 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
941 u_error = tcp->status.pr_reg[R_O0];
944 tcp->u_rval = tcp->status.pr_reg[R_O0];
949 /* Wanna know how to kill an hour single-stepping? */
950 if (tcp->status.PR_REG[EFL] & 0x1) {
952 u_error = tcp->status.PR_REG[EAX];
955 tcp->u_rval = tcp->status.PR_REG[EAX];
960 if (tcp->status.pr_reg[CTX_A3]) {
962 u_error = tcp->status.pr_reg[CTX_V0];
965 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
970 tcp->u_error = u_error;
972 internal_syscall(tcp);
973 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
974 tcp->flags &= ~TCB_INSYSCALL;
978 if (tcp->flags & TCB_REPRINT) {
981 if (tcp->scno >= nsyscalls)
982 tprintf("syscall_%lu", tcp->scno);
984 tprintf("%s", sysent[tcp->scno].sys_name);
985 tprintf(" resumed> ");
989 call_count[tcp->scno]++;
991 error_count[tcp->scno]++;
992 tv_sub(&tv, &tv, &tcp->etime);
994 if (tv_cmp(&tv, &tcp->dtime) > 0) {
995 static struct timeval one_tick =
998 if (tv_nz(&tcp->dtime))
1000 else if (tv_cmp(&tv, &one_tick) > 0) {
1001 if (tv_cmp(&shortest, &one_tick) < 0)
1008 if (tv_cmp(&tv, &shortest) < 0)
1010 tv_add(&tv_count[tcp->scno],
1011 &tv_count[tcp->scno], &tv);
1012 tcp->flags &= ~TCB_INSYSCALL;
1016 if (tcp->scno >= nsyscalls
1017 || (qual_flags[tcp->scno] & QUAL_RAW))
1018 sys_res = printargs(tcp);
1020 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1021 u_error = tcp->u_error;
1024 if (qual_flags[tcp->scno] & QUAL_RAW) {
1026 tprintf("= -1 (errno %ld)", u_error);
1028 tprintf("= %#lx", tcp->u_rval);
1030 else if (!(sys_res & RVAL_NONE) && u_error) {
1034 tprintf("= ? ERESTARTSYS (To be restarted)");
1036 case ERESTARTNOINTR:
1037 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1039 case ERESTARTNOHAND:
1040 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1045 if (u_error < nerrnos && u_error < sys_nerr)
1046 tprintf("%s (%s)", errnoent[u_error],
1047 sys_errlist[u_error]);
1048 else if (u_error < nerrnos)
1049 tprintf("%s (errno %ld)",
1050 errnoent[u_error], u_error);
1051 else if (u_error < sys_nerr)
1052 tprintf("ERRNO_%ld (%s)", u_error,
1053 sys_errlist[u_error]);
1055 tprintf("E??? (errno %ld)", u_error);
1062 if (sys_res & RVAL_NONE)
1065 switch (sys_res & RVAL_MASK) {
1067 tprintf("= %#lx", tcp->u_rval);
1070 tprintf("= %#lo", tcp->u_rval);
1073 tprintf("= %lu", tcp->u_rval);
1076 tprintf("= %ld", tcp->u_rval);
1080 "invalid rval format\n");
1084 if ((sys_res & RVAL_STR) && tcp->auxstr)
1085 tprintf(" (%s)", tcp->auxstr);
1088 tv_sub(&tv, &tv, &tcp->etime);
1089 tprintf(" <%ld.%06ld>",
1090 (long) tv.tv_sec, (long) tv.tv_usec);
1095 if (fflush(tcp->outf) == EOF)
1097 tcp->flags &= ~TCB_INSYSCALL;
1101 /* Entering system call */
1107 tcp->u_nargs = sysent[tcp->scno].nargs;
1108 for (i = 0; i < tcp->u_nargs; i++) {
1109 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
1113 #elif defined (ALPHA)
1116 tcp->u_nargs = sysent[tcp->scno].nargs;
1117 for (i = 0; i < tcp->u_nargs; i++) {
1118 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1119 * for scno somewhere above here!
1121 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1125 #elif defined (MIPS)
1130 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1132 if(upeek(pid, REG_SP, &sp) < 0)
1134 for(i = 0; i < 4; i++) {
1135 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1138 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1139 (char *)(tcp->u_arg + 4));
1141 for(i = 0; i < nargs; i++) {
1142 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1147 #elif defined (POWERPC)
1150 tcp->u_nargs = sysent[tcp->scno].nargs;
1151 for (i = 0; i < tcp->u_nargs; i++) {
1152 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1156 #elif defined (SPARC)
1160 tcp->u_nargs = sysent[tcp->scno].nargs;
1161 for (i = 0; i < tcp->u_nargs; i++)
1162 tcp->u_arg[i] = *((®s.r_o0) + i);
1167 tcp->u_nargs = sysent[tcp->scno].nargs;
1168 for (i = 0; i < tcp->u_nargs; i++) {
1169 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1178 tcp->u_nargs = sysent[tcp->scno].nargs;
1179 for (i = 0; i < tcp->u_nargs; i++) {
1182 if (upeek(pid, uoff(u_arg[0]) +
1183 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1191 * SGI is broken: even though it has pr_sysarg, it doesn't
1192 * set them on system call entry. Get a clue.
1194 if (sysent[tcp->scno].nargs != -1)
1195 tcp->u_nargs = sysent[tcp->scno].nargs;
1197 tcp->u_nargs = tcp->status.pr_nsysarg;
1198 if (tcp->u_nargs > 4) {
1199 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1200 4*sizeof(tcp->u_arg[0]));
1201 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1202 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1205 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1206 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1209 #ifdef HAVE_PR_SYSCALL
1210 if (sysent[tcp->scno].nargs != -1)
1211 tcp->u_nargs = sysent[tcp->scno].nargs;
1213 tcp->u_nargs = tcp->status.pr_nsysarg;
1216 for (i = 0; i < tcp->u_nargs; i++)
1217 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1219 #else /* !HAVE_PR_SYSCALL */
1221 if (sysent[tcp->scno].nargs != -1)
1222 tcp->u_nargs = sysent[tcp->scno].nargs;
1225 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1229 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1230 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1232 #endif /* !HAVE_PR_SYSCALL */
1236 switch (tcp->scno + __NR_SYSCALL_BASE) {
1238 switch (tcp->scno) {
1241 #if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS)
1242 case SYS_socketcall:
1243 decode_subcall(tcp, SYS_socket_subcall,
1244 SYS_socket_nsubcalls, deref_style);
1247 decode_subcall(tcp, SYS_ipc_subcall,
1248 SYS_ipc_nsubcalls, shift_style);
1250 #endif /* !ALPHA && !SPARC */
1252 case SYS_socketcall:
1253 sparc_socket_decode (tcp);
1258 #ifdef SYS_pgrpsys_subcall
1260 decode_subcall(tcp, SYS_pgrpsys_subcall,
1261 SYS_pgrpsys_nsubcalls, shift_style);
1263 #endif /* SYS_pgrpsys_subcall */
1264 #ifdef SYS_sigcall_subcall
1266 decode_subcall(tcp, SYS_sigcall_subcall,
1267 SYS_sigcall_nsubcalls, mask_style);
1269 #endif /* SYS_sigcall_subcall */
1271 decode_subcall(tcp, SYS_msgsys_subcall,
1272 SYS_msgsys_nsubcalls, shift_style);
1275 decode_subcall(tcp, SYS_shmsys_subcall,
1276 SYS_shmsys_nsubcalls, shift_style);
1279 decode_subcall(tcp, SYS_semsys_subcall,
1280 SYS_semsys_nsubcalls, shift_style);
1284 decode_subcall(tcp, SYS_utssys_subcall,
1285 SYS_utssys_nsubcalls, shift_style);
1289 decode_subcall(tcp, SYS_sysfs_subcall,
1290 SYS_sysfs_nsubcalls, shift_style);
1293 decode_subcall(tcp, SYS_spcall_subcall,
1294 SYS_spcall_nsubcalls, shift_style);
1296 #ifdef SYS_context_subcall
1298 decode_subcall(tcp, SYS_context_subcall,
1299 SYS_context_nsubcalls, shift_style);
1301 #endif /* SYS_context_subcall */
1302 #ifdef SYS_door_subcall
1304 decode_subcall(tcp, SYS_door_subcall,
1305 SYS_door_nsubcalls, door_style);
1307 #endif /* SYS_door_subcall */
1311 decode_subcall(tcp, SYS_semsys_subcall,
1312 SYS_semsys_nsubcalls, shift_style);
1315 decode_subcall(tcp, SYS_msgsys_subcall,
1316 SYS_msgsys_nsubcalls, shift_style);
1319 decode_subcall(tcp, SYS_shmsys_subcall,
1320 SYS_shmsys_nsubcalls, shift_style);
1325 internal_syscall(tcp);
1326 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1327 tcp->flags |= TCB_INSYSCALL;
1332 gettimeofday(&tcp->etime, NULL);
1333 tcp->flags |= TCB_INSYSCALL;
1338 tcp->flags &= ~TCB_REPRINT;
1340 if (tcp->scno >= nsyscalls)
1341 tprintf("syscall_%lu(", tcp->scno);
1343 tprintf("%s(", sysent[tcp->scno].sys_name);
1344 if (tcp->scno >= nsyscalls ||
1345 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1346 sys_res = printargs(tcp);
1348 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1349 if (fflush(tcp->outf) == EOF)
1351 tcp->flags |= TCB_INSYSCALL;
1352 /* Measure the entrance time as late as possible to avoid errors. */
1354 gettimeofday(&tcp->etime, NULL);
1362 if (entering(tcp)) {
1365 for (i = 0; i < tcp->u_nargs; i++)
1366 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1380 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
1387 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1393 val = tcp->status.PR_REG[R_O1];
1396 val = tcp->status.PR_REG[EDX];
1399 val = tcp->status.PR_REG[CTX_V1];
1407 * Apparently, indirect system calls have already be converted by ptrace(2),
1408 * so if you see "indir" this program has gone astray.
1416 if (entering(tcp)) {
1417 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1418 fprintf(stderr, "Bogus syscall: %u\n", scno);
1421 nargs = sysent[scno].nargs;
1422 tprintf("%s", sysent[scno].sys_name);
1423 for (i = 0; i < nargs; i++)
1424 tprintf(", %#lx", tcp->u_arg[i+1]);
1434 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1442 return strcmp(sysent[*((int *) a)].sys_name,
1443 sysent[*((int *) b)].sys_name);
1451 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1453 return (m < n) ? 1 : (m > n) ? -1 : 0;
1456 static int (*sortfun)();
1457 static struct timeval overhead = { -1, -1 };
1463 if (strcmp(sortby, "time") == 0)
1465 else if (strcmp(sortby, "calls") == 0)
1466 sortfun = count_cmp;
1467 else if (strcmp(sortby, "name") == 0)
1468 sortfun = syscall_cmp;
1469 else if (strcmp(sortby, "nothing") == 0)
1472 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1477 void set_overhead(n)
1480 overhead.tv_sec = n / 1000000;
1481 overhead.tv_usec = n % 1000000;
1489 int call_cum, error_cum;
1490 struct timeval tv_cum, dtv;
1492 char *dashes = "-------------------------";
1495 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1496 if (overhead.tv_sec == -1) {
1497 tv_mul(&overhead, &shortest, 8);
1498 tv_div(&overhead, &overhead, 10);
1500 for (i = 0; i < nsyscalls; i++) {
1501 sorted_count[i] = i;
1502 if (call_count[i] == 0)
1504 tv_mul(&dtv, &overhead, call_count[i]);
1505 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1506 call_cum += call_count[i];
1507 error_cum += error_count[i];
1508 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1511 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1512 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1513 "% time", "seconds", "usecs/call",
1514 "calls", "errors", "syscall");
1515 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1516 dashes, dashes, dashes, dashes, dashes, dashes);
1517 for (i = 0; i < nsyscalls; i++) {
1518 j = sorted_count[i];
1519 if (call_count[j] == 0)
1521 tv_div(&dtv, &tv_count[j], call_count[j]);
1523 sprintf(error_str, "%d", error_count[j]);
1525 error_str[0] = '\0';
1526 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1527 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1528 percent, (long) tv_count[j].tv_sec,
1529 (long) tv_count[j].tv_usec,
1530 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1531 call_count[j], error_str, sysent[j].sys_name);
1533 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1534 dashes, dashes, dashes, dashes, dashes, dashes);
1536 sprintf(error_str, "%d", error_cum);
1538 error_str[0] = '\0';
1539 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1540 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1541 call_cum, error_str, "total");