xref: /llvm-project/lldb/test/API/python_api/module_section/TestModuleAndSection.py (revision 193259cbcec77add8e189c4dedeefb15fef50d5e)
1"""
2Test some SBModule and SBSection APIs.
3"""
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9from lldbsuite.test.lldbutil import symbol_type_to_str
10
11
12class ModuleAndSectionAPIsTestCase(TestBase):
13
14    # Py3 asserts due to a bug in SWIG.  A fix for this was upstreamed into
15    # SWIG 3.0.8.
16    @skipIf(py_version=['>=', (3, 0)], swig_version=['<', (3, 0, 8)])
17    def test_module_and_section(self):
18        """Test module and section APIs."""
19        self.build()
20        exe = self.getBuildArtifact("a.out")
21
22        target = self.dbg.CreateTarget(exe)
23        self.assertTrue(target, VALID_TARGET)
24        self.assertTrue(target.GetNumModules() > 0)
25
26        # Hide stdout if not running with '-t' option.
27        if not self.TraceOn():
28            self.HideStdout()
29
30        print("Number of modules for the target: %d" % target.GetNumModules())
31        for module in target.module_iter():
32            print(module)
33
34        # Get the executable module at index 0.
35        exe_module = target.GetModuleAtIndex(0)
36
37        print("Exe module: %s" % str(exe_module))
38        print("Number of sections: %d" % exe_module.GetNumSections())
39        print("Number of symbols: %d" % len(exe_module))
40        INDENT = ' ' * 4
41        INDENT2 = INDENT * 2
42        for sec in exe_module.section_iter():
43            print(sec)
44            print(
45                INDENT +
46                "Number of subsections: %d" %
47                sec.GetNumSubSections())
48            if sec.GetNumSubSections() == 0:
49                for sym in exe_module.symbol_in_section_iter(sec):
50                    print(INDENT + str(sym))
51                    print(
52                        INDENT +
53                        "symbol type: %s" %
54                        symbol_type_to_str(
55                            sym.GetType()))
56            else:
57                for subsec in sec:
58                    print(INDENT + str(subsec))
59                    # Now print the symbols belonging to the subsection....
60                    for sym in exe_module.symbol_in_section_iter(subsec):
61                        print(INDENT2 + str(sym))
62                        print(
63                            INDENT2 +
64                            "symbol type: %s" %
65                            symbol_type_to_str(
66                                sym.GetType()))
67
68    def test_module_and_section_boundary_condition(self):
69        """Test module and section APIs by passing None when it expects a Python string."""
70        self.build()
71        exe = self.getBuildArtifact("a.out")
72
73        target = self.dbg.CreateTarget(exe)
74        self.assertTrue(target, VALID_TARGET)
75        self.assertTrue(target.GetNumModules() > 0)
76
77        # Hide stdout if not running with '-t' option.
78        if not self.TraceOn():
79            self.HideStdout()
80
81        print("Number of modules for the target: %d" % target.GetNumModules())
82        for module in target.module_iter():
83            print(module)
84
85        # Get the executable module at index 0.
86        exe_module = target.GetModuleAtIndex(0)
87
88        print("Exe module: %s" % str(exe_module))
89        print("Number of sections: %d" % exe_module.GetNumSections())
90
91        # Boundary condition testings.  Should not crash lldb!
92        exe_module.FindFirstType(None)
93        exe_module.FindTypes(None)
94        exe_module.FindGlobalVariables(target, None, 1)
95        exe_module.FindFunctions(None, 0)
96        exe_module.FindSection(None)
97
98        # Get the section at index 1.
99        if exe_module.GetNumSections() > 1:
100            sec1 = exe_module.GetSectionAtIndex(1)
101            print(sec1)
102        else:
103            sec1 = None
104
105        if sec1:
106            sec1.FindSubSection(None)
107
108    def test_module_compile_unit_iter(self):
109        """Test module's compile unit iterator APIs."""
110        self.build()
111        exe = self.getBuildArtifact("a.out")
112
113        target = self.dbg.CreateTarget(exe)
114        self.assertTrue(target, VALID_TARGET)
115        self.assertTrue(target.GetNumModules() > 0)
116
117        # Hide stdout if not running with '-t' option.
118        if not self.TraceOn():
119            self.HideStdout()
120
121        print("Number of modules for the target: %d" % target.GetNumModules())
122        for module in target.module_iter():
123            print(module)
124
125        # Get the executable module at index 0.
126        exe_module = target.GetModuleAtIndex(0)
127
128        print("Exe module: %s" % str(exe_module))
129        print("Number of compile units: %d" % exe_module.GetNumCompileUnits())
130        INDENT = ' ' * 4
131        INDENT2 = INDENT * 2
132        for cu in exe_module.compile_unit_iter():
133            print(cu)
134
135    def test_find_compile_units(self):
136        """Exercise SBModule.FindCompileUnits() API."""
137        d = {'EXE': 'b.out'}
138        self.build(dictionary=d)
139        self.setTearDownCleanup(dictionary=d)
140        self.find_compile_units(self.getBuildArtifact('b.out'))
141
142    def find_compile_units(self, exe):
143        """Exercise SBModule.FindCompileUnits() API."""
144        source_name_list = ["main.cpp", "b.cpp", "c.cpp"]
145
146        # Create a target by the debugger.
147        target = self.dbg.CreateTarget(exe)
148        self.assertTrue(target, VALID_TARGET)
149
150        num_modules = target.GetNumModules()
151        for i in range(num_modules):
152            module = target.GetModuleAtIndex(i)
153            for source_name in source_name_list:
154                list = module.FindCompileUnits(lldb.SBFileSpec(source_name, False))
155                for sc in list:
156                    self.assertTrue(
157                        sc.GetCompileUnit().GetFileSpec().GetFilename() ==
158                        source_name)
159