blob: da395c5c6234a13ac1e2d0b0b00dac11aea8d6da [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#
Johnny Chena79a21c2011-05-16 20:31:18 +000044# Called to implement the built-in function len().
45# Eligible objects are those containers with unambiguous iteration support.
46#
47len_def = " def __len__(self): return self.%s()"
48#
Johnny Chen3a3d6592011-04-29 19:03:02 +000049# This supports the rich comparison methods of __eq__ and __ne__.
Johnny Chen7616cb92011-05-02 19:05:52 +000050eq_def = " def __eq__(self, other): return isinstance(other, %s) and %s"
Johnny Chen3a3d6592011-04-29 19:03:02 +000051ne_def = " def __ne__(self, other): return not self.__eq__(other)"
Johnny Chen14097802011-04-28 21:31:18 +000052
53#
54# The dictionary defines a mapping from classname to (getsize, getelem) tuple.
55#
56d = { 'SBBreakpoint': ('GetNumLocations', 'GetLocationAtIndex'),
57 'SBCompileUnit': ('GetNumLineEntries', 'GetLineEntryAtIndex'),
58 'SBDebugger': ('GetNumTargets', 'GetTargetAtIndex'),
59 'SBModule': ('GetNumSymbols', 'GetSymbolAtIndex'),
60 'SBProcess': ('GetNumThreads', 'GetThreadAtIndex'),
61 'SBThread': ('GetNumFrames', 'GetFrameAtIndex'),
62
63 'SBInstructionList': ('GetSize', 'GetInstructionAtIndex'),
64 'SBStringList': ('GetSize', 'GetStringAtIndex',),
65 'SBSymbolContextList': ('GetSize', 'GetContextAtIndex'),
66 'SBValueList': ('GetSize', 'GetValueAtIndex'),
67
68 'SBType': ('GetNumberChildren', 'GetChildAtIndex'),
69 'SBValue': ('GetNumChildren', 'GetChildAtIndex'),
70
71 'SBTarget': {'module': ('GetNumModules', 'GetModuleAtIndex'),
72 'breakpoint': ('GetNumBreakpoints', 'GetBreakpointAtIndex')
73 }
74 }
75
Johnny Chen3a3d6592011-04-29 19:03:02 +000076#
Johnny Chen7616cb92011-05-02 19:05:52 +000077# This dictionary defines a mapping from classname to equality method name(s).
Johnny Chen3a3d6592011-04-29 19:03:02 +000078#
Johnny Chen7616cb92011-05-02 19:05:52 +000079e = { 'SBBreakpoint': ['GetID'],
80 'SBFileSpec': ['GetFilename', 'GetDirectory'],
81 'SBModule': ['GetFileSpec', 'GetUUIDString']
82 }
83
84def list_to_frag(list):
85 """Transform a list to equality program fragment.
86
87 For example, ['GetID'] is transformed to 'self.GetID() == other.GetID()',
88 and ['GetFilename', 'GetDirectory'] to 'self.GetFilename() == other.GetFilename()
89 and self.GetDirectory() == other.GetDirectory()'.
90 """
91 if not list:
92 raise Exception("list should be non-empty")
93 frag = StringIO.StringIO()
94 for i in range(len(list)):
95 if i > 0:
96 frag.write(" and ")
97 frag.write("self.{0}() == other.{0}()".format(list[i]))
98 return frag.getvalue()
Johnny Chen3a3d6592011-04-29 19:03:02 +000099
Johnny Chen14097802011-04-28 21:31:18 +0000100# The new content will have the iteration protocol defined for our lldb objects.
101new_content = StringIO.StringIO()
102
103with open(output_name, 'r') as f_in:
104 content = f_in.read()
105
106# The pattern for recognizing the beginning of an SB class definition.
107class_pattern = re.compile("^class (SB.*)\(_object\):$")
108
109# The pattern for recognizing the beginning of the __init__ method definition.
110init_pattern = re.compile("^ def __init__\(self, \*args\):")
111
Johnny Chena79a21c2011-05-16 20:31:18 +0000112# These define the states of our finite state machine.
Johnny Chen14097802011-04-28 21:31:18 +0000113NORMAL = 0
114DEFINING_ITERATOR = 1
Johnny Chen3a3d6592011-04-29 19:03:02 +0000115DEFINING_EQUALITY = 2
Johnny Chen14097802011-04-28 21:31:18 +0000116
117# The lldb_iter_def only needs to be inserted once.
118lldb_iter_defined = False;
119
120state = NORMAL
121for line in content.splitlines():
122 if state == NORMAL:
123 match = class_pattern.search(line)
124 if not lldb_iter_defined and match:
125 print >> new_content, lldb_iter_def
126 lldb_iter_defined = True
Johnny Chen3a3d6592011-04-29 19:03:02 +0000127 if match:
Johnny Chen14097802011-04-28 21:31:18 +0000128 cls = match.group(1)
Johnny Chen3a3d6592011-04-29 19:03:02 +0000129 if cls in d:
130 # Adding support for iteration for the matched SB class.
131 state = (state | DEFINING_ITERATOR)
132 if cls in e:
133 # Adding support for eq and ne for the matched SB class.
134 state = (state | DEFINING_EQUALITY)
135 elif state > NORMAL:
Johnny Chen14097802011-04-28 21:31:18 +0000136 match = init_pattern.search(line)
137 if match:
138 # We found the beginning of the __init__ method definition.
Johnny Chen3a3d6592011-04-29 19:03:02 +0000139 # This is a good spot to insert the iter and/or eq-ne support.
Johnny Chen14097802011-04-28 21:31:18 +0000140 #
141 # But note that SBTarget has two types of iterations.
142 if cls == "SBTarget":
143 print >> new_content, module_iter % (d[cls]['module'])
144 print >> new_content, breakpoint_iter % (d[cls]['breakpoint'])
145 else:
Johnny Chen3a3d6592011-04-29 19:03:02 +0000146 if (state & DEFINING_ITERATOR):
147 print >> new_content, iter_def % d[cls]
Johnny Chena79a21c2011-05-16 20:31:18 +0000148 print >> new_content, len_def % d[cls][0]
Johnny Chen3a3d6592011-04-29 19:03:02 +0000149 if (state & DEFINING_EQUALITY):
Johnny Chen7616cb92011-05-02 19:05:52 +0000150 print >> new_content, eq_def % (cls, list_to_frag(e[cls]))
Johnny Chen3a3d6592011-04-29 19:03:02 +0000151 print >> new_content, ne_def
Johnny Chena2f86e82011-04-29 19:19:13 +0000152
153 # Next state will be NORMAL.
154 state = NORMAL
Johnny Chen14097802011-04-28 21:31:18 +0000155
Johnny Chen6ea16c72011-05-02 17:53:04 +0000156 # Pass the original line of content to new_content.
Johnny Chen14097802011-04-28 21:31:18 +0000157 print >> new_content, line
158
159with open(output_name, 'w') as f_out:
160 f_out.write(new_content.getvalue())
161 f_out.write("debugger_unique_id = 0\n")
162 f_out.write("SBDebugger.Initialize()\n")