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    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.line_a = line_number(
21            'a.c', '// Set file and line breakpoint inside a().')
22        self.line_b = line_number(
23            'b.c', '// Set file and line breakpoint inside b().')
24        self.line_c = line_number(
25            'c.c', '// Set file and line breakpoint inside c().')
26        self.cache_dir = os.path.join(self.getBuildDir(), 'lldb-module-cache')
27        # Set the lldb module cache directory to a directory inside the build
28        # artifacts directory so no other tests are interfered with.
29        self.runCmd('settings set symbols.lldb-index-cache-path "%s"' % (self.cache_dir))
30        self.runCmd('settings set symbols.enable-lldb-index-cache true')
31        self.build()
32
33
34    def get_module_cache_files(self, basename):
35        module_cache_glob = os.path.join(self.cache_dir, "llvmcache-*%s*symtab*" % (basename))
36        return glob.glob(module_cache_glob)
37
38
39    # Requires no dSYM, so we let the Makefile make the right stuff for us
40    @no_debug_info_test
41    @skipUnlessDarwin
42    def test(self):
43        """
44            Test module cache functionality for bsd archive object files.
45
46            This will test that if we enable the module cache, we have a
47            corresponding cache entry for the .o files in libfoo.a.
48
49            The static library has two entries for "a.o":
50            - one from a.c
51            - one from c.c which had c.o renamed to a.o and then put into the
52              libfoo.a as an extra .o file with different contents from the
53              original a.o
54
55            We do this to test that we can correctly cache duplicate .o files
56            that appear in .a files.
57
58            This test only works on darwin because of the way DWARF is stored
59            where the debug map will refer to .o files inside of .a files.
60        """
61        exe = self.getBuildArtifact("a.out")
62
63        # Create a module with no depedencies.
64        target = self.createTestTarget(load_dependent_modules=False)
65
66        self.runCmd('breakpoint set -f a.c -l %d' % (self.line_a))
67        self.runCmd('breakpoint set -f b.c -l %d' % (self.line_b))
68        self.runCmd('breakpoint set -f c.c -l %d' % (self.line_c))
69
70        # Get the executable module and get the number of symbols to make
71        # sure the symbol table gets parsed and cached. The module cache is
72        # enabled in the setUp() function.
73        main_module = target.GetModuleAtIndex(0)
74        self.assertTrue(main_module.IsValid())
75        # Make sure the symbol table gets loaded and cached
76        main_module.GetNumSymbols()
77        a_o_cache_files = self.get_module_cache_files("libfoo.a(a.o)")
78        b_o_cache_files = self.get_module_cache_files("libfoo.a(b.o)")
79        # We expect the directory for a.o to have two cache directories:
80        # - 1 for the a.o with a earlier mod time
81        # - 1 for the a.o that was renamed from c.o that should be 2 seconds older
82        self.assertEqual(len(a_o_cache_files), 2,
83                "make sure there are two files in the module cache directory (%s) for libfoo.a(a.o)" % (self.cache_dir))
84        self.assertEqual(len(b_o_cache_files), 1,
85                "make sure there are two files in the module cache directory (%s) for libfoo.a(b.o)" % (self.cache_dir))
86