diff --git a/lldb/test/lldbutil.py b/lldb/test/lldbutil.py index 9c1ff9c94379..46314647d84a 100644 --- a/lldb/test/lldbutil.py +++ b/lldb/test/lldbutil.py @@ -41,6 +41,22 @@ def lldb_iter(obj, getsize, getelem): 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 # ========================================================== diff --git a/lldb/test/python_api/function_symbol/Makefile b/lldb/test/python_api/function_symbol/Makefile new file mode 100644 index 000000000000..0d70f2595019 --- /dev/null +++ b/lldb/test/python_api/function_symbol/Makefile @@ -0,0 +1,5 @@ +LEVEL = ../../make + +C_SOURCES := main.c + +include $(LEVEL)/Makefile.rules diff --git a/lldb/test/python_api/function_symbol/TestDisasmAPI.py b/lldb/test/python_api/function_symbol/TestDisasmAPI.py new file mode 100644 index 000000000000..39e6c64cccb7 --- /dev/null +++ b/lldb/test/python_api/function_symbol/TestDisasmAPI.py @@ -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() diff --git a/lldb/test/python_api/function_symbol/main.c b/lldb/test/python_api/function_symbol/main.c new file mode 100644 index 000000000000..1eb90d5fc23e --- /dev/null +++ b/lldb/test/python_api/function_symbol/main.c @@ -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 + +// 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; +} diff --git a/lldb/test/python_api/target/TestTargetAPI.py b/lldb/test/python_api/target/TestTargetAPI.py index 0c5f806bc722..7c3bd97b7699 100644 --- a/lldb/test/python_api/target/TestTargetAPI.py +++ b/lldb/test/python_api/target/TestTargetAPI.py @@ -43,8 +43,8 @@ class TargetAPITestCase(TestBase): # 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 @@ class TargetAPITestCase(TestBase): # 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 @@ class TargetAPITestCase(TestBase): 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)