blob: 1ebe9d33433779622578ab88d39ff1616cabf0f2 [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
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 Chen4efffd92011-12-19 20:16:22 +000022 @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 Chenb2c78252011-09-27 23:15:58 +000028 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 Chena32a13d2011-09-28 00:51:00 +000049 INDENT2 = INDENT * 2
Johnny Chenb2c78252011-09-27 23:15:58 +000050 for sec in exe_module.section_iter():
51 print sec
Johnny Chenc0f53df2011-09-28 18:33:50 +000052 print INDENT + "Number of subsections: %d" % sec.GetNumSubSections()
53 if sec.GetNumSubSections() == 0:
Johnny Chenc44e20c2011-09-30 00:42:49 +000054 for sym in exe_module.symbol_in_section_iter(sec):
Johnny Chenc0f53df2011-09-28 18:33:50 +000055 print INDENT + repr(sym)
56 print INDENT + "symbol type: %s" % symbol_type_to_str(sym.GetType())
57 else:
Johnny Chenb2c78252011-09-27 23:15:58 +000058 for subsec in sec:
59 print INDENT + repr(subsec)
Johnny Chena32a13d2011-09-28 00:51:00 +000060 # Now print the symbols belonging to the subsection....
Johnny Chenc44e20c2011-09-30 00:42:49 +000061 for sym in exe_module.symbol_in_section_iter(subsec):
Johnny Chena32a13d2011-09-28 00:51:00 +000062 print INDENT2 + repr(sym)
63 print INDENT2 + "symbol type: %s" % symbol_type_to_str(sym.GetType())
Johnny Chenb2c78252011-09-27 23:15:58 +000064
Johnny Chen4efffd92011-12-19 20:16:22 +000065 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 Chenb2c78252011-09-27 23:15:58 +0000102
103if __name__ == '__main__':
104 import atexit
105 lldb.SBDebugger.Initialize()
106 atexit.register(lambda: lldb.SBDebugger.Terminate())
107 unittest2.main()