Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 1 | # |
Johnny Chen | c622005 | 2011-04-28 23:53:16 +0000 | [diff] [blame] | 2 | # modify-lldb-python.py |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 3 | # |
| 4 | # This script modifies the lldb module (which was automatically generated via |
Johnny Chen | 22e418a | 2011-04-29 19:22:24 +0000 | [diff] [blame] | 5 | # running swig) to support iteration and/or equality operations for certain lldb |
Johnny Chen | e5637d2 | 2011-05-24 21:05:16 +0000 | [diff] [blame] | 6 | # objects, implements truth value testing for certain lldb objects, and adds a |
| 7 | # global variable 'debugger_unique_id' which is initialized to 0. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 8 | # |
Johnny Chen | 09e0a42 | 2011-07-01 22:14:07 +0000 | [diff] [blame] | 9 | # 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 Chen | 2c77fa4 | 2011-07-02 20:01:09 +0000 | [diff] [blame] | 11 | # advantage of the already existing doxygen C++-docblock and make it the Python |
Johnny Chen | 09e0a42 | 2011-07-01 22:14:07 +0000 | [diff] [blame] | 12 | # docstring for the same method. The 'residues' in this context include the |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 13 | # '#endif', the '#ifdef SWIG', the c comment marker, the trailing blank (SPC's) |
| 14 | # line, and the doxygen comment start marker. |
Johnny Chen | 09e0a42 | 2011-07-01 22:14:07 +0000 | [diff] [blame] | 15 | # |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 16 | # In addition to the 'residues' removal during the cleanup step, it also |
| 17 | # transforms the 'char' data type (which was actually 'char *' but the 'autodoc' |
| 18 | # feature of swig removes ' *' from it into 'str' (as a Python str type). |
| 19 | # |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 20 | # It also calls SBDebugger.Initialize() to initialize the lldb debugger |
| 21 | # subsystem. |
| 22 | # |
| 23 | |
| 24 | import sys, re, StringIO |
| 25 | |
| 26 | if len (sys.argv) != 2: |
| 27 | output_name = "./lldb.py" |
| 28 | else: |
| 29 | output_name = sys.argv[1] + "/lldb.py" |
| 30 | |
| 31 | # print "output_name is '" + output_name + "'" |
| 32 | |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 33 | # |
Johnny Chen | 09e0a42 | 2011-07-01 22:14:07 +0000 | [diff] [blame] | 34 | # Residues to be removed. |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 35 | # |
Johnny Chen | 09e0a42 | 2011-07-01 22:14:07 +0000 | [diff] [blame] | 36 | c_endif_swig = "#endif" |
| 37 | c_ifdef_swig = "#ifdef SWIG" |
Johnny Chen | 2c77fa4 | 2011-07-02 20:01:09 +0000 | [diff] [blame] | 38 | c_comment_marker = "//------------" |
Johnny Chen | f451e30 | 2011-07-03 01:43:29 +0000 | [diff] [blame] | 39 | trailing_blank_line = ' ' |
Johnny Chen | 2c77fa4 | 2011-07-02 20:01:09 +0000 | [diff] [blame] | 40 | # The pattern for recognizing the doxygen comment block line. |
| 41 | doxygen_comment_start = re.compile("^\s*( /// ?)") |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 42 | # The demarcation point for turning on/off residue removal state. |
| 43 | # When bracketed by the lines, the CLEANUP_DOCSTRING state (see below) is ON. |
| 44 | toggle_docstring_cleanup_line = ' """' |
Johnny Chen | 09e0a42 | 2011-07-01 22:14:07 +0000 | [diff] [blame] | 45 | |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 46 | def char_to_str_xform(line): |
| 47 | """This transforms the 'char', i.e, 'char *' to 'str', Python string.""" |
| 48 | line = line.replace(' char', ' str') |
| 49 | line = line.replace('char ', 'str ') |
| 50 | return line |
| 51 | |
| 52 | # |
| 53 | # The one-liner docstring also needs char_to_str transformation, btw. |
| 54 | # |
Johnny Chen | 21c0fd1 | 2011-07-08 23:57:20 +0000 | [diff] [blame^] | 55 | TWO_SPACES = ' ' * 2 |
| 56 | EIGHT_SPACES = ' ' * 8 |
| 57 | one_liner_docstring_pattern = re.compile('^(%s|%s)""".*"""$' % (TWO_SPACES, EIGHT_SPACES)) |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 58 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 59 | # |
Johnny Chen | ec5e0a2 | 2011-06-01 18:40:11 +0000 | [diff] [blame] | 60 | # lldb_iter() should appear before our first SB* class definition. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 61 | # |
| 62 | lldb_iter_def = ''' |
| 63 | # =================================== |
| 64 | # Iterator for lldb container objects |
| 65 | # =================================== |
| 66 | def lldb_iter(obj, getsize, getelem): |
| 67 | """A generator adaptor to support iteration for lldb container objects.""" |
| 68 | size = getattr(obj, getsize) |
| 69 | elem = getattr(obj, getelem) |
| 70 | for i in range(size()): |
| 71 | yield elem(i) |
| 72 | |
Johnny Chen | 8142220 | 2011-06-01 19:21:08 +0000 | [diff] [blame] | 73 | # ============================================================================== |
| 74 | # The modify-python-lldb.py script is responsible for post-processing this SWIG- |
| 75 | # generated lldb.py module. It is responsible for adding the above lldb_iter() |
| 76 | # function definition as well as the supports, in the following, for iteration |
| 77 | # protocol: __iter__, rich comparison methods: __eq__ and __ne__, truth value |
| 78 | # testing (and built-in operation bool()): __nonzero__, and built-in function |
| 79 | # len(): __len__. |
| 80 | # ============================================================================== |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 81 | ''' |
| 82 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 83 | # This supports the iteration protocol. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 84 | iter_def = " def __iter__(self): return lldb_iter(self, '%s', '%s')" |
| 85 | module_iter = " def module_iter(self): return lldb_iter(self, '%s', '%s')" |
| 86 | breakpoint_iter = " def breakpoint_iter(self): return lldb_iter(self, '%s', '%s')" |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 87 | |
Johnny Chen | a79a21c | 2011-05-16 20:31:18 +0000 | [diff] [blame] | 88 | # Called to implement the built-in function len(). |
| 89 | # Eligible objects are those containers with unambiguous iteration support. |
Johnny Chen | a79a21c | 2011-05-16 20:31:18 +0000 | [diff] [blame] | 90 | len_def = " def __len__(self): return self.%s()" |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 91 | |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 92 | # This supports the rich comparison methods of __eq__ and __ne__. |
Johnny Chen | 7616cb9 | 2011-05-02 19:05:52 +0000 | [diff] [blame] | 93 | eq_def = " def __eq__(self, other): return isinstance(other, %s) and %s" |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 94 | ne_def = " def __ne__(self, other): return not self.__eq__(other)" |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 95 | |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 96 | # Called to implement truth value testing and the built-in operation bool(); |
| 97 | # should return False or True, or their integer equivalents 0 or 1. |
| 98 | # Delegate to self.IsValid() if it is defined for the current lldb object. |
| 99 | nonzero_def = " def __nonzero__(self): return self.IsValid()" |
| 100 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 101 | # |
Johnny Chen | a6303ef | 2011-05-24 22:53:03 +0000 | [diff] [blame] | 102 | # This dictionary defines a mapping from classname to (getsize, getelem) tuple. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 103 | # |
| 104 | d = { 'SBBreakpoint': ('GetNumLocations', 'GetLocationAtIndex'), |
| 105 | 'SBCompileUnit': ('GetNumLineEntries', 'GetLineEntryAtIndex'), |
| 106 | 'SBDebugger': ('GetNumTargets', 'GetTargetAtIndex'), |
| 107 | 'SBModule': ('GetNumSymbols', 'GetSymbolAtIndex'), |
| 108 | 'SBProcess': ('GetNumThreads', 'GetThreadAtIndex'), |
| 109 | 'SBThread': ('GetNumFrames', 'GetFrameAtIndex'), |
| 110 | |
| 111 | 'SBInstructionList': ('GetSize', 'GetInstructionAtIndex'), |
| 112 | 'SBStringList': ('GetSize', 'GetStringAtIndex',), |
| 113 | 'SBSymbolContextList': ('GetSize', 'GetContextAtIndex'), |
| 114 | 'SBValueList': ('GetSize', 'GetValueAtIndex'), |
| 115 | |
| 116 | 'SBType': ('GetNumberChildren', 'GetChildAtIndex'), |
| 117 | 'SBValue': ('GetNumChildren', 'GetChildAtIndex'), |
| 118 | |
Johnny Chen | 08477f5 | 2011-05-24 22:57:42 +0000 | [diff] [blame] | 119 | # SBTarget needs special processing, see below. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 120 | 'SBTarget': {'module': ('GetNumModules', 'GetModuleAtIndex'), |
| 121 | 'breakpoint': ('GetNumBreakpoints', 'GetBreakpointAtIndex') |
| 122 | } |
| 123 | } |
| 124 | |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 125 | # |
Johnny Chen | 7616cb9 | 2011-05-02 19:05:52 +0000 | [diff] [blame] | 126 | # This dictionary defines a mapping from classname to equality method name(s). |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 127 | # |
Johnny Chen | 694cfd0 | 2011-06-09 22:04:56 +0000 | [diff] [blame] | 128 | e = { 'SBAddress': ['GetFileAddress', 'GetModule'], |
| 129 | 'SBBreakpoint': ['GetID'], |
Johnny Chen | 7616cb9 | 2011-05-02 19:05:52 +0000 | [diff] [blame] | 130 | 'SBFileSpec': ['GetFilename', 'GetDirectory'], |
| 131 | 'SBModule': ['GetFileSpec', 'GetUUIDString'] |
| 132 | } |
| 133 | |
| 134 | def list_to_frag(list): |
| 135 | """Transform a list to equality program fragment. |
| 136 | |
| 137 | For example, ['GetID'] is transformed to 'self.GetID() == other.GetID()', |
| 138 | and ['GetFilename', 'GetDirectory'] to 'self.GetFilename() == other.GetFilename() |
| 139 | and self.GetDirectory() == other.GetDirectory()'. |
| 140 | """ |
| 141 | if not list: |
| 142 | raise Exception("list should be non-empty") |
| 143 | frag = StringIO.StringIO() |
| 144 | for i in range(len(list)): |
| 145 | if i > 0: |
| 146 | frag.write(" and ") |
| 147 | frag.write("self.{0}() == other.{0}()".format(list[i])) |
| 148 | return frag.getvalue() |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 149 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 150 | # The new content will have the iteration protocol defined for our lldb objects. |
| 151 | new_content = StringIO.StringIO() |
| 152 | |
| 153 | with open(output_name, 'r') as f_in: |
| 154 | content = f_in.read() |
| 155 | |
| 156 | # The pattern for recognizing the beginning of an SB class definition. |
| 157 | class_pattern = re.compile("^class (SB.*)\(_object\):$") |
| 158 | |
| 159 | # The pattern for recognizing the beginning of the __init__ method definition. |
| 160 | init_pattern = re.compile("^ def __init__\(self, \*args\):") |
| 161 | |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 162 | # The pattern for recognizing the beginning of the IsValid method definition. |
Peter Collingbourne | f208453 | 2011-06-14 03:55:41 +0000 | [diff] [blame] | 163 | isvalid_pattern = re.compile("^ def IsValid\(") |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 164 | |
Johnny Chen | a79a21c | 2011-05-16 20:31:18 +0000 | [diff] [blame] | 165 | # These define the states of our finite state machine. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 166 | NORMAL = 0 |
| 167 | DEFINING_ITERATOR = 1 |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 168 | DEFINING_EQUALITY = 2 |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 169 | CLEANUP_DOCSTRING = 4 |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 170 | |
| 171 | # The lldb_iter_def only needs to be inserted once. |
| 172 | lldb_iter_defined = False; |
| 173 | |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 174 | # Our FSM begins its life in the NORMAL state, and transitions to the |
| 175 | # DEFINING_ITERATOR and/or DEFINING_EQUALITY state whenever it encounters the |
| 176 | # beginning of certain class definitions, see dictionaries 'd' and 'e' above. |
| 177 | # |
| 178 | # Note that the two states DEFINING_ITERATOR and DEFINING_EQUALITY are |
| 179 | # orthogonal in that our FSM can be in one, the other, or both states at the |
| 180 | # same time. During such time, the FSM is eagerly searching for the __init__ |
| 181 | # method definition in order to insert the appropriate method(s) into the lldb |
| 182 | # module. |
| 183 | # |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 184 | # The state CLEANUP_DOCSTRING can be entered from either the NORMAL or the |
| 185 | # DEFINING_ITERATOR/EQUALITY states. While in this state, the FSM is fixing/ |
| 186 | # cleaning the Python docstrings generated by the swig docstring features. |
| 187 | # |
Johnny Chen | b72d177 | 2011-05-24 22:29:49 +0000 | [diff] [blame] | 188 | # The FSM, in all possible states, also checks the current input for IsValid() |
| 189 | # definition, and inserts a __nonzero__() method definition to implement truth |
| 190 | # value testing and the built-in operation bool(). |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 191 | state = NORMAL |
| 192 | for line in content.splitlines(): |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 193 | # Handle the state transition into CLEANUP_DOCSTRING state as it is possible |
| 194 | # to enter this state from either NORMAL or DEFINING_ITERATOR/EQUALITY. |
| 195 | # |
| 196 | # If ' """' is the sole line, prepare to transition to the |
| 197 | # CLEANUP_DOCSTRING state or out of it. |
| 198 | if line == toggle_docstring_cleanup_line: |
| 199 | if state & CLEANUP_DOCSTRING: |
| 200 | state ^= CLEANUP_DOCSTRING |
| 201 | else: |
| 202 | state |= CLEANUP_DOCSTRING |
Johnny Chen | 09e0a42 | 2011-07-01 22:14:07 +0000 | [diff] [blame] | 203 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 204 | if state == NORMAL: |
| 205 | match = class_pattern.search(line) |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 206 | # Inserts the lldb_iter() definition before the first class definition. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 207 | if not lldb_iter_defined and match: |
| 208 | print >> new_content, lldb_iter_def |
| 209 | lldb_iter_defined = True |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 210 | |
| 211 | # If we are at the beginning of the class definitions, prepare to |
| 212 | # transition to the DEFINING_ITERATOR/DEFINING_EQUALITY state for the |
| 213 | # right class names. |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 214 | if match: |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 215 | cls = match.group(1) |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 216 | if cls in d: |
| 217 | # Adding support for iteration for the matched SB class. |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 218 | state |= DEFINING_ITERATOR |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 219 | if cls in e: |
| 220 | # Adding support for eq and ne for the matched SB class. |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 221 | state |= DEFINING_EQUALITY |
| 222 | |
| 223 | elif (state & DEFINING_ITERATOR) or (state & DEFINING_EQUALITY): |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 224 | match = init_pattern.search(line) |
| 225 | if match: |
| 226 | # We found the beginning of the __init__ method definition. |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 227 | # This is a good spot to insert the iter and/or eq-ne support. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 228 | # |
| 229 | # But note that SBTarget has two types of iterations. |
| 230 | if cls == "SBTarget": |
| 231 | print >> new_content, module_iter % (d[cls]['module']) |
| 232 | print >> new_content, breakpoint_iter % (d[cls]['breakpoint']) |
| 233 | else: |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 234 | if (state & DEFINING_ITERATOR): |
| 235 | print >> new_content, iter_def % d[cls] |
Johnny Chen | a79a21c | 2011-05-16 20:31:18 +0000 | [diff] [blame] | 236 | print >> new_content, len_def % d[cls][0] |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 237 | if (state & DEFINING_EQUALITY): |
Johnny Chen | 7616cb9 | 2011-05-02 19:05:52 +0000 | [diff] [blame] | 238 | print >> new_content, eq_def % (cls, list_to_frag(e[cls])) |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 239 | print >> new_content, ne_def |
Johnny Chen | a2f86e8 | 2011-04-29 19:19:13 +0000 | [diff] [blame] | 240 | |
| 241 | # Next state will be NORMAL. |
| 242 | state = NORMAL |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 243 | |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 244 | elif (state & CLEANUP_DOCSTRING): |
| 245 | # Cleanse the lldb.py of the autodoc'ed residues. |
| 246 | if c_ifdef_swig in line or c_endif_swig in line: |
| 247 | continue |
| 248 | # As well as the comment marker line and trailing blank line. |
| 249 | if c_comment_marker in line or line == trailing_blank_line: |
| 250 | continue |
| 251 | # Also remove the '\a ' substrings. |
| 252 | line = line.replace('\a ', '') |
| 253 | # And the leading '///' substring. |
| 254 | doxygen_comment_match = doxygen_comment_start.match(line) |
| 255 | if doxygen_comment_match: |
| 256 | line = line.replace(doxygen_comment_match.group(1), '', 1) |
| 257 | |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 258 | line = char_to_str_xform(line) |
| 259 | |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 260 | # Note that the transition out of CLEANUP_DOCSTRING is handled at the |
| 261 | # beginning of this function already. |
| 262 | |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 263 | # This deals with one-liner docstring, for example, SBThread.GetName: |
| 264 | # """GetName(self) -> char""". |
| 265 | if one_liner_docstring_pattern.match(line): |
| 266 | line = char_to_str_xform(line) |
| 267 | |
Johnny Chen | b72d177 | 2011-05-24 22:29:49 +0000 | [diff] [blame] | 268 | # Look for 'def IsValid(*args):', and once located, add implementation |
| 269 | # of truth value testing for this object by delegation. |
| 270 | if isvalid_pattern.search(line): |
| 271 | print >> new_content, nonzero_def |
| 272 | |
Johnny Chen | 6ea16c7 | 2011-05-02 17:53:04 +0000 | [diff] [blame] | 273 | # Pass the original line of content to new_content. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 274 | print >> new_content, line |
| 275 | |
| 276 | with open(output_name, 'w') as f_out: |
| 277 | f_out.write(new_content.getvalue()) |
| 278 | f_out.write("debugger_unique_id = 0\n") |
| 279 | f_out.write("SBDebugger.Initialize()\n") |