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