1 """ 2 Test some SBModule and SBSection APIs. 3 """ 4 5 import os, time 6 import re 7 import unittest2 8 import lldb 9 from lldbtest import * 10 from lldbutil import symbol_type_to_str 11 12 class ModuleAndSectionAPIsTestCase(TestBase): 13 14 mydir = os.path.join("python_api", "module_section") 15 16 @python_api_test 17 def test_module_and_section(self): 18 """Test module and section APIs.""" 19 self.buildDefault() 20 self.module_and_section() 21 22 @python_api_test 23 def test_module_and_section_boundary_condition(self): 24 """Test module and section APIs by passing None when it expects a Python string.""" 25 self.buildDefault() 26 self.module_and_section_boundary_condition() 27 28 @python_api_test 29 def test_module_compile_unit_iter(self): 30 """Test module's compile unit iterator APIs.""" 31 self.buildDefault() 32 self.module_compile_unit_iter() 33 34 def module_and_section(self): 35 exe = os.path.join(os.getcwd(), "a.out") 36 37 target = self.dbg.CreateTarget(exe) 38 self.assertTrue(target, VALID_TARGET) 39 self.assertTrue(target.GetNumModules() > 0) 40 41 # Hide stdout if not running with '-t' option. 42 if not self.TraceOn(): 43 self.HideStdout() 44 45 print "Number of modules for the target: %d" % target.GetNumModules() 46 for module in target.module_iter(): 47 print module 48 49 # Get the executable module at index 0. 50 exe_module = target.GetModuleAtIndex(0) 51 52 print "Exe module: %s" % str(exe_module) 53 print "Number of sections: %d" % exe_module.GetNumSections() 54 INDENT = ' ' * 4 55 INDENT2 = INDENT * 2 56 for sec in exe_module.section_iter(): 57 print sec 58 print INDENT + "Number of subsections: %d" % sec.GetNumSubSections() 59 if sec.GetNumSubSections() == 0: 60 for sym in exe_module.symbol_in_section_iter(sec): 61 print INDENT + str(sym) 62 print INDENT + "symbol type: %s" % symbol_type_to_str(sym.GetType()) 63 else: 64 for subsec in sec: 65 print INDENT + str(subsec) 66 # Now print the symbols belonging to the subsection.... 67 for sym in exe_module.symbol_in_section_iter(subsec): 68 print INDENT2 + str(sym) 69 print INDENT2 + "symbol type: %s" % symbol_type_to_str(sym.GetType()) 70 71 def module_and_section_boundary_condition(self): 72 exe = os.path.join(os.getcwd(), "a.out") 73 74 target = self.dbg.CreateTarget(exe) 75 self.assertTrue(target, VALID_TARGET) 76 self.assertTrue(target.GetNumModules() > 0) 77 78 # Hide stdout if not running with '-t' option. 79 if not self.TraceOn(): 80 self.HideStdout() 81 82 print "Number of modules for the target: %d" % target.GetNumModules() 83 for module in target.module_iter(): 84 print module 85 86 # Get the executable module at index 0. 87 exe_module = target.GetModuleAtIndex(0) 88 89 print "Exe module: %s" % str(exe_module) 90 print "Number of sections: %d" % exe_module.GetNumSections() 91 92 # Boundary condition testings. Should not crash lldb! 93 exe_module.FindFirstType(None) 94 exe_module.FindTypes(None) 95 exe_module.FindGlobalVariables(target, None, 1) 96 exe_module.FindFunctions(None, 0) 97 exe_module.FindSection(None) 98 99 # Get the section at index 1. 100 if exe_module.GetNumSections() > 1: 101 sec1 = exe_module.GetSectionAtIndex(1) 102 print sec1 103 else: 104 sec1 = None 105 106 if sec1: 107 sec1.FindSubSection(None) 108 109 def module_compile_unit_iter(self): 110 exe = os.path.join(os.getcwd(), "a.out") 111 112 target = self.dbg.CreateTarget(exe) 113 self.assertTrue(target, VALID_TARGET) 114 self.assertTrue(target.GetNumModules() > 0) 115 116 # Hide stdout if not running with '-t' option. 117 if not self.TraceOn(): 118 self.HideStdout() 119 120 print "Number of modules for the target: %d" % target.GetNumModules() 121 for module in target.module_iter(): 122 print module 123 124 # Get the executable module at index 0. 125 exe_module = target.GetModuleAtIndex(0) 126 127 print "Exe module: %s" % str(exe_module) 128 print "Number of compile units: %d" % exe_module.GetNumCompileUnits() 129 INDENT = ' ' * 4 130 INDENT2 = INDENT * 2 131 for cu in exe_module.compile_unit_iter(): 132 print cu 133 134 135 if __name__ == '__main__': 136 import atexit 137 lldb.SBDebugger.Initialize() 138 atexit.register(lambda: lldb.SBDebugger.Terminate()) 139 unittest2.main() 140