summaryrefslogtreecommitdiffstats
path: root/lldb/gtest/unittest/Plugins/Process/Linux/ThreadStateCoordinatorTest.cpp
blob: 6835b110d737a9a35257b37e3f2451d98a392552 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include <limits.h>
#include "gtest/gtest.h"

#include "Plugins/Process/Linux/ThreadStateCoordinator.h"

using namespace lldb_private;

namespace
{
    const ThreadStateCoordinator::ThreadIDSet EMPTY_THREAD_ID_SET;

    void
    NOPLogger (const char *format, va_list args)
    {
        // Do nothing.
    }
}

TEST(ThreadStateCoordinatorTest, StopCoordinatorWorksNoPriorEvents)
{
    ThreadStateCoordinator coordinator(NOPLogger);

    coordinator.StopCoordinator ();

    ASSERT_EQ(false, coordinator.ProcessNextEvent ());
}

TEST(ThreadStateCoordinatorTest, CallAfterThreadsStopFiresWhenNoPendingStops)
{
    ThreadStateCoordinator coordinator(NOPLogger);

    const lldb::tid_t TRIGGERING_TID = 4105;
    bool call_after_fired = false;
    lldb::tid_t reported_firing_tid = 0;

    // Notify we have a trigger that needs to be fired when all threads in the wait tid set have stopped.
    coordinator.CallAfterThreadsStop (TRIGGERING_TID,
                                      EMPTY_THREAD_ID_SET,
                                      [](lldb::tid_t tid) {},
                                      [&](lldb::tid_t tid) {
                                          call_after_fired = true;
                                          reported_firing_tid = tid;
                                      });

    // Notification trigger shouldn't go off yet.
    ASSERT_EQ (false, call_after_fired);

    // Process next event.  This will pick up the call after threads stop event.
    ASSERT_EQ (true, coordinator.ProcessNextEvent ());

    // Now the trigger should have fired, since there were no threads that needed to first stop.
    ASSERT_EQ (true, call_after_fired);

    // And the firing tid should be the one we indicated.
    ASSERT_EQ (TRIGGERING_TID, reported_firing_tid);
}

TEST(ThreadStateCoordinatorTest, CallAfterThreadsStopFiresWhenOnePendingStop)
{
    ThreadStateCoordinator coordinator(NOPLogger);

    const lldb::tid_t TRIGGERING_TID = 4105;
    const lldb::tid_t PENDING_STOP_TID = 3;

    ThreadStateCoordinator::ThreadIDSet pending_stop_tids { PENDING_STOP_TID };

    bool call_after_fired = false;
    lldb::tid_t reported_firing_tid = 0;

    bool request_thread_stop_called = false;
    lldb::tid_t request_thread_stop_tid = 0;

    // Notify we have a trigger that needs to be fired when all threads in the wait tid set have stopped.
    coordinator.CallAfterThreadsStop (TRIGGERING_TID,
                                      pending_stop_tids,
                                      [&](lldb::tid_t tid) {
                                          request_thread_stop_called = true;
                                          request_thread_stop_tid = tid;

                                      },
                                      [&](lldb::tid_t tid) {
                                          call_after_fired = true;
                                          reported_firing_tid = tid;
                                      });

    // Neither trigger should have gone off yet.
    ASSERT_EQ (false, call_after_fired);
    ASSERT_EQ (false, request_thread_stop_called);

    // Process next event.
    ASSERT_EQ (true, coordinator.ProcessNextEvent ());

    // Now the request thread stop should have been called for the pending stop.
    ASSERT_EQ (true, request_thread_stop_called);
    ASSERT_EQ (PENDING_STOP_TID, request_thread_stop_tid);

    // But we still shouldn't have the deferred signal call go off yet.  Need to wait for the stop to be reported.
    ASSERT_EQ (false, call_after_fired);

    // Now report the that the pending stop occurred.
    coordinator.NotifyThreadStop (PENDING_STOP_TID);

    // Shouldn't take effect until after next processing step.
    ASSERT_EQ (false, call_after_fired);

    // Process next event.
    ASSERT_EQ (true, coordinator.ProcessNextEvent ());

    // Deferred signal notification should have fired now.
    ASSERT_EQ (true, call_after_fired);
    ASSERT_EQ (TRIGGERING_TID, reported_firing_tid);
}

