blob: d1a69f934d63d298798c9158a59a97eccddc88b6 [file] [log] [blame]
Johnny Chenb2c78252011-09-27 23:15:58 +00001"""
2Test some SBModule and SBSection APIs.
3"""
4
Zachary Turner77db4a82015-10-22 20:06:20 +00005import lldb_shared
6
Johnny Chenb2c78252011-09-27 23:15:58 +00007import os, time
8import re
Johnny Chenb2c78252011-09-27 23:15:58 +00009import lldb
10from lldbtest import *
Johnny Chen1887fce2011-09-30 00:46:24 +000011from lldbutil import symbol_type_to_str
Johnny Chenb2c78252011-09-27 23:15:58 +000012
13class ModuleAndSectionAPIsTestCase(TestBase):
14
Greg Clayton4570d3e2013-12-10 23:19:29 +000015 mydir = TestBase.compute_mydir(__file__)
Johnny Chenb2c78252011-09-27 23:15:58 +000016
17 @python_api_test
18 def test_module_and_section(self):
19 """Test module and section APIs."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000020 self.build()
Johnny Chenb2c78252011-09-27 23:15:58 +000021 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 Clayton81e871e2012-02-04 02:27:34 +000038 print "Exe module: %s" % str(exe_module)
Johnny Chenb2c78252011-09-27 23:15:58 +000039 print "Number of sections: %d" % exe_module.GetNumSections()
40 INDENT = ' ' * 4
Johnny Chena32a13d2011-09-28 00:51:00 +000041 INDENT2 = INDENT * 2
Johnny Chenb2c78252011-09-27 23:15:58 +000042 for sec in exe_module.section_iter():
43 print sec
Johnny Chenc0f53df2011-09-28 18:33:50 +000044 print INDENT + "Number of subsections: %d" % sec.GetNumSubSections()
45 if sec.GetNumSubSections() == 0:
Johnny Chenc44e20c2011-09-30 00:42:49 +000046 for sym in exe_module.symbol_in_section_iter(sec):
Greg Clayton81e871e2012-02-04 02:27:34 +000047 print INDENT + str(sym)
Johnny Chenc0f53df2011-09-28 18:33:50 +000048 print INDENT + "symbol type: %s" % symbol_type_to_str(sym.GetType())
49 else:
Johnny Chenb2c78252011-09-27 23:15:58 +000050 for subsec in sec:
Greg Clayton81e871e2012-02-04 02:27:34 +000051 print INDENT + str(subsec)
Johnny Chena32a13d2011-09-28 00:51:00 +000052 # Now print the symbols belonging to the subsection....
Johnny Chenc44e20c2011-09-30 00:42:49 +000053 for sym in exe_module.symbol_in_section_iter(subsec):
Greg Clayton81e871e2012-02-04 02:27:34 +000054 print INDENT2 + str(sym)
Johnny Chena32a13d2011-09-28 00:51:00 +000055 print INDENT2 + "symbol type: %s" % symbol_type_to_str(sym.GetType())
Johnny Chenb2c78252011-09-27 23:15:58 +000056
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000057 @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 Chen4efffd92011-12-19 20:16:22 +000061 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 Clayton81e871e2012-02-04 02:27:34 +000078 print "Exe module: %s" % str(exe_module)
Johnny Chen4efffd92011-12-19 20:16:22 +000079 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 Clayton5569e642012-02-06 01:44:54 +000085 exe_module.FindFunctions(None, 0)
Johnny Chen4efffd92011-12-19 20:16:22 +000086 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 Chenb2c78252011-09-27 23:15:58 +000097
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000098 @python_api_test
99 def test_module_compile_unit_iter(self):
100 """Test module's compile unit iterator APIs."""
101 self.build()
Johnny Chen1b72f092012-03-16 21:55:42 +0000102 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