Skip to contentSkip to conversation

lib: stacklog_debug: Introduce helper tool for collecting and displaying stacktraces

RFC PATCH Superseded John Stultz 1 participant 2 messages Oct 25, 2024 Library code

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/2 lib: stacklog_debug: Introduce helper tool for collecting and displaying stacktraces

When debugging, its often useful to understand how a function is called and the different paths taken to get there. Usually dump_stack() can be used for this purpose. However there are a number of cases where a function is called very frequently, making dump_stack far too noisy to be useful.

This is a little debug tool that utilizes stackdepot to capture unique stack traces and store them in a circular buffer.

In the code, the developer adds: stacklog_debug_save() calls at points of interest (as they might with stack_dump()).

Then after running the kernel, the developer can dump the unique stack traces from the buffer via:
cat /sys/kernel/debug/stacklog_debug

This is pretty trivial, but I've had this hanging around for awhile and recently hit another case where it was helpful, so I figured it would be worth sending it out for feedback as to if others thought it would be useful enough to merge upstream or to possibly rework into stackdepot itself?

Feedback would be greatly appreciated!

Cc Steven Rostedt <[email protected]>
Cc Alexander Potapenko <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Andrew Morton <[email protected]>

---
v2:
* Add dependency on STACKTRACE_SUPPORT as reported by kernel test robot <[email protected]>
* Expand the Kconfig help to include basic usage

---
 include/linux/stacklog_debug.h |  13 ++++
 lib/Kconfig                    |  16 +++++
 lib/Makefile                   |   1 +
 lib/stacklog_debug.c           | 110 +++++++++++++++++++++++++++++++++
 4 files changed, 140 insertions(+)
 create mode 100644 include/linux/stacklog_debug.h
 create mode 100644 lib/stacklog_debug.c
include/linux/stacklog_debug.h new file +13 -0
new file mode 100644
index 000000000000..d88f05d7000a
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef _LINUX_STACKLOG_DEBUG_H
+#define _LINUX_STACKLOG_DEBUG_H
+
+#ifdef CONFIG_STACKLOG_DEBUG
+void stacklog_debug_save(void);
+#else
+static inline void stacklog_debug_save(void)
+{
+}
+#endif
+#endif
lib/Kconfig +16 -0
index b38849af6f13..17eb0f8d30db 100644
@@ -725,6 +725,22 @@ config REF_TRACKER
 	depends on STACKTRACE_SUPPORT
 	select STACKDEPOT
 
+config STACKLOG_DEBUG
+	bool "Debug tool for logging and later displaying stacktraces"
+	depends on STACKTRACE_SUPPORT
+	select STACKDEPOT
+	select STACKDEPOT_ALWAYS_INIT
+	help
+	  Enables debug infrastructure for logging unique stack traces at
+	  a specific point, which can be later displayed from userland.
+
+	  The developer adds: stacklog_debug_save() calls at points of
+	  interest (as they might with stack_dump()).
+
+	  Then after running the kernel, the developer can dump the unique
+	  stack traces from the buffer via:
+	    cat /sys/kernel/debug/stacklog_debug
+
 config SBITMAP
 	bool
 
lib/Makefile +1 -0
index 773adf88af41..5a07573be73c 100644
@@ -264,6 +264,7 @@ obj-$(CONFIG_IRQ_POLL) += irq_poll.o
 
 obj-$(CONFIG_POLYNOMIAL) += polynomial.o
 
+obj-$(CONFIG_STACKLOG_DEBUG) += stacklog_debug.o
 # stackdepot.c should not be instrumented or call instrumented functions.
 # Prevent the compiler from calling builtins like memcmp() or bcmp() from this
 # file.
lib/stacklog_debug.c new file +110 -0

110 lines not on this page · raw message

Signature · 1 line
2.47.0.163.g1226f6d8fa-goog
#1 lore raw

RFC PATCH v2 2/2 lib: stackdepot: Avoid null pointer if stack_depot_save is called too early

If stack_depot_save is called before stackdepot has initialized a null reference to the stack_table might be used, crashing the system.

Instead, check stack_table has been initialized before going further.

Cc Steven Rostedt <[email protected]>
Cc Alexander Potapenko <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Andrew Morton <[email protected]>
---
 lib/stackdepot.c | 3 +++
 1 file changed, 3 insertions(+)
lib/stackdepot.c +3 -0
index 5ed34cc963fc..09780c835362 100644
@@ -611,6 +611,9 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries,
 	if (unlikely(nr_entries == 0) || stack_depot_disabled)
 		return 0;
 
+	if (!stack_table)
+		return 0;
+
 	hash = hash_stack(entries, nr_entries);
 	bucket = &stack_table[hash & stack_hash_mask];
 
Signature · 1 line
2.47.0.163.g1226f6d8fa-goog