1 From f409ee343fe6cdc059bb411746f27a515aec66a8 Mon Sep 17 00:00:00 2001
2 From: Jan Tojnar <jtojnar@gmail.com>
3 Date: Sat, 24 Dec 2022 16:46:18 +0100
4 Subject: [PATCH 2/4] libbacktrace: Allow configuring debug dir
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 On platforms that do not use FHS like NixOS or GNU Guix,
10 the build-id directories are not under `/usr/lib/debug`.
12 Let’s add `--with-separate-debug-dir` configure flag so that
13 the path can be changed. The same flag is supported by gdb:
15 https://github.com/bminor/binutils-gdb/blob/095f84c7e3cf85cd68c657c46b80be078f336bc9/gdb/configure.ac#L113-L115
17 Makefile.am | 11 ++++++-----
18 configure.ac | 8 ++++++++
20 3 files changed, 16 insertions(+), 7 deletions(-)
22 diff --git a/Makefile.am b/Makefile.am
23 index 6eab991..da443c1 100644
26 @@ -33,7 +33,8 @@ ACLOCAL_AMFLAGS = -I config
30 -AM_CFLAGS = $(EXTRA_FLAGS) $(WARN_FLAGS) $(PIC_FLAG)
31 +AM_CFLAGS = $(EXTRA_FLAGS) $(WARN_FLAGS) $(PIC_FLAG) \
32 + -DSYSTEM_DEBUG_DIR=\"$(SEPARATE_DEBUG_DIR)\"
34 include_HEADERS = backtrace.h backtrace-supported.h
36 @@ -134,7 +135,7 @@ libbacktrace_noformat_la_DEPENDENCIES = $(libbacktrace_noformat_la_LIBADD)
38 if HAVE_OBJCOPY_DEBUGLINK
40 -TEST_BUILD_ID_DIR=$(abs_builddir)/usr/lib/debug/.build-id/
41 +TEST_DEBUG_DIR=$(abs_builddir)/usr/lib/debug
43 check_LTLIBRARIES += libbacktrace_elf_for_test.la
45 @@ -143,8 +144,8 @@ libbacktrace_elf_for_test_la_LIBADD = $(BACKTRACE_FILE) elf_for_test.lo \
46 $(VIEW_FILE) $(ALLOC_FILE)
49 - SEARCH='^#define SYSTEM_BUILD_ID_DIR.*$$'; \
50 - REPLACE="#define SYSTEM_BUILD_ID_DIR \"$(TEST_BUILD_ID_DIR)\""; \
51 + SEARCH='^#define BUILD_ID_DIR.*$$'; \
52 + REPLACE='\0\n#undef SYSTEM_DEBUG_DIR\n#define SYSTEM_DEBUG_DIR "$(TEST_DEBUG_DIR)"'; \
53 $(SED) "s%$$SEARCH%$$REPLACE%" \
56 @@ -468,7 +469,7 @@ endif HAVE_OBJCOPY_DEBUGLINK
59 ./install-debuginfo-for-buildid.sh \
60 - "$(TEST_BUILD_ID_DIR)" \
61 + "$(TEST_DEBUG_DIR)/.build-id" \
63 $(OBJCOPY) --strip-debug $< $@
65 diff --git a/configure.ac b/configure.ac
66 index 7f122cb..bb590ab 100644
69 @@ -67,6 +67,14 @@ AM_MAINTAINER_MODE
70 AC_ARG_WITH(target-subdir,
71 [ --with-target-subdir=SUBDIR Configuring in a subdirectory for target])
73 +AC_ARG_WITH(separate-debug-dir,
74 +[ --with-separate-debug-dir=DEBUGDIR Look for global separate debug info in this path @<:@LIBDIR/debug@:>@],
75 +[separate_debug_dir=$withval],
76 +[separate_debug_dir=$libdir/debug])
78 +SEPARATE_DEBUG_DIR=$separate_debug_dir
79 +AC_SUBST(SEPARATE_DEBUG_DIR)
81 # We must force CC to /not/ be precious variables; otherwise
82 # the wrong, non-multilib-adjusted value will be used in multilibs.
83 # As a side effect, we have to subst CFLAGS ourselves.
84 diff --git a/elf.c b/elf.c
85 index e82ecc5..8b1189c 100644
88 @@ -856,7 +856,7 @@ elf_readlink (struct backtrace_state *state, const char *filename,
92 -#define SYSTEM_BUILD_ID_DIR "/usr/lib/debug/.build-id/"
93 +#define BUILD_ID_DIR "/.build-id/"
95 /* Open a separate debug info file, using the build ID to find it.
96 Returns an open file descriptor, or -1.
97 @@ -870,7 +870,7 @@ elf_open_debugfile_by_buildid (struct backtrace_state *state,
98 backtrace_error_callback error_callback,
101 - const char * const prefix = SYSTEM_BUILD_ID_DIR;
102 + const char * const prefix = SYSTEM_DEBUG_DIR BUILD_ID_DIR;
103 const size_t prefix_len = strlen (prefix);
104 const char * const suffix = ".debug";
105 const size_t suffix_len = strlen (suffix);