Debian Codegraph @

src/mpt/base/debugging.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_BASE_DEBUGGING_HPP
4 #define MPT_BASE_DEBUGGING_HPP
5
6
7
8 #include "mpt/base/detect.hpp"
9 #include "mpt/base/integer.hpp"
10 #include "mpt/base/namespace.hpp"
11 #include "mpt/base/macros.hpp"
12
13 #if MPT_CXX_AT_LEAST(26)
14 #include <debugging>
15 #endif // C++26
16
17 #if MPT_CXX_BEFORE(26)
18 #if MPT_OS_LINUX
19 #include <cerrno>
20 #include <cstring>
21 #endif
22 #endif // !C++26
23
24 #if MPT_CXX_BEFORE(26)
25 #if MPT_OS_LINUX
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <unistd.h>
29 #endif
30 #endif // !C++26
31
32 #if MPT_CXX_BEFORE(26)
33 #if MPT_OS_WINDOWS
34 #include <windows.h>
35 #endif
36 #endif // !C++26
37
38 #if MPT_CXX_BEFORE(26)
39 #if MPT_COMPILER_MSVC
40 #include <intrin.h>
41 #endif
42 #endif // !C++26
43
44
45
46 namespace mpt {
47 inline namespace MPT_INLINE_NS {
48
49
50
51 #if MPT_CXX_AT_LEAST(26)
52
53 using std::breakpoint;
54 using std::breakpoint_if_debugging;
55 using std::is_debugger_present;
56
57 #else // !C++26
58
59 #if MPT_OS_WINDOWS
60
61 inline bool is_debugger_present() noexcept {
62 return (IsDebuggerPresent() != FALSE);
63 }
64
65 MPT_FORCEINLINE void breakpoint() noexcept {
66 #if MPT_COMPILER_MSVC
67 __debugbreak();
68 #elif MPT_COMPILER_CLANG
69 __builtin_debugtrap();
70 #elif MPT_COMPILER_GCC && (MPT_ARCH_X86 || MPT_ARCH_AMD64)
71 __asm__ __volatile__("int 3");
72 #else
73 DebugBreak();
74 #endif
75 }
76
77 #elif MPT_OS_LINUX
78
79 namespace detail {
80 namespace debugging {
81
82 inline bool parse_proc_status_line(char * buf, std::size_t size) noexcept {
83 if (std::strncmp(buf, "TracerPid:\t", 11) != 0) {
84 return false;
85 }
86 unsigned long long pid = 0;
87 std::size_t pos = 11;
88 while (pos < size) {
89 unsigned char byte = static_cast<unsigned char>(buf[pos]);
90 if (!(static_cast<unsigned char>('0') <= byte && byte <= static_cast<unsigned char>('9'))) {
91 return false;
92 }
93 uint8 digit = static_cast<uint8>(byte - static_cast<unsigned char>('0'));
94 pid = (pid * 10) + digit;
95 pos++;
96 }
97 return (pid != 0);
98 }
99
100 inline bool parse_proc_status() noexcept {
101 int olderrno = errno;
102 bool detected_debugger = false;
103 int error = 0;
104 int fd = -1;
105 bool open_done = false;
106 while (!open_done) {
107 int res = open("/proc/self/status", O_RDONLY);
108 if (res >= 0) {
109 fd = res;
110 open_done = true;
111 } else if (errno != EINTR) {
112 error = errno;
113 open_done = true;
114 }
115 }
116 if (error != 0) {
117 errno = olderrno;
118 return false;
119 }
120 if (fd < 0) {
121 errno = olderrno;
122 return false;
123 }
124 bool eof = false;
125 uint8 iobuf[1024];
126 std::size_t iobuf_size = 0;
127 char linebuf[128];
128 std::size_t linebuf_size = 0;
129 while (!eof) {
130 ssize_t bytes_read = read(fd, iobuf, 1024);
131 if (bytes_read == -1) {
132 if (errno == EINTR) {
133 continue;
134 }
135 } else if (bytes_read == 0) {
136 eof = true;
137 }
138 iobuf_size = static_cast<std::size_t>(bytes_read);
139 for (std::size_t i = 0; i < iobuf_size; ++i) {
140 if (static_cast<unsigned char>(iobuf[i]) == static_cast<unsigned char>('\n')) {
141 if (parse_proc_status_line(linebuf, linebuf_size)) {
142 detected_debugger = true;
143 }
144 linebuf_size = 0;
145 } else {
146 if (linebuf_size < 128) {
147 linebuf[linebuf_size] = static_cast<char>(static_cast<unsigned char>(iobuf[i]));
148 linebuf_size++;
149 }
150 }
151 }
152 if (linebuf_size > 0) {
153 if (parse_proc_status_line(linebuf, linebuf_size)) {
154 detected_debugger = true;
155 }
156 linebuf_size = 0;
157 }
158 }
159 bool close_done = false;
160 while (!close_done) {
161 int res = close(fd);
162 if (res == 0) {
163 fd = -1;
164 close_done = true;
165 } else if (errno != EINTR) {
166 error = errno;
167 close_done = true;
168 }
169 }
170 if (error != 0) {
171 errno = olderrno;
172 return false;
173 }
174 errno = olderrno;
175 return detected_debugger;
176 }
177
178 } // namespace debugging
179 } // namespace detail
180
181 inline MPT_NOINLINE bool is_debugger_present() noexcept {
182 return mpt::detail::debugging::parse_proc_status();
183 }
184
185 MPT_FORCEINLINE void breakpoint() noexcept {
186 #if MPT_COMPILER_CLANG
187 __builtin_debugtrap();
188 #elif MPT_COMPILER_GCC && (MPT_ARCH_X86 || MPT_ARCH_AMD64)
189 __asm__ __volatile__("int 3");
190 #else
191 kill(getpid(), SIGTRAP);
192 #endif
193 }
194
195 #else
196
197 inline bool is_debugger_present() noexcept {
198 return false;
199 }
200
201 MPT_FORCEINLINE void breakpoint() noexcept {
202 #if MPT_COMPILER_MSVC
203 __debugbreak();
204 #elif MPT_COMPILER_CLANG
205 __builtin_debugtrap();
206 #elif MPT_COMPILER_GCC && (MPT_ARCH_X86 || MPT_ARCH_AMD64)
207 __asm__ __volatile__("int 3");
208 #endif
209 }
210
211 #endif
212
213 MPT_FORCEINLINE void breakpoint_if_debugging() noexcept {
214 if (mpt::is_debugger_present()) {
215 mpt::breakpoint();
216 }
217 }
218
219 #endif // C++26
220
221
222
223 } // namespace MPT_INLINE_NS
224 } // namespace mpt
225
226
227
228 #endif // MPT_BASE_DEBUGGING_HPP