1"""
2Test SBSymbolContext APIs.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class SymbolContextTwoFilesTestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16
17    @expectedFailureAll(oslist=["windows"])
18    def test_lookup_by_address(self):
19        """Test lookup by address in a module with multiple compilation units"""
20        self.build()
21        exe = self.getBuildArtifact("a.out")
22        target = self.dbg.CreateTarget(exe)
23        self.assertTrue(target, VALID_TARGET)
24
25        module = target.GetModuleAtIndex(0)
26        self.assertTrue(module.IsValid())
27        for symbol_name in ["struct1::f()", "struct2::f()"]:
28            sc_list = module.FindFunctions(symbol_name, lldb.eSymbolTypeCode)
29            self.assertTrue(1, sc_list.GetSize())
30            symbol_address = sc_list.GetContextAtIndex(
31                0).GetSymbol().GetStartAddress()
32            self.assertTrue(symbol_address.IsValid())
33            sc_by_address = module.ResolveSymbolContextForAddress(
34                symbol_address, lldb.eSymbolContextFunction)
35            self.assertEqual(symbol_name,
36                             sc_by_address.GetFunction().GetName())
37
38    def test_ranges_in_multiple_compile_unit(self):
39        """This test verifies that we correctly handle the case when multiple
40        compile unit contains DW_AT_ranges and DW_AT_ranges_base attributes."""
41        self.build()
42        exe = self.getBuildArtifact("a.out")
43        target = self.dbg.CreateTarget(exe)
44        self.assertTrue(target, VALID_TARGET)
45
46        source1 = "file1.cpp"
47        line1 = line_number(source1, '// Break1')
48        breakpoint1 = target.BreakpointCreateByLocation(source1, line1)
49        self.assertIsNotNone(breakpoint1)
50        self.assertTrue(breakpoint1.IsValid())
51
52        source2 = "file2.cpp"
53        line2 = line_number(source2, '// Break2')
54        breakpoint2 = target.BreakpointCreateByLocation(source2, line2)
55        self.assertIsNotNone(breakpoint2)
56        self.assertTrue(breakpoint2.IsValid())
57
58        process = target.LaunchSimple(None, None, self.get_process_working_directory())
59        self.assertIsNotNone(process, PROCESS_IS_VALID)
60
61        threads = lldbutil.get_threads_stopped_at_breakpoint(
62            process, breakpoint2)
63        self.assertEqual(len(threads), 1)
64        frame = threads[0].GetFrameAtIndex(0)
65        value = frame.FindVariable("x")
66        self.assertTrue(value.IsValid())
67
68        process.Continue()
69
70        threads = lldbutil.get_threads_stopped_at_breakpoint(
71            process, breakpoint1)
72        self.assertEqual(len(threads), 1)
73        frame = threads[0].GetFrameAtIndex(0)
74        value = frame.FindVariable("x")
75        self.assertTrue(value.IsValid())
76
77        process.Continue()
78