blob: 6908fac09d5bfdcf05816743632fe12b8fee7827 [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
6# objects, adds a global variable 'debugger_unique_id' and initializes it to 0.
Johnny Chen14097802011-04-28 21:31:18 +00007#
8# It also calls SBDebugger.Initialize() to initialize the lldb debugger
9# subsystem.
10#
11
12import sys, re, StringIO
13
14if len (sys.argv) != 2:
15 output_name = "./lldb.py"
16else:
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#
24lldb_iter_def = '''
25# ===================================
26# Iterator for lldb container objects
27# ===================================
28def 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#
40iter_def = " def __iter__(self): return lldb_iter(self, '%s', '%s')"
41module_iter = " def module_iter(self): return lldb_iter(self, '%s', '%s')"
42breakpoint_iter = " def breakpoint_iter(self): return lldb_iter(self, '%s', '%s')"
Johnny Chen3a3d6592011-04-29 19:03:02 +000043#
44# This supports the rich comparison methods of __eq__ and __ne__.
45eq_def = " def __eq__(self, other): return isinstance(other, %s) and self.%s() == other.%s()"
46ne_def = " def __ne__(self, other): return not self.__eq__(other)"
Johnny Chen14097802011-04-28 21:31:18 +000047
48#
49# The dictionary defines a mapping from classname to (getsize, getelem) tuple.
50#
51d = { '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 Chen3a3d6592011-04-29 19:03:02 +000071#
72# This dictionary defines a mapping from classname to equality method name.
73#
74e = { 'SBBreakpoint': 'GetID' }
75
Johnny Chen14097802011-04-28 21:31:18 +000076# The new content will have the iteration protocol defined for our lldb objects.
77new_content = StringIO.StringIO()
78
79with 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.
83class_pattern = re.compile("^class (SB.*)\(_object\):$")
84
85# The pattern for recognizing the beginning of the __init__ method definition.
86init_pattern = re.compile("^ def __init__\(self, \*args\):")
87
88# These define the states of our state machine.
89NORMAL = 0
90DEFINING_ITERATOR = 1
Johnny Chen3a3d6592011-04-29 19:03:02 +000091DEFINING_EQUALITY = 2
Johnny Chen14097802011-04-28 21:31:18 +000092
93# The lldb_iter_def only needs to be inserted once.
94lldb_iter_defined = False;
95
96state = NORMAL
97for 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 Chen3a3d6592011-04-29 19:03:02 +0000103 if match:
Johnny Chen14097802011-04-28 21:31:18 +0000104 cls = match.group(1)
Johnny Chen3a3d6592011-04-29 19:03:02 +0000105 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 Chen14097802011-04-28 21:31:18 +0000112 match = init_pattern.search(line)
113 if match:
114 # We found the beginning of the __init__ method definition.
Johnny Chen3a3d6592011-04-29 19:03:02 +0000115 # This is a good spot to insert the iter and/or eq-ne support.
Johnny Chen14097802011-04-28 21:31:18 +0000116 #
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 Chen3a3d6592011-04-29 19:03:02 +0000122 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 Chena2f86e82011-04-29 19:19:13 +0000127
128 # Next state will be NORMAL.
129 state = NORMAL
Johnny Chen14097802011-04-28 21:31:18 +0000130
Johnny Chen6ea16c72011-05-02 17:53:04 +0000131 # Pass the original line of content to new_content.
Johnny Chen14097802011-04-28 21:31:18 +0000132 print >> new_content, line
133
134with 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")