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>
47 # define fpq kernel_fpq
49 # define fpu kernel_fpu
61 #ifndef PTRACE_PEEKUSR
62 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
64 #elif defined(HAVE_LINUX_PTRACE_H)
66 #include <linux/ptrace.h>
69 #if defined(LINUX) && defined(IA64)
70 # include <asm/ptrace_offsets.h>
74 #ifndef SYS_ERRLIST_DECLARED
76 extern char *sys_errlist[];
77 #endif /* SYS_ERRLIST_DECLARED */
79 #define NR_SYSCALL_BASE 0
82 #define ERESTARTSYS 512
84 #ifndef ERESTARTNOINTR
85 #define ERESTARTNOINTR 513
87 #ifndef ERESTARTNOHAND
88 #define ERESTARTNOHAND 514 /* restart if no handler.. */
91 #define ENOIOCTLCMD 515 /* No ioctl command */
99 #undef NR_SYSCALL_BASE
100 #define NR_SYSCALL_BASE __NR_SYSCALL_BASE
106 /* Define these shorthand notations to simplify the syscallent files. */
107 #define TF TRACE_FILE
109 #define TN TRACE_NETWORK
110 #define TP TRACE_PROCESS
111 #define TS TRACE_SIGNAL
113 struct sysent sysent0[] = {
114 #include "syscallent.h"
116 int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
118 #if SUPPORTED_PERSONALITIES >= 2
119 struct sysent sysent1[] = {
120 #include "syscallent1.h"
122 int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
123 #endif /* SUPPORTED_PERSONALITIES >= 2 */
125 #if SUPPORTED_PERSONALITIES >= 3
126 struct sysent sysent2[] = {
127 #include "syscallent2.h"
129 int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
130 #endif /* SUPPORTED_PERSONALITIES >= 3 */
132 struct sysent *sysent;
135 /* Now undef them since short defines cause wicked namespace pollution. */
142 char *errnoent0[] = {
143 #include "errnoent.h"
145 int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
147 #if SUPPORTED_PERSONALITIES >= 2
148 char *errnoent1[] = {
149 #include "errnoent1.h"
151 int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
152 #endif /* SUPPORTED_PERSONALITIES >= 2 */
154 #if SUPPORTED_PERSONALITIES >= 3
155 char *errnoent2[] = {
156 #include "errnoent2.h"
158 int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
159 #endif /* SUPPORTED_PERSONALITIES >= 3 */
164 int current_personality;
167 set_personality(personality)
170 switch (personality) {
172 errnoent = errnoent0;
175 nsyscalls = nsyscalls0;
176 ioctlent = ioctlent0;
177 nioctlents = nioctlents0;
178 signalent = signalent0;
179 nsignals = nsignals0;
182 #if SUPPORTED_PERSONALITIES >= 2
184 errnoent = errnoent1;
187 nsyscalls = nsyscalls1;
188 ioctlent = ioctlent1;
189 nioctlents = nioctlents1;
190 signalent = signalent1;
191 nsignals = nsignals1;
193 #endif /* SUPPORTED_PERSONALITIES >= 2 */
195 #if SUPPORTED_PERSONALITIES >= 3
197 errnoent = errnoent2;
200 nsyscalls = nsyscalls2;
201 ioctlent = ioctlent2;
202 nioctlents = nioctlents2;
203 signalent = signalent2;
204 nsignals = nsignals2;
206 #endif /* SUPPORTED_PERSONALITIES >= 3 */
212 current_personality = personality;
216 int qual_flags[MAX_QUALS];
218 static int call_count[MAX_QUALS];
219 static int error_count[MAX_QUALS];
220 static struct timeval tv_count[MAX_QUALS];
221 static int sorted_count[MAX_QUALS];
223 static struct timeval shortest = { 1000000, 0 };
225 static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
227 static struct qual_options {
233 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
234 { QUAL_TRACE, "t", lookup_syscall, "system call" },
235 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
236 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
237 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
238 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
239 { QUAL_RAW, "raw", lookup_syscall, "system call" },
240 { QUAL_RAW, "x", lookup_syscall, "system call" },
241 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
242 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
243 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
244 { QUAL_FAULT, "fault", lookup_fault, "fault" },
245 { QUAL_FAULT, "faults", lookup_fault, "fault" },
246 { QUAL_FAULT, "m", lookup_fault, "fault" },
247 { QUAL_READ, "read", lookup_desc, "descriptor" },
248 { QUAL_READ, "reads", lookup_desc, "descriptor" },
249 { QUAL_READ, "r", lookup_desc, "descriptor" },
250 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
251 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
252 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
253 { 0, NULL, NULL, NULL },
262 for (i = 0; i < nsyscalls; i++) {
263 if (strcmp(s, sysent[i].sys_name) == 0)
276 if (s && *s && isdigit((unsigned char)*s))
280 for (i = 0; s[i]; i++)
281 s[i] = toupper((unsigned char)(s[i]));
282 if (strncmp(s, "SIG", 3) == 0)
284 for (i = 0; i <= NSIG; i++) {
285 if (strcmp(s, signame(i) + 3) == 0)
302 if (s && *s && isdigit((unsigned char)*s))
311 if (strcmp(s, "file") == 0)
313 if (strcmp(s, "ipc") == 0)
315 if (strcmp(s, "network") == 0)
316 return TRACE_NETWORK;
317 if (strcmp(s, "process") == 0)
318 return TRACE_PROCESS;
319 if (strcmp(s, "signal") == 0)
328 struct qual_options *opt;
333 opt = &qual_options[0];
334 for (i = 0; (p = qual_options[i].option_name); i++) {
336 if (strncmp(s, p, n) == 0 && s[n] == '=') {
337 opt = &qual_options[i];
347 if (strcmp(s, "none") == 0) {
351 if (strcmp(s, "all") == 0) {
352 for (i = 0; i < MAX_QUALS; i++) {
354 qual_flags[i] &= ~opt->bitflag;
356 qual_flags[i] |= opt->bitflag;
360 for (i = 0; i < MAX_QUALS; i++) {
362 qual_flags[i] |= opt->bitflag;
364 qual_flags[i] &= ~opt->bitflag;
366 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
367 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
368 for (i = 0; i < MAX_QUALS; i++) {
369 if (sysent[i].sys_flags & n) {
371 qual_flags[i] &= ~opt->bitflag;
373 qual_flags[i] |= opt->bitflag;
378 if ((n = (*opt->lookup)(p)) < 0) {
379 fprintf(stderr, "strace: invalid %s `%s'\n",
380 opt->argument_name, p);
384 qual_flags[n] &= ~opt->bitflag;
386 qual_flags[n] |= opt->bitflag;
397 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
399 switch (tcp->scno + NR_SYSCALL_BASE) {
407 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
408 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
417 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
418 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
422 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
423 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
429 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
430 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
437 enum subcall_style { shift_style, deref_style, mask_style, door_style };
439 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
447 const struct subcall subcalls_table[] = {
448 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
450 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
452 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
454 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
458 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
460 const int socket_map [] = {
463 /* SYS_CONNECT */ 98,
464 /* SYS_LISTEN */ 106,
466 /* SYS_GETSOCKNAME */ 150,
467 /* SYS_GETPEERNAME */ 141,
468 /* SYS_SOCKETPAIR */ 135,
471 /* SYS_SENDTO */ 133,
472 /* SYS_RECVFROM */ 125,
473 /* SYS_SHUTDOWN */ 134,
474 /* SYS_SETSOCKOPT */ 105,
475 /* SYS_GETSOCKOPT */ 118,
476 /* SYS_SENDMSG */ 114,
477 /* SYS_RECVMSG */ 113
481 sparc_socket_decode (tcp)
487 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
490 tcp->scno = socket_map [tcp->u_arg [0]-1];
491 n = tcp->u_nargs = sysent [tcp->scno].nargs;
492 addr = tcp->u_arg [1];
493 for (i = 0; i < n; i++){
495 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
497 tcp->u_arg [i] = arg;
498 addr += sizeof (arg);
503 decode_subcall(tcp, subcall, nsubcalls, style)
507 enum subcall_style style;
509 int i, addr, mask, arg;
513 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
515 tcp->scno = subcall + tcp->u_arg[0];
516 if (sysent[tcp->scno].nargs != -1)
517 tcp->u_nargs = sysent[tcp->scno].nargs;
520 for (i = 0; i < tcp->u_nargs; i++)
521 tcp->u_arg[i] = tcp->u_arg[i + 1];
524 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
526 tcp->scno = subcall + tcp->u_arg[0];
527 addr = tcp->u_arg[1];
528 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
529 if (umove(tcp, addr, &arg) < 0)
534 tcp->u_nargs = sysent[tcp->scno].nargs;
537 mask = (tcp->u_arg[0] >> 8) & 0xff;
538 for (i = 0; mask; i++)
542 tcp->u_arg[0] &= 0xff;
543 tcp->scno = subcall + i;
544 if (sysent[tcp->scno].nargs != -1)
545 tcp->u_nargs = sysent[tcp->scno].nargs;
549 * Oh, yuck. The call code is the *sixth* argument.
550 * (don't you mean the *last* argument? - JH)
552 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
554 tcp->scno = subcall + tcp->u_arg[5];
555 if (sysent[tcp->scno].nargs != -1)
556 tcp->u_nargs = sysent[tcp->scno].nargs;
562 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
563 if (subcalls_table[i].call == tcp->scno) break;
564 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
565 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
566 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
567 for (i = 0; i < tcp->u_nargs; i++)
568 tcp->u_arg[i] = tcp->u_arg[i + 1];
576 struct tcb *tcp_last = NULL;
579 internal_syscall(tcp)
583 * We must always trace a few critical system calls in order to
584 * correctly support following forks in the presence of tracing
587 switch (tcp->scno + NR_SYSCALL_BASE) {
667 #elif defined (POWERPC)
668 static long result,flags;
673 #elif defined (ALPHA)
676 #elif defined (SPARC)
677 static struct regs regs;
678 static unsigned long trap;
704 if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
706 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
710 #elif defined (POWERPC)
711 if (upeek(pid, 4*PT_R0, &scno) < 0)
713 if (!(tcp->flags & TCB_INSYSCALL)) {
714 /* Check if we return from execve. */
715 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
716 tcp->flags &= ~TCB_WAITEXECVE;
721 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
724 # define IA64_PSR_IS ((long)1 << 34)
725 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
726 ia32 = (psr & IA64_PSR_IS) != 0;
727 if (!(tcp->flags & TCB_INSYSCALL)) {
729 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
731 /* Check if we return from execve. */
733 if (upeek (pid, PT_R15, &scno) < 0)
737 /* syscall in progress */
738 if (upeek (pid, PT_R8, &r8) < 0)
740 if (upeek (pid, PT_R10, &r10) < 0)
743 if (tcp->flags & TCB_WAITEXECVE) {
744 tcp->flags &= ~TCB_WAITEXECVE;
751 upeek(pid, 4*15, &pc);
752 umoven(tcp, pc-4, 4, (char *)&scno);
756 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
759 if (upeek(pid, REG_A3, &a3) < 0)
762 if(!(tcp->flags & TCB_INSYSCALL)) {
763 if (upeek(pid, REG_V0, &scno) < 0)
766 if (scno < 0 || scno > nsyscalls) {
767 if(a3 == 0 || a3 == -1) {
769 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
774 if (upeek(pid, REG_V0, &r2) < 0)
777 #elif defined (ALPHA)
778 if (upeek(pid, REG_A3, &a3) < 0)
781 if (!(tcp->flags & TCB_INSYSCALL)) {
782 if (upeek(pid, REG_R0, &scno) < 0)
785 /* Check if we return from execve. */
786 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
787 tcp->flags &= ~TCB_WAITEXECVE;
792 * Do some sanity checks to figure out if it's
793 * really a syscall entry
795 if (scno < 0 || scno > nsyscalls) {
796 if (a3 == 0 || a3 == -1) {
798 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
804 if (upeek(pid, REG_R0, &r0) < 0)
807 #elif defined (SPARC)
808 /* Everything we need is in the current register set. */
809 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
812 /* If we are entering, then disassemble the syscall trap. */
813 if (!(tcp->flags & TCB_INSYSCALL)) {
814 /* Retrieve the syscall trap instruction. */
816 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
820 /* Disassemble the trap to see what personality to use. */
823 /* Linux/SPARC syscall trap. */
827 /* Linux/SPARC64 syscall trap. */
828 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
831 /* SunOS syscall trap. (pers 1) */
832 fprintf(stderr,"syscall: SunOS no support\n");
835 /* Solaris 2.x syscall trap. (per 2) */
839 /* NetBSD/FreeBSD syscall trap. */
840 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
843 /* Solaris 2.x gettimeofday */
847 /* Unknown syscall trap. */
848 if(tcp->flags & TCB_WAITEXECVE) {
849 tcp->flags &= ~TCB_WAITEXECVE;
852 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
856 /* Extract the system call number from the registers. */
857 if (trap == 0x91d02027)
863 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
867 if (upeek(pid, PT_GR20, &scno) < 0)
869 if (!(tcp->flags & TCB_INSYSCALL)) {
870 /* Check if we return from execve. */
871 if ((tcp->flags & TCB_WAITEXECVE)) {
872 tcp->flags &= ~TCB_WAITEXECVE;
879 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
883 #ifdef HAVE_PR_SYSCALL
884 scno = tcp->status.PR_SYSCALL;
885 #else /* !HAVE_PR_SYSCALL */
887 scno = tcp->status.PR_WHAT;
889 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
893 switch (regs.r_eax) {
896 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
903 #endif /* !HAVE_PR_SYSCALL */
904 #endif /* USE_PROCFS */
905 if (!(tcp->flags & TCB_INSYSCALL))
917 #else /* USE_PROCFS */
918 int scno = tcp->scno;
920 if (!(tcp->flags & TCB_INSYSCALL)) {
921 if (tcp->status.PR_WHY != PR_SYSENTRY) {
926 #endif /* SYS_vfork */
929 #endif /* SYS_fork1 */
931 || scno == SYS_forkall
932 #endif /* SYS_forkall */
934 || scno == SYS_rfork1
935 #endif /* SYS_fork1 */
937 || scno == SYS_rforkall
938 #endif /* SYS_rforkall */
940 /* We are returning in the child, fake it. */
941 tcp->status.PR_WHY = PR_SYSENTRY;
943 tcp->status.PR_WHY = PR_SYSEXIT;
946 fprintf(stderr, "syscall: missing entry\n");
947 tcp->flags |= TCB_INSYSCALL;
952 if (tcp->status.PR_WHY != PR_SYSEXIT) {
953 fprintf(stderr, "syscall: missing exit\n");
954 tcp->flags &= ~TCB_INSYSCALL;
957 #endif /* USE_PROCFS */
959 if (!(tcp->flags & TCB_INSYSCALL)) {
961 fprintf(stderr, "syscall: missing entry\n");
962 tcp->flags |= TCB_INSYSCALL;
969 * This happens when a signal handler
970 * for a signal which interrupted a
971 * a system call makes another system call.
973 fprintf(stderr, "syscall: missing exit\n");
975 tcp->flags &= ~TCB_INSYSCALL;
981 if (upeek(pid, 4*EAX, &eax) < 0)
983 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
985 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
989 if (upeek(pid, PT_GPR2, &gpr2) < 0)
991 if (gpr2 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
993 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
996 #elif defined (POWERPC)
997 # define SO_MASK 0x10000000
998 if (upeek(pid, 4*PT_CCR, &flags) < 0)
1000 if (upeek(pid, 4*PT_R3, &result) < 0)
1002 if (flags & SO_MASK)
1004 #elif defined (M68K)
1005 if (upeek(pid, 4*PT_D0, &d0) < 0)
1007 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1009 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1013 if (upeek(pid, 4*0, (long *)&r0) < 0)
1015 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1017 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
1020 #elif defined (HPPA)
1021 if (upeek(pid, PT_GR28, &r28) < 0)
1024 if (upeek(pid, PT_R10, &r10) < 0)
1026 if (upeek(pid, PT_R8, &r8) < 0)
1028 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1030 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1045 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1055 if (eax < 0 && -eax < nerrnos) {
1069 if (err < 0 && -err < nerrnos) {
1097 if (result && (unsigned) -result < nerrnos) {
1102 tcp->u_rval = result;
1105 #else /* !POWERPC */
1107 if (d0 && (unsigned) -d0 < nerrnos) {
1117 if (r0 && (unsigned) -r0 < nerrnos) {
1137 if (regs.r_psr & PSR_C) {
1139 u_error = regs.r_o0;
1142 tcp->u_rval = regs.r_o0;
1147 if (r28 && (unsigned) -r28 < nerrnos) {
1160 #endif /* POWERPC */
1167 /* get error code from user struct */
1168 if (upeek(pid, uoff(u_error), &u_error) < 0)
1170 u_error >>= 24; /* u_error is a char */
1172 /* get system call return value */
1173 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1178 /* Judicious guessing goes a long way. */
1179 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1181 u_error = tcp->status.pr_reg[R_O0];
1184 tcp->u_rval = tcp->status.pr_reg[R_O0];
1189 /* Wanna know how to kill an hour single-stepping? */
1190 if (tcp->status.PR_REG[EFL] & 0x1) {
1192 u_error = tcp->status.PR_REG[EAX];
1195 tcp->u_rval = tcp->status.PR_REG[EAX];
1196 #ifdef HAVE_LONG_LONG
1198 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1199 tcp->status.PR_REG[EAX];
1205 if (tcp->status.pr_reg[CTX_A3]) {
1207 u_error = tcp->status.pr_reg[CTX_V0];
1210 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1216 if (regs.r_eflags & PSL_C) {
1218 u_error = regs.r_eax;
1220 tcp->u_rval = regs.r_eax;
1222 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1225 #endif /* FREEBSD */
1226 tcp->u_error = u_error;
1230 int syscall_enter(tcp)
1235 #endif /* !USE_PROCFS */
1240 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1241 tcp->u_nargs = sysent[tcp->scno].nargs;
1243 tcp->u_nargs = MAX_ARGS;
1244 for (i = 0; i < tcp->u_nargs; i++) {
1245 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
1249 #elif defined (ALPHA)
1252 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1253 tcp->u_nargs = sysent[tcp->scno].nargs;
1255 tcp->u_nargs = MAX_ARGS;
1256 for (i = 0; i < tcp->u_nargs; i++) {
1257 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1258 * for scno somewhere above here!
1260 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1264 #elif defined (IA64)
1267 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1268 /* be backwards compatible with kernel < 2.4.4... */
1270 # define PT_RBS_END PT_AR_BSP
1273 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
1275 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1278 sof = (cfm >> 0) & 0x7f;
1279 sol = (cfm >> 7) & 0x7f;
1280 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1282 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1283 && sysent[tcp->scno].nargs != -1)
1284 tcp->u_nargs = sysent[tcp->scno].nargs;
1286 tcp->u_nargs = MAX_ARGS;
1287 for (i = 0; i < tcp->u_nargs; ++i) {
1288 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1289 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1295 if (/* EBX = out0 */
1296 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1298 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1300 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1302 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1304 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1306 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1309 for (i = 0; i < 6; ++i)
1310 /* truncate away IVE sign-extension */
1311 tcp->u_arg[i] &= 0xffffffff;
1313 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1314 && sysent[tcp->scno].nargs != -1)
1315 tcp->u_nargs = sysent[tcp->scno].nargs;
1320 #elif defined (MIPS)
1325 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1326 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1328 nargs = tcp->u_nargs = MAX_ARGS;
1330 if(upeek(pid, REG_SP, &sp) < 0)
1332 for(i = 0; i < 4; i++) {
1333 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1336 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1337 (char *)(tcp->u_arg + 4));
1339 for(i = 0; i < nargs; i++) {
1340 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1345 #elif defined (POWERPC)
1348 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1349 tcp->u_nargs = sysent[tcp->scno].nargs;
1351 tcp->u_nargs = MAX_ARGS;
1352 for (i = 0; i < tcp->u_nargs; i++) {
1353 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1357 #elif defined (SPARC)
1361 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1362 tcp->u_nargs = sysent[tcp->scno].nargs;
1364 tcp->u_nargs = MAX_ARGS;
1365 for (i = 0; i < tcp->u_nargs; i++)
1366 tcp->u_arg[i] = *((®s.r_o0) + i);
1368 #elif defined (HPPA)
1372 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1373 tcp->u_nargs = sysent[tcp->scno].nargs;
1375 tcp->u_nargs = MAX_ARGS;
1376 for (i = 0; i < tcp->u_nargs; i++) {
1377 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1381 #else /* Other architecture (like i386) (32bits specific) */
1384 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1385 tcp->u_nargs = sysent[tcp->scno].nargs;
1387 tcp->u_nargs = MAX_ARGS;
1388 for (i = 0; i < tcp->u_nargs; i++) {
1389 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1398 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1399 tcp->u_nargs = sysent[tcp->scno].nargs;
1401 tcp->u_nargs = MAX_ARGS;
1402 for (i = 0; i < tcp->u_nargs; i++) {
1405 if (upeek(pid, uoff(u_arg[0]) +
1406 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1414 * SGI is broken: even though it has pr_sysarg, it doesn't
1415 * set them on system call entry. Get a clue.
1417 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1418 tcp->u_nargs = sysent[tcp->scno].nargs;
1420 tcp->u_nargs = tcp->status.pr_nsysarg;
1421 if (tcp->u_nargs > 4) {
1422 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1423 4*sizeof(tcp->u_arg[0]));
1424 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1425 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1428 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1429 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1433 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1435 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1436 tcp->u_nargs = sysent[tcp->scno].nargs;
1438 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1439 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1440 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1441 #elif defined (HAVE_PR_SYSCALL)
1442 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1443 tcp->u_nargs = sysent[tcp->scno].nargs;
1445 tcp->u_nargs = tcp->status.pr_nsysarg;
1448 for (i = 0; i < tcp->u_nargs; i++)
1449 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1451 #elif defined (I386)
1452 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1453 tcp->u_nargs = sysent[tcp->scno].nargs;
1456 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1457 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1459 I DONT KNOW WHAT TO DO
1460 #endif /* !HAVE_PR_SYSCALL */
1463 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1464 sysent[tcp->scno].nargs > tcp->status.val)
1465 tcp->u_nargs = sysent[tcp->scno].nargs;
1467 tcp->u_nargs = tcp->status.val;
1468 if (tcp->u_nargs < 0)
1470 if (tcp->u_nargs > MAX_ARGS)
1471 tcp->u_nargs = MAX_ARGS;
1472 switch(regs.r_eax) {
1474 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1475 regs.r_esp + sizeof(int) + sizeof(quad_t));
1478 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1479 regs.r_esp + 2 * sizeof(int));
1482 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1483 regs.r_esp + sizeof(int));
1486 #endif /* FREEBSD */
1498 /* Measure the exit time as early as possible to avoid errors. */
1499 if (dtime && (tcp->flags & TCB_INSYSCALL))
1500 gettimeofday(&tv, NULL);
1502 res = get_scno(tcp);
1506 res = syscall_fixup(tcp);
1510 if (tcp->flags & TCB_INSYSCALL) {
1512 res = get_error(tcp);
1515 u_error = tcp->u_error;
1518 internal_syscall(tcp);
1519 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1520 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
1521 tcp->flags &= ~TCB_INSYSCALL;
1525 if (tcp->flags & TCB_REPRINT) {
1528 if (tcp->scno >= nsyscalls || tcp->scno < 0)
1529 tprintf("syscall_%lu", tcp->scno);
1531 tprintf("%s", sysent[tcp->scno].sys_name);
1532 tprintf(" resumed> ");
1535 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
1536 call_count[tcp->scno]++;
1538 error_count[tcp->scno]++;
1539 tv_sub(&tv, &tv, &tcp->etime);
1541 if (tv_cmp(&tv, &tcp->dtime) > 0) {
1542 static struct timeval one_tick =
1543 { 0, 1000000 / HZ };
1545 if (tv_nz(&tcp->dtime))
1547 else if (tv_cmp(&tv, &one_tick) > 0) {
1548 if (tv_cmp(&shortest, &one_tick) < 0)
1555 if (tv_cmp(&tv, &shortest) < 0)
1557 tv_add(&tv_count[tcp->scno],
1558 &tv_count[tcp->scno], &tv);
1559 tcp->flags &= ~TCB_INSYSCALL;
1563 if (tcp->scno >= nsyscalls || tcp->scno < 0
1564 || (qual_flags[tcp->scno] & QUAL_RAW))
1565 sys_res = printargs(tcp);
1567 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1568 u_error = tcp->u_error;
1571 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
1572 qual_flags[tcp->scno] & QUAL_RAW) {
1574 tprintf("= -1 (errno %ld)", u_error);
1576 tprintf("= %#lx", tcp->u_rval);
1578 else if (!(sys_res & RVAL_NONE) && u_error) {
1582 tprintf("= ? ERESTARTSYS (To be restarted)");
1584 case ERESTARTNOINTR:
1585 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1587 case ERESTARTNOHAND:
1588 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1594 tprintf("E??? (errno %ld)", u_error);
1595 else if (u_error < nerrnos && u_error < sys_nerr)
1596 tprintf("%s (%s)", errnoent[u_error],
1597 sys_errlist[u_error]);
1598 else if (u_error < nerrnos)
1599 tprintf("%s (errno %ld)",
1600 errnoent[u_error], u_error);
1601 else if (u_error < sys_nerr)
1602 tprintf("ERRNO_%ld (%s)", u_error,
1603 sys_errlist[u_error]);
1605 tprintf("E??? (errno %ld)", u_error);
1610 if (sys_res & RVAL_NONE)
1613 switch (sys_res & RVAL_MASK) {
1615 tprintf("= %#lx", tcp->u_rval);
1618 tprintf("= %#lo", tcp->u_rval);
1621 tprintf("= %lu", tcp->u_rval);
1624 tprintf("= %ld", tcp->u_rval);
1626 #ifdef HAVE_LONG_LONG
1628 tprintf("= %#llx", tcp->u_lrval);
1631 tprintf("= %#llo", tcp->u_lrval);
1633 case RVAL_LUDECIMAL:
1634 tprintf("= %llu", tcp->u_lrval);
1637 tprintf("= %lld", tcp->u_lrval);
1642 "invalid rval format\n");
1646 if ((sys_res & RVAL_STR) && tcp->auxstr)
1647 tprintf(" (%s)", tcp->auxstr);
1650 tv_sub(&tv, &tv, &tcp->etime);
1651 tprintf(" <%ld.%06ld>",
1652 (long) tv.tv_sec, (long) tv.tv_usec);
1657 if (fflush(tcp->outf) == EOF)
1659 tcp->flags &= ~TCB_INSYSCALL;
1663 /* Entering system call */
1664 res = syscall_enter(tcp);
1668 switch (tcp->scno + NR_SYSCALL_BASE) {
1670 #if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA)
1671 case SYS_socketcall:
1672 decode_subcall(tcp, SYS_socket_subcall,
1673 SYS_socket_nsubcalls, deref_style);
1676 decode_subcall(tcp, SYS_ipc_subcall,
1677 SYS_ipc_nsubcalls, shift_style);
1679 #endif /* !ALPHA && !MIPS && !SPARC */
1681 case SYS_socketcall:
1682 sparc_socket_decode (tcp);
1687 #ifdef SYS_pgrpsys_subcall
1689 decode_subcall(tcp, SYS_pgrpsys_subcall,
1690 SYS_pgrpsys_nsubcalls, shift_style);
1692 #endif /* SYS_pgrpsys_subcall */
1693 #ifdef SYS_sigcall_subcall
1695 decode_subcall(tcp, SYS_sigcall_subcall,
1696 SYS_sigcall_nsubcalls, mask_style);
1698 #endif /* SYS_sigcall_subcall */
1700 decode_subcall(tcp, SYS_msgsys_subcall,
1701 SYS_msgsys_nsubcalls, shift_style);
1704 decode_subcall(tcp, SYS_shmsys_subcall,
1705 SYS_shmsys_nsubcalls, shift_style);
1708 decode_subcall(tcp, SYS_semsys_subcall,
1709 SYS_semsys_nsubcalls, shift_style);
1713 decode_subcall(tcp, SYS_utssys_subcall,
1714 SYS_utssys_nsubcalls, shift_style);
1718 decode_subcall(tcp, SYS_sysfs_subcall,
1719 SYS_sysfs_nsubcalls, shift_style);
1722 decode_subcall(tcp, SYS_spcall_subcall,
1723 SYS_spcall_nsubcalls, shift_style);
1725 #ifdef SYS_context_subcall
1727 decode_subcall(tcp, SYS_context_subcall,
1728 SYS_context_nsubcalls, shift_style);
1730 #endif /* SYS_context_subcall */
1731 #ifdef SYS_door_subcall
1733 decode_subcall(tcp, SYS_door_subcall,
1734 SYS_door_nsubcalls, door_style);
1736 #endif /* SYS_door_subcall */
1737 #ifdef SYS_kaio_subcall
1739 decode_subcall(tcp, SYS_kaio_subcall,
1740 SYS_kaio_nsubcalls, shift_style);
1748 decode_subcall(tcp, 0, 0, table_style);
1753 decode_subcall(tcp, SYS_semsys_subcall,
1754 SYS_semsys_nsubcalls, shift_style);
1757 decode_subcall(tcp, SYS_msgsys_subcall,
1758 SYS_msgsys_nsubcalls, shift_style);
1761 decode_subcall(tcp, SYS_shmsys_subcall,
1762 SYS_shmsys_nsubcalls, shift_style);
1767 internal_syscall(tcp);
1768 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
1769 tcp->flags |= TCB_INSYSCALL;
1774 gettimeofday(&tcp->etime, NULL);
1775 tcp->flags |= TCB_INSYSCALL;
1780 tcp->flags &= ~TCB_REPRINT;
1782 if (tcp->scno >= nsyscalls || tcp->scno < 0)
1783 tprintf("syscall_%lu(", tcp->scno);
1785 tprintf("%s(", sysent[tcp->scno].sys_name);
1786 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
1787 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1788 sys_res = printargs(tcp);
1790 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1791 if (fflush(tcp->outf) == EOF)
1793 tcp->flags |= TCB_INSYSCALL;
1794 /* Measure the entrance time as late as possible to avoid errors. */
1796 gettimeofday(&tcp->etime, NULL);
1804 if (entering(tcp)) {
1807 for (i = 0; i < tcp->u_nargs; i++)
1808 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1822 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
1829 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1835 val = tcp->status.PR_REG[R_O1];
1838 val = tcp->status.PR_REG[EDX];
1841 val = tcp->status.PR_REG[CTX_V1];
1846 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
1853 * Apparently, indirect system calls have already be converted by ptrace(2),
1854 * so if you see "indir" this program has gone astray.
1862 if (entering(tcp)) {
1863 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1864 fprintf(stderr, "Bogus syscall: %u\n", scno);
1867 nargs = sysent[scno].nargs;
1868 tprintf("%s", sysent[scno].sys_name);
1869 for (i = 0; i < nargs; i++)
1870 tprintf(", %#lx", tcp->u_arg[i+1]);
1880 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1888 return strcmp(sysent[*((int *) a)].sys_name,
1889 sysent[*((int *) b)].sys_name);
1897 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1899 return (m < n) ? 1 : (m > n) ? -1 : 0;
1902 static int (*sortfun)();
1903 static struct timeval overhead = { -1, -1 };
1909 if (strcmp(sortby, "time") == 0)
1911 else if (strcmp(sortby, "calls") == 0)
1912 sortfun = count_cmp;
1913 else if (strcmp(sortby, "name") == 0)
1914 sortfun = syscall_cmp;
1915 else if (strcmp(sortby, "nothing") == 0)
1918 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1923 void set_overhead(n)
1926 overhead.tv_sec = n / 1000000;
1927 overhead.tv_usec = n % 1000000;
1935 int call_cum, error_cum;
1936 struct timeval tv_cum, dtv;
1938 char *dashes = "-------------------------";
1941 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1942 if (overhead.tv_sec == -1) {
1943 tv_mul(&overhead, &shortest, 8);
1944 tv_div(&overhead, &overhead, 10);
1946 for (i = 0; i < nsyscalls; i++) {
1947 sorted_count[i] = i;
1948 if (call_count[i] == 0)
1950 tv_mul(&dtv, &overhead, call_count[i]);
1951 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1952 call_cum += call_count[i];
1953 error_cum += error_count[i];
1954 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1957 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1958 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1959 "% time", "seconds", "usecs/call",
1960 "calls", "errors", "syscall");
1961 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1962 dashes, dashes, dashes, dashes, dashes, dashes);
1963 for (i = 0; i < nsyscalls; i++) {
1964 j = sorted_count[i];
1965 if (call_count[j] == 0)
1967 tv_div(&dtv, &tv_count[j], call_count[j]);
1969 sprintf(error_str, "%d", error_count[j]);
1971 error_str[0] = '\0';
1972 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1973 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1974 percent, (long) tv_count[j].tv_sec,
1975 (long) tv_count[j].tv_usec,
1976 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1977 call_count[j], error_str, sysent[j].sys_name);
1979 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1980 dashes, dashes, dashes, dashes, dashes, dashes);
1982 sprintf(error_str, "%d", error_cum);
1984 error_str[0] = '\0';
1985 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1986 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1987 call_cum, error_str, "total");