blob: 1ebe9d33433779622578ab88d39ff1616cabf0f2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  | 
"""
Test some SBModule and SBSection APIs.
"""
import os, time
import re
import unittest2
import lldb
from lldbtest import *
from lldbutil import symbol_type_to_str
class ModuleAndSectionAPIsTestCase(TestBase):
    mydir = os.path.join("python_api", "module_section")
    @python_api_test
    def test_module_and_section(self):
        """Test module and section APIs."""
        self.buildDefault()
        self.module_and_section()
    @python_api_test
    def test_module_and_section_boundary_condition(self):
        """Test module and section APIs by passing None when it expects a Python string."""
        self.buildDefault()
        self.module_and_section_boundary_condition()
    def module_and_section(self):
        exe = os.path.join(os.getcwd(), "a.out")
        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)
        self.assertTrue(target.GetNumModules() > 0)
        # Hide stdout if not running with '-t' option.
        if not self.TraceOn():
            self.HideStdout()
        print "Number of modules for the target: %d" % target.GetNumModules()
        for module in target.module_iter():
            print module
        # Get the executable module at index 0.
        exe_module = target.GetModuleAtIndex(0)
        print "Exe module: %s" % repr(exe_module)
        print "Number of sections: %d" % exe_module.GetNumSections()
        INDENT = ' ' * 4
        INDENT2 = INDENT * 2
        for sec in exe_module.section_iter():
            print sec
            print INDENT + "Number of subsections: %d" % sec.GetNumSubSections()
            if sec.GetNumSubSections() == 0:
                for sym in exe_module.symbol_in_section_iter(sec):
                    print INDENT + repr(sym)
                    print INDENT + "symbol type: %s" % symbol_type_to_str(sym.GetType())
            else:
                for subsec in sec:
                    print INDENT + repr(subsec)
                    # Now print the symbols belonging to the subsection....
                    for sym in exe_module.symbol_in_section_iter(subsec):
                        print INDENT2 + repr(sym)
                        print INDENT2 + "symbol type: %s" % symbol_type_to_str(sym.GetType())
    def module_and_section_boundary_condition(self):
        exe = os.path.join(os.getcwd(), "a.out")
        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)
        self.assertTrue(target.GetNumModules() > 0)
        # Hide stdout if not running with '-t' option.
        if not self.TraceOn():
            self.HideStdout()
        print "Number of modules for the target: %d" % target.GetNumModules()
        for module in target.module_iter():
            print module
        # Get the executable module at index 0.
        exe_module = target.GetModuleAtIndex(0)
        print "Exe module: %s" % repr(exe_module)
        print "Number of sections: %d" % exe_module.GetNumSections()
        # Boundary condition testings.  Should not crash lldb!
        exe_module.FindFirstType(None)
        exe_module.FindTypes(None)
        exe_module.FindGlobalVariables(target, None, 1)
        exe_module.FindFunctions(None, 0, True, lldb.SBSymbolContextList())
        exe_module.FindSection(None)
        # Get the section at index 1.
        if exe_module.GetNumSections() > 1:
            sec1 = exe_module.GetSectionAtIndex(1)
            print sec1
        else:
            sec1 = None
        if sec1:
            sec1.FindSubSection(None)
if __name__ == '__main__':
    import atexit
    lldb.SBDebugger.Initialize()
    atexit.register(lambda: lldb.SBDebugger.Terminate())
    unittest2.main()
  |