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 #define NR_SYSCALL_BASE 0
77 #define ERESTARTSYS 512
79 #ifndef ERESTARTNOINTR
80 #define ERESTARTNOINTR 513
82 #ifndef ERESTARTNOHAND
83 #define ERESTARTNOHAND 514 /* restart if no handler.. */
86 #define ENOIOCTLCMD 515 /* No ioctl command */
88 #ifndef ERESTART_RESTARTBLOCK
89 #define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
97 #undef NR_SYSCALL_BASE
98 #define NR_SYSCALL_BASE __NR_SYSCALL_BASE
104 /* Define these shorthand notations to simplify the syscallent files. */
105 #define TF TRACE_FILE
107 #define TN TRACE_NETWORK
108 #define TP TRACE_PROCESS
109 #define TS TRACE_SIGNAL
111 struct sysent sysent0[] = {
112 #include "syscallent.h"
114 int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
116 #if SUPPORTED_PERSONALITIES >= 2
117 struct sysent sysent1[] = {
118 #include "syscallent1.h"
120 int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
121 #endif /* SUPPORTED_PERSONALITIES >= 2 */
123 #if SUPPORTED_PERSONALITIES >= 3
124 struct sysent sysent2[] = {
125 #include "syscallent2.h"
127 int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
128 #endif /* SUPPORTED_PERSONALITIES >= 3 */
130 struct sysent *sysent;
133 /* Now undef them since short defines cause wicked namespace pollution. */
140 char *errnoent0[] = {
141 #include "errnoent.h"
143 int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
145 #if SUPPORTED_PERSONALITIES >= 2
146 char *errnoent1[] = {
147 #include "errnoent1.h"
149 int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
150 #endif /* SUPPORTED_PERSONALITIES >= 2 */
152 #if SUPPORTED_PERSONALITIES >= 3
153 char *errnoent2[] = {
154 #include "errnoent2.h"
156 int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
157 #endif /* SUPPORTED_PERSONALITIES >= 3 */
162 int current_personality;
165 set_personality(personality)
168 switch (personality) {
170 errnoent = errnoent0;
173 nsyscalls = nsyscalls0;
174 ioctlent = ioctlent0;
175 nioctlents = nioctlents0;
176 signalent = signalent0;
177 nsignals = nsignals0;
180 #if SUPPORTED_PERSONALITIES >= 2
182 errnoent = errnoent1;
185 nsyscalls = nsyscalls1;
186 ioctlent = ioctlent1;
187 nioctlents = nioctlents1;
188 signalent = signalent1;
189 nsignals = nsignals1;
191 #endif /* SUPPORTED_PERSONALITIES >= 2 */
193 #if SUPPORTED_PERSONALITIES >= 3
195 errnoent = errnoent2;
198 nsyscalls = nsyscalls2;
199 ioctlent = ioctlent2;
200 nioctlents = nioctlents2;
201 signalent = signalent2;
202 nsignals = nsignals2;
204 #endif /* SUPPORTED_PERSONALITIES >= 3 */
210 current_personality = personality;
214 int qual_flags[MAX_QUALS];
216 static int call_count[MAX_QUALS];
217 static int error_count[MAX_QUALS];
218 static struct timeval tv_count[MAX_QUALS];
219 static int sorted_count[MAX_QUALS];
221 static struct timeval shortest = { 1000000, 0 };
223 static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
225 static struct qual_options {
231 { QUAL_TRACE, "trace", qual_syscall, "system call" },
232 { QUAL_TRACE, "t", qual_syscall, "system call" },
233 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
234 { QUAL_ABBREV, "a", qual_syscall, "system call" },
235 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
236 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
237 { QUAL_RAW, "raw", qual_syscall, "system call" },
238 { QUAL_RAW, "x", qual_syscall, "system call" },
239 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
240 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
241 { QUAL_SIGNAL, "s", qual_signal, "signal" },
242 { QUAL_FAULT, "fault", qual_fault, "fault" },
243 { QUAL_FAULT, "faults", qual_fault, "fault" },
244 { QUAL_FAULT, "m", qual_fault, "fault" },
245 { QUAL_READ, "read", qual_desc, "descriptor" },
246 { QUAL_READ, "reads", qual_desc, "descriptor" },
247 { QUAL_READ, "r", qual_desc, "descriptor" },
248 { QUAL_WRITE, "write", qual_desc, "descriptor" },
249 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
250 { QUAL_WRITE, "w", qual_desc, "descriptor" },
251 { 0, NULL, NULL, NULL },
255 qualify_one(n, opt, not)
257 struct qual_options *opt;
261 qual_flags[n] &= ~opt->bitflag;
263 qual_flags[n] |= opt->bitflag;
267 qual_syscall(s, opt, not)
269 struct qual_options *opt;
275 for (i = 0; i < nsyscalls; i++) {
276 if (strcmp(s, sysent[i].sys_name) == 0) {
277 qualify_one(i, opt, not);
285 qual_signal(s, opt, not)
287 struct qual_options *opt;
293 if (s && *s && isdigit((unsigned char)*s)) {
294 qualify_one(atoi(s), opt, not);
299 for (i = 0; s[i]; i++)
300 s[i] = toupper((unsigned char)(s[i]));
301 if (strncmp(s, "SIG", 3) == 0)
303 for (i = 0; i <= NSIG; i++)
304 if (strcmp(s, signame(i) + 3) == 0) {
305 qualify_one(atoi(s), opt, not);
312 qual_fault(s, opt, not)
314 struct qual_options *opt;
321 qual_desc(s, opt, not)
323 struct qual_options *opt;
326 if (s && *s && isdigit((unsigned char)*s)) {
327 qualify_one(atoi(s), opt, not);
337 if (strcmp(s, "file") == 0)
339 if (strcmp(s, "ipc") == 0)
341 if (strcmp(s, "network") == 0)
342 return TRACE_NETWORK;
343 if (strcmp(s, "process") == 0)
344 return TRACE_PROCESS;
345 if (strcmp(s, "signal") == 0)
354 struct qual_options *opt;
359 opt = &qual_options[0];
360 for (i = 0; (p = qual_options[i].option_name); i++) {
362 if (strncmp(s, p, n) == 0 && s[n] == '=') {
363 opt = &qual_options[i];
373 if (strcmp(s, "none") == 0) {
377 if (strcmp(s, "all") == 0) {
378 for (i = 0; i < MAX_QUALS; i++) {
380 qual_flags[i] &= ~opt->bitflag;
382 qual_flags[i] |= opt->bitflag;
386 for (i = 0; i < MAX_QUALS; i++) {
388 qual_flags[i] |= opt->bitflag;
390 qual_flags[i] &= ~opt->bitflag;
392 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
393 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
394 for (i = 0; i < MAX_QUALS; i++) {
395 if (sysent[i].sys_flags & n) {
397 qual_flags[i] &= ~opt->bitflag;
399 qual_flags[i] |= opt->bitflag;
404 if (opt->qualify(p, opt, not)) {
405 fprintf(stderr, "strace: invalid %s `%s'\n",
406 opt->argument_name, p);
419 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
421 switch (tcp->scno + NR_SYSCALL_BASE) {
429 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
430 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
439 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
440 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
444 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
445 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
451 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
452 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
459 enum subcall_style { shift_style, deref_style, mask_style, door_style };
461 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
469 const struct subcall subcalls_table[] = {
470 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
472 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
474 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
476 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
480 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
482 const int socket_map [] = {
485 /* SYS_CONNECT */ 98,
486 /* SYS_LISTEN */ 106,
488 /* SYS_GETSOCKNAME */ 150,
489 /* SYS_GETPEERNAME */ 141,
490 /* SYS_SOCKETPAIR */ 135,
493 /* SYS_SENDTO */ 133,
494 /* SYS_RECVFROM */ 125,
495 /* SYS_SHUTDOWN */ 134,
496 /* SYS_SETSOCKOPT */ 105,
497 /* SYS_GETSOCKOPT */ 118,
498 /* SYS_SENDMSG */ 114,
499 /* SYS_RECVMSG */ 113
503 sparc_socket_decode (tcp)
509 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
512 tcp->scno = socket_map [tcp->u_arg [0]-1];
513 n = tcp->u_nargs = sysent [tcp->scno].nargs;
514 addr = tcp->u_arg [1];
515 for (i = 0; i < n; i++){
517 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
519 tcp->u_arg [i] = arg;
520 addr += sizeof (arg);
525 decode_subcall(tcp, subcall, nsubcalls, style)
529 enum subcall_style style;
531 long addr, mask, arg;
536 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
538 tcp->scno = subcall + tcp->u_arg[0];
539 if (sysent[tcp->scno].nargs != -1)
540 tcp->u_nargs = sysent[tcp->scno].nargs;
543 for (i = 0; i < tcp->u_nargs; i++)
544 tcp->u_arg[i] = tcp->u_arg[i + 1];
547 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
549 tcp->scno = subcall + tcp->u_arg[0];
550 addr = tcp->u_arg[1];
551 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
552 if (umove(tcp, addr, &arg) < 0)
557 tcp->u_nargs = sysent[tcp->scno].nargs;
560 mask = (tcp->u_arg[0] >> 8) & 0xff;
561 for (i = 0; mask; i++)
565 tcp->u_arg[0] &= 0xff;
566 tcp->scno = subcall + i;
567 if (sysent[tcp->scno].nargs != -1)
568 tcp->u_nargs = sysent[tcp->scno].nargs;
572 * Oh, yuck. The call code is the *sixth* argument.
573 * (don't you mean the *last* argument? - JH)
575 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
577 tcp->scno = subcall + tcp->u_arg[5];
578 if (sysent[tcp->scno].nargs != -1)
579 tcp->u_nargs = sysent[tcp->scno].nargs;
585 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
586 if (subcalls_table[i].call == tcp->scno) break;
587 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
588 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
589 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
590 for (i = 0; i < tcp->u_nargs; i++)
591 tcp->u_arg[i] = tcp->u_arg[i + 1];
599 struct tcb *tcp_last = NULL;
602 internal_syscall(tcp)
606 * We must always trace a few critical system calls in order to
607 * correctly support following forks in the presence of tracing
610 switch (tcp->scno + NR_SYSCALL_BASE) {
680 #ifdef __NR_exit_group
681 case __NR_exit_group:
684 case 252: /* IA-32 __NR_exit_group */
699 #elif defined (POWERPC)
700 static long result,flags;
704 static struct pt_regs regs;
705 #elif defined (ALPHA)
708 #elif defined (SPARC)
709 static struct regs regs;
710 static unsigned long trap;
714 #elif defined(S390) || defined(S390X)
717 static long syscall_mode;
724 #elif defined(X86_64)
742 #if defined(S390) || defined(S390X)
743 if (tcp->flags & TCB_WAITEXECVE) {
745 * When the execve system call completes successfully, the
746 * new process still has -ENOSYS (old style) or __NR_execve
747 * (new style) in gpr2. We cannot recover the scno again
748 * by disassembly, because the image that executed the
749 * syscall is gone now. Fortunately, we don't want it. We
750 * leave the flag set so that syscall_fixup can fake the
753 if (tcp->flags & TCB_INSYSCALL)
756 * This is the SIGTRAP after execve. We cannot try to read
757 * the system call here either.
759 tcp->flags &= ~TCB_WAITEXECVE;
763 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
766 if (syscall_mode != -ENOSYS) {
768 * Since kernel version 2.5.44 the scno gets passed in gpr2.
773 * Old style of "passing" the scno via the SVC instruction.
776 long opcode, offset_reg, tmp;
778 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
779 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
780 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
781 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
783 if (upeek(pid, PT_PSWADDR, &pc) < 0)
786 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
788 perror("peektext(pc-oneword)");
793 * We have to check if the SVC got executed directly or via an
794 * EXECUTE instruction. In case of EXECUTE it is necessary to do
795 * instruction decoding to derive the system call number.
796 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
797 * so that this doesn't work if a SVC opcode is part of an EXECUTE
798 * opcode. Since there is no way to find out the opcode size this
799 * is the best we can do...
802 if ((opcode & 0xff00) == 0x0a00) {
804 scno = opcode & 0xff;
807 /* SVC got executed by EXECUTE instruction */
810 * Do instruction decoding of EXECUTE. If you really want to
811 * understand this, read the Principles of Operations.
813 svc_addr = (void *) (opcode & 0xfff);
816 offset_reg = (opcode & 0x000f0000) >> 16;
817 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
822 offset_reg = (opcode & 0x0000f000) >> 12;
823 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
827 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
836 offset_reg = (opcode & 0x00f00000) >> 20;
837 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
840 scno = (scno | tmp) & 0xff;
843 #elif defined (POWERPC)
844 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
846 if (!(tcp->flags & TCB_INSYSCALL)) {
847 /* Check if we return from execve. */
848 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
849 tcp->flags &= ~TCB_WAITEXECVE;
854 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
856 #elif defined (X86_64)
857 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
860 if (!(tcp->flags & TCB_INSYSCALL)) {
861 static int currpers=-1;
864 /* Check CS register value. On x86-64 linux it is:
865 * 0x33 for long mode (64 bit)
866 * 0x23 for compatibility mode (32 bit)
867 * It takes only one ptrace and thus doesn't need
870 if (upeek(pid, 8*CS, &val) < 0)
874 case 0x23: currpers = 1; break;
875 case 0x33: currpers = 0; break;
877 fprintf(stderr, "Unknown value CS=0x%02X while "
878 "detecting personality of process "
879 "PID=%d\n", (int)val, pid);
880 currpers = current_personality;
884 /* This version analyzes the opcode of a syscall instruction.
885 * (int 0x80 on i386 vs. syscall on x86-64)
886 * It works, but is too complicated.
888 unsigned long val, rip, i;
890 if(upeek(pid, 8*RIP, &rip)<0)
891 perror("upeek(RIP)");
893 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
897 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
899 printf("ptrace_peektext failed: %s\n",
901 switch (call & 0xffff)
903 /* x86-64: syscall = 0x0f 0x05 */
904 case 0x050f: currpers = 0; break;
905 /* i386: int 0x80 = 0xcd 0x80 */
906 case 0x80cd: currpers = 1; break;
908 currpers = current_personality;
910 "Unknown syscall opcode (0x%04X) while "
911 "detecting personality of process "
912 "PID=%d\n", (int)call, pid);
916 if(currpers != current_personality)
918 char *names[]={"64 bit", "32 bit"};
919 set_personality(currpers);
920 printf("[ Process PID=%d runs in %s mode. ]\n",
921 pid, names[current_personality]);
925 # define IA64_PSR_IS ((long)1 << 34)
926 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
927 ia32 = (psr & IA64_PSR_IS) != 0;
928 if (!(tcp->flags & TCB_INSYSCALL)) {
930 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
933 if (upeek (pid, PT_R15, &scno) < 0)
936 /* Check if we return from execve. */
937 if (tcp->flags & TCB_WAITEXECVE) {
938 tcp->flags &= ~TCB_WAITEXECVE;
942 /* syscall in progress */
943 if (upeek (pid, PT_R8, &r8) < 0)
945 if (upeek (pid, PT_R10, &r10) < 0)
950 * Read complete register set in one go.
952 if (ptrace(PTRACE_GETREGS, pid, NULL, (void *)®s) == -1)
956 * We only need to grab the syscall number on syscall entry.
958 if (regs.ARM_ip == 0) {
960 * Note: we only deal with only 32-bit CPUs here.
962 if (regs.ARM_cpsr & 0x20) {
964 * Get the Thumb-mode system call number
969 * Get the ARM-mode system call number
972 scno = ptrace(PTRACE_PEEKTEXT, pid, (void *)(regs.ARM_pc - 4), NULL);
976 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
977 tcp->flags &= ~TCB_WAITEXECVE;
981 if ((scno & 0x0ff00000) != 0x0f900000) {
982 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
988 * Fixup the syscall number
993 if (tcp->flags & TCB_INSYSCALL) {
994 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
995 tcp->flags &= ~TCB_INSYSCALL;
998 if (!(tcp->flags & TCB_INSYSCALL)) {
999 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1000 tcp->flags |= TCB_INSYSCALL;
1003 #elif defined (M68K)
1004 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
1006 #elif defined (MIPS)
1007 if (upeek(pid, REG_A3, &a3) < 0)
1010 if(!(tcp->flags & TCB_INSYSCALL)) {
1011 if (upeek(pid, REG_V0, &scno) < 0)
1014 if (scno < 0 || scno > nsyscalls) {
1015 if(a3 == 0 || a3 == -1) {
1017 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
1022 if (upeek(pid, REG_V0, &r2) < 0)
1025 #elif defined (ALPHA)
1026 if (upeek(pid, REG_A3, &a3) < 0)
1029 if (!(tcp->flags & TCB_INSYSCALL)) {
1030 if (upeek(pid, REG_R0, &scno) < 0)
1033 /* Check if we return from execve. */
1034 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1035 tcp->flags &= ~TCB_WAITEXECVE;
1040 * Do some sanity checks to figure out if it's
1041 * really a syscall entry
1043 if (scno < 0 || scno > nsyscalls) {
1044 if (a3 == 0 || a3 == -1) {
1046 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
1052 if (upeek(pid, REG_R0, &r0) < 0)
1055 #elif defined (SPARC)
1056 /* Everything we need is in the current register set. */
1057 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
1060 /* If we are entering, then disassemble the syscall trap. */
1061 if (!(tcp->flags & TCB_INSYSCALL)) {
1062 /* Retrieve the syscall trap instruction. */
1064 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
1068 /* Disassemble the trap to see what personality to use. */
1071 /* Linux/SPARC syscall trap. */
1075 /* Linux/SPARC64 syscall trap. */
1076 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
1079 /* SunOS syscall trap. (pers 1) */
1080 fprintf(stderr,"syscall: SunOS no support\n");
1083 /* Solaris 2.x syscall trap. (per 2) */
1087 /* NetBSD/FreeBSD syscall trap. */
1088 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1091 /* Solaris 2.x gettimeofday */
1095 /* Unknown syscall trap. */
1096 if(tcp->flags & TCB_WAITEXECVE) {
1097 tcp->flags &= ~TCB_WAITEXECVE;
1100 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
1104 /* Extract the system call number from the registers. */
1105 if (trap == 0x91d02027)
1111 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
1115 if (upeek(pid, PT_GR20, &scno) < 0)
1117 if (!(tcp->flags & TCB_INSYSCALL)) {
1118 /* Check if we return from execve. */
1119 if ((tcp->flags & TCB_WAITEXECVE)) {
1120 tcp->flags &= ~TCB_WAITEXECVE;
1126 * In the new syscall ABI, the system call number is in R3.
1128 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1132 /* Odd as it may seem, a glibc bug has been known to cause
1133 glibc to issue bogus negative syscall numbers. So for
1134 our purposes, make strace print what it *should* have been */
1135 long correct_scno = (scno & 0xff);
1138 "Detected glibc bug: bogus system call number = %ld, "
1139 "correcting to %ld\n",
1142 scno = correct_scno;
1146 if (!(tcp->flags & TCB_INSYSCALL)) {
1147 /* Check if we return from execve. */
1148 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1149 tcp->flags &= ~TCB_WAITEXECVE;
1154 if (upeek(pid, REG_SYSCALL, &scno) < 0)
1158 if (!(tcp->flags & TCB_INSYSCALL)) {
1159 /* Check if we return from execve. */
1160 if (tcp->flags & TCB_WAITEXECVE) {
1161 tcp->flags &= ~TCB_WAITEXECVE;
1168 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1171 /* new syscall ABI returns result in R0 */
1172 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1175 /* ABI defines result returned in r9 */
1176 if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
1181 #ifdef HAVE_PR_SYSCALL
1182 scno = tcp->status.PR_SYSCALL;
1183 #else /* !HAVE_PR_SYSCALL */
1185 scno = tcp->status.PR_WHAT;
1187 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1191 switch (regs.r_eax) {
1194 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1200 #endif /* FREEBSD */
1201 #endif /* !HAVE_PR_SYSCALL */
1202 #endif /* USE_PROCFS */
1203 if (!(tcp->flags & TCB_INSYSCALL))
1215 #else /* USE_PROCFS */
1216 int scno = tcp->scno;
1218 if (!(tcp->flags & TCB_INSYSCALL)) {
1219 if (tcp->status.PR_WHY != PR_SYSENTRY) {
1223 || scno == SYS_vfork
1224 #endif /* SYS_vfork */
1226 || scno == SYS_fork1
1227 #endif /* SYS_fork1 */
1229 || scno == SYS_forkall
1230 #endif /* SYS_forkall */
1232 || scno == SYS_rfork1
1233 #endif /* SYS_fork1 */
1235 || scno == SYS_rforkall
1236 #endif /* SYS_rforkall */
1238 /* We are returning in the child, fake it. */
1239 tcp->status.PR_WHY = PR_SYSENTRY;
1241 tcp->status.PR_WHY = PR_SYSEXIT;
1244 fprintf(stderr, "syscall: missing entry\n");
1245 tcp->flags |= TCB_INSYSCALL;
1250 if (tcp->status.PR_WHY != PR_SYSEXIT) {
1251 fprintf(stderr, "syscall: missing exit\n");
1252 tcp->flags &= ~TCB_INSYSCALL;
1255 #endif /* USE_PROCFS */
1257 if (!(tcp->flags & TCB_INSYSCALL)) {
1259 fprintf(stderr, "syscall: missing entry\n");
1260 tcp->flags |= TCB_INSYSCALL;
1267 * This happens when a signal handler
1268 * for a signal which interrupted a
1269 * a system call makes another system call.
1271 fprintf(stderr, "syscall: missing exit\n");
1273 tcp->flags &= ~TCB_INSYSCALL;
1279 if (upeek(pid, 4*EAX, &eax) < 0)
1281 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1283 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1286 #elif defined (X86_64)
1287 if (upeek(pid, 8*RAX, &rax) < 0)
1289 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1291 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1294 #elif defined (S390) || defined (S390X)
1295 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1297 if (syscall_mode != -ENOSYS)
1298 syscall_mode = tcp->scno;
1299 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
1301 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1304 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1305 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1306 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1308 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1309 * flag set for the post-execve SIGTRAP to see and reset.
1313 #elif defined (POWERPC)
1314 # define SO_MASK 0x10000000
1315 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1317 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
1319 if (flags & SO_MASK)
1321 #elif defined (M68K)
1322 if (upeek(pid, 4*PT_D0, &d0) < 0)
1324 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1326 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1333 #elif defined (HPPA)
1334 if (upeek(pid, PT_GR28, &r28) < 0)
1337 if (upeek(pid, PT_R10, &r10) < 0)
1339 if (upeek(pid, PT_R8, &r8) < 0)
1341 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1343 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1357 #if defined(S390) || defined(S390X)
1358 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1366 #else /* !S390 && !S390X */
1368 if (eax < 0 && -eax < nerrnos) {
1378 if (rax < 0 && -rax < nerrnos) {
1392 if (err < 0 && -err < nerrnos) {
1420 if (result && (unsigned long) -result < nerrnos) {
1425 tcp->u_rval = result;
1428 #else /* !POWERPC */
1430 if (d0 && (unsigned) -d0 < nerrnos) {
1440 if (regs.ARM_r0 && (unsigned) -regs.ARM_r0 < nerrnos) {
1442 u_error = -regs.ARM_r0;
1445 tcp->u_rval = regs.ARM_r0;
1460 if (regs.r_psr & PSR_C) {
1462 u_error = regs.r_o0;
1465 tcp->u_rval = regs.r_o0;
1470 if (r28 && (unsigned) -r28 < nerrnos) {
1480 /* interpret R0 as return value or error number */
1481 if (r0 && (unsigned) -r0 < nerrnos) {
1491 /* interpret result as return value or error number */
1492 if (r9 && (unsigned) -r9 < nerrnos) {
1507 #endif /* POWERPC */
1512 #endif /* S390 || S390X */
1515 /* get error code from user struct */
1516 if (upeek(pid, uoff(u_error), &u_error) < 0)
1518 u_error >>= 24; /* u_error is a char */
1520 /* get system call return value */
1521 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1526 /* Judicious guessing goes a long way. */
1527 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1529 u_error = tcp->status.pr_reg[R_O0];
1532 tcp->u_rval = tcp->status.pr_reg[R_O0];
1537 /* Wanna know how to kill an hour single-stepping? */
1538 if (tcp->status.PR_REG[EFL] & 0x1) {
1540 u_error = tcp->status.PR_REG[EAX];
1543 tcp->u_rval = tcp->status.PR_REG[EAX];
1544 #ifdef HAVE_LONG_LONG
1546 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1547 tcp->status.PR_REG[EAX];
1553 /* Wanna know how to kill an hour single-stepping? */
1554 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1556 u_error = tcp->status.PR_REG[RAX];
1559 tcp->u_rval = tcp->status.PR_REG[RAX];
1564 if (tcp->status.pr_reg[CTX_A3]) {
1566 u_error = tcp->status.pr_reg[CTX_V0];
1569 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1575 if (regs.r_eflags & PSL_C) {
1577 u_error = regs.r_eax;
1579 tcp->u_rval = regs.r_eax;
1581 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1584 #endif /* FREEBSD */
1585 tcp->u_error = u_error;
1590 force_result(tcp, error, rval)
1596 #if defined(S390) || defined(S390X)
1597 gpr2 = error ? -error : rval;
1598 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1600 #else /* !S390 && !S390X */
1602 eax = error ? -error : rval;
1603 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1607 rax = error ? -error : rval;
1608 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 4), rax) < 0)
1613 r8 = error ? -error : rval;
1614 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1626 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1627 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1640 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1641 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1645 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1655 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1656 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
1658 #else /* !POWERPC */
1660 d0 = error ? -error : rval;
1661 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1665 regs.ARM_r0 = error ? -error : rval;
1666 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), regs.ARM_r0) < 0)
1678 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1679 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1683 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1686 regs.r_psr |= PSR_C;
1690 regs.r_psr &= ~PSR_C;
1693 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1697 r28 = error ? -error : rval;
1698 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1702 r0 = error ? -error : rval;
1703 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1707 r9 = error ? -error : rval;
1708 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1717 #endif /* POWERPC */
1722 #endif /* S390 || S390X */
1725 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1727 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1735 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1740 regs.r_eflags |= PSL_C;
1744 regs.r_eflags &= ~PSL_C;
1747 if (pwrite(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1751 #endif /* FREEBSD */
1753 /* All branches reach here on success (only). */
1754 tcp->u_error = error;
1759 int syscall_enter(tcp)
1764 #endif /* !USE_PROCFS */
1766 #if defined(S390) || defined(S390X)
1769 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1770 tcp->u_nargs = sysent[tcp->scno].nargs;
1772 tcp->u_nargs = MAX_ARGS;
1773 for (i = 0; i < tcp->u_nargs; i++) {
1774 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
1778 #elif defined (ALPHA)
1781 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1782 tcp->u_nargs = sysent[tcp->scno].nargs;
1784 tcp->u_nargs = MAX_ARGS;
1785 for (i = 0; i < tcp->u_nargs; i++) {
1786 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1787 * for scno somewhere above here!
1789 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1793 #elif defined (IA64)
1796 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1797 /* be backwards compatible with kernel < 2.4.4... */
1799 # define PT_RBS_END PT_AR_BSP
1802 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
1804 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1807 sof = (cfm >> 0) & 0x7f;
1808 sol = (cfm >> 7) & 0x7f;
1809 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1811 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1812 && sysent[tcp->scno].nargs != -1)
1813 tcp->u_nargs = sysent[tcp->scno].nargs;
1815 tcp->u_nargs = MAX_ARGS;
1816 for (i = 0; i < tcp->u_nargs; ++i) {
1817 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1818 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1824 if (/* EBX = out0 */
1825 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1827 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1829 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1831 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1833 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1835 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1838 for (i = 0; i < 6; ++i)
1839 /* truncate away IVE sign-extension */
1840 tcp->u_arg[i] &= 0xffffffff;
1842 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1843 && sysent[tcp->scno].nargs != -1)
1844 tcp->u_nargs = sysent[tcp->scno].nargs;
1849 #elif defined (MIPS)
1854 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1855 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1857 nargs = tcp->u_nargs = MAX_ARGS;
1859 if(upeek(pid, REG_SP, &sp) < 0)
1861 for(i = 0; i < 4; i++) {
1862 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1865 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1866 (char *)(tcp->u_arg + 4));
1868 for(i = 0; i < nargs; i++) {
1869 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1874 #elif defined (POWERPC)
1876 #define PT_ORIG_R3 34
1880 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1881 tcp->u_nargs = sysent[tcp->scno].nargs;
1883 tcp->u_nargs = MAX_ARGS;
1884 for (i = 0; i < tcp->u_nargs; i++) {
1885 if (upeek(pid, (i==0) ?
1886 (sizeof(unsigned long)*PT_ORIG_R3) :
1887 ((i+PT_R3)*sizeof(unsigned long)),
1888 &tcp->u_arg[i]) < 0)
1892 #elif defined (SPARC)
1896 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1897 tcp->u_nargs = sysent[tcp->scno].nargs;
1899 tcp->u_nargs = MAX_ARGS;
1900 for (i = 0; i < tcp->u_nargs; i++)
1901 tcp->u_arg[i] = *((®s.r_o0) + i);
1903 #elif defined (HPPA)
1907 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1908 tcp->u_nargs = sysent[tcp->scno].nargs;
1910 tcp->u_nargs = MAX_ARGS;
1911 for (i = 0; i < tcp->u_nargs; i++) {
1912 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1920 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1921 tcp->u_nargs = sysent[tcp->scno].nargs;
1923 tcp->u_nargs = MAX_ARGS;
1924 for (i = 0; i < tcp->u_nargs; i++)
1925 tcp->u_arg[i] = regs.uregs[i];
1930 static int syscall_regs[] = {
1931 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
1932 REG_REG0, REG_REG0+1, REG_REG0+2
1935 tcp->u_nargs = sysent[tcp->scno].nargs;
1936 for (i = 0; i < tcp->u_nargs; i++) {
1937 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
1944 /* Registers used by SH5 Linux system calls for parameters */
1945 static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
1948 * TODO: should also check that the number of arguments encoded
1949 * in the trap number matches the number strace expects.
1952 assert(sysent[tcp->scno].nargs <
1953 sizeof(syscall_regs)/sizeof(syscall_regs[0]));
1956 tcp->u_nargs = sysent[tcp->scno].nargs;
1957 for (i = 0; i < tcp->u_nargs; i++) {
1958 if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1963 #elif defined(X86_64)
1966 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
1967 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
1968 {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */
1971 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1972 tcp->u_nargs = sysent[tcp->scno].nargs;
1974 tcp->u_nargs = MAX_ARGS;
1975 for (i = 0; i < tcp->u_nargs; i++) {
1976 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
1980 #else /* Other architecture (like i386) (32bits specific) */
1983 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1984 tcp->u_nargs = sysent[tcp->scno].nargs;
1986 tcp->u_nargs = MAX_ARGS;
1987 for (i = 0; i < tcp->u_nargs; i++) {
1988 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1997 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1998 tcp->u_nargs = sysent[tcp->scno].nargs;
2000 tcp->u_nargs = MAX_ARGS;
2001 for (i = 0; i < tcp->u_nargs; i++) {
2004 if (upeek(pid, uoff(u_arg[0]) +
2005 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
2013 * SGI is broken: even though it has pr_sysarg, it doesn't
2014 * set them on system call entry. Get a clue.
2016 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2017 tcp->u_nargs = sysent[tcp->scno].nargs;
2019 tcp->u_nargs = tcp->status.pr_nsysarg;
2020 if (tcp->u_nargs > 4) {
2021 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2022 4*sizeof(tcp->u_arg[0]));
2023 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
2024 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
2027 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2028 tcp->u_nargs*sizeof(tcp->u_arg[0]));
2032 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
2034 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2035 tcp->u_nargs = sysent[tcp->scno].nargs;
2037 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
2038 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2039 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2040 #elif defined (HAVE_PR_SYSCALL)
2041 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2042 tcp->u_nargs = sysent[tcp->scno].nargs;
2044 tcp->u_nargs = tcp->status.pr_nsysarg;
2047 for (i = 0; i < tcp->u_nargs; i++)
2048 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
2050 #elif defined (I386)
2051 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2052 tcp->u_nargs = sysent[tcp->scno].nargs;
2055 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2056 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2058 I DONT KNOW WHAT TO DO
2059 #endif /* !HAVE_PR_SYSCALL */
2062 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2063 sysent[tcp->scno].nargs > tcp->status.val)
2064 tcp->u_nargs = sysent[tcp->scno].nargs;
2066 tcp->u_nargs = tcp->status.val;
2067 if (tcp->u_nargs < 0)
2069 if (tcp->u_nargs > MAX_ARGS)
2070 tcp->u_nargs = MAX_ARGS;
2071 switch(regs.r_eax) {
2073 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2074 regs.r_esp + sizeof(int) + sizeof(quad_t));
2077 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2078 regs.r_esp + 2 * sizeof(int));
2081 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2082 regs.r_esp + sizeof(int));
2085 #endif /* FREEBSD */
2097 /* Measure the exit time as early as possible to avoid errors. */
2098 if (dtime && (tcp->flags & TCB_INSYSCALL))
2099 gettimeofday(&tv, NULL);
2101 res = get_scno(tcp);
2105 res = syscall_fixup(tcp);
2109 if (tcp->flags & TCB_INSYSCALL) {
2111 res = get_error(tcp);
2115 internal_syscall(tcp);
2116 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2117 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2118 tcp->flags &= ~TCB_INSYSCALL;
2122 if (tcp->flags & TCB_REPRINT) {
2125 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2126 tprintf("syscall_%lu", tcp->scno);
2128 tprintf("%s", sysent[tcp->scno].sys_name);
2129 tprintf(" resumed> ");
2132 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
2133 call_count[tcp->scno]++;
2135 error_count[tcp->scno]++;
2136 tv_sub(&tv, &tv, &tcp->etime);
2138 if (tv_cmp(&tv, &tcp->dtime) > 0) {
2139 static struct timeval one_tick;
2140 if (one_tick.tv_usec == 0) {
2141 /* Initialize it. */
2142 struct itimerval it;
2143 memset(&it, 0, sizeof it);
2144 it.it_interval.tv_usec = 1;
2145 setitimer(ITIMER_REAL, &it, NULL);
2146 getitimer(ITIMER_REAL, &it);
2147 one_tick = it.it_interval;
2150 if (tv_nz(&tcp->dtime))
2152 else if (tv_cmp(&tv, &one_tick) > 0) {
2153 if (tv_cmp(&shortest, &one_tick) < 0)
2160 if (tv_cmp(&tv, &shortest) < 0)
2162 tv_add(&tv_count[tcp->scno],
2163 &tv_count[tcp->scno], &tv);
2164 tcp->flags &= ~TCB_INSYSCALL;
2168 if (tcp->scno >= nsyscalls || tcp->scno < 0
2169 || (qual_flags[tcp->scno] & QUAL_RAW))
2170 sys_res = printargs(tcp);
2172 if (not_failing_only && tcp->u_error)
2173 return 0; /* ignore failed syscalls */
2174 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2176 u_error = tcp->u_error;
2179 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2180 qual_flags[tcp->scno] & QUAL_RAW) {
2182 tprintf("= -1 (errno %ld)", u_error);
2184 tprintf("= %#lx", tcp->u_rval);
2186 else if (!(sys_res & RVAL_NONE) && u_error) {
2190 tprintf("= ? ERESTARTSYS (To be restarted)");
2192 case ERESTARTNOINTR:
2193 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2195 case ERESTARTNOHAND:
2196 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2198 case ERESTART_RESTARTBLOCK:
2199 tprintf("= ? ERESTART_RESTARTBLOCK (To be restarted)");
2205 tprintf("E??? (errno %ld)", u_error);
2206 else if (u_error < nerrnos)
2207 tprintf("%s (%s)", errnoent[u_error],
2210 tprintf("ERRNO_%ld (%s)", u_error,
2216 if (sys_res & RVAL_NONE)
2219 switch (sys_res & RVAL_MASK) {
2221 tprintf("= %#lx", tcp->u_rval);
2224 tprintf("= %#lo", tcp->u_rval);
2227 tprintf("= %lu", tcp->u_rval);
2230 tprintf("= %ld", tcp->u_rval);
2232 #ifdef HAVE_LONG_LONG
2234 tprintf("= %#llx", tcp->u_lrval);
2237 tprintf("= %#llo", tcp->u_lrval);
2239 case RVAL_LUDECIMAL:
2240 tprintf("= %llu", tcp->u_lrval);
2243 tprintf("= %lld", tcp->u_lrval);
2248 "invalid rval format\n");
2252 if ((sys_res & RVAL_STR) && tcp->auxstr)
2253 tprintf(" (%s)", tcp->auxstr);
2256 tv_sub(&tv, &tv, &tcp->etime);
2257 tprintf(" <%ld.%06ld>",
2258 (long) tv.tv_sec, (long) tv.tv_usec);
2263 if (fflush(tcp->outf) == EOF)
2265 tcp->flags &= ~TCB_INSYSCALL;
2269 /* Entering system call */
2270 res = syscall_enter(tcp);
2274 switch (tcp->scno + NR_SYSCALL_BASE) {
2276 #if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
2277 case SYS_socketcall:
2278 decode_subcall(tcp, SYS_socket_subcall,
2279 SYS_socket_nsubcalls, deref_style);
2282 decode_subcall(tcp, SYS_ipc_subcall,
2283 SYS_ipc_nsubcalls, shift_style);
2285 #endif /* !ALPHA && !MIPS && !SPARC && !HPPA && !X86_64 */
2287 case SYS_socketcall:
2288 sparc_socket_decode (tcp);
2293 #ifdef SYS_pgrpsys_subcall
2295 decode_subcall(tcp, SYS_pgrpsys_subcall,
2296 SYS_pgrpsys_nsubcalls, shift_style);
2298 #endif /* SYS_pgrpsys_subcall */
2299 #ifdef SYS_sigcall_subcall
2301 decode_subcall(tcp, SYS_sigcall_subcall,
2302 SYS_sigcall_nsubcalls, mask_style);
2304 #endif /* SYS_sigcall_subcall */
2306 decode_subcall(tcp, SYS_msgsys_subcall,
2307 SYS_msgsys_nsubcalls, shift_style);
2310 decode_subcall(tcp, SYS_shmsys_subcall,
2311 SYS_shmsys_nsubcalls, shift_style);
2314 decode_subcall(tcp, SYS_semsys_subcall,
2315 SYS_semsys_nsubcalls, shift_style);
2319 decode_subcall(tcp, SYS_utssys_subcall,
2320 SYS_utssys_nsubcalls, shift_style);
2324 decode_subcall(tcp, SYS_sysfs_subcall,
2325 SYS_sysfs_nsubcalls, shift_style);
2328 decode_subcall(tcp, SYS_spcall_subcall,
2329 SYS_spcall_nsubcalls, shift_style);
2331 #ifdef SYS_context_subcall
2333 decode_subcall(tcp, SYS_context_subcall,
2334 SYS_context_nsubcalls, shift_style);
2336 #endif /* SYS_context_subcall */
2337 #ifdef SYS_door_subcall
2339 decode_subcall(tcp, SYS_door_subcall,
2340 SYS_door_nsubcalls, door_style);
2342 #endif /* SYS_door_subcall */
2343 #ifdef SYS_kaio_subcall
2345 decode_subcall(tcp, SYS_kaio_subcall,
2346 SYS_kaio_nsubcalls, shift_style);
2354 decode_subcall(tcp, 0, 0, table_style);
2359 decode_subcall(tcp, SYS_semsys_subcall,
2360 SYS_semsys_nsubcalls, shift_style);
2363 decode_subcall(tcp, SYS_msgsys_subcall,
2364 SYS_msgsys_nsubcalls, shift_style);
2367 decode_subcall(tcp, SYS_shmsys_subcall,
2368 SYS_shmsys_nsubcalls, shift_style);
2373 internal_syscall(tcp);
2374 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2375 tcp->flags |= TCB_INSYSCALL;
2380 gettimeofday(&tcp->etime, NULL);
2381 tcp->flags |= TCB_INSYSCALL;
2386 tcp->flags &= ~TCB_REPRINT;
2388 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2389 tprintf("syscall_%lu(", tcp->scno);
2391 tprintf("%s(", sysent[tcp->scno].sys_name);
2392 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2393 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2394 sys_res = printargs(tcp);
2396 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2397 if (fflush(tcp->outf) == EOF)
2399 tcp->flags |= TCB_INSYSCALL;
2400 /* Measure the entrance time as late as possible to avoid errors. */
2402 gettimeofday(&tcp->etime, NULL);
2410 if (entering(tcp)) {
2413 for (i = 0; i < tcp->u_nargs; i++)
2414 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2428 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
2432 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2438 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2444 val = tcp->status.PR_REG[R_O1];
2447 val = tcp->status.PR_REG[EDX];
2450 val = tcp->status.PR_REG[RDX];
2453 val = tcp->status.PR_REG[CTX_V1];
2458 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
2465 * Apparently, indirect system calls have already be converted by ptrace(2),
2466 * so if you see "indir" this program has gone astray.
2474 if (entering(tcp)) {
2475 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2476 fprintf(stderr, "Bogus syscall: %u\n", scno);
2479 nargs = sysent[scno].nargs;
2480 tprintf("%s", sysent[scno].sys_name);
2481 for (i = 0; i < nargs; i++)
2482 tprintf(", %#lx", tcp->u_arg[i+1]);
2492 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2500 return strcmp(sysent[*((int *) a)].sys_name,
2501 sysent[*((int *) b)].sys_name);
2509 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2511 return (m < n) ? 1 : (m > n) ? -1 : 0;
2514 static int (*sortfun)();
2515 static struct timeval overhead = { -1, -1 };
2521 if (strcmp(sortby, "time") == 0)
2523 else if (strcmp(sortby, "calls") == 0)
2524 sortfun = count_cmp;
2525 else if (strcmp(sortby, "name") == 0)
2526 sortfun = syscall_cmp;
2527 else if (strcmp(sortby, "nothing") == 0)
2530 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2535 void set_overhead(n)
2538 overhead.tv_sec = n / 1000000;
2539 overhead.tv_usec = n % 1000000;
2547 int call_cum, error_cum;
2548 struct timeval tv_cum, dtv;
2550 char *dashes = "-------------------------";
2553 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2554 if (overhead.tv_sec == -1) {
2555 tv_mul(&overhead, &shortest, 8);
2556 tv_div(&overhead, &overhead, 10);
2558 for (i = 0; i < nsyscalls; i++) {
2559 sorted_count[i] = i;
2560 if (call_count[i] == 0)
2562 tv_mul(&dtv, &overhead, call_count[i]);
2563 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2564 call_cum += call_count[i];
2565 error_cum += error_count[i];
2566 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2569 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2570 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2571 "% time", "seconds", "usecs/call",
2572 "calls", "errors", "syscall");
2573 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2574 dashes, dashes, dashes, dashes, dashes, dashes);
2575 for (i = 0; i < nsyscalls; i++) {
2576 j = sorted_count[i];
2577 if (call_count[j] == 0)
2579 tv_div(&dtv, &tv_count[j], call_count[j]);
2581 sprintf(error_str, "%d", error_count[j]);
2583 error_str[0] = '\0';
2584 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2585 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2586 percent, (long) tv_count[j].tv_sec,
2587 (long) tv_count[j].tv_usec,
2588 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2589 call_count[j], error_str, sysent[j].sys_name);
2591 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2592 dashes, dashes, dashes, dashes, dashes, dashes);
2594 sprintf(error_str, "%d", error_cum);
2596 error_str[0] = '\0';
2597 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2598 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2599 call_cum, error_str, "total");