From 1e554bd1c5aab75ba3e9d12f87b297ece2a2e76a Mon Sep 17 00:00:00 2001 From: Ross Bayer Date: Fri, 24 Jan 2020 01:19:58 -0800 Subject: [PATCH] [Gardening] Update the utils/backtrace-check script to use more modern Python formatting. --- utils/backtrace-check | 100 ++++++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 37 deletions(-) diff --git a/utils/backtrace-check b/utils/backtrace-check index a304f68bc22ed..a8058cff27b46 100755 --- a/utils/backtrace-check +++ b/utils/backtrace-check @@ -1,54 +1,82 @@ #!/usr/bin/env python -# This script uses a regex to validate the output of our backtraces. -# The layout we assume is: -# -#
+ -# -# It currently just checks that the backtrace results in at least one correctly -# formatted entry. It does not directly validate the input since the output -# would not be robust against standard library changes. -# -# TODO: We could have the user pass in the frame number, library name, and -# demangled name. These we can always validate as true in a robust way. On the -# other hand, address and offset are more difficult to validate in a robust way -# in the face of small codegen differences, so without any further thought I -# imagine we can just check the format. -# -# 11 libswiftCore.dylib 0x000000000dce84d0l _fatalErrorMessage(StaticString, -# StaticString, StaticString, UInt, flags : UInt32) -> () + 444 - -from __future__ import print_function + +""" +This script uses a regex to validate the output of our backtraces. +The layout we assume is: + +
+ + +It currently just checks that the backtrace results in at least one correctly +formatted entry. It does not directly validate the input since the output +would not be robust against standard library changes. + +TODO: We could have the user pass in the frame number, library name, and +demangled name. These we can always validate as true in a robust way. On the +other hand, address and offset are more difficult to validate in a robust way +in the face of small codegen differences, so without any further thought I +imagine we can just check the format. + +11 libswiftCore.dylib 0x000000000dce84d0l _fatalErrorMessage(StaticString, +StaticString, StaticString, UInt, flags : UInt32) -> () + 444 +""" + + +from __future__ import absolute_import, print_function, unicode_literals import argparse import re import sys -def main(): +# ----------------------------------------------------------------------------- +# Constants + +DESCRIPTION = """ +Checks that a stacktrace dump follows canonical formatting. +""" + +TARGET_PATTERN = re.compile( + r'(?P\d+) +(?P\S+) +(?P
0x[0-9a-fA-F]{16}) ' + r'(?P[^+]+) [+] (?P\d+)') + +RESULTS_FORMAT = """Stack Trace Entry: +\tIndex: '%(index)s' +\tObject File: '%(object)s' +\tAddress: '%(address)s' +\tRoutine: '%(routine)s' +\tOffset: '%(offset)s' +""" + + +# ----------------------------------------------------------------------------- + +def parse_args(): parser = argparse.ArgumentParser( formatter_class=argparse.RawDescriptionHelpFormatter, - description="""Checks that a stacktrace dump follows canonical -formatting.""") + description=DESCRIPTION) + parser.add_argument( - "-u", "--check-unavailable", action='store_true', - help="Checks if any symbols were unavailable") - args = parser.parse_args() + '-u', '--check-unavailable', + action='store_true', + help='Checks if any symbols were unavailable') - TARGET_RE = re.compile( - r"(?P\d+) +(?P\S+) +(?P
0x[0-9a-fA-F]{16}) " - r"(?P[^+]+) [+] (?P\d+)") + return parser.parse_args() + + +def main(): + args = parse_args() lines = sys.stdin.readlines() found_stack_trace_start = False found_stack_trace_entry = False for line in lines: - line = line.rstrip("\n") + line = line.rstrip('\n') # First see if we found the start of our stack trace start. If so, set # the found stack trace flag and continue. - if line == "Current stack trace:": + if line == 'Current stack trace:': assert(not found_stack_trace_start) found_stack_trace_start = True continue @@ -59,21 +87,19 @@ formatting.""") continue # Ok, we are in the middle of matching a stack trace entry. - m = TARGET_RE.match(line) + matches = TARGET_PATTERN.match(line) + # If we fail to match, we have exited the stack trace entry region - if m is None: + if matches is None: break # At this point, we know that we have some sort of match. found_stack_trace_entry = True - print("Stack Trace Entry:") - print("\tIndex: '%(index)s'\n\tObject File: '%(object)s'\n\tAddress: " - "'%(address)s'\n\tRoutine: '%(routine)s'\n\tOffset: '%(offset)s'" - "\n" % m.groupdict()) + print(RESULTS_FORMAT.format(**matches.groupdict())) # Check for unavailable symbols, if that was requested. if args.check_unavailable: - assert("unavailable" not in m.group("routine")) + assert('unavailable' not in matches.group('routine')) # Once we have processed all of the lines, make sure that we found at least # one stack trace entry.