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