Skip to contentSkip to conversation

eal_debug: do not use malloc in rte_dump_stack

RFC Superseded Stephen Hemminger 5 participants 14 messages Jan 29, 2022 – Jun 23, 2022 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

RFC 1/1 eal_debug: do not use malloc in rte_dump_stack

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

---
 lib/eal/linux/eal_debug.c | 45 ++++++++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 13 deletions(-)
lib/eal/linux/eal_debug.c +32 -12
index 64dab4e0da24..bf232f72f402 100644
@@ -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 */
}

Signature · 1 line
2.34.1
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Saturday, 29 January 2022 02.11

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.

Yes. We have experienced that problem with backtrace_symbols(); so as a workaround, our failure signal handler dumps all other information first, and calls backtrace_symbols() last, in case it crashes.

Quoted 2 lines

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

I took a brief look at the dladdr() source code, and it looks good to me.

Quoted 4 lines

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.

Good idea. Seems more logical.

Quoted 77 lines

Bugzilla ID: 929

---
 lib/eal/linux/eal_debug.c | 45 ++++++++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 13 deletions(-)
lib/eal/linux/eal_debug.c +32 -12
index 64dab4e0da24..bf232f72f402 100644
@@ -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);
 	}
-
Quoted 1 line
	free(symb);

Probably something is lost in formatting here, but free(symb) must also be removed.

Quoted 5 lines

#endif /* RTE_BACKTRACE */
}
--
2.34.1

Great improvement, Stephen!

#2 lore raw

v2 cover rte_dump_stack: improvements

In reply to Stephen Hemminger

This is update to earlier RFC. Add some more comments and changes to have common code for Linux and FreeBSD

Stephen Hemminger (2):
eal_debug: do not use malloc in rte_dump_stack
eal: common rte_dump_stack for both Linux and FreeBSD

 lib/eal/freebsd/eal_debug.c | 43 ------------------------
 lib/eal/freebsd/meson.build |  1 -
 lib/eal/linux/eal_debug.c   | 43 ------------------------
 lib/eal/linux/meson.build   |  1 -
 lib/eal/unix/eal_debug.c    | 65 +++++++++++++++++++++++++++++++++++++
 lib/eal/unix/meson.build    |  5 +--
 6 files changed, 68 insertions(+), 90 deletions(-)
 delete mode 100644 lib/eal/freebsd/eal_debug.c
 delete mode 100644 lib/eal/linux/eal_debug.c
 create mode 100644 lib/eal/unix/eal_debug.c
Signature · 1 line
2.34.1
#3 lore raw

PATCH v2 1/2 eal_debug: do not use malloc in rte_dump_stack

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.

---
 lib/eal/linux/eal_debug.c | 46 +++++++++++++++++++++++++++++----------
 1 file changed, 34 insertions(+), 12 deletions(-)
lib/eal/linux/eal_debug.c +34 -12
index 64dab4e0da24..51f24e3cccdf 100644
@@ -4,6 +4,7 @@
 
 #ifdef RTE_BACKTRACE
 #include <execinfo.h>
+#include <dlfcn.h>
 #endif
 #include <stdarg.h>
 #include <signal.h>
@@ -18,26 +19,47 @@
 
 #define BACKTRACE_SIZE 256
 
-/* dump the stack of the calling core */
+/* Dump the stack of the calling core
+ *
+ * Note: this requires limiting what functions are used in this code to stay safe
+ * when called from a signal handler and malloc pool maybe corrupt.
+ */
 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;
+	for (i = 0; i < size; i++) {
+		void *pc = func[i];
+		const char *fname;
+		Dl_info info;
 
-	while (size > 0) {
-		rte_log(RTE_LOG_ERR, RTE_LOGTYPE_EAL,
-			"%d: [%s]\n", size, symb[size - 1]);
-		size --;
-	}
+		if (dladdr(pc, &info) == 0) {
+			/* If symbol information not found print in hex */
+			rte_log(RTE_LOG_ERR, RTE_LOGTYPE_EAL,
+				"%d: ?? [%p]\n", i, pc);
+			continue;
+		}
 
-	free(symb);
+		/* Is file name known? */
+		fname = (info.dli_fname && *info.dli_fname) ? info.dli_fname : "(vdso)";
+
+		/* Is symbol name known? */
+		if (info.dli_sname != 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);
+	}
 #endif /* RTE_BACKTRACE */
 }
