xref: /llvm-project/lldb/test/API/functionalities/source-map/TestTargetSourceMap.py (revision 4cc8f2a017c76af25234afc7c380550e9c93135c)
1import lldb
2from lldbsuite.test.lldbtest import *
3from lldbsuite.test.decorators import *
4import os
5
6
7class TestTargetSourceMap(TestBase):
8
9    @no_debug_info_test
10    def test_source_map(self):
11        """Test target.source-map' functionality."""
12
13        def assertBreakpointWithSourceMap(src_path):
14            # Set a breakpoint after we remap source and verify that it succeeds
15            bp = target.BreakpointCreateByLocation(src_path, 2)
16            self.assertEquals(bp.GetNumLocations(), 1,
17                            "make sure breakpoint was resolved with map")
18
19            # Now make sure that we can actually FIND the source file using this
20            # remapping:
21            retval = lldb.SBCommandReturnObject()
22            self.dbg.GetCommandInterpreter().HandleCommand("source list -f main.c -l 2", retval)
23            self.assertTrue(retval.Succeeded(), "source list didn't succeed.")
24            self.assertNotEqual(retval.GetOutput(), None, "We got no ouput from source list")
25            self.assertIn("return", retval.GetOutput(), "We didn't find the source file...")
26
27        # Set the target soure map to map "./" to the current test directory
28        src_dir = self.getSourceDir()
29        src_path = os.path.join(src_dir, "main.c")
30        yaml_path = os.path.join(src_dir, "a.yaml")
31        yaml_base, ext = os.path.splitext(yaml_path)
32        obj_path = self.getBuildArtifact("main.o")
33        self.yaml2obj(yaml_path, obj_path)
34
35        # Create a target with the object file we just created from YAML
36        target = self.dbg.CreateTarget(obj_path)
37
38        # Set a breakpoint before we remap source and verify that it fails
39        bp = target.BreakpointCreateByLocation(src_path, 2)
40        self.assertEquals(bp.GetNumLocations(), 0,
41                        "make sure no breakpoints were resolved without map")
42
43        valid_path = os.path.dirname(src_dir)
44        valid_path2 = os.path.dirname(valid_path)
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" . "%s' \
51                % (invalid_path, src_dir, invalid_path2, valid_path),
52            substrs=[
53                'error: the replacement path doesn\'t exist: "%s"' % (invalid_path),
54                'the replacement path doesn\'t exist: "%s"' % (invalid_path2),
55            ],
56            error=True,
57        )
58        self.expect(
59            'settings show target.source-map',
60            substrs=[
61                '[0] "." -> "%s"' % (src_dir),
62                '[1] "." -> "%s"' % (valid_path),
63            ],
64        )
65        assertBreakpointWithSourceMap(src_path)
66
67        # Attempts to replace an index to an invalid mapping should have no effect.
68        # Modifications to valid mappings should work.
69        self.expect(
70            'settings replace target.source-map 0 . "%s" . "%s"' % (invalid_path, valid_path2),
71            substrs=[
72                'error: the replacement path doesn\'t exist: "%s"' % (invalid_path),
73            ],
74            error=True,
75        )
76        self.expect(
77            'settings show target.source-map',
78            substrs=[
79                '[0] "." -> "%s"' % (src_dir),
80                '[1] "." -> "%s"' % (valid_path2),
81            ]
82        )
83        assertBreakpointWithSourceMap(src_path)
84
85        # Let's clear and add the mapping back with insert-after
86        self.runCmd('settings remove target.source-map 0')
87        self.expect(
88            'settings show target.source-map',
89            substrs=['[0] "." -> "%s"' % (valid_path2)],
90        )
91
92        self.expect(
93            'settings insert-after target.source-map 0 . "%s" . "%s" . "%s"' \
94                % (invalid_path, invalid_path2, src_dir),
95            substrs=[
96                'error: the replacement path doesn\'t exist: "%s"' % (invalid_path),
97                'the replacement path doesn\'t exist: "%s"' % (invalid_path2),
98            ],
99            error=True,
100        )
101        self.expect(
102            'settings show target.source-map',
103            substrs=[
104                '[0] "." -> "%s"' % (valid_path2),
105                '[1] "." -> "%s"' % (src_dir),
106            ]
107        )
108
109        # Let's clear using remove and add the mapping in with append
110        self.runCmd('settings remove target.source-map 1')
111        self.expect(
112            'settings show target.source-map',
113            substrs=[
114                '[0] "." -> "%s"' % (valid_path2),
115            ]
116        )
117        self.runCmd('settings clear target.source-map')
118        self.expect(
119            'settings append target.source-map . "%s" . "%s" . "%s"' % (invalid_path, src_dir, invalid_path2),
120            substrs=[
121                'error: the replacement path doesn\'t exist: "%s"' % (invalid_path),
122                'the replacement path doesn\'t exist: "%s"' % (invalid_path2),
123            ],
124            error=True,
125        )
126        self.expect(
127            'settings show target.source-map',
128            substrs=[
129                '[0] "." -> "%s"' % (src_dir),
130            ]
131        )
132        assertBreakpointWithSourceMap(src_path)
133