Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 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 * |
Johnny Chen | 1887fce | 2011-09-30 00:46:24 +0000 | [diff] [blame] | 10 | from lldbutil import symbol_type_to_str |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 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 | |
Johnny Chen | 4efffd9 | 2011-12-19 20:16:22 +0000 | [diff] [blame^] | 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 | |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 28 | def module_and_section(self): |
| 29 | exe = os.path.join(os.getcwd(), "a.out") |
| 30 | |
| 31 | target = self.dbg.CreateTarget(exe) |
| 32 | self.assertTrue(target, VALID_TARGET) |
| 33 | self.assertTrue(target.GetNumModules() > 0) |
| 34 | |
| 35 | # Hide stdout if not running with '-t' option. |
| 36 | if not self.TraceOn(): |
| 37 | self.HideStdout() |
| 38 | |
| 39 | print "Number of modules for the target: %d" % target.GetNumModules() |
| 40 | for module in target.module_iter(): |
| 41 | print module |
| 42 | |
| 43 | # Get the executable module at index 0. |
| 44 | exe_module = target.GetModuleAtIndex(0) |
| 45 | |
| 46 | print "Exe module: %s" % repr(exe_module) |
| 47 | print "Number of sections: %d" % exe_module.GetNumSections() |
| 48 | INDENT = ' ' * 4 |
Johnny Chen | a32a13d | 2011-09-28 00:51:00 +0000 | [diff] [blame] | 49 | INDENT2 = INDENT * 2 |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 50 | for sec in exe_module.section_iter(): |
| 51 | print sec |
Johnny Chen | c0f53df | 2011-09-28 18:33:50 +0000 | [diff] [blame] | 52 | print INDENT + "Number of subsections: %d" % sec.GetNumSubSections() |
| 53 | if sec.GetNumSubSections() == 0: |
Johnny Chen | c44e20c | 2011-09-30 00:42:49 +0000 | [diff] [blame] | 54 | for sym in exe_module.symbol_in_section_iter(sec): |
Johnny Chen | c0f53df | 2011-09-28 18:33:50 +0000 | [diff] [blame] | 55 | print INDENT + repr(sym) |
| 56 | print INDENT + "symbol type: %s" % symbol_type_to_str(sym.GetType()) |
| 57 | else: |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 58 | for subsec in sec: |
| 59 | print INDENT + repr(subsec) |
Johnny Chen | a32a13d | 2011-09-28 00:51:00 +0000 | [diff] [blame] | 60 | # Now print the symbols belonging to the subsection.... |
Johnny Chen | c44e20c | 2011-09-30 00:42:49 +0000 | [diff] [blame] | 61 | for sym in exe_module.symbol_in_section_iter(subsec): |
Johnny Chen | a32a13d | 2011-09-28 00:51:00 +0000 | [diff] [blame] | 62 | print INDENT2 + repr(sym) |
| 63 | print INDENT2 + "symbol type: %s" % symbol_type_to_str(sym.GetType()) |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 64 | |
Johnny Chen | 4efffd9 | 2011-12-19 20:16:22 +0000 | [diff] [blame^] | 65 | def module_and_section_boundary_condition(self): |
| 66 | exe = os.path.join(os.getcwd(), "a.out") |
| 67 | |
| 68 | target = self.dbg.CreateTarget(exe) |
| 69 | self.assertTrue(target, VALID_TARGET) |
| 70 | self.assertTrue(target.GetNumModules() > 0) |
| 71 | |
| 72 | # Hide stdout if not running with '-t' option. |
| 73 | if not self.TraceOn(): |
| 74 | self.HideStdout() |
| 75 | |
| 76 | print "Number of modules for the target: %d" % target.GetNumModules() |
| 77 | for module in target.module_iter(): |
| 78 | print module |
| 79 | |
| 80 | # Get the executable module at index 0. |
| 81 | exe_module = target.GetModuleAtIndex(0) |
| 82 | |
| 83 | print "Exe module: %s" % repr(exe_module) |
| 84 | print "Number of sections: %d" % exe_module.GetNumSections() |
| 85 | |
| 86 | # Boundary condition testings. Should not crash lldb! |
| 87 | exe_module.FindFirstType(None) |
| 88 | exe_module.FindTypes(None) |
| 89 | exe_module.FindGlobalVariables(target, None, 1) |
| 90 | exe_module.FindFunctions(None, 0, True, lldb.SBSymbolContextList()) |
| 91 | exe_module.FindSection(None) |
| 92 | |
| 93 | # Get the section at index 1. |
| 94 | if exe_module.GetNumSections() > 1: |
| 95 | sec1 = exe_module.GetSectionAtIndex(1) |
| 96 | print sec1 |
| 97 | else: |
| 98 | sec1 = None |
| 99 | |
| 100 | if sec1: |
| 101 | sec1.FindSubSection(None) |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 102 | |
| 103 | if __name__ == '__main__': |
| 104 | import atexit |
| 105 | lldb.SBDebugger.Initialize() |
| 106 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 107 | unittest2.main() |