Add TestTargetAPI.py:

// When stopped on breakppint 1, and then 2, we can get the line entries using
// SBFrame API SBFrame.GetLineEntry().  We'll get the start addresses for the
// two line entries; with the start address (of SBAddress type), we can then
// resolve the symbol context using the SBTarget API
// SBTarget.ResolveSymbolContextForAddress().
//
// The two symbol context should point to the same symbol, i.e., 'a' function.


Add two utility functions to lldbutil.py:

o get_stopped_threads(process, reason):

  return the list of threads with the specified stop reason or an empty list if not found

o get_stopped_thread(process, reason):

  return the first thread with the given stop reason or None if not found

llvm-svn: 126916
diff --git a/lldb/test/python_api/process/TestProcessAPI.py b/lldb/test/python_api/process/TestProcessAPI.py
index fd8b229..474ae06 100644
--- a/lldb/test/python_api/process/TestProcessAPI.py
+++ b/lldb/test/python_api/process/TestProcessAPI.py
@@ -5,6 +5,7 @@
 import os, time
 import unittest2
 import lldb
+from lldbutil import get_stopped_thread
 from lldbtest import *
 
 class ProcessAPITestCase(TestBase):
@@ -71,8 +72,9 @@
         error = lldb.SBError()
         self.process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error)
 
-        thread = self.process.GetThreadAtIndex(0);
-        frame = thread.GetFrameAtIndex(0);
+        thread = get_stopped_thread(self.process, lldb.eStopReasonBreakpoint)
+        self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint")
+        frame = thread.GetFrameAtIndex(0)
 
         # Get the SBValue for the global variable 'my_char'.
         val = frame.FindValue("my_char", lldb.eValueTypeVariableGlobal)
@@ -111,8 +113,9 @@
         error = lldb.SBError()
         self.process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error)
 
-        thread = self.process.GetThreadAtIndex(0);
-        frame = thread.GetFrameAtIndex(0);
+        thread = get_stopped_thread(self.process, lldb.eStopReasonBreakpoint)
+        self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint")
+        frame = thread.GetFrameAtIndex(0)
 
         # Get the SBValue for the global variable 'my_char'.
         val = frame.FindValue("my_char", lldb.eValueTypeVariableGlobal)
@@ -160,8 +163,9 @@
         error = lldb.SBError()
         self.process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error)
 
-        thread = self.process.GetThreadAtIndex(0);
-        frame = thread.GetFrameAtIndex(0);
+        thread = get_stopped_thread(self.process, lldb.eStopReasonBreakpoint)
+        self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint")
+        frame = thread.GetFrameAtIndex(0)
 
         # Get the SBValue for the global variable 'my_int'.
         val = frame.FindValue("my_int", lldb.eValueTypeVariableGlobal)
diff --git a/lldb/test/python_api/symbol-context/TestSymbolContext.py b/lldb/test/python_api/symbol-context/TestSymbolContext.py
index 96d8356..4c373ad 100644
--- a/lldb/test/python_api/symbol-context/TestSymbolContext.py
+++ b/lldb/test/python_api/symbol-context/TestSymbolContext.py
@@ -54,7 +54,10 @@
         self.assertTrue(self.process.IsValid(), PROCESS_IS_VALID)
 
         # Frame #0 should be on self.line.
-        frame0 = self.process.GetThreadAtIndex(0).GetFrameAtIndex(0)
+        from lldbutil import get_stopped_thread
+        thread = get_stopped_thread(self.process, lldb.eStopReasonBreakpoint)
+        self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint")
+        frame0 = thread.GetFrameAtIndex(0)
         self.assertTrue(frame0.GetLineEntry().GetLine() == self.line)
 
         # Now get the SBSymbolContext from this frame.  We want everything. :-)
@@ -81,6 +84,7 @@
         #print "block:", block
 
         lineEntry = context.GetLineEntry()
+        #print "line entry:", lineEntry
         self.expect(lineEntry.GetFileSpec().GetDirectory(), "The line entry should have the correct directory",
                     exe=False,
             substrs = [self.mydir])
