blob: 357387fe4ca20a536c97df6594d7ac0eb7d06fb3 [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
17 def test_with_dsym(self):
18 """Exercise SBSymbolContext API extensively."""
19 self.buildDsym()
20 self.symbol_context()
21
22 @python_api_test
23 def test_with_dwarf(self):
24 """Exercise SBSymbolContext API extensively."""
25 self.buildDwarf()
26 self.symbol_context()
27
28 def setUp(self):
29 # Call super's setUp().
30 TestBase.setUp(self)
31 # Find the line number to of function 'c'.
32 self.line = line_number('main.c', '// Find the line number of function "c" here.')
33
34 def symbol_context(self):
35 """Get an SBSymbolContext object and call its many methods."""
36 exe = os.path.join(os.getcwd(), "a.out")
37
38 # Create a target by the debugger.
39 target = self.dbg.CreateTarget(exe)
40 self.assertTrue(target.IsValid(), VALID_TARGET)
41
42 # Now create a breakpoint on main.c by name 'c'.
43 breakpoint = target.BreakpointCreateByName('c', 'a.out')
44 #print "breakpoint:", breakpoint
45 self.assertTrue(breakpoint.IsValid() and
46 breakpoint.GetNumLocations() == 1,
47 VALID_BREAKPOINT)
48
49 # Now launch the process, and do not stop at entry point.
Greg Clayton6f907e62011-01-23 17:46:22 +000050 error = lldb.SBError()
51 self.process = target.Launch (None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error)
Johnny Chen6becf1c2010-12-16 01:41:37 +000052
53 self.process = target.GetProcess()
54 self.assertTrue(self.process.IsValid(), PROCESS_IS_VALID)
55
56 # Frame #0 should be on self.line.
57 frame0 = self.process.GetThreadAtIndex(0).GetFrameAtIndex(0)
58 self.assertTrue(frame0.GetLineEntry().GetLine() == self.line)
59
60 # Now get the SBSymbolContext from this frame. We want everything. :-)
61 context = frame0.GetSymbolContext(lldb.eSymbolContextEverything)
62 self.assertTrue(context.IsValid())
63
Johnny Chen62f68ee2010-12-16 18:37:46 +000064 # Get the description of this module.
Johnny Chen6becf1c2010-12-16 01:41:37 +000065 module = context.GetModule()
Johnny Chen62f68ee2010-12-16 18:37:46 +000066 stream = lldb.SBStream()
67 module.GetDescription(stream)
68 self.expect(stream.GetData(), "The module should match", exe=False,
Johnny Chen6becf1c2010-12-16 01:41:37 +000069 substrs = [os.path.join(self.mydir, 'a.out')])
Johnny Chen6becf1c2010-12-16 01:41:37 +000070
71 compileUnit = context.GetCompileUnit()
72 self.expect(repr(compileUnit), "The compile unit should match", exe=False,
73 substrs = [os.path.join(self.mydir, 'main.c')])
Johnny Chen6becf1c2010-12-16 01:41:37 +000074
75 function = context.GetFunction()
76 self.assertTrue(function.IsValid())
77 #print "function:", function
78
79 block = context.GetBlock()
80 self.assertTrue(block.IsValid())
81 #print "block:", block
82
83 lineEntry = context.GetLineEntry()
84 self.expect(lineEntry.GetFileSpec().GetDirectory(), "The line entry should have the correct directory",
85 exe=False,
86 substrs = [self.mydir])
87 self.expect(lineEntry.GetFileSpec().GetFilename(), "The line entry should have the correct filename",
88 exe=False,
89 substrs = ['main.c'])
90 self.assertTrue(lineEntry.GetLine() == self.line,
91 "The line entry's line number should match ")
92
93 symbol = context.GetSymbol()
94 self.assertTrue(function.GetName() == symbol.GetName() and symbol.GetName() == 'c',
95 "The symbol name should be 'c'")
96
97
98if __name__ == '__main__':
99 import atexit
100 lldb.SBDebugger.Initialize()
101 atexit.register(lambda: lldb.SBDebugger.Terminate())
102 unittest2.main()