configure.ac: Detect mallinfo automatically

Previously, configure.ac only checked whether glibc was available,
but other C runtime libraries like Newlib have mallinfo too.

This is a first step to remove configuration option --enable-malloc-logging
and replace it with a debug level configurable at runtime.

Change-Id: If30fc98a84158459e222fddf08043f46d6fa4112
Signed-off-by: R. Diez <rdiez-2006@rd10.de>
Reviewed-on: https://review.openocd.org/c/openocd/+/9394
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
Tested-by: jenkins
This commit is contained in:
R. Diez
2026-01-19 18:12:46 +01:00
committed by Antonio Borneo
parent 62f49b7fe2
commit 294538f0d4
2 changed files with 20 additions and 25 deletions

View File

@@ -56,15 +56,9 @@ AC_CHECK_TYPE([Elf64_Ehdr],
AC_DEFINE([HAVE_ELF64], [1], [Define to 1 if the system has the type 'Elf64_Ehdr'.]),
[], [[#include <elf.h>]])
AC_MSG_CHECKING([for glibc])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <features.h>]], [[int v = __GLIBC__;return 0;]])],
[have_glibc=yes], [have_glibc=no])
AC_MSG_RESULT($have_glibc)
AC_CHECK_HEADERS([fcntl.h])
AC_CHECK_HEADERS([linux/pci.h])
AC_CHECK_HEADERS([linux/spi/spidev.h])
AC_CHECK_HEADERS([malloc.h])
AC_CHECK_HEADERS([netdb.h])
AC_CHECK_HEADERS([poll.h])
AC_CHECK_HEADERS([strings.h])
@@ -102,6 +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])
# guess-rev.sh only exists in the repository, not in the released archives
AC_MSG_CHECKING([whether to build a release])
@@ -276,7 +272,10 @@ AC_ARG_ENABLE([malloc_logging],
AC_MSG_CHECKING([whether to enable malloc free space logging]);
AC_MSG_RESULT([$debug_malloc])
AS_IF([test "x$debug_malloc" = "xyes" -a "x$have_glibc" = "xyes"], [
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])
])

View File

@@ -25,23 +25,7 @@
#include <stdarg.h>
#ifdef _DEBUG_FREE_SPACE_
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#else
#error "malloc.h is required to use --enable-malloc-logging"
#endif
#ifdef __GLIBC__
#if __GLIBC_PREREQ(2, 33)
#define FORDBLKS_FORMAT " %zu"
#else
/* glibc older than 2.33 (2021-02-01) use mallinfo(). Overwrite it */
#define mallinfo2 mallinfo
#define FORDBLKS_FORMAT " %d"
#endif
#else
#error "GNU glibc is required to use --enable-malloc-logging"
#endif
#include <malloc.h> // For mallinfo/mallinfo2.
#endif
int debug_level = LOG_LVL_INFO;
@@ -118,11 +102,23 @@ static void log_puts(enum log_levels level,
/* print with count and time information */
int64_t t = timeval_ms() - start;
#ifdef _DEBUG_FREE_SPACE_
#ifdef HAVE_MALLINFO2
struct mallinfo2 info = mallinfo2();
#elif defined(HAVE_MALLINFO)
struct mallinfo info = mallinfo();
#else
#error "Configuration error: Neither mallinfo() nor mallinfo2() are available."
#endif
#endif
fprintf(log_output, "%s%d %" PRId64 " %s:%d %s()"
#ifdef _DEBUG_FREE_SPACE_
FORDBLKS_FORMAT
#ifdef HAVE_MALLINFO2
" %zu"
#elif defined(HAVE_MALLINFO)
" %d"
#else
#error "Configuration error: Neither mallinfo() nor mallinfo2() are available."
#endif
#endif
": %s", log_strings[level + 1], count, t, file, line, function,
#ifdef _DEBUG_FREE_SPACE_