LLDB mainline
ThreadPlan.cpp
Go to the documentation of this file.
1//===-- ThreadPlan.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
10#include "lldb/Core/Debugger.h"
13#include "lldb/Target/Process.h"
16#include "lldb/Target/Target.h"
17#include "lldb/Target/Thread.h"
19#include "lldb/Utility/Log.h"
20#include "lldb/Utility/State.h"
21
22#include <atomic>
23
24using namespace lldb;
25using namespace lldb_private;
26
27// ThreadPlan constructor
28ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread,
29 Vote report_stop_vote, Vote report_run_vote)
30 : m_process(*thread.GetProcess().get()), m_tid(thread.GetID()),
31 m_report_stop_vote(report_stop_vote), m_report_run_vote(report_run_vote),
33 m_thread(&thread), m_kind(kind), m_name(name), m_plan_complete_mutex(),
38}
39
40// Destructor
41ThreadPlan::~ThreadPlan() = default;
42
43Target &ThreadPlan::GetTarget() { return m_process.GetTarget(); }
44
45const Target &ThreadPlan::GetTarget() const { return m_process.GetTarget(); }
46
48 if (m_thread)
49 return *m_thread;
50
51 ThreadSP thread_sp = m_process.GetThreadList().FindThreadByID(m_tid);
52 m_thread = thread_sp.get();
53 return *m_thread;
54}
55
58 bool actual_value = DoPlanExplainsStop(event_ptr);
59 CachePlanExplainsStop(actual_value);
60 return actual_value;
61 } else {
63 }
64}
65
67 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
68 return m_plan_complete;
69}
70
71void ThreadPlan::SetPlanComplete(bool success) {
72 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
73 m_plan_complete = true;
74 m_plan_succeeded = success;
75}
76
78 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
79 // Mark the plan is complete, but don't override the success flag.
80 m_plan_complete = true;
81 return true;
82}
83
85 Log *log = GetLog(LLDBLog::Step);
86
88 ThreadPlan *prev_plan = GetPreviousPlan();
89 if (prev_plan) {
90 Vote prev_vote = prev_plan->ShouldReportStop(event_ptr);
91 LLDB_LOG(log, "returning previous thread plan vote: {0}", prev_vote);
92 return prev_vote;
93 }
94 }
95 LLDB_LOG(log, "Returning vote: {0}", m_report_stop_vote);
96 return m_report_stop_vote;
97}
98
101 ThreadPlan *prev_plan = GetPreviousPlan();
102 if (prev_plan)
103 return prev_plan->ShouldReportRun(event_ptr);
104 }
105 return m_report_run_vote;
106}
107
109
111 ThreadPlan *prev_plan;
112 prev_plan = GetPreviousPlan();
113 return (prev_plan == nullptr) ? false : prev_plan->StopOthers();
114}
115
116void ThreadPlan::SetStopOthers(bool new_value) {
117 // SetStopOthers doesn't work up the hierarchy. You have to set the explicit
118 // ThreadPlan you want to affect.
119}
120
121bool ThreadPlan::WillResume(StateType resume_state, bool current_plan) {
123
124 if (current_plan) {
125 Log *log = GetLog(LLDBLog::Step);
126
127 if (log) {
128 RegisterContext *reg_ctx = GetThread().GetRegisterContext().get();
129 assert(reg_ctx);
130 addr_t pc = reg_ctx->GetPC();
131 addr_t sp = reg_ctx->GetSP();
132 addr_t fp = reg_ctx->GetFP();
133 LLDB_LOGF(
134 log,
135 "%s Thread #%u (0x%p): tid = 0x%4.4" PRIx64 ", pc = 0x%8.8" PRIx64
136 ", sp = 0x%8.8" PRIx64 ", fp = 0x%8.8" PRIx64 ", "
137 "plan = '%s', state = %s, stop others = %d",
138 __FUNCTION__, GetThread().GetIndexID(),
139 static_cast<void *>(&GetThread()), m_tid, static_cast<uint64_t>(pc),
140 static_cast<uint64_t>(sp), static_cast<uint64_t>(fp), m_name.c_str(),
141 StateAsCString(resume_state), StopOthers());
142 }
143 }
144 bool success = DoWillResume(resume_state, current_plan);
145 ClearThreadCache(); // We don't cache the thread pointer over resumes. This
146 // Thread might go away, and another Thread represent
147 // the same underlying object on a later stop.
148 return success;
149}
150
152 static std::atomic<uint32_t> g_nextPlanID{0};
153 return ++g_nextPlanID;
154}
155
157
159
163
165 if (m_tracer_sp && m_tracer_sp->TracingEnabled())
166 return eStateStepping;
167 else
168 return GetPlanRunState();
169}
170
172 switch (reason) {
176 case eStopReasonExec:
179 case eStopReasonFork:
180 case eStopReasonVFork:
184 return true;
185 default:
186 return false;
187 }
188}
189
190// ThreadPlanNull
191
195
197
199 s->PutCString("Null thread plan - thread has been destroyed.");
200}
201
203#ifdef LLDB_CONFIGURATION_DEBUG
204 fprintf(stderr,
205 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
206 ", ptid = 0x%" PRIx64 ")",
207 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
208#else
209 Log *log = GetLog(LLDBLog::Thread);
210 LLDB_LOGF(log,
211 "error: %s called on thread that has been destroyed "
212 "(tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
213 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
214#endif
215 return true;
216}
217
219#ifdef LLDB_CONFIGURATION_DEBUG
220 fprintf(stderr,
221 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
222 ", ptid = 0x%" PRIx64 ")",
223 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
224#else
225 Log *log = GetLog(LLDBLog::Thread);
226 LLDB_LOGF(log,
227 "error: %s called on thread that has been destroyed "
228 "(tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
229 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
230#endif
231 return true;
232}
233
235#ifdef LLDB_CONFIGURATION_DEBUG
236 fprintf(stderr,
237 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
238 ", ptid = 0x%" PRIx64 ")",
239 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
240#else
241 Log *log = GetLog(LLDBLog::Thread);
242 LLDB_LOGF(log,
243 "error: %s called on thread that has been destroyed "
244 "(tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
245 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
246#endif
247 return true;
248}
249
251#ifdef LLDB_CONFIGURATION_DEBUG
252 fprintf(stderr,
253 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
254 ", ptid = 0x%" PRIx64 ")",
255 LLVM_PRETTY_FUNCTION, GetThread().GetID(), GetThread().GetProtocolID());
256#else
257 Log *log = GetLog(LLDBLog::Thread);
258 LLDB_LOGF(log,
259 "error: %s called on thread that has been destroyed "
260 "(tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
261 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
262#endif
263 return true;
264}
265
266// The null plan is never done.
268// The null plan is never done.
269#ifdef LLDB_CONFIGURATION_DEBUG
270 fprintf(stderr,
271 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
272 ", ptid = 0x%" PRIx64 ")",
273 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
274#else
275 Log *log = GetLog(LLDBLog::Thread);
276 LLDB_LOGF(log,
277 "error: %s called on thread that has been destroyed "
278 "(tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
279 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
280#endif
281 return false;
282}
283
285// Not sure what to return here. This is a dead thread.
286#ifdef LLDB_CONFIGURATION_DEBUG
287 fprintf(stderr,
288 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
289 ", ptid = 0x%" PRIx64 ")",
290 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
291#else
292 Log *log = GetLog(LLDBLog::Thread);
293 LLDB_LOGF(log,
294 "error: %s called on thread that has been destroyed "
295 "(tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
296 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
297#endif
298 return eStateRunning;
299}
300
301llvm::Expected<std::vector<addr_t>> lldb_private::GetStepUntilAddresses(
302 StackFrame &frame, const FileSpec &file, llvm::ArrayRef<uint32_t> lines,
303 llvm::ArrayRef<addr_t> requested_addresses) {
304 const SymbolContext &frame_sc =
305 frame.GetSymbolContext(eSymbolContextCompUnit | eSymbolContextFunction);
306 TargetSP target_sp = frame.CalculateTarget();
307 if (!target_sp)
308 return llvm::createStringError("null target from StepUntil frame");
309 if (!frame_sc.comp_unit || !frame_sc.function)
310 return llvm::createStringError("frame has no function debug information");
311
312 std::vector<addr_t> until_addrs;
313 auto add_if_in_scope = [&](const Address &addr) {
314 addr_t load_addr = addr.GetLoadAddress(target_sp.get());
315 if (load_addr != LLDB_INVALID_ADDRESS && frame.IsAddressInFrameScope(addr))
316 until_addrs.push_back(load_addr);
317 };
318
319 bool found_some_line_table_entry = false;
320 for (uint32_t line : lines) {
321 LineEntry line_entry;
322 uint32_t idx = frame_sc.comp_unit->FindLineEntry(
323 0, line, &file, /*exact=*/false, &line_entry);
324 if (idx == UINT32_MAX)
325 continue;
326 found_some_line_table_entry = true;
327 const uint32_t actual_line = line_entry.line;
328 while (idx != UINT32_MAX) {
329 add_if_in_scope(line_entry.range.GetBaseAddress());
330 idx = frame_sc.comp_unit->FindLineEntry(idx + 1, actual_line, &file,
331 /*exact=*/true, &line_entry);
332 }
333 }
334
335 for (addr_t address : requested_addresses) {
336 Address addr;
337 if (target_sp->ResolveLoadAddress(address, addr))
338 add_if_in_scope(addr);
339 }
340
341 if (!until_addrs.empty())
342 return until_addrs;
343 // Historically, LLDB has emphasized the error when "only lines were
344 // requested, but none of those lines had a line table entry".
345 if (requested_addresses.empty() && !found_some_line_table_entry)
346 return llvm::createStringError("No line entries matching until target");
347 return llvm::createStringError(
348 "Until target outside of the current function");
349}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:375
#define LLDB_LOGF(log,...)
Definition Log.h:389
Address & GetBaseAddress()
Get accessor for the base address of the range.
A section + offset based address class.
Definition Address.h:62
uint32_t FindLineEntry(uint32_t start_idx, uint32_t line, const FileSpec *file_spec_ptr, bool exact, LineEntry *line_entry)
Find the line entry by line and optional inlined file spec.
A file utility class.
Definition FileSpec.h:56
uint64_t GetPC(uint64_t fail_value=LLDB_INVALID_ADDRESS)
uint64_t GetSP(uint64_t fail_value=LLDB_INVALID_ADDRESS)
uint64_t GetFP(uint64_t fail_value=LLDB_INVALID_ADDRESS)
This base class provides an interface to stack frames.
Definition StackFrame.h:44
bool IsAddressInFrameScope(const Address &addr)
Let F be this frame's function.
virtual const SymbolContext & GetSymbolContext(lldb::SymbolContextItem resolve_scope)
Provide a SymbolContext for this StackFrame's current pc value.
lldb::TargetSP CalculateTarget() override
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
Defines a symbol context baton that can be handed other debug core functions.
Function * function
The Function for a given query.
CompileUnit * comp_unit
The CompileUnit for a given query.
bool MischiefManaged() override
lldb::StateType GetPlanRunState() override
bool DoPlanExplainsStop(Event *event_ptr) override
void GetDescription(Stream *s, lldb::DescriptionLevel level) override
Print a description of this thread to the stream s.
bool ValidatePlan(Stream *error) override
Returns whether this plan could be successfully created.
bool ShouldStop(Event *event_ptr) override
virtual Vote ShouldReportStop(Event *event_ptr)
ThreadPlanKind m_kind
Definition ThreadPlan.h:578
virtual bool DoPlanExplainsStop(Event *event_ptr)=0
virtual bool OkayToDiscard()
ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread, Vote report_stop_vote, Vote report_run_vote)
virtual bool DoWillResume(lldb::StateType resume_state, bool current_plan)
Definition ThreadPlan.h:524
virtual lldb::StateType GetPlanRunState()=0
Vote ShouldReportRun(Event *event_ptr)
bool IsUsuallyUnexplainedStopReason(lldb::StopReason)
LazyBool m_cached_plan_explains_stop
Definition ThreadPlan.h:581
void SetPlanComplete(bool success=true)
Thread & GetThread()
Returns the Thread that is using this thread plan.
bool PlanExplainsStop(Event *event_ptr)
virtual bool StopOthers()
static lldb::user_id_t GetNextID()
ThreadPlan * GetPreviousPlan()
Definition ThreadPlan.h:543
void ClearThreadCache()
Clear the Thread* cache.
bool WillResume(lldb::StateType resume_state, bool current_plan)
virtual bool MischiefManaged()
lldb::ThreadPlanTracerSP m_tracer_sp
Definition ThreadPlan.h:588
lldb::StateType RunState()
virtual void SetStopOthers(bool new_value)
void CachePlanExplainsStop(bool does_explain)
Definition ThreadPlan.h:568
std::recursive_mutex m_plan_complete_mutex
Definition ThreadPlan.h:580
virtual lldb::RegisterContextSP GetRegisterContext()=0
#define LLDB_INVALID_ADDRESS
#define UINT32_MAX
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
llvm::Expected< std::vector< lldb::addr_t > > GetStepUntilAddresses(StackFrame &frame, const FileSpec &file, llvm::ArrayRef< uint32_t > lines, llvm::ArrayRef< lldb::addr_t > requested_addresses)
Returns the load addresses of the line table entries for lines in file, and of addresses,...
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
Definition State.cpp:14
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
std::shared_ptr< lldb_private::Thread > ThreadSP
StateType
Process and Thread States.
@ eStateRunning
Process or thread is running and can't be examined.
@ eStateStepping
Process or thread is in the process of stepping and can not be examined.
uint64_t user_id_t
Definition lldb-types.h:83
uint64_t addr_t
Definition lldb-types.h:80
StopReason
Thread stop reasons.
@ eStopReasonInstrumentation
@ eStopReasonDynamicLoader
Indicates that execution stopped so a dynamic-loader plugin can update the loaded libraries.
@ eStopReasonExec
Program was re-exec'ed.
@ eStopReasonVForkDone
@ eStopReasonInterrupt
Thread requested interrupt.
@ eStopReasonThreadExiting
@ eStopReasonException
@ eStopReasonWatchpoint
std::shared_ptr< lldb_private::Target > TargetSP
A line table entry class.
Definition LineEntry.h:21
AddressRange range
The section offset address range for this line entry.
Definition LineEntry.h:137
uint32_t line
The source line number, or LLDB_INVALID_LINE_NUMBER if there is no line number information.
Definition LineEntry.h:151
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition UserID.h:47
void SetID(lldb::user_id_t uid)
Set accessor for the user ID.
Definition UserID.h:53