From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-f176.google.com (mail-lj1-f176.google.com [209.85.208.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 7645545C305 for ; Thu, 10 Dec 2020 19:19:00 +0300 (MSK) Received: by mail-lj1-f176.google.com with SMTP id f11so7311049ljm.8 for ; Thu, 10 Dec 2020 08:19:00 -0800 (PST) From: Cyrill Gorcunov Date: Thu, 10 Dec 2020 19:18:30 +0300 Message-Id: <20201210161832.729439-3-gorcunov@gmail.com> In-Reply-To: <20201210161832.729439-1-gorcunov@gmail.com> References: <20201210161832.729439-1-gorcunov@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH v4 2/4] backtrace: allow to specify destination buffer List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tml Cc: Mons Anderson , Vladislav Shpilevoy This will allow to reuse this routine in crash reports. Part-of #5261 Signed-off-by: Cyrill Gorcunov --- src/lib/core/backtrace.cc | 12 ++++++------ src/lib/core/backtrace.h | 3 +++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/lib/core/backtrace.cc b/src/lib/core/backtrace.cc index 456ce9a4d..b4048089f 100644 --- a/src/lib/core/backtrace.cc +++ b/src/lib/core/backtrace.cc @@ -131,7 +131,7 @@ get_proc_name(unw_cursor_t *unw_cur, unw_word_t *offset, bool skip_cache) } char * -backtrace(void) +backtrace(char *start, size_t size) { int frame_no = 0; unw_word_t sp = 0, old_sp = 0, ip, offset; @@ -139,10 +139,9 @@ backtrace(void) unw_getcontext(&unw_context); unw_cursor_t unw_cur; unw_init_local(&unw_cur, &unw_context); - char *backtrace_buf = (char *)static_alloc(SMALL_STATIC_SIZE); - char *p = backtrace_buf; - char *end = p + SMALL_STATIC_SIZE - 1; int unw_status; + char *p = start; + char *end = start + size - 1; *p = '\0'; while ((unw_status = unw_step(&unw_cur)) > 0) { const char *proc; @@ -174,7 +173,7 @@ backtrace(void) say_debug("unwinding error: %i", unw_status); #endif out: - return backtrace_buf; + return start; } /* @@ -436,7 +435,8 @@ backtrace_foreach(backtrace_cb cb, coro_context *coro_ctx, void *cb_ctx) void print_backtrace(void) { - fdprintf(STDERR_FILENO, "%s", backtrace()); + char *start = (char *)static_alloc(SMALL_STATIC_SIZE); + fdprintf(STDERR_FILENO, "%s", backtrace(start, SMALL_STATIC_SIZE)); } #endif /* ENABLE_BACKTRACE */ diff --git a/src/lib/core/backtrace.h b/src/lib/core/backtrace.h index c119d5402..e0ae56be4 100644 --- a/src/lib/core/backtrace.h +++ b/src/lib/core/backtrace.h @@ -40,6 +40,9 @@ extern "C" { #ifdef ENABLE_BACKTRACE #include +char * +backtrace(char *start, size_t size); + void print_backtrace(void); typedef int (backtrace_cb)(int frameno, void *frameret, -- 2.26.2