xref: /llvm-project/lldb/test/API/functionalities/module_cache/debug_index/TestDebugIndexCache.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1import glob
2import json
3import lldb
4from lldbsuite.test.decorators import *
5from lldbsuite.test.lldbtest import *
6from lldbsuite.test import lldbutil
7import os
8import time
9
10
11class DebugIndexCacheTestcase(TestBase):
12    def setUp(self):
13        # Call super's setUp().
14        TestBase.setUp(self)
15        # Set the lldb module cache directory to a directory inside the build
16        # artifacts directory so no other tests are interfered with.
17        self.cache_dir = os.path.join(self.getBuildDir(), "lldb-module-cache")
18
19    def get_module_cache_files(self, basename):
20        module_cache_glob = os.path.join(
21            self.cache_dir, "llvmcache-*%s*dwarf-index*" % (basename)
22        )
23        return glob.glob(module_cache_glob)
24
25    def get_stats(self, log_path=None):
26        """
27        Get the output of the "statistics dump" and return the JSON as a
28        python dictionary.
29        """
30        # If log_path is set, open the path and emit the output of the command
31        # for debugging purposes.
32        if log_path is not None:
33            f = open(log_path, "w")
34        else:
35            f = None
36        return_obj = lldb.SBCommandReturnObject()
37        command = "statistics dump "
38        if f:
39            f.write("(lldb) %s\n" % (command))
40        self.ci.HandleCommand(command, return_obj, False)
41        metrics_json = return_obj.GetOutput()
42        if f:
43            f.write(metrics_json)
44        return json.loads(metrics_json)
45
46    def enable_lldb_index_cache(self):
47        self.runCmd(
48            'settings set symbols.lldb-index-cache-path "%s"' % (self.cache_dir)
49        )
50        self.runCmd("settings set symbols.enable-lldb-index-cache true")
51
52    @no_debug_info_test
53    def test_with_caching_enabled(self):
54        """
55        Test module cache functionality for debug info index caching.
56
57        We test that a debug info index file is created for the debug
58        information when caching is enabled with a file that contains
59        at least one of each kind of DIE in ManualDWARFIndex::IndexSet.
60
61        The input file has DWARF that will fill in every member of the
62        ManualDWARFIndex::IndexSet class to ensure we can encode all of the
63        required information.
64
65        With caching enabled, we also verify that the appropriate statistics
66        specify that the cache file was saved to the cache.
67        """
68        self.enable_lldb_index_cache()
69        src_dir = self.getSourceDir()
70        yaml_path = os.path.join(src_dir, "exe.yaml")
71        yaml_base, ext = os.path.splitext(yaml_path)
72        obj_path = self.getBuildArtifact("main.o")
73        self.yaml2obj(yaml_path, obj_path)
74
75        # Create a target with the object file we just created from YAML
76        target = self.dbg.CreateTarget(obj_path)
77        self.assertTrue(target, VALID_TARGET)
78
79        debug_index_cache_files = self.get_module_cache_files("main.o")
80        self.assertEqual(
81            len(debug_index_cache_files),
82            1,
83            "make sure there is one file in the module cache directory (%s) for main.o that is a debug info cache"
84            % (self.cache_dir),
85        )
86
87        # Verify that the module statistics have the information that specifies
88        # if we loaded or saved the debug index and symtab to the cache
89        stats = self.get_stats()
90        module_stats = stats["modules"][0]
91        self.assertFalse(module_stats["debugInfoIndexLoadedFromCache"])
92        self.assertTrue(module_stats["debugInfoIndexSavedToCache"])
93        self.assertFalse(module_stats["symbolTableLoadedFromCache"])
94        self.assertTrue(module_stats["symbolTableSavedToCache"])
95        # Verify the top level stats track how many things were loaded or saved
96        # to the cache.
97        self.assertEqual(stats["totalDebugInfoIndexLoadedFromCache"], 0)
98        self.assertEqual(stats["totalDebugInfoIndexSavedToCache"], 1)
99        self.assertEqual(stats["totalSymbolTablesLoadedFromCache"], 0)
100        self.assertEqual(stats["totalSymbolTablesSavedToCache"], 1)
101
102    @no_debug_info_test
103    def test_with_caching_disabled(self):
104        """
105        Test module cache functionality for debug info index caching.
106
107        We test that a debug info index file is not created for the debug
108        information when caching is disabled with a file that contains
109        at least one of each kind of DIE in ManualDWARFIndex::IndexSet.
110
111        The input file has DWARF that will fill in every member of the
112        ManualDWARFIndex::IndexSet class to ensure we can encode all of the
113        required information.
114
115        With caching disabled, we also verify that the appropriate
116        statistics specify that the cache file was not saved to the cache.
117        """
118        src_dir = self.getSourceDir()
119        yaml_path = os.path.join(src_dir, "exe.yaml")
120        yaml_base, ext = os.path.splitext(yaml_path)
121        obj_path = self.getBuildArtifact("main.o")
122        self.yaml2obj(yaml_path, obj_path)
123
124        # Create a target with the object file we just created from YAML
125        target = self.dbg.CreateTarget(obj_path)
126        self.assertTrue(target, VALID_TARGET)
127
128        debug_index_cache_files = self.get_module_cache_files("main.o")
129        self.assertEqual(
130            len(debug_index_cache_files),
131            0,
132            "make sure there is no file in the module cache directory (%s) for main.o that is a debug info cache"
133            % (self.cache_dir),
134        )
135
136        # Verify that the module statistics have the information that specifies
137        # if we loaded or saved the debug index and symtab to the cache
138        stats = self.get_stats()
139        module_stats = stats["modules"][0]
140        self.assertFalse(module_stats["debugInfoIndexLoadedFromCache"])
141        self.assertFalse(module_stats["debugInfoIndexSavedToCache"])
142        self.assertFalse(module_stats["symbolTableLoadedFromCache"])
143        self.assertFalse(module_stats["symbolTableSavedToCache"])
144        # Verify the top level stats track how many things were loaded or saved
145        # to the cache.
146        self.assertEqual(stats["totalDebugInfoIndexLoadedFromCache"], 0)
147        self.assertEqual(stats["totalDebugInfoIndexSavedToCache"], 0)
148        self.assertEqual(stats["totalSymbolTablesLoadedFromCache"], 0)
149        self.assertEqual(stats["totalSymbolTablesSavedToCache"], 0)
150