diff mbox

[kvm-unit-tests,2/5] lib: backtrace printing

Message ID 1456867658-10937-3-git-send-email-pfeiner@google.com (mailing list archive)
State New, archived
Headers show

Commit Message

Peter Feiner March 1, 2016, 9:27 p.m. UTC
Functions to walk stack and print backtrace. The stack's unadorned as

	STACK: addr addr addr ...

A follow-up patch post-processes the output to pretty-print the stack.

Stack walker is just a stub on arm and ppc.

Signed-off-by: Peter Feiner <pfeiner@google.com>
---
 Makefile                 |  2 +-
 arm/Makefile.common      |  1 +
 lib/arm/dump_stack.c     |  6 ++++++
 lib/libcflat.h           |  4 ++++
 lib/powerpc/dump_stack.c |  6 ++++++
 lib/printf.c             | 37 +++++++++++++++++++++++++++++++++++++
 lib/x86/dump_stack.c     | 24 ++++++++++++++++++++++++
 powerpc/Makefile.common  |  1 +
 x86/Makefile.common      |  1 +
 9 files changed, 81 insertions(+), 1 deletion(-)
 create mode 100644 lib/arm/dump_stack.c
 create mode 100644 lib/powerpc/dump_stack.c
 create mode 100644 lib/x86/dump_stack.c

Comments

Peter Feiner March 1, 2016, 10:58 p.m. UTC | #1
On Tue, Mar 1, 2016 at 1:27 PM, Peter Feiner <pfeiner@google.com> wrote:
> -CFLAGS += $(call cc-option, -fomit-frame-pointer, "")
> +CFLAGS += $(call cc-option, -fno-omit-frame-pointer, "")

Please hold off on this series for now. -fno-omit-frame-pointer seems
to break x86/emulator.flat. I'll look into it and reply when I've
figured things out.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Peter Feiner March 1, 2016, 11:07 p.m. UTC | #2
On Tue, Mar 1, 2016 at 2:58 PM, Peter Feiner <pfeiner@google.com> wrote:
> On Tue, Mar 1, 2016 at 1:27 PM, Peter Feiner <pfeiner@google.com> wrote:
>> -CFLAGS += $(call cc-option, -fomit-frame-pointer, "")
>> +CFLAGS += $(call cc-option, -fno-omit-frame-pointer, "")
>
> Please hold off on this series for now. -fno-omit-frame-pointer seems
> to break x86/emulator.flat. I'll look into it and reply when I've
> figured things out.

Apparently x86/emulator.flat is very fickle:

   -O0 causes #PF
   -fno-omit-frame-pointer causes triple fault
   -fno-omit-frame-pointer and -O0 causes #GP

Oye!
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/Makefile b/Makefile
index ddba941..0ffa5e7 100644
--- a/Makefile
+++ b/Makefile
@@ -42,7 +42,7 @@  cc-option = $(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null \
 
 CFLAGS += -g
 CFLAGS += $(autodepend-flags) -Wall
-CFLAGS += $(call cc-option, -fomit-frame-pointer, "")
+CFLAGS += $(call cc-option, -fno-omit-frame-pointer, "")
 CFLAGS += $(call cc-option, -fno-stack-protector, "")
 CFLAGS += $(call cc-option, -fno-stack-protector-all, "")
 
diff --git a/arm/Makefile.common b/arm/Makefile.common
index dd3a0ca..054bdee 100644
--- a/arm/Makefile.common
+++ b/arm/Makefile.common
@@ -39,6 +39,7 @@  cflatobjs += lib/arm/mmu.o
 cflatobjs += lib/arm/bitops.o
 cflatobjs += lib/arm/psci.o
 cflatobjs += lib/arm/smp.o
+cflatobjs += lib/arm/dump_stack.o
 
 libeabi = lib/arm/libeabi.a
 eabiobjs = lib/arm/eabi_compat.o
diff --git a/lib/arm/dump_stack.c b/lib/arm/dump_stack.c
new file mode 100644
index 0000000..528ba63
--- /dev/null
+++ b/lib/arm/dump_stack.c
@@ -0,0 +1,6 @@ 
+#include "libcflat.h"
+
+int walk_stack(unsigned long bp, unsigned long *stack, int max_depth)
+{
+	return 0;
+}
diff --git a/lib/libcflat.h b/lib/libcflat.h
index 1f0049c..42c94df 100644
--- a/lib/libcflat.h
+++ b/lib/libcflat.h
@@ -65,6 +65,10 @@  extern void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...);
 extern void report_abort(const char *msg_fmt, ...);
 extern int report_summary(void);
 
