Skip to content

Commit d518d40

Browse files
committed
libbacktrace: Allow configuring debug dir
On platforms that do not use FHS like NixOS or GNU Guix, the build-id directories are not under `/usr/lib/debug`. Let’s add `--with-separate-debug-dir` configure flag so that the path can be changed. The same flag is supported by gdb: https://github.com/bminor/binutils-gdb/blob/095f84c7e3cf85cd68c657c46b80be078f336bc9/gdb/configure.ac#L113-L115
1 parent 28824f2 commit d518d40

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

Makefile.am

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ ACLOCAL_AMFLAGS = -I config
3333

3434
AM_CPPFLAGS =
3535

36-
AM_CFLAGS = $(EXTRA_FLAGS) $(WARN_FLAGS) $(PIC_FLAG)
36+
AM_CFLAGS = $(EXTRA_FLAGS) $(WARN_FLAGS) $(PIC_FLAG) \
37+
-DSYSTEM_DEBUG_DIR=\"$(SEPARATE_DEBUG_DIR)\"
3738

3839
include_HEADERS = backtrace.h backtrace-supported.h
3940

@@ -132,7 +133,7 @@ libbacktrace_noformat_la_DEPENDENCIES = $(libbacktrace_noformat_la_LIBADD)
132133
if HAVE_ELF
133134
if HAVE_OBJCOPY_DEBUGLINK
134135

135-
TEST_BUILD_ID_DIR=$(abs_builddir)/usr/lib/debug/.build-id/
136+
TEST_DEBUG_DIR=$(abs_builddir)/usr/lib/debug
136137

137138
check_LTLIBRARIES += libbacktrace_elf_for_test.la
138139

@@ -141,8 +142,8 @@ libbacktrace_elf_for_test_la_LIBADD = $(BACKTRACE_FILE) elf_for_test.lo \
141142
$(VIEW_FILE) $(ALLOC_FILE)
142143

143144
elf_for_test.c: elf.c
144-
SEARCH='^#define SYSTEM_BUILD_ID_DIR.*$$'; \
145-
REPLACE="#define SYSTEM_BUILD_ID_DIR \"$(TEST_BUILD_ID_DIR)\""; \
145+
SEARCH='^#define BUILD_ID_DIR.*$$'; \
146+
REPLACE='\0\n#undef SYSTEM_DEBUG_DIR\n#define SYSTEM_DEBUG_DIR "$(TEST_DEBUG_DIR)"'; \
146147
$(SED) "s%$$SEARCH%$$REPLACE%" \
147148
$< \
148149
> $@.tmp
@@ -458,13 +459,13 @@ endif HAVE_OBJCOPY_DEBUGLINK
458459

459460
%_buildid: %
460461
./install-debuginfo-for-buildid.sh \
461-
"$(TEST_BUILD_ID_DIR)" \
462+
"$(TEST_DEBUG_DIR)/.build-id" \
462463
$<
463464
$(OBJCOPY) --strip-debug $< $@
464465

465466
%_buildidfull: %
466467
./install-debuginfo-for-buildid.sh \
467-
"$(TEST_BUILD_ID_DIR)" \
468+
"$(TEST_DEBUG_DIR)/.build-id" \
468469
$<
469470
$(OBJCOPY) --strip-all $< $@
470471

configure.ac

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ AM_MAINTAINER_MODE
6767
AC_ARG_WITH(target-subdir,
6868
[ --with-target-subdir=SUBDIR Configuring in a subdirectory for target])
6969

70+
AC_ARG_WITH(separate-debug-dir,
71+
[ --with-separate-debug-dir=DEBUGDIR Look for global separate debug info in this path @<:@LIBDIR/debug@:>@],
72+
[separate_debug_dir=$withval],
73+
[separate_debug_dir=$libdir/debug])
74+
75+
SEPARATE_DEBUG_DIR=$separate_debug_dir
76+
AC_SUBST(SEPARATE_DEBUG_DIR)
77+
7078
# We must force CC to /not/ be precious variables; otherwise
7179
# the wrong, non-multilib-adjusted value will be used in multilibs.
7280
# As a side effect, we have to subst CFLAGS ourselves.

elf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ elf_readlink (struct backtrace_state *state, const char *filename,
856856
}
857857
}
858858

859-
#define SYSTEM_BUILD_ID_DIR "/usr/lib/debug/.build-id/"
859+
#define BUILD_ID_DIR "/.build-id/"
860860

861861
/* Open a separate debug info file, using the build ID to find it.
862862
Returns an open file descriptor, or -1.
@@ -870,7 +870,7 @@ elf_open_debugfile_by_buildid (struct backtrace_state *state,
870870
backtrace_error_callback error_callback,
871871
void *data)
872872
{
873-
const char * const prefix = SYSTEM_BUILD_ID_DIR;
873+
const char * const prefix = SYSTEM_DEBUG_DIR BUILD_ID_DIR;
874874
const size_t prefix_len = strlen (prefix);
875875
const char * const suffix = ".debug";
876876
const size_t suffix_len = strlen (suffix);

0 commit comments

Comments
 (0)