[Concept,07/10] backtrace: Add a test
Commit Message
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
@@ -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
new file mode 100644
@@ -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);