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]);
424 enum subcall_style { shift_style, deref_style, mask_style, door_style };
426 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
434 const struct subcall subcalls_table[] = {
435 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
437 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
439 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
441 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
445 #if !(defined(LINUX) && ( defined(ALPHA) || defined(IA64) || defined(MIPS) ))
447 const int socket_map [] = {
450 /* SYS_CONNECT */ 98,
451 /* SYS_LISTEN */ 106,
453 /* SYS_GETSOCKNAME */ 150,
454 /* SYS_GETPEERNAME */ 141,
455 /* SYS_SOCKETPAIR */ 135,
458 /* SYS_SENDTO */ 133,
459 /* SYS_RECVFROM */ 125,
460 /* SYS_SHUTDOWN */ 134,
461 /* SYS_SETSOCKOPT */ 105,
462 /* SYS_GETSOCKOPT */ 118,
463 /* SYS_SENDMSG */ 114,
464 /* SYS_RECVMSG */ 113
468 sparc_socket_decode (tcp)
474 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
477 tcp->scno = socket_map [tcp->u_arg [0]-1];
478 n = tcp->u_nargs = sysent [tcp->scno].nargs;
479 addr = tcp->u_arg [1];
480 for (i = 0; i < n; i++){
482 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
484 tcp->u_arg [i] = arg;
485 addr += sizeof (arg);
490 decode_subcall(tcp, subcall, nsubcalls, style)
494 enum subcall_style style;
496 int i, addr, mask, arg;
500 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
502 tcp->scno = subcall + tcp->u_arg[0];
503 if (sysent[tcp->scno].nargs != -1)
504 tcp->u_nargs = sysent[tcp->scno].nargs;
507 for (i = 0; i < tcp->u_nargs; i++)
508 tcp->u_arg[i] = tcp->u_arg[i + 1];
511 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
513 tcp->scno = subcall + tcp->u_arg[0];
514 addr = tcp->u_arg[1];
515 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
516 if (umove(tcp, addr, &arg) < 0)
521 tcp->u_nargs = sysent[tcp->scno].nargs;
524 mask = (tcp->u_arg[0] >> 8) & 0xff;
525 for (i = 0; mask; i++)
529 tcp->u_arg[0] &= 0xff;
530 tcp->scno = subcall + i;
531 if (sysent[tcp->scno].nargs != -1)
532 tcp->u_nargs = sysent[tcp->scno].nargs;
536 * Oh, yuck. The call code is the *sixth* argument.
537 * (don't you mean the *last* argument? - JH)
539 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
541 tcp->scno = subcall + tcp->u_arg[5];
542 if (sysent[tcp->scno].nargs != -1)
543 tcp->u_nargs = sysent[tcp->scno].nargs;
549 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
550 if (subcalls_table[i].call == tcp->scno) break;
551 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
552 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
553 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
554 for (i = 0; i < tcp->u_nargs; i++)
555 tcp->u_arg[i] = tcp->u_arg[i + 1];
563 struct tcb *tcp_last = NULL;
566 internal_syscall(tcp)
570 * We must always trace a few critical system calls in order to
571 * correctly support following forks in the presence of tracing
574 switch (tcp->scno + NR_SYSCALL_BASE) {
643 #elif defined (POWERPC)
644 static long result,flags;
649 #elif defined (ALPHA)
652 #elif defined (SPARC)
653 static struct regs regs;
654 static unsigned long trap;
680 if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
682 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
686 #elif defined (POWERPC)
687 if (upeek(pid, 4*PT_R0, &scno) < 0)
689 if (!(tcp->flags & TCB_INSYSCALL)) {
690 /* Check if we return from execve. */
691 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
692 tcp->flags &= ~TCB_WAITEXECVE;
697 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
700 #define IA64_PSR_IS ((long)1 << 34)
701 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
702 ia32 = (psr & IA64_PSR_IS);
703 if (!(tcp->flags & TCB_INSYSCALL)) {
705 if (upeek(pid, PT_R8, &scno) < 0)
708 if (upeek (pid, PT_R15, &scno) < 0)
712 /* syscall in progress */
713 if (upeek (pid, PT_R8, &r8) < 0)
715 if (upeek (pid, PT_R10, &r10) < 0)
721 upeek(pid, 4*15, &pc);
722 umoven(tcp, pc-4, 4, (char *)&scno);
726 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
729 if (upeek(pid, REG_A3, &a3) < 0)
732 if(!(tcp->flags & TCB_INSYSCALL)) {
733 if (upeek(pid, REG_V0, &scno) < 0)
736 if (scno < 0 || scno > nsyscalls) {
737 if(a3 == 0 || a3 == -1) {
739 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
744 if (upeek(pid, REG_V0, &r2) < 0)
747 #elif defined (ALPHA)
748 if (upeek(pid, REG_A3, &a3) < 0)
751 if (!(tcp->flags & TCB_INSYSCALL)) {
752 if (upeek(pid, REG_R0, &scno) < 0)
755 /* Check if we return from execve. */
756 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
757 tcp->flags &= ~TCB_WAITEXECVE;
762 * Do some sanity checks to figure out if it's
763 * really a syscall entry
765 if (scno < 0 || scno > nsyscalls) {
766 if (a3 == 0 || a3 == -1) {
768 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
774 if (upeek(pid, REG_R0, &r0) < 0)
777 #elif defined (SPARC)
778 /* Everything we need is in the current register set. */
779 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
782 /* If we are entering, then disassemble the syscall trap. */
783 if (!(tcp->flags & TCB_INSYSCALL)) {
784 /* Retrieve the syscall trap instruction. */
786 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
790 /* Disassemble the trap to see what personality to use. */
793 /* Linux/SPARC syscall trap. */
797 /* Linux/SPARC64 syscall trap. */
798 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
801 /* SunOS syscall trap. (pers 1) */
802 fprintf(stderr,"syscall: SunOS no support\n");
805 /* Solaris 2.x syscall trap. (per 2) */
809 /* NetBSD/FreeBSD syscall trap. */
810 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
813 /* Solaris 2.x gettimeofday */
817 /* Unknown syscall trap. */
818 if(tcp->flags & TCB_WAITEXECVE) {
819 tcp->flags &= ~TCB_WAITEXECVE;
822 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
826 /* Extract the system call number from the registers. */
827 if (trap == 0x91d02027)
833 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
837 if (upeek(pid, PT_GR20, &scno) < 0)
839 if (!(tcp->flags & TCB_INSYSCALL)) {
840 /* Check if we return from execve. */
841 if ((tcp->flags & TCB_WAITEXECVE)) {
842 tcp->flags &= ~TCB_WAITEXECVE;
849 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
853 #ifdef HAVE_PR_SYSCALL
854 scno = tcp->status.PR_SYSCALL;
855 #else /* !HAVE_PR_SYSCALL */
857 scno = tcp->status.PR_WHAT;
859 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
863 switch (regs.r_eax) {
866 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
873 #endif /* !HAVE_PR_SYSCALL */
874 #endif /* USE_PROCFS */
875 if (!(tcp->flags & TCB_INSYSCALL))
887 #else /* USE_PROCFS */
888 int scno = tcp->scno;
890 if (!(tcp->flags & TCB_INSYSCALL)) {
891 if (tcp->status.PR_WHY != PR_SYSENTRY) {
896 #endif /* SYS_vfork */
899 #endif /* SYS_fork1 */
901 || scno == SYS_forkall
902 #endif /* SYS_forkall */
904 || scno == SYS_rfork1
905 #endif /* SYS_fork1 */
907 || scno == SYS_rforkall
908 #endif /* SYS_rforkall */
910 /* We are returning in the child, fake it. */
911 tcp->status.PR_WHY = PR_SYSENTRY;
913 tcp->status.PR_WHY = PR_SYSEXIT;
916 fprintf(stderr, "syscall: missing entry\n");
917 tcp->flags |= TCB_INSYSCALL;
922 if (tcp->status.PR_WHY != PR_SYSEXIT) {
923 fprintf(stderr, "syscall: missing exit\n");
924 tcp->flags &= ~TCB_INSYSCALL;
927 #endif /* USE_PROCFS */
929 if (!(tcp->flags & TCB_INSYSCALL)) {
931 fprintf(stderr, "syscall: missing entry\n");
932 tcp->flags |= TCB_INSYSCALL;
939 * This happens when a signal handler
940 * for a signal which interrupted a
941 * a system call makes another system call.
943 fprintf(stderr, "syscall: missing exit\n");
945 tcp->flags &= ~TCB_INSYSCALL;
951 if (upeek(pid, 4*EAX, &eax) < 0)
953 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
955 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
959 if (upeek(pid, PT_GPR2, &gpr2) < 0)
961 if (gpr2 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
963 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
966 #elif defined (POWERPC)
967 # define SO_MASK 0x10000000
968 if (upeek(pid, 4*PT_CCR, &flags) < 0)
970 if (upeek(pid, 4*PT_R3, &result) < 0)
975 if (upeek(pid, 4*PT_D0, &d0) < 0)
977 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
979 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
983 if (upeek(pid, 4*0, (long *)&r0) < 0)
985 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
987 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
991 if (upeek(pid, PT_GR28, &r28) < 0)
1005 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1015 if (eax < 0 && -eax < nerrnos) {
1029 if (err < 0 && -err < nerrnos) {
1057 if (result && (unsigned) -result < nerrnos) {
1062 tcp->u_rval = result;
1065 #else /* !POWERPC */
1067 if (d0 && (unsigned) -d0 < nerrnos) {
1077 if (r0 && (unsigned) -r0 < nerrnos) {
1097 if (regs.r_psr & PSR_C) {
1099 u_error = regs.r_o0;
1102 tcp->u_rval = regs.r_o0;
1107 if (r28 && (unsigned) -r28 < nerrnos) {
1120 #endif /* POWERPC */
1127 /* get error code from user struct */
1128 if (upeek(pid, uoff(u_error), &u_error) < 0)
1130 u_error >>= 24; /* u_error is a char */
1132 /* get system call return value */
1133 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1138 /* Judicious guessing goes a long way. */
1139 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1141 u_error = tcp->status.pr_reg[R_O0];
1144 tcp->u_rval = tcp->status.pr_reg[R_O0];
1149 /* Wanna know how to kill an hour single-stepping? */
1150 if (tcp->status.PR_REG[EFL] & 0x1) {
1152 u_error = tcp->status.PR_REG[EAX];
1155 tcp->u_rval = tcp->status.PR_REG[EAX];
1156 #ifdef HAVE_LONG_LONG
1158 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1159 tcp->status.PR_REG[EAX];
1165 if (tcp->status.pr_reg[CTX_A3]) {
1167 u_error = tcp->status.pr_reg[CTX_V0];
1170 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1176 if (regs.r_eflags & PSL_C) {
1178 u_error = regs.r_eax;
1180 tcp->u_rval = regs.r_eax;
1182 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1185 #endif /* FREEBSD */
1186 tcp->u_error = u_error;
1190 int syscall_enter(tcp)
1195 #endif /* !USE_PROCFS */
1200 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1201 tcp->u_nargs = sysent[tcp->scno].nargs;
1203 tcp->u_nargs = MAX_ARGS;
1204 for (i = 0; i < tcp->u_nargs; i++) {
1205 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
1209 #elif defined (ALPHA)
1212 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1213 tcp->u_nargs = sysent[tcp->scno].nargs;
1215 tcp->u_nargs = MAX_ARGS;
1216 for (i = 0; i < tcp->u_nargs; i++) {
1217 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1218 * for scno somewhere above here!
1220 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1224 #elif defined (IA64)
1226 unsigned long *bsp, cfm, i;
1228 if (upeek(pid, PT_AR_BSP, (long *) &bsp) < 0)
1230 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1233 bsp = ia64_rse_skip_regs(bsp, -(cfm & 0x7f));
1235 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1236 tcp->u_nargs = sysent[tcp->scno].nargs;
1238 tcp->u_nargs = MAX_ARGS;
1239 for (i = 0; i < tcp->u_nargs; ++i) {
1240 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(bsp, i), sizeof(long),
1241 (char *) &tcp->u_arg[i])
1246 #elif defined (MIPS)
1251 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1252 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1254 nargs = tcp->u_nargs = MAX_ARGS;
1256 if(upeek(pid, REG_SP, &sp) < 0)
1258 for(i = 0; i < 4; i++) {
1259 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1262 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1263 (char *)(tcp->u_arg + 4));
1265 for(i = 0; i < nargs; i++) {
1266 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1271 #elif defined (POWERPC)
1274 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1275 tcp->u_nargs = sysent[tcp->scno].nargs;
1277 tcp->u_nargs = MAX_ARGS;
1278 for (i = 0; i < tcp->u_nargs; i++) {
1279 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1283 #elif defined (SPARC)
1287 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1288 tcp->u_nargs = sysent[tcp->scno].nargs;
1290 tcp->u_nargs = MAX_ARGS;
1291 for (i = 0; i < tcp->u_nargs; i++)
1292 tcp->u_arg[i] = *((®s.r_o0) + i);
1294 #elif defined (HPPA)
1298 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1299 tcp->u_nargs = sysent[tcp->scno].nargs;
1301 tcp->u_nargs = MAX_ARGS;
1302 for (i = 0; i < tcp->u_nargs; i++) {
1303 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1307 #else /* Other architecture (like i386) (32bits specific) */
1310 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1311 tcp->u_nargs = sysent[tcp->scno].nargs;
1313 tcp->u_nargs = MAX_ARGS;
1314 for (i = 0; i < tcp->u_nargs; i++) {
1315 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1324 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1325 tcp->u_nargs = sysent[tcp->scno].nargs;
1327 tcp->u_nargs = MAX_ARGS;
1328 for (i = 0; i < tcp->u_nargs; i++) {
1331 if (upeek(pid, uoff(u_arg[0]) +
1332 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1340 * SGI is broken: even though it has pr_sysarg, it doesn't
1341 * set them on system call entry. Get a clue.
1343 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1344 tcp->u_nargs = sysent[tcp->scno].nargs;
1346 tcp->u_nargs = tcp->status.pr_nsysarg;
1347 if (tcp->u_nargs > 4) {
1348 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1349 4*sizeof(tcp->u_arg[0]));
1350 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1351 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1354 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1355 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1359 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
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 = tcp->status.pr_lwp.pr_nsysarg;
1365 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1366 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1367 #elif defined (HAVE_PR_SYSCALL)
1368 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1369 tcp->u_nargs = sysent[tcp->scno].nargs;
1371 tcp->u_nargs = tcp->status.pr_nsysarg;
1374 for (i = 0; i < tcp->u_nargs; i++)
1375 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1377 #elif defined (I386)
1378 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1379 tcp->u_nargs = sysent[tcp->scno].nargs;
1382 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1383 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1385 I DONT KNOW WHAT TO DO
1386 #endif /* !HAVE_PR_SYSCALL */
1389 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1390 sysent[tcp->scno].nargs > tcp->status.val)
1391 tcp->u_nargs = sysent[tcp->scno].nargs;
1393 tcp->u_nargs = tcp->status.val;
1394 if (tcp->u_nargs < 0)
1396 if (tcp->u_nargs > MAX_ARGS)
1397 tcp->u_nargs = MAX_ARGS;
1398 switch(regs.r_eax) {
1400 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1401 regs.r_esp + sizeof(int) + sizeof(quad_t));
1404 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1405 regs.r_esp + 2 * sizeof(int));
1408 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1409 regs.r_esp + sizeof(int));
1412 #endif /* FREEBSD */
1424 /* Measure the exit time as early as possible to avoid errors. */
1425 if (dtime && (tcp->flags & TCB_INSYSCALL))
1426 gettimeofday(&tv, NULL);
1428 res = get_scno(tcp);
1432 res = syscall_fixup(tcp);
1436 if (tcp->flags & TCB_INSYSCALL) {
1438 res = get_error(tcp);
1441 u_error = tcp->u_error;
1444 internal_syscall(tcp);
1445 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1446 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
1447 tcp->flags &= ~TCB_INSYSCALL;
1451 if (tcp->flags & TCB_REPRINT) {
1454 if (tcp->scno >= nsyscalls || tcp->scno < 0)
1455 tprintf("syscall_%lu", tcp->scno);
1457 tprintf("%s", sysent[tcp->scno].sys_name);
1458 tprintf(" resumed> ");
1461 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
1462 call_count[tcp->scno]++;
1464 error_count[tcp->scno]++;
1465 tv_sub(&tv, &tv, &tcp->etime);
1467 if (tv_cmp(&tv, &tcp->dtime) > 0) {
1468 static struct timeval one_tick =
1469 { 0, 1000000 / HZ };
1471 if (tv_nz(&tcp->dtime))
1473 else if (tv_cmp(&tv, &one_tick) > 0) {
1474 if (tv_cmp(&shortest, &one_tick) < 0)
1481 if (tv_cmp(&tv, &shortest) < 0)
1483 tv_add(&tv_count[tcp->scno],
1484 &tv_count[tcp->scno], &tv);
1485 tcp->flags &= ~TCB_INSYSCALL;
1489 if (tcp->scno >= nsyscalls || tcp->scno < 0
1490 || (qual_flags[tcp->scno] & QUAL_RAW))
1491 sys_res = printargs(tcp);
1493 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1494 u_error = tcp->u_error;
1497 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
1498 qual_flags[tcp->scno] & QUAL_RAW) {
1500 tprintf("= -1 (errno %ld)", u_error);
1502 tprintf("= %#lx", tcp->u_rval);
1504 else if (!(sys_res & RVAL_NONE) && u_error) {
1508 tprintf("= ? ERESTARTSYS (To be restarted)");
1510 case ERESTARTNOINTR:
1511 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1513 case ERESTARTNOHAND:
1514 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1519 if (u_error < nerrnos && u_error < sys_nerr)
1520 tprintf("%s (%s)", errnoent[u_error],
1521 sys_errlist[u_error]);
1522 else if (u_error < nerrnos)
1523 tprintf("%s (errno %ld)",
1524 errnoent[u_error], u_error);
1525 else if (u_error < sys_nerr)
1526 tprintf("ERRNO_%ld (%s)", u_error,
1527 sys_errlist[u_error]);
1529 tprintf("E??? (errno %ld)", u_error);
1534 if (sys_res & RVAL_NONE)
1537 switch (sys_res & RVAL_MASK) {
1539 tprintf("= %#lx", tcp->u_rval);
1542 tprintf("= %#lo", tcp->u_rval);
1545 tprintf("= %lu", tcp->u_rval);
1548 tprintf("= %ld", tcp->u_rval);
1550 #ifdef HAVE_LONG_LONG
1552 tprintf("= %#llx", tcp->u_lrval);
1555 tprintf("= %#llo", tcp->u_lrval);
1557 case RVAL_LUDECIMAL:
1558 tprintf("= %llu", tcp->u_lrval);
1561 tprintf("= %lld", tcp->u_lrval);
1566 "invalid rval format\n");
1570 if ((sys_res & RVAL_STR) && tcp->auxstr)
1571 tprintf(" (%s)", tcp->auxstr);
1574 tv_sub(&tv, &tv, &tcp->etime);
1575 tprintf(" <%ld.%06ld>",
1576 (long) tv.tv_sec, (long) tv.tv_usec);
1581 if (fflush(tcp->outf) == EOF)
1583 tcp->flags &= ~TCB_INSYSCALL;
1587 /* Entering system call */
1588 res = syscall_enter(tcp);
1592 switch (tcp->scno + NR_SYSCALL_BASE) {
1594 #if !defined (ALPHA) && !defined(IA64) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA)
1595 case SYS_socketcall:
1596 decode_subcall(tcp, SYS_socket_subcall,
1597 SYS_socket_nsubcalls, deref_style);
1600 decode_subcall(tcp, SYS_ipc_subcall,
1601 SYS_ipc_nsubcalls, shift_style);
1603 #endif /* !ALPHA && !IA64 && !MIPS && !SPARC */
1605 case SYS_socketcall:
1606 sparc_socket_decode (tcp);
1611 #ifdef SYS_pgrpsys_subcall
1613 decode_subcall(tcp, SYS_pgrpsys_subcall,
1614 SYS_pgrpsys_nsubcalls, shift_style);
1616 #endif /* SYS_pgrpsys_subcall */
1617 #ifdef SYS_sigcall_subcall
1619 decode_subcall(tcp, SYS_sigcall_subcall,
1620 SYS_sigcall_nsubcalls, mask_style);
1622 #endif /* SYS_sigcall_subcall */
1624 decode_subcall(tcp, SYS_msgsys_subcall,
1625 SYS_msgsys_nsubcalls, shift_style);
1628 decode_subcall(tcp, SYS_shmsys_subcall,
1629 SYS_shmsys_nsubcalls, shift_style);
1632 decode_subcall(tcp, SYS_semsys_subcall,
1633 SYS_semsys_nsubcalls, shift_style);
1637 decode_subcall(tcp, SYS_utssys_subcall,
1638 SYS_utssys_nsubcalls, shift_style);
1642 decode_subcall(tcp, SYS_sysfs_subcall,
1643 SYS_sysfs_nsubcalls, shift_style);
1646 decode_subcall(tcp, SYS_spcall_subcall,
1647 SYS_spcall_nsubcalls, shift_style);
1649 #ifdef SYS_context_subcall
1651 decode_subcall(tcp, SYS_context_subcall,
1652 SYS_context_nsubcalls, shift_style);
1654 #endif /* SYS_context_subcall */
1655 #ifdef SYS_door_subcall
1657 decode_subcall(tcp, SYS_door_subcall,
1658 SYS_door_nsubcalls, door_style);
1660 #endif /* SYS_door_subcall */
1661 #ifdef SYS_kaio_subcall
1663 decode_subcall(tcp, SYS_kaio_subcall,
1664 SYS_kaio_nsubcalls, shift_style);
1672 decode_subcall(tcp, 0, 0, table_style);
1677 decode_subcall(tcp, SYS_semsys_subcall,
1678 SYS_semsys_nsubcalls, shift_style);
1681 decode_subcall(tcp, SYS_msgsys_subcall,
1682 SYS_msgsys_nsubcalls, shift_style);
1685 decode_subcall(tcp, SYS_shmsys_subcall,
1686 SYS_shmsys_nsubcalls, shift_style);
1691 internal_syscall(tcp);
1692 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
1693 tcp->flags |= TCB_INSYSCALL;
1698 gettimeofday(&tcp->etime, NULL);
1699 tcp->flags |= TCB_INSYSCALL;
1704 tcp->flags &= ~TCB_REPRINT;
1706 if (tcp->scno >= nsyscalls || tcp->scno < 0)
1707 tprintf("syscall_%lu(", tcp->scno);
1709 tprintf("%s(", sysent[tcp->scno].sys_name);
1710 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
1711 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1712 sys_res = printargs(tcp);
1714 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1715 if (fflush(tcp->outf) == EOF)
1717 tcp->flags |= TCB_INSYSCALL;
1718 /* Measure the entrance time as late as possible to avoid errors. */
1720 gettimeofday(&tcp->etime, NULL);
1728 if (entering(tcp)) {
1731 for (i = 0; i < tcp->u_nargs; i++)
1732 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1746 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
1753 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1759 val = tcp->status.PR_REG[R_O1];
1762 val = tcp->status.PR_REG[EDX];
1765 val = tcp->status.PR_REG[CTX_V1];
1770 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
1777 * Apparently, indirect system calls have already be converted by ptrace(2),
1778 * so if you see "indir" this program has gone astray.
1786 if (entering(tcp)) {
1787 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1788 fprintf(stderr, "Bogus syscall: %u\n", scno);
1791 nargs = sysent[scno].nargs;
1792 tprintf("%s", sysent[scno].sys_name);
1793 for (i = 0; i < nargs; i++)
1794 tprintf(", %#lx", tcp->u_arg[i+1]);
1804 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1812 return strcmp(sysent[*((int *) a)].sys_name,
1813 sysent[*((int *) b)].sys_name);
1821 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1823 return (m < n) ? 1 : (m > n) ? -1 : 0;
1826 static int (*sortfun)();
1827 static struct timeval overhead = { -1, -1 };
1833 if (strcmp(sortby, "time") == 0)
1835 else if (strcmp(sortby, "calls") == 0)
1836 sortfun = count_cmp;
1837 else if (strcmp(sortby, "name") == 0)
1838 sortfun = syscall_cmp;
1839 else if (strcmp(sortby, "nothing") == 0)
1842 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1847 void set_overhead(n)
1850 overhead.tv_sec = n / 1000000;
1851 overhead.tv_usec = n % 1000000;
1859 int call_cum, error_cum;
1860 struct timeval tv_cum, dtv;
1862 char *dashes = "-------------------------";
1865 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1866 if (overhead.tv_sec == -1) {
1867 tv_mul(&overhead, &shortest, 8);
1868 tv_div(&overhead, &overhead, 10);
1870 for (i = 0; i < nsyscalls; i++) {
1871 sorted_count[i] = i;
1872 if (call_count[i] == 0)
1874 tv_mul(&dtv, &overhead, call_count[i]);
1875 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1876 call_cum += call_count[i];
1877 error_cum += error_count[i];
1878 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1881 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1882 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1883 "% time", "seconds", "usecs/call",
1884 "calls", "errors", "syscall");
1885 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1886 dashes, dashes, dashes, dashes, dashes, dashes);
1887 for (i = 0; i < nsyscalls; i++) {
1888 j = sorted_count[i];
1889 if (call_count[j] == 0)
1891 tv_div(&dtv, &tv_count[j], call_count[j]);
1893 sprintf(error_str, "%d", error_count[j]);
1895 error_str[0] = '\0';
1896 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1897 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1898 percent, (long) tv_count[j].tv_sec,
1899 (long) tv_count[j].tv_usec,
1900 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1901 call_count[j], error_str, sysent[j].sys_name);
1903 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1904 dashes, dashes, dashes, dashes, dashes, dashes);
1906 sprintf(error_str, "%d", error_cum);
1908 error_str[0] = '\0';
1909 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1910 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1911 call_cum, error_str, "total");