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