[Lldb-commits] [lldb] r130442 - in /lldb/trunk: scripts/Python/append-debugger-id.py scripts/Python/build-swig-Python.sh scripts/Python/modify-python-lldb.py test/python_api/lldbutil/TestLLDBIterator.py
Johnny Chen
johnny.chen at apple.com
Thu Apr 28 14:31:18 PDT 2011
Author: johnny
Date: Thu Apr 28 16:31:18 2011
New Revision: 130442
URL: http://llvm.org/viewvc/llvm-project?rev=130442&view=rev
Log:
Move the iteration protocol of lldb objects to the auto-generated lldb Python module.
This is so that the objects which support the iteration protocol are immediately obvious
from looking at the lldb.py file.
SBTarget supports two types of iterations: module and breakpoint. For an SBTarget instance,
you will need to issue either:
for m in target.module_iter()
or
for b in target.breakpoint_iter()
For other single iteration protocol objects, just use, for example:
for thread in process:
ID = thread.GetThreadID()
for frame in thread:
frame.Disassemble()
....
Added:
lldb/trunk/scripts/Python/modify-python-lldb.py
- copied, changed from r130407, lldb/trunk/scripts/Python/append-debugger-id.py
Removed:
lldb/trunk/scripts/Python/append-debugger-id.py
Modified:
lldb/trunk/scripts/Python/build-swig-Python.sh
lldb/trunk/test/python_api/lldbutil/TestLLDBIterator.py
Removed: lldb/trunk/scripts/Python/append-debugger-id.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/append-debugger-id.py?rev=130441&view=auto
==============================================================================
--- lldb/trunk/scripts/Python/append-debugger-id.py (original)
+++ lldb/trunk/scripts/Python/append-debugger-id.py (removed)
@@ -1,23 +0,0 @@
-#
-# append-debugger-id.py
-#
-# This script adds a global variable, 'debugger_unique_id' to the lldb
-# module (which was automatically generated via running swig), and
-# initializes it to 0.
-#
-# It also calls SBDebugger.Initialize() to initialize the lldb debugger
-# subsystem.
-#
-
-import sys
-
-if len (sys.argv) != 2:
- output_name = "./lldb.py"
-else:
- output_name = sys.argv[1] + "/lldb.py"
-
-# print "output_name is '" + output_name + "'"
-
-with open(output_name, 'a') as f_out:
- f_out.write("debugger_unique_id = 0\n")
- f_out.write("SBDebugger.Initialize()\n")
Modified: lldb/trunk/scripts/Python/build-swig-Python.sh
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/build-swig-Python.sh?rev=130442&r1=130441&r2=130442&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/build-swig-Python.sh (original)
+++ lldb/trunk/scripts/Python/build-swig-Python.sh Thu Apr 28 16:31:18 2011
@@ -173,12 +173,13 @@
$SWIG -c++ -shadow -python -I"/usr/include" -I"${SRC_ROOT}/include" -I./. -outdir "${CONFIG_BUILD_DIR}" -o "${swig_output_file}" "${swig_input_file}"
+# Implement the iterator protocol for some lldb objects.
# Append global variable to lldb Python module.
-
+# And initialize the lldb debugger subsystem.
current_dir=`pwd`
-if [ -f "${current_dir}/append-debugger-id.py" ]
+if [ -f "${current_dir}/modify-python-lldb.py" ]
then
- python ${current_dir}/append-debugger-id.py ${CONFIG_BUILD_DIR}
+ python ${current_dir}/modify-python-lldb.py ${CONFIG_BUILD_DIR}
fi
# Fix the "#include" statement in the swig output file
Copied: lldb/trunk/scripts/Python/modify-python-lldb.py (from r130407, lldb/trunk/scripts/Python/append-debugger-id.py)
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/modify-python-lldb.py?p2=lldb/trunk/scripts/Python/modify-python-lldb.py&p1=lldb/trunk/scripts/Python/append-debugger-id.py&r1=130407&r2=130442&rev=130442&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/append-debugger-id.py (original)
+++ lldb/trunk/scripts/Python/modify-python-lldb.py Thu Apr 28 16:31:18 2011
@@ -1,15 +1,15 @@
#
-# append-debugger-id.py
+# finish-lldb-python.py
#
-# This script adds a global variable, 'debugger_unique_id' to the lldb
-# module (which was automatically generated via running swig), and
-# initializes it to 0.
+# This script modifies the lldb module (which was automatically generated via
+# running swig) to support iteration for certain lldb objects, adds a global
+# variable 'debugger_unique_id' and initializes it to 0.
#
# It also calls SBDebugger.Initialize() to initialize the lldb debugger
# subsystem.
#
-import sys
+import sys, re, StringIO
if len (sys.argv) != 2:
output_name = "./lldb.py"
@@ -18,6 +18,103 @@
# print "output_name is '" + output_name + "'"
-with open(output_name, 'a') as f_out:
+#
+# lldb_iter() should appear before the our first SB* class definition.
+#
+lldb_iter_def = '''
+# ===================================
+# Iterator for lldb container objects
+# ===================================
+def lldb_iter(obj, getsize, getelem):
+ """A generator adaptor to support iteration for lldb container objects."""
+ size = getattr(obj, getsize)
+ elem = getattr(obj, getelem)
+ for i in range(size()):
+ yield elem(i)
+
+'''
+
+#
+# This supports the iteration protocol.
+#
+iter_def = " def __iter__(self): return lldb_iter(self, '%s', '%s')"
+module_iter = " def module_iter(self): return lldb_iter(self, '%s', '%s')"
+breakpoint_iter = " def breakpoint_iter(self): return lldb_iter(self, '%s', '%s')"
+
+#
+# The dictionary defines a mapping from classname to (getsize, getelem) tuple.
+#
+d = { 'SBBreakpoint': ('GetNumLocations', 'GetLocationAtIndex'),
+ 'SBCompileUnit': ('GetNumLineEntries', 'GetLineEntryAtIndex'),
+ 'SBDebugger': ('GetNumTargets', 'GetTargetAtIndex'),
+ 'SBModule': ('GetNumSymbols', 'GetSymbolAtIndex'),
+ 'SBProcess': ('GetNumThreads', 'GetThreadAtIndex'),
+ 'SBThread': ('GetNumFrames', 'GetFrameAtIndex'),
+
+ 'SBInstructionList': ('GetSize', 'GetInstructionAtIndex'),
+ 'SBStringList': ('GetSize', 'GetStringAtIndex',),
+ 'SBSymbolContextList': ('GetSize', 'GetContextAtIndex'),
+ 'SBValueList': ('GetSize', 'GetValueAtIndex'),
+
+ 'SBType': ('GetNumberChildren', 'GetChildAtIndex'),
+ 'SBValue': ('GetNumChildren', 'GetChildAtIndex'),
+
+ 'SBTarget': {'module': ('GetNumModules', 'GetModuleAtIndex'),
+ 'breakpoint': ('GetNumBreakpoints', 'GetBreakpointAtIndex')
+ }
+ }
+
+# The new content will have the iteration protocol defined for our lldb objects.
+new_content = StringIO.StringIO()
+
+with open(output_name, 'r') as f_in:
+ content = f_in.read()
+
+# The pattern for recognizing the beginning of an SB class definition.
+class_pattern = re.compile("^class (SB.*)\(_object\):$")
+
+# The pattern for recognizing the beginning of the __init__ method definition.
+init_pattern = re.compile("^ def __init__\(self, \*args\):")
+
+# These define the states of our state machine.
+NORMAL = 0
+DEFINING_ITERATOR = 1
+DEFINING_TARGET_ITERATOR = 2
+
+# The lldb_iter_def only needs to be inserted once.
+lldb_iter_defined = False;
+
+state = NORMAL
+for line in content.splitlines():
+ if state == NORMAL:
+ match = class_pattern.search(line)
+ if not lldb_iter_defined and match:
+ print >> new_content, lldb_iter_def
+ lldb_iter_defined = True
+ if match and match.group(1) in d:
+ # Adding support for iteration for the matched SB class.
+ cls = match.group(1)
+ # Next state will be DEFINING_ITERATOR.
+ state = DEFINING_ITERATOR
+ elif state == DEFINING_ITERATOR:
+ match = init_pattern.search(line)
+ if match:
+ # We found the beginning of the __init__ method definition.
+ # This is a good spot to insert the iteration support.
+ #
+ # But note that SBTarget has two types of iterations.
+ if cls == "SBTarget":
+ print >> new_content, module_iter % (d[cls]['module'])
+ print >> new_content, breakpoint_iter % (d[cls]['breakpoint'])
+ else:
+ print >> new_content, iter_def % d[cls]
+ # Next state will be NORMAL.
+ state = NORMAL
+
+ # Pass the original line of content to the ew_content.
+ print >> new_content, line
+
+with open(output_name, 'w') as f_out:
+ f_out.write(new_content.getvalue())
f_out.write("debugger_unique_id = 0\n")
f_out.write("SBDebugger.Initialize()\n")
Modified: lldb/trunk/test/python_api/lldbutil/TestLLDBIterator.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/lldbutil/TestLLDBIterator.py?rev=130442&r1=130441&r2=130442&view=diff
==============================================================================
--- lldb/trunk/test/python_api/lldbutil/TestLLDBIterator.py (original)
+++ lldb/trunk/test/python_api/lldbutil/TestLLDBIterator.py Thu Apr 28 16:31:18 2011
@@ -21,19 +21,19 @@
self.line2 = line_number('main.cpp', '// And that line.')
def test_lldb_iter_1(self):
- """Test lldb_iter works correctly for SBTarget -> SBModule."""
+ """Test module_iter works correctly for SBTarget -> SBModule."""
self.buildDefault()
self.lldb_iter_1()
def test_lldb_iter_2(self):
- """Test lldb_iter works correctly for SBTarget -> SBBreakpoint."""
+ """Test breakpoint_iter works correctly for SBTarget -> SBBreakpoint."""
self.buildDefault()
self.lldb_iter_2()
- def test_smart_iter_1(self):
- """Test smart_iter works correctly for SBProcess->SBThread->SBFrame."""
+ def test_lldb_iter_3(self):
+ """Test iterator works correctly for SBProcess->SBThread->SBFrame."""
self.buildDefault()
- self.smart_iter_1()
+ self.lldb_iter_3()
def lldb_iter_1(self):
exe = os.path.join(os.getcwd(), "a.out")
@@ -51,12 +51,12 @@
if not rc.Success() or not self.process.IsValid():
self.fail("SBTarget.LaunchProcess() failed")
- from lldbutil import lldb_iter, get_description
+ from lldbutil import get_description
yours = []
for i in range(target.GetNumModules()):
yours.append(target.GetModuleAtIndex(i))
mine = []
- for m in lldb_iter(target, 'GetNumModules', 'GetModuleAtIndex'):
+ for m in target.module_iter():
mine.append(m)
self.assertTrue(len(yours) == len(mine))
@@ -80,12 +80,12 @@
self.assertTrue(target.GetNumBreakpoints() == 2)
- from lldbutil import lldb_iter, get_description
+ from lldbutil import get_description
yours = []
for i in range(target.GetNumBreakpoints()):
yours.append(target.GetBreakpointAtIndex(i))
mine = []
- for m in lldb_iter(target, 'GetNumBreakpoints', 'GetBreakpointAtIndex'):
+ for m in target.breakpoint_iter():
mine.append(m)
self.assertTrue(len(yours) == len(mine))
@@ -96,7 +96,7 @@
self.assertTrue(yours[i].GetID() == mine[i].GetID(),
"ID of yours[{0}] and mine[{0}] matches".format(i))
- def smart_iter_1(self):
+ def lldb_iter_3(self):
exe = os.path.join(os.getcwd(), "a.out")
target = self.dbg.CreateTarget(exe)
@@ -112,15 +112,15 @@
if not rc.Success() or not self.process.IsValid():
self.fail("SBTarget.LaunchProcess() failed")
- from lldbutil import smart_iter, print_stacktrace
+ from lldbutil import print_stacktrace
stopped_due_to_breakpoint = False
- for thread in smart_iter(self.process):
+ for thread in self.process:
if self.TraceOn():
print_stacktrace(thread)
ID = thread.GetThreadID()
if thread.GetStopReason() == lldb.eStopReasonBreakpoint:
stopped_due_to_breakpoint = True
- for frame in smart_iter(thread):
+ for frame in thread:
self.assertTrue(frame.GetThread().GetThreadID() == ID)
if self.TraceOn():
print frame
More information about the lldb-commits
mailing list