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:
696 #elif defined (POWERPC)
697 static long result,flags;
701 static struct pt_regs regs;
702 #elif defined (ALPHA)
705 #elif defined (SPARC)
706 static struct regs regs;
707 static unsigned long trap;
711 #elif defined(S390) || defined(S390X)
714 static long syscall_mode;
721 #elif defined(X86_64)
739 #if defined(S390) || defined(S390X)
740 if (tcp->flags & TCB_WAITEXECVE) {
742 * When the execve system call completes successfully, the
743 * new process still has -ENOSYS (old style) or __NR_execve
744 * (new style) in gpr2. We cannot recover the scno again
745 * by disassembly, because the image that executed the
746 * syscall is gone now. Fortunately, we don't want it. We
747 * leave the flag set so that syscall_fixup can fake the
750 if (tcp->flags & TCB_INSYSCALL)
753 * This is the SIGTRAP after execve. We cannot try to read
754 * the system call here either.
756 tcp->flags &= ~TCB_WAITEXECVE;
760 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
763 if (syscall_mode != -ENOSYS) {
765 * Since kernel version 2.5.44 the scno gets passed in gpr2.
770 * Old style of "passing" the scno via the SVC instruction.
773 long opcode, offset_reg, tmp;
775 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
776 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
777 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
778 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
780 if (upeek(pid, PT_PSWADDR, &pc) < 0)
783 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
785 perror("peektext(pc-oneword)");
790 * We have to check if the SVC got executed directly or via an
791 * EXECUTE instruction. In case of EXECUTE it is necessary to do
792 * instruction decoding to derive the system call number.
793 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
794 * so that this doesn't work if a SVC opcode is part of an EXECUTE
795 * opcode. Since there is no way to find out the opcode size this
796 * is the best we can do...
799 if ((opcode & 0xff00) == 0x0a00) {
801 scno = opcode & 0xff;
804 /* SVC got executed by EXECUTE instruction */
807 * Do instruction decoding of EXECUTE. If you really want to
808 * understand this, read the Principles of Operations.
810 svc_addr = (void *) (opcode & 0xfff);
813 offset_reg = (opcode & 0x000f0000) >> 16;
814 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
819 offset_reg = (opcode & 0x0000f000) >> 12;
820 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
824 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
833 offset_reg = (opcode & 0x00f00000) >> 20;
834 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
837 scno = (scno | tmp) & 0xff;
840 #elif defined (POWERPC)
841 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
843 if (!(tcp->flags & TCB_INSYSCALL)) {
844 /* Check if we return from execve. */
845 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
846 tcp->flags &= ~TCB_WAITEXECVE;
851 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
853 #elif defined (X86_64)
854 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
857 if (!(tcp->flags & TCB_INSYSCALL)) {
858 static int currpers=-1;
861 /* Check CS register value. On x86-64 linux it is:
862 * 0x33 for long mode (64 bit)
863 * 0x23 for compatibility mode (32 bit)
864 * It takes only one ptrace and thus doesn't need
867 if (upeek(pid, 8*CS, &val) < 0)
871 case 0x23: currpers = 1; break;
872 case 0x33: currpers = 0; break;
874 fprintf(stderr, "Unknown value CS=0x%02X while "
875 "detecting personality of process "
876 "PID=%d\n", (int)val, pid);
877 currpers = current_personality;
881 /* This version analyzes the opcode of a syscall instruction.
882 * (int 0x80 on i386 vs. syscall on x86-64)
883 * It works, but is too complicated.
885 unsigned long val, rip, i;
887 if(upeek(pid, 8*RIP, &rip)<0)
888 perror("upeek(RIP)");
890 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
894 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
896 printf("ptrace_peektext failed: %s\n",
898 switch (call & 0xffff)
900 /* x86-64: syscall = 0x0f 0x05 */
901 case 0x050f: currpers = 0; break;
902 /* i386: int 0x80 = 0xcd 0x80 */
903 case 0x80cd: currpers = 1; break;
905 currpers = current_personality;
907 "Unknown syscall opcode (0x%04X) while "
908 "detecting personality of process "
909 "PID=%d\n", (int)call, pid);
913 if(currpers != current_personality)
915 char *names[]={"64 bit", "32 bit"};
916 set_personality(currpers);
917 printf("[ Process PID=%d runs in %s mode. ]\n",
918 pid, names[current_personality]);
922 # define IA64_PSR_IS ((long)1 << 34)
923 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
924 ia32 = (psr & IA64_PSR_IS) != 0;
925 if (!(tcp->flags & TCB_INSYSCALL)) {
927 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
930 if (upeek (pid, PT_R15, &scno) < 0)
933 /* Check if we return from execve. */
934 if (tcp->flags & TCB_WAITEXECVE) {
935 tcp->flags &= ~TCB_WAITEXECVE;
939 /* syscall in progress */
940 if (upeek (pid, PT_R8, &r8) < 0)
942 if (upeek (pid, PT_R10, &r10) < 0)
947 * Read complete register set in one go.
949 if (ptrace(PTRACE_GETREGS, pid, NULL, (void *)®s) == -1)
953 * We only need to grab the syscall number on syscall entry.
955 if (regs.ARM_ip == 0) {
957 * Note: we only deal with only 32-bit CPUs here.
959 if (regs.ARM_cpsr & 0x20) {
961 * Get the Thumb-mode system call number
966 * Get the ARM-mode system call number
969 scno = ptrace(PTRACE_PEEKTEXT, pid, (void *)(regs.ARM_pc - 4), NULL);
973 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
974 tcp->flags &= ~TCB_WAITEXECVE;
978 if ((scno & 0x0ff00000) != 0x0f900000) {
979 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
985 * Fixup the syscall number
990 if (tcp->flags & TCB_INSYSCALL) {
991 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
992 tcp->flags &= ~TCB_INSYSCALL;
995 if (!(tcp->flags & TCB_INSYSCALL)) {
996 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
997 tcp->flags |= TCB_INSYSCALL;
1000 #elif defined (M68K)
1001 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
1003 #elif defined (MIPS)
1004 if (upeek(pid, REG_A3, &a3) < 0)
1007 if(!(tcp->flags & TCB_INSYSCALL)) {
1008 if (upeek(pid, REG_V0, &scno) < 0)
1011 if (scno < 0 || scno > nsyscalls) {
1012 if(a3 == 0 || a3 == -1) {
1014 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
1019 if (upeek(pid, REG_V0, &r2) < 0)
1022 #elif defined (ALPHA)
1023 if (upeek(pid, REG_A3, &a3) < 0)
1026 if (!(tcp->flags & TCB_INSYSCALL)) {
1027 if (upeek(pid, REG_R0, &scno) < 0)
1030 /* Check if we return from execve. */
1031 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1032 tcp->flags &= ~TCB_WAITEXECVE;
1037 * Do some sanity checks to figure out if it's
1038 * really a syscall entry
1040 if (scno < 0 || scno > nsyscalls) {
1041 if (a3 == 0 || a3 == -1) {
1043 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
1049 if (upeek(pid, REG_R0, &r0) < 0)
1052 #elif defined (SPARC)
1053 /* Everything we need is in the current register set. */
1054 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
1057 /* If we are entering, then disassemble the syscall trap. */
1058 if (!(tcp->flags & TCB_INSYSCALL)) {
1059 /* Retrieve the syscall trap instruction. */
1061 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
1065 /* Disassemble the trap to see what personality to use. */
1068 /* Linux/SPARC syscall trap. */
1072 /* Linux/SPARC64 syscall trap. */
1073 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
1076 /* SunOS syscall trap. (pers 1) */
1077 fprintf(stderr,"syscall: SunOS no support\n");
1080 /* Solaris 2.x syscall trap. (per 2) */
1084 /* NetBSD/FreeBSD syscall trap. */
1085 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1088 /* Solaris 2.x gettimeofday */
1092 /* Unknown syscall trap. */
1093 if(tcp->flags & TCB_WAITEXECVE) {
1094 tcp->flags &= ~TCB_WAITEXECVE;
1097 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
1101 /* Extract the system call number from the registers. */
1102 if (trap == 0x91d02027)
1108 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
1112 if (upeek(pid, PT_GR20, &scno) < 0)
1114 if (!(tcp->flags & TCB_INSYSCALL)) {
1115 /* Check if we return from execve. */
1116 if ((tcp->flags & TCB_WAITEXECVE)) {
1117 tcp->flags &= ~TCB_WAITEXECVE;
1123 * In the new syscall ABI, the system call number is in R3.
1125 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1129 /* Odd as it may seem, a glibc bug has been known to cause
1130 glibc to issue bogus negative syscall numbers. So for
1131 our purposes, make strace print what it *should* have been */
1132 long correct_scno = (scno & 0xff);
1135 "Detected glibc bug: bogus system call number = %ld, "
1136 "correcting to %ld\n",
1139 scno = correct_scno;
1143 if (!(tcp->flags & TCB_INSYSCALL)) {
1144 /* Check if we return from execve. */
1145 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1146 tcp->flags &= ~TCB_WAITEXECVE;
1151 if (upeek(pid, REG_SYSCALL, &scno) < 0)
1155 if (!(tcp->flags & TCB_INSYSCALL)) {
1156 /* Check if we return from execve. */
1157 if (tcp->flags & TCB_WAITEXECVE) {
1158 tcp->flags &= ~TCB_WAITEXECVE;
1165 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1168 /* new syscall ABI returns result in R0 */
1169 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1172 /* ABI defines result returned in r9 */
1173 if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
1178 #ifdef HAVE_PR_SYSCALL
1179 scno = tcp->status.PR_SYSCALL;
1180 #else /* !HAVE_PR_SYSCALL */
1182 scno = tcp->status.PR_WHAT;
1184 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1188 switch (regs.r_eax) {
1191 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1197 #endif /* FREEBSD */
1198 #endif /* !HAVE_PR_SYSCALL */
1199 #endif /* USE_PROCFS */
1200 if (!(tcp->flags & TCB_INSYSCALL))
1212 #else /* USE_PROCFS */
1213 int scno = tcp->scno;
1215 if (!(tcp->flags & TCB_INSYSCALL)) {
1216 if (tcp->status.PR_WHY != PR_SYSENTRY) {
1220 || scno == SYS_vfork
1221 #endif /* SYS_vfork */
1223 || scno == SYS_fork1
1224 #endif /* SYS_fork1 */
1226 || scno == SYS_forkall
1227 #endif /* SYS_forkall */
1229 || scno == SYS_rfork1
1230 #endif /* SYS_fork1 */
1232 || scno == SYS_rforkall
1233 #endif /* SYS_rforkall */
1235 /* We are returning in the child, fake it. */
1236 tcp->status.PR_WHY = PR_SYSENTRY;
1238 tcp->status.PR_WHY = PR_SYSEXIT;
1241 fprintf(stderr, "syscall: missing entry\n");
1242 tcp->flags |= TCB_INSYSCALL;
1247 if (tcp->status.PR_WHY != PR_SYSEXIT) {
1248 fprintf(stderr, "syscall: missing exit\n");
1249 tcp->flags &= ~TCB_INSYSCALL;
1252 #endif /* USE_PROCFS */
1254 if (!(tcp->flags & TCB_INSYSCALL)) {
1256 fprintf(stderr, "syscall: missing entry\n");
1257 tcp->flags |= TCB_INSYSCALL;
1264 * This happens when a signal handler
1265 * for a signal which interrupted a
1266 * a system call makes another system call.
1268 fprintf(stderr, "syscall: missing exit\n");
1270 tcp->flags &= ~TCB_INSYSCALL;
1276 if (upeek(pid, 4*EAX, &eax) < 0)
1278 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1280 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1283 #elif defined (X86_64)
1284 if (upeek(pid, 8*RAX, &rax) < 0)
1286 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1288 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1291 #elif defined (S390) || defined (S390X)
1292 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1294 if (syscall_mode != -ENOSYS)
1295 syscall_mode = tcp->scno;
1296 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
1298 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1301 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1302 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1303 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1305 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1306 * flag set for the post-execve SIGTRAP to see and reset.
1310 #elif defined (POWERPC)
1311 # define SO_MASK 0x10000000
1312 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1314 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
1316 if (flags & SO_MASK)
1318 #elif defined (M68K)
1319 if (upeek(pid, 4*PT_D0, &d0) < 0)
1321 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1323 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1330 #elif defined (HPPA)
1331 if (upeek(pid, PT_GR28, &r28) < 0)
1334 if (upeek(pid, PT_R10, &r10) < 0)
1336 if (upeek(pid, PT_R8, &r8) < 0)
1338 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1340 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1354 #if defined(S390) || defined(S390X)
1355 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1363 #else /* !S390 && !S390X */
1365 if (eax < 0 && -eax < nerrnos) {
1375 if (rax < 0 && -rax < nerrnos) {
1389 if (err < 0 && -err < nerrnos) {
1417 if (result && (unsigned long) -result < nerrnos) {
1422 tcp->u_rval = result;
1425 #else /* !POWERPC */
1427 if (d0 && (unsigned) -d0 < nerrnos) {
1437 if (regs.ARM_r0 && (unsigned) -regs.ARM_r0 < nerrnos) {
1439 u_error = -regs.ARM_r0;
1442 tcp->u_rval = regs.ARM_r0;
1457 if (regs.r_psr & PSR_C) {
1459 u_error = regs.r_o0;
1462 tcp->u_rval = regs.r_o0;
1467 if (r28 && (unsigned) -r28 < nerrnos) {
1477 /* interpret R0 as return value or error number */
1478 if (r0 && (unsigned) -r0 < nerrnos) {
1488 /* interpret result as return value or error number */
1489 if (r9 && (unsigned) -r9 < nerrnos) {
1504 #endif /* POWERPC */
1509 #endif /* S390 || S390X */
1512 /* get error code from user struct */
1513 if (upeek(pid, uoff(u_error), &u_error) < 0)
1515 u_error >>= 24; /* u_error is a char */
1517 /* get system call return value */
1518 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1523 /* Judicious guessing goes a long way. */
1524 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1526 u_error = tcp->status.pr_reg[R_O0];
1529 tcp->u_rval = tcp->status.pr_reg[R_O0];
1534 /* Wanna know how to kill an hour single-stepping? */
1535 if (tcp->status.PR_REG[EFL] & 0x1) {
1537 u_error = tcp->status.PR_REG[EAX];
1540 tcp->u_rval = tcp->status.PR_REG[EAX];
1541 #ifdef HAVE_LONG_LONG
1543 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1544 tcp->status.PR_REG[EAX];
1550 /* Wanna know how to kill an hour single-stepping? */
1551 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1553 u_error = tcp->status.PR_REG[RAX];
1556 tcp->u_rval = tcp->status.PR_REG[RAX];
1561 if (tcp->status.pr_reg[CTX_A3]) {
1563 u_error = tcp->status.pr_reg[CTX_V0];
1566 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1572 if (regs.r_eflags & PSL_C) {
1574 u_error = regs.r_eax;
1576 tcp->u_rval = regs.r_eax;
1578 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1581 #endif /* FREEBSD */
1582 tcp->u_error = u_error;
1587 force_result(tcp, error, rval)
1593 #if defined(S390) || defined(S390X)
1594 gpr2 = error ? -error : rval;
1595 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1597 #else /* !S390 && !S390X */
1599 eax = error ? -error : rval;
1600 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1604 rax = error ? -error : rval;
1605 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 4), rax) < 0)
1610 r8 = error ? -error : rval;
1611 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1623 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1624 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1637 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1638 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1642 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1652 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1653 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
1655 #else /* !POWERPC */
1657 d0 = error ? -error : rval;
1658 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1662 regs.ARM_r0 = error ? -error : rval;
1663 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), regs.ARM_r0) < 0)
1675 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1676 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1680 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1683 regs.r_psr |= PSR_C;
1687 regs.r_psr &= ~PSR_C;
1690 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1694 r28 = error ? -error : rval;
1695 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1699 r0 = error ? -error : rval;
1700 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1704 r9 = error ? -error : rval;
1705 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1714 #endif /* POWERPC */
1719 #endif /* S390 || S390X */
1722 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1724 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1732 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1737 regs.r_eflags |= PSL_C;
1741 regs.r_eflags &= ~PSL_C;
1744 if (pwrite(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1748 #endif /* FREEBSD */
1750 /* All branches reach here on success (only). */
1751 tcp->u_error = error;
1756 int syscall_enter(tcp)
1761 #endif /* !USE_PROCFS */
1763 #if defined(S390) || defined(S390X)
1766 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1767 tcp->u_nargs = sysent[tcp->scno].nargs;
1769 tcp->u_nargs = MAX_ARGS;
1770 for (i = 0; i < tcp->u_nargs; i++) {
1771 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
1775 #elif defined (ALPHA)
1778 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1779 tcp->u_nargs = sysent[tcp->scno].nargs;
1781 tcp->u_nargs = MAX_ARGS;
1782 for (i = 0; i < tcp->u_nargs; i++) {
1783 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1784 * for scno somewhere above here!
1786 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1790 #elif defined (IA64)
1793 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1794 /* be backwards compatible with kernel < 2.4.4... */
1796 # define PT_RBS_END PT_AR_BSP
1799 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
1801 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1804 sof = (cfm >> 0) & 0x7f;
1805 sol = (cfm >> 7) & 0x7f;
1806 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1808 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1809 && sysent[tcp->scno].nargs != -1)
1810 tcp->u_nargs = sysent[tcp->scno].nargs;
1812 tcp->u_nargs = MAX_ARGS;
1813 for (i = 0; i < tcp->u_nargs; ++i) {
1814 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1815 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1821 if (/* EBX = out0 */
1822 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1824 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1826 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1828 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1830 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1832 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1835 for (i = 0; i < 6; ++i)
1836 /* truncate away IVE sign-extension */
1837 tcp->u_arg[i] &= 0xffffffff;
1839 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1840 && sysent[tcp->scno].nargs != -1)
1841 tcp->u_nargs = sysent[tcp->scno].nargs;
1846 #elif defined (MIPS)
1851 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1852 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1854 nargs = tcp->u_nargs = MAX_ARGS;
1856 if(upeek(pid, REG_SP, &sp) < 0)
1858 for(i = 0; i < 4; i++) {
1859 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1862 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1863 (char *)(tcp->u_arg + 4));
1865 for(i = 0; i < nargs; i++) {
1866 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1871 #elif defined (POWERPC)
1873 #define PT_ORIG_R3 34
1877 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1878 tcp->u_nargs = sysent[tcp->scno].nargs;
1880 tcp->u_nargs = MAX_ARGS;
1881 for (i = 0; i < tcp->u_nargs; i++) {
1882 if (upeek(pid, (i==0) ?
1883 (sizeof(unsigned long)*PT_ORIG_R3) :
1884 ((i+PT_R3)*sizeof(unsigned long)),
1885 &tcp->u_arg[i]) < 0)
1889 #elif defined (SPARC)
1893 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1894 tcp->u_nargs = sysent[tcp->scno].nargs;
1896 tcp->u_nargs = MAX_ARGS;
1897 for (i = 0; i < tcp->u_nargs; i++)
1898 tcp->u_arg[i] = *((®s.r_o0) + i);
1900 #elif defined (HPPA)
1904 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1905 tcp->u_nargs = sysent[tcp->scno].nargs;
1907 tcp->u_nargs = MAX_ARGS;
1908 for (i = 0; i < tcp->u_nargs; i++) {
1909 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1917 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1918 tcp->u_nargs = sysent[tcp->scno].nargs;
1920 tcp->u_nargs = MAX_ARGS;
1921 for (i = 0; i < tcp->u_nargs; i++)
1922 tcp->u_arg[i] = regs.uregs[i];
1927 static int syscall_regs[] = {
1928 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
1929 REG_REG0, REG_REG0+1, REG_REG0+2
1932 tcp->u_nargs = sysent[tcp->scno].nargs;
1933 for (i = 0; i < tcp->u_nargs; i++) {
1934 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
1941 /* Registers used by SH5 Linux system calls for parameters */
1942 static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
1945 * TODO: should also check that the number of arguments encoded
1946 * in the trap number matches the number strace expects.
1949 assert(sysent[tcp->scno].nargs <
1950 sizeof(syscall_regs)/sizeof(syscall_regs[0]));
1953 tcp->u_nargs = sysent[tcp->scno].nargs;
1954 for (i = 0; i < tcp->u_nargs; i++) {
1955 if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1960 #elif defined(X86_64)
1963 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
1964 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
1965 {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */
1968 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1969 tcp->u_nargs = sysent[tcp->scno].nargs;
1971 tcp->u_nargs = MAX_ARGS;
1972 for (i = 0; i < tcp->u_nargs; i++) {
1973 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
1977 #else /* Other architecture (like i386) (32bits specific) */
1980 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1981 tcp->u_nargs = sysent[tcp->scno].nargs;
1983 tcp->u_nargs = MAX_ARGS;
1984 for (i = 0; i < tcp->u_nargs; i++) {
1985 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1994 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1995 tcp->u_nargs = sysent[tcp->scno].nargs;
1997 tcp->u_nargs = MAX_ARGS;
1998 for (i = 0; i < tcp->u_nargs; i++) {
2001 if (upeek(pid, uoff(u_arg[0]) +
2002 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
2010 * SGI is broken: even though it has pr_sysarg, it doesn't
2011 * set them on system call entry. Get a clue.
2013 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2014 tcp->u_nargs = sysent[tcp->scno].nargs;
2016 tcp->u_nargs = tcp->status.pr_nsysarg;
2017 if (tcp->u_nargs > 4) {
2018 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2019 4*sizeof(tcp->u_arg[0]));
2020 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
2021 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
2024 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2025 tcp->u_nargs*sizeof(tcp->u_arg[0]));
2029 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
2031 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2032 tcp->u_nargs = sysent[tcp->scno].nargs;
2034 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
2035 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2036 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2037 #elif defined (HAVE_PR_SYSCALL)
2038 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2039 tcp->u_nargs = sysent[tcp->scno].nargs;
2041 tcp->u_nargs = tcp->status.pr_nsysarg;
2044 for (i = 0; i < tcp->u_nargs; i++)
2045 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
2047 #elif defined (I386)
2048 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2049 tcp->u_nargs = sysent[tcp->scno].nargs;
2052 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2053 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2055 I DONT KNOW WHAT TO DO
2056 #endif /* !HAVE_PR_SYSCALL */
2059 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2060 sysent[tcp->scno].nargs > tcp->status.val)
2061 tcp->u_nargs = sysent[tcp->scno].nargs;
2063 tcp->u_nargs = tcp->status.val;
2064 if (tcp->u_nargs < 0)
2066 if (tcp->u_nargs > MAX_ARGS)
2067 tcp->u_nargs = MAX_ARGS;
2068 switch(regs.r_eax) {
2070 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2071 regs.r_esp + sizeof(int) + sizeof(quad_t));
2074 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2075 regs.r_esp + 2 * sizeof(int));
2078 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2079 regs.r_esp + sizeof(int));
2082 #endif /* FREEBSD */
2094 /* Measure the exit time as early as possible to avoid errors. */
2095 if (dtime && (tcp->flags & TCB_INSYSCALL))
2096 gettimeofday(&tv, NULL);
2098 res = get_scno(tcp);
2102 res = syscall_fixup(tcp);
2106 if (tcp->flags & TCB_INSYSCALL) {
2108 res = get_error(tcp);
2112 internal_syscall(tcp);
2113 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2114 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2115 tcp->flags &= ~TCB_INSYSCALL;
2119 if (tcp->flags & TCB_REPRINT) {
2122 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2123 tprintf("syscall_%lu", tcp->scno);
2125 tprintf("%s", sysent[tcp->scno].sys_name);
2126 tprintf(" resumed> ");
2129 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
2130 call_count[tcp->scno]++;
2132 error_count[tcp->scno]++;
2133 tv_sub(&tv, &tv, &tcp->etime);
2135 if (tv_cmp(&tv, &tcp->dtime) > 0) {
2136 static struct timeval one_tick;
2137 if (one_tick.tv_usec == 0) {
2138 /* Initialize it. */
2139 struct itimerval it;
2140 memset(&it, 0, sizeof it);
2141 it.it_interval.tv_usec = 1;
2142 setitimer(ITIMER_REAL, &it, NULL);
2143 getitimer(ITIMER_REAL, &it);
2144 one_tick = it.it_interval;
2147 if (tv_nz(&tcp->dtime))
2149 else if (tv_cmp(&tv, &one_tick) > 0) {
2150 if (tv_cmp(&shortest, &one_tick) < 0)
2157 if (tv_cmp(&tv, &shortest) < 0)
2159 tv_add(&tv_count[tcp->scno],
2160 &tv_count[tcp->scno], &tv);
2161 tcp->flags &= ~TCB_INSYSCALL;
2165 if (tcp->scno >= nsyscalls || tcp->scno < 0
2166 || (qual_flags[tcp->scno] & QUAL_RAW))
2167 sys_res = printargs(tcp);
2169 if (not_failing_only && tcp->u_error)
2170 return 0; /* ignore failed syscalls */
2171 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2173 u_error = tcp->u_error;
2176 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2177 qual_flags[tcp->scno] & QUAL_RAW) {
2179 tprintf("= -1 (errno %ld)", u_error);
2181 tprintf("= %#lx", tcp->u_rval);
2183 else if (!(sys_res & RVAL_NONE) && u_error) {
2187 tprintf("= ? ERESTARTSYS (To be restarted)");
2189 case ERESTARTNOINTR:
2190 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2192 case ERESTARTNOHAND:
2193 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2195 case ERESTART_RESTARTBLOCK:
2196 tprintf("= ? ERESTART_RESTARTBLOCK (To be restarted)");
2202 tprintf("E??? (errno %ld)", u_error);
2203 else if (u_error < nerrnos)
2204 tprintf("%s (%s)", errnoent[u_error],
2207 tprintf("ERRNO_%ld (%s)", u_error,
2213 if (sys_res & RVAL_NONE)
2216 switch (sys_res & RVAL_MASK) {
2218 tprintf("= %#lx", tcp->u_rval);
2221 tprintf("= %#lo", tcp->u_rval);
2224 tprintf("= %lu", tcp->u_rval);
2227 tprintf("= %ld", tcp->u_rval);
2229 #ifdef HAVE_LONG_LONG
2231 tprintf("= %#llx", tcp->u_lrval);
2234 tprintf("= %#llo", tcp->u_lrval);
2236 case RVAL_LUDECIMAL:
2237 tprintf("= %llu", tcp->u_lrval);
2240 tprintf("= %lld", tcp->u_lrval);
2245 "invalid rval format\n");
2249 if ((sys_res & RVAL_STR) && tcp->auxstr)
2250 tprintf(" (%s)", tcp->auxstr);
2253 tv_sub(&tv, &tv, &tcp->etime);
2254 tprintf(" <%ld.%06ld>",
2255 (long) tv.tv_sec, (long) tv.tv_usec);
2260 if (fflush(tcp->outf) == EOF)
2262 tcp->flags &= ~TCB_INSYSCALL;
2266 /* Entering system call */
2267 res = syscall_enter(tcp);
2271 switch (tcp->scno + NR_SYSCALL_BASE) {
2273 #if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
2274 case SYS_socketcall:
2275 decode_subcall(tcp, SYS_socket_subcall,
2276 SYS_socket_nsubcalls, deref_style);
2279 decode_subcall(tcp, SYS_ipc_subcall,
2280 SYS_ipc_nsubcalls, shift_style);
2282 #endif /* !ALPHA && !MIPS && !SPARC && !HPPA && !X86_64 */
2284 case SYS_socketcall:
2285 sparc_socket_decode (tcp);
2290 #ifdef SYS_pgrpsys_subcall
2292 decode_subcall(tcp, SYS_pgrpsys_subcall,
2293 SYS_pgrpsys_nsubcalls, shift_style);
2295 #endif /* SYS_pgrpsys_subcall */
2296 #ifdef SYS_sigcall_subcall
2298 decode_subcall(tcp, SYS_sigcall_subcall,
2299 SYS_sigcall_nsubcalls, mask_style);
2301 #endif /* SYS_sigcall_subcall */
2303 decode_subcall(tcp, SYS_msgsys_subcall,
2304 SYS_msgsys_nsubcalls, shift_style);
2307 decode_subcall(tcp, SYS_shmsys_subcall,
2308 SYS_shmsys_nsubcalls, shift_style);
2311 decode_subcall(tcp, SYS_semsys_subcall,
2312 SYS_semsys_nsubcalls, shift_style);
2316 decode_subcall(tcp, SYS_utssys_subcall,
2317 SYS_utssys_nsubcalls, shift_style);
2321 decode_subcall(tcp, SYS_sysfs_subcall,
2322 SYS_sysfs_nsubcalls, shift_style);
2325 decode_subcall(tcp, SYS_spcall_subcall,
2326 SYS_spcall_nsubcalls, shift_style);
2328 #ifdef SYS_context_subcall
2330 decode_subcall(tcp, SYS_context_subcall,
2331 SYS_context_nsubcalls, shift_style);
2333 #endif /* SYS_context_subcall */
2334 #ifdef SYS_door_subcall
2336 decode_subcall(tcp, SYS_door_subcall,
2337 SYS_door_nsubcalls, door_style);
2339 #endif /* SYS_door_subcall */
2340 #ifdef SYS_kaio_subcall
2342 decode_subcall(tcp, SYS_kaio_subcall,
2343 SYS_kaio_nsubcalls, shift_style);
2351 decode_subcall(tcp, 0, 0, table_style);
2356 decode_subcall(tcp, SYS_semsys_subcall,
2357 SYS_semsys_nsubcalls, shift_style);
2360 decode_subcall(tcp, SYS_msgsys_subcall,
2361 SYS_msgsys_nsubcalls, shift_style);
2364 decode_subcall(tcp, SYS_shmsys_subcall,
2365 SYS_shmsys_nsubcalls, shift_style);
2370 internal_syscall(tcp);
2371 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2372 tcp->flags |= TCB_INSYSCALL;
2377 gettimeofday(&tcp->etime, NULL);
2378 tcp->flags |= TCB_INSYSCALL;
2383 tcp->flags &= ~TCB_REPRINT;
2385 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2386 tprintf("syscall_%lu(", tcp->scno);
2388 tprintf("%s(", sysent[tcp->scno].sys_name);
2389 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2390 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2391 sys_res = printargs(tcp);
2393 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2394 if (fflush(tcp->outf) == EOF)
2396 tcp->flags |= TCB_INSYSCALL;
2397 /* Measure the entrance time as late as possible to avoid errors. */
2399 gettimeofday(&tcp->etime, NULL);
2407 if (entering(tcp)) {
2410 for (i = 0; i < tcp->u_nargs; i++)
2411 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2425 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
2429 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2435 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2441 val = tcp->status.PR_REG[R_O1];
2444 val = tcp->status.PR_REG[EDX];
2447 val = tcp->status.PR_REG[RDX];
2450 val = tcp->status.PR_REG[CTX_V1];
2455 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
2462 * Apparently, indirect system calls have already be converted by ptrace(2),
2463 * so if you see "indir" this program has gone astray.
2471 if (entering(tcp)) {
2472 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2473 fprintf(stderr, "Bogus syscall: %u\n", scno);
2476 nargs = sysent[scno].nargs;
2477 tprintf("%s", sysent[scno].sys_name);
2478 for (i = 0; i < nargs; i++)
2479 tprintf(", %#lx", tcp->u_arg[i+1]);
2489 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2497 return strcmp(sysent[*((int *) a)].sys_name,
2498 sysent[*((int *) b)].sys_name);
2506 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2508 return (m < n) ? 1 : (m > n) ? -1 : 0;
2511 static int (*sortfun)();
2512 static struct timeval overhead = { -1, -1 };
2518 if (strcmp(sortby, "time") == 0)
2520 else if (strcmp(sortby, "calls") == 0)
2521 sortfun = count_cmp;
2522 else if (strcmp(sortby, "name") == 0)
2523 sortfun = syscall_cmp;
2524 else if (strcmp(sortby, "nothing") == 0)
2527 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2532 void set_overhead(n)
2535 overhead.tv_sec = n / 1000000;
2536 overhead.tv_usec = n % 1000000;
2544 int call_cum, error_cum;
2545 struct timeval tv_cum, dtv;
2547 char *dashes = "-------------------------";
2550 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2551 if (overhead.tv_sec == -1) {
2552 tv_mul(&overhead, &shortest, 8);
2553 tv_div(&overhead, &overhead, 10);
2555 for (i = 0; i < nsyscalls; i++) {
2556 sorted_count[i] = i;
2557 if (call_count[i] == 0)
2559 tv_mul(&dtv, &overhead, call_count[i]);
2560 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2561 call_cum += call_count[i];
2562 error_cum += error_count[i];
2563 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2566 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2567 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2568 "% time", "seconds", "usecs/call",
2569 "calls", "errors", "syscall");
2570 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2571 dashes, dashes, dashes, dashes, dashes, dashes);
2572 for (i = 0; i < nsyscalls; i++) {
2573 j = sorted_count[i];
2574 if (call_count[j] == 0)
2576 tv_div(&dtv, &tv_count[j], call_count[j]);
2578 sprintf(error_str, "%d", error_count[j]);
2580 error_str[0] = '\0';
2581 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2582 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2583 percent, (long) tv_count[j].tv_sec,
2584 (long) tv_count[j].tv_usec,
2585 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2586 call_count[j], error_str, sysent[j].sys_name);
2588 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2589 dashes, dashes, dashes, dashes, dashes, dashes);
2591 sprintf(error_str, "%d", error_cum);
2593 error_str[0] = '\0';
2594 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2595 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2596 call_cum, error_str, "total");