diff --git a/configure.ac b/configure.ac index 5bb76fe00..39f5e074e 100644 --- a/configure.ac +++ b/configure.ac @@ -96,8 +96,8 @@ AC_CHECK_FUNCS([strnlen]) AC_CHECK_FUNCS([gettimeofday]) AC_CHECK_FUNCS([usleep]) AC_CHECK_FUNCS([realpath]) -AC_CHECK_FUNCS([mallinfo], [has_at_least_one_mallinfo=yes]) -AC_CHECK_FUNCS([mallinfo2], [has_at_least_one_mallinfo=yes]) +AC_CHECK_FUNCS([mallinfo]) +AC_CHECK_FUNCS([mallinfo2]) # guess-rev.sh only exists in the repository, not in the released archives AC_MSG_CHECKING([whether to build a release]) @@ -264,21 +264,6 @@ AS_IF([test "x$enable_gcov" = "xyes"], [ AC_DEFINE([USE_GCOV], [0], [0 to leave coverage collection disabled.]) ]) -debug_malloc=no -AC_ARG_ENABLE([malloc_logging], - AS_HELP_STRING([--enable-malloc-logging], - [Include free space in logging messages (requires malloc.h).]), - [debug_malloc=$enableval], []) - -AC_MSG_CHECKING([whether to enable malloc free space logging]); -AC_MSG_RESULT([$debug_malloc]) -AS_IF([test "x$debug_malloc" = "xyes"], [ - AS_IF([test "x$has_at_least_one_mallinfo" != "xyes"], [ - AC_MSG_ERROR([Option --enable-malloc-logging needs a libc with mallinfo or mallinfo2.]) - ]) - AC_DEFINE([_DEBUG_FREE_SPACE_],[1], [Include malloc free space in logging]) -]) - m4_define([AC_ARG_ADAPTERS], [ m4_foreach([adapter_driver], [$1], [AC_ARG_ENABLE(ADAPTER_OPT([adapter_driver]), diff --git a/doc/openocd.texi b/doc/openocd.texi index 4c5df9b88..579f39844 100644 --- a/doc/openocd.texi +++ b/doc/openocd.texi @@ -9589,7 +9589,8 @@ level 1 adds warnings; level 2 adds informational messages; level 3 adds debugging messages; level 4 adds verbose low-level debug messages; -and level 5 adds USB communication messages. +and level 5 adds USB communication messages +and amount of free heap space if mallinfo is available. The default is level 2, but that can be overridden on the command line along with the location of that log file (which is normally the server's standard output). diff --git a/src/helper/log.c b/src/helper/log.c index caed8a9da..06d381055 100644 --- a/src/helper/log.c +++ b/src/helper/log.c @@ -24,8 +24,8 @@ #include -#ifdef _DEBUG_FREE_SPACE_ -#include // For mallinfo/mallinfo2. +#if defined(HAVE_MALLINFO) || defined(HAVE_MALLINFO2) +#include #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; diff --git a/src/helper/log.h b/src/helper/log.h index 07c195d28..01917abe2 100644 --- a/src/helper/log.h +++ b/src/helper/log.h @@ -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)