DPDK patches and discussions
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>
Subject: [RFC] eal_debug: do not use malloc in rte_dump_stack
Date: Fri, 28 Jan 2022 17:10:39 -0800	[thread overview]
Message-ID: <20220129011039.264377-1-stephen@networkplumber.org> (raw)

The glibc backtrace_symbols() calls malloc which makes it
dangerous to use rte_dump_stack() in a signal handler that
is handling errors that maybe due to memory corruption.

Instead, use dladdr() to lookup up symbols incrementally.

The format of the messages is based on what X org server
has been doing for many years. It changes from bottom up
to top down order.

Bugzilla ID: 929
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/linux/eal_debug.c | 45 ++++++++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 13 deletions(-)

diff --git a/lib/eal/linux/eal_debug.c b/lib/eal/linux/eal_debug.c
index 64dab4e0da24..bf232f72f402 100644
--- a/lib/eal/linux/eal_debug.c
+++ b/lib/eal/linux/eal_debug.c
@@ -4,6 +4,7 @@
 
 #ifdef RTE_BACKTRACE
 #include <execinfo.h>
+#include <dlfcn.h>
 #endif
 #include <stdarg.h>
 #include <signal.h>
@@ -18,26 +19,44 @@
 
 #define BACKTRACE_SIZE 256
 
-/* dump the stack of the calling core */
+/* Dump the stack of the calling core
+ *
+ * Note: this requires some careful usage in order to
+ * stay safe in case where called from a signal
+ * handler and the malloc pool may be corrupted.
+ */
 void rte_dump_stack(void)
 {
 #ifdef RTE_BACKTRACE
 	void *func[BACKTRACE_SIZE];
-	char **symb = NULL;
-	int size;
+	int i, size;
 
 	size = backtrace(func, BACKTRACE_SIZE);
-	symb = backtrace_symbols(func, size);
-
-	if (symb == NULL)
-		return;
 
-	while (size > 0) {
-		rte_log(RTE_LOG_ERR, RTE_LOGTYPE_EAL,
-			"%d: [%s]\n", size, symb[size - 1]);
-		size --;
+	for (i = 0; i < size; i++) {
+		void *pc = func[i];
+		const char *fname;
+		Dl_info info;
+
+		if (dladdr(pc, &info) == 0) {
+			rte_log(RTE_LOG_ERR, RTE_LOGTYPE_EAL,
+				"%d: ?? [%p]\n", i, pc);
+			continue;
+		}
+
+		fname = (info.dli_fname && *info.dli_fname) ? info.dli_fname : "(vdso)";
+		if (info.dli_saddr != NULL)
+			rte_log(RTE_LOG_ERR, RTE_LOGTYPE_EAL,
+				"%d: %s (%s+%#tx) [%p]\n",
+				i, fname, info.dli_sname,
+				(ptrdiff_t)((uintptr_t)pc - (uintptr_t)info.dli_saddr),
+				pc);
+		else
+			rte_log(RTE_LOG_ERR, RTE_LOGTYPE_EAL,
+				"%d: %s (%p+%#tx) [%p]\n",
+				i, fname, info.dli_fbase,
+				(ptrdiff_t)((uintptr_t)pc - (uintptr_t)info.dli_fbase),
+				pc);
 	}
-
	free(symb);
 #endif /* RTE_BACKTRACE */
 }
-- 
2.34.1


             reply	other threads:[~2022-01-29  1:10 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-29  1:10 Stephen Hemminger [this message]
2022-01-29  8:25 ` Morten Brørup
2022-02-12 18:44 ` [PATCH v2 0/2] rte_dump_stack: improvements Stephen Hemminger
2022-02-12 18:44   ` [PATCH v2 1/2] eal_debug: do not use malloc in rte_dump_stack Stephen Hemminger
2022-02-13 11:41     ` Thomas Monjalon
2022-03-17 23:13       ` Stephen Hemminger
2022-02-12 18:44   ` [PATCH v2 2/2] eal: common rte_dump_stack for both Linux and FreeBSD Stephen Hemminger
2022-02-14 11:10   ` [PATCH v2 0/2] rte_dump_stack: improvements Morten Brørup
2022-02-14 11:51     ` Bruce Richardson
2022-04-07 12:45   ` David Marchand
2022-04-07 23:06     ` Stephen Hemminger
2022-04-14 19:41 ` [PATCH v3] rte_dump_stack: make in async signal safe Stephen Hemminger
2022-04-14 20:19 ` [PATCH v4] " Stephen Hemminger
2022-06-23  7:51   ` David Marchand

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=20220129011039.264377-1-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=dev@dpdk.org \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).