Seamly2D
Code documentation
debugbreak.h
Go to the documentation of this file.
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
30 
31 #define debug_break __debugbreak
32 
33 #else
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 #define DEBUG_BREAK_USE_TRAP_INSTRUCTION 1
40 #define DEBUG_BREAK_USE_BULTIN_TRAP 2
41 #define DEBUG_BREAK_USE_SIGTRAP 3
42 
43 #if defined(__i386__) || defined(__x86_64__)
44  #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
45 __inline__ static void trap_instruction(void)
46 {
47  __asm__ volatile("int $0x03");
48 }
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)
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))
85 __inline__ static void trap_instruction(void)
86 {
87  /* See 'arm-linux-tdep.c' in GDB source,
88  * 'eabi_linux_arm_le_breakpoint' */
89  __asm__ volatile(".inst 0xe7f001f0");
90  /* Known problem:
91  * Same problem and workaround as Thumb mode */
92 }
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)
99 {
100  /* See 'aarch64-tdep.c' in GDB source,
101  * 'aarch64_default_breakpoint' */
102  __asm__ volatile(".inst 0xd4200000");
103 }
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)
109 {
110  /* See 'rs6000-tdep.c' in GDB source,
111  * 'rs6000_breakpoint' */
112  __asm__ volatile(".4byte 0x7d821008");
113 
114  /* Known problem:
115  * After a breakpoint hit, can't 'stepi', 'step', or 'continue' in GDB.
116  * 'step' stuck on the same instruction ("twge r2,r2").
117  *
118  * The workaround is the same as ARM Thumb mode: use debugbreak-gdb.py
119  * or manually jump over the instruction. */
120 }
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)
127 {
128  /* See 'riscv-tdep.c' in GDB source,
129  * 'riscv_sw_breakpoint_from_kind' */
130  __asm__ volatile(".4byte 0x00100073");
131 }
132 #else
133  #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_SIGTRAP
134 #endif
135 
136 
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)
142 {
143  trap_instruction();
144 }
145 #elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_BULTIN_DEBUGTRAP
146 __attribute__((always_inline))
147 __inline__ static void debug_break(void)
148 {
149  __builtin_debugtrap();
150 }
151 #elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_BULTIN_TRAP
152 __attribute__((always_inline))
153 __inline__ static void debug_break(void)
154 {
155  __builtin_trap();
156 }
157 #elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_SIGTRAP
158 #include <signal.h>
159 __attribute__((always_inline))
160 __inline__ static void debug_break(void)
161 {
162  raise(SIGTRAP);
163 }
164 #else
165 #error "invalid DEBUG_BREAK_IMPL value"
166 #endif
167 
168 #ifdef __cplusplus
169 }
170 #endif
171 
172 #endif /* ifdef _MSC_VER */
173 
174 #endif /* ifndef DEBUG_BREAK_H */
__attribute__((always_inline)) __inline__ static void debug_break(void)
Definition: debugbreak.h:159