Added locking functions to libdebug, to avoid printf race conditions

This commit is contained in:
Andreas Eversberg 2021-12-30 01:40:01 +01:00
parent 2ddf33837e
commit f613bfcc37
7 changed files with 48 additions and 10 deletions

View File

@ -106,6 +106,25 @@ int debug_limit_scroll = 0;
static int lock_initialized = 0; static int lock_initialized = 0;
static pthread_mutex_t debug_mutex; static pthread_mutex_t debug_mutex;
void lock_debug(void)
{
int rc;
if (!lock_initialized) {
rc = pthread_mutex_init(&debug_mutex, NULL);
if (rc == 0)
lock_initialized = 1;
}
if (lock_initialized)
pthread_mutex_lock(&debug_mutex);
}
void unlock_debug(void)
{
if (lock_initialized)
pthread_mutex_unlock(&debug_mutex);
}
void get_win_size(int *w, int *h) void get_win_size(int *w, int *h)
{ {
struct winsize win; struct winsize win;
@ -129,7 +148,6 @@ void _printdebug(const char *file, const char __attribute__((unused)) *function,
const char *p; const char *p;
va_list args; va_list args;
int w, h; int w, h;
int rc;
if (debuglevel > level) if (debuglevel > level)
return; return;
@ -137,13 +155,7 @@ void _printdebug(const char *file, const char __attribute__((unused)) *function,
if (!(debug_mask & ((uint64_t)1 << cat))) if (!(debug_mask & ((uint64_t)1 << cat)))
return; return;
if (!lock_initialized) { lock_debug();
rc = pthread_mutex_init(&debug_mutex, NULL);
if (rc == 0)
lock_initialized = 1;
}
if (lock_initialized)
pthread_mutex_lock(&debug_mutex);
buffer[sizeof(buffer) - 1] = '\0'; buffer[sizeof(buffer) - 1] = '\0';
@ -182,8 +194,7 @@ void _printdebug(const char *file, const char __attribute__((unused)) *function,
print_console_text(); print_console_text();
fflush(stdout); fflush(stdout);
if (lock_initialized) unlock_debug();
pthread_mutex_unlock(&debug_mutex);
} }
const char *debug_amplitude(double level) const char *debug_amplitude(double level)

View File

@ -53,6 +53,9 @@
#define DSIP 46 #define DSIP 46
#define DTEL 47 #define DTEL 47
void lock_debug(void);
void unlock_debug(void);
void get_win_size(int *w, int *h); void get_win_size(int *w, int *h);
#define PDEBUG(cat, level, fmt, arg...) _printdebug(__FILE__, __FUNCTION__, __LINE__, cat, level, NULL, fmt, ## arg) #define PDEBUG(cat, level, fmt, arg...) _printdebug(__FILE__, __FUNCTION__, __LINE__, cat, level, NULL, fmt, ## arg)

View File

@ -60,12 +60,14 @@ void display_iq_on(int on)
if (iq_on) { if (iq_on) {
memset(&screen, ' ', sizeof(screen)); memset(&screen, ' ', sizeof(screen));
memset(&screen_history, 0, sizeof(screen_history)); memset(&screen_history, 0, sizeof(screen_history));
lock_debug();
printf("\0337\033[H"); printf("\0337\033[H");
for (j = 0; j < SIZE; j++) { for (j = 0; j < SIZE; j++) {
screen[j][w] = '\0'; screen[j][w] = '\0';
puts(screen[j]); puts(screen[j]);
} }
printf("\0338"); fflush(stdout); printf("\0338"); fflush(stdout);
unlock_debug();
} }
if (on < 0) { if (on < 0) {
@ -114,6 +116,8 @@ void display_iq(float *samples, int length)
if (!iq_on) if (!iq_on)
return; return;
lock_debug();
get_win_size(&width, &h); get_win_size(&width, &h);
if (width > MAX_DISPLAY_WIDTH - 1) if (width > MAX_DISPLAY_WIDTH - 1)
width = MAX_DISPLAY_WIDTH - 1; width = MAX_DISPLAY_WIDTH - 1;
@ -271,6 +275,8 @@ cont:
} }
disp.interval_pos = pos; disp.interval_pos = pos;
unlock_debug();
} }

View File

@ -110,6 +110,8 @@ static void print_measurements(int on)
if (bar_width < 1) if (bar_width < 1)
return; return;
lock_debug();
lines_total = 0; lines_total = 0;
color = -1; color = -1;
printf("\0337\033[H"); printf("\0337\033[H");
@ -252,6 +254,8 @@ static void print_measurements(int on)
printf("\033[0;39m\0338"); fflush(stdout); printf("\033[0;39m\0338"); fflush(stdout);
debug_limit_scroll = lines_total; debug_limit_scroll = lines_total;
unlock_debug();
} }
void display_measurements_on(int on) void display_measurements_on(int on)

