annotate lldb/examples/python/disasm-stress-test.py @ 227:21e6aa2e49ef

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 19 Jul 2021 06:57:16 +0900
parents 2e18cbf3894f
children 1f2b6ac9f198
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1 #!/usr/bin/env python
150
anatofuz
parents:
diff changeset
2
anatofuz
parents:
diff changeset
3 import argparse
anatofuz
parents:
diff changeset
4 import datetime
anatofuz
parents:
diff changeset
5 import re
anatofuz
parents:
diff changeset
6 import subprocess
anatofuz
parents:
diff changeset
7 import sys
anatofuz
parents:
diff changeset
8 import time
anatofuz
parents:
diff changeset
9
anatofuz
parents:
diff changeset
10 parser = argparse.ArgumentParser(
anatofuz
parents:
diff changeset
11 description="Run an exhaustive test of the LLDB disassembler for a specific architecture.")
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 parser.add_argument(
anatofuz
parents:
diff changeset
14 '--arch',
anatofuz
parents:
diff changeset
15 required=True,
anatofuz
parents:
diff changeset
16 action='store',
anatofuz
parents:
diff changeset
17 help='The architecture whose disassembler is to be tested')
anatofuz
parents:
diff changeset
18 parser.add_argument(
anatofuz
parents:
diff changeset
19 '--bytes',
anatofuz
parents:
diff changeset
20 required=True,
anatofuz
parents:
diff changeset
21 action='store',
anatofuz
parents:
diff changeset
22 type=int,
anatofuz
parents:
diff changeset
23 help='The byte width of instructions for that architecture')
anatofuz
parents:
diff changeset
24 parser.add_argument(
anatofuz
parents:
diff changeset
25 '--random',
anatofuz
parents:
diff changeset
26 required=False,
anatofuz
parents:
diff changeset
27 action='store_true',
anatofuz
parents:
diff changeset
28 help='Enables non-sequential testing')
anatofuz
parents:
diff changeset
29 parser.add_argument(
anatofuz
parents:
diff changeset
30 '--start',
anatofuz
parents:
diff changeset
31 required=False,
anatofuz
parents:
diff changeset
32 action='store',
anatofuz
parents:
diff changeset
33 type=int,
anatofuz
parents:
diff changeset
34 help='The first instruction value to test')
anatofuz
parents:
diff changeset
35 parser.add_argument(
anatofuz
parents:
diff changeset
36 '--skip',
anatofuz
parents:
diff changeset
37 required=False,
anatofuz
parents:
diff changeset
38 action='store',
anatofuz
parents:
diff changeset
39 type=int,
anatofuz
parents:
diff changeset
40 help='The interval between instructions to test')
anatofuz
parents:
diff changeset
41 parser.add_argument(
anatofuz
parents:
diff changeset
42 '--log',
anatofuz
parents:
diff changeset
43 required=False,
anatofuz
parents:
diff changeset
44 action='store',
anatofuz
parents:
diff changeset
45 help='A log file to write the most recent instruction being tested')
anatofuz
parents:
diff changeset
46 parser.add_argument(
anatofuz
parents:
diff changeset
47 '--time',
anatofuz
parents:
diff changeset
48 required=False,
anatofuz
parents:
diff changeset
49 action='store_true',
anatofuz
parents:
diff changeset
50 help='Every 100,000 instructions, print an ETA to standard out')
anatofuz
parents:
diff changeset
51 parser.add_argument(
anatofuz
parents:
diff changeset
52 '--lldb',
anatofuz
parents:
diff changeset
53 required=False,
anatofuz
parents:
diff changeset
54 action='store',
anatofuz
parents:
diff changeset
55 help='The path to LLDB.framework, if LLDB should be overridden')
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 arguments = sys.argv[1:]
anatofuz
parents:
diff changeset
58
anatofuz
parents:
diff changeset
59 arg_ns = parser.parse_args(arguments)
anatofuz
parents:
diff changeset
60
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 def AddLLDBToSysPathOnMacOSX():
anatofuz
parents:
diff changeset
63 def GetLLDBFrameworkPath():
anatofuz
parents:
diff changeset
64 lldb_path = subprocess.check_output(["xcrun", "-find", "lldb"])
anatofuz
parents:
diff changeset
65 re_result = re.match("(.*)/Developer/usr/bin/lldb", lldb_path)
anatofuz
parents:
diff changeset
66 if re_result is None:
anatofuz
parents:
diff changeset
67 return None
anatofuz
parents:
diff changeset
68 xcode_contents_path = re_result.group(1)
anatofuz
parents:
diff changeset
69 return xcode_contents_path + "/SharedFrameworks/LLDB.framework"
anatofuz
parents:
diff changeset
70
anatofuz
parents:
diff changeset
71 lldb_framework_path = GetLLDBFrameworkPath()
anatofuz
parents:
diff changeset
72
anatofuz
parents:
diff changeset
73 if lldb_framework_path is None:
anatofuz
parents:
diff changeset
74 print("Couldn't find LLDB.framework")
anatofuz
parents:
diff changeset
75 sys.exit(-1)
anatofuz
parents:
diff changeset
76
anatofuz
parents:
diff changeset
77 sys.path.append(lldb_framework_path + "/Resources/Python")
anatofuz
parents:
diff changeset
78
anatofuz
parents:
diff changeset
79 if arg_ns.lldb is None:
anatofuz
parents:
diff changeset
80 AddLLDBToSysPathOnMacOSX()
anatofuz
parents:
diff changeset
81 else:
anatofuz
parents:
diff changeset
82 sys.path.append(arg_ns.lldb + "/Resources/Python")
anatofuz
parents:
diff changeset
83
anatofuz
parents:
diff changeset
84 import lldb
anatofuz
parents:
diff changeset
85
anatofuz
parents:
diff changeset
86 debugger = lldb.SBDebugger.Create()
anatofuz
parents:
diff changeset
87
anatofuz
parents:
diff changeset
88 if debugger.IsValid() == False:
anatofuz
parents:
diff changeset
89 print("Couldn't create an SBDebugger")
anatofuz
parents:
diff changeset
90 sys.exit(-1)
anatofuz
parents:
diff changeset
91
anatofuz
parents:
diff changeset
92 target = debugger.CreateTargetWithFileAndArch(None, arg_ns.arch)
anatofuz
parents:
diff changeset
93
anatofuz
parents:
diff changeset
94 if target.IsValid() == False:
anatofuz
parents:
diff changeset
95 print("Couldn't create an SBTarget for architecture " + arg_ns.arch)
anatofuz
parents:
diff changeset
96 sys.exit(-1)
anatofuz
parents:
diff changeset
97
anatofuz
parents:
diff changeset
98
anatofuz
parents:
diff changeset
99 def ResetLogFile(log_file):
anatofuz
parents:
diff changeset
100 if log_file != sys.stdout:
anatofuz
parents:
diff changeset
101 log_file.seek(0)
anatofuz
parents:
diff changeset
102
anatofuz
parents:
diff changeset
103
anatofuz
parents:
diff changeset
104 def PrintByteArray(log_file, byte_array):
anatofuz
parents:
diff changeset
105 for byte in byte_array:
anatofuz
parents:
diff changeset
106 print(hex(byte) + " ", end=' ', file=log_file)
anatofuz
parents:
diff changeset
107 print(file=log_file)
anatofuz
parents:
diff changeset
108
anatofuz
parents:
diff changeset
109
anatofuz
parents:
diff changeset
110 class SequentialInstructionProvider:
anatofuz
parents:
diff changeset
111
anatofuz
parents:
diff changeset
112 def __init__(self, byte_width, log_file, start=0, skip=1):
anatofuz
parents:
diff changeset
113 self.m_byte_width = byte_width
anatofuz
parents:
diff changeset
114 self.m_log_file = log_file
anatofuz
parents:
diff changeset
115 self.m_start = start
anatofuz
parents:
diff changeset
116 self.m_skip = skip
anatofuz
parents:
diff changeset
117 self.m_value = start
anatofuz
parents:
diff changeset
118 self.m_last = (1 << (byte_width * 8)) - 1
anatofuz
parents:
diff changeset
119
anatofuz
parents:
diff changeset
120 def PrintCurrentState(self, ret):
anatofuz
parents:
diff changeset
121 ResetLogFile(self.m_log_file)
anatofuz
parents:
diff changeset
122 print(self.m_value, file=self.m_log_file)
anatofuz
parents:
diff changeset
123 PrintByteArray(self.m_log_file, ret)
anatofuz
parents:
diff changeset
124
anatofuz
parents:
diff changeset
125 def GetNextInstruction(self):
anatofuz
parents:
diff changeset
126 if self.m_value > self.m_last:
anatofuz
parents:
diff changeset
127 return None
anatofuz
parents:
diff changeset
128 ret = bytearray(self.m_byte_width)
anatofuz
parents:
diff changeset
129 for i in range(self.m_byte_width):
anatofuz
parents:
diff changeset
130 ret[self.m_byte_width - (i + 1)] = (self.m_value >> (i * 8)) & 255
anatofuz
parents:
diff changeset
131 self.PrintCurrentState(ret)
anatofuz
parents:
diff changeset
132 self.m_value += self.m_skip
anatofuz
parents:
diff changeset
133 return ret
anatofuz
parents:
diff changeset
134
anatofuz
parents:
diff changeset
135 def GetNumInstructions(self):
anatofuz
parents:
diff changeset
136 return (self.m_last - self.m_start) / self.m_skip
anatofuz
parents:
diff changeset
137
anatofuz
parents:
diff changeset
138 def __iter__(self):
anatofuz
parents:
diff changeset
139 return self
anatofuz
parents:
diff changeset
140
anatofuz
parents:
diff changeset
141 def next(self):
anatofuz
parents:
diff changeset
142 ret = self.GetNextInstruction()
anatofuz
parents:
diff changeset
143 if ret is None:
anatofuz
parents:
diff changeset
144 raise StopIteration
anatofuz
parents:
diff changeset
145 return ret
anatofuz
parents:
diff changeset
146
anatofuz
parents:
diff changeset
147
anatofuz
parents:
diff changeset
148 class RandomInstructionProvider:
anatofuz
parents:
diff changeset
149
anatofuz
parents:
diff changeset
150 def __init__(self, byte_width, log_file):
anatofuz
parents:
diff changeset
151 self.m_byte_width = byte_width
anatofuz
parents:
diff changeset
152 self.m_log_file = log_file
anatofuz
parents:
diff changeset
153 self.m_random_file = open("/dev/random", 'r')
anatofuz
parents:
diff changeset
154
anatofuz
parents:
diff changeset
155 def PrintCurrentState(self, ret):
anatofuz
parents:
diff changeset
156 ResetLogFile(self.m_log_file)
anatofuz
parents:
diff changeset
157 PrintByteArray(self.m_log_file, ret)
anatofuz
parents:
diff changeset
158
anatofuz
parents:
diff changeset
159 def GetNextInstruction(self):
anatofuz
parents:
diff changeset
160 ret = bytearray(self.m_byte_width)
anatofuz
parents:
diff changeset
161 for i in range(self.m_byte_width):
anatofuz
parents:
diff changeset
162 ret[i] = self.m_random_file.read(1)
anatofuz
parents:
diff changeset
163 self.PrintCurrentState(ret)
anatofuz
parents:
diff changeset
164 return ret
anatofuz
parents:
diff changeset
165
anatofuz
parents:
diff changeset
166 def __iter__(self):
anatofuz
parents:
diff changeset
167 return self
anatofuz
parents:
diff changeset
168
anatofuz
parents:
diff changeset
169 def next(self):
anatofuz
parents:
diff changeset
170 ret = self.GetNextInstruction()
anatofuz
parents:
diff changeset
171 if ret is None:
anatofuz
parents:
diff changeset
172 raise StopIteration
anatofuz
parents:
diff changeset
173 return ret
anatofuz
parents:
diff changeset
174
anatofuz
parents:
diff changeset
175 log_file = None
anatofuz
parents:
diff changeset
176
anatofuz
parents:
diff changeset
177
anatofuz
parents:
diff changeset
178 def GetProviderWithArguments(args):
anatofuz
parents:
diff changeset
179 global log_file
anatofuz
parents:
diff changeset
180 if args.log is not None:
anatofuz
parents:
diff changeset
181 log_file = open(args.log, 'w')
anatofuz
parents:
diff changeset
182 else:
anatofuz
parents:
diff changeset
183 log_file = sys.stdout
anatofuz
parents:
diff changeset
184 instruction_provider = None
anatofuz
parents:
diff changeset
185 if args.random:
anatofuz
parents:
diff changeset
186 instruction_provider = RandomInstructionProvider(args.bytes, log_file)
anatofuz
parents:
diff changeset
187 else:
anatofuz
parents:
diff changeset
188 start = 0
anatofuz
parents:
diff changeset
189 skip = 1
anatofuz
parents:
diff changeset
190 if args.start is not None:
anatofuz
parents:
diff changeset
191 start = args.start
anatofuz
parents:
diff changeset
192 if args.skip is not None:
anatofuz
parents:
diff changeset
193 skip = args.skip
anatofuz
parents:
diff changeset
194 instruction_provider = SequentialInstructionProvider(
anatofuz
parents:
diff changeset
195 args.bytes, log_file, start, skip)
anatofuz
parents:
diff changeset
196 return instruction_provider
anatofuz
parents:
diff changeset
197
anatofuz
parents:
diff changeset
198 instruction_provider = GetProviderWithArguments(arg_ns)
anatofuz
parents:
diff changeset
199
anatofuz
parents:
diff changeset
200 fake_address = lldb.SBAddress()
anatofuz
parents:
diff changeset
201
anatofuz
parents:
diff changeset
202 actually_time = arg_ns.time and not arg_ns.random
anatofuz
parents:
diff changeset
203
anatofuz
parents:
diff changeset
204 if actually_time:
anatofuz
parents:
diff changeset
205 num_instructions_logged = 0
anatofuz
parents:
diff changeset
206 total_num_instructions = instruction_provider.GetNumInstructions()
anatofuz
parents:
diff changeset
207 start_time = time.time()
anatofuz
parents:
diff changeset
208
anatofuz
parents:
diff changeset
209 for inst_bytes in instruction_provider:
anatofuz
parents:
diff changeset
210 if actually_time:
anatofuz
parents:
diff changeset
211 if (num_instructions_logged != 0) and (
anatofuz
parents:
diff changeset
212 num_instructions_logged % 100000 == 0):
anatofuz
parents:
diff changeset
213 curr_time = time.time()
anatofuz
parents:
diff changeset
214 elapsed_time = curr_time - start_time
anatofuz
parents:
diff changeset
215 remaining_time = float(
anatofuz
parents:
diff changeset
216 total_num_instructions - num_instructions_logged) * (
anatofuz
parents:
diff changeset
217 float(elapsed_time) / float(num_instructions_logged))
anatofuz
parents:
diff changeset
218 print(str(datetime.timedelta(seconds=remaining_time)))
anatofuz
parents:
diff changeset
219 num_instructions_logged = num_instructions_logged + 1
anatofuz
parents:
diff changeset
220 inst_list = target.GetInstructions(fake_address, inst_bytes)
anatofuz
parents:
diff changeset
221 if not inst_list.IsValid():
anatofuz
parents:
diff changeset
222 print("Invalid instruction list", file=log_file)
anatofuz
parents:
diff changeset
223 continue
anatofuz
parents:
diff changeset
224 inst = inst_list.GetInstructionAtIndex(0)
anatofuz
parents:
diff changeset
225 if not inst.IsValid():
anatofuz
parents:
diff changeset
226 print("Invalid instruction", file=log_file)
anatofuz
parents:
diff changeset
227 continue
anatofuz
parents:
diff changeset
228 instr_output_stream = lldb.SBStream()
anatofuz
parents:
diff changeset
229 inst.GetDescription(instr_output_stream)
anatofuz
parents:
diff changeset
230 print(instr_output_stream.GetData(), file=log_file)