xref: /llvm-project/lldb/test/API/functionalities/module_cache/simple_exe/TestModuleCacheSimple.py (revision 59f1d0eed58ce0f23c3a5a324d122a99f9757228)
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 ModuleCacheTestcaseSimple(TestBase):
13
14    mydir = TestBase.compute_mydir(__file__)
15
16    def setUp(self):
17        # Call super's setUp().
18        TestBase.setUp(self)
19        # Find the line number in a(int) to break at.
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('settings set symbols.lldb-index-cache-path "%s"' % (self.cache_dir))
24        self.runCmd('settings set symbols.enable-lldb-index-cache true')
25        self.build()
26
27
28    def get_module_cache_files(self, basename):
29        module_file_glob = os.path.join(self.cache_dir, "llvmcache-*%s*" % (basename))
30        return glob.glob(module_file_glob)
31
32    # Doesn't depend on any specific debug information.
33    @no_debug_info_test
34    @skipIfWindows # Windows runs into trouble deleting the executable
35    def test(self):
36        """
37            Test module cache functionality for a simple object file.
38
39            This will test that if we enable the module cache, we have a
40            corresponding index cache entry for the symbol table for the
41            executable. It also removes the executable, rebuilds so that the
42            modification time of the binary gets updated, and then creates a new
43            target and should cause the cache to get updated so the cache file
44            should get an updated modification time.
45        """
46        exe = self.getBuildArtifact("a.out")
47
48        # Create a module with no depedencies.
49        target = self.createTestTarget(load_dependent_modules=False)
50
51        # Get the executable module and get the number of symbols to make
52        # sure the symbol table gets parsed and cached. The module cache is
53        # enabled in the setUp() function.
54        main_module = target.GetModuleAtIndex(0)
55        self.assertTrue(main_module.IsValid())
56        # Make sure the symbol table gets loaded and cached
57        main_module.GetNumSymbols()
58        cache_files = self.get_module_cache_files("a.out")
59        self.assertEqual(len(cache_files), 1,
60                         "make sure there is only one cache file for 'a.out'")
61        symtab_cache_path = cache_files[0]
62        exe_mtime_1 = os.path.getmtime(exe)
63        symtab_mtime_1 = os.path.getmtime(symtab_cache_path)
64        # Now remove the executable and sleep for a few seconds to make sure we
65        # get a different creation and modification time for the file since some
66        # OSs store the modification time in seconds since Jan 1, 1970.
67        os.remove(exe)
68        self.assertEqual(os.path.exists(exe), False,
69                         'make sure we were able to remove the executable')
70        time.sleep(2)
71        # Now rebuild the binary so it has a different content which should
72        # update the UUID to make the cache miss when it tries to load the
73        # symbol table from the binary at the same path.
74        self.build(dictionary={'CFLAGS_EXTRAS': '-DEXTRA_FUNCTION'})
75        self.assertEqual(os.path.exists(exe), True,
76                         'make sure executable exists after rebuild')
77        # Make sure the modification time has changed or this test will fail.
78        exe_mtime_2 = os.path.getmtime(exe)
79        self.assertNotEqual(
80                exe_mtime_1,
81                exe_mtime_2,
82                "make sure the modification time of the executable has changed")
83        # Make sure the module cache still has an out of date cache with the
84        # same old modification time.
85        self.assertEqual(symtab_mtime_1,
86                         os.path.getmtime(symtab_cache_path),
87                         "check that the 'symtab' cache file modification time doesn't match the executable modification time after rebuild")
88        # Create a new target and get the symbols again, and make sure the cache
89        # gets updated for the symbol table cache
90        target = self.createTestTarget(load_dependent_modules=False)
91        main_module = target.GetModuleAtIndex(0)
92        self.assertTrue(main_module.IsValid())
93        main_module.GetNumSymbols()
94        self.assertEqual(os.path.exists(symtab_cache_path), True,
95                         'make sure "symtab" cache files exists after cache is updated')
96        symtab_mtime_2 = os.path.getmtime(symtab_cache_path)
97        self.assertNotEqual(
98                symtab_mtime_1,
99                symtab_mtime_2,
100                'make sure modification time of "symtab-..." changed')
101