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 # ifndef PTRACE_PEEKUSR
41 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
43 #elif defined(HAVE_LINUX_PTRACE_H)
44 # undef PTRACE_SYSCALL
45 # ifdef HAVE_STRUCT_IA64_FPREG
46 # define ia64_fpreg XXX_ia64_fpreg
48 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
49 # define pt_all_user_regs XXX_pt_all_user_regs
51 # include <linux/ptrace.h>
53 # undef pt_all_user_regs
57 # undef PTRACE_GETREGS
58 # define PTRACE_GETREGS PTRACE_GETREGS64
59 # undef PTRACE_SETREGS
60 # define PTRACE_SETREGS PTRACE_SETREGS64
64 # include <asm/ptrace_offsets.h>
69 # include <asm/ptrace.h>
75 # ifndef PT_FLAGS_COMPAT
76 # define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
81 # define ERESTARTSYS 512
83 #ifndef ERESTARTNOINTR
84 # define ERESTARTNOINTR 513
86 #ifndef ERESTARTNOHAND
87 # define ERESTARTNOHAND 514 /* restart if no handler */
89 #ifndef ERESTART_RESTARTBLOCK
90 # define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
94 # warning: NSIG is not defined, using 32
98 /* Ugh. Is this really correct? ARM has no RT signals?! */
105 /* Define these shorthand notations to simplify the syscallent files. */
106 #define TD TRACE_DESC
107 #define TF TRACE_FILE
109 #define TN TRACE_NETWORK
110 #define TP TRACE_PROCESS
111 #define TS TRACE_SIGNAL
112 #define TM TRACE_MEMORY
113 #define NF SYSCALL_NEVER_FAILS
116 static const struct sysent sysent0[] = {
117 #include "syscallent.h"
120 #if SUPPORTED_PERSONALITIES >= 2
121 static const struct sysent sysent1[] = {
122 # include "syscallent1.h"
126 #if SUPPORTED_PERSONALITIES >= 3
127 static const struct sysent sysent2[] = {
128 # include "syscallent2.h"
132 /* Now undef them since short defines cause wicked namespace pollution. */
144 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
145 * program `ioctlsort', such that the list is sorted by the `code' field.
146 * This has the side-effect of resolving the _IO.. macros into
147 * plain integers, eliminating the need to include here everything
151 static const char *const errnoent0[] = {
152 #include "errnoent.h"
154 static const char *const signalent0[] = {
155 #include "signalent.h"
157 static const struct ioctlent ioctlent0[] = {
158 #include "ioctlent.h"
160 enum { nsyscalls0 = ARRAY_SIZE(sysent0) };
161 enum { nerrnos0 = ARRAY_SIZE(errnoent0) };
162 enum { nsignals0 = ARRAY_SIZE(signalent0) };
163 enum { nioctlents0 = ARRAY_SIZE(ioctlent0) };
164 int qual_flags0[MAX_QUALS];
166 #if SUPPORTED_PERSONALITIES >= 2
167 static const char *const errnoent1[] = {
168 # include "errnoent1.h"
170 static const char *const signalent1[] = {
171 # include "signalent1.h"
173 static const struct ioctlent ioctlent1[] = {
174 # include "ioctlent1.h"
176 enum { nsyscalls1 = ARRAY_SIZE(sysent1) };
177 enum { nerrnos1 = ARRAY_SIZE(errnoent1) };
178 enum { nsignals1 = ARRAY_SIZE(signalent1) };
179 enum { nioctlents1 = ARRAY_SIZE(ioctlent1) };
180 int qual_flags1[MAX_QUALS];
183 #if SUPPORTED_PERSONALITIES >= 3
184 static const char *const errnoent2[] = {
185 # include "errnoent2.h"
187 static const char *const signalent2[] = {
188 # include "signalent2.h"
190 static const struct ioctlent ioctlent2[] = {
191 # include "ioctlent2.h"
193 enum { nsyscalls2 = ARRAY_SIZE(sysent2) };
194 enum { nerrnos2 = ARRAY_SIZE(errnoent2) };
195 enum { nsignals2 = ARRAY_SIZE(signalent2) };
196 enum { nioctlents2 = ARRAY_SIZE(ioctlent2) };
197 int qual_flags2[MAX_QUALS];
200 const struct sysent *sysent = sysent0;
201 const char *const *errnoent = errnoent0;
202 const char *const *signalent = signalent0;
203 const struct ioctlent *ioctlent = ioctlent0;
204 unsigned nsyscalls = nsyscalls0;
205 unsigned nerrnos = nerrnos0;
206 unsigned nsignals = nsignals0;
207 unsigned nioctlents = nioctlents0;
208 int *qual_flags = qual_flags0;
210 #if SUPPORTED_PERSONALITIES > 1
211 int current_personality;
213 const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
214 PERSONALITY0_WORDSIZE,
215 PERSONALITY1_WORDSIZE,
216 # if SUPPORTED_PERSONALITIES > 2
217 PERSONALITY2_WORDSIZE,
222 set_personality(int personality)
224 switch (personality) {
226 errnoent = errnoent0;
229 nsyscalls = nsyscalls0;
230 ioctlent = ioctlent0;
231 nioctlents = nioctlents0;
232 signalent = signalent0;
233 nsignals = nsignals0;
234 qual_flags = qual_flags0;
238 errnoent = errnoent1;
241 nsyscalls = nsyscalls1;
242 ioctlent = ioctlent1;
243 nioctlents = nioctlents1;
244 signalent = signalent1;
245 nsignals = nsignals1;
246 qual_flags = qual_flags1;
249 # if SUPPORTED_PERSONALITIES >= 3
251 errnoent = errnoent2;
254 nsyscalls = nsyscalls2;
255 ioctlent = ioctlent2;
256 nioctlents = nioctlents2;
257 signalent = signalent2;
258 nsignals = nsignals2;
259 qual_flags = qual_flags2;
264 current_personality = personality;
268 update_personality(struct tcb *tcp, int personality)
270 if (personality == current_personality)
272 set_personality(personality);
274 if (personality == tcp->currpers)
276 tcp->currpers = personality;
278 # if defined(POWERPC64)
280 static const char *const names[] = {"64 bit", "32 bit"};
281 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
282 tcp->pid, names[personality]);
284 # elif defined(X86_64)
286 static const char *const names[] = {"64 bit", "32 bit", "x32"};
287 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
288 tcp->pid, names[personality]);
292 static const char *const names[] = {"x32", "32 bit"};
293 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
294 tcp->pid, names[personality]);
296 # elif defined(AARCH64)
298 static const char *const names[] = {"32-bit", "AArch64"};
299 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
300 tcp->pid, names[personality]);
304 static const char *const names[] = {"64-bit", "32-bit"};
305 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
306 tcp->pid, names[personality]);
312 static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
314 static const struct qual_options {
316 const char *option_name;
317 int (*qualify)(const char *, int, int);
318 const char *argument_name;
320 { QUAL_TRACE, "trace", qual_syscall, "system call" },
321 { QUAL_TRACE, "t", qual_syscall, "system call" },
322 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
323 { QUAL_ABBREV, "a", qual_syscall, "system call" },
324 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
325 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
326 { QUAL_RAW, "raw", qual_syscall, "system call" },
327 { QUAL_RAW, "x", qual_syscall, "system call" },
328 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
329 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
330 { QUAL_SIGNAL, "s", qual_signal, "signal" },
331 { QUAL_FAULT, "fault", qual_fault, "fault" },
332 { QUAL_FAULT, "faults", qual_fault, "fault" },
333 { QUAL_FAULT, "m", qual_fault, "fault" },
334 { QUAL_READ, "read", qual_desc, "descriptor" },
335 { QUAL_READ, "reads", qual_desc, "descriptor" },
336 { QUAL_READ, "r", qual_desc, "descriptor" },
337 { QUAL_WRITE, "write", qual_desc, "descriptor" },
338 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
339 { QUAL_WRITE, "w", qual_desc, "descriptor" },
340 { 0, NULL, NULL, NULL },
344 qualify_one(int n, int bitflag, int not, int pers)
346 if (pers == 0 || pers < 0) {
348 qual_flags0[n] &= ~bitflag;
350 qual_flags0[n] |= bitflag;
353 #if SUPPORTED_PERSONALITIES >= 2
354 if (pers == 1 || pers < 0) {
356 qual_flags1[n] &= ~bitflag;
358 qual_flags1[n] |= bitflag;
362 #if SUPPORTED_PERSONALITIES >= 3
363 if (pers == 2 || pers < 0) {
365 qual_flags2[n] &= ~bitflag;
367 qual_flags2[n] |= bitflag;
373 qual_syscall(const char *s, int bitflag, int not)
378 if (*s >= '0' && *s <= '9') {
379 int i = string_to_uint(s);
380 if (i < 0 || i >= MAX_QUALS)
382 qualify_one(i, bitflag, not, -1);
385 for (i = 0; i < nsyscalls0; i++)
386 if (sysent0[i].sys_name &&
387 strcmp(s, sysent0[i].sys_name) == 0) {
388 qualify_one(i, bitflag, not, 0);
392 #if SUPPORTED_PERSONALITIES >= 2
393 for (i = 0; i < nsyscalls1; i++)
394 if (sysent1[i].sys_name &&
395 strcmp(s, sysent1[i].sys_name) == 0) {
396 qualify_one(i, bitflag, not, 1);
401 #if SUPPORTED_PERSONALITIES >= 3
402 for (i = 0; i < nsyscalls2; i++)
403 if (sysent2[i].sys_name &&
404 strcmp(s, sysent2[i].sys_name) == 0) {
405 qualify_one(i, bitflag, not, 2);
414 qual_signal(const char *s, int bitflag, int not)
418 if (*s >= '0' && *s <= '9') {
419 int signo = string_to_uint(s);
420 if (signo < 0 || signo >= MAX_QUALS)
422 qualify_one(signo, bitflag, not, -1);
425 if (strncasecmp(s, "SIG", 3) == 0)
427 for (i = 0; i <= NSIG; i++) {
428 if (strcasecmp(s, signame(i) + 3) == 0) {
429 qualify_one(i, bitflag, not, -1);
437 qual_fault(const char *s, int bitflag, int not)
443 qual_desc(const char *s, int bitflag, int not)
445 if (*s >= '0' && *s <= '9') {
446 int desc = string_to_uint(s);
447 if (desc < 0 || desc >= MAX_QUALS)
449 qualify_one(desc, bitflag, not, -1);
456 lookup_class(const char *s)
458 if (strcmp(s, "file") == 0)
460 if (strcmp(s, "ipc") == 0)
462 if (strcmp(s, "network") == 0)
463 return TRACE_NETWORK;
464 if (strcmp(s, "process") == 0)
465 return TRACE_PROCESS;
466 if (strcmp(s, "signal") == 0)
468 if (strcmp(s, "desc") == 0)
470 if (strcmp(s, "memory") == 0)
476 qualify(const char *s)
478 const struct qual_options *opt;
484 opt = &qual_options[0];
485 for (i = 0; (p = qual_options[i].option_name); i++) {
487 if (strncmp(s, p, n) == 0 && s[n] == '=') {
488 opt = &qual_options[i];
498 if (strcmp(s, "none") == 0) {
502 if (strcmp(s, "all") == 0) {
503 for (i = 0; i < MAX_QUALS; i++) {
504 qualify_one(i, opt->bitflag, not, -1);
508 for (i = 0; i < MAX_QUALS; i++) {
509 qualify_one(i, opt->bitflag, !not, -1);
514 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
515 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
516 for (i = 0; i < nsyscalls0; i++)
517 if (sysent0[i].sys_flags & n)
518 qualify_one(i, opt->bitflag, not, 0);
520 #if SUPPORTED_PERSONALITIES >= 2
521 for (i = 0; i < nsyscalls1; i++)
522 if (sysent1[i].sys_flags & n)
523 qualify_one(i, opt->bitflag, not, 1);
526 #if SUPPORTED_PERSONALITIES >= 3
527 for (i = 0; i < nsyscalls2; i++)
528 if (sysent2[i].sys_flags & n)
529 qualify_one(i, opt->bitflag, not, 2);
534 if (opt->qualify(p, opt->bitflag, not)) {
535 error_msg_and_die("invalid %s '%s'",
536 opt->argument_name, p);
543 #ifdef SYS_socket_subcall
545 decode_socket_subcall(struct tcb *tcp)
548 unsigned int i, size;
550 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
553 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
554 addr = tcp->u_arg[1];
555 tcp->u_nargs = sysent[tcp->scno].nargs;
556 size = current_wordsize;
557 for (i = 0; i < tcp->u_nargs; ++i) {
558 if (size == sizeof(int)) {
560 if (umove(tcp, addr, &arg) < 0)
566 if (umove(tcp, addr, &arg) < 0)
575 #ifdef SYS_ipc_subcall
577 decode_ipc_subcall(struct tcb *tcp)
581 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
584 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
585 tcp->u_nargs = sysent[tcp->scno].nargs;
586 for (i = 0; i < tcp->u_nargs; i++)
587 tcp->u_arg[i] = tcp->u_arg[i + 1];
592 printargs(struct tcb *tcp)
597 for (i = 0; i < tcp->u_nargs; i++)
598 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
604 printargs_lu(struct tcb *tcp)
609 for (i = 0; i < tcp->u_nargs; i++)
610 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
616 printargs_ld(struct tcb *tcp)
621 for (i = 0; i < tcp->u_nargs; i++)
622 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
627 #if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
629 getrval2(struct tcb *tcp)
633 # if defined(SPARC) || defined(SPARC64)
634 val = regs.u_regs[U_REG_O1];
636 if (upeek(tcp, 4*(REG_REG0+1), &val) < 0)
639 if (upeek(tcp, PT_R9, &val) < 0)
648 is_restart_error(struct tcb *tcp)
650 switch (tcp->u_error) {
654 case ERESTART_RESTARTBLOCK:
663 struct pt_regs i386_regs;
664 #elif defined(X86_64) || defined(X32)
666 * On 32 bits, pt_regs and user_regs_struct are the same,
667 * but on 64 bits, user_regs_struct has six more fields:
668 * fs_base, gs_base, ds, es, fs, gs.
669 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
671 static struct user_regs_struct x86_64_regs;
673 long ia32 = 0; /* not static */
674 static long ia64_r8, ia64_r10;
675 #elif defined(POWERPC)
676 static long ppc_result;
682 struct pt_regs arm_regs; /* not static */
683 #elif defined(AARCH64)
685 struct user_pt_regs aarch64;
686 struct arm_pt_regs arm;
688 # define aarch64_regs arm_regs_union.aarch64
689 # define arm_regs arm_regs_union.arm
690 static struct iovec aarch64_io = {
691 .iov_base = &aarch64_regs
695 static long alpha_a3;
697 static struct pt_regs regs;
698 #elif defined(SPARC) || defined(SPARC64)
699 struct pt_regs regs; /* not static */
700 #elif defined(LINUX_MIPSN32)
706 #elif defined(S390) || defined(S390X)
708 static long syscall_mode;
710 static long hppa_r28;
715 #elif defined(CRISV10) || defined(CRISV32)
716 static long cris_r10;
718 struct pt_regs tile_regs;
719 #elif defined(MICROBLAZE)
720 static long microblaze_r3;
724 printcall(struct tcb *tcp)
726 #define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
727 sizeof(long) == 8 ? "[????????????????] " : \
729 if (get_regs_error) {
734 tprintf("[%08lx] ", i386_regs.eip);
735 #elif defined(S390) || defined(S390X)
737 if (upeek(tcp, PT_PSWADDR, &psw) < 0) {
742 tprintf("[%08lx] ", psw);
744 tprintf("[%016lx] ", psw);
746 #elif defined(X86_64) || defined(X32)
747 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
751 if (upeek(tcp, PT_B0, &ip) < 0) {
755 tprintf("[%08lx] ", ip);
756 #elif defined(POWERPC)
759 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
764 tprintf("[%016lx] ", pc);
766 tprintf("[%08lx] ", pc);
771 if (upeek(tcp, 4*PT_PC, &pc) < 0) {
772 tprints("[????????] ");
775 tprintf("[%08lx] ", pc);
779 if (upeek(tcp, REG_PC, &pc) < 0) {
780 tprints("[????????????????] ");
783 tprintf("[%08lx] ", pc);
785 tprintf("[%08lx] ", regs.pc);
786 #elif defined(SPARC64)
787 tprintf("[%08lx] ", regs.tpc);
791 if (upeek(tcp, PT_IAOQ0, &pc) < 0) {
792 tprints("[????????] ");
795 tprintf("[%08lx] ", pc);
799 if (upeek(tcp, REG_EPC, &pc) < 0) {
800 tprints("[????????] ");
803 tprintf("[%08lx] ", pc);
807 if (upeek(tcp, 4*REG_PC, &pc) < 0) {
808 tprints("[????????] ");
811 tprintf("[%08lx] ", pc);
815 if (upeek(tcp, REG_PC, &pc) < 0) {
816 tprints("[????????????????] ");
819 tprintf("[%08lx] ", pc);
821 tprintf("[%08lx] ", arm_regs.ARM_pc);
822 #elif defined(AARCH64)
823 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
825 tprintf("[%08lx] ", regs.pc);
829 if (upeek(tcp, PT_PC, &pc) < 0) {
833 tprintf("[%08lx] ", pc);
834 #elif defined(CRISV10)
837 if (upeek(tcp, 4*PT_IRP, &pc) < 0) {
841 tprintf("[%08lx] ", pc);
842 #elif defined(CRISV32)
845 if (upeek(tcp, 4*PT_ERP, &pc) < 0) {
849 tprintf("[%08lx] ", pc);
852 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
854 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
856 #endif /* architecture */
861 void get_regs(pid_t pid)
864 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, ®s);
866 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &i386_regs);
867 # elif defined(X86_64) || defined(X32)
868 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &x86_64_regs);
870 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (void *)&arm_regs);
871 # elif defined(AARCH64)
872 /*aarch64_io.iov_base = &aarch64_regs; - already is */
873 aarch64_io.iov_len = sizeof(aarch64_regs);
874 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (void *)&aarch64_io);
876 /* Paranoia checks */
879 switch (aarch64_io.iov_len) {
880 case sizeof(aarch64_regs):
881 /* We are in 64-bit mode */
883 case sizeof(arm_regs):
884 /* We are in 32-bit mode */
891 # elif defined(SPARC) || defined(SPARC64)
892 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)®s, 0);
894 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &tile_regs);
900 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
901 * 1: ok, continue in trace_syscall_entering().
902 * other: error, trace_syscall_entering() should print error indicator
903 * ("????" etc) and bail out.
906 get_scno(struct tcb *tcp)
910 #if defined(S390) || defined(S390X)
911 if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
914 if (syscall_mode != -ENOSYS) {
916 * Since kernel version 2.5.44 the scno gets passed in gpr2.
921 * Old style of "passing" the scno via the SVC instruction.
924 long opcode, offset_reg, tmp;
926 static const int gpr_offset[16] = {
927 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
928 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
929 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
930 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
933 if (upeek(tcp, PT_PSWADDR, &psw) < 0)
936 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
938 perror_msg("%s", "peektext(psw-oneword)");
943 * We have to check if the SVC got executed directly or via an
944 * EXECUTE instruction. In case of EXECUTE it is necessary to do
945 * instruction decoding to derive the system call number.
946 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
947 * so that this doesn't work if a SVC opcode is part of an EXECUTE
948 * opcode. Since there is no way to find out the opcode size this
949 * is the best we can do...
951 if ((opcode & 0xff00) == 0x0a00) {
953 scno = opcode & 0xff;
956 /* SVC got executed by EXECUTE instruction */
959 * Do instruction decoding of EXECUTE. If you really want to
960 * understand this, read the Principles of Operations.
962 svc_addr = (void *) (opcode & 0xfff);
965 offset_reg = (opcode & 0x000f0000) >> 16;
966 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
971 offset_reg = (opcode & 0x0000f000) >> 12;
972 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
976 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
985 offset_reg = (opcode & 0x00f00000) >> 20;
986 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
989 scno = (scno | tmp) & 0xff;
992 #elif defined(POWERPC)
993 if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
996 /* TODO: speed up strace by not doing this at every syscall.
997 * We only need to do it after execve.
1002 /* Check for 64/32 bit mode. */
1003 if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
1005 /* SF is bit 0 of MSR */
1010 update_personality(tcp, currpers);
1012 #elif defined(AVR32)
1015 if (upeek(tcp, PT_ORIG_P0, &scno))
1018 scno = i386_regs.orig_eax;
1019 #elif defined(X86_64) || defined(X32)
1020 # ifndef __X32_SYSCALL_BIT
1021 # define __X32_SYSCALL_BIT 0x40000000
1023 # ifndef __X32_SYSCALL_MASK
1024 # define __X32_SYSCALL_MASK __X32_SYSCALL_BIT
1028 scno = x86_64_regs.orig_rax;
1030 /* Check CS register value. On x86-64 linux it is:
1031 * 0x33 for long mode (64 bit)
1032 * 0x23 for compatibility mode (32 bit)
1033 * Check DS register value. On x86-64 linux it is:
1034 * 0x2b for x32 mode (x86-64 in 32 bit)
1036 switch (x86_64_regs.cs) {
1037 case 0x23: currpers = 1; break;
1039 if (x86_64_regs.ds == 0x2b) {
1041 scno &= ~__X32_SYSCALL_MASK;
1046 fprintf(stderr, "Unknown value CS=0x%08X while "
1047 "detecting personality of process "
1048 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
1049 currpers = current_personality;
1053 /* This version analyzes the opcode of a syscall instruction.
1054 * (int 0x80 on i386 vs. syscall on x86-64)
1055 * It works, but is too complicated.
1057 unsigned long val, rip, i;
1059 rip = x86_64_regs.rip;
1061 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1065 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
1067 fprintf(stderr, "ptrace_peektext failed: %s\n",
1069 switch (call & 0xffff) {
1070 /* x86-64: syscall = 0x0f 0x05 */
1071 case 0x050f: currpers = 0; break;
1072 /* i386: int 0x80 = 0xcd 0x80 */
1073 case 0x80cd: currpers = 1; break;
1075 currpers = current_personality;
1077 "Unknown syscall opcode (0x%04X) while "
1078 "detecting personality of process "
1079 "PID=%d\n", (int)call, tcp->pid);
1084 /* Value of currpers:
1088 * Value of current_personality:
1094 fprintf(stderr, "syscall_%lu (...) in unsupported "
1095 "64-bit mode of process PID=%d\n",
1102 update_personality(tcp, currpers);
1104 # define IA64_PSR_IS ((long)1 << 34)
1106 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
1107 ia32 = (psr & IA64_PSR_IS) != 0;
1109 if (upeek(tcp, PT_R1, &scno) < 0)
1112 if (upeek(tcp, PT_R15, &scno) < 0)
1115 #elif defined(AARCH64)
1116 switch (aarch64_io.iov_len) {
1117 case sizeof(aarch64_regs):
1118 /* We are in 64-bit mode */
1119 scno = aarch64_regs.regs[8];
1120 update_personality(tcp, 1);
1122 case sizeof(arm_regs):
1123 /* We are in 32-bit mode */
1124 scno = arm_regs.ARM_r7;
1125 update_personality(tcp, 0);
1130 * We only need to grab the syscall number on syscall entry.
1132 if (arm_regs.ARM_ip == 0) {
1134 * Note: we only deal with 32-bit CPUs here
1136 if (arm_regs.ARM_cpsr & 0x20) {
1138 * Get the Thumb-mode system call number
1140 scno = arm_regs.ARM_r7;
1143 * Get the ARM-mode system call number
1146 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1150 /* Handle the EABI syscall convention. We do not
1151 bother converting structures between the two
1152 ABIs, but basic functionality should work even
1153 if strace and the traced program have different
1155 if (scno == 0xef000000) {
1156 scno = arm_regs.ARM_r7;
1158 if ((scno & 0x0ff00000) != 0x0f900000) {
1159 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
1165 * Fixup the syscall number
1170 if (scno & 0x0f0000) {
1172 * Handle ARM specific syscall
1174 update_personality(tcp, 1);
1177 update_personality(tcp, 0);
1180 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
1181 tcp->flags |= TCB_INSYSCALL;
1184 if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
1186 #elif defined(LINUX_MIPSN32)
1187 unsigned long long regs[38];
1189 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1195 if (!SCNO_IN_RANGE(scno)) {
1196 if (a3 == 0 || a3 == -1) {
1198 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1203 if (upeek(tcp, REG_A3, &a3) < 0)
1205 if (upeek(tcp, REG_V0, &scno) < 0)
1208 if (!SCNO_IN_RANGE(scno)) {
1209 if (a3 == 0 || a3 == -1) {
1211 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1215 #elif defined(ALPHA)
1216 if (upeek(tcp, REG_A3, &alpha_a3) < 0)
1218 if (upeek(tcp, REG_R0, &scno) < 0)
1222 * Do some sanity checks to figure out if it's
1223 * really a syscall entry
1225 if (!SCNO_IN_RANGE(scno)) {
1226 if (alpha_a3 == 0 || alpha_a3 == -1) {
1228 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
1232 #elif defined(SPARC) || defined(SPARC64)
1233 /* Disassemble the syscall trap. */
1234 /* Retrieve the syscall trap instruction. */
1237 # if defined(SPARC64)
1238 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.tpc, 0);
1241 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.pc, 0);
1246 /* Disassemble the trap to see what personality to use. */
1249 /* Linux/SPARC syscall trap. */
1250 update_personality(tcp, 0);
1253 /* Linux/SPARC64 syscall trap. */
1254 update_personality(tcp, 2);
1257 /* SunOS syscall trap. (pers 1) */
1258 fprintf(stderr, "syscall: SunOS no support\n");
1261 /* Solaris 2.x syscall trap. (per 2) */
1262 update_personality(tcp, 1);
1265 /* NetBSD/FreeBSD syscall trap. */
1266 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1269 /* Solaris 2.x gettimeofday */
1270 update_personality(tcp, 1);
1273 # if defined(SPARC64)
1274 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, regs.tpc);
1276 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, regs.pc);
1281 /* Extract the system call number from the registers. */
1282 if (trap == 0x91d02027)
1285 scno = regs.u_regs[U_REG_G1];
1287 scno = regs.u_regs[U_REG_O0];
1288 memmove(®s.u_regs[U_REG_O0], ®s.u_regs[U_REG_O1], 7*sizeof(regs.u_regs[0]));
1291 if (upeek(tcp, PT_GR20, &scno) < 0)
1295 * In the new syscall ABI, the system call number is in R3.
1297 if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
1301 /* Odd as it may seem, a glibc bug has been known to cause
1302 glibc to issue bogus negative syscall numbers. So for
1303 our purposes, make strace print what it *should* have been */
1304 long correct_scno = (scno & 0xff);
1307 "Detected glibc bug: bogus system call"
1308 " number = %ld, correcting to %ld\n",
1311 scno = correct_scno;
1314 if (upeek(tcp, REG_SYSCALL, &scno) < 0)
1317 #elif defined(CRISV10) || defined(CRISV32)
1318 if (upeek(tcp, 4*PT_R9, &scno) < 0)
1322 scno = tile_regs.regs[10];
1326 if (tile_regs.flags & PT_FLAGS_COMPAT)
1331 update_personality(tcp, currpers);
1333 if (!(tcp->flags & TCB_INSYSCALL)) {
1334 /* Check if we return from execve. */
1335 if (tcp->flags & TCB_WAITEXECVE) {
1336 tcp->flags &= ~TCB_WAITEXECVE;
1340 #elif defined(MICROBLAZE)
1341 if (upeek(tcp, 0, &scno) < 0)
1346 /* new syscall ABI returns result in R0 */
1347 if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0)
1350 /* ABI defines result returned in r9 */
1351 if (upeek(tcp, REG_GENERAL(9), (long *)&sh64_r9) < 0)
1359 /* Called at each syscall entry.
1361 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1362 * 1: ok, continue in trace_syscall_entering().
1363 * other: error, trace_syscall_entering() should print error indicator
1364 * ("????" etc) and bail out.
1367 syscall_fixup_on_sysenter(struct tcb *tcp)
1369 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
1371 if (i386_regs.eax != -ENOSYS) {
1373 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1376 #elif defined(X86_64) || defined(X32)
1378 long rax = x86_64_regs.rax;
1379 if (current_personality == 1)
1380 rax = (int)rax; /* sign extend from 32 bits */
1381 if (rax != -ENOSYS) {
1383 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1387 #elif defined(S390) || defined(S390X)
1388 /* TODO: we already fetched PT_GPR2 in get_scno
1389 * and stored it in syscall_mode, reuse it here
1390 * instead of re-fetching?
1392 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1394 if (syscall_mode != -ENOSYS)
1395 syscall_mode = tcp->scno;
1396 if (gpr2 != syscall_mode) {
1398 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
1402 /* TODO? Eliminate upeek's in arches below like we did in x86 */
1403 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
1405 if (m68k_d0 != -ENOSYS) {
1407 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
1411 if (upeek(tcp, PT_R10, &ia64_r10) < 0)
1413 if (upeek(tcp, PT_R8, &ia64_r8) < 0)
1415 if (ia32 && ia64_r8 != -ENOSYS) {
1417 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
1420 #elif defined(CRISV10) || defined(CRISV32)
1421 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
1423 if (cris_r10 != -ENOSYS) {
1425 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
1428 #elif defined(MICROBLAZE)
1429 if (upeek(tcp, 3 * 4, µblaze_r3) < 0)
1431 if (microblaze_r3 != -ENOSYS) {
1433 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
1441 internal_fork(struct tcb *tcp)
1443 #if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1444 # define ARG_FLAGS 1
1446 # define ARG_FLAGS 0
1448 #ifndef CLONE_UNTRACED
1449 # define CLONE_UNTRACED 0x00800000
1451 if ((ptrace_setoptions
1452 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1453 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1459 if (entering(tcp)) {
1461 * We won't see the new child if clone is called with
1462 * CLONE_UNTRACED, so we keep the same logic with that option
1463 * and don't trace it.
1465 if ((sysent[tcp->scno].sys_func == sys_clone) &&
1466 (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED))
1470 if (tcp->flags & TCB_BPTSET)
1475 #if defined(TCB_WAITEXECVE)
1477 internal_exec(struct tcb *tcp)
1479 /* Maybe we have post-execve SIGTRAP suppressed? */
1480 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1481 return; /* yes, no need to do anything */
1483 if (exiting(tcp) && syserror(tcp))
1484 /* Error in execve, no post-execve SIGTRAP expected */
1485 tcp->flags &= ~TCB_WAITEXECVE;
1487 tcp->flags |= TCB_WAITEXECVE;
1492 syscall_fixup_for_fork_exec(struct tcb *tcp)
1495 * We must always trace a few critical system calls in order to
1496 * correctly support following forks in the presence of tracing
1501 if (!SCNO_IN_RANGE(tcp->scno))
1504 func = sysent[tcp->scno].sys_func;
1506 if ( sys_fork == func
1507 || sys_vfork == func
1508 || sys_clone == func
1514 #if defined(TCB_WAITEXECVE)
1515 if ( sys_execve == func
1516 # if defined(SPARC) || defined(SPARC64)
1517 || sys_execv == func
1526 /* Return -1 on error or 1 on success (never 0!) */
1528 get_syscall_args(struct tcb *tcp)
1532 if (SCNO_IN_RANGE(tcp->scno))
1533 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1535 nargs = tcp->u_nargs = MAX_ARGS;
1537 #if defined(S390) || defined(S390X)
1538 for (i = 0; i < nargs; ++i)
1539 if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1541 #elif defined(ALPHA)
1542 for (i = 0; i < nargs; ++i)
1543 if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
1547 unsigned long *out0, cfm, sof, sol;
1549 /* be backwards compatible with kernel < 2.4.4... */
1551 # define PT_RBS_END PT_AR_BSP
1554 if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
1556 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1559 sof = (cfm >> 0) & 0x7f;
1560 sol = (cfm >> 7) & 0x7f;
1561 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1563 for (i = 0; i < nargs; ++i) {
1564 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1565 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1569 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1570 PT_R9 /* ECX = out1 */,
1571 PT_R10 /* EDX = out2 */,
1572 PT_R14 /* ESI = out3 */,
1573 PT_R15 /* EDI = out4 */,
1574 PT_R13 /* EBP = out5 */};
1576 for (i = 0; i < nargs; ++i) {
1577 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1579 /* truncate away IVE sign-extension */
1580 tcp->u_arg[i] &= 0xffffffff;
1583 #elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
1584 /* N32 and N64 both use up to six registers. */
1585 unsigned long long regs[38];
1587 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1590 for (i = 0; i < nargs; ++i) {
1591 tcp->u_arg[i] = regs[REG_A0 + i];
1592 # if defined(LINUX_MIPSN32)
1593 tcp->ext_arg[i] = regs[REG_A0 + i];
1600 if (upeek(tcp, REG_SP, &sp) < 0)
1602 for (i = 0; i < 4; ++i)
1603 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1605 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
1606 (char *)(tcp->u_arg + 4));
1608 for (i = 0; i < nargs; ++i)
1609 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1612 #elif defined(POWERPC)
1614 # define PT_ORIG_R3 34
1616 for (i = 0; i < nargs; ++i) {
1617 if (upeek(tcp, (i==0) ?
1618 (sizeof(unsigned long) * PT_ORIG_R3) :
1619 ((i+PT_R3) * sizeof(unsigned long)),
1620 &tcp->u_arg[i]) < 0)
1623 #elif defined(SPARC) || defined(SPARC64)
1624 for (i = 0; i < nargs; ++i)
1625 tcp->u_arg[i] = regs.u_regs[U_REG_O0 + i];
1627 for (i = 0; i < nargs; ++i)
1628 if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1630 #elif defined(ARM) || defined(AARCH64)
1631 # if defined(AARCH64)
1632 if (tcp->currpers == 1)
1633 for (i = 0; i < nargs; ++i)
1634 tcp->u_arg[i] = aarch64_regs.regs[i];
1637 for (i = 0; i < nargs; ++i)
1638 tcp->u_arg[i] = arm_regs.uregs[i];
1639 #elif defined(AVR32)
1642 tcp->u_arg[0] = regs.r12;
1643 tcp->u_arg[1] = regs.r11;
1644 tcp->u_arg[2] = regs.r10;
1645 tcp->u_arg[3] = regs.r9;
1646 tcp->u_arg[4] = regs.r5;
1647 tcp->u_arg[5] = regs.r3;
1649 static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
1651 for (i = 0; i < nargs; ++i)
1652 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1655 static const int syscall_regs[MAX_ARGS] = {
1656 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1657 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
1660 for (i = 0; i < nargs; ++i)
1661 if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
1665 /* Registers used by SH5 Linux system calls for parameters */
1666 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
1668 for (i = 0; i < nargs; ++i)
1669 if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1671 #elif defined(X86_64) || defined(X32)
1674 if (current_personality != 1) { /* x86-64 or x32 ABI */
1675 tcp->u_arg[0] = x86_64_regs.rdi;
1676 tcp->u_arg[1] = x86_64_regs.rsi;
1677 tcp->u_arg[2] = x86_64_regs.rdx;
1678 tcp->u_arg[3] = x86_64_regs.r10;
1679 tcp->u_arg[4] = x86_64_regs.r8;
1680 tcp->u_arg[5] = x86_64_regs.r9;
1682 tcp->ext_arg[0] = x86_64_regs.rdi;
1683 tcp->ext_arg[1] = x86_64_regs.rsi;
1684 tcp->ext_arg[2] = x86_64_regs.rdx;
1685 tcp->ext_arg[3] = x86_64_regs.r10;
1686 tcp->ext_arg[4] = x86_64_regs.r8;
1687 tcp->ext_arg[5] = x86_64_regs.r9;
1689 } else { /* i386 ABI */
1690 /* Sign-extend lower 32 bits */
1691 tcp->u_arg[0] = (long)(int)x86_64_regs.rbx;
1692 tcp->u_arg[1] = (long)(int)x86_64_regs.rcx;
1693 tcp->u_arg[2] = (long)(int)x86_64_regs.rdx;
1694 tcp->u_arg[3] = (long)(int)x86_64_regs.rsi;
1695 tcp->u_arg[4] = (long)(int)x86_64_regs.rdi;
1696 tcp->u_arg[5] = (long)(int)x86_64_regs.rbp;
1698 #elif defined(MICROBLAZE)
1699 for (i = 0; i < nargs; ++i)
1700 if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1702 #elif defined(CRISV10) || defined(CRISV32)
1703 static const int crisregs[MAX_ARGS] = {
1704 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
1705 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
1708 for (i = 0; i < nargs; ++i)
1709 if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0)
1712 for (i = 0; i < nargs; ++i)
1713 tcp->u_arg[i] = tile_regs.regs[i];
1715 for (i = 0; i < nargs; ++i)
1716 if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1721 tcp->u_arg[0] = i386_regs.ebx;
1722 tcp->u_arg[1] = i386_regs.ecx;
1723 tcp->u_arg[2] = i386_regs.edx;
1724 tcp->u_arg[3] = i386_regs.esi;
1725 tcp->u_arg[4] = i386_regs.edi;
1726 tcp->u_arg[5] = i386_regs.ebp;
1727 #else /* Other architecture (32bits specific) */
1728 for (i = 0; i < nargs; ++i)
1729 if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
1736 trace_syscall_entering(struct tcb *tcp)
1740 #if defined TCB_WAITEXECVE
1741 if (tcp->flags & TCB_WAITEXECVE) {
1742 /* This is the post-execve SIGTRAP. */
1743 tcp->flags &= ~TCB_WAITEXECVE;
1748 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
1752 res = syscall_fixup_on_sysenter(tcp);
1756 res = get_syscall_args(tcp);
1762 tprints("????" /* anti-trigraph gap */ "(");
1763 else if (!SCNO_IN_RANGE(tcp->scno))
1764 tprintf("syscall_%lu(", tcp->scno);
1766 tprintf("%s(", sysent[tcp->scno].sys_name);
1768 * " <unavailable>" will be added later by the code which
1769 * detects ptrace errors.
1774 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
1775 while (SCNO_IN_RANGE(tcp->scno)) {
1776 # ifdef SYS_socket_subcall
1777 if (sysent[tcp->scno].sys_func == sys_socketcall) {
1778 decode_socket_subcall(tcp);
1782 # ifdef SYS_ipc_subcall
1783 if (sysent[tcp->scno].sys_func == sys_ipc) {
1784 decode_ipc_subcall(tcp);
1790 #endif /* SYS_socket_subcall || SYS_ipc_subcall */
1792 if (need_fork_exec_workarounds)
1793 syscall_fixup_for_fork_exec(tcp);
1795 if ((SCNO_IN_RANGE(tcp->scno) &&
1796 !(qual_flags[tcp->scno] & QUAL_TRACE)) ||
1797 (tracing_paths && !pathtrace_match(tcp))) {
1798 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1802 tcp->flags &= ~TCB_FILTERED;
1804 if (cflag == CFLAG_ONLY_STATS) {
1810 if (!SCNO_IN_RANGE(tcp->scno))
1811 tprintf("syscall_%lu(", tcp->scno);
1813 tprintf("%s(", sysent[tcp->scno].sys_name);
1814 if (!SCNO_IN_RANGE(tcp->scno) ||
1815 ((qual_flags[tcp->scno] & QUAL_RAW) &&
1816 sysent[tcp->scno].sys_func != sys_exit))
1817 res = printargs(tcp);
1819 res = (*sysent[tcp->scno].sys_func)(tcp);
1823 tcp->flags |= TCB_INSYSCALL;
1824 /* Measure the entrance time as late as possible to avoid errors. */
1826 gettimeofday(&tcp->etime, NULL);
1831 * 1: ok, continue in trace_syscall_exiting().
1832 * -1: error, trace_syscall_exiting() should print error indicator
1833 * ("????" etc) and bail out.
1836 get_syscall_result(struct tcb *tcp)
1838 #if defined(S390) || defined(S390X)
1839 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1841 #elif defined(POWERPC)
1842 # define SO_MASK 0x10000000
1845 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1847 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &ppc_result) < 0)
1849 if (flags & SO_MASK)
1850 ppc_result = -ppc_result;
1852 #elif defined(AVR32)
1853 /* already done by get_regs */
1855 if (upeek(tcp, PT_R0, &r0) < 0)
1858 /* already done by get_regs */
1859 #elif defined(X86_64) || defined(X32)
1860 /* already done by get_regs */
1862 # define IA64_PSR_IS ((long)1 << 34)
1864 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
1865 ia32 = (psr & IA64_PSR_IS) != 0;
1866 if (upeek(tcp, PT_R8, &ia64_r8) < 0)
1868 if (upeek(tcp, PT_R10, &ia64_r10) < 0)
1871 /* already done by get_regs */
1872 #elif defined(AARCH64)
1873 /* register reading already done by get_regs */
1875 /* Used to do this, but we did it on syscall entry already: */
1876 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
1877 * else it's personality 0.
1879 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
1881 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
1883 #elif defined(LINUX_MIPSN32)
1884 unsigned long long regs[38];
1886 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
1891 if (upeek(tcp, REG_A3, &a3) < 0)
1893 if (upeek(tcp, REG_V0, &r2) < 0)
1895 #elif defined(ALPHA)
1896 if (upeek(tcp, REG_A3, &a3) < 0)
1898 if (upeek(tcp, REG_R0, &r0) < 0)
1900 #elif defined(SPARC) || defined(SPARC64)
1901 /* already done by get_regs */
1903 if (upeek(tcp, PT_GR28, &hppa_r28) < 0)
1906 /* new syscall ABI returns result in R0 */
1907 if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0)
1910 /* ABI defines result returned in r9 */
1911 if (upeek(tcp, REG_GENERAL(9), (long *)&sh64_r9) < 0)
1913 #elif defined(CRISV10) || defined(CRISV32)
1914 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
1917 /* already done by get_regs */
1918 #elif defined(MICROBLAZE)
1919 if (upeek(tcp, 3 * 4, µblaze_r3) < 0)
1925 /* Called at each syscall exit */
1927 syscall_fixup_on_sysexit(struct tcb *tcp)
1929 #if defined(S390) || defined(S390X)
1930 if (syscall_mode != -ENOSYS)
1931 syscall_mode = tcp->scno;
1932 if ((tcp->flags & TCB_WAITEXECVE)
1933 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1935 * Return from execve.
1936 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1937 * flag set for the post-execve SIGTRAP to see and reset.
1945 * Check the syscall return value register value for whether it is
1946 * a negated errno code indicating an error, or a success return value.
1949 is_negated_errno(unsigned long int val)
1951 unsigned long int max = -(long int) nerrnos;
1952 #if SUPPORTED_PERSONALITIES > 1
1953 if (current_wordsize < sizeof(val)) {
1954 val = (unsigned int) val;
1955 max = (unsigned int) max;
1962 * 1: ok, continue in trace_syscall_exiting().
1963 * -1: error, trace_syscall_exiting() should print error indicator
1964 * ("????" etc) and bail out.
1967 get_error(struct tcb *tcp)
1970 int check_errno = 1;
1971 if (SCNO_IN_RANGE(tcp->scno) &&
1972 sysent[tcp->scno].sys_flags & SYSCALL_NEVER_FAILS) {
1975 #if defined(S390) || defined(S390X)
1976 if (check_errno && is_negated_errno(gpr2)) {
1984 if (check_errno && is_negated_errno(i386_regs.eax)) {
1986 u_error = -i386_regs.eax;
1989 tcp->u_rval = i386_regs.eax;
1991 #elif defined(X86_64) || defined(X32)
1992 if (check_errno && is_negated_errno(x86_64_regs.rax)) {
1994 u_error = -x86_64_regs.rax;
1997 tcp->u_rval = x86_64_regs.rax;
1999 tcp->u_lrval = x86_64_regs.rax;
2007 if (check_errno && is_negated_errno(err)) {
2015 if (check_errno && ia64_r10) {
2019 tcp->u_rval = ia64_r8;
2023 if (check_errno && a3) {
2028 # if defined(LINUX_MIPSN32)
2032 #elif defined(POWERPC)
2033 if (check_errno && is_negated_errno(ppc_result)) {
2035 u_error = -ppc_result;
2038 tcp->u_rval = ppc_result;
2041 if (check_errno && is_negated_errno(m68k_d0)) {
2046 tcp->u_rval = m68k_d0;
2048 #elif defined(ARM) || defined(AARCH64)
2049 # if defined(AARCH64)
2050 if (tcp->currpers == 1) {
2051 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2053 u_error = -aarch64_regs.regs[0];
2056 tcp->u_rval = aarch64_regs.regs[0];
2062 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
2064 u_error = -arm_regs.ARM_r0;
2067 tcp->u_rval = arm_regs.ARM_r0;
2070 #elif defined(AVR32)
2071 if (check_errno && regs.r12 && (unsigned) -regs.r12 < nerrnos) {
2073 u_error = -regs.r12;
2076 tcp->u_rval = regs.r12;
2079 if (check_errno && is_negated_errno(r0)) {
2085 #elif defined(ALPHA)
2086 if (check_errno && alpha_a3) {
2093 #elif defined(SPARC)
2094 if (check_errno && regs.psr & PSR_C) {
2096 u_error = regs.u_regs[U_REG_O0];
2099 tcp->u_rval = regs.u_regs[U_REG_O0];
2101 #elif defined(SPARC64)
2102 if (check_errno && regs.tstate & 0x1100000000UL) {
2104 u_error = regs.u_regs[U_REG_O0];
2107 tcp->u_rval = regs.u_regs[U_REG_O0];
2110 if (check_errno && is_negated_errno(hppa_r28)) {
2112 u_error = -hppa_r28;
2115 tcp->u_rval = hppa_r28;
2118 if (check_errno && is_negated_errno(r0)) {
2126 if (check_errno && is_negated_errno(sh64_r9)) {
2131 tcp->u_rval = sh64_r9;
2133 #elif defined(CRISV10) || defined(CRISV32)
2134 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
2136 u_error = -cris_r10;
2139 tcp->u_rval = cris_r10;
2143 * The standard tile calling convention returns the value (or negative
2144 * errno) in r0, and zero (or positive errno) in r1.
2145 * Until at least kernel 3.8, however, the r1 value is not reflected
2146 * in ptregs at this point, so we use r0 here.
2148 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
2150 u_error = -tile_regs.regs[0];
2152 tcp->u_rval = tile_regs.regs[0];
2154 #elif defined(MICROBLAZE)
2155 if (check_errno && is_negated_errno(microblaze_r3)) {
2157 u_error = -microblaze_r3;
2160 tcp->u_rval = microblaze_r3;
2163 tcp->u_error = u_error;
2168 dumpio(struct tcb *tcp)
2172 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
2174 if (!SCNO_IN_RANGE(tcp->scno))
2176 if (sysent[tcp->scno].sys_func == printargs)
2178 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
2179 if (sysent[tcp->scno].sys_func == sys_read ||
2180 sysent[tcp->scno].sys_func == sys_pread ||
2181 sysent[tcp->scno].sys_func == sys_recv ||
2182 sysent[tcp->scno].sys_func == sys_recvfrom)
2183 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
2184 else if (sysent[tcp->scno].sys_func == sys_readv)
2185 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2188 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
2189 if (sysent[tcp->scno].sys_func == sys_write ||
2190 sysent[tcp->scno].sys_func == sys_pwrite ||
2191 sysent[tcp->scno].sys_func == sys_send ||
2192 sysent[tcp->scno].sys_func == sys_sendto)
2193 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
2194 else if (sysent[tcp->scno].sys_func == sys_writev)
2195 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2201 trace_syscall_exiting(struct tcb *tcp)
2208 /* Measure the exit time as early as possible to avoid errors. */
2210 gettimeofday(&tv, NULL);
2212 #if SUPPORTED_PERSONALITIES > 1
2213 update_personality(tcp, tcp->currpers);
2215 res = (get_regs_error ? -1 : get_syscall_result(tcp));
2217 syscall_fixup_on_sysexit(tcp); /* never fails */
2218 res = get_error(tcp); /* returns 1 or -1 */
2220 if (need_fork_exec_workarounds)
2221 syscall_fixup_for_fork_exec(tcp);
2222 if (filtered(tcp)) {
2229 struct timeval t = tv;
2230 count_syscall(tcp, &t);
2231 if (cflag == CFLAG_ONLY_STATS) {
2236 /* If not in -ff mode, and printing_tcp != tcp,
2237 * then the log currently does not end with output
2238 * of _our syscall entry_, but with something else.
2239 * We need to say which syscall's return is this.
2241 * Forced reprinting via TCB_REPRINT is used only by
2242 * "strace -ff -oLOG test/threaded_execve" corner case.
2243 * It's the only case when -ff mode needs reprinting.
2245 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2246 tcp->flags &= ~TCB_REPRINT;
2248 if (!SCNO_IN_RANGE(tcp->scno))
2249 tprintf("<... syscall_%lu resumed> ", tcp->scno);
2251 tprintf("<... %s resumed> ", sysent[tcp->scno].sys_name);
2256 /* There was error in one of prior ptrace ops */
2259 tprints("= ? <unavailable>\n");
2261 tcp->flags &= ~TCB_INSYSCALL;
2266 if (!SCNO_IN_RANGE(tcp->scno)
2267 || (qual_flags[tcp->scno] & QUAL_RAW)) {
2268 /* sys_res = printargs(tcp); - but it's nop on sysexit */
2270 /* FIXME: not_failing_only (IOW, option -z) is broken:
2271 * failure of syscall is known only after syscall return.
2272 * Thus we end up with something like this on, say, ENOENT:
2273 * open("doesnt_exist", O_RDONLY <unfinished ...>
2274 * {next syscall decode}
2275 * whereas the intended result is that open(...) line
2276 * is not shown at all.
2278 if (not_failing_only && tcp->u_error)
2279 goto ret; /* ignore failed syscalls */
2280 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2285 u_error = tcp->u_error;
2286 if (!SCNO_IN_RANGE(tcp->scno) ||
2287 qual_flags[tcp->scno] & QUAL_RAW) {
2289 tprintf("= -1 (errno %ld)", u_error);
2291 tprintf("= %#lx", tcp->u_rval);
2293 else if (!(sys_res & RVAL_NONE) && u_error) {
2295 /* Blocked signals do not interrupt any syscalls.
2296 * In this case syscalls don't return ERESTARTfoo codes.
2298 * Deadly signals set to SIG_DFL interrupt syscalls
2299 * and kill the process regardless of which of the codes below
2300 * is returned by the interrupted syscall.
2301 * In some cases, kernel forces a kernel-generated deadly
2302 * signal to be unblocked and set to SIG_DFL (and thus cause
2303 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2304 * or SIGILL. (The alternative is to leave process spinning
2305 * forever on the faulty instruction - not useful).
2307 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2308 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2309 * but kernel will always restart them.
2312 /* Most common type of signal-interrupted syscall exit code.
2313 * The system call will be restarted with the same arguments
2314 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2316 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
2318 case ERESTARTNOINTR:
2319 /* Rare. For example, fork() returns this if interrupted.
2320 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2322 tprints("= ? ERESTARTNOINTR (To be restarted)");
2324 case ERESTARTNOHAND:
2325 /* pause(), rt_sigsuspend() etc use this code.
2326 * SA_RESTART is ignored (assumed not set):
2327 * syscall won't restart (will return EINTR instead)
2328 * even after signal with SA_RESTART set.
2329 * However, after SIG_IGN or SIG_DFL signal it will.
2331 tprints("= ? ERESTARTNOHAND (Interrupted by signal)");
2333 case ERESTART_RESTARTBLOCK:
2334 /* Syscalls like nanosleep(), poll() which can't be
2335 * restarted with their original arguments use this
2336 * code. Kernel will execute restart_syscall() instead,
2337 * which changes arguments before restarting syscall.
2338 * SA_RESTART is ignored (assumed not set) similarly
2339 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2340 * since restart data is saved in "restart block"
2341 * in task struct, and if signal handler uses a syscall
2342 * which in turn saves another such restart block,
2343 * old data is lost and restart becomes impossible)
2345 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
2349 tprintf("= -1 E??? (errno %ld)", u_error);
2350 else if (u_error < nerrnos)
2351 tprintf("= -1 %s (%s)", errnoent[u_error],
2354 tprintf("= -1 ERRNO_%ld (%s)", u_error,
2358 if ((sys_res & RVAL_STR) && tcp->auxstr)
2359 tprintf(" (%s)", tcp->auxstr);
2362 if (sys_res & RVAL_NONE)
2365 switch (sys_res & RVAL_MASK) {
2367 tprintf("= %#lx", tcp->u_rval);
2370 tprintf("= %#lo", tcp->u_rval);
2373 tprintf("= %lu", tcp->u_rval);
2376 tprintf("= %ld", tcp->u_rval);
2378 #if defined(LINUX_MIPSN32) || defined(X32)
2381 tprintf("= %#llx", tcp->u_lrval);
2384 tprintf("= %#llo", tcp->u_lrval);
2387 case RVAL_LUDECIMAL:
2388 tprintf("= %llu", tcp->u_lrval);
2392 tprintf("= %lld", tcp->u_lrval);
2398 "invalid rval format\n");
2402 if ((sys_res & RVAL_STR) && tcp->auxstr)
2403 tprintf(" (%s)", tcp->auxstr);
2406 tv_sub(&tv, &tv, &tcp->etime);
2407 tprintf(" <%ld.%06ld>",
2408 (long) tv.tv_sec, (long) tv.tv_usec);
2415 tcp->flags &= ~TCB_INSYSCALL;
2420 trace_syscall(struct tcb *tcp)
2422 return exiting(tcp) ?
2423 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);