1ac7ddfbfSEd Maste //===-- ThreadPlanCallFunction.cpp ------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
109f2f44ceSEd Maste #include "lldb/Target/ThreadPlanCallFunction.h"
11ac7ddfbfSEd Maste #include "lldb/Breakpoint/Breakpoint.h"
12ac7ddfbfSEd Maste #include "lldb/Breakpoint/BreakpointLocation.h"
13ac7ddfbfSEd Maste #include "lldb/Core/Address.h"
14*4ba319b5SDimitry Andric #include "lldb/Core/DumpRegisterValue.h"
15ac7ddfbfSEd Maste #include "lldb/Core/Module.h"
16ac7ddfbfSEd Maste #include "lldb/Symbol/ObjectFile.h"
171c3bbb01SEd Maste #include "lldb/Target/ABI.h"
18ac7ddfbfSEd Maste #include "lldb/Target/LanguageRuntime.h"
19ac7ddfbfSEd Maste #include "lldb/Target/Process.h"
20ac7ddfbfSEd Maste #include "lldb/Target/RegisterContext.h"
21ac7ddfbfSEd Maste #include "lldb/Target/StopInfo.h"
22ac7ddfbfSEd Maste #include "lldb/Target/Target.h"
23ac7ddfbfSEd Maste #include "lldb/Target/Thread.h"
24ac7ddfbfSEd Maste #include "lldb/Target/ThreadPlanRunToAddress.h"
25f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
26f678e45dSDimitry Andric #include "lldb/Utility/Stream.h"
27ac7ddfbfSEd Maste 
28ac7ddfbfSEd Maste using namespace lldb;
29ac7ddfbfSEd Maste using namespace lldb_private;
30ac7ddfbfSEd Maste 
31ac7ddfbfSEd Maste //----------------------------------------------------------------------
32ac7ddfbfSEd Maste // ThreadPlanCallFunction: Plan to call a single function
33ac7ddfbfSEd Maste //----------------------------------------------------------------------
ConstructorSetup(Thread & thread,ABI * & abi,lldb::addr_t & start_load_addr,lldb::addr_t & function_load_addr)34435933ddSDimitry Andric bool ThreadPlanCallFunction::ConstructorSetup(
35435933ddSDimitry Andric     Thread &thread, ABI *&abi, lldb::addr_t &start_load_addr,
36435933ddSDimitry Andric     lldb::addr_t &function_load_addr) {
37ac7ddfbfSEd Maste   SetIsMasterPlan(true);
38ac7ddfbfSEd Maste   SetOkayToDiscard(false);
39ac7ddfbfSEd Maste   SetPrivate(true);
40ac7ddfbfSEd Maste 
41ac7ddfbfSEd Maste   ProcessSP process_sp(thread.GetProcess());
42ac7ddfbfSEd Maste   if (!process_sp)
43ac7ddfbfSEd Maste     return false;
44ac7ddfbfSEd Maste 
45ac7ddfbfSEd Maste   abi = process_sp->GetABI().get();
46ac7ddfbfSEd Maste 
47ac7ddfbfSEd Maste   if (!abi)
48ac7ddfbfSEd Maste     return false;
49ac7ddfbfSEd Maste 
50ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
51ac7ddfbfSEd Maste 
52ac7ddfbfSEd Maste   SetBreakpoints();
53ac7ddfbfSEd Maste 
54ac7ddfbfSEd Maste   m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
55435933ddSDimitry Andric   // If we can't read memory at the point of the process where we are planning
56*4ba319b5SDimitry Andric   // to put our function, we're not going to get any further...
575517e702SDimitry Andric   Status error;
58ac7ddfbfSEd Maste   process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error);
59435933ddSDimitry Andric   if (!error.Success()) {
60435933ddSDimitry Andric     m_constructor_errors.Printf(
61435933ddSDimitry Andric         "Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".",
62435933ddSDimitry Andric         m_function_sp);
63ac7ddfbfSEd Maste     if (log)
64435933ddSDimitry Andric       log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
650127ef0fSEd Maste                   m_constructor_errors.GetData());
66ac7ddfbfSEd Maste     return false;
67ac7ddfbfSEd Maste   }
68ac7ddfbfSEd Maste 
69b952cd58SEd Maste   Module *exe_module = GetTarget().GetExecutableModulePointer();
70ac7ddfbfSEd Maste 
71435933ddSDimitry Andric   if (exe_module == nullptr) {
72435933ddSDimitry Andric     m_constructor_errors.Printf(
73435933ddSDimitry Andric         "Can't execute code without an executable module.");
74ac7ddfbfSEd Maste     if (log)
75435933ddSDimitry Andric       log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
760127ef0fSEd Maste                   m_constructor_errors.GetData());
77ac7ddfbfSEd Maste     return false;
78435933ddSDimitry Andric   } else {
79ac7ddfbfSEd Maste     ObjectFile *objectFile = exe_module->GetObjectFile();
80435933ddSDimitry Andric     if (!objectFile) {
81435933ddSDimitry Andric       m_constructor_errors.Printf(
82435933ddSDimitry Andric           "Could not find object file for module \"%s\".",
83ac7ddfbfSEd Maste           exe_module->GetFileSpec().GetFilename().AsCString());
84ac7ddfbfSEd Maste 
85ac7ddfbfSEd Maste       if (log)
860127ef0fSEd Maste         log->Printf("ThreadPlanCallFunction(%p): %s.",
87435933ddSDimitry Andric                     static_cast<void *>(this), m_constructor_errors.GetData());
88ac7ddfbfSEd Maste       return false;
89ac7ddfbfSEd Maste     }
90ac7ddfbfSEd Maste 
91ac7ddfbfSEd Maste     m_start_addr = objectFile->GetEntryPointAddress();
92435933ddSDimitry Andric     if (!m_start_addr.IsValid()) {
93435933ddSDimitry Andric       m_constructor_errors.Printf(
94435933ddSDimitry Andric           "Could not find entry point address for executable module \"%s\".",
95ac7ddfbfSEd Maste           exe_module->GetFileSpec().GetFilename().AsCString());
96ac7ddfbfSEd Maste       if (log)
970127ef0fSEd Maste         log->Printf("ThreadPlanCallFunction(%p): %s.",
98435933ddSDimitry Andric                     static_cast<void *>(this), m_constructor_errors.GetData());
99ac7ddfbfSEd Maste       return false;
100ac7ddfbfSEd Maste     }
101ac7ddfbfSEd Maste   }
102ac7ddfbfSEd Maste 
103b952cd58SEd Maste   start_load_addr = m_start_addr.GetLoadAddress(&GetTarget());
104ac7ddfbfSEd Maste 
105ac7ddfbfSEd Maste   // Checkpoint the thread state so we can restore it later.
106ac7ddfbfSEd Maste   if (log && log->GetVerbose())
107435933ddSDimitry Andric     ReportRegisterState("About to checkpoint thread before function call.  "
108435933ddSDimitry Andric                         "Original register state was:");
109ac7ddfbfSEd Maste 
110435933ddSDimitry Andric   if (!thread.CheckpointThreadState(m_stored_thread_state)) {
111435933ddSDimitry Andric     m_constructor_errors.Printf("Setting up ThreadPlanCallFunction, failed to "
112435933ddSDimitry Andric                                 "checkpoint thread state.");
113ac7ddfbfSEd Maste     if (log)
114435933ddSDimitry Andric       log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
1150127ef0fSEd Maste                   m_constructor_errors.GetData());
116ac7ddfbfSEd Maste     return false;
117ac7ddfbfSEd Maste   }
118b952cd58SEd Maste   function_load_addr = m_function_addr.GetLoadAddress(&GetTarget());
119ac7ddfbfSEd Maste 
120ac7ddfbfSEd Maste   return true;
121ac7ddfbfSEd Maste }
122ac7ddfbfSEd Maste 
ThreadPlanCallFunction(Thread & thread,const Address & function,const CompilerType & return_type,llvm::ArrayRef<addr_t> args,const EvaluateExpressionOptions & options)123435933ddSDimitry Andric ThreadPlanCallFunction::ThreadPlanCallFunction(
124435933ddSDimitry Andric     Thread &thread, const Address &function, const CompilerType &return_type,
125435933ddSDimitry Andric     llvm::ArrayRef<addr_t> args, const EvaluateExpressionOptions &options)
126435933ddSDimitry Andric     : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread,
127435933ddSDimitry Andric                  eVoteNoOpinion, eVoteNoOpinion),
128435933ddSDimitry Andric       m_valid(false), m_stop_other_threads(options.GetStopOthers()),
129b952cd58SEd Maste       m_unwind_on_error(options.DoesUnwindOnError()),
130b952cd58SEd Maste       m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
131b952cd58SEd Maste       m_debug_execution(options.GetDebug()),
132435933ddSDimitry Andric       m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function),
133435933ddSDimitry Andric       m_function_sp(0), m_takedown_done(false),
134b952cd58SEd Maste       m_should_clear_objc_exception_bp(false),
135b952cd58SEd Maste       m_should_clear_cxx_exception_bp(false),
136435933ddSDimitry Andric       m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(return_type) {
137b91a7dfcSDimitry Andric   lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS;
138b91a7dfcSDimitry Andric   lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS;
139b91a7dfcSDimitry Andric   ABI *abi = nullptr;
140b91a7dfcSDimitry Andric 
141ac7ddfbfSEd Maste   if (!ConstructorSetup(thread, abi, start_load_addr, function_load_addr))
142ac7ddfbfSEd Maste     return;
143ac7ddfbfSEd Maste 
144435933ddSDimitry Andric   if (!abi->PrepareTrivialCall(thread, m_function_sp, function_load_addr,
145435933ddSDimitry Andric                                start_load_addr, args))
146ac7ddfbfSEd Maste     return;
147ac7ddfbfSEd Maste 
148ac7ddfbfSEd Maste   ReportRegisterState("Function call was set up.  Register state was:");
149ac7ddfbfSEd Maste 
150ac7ddfbfSEd Maste   m_valid = true;
151ac7ddfbfSEd Maste }
152ac7ddfbfSEd Maste 
ThreadPlanCallFunction(Thread & thread,const Address & function,const EvaluateExpressionOptions & options)153435933ddSDimitry Andric ThreadPlanCallFunction::ThreadPlanCallFunction(
154435933ddSDimitry Andric     Thread &thread, const Address &function,
155435933ddSDimitry Andric     const EvaluateExpressionOptions &options)
156435933ddSDimitry Andric     : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread,
157435933ddSDimitry Andric                  eVoteNoOpinion, eVoteNoOpinion),
158435933ddSDimitry Andric       m_valid(false), m_stop_other_threads(options.GetStopOthers()),
159b91a7dfcSDimitry Andric       m_unwind_on_error(options.DoesUnwindOnError()),
160b91a7dfcSDimitry Andric       m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
161b91a7dfcSDimitry Andric       m_debug_execution(options.GetDebug()),
162435933ddSDimitry Andric       m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function),
163435933ddSDimitry Andric       m_function_sp(0), m_takedown_done(false),
164b91a7dfcSDimitry Andric       m_should_clear_objc_exception_bp(false),
165b91a7dfcSDimitry Andric       m_should_clear_cxx_exception_bp(false),
166435933ddSDimitry Andric       m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(CompilerType()) {}
167b91a7dfcSDimitry Andric 
~ThreadPlanCallFunction()168435933ddSDimitry Andric ThreadPlanCallFunction::~ThreadPlanCallFunction() {
169ac7ddfbfSEd Maste   DoTakedown(PlanSucceeded());
170ac7ddfbfSEd Maste }
171ac7ddfbfSEd Maste 
ReportRegisterState(const char * message)172435933ddSDimitry Andric void ThreadPlanCallFunction::ReportRegisterState(const char *message) {
173f678e45dSDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
174f678e45dSDimitry Andric   if (log && log->GetVerbose()) {
175ac7ddfbfSEd Maste     StreamString strm;
176ac7ddfbfSEd Maste     RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
177ac7ddfbfSEd Maste 
178ac7ddfbfSEd Maste     log->PutCString(message);
179ac7ddfbfSEd Maste 
180ac7ddfbfSEd Maste     RegisterValue reg_value;
181ac7ddfbfSEd Maste 
182ac7ddfbfSEd Maste     for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
183435933ddSDimitry Andric          reg_idx < num_registers; ++reg_idx) {
184ac7ddfbfSEd Maste       const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_idx);
185435933ddSDimitry Andric       if (reg_ctx->ReadRegister(reg_info, reg_value)) {
186*4ba319b5SDimitry Andric         DumpRegisterValue(reg_value, &strm, reg_info, true, false,
187*4ba319b5SDimitry Andric                           eFormatDefault);
188ac7ddfbfSEd Maste         strm.EOL();
189ac7ddfbfSEd Maste       }
190ac7ddfbfSEd Maste     }
191435933ddSDimitry Andric     log->PutString(strm.GetString());
192ac7ddfbfSEd Maste   }
193ac7ddfbfSEd Maste }
194ac7ddfbfSEd Maste 
DoTakedown(bool success)195435933ddSDimitry Andric void ThreadPlanCallFunction::DoTakedown(bool success) {
196ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
197ac7ddfbfSEd Maste 
198435933ddSDimitry Andric   if (!m_valid) {
199ac7ddfbfSEd Maste     // Don't call DoTakedown if we were never valid to begin with.
200ac7ddfbfSEd Maste     if (log)
201435933ddSDimitry Andric       log->Printf("ThreadPlanCallFunction(%p): Log called on "
202435933ddSDimitry Andric                   "ThreadPlanCallFunction that was never valid.",
2030127ef0fSEd Maste                   static_cast<void *>(this));
204ac7ddfbfSEd Maste     return;
205ac7ddfbfSEd Maste   }
206ac7ddfbfSEd Maste 
207435933ddSDimitry Andric   if (!m_takedown_done) {
208435933ddSDimitry Andric     if (success) {
209b91a7dfcSDimitry Andric       SetReturnValue();
210ac7ddfbfSEd Maste     }
211ac7ddfbfSEd Maste     if (log)
212435933ddSDimitry Andric       log->Printf("ThreadPlanCallFunction(%p): DoTakedown called for thread "
213435933ddSDimitry Andric                   "0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
2140127ef0fSEd Maste                   static_cast<void *>(this), m_thread.GetID(), m_valid,
2150127ef0fSEd Maste                   IsPlanComplete());
216ac7ddfbfSEd Maste     m_takedown_done = true;
217435933ddSDimitry Andric     m_stop_address =
218435933ddSDimitry Andric         m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
219ac7ddfbfSEd Maste     m_real_stop_info_sp = GetPrivateStopInfo();
220435933ddSDimitry Andric     if (!m_thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state)) {
221afda932aSEd Maste       if (log)
222435933ddSDimitry Andric         log->Printf("ThreadPlanCallFunction(%p): DoTakedown failed to restore "
223435933ddSDimitry Andric                     "register state",
2240127ef0fSEd Maste                     static_cast<void *>(this));
225afda932aSEd Maste     }
226ac7ddfbfSEd Maste     SetPlanComplete(success);
227ac7ddfbfSEd Maste     ClearBreakpoints();
228ac7ddfbfSEd Maste     if (log && log->GetVerbose())
229435933ddSDimitry Andric       ReportRegisterState("Restoring thread state after function call.  "
230435933ddSDimitry Andric                           "Restored register state:");
231435933ddSDimitry Andric   } else {
232ac7ddfbfSEd Maste     if (log)
233435933ddSDimitry Andric       log->Printf("ThreadPlanCallFunction(%p): DoTakedown called as no-op for "
234435933ddSDimitry Andric                   "thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
2350127ef0fSEd Maste                   static_cast<void *>(this), m_thread.GetID(), m_valid,
2360127ef0fSEd Maste                   IsPlanComplete());
237ac7ddfbfSEd Maste   }
238ac7ddfbfSEd Maste }
239ac7ddfbfSEd Maste 
WillPop()240435933ddSDimitry Andric void ThreadPlanCallFunction::WillPop() { DoTakedown(PlanSucceeded()); }
241ac7ddfbfSEd Maste 
GetDescription(Stream * s,DescriptionLevel level)242435933ddSDimitry Andric void ThreadPlanCallFunction::GetDescription(Stream *s, DescriptionLevel level) {
243435933ddSDimitry Andric   if (level == eDescriptionLevelBrief) {
244ac7ddfbfSEd Maste     s->Printf("Function call thread plan");
245435933ddSDimitry Andric   } else {
246ac7ddfbfSEd Maste     TargetSP target_sp(m_thread.CalculateTarget());
247435933ddSDimitry Andric     s->Printf("Thread plan to call 0x%" PRIx64,
248435933ddSDimitry Andric               m_function_addr.GetLoadAddress(target_sp.get()));
249ac7ddfbfSEd Maste   }
250ac7ddfbfSEd Maste }
251ac7ddfbfSEd Maste 
ValidatePlan(Stream * error)252435933ddSDimitry Andric bool ThreadPlanCallFunction::ValidatePlan(Stream *error) {
253435933ddSDimitry Andric   if (!m_valid) {
254435933ddSDimitry Andric     if (error) {
255ac7ddfbfSEd Maste       if (m_constructor_errors.GetSize() > 0)
256435933ddSDimitry Andric         error->PutCString(m_constructor_errors.GetString());
257ac7ddfbfSEd Maste       else
258ac7ddfbfSEd Maste         error->PutCString("Unknown error");
259ac7ddfbfSEd Maste     }
260ac7ddfbfSEd Maste     return false;
261ac7ddfbfSEd Maste   }
262ac7ddfbfSEd Maste 
263ac7ddfbfSEd Maste   return true;
264ac7ddfbfSEd Maste }
265ac7ddfbfSEd Maste 
ShouldReportStop(Event * event_ptr)266435933ddSDimitry Andric Vote ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr) {
267ac7ddfbfSEd Maste   if (m_takedown_done || IsPlanComplete())
268ac7ddfbfSEd Maste     return eVoteYes;
269ac7ddfbfSEd Maste   else
270ac7ddfbfSEd Maste     return ThreadPlan::ShouldReportStop(event_ptr);
271ac7ddfbfSEd Maste }
272ac7ddfbfSEd Maste 
DoPlanExplainsStop(Event * event_ptr)273435933ddSDimitry Andric bool ThreadPlanCallFunction::DoPlanExplainsStop(Event *event_ptr) {
274435933ddSDimitry Andric   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP |
275435933ddSDimitry Andric                                                   LIBLLDB_LOG_PROCESS));
276ac7ddfbfSEd Maste   m_real_stop_info_sp = GetPrivateStopInfo();
277ac7ddfbfSEd Maste 
278*4ba319b5SDimitry Andric   // If our subplan knows why we stopped, even if it's done (which would
279*4ba319b5SDimitry Andric   // forward the question to us) we answer yes.
280435933ddSDimitry Andric   if (m_subplan_sp && m_subplan_sp->PlanExplainsStop(event_ptr)) {
281ac7ddfbfSEd Maste     SetPlanComplete();
282ac7ddfbfSEd Maste     return true;
283ac7ddfbfSEd Maste   }
284ac7ddfbfSEd Maste 
285ac7ddfbfSEd Maste   // Check if the breakpoint is one of ours.
286ac7ddfbfSEd Maste 
287ac7ddfbfSEd Maste   StopReason stop_reason;
288ac7ddfbfSEd Maste   if (!m_real_stop_info_sp)
289ac7ddfbfSEd Maste     stop_reason = eStopReasonNone;
290ac7ddfbfSEd Maste   else
291ac7ddfbfSEd Maste     stop_reason = m_real_stop_info_sp->GetStopReason();
292ac7ddfbfSEd Maste   if (log)
293435933ddSDimitry Andric     log->Printf(
294435933ddSDimitry Andric         "ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - %s.",
295435933ddSDimitry Andric         Thread::StopReasonAsCString(stop_reason));
296ac7ddfbfSEd Maste 
297ac7ddfbfSEd Maste   if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
298ac7ddfbfSEd Maste     return true;
299ac7ddfbfSEd Maste 
300435933ddSDimitry Andric   // One more quirk here.  If this event was from Halt interrupting the target,
301*4ba319b5SDimitry Andric   // then we should not consider ourselves complete.  Return true to
302*4ba319b5SDimitry Andric   // acknowledge the stop.
303435933ddSDimitry Andric   if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr)) {
3040127ef0fSEd Maste     if (log)
305435933ddSDimitry Andric       log->Printf("ThreadPlanCallFunction::PlanExplainsStop: The event is an "
306435933ddSDimitry Andric                   "Interrupt, returning true.");
3070127ef0fSEd Maste     return true;
3080127ef0fSEd Maste   }
309ac7ddfbfSEd Maste   // We control breakpoints separately from other "stop reasons."  So first,
310435933ddSDimitry Andric   // check the case where we stopped for an internal breakpoint, in that case,
311*4ba319b5SDimitry Andric   // continue on. If it is not an internal breakpoint, consult
312*4ba319b5SDimitry Andric   // m_ignore_breakpoints.
313ac7ddfbfSEd Maste 
314435933ddSDimitry Andric   if (stop_reason == eStopReasonBreakpoint) {
315ac7ddfbfSEd Maste     ProcessSP process_sp(m_thread.CalculateProcess());
316ac7ddfbfSEd Maste     uint64_t break_site_id = m_real_stop_info_sp->GetValue();
317ac7ddfbfSEd Maste     BreakpointSiteSP bp_site_sp;
318ac7ddfbfSEd Maste     if (process_sp)
319ac7ddfbfSEd Maste       bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
320435933ddSDimitry Andric     if (bp_site_sp) {
321ac7ddfbfSEd Maste       uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
322ac7ddfbfSEd Maste       bool is_internal = true;
323435933ddSDimitry Andric       for (uint32_t i = 0; i < num_owners; i++) {
324ac7ddfbfSEd Maste         Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
325ac7ddfbfSEd Maste         if (log)
326435933ddSDimitry Andric           log->Printf("ThreadPlanCallFunction::PlanExplainsStop: hit "
327435933ddSDimitry Andric                       "breakpoint %d while calling function",
328435933ddSDimitry Andric                       bp.GetID());
329ac7ddfbfSEd Maste 
330435933ddSDimitry Andric         if (!bp.IsInternal()) {
331ac7ddfbfSEd Maste           is_internal = false;
332ac7ddfbfSEd Maste           break;
333ac7ddfbfSEd Maste         }
334ac7ddfbfSEd Maste       }
335435933ddSDimitry Andric       if (is_internal) {
336ac7ddfbfSEd Maste         if (log)
337435933ddSDimitry Andric           log->Printf("ThreadPlanCallFunction::PlanExplainsStop hit an "
338435933ddSDimitry Andric                       "internal breakpoint, not stopping.");
339ac7ddfbfSEd Maste         return false;
340ac7ddfbfSEd Maste       }
341ac7ddfbfSEd Maste     }
342ac7ddfbfSEd Maste 
343435933ddSDimitry Andric     if (m_ignore_breakpoints) {
344ac7ddfbfSEd Maste       if (log)
345435933ddSDimitry Andric         log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are ignoring "
346435933ddSDimitry Andric                     "breakpoints, overriding breakpoint stop info ShouldStop, "
347435933ddSDimitry Andric                     "returning true");
348ac7ddfbfSEd Maste       m_real_stop_info_sp->OverrideShouldStop(false);
349ac7ddfbfSEd Maste       return true;
350435933ddSDimitry Andric     } else {
351ac7ddfbfSEd Maste       if (log)
352435933ddSDimitry Andric         log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are not "
353435933ddSDimitry Andric                     "ignoring breakpoints, overriding breakpoint stop info "
354435933ddSDimitry Andric                     "ShouldStop, returning true");
355ac7ddfbfSEd Maste       m_real_stop_info_sp->OverrideShouldStop(true);
356ac7ddfbfSEd Maste       return false;
357ac7ddfbfSEd Maste     }
358435933ddSDimitry Andric   } else if (!m_unwind_on_error) {
359435933ddSDimitry Andric     // If we don't want to discard this plan, than any stop we don't understand
360435933ddSDimitry Andric     // should be propagated up the stack.
361ac7ddfbfSEd Maste     return false;
362435933ddSDimitry Andric   } else {
363*4ba319b5SDimitry Andric     // If the subplan is running, any crashes are attributable to us. If we
364*4ba319b5SDimitry Andric     // want to discard the plan, then we say we explain the stop but if we are
365*4ba319b5SDimitry Andric     // going to be discarded, let whoever is above us explain the stop. But
366*4ba319b5SDimitry Andric     // don't discard the plan if the stop would restart itself (for instance if
367*4ba319b5SDimitry Andric     // it is a signal that is set not to stop.  Check that here first.  We just
368*4ba319b5SDimitry Andric     // say we explain the stop but aren't done and everything will continue on
369*4ba319b5SDimitry Andric     // from there.
370ac7ddfbfSEd Maste 
371435933ddSDimitry Andric     if (m_real_stop_info_sp &&
372435933ddSDimitry Andric         m_real_stop_info_sp->ShouldStopSynchronous(event_ptr)) {
373ac7ddfbfSEd Maste       SetPlanComplete(false);
3749f2f44ceSEd Maste       return m_subplan_sp ? m_unwind_on_error : false;
375435933ddSDimitry Andric     } else
376ac7ddfbfSEd Maste       return true;
377ac7ddfbfSEd Maste   }
378ac7ddfbfSEd Maste }
379ac7ddfbfSEd Maste 
ShouldStop(Event * event_ptr)380435933ddSDimitry Andric bool ThreadPlanCallFunction::ShouldStop(Event *event_ptr) {
381435933ddSDimitry Andric   // We do some computation in DoPlanExplainsStop that may or may not set the
382*4ba319b5SDimitry Andric   // plan as complete. We need to do that here to make sure our state is
383*4ba319b5SDimitry Andric   // correct.
384ac7ddfbfSEd Maste   DoPlanExplainsStop(event_ptr);
385ac7ddfbfSEd Maste 
386435933ddSDimitry Andric   if (IsPlanComplete()) {
387ac7ddfbfSEd Maste     ReportRegisterState("Function completed.  Register state was:");
388ac7ddfbfSEd Maste     return true;
389435933ddSDimitry Andric   } else {
390ac7ddfbfSEd Maste     return false;
391ac7ddfbfSEd Maste   }
392ac7ddfbfSEd Maste }
393ac7ddfbfSEd Maste 
StopOthers()394435933ddSDimitry Andric bool ThreadPlanCallFunction::StopOthers() { return m_stop_other_threads; }
395ac7ddfbfSEd Maste 
GetPlanRunState()396435933ddSDimitry Andric StateType ThreadPlanCallFunction::GetPlanRunState() { return eStateRunning; }
397ac7ddfbfSEd Maste 
DidPush()398435933ddSDimitry Andric void ThreadPlanCallFunction::DidPush() {
399ac7ddfbfSEd Maste   //#define SINGLE_STEP_EXPRESSIONS
400ac7ddfbfSEd Maste 
401435933ddSDimitry Andric   // Now set the thread state to "no reason" so we don't run with whatever
402*4ba319b5SDimitry Andric   // signal was outstanding... Wait till the plan is pushed so we aren't
403*4ba319b5SDimitry Andric   // changing the stop info till we're about to run.
404ac7ddfbfSEd Maste 
405ac7ddfbfSEd Maste   GetThread().SetStopInfoToNothing();
406ac7ddfbfSEd Maste 
407ac7ddfbfSEd Maste #ifndef SINGLE_STEP_EXPRESSIONS
408435933ddSDimitry Andric   m_subplan_sp.reset(
409435933ddSDimitry Andric       new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
410ac7ddfbfSEd Maste 
411ac7ddfbfSEd Maste   m_thread.QueueThreadPlan(m_subplan_sp, false);
412ac7ddfbfSEd Maste   m_subplan_sp->SetPrivate(true);
413ac7ddfbfSEd Maste #endif
414ac7ddfbfSEd Maste }
415ac7ddfbfSEd Maste 
WillStop()416435933ddSDimitry Andric bool ThreadPlanCallFunction::WillStop() { return true; }
417ac7ddfbfSEd Maste 
MischiefManaged()418435933ddSDimitry Andric bool ThreadPlanCallFunction::MischiefManaged() {
419ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
420ac7ddfbfSEd Maste 
421435933ddSDimitry Andric   if (IsPlanComplete()) {
422ac7ddfbfSEd Maste     if (log)
4230127ef0fSEd Maste       log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.",
4240127ef0fSEd Maste                   static_cast<void *>(this));
425ac7ddfbfSEd Maste 
426ac7ddfbfSEd Maste     ThreadPlan::MischiefManaged();
427ac7ddfbfSEd Maste     return true;
428435933ddSDimitry Andric   } else {
429ac7ddfbfSEd Maste     return false;
430ac7ddfbfSEd Maste   }
431ac7ddfbfSEd Maste }
432ac7ddfbfSEd Maste 
SetBreakpoints()433435933ddSDimitry Andric void ThreadPlanCallFunction::SetBreakpoints() {
434ac7ddfbfSEd Maste   ProcessSP process_sp(m_thread.CalculateProcess());
435435933ddSDimitry Andric   if (m_trap_exceptions && process_sp) {
436435933ddSDimitry Andric     m_cxx_language_runtime =
437435933ddSDimitry Andric         process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus);
438ac7ddfbfSEd Maste     m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC);
439ac7ddfbfSEd Maste 
440435933ddSDimitry Andric     if (m_cxx_language_runtime) {
441435933ddSDimitry Andric       m_should_clear_cxx_exception_bp =
442435933ddSDimitry Andric           !m_cxx_language_runtime->ExceptionBreakpointsAreSet();
443ac7ddfbfSEd Maste       m_cxx_language_runtime->SetExceptionBreakpoints();
444b952cd58SEd Maste     }
445435933ddSDimitry Andric     if (m_objc_language_runtime) {
446435933ddSDimitry Andric       m_should_clear_objc_exception_bp =
447435933ddSDimitry Andric           !m_objc_language_runtime->ExceptionBreakpointsAreSet();
448ac7ddfbfSEd Maste       m_objc_language_runtime->SetExceptionBreakpoints();
449ac7ddfbfSEd Maste     }
450ac7ddfbfSEd Maste   }
451b952cd58SEd Maste }
452ac7ddfbfSEd Maste 
ClearBreakpoints()453435933ddSDimitry Andric void ThreadPlanCallFunction::ClearBreakpoints() {
454435933ddSDimitry Andric   if (m_trap_exceptions) {
455b952cd58SEd Maste     if (m_cxx_language_runtime && m_should_clear_cxx_exception_bp)
456ac7ddfbfSEd Maste       m_cxx_language_runtime->ClearExceptionBreakpoints();
457b952cd58SEd Maste     if (m_objc_language_runtime && m_should_clear_objc_exception_bp)
458ac7ddfbfSEd Maste       m_objc_language_runtime->ClearExceptionBreakpoints();
459ac7ddfbfSEd Maste   }
460b952cd58SEd Maste }
461ac7ddfbfSEd Maste 
BreakpointsExplainStop()462435933ddSDimitry Andric bool ThreadPlanCallFunction::BreakpointsExplainStop() {
463ac7ddfbfSEd Maste   StopInfoSP stop_info_sp = GetPrivateStopInfo();
464ac7ddfbfSEd Maste 
465435933ddSDimitry Andric   if (m_trap_exceptions) {
466ac7ddfbfSEd Maste     if ((m_cxx_language_runtime &&
467435933ddSDimitry Andric          m_cxx_language_runtime->ExceptionBreakpointsExplainStop(
468435933ddSDimitry Andric              stop_info_sp)) ||
469435933ddSDimitry Andric         (m_objc_language_runtime &&
470435933ddSDimitry Andric          m_objc_language_runtime->ExceptionBreakpointsExplainStop(
471435933ddSDimitry Andric              stop_info_sp))) {
472ac7ddfbfSEd Maste       Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
473ac7ddfbfSEd Maste       if (log)
474435933ddSDimitry Andric         log->Printf("ThreadPlanCallFunction::BreakpointsExplainStop - Hit an "
475435933ddSDimitry Andric                     "exception breakpoint, setting plan complete.");
476ac7ddfbfSEd Maste 
477ac7ddfbfSEd Maste       SetPlanComplete(false);
478ac7ddfbfSEd Maste 
479*4ba319b5SDimitry Andric       // If the user has set the ObjC language breakpoint, it would normally
480*4ba319b5SDimitry Andric       // get priority over our internal catcher breakpoint, but in this case we
481*4ba319b5SDimitry Andric       // can't let that happen, so force the ShouldStop here.
482ac7ddfbfSEd Maste       stop_info_sp->OverrideShouldStop(true);
483ac7ddfbfSEd Maste       return true;
484ac7ddfbfSEd Maste     }
485b952cd58SEd Maste   }
486ac7ddfbfSEd Maste 
487ac7ddfbfSEd Maste   return false;
488ac7ddfbfSEd Maste }
489ac7ddfbfSEd Maste 
SetStopOthers(bool new_value)490435933ddSDimitry Andric void ThreadPlanCallFunction::SetStopOthers(bool new_value) {
4910127ef0fSEd Maste   m_subplan_sp->SetStopOthers(new_value);
4920127ef0fSEd Maste }
4930127ef0fSEd Maste 
RestoreThreadState()494435933ddSDimitry Andric bool ThreadPlanCallFunction::RestoreThreadState() {
495ac7ddfbfSEd Maste   return GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state);
496ac7ddfbfSEd Maste }
497ac7ddfbfSEd Maste 
SetReturnValue()498435933ddSDimitry Andric void ThreadPlanCallFunction::SetReturnValue() {
499b91a7dfcSDimitry Andric   ProcessSP process_sp(m_thread.GetProcess());
5009f2f44ceSEd Maste   const ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
501435933ddSDimitry Andric   if (abi && m_return_type.IsValid()) {
502b91a7dfcSDimitry Andric     const bool persistent = false;
503435933ddSDimitry Andric     m_return_valobj_sp =
504435933ddSDimitry Andric         abi->GetReturnValueObject(m_thread, m_return_type, persistent);
505b91a7dfcSDimitry Andric   }
506b91a7dfcSDimitry Andric }
507