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