1"""
2Test the iteration protocol for some lldb container objects.
3"""
4
5import os, time
6import re
7import unittest2
8import lldb
9from lldbtest import *
10
11class LLDBIteratorTestCase(TestBase):
12
13    mydir = os.path.join("python_api", "lldbutil", "iter")
14
15    def setUp(self):
16        # Call super's setUp().
17        TestBase.setUp(self)
18        # Find the line numbers to break inside main().
19        self.line1 = line_number('main.cpp', '// Set break point at this line.')
20        self.line2 = line_number('main.cpp', '// And that line.')
21
22    @python_api_test
23    def test_lldb_iter_module(self):
24        """Test module_iter works correctly for SBTarget -> SBModule."""
25        self.buildDefault()
26        self.lldb_iter_module()
27
28    @python_api_test
29    def test_lldb_iter_breakpoint(self):
30        """Test breakpoint_iter works correctly for SBTarget -> SBBreakpoint."""
31        self.buildDefault()
32        self.lldb_iter_breakpoint()
33
34    @python_api_test
35    def test_lldb_iter_frame(self):
36        """Test iterator works correctly for SBProcess->SBThread->SBFrame."""
37        self.buildDefault()
38        self.lldb_iter_frame()
39
40    def lldb_iter_module(self):
41        exe = os.path.join(os.getcwd(), "a.out")
42
43        target = self.dbg.CreateTarget(exe)
44        self.assertTrue(target, VALID_TARGET)
45
46        breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line1)
47        self.assertTrue(breakpoint, VALID_BREAKPOINT)
48
49        # Now launch the process, and do not stop at entry point.
50        process = target.LaunchSimple(None, None, os.getcwd())
51
52        if not process:
53            self.fail("SBTarget.LaunchProcess() failed")
54
55        from lldbutil import get_description
56        yours = []
57        for i in range(target.GetNumModules()):
58            yours.append(target.GetModuleAtIndex(i))
59        mine = []
60        for m in target.module_iter():
61            mine.append(m)
62
63        self.assertTrue(len(yours) == len(mine))
64        for i in range(len(yours)):
65            if self.TraceOn():
66                print "yours[%d]='%s'" % (i, get_description(yours[i]))
67                print "mine[%d]='%s'" % (i, get_description(mine[i]))
68            self.assertTrue(yours[i] == mine[i],
69                            "UUID+FileSpec of yours[{0}] and mine[{0}] matches".format(i))
70
71    def lldb_iter_breakpoint(self):
72        exe = os.path.join(os.getcwd(), "a.out")
73
74        target = self.dbg.CreateTarget(exe)
75        self.assertTrue(target, VALID_TARGET)
76
77        breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line1)
78        self.assertTrue(breakpoint, VALID_BREAKPOINT)
79        breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line2)
80        self.assertTrue(breakpoint, VALID_BREAKPOINT)
81
82        self.assertTrue(target.GetNumBreakpoints() == 2)
83
84        from lldbutil import get_description
85        yours = []
86        for i in range(target.GetNumBreakpoints()):
87            yours.append(target.GetBreakpointAtIndex(i))
88        mine = []
89        for b in target.breakpoint_iter():
90            mine.append(b)
91
92        self.assertTrue(len(yours) == len(mine))
93        for i in range(len(yours)):
94            if self.TraceOn():
95                print "yours[%d]='%s'" % (i, get_description(yours[i]))
96                print "mine[%d]='%s'" % (i, get_description(mine[i]))
97            self.assertTrue(yours[i] == mine[i],
98                            "ID of yours[{0}] and mine[{0}] matches".format(i))
99
100    def lldb_iter_frame(self):
101        exe = os.path.join(os.getcwd(), "a.out")
102
103        target = self.dbg.CreateTarget(exe)
104        self.assertTrue(target, VALID_TARGET)
105
106        breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line1)
107        self.assertTrue(breakpoint, VALID_BREAKPOINT)
108
109        # Now launch the process, and do not stop at entry point.
110        process = target.LaunchSimple(None, None, os.getcwd())
111
112        if not process:
113            self.fail("SBTarget.LaunchProcess() failed")
114
115        from lldbutil import print_stacktrace
116        stopped_due_to_breakpoint = False
117        for thread in process:
118            if self.TraceOn():
119                print_stacktrace(thread)
120            ID = thread.GetThreadID()
121            if thread.GetStopReason() == lldb.eStopReasonBreakpoint:
122                stopped_due_to_breakpoint = True
123            for frame in thread:
124                self.assertTrue(frame.GetThread().GetThreadID() == ID)
125                if self.TraceOn():
126                    print frame
127
128        self.assertTrue(stopped_due_to_breakpoint)
129
130if __name__ == '__main__':
131    import atexit
132    lldb.SBDebugger.Initialize()
133    atexit.register(lambda: lldb.SBDebugger.Terminate())
134    unittest2.main()
135