TEST(ThreadStateCoordinatorTest, CallAfterThreadsStopFiresWhenTwoPendingStops)
{
    ThreadStateCoordinator coordinator(NOPLogger);

    const lldb::tid_t TRIGGERING_TID = 4105;
    const lldb::tid_t PENDING_STOP_TID = 3;
    const lldb::tid_t PENDING_STOP_TID_02 = 29016;

    ThreadStateCoordinator::ThreadIDSet pending_stop_tids { PENDING_STOP_TID, PENDING_STOP_TID_02 };

    bool call_after_fired = false;
    lldb::tid_t reported_firing_tid = 0;

    int request_thread_stop_calls = 0;
    ThreadStateCoordinator::ThreadIDSet request_thread_stop_tids;

    // Notify we have a trigger that needs to be fired when all threads in the wait tid set have stopped.
    coordinator.CallAfterThreadsStop (TRIGGERING_TID,
                                      pending_stop_tids,
                                      [&](lldb::tid_t tid) {
                                          ++request_thread_stop_calls;
                                          request_thread_stop_tids.insert (tid);

                                      },
                                      [&](lldb::tid_t tid) {
                                          call_after_fired = true;
                                          reported_firing_tid = tid;
                                      });

    // Neither trigger should have gone off yet.
    ASSERT_EQ (false, call_after_fired);
    ASSERT_EQ (0, request_thread_stop_calls);

    // Process next event.
    ASSERT_EQ (true, coordinator.ProcessNextEvent ());

    // Now the request thread stops should have been called for the pending stop tids.
    ASSERT_EQ (2, request_thread_stop_calls);
    ASSERT_EQ (1, request_thread_stop_tids.count (PENDING_STOP_TID));
    ASSERT_EQ (1, request_thread_stop_tids.count (PENDING_STOP_TID_02));

    // But we still shouldn't have the deferred signal call go off yet.  Need to wait for the stop to be reported.
    ASSERT_EQ (false, call_after_fired);

    // Report the that the first pending stop occurred.
    coordinator.NotifyThreadStop (PENDING_STOP_TID);
    ASSERT_EQ (true, coordinator.ProcessNextEvent ());

    // Shouldn't take effect until after both pending threads are notified.
    ASSERT_EQ (false, call_after_fired);

    // Report the that the first pending stop occurred.
    coordinator.NotifyThreadStop (PENDING_STOP_TID_02);
    ASSERT_EQ (false, call_after_fired);
    ASSERT_EQ (true, coordinator.ProcessNextEvent ());

    // Deferred signal notification should have fired now.
    ASSERT_EQ (true, call_after_fired);
    ASSERT_EQ (TRIGGERING_TID, reported_firing_tid);
}

TEST(ThreadStateCoordinatorTest, CallAfterThreadsStopFiresWhenPendingAlreadyStopped)
{
    ThreadStateCoordinator coordinator(NOPLogger);

    const lldb::tid_t TRIGGERING_TID = 4105;
    const lldb::tid_t PENDING_STOP_TID = 3;

    ThreadStateCoordinator::ThreadIDSet pending_stop_tids { PENDING_STOP_TID };

    // Tell coordinator the pending stop tid is already stopped.
    coordinator.NotifyThreadStop (PENDING_STOP_TID);
    ASSERT_EQ (true, coordinator.ProcessNextEvent ());

    // Now fire the deferred thread stop notification, indicating that the pending thread
    // must be stopped before we notify.
    bool call_after_fired = false;
    lldb::tid_t reported_firing_tid = 0;

    bool request_thread_stop_called = false;
    lldb::tid_t request_thread_stop_tid = 0;

    // Notify we have a trigger that needs to be fired when all threads in the wait tid set have stopped.
    coordinator.CallAfterThreadsStop (TRIGGERING_TID,
                                      pending_stop_tids,
                                      [&](lldb::tid_t tid) {
                                          request_thread_stop_called = true;
                                          request_thread_stop_tid = tid;

                                      },
                                      [&](lldb::tid_t tid) {
                                          call_after_fired = true;
                                          reported_firing_tid = tid;
                                      });

    // Neither trigger should have gone off yet.
    ASSERT_EQ (false, call_after_fired);
    ASSERT_EQ (false, request_thread_stop_called);

    // Process next event.
    ASSERT_EQ (true, coordinator.ProcessNextEvent ());

    // The pending stop should *not* fire because the coordinator knows it has already stopped.
    ASSERT_EQ (false, request_thread_stop_called);

    // The deferred signal notification should have fired since all requirements were met.
    ASSERT_EQ (true, call_after_fired);
    ASSERT_EQ (TRIGGERING_TID, reported_firing_tid);
}

TEST(ThreadStateCoordinatorTest, CallAfterThreadsStopFiresWhenTwoPendingOneAlreadyStopped)
{
    ThreadStateCoordinator coordinator(NOPLogger);

    const lldb::tid_t TRIGGERING_TID = 4105;
    const lldb::tid_t PENDING_STOP_TID = 3;
    const lldb::tid_t PENDING_STOP_TID_02 = 29016;

    ThreadStateCoordinator::ThreadIDSet pending_stop_tids { PENDING_STOP_TID, PENDING_STOP_TID_02 };

    // Tell coordinator the pending stop tid is already stopped.
    coordinator.NotifyThreadStop (PENDING_STOP_TID);
    ASSERT_EQ (true, coordinator.ProcessNextEvent ());

    // Now fire the deferred thread stop notification, indicating that the pending thread
    // must be stopped before we notify.
    bool call_after_fired = false;
    lldb::tid_t reported_firing_tid = 0;

    int request_thread_stop_calls = 0;
    lldb::tid_t request_thread_stop_tid = 0;

    // Notify we have a trigger that needs to be fired when all threads in the wait tid set have stopped.
    coordinator.CallAfterThreadsStop (TRIGGERING_TID,
                                      pending_stop_tids,
                                      [&](lldb::tid_t tid) {
                                          ++request_thread_stop_calls;
                                          request_thread_stop_tid = tid;

                                      },
                                      [&](lldb::tid_t tid) {
                                          call_after_fired = true;
                                          reported_firing_tid = tid;
                                      });

    // Neither trigger should have gone off yet.
    ASSERT_EQ (false, call_after_fired);
    ASSERT_EQ (0, request_thread_stop_calls);

    // Process next event.
    ASSERT_EQ (true, coordinator.ProcessNextEvent ());

    // The pending stop should only fire for one of the threads, the one that wasn't already stopped.
    ASSERT_EQ (1, request_thread_stop_calls);
    ASSERT_EQ (PENDING_STOP_TID_02, request_thread_stop_tid);

    // The deferred signal notification should not yet have fired since all pending thread stops have not yet occurred.
    ASSERT_EQ (false, call_after_fired);

    // Notify final thread has stopped.
    coordinator.NotifyThreadStop (PENDING_STOP_TID_02);
    ASSERT_EQ (true, coordinator.ProcessNextEvent ());

    // The deferred signal notification should have fired since all requirements were met.
    ASSERT_EQ (true, call_after_fired);
    ASSERT_EQ (TRIGGERING_TID, reported_firing_tid);
}

TEST(ThreadStateCoordinatorTest, CallAfterThreadsStopFiresWhenOnePendingThreadDies)
{
    ThreadStateCoordinator coordinator(NOPLogger);

    const lldb::tid_t TRIGGERING_TID = 4105;
    const lldb::tid_t PENDING_STOP_TID = 3;

    ThreadStateCoordinator::ThreadIDSet pending_stop_tids { PENDING_STOP_TID };

    bool call_after_fired = false;
    lldb::tid_t reported_firing_tid = 0;

    bool request_thread_stop_called = false;
    lldb::tid_t request_thread_stop_tid = 0;

    // Notify we have a trigger that needs to be fired when all threads in the wait tid set have stopped.
    coordinator.CallAfterThreadsStop (TRIGGERING_TID,
                                      pending_stop_tids,
                                      [&](lldb::tid_t tid) {
                                          request_thread_stop_called = true;
                                          request_thread_stop_tid = tid;

                                      },
                                      [&](lldb::tid_t tid) {
                                          call_after_fired = true;
                                          reported_firing_tid = tid;
                                      });

    // Neither trigger should have gone off yet.
    ASSERT_EQ (false, call_after_fired);
    ASSERT_EQ (false, request_thread_stop_called);

    // Process next event.
    ASSERT_EQ (true, coordinator.ProcessNextEvent ());

    // Now the request thread stop should have been called for the pending stop.
    ASSERT_EQ (true, request_thread_stop_called);
    ASSERT_EQ (PENDING_STOP_TID, request_thread_stop_tid);

    // But we still shouldn't have the deferred signal call go off yet.  Need to wait for the death to be reported.
    ASSERT_EQ (false, call_after_fired);

    // Now report the that the thread with pending stop dies.
    coordinator.NotifyThreadDeath (PENDING_STOP_TID);

    // Shouldn't take effect until after next processing step.
    ASSERT_EQ (false, call_after_fired);

    // Process next event.
    ASSERT_EQ (true, coordinator.ProcessNextEvent ());

    // Deferred signal notification should have fired now.
    ASSERT_EQ (true, call_after_fired);
    ASSERT_EQ (TRIGGERING_TID, reported_firing_tid);
}

TEST(ThreadStateCoordinatorTest, ExistingPendingNotificationRequiresStopFromNewThread)
{
    ThreadStateCoordinator coordinator(NOPLogger);

    const lldb::tid_t TRIGGERING_TID = 4105;
    const lldb::tid_t PENDING_STOP_TID = 3;

    ThreadStateCoordinator::ThreadIDSet pending_stop_tids { PENDING_STOP_TID };

    bool call_after_fired = false;
    lldb::tid_t reported_firing_tid = 0;

    int request_thread_stop_calls = 0;
    ThreadStateCoordinator::ThreadIDSet request_thread_stop_tids;

    // Notify we have a trigger that needs to be fired when all threads in the wait tid set have stopped.
    coordinator.CallAfterThreadsStop (TRIGGERING_TID,
                                      pending_stop_tids,
                                      [&](lldb::tid_t tid) {
                                          ++request_thread_stop_calls;
                                          request_thread_stop_tids.insert (tid);

                                      },
                                      [&](lldb::tid_t tid) {
                                          call_after_fired = true;
                                          reported_firing_tid = tid;
                                      });

    // Neither trigger should have gone off yet.
    ASSERT_EQ (false, call_after_fired);
    ASSERT_EQ (0, request_thread_stop_calls);

    // Process next event.
    ASSERT_EQ (true, coordinator.ProcessNextEvent ());

    // Now the request thread stop should have been called for the pending stop.
    ASSERT_EQ (1, request_thread_stop_calls);
    ASSERT_EQ (true, request_thread_stop_tids.count (PENDING_STOP_TID));

    // But we still shouldn't have the deferred signal call go off yet.
    ASSERT_EQ (false, call_after_fired);

    // Indicate a new thread has just been created.
    const lldb::tid_t NEW_THREAD_TID = 1234;

    coordinator.NotifyThreadCreate (NEW_THREAD_TID);
    ASSERT_EQ (true, coordinator.ProcessNextEvent ());

    // We should have just received a stop request for the new thread id.
    ASSERT_EQ (2, request_thread_stop_calls);
    ASSERT_EQ (true, request_thread_stop_tids.count (NEW_THREAD_TID));

    // Now report the original pending tid stopped.  This should no longer
    // trigger the pending notification because we should now require the
    // new thread to stop too.
    coordinator.NotifyThreadStop (PENDING_STOP_TID);
    ASSERT_EQ (true, coordinator.ProcessNextEvent ());
    ASSERT_EQ (false, call_after_fired);

    // Now notify the new thread stopped.
    coordinator.NotifyThreadStop (NEW_THREAD_TID);
    ASSERT_EQ (true, coordinator.ProcessNextEvent ());

    // Deferred signal notification should have fired now.
    ASSERT_EQ (true, call_after_fired);
    ASSERT_EQ (TRIGGERING_TID, reported_firing_tid);
}

OpenPOWER on IntegriCloud