From: Egor Elchinov via Tarantool-patches <tarantool-patches@dev.tarantool.org> To: gorcunov@tarantool.org, tarantool-patches@dev.tarantool.org Cc: Egor2001 <elchinov.es@gmail.com> Subject: [Tarantool-patches] [PATCH 2/2] fiber: fix DARWIN build Date: Fri, 4 Jun 2021 14:13:11 +0300 [thread overview] Message-ID: <ec32ca6cbb7b75ee6acde37858a1d693d33d7b44.1622792861.git.elchinov.es@gmail.com> (raw) In-Reply-To: <cover.1622792861.git.elchinov.es@gmail.com> From: Egor2001 <elchinov.es@gmail.com> Due to the limited libunwind API under osx this fix uses backtrace() from <execinfo.h> and dladdr() from <dlfcn.h> instead of absent libunwind routines. Custom backtrace() function was renamed to backtrace_to_buf() because of the name conflict with backtrace() routine from <execinfo.h>. Needed for: #4002 --- src/lib/core/backtrace.cc | 38 +++++++++++++++++++++++++++++++------- src/lib/core/backtrace.h | 2 +- src/lib/core/crash.c | 2 +- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/lib/core/backtrace.cc b/src/lib/core/backtrace.cc index 276933ad6..a1727374c 100644 --- a/src/lib/core/backtrace.cc +++ b/src/lib/core/backtrace.cc @@ -46,6 +46,11 @@ #ifdef ENABLE_BACKTRACE #include <libunwind.h> +#ifdef TARGET_OS_DARWIN +#include <execinfo.h> +#include <dlfcn.h> +#endif + #include "small/region.h" #include "small/static.h" /* @@ -131,7 +136,7 @@ error: } char * -backtrace(char *start, size_t size) +backtrace_to_buf(char *start, size_t size) { int frame_no = 0; unw_word_t sp = 0, old_sp = 0, ip, offset; @@ -442,7 +447,11 @@ void fast_trace_collect(void **ip_buf, int limit) { memset(ip_buf, 0, limit * sizeof(*ip_buf)); +#ifndef TARGET_OS_DARWIN unw_backtrace(ip_buf, limit); +#else + backtrace(ip_buf, limit); +#endif } /** @@ -459,12 +468,12 @@ fast_trace_collect(void **ip_buf, int limit) void fast_trace_foreach(backtrace_cb cb, void **ip_buf, int limit, void *cb_ctx) { +#ifndef TARGET_OS_DARWIN static __thread char proc_name[BACKTRACE_NAME_MAX]; - int frame_no = 0; + char* proc = NULL; + int frame_no = 0, ret = 0; unw_word_t ip = 0, offset = 0; unw_proc_info_t pi; - int ret = 0; - char* proc = NULL; unw_accessors_t* acc = unw_get_accessors(unw_local_addr_space); assert(acc); @@ -489,11 +498,26 @@ fast_trace_foreach(backtrace_cb cb, void **ip_buf, int limit, void *cb_ctx) break; } -#ifndef TARGET_OS_DARWIN if (ret != 0) say_debug("unwinding error: %s", unw_strerror(ret)); #else - if (ret != 0) + int frame_no = 0, ret = 1; + void *ip = NULL; + size_t offset = 0; + Dl_info dli; + + for (frame_no = 0; frame_no < limit && ip_buf[frame_no] != NULL; + ++frame_no) { + ip = ip_buf[frame_no]; + ret = dladdr(ip, &dli); + offset = (char *)ip - (char *)dli.dli_saddr; + + if (frame_no > 0 && + cb(frame_no - 1, ip, dli.dli_sname, offset, cb_ctx) != 0) + break; + } + + if (ret == 0) say_debug("unwinding error: %i", ret); #endif } @@ -502,7 +526,7 @@ void print_backtrace(void) { char *start = (char *)static_alloc(SMALL_STATIC_SIZE); - fdprintf(STDERR_FILENO, "%s", backtrace(start, SMALL_STATIC_SIZE)); + fdprintf(STDERR_FILENO, "%s", backtrace_to_buf(start, SMALL_STATIC_SIZE)); } #endif /* ENABLE_BACKTRACE */ diff --git a/src/lib/core/backtrace.h b/src/lib/core/backtrace.h index 1560f03e3..8bb668366 100644 --- a/src/lib/core/backtrace.h +++ b/src/lib/core/backtrace.h @@ -41,7 +41,7 @@ extern "C" { #include <coro.h> char * -backtrace(char *start, size_t size); +backtrace_to_buf(char *start, size_t size); void print_backtrace(void); diff --git a/src/lib/core/crash.c b/src/lib/core/crash.c index abb7837e6..d1990d68b 100644 --- a/src/lib/core/crash.c +++ b/src/lib/core/crash.c @@ -213,7 +213,7 @@ crash_collect(int signo, siginfo_t *siginfo, void *ucontext) #ifdef ENABLE_BACKTRACE char *start = cinfo->backtrace_buf; - backtrace(start, sizeof(cinfo->backtrace_buf)); + backtrace_to_buf(start, sizeof(cinfo->backtrace_buf)); #endif #ifdef HAS_GREG -- 2.31.1
prev parent reply other threads:[~2021-06-04 11:14 UTC|newest] Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-06-04 11:13 [Tarantool-patches] [PATCH 0/2] [draft] fiber: introduce fiber creation backtrace Egor Elchinov via Tarantool-patches 2021-06-04 11:13 ` [Tarantool-patches] [PATCH 1/2] fiber: add PoC for " Egor Elchinov via Tarantool-patches 2021-06-07 8:28 ` Cyrill Gorcunov via Tarantool-patches 2021-06-07 16:39 ` Egor Elchinov via Tarantool-patches 2021-06-04 11:13 ` Egor Elchinov via Tarantool-patches [this message]
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=ec32ca6cbb7b75ee6acde37858a1d693d33d7b44.1622792861.git.elchinov.es@gmail.com \ --to=tarantool-patches@dev.tarantool.org \ --cc=eelchinov@tarantool.org \ --cc=elchinov.es@gmail.com \ --cc=gorcunov@tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH 2/2] fiber: fix DARWIN build' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox