blob: 44aaf84d9b5390ff13d09e7d1828f17f04626024 [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#
Johnny Chen09e0a422011-07-01 22:14:07 +00009# As a cleanup step, it also removes the 'residues' from the autodoc features of
10# swig. For an example, take a look at SBTarget.h header file, where we take
Johnny Chen2c77fa42011-07-02 20:01:09 +000011# advantage of the already existing doxygen C++-docblock and make it the Python
Johnny Chen09e0a422011-07-01 22:14:07 +000012# docstring for the same method. The 'residues' in this context include the
Johnny Chenf6ce70a2011-07-03 19:55:50 +000013# '#endif', the '#ifdef SWIG', the c comment marker, the trailing blank (SPC's)
14# line, and the doxygen comment start marker.
Johnny Chen09e0a422011-07-01 22:14:07 +000015#
Johnny Chen14097802011-04-28 21:31:18 +000016# It also calls SBDebugger.Initialize() to initialize the lldb debugger
17# subsystem.
18#
19
20import sys, re, StringIO
21
22if len (sys.argv) != 2:
23 output_name = "./lldb.py"
24else:
25 output_name = sys.argv[1] + "/lldb.py"
26
27# print "output_name is '" + output_name + "'"
28
Johnny Chen09e0a422011-07-01 22:14:07 +000029# Residues to be removed.
30c_endif_swig = "#endif"
31c_ifdef_swig = "#ifdef SWIG"
Johnny Chen2c77fa42011-07-02 20:01:09 +000032c_comment_marker = "//------------"
Johnny Chenf451e302011-07-03 01:43:29 +000033trailing_blank_line = ' '
Johnny Chen2c77fa42011-07-02 20:01:09 +000034# The pattern for recognizing the doxygen comment block line.
35doxygen_comment_start = re.compile("^\s*( /// ?)")
Johnny Chenf6ce70a2011-07-03 19:55:50 +000036# The demarcation point for turning on/off residue removal state.
37# When bracketed by the lines, the CLEANUP_DOCSTRING state (see below) is ON.
38toggle_docstring_cleanup_line = ' """'
Johnny Chen09e0a422011-07-01 22:14:07 +000039
Johnny Chen14097802011-04-28 21:31:18 +000040#
Johnny Chenec5e0a22011-06-01 18:40:11 +000041# lldb_iter() should appear before our first SB* class definition.
Johnny Chen14097802011-04-28 21:31:18 +000042#
43lldb_iter_def = '''
44# ===================================
45# Iterator for lldb container objects
46# ===================================
47def lldb_iter(obj, getsize, getelem):
48 """A generator adaptor to support iteration for lldb container objects."""
49 size = getattr(obj, getsize)
50 elem = getattr(obj, getelem)
51 for i in range(size()):
52 yield elem(i)
53
Johnny Chen81422202011-06-01 19:21:08 +000054# ==============================================================================
55# The modify-python-lldb.py script is responsible for post-processing this SWIG-
56# generated lldb.py module. It is responsible for adding the above lldb_iter()
57# function definition as well as the supports, in the following, for iteration
58# protocol: __iter__, rich comparison methods: __eq__ and __ne__, truth value
59# testing (and built-in operation bool()): __nonzero__, and built-in function
60# len(): __len__.
61# ==============================================================================
Johnny Chen14097802011-04-28 21:31:18 +000062'''
63
Johnny Chen14097802011-04-28 21:31:18 +000064# This supports the iteration protocol.
Johnny Chen14097802011-04-28 21:31:18 +000065iter_def = " def __iter__(self): return lldb_iter(self, '%s', '%s')"
66module_iter = " def module_iter(self): return lldb_iter(self, '%s', '%s')"
67breakpoint_iter = " def breakpoint_iter(self): return lldb_iter(self, '%s', '%s')"
Johnny Chen2077f0d2011-05-17 22:14:39 +000068
Johnny Chena79a21c2011-05-16 20:31:18 +000069# Called to implement the built-in function len().
70# Eligible objects are those containers with unambiguous iteration support.
Johnny Chena79a21c2011-05-16 20:31:18 +000071len_def = " def __len__(self): return self.%s()"
Johnny Chen2077f0d2011-05-17 22:14:39 +000072
Johnny Chen3a3d6592011-04-29 19:03:02 +000073# This supports the rich comparison methods of __eq__ and __ne__.
Johnny Chen7616cb92011-05-02 19:05:52 +000074eq_def = " def __eq__(self, other): return isinstance(other, %s) and %s"
Johnny Chen3a3d6592011-04-29 19:03:02 +000075ne_def = " def __ne__(self, other): return not self.__eq__(other)"
Johnny Chen14097802011-04-28 21:31:18 +000076
Johnny Chen2077f0d2011-05-17 22:14:39 +000077# Called to implement truth value testing and the built-in operation bool();
78# should return False or True, or their integer equivalents 0 or 1.
79# Delegate to self.IsValid() if it is defined for the current lldb object.
80nonzero_def = " def __nonzero__(self): return self.IsValid()"
81
Johnny Chen14097802011-04-28 21:31:18 +000082#
Johnny Chena6303ef2011-05-24 22:53:03 +000083# This dictionary defines a mapping from classname to (getsize, getelem) tuple.
Johnny Chen14097802011-04-28 21:31:18 +000084#
85d = { 'SBBreakpoint': ('GetNumLocations', 'GetLocationAtIndex'),
86 'SBCompileUnit': ('GetNumLineEntries', 'GetLineEntryAtIndex'),
87 'SBDebugger': ('GetNumTargets', 'GetTargetAtIndex'),
88 'SBModule': ('GetNumSymbols', 'GetSymbolAtIndex'),
89 'SBProcess': ('GetNumThreads', 'GetThreadAtIndex'),
90 'SBThread': ('GetNumFrames', 'GetFrameAtIndex'),
91
92 'SBInstructionList': ('GetSize', 'GetInstructionAtIndex'),
93 'SBStringList': ('GetSize', 'GetStringAtIndex',),
94 'SBSymbolContextList': ('GetSize', 'GetContextAtIndex'),
95 'SBValueList': ('GetSize', 'GetValueAtIndex'),
96
97 'SBType': ('GetNumberChildren', 'GetChildAtIndex'),
98 'SBValue': ('GetNumChildren', 'GetChildAtIndex'),
99
Johnny Chen08477f52011-05-24 22:57:42 +0000100 # SBTarget needs special processing, see below.
Johnny Chen14097802011-04-28 21:31:18 +0000101 'SBTarget': {'module': ('GetNumModules', 'GetModuleAtIndex'),
102 'breakpoint': ('GetNumBreakpoints', 'GetBreakpointAtIndex')
103 }
104 }
105
Johnny Chen3a3d6592011-04-29 19:03:02 +0000106#
Johnny Chen7616cb92011-05-02 19:05:52 +0000107# This dictionary defines a mapping from classname to equality method name(s).
Johnny Chen3a3d6592011-04-29 19:03:02 +0000108#
Johnny Chen694cfd02011-06-09 22:04:56 +0000109e = { 'SBAddress': ['GetFileAddress', 'GetModule'],
110 'SBBreakpoint': ['GetID'],
Johnny Chen7616cb92011-05-02 19:05:52 +0000111 'SBFileSpec': ['GetFilename', 'GetDirectory'],
112 'SBModule': ['GetFileSpec', 'GetUUIDString']
113 }
114
115def list_to_frag(list):
116 """Transform a list to equality program fragment.
117
118 For example, ['GetID'] is transformed to 'self.GetID() == other.GetID()',
119 and ['GetFilename', 'GetDirectory'] to 'self.GetFilename() == other.GetFilename()
120 and self.GetDirectory() == other.GetDirectory()'.
121 """
122 if not list:
123 raise Exception("list should be non-empty")
124 frag = StringIO.StringIO()
125 for i in range(len(list)):
126 if i > 0:
127 frag.write(" and ")
128 frag.write("self.{0}() == other.{0}()".format(list[i]))
129 return frag.getvalue()
Johnny Chen3a3d6592011-04-29 19:03:02 +0000130
Johnny Chen14097802011-04-28 21:31:18 +0000131# The new content will have the iteration protocol defined for our lldb objects.
132new_content = StringIO.StringIO()
133
134with open(output_name, 'r') as f_in:
135 content = f_in.read()
136
137# The pattern for recognizing the beginning of an SB class definition.
138class_pattern = re.compile("^class (SB.*)\(_object\):$")
139
140# The pattern for recognizing the beginning of the __init__ method definition.
141init_pattern = re.compile("^ def __init__\(self, \*args\):")
142
Johnny Chen2077f0d2011-05-17 22:14:39 +0000143# The pattern for recognizing the beginning of the IsValid method definition.
Peter Collingbournef2084532011-06-14 03:55:41 +0000144isvalid_pattern = re.compile("^ def IsValid\(")
Johnny Chen2077f0d2011-05-17 22:14:39 +0000145
Johnny Chena79a21c2011-05-16 20:31:18 +0000146# These define the states of our finite state machine.
Johnny Chen14097802011-04-28 21:31:18 +0000147NORMAL = 0
148DEFINING_ITERATOR = 1
Johnny Chen3a3d6592011-04-29 19:03:02 +0000149DEFINING_EQUALITY = 2
Johnny Chenf6ce70a2011-07-03 19:55:50 +0000150CLEANUP_DOCSTRING = 4
Johnny Chen14097802011-04-28 21:31:18 +0000151
152# The lldb_iter_def only needs to be inserted once.
153lldb_iter_defined = False;
154
Johnny Chen2077f0d2011-05-17 22:14:39 +0000155# Our FSM begins its life in the NORMAL state, and transitions to the
156# DEFINING_ITERATOR and/or DEFINING_EQUALITY state whenever it encounters the
157# beginning of certain class definitions, see dictionaries 'd' and 'e' above.
158#
159# Note that the two states DEFINING_ITERATOR and DEFINING_EQUALITY are
160# orthogonal in that our FSM can be in one, the other, or both states at the
161# same time. During such time, the FSM is eagerly searching for the __init__
162# method definition in order to insert the appropriate method(s) into the lldb
163# module.
164#
Johnny Chenf6ce70a2011-07-03 19:55:50 +0000165# The state CLEANUP_DOCSTRING can be entered from either the NORMAL or the
166# DEFINING_ITERATOR/EQUALITY states. While in this state, the FSM is fixing/
167# cleaning the Python docstrings generated by the swig docstring features.
168#
Johnny Chenb72d1772011-05-24 22:29:49 +0000169# The FSM, in all possible states, also checks the current input for IsValid()
170# definition, and inserts a __nonzero__() method definition to implement truth
171# value testing and the built-in operation bool().
Johnny Chen14097802011-04-28 21:31:18 +0000172state = NORMAL
173for line in content.splitlines():
Johnny Chenf6ce70a2011-07-03 19:55:50 +0000174 # Handle the state transition into CLEANUP_DOCSTRING state as it is possible
175 # to enter this state from either NORMAL or DEFINING_ITERATOR/EQUALITY.
176 #
177 # If ' """' is the sole line, prepare to transition to the
178 # CLEANUP_DOCSTRING state or out of it.
179 if line == toggle_docstring_cleanup_line:
180 if state & CLEANUP_DOCSTRING:
181 state ^= CLEANUP_DOCSTRING
182 else:
183 state |= CLEANUP_DOCSTRING
Johnny Chen09e0a422011-07-01 22:14:07 +0000184
Johnny Chen14097802011-04-28 21:31:18 +0000185 if state == NORMAL:
186 match = class_pattern.search(line)
Johnny Chen2077f0d2011-05-17 22:14:39 +0000187 # Inserts the lldb_iter() definition before the first class definition.
Johnny Chen14097802011-04-28 21:31:18 +0000188 if not lldb_iter_defined and match:
189 print >> new_content, lldb_iter_def
190 lldb_iter_defined = True
Johnny Chen2077f0d2011-05-17 22:14:39 +0000191
192 # If we are at the beginning of the class definitions, prepare to
193 # transition to the DEFINING_ITERATOR/DEFINING_EQUALITY state for the
194 # right class names.
Johnny Chen3a3d6592011-04-29 19:03:02 +0000195 if match:
Johnny Chen14097802011-04-28 21:31:18 +0000196 cls = match.group(1)
Johnny Chen3a3d6592011-04-29 19:03:02 +0000197 if cls in d:
198 # Adding support for iteration for the matched SB class.
Johnny Chenf6ce70a2011-07-03 19:55:50 +0000199 state |= DEFINING_ITERATOR
Johnny Chen3a3d6592011-04-29 19:03:02 +0000200 if cls in e:
201 # Adding support for eq and ne for the matched SB class.
Johnny Chenf6ce70a2011-07-03 19:55:50 +0000202 state |= DEFINING_EQUALITY
203
204 elif (state & DEFINING_ITERATOR) or (state & DEFINING_EQUALITY):
Johnny Chen14097802011-04-28 21:31:18 +0000205 match = init_pattern.search(line)
206 if match:
207 # We found the beginning of the __init__ method definition.
Johnny Chen3a3d6592011-04-29 19:03:02 +0000208 # This is a good spot to insert the iter and/or eq-ne support.
Johnny Chen14097802011-04-28 21:31:18 +0000209 #
210 # But note that SBTarget has two types of iterations.
211 if cls == "SBTarget":
212 print >> new_content, module_iter % (d[cls]['module'])
213 print >> new_content, breakpoint_iter % (d[cls]['breakpoint'])
214 else:
Johnny Chen3a3d6592011-04-29 19:03:02 +0000215 if (state & DEFINING_ITERATOR):
216 print >> new_content, iter_def % d[cls]
Johnny Chena79a21c2011-05-16 20:31:18 +0000217 print >> new_content, len_def % d[cls][0]
Johnny Chen3a3d6592011-04-29 19:03:02 +0000218 if (state & DEFINING_EQUALITY):
Johnny Chen7616cb92011-05-02 19:05:52 +0000219 print >> new_content, eq_def % (cls, list_to_frag(e[cls]))
Johnny Chen3a3d6592011-04-29 19:03:02 +0000220 print >> new_content, ne_def
Johnny Chena2f86e82011-04-29 19:19:13 +0000221
222 # Next state will be NORMAL.
223 state = NORMAL
Johnny Chen14097802011-04-28 21:31:18 +0000224
Johnny Chenf6ce70a2011-07-03 19:55:50 +0000225 elif (state & CLEANUP_DOCSTRING):
226 # Cleanse the lldb.py of the autodoc'ed residues.
227 if c_ifdef_swig in line or c_endif_swig in line:
228 continue
229 # As well as the comment marker line and trailing blank line.
230 if c_comment_marker in line or line == trailing_blank_line:
231 continue
232 # Also remove the '\a ' substrings.
233 line = line.replace('\a ', '')
234 # And the leading '///' substring.
235 doxygen_comment_match = doxygen_comment_start.match(line)
236 if doxygen_comment_match:
237 line = line.replace(doxygen_comment_match.group(1), '', 1)
238
239 # Note that the transition out of CLEANUP_DOCSTRING is handled at the
240 # beginning of this function already.
241
Johnny Chenb72d1772011-05-24 22:29:49 +0000242 # Look for 'def IsValid(*args):', and once located, add implementation
243 # of truth value testing for this object by delegation.
244 if isvalid_pattern.search(line):
245 print >> new_content, nonzero_def
246
Johnny Chen6ea16c72011-05-02 17:53:04 +0000247 # Pass the original line of content to new_content.
Johnny Chen14097802011-04-28 21:31:18 +0000248 print >> new_content, line
249
250with open(output_name, 'w') as f_out:
251 f_out.write(new_content.getvalue())
252 f_out.write("debugger_unique_id = 0\n")
253 f_out.write("SBDebugger.Initialize()\n")