Skip to content

Commit

Permalink
Test for libunwind in ./configure
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Mar 7, 2023
1 parent f1cd451 commit 73f8985
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 20 deletions.
4 changes: 2 additions & 2 deletions blink/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#include <sched.h>
#endif

#ifdef UNWIND
#if defined(HAVE_LIBUNWIND) && !defined(MUSL_CROSS_MAKE)
#define UNW_LOCAL_ONLY
#include <libunwind.h>
#endif
Expand All @@ -63,7 +63,7 @@
_Thread_local static jmp_buf g_busted;

const char *GetBlinkBacktrace(void) {
#ifdef UNWIND
#if defined(HAVE_LIBUNWIND) && !defined(MUSL_CROSS_MAKE)
_Thread_local static char b[2048];
int o = 0;
char sym[256];
Expand Down
9 changes: 3 additions & 6 deletions build/config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ ifeq ($(HOST_SYSTEM), FreeBSD)
LDFLAGS += -Wl,--image-base=$(IMAGE_BASE_VIRTUAL)
endif

# Tune the build when using our prebuilt musl-cross-make toolchains.
CPPFLAGS_STATIC = \
-DMUSL_CROSS_MAKE
LDFLAGS_STATIC = \
-static \
-fno-exceptions \
Expand Down Expand Up @@ -60,12 +63,6 @@ endif
ifeq ($(MODE), dbg)
CFLAGS += -O0 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer
CPPFLAGS += -DDEBUG
ifneq ($(HOST_SYSTEM), Darwin)
ifneq ($(HOST_SYSTEM), FreeBSD)
CPPFLAGS += -DUNWIND
LDLIBS += -lunwind -llzma
endif
endif
endif

ifeq ($(MODE), rel)
Expand Down
1 change: 1 addition & 0 deletions config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
// #define HAVE_KERN_ARND
// #define HAVE_GETRANDOM
// #define HAVE_SETGROUPS
// #define HAVE_LIBUNWIND
// #define HAVE_GETENTROPY
// #define HAVE_MAP_SHARED
// #define HAVE_SENDTO_ZERO
Expand Down
38 changes: 27 additions & 11 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ compile() {
${TARGET_ARCH} \
"$@" \
${LOADLIBES} \
${LDLIBS} \
${EXTRA_LDLIBS}
${EXTRA_LDLIBS} \
${LDLIBS}
}

config() {
Expand Down Expand Up @@ -476,13 +476,7 @@ fi
# check for the math library
EXTRA_LDLIBS=-lm
if config lm "checking for -lm... "; then
LDLIBS="${LDLIBS} -lm"
fi

# check for the posix.1b realtime extensions library
EXTRA_LDLIBS=-lrt
if config lrt "checking for -lrt... "; then
LDLIBS="${LDLIBS} -lrt"
LDLIBS="-lm ${LDLIBS}"
fi

# check for the posix threads library
Expand All @@ -495,17 +489,24 @@ if [ -z "${DISABLE_THREADS}" ]; then
else
EXTRA_LDLIBS=-lpthread
if config pthread "checking for -lpthread... "; then
LDLIBS="${LDLIBS} -lpthread"
LDLIBS="-lpthread ${LDLIBS}"
else
if config pthread "checking for pthreads... "; then
LDLIBS="${LDLIBS} -lpthread"
LDLIBS="-lpthread ${LDLIBS}"
else
uncomment "#define DISABLE_THREADS"
fi
fi
fi
fi

# check for the posix.1b realtime extensions library
# note: librt depends on pthreads, on rhel5 at least
EXTRA_LDLIBS=-lrt
if config lrt "checking for -lrt... "; then
LDLIBS="-lrt ${LDLIBS}"
fi

# should be the default and macos build fails without it
EXTRA_CFLAGS=-fno-common
if config noop "checking for -fno-common... "; then
Expand Down Expand Up @@ -566,6 +567,21 @@ if [ -z "${MODE}" ] && [ -n "${JIT_POSSIBLE}" ] && [ -z "${DISABLE_JIT}" ]; then
fi
fi

# try to get blink backtraces if possible
if [ "${MODE}" != "tiny" ] && [ "${MODE}" != "rel" ]; then
UNWIND_CFLAGS="$(pkg-config --cflags libunwind 2>/dev/null)"
if ! UNWIND_LDLIBS="$(pkg-config --libs libunwind 2>/dev/null)"; then
UNWIND_LDLIBS="-lunwind -llzma"
fi
EXTRA_CFLAGS="${UNWIND_CFLAGS}"
EXTRA_LDLIBS="${UNWIND_LDLIBS}"
if config lrt "checking for libunwind... "; then
CFLAGS="${UNWIND_CFLAGS} ${CFLAGS}"
LDLIBS="${UNWIND_LDLIBS} ${LDLIBS}"
uncomment "#define HAVE_LIBUNWIND"
fi
fi

# attempt to polyfill c11 atomics if it's not available
if ! config stdatomic "checking for stdatomic.h... "; then
CPPFLAGS="-isystem tool/stdatomic"
Expand Down
5 changes: 4 additions & 1 deletion tool/config/config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘

o/$(MODE)/tool/config/%.com: tool/config/%.c
$(CC) -o $@ $<
@mkdir -p $(@D)
$(CC) -o $@ $< $(LDLIBS)

o/$(MODE)/tool/config/libunwind.com: private LDLIBS += -lunwind -lucontext -llzma
45 changes: 45 additions & 0 deletions tool/config/libunwind.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// checks for libunwind backtrace support
#define UNW_LOCAL_ONLY
#include <libunwind.h>
#include <stdio.h>
#include <string.h>

#define APPEND(...) o += snprintf(b + o, n - o, __VA_ARGS__)

const char *GetBacktrace(void) {
static char b[2048];
int o = 0;
char sym[256];
int n = sizeof(b);
unw_cursor_t cursor;
unw_context_t context;
unw_word_t offset, pc;
unw_getcontext(&context);
unw_init_local(&cursor, &context);
APPEND("backtrace");
while (unw_step(&cursor) > 0) {
unw_get_reg(&cursor, UNW_REG_IP, &pc);
if (!pc) break;
APPEND("\n\t%lx ", pc);
if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) {
APPEND("%s+%ld", sym, offset);
} else {
APPEND("<unknown>");
}
}
return b;
}

const char *MyApp(void) {
return GetBacktrace();
}

const char *(*MyAppPtr)(void) = MyApp;

int main(int argc, char *argv[]) {
const char *bt;
bt = MyAppPtr();
if (!strstr(bt, "MyApp")) return 1;
if (!strstr(bt, "main")) return 2;
return 0;
}

0 comments on commit 73f8985

Please sign in to comment.