Skip to contentSkip to conversation

xtensa: improve stack dumping

PATCH Superseded Max Filippov 4 participants 8 messages Nov 6, 2019 – Nov 7, 2019 Tensilica Xtensa port (xtensa)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Max Filippov

author maintainer
#0 lore raw

PATCH 1/1 xtensa: improve stack dumping

Collect whole stack dump lines in a buffer and print the whole buffer when it's ready with pr_info instead of pr_cont. This makes stack dump lines consistent in SMP case and relies less on pr_cont/printk differences related to timestamps.
Make size of stack dump configurable.
Drop extra newline output in show_trace as its output format does not depend on CONFIG_KALLSYMS.

---
 arch/xtensa/Kconfig.debug  |  7 +++++++
 arch/xtensa/kernel/traps.c | 12 ++++++------
 2 files changed, 13 insertions(+), 6 deletions(-)
arch/xtensa/Kconfig.debug +7 -0
index 39de98e20018..83cc8d12fa0e 100644
@@ -31,3 +31,10 @@ config S32C1I_SELFTEST
 	  It is easy to make wrong hardware configuration, this test should catch it early.
 
 	  Say 'N' on stable hardware.
+
+config PRINT_STACK_DEPTH
+	int "Stack depth to print" if DEBUG_KERNEL
+	default 64
+	help
+	  This option allows you to set the stack depth that the kernel
+	  prints in stack traces.
arch/xtensa/kernel/traps.c +6 -6
index 4a6c495ce9b6..b0c952652c73 100644
@@ -491,17 +491,15 @@ void show_trace(struct task_struct *task, unsigned long *sp)
 
 	pr_info("Call Trace:\n");
 	walk_stackframe(sp, show_trace_cb, NULL);
-#ifndef CONFIG_KALLSYMS
-	pr_cont("\n");
-#endif
 }
 
-static int kstack_depth_to_print = 24;
+static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
 
 void show_stack(struct task_struct *task, unsigned long *sp)
 {
 	int i = 0;
 	unsigned long *stack;
+	char buf[9 * 8 + 1];
 
 	if (!sp)
 		sp = stack_pointer(task);
@@ -512,10 +510,12 @@ void show_stack(struct task_struct *task, unsigned long *sp)
 	for (i = 0; i < kstack_depth_to_print; i++) {
 		if (kstack_end(sp))
 			break;
-		pr_cont(" %08lx", *sp++);
+		sprintf(buf + (i % 8) * 9, " %08lx", *sp++);
 		if (i % 8 == 7)
-			pr_cont("\n");
+			pr_info("%s\n", buf);
 	}
+	if (i % 8)
+		pr_info("%s\n", buf);
 	show_trace(task, stack);
 }
 
Signature · 1 line
2.20.1
#1 lore raw

Hi Max,

Quoted 9 lines · Max Filippov

Collect whole stack dump lines in a buffer and print the whole buffer when it's ready with pr_info instead of pr_cont. This makes stack dump lines consistent in SMP case and relies less on pr_cont/printk differences related to timestamps.
Make size of stack dump configurable.
Drop extra newline output in show_trace as its output format does not depend on CONFIG_KALLSYMS.

[..]

Quoted 9 lines

