1 //===-- ThreadPlanCallFunction.cpp ------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/ThreadPlanCallFunction.h"
14 // Other libraries and framework includes
15 #include "llvm/Support/MachO.h"
17 #include "lldb/lldb-private-log.h"
18 #include "lldb/Breakpoint/Breakpoint.h"
19 #include "lldb/Breakpoint/BreakpointLocation.h"
20 #include "lldb/Core/Address.h"
21 #include "lldb/Core/Log.h"
22 #include "lldb/Core/Module.h"
23 #include "lldb/Core/Stream.h"
24 #include "lldb/Symbol/ObjectFile.h"
25 #include "lldb/Target/LanguageRuntime.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/RegisterContext.h"
28 #include "lldb/Target/StopInfo.h"
29 #include "lldb/Target/Target.h"
30 #include "lldb/Target/Thread.h"
31 #include "lldb/Target/ThreadPlanRunToAddress.h"
34 using namespace lldb_private;
36 //----------------------------------------------------------------------
37 // ThreadPlanCallFunction: Plan to call a single function
38 //----------------------------------------------------------------------
40 ThreadPlanCallFunction::ConstructorSetup (Thread &thread,
42 lldb::addr_t &start_load_addr,
43 lldb::addr_t &function_load_addr)
45 SetIsMasterPlan (true);
46 SetOkayToDiscard (false);
49 ProcessSP process_sp (thread.GetProcess());
53 abi = process_sp->GetABI().get();
58 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
62 m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
63 // If we can't read memory at the point of the process where we are planning to put our function, we're
64 // not going to get any further...
66 process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error);
69 m_constructor_errors.Printf ("Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".", m_function_sp);
71 log->Printf ("ThreadPlanCallFunction(%p): %s.", this, m_constructor_errors.GetData());
75 Module *exe_module = GetTarget().GetExecutableModulePointer();
77 if (exe_module == NULL)
79 m_constructor_errors.Printf ("Can't execute code without an executable module.");
81 log->Printf ("ThreadPlanCallFunction(%p): %s.", this, m_constructor_errors.GetData());
86 ObjectFile *objectFile = exe_module->GetObjectFile();
89 m_constructor_errors.Printf ("Could not find object file for module \"%s\".",
90 exe_module->GetFileSpec().GetFilename().AsCString());
93 log->Printf ("ThreadPlanCallFunction(%p): %s.", this, m_constructor_errors.GetData());
97 m_start_addr = objectFile->GetEntryPointAddress();
98 if (!m_start_addr.IsValid())
100 m_constructor_errors.Printf ("Could not find entry point address for executable module \"%s\".",
101 exe_module->GetFileSpec().GetFilename().AsCString());
103 log->Printf ("ThreadPlanCallFunction(%p): %s.", this, m_constructor_errors.GetData());
108 start_load_addr = m_start_addr.GetLoadAddress (&GetTarget());
110 // Checkpoint the thread state so we can restore it later.
111 if (log && log->GetVerbose())
112 ReportRegisterState ("About to checkpoint thread before function call. Original register state was:");
114 if (!thread.CheckpointThreadState (m_stored_thread_state))
116 m_constructor_errors.Printf ("Setting up ThreadPlanCallFunction, failed to checkpoint thread state.");
118 log->Printf ("ThreadPlanCallFunction(%p): %s.", this, m_constructor_errors.GetData());
121 function_load_addr = m_function_addr.GetLoadAddress (&GetTarget());
126 ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
127 const Address &function,
128 const ClangASTType &return_type,
129 llvm::ArrayRef<addr_t> args,
130 const EvaluateExpressionOptions &options) :
131 ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
133 m_stop_other_threads (options.GetStopOthers()),
134 m_unwind_on_error (options.DoesUnwindOnError()),
135 m_ignore_breakpoints (options.DoesIgnoreBreakpoints()),
136 m_debug_execution (options.GetDebug()),
137 m_trap_exceptions (options.GetTrapExceptions()),
138 m_function_addr (function),
140 m_return_type (return_type),
141 m_takedown_done (false),
142 m_should_clear_objc_exception_bp(false),
143 m_should_clear_cxx_exception_bp (false),
144 m_stop_address (LLDB_INVALID_ADDRESS)
146 lldb::addr_t start_load_addr;
148 lldb::addr_t function_load_addr;
149 if (!ConstructorSetup (thread, abi, start_load_addr, function_load_addr))
152 if (!abi->PrepareTrivialCall(thread,
159 ReportRegisterState ("Function call was set up. Register state was:");
164 ThreadPlanCallFunction::~ThreadPlanCallFunction ()
166 DoTakedown(PlanSucceeded());
170 ThreadPlanCallFunction::ReportRegisterState (const char *message)
172 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_VERBOSE));
176 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
178 log->PutCString(message);
180 RegisterValue reg_value;
182 for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
183 reg_idx < num_registers;
186 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_idx);
187 if (reg_ctx->ReadRegister(reg_info, reg_value))
189 reg_value.Dump(&strm, reg_info, true, false, eFormatDefault);
193 log->PutCString(strm.GetData());
198 ThreadPlanCallFunction::DoTakedown (bool success)
200 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
204 //Don't call DoTakedown if we were never valid to begin with.
206 log->Printf ("ThreadPlanCallFunction(%p): Log called on ThreadPlanCallFunction that was never valid.", this);
210 if (!m_takedown_done)
214 ProcessSP process_sp (m_thread.GetProcess());
215 const ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
216 if (abi && m_return_type.IsValid())
218 const bool persistent = false;
219 m_return_valobj_sp = abi->GetReturnValueObject (m_thread, m_return_type, persistent);
223 log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n", this, m_thread.GetID(), m_valid, IsPlanComplete());
224 m_takedown_done = true;
225 m_stop_address = m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
226 m_real_stop_info_sp = GetPrivateStopInfo ();
227 if (!m_thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state))
230 log->Printf("ThreadPlanCallFunction(%p): DoTakedown failed to restore register state", this);
232 SetPlanComplete(success);
234 if (log && log->GetVerbose())
235 ReportRegisterState ("Restoring thread state after function call. Restored register state:");
241 log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called as no-op for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n", this, m_thread.GetID(), m_valid, IsPlanComplete());
246 ThreadPlanCallFunction::WillPop ()
248 DoTakedown(PlanSucceeded());
252 ThreadPlanCallFunction::GetDescription (Stream *s, DescriptionLevel level)
254 if (level == eDescriptionLevelBrief)
256 s->Printf("Function call thread plan");
260 TargetSP target_sp (m_thread.CalculateTarget());
261 s->Printf("Thread plan to call 0x%" PRIx64, m_function_addr.GetLoadAddress(target_sp.get()));
266 ThreadPlanCallFunction::ValidatePlan (Stream *error)
272 if (m_constructor_errors.GetSize() > 0)
273 error->PutCString (m_constructor_errors.GetData());
275 error->PutCString ("Unknown error");
285 ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr)
287 if (m_takedown_done || IsPlanComplete())
290 return ThreadPlan::ShouldReportStop(event_ptr);
294 ThreadPlanCallFunction::DoPlanExplainsStop (Event *event_ptr)
296 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP|LIBLLDB_LOG_PROCESS));
297 m_real_stop_info_sp = GetPrivateStopInfo ();
299 // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
301 if (m_subplan_sp.get() != NULL && m_subplan_sp->PlanExplainsStop(event_ptr))
307 // Check if the breakpoint is one of ours.
309 StopReason stop_reason;
310 if (!m_real_stop_info_sp)
311 stop_reason = eStopReasonNone;
313 stop_reason = m_real_stop_info_sp->GetStopReason();
315 log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - %s.", Thread::StopReasonAsCString(stop_reason));
317 if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
320 // We control breakpoints separately from other "stop reasons." So first,
321 // check the case where we stopped for an internal breakpoint, in that case, continue on.
322 // If it is not an internal breakpoint, consult m_ignore_breakpoints.
325 if (stop_reason == eStopReasonBreakpoint)
327 ProcessSP process_sp (m_thread.CalculateProcess());
328 uint64_t break_site_id = m_real_stop_info_sp->GetValue();
329 BreakpointSiteSP bp_site_sp;
331 bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
334 uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
335 bool is_internal = true;
336 for (uint32_t i = 0; i < num_owners; i++)
338 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
340 log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: hit breakpoint %d while calling function", bp.GetID());
342 if (!bp.IsInternal())
351 log->Printf ("ThreadPlanCallFunction::PlanExplainsStop hit an internal breakpoint, not stopping.");
356 if (m_ignore_breakpoints)
359 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
360 m_real_stop_info_sp->OverrideShouldStop(false);
366 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are not ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
367 m_real_stop_info_sp->OverrideShouldStop(true);
371 else if (!m_unwind_on_error)
373 // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
378 // If the subplan is running, any crashes are attributable to us.
379 // If we want to discard the plan, then we say we explain the stop
380 // but if we are going to be discarded, let whoever is above us
382 // But don't discard the plan if the stop would restart itself (for instance if it is a
383 // signal that is set not to stop. Check that here first. We just say we explain the stop
384 // but aren't done and everything will continue on from there.
386 if (m_real_stop_info_sp->ShouldStopSynchronous(event_ptr))
388 SetPlanComplete(false);
391 if (m_unwind_on_error)
405 ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
407 // We do some computation in DoPlanExplainsStop that may or may not set the plan as complete.
408 // We need to do that here to make sure our state is correct.
409 DoPlanExplainsStop(event_ptr);
411 if (IsPlanComplete())
413 ReportRegisterState ("Function completed. Register state was:");
423 ThreadPlanCallFunction::StopOthers ()
425 return m_stop_other_threads;
429 ThreadPlanCallFunction::SetStopOthers (bool new_value)
433 ThreadPlanRunToAddress *address_plan = static_cast<ThreadPlanRunToAddress *>(m_subplan_sp.get());
434 address_plan->SetStopOthers(new_value);
436 m_stop_other_threads = new_value;
440 ThreadPlanCallFunction::GetPlanRunState ()
442 return eStateRunning;
446 ThreadPlanCallFunction::DidPush ()
448 //#define SINGLE_STEP_EXPRESSIONS
450 // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
451 // Wait till the plan is pushed so we aren't changing the stop info till we're about to run.
453 GetThread().SetStopInfoToNothing();
455 #ifndef SINGLE_STEP_EXPRESSIONS
456 m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
458 m_thread.QueueThreadPlan(m_subplan_sp, false);
459 m_subplan_sp->SetPrivate (true);
464 ThreadPlanCallFunction::WillStop ()
470 ThreadPlanCallFunction::MischiefManaged ()
472 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
474 if (IsPlanComplete())
477 log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.", this);
479 ThreadPlan::MischiefManaged ();
489 ThreadPlanCallFunction::SetBreakpoints ()
491 ProcessSP process_sp (m_thread.CalculateProcess());
492 if (m_trap_exceptions && process_sp)
494 m_cxx_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus);
495 m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC);
497 if (m_cxx_language_runtime)
499 m_should_clear_cxx_exception_bp = !m_cxx_language_runtime->ExceptionBreakpointsAreSet();
500 m_cxx_language_runtime->SetExceptionBreakpoints();
502 if (m_objc_language_runtime)
504 m_should_clear_objc_exception_bp = !m_objc_language_runtime->ExceptionBreakpointsAreSet();
505 m_objc_language_runtime->SetExceptionBreakpoints();
511 ThreadPlanCallFunction::ClearBreakpoints ()
513 if (m_trap_exceptions)
515 if (m_cxx_language_runtime && m_should_clear_cxx_exception_bp)
516 m_cxx_language_runtime->ClearExceptionBreakpoints();
517 if (m_objc_language_runtime && m_should_clear_objc_exception_bp)
518 m_objc_language_runtime->ClearExceptionBreakpoints();
523 ThreadPlanCallFunction::BreakpointsExplainStop()
525 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
527 if (m_trap_exceptions)
529 if ((m_cxx_language_runtime &&
530 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
531 ||(m_objc_language_runtime &&
532 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp)))
534 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
536 log->Printf ("ThreadPlanCallFunction::BreakpointsExplainStop - Hit an exception breakpoint, setting plan complete.");
538 SetPlanComplete(false);
540 // If the user has set the ObjC language breakpoint, it would normally get priority over our internal
541 // catcher breakpoint, but in this case we can't let that happen, so force the ShouldStop here.
542 stop_info_sp->OverrideShouldStop (true);
551 ThreadPlanCallFunction::RestoreThreadState()
553 return GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state);