]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - examples/python/disasm-stress-test.py
Vendor import of lldb trunk r256945:
[FreeBSD/FreeBSD.git] / examples / python / disasm-stress-test.py
1 #!/usr/bin/python
2
3 import argparse, datetime, re, subprocess, sys, time
4
5 parser = argparse.ArgumentParser(description="Run an exhaustive test of the LLDB disassembler for a specific architecture.")
6
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')
15
16 arguments = sys.argv[1:]
17
18 arg_ns = parser.parse_args(arguments)
19
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)
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
37 if arg_ns.lldb == None:
38     AddLLDBToSysPathOnMacOSX()
39 else:
40     sys.path.append(arg_ns.lldb + "/Resources/Python")
41
42 import lldb
43
44 debugger = lldb.SBDebugger.Create()
45
46 if debugger.IsValid() == False:
47     print "Couldn't create an SBDebugger"
48     sys.exit(-1)
49
50 target = debugger.CreateTargetWithFileAndArch(None, arg_ns.arch)
51
52 if target.IsValid() == False:
53     print "Couldn't create an SBTarget for architecture " + arg_ns.arch
54     sys.exit(-1)
55
56 def ResetLogFile(log_file):
57     if log_file != sys.stdout:
58         log_file.seek(0)
59
60 def PrintByteArray(log_file, byte_array):
61     for byte in byte_array:
62         print >>log_file, hex(byte) + " ",
63     print >>log_file
64     
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
69         self.m_start = start
70         self.m_skip = skip
71         self.m_value = start
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:
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 
83         self.PrintCurrentState(ret)
84         self.m_value += self.m_skip
85         return ret
86     def GetNumInstructions(self):
87         return (self.m_last - self.m_start) / self.m_skip
88     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
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)
109         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
118 log_file = None
119
120 def GetProviderWithArguments(args):
121     global log_file
122     if args.log != None:
123         log_file = open(args.log, 'w')
124     else:
125         log_file = sys.stdout
126     instruction_provider = None
127     if args.random == True:
128         instruction_provider = RandomInstructionProvider(args.bytes, log_file)
129     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
136         instruction_provider = SequentialInstructionProvider(args.bytes, log_file, start, skip)
137     return instruction_provider
138
139 instruction_provider = GetProviderWithArguments(arg_ns)
140
141 fake_address = lldb.SBAddress()
142
143 actually_time = arg_ns.time and not arg_ns.random
144
145 if actually_time:
146     num_instructions_logged = 0
147     total_num_instructions = instruction_provider.GetNumInstructions()
148     start_time = time.time()
149
150 for inst_bytes in instruction_provider:
151     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
158     inst_list = target.GetInstructions(fake_address, inst_bytes)
159     if not inst_list.IsValid():
160         print >>log_file, "Invalid instruction list"
161         continue
162     inst = inst_list.GetInstructionAtIndex(0)
163     if not inst.IsValid():
164         print >>log_file, "Invalid instruction"
165         continue
166     instr_output_stream = lldb.SBStream()
167     inst.GetDescription(instr_output_stream)
168     print >>log_file, instr_output_stream.GetData()