Skip to content

Commit

Permalink
Update our vendored libbacktrace
Browse files Browse the repository at this point in the history
  Updated to commit 7e2b7da3d6568d2e4e78658f22e701746a48d7e1

Signed-off-by: Pablo Galindo <[email protected]>
  • Loading branch information
pablogsal committed Jul 25, 2024
1 parent c0efc31 commit e458592
Show file tree
Hide file tree
Showing 54 changed files with 848 additions and 8,457 deletions.
4 changes: 3 additions & 1 deletion src/memray/_memray/native_resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,15 @@ SymbolResolver::getBacktraceState(InternedString interned_filename, uintptr_t ad
int foundSym = 0;
#ifdef __linux__
int foundDwarf = 0;
struct libbacktrace_base_address base_address;
base_address.m = address_start;
auto ret =
elf_add(state,
data.fileName,
descriptor,
nullptr,
0,
address_start,
base_address,
nullptr,
errorHandler,
&data,
Expand Down
46 changes: 40 additions & 6 deletions src/vendor/libbacktrace/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Makefile.am -- Backtrace Makefile.
# Copyright (C) 2012-2021 Free Software Foundation, Inc.
# Copyright (C) 2012-2024 Free Software Foundation, Inc.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
Expand Down Expand Up @@ -157,18 +157,18 @@ endif HAVE_OBJCOPY_DEBUGLINK
endif HAVE_ELF

elf_%.c: elf.c
nlinit=`echo 'nl="'; echo '"'`; eval "$$nlinit"; \
SEARCH='#error "Unknown BACKTRACE_ELF_SIZE"'; \
REPLACE='#undef BACKTRACE_ELF_SIZE\
#define BACKTRACE_ELF_SIZE'; \
REPLACE="#undef BACKTRACE_ELF_SIZE\\$${nl}#define BACKTRACE_ELF_SIZE"; \
$(SED) "s/^$$SEARCH\$$/$$REPLACE $*/" \
$< \
> $@.tmp
mv $@.tmp $@

xcoff_%.c: xcoff.c
nlinit=`echo 'nl="'; echo '"'`; eval "$$nlinit"; \
SEARCH='#error "Unknown BACKTRACE_XCOFF_SIZE"'; \
REPLACE='#undef BACKTRACE_XCOFF_SIZE\
#define BACKTRACE_XCOFF_SIZE'; \
REPLACE="#undef BACKTRACE_XCOFF_SIZE\\$${nl}#define BACKTRACE_XCOFF_SIZE"; \
$(SED) "s/^$$SEARCH\$$/$$REPLACE $*/" \
$< \
> $@.tmp
Expand Down Expand Up @@ -596,6 +596,39 @@ MAKETESTS += mtest_minidebug
$(OBJCOPY) --add-section .gnu_debugdata=$<.mdbg.xz $<.strip
mv $<.strip $@

if HAVE_ELF
if HAVE_BUILDID
if HAVE_OBJCOPY_DEBUGLINK

m2test_SOURCES = $(mtest_SOURCES)
m2test_CFLAGS = $(libbacktrace_TEST_CFLAGS) -O
m2test_LDFLAGS = -Wl,--build-id $(libbacktrace_testing_ldflags)
m2test_LDADD = libbacktrace_elf_for_test.la

check_PROGRAMS += m2test
MAKETESTS += m2test_minidebug2

# minidebug2 is like minidebug but also adds the gnu_debugdata section
# to the debug file, and uses a build ID file. There is no reason to do
# this but it was causing an infinite recursion.
%_minidebug2: %
$(NM) -D $< -P --defined-only | $(AWK) '{ print $$1 }' | sort > $<.dsyms2
$(NM) $< -P --defined-only | $(AWK) '{ if ($$2 == "T" || $$2 == "t" || $$2 == "D") print $$1 }' | sort > $<.fsyms2
$(COMM) -13 $<.dsyms2 $<.fsyms2 > $<.keepsyms2
$(OBJCOPY) --only-keep-debug $< $<.dbg2
$(OBJCOPY) -S --remove-section .gdb_index --remove-section .comment --keep-symbols=$<.keepsyms2 $<.dbg2 $<.mdbg2
$(OBJCOPY) --strip-all --remove-section ..comment $< $<.strip2
rm -f $<.mdbg2.xz
$(XZ) $<.mdbg2
$(OBJCOPY) --add-section .gnu_debugdata=$<.mdbg2.xz $<.dbg2
$(OBJCOPY) --add-section .gnu_debugdata=$<.mdbg2.xz $<.strip2
$(SHELL) ./install-debuginfo-for-buildid.sh $(TEST_BUILD_ID_DIR) $<.dbg2
mv $<.strip2 $@

endif HAVE_OBJCOPY_DEBUGLINK
endif HAVE_BUILDID
endif HAVE_ELF

endif HAVE_MINIDEBUG

endif NATIVE
Expand Down Expand Up @@ -631,7 +664,8 @@ TESTS += $(MAKETESTS) $(BUILDTESTS)
CLEANFILES = \
$(MAKETESTS) $(BUILDTESTS) *.debug elf_for_test.c edtest2_build.c \
gen_edtest2_build \
*.dsyms *.fsyms *.keepsyms *.dbg *.mdbg *.mdbg.xz *.strip
*.dsyms *.fsyms *.keepsyms *.dbg *.mdbg *.mdbg.xz *.strip \
*.dsyms2 *.fsyms2 *.keepsyms2 *.dbg2 *.mdbg2 *.mdbg2.xz *.strip2

clean-local:
-rm -rf usr
Expand Down
147 changes: 102 additions & 45 deletions src/vendor/libbacktrace/Makefile.in

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion src/vendor/libbacktrace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ The libbacktrace library may be linked into a program or library and
used to produce symbolic backtraces.
Sample uses would be to print a detailed backtrace when an error
occurs or to gather detailed profiling information.

In general the functions provided by this library are async-signal-safe,
meaning that they may be safely called from a signal handler.
That said, on systems that use `dl_iterate_phdr`, such as GNU/Linux,
gitthe first call to a libbacktrace function will call `dl_iterate_phdr`,
which is not in general async-signal-safe. Therefore, programs
that call libbacktrace from a signal handler should ensure that they
make an initial call from outside of a signal handler.
Similar considerations apply when arranging to call libbacktrace
from within malloc; `dl_iterate_phdr` can also call malloc,
so make an initial call to a libbacktrace function outside of
malloc before trying to call libbacktrace functions within malloc.

The libbacktrace library is provided under a BSD license.
See the source files for the exact license text.
Expand All @@ -25,7 +35,7 @@ will work.
See the source file backtrace-supported.h.in for the macros that it
defines.

As of October 2020, libbacktrace supports ELF, PE/COFF, Mach-O, and
As of July 2024, libbacktrace supports ELF, PE/COFF, Mach-O, and
XCOFF executables with DWARF debugging information.
In other words, it supports GNU/Linux, *BSD, macOS, Windows, and AIX.
The library is written to make it straightforward to add support for
Expand Down
2 changes: 1 addition & 1 deletion src/vendor/libbacktrace/alloc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* alloc.c -- Memory allocation without mmap.
Copyright (C) 2012-2021 Free Software Foundation, Inc.
Copyright (C) 2012-2024 Free Software Foundation, Inc.
Written by Ian Lance Taylor, Google.
Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion src/vendor/libbacktrace/allocfail.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* allocfail.c -- Test for libbacktrace library
Copyright (C) 2018-2021 Free Software Foundation, Inc.
Copyright (C) 2018-2024 Free Software Foundation, Inc.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
Expand Down
2 changes: 1 addition & 1 deletion src/vendor/libbacktrace/allocfail.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh

# allocfail.sh -- Test for libbacktrace library.
# Copyright (C) 2018-2021 Free Software Foundation, Inc.
# Copyright (C) 2018-2024 Free Software Foundation, Inc.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
Expand Down
2 changes: 1 addition & 1 deletion src/vendor/libbacktrace/atomic.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* atomic.c -- Support for atomic functions if not present.
Copyright (C) 2013-2021 Free Software Foundation, Inc.
Copyright (C) 2013-2024 Free Software Foundation, Inc.
Written by Ian Lance Taylor, Google.
Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion src/vendor/libbacktrace/backtrace-supported.h.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* backtrace-supported.h.in -- Whether stack backtrace is supported.
Copyright (C) 2012-2021 Free Software Foundation, Inc.
Copyright (C) 2012-2024 Free Software Foundation, Inc.
Written by Ian Lance Taylor, Google.

Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion src/vendor/libbacktrace/backtrace.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* backtrace.c -- Entry point for stack backtrace library.
Copyright (C) 2012-2021 Free Software Foundation, Inc.
Copyright (C) 2012-2024 Free Software Foundation, Inc.
Written by Ian Lance Taylor, Google.
Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion src/vendor/libbacktrace/backtrace.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* backtrace.h -- Public header file for stack backtrace library.
Copyright (C) 2012-2021 Free Software Foundation, Inc.
Copyright (C) 2012-2024 Free Software Foundation, Inc.
Written by Ian Lance Taylor, Google.
Redistribution and use in source and binary forms, with or without
Expand Down
32 changes: 24 additions & 8 deletions src/vendor/libbacktrace/btest.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* btest.c -- Test for libbacktrace library
Copyright (C) 2012-2021 Free Software Foundation, Inc.
Copyright (C) 2012-2024 Free Software Foundation, Inc.
Written by Ian Lance Taylor, Google.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE. */
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>

#include "filenames.h"

Expand All @@ -48,7 +49,7 @@ POSSIBILITY OF SUCH DAMAGE. */

/* Test the backtrace function with non-inlined functions. */

static int test1 (void) __attribute__ ((noinline, noclone, unused));
static int test1 (void) __attribute__ ((noinline, noclone, optnone, unused));
static int f2 (int) __attribute__ ((noinline, noclone));
static int f3 (int, int) __attribute__ ((noinline, noclone));

Expand Down Expand Up @@ -162,7 +163,7 @@ f13 (int f1line, int f2line)

/* Test the backtrace_simple function with non-inlined functions. */

static int test3 (void) __attribute__ ((noinline, noclone, unused));
static int test3 (void) __attribute__ ((noinline, noclone, optnone, unused));
static int f22 (int) __attribute__ ((noinline, noclone));
static int f23 (int, int) __attribute__ ((noinline, noclone));

Expand Down Expand Up @@ -439,7 +440,7 @@ test5 (void)
(unsigned long) (uintptr_t) &global);
symdata.failed = 1;
}
else if (symdata.size != sizeof (global))
else if (symdata.size != sizeof (global) && symdata.size != 0)
{
fprintf (stderr,
"test5: unexpected syminfo size got %lx expected %lx\n",
Expand All @@ -458,16 +459,29 @@ test5 (void)
return failures;
}

#define MIN_DESCRIPTOR 3
#define MAX_DESCRIPTOR 10

static int fstat_status[MAX_DESCRIPTOR];

/* Check files that are available. */

static void
check_available_files (void)
{
struct stat s;
for (unsigned i = MIN_DESCRIPTOR; i < MAX_DESCRIPTOR; i++)
fstat_status[i] = fstat (i, &s);
}

/* Check that are no files left open. */

static void
check_open_files (void)
{
int i;

for (i = 3; i < 10; i++)
for (unsigned i = MIN_DESCRIPTOR; i < MAX_DESCRIPTOR; i++)
{
if (close (i) == 0)
if (fstat_status[i] != 0 && close (i) == 0)
{
fprintf (stderr,
"ERROR: descriptor %d still open after tests complete\n",
Expand All @@ -482,6 +496,8 @@ check_open_files (void)
int
main (int argc ATTRIBUTE_UNUSED, char **argv)
{
check_available_files ();

state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
error_callback_create, NULL);

Expand Down
Loading

0 comments on commit e458592

Please sign in to comment.