Skip to contentSkip to conversation

generic show_stack facility

PATCH In review Christoph Hellwig 6 participants 13 messages Mar 29, 2002 – Mar 30, 2002

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 generic show_stack facility

This patch adds a prototype for show_stack to sched.h and exports it. Andrea's VM updates want this, as does Tux and I think it's a facility we want to proive genericly.

Note that some architectures (e.g. ia64) have a conflicting prototype and some (e.g. sparc) don't have show_stack at all, but I think -pre5 is early enough in the 2.4.19 cycle to let the maintainers fix it.

Could you apply the patch to your tree?

Christoph

arch/i386/kernel/irq.c renamed from master/linux-2.4.19-pre4/arch/i386/kernel/irq.c +1 -3
diff -uNr -Xdontdiff ../master/linux-2.4.19-pre4/arch/i386/kernel/irq.c linux/arch/i386/kernel/irq.c
@@ -192,8 +192,6 @@
 unsigned char global_irq_holder = NO_PROC_ID;
 unsigned volatile long global_irq_lock; /* pendantic: long for set_bit --RR */
 
-extern void show_stack(unsigned long* esp);
-
 static void show(char * str)
 {
 	int i;
@@ -224,7 +222,7 @@
 		}
 		esp &= ~(THREAD_SIZE-1);
 		esp += sizeof(struct task_struct);
-		show_stack((void*)esp);
+		show_stack((void *)esp);
  	}
 	printk("\nCPU %d:",cpu);
 	show_stack(NULL);
arch/ia64/kernel/irq.c renamed from master/linux-2.4.19-pre4/arch/ia64/kernel/irq.c +0 -2
diff -uNr -Xdontdiff ../master/linux-2.4.19-pre4/arch/ia64/kernel/irq.c linux/arch/ia64/kernel/irq.c
@@ -192,8 +192,6 @@
 unsigned int global_irq_holder = NO_PROC_ID;
 unsigned volatile long global_irq_lock; /* pedantic: long for set_bit --RR */
 
-extern void show_stack(unsigned long* esp);
-
 static void show(char * str)
 {
 	int i;
arch/mips/kernel/traps.c renamed from master/linux-2.4.19-pre4/arch/mips/kernel/traps.c +3 -3
diff -uNr -Xdontdiff ../master/linux-2.4.19-pre4/arch/mips/kernel/traps.c linux/arch/mips/kernel/traps.c
@@ -182,12 +182,12 @@
  * This routine abuses get_user()/put_user() to reference pointers
  * with at least a bit of error checking ...
  */
-void show_stack(unsigned int *sp)
+void show_stack(unsigned long *sp)
 {
 	int i;
-	unsigned int *stack;
+	unsigned long *stack;
 
-	stack = sp ? sp : (unsigned int *)&sp;
+	stack = sp ? sp : (unsigned long *)&sp;
 	i = 0;
 
 	printk("Stack:");
include/linux/sched.h renamed from master/linux-2.4.19-pre4/include/linux/sched.h +1 -0
diff -uNr -Xdontdiff ../master/linux-2.4.19-pre4/include/linux/sched.h linux/include/linux/sched.h
@@ -145,6 +145,7 @@
 extern void sched_init(void);
 extern void init_idle(void);
 extern void show_state(void);
+extern void show_stack(unsigned long *esp);
 extern void cpu_init (void);
 extern void trap_init(void);
 extern void update_process_times(int user);
kernel/ksyms.c renamed from master/linux-2.4.19-pre4/kernel/ksyms.c +1 -0
diff -uNr -Xdontdiff ../master/linux-2.4.19-pre4/kernel/ksyms.c linux/kernel/ksyms.c
@@ -487,6 +487,7 @@
 EXPORT_SYMBOL(seq_release);
 EXPORT_SYMBOL(seq_read);
 EXPORT_SYMBOL(seq_lseek);
+EXPORT_SYMBOL(show_stack);
 
 /* Program loader interfaces */
 EXPORT_SYMBOL(setup_arg_pages);
#1 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Christoph Hellwig

Christoph> This patch adds a prototype for show_stack to sched.h and Christoph> exports it. Andrea's VM updates want this, as does Tux Christoph> and I think it's a facility we want to proive genericly.

Christoph> Note that some architectures (e.g. ia64) have a Christoph> conflicting prototype and some (e.g. sparc) don't have Christoph> show_stack at all, but I think -pre5 is early enough in Christoph> the 2.4.19 cycle to let the maintainers fix it.

Christoph> Could you apply the patch to your tree?

Please don't. The patch is broken.

Christoph, why do you think the prototype for ia64 is different? It's because it *has to be*. In general, you can't do a backtrace without having the full (preserved) state of the CPU at the point of which the backtrace begins.

	--david
#2 lore raw
In reply to Christoph Hellwig
Quoted 1 line · David Mosberger

Christoph, why do you think the prototype for ia64 is different?

I have stopped to wonder why ia64 does things differently.

Quoted 4 lines

It's
because it *has to be*. In general, you can't do a backtrace without having the full (preserved) state of the CPU at the point of which the backtrace begins.

So your suggestion is to move the other architectures to the ia64 prototype or to not have an architecture-independand stack-traceback facility at all?

Christoph

#3 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Christoph Hellwig

Christoph> On Fri, Mar 29, 2002 at 07:46:07AM -0800, David Mosberger

Quoted 1 line · Christoph> wrote:
Quoted 1 line

Christoph, why do you think the prototype for ia64 is different?

Christoph> I have stopped to wonder why ia64 does things Christoph> differently.

You might want to reconsider that stance. It could open your mind. ;-)

BTW: this is not at all an ia64-specific issue. It applies to any arch that doesn't maintain a frame pointer on the stack. Basic compiler technology.

Quoted 3 lines
Quoted 3 lines

It's because it *has to be*. In general, you can't do a backtrace without having the full (preserved) state of the CPU at the point of which the backtrace begins.

Christoph> So your suggestion is to move the other architectures to Christoph> the ia64 prototype or to not have an Christoph> architecture-independand stack-traceback facility at all?

I haven't done such an investigation. I believe the ia64 prototype is reasonable and probably implementable on all platforms that can unwind the stack in the first place, but before making a change to the stable kernel API, I'd think someone would have to investigate this a bit further.

One issue to consider is whether it's safe to call the routine on a task that is running on another processor. The ia64 implementation can handle this gracefully, because it's sometimes better to print a meaningless (garbled) stack trace than to make provably certain that the task won't be running on any other CPU. A related question is whether the routine can be called with interrupts masked.

My sense is this is something that should be considered for 2.5 first.

	--david
#4 lore raw
Quoted 6 lines · In article <[email protected]> you wrote:
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Christoph Hellwig

Christoph> On Fri, Mar 29, 2002 at 07:46:07AM -0800, David Mosberger

Quoted 1 line · Christoph> wrote:
Quoted 1 line

Christoph, why do you think the prototype for ia64 is different?

Quoted 8 lines

Christoph> I have stopped to wonder why ia64 does things Christoph> differently.

You might want to reconsider that stance. It could open your mind. ;-)

