Tarantool development patches archive
 help / color / mirror / Atom feed
From: Egor Elchinov via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: tarantool-patches@dev.tarantool.org
Cc: gorcunov@tarantool.org
Subject: [Tarantool-patches] [PATCH 3/7] fiber: apply fix patch
Date: Thu,  1 Jul 2021 18:54:46 +0300	[thread overview]
Message-ID: <d192ebfa1a070fdd139bfce2d30d398dbeb0a11d.1625153622.git.elchinov.es@gmail.com> (raw)
In-Reply-To: <cover.1625153622.git.elchinov.es@gmail.com>

From: Egor Elchinov <eelchinov@tarantool.org>

Some mistakes fixed.
More consistent naming provided.
Based on review and patch from
Cyrill Gorcunov <gorcunov@gmail.com>

Needed for: #4002
---
 src/lib/core/backtrace.cc | 47 +++++++++++++++++++++------------------
 src/lib/core/backtrace.h  |  8 ++++---
 src/lib/core/fiber.c      |  2 +-
 src/lib/core/fiber.h      |  2 +-
 src/lua/fiber.c           |  5 +++--
 5 files changed, 35 insertions(+), 29 deletions(-)

diff --git a/src/lib/core/backtrace.cc b/src/lib/core/backtrace.cc
index a1727374c..f97c4c0a9 100644
--- a/src/lib/core/backtrace.cc
+++ b/src/lib/core/backtrace.cc
@@ -443,8 +443,8 @@ out:
  * Must be by far faster than usual backtrace according to the
  * libunwind doc for unw_backtrace().
  */
