From patchwork Tue Mar 22 23:35:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Feiner X-Patchwork-Id: 8646551 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 D46C89F294 for ; Tue, 22 Mar 2016 23:35:26 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DE50320398 for ; Tue, 22 Mar 2016 23:35:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E777A2039C for ; Tue, 22 Mar 2016 23:35:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752466AbcCVXfW (ORCPT ); Tue, 22 Mar 2016 19:35:22 -0400 Received: from mail-pf0-f172.google.com ([209.85.192.172]:33909 "EHLO mail-pf0-f172.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751370AbcCVXfO (ORCPT ); Tue, 22 Mar 2016 19:35:14 -0400 Received: by mail-pf0-f172.google.com with SMTP id x3so330512967pfb.1 for ; Tue, 22 Mar 2016 16:35:14 -0700 (PDT) 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 :in-reply-to:references; bh=ngnUTKch5PhOB8DBCMUr/ZWfq/cd3Ej88HbHRaxNsMg=; b=nt2Jgy5fkOQa5wdE/ZNJSMcgMBghVE6sqX7Q4mLCEFZNZq/aKDwwMc1gw3TC6CrKLY od4XDuqFYTLJyOj6snKtzyUPfh6C9bhYTt73i0yAaV4V2nWGcWC/VOAZlkzQzlXuvF6J MWYoQN8w5ivPyzTG/3/mPoCBHJ0pOUu9O3jE5SAb0ToDRbsRPiuxrNi0xGMqtTHMZPZN ny3BG4LYHXsqLkAo02RV1mnw+R46v9yLTWXESUDOQ9/+p7F2rRKWBwhSMnGKRkXZMegj osfehTn4oEgmZRNFe1ZBh5C/wEChK+KVrPFi6HJ402DPDlObnim5sPHyRTwkcV0UtGLX MvVA== 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:in-reply-to:references; bh=ngnUTKch5PhOB8DBCMUr/ZWfq/cd3Ej88HbHRaxNsMg=; b=kAo/1P/pBVtT1cfLH511THY6V6+PN37nJ0eBZ/IxJXB0kW+QSeOc84OMDE+bRt1CZI uNSJeu/i2722Xf3knwnb4vbUX7WoNeS7XY5DxqLYJkpwTGmyIt1jwzoRXFnG6wQjlR0g Ri00xbjKRcLZSl7YnUNYdGULuV1cv6KPVawN7B1O5tD10S0o9WiP3p49r+CHQgLPXhm/ uhnLUVD8O0lHVc6s3OeJO5ZBy+RoaDNzpl1U52zdlIT/4+RHlBB4Ryb2qdWFDi4uY1o5 7fGlFke+V41Q0klcOZ01PUYaUf0LB97PPKLIpaqfHW16UZEwNlVGG1dvhfjcz0X8rVpv btNA== X-Gm-Message-State: AD7BkJKqQZkfda1kPIqsI+OQfhraHMZGC9yX2ZQ43ojLIxLKoeu2vcQ3GJg0hC+8t9cE67Vk X-Received: by 10.66.66.42 with SMTP id c10mr29421549pat.119.1458689713521; Tue, 22 Mar 2016 16:35:13 -0700 (PDT) Received: from localhost ([2620:0:1009:3:4fb:9875:8ad2:ab82]) by smtp.gmail.com with ESMTPSA id uh1sm50838095pab.26.2016.03.22.16.35.12 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Tue, 22 Mar 2016 16:35:12 -0700 (PDT) From: Peter Feiner To: drjones@redhat.com, pbonzini@redhat.com, kvm@vger.kernel.org Cc: pfeiner@google.com Subject: [PATCH kvm-unit-tests v7 4/5] scripts: pretty print stack traces Date: Tue, 22 Mar 2016 16:35:05 -0700 Message-Id: <62a708253a7cef177856f6c044a9cf54910fdc12.1458689655.git.pfeiner@google.com> X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 In-Reply-To: References: In-Reply-To: References: 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,T_DKIM_INVALID,T_RP_MATCHES_RCVD, 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 The script runs addresses through addr2line to get file names and line numbers. Also prints out source code lines if files are available. For example 0x004013f5: ac_test_exec at x86/access.c:818 } > ac_test_setup_pte(at, pool); r = ac_test_do_access(at); 0x004014dd: ac_test_run at x86/access.c:852 ++tests; > successes += ac_test_exec(&at, &pool); } while (ac_test_bump(&at)); 0x0040157d: main at x86/access.c:873 printf("starting test\n\n"); > r = ac_test_run(); return r ? 0 : 1; 0x0040028e: start64 at x86/cstart64.S:206 lea __argv(%rip), %rsi > call main mov %eax, %edi Signed-off-by: Peter Feiner Reviewed-By: Andrew Jones --- scripts/pretty_print_stacks.py | 89 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 scripts/pretty_print_stacks.py diff --git a/scripts/pretty_print_stacks.py b/scripts/pretty_print_stacks.py new file mode 100755 index 0000000..ee5a52e --- /dev/null +++ b/scripts/pretty_print_stacks.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python + +import re +import subprocess +import sys +import traceback + +# Subvert output buffering. +def puts(string): + sys.stdout.write(string) + sys.stdout.flush() + +def pretty_print_stack(binary, line): + addrs = line.split()[1:] + # Addresses are return addresses unless preceded by a '@'. We want the + # caller address so line numbers are more intuitive. Thus we subtract 1 + # from the address to get the call code. + for i in range(len(addrs)): + addr = addrs[i] + if addr.startswith('@'): + addrs[i] = addr[1:] + else: + addrs[i] = '%lx' % (int(addrs[i], 16) - 1) + + # Output like this: + # 0x004002be: start64 at path/to/kvm-unit-tests/x86/cstart64.S:208 + # (inlined by) test_ept_violation at path/to/kvm-unit-tests/x86/vmx_tests.c:1719 (discriminator 1) + cmd = ['addr2line', '-e', binary, '-i', '-f', '--pretty', '--address'] + cmd.extend(addrs) + + p = subprocess.Popen(cmd, stdout=subprocess.PIPE) + out, err = p.communicate() + if p.returncode != 0: + puts(line) + return + + for line in out.splitlines(): + m = re.match('(.*) at [^ ]*/kvm-unit-tests/([^ ]*):([0-9]+)(.*)', line) + if m is None: + puts('%s\n' % line) + return + + head, path, line, tail = m.groups() + line = int(line) + puts('%s at %s:%d%s\n' % (head, path, line, tail)) + try: + lines = open(path).readlines() + except IOError: + continue + if line > 1: + puts(' %s\n' % lines[line - 2].rstrip()) + puts(' > %s\n' % lines[line - 1].rstrip()) + if line < len(lines): + puts(' %s\n' % lines[line].rstrip()) + +def main(): + if len(sys.argv) != 2: + sys.stderr.write('usage: %s \n' % sys.argv[0]) + sys.exit(1) + + binary = sys.argv[1] + + try: + while True: + # Subvert input buffering. + line = sys.stdin.readline() + if line == '': + break + + if not line.strip().startswith('STACK:'): + puts(line) + continue + + try: + pretty_print_stack(binary, line) + except Exception: + puts('Error pretty printing stack:\n') + puts(traceback.format_exc()) + puts('Continuing without pretty printing...\n') + while True: + puts(line) + line = sys.stdin.readline() + if line == '': + break + except: + sys.exit(1) + +if __name__ == '__main__': + main()