Use PTRACE_POKEUSER to update rip is unsupported for x32. We should use PTRACE_GETREGS/PTRACE_SETREGS to update rip for x32.
-- H.J.
From efcabc18891755d170f0ee75bd96f1a0f29477ea Mon Sep 17 00:00:00 2001 From: "H.J. Lu" <[email protected]> Date: Thu, 12 Mar 2015 12:14:43 -0700 Subject: [PATCH 2/3] Add x32 support to tests/backtrace.c Use PTRACE_POKEUSER to update rip is unsupported for x32. We should use PTRACE_GETREGS/PTRACE_SETREGS to update rip for x32. --- tests/backtrace.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/backtrace.c b/tests/backtrace.c index ab4110f..a59f46b 100644 --- a/tests/backtrace.c +++ b/tests/backtrace.c @@ -63,6 +63,7 @@ dump_modules (Dwfl_Module *mod, void **userdata __attribute__ ((unused)), } static bool is_x86_64_native; +static bool is_x32_native; static pid_t check_tid; static void @@ -261,8 +262,20 @@ prepare_thread (pid_t pid2 __attribute__ ((unused)), #else /* x86_64 */ long l; errno = 0; - l = ptrace (PTRACE_POKEUSER, pid2, - (void *) (intptr_t) offsetof (struct user_regs_struct, rip), jmp); + if (is_x32_native) + { + /* PTRACE_POKEUSER doesn't work for x32. */ + struct user_regs_struct user_regs; + l = ptrace (PTRACE_GETREGS, pid2, 0, (intptr_t) &user_regs); + assert_perror (errno); + assert (l == 0); + user_regs.rip = (intptr_t) jmp; + l = ptrace (PTRACE_SETREGS, pid2, 0, (intptr_t) &user_regs); + } + else + l = ptrace (PTRACE_POKEUSER, pid2, + (void *) (intptr_t) offsetof (struct user_regs_struct, rip), + jmp); assert_perror (errno); assert (l == 0); l = ptrace (PTRACE_CONT, pid2, NULL, (void *) (intptr_t) SIGUSR2); @@ -380,8 +393,10 @@ exec_dump (const char *exec) /* It is false also on x86_64 with i386 inferior. */ #ifndef __x86_64__ is_x86_64_native = false; + is_x32_native = false; #else /* __x86_64__ */ - is_x86_64_native = ehdr->e_ident[EI_CLASS] == ELFCLASS64; + is_x86_64_native = ehdr->e_machine == EM_X86_64; + is_x32_native = ehdr->e_ident[EI_CLASS] == ELFCLASS32; #endif /* __x86_64__ */ void (*jmp) (void) = 0; if (is_x86_64_native) -- 1.9.3
