From patchwork Thu Mar 3 20:48:33 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Feiner X-Patchwork-Id: 8496101 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id CEF289F8A8 for ; Thu, 3 Mar 2016 20:48:48 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8FD352012E for ; Thu, 3 Mar 2016 20:48:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2CE5E2034E for ; Thu, 3 Mar 2016 20:48:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756905AbcCCUsn (ORCPT ); Thu, 3 Mar 2016 15:48:43 -0500 Received: from mail-pa0-f41.google.com ([209.85.220.41]:36752 "EHLO mail-pa0-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751938AbcCCUsm (ORCPT ); Thu, 3 Mar 2016 15:48:42 -0500 Received: by mail-pa0-f41.google.com with SMTP id fi3so19116289pac.3 for ; Thu, 03 Mar 2016 12:48:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=eo8BsME3Dtxa27ddVSzGZnaSPwDgk6+hybuQra1U9eo=; b=lvCsGLaPhz88DfvZHXxToUAVJ4QElRjOeyuIALQggkLb0QfCcPLsJl2d6kPBfnufvd FBjSSE+BAtH5PCs532IT+xqTbblPCTkmTQjbgiHFe2zuvPrD2P6jouUcN5+fpqHDHcWb ZtBSWy3iDCEH6PFJAWeNQzt70ixz4k7YN+Wl7eKpvSWI6HJXEQnvn6jtbVra/h2S9eyA Y5bwBnwOsHSo4GjNH8fRuMXrqRSapdmCO/+ws0bQ65OZoJN1fQYCiWIlwtiwP579tYTl pGD6ZkYldFxtKPz2mU2PmbD8UoWvyasTInJ9w7SU+BSEWZ9PJPTeY1ytoLSLsdhyQswn 0hBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=eo8BsME3Dtxa27ddVSzGZnaSPwDgk6+hybuQra1U9eo=; b=cGV0ru4SbGhlQfzpEA3kBQUclqkEkRfSi2vRmtqOblgrULJRRrHNJaOATveRYKtrMA hNWsgygK4Rq0g6Q5fQZ9eAUuf3GTT2nBk5Olhff75stDR6MCTf7OW/UdEwF2M87b1ic0 UmG7ctc7tRGRQh9ni9FDDs4SMpWMsSDbiZdQElcxShTjwjBmDuonbgBkZvw5J3AviEOZ p5U9PZ8ROxm/bgmM/CHsQpdi8fQY1h0qMZd5hQz0Mj0h4BYIKr0qYrQ58PQNGXkk8QhT yZHP5rYHi3EYm2ExnHSA9wRfBbjezoZXAPYPN/2PrciXhiz0hH2KTdZEfRzP1x3u+Owv CAeg== X-Gm-Message-State: AD7BkJKS8d/5m0600fuuSsWtjLBUZEJHGcZT1shKNU5fRlNhjfTyw9Xz1OGIFNGNysRf4UAg X-Received: by 10.66.157.161 with SMTP id wn1mr6635825pab.146.1457038121820; Thu, 03 Mar 2016 12:48:41 -0800 (PST) Received: from localhost ([2620:0:1009:3:48a8:c9fe:5013:71d]) by smtp.gmail.com with ESMTPSA id 1sm232045pfm.10.2016.03.03.12.48.40 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Thu, 03 Mar 2016 12:48:40 -0800 (PST) From: Peter Feiner To: kvm@vger.kernel.org, drjones@redhat.com, pbonzini@redhat.com Cc: pfeiner@google.com Subject: [kvm-unit-tests v3 1/4] lib: backtrace printing Date: Thu, 3 Mar 2016 12:48:33 -0800 Message-Id: <1457038116-3448-2-git-send-email-pfeiner@google.com> X-Mailer: git-send-email 2.7.0.rc3.207.g0ac5344 In-Reply-To: <1457038116-3448-1-git-send-email-pfeiner@google.com> References: <1456867658-10937-1-git-send-email-pfeiner@google.com> <1457038116-3448-1-git-send-email-pfeiner@google.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Functions to walk stack and print backtrace. The stack's unadorned as STACK: [@]addr addr addr ... where the optional @ indicates that addr isn't a return address. A follow-up patch post-processes the output to pretty-print the stack. Frame stack walker is just a stub on arm and ppc. Signed-off-by: Peter Feiner --- Makefile | 3 ++- arm/Makefile.common | 1 + lib/arm/asm/stack.h | 1 + lib/arm64/asm/stack.h | 1 + lib/asm-generic/stack.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/asm-generic/stack.h | 14 +++++++++++++ lib/libcflat.h | 3 +++ lib/powerpc/asm/stack.h | 1 + lib/ppc64/asm/stack.h | 1 + lib/printf.c | 42 +++++++++++++++++++++++++++++++++++++++ lib/x86/asm/stack.c | 25 +++++++++++++++++++++++ lib/x86/asm/stack.h | 12 +++++++++++ powerpc/Makefile.common | 1 + x86/Makefile.common | 4 ++++ 14 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 lib/arm/asm/stack.h create mode 100644 lib/arm64/asm/stack.h create mode 100644 lib/asm-generic/stack.c create mode 100644 lib/asm-generic/stack.h create mode 100644 lib/powerpc/asm/stack.h create mode 100644 lib/ppc64/asm/stack.h create mode 100644 lib/x86/asm/stack.c create mode 100644 lib/x86/asm/stack.h diff --git a/Makefile b/Makefile index 72e6711..536f2e6 100644 --- a/Makefile +++ b/Makefile @@ -42,7 +42,8 @@ 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, "") +frame-pointer-flag=-f$(if $(KEEP_FRAME_POINTER),no-,)omit-frame-pointer +CFLAGS += $(call cc-option, $(frame-pointer-flag), "") 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..ede8851 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/asm-generic/stack.o libeabi = lib/arm/libeabi.a eabiobjs = lib/arm/eabi_compat.o diff --git a/lib/arm/asm/stack.h b/lib/arm/asm/stack.h new file mode 100644 index 0000000..f101d03 --- /dev/null +++ b/lib/arm/asm/stack.h @@ -0,0 +1 @@ +#include diff --git a/lib/arm64/asm/stack.h b/lib/arm64/asm/stack.h new file mode 100644 index 0000000..f101d03 --- /dev/null +++ b/lib/arm64/asm/stack.h @@ -0,0 +1 @@ +#include diff --git a/lib/asm-generic/stack.c b/lib/asm-generic/stack.c new file mode 100644 index 0000000..93219cb --- /dev/null +++ b/lib/asm-generic/stack.c @@ -0,0 +1,53 @@ +#include +#include + +int backtrace(const void **return_addrs, int max_depth) +{ + static int walking; + int depth = 0; + void *addr; + + if (walking) { + printf("RECURSIVE STACK WALK!!!\n"); + return 0; + } + walking = 1; + + /* __builtin_return_address requires a compile-time constant argument */ +#define GET_RETURN_ADDRESS(i) \ + if (max_depth == i) \ + goto done; \ + addr = __builtin_return_address(i); \ + if (!addr) \ + goto done; \ + return_addrs[i] = __builtin_extract_return_addr(addr); \ + depth = i + 1; \ + + GET_RETURN_ADDRESS(0) + GET_RETURN_ADDRESS(1) + GET_RETURN_ADDRESS(2) + GET_RETURN_ADDRESS(3) + GET_RETURN_ADDRESS(4) + GET_RETURN_ADDRESS(5) + GET_RETURN_ADDRESS(6) + GET_RETURN_ADDRESS(7) + GET_RETURN_ADDRESS(8) + GET_RETURN_ADDRESS(9) + GET_RETURN_ADDRESS(10) + GET_RETURN_ADDRESS(11) + GET_RETURN_ADDRESS(12) + GET_RETURN_ADDRESS(13) + GET_RETURN_ADDRESS(14) + GET_RETURN_ADDRESS(15) + GET_RETURN_ADDRESS(16) + GET_RETURN_ADDRESS(17) + GET_RETURN_ADDRESS(18) + GET_RETURN_ADDRESS(19) + GET_RETURN_ADDRESS(20) + +#undef GET_RETURN_ADDRESS + +done: + walking = 0; + return depth; +} diff --git a/lib/asm-generic/stack.h b/lib/asm-generic/stack.h new file mode 100644 index 0000000..381fce4 --- /dev/null +++ b/lib/asm-generic/stack.h @@ -0,0 +1,14 @@ +#ifndef _ASMGENERIC_STACK_H_ +#define _ASMGENERIC_STACK_H_ + +static inline int +backtrace_frame(const void *frame __attribute__((unused)), + const void **return_addrs __attribute__((unused)), + int max_depth __attribute__((unused))) +{ + return 0; +} + +int backtrace(const void **return_addrs, int max_depth); + +#endif diff --git a/lib/libcflat.h b/lib/libcflat.h index b58a8a1..55bddca 100644 --- a/lib/libcflat.h +++ b/lib/libcflat.h @@ -81,6 +81,9 @@ 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); +extern void dump_stack(void); +extern void dump_frame_stack(const void *instruction, const void *frame); + #define ARRAY_SIZE(_a) (sizeof(_a)/sizeof((_a)[0])) #define container_of(ptr, type, member) ({ \ diff --git a/lib/powerpc/asm/stack.h b/lib/powerpc/asm/stack.h new file mode 100644 index 0000000..f101d03 --- /dev/null +++ b/lib/powerpc/asm/stack.h @@ -0,0 +1 @@ +#include diff --git a/lib/ppc64/asm/stack.h b/lib/ppc64/asm/stack.h new file mode 100644 index 0000000..f101d03 --- /dev/null +++ b/lib/ppc64/asm/stack.h @@ -0,0 +1 @@ +#include diff --git a/lib/printf.c b/lib/printf.c index 2aec59a..096e175 100644 --- a/lib/printf.c +++ b/lib/printf.c @@ -1,4 +1,5 @@ #include "libcflat.h" +#include "asm/stack.h" #define BUFSZ 2000 @@ -259,3 +260,44 @@ int printf(const char *fmt, ...) puts(buf); return r; } + +static void print_stack(const void **return_addrs, int depth, + bool top_is_return_address) +{ + int i = 0; + + printf("\tSTACK:"); + + /* @addr indicates a non-return address, as expected by the stack + * pretty printer script. */ + if (depth > 0 && !top_is_return_address) { + printf(" @%lx", (unsigned long) return_addrs[0]); + i++; + } + + for (; i < depth; i++) { + printf(" %lx", (unsigned long) return_addrs[i]); + } + printf("\n"); +} + +#define MAX_DEPTH 10 + +void dump_stack(void) +{ + const void *return_addrs[MAX_DEPTH]; + int depth; + + depth = backtrace(return_addrs, MAX_DEPTH); + print_stack(return_addrs, depth, true); +} + +void dump_frame_stack(const void *instruction, const void *frame) +{ + const void *return_addrs[MAX_DEPTH]; + int depth; + + return_addrs[0] = instruction; + depth = backtrace_frame(frame, &return_addrs[1], MAX_DEPTH - 1); + print_stack(return_addrs, depth + 1, false); +} diff --git a/lib/x86/asm/stack.c b/lib/x86/asm/stack.c new file mode 100644 index 0000000..b30b3b1 --- /dev/null +++ b/lib/x86/asm/stack.c @@ -0,0 +1,25 @@ +#include +#include + +int backtrace_frame(const void *frame, const void **return_addrs, int max_depth) +{ + static int walking; + int depth = 0; + const unsigned long *bp = (unsigned long *) frame; + + if (walking) { + printf("RECURSIVE STACK WALK!!!\n"); + return 0; + } + walking = 1; + + for (depth = 0; depth < max_depth; depth++) { + return_addrs[depth] = (void *) bp[1]; + if (return_addrs[depth] == 0) + break; + bp = (unsigned long *) bp[0]; + } + + walking = 0; + return depth; +} diff --git a/lib/x86/asm/stack.h b/lib/x86/asm/stack.h new file mode 100644 index 0000000..c43bb96 --- /dev/null +++ b/lib/x86/asm/stack.h @@ -0,0 +1,12 @@ +#ifndef _X86ASM_STACK_H_ +#define _X86ASM_STACK_H_ + +int backtrace_frame(const void *frame, const void **return_addrs, int max_depth); + +static inline int backtrace(const void **return_addrs, int max_depth) +{ + return backtrace_frame(__builtin_frame_address(0), return_addrs, + max_depth); +} + +#endif diff --git a/powerpc/Makefile.common b/powerpc/Makefile.common index 424983e..ea8f669 100644 --- a/powerpc/Makefile.common +++ b/powerpc/Makefile.common @@ -31,6 +31,7 @@ cflatobjs += lib/powerpc/io.o cflatobjs += lib/powerpc/hcall.o cflatobjs += lib/powerpc/setup.o cflatobjs += lib/powerpc/rtas.o +cflatobjs += lib/asm-generic/stack.o FLATLIBS = $(libcflat) $(LIBFDT_archive) %.elf: CFLAGS += $(arch_CFLAGS) diff --git a/x86/Makefile.common b/x86/Makefile.common index 3a14fea..c4750f0 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/asm/stack.o $(libcflat): LDFLAGS += -nostdlib $(libcflat): CFLAGS += -ffreestanding -I lib @@ -19,6 +20,9 @@ $(libcflat): CFLAGS += -ffreestanding -I lib CFLAGS += -m$(bits) CFLAGS += -O1 +# dump_stack.o relies on frame pointers. +KEEP_FRAME_POINTER := y + libgcc := $(shell $(CC) -m$(bits) --print-libgcc-file-name) FLATLIBS = lib/libcflat.a $(libgcc)