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 | |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 14 | mydir = TestBase.compute_mydir(__file__) |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 15 | |
| 16 | @python_api_test |
| 17 | def test_module_and_section(self): |
| 18 | """Test module and section APIs.""" |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame^] | 19 | self.build() |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 20 | exe = os.path.join(os.getcwd(), "a.out") |
| 21 | |
| 22 | target = self.dbg.CreateTarget(exe) |
| 23 | self.assertTrue(target, VALID_TARGET) |
| 24 | self.assertTrue(target.GetNumModules() > 0) |
| 25 | |
| 26 | # Hide stdout if not running with '-t' option. |
| 27 | if not self.TraceOn(): |
| 28 | self.HideStdout() |
| 29 | |
| 30 | print "Number of modules for the target: %d" % target.GetNumModules() |
| 31 | for module in target.module_iter(): |
| 32 | print module |
| 33 | |
| 34 | # Get the executable module at index 0. |
| 35 | exe_module = target.GetModuleAtIndex(0) |
| 36 | |
Greg Clayton | 81e871e | 2012-02-04 02:27:34 +0000 | [diff] [blame] | 37 | print "Exe module: %s" % str(exe_module) |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 38 | print "Number of sections: %d" % exe_module.GetNumSections() |
| 39 | INDENT = ' ' * 4 |
Johnny Chen | a32a13d | 2011-09-28 00:51:00 +0000 | [diff] [blame] | 40 | INDENT2 = INDENT * 2 |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 41 | for sec in exe_module.section_iter(): |
| 42 | print sec |
Johnny Chen | c0f53df | 2011-09-28 18:33:50 +0000 | [diff] [blame] | 43 | print INDENT + "Number of subsections: %d" % sec.GetNumSubSections() |
| 44 | if sec.GetNumSubSections() == 0: |
Johnny Chen | c44e20c | 2011-09-30 00:42:49 +0000 | [diff] [blame] | 45 | for sym in exe_module.symbol_in_section_iter(sec): |
Greg Clayton | 81e871e | 2012-02-04 02:27:34 +0000 | [diff] [blame] | 46 | print INDENT + str(sym) |
Johnny Chen | c0f53df | 2011-09-28 18:33:50 +0000 | [diff] [blame] | 47 | print INDENT + "symbol type: %s" % symbol_type_to_str(sym.GetType()) |
| 48 | else: |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 49 | for subsec in sec: |
Greg Clayton | 81e871e | 2012-02-04 02:27:34 +0000 | [diff] [blame] | 50 | print INDENT + str(subsec) |
Johnny Chen | a32a13d | 2011-09-28 00:51:00 +0000 | [diff] [blame] | 51 | # Now print the symbols belonging to the subsection.... |
Johnny Chen | c44e20c | 2011-09-30 00:42:49 +0000 | [diff] [blame] | 52 | for sym in exe_module.symbol_in_section_iter(subsec): |
Greg Clayton | 81e871e | 2012-02-04 02:27:34 +0000 | [diff] [blame] | 53 | print INDENT2 + str(sym) |
Johnny Chen | a32a13d | 2011-09-28 00:51:00 +0000 | [diff] [blame] | 54 | print INDENT2 + "symbol type: %s" % symbol_type_to_str(sym.GetType()) |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 55 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame^] | 56 | @python_api_test |
| 57 | def test_module_and_section_boundary_condition(self): |
| 58 | """Test module and section APIs by passing None when it expects a Python string.""" |
| 59 | self.build() |
Johnny Chen | 4efffd9 | 2011-12-19 20:16:22 +0000 | [diff] [blame] | 60 | exe = os.path.join(os.getcwd(), "a.out") |
| 61 | |
| 62 | target = self.dbg.CreateTarget(exe) |
| 63 | self.assertTrue(target, VALID_TARGET) |
| 64 | self.assertTrue(target.GetNumModules() > 0) |
| 65 | |
| 66 | # Hide stdout if not running with '-t' option. |
| 67 | if not self.TraceOn(): |
| 68 | self.HideStdout() |
| 69 | |
| 70 | print "Number of modules for the target: %d" % target.GetNumModules() |
| 71 | for module in target.module_iter(): |
| 72 | print module |
| 73 | |
| 74 | # Get the executable module at index 0. |
| 75 | exe_module = target.GetModuleAtIndex(0) |
| 76 | |
Greg Clayton | 81e871e | 2012-02-04 02:27:34 +0000 | [diff] [blame] | 77 | print "Exe module: %s" % str(exe_module) |
Johnny Chen | 4efffd9 | 2011-12-19 20:16:22 +0000 | [diff] [blame] | 78 | print "Number of sections: %d" % exe_module.GetNumSections() |
| 79 | |
| 80 | # Boundary condition testings. Should not crash lldb! |
| 81 | exe_module.FindFirstType(None) |
| 82 | exe_module.FindTypes(None) |
| 83 | exe_module.FindGlobalVariables(target, None, 1) |
Greg Clayton | 5569e64 | 2012-02-06 01:44:54 +0000 | [diff] [blame] | 84 | exe_module.FindFunctions(None, 0) |
Johnny Chen | 4efffd9 | 2011-12-19 20:16:22 +0000 | [diff] [blame] | 85 | exe_module.FindSection(None) |
| 86 | |
| 87 | # Get the section at index 1. |
| 88 | if exe_module.GetNumSections() > 1: |
| 89 | sec1 = exe_module.GetSectionAtIndex(1) |
| 90 | print sec1 |
| 91 | else: |
| 92 | sec1 = None |
| 93 | |
| 94 | if sec1: |
| 95 | sec1.FindSubSection(None) |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 96 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame^] | 97 | @python_api_test |
| 98 | def test_module_compile_unit_iter(self): |
| 99 | """Test module's compile unit iterator APIs.""" |
| 100 | self.build() |
Johnny Chen | 1b72f09 | 2012-03-16 21:55:42 +0000 | [diff] [blame] | 101 | exe = os.path.join(os.getcwd(), "a.out") |
| 102 | |
| 103 | target = self.dbg.CreateTarget(exe) |
| 104 | self.assertTrue(target, VALID_TARGET) |
| 105 | self.assertTrue(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 | |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 125 | if __name__ == '__main__': |
| 126 | import atexit |
| 127 | lldb.SBDebugger.Initialize() |
| 128 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 129 | unittest2.main() |