From patchwork Fri Mar 11 00:47:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Feiner X-Patchwork-Id: 8561181 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 CAB76C0553 for ; Fri, 11 Mar 2016 00:48:04 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B6D10201ED for ; Fri, 11 Mar 2016 00:48:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id AE6C920211 for ; Fri, 11 Mar 2016 00:48:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933242AbcCKAr5 (ORCPT ); Thu, 10 Mar 2016 19:47:57 -0500 Received: from mail-pf0-f178.google.com ([209.85.192.178]:34965 "EHLO mail-pf0-f178.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932949AbcCKArx (ORCPT ); Thu, 10 Mar 2016 19:47:53 -0500 Received: by mail-pf0-f178.google.com with SMTP id n5so43120301pfn.2 for ; Thu, 10 Mar 2016 16:47:53 -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=e4sowjXeedNi4ZgqrIKsis59fAhKpNWqFvDRJlxJ2X/DOLweV1HKoa4CFL9mtxVX+N igw8BZ3dtP/FboQfwulWKLPD0wLR1m2daPvqGcgl/qx/X8065EGwjhWgvGrgxoZjgucZ aIHEj81cHYC1q6SgQ2t4htlJ2J2zMjp1vDMc96otIlnnVuYGDfqdpTtSVZ5W1mXqzavu m8bNmjPat2k6QH8lHtKPtdrevAB6pjQRjrnVdZZZE7v5eMGracbIzdd70symPqV+D5Lt cSdGjTLCWN1avgE6hsfQo9otVVwkiSFaWaXpRfEmEHgu4XMnGQ+BygG1QQcJvCHxmD0d lPYA== 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=GlDeZ82xHRDghyg+YqvzNcxQec5Gs5LxYPhh71qPhNLabQ+3LaLs8/PlCkdg2hrR+Z gVe7+fNB7FoytdKFBjJ/5ZI//gMrG0r6fxDVg87CbpUipN/kRkzMW0uMaccCmsAWSt7u w8C6PtGvDP91kgcA7/jgXl8BSu0Wn4gUPMxRE/0M+fN8odDuQyIO0JGoMdG1+FNNgpAH VeLOG1WZR9mimRhhrk+lOwCxD8VCClUnH3PuvaJBIg+xsASHCJbwNuAhI8Jr28VOAjpR 0RJim+BNtfDf87elViZltr9yxkS+vvi75TwzBQ7eFX1x9/OUeq+1iuby+zGP6jnfS+ms UAKw== X-Gm-Message-State: AD7BkJIglA4O01Y8enf/2iYPgEtmfxZPtWgRwuNK3tNFH5JmuY9YxrQ7lf6iszOBrXPcYmPG X-Received: by 10.98.93.1 with SMTP id r1mr9201448pfb.57.1457657272755; Thu, 10 Mar 2016 16:47:52 -0800 (PST) Received: from localhost ([2620:0:1009:3:fd91:211e:47f5:d0b0]) by smtp.gmail.com with ESMTPSA id u81sm8165376pfi.15.2016.03.10.16.47.52 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Thu, 10 Mar 2016 16:47:52 -0800 (PST) From: Peter Feiner To: kvm@vger.kernel.org, drjones@redhat.com, pbonzini@redhat.com Cc: Peter Feiner Subject: [PATCH kvm-unit-tests v6 4/5] scripts: pretty print stack traces Date: Thu, 10 Mar 2016 16:47:38 -0800 Message-Id: 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=unavailable 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()