From patchwork Fri Mar 4 19:34:00 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Feiner X-Patchwork-Id: 8507161 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 9AA68C0553 for ; Fri, 4 Mar 2016 19:34:51 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id AF5882022A for ; Fri, 4 Mar 2016 19:34:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A092920259 for ; Fri, 4 Mar 2016 19:34:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932134AbcCDTe2 (ORCPT ); Fri, 4 Mar 2016 14:34:28 -0500 Received: from mail-pf0-f172.google.com ([209.85.192.172]:36441 "EHLO mail-pf0-f172.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1759976AbcCDTeY (ORCPT ); Fri, 4 Mar 2016 14:34:24 -0500 Received: by mail-pf0-f172.google.com with SMTP id 63so40127003pfe.3 for ; Fri, 04 Mar 2016 11:34:24 -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=c0962xDAEZheV1hqUGIUtmRC8OnsocQsmFXHWqYZCT85NROc3P4uerfF5CSKp7eAKe R/JADZvMb1y0gCizRnXfP7BzOxrJ5fEHFMEgxLgszdi1P4mUDCPx/e3JA8x10TIacxTd feo+ij+5tEnL51RkvrFuadT2OVJv1qell5oqrockzbgoTeyTtUsz13F6UxoEFQCxDTDY er8o92ox672stwsEkn4llNJn4cJYC24JbsF74sKE6IcpBjZxAP9vyGCKsKfarvDkTzYE +wm9aCANxCTbcfGNSgg/2WZHpe/sy+oI2P2TJLu1I55rrrSLoNpdPiRRAG/hG2z48Njf C+mw== 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=X747Efec649uzHfooMSUVWXL1YId43DHUtw37O0Zpp9j6m+fLu/EmvRDgL108io9RN BX9XRMnLLGO9p595n786G8TWZsYdQ55HJVpCp+GDqQ5gZHBd06hq/7I77I/Qeg8Hgh9+ uxe5TFSKdDFK1Zj/YUkg/hYockxuygMGcEmzHXdJIibwJVW1tWBeDjWRwt0UW/VAgSVp c1Ra0N2iWK2T3zzVXqcKGSJ7A0dfKnrZsaM7pUgivqLxedgsXwRQpvM6d0PX9XOovwTK 6Cluxrg2A8r0ywGWaqH1NTWf3Iwuo6gWYYf2cHGNW7UnkzmxZovQU460gAuLf8WwTyxU 9W+g== X-Gm-Message-State: AD7BkJIIMd+5WfOQ2P7rVc/MFR+HAG6GkrP8hdMWpCD+oGD1ARajKPqQfXnDCP1QE8FEkjAH X-Received: by 10.98.89.7 with SMTP id n7mr8389440pfb.81.1457120063566; Fri, 04 Mar 2016 11:34:23 -0800 (PST) Received: from localhost ([2620:0:1009:3:90d6:d6fe:49e8:86b0]) by smtp.gmail.com with ESMTPSA id c9sm7266963pfd.90.2016.03.04.11.34.22 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 04 Mar 2016 11:34:22 -0800 (PST) From: Peter Feiner To: kvm@vger.kernel.org, drjones@redhat.com, pbonzini@redhat.com Cc: Peter Feiner Subject: [PATCH kvm-unit-tests v4 4/5] scripts: pretty print stack traces Date: Fri, 4 Mar 2016 11:34:00 -0800 Message-Id: <2d91b48be9e372cfd6e985cd46130fbb445daa85.1457119891.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=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()