3 import argparse, datetime, re, subprocess, sys, time
5 parser = argparse.ArgumentParser(description="Run an exhaustive test of the LLDB disassembler for a specific architecture.")
7 parser.add_argument('--arch', required=True, action='store', help='The architecture whose disassembler is to be tested')
8 parser.add_argument('--bytes', required=True, action='store', type=int, help='The byte width of instructions for that architecture')
9 parser.add_argument('--random', required=False, action='store_true', help='Enables non-sequential testing')
10 parser.add_argument('--start', required=False, action='store', type=int, help='The first instruction value to test')
11 parser.add_argument('--skip', required=False, action='store', type=int, help='The interval between instructions to test')
12 parser.add_argument('--log', required=False, action='store', help='A log file to write the most recent instruction being tested')
13 parser.add_argument('--time', required=False, action='store_true', help='Every 100,000 instructions, print an ETA to standard out')
14 parser.add_argument('--lldb', required=False, action='store', help='The path to LLDB.framework, if LLDB should be overridden')
16 arguments = sys.argv[1:]
18 arg_ns = parser.parse_args(arguments)
20 def AddLLDBToSysPathOnMacOSX():
21 def GetLLDBFrameworkPath():
22 lldb_path = subprocess.check_output(["xcrun", "-find", "lldb"])
23 re_result = re.match("(.*)/Developer/usr/bin/lldb", lldb_path)
26 xcode_contents_path = re_result.group(1)
27 return xcode_contents_path + "/SharedFrameworks/LLDB.framework"
29 lldb_framework_path = GetLLDBFrameworkPath()
31 if lldb_framework_path == None:
32 print "Couldn't find LLDB.framework"
35 sys.path.append(lldb_framework_path + "/Resources/Python")
37 if arg_ns.lldb == None:
38 AddLLDBToSysPathOnMacOSX()
40 sys.path.append(arg_ns.lldb + "/Resources/Python")
44 debugger = lldb.SBDebugger.Create()
46 if debugger.IsValid() == False:
47 print "Couldn't create an SBDebugger"
50 target = debugger.CreateTargetWithFileAndArch(None, arg_ns.arch)
52 if target.IsValid() == False:
53 print "Couldn't create an SBTarget for architecture " + arg_ns.arch
56 def ResetLogFile(log_file):
57 if log_file != sys.stdout:
60 def PrintByteArray(log_file, byte_array):
61 for byte in byte_array:
62 print >>log_file, hex(byte) + " ",
65 class SequentialInstructionProvider:
66 def __init__(self, byte_width, log_file, start=0, skip=1):
67 self.m_byte_width = byte_width
68 self.m_log_file = log_file
72 self.m_last = (1 << (byte_width * 8)) - 1
73 def PrintCurrentState(self, ret):
74 ResetLogFile(self.m_log_file)
75 print >>self.m_log_file, self.m_value
76 PrintByteArray(self.m_log_file, ret)
77 def GetNextInstruction(self):
78 if self.m_value > self.m_last:
80 ret = bytearray(self.m_byte_width)
81 for i in range(self.m_byte_width):
82 ret[self.m_byte_width - (i + 1)] = (self.m_value >> (i * 8)) & 255
83 self.PrintCurrentState(ret)
84 self.m_value += self.m_skip
86 def GetNumInstructions(self):
87 return (self.m_last - self.m_start) / self.m_skip
91 ret = self.GetNextInstruction()
96 class RandomInstructionProvider:
97 def __init__(self, byte_width, log_file):
98 self.m_byte_width = byte_width
99 self.m_log_file = log_file
100 self.m_random_file = open("/dev/random", 'r')
101 def PrintCurrentState(self, ret):
102 ResetLogFile(self.m_log_file)
103 PrintByteArray(self.m_log_file, ret)
104 def GetNextInstruction(self):
105 ret = bytearray(self.m_byte_width)
106 for i in range(self.m_byte_width):
107 ret[i] = self.m_random_file.read(1)
108 self.PrintCurrentState(ret)
113 ret = self.GetNextInstruction()
120 def GetProviderWithArguments(args):
123 log_file = open(args.log, 'w')
125 log_file = sys.stdout
126 instruction_provider = None
127 if args.random == True:
128 instruction_provider = RandomInstructionProvider(args.bytes, log_file)
132 if args.start != None:
134 if args.skip != None:
136 instruction_provider = SequentialInstructionProvider(args.bytes, log_file, start, skip)
137 return instruction_provider
139 instruction_provider = GetProviderWithArguments(arg_ns)
141 fake_address = lldb.SBAddress()
143 actually_time = arg_ns.time and not arg_ns.random
146 num_instructions_logged = 0
147 total_num_instructions = instruction_provider.GetNumInstructions()
148 start_time = time.time()
150 for inst_bytes in instruction_provider:
152 if (num_instructions_logged != 0) and (num_instructions_logged % 100000 == 0):
153 curr_time = time.time()
154 elapsed_time = curr_time - start_time
155 remaining_time = float(total_num_instructions - num_instructions_logged) * (float(elapsed_time) / float(num_instructions_logged))
156 print str(datetime.timedelta(seconds=remaining_time))
157 num_instructions_logged = num_instructions_logged + 1
158 inst_list = target.GetInstructions(fake_address, inst_bytes)
159 if not inst_list.IsValid():
160 print >>log_file, "Invalid instruction list"
162 inst = inst_list.GetInstructionAtIndex(0)
163 if not inst.IsValid():
164 print >>log_file, "Invalid instruction"
166 instr_output_stream = lldb.SBStream()
167 inst.GetDescription(instr_output_stream)
168 print >>log_file, instr_output_stream.GetData()