xref: /llvm-project/lldb/test/API/functionalities/module_cache/bsd/TestModuleCacheBSD.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""Test the LLDB module cache funcionality."""
2
3import glob
4import lldb
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8import os
9import time
10
11
12class ModuleCacheTestcaseBSD(TestBase):
13    def setUp(self):
14        # Call super's setUp().
15        TestBase.setUp(self)
16        # Find the line number in a(int) to break at.
17        self.line_a = line_number("a.c", "// Set file and line breakpoint inside a().")
18        self.line_b = line_number("b.c", "// Set file and line breakpoint inside b().")
19        self.line_c = line_number("c.c", "// Set file and line breakpoint inside c().")
20        self.cache_dir = os.path.join(self.getBuildDir(), "lldb-module-cache")
21        # Set the lldb module cache directory to a directory inside the build
22        # artifacts directory so no other tests are interfered with.
23        self.runCmd(
24            'settings set symbols.lldb-index-cache-path "%s"' % (self.cache_dir)
25        )
26        self.runCmd("settings set symbols.enable-lldb-index-cache true")
27        self.build()
28
29    def get_module_cache_files(self, basename):
30        module_cache_glob = os.path.join(
31            self.cache_dir, "llvmcache-*%s*symtab*" % (basename)
32        )
33        return glob.glob(module_cache_glob)
34
35    # Requires no dSYM, so we let the Makefile make the right stuff for us
36    @no_debug_info_test
37    @skipUnlessDarwin
38    def test(self):
39        """
40        This test has been modified to make sure .o files that don't have
41        UUIDs are not cached after discovering build systems that play with
42        modification times of .o files that the modification times are not
43        unique enough to ensure the .o file within the .a file are the right
44        files as this was causing older cache files to be accepted for new
45        updated .o files.
46
47        ELF .o files do calculate a UUID from the contents of the file,
48        which is expensive, but no one loads .o files into a debug sessions
49        when using ELF files. Mach-o .o files do not have UUID values and do
50        no calculate one as they _are_ used during debug sessions when no
51        dSYM file is generated. If we can find a way to uniquely and cheaply
52        create UUID values for mach-o .o files in the future, this test will
53        be updated to test this functionality. This test will now make sure
54        there are no cache entries for any .o files in BSD archives.
55
56        The old test case description is below in case we enable caching for
57        .o files again:
58
59        Test module cache functionality for bsd archive object files.
60
61        This will test that if we enable the module cache, we have a
62        corresponding cache entry for the .o files in libfoo.a.
63
64        The static library has two entries for "a.o":
65        - one from a.c
66        - one from c.c which had c.o renamed to a.o and then put into the
67          libfoo.a as an extra .o file with different contents from the
68          original a.o
69
70        We do this to test that we can correctly cache duplicate .o files
71        that appear in .a files.
72
73        This test only works on darwin because of the way DWARF is stored
74        where the debug map will refer to .o files inside of .a files.
75        """
76        exe = self.getBuildArtifact("a.out")
77
78        # Create a module with no dependencies.
79        target = self.createTestTarget(load_dependent_modules=False)
80
81        self.runCmd("breakpoint set -f a.c -l %d" % (self.line_a))
82        self.runCmd("breakpoint set -f b.c -l %d" % (self.line_b))
83        self.runCmd("breakpoint set -f c.c -l %d" % (self.line_c))
84
85        # Get the executable module and get the number of symbols to make
86        # sure the symbol table gets parsed and cached. The module cache is
87        # enabled in the setUp() function.
88        main_module = target.GetModuleAtIndex(0)
89        self.assertTrue(main_module.IsValid())
90        # Make sure the symbol table gets loaded and cached
91        main_module.GetNumSymbols()
92        a_o_cache_files = self.get_module_cache_files("libfoo.a(a.o)")
93        b_o_cache_files = self.get_module_cache_files("libfoo.a(b.o)")
94
95        # We expect the directory for a.o to have two cache directories:
96        # - 1 for the a.o with a earlier mod time
97        # - 1 for the a.o that was renamed from c.o that should be 2 seconds older
98        # self.assertEqual(len(a_o_cache_files), 2,
99        #         "make sure there are two files in the module cache directory (%s) for libfoo.a(a.o)" % (self.cache_dir))
100        # self.assertEqual(len(b_o_cache_files), 1,
101        #         "make sure there are two files in the module cache directory (%s) for libfoo.a(b.o)" % (self.cache_dir))
102
103        # We are no longer caching .o files in the lldb index cache. If we ever
104        # re-enable this functionality, we can uncomment out the above lines of
105        # code.
106        self.assertEqual(
107            len(a_o_cache_files),
108            0,
109            "make sure there are no files in the module cache directory (%s) for libfoo.a(a.o)"
110            % (self.cache_dir),
111        )
112        self.assertEqual(
113            len(b_o_cache_files),
114            0,
115            "make sure there are no files in the module cache directory (%s) for libfoo.a(b.o)"
116            % (self.cache_dir),
117        )
118