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 TargetSP target_sp (thread.CalculateTarget());
60 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
64 m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
65 // If we can't read memory at the point of the process where we are planning to put our function, we're
66 // not going to get any further...
68 process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error);
71 m_constructor_errors.Printf ("Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".", m_function_sp);
73 log->Printf ("ThreadPlanCallFunction(%p): %s.", this, m_constructor_errors.GetData());
77 Module *exe_module = target_sp->GetExecutableModulePointer();
79 if (exe_module == NULL)
81 m_constructor_errors.Printf ("Can't execute code without an executable module.");
83 log->Printf ("ThreadPlanCallFunction(%p): %s.", this, m_constructor_errors.GetData());
88 ObjectFile *objectFile = exe_module->GetObjectFile();
91 m_constructor_errors.Printf ("Could not find object file for module \"%s\".",
92 exe_module->GetFileSpec().GetFilename().AsCString());
95 log->Printf ("ThreadPlanCallFunction(%p): %s.", this, m_constructor_errors.GetData());
99 m_start_addr = objectFile->GetEntryPointAddress();
100 if (!m_start_addr.IsValid())
102 m_constructor_errors.Printf ("Could not find entry point address for executable module \"%s\".",
103 exe_module->GetFileSpec().GetFilename().AsCString());
105 log->Printf ("ThreadPlanCallFunction(%p): %s.", this, m_constructor_errors.GetData());
110 start_load_addr = m_start_addr.GetLoadAddress (target_sp.get());
112 // Checkpoint the thread state so we can restore it later.
113 if (log && log->GetVerbose())
114 ReportRegisterState ("About to checkpoint thread before function call. Original register state was:");
116 if (!thread.CheckpointThreadState (m_stored_thread_state))
118 m_constructor_errors.Printf ("Setting up ThreadPlanCallFunction, failed to checkpoint thread state.");
120 log->Printf ("ThreadPlanCallFunction(%p): %s.", this, m_constructor_errors.GetData());
123 function_load_addr = m_function_addr.GetLoadAddress (target_sp.get());
128 ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
129 const Address &function,
130 const ClangASTType &return_type,
132 bool stop_other_threads,
133 bool unwind_on_error,
134 bool ignore_breakpoints,
137 ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
139 m_stop_other_threads (stop_other_threads),
140 m_function_addr (function),
142 m_return_type (return_type),
143 m_takedown_done (false),
144 m_stop_address (LLDB_INVALID_ADDRESS),
145 m_unwind_on_error (unwind_on_error),
146 m_ignore_breakpoints (ignore_breakpoints)
148 lldb::addr_t start_load_addr;
150 lldb::addr_t function_load_addr;
151 if (!ConstructorSetup (thread, abi, start_load_addr, function_load_addr))
154 if (this_arg && cmd_arg)
156 if (!abi->PrepareTrivialCall (thread,
167 if (!abi->PrepareTrivialCall (thread,
177 if (!abi->PrepareTrivialCall (thread,
185 ReportRegisterState ("Function call was set up. Register state was:");
191 ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
192 const Address &function,
193 const ClangASTType &return_type,
194 bool stop_other_threads,
195 bool unwind_on_error,
196 bool ignore_breakpoints,
203 ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
205 m_stop_other_threads (stop_other_threads),
206 m_function_addr (function),
208 m_return_type (return_type),
209 m_takedown_done (false),
210 m_stop_address (LLDB_INVALID_ADDRESS),
211 m_unwind_on_error (unwind_on_error),
212 m_ignore_breakpoints (ignore_breakpoints)
214 lldb::addr_t start_load_addr;
216 lldb::addr_t function_load_addr;
217 if (!ConstructorSetup (thread, abi, start_load_addr, function_load_addr))
220 if (!abi->PrepareTrivialCall (thread,
234 ReportRegisterState ("Function call was set up. Register state was:");
239 ThreadPlanCallFunction::~ThreadPlanCallFunction ()
241 DoTakedown(PlanSucceeded());
245 ThreadPlanCallFunction::ReportRegisterState (const char *message)
247 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_VERBOSE));
251 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
253 log->PutCString(message);
255 RegisterValue reg_value;
257 for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
258 reg_idx < num_registers;
261 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_idx);
262 if (reg_ctx->ReadRegister(reg_info, reg_value))
264 reg_value.Dump(&strm, reg_info, true, false, eFormatDefault);
268 log->PutCString(strm.GetData());
273 ThreadPlanCallFunction::DoTakedown (bool success)
275 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
279 //Don't call DoTakedown if we were never valid to begin with.
281 log->Printf ("ThreadPlanCallFunction(%p): Log called on ThreadPlanCallFunction that was never valid.", this);
285 if (!m_takedown_done)
289 ProcessSP process_sp (m_thread.GetProcess());
290 const ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
291 if (abi && m_return_type.IsValid())
293 const bool persistent = false;
294 m_return_valobj_sp = abi->GetReturnValueObject (m_thread, m_return_type, persistent);
298 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());
299 m_takedown_done = true;
300 m_stop_address = m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
301 m_real_stop_info_sp = GetPrivateStopInfo ();
302 m_thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state);
303 SetPlanComplete(success);
305 if (log && log->GetVerbose())
306 ReportRegisterState ("Restoring thread state after function call. Restored register state:");
312 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());
317 ThreadPlanCallFunction::WillPop ()
319 DoTakedown(PlanSucceeded());
323 ThreadPlanCallFunction::GetDescription (Stream *s, DescriptionLevel level)
325 if (level == eDescriptionLevelBrief)
327 s->Printf("Function call thread plan");
331 TargetSP target_sp (m_thread.CalculateTarget());
332 s->Printf("Thread plan to call 0x%" PRIx64, m_function_addr.GetLoadAddress(target_sp.get()));
337 ThreadPlanCallFunction::ValidatePlan (Stream *error)
343 if (m_constructor_errors.GetSize() > 0)
344 error->PutCString (m_constructor_errors.GetData());
346 error->PutCString ("Unknown error");
356 ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr)
358 if (m_takedown_done || IsPlanComplete())
361 return ThreadPlan::ShouldReportStop(event_ptr);
365 ThreadPlanCallFunction::DoPlanExplainsStop (Event *event_ptr)
367 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP|LIBLLDB_LOG_PROCESS));
368 m_real_stop_info_sp = GetPrivateStopInfo ();
370 // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
372 if (m_subplan_sp.get() != NULL && m_subplan_sp->PlanExplainsStop(event_ptr))
378 // Check if the breakpoint is one of ours.
380 StopReason stop_reason;
381 if (!m_real_stop_info_sp)
382 stop_reason = eStopReasonNone;
384 stop_reason = m_real_stop_info_sp->GetStopReason();
386 log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - %s.", Thread::StopReasonAsCString(stop_reason));
388 if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
391 // We control breakpoints separately from other "stop reasons." So first,
392 // check the case where we stopped for an internal breakpoint, in that case, continue on.
393 // If it is not an internal breakpoint, consult m_ignore_breakpoints.
396 if (stop_reason == eStopReasonBreakpoint)
398 ProcessSP process_sp (m_thread.CalculateProcess());
399 uint64_t break_site_id = m_real_stop_info_sp->GetValue();
400 BreakpointSiteSP bp_site_sp;
402 bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
405 uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
406 bool is_internal = true;
407 for (uint32_t i = 0; i < num_owners; i++)
409 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
411 log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: hit breakpoint %d while calling function", bp.GetID());
413 if (!bp.IsInternal())
422 log->Printf ("ThreadPlanCallFunction::PlanExplainsStop hit an internal breakpoint, not stopping.");
427 if (m_ignore_breakpoints)
430 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
431 m_real_stop_info_sp->OverrideShouldStop(false);
437 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are not ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
438 m_real_stop_info_sp->OverrideShouldStop(true);
442 else if (!m_unwind_on_error)
444 // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
449 // If the subplan is running, any crashes are attributable to us.
450 // If we want to discard the plan, then we say we explain the stop
451 // but if we are going to be discarded, let whoever is above us
453 // But don't discard the plan if the stop would restart itself (for instance if it is a
454 // signal that is set not to stop. Check that here first. We just say we explain the stop
455 // but aren't done and everything will continue on from there.
457 if (m_real_stop_info_sp->ShouldStopSynchronous(event_ptr))
459 SetPlanComplete(false);
462 if (m_unwind_on_error)
476 ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
478 // We do some computation in DoPlanExplainsStop that may or may not set the plan as complete.
479 // We need to do that here to make sure our state is correct.
480 DoPlanExplainsStop(event_ptr);
482 if (IsPlanComplete())
484 ReportRegisterState ("Function completed. Register state was:");
494 ThreadPlanCallFunction::StopOthers ()
496 return m_stop_other_threads;
500 ThreadPlanCallFunction::SetStopOthers (bool new_value)
504 ThreadPlanRunToAddress *address_plan = static_cast<ThreadPlanRunToAddress *>(m_subplan_sp.get());
505 address_plan->SetStopOthers(new_value);
507 m_stop_other_threads = new_value;
511 ThreadPlanCallFunction::GetPlanRunState ()
513 return eStateRunning;
517 ThreadPlanCallFunction::DidPush ()
519 //#define SINGLE_STEP_EXPRESSIONS
521 // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
522 // Wait till the plan is pushed so we aren't changing the stop info till we're about to run.
524 GetThread().SetStopInfoToNothing();
526 #ifndef SINGLE_STEP_EXPRESSIONS
527 m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
529 m_thread.QueueThreadPlan(m_subplan_sp, false);
530 m_subplan_sp->SetPrivate (true);
535 ThreadPlanCallFunction::WillStop ()
541 ThreadPlanCallFunction::MischiefManaged ()
543 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
545 if (IsPlanComplete())
548 log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.", this);
550 ThreadPlan::MischiefManaged ();
560 ThreadPlanCallFunction::SetBreakpoints ()
562 ProcessSP process_sp (m_thread.CalculateProcess());
565 m_cxx_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus);
566 m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC);
568 if (m_cxx_language_runtime)
569 m_cxx_language_runtime->SetExceptionBreakpoints();
570 if (m_objc_language_runtime)
571 m_objc_language_runtime->SetExceptionBreakpoints();
576 ThreadPlanCallFunction::ClearBreakpoints ()
578 if (m_cxx_language_runtime)
579 m_cxx_language_runtime->ClearExceptionBreakpoints();
580 if (m_objc_language_runtime)
581 m_objc_language_runtime->ClearExceptionBreakpoints();
585 ThreadPlanCallFunction::BreakpointsExplainStop()
587 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
589 if ((m_cxx_language_runtime &&
590 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
591 ||(m_objc_language_runtime &&
592 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp)))
594 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
596 log->Printf ("ThreadPlanCallFunction::BreakpointsExplainStop - Hit an exception breakpoint, setting plan complete.");
598 SetPlanComplete(false);
600 // If the user has set the ObjC language breakpoint, it would normally get priority over our internal
601 // catcher breakpoint, but in this case we can't let that happen, so force the ShouldStop here.
602 stop_info_sp->OverrideShouldStop (true);
610 ThreadPlanCallFunction::RestoreThreadState()
612 return GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state);