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