Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test some SBModule and SBSection APIs. |
| 3 | """ |
| 4 | |
Zachary Turner | 77db4a8 | 2015-10-22 20:06:20 +0000 | [diff] [blame] | 5 | import lldb_shared |
| 6 | |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 7 | import os, time |
| 8 | import re |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 9 | import lldb |
| 10 | from lldbtest import * |
Johnny Chen | 1887fce | 2011-09-30 00:46:24 +0000 | [diff] [blame] | 11 | from lldbutil import symbol_type_to_str |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 12 | |
| 13 | class ModuleAndSectionAPIsTestCase(TestBase): |
| 14 | |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 15 | mydir = TestBase.compute_mydir(__file__) |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 16 | |
| 17 | @python_api_test |
| 18 | def test_module_and_section(self): |
| 19 | """Test module and section APIs.""" |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 20 | self.build() |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 21 | exe = os.path.join(os.getcwd(), "a.out") |
| 22 | |
| 23 | target = self.dbg.CreateTarget(exe) |
| 24 | self.assertTrue(target, VALID_TARGET) |
| 25 | self.assertTrue(target.GetNumModules() > 0) |
| 26 | |
| 27 | # Hide stdout if not running with '-t' option. |
| 28 | if not self.TraceOn(): |
| 29 | self.HideStdout() |
| 30 | |
| 31 | print "Number of modules for the target: %d" % target.GetNumModules() |
| 32 | for module in target.module_iter(): |
| 33 | print module |
| 34 | |
| 35 | # Get the executable module at index 0. |
| 36 | exe_module = target.GetModuleAtIndex(0) |
| 37 | |
Greg Clayton | 81e871e | 2012-02-04 02:27:34 +0000 | [diff] [blame] | 38 | print "Exe module: %s" % str(exe_module) |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 39 | print "Number of sections: %d" % exe_module.GetNumSections() |
| 40 | INDENT = ' ' * 4 |
Johnny Chen | a32a13d | 2011-09-28 00:51:00 +0000 | [diff] [blame] | 41 | INDENT2 = INDENT * 2 |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 42 | for sec in exe_module.section_iter(): |
| 43 | print sec |
Johnny Chen | c0f53df | 2011-09-28 18:33:50 +0000 | [diff] [blame] | 44 | print INDENT + "Number of subsections: %d" % sec.GetNumSubSections() |
| 45 | if sec.GetNumSubSections() == 0: |
Johnny Chen | c44e20c | 2011-09-30 00:42:49 +0000 | [diff] [blame] | 46 | for sym in exe_module.symbol_in_section_iter(sec): |
Greg Clayton | 81e871e | 2012-02-04 02:27:34 +0000 | [diff] [blame] | 47 | print INDENT + str(sym) |
Johnny Chen | c0f53df | 2011-09-28 18:33:50 +0000 | [diff] [blame] | 48 | print INDENT + "symbol type: %s" % symbol_type_to_str(sym.GetType()) |
| 49 | else: |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 50 | for subsec in sec: |
Greg Clayton | 81e871e | 2012-02-04 02:27:34 +0000 | [diff] [blame] | 51 | print INDENT + str(subsec) |
Johnny Chen | a32a13d | 2011-09-28 00:51:00 +0000 | [diff] [blame] | 52 | # Now print the symbols belonging to the subsection.... |
Johnny Chen | c44e20c | 2011-09-30 00:42:49 +0000 | [diff] [blame] | 53 | for sym in exe_module.symbol_in_section_iter(subsec): |
Greg Clayton | 81e871e | 2012-02-04 02:27:34 +0000 | [diff] [blame] | 54 | print INDENT2 + str(sym) |
Johnny Chen | a32a13d | 2011-09-28 00:51:00 +0000 | [diff] [blame] | 55 | print INDENT2 + "symbol type: %s" % symbol_type_to_str(sym.GetType()) |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 56 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 57 | @python_api_test |
| 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() |
Johnny Chen | 4efffd9 | 2011-12-19 20:16:22 +0000 | [diff] [blame] | 61 | exe = os.path.join(os.getcwd(), "a.out") |
| 62 | |
| 63 | target = self.dbg.CreateTarget(exe) |
| 64 | self.assertTrue(target, VALID_TARGET) |
| 65 | self.assertTrue(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 | |
Greg Clayton | 81e871e | 2012-02-04 02:27:34 +0000 | [diff] [blame] | 78 | print "Exe module: %s" % str(exe_module) |
Johnny Chen | 4efffd9 | 2011-12-19 20:16:22 +0000 | [diff] [blame] | 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) |
Greg Clayton | 5569e64 | 2012-02-06 01:44:54 +0000 | [diff] [blame] | 85 | exe_module.FindFunctions(None, 0) |
Johnny Chen | 4efffd9 | 2011-12-19 20:16:22 +0000 | [diff] [blame] | 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) |
Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 97 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 98 | @python_api_test |
| 99 | def test_module_compile_unit_iter(self): |
| 100 | """Test module's compile unit iterator APIs.""" |
| 101 | self.build() |
Johnny Chen | 1b72f09 | 2012-03-16 21:55:42 +0000 | [diff] [blame] | 102 | exe = os.path.join(os.getcwd(), "a.out") |
| 103 | |
| 104 | target = self.dbg.CreateTarget(exe) |
| 105 | self.assertTrue(target, VALID_TARGET) |
| 106 | self.assertTrue(target.GetNumModules() > 0) |
| 107 | |
| 108 | # Hide stdout if not running with '-t' option. |
| 109 | if not self.TraceOn(): |
| 110 | self.HideStdout() |
| 111 | |
| 112 | print "Number of modules for the target: %d" % target.GetNumModules() |
| 113 | for module in target.module_iter(): |
| 114 | print module |
| 115 | |
| 116 | # Get the executable module at index 0. |
| 117 | exe_module = target.GetModuleAtIndex(0) |
| 118 | |
| 119 | print "Exe module: %s" % str(exe_module) |
| 120 | print "Number of compile units: %d" % exe_module.GetNumCompileUnits() |
| 121 | INDENT = ' ' * 4 |
| 122 | INDENT2 = INDENT * 2 |
| 123 | for cu in exe_module.compile_unit_iter(): |
| 124 | print cu |