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.
35 #include <sys/param.h>
37 /* for struct iovec */
43 # undef PTRACE_GETREGS
44 # define PTRACE_GETREGS PTRACE_GETREGS64
45 # undef PTRACE_SETREGS
46 # define PTRACE_SETREGS PTRACE_SETREGS64
52 # include <asm/psrcompat.h>
58 # define NT_PRSTATUS 1
62 # warning: NSIG is not defined, using 32
68 /* Define these shorthand notations to simplify the syscallent files. */
72 #define TN TRACE_NETWORK
73 #define TP TRACE_PROCESS
74 #define TS TRACE_SIGNAL
75 #define TM TRACE_MEMORY
76 #define NF SYSCALL_NEVER_FAILS
78 #define SI STACKTRACE_INVALIDATE_CACHE
79 #define SE STACKTRACE_CAPTURE_ON_ENTER
81 const struct_sysent sysent0[] = {
82 #include "syscallent.h"
85 #if SUPPORTED_PERSONALITIES > 1
86 static const struct_sysent sysent1[] = {
87 # include "syscallent1.h"
91 #if SUPPORTED_PERSONALITIES > 2
92 static const struct_sysent sysent2[] = {
93 # include "syscallent2.h"
97 /* Now undef them since short defines cause wicked namespace pollution. */
111 * `ioctlent[012].h' files are automatically generated by the auxiliary
112 * program `ioctlsort', such that the list is sorted by the `code' field.
113 * This has the side-effect of resolving the _IO.. macros into
114 * plain integers, eliminating the need to include here everything
118 const char *const errnoent0[] = {
119 #include "errnoent.h"
121 const char *const signalent0[] = {
122 #include "signalent.h"
124 const struct_ioctlent ioctlent0[] = {
125 #include "ioctlent0.h"
128 #if SUPPORTED_PERSONALITIES > 1
129 static const char *const errnoent1[] = {
130 # include "errnoent1.h"
132 static const char *const signalent1[] = {
133 # include "signalent1.h"
135 static const struct_ioctlent ioctlent1[] = {
136 # include "ioctlent1.h"
140 #if SUPPORTED_PERSONALITIES > 2
141 static const char *const errnoent2[] = {
142 # include "errnoent2.h"
144 static const char *const signalent2[] = {
145 # include "signalent2.h"
147 static const struct_ioctlent ioctlent2[] = {
148 # include "ioctlent2.h"
153 nsyscalls0 = ARRAY_SIZE(sysent0)
154 #if SUPPORTED_PERSONALITIES > 1
155 , nsyscalls1 = ARRAY_SIZE(sysent1)
156 # if SUPPORTED_PERSONALITIES > 2
157 , nsyscalls2 = ARRAY_SIZE(sysent2)
163 nerrnos0 = ARRAY_SIZE(errnoent0)
164 #if SUPPORTED_PERSONALITIES > 1
165 , nerrnos1 = ARRAY_SIZE(errnoent1)
166 # if SUPPORTED_PERSONALITIES > 2
167 , nerrnos2 = ARRAY_SIZE(errnoent2)
173 nsignals0 = ARRAY_SIZE(signalent0)
174 #if SUPPORTED_PERSONALITIES > 1
175 , nsignals1 = ARRAY_SIZE(signalent1)
176 # if SUPPORTED_PERSONALITIES > 2
177 , nsignals2 = ARRAY_SIZE(signalent2)
183 nioctlents0 = ARRAY_SIZE(ioctlent0)
184 #if SUPPORTED_PERSONALITIES > 1
185 , nioctlents1 = ARRAY_SIZE(ioctlent1)
186 # if SUPPORTED_PERSONALITIES > 2
187 , nioctlents2 = ARRAY_SIZE(ioctlent2)
192 #if SUPPORTED_PERSONALITIES > 1
193 const struct_sysent *sysent = sysent0;
194 const char *const *errnoent = errnoent0;
195 const char *const *signalent = signalent0;
196 const struct_ioctlent *ioctlent = ioctlent0;
198 unsigned nsyscalls = nsyscalls0;
199 unsigned nerrnos = nerrnos0;
200 unsigned nsignals = nsignals0;
201 unsigned nioctlents = nioctlents0;
204 qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
206 static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
208 #if SUPPORTED_PERSONALITIES > 1
211 #if SUPPORTED_PERSONALITIES > 2
215 static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
217 #if SUPPORTED_PERSONALITIES > 1
220 #if SUPPORTED_PERSONALITIES > 2
226 MAX_NSYSCALLS1 = (nsyscalls0
227 #if SUPPORTED_PERSONALITIES > 1
228 > nsyscalls1 ? nsyscalls0 : nsyscalls1
231 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
232 #if SUPPORTED_PERSONALITIES > 2
233 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
236 MAX_NSYSCALLS = MAX_NSYSCALLS2,
237 /* We are ready for arches with up to 255 signals,
238 * even though the largest known signo is on MIPS and it is 128.
239 * The number of existing syscalls on all arches is
240 * larger that 255 anyway, so it is just a pedantic matter.
242 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
245 #if SUPPORTED_PERSONALITIES > 1
246 unsigned current_personality;
248 # ifndef current_wordsize
249 unsigned current_wordsize;
250 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
251 PERSONALITY0_WORDSIZE,
252 PERSONALITY1_WORDSIZE,
253 # if SUPPORTED_PERSONALITIES > 2
254 PERSONALITY2_WORDSIZE,
260 set_personality(int personality)
262 nsyscalls = nsyscall_vec[personality];
263 sysent = sysent_vec[personality];
265 switch (personality) {
267 errnoent = errnoent0;
269 ioctlent = ioctlent0;
270 nioctlents = nioctlents0;
271 signalent = signalent0;
272 nsignals = nsignals0;
276 errnoent = errnoent1;
278 ioctlent = ioctlent1;
279 nioctlents = nioctlents1;
280 signalent = signalent1;
281 nsignals = nsignals1;
284 # if SUPPORTED_PERSONALITIES > 2
286 errnoent = errnoent2;
288 ioctlent = ioctlent2;
289 nioctlents = nioctlents2;
290 signalent = signalent2;
291 nsignals = nsignals2;
296 current_personality = personality;
297 # ifndef current_wordsize
298 current_wordsize = personality_wordsize[personality];
303 update_personality(struct tcb *tcp, unsigned int personality)
305 if (personality == current_personality)
307 set_personality(personality);
309 if (personality == tcp->currpers)
311 tcp->currpers = personality;
313 # if defined(POWERPC64)
315 static const char *const names[] = {"64 bit", "32 bit"};
316 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
317 tcp->pid, names[personality]);
319 # elif defined(X86_64)
321 static const char *const names[] = {"64 bit", "32 bit", "x32"};
322 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
323 tcp->pid, names[personality]);
327 static const char *const names[] = {"x32", "32 bit"};
328 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
329 tcp->pid, names[personality]);
331 # elif defined(AARCH64)
333 static const char *const names[] = {"32-bit", "AArch64"};
334 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
335 tcp->pid, names[personality]);
339 static const char *const names[] = {"64-bit", "32-bit"};
340 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
341 tcp->pid, names[personality]);
347 static int qual_syscall(), qual_signal(), qual_desc();
349 static const struct qual_options {
350 unsigned int bitflag;
351 const char *option_name;
352 int (*qualify)(const char *, int, int);
353 const char *argument_name;
355 { QUAL_TRACE, "trace", qual_syscall, "system call" },
356 { QUAL_TRACE, "t", qual_syscall, "system call" },
357 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
358 { QUAL_ABBREV, "a", qual_syscall, "system call" },
359 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
360 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
361 { QUAL_RAW, "raw", qual_syscall, "system call" },
362 { QUAL_RAW, "x", qual_syscall, "system call" },
363 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
364 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
365 { QUAL_SIGNAL, "s", qual_signal, "signal" },
366 { QUAL_READ, "read", qual_desc, "descriptor" },
367 { QUAL_READ, "reads", qual_desc, "descriptor" },
368 { QUAL_READ, "r", qual_desc, "descriptor" },
369 { QUAL_WRITE, "write", qual_desc, "descriptor" },
370 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
371 { QUAL_WRITE, "w", qual_desc, "descriptor" },
372 { 0, NULL, NULL, NULL },
376 reallocate_qual(const unsigned int n)
380 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
381 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
384 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
390 qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
395 reallocate_qual(n + 1);
397 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
398 if (pers == p || pers < 0) {
400 qual_vec[p][n] &= ~bitflag;
402 qual_vec[p][n] |= bitflag;
408 qual_syscall(const char *s, const unsigned int bitflag, const int not)
414 if (*s >= '0' && *s <= '9') {
415 i = string_to_uint(s);
416 if (i >= MAX_NSYSCALLS)
418 qualify_one(i, bitflag, not, -1);
422 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
423 for (i = 0; i < nsyscall_vec[p]; i++) {
424 if (sysent_vec[p][i].sys_name
425 && strcmp(s, sysent_vec[p][i].sys_name) == 0
427 qualify_one(i, bitflag, not, p);
437 qual_signal(const char *s, const unsigned int bitflag, const int not)
441 if (*s >= '0' && *s <= '9') {
442 int signo = string_to_uint(s);
443 if (signo < 0 || signo > 255)
445 qualify_one(signo, bitflag, not, -1);
448 if (strncasecmp(s, "SIG", 3) == 0)
450 for (i = 0; i <= NSIG; i++) {
451 if (strcasecmp(s, signame(i) + 3) == 0) {
452 qualify_one(i, bitflag, not, -1);
460 qual_desc(const char *s, const unsigned int bitflag, const int not)
462 if (*s >= '0' && *s <= '9') {
463 int desc = string_to_uint(s);
464 if (desc < 0 || desc > 0x7fff) /* paranoia */
466 qualify_one(desc, bitflag, not, -1);
473 lookup_class(const char *s)
475 if (strcmp(s, "file") == 0)
477 if (strcmp(s, "ipc") == 0)
479 if (strcmp(s, "network") == 0)
480 return TRACE_NETWORK;
481 if (strcmp(s, "process") == 0)
482 return TRACE_PROCESS;
483 if (strcmp(s, "signal") == 0)
485 if (strcmp(s, "desc") == 0)
487 if (strcmp(s, "memory") == 0)
493 qualify(const char *s)
495 const struct qual_options *opt;
502 reallocate_qual(MIN_QUALS);
504 opt = &qual_options[0];
505 for (i = 0; (p = qual_options[i].option_name); i++) {
506 unsigned int len = strlen(p);
507 if (strncmp(s, p, len) == 0 && s[len] == '=') {
508 opt = &qual_options[i];
518 if (strcmp(s, "none") == 0) {
522 if (strcmp(s, "all") == 0) {
523 for (i = 0; i < num_quals; i++) {
524 qualify_one(i, opt->bitflag, not, -1);
528 for (i = 0; i < num_quals; i++) {
529 qualify_one(i, opt->bitflag, !not, -1);
534 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
536 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
538 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
539 for (i = 0; i < nsyscall_vec[pers]; i++)
540 if (sysent_vec[pers][i].sys_flags & n)
541 qualify_one(i, opt->bitflag, not, pers);
545 if (opt->qualify(p, opt->bitflag, not)) {
546 error_msg_and_die("invalid %s '%s'",
547 opt->argument_name, p);
554 #ifdef SYS_socket_subcall
556 decode_socket_subcall(struct tcb *tcp)
559 unsigned int i, n, size;
561 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
564 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
565 tcp->qual_flg = qual_flags[tcp->scno];
566 tcp->s_ent = &sysent[tcp->scno];
567 addr = tcp->u_arg[1];
568 size = current_wordsize;
569 n = tcp->s_ent->nargs;
570 for (i = 0; i < n; ++i) {
571 if (size == sizeof(int)) {
573 if (umove(tcp, addr, &arg) < 0)
579 if (umove(tcp, addr, &arg) < 0)
588 #ifdef SYS_ipc_subcall
590 decode_ipc_subcall(struct tcb *tcp)
594 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
597 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
598 tcp->qual_flg = qual_flags[tcp->scno];
599 tcp->s_ent = &sysent[tcp->scno];
600 n = tcp->s_ent->nargs;
601 for (i = 0; i < n; i++)
602 tcp->u_arg[i] = tcp->u_arg[i + 1];
607 printargs(struct tcb *tcp)
611 int n = tcp->s_ent->nargs;
612 for (i = 0; i < n; i++)
613 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
619 printargs_lu(struct tcb *tcp)
623 int n = tcp->s_ent->nargs;
624 for (i = 0; i < n; i++)
625 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
631 printargs_ld(struct tcb *tcp)
635 int n = tcp->s_ent->nargs;
636 for (i = 0; i < n; i++)
637 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
642 #if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
644 getrval2(struct tcb *tcp)
648 # if defined(SPARC) || defined(SPARC64)
649 val = sparc_regs.u_regs[U_REG_O1];
651 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
654 if (upeek(tcp->pid, PT_R9, &val) < 0)
663 static struct user_regs_struct i386_regs;
664 /* Cast suppresses signedness warning (.esp is long, not unsigned long) */
665 uint32_t *const i386_esp_ptr = (uint32_t*)&i386_regs.esp;
666 # define ARCH_REGS_FOR_GETREGSET i386_regs
667 #elif defined(X86_64) || defined(X32)
669 * On i386, pt_regs and user_regs_struct are the same,
670 * but on 64 bit x86, user_regs_struct has six more fields:
671 * fs_base, gs_base, ds, es, fs, gs.
672 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
674 struct i386_user_regs_struct {
694 struct user_regs_struct x86_64_r;
695 struct i386_user_regs_struct i386_r;
697 # define x86_64_regs x86_regs_union.x86_64_r
698 # define i386_regs x86_regs_union.i386_r
699 uint32_t *const i386_esp_ptr = &i386_regs.esp;
700 static struct iovec x86_io = {
701 .iov_base = &x86_regs_union
704 bool ia64_ia32mode = 0; /* not static */
705 static long ia64_r8, ia64_r10;
706 #elif defined(POWERPC)
707 struct pt_regs ppc_regs;
713 struct pt_regs arm_regs; /* not static */
714 # define ARCH_REGS_FOR_GETREGSET arm_regs
715 #elif defined(AARCH64)
719 # define ARM_cpsr uregs[16]
720 # define ARM_pc uregs[15]
721 # define ARM_lr uregs[14]
722 # define ARM_sp uregs[13]
723 # define ARM_ip uregs[12]
724 # define ARM_fp uregs[11]
725 # define ARM_r10 uregs[10]
726 # define ARM_r9 uregs[9]
727 # define ARM_r8 uregs[8]
728 # define ARM_r7 uregs[7]
729 # define ARM_r6 uregs[6]
730 # define ARM_r5 uregs[5]
731 # define ARM_r4 uregs[4]
732 # define ARM_r3 uregs[3]
733 # define ARM_r2 uregs[2]
734 # define ARM_r1 uregs[1]
735 # define ARM_r0 uregs[0]
736 # define ARM_ORIG_r0 uregs[17]
738 struct user_pt_regs aarch64_r;
739 struct arm_pt_regs arm_r;
741 # define aarch64_regs arm_regs_union.aarch64_r
742 # define arm_regs arm_regs_union.arm_r
743 static struct iovec aarch64_io = {
744 .iov_base = &arm_regs_union
747 static long alpha_r0;
748 static long alpha_a3;
750 static struct pt_regs avr32_regs;
751 #elif defined(SPARC) || defined(SPARC64)
752 struct pt_regs sparc_regs; /* not static */
753 #elif defined(LINUX_MIPSN32)
754 static long long mips_a3;
755 static long long mips_r2;
759 #elif defined(S390) || defined(S390X)
760 static long s390_gpr2;
762 static long hppa_r28;
767 #elif defined(CRISV10) || defined(CRISV32)
768 static long cris_r10;
770 struct pt_regs tile_regs;
771 #elif defined(MICROBLAZE)
772 static long microblaze_r3;
774 static struct user_regs_struct or1k_regs;
775 # define ARCH_REGS_FOR_GETREGSET or1k_regs
777 static struct user_gp_regs metag_regs;
778 # define ARCH_REGS_FOR_GETREGSET metag_regs
779 #elif defined(XTENSA)
780 static long xtensa_a2;
782 static struct user_regs_struct arc_regs;
783 # define ARCH_REGS_FOR_GETREGSET arc_regs
787 print_pc(struct tcb *tcp)
789 #define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
790 sizeof(long) == 8 ? "[????????????????] " : \
792 if (get_regs_error) {
797 tprintf("[%08lx] ", i386_regs.eip);
798 #elif defined(S390) || defined(S390X)
800 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0) {
805 tprintf("[%08lx] ", psw);
807 tprintf("[%016lx] ", psw);
809 #elif defined(X86_64) || defined(X32)
810 if (x86_io.iov_len == sizeof(i386_regs)) {
811 tprintf("[%08x] ", (unsigned) i386_regs.eip);
814 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
816 /* Note: this truncates 64-bit rip to 32 bits */
817 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
822 if (upeek(tcp->pid, PT_B0, &ip) < 0) {
826 tprintf("[%08lx] ", ip);
827 #elif defined(POWERPC)
828 long pc = ppc_regs.nip;
830 tprintf("[%016lx] ", pc);
832 tprintf("[%08lx] ", pc);
836 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
837 tprints("[????????] ");
840 tprintf("[%08lx] ", pc);
843 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
844 tprints("[????????????????] ");
847 tprintf("[%08lx] ", pc);
849 tprintf("[%08lx] ", sparc_regs.pc);
850 #elif defined(SPARC64)
851 tprintf("[%08lx] ", sparc_regs.tpc);
854 if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
855 tprints("[????????] ");
858 tprintf("[%08lx] ", pc);
861 if (upeek(tcp->pid, REG_EPC, &pc) < 0) {
862 tprints("[????????] ");
865 tprintf("[%08lx] ", pc);
868 if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
869 tprints("[????????] ");
872 tprintf("[%08lx] ", pc);
875 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
876 tprints("[????????????????] ");
879 tprintf("[%08lx] ", pc);
881 tprintf("[%08lx] ", arm_regs.ARM_pc);
882 #elif defined(AARCH64)
883 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
885 tprintf("[%08lx] ", avr32_regs.pc);
888 if (upeek(tcp->pid, PT_PC, &pc) < 0) {
892 tprintf("[%08lx] ", pc);
893 #elif defined(CRISV10)
895 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
899 tprintf("[%08lx] ", pc);
900 #elif defined(CRISV32)
902 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
906 tprintf("[%08lx] ", pc);
909 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
911 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
914 tprintf("[%08lx] ", or1k_regs.pc);
916 tprintf("[%08lx] ", metag_regs.pc);
917 #elif defined(XTENSA)
919 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
923 tprintf("[%08lx] ", pc);
925 tprintf("[%08lx] ", arc_regs.efa);
926 #endif /* architecture */
930 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
931 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
933 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
935 shuffle_scno(unsigned long scno)
937 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
940 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
941 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
943 if (scno == 0x000ffff0)
944 return ARM_FIRST_SHUFFLED_SYSCALL;
946 #define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
948 * Is it ARM specific syscall?
949 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
950 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
952 if (scno >= 0x000f0000 &&
953 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
954 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
956 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
957 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
963 # define shuffle_scno(scno) ((long)(scno))
967 undefined_scno_name(struct tcb *tcp)
969 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
971 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
977 * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
978 * we provide a slow fallback for old kernels.
980 static int powerpc_getregs_old(pid_t pid)
986 r = upeek(pid, sizeof(long) * PT_NIP, (long *)&ppc_regs.nip);
990 #ifdef POWERPC64 /* else we never use it */
991 r = upeek(pid, sizeof(long) * PT_MSR, (long *)&ppc_regs.msr);
995 r = upeek(pid, sizeof(long) * PT_CCR, (long *)&ppc_regs.ccr);
998 r = upeek(pid, sizeof(long) * PT_ORIG_R3, (long *)&ppc_regs.orig_gpr3);
1001 for (i = 0; i <= 8; i++) {
1002 r = upeek(pid, sizeof(long) * (PT_R0 + i),
1003 (long *)&ppc_regs.gpr[i]);
1013 long get_regs_error;
1015 #if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1016 static void get_regset(pid_t pid)
1018 /* constant iovec */
1024 static struct iovec io = {
1025 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1026 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1028 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1030 /* variable iovec */
1031 # elif defined(X86_64) || defined(X32)
1032 /* x86_io.iov_base = &x86_regs_union; - already is */
1033 x86_io.iov_len = sizeof(x86_regs_union);
1034 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &x86_io);
1035 # elif defined(AARCH64)
1036 /* aarch64_io.iov_base = &arm_regs_union; - already is */
1037 aarch64_io.iov_len = sizeof(arm_regs_union);
1038 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &aarch64_io);
1040 # warning both PTRACE_GETREGSET and NT_PRSTATUS are available but not yet used
1043 #endif /* PTRACE_GETREGSET && NT_PRSTATUS */
1048 /* PTRACE_GETREGSET only */
1049 # if defined(METAG) || defined(OR1K) || defined(X32) || defined(AARCH64) || defined(ARC)
1052 /* PTRACE_GETREGS only */
1053 # elif defined(AVR32)
1054 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
1055 # elif defined(TILE)
1056 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &tile_regs);
1057 # elif defined(SPARC) || defined(SPARC64)
1058 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
1059 # elif defined(POWERPC)
1060 static bool old_kernel = 0;
1063 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &ppc_regs);
1064 if (get_regs_error && errno == EIO) {
1067 get_regs_error = powerpc_getregs_old(pid);
1070 /* try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS */
1072 # if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1073 static int getregset_support;
1075 if (getregset_support >= 0) {
1077 if (getregset_support > 0)
1079 if (get_regs_error >= 0) {
1080 getregset_support = 1;
1083 if (errno == EPERM || errno == ESRCH)
1085 getregset_support = -1;
1087 # endif /* PTRACE_GETREGSET && NT_PRSTATUS */
1089 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &arm_regs);
1090 # elif defined(I386)
1091 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &i386_regs);
1092 # elif defined(X86_64)
1093 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1094 x86_io.iov_len = sizeof(x86_64_regs);
1095 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
1096 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1097 x86_io.iov_len = sizeof(i386_regs);
1099 * The order is important: i386_regs and x86_64_regs
1100 * are overlaid in memory!
1102 i386_regs.ebx = x86_64_regs.rbx;
1103 i386_regs.ecx = x86_64_regs.rcx;
1104 i386_regs.edx = x86_64_regs.rdx;
1105 i386_regs.esi = x86_64_regs.rsi;
1106 i386_regs.edi = x86_64_regs.rdi;
1107 i386_regs.ebp = x86_64_regs.rbp;
1108 i386_regs.eax = x86_64_regs.rax;
1109 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1110 /* i386_regs.xes = x86_64_regs.es; ditto... */
1111 /* i386_regs.xfs = x86_64_regs.fs; */
1112 /* i386_regs.xgs = x86_64_regs.gs; */
1113 i386_regs.orig_eax = x86_64_regs.orig_rax;
1114 i386_regs.eip = x86_64_regs.rip;
1115 /* i386_regs.xcs = x86_64_regs.cs; */
1116 /* i386_regs.eflags = x86_64_regs.eflags; */
1117 i386_regs.esp = x86_64_regs.rsp;
1118 /* i386_regs.xss = x86_64_regs.ss; */
1121 # error unhandled architecture
1122 # endif /* ARM || I386 || X86_64 */
1125 #endif /* !get_regs */
1128 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1129 * 1: ok, continue in trace_syscall_entering().
1130 * other: error, trace_syscall_entering() should print error indicator
1131 * ("????" etc) and bail out.
1134 get_scno(struct tcb *tcp)
1138 #if defined(S390) || defined(S390X)
1139 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
1142 if (s390_gpr2 != -ENOSYS) {
1144 * Since kernel version 2.5.44 the scno gets passed in gpr2.
1149 * Old style of "passing" the scno via the SVC instruction.
1152 long opcode, offset_reg, tmp;
1154 static const int gpr_offset[16] = {
1155 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1156 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1157 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1158 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1161 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0)
1164 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
1166 perror_msg("peektext(psw-oneword)");
1171 * We have to check if the SVC got executed directly or via an
1172 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1173 * instruction decoding to derive the system call number.
1174 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1175 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1176 * opcode. Since there is no way to find out the opcode size this
1177 * is the best we can do...
1179 if ((opcode & 0xff00) == 0x0a00) {
1181 scno = opcode & 0xff;
1184 /* SVC got executed by EXECUTE instruction */
1187 * Do instruction decoding of EXECUTE. If you really want to
1188 * understand this, read the Principles of Operations.
1190 svc_addr = (void *) (opcode & 0xfff);
1193 offset_reg = (opcode & 0x000f0000) >> 16;
1194 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
1199 offset_reg = (opcode & 0x0000f000) >> 12;
1200 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
1204 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
1213 offset_reg = (opcode & 0x00f00000) >> 20;
1214 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
1217 scno = (scno | tmp) & 0xff;
1220 #elif defined(POWERPC)
1221 scno = ppc_regs.gpr[0];
1223 unsigned int currpers;
1226 * Check for 64/32 bit mode.
1227 * Embedded implementations covered by Book E extension of PPC use
1228 * bit 0 (CM) of 32-bit Machine state register (MSR).
1229 * Other implementations use bit 0 (SF) of 64-bit MSR.
1231 currpers = (ppc_regs.msr & 0x8000000080000000) ? 0 : 1;
1232 update_personality(tcp, currpers);
1234 #elif defined(AVR32)
1235 scno = avr32_regs.r8;
1237 if (upeek(tcp->pid, PT_ORIG_P0, &scno))
1240 scno = i386_regs.orig_eax;
1241 #elif defined(X86_64) || defined(X32)
1242 # ifndef __X32_SYSCALL_BIT
1243 # define __X32_SYSCALL_BIT 0x40000000
1245 unsigned int currpers;
1247 /* GETREGSET of NT_PRSTATUS tells us regset size,
1248 * which unambiguously detects i386.
1250 * Linux kernel distinguishes x86-64 and x32 processes
1251 * solely by looking at __X32_SYSCALL_BIT:
1252 * arch/x86/include/asm/compat.h::is_x32_task():
1253 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1256 if (x86_io.iov_len == sizeof(i386_regs)) {
1257 scno = i386_regs.orig_eax;
1260 scno = x86_64_regs.orig_rax;
1262 if (scno & __X32_SYSCALL_BIT) {
1264 * Syscall number -1 requires special treatment:
1265 * it might be a side effect of SECCOMP_RET_ERRNO
1266 * filtering that sets orig_rax to -1
1267 * in some versions of linux kernel.
1268 * If that is the case, then
1269 * __X32_SYSCALL_BIT logic does not apply.
1271 if ((long long) x86_64_regs.orig_rax != -1) {
1272 scno -= __X32_SYSCALL_BIT;
1282 /* cs = 0x33 for long mode (native 64 bit and x32)
1283 * cs = 0x23 for compatibility mode (32 bit)
1284 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1286 scno = x86_64_regs.orig_rax;
1287 switch (x86_64_regs.cs) {
1288 case 0x23: currpers = 1; break;
1290 if (x86_64_regs.ds == 0x2b) {
1292 scno &= ~__X32_SYSCALL_BIT;
1297 fprintf(stderr, "Unknown value CS=0x%08X while "
1298 "detecting personality of process "
1299 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
1300 currpers = current_personality;
1304 /* This version analyzes the opcode of a syscall instruction.
1305 * (int 0x80 on i386 vs. syscall on x86-64)
1306 * It works, but is too complicated, and strictly speaking, unreliable.
1308 unsigned long call, rip = x86_64_regs.rip;
1309 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1312 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
1314 fprintf(stderr, "ptrace_peektext failed: %s\n",
1316 switch (call & 0xffff) {
1317 /* x86-64: syscall = 0x0f 0x05 */
1318 case 0x050f: currpers = 0; break;
1319 /* i386: int 0x80 = 0xcd 0x80 */
1320 case 0x80cd: currpers = 1; break;
1322 currpers = current_personality;
1324 "Unknown syscall opcode (0x%04X) while "
1325 "detecting personality of process "
1326 "PID=%d\n", (int)call, tcp->pid);
1332 /* If we are built for a x32 system, then personality 0 is x32
1333 * (not x86_64), and stracing of x86_64 apps is not supported.
1334 * Stracing of i386 apps is still supported.
1336 if (currpers == 0) {
1337 fprintf(stderr, "syscall_%lu(...) in unsupported "
1338 "64-bit mode of process PID=%d\n",
1342 currpers &= ~2; /* map 2,1 to 0,1 */
1344 update_personality(tcp, currpers);
1346 # define IA64_PSR_IS ((long)1 << 34)
1348 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
1349 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
1350 if (ia64_ia32mode) {
1351 if (upeek(tcp->pid, PT_R1, &scno) < 0)
1354 if (upeek(tcp->pid, PT_R15, &scno) < 0)
1357 #elif defined(AARCH64)
1358 switch (aarch64_io.iov_len) {
1359 case sizeof(aarch64_regs):
1360 /* We are in 64-bit mode */
1361 scno = aarch64_regs.regs[8];
1362 update_personality(tcp, 1);
1364 case sizeof(arm_regs):
1365 /* We are in 32-bit mode */
1366 /* Note: we don't support OABI, unlike 32-bit ARM build */
1367 scno = arm_regs.ARM_r7;
1368 scno = shuffle_scno(scno);
1369 update_personality(tcp, 0);
1373 if (arm_regs.ARM_ip != 0) {
1374 /* It is not a syscall entry */
1375 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1376 tcp->flags |= TCB_INSYSCALL;
1379 /* Note: we support only 32-bit CPUs, not 26-bit */
1381 # if !defined(__ARM_EABI__) || ENABLE_ARM_OABI
1382 if (arm_regs.ARM_cpsr & 0x20)
1386 /* Check EABI/OABI by examining SVC insn's low 24 bits */
1388 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1391 /* EABI syscall convention? */
1392 if ((unsigned long) scno != 0xef000000) {
1394 if ((scno & 0x0ff00000) != 0x0f900000) {
1395 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1399 /* Fixup the syscall number */
1403 scno = arm_regs.ARM_r7;
1405 # else /* __ARM_EABI__ || !ENABLE_ARM_OABI */
1406 scno = arm_regs.ARM_r7;
1408 scno = shuffle_scno(scno);
1410 if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
1412 #elif defined(LINUX_MIPSN32)
1413 unsigned long long regs[38];
1415 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1417 mips_a3 = regs[REG_A3];
1418 mips_r2 = regs[REG_V0];
1421 if (!SCNO_IN_RANGE(scno)) {
1422 if (mips_a3 == 0 || mips_a3 == -1) {
1424 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1429 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
1431 if (upeek(tcp->pid, REG_V0, &scno) < 0)
1434 if (!SCNO_IN_RANGE(scno)) {
1435 if (mips_a3 == 0 || mips_a3 == -1) {
1437 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1441 #elif defined(ALPHA)
1442 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
1444 if (upeek(tcp->pid, REG_R0, &scno) < 0)
1448 * Do some sanity checks to figure out if it's
1449 * really a syscall entry
1451 if (!SCNO_IN_RANGE(scno)) {
1452 if (alpha_a3 == 0 || alpha_a3 == -1) {
1454 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
1458 #elif defined(SPARC) || defined(SPARC64)
1459 /* Disassemble the syscall trap. */
1460 /* Retrieve the syscall trap instruction. */
1463 # if defined(SPARC64)
1464 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
1467 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
1472 /* Disassemble the trap to see what personality to use. */
1475 /* Linux/SPARC syscall trap. */
1476 update_personality(tcp, 0);
1479 /* Linux/SPARC64 syscall trap. */
1480 update_personality(tcp, 2);
1483 /* SunOS syscall trap. (pers 1) */
1484 fprintf(stderr, "syscall: SunOS no support\n");
1487 /* Solaris 2.x syscall trap. (per 2) */
1488 update_personality(tcp, 1);
1491 /* NetBSD/FreeBSD syscall trap. */
1492 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1495 /* Solaris 2.x gettimeofday */
1496 update_personality(tcp, 1);
1499 # if defined(SPARC64)
1500 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
1502 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
1507 /* Extract the system call number from the registers. */
1508 if (trap == 0x91d02027)
1511 scno = sparc_regs.u_regs[U_REG_G1];
1513 scno = sparc_regs.u_regs[U_REG_O0];
1514 memmove(&sparc_regs.u_regs[U_REG_O0], &sparc_regs.u_regs[U_REG_O1], 7*sizeof(sparc_regs.u_regs[0]));
1517 if (upeek(tcp->pid, PT_GR20, &scno) < 0)
1521 * In the new syscall ABI, the system call number is in R3.
1523 if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
1527 /* Odd as it may seem, a glibc bug has been known to cause
1528 glibc to issue bogus negative syscall numbers. So for
1529 our purposes, make strace print what it *should* have been */
1530 long correct_scno = (scno & 0xff);
1533 "Detected glibc bug: bogus system call"
1534 " number = %ld, correcting to %ld\n",
1537 scno = correct_scno;
1540 if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
1543 #elif defined(CRISV10) || defined(CRISV32)
1544 if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
1547 unsigned int currpers;
1548 scno = tile_regs.regs[10];
1552 # ifndef PT_FLAGS_COMPAT
1553 # define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1555 if (tile_regs.flags & PT_FLAGS_COMPAT)
1560 update_personality(tcp, currpers);
1561 #elif defined(MICROBLAZE)
1562 if (upeek(tcp->pid, 0, &scno) < 0)
1565 scno = or1k_regs.gpr[11];
1566 #elif defined(METAG)
1567 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
1568 #elif defined(XTENSA)
1569 if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
1572 scno = arc_regs.scratch.r8;
1576 if (SCNO_IS_VALID(tcp->scno)) {
1577 tcp->s_ent = &sysent[scno];
1578 tcp->qual_flg = qual_flags[scno];
1580 static const struct_sysent unknown = {
1583 .sys_func = printargs,
1584 .sys_name = "unknown", /* not used */
1586 tcp->s_ent = &unknown;
1587 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1593 * Cannot rely on __kernel_[u]long_t being defined,
1594 * it is quite a recent feature of <asm/posix_types.h>.
1596 #ifdef __kernel_long_t
1597 typedef __kernel_long_t kernel_long_t;
1598 typedef __kernel_ulong_t kernel_ulong_t;
1601 typedef long long kernel_long_t;
1602 typedef unsigned long long kernel_ulong_t;
1604 typedef long kernel_long_t;
1605 typedef unsigned long kernel_ulong_t;
1610 * Check the syscall return value register value for whether it is
1611 * a negated errno code indicating an error, or a success return value.
1614 is_negated_errno(kernel_ulong_t val)
1617 * Thanks to SECCOMP_RET_DATA == 0xffff, abnormally large errno
1618 * values could be easily seen when a seccomp filter is used, e.g.
1619 * BPF_STMT(BPF_RET, SECCOMP_RET_ERRNO | SECCOMP_RET_DATA)
1621 kernel_ulong_t max = -(kernel_long_t) 0x10000; /* SECCOMP_RET_DATA + 1 */
1623 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1624 if (current_wordsize < sizeof(val)) {
1625 val = (uint32_t) val;
1626 max = (uint32_t) max;
1630 * current_wordsize is 4 even in personality 0 (native X32)
1631 * but truncation _must not_ be done in it.
1632 * can't check current_wordsize here!
1634 if (current_personality != 0) {
1635 val = (uint32_t) val;
1636 max = (uint32_t) max;
1643 /* Called at each syscall entry.
1645 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1646 * 1: ok, continue in trace_syscall_entering().
1647 * other: error, trace_syscall_entering() should print error indicator
1648 * ("????" etc) and bail out.
1651 syscall_fixup_on_sysenter(struct tcb *tcp)
1653 /* Do we have post-execve SIGTRAP suppressed? */
1654 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1658 * No, unfortunately. Apply -ENOSYS heuristics.
1659 * We don't have to workaround SECCOMP_RET_ERRNO side effects
1660 * because any kernel with SECCOMP_RET_ERRNO support surely
1661 * implements PTRACE_O_TRACEEXEC.
1664 if (i386_regs.eax != -ENOSYS) {
1666 fprintf(stderr, "not a syscall entry (eax = %ld)\n",
1670 #elif defined(X86_64) || defined(X32)
1671 if (x86_io.iov_len == sizeof(i386_regs)) {
1672 if ((int) i386_regs.eax != -ENOSYS) {
1675 "not a syscall entry (eax = %d)\n",
1676 (int) i386_regs.eax);
1680 if ((long long) x86_64_regs.rax != -ENOSYS) {
1683 "not a syscall entry (rax = %lld)\n",
1684 (long long) x86_64_regs.rax);
1689 /* TODO? Eliminate upeek's in arches below like we did in x86 */
1690 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
1692 if (m68k_d0 != -ENOSYS) {
1694 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
1698 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
1700 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
1702 if (ia64_ia32mode && ia64_r8 != -ENOSYS) {
1704 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
1707 #elif defined(CRISV10) || defined(CRISV32)
1708 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
1710 if (cris_r10 != -ENOSYS) {
1712 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
1715 #elif defined(MICROBLAZE)
1716 if (upeek(tcp->pid, 3 * 4, µblaze_r3) < 0)
1718 if (microblaze_r3 != -ENOSYS) {
1720 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
1728 internal_fork(struct tcb *tcp)
1730 #if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1731 # define ARG_FLAGS 1
1733 # define ARG_FLAGS 0
1735 #ifndef CLONE_UNTRACED
1736 # define CLONE_UNTRACED 0x00800000
1738 if ((ptrace_setoptions
1739 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1740 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1746 if (entering(tcp)) {
1748 * We won't see the new child if clone is called with
1749 * CLONE_UNTRACED, so we keep the same logic with that option
1750 * and don't trace it.
1752 if ((tcp->s_ent->sys_func == sys_clone)
1753 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1758 if (tcp->flags & TCB_BPTSET)
1763 #if defined(TCB_WAITEXECVE)
1765 internal_exec(struct tcb *tcp)
1767 /* Maybe we have post-execve SIGTRAP suppressed? */
1768 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1769 return; /* yes, no need to do anything */
1771 if (exiting(tcp) && syserror(tcp))
1772 /* Error in execve, no post-execve SIGTRAP expected */
1773 tcp->flags &= ~TCB_WAITEXECVE;
1775 tcp->flags |= TCB_WAITEXECVE;
1780 syscall_fixup_for_fork_exec(struct tcb *tcp)
1783 * We must always trace a few critical system calls in order to
1784 * correctly support following forks in the presence of tracing
1789 func = tcp->s_ent->sys_func;
1791 if ( sys_fork == func
1792 || sys_clone == func
1798 #if defined(TCB_WAITEXECVE)
1799 if ( sys_execve == func
1800 # if defined(SPARC) || defined(SPARC64)
1801 || sys_execv == func
1810 /* Return -1 on error or 1 on success (never 0!) */
1812 get_syscall_args(struct tcb *tcp)
1816 nargs = tcp->s_ent->nargs;
1818 #if defined(S390) || defined(S390X)
1819 for (i = 0; i < nargs; ++i)
1820 if (upeek(tcp->pid, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1822 #elif defined(ALPHA)
1823 for (i = 0; i < nargs; ++i)
1824 if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1827 if (!ia64_ia32mode) {
1828 unsigned long *out0, cfm, sof, sol;
1830 /* be backwards compatible with kernel < 2.4.4... */
1832 # define PT_RBS_END PT_AR_BSP
1835 if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
1837 if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
1840 sof = (cfm >> 0) & 0x7f;
1841 sol = (cfm >> 7) & 0x7f;
1842 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1844 for (i = 0; i < nargs; ++i) {
1845 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1846 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1850 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1851 PT_R9 /* ECX = out1 */,
1852 PT_R10 /* EDX = out2 */,
1853 PT_R14 /* ESI = out3 */,
1854 PT_R15 /* EDI = out4 */,
1855 PT_R13 /* EBP = out5 */};
1857 for (i = 0; i < nargs; ++i) {
1858 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
1860 /* truncate away IVE sign-extension */
1861 tcp->u_arg[i] &= 0xffffffff;
1864 #elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
1865 /* N32 and N64 both use up to six registers. */
1866 unsigned long long regs[38];
1868 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1871 for (i = 0; i < nargs; ++i) {
1872 tcp->u_arg[i] = regs[REG_A0 + i];
1873 # if defined(LINUX_MIPSN32)
1874 tcp->ext_arg[i] = regs[REG_A0 + i];
1881 if (upeek(tcp->pid, REG_SP, &sp) < 0)
1883 for (i = 0; i < 4; ++i)
1884 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1886 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
1887 (char *)(tcp->u_arg + 4));
1889 for (i = 0; i < nargs; ++i)
1890 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1893 #elif defined(POWERPC)
1896 tcp->u_arg[0] = ppc_regs.orig_gpr3;
1897 tcp->u_arg[1] = ppc_regs.gpr[4];
1898 tcp->u_arg[2] = ppc_regs.gpr[5];
1899 tcp->u_arg[3] = ppc_regs.gpr[6];
1900 tcp->u_arg[4] = ppc_regs.gpr[7];
1901 tcp->u_arg[5] = ppc_regs.gpr[8];
1902 #elif defined(SPARC) || defined(SPARC64)
1903 for (i = 0; i < nargs; ++i)
1904 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
1906 for (i = 0; i < nargs; ++i)
1907 if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1909 #elif defined(ARM) || defined(AARCH64)
1910 # if defined(AARCH64)
1911 if (tcp->currpers == 1)
1912 for (i = 0; i < nargs; ++i)
1913 tcp->u_arg[i] = aarch64_regs.regs[i];
1916 for (i = 0; i < nargs; ++i)
1917 tcp->u_arg[i] = arm_regs.uregs[i];
1918 #elif defined(AVR32)
1921 tcp->u_arg[0] = avr32_regs.r12;
1922 tcp->u_arg[1] = avr32_regs.r11;
1923 tcp->u_arg[2] = avr32_regs.r10;
1924 tcp->u_arg[3] = avr32_regs.r9;
1925 tcp->u_arg[4] = avr32_regs.r5;
1926 tcp->u_arg[5] = avr32_regs.r3;
1928 static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
1930 for (i = 0; i < nargs; ++i)
1931 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
1934 static const int syscall_regs[MAX_ARGS] = {
1935 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1936 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
1939 for (i = 0; i < nargs; ++i)
1940 if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
1944 /* Registers used by SH5 Linux system calls for parameters */
1945 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
1947 for (i = 0; i < nargs; ++i)
1948 if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1953 tcp->u_arg[0] = i386_regs.ebx;
1954 tcp->u_arg[1] = i386_regs.ecx;
1955 tcp->u_arg[2] = i386_regs.edx;
1956 tcp->u_arg[3] = i386_regs.esi;
1957 tcp->u_arg[4] = i386_regs.edi;
1958 tcp->u_arg[5] = i386_regs.ebp;
1959 #elif defined(X86_64) || defined(X32)
1962 if (x86_io.iov_len != sizeof(i386_regs)) {
1963 /* x86-64 or x32 ABI */
1964 tcp->u_arg[0] = x86_64_regs.rdi;
1965 tcp->u_arg[1] = x86_64_regs.rsi;
1966 tcp->u_arg[2] = x86_64_regs.rdx;
1967 tcp->u_arg[3] = x86_64_regs.r10;
1968 tcp->u_arg[4] = x86_64_regs.r8;
1969 tcp->u_arg[5] = x86_64_regs.r9;
1971 tcp->ext_arg[0] = x86_64_regs.rdi;
1972 tcp->ext_arg[1] = x86_64_regs.rsi;
1973 tcp->ext_arg[2] = x86_64_regs.rdx;
1974 tcp->ext_arg[3] = x86_64_regs.r10;
1975 tcp->ext_arg[4] = x86_64_regs.r8;
1976 tcp->ext_arg[5] = x86_64_regs.r9;
1980 /* Zero-extend from 32 bits */
1981 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1982 * if you need to use *sign-extended* parameter.
1984 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1985 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1986 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1987 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1988 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1989 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
1991 #elif defined(MICROBLAZE)
1992 for (i = 0; i < nargs; ++i)
1993 if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1995 #elif defined(CRISV10) || defined(CRISV32)
1996 static const int crisregs[MAX_ARGS] = {
1997 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
1998 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
2001 for (i = 0; i < nargs; ++i)
2002 if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
2005 for (i = 0; i < nargs; ++i)
2006 tcp->u_arg[i] = tile_regs.regs[i];
2008 for (i = 0; i < nargs; ++i)
2009 if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
2013 for (i = 0; i < 6; ++i)
2014 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
2015 #elif defined(METAG)
2016 for (i = 0; i < nargs; i++)
2017 /* arguments go backwards from D1Ar1 (D1.3) */
2018 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
2019 #elif defined(XTENSA)
2020 /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
2021 static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
2022 for (i = 0; i < nargs; ++i)
2023 if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
2026 long *arc_args = &arc_regs.scratch.r0;
2027 for (i = 0; i < nargs; ++i)
2028 tcp->u_arg[i] = *arc_args--;
2030 #else /* Other architecture (32bits specific) */
2031 for (i = 0; i < nargs; ++i)
2032 if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
2039 trace_syscall_entering(struct tcb *tcp)
2043 #if defined TCB_WAITEXECVE
2044 if (tcp->flags & TCB_WAITEXECVE) {
2045 /* This is the post-execve SIGTRAP. */
2046 tcp->flags &= ~TCB_WAITEXECVE;
2051 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
2055 res = syscall_fixup_on_sysenter(tcp);
2059 res = get_syscall_args(tcp);
2065 tprints("????" /* anti-trigraph gap */ "(");
2066 else if (tcp->qual_flg & UNDEFINED_SCNO)
2067 tprintf("%s(", undefined_scno_name(tcp));
2069 tprintf("%s(", tcp->s_ent->sys_name);
2071 * " <unavailable>" will be added later by the code which
2072 * detects ptrace errors.
2077 if ( sys_execve == tcp->s_ent->sys_func
2078 # if defined(SPARC) || defined(SPARC64)
2079 || sys_execv == tcp->s_ent->sys_func
2082 hide_log_until_execve = 0;
2085 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
2087 # ifdef SYS_socket_subcall
2088 if (tcp->s_ent->sys_func == sys_socketcall) {
2089 decode_socket_subcall(tcp);
2093 # ifdef SYS_ipc_subcall
2094 if (tcp->s_ent->sys_func == sys_ipc) {
2095 decode_ipc_subcall(tcp);
2103 if (need_fork_exec_workarounds)
2104 syscall_fixup_for_fork_exec(tcp);
2106 if (!(tcp->qual_flg & QUAL_TRACE)
2107 || (tracing_paths && !pathtrace_match(tcp))
2109 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
2113 tcp->flags &= ~TCB_FILTERED;
2115 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
2120 #ifdef USE_LIBUNWIND
2121 if (stack_trace_enabled) {
2122 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
2123 unwind_capture_stacktrace(tcp);
2128 if (tcp->qual_flg & UNDEFINED_SCNO)
2129 tprintf("%s(", undefined_scno_name(tcp));
2131 tprintf("%s(", tcp->s_ent->sys_name);
2132 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
2133 res = printargs(tcp);
2135 res = tcp->s_ent->sys_func(tcp);
2139 tcp->flags |= TCB_INSYSCALL;
2140 /* Measure the entrance time as late as possible to avoid errors. */
2142 gettimeofday(&tcp->etime, NULL);
2147 * 1: ok, continue in trace_syscall_exiting().
2148 * -1: error, trace_syscall_exiting() should print error indicator
2149 * ("????" etc) and bail out.
2152 get_syscall_result(struct tcb *tcp)
2154 #if defined(S390) || defined(S390X)
2155 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
2157 #elif defined(POWERPC)
2158 /* already done by get_regs */
2159 #elif defined(AVR32)
2160 /* already done by get_regs */
2162 if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
2165 /* already done by get_regs */
2166 #elif defined(X86_64) || defined(X32)
2167 /* already done by get_regs */
2169 # define IA64_PSR_IS ((long)1 << 34)
2171 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
2172 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
2173 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
2175 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
2178 /* already done by get_regs */
2179 #elif defined(AARCH64)
2180 /* register reading already done by get_regs */
2182 /* Used to do this, but we did it on syscall entry already: */
2183 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2184 * else it's personality 0.
2186 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
2188 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
2190 #elif defined(LINUX_MIPSN32)
2191 unsigned long long regs[38];
2193 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
2195 mips_a3 = regs[REG_A3];
2196 mips_r2 = regs[REG_V0];
2198 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
2200 if (upeek(tcp->pid, REG_V0, &mips_r2) < 0)
2202 #elif defined(ALPHA)
2203 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
2205 if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
2207 #elif defined(SPARC) || defined(SPARC64)
2208 /* already done by get_regs */
2210 if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
2213 /* new syscall ABI returns result in R0 */
2214 if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
2217 /* ABI defines result returned in r9 */
2218 if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
2220 #elif defined(CRISV10) || defined(CRISV32)
2221 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
2224 /* already done by get_regs */
2225 #elif defined(MICROBLAZE)
2226 if (upeek(tcp->pid, 3 * 4, µblaze_r3) < 0)
2229 /* already done by get_regs */
2230 #elif defined(METAG)
2231 /* already done by get_regs */
2232 #elif defined(XTENSA)
2233 if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
2236 /* already done by get_regs */
2241 /* Called at each syscall exit */
2243 syscall_fixup_on_sysexit(struct tcb *tcp)
2245 #if defined(S390) || defined(S390X)
2246 if ((tcp->flags & TCB_WAITEXECVE)
2247 && (s390_gpr2 == -ENOSYS || s390_gpr2 == tcp->scno)) {
2249 * Return from execve.
2250 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2251 * flag set for the post-execve SIGTRAP to see and reset.
2259 * 1: ok, continue in trace_syscall_exiting().
2260 * -1: error, trace_syscall_exiting() should print error indicator
2261 * ("????" etc) and bail out.
2264 get_error(struct tcb *tcp)
2267 int check_errno = 1;
2268 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
2271 #if defined(S390) || defined(S390X)
2272 if (check_errno && is_negated_errno(s390_gpr2)) {
2274 u_error = -s390_gpr2;
2277 tcp->u_rval = s390_gpr2;
2280 if (check_errno && is_negated_errno(i386_regs.eax)) {
2282 u_error = -i386_regs.eax;
2285 tcp->u_rval = i386_regs.eax;
2287 #elif defined(X86_64) || defined(X32)
2289 * In X32, return value is 64-bit (llseek uses one).
2290 * Using merely "long rax" would not work.
2294 if (x86_io.iov_len == sizeof(i386_regs)) {
2295 /* Sign extend from 32 bits */
2296 rax = (int32_t) i386_regs.eax;
2298 rax = x86_64_regs.rax;
2300 if (check_errno && is_negated_errno(rax)) {
2307 /* tcp->u_rval contains a truncated value */
2312 if (ia64_ia32mode) {
2316 if (check_errno && is_negated_errno(err)) {
2324 if (check_errno && ia64_r10) {
2328 tcp->u_rval = ia64_r8;
2332 if (check_errno && mips_a3) {
2336 tcp->u_rval = mips_r2;
2337 # if defined(LINUX_MIPSN32)
2338 tcp->u_lrval = mips_r2;
2341 #elif defined(POWERPC)
2342 if (check_errno && (ppc_regs.ccr & 0x10000000)) {
2344 u_error = ppc_regs.gpr[3];
2347 tcp->u_rval = ppc_regs.gpr[3];
2350 if (check_errno && is_negated_errno(m68k_d0)) {
2355 tcp->u_rval = m68k_d0;
2357 #elif defined(ARM) || defined(AARCH64)
2358 # if defined(AARCH64)
2359 if (tcp->currpers == 1) {
2360 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2362 u_error = -aarch64_regs.regs[0];
2365 tcp->u_rval = aarch64_regs.regs[0];
2371 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
2373 u_error = -arm_regs.ARM_r0;
2376 tcp->u_rval = arm_regs.ARM_r0;
2379 #elif defined(AVR32)
2380 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
2382 u_error = -avr32_regs.r12;
2385 tcp->u_rval = avr32_regs.r12;
2388 if (check_errno && is_negated_errno(bfin_r0)) {
2392 tcp->u_rval = bfin_r0;
2394 #elif defined(ALPHA)
2395 if (check_errno && alpha_a3) {
2400 tcp->u_rval = alpha_r0;
2402 #elif defined(SPARC)
2403 if (check_errno && sparc_regs.psr & PSR_C) {
2405 u_error = sparc_regs.u_regs[U_REG_O0];
2408 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
2410 #elif defined(SPARC64)
2411 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
2413 u_error = sparc_regs.u_regs[U_REG_O0];
2416 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
2419 if (check_errno && is_negated_errno(hppa_r28)) {
2421 u_error = -hppa_r28;
2424 tcp->u_rval = hppa_r28;
2427 if (check_errno && is_negated_errno(sh_r0)) {
2432 tcp->u_rval = sh_r0;
2435 if (check_errno && is_negated_errno(sh64_r9)) {
2440 tcp->u_rval = sh64_r9;
2442 #elif defined(METAG)
2443 /* result pointer in D0Re0 (D0.0) */
2444 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2446 u_error = -metag_regs.dx[0][0];
2449 tcp->u_rval = metag_regs.dx[0][0];
2451 #elif defined(CRISV10) || defined(CRISV32)
2452 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
2454 u_error = -cris_r10;
2457 tcp->u_rval = cris_r10;
2461 * The standard tile calling convention returns the value (or negative
2462 * errno) in r0, and zero (or positive errno) in r1.
2463 * Until at least kernel 3.8, however, the r1 value is not reflected
2464 * in ptregs at this point, so we use r0 here.
2466 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
2468 u_error = -tile_regs.regs[0];
2470 tcp->u_rval = tile_regs.regs[0];
2472 #elif defined(MICROBLAZE)
2473 if (check_errno && is_negated_errno(microblaze_r3)) {
2475 u_error = -microblaze_r3;
2478 tcp->u_rval = microblaze_r3;
2481 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2483 u_error = -or1k_regs.gpr[11];
2486 tcp->u_rval = or1k_regs.gpr[11];
2488 #elif defined(XTENSA)
2489 if (check_errno && is_negated_errno(xtensa_a2)) {
2491 u_error = -xtensa_a2;
2494 tcp->u_rval = xtensa_a2;
2497 if (check_errno && is_negated_errno(arc_regs.scratch.r0)) {
2499 u_error = -arc_regs.scratch.r0;
2502 tcp->u_rval = arc_regs.scratch.r0;
2505 tcp->u_error = u_error;
2509 dumpio(struct tcb *tcp)
2515 if ((unsigned long) tcp->u_arg[0] >= num_quals)
2517 func = tcp->s_ent->sys_func;
2518 if (func == printargs)
2520 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
2521 if (func == sys_read ||
2522 func == sys_pread ||
2524 func == sys_recvfrom) {
2525 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
2527 } else if (func == sys_readv) {
2528 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2531 } else if (func == sys_recvmsg) {
2532 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
2534 } else if (func == sys_recvmmsg) {
2535 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
2540 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
2541 if (func == sys_write ||
2542 func == sys_pwrite ||
2545 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
2546 else if (func == sys_writev)
2547 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2549 else if (func == sys_sendmsg)
2550 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
2551 else if (func == sys_sendmmsg)
2552 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
2558 trace_syscall_exiting(struct tcb *tcp)
2565 /* Measure the exit time as early as possible to avoid errors. */
2567 gettimeofday(&tv, NULL);
2569 #ifdef USE_LIBUNWIND
2570 if (stack_trace_enabled) {
2571 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
2572 unwind_cache_invalidate(tcp);
2576 #if SUPPORTED_PERSONALITIES > 1
2577 update_personality(tcp, tcp->currpers);
2579 res = (get_regs_error ? -1 : get_syscall_result(tcp));
2581 syscall_fixup_on_sysexit(tcp); /* never fails */
2582 get_error(tcp); /* never fails */
2583 if (need_fork_exec_workarounds)
2584 syscall_fixup_for_fork_exec(tcp);
2585 if (filtered(tcp) || hide_log_until_execve)
2590 count_syscall(tcp, &tv);
2591 if (cflag == CFLAG_ONLY_STATS) {
2596 /* If not in -ff mode, and printing_tcp != tcp,
2597 * then the log currently does not end with output
2598 * of _our syscall entry_, but with something else.
2599 * We need to say which syscall's return is this.
2601 * Forced reprinting via TCB_REPRINT is used only by
2602 * "strace -ff -oLOG test/threaded_execve" corner case.
2603 * It's the only case when -ff mode needs reprinting.
2605 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2606 tcp->flags &= ~TCB_REPRINT;
2608 if (tcp->qual_flg & UNDEFINED_SCNO)
2609 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
2611 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
2616 /* There was error in one of prior ptrace ops */
2619 tprints("= ? <unavailable>\n");
2621 tcp->flags &= ~TCB_INSYSCALL;
2626 if (tcp->qual_flg & QUAL_RAW) {
2627 /* sys_res = printargs(tcp); - but it's nop on sysexit */
2629 /* FIXME: not_failing_only (IOW, option -z) is broken:
2630 * failure of syscall is known only after syscall return.
2631 * Thus we end up with something like this on, say, ENOENT:
2632 * open("doesnt_exist", O_RDONLY <unfinished ...>
2633 * {next syscall decode}
2634 * whereas the intended result is that open(...) line
2635 * is not shown at all.
2637 if (not_failing_only && tcp->u_error)
2638 goto ret; /* ignore failed syscalls */
2639 sys_res = tcp->s_ent->sys_func(tcp);
2644 u_error = tcp->u_error;
2645 if (tcp->qual_flg & QUAL_RAW) {
2647 tprintf("= -1 (errno %ld)", u_error);
2649 tprintf("= %#lx", tcp->u_rval);
2651 else if (!(sys_res & RVAL_NONE) && u_error) {
2653 /* Blocked signals do not interrupt any syscalls.
2654 * In this case syscalls don't return ERESTARTfoo codes.
2656 * Deadly signals set to SIG_DFL interrupt syscalls
2657 * and kill the process regardless of which of the codes below
2658 * is returned by the interrupted syscall.
2659 * In some cases, kernel forces a kernel-generated deadly
2660 * signal to be unblocked and set to SIG_DFL (and thus cause
2661 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2662 * or SIGILL. (The alternative is to leave process spinning
2663 * forever on the faulty instruction - not useful).
2665 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2666 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2667 * but kernel will always restart them.
2670 /* Most common type of signal-interrupted syscall exit code.
2671 * The system call will be restarted with the same arguments
2672 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2674 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
2676 case ERESTARTNOINTR:
2677 /* Rare. For example, fork() returns this if interrupted.
2678 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2680 tprints("= ? ERESTARTNOINTR (To be restarted)");
2682 case ERESTARTNOHAND:
2683 /* pause(), rt_sigsuspend() etc use this code.
2684 * SA_RESTART is ignored (assumed not set):
2685 * syscall won't restart (will return EINTR instead)
2686 * even after signal with SA_RESTART set. However,
2687 * after SIG_IGN or SIG_DFL signal it will restart
2688 * (thus the name "restart only if has no handler").
2690 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
2692 case ERESTART_RESTARTBLOCK:
2693 /* Syscalls like nanosleep(), poll() which can't be
2694 * restarted with their original arguments use this
2695 * code. Kernel will execute restart_syscall() instead,
2696 * which changes arguments before restarting syscall.
2697 * SA_RESTART is ignored (assumed not set) similarly
2698 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2699 * since restart data is saved in "restart block"
2700 * in task struct, and if signal handler uses a syscall
2701 * which in turn saves another such restart block,
2702 * old data is lost and restart becomes impossible)
2704 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
2708 tprintf("= -1 E??? (errno %ld)", u_error);
2709 else if ((unsigned long) u_error < nerrnos)
2710 tprintf("= -1 %s (%s)", errnoent[u_error],
2713 tprintf("= -1 ERRNO_%ld (%s)", u_error,
2717 if ((sys_res & RVAL_STR) && tcp->auxstr)
2718 tprintf(" (%s)", tcp->auxstr);
2721 if (sys_res & RVAL_NONE)
2724 switch (sys_res & RVAL_MASK) {
2726 tprintf("= %#lx", tcp->u_rval);
2729 tprintf("= %#lo", tcp->u_rval);
2732 tprintf("= %lu", tcp->u_rval);
2735 tprintf("= %ld", tcp->u_rval);
2740 printfd(tcp, tcp->u_rval);
2743 tprintf("= %ld", tcp->u_rval);
2745 #if defined(LINUX_MIPSN32) || defined(X32)
2748 tprintf("= %#llx", tcp->u_lrval);
2751 tprintf("= %#llo", tcp->u_lrval);
2754 case RVAL_LUDECIMAL:
2755 tprintf("= %llu", tcp->u_lrval);
2759 tprintf("= %lld", tcp->u_lrval);
2765 "invalid rval format\n");
2769 if ((sys_res & RVAL_STR) && tcp->auxstr)
2770 tprintf(" (%s)", tcp->auxstr);
2773 tv_sub(&tv, &tv, &tcp->etime);
2774 tprintf(" <%ld.%06ld>",
2775 (long) tv.tv_sec, (long) tv.tv_usec);
2781 #ifdef USE_LIBUNWIND
2782 if (stack_trace_enabled)
2783 unwind_print_stacktrace(tcp);
2787 tcp->flags &= ~TCB_INSYSCALL;
2792 trace_syscall(struct tcb *tcp)
2794 return exiting(tcp) ?
2795 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);