From patchwork Mon Mar 7 17:46:56 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Feiner X-Patchwork-Id: 8521471 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id C2780C0553 for ; Mon, 7 Mar 2016 17:47:17 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 9CF012027D for ; Mon, 7 Mar 2016 17:47:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9DC12202A1 for ; Mon, 7 Mar 2016 17:47:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753460AbcCGRrN (ORCPT ); Mon, 7 Mar 2016 12:47:13 -0500 Received: from mail-pa0-f45.google.com ([209.85.220.45]:35613 "EHLO mail-pa0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753428AbcCGRrF (ORCPT ); Mon, 7 Mar 2016 12:47:05 -0500 Received: by mail-pa0-f45.google.com with SMTP id bj10so82561993pad.2 for ; Mon, 07 Mar 2016 09:47:05 -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 :in-reply-to:references; bh=XZkxWKgecQsuETS6Nt3PD+IrJNSFsCUzM+nQ+t/fEVI=; b=Tq46NNTLw+CNZhQFYxJhat96RLC/UGENkm1aF6NWOhu5VBtfWHdwQFG77vGvCkMZso ObMr0H1DSdKBaI9RrrYGYqx1mz+qvoFwDUGMzXY+W8iqXwMUy+Hw9+grU9yhFSfPQrRB vb2MqKZG9OV2VqzeH/+SzGBYgtE9Nsx3IdKip5KuKdtBmuNmSJQIuw7bOJoDU9Qhgvbc dRI2NyNYXb/sWTtfcR0v6s/oeMjwy1orcJh0iRtnBSbx3FM91Ptyyf3pAYDvfiigYc+K Ur6TjiTxeezZWUt8Judexw/NXppzvw5p85s310nn0ZRoDSg7vjcSXVU9s3NuCK7Sdg8Y kgQg== 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=XZkxWKgecQsuETS6Nt3PD+IrJNSFsCUzM+nQ+t/fEVI=; b=bEIDUkh2OGJpJlk4XyWHBtJH9ah7Fue86Wb5LQb8yxR6LEOMB0kVpKiwF46UWhY9pK EQio8P2T8QFenT3vDjy+l5+4j347ATTvwoRD6Dm49B8q0L6cj8vxS4xIwRR8/Pj8Ik/Z z6YMblc50u+JjCoGcKPfwlEDMUBtSJ86EelLkceX5/85rCnXaWbtCebeSl1UOZggy1Xq +nBAI68DR3e3kADIKG1bksy4oGEH96CZXy6sw/iPNedb+/SfyRIYA46ZTBItownIS1Nn N+XKOjX2AJ+2E/SrhwyIRDVJcQAR1bi+/IhMc4bkshQKlWO36+BVrYvYH5otKGhybUSU RkPw== X-Gm-Message-State: AD7BkJLw/MNWsxo3x9R7APJl6GaFWidBQcy8tFnHIQvkVNUQf4+H5SwrSW5C4bFzNK90HKPN X-Received: by 10.66.142.132 with SMTP id rw4mr34517106pab.26.1457372825131; Mon, 07 Mar 2016 09:47:05 -0800 (PST) Received: from localhost ([2620:0:1009:3:d845:64d7:b824:3f90]) by smtp.gmail.com with ESMTPSA id c9sm25429656pfd.90.2016.03.07.09.47.04 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Mon, 07 Mar 2016 09:47:04 -0800 (PST) From: Peter Feiner To: kvm@vger.kernel.org, drjones@redhat.com, pbonzini@redhat.com Cc: Peter Feiner Subject: [PATCH kvm-unit-tests v5 4/5] scripts: pretty print stack traces Date: Mon, 7 Mar 2016 09:46:56 -0800 Message-Id: <2d91b48be9e372cfd6e985cd46130fbb445daa85.1457372594.git.pfeiner@google.com> X-Mailer: git-send-email 2.7.0.rc3.207.g0ac5344 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, 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 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 --- 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()