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.
36 #include <sys/param.h>
40 #elif defined(HAVE_LINUX_PTRACE_H)
41 # undef PTRACE_SYSCALL
42 # ifdef HAVE_STRUCT_IA64_FPREG
43 # define ia64_fpreg XXX_ia64_fpreg
45 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
46 # define pt_all_user_regs XXX_pt_all_user_regs
48 # ifdef HAVE_STRUCT_PTRACE_PEEKSIGINFO_ARGS
49 # define ptrace_peeksiginfo_args XXX_ptrace_peeksiginfo_args
51 # include <linux/ptrace.h>
52 # undef ptrace_peeksiginfo_args
54 # undef pt_all_user_regs
58 # undef PTRACE_GETREGS
59 # define PTRACE_GETREGS PTRACE_GETREGS64
60 # undef PTRACE_SETREGS
61 # define PTRACE_SETREGS PTRACE_SETREGS64
65 # include <asm/ptrace_offsets.h>
69 /* for struct iovec */
77 # include <asm/ptrace.h>
81 # include <asm/ptrace.h>
85 # warning: NSIG is not defined, using 32
91 /* Define these shorthand notations to simplify the syscallent files. */
95 #define TN TRACE_NETWORK
96 #define TP TRACE_PROCESS
97 #define TS TRACE_SIGNAL
98 #define TM TRACE_MEMORY
99 #define NF SYSCALL_NEVER_FAILS
101 #define SI STACKTRACE_INVALIDATE_CACHE
102 #define SE STACKTRACE_CAPTURE_ON_ENTER
104 const struct_sysent sysent0[] = {
105 #include "syscallent.h"
108 #if SUPPORTED_PERSONALITIES > 1
109 static const struct_sysent sysent1[] = {
110 # include "syscallent1.h"
114 #if SUPPORTED_PERSONALITIES > 2
115 static const struct_sysent sysent2[] = {
116 # include "syscallent2.h"
120 /* Now undef them since short defines cause wicked namespace pollution. */
134 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
135 * program `ioctlsort', such that the list is sorted by the `code' field.
136 * This has the side-effect of resolving the _IO.. macros into
137 * plain integers, eliminating the need to include here everything
141 const char *const errnoent0[] = {
142 #include "errnoent.h"
144 const char *const signalent0[] = {
145 #include "signalent.h"
147 const struct_ioctlent ioctlent0[] = {
148 #include "ioctlent.h"
151 #if SUPPORTED_PERSONALITIES > 1
152 static const char *const errnoent1[] = {
153 # include "errnoent1.h"
155 static const char *const signalent1[] = {
156 # include "signalent1.h"
158 static const struct_ioctlent ioctlent1[] = {
159 # include "ioctlent1.h"
163 #if SUPPORTED_PERSONALITIES > 2
164 static const char *const errnoent2[] = {
165 # include "errnoent2.h"
167 static const char *const signalent2[] = {
168 # include "signalent2.h"
170 static const struct_ioctlent ioctlent2[] = {
171 # include "ioctlent2.h"
176 nsyscalls0 = ARRAY_SIZE(sysent0)
177 #if SUPPORTED_PERSONALITIES > 1
178 , nsyscalls1 = ARRAY_SIZE(sysent1)
179 # if SUPPORTED_PERSONALITIES > 2
180 , nsyscalls2 = ARRAY_SIZE(sysent2)
186 nerrnos0 = ARRAY_SIZE(errnoent0)
187 #if SUPPORTED_PERSONALITIES > 1
188 , nerrnos1 = ARRAY_SIZE(errnoent1)
189 # if SUPPORTED_PERSONALITIES > 2
190 , nerrnos2 = ARRAY_SIZE(errnoent2)
196 nsignals0 = ARRAY_SIZE(signalent0)
197 #if SUPPORTED_PERSONALITIES > 1
198 , nsignals1 = ARRAY_SIZE(signalent1)
199 # if SUPPORTED_PERSONALITIES > 2
200 , nsignals2 = ARRAY_SIZE(signalent2)
206 nioctlents0 = ARRAY_SIZE(ioctlent0)
207 #if SUPPORTED_PERSONALITIES > 1
208 , nioctlents1 = ARRAY_SIZE(ioctlent1)
209 # if SUPPORTED_PERSONALITIES > 2
210 , nioctlents2 = ARRAY_SIZE(ioctlent2)
215 #if SUPPORTED_PERSONALITIES > 1
216 const struct_sysent *sysent = sysent0;
217 const char *const *errnoent = errnoent0;
218 const char *const *signalent = signalent0;
219 const struct_ioctlent *ioctlent = ioctlent0;
221 unsigned nsyscalls = nsyscalls0;
222 unsigned nerrnos = nerrnos0;
223 unsigned nsignals = nsignals0;
224 unsigned nioctlents = nioctlents0;
227 qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
229 static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
231 #if SUPPORTED_PERSONALITIES > 1
234 #if SUPPORTED_PERSONALITIES > 2
238 static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
240 #if SUPPORTED_PERSONALITIES > 1
243 #if SUPPORTED_PERSONALITIES > 2
249 MAX_NSYSCALLS1 = (nsyscalls0
250 #if SUPPORTED_PERSONALITIES > 1
251 > nsyscalls1 ? nsyscalls0 : nsyscalls1
254 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
255 #if SUPPORTED_PERSONALITIES > 2
256 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
259 MAX_NSYSCALLS = MAX_NSYSCALLS2,
260 /* We are ready for arches with up to 255 signals,
261 * even though the largest known signo is on MIPS and it is 128.
262 * The number of existing syscalls on all arches is
263 * larger that 255 anyway, so it is just a pedantic matter.
265 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
268 #if SUPPORTED_PERSONALITIES > 1
269 unsigned current_personality;
271 # ifndef current_wordsize
272 unsigned current_wordsize;
273 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
274 PERSONALITY0_WORDSIZE,
275 PERSONALITY1_WORDSIZE,
276 # if SUPPORTED_PERSONALITIES > 2
277 PERSONALITY2_WORDSIZE,
283 set_personality(int personality)
285 nsyscalls = nsyscall_vec[personality];
286 sysent = sysent_vec[personality];
288 switch (personality) {
290 errnoent = errnoent0;
292 ioctlent = ioctlent0;
293 nioctlents = nioctlents0;
294 signalent = signalent0;
295 nsignals = nsignals0;
299 errnoent = errnoent1;
301 ioctlent = ioctlent1;
302 nioctlents = nioctlents1;
303 signalent = signalent1;
304 nsignals = nsignals1;
307 # if SUPPORTED_PERSONALITIES > 2
309 errnoent = errnoent2;
311 ioctlent = ioctlent2;
312 nioctlents = nioctlents2;
313 signalent = signalent2;
314 nsignals = nsignals2;
319 current_personality = personality;
320 # ifndef current_wordsize
321 current_wordsize = personality_wordsize[personality];
326 update_personality(struct tcb *tcp, unsigned int personality)
328 if (personality == current_personality)
330 set_personality(personality);
332 if (personality == tcp->currpers)
334 tcp->currpers = personality;
336 # if defined(POWERPC64)
338 static const char *const names[] = {"64 bit", "32 bit"};
339 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
340 tcp->pid, names[personality]);
342 # elif defined(X86_64)
344 static const char *const names[] = {"64 bit", "32 bit", "x32"};
345 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
346 tcp->pid, names[personality]);
350 static const char *const names[] = {"x32", "32 bit"};
351 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
352 tcp->pid, names[personality]);
354 # elif defined(AARCH64)
356 static const char *const names[] = {"32-bit", "AArch64"};
357 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
358 tcp->pid, names[personality]);
362 static const char *const names[] = {"64-bit", "32-bit"};
363 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
364 tcp->pid, names[personality]);
370 static int qual_syscall(), qual_signal(), qual_desc();
372 static const struct qual_options {
373 unsigned int bitflag;
374 const char *option_name;
375 int (*qualify)(const char *, int, int);
376 const char *argument_name;
378 { QUAL_TRACE, "trace", qual_syscall, "system call" },
379 { QUAL_TRACE, "t", qual_syscall, "system call" },
380 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
381 { QUAL_ABBREV, "a", qual_syscall, "system call" },
382 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
383 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
384 { QUAL_RAW, "raw", qual_syscall, "system call" },
385 { QUAL_RAW, "x", qual_syscall, "system call" },
386 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
387 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
388 { QUAL_SIGNAL, "s", qual_signal, "signal" },
389 { QUAL_READ, "read", qual_desc, "descriptor" },
390 { QUAL_READ, "reads", qual_desc, "descriptor" },
391 { QUAL_READ, "r", qual_desc, "descriptor" },
392 { QUAL_WRITE, "write", qual_desc, "descriptor" },
393 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
394 { QUAL_WRITE, "w", qual_desc, "descriptor" },
395 { 0, NULL, NULL, NULL },
399 reallocate_qual(const unsigned int n)
403 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
404 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
407 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
413 qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
418 reallocate_qual(n + 1);
420 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
421 if (pers == p || pers < 0) {
423 qual_vec[p][n] &= ~bitflag;
425 qual_vec[p][n] |= bitflag;
431 qual_syscall(const char *s, const unsigned int bitflag, const int not)
437 if (*s >= '0' && *s <= '9') {
438 i = string_to_uint(s);
439 if (i >= MAX_NSYSCALLS)
441 qualify_one(i, bitflag, not, -1);
445 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
446 for (i = 0; i < nsyscall_vec[p]; i++) {
447 if (sysent_vec[p][i].sys_name
448 && strcmp(s, sysent_vec[p][i].sys_name) == 0
450 qualify_one(i, bitflag, not, p);
460 qual_signal(const char *s, const unsigned int bitflag, const int not)
464 if (*s >= '0' && *s <= '9') {
465 int signo = string_to_uint(s);
466 if (signo < 0 || signo > 255)
468 qualify_one(signo, bitflag, not, -1);
471 if (strncasecmp(s, "SIG", 3) == 0)
473 for (i = 0; i <= NSIG; i++) {
474 if (strcasecmp(s, signame(i) + 3) == 0) {
475 qualify_one(i, bitflag, not, -1);
483 qual_desc(const char *s, const unsigned int bitflag, const int not)
485 if (*s >= '0' && *s <= '9') {
486 int desc = string_to_uint(s);
487 if (desc < 0 || desc > 0x7fff) /* paranoia */
489 qualify_one(desc, bitflag, not, -1);
496 lookup_class(const char *s)
498 if (strcmp(s, "file") == 0)
500 if (strcmp(s, "ipc") == 0)
502 if (strcmp(s, "network") == 0)
503 return TRACE_NETWORK;
504 if (strcmp(s, "process") == 0)
505 return TRACE_PROCESS;
506 if (strcmp(s, "signal") == 0)
508 if (strcmp(s, "desc") == 0)
510 if (strcmp(s, "memory") == 0)
516 qualify(const char *s)
518 const struct qual_options *opt;
525 reallocate_qual(MIN_QUALS);
527 opt = &qual_options[0];
528 for (i = 0; (p = qual_options[i].option_name); i++) {
529 unsigned int len = strlen(p);
530 if (strncmp(s, p, len) == 0 && s[len] == '=') {
531 opt = &qual_options[i];
541 if (strcmp(s, "none") == 0) {
545 if (strcmp(s, "all") == 0) {
546 for (i = 0; i < num_quals; i++) {
547 qualify_one(i, opt->bitflag, not, -1);
551 for (i = 0; i < num_quals; i++) {
552 qualify_one(i, opt->bitflag, !not, -1);
557 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
559 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
561 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
562 for (i = 0; i < nsyscall_vec[pers]; i++)
563 if (sysent_vec[pers][i].sys_flags & n)
564 qualify_one(i, opt->bitflag, not, pers);
568 if (opt->qualify(p, opt->bitflag, not)) {
569 error_msg_and_die("invalid %s '%s'",
570 opt->argument_name, p);
577 #ifdef SYS_socket_subcall
579 decode_socket_subcall(struct tcb *tcp)
582 unsigned int i, n, size;
584 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
587 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
588 tcp->qual_flg = qual_flags[tcp->scno];
589 tcp->s_ent = &sysent[tcp->scno];
590 addr = tcp->u_arg[1];
591 size = current_wordsize;
592 n = tcp->s_ent->nargs;
593 for (i = 0; i < n; ++i) {
594 if (size == sizeof(int)) {
596 if (umove(tcp, addr, &arg) < 0)
602 if (umove(tcp, addr, &arg) < 0)
611 #ifdef SYS_ipc_subcall
613 decode_ipc_subcall(struct tcb *tcp)
617 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
620 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
621 tcp->qual_flg = qual_flags[tcp->scno];
622 tcp->s_ent = &sysent[tcp->scno];
623 n = tcp->s_ent->nargs;
624 for (i = 0; i < n; i++)
625 tcp->u_arg[i] = tcp->u_arg[i + 1];
630 printargs(struct tcb *tcp)
634 int n = tcp->s_ent->nargs;
635 for (i = 0; i < n; i++)
636 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
642 printargs_lu(struct tcb *tcp)
646 int n = tcp->s_ent->nargs;
647 for (i = 0; i < n; i++)
648 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
654 printargs_ld(struct tcb *tcp)
658 int n = tcp->s_ent->nargs;
659 for (i = 0; i < n; i++)
660 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
665 #if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
667 getrval2(struct tcb *tcp)
671 # if defined(SPARC) || defined(SPARC64)
672 val = sparc_regs.u_regs[U_REG_O1];
674 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
677 if (upeek(tcp->pid, PT_R9, &val) < 0)
686 static struct user_regs_struct i386_regs;
687 /* Cast suppresses signedness warning (.esp is long, not unsigned long) */
688 uint32_t *const i386_esp_ptr = (uint32_t*)&i386_regs.esp;
689 # define ARCH_REGS_FOR_GETREGSET i386_regs
690 #elif defined(X86_64) || defined(X32)
692 * On i386, pt_regs and user_regs_struct are the same,
693 * but on 64 bit x86, user_regs_struct has six more fields:
694 * fs_base, gs_base, ds, es, fs, gs.
695 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
697 struct i386_user_regs_struct {
717 struct user_regs_struct x86_64_r;
718 struct i386_user_regs_struct i386_r;
720 # define x86_64_regs x86_regs_union.x86_64_r
721 # define i386_regs x86_regs_union.i386_r
722 uint32_t *const i386_esp_ptr = &i386_regs.esp;
723 static struct iovec x86_io = {
724 .iov_base = &x86_regs_union
727 bool ia64_ia32mode = 0; /* not static */
728 static long ia64_r8, ia64_r10;
729 #elif defined(POWERPC)
730 struct pt_regs ppc_regs;
736 struct pt_regs arm_regs; /* not static */
737 # define ARCH_REGS_FOR_GETREGSET arm_regs
738 #elif defined(AARCH64)
740 struct user_pt_regs aarch64_r;
741 struct arm_pt_regs arm_r;
743 # define aarch64_regs arm_regs_union.aarch64_r
744 # define arm_regs arm_regs_union.arm_r
745 static struct iovec aarch64_io = {
746 .iov_base = &arm_regs_union
749 static long alpha_r0;
750 static long alpha_a3;
752 static struct pt_regs avr32_regs;
753 #elif defined(SPARC) || defined(SPARC64)
754 struct pt_regs sparc_regs; /* not static */
755 #elif defined(LINUX_MIPSN32)
756 static long long mips_a3;
757 static long long mips_r2;
761 #elif defined(S390) || defined(S390X)
762 static long s390_gpr2;
764 static long hppa_r28;
769 #elif defined(CRISV10) || defined(CRISV32)
770 static long cris_r10;
772 struct pt_regs tile_regs;
773 #elif defined(MICROBLAZE)
774 static long microblaze_r3;
776 static struct user_regs_struct or1k_regs;
777 # define ARCH_REGS_FOR_GETREGSET or1k_regs
779 static struct user_gp_regs metag_regs;
780 # define ARCH_REGS_FOR_GETREGSET metag_regs
781 #elif defined(XTENSA)
782 static long xtensa_a2;
784 static struct user_regs_struct arc_regs;
785 # define ARCH_REGS_FOR_GETREGSET arc_regs
789 print_pc(struct tcb *tcp)
791 #define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
792 sizeof(long) == 8 ? "[????????????????] " : \
794 if (get_regs_error) {
799 tprintf("[%08lx] ", i386_regs.eip);
800 #elif defined(S390) || defined(S390X)
802 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0) {
807 tprintf("[%08lx] ", psw);
809 tprintf("[%016lx] ", psw);
811 #elif defined(X86_64) || defined(X32)
812 if (x86_io.iov_len == sizeof(i386_regs)) {
813 tprintf("[%08x] ", (unsigned) i386_regs.eip);
816 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
818 /* Note: this truncates 64-bit rip to 32 bits */
819 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
824 if (upeek(tcp->pid, PT_B0, &ip) < 0) {
828 tprintf("[%08lx] ", ip);
829 #elif defined(POWERPC)
830 long pc = ppc_regs.nip;
832 tprintf("[%016lx] ", pc);
834 tprintf("[%08lx] ", pc);
838 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
839 tprints("[????????] ");
842 tprintf("[%08lx] ", pc);
845 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
846 tprints("[????????????????] ");
849 tprintf("[%08lx] ", pc);
851 tprintf("[%08lx] ", sparc_regs.pc);
852 #elif defined(SPARC64)
853 tprintf("[%08lx] ", sparc_regs.tpc);
856 if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
857 tprints("[????????] ");
860 tprintf("[%08lx] ", pc);
863 if (upeek(tcp->pid, REG_EPC, &pc) < 0) {
864 tprints("[????????] ");
867 tprintf("[%08lx] ", pc);
870 if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
871 tprints("[????????] ");
874 tprintf("[%08lx] ", pc);
877 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
878 tprints("[????????????????] ");
881 tprintf("[%08lx] ", pc);
883 tprintf("[%08lx] ", arm_regs.ARM_pc);
884 #elif defined(AARCH64)
885 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
887 tprintf("[%08lx] ", avr32_regs.pc);
890 if (upeek(tcp->pid, PT_PC, &pc) < 0) {
894 tprintf("[%08lx] ", pc);
895 #elif defined(CRISV10)
897 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
901 tprintf("[%08lx] ", pc);
902 #elif defined(CRISV32)
904 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
908 tprintf("[%08lx] ", pc);
911 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
913 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
916 tprintf("[%08lx] ", or1k_regs.pc);
918 tprintf("[%08lx] ", metag_regs.pc);
919 #elif defined(XTENSA)
921 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
925 tprintf("[%08lx] ", pc);
927 tprintf("[%08lx] ", arc_regs.efa);
928 #endif /* architecture */
931 /* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
932 * The shuffling should be reversible: shuffle_scno(shuffle_scno(n)) == n.
934 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
936 shuffle_scno(unsigned long scno)
938 if (scno <= ARM_LAST_ORDINARY_SYSCALL)
941 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
942 if (scno == 0x000ffff0)
943 return ARM_LAST_ORDINARY_SYSCALL+1;
944 if (scno == ARM_LAST_ORDINARY_SYSCALL+1)
947 /* Is it ARM specific syscall?
948 * Swap with [LAST_ORDINARY+2, LAST_ORDINARY+2 + LAST_SPECIAL] range.
950 if (scno >= 0x000f0000
951 && scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL
953 return scno - 0x000f0000 + (ARM_LAST_ORDINARY_SYSCALL+2);
955 if (/* scno >= ARM_LAST_ORDINARY_SYSCALL+2 - always true */ 1
956 && scno <= (ARM_LAST_ORDINARY_SYSCALL+2) + ARM_LAST_SPECIAL_SYSCALL
958 return scno + 0x000f0000 - (ARM_LAST_ORDINARY_SYSCALL+2);
964 # define shuffle_scno(scno) ((long)(scno))
968 undefined_scno_name(struct tcb *tcp)
970 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
972 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
978 * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
979 * we provide a slow fallback for old kernels.
981 static int powerpc_getregs_old(pid_t pid)
987 r = upeek(pid, sizeof(long) * PT_NIP, (long *)&ppc_regs.nip);
991 #ifdef POWERPC64 /* else we never use it */
992 r = upeek(pid, sizeof(long) * PT_MSR, (long *)&ppc_regs.msr);
996 r = upeek(pid, sizeof(long) * PT_CCR, (long *)&ppc_regs.ccr);
999 r = upeek(pid, sizeof(long) * PT_ORIG_R3, (long *)&ppc_regs.orig_gpr3);
1002 for (i = 0; i <= 8; i++) {
1003 r = upeek(pid, sizeof(long) * (PT_R0 + i),
1004 (long *)&ppc_regs.gpr[i]);
1014 long get_regs_error;
1016 #if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1017 static void get_regset(pid_t pid)
1019 /* constant iovec */
1025 static struct iovec io = {
1026 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1027 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1029 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1031 /* variable iovec */
1032 # elif defined(X86_64) || defined(X32)
1033 /* x86_io.iov_base = &x86_regs_union; - already is */
1034 x86_io.iov_len = sizeof(x86_regs_union);
1035 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &x86_io);
1036 # elif defined(AARCH64)
1037 /* aarch64_io.iov_base = &arm_regs_union; - already is */
1038 aarch64_io.iov_len = sizeof(arm_regs_union);
1039 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &aarch64_io);
1041 # warning both PTRACE_GETREGSET and NT_PRSTATUS are available but not yet used
1044 #endif /* PTRACE_GETREGSET && NT_PRSTATUS */
1049 /* PTRACE_GETREGSET only */
1050 # if defined(METAG) || defined(OR1K) || defined(X32) || defined(AARCH64) || defined(ARC)
1053 /* PTRACE_GETREGS only */
1054 # elif defined(AVR32)
1055 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
1056 # elif defined(TILE)
1057 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &tile_regs);
1058 # elif defined(SPARC) || defined(SPARC64)
1059 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
1060 # elif defined(POWERPC)
1061 static bool old_kernel = 0;
1064 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &ppc_regs);
1065 if (get_regs_error && errno == EIO) {
1068 get_regs_error = powerpc_getregs_old(pid);
1071 /* try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS */
1073 # if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1074 static int getregset_support;
1076 if (getregset_support >= 0) {
1078 if (getregset_support > 0)
1080 if (get_regs_error >= 0) {
1081 getregset_support = 1;
1084 if (errno == EPERM || errno == ESRCH)
1086 getregset_support = -1;
1088 # endif /* PTRACE_GETREGSET && NT_PRSTATUS */
1090 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &arm_regs);
1091 # elif defined(I386)
1092 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &i386_regs);
1093 # elif defined(X86_64)
1094 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1095 x86_io.iov_len = sizeof(x86_64_regs);
1096 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
1097 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1098 x86_io.iov_len = sizeof(i386_regs);
1100 * The order is important: i386_regs and x86_64_regs
1101 * are overlaid in memory!
1103 i386_regs.ebx = x86_64_regs.rbx;
1104 i386_regs.ecx = x86_64_regs.rcx;
1105 i386_regs.edx = x86_64_regs.rdx;
1106 i386_regs.esi = x86_64_regs.rsi;
1107 i386_regs.edi = x86_64_regs.rdi;
1108 i386_regs.ebp = x86_64_regs.rbp;
1109 i386_regs.eax = x86_64_regs.rax;
1110 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1111 /* i386_regs.xes = x86_64_regs.es; ditto... */
1112 /* i386_regs.xfs = x86_64_regs.fs; */
1113 /* i386_regs.xgs = x86_64_regs.gs; */
1114 i386_regs.orig_eax = x86_64_regs.orig_rax;
1115 i386_regs.eip = x86_64_regs.rip;
1116 /* i386_regs.xcs = x86_64_regs.cs; */
1117 /* i386_regs.eflags = x86_64_regs.eflags; */
1118 i386_regs.esp = x86_64_regs.rsp;
1119 /* i386_regs.xss = x86_64_regs.ss; */
1122 # error unhandled architecture
1123 # endif /* ARM || I386 || X86_64 */
1126 #endif /* !get_regs */
1129 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1130 * 1: ok, continue in trace_syscall_entering().
1131 * other: error, trace_syscall_entering() should print error indicator
1132 * ("????" etc) and bail out.
1135 get_scno(struct tcb *tcp)
1139 #if defined(S390) || defined(S390X)
1140 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
1143 if (s390_gpr2 != -ENOSYS) {
1145 * Since kernel version 2.5.44 the scno gets passed in gpr2.
1150 * Old style of "passing" the scno via the SVC instruction.
1153 long opcode, offset_reg, tmp;
1155 static const int gpr_offset[16] = {
1156 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1157 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1158 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1159 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1162 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0)
1165 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
1167 perror_msg("peektext(psw-oneword)");
1172 * We have to check if the SVC got executed directly or via an
1173 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1174 * instruction decoding to derive the system call number.
1175 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1176 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1177 * opcode. Since there is no way to find out the opcode size this
1178 * is the best we can do...
1180 if ((opcode & 0xff00) == 0x0a00) {
1182 scno = opcode & 0xff;
1185 /* SVC got executed by EXECUTE instruction */
1188 * Do instruction decoding of EXECUTE. If you really want to
1189 * understand this, read the Principles of Operations.
1191 svc_addr = (void *) (opcode & 0xfff);
1194 offset_reg = (opcode & 0x000f0000) >> 16;
1195 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
1200 offset_reg = (opcode & 0x0000f000) >> 12;
1201 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
1205 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
1214 offset_reg = (opcode & 0x00f00000) >> 20;
1215 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
1218 scno = (scno | tmp) & 0xff;
1221 #elif defined(POWERPC)
1222 scno = ppc_regs.gpr[0];
1224 unsigned int currpers;
1227 * Check for 64/32 bit mode.
1228 * Embedded implementations covered by Book E extension of PPC use
1229 * bit 0 (CM) of 32-bit Machine state register (MSR).
1230 * Other implementations use bit 0 (SF) of 64-bit MSR.
1232 currpers = (ppc_regs.msr & 0x8000000080000000) ? 0 : 1;
1233 update_personality(tcp, currpers);
1235 #elif defined(AVR32)
1236 scno = avr32_regs.r8;
1238 if (upeek(tcp->pid, PT_ORIG_P0, &scno))
1241 scno = i386_regs.orig_eax;
1242 #elif defined(X86_64) || defined(X32)
1243 # ifndef __X32_SYSCALL_BIT
1244 # define __X32_SYSCALL_BIT 0x40000000
1246 unsigned int currpers;
1248 /* GETREGSET of NT_PRSTATUS tells us regset size,
1249 * which unambiguously detects i386.
1251 * Linux kernel distinguishes x86-64 and x32 processes
1252 * solely by looking at __X32_SYSCALL_BIT:
1253 * arch/x86/include/asm/compat.h::is_x32_task():
1254 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1257 if (x86_io.iov_len == sizeof(i386_regs)) {
1258 scno = i386_regs.orig_eax;
1261 scno = x86_64_regs.orig_rax;
1263 if (scno & __X32_SYSCALL_BIT) {
1264 scno -= __X32_SYSCALL_BIT;
1269 /* cs = 0x33 for long mode (native 64 bit and x32)
1270 * cs = 0x23 for compatibility mode (32 bit)
1271 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1273 scno = x86_64_regs.orig_rax;
1274 switch (x86_64_regs.cs) {
1275 case 0x23: currpers = 1; break;
1277 if (x86_64_regs.ds == 0x2b) {
1279 scno &= ~__X32_SYSCALL_BIT;
1284 fprintf(stderr, "Unknown value CS=0x%08X while "
1285 "detecting personality of process "
1286 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
1287 currpers = current_personality;
1291 /* This version analyzes the opcode of a syscall instruction.
1292 * (int 0x80 on i386 vs. syscall on x86-64)
1293 * It works, but is too complicated, and strictly speaking, unreliable.
1295 unsigned long call, rip = x86_64_regs.rip;
1296 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1299 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
1301 fprintf(stderr, "ptrace_peektext failed: %s\n",
1303 switch (call & 0xffff) {
1304 /* x86-64: syscall = 0x0f 0x05 */
1305 case 0x050f: currpers = 0; break;
1306 /* i386: int 0x80 = 0xcd 0x80 */
1307 case 0x80cd: currpers = 1; break;
1309 currpers = current_personality;
1311 "Unknown syscall opcode (0x%04X) while "
1312 "detecting personality of process "
1313 "PID=%d\n", (int)call, tcp->pid);
1319 /* If we are built for a x32 system, then personality 0 is x32
1320 * (not x86_64), and stracing of x86_64 apps is not supported.
1321 * Stracing of i386 apps is still supported.
1323 if (currpers == 0) {
1324 fprintf(stderr, "syscall_%lu(...) in unsupported "
1325 "64-bit mode of process PID=%d\n",
1329 currpers &= ~2; /* map 2,1 to 0,1 */
1331 update_personality(tcp, currpers);
1333 # define IA64_PSR_IS ((long)1 << 34)
1335 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
1336 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
1337 if (ia64_ia32mode) {
1338 if (upeek(tcp->pid, PT_R1, &scno) < 0)
1341 if (upeek(tcp->pid, PT_R15, &scno) < 0)
1344 #elif defined(AARCH64)
1345 switch (aarch64_io.iov_len) {
1346 case sizeof(aarch64_regs):
1347 /* We are in 64-bit mode */
1348 scno = aarch64_regs.regs[8];
1349 update_personality(tcp, 1);
1351 case sizeof(arm_regs):
1352 /* We are in 32-bit mode */
1353 /* Note: we don't support OABI, unlike 32-bit ARM build */
1354 scno = arm_regs.ARM_r7;
1355 scno = shuffle_scno(scno);
1356 update_personality(tcp, 0);
1360 if (arm_regs.ARM_ip != 0) {
1361 /* It is not a syscall entry */
1362 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1363 tcp->flags |= TCB_INSYSCALL;
1366 /* Note: we support only 32-bit CPUs, not 26-bit */
1368 # if !defined(__ARM_EABI__) || ENABLE_ARM_OABI
1369 if (arm_regs.ARM_cpsr & 0x20)
1373 /* Check EABI/OABI by examining SVC insn's low 24 bits */
1375 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1378 /* EABI syscall convention? */
1379 if (scno != 0xef000000) {
1381 if ((scno & 0x0ff00000) != 0x0f900000) {
1382 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1386 /* Fixup the syscall number */
1390 scno = arm_regs.ARM_r7;
1392 # else /* __ARM_EABI__ || !ENABLE_ARM_OABI */
1393 scno = arm_regs.ARM_r7;
1395 scno = shuffle_scno(scno);
1397 if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
1399 #elif defined(LINUX_MIPSN32)
1400 unsigned long long regs[38];
1402 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1404 mips_a3 = regs[REG_A3];
1405 mips_r2 = regs[REG_V0];
1408 if (!SCNO_IN_RANGE(scno)) {
1409 if (mips_a3 == 0 || mips_a3 == -1) {
1411 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1416 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
1418 if (upeek(tcp->pid, REG_V0, &scno) < 0)
1421 if (!SCNO_IN_RANGE(scno)) {
1422 if (mips_a3 == 0 || mips_a3 == -1) {
1424 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1428 #elif defined(ALPHA)
1429 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
1431 if (upeek(tcp->pid, REG_R0, &scno) < 0)
1435 * Do some sanity checks to figure out if it's
1436 * really a syscall entry
1438 if (!SCNO_IN_RANGE(scno)) {
1439 if (alpha_a3 == 0 || alpha_a3 == -1) {
1441 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
1445 #elif defined(SPARC) || defined(SPARC64)
1446 /* Disassemble the syscall trap. */
1447 /* Retrieve the syscall trap instruction. */
1450 # if defined(SPARC64)
1451 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
1454 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
1459 /* Disassemble the trap to see what personality to use. */
1462 /* Linux/SPARC syscall trap. */
1463 update_personality(tcp, 0);
1466 /* Linux/SPARC64 syscall trap. */
1467 update_personality(tcp, 2);
1470 /* SunOS syscall trap. (pers 1) */
1471 fprintf(stderr, "syscall: SunOS no support\n");
1474 /* Solaris 2.x syscall trap. (per 2) */
1475 update_personality(tcp, 1);
1478 /* NetBSD/FreeBSD syscall trap. */
1479 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1482 /* Solaris 2.x gettimeofday */
1483 update_personality(tcp, 1);
1486 # if defined(SPARC64)
1487 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
1489 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
1494 /* Extract the system call number from the registers. */
1495 if (trap == 0x91d02027)
1498 scno = sparc_regs.u_regs[U_REG_G1];
1500 scno = sparc_regs.u_regs[U_REG_O0];
1501 memmove(&sparc_regs.u_regs[U_REG_O0], &sparc_regs.u_regs[U_REG_O1], 7*sizeof(sparc_regs.u_regs[0]));
1504 if (upeek(tcp->pid, PT_GR20, &scno) < 0)
1508 * In the new syscall ABI, the system call number is in R3.
1510 if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
1514 /* Odd as it may seem, a glibc bug has been known to cause
1515 glibc to issue bogus negative syscall numbers. So for
1516 our purposes, make strace print what it *should* have been */
1517 long correct_scno = (scno & 0xff);
1520 "Detected glibc bug: bogus system call"
1521 " number = %ld, correcting to %ld\n",
1524 scno = correct_scno;
1527 if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
1530 #elif defined(CRISV10) || defined(CRISV32)
1531 if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
1534 unsigned int currpers;
1535 scno = tile_regs.regs[10];
1539 # ifndef PT_FLAGS_COMPAT
1540 # define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1542 if (tile_regs.flags & PT_FLAGS_COMPAT)
1547 update_personality(tcp, currpers);
1548 #elif defined(MICROBLAZE)
1549 if (upeek(tcp->pid, 0, &scno) < 0)
1552 scno = or1k_regs.gpr[11];
1553 #elif defined(METAG)
1554 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
1555 #elif defined(XTENSA)
1556 if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
1559 scno = arc_regs.scratch.r8;
1563 if (SCNO_IS_VALID(tcp->scno)) {
1564 tcp->s_ent = &sysent[scno];
1565 tcp->qual_flg = qual_flags[scno];
1567 static const struct_sysent unknown = {
1570 .sys_func = printargs,
1571 .sys_name = "unknown", /* not used */
1573 tcp->s_ent = &unknown;
1574 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1579 /* Called at each syscall entry.
1581 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1582 * 1: ok, continue in trace_syscall_entering().
1583 * other: error, trace_syscall_entering() should print error indicator
1584 * ("????" etc) and bail out.
1587 syscall_fixup_on_sysenter(struct tcb *tcp)
1589 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
1591 if (i386_regs.eax != -ENOSYS) {
1593 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1596 #elif defined(X86_64) || defined(X32)
1599 if (x86_io.iov_len == sizeof(i386_regs)) {
1600 /* Sign extend from 32 bits */
1601 rax = (int32_t)i386_regs.eax;
1603 /* Note: in X32 build, this truncates 64 to 32 bits */
1604 rax = x86_64_regs.rax;
1606 if (rax != -ENOSYS) {
1608 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1613 /* TODO? Eliminate upeek's in arches below like we did in x86 */
1614 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
1616 if (m68k_d0 != -ENOSYS) {
1618 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
1622 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
1624 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
1626 if (ia64_ia32mode && ia64_r8 != -ENOSYS) {
1628 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
1631 #elif defined(CRISV10) || defined(CRISV32)
1632 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
1634 if (cris_r10 != -ENOSYS) {
1636 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
1639 #elif defined(MICROBLAZE)
1640 if (upeek(tcp->pid, 3 * 4, µblaze_r3) < 0)
1642 if (microblaze_r3 != -ENOSYS) {
1644 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
1652 internal_fork(struct tcb *tcp)
1654 #if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1655 # define ARG_FLAGS 1
1657 # define ARG_FLAGS 0
1659 #ifndef CLONE_UNTRACED
1660 # define CLONE_UNTRACED 0x00800000
1662 if ((ptrace_setoptions
1663 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1664 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1670 if (entering(tcp)) {
1672 * We won't see the new child if clone is called with
1673 * CLONE_UNTRACED, so we keep the same logic with that option
1674 * and don't trace it.
1676 if ((tcp->s_ent->sys_func == sys_clone)
1677 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1682 if (tcp->flags & TCB_BPTSET)
1687 #if defined(TCB_WAITEXECVE)
1689 internal_exec(struct tcb *tcp)
1691 /* Maybe we have post-execve SIGTRAP suppressed? */
1692 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1693 return; /* yes, no need to do anything */
1695 if (exiting(tcp) && syserror(tcp))
1696 /* Error in execve, no post-execve SIGTRAP expected */
1697 tcp->flags &= ~TCB_WAITEXECVE;
1699 tcp->flags |= TCB_WAITEXECVE;
1704 syscall_fixup_for_fork_exec(struct tcb *tcp)
1707 * We must always trace a few critical system calls in order to
1708 * correctly support following forks in the presence of tracing
1713 func = tcp->s_ent->sys_func;
1715 if ( sys_fork == func
1716 || sys_clone == func
1722 #if defined(TCB_WAITEXECVE)
1723 if ( sys_execve == func
1724 # if defined(SPARC) || defined(SPARC64)
1725 || sys_execv == func
1734 /* Return -1 on error or 1 on success (never 0!) */
1736 get_syscall_args(struct tcb *tcp)
1740 nargs = tcp->s_ent->nargs;
1742 #if defined(S390) || defined(S390X)
1743 for (i = 0; i < nargs; ++i)
1744 if (upeek(tcp->pid, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1746 #elif defined(ALPHA)
1747 for (i = 0; i < nargs; ++i)
1748 if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1751 if (!ia64_ia32mode) {
1752 unsigned long *out0, cfm, sof, sol;
1754 /* be backwards compatible with kernel < 2.4.4... */
1756 # define PT_RBS_END PT_AR_BSP
1759 if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
1761 if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
1764 sof = (cfm >> 0) & 0x7f;
1765 sol = (cfm >> 7) & 0x7f;
1766 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1768 for (i = 0; i < nargs; ++i) {
1769 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1770 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1774 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1775 PT_R9 /* ECX = out1 */,
1776 PT_R10 /* EDX = out2 */,
1777 PT_R14 /* ESI = out3 */,
1778 PT_R15 /* EDI = out4 */,
1779 PT_R13 /* EBP = out5 */};
1781 for (i = 0; i < nargs; ++i) {
1782 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
1784 /* truncate away IVE sign-extension */
1785 tcp->u_arg[i] &= 0xffffffff;
1788 #elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
1789 /* N32 and N64 both use up to six registers. */
1790 unsigned long long regs[38];
1792 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1795 for (i = 0; i < nargs; ++i) {
1796 tcp->u_arg[i] = regs[REG_A0 + i];
1797 # if defined(LINUX_MIPSN32)
1798 tcp->ext_arg[i] = regs[REG_A0 + i];
1805 if (upeek(tcp->pid, REG_SP, &sp) < 0)
1807 for (i = 0; i < 4; ++i)
1808 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1810 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
1811 (char *)(tcp->u_arg + 4));
1813 for (i = 0; i < nargs; ++i)
1814 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1817 #elif defined(POWERPC)
1820 tcp->u_arg[0] = ppc_regs.orig_gpr3;
1821 tcp->u_arg[1] = ppc_regs.gpr[4];
1822 tcp->u_arg[2] = ppc_regs.gpr[5];
1823 tcp->u_arg[3] = ppc_regs.gpr[6];
1824 tcp->u_arg[4] = ppc_regs.gpr[7];
1825 tcp->u_arg[5] = ppc_regs.gpr[8];
1826 #elif defined(SPARC) || defined(SPARC64)
1827 for (i = 0; i < nargs; ++i)
1828 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
1830 for (i = 0; i < nargs; ++i)
1831 if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1833 #elif defined(ARM) || defined(AARCH64)
1834 # if defined(AARCH64)
1835 if (tcp->currpers == 1)
1836 for (i = 0; i < nargs; ++i)
1837 tcp->u_arg[i] = aarch64_regs.regs[i];
1840 for (i = 0; i < nargs; ++i)
1841 tcp->u_arg[i] = arm_regs.uregs[i];
1842 #elif defined(AVR32)
1845 tcp->u_arg[0] = avr32_regs.r12;
1846 tcp->u_arg[1] = avr32_regs.r11;
1847 tcp->u_arg[2] = avr32_regs.r10;
1848 tcp->u_arg[3] = avr32_regs.r9;
1849 tcp->u_arg[4] = avr32_regs.r5;
1850 tcp->u_arg[5] = avr32_regs.r3;
1852 static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
1854 for (i = 0; i < nargs; ++i)
1855 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
1858 static const int syscall_regs[MAX_ARGS] = {
1859 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1860 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
1863 for (i = 0; i < nargs; ++i)
1864 if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
1868 /* Registers used by SH5 Linux system calls for parameters */
1869 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
1871 for (i = 0; i < nargs; ++i)
1872 if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1877 tcp->u_arg[0] = i386_regs.ebx;
1878 tcp->u_arg[1] = i386_regs.ecx;
1879 tcp->u_arg[2] = i386_regs.edx;
1880 tcp->u_arg[3] = i386_regs.esi;
1881 tcp->u_arg[4] = i386_regs.edi;
1882 tcp->u_arg[5] = i386_regs.ebp;
1883 #elif defined(X86_64) || defined(X32)
1886 if (x86_io.iov_len != sizeof(i386_regs)) {
1887 /* x86-64 or x32 ABI */
1888 tcp->u_arg[0] = x86_64_regs.rdi;
1889 tcp->u_arg[1] = x86_64_regs.rsi;
1890 tcp->u_arg[2] = x86_64_regs.rdx;
1891 tcp->u_arg[3] = x86_64_regs.r10;
1892 tcp->u_arg[4] = x86_64_regs.r8;
1893 tcp->u_arg[5] = x86_64_regs.r9;
1895 tcp->ext_arg[0] = x86_64_regs.rdi;
1896 tcp->ext_arg[1] = x86_64_regs.rsi;
1897 tcp->ext_arg[2] = x86_64_regs.rdx;
1898 tcp->ext_arg[3] = x86_64_regs.r10;
1899 tcp->ext_arg[4] = x86_64_regs.r8;
1900 tcp->ext_arg[5] = x86_64_regs.r9;
1904 /* Zero-extend from 32 bits */
1905 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1906 * if you need to use *sign-extended* parameter.
1908 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1909 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1910 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1911 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1912 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1913 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
1915 #elif defined(MICROBLAZE)
1916 for (i = 0; i < nargs; ++i)
1917 if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1919 #elif defined(CRISV10) || defined(CRISV32)
1920 static const int crisregs[MAX_ARGS] = {
1921 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
1922 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
1925 for (i = 0; i < nargs; ++i)
1926 if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
1929 for (i = 0; i < nargs; ++i)
1930 tcp->u_arg[i] = tile_regs.regs[i];
1932 for (i = 0; i < nargs; ++i)
1933 if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1937 for (i = 0; i < 6; ++i)
1938 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
1939 #elif defined(METAG)
1940 for (i = 0; i < nargs; i++)
1941 /* arguments go backwards from D1Ar1 (D1.3) */
1942 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
1943 #elif defined(XTENSA)
1944 /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
1945 static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
1946 for (i = 0; i < nargs; ++i)
1947 if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
1950 long *arc_args = &arc_regs.scratch.r0;
1951 for (i = 0; i < nargs; ++i)
1952 tcp->u_arg[i] = *arc_args--;
1954 #else /* Other architecture (32bits specific) */
1955 for (i = 0; i < nargs; ++i)
1956 if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
1963 trace_syscall_entering(struct tcb *tcp)
1967 #if defined TCB_WAITEXECVE
1968 if (tcp->flags & TCB_WAITEXECVE) {
1969 /* This is the post-execve SIGTRAP. */
1970 tcp->flags &= ~TCB_WAITEXECVE;
1975 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
1979 res = syscall_fixup_on_sysenter(tcp);
1983 res = get_syscall_args(tcp);
1989 tprints("????" /* anti-trigraph gap */ "(");
1990 else if (tcp->qual_flg & UNDEFINED_SCNO)
1991 tprintf("%s(", undefined_scno_name(tcp));
1993 tprintf("%s(", tcp->s_ent->sys_name);
1995 * " <unavailable>" will be added later by the code which
1996 * detects ptrace errors.
2001 if ( sys_execve == tcp->s_ent->sys_func
2002 # if defined(SPARC) || defined(SPARC64)
2003 || sys_execv == tcp->s_ent->sys_func
2006 hide_log_until_execve = 0;
2009 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
2011 # ifdef SYS_socket_subcall
2012 if (tcp->s_ent->sys_func == sys_socketcall) {
2013 decode_socket_subcall(tcp);
2017 # ifdef SYS_ipc_subcall
2018 if (tcp->s_ent->sys_func == sys_ipc) {
2019 decode_ipc_subcall(tcp);
2027 if (need_fork_exec_workarounds)
2028 syscall_fixup_for_fork_exec(tcp);
2030 if (!(tcp->qual_flg & QUAL_TRACE)
2031 || (tracing_paths && !pathtrace_match(tcp))
2033 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
2037 tcp->flags &= ~TCB_FILTERED;
2039 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
2044 #ifdef USE_LIBUNWIND
2045 if (stack_trace_enabled) {
2046 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
2047 unwind_capture_stacktrace(tcp);
2052 if (tcp->qual_flg & UNDEFINED_SCNO)
2053 tprintf("%s(", undefined_scno_name(tcp));
2055 tprintf("%s(", tcp->s_ent->sys_name);
2056 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
2057 res = printargs(tcp);
2059 res = tcp->s_ent->sys_func(tcp);
2063 tcp->flags |= TCB_INSYSCALL;
2064 /* Measure the entrance time as late as possible to avoid errors. */
2066 gettimeofday(&tcp->etime, NULL);
2071 * 1: ok, continue in trace_syscall_exiting().
2072 * -1: error, trace_syscall_exiting() should print error indicator
2073 * ("????" etc) and bail out.
2076 get_syscall_result(struct tcb *tcp)
2078 #if defined(S390) || defined(S390X)
2079 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
2081 #elif defined(POWERPC)
2082 /* already done by get_regs */
2083 #elif defined(AVR32)
2084 /* already done by get_regs */
2086 if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
2089 /* already done by get_regs */
2090 #elif defined(X86_64) || defined(X32)
2091 /* already done by get_regs */
2093 # define IA64_PSR_IS ((long)1 << 34)
2095 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
2096 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
2097 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
2099 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
2102 /* already done by get_regs */
2103 #elif defined(AARCH64)
2104 /* register reading already done by get_regs */
2106 /* Used to do this, but we did it on syscall entry already: */
2107 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2108 * else it's personality 0.
2110 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
2112 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
2114 #elif defined(LINUX_MIPSN32)
2115 unsigned long long regs[38];
2117 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
2119 mips_a3 = regs[REG_A3];
2120 mips_r2 = regs[REG_V0];
2122 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
2124 if (upeek(tcp->pid, REG_V0, &mips_r2) < 0)
2126 #elif defined(ALPHA)
2127 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
2129 if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
2131 #elif defined(SPARC) || defined(SPARC64)
2132 /* already done by get_regs */
2134 if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
2137 /* new syscall ABI returns result in R0 */
2138 if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
2141 /* ABI defines result returned in r9 */
2142 if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
2144 #elif defined(CRISV10) || defined(CRISV32)
2145 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
2148 /* already done by get_regs */
2149 #elif defined(MICROBLAZE)
2150 if (upeek(tcp->pid, 3 * 4, µblaze_r3) < 0)
2153 /* already done by get_regs */
2154 #elif defined(METAG)
2155 /* already done by get_regs */
2156 #elif defined(XTENSA)
2157 if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
2160 /* already done by get_regs */
2165 /* Called at each syscall exit */
2167 syscall_fixup_on_sysexit(struct tcb *tcp)
2169 #if defined(S390) || defined(S390X)
2170 if ((tcp->flags & TCB_WAITEXECVE)
2171 && (s390_gpr2 == -ENOSYS || s390_gpr2 == tcp->scno)) {
2173 * Return from execve.
2174 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2175 * flag set for the post-execve SIGTRAP to see and reset.
2183 * Check the syscall return value register value for whether it is
2184 * a negated errno code indicating an error, or a success return value.
2187 is_negated_errno(unsigned long int val)
2189 unsigned long int max = -(long int) nerrnos;
2190 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
2191 if (current_wordsize < sizeof(val)) {
2192 val = (unsigned int) val;
2193 max = (unsigned int) max;
2201 is_negated_errno_x32(unsigned long long val)
2203 unsigned long long max = -(long long) nerrnos;
2205 * current_wordsize is 4 even in personality 0 (native X32)
2206 * but truncation _must not_ be done in it.
2207 * can't check current_wordsize here!
2209 if (current_personality != 0) {
2210 val = (uint32_t) val;
2211 max = (uint32_t) max;
2218 * 1: ok, continue in trace_syscall_exiting().
2219 * -1: error, trace_syscall_exiting() should print error indicator
2220 * ("????" etc) and bail out.
2223 get_error(struct tcb *tcp)
2226 int check_errno = 1;
2227 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
2230 #if defined(S390) || defined(S390X)
2231 if (check_errno && is_negated_errno(s390_gpr2)) {
2233 u_error = -s390_gpr2;
2236 tcp->u_rval = s390_gpr2;
2239 if (check_errno && is_negated_errno(i386_regs.eax)) {
2241 u_error = -i386_regs.eax;
2244 tcp->u_rval = i386_regs.eax;
2246 #elif defined(X86_64)
2248 if (x86_io.iov_len == sizeof(i386_regs)) {
2249 /* Sign extend from 32 bits */
2250 rax = (int32_t)i386_regs.eax;
2252 rax = x86_64_regs.rax;
2254 if (check_errno && is_negated_errno(rax)) {
2262 /* In X32, return value is 64-bit (llseek uses one).
2263 * Using merely "long rax" would not work.
2266 if (x86_io.iov_len == sizeof(i386_regs)) {
2267 /* Sign extend from 32 bits */
2268 rax = (int32_t)i386_regs.eax;
2270 rax = x86_64_regs.rax;
2272 /* Careful: is_negated_errno() works only on longs */
2273 if (check_errno && is_negated_errno_x32(rax)) {
2278 tcp->u_rval = rax; /* truncating */
2282 if (ia64_ia32mode) {
2286 if (check_errno && is_negated_errno(err)) {
2294 if (check_errno && ia64_r10) {
2298 tcp->u_rval = ia64_r8;
2302 if (check_errno && mips_a3) {
2306 tcp->u_rval = mips_r2;
2307 # if defined(LINUX_MIPSN32)
2308 tcp->u_lrval = mips_r2;
2311 #elif defined(POWERPC)
2312 if (check_errno && (ppc_regs.ccr & 0x10000000)) {
2314 u_error = ppc_regs.gpr[3];
2317 tcp->u_rval = ppc_regs.gpr[3];
2320 if (check_errno && is_negated_errno(m68k_d0)) {
2325 tcp->u_rval = m68k_d0;
2327 #elif defined(ARM) || defined(AARCH64)
2328 # if defined(AARCH64)
2329 if (tcp->currpers == 1) {
2330 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2332 u_error = -aarch64_regs.regs[0];
2335 tcp->u_rval = aarch64_regs.regs[0];
2341 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
2343 u_error = -arm_regs.ARM_r0;
2346 tcp->u_rval = arm_regs.ARM_r0;
2349 #elif defined(AVR32)
2350 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
2352 u_error = -avr32_regs.r12;
2355 tcp->u_rval = avr32_regs.r12;
2358 if (check_errno && is_negated_errno(bfin_r0)) {
2362 tcp->u_rval = bfin_r0;
2364 #elif defined(ALPHA)
2365 if (check_errno && alpha_a3) {
2370 tcp->u_rval = alpha_r0;
2372 #elif defined(SPARC)
2373 if (check_errno && sparc_regs.psr & PSR_C) {
2375 u_error = sparc_regs.u_regs[U_REG_O0];
2378 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
2380 #elif defined(SPARC64)
2381 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
2383 u_error = sparc_regs.u_regs[U_REG_O0];
2386 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
2389 if (check_errno && is_negated_errno(hppa_r28)) {
2391 u_error = -hppa_r28;
2394 tcp->u_rval = hppa_r28;
2397 if (check_errno && is_negated_errno(sh_r0)) {
2402 tcp->u_rval = sh_r0;
2405 if (check_errno && is_negated_errno(sh64_r9)) {
2410 tcp->u_rval = sh64_r9;
2412 #elif defined(METAG)
2413 /* result pointer in D0Re0 (D0.0) */
2414 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2416 u_error = -metag_regs.dx[0][0];
2419 tcp->u_rval = metag_regs.dx[0][0];
2421 #elif defined(CRISV10) || defined(CRISV32)
2422 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
2424 u_error = -cris_r10;
2427 tcp->u_rval = cris_r10;
2431 * The standard tile calling convention returns the value (or negative
2432 * errno) in r0, and zero (or positive errno) in r1.
2433 * Until at least kernel 3.8, however, the r1 value is not reflected
2434 * in ptregs at this point, so we use r0 here.
2436 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
2438 u_error = -tile_regs.regs[0];
2440 tcp->u_rval = tile_regs.regs[0];
2442 #elif defined(MICROBLAZE)
2443 if (check_errno && is_negated_errno(microblaze_r3)) {
2445 u_error = -microblaze_r3;
2448 tcp->u_rval = microblaze_r3;
2451 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2453 u_error = -or1k_regs.gpr[11];
2456 tcp->u_rval = or1k_regs.gpr[11];
2458 #elif defined(XTENSA)
2459 if (check_errno && is_negated_errno(xtensa_a2)) {
2461 u_error = -xtensa_a2;
2464 tcp->u_rval = xtensa_a2;
2467 if (check_errno && is_negated_errno(arc_regs.scratch.r0)) {
2469 u_error = -arc_regs.scratch.r0;
2472 tcp->u_rval = arc_regs.scratch.r0;
2475 tcp->u_error = u_error;
2479 dumpio(struct tcb *tcp)
2485 if ((unsigned long) tcp->u_arg[0] >= num_quals)
2487 func = tcp->s_ent->sys_func;
2488 if (func == printargs)
2490 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
2491 if (func == sys_read ||
2492 func == sys_pread ||
2494 func == sys_recvfrom)
2495 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
2496 else if (func == sys_readv)
2497 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2500 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
2501 if (func == sys_write ||
2502 func == sys_pwrite ||
2505 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
2506 else if (func == sys_writev)
2507 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2513 trace_syscall_exiting(struct tcb *tcp)
2520 /* Measure the exit time as early as possible to avoid errors. */
2522 gettimeofday(&tv, NULL);
2524 #ifdef USE_LIBUNWIND
2525 if (stack_trace_enabled) {
2526 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
2527 unwind_cache_invalidate(tcp);
2531 #if SUPPORTED_PERSONALITIES > 1
2532 update_personality(tcp, tcp->currpers);
2534 res = (get_regs_error ? -1 : get_syscall_result(tcp));
2536 syscall_fixup_on_sysexit(tcp); /* never fails */
2537 get_error(tcp); /* never fails */
2538 if (need_fork_exec_workarounds)
2539 syscall_fixup_for_fork_exec(tcp);
2540 if (filtered(tcp) || hide_log_until_execve)
2545 count_syscall(tcp, &tv);
2546 if (cflag == CFLAG_ONLY_STATS) {
2551 /* If not in -ff mode, and printing_tcp != tcp,
2552 * then the log currently does not end with output
2553 * of _our syscall entry_, but with something else.
2554 * We need to say which syscall's return is this.
2556 * Forced reprinting via TCB_REPRINT is used only by
2557 * "strace -ff -oLOG test/threaded_execve" corner case.
2558 * It's the only case when -ff mode needs reprinting.
2560 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2561 tcp->flags &= ~TCB_REPRINT;
2563 if (tcp->qual_flg & UNDEFINED_SCNO)
2564 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
2566 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
2571 /* There was error in one of prior ptrace ops */
2574 tprints("= ? <unavailable>\n");
2576 tcp->flags &= ~TCB_INSYSCALL;
2581 if (tcp->qual_flg & QUAL_RAW) {
2582 /* sys_res = printargs(tcp); - but it's nop on sysexit */
2584 /* FIXME: not_failing_only (IOW, option -z) is broken:
2585 * failure of syscall is known only after syscall return.
2586 * Thus we end up with something like this on, say, ENOENT:
2587 * open("doesnt_exist", O_RDONLY <unfinished ...>
2588 * {next syscall decode}
2589 * whereas the intended result is that open(...) line
2590 * is not shown at all.
2592 if (not_failing_only && tcp->u_error)
2593 goto ret; /* ignore failed syscalls */
2594 sys_res = tcp->s_ent->sys_func(tcp);
2599 u_error = tcp->u_error;
2600 if (tcp->qual_flg & QUAL_RAW) {
2602 tprintf("= -1 (errno %ld)", u_error);
2604 tprintf("= %#lx", tcp->u_rval);
2606 else if (!(sys_res & RVAL_NONE) && u_error) {
2608 /* Blocked signals do not interrupt any syscalls.
2609 * In this case syscalls don't return ERESTARTfoo codes.
2611 * Deadly signals set to SIG_DFL interrupt syscalls
2612 * and kill the process regardless of which of the codes below
2613 * is returned by the interrupted syscall.
2614 * In some cases, kernel forces a kernel-generated deadly
2615 * signal to be unblocked and set to SIG_DFL (and thus cause
2616 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2617 * or SIGILL. (The alternative is to leave process spinning
2618 * forever on the faulty instruction - not useful).
2620 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2621 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2622 * but kernel will always restart them.
2625 /* Most common type of signal-interrupted syscall exit code.
2626 * The system call will be restarted with the same arguments
2627 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2629 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
2631 case ERESTARTNOINTR:
2632 /* Rare. For example, fork() returns this if interrupted.
2633 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2635 tprints("= ? ERESTARTNOINTR (To be restarted)");
2637 case ERESTARTNOHAND:
2638 /* pause(), rt_sigsuspend() etc use this code.
2639 * SA_RESTART is ignored (assumed not set):
2640 * syscall won't restart (will return EINTR instead)
2641 * even after signal with SA_RESTART set. However,
2642 * after SIG_IGN or SIG_DFL signal it will restart
2643 * (thus the name "restart only if has no handler").
2645 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
2647 case ERESTART_RESTARTBLOCK:
2648 /* Syscalls like nanosleep(), poll() which can't be
2649 * restarted with their original arguments use this
2650 * code. Kernel will execute restart_syscall() instead,
2651 * which changes arguments before restarting syscall.
2652 * SA_RESTART is ignored (assumed not set) similarly
2653 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2654 * since restart data is saved in "restart block"
2655 * in task struct, and if signal handler uses a syscall
2656 * which in turn saves another such restart block,
2657 * old data is lost and restart becomes impossible)
2659 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
2663 tprintf("= -1 E??? (errno %ld)", u_error);
2664 else if ((unsigned long) u_error < nerrnos)
2665 tprintf("= -1 %s (%s)", errnoent[u_error],
2668 tprintf("= -1 ERRNO_%ld (%s)", u_error,
2672 if ((sys_res & RVAL_STR) && tcp->auxstr)
2673 tprintf(" (%s)", tcp->auxstr);
2676 if (sys_res & RVAL_NONE)
2679 switch (sys_res & RVAL_MASK) {
2681 tprintf("= %#lx", tcp->u_rval);
2684 tprintf("= %#lo", tcp->u_rval);
2687 tprintf("= %lu", tcp->u_rval);
2690 tprintf("= %ld", tcp->u_rval);
2695 printfd(tcp, tcp->u_rval);
2698 tprintf("= %ld", tcp->u_rval);
2700 #if defined(LINUX_MIPSN32) || defined(X32)
2703 tprintf("= %#llx", tcp->u_lrval);
2706 tprintf("= %#llo", tcp->u_lrval);
2709 case RVAL_LUDECIMAL:
2710 tprintf("= %llu", tcp->u_lrval);
2714 tprintf("= %lld", tcp->u_lrval);
2720 "invalid rval format\n");
2724 if ((sys_res & RVAL_STR) && tcp->auxstr)
2725 tprintf(" (%s)", tcp->auxstr);
2728 tv_sub(&tv, &tv, &tcp->etime);
2729 tprintf(" <%ld.%06ld>",
2730 (long) tv.tv_sec, (long) tv.tv_usec);
2736 #ifdef USE_LIBUNWIND
2737 if (stack_trace_enabled)
2738 unwind_print_stacktrace(tcp);
2742 tcp->flags &= ~TCB_INSYSCALL;
2747 trace_syscall(struct tcb *tcp)
2749 return exiting(tcp) ?
2750 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);