]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Core/Event.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r306956, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Core / Event.h
1 //===-- Event.h -------------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef liblldb_Event_h_
11 #define liblldb_Event_h_
12
13 #include "lldb/Core/Broadcaster.h"
14 #include "lldb/Host/Predicate.h"
15 #include "lldb/Utility/ConstString.h"
16 #include "lldb/Utility/StructuredData.h"
17 #include "lldb/lldb-defines.h" // for DISALLOW_COPY_AND_ASSIGN
18 #include "lldb/lldb-forward.h" // for EventDataSP, ProcessSP, Struct...
19
20 #include "llvm/ADT/StringRef.h" // for StringRef
21
22 #include <chrono>
23 #include <memory>
24 #include <string>
25
26 #include <stddef.h> // for size_t
27 #include <stdint.h> // for uint32_t
28
29 namespace lldb_private {
30 class Event;
31 }
32 namespace lldb_private {
33 class Stream;
34 }
35
36 namespace lldb_private {
37
38 //----------------------------------------------------------------------
39 // lldb::EventData
40 //----------------------------------------------------------------------
41 class EventData {
42   friend class Event;
43
44 public:
45   EventData();
46
47   virtual ~EventData();
48
49   virtual const ConstString &GetFlavor() const = 0;
50
51   virtual void Dump(Stream *s) const;
52
53 private:
54   virtual void DoOnRemoval(Event *event_ptr) {}
55
56   DISALLOW_COPY_AND_ASSIGN(EventData);
57 };
58
59 //----------------------------------------------------------------------
60 // lldb::EventDataBytes
61 //----------------------------------------------------------------------
62 class EventDataBytes : public EventData {
63 public:
64   //------------------------------------------------------------------
65   // Constructors
66   //------------------------------------------------------------------
67   EventDataBytes();
68
69   EventDataBytes(const char *cstr);
70
71   EventDataBytes(llvm::StringRef str);
72
73   EventDataBytes(const void *src, size_t src_len);
74
75   ~EventDataBytes() override;
76
77   //------------------------------------------------------------------
78   // Member functions
79   //------------------------------------------------------------------
80   const ConstString &GetFlavor() const override;
81
82   void Dump(Stream *s) const override;
83
84   const void *GetBytes() const;
85
86   size_t GetByteSize() const;
87
88   void SetBytes(const void *src, size_t src_len);
89
90   void SwapBytes(std::string &new_bytes);
91
92   void SetBytesFromCString(const char *cstr);
93
94   //------------------------------------------------------------------
95   // Static functions
96   //------------------------------------------------------------------
97   static const EventDataBytes *GetEventDataFromEvent(const Event *event_ptr);
98
99   static const void *GetBytesFromEvent(const Event *event_ptr);
100
101   static size_t GetByteSizeFromEvent(const Event *event_ptr);
102
103   static const ConstString &GetFlavorString();
104
105 private:
106   std::string m_bytes;
107
108   DISALLOW_COPY_AND_ASSIGN(EventDataBytes);
109 };
110
111 class EventDataReceipt : public EventData {
112 public:
113   EventDataReceipt() : EventData(), m_predicate(false) {}
114
115   ~EventDataReceipt() override {}
116
117   static const ConstString &GetFlavorString() {
118     static ConstString g_flavor("Process::ProcessEventData");
119     return g_flavor;
120   }
121
122   const ConstString &GetFlavor() const override { return GetFlavorString(); }
123
124   bool WaitForEventReceived(
125       const std::chrono::microseconds &abstime = std::chrono::microseconds(0),
126       bool *timed_out = nullptr) {
127     return m_predicate.WaitForValueEqualTo(true, abstime, timed_out);
128   }
129
130 private:
131   Predicate<bool> m_predicate;
132
133   void DoOnRemoval(Event *event_ptr) override {
134     m_predicate.SetValue(true, eBroadcastAlways);
135   }
136 };
137
138 //----------------------------------------------------------------------
139 /// This class handles one or more StructuredData::Dictionary entries
140 /// that are raised for structured data events.
141 //----------------------------------------------------------------------
142
143 class EventDataStructuredData : public EventData {
144 public:
145   //------------------------------------------------------------------
146   // Constructors
147   //------------------------------------------------------------------
148   EventDataStructuredData();
149
150   EventDataStructuredData(const lldb::ProcessSP &process_sp,
151                           const StructuredData::ObjectSP &object_sp,
152                           const lldb::StructuredDataPluginSP &plugin_sp);
153
154   ~EventDataStructuredData() override;
155
156   //------------------------------------------------------------------
157   // Member functions
158   //------------------------------------------------------------------
159   const ConstString &GetFlavor() const override;
160
161   void Dump(Stream *s) const override;
162
163   const lldb::ProcessSP &GetProcess() const;
164
165   const StructuredData::ObjectSP &GetObject() const;
166
167   const lldb::StructuredDataPluginSP &GetStructuredDataPlugin() const;
168
169   void SetProcess(const lldb::ProcessSP &process_sp);
170
171   void SetObject(const StructuredData::ObjectSP &object_sp);
172
173   void SetStructuredDataPlugin(const lldb::StructuredDataPluginSP &plugin_sp);
174
175   //------------------------------------------------------------------
176   // Static functions
177   //------------------------------------------------------------------
178   static const EventDataStructuredData *
179   GetEventDataFromEvent(const Event *event_ptr);
180
181   static lldb::ProcessSP GetProcessFromEvent(const Event *event_ptr);
182
183   static StructuredData::ObjectSP GetObjectFromEvent(const Event *event_ptr);
184
185   static lldb::StructuredDataPluginSP
186   GetPluginFromEvent(const Event *event_ptr);
187
188   static const ConstString &GetFlavorString();
189
190 private:
191   lldb::ProcessSP m_process_sp;
192   StructuredData::ObjectSP m_object_sp;
193   lldb::StructuredDataPluginSP m_plugin_sp;
194
195   DISALLOW_COPY_AND_ASSIGN(EventDataStructuredData);
196 };
197
198 //----------------------------------------------------------------------
199 // lldb::Event
200 //----------------------------------------------------------------------
201 class Event {
202   friend class Listener;
203   friend class EventData;
204   friend class Broadcaster::BroadcasterImpl;
205
206 public:
207   Event(Broadcaster *broadcaster, uint32_t event_type,
208         EventData *data = nullptr);
209
210   Event(Broadcaster *broadcaster, uint32_t event_type,
211         const lldb::EventDataSP &event_data_sp);
212
213   Event(uint32_t event_type, EventData *data = nullptr);
214
215   Event(uint32_t event_type, const lldb::EventDataSP &event_data_sp);
216
217   ~Event();
218
219   void Dump(Stream *s) const;
220
221   EventData *GetData() { return m_data_sp.get(); }
222
223   const EventData *GetData() const { return m_data_sp.get(); }
224
225   void SetData(EventData *new_data) { m_data_sp.reset(new_data); }
226
227   uint32_t GetType() const { return m_type; }
228
229   void SetType(uint32_t new_type) { m_type = new_type; }
230
231   Broadcaster *GetBroadcaster() const {
232     Broadcaster::BroadcasterImplSP broadcaster_impl_sp =
233         m_broadcaster_wp.lock();
234     if (broadcaster_impl_sp)
235       return broadcaster_impl_sp->GetBroadcaster();
236     else
237       return nullptr;
238   }
239
240   bool BroadcasterIs(Broadcaster *broadcaster) {
241     Broadcaster::BroadcasterImplSP broadcaster_impl_sp =
242         m_broadcaster_wp.lock();
243     if (broadcaster_impl_sp)
244       return broadcaster_impl_sp->GetBroadcaster() == broadcaster;
245     else
246       return false;
247   }
248
249   void Clear() { m_data_sp.reset(); }
250
251 private:
252   // This is only called by Listener when it pops an event off the queue for
253   // the listener.  It calls the Event Data's DoOnRemoval() method, which is
254   // virtual and can be overridden by the specific data classes.
255
256   void DoOnRemoval();
257
258   // Called by Broadcaster::BroadcastEvent prior to letting all the listeners
259   // know about it update the contained broadcaster so that events can be
260   // popped off one queue and re-broadcast to others.
261   void SetBroadcaster(Broadcaster *broadcaster) {
262     m_broadcaster_wp = broadcaster->GetBroadcasterImpl();
263   }
264
265   Broadcaster::BroadcasterImplWP
266       m_broadcaster_wp;        // The broadcaster that sent this event
267   uint32_t m_type;             // The bit describing this event
268   lldb::EventDataSP m_data_sp; // User specific data for this event
269
270   DISALLOW_COPY_AND_ASSIGN(Event);
271   Event(); // Disallow default constructor
272 };
273
274 } // namespace lldb_private
275
276 #endif // liblldb_Event_h_