From 5c5e2eae1ea3a3e3854c7be2a17e498357bca9d7 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 12 May 2024 12:29:38 +0200 Subject: [PATCH] execinfo tests: Strengthen tests. * tests/test-execinfo.c (test_backtrace): Add an argument. Check the return value of backtrace(). Check that backtrace_symbols_fd is defined. Check the return value of backtrace_symbols(). (main): Test also the case of a short buffer. * modules/execinfo-tests (Makefile.am): Verify that LIB_EXECINFO is defined. --- ChangeLog | 10 ++++++++++ modules/execinfo-tests | 2 +- tests/test-execinfo.c | 24 +++++++++++++++++++----- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3e5df0d22d..34d53eb4e1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2024-05-12 Bruno Haible + + execinfo tests: Strengthen tests. + * tests/test-execinfo.c (test_backtrace): Add an argument. Check the + return value of backtrace(). Check that backtrace_symbols_fd is defined. + Check the return value of backtrace_symbols(). + (main): Test also the case of a short buffer. + * modules/execinfo-tests (Makefile.am): Verify that LIB_EXECINFO is + defined. + 2024-05-11 Paul Eggert stdbit: don’t assume -DHAVE_CONFIG_H diff --git a/modules/execinfo-tests b/modules/execinfo-tests index 191345cd5c..5d6a28c69b 100644 --- a/modules/execinfo-tests +++ b/modules/execinfo-tests @@ -9,4 +9,4 @@ configure.ac: Makefile.am: TESTS += test-execinfo check_PROGRAMS += test-execinfo -test_execinfo_LDADD = $(LDADD) $(LIB_EXECINFO) +test_execinfo_LDADD = $(LDADD) @LIB_EXECINFO@ diff --git a/tests/test-execinfo.c b/tests/test-execinfo.c index af457c468f..5745306ed5 100644 --- a/tests/test-execinfo.c +++ b/tests/test-execinfo.c @@ -27,17 +27,28 @@ #include "macros.h" static void -test_backtrace (void) +test_backtrace (int pass) { void *buffer[10]; - char **symbols; + int max_size; int size; + char **symbols; + + max_size = (pass == 0 ? SIZEOF (buffer) : 1); + size = backtrace (buffer, max_size); + ASSERT (size >= 0 && size <= max_size); + + /* Print the backtrace to a file descriptor. */ + backtrace_symbols_fd (buffer, size, 1); + printf ("\n"); - size = backtrace (buffer, SIZEOF (buffer)); symbols = backtrace_symbols (buffer, size); + if (size > 0) + /* We have enough memory available. */ + ASSERT (symbols != NULL); /* Print the backtrace if possible. */ - if (0 < size && symbols != NULL) + if (symbols != NULL) { for (int i = 0; i < size; ++i) printf ("%s\n", symbols[i]); @@ -48,7 +59,10 @@ test_backtrace (void) int main (void) { - test_backtrace (); + printf ("Full stack trace:\n"); fflush (stdout); + test_backtrace (0); + printf ("\nTruncated stack trace:\n"); fflush (stdout); + test_backtrace (1); return 0; } -- 2.39.5