Sean Callanan | d181730 | 2012-04-06 00:04:36 +0000 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import argparse, re, subprocess, sys |
| 4 | |
| 5 | #-- This code fragment loads LLDB -- |
| 6 | |
| 7 | def AddLLDBToSysPathOnMacOSX(): |
| 8 | def GetLLDBFrameworkPath(): |
| 9 | lldb_path = subprocess.check_output(["xcrun", "-find", "lldb"]) |
| 10 | re_result = re.match("(.*)/Developer/usr/bin/lldb", lldb_path) |
| 11 | if re_result == None: |
| 12 | return None |
| 13 | xcode_contents_path = re_result.group(1) |
| 14 | return xcode_contents_path + "/SharedFrameworks/LLDB.framework" |
| 15 | |
| 16 | lldb_framework_path = GetLLDBFrameworkPath() |
| 17 | |
| 18 | if lldb_framework_path == None: |
| 19 | print "Couldn't find LLDB.framework" |
| 20 | sys.exit(-1) |
| 21 | |
| 22 | sys.path.append(lldb_framework_path + "/Resources/Python") |
| 23 | |
| 24 | AddLLDBToSysPathOnMacOSX() |
| 25 | |
| 26 | import lldb |
| 27 | |
| 28 | #-- End of code fragment that loads LLDB -- |
| 29 | |
| 30 | parser = argparse.ArgumentParser(description="Run an exhaustive test of the LLDB disassembler for a specific architecture.") |
| 31 | |
| 32 | parser.add_argument('--arch', required=True, action='store', help='The architecture whose disassembler is to be tested') |
| 33 | parser.add_argument('--bytes', required=True, action='store', type=int, help='The byte width of instructions for that architecture') |
| 34 | parser.add_argument('--random', required=False, action='store_true', help='Enables non-sequential testing') |
| 35 | parser.add_argument('--start', required=False, action='store', type=int, help='The first instruction value to test') |
| 36 | parser.add_argument('--skip', required=False, action='store', type=int, help='The interval between instructions to test') |
| 37 | |
| 38 | arguments = sys.argv[1:] |
| 39 | |
| 40 | arg_ns = parser.parse_args(arguments) |
| 41 | |
| 42 | debugger = lldb.SBDebugger.Create() |
| 43 | |
| 44 | if debugger.IsValid() == False: |
| 45 | print "Couldn't create an SBDebugger" |
| 46 | sys.exit(-1) |
| 47 | |
| 48 | target = debugger.CreateTargetWithFileAndArch(None, arg_ns.arch) |
| 49 | |
| 50 | if target.IsValid() == False: |
| 51 | print "Couldn't create an SBTarget for architecture " + arg_ns.arch |
| 52 | sys.exit(-1) |
| 53 | |
| 54 | class SequentialInstructionProvider: |
| 55 | def __init__(self, byte_width, start=0, skip=1): |
| 56 | self.m_byte_width = byte_width |
| 57 | self.m_start = start |
| 58 | self.m_skip = skip |
| 59 | self.m_value = start |
| 60 | self.m_last = (1 << (byte_width * 8)) - 1 |
| 61 | def GetNextInstruction(self): |
| 62 | # Print current state |
| 63 | print self.m_value |
| 64 | # Done printing current state |
| 65 | if self.m_value > self.m_last: |
| 66 | return None |
| 67 | ret = bytearray(self.m_byte_width) |
| 68 | for i in range(self.m_byte_width): |
| 69 | ret[self.m_byte_width - (i + 1)] = (self.m_value >> (i * 8)) & 255 |
| 70 | self.m_value += self.m_skip |
| 71 | return ret |
| 72 | def __iter__(self): |
| 73 | return self |
| 74 | def next(self): |
| 75 | ret = self.GetNextInstruction() |
| 76 | if ret == None: |
| 77 | raise StopIteration |
| 78 | return ret |
| 79 | |
| 80 | class RandomInstructionProvider: |
| 81 | def __init__(self, byte_width): |
| 82 | self.m_byte_width = byte_width |
| 83 | self.m_random_file = open("/dev/random", 'r') |
| 84 | def GetNextInstruction(self): |
| 85 | ret = bytearray(self.m_byte_width) |
| 86 | for i in range(self.m_byte_width): |
| 87 | ret[i] = self.m_random_file.read(1) |
| 88 | # Print current state |
| 89 | for ret_byte in ret: |
| 90 | print hex(ret_byte) + " ", |
| 91 | print |
| 92 | # Done printing current state |
| 93 | return ret |
| 94 | def __iter__(self): |
| 95 | return self |
| 96 | def next(self): |
| 97 | ret = self.GetNextInstruction() |
| 98 | if ret == None: |
| 99 | raise StopIteration |
| 100 | return ret |
| 101 | |
| 102 | def GetProviderWithArguments(args): |
| 103 | instruction_provider = None |
| 104 | if args.random == True: |
| 105 | instruction_provider = RandomInstructionProvider(args.bytes) |
| 106 | else: |
| 107 | start = 0 |
| 108 | skip = 1 |
| 109 | if args.start != None: |
| 110 | start = args.start |
| 111 | if args.skip != None: |
| 112 | skip = args.skip |
| 113 | instruction_provider = SequentialInstructionProvider(args.bytes, start, skip) |
| 114 | return instruction_provider |
| 115 | |
| 116 | instruction_provider = GetProviderWithArguments(arg_ns) |
| 117 | |
| 118 | fake_address = lldb.SBAddress() |
| 119 | |
| 120 | for inst_bytes in instruction_provider: |
| 121 | inst_list = target.GetInstructions(fake_address, inst_bytes) |
| 122 | if not inst_list.IsValid(): |
| 123 | print "Invalid instruction list" |
| 124 | continue |
| 125 | inst = inst_list.GetInstructionAtIndex(0) |
| 126 | if not inst.IsValid(): |
| 127 | print "Invalid instruction" |
| 128 | continue |
| 129 | instr_output_stream = lldb.SBStream() |
| 130 | inst.GetDescription(instr_output_stream) |
| 131 | print instr_output_stream.GetData() |