1 /* Copyright (c) 2011-2021, Scott Tsai
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
31 #define debug_break __debugbreak
39 #define DEBUG_BREAK_USE_TRAP_INSTRUCTION 1
40 #define DEBUG_BREAK_USE_BULTIN_TRAP 2
41 #define DEBUG_BREAK_USE_SIGTRAP 3
43 #if defined(__i386__) || defined(__x86_64__)
44 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
45 __inline__ static void trap_instruction(void)
47 __asm__ volatile("int $0x03");
49 #elif defined(__thumb__)
50 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
51 /* FIXME: handle __THUMB_INTERWORK__ */
52 __attribute__((always_inline))
53 __inline__ static void trap_instruction(void)
55 /* See 'arm-linux-tdep.c' in GDB source.
56 * Both instruction sequences below work. */
58 /* 'eabi_linux_thumb_le_breakpoint' */
59 __asm__ volatile(".inst 0xde01");
61 /* 'eabi_linux_thumb2_le_breakpoint' */
62 __asm__ volatile(".inst.w 0xf7f0a000");
66 * After a breakpoint hit, can't 'stepi', 'step', or 'continue' in GDB.
67 * 'step' would keep getting stuck on the same instruction.
69 * Workaround: use the new GDB commands 'debugbreak-step' and
70 * 'debugbreak-continue' that become available
71 * after you source the script from GDB:
73 * $ gdb -x debugbreak-gdb.py <... USUAL ARGUMENTS ...>
75 * 'debugbreak-step' would jump over the breakpoint instruction with
76 * roughly equivalent of:
77 * (gdb) set $instruction_len = 2
78 * (gdb) tbreak *($pc + $instruction_len)
79 * (gdb) jump *($pc + $instruction_len)
82 #elif defined(__arm__) && !defined(__thumb__)
83 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
84 __attribute__((always_inline))
85 __inline__ static void trap_instruction(void)
87 /* See 'arm-linux-tdep.c' in GDB source,
88 * 'eabi_linux_arm_le_breakpoint' */
89 __asm__ volatile(".inst 0xe7f001f0");
91 * Same problem and workaround as Thumb mode */
93 #elif defined(__aarch64__) && defined(__APPLE__)
94 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_BULTIN_DEBUGTRAP
95 #elif defined(__aarch64__)
96 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
97 __attribute__((always_inline))
98 __inline__ static void trap_instruction(void)
100 /* See 'aarch64-tdep.c' in GDB source,
101 * 'aarch64_default_breakpoint' */
102 __asm__ volatile(".inst 0xd4200000");
104 #elif defined(__powerpc__)
105 /* PPC 32 or 64-bit, big or little endian */
106 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
107 __attribute__((always_inline))
108 __inline__ static void trap_instruction(void)
110 /* See 'rs6000-tdep.c' in GDB source,
111 * 'rs6000_breakpoint' */
112 __asm__ volatile(".4byte 0x7d821008");
115 * After a breakpoint hit, can't 'stepi', 'step', or 'continue' in GDB.
116 * 'step' stuck on the same instruction ("twge r2,r2").
118 * The workaround is the same as ARM Thumb mode: use debugbreak-gdb.py
119 * or manually jump over the instruction. */
121 #elif defined(__riscv)
122 /* RISC-V 32 or 64-bit, whether the "C" extension
123 * for compressed, 16-bit instructions are supported or not */
124 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
125 __attribute__((always_inline))
126 __inline__ static void trap_instruction(void)
128 /* See 'riscv-tdep.c' in GDB source,
129 * 'riscv_sw_breakpoint_from_kind' */
130 __asm__ volatile(".4byte 0x00100073");
133 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_SIGTRAP
137 #ifndef DEBUG_BREAK_IMPL
138 #error "debugbreak.h is not supported on this target"
139 #elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_TRAP_INSTRUCTION
140 __attribute__((always_inline))
141 __inline__ static void debug_break(void)
145 #elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_BULTIN_DEBUGTRAP
146 __attribute__((always_inline))
147 __inline__ static void debug_break(void)
149 __builtin_debugtrap();
151 #elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_BULTIN_TRAP
152 __attribute__((always_inline))
153 __inline__ static void debug_break(void)
157 #elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_SIGTRAP
159 __attribute__((always_inline))
160 __inline__ static void debug_break(void)
165 #error "invalid DEBUG_BREAK_IMPL value"
172 #endif /* ifdef _MSC_VER */
174 #endif /* ifndef DEBUG_BREAK_H */