Line | Branch | Exec | Source |
---|---|---|---|
1 | /* GATE PROJECT LICENSE: | ||
2 | +----------------------------------------------------------------------------+ | ||
3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
4 | | All rights reserved. | | ||
5 | | | | ||
6 | | Redistribution and use in source and binary forms, with or without | | ||
7 | | modification, are permitted provided that the following conditions are met:| | ||
8 | | | | ||
9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
10 | | this list of conditions and the following disclaimer. | | ||
11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
12 | | notice, this list of conditions and the following disclaimer in the | | ||
13 | | documentation and/or other materials provided with the distribution. | | ||
14 | | | | ||
15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
26 | +----------------------------------------------------------------------------+ | ||
27 | */ | ||
28 | |||
29 | #include "gate/debugging.h" | ||
30 | |||
31 | #include "gate/streams.h" | ||
32 | #include "gate/strings.h" | ||
33 | #include "gate/platforms.h" | ||
34 | |||
35 | 509598 | void gate_debug_assert(gate_bool_t xpression, char const* msg, char const* file, unsigned int line) | |
36 | { | ||
37 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 509598 times.
|
509598 | if (!xpression) |
38 | { | ||
39 | ✗ | gate_debug_trace(msg, file, line); | |
40 | ✗ | GATE_DEBUG_BREAKPOINT; | |
41 | ✗ | gate_platform_exit(1); | |
42 | } | ||
43 | 509598 | } | |
44 | |||
45 | 43 | void gate_debug_trace(char const* msg, char const* file, unsigned int line) | |
46 | { | ||
47 | 43 | char const* msgs[16] = GATE_INIT_EMPTY; | |
48 | 43 | gate_size_t msg_count = 0; | |
49 | 43 | char lineBuffer[16] = GATE_INIT_EMPTY; | |
50 | |||
51 |
1/2✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
|
43 | if (!gate_str_is_empty(file)) |
52 | { | ||
53 | 43 | msgs[msg_count++] = file; | |
54 |
1/2✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
|
43 | if (line) |
55 | { | ||
56 | 43 | gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line); | |
57 | 43 | msgs[msg_count++] = "("; | |
58 | 43 | msgs[msg_count++] = lineBuffer; | |
59 | 43 | msgs[msg_count++] = ")"; | |
60 | } | ||
61 | 43 | msgs[msg_count++] = ": "; | |
62 | } | ||
63 | else | ||
64 | { | ||
65 | ✗ | if (line) | |
66 | { | ||
67 | ✗ | gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line); | |
68 | ✗ | msgs[msg_count++] = "line "; | |
69 | ✗ | msgs[msg_count++] = lineBuffer; | |
70 | ✗ | msgs[msg_count++] = ": "; | |
71 | } | ||
72 | } | ||
73 | |||
74 |
1/2✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
|
43 | if (!gate_str_is_empty(msg)) |
75 | { | ||
76 | 43 | msgs[msg_count++] = msg; | |
77 | } | ||
78 |
1/2✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
|
43 | if (msg_count != 0) |
79 | { | ||
80 | 43 | gate_debug_trace_messages(msgs, msg_count); | |
81 | } | ||
82 | 43 | } | |
83 | 38 | void gate_debug_trace_value(char const* msg, gate_int64_t value, char const* file, unsigned int line) | |
84 | { | ||
85 | 38 | char const* msgs[16] = GATE_INIT_EMPTY; | |
86 | 38 | gate_size_t msg_count = 0; | |
87 | 38 | char lineBuffer[16] = GATE_INIT_EMPTY; | |
88 | 38 | char valueBuffer[24] = GATE_INIT_EMPTY; | |
89 | |||
90 |
1/2✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
|
38 | if (!gate_str_is_empty(file)) |
91 | { | ||
92 | 38 | msgs[msg_count++] = file; | |
93 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | if (line) |
94 | { | ||
95 | 38 | gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line); | |
96 | 38 | msgs[msg_count++] = "("; | |
97 | 38 | msgs[msg_count++] = lineBuffer; | |
98 | 38 | msgs[msg_count++] = ")"; | |
99 | } | ||
100 | 38 | msgs[msg_count++] = ": "; | |
101 | } | ||
102 | else | ||
103 | { | ||
104 | ✗ | if (line) | |
105 | { | ||
106 | ✗ | gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line); | |
107 | ✗ | msgs[msg_count++] = "line "; | |
108 | ✗ | msgs[msg_count++] = lineBuffer; | |
109 | ✗ | msgs[msg_count++] = ": "; | |
110 | } | ||
111 | } | ||
112 | |||
113 |
1/2✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
|
38 | if (!gate_str_is_empty(msg)) |
114 | { | ||
115 | 38 | gate_str_print_int64(valueBuffer, sizeof(valueBuffer), value); | |
116 | |||
117 | 38 | msgs[msg_count++] = msg; | |
118 | 38 | msgs[msg_count++] = " = "; | |
119 | 38 | msgs[msg_count++] = valueBuffer; | |
120 | } | ||
121 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | if (msg_count != 0) |
122 | { | ||
123 | 38 | gate_debug_trace_messages(msgs, msg_count); | |
124 | } | ||
125 | 38 | } | |
126 | 12 | void gate_debug_trace_msg_value(char const* msg, char const* value_name, gate_int64_t value, char const* file, unsigned int line) | |
127 | { | ||
128 | 12 | char const* msgs[16] = GATE_INIT_EMPTY; | |
129 | 12 | gate_size_t msg_count = 0; | |
130 | 12 | char lineBuffer[16] = GATE_INIT_EMPTY; | |
131 | 12 | char valueBuffer[24] = GATE_INIT_EMPTY; | |
132 | |||
133 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | if (!gate_str_is_empty(file)) |
134 | { | ||
135 | 12 | msgs[msg_count++] = file; | |
136 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (line) |
137 | { | ||
138 | 12 | gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line); | |
139 | 12 | msgs[msg_count++] = "("; | |
140 | 12 | msgs[msg_count++] = lineBuffer; | |
141 | 12 | msgs[msg_count++] = ")"; | |
142 | } | ||
143 | 12 | msgs[msg_count++] = ": "; | |
144 | } | ||
145 | else | ||
146 | { | ||
147 | ✗ | if (line) | |
148 | { | ||
149 | ✗ | gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line); | |
150 | ✗ | msgs[msg_count++] = "line "; | |
151 | ✗ | msgs[msg_count++] = lineBuffer; | |
152 | ✗ | msgs[msg_count++] = ": "; | |
153 | } | ||
154 | } | ||
155 | |||
156 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | if (!gate_str_is_empty(msg)) |
157 | { | ||
158 | 12 | msgs[msg_count++] = msg; | |
159 | 12 | msgs[msg_count++] = "; "; | |
160 | } | ||
161 | |||
162 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | if (!gate_str_is_empty(value_name)) |
163 | { | ||
164 | 12 | msgs[msg_count++] = value_name; | |
165 | 12 | msgs[msg_count++] = " = "; | |
166 | } | ||
167 | 12 | gate_str_print_int64(valueBuffer, sizeof(valueBuffer), value); | |
168 | 12 | msgs[msg_count++] = valueBuffer; | |
169 | |||
170 | |||
171 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (msg_count != 0) |
172 | { | ||
173 | 12 | gate_debug_trace_messages(msgs, msg_count); | |
174 | } | ||
175 | 12 | } | |
176 | |||
177 | |||
178 | ✗ | void gate_debug_trace_platform_error(char const* msg, char const* file, unsigned int line) | |
179 | { | ||
180 | ✗ | char const* msgs[16] = GATE_INIT_EMPTY; | |
181 | ✗ | gate_size_t msg_count = 0; | |
182 | ✗ | char lineBuffer[16] = GATE_INIT_EMPTY; | |
183 | ✗ | char codeBuffer[24] = GATE_INIT_EMPTY; | |
184 | ✗ | gate_int32_t platform_error = gate_platform_get_last_error(); | |
185 | ✗ | char errorMsg[1024] = GATE_INIT_EMPTY; | |
186 | |||
187 | ✗ | if (!gate_str_is_empty(file)) | |
188 | { | ||
189 | ✗ | msgs[msg_count++] = file; | |
190 | ✗ | if (line) | |
191 | { | ||
192 | ✗ | gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line); | |
193 | ✗ | msgs[msg_count++] = "("; | |
194 | ✗ | msgs[msg_count++] = lineBuffer; | |
195 | ✗ | msgs[msg_count++] = ")"; | |
196 | } | ||
197 | ✗ | msgs[msg_count++] = ": "; | |
198 | } | ||
199 | else | ||
200 | { | ||
201 | ✗ | if (line) | |
202 | { | ||
203 | ✗ | gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line); | |
204 | ✗ | msgs[msg_count++] = "line "; | |
205 | ✗ | msgs[msg_count++] = lineBuffer; | |
206 | ✗ | msgs[msg_count++] = ": "; | |
207 | } | ||
208 | } | ||
209 | |||
210 | ✗ | if (!gate_str_is_empty(msg)) | |
211 | { | ||
212 | ✗ | msgs[msg_count++] = msg; | |
213 | ✗ | msgs[msg_count++] = ", "; | |
214 | } | ||
215 | |||
216 | ✗ | gate_str_print_int64(codeBuffer, sizeof(codeBuffer), (gate_int64_t)platform_error); | |
217 | ✗ | msgs[msg_count++] = "#"; | |
218 | ✗ | msgs[msg_count++] = codeBuffer; | |
219 | |||
220 | ✗ | gate_platform_print_error(platform_error, errorMsg, sizeof(errorMsg)); | |
221 | ✗ | if (!gate_str_is_empty(errorMsg)) | |
222 | { | ||
223 | ✗ | msgs[msg_count++] = ": "; | |
224 | ✗ | msgs[msg_count++] = errorMsg; | |
225 | } | ||
226 | |||
227 | ✗ | if (msg_count != 0) | |
228 | { | ||
229 | ✗ | gate_debug_trace_messages(msgs, msg_count); | |
230 | } | ||
231 | ✗ | } | |
232 | ✗ | void gate_debug_trace_result(gate_result_t result, char const* origin, char const* msg, gate_int32_t error_code, | |
233 | char const* file, unsigned int line) | ||
234 | { | ||
235 | ✗ | char const* msgs[24] = GATE_INIT_EMPTY; | |
236 | ✗ | gate_size_t msg_count = 0; | |
237 | ✗ | char lineBuffer[16] = GATE_INIT_EMPTY; | |
238 | ✗ | char resultBuffer[24] = GATE_INIT_EMPTY; | |
239 | ✗ | char errorBuffer[24] = GATE_INIT_EMPTY; | |
240 | |||
241 | ✗ | if (!gate_str_is_empty(file)) | |
242 | { | ||
243 | ✗ | msgs[msg_count++] = file; | |
244 | ✗ | if (line) | |
245 | { | ||
246 | ✗ | gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line); | |
247 | ✗ | msgs[msg_count++] = "("; | |
248 | ✗ | msgs[msg_count++] = lineBuffer; | |
249 | ✗ | msgs[msg_count++] = ")"; | |
250 | } | ||
251 | ✗ | msgs[msg_count++] = ":"; | |
252 | } | ||
253 | else | ||
254 | { | ||
255 | ✗ | if (line) | |
256 | { | ||
257 | ✗ | gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line); | |
258 | ✗ | msgs[msg_count++] = "line "; | |
259 | ✗ | msgs[msg_count++] = lineBuffer; | |
260 | ✗ | msgs[msg_count++] = ":"; | |
261 | } | ||
262 | } | ||
263 | |||
264 | ✗ | if (!gate_str_is_empty(origin)) | |
265 | { | ||
266 | ✗ | msgs[msg_count++] = " "; | |
267 | ✗ | msgs[msg_count++] = origin; | |
268 | ✗ | msgs[msg_count++] = ":"; | |
269 | } | ||
270 | |||
271 | ✗ | if (GATE_FAILED(result)) | |
272 | { | ||
273 | ✗ | msgs[msg_count++] = " #"; | |
274 | ✗ | gate_str_print_int64(resultBuffer, sizeof(resultBuffer), (gate_int64_t)result); | |
275 | ✗ | msgs[msg_count++] = resultBuffer; | |
276 | } | ||
277 | |||
278 | ✗ | if (error_code != 0) | |
279 | { | ||
280 | ✗ | msgs[msg_count++] = " %"; | |
281 | ✗ | gate_str_print_int64(errorBuffer, sizeof(errorBuffer), (gate_int64_t)error_code); | |
282 | ✗ | msgs[msg_count++] = errorBuffer; | |
283 | } | ||
284 | |||
285 | |||
286 | ✗ | if (!gate_str_is_empty(msg)) | |
287 | { | ||
288 | ✗ | msgs[msg_count++] = " "; | |
289 | ✗ | msgs[msg_count++] = msg; | |
290 | } | ||
291 | |||
292 | ✗ | if (msg_count != 0) | |
293 | { | ||
294 | ✗ | gate_debug_trace_messages(msgs, msg_count); | |
295 | } | ||
296 | ✗ | } | |
297 | |||
298 | |||
299 | |||
300 | #if defined(GATE_SYS_WIN) | ||
301 | |||
302 | #include "gate/platforms.h" | ||
303 | |||
304 | void gate_debug_breakpoint() | ||
305 | { | ||
306 | DebugBreak(); | ||
307 | } | ||
308 | |||
309 | #if defined(GATE_SYS_WINCE) | ||
310 | |||
311 | void gate_debug_trace_messages(char const* const* msgs, gate_size_t msg_count) | ||
312 | { | ||
313 | wchar_t msgbuffer[4096]; | ||
314 | |||
315 | if (gate_platform.Win32IsDebuggerPresent) | ||
316 | { | ||
317 | if (gate_platform.Win32IsDebuggerPresent()) | ||
318 | { | ||
319 | while (msg_count-- != 0) | ||
320 | { | ||
321 | gate_win32_ansi_2_utf16(*msgs, gate_str_length(*msgs), msgbuffer, sizeof(msgbuffer) / sizeof(msgbuffer[0])); | ||
322 | |||
323 | OutputDebugString(msgbuffer); | ||
324 | ++msgs; | ||
325 | } | ||
326 | OutputDebugString(_T("\r\n")); | ||
327 | } | ||
328 | } | ||
329 | } | ||
330 | |||
331 | #else /* defined(GATE_SYS_WINCE) */ | ||
332 | |||
333 | #if defined(GATE_SYS_WIN16) | ||
334 | # define OutputDebugStringA OutputDebugString | ||
335 | #endif | ||
336 | |||
337 | void gate_debug_trace_messages(char const* const* msgs, gate_size_t msg_count) | ||
338 | { | ||
339 | #if defined(GATE_SYS_WIN16) | ||
340 | while (msg_count-- != 0) | ||
341 | { | ||
342 | OutputDebugString(*msgs); | ||
343 | ++msgs; | ||
344 | } | ||
345 | OutputDebugString("\r\n"); | ||
346 | #else | ||
347 | HANDLE hErr = (HANDLE)NULL; | ||
348 | if (gate_platform.Win32IsDebuggerPresent) | ||
349 | { | ||
350 | if (gate_platform.Win32IsDebuggerPresent()) | ||
351 | { | ||
352 | while (msg_count-- != 0) | ||
353 | { | ||
354 | OutputDebugStringA(*msgs); | ||
355 | ++msgs; | ||
356 | } | ||
357 | OutputDebugStringA("\r\n"); | ||
358 | return; | ||
359 | } | ||
360 | } | ||
361 | hErr = GetStdHandle(STD_ERROR_HANDLE); | ||
362 | if ((INVALID_HANDLE_VALUE != hErr) && (NULL != hErr)) | ||
363 | { | ||
364 | while (msg_count-- != 0) | ||
365 | { | ||
366 | if (*msgs) | ||
367 | { | ||
368 | gate_win32_writefile(hErr, *msgs, (DWORD)gate_str_length(*msgs), NULL, NULL); | ||
369 | } | ||
370 | ++msgs; | ||
371 | } | ||
372 | gate_win32_writefile(hErr, "\r\n", 2, NULL, NULL); | ||
373 | return; | ||
374 | } | ||
375 | #endif | ||
376 | } | ||
377 | |||
378 | #endif /* defined(GATE_SYS_WINCE) */ | ||
379 | |||
380 | #endif /* defined(GATE_SYS_WIN) */ | ||
381 | |||
382 | |||
383 | |||
384 | |||
385 | |||
386 | #if defined(GATE_SYS_POSIX) | ||
387 | |||
388 | #include <stdio.h> | ||
389 | #include <signal.h> | ||
390 | |||
391 | ✗ | void gate_debug_breakpoint() | |
392 | { | ||
393 | ✗ | raise(SIGINT); | |
394 | ✗ | } | |
395 | |||
396 | 93 | void gate_debug_trace_messages(char const* const* msgs, gate_size_t msg_count) | |
397 | { | ||
398 |
2/2✓ Branch 0 taken 682 times.
✓ Branch 1 taken 93 times.
|
775 | while (msg_count-- != 0) |
399 | { | ||
400 |
1/2✓ Branch 0 taken 682 times.
✗ Branch 1 not taken.
|
682 | if (*msgs) |
401 | { | ||
402 | 682 | fprintf(stderr, "%s", *msgs); | |
403 | } | ||
404 | 682 | ++msgs; | |
405 | } | ||
406 | 93 | fprintf(stderr, "\n"); | |
407 | 93 | } | |
408 | |||
409 | #endif /* defined(GATE_SYS_POSIX) */ | ||
410 | |||
411 | |||
412 | #if defined(GATE_SYS_DOS) | ||
413 | |||
414 | #include <stdio.h> | ||
415 | #include <signal.h> | ||
416 | |||
417 | void gate_debug_breakpoint() | ||
418 | { | ||
419 | raise(SIGINT); | ||
420 | } | ||
421 | |||
422 | void gate_debug_trace_messages(char const* const* msgs, gate_size_t msg_count) | ||
423 | { | ||
424 | while (msg_count-- != 0) | ||
425 | { | ||
426 | if (*msgs) | ||
427 | { | ||
428 | fprintf(stderr, "%s", *msgs); | ||
429 | } | ||
430 | ++msgs; | ||
431 | } | ||
432 | fprintf(stderr, "\n"); | ||
433 | } | ||
434 | |||
435 | #endif /* GATE_SYS_DOS */ | ||
436 | |||
437 | #if defined(GATE_SYS_EFI) | ||
438 | |||
439 | #include "gate/platform//efi/efi_gate.h" | ||
440 | |||
441 | void gate_debug_breakpoint() | ||
442 | { | ||
443 | } | ||
444 | |||
445 | void gate_debug_trace_messages(char const* const* msgs, gate_size_t msg_count) | ||
446 | { | ||
447 | gate_char16_t buffer[4096 + 2048 + 1024]; | ||
448 | static gate_size_t const buffer_len = sizeof(buffer) / sizeof(buffer[0]); | ||
449 | |||
450 | while (msg_count-- != 0) | ||
451 | { | ||
452 | if (*msgs) | ||
453 | { | ||
454 | gate_str_utf8_2_utf16(*msgs, gate_str_length(*msgs), buffer, buffer_len); | ||
455 | gate_platform_efi_print_debug_message(buffer); | ||
456 | } | ||
457 | ++msgs; | ||
458 | } | ||
459 | gate_platform_efi_print_debug_message(L"\r\n"); | ||
460 | } | ||
461 | |||
462 | #endif /* GATE_SYS_EFI */ | ||
463 | |||
464 | #if defined(GATE_SYS_WASM) | ||
465 | |||
466 | void gate_debug_breakpoint() | ||
467 | { | ||
468 | /* TODO */ | ||
469 | } | ||
470 | |||
471 | void gate_debug_trace_messages(char const* const* msgs, gate_size_t msg_count) | ||
472 | { | ||
473 | /* TODO */ | ||
474 | } | ||
475 | |||
476 | |||
477 | #endif | ||
478 |