BTW: this is not at all an ia64-specific issue. It applies to any arch that doesn't maintain a frame pointer on the stack. Basic compiler technology.

oh you mean like x86 ?

#5 lore raw
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line · On Fri, 29 Mar 2002 17:16:23 GMT, [email protected] said:

BTW: this is not at all an ia64-specific issue. It applies to any arch that doesn't maintain a frame pointer on the stack. Basic compiler technology.

Arjan> oh you mean like x86 ?

As far as I know, the x86 version of show_trace() still relies on the fact that (a) return addresses are stored on the memory stack, (b) they are stored in the order in which the routines were called, and
(c) that there aren't too many other values on the stack that look like kernel text addresses. As long as an x86 compiler uses the CALL instruction, that should be the case. But this certainly isn't the case for all architectures (though I do agree that it is a heuristic should work reasonably well for several architectures).

	--david
#6 lore raw

[email protected] (David Mosberger) wrote on 29.03.02 in <[email protected]>:

Quoted 6 lines

As far as I know, the x86 version of show_trace() still relies on the fact that (a) return addresses are stored on the memory stack, (b) they are stored in the order in which the routines were called, and
(c) that there aren't too many other values on the stack that look like kernel text addresses. As long as an x86 compiler uses the CALL instruction, that should be the case.

(b) is certainly not necessarily true for architectures like PPC, but the rest seems fairly unobjectionable - assuming that you first flush out your registers so any addresses only in registers get put on the stack, too. At least outside of Forth, separate return stacks seem to be extremely rare.

And as for the possibility of using several registers for return addresses such that dumping them leaves them on the stack out of order, well, the only thing I can see to get the original order back is to consult debug information that says where, on every call, to look for the next-up return address. Which doesn't seem quite feasible with anything less than a full gdb to do it.

What I don't see is what connection this can possibly have to the prototype of a stack dump routine.

MfG Kai

#7 lore raw
In reply to Christoph Hellwig
Quoted 6 lines · David Mosberger

..
Christoph> So your suggestion is to move the other architectures to Christoph> the ia64 prototype or to not have an Christoph> architecture-independand stack-traceback facility at all?

Quoted 6 lines

I haven't done such an investigation. I believe the ia64 prototype is reasonable and probably implementable on all platforms that can unwind the stack in the first place, but before making a change to the stable kernel API, I'd think someone would have to investigate this a bit further.

The way I ended up resolving these sorts of issues was to make the generic function

	void dump_stack(void);

under the (hopefully valid) assumption that all architectures can somehow, in some manner, manage to spit out something useful.

For a transitional/compatibility think, there's lib/dump_stack.c which just prints "I'm broken".

Here's the diff. Comments?