View File

@ -101,12 +101,14 @@ void display_spectrum_on(int on)
if (spectrum_on) { if (spectrum_on) {
memset(&screen, ' ', sizeof(screen)); memset(&screen, ' ', sizeof(screen));
memset(&buffer_hold, 0, sizeof(buffer_hold)); memset(&buffer_hold, 0, sizeof(buffer_hold));
lock_debug();
printf("\0337\033[H"); printf("\0337\033[H");
for (j = 0; j < HEIGHT; j++) { for (j = 0; j < HEIGHT; j++) {
screen[j][w] = '\0'; screen[j][w] = '\0';
puts(screen[j]); puts(screen[j]);
} }
printf("\0338"); fflush(stdout); printf("\0338"); fflush(stdout);
unlock_debug();
} }
if (on < 0) { if (on < 0) {
@ -140,6 +142,8 @@ void display_spectrum(float *samples, int length)
if (!spectrum_on) if (!spectrum_on)
return; return;
lock_debug();
get_win_size(&width, &h); get_win_size(&width, &h);
if (width > MAX_DISPLAY_WIDTH - 1) if (width > MAX_DISPLAY_WIDTH - 1)
width = MAX_DISPLAY_WIDTH - 1; width = MAX_DISPLAY_WIDTH - 1;
@ -403,6 +407,8 @@ void display_spectrum(float *samples, int length)
} }
disp.interval_pos = pos; disp.interval_pos = pos;
unlock_debug();
} }

View File

@ -46,6 +46,7 @@ static void print_status(int on)
if (h > lines_total) if (h > lines_total)
h = lines_total; h = lines_total;
lock_debug();
printf("\0337\033[H\033[1;37m"); printf("\0337\033[H\033[1;37m");
for (i = 0; i < h; i++) { for (i = 0; i < h; i++) {
j = 0; j = 0;
@ -59,6 +60,7 @@ static void print_status(int on)
putchar('\n'); putchar('\n');
} }
printf("\0338"); fflush(stdout); printf("\0338"); fflush(stdout);
unlock_debug();
} }
void display_status_on(int on) void display_status_on(int on)

View File

@ -52,6 +52,7 @@ void display_wave_on(int on)
if (wave_on) { if (wave_on) {
memset(&screen, ' ', sizeof(screen)); memset(&screen, ' ', sizeof(screen));
lock_debug();
printf("\0337\033[H"); printf("\0337\033[H");
for (i = 0; i < num_sender; i++) { for (i = 0; i < num_sender; i++) {
for (j = 0; j < HEIGHT; j++) { for (j = 0; j < HEIGHT; j++) {
@ -60,6 +61,7 @@ void display_wave_on(int on)
} }
} }
printf("\0338"); fflush(stdout); printf("\0338"); fflush(stdout);
unlock_debug();
} }
if (on < 0) if (on < 0)
@ -102,6 +104,8 @@ void display_wave(dispwav_t *disp, sample_t *samples, int length, double range)
if (!wave_on) if (!wave_on)
return; return;
lock_debug();
get_win_size(&width, &h); get_win_size(&width, &h);
if (width > MAX_DISPLAY_WIDTH - 1) if (width > MAX_DISPLAY_WIDTH - 1)
width = MAX_DISPLAY_WIDTH - 1; width = MAX_DISPLAY_WIDTH - 1;
@ -241,6 +245,8 @@ void display_wave(dispwav_t *disp, sample_t *samples, int length, double range)
} }
disp->interval_pos = pos; disp->interval_pos = pos;
unlock_debug();
} }