blob: 5aa354dc24cb12467ef823103b03f19adeda6758 [file] [log] [blame]
Sean Callanand1817302012-04-06 00:04:36 +00001#!/usr/bin/python
2
Sean Callananceabd2d2012-04-06 20:53:23 +00003import argparse, datetime, re, subprocess, sys, time
Sean Callanand1817302012-04-06 00:04:36 +00004
Sean Callananf5c87882012-04-14 01:06:06 +00005parser = argparse.ArgumentParser(description="Run an exhaustive test of the LLDB disassembler for a specific architecture.")
6
7parser.add_argument('--arch', required=True, action='store', help='The architecture whose disassembler is to be tested')
8parser.add_argument('--bytes', required=True, action='store', type=int, help='The byte width of instructions for that architecture')
9parser.add_argument('--random', required=False, action='store_true', help='Enables non-sequential testing')
10parser.add_argument('--start', required=False, action='store', type=int, help='The first instruction value to test')
11parser.add_argument('--skip', required=False, action='store', type=int, help='The interval between instructions to test')
12parser.add_argument('--log', required=False, action='store', help='A log file to write the most recent instruction being tested')
13parser.add_argument('--time', required=False, action='store_true', help='Every 100,000 instructions, print an ETA to standard out')
14parser.add_argument('--lldb', required=False, action='store', help='The path to LLDB.framework, if LLDB should be overridden')
15
16arguments = sys.argv[1:]
17
18arg_ns = parser.parse_args(arguments)
19
Sean Callanand1817302012-04-06 00:04:36 +000020def AddLLDBToSysPathOnMacOSX():
21 def GetLLDBFrameworkPath():
22 lldb_path = subprocess.check_output(["xcrun", "-find", "lldb"])
23 re_result = re.match("(.*)/Developer/usr/bin/lldb", lldb_path)
24 if re_result == None:
25 return None
26 xcode_contents_path = re_result.group(1)
27 return xcode_contents_path + "/SharedFrameworks/LLDB.framework"
28
29 lldb_framework_path = GetLLDBFrameworkPath()
30
31 if lldb_framework_path == None:
32 print "Couldn't find LLDB.framework"
33 sys.exit(-1)
34
35 sys.path.append(lldb_framework_path + "/Resources/Python")
36
Sean Callananf5c87882012-04-14 01:06:06 +000037if arg_ns.lldb == None:
38 AddLLDBToSysPathOnMacOSX()
39else:
40 sys.path.append(arg_ns.lldb + "/Resources/Python")
Sean Callanand1817302012-04-06 00:04:36 +000041
42import lldb
43
Sean Callanand1817302012-04-06 00:04:36 +000044debugger = lldb.SBDebugger.Create()
45
46if debugger.IsValid() == False:
47 print "Couldn't create an SBDebugger"
48 sys.exit(-1)
49
50target = debugger.CreateTargetWithFileAndArch(None, arg_ns.arch)
51
52if target.IsValid() == False:
53 print "Couldn't create an SBTarget for architecture " + arg_ns.arch
54 sys.exit(-1)
55
Sean Callananceabd2d2012-04-06 20:53:23 +000056def ResetLogFile(log_file):
57 if log_file != sys.stdout:
58 log_file.seek(0)
Sean Callananceabd2d2012-04-06 20:53:23 +000059
60def PrintByteArray(log_file, byte_array):
61 for byte in byte_array:
62 print >>log_file, hex(byte) + " ",
63 print >>log_file
64
Sean Callanand1817302012-04-06 00:04:36 +000065class SequentialInstructionProvider:
Sean Callananceabd2d2012-04-06 20:53:23 +000066 def __init__(self, byte_width, log_file, start=0, skip=1):
Sean Callanand1817302012-04-06 00:04:36 +000067 self.m_byte_width = byte_width
Sean Callananceabd2d2012-04-06 20:53:23 +000068 self.m_log_file = log_file
Sean Callanand1817302012-04-06 00:04:36 +000069 self.m_start = start
70 self.m_skip = skip
71 self.m_value = start
72 self.m_last = (1 << (byte_width * 8)) - 1
Sean Callananceabd2d2012-04-06 20:53:23 +000073 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)
Sean Callanand1817302012-04-06 00:04:36 +000077 def GetNextInstruction(self):
Sean Callanand1817302012-04-06 00:04:36 +000078 if self.m_value > self.m_last:
79 return None
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
Sean Callananceabd2d2012-04-06 20:53:23 +000083 self.PrintCurrentState(ret)
Sean Callanand1817302012-04-06 00:04:36 +000084 self.m_value += self.m_skip
85 return ret
Sean Callananceabd2d2012-04-06 20:53:23 +000086 def GetNumInstructions(self):
Sean Callanan11064342012-04-06 23:00:31 +000087 return (self.m_last - self.m_start) / self.m_skip
Sean Callanand1817302012-04-06 00:04:36 +000088 def __iter__(self):
89 return self
90 def next(self):
91 ret = self.GetNextInstruction()
92 if ret == None:
93 raise StopIteration
94 return ret
95
96class RandomInstructionProvider:
Sean Callananceabd2d2012-04-06 20:53:23 +000097 def __init__(self, byte_width, log_file):
Sean Callanand1817302012-04-06 00:04:36 +000098 self.m_byte_width = byte_width
Sean Callananceabd2d2012-04-06 20:53:23 +000099 self.m_log_file = log_file
Sean Callanand1817302012-04-06 00:04:36 +0000100 self.m_random_file = open("/dev/random", 'r')
Sean Callananceabd2d2012-04-06 20:53:23 +0000101 def PrintCurrentState(self, ret):
102 ResetLogFile(self.m_log_file)
103 PrintByteArray(self.m_log_file, ret)
Sean Callanand1817302012-04-06 00:04:36 +0000104 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)
Sean Callananceabd2d2012-04-06 20:53:23 +0000108 self.PrintCurrentState(ret)
Sean Callanand1817302012-04-06 00:04:36 +0000109 return ret
110 def __iter__(self):
111 return self
112 def next(self):
113 ret = self.GetNextInstruction()
114 if ret == None:
115 raise StopIteration
116 return ret
117
Sean Callananceabd2d2012-04-06 20:53:23 +0000118log_file = None
119
Sean Callanand1817302012-04-06 00:04:36 +0000120def GetProviderWithArguments(args):
Sean Callananceabd2d2012-04-06 20:53:23 +0000121 global log_file
122 if args.log != None:
123 log_file = open(args.log, 'w')
124 else:
125 log_file = sys.stdout
Sean Callanand1817302012-04-06 00:04:36 +0000126 instruction_provider = None
127 if args.random == True:
Sean Callananceabd2d2012-04-06 20:53:23 +0000128 instruction_provider = RandomInstructionProvider(args.bytes, log_file)
Sean Callanand1817302012-04-06 00:04:36 +0000129 else:
130 start = 0
131 skip = 1
132 if args.start != None:
133 start = args.start
134 if args.skip != None:
135 skip = args.skip
Sean Callananceabd2d2012-04-06 20:53:23 +0000136 instruction_provider = SequentialInstructionProvider(args.bytes, log_file, start, skip)
Sean Callanand1817302012-04-06 00:04:36 +0000137 return instruction_provider
138
139instruction_provider = GetProviderWithArguments(arg_ns)
140
141fake_address = lldb.SBAddress()
142
Sean Callananceabd2d2012-04-06 20:53:23 +0000143actually_time = arg_ns.time and not arg_ns.random
144
145if actually_time:
146 num_instructions_logged = 0
147 total_num_instructions = instruction_provider.GetNumInstructions()
148 start_time = time.time()
149
Sean Callanand1817302012-04-06 00:04:36 +0000150for inst_bytes in instruction_provider:
Sean Callananceabd2d2012-04-06 20:53:23 +0000151 if actually_time:
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
Sean Callanand1817302012-04-06 00:04:36 +0000158 inst_list = target.GetInstructions(fake_address, inst_bytes)
159 if not inst_list.IsValid():
Sean Callananceabd2d2012-04-06 20:53:23 +0000160 print >>log_file, "Invalid instruction list"
Sean Callanand1817302012-04-06 00:04:36 +0000161 continue
162 inst = inst_list.GetInstructionAtIndex(0)
163 if not inst.IsValid():
Sean Callananceabd2d2012-04-06 20:53:23 +0000164 print >>log_file, "Invalid instruction"
Sean Callanand1817302012-04-06 00:04:36 +0000165 continue
166 instr_output_stream = lldb.SBStream()
167 inst.GetDescription(instr_output_stream)
Sean Callananceabd2d2012-04-06 20:53:23 +0000168 print >>log_file, instr_output_stream.GetData()