[Lldb-commits] [lldb] r126955 - in /lldb/trunk/test: lldbutil.py python_api/function_symbol/ python_api/function_symbol/Makefile python_api/function_symbol/TestDisasmAPI.py python_api/function_symbol/main.c python_api/target/TestTargetAPI.py

Johnny Chen johnny.chen at apple.com
Thu Mar 3 11:14:00 PST 2011


Author: johnny
Date: Thu Mar  3 13:14:00 2011
New Revision: 126955

URL: http://llvm.org/viewvc/llvm-project?rev=126955&view=rev
Log:
Add TestDisasmAPI.py which exercises the newly added SBFunction/SBSymbol.GetStartAddress(),
among other things:

// When stopped on breakppint 1, we can get the line entry using SBFrame API
// SBFrame.GetLineEntry().  We'll get the start address for the the line entry
// with the SBAddress type, resolve the symbol context using the SBTarget API
// SBTarget.ResolveSymbolContextForAddress() in order to get the SBSymbol.
//
// We then stop at breakpoint 2, get the SBFrame, and the the SBFunction object.
//
// The address from calling GetStartAddress() on the symbol and the function
// should point to the same address, and we also verify that.

And add one utility function disassemble(target, function_or_symbol) to lldbutil.py:

    """Disassemble the function or symbol given a target.

    It returns the disassembly content in a string object.
    """

TestDisasm.py uses the disassemble() function to do disassembly on the SBSymbol, and
then the SBFunction object.

Added:
    lldb/trunk/test/python_api/function_symbol/
    lldb/trunk/test/python_api/function_symbol/Makefile
    lldb/trunk/test/python_api/function_symbol/TestDisasmAPI.py
    lldb/trunk/test/python_api/function_symbol/main.c
Modified:
    lldb/trunk/test/lldbutil.py
    lldb/trunk/test/python_api/target/TestTargetAPI.py

Modified: lldb/trunk/test/lldbutil.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/lldbutil.py?rev=126955&r1=126954&r2=126955&view=diff
==============================================================================
--- lldb/trunk/test/lldbutil.py (original)
+++ lldb/trunk/test/lldbutil.py Thu Mar  3 13:14:00 2011
@@ -41,6 +41,22 @@
         yield elem(i)
 
 
+# ===================================================
+# Disassembly for an SBFunction or an SBSymbol object
+# ===================================================
+
+def disassemble(target, function_or_symbol):
+    """Disassemble the function or symbol given a target.
+
+    It returns the disassembly content in a string object.
+    """
+    buf = StringIO.StringIO()
+    insts = function_or_symbol.GetInstructions(target)
+    for i in lldb_iter(insts, 'GetSize', 'GetInstructionAtIndex'):
+        print >> buf, i
+    return buf.getvalue()
+
+
 # ==========================================================
 # Integer (byte size 1, 2, 4, and 8) to bytearray conversion
 # ==========================================================

Added: lldb/trunk/test/python_api/function_symbol/Makefile
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/function_symbol/Makefile?rev=126955&view=auto
==============================================================================
--- lldb/trunk/test/python_api/function_symbol/Makefile (added)
+++ lldb/trunk/test/python_api/function_symbol/Makefile Thu Mar  3 13:14:00 2011
@@ -0,0 +1,5 @@
+LEVEL = ../../make
+
+C_SOURCES := main.c
+
+include $(LEVEL)/Makefile.rules

