1From f409ee343fe6cdc059bb411746f27a515aec66a8 Mon Sep 17 00:00:00 2001
2From: Jan Tojnar <jtojnar@gmail.com>
3Date: Sat, 24 Dec 2022 16:46:18 +0100
4Subject: [PATCH 2/4] libbacktrace: Allow configuring debug dir
5MIME-Version: 1.0
6Content-Type: text/plain; charset=UTF-8
7Content-Transfer-Encoding: 8bit
8
9On platforms that do not use FHS like NixOS or GNU Guix,
10the build-id directories are not under `/usr/lib/debug`.
11
12Let’s add `--with-separate-debug-dir` configure flag so that
13the path can be changed. The same flag is supported by gdb:
14
15https://github.com/bminor/binutils-gdb/blob/095f84c7e3cf85cd68c657c46b80be078f336bc9/gdb/configure.ac#L113-L115
16---
17 Makefile.am | 11 ++++++-----
18 configure.ac | 8 ++++++++
19 elf.c | 4 ++--
20 3 files changed, 16 insertions(+), 7 deletions(-)
21
22diff --git a/Makefile.am b/Makefile.am
23index 6eab991..da443c1 100644
24--- a/Makefile.am
25+++ b/Makefile.am
26@@ -33,7 +33,8 @@ ACLOCAL_AMFLAGS = -I config
27
28 AM_CPPFLAGS =
29
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)\"
33
34 include_HEADERS = backtrace.h backtrace-supported.h
35
36@@ -134,7 +135,7 @@ libbacktrace_noformat_la_DEPENDENCIES = $(libbacktrace_noformat_la_LIBADD)
37 if HAVE_ELF
38 if HAVE_OBJCOPY_DEBUGLINK
39
40-TEST_BUILD_ID_DIR=$(abs_builddir)/usr/lib/debug/.build-id/
41+TEST_DEBUG_DIR=$(abs_builddir)/usr/lib/debug
42
43 check_LTLIBRARIES += libbacktrace_elf_for_test.la
44
45@@ -143,8 +144,8 @@ libbacktrace_elf_for_test_la_LIBADD = $(BACKTRACE_FILE) elf_for_test.lo \
46 $(VIEW_FILE) $(ALLOC_FILE)
47
48 elf_for_test.c: elf.c
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%" \
54 $< \
55 > $@.tmp
56@@ -468,7 +469,7 @@ endif HAVE_OBJCOPY_DEBUGLINK
57
58 %_buildid: %
59 ./install-debuginfo-for-buildid.sh \
60- "$(TEST_BUILD_ID_DIR)" \
61+ "$(TEST_DEBUG_DIR)/.build-id" \
62 $<
63 $(OBJCOPY) --strip-debug $< $@
64
65diff --git a/configure.ac b/configure.ac
66index 7f122cb..bb590ab 100644
67--- a/configure.ac
68+++ b/configure.ac
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])
72
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])
77+
78+SEPARATE_DEBUG_DIR=$separate_debug_dir
79+AC_SUBST(SEPARATE_DEBUG_DIR)
80+
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.
84diff --git a/elf.c b/elf.c
85index e82ecc5..8b1189c 100644
86--- a/elf.c
87+++ b/elf.c
88@@ -856,7 +856,7 @@ elf_readlink (struct backtrace_state *state, const char *filename,
89 }
90 }
91
92-#define SYSTEM_BUILD_ID_DIR "/usr/lib/debug/.build-id/"
93+#define BUILD_ID_DIR "/.build-id/"
94
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,
99 void *data)
100 {
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);
106--
1072.38.1
108