blob: a92315e71c3fbd9db7491755aa23f96ee6b2426e [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
Greg Clayton4570d3e2013-12-10 23:19:29 +000013 mydir = TestBase.compute_mydir(__file__)
Johnny Chen6becf1c2010-12-16 01:41:37 +000014
Johnny Chen6becf1c2010-12-16 01:41:37 +000015 def setUp(self):
16 # Call super's setUp().
17 TestBase.setUp(self)
18 # Find the line number to of function 'c'.
19 self.line = line_number('main.c', '// Find the line number of function "c" here.')
20
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000021 @python_api_test
22 @expectedFailureWindows("llvm.org/pr24778")
23 def test(self):
24 """Exercise SBSymbolContext API extensively."""
25 self.build()
Johnny Chen6becf1c2010-12-16 01:41:37 +000026 exe = os.path.join(os.getcwd(), "a.out")
27
28 # Create a target by the debugger.
29 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000030 self.assertTrue(target, VALID_TARGET)
Johnny Chen6becf1c2010-12-16 01:41:37 +000031
32 # Now create a breakpoint on main.c by name 'c'.
33 breakpoint = target.BreakpointCreateByName('c', 'a.out')
34 #print "breakpoint:", breakpoint
Johnny Chen4ebd0192011-05-24 18:22:45 +000035 self.assertTrue(breakpoint and
Johnny Chen6becf1c2010-12-16 01:41:37 +000036 breakpoint.GetNumLocations() == 1,
37 VALID_BREAKPOINT)
38
39 # Now launch the process, and do not stop at entry point.
Greg Claytonc6947512013-12-13 19:18:59 +000040 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chen5a0bee72011-06-15 22:14:12 +000041 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chen6becf1c2010-12-16 01:41:37 +000042
43 # Frame #0 should be on self.line.
Johnny Chend61816b2011-03-03 01:41:57 +000044 from lldbutil import get_stopped_thread
Johnny Chen5a0bee72011-06-15 22:14:12 +000045 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +000046 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint")
Johnny Chend61816b2011-03-03 01:41:57 +000047 frame0 = thread.GetFrameAtIndex(0)
Johnny Chen6becf1c2010-12-16 01:41:37 +000048 self.assertTrue(frame0.GetLineEntry().GetLine() == self.line)
49
50 # Now get the SBSymbolContext from this frame. We want everything. :-)
51 context = frame0.GetSymbolContext(lldb.eSymbolContextEverything)
Johnny Chen4ebd0192011-05-24 18:22:45 +000052 self.assertTrue(context)
Johnny Chen6becf1c2010-12-16 01:41:37 +000053
Johnny Chen62f68ee2010-12-16 18:37:46 +000054 # Get the description of this module.
Johnny Chen6becf1c2010-12-16 01:41:37 +000055 module = context.GetModule()
Johnny Chen9ae98202011-04-23 00:34:56 +000056 desc = lldbutil.get_description(module)
57 self.expect(desc, "The module should match", exe=False,
Johnny Chen6becf1c2010-12-16 01:41:37 +000058 substrs = [os.path.join(self.mydir, 'a.out')])
Johnny Chen6becf1c2010-12-16 01:41:37 +000059
60 compileUnit = context.GetCompileUnit()
Greg Clayton81e871e2012-02-04 02:27:34 +000061 self.expect(str(compileUnit), "The compile unit should match", exe=False,
Johnny Chen6becf1c2010-12-16 01:41:37 +000062 substrs = [os.path.join(self.mydir, 'main.c')])
Johnny Chen6becf1c2010-12-16 01:41:37 +000063
64 function = context.GetFunction()
Johnny Chen4ebd0192011-05-24 18:22:45 +000065 self.assertTrue(function)
Johnny Chen6becf1c2010-12-16 01:41:37 +000066 #print "function:", function
67
68 block = context.GetBlock()
Johnny Chen4ebd0192011-05-24 18:22:45 +000069 self.assertTrue(block)
Johnny Chen6becf1c2010-12-16 01:41:37 +000070 #print "block:", block
71
72 lineEntry = context.GetLineEntry()
Johnny Chend61816b2011-03-03 01:41:57 +000073 #print "line entry:", lineEntry
Johnny Chen6becf1c2010-12-16 01:41:37 +000074 self.expect(lineEntry.GetFileSpec().GetDirectory(), "The line entry should have the correct directory",
75 exe=False,
76 substrs = [self.mydir])
77 self.expect(lineEntry.GetFileSpec().GetFilename(), "The line entry should have the correct filename",
78 exe=False,
79 substrs = ['main.c'])
80 self.assertTrue(lineEntry.GetLine() == self.line,
81 "The line entry's line number should match ")
82
83 symbol = context.GetSymbol()
84 self.assertTrue(function.GetName() == symbol.GetName() and symbol.GetName() == 'c',
85 "The symbol name should be 'c'")
86
87
88if __name__ == '__main__':
89 import atexit
90 lldb.SBDebugger.Initialize()
91 atexit.register(lambda: lldb.SBDebugger.Terminate())
92 unittest2.main()