include/linux/kernel.h renamed from include/linux/kernel.h~aa-094-dump_stack +2 -0
@@ -106,6 +106,8 @@ extern int oops_in_progress;		/* If set,
 extern int tainted;
 extern const char *print_tainted(void);
 
+extern void dump_stack(void);
+
 #if DEBUG
 #define pr_debug(fmt,arg...) \
 	printk(KERN_DEBUG fmt,##arg)
lib/dump_stack.c new file +13 -0
@@ -0,0 +1,13 @@
+/*
+ * Provide a default dump_stack() function for architectures
+ * which don't implement their own.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+
+void dump_stack(void)
+{
+	printk(KERN_NOTICE
+		"This architecture does not implement dump_stack()\n");
+}
lib/Makefile renamed from lib/Makefile~aa-094-dump_stack +2 -1
@@ -10,7 +10,8 @@ L_TARGET := lib.a
 
 export-objs := cmdline.o dec_and_lock.o rwsem-spinlock.o rwsem.o rbtree.o
 
-obj-y := errno.o ctype.o string.o vsprintf.o brlock.o cmdline.o bust_spinlocks.o rbtree.o
+obj-y := errno.o ctype.o string.o vsprintf.o brlock.o cmdline.o \
+		bust_spinlocks.o rbtree.o dump_stack.o
 
 obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
 obj-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
kernel/ksyms.c renamed from kernel/ksyms.c~aa-094-dump_stack +3 -0
@@ -560,3 +560,6 @@ EXPORT_SYMBOL(init_task_union);
 
 EXPORT_SYMBOL(tasklist_lock);
 EXPORT_SYMBOL(pidhash);
+
+/* debug */
+EXPORT_SYMBOL(dump_stack);
arch/i386/kernel/traps.c renamed from arch/i386/kernel/traps.c~aa-094-dump_stack +8 -0
@@ -186,6 +186,14 @@ void show_stack(unsigned long * esp)
 	show_trace(esp);
 }
 
+/*
+ * The architecture-independent backtrace generator
+ */
+void dump_stack(void)
+{
+	show_stack(0);
+}
+
 void show_registers(struct pt_regs *regs)
 {
 	int i;
#8 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Andrew Morton

Andrew> The way I ended up resolving these sorts of issues was to Andrew> make the generic function

  Andrew> 	void dump_stack(void);

Andrew> under the (hopefully valid) assumption that all Andrew> architectures can somehow, in some manner, manage to spit Andrew> out something useful.

Andrew> For a transitional/compatibility think, there's Andrew> lib/dump_stack.c which just prints "I'm broken".

Andrew> Here's the diff. Comments?

Looks good to me.

Thanks,

	--david
#9 lore raw
In reply to Christoph Hellwig
Quoted 1 line · Andrew Morton

Here's the diff. Comments?

I don't see who having to independand declaration in the same kernel image are supposed to work..

I think you really want some HAVE_ARCH_SHOW_STACK define to disable the generic version..

Christoph

#10 lore raw
In reply to Christoph Hellwig
Quoted 6 lines · Christoph Hellwig
Quoted 1 line · Andrew Morton

Here's the diff. Comments?

I don't see who having to independand declaration in the same kernel image are supposed to work..

It goes in lib/lib.a. The linker will only pick up the default version if the architecture doesn't have its own dump_stack().

bust_spinlocks() has worked that way for quite some time.

Quoted 2 lines

I think you really want some HAVE_ARCH_SHOW_STACK define to disable the generic version..

Yup. But it's nice to be able to slot the default version
into lib.a. In the bust_spinlocks case, architectures only
need to implement a version if they have special needs.

-

#11 lore raw
Quoted 5 lines · Andrew Morton
Quoted 4 lines · Christoph Hellwig
Quoted 1 line · Andrew Morton

Here's the diff. Comments?

Quoted 8 lines
Quoted 2 lines

I don't see who having to independand declaration in the same kernel image are supposed to work..

It goes in lib/lib.a. The linker will only pick up the default version if the architecture doesn't have its own dump_stack().

bust_spinlocks() has worked that way for quite some time.

I have a problem with putting routines in lib.a and relying on the linker to pull them out by default. It does not work for routines called from modules, modules do not include lib.a. Remember the recent problems with crc32.o?

bust_spinlocks() is not an issue because it is only called from built in code. show_stack() has been used as a debugging facility and it could be called from a module.

#12 lore raw
Quoted 17 lines · Keith Owens
Quoted 5 lines · Andrew Morton
Quoted 4 lines · Christoph Hellwig
Quoted 1 line · Andrew Morton

Here's the diff. Comments?

Quoted 8 lines
Quoted 2 lines

I don't see who having to independand declaration in the same kernel image are supposed to work..

It goes in lib/lib.a. The linker will only pick up the default version if the architecture doesn't have its own dump_stack().

bust_spinlocks() has worked that way for quite some time.

Quoted 8 lines

I have a problem with putting routines in lib.a and relying on the linker to pull them out by default. It does not work for routines called from modules, modules do not include lib.a. Remember the recent problems with crc32.o?

bust_spinlocks() is not an issue because it is only called from built in code. show_stack() has been used as a debugging facility and it could be called from a module.

Yes, that's a good point. We're safe as long as core kernel always contains a call to dump_stack(). Which is the case, but that's a bit subtle for general usage.