void show_stack(struct task_struct *task, unsigned long *sp) {

arch/xtensa/kernel/traps.c +1 -0
 	int i = 0;
 	unsigned long *stack;
+	char buf[9 * 8 + 1];
 
 	if (!sp)
 		sp = stack_pointer(task);

arch/xtensa/kernel/traps.c void show_stack(struct task_struct *task, unsigned long *sp) · line 513

Quoted 5 lines
arch/xtensa/kernel/traps.c +1 -1
@@ -512,10 +510,12 @@ void show_stack(struct task_struct *task, unsigned long *sp)
 	for (i = 0; i < kstack_depth_to_print; i++) {
 		if (kstack_end(sp))
 			break;
-		pr_cont(" %08lx", *sp++);
+		sprintf(buf + (i % 8) * 9, " %08lx", *sp++);

buf is on the stack, does sprintf() put null-terminator for hex?

arch/xtensa/kernel/traps.c void show_stack(struct task_struct *task, unsigned long *sp) · line 518

Quoted 6 lines
arch/xtensa/kernel/traps.c +3 -1
 		if (i % 8 == 7)
-			pr_cont("\n");
+			pr_info("%s\n", buf);
 	}
+	if (i % 8)
+		pr_info("%s\n", buf);

If the stack trace ends with (i % 8 == 7), you'll double-print the last line?

Thanks,
Dmitry

author maintainer
#2 lore raw
Quoted 9 lines · Dmitry Safonov

arch/xtensa/kernel/traps.c void show_stack(struct task_struct *task, unsigned long *sp) · line 513

Quoted 6 lines · Max Filippov
arch/xtensa/kernel/traps.c +1 -1
@@ -512,10 +510,12 @@ void show_stack(struct task_struct *task, unsigned long *sp)
      for (i = 0; i < kstack_depth_to_print; i++) {
              if (kstack_end(sp))
                      break;
-             pr_cont(" %08lx", *sp++);
+             sprintf(buf + (i % 8) * 9, " %08lx", *sp++);

buf is on the stack, does sprintf() put null-terminator for hex?

It should put null-terminator regardless of the format string.

Quoted 9 lines

arch/xtensa/kernel/traps.c void show_stack(struct task_struct *task, unsigned long *sp) · line 518

Quoted 6 lines
arch/xtensa/kernel/traps.c +3 -1
              if (i % 8 == 7)
-                     pr_cont("\n");
+                     pr_info("%s\n", buf);
      }
+     if (i % 8)
+             pr_info("%s\n", buf);

If the stack trace ends with (i % 8 == 7), you'll double-print the last line?

No, I don't think so. 'For' loop condition is checked after i++, so if loop ends with i % 8 == 7 then its last iteration was done with i % 8 == 6 and thus the buf haven't been printed.

Signature · 2 lines
Thanks.
-- Max
#3 lore raw
Quoted 11 lines · Max Filippov
Quoted 9 lines · Dmitry Safonov

arch/xtensa/kernel/traps.c void show_stack(struct task_struct *task, unsigned long *sp) · line 513

Quoted 6 lines · Max Filippov
arch/xtensa/kernel/traps.c +1 -1
@@ -512,10 +510,12 @@ void show_stack(struct task_struct *task, unsigned long *sp)
      for (i = 0; i < kstack_depth_to_print; i++) {
              if (kstack_end(sp))
                      break;
-             pr_cont(" %08lx", *sp++);
+             sprintf(buf + (i % 8) * 9, " %08lx", *sp++);

buf is on the stack, does sprintf() put null-terminator for hex?

Quoted 1 line

It should put null-terminator regardless of the format string.

Always unsure what to expect from printf() :)

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
arch/xtensa/kernel/traps.c +3 -1
              if (i % 8 == 7)
-                     pr_cont("\n");
+                     pr_info("%s\n", buf);
      }
+     if (i % 8)
+             pr_info("%s\n", buf);
Quoted 6 lines
Quoted 2 lines

If the stack trace ends with (i % 8 == 7), you'll double-print the last line?

No, I don't think so. 'For' loop condition is checked after i++, so if loop ends with i % 8 == 7 then its last iteration was done with i % 8 == 6 and thus the buf haven't been printed.

Ah, right - me being stupid.

Thanks,
Dmitry

#4 lore raw
In reply to Max Filippov
Quoted 7 lines · Max Filippov

Collect whole stack dump lines in a buffer and print the whole buffer when it's ready with pr_info instead of pr_cont. This makes stack dump lines consistent in SMP case and relies less on pr_cont/printk differences related to timestamps.
Make size of stack dump configurable.
Drop extra newline output in show_trace as its output format does not depend on CONFIG_KALLSYMS.

[]

arch/xtensa/kernel/traps.c

Quoted 1 line
arch/xtensa/kernel/traps.c +0 -0

[]

arch/xtensa/kernel/traps.c void show_stack(struct task_struct *task, unsigned long *sp) · line 518

Quoted 12 lines
arch/xtensa/kernel/traps.c +4 -2
@@ -512,10 +510,12 @@ void show_stack(struct task_struct *task, unsigned long *sp)
 	for (i = 0; i < kstack_depth_to_print; i++) {
 		if (kstack_end(sp))
 			break;
-		pr_cont(" %08lx", *sp++);
+		sprintf(buf + (i % 8) * 9, " %08lx", *sp++);
 		if (i % 8 == 7)
-			pr_cont("\n");
+			pr_info("%s\n", buf);
 	}
+	if (i % 8)
+		pr_info("%s\n", buf);

Could this be done using hex_dump_to_buffer
by precalculating kstack_end ?

author maintainer
#5 lore raw
Quoted 7 lines · Joe Perches
Quoted 7 lines
arch/xtensa/kernel/traps.c +1 -1
@@ -512,10 +510,12 @@ void show_stack(struct task_struct *task, unsigned long *sp)
      for (i = 0; i < kstack_depth_to_print; i++) {
              if (kstack_end(sp))
                      break;
-             pr_cont(" %08lx", *sp++);
+             sprintf(buf + (i % 8) * 9, " %08lx", *sp++);
              if (i % 8 == 7)
Quoted 8 lines

arch/xtensa/kernel/traps.c void show_stack(struct task_struct *task, unsigned long *sp) · line 518

Quoted 5 lines
arch/xtensa/kernel/traps.c +3 -1
-                     pr_cont("\n");
+                     pr_info("%s\n", buf);
      }
+     if (i % 8)
+             pr_info("%s\n", buf);

