blob: 4026d8930a613b8ded7903607e83704a54e2600b [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
5# running swig) to support iteration for certain lldb objects, adds a global
6# variable 'debugger_unique_id' and initializes it to 0.
7#
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')"
43
44#
45# The dictionary defines a mapping from classname to (getsize, getelem) tuple.
46#
47d = { 'SBBreakpoint': ('GetNumLocations', 'GetLocationAtIndex'),
48 'SBCompileUnit': ('GetNumLineEntries', 'GetLineEntryAtIndex'),
49 'SBDebugger': ('GetNumTargets', 'GetTargetAtIndex'),
50 'SBModule': ('GetNumSymbols', 'GetSymbolAtIndex'),
51 'SBProcess': ('GetNumThreads', 'GetThreadAtIndex'),
52 'SBThread': ('GetNumFrames', 'GetFrameAtIndex'),
53
54 'SBInstructionList': ('GetSize', 'GetInstructionAtIndex'),
55 'SBStringList': ('GetSize', 'GetStringAtIndex',),
56 'SBSymbolContextList': ('GetSize', 'GetContextAtIndex'),
57 'SBValueList': ('GetSize', 'GetValueAtIndex'),
58
59 'SBType': ('GetNumberChildren', 'GetChildAtIndex'),
60 'SBValue': ('GetNumChildren', 'GetChildAtIndex'),
61
62 'SBTarget': {'module': ('GetNumModules', 'GetModuleAtIndex'),
63 'breakpoint': ('GetNumBreakpoints', 'GetBreakpointAtIndex')
64 }
65 }
66
67# The new content will have the iteration protocol defined for our lldb objects.
68new_content = StringIO.StringIO()
69
70with open(output_name, 'r') as f_in:
71 content = f_in.read()
72
73# The pattern for recognizing the beginning of an SB class definition.
74class_pattern = re.compile("^class (SB.*)\(_object\):$")
75
76# The pattern for recognizing the beginning of the __init__ method definition.
77init_pattern = re.compile("^ def __init__\(self, \*args\):")
78
79# These define the states of our state machine.
80NORMAL = 0
81DEFINING_ITERATOR = 1
82DEFINING_TARGET_ITERATOR = 2
83
84# The lldb_iter_def only needs to be inserted once.
85lldb_iter_defined = False;
86
87state = NORMAL
88for line in content.splitlines():
89 if state == NORMAL:
90 match = class_pattern.search(line)
91 if not lldb_iter_defined and match:
92 print >> new_content, lldb_iter_def
93 lldb_iter_defined = True
94 if match and match.group(1) in d:
95 # Adding support for iteration for the matched SB class.
96 cls = match.group(1)
97 # Next state will be DEFINING_ITERATOR.
98 state = DEFINING_ITERATOR
99 elif state == DEFINING_ITERATOR:
100 match = init_pattern.search(line)
101 if match:
102 # We found the beginning of the __init__ method definition.
103 # This is a good spot to insert the iteration support.
104 #
105 # But note that SBTarget has two types of iterations.
106 if cls == "SBTarget":
107 print >> new_content, module_iter % (d[cls]['module'])
108 print >> new_content, breakpoint_iter % (d[cls]['breakpoint'])
109 else:
110 print >> new_content, iter_def % d[cls]
111 # Next state will be NORMAL.
112 state = NORMAL
113
114 # Pass the original line of content to the ew_content.
115 print >> new_content, line
116
117with open(output_name, 'w') as f_out:
118 f_out.write(new_content.getvalue())
119 f_out.write("debugger_unique_id = 0\n")
120 f_out.write("SBDebugger.Initialize()\n")