Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 1 | # |
Johnny Chen | d7e04d9 | 2011-08-05 20:17:27 +0000 | [diff] [blame] | 2 | # modify-python-lldb.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' |
Johnny Chen | 106d593 | 2011-11-08 23:08:03 +0000 | [diff] [blame] | 18 | # feature of swig removes ' *' from it) into 'str' (as a Python str type). |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 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 = "//------------" |
| 39 | # The pattern for recognizing the doxygen comment block line. |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 40 | doxygen_comment_start = re.compile("^\s*(/// ?)") |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 41 | # The demarcation point for turning on/off residue removal state. |
| 42 | # When bracketed by the lines, the CLEANUP_DOCSTRING state (see below) is ON. |
| 43 | toggle_docstring_cleanup_line = ' """' |
Johnny Chen | 09e0a42 | 2011-07-01 22:14:07 +0000 | [diff] [blame] | 44 | |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 45 | def char_to_str_xform(line): |
| 46 | """This transforms the 'char', i.e, 'char *' to 'str', Python string.""" |
| 47 | line = line.replace(' char', ' str') |
| 48 | line = line.replace('char ', 'str ') |
Johnny Chen | 2de7ce6 | 2011-07-14 00:17:49 +0000 | [diff] [blame] | 49 | # Special case handling of 'char **argv' and 'char **envp'. |
| 50 | line = line.replace('str argv', 'list argv') |
| 51 | line = line.replace('str envp', 'list envp') |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 52 | return line |
| 53 | |
| 54 | # |
| 55 | # The one-liner docstring also needs char_to_str transformation, btw. |
| 56 | # |
Johnny Chen | 21c0fd1 | 2011-07-08 23:57:20 +0000 | [diff] [blame] | 57 | TWO_SPACES = ' ' * 2 |
| 58 | EIGHT_SPACES = ' ' * 8 |
| 59 | one_liner_docstring_pattern = re.compile('^(%s|%s)""".*"""$' % (TWO_SPACES, EIGHT_SPACES)) |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 60 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 61 | # |
Johnny Chen | bf338e6 | 2011-09-30 00:42:49 +0000 | [diff] [blame] | 62 | # lldb_helpers and lldb_iter() should appear before our first SB* class definition. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 63 | # |
Johnny Chen | bf338e6 | 2011-09-30 00:42:49 +0000 | [diff] [blame] | 64 | lldb_helpers = ''' |
Johnny Chen | 3689431 | 2011-10-03 21:48:40 +0000 | [diff] [blame] | 65 | # ================================== |
| 66 | # Helper function for SBModule class |
| 67 | # ================================== |
Johnny Chen | bf338e6 | 2011-09-30 00:42:49 +0000 | [diff] [blame] | 68 | def in_range(symbol, section): |
Johnny Chen | f11c029 | 2011-09-30 00:49:02 +0000 | [diff] [blame] | 69 | """Test whether a symbol is within the range of a section.""" |
Johnny Chen | bf338e6 | 2011-09-30 00:42:49 +0000 | [diff] [blame] | 70 | symSA = symbol.GetStartAddress().GetFileAddress() |
| 71 | symEA = symbol.GetEndAddress().GetFileAddress() |
| 72 | secSA = section.GetFileAddress() |
| 73 | secEA = secSA + section.GetByteSize() |
| 74 | |
| 75 | if symEA != LLDB_INVALID_ADDRESS: |
| 76 | if secSA <= symSA and symEA <= secEA: |
| 77 | return True |
| 78 | else: |
| 79 | return False |
| 80 | else: |
| 81 | if secSA <= symSA and symSA < secEA: |
| 82 | return True |
| 83 | else: |
| 84 | return False |
| 85 | ''' |
| 86 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 87 | lldb_iter_def = ''' |
| 88 | # =================================== |
| 89 | # Iterator for lldb container objects |
| 90 | # =================================== |
| 91 | def lldb_iter(obj, getsize, getelem): |
| 92 | """A generator adaptor to support iteration for lldb container objects.""" |
| 93 | size = getattr(obj, getsize) |
| 94 | elem = getattr(obj, getelem) |
| 95 | for i in range(size()): |
| 96 | yield elem(i) |
| 97 | |
Johnny Chen | 8142220 | 2011-06-01 19:21:08 +0000 | [diff] [blame] | 98 | # ============================================================================== |
| 99 | # The modify-python-lldb.py script is responsible for post-processing this SWIG- |
| 100 | # generated lldb.py module. It is responsible for adding the above lldb_iter() |
| 101 | # function definition as well as the supports, in the following, for iteration |
| 102 | # protocol: __iter__, rich comparison methods: __eq__ and __ne__, truth value |
| 103 | # testing (and built-in operation bool()): __nonzero__, and built-in function |
| 104 | # len(): __len__. |
| 105 | # ============================================================================== |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 106 | ''' |
| 107 | |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 108 | # |
Johnny Chen | de856cc | 2011-07-25 23:41:08 +0000 | [diff] [blame] | 109 | # linked_list_iter() is a special purpose iterator to treat the SBValue as the |
| 110 | # head of a list data structure, where you specify the child member name which |
| 111 | # points to the next item on the list and you specify the end-of-list function |
| 112 | # which takes an SBValue and returns True if EOL is reached and False if not. |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 113 | # |
| 114 | linked_list_iter_def = ''' |
Johnny Chen | a4673e1 | 2011-07-26 20:57:10 +0000 | [diff] [blame] | 115 | def __eol_test__(val): |
| 116 | """Default function for end of list test takes an SBValue object. |
| 117 | |
| 118 | Return True if val is invalid or it corresponds to a null pointer. |
| 119 | Otherwise, return False. |
| 120 | """ |
Johnny Chen | d96c9e8 | 2011-08-11 00:49:03 +0000 | [diff] [blame] | 121 | if not val or val.GetValueAsUnsigned() == 0: |
Johnny Chen | a4673e1 | 2011-07-26 20:57:10 +0000 | [diff] [blame] | 122 | return True |
| 123 | else: |
| 124 | return False |
| 125 | |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 126 | # ================================================== |
| 127 | # Iterator for lldb.SBValue treated as a linked list |
| 128 | # ================================================== |
Johnny Chen | a4673e1 | 2011-07-26 20:57:10 +0000 | [diff] [blame] | 129 | def linked_list_iter(self, next_item_name, end_of_list_test=__eol_test__): |
Johnny Chen | de856cc | 2011-07-25 23:41:08 +0000 | [diff] [blame] | 130 | """Generator adaptor to support iteration for SBValue as a linked list. |
| 131 | |
| 132 | linked_list_iter() is a special purpose iterator to treat the SBValue as |
| 133 | the head of a list data structure, where you specify the child member |
| 134 | name which points to the next item on the list and you specify the |
| 135 | end-of-list test function which takes an SBValue for an item and returns |
| 136 | True if EOL is reached and False if not. |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 137 | |
Johnny Chen | 758db96 | 2011-08-11 01:19:46 +0000 | [diff] [blame] | 138 | linked_list_iter() also detects infinite loop and bails out early. |
| 139 | |
Johnny Chen | a4673e1 | 2011-07-26 20:57:10 +0000 | [diff] [blame] | 140 | The end_of_list_test arg, if omitted, defaults to the __eol_test__ |
| 141 | function above. |
| 142 | |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 143 | For example, |
| 144 | |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 145 | # Get Frame #0. |
| 146 | ... |
| 147 | |
| 148 | # Get variable 'task_head'. |
| 149 | task_head = frame0.FindVariable('task_head') |
| 150 | ... |
| 151 | |
Johnny Chen | a4673e1 | 2011-07-26 20:57:10 +0000 | [diff] [blame] | 152 | for t in task_head.linked_list_iter('next'): |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 153 | print t |
| 154 | """ |
Johnny Chen | d96c9e8 | 2011-08-11 00:49:03 +0000 | [diff] [blame] | 155 | if end_of_list_test(self): |
| 156 | return |
| 157 | item = self |
Johnny Chen | 758db96 | 2011-08-11 01:19:46 +0000 | [diff] [blame] | 158 | visited = set() |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 159 | try: |
Johnny Chen | 758db96 | 2011-08-11 01:19:46 +0000 | [diff] [blame] | 160 | while not end_of_list_test(item) and not item.GetValueAsUnsigned() in visited: |
| 161 | visited.add(item.GetValueAsUnsigned()) |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 162 | yield item |
| 163 | # Prepare for the next iteration. |
| 164 | item = item.GetChildMemberWithName(next_item_name) |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 165 | except: |
| 166 | # Exception occurred. Stop the generator. |
| 167 | pass |
| 168 | |
| 169 | return |
| 170 | ''' |
| 171 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 172 | # This supports the iteration protocol. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 173 | iter_def = " def __iter__(self): return lldb_iter(self, '%s', '%s')" |
| 174 | module_iter = " def module_iter(self): return lldb_iter(self, '%s', '%s')" |
| 175 | breakpoint_iter = " def breakpoint_iter(self): return lldb_iter(self, '%s', '%s')" |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 176 | watchpoint_iter = " def watchpoint_iter(self): return lldb_iter(self, '%s', '%s')" |
Johnny Chen | dc0cbd1 | 2011-09-24 04:51:43 +0000 | [diff] [blame] | 177 | section_iter = " def section_iter(self): return lldb_iter(self, '%s', '%s')" |
Johnny Chen | 1dbf2a8 | 2012-03-16 21:55:42 +0000 | [diff] [blame] | 178 | compile_unit_iter = " def compile_unit_iter(self): return lldb_iter(self, '%s', '%s')" |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 179 | |
Johnny Chen | a79a21c | 2011-05-16 20:31:18 +0000 | [diff] [blame] | 180 | # Called to implement the built-in function len(). |
| 181 | # Eligible objects are those containers with unambiguous iteration support. |
Johnny Chen | a79a21c | 2011-05-16 20:31:18 +0000 | [diff] [blame] | 182 | len_def = " def __len__(self): return self.%s()" |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 183 | |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 184 | # This supports the rich comparison methods of __eq__ and __ne__. |
Johnny Chen | 7616cb9 | 2011-05-02 19:05:52 +0000 | [diff] [blame] | 185 | eq_def = " def __eq__(self, other): return isinstance(other, %s) and %s" |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 186 | ne_def = " def __ne__(self, other): return not self.__eq__(other)" |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 187 | |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 188 | # Called to implement truth value testing and the built-in operation bool(); |
| 189 | # should return False or True, or their integer equivalents 0 or 1. |
| 190 | # Delegate to self.IsValid() if it is defined for the current lldb object. |
| 191 | nonzero_def = " def __nonzero__(self): return self.IsValid()" |
| 192 | |
Johnny Chen | bf338e6 | 2011-09-30 00:42:49 +0000 | [diff] [blame] | 193 | # A convenience iterator for SBSymbol! |
| 194 | symbol_in_section_iter_def = ''' |
| 195 | def symbol_in_section_iter(self, section): |
| 196 | """Given a module and its contained section, returns an iterator on the |
| 197 | symbols within the section.""" |
| 198 | for sym in self: |
| 199 | if in_range(sym, section): |
| 200 | yield sym |
| 201 | ''' |
| 202 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 203 | # |
Johnny Chen | a6303ef | 2011-05-24 22:53:03 +0000 | [diff] [blame] | 204 | # This dictionary defines a mapping from classname to (getsize, getelem) tuple. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 205 | # |
| 206 | d = { 'SBBreakpoint': ('GetNumLocations', 'GetLocationAtIndex'), |
| 207 | 'SBCompileUnit': ('GetNumLineEntries', 'GetLineEntryAtIndex'), |
| 208 | 'SBDebugger': ('GetNumTargets', 'GetTargetAtIndex'), |
| 209 | 'SBModule': ('GetNumSymbols', 'GetSymbolAtIndex'), |
| 210 | 'SBProcess': ('GetNumThreads', 'GetThreadAtIndex'), |
Johnny Chen | dc0cbd1 | 2011-09-24 04:51:43 +0000 | [diff] [blame] | 211 | 'SBSection': ('GetNumSubSections', 'GetSubSectionAtIndex'), |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 212 | 'SBThread': ('GetNumFrames', 'GetFrameAtIndex'), |
| 213 | |
| 214 | 'SBInstructionList': ('GetSize', 'GetInstructionAtIndex'), |
| 215 | 'SBStringList': ('GetSize', 'GetStringAtIndex',), |
| 216 | 'SBSymbolContextList': ('GetSize', 'GetContextAtIndex'), |
Johnny Chen | 3ee8520 | 2011-08-05 01:35:49 +0000 | [diff] [blame] | 217 | 'SBTypeList': ('GetSize', 'GetTypeAtIndex'), |
| 218 | 'SBValueList': ('GetSize', 'GetValueAtIndex'), |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 219 | |
| 220 | 'SBType': ('GetNumberChildren', 'GetChildAtIndex'), |
| 221 | 'SBValue': ('GetNumChildren', 'GetChildAtIndex'), |
| 222 | |
Johnny Chen | 08477f5 | 2011-05-24 22:57:42 +0000 | [diff] [blame] | 223 | # SBTarget needs special processing, see below. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 224 | 'SBTarget': {'module': ('GetNumModules', 'GetModuleAtIndex'), |
Johnny Chen | 092bd15 | 2011-09-27 01:19:20 +0000 | [diff] [blame] | 225 | 'breakpoint': ('GetNumBreakpoints', 'GetBreakpointAtIndex'), |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 226 | 'watchpoint': ('GetNumWatchpoints', 'GetWatchpointAtIndex') |
Johnny Chen | dc0cbd1 | 2011-09-24 04:51:43 +0000 | [diff] [blame] | 227 | }, |
| 228 | |
| 229 | # SBModule has an additional section_iter(), see below. |
Johnny Chen | bf338e6 | 2011-09-30 00:42:49 +0000 | [diff] [blame] | 230 | 'SBModule-section': ('GetNumSections', 'GetSectionAtIndex'), |
Johnny Chen | 1dbf2a8 | 2012-03-16 21:55:42 +0000 | [diff] [blame] | 231 | # And compile_unit_iter(). |
| 232 | 'SBModule-compile-unit': ('GetNumCompileUnits', 'GetCompileUnitAtIndex'), |
Johnny Chen | bf338e6 | 2011-09-30 00:42:49 +0000 | [diff] [blame] | 233 | # As well as symbol_in_section_iter(). |
| 234 | 'SBModule-symbol-in-section': symbol_in_section_iter_def |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 237 | # |
Johnny Chen | 7616cb9 | 2011-05-02 19:05:52 +0000 | [diff] [blame] | 238 | # This dictionary defines a mapping from classname to equality method name(s). |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 239 | # |
Johnny Chen | 5eb54bb | 2011-09-27 20:29:45 +0000 | [diff] [blame] | 240 | e = { 'SBAddress': ['GetFileAddress', 'GetModule'], |
| 241 | 'SBBreakpoint': ['GetID'], |
Greg Clayton | 1fa6b3d | 2011-10-13 18:08:26 +0000 | [diff] [blame] | 242 | 'SBWatchpoint': ['GetID'], |
Johnny Chen | 5eb54bb | 2011-09-27 20:29:45 +0000 | [diff] [blame] | 243 | 'SBFileSpec': ['GetFilename', 'GetDirectory'], |
| 244 | 'SBModule': ['GetFileSpec', 'GetUUIDString'], |
| 245 | 'SBType': ['GetByteSize', 'GetName'] |
Johnny Chen | 7616cb9 | 2011-05-02 19:05:52 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | def list_to_frag(list): |
| 249 | """Transform a list to equality program fragment. |
| 250 | |
| 251 | For example, ['GetID'] is transformed to 'self.GetID() == other.GetID()', |
| 252 | and ['GetFilename', 'GetDirectory'] to 'self.GetFilename() == other.GetFilename() |
| 253 | and self.GetDirectory() == other.GetDirectory()'. |
| 254 | """ |
| 255 | if not list: |
| 256 | raise Exception("list should be non-empty") |
| 257 | frag = StringIO.StringIO() |
| 258 | for i in range(len(list)): |
| 259 | if i > 0: |
| 260 | frag.write(" and ") |
| 261 | frag.write("self.{0}() == other.{0}()".format(list[i])) |
| 262 | return frag.getvalue() |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 263 | |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 264 | class NewContent(StringIO.StringIO): |
| 265 | """Simple facade to keep track of the previous line to be committed.""" |
| 266 | def __init__(self): |
| 267 | StringIO.StringIO.__init__(self) |
| 268 | self.prev_line = None |
| 269 | def add_line(self, a_line): |
| 270 | """Add a line to the content, if there is a previous line, commit it.""" |
| 271 | if self.prev_line != None: |
| 272 | print >> self, self.prev_line |
| 273 | self.prev_line = a_line |
| 274 | def del_line(self): |
| 275 | """Forget about the previous line, do not commit it.""" |
| 276 | self.prev_line = None |
| 277 | def del_blank_line(self): |
| 278 | """Forget about the previous line if it is a blank line.""" |
| 279 | if self.prev_line != None and not self.prev_line.strip(): |
| 280 | self.prev_line = None |
| 281 | def finish(self): |
| 282 | """Call this when you're finished with populating content.""" |
| 283 | if self.prev_line != None: |
| 284 | print >> self, self.prev_line |
| 285 | self.prev_line = None |
| 286 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 287 | # The new content will have the iteration protocol defined for our lldb objects. |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 288 | new_content = NewContent() |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 289 | |
| 290 | with open(output_name, 'r') as f_in: |
| 291 | content = f_in.read() |
| 292 | |
| 293 | # The pattern for recognizing the beginning of an SB class definition. |
| 294 | class_pattern = re.compile("^class (SB.*)\(_object\):$") |
| 295 | |
| 296 | # The pattern for recognizing the beginning of the __init__ method definition. |
Johnny Chen | 3ee8520 | 2011-08-05 01:35:49 +0000 | [diff] [blame] | 297 | init_pattern = re.compile("^ def __init__\(self.*\):") |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 298 | |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 299 | # The pattern for recognizing the beginning of the IsValid method definition. |
Peter Collingbourne | f208453 | 2011-06-14 03:55:41 +0000 | [diff] [blame] | 300 | isvalid_pattern = re.compile("^ def IsValid\(") |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 301 | |
Johnny Chen | a79a21c | 2011-05-16 20:31:18 +0000 | [diff] [blame] | 302 | # These define the states of our finite state machine. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 303 | NORMAL = 0 |
| 304 | DEFINING_ITERATOR = 1 |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 305 | DEFINING_EQUALITY = 2 |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 306 | CLEANUP_DOCSTRING = 4 |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 307 | |
| 308 | # The lldb_iter_def only needs to be inserted once. |
| 309 | lldb_iter_defined = False; |
| 310 | |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 311 | # Our FSM begins its life in the NORMAL state, and transitions to the |
| 312 | # DEFINING_ITERATOR and/or DEFINING_EQUALITY state whenever it encounters the |
| 313 | # beginning of certain class definitions, see dictionaries 'd' and 'e' above. |
| 314 | # |
| 315 | # Note that the two states DEFINING_ITERATOR and DEFINING_EQUALITY are |
| 316 | # orthogonal in that our FSM can be in one, the other, or both states at the |
| 317 | # same time. During such time, the FSM is eagerly searching for the __init__ |
| 318 | # method definition in order to insert the appropriate method(s) into the lldb |
| 319 | # module. |
| 320 | # |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 321 | # The state CLEANUP_DOCSTRING can be entered from either the NORMAL or the |
| 322 | # DEFINING_ITERATOR/EQUALITY states. While in this state, the FSM is fixing/ |
| 323 | # cleaning the Python docstrings generated by the swig docstring features. |
| 324 | # |
Johnny Chen | b72d177 | 2011-05-24 22:29:49 +0000 | [diff] [blame] | 325 | # The FSM, in all possible states, also checks the current input for IsValid() |
| 326 | # definition, and inserts a __nonzero__() method definition to implement truth |
| 327 | # value testing and the built-in operation bool(). |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 328 | state = NORMAL |
| 329 | for line in content.splitlines(): |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 330 | # Handle the state transition into CLEANUP_DOCSTRING state as it is possible |
| 331 | # to enter this state from either NORMAL or DEFINING_ITERATOR/EQUALITY. |
| 332 | # |
| 333 | # If ' """' is the sole line, prepare to transition to the |
| 334 | # CLEANUP_DOCSTRING state or out of it. |
| 335 | if line == toggle_docstring_cleanup_line: |
| 336 | if state & CLEANUP_DOCSTRING: |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 337 | # Special handling of the trailing blank line right before the '"""' |
| 338 | # end docstring marker. |
| 339 | new_content.del_blank_line() |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 340 | state ^= CLEANUP_DOCSTRING |
| 341 | else: |
| 342 | state |= CLEANUP_DOCSTRING |
Johnny Chen | 09e0a42 | 2011-07-01 22:14:07 +0000 | [diff] [blame] | 343 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 344 | if state == NORMAL: |
| 345 | match = class_pattern.search(line) |
Johnny Chen | bf338e6 | 2011-09-30 00:42:49 +0000 | [diff] [blame] | 346 | # Inserts lldb_helpers and the lldb_iter() definition before the first |
| 347 | # class definition. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 348 | if not lldb_iter_defined and match: |
Johnny Chen | bf338e6 | 2011-09-30 00:42:49 +0000 | [diff] [blame] | 349 | new_content.add_line(lldb_helpers) |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 350 | new_content.add_line(lldb_iter_def) |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 351 | lldb_iter_defined = True |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 352 | |
| 353 | # If we are at the beginning of the class definitions, prepare to |
| 354 | # transition to the DEFINING_ITERATOR/DEFINING_EQUALITY state for the |
| 355 | # right class names. |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 356 | if match: |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 357 | cls = match.group(1) |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 358 | if cls in d: |
| 359 | # Adding support for iteration for the matched SB class. |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 360 | state |= DEFINING_ITERATOR |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 361 | if cls in e: |
| 362 | # Adding support for eq and ne for the matched SB class. |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 363 | state |= DEFINING_EQUALITY |
| 364 | |
Johnny Chen | 533ed2f | 2011-07-15 20:46:19 +0000 | [diff] [blame] | 365 | if (state & DEFINING_ITERATOR) or (state & DEFINING_EQUALITY): |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 366 | match = init_pattern.search(line) |
| 367 | if match: |
| 368 | # We found the beginning of the __init__ method definition. |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 369 | # 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] | 370 | # |
Johnny Chen | 092bd15 | 2011-09-27 01:19:20 +0000 | [diff] [blame] | 371 | # But note that SBTarget has three types of iterations. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 372 | if cls == "SBTarget": |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 373 | new_content.add_line(module_iter % (d[cls]['module'])) |
| 374 | new_content.add_line(breakpoint_iter % (d[cls]['breakpoint'])) |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 375 | new_content.add_line(watchpoint_iter % (d[cls]['watchpoint'])) |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 376 | else: |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 377 | if (state & DEFINING_ITERATOR): |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 378 | new_content.add_line(iter_def % d[cls]) |
| 379 | new_content.add_line(len_def % d[cls][0]) |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 380 | if (state & DEFINING_EQUALITY): |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 381 | new_content.add_line(eq_def % (cls, list_to_frag(e[cls]))) |
| 382 | new_content.add_line(ne_def) |
Johnny Chen | a2f86e8 | 2011-04-29 19:19:13 +0000 | [diff] [blame] | 383 | |
Johnny Chen | 1dbf2a8 | 2012-03-16 21:55:42 +0000 | [diff] [blame] | 384 | # SBModule has extra SBSection, SBCompileUnit iterators and symbol_in_section_iter()! |
Johnny Chen | dc0cbd1 | 2011-09-24 04:51:43 +0000 | [diff] [blame] | 385 | if cls == "SBModule": |
Johnny Chen | bf338e6 | 2011-09-30 00:42:49 +0000 | [diff] [blame] | 386 | new_content.add_line(section_iter % d[cls+'-section']) |
Johnny Chen | 1dbf2a8 | 2012-03-16 21:55:42 +0000 | [diff] [blame] | 387 | new_content.add_line(compile_unit_iter % d[cls+'-compile-unit']) |
Johnny Chen | bf338e6 | 2011-09-30 00:42:49 +0000 | [diff] [blame] | 388 | new_content.add_line(d[cls+'-symbol-in-section']) |
| 389 | |
Johnny Chen | fbebbc9 | 2011-07-25 19:32:35 +0000 | [diff] [blame] | 390 | # This special purpose iterator is for SBValue only!!! |
| 391 | if cls == "SBValue": |
| 392 | new_content.add_line(linked_list_iter_def) |
| 393 | |
Johnny Chen | a2f86e8 | 2011-04-29 19:19:13 +0000 | [diff] [blame] | 394 | # Next state will be NORMAL. |
| 395 | state = NORMAL |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 396 | |
Johnny Chen | 533ed2f | 2011-07-15 20:46:19 +0000 | [diff] [blame] | 397 | if (state & CLEANUP_DOCSTRING): |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 398 | # Cleanse the lldb.py of the autodoc'ed residues. |
| 399 | if c_ifdef_swig in line or c_endif_swig in line: |
| 400 | continue |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 401 | # As well as the comment marker line. |
| 402 | if c_comment_marker in line: |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 403 | continue |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 404 | |
Johnny Chen | 533ed2f | 2011-07-15 20:46:19 +0000 | [diff] [blame] | 405 | # Also remove the '\a ' and '\b 'substrings. |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 406 | line = line.replace('\a ', '') |
Johnny Chen | 533ed2f | 2011-07-15 20:46:19 +0000 | [diff] [blame] | 407 | line = line.replace('\b ', '') |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 408 | # And the leading '///' substring. |
| 409 | doxygen_comment_match = doxygen_comment_start.match(line) |
| 410 | if doxygen_comment_match: |
| 411 | line = line.replace(doxygen_comment_match.group(1), '', 1) |
| 412 | |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 413 | line = char_to_str_xform(line) |
| 414 | |
Johnny Chen | f6ce70a | 2011-07-03 19:55:50 +0000 | [diff] [blame] | 415 | # Note that the transition out of CLEANUP_DOCSTRING is handled at the |
| 416 | # beginning of this function already. |
| 417 | |
Johnny Chen | 3781137 | 2011-07-06 21:55:45 +0000 | [diff] [blame] | 418 | # This deals with one-liner docstring, for example, SBThread.GetName: |
| 419 | # """GetName(self) -> char""". |
| 420 | if one_liner_docstring_pattern.match(line): |
| 421 | line = char_to_str_xform(line) |
| 422 | |
Johnny Chen | b72d177 | 2011-05-24 22:29:49 +0000 | [diff] [blame] | 423 | # Look for 'def IsValid(*args):', and once located, add implementation |
| 424 | # of truth value testing for this object by delegation. |
| 425 | if isvalid_pattern.search(line): |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 426 | new_content.add_line(nonzero_def) |
Johnny Chen | b72d177 | 2011-05-24 22:29:49 +0000 | [diff] [blame] | 427 | |
Johnny Chen | 6ea16c7 | 2011-05-02 17:53:04 +0000 | [diff] [blame] | 428 | # Pass the original line of content to new_content. |
Johnny Chen | ebd63b2 | 2011-07-16 21:15:39 +0000 | [diff] [blame] | 429 | new_content.add_line(line) |
| 430 | |
| 431 | # We are finished with recording new content. |
| 432 | new_content.finish() |
| 433 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 434 | with open(output_name, 'w') as f_out: |
| 435 | f_out.write(new_content.getvalue()) |
| 436 | f_out.write("debugger_unique_id = 0\n") |
| 437 | f_out.write("SBDebugger.Initialize()\n") |