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.
39 #include <sys/param.h>
43 # ifndef PTRACE_PEEKUSR
44 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
46 #elif defined(HAVE_LINUX_PTRACE_H)
47 # undef PTRACE_SYSCALL
48 # ifdef HAVE_STRUCT_IA64_FPREG
49 # define ia64_fpreg XXX_ia64_fpreg
51 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
52 # define pt_all_user_regs XXX_pt_all_user_regs
54 # include <linux/ptrace.h>
56 # undef pt_all_user_regs
60 # undef PTRACE_GETREGS
61 # define PTRACE_GETREGS PTRACE_GETREGS64
62 # undef PTRACE_SETREGS
63 # define PTRACE_SETREGS PTRACE_SETREGS64
67 # include <asm/ptrace_offsets.h>
72 # define ERESTARTSYS 512
74 # ifndef ERESTARTNOINTR
75 # define ERESTARTNOINTR 513
77 # ifndef ERESTARTNOHAND
78 # define ERESTARTNOHAND 514 /* restart if no handler.. */
81 # define ENOIOCTLCMD 515 /* No ioctl command */
83 # ifndef ERESTART_RESTARTBLOCK
84 # define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
88 # warning: NSIG is not defined, using 32
92 /* Ugh. Is this really correct? ARM has no RT signals?! */
99 /* Define these shorthand notations to simplify the syscallent files. */
100 #define TD TRACE_DESC
101 #define TF TRACE_FILE
103 #define TN TRACE_NETWORK
104 #define TP TRACE_PROCESS
105 #define TS TRACE_SIGNAL
106 #define NF SYSCALL_NEVER_FAILS
109 static const struct sysent sysent0[] = {
110 #include "syscallent.h"
113 #if SUPPORTED_PERSONALITIES >= 2
114 static const struct sysent sysent1[] = {
115 # include "syscallent1.h"
119 #if SUPPORTED_PERSONALITIES >= 3
120 static const struct sysent sysent2[] = {
121 # include "syscallent2.h"
125 /* Now undef them since short defines cause wicked namespace pollution. */
136 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
137 * program `ioctlsort', such that the list is sorted by the `code' field.
138 * This has the side-effect of resolving the _IO.. macros into
139 * plain integers, eliminating the need to include here everything
143 static const char *const errnoent0[] = {
144 #include "errnoent.h"
146 static const char *const signalent0[] = {
147 #include "signalent.h"
149 static const struct ioctlent ioctlent0[] = {
150 #include "ioctlent.h"
152 enum { nsyscalls0 = ARRAY_SIZE(sysent0) };
153 enum { nerrnos0 = ARRAY_SIZE(errnoent0) };
154 enum { nsignals0 = ARRAY_SIZE(signalent0) };
155 enum { nioctlents0 = ARRAY_SIZE(ioctlent0) };
156 int qual_flags0[MAX_QUALS];
158 #if SUPPORTED_PERSONALITIES >= 2
159 static const char *const errnoent1[] = {
160 # include "errnoent1.h"
162 static const char *const signalent1[] = {
163 # include "signalent1.h"
165 static const struct ioctlent ioctlent1[] = {
166 # include "ioctlent1.h"
168 enum { nsyscalls1 = ARRAY_SIZE(sysent1) };
169 enum { nerrnos1 = ARRAY_SIZE(errnoent1) };
170 enum { nsignals1 = ARRAY_SIZE(signalent1) };
171 enum { nioctlents1 = ARRAY_SIZE(ioctlent1) };
172 int qual_flags1[MAX_QUALS];
175 #if SUPPORTED_PERSONALITIES >= 3
176 static const char *const errnoent2[] = {
177 # include "errnoent2.h"
179 static const char *const signalent2[] = {
180 # include "signalent2.h"
182 static const struct ioctlent ioctlent2[] = {
183 # include "ioctlent2.h"
185 enum { nsyscalls2 = ARRAY_SIZE(sysent2) };
186 enum { nerrnos2 = ARRAY_SIZE(errnoent2) };
187 enum { nsignals2 = ARRAY_SIZE(signalent2) };
188 enum { nioctlents2 = ARRAY_SIZE(ioctlent2) };
189 int qual_flags2[MAX_QUALS];
192 const struct sysent *sysent;
193 const char *const *errnoent;
194 const char *const *signalent;
195 const struct ioctlent *ioctlent;
202 int current_personality;
204 #ifndef PERSONALITY0_WORDSIZE
205 # define PERSONALITY0_WORDSIZE sizeof(long)
207 const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
208 PERSONALITY0_WORDSIZE,
209 #if SUPPORTED_PERSONALITIES > 1
210 PERSONALITY1_WORDSIZE,
212 #if SUPPORTED_PERSONALITIES > 2
213 PERSONALITY2_WORDSIZE,
218 set_personality(int personality)
220 switch (personality) {
222 errnoent = errnoent0;
225 nsyscalls = nsyscalls0;
226 ioctlent = ioctlent0;
227 nioctlents = nioctlents0;
228 signalent = signalent0;
229 nsignals = nsignals0;
230 qual_flags = qual_flags0;
233 #if SUPPORTED_PERSONALITIES >= 2
235 errnoent = errnoent1;
238 nsyscalls = nsyscalls1;
239 ioctlent = ioctlent1;
240 nioctlents = nioctlents1;
241 signalent = signalent1;
242 nsignals = nsignals1;
243 qual_flags = qual_flags1;
247 #if SUPPORTED_PERSONALITIES >= 3
249 errnoent = errnoent2;
252 nsyscalls = nsyscalls2;
253 ioctlent = ioctlent2;
254 nioctlents = nioctlents2;
255 signalent = signalent2;
256 nsignals = nsignals2;
257 qual_flags = qual_flags2;
262 current_personality = personality;
265 #if SUPPORTED_PERSONALITIES > 1
267 update_personality(struct tcb *tcp, int personality)
269 if (personality == current_personality)
271 set_personality(personality);
273 if (personality == tcp->currpers)
275 tcp->currpers = personality;
277 # if defined(POWERPC64) || defined(X86_64)
279 static const char *const names[] = {"64 bit", "32 bit"};
280 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
281 tcp->pid, names[personality]);
287 static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
289 static const struct qual_options {
291 const char *option_name;
292 int (*qualify)(const char *, int, int);
293 const char *argument_name;
295 { QUAL_TRACE, "trace", qual_syscall, "system call" },
296 { QUAL_TRACE, "t", qual_syscall, "system call" },
297 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
298 { QUAL_ABBREV, "a", qual_syscall, "system call" },
299 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
300 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
301 { QUAL_RAW, "raw", qual_syscall, "system call" },
302 { QUAL_RAW, "x", qual_syscall, "system call" },
303 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
304 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
305 { QUAL_SIGNAL, "s", qual_signal, "signal" },
306 { QUAL_FAULT, "fault", qual_fault, "fault" },
307 { QUAL_FAULT, "faults", qual_fault, "fault" },
308 { QUAL_FAULT, "m", qual_fault, "fault" },
309 { QUAL_READ, "read", qual_desc, "descriptor" },
310 { QUAL_READ, "reads", qual_desc, "descriptor" },
311 { QUAL_READ, "r", qual_desc, "descriptor" },
312 { QUAL_WRITE, "write", qual_desc, "descriptor" },
313 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
314 { QUAL_WRITE, "w", qual_desc, "descriptor" },
315 { 0, NULL, NULL, NULL },
319 qualify_one(int n, int bitflag, int not, int pers)
321 if (pers == 0 || pers < 0) {
323 qual_flags0[n] &= ~bitflag;
325 qual_flags0[n] |= bitflag;
328 #if SUPPORTED_PERSONALITIES >= 2
329 if (pers == 1 || pers < 0) {
331 qual_flags1[n] &= ~bitflag;
333 qual_flags1[n] |= bitflag;
337 #if SUPPORTED_PERSONALITIES >= 3
338 if (pers == 2 || pers < 0) {
340 qual_flags2[n] &= ~bitflag;
342 qual_flags2[n] |= bitflag;
348 qual_syscall(const char *s, int bitflag, int not)
353 if (isdigit((unsigned char)*s)) {
355 if (i < 0 || i >= MAX_QUALS)
357 qualify_one(i, bitflag, not, -1);
360 for (i = 0; i < nsyscalls0; i++)
361 if (strcmp(s, sysent0[i].sys_name) == 0) {
362 qualify_one(i, bitflag, not, 0);
366 #if SUPPORTED_PERSONALITIES >= 2
367 for (i = 0; i < nsyscalls1; i++)
368 if (strcmp(s, sysent1[i].sys_name) == 0) {
369 qualify_one(i, bitflag, not, 1);
374 #if SUPPORTED_PERSONALITIES >= 3
375 for (i = 0; i < nsyscalls2; i++)
376 if (strcmp(s, sysent2[i].sys_name) == 0) {
377 qualify_one(i, bitflag, not, 2);
386 qual_signal(const char *s, int bitflag, int not)
391 if (isdigit((unsigned char)*s)) {
393 if (signo < 0 || signo >= MAX_QUALS)
395 qualify_one(signo, bitflag, not, -1);
398 if (strlen(s) >= sizeof buf)
402 if (strncasecmp(s, "SIG", 3) == 0)
404 for (i = 0; i <= NSIG; i++)
405 if (strcasecmp(s, signame(i) + 3) == 0) {
406 qualify_one(i, bitflag, not, -1);
413 qual_fault(const char *s, int bitflag, int not)
419 qual_desc(const char *s, int bitflag, int not)
421 if (isdigit((unsigned char)*s)) {
423 if (desc < 0 || desc >= MAX_QUALS)
425 qualify_one(desc, bitflag, not, -1);
432 lookup_class(const char *s)
434 if (strcmp(s, "file") == 0)
436 if (strcmp(s, "ipc") == 0)
438 if (strcmp(s, "network") == 0)
439 return TRACE_NETWORK;
440 if (strcmp(s, "process") == 0)
441 return TRACE_PROCESS;
442 if (strcmp(s, "signal") == 0)
444 if (strcmp(s, "desc") == 0)
450 qualify(const char *s)
452 const struct qual_options *opt;
458 opt = &qual_options[0];
459 for (i = 0; (p = qual_options[i].option_name); i++) {
461 if (strncmp(s, p, n) == 0 && s[n] == '=') {
462 opt = &qual_options[i];
472 if (strcmp(s, "none") == 0) {
476 if (strcmp(s, "all") == 0) {
477 for (i = 0; i < MAX_QUALS; i++) {
478 qualify_one(i, opt->bitflag, not, -1);
482 for (i = 0; i < MAX_QUALS; i++) {
483 qualify_one(i, opt->bitflag, !not, -1);
488 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
489 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
490 for (i = 0; i < nsyscalls0; i++)
491 if (sysent0[i].sys_flags & n)
492 qualify_one(i, opt->bitflag, not, 0);
494 #if SUPPORTED_PERSONALITIES >= 2
495 for (i = 0; i < nsyscalls1; i++)
496 if (sysent1[i].sys_flags & n)
497 qualify_one(i, opt->bitflag, not, 1);
500 #if SUPPORTED_PERSONALITIES >= 3
501 for (i = 0; i < nsyscalls2; i++)
502 if (sysent2[i].sys_flags & n)
503 qualify_one(i, opt->bitflag, not, 2);
508 if (opt->qualify(p, opt->bitflag, not)) {
509 error_msg_and_die("invalid %s '%s'",
510 opt->argument_name, p);
517 #ifdef SYS_socket_subcall
519 decode_socket_subcall(struct tcb *tcp)
522 unsigned int i, size;
524 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
527 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
528 addr = tcp->u_arg[1];
529 tcp->u_nargs = sysent[tcp->scno].nargs;
530 size = personality_wordsize[current_personality];
531 for (i = 0; i < tcp->u_nargs; ++i) {
532 if (size == sizeof(int)) {
534 if (umove(tcp, addr, &arg) < 0)
540 if (umove(tcp, addr, &arg) < 0)
549 #ifdef SYS_ipc_subcall
551 decode_ipc_subcall(struct tcb *tcp)
555 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
558 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
559 tcp->u_nargs = sysent[tcp->scno].nargs;
560 for (i = 0; i < tcp->u_nargs; i++)
561 tcp->u_arg[i] = tcp->u_arg[i + 1];
566 printargs(struct tcb *tcp)
571 for (i = 0; i < tcp->u_nargs; i++)
572 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
578 printargs_lu(struct tcb *tcp)
583 for (i = 0; i < tcp->u_nargs; i++)
584 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
590 printargs_ld(struct tcb *tcp)
595 for (i = 0; i < tcp->u_nargs; i++)
596 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
602 getrval2(struct tcb *tcp)
606 #if defined(SPARC) || defined(SPARC64)
608 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
610 val = regs.u_regs[U_REG_O1];
612 if (upeek(tcp, 4*(REG_REG0+1), &val) < 0)
615 if (upeek(tcp, PT_R9, &val) < 0)
623 is_restart_error(struct tcb *tcp)
625 switch (tcp->u_error) {
629 case ERESTART_RESTARTBLOCK:
638 struct pt_regs i386_regs;
639 #elif defined(X86_64)
641 * On 32 bits, pt_regs and user_regs_struct are the same,
642 * but on 64 bits, user_regs_struct has six more fields:
643 * fs_base, gs_base, ds, es, fs, gs.
644 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
646 static struct user_regs_struct x86_64_regs;
648 long r8, r10, psr; /* TODO: make static? */
649 long ia32 = 0; /* not static */
650 #elif defined(POWERPC)
657 static struct pt_regs regs;
662 static struct pt_regs regs;
663 #elif defined(SPARC) || defined(SPARC64)
664 static struct pt_regs regs;
665 static unsigned long trap;
666 #elif defined(LINUX_MIPSN32)
672 #elif defined(S390) || defined(S390X)
675 static long syscall_mode;
682 #elif defined(CRISV10) || defined(CRISV32)
684 #elif defined(MICROBLAZE)
689 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
690 * 1: ok, continue in trace_syscall().
691 * other: error, trace_syscall() should print error indicator
692 * ("????" etc) and bail out.
696 get_scno(struct tcb *tcp)
700 #if defined(S390) || defined(S390X)
701 if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
704 if (syscall_mode != -ENOSYS) {
706 * Since kernel version 2.5.44 the scno gets passed in gpr2.
711 * Old style of "passing" the scno via the SVC instruction.
713 long opcode, offset_reg, tmp;
715 static const int gpr_offset[16] = {
716 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
717 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
718 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
719 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
722 if (upeek(tcp, PT_PSWADDR, &pc) < 0)
725 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-sizeof(long)), 0);
727 perror("peektext(pc-oneword)");
732 * We have to check if the SVC got executed directly or via an
733 * EXECUTE instruction. In case of EXECUTE it is necessary to do
734 * instruction decoding to derive the system call number.
735 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
736 * so that this doesn't work if a SVC opcode is part of an EXECUTE
737 * opcode. Since there is no way to find out the opcode size this
738 * is the best we can do...
740 if ((opcode & 0xff00) == 0x0a00) {
742 scno = opcode & 0xff;
745 /* SVC got executed by EXECUTE instruction */
748 * Do instruction decoding of EXECUTE. If you really want to
749 * understand this, read the Principles of Operations.
751 svc_addr = (void *) (opcode & 0xfff);
754 offset_reg = (opcode & 0x000f0000) >> 16;
755 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
760 offset_reg = (opcode & 0x0000f000) >> 12;
761 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
765 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
774 offset_reg = (opcode & 0x00f00000) >> 20;
775 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
778 scno = (scno | tmp) & 0xff;
781 #elif defined(POWERPC)
782 if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
785 /* TODO: speed up strace by not doing this at every syscall.
786 * We only need to do it after execve.
791 /* Check for 64/32 bit mode. */
792 if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
794 /* SF is bit 0 of MSR */
799 update_personality(tcp, currpers);
802 /* Read complete register set in one go. */
803 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, ®s) < 0)
807 if (upeek(tcp, PT_ORIG_P0, &scno))
810 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &i386_regs) < 0)
812 scno = i386_regs.orig_eax;
813 #elif defined(X86_64)
815 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &x86_64_regs) < 0)
817 scno = x86_64_regs.orig_rax;
819 /* Check CS register value. On x86-64 linux it is:
820 * 0x33 for long mode (64 bit)
821 * 0x23 for compatibility mode (32 bit)
823 switch (x86_64_regs.cs) {
824 case 0x23: currpers = 1; break;
825 case 0x33: currpers = 0; break;
827 fprintf(stderr, "Unknown value CS=0x%08X while "
828 "detecting personality of process "
829 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
830 currpers = current_personality;
834 /* This version analyzes the opcode of a syscall instruction.
835 * (int 0x80 on i386 vs. syscall on x86-64)
836 * It works, but is too complicated.
838 unsigned long val, rip, i;
840 rip = x86_64_regs.rip;
842 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
846 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
848 fprintf(stderr, "ptrace_peektext failed: %s\n",
850 switch (call & 0xffff) {
851 /* x86-64: syscall = 0x0f 0x05 */
852 case 0x050f: currpers = 0; break;
853 /* i386: int 0x80 = 0xcd 0x80 */
854 case 0x80cd: currpers = 1; break;
856 currpers = current_personality;
858 "Unknown syscall opcode (0x%04X) while "
859 "detecting personality of process "
860 "PID=%d\n", (int)call, tcp->pid);
864 update_personality(tcp, currpers);
866 # define IA64_PSR_IS ((long)1 << 34)
867 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
868 ia32 = (psr & IA64_PSR_IS) != 0;
870 if (upeek(tcp, PT_R1, &scno) < 0)
873 if (upeek(tcp, PT_R15, &scno) < 0)
877 /* Read complete register set in one go. */
878 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (void *)®s) == -1)
882 * We only need to grab the syscall number on syscall entry.
884 if (regs.ARM_ip == 0) {
886 * Note: we only deal with only 32-bit CPUs here.
888 if (regs.ARM_cpsr & 0x20) {
890 * Get the Thumb-mode system call number
895 * Get the ARM-mode system call number
898 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(regs.ARM_pc - 4), NULL);
902 /* Handle the EABI syscall convention. We do not
903 bother converting structures between the two
904 ABIs, but basic functionality should work even
905 if strace and the traced program have different
907 if (scno == 0xef000000) {
910 if ((scno & 0x0ff00000) != 0x0f900000) {
911 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
917 * Fixup the syscall number
922 if (scno & 0x0f0000) {
924 * Handle ARM specific syscall
926 update_personality(tcp, 1);
929 update_personality(tcp, 0);
932 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
933 tcp->flags |= TCB_INSYSCALL;
936 if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
938 #elif defined(LINUX_MIPSN32)
939 unsigned long long regs[38];
941 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
947 if (!SCNO_IN_RANGE(scno)) {
948 if (a3 == 0 || a3 == -1) {
950 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
955 if (upeek(tcp, REG_A3, &a3) < 0)
957 if (upeek(tcp, REG_V0, &scno) < 0)
960 if (!SCNO_IN_RANGE(scno)) {
961 if (a3 == 0 || a3 == -1) {
963 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
968 if (upeek(tcp, REG_A3, &a3) < 0)
970 if (upeek(tcp, REG_R0, &scno) < 0)
974 * Do some sanity checks to figure out if it's
975 * really a syscall entry
977 if (!SCNO_IN_RANGE(scno)) {
978 if (a3 == 0 || a3 == -1) {
980 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
984 #elif defined(SPARC) || defined(SPARC64)
985 /* Everything we need is in the current register set. */
986 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
989 /* Disassemble the syscall trap. */
990 /* Retrieve the syscall trap instruction. */
992 # if defined(SPARC64)
993 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.tpc, 0);
996 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.pc, 0);
1001 /* Disassemble the trap to see what personality to use. */
1004 /* Linux/SPARC syscall trap. */
1005 update_personality(tcp, 0);
1008 /* Linux/SPARC64 syscall trap. */
1009 update_personality(tcp, 2);
1012 /* SunOS syscall trap. (pers 1) */
1013 fprintf(stderr, "syscall: SunOS no support\n");
1016 /* Solaris 2.x syscall trap. (per 2) */
1017 update_personality(tcp, 1);
1020 /* NetBSD/FreeBSD syscall trap. */
1021 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1024 /* Solaris 2.x gettimeofday */
1025 update_personality(tcp, 1);
1028 # if defined(SPARC64)
1029 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, regs.tpc);
1031 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, regs.pc);
1036 /* Extract the system call number from the registers. */
1037 if (trap == 0x91d02027)
1040 scno = regs.u_regs[U_REG_G1];
1042 scno = regs.u_regs[U_REG_O0];
1043 memmove(®s.u_regs[U_REG_O0], ®s.u_regs[U_REG_O1], 7*sizeof(regs.u_regs[0]));
1046 if (upeek(tcp, PT_GR20, &scno) < 0)
1050 * In the new syscall ABI, the system call number is in R3.
1052 if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
1056 /* Odd as it may seem, a glibc bug has been known to cause
1057 glibc to issue bogus negative syscall numbers. So for
1058 our purposes, make strace print what it *should* have been */
1059 long correct_scno = (scno & 0xff);
1062 "Detected glibc bug: bogus system call"
1063 " number = %ld, correcting to %ld\n",
1066 scno = correct_scno;
1069 if (upeek(tcp, REG_SYSCALL, &scno) < 0)
1072 #elif defined(CRISV10) || defined(CRISV32)
1073 if (upeek(tcp, 4*PT_R9, &scno) < 0)
1076 if (upeek(tcp, PTREGS_OFFSET_REG(10), &scno) < 0)
1078 #elif defined(MICROBLAZE)
1079 if (upeek(tcp, 0, &scno) < 0)
1084 /* new syscall ABI returns result in R0 */
1085 if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0)
1088 /* ABI defines result returned in r9 */
1089 if (upeek(tcp, REG_GENERAL(9), (long *)&r9) < 0)
1097 /* Called at each syscall entry.
1099 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1100 * 1: ok, continue in trace_syscall().
1101 * other: error, trace_syscall() should print error indicator
1102 * ("????" etc) and bail out.
1105 syscall_fixup_on_sysenter(struct tcb *tcp)
1107 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
1109 if (i386_regs.eax != -ENOSYS) {
1111 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1114 #elif defined(X86_64)
1116 long rax = x86_64_regs.rax;
1117 if (current_personality == 1)
1118 rax = (int)rax; /* sign extend from 32 bits */
1119 if (rax != -ENOSYS) {
1121 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1125 #elif defined(S390) || defined(S390X)
1126 /* TODO: we already fetched PT_GPR2 in get_scno
1127 * and stored it in syscall_mode, reuse it here
1128 * instead of re-fetching?
1130 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1132 if (syscall_mode != -ENOSYS)
1133 syscall_mode = tcp->scno;
1134 if (gpr2 != syscall_mode) {
1136 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
1140 /* TODO? Eliminate upeek's in arches below like we did in x86 */
1141 if (upeek(tcp, 4*PT_D0, &d0) < 0)
1143 if (d0 != -ENOSYS) {
1145 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", d0);
1149 if (upeek(tcp, PT_R10, &r10) < 0)
1151 if (upeek(tcp, PT_R8, &r8) < 0)
1153 if (ia32 && r8 != -ENOSYS) {
1155 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", r8);
1158 #elif defined(CRISV10) || defined(CRISV32)
1159 if (upeek(tcp, 4*PT_R10, &r10) < 0)
1161 if (r10 != -ENOSYS) {
1163 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", r10);
1166 #elif defined(MICROBLAZE)
1167 if (upeek(tcp, 3 * 4, &r3) < 0)
1169 if (r3 != -ENOSYS) {
1171 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", r3);
1179 internal_syscall(struct tcb *tcp)
1182 * We must always trace a few critical system calls in order to
1183 * correctly support following forks in the presence of tracing
1188 if (!SCNO_IN_RANGE(tcp->scno))
1191 func = sysent[tcp->scno].sys_func;
1193 if ( sys_fork == func
1194 || sys_vfork == func
1195 || sys_clone == func
1197 return internal_fork(tcp);
1199 #if defined(TCB_WAITEXECVE)
1200 if ( sys_execve == func
1201 # if defined(SPARC) || defined(SPARC64)
1202 || sys_execv == func
1205 return internal_exec(tcp);
1212 syscall_enter(struct tcb *tcp)
1216 if (SCNO_IN_RANGE(tcp->scno))
1217 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1219 nargs = tcp->u_nargs = MAX_ARGS;
1221 #if defined(S390) || defined(S390X)
1222 for (i = 0; i < nargs; ++i)
1223 if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1225 #elif defined(ALPHA)
1226 for (i = 0; i < nargs; ++i)
1227 if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
1231 unsigned long *out0, cfm, sof, sol;
1233 /* be backwards compatible with kernel < 2.4.4... */
1235 # define PT_RBS_END PT_AR_BSP
1238 if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
1240 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1243 sof = (cfm >> 0) & 0x7f;
1244 sol = (cfm >> 7) & 0x7f;
1245 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1247 for (i = 0; i < nargs; ++i) {
1248 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1249 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1253 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1254 PT_R9 /* ECX = out1 */,
1255 PT_R10 /* EDX = out2 */,
1256 PT_R14 /* ESI = out3 */,
1257 PT_R15 /* EDI = out4 */,
1258 PT_R13 /* EBP = out5 */};
1260 for (i = 0; i < nargs; ++i) {
1261 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1263 /* truncate away IVE sign-extension */
1264 tcp->u_arg[i] &= 0xffffffff;
1267 #elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
1268 /* N32 and N64 both use up to six registers. */
1269 unsigned long long regs[38];
1271 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1274 for (i = 0; i < nargs; ++i) {
1275 tcp->u_arg[i] = regs[REG_A0 + i];
1276 # if defined(LINUX_MIPSN32)
1277 tcp->ext_arg[i] = regs[REG_A0 + i];
1284 if (upeek(tcp, REG_SP, &sp) < 0)
1286 for (i = 0; i < 4; ++i)
1287 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1289 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
1290 (char *)(tcp->u_arg + 4));
1292 for (i = 0; i < nargs; ++i)
1293 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1296 #elif defined(POWERPC)
1298 # define PT_ORIG_R3 34
1300 for (i = 0; i < nargs; ++i) {
1301 if (upeek(tcp, (i==0) ?
1302 (sizeof(unsigned long) * PT_ORIG_R3) :
1303 ((i+PT_R3) * sizeof(unsigned long)),
1304 &tcp->u_arg[i]) < 0)
1307 #elif defined(SPARC) || defined(SPARC64)
1308 for (i = 0; i < nargs; ++i)
1309 tcp->u_arg[i] = regs.u_regs[U_REG_O0 + i];
1311 for (i = 0; i < nargs; ++i)
1312 if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1315 for (i = 0; i < nargs; ++i)
1316 tcp->u_arg[i] = regs.uregs[i];
1317 #elif defined(AVR32)
1320 tcp->u_arg[0] = regs.r12;
1321 tcp->u_arg[1] = regs.r11;
1322 tcp->u_arg[2] = regs.r10;
1323 tcp->u_arg[3] = regs.r9;
1324 tcp->u_arg[4] = regs.r5;
1325 tcp->u_arg[5] = regs.r3;
1327 static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
1329 for (i = 0; i < nargs; ++i)
1330 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1333 static const int syscall_regs[MAX_ARGS] = {
1334 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1335 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
1338 for (i = 0; i < nargs; ++i)
1339 if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
1343 /* Registers used by SH5 Linux system calls for parameters */
1344 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
1346 for (i = 0; i < nargs; ++i)
1347 if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1349 #elif defined(X86_64)
1352 if (current_personality == 0) { /* x86-64 ABI */
1353 tcp->u_arg[0] = x86_64_regs.rdi;
1354 tcp->u_arg[1] = x86_64_regs.rsi;
1355 tcp->u_arg[2] = x86_64_regs.rdx;
1356 tcp->u_arg[3] = x86_64_regs.r10;
1357 tcp->u_arg[4] = x86_64_regs.r8;
1358 tcp->u_arg[5] = x86_64_regs.r9;
1359 } else { /* i386 ABI */
1360 /* Sign-extend lower 32 bits */
1361 tcp->u_arg[0] = (long)(int)x86_64_regs.rbx;
1362 tcp->u_arg[1] = (long)(int)x86_64_regs.rcx;
1363 tcp->u_arg[2] = (long)(int)x86_64_regs.rdx;
1364 tcp->u_arg[3] = (long)(int)x86_64_regs.rsi;
1365 tcp->u_arg[4] = (long)(int)x86_64_regs.rdi;
1366 tcp->u_arg[5] = (long)(int)x86_64_regs.rbp;
1368 #elif defined(MICROBLAZE)
1369 for (i = 0; i < nargs; ++i)
1370 if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1372 #elif defined(CRISV10) || defined(CRISV32)
1373 static const int crisregs[MAX_ARGS] = {
1374 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
1375 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
1378 for (i = 0; i < nargs; ++i)
1379 if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0)
1382 for (i = 0; i < nargs; ++i)
1383 if (upeek(tcp, PTREGS_OFFSET_REG(i), &tcp->u_arg[i]) < 0)
1386 for (i = 0; i < nargs; ++i)
1387 if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1392 tcp->u_arg[0] = i386_regs.ebx;
1393 tcp->u_arg[1] = i386_regs.ecx;
1394 tcp->u_arg[2] = i386_regs.edx;
1395 tcp->u_arg[3] = i386_regs.esi;
1396 tcp->u_arg[4] = i386_regs.edi;
1397 tcp->u_arg[5] = i386_regs.ebp;
1398 #else /* Other architecture (32bits specific) */
1399 for (i = 0; i < nargs; ++i)
1400 if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
1407 trace_syscall_entering(struct tcb *tcp)
1411 #if defined TCB_WAITEXECVE
1412 if (tcp->flags & TCB_WAITEXECVE) {
1413 /* This is the post-execve SIGTRAP. */
1414 tcp->flags &= ~TCB_WAITEXECVE;
1419 scno_good = res = get_scno(tcp);
1423 res = syscall_fixup_on_sysenter(tcp);
1427 res = syscall_enter(tcp);
1433 tcp->flags &= ~TCB_REPRINT;
1435 tprintf("????" /* anti-trigraph gap */ "(");
1436 else if (!SCNO_IN_RANGE(tcp->scno))
1437 tprintf("syscall_%lu(", tcp->scno);
1439 tprintf("%s(", sysent[tcp->scno].sys_name);
1441 * " <unavailable>" will be added later by the code which
1442 * detects ptrace errors.
1447 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
1448 while (SCNO_IN_RANGE(tcp->scno)) {
1449 # ifdef SYS_socket_subcall
1450 if (sysent[tcp->scno].sys_func == sys_socketcall) {
1451 decode_socket_subcall(tcp);
1455 # ifdef SYS_ipc_subcall
1456 if (sysent[tcp->scno].sys_func == sys_ipc) {
1457 decode_ipc_subcall(tcp);
1463 #endif /* SYS_socket_subcall || SYS_ipc_subcall */
1465 internal_syscall(tcp);
1467 if ((SCNO_IN_RANGE(tcp->scno) &&
1468 !(qual_flags[tcp->scno] & QUAL_TRACE)) ||
1469 (tracing_paths && !pathtrace_match(tcp))) {
1470 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1474 tcp->flags &= ~TCB_FILTERED;
1476 if (cflag == CFLAG_ONLY_STATS) {
1482 tcp->flags &= ~TCB_REPRINT;
1483 if (!SCNO_IN_RANGE(tcp->scno))
1484 tprintf("syscall_%lu(", tcp->scno);
1486 tprintf("%s(", sysent[tcp->scno].sys_name);
1487 if (!SCNO_IN_RANGE(tcp->scno) ||
1488 ((qual_flags[tcp->scno] & QUAL_RAW) &&
1489 sysent[tcp->scno].sys_func != sys_exit))
1490 res = printargs(tcp);
1492 res = (*sysent[tcp->scno].sys_func)(tcp);
1494 if (fflush(tcp->outf) == EOF)
1497 tcp->flags |= TCB_INSYSCALL;
1498 /* Measure the entrance time as late as possible to avoid errors. */
1500 gettimeofday(&tcp->etime, NULL);
1505 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1506 * 1: ok, continue in trace_syscall().
1507 * other: error, trace_syscall() should print error indicator
1508 * ("????" etc) and bail out.
1511 get_syscall_result(struct tcb *tcp)
1513 #if defined(S390) || defined(S390X)
1514 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1516 #elif defined(POWERPC)
1517 # define SO_MASK 0x10000000
1520 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1522 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &result) < 0)
1524 if (flags & SO_MASK)
1527 #elif defined(AVR32)
1528 /* Read complete register set in one go. */
1529 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, ®s) < 0)
1532 if (upeek(tcp, PT_R0, &r0) < 0)
1535 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &i386_regs) < 0)
1537 #elif defined(X86_64)
1538 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &x86_64_regs) < 0)
1541 # define IA64_PSR_IS ((long)1 << 34)
1542 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
1543 ia32 = (psr & IA64_PSR_IS) != 0;
1544 if (upeek(tcp, PT_R8, &r8) < 0)
1546 if (upeek(tcp, PT_R10, &r10) < 0)
1549 /* Read complete register set in one go. */
1550 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (void *)®s) == -1)
1553 if (upeek(tcp, 4*PT_D0, &d0) < 0)
1555 #elif defined(LINUX_MIPSN32)
1556 unsigned long long regs[38];
1558 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1563 if (upeek(tcp, REG_A3, &a3) < 0)
1565 if (upeek(tcp, REG_V0, &r2) < 0)
1567 #elif defined(ALPHA)
1568 if (upeek(tcp, REG_A3, &a3) < 0)
1570 if (upeek(tcp, REG_R0, &r0) < 0)
1572 #elif defined(SPARC) || defined(SPARC64)
1573 /* Everything we need is in the current register set. */
1574 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0)
1577 if (upeek(tcp, PT_GR28, &r28) < 0)
1581 #elif defined(CRISV10) || defined(CRISV32)
1582 if (upeek(tcp, 4*PT_R10, &r10) < 0)
1585 #elif defined(MICROBLAZE)
1586 if (upeek(tcp, 3 * 4, &r3) < 0)
1591 /* new syscall ABI returns result in R0 */
1592 if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0)
1595 /* ABI defines result returned in r9 */
1596 if (upeek(tcp, REG_GENERAL(9), (long *)&r9) < 0)
1603 /* Called at each syscall exit.
1605 * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1606 * 1: ok, continue in trace_syscall().
1607 * other: error, trace_syscall() should print error indicator
1608 * ("????" etc) and bail out.
1611 syscall_fixup_on_sysexit(struct tcb *tcp)
1613 #if defined(S390) || defined(S390X)
1614 if (syscall_mode != -ENOSYS)
1615 syscall_mode = tcp->scno;
1616 if ((tcp->flags & TCB_WAITEXECVE)
1617 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1619 * Return from execve.
1620 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1621 * flag set for the post-execve SIGTRAP to see and reset.
1630 * Check the syscall return value register value for whether it is
1631 * a negated errno code indicating an error, or a success return value.
1634 is_negated_errno(unsigned long int val)
1636 unsigned long int max = -(long int) nerrnos;
1637 #if SUPPORTED_PERSONALITIES > 1
1638 if (personality_wordsize[current_personality] < sizeof(val)) {
1639 val = (unsigned int) val;
1640 max = (unsigned int) max;
1647 get_error(struct tcb *tcp)
1650 int check_errno = 1;
1651 if (SCNO_IN_RANGE(tcp->scno) &&
1652 sysent[tcp->scno].sys_flags & SYSCALL_NEVER_FAILS) {
1655 #if defined(S390) || defined(S390X)
1656 if (check_errno && is_negated_errno(gpr2)) {
1664 if (check_errno && is_negated_errno(i386_regs.eax)) {
1666 u_error = -i386_regs.eax;
1669 tcp->u_rval = i386_regs.eax;
1671 #elif defined(X86_64)
1672 if (check_errno && is_negated_errno(x86_64_regs.rax)) {
1674 u_error = -x86_64_regs.rax;
1677 tcp->u_rval = x86_64_regs.rax;
1684 if (check_errno && is_negated_errno(err)) {
1692 if (check_errno && r10) {
1700 if (check_errno && a3) {
1706 #elif defined(POWERPC)
1707 if (check_errno && is_negated_errno(result)) {
1712 tcp->u_rval = result;
1715 if (check_errno && is_negated_errno(d0)) {
1723 if (check_errno && is_negated_errno(regs.ARM_r0)) {
1725 u_error = -regs.ARM_r0;
1728 tcp->u_rval = regs.ARM_r0;
1730 #elif defined(AVR32)
1731 if (check_errno && regs.r12 && (unsigned) -regs.r12 < nerrnos) {
1733 u_error = -regs.r12;
1736 tcp->u_rval = regs.r12;
1739 if (check_errno && is_negated_errno(r0)) {
1745 #elif defined(ALPHA)
1746 if (check_errno && a3) {
1753 #elif defined(SPARC)
1754 if (check_errno && regs.psr & PSR_C) {
1756 u_error = regs.u_regs[U_REG_O0];
1759 tcp->u_rval = regs.u_regs[U_REG_O0];
1761 #elif defined(SPARC64)
1762 if (check_errno && regs.tstate & 0x1100000000UL) {
1764 u_error = regs.u_regs[U_REG_O0];
1767 tcp->u_rval = regs.u_regs[U_REG_O0];
1770 if (check_errno && is_negated_errno(r28)) {
1778 if (check_errno && is_negated_errno(r0)) {
1786 if (check_errno && is_negated_errno(r9)) {
1793 #elif defined(CRISV10) || defined(CRISV32)
1794 if (check_errno && r10 && (unsigned) -r10 < nerrnos) {
1803 if (upeek(tcp, PTREGS_OFFSET_REG(0), &rval) < 0)
1805 if (check_errno && rval < 0 && rval > -nerrnos) {
1812 #elif defined(MICROBLAZE)
1813 if (check_errno && is_negated_errno(r3)) {
1821 tcp->u_error = u_error;
1826 dumpio(struct tcb *tcp)
1830 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
1832 if (!SCNO_IN_RANGE(tcp->scno))
1834 if (sysent[tcp->scno].sys_func == printargs)
1836 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
1837 if (sysent[tcp->scno].sys_func == sys_read ||
1838 sysent[tcp->scno].sys_func == sys_pread ||
1839 sysent[tcp->scno].sys_func == sys_recv ||
1840 sysent[tcp->scno].sys_func == sys_recvfrom)
1841 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
1842 else if (sysent[tcp->scno].sys_func == sys_readv)
1843 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
1846 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
1847 if (sysent[tcp->scno].sys_func == sys_write ||
1848 sysent[tcp->scno].sys_func == sys_pwrite ||
1849 sysent[tcp->scno].sys_func == sys_send ||
1850 sysent[tcp->scno].sys_func == sys_sendto)
1851 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
1852 else if (sysent[tcp->scno].sys_func == sys_writev)
1853 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
1859 trace_syscall_exiting(struct tcb *tcp)
1866 /* Measure the exit time as early as possible to avoid errors. */
1868 gettimeofday(&tv, NULL);
1870 #if SUPPORTED_PERSONALITIES > 1
1871 update_personality(tcp, tcp->currpers);
1873 res = get_syscall_result(tcp);
1877 res = syscall_fixup_on_sysexit(tcp);
1881 res = get_error(tcp);
1885 internal_syscall(tcp);
1887 if (res == 1 && filtered(tcp)) {
1891 /* TODO: TCB_REPRINT is probably not necessary:
1892 * we can determine whether reprinting is needed
1893 * by examining printing_tcp. Something like:
1894 * if not in -ff mode, and printing_tcp != tcp,
1895 * then the log is not currenlty ends with *our*
1896 * syscall entry output, but with something else,
1897 * and we need to reprint.
1898 * If we'd implement this, printing_tcp = tcp
1899 * assignments in code below can be made more logical.
1902 if (tcp->flags & TCB_REPRINT) {
1904 if (!SCNO_IN_RANGE(tcp->scno))
1905 tprintf("<... syscall_%lu resumed> ", tcp->scno);
1907 tprintf("<... %s resumed> ", sysent[tcp->scno].sys_name);
1911 struct timeval t = tv;
1912 count_syscall(tcp, &t);
1913 if (cflag == CFLAG_ONLY_STATS) {
1922 tprints("= ? <unavailable>\n");
1924 tcp->flags &= ~TCB_INSYSCALL;
1928 if (!SCNO_IN_RANGE(tcp->scno)
1929 || (qual_flags[tcp->scno] & QUAL_RAW)) {
1931 sys_res = printargs(tcp);
1933 /* FIXME: not_failing_only (IOW, option -z) is broken:
1934 * failure of syscall is known only after syscall return.
1935 * Thus we end up with something like this on, say, ENOENT:
1936 * open("doesnt_exist", O_RDONLY <unfinished ...>
1937 * {next syscall decode}
1938 * whereas the intended result is that open(...) line
1939 * is not shown at all.
1941 if (not_failing_only && tcp->u_error)
1942 goto ret; /* ignore failed syscalls */
1944 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1949 u_error = tcp->u_error;
1950 if (!SCNO_IN_RANGE(tcp->scno) ||
1951 qual_flags[tcp->scno] & QUAL_RAW) {
1953 tprintf("= -1 (errno %ld)", u_error);
1955 tprintf("= %#lx", tcp->u_rval);
1957 else if (!(sys_res & RVAL_NONE) && u_error) {
1959 /* Blocked signals do not interrupt any syscalls.
1960 * In this case syscalls don't return ERESTARTfoo codes.
1962 * Deadly signals set to SIG_DFL interrupt syscalls
1963 * and kill the process regardless of which of the codes below
1964 * is returned by the interrupted syscall.
1965 * In some cases, kernel forces a kernel-generated deadly
1966 * signal to be unblocked and set to SIG_DFL (and thus cause
1967 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
1968 * or SIGILL. (The alternative is to leave process spinning
1969 * forever on the faulty instruction - not useful).
1971 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
1972 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
1973 * but kernel will always restart them.
1976 /* Most common type of signal-interrupted syscall exit code.
1977 * The system call will be restarted with the same arguments
1978 * if SA_RESTART is set; otherwise, it will fail with EINTR.
1980 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
1982 case ERESTARTNOINTR:
1983 /* Rare. For example, fork() returns this if interrupted.
1984 * SA_RESTART is ignored (assumed set): the restart is unconditional.
1986 tprints("= ? ERESTARTNOINTR (To be restarted)");
1988 case ERESTARTNOHAND:
1989 /* pause(), rt_sigsuspend() etc use this code.
1990 * SA_RESTART is ignored (assumed not set):
1991 * syscall won't restart (will return EINTR instead)
1992 * even after signal with SA_RESTART set.
1993 * However, after SIG_IGN or SIG_DFL signal it will.
1995 tprints("= ? ERESTARTNOHAND (Interrupted by signal)");
1997 case ERESTART_RESTARTBLOCK:
1998 /* Syscalls like nanosleep(), poll() which can't be
1999 * restarted with their original arguments use this
2000 * code. Kernel will execute restart_syscall() instead,
2001 * which changes arguments before restarting syscall.
2002 * SA_RESTART is ignored (assumed not set) similarly
2003 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2004 * since restart data is saved in "restart block"
2005 * in task struct, and if signal handler uses a syscall
2006 * which in turn saves another such restart block,
2007 * old data is lost and restart becomes impossible)
2009 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
2013 tprintf("= -1 E??? (errno %ld)", u_error);
2014 else if (u_error < nerrnos)
2015 tprintf("= -1 %s (%s)", errnoent[u_error],
2018 tprintf("= -1 ERRNO_%ld (%s)", u_error,
2022 if ((sys_res & RVAL_STR) && tcp->auxstr)
2023 tprintf(" (%s)", tcp->auxstr);
2026 if (sys_res & RVAL_NONE)
2029 switch (sys_res & RVAL_MASK) {
2031 tprintf("= %#lx", tcp->u_rval);
2034 tprintf("= %#lo", tcp->u_rval);
2037 tprintf("= %lu", tcp->u_rval);
2040 tprintf("= %ld", tcp->u_rval);
2042 #ifdef HAVE_LONG_LONG
2044 tprintf("= %#llx", tcp->u_lrval);
2047 tprintf("= %#llo", tcp->u_lrval);
2049 case RVAL_LUDECIMAL:
2050 tprintf("= %llu", tcp->u_lrval);
2053 tprintf("= %lld", tcp->u_lrval);
2058 "invalid rval format\n");
2062 if ((sys_res & RVAL_STR) && tcp->auxstr)
2063 tprintf(" (%s)", tcp->auxstr);
2066 tv_sub(&tv, &tv, &tcp->etime);
2067 tprintf(" <%ld.%06ld>",
2068 (long) tv.tv_sec, (long) tv.tv_usec);
2075 tcp->flags &= ~TCB_INSYSCALL;
2080 trace_syscall(struct tcb *tcp)
2082 return exiting(tcp) ?
2083 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);