blob: 30d62acab45c2138e2475b63684b8798a471c100 [file] [log] [blame]
Johnny Chen6becf1c2010-12-16 01:41:37 +00001"""
2Test SBSymbolContext APIs.
3"""
4
5import os, time
6import re
7import unittest2
8import lldb, lldbutil
9from lldbtest import *
10
11class SymbolContextAPITestCase(TestBase):
12
13 mydir = os.path.join("python_api", "symbol-context")
14
15 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000017 @dsym_test
Johnny Chen6becf1c2010-12-16 01:41:37 +000018 def test_with_dsym(self):
19 """Exercise SBSymbolContext API extensively."""
20 self.buildDsym()
21 self.symbol_context()
22
23 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000024 @dwarf_test
Johnny Chen6becf1c2010-12-16 01:41:37 +000025 def test_with_dwarf(self):
26 """Exercise SBSymbolContext API extensively."""
27 self.buildDwarf()
28 self.symbol_context()
29
30 def setUp(self):
31 # Call super's setUp().
32 TestBase.setUp(self)
33 # Find the line number to of function 'c'.
34 self.line = line_number('main.c', '// Find the line number of function "c" here.')
35
36 def symbol_context(self):
37 """Get an SBSymbolContext object and call its many methods."""
38 exe = os.path.join(os.getcwd(), "a.out")
39
40 # Create a target by the debugger.
41 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000042 self.assertTrue(target, VALID_TARGET)
Johnny Chen6becf1c2010-12-16 01:41:37 +000043
44 # Now create a breakpoint on main.c by name 'c'.
45 breakpoint = target.BreakpointCreateByName('c', 'a.out')
46 #print "breakpoint:", breakpoint
Johnny Chen4ebd0192011-05-24 18:22:45 +000047 self.assertTrue(breakpoint and
Johnny Chen6becf1c2010-12-16 01:41:37 +000048 breakpoint.GetNumLocations() == 1,
49 VALID_BREAKPOINT)
50
51 # Now launch the process, and do not stop at entry point.
Johnny Chen5a0bee72011-06-15 22:14:12 +000052 process = target.LaunchSimple(None, None, os.getcwd())
53 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chen6becf1c2010-12-16 01:41:37 +000054
55 # Frame #0 should be on self.line.
Johnny Chend61816b2011-03-03 01:41:57 +000056 from lldbutil import get_stopped_thread
Johnny Chen5a0bee72011-06-15 22:14:12 +000057 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +000058 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint")
Johnny Chend61816b2011-03-03 01:41:57 +000059 frame0 = thread.GetFrameAtIndex(0)
Johnny Chen6becf1c2010-12-16 01:41:37 +000060 self.assertTrue(frame0.GetLineEntry().GetLine() == self.line)
61
62 # Now get the SBSymbolContext from this frame. We want everything. :-)
63 context = frame0.GetSymbolContext(lldb.eSymbolContextEverything)
Johnny Chen4ebd0192011-05-24 18:22:45 +000064 self.assertTrue(context)
Johnny Chen6becf1c2010-12-16 01:41:37 +000065
Johnny Chen62f68ee2010-12-16 18:37:46 +000066 # Get the description of this module.
Johnny Chen6becf1c2010-12-16 01:41:37 +000067 module = context.GetModule()
Johnny Chen9ae98202011-04-23 00:34:56 +000068 desc = lldbutil.get_description(module)
69 self.expect(desc, "The module should match", exe=False,
Johnny Chen6becf1c2010-12-16 01:41:37 +000070 substrs = [os.path.join(self.mydir, 'a.out')])
Johnny Chen6becf1c2010-12-16 01:41:37 +000071
72 compileUnit = context.GetCompileUnit()
Greg Clayton81e871e2012-02-04 02:27:34 +000073 self.expect(str(compileUnit), "The compile unit should match", exe=False,
Johnny Chen6becf1c2010-12-16 01:41:37 +000074 substrs = [os.path.join(self.mydir, 'main.c')])
Johnny Chen6becf1c2010-12-16 01:41:37 +000075
76 function = context.GetFunction()
Johnny Chen4ebd0192011-05-24 18:22:45 +000077 self.assertTrue(function)
Johnny Chen6becf1c2010-12-16 01:41:37 +000078 #print "function:", function
79
80 block = context.GetBlock()
Johnny Chen4ebd0192011-05-24 18:22:45 +000081 self.assertTrue(block)
Johnny Chen6becf1c2010-12-16 01:41:37 +000082 #print "block:", block
83
84 lineEntry = context.GetLineEntry()
Johnny Chend61816b2011-03-03 01:41:57 +000085 #print "line entry:", lineEntry
Johnny Chen6becf1c2010-12-16 01:41:37 +000086 self.expect(lineEntry.GetFileSpec().GetDirectory(), "The line entry should have the correct directory",
87 exe=False,
88 substrs = [self.mydir])
89 self.expect(lineEntry.GetFileSpec().GetFilename(), "The line entry should have the correct filename",
90 exe=False,
91 substrs = ['main.c'])
92 self.assertTrue(lineEntry.GetLine() == self.line,
93 "The line entry's line number should match ")
94
95 symbol = context.GetSymbol()
96 self.assertTrue(function.GetName() == symbol.GetName() and symbol.GetName() == 'c',
97 "The symbol name should be 'c'")
98
99
100if __name__ == '__main__':
101 import atexit
102 lldb.SBDebugger.Initialize()
103 atexit.register(lambda: lldb.SBDebugger.Terminate())
104 unittest2.main()