Signature · 1 line
2.34.1
#4 lore raw

12/02/2022 19:44, Stephen Hemminger:

Quoted 11 lines

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.

Morten acked the RFC.
Is there any significant change?

#5 lore raw
Quoted 7 lines · On Sun, 13 Feb 2022 12:41:59 +0100 Thomas Monjalon <[email protected]> wrote:

12/02/2022 19:44, Stephen Hemminger:

Quoted 6 lines

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.

Quoted 11 lines
Quoted 5 lines

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.

Morten acked the RFC.
Is there any significant change?

The change was making it not RFC.
Lets make a common code for this FreeBSD should have same code.

#6 lore raw

PATCH v2 2/2 eal: common rte_dump_stack for both Linux and FreeBSD

In reply to Stephen Hemminger

The core functions used by rte_dump_stack() are backtrace_symbols and dladdr. Both of those functions are the same in Linux and FreeBSD so the code for decoding stack should be common.

Also, the filenames unix/meson.build are now resorted.

---
 lib/eal/freebsd/eal_debug.c         | 43 -----------------------------
 lib/eal/freebsd/meson.build         |  1 -
 lib/eal/linux/meson.build           |  1 -
 lib/eal/{linux => unix}/eal_debug.c |  0
 lib/eal/unix/meson.build            |  5 ++--
 5 files changed, 3 insertions(+), 47 deletions(-)
 delete mode 100644 lib/eal/freebsd/eal_debug.c
 rename lib/eal/{linux => unix}/eal_debug.c (100%)
