blob: 2a73e6788dd52efdb0fbe7bd158ac11ff96e7512 [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__.
Johnny Chen7616cb92011-05-02 19:05:52 +000045eq_def = " def __eq__(self, other): return isinstance(other, %s) and %s"
Johnny Chen3a3d6592011-04-29 19:03:02 +000046ne_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#
Johnny Chen7616cb92011-05-02 19:05:52 +000072# This dictionary defines a mapping from classname to equality method name(s).
Johnny Chen3a3d6592011-04-29 19:03:02 +000073#
Johnny Chen7616cb92011-05-02 19:05:52 +000074e = { 'SBBreakpoint': ['GetID'],
75 'SBFileSpec': ['GetFilename', 'GetDirectory'],
76 'SBModule': ['GetFileSpec', 'GetUUIDString']
77 }
78
79def list_to_frag(list):
80 """Transform a list to equality program fragment.
81
82 For example, ['GetID'] is transformed to 'self.GetID() == other.GetID()',
83 and ['GetFilename', 'GetDirectory'] to 'self.GetFilename() == other.GetFilename()
84 and self.GetDirectory() == other.GetDirectory()'.
85 """
86 if not list:
87 raise Exception("list should be non-empty")
88 frag = StringIO.StringIO()
89 for i in range(len(list)):
90 if i > 0:
91 frag.write(" and ")
92 frag.write("self.{0}() == other.{0}()".format(list[i]))
93 return frag.getvalue()
Johnny Chen3a3d6592011-04-29 19:03:02 +000094
Johnny Chen14097802011-04-28 21:31:18 +000095# The new content will have the iteration protocol defined for our lldb objects.
96new_content = StringIO.StringIO()
97
98with open(output_name, 'r') as f_in:
99 content = f_in.read()
100
101# The pattern for recognizing the beginning of an SB class definition.
102class_pattern = re.compile("^class (SB.*)\(_object\):$")
103
104# The pattern for recognizing the beginning of the __init__ method definition.
105init_pattern = re.compile("^ def __init__\(self, \*args\):")
106
107# These define the states of our state machine.
108NORMAL = 0
109DEFINING_ITERATOR = 1
Johnny Chen3a3d6592011-04-29 19:03:02 +0000110DEFINING_EQUALITY = 2
Johnny Chen14097802011-04-28 21:31:18 +0000111
112# The lldb_iter_def only needs to be inserted once.
113lldb_iter_defined = False;
114
115state = NORMAL
116for line in content.splitlines():
117 if state == NORMAL:
118 match = class_pattern.search(line)
119 if not lldb_iter_defined and match:
120 print >> new_content, lldb_iter_def
121 lldb_iter_defined = True
Johnny Chen3a3d6592011-04-29 19:03:02 +0000122 if match:
Johnny Chen14097802011-04-28 21:31:18 +0000123 cls = match.group(1)
Johnny Chen3a3d6592011-04-29 19:03:02 +0000124 if cls in d:
125 # Adding support for iteration for the matched SB class.
126 state = (state | DEFINING_ITERATOR)
127 if cls in e:
128 # Adding support for eq and ne for the matched SB class.
129 state = (state | DEFINING_EQUALITY)
130 elif state > NORMAL:
Johnny Chen14097802011-04-28 21:31:18 +0000131 match = init_pattern.search(line)
132 if match:
133 # We found the beginning of the __init__ method definition.
Johnny Chen3a3d6592011-04-29 19:03:02 +0000134 # This is a good spot to insert the iter and/or eq-ne support.
Johnny Chen14097802011-04-28 21:31:18 +0000135 #
136 # But note that SBTarget has two types of iterations.
137 if cls == "SBTarget":
138 print >> new_content, module_iter % (d[cls]['module'])
139 print >> new_content, breakpoint_iter % (d[cls]['breakpoint'])
140 else:
Johnny Chen3a3d6592011-04-29 19:03:02 +0000141 if (state & DEFINING_ITERATOR):
142 print >> new_content, iter_def % d[cls]
143 if (state & DEFINING_EQUALITY):
Johnny Chen7616cb92011-05-02 19:05:52 +0000144 print >> new_content, eq_def % (cls, list_to_frag(e[cls]))
Johnny Chen3a3d6592011-04-29 19:03:02 +0000145 print >> new_content, ne_def
Johnny Chena2f86e82011-04-29 19:19:13 +0000146
147 # Next state will be NORMAL.
148 state = NORMAL
Johnny Chen14097802011-04-28 21:31:18 +0000149
Johnny Chen6ea16c72011-05-02 17:53:04 +0000150 # Pass the original line of content to new_content.
Johnny Chen14097802011-04-28 21:31:18 +0000151 print >> new_content, line
152
153with open(output_name, 'w') as f_out:
154 f_out.write(new_content.getvalue())
155 f_out.write("debugger_unique_id = 0\n")
156 f_out.write("SBDebugger.Initialize()\n")