LLDB mainline
ExecutionContext.cpp
Go to the documentation of this file.
1//===-- ExecutionContext.cpp ----------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
11#include "lldb/Target/Process.h"
13#include "lldb/Target/Target.h"
14#include "lldb/Target/Thread.h"
15#include "lldb/Utility/State.h"
16
17using namespace lldb_private;
18
20 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {}
21
23
25 bool get_process)
26 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
27 if (target_sp)
28 SetContext(target_sp, get_process);
29}
30
32 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
33 if (process_sp)
34 SetContext(process_sp);
35}
36
38 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
39 if (thread_sp)
40 SetContext(thread_sp);
41}
42
44 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
45 if (frame_sp)
46 SetContext(frame_sp);
47}
48
50 bool get_process)
51 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
52 lldb::TargetSP target_sp(target_wp.lock());
53 if (target_sp)
54 SetContext(target_sp, get_process);
55}
56
58 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
59 lldb::ProcessSP process_sp(process_wp.lock());
60 if (process_sp)
61 SetContext(process_sp);
62}
63
65 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
66 lldb::ThreadSP thread_sp(thread_wp.lock());
67 if (thread_sp)
68 SetContext(thread_sp);
69}
70
72 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
73 lldb::StackFrameSP frame_sp(frame_wp.lock());
74 if (frame_sp)
75 SetContext(frame_sp);
76}
77
79 bool fill_current_process_thread_frame)
80 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
81 if (t) {
82 m_target_sp = t->shared_from_this();
83 if (fill_current_process_thread_frame) {
85 if (m_process_sp) {
86 m_thread_sp = m_process_sp->GetThreadList().GetSelectedThread();
87 if (m_thread_sp)
90 }
91 }
92 }
93}
94
96 StackFrame *frame)
97 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
98 if (process) {
99 m_process_sp = process->shared_from_this();
100 m_target_sp = process->GetTarget().shared_from_this();
101 }
102 if (thread)
103 m_thread_sp = thread->shared_from_this();
104 if (frame)
105 m_frame_sp = frame->shared_from_this();
106}
107
109 : m_target_sp(exe_ctx_ref.GetTargetSP()),
110 m_process_sp(exe_ctx_ref.GetProcessSP()),
111 m_thread_sp(exe_ctx_ref.GetThreadSP()),
112 m_frame_sp(exe_ctx_ref.GetFrameSP()) {}
113
115 bool thread_and_frame_only_if_stopped)
116 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
117 if (exe_ctx_ref_ptr) {
118 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
119 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
120 if (!thread_and_frame_only_if_stopped ||
121 (m_process_sp && StateIsStoppedState(m_process_sp->GetState(), true))) {
122 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
123 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
124 }
125 }
126}
127
129 std::unique_lock<std::recursive_mutex> &lock)
130 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
131 if (exe_ctx_ref_ptr) {
132 m_target_sp = exe_ctx_ref_ptr->GetTargetSP();
133 if (m_target_sp) {
134 lock = std::unique_lock<std::recursive_mutex>(m_target_sp->GetAPIMutex());
135
136 m_process_sp = exe_ctx_ref_ptr->GetProcessSP();
137 m_thread_sp = exe_ctx_ref_ptr->GetThreadSP();
138 m_frame_sp = exe_ctx_ref_ptr->GetFrameSP();
139 }
140 }
141}
142
144 std::unique_lock<std::recursive_mutex> &lock)
145 : m_target_sp(exe_ctx_ref.GetTargetSP()), m_process_sp(), m_thread_sp(),
146 m_frame_sp() {
147 if (m_target_sp) {
148 lock = std::unique_lock<std::recursive_mutex>(m_target_sp->GetAPIMutex());
149
150 m_process_sp = exe_ctx_ref.GetProcessSP();
151 m_thread_sp = exe_ctx_ref.GetThreadSP();
152 m_frame_sp = exe_ctx_ref.GetFrameSP();
153 }
154}
155
157 : m_target_sp(), m_process_sp(), m_thread_sp(), m_frame_sp() {
158 if (exe_scope_ptr)
159 exe_scope_ptr->CalculateExecutionContext(*this);
160}
161
163 exe_scope_ref.CalculateExecutionContext(*this);
164}
165
167 m_target_sp.reset();
168 m_process_sp.reset();
169 m_thread_sp.reset();
170 m_frame_sp.reset();
171}
172
174
176 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
177 return m_target_sp->GetArchitecture().GetAddressByteSize();
178 if (m_process_sp)
179 return m_process_sp->GetAddressByteSize();
180 return sizeof(void *);
181}
182
184 if (m_target_sp && m_target_sp->GetArchitecture().IsValid())
185 return m_target_sp->GetArchitecture().GetByteOrder();
186 if (m_process_sp)
187 return m_process_sp->GetByteOrder();
189}
190
192 if (m_frame_sp)
193 return m_frame_sp->GetRegisterContext().get();
194 else if (m_thread_sp)
195 return m_thread_sp->GetRegisterContext().get();
196 return nullptr;
197}
198
200 if (m_target_sp)
201 return m_target_sp.get();
202 if (m_process_sp)
203 return &m_process_sp->GetTarget();
204 return nullptr;
205}
206
208 if (m_process_sp)
209 return m_process_sp.get();
210 if (m_target_sp)
211 return m_target_sp->GetProcessSP().get();
212 return nullptr;
213}
214
216 if (m_frame_sp)
217 return m_frame_sp.get();
218 if (m_thread_sp)
219 return m_thread_sp.get();
220 if (m_process_sp)
221 return m_process_sp.get();
222 return m_target_sp.get();
223}
224
226 assert(m_target_sp);
227 return *m_target_sp;
228}
229
231 assert(m_process_sp);
232 return *m_process_sp;
233}
234
236 assert(m_thread_sp);
237 return *m_thread_sp;
238}
239
241 assert(m_frame_sp);
242 return *m_frame_sp;
243}
244
246 m_target_sp = target_sp;
247}
248
250 m_process_sp = process_sp;
251}
252
254 m_thread_sp = thread_sp;
255}
256
258 m_frame_sp = frame_sp;
259}
260
262 if (target)
263 m_target_sp = target->shared_from_this();
264 else
265 m_target_sp.reset();
266}
267
269 if (process)
270 m_process_sp = process->shared_from_this();
271 else
272 m_process_sp.reset();
273}
274
276 if (thread)
277 m_thread_sp = thread->shared_from_this();
278 else
279 m_thread_sp.reset();
280}
281
283 if (frame)
284 m_frame_sp = frame->shared_from_this();
285 else
286 m_frame_sp.reset();
287}
288
290 bool get_process) {
291 m_target_sp = target_sp;
292 if (get_process && target_sp)
293 m_process_sp = target_sp->GetProcessSP();
294 else
295 m_process_sp.reset();
296 m_thread_sp.reset();
297 m_frame_sp.reset();
298}
299
301 m_process_sp = process_sp;
302 if (process_sp)
303 m_target_sp = process_sp->GetTarget().shared_from_this();
304 else
305 m_target_sp.reset();
306 m_thread_sp.reset();
307 m_frame_sp.reset();
308}
309
311 m_frame_sp.reset();
312 m_thread_sp = thread_sp;
313 if (thread_sp) {
314 m_process_sp = thread_sp->GetProcess();
315 if (m_process_sp)
316 m_target_sp = m_process_sp->GetTarget().shared_from_this();
317 else
318 m_target_sp.reset();
319 } else {
320 m_target_sp.reset();
321 m_process_sp.reset();
322 }
323}
324
326 m_frame_sp = frame_sp;
327 if (frame_sp) {
328 m_thread_sp = frame_sp->CalculateThread();
329 if (m_thread_sp) {
330 m_process_sp = m_thread_sp->GetProcess();
331 if (m_process_sp)
332 m_target_sp = m_process_sp->GetTarget().shared_from_this();
333 else
334 m_target_sp.reset();
335 } else {
336 m_target_sp.reset();
337 m_process_sp.reset();
338 }
339 } else {
340 m_target_sp.reset();
341 m_process_sp.reset();
342 m_thread_sp.reset();
343 }
344}
345
347 if (this != &rhs) {
352 }
353 return *this;
354}
355
357 // Check that the frame shared pointers match, or both are valid and their
358 // stack IDs match since sometimes we get new objects that represent the same
359 // frame within a thread.
360 if ((m_frame_sp == rhs.m_frame_sp) ||
361 (m_frame_sp && rhs.m_frame_sp &&
362 m_frame_sp->GetStackID() == rhs.m_frame_sp->GetStackID())) {
363 // Check that the thread shared pointers match, or both are valid and their
364 // thread IDs match since sometimes we get new objects that represent the
365 // same thread within a process.
366 if ((m_thread_sp == rhs.m_thread_sp) ||
367 (m_thread_sp && rhs.m_thread_sp &&
368 m_thread_sp->GetID() == rhs.m_thread_sp->GetID())) {
369 // Processes and targets don't change much
370 return m_process_sp == rhs.m_process_sp && m_target_sp == rhs.m_target_sp;
371 }
372 }
373 return false;
374}
375
377 return !(*this == rhs);
378}
379
381 return ((bool)m_target_sp && m_target_sp->IsValid());
382}
383
385 return (HasTargetScope() && ((bool)m_process_sp && m_process_sp->IsValid()));
386}
387
389 return (HasProcessScope() && ((bool)m_thread_sp && m_thread_sp->IsValid()));
390}
391
393 return HasThreadScope() && m_frame_sp;
394}
395
397 : m_target_wp(), m_process_wp(), m_thread_wp(), m_stack_id() {}
398
400 : m_target_wp(), m_process_wp(), m_thread_wp(), m_stack_id() {
401 if (exe_ctx)
402 *this = *exe_ctx;
403}
404
406 : m_target_wp(), m_process_wp(), m_thread_wp(), m_stack_id() {
407 *this = exe_ctx;
408}
409
411 : m_target_wp(), m_process_wp(), m_thread_wp(), m_stack_id() {
412 SetTargetPtr(target, adopt_selected);
413}
414
416
417 = default;
418
420operator=(const ExecutionContextRef &rhs) {
421 if (this != &rhs) {
425 m_tid = rhs.m_tid;
427 }
428 return *this;
429}
430
432operator=(const ExecutionContext &exe_ctx) {
433 m_target_wp = exe_ctx.GetTargetSP();
434 m_process_wp = exe_ctx.GetProcessSP();
435 lldb::ThreadSP thread_sp(exe_ctx.GetThreadSP());
436 m_thread_wp = thread_sp;
437 if (thread_sp)
438 m_tid = thread_sp->GetID();
439 else
441 lldb::StackFrameSP frame_sp(exe_ctx.GetFrameSP());
442 if (frame_sp)
443 m_stack_id = frame_sp->GetStackID();
444 else
446 return *this;
447}
448
450 m_target_wp.reset();
451 m_process_wp.reset();
452 ClearThread();
453 ClearFrame();
454}
455
457
459 m_target_wp = target_sp;
460}
461
463 if (process_sp) {
464 m_process_wp = process_sp;
465 SetTargetSP(process_sp->GetTarget().shared_from_this());
466 } else {
467 m_process_wp.reset();
468 m_target_wp.reset();
469 }
470}
471
473 if (thread_sp) {
474 m_thread_wp = thread_sp;
475 m_tid = thread_sp->GetID();
476 SetProcessSP(thread_sp->GetProcess());
477 } else {
478 ClearThread();
479 m_process_wp.reset();
480 m_target_wp.reset();
481 }
482}
483
485 if (frame_sp) {
486 m_stack_id = frame_sp->GetStackID();
487 SetThreadSP(frame_sp->GetThread());
488 } else {
489 ClearFrame();
490 ClearThread();
491 m_process_wp.reset();
492 m_target_wp.reset();
493 }
494}
495
496void ExecutionContextRef::SetTargetPtr(Target *target, bool adopt_selected) {
497 Clear();
498 if (target) {
499 lldb::TargetSP target_sp(target->shared_from_this());
500 if (target_sp) {
501 m_target_wp = target_sp;
502 if (adopt_selected) {
503 lldb::ProcessSP process_sp(target_sp->GetProcessSP());
504 if (process_sp) {
505 m_process_wp = process_sp;
506 if (process_sp) {
507 // Only fill in the thread and frame if our process is stopped
508 // Don't just check the state, since we might be in the middle of
509 // resuming.
510 Process::StopLocker stop_locker;
511
512 if (stop_locker.TryLock(&process_sp->GetRunLock()) &&
513 StateIsStoppedState(process_sp->GetState(), true)) {
514 lldb::ThreadSP thread_sp(
515 process_sp->GetThreadList().GetSelectedThread());
516 if (!thread_sp)
517 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(0);
518
519 if (thread_sp) {
520 SetThreadSP(thread_sp);
521 lldb::StackFrameSP frame_sp(
522 thread_sp->GetSelectedFrame(DoNoSelectMostRelevantFrame));
523 if (!frame_sp)
524 frame_sp = thread_sp->GetStackFrameAtIndex(0);
525 if (frame_sp)
526 SetFrameSP(frame_sp);
527 }
528 }
529 }
530 }
531 }
532 }
533 }
534}
535
537 if (process) {
538 SetProcessSP(process->shared_from_this());
539 } else {
540 m_process_wp.reset();
541 m_target_wp.reset();
542 }
543}
544
546 if (thread) {
547 SetThreadSP(thread->shared_from_this());
548 } else {
549 ClearThread();
550 m_process_wp.reset();
551 m_target_wp.reset();
552 }
553}
554
556 if (frame)
557 SetFrameSP(frame->shared_from_this());
558 else
559 Clear();
560}
561
563 lldb::TargetSP target_sp(m_target_wp.lock());
564 if (target_sp && !target_sp->IsValid())
565 target_sp.reset();
566 return target_sp;
567}
568
570 lldb::ProcessSP process_sp(m_process_wp.lock());
571 if (process_sp && !process_sp->IsValid())
572 process_sp.reset();
573 return process_sp;
574}
575
577 lldb::ThreadSP thread_sp(m_thread_wp.lock());
578
580 // We check if the thread has been destroyed in cases where clients might
581 // still have shared pointer to a thread, but the thread is not valid
582 // anymore (not part of the process)
583 if (!thread_sp || !thread_sp->IsValid()) {
584 lldb::ProcessSP process_sp(GetProcessSP());
585 if (process_sp && process_sp->IsValid()) {
586 thread_sp = process_sp->GetThreadList().FindThreadByID(m_tid);
587 m_thread_wp = thread_sp;
588 }
589 }
590 }
591
592 // Check that we aren't about to return an invalid thread sp. We might
593 // return a nullptr thread_sp, but don't return an invalid one.
594
595 if (thread_sp && !thread_sp->IsValid())
596 thread_sp.reset();
597
598 return thread_sp;
599}
600
602 if (m_stack_id.IsValid()) {
603 lldb::ThreadSP thread_sp(GetThreadSP());
604 if (thread_sp)
605 return thread_sp->GetFrameWithStackID(m_stack_id);
606 }
607 return lldb::StackFrameSP();
608}
609
611ExecutionContextRef::Lock(bool thread_and_frame_only_if_stopped) const {
612 return ExecutionContext(this, thread_and_frame_only_if_stopped);
613}
Execution context objects refer to objects in the execution of the program that is being debugged.
StackID m_stack_id
The stack ID that this object refers to in case the backing object changes.
ExecutionContext Lock(bool thread_and_frame_only_if_stopped) const
Create an ExecutionContext object from this object.
void SetThreadSP(const lldb::ThreadSP &thread_sp)
Set accessor that creates a weak reference to the thread referenced in thread_sp.
void Clear()
Clear the object's state.
lldb::StackFrameSP GetFrameSP() const
Get accessor that creates a strong reference from the weak frame reference contained in this object.
lldb::ProcessWP m_process_wp
A weak reference to a process.
lldb::ThreadSP GetThreadSP() const
Get accessor that creates a strong reference from the weak thread reference contained in this object.
void SetFrameSP(const lldb::StackFrameSP &frame_sp)
Set accessor that creates a weak reference to the frame referenced in frame_sp.
lldb::tid_t m_tid
The thread ID that this object refers to in case the backing object changes.
lldb::TargetSP GetTargetSP() const
Get accessor that creates a strong reference from the weak target reference contained in this object.
void SetTargetPtr(Target *target, bool adopt_selected)
lldb::ProcessSP GetProcessSP() const
Get accessor that creates a strong reference from the weak process reference contained in this object...
ExecutionContextRef & operator=(const ExecutionContextRef &rhs)
Assignment operator.
void SetProcessPtr(Process *process)
void SetFramePtr(StackFrame *frame)
ExecutionContextRef()
Default Constructor.
void SetTargetSP(const lldb::TargetSP &target_sp)
Set accessor that creates a weak reference to the target referenced in target_sp.
lldb::TargetWP m_target_wp
A weak reference to a target.
void SetProcessSP(const lldb::ProcessSP &process_sp)
Set accessor that creates a weak reference to the process referenced in process_sp.
lldb::ThreadWP m_thread_wp
A weak reference to a thread.
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
virtual void CalculateExecutionContext(ExecutionContext &exe_ctx)=0
Reconstruct the object's execution context into sc.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
void SetFrameSP(const lldb::StackFrameSP &frame_sp)
Set accessor to set only the frame shared pointer.
bool HasThreadScope() const
Returns true the ExecutionContext object contains a valid target, process, and thread.
void SetProcessPtr(Process *process)
Set accessor to set only the process shared pointer from a process pointer.
bool HasProcessScope() const
Returns true the ExecutionContext object contains a valid target and process.
ExecutionContextScope * GetBestExecutionContextScope() const
const lldb::TargetSP & GetTargetSP() const
Get accessor to get the target shared pointer.
void Clear()
Clear the object's state.
ExecutionContext()
Default Constructor.
const lldb::ProcessSP & GetProcessSP() const
Get accessor to get the process shared pointer.
void SetProcessSP(const lldb::ProcessSP &process_sp)
Set accessor to set only the process shared pointer.
lldb::StackFrameSP m_frame_sp
The stack frame in thread.
bool operator==(const ExecutionContext &rhs) const
void SetThreadPtr(Thread *thread)
Set accessor to set only the thread shared pointer from a thread pointer.
lldb::ByteOrder GetByteOrder() const
void SetTargetPtr(Target *target)
Set accessor to set only the target shared pointer from a target pointer.
lldb::TargetSP m_target_sp
The target that owns the process/thread/frame.
const lldb::StackFrameSP & GetFrameSP() const
Get accessor to get the frame shared pointer.
Process & GetProcessRef() const
Returns a reference to the process object.
void SetContext(const lldb::TargetSP &target_sp, bool get_process)
void SetTargetSP(const lldb::TargetSP &target_sp)
Set accessor to set only the target shared pointer.
Target * GetTargetPtr() const
Returns a pointer to the target object.
const lldb::ThreadSP & GetThreadSP() const
Get accessor to get the thread shared pointer.
void SetThreadSP(const lldb::ThreadSP &thread_sp)
Set accessor to set only the thread shared pointer.
bool HasTargetScope() const
Returns true the ExecutionContext object contains a valid target.
StackFrame & GetFrameRef() const
Returns a reference to the thread object.
bool HasFrameScope() const
Returns true the ExecutionContext object contains a valid target, process, thread and frame.
lldb::ThreadSP m_thread_sp
The thread that owns the frame.
lldb::ProcessSP m_process_sp
The process that owns the thread/frame.
bool operator!=(const ExecutionContext &rhs) const
Target & GetTargetRef() const
Returns a reference to the target object.
void SetFramePtr(StackFrame *frame)
Set accessor to set only the frame shared pointer from a frame pointer.
ExecutionContext & operator=(const ExecutionContext &rhs)
Process * GetProcessPtr() const
Returns a pointer to the process object.
Thread & GetThreadRef() const
Returns a reference to the thread object.
RegisterContext * GetRegisterContext() const
A plug-in interface definition class for debugging a process.
Definition: Process.h:341
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1285
This base class provides an interface to stack frames.
Definition: StackFrame.h:43
bool IsValid() const
Definition: StackID.h:47
const lldb::ProcessSP & GetProcessSP() const
Definition: Target.cpp:222
#define LLDB_INVALID_THREAD_ID
Definition: lldb-defines.h:90
@ DoNoSelectMostRelevantFrame
lldb::ByteOrder InlHostByteOrder()
Definition: Endian.h:25
A class that represents a running process on the host machine.
bool StateIsStoppedState(lldb::StateType state, bool must_exist)
Check if a state represents a state where the process or thread is stopped.
Definition: State.cpp:89
std::weak_ptr< lldb_private::StackFrame > StackFrameWP
Definition: lldb-forward.h:421
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
Definition: lldb-forward.h:420
std::shared_ptr< lldb_private::Thread > ThreadSP
Definition: lldb-forward.h:446
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:387
ByteOrder
Byte ordering definitions.
std::weak_ptr< lldb_private::Process > ProcessWP
Definition: lldb-forward.h:390
std::weak_ptr< lldb_private::Target > TargetWP
Definition: lldb-forward.h:445
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:444
std::weak_ptr< lldb_private::Thread > ThreadWP
Definition: lldb-forward.h:447