lib/eal/freebsd/eal_debug.c deleted +0 -43
deleted file mode 100644
index 64dab4e0da24..000000000000
@@ -1,43 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2010-2014 Intel Corporation
- */
-
-#ifdef RTE_BACKTRACE
-#include <execinfo.h>
-#endif
-#include <stdarg.h>
-#include <signal.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdint.h>
-
-#include <rte_log.h>
-#include <rte_debug.h>
-#include <rte_common.h>
-#include <rte_eal.h>
-
-#define BACKTRACE_SIZE 256
-
-/* dump the stack of the calling core */
-void rte_dump_stack(void)
-{
-#ifdef RTE_BACKTRACE
-	void *func[BACKTRACE_SIZE];
-	char **symb = NULL;
-	int 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 --;
-	}
-
-	free(symb);
-#endif /* RTE_BACKTRACE */
-}
lib/eal/freebsd/meson.build +0 -1
index 398ceab71d03..85cca5a096ca 100644
@@ -7,7 +7,6 @@ sources += files(
         'eal.c',
         'eal_alarm.c',
         'eal_cpuflags.c',
-        'eal_debug.c',
         'eal_dev.c',
         'eal_hugepage_info.c',
         'eal_interrupts.c',
lib/eal/linux/meson.build +0 -1
index 65f2ac6b4798..3cccfa36c0a4 100644
@@ -7,7 +7,6 @@ sources += files(
         'eal.c',
         'eal_alarm.c',
         'eal_cpuflags.c',
-        'eal_debug.c',
         'eal_dev.c',
         'eal_hugepage_info.c',
         'eal_interrupts.c',
lib/eal/unix/eal_debug.c renamed from lib/eal/linux/eal_debug.c +0 -0
similarity index 100%
rename from lib/eal/linux/eal_debug.c
rename to lib/eal/unix/eal_debug.c
lib/eal/unix/meson.build +3 -2
index a22ea7cabc46..9f724bfd5d7a 100644
@@ -2,10 +2,11 @@
 # Copyright(c) 2020 Dmitry Kozlyuk
 
 sources += files(
+        'eal_debug.c',
         'eal_file.c',
+        'eal_filesystem.c',
+        'eal_firmware.c',
         'eal_unix_memory.c',
         'eal_unix_timer.c',
-        'eal_firmware.c',
-        'eal_filesystem.c',
         'rte_thread.c',
 )
Signature · 1 line
2.34.1
Acked-by on the v2 series
#7 lore raw

RE: [PATCH v2 0/2] rte_dump_stack: improvements

In reply to Stephen Hemminger
Quoted 21 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Saturday, 12 February 2022 19.45

This is update to earlier RFC. Add some more comments and changes to have common code for Linux and FreeBSD

Stephen Hemminger (2):
eal_debug: do not use malloc in rte_dump_stack
eal: common rte_dump_stack for both Linux and FreeBSD

 lib/eal/freebsd/eal_debug.c | 43 ------------------------
 lib/eal/freebsd/meson.build |  1 -
 lib/eal/linux/eal_debug.c   | 43 ------------------------
 lib/eal/linux/meson.build   |  1 -
 lib/eal/unix/eal_debug.c    | 65 +++++++++++++++++++++++++++++++++++++
 lib/eal/unix/meson.build    |  5 +--
 6 files changed, 68 insertions(+), 90 deletions(-)
 delete mode 100644 lib/eal/freebsd/eal_debug.c
 delete mode 100644 lib/eal/linux/eal_debug.c
 create mode 100644 lib/eal/unix/eal_debug.c
Quoted 3 lines

--
2.34.1

The dladdr() man page mentions that linking with -ldl is required; I assume this is already part of the DPDK EAL build system?

For the series,

#8 lore raw

Re: [PATCH v2 0/2] rte_dump_stack: improvements

Quoted 21 lines · Morten Brørup
Quoted 21 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Saturday, 12 February 2022 19.45

This is update to earlier RFC. Add some more comments and changes to have common code for Linux and FreeBSD

Stephen Hemminger (2):
eal_debug: do not use malloc in rte_dump_stack
eal: common rte_dump_stack for both Linux and FreeBSD

 lib/eal/freebsd/eal_debug.c | 43 ------------------------
 lib/eal/freebsd/meson.build |  1 -
 lib/eal/linux/eal_debug.c   | 43 ------------------------
 lib/eal/linux/meson.build   |  1 -
 lib/eal/unix/eal_debug.c    | 65 +++++++++++++++++++++++++++++++++++++
 lib/eal/unix/meson.build    |  5 +--
 6 files changed, 68 insertions(+), 90 deletions(-)
 delete mode 100644 lib/eal/freebsd/eal_debug.c
 delete mode 100644 lib/eal/linux/eal_debug.c
 create mode 100644 lib/eal/unix/eal_debug.c
Quoted 6 lines
Quoted 3 lines

--
2.34.1

The dladdr() man page mentions that linking with -ldl is required; I assume this is already part of the DPDK EAL build system?

Yes, we should be already linking against dl because of the use of dlopen for loading drivers dynamically.

#9 lore raw

Re: [PATCH v2 0/2] rte_dump_stack: improvements

In reply to Stephen Hemminger
Quoted 8 lines · Stephen Hemminger

This is update to earlier RFC. Add some more comments and changes to have common code for Linux and FreeBSD

Stephen Hemminger (2):
eal_debug: do not use malloc in rte_dump_stack
eal: common rte_dump_stack for both Linux and FreeBSD

Quoted 10 lines
 lib/eal/freebsd/eal_debug.c | 43 ------------------------
 lib/eal/freebsd/meson.build |  1 -
 lib/eal/linux/eal_debug.c   | 43 ------------------------
 lib/eal/linux/meson.build   |  1 -
 lib/eal/unix/eal_debug.c    | 65 +++++++++++++++++++++++++++++++++++++
 lib/eal/unix/meson.build    |  5 +--
 6 files changed, 68 insertions(+), 90 deletions(-)
 delete mode 100644 lib/eal/freebsd/eal_debug.c
 delete mode 100644 lib/eal/linux/eal_debug.c
 create mode 100644 lib/eal/unix/eal_debug.c

Strange to change only the Linux implementation as a first patch, then merge implementations in a second time effectively changing FreeBSD implementation in what is presented in commitlog as a factorisation cleanup.
Please invert the patches.

Besides, the series does not compile on current main. It's probably a result of the header inclusion cleanup we had in v22.03, but I prefer you check.

Thanks.

Signature · 1 line
David Marchand
#10 lore raw

Re: [PATCH v2 0/2] rte_dump_stack: improvements

Quoted 27 lines · On Thu, 7 Apr 2022 14:45:07 +0200 David Marchand <[email protected]>…
Quoted 8 lines · Stephen Hemminger

This is update to earlier RFC. Add some more comments and changes to have common code for Linux and FreeBSD

Stephen Hemminger (2):
eal_debug: do not use malloc in rte_dump_stack
eal: common rte_dump_stack for both Linux and FreeBSD

Quoted 10 lines
 lib/eal/freebsd/eal_debug.c | 43 ------------------------
 lib/eal/freebsd/meson.build |  1 -
 lib/eal/linux/eal_debug.c   | 43 ------------------------
 lib/eal/linux/meson.build   |  1 -
 lib/eal/unix/eal_debug.c    | 65 +++++++++++++++++++++++++++++++++++++
 lib/eal/unix/meson.build    |  5 +--
 6 files changed, 68 insertions(+), 90 deletions(-)
 delete mode 100644 lib/eal/freebsd/eal_debug.c
 delete mode 100644 lib/eal/linux/eal_debug.c
 create mode 100644 lib/eal/unix/eal_debug.c  

Strange to change only the Linux implementation as a first patch, then merge implementations in a second time effectively changing FreeBSD implementation in what is presented in commitlog as a factorisation cleanup.
Please invert the patches.

Quoted 6 lines

Besides, the series does not compile on current main. It's probably a result of the header inclusion cleanup we had in v22.03, but I prefer you check.

Thanks.

As I looked at it more, there was more there.
Turns out that printf and therefore rte_log() is not signal safe.
There is a version of backtrace_symbols_fd that just uses writev() on glibc for Linux so that is a better alternative, but format changes.

But the BSD version of backtrace_symbols_fd uses printf and is therefore not signal safe. Not sure if that matters.

#11 lore raw

PATCH v3 1/1 rte_dump_stack: make in async signal safe

In reply to Stephen Hemminger

rte_dump_stack needs to be usable in situations when a bug is encountered and from signal handlers (such as SEGV).

Glibc backtrace_symbols() calls malloc which makes it dangerous in a signal handler that is handling errors that maybe due to memory corruption. Additionally, rte_log() is unsafe because syslog() is not signal safe; printf() is also documented as not being safe.

This version formats message and uses writev for each line in a manner similar to what glibc version of backtrace_symbols_fd() does. The FreeBSD version of backtrace_symbols_fd() is not signal safe.

Sample output:

0: ./build/app/dpdk-testpmd (rte_dump_stack+0x2b) [560a6e9c002b] 1: ./build/app/dpdk-testpmd (main+0xad) [560a6decd5ad]
2: /lib/x86_64-linux-gnu/libc.so.6 (__libc_start_main+0xcd) [7fd43d3e27fd] 3: ./build/app/dpdk-testpmd (_start+0x2a) [560a6e83628a]

Bugzilla ID: 929

---
v3
- merge previous two patches into one
common Linux/FreeBSD code.
- rewrite the code to not use functions which are not documented to be signal safe.

 lib/eal/freebsd/eal_debug.c |  43 -------------
 lib/eal/freebsd/meson.build |   1 -
 lib/eal/include/rte_debug.h |   2 +-
 lib/eal/linux/eal_debug.c   |  38 -----------
 lib/eal/linux/meson.build   |   1 -
 lib/eal/unix/eal_debug.c    | 123 ++++++++++++++++++++++++++++++++++++
 lib/eal/unix/meson.build    |   1 +
 7 files changed, 125 insertions(+), 84 deletions(-)
 delete mode 100644 lib/eal/freebsd/eal_debug.c
 delete mode 100644 lib/eal/linux/eal_debug.c
 create mode 100644 lib/eal/unix/eal_debug.c
lib/eal/freebsd/eal_debug.c deleted +0 -43
deleted file mode 100644
index 64dab4e0da24..000000000000
@@ -1,43 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2010-2014 Intel Corporation
- */
-
-#ifdef RTE_BACKTRACE
-#include <execinfo.h>
-#endif
-#include <stdarg.h>
-#include <signal.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdint.h>
-
-#include <rte_log.h>
-#include <rte_debug.h>
-#include <rte_common.h>
-#include <rte_eal.h>
-
-#define BACKTRACE_SIZE 256
-
-/* dump the stack of the calling core */
-void rte_dump_stack(void)
-{
-#ifdef RTE_BACKTRACE
-	void *func[BACKTRACE_SIZE];
-	char **symb = NULL;
-	int 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 --;
-	}
-
-	free(symb);
-#endif /* RTE_BACKTRACE */
-}
lib/eal/freebsd/meson.build +0 -1
index 398ceab71d03..85cca5a096ca 100644
@@ -7,7 +7,6 @@ sources += files(
         'eal.c',
         'eal_alarm.c',
         'eal_cpuflags.c',
-        'eal_debug.c',
         'eal_dev.c',
         'eal_hugepage_info.c',
         'eal_interrupts.c',
lib/eal/include/rte_debug.h +1 -1
index c4bc71ce28f5..2c4b94a7c9bf 100644
@@ -22,7 +22,7 @@ extern "C" {
 #endif
 
 /**
- * Dump the stack of the calling core to the console.
+ * Dump the stack of the calling core to the standard error.
  */
 void rte_dump_stack(void);
 
lib/eal/linux/eal_debug.c deleted +0 -38
deleted file mode 100644
index b0ecf5a9dcde..000000000000
@@ -1,38 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2010-2014 Intel Corporation
- */
-
-#ifdef RTE_BACKTRACE
-#include <execinfo.h>
-#endif
-#include <stdlib.h>
-#include <stdio.h>
-
-#include <rte_log.h>
-#include <rte_debug.h>
-
-#define BACKTRACE_SIZE 256
-
-/* dump the stack of the calling core */
-void rte_dump_stack(void)
-{
-#ifdef RTE_BACKTRACE
-	void *func[BACKTRACE_SIZE];
-	char **symb = NULL;
-	int 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 --;
-	}
-
-	free(symb);
-#endif /* RTE_BACKTRACE */
-}
lib/eal/linux/meson.build +0 -1
index 65f2ac6b4798..3cccfa36c0a4 100644
@@ -7,7 +7,6 @@ sources += files(
         'eal.c',
         'eal_alarm.c',
         'eal_cpuflags.c',
-        'eal_debug.c',
         'eal_dev.c',
         'eal_hugepage_info.c',
         'eal_interrupts.c',
lib/eal/unix/eal_debug.c new file +123 -0

123 lines not on this page · raw message

lib/eal/unix/meson.build +1 -0
index a22ea7cabc46..9c59f3d31693 100644
@@ -2,6 +2,7 @@
 # Copyright(c) 2020 Dmitry Kozlyuk
 
 sources += files(
+        'eal_debug.c',
         'eal_file.c',
         'eal_unix_memory.c',
         'eal_unix_timer.c',
Signature · 1 line
2.35.1
#12 lore raw

PATCH v4 1/1 rte_dump_stack: make in async signal safe

In reply to Stephen Hemminger

rte_dump_stack() needs to be usable in situations when a bug is encountered and from signal handlers (such as SEGV).

Glibc backtrace_symbols() calls malloc which makes it dangerous in a signal handler that is handling errors that maybe due to memory corruption. Additionally, rte_log() is unsafe because syslog() is not signal safe; printf() is also documented as not being safe.

This version formats message and uses writev for each line in a manner similar to what glibc version of backtrace_symbols_fd() does. The FreeBSD version of backtrace_symbols_fd() is not signal safe.

Sample output:

0: ./build/app/dpdk-testpmd (rte_dump_stack+0x2b) [560a6e9c002b] 1: ./build/app/dpdk-testpmd (main+0xad) [560a6decd5ad]
2: /lib/x86_64-linux-gnu/libc.so.6 (__libc_start_main+0xcd) [7fd43d3e27fd] 3: ./build/app/dpdk-testpmd (_start+0x2a) [560a6e83628a]

Bugzilla ID: 929

---
v4
- fix whitespace report from checkpatch
v3
- merge previous two patches into one
common Linux/FreeBSD code.
- rewrite the code to not use functions which are not documented to be signal safe.

 lib/eal/freebsd/eal_debug.c |  43 -------------
 lib/eal/freebsd/meson.build |   1 -
 lib/eal/include/rte_debug.h |   2 +-
 lib/eal/linux/eal_debug.c   |  38 -----------
 lib/eal/linux/meson.build   |   1 -
 lib/eal/unix/eal_debug.c    | 123 ++++++++++++++++++++++++++++++++++++
 lib/eal/unix/meson.build    |   1 +
 7 files changed, 125 insertions(+), 84 deletions(-)
 delete mode 100644 lib/eal/freebsd/eal_debug.c
 delete mode 100644 lib/eal/linux/eal_debug.c
 create mode 100644 lib/eal/unix/eal_debug.c
lib/eal/freebsd/eal_debug.c deleted +0 -43
deleted file mode 100644
index 64dab4e0da24..000000000000
@@ -1,43 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2010-2014 Intel Corporation
- */
-
-#ifdef RTE_BACKTRACE
-#include <execinfo.h>
-#endif
-#include <stdarg.h>
-#include <signal.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdint.h>
-
-#include <rte_log.h>
-#include <rte_debug.h>
-#include <rte_common.h>
-#include <rte_eal.h>
-
-#define BACKTRACE_SIZE 256
-
-/* dump the stack of the calling core */
-void rte_dump_stack(void)
-{
-#ifdef RTE_BACKTRACE
-	void *func[BACKTRACE_SIZE];
-	char **symb = NULL;
-	int 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 --;
-	}
-
-	free(symb);
-#endif /* RTE_BACKTRACE */
-}
lib/eal/freebsd/meson.build +0 -1
index 398ceab71d03..85cca5a096ca 100644
@@ -7,7 +7,6 @@ sources += files(
         'eal.c',
         'eal_alarm.c',
         'eal_cpuflags.c',
-        'eal_debug.c',
         'eal_dev.c',
         'eal_hugepage_info.c',
         'eal_interrupts.c',
lib/eal/include/rte_debug.h +1 -1
index c4bc71ce28f5..2c4b94a7c9bf 100644
@@ -22,7 +22,7 @@ extern "C" {
 #endif
 
 /**
- * Dump the stack of the calling core to the console.
+ * Dump the stack of the calling core to the standard error.
  */
 void rte_dump_stack(void);
 
lib/eal/linux/eal_debug.c deleted +0 -38
deleted file mode 100644
index b0ecf5a9dcde..000000000000
@@ -1,38 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2010-2014 Intel Corporation
- */
-
-#ifdef RTE_BACKTRACE
-#include <execinfo.h>
-#endif
-#include <stdlib.h>
-#include <stdio.h>
-
-#include <rte_log.h>
-#include <rte_debug.h>
-
-#define BACKTRACE_SIZE 256
-
-/* dump the stack of the calling core */
-void rte_dump_stack(void)
-{
-#ifdef RTE_BACKTRACE
-	void *func[BACKTRACE_SIZE];
-	char **symb = NULL;
-	int 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 --;
-	}
-
-	free(symb);
-#endif /* RTE_BACKTRACE */
-}
lib/eal/linux/meson.build +0 -1
index 65f2ac6b4798..3cccfa36c0a4 100644
@@ -7,7 +7,6 @@ sources += files(
         'eal.c',
         'eal_alarm.c',
         'eal_cpuflags.c',
-        'eal_debug.c',
         'eal_dev.c',
         'eal_hugepage_info.c',
         'eal_interrupts.c',
lib/eal/unix/eal_debug.c new file +123 -0

123 lines not on this page · raw message

lib/eal/unix/meson.build +1 -0
index 781505ca9061..cc7d67dd321d 100644
@@ -2,6 +2,7 @@
 # Copyright(c) 2020 Dmitry Kozlyuk
 
 sources += files(
+        'eal_debug.c',
         'eal_file.c',
         'eal_filesystem.c',
         'eal_firmware.c',
Signature · 1 line
2.35.1
Reviewed-by on v4 1/1 Applied
#13 lore raw

Re: [PATCH v4] rte_dump_stack: make in async signal safe

Quoted 16 lines · Stephen Hemminger

rte_dump_stack() needs to be usable in situations when a bug is encountered and from signal handlers (such as SEGV).

Glibc backtrace_symbols() calls malloc which makes it dangerous in a signal handler that is handling errors that maybe due to memory corruption. Additionally, rte_log() is unsafe because syslog() is not signal safe; printf() is also documented as not being safe.

This version formats message and uses writev for each line in a manner similar to what glibc version of backtrace_symbols_fd() does. The FreeBSD version of backtrace_symbols_fd() is not signal safe.

Sample output:

Quoted 8 lines

0: ./build/app/dpdk-testpmd (rte_dump_stack+0x2b) [560a6e9c002b] 1: ./build/app/dpdk-testpmd (main+0xad) [560a6decd5ad]
2: /lib/x86_64-linux-gnu/libc.so.6 (__libc_start_main+0xcd) [7fd43d3e27fd] 3: ./build/app/dpdk-testpmd (_start+0x2a) [560a6e83628a]

Bugzilla ID: 929

Applied, thanks.

Signature · 1 line
David Marchand