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>
46 #if defined (SPARC) || defined (SPARC64)
47 # define fpq kernel_fpq
49 # define fpu kernel_fpu
52 #if defined (SPARC) || defined (SPARC64)
61 #ifndef PTRACE_PEEKUSR
62 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
64 #elif defined(HAVE_LINUX_PTRACE_H)
66 # ifdef HAVE_STRUCT_IA64_FPREG
67 # define ia64_fpreg XXX_ia64_fpreg
69 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
70 # define pt_all_user_regs XXX_pt_all_user_regs
72 #include <linux/ptrace.h>
74 # undef pt_all_user_regs
77 #if defined (LINUX) && defined (SPARC64)
79 # undef PTRACE_GETREGS
80 # define PTRACE_GETREGS PTRACE_GETREGS64
81 # undef PTRACE_SETREGS
82 # define PTRACE_SETREGS PTRACE_SETREGS64
83 #endif /* LINUX && SPARC64 */
85 #if defined(LINUX) && defined(IA64)
86 # include <asm/ptrace_offsets.h>
90 #define NR_SYSCALL_BASE 0
93 #define ERESTARTSYS 512
95 #ifndef ERESTARTNOINTR
96 #define ERESTARTNOINTR 513
98 #ifndef ERESTARTNOHAND
99 #define ERESTARTNOHAND 514 /* restart if no handler.. */
102 #define ENOIOCTLCMD 515 /* No ioctl command */
104 #ifndef ERESTART_RESTARTBLOCK
105 #define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
113 #undef NR_SYSCALL_BASE
114 #define NR_SYSCALL_BASE __NR_SYSCALL_BASE
120 /* Define these shorthand notations to simplify the syscallent files. */
121 #define TF TRACE_FILE
123 #define TN TRACE_NETWORK
124 #define TP TRACE_PROCESS
125 #define TS TRACE_SIGNAL
127 static const struct sysent sysent0[] = {
128 #include "syscallent.h"
130 static const int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
132 #if SUPPORTED_PERSONALITIES >= 2
133 static const struct sysent sysent1[] = {
134 #include "syscallent1.h"
136 static const int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
137 #endif /* SUPPORTED_PERSONALITIES >= 2 */
139 #if SUPPORTED_PERSONALITIES >= 3
140 static const struct sysent sysent2[] = {
141 #include "syscallent2.h"
143 static const int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
144 #endif /* SUPPORTED_PERSONALITIES >= 3 */
146 const struct sysent *sysent;
149 /* Now undef them since short defines cause wicked namespace pollution. */
156 static const char *const errnoent0[] = {
157 #include "errnoent.h"
159 static const int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
161 #if SUPPORTED_PERSONALITIES >= 2
162 static const char *const errnoent1[] = {
163 #include "errnoent1.h"
165 static const int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
166 #endif /* SUPPORTED_PERSONALITIES >= 2 */
168 #if SUPPORTED_PERSONALITIES >= 3
169 static const char *const errnoent2[] = {
170 #include "errnoent2.h"
172 static const int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
173 #endif /* SUPPORTED_PERSONALITIES >= 3 */
175 const char *const *errnoent;
178 int current_personality;
181 set_personality(personality)
184 switch (personality) {
186 errnoent = errnoent0;
189 nsyscalls = nsyscalls0;
190 ioctlent = ioctlent0;
191 nioctlents = nioctlents0;
192 signalent = signalent0;
193 nsignals = nsignals0;
196 #if SUPPORTED_PERSONALITIES >= 2
198 errnoent = errnoent1;
201 nsyscalls = nsyscalls1;
202 ioctlent = ioctlent1;
203 nioctlents = nioctlents1;
204 signalent = signalent1;
205 nsignals = nsignals1;
207 #endif /* SUPPORTED_PERSONALITIES >= 2 */
209 #if SUPPORTED_PERSONALITIES >= 3
211 errnoent = errnoent2;
214 nsyscalls = nsyscalls2;
215 ioctlent = ioctlent2;
216 nioctlents = nioctlents2;
217 signalent = signalent2;
218 nsignals = nsignals2;
220 #endif /* SUPPORTED_PERSONALITIES >= 3 */
226 current_personality = personality;
230 int qual_flags[MAX_QUALS];
232 static int call_count[MAX_QUALS];
233 static int error_count[MAX_QUALS];
234 static struct timeval tv_count[MAX_QUALS];
235 static int sorted_count[MAX_QUALS];
237 static struct timeval shortest = { 1000000, 0 };
239 static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
241 static struct qual_options {
247 { QUAL_TRACE, "trace", qual_syscall, "system call" },
248 { QUAL_TRACE, "t", qual_syscall, "system call" },
249 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
250 { QUAL_ABBREV, "a", qual_syscall, "system call" },
251 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
252 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
253 { QUAL_RAW, "raw", qual_syscall, "system call" },
254 { QUAL_RAW, "x", qual_syscall, "system call" },
255 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
256 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
257 { QUAL_SIGNAL, "s", qual_signal, "signal" },
258 { QUAL_FAULT, "fault", qual_fault, "fault" },
259 { QUAL_FAULT, "faults", qual_fault, "fault" },
260 { QUAL_FAULT, "m", qual_fault, "fault" },
261 { QUAL_READ, "read", qual_desc, "descriptor" },
262 { QUAL_READ, "reads", qual_desc, "descriptor" },
263 { QUAL_READ, "r", qual_desc, "descriptor" },
264 { QUAL_WRITE, "write", qual_desc, "descriptor" },
265 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
266 { QUAL_WRITE, "w", qual_desc, "descriptor" },
267 { 0, NULL, NULL, NULL },
271 qualify_one(n, opt, not)
273 struct qual_options *opt;
277 qual_flags[n] &= ~opt->bitflag;
279 qual_flags[n] |= opt->bitflag;
283 qual_syscall(s, opt, not)
285 struct qual_options *opt;
291 for (i = 0; i < nsyscalls; i++) {
292 if (strcmp(s, sysent[i].sys_name) == 0) {
293 qualify_one(i, opt, not);
301 qual_signal(s, opt, not)
303 struct qual_options *opt;
309 if (s && *s && isdigit((unsigned char)*s)) {
310 qualify_one(atoi(s), opt, not);
313 if (strlen(s) >= sizeof buf)
317 for (i = 0; s[i]; i++)
318 s[i] = toupper((unsigned char)(s[i]));
319 if (strncmp(s, "SIG", 3) == 0)
321 for (i = 0; i <= NSIG; i++)
322 if (strcmp(s, signame(i) + 3) == 0) {
323 qualify_one(atoi(s), opt, not);
330 qual_fault(s, opt, not)
332 struct qual_options *opt;
339 qual_desc(s, opt, not)
341 struct qual_options *opt;
344 if (s && *s && isdigit((unsigned char)*s)) {
345 qualify_one(atoi(s), opt, not);
355 if (strcmp(s, "file") == 0)
357 if (strcmp(s, "ipc") == 0)
359 if (strcmp(s, "network") == 0)
360 return TRACE_NETWORK;
361 if (strcmp(s, "process") == 0)
362 return TRACE_PROCESS;
363 if (strcmp(s, "signal") == 0)
372 struct qual_options *opt;
377 opt = &qual_options[0];
378 for (i = 0; (p = qual_options[i].option_name); i++) {
380 if (strncmp(s, p, n) == 0 && s[n] == '=') {
381 opt = &qual_options[i];
391 if (strcmp(s, "none") == 0) {
395 if (strcmp(s, "all") == 0) {
396 for (i = 0; i < MAX_QUALS; i++) {
398 qual_flags[i] &= ~opt->bitflag;
400 qual_flags[i] |= opt->bitflag;
404 for (i = 0; i < MAX_QUALS; i++) {
406 qual_flags[i] |= opt->bitflag;
408 qual_flags[i] &= ~opt->bitflag;
410 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
411 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
412 for (i = 0; i < MAX_QUALS; i++) {
413 if (sysent[i].sys_flags & n) {
415 qual_flags[i] &= ~opt->bitflag;
417 qual_flags[i] |= opt->bitflag;
422 if (opt->qualify(p, opt, not)) {
423 fprintf(stderr, "strace: invalid %s `%s'\n",
424 opt->argument_name, p);
437 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
439 switch (tcp->scno + NR_SYSCALL_BASE) {
444 #if defined SYS_pread && SYS_pread64 != SYS_pread
453 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
454 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
460 #if defined SYS_pwrite && SYS_pwrite64 != SYS_pwrite
469 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
470 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
474 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
475 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
481 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
482 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
489 enum subcall_style { shift_style, deref_style, mask_style, door_style };
491 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
499 const struct subcall subcalls_table[] = {
500 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
502 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
504 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
506 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
510 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
512 const int socket_map [] = {
515 /* SYS_CONNECT */ 98,
516 /* SYS_LISTEN */ 106,
518 /* SYS_GETSOCKNAME */ 150,
519 /* SYS_GETPEERNAME */ 141,
520 /* SYS_SOCKETPAIR */ 135,
523 /* SYS_SENDTO */ 133,
524 /* SYS_RECVFROM */ 125,
525 /* SYS_SHUTDOWN */ 134,
526 /* SYS_SETSOCKOPT */ 105,
527 /* SYS_GETSOCKOPT */ 118,
528 /* SYS_SENDMSG */ 114,
529 /* SYS_RECVMSG */ 113
533 sparc_socket_decode (tcp)
539 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
542 tcp->scno = socket_map [tcp->u_arg [0]-1];
543 n = tcp->u_nargs = sysent [tcp->scno].nargs;
544 addr = tcp->u_arg [1];
545 for (i = 0; i < n; i++){
547 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
549 tcp->u_arg [i] = arg;
550 addr += sizeof (arg);
555 decode_subcall(tcp, subcall, nsubcalls, style)
559 enum subcall_style style;
561 long addr, mask, arg;
566 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
568 tcp->scno = subcall + tcp->u_arg[0];
569 if (sysent[tcp->scno].nargs != -1)
570 tcp->u_nargs = sysent[tcp->scno].nargs;
573 for (i = 0; i < tcp->u_nargs; i++)
574 tcp->u_arg[i] = tcp->u_arg[i + 1];
577 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
579 tcp->scno = subcall + tcp->u_arg[0];
580 addr = tcp->u_arg[1];
581 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
582 if (umove(tcp, addr, &arg) < 0)
587 tcp->u_nargs = sysent[tcp->scno].nargs;
590 mask = (tcp->u_arg[0] >> 8) & 0xff;
591 for (i = 0; mask; i++)
595 tcp->u_arg[0] &= 0xff;
596 tcp->scno = subcall + i;
597 if (sysent[tcp->scno].nargs != -1)
598 tcp->u_nargs = sysent[tcp->scno].nargs;
602 * Oh, yuck. The call code is the *sixth* argument.
603 * (don't you mean the *last* argument? - JH)
605 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
607 tcp->scno = subcall + tcp->u_arg[5];
608 if (sysent[tcp->scno].nargs != -1)
609 tcp->u_nargs = sysent[tcp->scno].nargs;
615 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
616 if (subcalls_table[i].call == tcp->scno) break;
617 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
618 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
619 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
620 for (i = 0; i < tcp->u_nargs; i++)
621 tcp->u_arg[i] = tcp->u_arg[i + 1];
629 struct tcb *tcp_last = NULL;
632 internal_syscall(tcp)
636 * We must always trace a few critical system calls in order to
637 * correctly support following forks in the presence of tracing
640 switch (tcp->scno + NR_SYSCALL_BASE) {
701 internal_wait(tcp, 2);
705 internal_wait(tcp, 3);
715 #ifdef __NR_exit_group
716 case __NR_exit_group:
719 case 252: /* IA-32 __NR_exit_group */
734 #elif defined (POWERPC)
735 static long result,flags;
739 static struct pt_regs regs;
740 #elif defined (ALPHA)
743 #elif defined (SPARC) || defined (SPARC64)
744 static struct regs regs;
745 static unsigned long trap;
749 #elif defined(S390) || defined(S390X)
752 static long syscall_mode;
759 #elif defined(X86_64)
777 #if defined(S390) || defined(S390X)
778 if (tcp->flags & TCB_WAITEXECVE) {
780 * When the execve system call completes successfully, the
781 * new process still has -ENOSYS (old style) or __NR_execve
782 * (new style) in gpr2. We cannot recover the scno again
783 * by disassembly, because the image that executed the
784 * syscall is gone now. Fortunately, we don't want it. We
785 * leave the flag set so that syscall_fixup can fake the
788 if (tcp->flags & TCB_INSYSCALL)
791 * This is the SIGTRAP after execve. We cannot try to read
792 * the system call here either.
794 tcp->flags &= ~TCB_WAITEXECVE;
798 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
801 if (syscall_mode != -ENOSYS) {
803 * Since kernel version 2.5.44 the scno gets passed in gpr2.
808 * Old style of "passing" the scno via the SVC instruction.
811 long opcode, offset_reg, tmp;
813 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
814 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
815 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
816 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
818 if (upeek(pid, PT_PSWADDR, &pc) < 0)
821 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
823 perror("peektext(pc-oneword)");
828 * We have to check if the SVC got executed directly or via an
829 * EXECUTE instruction. In case of EXECUTE it is necessary to do
830 * instruction decoding to derive the system call number.
831 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
832 * so that this doesn't work if a SVC opcode is part of an EXECUTE
833 * opcode. Since there is no way to find out the opcode size this
834 * is the best we can do...
837 if ((opcode & 0xff00) == 0x0a00) {
839 scno = opcode & 0xff;
842 /* SVC got executed by EXECUTE instruction */
845 * Do instruction decoding of EXECUTE. If you really want to
846 * understand this, read the Principles of Operations.
848 svc_addr = (void *) (opcode & 0xfff);
851 offset_reg = (opcode & 0x000f0000) >> 16;
852 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
857 offset_reg = (opcode & 0x0000f000) >> 12;
858 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
862 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
871 offset_reg = (opcode & 0x00f00000) >> 20;
872 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
875 scno = (scno | tmp) & 0xff;
878 #elif defined (POWERPC)
879 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
881 if (!(tcp->flags & TCB_INSYSCALL)) {
882 /* Check if we return from execve. */
883 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
884 tcp->flags &= ~TCB_WAITEXECVE;
889 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
891 #elif defined (X86_64)
892 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
895 if (!(tcp->flags & TCB_INSYSCALL)) {
896 static int currpers=-1;
899 /* Check CS register value. On x86-64 linux it is:
900 * 0x33 for long mode (64 bit)
901 * 0x23 for compatibility mode (32 bit)
902 * It takes only one ptrace and thus doesn't need
905 if (upeek(pid, 8*CS, &val) < 0)
909 case 0x23: currpers = 1; break;
910 case 0x33: currpers = 0; break;
912 fprintf(stderr, "Unknown value CS=0x%02X while "
913 "detecting personality of process "
914 "PID=%d\n", (int)val, pid);
915 currpers = current_personality;
919 /* This version analyzes the opcode of a syscall instruction.
920 * (int 0x80 on i386 vs. syscall on x86-64)
921 * It works, but is too complicated.
923 unsigned long val, rip, i;
925 if(upeek(pid, 8*RIP, &rip)<0)
926 perror("upeek(RIP)");
928 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
932 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
934 printf("ptrace_peektext failed: %s\n",
936 switch (call & 0xffff)
938 /* x86-64: syscall = 0x0f 0x05 */
939 case 0x050f: currpers = 0; break;
940 /* i386: int 0x80 = 0xcd 0x80 */
941 case 0x80cd: currpers = 1; break;
943 currpers = current_personality;
945 "Unknown syscall opcode (0x%04X) while "
946 "detecting personality of process "
947 "PID=%d\n", (int)call, pid);
951 if(currpers != current_personality)
953 char *names[]={"64 bit", "32 bit"};
954 set_personality(currpers);
955 printf("[ Process PID=%d runs in %s mode. ]\n",
956 pid, names[current_personality]);
960 # define IA64_PSR_IS ((long)1 << 34)
961 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
962 ia32 = (psr & IA64_PSR_IS) != 0;
963 if (!(tcp->flags & TCB_INSYSCALL)) {
965 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
968 if (upeek (pid, PT_R15, &scno) < 0)
971 /* Check if we return from execve. */
972 if (tcp->flags & TCB_WAITEXECVE) {
973 tcp->flags &= ~TCB_WAITEXECVE;
977 /* syscall in progress */
978 if (upeek (pid, PT_R8, &r8) < 0)
980 if (upeek (pid, PT_R10, &r10) < 0)
985 * Read complete register set in one go.
987 if (ptrace(PTRACE_GETREGS, pid, NULL, (void *)®s) == -1)
991 * We only need to grab the syscall number on syscall entry.
993 if (regs.ARM_ip == 0) {
995 * Note: we only deal with only 32-bit CPUs here.
997 if (regs.ARM_cpsr & 0x20) {
999 * Get the Thumb-mode system call number
1004 * Get the ARM-mode system call number
1007 scno = ptrace(PTRACE_PEEKTEXT, pid, (void *)(regs.ARM_pc - 4), NULL);
1011 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
1012 tcp->flags &= ~TCB_WAITEXECVE;
1016 if ((scno & 0x0ff00000) != 0x0f900000) {
1017 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
1023 * Fixup the syscall number
1028 if (tcp->flags & TCB_INSYSCALL) {
1029 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
1030 tcp->flags &= ~TCB_INSYSCALL;
1033 if (!(tcp->flags & TCB_INSYSCALL)) {
1034 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1035 tcp->flags |= TCB_INSYSCALL;
1038 #elif defined (M68K)
1039 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
1041 #elif defined (MIPS)
1042 if (upeek(pid, REG_A3, &a3) < 0)
1045 if(!(tcp->flags & TCB_INSYSCALL)) {
1046 if (upeek(pid, REG_V0, &scno) < 0)
1049 if (scno < 0 || scno > nsyscalls) {
1050 if(a3 == 0 || a3 == -1) {
1052 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
1057 if (upeek(pid, REG_V0, &r2) < 0)
1060 #elif defined (ALPHA)
1061 if (upeek(pid, REG_A3, &a3) < 0)
1064 if (!(tcp->flags & TCB_INSYSCALL)) {
1065 if (upeek(pid, REG_R0, &scno) < 0)
1068 /* Check if we return from execve. */
1069 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1070 tcp->flags &= ~TCB_WAITEXECVE;
1075 * Do some sanity checks to figure out if it's
1076 * really a syscall entry
1078 if (scno < 0 || scno > nsyscalls) {
1079 if (a3 == 0 || a3 == -1) {
1081 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
1087 if (upeek(pid, REG_R0, &r0) < 0)
1090 #elif defined (SPARC) || defined (SPARC64)
1091 /* Everything we need is in the current register set. */
1092 if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0)
1095 /* If we are entering, then disassemble the syscall trap. */
1096 if (!(tcp->flags & TCB_INSYSCALL)) {
1097 /* Retrieve the syscall trap instruction. */
1099 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
1100 #if defined(SPARC64)
1106 /* Disassemble the trap to see what personality to use. */
1109 /* Linux/SPARC syscall trap. */
1113 /* Linux/SPARC64 syscall trap. */
1117 /* SunOS syscall trap. (pers 1) */
1118 fprintf(stderr,"syscall: SunOS no support\n");
1121 /* Solaris 2.x syscall trap. (per 2) */
1125 /* NetBSD/FreeBSD syscall trap. */
1126 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1129 /* Solaris 2.x gettimeofday */
1133 /* Unknown syscall trap. */
1134 if(tcp->flags & TCB_WAITEXECVE) {
1135 tcp->flags &= ~TCB_WAITEXECVE;
1138 #if defined (SPARC64)
1139 fprintf(stderr,"syscall: unknown syscall trap %08lx %016lx\n", trap, regs.r_tpc);
1141 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
1146 /* Extract the system call number from the registers. */
1147 if (trap == 0x91d02027)
1153 memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0));
1157 if (upeek(pid, PT_GR20, &scno) < 0)
1159 if (!(tcp->flags & TCB_INSYSCALL)) {
1160 /* Check if we return from execve. */
1161 if ((tcp->flags & TCB_WAITEXECVE)) {
1162 tcp->flags &= ~TCB_WAITEXECVE;
1168 * In the new syscall ABI, the system call number is in R3.
1170 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1174 /* Odd as it may seem, a glibc bug has been known to cause
1175 glibc to issue bogus negative syscall numbers. So for
1176 our purposes, make strace print what it *should* have been */
1177 long correct_scno = (scno & 0xff);
1180 "Detected glibc bug: bogus system call number = %ld, "
1181 "correcting to %ld\n",
1184 scno = correct_scno;
1188 if (!(tcp->flags & TCB_INSYSCALL)) {
1189 /* Check if we return from execve. */
1190 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1191 tcp->flags &= ~TCB_WAITEXECVE;
1196 if (upeek(pid, REG_SYSCALL, &scno) < 0)
1200 if (!(tcp->flags & TCB_INSYSCALL)) {
1201 /* Check if we return from execve. */
1202 if (tcp->flags & TCB_WAITEXECVE) {
1203 tcp->flags &= ~TCB_WAITEXECVE;
1210 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1213 /* new syscall ABI returns result in R0 */
1214 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1217 /* ABI defines result returned in r9 */
1218 if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
1223 #ifdef HAVE_PR_SYSCALL
1224 scno = tcp->status.PR_SYSCALL;
1225 #else /* !HAVE_PR_SYSCALL */
1227 scno = tcp->status.PR_WHAT;
1229 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1233 switch (regs.r_eax) {
1236 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1242 #endif /* FREEBSD */
1243 #endif /* !HAVE_PR_SYSCALL */
1244 #endif /* USE_PROCFS */
1245 if (!(tcp->flags & TCB_INSYSCALL))
1257 #else /* USE_PROCFS */
1258 int scno = tcp->scno;
1260 if (!(tcp->flags & TCB_INSYSCALL)) {
1261 if (tcp->status.PR_WHY != PR_SYSENTRY) {
1265 || scno == SYS_vfork
1266 #endif /* SYS_vfork */
1268 || scno == SYS_fork1
1269 #endif /* SYS_fork1 */
1271 || scno == SYS_forkall
1272 #endif /* SYS_forkall */
1274 || scno == SYS_rfork1
1275 #endif /* SYS_fork1 */
1277 || scno == SYS_rforkall
1278 #endif /* SYS_rforkall */
1280 /* We are returning in the child, fake it. */
1281 tcp->status.PR_WHY = PR_SYSENTRY;
1283 tcp->status.PR_WHY = PR_SYSEXIT;
1286 fprintf(stderr, "syscall: missing entry\n");
1287 tcp->flags |= TCB_INSYSCALL;
1292 if (tcp->status.PR_WHY != PR_SYSEXIT) {
1293 fprintf(stderr, "syscall: missing exit\n");
1294 tcp->flags &= ~TCB_INSYSCALL;
1297 #endif /* USE_PROCFS */
1299 if (!(tcp->flags & TCB_INSYSCALL)) {
1301 fprintf(stderr, "syscall: missing entry\n");
1302 tcp->flags |= TCB_INSYSCALL;
1309 * This happens when a signal handler
1310 * for a signal which interrupted a
1311 * a system call makes another system call.
1313 fprintf(stderr, "syscall: missing exit\n");
1315 tcp->flags &= ~TCB_INSYSCALL;
1321 if (upeek(pid, 4*EAX, &eax) < 0)
1323 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1325 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1328 #elif defined (X86_64)
1329 if (upeek(pid, 8*RAX, &rax) < 0)
1331 if (current_personality == 1)
1332 rax = (long int)(int)rax; /* sign extend from 32 bits */
1333 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1335 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1338 #elif defined (S390) || defined (S390X)
1339 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1341 if (syscall_mode != -ENOSYS)
1342 syscall_mode = tcp->scno;
1343 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
1345 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1348 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1349 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1350 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1352 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1353 * flag set for the post-execve SIGTRAP to see and reset.
1357 #elif defined (POWERPC)
1358 # define SO_MASK 0x10000000
1359 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1361 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
1363 if (flags & SO_MASK)
1365 #elif defined (M68K)
1366 if (upeek(pid, 4*PT_D0, &d0) < 0)
1368 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1370 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1377 #elif defined (HPPA)
1378 if (upeek(pid, PT_GR28, &r28) < 0)
1381 if (upeek(pid, PT_R10, &r10) < 0)
1383 if (upeek(pid, PT_R8, &r8) < 0)
1385 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1387 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1401 #if defined(S390) || defined(S390X)
1402 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1410 #else /* !S390 && !S390X */
1412 if (eax < 0 && -eax < nerrnos) {
1422 if (rax < 0 && -rax < nerrnos) {
1436 if (err < 0 && -err < nerrnos) {
1464 if (result && (unsigned long) -result < nerrnos) {
1469 tcp->u_rval = result;
1472 #else /* !POWERPC */
1474 if (d0 && (unsigned) -d0 < nerrnos) {
1484 if (regs.ARM_r0 && (unsigned) -regs.ARM_r0 < nerrnos) {
1486 u_error = -regs.ARM_r0;
1489 tcp->u_rval = regs.ARM_r0;
1504 if (regs.r_psr & PSR_C) {
1506 u_error = regs.r_o0;
1509 tcp->u_rval = regs.r_o0;
1514 if (regs.r_tstate & 0x1100000000UL) {
1516 u_error = regs.r_o0;
1519 tcp->u_rval = regs.r_o0;
1522 #else /* !SPARC64 */
1524 if (r28 && (unsigned) -r28 < nerrnos) {
1534 /* interpret R0 as return value or error number */
1535 if (r0 && (unsigned) -r0 < nerrnos) {
1545 /* interpret result as return value or error number */
1546 if (r9 && (unsigned) -r9 < nerrnos) {
1558 #endif /* SPARC64 */
1562 #endif /* POWERPC */
1567 #endif /* S390 || S390X */
1570 /* get error code from user struct */
1571 if (upeek(pid, uoff(u_error), &u_error) < 0)
1573 u_error >>= 24; /* u_error is a char */
1575 /* get system call return value */
1576 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1581 /* Judicious guessing goes a long way. */
1582 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1584 u_error = tcp->status.pr_reg[R_O0];
1587 tcp->u_rval = tcp->status.pr_reg[R_O0];
1592 /* Wanna know how to kill an hour single-stepping? */
1593 if (tcp->status.PR_REG[EFL] & 0x1) {
1595 u_error = tcp->status.PR_REG[EAX];
1598 tcp->u_rval = tcp->status.PR_REG[EAX];
1599 #ifdef HAVE_LONG_LONG
1601 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1602 tcp->status.PR_REG[EAX];
1608 /* Wanna know how to kill an hour single-stepping? */
1609 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1611 u_error = tcp->status.PR_REG[RAX];
1614 tcp->u_rval = tcp->status.PR_REG[RAX];
1619 if (tcp->status.pr_reg[CTX_A3]) {
1621 u_error = tcp->status.pr_reg[CTX_V0];
1624 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1630 if (regs.r_eflags & PSL_C) {
1632 u_error = regs.r_eax;
1634 tcp->u_rval = regs.r_eax;
1636 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1639 #endif /* FREEBSD */
1640 tcp->u_error = u_error;
1645 force_result(tcp, error, rval)
1651 #if defined(S390) || defined(S390X)
1652 gpr2 = error ? -error : rval;
1653 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1655 #else /* !S390 && !S390X */
1657 eax = error ? -error : rval;
1658 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1662 rax = error ? -error : rval;
1663 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 8), rax) < 0)
1668 r8 = error ? -error : rval;
1669 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1681 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1682 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1695 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1696 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1700 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1710 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1711 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
1713 #else /* !POWERPC */
1715 d0 = error ? -error : rval;
1716 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1720 regs.ARM_r0 = error ? -error : rval;
1721 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), regs.ARM_r0) < 0)
1733 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1734 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1738 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1741 regs.r_psr |= PSR_C;
1745 regs.r_psr &= ~PSR_C;
1748 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1752 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1755 regs.r_tstate |= 0x1100000000UL;
1759 regs.r_tstate &= ~0x1100000000UL;
1762 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1764 #else /* !SPARC64 */
1766 r28 = error ? -error : rval;
1767 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1771 r0 = error ? -error : rval;
1772 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1776 r9 = error ? -error : rval;
1777 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1783 #endif /* SPARC64 */
1787 #endif /* POWERPC */
1792 #endif /* S390 || S390X */
1795 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1797 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1805 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1810 regs.r_eflags |= PSL_C;
1814 regs.r_eflags &= ~PSL_C;
1817 if (pwrite(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1821 #endif /* FREEBSD */
1823 /* All branches reach here on success (only). */
1824 tcp->u_error = error;
1829 int syscall_enter(tcp)
1834 #endif /* !USE_PROCFS */
1836 #if defined(S390) || defined(S390X)
1839 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1840 tcp->u_nargs = sysent[tcp->scno].nargs;
1842 tcp->u_nargs = MAX_ARGS;
1843 for (i = 0; i < tcp->u_nargs; i++) {
1844 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
1848 #elif defined (ALPHA)
1851 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1852 tcp->u_nargs = sysent[tcp->scno].nargs;
1854 tcp->u_nargs = MAX_ARGS;
1855 for (i = 0; i < tcp->u_nargs; i++) {
1856 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1857 * for scno somewhere above here!
1859 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1863 #elif defined (IA64)
1866 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1867 /* be backwards compatible with kernel < 2.4.4... */
1869 # define PT_RBS_END PT_AR_BSP
1872 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
1874 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1877 sof = (cfm >> 0) & 0x7f;
1878 sol = (cfm >> 7) & 0x7f;
1879 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1881 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1882 && sysent[tcp->scno].nargs != -1)
1883 tcp->u_nargs = sysent[tcp->scno].nargs;
1885 tcp->u_nargs = MAX_ARGS;
1886 for (i = 0; i < tcp->u_nargs; ++i) {
1887 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1888 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1894 if (/* EBX = out0 */
1895 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1897 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1899 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1901 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1903 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1905 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1908 for (i = 0; i < 6; ++i)
1909 /* truncate away IVE sign-extension */
1910 tcp->u_arg[i] &= 0xffffffff;
1912 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1913 && sysent[tcp->scno].nargs != -1)
1914 tcp->u_nargs = sysent[tcp->scno].nargs;
1919 #elif defined (MIPS)
1924 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1925 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1927 nargs = tcp->u_nargs = MAX_ARGS;
1929 if(upeek(pid, REG_SP, &sp) < 0)
1931 for(i = 0; i < 4; i++) {
1932 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1935 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1936 (char *)(tcp->u_arg + 4));
1938 for(i = 0; i < nargs; i++) {
1939 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1944 #elif defined (POWERPC)
1946 #define PT_ORIG_R3 34
1950 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1951 tcp->u_nargs = sysent[tcp->scno].nargs;
1953 tcp->u_nargs = MAX_ARGS;
1954 for (i = 0; i < tcp->u_nargs; i++) {
1955 if (upeek(pid, (i==0) ?
1956 (sizeof(unsigned long)*PT_ORIG_R3) :
1957 ((i+PT_R3)*sizeof(unsigned long)),
1958 &tcp->u_arg[i]) < 0)
1962 #elif defined (SPARC) || defined (SPARC64)
1966 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1967 tcp->u_nargs = sysent[tcp->scno].nargs;
1969 tcp->u_nargs = MAX_ARGS;
1970 for (i = 0; i < tcp->u_nargs; i++)
1971 tcp->u_arg[i] = *((®s.r_o0) + i);
1973 #elif defined (HPPA)
1977 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1978 tcp->u_nargs = sysent[tcp->scno].nargs;
1980 tcp->u_nargs = MAX_ARGS;
1981 for (i = 0; i < tcp->u_nargs; i++) {
1982 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1990 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1991 tcp->u_nargs = sysent[tcp->scno].nargs;
1993 tcp->u_nargs = MAX_ARGS;
1994 for (i = 0; i < tcp->u_nargs; i++)
1995 tcp->u_arg[i] = regs.uregs[i];
2000 static int syscall_regs[] = {
2001 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
2002 REG_REG0, REG_REG0+1, REG_REG0+2
2005 tcp->u_nargs = sysent[tcp->scno].nargs;
2006 for (i = 0; i < tcp->u_nargs; i++) {
2007 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
2014 /* Registers used by SH5 Linux system calls for parameters */
2015 static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
2018 * TODO: should also check that the number of arguments encoded
2019 * in the trap number matches the number strace expects.
2022 assert(sysent[tcp->scno].nargs <
2023 sizeof(syscall_regs)/sizeof(syscall_regs[0]));
2026 tcp->u_nargs = sysent[tcp->scno].nargs;
2027 for (i = 0; i < tcp->u_nargs; i++) {
2028 if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
2033 #elif defined(X86_64)
2036 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
2037 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
2038 {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */
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 = MAX_ARGS;
2045 for (i = 0; i < tcp->u_nargs; i++) {
2046 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
2050 #else /* Other architecture (like i386) (32bits specific) */
2053 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2054 tcp->u_nargs = sysent[tcp->scno].nargs;
2056 tcp->u_nargs = MAX_ARGS;
2057 for (i = 0; i < tcp->u_nargs; i++) {
2058 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
2067 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2068 tcp->u_nargs = sysent[tcp->scno].nargs;
2070 tcp->u_nargs = MAX_ARGS;
2071 for (i = 0; i < tcp->u_nargs; i++) {
2074 if (upeek(pid, uoff(u_arg[0]) +
2075 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
2083 * SGI is broken: even though it has pr_sysarg, it doesn't
2084 * set them on system call entry. Get a clue.
2086 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2087 tcp->u_nargs = sysent[tcp->scno].nargs;
2089 tcp->u_nargs = tcp->status.pr_nsysarg;
2090 if (tcp->u_nargs > 4) {
2091 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2092 4*sizeof(tcp->u_arg[0]));
2093 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
2094 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
2097 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2098 tcp->u_nargs*sizeof(tcp->u_arg[0]));
2102 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
2104 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2105 tcp->u_nargs = sysent[tcp->scno].nargs;
2107 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
2108 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2109 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2110 #elif defined (HAVE_PR_SYSCALL)
2111 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2112 tcp->u_nargs = sysent[tcp->scno].nargs;
2114 tcp->u_nargs = tcp->status.pr_nsysarg;
2117 for (i = 0; i < tcp->u_nargs; i++)
2118 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
2120 #elif defined (I386)
2121 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2122 tcp->u_nargs = sysent[tcp->scno].nargs;
2125 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2126 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2128 I DONT KNOW WHAT TO DO
2129 #endif /* !HAVE_PR_SYSCALL */
2132 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2133 sysent[tcp->scno].nargs > tcp->status.val)
2134 tcp->u_nargs = sysent[tcp->scno].nargs;
2136 tcp->u_nargs = tcp->status.val;
2137 if (tcp->u_nargs < 0)
2139 if (tcp->u_nargs > MAX_ARGS)
2140 tcp->u_nargs = MAX_ARGS;
2141 switch(regs.r_eax) {
2143 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2144 regs.r_esp + sizeof(int) + sizeof(quad_t));
2147 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2148 regs.r_esp + 2 * sizeof(int));
2151 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2152 regs.r_esp + sizeof(int));
2155 #endif /* FREEBSD */
2167 /* Measure the exit time as early as possible to avoid errors. */
2168 if (dtime && (tcp->flags & TCB_INSYSCALL))
2169 gettimeofday(&tv, NULL);
2171 res = get_scno(tcp);
2175 res = syscall_fixup(tcp);
2179 if (tcp->flags & TCB_INSYSCALL) {
2181 res = get_error(tcp);
2185 internal_syscall(tcp);
2186 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2187 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2188 tcp->flags &= ~TCB_INSYSCALL;
2192 if (tcp->flags & TCB_REPRINT) {
2195 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2196 tprintf("syscall_%lu", tcp->scno);
2198 tprintf("%s", sysent[tcp->scno].sys_name);
2199 tprintf(" resumed> ");
2202 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
2203 call_count[tcp->scno]++;
2205 error_count[tcp->scno]++;
2206 tv_sub(&tv, &tv, &tcp->etime);
2208 if (tv_cmp(&tv, &tcp->dtime) > 0) {
2209 static struct timeval one_tick;
2210 if (one_tick.tv_usec == 0) {
2211 /* Initialize it. */
2212 struct itimerval it;
2213 memset(&it, 0, sizeof it);
2214 it.it_interval.tv_usec = 1;
2215 setitimer(ITIMER_REAL, &it, NULL);
2216 getitimer(ITIMER_REAL, &it);
2217 one_tick = it.it_interval;
2220 if (tv_nz(&tcp->dtime))
2222 else if (tv_cmp(&tv, &one_tick) > 0) {
2223 if (tv_cmp(&shortest, &one_tick) < 0)
2230 if (tv_cmp(&tv, &shortest) < 0)
2232 tv_add(&tv_count[tcp->scno],
2233 &tv_count[tcp->scno], &tv);
2234 tcp->flags &= ~TCB_INSYSCALL;
2238 if (tcp->scno >= nsyscalls || tcp->scno < 0
2239 || (qual_flags[tcp->scno] & QUAL_RAW))
2240 sys_res = printargs(tcp);
2242 if (not_failing_only && tcp->u_error)
2243 return 0; /* ignore failed syscalls */
2244 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2246 u_error = tcp->u_error;
2249 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2250 qual_flags[tcp->scno] & QUAL_RAW) {
2252 tprintf("= -1 (errno %ld)", u_error);
2254 tprintf("= %#lx", tcp->u_rval);
2256 else if (!(sys_res & RVAL_NONE) && u_error) {
2260 tprintf("= ? ERESTARTSYS (To be restarted)");
2262 case ERESTARTNOINTR:
2263 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2265 case ERESTARTNOHAND:
2266 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2268 case ERESTART_RESTARTBLOCK:
2269 tprintf("= ? ERESTART_RESTARTBLOCK (To be restarted)");
2275 tprintf("E??? (errno %ld)", u_error);
2276 else if (u_error < nerrnos)
2277 tprintf("%s (%s)", errnoent[u_error],
2280 tprintf("ERRNO_%ld (%s)", u_error,
2286 if (sys_res & RVAL_NONE)
2289 switch (sys_res & RVAL_MASK) {
2291 tprintf("= %#lx", tcp->u_rval);
2294 tprintf("= %#lo", tcp->u_rval);
2297 tprintf("= %lu", tcp->u_rval);
2300 tprintf("= %ld", tcp->u_rval);
2302 #ifdef HAVE_LONG_LONG
2304 tprintf("= %#llx", tcp->u_lrval);
2307 tprintf("= %#llo", tcp->u_lrval);
2309 case RVAL_LUDECIMAL:
2310 tprintf("= %llu", tcp->u_lrval);
2313 tprintf("= %lld", tcp->u_lrval);
2318 "invalid rval format\n");
2322 if ((sys_res & RVAL_STR) && tcp->auxstr)
2323 tprintf(" (%s)", tcp->auxstr);
2326 tv_sub(&tv, &tv, &tcp->etime);
2327 tprintf(" <%ld.%06ld>",
2328 (long) tv.tv_sec, (long) tv.tv_usec);
2333 if (fflush(tcp->outf) == EOF)
2335 tcp->flags &= ~TCB_INSYSCALL;
2339 /* Entering system call */
2340 res = syscall_enter(tcp);
2344 switch (tcp->scno + NR_SYSCALL_BASE) {
2346 #if !defined (ALPHA) && !defined(SPARC) && !defined(SPARC64) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
2347 case SYS_socketcall:
2348 decode_subcall(tcp, SYS_socket_subcall,
2349 SYS_socket_nsubcalls, deref_style);
2352 decode_subcall(tcp, SYS_ipc_subcall,
2353 SYS_ipc_nsubcalls, shift_style);
2355 #endif /* !ALPHA && !MIPS && !SPARC && !SPARC64 && !HPPA && !X86_64 */
2356 #if defined (SPARC) || defined (SPARC64)
2357 case SYS_socketcall:
2358 sparc_socket_decode (tcp);
2363 #ifdef SYS_pgrpsys_subcall
2365 decode_subcall(tcp, SYS_pgrpsys_subcall,
2366 SYS_pgrpsys_nsubcalls, shift_style);
2368 #endif /* SYS_pgrpsys_subcall */
2369 #ifdef SYS_sigcall_subcall
2371 decode_subcall(tcp, SYS_sigcall_subcall,
2372 SYS_sigcall_nsubcalls, mask_style);
2374 #endif /* SYS_sigcall_subcall */
2376 decode_subcall(tcp, SYS_msgsys_subcall,
2377 SYS_msgsys_nsubcalls, shift_style);
2380 decode_subcall(tcp, SYS_shmsys_subcall,
2381 SYS_shmsys_nsubcalls, shift_style);
2384 decode_subcall(tcp, SYS_semsys_subcall,
2385 SYS_semsys_nsubcalls, shift_style);
2389 decode_subcall(tcp, SYS_utssys_subcall,
2390 SYS_utssys_nsubcalls, shift_style);
2394 decode_subcall(tcp, SYS_sysfs_subcall,
2395 SYS_sysfs_nsubcalls, shift_style);
2398 decode_subcall(tcp, SYS_spcall_subcall,
2399 SYS_spcall_nsubcalls, shift_style);
2401 #ifdef SYS_context_subcall
2403 decode_subcall(tcp, SYS_context_subcall,
2404 SYS_context_nsubcalls, shift_style);
2406 #endif /* SYS_context_subcall */
2407 #ifdef SYS_door_subcall
2409 decode_subcall(tcp, SYS_door_subcall,
2410 SYS_door_nsubcalls, door_style);
2412 #endif /* SYS_door_subcall */
2413 #ifdef SYS_kaio_subcall
2415 decode_subcall(tcp, SYS_kaio_subcall,
2416 SYS_kaio_nsubcalls, shift_style);
2424 decode_subcall(tcp, 0, 0, table_style);
2429 decode_subcall(tcp, SYS_semsys_subcall,
2430 SYS_semsys_nsubcalls, shift_style);
2433 decode_subcall(tcp, SYS_msgsys_subcall,
2434 SYS_msgsys_nsubcalls, shift_style);
2437 decode_subcall(tcp, SYS_shmsys_subcall,
2438 SYS_shmsys_nsubcalls, shift_style);
2443 internal_syscall(tcp);
2444 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2445 tcp->flags |= TCB_INSYSCALL;
2450 gettimeofday(&tcp->etime, NULL);
2451 tcp->flags |= TCB_INSYSCALL;
2456 tcp->flags &= ~TCB_REPRINT;
2458 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2459 tprintf("syscall_%lu(", tcp->scno);
2461 tprintf("%s(", sysent[tcp->scno].sys_name);
2462 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2463 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2464 sys_res = printargs(tcp);
2466 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2467 if (fflush(tcp->outf) == EOF)
2469 tcp->flags |= TCB_INSYSCALL;
2470 /* Measure the entrance time as late as possible to avoid errors. */
2472 gettimeofday(&tcp->etime, NULL);
2480 if (entering(tcp)) {
2483 for (i = 0; i < tcp->u_nargs; i++)
2484 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2496 #if defined (SPARC) || defined (SPARC64)
2498 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
2502 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2504 #endif /* SPARC || SPARC64 */
2506 if (upeek(tcp->pid, PT_R9, &val) < 0)
2511 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2517 val = tcp->status.PR_REG[R_O1];
2520 val = tcp->status.PR_REG[EDX];
2523 val = tcp->status.PR_REG[RDX];
2526 val = tcp->status.PR_REG[CTX_V1];
2531 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
2538 * Apparently, indirect system calls have already be converted by ptrace(2),
2539 * so if you see "indir" this program has gone astray.
2547 if (entering(tcp)) {
2548 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2549 fprintf(stderr, "Bogus syscall: %u\n", scno);
2552 nargs = sysent[scno].nargs;
2553 tprintf("%s", sysent[scno].sys_name);
2554 for (i = 0; i < nargs; i++)
2555 tprintf(", %#lx", tcp->u_arg[i+1]);
2565 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2573 return strcmp(sysent[*((int *) a)].sys_name,
2574 sysent[*((int *) b)].sys_name);
2582 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2584 return (m < n) ? 1 : (m > n) ? -1 : 0;
2587 static int (*sortfun)();
2588 static struct timeval overhead = { -1, -1 };
2594 if (strcmp(sortby, "time") == 0)
2596 else if (strcmp(sortby, "calls") == 0)
2597 sortfun = count_cmp;
2598 else if (strcmp(sortby, "name") == 0)
2599 sortfun = syscall_cmp;
2600 else if (strcmp(sortby, "nothing") == 0)
2603 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2608 void set_overhead(n)
2611 overhead.tv_sec = n / 1000000;
2612 overhead.tv_usec = n % 1000000;
2620 int call_cum, error_cum;
2621 struct timeval tv_cum, dtv;
2623 char *dashes = "-------------------------";
2626 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2627 if (overhead.tv_sec == -1) {
2628 tv_mul(&overhead, &shortest, 8);
2629 tv_div(&overhead, &overhead, 10);
2631 for (i = 0; i < nsyscalls; i++) {
2632 sorted_count[i] = i;
2633 if (call_count[i] == 0)
2635 tv_mul(&dtv, &overhead, call_count[i]);
2636 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2637 call_cum += call_count[i];
2638 error_cum += error_count[i];
2639 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2642 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2643 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2644 "% time", "seconds", "usecs/call",
2645 "calls", "errors", "syscall");
2646 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2647 dashes, dashes, dashes, dashes, dashes, dashes);
2648 for (i = 0; i < nsyscalls; i++) {
2649 j = sorted_count[i];
2650 if (call_count[j] == 0)
2652 tv_div(&dtv, &tv_count[j], call_count[j]);
2654 sprintf(error_str, "%d", error_count[j]);
2656 error_str[0] = '\0';
2657 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2658 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2659 percent, (long) tv_count[j].tv_sec,
2660 (long) tv_count[j].tv_usec,
2661 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2662 call_count[j], error_str, sysent[j].sys_name);
2664 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2665 dashes, dashes, dashes, dashes, dashes, dashes);
2667 sprintf(error_str, "%d", error_cum);
2669 error_str[0] = '\0';
2670 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2671 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2672 call_cum, error_str, "total");