LLDB mainline
ThreadPlanCallFunction.cpp
Go to the documentation of this file.
1//===-- ThreadPlanCallFunction.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
12#include "lldb/Core/Address.h"
14#include "lldb/Core/Module.h"
16#include "lldb/Target/ABI.h"
18#include "lldb/Target/Process.h"
21#include "lldb/Target/Target.h"
22#include "lldb/Target/Thread.h"
25#include "lldb/Utility/Log.h"
26#include "lldb/Utility/Stream.h"
27
28#include <memory>
29
30using namespace lldb;
31using namespace lldb_private;
32
33// ThreadPlanCallFunction: Plan to call a single function
35 Thread &thread, ABI *&abi, lldb::addr_t &start_load_addr,
36 lldb::addr_t &function_load_addr) {
38 SetOkayToDiscard(false);
39 SetPrivate(true);
40
41 ProcessSP process_sp(thread.GetProcess());
42 if (!process_sp)
43 return false;
44
45 abi = process_sp->GetABI().get();
46
47 if (!abi)
48 return false;
49
50 Log *log = GetLog(LLDBLog::Step);
51
53
54 m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
55 // If we can't read memory at the point of the process where we are planning
56 // to put our function, we're not going to get any further...
58 process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error);
59 if (!error.Success()) {
61 "Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".",
63 LLDB_LOGF(log, "ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
65 return false;
66 }
67
68 llvm::Expected<Address> start_address = GetTarget().GetEntryPointAddress();
69 if (!start_address) {
71 "%s", llvm::toString(start_address.takeError()).c_str());
72 LLDB_LOGF(log, "ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
74 return false;
75 }
76
77 m_start_addr = *start_address;
78 start_load_addr = m_start_addr.GetLoadAddress(&GetTarget());
79
80 // Checkpoint the thread state so we can restore it later.
81 if (log && log->GetVerbose())
82 ReportRegisterState("About to checkpoint thread before function call. "
83 "Original register state was:");
84
86 m_constructor_errors.Printf("Setting up ThreadPlanCallFunction, failed to "
87 "checkpoint thread state.");
88 LLDB_LOGF(log, "ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
90 return false;
91 }
92 function_load_addr = m_function_addr.GetLoadAddress(&GetTarget());
93
94 return true;
95}
96
98 Thread &thread, const Address &function, const CompilerType &return_type,
99 llvm::ArrayRef<addr_t> args, const EvaluateExpressionOptions &options)
100 : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread,
102 m_valid(false), m_stop_other_threads(options.GetStopOthers()),
103 m_unwind_on_error(options.DoesUnwindOnError()),
104 m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
105 m_debug_execution(options.GetDebug()),
106 m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function),
107 m_start_addr(), m_function_sp(0), m_subplan_sp(),
108 m_cxx_language_runtime(nullptr), m_objc_language_runtime(nullptr),
109 m_stored_thread_state(), m_real_stop_info_sp(), m_constructor_errors(),
110 m_return_valobj_sp(), m_takedown_done(false),
111 m_should_clear_objc_exception_bp(false),
112 m_should_clear_cxx_exception_bp(false),
113 m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(return_type) {
114 lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS;
115 lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS;
116 ABI *abi = nullptr;
117
118 if (!ConstructorSetup(thread, abi, start_load_addr, function_load_addr))
119 return;
120
121 if (!abi->PrepareTrivialCall(thread, m_function_sp, function_load_addr,
122 start_load_addr, args))
123 return;
124
125 ReportRegisterState("Function call was set up. Register state was:");
126
127 m_valid = true;
128}
129
131 Thread &thread, const Address &function,
132 const EvaluateExpressionOptions &options)
133 : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread,
135 m_valid(false), m_stop_other_threads(options.GetStopOthers()),
136 m_unwind_on_error(options.DoesUnwindOnError()),
137 m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
138 m_debug_execution(options.GetDebug()),
139 m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function),
140 m_start_addr(), m_function_sp(0), m_subplan_sp(),
141 m_cxx_language_runtime(nullptr), m_objc_language_runtime(nullptr),
142 m_stored_thread_state(), m_real_stop_info_sp(), m_constructor_errors(),
143 m_return_valobj_sp(), m_takedown_done(false),
144 m_should_clear_objc_exception_bp(false),
145 m_should_clear_cxx_exception_bp(false),
146 m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(CompilerType()) {}
147
150}
151
153 Log *log = GetLog(LLDBLog::Step);
154 if (log && log->GetVerbose()) {
155 StreamString strm;
156 RegisterContext *reg_ctx = GetThread().GetRegisterContext().get();
157
158 log->PutCString(message);
159
160 RegisterValue reg_value;
161
162 for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
163 reg_idx < num_registers; ++reg_idx) {
164 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_idx);
165 if (reg_ctx->ReadRegister(reg_info, reg_value)) {
166 DumpRegisterValue(reg_value, strm, *reg_info, true, false,
168 strm.EOL();
169 }
170 }
171 log->PutString(strm.GetString());
172 }
173}
174
176 Log *log = GetLog(LLDBLog::Step);
177
178 if (!m_valid) {
179 // Don't call DoTakedown if we were never valid to begin with.
180 LLDB_LOGF(log,
181 "ThreadPlanCallFunction(%p): Log called on "
182 "ThreadPlanCallFunction that was never valid.",
183 static_cast<void *>(this));
184 return;
185 }
186
187 if (!m_takedown_done) {
188 Thread &thread = GetThread();
189 if (success) {
191 }
192 LLDB_LOGF(log,
193 "ThreadPlanCallFunction(%p): DoTakedown called for thread "
194 "0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
195 static_cast<void *>(this), m_tid, m_valid, IsPlanComplete());
196 m_takedown_done = true;
198 thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
201 LLDB_LOGF(log,
202 "ThreadPlanCallFunction(%p): DoTakedown failed to restore "
203 "register state",
204 static_cast<void *>(this));
205 }
206 SetPlanComplete(success);
208 if (log && log->GetVerbose())
209 ReportRegisterState("Restoring thread state after function call. "
210 "Restored register state:");
211 } else {
212 LLDB_LOGF(log,
213 "ThreadPlanCallFunction(%p): DoTakedown called as no-op for "
214 "thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
215 static_cast<void *>(this), m_tid, m_valid, IsPlanComplete());
216 }
217}
218
220
222 if (level == eDescriptionLevelBrief) {
223 s->Printf("Function call thread plan");
224 } else {
225 s->Printf("Thread plan to call 0x%" PRIx64,
227 }
228}
229
231 if (!m_valid) {
232 if (error) {
234 error->PutCString(m_constructor_errors.GetString());
235 else
236 error->PutCString("Unknown error");
237 }
238 return false;
239 }
240
241 return true;
242}
243
246 return eVoteYes;
247 else
248 return ThreadPlan::ShouldReportStop(event_ptr);
249}
250
254
255 // If our subplan knows why we stopped, even if it's done (which would
256 // forward the question to us) we answer yes.
257 if (m_subplan_sp && m_subplan_sp->PlanExplainsStop(event_ptr)) {
259 return true;
260 }
261
262 // Check if the breakpoint is one of ours.
263
264 StopReason stop_reason;
266 stop_reason = eStopReasonNone;
267 else
268 stop_reason = m_real_stop_info_sp->GetStopReason();
269 LLDB_LOG(log,
270 "ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - {0}.",
271 Thread::StopReasonAsString(stop_reason));
272
273 if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
274 return true;
275
276 // One more quirk here. If this event was from Halt interrupting the target,
277 // then we should not consider ourselves complete. Return true to
278 // acknowledge the stop.
280 LLDB_LOGF(log, "ThreadPlanCallFunction::PlanExplainsStop: The event is an "
281 "Interrupt, returning true.");
282 return true;
283 }
284 // We control breakpoints separately from other "stop reasons." So first,
285 // check the case where we stopped for an internal breakpoint, in that case,
286 // continue on. If it is not an internal breakpoint, consult
287 // m_ignore_breakpoints.
288
289 if (stop_reason == eStopReasonBreakpoint) {
290 uint64_t break_site_id = m_real_stop_info_sp->GetValue();
291 BreakpointSiteSP bp_site_sp;
292 bp_site_sp = m_process.GetBreakpointSiteList().FindByID(break_site_id);
293 if (bp_site_sp) {
294 uint32_t num_owners = bp_site_sp->GetNumberOfConstituents();
295 bool is_internal = true;
296 for (uint32_t i = 0; i < num_owners; i++) {
297 Breakpoint &bp = bp_site_sp->GetConstituentAtIndex(i)->GetBreakpoint();
298 LLDB_LOGF(log,
299 "ThreadPlanCallFunction::PlanExplainsStop: hit "
300 "breakpoint %d while calling function",
301 bp.GetID());
302
303 if (!bp.IsInternal()) {
304 is_internal = false;
305 break;
306 }
307 }
308 if (is_internal) {
309 LLDB_LOGF(log, "ThreadPlanCallFunction::PlanExplainsStop hit an "
310 "internal breakpoint, not stopping.");
311 return false;
312 }
313 }
314
316 LLDB_LOGF(log,
317 "ThreadPlanCallFunction::PlanExplainsStop: we are ignoring "
318 "breakpoints, overriding breakpoint stop info ShouldStop, "
319 "returning true");
320 m_real_stop_info_sp->OverrideShouldStop(false);
321 return true;
322 } else {
323 LLDB_LOGF(log, "ThreadPlanCallFunction::PlanExplainsStop: we are not "
324 "ignoring breakpoints, overriding breakpoint stop info "
325 "ShouldStop, returning true");
326 m_real_stop_info_sp->OverrideShouldStop(true);
327 return false;
328 }
329 } else if (!m_unwind_on_error) {
330 // If we don't want to discard this plan, than any stop we don't understand
331 // should be propagated up the stack.
332 return false;
333 } else {
334 // If the subplan is running, any crashes are attributable to us. If we
335 // want to discard the plan, then we say we explain the stop but if we are
336 // going to be discarded, let whoever is above us explain the stop. But
337 // don't discard the plan if the stop would restart itself (for instance if
338 // it is a signal that is set not to stop. Check that here first. We just
339 // say we explain the stop but aren't done and everything will continue on
340 // from there.
341
343 m_real_stop_info_sp->ShouldStopSynchronous(event_ptr)) {
344 SetPlanComplete(false);
345 return m_subplan_sp ? m_unwind_on_error : false;
346 } else
347 return true;
348 }
349}
350
352 // We do some computation in DoPlanExplainsStop that may or may not set the
353 // plan as complete. We need to do that here to make sure our state is
354 // correct.
355 DoPlanExplainsStop(event_ptr);
356
357 if (IsPlanComplete()) {
358 ReportRegisterState("Function completed. Register state was:");
359 return true;
360 } else {
361 return false;
362 }
363}
364
366
368
370 //#define SINGLE_STEP_EXPRESSIONS
371
372 // Now set the thread state to "no reason" so we don't run with whatever
373 // signal was outstanding... Wait till the plan is pushed so we aren't
374 // changing the stop info till we're about to run.
375
377
378#ifndef SINGLE_STEP_EXPRESSIONS
379 Thread &thread = GetThread();
380 m_subplan_sp = std::make_shared<ThreadPlanRunToAddress>(thread, m_start_addr,
382
383 thread.QueueThreadPlan(m_subplan_sp, false);
384 m_subplan_sp->SetPrivate(true);
385#endif
386}
387
388bool ThreadPlanCallFunction::WillStop() { return true; }
389
391 Log *log = GetLog(LLDBLog::Step);
392
393 if (IsPlanComplete()) {
394 LLDB_LOGF(log, "ThreadPlanCallFunction(%p): Completed call function plan.",
395 static_cast<void *>(this));
396
398 return true;
399 } else {
400 return false;
401 }
402}
403
405 if (m_trap_exceptions) {
409
414 }
419 }
420 }
421}
422
424 if (m_trap_exceptions) {
429 }
430}
431
433 StopInfoSP stop_info_sp = GetPrivateStopInfo();
434
435 if (m_trap_exceptions) {
438 stop_info_sp)) ||
441 stop_info_sp))) {
442 Log *log = GetLog(LLDBLog::Step);
443 LLDB_LOGF(log, "ThreadPlanCallFunction::BreakpointsExplainStop - Hit an "
444 "exception breakpoint, setting plan complete.");
445
446 SetPlanComplete(false);
447
448 // If the user has set the ObjC language breakpoint, it would normally
449 // get priority over our internal catcher breakpoint, but in this case we
450 // can't let that happen, so force the ShouldStop here.
451 stop_info_sp->OverrideShouldStop(true);
452 return true;
453 }
454 }
455
456 return false;
457}
458
460 m_subplan_sp->SetStopOthers(new_value);
461}
462
465}
466
468 const ABI *abi = m_process.GetABI().get();
469 if (abi && m_return_type.IsValid()) {
470 const bool persistent = false;
472 abi->GetReturnValueObject(GetThread(), m_return_type, persistent);
473 }
474}
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:342
#define LLDB_LOGF(log,...)
Definition: Log.h:349
virtual bool PrepareTrivialCall(lldb_private::Thread &thread, lldb::addr_t sp, lldb::addr_t functionAddress, lldb::addr_t returnAddress, llvm::ArrayRef< lldb::addr_t > args) const =0
lldb::ValueObjectSP GetReturnValueObject(Thread &thread, CompilerType &type, bool persistent=true) const
Definition: ABI.cpp:70
virtual size_t GetRedZoneSize() const =0
A section + offset based address class.
Definition: Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:313
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition: Breakpoint.h:81
bool IsInternal() const
Tell whether this breakpoint is an "internal" breakpoint.
Definition: Breakpoint.cpp:249
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
virtual void SetExceptionBreakpoints()
virtual bool ExceptionBreakpointsExplainStop(lldb::StopInfoSP stop_reason)
virtual void ClearExceptionBreakpoints()
virtual bool ExceptionBreakpointsAreSet()
void PutCString(const char *cstr)
Definition: Log.cpp:134
bool GetVerbose() const
Definition: Log.cpp:313
void PutString(llvm::StringRef str)
Definition: Log.cpp:136
static bool GetInterruptedFromEvent(const Event *event_ptr)
Definition: Process.cpp:4227
StopPointSiteList< lldb_private::BreakpointSite > & GetBreakpointSiteList()
Definition: Process.cpp:1576
LanguageRuntime * GetLanguageRuntime(lldb::LanguageType language)
Definition: Process.cpp:1523
const lldb::ABISP & GetABI()
Definition: Process.cpp:1497
virtual const RegisterInfo * GetRegisterInfoAtIndex(size_t reg)=0
virtual size_t GetRegisterCount()=0
virtual bool ReadRegister(const RegisterInfo *reg_info, RegisterValue &reg_value)=0
An error handling class.
Definition: Status.h:44
StopPointSiteSP FindByID(typename StopPointSite::SiteID site_id)
Returns a shared pointer to the site with id site_id.
lldb::break_id_t GetID() const
Definition: Stoppoint.cpp:22
const char * GetData() const
Definition: StreamString.h:43
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:155
llvm::Expected< lldb_private::Address > GetEntryPointAddress()
This method will return the address of the starting function for this binary, e.g.
Definition: Target.cpp:2767
void SetStopOthers(bool new_value) override
bool ValidatePlan(Stream *error) override
Returns whether this plan could be successfully created.
bool ConstructorSetup(Thread &thread, ABI *&abi, lldb::addr_t &start_load_addr, lldb::addr_t &function_load_addr)
Vote ShouldReportStop(Event *event_ptr) override
ThreadPlanCallFunction(Thread &thread, const Address &function, const CompilerType &return_type, llvm::ArrayRef< lldb::addr_t > args, const EvaluateExpressionOptions &options)
bool DoPlanExplainsStop(Event *event_ptr) override
bool ShouldStop(Event *event_ptr) override
void GetDescription(Stream *s, lldb::DescriptionLevel level) override
Print a description of this thread to the stream s.
Thread::ThreadStateCheckpoint m_stored_thread_state
virtual Vote ShouldReportStop(Event *event_ptr)
Definition: ThreadPlan.cpp:79
void SetPrivate(bool input)
Definition: ThreadPlan.h:419
void SetPlanComplete(bool success=true)
Definition: ThreadPlan.cpp:66
Thread & GetThread()
Returns the Thread that is using this thread plan.
Definition: ThreadPlan.cpp:42
void SetOkayToDiscard(bool value)
Definition: ThreadPlan.h:406
virtual bool MischiefManaged()
Definition: ThreadPlan.cpp:72
bool SetIsControllingPlan(bool value)
Definition: ThreadPlan.h:398
lldb::StopInfoSP GetPrivateStopInfo()
Definition: ThreadPlan.h:517
virtual lldb::StackFrameSP GetStackFrameAtIndex(uint32_t idx)
Definition: Thread.h:405
void RestoreThreadStateFromCheckpoint(ThreadStateCheckpoint &saved_state)
Definition: Thread.cpp:548
Status QueueThreadPlan(lldb::ThreadPlanSP &plan_sp, bool abort_other_plans)
Queues a generic thread plan.
Definition: Thread.cpp:1156
virtual lldb::RegisterContextSP GetRegisterContext()=0
virtual bool RestoreRegisterStateFromCheckpoint(ThreadStateCheckpoint &saved_state)
Definition: Thread.cpp:526
void SetStopInfoToNothing()
Definition: Thread.cpp:490
static std::string StopReasonAsString(lldb::StopReason reason)
Definition: Thread.cpp:1686
virtual bool CheckpointThreadState(ThreadStateCheckpoint &saved_state)
Definition: Thread.cpp:500
lldb::ProcessSP GetProcess() const
Definition: Thread.h:154
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:314
void DumpRegisterValue(const RegisterValue &reg_val, Stream &s, const RegisterInfo &reg_info, bool prefix_with_name, bool prefix_with_alt_name, lldb::Format format, uint32_t reg_name_right_align_at=0, ExecutionContextScope *exe_scope=nullptr, bool print_flags=false, lldb::TargetSP target_sp=nullptr)
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::BreakpointSite > BreakpointSiteSP
Definition: lldb-forward.h:315
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
@ eDescriptionLevelBrief
StateType
Process and Thread States.
@ eStateRunning
Process or thread is running and can't be examined.
@ eLanguageTypeObjC
Objective-C.
@ eLanguageTypeC_plus_plus
ISO C++:1998.
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:381
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
Definition: lldb-forward.h:419
uint64_t addr_t
Definition: lldb-types.h:79
StopReason
Thread stop reasons.
@ eStopReasonBreakpoint
Every register is described in detail including its name, alternate name (optional),...