[Concept,07/10] backtrace: Add a test

Message ID 20251129080014.758001-8-sjg@u-boot.org
State New
Headers
Series backtrace: Add runtime support for looking at the backtrace |

Commit Message

Simon Glass Nov. 29, 2025, 7:59 a.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Add a simple test for the backtrace library.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 test/lib/Makefile    |  1 +
 test/lib/backtrace.c | 47 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
 create mode 100644 test/lib/backtrace.c
  

Patch

diff --git a/test/lib/Makefile b/test/lib/Makefile
index 1d94d6604d5..8b322df3b45 100644
--- a/test/lib/Makefile
+++ b/test/lib/Makefile
@@ -8,6 +8,7 @@  obj-$(CONFIG_$(PHASE_)UT_COMPRESSION) += compression.o
 ifeq ($(CONFIG_XPL_BUILD),)
 obj-y += abuf.o
 obj-y += alist.o
+obj-$(CONFIG_BACKTRACE) += backtrace.o
 obj-$(CONFIG_EFI_LOADER) += efi_device_path.o
 obj-$(CONFIG_EFI_SECURE_BOOT) += efi_image_region.o
 obj-$(CONFIG_EFI_LOG) += efi_log.o
diff --git a/test/lib/backtrace.c b/test/lib/backtrace.c
new file mode 100644
index 00000000000..d9c36bbd495
--- /dev/null
+++ b/test/lib/backtrace.c
@@ -0,0 +1,47 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Test for backtrace functions
+ *
+ * Copyright 2025 Canonical Ltd
+ * Written by Simon Glass <simon.glass@canonical.com>
+ */
+
+#include <backtrace.h>
+#include <string.h>
+#include <test/lib.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+/* Test backtrace_init() and backtrace_get_syms() */
+static int lib_test_backtrace(struct unit_test_state *uts)
+{
+	char buf[BACKTRACE_BUFSZ];
+	struct backtrace_ctx ctx;
+	bool found_self = false;
+	bool found_ut_run_list = false;
+	uint i;
+
+	ut_assert(backtrace_init(&ctx, 0) > 2);
+	ut_assertok(backtrace_get_syms(&ctx, buf, sizeof(buf)));
+
+	/*
+	 * Check for known functions in the call stack. With libbacktrace
+	 * we can find static functions too, so check for this test function.
+	 */
+	for (i = 0; i < ctx.count; i++) {
+		if (ctx.syms[i]) {
+			if (strstr(ctx.syms[i], "lib_test_backtrace"))
+				found_self = true;
+			if (strstr(ctx.syms[i], "ut_run_list"))
+				found_ut_run_list = true;
+		}
+	}
+
+	ut_assert(found_self);
+	ut_assert(found_ut_run_list);
+
+	backtrace_uninit(&ctx);
+
+	return 0;
+}
+LIB_TEST(lib_test_backtrace, 0);