diff --git a/lldb/test/python_api/target/Makefile b/lldb/test/python_api/target/Makefile
new file mode 100644
index 0000000..0d70f25
--- /dev/null
+++ b/lldb/test/python_api/target/Makefile
@@ -0,0 +1,5 @@
+LEVEL = ../../make
+
+C_SOURCES := main.c
+
+include $(LEVEL)/Makefile.rules
diff --git a/lldb/test/python_api/target/TestTargetAPI.py b/lldb/test/python_api/target/TestTargetAPI.py
new file mode 100644
index 0000000..0c5f806
--- /dev/null
+++ b/lldb/test/python_api/target/TestTargetAPI.py
@@ -0,0 +1,114 @@
+"""
+Test SBTarget APIs.
+"""
+
+import os, time
+import re
+import unittest2
+import lldb, lldbutil
+from lldbtest import *
+
+class TargetAPITestCase(TestBase):
+
+    mydir = os.path.join("python_api", "target")
+
+    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
+    @python_api_test
+    def test_with_dsym(self):
+        """Exercise SBTaget APIs."""
+        self.buildDsym()
+        self.target_api()
+
+    @python_api_test
+    def test_with_dwarf(self):
+        """Exercise SBTarget APIs."""
+        self.buildDwarf()
+        self.target_api()
+
+    def setUp(self):
+        # Call super's setUp().
+        TestBase.setUp(self)
+        # Find the line number to of function 'c'.
+        self.line1 = line_number('main.c', '// Find the line number for breakpoint 1 here.')
+        self.line2 = line_number('main.c', '// Find the line number for breakpoint 2 here.')
+
+    def target_api(self):
+        """Exercise SBTarget APIs."""
+        exe = os.path.join(os.getcwd(), "a.out")
+
+        # Create a target by the debugger.
+        target = self.dbg.CreateTarget(exe)
+        self.assertTrue(target.IsValid(), VALID_TARGET)
+
+        # Now create the two breakpoints inside function 'a'.
+        breakpoint1 = target.BreakpointCreateByLocation('main.c', self.line1)
+        breakpoint2 = target.BreakpointCreateByLocation('main.c', self.line2)
+        print "breakpoint1:", breakpoint1
+        print "breakpoint2:", breakpoint2
+        self.assertTrue(breakpoint1.IsValid() and
+                        breakpoint1.GetNumLocations() == 1,
+                        VALID_BREAKPOINT)
+        self.assertTrue(breakpoint2.IsValid() and
+                        breakpoint2.GetNumLocations() == 1,
+                        VALID_BREAKPOINT)
+
+        # Now launch the process, and do not stop at entry point.
+        error = lldb.SBError()
+        self.process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error)
+
+        self.process = target.GetProcess()
+        self.assertTrue(self.process.IsValid(), PROCESS_IS_VALID)
+
+        # Frame #0 should be on self.line1.
+        self.assertTrue(self.process.GetState() == lldb.eStateStopped)
+        thread = lldbutil.get_stopped_thread(self.process, lldb.eStopReasonBreakpoint)
+        self.runCmd("process status")
+        frame0 = thread.GetFrameAtIndex(0)
+        lineEntry = frame0.GetLineEntry()
+        self.assertTrue(lineEntry.GetLine() == self.line1)
+
+        address1 = lineEntry.GetStartAddress()
+
+        # Continue the inferior, the breakpoint 2 should be hit.
+        self.process.Continue()
+        self.assertTrue(self.process.GetState() == lldb.eStateStopped)
+        thread = lldbutil.get_stopped_thread(self.process, lldb.eStopReasonBreakpoint)
+        self.runCmd("process status")
+        frame0 = thread.GetFrameAtIndex(0)
+        lineEntry = frame0.GetLineEntry()
+        self.assertTrue(lineEntry.GetLine() == self.line2)
+
+        address2 = lineEntry.GetStartAddress()
+
+        print "address1:", address1
+        print "address2:", address2
+
+        # Now call SBTarget.ResolveSymbolContextForAddress() with the addresses from our line entry.
+        context1 = target.ResolveSymbolContextForAddress(address1, lldb.eSymbolContextEverything)
+        context2 = target.ResolveSymbolContextForAddress(address2, lldb.eSymbolContextEverything)
+
+        self.assertTrue(context1.IsValid() and context2.IsValid())
+        print "context1:", context1
+        print "context2:", context2
+
+        # Verify that the context point to the same function 'a'.
+        symbol1 = context1.GetSymbol()
+        symbol2 = context2.GetSymbol()
+        self.assertTrue(symbol1.IsValid() and symbol2.IsValid())
+        print "symbol1:", symbol1
+        print "symbol2:", symbol2
+
+        stream1 = lldb.SBStream()
+        symbol1.GetDescription(stream1)
+        stream2 = lldb.SBStream()
+        symbol2.GetDescription(stream2)
+        
+        self.expect(stream1.GetData(), "The two addresses should resolve to the same symbol", exe=False,
+            startstr = stream2.GetData())
+
+        
+if __name__ == '__main__':
+    import atexit
+    lldb.SBDebugger.Initialize()
+    atexit.register(lambda: lldb.SBDebugger.Terminate())
+    unittest2.main()
diff --git a/lldb/test/python_api/target/main.c b/lldb/test/python_api/target/main.c
new file mode 100644
index 0000000..aa9fc09
--- /dev/null
+++ b/lldb/test/python_api/target/main.c
@@ -0,0 +1,57 @@
+//===-- main.c --------------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+#include <stdio.h>
+
+// This simple program is to test the lldb Python API SBTarget.
+//
+// When stopped on breakppint 1, and then 2, we can get the line entries using
+// SBFrame API SBFrame.GetLineEntry().  We'll get the start addresses for the
+// two line entries; with the start address (of SBAddress type), we can then
+// resolve the symbol context using the SBTarget API
+// SBTarget.ResolveSymbolContextForAddress().
+//
+// The two symbol context should point to the same symbol, i.e., 'a' function.
+
+int a(int);
+int b(int);
+int c(int);
+
+int a(int val)
+{
+    if (val <= 1) // Find the line number for breakpoint 1 here.
+        val = b(val);
+    else if (val >= 3)
+        val = c(val);
+
+    return val; // Find the line number for breakpoint 2 here.
+}
+
+int b(int val)
+{
+    return c(val);
+}
+
+int c(int val)
+{
+    return val + 3;
+}
+
+int main (int argc, char const *argv[])
+{
+    int A1 = a(1);  // a(1) -> b(1) -> c(1)
+    printf("a(1) returns %d\n", A1);
+    
+    int B2 = b(2);  // b(2) -> c(2)
+    printf("b(2) returns %d\n", B2);
+    
+    int A3 = a(3);  // a(3) -> c(3)
+    printf("a(3) returns %d\n", A3);
+    
+    return 0;
+}