]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/macosx/queues/TestQueues.py
Vendor import of lldb trunk r256945:
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / macosx / queues / TestQueues.py
1 """Test queues inspection SB APIs."""
2
3 from __future__ import print_function
4
5
6
7 import unittest2
8 import os, time
9 import lldb
10 import lldbsuite.test.lldbutil as lldbutil
11 from lldbsuite.test.lldbtest import *
12
13 class TestQueues(TestBase):
14
15     mydir = TestBase.compute_mydir(__file__)
16
17     @skipUnlessDarwin
18     @add_test_categories(['pyapi'])      
19     @unittest2.expectedFailure("rdar://22531180")
20     def test_with_python_api(self):
21         """Test queues inspection SB APIs."""
22         self.build()
23         self.queues()
24         self.queues_with_libBacktraceRecording()
25
26     def setUp(self):
27         # Call super's setUp().
28         TestBase.setUp(self)
29         # Find the line numbers that we will step to in main:
30         self.main_source = "main.c"
31
32     def check_queue_for_valid_queue_id(self, queue):
33         self.assertTrue(queue.GetQueueID() != 0, "Check queue %s for valid QueueID (got 0x%x)" % (queue.GetName(), queue.GetQueueID()))
34
35     def check_running_and_pending_items_on_queue(self, queue, expected_running, expected_pending):
36         self.assertTrue(queue.GetNumPendingItems() == expected_pending, "queue %s should have %d pending items, instead has %d pending items" % (queue.GetName(), expected_pending, (queue.GetNumPendingItems())))
37         self.assertTrue(queue.GetNumRunningItems() == expected_running, "queue %s should have %d running items, instead has %d running items" % (queue.GetName(), expected_running, (queue.GetNumRunningItems())))
38
39     def check_number_of_threads_owned_by_queue(self, queue, number_threads):
40         self.assertTrue(queue.GetNumThreads() == number_threads, "queue %s should have %d thread executing, but has %d" % (queue.GetName(), number_threads, queue.GetNumThreads()))
41
42     def check_queue_kind (self, queue, kind):
43         expected_kind_string = "Unknown"
44         if kind == lldb.eQueueKindSerial:
45             expected_kind_string = "Serial queue"
46         if kind == lldb.eQueueKindConcurrent:
47             expected_kind_string = "Concurrent queue"
48         actual_kind_string = "Unknown"
49         if queue.GetKind() == lldb.eQueueKindSerial:
50             actual_kind_string = "Serial queue"
51         if queue.GetKind() == lldb.eQueueKindConcurrent:
52             actual_kind_string = "Concurrent queue"
53         self.assertTrue(queue.GetKind() == kind, "queue %s is expected to be a %s but it is actually a %s" % (queue.GetName(), expected_kind_string, actual_kind_string))
54
55     def check_queues_threads_match_queue(self, queue):
56         for idx in range(0, queue.GetNumThreads()):
57             t = queue.GetThreadAtIndex(idx)
58             self.assertTrue(t.IsValid(), "Queue %s's thread #%d must be valid" % (queue.GetName(), idx))
59             self.assertTrue(t.GetQueueID() == queue.GetQueueID(), "Queue %s has a QueueID of %d but its thread #%d has a QueueID of %d" % (queue.GetName(), queue.GetQueueID(), idx, t.GetQueueID()))
60             self.assertTrue(t.GetQueueName() == queue.GetName(), "Queue %s has a QueueName of %s but its thread #%d has a QueueName of %s" % (queue.GetName(), queue.GetName(), idx, t.GetQueueName()))
61             self.assertTrue(t.GetQueue().GetQueueID() == queue.GetQueueID(), "Thread #%d's Queue's QueueID of %d is not the same as the QueueID of its owning queue %d" % (idx, t.GetQueue().GetQueueID(), queue.GetQueueID()))
62
63     def queues(self):
64         """Test queues inspection SB APIs without libBacktraceRecording."""
65         exe = os.path.join(os.getcwd(), "a.out")
66
67         target = self.dbg.CreateTarget(exe)
68         self.assertTrue(target, VALID_TARGET)
69         self.main_source_spec = lldb.SBFileSpec (self.main_source)
70         break1 = target.BreakpointCreateByName ("stopper", 'a.out')
71         self.assertTrue(break1, VALID_BREAKPOINT)
72         process = target.LaunchSimple (None, None, self.get_process_working_directory())
73         self.assertTrue(process, PROCESS_IS_VALID)
74         threads = lldbutil.get_threads_stopped_at_breakpoint (process, break1)
75         if len(threads) != 1:
76             self.fail ("Failed to stop at breakpoint 1.")
77
78         queue_submittor_1 = lldb.SBQueue()
79         queue_performer_1 = lldb.SBQueue()
80         queue_performer_2 = lldb.SBQueue()
81         queue_performer_3 = lldb.SBQueue()
82         for idx in range (0, process.GetNumQueues()):
83           q = process.GetQueueAtIndex(idx)
84           if q.GetName() == "com.apple.work_submittor_1":
85             queue_submittor_1 = q
86           if q.GetName() == "com.apple.work_performer_1":
87             queue_performer_1 = q
88           if q.GetName() == "com.apple.work_performer_2":
89             queue_performer_2 = q
90           if q.GetName() == "com.apple.work_performer_3":
91             queue_performer_3 = q
92
93         self.assertTrue(queue_submittor_1.IsValid() and queue_performer_1.IsValid() and queue_performer_2.IsValid() and queue_performer_3.IsValid(), "Got all four expected queues: %s %s %s %s" % (queue_submittor_1.IsValid(), queue_performer_1.IsValid(), queue_performer_2.IsValid(), queue_performer_3.IsValid()))
94
95         self.check_queue_for_valid_queue_id (queue_submittor_1)
96         self.check_queue_for_valid_queue_id (queue_performer_1)
97         self.check_queue_for_valid_queue_id (queue_performer_2)
98         self.check_queue_for_valid_queue_id (queue_performer_3)
99
100         self.check_number_of_threads_owned_by_queue (queue_submittor_1, 1)
101         self.check_number_of_threads_owned_by_queue (queue_performer_1, 1)
102         self.check_number_of_threads_owned_by_queue (queue_performer_2, 1)
103         self.check_number_of_threads_owned_by_queue (queue_performer_3, 4)
104
105         self.check_queue_kind (queue_submittor_1, lldb.eQueueKindSerial)
106         self.check_queue_kind (queue_performer_1, lldb.eQueueKindSerial)
107         self.check_queue_kind (queue_performer_2, lldb.eQueueKindSerial)
108         self.check_queue_kind (queue_performer_3, lldb.eQueueKindConcurrent)
109         
110         self.check_queues_threads_match_queue (queue_submittor_1)
111         self.check_queues_threads_match_queue (queue_performer_1)
112         self.check_queues_threads_match_queue (queue_performer_2)
113         self.check_queues_threads_match_queue (queue_performer_3)
114
115
116
117         # We have threads running with all the different dispatch QoS service
118         # levels - find those threads and check that we can get the correct
119         # QoS name for each of them.
120
121         user_initiated_thread = lldb.SBThread()
122         user_interactive_thread = lldb.SBThread()
123         utility_thread = lldb.SBThread()
124         unspecified_thread = lldb.SBThread()
125         background_thread = lldb.SBThread()
126         for th in process.threads:
127             if th.GetName() == "user initiated QoS":
128                 user_initiated_thread = th
129             if th.GetName() == "user interactive QoS":
130                 user_interactive_thread = th
131             if th.GetName() == "utility QoS":
132                 utility_thread = th
133             if th.GetName() == "unspecified QoS":
134                 unspecified_thread = th
135             if th.GetName() == "background QoS":
136                 background_thread = th
137
138         self.assertTrue(user_initiated_thread.IsValid(), "Found user initiated QoS thread")
139         self.assertTrue(user_interactive_thread.IsValid(), "Found user interactive QoS thread")
140         self.assertTrue(utility_thread.IsValid(), "Found utility QoS thread")
141         self.assertTrue(unspecified_thread.IsValid(), "Found unspecified QoS thread")
142         self.assertTrue(background_thread.IsValid(), "Found background QoS thread")
143
144         stream = lldb.SBStream()
145         self.assertTrue(user_initiated_thread.GetInfoItemByPathAsString("requested_qos.printable_name", stream), "Get QoS printable string for user initiated QoS thread")
146         self.assertTrue(stream.GetData() == "User Initiated", "user initiated QoS thread name is valid")
147         stream.Clear()
148         self.assertTrue(user_interactive_thread.GetInfoItemByPathAsString("requested_qos.printable_name", stream), "Get QoS printable string for user interactive QoS thread")
149         self.assertTrue(stream.GetData() == "User Interactive", "user interactive QoS thread name is valid")
150         stream.Clear()
151         self.assertTrue(utility_thread.GetInfoItemByPathAsString("requested_qos.printable_name", stream), "Get QoS printable string for utility QoS thread")
152         self.assertTrue(stream.GetData() == "Utility", "utility QoS thread name is valid")
153         stream.Clear()
154         self.assertTrue(unspecified_thread.GetInfoItemByPathAsString("requested_qos.printable_name", stream), "Get QoS printable string for unspecified QoS thread")
155         self.assertTrue(stream.GetData() == "User Initiated", "unspecified QoS thread name is valid")
156         stream.Clear()
157         self.assertTrue(background_thread.GetInfoItemByPathAsString("requested_qos.printable_name", stream), "Get QoS printable string for background QoS thread")
158         self.assertTrue(stream.GetData() == "Background", "background QoS thread name is valid")
159
160     def queues_with_libBacktraceRecording(self):
161         """Test queues inspection SB APIs with libBacktraceRecording present."""
162         exe = os.path.join(os.getcwd(), "a.out")
163
164         if not os.path.isfile('/Applications/Xcode.app/Contents/Developer/usr/lib/libBacktraceRecording.dylib'):
165           self.skipTest ("Skipped because libBacktraceRecording.dylib was present on the system.")
166           
167         if not os.path.isfile('/usr/lib/system/introspection/libdispatch.dylib'):
168           self.skipTest ("Skipped because introspection libdispatch dylib is not present.")
169           
170         target = self.dbg.CreateTarget(exe)
171         self.assertTrue(target, VALID_TARGET)
172
173         self.main_source_spec = lldb.SBFileSpec (self.main_source)
174
175         break1 = target.BreakpointCreateByName ("stopper", 'a.out')
176         self.assertTrue(break1, VALID_BREAKPOINT)
177
178         # Now launch the process, and do not stop at entry point.
179         process = target.LaunchSimple (None, ['DYLD_INSERT_LIBRARIES=/Applications/Xcode.app/Contents/Developer/usr/lib/libBacktraceRecording.dylib', 'DYLD_LIBRARY_PATH=/usr/lib/system/introspection'], self.get_process_working_directory())
180
181         self.assertTrue(process, PROCESS_IS_VALID)
182
183         # The stop reason of the thread should be breakpoint.
184         threads = lldbutil.get_threads_stopped_at_breakpoint (process, break1)
185         if len(threads) != 1:
186             self.fail ("Failed to stop at breakpoint 1.")
187
188         libbtr_module_filespec = lldb.SBFileSpec("libBacktraceRecording.dylib")
189         libbtr_module = target.FindModule (libbtr_module_filespec)
190         if not libbtr_module.IsValid():
191           self.skipTest ("Skipped because libBacktraceRecording.dylib was not loaded into the process.")
192
193         self.assertTrue(process.GetNumQueues() >= 4, "Found the correct number of queues.")
194
195         queue_submittor_1 = lldb.SBQueue()
196         queue_performer_1 = lldb.SBQueue()
197         queue_performer_2 = lldb.SBQueue()
198         queue_performer_3 = lldb.SBQueue()
199         for idx in range (0, process.GetNumQueues()):
200           q = process.GetQueueAtIndex(idx)
201           if q.GetName() == "com.apple.work_submittor_1":
202             queue_submittor_1 = q
203           if q.GetName() == "com.apple.work_performer_1":
204             queue_performer_1 = q
205           if q.GetName() == "com.apple.work_performer_2":
206             queue_performer_2 = q
207           if q.GetName() == "com.apple.work_performer_3":
208             queue_performer_3 = q
209
210         self.assertTrue(queue_submittor_1.IsValid() and queue_performer_1.IsValid() and queue_performer_2.IsValid() and queue_performer_3.IsValid(), "Got all four expected queues: %s %s %s %s" % (queue_submittor_1.IsValid(), queue_performer_1.IsValid(), queue_performer_2.IsValid(), queue_performer_3.IsValid()))
211
212         self.check_queue_for_valid_queue_id (queue_submittor_1)
213         self.check_queue_for_valid_queue_id (queue_performer_1)
214         self.check_queue_for_valid_queue_id (queue_performer_2)
215         self.check_queue_for_valid_queue_id (queue_performer_3)
216
217         self.check_running_and_pending_items_on_queue (queue_submittor_1, 1, 0)
218         self.check_running_and_pending_items_on_queue (queue_performer_1, 1, 3)
219         self.check_running_and_pending_items_on_queue (queue_performer_2, 1, 9999)
220         self.check_running_and_pending_items_on_queue (queue_performer_3, 4, 0)
221        
222         self.check_number_of_threads_owned_by_queue (queue_submittor_1, 1)
223         self.check_number_of_threads_owned_by_queue (queue_performer_1, 1)
224         self.check_number_of_threads_owned_by_queue (queue_performer_2, 1)
225         self.check_number_of_threads_owned_by_queue (queue_performer_3, 4)
226
227         self.check_queue_kind (queue_submittor_1, lldb.eQueueKindSerial)
228         self.check_queue_kind (queue_performer_1, lldb.eQueueKindSerial)
229         self.check_queue_kind (queue_performer_2, lldb.eQueueKindSerial)
230         self.check_queue_kind (queue_performer_3, lldb.eQueueKindConcurrent)
231         
232
233         self.check_queues_threads_match_queue (queue_submittor_1)
234         self.check_queues_threads_match_queue (queue_performer_1)
235         self.check_queues_threads_match_queue (queue_performer_2)
236         self.check_queues_threads_match_queue (queue_performer_3)
237
238         self.assertTrue(queue_performer_2.GetPendingItemAtIndex(0).IsValid(), "queue 2's pending item #0 is valid")
239         self.assertTrue(queue_performer_2.GetPendingItemAtIndex(0).GetAddress().GetSymbol().GetName() == "doing_the_work_2", "queue 2's pending item #0 should be doing_the_work_2")
240         self.assertTrue(queue_performer_2.GetNumPendingItems() == 9999, "verify that queue 2 still has 9999 pending items")
241         self.assertTrue(queue_performer_2.GetPendingItemAtIndex(9998).IsValid(), "queue 2's pending item #9998 is valid")
242         self.assertTrue(queue_performer_2.GetPendingItemAtIndex(9998).GetAddress().GetSymbol().GetName() == "doing_the_work_2", "queue 2's pending item #0 should be doing_the_work_2")
243         self.assertTrue(queue_performer_2.GetPendingItemAtIndex(9999).IsValid() == False, "queue 2's pending item #9999 is invalid")