Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom debug dirs #100

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 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)"'; \
Copy link
Author

@jtojnar jtojnar Dec 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we would be able to just use -D flag here but I tried the following

--- a/Makefile.am
+++ b/Makefile.am
@@ -144,12 +144,9 @@ libbacktrace_elf_for_test_la_LIBADD = $(BACKTRACE_FILE) elf_for_test.lo \
 	$(VIEW_FILE) $(ALLOC_FILE)
 
 elf_for_test.c: elf.c
-	SEARCH='^#define BUILD_ID_DIR.*$$'; \
-	REPLACE='\0\n#undef SYSTEM_DEBUG_DIR\n#define SYSTEM_DEBUG_DIR "$(TEST_DEBUG_DIR)"'; \
-	$(SED) "s%$$SEARCH%$$REPLACE%" \
-		$< \
-		> [email protected]
-	mv [email protected] $@
+	cp $< $@
+
+libbacktrace_elf_for_test_la_CFLAGS = -USYSTEM_DEBUG_DIR -DSYSTEM_DEBUG_DIR=\"$(TEST_DEBUG_DIR)\"
 
 endif HAVE_OBJCOPY_DEBUGLINK
 endif HAVE_ELF

without success and autotools are just too confusing to me.

libbacktrace_elf_for_test_la_CFLAGS does not seem to affect the compilation of elf_for_test.lo (it still gets -DSYSTEM_DEBUG_DIR from AM_CFLAGS) and it only appears when linking the objects into libbacktrace_elf_for_test.la:

cp elf.c elf_for_test.c
/nix/store/4xw8n979xpivdc46a9ndcvyhwgif00hz-bash-5.1-p16/bin/bash ./libtool  --tag=CC   --mode=compile gcc -DHAVE_CONFIG_H -I.    -funwind-tables -frandom-seed=elf_for_test.lo -W -Wall -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition -Wmissing-format-attribute -Wcast-qual  -DSYSTEM_DEBUG_DIR=\"/nix/store/dkpyi2cxvjxk74l2pjighshbzncy2hmy-libbacktrace-unstable-2022-12-16/lib/debug\" -g -O2 -c -o elf_for_test.lo elf_for_test.c
libtool: compile:  gcc -DHAVE_CONFIG_H -I. -funwind-tables -frandom-seed=elf_for_test.lo -W -Wall -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition -Wmissing-format-attribute -Wcast-qual -DSYSTEM_DEBUG_DIR=\"/nix/store/dkpyi2cxvjxk74l2pjighshbzncy2hmy-libbacktrace-unstable-2022-12-16/lib/debug\" -g -O2 -c elf_for_test.c  -fPIC -DPIC -o .libs/elf_for_test.o
/nix/store/4xw8n979xpivdc46a9ndcvyhwgif00hz-bash-5.1-p16/bin/bash ./libtool  --tag=CC   --mode=link gcc -USYSTEM_DEBUG_DIR -DSYSTEM_DEBUG_DIR=\"/build/libbacktrace/usr/lib/debug\" -g -O2   -o libbacktrace_elf_for_test.la  libbacktrace_elf_for_test_la-atomic.lo libbacktrace_elf_for_test_la-dwarf.lo libbacktrace_elf_for_test_la-fileline.lo libbacktrace_elf_for_test_la-posix.lo libbacktrace_elf_for_test_la-print.lo libbacktrace_elf_for_test_la-sort.lo libbacktrace_elf_for_test_la-state.lo backtrace.lo simple.lo elf_for_test.lo mmapio.lo mmap.lo 
libtool: link: ar cr .libs/libbacktrace_elf_for_test.a .libs/libbacktrace_elf_for_test_la-atomic.o .libs/libbacktrace_elf_for_test_la-dwarf.o .libs/libbacktrace_elf_for_test_la-fileline.o .libs/libbacktrace_elf_for_test_la-posix.o .libs/libbacktrace_elf_for_test_la-print.o .libs/libbacktrace_elf_for_test_la-sort.o .libs/libbacktrace_elf_for_test_la-state.o .libs/backtrace.o .libs/simple.o .libs/elf_for_test.o .libs/mmapio.o .libs/mmap.o 
libtool: link: ranlib .libs/libbacktrace_elf_for_test.a

$(SED) "s%$$SEARCH%$$REPLACE%" \
$< \
> [email protected]
Expand Down Expand Up @@ -458,13 +459,13 @@ endif HAVE_OBJCOPY_DEBUGLINK

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

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

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
59 changes: 37 additions & 22 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 @@ -865,12 +865,12 @@ elf_readlink (struct backtrace_state *state, const char *filename,
when the build ID is known is in /usr/lib/debug/.build-id. */

static int
elf_open_debugfile_by_buildid (struct backtrace_state *state,
elf_open_debugfile_by_buildid (const char * const prefix,
struct backtrace_state *state,
const char *buildid_data, size_t buildid_size,
backtrace_error_callback error_callback,
void *data)
{
const char * const prefix = SYSTEM_BUILD_ID_DIR;
const size_t prefix_len = strlen (prefix);
const char * const suffix = ".debug";
const size_t suffix_len = strlen (suffix);
Expand Down Expand Up @@ -6947,27 +6947,42 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
if (buildid_data != NULL)
{
int d;
char debug_directories[strlen(SYSTEM_DEBUG_DIR) + 1];
char *debug_dir;

d = elf_open_debugfile_by_buildid (state, buildid_data, buildid_size,
error_callback, data);
if (d >= 0)
{
int ret;
strcpy(debug_directories, SYSTEM_DEBUG_DIR);

elf_release_view (state, &buildid_view, error_callback, data);
if (debuglink_view_valid)
elf_release_view (state, &debuglink_view, error_callback, data);
if (debugaltlink_view_valid)
elf_release_view (state, &debugaltlink_view, error_callback, data);
ret = elf_add (state, "", d, NULL, 0, base_address, opd,
error_callback, data, fileline_fn, found_sym,
found_dwarf, NULL, 0, 1, NULL, 0);
if (ret < 0)
backtrace_close (d, error_callback, data);
else if (descriptor >= 0)
backtrace_close (descriptor, error_callback, data);
return ret;
}
debug_dir = strtok (debug_directories, ":");
while (debug_dir != NULL)
{
char prefix[strlen(debug_dir) + strlen(BUILD_ID_DIR) + 1];
strcpy(prefix, debug_dir);
strcat(prefix, BUILD_ID_DIR);

d = elf_open_debugfile_by_buildid (prefix, state, buildid_data, buildid_size,
error_callback, data);

if (d >= 0)
{
int ret;

elf_release_view (state, &buildid_view, error_callback, data);
if (debuglink_view_valid)
elf_release_view (state, &debuglink_view, error_callback, data);
if (debugaltlink_view_valid)
elf_release_view (state, &debugaltlink_view, error_callback, data);
ret = elf_add (state, "", d, NULL, 0, base_address, opd,
error_callback, data, fileline_fn, found_sym,
found_dwarf, NULL, 0, 1, NULL, 0);
if (ret < 0)
backtrace_close (d, error_callback, data);
else if (descriptor >= 0)
backtrace_close (descriptor, error_callback, data);
return ret;
}

debug_dir = strtok (NULL, ":");
}
}

if (buildid_view_valid)
Expand Down