- currpers &= ~2; /* map 2,1 to 0,1 */
-# endif
- update_personality(tcp, currpers);
-#elif defined(IA64)
-# define IA64_PSR_IS ((long)1 << 34)
- long psr;
- if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
- ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
- if (ia64_ia32mode) {
- if (upeek(tcp->pid, PT_R1, &scno) < 0)
- return -1;
- } else {
- if (upeek(tcp->pid, PT_R15, &scno) < 0)
- return -1;
- }
-#elif defined(AARCH64)
- switch (aarch64_io.iov_len) {
- case sizeof(aarch64_regs):
- /* We are in 64-bit mode */
- scno = aarch64_regs.regs[8];
- update_personality(tcp, 1);
- break;
- case sizeof(arm_regs):
- /* We are in 32-bit mode */
- /* Note: we don't support OABI, unlike 32-bit ARM build */
- scno = arm_regs.ARM_r7;
- update_personality(tcp, 0);
- break;
- }
-#elif defined(ARM)
- if (arm_regs.ARM_ip != 0) {
- /* It is not a syscall entry */
- fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
- tcp->flags |= TCB_INSYSCALL;
- return 0;
- }
- /* Note: we support only 32-bit CPUs, not 26-bit */
-
-# ifndef STRACE_KNOWS_ONLY_EABI
-# warning STRACE_KNOWS_ONLY_EABI not set, will PTRACE_PEEKTEXT on every syscall (slower tracing)
- if (arm_regs.ARM_cpsr & 0x20)
- /* Thumb mode */
- goto scno_in_r7;
- /* ARM mode */
- /* Check EABI/OABI by examining SVC insn's low 24 bits */
- errno = 0;
- scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
- if (errno)
- return -1;
- /* EABI syscall convention? */
- if (scno != 0xef000000) {
- /* No, it's OABI */
- if ((scno & 0x0ff00000) != 0x0f900000) {
- fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
- tcp->pid, scno);
- return -1;
- }
- /* Fixup the syscall number */
- scno &= 0x000fffff;
- } else {
- scno_in_r7:
- scno = arm_regs.ARM_r7;
- }
-# else
- scno = arm_regs.ARM_r7;
-# endif
- scno = shuffle_scno(scno);
-#elif defined(M68K)
- if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
- return -1;
-#elif defined(LINUX_MIPSN32)
- unsigned long long regs[38];
-
- if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
- return -1;
- mips_a3 = regs[REG_A3];
- mips_r2 = regs[REG_V0];
-
- scno = mips_r2;
- if (!SCNO_IN_RANGE(scno)) {
- if (mips_a3 == 0 || mips_a3 == -1) {
- if (debug_flag)
- fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
- return 0;
- }
- }
-#elif defined(MIPS)
- if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
- return -1;
- if (upeek(tcp->pid, REG_V0, &scno) < 0)
- return -1;
-
- if (!SCNO_IN_RANGE(scno)) {
- if (mips_a3 == 0 || mips_a3 == -1) {
- if (debug_flag)
- fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
- return 0;
- }
- }
-#elif defined(ALPHA)
- if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
- return -1;
- if (upeek(tcp->pid, REG_R0, &scno) < 0)
- return -1;
-
- /*
- * Do some sanity checks to figure out if it's
- * really a syscall entry
- */
- if (!SCNO_IN_RANGE(scno)) {
- if (alpha_a3 == 0 || alpha_a3 == -1) {
- if (debug_flag)
- fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
- return 0;
- }
- }
-#elif defined(SPARC) || defined(SPARC64)
- /* Disassemble the syscall trap. */
- /* Retrieve the syscall trap instruction. */
- unsigned long trap;
- errno = 0;
-# if defined(SPARC64)
- trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
- trap >>= 32;
-# else
- trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
-# endif
- if (errno)
- return -1;
-
- /* Disassemble the trap to see what personality to use. */
- switch (trap) {
- case 0x91d02010:
- /* Linux/SPARC syscall trap. */
- update_personality(tcp, 0);
- break;
- case 0x91d0206d:
- /* Linux/SPARC64 syscall trap. */
- update_personality(tcp, 2);
- break;
- case 0x91d02000:
- /* SunOS syscall trap. (pers 1) */
- fprintf(stderr, "syscall: SunOS no support\n");
- return -1;
- case 0x91d02008:
- /* Solaris 2.x syscall trap. (per 2) */
- update_personality(tcp, 1);
- break;
- case 0x91d02009:
- /* NetBSD/FreeBSD syscall trap. */
- fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
- return -1;
- case 0x91d02027:
- /* Solaris 2.x gettimeofday */
- update_personality(tcp, 1);
- break;
- default:
-# if defined(SPARC64)
- fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
-# else
- fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
-# endif
- return -1;
- }
-
- /* Extract the system call number from the registers. */
- if (trap == 0x91d02027)
- scno = 156;
- else
- scno = sparc_regs.u_regs[U_REG_G1];
- if (scno == 0) {
- scno = sparc_regs.u_regs[U_REG_O0];
- memmove(&sparc_regs.u_regs[U_REG_O0], &sparc_regs.u_regs[U_REG_O1], 7*sizeof(sparc_regs.u_regs[0]));
- }
-#elif defined(HPPA)
- if (upeek(tcp->pid, PT_GR20, &scno) < 0)
- return -1;
-#elif defined(SH)
- /*
- * In the new syscall ABI, the system call number is in R3.
- */
- if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
- return -1;
-
- if (scno < 0) {
- /* Odd as it may seem, a glibc bug has been known to cause
- glibc to issue bogus negative syscall numbers. So for
- our purposes, make strace print what it *should* have been */
- long correct_scno = (scno & 0xff);
- if (debug_flag)
- fprintf(stderr,
- "Detected glibc bug: bogus system call"
- " number = %ld, correcting to %ld\n",
- scno,
- correct_scno);
- scno = correct_scno;
- }
-#elif defined(SH64)
- if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
- return -1;
- scno &= 0xFFFF;
-#elif defined(CRISV10) || defined(CRISV32)
- if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
- return -1;
-#elif defined(TILE)
- int currpers;
- scno = tile_regs.regs[10];
-# ifdef __tilepro__
- currpers = 1;
-# else
-# ifndef PT_FLAGS_COMPAT
-# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
-# endif
- if (tile_regs.flags & PT_FLAGS_COMPAT)
- currpers = 1;
- else
- currpers = 0;
-# endif
- update_personality(tcp, currpers);
-#elif defined(MICROBLAZE)
- if (upeek(tcp->pid, 0, &scno) < 0)
- return -1;
-#elif defined(OR1K)
- scno = or1k_regs.gpr[11];
-#elif defined(METAG)
- scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
-#elif defined(XTENSA)
- if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
- return -1;
-#endif
-
- tcp->scno = scno;
- if (SCNO_IS_VALID(tcp->scno)) {
- tcp->s_ent = &sysent[scno];
- tcp->qual_flg = qual_flags[scno];
- } else {
- static const struct_sysent unknown = {
- .nargs = MAX_ARGS,
- .sys_flags = 0,
- .sys_func = printargs,
- .sys_name = "unknown", /* not used */
- };
- tcp->s_ent = &unknown;
- tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
- }
- return 1;
-}
-
-/* Called at each syscall entry.
- * Returns:
- * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
- * 1: ok, continue in trace_syscall_entering().
- * other: error, trace_syscall_entering() should print error indicator
- * ("????" etc) and bail out.
- */
-static int
-syscall_fixup_on_sysenter(struct tcb *tcp)
-{
- /* A common case of "not a syscall entry" is post-execve SIGTRAP */
-#if defined(I386)
- if (i386_regs.eax != -ENOSYS) {
- if (debug_flag)
- fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
- return 0;
- }
-#elif defined(X86_64) || defined(X32)
- {
- long rax;
- if (x86_io.iov_len == sizeof(i386_regs)) {
- /* Sign extend from 32 bits */
- rax = (int32_t)i386_regs.eax;
- } else {
- /* Note: in X32 build, this truncates 64 to 32 bits */
- rax = x86_64_regs.rax;
- }
- if (rax != -ENOSYS) {
- if (debug_flag)
- fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
- return 0;
- }
- }
-#elif defined(M68K)
- /* TODO? Eliminate upeek's in arches below like we did in x86 */
- if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
- return -1;
- if (m68k_d0 != -ENOSYS) {
- if (debug_flag)
- fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
- return 0;
- }
-#elif defined(IA64)
- if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
- return -1;
- if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
- return -1;
- if (ia64_ia32mode && ia64_r8 != -ENOSYS) {
- if (debug_flag)
- fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
- return 0;
- }
-#elif defined(CRISV10) || defined(CRISV32)
- if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
- return -1;
- if (cris_r10 != -ENOSYS) {
- if (debug_flag)
- fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
- return 0;
- }
-#elif defined(MICROBLAZE)
- if (upeek(tcp->pid, 3 * 4, µblaze_r3) < 0)
- return -1;
- if (microblaze_r3 != -ENOSYS) {
- if (debug_flag)
- fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
- return 0;
- }
-#endif
- return 1;
-}
-
-static void
-internal_fork(struct tcb *tcp)
-{
-#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
-# define ARG_FLAGS 1
-#else
-# define ARG_FLAGS 0
-#endif
-#ifndef CLONE_UNTRACED
-# define CLONE_UNTRACED 0x00800000
-#endif
- if ((ptrace_setoptions
- & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
- == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
- return;
-
- if (!followfork)
- return;
-
- if (entering(tcp)) {
- /*
- * We won't see the new child if clone is called with
- * CLONE_UNTRACED, so we keep the same logic with that option
- * and don't trace it.
- */
- if ((tcp->s_ent->sys_func == sys_clone)
- && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
- )
- return;
- setbpt(tcp);
- } else {
- if (tcp->flags & TCB_BPTSET)
- clearbpt(tcp);
- }
-}
-
-#if defined(TCB_WAITEXECVE)
-static void
-internal_exec(struct tcb *tcp)
-{
- /* Maybe we have post-execve SIGTRAP suppressed? */
- if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
- return; /* yes, no need to do anything */
-
- if (exiting(tcp) && syserror(tcp))
- /* Error in execve, no post-execve SIGTRAP expected */
- tcp->flags &= ~TCB_WAITEXECVE;
- else
- tcp->flags |= TCB_WAITEXECVE;
-}
-#endif
-
-static void
-syscall_fixup_for_fork_exec(struct tcb *tcp)
-{
- /*
- * We must always trace a few critical system calls in order to
- * correctly support following forks in the presence of tracing
- * qualifiers.
- */
- int (*func)();
-
- func = tcp->s_ent->sys_func;
-
- if ( sys_fork == func
- || sys_vfork == func
- || sys_clone == func
- ) {
- internal_fork(tcp);
- return;
- }
-
-#if defined(TCB_WAITEXECVE)
- if ( sys_execve == func
-# if defined(SPARC) || defined(SPARC64)
- || sys_execv == func
-# endif
- ) {
- internal_exec(tcp);
- return;
- }
-#endif
-}
-
-/* Return -1 on error or 1 on success (never 0!) */
-static int
-get_syscall_args(struct tcb *tcp)
-{
- int i, nargs;
-
- nargs = tcp->s_ent->nargs;
-
-#if defined(S390) || defined(S390X)
- for (i = 0; i < nargs; ++i)
- if (upeek(tcp->pid, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
- return -1;
-#elif defined(ALPHA)
- for (i = 0; i < nargs; ++i)
- if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
- return -1;
-#elif defined(IA64)
- if (!ia64_ia32mode) {
- unsigned long *out0, cfm, sof, sol;
- long rbs_end;
- /* be backwards compatible with kernel < 2.4.4... */
-# ifndef PT_RBS_END
-# define PT_RBS_END PT_AR_BSP
-# endif
-
- if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
- return -1;
- if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
- return -1;
-
- sof = (cfm >> 0) & 0x7f;
- sol = (cfm >> 7) & 0x7f;
- out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
-
- for (i = 0; i < nargs; ++i) {
- if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
- sizeof(long), (char *) &tcp->u_arg[i]) < 0)
- return -1;
- }
- } else {
- static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
- PT_R9 /* ECX = out1 */,
- PT_R10 /* EDX = out2 */,
- PT_R14 /* ESI = out3 */,
- PT_R15 /* EDI = out4 */,
- PT_R13 /* EBP = out5 */};
-
- for (i = 0; i < nargs; ++i) {
- if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
- return -1;
- /* truncate away IVE sign-extension */
- tcp->u_arg[i] &= 0xffffffff;
- }
- }
-#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
- /* N32 and N64 both use up to six registers. */
- unsigned long long regs[38];
-
- if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
- return -1;
-
- for (i = 0; i < nargs; ++i) {
- tcp->u_arg[i] = regs[REG_A0 + i];
-# if defined(LINUX_MIPSN32)
- tcp->ext_arg[i] = regs[REG_A0 + i];
-# endif
- }
-#elif defined(MIPS)
- if (nargs > 4) {
- long sp;
-
- if (upeek(tcp->pid, REG_SP, &sp) < 0)
- return -1;
- for (i = 0; i < 4; ++i)
- if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
- return -1;
- umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
- (char *)(tcp->u_arg + 4));
- } else {
- for (i = 0; i < nargs; ++i)
- if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
- return -1;
- }
-#elif defined(POWERPC)
- (void)i;
- (void)nargs;
- tcp->u_arg[0] = ppc_regs.orig_gpr3;
- tcp->u_arg[1] = ppc_regs.gpr[4];
- tcp->u_arg[2] = ppc_regs.gpr[5];
- tcp->u_arg[3] = ppc_regs.gpr[6];
- tcp->u_arg[4] = ppc_regs.gpr[7];
- tcp->u_arg[5] = ppc_regs.gpr[8];
-#elif defined(SPARC) || defined(SPARC64)
- for (i = 0; i < nargs; ++i)
- tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
-#elif defined(HPPA)
- for (i = 0; i < nargs; ++i)
- if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
- return -1;
-#elif defined(ARM) || defined(AARCH64)
-# if defined(AARCH64)
- if (tcp->currpers == 1)
- for (i = 0; i < nargs; ++i)
- tcp->u_arg[i] = aarch64_regs.regs[i];
- else
-# endif
- for (i = 0; i < nargs; ++i)
- tcp->u_arg[i] = arm_regs.uregs[i];
-#elif defined(AVR32)
- (void)i;
- (void)nargs;
- tcp->u_arg[0] = avr32_regs.r12;
- tcp->u_arg[1] = avr32_regs.r11;
- tcp->u_arg[2] = avr32_regs.r10;
- tcp->u_arg[3] = avr32_regs.r9;
- tcp->u_arg[4] = avr32_regs.r5;
- tcp->u_arg[5] = avr32_regs.r3;
-#elif defined(BFIN)
- static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
-
- for (i = 0; i < nargs; ++i)
- if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
- return -1;
-#elif defined(SH)
- static const int syscall_regs[MAX_ARGS] = {
- 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
- 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
- };
-
- for (i = 0; i < nargs; ++i)
- if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
- return -1;
-#elif defined(SH64)
- int i;
- /* Registers used by SH5 Linux system calls for parameters */
- static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
-
- for (i = 0; i < nargs; ++i)
- if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
- return -1;
-#elif defined(I386)
- (void)i;
- (void)nargs;
- tcp->u_arg[0] = i386_regs.ebx;
- tcp->u_arg[1] = i386_regs.ecx;
- tcp->u_arg[2] = i386_regs.edx;
- tcp->u_arg[3] = i386_regs.esi;
- tcp->u_arg[4] = i386_regs.edi;
- tcp->u_arg[5] = i386_regs.ebp;
-#elif defined(X86_64) || defined(X32)
- (void)i;
- (void)nargs;
- if (x86_io.iov_len != sizeof(i386_regs)) {
- /* x86-64 or x32 ABI */
- tcp->u_arg[0] = x86_64_regs.rdi;
- tcp->u_arg[1] = x86_64_regs.rsi;
- tcp->u_arg[2] = x86_64_regs.rdx;
- tcp->u_arg[3] = x86_64_regs.r10;
- tcp->u_arg[4] = x86_64_regs.r8;
- tcp->u_arg[5] = x86_64_regs.r9;
-# ifdef X32
- tcp->ext_arg[0] = x86_64_regs.rdi;
- tcp->ext_arg[1] = x86_64_regs.rsi;
- tcp->ext_arg[2] = x86_64_regs.rdx;
- tcp->ext_arg[3] = x86_64_regs.r10;
- tcp->ext_arg[4] = x86_64_regs.r8;
- tcp->ext_arg[5] = x86_64_regs.r9;
-# endif
- } else {
- /* i386 ABI */
- /* Zero-extend from 32 bits */
- /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
- * if you need to use *sign-extended* parameter.
- */
- tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
- tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
- tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
- tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
- tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
- tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
- }
-#elif defined(MICROBLAZE)
- for (i = 0; i < nargs; ++i)
- if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
- return -1;
-#elif defined(CRISV10) || defined(CRISV32)
- static const int crisregs[MAX_ARGS] = {
- 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
- 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
- };
-
- for (i = 0; i < nargs; ++i)
- if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
- return -1;
-#elif defined(TILE)
- for (i = 0; i < nargs; ++i)
- tcp->u_arg[i] = tile_regs.regs[i];
-#elif defined(M68K)
- for (i = 0; i < nargs; ++i)
- if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
- return -1;
-#elif defined(OR1K)
- (void)nargs;
- for (i = 0; i < 6; ++i)
- tcp->u_arg[i] = or1k_regs.gpr[3 + i];
-#elif defined(METAG)
- for (i = 0; i < nargs; i++)
- /* arguments go backwards from D1Ar1 (D1.3) */
- tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
-#elif defined(XTENSA)
- /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
- static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
- for (i = 0; i < nargs; ++i)
- if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
- return -1;
-#else /* Other architecture (32bits specific) */
- for (i = 0; i < nargs; ++i)
- if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
- return -1;
-#endif
- return 1;
-}
-
-static int
-trace_syscall_entering(struct tcb *tcp)
-{
- int res, scno_good;
-
-#if defined TCB_WAITEXECVE
- if (tcp->flags & TCB_WAITEXECVE) {
- /* This is the post-execve SIGTRAP. */
- tcp->flags &= ~TCB_WAITEXECVE;
- return 0;
- }
-#endif
-
- scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
- if (res == 0)
- return res;
- if (res == 1) {
- res = syscall_fixup_on_sysenter(tcp);
- if (res == 0)
- return res;
- if (res == 1)
- res = get_syscall_args(tcp);
- }
-
- if (res != 1) {
- printleader(tcp);
- if (scno_good != 1)
- tprints("????" /* anti-trigraph gap */ "(");
- else if (tcp->qual_flg & UNDEFINED_SCNO)
- tprintf("%s(", undefined_scno_name(tcp));
- else
- tprintf("%s(", tcp->s_ent->sys_name);
- /*
- * " <unavailable>" will be added later by the code which
- * detects ptrace errors.
- */
- goto ret;
- }
-
- if ( sys_execve == tcp->s_ent->sys_func
-# if defined(SPARC) || defined(SPARC64)
- || sys_execv == tcp->s_ent->sys_func
-# endif
- ) {
- hide_log_until_execve = 0;
- }
-
-#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
- while (1) {
-# ifdef SYS_socket_subcall
- if (tcp->s_ent->sys_func == sys_socketcall) {
- decode_socket_subcall(tcp);
- break;
- }
-# endif
-# ifdef SYS_ipc_subcall
- if (tcp->s_ent->sys_func == sys_ipc) {
- decode_ipc_subcall(tcp);
- break;
- }
-# endif
- break;
- }
-#endif
-
- if (need_fork_exec_workarounds)
- syscall_fixup_for_fork_exec(tcp);
-
- if (!(tcp->qual_flg & QUAL_TRACE)
- || (tracing_paths && !pathtrace_match(tcp))
- ) {
- tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
- return 0;
- }
-
- tcp->flags &= ~TCB_FILTERED;
-
- if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
- res = 0;
- goto ret;
- }
-
- printleader(tcp);
- if (tcp->qual_flg & UNDEFINED_SCNO)
- tprintf("%s(", undefined_scno_name(tcp));
- else
- tprintf("%s(", tcp->s_ent->sys_name);
- if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
- res = printargs(tcp);
- else
- res = tcp->s_ent->sys_func(tcp);
-
- fflush(tcp->outf);
- ret:
- tcp->flags |= TCB_INSYSCALL;
- /* Measure the entrance time as late as possible to avoid errors. */
- if (Tflag || cflag)
- gettimeofday(&tcp->etime, NULL);
- return res;
-}
-
-/* Returns:
- * 1: ok, continue in trace_syscall_exiting().
- * -1: error, trace_syscall_exiting() should print error indicator
- * ("????" etc) and bail out.
- */
-static int
-get_syscall_result(struct tcb *tcp)
-{
-#if defined(S390) || defined(S390X)
- if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
- return -1;
-#elif defined(POWERPC)
- /* already done by get_regs */
-#elif defined(AVR32)
- /* already done by get_regs */
-#elif defined(BFIN)
- if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
- return -1;
-#elif defined(I386)
- /* already done by get_regs */
-#elif defined(X86_64) || defined(X32)
- /* already done by get_regs */
-#elif defined(IA64)
-# define IA64_PSR_IS ((long)1 << 34)
- long psr;
- if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
- ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
- if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
- return -1;
- if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
- return -1;
-#elif defined(ARM)
- /* already done by get_regs */
-#elif defined(AARCH64)
- /* register reading already done by get_regs */
-
- /* Used to do this, but we did it on syscall entry already: */
- /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
- * else it's personality 0.
- */
- /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
-#elif defined(M68K)
- if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
- return -1;
-#elif defined(LINUX_MIPSN32)
- unsigned long long regs[38];
-
- if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0)
- return -1;
- mips_a3 = regs[REG_A3];
- mips_r2 = regs[REG_V0];
-#elif defined(MIPS)
- if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
- return -1;
- if (upeek(tcp->pid, REG_V0, &mips_r2) < 0)
- return -1;
-#elif defined(ALPHA)
- if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
- return -1;
- if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
- return -1;
-#elif defined(SPARC) || defined(SPARC64)
- /* already done by get_regs */
-#elif defined(HPPA)
- if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
- return -1;
-#elif defined(SH)
- /* new syscall ABI returns result in R0 */
- if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
- return -1;
-#elif defined(SH64)
- /* ABI defines result returned in r9 */
- if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
- return -1;
-#elif defined(CRISV10) || defined(CRISV32)
- if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
- return -1;
-#elif defined(TILE)
- /* already done by get_regs */
-#elif defined(MICROBLAZE)
- if (upeek(tcp->pid, 3 * 4, µblaze_r3) < 0)
- return -1;
-#elif defined(OR1K)
- /* already done by get_regs */
-#elif defined(METAG)
- /* already done by get_regs */
-#elif defined(XTENSA)
- if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
- return -1;
-#endif
- return 1;
-}
-
-/* Called at each syscall exit */
-static void
-syscall_fixup_on_sysexit(struct tcb *tcp)
-{
-#if defined(S390) || defined(S390X)
- if ((tcp->flags & TCB_WAITEXECVE)
- && (s390_gpr2 == -ENOSYS || s390_gpr2 == tcp->scno)) {
- /*
- * Return from execve.
- * Fake a return value of zero. We leave the TCB_WAITEXECVE
- * flag set for the post-execve SIGTRAP to see and reset.
- */
- s390_gpr2 = 0;
- }
-#endif
-}
-
-/*
- * Check the syscall return value register value for whether it is
- * a negated errno code indicating an error, or a success return value.
- */
-static inline int
-is_negated_errno(unsigned long int val)
-{
- unsigned long int max = -(long int) nerrnos;
-#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
- if (current_wordsize < sizeof(val)) {
- val = (unsigned int) val;
- max = (unsigned int) max;
- }
-#endif
- return val > max;
-}
-
-#if defined(X32)
-static inline int
-is_negated_errno_x32(unsigned long long val)
-{
- unsigned long long max = -(long long) nerrnos;
- /*
- * current_wordsize is 4 even in personality 0 (native X32)
- * but truncation _must not_ be done in it.
- * can't check current_wordsize here!
- */
- if (current_personality != 0) {
- val = (uint32_t) val;
- max = (uint32_t) max;
- }
- return val > max;
-}
-#endif
-
-/* Returns:
- * 1: ok, continue in trace_syscall_exiting().
- * -1: error, trace_syscall_exiting() should print error indicator
- * ("????" etc) and bail out.
- */
-static void
-get_error(struct tcb *tcp)
-{
- int u_error = 0;
- int check_errno = 1;
- if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
- check_errno = 0;
- }
-#if defined(S390) || defined(S390X)
- if (check_errno && is_negated_errno(s390_gpr2)) {
- tcp->u_rval = -1;
- u_error = -s390_gpr2;
- }
- else {
- tcp->u_rval = s390_gpr2;
- }
-#elif defined(I386)
- if (check_errno && is_negated_errno(i386_regs.eax)) {
- tcp->u_rval = -1;
- u_error = -i386_regs.eax;
- }
- else {
- tcp->u_rval = i386_regs.eax;
- }
-#elif defined(X86_64)
- long rax;
- if (x86_io.iov_len == sizeof(i386_regs)) {
- /* Sign extend from 32 bits */
- rax = (int32_t)i386_regs.eax;
- } else {
- rax = x86_64_regs.rax;
- }
- if (check_errno && is_negated_errno(rax)) {
- tcp->u_rval = -1;
- u_error = -rax;
- }
- else {
- tcp->u_rval = rax;
- }
-#elif defined(X32)
- /* In X32, return value is 64-bit (llseek uses one).
- * Using merely "long rax" would not work.
- */
- long long rax;
- if (x86_io.iov_len == sizeof(i386_regs)) {
- /* Sign extend from 32 bits */
- rax = (int32_t)i386_regs.eax;
- } else {
- rax = x86_64_regs.rax;
- }
- /* Careful: is_negated_errno() works only on longs */
- if (check_errno && is_negated_errno_x32(rax)) {
- tcp->u_rval = -1;
- u_error = -rax;
- }
- else {
- tcp->u_rval = rax; /* truncating */
- tcp->u_lrval = rax;
- }
-#elif defined(IA64)
- if (ia64_ia32mode) {
- int err;
-
- err = (int)ia64_r8;
- if (check_errno && is_negated_errno(err)) {
- tcp->u_rval = -1;
- u_error = -err;
- }
- else {
- tcp->u_rval = err;
- }
- } else {
- if (check_errno && ia64_r10) {
- tcp->u_rval = -1;
- u_error = ia64_r8;
- } else {
- tcp->u_rval = ia64_r8;
- }
- }
-#elif defined(MIPS)
- if (check_errno && mips_a3) {
- tcp->u_rval = -1;
- u_error = mips_r2;
- } else {
- tcp->u_rval = mips_r2;
-# if defined(LINUX_MIPSN32)
- tcp->u_lrval = mips_r2;
-# endif
- }
-#elif defined(POWERPC)
- if (check_errno && (ppc_regs.ccr & 0x10000000)) {
- tcp->u_rval = -1;
- u_error = ppc_regs.gpr[3];
- }
- else {
- tcp->u_rval = ppc_regs.gpr[3];
- }
-#elif defined(M68K)
- if (check_errno && is_negated_errno(m68k_d0)) {
- tcp->u_rval = -1;
- u_error = -m68k_d0;
- }
- else {
- tcp->u_rval = m68k_d0;
- }
-#elif defined(ARM) || defined(AARCH64)
-# if defined(AARCH64)
- if (tcp->currpers == 1) {
- if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
- tcp->u_rval = -1;
- u_error = -aarch64_regs.regs[0];
- }
- else {
- tcp->u_rval = aarch64_regs.regs[0];
- }
- }
- else
-# endif
- {
- if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
- tcp->u_rval = -1;
- u_error = -arm_regs.ARM_r0;
- }
- else {
- tcp->u_rval = arm_regs.ARM_r0;
- }
- }
-#elif defined(AVR32)
- if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
- tcp->u_rval = -1;
- u_error = -avr32_regs.r12;
- }
- else {
- tcp->u_rval = avr32_regs.r12;
- }
-#elif defined(BFIN)
- if (check_errno && is_negated_errno(bfin_r0)) {
- tcp->u_rval = -1;
- u_error = -bfin_r0;
- } else {
- tcp->u_rval = bfin_r0;
- }
-#elif defined(ALPHA)
- if (check_errno && alpha_a3) {
- tcp->u_rval = -1;
- u_error = alpha_r0;
- }
- else {
- tcp->u_rval = alpha_r0;
- }
-#elif defined(SPARC)
- if (check_errno && sparc_regs.psr & PSR_C) {
- tcp->u_rval = -1;
- u_error = sparc_regs.u_regs[U_REG_O0];
- }
- else {
- tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
- }
-#elif defined(SPARC64)
- if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
- tcp->u_rval = -1;
- u_error = sparc_regs.u_regs[U_REG_O0];
- }
- else {
- tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
- }
-#elif defined(HPPA)
- if (check_errno && is_negated_errno(hppa_r28)) {
- tcp->u_rval = -1;
- u_error = -hppa_r28;
- }
- else {
- tcp->u_rval = hppa_r28;
- }
-#elif defined(SH)
- if (check_errno && is_negated_errno(sh_r0)) {
- tcp->u_rval = -1;
- u_error = -sh_r0;
- }
- else {
- tcp->u_rval = sh_r0;
- }
-#elif defined(SH64)
- if (check_errno && is_negated_errno(sh64_r9)) {
- tcp->u_rval = -1;
- u_error = -sh64_r9;
- }
- else {
- tcp->u_rval = sh64_r9;
- }
-#elif defined(METAG)
- /* result pointer in D0Re0 (D0.0) */
- if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
- tcp->u_rval = -1;
- u_error = -metag_regs.dx[0][0];
- }
- else {
- tcp->u_rval = metag_regs.dx[0][0];
- }
-#elif defined(CRISV10) || defined(CRISV32)
- if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
- tcp->u_rval = -1;
- u_error = -cris_r10;
- }
- else {
- tcp->u_rval = cris_r10;
- }
-#elif defined(TILE)
- /*
- * The standard tile calling convention returns the value (or negative
- * errno) in r0, and zero (or positive errno) in r1.
- * Until at least kernel 3.8, however, the r1 value is not reflected
- * in ptregs at this point, so we use r0 here.
- */
- if (check_errno && is_negated_errno(tile_regs.regs[0])) {
- tcp->u_rval = -1;
- u_error = -tile_regs.regs[0];
- } else {
- tcp->u_rval = tile_regs.regs[0];
- }
-#elif defined(MICROBLAZE)
- if (check_errno && is_negated_errno(microblaze_r3)) {
- tcp->u_rval = -1;
- u_error = -microblaze_r3;
- }
- else {
- tcp->u_rval = microblaze_r3;
- }
-#elif defined(OR1K)
- if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
- tcp->u_rval = -1;
- u_error = -or1k_regs.gpr[11];
- }
- else {
- tcp->u_rval = or1k_regs.gpr[11];
- }
-#elif defined(XTENSA)
- if (check_errno && is_negated_errno(xtensa_a2)) {
- tcp->u_rval = -1;
- u_error = -xtensa_a2;