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 |
| 6 | # objects, adds a global variable 'debugger_unique_id' and initializes it to 0. |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 7 | # |
| 8 | # It also calls SBDebugger.Initialize() to initialize the lldb debugger |
| 9 | # subsystem. |
| 10 | # |
| 11 | |
| 12 | import sys, re, StringIO |
| 13 | |
| 14 | if len (sys.argv) != 2: |
| 15 | output_name = "./lldb.py" |
| 16 | else: |
| 17 | output_name = sys.argv[1] + "/lldb.py" |
| 18 | |
| 19 | # print "output_name is '" + output_name + "'" |
| 20 | |
| 21 | # |
| 22 | # lldb_iter() should appear before the our first SB* class definition. |
| 23 | # |
| 24 | lldb_iter_def = ''' |
| 25 | # =================================== |
| 26 | # Iterator for lldb container objects |
| 27 | # =================================== |
| 28 | def lldb_iter(obj, getsize, getelem): |
| 29 | """A generator adaptor to support iteration for lldb container objects.""" |
| 30 | size = getattr(obj, getsize) |
| 31 | elem = getattr(obj, getelem) |
| 32 | for i in range(size()): |
| 33 | yield elem(i) |
| 34 | |
| 35 | ''' |
| 36 | |
| 37 | # |
| 38 | # This supports the iteration protocol. |
| 39 | # |
| 40 | iter_def = " def __iter__(self): return lldb_iter(self, '%s', '%s')" |
| 41 | module_iter = " def module_iter(self): return lldb_iter(self, '%s', '%s')" |
| 42 | breakpoint_iter = " def breakpoint_iter(self): return lldb_iter(self, '%s', '%s')" |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 43 | # |
| 44 | # This supports the rich comparison methods of __eq__ and __ne__. |
| 45 | eq_def = " def __eq__(self, other): return isinstance(other, %s) and self.%s() == other.%s()" |
| 46 | ne_def = " def __ne__(self, other): return not self.__eq__(other)" |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 47 | |
| 48 | # |
| 49 | # The dictionary defines a mapping from classname to (getsize, getelem) tuple. |
| 50 | # |
| 51 | d = { 'SBBreakpoint': ('GetNumLocations', 'GetLocationAtIndex'), |
| 52 | 'SBCompileUnit': ('GetNumLineEntries', 'GetLineEntryAtIndex'), |
| 53 | 'SBDebugger': ('GetNumTargets', 'GetTargetAtIndex'), |
| 54 | 'SBModule': ('GetNumSymbols', 'GetSymbolAtIndex'), |
| 55 | 'SBProcess': ('GetNumThreads', 'GetThreadAtIndex'), |
| 56 | 'SBThread': ('GetNumFrames', 'GetFrameAtIndex'), |
| 57 | |
| 58 | 'SBInstructionList': ('GetSize', 'GetInstructionAtIndex'), |
| 59 | 'SBStringList': ('GetSize', 'GetStringAtIndex',), |
| 60 | 'SBSymbolContextList': ('GetSize', 'GetContextAtIndex'), |
| 61 | 'SBValueList': ('GetSize', 'GetValueAtIndex'), |
| 62 | |
| 63 | 'SBType': ('GetNumberChildren', 'GetChildAtIndex'), |
| 64 | 'SBValue': ('GetNumChildren', 'GetChildAtIndex'), |
| 65 | |
| 66 | 'SBTarget': {'module': ('GetNumModules', 'GetModuleAtIndex'), |
| 67 | 'breakpoint': ('GetNumBreakpoints', 'GetBreakpointAtIndex') |
| 68 | } |
| 69 | } |
| 70 | |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 71 | # |
| 72 | # This dictionary defines a mapping from classname to equality method name. |
| 73 | # |
| 74 | e = { 'SBBreakpoint': 'GetID' } |
| 75 | |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 76 | # The new content will have the iteration protocol defined for our lldb objects. |
| 77 | new_content = StringIO.StringIO() |
| 78 | |
| 79 | with open(output_name, 'r') as f_in: |
| 80 | content = f_in.read() |
| 81 | |
| 82 | # The pattern for recognizing the beginning of an SB class definition. |
| 83 | class_pattern = re.compile("^class (SB.*)\(_object\):$") |
| 84 | |
| 85 | # The pattern for recognizing the beginning of the __init__ method definition. |
| 86 | init_pattern = re.compile("^ def __init__\(self, \*args\):") |
| 87 | |
| 88 | # These define the states of our state machine. |
| 89 | NORMAL = 0 |
| 90 | DEFINING_ITERATOR = 1 |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 91 | DEFINING_EQUALITY = 2 |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 92 | |
| 93 | # The lldb_iter_def only needs to be inserted once. |
| 94 | lldb_iter_defined = False; |
| 95 | |
| 96 | state = NORMAL |
| 97 | for line in content.splitlines(): |
| 98 | if state == NORMAL: |
| 99 | match = class_pattern.search(line) |
| 100 | if not lldb_iter_defined and match: |
| 101 | print >> new_content, lldb_iter_def |
| 102 | lldb_iter_defined = True |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 103 | if match: |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 104 | cls = match.group(1) |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 105 | if cls in d: |
| 106 | # Adding support for iteration for the matched SB class. |
| 107 | state = (state | DEFINING_ITERATOR) |
| 108 | if cls in e: |
| 109 | # Adding support for eq and ne for the matched SB class. |
| 110 | state = (state | DEFINING_EQUALITY) |
| 111 | elif state > NORMAL: |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 112 | match = init_pattern.search(line) |
| 113 | if match: |
| 114 | # We found the beginning of the __init__ method definition. |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 115 | # 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] | 116 | # |
| 117 | # But note that SBTarget has two types of iterations. |
| 118 | if cls == "SBTarget": |
| 119 | print >> new_content, module_iter % (d[cls]['module']) |
| 120 | print >> new_content, breakpoint_iter % (d[cls]['breakpoint']) |
| 121 | else: |
Johnny Chen | 3a3d659 | 2011-04-29 19:03:02 +0000 | [diff] [blame] | 122 | if (state & DEFINING_ITERATOR): |
| 123 | print >> new_content, iter_def % d[cls] |
| 124 | if (state & DEFINING_EQUALITY): |
| 125 | print >> new_content, eq_def % (cls, e[cls], e[cls]) |
| 126 | print >> new_content, ne_def |
Johnny Chen | a2f86e8 | 2011-04-29 19:19:13 +0000 | [diff] [blame] | 127 | |
| 128 | # Next state will be NORMAL. |
| 129 | state = NORMAL |
Johnny Chen | 1409780 | 2011-04-28 21:31:18 +0000 | [diff] [blame] | 130 | |
| 131 | # Pass the original line of content to the ew_content. |
| 132 | print >> new_content, line |
| 133 | |
| 134 | with open(output_name, 'w') as f_out: |
| 135 | f_out.write(new_content.getvalue()) |
| 136 | f_out.write("debugger_unique_id = 0\n") |
| 137 | f_out.write("SBDebugger.Initialize()\n") |