Could this be done using hex_dump_to_buffer
by precalculating kstack_end ?

I've got this, but it doesn't look very attractive to me:

void show_stack(struct task_struct *task, unsigned long *sp)

{
        unsigned long *stack;
        int len;

        if (!sp)
                sp = stack_pointer(task);
        stack = sp;

        len = min((-(unsigned long)stack) & (THREAD_SIZE - 4),
                  kstack_depth_to_print * 4ul);

        pr_info("Stack:\n");

        for (; len > 0; len -= 32) {
                char buf[9 * 8 + 1];

                hex_dump_to_buffer(sp, min(len, 32), 32, 4,
                                   buf, sizeof(buf), false);
                pr_info(" %08lx:  %s\n", (unsigned long)sp, buf);
                sp += 8;
        }
        show_trace(task, stack);
}
Signature · 2 lines
Thanks.
-- Max
#6 lore raw
Quoted 33 lines · Max Filippov
Quoted 7 lines · Joe Perches
Quoted 7 lines
arch/xtensa/kernel/traps.c +1 -1
@@ -512,10 +510,12 @@ void show_stack(struct task_struct *task, unsigned long *sp)
      for (i = 0; i < kstack_depth_to_print; i++) {
              if (kstack_end(sp))
                      break;
-             pr_cont(" %08lx", *sp++);
+             sprintf(buf + (i % 8) * 9, " %08lx", *sp++);
              if (i % 8 == 7)
Quoted 8 lines

arch/xtensa/kernel/traps.c void show_stack(struct task_struct *task, unsigned long *sp) · line 518

Quoted 5 lines
arch/xtensa/kernel/traps.c +3 -1
-                     pr_cont("\n");
+                     pr_info("%s\n", buf);
      }
+     if (i % 8)
+             pr_info("%s\n", buf);

Could this be done using hex_dump_to_buffer
by precalculating kstack_end ?

I've got this, but it doesn't look very attractive to me:

void show_stack(struct task_struct *task, unsigned long *sp)

{
        unsigned long *stack;
        int len;

        if (!sp)
                sp = stack_pointer(task);
        stack = sp;

        len = min((-(unsigned long)stack) & (THREAD_SIZE - 4),
                  kstack_depth_to_print * 4ul);

        pr_info("Stack:\n");
Quoted 8 lines
        for (; len > 0; len -= 32) {
                char buf[9 * 8 + 1];

                hex_dump_to_buffer(sp, min(len, 32), 32, 4,
                                   buf, sizeof(buf), false);
                pr_info(" %08lx:  %s\n", (unsigned long)sp, buf);
                sp += 8;
        }

I wonder if the cycle actually could get replaced by a single call:

	print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
		       16, 1, sp, len, false);

print_hex_dump() currently does not allow to print 8 bytes per-line. Either 16 is acceptable or hex_dump() function could be updated.

Best Regards,
Petr

#7 lore raw
Quoted 43 lines · Petr Mladek
Quoted 33 lines · Max Filippov
Quoted 7 lines · Joe Perches
Quoted 7 lines
arch/xtensa/kernel/traps.c +1 -1
@@ -512,10 +510,12 @@ void show_stack(struct task_struct *task, unsigned long *sp)
      for (i = 0; i < kstack_depth_to_print; i++) {
              if (kstack_end(sp))
                      break;
-             pr_cont(" %08lx", *sp++);
+             sprintf(buf + (i % 8) * 9, " %08lx", *sp++);
              if (i % 8 == 7)
Quoted 8 lines

arch/xtensa/kernel/traps.c void show_stack(struct task_struct *task, unsigned long *sp) · line 518

Quoted 5 lines
arch/xtensa/kernel/traps.c +3 -1
-                     pr_cont("\n");
+                     pr_info("%s\n", buf);
      }
+     if (i % 8)
+             pr_info("%s\n", buf);

Could this be done using hex_dump_to_buffer
by precalculating kstack_end ?

I've got this, but it doesn't look very attractive to me:

void show_stack(struct task_struct *task, unsigned long *sp)

{
        unsigned long *stack;
        int len;

        if (!sp)
                sp = stack_pointer(task);
        stack = sp;

        len = min((-(unsigned long)stack) & (THREAD_SIZE - 4),
                  kstack_depth_to_print * 4ul);

        pr_info("Stack:\n");
Quoted 8 lines
        for (; len > 0; len -= 32) {
                char buf[9 * 8 + 1];

                hex_dump_to_buffer(sp, min(len, 32), 32, 4,
                                   buf, sizeof(buf), false);
                pr_info(" %08lx:  %s\n", (unsigned long)sp, buf);
                sp += 8;
        }
Quoted 4 lines

I wonder if the cycle actually could get replaced by a single call:

	print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
		       16, 1, sp, len, false);

I think it could be using 4 and not 1 to keep the same output of a u32 instead of spaces between bytes.