1 //===-- ExecutionContext.h --------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_ExecutionContext_h_
10 #define liblldb_ExecutionContext_h_
14 #include "lldb/Target/StackID.h"
15 #include "lldb/lldb-private.h"
17 namespace lldb_private {
19 //===----------------------------------------------------------------------===//
20 /// Execution context objects refer to objects in the execution of the program
21 /// that is being debugged. The consist of one or more of the following
22 /// objects: target, process, thread, and frame. Many objects in the debugger
23 /// need to track different executions contexts. For example, a local function
24 /// variable might have an execution context that refers to a stack frame. A
25 /// global or static variable might refer to a target since a stack frame
26 /// isn't required in order to evaluate a global or static variable (a process
27 /// isn't necessarily needed for a global variable since we might be able to
28 /// read the variable value from a data section in one of the object files in
29 /// a target). There are two types of objects that hold onto execution
30 /// contexts: ExecutionContextRef and ExecutionContext. Both of these objects
31 /// are described below.
33 /// Not all objects in an ExecutionContext objects will be valid. If you want
34 /// to refer strongly (ExecutionContext) or weakly (ExecutionContextRef) to a
35 /// process, then only the process and target references will be valid. For
36 /// threads, only the thread, process and target references will be filled in.
37 /// For frames, all of the objects will be filled in.
39 /// These classes are designed to be used as baton objects that get passed to
40 /// a wide variety of functions that require execution contexts.
41 //===----------------------------------------------------------------------===//
43 /// \class ExecutionContextRef ExecutionContext.h
44 /// "lldb/Target/ExecutionContext.h"
45 /// A class that holds a weak reference to an execution context.
47 /// ExecutionContextRef objects are designed to hold onto an execution context
48 /// that might change over time. For example, if an object wants to refer to a
49 /// stack frame, it should hold onto an ExecutionContextRef to a frame object.
50 /// The backing object that represents the stack frame might change over time
51 /// and instances of this object can track the logical object that refers to a
52 /// frame even if it does change.
54 /// These objects also don't keep execution objects around longer than they
55 /// should since they use weak pointers. For example if an object refers to a
56 /// stack frame and a stack frame is no longer in a thread, then a
57 /// ExecutionContextRef object that refers to that frame will not be able to
58 /// get a shared pointer to those objects since they are no longer around.
60 /// ExecutionContextRef objects can also be used as objects in classes that
61 /// want to track a "previous execution context". Since the weak references to
62 /// the execution objects (target, process, thread and frame) don't keep these
63 /// objects around, they are safe to keep around.
65 /// The general rule of thumb is all long lived objects that want to refer to
66 /// execution contexts should use ExecutionContextRef objects. The
67 /// ExecutionContext class is used to temporarily get shared pointers to any
68 /// execution context objects that are still around so they are guaranteed to
69 /// exist during a function that requires the objects. ExecutionContext
70 /// objects should NOT be used for long term storage since they will keep
71 /// objects alive with extra shared pointer references to these objects.
72 class ExecutionContextRef {
74 /// Default Constructor.
75 ExecutionContextRef();
78 ExecutionContextRef(const ExecutionContextRef &rhs);
80 /// Construct using an ExecutionContext object that might be nullptr.
82 /// If \a exe_ctx_ptr is valid, then make weak references to any valid
83 /// objects in the ExecutionContext, otherwise no weak references to any
84 /// execution context objects will be made.
85 ExecutionContextRef(const ExecutionContext *exe_ctx_ptr);
87 /// Construct using an ExecutionContext object.
89 /// Make weak references to any valid objects in the ExecutionContext.
90 ExecutionContextRef(const ExecutionContext &exe_ctx);
92 /// Construct using the target and all the selected items inside of it (the
93 /// process and its selected thread, and the thread's selected frame). If
94 /// there is no selected thread, default to the first thread If there is no
95 /// selected frame, default to the first frame.
96 ExecutionContextRef(Target *target, bool adopt_selected);
98 /// Construct using an execution context scope.
100 /// If the ExecutionContextScope object is valid and refers to a frame, make
101 /// weak references too the frame, thread, process and target. If the
102 /// ExecutionContextScope object is valid and refers to a thread, make weak
103 /// references too the thread, process and target. If the
104 /// ExecutionContextScope object is valid and refers to a process, make weak
105 /// references too the process and target. If the ExecutionContextScope
106 /// object is valid and refers to a target, make weak references too the
108 ExecutionContextRef(ExecutionContextScope *exe_scope);
110 /// Construct using an execution context scope.
112 /// If the ExecutionContextScope object refers to a frame, make weak
113 /// references too the frame, thread, process and target. If the
114 /// ExecutionContextScope object refers to a thread, make weak references
115 /// too the thread, process and target. If the ExecutionContextScope object
116 /// refers to a process, make weak references too the process and target. If
117 /// the ExecutionContextScope object refers to a target, make weak
118 /// references too the target.
119 ExecutionContextRef(ExecutionContextScope &exe_scope);
121 ~ExecutionContextRef();
123 /// Assignment operator
125 /// Copy all weak references in \a rhs.
126 ExecutionContextRef &operator=(const ExecutionContextRef &rhs);
128 /// Assignment operator from a ExecutionContext
130 /// Make weak references to any strongly referenced objects in \a exe_ctx.
131 ExecutionContextRef &operator=(const ExecutionContext &exe_ctx);
133 /// Clear the object's state.
135 /// Sets the process and thread to nullptr, and the frame index to an
139 /// Set accessor that creates a weak reference to the target referenced in
142 /// If \a target_sp is valid this object will create a weak reference to
143 /// that object, otherwise any previous target weak reference contained in
144 /// this object will be reset.
146 /// Only the weak reference to the target will be updated, no other weak
147 /// references will be modified. If you want this execution context to make
148 /// a weak reference to the target's process, use the
149 /// ExecutionContextRef::SetContext() functions.
151 /// \see ExecutionContextRef::SetContext(const lldb::TargetSP &, bool)
152 void SetTargetSP(const lldb::TargetSP &target_sp);
154 /// Set accessor that creates a weak reference to the process referenced in
157 /// If \a process_sp is valid this object will create a weak reference to
158 /// that object, otherwise any previous process weak reference contained in
159 /// this object will be reset.
161 /// Only the weak reference to the process will be updated, no other weak
162 /// references will be modified. If you want this execution context to make
163 /// a weak reference to the target, use the
164 /// ExecutionContextRef::SetContext() functions.
166 /// \see ExecutionContextRef::SetContext(const lldb::ProcessSP &)
167 void SetProcessSP(const lldb::ProcessSP &process_sp);
169 /// Set accessor that creates a weak reference to the thread referenced in
172 /// If \a thread_sp is valid this object will create a weak reference to
173 /// that object, otherwise any previous thread weak reference contained in
174 /// this object will be reset.
176 /// Only the weak reference to the thread will be updated, no other weak
177 /// references will be modified. If you want this execution context to make
178 /// a weak reference to the thread's process and target, use the
179 /// ExecutionContextRef::SetContext() functions.
181 /// \see ExecutionContextRef::SetContext(const lldb::ThreadSP &)
182 void SetThreadSP(const lldb::ThreadSP &thread_sp);
184 /// Set accessor that creates a weak reference to the frame referenced in \a
187 /// If \a frame_sp is valid this object will create a weak reference to that
188 /// object, otherwise any previous frame weak reference contained in this
189 /// object will be reset.
191 /// Only the weak reference to the frame will be updated, no other weak
192 /// references will be modified. If you want this execution context to make
193 /// a weak reference to the frame's thread, process and target, use the
194 /// ExecutionContextRef::SetContext() functions.
196 /// \see ExecutionContextRef::SetContext(const lldb::StackFrameSP &)
197 void SetFrameSP(const lldb::StackFrameSP &frame_sp);
199 void SetTargetPtr(Target *target, bool adopt_selected);
201 void SetProcessPtr(Process *process);
203 void SetThreadPtr(Thread *thread);
205 void SetFramePtr(StackFrame *frame);
207 /// Get accessor that creates a strong reference from the weak target
208 /// reference contained in this object.
211 /// A shared pointer to a target that is not guaranteed to be valid.
212 lldb::TargetSP GetTargetSP() const;
214 /// Get accessor that creates a strong reference from the weak process
215 /// reference contained in this object.
218 /// A shared pointer to a process that is not guaranteed to be valid.
219 lldb::ProcessSP GetProcessSP() const;
221 /// Get accessor that creates a strong reference from the weak thread
222 /// reference contained in this object.
225 /// A shared pointer to a thread that is not guaranteed to be valid.
226 lldb::ThreadSP GetThreadSP() const;
228 /// Get accessor that creates a strong reference from the weak frame
229 /// reference contained in this object.
232 /// A shared pointer to a frame that is not guaranteed to be valid.
233 lldb::StackFrameSP GetFrameSP() const;
235 /// Create an ExecutionContext object from this object.
237 /// Create strong references to any execution context objects that are still
238 /// valid. Any of the returned shared pointers in the ExecutionContext
239 /// objects is not guaranteed to be valid. \returns
240 /// An execution context object that has strong references to
241 /// any valid weak references in this object.
242 ExecutionContext Lock(bool thread_and_frame_only_if_stopped) const;
244 /// Returns true if this object has a weak reference to a thread. The return
245 /// value is only an indication of whether this object has a weak reference
246 /// and does not indicate whether the weak reference is valid or not.
247 bool HasThreadRef() const { return m_tid != LLDB_INVALID_THREAD_ID; }
249 /// Returns true if this object has a weak reference to a frame. The return
250 /// value is only an indication of whether this object has a weak reference
251 /// and does not indicate whether the weak reference is valid or not.
252 bool HasFrameRef() const { return m_stack_id.IsValid(); }
256 m_tid = LLDB_INVALID_THREAD_ID;
259 void ClearFrame() { m_stack_id.Clear(); }
263 lldb::TargetWP m_target_wp; ///< A weak reference to a target
264 lldb::ProcessWP m_process_wp; ///< A weak reference to a process
265 mutable lldb::ThreadWP m_thread_wp; ///< A weak reference to a thread
266 lldb::tid_t m_tid; ///< The thread ID that this object refers to in case the
267 ///backing object changes
268 StackID m_stack_id; ///< The stack ID that this object refers to in case the
269 ///backing object changes
272 /// \class ExecutionContext ExecutionContext.h
273 /// "lldb/Target/ExecutionContext.h"
274 /// A class that contains an execution context.
276 /// This baton object can be passed into any function that requires a context
277 /// that specifies a target, process, thread and frame. These objects are
278 /// designed to be used for short term execution context object storage while
279 /// a function might be trying to evaluate something that requires a thread or
280 /// frame. ExecutionContextRef objects can be used to initialize one of these
281 /// objects to turn the weak execution context object references to the
282 /// target, process, thread and frame into strong references (shared pointers)
283 /// so that functions can guarantee that these objects won't go away in the
284 /// middle of a function.
286 /// ExecutionContext objects should be used as short lived objects (typically
287 /// on the stack) in order to lock down an execution context for local use and
288 /// for passing down to other functions that also require specific contexts.
289 /// They should NOT be used for long term storage, for long term storage use
290 /// ExecutionContextRef objects.
291 class ExecutionContext {
293 /// Default Constructor.
297 ExecutionContext(const ExecutionContext &rhs);
299 // Adopt the target and optionally its current context.
300 ExecutionContext(Target *t, bool fill_current_process_thread_frame = true);
302 // Create execution contexts from shared pointers
303 ExecutionContext(const lldb::TargetSP &target_sp, bool get_process);
304 ExecutionContext(const lldb::ProcessSP &process_sp);
305 ExecutionContext(const lldb::ThreadSP &thread_sp);
306 ExecutionContext(const lldb::StackFrameSP &frame_sp);
308 // Create execution contexts from weak pointers
309 ExecutionContext(const lldb::TargetWP &target_wp, bool get_process);
310 ExecutionContext(const lldb::ProcessWP &process_wp);
311 ExecutionContext(const lldb::ThreadWP &thread_wp);
312 ExecutionContext(const lldb::StackFrameWP &frame_wp);
313 ExecutionContext(const ExecutionContextRef &exe_ctx_ref);
314 ExecutionContext(const ExecutionContextRef *exe_ctx_ref,
315 bool thread_and_frame_only_if_stopped = false);
317 // These two variants take in a locker, and grab the target, lock the API
318 // mutex into locker, then fill in the rest of the shared pointers.
319 ExecutionContext(const ExecutionContextRef &exe_ctx_ref,
320 std::unique_lock<std::recursive_mutex> &locker);
321 ExecutionContext(const ExecutionContextRef *exe_ctx_ref,
322 std::unique_lock<std::recursive_mutex> &locker);
323 // Create execution contexts from execution context scopes
324 ExecutionContext(ExecutionContextScope *exe_scope);
325 ExecutionContext(ExecutionContextScope &exe_scope);
327 /// Construct with process, thread, and frame index.
329 /// Initialize with process \a p, thread \a t, and frame index \a f.
331 /// \param[in] process
332 /// The process for this execution context.
334 /// \param[in] thread
335 /// The thread for this execution context.
338 /// The frame index for this execution context.
339 ExecutionContext(Process *process, Thread *thread = nullptr,
340 StackFrame *frame = nullptr);
344 ExecutionContext &operator=(const ExecutionContext &rhs);
346 bool operator==(const ExecutionContext &rhs) const;
348 bool operator!=(const ExecutionContext &rhs) const;
350 /// Clear the object's state.
352 /// Sets the process and thread to nullptr, and the frame index to an
356 RegisterContext *GetRegisterContext() const;
358 ExecutionContextScope *GetBestExecutionContextScope() const;
360 uint32_t GetAddressByteSize() const;
362 lldb::ByteOrder GetByteOrder() const;
364 /// Returns a pointer to the target object.
366 /// The returned pointer might be nullptr. Calling HasTargetScope(),
367 /// HasProcessScope(), HasThreadScope(), or HasFrameScope() can help to pre-
368 /// validate this pointer so that this accessor can freely be used without
369 /// having to check for nullptr each time.
371 /// \see ExecutionContext::HasTargetScope() const @see
372 /// ExecutionContext::HasProcessScope() const @see
373 /// ExecutionContext::HasThreadScope() const @see
374 /// ExecutionContext::HasFrameScope() const
375 Target *GetTargetPtr() const;
377 /// Returns a pointer to the process object.
379 /// The returned pointer might be nullptr. Calling HasProcessScope(),
380 /// HasThreadScope(), or HasFrameScope() can help to pre-validate this
381 /// pointer so that this accessor can freely be used without having to check
382 /// for nullptr each time.
384 /// \see ExecutionContext::HasProcessScope() const @see
385 /// ExecutionContext::HasThreadScope() const @see
386 /// ExecutionContext::HasFrameScope() const
387 Process *GetProcessPtr() const;
389 /// Returns a pointer to the thread object.
391 /// The returned pointer might be nullptr. Calling HasThreadScope() or
392 /// HasFrameScope() can help to pre-validate this pointer so that this
393 /// accessor can freely be used without having to check for nullptr each
396 /// \see ExecutionContext::HasThreadScope() const @see
397 /// ExecutionContext::HasFrameScope() const
398 Thread *GetThreadPtr() const { return m_thread_sp.get(); }
400 /// Returns a pointer to the frame object.
402 /// The returned pointer might be nullptr. Calling HasFrameScope(), can help
403 /// to pre-validate this pointer so that this accessor can freely be used
404 /// without having to check for nullptr each time.
406 /// \see ExecutionContext::HasFrameScope() const
407 StackFrame *GetFramePtr() const { return m_frame_sp.get(); }
409 /// Returns a reference to the target object.
411 /// Clients should call HasTargetScope(), HasProcessScope(),
412 /// HasThreadScope(), or HasFrameScope() prior to calling this function to
413 /// ensure that this ExecutionContext object contains a valid target.
415 /// \see ExecutionContext::HasTargetScope() const @see
416 /// ExecutionContext::HasProcessScope() const @see
417 /// ExecutionContext::HasThreadScope() const @see
418 /// ExecutionContext::HasFrameScope() const
419 Target &GetTargetRef() const;
421 /// Returns a reference to the process object.
423 /// Clients should call HasProcessScope(), HasThreadScope(), or
424 /// HasFrameScope() prior to calling this function to ensure that this
425 /// ExecutionContext object contains a valid target.
427 /// \see ExecutionContext::HasProcessScope() const @see
428 /// ExecutionContext::HasThreadScope() const @see
429 /// ExecutionContext::HasFrameScope() const
430 Process &GetProcessRef() const;
432 /// Returns a reference to the thread object.
434 /// Clients should call HasThreadScope(), or HasFrameScope() prior to
435 /// calling this function to ensure that this ExecutionContext object
436 /// contains a valid target.
438 /// \see ExecutionContext::HasThreadScope() const @see
439 /// ExecutionContext::HasFrameScope() const
440 Thread &GetThreadRef() const;
442 /// Returns a reference to the thread object.
444 /// Clients should call HasFrameScope() prior to calling this function to
445 /// ensure that this ExecutionContext object contains a valid target.
447 /// \see ExecutionContext::HasFrameScope() const
448 StackFrame &GetFrameRef() const;
450 /// Get accessor to get the target shared pointer.
452 /// The returned shared pointer is not guaranteed to be valid.
453 const lldb::TargetSP &GetTargetSP() const { return m_target_sp; }
455 /// Get accessor to get the process shared pointer.
457 /// The returned shared pointer is not guaranteed to be valid.
458 const lldb::ProcessSP &GetProcessSP() const { return m_process_sp; }
460 /// Get accessor to get the thread shared pointer.
462 /// The returned shared pointer is not guaranteed to be valid.
463 const lldb::ThreadSP &GetThreadSP() const { return m_thread_sp; }
465 /// Get accessor to get the frame shared pointer.
467 /// The returned shared pointer is not guaranteed to be valid.
468 const lldb::StackFrameSP &GetFrameSP() const { return m_frame_sp; }
470 /// Set accessor to set only the target shared pointer.
471 void SetTargetSP(const lldb::TargetSP &target_sp);
473 /// Set accessor to set only the process shared pointer.
474 void SetProcessSP(const lldb::ProcessSP &process_sp);
476 /// Set accessor to set only the thread shared pointer.
477 void SetThreadSP(const lldb::ThreadSP &thread_sp);
479 /// Set accessor to set only the frame shared pointer.
480 void SetFrameSP(const lldb::StackFrameSP &frame_sp);
482 /// Set accessor to set only the target shared pointer from a target
484 void SetTargetPtr(Target *target);
486 /// Set accessor to set only the process shared pointer from a process
488 void SetProcessPtr(Process *process);
490 /// Set accessor to set only the thread shared pointer from a thread
492 void SetThreadPtr(Thread *thread);
494 /// Set accessor to set only the frame shared pointer from a frame pointer.
495 void SetFramePtr(StackFrame *frame);
497 // Set the execution context using a target shared pointer.
499 // If "target_sp" is valid, sets the target context to match and if
500 // "get_process" is true, sets the process shared pointer if the target
501 // currently has a process.
502 void SetContext(const lldb::TargetSP &target_sp, bool get_process);
504 // Set the execution context using a process shared pointer.
506 // If "process_sp" is valid, then set the process and target in this context.
507 // Thread and frame contexts will be cleared. If "process_sp" is not valid,
508 // all shared pointers are reset.
509 void SetContext(const lldb::ProcessSP &process_sp);
511 // Set the execution context using a thread shared pointer.
513 // If "thread_sp" is valid, then set the thread, process and target in this
514 // context. The frame context will be cleared. If "thread_sp" is not valid,
515 // all shared pointers are reset.
516 void SetContext(const lldb::ThreadSP &thread_sp);
518 // Set the execution context using a frame shared pointer.
520 // If "frame_sp" is valid, then set the frame, thread, process and target in
521 // this context If "frame_sp" is not valid, all shared pointers are reset.
522 void SetContext(const lldb::StackFrameSP &frame_sp);
524 /// Returns true the ExecutionContext object contains a valid target.
526 /// This function can be called after initializing an ExecutionContext
527 /// object, and if it returns true, calls to GetTargetPtr() and
528 /// GetTargetRef() do not need to be checked for validity.
529 bool HasTargetScope() const;
531 /// Returns true the ExecutionContext object contains a valid target and
534 /// This function can be called after initializing an ExecutionContext
535 /// object, and if it returns true, calls to GetTargetPtr() and
536 /// GetTargetRef(), GetProcessPtr(), and GetProcessRef(), do not need to be
537 /// checked for validity.
538 bool HasProcessScope() const;
540 /// Returns true the ExecutionContext object contains a valid target,
541 /// process, and thread.
543 /// This function can be called after initializing an ExecutionContext
544 /// object, and if it returns true, calls to GetTargetPtr(), GetTargetRef(),
545 /// GetProcessPtr(), GetProcessRef(), GetThreadPtr(), and GetThreadRef() do
546 /// not need to be checked for validity.
547 bool HasThreadScope() const;
549 /// Returns true the ExecutionContext object contains a valid target,
550 /// process, thread and frame.
552 /// This function can be called after initializing an ExecutionContext
553 /// object, and if it returns true, calls to GetTargetPtr(), GetTargetRef(),
554 /// GetProcessPtr(), GetProcessRef(), GetThreadPtr(), GetThreadRef(),
555 /// GetFramePtr(), and GetFrameRef() do not need to be checked for validity.
556 bool HasFrameScope() const;
560 lldb::TargetSP m_target_sp; ///< The target that owns the process/thread/frame
561 lldb::ProcessSP m_process_sp; ///< The process that owns the thread/frame
562 lldb::ThreadSP m_thread_sp; ///< The thread that owns the frame
563 lldb::StackFrameSP m_frame_sp; ///< The stack frame in thread.
566 } // namespace lldb_private
568 #endif // liblldb_ExecutionContext_h_