MUDA
Loading...
Searching...
No Matches
debug_break.h
1/* Copyright (c) 2011-2021, Scott Tsai
2 *
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
13 *
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.
25 */
26#ifndef DEBUG_BREAK_H
27#define DEBUG_BREAK_H
28
29#ifdef _MSC_VER
30inline static void debug_break(void)
31{
32 __debugbreak();
33}
34#else
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40#define DEBUG_BREAK_USE_TRAP_INSTRUCTION 1
41#define DEBUG_BREAK_USE_BULTIN_TRAP 2
42#define DEBUG_BREAK_USE_SIGTRAP 3
43
44#if defined(__i386__) || defined(__x86_64__)
45#define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
46__inline__ static void trap_instruction(void)
47{
48 __asm__ volatile("int $0x03");
49}
50#elif defined(__thumb__)
51#define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
52/* FIXME: handle __THUMB_INTERWORK__ */
53__attribute__((always_inline)) __inline__ static void trap_instruction(void)
54{
55 /* See 'arm-linux-tdep.c' in GDB source.
56 * Both instruction sequences below work. */
57#if 1
58 /* 'eabi_linux_thumb_le_breakpoint' */
59 __asm__ volatile(".inst 0xde01");
60#else
61 /* 'eabi_linux_thumb2_le_breakpoint' */
62 __asm__ volatile(".inst.w 0xf7f0a000");
63#endif
64
65 /* Known problem:
66 * After a breakpoint hit, can't 'stepi', 'step', or 'continue' in GDB.
67 * 'step' would keep getting stuck on the same instruction.
68 *
69 * Workaround: use the new GDB commands 'debugbreak-step' and
70 * 'debugbreak-continue' that become available
71 * after you source the script from GDB:
72 *
73 * $ gdb -x debugbreak-gdb.py <... USUAL ARGUMENTS ...>
74 *
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)
80 */
81}
82#elif defined(__arm__) && !defined(__thumb__)
83#define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
84__attribute__((always_inline)) __inline__ static void trap_instruction(void)
85{
86 /* See 'arm-linux-tdep.c' in GDB source,
87 * 'eabi_linux_arm_le_breakpoint' */
88 __asm__ volatile(".inst 0xe7f001f0");
89 /* Known problem:
90 * Same problem and workaround as Thumb mode */
91}
92#elif defined(__aarch64__) && defined(__APPLE__)
93#define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_BULTIN_DEBUGTRAP
94#elif defined(__aarch64__)
95#define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
96__attribute__((always_inline)) __inline__ static void trap_instruction(void)
97{
98 /* See 'aarch64-tdep.c' in GDB source,
99 * 'aarch64_default_breakpoint' */
100 __asm__ volatile(".inst 0xd4200000");
101}
102#elif defined(__powerpc__)
103/* PPC 32 or 64-bit, big or little endian */
104#define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
105__attribute__((always_inline)) __inline__ static void trap_instruction(void)
106{
107 /* See 'rs6000-tdep.c' in GDB source,
108 * 'rs6000_breakpoint' */
109 __asm__ volatile(".4byte 0x7d821008");
110
111 /* Known problem:
112 * After a breakpoint hit, can't 'stepi', 'step', or 'continue' in GDB.
113 * 'step' stuck on the same instruction ("twge r2,r2").
114 *
115 * The workaround is the same as ARM Thumb mode: use debugbreak-gdb.py
116 * or manually jump over the instruction. */
117}
118#elif defined(__riscv)
119/* RISC-V 32 or 64-bit, whether the "C" extension
120 * for compressed, 16-bit instructions are supported or not */
121#define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
122__attribute__((always_inline)) __inline__ static void trap_instruction(void)
123{
124 /* See 'riscv-tdep.c' in GDB source,
125 * 'riscv_sw_breakpoint_from_kind' */
126 __asm__ volatile(".4byte 0x00100073");
127}
128#else
129#define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_SIGTRAP
130#endif
131
132
133#ifndef DEBUG_BREAK_IMPL
134#error "debugbreak.h is not supported on this target"
135#elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_TRAP_INSTRUCTION
136__attribute__((always_inline)) __inline__ static void debug_break(void)
137{
138 trap_instruction();
139}
140#elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_BULTIN_DEBUGTRAP
141__attribute__((always_inline)) __inline__ static void debug_break(void)
142{
143 __builtin_debugtrap();
144}
145#elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_BULTIN_TRAP
146__attribute__((always_inline)) __inline__ static void debug_break(void)
147{
148 __builtin_trap();
149}
150#elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_SIGTRAP
151#include <signal.h>
152__attribute__((always_inline)) __inline__ static void debug_break(void)
153{
154 raise(SIGTRAP);
155}
156#else
157#error "invalid DEBUG_BREAK_IMPL value"
158#endif
159
160#ifdef __cplusplus
161}
162#endif
163
164#endif /* ifdef _MSC_VER */
165
166#endif /* ifndef DEBUG_BREAK_H */