blob: 2c856acee8006a0b6b5e76f42d3e7d334e9dbb1c [file] [log] [blame]
Johnny Chen14097802011-04-28 21:31:18 +00001#
Johnny Chenc6220052011-04-28 23:53:16 +00002# modify-lldb-python.py
Johnny Chen14097802011-04-28 21:31:18 +00003#
4# This script modifies the lldb module (which was automatically generated via
Johnny Chen22e418a2011-04-29 19:22:24 +00005# running swig) to support iteration and/or equality operations for certain lldb
Johnny Chene5637d22011-05-24 21:05:16 +00006# objects, implements truth value testing for certain lldb objects, and adds a
7# global variable 'debugger_unique_id' which is initialized to 0.
Johnny Chen14097802011-04-28 21:31:18 +00008#
9# It also calls SBDebugger.Initialize() to initialize the lldb debugger
10# subsystem.
11#
12
13import sys, re, StringIO
14
15if len (sys.argv) != 2:
16 output_name = "./lldb.py"
17else:
18 output_name = sys.argv[1] + "/lldb.py"
19
20# print "output_name is '" + output_name + "'"
21
22#
23# lldb_iter() should appear before the our first SB* class definition.
24#
25lldb_iter_def = '''
26# ===================================
27# Iterator for lldb container objects
28# ===================================
29def lldb_iter(obj, getsize, getelem):
30 """A generator adaptor to support iteration for lldb container objects."""
31 size = getattr(obj, getsize)
32 elem = getattr(obj, getelem)
33 for i in range(size()):
34 yield elem(i)
35
36'''
37
Johnny Chen14097802011-04-28 21:31:18 +000038# This supports the iteration protocol.
Johnny Chen14097802011-04-28 21:31:18 +000039iter_def = " def __iter__(self): return lldb_iter(self, '%s', '%s')"
40module_iter = " def module_iter(self): return lldb_iter(self, '%s', '%s')"
41breakpoint_iter = " def breakpoint_iter(self): return lldb_iter(self, '%s', '%s')"
Johnny Chen2077f0d2011-05-17 22:14:39 +000042
Johnny Chena79a21c2011-05-16 20:31:18 +000043# Called to implement the built-in function len().
44# Eligible objects are those containers with unambiguous iteration support.
Johnny Chena79a21c2011-05-16 20:31:18 +000045len_def = " def __len__(self): return self.%s()"
Johnny Chen2077f0d2011-05-17 22:14:39 +000046
Johnny Chen3a3d6592011-04-29 19:03:02 +000047# This supports the rich comparison methods of __eq__ and __ne__.
Johnny Chen7616cb92011-05-02 19:05:52 +000048eq_def = " def __eq__(self, other): return isinstance(other, %s) and %s"
Johnny Chen3a3d6592011-04-29 19:03:02 +000049ne_def = " def __ne__(self, other): return not self.__eq__(other)"
Johnny Chen14097802011-04-28 21:31:18 +000050
Johnny Chen2077f0d2011-05-17 22:14:39 +000051# Called to implement truth value testing and the built-in operation bool();
52# should return False or True, or their integer equivalents 0 or 1.
53# Delegate to self.IsValid() if it is defined for the current lldb object.
54nonzero_def = " def __nonzero__(self): return self.IsValid()"
55
Johnny Chen14097802011-04-28 21:31:18 +000056#
Johnny Chena6303ef2011-05-24 22:53:03 +000057# This dictionary defines a mapping from classname to (getsize, getelem) tuple.
Johnny Chen14097802011-04-28 21:31:18 +000058#
59d = { 'SBBreakpoint': ('GetNumLocations', 'GetLocationAtIndex'),
60 'SBCompileUnit': ('GetNumLineEntries', 'GetLineEntryAtIndex'),
61 'SBDebugger': ('GetNumTargets', 'GetTargetAtIndex'),
62 'SBModule': ('GetNumSymbols', 'GetSymbolAtIndex'),
63 'SBProcess': ('GetNumThreads', 'GetThreadAtIndex'),
64 'SBThread': ('GetNumFrames', 'GetFrameAtIndex'),
65
66 'SBInstructionList': ('GetSize', 'GetInstructionAtIndex'),
67 'SBStringList': ('GetSize', 'GetStringAtIndex',),
68 'SBSymbolContextList': ('GetSize', 'GetContextAtIndex'),
69 'SBValueList': ('GetSize', 'GetValueAtIndex'),
70
71 'SBType': ('GetNumberChildren', 'GetChildAtIndex'),
72 'SBValue': ('GetNumChildren', 'GetChildAtIndex'),
73
Johnny Chen08477f52011-05-24 22:57:42 +000074 # SBTarget needs special processing, see below.
Johnny Chen14097802011-04-28 21:31:18 +000075 'SBTarget': {'module': ('GetNumModules', 'GetModuleAtIndex'),
76 'breakpoint': ('GetNumBreakpoints', 'GetBreakpointAtIndex')
77 }
78 }
79
Johnny Chen3a3d6592011-04-29 19:03:02 +000080#
Johnny Chen7616cb92011-05-02 19:05:52 +000081# This dictionary defines a mapping from classname to equality method name(s).
Johnny Chen3a3d6592011-04-29 19:03:02 +000082#
Johnny Chen7616cb92011-05-02 19:05:52 +000083e = { 'SBBreakpoint': ['GetID'],
84 'SBFileSpec': ['GetFilename', 'GetDirectory'],
85 'SBModule': ['GetFileSpec', 'GetUUIDString']
86 }
87
88def list_to_frag(list):
89 """Transform a list to equality program fragment.
90
91 For example, ['GetID'] is transformed to 'self.GetID() == other.GetID()',
92 and ['GetFilename', 'GetDirectory'] to 'self.GetFilename() == other.GetFilename()
93 and self.GetDirectory() == other.GetDirectory()'.
94 """
95 if not list:
96 raise Exception("list should be non-empty")
97 frag = StringIO.StringIO()
98 for i in range(len(list)):
99 if i > 0:
100 frag.write(" and ")
101 frag.write("self.{0}() == other.{0}()".format(list[i]))
102 return frag.getvalue()
Johnny Chen3a3d6592011-04-29 19:03:02 +0000103
Johnny Chen14097802011-04-28 21:31:18 +0000104# The new content will have the iteration protocol defined for our lldb objects.
105new_content = StringIO.StringIO()
106
107with open(output_name, 'r') as f_in:
108 content = f_in.read()
109
110# The pattern for recognizing the beginning of an SB class definition.
111class_pattern = re.compile("^class (SB.*)\(_object\):$")
112
113# The pattern for recognizing the beginning of the __init__ method definition.
114init_pattern = re.compile("^ def __init__\(self, \*args\):")
115
Johnny Chen2077f0d2011-05-17 22:14:39 +0000116# The pattern for recognizing the beginning of the IsValid method definition.
117isvalid_pattern = re.compile("^ def IsValid\(\*args\):")
118
Johnny Chena79a21c2011-05-16 20:31:18 +0000119# These define the states of our finite state machine.
Johnny Chen14097802011-04-28 21:31:18 +0000120NORMAL = 0
121DEFINING_ITERATOR = 1
Johnny Chen3a3d6592011-04-29 19:03:02 +0000122DEFINING_EQUALITY = 2
Johnny Chen14097802011-04-28 21:31:18 +0000123
124# The lldb_iter_def only needs to be inserted once.
125lldb_iter_defined = False;
126
Johnny Chen2077f0d2011-05-17 22:14:39 +0000127# Our FSM begins its life in the NORMAL state, and transitions to the
128# DEFINING_ITERATOR and/or DEFINING_EQUALITY state whenever it encounters the
129# beginning of certain class definitions, see dictionaries 'd' and 'e' above.
130#
131# Note that the two states DEFINING_ITERATOR and DEFINING_EQUALITY are
132# orthogonal in that our FSM can be in one, the other, or both states at the
133# same time. During such time, the FSM is eagerly searching for the __init__
134# method definition in order to insert the appropriate method(s) into the lldb
135# module.
136#
Johnny Chenb72d1772011-05-24 22:29:49 +0000137# The FSM, in all possible states, also checks the current input for IsValid()
138# definition, and inserts a __nonzero__() method definition to implement truth
139# value testing and the built-in operation bool().
Johnny Chen14097802011-04-28 21:31:18 +0000140state = NORMAL
141for line in content.splitlines():
142 if state == NORMAL:
143 match = class_pattern.search(line)
Johnny Chen2077f0d2011-05-17 22:14:39 +0000144 # Inserts the lldb_iter() definition before the first class definition.
Johnny Chen14097802011-04-28 21:31:18 +0000145 if not lldb_iter_defined and match:
146 print >> new_content, lldb_iter_def
147 lldb_iter_defined = True
Johnny Chen2077f0d2011-05-17 22:14:39 +0000148
149 # If we are at the beginning of the class definitions, prepare to
150 # transition to the DEFINING_ITERATOR/DEFINING_EQUALITY state for the
151 # right class names.
Johnny Chen3a3d6592011-04-29 19:03:02 +0000152 if match:
Johnny Chen14097802011-04-28 21:31:18 +0000153 cls = match.group(1)
Johnny Chen3a3d6592011-04-29 19:03:02 +0000154 if cls in d:
155 # Adding support for iteration for the matched SB class.
156 state = (state | DEFINING_ITERATOR)
157 if cls in e:
158 # Adding support for eq and ne for the matched SB class.
159 state = (state | DEFINING_EQUALITY)
160 elif state > NORMAL:
Johnny Chen14097802011-04-28 21:31:18 +0000161 match = init_pattern.search(line)
162 if match:
163 # We found the beginning of the __init__ method definition.
Johnny Chen3a3d6592011-04-29 19:03:02 +0000164 # This is a good spot to insert the iter and/or eq-ne support.
Johnny Chen14097802011-04-28 21:31:18 +0000165 #
166 # But note that SBTarget has two types of iterations.
167 if cls == "SBTarget":
168 print >> new_content, module_iter % (d[cls]['module'])
169 print >> new_content, breakpoint_iter % (d[cls]['breakpoint'])
170 else:
Johnny Chen3a3d6592011-04-29 19:03:02 +0000171 if (state & DEFINING_ITERATOR):
172 print >> new_content, iter_def % d[cls]
Johnny Chena79a21c2011-05-16 20:31:18 +0000173 print >> new_content, len_def % d[cls][0]
Johnny Chen3a3d6592011-04-29 19:03:02 +0000174 if (state & DEFINING_EQUALITY):
Johnny Chen7616cb92011-05-02 19:05:52 +0000175 print >> new_content, eq_def % (cls, list_to_frag(e[cls]))
Johnny Chen3a3d6592011-04-29 19:03:02 +0000176 print >> new_content, ne_def
Johnny Chena2f86e82011-04-29 19:19:13 +0000177
178 # Next state will be NORMAL.
179 state = NORMAL
Johnny Chen14097802011-04-28 21:31:18 +0000180
Johnny Chenb72d1772011-05-24 22:29:49 +0000181 # Look for 'def IsValid(*args):', and once located, add implementation
182 # of truth value testing for this object by delegation.
183 if isvalid_pattern.search(line):
184 print >> new_content, nonzero_def
185
Johnny Chen6ea16c72011-05-02 17:53:04 +0000186 # Pass the original line of content to new_content.
Johnny Chen14097802011-04-28 21:31:18 +0000187 print >> new_content, line
188
189with open(output_name, 'w') as f_out:
190 f_out.write(new_content.getvalue())
191 f_out.write("debugger_unique_id = 0\n")
192 f_out.write("SBDebugger.Initialize()\n")