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:
183 return true;
184 default:
185 return false;
186 }
187}
188
189// ThreadPlanNull
190
194
196
198 s->PutCString("Null thread plan - thread has been destroyed.");
199}
200
202#ifdef LLDB_CONFIGURATION_DEBUG
203 fprintf(stderr,
204 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
205 ", ptid = 0x%" PRIx64 ")",
206 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
207#else
208 Log *log = GetLog(LLDBLog::Thread);
209 LLDB_LOGF(log,
210 "error: %s called on thread that has been destroyed "
211 "(tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
212 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
213#endif
214 return true;
215}
216
218#ifdef LLDB_CONFIGURATION_DEBUG
219 fprintf(stderr,
220 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
221 ", ptid = 0x%" PRIx64 ")",
222 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
223#else
224 Log *log = GetLog(LLDBLog::Thread);
225 LLDB_LOGF(log,
226 "error: %s called on thread that has been destroyed "
227 "(tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
228 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
229#endif
230 return true;
231}
232
234#ifdef LLDB_CONFIGURATION_DEBUG
235 fprintf(stderr,
236 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
237 ", ptid = 0x%" PRIx64 ")",
238 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
239#else
240 Log *log = GetLog(LLDBLog::Thread);
241 LLDB_LOGF(log,
242 "error: %s called on thread that has been destroyed "
243 "(tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
244 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
245#endif
246 return true;
247}
248
250#ifdef LLDB_CONFIGURATION_DEBUG
251 fprintf(stderr,
252 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
253 ", ptid = 0x%" PRIx64 ")",
254 LLVM_PRETTY_FUNCTION, GetThread().GetID(), GetThread().GetProtocolID());
255#else
256 Log *log = GetLog(LLDBLog::Thread);
257 LLDB_LOGF(log,
258 "error: %s called on thread that has been destroyed "
259 "(tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
260 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
261#endif
262 return true;
263}
264
265// The null plan is never done.
267// The null plan is never done.
268#ifdef LLDB_CONFIGURATION_DEBUG
269 fprintf(stderr,
270 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
271 ", ptid = 0x%" PRIx64 ")",
272 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
273#else
274 Log *log = GetLog(LLDBLog::Thread);
275 LLDB_LOGF(log,
276 "error: %s called on thread that has been destroyed "
277 "(tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
278 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
279#endif
280 return false;
281}
282
284// Not sure what to return here. This is a dead thread.
285#ifdef LLDB_CONFIGURATION_DEBUG
286 fprintf(stderr,
287 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
288 ", ptid = 0x%" PRIx64 ")",
289 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
290#else
291 Log *log = GetLog(LLDBLog::Thread);
292 LLDB_LOGF(log,
293 "error: %s called on thread that has been destroyed "
294 "(tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
295 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
296#endif
297 return eStateRunning;
298}
299
300llvm::Expected<std::vector<addr_t>> lldb_private::GetStepUntilAddresses(
301 StackFrame &frame, const FileSpec &file, llvm::ArrayRef<uint32_t> lines,
302 llvm::ArrayRef<addr_t> requested_addresses) {
303 const SymbolContext &frame_sc =
304 frame.GetSymbolContext(eSymbolContextCompUnit | eSymbolContextFunction);
305 TargetSP target_sp = frame.CalculateTarget();
306 if (!target_sp)
307 return llvm::createStringError("null target from StepUntil frame");
308 if (!frame_sc.comp_unit || !frame_sc.function)
309 return llvm::createStringError("frame has no function debug information");
310
311 std::vector<addr_t> until_addrs;
312 auto add_if_in_scope = [&](const Address &addr) {
313 addr_t load_addr = addr.GetLoadAddress(target_sp.get());
314 if (load_addr != LLDB_INVALID_ADDRESS && frame.IsAddressInFrameScope(addr))
315 until_addrs.push_back(load_addr);
316 };
317
318 bool found_some_line_table_entry = false;
319 for (uint32_t line : lines) {
320 LineEntry line_entry;
321 uint32_t idx = frame_sc.comp_unit->FindLineEntry(
322 0, line, &file, /*exact=*/false, &line_entry);
323 if (idx == UINT32_MAX)
324 continue;
325 found_some_line_table_entry = true;
326 const uint32_t actual_line = line_entry.line;
327 while (idx != UINT32_MAX) {
328 add_if_in_scope(line_entry.range.GetBaseAddress());
329 idx = frame_sc.comp_unit->FindLineEntry(idx + 1, actual_line, &file,
330 /*exact=*/true, &line_entry);
331 }
332 }
333
334 for (addr_t address : requested_addresses) {
335 Address addr;
336 if (target_sp->ResolveLoadAddress(address, addr))
337 add_if_in_scope(addr);
338 }
339
340 if (!until_addrs.empty())
341 return until_addrs;
342 // Historically, LLDB has emphasized the error when "only lines were
343 // requested, but none of those lines had a line table entry".
344 if (requested_addresses.empty() && !found_some_line_table_entry)
345 return llvm::createStringError("No line entries matching until target");
346 return llvm::createStringError(
347 "Until target outside of the current function");
348}
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
@ 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