xref: /llvm-project/lldb/test/API/functionalities/source-map/TestTargetSourceMap.py (revision 30350c254106ce25b16b13f05e713ceb2b15ce09)
1import lldb
2from lldbsuite.test.lldbtest import *
3from lldbsuite.test.decorators import *
4import os
5
6
7class TestTargetSourceMap(TestBase):
8
9    mydir = TestBase.compute_mydir(__file__)
10
11    @no_debug_info_test
12    def test_source_map(self):
13        """Test target.source-map' functionality."""
14
15        def assertBreakpointWithSourceMap(src_path):
16            # Set a breakpoint after we remap source and verify that it succeeds
17            bp = target.BreakpointCreateByLocation(src_path, 2)
18            self.assertEquals(bp.GetNumLocations(), 1,
19                            "make sure breakpoint was resolved with map")
20
21            # Now make sure that we can actually FIND the source file using this
22            # remapping:
23            retval = lldb.SBCommandReturnObject()
24            self.dbg.GetCommandInterpreter().HandleCommand("source list -f main.c -l 2", retval)
25            self.assertTrue(retval.Succeeded(), "source list didn't succeed.")
26            self.assertNotEqual(retval.GetOutput(), None, "We got no ouput from source list")
27            self.assertTrue("return" in retval.GetOutput(), "We didn't find the source file...")
28
29        # Set the target soure map to map "./" to the current test directory
30        src_dir = self.getSourceDir()
31        src_path = os.path.join(src_dir, "main.c")
32        yaml_path = os.path.join(src_dir, "a.yaml")
33        yaml_base, ext = os.path.splitext(yaml_path)
34        obj_path = self.getBuildArtifact("main.o")
35        self.yaml2obj(yaml_path, obj_path)
36
37        # Create a target with the object file we just created from YAML
38        target = self.dbg.CreateTarget(obj_path)
39
40        # Set a breakpoint before we remap source and verify that it fails
41        bp = target.BreakpointCreateByLocation(src_path, 2)
42        self.assertEquals(bp.GetNumLocations(), 0,
43                        "make sure no breakpoints were resolved without map")
44
45        invalid_path = src_dir + "invalid_path"
46        invalid_path2 = src_dir + "invalid_path2"
47
48        # We make sure the error message contains all the invalid paths
49        self.expect(
50            'settings set target.source-map . "%s" . "%s" . "%s"' % (invalid_path, src_dir, invalid_path2),
51            substrs=[
52                'the replacement path doesn\'t exist: "%s"' % (invalid_path),
53                'the replacement path doesn\'t exist: "%s"' % (invalid_path2),
54            ],
55            error=True,
56        )
57        self.expect(
58            'settings show target.source-map',
59            substrs=['[0] "." -> "%s"' % (src_dir)],
60        )
61        assertBreakpointWithSourceMap(src_path)
62
63        # Index 0 is the valid mapping, and modifying it to an invalid one should have no effect
64        self.expect(
65            'settings replace target.source-map 0 . "%s"' % (invalid_path),
66            substrs=['error: the replacement path doesn\'t exist: "%s"' % (invalid_path)],
67            error=True,
68        )
69        self.expect(
70            'settings show target.source-map',
71            substrs=['[0] "." -> "%s"' % (src_dir)]
72        )
73        assertBreakpointWithSourceMap(src_path)
74
75        # Let's clear and add the mapping in with insert-after
76        self.runCmd('settings remove target.source-map 0')
77        self.expect(
78            'settings show target.source-map',
79            endstr="target.source-map (path-map) =\n",
80        )
81
82        # We add a valid but useless mapping so that we can use insert-after
83        another_valid_path = os.path.dirname(src_dir)
84        self.runCmd('settings set target.source-map . "%s"' % (another_valid_path))
85
86        self.expect(
87            'settings replace target.source-map 0 . "%s"' % (invalid_path),
88            substrs=['error: the replacement path doesn\'t exist: "%s"' % (invalid_path)],
89            error=True,
90        )
91        self.expect(
92            'settings show target.source-map',
93            substrs=['[0] "." -> "%s"' % (another_valid_path)]
94        )
95
96        # Let's clear and add the mapping in with append
97        self.expect('settings remove target.source-map 0')
98        self.expect(
99            'settings show target.source-map',
100            endstr="target.source-map (path-map) =\n",
101        )
102
103        self.expect(
104            'settings append target.source-map . "%s" . "%s"' % (invalid_path, src_dir),
105            substrs=['error: the replacement path doesn\'t exist: "%s"' % (invalid_path)],
106            error=True,
107        )
108        assertBreakpointWithSourceMap(src_path)
109