1 //===-- SystemRuntime.h -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_SystemRuntime_h_
11 #define liblldb_SystemRuntime_h_
15 // Other libraries and framework includes
19 #include "lldb/Core/ModuleList.h"
20 #include "lldb/Core/PluginInterface.h"
21 #include "lldb/Target/QueueItem.h"
22 #include "lldb/Target/QueueList.h"
23 #include "lldb/Utility/ConstString.h"
24 #include "lldb/Utility/StructuredData.h"
25 #include "lldb/lldb-private.h"
26 #include "lldb/lldb-public.h"
28 namespace lldb_private {
30 //----------------------------------------------------------------------
31 /// @class SystemRuntime SystemRuntime.h "lldb/Target/SystemRuntime.h"
32 /// A plug-in interface definition class for system runtimes.
34 /// The system runtime plugins can collect information from the system
35 /// libraries during a Process' lifetime and provide information about how
36 /// objects/threads were originated.
38 /// For instance, a system runtime plugin use a breakpoint when threads are
39 /// created to record the backtrace of where that thread was created. Later,
40 /// when backtracing the created thread, it could extend the backtrace to show
41 /// where it was originally created from.
43 /// The plugin will insert its own breakpoint when Created and start
44 /// collecting information. Later when it comes time to augment a Thread, it
45 /// can be asked to provide that information.
47 //----------------------------------------------------------------------
49 class SystemRuntime : public PluginInterface {
51 //------------------------------------------------------------------
52 /// Find a system runtime plugin for a given process.
54 /// Scans the installed SystemRuntime plugins and tries to find an instance
55 /// that can be used to track image changes in \a process.
57 /// @param[in] process
58 /// The process for which to try and locate a system runtime
60 //------------------------------------------------------------------
61 static SystemRuntime *FindPlugin(Process *process);
63 //------------------------------------------------------------------
64 /// Construct with a process.
65 // -----------------------------------------------------------------
66 SystemRuntime(lldb_private::Process *process);
68 //------------------------------------------------------------------
71 /// The destructor is virtual since this class is designed to be inherited
72 /// by the plug-in instance.
73 //------------------------------------------------------------------
74 ~SystemRuntime() override;
76 //------------------------------------------------------------------
77 /// Called after attaching to a process.
79 /// Allow the SystemRuntime plugin to execute some code after attaching to a
81 //------------------------------------------------------------------
82 virtual void DidAttach();
84 //------------------------------------------------------------------
85 /// Called after launching a process.
87 /// Allow the SystemRuntime plugin to execute some code after launching a
89 //------------------------------------------------------------------
90 virtual void DidLaunch();
92 //------------------------------------------------------------------
93 /// Called when modules have been loaded in the process.
95 /// Allow the SystemRuntime plugin to enable logging features in the system
96 /// runtime libraries.
97 //------------------------------------------------------------------
98 virtual void ModulesDidLoad(lldb_private::ModuleList &module_list);
100 //------------------------------------------------------------------
101 /// Called before detaching from a process.
103 /// This will give a SystemRuntime plugin a chance to free any resources in
104 /// the inferior process before we detach.
105 //------------------------------------------------------------------
106 virtual void Detach();
108 //------------------------------------------------------------------
109 /// Return a list of thread origin extended backtraces that may be
112 /// A System Runtime may be able to provide a backtrace of when this
113 /// thread was originally created. Furthermore, it may be able to provide
114 /// that extended backtrace for different styles of creation. On a system
115 /// with both pthreads and libdispatch, aka Grand Central Dispatch, queues,
116 /// the system runtime may be able to provide the pthread creation of the
117 /// thread and it may also be able to provide the backtrace of when this GCD
118 /// queue work block was enqueued. The caller may request these different
121 /// The names will be provided in the order that they are most likely to be
122 /// requested. For instance, a most natural order may be to request the GCD
123 /// libdispatch queue origin. If there is none, then request the pthread
127 /// A vector of ConstStrings with names like "pthread" or "libdispatch".
128 /// An empty vector may be returned if no thread origin extended
129 /// backtrace capabilities are available.
130 //------------------------------------------------------------------
131 virtual const std::vector<ConstString> &GetExtendedBacktraceTypes();
133 //------------------------------------------------------------------
134 /// Return a Thread which shows the origin of this thread's creation.
136 /// This likely returns a HistoryThread which shows how thread was
137 /// originally created (e.g. "pthread" type), or how the work that is
138 /// currently executing on it was originally enqueued (e.g. "libdispatch"
141 /// There may be a chain of thread-origins; it may be informative to the end
142 /// user to query the returned ThreadSP for its origins as well.
144 /// @param [in] thread
145 /// The thread to examine.
148 /// The type of thread origin being requested. The types supported
149 /// are returned from SystemRuntime::GetExtendedBacktraceTypes.
152 /// A ThreadSP which will have a StackList of frames. This Thread will
153 /// not appear in the Process' list of current threads. Normal thread
154 /// operations like stepping will not be available. This is a historical
155 /// view thread and may be only useful for showing a backtrace.
157 /// An empty ThreadSP will be returned if no thread origin is available.
158 //------------------------------------------------------------------
159 virtual lldb::ThreadSP GetExtendedBacktraceThread(lldb::ThreadSP thread,
162 //------------------------------------------------------------------
163 /// Get the extended backtrace thread for a QueueItem
165 /// A QueueItem represents a function/block that will be executed on
166 /// a libdispatch queue in the future, or it represents a function/block
167 /// that is currently executing on a thread.
169 /// This method will report a thread backtrace of the function that enqueued
170 /// it originally, if possible.
172 /// @param [in] queue_item_sp
173 /// The QueueItem that we are getting an extended backtrace for.
176 /// The type of extended backtrace to fetch. The types supported
177 /// are returned from SystemRuntime::GetExtendedBacktraceTypes.
180 /// If an extended backtrace is available, it is returned. Else
181 /// an empty ThreadSP is returned.
182 //------------------------------------------------------------------
183 virtual lldb::ThreadSP
184 GetExtendedBacktraceForQueueItem(lldb::QueueItemSP queue_item_sp,
186 return lldb::ThreadSP();
189 //------------------------------------------------------------------
190 /// Populate the Process' QueueList with libdispatch / GCD queues that
193 /// When process execution is paused, the SystemRuntime may be called to
194 /// fill in the list of Queues that currently exist.
196 /// @param [out] queue_list
197 /// This QueueList will be cleared, and any queues that currently exist
198 /// will be added. An empty QueueList will be returned if no queues
199 /// exist or if this Systemruntime does not support libdispatch queues.
200 //------------------------------------------------------------------
201 virtual void PopulateQueueList(lldb_private::QueueList &queue_list) {}
203 //------------------------------------------------------------------
204 /// Get the queue name for a thread given a thread's dispatch_qaddr.
206 /// On systems using libdispatch queues, a thread may be associated with a
207 /// queue. There will be a call to get the thread's dispatch_qaddr. At the
208 /// dispatch_qaddr we will find the address of this thread's
209 /// dispatch_queue_t structure. Given the address of the dispatch_queue_t
210 /// structure for a thread, get the queue name and return it.
212 /// @param [in] dispatch_qaddr
213 /// The address of the dispatch_qaddr pointer for this thread.
216 /// The string of this queue's name. An empty string is returned if the
217 /// name could not be found.
218 //------------------------------------------------------------------
220 GetQueueNameFromThreadQAddress(lldb::addr_t dispatch_qaddr) {
224 //------------------------------------------------------------------
225 /// Get the QueueID for the libdispatch queue given the thread's
228 /// On systems using libdispatch queues, a thread may be associated with a
229 /// queue. There will be a call to get the thread's dispatch_qaddr. At the
230 /// dispatch_qaddr we will find the address of this thread's
231 /// dispatch_queue_t structure. Given the address of the dispatch_queue_t
232 /// structure for a thread, get the queue ID and return it.
234 /// @param [in] dispatch_qaddr
235 /// The address of the dispatch_qaddr pointer for this thread.
238 /// The queue ID, or if it could not be retrieved, LLDB_INVALID_QUEUE_ID.
239 //------------------------------------------------------------------
240 virtual lldb::queue_id_t
241 GetQueueIDFromThreadQAddress(lldb::addr_t dispatch_qaddr) {
242 return LLDB_INVALID_QUEUE_ID;
245 //------------------------------------------------------------------
246 /// Get the libdispatch_queue_t address for the queue given the thread's
249 /// On systems using libdispatch queues, a thread may be associated with a
250 /// queue. There will be a call to get the thread's dispatch_qaddr. Given
251 /// the thread's dispatch_qaddr, find the libdispatch_queue_t address and
254 /// @param [in] dispatch_qaddr
255 /// The address of the dispatch_qaddr pointer for this thread.
258 /// The libdispatch_queue_t address, or LLDB_INVALID_ADDRESS if
259 /// unavailable/not found.
260 //------------------------------------------------------------------
262 GetLibdispatchQueueAddressFromThreadQAddress(lldb::addr_t dispatch_qaddr) {
263 return LLDB_INVALID_ADDRESS;
266 //------------------------------------------------------------------
267 /// Retrieve the Queue kind for the queue at a thread's dispatch_qaddr.
269 /// Retrieve the Queue kind - either eQueueKindSerial or
270 /// eQueueKindConcurrent, indicating that this queue processes work items
271 /// serially or concurrently.
274 /// The Queue kind, if it could be read, else eQueueKindUnknown.
275 //------------------------------------------------------------------
276 virtual lldb::QueueKind GetQueueKind(lldb::addr_t dispatch_qaddr) {
277 return lldb::eQueueKindUnknown;
280 //------------------------------------------------------------------
281 /// Get the pending work items for a libdispatch Queue
283 /// If this system/process is using libdispatch and the runtime can do so,
284 /// retrieve the list of pending work items for the specified Queue and add
287 /// @param [in] queue
288 /// The queue of interest.
289 //------------------------------------------------------------------
290 virtual void PopulatePendingItemsForQueue(lldb_private::Queue *queue) {}
292 //------------------------------------------------------------------
293 /// Complete the fields in a QueueItem
295 /// PopulatePendingItemsForQueue() may not fill in all of the QueueItem
296 /// details; when the remaining fields are needed, they will be fetched by
297 /// call this method.
299 /// @param [in] queue_item
300 /// The QueueItem that we will be completing.
302 /// @param [in] item_ref
303 /// The item_ref token that is needed to retrieve the rest of the
304 /// information about the QueueItem.
305 //------------------------------------------------------------------
306 virtual void CompleteQueueItem(lldb_private::QueueItem *queue_item,
307 lldb::addr_t item_ref) {}
309 //------------------------------------------------------------------
310 /// Add key-value pairs to the StructuredData dictionary object with
311 /// information debugserver may need when constructing the
312 /// jThreadExtendedInfo packet.
314 /// @param [out] dict
315 /// Dictionary to which key-value pairs should be added; they will
316 /// be sent to the remote gdb server stub as arguments in the
317 /// jThreadExtendedInfo request.
318 //------------------------------------------------------------------
319 virtual void AddThreadExtendedInfoPacketHints(
320 lldb_private::StructuredData::ObjectSP dict) {}
322 /// Determine whether it is safe to run an expression on a given thread
324 /// If a system must not run functions on a thread in some particular state,
325 /// this method gives a way for it to flag that the expression should not be
328 /// @param [in] thread_sp
329 /// The thread we want to run the expression on.
332 /// True will be returned if there are no known problems with running an
333 /// expression on this thread. False means that the inferior function
334 /// call should not be made on this thread.
335 //------------------------------------------------------------------
336 virtual bool SafeToCallFunctionsOnThisThread(lldb::ThreadSP thread_sp) {
341 //------------------------------------------------------------------
343 //------------------------------------------------------------------
346 std::vector<ConstString> m_types;
349 DISALLOW_COPY_AND_ASSIGN(SystemRuntime);
352 } // namespace lldb_private
354 #endif // liblldb_SystemRuntime_h_