lib: stacklog_debug: Introduce helper tool for collecting and displaying stacktraces
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!
---
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
@@ -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
@@ -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
@@ -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
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.
--- lib/stackdepot.c | 3 +++ 1 file changed, 3 insertions(+)
lib/stackdepot.c +3 -0
@@ -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