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 | # |
| 9 | # It also calls SBDebugger.Initialize() to initialize the lldb debugger |
| 10 | # subsystem. |
| 11 | # |
| 12 | |
| 13 | import sys, re, StringIO |
| 14 | |
| 15 | if len (sys.argv) != 2: |
| 16 | output_name = "./lldb.py" |
| 17 | else: |
| 18 | output_name = sys.argv[1] + "/lldb.py" |
| 19 | |
| 20 | # print "output_name is '" + output_name + "'" |
| 21 | |
| 22 | # |
Johnny Chen | ec5e0a2 | 2011-06-01 18:40:11 +0000 | [diff] [blame] | 23 | # lldb_iter() should appear before our first SB* class definition. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 24 | # |
| 25 | lldb_iter_def = ''' |
| 26 | # =================================== |
| 27 | # Iterator for lldb container objects |
| 28 | # =================================== |
| 29 | def 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 | |
Johnny Chen | 8142220 | 2011-06-01 19:21:08 +0000 | [diff] [blame] | 36 | # ============================================================================== |
| 37 | # The modify-python-lldb.py script is responsible for post-processing this SWIG- |
| 38 | # generated lldb.py module. It is responsible for adding the above lldb_iter() |
| 39 | # function definition as well as the supports, in the following, for iteration |
| 40 | # protocol: __iter__, rich comparison methods: __eq__ and __ne__, truth value |
| 41 | # testing (and built-in operation bool()): __nonzero__, and built-in function |
| 42 | # len(): __len__. |
| 43 | # ============================================================================== |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 44 | ''' |
| 45 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 46 | # This supports the iteration protocol. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 47 | iter_def = " def __iter__(self): return lldb_iter(self, '%s', '%s')" |
| 48 | module_iter = " def module_iter(self): return lldb_iter(self, '%s', '%s')" |
| 49 | breakpoint_iter = " def breakpoint_iter(self): return lldb_iter(self, '%s', '%s')" |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 50 | |
Johnny Chen | a79a21c | 2011-05-16 20:31:18 +0000 | [diff] [blame] | 51 | # Called to implement the built-in function len(). |
| 52 | # Eligible objects are those containers with unambiguous iteration support. |
Johnny Chen | a79a21c | 2011-05-16 20:31:18 +0000 | [diff] [blame] | 53 | len_def = " def __len__(self): return self.%s()" |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 54 | |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 55 | # This supports the rich comparison methods of __eq__ and __ne__. |
Johnny Chen | 7616cb9 | 2011-05-02 19:05:52 +0000 | [diff] [blame] | 56 | eq_def = " def __eq__(self, other): return isinstance(other, %s) and %s" |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 57 | ne_def = " def __ne__(self, other): return not self.__eq__(other)" |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 58 | |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 59 | # Called to implement truth value testing and the built-in operation bool(); |
| 60 | # should return False or True, or their integer equivalents 0 or 1. |
| 61 | # Delegate to self.IsValid() if it is defined for the current lldb object. |
| 62 | nonzero_def = " def __nonzero__(self): return self.IsValid()" |
| 63 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 64 | # |
Johnny Chen | a6303ef | 2011-05-24 22:53:03 +0000 | [diff] [blame] | 65 | # This dictionary defines a mapping from classname to (getsize, getelem) tuple. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 66 | # |
| 67 | d = { 'SBBreakpoint': ('GetNumLocations', 'GetLocationAtIndex'), |
| 68 | 'SBCompileUnit': ('GetNumLineEntries', 'GetLineEntryAtIndex'), |
| 69 | 'SBDebugger': ('GetNumTargets', 'GetTargetAtIndex'), |
| 70 | 'SBModule': ('GetNumSymbols', 'GetSymbolAtIndex'), |
| 71 | 'SBProcess': ('GetNumThreads', 'GetThreadAtIndex'), |
| 72 | 'SBThread': ('GetNumFrames', 'GetFrameAtIndex'), |
| 73 | |
| 74 | 'SBInstructionList': ('GetSize', 'GetInstructionAtIndex'), |
| 75 | 'SBStringList': ('GetSize', 'GetStringAtIndex',), |
| 76 | 'SBSymbolContextList': ('GetSize', 'GetContextAtIndex'), |
| 77 | 'SBValueList': ('GetSize', 'GetValueAtIndex'), |
| 78 | |
| 79 | 'SBType': ('GetNumberChildren', 'GetChildAtIndex'), |
| 80 | 'SBValue': ('GetNumChildren', 'GetChildAtIndex'), |
| 81 | |
Johnny Chen | 08477f5 | 2011-05-24 22:57:42 +0000 | [diff] [blame] | 82 | # SBTarget needs special processing, see below. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 83 | 'SBTarget': {'module': ('GetNumModules', 'GetModuleAtIndex'), |
| 84 | 'breakpoint': ('GetNumBreakpoints', 'GetBreakpointAtIndex') |
| 85 | } |
| 86 | } |
| 87 | |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 88 | # |
Johnny Chen | 7616cb9 | 2011-05-02 19:05:52 +0000 | [diff] [blame] | 89 | # This dictionary defines a mapping from classname to equality method name(s). |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 90 | # |
Johnny Chen | 694cfd0 | 2011-06-09 22:04:56 +0000 | [diff] [blame] | 91 | e = { 'SBAddress': ['GetFileAddress', 'GetModule'], |
| 92 | 'SBBreakpoint': ['GetID'], |
Johnny Chen | 7616cb9 | 2011-05-02 19:05:52 +0000 | [diff] [blame] | 93 | 'SBFileSpec': ['GetFilename', 'GetDirectory'], |
| 94 | 'SBModule': ['GetFileSpec', 'GetUUIDString'] |
| 95 | } |
| 96 | |
| 97 | def list_to_frag(list): |
| 98 | """Transform a list to equality program fragment. |
| 99 | |
| 100 | For example, ['GetID'] is transformed to 'self.GetID() == other.GetID()', |
| 101 | and ['GetFilename', 'GetDirectory'] to 'self.GetFilename() == other.GetFilename() |
| 102 | and self.GetDirectory() == other.GetDirectory()'. |
| 103 | """ |
| 104 | if not list: |
| 105 | raise Exception("list should be non-empty") |
| 106 | frag = StringIO.StringIO() |
| 107 | for i in range(len(list)): |
| 108 | if i > 0: |
| 109 | frag.write(" and ") |
| 110 | frag.write("self.{0}() == other.{0}()".format(list[i])) |
| 111 | return frag.getvalue() |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 112 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 113 | # The new content will have the iteration protocol defined for our lldb objects. |
| 114 | new_content = StringIO.StringIO() |
| 115 | |
| 116 | with open(output_name, 'r') as f_in: |
| 117 | content = f_in.read() |
| 118 | |
| 119 | # The pattern for recognizing the beginning of an SB class definition. |
| 120 | class_pattern = re.compile("^class (SB.*)\(_object\):$") |
| 121 | |
| 122 | # The pattern for recognizing the beginning of the __init__ method definition. |
| 123 | init_pattern = re.compile("^ def __init__\(self, \*args\):") |
| 124 | |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 125 | # The pattern for recognizing the beginning of the IsValid method definition. |
Peter Collingbourne | f208453 | 2011-06-14 03:55:41 +0000 | [diff] [blame] | 126 | isvalid_pattern = re.compile("^ def IsValid\(") |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 127 | |
Johnny Chen | a79a21c | 2011-05-16 20:31:18 +0000 | [diff] [blame] | 128 | # These define the states of our finite state machine. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 129 | NORMAL = 0 |
| 130 | DEFINING_ITERATOR = 1 |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 131 | DEFINING_EQUALITY = 2 |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 132 | |
| 133 | # The lldb_iter_def only needs to be inserted once. |
| 134 | lldb_iter_defined = False; |
| 135 | |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 136 | # Our FSM begins its life in the NORMAL state, and transitions to the |
| 137 | # DEFINING_ITERATOR and/or DEFINING_EQUALITY state whenever it encounters the |
| 138 | # beginning of certain class definitions, see dictionaries 'd' and 'e' above. |
| 139 | # |
| 140 | # Note that the two states DEFINING_ITERATOR and DEFINING_EQUALITY are |
| 141 | # orthogonal in that our FSM can be in one, the other, or both states at the |
| 142 | # same time. During such time, the FSM is eagerly searching for the __init__ |
| 143 | # method definition in order to insert the appropriate method(s) into the lldb |
| 144 | # module. |
| 145 | # |
Johnny Chen | b72d177 | 2011-05-24 22:29:49 +0000 | [diff] [blame] | 146 | # The FSM, in all possible states, also checks the current input for IsValid() |
| 147 | # definition, and inserts a __nonzero__() method definition to implement truth |
| 148 | # value testing and the built-in operation bool(). |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 149 | state = NORMAL |
| 150 | for line in content.splitlines(): |
| 151 | if state == NORMAL: |
| 152 | match = class_pattern.search(line) |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 153 | # Inserts the lldb_iter() definition before the first class definition. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 154 | if not lldb_iter_defined and match: |
| 155 | print >> new_content, lldb_iter_def |
| 156 | lldb_iter_defined = True |
Johnny Chen | 2077f0d | 2011-05-17 22:14:39 +0000 | [diff] [blame] | 157 | |
| 158 | # If we are at the beginning of the class definitions, prepare to |
| 159 | # transition to the DEFINING_ITERATOR/DEFINING_EQUALITY state for the |
| 160 | # right class names. |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 161 | if match: |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 162 | cls = match.group(1) |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 163 | if cls in d: |
| 164 | # Adding support for iteration for the matched SB class. |
| 165 | state = (state | DEFINING_ITERATOR) |
| 166 | if cls in e: |
| 167 | # Adding support for eq and ne for the matched SB class. |
| 168 | state = (state | DEFINING_EQUALITY) |
| 169 | elif state > NORMAL: |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 170 | match = init_pattern.search(line) |
| 171 | if match: |
| 172 | # We found the beginning of the __init__ method definition. |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 173 | # 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] | 174 | # |
| 175 | # But note that SBTarget has two types of iterations. |
| 176 | if cls == "SBTarget": |
| 177 | print >> new_content, module_iter % (d[cls]['module']) |
| 178 | print >> new_content, breakpoint_iter % (d[cls]['breakpoint']) |
| 179 | else: |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 180 | if (state & DEFINING_ITERATOR): |
| 181 | print >> new_content, iter_def % d[cls] |
Johnny Chen | a79a21c | 2011-05-16 20:31:18 +0000 | [diff] [blame] | 182 | print >> new_content, len_def % d[cls][0] |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 183 | if (state & DEFINING_EQUALITY): |
Johnny Chen | 7616cb9 | 2011-05-02 19:05:52 +0000 | [diff] [blame] | 184 | print >> new_content, eq_def % (cls, list_to_frag(e[cls])) |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 185 | print >> new_content, ne_def |
Johnny Chen | a2f86e8 | 2011-04-29 19:19:13 +0000 | [diff] [blame] | 186 | |
| 187 | # Next state will be NORMAL. |
| 188 | state = NORMAL |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 189 | |
Johnny Chen | b72d177 | 2011-05-24 22:29:49 +0000 | [diff] [blame] | 190 | # Look for 'def IsValid(*args):', and once located, add implementation |
| 191 | # of truth value testing for this object by delegation. |
| 192 | if isvalid_pattern.search(line): |
| 193 | print >> new_content, nonzero_def |
| 194 | |
Johnny Chen | 6ea16c7 | 2011-05-02 17:53:04 +0000 | [diff] [blame] | 195 | # Pass the original line of content to new_content. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 196 | print >> new_content, line |
| 197 | |
| 198 | with open(output_name, 'w') as f_out: |
| 199 | f_out.write(new_content.getvalue()) |
| 200 | f_out.write("debugger_unique_id = 0\n") |
| 201 | f_out.write("SBDebugger.Initialize()\n") |