flight data recorder for reference count debugging
Subsystems (3)
I was trying to debug a refcnt bug and it was just painful and none of the available debugging options seemed good, so I wrote an event tracking debugging facility.
It records fixed number of latest events, each of which consist of u8 user data, stack trace and time delta from the last event, per target data structure and transfers the records into storage area when the data structure gets destroyed. Later when refcnt discrepancy is discovered, the record can be dumped showing how the refcnt was manipulated when, making it easy (or at least possible) to spot which path forgot to obtain a refcnt or did double put.
For details, please read the comment at the top of fdr.h.
This can be pretty useful in certain situations but I'm not sure whether this fits upstream, so, for now, just posting so that people, including me, can find it.
NOT FOR UPSTREAM INCLUSION
--- arch/x86/include/asm/stacktrace.h | 3 arch/x86/kernel/stacktrace.c | 40 +++ include/linux/fdr.h | 181 +++++++++++++++ include/linux/stacktrace.h | 6 kernel/stacktrace.c | 6 lib/Kconfig.debug | 7 lib/Makefile | 1 lib/fdr.c | 435 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 679 insertions(+)
include/linux/stacktrace.h +6 -0
@@ -19,6 +19,12 @@ extern void save_stack_trace_regs(structextern void save_stack_trace_tsk (struct task_struct *tsk,struct stack_trace *trace); +/* +* Saves only trace from the current context. Doesn't handle exception +* stacks or verify text address. +*/ +extern void save_stack_trace_quick (struct stack_trace *trace); +extern void print_stack_trace (struct stack_trace *trace,int spaces);#ifdef CONFIG_USER_STACKTRACE_SUPPORT
kernel/stacktrace.c +6 -0
@@ -31,6 +31,12 @@ EXPORT_SYMBOL_GPL(print_stack_trace);* (whenever this facility is utilized - for example by procfs): */ __weak void +save_stack_trace_quick (struct stack_trace *trace) +{ +WARN_ONCE (1 ,KERN_INFO "save_stack_trace_quick() not implemented yet.\n" ); +} + +__weak void save_stack_trace_tsk (struct task_struct *tsk,struct stack_trace *trace) {WARN_ONCE (1 ,KERN_INFO "save_stack_trace_tsk() not implemented yet.\n" );
arch/x86/include/asm/stacktrace.h +3 -0
@@ -48,11 +48,14 @@ void dump_trace(struct task_struct *tsk, const struct stacktrace_ops *ops, void *data);#ifdef CONFIG_X86_32 +#define STACKSLOTS_PER_LINE 8#define get_bp (bp) asm("movl %%ebp, %0" : "=r" (bp) :) +#define get_irq_stack_end () 0#else #define STACKSLOTS_PER_LINE 4#define get_bp (bp) asm("movq %%rbp, %0" : "=r" (bp) :) +#define get_irq_stack_end () (unsigned long)this_cpu_read(irq_stack_ptr)#endif #ifdef CONFIG_FRAME_POINTER
arch/x86/kernel/stacktrace.c +40 -0
@@ -81,6 +81,46 @@ void save_stack_trace_tsk(struct task_st }EXPORT_SYMBOL_GPL (save_stack_trace_tsk); +#ifdef CONFIG_FRAME_POINTER +void save_stack_trace_quick (struct stack_trace *trace) +{ +const unsigned long stk_sz =THREAD_SIZE -sizeof (struct stack_frame ); +unsigned long tstk = (unsigned long )current_thread_info (); +unsigned long istk =get_irq_stack_end (); +unsigned long last_bp =0 ; +unsigned long bp, stk; + +get_bp (bp); + +if (bp > tstk && bp <= tstk + stk_sz) + stk = tstk; +else if (istk && (bp > istk && bp <= stk_sz)) + stk = istk; +else + goto out; + +while (bp > last_bp && bp <= stk + stk_sz) { +struct stack_frame *frame = (struct stack_frame *)bp; +unsigned long ret_addr = frame->return_address; + +if (!trace->skip) { +if (trace->nr_entries >= trace->max_entries) +return ; + trace->entries[trace->nr_entries++] = ret_addr; + }else { + trace->skip--; + } + + last_bp = bp; + bp = (unsigned long )frame->next_frame; + } +out: +if (trace->nr_entries < trace->max_entries) + trace->entries[trace->nr_entries++] =ULONG_MAX ; +} +EXPORT_SYMBOL_GPL (save_stack_trace_quick); +#endif +/* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */ struct stack_frame_user {
include/linux/fdr.h new file +181 -0
181 lines not on this page · raw message
lib/Kconfig.debug +7 -0
@@ -374,6 +374,13 @@ config DEBUG_OBJECTS_ENABLE_DEFAULT help Debug objects boot parameter default value +config FDR + bool "Flight data recorder (for refcnt debugging)" + depends on DEBUG_KERNEL + select FRAME_POINTER if !MIPS && !PPC && !ARM_UNWIND && !S390 && !MICROBLAZE + help + If you're lost in refcounting hell without any hope. + config DEBUG_SLAB bool "Debug slab memory allocations" depends on DEBUG_KERNEL && SLAB && !KMEMCHECK
lib/Makefile +1 -0
@@ -47,6 +47,7 @@ obj-$(CONFIG_BTREE) += btree.o obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o obj-$(CONFIG_DEBUG_LIST) += list_debug.o obj-$(CONFIG_DEBUG_OBJECTS) += debugobjects.o +obj-$(CONFIG_FDR) += fdr.o ifneq ($(CONFIG_HAVE_DEC_LOCK),y) lib-y += dec_and_lock.o
lib/fdr.c new file +435 -0
435 lines not on this page · raw message