2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/param.h>
40 #elif defined(HAVE_LINUX_PTRACE_H)
41 # undef PTRACE_SYSCALL
42 # ifdef HAVE_STRUCT_IA64_FPREG
43 # define ia64_fpreg XXX_ia64_fpreg
45 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
46 # define pt_all_user_regs XXX_pt_all_user_regs
48 # ifdef HAVE_STRUCT_PTRACE_PEEKSIGINFO_ARGS
49 # define ptrace_peeksiginfo_args XXX_ptrace_peeksiginfo_args
51 # include <linux/ptrace.h>
52 # undef ptrace_peeksiginfo_args
54 # undef pt_all_user_regs
58 # undef PTRACE_GETREGS
59 # define PTRACE_GETREGS PTRACE_GETREGS64
60 # undef PTRACE_SETREGS
61 # define PTRACE_SETREGS PTRACE_SETREGS64
65 # include <asm/ptrace_offsets.h>
69 /* for struct iovec */
77 # include <asm/ptrace.h>
81 # include <asm/ptrace.h>
85 # warning: NSIG is not defined, using 32
91 /* Define these shorthand notations to simplify the syscallent files. */
95 #define TN TRACE_NETWORK
96 #define TP TRACE_PROCESS
97 #define TS TRACE_SIGNAL
98 #define TM TRACE_MEMORY
99 #define NF SYSCALL_NEVER_FAILS
102 const struct_sysent sysent0[] = {
103 #include "syscallent.h"
106 #if SUPPORTED_PERSONALITIES > 1
107 static const struct_sysent sysent1[] = {
108 # include "syscallent1.h"
112 #if SUPPORTED_PERSONALITIES > 2
113 static const struct_sysent sysent2[] = {
114 # include "syscallent2.h"
118 /* Now undef them since short defines cause wicked namespace pollution. */
130 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
131 * program `ioctlsort', such that the list is sorted by the `code' field.
132 * This has the side-effect of resolving the _IO.. macros into
133 * plain integers, eliminating the need to include here everything
137 const char *const errnoent0[] = {
138 #include "errnoent.h"
140 const char *const signalent0[] = {
141 #include "signalent.h"
143 const struct_ioctlent ioctlent0[] = {
144 #include "ioctlent.h"
147 #if SUPPORTED_PERSONALITIES > 1
148 static const char *const errnoent1[] = {
149 # include "errnoent1.h"
151 static const char *const signalent1[] = {
152 # include "signalent1.h"
154 static const struct_ioctlent ioctlent1[] = {
155 # include "ioctlent1.h"
159 #if SUPPORTED_PERSONALITIES > 2
160 static const char *const errnoent2[] = {
161 # include "errnoent2.h"
163 static const char *const signalent2[] = {
164 # include "signalent2.h"
166 static const struct_ioctlent ioctlent2[] = {
167 # include "ioctlent2.h"
172 nsyscalls0 = ARRAY_SIZE(sysent0)
173 #if SUPPORTED_PERSONALITIES > 1
174 , nsyscalls1 = ARRAY_SIZE(sysent1)
175 # if SUPPORTED_PERSONALITIES > 2
176 , nsyscalls2 = ARRAY_SIZE(sysent2)
182 nerrnos0 = ARRAY_SIZE(errnoent0)
183 #if SUPPORTED_PERSONALITIES > 1
184 , nerrnos1 = ARRAY_SIZE(errnoent1)
185 # if SUPPORTED_PERSONALITIES > 2
186 , nerrnos2 = ARRAY_SIZE(errnoent2)
192 nsignals0 = ARRAY_SIZE(signalent0)
193 #if SUPPORTED_PERSONALITIES > 1
194 , nsignals1 = ARRAY_SIZE(signalent1)
195 # if SUPPORTED_PERSONALITIES > 2
196 , nsignals2 = ARRAY_SIZE(signalent2)
202 nioctlents0 = ARRAY_SIZE(ioctlent0)
203 #if SUPPORTED_PERSONALITIES > 1
204 , nioctlents1 = ARRAY_SIZE(ioctlent1)
205 # if SUPPORTED_PERSONALITIES > 2
206 , nioctlents2 = ARRAY_SIZE(ioctlent2)
211 #if SUPPORTED_PERSONALITIES > 1
212 const struct_sysent *sysent = sysent0;
213 const char *const *errnoent = errnoent0;
214 const char *const *signalent = signalent0;
215 const struct_ioctlent *ioctlent = ioctlent0;
217 unsigned nsyscalls = nsyscalls0;
218 unsigned nerrnos = nerrnos0;
219 unsigned nsignals = nsignals0;
220 unsigned nioctlents = nioctlents0;
223 qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
225 static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
227 #if SUPPORTED_PERSONALITIES > 1
230 #if SUPPORTED_PERSONALITIES > 2
234 static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
236 #if SUPPORTED_PERSONALITIES > 1
239 #if SUPPORTED_PERSONALITIES > 2
245 MAX_NSYSCALLS1 = (nsyscalls0
246 #if SUPPORTED_PERSONALITIES > 1
247 > nsyscalls1 ? nsyscalls0 : nsyscalls1
250 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
251 #if SUPPORTED_PERSONALITIES > 2
252 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
255 MAX_NSYSCALLS = MAX_NSYSCALLS2,
256 /* We are ready for arches with up to 255 signals,
257 * even though the largest known signo is on MIPS and it is 128.
258 * The number of existing syscalls on all arches is
259 * larger that 255 anyway, so it is just a pedantic matter.
261 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
264 #if SUPPORTED_PERSONALITIES > 1
265 unsigned current_personality;
267 # ifndef current_wordsize
268 unsigned current_wordsize;
269 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
270 PERSONALITY0_WORDSIZE,
271 PERSONALITY1_WORDSIZE,
272 # if SUPPORTED_PERSONALITIES > 2
273 PERSONALITY2_WORDSIZE,
279 set_personality(int personality)
281 nsyscalls = nsyscall_vec[personality];
282 sysent = sysent_vec[personality];
284 switch (personality) {
286 errnoent = errnoent0;
288 ioctlent = ioctlent0;
289 nioctlents = nioctlents0;
290 signalent = signalent0;
291 nsignals = nsignals0;
295 errnoent = errnoent1;
297 ioctlent = ioctlent1;
298 nioctlents = nioctlents1;
299 signalent = signalent1;
300 nsignals = nsignals1;
303 # if SUPPORTED_PERSONALITIES > 2
305 errnoent = errnoent2;
307 ioctlent = ioctlent2;
308 nioctlents = nioctlents2;
309 signalent = signalent2;
310 nsignals = nsignals2;
315 current_personality = personality;
316 # ifndef current_wordsize
317 current_wordsize = personality_wordsize[personality];
322 update_personality(struct tcb *tcp, int personality)
324 if (personality == current_personality)
326 set_personality(personality);
328 if (personality == tcp->currpers)
330 tcp->currpers = personality;
332 # if defined(POWERPC64)
334 static const char *const names[] = {"64 bit", "32 bit"};
335 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
336 tcp->pid, names[personality]);
338 # elif defined(X86_64)
340 static const char *const names[] = {"64 bit", "32 bit", "x32"};
341 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
342 tcp->pid, names[personality]);
346 static const char *const names[] = {"x32", "32 bit"};
347 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
348 tcp->pid, names[personality]);
350 # elif defined(AARCH64)
352 static const char *const names[] = {"32-bit", "AArch64"};
353 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
354 tcp->pid, names[personality]);
358 static const char *const names[] = {"64-bit", "32-bit"};
359 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
360 tcp->pid, names[personality]);
366 static int qual_syscall(), qual_signal(), qual_desc();
368 static const struct qual_options {
370 const char *option_name;
371 int (*qualify)(const char *, int, int);
372 const char *argument_name;
374 { QUAL_TRACE, "trace", qual_syscall, "system call" },
375 { QUAL_TRACE, "t", qual_syscall, "system call" },
376 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
377 { QUAL_ABBREV, "a", qual_syscall, "system call" },
378 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
379 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
380 { QUAL_RAW, "raw", qual_syscall, "system call" },
381 { QUAL_RAW, "x", qual_syscall, "system call" },
382 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
383 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
384 { QUAL_SIGNAL, "s", qual_signal, "signal" },
385 { QUAL_READ, "read", qual_desc, "descriptor" },
386 { QUAL_READ, "reads", qual_desc, "descriptor" },
387 { QUAL_READ, "r", qual_desc, "descriptor" },
388 { QUAL_WRITE, "write", qual_desc, "descriptor" },
389 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
390 { QUAL_WRITE, "w", qual_desc, "descriptor" },
391 { 0, NULL, NULL, NULL },
395 reallocate_qual(int n)
399 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
400 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
403 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
409 qualify_one(int n, int bitflag, int not, int pers)
414 reallocate_qual(n + 1);
416 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
417 if (pers == p || pers < 0) {
419 qual_vec[p][n] &= ~bitflag;
421 qual_vec[p][n] |= bitflag;
427 qual_syscall(const char *s, int bitflag, int not)
433 if (*s >= '0' && *s <= '9') {
434 i = string_to_uint(s);
435 if (i >= MAX_NSYSCALLS)
437 qualify_one(i, bitflag, not, -1);
441 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
442 for (i = 0; i < nsyscall_vec[p]; i++) {
443 if (sysent_vec[p][i].sys_name
444 && strcmp(s, sysent_vec[p][i].sys_name) == 0
446 qualify_one(i, bitflag, not, p);
456 qual_signal(const char *s, int bitflag, int not)
460 if (*s >= '0' && *s <= '9') {
461 int signo = string_to_uint(s);
462 if (signo < 0 || signo > 255)
464 qualify_one(signo, bitflag, not, -1);
467 if (strncasecmp(s, "SIG", 3) == 0)
469 for (i = 0; i <= NSIG; i++) {
470 if (strcasecmp(s, signame(i) + 3) == 0) {
471 qualify_one(i, bitflag, not, -1);
479 qual_desc(const char *s, int bitflag, int not)
481 if (*s >= '0' && *s <= '9') {
482 int desc = string_to_uint(s);
483 if (desc < 0 || desc > 0x7fff) /* paranoia */
485 qualify_one(desc, bitflag, not, -1);
492 lookup_class(const char *s)
494 if (strcmp(s, "file") == 0)
496 if (strcmp(s, "ipc") == 0)
498 if (strcmp(s, "network") == 0)
499 return TRACE_NETWORK;
500 if (strcmp(s, "process") == 0)
501 return TRACE_PROCESS;
502 if (strcmp(s, "signal") == 0)
504 if (strcmp(s, "desc") == 0)
506 if (strcmp(s, "memory") == 0)
512 qualify(const char *s)
514 const struct qual_options *opt;
521 reallocate_qual(MIN_QUALS);
523 opt = &qual_options[0];
524 for (i = 0; (p = qual_options[i].option_name); i++) {
526 if (strncmp(s, p, n) == 0 && s[n] == '=') {
527 opt = &qual_options[i];
537 if (strcmp(s, "none") == 0) {
541 if (strcmp(s, "all") == 0) {
542 for (i = 0; i < num_quals; i++) {
543 qualify_one(i, opt->bitflag, not, -1);
547 for (i = 0; i < num_quals; i++) {
548 qualify_one(i, opt->bitflag, !not, -1);
553 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
554 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
556 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
557 for (i = 0; i < nsyscall_vec[pers]; i++)
558 if (sysent_vec[pers][i].sys_flags & n)
559 qualify_one(i, opt->bitflag, not, pers);
563 if (opt->qualify(p, opt->bitflag, not)) {
564 error_msg_and_die("invalid %s '%s'",
565 opt->argument_name, p);
572 #ifdef SYS_socket_subcall
574 decode_socket_subcall(struct tcb *tcp)
577 unsigned int i, n, size;
579 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
582 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
583 tcp->qual_flg = qual_flags[tcp->scno];
584 tcp->s_ent = &sysent[tcp->scno];
585 addr = tcp->u_arg[1];
586 size = current_wordsize;
587 n = tcp->s_ent->nargs;
588 for (i = 0; i < n; ++i) {
589 if (size == sizeof(int)) {
591 if (umove(tcp, addr, &arg) < 0)
597 if (umove(tcp, addr, &arg) < 0)
606 #ifdef SYS_ipc_subcall
608 decode_ipc_subcall(struct tcb *tcp)
612 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
615 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
616 tcp->qual_flg = qual_flags[tcp->scno];
617 tcp->s_ent = &sysent[tcp->scno];
618 n = tcp->s_ent->nargs;
619 for (i = 0; i < n; i++)
620 tcp->u_arg[i] = tcp->u_arg[i + 1];
625 printargs(struct tcb *tcp)
629 int n = tcp->s_ent->nargs;
630 for (i = 0; i < n; i++)
631 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
637 printargs_lu(struct tcb *tcp)
641 int n = tcp->s_ent->nargs;
642 for (i = 0; i < n; i++)
643 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
649 printargs_ld(struct tcb *tcp)
653 int n = tcp->s_ent->nargs;
654 for (i = 0; i < n; i++)
655 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
660 #if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
662 getrval2(struct tcb *tcp)
666 # if defined(SPARC) || defined(SPARC64)
667 val = sparc_regs.u_regs[U_REG_O1];
669 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
672 if (upeek(tcp->pid, PT_R9, &val) < 0)
681 static struct user_regs_struct i386_regs;
682 /* Cast suppresses signedness warning (.esp is long, not unsigned long) */
683 uint32_t *const i386_esp_ptr = (uint32_t*)&i386_regs.esp;
684 # define ARCH_REGS_FOR_GETREGSET i386_regs
685 #elif defined(X86_64) || defined(X32)
687 * On i386, pt_regs and user_regs_struct are the same,
688 * but on 64 bit x86, user_regs_struct has six more fields:
689 * fs_base, gs_base, ds, es, fs, gs.
690 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
692 struct i386_user_regs_struct {
712 struct user_regs_struct x86_64_r;
713 struct i386_user_regs_struct i386_r;
715 # define x86_64_regs x86_regs_union.x86_64_r
716 # define i386_regs x86_regs_union.i386_r
717 uint32_t *const i386_esp_ptr = &i386_regs.esp;
718 static struct iovec x86_io = {
719 .iov_base = &x86_regs_union
722 bool ia64_ia32mode = 0; /* not static */
723 static long ia64_r8, ia64_r10;
724 #elif defined(POWERPC)
725 struct pt_regs ppc_regs;
731 struct pt_regs arm_regs; /* not static */
732 # define ARCH_REGS_FOR_GETREGSET arm_regs
733 #elif defined(AARCH64)
735 struct user_pt_regs aarch64_r;
736 struct arm_pt_regs arm_r;
738 # define aarch64_regs arm_regs_union.aarch64_r
739 # define arm_regs arm_regs_union.arm_r
740 static struct iovec aarch64_io = {
741 .iov_base = &arm_regs_union
744 static long alpha_r0;
745 static long alpha_a3;
747 static struct pt_regs avr32_regs;
748 #elif defined(SPARC) || defined(SPARC64)
749 struct pt_regs sparc_regs; /* not static */
750 #elif defined(LINUX_MIPSN32)
751 static long long mips_a3;
752 static long long mips_r2;
756 #elif defined(S390) || defined(S390X)
757 static long s390_gpr2;
759 static long hppa_r28;
764 #elif defined(CRISV10) || defined(CRISV32)
765 static long cris_r10;
767 struct pt_regs tile_regs;
768 #elif defined(MICROBLAZE)
769 static long microblaze_r3;
771 static struct user_regs_struct or1k_regs;
772 # define ARCH_REGS_FOR_GETREGSET or1k_regs
774 static struct user_gp_regs metag_regs;
775 # define ARCH_REGS_FOR_GETREGSET metag_regs
776 #elif defined(XTENSA)
777 static long xtensa_a2;
779 static struct user_regs_struct arc_regs;
780 # define ARCH_REGS_FOR_GETREGSET arc_regs
784 print_pc(struct tcb *tcp)
786 #define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
787 sizeof(long) == 8 ? "[????????????????] " : \
789 if (get_regs_error) {
794 tprintf("[%08lx] ", i386_regs.eip);
795 #elif defined(S390) || defined(S390X)
797 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0) {
802 tprintf("[%08lx] ", psw);
804 tprintf("[%016lx] ", psw);
806 #elif defined(X86_64) || defined(X32)
807 if (x86_io.iov_len == sizeof(i386_regs)) {
808 tprintf("[%08x] ", (unsigned) i386_regs.eip);
811 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
813 /* Note: this truncates 64-bit rip to 32 bits */
814 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
819 if (upeek(tcp->pid, PT_B0, &ip) < 0) {
823 tprintf("[%08lx] ", ip);
824 #elif defined(POWERPC)
825 long pc = ppc_regs.nip;
827 tprintf("[%016lx] ", pc);
829 tprintf("[%08lx] ", pc);
833 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
834 tprints("[????????] ");
837 tprintf("[%08lx] ", pc);
840 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
841 tprints("[????????????????] ");
844 tprintf("[%08lx] ", pc);
846 tprintf("[%08lx] ", sparc_regs.pc);
847 #elif defined(SPARC64)
848 tprintf("[%08lx] ", sparc_regs.tpc);
851 if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
852 tprints("[????????] ");
855 tprintf("[%08lx] ", pc);
858 if (upeek(tcp->pid, REG_EPC, &pc) < 0) {
859 tprints("[????????] ");
862 tprintf("[%08lx] ", pc);
865 if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
866 tprints("[????????] ");
869 tprintf("[%08lx] ", pc);
872 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
873 tprints("[????????????????] ");
876 tprintf("[%08lx] ", pc);
878 tprintf("[%08lx] ", arm_regs.ARM_pc);
879 #elif defined(AARCH64)
880 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
882 tprintf("[%08lx] ", avr32_regs.pc);
885 if (upeek(tcp->pid, PT_PC, &pc) < 0) {
889 tprintf("[%08lx] ", pc);
890 #elif defined(CRISV10)
892 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
896 tprintf("[%08lx] ", pc);
897 #elif defined(CRISV32)
899 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
903 tprintf("[%08lx] ", pc);
906 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
908 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
911 tprintf("[%08lx] ", or1k_regs.pc);
913 tprintf("[%08lx] ", metag_regs.pc);
914 #elif defined(XTENSA)
916 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
920 tprintf("[%08lx] ", pc);
922 tprintf("[%08lx] ", arc_regs.efa);
923 #endif /* architecture */
926 /* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
927 * The shuffling should be reversible: shuffle_scno(shuffle_scno(n)) == n.
929 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
931 shuffle_scno(unsigned long scno)
933 if (scno <= ARM_LAST_ORDINARY_SYSCALL)
936 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
937 if (scno == 0x000ffff0)
938 return ARM_LAST_ORDINARY_SYSCALL+1;
939 if (scno == ARM_LAST_ORDINARY_SYSCALL+1)
942 /* Is it ARM specific syscall?
943 * Swap with [LAST_ORDINARY+2, LAST_ORDINARY+2 + LAST_SPECIAL] range.
945 if (scno >= 0x000f0000
946 && scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL
948 return scno - 0x000f0000 + (ARM_LAST_ORDINARY_SYSCALL+2);
950 if (/* scno >= ARM_LAST_ORDINARY_SYSCALL+2 - always true */ 1
951 && scno <= (ARM_LAST_ORDINARY_SYSCALL+2) + ARM_LAST_SPECIAL_SYSCALL
953 return scno + 0x000f0000 - (ARM_LAST_ORDINARY_SYSCALL+2);
959 # define shuffle_scno(scno) ((long)(scno))
963 undefined_scno_name(struct tcb *tcp)
965 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
967 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
973 * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
974 * we provide a slow fallback for old kernels.
976 static int powerpc_getregs_old(pid_t pid)
982 r = upeek(pid, sizeof(long) * PT_NIP, (long *)&ppc_regs.nip);
986 #ifdef POWERPC64 /* else we never use it */
987 r = upeek(pid, sizeof(long) * PT_MSR, (long *)&ppc_regs.msr);
991 r = upeek(pid, sizeof(long) * PT_CCR, (long *)&ppc_regs.ccr);
994 r = upeek(pid, sizeof(long) * PT_ORIG_R3, (long *)&ppc_regs.orig_gpr3);
997 for (i = 0; i <= 8; i++) {
998 r = upeek(pid, sizeof(long) * (PT_R0 + i),
999 (long *)&ppc_regs.gpr[i]);
1009 long get_regs_error;
1011 #if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1012 static void get_regset(pid_t pid)
1014 /* constant iovec */
1020 static struct iovec io = {
1021 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1022 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1024 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1026 /* variable iovec */
1027 # elif defined(X86_64) || defined(X32)
1028 /* x86_io.iov_base = &x86_regs_union; - already is */
1029 x86_io.iov_len = sizeof(x86_regs_union);
1030 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &x86_io);
1031 # elif defined(AARCH64)
1032 /* aarch64_io.iov_base = &arm_regs_union; - already is */
1033 aarch64_io.iov_len = sizeof(arm_regs_union);
1034 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &aarch64_io);
1036 # warning both PTRACE_GETREGSET and NT_PRSTATUS are available but not yet used
1039 #endif /* PTRACE_GETREGSET && NT_PRSTATUS */
1044 /* PTRACE_GETREGSET only */
1045 # if defined(METAG) || defined(OR1K) || defined(X32) || defined(AARCH64) || defined(ARC)
1048 /* PTRACE_GETREGS only */
1049 # elif defined(AVR32)
1050 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
1051 # elif defined(TILE)
1052 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &tile_regs);
1053 # elif defined(SPARC) || defined(SPARC64)
1054 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
1055 # elif defined(POWERPC)
1056 static bool old_kernel = 0;
1059 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &ppc_regs);
1060 if (get_regs_error && errno == EIO) {
1063 get_regs_error = powerpc_getregs_old(pid);
1066 /* try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS */
1068 # if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1069 static int getregset_support;
1071 if (getregset_support >= 0) {
1073 if (getregset_support > 0)
1075 if (get_regs_error >= 0) {
1076 getregset_support = 1;
1079 if (errno == EPERM || errno == ESRCH)
1081 getregset_support = -1;
1083 # endif /* PTRACE_GETREGSET && NT_PRSTATUS */
1085 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &arm_regs);
1086 # elif defined(I386)
1087 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &i386_regs);
1088 # elif defined(X86_64)
1089 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1090 x86_io.iov_len = sizeof(x86_64_regs);
1091 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
1092 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1093 x86_io.iov_len = sizeof(i386_regs);
1095 * The order is important: i386_regs and x86_64_regs
1096 * are overlaid in memory!
1098 i386_regs.ebx = x86_64_regs.rbx;
1099 i386_regs.ecx = x86_64_regs.rcx;
1100 i386_regs.edx = x86_64_regs.rdx;
1101 i386_regs.esi = x86_64_regs.rsi;
1102 i386_regs.edi = x86_64_regs.rdi;
1103 i386_regs.ebp = x86_64_regs.rbp;
1104 i386_regs.eax = x86_64_regs.rax;
1105 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1106 /* i386_regs.xes = x86_64_regs.es; ditto... */
1107 /* i386_regs.xfs = x86_64_regs.fs; */
1108 /* i386_regs.xgs = x86_64_regs.gs; */
1109 i386_regs.orig_eax = x86_64_regs.orig_rax;
1110 i386_regs.eip = x86_64_regs.rip;
1111 /* i386_regs.xcs = x86_64_regs.cs; */
1112 /* i386_regs.eflags = x86_64_regs.eflags; */
1113 i386_regs.esp = x86_64_regs.rsp;
1114 /* i386_regs.xss = x86_64_regs.ss; */
1117 # error unhandled architecture
1118 # endif /* ARM || I386 || X86_64 */
1121 #endif /* !get_regs */
1124 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1125 * 1: ok, continue in trace_syscall_entering().
1126 * other: error, trace_syscall_entering() should print error indicator
1127 * ("????" etc) and bail out.
1130 get_scno(struct tcb *tcp)
1134 #if defined(S390) || defined(S390X)
1135 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
1138 if (s390_gpr2 != -ENOSYS) {
1140 * Since kernel version 2.5.44 the scno gets passed in gpr2.
1145 * Old style of "passing" the scno via the SVC instruction.
1148 long opcode, offset_reg, tmp;
1150 static const int gpr_offset[16] = {
1151 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1152 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1153 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1154 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1157 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0)
1160 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
1162 perror_msg("peektext(psw-oneword)");
1167 * We have to check if the SVC got executed directly or via an
1168 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1169 * instruction decoding to derive the system call number.
1170 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1171 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1172 * opcode. Since there is no way to find out the opcode size this
1173 * is the best we can do...
1175 if ((opcode & 0xff00) == 0x0a00) {
1177 scno = opcode & 0xff;
1180 /* SVC got executed by EXECUTE instruction */
1183 * Do instruction decoding of EXECUTE. If you really want to
1184 * understand this, read the Principles of Operations.
1186 svc_addr = (void *) (opcode & 0xfff);
1189 offset_reg = (opcode & 0x000f0000) >> 16;
1190 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
1195 offset_reg = (opcode & 0x0000f000) >> 12;
1196 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
1200 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
1209 offset_reg = (opcode & 0x00f00000) >> 20;
1210 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
1213 scno = (scno | tmp) & 0xff;
1216 #elif defined(POWERPC)
1217 scno = ppc_regs.gpr[0];
1222 * Check for 64/32 bit mode.
1223 * Embedded implementations covered by Book E extension of PPC use
1224 * bit 0 (CM) of 32-bit Machine state register (MSR).
1225 * Other implementations use bit 0 (SF) of 64-bit MSR.
1227 currpers = (ppc_regs.msr & 0x8000000080000000) ? 0 : 1;
1228 update_personality(tcp, currpers);
1230 #elif defined(AVR32)
1231 scno = avr32_regs.r8;
1233 if (upeek(tcp->pid, PT_ORIG_P0, &scno))
1236 scno = i386_regs.orig_eax;
1237 #elif defined(X86_64) || defined(X32)
1238 # ifndef __X32_SYSCALL_BIT
1239 # define __X32_SYSCALL_BIT 0x40000000
1243 /* GETREGSET of NT_PRSTATUS tells us regset size,
1244 * which unambiguously detects i386.
1246 * Linux kernel distinguishes x86-64 and x32 processes
1247 * solely by looking at __X32_SYSCALL_BIT:
1248 * arch/x86/include/asm/compat.h::is_x32_task():
1249 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1252 if (x86_io.iov_len == sizeof(i386_regs)) {
1253 scno = i386_regs.orig_eax;
1256 scno = x86_64_regs.orig_rax;
1258 if (scno & __X32_SYSCALL_BIT) {
1259 scno -= __X32_SYSCALL_BIT;
1264 /* cs = 0x33 for long mode (native 64 bit and x32)
1265 * cs = 0x23 for compatibility mode (32 bit)
1266 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1268 scno = x86_64_regs.orig_rax;
1269 switch (x86_64_regs.cs) {
1270 case 0x23: currpers = 1; break;
1272 if (x86_64_regs.ds == 0x2b) {
1274 scno &= ~__X32_SYSCALL_BIT;
1279 fprintf(stderr, "Unknown value CS=0x%08X while "
1280 "detecting personality of process "
1281 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
1282 currpers = current_personality;
1286 /* This version analyzes the opcode of a syscall instruction.
1287 * (int 0x80 on i386 vs. syscall on x86-64)
1288 * It works, but is too complicated, and strictly speaking, unreliable.
1290 unsigned long call, rip = x86_64_regs.rip;
1291 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1294 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
1296 fprintf(stderr, "ptrace_peektext failed: %s\n",
1298 switch (call & 0xffff) {
1299 /* x86-64: syscall = 0x0f 0x05 */
1300 case 0x050f: currpers = 0; break;
1301 /* i386: int 0x80 = 0xcd 0x80 */
1302 case 0x80cd: currpers = 1; break;
1304 currpers = current_personality;
1306 "Unknown syscall opcode (0x%04X) while "
1307 "detecting personality of process "
1308 "PID=%d\n", (int)call, tcp->pid);
1314 /* If we are built for a x32 system, then personality 0 is x32
1315 * (not x86_64), and stracing of x86_64 apps is not supported.
1316 * Stracing of i386 apps is still supported.
1318 if (currpers == 0) {
1319 fprintf(stderr, "syscall_%lu(...) in unsupported "
1320 "64-bit mode of process PID=%d\n",
1324 currpers &= ~2; /* map 2,1 to 0,1 */
1326 update_personality(tcp, currpers);
1328 # define IA64_PSR_IS ((long)1 << 34)
1330 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
1331 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
1332 if (ia64_ia32mode) {
1333 if (upeek(tcp->pid, PT_R1, &scno) < 0)
1336 if (upeek(tcp->pid, PT_R15, &scno) < 0)
1339 #elif defined(AARCH64)
1340 switch (aarch64_io.iov_len) {
1341 case sizeof(aarch64_regs):
1342 /* We are in 64-bit mode */
1343 scno = aarch64_regs.regs[8];
1344 update_personality(tcp, 1);
1346 case sizeof(arm_regs):
1347 /* We are in 32-bit mode */
1348 /* Note: we don't support OABI, unlike 32-bit ARM build */
1349 scno = arm_regs.ARM_r7;
1350 scno = shuffle_scno(scno);
1351 update_personality(tcp, 0);
1355 if (arm_regs.ARM_ip != 0) {
1356 /* It is not a syscall entry */
1357 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1358 tcp->flags |= TCB_INSYSCALL;
1361 /* Note: we support only 32-bit CPUs, not 26-bit */
1363 # if !defined(__ARM_EABI__) || ENABLE_ARM_OABI
1364 if (arm_regs.ARM_cpsr & 0x20)
1368 /* Check EABI/OABI by examining SVC insn's low 24 bits */
1370 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1373 /* EABI syscall convention? */
1374 if (scno != 0xef000000) {
1376 if ((scno & 0x0ff00000) != 0x0f900000) {
1377 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1381 /* Fixup the syscall number */
1385 scno = arm_regs.ARM_r7;
1387 # else /* __ARM_EABI__ || !ENABLE_ARM_OABI */
1388 scno = arm_regs.ARM_r7;
1390 scno = shuffle_scno(scno);
1392 if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
1394 #elif defined(LINUX_MIPSN32)
1395 unsigned long long regs[38];
1397 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1399 mips_a3 = regs[REG_A3];
1400 mips_r2 = regs[REG_V0];
1403 if (!SCNO_IN_RANGE(scno)) {
1404 if (mips_a3 == 0 || mips_a3 == -1) {
1406 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1411 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
1413 if (upeek(tcp->pid, REG_V0, &scno) < 0)
1416 if (!SCNO_IN_RANGE(scno)) {
1417 if (mips_a3 == 0 || mips_a3 == -1) {
1419 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1423 #elif defined(ALPHA)
1424 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
1426 if (upeek(tcp->pid, REG_R0, &scno) < 0)
1430 * Do some sanity checks to figure out if it's
1431 * really a syscall entry
1433 if (!SCNO_IN_RANGE(scno)) {
1434 if (alpha_a3 == 0 || alpha_a3 == -1) {
1436 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
1440 #elif defined(SPARC) || defined(SPARC64)
1441 /* Disassemble the syscall trap. */
1442 /* Retrieve the syscall trap instruction. */
1445 # if defined(SPARC64)
1446 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
1449 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
1454 /* Disassemble the trap to see what personality to use. */
1457 /* Linux/SPARC syscall trap. */
1458 update_personality(tcp, 0);
1461 /* Linux/SPARC64 syscall trap. */
1462 update_personality(tcp, 2);
1465 /* SunOS syscall trap. (pers 1) */
1466 fprintf(stderr, "syscall: SunOS no support\n");
1469 /* Solaris 2.x syscall trap. (per 2) */
1470 update_personality(tcp, 1);
1473 /* NetBSD/FreeBSD syscall trap. */
1474 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1477 /* Solaris 2.x gettimeofday */
1478 update_personality(tcp, 1);
1481 # if defined(SPARC64)
1482 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
1484 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
1489 /* Extract the system call number from the registers. */
1490 if (trap == 0x91d02027)
1493 scno = sparc_regs.u_regs[U_REG_G1];
1495 scno = sparc_regs.u_regs[U_REG_O0];
1496 memmove(&sparc_regs.u_regs[U_REG_O0], &sparc_regs.u_regs[U_REG_O1], 7*sizeof(sparc_regs.u_regs[0]));
1499 if (upeek(tcp->pid, PT_GR20, &scno) < 0)
1503 * In the new syscall ABI, the system call number is in R3.
1505 if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
1509 /* Odd as it may seem, a glibc bug has been known to cause
1510 glibc to issue bogus negative syscall numbers. So for
1511 our purposes, make strace print what it *should* have been */
1512 long correct_scno = (scno & 0xff);
1515 "Detected glibc bug: bogus system call"
1516 " number = %ld, correcting to %ld\n",
1519 scno = correct_scno;
1522 if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
1525 #elif defined(CRISV10) || defined(CRISV32)
1526 if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
1530 scno = tile_regs.regs[10];
1534 # ifndef PT_FLAGS_COMPAT
1535 # define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1537 if (tile_regs.flags & PT_FLAGS_COMPAT)
1542 update_personality(tcp, currpers);
1543 #elif defined(MICROBLAZE)
1544 if (upeek(tcp->pid, 0, &scno) < 0)
1547 scno = or1k_regs.gpr[11];
1548 #elif defined(METAG)
1549 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
1550 #elif defined(XTENSA)
1551 if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
1554 scno = arc_regs.scratch.r8;
1558 if (SCNO_IS_VALID(tcp->scno)) {
1559 tcp->s_ent = &sysent[scno];
1560 tcp->qual_flg = qual_flags[scno];
1562 static const struct_sysent unknown = {
1565 .sys_func = printargs,
1566 .sys_name = "unknown", /* not used */
1568 tcp->s_ent = &unknown;
1569 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1574 /* Called at each syscall entry.
1576 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1577 * 1: ok, continue in trace_syscall_entering().
1578 * other: error, trace_syscall_entering() should print error indicator
1579 * ("????" etc) and bail out.
1582 syscall_fixup_on_sysenter(struct tcb *tcp)
1584 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
1586 if (i386_regs.eax != -ENOSYS) {
1588 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1591 #elif defined(X86_64) || defined(X32)
1594 if (x86_io.iov_len == sizeof(i386_regs)) {
1595 /* Sign extend from 32 bits */
1596 rax = (int32_t)i386_regs.eax;
1598 /* Note: in X32 build, this truncates 64 to 32 bits */
1599 rax = x86_64_regs.rax;
1601 if (rax != -ENOSYS) {
1603 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1608 /* TODO? Eliminate upeek's in arches below like we did in x86 */
1609 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
1611 if (m68k_d0 != -ENOSYS) {
1613 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
1617 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
1619 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
1621 if (ia64_ia32mode && ia64_r8 != -ENOSYS) {
1623 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
1626 #elif defined(CRISV10) || defined(CRISV32)
1627 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
1629 if (cris_r10 != -ENOSYS) {
1631 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
1634 #elif defined(MICROBLAZE)
1635 if (upeek(tcp->pid, 3 * 4, µblaze_r3) < 0)
1637 if (microblaze_r3 != -ENOSYS) {
1639 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
1647 internal_fork(struct tcb *tcp)
1649 #if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1650 # define ARG_FLAGS 1
1652 # define ARG_FLAGS 0
1654 #ifndef CLONE_UNTRACED
1655 # define CLONE_UNTRACED 0x00800000
1657 if ((ptrace_setoptions
1658 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1659 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1665 if (entering(tcp)) {
1667 * We won't see the new child if clone is called with
1668 * CLONE_UNTRACED, so we keep the same logic with that option
1669 * and don't trace it.
1671 if ((tcp->s_ent->sys_func == sys_clone)
1672 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1677 if (tcp->flags & TCB_BPTSET)
1682 #if defined(TCB_WAITEXECVE)
1684 internal_exec(struct tcb *tcp)
1686 /* Maybe we have post-execve SIGTRAP suppressed? */
1687 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1688 return; /* yes, no need to do anything */
1690 if (exiting(tcp) && syserror(tcp))
1691 /* Error in execve, no post-execve SIGTRAP expected */
1692 tcp->flags &= ~TCB_WAITEXECVE;
1694 tcp->flags |= TCB_WAITEXECVE;
1699 syscall_fixup_for_fork_exec(struct tcb *tcp)
1702 * We must always trace a few critical system calls in order to
1703 * correctly support following forks in the presence of tracing
1708 func = tcp->s_ent->sys_func;
1710 if ( sys_fork == func
1711 || sys_clone == func
1717 #if defined(TCB_WAITEXECVE)
1718 if ( sys_execve == func
1719 # if defined(SPARC) || defined(SPARC64)
1720 || sys_execv == func
1729 /* Return -1 on error or 1 on success (never 0!) */
1731 get_syscall_args(struct tcb *tcp)
1735 nargs = tcp->s_ent->nargs;
1737 #if defined(S390) || defined(S390X)
1738 for (i = 0; i < nargs; ++i)
1739 if (upeek(tcp->pid, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1741 #elif defined(ALPHA)
1742 for (i = 0; i < nargs; ++i)
1743 if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1746 if (!ia64_ia32mode) {
1747 unsigned long *out0, cfm, sof, sol;
1749 /* be backwards compatible with kernel < 2.4.4... */
1751 # define PT_RBS_END PT_AR_BSP
1754 if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
1756 if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
1759 sof = (cfm >> 0) & 0x7f;
1760 sol = (cfm >> 7) & 0x7f;
1761 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1763 for (i = 0; i < nargs; ++i) {
1764 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1765 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1769 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1770 PT_R9 /* ECX = out1 */,
1771 PT_R10 /* EDX = out2 */,
1772 PT_R14 /* ESI = out3 */,
1773 PT_R15 /* EDI = out4 */,
1774 PT_R13 /* EBP = out5 */};
1776 for (i = 0; i < nargs; ++i) {
1777 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
1779 /* truncate away IVE sign-extension */
1780 tcp->u_arg[i] &= 0xffffffff;
1783 #elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
1784 /* N32 and N64 both use up to six registers. */
1785 unsigned long long regs[38];
1787 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1790 for (i = 0; i < nargs; ++i) {
1791 tcp->u_arg[i] = regs[REG_A0 + i];
1792 # if defined(LINUX_MIPSN32)
1793 tcp->ext_arg[i] = regs[REG_A0 + i];
1800 if (upeek(tcp->pid, REG_SP, &sp) < 0)
1802 for (i = 0; i < 4; ++i)
1803 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1805 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
1806 (char *)(tcp->u_arg + 4));
1808 for (i = 0; i < nargs; ++i)
1809 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1812 #elif defined(POWERPC)
1815 tcp->u_arg[0] = ppc_regs.orig_gpr3;
1816 tcp->u_arg[1] = ppc_regs.gpr[4];
1817 tcp->u_arg[2] = ppc_regs.gpr[5];
1818 tcp->u_arg[3] = ppc_regs.gpr[6];
1819 tcp->u_arg[4] = ppc_regs.gpr[7];
1820 tcp->u_arg[5] = ppc_regs.gpr[8];
1821 #elif defined(SPARC) || defined(SPARC64)
1822 for (i = 0; i < nargs; ++i)
1823 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
1825 for (i = 0; i < nargs; ++i)
1826 if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1828 #elif defined(ARM) || defined(AARCH64)
1829 # if defined(AARCH64)
1830 if (tcp->currpers == 1)
1831 for (i = 0; i < nargs; ++i)
1832 tcp->u_arg[i] = aarch64_regs.regs[i];
1835 for (i = 0; i < nargs; ++i)
1836 tcp->u_arg[i] = arm_regs.uregs[i];
1837 #elif defined(AVR32)
1840 tcp->u_arg[0] = avr32_regs.r12;
1841 tcp->u_arg[1] = avr32_regs.r11;
1842 tcp->u_arg[2] = avr32_regs.r10;
1843 tcp->u_arg[3] = avr32_regs.r9;
1844 tcp->u_arg[4] = avr32_regs.r5;
1845 tcp->u_arg[5] = avr32_regs.r3;
1847 static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
1849 for (i = 0; i < nargs; ++i)
1850 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
1853 static const int syscall_regs[MAX_ARGS] = {
1854 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1855 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
1858 for (i = 0; i < nargs; ++i)
1859 if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
1863 /* Registers used by SH5 Linux system calls for parameters */
1864 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
1866 for (i = 0; i < nargs; ++i)
1867 if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1872 tcp->u_arg[0] = i386_regs.ebx;
1873 tcp->u_arg[1] = i386_regs.ecx;
1874 tcp->u_arg[2] = i386_regs.edx;
1875 tcp->u_arg[3] = i386_regs.esi;
1876 tcp->u_arg[4] = i386_regs.edi;
1877 tcp->u_arg[5] = i386_regs.ebp;
1878 #elif defined(X86_64) || defined(X32)
1881 if (x86_io.iov_len != sizeof(i386_regs)) {
1882 /* x86-64 or x32 ABI */
1883 tcp->u_arg[0] = x86_64_regs.rdi;
1884 tcp->u_arg[1] = x86_64_regs.rsi;
1885 tcp->u_arg[2] = x86_64_regs.rdx;
1886 tcp->u_arg[3] = x86_64_regs.r10;
1887 tcp->u_arg[4] = x86_64_regs.r8;
1888 tcp->u_arg[5] = x86_64_regs.r9;
1890 tcp->ext_arg[0] = x86_64_regs.rdi;
1891 tcp->ext_arg[1] = x86_64_regs.rsi;
1892 tcp->ext_arg[2] = x86_64_regs.rdx;
1893 tcp->ext_arg[3] = x86_64_regs.r10;
1894 tcp->ext_arg[4] = x86_64_regs.r8;
1895 tcp->ext_arg[5] = x86_64_regs.r9;
1899 /* Zero-extend from 32 bits */
1900 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1901 * if you need to use *sign-extended* parameter.
1903 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1904 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1905 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1906 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1907 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1908 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
1910 #elif defined(MICROBLAZE)
1911 for (i = 0; i < nargs; ++i)
1912 if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1914 #elif defined(CRISV10) || defined(CRISV32)
1915 static const int crisregs[MAX_ARGS] = {
1916 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
1917 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
1920 for (i = 0; i < nargs; ++i)
1921 if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
1924 for (i = 0; i < nargs; ++i)
1925 tcp->u_arg[i] = tile_regs.regs[i];
1927 for (i = 0; i < nargs; ++i)
1928 if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1932 for (i = 0; i < 6; ++i)
1933 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
1934 #elif defined(METAG)
1935 for (i = 0; i < nargs; i++)
1936 /* arguments go backwards from D1Ar1 (D1.3) */
1937 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
1938 #elif defined(XTENSA)
1939 /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
1940 static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
1941 for (i = 0; i < nargs; ++i)
1942 if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
1945 long *arc_args = &arc_regs.scratch.r0;
1946 for (i = 0; i < nargs; ++i)
1947 tcp->u_arg[i] = *arc_args--;
1949 #else /* Other architecture (32bits specific) */
1950 for (i = 0; i < nargs; ++i)
1951 if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
1958 trace_syscall_entering(struct tcb *tcp)
1962 #if defined TCB_WAITEXECVE
1963 if (tcp->flags & TCB_WAITEXECVE) {
1964 /* This is the post-execve SIGTRAP. */
1965 tcp->flags &= ~TCB_WAITEXECVE;
1970 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
1974 res = syscall_fixup_on_sysenter(tcp);
1978 res = get_syscall_args(tcp);
1984 tprints("????" /* anti-trigraph gap */ "(");
1985 else if (tcp->qual_flg & UNDEFINED_SCNO)
1986 tprintf("%s(", undefined_scno_name(tcp));
1988 tprintf("%s(", tcp->s_ent->sys_name);
1990 * " <unavailable>" will be added later by the code which
1991 * detects ptrace errors.
1996 if ( sys_execve == tcp->s_ent->sys_func
1997 # if defined(SPARC) || defined(SPARC64)
1998 || sys_execv == tcp->s_ent->sys_func
2001 hide_log_until_execve = 0;
2004 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
2006 # ifdef SYS_socket_subcall
2007 if (tcp->s_ent->sys_func == sys_socketcall) {
2008 decode_socket_subcall(tcp);
2012 # ifdef SYS_ipc_subcall
2013 if (tcp->s_ent->sys_func == sys_ipc) {
2014 decode_ipc_subcall(tcp);
2022 if (need_fork_exec_workarounds)
2023 syscall_fixup_for_fork_exec(tcp);
2025 if (!(tcp->qual_flg & QUAL_TRACE)
2026 || (tracing_paths && !pathtrace_match(tcp))
2028 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
2032 tcp->flags &= ~TCB_FILTERED;
2034 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
2040 if (tcp->qual_flg & UNDEFINED_SCNO)
2041 tprintf("%s(", undefined_scno_name(tcp));
2043 tprintf("%s(", tcp->s_ent->sys_name);
2044 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
2045 res = printargs(tcp);
2047 res = tcp->s_ent->sys_func(tcp);
2051 tcp->flags |= TCB_INSYSCALL;
2052 /* Measure the entrance time as late as possible to avoid errors. */
2054 gettimeofday(&tcp->etime, NULL);
2059 * 1: ok, continue in trace_syscall_exiting().
2060 * -1: error, trace_syscall_exiting() should print error indicator
2061 * ("????" etc) and bail out.
2064 get_syscall_result(struct tcb *tcp)
2066 #if defined(S390) || defined(S390X)
2067 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
2069 #elif defined(POWERPC)
2070 /* already done by get_regs */
2071 #elif defined(AVR32)
2072 /* already done by get_regs */
2074 if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
2077 /* already done by get_regs */
2078 #elif defined(X86_64) || defined(X32)
2079 /* already done by get_regs */
2081 # define IA64_PSR_IS ((long)1 << 34)
2083 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
2084 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
2085 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
2087 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
2090 /* already done by get_regs */
2091 #elif defined(AARCH64)
2092 /* register reading already done by get_regs */
2094 /* Used to do this, but we did it on syscall entry already: */
2095 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2096 * else it's personality 0.
2098 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
2100 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
2102 #elif defined(LINUX_MIPSN32)
2103 unsigned long long regs[38];
2105 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
2107 mips_a3 = regs[REG_A3];
2108 mips_r2 = regs[REG_V0];
2110 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
2112 if (upeek(tcp->pid, REG_V0, &mips_r2) < 0)
2114 #elif defined(ALPHA)
2115 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
2117 if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
2119 #elif defined(SPARC) || defined(SPARC64)
2120 /* already done by get_regs */
2122 if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
2125 /* new syscall ABI returns result in R0 */
2126 if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
2129 /* ABI defines result returned in r9 */
2130 if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
2132 #elif defined(CRISV10) || defined(CRISV32)
2133 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
2136 /* already done by get_regs */
2137 #elif defined(MICROBLAZE)
2138 if (upeek(tcp->pid, 3 * 4, µblaze_r3) < 0)
2141 /* already done by get_regs */
2142 #elif defined(METAG)
2143 /* already done by get_regs */
2144 #elif defined(XTENSA)
2145 if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
2148 /* already done by get_regs */
2153 /* Called at each syscall exit */
2155 syscall_fixup_on_sysexit(struct tcb *tcp)
2157 #if defined(S390) || defined(S390X)
2158 if ((tcp->flags & TCB_WAITEXECVE)
2159 && (s390_gpr2 == -ENOSYS || s390_gpr2 == tcp->scno)) {
2161 * Return from execve.
2162 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2163 * flag set for the post-execve SIGTRAP to see and reset.
2171 * Check the syscall return value register value for whether it is
2172 * a negated errno code indicating an error, or a success return value.
2175 is_negated_errno(unsigned long int val)
2177 unsigned long int max = -(long int) nerrnos;
2178 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
2179 if (current_wordsize < sizeof(val)) {
2180 val = (unsigned int) val;
2181 max = (unsigned int) max;
2189 is_negated_errno_x32(unsigned long long val)
2191 unsigned long long max = -(long long) nerrnos;
2193 * current_wordsize is 4 even in personality 0 (native X32)
2194 * but truncation _must not_ be done in it.
2195 * can't check current_wordsize here!
2197 if (current_personality != 0) {
2198 val = (uint32_t) val;
2199 max = (uint32_t) max;
2206 * 1: ok, continue in trace_syscall_exiting().
2207 * -1: error, trace_syscall_exiting() should print error indicator
2208 * ("????" etc) and bail out.
2211 get_error(struct tcb *tcp)
2214 int check_errno = 1;
2215 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
2218 #if defined(S390) || defined(S390X)
2219 if (check_errno && is_negated_errno(s390_gpr2)) {
2221 u_error = -s390_gpr2;
2224 tcp->u_rval = s390_gpr2;
2227 if (check_errno && is_negated_errno(i386_regs.eax)) {
2229 u_error = -i386_regs.eax;
2232 tcp->u_rval = i386_regs.eax;
2234 #elif defined(X86_64)
2236 if (x86_io.iov_len == sizeof(i386_regs)) {
2237 /* Sign extend from 32 bits */
2238 rax = (int32_t)i386_regs.eax;
2240 rax = x86_64_regs.rax;
2242 if (check_errno && is_negated_errno(rax)) {
2250 /* In X32, return value is 64-bit (llseek uses one).
2251 * Using merely "long rax" would not work.
2254 if (x86_io.iov_len == sizeof(i386_regs)) {
2255 /* Sign extend from 32 bits */
2256 rax = (int32_t)i386_regs.eax;
2258 rax = x86_64_regs.rax;
2260 /* Careful: is_negated_errno() works only on longs */
2261 if (check_errno && is_negated_errno_x32(rax)) {
2266 tcp->u_rval = rax; /* truncating */
2270 if (ia64_ia32mode) {
2274 if (check_errno && is_negated_errno(err)) {
2282 if (check_errno && ia64_r10) {
2286 tcp->u_rval = ia64_r8;
2290 if (check_errno && mips_a3) {
2294 tcp->u_rval = mips_r2;
2295 # if defined(LINUX_MIPSN32)
2296 tcp->u_lrval = mips_r2;
2299 #elif defined(POWERPC)
2300 if (check_errno && (ppc_regs.ccr & 0x10000000)) {
2302 u_error = ppc_regs.gpr[3];
2305 tcp->u_rval = ppc_regs.gpr[3];
2308 if (check_errno && is_negated_errno(m68k_d0)) {
2313 tcp->u_rval = m68k_d0;
2315 #elif defined(ARM) || defined(AARCH64)
2316 # if defined(AARCH64)
2317 if (tcp->currpers == 1) {
2318 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2320 u_error = -aarch64_regs.regs[0];
2323 tcp->u_rval = aarch64_regs.regs[0];
2329 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
2331 u_error = -arm_regs.ARM_r0;
2334 tcp->u_rval = arm_regs.ARM_r0;
2337 #elif defined(AVR32)
2338 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
2340 u_error = -avr32_regs.r12;
2343 tcp->u_rval = avr32_regs.r12;
2346 if (check_errno && is_negated_errno(bfin_r0)) {
2350 tcp->u_rval = bfin_r0;
2352 #elif defined(ALPHA)
2353 if (check_errno && alpha_a3) {
2358 tcp->u_rval = alpha_r0;
2360 #elif defined(SPARC)
2361 if (check_errno && sparc_regs.psr & PSR_C) {
2363 u_error = sparc_regs.u_regs[U_REG_O0];
2366 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
2368 #elif defined(SPARC64)
2369 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
2371 u_error = sparc_regs.u_regs[U_REG_O0];
2374 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
2377 if (check_errno && is_negated_errno(hppa_r28)) {
2379 u_error = -hppa_r28;
2382 tcp->u_rval = hppa_r28;
2385 if (check_errno && is_negated_errno(sh_r0)) {
2390 tcp->u_rval = sh_r0;
2393 if (check_errno && is_negated_errno(sh64_r9)) {
2398 tcp->u_rval = sh64_r9;
2400 #elif defined(METAG)
2401 /* result pointer in D0Re0 (D0.0) */
2402 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2404 u_error = -metag_regs.dx[0][0];
2407 tcp->u_rval = metag_regs.dx[0][0];
2409 #elif defined(CRISV10) || defined(CRISV32)
2410 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
2412 u_error = -cris_r10;
2415 tcp->u_rval = cris_r10;
2419 * The standard tile calling convention returns the value (or negative
2420 * errno) in r0, and zero (or positive errno) in r1.
2421 * Until at least kernel 3.8, however, the r1 value is not reflected
2422 * in ptregs at this point, so we use r0 here.
2424 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
2426 u_error = -tile_regs.regs[0];
2428 tcp->u_rval = tile_regs.regs[0];
2430 #elif defined(MICROBLAZE)
2431 if (check_errno && is_negated_errno(microblaze_r3)) {
2433 u_error = -microblaze_r3;
2436 tcp->u_rval = microblaze_r3;
2439 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2441 u_error = -or1k_regs.gpr[11];
2444 tcp->u_rval = or1k_regs.gpr[11];
2446 #elif defined(XTENSA)
2447 if (check_errno && is_negated_errno(xtensa_a2)) {
2449 u_error = -xtensa_a2;
2452 tcp->u_rval = xtensa_a2;
2455 if (check_errno && is_negated_errno(arc_regs.scratch.r0)) {
2457 u_error = -arc_regs.scratch.r0;
2460 tcp->u_rval = arc_regs.scratch.r0;
2463 tcp->u_error = u_error;
2467 dumpio(struct tcb *tcp)
2473 if ((unsigned long) tcp->u_arg[0] >= num_quals)
2475 func = tcp->s_ent->sys_func;
2476 if (func == printargs)
2478 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
2479 if (func == sys_read ||
2480 func == sys_pread ||
2482 func == sys_recvfrom)
2483 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
2484 else if (func == sys_readv)
2485 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2488 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
2489 if (func == sys_write ||
2490 func == sys_pwrite ||
2493 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
2494 else if (func == sys_writev)
2495 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2501 trace_syscall_exiting(struct tcb *tcp)
2508 /* Measure the exit time as early as possible to avoid errors. */
2510 gettimeofday(&tv, NULL);
2512 #if SUPPORTED_PERSONALITIES > 1
2513 update_personality(tcp, tcp->currpers);
2515 res = (get_regs_error ? -1 : get_syscall_result(tcp));
2517 syscall_fixup_on_sysexit(tcp); /* never fails */
2518 get_error(tcp); /* never fails */
2519 if (need_fork_exec_workarounds)
2520 syscall_fixup_for_fork_exec(tcp);
2521 if (filtered(tcp) || hide_log_until_execve)
2526 count_syscall(tcp, &tv);
2527 if (cflag == CFLAG_ONLY_STATS) {
2532 /* If not in -ff mode, and printing_tcp != tcp,
2533 * then the log currently does not end with output
2534 * of _our syscall entry_, but with something else.
2535 * We need to say which syscall's return is this.
2537 * Forced reprinting via TCB_REPRINT is used only by
2538 * "strace -ff -oLOG test/threaded_execve" corner case.
2539 * It's the only case when -ff mode needs reprinting.
2541 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2542 tcp->flags &= ~TCB_REPRINT;
2544 if (tcp->qual_flg & UNDEFINED_SCNO)
2545 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
2547 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
2552 /* There was error in one of prior ptrace ops */
2555 tprints("= ? <unavailable>\n");
2557 tcp->flags &= ~TCB_INSYSCALL;
2562 if (tcp->qual_flg & QUAL_RAW) {
2563 /* sys_res = printargs(tcp); - but it's nop on sysexit */
2565 /* FIXME: not_failing_only (IOW, option -z) is broken:
2566 * failure of syscall is known only after syscall return.
2567 * Thus we end up with something like this on, say, ENOENT:
2568 * open("doesnt_exist", O_RDONLY <unfinished ...>
2569 * {next syscall decode}
2570 * whereas the intended result is that open(...) line
2571 * is not shown at all.
2573 if (not_failing_only && tcp->u_error)
2574 goto ret; /* ignore failed syscalls */
2575 sys_res = tcp->s_ent->sys_func(tcp);
2580 u_error = tcp->u_error;
2581 if (tcp->qual_flg & QUAL_RAW) {
2583 tprintf("= -1 (errno %ld)", u_error);
2585 tprintf("= %#lx", tcp->u_rval);
2587 else if (!(sys_res & RVAL_NONE) && u_error) {
2589 /* Blocked signals do not interrupt any syscalls.
2590 * In this case syscalls don't return ERESTARTfoo codes.
2592 * Deadly signals set to SIG_DFL interrupt syscalls
2593 * and kill the process regardless of which of the codes below
2594 * is returned by the interrupted syscall.
2595 * In some cases, kernel forces a kernel-generated deadly
2596 * signal to be unblocked and set to SIG_DFL (and thus cause
2597 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2598 * or SIGILL. (The alternative is to leave process spinning
2599 * forever on the faulty instruction - not useful).
2601 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2602 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2603 * but kernel will always restart them.
2606 /* Most common type of signal-interrupted syscall exit code.
2607 * The system call will be restarted with the same arguments
2608 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2610 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
2612 case ERESTARTNOINTR:
2613 /* Rare. For example, fork() returns this if interrupted.
2614 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2616 tprints("= ? ERESTARTNOINTR (To be restarted)");
2618 case ERESTARTNOHAND:
2619 /* pause(), rt_sigsuspend() etc use this code.
2620 * SA_RESTART is ignored (assumed not set):
2621 * syscall won't restart (will return EINTR instead)
2622 * even after signal with SA_RESTART set. However,
2623 * after SIG_IGN or SIG_DFL signal it will restart
2624 * (thus the name "restart only if has no handler").
2626 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
2628 case ERESTART_RESTARTBLOCK:
2629 /* Syscalls like nanosleep(), poll() which can't be
2630 * restarted with their original arguments use this
2631 * code. Kernel will execute restart_syscall() instead,
2632 * which changes arguments before restarting syscall.
2633 * SA_RESTART is ignored (assumed not set) similarly
2634 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2635 * since restart data is saved in "restart block"
2636 * in task struct, and if signal handler uses a syscall
2637 * which in turn saves another such restart block,
2638 * old data is lost and restart becomes impossible)
2640 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
2644 tprintf("= -1 E??? (errno %ld)", u_error);
2645 else if (u_error < nerrnos)
2646 tprintf("= -1 %s (%s)", errnoent[u_error],
2649 tprintf("= -1 ERRNO_%ld (%s)", u_error,
2653 if ((sys_res & RVAL_STR) && tcp->auxstr)
2654 tprintf(" (%s)", tcp->auxstr);
2657 if (sys_res & RVAL_NONE)
2660 switch (sys_res & RVAL_MASK) {
2662 tprintf("= %#lx", tcp->u_rval);
2665 tprintf("= %#lo", tcp->u_rval);
2668 tprintf("= %lu", tcp->u_rval);
2671 tprintf("= %ld", tcp->u_rval);
2673 #if defined(LINUX_MIPSN32) || defined(X32)
2676 tprintf("= %#llx", tcp->u_lrval);
2679 tprintf("= %#llo", tcp->u_lrval);
2682 case RVAL_LUDECIMAL:
2683 tprintf("= %llu", tcp->u_lrval);
2687 tprintf("= %lld", tcp->u_lrval);
2693 "invalid rval format\n");
2697 if ((sys_res & RVAL_STR) && tcp->auxstr)
2698 tprintf(" (%s)", tcp->auxstr);
2701 tv_sub(&tv, &tv, &tcp->etime);
2702 tprintf(" <%ld.%06ld>",
2703 (long) tv.tv_sec, (long) tv.tv_usec);
2709 #ifdef USE_LIBUNWIND
2710 if (stack_trace_enabled)
2711 unwind_print_stacktrace(tcp);
2715 tcp->flags &= ~TCB_INSYSCALL;
2720 trace_syscall(struct tcb *tcp)
2722 return exiting(tcp) ?
2723 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);