blob: 004c656fbbd49ffbe2ff90f1b53f9857f92ff189 [file] [log] [blame]
Sean Callanand1817302012-04-06 00:04:36 +00001#!/usr/bin/python
2
3import argparse, re, subprocess, sys
4
5#-- This code fragment loads LLDB --
6
7def 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
24AddLLDBToSysPathOnMacOSX()
25
26import lldb
27
28#-- End of code fragment that loads LLDB --
29
30parser = argparse.ArgumentParser(description="Run an exhaustive test of the LLDB disassembler for a specific architecture.")
31
32parser.add_argument('--arch', required=True, action='store', help='The architecture whose disassembler is to be tested')
33parser.add_argument('--bytes', required=True, action='store', type=int, help='The byte width of instructions for that architecture')
34parser.add_argument('--random', required=False, action='store_true', help='Enables non-sequential testing')
35parser.add_argument('--start', required=False, action='store', type=int, help='The first instruction value to test')
36parser.add_argument('--skip', required=False, action='store', type=int, help='The interval between instructions to test')
37
38arguments = sys.argv[1:]
39
40arg_ns = parser.parse_args(arguments)
41
42debugger = lldb.SBDebugger.Create()
43
44if debugger.IsValid() == False:
45 print "Couldn't create an SBDebugger"
46 sys.exit(-1)
47
48target = debugger.CreateTargetWithFileAndArch(None, arg_ns.arch)
49
50if target.IsValid() == False:
51 print "Couldn't create an SBTarget for architecture " + arg_ns.arch
52 sys.exit(-1)
53
54class 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
80class 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
102def 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
116instruction_provider = GetProviderWithArguments(arg_ns)
117
118fake_address = lldb.SBAddress()
119
120for 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()