-void
-fast_trace_collect(void **ip_buf, int limit)
+void NOINLINE
+backtrace_collect_ip(void **ip_buf, int limit)
 {
 	memset(ip_buf, 0, limit * sizeof(*ip_buf));
 #ifndef TARGET_OS_DARWIN
@@ -466,54 +466,57 @@ fast_trace_collect(void **ip_buf, int limit)
  * TODO: to add cache and demangling support
  */
 void
-fast_trace_foreach(backtrace_cb cb, void **ip_buf, int limit, void *cb_ctx)
+backtrace_foreach_ip(backtrace_cb cb, void **ip_buf, int limit,
+		     void *cb_ctx)
 {
 #ifndef TARGET_OS_DARWIN
-	static __thread char proc_name[BACKTRACE_NAME_MAX];
-	char* proc = NULL;
-	int frame_no = 0, ret = 0;
+	char proc_name[BACKTRACE_NAME_MAX];
 	unw_word_t ip = 0, offset = 0;
 	unw_proc_info_t pi;
-
-	unw_accessors_t* acc = unw_get_accessors(unw_local_addr_space);
-	assert(acc);
-
-	for (frame_no = 0; frame_no < limit && ip_buf[frame_no] != NULL;
-	     ++frame_no) {
+	int frame_no, ret = 0;
+	char *proc = NULL;
+
+	unw_accessors_t *acc = unw_get_accessors(unw_local_addr_space);
+
+	/*
+	 * RIPs collecting comes from inside a helper routine
+	 * so we skip the collector function address itself thus
+	 * start fetching functions with frame number = 1.
+	 */
+	for (frame_no = 1; frame_no < limit && ip_buf[frame_no] != NULL;
+	     frame_no++) {
 		ip = (unw_word_t)ip_buf[frame_no];
+
 		if (acc->get_proc_name == NULL) {
 			ret = unw_get_proc_info_by_ip(unw_local_addr_space,
 						      ip, &pi, NULL);
 			offset = ip - pi.start_ip;
 		} else {
 			ret = acc->get_proc_name(unw_local_addr_space, ip,
-			    			 proc_name, sizeof(proc_name),
-			    			 &offset, NULL);
+						 proc_name, sizeof(proc_name),
+						 &offset, NULL);
 			proc = proc_name;
 		}
-
-		if (ret != 0 || (frame_no > 0 &&
-		    cb(frame_no - 1, (void *)ip, proc,
-	 	       (size_t)offset, cb_ctx) != 0))
+		if (ret != 0 || cb(frame_no - 1, (void *)ip, proc,
+				   (size_t)offset, cb_ctx) != 0)
 			break;
 	}
 
 	if (ret != 0)
 		say_debug("unwinding error: %s", unw_strerror(ret));
 #else
-	int frame_no = 0, ret = 1;
+	int frame_no, ret = 1;
 	void *ip = NULL;
 	size_t offset = 0;
 	Dl_info dli;
 
-	for (frame_no = 0; frame_no < limit && ip_buf[frame_no] != NULL;
+	for (frame_no = 1; 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)
+		if (cb(frame_no - 1, ip, dli.dli_sname, offset, cb_ctx) != 0)
 			break;
 	}
 
diff --git a/src/lib/core/backtrace.h b/src/lib/core/backtrace.h
index 8bb668366..4552c14e1 100644
--- a/src/lib/core/backtrace.h
+++ b/src/lib/core/backtrace.h
@@ -31,6 +31,7 @@
  * SUCH DAMAGE.
  */
 #include "trivia/config.h"
+#include "trivia/util.h"
 #include <stddef.h>
 
 #if defined(__cplusplus)
@@ -55,11 +56,12 @@ backtrace_foreach(backtrace_cb cb, coro_context *coro_ctx, void *cb_ctx);
 void
 backtrace_proc_cache_clear(void);
 
-void
-fast_trace_collect(void **ip_buf, int limit);
+void NOINLINE
+backtrace_collect_ip(void **ip_buf, int limit);
 
 void
-fast_trace_foreach(backtrace_cb cb, void **ip_buf, int limit, void *cb_ctx);
+backtrace_foreach_ip(backtrace_cb cb, void **ip_buf, int limit,
+		     void *cb_ctx);
 
 #endif /* ENABLE_BACKTRACE */
 
diff --git a/src/lib/core/fiber.c b/src/lib/core/fiber.c
index 73500e043..924ff3c82 100644
--- a/src/lib/core/fiber.c
+++ b/src/lib/core/fiber.c
@@ -1265,7 +1265,7 @@ fiber_new_ex(const char *name, const struct fiber_attr *fiber_attr,
 	fiber->fid = cord->next_fid;
 	fiber_set_name(fiber, name);
 #if ENABLE_BACKTRACE
-	fast_trace_collect(fiber->parent_bt_ip_buf, FIBER_PARENT_BT_MAX);
+	backtrace_collect_ip(fiber->parent_bt_ip_buf, FIBER_PARENT_BT_MAX);
 #endif /* ENABLE_BACKTRACE */
 	register_fid(fiber);
 	fiber->csw = 0;
diff --git a/src/lib/core/fiber.h b/src/lib/core/fiber.h
index 6a155c0fe..beed58866 100644
--- a/src/lib/core/fiber.h
+++ b/src/lib/core/fiber.h
@@ -654,7 +654,7 @@ struct fiber {
 	char inline_name[FIBER_NAME_INLINE];
 #if ENABLE_BACKTRACE
 	/** Fiber creation backtrace chunk. */
-	void* parent_bt_ip_buf[FIBER_PARENT_BT_MAX];
+	void *parent_bt_ip_buf[FIBER_PARENT_BT_MAX];
 #endif /* ENABLE_BACKTRACE */
 };
 
diff --git a/src/lua/fiber.c b/src/lua/fiber.c
index dc7051edd..7b21361d4 100644
--- a/src/lua/fiber.c
+++ b/src/lua/fiber.c
@@ -314,8 +314,9 @@ lbox_fiber_statof_map(struct fiber *f, void *cb_ctx, bool backtrace)
 		tb_ctx.R = NULL;
 		lua_pushstring(L, "backtrace_parent");
 		lua_newtable(L);
-		fast_trace_foreach(fiber_backtrace_cb, f->parent_bt_ip_buf,
-		     		   FIBER_PARENT_BT_MAX, &tb_ctx);
+		backtrace_foreach_ip(fiber_backtrace_cb,
+				     f->parent_bt_ip_buf,
+				     FIBER_PARENT_BT_MAX, &tb_ctx);
 		lua_settable(L, -3);
 #endif /* ENABLE_BACKTRACE */
 	}
-- 
2.31.1


  parent reply	other threads:[~2021-07-01 15:56 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-01 15:54 [Tarantool-patches] [PATCH 0/7] fiber: introduce creation backtrace Egor Elchinov via Tarantool-patches
2021-07-01 15:54 ` [Tarantool-patches] [PATCH 1/7] fiber: add PoC for fiber " Egor Elchinov via Tarantool-patches
2021-07-01 15:54 ` [Tarantool-patches] [PATCH 2/7] fiber: fix DARWIN build Egor Elchinov via Tarantool-patches
2021-07-01 15:54 ` Egor Elchinov via Tarantool-patches [this message]
2021-07-01 15:54 ` [Tarantool-patches] [PATCH 4/7] fiber: add PoC for Lua parent backtrace Egor Elchinov via Tarantool-patches
2021-07-01 15:54 ` [Tarantool-patches] [PATCH 5/7] fiber: add dynamic option for " Egor Elchinov via Tarantool-patches
2021-07-01 15:54 ` [Tarantool-patches] [PATCH 6/7] fiber: refactor lua backtrace routine Egor Elchinov via Tarantool-patches
2021-07-01 15:54 ` [Tarantool-patches] [PATCH 7/7] fiber: refactor C backtrace and add changelog Egor Elchinov via Tarantool-patches
2021-07-01 20:24 [Tarantool-patches] [PATCH 0/7] fiber: introduce creation backtrace Egor Elchinov via Tarantool-patches
2021-07-01 20:24 ` [Tarantool-patches] [PATCH 3/7] fiber: apply fix patch Egor Elchinov via Tarantool-patches
2021-07-05  9:42   ` Cyrill Gorcunov via Tarantool-patches
2021-07-09  9:32     ` Egor Elchinov via Tarantool-patches

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=d192ebfa1a070fdd139bfce2d30d398dbeb0a11d.1625153622.git.elchinov.es@gmail.com \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=eelchinov@tarantool.org \
    --cc=gorcunov@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 3/7] fiber: apply fix patch' \
    /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