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