blob: 78e4b229bf985fe4fca3378d0bf515372d5bdac5 [file] [log] [blame]
Johnny Chenb2c78252011-09-27 23:15:58 +00001"""
2Test some SBModule and SBSection APIs.
3"""
4
5import os, time
6import re
7import unittest2
8import lldb
9from lldbtest import *
Johnny Chen1887fce2011-09-30 00:46:24 +000010from lldbutil import symbol_type_to_str
Johnny Chenb2c78252011-09-27 23:15:58 +000011
12class ModuleAndSectionAPIsTestCase(TestBase):
13
Greg Clayton4570d3e2013-12-10 23:19:29 +000014 mydir = TestBase.compute_mydir(__file__)
Johnny Chenb2c78252011-09-27 23:15:58 +000015
16 @python_api_test
17 def test_module_and_section(self):
18 """Test module and section APIs."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000019 self.build()
Johnny Chenb2c78252011-09-27 23:15:58 +000020 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 Clayton81e871e2012-02-04 02:27:34 +000037 print "Exe module: %s" % str(exe_module)
Johnny Chenb2c78252011-09-27 23:15:58 +000038 print "Number of sections: %d" % exe_module.GetNumSections()
39 INDENT = ' ' * 4
Johnny Chena32a13d2011-09-28 00:51:00 +000040 INDENT2 = INDENT * 2
Johnny Chenb2c78252011-09-27 23:15:58 +000041 for sec in exe_module.section_iter():
42 print sec
Johnny Chenc0f53df2011-09-28 18:33:50 +000043 print INDENT + "Number of subsections: %d" % sec.GetNumSubSections()
44 if sec.GetNumSubSections() == 0:
Johnny Chenc44e20c2011-09-30 00:42:49 +000045 for sym in exe_module.symbol_in_section_iter(sec):
Greg Clayton81e871e2012-02-04 02:27:34 +000046 print INDENT + str(sym)
Johnny Chenc0f53df2011-09-28 18:33:50 +000047 print INDENT + "symbol type: %s" % symbol_type_to_str(sym.GetType())
48 else:
Johnny Chenb2c78252011-09-27 23:15:58 +000049 for subsec in sec:
Greg Clayton81e871e2012-02-04 02:27:34 +000050 print INDENT + str(subsec)
Johnny Chena32a13d2011-09-28 00:51:00 +000051 # Now print the symbols belonging to the subsection....
Johnny Chenc44e20c2011-09-30 00:42:49 +000052 for sym in exe_module.symbol_in_section_iter(subsec):
Greg Clayton81e871e2012-02-04 02:27:34 +000053 print INDENT2 + str(sym)
Johnny Chena32a13d2011-09-28 00:51:00 +000054 print INDENT2 + "symbol type: %s" % symbol_type_to_str(sym.GetType())
Johnny Chenb2c78252011-09-27 23:15:58 +000055
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000056 @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 Chen4efffd92011-12-19 20:16:22 +000060 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 Clayton81e871e2012-02-04 02:27:34 +000077 print "Exe module: %s" % str(exe_module)
Johnny Chen4efffd92011-12-19 20:16:22 +000078 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 Clayton5569e642012-02-06 01:44:54 +000084 exe_module.FindFunctions(None, 0)
Johnny Chen4efffd92011-12-19 20:16:22 +000085 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 Chenb2c78252011-09-27 23:15:58 +000096
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000097 @python_api_test
98 def test_module_compile_unit_iter(self):
99 """Test module's compile unit iterator APIs."""
100 self.build()
Johnny Chen1b72f092012-03-16 21:55:42 +0000101 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 Chenb2c78252011-09-27 23:15:58 +0000125if __name__ == '__main__':
126 import atexit
127 lldb.SBDebugger.Initialize()
128 atexit.register(lambda: lldb.SBDebugger.Terminate())
129 unittest2.main()