GCC Code Coverage Report


Directory: src/gate/
File: src/gate/debugging.c
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 132 163 81.0%
Functions: 7 8 87.5%
Branches: 29 66 43.9%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, 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 1 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 1 times.
1 if (!xpression)
38 {
39 ✗ gate_debug_trace(msg, file, line);
40 ✗ GATE_DEBUG_BREAKPOINT;
41 ✗ gate_platform_exit(1);
42 }
43 1 }
44
45 static char const strBraceOpen[] = "(";
46 static char const strBraceClose[] = ")";
47 static char const strColon[] = ": ";
48 static char const strSemiColon[] = "; ";
49 static char const strComma[] = ", ";
50 static char const strLine[] = "line ";
51 static char const strSpace[] = " ";
52 static char const strNum[] = " #";
53 static char const strPercent[] = " %";
54 static char const strEquals[] = " = ";
55
56
57 138 void gate_debug_trace(char const* msg, char const* file, unsigned int line)
58 {
59 138 char const* msgs[16] = GATE_INIT_EMPTY;
60 138 gate_size_t msg_count = 0;
61 138 char lineBuffer[20] = GATE_INIT_EMPTY;
62
63
1/2
✓ Branch 1 taken 138 times.
✗ Branch 2 not taken.
138 if (!gate_str_is_empty(file))
64 {
65 138 msgs[msg_count++] = file;
66
1/2
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
138 if (line)
67 {
68 138 gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line);
69 138 msgs[msg_count++] = strBraceOpen;
70 138 msgs[msg_count++] = lineBuffer;
71 138 msgs[msg_count++] = strBraceClose;
72 }
73 138 msgs[msg_count++] = strColon;
74 }
75 else
76 {
77 ✗ if (line)
78 {
79 ✗ gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line);
80 ✗ msgs[msg_count++] = strLine;
81 ✗ msgs[msg_count++] = lineBuffer;
82 ✗ msgs[msg_count++] = strColon;
83 }
84 }
85
86
1/2
✓ Branch 1 taken 138 times.
✗ Branch 2 not taken.
138 if (!gate_str_is_empty(msg))
87 {
88 138 msgs[msg_count++] = msg;
89 }
90
1/2
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
138 if (msg_count != 0)
91 {
92 138 gate_debug_trace_messages(msgs, msg_count);
93 }
94 138 }
95 792 void gate_debug_trace_value(char const* msg, gate_int64_t value, char const* file, unsigned int line)
96 {
97 792 char const* msgs[16] = GATE_INIT_EMPTY;
98 792 gate_size_t msg_count = 0;
99 792 char lineBuffer[20] = GATE_INIT_EMPTY;
100 792 char valueBuffer[24] = GATE_INIT_EMPTY;
101
102
1/2
✓ Branch 1 taken 792 times.
✗ Branch 2 not taken.
792 if (!gate_str_is_empty(file))
103 {
104 792 msgs[msg_count++] = file;
105
1/2
✓ Branch 0 taken 792 times.
✗ Branch 1 not taken.
792 if (line)
106 {
107 792 gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line);
108 792 msgs[msg_count++] = strBraceOpen;
109 792 msgs[msg_count++] = lineBuffer;
110 792 msgs[msg_count++] = strBraceClose;
111 }
112 792 msgs[msg_count++] = strColon;
113 }
114 else
115 {
116 ✗ if (line)
117 {
118 ✗ gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line);
119 ✗ msgs[msg_count++] = strLine;
120 ✗ msgs[msg_count++] = lineBuffer;
121 ✗ msgs[msg_count++] = strColon;
122 }
123 }
124
125
1/2
✓ Branch 1 taken 792 times.
✗ Branch 2 not taken.
792 if (!gate_str_is_empty(msg))
126 {
127 792 gate_str_print_int64(valueBuffer, sizeof(valueBuffer), value);
128
129 792 msgs[msg_count++] = msg;
130 792 msgs[msg_count++] = strEquals;
131 792 msgs[msg_count++] = valueBuffer;
132 }
133
1/2
✓ Branch 0 taken 792 times.
✗ Branch 1 not taken.
792 if (msg_count != 0)
134 {
135 792 gate_debug_trace_messages(msgs, msg_count);
136 }
137 792 }
138
139 44 void gate_debug_trace_msg_value(char const* msg, char const* value_name, gate_int64_t value, char const* file, unsigned int line)
140 {
141 44 char const* msgs[16] = GATE_INIT_EMPTY;
142 44 gate_size_t msg_count = 0;
143 44 char lineBuffer[20] = GATE_INIT_EMPTY;
144 44 char valueBuffer[24] = GATE_INIT_EMPTY;
145
146
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 if (!gate_str_is_empty(file))
147 {
148 44 msgs[msg_count++] = file;
149
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if (line)
150 {
151 44 gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line);
152 44 msgs[msg_count++] = strBraceOpen;
153 44 msgs[msg_count++] = lineBuffer;
154 44 msgs[msg_count++] = strBraceClose;
155 }
156 44 msgs[msg_count++] = strColon;
157 }
158 else
159 {
160 ✗ if (line)
161 {
162 ✗ gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line);
163 ✗ msgs[msg_count++] = strLine;
164 ✗ msgs[msg_count++] = lineBuffer;
165 ✗ msgs[msg_count++] = strColon;
166 }
167 }
168
169
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 if (!gate_str_is_empty(msg))
170 {
171 44 msgs[msg_count++] = msg;
172 44 msgs[msg_count++] = strSemiColon;
173 }
174
175
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 if (!gate_str_is_empty(value_name))
176 {
177 44 msgs[msg_count++] = value_name;
178 44 msgs[msg_count++] = strEquals;
179 }
180 44 gate_str_print_int64(valueBuffer, sizeof(valueBuffer), value);
181 44 msgs[msg_count++] = valueBuffer;
182
183
184
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if (msg_count != 0)
185 {
186 44 gate_debug_trace_messages(msgs, msg_count);
187 }
188 44 }
189
190 1 void gate_debug_trace_platform_error(char const* msg, char const* file, unsigned int line)
191 {
192 1 char const* msgs[16] = GATE_INIT_EMPTY;
193 1 gate_size_t msg_count = 0;
194 1 char lineBuffer[20] = GATE_INIT_EMPTY;
195 1 char codeBuffer[24] = GATE_INIT_EMPTY;
196 1 gate_int32_t platform_error = gate_platform_get_last_error();
197 1 char errorMsg[1024] = GATE_INIT_EMPTY;
198
199
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (!gate_str_is_empty(file))
200 {
201 1 msgs[msg_count++] = file;
202
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (line)
203 {
204 1 gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line);
205 1 msgs[msg_count++] = strBraceOpen;
206 1 msgs[msg_count++] = lineBuffer;
207 1 msgs[msg_count++] = strBraceClose;
208 }
209 1 msgs[msg_count++] = strColon;
210 }
211 else
212 {
213 ✗ if (line)
214 {
215 ✗ gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line);
216 ✗ msgs[msg_count++] = strLine;
217 ✗ msgs[msg_count++] = lineBuffer;
218 ✗ msgs[msg_count++] = strColon;
219 }
220 }
221
222
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (!gate_str_is_empty(msg))
223 {
224 1 msgs[msg_count++] = msg;
225 1 msgs[msg_count++] = strComma;
226 }
227
228 1 gate_str_print_int64(codeBuffer, sizeof(codeBuffer), (gate_int64_t)platform_error);
229 1 msgs[msg_count++] = strNum;
230 1 msgs[msg_count++] = codeBuffer;
231
232 1 gate_platform_print_error(platform_error, errorMsg, sizeof(errorMsg));
233
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (!gate_str_is_empty(errorMsg))
234 {
235 1 msgs[msg_count++] = strColon;
236 1 msgs[msg_count++] = errorMsg;
237 }
238
239
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (msg_count != 0)
240 {
241 1 gate_debug_trace_messages(msgs, msg_count);
242 }
243 1 }
244 1 void gate_debug_trace_result(gate_result_t result, char const* origin, char const* msg, gate_int32_t error_code,
245 char const* file, unsigned int line)
246 {
247 1 char const* msgs[24] = GATE_INIT_EMPTY;
248 1 gate_size_t msg_count = 0;
249 1 char lineBuffer[20] = GATE_INIT_EMPTY;
250 1 char resultBuffer[24] = GATE_INIT_EMPTY;
251 1 char errorBuffer[24] = GATE_INIT_EMPTY;
252
253
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (!gate_str_is_empty(file))
254 {
255 1 msgs[msg_count++] = file;
256
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (line)
257 {
258 1 gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line);
259 1 msgs[msg_count++] = strBraceOpen;
260 1 msgs[msg_count++] = lineBuffer;
261 1 msgs[msg_count++] = strBraceClose;
262 }
263 1 msgs[msg_count++] = strColon;
264 }
265 else
266 {
267 ✗ if (line)
268 {
269 ✗ gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line);
270 ✗ msgs[msg_count++] = strLine;
271 ✗ msgs[msg_count++] = lineBuffer;
272 ✗ msgs[msg_count++] = strColon;
273 }
274 }
275
276
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (!gate_str_is_empty(origin))
277 {
278 1 msgs[msg_count++] = strSpace;
279 1 msgs[msg_count++] = origin;
280 1 msgs[msg_count++] = strColon;
281 }
282
283
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_FAILED(result))
284 {
285 1 msgs[msg_count++] = strNum;
286 1 gate_str_print_int64(resultBuffer, sizeof(resultBuffer), (gate_int64_t)result);
287 1 msgs[msg_count++] = resultBuffer;
288 }
289
290
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (error_code != 0)
291 {
292 1 msgs[msg_count++] = strPercent;
293 1 gate_str_print_int64(errorBuffer, sizeof(errorBuffer), (gate_int64_t)error_code);
294 1 msgs[msg_count++] = errorBuffer;
295 }
296
297
298
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (!gate_str_is_empty(msg))
299 {
300 1 msgs[msg_count++] = strSpace;
301 1 msgs[msg_count++] = msg;
302 }
303
304
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (msg_count != 0)
305 {
306 1 gate_debug_trace_messages(msgs, msg_count);
307 }
308 1 }
309
310
311
312 #if defined(GATE_SYS_WIN)
313
314 #include "gate/platforms.h"
315
316 void gate_debug_breakpoint()
317 {
318 DebugBreak();
319 }
320
321 #if defined(GATE_SYS_WINCE)
322
323 void gate_debug_trace_messages(char const* const* msgs, gate_size_t msg_count)
324 {
325 if (gate_platform.Win32IsDebuggerPresent && gate_platform.Win32IsDebuggerPresent())
326 {
327 while (msg_count-- != 0)
328 {
329 wchar_t msgbuffer[4096];
330 gate_win32_ansi_2_utf16(*msgs, gate_str_length(*msgs), msgbuffer, sizeof(msgbuffer) / sizeof(msgbuffer[0]));
331
332 OutputDebugString(msgbuffer);
333 ++msgs;
334 }
335 OutputDebugString(_T("\r\n"));
336 }
337 }
338
339 #else /* defined(GATE_SYS_WINCE) */
340
341 #if defined(GATE_SYS_WIN16)
342 # define OutputDebugStringA OutputDebugString
343 #endif
344
345 void gate_debug_trace_messages(char const* const* msgs, gate_size_t msg_count)
346 {
347 #if defined(GATE_SYS_WIN16)
348 while (msg_count-- != 0)
349 {
350 OutputDebugString(*msgs);
351 ++msgs;
352 }
353 OutputDebugString("\r\n");
354 #else
355 HANDLE hErr = (HANDLE)NULL;
356 if (gate_platform.Win32IsDebuggerPresent)
357 {
358 if (gate_platform.Win32IsDebuggerPresent())
359 {
360 while (msg_count-- != 0)
361 {
362 OutputDebugStringA(*msgs);
363 ++msgs;
364 }
365 OutputDebugStringA("\r\n");
366 return;
367 }
368 }
369 hErr = GetStdHandle(STD_ERROR_HANDLE);
370 if ((INVALID_HANDLE_VALUE != hErr) && (NULL != hErr))
371 {
372 for (; msg_count != 0; ++msgs, --msg_count)
373 {
374 char const* msg = *msgs;
375 gate_size_t const msglen = gate_str_length(msg);
376 if (msglen != 0)
377 {
378 gate_win32_writefile(hErr, msg, (DWORD)msglen, NULL, NULL);
379 }
380 }
381 gate_win32_writefile(hErr, "\r\n", 2, NULL, NULL);
382 return;
383 }
384 #endif
385 }
386
387 #endif /* defined(GATE_SYS_WINCE) */
388
389 #endif /* defined(GATE_SYS_WIN) */
390
391
392
393 #if defined(GATE_SYS_POSIX)
394
395 #include <stdio.h>
396 #include <signal.h>
397
398 ✗ void gate_debug_breakpoint()
399 {
400 ✗ raise(SIGINT);
401 ✗ }
402
403 978 void gate_debug_trace_messages(char const* const* msgs, gate_size_t msg_count)
404 {
405
2/2
✓ Branch 0 taken 7635 times.
✓ Branch 1 taken 978 times.
8613 while (msg_count-- != 0)
406 {
407
1/2
✓ Branch 0 taken 7635 times.
✗ Branch 1 not taken.
7635 if (*msgs)
408 {
409 7635 fprintf(stderr, "%s", *msgs);
410 }
411 7635 ++msgs;
412 }
413 978 fprintf(stderr, "\n");
414 978 }
415
416 #endif /* defined(GATE_SYS_POSIX) */
417
418
419
420 #if defined(GATE_SYS_DOS)
421
422 #include <stdio.h>
423 #include <signal.h>
424
425 void gate_debug_breakpoint()
426 {
427 raise(SIGINT);
428 }
429
430 void gate_debug_trace_messages(char const* const* msgs, gate_size_t msg_count)
431 {
432 while (msg_count-- != 0)
433 {
434 if (*msgs)
435 {
436 fprintf(stderr, "%s", *msgs);
437 }
438 ++msgs;
439 }
440 fprintf(stderr, "\n");
441 }
442
443 #endif /* GATE_SYS_DOS */
444
445
446
447 #if defined(GATE_SYS_EFI)
448
449 #include "gate/platform//efi/efi_gate.h"
450
451 void gate_debug_breakpoint()
452 {
453 }
454
455 void gate_debug_trace_messages(char const* const* msgs, gate_size_t msg_count)
456 {
457 gate_char16_t buffer[4096 + 2048 + 1024];
458 static gate_size_t const buffer_len = sizeof(buffer) / sizeof(buffer[0]);
459
460 while (msg_count-- != 0)
461 {
462 if (*msgs)
463 {
464 gate_str_utf8_2_utf16(*msgs, gate_str_length(*msgs), buffer, buffer_len);
465 gate_platform_efi_print_debug_message(buffer);
466 }
467 ++msgs;
468 }
469 gate_platform_efi_print_debug_message(L"\r\n");
470 }
471
472 #endif /* GATE_SYS_EFI */
473
474
475
476 #if defined(GATE_SYS_WASM_WEB)
477
478 #include "gate/platform/wasm/wasm_gate.h"
479 #include "gate/strings.h"
480
481 void gate_debug_breakpoint()
482 {
483 /* TODO */
484 }
485
486 void gate_debug_trace_messages(char const* const* msgs, gate_size_t msg_count)
487 {
488 char buffer[2048];
489 gate_strbuilder_t builder;
490
491 gate_strbuilder_create_static(&builder, buffer, sizeof(buffer) - 1, 0);
492
493 for(; msg_count > 0; ++msgs, --msg_count)
494 {
495 gate_strbuilder_append_cstr(&builder, *msgs);
496 gate_strbuilder_append_cstr(&builder, "\t");
497 }
498 gate_wasm_debug_msg(gate_strbuilder_ptr(&builder, 0));
499 }
500
501 #endif /* GATE_SYS_WASM_WEB */
502
503
504
505
506 #if defined(GATE_SYS_NDS)
507
508 #include <nds.h>
509
510 void gate_debug_breakpoint()
511 {
512 /* TODO */
513 }
514
515 void gate_debug_trace_messages(char const* const* msgs, gate_size_t msg_count)
516 {
517 char buffer[2048];
518 gate_strbuilder_t builder;
519
520 gate_strbuilder_create_static(&builder, buffer, sizeof(buffer) - 1, 0);
521
522 for(; msg_count > 0; ++msgs, --msg_count)
523 {
524 gate_strbuilder_append_cstr(&builder, *msgs);
525 gate_strbuilder_append_cstr(&builder, "\t");
526 }
527 nocashMessage(gate_strbuilder_ptr(&builder, 0));
528 }
529
530 #endif /* GATE_SYS_NDS */
531