Added: lldb/trunk/test/python_api/function_symbol/TestDisasmAPI.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/function_symbol/TestDisasmAPI.py?rev=126955&view=auto
==============================================================================
--- lldb/trunk/test/python_api/function_symbol/TestDisasmAPI.py (added)
+++ lldb/trunk/test/python_api/function_symbol/TestDisasmAPI.py Thu Mar  3 13:14:00 2011
@@ -0,0 +1,121 @@
+"""
+Test retrieval of SBAddress from function/symbol, disassembly, and SBAddress APIs.
+"""
+
+import os, time
+import re
+import unittest2
+import lldb, lldbutil
+from lldbtest import *
+
+class DisasmAPITestCase(TestBase):
+
+    mydir = os.path.join("python_api", "function_symbol")
+
+    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
+    @python_api_test
+    def test_with_dsym(self):
+        """Exercise getting SBAddress objects, disassembly, and SBAddress APIs."""
+        self.buildDsym()
+        self.disasm_and_address_api()
+
+    @python_api_test
+    def test_with_dwarf(self):
+        """Exercise getting SBAddress objects, disassembly, and SBAddress APIs."""
+        self.buildDwarf()
+        self.disasm_and_address_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 disasm_and_address_api(self):
+        """Exercise getting SBAddress objects, disassembly, and SBAddress 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.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
+        frame0 = thread.GetFrameAtIndex(0)
+        lineEntry = frame0.GetLineEntry()
+        self.assertTrue(lineEntry.GetLine() == self.line1)
+
+        address1 = lineEntry.GetStartAddress()
+        #print "address1:", address1
+
+        # Now call SBTarget.ResolveSymbolContextForAddress() with address1.
+        context1 = target.ResolveSymbolContextForAddress(address1, lldb.eSymbolContextEverything)
+
+        self.assertTrue(context1.IsValid())
+        print "context1:", context1
+
+        # 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.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
+        frame0 = thread.GetFrameAtIndex(0)
+        lineEntry = frame0.GetLineEntry()
+        self.assertTrue(lineEntry.GetLine() == self.line2)
+
+        # Verify that the symbol and the function has the same address range per function 'a'.
+        symbol = context1.GetSymbol()
+        function = frame0.GetFunction()
+        self.assertTrue(symbol.IsValid() and function.IsValid())
+
+        print "symbol:", symbol
+        print "disassembly=>\n", lldbutil.disassemble(target, symbol)
+
+        print "function:", function
+        print "disassembly=>\n", lldbutil.disassemble(target, function)
+
+        sa1 = symbol.GetStartAddress()
+        #print "sa1:", sa1
+        #ea1 = symbol.GetEndAddress()
+        #print "ea1:", ea1
+        sa2 = function.GetStartAddress()
+        #print "sa2:", sa2
+        #ea2 = function.GetEndAddress()
+        #print "ea2:", ea2
+
+        stream1 = lldb.SBStream()
+        sa1.GetDescription(stream1)
+        stream2 = lldb.SBStream()
+        sa2.GetDescription(stream2)
+
+        self.expect(stream1.GetData(), "The two starting addresses should be the same", exe=False,
+            startstr = stream2.GetData())
+
+        
+if __name__ == '__main__':
+    import atexit
+    lldb.SBDebugger.Initialize()
+    atexit.register(lambda: lldb.SBDebugger.Terminate())
+    unittest2.main()

Added: lldb/trunk/test/python_api/function_symbol/main.c
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/function_symbol/main.c?rev=126955&view=auto
==============================================================================
--- lldb/trunk/test/python_api/function_symbol/main.c (added)
+++ lldb/trunk/test/python_api/function_symbol/main.c Thu Mar  3 13:14:00 2011
@@ -0,0 +1,60 @@
+//===-- 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 APIs SBTarget, SBFrame,
+// SBFunction, SBSymbol, and SBAddress.
+//
+// When stopped on breakppint 1, we can get the line entry using SBFrame API
+// SBFrame.GetLineEntry().  We'll get the start address for the the line entry
+// with the SBAddress type, resolve the symbol context using the SBTarget API
+// SBTarget.ResolveSymbolContextForAddress() in order to get the SBSymbol.
+//
+// We then stop at breakpoint 2, get the SBFrame, and the the SBFunction object.
+//
+// The address from calling GetStartAddress() on the symbol and the function
+// should point to the same address, and we also verify that.
+
+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;
+}

Modified: lldb/trunk/test/python_api/target/TestTargetAPI.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/target/TestTargetAPI.py?rev=126955&r1=126954&r2=126955&view=diff
==============================================================================
--- lldb/trunk/test/python_api/target/TestTargetAPI.py (original)
+++ lldb/trunk/test/python_api/target/TestTargetAPI.py Thu Mar  3 13:14:00 2011
@@ -43,8 +43,8 @@
         # 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
+        #print "breakpoint1:", breakpoint1
+        #print "breakpoint2:", breakpoint2
         self.assertTrue(breakpoint1.IsValid() and
                         breakpoint1.GetNumLocations() == 1,
                         VALID_BREAKPOINT)
@@ -62,7 +62,8 @@
         # 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")
+        self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
+        #self.runCmd("process status")
         frame0 = thread.GetFrameAtIndex(0)
         lineEntry = frame0.GetLineEntry()
         self.assertTrue(lineEntry.GetLine() == self.line1)
@@ -73,30 +74,31 @@
         self.process.Continue()
         self.assertTrue(self.process.GetState() == lldb.eStateStopped)
         thread = lldbutil.get_stopped_thread(self.process, lldb.eStopReasonBreakpoint)
-        self.runCmd("process status")
+        self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
+        #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
+        #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
+        #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
+        #print "symbol1:", symbol1
+        #print "symbol2:", symbol2
 
         stream1 = lldb.SBStream()
         symbol1.GetDescription(stream1)





More information about the lldb-commits mailing list