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