GCC Code Coverage Report


Directory: src/gate/
File: src/gate/debugging.c
Date: 2025-12-12 23:40:09
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-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 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 98 void gate_debug_trace(char const* msg, char const* file, unsigned int line)
58 {
59 98 char const* msgs[16] = GATE_INIT_EMPTY;
60 98 gate_size_t msg_count = 0;
61 98 char lineBuffer[20] = GATE_INIT_EMPTY;
62
63
1/2
✓ Branch 1 taken 98 times.
✗ Branch 2 not taken.
98 if (!gate_str_is_empty(file))
64 {
65 98 msgs[msg_count++] = file;
66
1/2
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
98 if (line)
67 {
68 98 gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line);
69 98 msgs[msg_count++] = strBraceOpen;
70 98 msgs[msg_count++] = lineBuffer;
71 98 msgs[msg_count++] = strBraceClose;
72 }
73 98 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 98 times.
✗ Branch 2 not taken.
98 if (!gate_str_is_empty(msg))
87 {
88 98 msgs[msg_count++] = msg;
89 }
90
1/2
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
98 if (msg_count != 0)
91 {
92 98 gate_debug_trace_messages(msgs, msg_count);
93 }
94 98 }
95 96 void gate_debug_trace_value(char const* msg, gate_int64_t value, char const* file, unsigned int line)
96 {
97 96 char const* msgs[16] = GATE_INIT_EMPTY;
98 96 gate_size_t msg_count = 0;
99 96 char lineBuffer[20] = GATE_INIT_EMPTY;
100 96 char valueBuffer[24] = GATE_INIT_EMPTY;
101
102
1/2
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
96 if (!gate_str_is_empty(file))
103 {
104 96 msgs[msg_count++] = file;
105
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 if (line)
106 {
107 96 gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line);
108 96 msgs[msg_count++] = strBraceOpen;
109 96 msgs[msg_count++] = lineBuffer;
110 96 msgs[msg_count++] = strBraceClose;
111 }
112 96 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 96 times.
✗ Branch 2 not taken.
96 if (!gate_str_is_empty(msg))
126 {
127 96 gate_str_print_int64(valueBuffer, sizeof(valueBuffer), value);
128
129 96 msgs[msg_count++] = msg;
130 96 msgs[msg_count++] = strEquals;
131 96 msgs[msg_count++] = valueBuffer;
132 }
133
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 if (msg_count != 0)
134 {
135 96 gate_debug_trace_messages(msgs, msg_count);
136 }
137 96 }
138
139 34 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 34 char const* msgs[16] = GATE_INIT_EMPTY;
142 34 gate_size_t msg_count = 0;
143 34 char lineBuffer[20] = GATE_INIT_EMPTY;
144 34 char valueBuffer[24] = GATE_INIT_EMPTY;
145
146
1/2
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
34 if (!gate_str_is_empty(file))
147 {
148 34 msgs[msg_count++] = file;
149
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (line)
150 {
151 34 gate_str_print_int32(lineBuffer, sizeof(lineBuffer), (gate_int32_t)line);
152 34 msgs[msg_count++] = strBraceOpen;
153 34 msgs[msg_count++] = lineBuffer;
154 34 msgs[msg_count++] = strBraceClose;
155 }
156 34 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 34 times.
✗ Branch 2 not taken.
34 if (!gate_str_is_empty(msg))
170 {
171 34 msgs[msg_count++] = msg;
172 34 msgs[msg_count++] = strSemiColon;
173 }
174
175
1/2
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
34 if (!gate_str_is_empty(value_name))
176 {
177 34 msgs[msg_count++] = value_name;
178 34 msgs[msg_count++] = strEquals;
179 }
180 34 gate_str_print_int64(valueBuffer, sizeof(valueBuffer), value);
181 34 msgs[msg_count++] = valueBuffer;
182
183
184
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (msg_count != 0)
185 {
186 34 gate_debug_trace_messages(msgs, msg_count);
187 }
188 34 }
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
394
395 #if defined(GATE_SYS_POSIX)
396
397 #include <stdio.h>
398 #include <signal.h>
399
400 void gate_debug_breakpoint()
401 {
402 raise(SIGINT);
403 }
404
405 232 void gate_debug_trace_messages(char const* const* msgs, gate_size_t msg_count)
406 {
407
2/2
✓ Branch 0 taken 1727 times.
✓ Branch 1 taken 232 times.
1959 while (msg_count-- != 0)
408 {
409
1/2
✓ Branch 0 taken 1727 times.
✗ Branch 1 not taken.
1727 if (*msgs)
410 {
411 1727 fprintf(stderr, "%s", *msgs);
412 }
413 1727 ++msgs;
414 }
415 232 fprintf(stderr, "\n");
416 232 }
417
418 #endif /* defined(GATE_SYS_POSIX) */
419
420
421 #if defined(GATE_SYS_DOS)
422
423 #include <stdio.h>
424 #include <signal.h>
425
426 void gate_debug_breakpoint()
427 {
428 raise(SIGINT);
429 }
430
431 void gate_debug_trace_messages(char const* const* msgs, gate_size_t msg_count)
432 {
433 while (msg_count-- != 0)
434 {
435 if (*msgs)
436 {
437 fprintf(stderr, "%s", *msgs);
438 }
439 ++msgs;
440 }
441 fprintf(stderr, "\n");
442 }
443
444 #endif /* GATE_SYS_DOS */
445
446 #if defined(GATE_SYS_EFI)
447
448 #include "gate/platform//efi/efi_gate.h"
449
450 void gate_debug_breakpoint()
451 {
452 }
453
454 void gate_debug_trace_messages(char const* const* msgs, gate_size_t msg_count)
455 {
456 gate_char16_t buffer[4096 + 2048 + 1024];
457 static gate_size_t const buffer_len = sizeof(buffer) / sizeof(buffer[0]);
458
459 while (msg_count-- != 0)
460 {
461 if (*msgs)
462 {
463 gate_str_utf8_2_utf16(*msgs, gate_str_length(*msgs), buffer, buffer_len);
464 gate_platform_efi_print_debug_message(buffer);
465 }
466 ++msgs;
467 }
468 gate_platform_efi_print_debug_message(L"\r\n");
469 }
470
471 #endif /* GATE_SYS_EFI */
472
473 #if defined(GATE_SYS_WASM)
474
475 void gate_debug_breakpoint()
476 {
477 /* TODO */
478 }
479
480 void gate_debug_trace_messages(char const* const* msgs, gate_size_t msg_count)
481 {
482 /* TODO */
483 }
484
485
486 #endif
487