From: Davide Italiano Date: Wed, 6 Mar 2019 22:54:11 +0000 (+0000) Subject: [testsuite] Port crashlog to python 3, second attempt. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a658ab9f55662f4048cb59e9520b7bf9b1b20494;p=platform%2Fupstream%2Fllvm.git [testsuite] Port crashlog to python 3, second attempt. llvm-svn: 355562 --- diff --git a/lldb/examples/darwin/heap_find/heap.py b/lldb/examples/darwin/heap_find/heap.py index c463380..3157dfb 100644 --- a/lldb/examples/darwin/heap_find/heap.py +++ b/lldb/examples/darwin/heap_find/heap.py @@ -9,7 +9,6 @@ #---------------------------------------------------------------------- import lldb -import commands import optparse import os import os.path @@ -228,7 +227,7 @@ def append_regex_callback(option, opt, value, parser): ivar_regex = re.compile(value) parser.values.ivar_regex_blacklist.append(ivar_regex) except: - print 'error: an exception was thrown when compiling the ivar regular expression for "%s"' % value + print('error: an exception was thrown when compiling the ivar regular expression for "%s"' % value) def add_common_options(parser): @@ -389,16 +388,16 @@ def find_variable_containing_address(verbose, frame, match_addr): if var_addr != lldb.LLDB_INVALID_ADDRESS: byte_size = var.GetType().GetByteSize() if verbose: - print 'frame #%u: [%#x - %#x) %s' % (frame.GetFrameID(), var.load_addr, var.load_addr + byte_size, var.name) + print('frame #%u: [%#x - %#x) %s' % (frame.GetFrameID(), var.load_addr, var.load_addr + byte_size, var.name)) if var_addr == match_addr: if verbose: - print 'match' + print('match') return var else: if byte_size > 0 and var_addr <= match_addr and match_addr < ( var_addr + byte_size): if verbose: - print 'match' + print('match') return var return None @@ -616,10 +615,10 @@ lldb_info''' % (options.max_frames, options.max_history, addr) expr_options.SetPrefix(expr_prefix) expr_sbvalue = frame.EvaluateExpression(expr, expr_options) if options.verbose: - print "expression:" - print expr - print "expression result:" - print expr_sbvalue + print("expression:") + print(expr) + print("expression result:") + print(expr_sbvalue) if expr_sbvalue.error.Success(): if history: malloc_stack_history = lldb.value(expr_sbvalue) @@ -670,10 +669,10 @@ def display_match_results( expr_options.SetPrefix(expr_prefix) expr_sbvalue = frame.EvaluateExpression(expr, expr_options) if options.verbose: - print "expression:" - print expr - print "expression result:" - print expr_sbvalue + print("expression:") + print(expr) + print("expression result:") + print(expr_sbvalue) if expr_sbvalue.error.Success(): match_value = lldb.value(expr_sbvalue) i = 0 @@ -863,14 +862,14 @@ def find_variable(debugger, command, result, dict): for arg in args: var_addr = int(arg, 16) - print >>result, "Finding a variable with address %#x..." % (var_addr) + print("Finding a variable with address %#x..." % (var_addr), file=result) done = False for thread in process: for frame in thread: var = find_variable_containing_address( options.verbose, frame, var_addr) if var: - print var + print(var) done = True break if done: @@ -1519,4 +1518,4 @@ lldb.debugger.HandleCommand( lldb.debugger.HandleCommand( 'command script add -f %s.objc_refs objc_refs' % __name__) -print '"malloc_info", "ptr_refs", "cstr_refs", "find_variable", and "objc_refs" commands have been installed, use the "--help" options on these commands for detailed help.' +print('"malloc_info", "ptr_refs", "cstr_refs", "find_variable", and "objc_refs" commands have been installed, use the "--help" options on these commands for detailed help.') diff --git a/lldb/examples/python/crashlog.py b/lldb/examples/python/crashlog.py index 540aca6..146c2a6 100755 --- a/lldb/examples/python/crashlog.py +++ b/lldb/examples/python/crashlog.py @@ -26,7 +26,6 @@ # PYTHONPATH=/path/to/LLDB.framework/Resources/Python ./crashlog.py ~/Library/Logs/DiagnosticReports/a.crash #---------------------------------------------------------------------- -import commands import cmd import datetime import glob @@ -38,6 +37,7 @@ import pprint # pp = pprint.PrettyPrinter(indent=4); pp.pprint(command_args) import re import shlex import string +import subprocess import sys import time import uuid @@ -51,7 +51,7 @@ except ImportError: platform_system = platform.system() if platform_system == 'Darwin': # On Darwin, try the currently selected Xcode directory - xcode_dir = commands.getoutput("xcode-select --print-path") + xcode_dir = subprocess.check_output("xcode-select --print-path", shell=True) if xcode_dir: lldb_python_dirs.append( os.path.realpath( @@ -71,11 +71,11 @@ except ImportError: except ImportError: pass else: - print 'imported lldb from: "%s"' % (lldb_python_dir) + print('imported lldb from: "%s"' % (lldb_python_dir)) success = True break if not success: - print "error: couldn't locate the 'lldb' module, please set PYTHONPATH correctly" + print("error: couldn't locate the 'lldb' module, please set PYTHONPATH correctly") sys.exit(1) from lldb.utils import symbolication @@ -113,17 +113,17 @@ class CrashLog(symbolication.Symbolicator): def dump(self, prefix): if self.app_specific_backtrace: - print "%Application Specific Backtrace[%u] %s" % (prefix, self.index, self.reason) + print("%Application Specific Backtrace[%u] %s" % (prefix, self.index, self.reason)) else: - print "%sThread[%u] %s" % (prefix, self.index, self.reason) + print("%sThread[%u] %s" % (prefix, self.index, self.reason)) if self.frames: - print "%s Frames:" % (prefix) + print("%s Frames:" % (prefix)) for frame in self.frames: frame.dump(prefix + ' ') if self.registers: - print "%s Registers:" % (prefix) + print("%s Registers:" % (prefix)) for reg in self.registers.keys(): - print "%s %-5s = %#16.16x" % (prefix, reg, self.registers[reg]) + print("%s %-5s = %#16.16x" % (prefix, reg, self.registers[reg])) def dump_symbolicated(self, crash_log, options): this_thread_crashed = self.app_specific_backtrace @@ -132,7 +132,7 @@ class CrashLog(symbolication.Symbolicator): if options.crashed_only and this_thread_crashed == False: return - print "%s" % self + print("%s" % self) #prev_frame_index = -1 display_frame_idx = -1 for frame_idx, frame in enumerate(self.frames): @@ -151,7 +151,7 @@ class CrashLog(symbolication.Symbolicator): symbolicated_frame_address_idx = 0 for symbolicated_frame_address in symbolicated_frame_addresses: display_frame_idx += 1 - print '[%3u] %s' % (frame_idx, symbolicated_frame_address) + print('[%3u] %s' % (frame_idx, symbolicated_frame_address)) if (options.source_all or self.did_crash( )) and display_frame_idx < options.source_frames and options.source_context: source_context = options.source_context @@ -166,12 +166,12 @@ class CrashLog(symbolication.Symbolicator): # Indent the source a bit indent_str = ' ' join_str = '\n' + indent_str - print '%s%s' % (indent_str, join_str.join(source_text.split('\n'))) + print('%s%s' % (indent_str, join_str.join(source_text.split('\n')))) if symbolicated_frame_address_idx == 0: if disassemble: instructions = symbolicated_frame_address.get_instructions() if instructions: - print + print() symbolication.disassemble_instructions( crash_log.get_target(), instructions, @@ -179,10 +179,10 @@ class CrashLog(symbolication.Symbolicator): options.disassemble_before, options.disassemble_after, frame.index > 0) - print + print() symbolicated_frame_address_idx += 1 else: - print frame + print(frame) def add_ident(self, ident): if ident not in self.idents: @@ -216,13 +216,13 @@ class CrashLog(symbolication.Symbolicator): return "[%3u] 0x%16.16x" % (self.index, self.pc) def dump(self, prefix): - print "%s%s" % (prefix, str(self)) + print("%s%s" % (prefix, str(self))) class DarwinImage(symbolication.Image): """Class that represents a binary images in a darwin crash log""" dsymForUUIDBinary = os.path.expanduser('~rc/bin/dsymForUUID') if not os.path.exists(dsymForUUIDBinary): - dsymForUUIDBinary = commands.getoutput('which dsymForUUID') + dsymForUUIDBinary = subprocess.check_output('which dsymForUUID', shell=True) dwarfdump_uuid_regex = re.compile( 'UUID: ([-0-9a-fA-F]+) \(([^\(]+)\) .*') @@ -245,8 +245,8 @@ class CrashLog(symbolication.Symbolicator): self.version = version def find_matching_slice(self): - dwarfdump_cmd_output = commands.getoutput( - 'dwarfdump --uuid "%s"' % self.path) + dwarfdump_cmd_output = subprocess.check_output( + 'dwarfdump --uuid "%s"' % self.path, shell=True) self_uuid = self.get_uuid() for line in dwarfdump_cmd_output.splitlines(): match = self.dwarfdump_uuid_regex.search(line) @@ -259,8 +259,8 @@ class CrashLog(symbolication.Symbolicator): return True if not self.resolved_path: self.unavailable = True - print("error\n error: unable to locate '%s' with UUID %s" - % (self.path, self.get_normalized_uuid_string())) + print(("error\n error: unable to locate '%s' with UUID %s" + % (self.path, self.get_normalized_uuid_string()))) return False def locate_module_and_debug_symbols(self): @@ -270,16 +270,16 @@ class CrashLog(symbolication.Symbolicator): # Mark this as resolved so we don't keep trying self.resolved = True uuid_str = self.get_normalized_uuid_string() - print 'Getting symbols for %s %s...' % (uuid_str, self.path), + print('Getting symbols for %s %s...' % (uuid_str, self.path), end=' ') if os.path.exists(self.dsymForUUIDBinary): dsym_for_uuid_command = '%s %s' % ( self.dsymForUUIDBinary, uuid_str) - s = commands.getoutput(dsym_for_uuid_command) + s = subprocess.check_output(dsym_for_uuid_command, shell=True) if s: try: plist_root = plistlib.readPlistFromString(s) except: - print("Got exception: ", sys.exc_value, " handling dsymForUUID output: \n", s) + print(("Got exception: ", sys.exc_value, " handling dsymForUUID output: \n", s)) raise if plist_root: plist = plist_root[uuid_str] @@ -303,7 +303,7 @@ class CrashLog(symbolication.Symbolicator): ["/usr/bin/mdfind", "com_apple_xcode_dsym_uuids == %s"%uuid_str])[:-1] if dsym and os.path.exists(dsym): - print('falling back to binary inside "%s"'%dsym) + print(('falling back to binary inside "%s"'%dsym)) self.symfile = dsym dwarf_dir = os.path.join(dsym, 'Contents/Resources/DWARF') for filename in os.listdir(dwarf_dir): @@ -315,7 +315,7 @@ class CrashLog(symbolication.Symbolicator): pass if (self.resolved_path and os.path.exists(self.resolved_path)) or ( self.path and os.path.exists(self.path)): - print 'ok' + print('ok') # if self.resolved_path: # print ' exe = "%s"' % self.resolved_path # if self.symfile: @@ -471,7 +471,7 @@ class CrashLog(symbolication.Symbolicator): thread.frames.append(CrashLog.Frame(int(frame_match.group(1)), int( frame_match.group(3), 0), frame_match.group(4))) else: - print 'error: frame regex failed for line: "%s"' % line + print('error: frame regex failed for line: "%s"' % line) elif parse_mode == PARSE_MODE_IMAGES: image_match = self.image_regex_uuid.search(line) if image_match: @@ -484,7 +484,7 @@ class CrashLog(symbolication.Symbolicator): uuid.UUID(img_uuid), img_path) self.images.append(image) else: - print "error: image regex failed for: %s" % line + print("error: image regex failed for: %s" % line) elif parse_mode == PARSE_MODE_THREGS: stripped_line = line.strip() @@ -502,15 +502,15 @@ class CrashLog(symbolication.Symbolicator): f.close() def dump(self): - print "Crash Log File: %s" % (self.path) + print("Crash Log File: %s" % (self.path)) if self.backtraces: - print "\nApplication Specific Backtraces:" + print("\nApplication Specific Backtraces:") for thread in self.backtraces: thread.dump(' ') - print "\nThreads:" + print("\nThreads:") for thread in self.threads: thread.dump(' ') - print "\nImages:" + print("\nImages:") for image in self.images: image.dump(' ') @@ -533,7 +533,7 @@ class CrashLog(symbolication.Symbolicator): return self.target # We weren't able to open the main executable as, but we can still # symbolicate - print 'crashlog.create_target()...2' + print('crashlog.create_target()...2') if self.idents: for ident in self.idents: image = self.find_image_with_identifier(ident) @@ -541,13 +541,13 @@ class CrashLog(symbolication.Symbolicator): self.target = image.create_target() if self.target: return self.target # success - print 'crashlog.create_target()...3' + print('crashlog.create_target()...3') for image in self.images: self.target = image.create_target() if self.target: return self.target # success - print 'crashlog.create_target()...4' - print 'error: unable to locate any executables from the crash log' + print('crashlog.create_target()...4') + print('error: unable to locate any executables from the crash log') return self.target def get_target(self): @@ -555,7 +555,7 @@ class CrashLog(symbolication.Symbolicator): def usage(): - print "Usage: lldb-symbolicate.py [-n name] executable-image" + print("Usage: lldb-symbolicate.py [-n name] executable-image") sys.exit(0) @@ -572,7 +572,7 @@ class Interactive(cmd.Cmd): def default(self, line): '''Catch all for unknown command, which will exit the interpreter.''' - print "uknown command: %s" % line + print("uknown command: %s" % line) return True def do_q(self, line): @@ -602,7 +602,7 @@ class Interactive(cmd.Cmd): if idx < len(self.crash_logs): SymbolicateCrashLog(self.crash_logs[idx], options) else: - print 'error: crash log index %u is out of range' % (idx) + print('error: crash log index %u is out of range' % (idx)) else: # No arguments, symbolicate all crash logs using the options # provided @@ -613,9 +613,9 @@ class Interactive(cmd.Cmd): '''Dump a list of all crash logs that are currently loaded. USAGE: list''' - print '%u crash logs are loaded:' % len(self.crash_logs) + print('%u crash logs are loaded:' % len(self.crash_logs)) for (crash_log_idx, crash_log) in enumerate(self.crash_logs): - print '[%u] = %s' % (crash_log_idx, crash_log.path) + print('[%u] = %s' % (crash_log_idx, crash_log.path)) def do_image(self, line): '''Dump information about one or more binary images in the crash log given an image basename, or all images if no arguments are provided.''' @@ -645,22 +645,22 @@ class Interactive(cmd.Cmd): if fullpath_search: if image.get_resolved_path() == image_path: matches_found += 1 - print '[%u] ' % (crash_log_idx), image + print('[%u] ' % (crash_log_idx), image) else: image_basename = image.get_resolved_path_basename() if image_basename == image_path: matches_found += 1 - print '[%u] ' % (crash_log_idx), image + print('[%u] ' % (crash_log_idx), image) if matches_found == 0: for (image_idx, image) in enumerate(crash_log.images): resolved_image_path = image.get_resolved_path() if resolved_image_path and string.find( image.get_resolved_path(), image_path) >= 0: - print '[%u] ' % (crash_log_idx), image + print('[%u] ' % (crash_log_idx), image) else: for crash_log in self.crash_logs: for (image_idx, image) in enumerate(crash_log.images): - print '[%u] %s' % (image_idx, image) + print('[%u] %s' % (image_idx, image)) return False @@ -675,12 +675,12 @@ def interactive_crashlogs(options, args): # print 'crash_log_file = "%s"' % crash_log_file crash_log = CrashLog(crash_log_file) if crash_log.error: - print crash_log.error + print(crash_log.error) continue if options.debug: crash_log.dump() if not crash_log.images: - print 'error: no images in crash log "%s"' % (crash_log) + print('error: no images in crash log "%s"' % (crash_log)) continue else: crash_logs.append(crash_log) @@ -736,7 +736,7 @@ def save_crashlog(debugger, command, exe_ctx, result, dict): (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) out_file.write( 'OS Version: Mac OS X %s (%s)\n' % - (platform.mac_ver()[0], commands.getoutput('sysctl -n kern.osversion'))) + (platform.mac_ver()[0], subprocess.check_output('sysctl -n kern.osversion', shell=True))) out_file.write('Report Version: 9\n') for thread_idx in range(process.num_threads): thread = process.thread[thread_idx] @@ -808,21 +808,21 @@ def Symbolicate(debugger, command, result, dict): def SymbolicateCrashLog(crash_log, options): if crash_log.error: - print crash_log.error + print(crash_log.error) return if options.debug: crash_log.dump() if not crash_log.images: - print 'error: no images in crash log' + print('error: no images in crash log') return if options.dump_image_list: - print "Binary Images:" + print("Binary Images:") for image in crash_log.images: if options.verbose: - print image.debug_dump() + print(image.debug_dump()) else: - print image + print(image) target = crash_log.create_target() if not target: @@ -845,7 +845,7 @@ def SymbolicateCrashLog(crash_log, options): for image in images: images_to_load.append(image) else: - print 'error: can\'t find image for identifier "%s"' % ident + print('error: can\'t find image for identifier "%s"' % ident) else: for ident in crash_log.idents: images = crash_log.find_images_with_identifier(ident) @@ -853,13 +853,13 @@ def SymbolicateCrashLog(crash_log, options): for image in images: images_to_load.append(image) else: - print 'error: can\'t find image for identifier "%s"' % ident + print('error: can\'t find image for identifier "%s"' % ident) for image in images_to_load: if image not in loaded_images: err = image.add_module(target) if err: - print err + print(err) else: # print 'loaded %s' % image loaded_images.append(image) @@ -867,11 +867,11 @@ def SymbolicateCrashLog(crash_log, options): if crash_log.backtraces: for thread in crash_log.backtraces: thread.dump_symbolicated(crash_log, options) - print + print() for thread in crash_log.threads: thread.dump_symbolicated(crash_log, options) - print + print() def CreateSymbolicateCrashLogOptions( @@ -998,12 +998,12 @@ be disassembled and lookups can be performed using the addresses found in the cr return if options.debug: - print 'command_args = %s' % command_args - print 'options', options - print 'args', args + print('command_args = %s' % command_args) + print('options', options) + print('args', args) if options.debug_delay > 0: - print "Waiting %u seconds for debugger to attach..." % options.debug_delay + print("Waiting %u seconds for debugger to attach..." % options.debug_delay) time.sleep(options.debug_delay) error = lldb.SBError() @@ -1024,4 +1024,4 @@ elif getattr(lldb, 'debugger', None): 'command script add -f lldb.macosx.crashlog.Symbolicate crashlog') lldb.debugger.HandleCommand( 'command script add -f lldb.macosx.crashlog.save_crashlog save_crashlog') - print '"crashlog" and "save_crashlog" command installed, use the "--help" option for detailed help' + print('"crashlog" and "save_crashlog" command installed, use the "--help" option for detailed help') diff --git a/lldb/examples/python/symbolication.py b/lldb/examples/python/symbolication.py index b655ad0..141dd7a 100755 --- a/lldb/examples/python/symbolication.py +++ b/lldb/examples/python/symbolication.py @@ -27,7 +27,6 @@ #---------------------------------------------------------------------- import lldb -import commands import optparse import os import plistlib @@ -203,13 +202,13 @@ class Section: if op == '+': self.end_addr += self.start_addr return True - print 'error: invalid section info string "%s"' % s - print 'Valid section info formats are:' - print 'Format Example Description' - print '--------------------- -----------------------------------------------' - print '= __TEXT=0x123000 Section from base address only' - print '=- __TEXT=0x123000-0x124000 Section from base address and end address' - print '=+ __TEXT=0x123000+0x1000 Section from base address and size' + print('error: invalid section info string "%s"' % s) + print('Valid section info formats are:') + print('Format Example Description') + print('--------------------- -----------------------------------------------') + print('= __TEXT=0x123000 Section from base address only') + print('=- __TEXT=0x123000-0x124000 Section from base address and end address') + print('=+ __TEXT=0x123000+0x1000 Section from base address and size') return False def __str__(self): @@ -261,21 +260,21 @@ class Image: return obj def dump(self, prefix): - print "%s%s" % (prefix, self) + print("%s%s" % (prefix, self)) def debug_dump(self): - print 'path = "%s"' % (self.path) - print 'resolved_path = "%s"' % (self.resolved_path) - print 'resolved = %i' % (self.resolved) - print 'unavailable = %i' % (self.unavailable) - print 'uuid = %s' % (self.uuid) - print 'section_infos = %s' % (self.section_infos) - print 'identifier = "%s"' % (self.identifier) - print 'version = %s' % (self.version) - print 'arch = %s' % (self.arch) - print 'module = %s' % (self.module) - print 'symfile = "%s"' % (self.symfile) - print 'slide = %i (0x%x)' % (self.slide, self.slide) + print('path = "%s"' % (self.path)) + print('resolved_path = "%s"' % (self.resolved_path)) + print('resolved = %i' % (self.resolved)) + print('unavailable = %i' % (self.unavailable)) + print('uuid = %s' % (self.uuid)) + print('section_infos = %s' % (self.section_infos)) + print('identifier = "%s"' % (self.identifier)) + print('version = %s' % (self.version)) + print('arch = %s' % (self.arch)) + print('module = %s' % (self.module)) + print('symfile = "%s"' % (self.symfile)) + print('slide = %i (0x%x)' % (self.slide, self.slide)) def __str__(self): s = '' @@ -428,12 +427,12 @@ class Image: if self.has_section_load_info(): err = self.load_module(target) if err: - print 'ERROR: ', err + print('ERROR: ', err) return target else: - print 'error: unable to create a valid target for (%s) "%s"' % (self.arch, self.path) + print('error: unable to create a valid target for (%s) "%s"' % (self.arch, self.path)) else: - print 'error: unable to locate main executable (%s) "%s"' % (self.arch, self.path) + print('error: unable to locate main executable (%s) "%s"' % (self.arch, self.path)) return None @@ -554,7 +553,7 @@ class Symbolicator: if symbolicated_addresses: return symbolicated_addresses else: - print 'error: no target in Symbolicator' + print('error: no target in Symbolicator') return None @@ -602,22 +601,22 @@ def disassemble_instructions( end_idx = inst_idx for i in range(start_idx, end_idx + 1): if i == pc_index: - print ' -> ', lines[i] + print(' -> ', lines[i]) else: - print ' ', lines[i] + print(' ', lines[i]) def print_module_section_data(section): - print section + print(section) section_data = section.GetSectionData() if section_data: ostream = lldb.SBStream() section_data.GetDescription(ostream, section.GetFileAddress()) - print ostream.GetData() + print(ostream.GetData()) def print_module_section(section, depth): - print section + print(section) if depth > 0: num_sub_sections = section.GetNumSubSections() for sect_idx in range(num_sub_sections): @@ -632,7 +631,7 @@ def print_module_sections(module, depth): def print_module_symbols(module): for sym in module: - print sym + print(sym) def Symbolicate(command_args): @@ -709,17 +708,17 @@ def Symbolicate(command_args): target = symbolicator.create_target() if options.verbose: - print symbolicator + print(symbolicator) if target: for addr_str in args: addr = int(addr_str, 0) symbolicated_addrs = symbolicator.symbolicate( addr, options.verbose) for symbolicated_addr in symbolicated_addrs: - print symbolicated_addr - print + print(symbolicated_addr) + print() else: - print 'error: no target for %s' % (symbolicator) + print('error: no target for %s' % (symbolicator)) if __name__ == '__main__': # Create a new debugger instance