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