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>
42 #ifdef HAVE_LINUX_PTRACE_H
43 # undef PTRACE_SYSCALL
44 # ifdef HAVE_STRUCT_IA64_FPREG
45 # define ia64_fpreg XXX_ia64_fpreg
47 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
48 # define pt_all_user_regs XXX_pt_all_user_regs
50 # ifdef HAVE_STRUCT_PTRACE_PEEKSIGINFO_ARGS
51 # define ptrace_peeksiginfo_args XXX_ptrace_peeksiginfo_args
53 # include <linux/ptrace.h>
54 # undef ptrace_peeksiginfo_args
56 # undef pt_all_user_regs
60 # undef PTRACE_GETREGS
61 # define PTRACE_GETREGS PTRACE_GETREGS64
62 # undef PTRACE_SETREGS
63 # define PTRACE_SETREGS PTRACE_SETREGS64
67 # include <asm/ptrace_offsets.h>
71 /* for struct iovec */
79 # include <asm/ptrace.h>
83 # include <asm/ptrace.h>
87 # warning: NSIG is not defined, using 32
93 /* Define these shorthand notations to simplify the syscallent files. */
97 #define TN TRACE_NETWORK
98 #define TP TRACE_PROCESS
99 #define TS TRACE_SIGNAL
100 #define TM TRACE_MEMORY
101 #define NF SYSCALL_NEVER_FAILS
103 #define SI STACKTRACE_INVALIDATE_CACHE
104 #define SE STACKTRACE_CAPTURE_ON_ENTER
106 const struct_sysent sysent0[] = {
107 #include "syscallent.h"
110 #if SUPPORTED_PERSONALITIES > 1
111 static const struct_sysent sysent1[] = {
112 # include "syscallent1.h"
116 #if SUPPORTED_PERSONALITIES > 2
117 static const struct_sysent sysent2[] = {
118 # include "syscallent2.h"
122 /* Now undef them since short defines cause wicked namespace pollution. */
136 * `ioctlent[012].h' files are automatically generated by the auxiliary
137 * program `ioctlsort', such that the list is sorted by the `code' field.
138 * This has the side-effect of resolving the _IO.. macros into
139 * plain integers, eliminating the need to include here everything
143 const char *const errnoent0[] = {
144 #include "errnoent.h"
146 const char *const signalent0[] = {
147 #include "signalent.h"
149 const struct_ioctlent ioctlent0[] = {
150 #include "ioctlent0.h"
153 #if SUPPORTED_PERSONALITIES > 1
154 static const char *const errnoent1[] = {
155 # include "errnoent1.h"
157 static const char *const signalent1[] = {
158 # include "signalent1.h"
160 static const struct_ioctlent ioctlent1[] = {
161 # include "ioctlent1.h"
165 #if SUPPORTED_PERSONALITIES > 2
166 static const char *const errnoent2[] = {
167 # include "errnoent2.h"
169 static const char *const signalent2[] = {
170 # include "signalent2.h"
172 static const struct_ioctlent ioctlent2[] = {
173 # include "ioctlent2.h"
178 nsyscalls0 = ARRAY_SIZE(sysent0)
179 #if SUPPORTED_PERSONALITIES > 1
180 , nsyscalls1 = ARRAY_SIZE(sysent1)
181 # if SUPPORTED_PERSONALITIES > 2
182 , nsyscalls2 = ARRAY_SIZE(sysent2)
188 nerrnos0 = ARRAY_SIZE(errnoent0)
189 #if SUPPORTED_PERSONALITIES > 1
190 , nerrnos1 = ARRAY_SIZE(errnoent1)
191 # if SUPPORTED_PERSONALITIES > 2
192 , nerrnos2 = ARRAY_SIZE(errnoent2)
198 nsignals0 = ARRAY_SIZE(signalent0)
199 #if SUPPORTED_PERSONALITIES > 1
200 , nsignals1 = ARRAY_SIZE(signalent1)
201 # if SUPPORTED_PERSONALITIES > 2
202 , nsignals2 = ARRAY_SIZE(signalent2)
208 nioctlents0 = ARRAY_SIZE(ioctlent0)
209 #if SUPPORTED_PERSONALITIES > 1
210 , nioctlents1 = ARRAY_SIZE(ioctlent1)
211 # if SUPPORTED_PERSONALITIES > 2
212 , nioctlents2 = ARRAY_SIZE(ioctlent2)
217 #if SUPPORTED_PERSONALITIES > 1
218 const struct_sysent *sysent = sysent0;
219 const char *const *errnoent = errnoent0;
220 const char *const *signalent = signalent0;
221 const struct_ioctlent *ioctlent = ioctlent0;
223 unsigned nsyscalls = nsyscalls0;
224 unsigned nerrnos = nerrnos0;
225 unsigned nsignals = nsignals0;
226 unsigned nioctlents = nioctlents0;
229 qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
231 static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
233 #if SUPPORTED_PERSONALITIES > 1
236 #if SUPPORTED_PERSONALITIES > 2
240 static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
242 #if SUPPORTED_PERSONALITIES > 1
245 #if SUPPORTED_PERSONALITIES > 2
251 MAX_NSYSCALLS1 = (nsyscalls0
252 #if SUPPORTED_PERSONALITIES > 1
253 > nsyscalls1 ? nsyscalls0 : nsyscalls1
256 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
257 #if SUPPORTED_PERSONALITIES > 2
258 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
261 MAX_NSYSCALLS = MAX_NSYSCALLS2,
262 /* We are ready for arches with up to 255 signals,
263 * even though the largest known signo is on MIPS and it is 128.
264 * The number of existing syscalls on all arches is
265 * larger that 255 anyway, so it is just a pedantic matter.
267 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
270 #if SUPPORTED_PERSONALITIES > 1
271 unsigned current_personality;
273 # ifndef current_wordsize
274 unsigned current_wordsize;
275 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
276 PERSONALITY0_WORDSIZE,
277 PERSONALITY1_WORDSIZE,
278 # if SUPPORTED_PERSONALITIES > 2
279 PERSONALITY2_WORDSIZE,
285 set_personality(int personality)
287 nsyscalls = nsyscall_vec[personality];
288 sysent = sysent_vec[personality];
290 switch (personality) {
292 errnoent = errnoent0;
294 ioctlent = ioctlent0;
295 nioctlents = nioctlents0;
296 signalent = signalent0;
297 nsignals = nsignals0;
301 errnoent = errnoent1;
303 ioctlent = ioctlent1;
304 nioctlents = nioctlents1;
305 signalent = signalent1;
306 nsignals = nsignals1;
309 # if SUPPORTED_PERSONALITIES > 2
311 errnoent = errnoent2;
313 ioctlent = ioctlent2;
314 nioctlents = nioctlents2;
315 signalent = signalent2;
316 nsignals = nsignals2;
321 current_personality = personality;
322 # ifndef current_wordsize
323 current_wordsize = personality_wordsize[personality];
328 update_personality(struct tcb *tcp, unsigned int personality)
330 if (personality == current_personality)
332 set_personality(personality);
334 if (personality == tcp->currpers)
336 tcp->currpers = personality;
338 # if defined(POWERPC64)
340 static const char *const names[] = {"64 bit", "32 bit"};
341 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
342 tcp->pid, names[personality]);
344 # elif defined(X86_64)
346 static const char *const names[] = {"64 bit", "32 bit", "x32"};
347 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
348 tcp->pid, names[personality]);
352 static const char *const names[] = {"x32", "32 bit"};
353 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
354 tcp->pid, names[personality]);
356 # elif defined(AARCH64)
358 static const char *const names[] = {"32-bit", "AArch64"};
359 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
360 tcp->pid, names[personality]);
364 static const char *const names[] = {"64-bit", "32-bit"};
365 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
366 tcp->pid, names[personality]);
372 static int qual_syscall(), qual_signal(), qual_desc();
374 static const struct qual_options {
375 unsigned int bitflag;
376 const char *option_name;
377 int (*qualify)(const char *, int, int);
378 const char *argument_name;
380 { QUAL_TRACE, "trace", qual_syscall, "system call" },
381 { QUAL_TRACE, "t", qual_syscall, "system call" },
382 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
383 { QUAL_ABBREV, "a", qual_syscall, "system call" },
384 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
385 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
386 { QUAL_RAW, "raw", qual_syscall, "system call" },
387 { QUAL_RAW, "x", qual_syscall, "system call" },
388 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
389 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
390 { QUAL_SIGNAL, "s", qual_signal, "signal" },
391 { QUAL_READ, "read", qual_desc, "descriptor" },
392 { QUAL_READ, "reads", qual_desc, "descriptor" },
393 { QUAL_READ, "r", qual_desc, "descriptor" },
394 { QUAL_WRITE, "write", qual_desc, "descriptor" },
395 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
396 { QUAL_WRITE, "w", qual_desc, "descriptor" },
397 { 0, NULL, NULL, NULL },
401 reallocate_qual(const unsigned int n)
405 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
406 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
409 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
415 qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
420 reallocate_qual(n + 1);
422 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
423 if (pers == p || pers < 0) {
425 qual_vec[p][n] &= ~bitflag;
427 qual_vec[p][n] |= bitflag;
433 qual_syscall(const char *s, const unsigned int bitflag, const int not)
439 if (*s >= '0' && *s <= '9') {
440 i = string_to_uint(s);
441 if (i >= MAX_NSYSCALLS)
443 qualify_one(i, bitflag, not, -1);
447 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
448 for (i = 0; i < nsyscall_vec[p]; i++) {
449 if (sysent_vec[p][i].sys_name
450 && strcmp(s, sysent_vec[p][i].sys_name) == 0
452 qualify_one(i, bitflag, not, p);
462 qual_signal(const char *s, const unsigned int bitflag, const int not)
466 if (*s >= '0' && *s <= '9') {
467 int signo = string_to_uint(s);
468 if (signo < 0 || signo > 255)
470 qualify_one(signo, bitflag, not, -1);
473 if (strncasecmp(s, "SIG", 3) == 0)
475 for (i = 0; i <= NSIG; i++) {
476 if (strcasecmp(s, signame(i) + 3) == 0) {
477 qualify_one(i, bitflag, not, -1);
485 qual_desc(const char *s, const unsigned int bitflag, const int not)
487 if (*s >= '0' && *s <= '9') {
488 int desc = string_to_uint(s);
489 if (desc < 0 || desc > 0x7fff) /* paranoia */
491 qualify_one(desc, bitflag, not, -1);
498 lookup_class(const char *s)
500 if (strcmp(s, "file") == 0)
502 if (strcmp(s, "ipc") == 0)
504 if (strcmp(s, "network") == 0)
505 return TRACE_NETWORK;
506 if (strcmp(s, "process") == 0)
507 return TRACE_PROCESS;
508 if (strcmp(s, "signal") == 0)
510 if (strcmp(s, "desc") == 0)
512 if (strcmp(s, "memory") == 0)
518 qualify(const char *s)
520 const struct qual_options *opt;
527 reallocate_qual(MIN_QUALS);
529 opt = &qual_options[0];
530 for (i = 0; (p = qual_options[i].option_name); i++) {
531 unsigned int len = strlen(p);
532 if (strncmp(s, p, len) == 0 && s[len] == '=') {
533 opt = &qual_options[i];
543 if (strcmp(s, "none") == 0) {
547 if (strcmp(s, "all") == 0) {
548 for (i = 0; i < num_quals; i++) {
549 qualify_one(i, opt->bitflag, not, -1);
553 for (i = 0; i < num_quals; i++) {
554 qualify_one(i, opt->bitflag, !not, -1);
559 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
561 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
563 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
564 for (i = 0; i < nsyscall_vec[pers]; i++)
565 if (sysent_vec[pers][i].sys_flags & n)
566 qualify_one(i, opt->bitflag, not, pers);
570 if (opt->qualify(p, opt->bitflag, not)) {
571 error_msg_and_die("invalid %s '%s'",
572 opt->argument_name, p);
579 #ifdef SYS_socket_subcall
581 decode_socket_subcall(struct tcb *tcp)
584 unsigned int i, n, size;
586 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
589 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
590 tcp->qual_flg = qual_flags[tcp->scno];
591 tcp->s_ent = &sysent[tcp->scno];
592 addr = tcp->u_arg[1];
593 size = current_wordsize;
594 n = tcp->s_ent->nargs;
595 for (i = 0; i < n; ++i) {
596 if (size == sizeof(int)) {
598 if (umove(tcp, addr, &arg) < 0)
604 if (umove(tcp, addr, &arg) < 0)
613 #ifdef SYS_ipc_subcall
615 decode_ipc_subcall(struct tcb *tcp)
619 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
622 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
623 tcp->qual_flg = qual_flags[tcp->scno];
624 tcp->s_ent = &sysent[tcp->scno];
625 n = tcp->s_ent->nargs;
626 for (i = 0; i < n; i++)
627 tcp->u_arg[i] = tcp->u_arg[i + 1];
632 printargs(struct tcb *tcp)
636 int n = tcp->s_ent->nargs;
637 for (i = 0; i < n; i++)
638 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
644 printargs_lu(struct tcb *tcp)
648 int n = tcp->s_ent->nargs;
649 for (i = 0; i < n; i++)
650 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
656 printargs_ld(struct tcb *tcp)
660 int n = tcp->s_ent->nargs;
661 for (i = 0; i < n; i++)
662 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
667 #if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
669 getrval2(struct tcb *tcp)
673 # if defined(SPARC) || defined(SPARC64)
674 val = sparc_regs.u_regs[U_REG_O1];
676 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
679 if (upeek(tcp->pid, PT_R9, &val) < 0)
688 static struct user_regs_struct i386_regs;
689 /* Cast suppresses signedness warning (.esp is long, not unsigned long) */
690 uint32_t *const i386_esp_ptr = (uint32_t*)&i386_regs.esp;
691 # define ARCH_REGS_FOR_GETREGSET i386_regs
692 #elif defined(X86_64) || defined(X32)
694 * On i386, pt_regs and user_regs_struct are the same,
695 * but on 64 bit x86, user_regs_struct has six more fields:
696 * fs_base, gs_base, ds, es, fs, gs.
697 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
699 struct i386_user_regs_struct {
719 struct user_regs_struct x86_64_r;
720 struct i386_user_regs_struct i386_r;
722 # define x86_64_regs x86_regs_union.x86_64_r
723 # define i386_regs x86_regs_union.i386_r
724 uint32_t *const i386_esp_ptr = &i386_regs.esp;
725 static struct iovec x86_io = {
726 .iov_base = &x86_regs_union
729 bool ia64_ia32mode = 0; /* not static */
730 static long ia64_r8, ia64_r10;
731 #elif defined(POWERPC)
732 struct pt_regs ppc_regs;
738 struct pt_regs arm_regs; /* not static */
739 # define ARCH_REGS_FOR_GETREGSET arm_regs
740 #elif defined(AARCH64)
742 struct user_pt_regs aarch64_r;
743 struct arm_pt_regs arm_r;
745 # define aarch64_regs arm_regs_union.aarch64_r
746 # define arm_regs arm_regs_union.arm_r
747 static struct iovec aarch64_io = {
748 .iov_base = &arm_regs_union
751 static long alpha_r0;
752 static long alpha_a3;
754 static struct pt_regs avr32_regs;
755 #elif defined(SPARC) || defined(SPARC64)
756 struct pt_regs sparc_regs; /* not static */
757 #elif defined(LINUX_MIPSN32)
758 static long long mips_a3;
759 static long long mips_r2;
763 #elif defined(S390) || defined(S390X)
764 static long s390_gpr2;
766 static long hppa_r28;
771 #elif defined(CRISV10) || defined(CRISV32)
772 static long cris_r10;
774 struct pt_regs tile_regs;
775 #elif defined(MICROBLAZE)
776 static long microblaze_r3;
778 static struct user_regs_struct or1k_regs;
779 # define ARCH_REGS_FOR_GETREGSET or1k_regs
781 static struct user_gp_regs metag_regs;
782 # define ARCH_REGS_FOR_GETREGSET metag_regs
783 #elif defined(XTENSA)
784 static long xtensa_a2;
786 static struct user_regs_struct arc_regs;
787 # define ARCH_REGS_FOR_GETREGSET arc_regs
791 print_pc(struct tcb *tcp)
793 #define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
794 sizeof(long) == 8 ? "[????????????????] " : \
796 if (get_regs_error) {
801 tprintf("[%08lx] ", i386_regs.eip);
802 #elif defined(S390) || defined(S390X)
804 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0) {
809 tprintf("[%08lx] ", psw);
811 tprintf("[%016lx] ", psw);
813 #elif defined(X86_64) || defined(X32)
814 if (x86_io.iov_len == sizeof(i386_regs)) {
815 tprintf("[%08x] ", (unsigned) i386_regs.eip);
818 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
820 /* Note: this truncates 64-bit rip to 32 bits */
821 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
826 if (upeek(tcp->pid, PT_B0, &ip) < 0) {
830 tprintf("[%08lx] ", ip);
831 #elif defined(POWERPC)
832 long pc = ppc_regs.nip;
834 tprintf("[%016lx] ", pc);
836 tprintf("[%08lx] ", pc);
840 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
841 tprints("[????????] ");
844 tprintf("[%08lx] ", pc);
847 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
848 tprints("[????????????????] ");
851 tprintf("[%08lx] ", pc);
853 tprintf("[%08lx] ", sparc_regs.pc);
854 #elif defined(SPARC64)
855 tprintf("[%08lx] ", sparc_regs.tpc);
858 if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
859 tprints("[????????] ");
862 tprintf("[%08lx] ", pc);
865 if (upeek(tcp->pid, REG_EPC, &pc) < 0) {
866 tprints("[????????] ");
869 tprintf("[%08lx] ", pc);
872 if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
873 tprints("[????????] ");
876 tprintf("[%08lx] ", pc);
879 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
880 tprints("[????????????????] ");
883 tprintf("[%08lx] ", pc);
885 tprintf("[%08lx] ", arm_regs.ARM_pc);
886 #elif defined(AARCH64)
887 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
889 tprintf("[%08lx] ", avr32_regs.pc);
892 if (upeek(tcp->pid, PT_PC, &pc) < 0) {
896 tprintf("[%08lx] ", pc);
897 #elif defined(CRISV10)
899 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
903 tprintf("[%08lx] ", pc);
904 #elif defined(CRISV32)
906 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
910 tprintf("[%08lx] ", pc);
913 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
915 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
918 tprintf("[%08lx] ", or1k_regs.pc);
920 tprintf("[%08lx] ", metag_regs.pc);
921 #elif defined(XTENSA)
923 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
927 tprintf("[%08lx] ", pc);
929 tprintf("[%08lx] ", arc_regs.efa);
930 #endif /* architecture */
934 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
935 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
937 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
939 shuffle_scno(unsigned long scno)
941 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
944 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
945 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
947 if (scno == 0x000ffff0)
948 return ARM_FIRST_SHUFFLED_SYSCALL;
950 #define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
952 * Is it ARM specific syscall?
953 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
954 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
956 if (scno >= 0x000f0000 &&
957 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
958 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
960 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
961 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
967 # define shuffle_scno(scno) ((long)(scno))
971 undefined_scno_name(struct tcb *tcp)
973 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
975 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
981 * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
982 * we provide a slow fallback for old kernels.
984 static int powerpc_getregs_old(pid_t pid)
990 r = upeek(pid, sizeof(long) * PT_NIP, (long *)&ppc_regs.nip);
994 #ifdef POWERPC64 /* else we never use it */
995 r = upeek(pid, sizeof(long) * PT_MSR, (long *)&ppc_regs.msr);
999 r = upeek(pid, sizeof(long) * PT_CCR, (long *)&ppc_regs.ccr);
1002 r = upeek(pid, sizeof(long) * PT_ORIG_R3, (long *)&ppc_regs.orig_gpr3);
1005 for (i = 0; i <= 8; i++) {
1006 r = upeek(pid, sizeof(long) * (PT_R0 + i),
1007 (long *)&ppc_regs.gpr[i]);
1017 long get_regs_error;
1019 #if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1020 static void get_regset(pid_t pid)
1022 /* constant iovec */
1028 static struct iovec io = {
1029 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1030 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1032 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1034 /* variable iovec */
1035 # elif defined(X86_64) || defined(X32)
1036 /* x86_io.iov_base = &x86_regs_union; - already is */
1037 x86_io.iov_len = sizeof(x86_regs_union);
1038 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &x86_io);
1039 # elif defined(AARCH64)
1040 /* aarch64_io.iov_base = &arm_regs_union; - already is */
1041 aarch64_io.iov_len = sizeof(arm_regs_union);
1042 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &aarch64_io);
1044 # warning both PTRACE_GETREGSET and NT_PRSTATUS are available but not yet used
1047 #endif /* PTRACE_GETREGSET && NT_PRSTATUS */
1052 /* PTRACE_GETREGSET only */
1053 # if defined(METAG) || defined(OR1K) || defined(X32) || defined(AARCH64) || defined(ARC)
1056 /* PTRACE_GETREGS only */
1057 # elif defined(AVR32)
1058 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
1059 # elif defined(TILE)
1060 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &tile_regs);
1061 # elif defined(SPARC) || defined(SPARC64)
1062 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
1063 # elif defined(POWERPC)
1064 static bool old_kernel = 0;
1067 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &ppc_regs);
1068 if (get_regs_error && errno == EIO) {
1071 get_regs_error = powerpc_getregs_old(pid);
1074 /* try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS */
1076 # if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1077 static int getregset_support;
1079 if (getregset_support >= 0) {
1081 if (getregset_support > 0)
1083 if (get_regs_error >= 0) {
1084 getregset_support = 1;
1087 if (errno == EPERM || errno == ESRCH)
1089 getregset_support = -1;
1091 # endif /* PTRACE_GETREGSET && NT_PRSTATUS */
1093 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &arm_regs);
1094 # elif defined(I386)
1095 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &i386_regs);
1096 # elif defined(X86_64)
1097 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1098 x86_io.iov_len = sizeof(x86_64_regs);
1099 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
1100 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1101 x86_io.iov_len = sizeof(i386_regs);
1103 * The order is important: i386_regs and x86_64_regs
1104 * are overlaid in memory!
1106 i386_regs.ebx = x86_64_regs.rbx;
1107 i386_regs.ecx = x86_64_regs.rcx;
1108 i386_regs.edx = x86_64_regs.rdx;
1109 i386_regs.esi = x86_64_regs.rsi;
1110 i386_regs.edi = x86_64_regs.rdi;
1111 i386_regs.ebp = x86_64_regs.rbp;
1112 i386_regs.eax = x86_64_regs.rax;
1113 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1114 /* i386_regs.xes = x86_64_regs.es; ditto... */
1115 /* i386_regs.xfs = x86_64_regs.fs; */
1116 /* i386_regs.xgs = x86_64_regs.gs; */
1117 i386_regs.orig_eax = x86_64_regs.orig_rax;
1118 i386_regs.eip = x86_64_regs.rip;
1119 /* i386_regs.xcs = x86_64_regs.cs; */
1120 /* i386_regs.eflags = x86_64_regs.eflags; */
1121 i386_regs.esp = x86_64_regs.rsp;
1122 /* i386_regs.xss = x86_64_regs.ss; */
1125 # error unhandled architecture
1126 # endif /* ARM || I386 || X86_64 */
1129 #endif /* !get_regs */
1132 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1133 * 1: ok, continue in trace_syscall_entering().
1134 * other: error, trace_syscall_entering() should print error indicator
1135 * ("????" etc) and bail out.
1138 get_scno(struct tcb *tcp)
1142 #if defined(S390) || defined(S390X)
1143 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
1146 if (s390_gpr2 != -ENOSYS) {
1148 * Since kernel version 2.5.44 the scno gets passed in gpr2.
1153 * Old style of "passing" the scno via the SVC instruction.
1156 long opcode, offset_reg, tmp;
1158 static const int gpr_offset[16] = {
1159 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1160 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1161 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1162 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1165 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0)
1168 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
1170 perror_msg("peektext(psw-oneword)");
1175 * We have to check if the SVC got executed directly or via an
1176 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1177 * instruction decoding to derive the system call number.
1178 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1179 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1180 * opcode. Since there is no way to find out the opcode size this
1181 * is the best we can do...
1183 if ((opcode & 0xff00) == 0x0a00) {
1185 scno = opcode & 0xff;
1188 /* SVC got executed by EXECUTE instruction */
1191 * Do instruction decoding of EXECUTE. If you really want to
1192 * understand this, read the Principles of Operations.
1194 svc_addr = (void *) (opcode & 0xfff);
1197 offset_reg = (opcode & 0x000f0000) >> 16;
1198 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
1203 offset_reg = (opcode & 0x0000f000) >> 12;
1204 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
1208 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
1217 offset_reg = (opcode & 0x00f00000) >> 20;
1218 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
1221 scno = (scno | tmp) & 0xff;
1224 #elif defined(POWERPC)
1225 scno = ppc_regs.gpr[0];
1227 unsigned int currpers;
1230 * Check for 64/32 bit mode.
1231 * Embedded implementations covered by Book E extension of PPC use
1232 * bit 0 (CM) of 32-bit Machine state register (MSR).
1233 * Other implementations use bit 0 (SF) of 64-bit MSR.
1235 currpers = (ppc_regs.msr & 0x8000000080000000) ? 0 : 1;
1236 update_personality(tcp, currpers);
1238 #elif defined(AVR32)
1239 scno = avr32_regs.r8;
1241 if (upeek(tcp->pid, PT_ORIG_P0, &scno))
1244 scno = i386_regs.orig_eax;
1245 #elif defined(X86_64) || defined(X32)
1246 # ifndef __X32_SYSCALL_BIT
1247 # define __X32_SYSCALL_BIT 0x40000000
1249 unsigned int currpers;
1251 /* GETREGSET of NT_PRSTATUS tells us regset size,
1252 * which unambiguously detects i386.
1254 * Linux kernel distinguishes x86-64 and x32 processes
1255 * solely by looking at __X32_SYSCALL_BIT:
1256 * arch/x86/include/asm/compat.h::is_x32_task():
1257 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1260 if (x86_io.iov_len == sizeof(i386_regs)) {
1261 scno = i386_regs.orig_eax;
1264 scno = x86_64_regs.orig_rax;
1266 if (scno & __X32_SYSCALL_BIT) {
1268 * Syscall number -1 requires special treatment:
1269 * it might be a side effect of SECCOMP_RET_ERRNO
1270 * filtering that sets orig_rax to -1
1271 * in some versions of linux kernel.
1272 * If that is the case, then
1273 * __X32_SYSCALL_BIT logic does not apply.
1275 if ((long long) x86_64_regs.orig_rax != -1) {
1276 scno -= __X32_SYSCALL_BIT;
1286 /* cs = 0x33 for long mode (native 64 bit and x32)
1287 * cs = 0x23 for compatibility mode (32 bit)
1288 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1290 scno = x86_64_regs.orig_rax;
1291 switch (x86_64_regs.cs) {
1292 case 0x23: currpers = 1; break;
1294 if (x86_64_regs.ds == 0x2b) {
1296 scno &= ~__X32_SYSCALL_BIT;
1301 fprintf(stderr, "Unknown value CS=0x%08X while "
1302 "detecting personality of process "
1303 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
1304 currpers = current_personality;
1308 /* This version analyzes the opcode of a syscall instruction.
1309 * (int 0x80 on i386 vs. syscall on x86-64)
1310 * It works, but is too complicated, and strictly speaking, unreliable.
1312 unsigned long call, rip = x86_64_regs.rip;
1313 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1316 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
1318 fprintf(stderr, "ptrace_peektext failed: %s\n",
1320 switch (call & 0xffff) {
1321 /* x86-64: syscall = 0x0f 0x05 */
1322 case 0x050f: currpers = 0; break;
1323 /* i386: int 0x80 = 0xcd 0x80 */
1324 case 0x80cd: currpers = 1; break;
1326 currpers = current_personality;
1328 "Unknown syscall opcode (0x%04X) while "
1329 "detecting personality of process "
1330 "PID=%d\n", (int)call, tcp->pid);
1336 /* If we are built for a x32 system, then personality 0 is x32
1337 * (not x86_64), and stracing of x86_64 apps is not supported.
1338 * Stracing of i386 apps is still supported.
1340 if (currpers == 0) {
1341 fprintf(stderr, "syscall_%lu(...) in unsupported "
1342 "64-bit mode of process PID=%d\n",
1346 currpers &= ~2; /* map 2,1 to 0,1 */
1348 update_personality(tcp, currpers);
1350 # define IA64_PSR_IS ((long)1 << 34)
1352 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
1353 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
1354 if (ia64_ia32mode) {
1355 if (upeek(tcp->pid, PT_R1, &scno) < 0)
1358 if (upeek(tcp->pid, PT_R15, &scno) < 0)
1361 #elif defined(AARCH64)
1362 switch (aarch64_io.iov_len) {
1363 case sizeof(aarch64_regs):
1364 /* We are in 64-bit mode */
1365 scno = aarch64_regs.regs[8];
1366 update_personality(tcp, 1);
1368 case sizeof(arm_regs):
1369 /* We are in 32-bit mode */
1370 /* Note: we don't support OABI, unlike 32-bit ARM build */
1371 scno = arm_regs.ARM_r7;
1372 scno = shuffle_scno(scno);
1373 update_personality(tcp, 0);
1377 if (arm_regs.ARM_ip != 0) {
1378 /* It is not a syscall entry */
1379 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1380 tcp->flags |= TCB_INSYSCALL;
1383 /* Note: we support only 32-bit CPUs, not 26-bit */
1385 # if !defined(__ARM_EABI__) || ENABLE_ARM_OABI
1386 if (arm_regs.ARM_cpsr & 0x20)
1390 /* Check EABI/OABI by examining SVC insn's low 24 bits */
1392 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1395 /* EABI syscall convention? */
1396 if ((unsigned long) scno != 0xef000000) {
1398 if ((scno & 0x0ff00000) != 0x0f900000) {
1399 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1403 /* Fixup the syscall number */
1407 scno = arm_regs.ARM_r7;
1409 # else /* __ARM_EABI__ || !ENABLE_ARM_OABI */
1410 scno = arm_regs.ARM_r7;
1412 scno = shuffle_scno(scno);
1414 if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
1416 #elif defined(LINUX_MIPSN32)
1417 unsigned long long regs[38];
1419 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1421 mips_a3 = regs[REG_A3];
1422 mips_r2 = regs[REG_V0];
1425 if (!SCNO_IN_RANGE(scno)) {
1426 if (mips_a3 == 0 || mips_a3 == -1) {
1428 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1433 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
1435 if (upeek(tcp->pid, REG_V0, &scno) < 0)
1438 if (!SCNO_IN_RANGE(scno)) {
1439 if (mips_a3 == 0 || mips_a3 == -1) {
1441 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1445 #elif defined(ALPHA)
1446 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
1448 if (upeek(tcp->pid, REG_R0, &scno) < 0)
1452 * Do some sanity checks to figure out if it's
1453 * really a syscall entry
1455 if (!SCNO_IN_RANGE(scno)) {
1456 if (alpha_a3 == 0 || alpha_a3 == -1) {
1458 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
1462 #elif defined(SPARC) || defined(SPARC64)
1463 /* Disassemble the syscall trap. */
1464 /* Retrieve the syscall trap instruction. */
1467 # if defined(SPARC64)
1468 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
1471 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
1476 /* Disassemble the trap to see what personality to use. */
1479 /* Linux/SPARC syscall trap. */
1480 update_personality(tcp, 0);
1483 /* Linux/SPARC64 syscall trap. */
1484 update_personality(tcp, 2);
1487 /* SunOS syscall trap. (pers 1) */
1488 fprintf(stderr, "syscall: SunOS no support\n");
1491 /* Solaris 2.x syscall trap. (per 2) */
1492 update_personality(tcp, 1);
1495 /* NetBSD/FreeBSD syscall trap. */
1496 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1499 /* Solaris 2.x gettimeofday */
1500 update_personality(tcp, 1);
1503 # if defined(SPARC64)
1504 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
1506 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
1511 /* Extract the system call number from the registers. */
1512 if (trap == 0x91d02027)
1515 scno = sparc_regs.u_regs[U_REG_G1];
1517 scno = sparc_regs.u_regs[U_REG_O0];
1518 memmove(&sparc_regs.u_regs[U_REG_O0], &sparc_regs.u_regs[U_REG_O1], 7*sizeof(sparc_regs.u_regs[0]));
1521 if (upeek(tcp->pid, PT_GR20, &scno) < 0)
1525 * In the new syscall ABI, the system call number is in R3.
1527 if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
1531 /* Odd as it may seem, a glibc bug has been known to cause
1532 glibc to issue bogus negative syscall numbers. So for
1533 our purposes, make strace print what it *should* have been */
1534 long correct_scno = (scno & 0xff);
1537 "Detected glibc bug: bogus system call"
1538 " number = %ld, correcting to %ld\n",
1541 scno = correct_scno;
1544 if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
1547 #elif defined(CRISV10) || defined(CRISV32)
1548 if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
1551 unsigned int currpers;
1552 scno = tile_regs.regs[10];
1556 # ifndef PT_FLAGS_COMPAT
1557 # define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1559 if (tile_regs.flags & PT_FLAGS_COMPAT)
1564 update_personality(tcp, currpers);
1565 #elif defined(MICROBLAZE)
1566 if (upeek(tcp->pid, 0, &scno) < 0)
1569 scno = or1k_regs.gpr[11];
1570 #elif defined(METAG)
1571 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
1572 #elif defined(XTENSA)
1573 if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
1576 scno = arc_regs.scratch.r8;
1580 if (SCNO_IS_VALID(tcp->scno)) {
1581 tcp->s_ent = &sysent[scno];
1582 tcp->qual_flg = qual_flags[scno];
1584 static const struct_sysent unknown = {
1587 .sys_func = printargs,
1588 .sys_name = "unknown", /* not used */
1590 tcp->s_ent = &unknown;
1591 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1596 /* Called at each syscall entry.
1598 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1599 * 1: ok, continue in trace_syscall_entering().
1600 * other: error, trace_syscall_entering() should print error indicator
1601 * ("????" etc) and bail out.
1604 syscall_fixup_on_sysenter(struct tcb *tcp)
1606 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
1609 * Syscall number -1 requires special treatment.
1610 * On X86, it might be a side effect of SECCOMP_RET_ERRNO filtering
1611 * that sets orig_eax to -1 in some versions of linux kernel.
1613 if (i386_regs.orig_eax != -1 &&
1614 i386_regs.eax != -ENOSYS) {
1617 "not a syscall entry (eax = %ld, orig_eax = %ld)\n",
1618 i386_regs.eax, i386_regs.orig_eax);
1621 #elif defined(X86_64) || defined(X32)
1623 * Syscall number -1 requires special treatment.
1624 * On X86_64/X32, it might be a side effect of SECCOMP_RET_ERRNO
1625 * filtering that sets orig_rax to -1 in some versions of linux kernel.
1627 if (x86_io.iov_len == sizeof(i386_regs)) {
1628 if ((int) i386_regs.orig_eax != -1 &&
1629 (int) i386_regs.eax != -ENOSYS) {
1632 "not a syscall entry (eax = %d, orig_eax = %d)\n",
1633 (int) i386_regs.eax,
1634 (int) i386_regs.orig_eax);
1638 if ((long long) x86_64_regs.orig_rax != -1 &&
1639 (long long) x86_64_regs.rax != -ENOSYS) {
1642 "not a syscall entry (rax = %lld, orig_rax = %lld)\n",
1643 (long long) x86_64_regs.rax,
1644 (long long) x86_64_regs.orig_rax);
1649 /* TODO? Eliminate upeek's in arches below like we did in x86 */
1650 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
1652 if (m68k_d0 != -ENOSYS) {
1654 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
1658 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
1660 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
1662 if (ia64_ia32mode && ia64_r8 != -ENOSYS) {
1664 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
1667 #elif defined(CRISV10) || defined(CRISV32)
1668 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
1670 if (cris_r10 != -ENOSYS) {
1672 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
1675 #elif defined(MICROBLAZE)
1676 if (upeek(tcp->pid, 3 * 4, µblaze_r3) < 0)
1678 if (microblaze_r3 != -ENOSYS) {
1680 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
1688 internal_fork(struct tcb *tcp)
1690 #if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1691 # define ARG_FLAGS 1
1693 # define ARG_FLAGS 0
1695 #ifndef CLONE_UNTRACED
1696 # define CLONE_UNTRACED 0x00800000
1698 if ((ptrace_setoptions
1699 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1700 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1706 if (entering(tcp)) {
1708 * We won't see the new child if clone is called with
1709 * CLONE_UNTRACED, so we keep the same logic with that option
1710 * and don't trace it.
1712 if ((tcp->s_ent->sys_func == sys_clone)
1713 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1718 if (tcp->flags & TCB_BPTSET)
1723 #if defined(TCB_WAITEXECVE)
1725 internal_exec(struct tcb *tcp)
1727 /* Maybe we have post-execve SIGTRAP suppressed? */
1728 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1729 return; /* yes, no need to do anything */
1731 if (exiting(tcp) && syserror(tcp))
1732 /* Error in execve, no post-execve SIGTRAP expected */
1733 tcp->flags &= ~TCB_WAITEXECVE;
1735 tcp->flags |= TCB_WAITEXECVE;
1740 syscall_fixup_for_fork_exec(struct tcb *tcp)
1743 * We must always trace a few critical system calls in order to
1744 * correctly support following forks in the presence of tracing
1749 func = tcp->s_ent->sys_func;
1751 if ( sys_fork == func
1752 || sys_clone == func
1758 #if defined(TCB_WAITEXECVE)
1759 if ( sys_execve == func
1760 # if defined(SPARC) || defined(SPARC64)
1761 || sys_execv == func
1770 /* Return -1 on error or 1 on success (never 0!) */
1772 get_syscall_args(struct tcb *tcp)
1776 nargs = tcp->s_ent->nargs;
1778 #if defined(S390) || defined(S390X)
1779 for (i = 0; i < nargs; ++i)
1780 if (upeek(tcp->pid, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1782 #elif defined(ALPHA)
1783 for (i = 0; i < nargs; ++i)
1784 if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1787 if (!ia64_ia32mode) {
1788 unsigned long *out0, cfm, sof, sol;
1790 /* be backwards compatible with kernel < 2.4.4... */
1792 # define PT_RBS_END PT_AR_BSP
1795 if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
1797 if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
1800 sof = (cfm >> 0) & 0x7f;
1801 sol = (cfm >> 7) & 0x7f;
1802 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1804 for (i = 0; i < nargs; ++i) {
1805 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1806 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1810 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1811 PT_R9 /* ECX = out1 */,
1812 PT_R10 /* EDX = out2 */,
1813 PT_R14 /* ESI = out3 */,
1814 PT_R15 /* EDI = out4 */,
1815 PT_R13 /* EBP = out5 */};
1817 for (i = 0; i < nargs; ++i) {
1818 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
1820 /* truncate away IVE sign-extension */
1821 tcp->u_arg[i] &= 0xffffffff;
1824 #elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
1825 /* N32 and N64 both use up to six registers. */
1826 unsigned long long regs[38];
1828 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1831 for (i = 0; i < nargs; ++i) {
1832 tcp->u_arg[i] = regs[REG_A0 + i];
1833 # if defined(LINUX_MIPSN32)
1834 tcp->ext_arg[i] = regs[REG_A0 + i];
1841 if (upeek(tcp->pid, REG_SP, &sp) < 0)
1843 for (i = 0; i < 4; ++i)
1844 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1846 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
1847 (char *)(tcp->u_arg + 4));
1849 for (i = 0; i < nargs; ++i)
1850 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1853 #elif defined(POWERPC)
1856 tcp->u_arg[0] = ppc_regs.orig_gpr3;
1857 tcp->u_arg[1] = ppc_regs.gpr[4];
1858 tcp->u_arg[2] = ppc_regs.gpr[5];
1859 tcp->u_arg[3] = ppc_regs.gpr[6];
1860 tcp->u_arg[4] = ppc_regs.gpr[7];
1861 tcp->u_arg[5] = ppc_regs.gpr[8];
1862 #elif defined(SPARC) || defined(SPARC64)
1863 for (i = 0; i < nargs; ++i)
1864 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
1866 for (i = 0; i < nargs; ++i)
1867 if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1869 #elif defined(ARM) || defined(AARCH64)
1870 # if defined(AARCH64)
1871 if (tcp->currpers == 1)
1872 for (i = 0; i < nargs; ++i)
1873 tcp->u_arg[i] = aarch64_regs.regs[i];
1876 for (i = 0; i < nargs; ++i)
1877 tcp->u_arg[i] = arm_regs.uregs[i];
1878 #elif defined(AVR32)
1881 tcp->u_arg[0] = avr32_regs.r12;
1882 tcp->u_arg[1] = avr32_regs.r11;
1883 tcp->u_arg[2] = avr32_regs.r10;
1884 tcp->u_arg[3] = avr32_regs.r9;
1885 tcp->u_arg[4] = avr32_regs.r5;
1886 tcp->u_arg[5] = avr32_regs.r3;
1888 static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
1890 for (i = 0; i < nargs; ++i)
1891 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
1894 static const int syscall_regs[MAX_ARGS] = {
1895 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1896 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
1899 for (i = 0; i < nargs; ++i)
1900 if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
1904 /* Registers used by SH5 Linux system calls for parameters */
1905 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
1907 for (i = 0; i < nargs; ++i)
1908 if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1913 tcp->u_arg[0] = i386_regs.ebx;
1914 tcp->u_arg[1] = i386_regs.ecx;
1915 tcp->u_arg[2] = i386_regs.edx;
1916 tcp->u_arg[3] = i386_regs.esi;
1917 tcp->u_arg[4] = i386_regs.edi;
1918 tcp->u_arg[5] = i386_regs.ebp;
1919 #elif defined(X86_64) || defined(X32)
1922 if (x86_io.iov_len != sizeof(i386_regs)) {
1923 /* x86-64 or x32 ABI */
1924 tcp->u_arg[0] = x86_64_regs.rdi;
1925 tcp->u_arg[1] = x86_64_regs.rsi;
1926 tcp->u_arg[2] = x86_64_regs.rdx;
1927 tcp->u_arg[3] = x86_64_regs.r10;
1928 tcp->u_arg[4] = x86_64_regs.r8;
1929 tcp->u_arg[5] = x86_64_regs.r9;
1931 tcp->ext_arg[0] = x86_64_regs.rdi;
1932 tcp->ext_arg[1] = x86_64_regs.rsi;
1933 tcp->ext_arg[2] = x86_64_regs.rdx;
1934 tcp->ext_arg[3] = x86_64_regs.r10;
1935 tcp->ext_arg[4] = x86_64_regs.r8;
1936 tcp->ext_arg[5] = x86_64_regs.r9;
1940 /* Zero-extend from 32 bits */
1941 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1942 * if you need to use *sign-extended* parameter.
1944 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1945 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1946 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1947 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1948 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1949 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
1951 #elif defined(MICROBLAZE)
1952 for (i = 0; i < nargs; ++i)
1953 if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1955 #elif defined(CRISV10) || defined(CRISV32)
1956 static const int crisregs[MAX_ARGS] = {
1957 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
1958 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
1961 for (i = 0; i < nargs; ++i)
1962 if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
1965 for (i = 0; i < nargs; ++i)
1966 tcp->u_arg[i] = tile_regs.regs[i];
1968 for (i = 0; i < nargs; ++i)
1969 if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1973 for (i = 0; i < 6; ++i)
1974 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
1975 #elif defined(METAG)
1976 for (i = 0; i < nargs; i++)
1977 /* arguments go backwards from D1Ar1 (D1.3) */
1978 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
1979 #elif defined(XTENSA)
1980 /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
1981 static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
1982 for (i = 0; i < nargs; ++i)
1983 if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
1986 long *arc_args = &arc_regs.scratch.r0;
1987 for (i = 0; i < nargs; ++i)
1988 tcp->u_arg[i] = *arc_args--;
1990 #else /* Other architecture (32bits specific) */
1991 for (i = 0; i < nargs; ++i)
1992 if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
1999 trace_syscall_entering(struct tcb *tcp)
2003 #if defined TCB_WAITEXECVE
2004 if (tcp->flags & TCB_WAITEXECVE) {
2005 /* This is the post-execve SIGTRAP. */
2006 tcp->flags &= ~TCB_WAITEXECVE;
2011 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
2015 res = syscall_fixup_on_sysenter(tcp);
2019 res = get_syscall_args(tcp);
2025 tprints("????" /* anti-trigraph gap */ "(");
2026 else if (tcp->qual_flg & UNDEFINED_SCNO)
2027 tprintf("%s(", undefined_scno_name(tcp));
2029 tprintf("%s(", tcp->s_ent->sys_name);
2031 * " <unavailable>" will be added later by the code which
2032 * detects ptrace errors.
2037 if ( sys_execve == tcp->s_ent->sys_func
2038 # if defined(SPARC) || defined(SPARC64)
2039 || sys_execv == tcp->s_ent->sys_func
2042 hide_log_until_execve = 0;
2045 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
2047 # ifdef SYS_socket_subcall
2048 if (tcp->s_ent->sys_func == sys_socketcall) {
2049 decode_socket_subcall(tcp);
2053 # ifdef SYS_ipc_subcall
2054 if (tcp->s_ent->sys_func == sys_ipc) {
2055 decode_ipc_subcall(tcp);
2063 if (need_fork_exec_workarounds)
2064 syscall_fixup_for_fork_exec(tcp);
2066 if (!(tcp->qual_flg & QUAL_TRACE)
2067 || (tracing_paths && !pathtrace_match(tcp))
2069 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
2073 tcp->flags &= ~TCB_FILTERED;
2075 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
2080 #ifdef USE_LIBUNWIND
2081 if (stack_trace_enabled) {
2082 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
2083 unwind_capture_stacktrace(tcp);
2088 if (tcp->qual_flg & UNDEFINED_SCNO)
2089 tprintf("%s(", undefined_scno_name(tcp));
2091 tprintf("%s(", tcp->s_ent->sys_name);
2092 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
2093 res = printargs(tcp);
2095 res = tcp->s_ent->sys_func(tcp);
2099 tcp->flags |= TCB_INSYSCALL;
2100 /* Measure the entrance time as late as possible to avoid errors. */
2102 gettimeofday(&tcp->etime, NULL);
2107 * 1: ok, continue in trace_syscall_exiting().
2108 * -1: error, trace_syscall_exiting() should print error indicator
2109 * ("????" etc) and bail out.
2112 get_syscall_result(struct tcb *tcp)
2114 #if defined(S390) || defined(S390X)
2115 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
2117 #elif defined(POWERPC)
2118 /* already done by get_regs */
2119 #elif defined(AVR32)
2120 /* already done by get_regs */
2122 if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
2125 /* already done by get_regs */
2126 #elif defined(X86_64) || defined(X32)
2127 /* already done by get_regs */
2129 # define IA64_PSR_IS ((long)1 << 34)
2131 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
2132 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
2133 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
2135 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
2138 /* already done by get_regs */
2139 #elif defined(AARCH64)
2140 /* register reading already done by get_regs */
2142 /* Used to do this, but we did it on syscall entry already: */
2143 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2144 * else it's personality 0.
2146 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
2148 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
2150 #elif defined(LINUX_MIPSN32)
2151 unsigned long long regs[38];
2153 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
2155 mips_a3 = regs[REG_A3];
2156 mips_r2 = regs[REG_V0];
2158 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
2160 if (upeek(tcp->pid, REG_V0, &mips_r2) < 0)
2162 #elif defined(ALPHA)
2163 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
2165 if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
2167 #elif defined(SPARC) || defined(SPARC64)
2168 /* already done by get_regs */
2170 if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
2173 /* new syscall ABI returns result in R0 */
2174 if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
2177 /* ABI defines result returned in r9 */
2178 if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
2180 #elif defined(CRISV10) || defined(CRISV32)
2181 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
2184 /* already done by get_regs */
2185 #elif defined(MICROBLAZE)
2186 if (upeek(tcp->pid, 3 * 4, µblaze_r3) < 0)
2189 /* already done by get_regs */
2190 #elif defined(METAG)
2191 /* already done by get_regs */
2192 #elif defined(XTENSA)
2193 if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
2196 /* already done by get_regs */
2201 /* Called at each syscall exit */
2203 syscall_fixup_on_sysexit(struct tcb *tcp)
2205 #if defined(S390) || defined(S390X)
2206 if ((tcp->flags & TCB_WAITEXECVE)
2207 && (s390_gpr2 == -ENOSYS || s390_gpr2 == tcp->scno)) {
2209 * Return from execve.
2210 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2211 * flag set for the post-execve SIGTRAP to see and reset.
2219 * Check the syscall return value register value for whether it is
2220 * a negated errno code indicating an error, or a success return value.
2224 is_negated_errno(unsigned long int val)
2226 unsigned long int max = -(long int) nerrnos;
2227 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
2228 if (current_wordsize < sizeof(val)) {
2229 val = (unsigned int) val;
2230 max = (unsigned int) max;
2239 is_negated_errno(unsigned long long val)
2241 unsigned long long max = -(long long) nerrnos;
2243 * current_wordsize is 4 even in personality 0 (native X32)
2244 * but truncation _must not_ be done in it.
2245 * can't check current_wordsize here!
2247 if (current_personality != 0) {
2248 val = (uint32_t) val;
2249 max = (uint32_t) max;
2256 * 1: ok, continue in trace_syscall_exiting().
2257 * -1: error, trace_syscall_exiting() should print error indicator
2258 * ("????" etc) and bail out.
2261 get_error(struct tcb *tcp)
2264 int check_errno = 1;
2265 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
2268 #if defined(S390) || defined(S390X)
2269 if (check_errno && is_negated_errno(s390_gpr2)) {
2271 u_error = -s390_gpr2;
2274 tcp->u_rval = s390_gpr2;
2277 if (check_errno && is_negated_errno(i386_regs.eax)) {
2279 u_error = -i386_regs.eax;
2282 tcp->u_rval = i386_regs.eax;
2284 #elif defined(X86_64)
2286 if (x86_io.iov_len == sizeof(i386_regs)) {
2287 /* Sign extend from 32 bits */
2288 rax = (int32_t)i386_regs.eax;
2290 rax = x86_64_regs.rax;
2292 if (check_errno && is_negated_errno(rax)) {
2300 /* In X32, return value is 64-bit (llseek uses one).
2301 * Using merely "long rax" would not work.
2304 if (x86_io.iov_len == sizeof(i386_regs)) {
2305 /* Sign extend from 32 bits */
2306 rax = (int32_t)i386_regs.eax;
2308 rax = x86_64_regs.rax;
2310 if (check_errno && is_negated_errno(rax)) {
2315 tcp->u_rval = rax; /* truncating */
2319 if (ia64_ia32mode) {
2323 if (check_errno && is_negated_errno(err)) {
2331 if (check_errno && ia64_r10) {
2335 tcp->u_rval = ia64_r8;
2339 if (check_errno && mips_a3) {
2343 tcp->u_rval = mips_r2;
2344 # if defined(LINUX_MIPSN32)
2345 tcp->u_lrval = mips_r2;
2348 #elif defined(POWERPC)
2349 if (check_errno && (ppc_regs.ccr & 0x10000000)) {
2351 u_error = ppc_regs.gpr[3];
2354 tcp->u_rval = ppc_regs.gpr[3];
2357 if (check_errno && is_negated_errno(m68k_d0)) {
2362 tcp->u_rval = m68k_d0;
2364 #elif defined(ARM) || defined(AARCH64)
2365 # if defined(AARCH64)
2366 if (tcp->currpers == 1) {
2367 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2369 u_error = -aarch64_regs.regs[0];
2372 tcp->u_rval = aarch64_regs.regs[0];
2378 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
2380 u_error = -arm_regs.ARM_r0;
2383 tcp->u_rval = arm_regs.ARM_r0;
2386 #elif defined(AVR32)
2387 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
2389 u_error = -avr32_regs.r12;
2392 tcp->u_rval = avr32_regs.r12;
2395 if (check_errno && is_negated_errno(bfin_r0)) {
2399 tcp->u_rval = bfin_r0;
2401 #elif defined(ALPHA)
2402 if (check_errno && alpha_a3) {
2407 tcp->u_rval = alpha_r0;
2409 #elif defined(SPARC)
2410 if (check_errno && sparc_regs.psr & PSR_C) {
2412 u_error = sparc_regs.u_regs[U_REG_O0];
2415 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
2417 #elif defined(SPARC64)
2418 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
2420 u_error = sparc_regs.u_regs[U_REG_O0];
2423 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
2426 if (check_errno && is_negated_errno(hppa_r28)) {
2428 u_error = -hppa_r28;
2431 tcp->u_rval = hppa_r28;
2434 if (check_errno && is_negated_errno(sh_r0)) {
2439 tcp->u_rval = sh_r0;
2442 if (check_errno && is_negated_errno(sh64_r9)) {
2447 tcp->u_rval = sh64_r9;
2449 #elif defined(METAG)
2450 /* result pointer in D0Re0 (D0.0) */
2451 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2453 u_error = -metag_regs.dx[0][0];
2456 tcp->u_rval = metag_regs.dx[0][0];
2458 #elif defined(CRISV10) || defined(CRISV32)
2459 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
2461 u_error = -cris_r10;
2464 tcp->u_rval = cris_r10;
2468 * The standard tile calling convention returns the value (or negative
2469 * errno) in r0, and zero (or positive errno) in r1.
2470 * Until at least kernel 3.8, however, the r1 value is not reflected
2471 * in ptregs at this point, so we use r0 here.
2473 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
2475 u_error = -tile_regs.regs[0];
2477 tcp->u_rval = tile_regs.regs[0];
2479 #elif defined(MICROBLAZE)
2480 if (check_errno && is_negated_errno(microblaze_r3)) {
2482 u_error = -microblaze_r3;
2485 tcp->u_rval = microblaze_r3;
2488 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2490 u_error = -or1k_regs.gpr[11];
2493 tcp->u_rval = or1k_regs.gpr[11];
2495 #elif defined(XTENSA)
2496 if (check_errno && is_negated_errno(xtensa_a2)) {
2498 u_error = -xtensa_a2;
2501 tcp->u_rval = xtensa_a2;
2504 if (check_errno && is_negated_errno(arc_regs.scratch.r0)) {
2506 u_error = -arc_regs.scratch.r0;
2509 tcp->u_rval = arc_regs.scratch.r0;
2512 tcp->u_error = u_error;
2516 dumpio(struct tcb *tcp)
2522 if ((unsigned long) tcp->u_arg[0] >= num_quals)
2524 func = tcp->s_ent->sys_func;
2525 if (func == printargs)
2527 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
2528 if (func == sys_read ||
2529 func == sys_pread ||
2531 func == sys_recvfrom) {
2532 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
2534 } else if (func == sys_readv) {
2535 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2538 } else if (func == sys_recvmsg) {
2539 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
2541 } else if (func == sys_recvmmsg) {
2542 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
2547 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
2548 if (func == sys_write ||
2549 func == sys_pwrite ||
2552 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
2553 else if (func == sys_writev)
2554 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2556 else if (func == sys_sendmsg)
2557 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
2558 else if (func == sys_sendmmsg)
2559 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
2565 trace_syscall_exiting(struct tcb *tcp)
2572 /* Measure the exit time as early as possible to avoid errors. */
2574 gettimeofday(&tv, NULL);
2576 #ifdef USE_LIBUNWIND
2577 if (stack_trace_enabled) {
2578 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
2579 unwind_cache_invalidate(tcp);
2583 #if SUPPORTED_PERSONALITIES > 1
2584 update_personality(tcp, tcp->currpers);
2586 res = (get_regs_error ? -1 : get_syscall_result(tcp));
2588 syscall_fixup_on_sysexit(tcp); /* never fails */
2589 get_error(tcp); /* never fails */
2590 if (need_fork_exec_workarounds)
2591 syscall_fixup_for_fork_exec(tcp);
2592 if (filtered(tcp) || hide_log_until_execve)
2597 count_syscall(tcp, &tv);
2598 if (cflag == CFLAG_ONLY_STATS) {
2603 /* If not in -ff mode, and printing_tcp != tcp,
2604 * then the log currently does not end with output
2605 * of _our syscall entry_, but with something else.
2606 * We need to say which syscall's return is this.
2608 * Forced reprinting via TCB_REPRINT is used only by
2609 * "strace -ff -oLOG test/threaded_execve" corner case.
2610 * It's the only case when -ff mode needs reprinting.
2612 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2613 tcp->flags &= ~TCB_REPRINT;
2615 if (tcp->qual_flg & UNDEFINED_SCNO)
2616 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
2618 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
2623 /* There was error in one of prior ptrace ops */
2626 tprints("= ? <unavailable>\n");
2628 tcp->flags &= ~TCB_INSYSCALL;
2633 if (tcp->qual_flg & QUAL_RAW) {
2634 /* sys_res = printargs(tcp); - but it's nop on sysexit */
2636 /* FIXME: not_failing_only (IOW, option -z) is broken:
2637 * failure of syscall is known only after syscall return.
2638 * Thus we end up with something like this on, say, ENOENT:
2639 * open("doesnt_exist", O_RDONLY <unfinished ...>
2640 * {next syscall decode}
2641 * whereas the intended result is that open(...) line
2642 * is not shown at all.
2644 if (not_failing_only && tcp->u_error)
2645 goto ret; /* ignore failed syscalls */
2646 sys_res = tcp->s_ent->sys_func(tcp);
2651 u_error = tcp->u_error;
2652 if (tcp->qual_flg & QUAL_RAW) {
2654 tprintf("= -1 (errno %ld)", u_error);
2656 tprintf("= %#lx", tcp->u_rval);
2658 else if (!(sys_res & RVAL_NONE) && u_error) {
2660 /* Blocked signals do not interrupt any syscalls.
2661 * In this case syscalls don't return ERESTARTfoo codes.
2663 * Deadly signals set to SIG_DFL interrupt syscalls
2664 * and kill the process regardless of which of the codes below
2665 * is returned by the interrupted syscall.
2666 * In some cases, kernel forces a kernel-generated deadly
2667 * signal to be unblocked and set to SIG_DFL (and thus cause
2668 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2669 * or SIGILL. (The alternative is to leave process spinning
2670 * forever on the faulty instruction - not useful).
2672 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2673 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2674 * but kernel will always restart them.
2677 /* Most common type of signal-interrupted syscall exit code.
2678 * The system call will be restarted with the same arguments
2679 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2681 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
2683 case ERESTARTNOINTR:
2684 /* Rare. For example, fork() returns this if interrupted.
2685 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2687 tprints("= ? ERESTARTNOINTR (To be restarted)");
2689 case ERESTARTNOHAND:
2690 /* pause(), rt_sigsuspend() etc use this code.
2691 * SA_RESTART is ignored (assumed not set):
2692 * syscall won't restart (will return EINTR instead)
2693 * even after signal with SA_RESTART set. However,
2694 * after SIG_IGN or SIG_DFL signal it will restart
2695 * (thus the name "restart only if has no handler").
2697 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
2699 case ERESTART_RESTARTBLOCK:
2700 /* Syscalls like nanosleep(), poll() which can't be
2701 * restarted with their original arguments use this
2702 * code. Kernel will execute restart_syscall() instead,
2703 * which changes arguments before restarting syscall.
2704 * SA_RESTART is ignored (assumed not set) similarly
2705 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2706 * since restart data is saved in "restart block"
2707 * in task struct, and if signal handler uses a syscall
2708 * which in turn saves another such restart block,
2709 * old data is lost and restart becomes impossible)
2711 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
2715 tprintf("= -1 E??? (errno %ld)", u_error);
2716 else if ((unsigned long) u_error < nerrnos)
2717 tprintf("= -1 %s (%s)", errnoent[u_error],
2720 tprintf("= -1 ERRNO_%ld (%s)", u_error,
2724 if ((sys_res & RVAL_STR) && tcp->auxstr)
2725 tprintf(" (%s)", tcp->auxstr);
2728 if (sys_res & RVAL_NONE)
2731 switch (sys_res & RVAL_MASK) {
2733 tprintf("= %#lx", tcp->u_rval);
2736 tprintf("= %#lo", tcp->u_rval);
2739 tprintf("= %lu", tcp->u_rval);
2742 tprintf("= %ld", tcp->u_rval);
2747 printfd(tcp, tcp->u_rval);
2750 tprintf("= %ld", tcp->u_rval);
2752 #if defined(LINUX_MIPSN32) || defined(X32)
2755 tprintf("= %#llx", tcp->u_lrval);
2758 tprintf("= %#llo", tcp->u_lrval);
2761 case RVAL_LUDECIMAL:
2762 tprintf("= %llu", tcp->u_lrval);
2766 tprintf("= %lld", tcp->u_lrval);
2772 "invalid rval format\n");
2776 if ((sys_res & RVAL_STR) && tcp->auxstr)
2777 tprintf(" (%s)", tcp->auxstr);
2780 tv_sub(&tv, &tv, &tcp->etime);
2781 tprintf(" <%ld.%06ld>",
2782 (long) tv.tv_sec, (long) tv.tv_usec);
2788 #ifdef USE_LIBUNWIND
2789 if (stack_trace_enabled)
2790 unwind_print_stacktrace(tcp);
2794 tcp->flags &= ~TCB_INSYSCALL;
2799 trace_syscall(struct tcb *tcp)
2801 return exiting(tcp) ?
2802 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);