TestTypeList.py revision 166b89f089d6bec5bb9dd40470a4dc951ffc9daa
1"""
2Test SBType and SBTypeList API.
3"""
4
5import os, time
6import re
7import unittest2
8import lldb, lldbutil
9from lldbtest import *
10
11class TypeAndTypeListTestCase(TestBase):
12
13    mydir = os.path.join("python_api", "type")
14
15    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16    @python_api_test
17    @dsym_test
18    def test_with_dsym(self):
19        """Exercise SBType and SBTypeList API."""
20        d = {'EXE': self.exe_name}
21        self.buildDsym(dictionary=d)
22        self.setTearDownCleanup(dictionary=d)
23        self.type_and_typelist_api(self.exe_name)
24
25    @python_api_test
26    @dwarf_test
27    def test_with_dwarf(self):
28        """Exercise SBType and SBTypeList API."""
29        d = {'EXE': self.exe_name}
30        self.buildDwarf(dictionary=d)
31        self.setTearDownCleanup(dictionary=d)
32        self.type_and_typelist_api(self.exe_name)
33
34    def setUp(self):
35        # Call super's setUp().
36        TestBase.setUp(self)
37        # We'll use the test method name as the exe_name.
38        self.exe_name = self.testMethodName
39        # Find the line number to break at.
40        self.source = 'main.cpp'
41        self.line = line_number(self.source, '// Break at this line')
42
43    def type_and_typelist_api(self, exe_name):
44        """Exercise SBType and SBTypeList API."""
45        exe = os.path.join(os.getcwd(), exe_name)
46
47        # Create a target by the debugger.
48        target = self.dbg.CreateTarget(exe)
49        self.assertTrue(target, VALID_TARGET)
50
51        # Create the breakpoint inside function 'main'.
52        breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
53        self.assertTrue(breakpoint, VALID_BREAKPOINT)
54
55        # Now launch the process, and do not stop at entry point.
56        process = target.LaunchSimple(None, None, os.getcwd())
57        self.assertTrue(process, PROCESS_IS_VALID)
58
59        # Get Frame #0.
60        self.assertTrue(process.GetState() == lldb.eStateStopped)
61        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
62        self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition")
63        frame0 = thread.GetFrameAtIndex(0)
64
65        # Get the type 'Task'.
66        type_list = target.FindTypes('Task')
67        if self.TraceOn():
68            print "Size of type_list from target.FindTypes('Task') query: %d" % type_list.GetSize()
69        self.assertTrue(len(type_list) >= 1) # a second Task make be scared up by the Objective-C runtime
70        for type in type_list:
71            self.assertTrue(type)
72            self.DebugSBType(type)
73
74        # Pass an empty string.  LLDB should not crash. :-)
75        fuzz_types = target.FindTypes(None)
76        fuzz_type = target.FindFirstType(None)
77
78        # Now use the SBTarget.FindFirstType() API to find 'Task'.
79        task_type = target.FindFirstType('Task')
80        self.assertTrue(task_type)
81        self.DebugSBType(task_type)
82
83        # Get the reference type of 'Task', just for fun.
84        task_ref_type = task_type.GetReferenceType()
85        self.assertTrue(task_ref_type)
86        self.DebugSBType(task_ref_type)
87
88        # Get the pointer type of 'Task', which is the same as task_head's type.
89        task_pointer_type = task_type.GetPointerType()
90        self.assertTrue(task_pointer_type)
91        self.DebugSBType(task_pointer_type)
92
93        # Get variable 'task_head'.
94        task_head = frame0.FindVariable('task_head')
95        self.assertTrue(task_head, VALID_VARIABLE)
96        self.DebugSBValue(task_head)
97        task_head_type = task_head.GetType()
98        self.DebugSBType(task_head_type)
99        self.assertTrue(task_head_type.IsPointerType())
100
101        self.assertTrue(task_head_type == task_pointer_type)
102
103        # Get the pointee type of 'task_head'.
104        task_head_pointee_type = task_head_type.GetPointeeType()
105        self.DebugSBType(task_head_pointee_type)
106
107        self.assertTrue(task_type == task_head_pointee_type)
108
109        # We'll now get the child member 'id' from 'task_head'.
110        id = task_head.GetChildMemberWithName('id')
111        self.DebugSBValue(id)
112        id_type = id.GetType()
113        self.DebugSBType(id_type)
114
115        # SBType.GetBasicType() takes an enum 'BasicType' (lldb-enumerations.h).
116        int_type = id_type.GetBasicType(lldb.eBasicTypeInt)
117        self.assertTrue(id_type == int_type)
118
119if __name__ == '__main__':
120    import atexit
121    lldb.SBDebugger.Initialize()
122    atexit.register(lambda: lldb.SBDebugger.Terminate())
123    unittest2.main()
124