configure.ac: Replace --enable-malloc-logging with a new runtime log level.

About why the new log level LOG_LVL_DEBUG_USB has the same value a
LOG_LVL_DEBUG_MALLOC, see the mailing list discussion starting here:
Replacing --enable-verbose-usb-comms in configure.ac
https://sourceforge.net/p/openocd/mailman/message/59215751/

Other minor fixes included here which are probably
not worth submmitting in separate patches:
- In error message "level must be between -3 and 4", increase 4 to 5.
- LOG_DEBUG_IO was passing LOG_LVL_DEBUG instead of LOG_LVL_DEBUG_IO.

Change-Id: I71440bbabe4785338c0a27562cc76fa1b7d54bf5
Signed-off-by: R. Diez <rdiez-2006@rd10.de>
Reviewed-on: https://review.openocd.org/c/openocd/+/9432
Tested-by: jenkins
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
This commit is contained in:
R. Diez
2026-02-02 18:00:49 +01:00
committed by Antonio Borneo
parent aaceff81f0
commit 129e9d3005
4 changed files with 37 additions and 39 deletions

View File

@@ -24,8 +24,8 @@
#include <stdarg.h>
#ifdef _DEBUG_FREE_SPACE_
#include <malloc.h> // For mallinfo/mallinfo2.
#if defined(HAVE_MALLINFO) || defined(HAVE_MALLINFO2)
#include <malloc.h>
#endif
int debug_level = LOG_LVL_INFO;
@@ -101,30 +101,35 @@ static void log_puts(enum log_levels level,
if (LOG_LEVEL_IS(LOG_LVL_DEBUG)) {
/* print with count and time information */
int64_t t = timeval_ms() - start;
#ifdef _DEBUG_FREE_SPACE_
#if defined(HAVE_MALLINFO) || defined(HAVE_MALLINFO2)
const int should_use_mallinfo = LOG_LEVEL_IS(LOG_LVL_DEBUG_MALLOC);
if (should_use_mallinfo) {
#ifdef HAVE_MALLINFO2
struct mallinfo2 info = mallinfo2();
#elif defined(HAVE_MALLINFO)
struct mallinfo info = mallinfo();
struct mallinfo2 info = mallinfo2();
#else
#error "Configuration error: Neither mallinfo() nor mallinfo2() are available."
struct mallinfo info = mallinfo();
#endif
#endif
fprintf(log_output, "%s%u %" PRId64 " %s:%d %s()"
#ifdef _DEBUG_FREE_SPACE_
fprintf(log_output, "%s%u %" PRId64 " %s:%d %s()"
#ifdef HAVE_MALLINFO2
" %zu"
#elif defined(HAVE_MALLINFO)
" %d"
" %zu"
#else
#error "Configuration error: Neither mallinfo() nor mallinfo2() are available."
" %d"
#endif
": %s", log_strings[level + 1], count, t, file, line, function,
info.fordblks,
string);
}
#else
const int should_use_mallinfo = 0;
#endif
": %s", log_strings[level + 1], count, t, file, line, function,
#ifdef _DEBUG_FREE_SPACE_
info.fordblks,
#endif
string);
if (!should_use_mallinfo) {
fprintf(log_output, "%s%u %" PRId64 " %s:%d %s()"
": %s", log_strings[level + 1], count, t, file, line, function,
string);
}
} else {
/* if we are using gdb through pipes then we do not want any output
* to the pipe otherwise we get repeated strings */
@@ -211,7 +216,7 @@ COMMAND_HANDLER(handle_debug_level_command)
int new_level;
COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], new_level);
if (new_level > LOG_LVL_DEBUG_USB || new_level < LOG_LVL_SILENT) {
command_print(CMD, "level must be between %d and %d", LOG_LVL_SILENT, LOG_LVL_DEBUG_IO);
command_print(CMD, "level must be between %d and %d", LOG_LVL_SILENT, LOG_LVL_DEBUG_USB);
return ERROR_COMMAND_ARGUMENT_INVALID;
}
debug_level = new_level;

View File

@@ -40,6 +40,10 @@
* LOG_LVL_DEBUG_USB - verbose USB trace
* In the past this corresponded to build configuration options
--enable-verbose and --enable-verbose-usb-comms.
* LOG_LVL_DEBUG_MALLOC - log messages will include the amount of free heap space
* maintained by malloc in its free list, if mallinfo is available.
* In the past this corresponded to build configuration
* option --enable-malloc-logging.
*/
enum log_levels {
LOG_LVL_SILENT = -3,
@@ -50,7 +54,10 @@ enum log_levels {
LOG_LVL_INFO = 2,
LOG_LVL_DEBUG = 3,
LOG_LVL_DEBUG_IO = 4,
// LOG_LVL_DEBUG_USB and LOG_LVL_DEBUG_MALLOC have the same value at the moment.
// In the future, these logging categories will be individually switchable.
LOG_LVL_DEBUG_USB = 5,
LOG_LVL_DEBUG_MALLOC = 5,
};
void log_printf(enum log_levels level, const char *file, unsigned int line,
@@ -109,7 +116,7 @@ extern int debug_level;
#define LOG_DEBUG_IO(expr ...) \
do { \
if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO)) \
log_printf_lf(LOG_LVL_DEBUG, \
log_printf_lf(LOG_LVL_DEBUG_IO, \
__FILE__, __LINE__, __func__, \
expr); \
} while (0)