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 #ifndef PTRACE_PEEKUSR
48 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
50 #elif defined(HAVE_LINUX_PTRACE_H)
52 # ifdef HAVE_STRUCT_IA64_FPREG
53 # define ia64_fpreg XXX_ia64_fpreg
55 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
56 # define pt_all_user_regs XXX_pt_all_user_regs
58 #include <linux/ptrace.h>
60 # undef pt_all_user_regs
63 #if defined (LINUX) && defined (SPARC64)
64 # undef PTRACE_GETREGS
65 # define PTRACE_GETREGS PTRACE_GETREGS64
66 # undef PTRACE_SETREGS
67 # define PTRACE_SETREGS PTRACE_SETREGS64
68 #endif /* LINUX && SPARC64 */
70 #if defined(LINUX) && defined(IA64)
71 # include <asm/ptrace_offsets.h>
75 #define NR_SYSCALL_BASE 0
78 #define ERESTARTSYS 512
80 #ifndef ERESTARTNOINTR
81 #define ERESTARTNOINTR 513
83 #ifndef ERESTARTNOHAND
84 #define ERESTARTNOHAND 514 /* restart if no handler.. */
87 #define ENOIOCTLCMD 515 /* No ioctl command */
89 #ifndef ERESTART_RESTARTBLOCK
90 #define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
93 #warning: NSIG is not defined, using 32
97 /* Ugh. Is this really correct? ARM has no RT signals?! */
100 #undef NR_SYSCALL_BASE
101 #define NR_SYSCALL_BASE __NR_SYSCALL_BASE
107 /* Define these shorthand notations to simplify the syscallent files. */
108 #define TD TRACE_DESC
109 #define TF TRACE_FILE
111 #define TN TRACE_NETWORK
112 #define TP TRACE_PROCESS
113 #define TS TRACE_SIGNAL
114 #define NF SYSCALL_NEVER_FAILS
117 static const struct sysent sysent0[] = {
118 #include "syscallent.h"
121 #if SUPPORTED_PERSONALITIES >= 2
122 static const struct sysent sysent1[] = {
123 #include "syscallent1.h"
127 #if SUPPORTED_PERSONALITIES >= 3
128 static const struct sysent sysent2[] = {
129 #include "syscallent2.h"
133 /* Now undef them since short defines cause wicked namespace pollution. */
145 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
146 * program `ioctlsort', such that the list is sorted by the `code' field.
147 * This has the side-effect of resolving the _IO.. macros into
148 * plain integers, eliminating the need to include here everything
153 static const char *const errnoent0[] = {
154 #include "errnoent.h"
156 static const char *const signalent0[] = {
157 #include "signalent.h"
159 static const struct ioctlent ioctlent0[] = {
160 #include "ioctlent.h"
162 enum { nsyscalls0 = ARRAY_SIZE(sysent0) };
163 enum { nerrnos0 = ARRAY_SIZE(errnoent0) };
164 enum { nsignals0 = ARRAY_SIZE(signalent0) };
165 enum { nioctlents0 = ARRAY_SIZE(ioctlent0) };
166 int qual_flags0[MAX_QUALS];
168 #if SUPPORTED_PERSONALITIES >= 2
169 static const char *const errnoent1[] = {
170 #include "errnoent1.h"
172 static const char *const signalent1[] = {
173 #include "signalent1.h"
175 static const struct ioctlent ioctlent1[] = {
176 #include "ioctlent1.h"
178 enum { nsyscalls1 = ARRAY_SIZE(sysent1) };
179 enum { nerrnos1 = ARRAY_SIZE(errnoent1) };
180 enum { nsignals1 = ARRAY_SIZE(signalent1) };
181 enum { nioctlents1 = ARRAY_SIZE(ioctlent1) };
182 int qual_flags1[MAX_QUALS];
185 #if SUPPORTED_PERSONALITIES >= 3
186 static const char *const errnoent2[] = {
187 #include "errnoent2.h"
189 static const char *const signalent2[] = {
190 #include "signalent2.h"
192 static const struct ioctlent ioctlent2[] = {
193 #include "ioctlent2.h"
195 enum { nsyscalls2 = ARRAY_SIZE(sysent2) };
196 enum { nerrnos2 = ARRAY_SIZE(errnoent2) };
197 enum { nsignals2 = ARRAY_SIZE(signalent2) };
198 enum { nioctlents2 = ARRAY_SIZE(ioctlent2) };
199 int qual_flags2[MAX_QUALS];
203 const struct sysent *sysent;
204 const char *const *errnoent;
205 const char *const *signalent;
206 const struct ioctlent *ioctlent;
213 int current_personality;
215 #ifndef PERSONALITY0_WORDSIZE
216 # define PERSONALITY0_WORDSIZE sizeof(long)
218 const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
219 PERSONALITY0_WORDSIZE,
220 #if SUPPORTED_PERSONALITIES > 1
221 PERSONALITY1_WORDSIZE,
223 #if SUPPORTED_PERSONALITIES > 2
224 PERSONALITY2_WORDSIZE,
229 set_personality(int personality)
231 switch (personality) {
233 errnoent = errnoent0;
236 nsyscalls = nsyscalls0;
237 ioctlent = ioctlent0;
238 nioctlents = nioctlents0;
239 signalent = signalent0;
240 nsignals = nsignals0;
241 qual_flags = qual_flags0;
244 #if SUPPORTED_PERSONALITIES >= 2
246 errnoent = errnoent1;
249 nsyscalls = nsyscalls1;
250 ioctlent = ioctlent1;
251 nioctlents = nioctlents1;
252 signalent = signalent1;
253 nsignals = nsignals1;
254 qual_flags = qual_flags1;
258 #if SUPPORTED_PERSONALITIES >= 3
260 errnoent = errnoent2;
263 nsyscalls = nsyscalls2;
264 ioctlent = ioctlent2;
265 nioctlents = nioctlents2;
266 signalent = signalent2;
267 nsignals = nsignals2;
268 qual_flags = qual_flags2;
273 current_personality = personality;
277 static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
279 static const struct qual_options {
281 const char *option_name;
282 int (*qualify)(const char *, int, int);
283 const char *argument_name;
285 { QUAL_TRACE, "trace", qual_syscall, "system call" },
286 { QUAL_TRACE, "t", qual_syscall, "system call" },
287 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
288 { QUAL_ABBREV, "a", qual_syscall, "system call" },
289 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
290 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
291 { QUAL_RAW, "raw", qual_syscall, "system call" },
292 { QUAL_RAW, "x", qual_syscall, "system call" },
293 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
294 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
295 { QUAL_SIGNAL, "s", qual_signal, "signal" },
296 { QUAL_FAULT, "fault", qual_fault, "fault" },
297 { QUAL_FAULT, "faults", qual_fault, "fault" },
298 { QUAL_FAULT, "m", qual_fault, "fault" },
299 { QUAL_READ, "read", qual_desc, "descriptor" },
300 { QUAL_READ, "reads", qual_desc, "descriptor" },
301 { QUAL_READ, "r", qual_desc, "descriptor" },
302 { QUAL_WRITE, "write", qual_desc, "descriptor" },
303 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
304 { QUAL_WRITE, "w", qual_desc, "descriptor" },
305 { 0, NULL, NULL, NULL },
309 qualify_one(int n, int bitflag, int not, int pers)
311 if (pers == 0 || pers < 0) {
313 qual_flags0[n] &= ~bitflag;
315 qual_flags0[n] |= bitflag;
318 #if SUPPORTED_PERSONALITIES >= 2
319 if (pers == 1 || pers < 0) {
321 qual_flags1[n] &= ~bitflag;
323 qual_flags1[n] |= bitflag;
325 #endif /* SUPPORTED_PERSONALITIES >= 2 */
327 #if SUPPORTED_PERSONALITIES >= 3
328 if (pers == 2 || pers < 0) {
330 qual_flags2[n] &= ~bitflag;
332 qual_flags2[n] |= bitflag;
334 #endif /* SUPPORTED_PERSONALITIES >= 3 */
338 qual_syscall(const char *s, int bitflag, int not)
343 if (isdigit((unsigned char)*s)) {
345 if (i < 0 || i >= MAX_QUALS)
347 qualify_one(i, bitflag, not, -1);
350 for (i = 0; i < nsyscalls0; i++)
351 if (strcmp(s, sysent0[i].sys_name) == 0) {
352 qualify_one(i, bitflag, not, 0);
356 #if SUPPORTED_PERSONALITIES >= 2
357 for (i = 0; i < nsyscalls1; i++)
358 if (strcmp(s, sysent1[i].sys_name) == 0) {
359 qualify_one(i, bitflag, not, 1);
362 #endif /* SUPPORTED_PERSONALITIES >= 2 */
364 #if SUPPORTED_PERSONALITIES >= 3
365 for (i = 0; i < nsyscalls2; i++)
366 if (strcmp(s, sysent2[i].sys_name) == 0) {
367 qualify_one(i, bitflag, not, 2);
370 #endif /* SUPPORTED_PERSONALITIES >= 3 */
376 qual_signal(const char *s, int bitflag, int not)
381 if (isdigit((unsigned char)*s)) {
383 if (signo < 0 || signo >= MAX_QUALS)
385 qualify_one(signo, bitflag, not, -1);
388 if (strlen(s) >= sizeof buf)
392 if (strncasecmp(s, "SIG", 3) == 0)
394 for (i = 0; i <= NSIG; i++)
395 if (strcasecmp(s, signame(i) + 3) == 0) {
396 qualify_one(i, bitflag, not, -1);
403 qual_fault(const char *s, int bitflag, int not)
409 qual_desc(const char *s, int bitflag, int not)
411 if (isdigit((unsigned char)*s)) {
413 if (desc < 0 || desc >= MAX_QUALS)
415 qualify_one(desc, bitflag, not, -1);
422 lookup_class(const char *s)
424 if (strcmp(s, "file") == 0)
426 if (strcmp(s, "ipc") == 0)
428 if (strcmp(s, "network") == 0)
429 return TRACE_NETWORK;
430 if (strcmp(s, "process") == 0)
431 return TRACE_PROCESS;
432 if (strcmp(s, "signal") == 0)
434 if (strcmp(s, "desc") == 0)
440 qualify(const char *s)
442 const struct qual_options *opt;
448 opt = &qual_options[0];
449 for (i = 0; (p = qual_options[i].option_name); i++) {
451 if (strncmp(s, p, n) == 0 && s[n] == '=') {
452 opt = &qual_options[i];
462 if (strcmp(s, "none") == 0) {
466 if (strcmp(s, "all") == 0) {
467 for (i = 0; i < MAX_QUALS; i++) {
468 qualify_one(i, opt->bitflag, not, -1);
472 for (i = 0; i < MAX_QUALS; i++) {
473 qualify_one(i, opt->bitflag, !not, -1);
477 fprintf(stderr, "out of memory\n");
480 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
481 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
482 for (i = 0; i < nsyscalls0; i++)
483 if (sysent0[i].sys_flags & n)
484 qualify_one(i, opt->bitflag, not, 0);
486 #if SUPPORTED_PERSONALITIES >= 2
487 for (i = 0; i < nsyscalls1; i++)
488 if (sysent1[i].sys_flags & n)
489 qualify_one(i, opt->bitflag, not, 1);
490 #endif /* SUPPORTED_PERSONALITIES >= 2 */
492 #if SUPPORTED_PERSONALITIES >= 3
493 for (i = 0; i < nsyscalls2; i++)
494 if (sysent2[i].sys_flags & n)
495 qualify_one(i, opt->bitflag, not, 2);
496 #endif /* SUPPORTED_PERSONALITIES >= 3 */
500 if (opt->qualify(p, opt->bitflag, not)) {
501 fprintf(stderr, "strace: invalid %s `%s'\n",
502 opt->argument_name, p);
511 dumpio(struct tcb *tcp)
515 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
517 if (tcp->scno < 0 || tcp->scno >= nsyscalls)
519 if (sysent[tcp->scno].sys_func == printargs)
521 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
522 if (sysent[tcp->scno].sys_func == sys_read ||
523 sysent[tcp->scno].sys_func == sys_pread ||
524 sysent[tcp->scno].sys_func == sys_pread64 ||
525 sysent[tcp->scno].sys_func == sys_recv ||
526 sysent[tcp->scno].sys_func == sys_recvfrom)
527 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
528 else if (sysent[tcp->scno].sys_func == sys_readv)
529 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
532 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
533 if (sysent[tcp->scno].sys_func == sys_write ||
534 sysent[tcp->scno].sys_func == sys_pwrite ||
535 sysent[tcp->scno].sys_func == sys_pwrite64 ||
536 sysent[tcp->scno].sys_func == sys_send ||
537 sysent[tcp->scno].sys_func == sys_sendto)
538 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
539 else if (sysent[tcp->scno].sys_func == sys_writev)
540 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
546 enum subcall_style { shift_style, deref_style, mask_style, door_style };
548 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
556 static const struct subcall subcalls_table[] = {
557 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
559 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
561 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
563 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
567 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) || defined(__ARM_EABI__) ))
570 decode_subcall(struct tcb *tcp, int subcall, int nsubcalls, enum subcall_style style)
572 unsigned long addr, mask;
574 int size = personality_wordsize[current_personality];
578 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
580 tcp->scno = subcall + tcp->u_arg[0];
581 if (sysent[tcp->scno].nargs != -1)
582 tcp->u_nargs = sysent[tcp->scno].nargs;
585 for (i = 0; i < tcp->u_nargs; i++)
586 tcp->u_arg[i] = tcp->u_arg[i + 1];
589 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
591 tcp->scno = subcall + tcp->u_arg[0];
592 addr = tcp->u_arg[1];
593 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
594 if (size == sizeof(int)) {
596 if (umove(tcp, addr, &arg) < 0)
600 else if (size == sizeof(long)) {
602 if (umove(tcp, addr, &arg) < 0)
610 tcp->u_nargs = sysent[tcp->scno].nargs;
613 mask = (tcp->u_arg[0] >> 8) & 0xff;
614 for (i = 0; mask; i++)
618 tcp->u_arg[0] &= 0xff;
619 tcp->scno = subcall + i;
620 if (sysent[tcp->scno].nargs != -1)
621 tcp->u_nargs = sysent[tcp->scno].nargs;
625 * Oh, yuck. The call code is the *sixth* argument.
626 * (don't you mean the *last* argument? - JH)
628 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
630 tcp->scno = subcall + tcp->u_arg[5];
631 if (sysent[tcp->scno].nargs != -1)
632 tcp->u_nargs = sysent[tcp->scno].nargs;
638 for (i = 0; i < ARRAY_SIZE(subcalls_table); i++)
639 if (subcalls_table[i].call == tcp->scno) break;
640 if (i < ARRAY_SIZE(subcalls_table) &&
641 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
642 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
643 for (i = 0; i < tcp->u_nargs; i++)
644 tcp->u_arg[i] = tcp->u_arg[i + 1];
652 struct tcb *tcp_last = NULL;
655 internal_syscall(struct tcb *tcp)
658 * We must always trace a few critical system calls in order to
659 * correctly support following forks in the presence of tracing
664 if (tcp->scno < 0 || tcp->scno >= nsyscalls)
667 func = sysent[tcp->scno].sys_func;
669 if ( sys_fork == func
670 #if defined(FREEBSD) || defined(LINUX) || defined(SUNOS4)
680 return internal_fork(tcp);
682 #if defined SUNOS4 || (defined LINUX && defined TCB_WAITEXECVE)
683 if ( sys_execve == func
684 # if defined(SPARC) || defined(SPARC64) || defined(SUNOS4)
688 || sys_rexecve == func
691 return internal_exec(tcp);
701 # elif defined (IA64)
702 long r8, r10, psr; /* TODO: make static? */
703 long ia32 = 0; /* not static */
704 # elif defined (POWERPC)
705 static long result, flags;
706 # elif defined (M68K)
711 static struct pt_regs regs;
712 # elif defined (ALPHA)
715 # elif defined(AVR32)
716 static struct pt_regs regs;
717 # elif defined (SPARC) || defined (SPARC64)
718 static struct pt_regs regs;
719 static unsigned long trap;
720 # elif defined(LINUX_MIPSN32)
726 # elif defined(S390) || defined(S390X)
729 static long syscall_mode;
736 # elif defined(X86_64)
738 # elif defined(CRISV10) || defined(CRISV32)
740 # elif defined(MICROBLAZE)
745 struct reg regs; /* TODO: make static? */
749 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
750 * 1: ok, continue in trace_syscall().
751 * other: error, trace_syscall() should print error indicator
752 * ("????" etc) and bail out.
755 get_scno(struct tcb *tcp)
760 # if defined(S390) || defined(S390X)
761 if (tcp->flags & TCB_WAITEXECVE) {
763 * When the execve system call completes successfully, the
764 * new process still has -ENOSYS (old style) or __NR_execve
765 * (new style) in gpr2. We cannot recover the scno again
766 * by disassembly, because the image that executed the
767 * syscall is gone now. Fortunately, we don't want it. We
768 * leave the flag set so that syscall_fixup can fake the
774 * This is the post-execve SIGTRAP. We cannot try to read
775 * the system call here either.
777 tcp->flags &= ~TCB_WAITEXECVE;
781 if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
784 if (syscall_mode != -ENOSYS) {
786 * Since kernel version 2.5.44 the scno gets passed in gpr2.
791 * Old style of "passing" the scno via the SVC instruction.
794 long opcode, offset_reg, tmp;
796 static const int gpr_offset[16] = {
797 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
798 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
799 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
800 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
803 if (upeek(tcp, PT_PSWADDR, &pc) < 0)
806 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-sizeof(long)), 0);
808 perror("peektext(pc-oneword)");
813 * We have to check if the SVC got executed directly or via an
814 * EXECUTE instruction. In case of EXECUTE it is necessary to do
815 * instruction decoding to derive the system call number.
816 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
817 * so that this doesn't work if a SVC opcode is part of an EXECUTE
818 * opcode. Since there is no way to find out the opcode size this
819 * is the best we can do...
822 if ((opcode & 0xff00) == 0x0a00) {
824 scno = opcode & 0xff;
827 /* SVC got executed by EXECUTE instruction */
830 * Do instruction decoding of EXECUTE. If you really want to
831 * understand this, read the Principles of Operations.
833 svc_addr = (void *) (opcode & 0xfff);
836 offset_reg = (opcode & 0x000f0000) >> 16;
837 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
842 offset_reg = (opcode & 0x0000f000) >> 12;
843 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
847 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
856 offset_reg = (opcode & 0x00f00000) >> 20;
857 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
860 scno = (scno | tmp) & 0xff;
863 # elif defined (POWERPC)
864 if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
867 /* Check if this is the post-execve SIGTRAP. */
868 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
869 tcp->flags &= ~TCB_WAITEXECVE;
876 /* TODO: speed up strace by not doing this at every syscall.
877 * We only need to do it after execve.
883 /* Check for 64/32 bit mode. */
884 if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
886 /* SF is bit 0 of MSR */
891 if (currpers != current_personality) {
892 static const char *const names[] = {"64 bit", "32 bit"};
893 set_personality(currpers);
894 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
895 pid, names[current_personality]);
899 # elif defined(AVR32)
901 * Read complete register set in one go.
903 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, ®s) < 0)
907 * We only need to grab the syscall number on syscall entry.
912 /* Check if this is the post-execve SIGTRAP. */
913 if (tcp->flags & TCB_WAITEXECVE) {
914 tcp->flags &= ~TCB_WAITEXECVE;
919 if (upeek(tcp, PT_ORIG_P0, &scno))
921 # elif defined (I386)
922 if (upeek(tcp, 4*ORIG_EAX, &scno) < 0)
924 # elif defined (X86_64)
925 if (upeek(tcp, 8*ORIG_RAX, &scno) < 0)
929 /* TODO: speed up strace by not doing this at every syscall.
930 * We only need to do it after execve.
936 /* Check CS register value. On x86-64 linux it is:
937 * 0x33 for long mode (64 bit)
938 * 0x23 for compatibility mode (32 bit)
939 * It takes only one ptrace and thus doesn't need
942 if (upeek(tcp, 8*CS, &val) < 0)
945 case 0x23: currpers = 1; break;
946 case 0x33: currpers = 0; break;
948 fprintf(stderr, "Unknown value CS=0x%02X while "
949 "detecting personality of process "
950 "PID=%d\n", (int)val, pid);
951 currpers = current_personality;
955 /* This version analyzes the opcode of a syscall instruction.
956 * (int 0x80 on i386 vs. syscall on x86-64)
957 * It works, but is too complicated.
959 unsigned long val, rip, i;
961 if (upeek(tcp, 8*RIP, &rip) < 0)
962 perror("upeek(RIP)");
964 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
968 call = ptrace(PTRACE_PEEKTEXT, pid, (char *)rip, (char *)0);
970 fprintf(stderr, "ptrace_peektext failed: %s\n",
972 switch (call & 0xffff) {
973 /* x86-64: syscall = 0x0f 0x05 */
974 case 0x050f: currpers = 0; break;
975 /* i386: int 0x80 = 0xcd 0x80 */
976 case 0x80cd: currpers = 1; break;
978 currpers = current_personality;
980 "Unknown syscall opcode (0x%04X) while "
981 "detecting personality of process "
982 "PID=%d\n", (int)call, pid);
986 if (currpers != current_personality) {
987 static const char *const names[] = {"64 bit", "32 bit"};
988 set_personality(currpers);
989 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
990 pid, names[current_personality]);
994 # define IA64_PSR_IS ((long)1 << 34)
995 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
996 ia32 = (psr & IA64_PSR_IS) != 0;
999 if (upeek(tcp, PT_R1, &scno) < 0) /* orig eax */
1002 if (upeek(tcp, PT_R15, &scno) < 0)
1005 /* Check if this is the post-execve SIGTRAP. */
1006 if (tcp->flags & TCB_WAITEXECVE) {
1007 tcp->flags &= ~TCB_WAITEXECVE;
1012 if (upeek(tcp, PT_R8, &r8) < 0)
1014 if (upeek(tcp, PT_R10, &r10) < 0)
1017 # elif defined (ARM)
1019 * Read complete register set in one go.
1021 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (void *)®s) == -1)
1025 * We only need to grab the syscall number on syscall entry.
1027 if (regs.ARM_ip == 0) {
1028 if (entering(tcp)) {
1029 /* Check if this is the post-execve SIGTRAP. */
1030 if (tcp->flags & TCB_WAITEXECVE) {
1031 tcp->flags &= ~TCB_WAITEXECVE;
1037 * Note: we only deal with only 32-bit CPUs here.
1039 if (regs.ARM_cpsr & 0x20) {
1041 * Get the Thumb-mode system call number
1046 * Get the ARM-mode system call number
1049 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(regs.ARM_pc - 4), NULL);
1053 /* FIXME: bogus check? it is already done on entering before,
1054 * so we never can see it here?
1056 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
1057 tcp->flags &= ~TCB_WAITEXECVE;
1061 /* Handle the EABI syscall convention. We do not
1062 bother converting structures between the two
1063 ABIs, but basic functionality should work even
1064 if strace and the traced program have different
1066 if (scno == 0xef000000) {
1069 if ((scno & 0x0ff00000) != 0x0f900000) {
1070 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
1076 * Fixup the syscall number
1081 if (scno & 0x0f0000) {
1083 * Handle ARM specific syscall
1091 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1092 tcp->flags &= ~TCB_INSYSCALL;
1095 if (entering(tcp)) {
1096 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
1097 tcp->flags |= TCB_INSYSCALL;
1100 # elif defined (M68K)
1101 if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
1103 # elif defined (LINUX_MIPSN32)
1104 unsigned long long regs[38];
1106 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1111 if (entering(tcp)) {
1114 /* Check if this is the post-execve SIGTRAP. */
1115 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1116 tcp->flags &= ~TCB_WAITEXECVE;
1120 if (scno < 0 || scno > nsyscalls) {
1121 if (a3 == 0 || a3 == -1) {
1123 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1128 # elif defined (MIPS)
1129 if (upeek(tcp, REG_A3, &a3) < 0)
1131 if (entering(tcp)) {
1132 if (upeek(tcp, REG_V0, &scno) < 0)
1135 /* Check if this is the post-execve SIGTRAP. */
1136 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1137 tcp->flags &= ~TCB_WAITEXECVE;
1141 if (scno < 0 || scno > nsyscalls) {
1142 if (a3 == 0 || a3 == -1) {
1144 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1149 if (upeek(tcp, REG_V0, &r2) < 0)
1152 # elif defined (ALPHA)
1153 if (upeek(tcp, REG_A3, &a3) < 0)
1156 if (entering(tcp)) {
1157 if (upeek(tcp, REG_R0, &scno) < 0)
1160 /* Check if this is the post-execve SIGTRAP. */
1161 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1162 tcp->flags &= ~TCB_WAITEXECVE;
1167 * Do some sanity checks to figure out if it's
1168 * really a syscall entry
1170 if (scno < 0 || scno > nsyscalls) {
1171 if (a3 == 0 || a3 == -1) {
1173 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
1179 if (upeek(tcp, REG_R0, &r0) < 0)
1182 # elif defined (SPARC) || defined (SPARC64)
1183 /* Everything we need is in the current register set. */
1184 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1187 /* If we are entering, then disassemble the syscall trap. */
1188 if (entering(tcp)) {
1189 /* Retrieve the syscall trap instruction. */
1191 # if defined(SPARC64)
1192 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.tpc, 0);
1195 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.pc, 0);
1200 /* Disassemble the trap to see what personality to use. */
1203 /* Linux/SPARC syscall trap. */
1207 /* Linux/SPARC64 syscall trap. */
1211 /* SunOS syscall trap. (pers 1) */
1212 fprintf(stderr, "syscall: SunOS no support\n");
1215 /* Solaris 2.x syscall trap. (per 2) */
1219 /* NetBSD/FreeBSD syscall trap. */
1220 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1223 /* Solaris 2.x gettimeofday */
1227 /* Check if this is the post-execve SIGTRAP. */
1228 if (tcp->flags & TCB_WAITEXECVE) {
1229 tcp->flags &= ~TCB_WAITEXECVE;
1232 # if defined (SPARC64)
1233 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, regs.tpc);
1235 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, regs.pc);
1240 /* Extract the system call number from the registers. */
1241 if (trap == 0x91d02027)
1244 scno = regs.u_regs[U_REG_G1];
1246 scno = regs.u_regs[U_REG_O0];
1247 memmove(®s.u_regs[U_REG_O0], ®s.u_regs[U_REG_O1], 7*sizeof(regs.u_regs[0]));
1250 # elif defined(HPPA)
1251 if (upeek(tcp, PT_GR20, &scno) < 0)
1253 if (entering(tcp)) {
1254 /* Check if this is the post-execve SIGTRAP. */
1255 if (tcp->flags & TCB_WAITEXECVE) {
1256 tcp->flags &= ~TCB_WAITEXECVE;
1262 * In the new syscall ABI, the system call number is in R3.
1264 if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
1268 /* Odd as it may seem, a glibc bug has been known to cause
1269 glibc to issue bogus negative syscall numbers. So for
1270 our purposes, make strace print what it *should* have been */
1271 long correct_scno = (scno & 0xff);
1274 "Detected glibc bug: bogus system call"
1275 " number = %ld, correcting to %ld\n",
1278 scno = correct_scno;
1281 if (entering(tcp)) {
1282 /* Check if this is the post-execve SIGTRAP. */
1283 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1284 tcp->flags &= ~TCB_WAITEXECVE;
1288 # elif defined(SH64)
1289 if (upeek(tcp, REG_SYSCALL, &scno) < 0)
1293 if (entering(tcp)) {
1294 /* Check if this is the post-execve SIGTRAP. */
1295 if (tcp->flags & TCB_WAITEXECVE) {
1296 tcp->flags &= ~TCB_WAITEXECVE;
1300 # elif defined(CRISV10) || defined(CRISV32)
1301 if (upeek(tcp, 4*PT_R9, &scno) < 0)
1303 # elif defined(TILE)
1304 if (upeek(tcp, PTREGS_OFFSET_REG(10), &scno) < 0)
1307 if (entering(tcp)) {
1308 /* Check if this is the post-execve SIGTRAP. */
1309 if (tcp->flags & TCB_WAITEXECVE) {
1310 tcp->flags &= ~TCB_WAITEXECVE;
1314 # elif defined(MICROBLAZE)
1315 if (upeek(tcp, 0, &scno) < 0)
1321 if (upeek(tcp, uoff(u_arg[7]), &scno) < 0)
1324 /* new syscall ABI returns result in R0 */
1325 if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0)
1328 /* ABI defines result returned in r9 */
1329 if (upeek(tcp, REG_GENERAL(9), (long *)&r9) < 0)
1334 # ifdef HAVE_PR_SYSCALL
1335 scno = tcp->status.PR_SYSCALL;
1338 scno = tcp->status.PR_WHAT;
1340 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1344 switch (regs.r_eax) {
1347 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1353 # endif /* FREEBSD */
1354 # endif /* !HAVE_PR_SYSCALL */
1355 #endif /* USE_PROCFS */
1364 known_scno(struct tcb *tcp)
1366 long scno = tcp->scno;
1367 #if SUPPORTED_PERSONALITIES > 1
1368 if (scno >= 0 && scno < nsyscalls && sysent[scno].native_scno != 0)
1369 scno = sysent[scno].native_scno;
1372 scno += NR_SYSCALL_BASE;
1376 /* Called in trace_syscall() at each syscall entry and exit.
1378 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1379 * 1: ok, continue in trace_syscall().
1380 * other: error, trace_syscall() should print error indicator
1381 * ("????" etc) and bail out.
1384 syscall_fixup(struct tcb *tcp)
1387 int scno = known_scno(tcp);
1389 if (entering(tcp)) {
1390 if (tcp->status.PR_WHY != PR_SYSENTRY) {
1394 || scno == SYS_vfork
1395 #endif /* SYS_vfork */
1397 || scno == SYS_fork1
1398 #endif /* SYS_fork1 */
1400 || scno == SYS_forkall
1401 #endif /* SYS_forkall */
1403 || scno == SYS_rfork1
1404 #endif /* SYS_fork1 */
1406 || scno == SYS_rforkall
1407 #endif /* SYS_rforkall */
1409 /* We are returning in the child, fake it. */
1410 tcp->status.PR_WHY = PR_SYSENTRY;
1412 tcp->status.PR_WHY = PR_SYSEXIT;
1415 fprintf(stderr, "syscall: missing entry\n");
1416 tcp->flags |= TCB_INSYSCALL;
1421 if (tcp->status.PR_WHY != PR_SYSEXIT) {
1422 fprintf(stderr, "syscall: missing exit\n");
1423 tcp->flags &= ~TCB_INSYSCALL;
1426 #endif /* USE_PROCFS */
1429 if (entering(tcp)) {
1431 fprintf(stderr, "syscall: missing entry\n");
1432 tcp->flags |= TCB_INSYSCALL;
1439 * This happens when a signal handler
1440 * for a signal which interrupted a
1441 * a system call makes another system call.
1443 fprintf(stderr, "syscall: missing exit\n");
1445 tcp->flags &= ~TCB_INSYSCALL;
1451 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
1453 if (upeek(tcp, 4*EAX, &eax) < 0)
1455 if (eax != -ENOSYS && entering(tcp)) {
1457 fprintf(stderr, "not a syscall entry (eax = %ld)\n", eax);
1460 #elif defined (X86_64)
1461 if (upeek(tcp, 8*RAX, &rax) < 0)
1463 if (current_personality == 1)
1464 rax = (long int)(int)rax; /* sign extend from 32 bits */
1465 if (rax != -ENOSYS && entering(tcp)) {
1467 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1470 #elif defined (S390) || defined (S390X)
1471 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1473 if (syscall_mode != -ENOSYS)
1474 syscall_mode = tcp->scno;
1475 if (gpr2 != syscall_mode && entering(tcp)) {
1477 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
1480 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1481 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1482 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1484 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1485 * flag set for the post-execve SIGTRAP to see and reset.
1489 #elif defined (POWERPC)
1490 # define SO_MASK 0x10000000
1491 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1493 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &result) < 0)
1495 if (flags & SO_MASK)
1497 #elif defined (M68K)
1498 if (upeek(tcp, 4*PT_D0, &d0) < 0)
1500 if (d0 != -ENOSYS && entering(tcp)) {
1502 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", d0);
1510 if (upeek(tcp, PT_R0, &r0) < 0)
1512 #elif defined (HPPA)
1513 if (upeek(tcp, PT_GR28, &r28) < 0)
1516 if (upeek(tcp, PT_R10, &r10) < 0)
1518 if (upeek(tcp, PT_R8, &r8) < 0)
1520 if (ia32 && r8 != -ENOSYS && entering(tcp)) {
1522 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", r8);
1525 #elif defined(CRISV10) || defined(CRISV32)
1526 if (upeek(tcp, 4*PT_R10, &r10) < 0)
1528 if (r10 != -ENOSYS && entering(tcp)) {
1530 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", r10);
1533 #elif defined(MICROBLAZE)
1534 if (upeek(tcp, 3 * 4, &r3) < 0)
1536 if (r3 != -ENOSYS && entering(tcp)) {
1538 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", r3);
1548 * Check the syscall return value register value for whether it is
1549 * a negated errno code indicating an error, or a success return value.
1552 is_negated_errno(unsigned long int val)
1554 unsigned long int max = -(long int) nerrnos;
1555 # if SUPPORTED_PERSONALITIES > 1
1556 if (personality_wordsize[current_personality] < sizeof(val)) {
1557 val = (unsigned int) val;
1558 max = (unsigned int) max;
1566 get_error(struct tcb *tcp)
1570 int check_errno = 1;
1571 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1572 sysent[tcp->scno].sys_flags & SYSCALL_NEVER_FAILS) {
1575 # if defined(S390) || defined(S390X)
1576 if (check_errno && is_negated_errno(gpr2)) {
1584 # elif defined(I386)
1585 if (check_errno && is_negated_errno(eax)) {
1593 # elif defined(X86_64)
1594 if (check_errno && is_negated_errno(rax)) {
1602 # elif defined(IA64)
1607 if (check_errno && is_negated_errno(err)) {
1616 if (check_errno && r10) {
1624 # elif defined(MIPS)
1625 if (check_errno && a3) {
1632 # elif defined(POWERPC)
1633 if (check_errno && is_negated_errno(result)) {
1638 tcp->u_rval = result;
1641 # elif defined(M68K)
1642 if (check_errno && is_negated_errno(d0)) {
1651 if (check_errno && is_negated_errno(regs.ARM_r0)) {
1653 u_error = -regs.ARM_r0;
1656 tcp->u_rval = regs.ARM_r0;
1659 # elif defined(AVR32)
1660 if (check_errno && regs.r12 && (unsigned) -regs.r12 < nerrnos) {
1662 u_error = -regs.r12;
1665 tcp->u_rval = regs.r12;
1668 # elif defined(BFIN)
1669 if (check_errno && is_negated_errno(r0)) {
1676 # elif defined(ALPHA)
1677 if (check_errno && a3) {
1685 # elif defined(SPARC)
1686 if (check_errno && regs.psr & PSR_C) {
1688 u_error = regs.u_regs[U_REG_O0];
1691 tcp->u_rval = regs.u_regs[U_REG_O0];
1694 # elif defined(SPARC64)
1695 if (check_errno && regs.tstate & 0x1100000000UL) {
1697 u_error = regs.u_regs[U_REG_O0];
1700 tcp->u_rval = regs.u_regs[U_REG_O0];
1703 # elif defined(HPPA)
1704 if (check_errno && is_negated_errno(r28)) {
1713 /* interpret R0 as return value or error number */
1714 if (check_errno && is_negated_errno(r0)) {
1722 # elif defined(SH64)
1723 /* interpret result as return value or error number */
1724 if (check_errno && is_negated_errno(r9)) {
1732 # elif defined(CRISV10) || defined(CRISV32)
1733 if (check_errno && r10 && (unsigned) -r10 < nerrnos) {
1741 # elif defined(TILE)
1743 /* interpret result as return value or error number */
1744 if (upeek(tcp, PTREGS_OFFSET_REG(0), &rval) < 0)
1746 if (check_errno && rval < 0 && rval > -nerrnos) {
1754 # elif defined(MICROBLAZE)
1755 /* interpret result as return value or error number */
1756 if (check_errno && is_negated_errno(r3)) {
1767 /* get error code from user struct */
1768 if (upeek(tcp, uoff(u_error), &u_error) < 0)
1770 u_error >>= 24; /* u_error is a char */
1772 /* get system call return value */
1773 if (upeek(tcp, uoff(u_rval1), &tcp->u_rval) < 0)
1778 /* Judicious guessing goes a long way. */
1779 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1781 u_error = tcp->status.pr_reg[R_O0];
1784 tcp->u_rval = tcp->status.pr_reg[R_O0];
1789 /* Wanna know how to kill an hour single-stepping? */
1790 if (tcp->status.PR_REG[EFL] & 0x1) {
1792 u_error = tcp->status.PR_REG[EAX];
1795 tcp->u_rval = tcp->status.PR_REG[EAX];
1796 # ifdef HAVE_LONG_LONG
1798 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1799 tcp->status.PR_REG[EAX];
1805 /* Wanna know how to kill an hour single-stepping? */
1806 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1808 u_error = tcp->status.PR_REG[RAX];
1811 tcp->u_rval = tcp->status.PR_REG[RAX];
1814 # endif /* X86_64 */
1816 if (tcp->status.pr_reg[CTX_A3]) {
1818 u_error = tcp->status.pr_reg[CTX_V0];
1821 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1827 if (regs.r_eflags & PSL_C) {
1829 u_error = regs.r_eax;
1831 tcp->u_rval = regs.r_eax;
1833 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1836 #endif /* FREEBSD */
1837 tcp->u_error = u_error;
1842 force_result(struct tcb *tcp, int error, long rval)
1845 # if defined(S390) || defined(S390X)
1846 gpr2 = error ? -error : rval;
1847 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1849 # elif defined(I386)
1850 eax = error ? -error : rval;
1851 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1853 # elif defined(X86_64)
1854 rax = error ? -error : rval;
1855 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 8), rax) < 0)
1857 # elif defined(IA64)
1859 r8 = error ? -error : rval;
1860 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1872 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1873 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1876 # elif defined(BFIN)
1877 r0 = error ? -error : rval;
1878 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_R0, r0) < 0)
1880 # elif defined(MIPS)
1889 /* PTRACE_POKEUSER is OK even for n32 since rval is only a long. */
1890 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1891 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1893 # elif defined(POWERPC)
1894 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1904 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1905 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
1907 # elif defined(M68K)
1908 d0 = error ? -error : rval;
1909 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1912 regs.ARM_r0 = error ? -error : rval;
1913 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), regs.ARM_r0) < 0)
1915 # elif defined(AVR32)
1916 regs.r12 = error ? -error : rval;
1917 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_R12, regs.r12) < 0)
1919 # elif defined(ALPHA)
1928 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1929 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1931 # elif defined(SPARC)
1932 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1936 regs.u_regs[U_REG_O0] = error;
1940 regs.u_regs[U_REG_O0] = rval;
1942 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1944 # elif defined(SPARC64)
1945 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1948 regs.tstate |= 0x1100000000UL;
1949 regs.u_regs[U_REG_O0] = error;
1952 regs.tstate &= ~0x1100000000UL;
1953 regs.u_regs[U_REG_O0] = rval;
1955 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0)
1957 # elif defined(HPPA)
1958 r28 = error ? -error : rval;
1959 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1962 r0 = error ? -error : rval;
1963 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1965 # elif defined(SH64)
1966 r9 = error ? -error : rval;
1967 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1973 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1975 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1985 if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
1990 regs.r_eflags |= PSL_C;
1994 regs.r_eflags &= ~PSL_C;
1997 if (pwrite(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) {
2001 #endif /* FREEBSD */
2003 /* All branches reach here on success (only). */
2004 tcp->u_error = error;
2010 syscall_enter(struct tcb *tcp)
2013 # if defined(S390) || defined(S390X)
2015 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2016 tcp->u_nargs = sysent[tcp->scno].nargs;
2018 tcp->u_nargs = MAX_ARGS;
2019 for (i = 0; i < tcp->u_nargs; i++) {
2020 if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
2023 # elif defined(ALPHA)
2025 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2026 tcp->u_nargs = sysent[tcp->scno].nargs;
2028 tcp->u_nargs = MAX_ARGS;
2029 for (i = 0; i < tcp->u_nargs; i++) {
2030 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
2031 * for scno somewhere above here!
2033 if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
2036 # elif defined(IA64)
2038 unsigned long *out0, cfm, sof, sol, i;
2040 /* be backwards compatible with kernel < 2.4.4... */
2042 # define PT_RBS_END PT_AR_BSP
2045 if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
2047 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
2050 sof = (cfm >> 0) & 0x7f;
2051 sol = (cfm >> 7) & 0x7f;
2052 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
2054 if (tcp->scno >= 0 && tcp->scno < nsyscalls
2055 && sysent[tcp->scno].nargs != -1)
2056 tcp->u_nargs = sysent[tcp->scno].nargs;
2058 tcp->u_nargs = MAX_ARGS;
2059 for (i = 0; i < tcp->u_nargs; ++i) {
2060 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
2061 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
2067 if (/* EBX = out0 */
2068 upeek(tcp, PT_R11, (long *) &tcp->u_arg[0]) < 0
2070 || upeek(tcp, PT_R9, (long *) &tcp->u_arg[1]) < 0
2072 || upeek(tcp, PT_R10, (long *) &tcp->u_arg[2]) < 0
2074 || upeek(tcp, PT_R14, (long *) &tcp->u_arg[3]) < 0
2076 || upeek(tcp, PT_R15, (long *) &tcp->u_arg[4]) < 0
2078 || upeek(tcp, PT_R13, (long *) &tcp->u_arg[5]) < 0)
2081 for (i = 0; i < 6; ++i)
2082 /* truncate away IVE sign-extension */
2083 tcp->u_arg[i] &= 0xffffffff;
2085 if (tcp->scno >= 0 && tcp->scno < nsyscalls
2086 && sysent[tcp->scno].nargs != -1)
2087 tcp->u_nargs = sysent[tcp->scno].nargs;
2091 # elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
2092 /* N32 and N64 both use up to six registers. */
2093 unsigned long long regs[38];
2095 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2096 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
2098 nargs = tcp->u_nargs = MAX_ARGS;
2100 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
2103 for (i = 0; i < nargs; i++) {
2104 tcp->u_arg[i] = regs[REG_A0 + i];
2105 # if defined(LINUX_MIPSN32)
2106 tcp->ext_arg[i] = regs[REG_A0 + i];
2109 # elif defined(MIPS)
2113 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2114 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
2116 nargs = tcp->u_nargs = MAX_ARGS;
2118 if (upeek(tcp, REG_SP, &sp) < 0)
2120 for (i = 0; i < 4; i++) {
2121 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
2124 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
2125 (char *)(tcp->u_arg + 4));
2127 for (i = 0; i < nargs; i++) {
2128 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
2132 # elif defined(POWERPC)
2134 # define PT_ORIG_R3 34
2137 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2138 tcp->u_nargs = sysent[tcp->scno].nargs;
2140 tcp->u_nargs = MAX_ARGS;
2141 for (i = 0; i < tcp->u_nargs; i++) {
2142 if (upeek(tcp, (i==0) ?
2143 (sizeof(unsigned long) * PT_ORIG_R3) :
2144 ((i+PT_R3) * sizeof(unsigned long)),
2145 &tcp->u_arg[i]) < 0)
2148 # elif defined(SPARC) || defined(SPARC64)
2150 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2151 tcp->u_nargs = sysent[tcp->scno].nargs;
2153 tcp->u_nargs = MAX_ARGS;
2154 for (i = 0; i < tcp->u_nargs; i++)
2155 tcp->u_arg[i] = regs.u_regs[U_REG_O0 + i];
2156 # elif defined(HPPA)
2158 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2159 tcp->u_nargs = sysent[tcp->scno].nargs;
2161 tcp->u_nargs = MAX_ARGS;
2162 for (i = 0; i < tcp->u_nargs; i++) {
2163 if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
2168 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2169 tcp->u_nargs = sysent[tcp->scno].nargs;
2171 tcp->u_nargs = MAX_ARGS;
2172 for (i = 0; i < tcp->u_nargs; i++)
2173 tcp->u_arg[i] = regs.uregs[i];
2174 # elif defined(AVR32)
2175 tcp->u_nargs = sysent[tcp->scno].nargs;
2176 tcp->u_arg[0] = regs.r12;
2177 tcp->u_arg[1] = regs.r11;
2178 tcp->u_arg[2] = regs.r10;
2179 tcp->u_arg[3] = regs.r9;
2180 tcp->u_arg[4] = regs.r5;
2181 tcp->u_arg[5] = regs.r3;
2182 # elif defined(BFIN)
2184 static const int argreg[] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
2186 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2187 tcp->u_nargs = sysent[tcp->scno].nargs;
2189 tcp->u_nargs = ARRAY_SIZE(argreg);
2191 for (i = 0; i < tcp->u_nargs; ++i)
2192 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
2196 static const int syscall_regs[] = {
2197 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6), 4 * (REG_REG0+7),
2198 4 * (REG_REG0 ), 4 * (REG_REG0+1), 4 * (REG_REG0+2)
2201 tcp->u_nargs = sysent[tcp->scno].nargs;
2202 for (i = 0; i < tcp->u_nargs; i++) {
2203 if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
2206 # elif defined(SH64)
2208 /* Registers used by SH5 Linux system calls for parameters */
2209 static const int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
2212 * TODO: should also check that the number of arguments encoded
2213 * in the trap number matches the number strace expects.
2216 assert(sysent[tcp->scno].nargs < ARRAY_SIZE(syscall_regs));
2219 tcp->u_nargs = sysent[tcp->scno].nargs;
2220 for (i = 0; i < tcp->u_nargs; i++) {
2221 if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
2224 # elif defined(X86_64)
2226 static const int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
2227 { 8 * RDI, 8 * RSI, 8 * RDX, 8 * R10, 8 * R8 , 8 * R9 }, /* x86-64 ABI */
2228 { 8 * RBX, 8 * RCX, 8 * RDX, 8 * RSI, 8 * RDI, 8 * RBP } /* i386 ABI */
2231 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2232 tcp->u_nargs = sysent[tcp->scno].nargs;
2234 tcp->u_nargs = MAX_ARGS;
2235 for (i = 0; i < tcp->u_nargs; i++) {
2236 if (upeek(tcp, argreg[current_personality][i], &tcp->u_arg[i]) < 0)
2239 # elif defined(MICROBLAZE)
2241 if (tcp->scno >= 0 && tcp->scno < nsyscalls)
2242 tcp->u_nargs = sysent[tcp->scno].nargs;
2245 for (i = 0; i < tcp->u_nargs; i++) {
2246 if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
2249 # elif defined(CRISV10) || defined(CRISV32)
2251 static const int crisregs[] = {
2252 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
2253 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
2256 if (tcp->scno >= 0 && tcp->scno < nsyscalls)
2257 tcp->u_nargs = sysent[tcp->scno].nargs;
2260 for (i = 0; i < tcp->u_nargs; i++) {
2261 if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0)
2264 # elif defined(TILE)
2266 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2267 tcp->u_nargs = sysent[tcp->scno].nargs;
2269 tcp->u_nargs = MAX_ARGS;
2270 for (i = 0; i < tcp->u_nargs; ++i) {
2271 if (upeek(tcp, PTREGS_OFFSET_REG(i), &tcp->u_arg[i]) < 0)
2274 # elif defined(M68K)
2276 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2277 tcp->u_nargs = sysent[tcp->scno].nargs;
2279 tcp->u_nargs = MAX_ARGS;
2280 for (i = 0; i < tcp->u_nargs; i++) {
2281 if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
2284 # else /* Other architecture (like i386) (32bits specific) */
2286 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2287 tcp->u_nargs = sysent[tcp->scno].nargs;
2289 tcp->u_nargs = MAX_ARGS;
2290 for (i = 0; i < tcp->u_nargs; i++) {
2291 if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
2298 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2299 tcp->u_nargs = sysent[tcp->scno].nargs;
2301 tcp->u_nargs = MAX_ARGS;
2302 for (i = 0; i < tcp->u_nargs; i++) {
2305 if (upeek(tcp, uoff(u_arg[0]) +
2306 (i * sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
2313 * SGI is broken: even though it has pr_sysarg, it doesn't
2314 * set them on system call entry. Get a clue.
2316 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2317 tcp->u_nargs = sysent[tcp->scno].nargs;
2319 tcp->u_nargs = tcp->status.pr_nsysarg;
2320 if (tcp->u_nargs > 4) {
2321 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2322 4 * sizeof(tcp->u_arg[0]));
2323 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
2324 (tcp->u_nargs - 4) * sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
2327 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2328 tcp->u_nargs * sizeof(tcp->u_arg[0]));
2330 # elif UNIXWARE >= 2
2332 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
2334 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2335 tcp->u_nargs = sysent[tcp->scno].nargs;
2337 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
2338 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2339 tcp->u_nargs * sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2340 # elif defined(HAVE_PR_SYSCALL)
2342 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2343 tcp->u_nargs = sysent[tcp->scno].nargs;
2345 tcp->u_nargs = tcp->status.pr_nsysarg;
2346 for (i = 0; i < tcp->u_nargs; i++)
2347 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
2348 # elif defined(I386)
2349 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2350 tcp->u_nargs = sysent[tcp->scno].nargs;
2353 if (tcp->u_nargs > 0)
2354 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2355 tcp->u_nargs * sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2357 I DONT KNOW WHAT TO DO
2361 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2362 sysent[tcp->scno].nargs > tcp->status.val)
2363 tcp->u_nargs = sysent[tcp->scno].nargs;
2365 tcp->u_nargs = tcp->status.val;
2366 if (tcp->u_nargs < 0)
2368 if (tcp->u_nargs > MAX_ARGS)
2369 tcp->u_nargs = MAX_ARGS;
2370 switch (regs.r_eax) {
2372 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2373 regs.r_esp + sizeof(int) + sizeof(quad_t));
2376 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2377 regs.r_esp + 2 * sizeof(int));
2380 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2381 regs.r_esp + sizeof(int));
2384 #endif /* FREEBSD */
2389 trace_syscall_entering(struct tcb *tcp)
2393 scno_good = res = get_scno(tcp);
2397 res = syscall_fixup(tcp);
2401 res = syscall_enter(tcp);
2407 tcp->flags &= ~TCB_REPRINT;
2410 tprintf("????" /* anti-trigraph gap */ "(");
2411 else if (tcp->scno >= nsyscalls || tcp->scno < 0)
2412 tprintf("syscall_%lu(", tcp->scno);
2414 tprintf("%s(", sysent[tcp->scno].sys_name);
2416 * " <unavailable>" will be added later by the code which
2417 * detects ptrace errors.
2422 switch (known_scno(tcp)) {
2423 #ifdef SYS_socket_subcall
2424 case SYS_socketcall:
2425 decode_subcall(tcp, SYS_socket_subcall,
2426 SYS_socket_nsubcalls, deref_style);
2429 #ifdef SYS_ipc_subcall
2431 decode_subcall(tcp, SYS_ipc_subcall,
2432 SYS_ipc_nsubcalls, shift_style);
2436 #ifdef SYS_pgrpsys_subcall
2438 decode_subcall(tcp, SYS_pgrpsys_subcall,
2439 SYS_pgrpsys_nsubcalls, shift_style);
2441 #endif /* SYS_pgrpsys_subcall */
2442 #ifdef SYS_sigcall_subcall
2444 decode_subcall(tcp, SYS_sigcall_subcall,
2445 SYS_sigcall_nsubcalls, mask_style);
2447 #endif /* SYS_sigcall_subcall */
2449 decode_subcall(tcp, SYS_msgsys_subcall,
2450 SYS_msgsys_nsubcalls, shift_style);
2453 decode_subcall(tcp, SYS_shmsys_subcall,
2454 SYS_shmsys_nsubcalls, shift_style);
2457 decode_subcall(tcp, SYS_semsys_subcall,
2458 SYS_semsys_nsubcalls, shift_style);
2461 decode_subcall(tcp, SYS_sysfs_subcall,
2462 SYS_sysfs_nsubcalls, shift_style);
2465 decode_subcall(tcp, SYS_spcall_subcall,
2466 SYS_spcall_nsubcalls, shift_style);
2468 #ifdef SYS_context_subcall
2470 decode_subcall(tcp, SYS_context_subcall,
2471 SYS_context_nsubcalls, shift_style);
2473 #endif /* SYS_context_subcall */
2474 #ifdef SYS_door_subcall
2476 decode_subcall(tcp, SYS_door_subcall,
2477 SYS_door_nsubcalls, door_style);
2479 #endif /* SYS_door_subcall */
2480 #ifdef SYS_kaio_subcall
2482 decode_subcall(tcp, SYS_kaio_subcall,
2483 SYS_kaio_nsubcalls, shift_style);
2491 decode_subcall(tcp, 0, 0, table_style);
2496 decode_subcall(tcp, SYS_semsys_subcall,
2497 SYS_semsys_nsubcalls, shift_style);
2500 decode_subcall(tcp, SYS_msgsys_subcall,
2501 SYS_msgsys_nsubcalls, shift_style);
2504 decode_subcall(tcp, SYS_shmsys_subcall,
2505 SYS_shmsys_nsubcalls, shift_style);
2510 internal_syscall(tcp);
2512 if ((tcp->scno >= 0 && tcp->scno < nsyscalls &&
2513 !(qual_flags[tcp->scno] & QUAL_TRACE)) ||
2514 (tracing_paths && !pathtrace_match(tcp))) {
2515 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
2519 tcp->flags &= ~TCB_FILTERED;
2521 if (cflag == CFLAG_ONLY_STATS) {
2527 tcp->flags &= ~TCB_REPRINT;
2529 if (tcp->scno >= nsyscalls || tcp->scno < 0)
2530 tprintf("syscall_%lu(", tcp->scno);
2532 tprintf("%s(", sysent[tcp->scno].sys_name);
2533 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2534 ((qual_flags[tcp->scno] & QUAL_RAW) &&
2535 sysent[tcp->scno].sys_func != sys_exit))
2536 res = printargs(tcp);
2538 res = (*sysent[tcp->scno].sys_func)(tcp);
2540 if (fflush(tcp->outf) == EOF)
2543 tcp->flags |= TCB_INSYSCALL;
2544 /* Measure the entrance time as late as possible to avoid errors. */
2546 gettimeofday(&tcp->etime, NULL);
2551 trace_syscall_exiting(struct tcb *tcp)
2558 /* Measure the exit time as early as possible to avoid errors. */
2560 gettimeofday(&tv, NULL);
2562 /* BTW, why we don't just memorize syscall no. on entry
2563 * in tcp->something?
2565 scno_good = res = get_scno(tcp);
2569 res = syscall_fixup(tcp);
2573 res = get_error(tcp);
2577 internal_syscall(tcp);
2579 if (res == 1 && filtered(tcp)) {
2583 if (tcp->flags & TCB_REPRINT) {
2588 else if (tcp->scno >= nsyscalls || tcp->scno < 0)
2589 tprintf("syscall_%lu", tcp->scno);
2591 tprintf("%s", sysent[tcp->scno].sys_name);
2592 tprintf(" resumed> ");
2596 struct timeval t = tv;
2597 count_syscall(tcp, &t);
2598 if (cflag == CFLAG_ONLY_STATS) {
2606 tprintf("= ? <unavailable>");
2608 tcp->flags &= ~TCB_INSYSCALL;
2612 if (tcp->scno >= nsyscalls || tcp->scno < 0
2613 || (qual_flags[tcp->scno] & QUAL_RAW))
2614 sys_res = printargs(tcp);
2616 /* FIXME: not_failing_only (IOW, option -z) is broken:
2617 * failure of syscall is known only after syscall return.
2618 * Thus we end up with something like this on, say, ENOENT:
2619 * open("doesnt_exist", O_RDONLY <unfinished ...>
2620 * {next syscall decode}
2621 * whereas the intended result is that open(...) line
2622 * is not shown at all.
2624 if (not_failing_only && tcp->u_error)
2625 goto ret; /* ignore failed syscalls */
2626 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2631 u_error = tcp->u_error;
2632 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2633 qual_flags[tcp->scno] & QUAL_RAW) {
2635 tprintf("= -1 (errno %ld)", u_error);
2637 tprintf("= %#lx", tcp->u_rval);
2639 else if (!(sys_res & RVAL_NONE) && u_error) {
2643 tprintf("= ? ERESTARTSYS (To be restarted)");
2645 case ERESTARTNOINTR:
2646 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2648 case ERESTARTNOHAND:
2649 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2651 case ERESTART_RESTARTBLOCK:
2652 tprintf("= ? ERESTART_RESTARTBLOCK (To be restarted)");
2657 tprintf("= -1 E??? (errno %ld)", u_error);
2658 else if (u_error < nerrnos)
2659 tprintf("= -1 %s (%s)", errnoent[u_error],
2662 tprintf("= -1 ERRNO_%ld (%s)", u_error,
2666 if ((sys_res & RVAL_STR) && tcp->auxstr)
2667 tprintf(" (%s)", tcp->auxstr);
2670 if (sys_res & RVAL_NONE)
2673 switch (sys_res & RVAL_MASK) {
2675 tprintf("= %#lx", tcp->u_rval);
2678 tprintf("= %#lo", tcp->u_rval);
2681 tprintf("= %lu", tcp->u_rval);
2684 tprintf("= %ld", tcp->u_rval);
2686 #ifdef HAVE_LONG_LONG
2688 tprintf("= %#llx", tcp->u_lrval);
2691 tprintf("= %#llo", tcp->u_lrval);
2693 case RVAL_LUDECIMAL:
2694 tprintf("= %llu", tcp->u_lrval);
2697 tprintf("= %lld", tcp->u_lrval);
2702 "invalid rval format\n");
2706 if ((sys_res & RVAL_STR) && tcp->auxstr)
2707 tprintf(" (%s)", tcp->auxstr);
2710 tv_sub(&tv, &tv, &tcp->etime);
2711 tprintf(" <%ld.%06ld>",
2712 (long) tv.tv_sec, (long) tv.tv_usec);
2717 if (fflush(tcp->outf) == EOF)
2720 tcp->flags &= ~TCB_INSYSCALL;
2725 trace_syscall(struct tcb *tcp)
2727 return exiting(tcp) ?
2728 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2732 printargs(struct tcb *tcp)
2734 if (entering(tcp)) {
2737 for (i = 0; i < tcp->u_nargs; i++)
2738 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2744 getrval2(struct tcb *tcp)
2749 #if defined (SPARC) || defined (SPARC64)
2750 struct pt_regs regs;
2751 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
2753 val = regs.u_regs[U_REG_O1];
2755 if (upeek(tcp, 4*(REG_REG0+1), &val) < 0)
2758 if (upeek(tcp, PT_R9, &val) < 0)
2764 if (upeek(tcp, uoff(u_rval2), &val) < 0)
2770 val = tcp->status.PR_REG[R_O1];
2773 val = tcp->status.PR_REG[EDX];
2776 val = tcp->status.PR_REG[RDX];
2779 val = tcp->status.PR_REG[CTX_V1];
2785 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
2793 * Apparently, indirect system calls have already be converted by ptrace(2),
2794 * so if you see "indir" this program has gone astray.
2797 sys_indir(struct tcb *tcp)
2801 if (entering(tcp)) {
2802 scno = tcp->u_arg[0];
2803 if (scno > nsyscalls) {
2804 fprintf(stderr, "Bogus syscall: %u\n", scno);
2807 nargs = sysent[scno].nargs;
2808 tprintf("%s", sysent[scno].sys_name);
2809 for (i = 0; i < nargs; i++)
2810 tprintf(", %#lx", tcp->u_arg[i+1]);
2817 is_restart_error(struct tcb *tcp)
2822 switch (tcp->u_error) {
2824 case ERESTARTNOINTR:
2825 case ERESTARTNOHAND:
2826 case ERESTART_RESTARTBLOCK: