1
2// LLDB C++ API Test: verify the event description that is received by an
3// SBListener object registered with a process with a breakpoint.
4
5#include <atomic>
6#include <array>
7#include <iostream>
8#include <string>
9#include <thread>
10
11%include_SB_APIs%
12
13#include "common.h"
14
15using namespace lldb;
16using namespace std;
17
18// listener thread control
19extern atomic<bool> g_done;
20extern SBListener g_listener;
21
22multithreaded_queue<string> g_event_descriptions;
23string g_error_desc;
24
25void listener_func() {
26  while (!g_done) {
27    SBEvent event;
28    bool got_event = g_listener.WaitForEvent(1, event);
29
30    if (got_event) {
31      if (!event.IsValid())
32        throw Exception("event is not valid in listener thread");
33
34      SBStream description;
35      event.GetDescription(description);
36      string str(description.GetData());
37      g_event_descriptions.push(str);
38    }
39  }
40}
41
42bool check_state(string &state, string &desc, bool got_description)
43{
44    g_error_desc.clear();
45
46    if(!got_description)
47    {
48        g_error_desc.append("Did not get expected event description");
49        return false;
50    }
51
52    if (desc.find("state-changed") == desc.npos)
53        g_error_desc.append("Event description incorrect: missing 'state-changed' ");
54
55    if (desc.find("pid = ") == desc.npos)
56        g_error_desc.append("Event description incorrect: missing process pid ");
57
58    string state_search_str = "state = " + state;
59    if (desc.find(state_search_str) == desc.npos)
60    {
61        string errString = ("Event description incorrect: expected state "
62                      + state
63                      + " but desc was "
64                      + desc);
65        g_error_desc.append(errString);
66    }
67
68    if (g_error_desc.length() > 0)
69        return false;
70
71    cout << "check_state: " << state << "  OK\n";
72    return true;
73}
74
75void check_listener(SBDebugger &dbg)
76{
77    bool got_description;
78    string state;
79
80    // check for "launching" state, this may or may not be present
81    string desc = g_event_descriptions.pop(5, got_description);
82    state = "launching";
83    if (check_state(state, desc, got_description))
84    {
85        // found a 'launching' state, pop next one from queue
86        desc = g_event_descriptions.pop(5, got_description);
87    }
88
89    state = "running";
90    if( !check_state(state, desc, got_description) )
91        throw Exception(g_error_desc);
92
93    desc = g_event_descriptions.pop(5, got_description);
94    state = "stopped";
95    if( !check_state(state, desc, got_description) )
96        throw Exception(g_error_desc);
97}
98