From faf09f0119da40d9b408021ad5665a906e00ee59 Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Wed, 4 Mar 2015 20:41:16 +0000 Subject: Linux native: Use TRAP_BRKPT/TRAP_HWBPT This patch adjusts the native Linux target backend to tell the core whether a trap was caused by a breakpoint. It teaches the target to get that information out of the si_code of the SIGTRAP siginfo. Tested on x86-64 Fedora 20, s390 RHEL 7, and PPC64 Fedora 18. An earlier version was tested on ARM Fedora 21. gdb/ChangeLog: 2015-03-04 Pedro Alves * linux-nat.c (save_sigtrap): Check for breakpoints before checking watchpoints. (status_callback) [USE_SIGTRAP_SIGINFO]: Don't check whether a breakpoint is inserted if relying on SIGTRAP's siginfo.si_code. (check_stopped_by_breakpoint) [USE_SIGTRAP_SIGINFO]: Decide whether a breakpoint triggered based on the SIGTRAP's siginfo.si_code. (linux_nat_stopped_by_sw_breakpoint) (linux_nat_supports_stopped_by_sw_breakpoint) (linux_nat_stopped_by_hw_breakpoint) (linux_nat_supports_stopped_by_hw_breakpoint): New functions. (linux_nat_wait_1): Don't re-increment the PC if relying on SIGTRAP's siginfo->si_code. (linux_nat_add_target): Install new target methods. * linux-thread-db.c (check_event): Don't account for breakpoint PC offset if the target already adjusted the PC. * nat/linux-ptrace.h (USE_SIGTRAP_SIGINFO): New. (GDB_ARCH_TRAP_BRKPT): New. (TRAP_HWBKPT): Define if not already defined. --- gdb/nat/linux-ptrace.h | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'gdb/nat/linux-ptrace.h') diff --git a/gdb/nat/linux-ptrace.h b/gdb/nat/linux-ptrace.h index c5b0f14..8354a4d 100644 --- a/gdb/nat/linux-ptrace.h +++ b/gdb/nat/linux-ptrace.h @@ -88,6 +88,57 @@ struct buffer; #define __WALL 0x40000000 /* Wait for any child. */ #endif +/* True if whether a breakpoint/watchpoint triggered can be determined + from the si_code of SIGTRAP's siginfo_t (TRAP_BRKPT/TRAP_HWBKPT). + That is, if the kernel can tell us whether the thread executed a + software breakpoint, we trust it. The kernel will be determining + that from the hardware (e.g., from which exception was raised in + the CPU). Relying on whether a breakpoint is planted in memory at + the time the SIGTRAP is processed to determine whether the thread + stopped for a software breakpoint can be too late. E.g., the + breakpoint could have been removed since. Or the thread could have + stepped an instruction the size of a breakpoint instruction, and + before the stop is processed a breakpoint is inserted at its + address. Getting these wrong is disastrous on decr_pc_after_break + architectures. The moribund location mechanism helps with that + somewhat but it is an heuristic, and can well fail. Getting that + information out of the kernel and ultimately out of the CPU is the + way to go. That said, some architecture may get the si_code wrong, + and as such we're leaving fallback code in place. We'll remove + this after a while if no problem is reported. */ +#define USE_SIGTRAP_SIGINFO 1 + +/* The x86 kernel gets some of the si_code values backwards, like + this: + + | what | si_code | + |------------------------------------------+------------| + | software breakpoints (int3) | SI_KERNEL | + | single-steps | TRAP_TRACE | + | single-stepping a syscall | TRAP_BRKPT | + | user sent SIGTRAP | 0 | + | exec SIGTRAP (when no PTRACE_EVENT_EXEC) | 0 | + | hardware breakpoints/watchpoints | TRAP_HWBPT | + + That is, it reports SI_KERNEL for software breakpoints (and only + for those), and TRAP_BRKPT for single-stepping a syscall... If the + kernel is ever fixed, we'll just have to detect it like we detect + optional ptrace features: by forking and debugging ourselves, + running to a breakpoint and checking what comes out of + siginfo->si_code. + + The generic Linux target code should use GDB_ARCH_TRAP_BRKPT + instead of TRAP_BRKPT to abstract out this x86 peculiarity. */ +#if defined __i386__ || defined __x86_64__ +# define GDB_ARCH_TRAP_BRKPT SI_KERNEL +#else +# define GDB_ARCH_TRAP_BRKPT TRAP_BRKPT +#endif + +#ifndef TRAP_HWBKPT +# define TRAP_HWBKPT 4 +#endif + extern void linux_ptrace_attach_fail_reason (pid_t pid, struct buffer *buffer); /* Find all possible reasons we could have failed to attach to PTID -- cgit v1.1