Skip to content

Commit

Permalink
libbacktrace: Allow configuring debug dir
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jtojnar committed Mar 2, 2024
1 parent 28824f2 commit f12d1e2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
11 changes: 6 additions & 5 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ ACLOCAL_AMFLAGS = -I config

AM_CPPFLAGS =

AM_CFLAGS = $(EXTRA_FLAGS) $(WARN_FLAGS) $(PIC_FLAG)
AM_CFLAGS = $(EXTRA_FLAGS) $(WARN_FLAGS) $(PIC_FLAG) \
-DSYSTEM_DEBUG_DIR=\"$(SEPARATE_DEBUG_DIR)\"

include_HEADERS = backtrace.h backtrace-supported.h

Expand Down Expand Up @@ -132,7 +133,7 @@ libbacktrace_noformat_la_DEPENDENCIES = $(libbacktrace_noformat_la_LIBADD)
if HAVE_ELF
if HAVE_OBJCOPY_DEBUGLINK

TEST_BUILD_ID_DIR=$(abs_builddir)/usr/lib/debug/.build-id/
TEST_DEBUG_DIR=$(abs_builddir)/usr/lib/debug

check_LTLIBRARIES += libbacktrace_elf_for_test.la

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

elf_for_test.c: elf.c
SEARCH='^#define SYSTEM_BUILD_ID_DIR.*$$'; \
REPLACE="#define SYSTEM_BUILD_ID_DIR \"$(TEST_BUILD_ID_DIR)\""; \
SEARCH='^#define BUILD_ID_DIR.*$$'; \
REPLACE='\0\n#undef SYSTEM_DEBUG_DIR\n#define SYSTEM_DEBUG_DIR "$(TEST_DEBUG_DIR)"'; \
$(SED) "s%$$SEARCH%$$REPLACE%" \
$< \
> $@.tmp
Expand Down Expand Up @@ -458,7 +459,7 @@ endif HAVE_OBJCOPY_DEBUGLINK

%_buildid: %
./install-debuginfo-for-buildid.sh \
"$(TEST_BUILD_ID_DIR)" \
"$(TEST_DEBUG_DIR)/.build-id" \
$<
$(OBJCOPY) --strip-debug $< $@

Expand Down
8 changes: 8 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ AM_MAINTAINER_MODE
AC_ARG_WITH(target-subdir,
[ --with-target-subdir=SUBDIR Configuring in a subdirectory for target])

AC_ARG_WITH(separate-debug-dir,
[ --with-separate-debug-dir=DEBUGDIR Look for global separate debug info in this path @<:@LIBDIR/debug@:>@],
[separate_debug_dir=$withval],
[separate_debug_dir=$libdir/debug])

SEPARATE_DEBUG_DIR=$separate_debug_dir
AC_SUBST(SEPARATE_DEBUG_DIR)

# We must force CC to /not/ be precious variables; otherwise
# the wrong, non-multilib-adjusted value will be used in multilibs.
# As a side effect, we have to subst CFLAGS ourselves.
Expand Down
4 changes: 2 additions & 2 deletions elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ elf_readlink (struct backtrace_state *state, const char *filename,
}
}

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

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

0 comments on commit f12d1e2

Please sign in to comment.