+int walk_stack(unsigned long bp, unsigned long *stack, int max_depth);
+void dump_stack(unsigned long ip, unsigned long bp);
+void dump_current_stack(void);
+
 #define ARRAY_SIZE(_a) (sizeof(_a)/sizeof((_a)[0]))
 
 #define container_of(ptr, type, member) ({				\
diff --git a/lib/powerpc/dump_stack.c b/lib/powerpc/dump_stack.c
new file mode 100644
index 0000000..528ba63
--- /dev/null
+++ b/lib/powerpc/dump_stack.c
@@ -0,0 +1,6 @@ 
+#include "libcflat.h"
+
+int walk_stack(unsigned long bp, unsigned long *stack, int max_depth)
+{
+	return 0;
+}
diff --git a/lib/printf.c b/lib/printf.c
index 2aec59a..e97fca9 100644
--- a/lib/printf.c
+++ b/lib/printf.c
@@ -259,3 +259,40 @@  int printf(const char *fmt, ...)
     puts(buf);
     return r;
 }
+
+static void print_stack(unsigned long *stack, int depth,
+			bool top_is_return_address)
+{
+	int i;
+
+	printf("\tSTACK: " );
+	for (i = 0; i < depth; i++) {
+		int offset = -1;
+		if (i == 0 && !top_is_return_address)
+			offset = 0;
+		printf(" %lx", stack[i] + offset);
+	}
+	printf("\n");
+}
+
+#define MAX_DEPTH 10
+
+void dump_stack(unsigned long ip, unsigned long bp)
+{
+	unsigned long stack[MAX_DEPTH];
+	int depth;
+
+	stack[0] = ip;
+	depth = walk_stack(bp, &stack[1], MAX_DEPTH - 1);
+	print_stack(stack, depth + 1, false);
+}
+
+void dump_current_stack(void)
+{
+	unsigned long stack[MAX_DEPTH];
+	int depth;
+
+	depth = walk_stack((unsigned long)__builtin_frame_address(1), stack,
+			   MAX_DEPTH);
+	print_stack(stack, depth, true);
+}
diff --git a/lib/x86/dump_stack.c b/lib/x86/dump_stack.c
new file mode 100644
index 0000000..6e9d126
--- /dev/null
+++ b/lib/x86/dump_stack.c
@@ -0,0 +1,24 @@ 
+#include "libcflat.h"
+
+int walk_stack(unsigned long bp, unsigned long *stack, int max_depth)
+{
+	static int walking;
+	int depth = 0;
+	unsigned long *frame = (unsigned long *) bp;
+
+	if (walking) {
+		printf("RECURSIVE STACK WALK!!!\n");
+		return 0;
+	}
+	walking = 1;
+
+	for (depth = 0; depth < max_depth; depth++) {
+		stack[depth] = frame[1];
+		if (stack[depth] == 0)
+			break;
+		frame = (unsigned long *) frame[0];
+	}
+
+	walking = 0;
+	return depth;
+}
diff --git a/powerpc/Makefile.common b/powerpc/Makefile.common
index cc27ac8..97f5ddc 100644
--- a/powerpc/Makefile.common
+++ b/powerpc/Makefile.common
@@ -30,6 +30,7 @@  cflatobjs += lib/powerpc/io.o
 cflatobjs += lib/powerpc/hcall.o
 cflatobjs += lib/powerpc/setup.o
 cflatobjs += lib/powerpc/rtas.o
+cflatobjs += lib/powerpc/dump_stack.o
 
 FLATLIBS = $(libcflat) $(LIBFDT_archive)
 %.elf: LDFLAGS = $(arch_LDFLAGS) -nostdlib -pie
diff --git a/x86/Makefile.common b/x86/Makefile.common
index 3a14fea..250d95d 100644
--- a/x86/Makefile.common
+++ b/x86/Makefile.common
@@ -12,6 +12,7 @@  cflatobjs += lib/x86/atomic.o
 cflatobjs += lib/x86/desc.o
 cflatobjs += lib/x86/isr.o
 cflatobjs += lib/x86/acpi.o
+cflatobjs += lib/x86/dump_stack.o
 
 $(libcflat): LDFLAGS += -nostdlib
 $(libcflat): CFLAGS += -ffreestanding -I lib