GCC Code Coverage Report


Directory: src/
File: src/support/debug.c
Date: 2026-06-05 08:54:23
Exec Total Coverage
Lines: 146 219 66.7%
Functions: 18 25 72.0%
Branches: 60 112 53.6%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2021 Barcelona Supercomputing Center */
3 /* */
4 /* This file is part of the DLB library. */
5 /* */
6 /* DLB is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU Lesser General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* DLB is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU Lesser General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU Lesser General Public License */
17 /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */
18 /*********************************************************************************/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include "support/debug.h"
25
26 #include "apis/dlb_errors.h"
27 #include "support/options.h"
28 #include "support/types.h"
29 #include "support/mask_utils.h"
30 #include "LB_comm/comm_lend_light.h"
31 #include "LB_comm/shmem.h"
32 #include "LB_comm/shmem_async.h"
33 #include "LB_comm/shmem_barrier.h"
34 #include "LB_comm/shmem_cpuinfo.h"
35 #include "LB_comm/shmem_procinfo.h"
36 #include "LB_comm/shmem_talp.h"
37 #include "LB_core/spd.h"
38
39 #ifdef MPI_LIB
40 #include "mpi/mpi_core.h"
41 #endif
42
43 #include <sys/types.h>
44 #include <sys/syscall.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <limits.h>
49 #include <unistd.h>
50 #include <stdarg.h>
51 #include <time.h>
52
53 #ifdef HAVE_EXECINFO_H
54 #include <execinfo.h>
55 #endif
56
57 verbose_opts_t vb_opts = VB_UNDEF;
58
59 enum { VBFORMAT_LEN = 128 };
60 static verbose_fmt_t vb_fmt;
61 static char fmt_str[VBFORMAT_LEN];
62 static bool quiet = false;
63 static bool silent = false;
64 static bool wall = false;
65 static bool werror = false;
66 static pthread_mutex_t dlb_clean_mutex = PTHREAD_MUTEX_INITIALIZER;
67
68 144 void debug_init(const options_t *options) {
69 144 quiet = options->quiet;
70 144 silent = options->silent;
71 144 wall = options->debug_opts & DBG_WALL;
72 144 werror = options->debug_opts & DBG_WERROR;
73 144 vb_opts = options->verbose;
74 144 vb_fmt = options->verbose_fmt;
75
76 144 int i = 0;
77
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 2 times.
144 if ( vb_fmt & VBF_NODE ) {
78 char hostname[VBFORMAT_LEN/2];
79 142 gethostname(hostname, sizeof(hostname));
80 142 hostname[sizeof(hostname)-1] = '\0';
81 142 i += sprintf( &fmt_str[i], "%s:", hostname);
82 }
83 #ifdef MPI_LIB
84 if ( vb_fmt & VBF_MPINODE ) { i += sprintf( &fmt_str[i], "%d:", _node_id); }
85 if ( vb_fmt & VBF_MPIRANK ) { i += sprintf( &fmt_str[i], "%d:", _mpi_rank); }
86 #endif
87
88 // Remove last separator ':' if fmt_str is not empty
89
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 2 times.
144 if ( i !=0 ) {
90 142 fmt_str[i-1] = '\0';
91 }
92 144 }
93
94 1653 static void vprint(FILE *fp, const char *prefix, const char *fmt, va_list list) {
95 // Write timestamp
96 enum { TIMESTAMP_MAX_SIZE = 32 };
97 char timestamp[TIMESTAMP_MAX_SIZE];
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1653 times.
1653 if (vb_fmt & VBF_TSTAMP) {
99 time_t t = time(NULL);
100 struct tm *tm = localtime(&t);
101 strftime(timestamp, TIMESTAMP_MAX_SIZE, "[%Y-%m-%dT%T] ", tm);
102 } else {
103 1653 timestamp[0] = '\0';
104 }
105
106 // Write spid
107 enum { SPID_MAX_SIZE = 16 };
108 char spid[SPID_MAX_SIZE];
109
4/4
✓ Branch 0 taken 566 times.
✓ Branch 1 taken 1087 times.
✓ Branch 2 taken 283 times.
✓ Branch 3 taken 283 times.
1653 if (vb_fmt & VBF_SPID && thread_spd) {
110 283 snprintf(spid, SPID_MAX_SIZE, ":%d", thread_spd->id);
111 } else {
112 1370 spid[0] = '\0';
113 }
114
115 // Write thread id
116 enum { THREADID_MAX_SIZE = 24 };
117 char threadid[THREADID_MAX_SIZE];
118
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1652 times.
1653 if (vb_fmt & VBF_THREAD) {
119 1 snprintf(threadid, THREADID_MAX_SIZE, ":%ld", syscall(SYS_gettid));
120 } else {
121 1652 threadid[0] = '\0';
122 }
123
124 // Allocate message in an intermediate buffer and print in one function
125 char *msg;
126 1653 vasprintf(&msg, fmt, list);
127 1653 fprintf(fp, "%s%s[%s%s%s]: %s\n", timestamp, prefix, fmt_str, spid, threadid, msg);
128 1653 free(msg);
129 1653 }
130
131 static void __attribute__((__noreturn__)) vfatal(const char *fmt, va_list list) {
132 /* Parse --silent option if fatal() was invoked before init */
133 if (unlikely(vb_opts == VB_UNDEF)) {
134 /* If fatal() was invoked before debug_init, we want to parse the
135 * --silent option but ensuring that parsing the options does not cause
136 * a recursive fatal error */
137 vb_opts = VB_CLEAR;
138 options_parse_entry("--silent", &silent);
139 }
140
141 if (!silent) {
142 vprint(stderr, "DLB PANIC", fmt, list);
143 }
144 dlb_clean();
145 abort();
146 }
147
148 void fatal(const char *fmt, ...) {
149 va_list list;
150 va_start(list, fmt);
151 vfatal(fmt, list);
152 va_end(list);
153 }
154
155 void fatal0(const char *fmt, ...) {
156 #ifdef MPI_LIB
157 if (_mpi_rank <= 0) {
158 #endif
159 va_list list;
160 va_start(list, fmt);
161 vfatal(fmt, list);
162 va_end(list);
163 #ifdef MPI_LIB
164 } else {
165 dlb_clean();
166 abort();
167 }
168 #endif
169 }
170
171 104 static void vwarning(const char *fmt, va_list list) {
172 /* Parse --silent option if warning() was invoked before init */
173
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 64 times.
104 if (unlikely(vb_opts == VB_UNDEF)) {
174 40 options_parse_entry("--silent", &silent);
175 }
176
177
1/2
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
104 if (!silent) {
178 104 vprint(stderr, "DLB WARNING", fmt, list);
179 }
180 104 }
181
182 104 void warning(const char *fmt, ...) {
183 va_list list;
184 104 va_start(list, fmt);
185
1/2
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
104 if (!werror) vwarning(fmt, list);
186 else vfatal(fmt, list);
187 104 va_end(list);
188 104 }
189
190 void warning0(const char *fmt, ...) {
191 #ifdef MPI_LIB
192 if (_mpi_rank <= 0) {
193 #endif
194 va_list list;
195 va_start(list, fmt);
196 if (!werror) vwarning(fmt, list);
197 else vfatal(fmt, list);
198 va_end(list);
199 #ifdef MPI_LIB
200 }
201 #endif
202 }
203
204 108 void debug_warning_impl(const char *file, int line, const char *func, const char *fmt, ...) {
205
206
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 if (!wall) return;
207
208 /* Construct new fmt based on args */
209 enum { MAX_WARNING_LEN = 8096 };
210 const char *warning_fmt = "%s:%d:%s(): %s";
211 int newfmt_len = snprintf(NULL, 0, warning_fmt, file, line, func, fmt);
212 if (newfmt_len < 0) return;
213 newfmt_len = max_int(newfmt_len, MAX_WARNING_LEN - 1) + 1;
214
215 char *newfmt = malloc((size_t)newfmt_len);
216 if (newfmt == NULL) return;
217 snprintf(newfmt, newfmt_len, warning_fmt, file, line, func, fmt);
218
219 va_list list;
220 va_start(list, fmt);
221 if (!werror) vwarning(newfmt, list);
222 else vfatal(newfmt, list);
223 va_end(list);
224
225 free(newfmt);
226 }
227
228 517 static void vinfo(const char *fmt, va_list list) {
229 /* Parse --quiet and --silent options if info() was invoked before init */
230
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 469 times.
517 if (unlikely(vb_opts == VB_UNDEF)) {
231 48 options_parse_entry("--quiet", &quiet);
232 48 options_parse_entry("--silent", &silent);
233 }
234
235
2/4
✓ Branch 0 taken 517 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 517 times.
✗ Branch 3 not taken.
517 if (!quiet && !silent) {
236 517 vprint(stderr, "DLB", fmt, list);
237 }
238 517 }
239
240 245 void info(const char *fmt, ...) {
241 va_list list;
242 245 va_start(list, fmt);
243 245 vinfo(fmt, list);
244 245 va_end(list);
245 245 }
246
247 272 void info0(const char *fmt, ...) {
248 #ifdef MPI_LIB
249 if (_mpi_rank <= 0) {
250 #endif
251 va_list list;
252 272 va_start(list, fmt);
253 272 vinfo(fmt, list);
254 272 va_end(list);
255 #ifdef MPI_LIB
256 }
257 #endif
258 272 }
259
260 /* Ignores --quiet and --silent, used for explicit prints like variable listing */
261 4 void info0_force_print(const char *fmt, ...) {
262 #ifdef MPI_LIB
263 if (_mpi_rank <= 0) {
264 #endif
265 va_list list;
266 4 va_start(list, fmt);
267 4 vprint(stderr, "DLB", fmt, list);
268 4 va_end(list);
269 #ifdef MPI_LIB
270 }
271 #endif
272 4 }
273
274 1390 static void vverbose(verbose_opts_t flag, const char *fmt, va_list list) {
275
276 /* Parse verbose options if verbose() was invoked before init */
277
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 1335 times.
1390 if (unlikely(vb_opts == VB_UNDEF)) {
278 55 options_parse_entry("--verbose", &vb_opts);
279 }
280
281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1390 times.
1390 if (vb_opts & flag & VB_API) { vprint(stderr, "DLB API", fmt, list); }
282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1390 times.
1390 else if (vb_opts & flag & VB_MICROLB) { vprint(stderr, "DLB MICROLB", fmt, list); }
283
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 1251 times.
1390 else if (vb_opts & flag & VB_SHMEM) { vprint(stderr, "DLB SHMEM", fmt, list); }
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1251 times.
1251 else if (vb_opts & flag & VB_MPI_API) { vprint(stderr, "DLB MPI API", fmt, list); }
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1251 times.
1251 else if (vb_opts & flag & VB_MPI_INT) { vprint(stderr, "DLB MPI INT", fmt, list); }
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1251 times.
1251 else if (vb_opts & flag & VB_STATS) { vprint(stderr, "DLB STATS", fmt, list); }
287
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1250 times.
1251 else if (vb_opts & flag & VB_DROM) { vprint(stderr, "DLB DROM", fmt, list); }
288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1250 times.
1250 else if (vb_opts & flag & VB_ASYNC) { vprint(stderr, "DLB ASYNC", fmt, list); }
289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1250 times.
1250 else if (vb_opts & flag & VB_OMPT) { vprint(stderr, "DLB OMPT", fmt, list); }
290
2/2
✓ Branch 0 taken 888 times.
✓ Branch 1 taken 362 times.
1250 else if (vb_opts & flag & VB_AFFINITY){ vprint(stderr, "DLB AFFINITY", fmt, list); }
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 362 times.
362 else if (vb_opts & flag & VB_BARRIER) { vprint(stderr, "DLB BARRIER", fmt, list); }
292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 362 times.
362 else if (vb_opts & flag & VB_TALP) { vprint(stderr, "DLB TALP", fmt, list); }
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 362 times.
362 else if (vb_opts & flag & VB_INSTR) { vprint(stderr, "DLB INSTRUMENT", fmt, list); }
294 1390 }
295
296 #undef verbose
297 1390 void verbose(verbose_opts_t flag, const char *fmt, ...) {
298
299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1390 times.
1390 if (quiet) return;
300
301 va_list list;
302 1390 va_start(list, fmt);
303 1390 vverbose(flag, fmt, list);
304 1390 va_end(list);
305 }
306
307 #undef verbose0
308 void verbose0(verbose_opts_t flag, const char *fmt, ...) {
309 #ifdef MPI_LIB
310 if (_mpi_rank <= 0) {
311 #endif
312 va_list list;
313 va_start(list, fmt);
314 vverbose(flag, fmt, list);
315 va_end(list);
316 #ifdef MPI_LIB
317 }
318 #endif
319 }
320
321 1 void print_backtrace(void) {
322 #ifdef HAVE_EXECINFO_H
323 void* trace_ptrs[100];
324 1 int count = backtrace( trace_ptrs, 100 );
325 1 char** func_names = backtrace_symbols( trace_ptrs, count );
326 1 fprintf( stderr, "+--------------------------------------\n" );
327
328 // Print the stack trace
329 int i;
330
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 for( i = 0; i < count; i++ ) {
331 5 fprintf( stderr, "| %s\n", func_names[i] );
332 }
333
334 // Free the string pointers
335 1 free( func_names );
336 1 fprintf( stderr, "+--------------------------------------\n" );
337 #else
338 fprintf( stderr, "+--------------------------------------\n" );
339 fprintf( stderr, " Backtrace not supported\n") ;
340 fprintf( stderr, "+--------------------------------------\n" );
341 #endif
342 1 }
343
344 static void clean_shmems(pid_t id, const char *shmem_key,
345 int shmem_size_multiplier, int lewi_color) {
346
347 if (shmem_cpuinfo__exists()) {
348 shmem_cpuinfo__finalize(id, shmem_key, lewi_color);
349 }
350 if (shmem_procinfo__exists()) {
351 shmem_procinfo__finalize(id, false, shmem_key, shmem_size_multiplier);
352 }
353 if (shmem_talp__exists()) {
354 shmem_talp__finalize(id);
355 }
356 shmem_async_finalize(id);
357 }
358
359 void dlb_clean(void) {
360 pthread_mutex_lock(&dlb_clean_mutex);
361 {
362 /* First, try to finalize shmems of registered subprocess */
363 const subprocess_descriptor_t** spds = spd_get_spds();
364 const subprocess_descriptor_t** spd = spds;
365 while (*spd) {
366 pid_t id = (*spd)->id;
367 const char *shmem_key = (*spd)->options.shm_key;
368 int shmem_size_multiplier = (*spd)->options.shm_size_multiplier;
369 int lewi_color = (*spd)->options.lewi_color;
370 clean_shmems(id, shmem_key, shmem_size_multiplier, lewi_color);
371 ++spd;
372 }
373 free(spds);
374
375 /* Then, try to finalize current pid */
376 pid_t pid = thread_spd ? thread_spd->id : getpid();
377 const char *shmem_key = thread_spd ? thread_spd->options.shm_key : NULL;
378 int shmem_size_multiplier = thread_spd ? thread_spd->options.shm_size_multiplier : 1;
379 int lewi_color = thread_spd ? thread_spd->options.lewi_color : 0;
380 clean_shmems(pid, shmem_key, shmem_size_multiplier, lewi_color);
381
382 /* Finalize shared memories that do not support subprocesses */
383 shmem_barrier__finalize(shmem_key, shmem_size_multiplier);
384 finalize_comm();
385
386 /* Destroy shared memories if they still exist */
387 const char *shmem_names[] = {"cpuinfo", "procinfo", "talp", "async"};
388 enum { shmem_nelems = sizeof(shmem_names) / sizeof(shmem_names[0]) };
389 int i;
390 for (i=0; i<shmem_nelems; ++i) {
391 if (shmem_exists(shmem_names[i], shmem_key)) {
392 shmem_destroy(shmem_names[i], shmem_key);
393 }
394 }
395 }
396 pthread_mutex_unlock(&dlb_clean_mutex);
397 }
398
399 /* Trigger warning on some errors, tipically common or complex errors during init */
400 26 void warn_error(int error) {
401
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
26 switch(error) {
402 12 case DLB_ERR_NOMEM:
403 12 warning("DLB initialization failed due to insufficient space in shared memory."
404 " This error may be caused by corrupted DLB shared memory. If that's the case,"
405 " try running dlb_shm --delete and then attempt again. Alternatively, if you"
406 " need to register a large number of processes, you can use the"
407 " --shm-size-multiplier flag to increase the default shared memory size."
408 " See dlb -hh for more info.");
409 12 break;
410 11 case DLB_ERR_PERM:
411
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
11 if (thread_spd != NULL) {
412 1 warning("The process with CPU affinity mask %s failed to initialize DLB."
413 " Please, check that each process initializing DLB has a"
414 1 " non-overlapping set of CPUs.", mu_to_str(&thread_spd->process_mask));
415 } else {
416 10 warning("This process has failed to initialize DLB."
417 " Please, check that each process initializing DLB has a"
418 " non-overlapping set of CPUs.");
419 }
420
4/4
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 4 times.
11 if (shmem_procinfo__exists() && thread_spd != NULL) {
421 1 warning("This is the list of current registered processes and their"
422 " affinity mask:");
423 1 shmem_procinfo__print_info(thread_spd->options.shm_key,
424 1 thread_spd->options.shm_size_multiplier);
425 }
426 11 break;
427 3 case DLB_ERR_NOCOMP:
428 3 warning("DLB could not initialize the shared memory due to incompatible"
429 " options among processes, likely ones sharing CPUs and others not."
430 " Please, if you believe this is a bug contact us at " PACKAGE_BUGREPORT);
431 3 break;
432 }
433 26 }
434
435
436 /* Print Buffers */
437
438 enum { INITIAL_BUFFER_SIZE = 1024 };
439
440 42 void printbuffer_init(print_buffer_t *buffer) {
441 42 buffer->addr = malloc(INITIAL_BUFFER_SIZE*sizeof(char));
442 42 buffer->size = INITIAL_BUFFER_SIZE;
443 42 buffer->len = 0;
444 42 buffer->addr[0] = '\0';
445 42 }
446
447 42 void printbuffer_destroy(print_buffer_t *buffer) {
448 42 free(buffer->addr);
449 42 buffer->addr = NULL;
450 42 buffer->size = 0;
451 42 buffer->len = 0;
452 42 }
453
454 1087 static void printbuffer_append_internal(print_buffer_t *buffer, const char *line, bool newline) {
455
456
2/2
✓ Branch 0 taken 335 times.
✓ Branch 1 taken 752 times.
1087 size_t line_len = strlen(line) + (newline ? 1 : 0) + 1; /* + '\0' */
457 1087 size_t buffer_len = buffer->len;
458
459 /* Realloc buffer if needed */
460
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1075 times.
1087 if (buffer_len + line_len > buffer->size) {
461 12 size_t new_size = buffer->size * 2;
462
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
14 while (buffer_len + line_len > new_size) {
463 2 new_size *= 2;
464 }
465
466 12 void *p = realloc(buffer->addr, new_size*sizeof(char));
467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (!p) {
468 fatal("realloc failed");
469 }
470
471 12 buffer->addr = p;
472 12 buffer->size = new_size;
473 }
474
475 /* Append line to buffer */
476
2/2
✓ Branch 0 taken 335 times.
✓ Branch 1 taken 752 times.
1087 int written = snprintf(buffer->addr + buffer_len, buffer->size - buffer_len,
477 "%s%s", line, newline ? "\n" : "");
478
479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1087 times.
1087 if (written < 0) {
480 fatal("snprintf failed");
481 }
482
483 1087 buffer->len = buffer_len + written;
484 1087 }
485
486 335 void printbuffer_append(print_buffer_t *buffer, const char *line) {
487 335 printbuffer_append_internal(buffer, line, true);
488 335 }
489
490 752 void printbuffer_append_no_newline(print_buffer_t *buffer, const char *text) {
491 752 printbuffer_append_internal(buffer, text, false);
492 752 }
493