Debian Codegraph @

ddebug/debugbreak.h

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /* Copyright (c) 2011-2021, Scott Tsai
20 *
21 * All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions are met:
25 *
26 * 1. Redistributions of source code must retain the above copyright notice,
27 * this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright notice,
29 * this list of conditions and the following disclaimer in the documentation
30 * and/or other materials provided with the distribution.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
36 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGE.
43 */
44 #ifndef DEBUG_BREAK_H
45 #define DEBUG_BREAK_H
46
47 #ifdef _MSC_VER
48
49 #define debug_break __debugbreak
50
51 #else
52
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56
57 #define DEBUG_BREAK_USE_TRAP_INSTRUCTION 1
58 #define DEBUG_BREAK_USE_BULTIN_TRAP 2
59 #define DEBUG_BREAK_USE_SIGTRAP 3
60
61 #if defined(__i386__) || defined(__x86_64__)
62 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
63 __inline__ static void trap_instruction(void)
64 {
65 __asm__ volatile("int $0x03");
66 }
67 #elif defined(__thumb__)
68 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
69 /* FIXME: handle __THUMB_INTERWORK__ */
70 __attribute__((always_inline))
71 __inline__ static void trap_instruction(void)
72 {
73 /* See 'arm-linux-tdep.c' in GDB source.
74 * Both instruction sequences below work. */
75 #if 1
76 /* 'eabi_linux_thumb_le_breakpoint' */
77 __asm__ volatile(".inst 0xde01");
78 #else
79 /* 'eabi_linux_thumb2_le_breakpoint' */
80 __asm__ volatile(".inst.w 0xf7f0a000");
81 #endif
82
83 /* Known problem:
84 * After a breakpoint hit, can't 'stepi', 'step', or 'continue' in GDB.
85 * 'step' would keep getting stuck on the same instruction.
86 *
87 * Workaround: use the new GDB commands 'debugbreak-step' and
88 * 'debugbreak-continue' that become available
89 * after you source the script from GDB:
90 *
91 * $ gdb -x debugbreak-gdb.py <... USUAL ARGUMENTS ...>
92 *
93 * 'debugbreak-step' would jump over the breakpoint instruction with
94 * roughly equivalent of:
95 * (gdb) set $instruction_len = 2
96 * (gdb) tbreak *($pc + $instruction_len)
97 * (gdb) jump *($pc + $instruction_len)
98 */
99 }
100 #elif defined(__arm__) && !defined(__thumb__)
101 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
102 __attribute__((always_inline))
103 __inline__ static void trap_instruction(void)
104 {
105 /* See 'arm-linux-tdep.c' in GDB source,
106 * 'eabi_linux_arm_le_breakpoint' */
107 __asm__ volatile(".inst 0xe7f001f0");
108 /* Known problem:
109 * Same problem and workaround as Thumb mode */
110 }
111 #elif defined(__aarch64__) && defined(__APPLE__)
112 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_BULTIN_DEBUGTRAP
113 #elif defined(__aarch64__)
114 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
115 __attribute__((always_inline))
116 __inline__ static void trap_instruction(void)
117 {
118 /* See 'aarch64-tdep.c' in GDB source,
119 * 'aarch64_default_breakpoint' */
120 __asm__ volatile(".inst 0xd4200000");
121 }
122 #elif defined(__powerpc__)
123 /* PPC 32 or 64-bit, big or little endian */
124 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
125 __attribute__((always_inline))
126 __inline__ static void trap_instruction(void)
127 {
128 /* See 'rs6000-tdep.c' in GDB source,
129 * 'rs6000_breakpoint' */
130 __asm__ volatile(".4byte 0x7d821008");
131
132 /* Known problem:
133 * After a breakpoint hit, can't 'stepi', 'step', or 'continue' in GDB.
134 * 'step' stuck on the same instruction ("twge r2,r2").
135 *
136 * The workaround is the same as ARM Thumb mode: use debugbreak-gdb.py
137 * or manually jump over the instruction. */
138 }
139 #elif defined(__riscv)
140 /* RISC-V 32 or 64-bit, whether the "C" extension
141 * for compressed, 16-bit instructions are supported or not */
142 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
143 __attribute__((always_inline))
144 __inline__ static void trap_instruction(void)
145 {
146 /* See 'riscv-tdep.c' in GDB source,
147 * 'riscv_sw_breakpoint_from_kind' */
148 __asm__ volatile(".4byte 0x00100073");
149 }
150 #else
151 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_SIGTRAP
152 #endif
153
154
155 #ifndef DEBUG_BREAK_IMPL
156 #error "debugbreak.h is not supported on this target"
157 #elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_TRAP_INSTRUCTION
158 __attribute__((always_inline))
159 __inline__ static void debug_break(void)
160 {
161 trap_instruction();
162 }
163 #elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_BULTIN_DEBUGTRAP
164 __attribute__((always_inline))
165 __inline__ static void debug_break(void)
166 {
167 __builtin_debugtrap();
168 }
169 #elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_BULTIN_TRAP
170 __attribute__((always_inline))
171 __inline__ static void debug_break(void)
172 {
173 __builtin_trap();
174 }
175 #elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_SIGTRAP
176 #include <signal.h>
177 __attribute__((always_inline))
178 __inline__ static void debug_break(void)
179 {
180 raise(SIGTRAP);
181 }
182 #else
183 #error "invalid DEBUG_BREAK_IMPL value"
184 #endif
185
186 #ifdef __cplusplus
187 }
188 #endif
189
190 #endif /* ifdef _MSC_VER */
191
192 #endif /* ifndef DEBUG_BREAK_H */