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 
10ac7ddfbfSEd Maste // C Includes
11ac7ddfbfSEd Maste // C++ Includes
12ac7ddfbfSEd Maste // Other libraries and framework includes
13ac7ddfbfSEd Maste // Project includes
149f2f44ceSEd Maste #include "lldb/Target/ThreadPlanCallFunction.h"
15ac7ddfbfSEd Maste #include "lldb/Breakpoint/Breakpoint.h"
16ac7ddfbfSEd Maste #include "lldb/Breakpoint/BreakpointLocation.h"
17ac7ddfbfSEd Maste #include "lldb/Core/Address.h"
18ac7ddfbfSEd Maste #include "lldb/Core/Log.h"
19ac7ddfbfSEd Maste #include "lldb/Core/Module.h"
20ac7ddfbfSEd Maste #include "lldb/Core/Stream.h"
21ac7ddfbfSEd Maste #include "lldb/Symbol/ObjectFile.h"
221c3bbb01SEd Maste #include "lldb/Target/ABI.h"
23ac7ddfbfSEd Maste #include "lldb/Target/LanguageRuntime.h"
24ac7ddfbfSEd Maste #include "lldb/Target/Process.h"
25ac7ddfbfSEd Maste #include "lldb/Target/RegisterContext.h"
26ac7ddfbfSEd Maste #include "lldb/Target/StopInfo.h"
27ac7ddfbfSEd Maste #include "lldb/Target/Target.h"
28ac7ddfbfSEd Maste #include "lldb/Target/Thread.h"
29ac7ddfbfSEd Maste #include "lldb/Target/ThreadPlanRunToAddress.h"
30ac7ddfbfSEd Maste 
31ac7ddfbfSEd Maste using namespace lldb;
32ac7ddfbfSEd Maste using namespace lldb_private;
33ac7ddfbfSEd Maste 
34ac7ddfbfSEd Maste //----------------------------------------------------------------------
35ac7ddfbfSEd Maste // ThreadPlanCallFunction: Plan to call a single function
36ac7ddfbfSEd Maste //----------------------------------------------------------------------
37ac7ddfbfSEd Maste bool
38ac7ddfbfSEd Maste ThreadPlanCallFunction::ConstructorSetup (Thread &thread,
39ac7ddfbfSEd Maste                                           ABI *& abi,
40ac7ddfbfSEd Maste                                           lldb::addr_t &start_load_addr,
41ac7ddfbfSEd Maste                                           lldb::addr_t &function_load_addr)
42ac7ddfbfSEd Maste {
43ac7ddfbfSEd Maste     SetIsMasterPlan (true);
44ac7ddfbfSEd Maste     SetOkayToDiscard (false);
45ac7ddfbfSEd Maste     SetPrivate (true);
46ac7ddfbfSEd Maste 
47ac7ddfbfSEd Maste     ProcessSP process_sp (thread.GetProcess());
48ac7ddfbfSEd Maste     if (!process_sp)
49ac7ddfbfSEd Maste         return false;
50ac7ddfbfSEd Maste 
51ac7ddfbfSEd Maste     abi = process_sp->GetABI().get();
52ac7ddfbfSEd Maste 
53ac7ddfbfSEd Maste     if (!abi)
54ac7ddfbfSEd Maste         return false;
55ac7ddfbfSEd Maste 
56ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
57ac7ddfbfSEd Maste 
58ac7ddfbfSEd Maste     SetBreakpoints();
59ac7ddfbfSEd Maste 
60ac7ddfbfSEd Maste     m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
61ac7ddfbfSEd Maste     // If we can't read memory at the point of the process where we are planning to put our function, we're
62ac7ddfbfSEd Maste     // not going to get any further...
63ac7ddfbfSEd Maste     Error error;
64ac7ddfbfSEd Maste     process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error);
65ac7ddfbfSEd Maste     if (!error.Success())
66ac7ddfbfSEd Maste     {
67ac7ddfbfSEd Maste         m_constructor_errors.Printf ("Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".", m_function_sp);
68ac7ddfbfSEd Maste         if (log)
690127ef0fSEd Maste             log->Printf ("ThreadPlanCallFunction(%p): %s.",
700127ef0fSEd Maste                          static_cast<void*>(this),
710127ef0fSEd Maste                          m_constructor_errors.GetData());
72ac7ddfbfSEd Maste         return false;
73ac7ddfbfSEd Maste     }
74ac7ddfbfSEd Maste 
75b952cd58SEd Maste     Module *exe_module = GetTarget().GetExecutableModulePointer();
76ac7ddfbfSEd Maste 
779f2f44ceSEd Maste     if (exe_module == nullptr)
78ac7ddfbfSEd Maste     {
79ac7ddfbfSEd Maste         m_constructor_errors.Printf ("Can't execute code without an executable module.");
80ac7ddfbfSEd Maste         if (log)
810127ef0fSEd Maste             log->Printf ("ThreadPlanCallFunction(%p): %s.",
820127ef0fSEd Maste                          static_cast<void*>(this),
830127ef0fSEd Maste                          m_constructor_errors.GetData());
84ac7ddfbfSEd Maste         return false;
85ac7ddfbfSEd Maste     }
86ac7ddfbfSEd Maste     else
87ac7ddfbfSEd Maste     {
88ac7ddfbfSEd Maste         ObjectFile *objectFile = exe_module->GetObjectFile();
89ac7ddfbfSEd Maste         if (!objectFile)
90ac7ddfbfSEd Maste         {
91ac7ddfbfSEd Maste             m_constructor_errors.Printf ("Could not find object file for module \"%s\".",
92ac7ddfbfSEd Maste                                          exe_module->GetFileSpec().GetFilename().AsCString());
93ac7ddfbfSEd Maste 
94ac7ddfbfSEd Maste             if (log)
950127ef0fSEd Maste                 log->Printf ("ThreadPlanCallFunction(%p): %s.",
960127ef0fSEd Maste                              static_cast<void*>(this),
970127ef0fSEd Maste                              m_constructor_errors.GetData());
98ac7ddfbfSEd Maste             return false;
99ac7ddfbfSEd Maste         }
100ac7ddfbfSEd Maste 
101ac7ddfbfSEd Maste         m_start_addr = objectFile->GetEntryPointAddress();
102ac7ddfbfSEd Maste         if (!m_start_addr.IsValid())
103ac7ddfbfSEd Maste         {
104ac7ddfbfSEd Maste             m_constructor_errors.Printf ("Could not find entry point address for executable module \"%s\".",
105ac7ddfbfSEd Maste                                          exe_module->GetFileSpec().GetFilename().AsCString());
106ac7ddfbfSEd Maste             if (log)
1070127ef0fSEd Maste                 log->Printf ("ThreadPlanCallFunction(%p): %s.",
1080127ef0fSEd Maste                              static_cast<void*>(this),
1090127ef0fSEd Maste                              m_constructor_errors.GetData());
110ac7ddfbfSEd Maste             return false;
111ac7ddfbfSEd Maste         }
112ac7ddfbfSEd Maste     }
113ac7ddfbfSEd Maste 
114b952cd58SEd Maste     start_load_addr = m_start_addr.GetLoadAddress (&GetTarget());
115ac7ddfbfSEd Maste 
116ac7ddfbfSEd Maste     // Checkpoint the thread state so we can restore it later.
117ac7ddfbfSEd Maste     if (log && log->GetVerbose())
118ac7ddfbfSEd Maste         ReportRegisterState ("About to checkpoint thread before function call.  Original register state was:");
119ac7ddfbfSEd Maste 
120ac7ddfbfSEd Maste     if (!thread.CheckpointThreadState (m_stored_thread_state))
121ac7ddfbfSEd Maste     {
122ac7ddfbfSEd Maste         m_constructor_errors.Printf ("Setting up ThreadPlanCallFunction, failed to checkpoint thread state.");
123ac7ddfbfSEd Maste         if (log)
1240127ef0fSEd Maste             log->Printf ("ThreadPlanCallFunction(%p): %s.",
1250127ef0fSEd Maste                          static_cast<void*>(this),
1260127ef0fSEd Maste                          m_constructor_errors.GetData());
127ac7ddfbfSEd Maste         return false;
128ac7ddfbfSEd Maste     }
129b952cd58SEd Maste     function_load_addr = m_function_addr.GetLoadAddress (&GetTarget());
130ac7ddfbfSEd Maste 
131ac7ddfbfSEd Maste     return true;
132ac7ddfbfSEd Maste }
133ac7ddfbfSEd Maste 
134ac7ddfbfSEd Maste ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
135ac7ddfbfSEd Maste                                                 const Address &function,
1369f2f44ceSEd Maste                                                 const CompilerType &return_type,
137b952cd58SEd Maste                                                 llvm::ArrayRef<addr_t> args,
138b952cd58SEd Maste                                                 const EvaluateExpressionOptions &options) :
139ac7ddfbfSEd Maste     ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
140ac7ddfbfSEd Maste     m_valid (false),
141b952cd58SEd Maste     m_stop_other_threads (options.GetStopOthers()),
142b952cd58SEd Maste     m_unwind_on_error (options.DoesUnwindOnError()),
143b952cd58SEd Maste     m_ignore_breakpoints (options.DoesIgnoreBreakpoints()),
144b952cd58SEd Maste     m_debug_execution (options.GetDebug()),
145b952cd58SEd Maste     m_trap_exceptions (options.GetTrapExceptions()),
146ac7ddfbfSEd Maste     m_function_addr (function),
147ac7ddfbfSEd Maste     m_function_sp (0),
148ac7ddfbfSEd Maste     m_takedown_done (false),
149b952cd58SEd Maste     m_should_clear_objc_exception_bp(false),
150b952cd58SEd Maste     m_should_clear_cxx_exception_bp (false),
151b91a7dfcSDimitry Andric     m_stop_address (LLDB_INVALID_ADDRESS),
152b91a7dfcSDimitry Andric     m_return_type (return_type)
153ac7ddfbfSEd Maste {
154b91a7dfcSDimitry Andric     lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS;
155b91a7dfcSDimitry Andric     lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS;
156b91a7dfcSDimitry Andric     ABI *abi = nullptr;
157b91a7dfcSDimitry Andric 
158ac7ddfbfSEd Maste     if (!ConstructorSetup (thread, abi, start_load_addr, function_load_addr))
159ac7ddfbfSEd Maste         return;
160ac7ddfbfSEd Maste 
161ac7ddfbfSEd Maste     if (!abi->PrepareTrivialCall(thread,
162ac7ddfbfSEd Maste                                  m_function_sp,
163ac7ddfbfSEd Maste                                  function_load_addr,
164ac7ddfbfSEd Maste                                  start_load_addr,
165b952cd58SEd Maste                                  args))
166ac7ddfbfSEd Maste         return;
167ac7ddfbfSEd Maste 
168ac7ddfbfSEd Maste     ReportRegisterState ("Function call was set up.  Register state was:");
169ac7ddfbfSEd Maste 
170ac7ddfbfSEd Maste     m_valid = true;
171ac7ddfbfSEd Maste }
172ac7ddfbfSEd Maste 
173b91a7dfcSDimitry Andric ThreadPlanCallFunction::ThreadPlanCallFunction(Thread &thread,
174b91a7dfcSDimitry Andric                                                const Address &function,
175b91a7dfcSDimitry Andric                                                const EvaluateExpressionOptions &options) :
176b91a7dfcSDimitry Andric     ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
177b91a7dfcSDimitry Andric     m_valid(false),
178b91a7dfcSDimitry Andric     m_stop_other_threads(options.GetStopOthers()),
179b91a7dfcSDimitry Andric     m_unwind_on_error(options.DoesUnwindOnError()),
180b91a7dfcSDimitry Andric     m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
181b91a7dfcSDimitry Andric     m_debug_execution(options.GetDebug()),
182b91a7dfcSDimitry Andric     m_trap_exceptions(options.GetTrapExceptions()),
183b91a7dfcSDimitry Andric     m_function_addr(function),
184b91a7dfcSDimitry Andric     m_function_sp(0),
185b91a7dfcSDimitry Andric     m_takedown_done(false),
186b91a7dfcSDimitry Andric     m_should_clear_objc_exception_bp(false),
187b91a7dfcSDimitry Andric     m_should_clear_cxx_exception_bp(false),
188b91a7dfcSDimitry Andric     m_stop_address(LLDB_INVALID_ADDRESS),
1899f2f44ceSEd Maste     m_return_type(CompilerType())
190b91a7dfcSDimitry Andric {
191b91a7dfcSDimitry Andric }
192b91a7dfcSDimitry Andric 
193ac7ddfbfSEd Maste ThreadPlanCallFunction::~ThreadPlanCallFunction ()
194ac7ddfbfSEd Maste {
195ac7ddfbfSEd Maste     DoTakedown(PlanSucceeded());
196ac7ddfbfSEd Maste }
197ac7ddfbfSEd Maste 
198ac7ddfbfSEd Maste void
199ac7ddfbfSEd Maste ThreadPlanCallFunction::ReportRegisterState (const char *message)
200ac7ddfbfSEd Maste {
201ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_VERBOSE));
202ac7ddfbfSEd Maste     if (log)
203ac7ddfbfSEd Maste     {
204ac7ddfbfSEd Maste         StreamString strm;
205ac7ddfbfSEd Maste         RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
206ac7ddfbfSEd Maste 
207ac7ddfbfSEd Maste         log->PutCString(message);
208ac7ddfbfSEd Maste 
209ac7ddfbfSEd Maste         RegisterValue reg_value;
210ac7ddfbfSEd Maste 
211ac7ddfbfSEd Maste         for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
212ac7ddfbfSEd Maste              reg_idx < num_registers;
213ac7ddfbfSEd Maste              ++reg_idx)
214ac7ddfbfSEd Maste         {
215ac7ddfbfSEd Maste             const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_idx);
216ac7ddfbfSEd Maste             if (reg_ctx->ReadRegister(reg_info, reg_value))
217ac7ddfbfSEd Maste             {
218ac7ddfbfSEd Maste                 reg_value.Dump(&strm, reg_info, true, false, eFormatDefault);
219ac7ddfbfSEd Maste                 strm.EOL();
220ac7ddfbfSEd Maste             }
221ac7ddfbfSEd Maste         }
222ac7ddfbfSEd Maste         log->PutCString(strm.GetData());
223ac7ddfbfSEd Maste     }
224ac7ddfbfSEd Maste }
225ac7ddfbfSEd Maste 
226ac7ddfbfSEd Maste void
227ac7ddfbfSEd Maste ThreadPlanCallFunction::DoTakedown (bool success)
228ac7ddfbfSEd Maste {
229ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
230ac7ddfbfSEd Maste 
231ac7ddfbfSEd Maste     if (!m_valid)
232ac7ddfbfSEd Maste     {
233ac7ddfbfSEd Maste         //Don't call DoTakedown if we were never valid to begin with.
234ac7ddfbfSEd Maste         if (log)
2350127ef0fSEd Maste             log->Printf ("ThreadPlanCallFunction(%p): Log called on ThreadPlanCallFunction that was never valid.",
2360127ef0fSEd Maste                          static_cast<void*>(this));
237ac7ddfbfSEd Maste         return;
238ac7ddfbfSEd Maste     }
239ac7ddfbfSEd Maste 
240ac7ddfbfSEd Maste     if (!m_takedown_done)
241ac7ddfbfSEd Maste     {
242ac7ddfbfSEd Maste         if (success)
243ac7ddfbfSEd Maste         {
244b91a7dfcSDimitry Andric             SetReturnValue();
245ac7ddfbfSEd Maste         }
246ac7ddfbfSEd Maste         if (log)
2470127ef0fSEd Maste             log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
2480127ef0fSEd Maste                          static_cast<void*>(this), m_thread.GetID(), m_valid,
2490127ef0fSEd Maste                          IsPlanComplete());
250ac7ddfbfSEd Maste         m_takedown_done = true;
251ac7ddfbfSEd Maste         m_stop_address = m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
252ac7ddfbfSEd Maste         m_real_stop_info_sp = GetPrivateStopInfo ();
253*afda932aSEd Maste         if (!m_thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state))
254*afda932aSEd Maste         {
255*afda932aSEd Maste             if (log)
2560127ef0fSEd Maste                 log->Printf("ThreadPlanCallFunction(%p): DoTakedown failed to restore register state",
2570127ef0fSEd Maste                             static_cast<void*>(this));
258*afda932aSEd Maste         }
259ac7ddfbfSEd Maste         SetPlanComplete(success);
260ac7ddfbfSEd Maste         ClearBreakpoints();
261ac7ddfbfSEd Maste         if (log && log->GetVerbose())
262ac7ddfbfSEd Maste             ReportRegisterState ("Restoring thread state after function call.  Restored register state:");
263ac7ddfbfSEd Maste     }
264ac7ddfbfSEd Maste     else
265ac7ddfbfSEd Maste     {
266ac7ddfbfSEd Maste         if (log)
2670127ef0fSEd Maste             log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called as no-op for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
2680127ef0fSEd Maste                          static_cast<void*>(this), m_thread.GetID(), m_valid,
2690127ef0fSEd Maste                          IsPlanComplete());
270ac7ddfbfSEd Maste     }
271ac7ddfbfSEd Maste }
272ac7ddfbfSEd Maste 
273ac7ddfbfSEd Maste void
274ac7ddfbfSEd Maste ThreadPlanCallFunction::WillPop ()
275ac7ddfbfSEd Maste {
276ac7ddfbfSEd Maste     DoTakedown(PlanSucceeded());
277ac7ddfbfSEd Maste }
278ac7ddfbfSEd Maste 
279ac7ddfbfSEd Maste void
280ac7ddfbfSEd Maste ThreadPlanCallFunction::GetDescription (Stream *s, DescriptionLevel level)
281ac7ddfbfSEd Maste {
282ac7ddfbfSEd Maste     if (level == eDescriptionLevelBrief)
283ac7ddfbfSEd Maste     {
284ac7ddfbfSEd Maste         s->Printf("Function call thread plan");
285ac7ddfbfSEd Maste     }
286ac7ddfbfSEd Maste     else
287ac7ddfbfSEd Maste     {
288ac7ddfbfSEd Maste         TargetSP target_sp (m_thread.CalculateTarget());
289ac7ddfbfSEd Maste         s->Printf("Thread plan to call 0x%" PRIx64, m_function_addr.GetLoadAddress(target_sp.get()));
290ac7ddfbfSEd Maste     }
291ac7ddfbfSEd Maste }
292ac7ddfbfSEd Maste 
293ac7ddfbfSEd Maste bool
294ac7ddfbfSEd Maste ThreadPlanCallFunction::ValidatePlan (Stream *error)
295ac7ddfbfSEd Maste {
296ac7ddfbfSEd Maste     if (!m_valid)
297ac7ddfbfSEd Maste     {
298ac7ddfbfSEd Maste         if (error)
299ac7ddfbfSEd Maste         {
300ac7ddfbfSEd Maste             if (m_constructor_errors.GetSize() > 0)
301ac7ddfbfSEd Maste                 error->PutCString (m_constructor_errors.GetData());
302ac7ddfbfSEd Maste             else
303ac7ddfbfSEd Maste                 error->PutCString ("Unknown error");
304ac7ddfbfSEd Maste         }
305ac7ddfbfSEd Maste         return false;
306ac7ddfbfSEd Maste     }
307ac7ddfbfSEd Maste 
308ac7ddfbfSEd Maste     return true;
309ac7ddfbfSEd Maste }
310ac7ddfbfSEd Maste 
311ac7ddfbfSEd Maste Vote
312ac7ddfbfSEd Maste ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr)
313ac7ddfbfSEd Maste {
314ac7ddfbfSEd Maste     if (m_takedown_done || IsPlanComplete())
315ac7ddfbfSEd Maste         return eVoteYes;
316ac7ddfbfSEd Maste     else
317ac7ddfbfSEd Maste         return ThreadPlan::ShouldReportStop(event_ptr);
318ac7ddfbfSEd Maste }
319ac7ddfbfSEd Maste 
320ac7ddfbfSEd Maste bool
321ac7ddfbfSEd Maste ThreadPlanCallFunction::DoPlanExplainsStop (Event *event_ptr)
322ac7ddfbfSEd Maste {
323ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP|LIBLLDB_LOG_PROCESS));
324ac7ddfbfSEd Maste     m_real_stop_info_sp = GetPrivateStopInfo ();
325ac7ddfbfSEd Maste 
326ac7ddfbfSEd Maste     // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
327ac7ddfbfSEd Maste     // we answer yes.
3289f2f44ceSEd Maste     if (m_subplan_sp && m_subplan_sp->PlanExplainsStop(event_ptr))
329ac7ddfbfSEd Maste     {
330ac7ddfbfSEd Maste         SetPlanComplete();
331ac7ddfbfSEd Maste         return true;
332ac7ddfbfSEd Maste     }
333ac7ddfbfSEd Maste 
334ac7ddfbfSEd Maste     // Check if the breakpoint is one of ours.
335ac7ddfbfSEd Maste 
336ac7ddfbfSEd Maste     StopReason stop_reason;
337ac7ddfbfSEd Maste     if (!m_real_stop_info_sp)
338ac7ddfbfSEd Maste         stop_reason = eStopReasonNone;
339ac7ddfbfSEd Maste     else
340ac7ddfbfSEd Maste         stop_reason = m_real_stop_info_sp->GetStopReason();
341ac7ddfbfSEd Maste     if (log)
342ac7ddfbfSEd Maste         log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - %s.", Thread::StopReasonAsCString(stop_reason));
343ac7ddfbfSEd Maste 
344ac7ddfbfSEd Maste     if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
345ac7ddfbfSEd Maste         return true;
346ac7ddfbfSEd Maste 
3470127ef0fSEd Maste     // One more quirk here.  If this event was from Halt interrupting the target, then we should not consider
3480127ef0fSEd Maste     // ourselves complete.  Return true to acknowledge the stop.
3490127ef0fSEd Maste     if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr))
3500127ef0fSEd Maste     {
3510127ef0fSEd Maste         if (log)
3520127ef0fSEd Maste             log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: The event is an Interrupt, returning true.");
3530127ef0fSEd Maste         return true;
3540127ef0fSEd Maste     }
355ac7ddfbfSEd Maste     // We control breakpoints separately from other "stop reasons."  So first,
356ac7ddfbfSEd Maste     // check the case where we stopped for an internal breakpoint, in that case, continue on.
357ac7ddfbfSEd Maste     // If it is not an internal breakpoint, consult m_ignore_breakpoints.
358ac7ddfbfSEd Maste 
359ac7ddfbfSEd Maste     if (stop_reason == eStopReasonBreakpoint)
360ac7ddfbfSEd Maste     {
361ac7ddfbfSEd Maste         ProcessSP process_sp (m_thread.CalculateProcess());
362ac7ddfbfSEd Maste         uint64_t break_site_id = m_real_stop_info_sp->GetValue();
363ac7ddfbfSEd Maste         BreakpointSiteSP bp_site_sp;
364ac7ddfbfSEd Maste         if (process_sp)
365ac7ddfbfSEd Maste             bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
366ac7ddfbfSEd Maste         if (bp_site_sp)
367ac7ddfbfSEd Maste         {
368ac7ddfbfSEd Maste             uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
369ac7ddfbfSEd Maste             bool is_internal = true;
370ac7ddfbfSEd Maste             for (uint32_t i = 0; i < num_owners; i++)
371ac7ddfbfSEd Maste             {
372ac7ddfbfSEd Maste                 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
373ac7ddfbfSEd Maste                 if (log)
374ac7ddfbfSEd Maste                     log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: hit breakpoint %d while calling function", bp.GetID());
375ac7ddfbfSEd Maste 
376ac7ddfbfSEd Maste                 if (!bp.IsInternal())
377ac7ddfbfSEd Maste                 {
378ac7ddfbfSEd Maste                     is_internal = false;
379ac7ddfbfSEd Maste                     break;
380ac7ddfbfSEd Maste                 }
381ac7ddfbfSEd Maste             }
382ac7ddfbfSEd Maste             if (is_internal)
383ac7ddfbfSEd Maste             {
384ac7ddfbfSEd Maste                 if (log)
385ac7ddfbfSEd Maste                     log->Printf ("ThreadPlanCallFunction::PlanExplainsStop hit an internal breakpoint, not stopping.");
386ac7ddfbfSEd Maste                 return false;
387ac7ddfbfSEd Maste             }
388ac7ddfbfSEd Maste         }
389ac7ddfbfSEd Maste 
390ac7ddfbfSEd Maste         if (m_ignore_breakpoints)
391ac7ddfbfSEd Maste         {
392ac7ddfbfSEd Maste             if (log)
393ac7ddfbfSEd Maste                 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
394ac7ddfbfSEd Maste             m_real_stop_info_sp->OverrideShouldStop(false);
395ac7ddfbfSEd Maste             return true;
396ac7ddfbfSEd Maste         }
397ac7ddfbfSEd Maste         else
398ac7ddfbfSEd Maste         {
399ac7ddfbfSEd Maste             if (log)
400ac7ddfbfSEd Maste                 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are not ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
401ac7ddfbfSEd Maste             m_real_stop_info_sp->OverrideShouldStop(true);
402ac7ddfbfSEd Maste             return false;
403ac7ddfbfSEd Maste         }
404ac7ddfbfSEd Maste     }
405ac7ddfbfSEd Maste     else if (!m_unwind_on_error)
406ac7ddfbfSEd Maste     {
407ac7ddfbfSEd Maste         // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
408ac7ddfbfSEd Maste         return false;
409ac7ddfbfSEd Maste     }
410ac7ddfbfSEd Maste     else
411ac7ddfbfSEd Maste     {
412ac7ddfbfSEd Maste         // If the subplan is running, any crashes are attributable to us.
413ac7ddfbfSEd Maste         // If we want to discard the plan, then we say we explain the stop
414ac7ddfbfSEd Maste         // but if we are going to be discarded, let whoever is above us
415ac7ddfbfSEd Maste         // explain the stop.
416ac7ddfbfSEd Maste         // But don't discard the plan if the stop would restart itself (for instance if it is a
417ac7ddfbfSEd Maste         // signal that is set not to stop.  Check that here first.  We just say we explain the stop
418ac7ddfbfSEd Maste         // but aren't done and everything will continue on from there.
419ac7ddfbfSEd Maste 
4201c3bbb01SEd Maste         if (m_real_stop_info_sp && m_real_stop_info_sp->ShouldStopSynchronous(event_ptr))
421ac7ddfbfSEd Maste         {
422ac7ddfbfSEd Maste             SetPlanComplete(false);
4239f2f44ceSEd Maste             return m_subplan_sp ? m_unwind_on_error : false;
424ac7ddfbfSEd Maste         }
425ac7ddfbfSEd Maste         else
426ac7ddfbfSEd Maste             return true;
427ac7ddfbfSEd Maste     }
428ac7ddfbfSEd Maste }
429ac7ddfbfSEd Maste 
430ac7ddfbfSEd Maste bool
431ac7ddfbfSEd Maste ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
432ac7ddfbfSEd Maste {
433ac7ddfbfSEd Maste     // We do some computation in DoPlanExplainsStop that may or may not set the plan as complete.
434ac7ddfbfSEd Maste     // We need to do that here to make sure our state is correct.
435ac7ddfbfSEd Maste     DoPlanExplainsStop(event_ptr);
436ac7ddfbfSEd Maste 
437ac7ddfbfSEd Maste     if (IsPlanComplete())
438ac7ddfbfSEd Maste     {
439ac7ddfbfSEd Maste         ReportRegisterState ("Function completed.  Register state was:");
440ac7ddfbfSEd Maste         return true;
441ac7ddfbfSEd Maste     }
442ac7ddfbfSEd Maste     else
443ac7ddfbfSEd Maste     {
444ac7ddfbfSEd Maste         return false;
445ac7ddfbfSEd Maste     }
446ac7ddfbfSEd Maste }
447ac7ddfbfSEd Maste 
448ac7ddfbfSEd Maste bool
449ac7ddfbfSEd Maste ThreadPlanCallFunction::StopOthers ()
450ac7ddfbfSEd Maste {
451ac7ddfbfSEd Maste     return m_stop_other_threads;
452ac7ddfbfSEd Maste }
453ac7ddfbfSEd Maste 
454ac7ddfbfSEd Maste StateType
455ac7ddfbfSEd Maste ThreadPlanCallFunction::GetPlanRunState ()
456ac7ddfbfSEd Maste {
457ac7ddfbfSEd Maste     return eStateRunning;
458ac7ddfbfSEd Maste }
459ac7ddfbfSEd Maste 
460ac7ddfbfSEd Maste void
461ac7ddfbfSEd Maste ThreadPlanCallFunction::DidPush ()
462ac7ddfbfSEd Maste {
463ac7ddfbfSEd Maste //#define SINGLE_STEP_EXPRESSIONS
464ac7ddfbfSEd Maste 
465ac7ddfbfSEd Maste     // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
466ac7ddfbfSEd Maste     // Wait till the plan is pushed so we aren't changing the stop info till we're about to run.
467ac7ddfbfSEd Maste 
468ac7ddfbfSEd Maste     GetThread().SetStopInfoToNothing();
469ac7ddfbfSEd Maste 
470ac7ddfbfSEd Maste #ifndef SINGLE_STEP_EXPRESSIONS
471ac7ddfbfSEd Maste     m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
472ac7ddfbfSEd Maste 
473ac7ddfbfSEd Maste     m_thread.QueueThreadPlan(m_subplan_sp, false);
474ac7ddfbfSEd Maste     m_subplan_sp->SetPrivate (true);
475ac7ddfbfSEd Maste #endif
476ac7ddfbfSEd Maste }
477ac7ddfbfSEd Maste 
478ac7ddfbfSEd Maste bool
479ac7ddfbfSEd Maste ThreadPlanCallFunction::WillStop ()
480ac7ddfbfSEd Maste {
481ac7ddfbfSEd Maste     return true;
482ac7ddfbfSEd Maste }
483ac7ddfbfSEd Maste 
484ac7ddfbfSEd Maste bool
485ac7ddfbfSEd Maste ThreadPlanCallFunction::MischiefManaged ()
486ac7ddfbfSEd Maste {
487ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
488ac7ddfbfSEd Maste 
489ac7ddfbfSEd Maste     if (IsPlanComplete())
490ac7ddfbfSEd Maste     {
491ac7ddfbfSEd Maste         if (log)
4920127ef0fSEd Maste             log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.",
4930127ef0fSEd Maste                         static_cast<void*>(this));
494ac7ddfbfSEd Maste 
495ac7ddfbfSEd Maste         ThreadPlan::MischiefManaged ();
496ac7ddfbfSEd Maste         return true;
497ac7ddfbfSEd Maste     }
498ac7ddfbfSEd Maste     else
499ac7ddfbfSEd Maste     {
500ac7ddfbfSEd Maste         return false;
501ac7ddfbfSEd Maste     }
502ac7ddfbfSEd Maste }
503ac7ddfbfSEd Maste 
504ac7ddfbfSEd Maste void
505ac7ddfbfSEd Maste ThreadPlanCallFunction::SetBreakpoints ()
506ac7ddfbfSEd Maste {
507ac7ddfbfSEd Maste     ProcessSP process_sp (m_thread.CalculateProcess());
508b952cd58SEd Maste     if (m_trap_exceptions && process_sp)
509ac7ddfbfSEd Maste     {
510ac7ddfbfSEd Maste         m_cxx_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus);
511ac7ddfbfSEd Maste         m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC);
512ac7ddfbfSEd Maste 
513ac7ddfbfSEd Maste         if (m_cxx_language_runtime)
514b952cd58SEd Maste         {
515b952cd58SEd Maste             m_should_clear_cxx_exception_bp = !m_cxx_language_runtime->ExceptionBreakpointsAreSet();
516ac7ddfbfSEd Maste             m_cxx_language_runtime->SetExceptionBreakpoints();
517b952cd58SEd Maste         }
518ac7ddfbfSEd Maste         if (m_objc_language_runtime)
519b952cd58SEd Maste         {
520b952cd58SEd Maste             m_should_clear_objc_exception_bp = !m_objc_language_runtime->ExceptionBreakpointsAreSet();
521ac7ddfbfSEd Maste             m_objc_language_runtime->SetExceptionBreakpoints();
522ac7ddfbfSEd Maste         }
523ac7ddfbfSEd Maste     }
524b952cd58SEd Maste }
525ac7ddfbfSEd Maste 
526ac7ddfbfSEd Maste void
527ac7ddfbfSEd Maste ThreadPlanCallFunction::ClearBreakpoints ()
528ac7ddfbfSEd Maste {
529b952cd58SEd Maste     if (m_trap_exceptions)
530b952cd58SEd Maste     {
531b952cd58SEd Maste         if (m_cxx_language_runtime && m_should_clear_cxx_exception_bp)
532ac7ddfbfSEd Maste             m_cxx_language_runtime->ClearExceptionBreakpoints();
533b952cd58SEd Maste         if (m_objc_language_runtime && m_should_clear_objc_exception_bp)
534ac7ddfbfSEd Maste             m_objc_language_runtime->ClearExceptionBreakpoints();
535ac7ddfbfSEd Maste     }
536b952cd58SEd Maste }
537ac7ddfbfSEd Maste 
538ac7ddfbfSEd Maste bool
539ac7ddfbfSEd Maste ThreadPlanCallFunction::BreakpointsExplainStop()
540ac7ddfbfSEd Maste {
541ac7ddfbfSEd Maste     StopInfoSP stop_info_sp = GetPrivateStopInfo ();
542ac7ddfbfSEd Maste 
543b952cd58SEd Maste     if (m_trap_exceptions)
544b952cd58SEd Maste     {
545ac7ddfbfSEd Maste         if ((m_cxx_language_runtime &&
546ac7ddfbfSEd Maste                 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
547ac7ddfbfSEd Maste            ||(m_objc_language_runtime &&
548ac7ddfbfSEd Maste                 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp)))
549ac7ddfbfSEd Maste         {
550ac7ddfbfSEd Maste             Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
551ac7ddfbfSEd Maste             if (log)
552ac7ddfbfSEd Maste                 log->Printf ("ThreadPlanCallFunction::BreakpointsExplainStop - Hit an exception breakpoint, setting plan complete.");
553ac7ddfbfSEd Maste 
554ac7ddfbfSEd Maste             SetPlanComplete(false);
555ac7ddfbfSEd Maste 
556ac7ddfbfSEd Maste             // If the user has set the ObjC language breakpoint, it would normally get priority over our internal
557ac7ddfbfSEd Maste             // catcher breakpoint, but in this case we can't let that happen, so force the ShouldStop here.
558ac7ddfbfSEd Maste             stop_info_sp->OverrideShouldStop (true);
559ac7ddfbfSEd Maste             return true;
560ac7ddfbfSEd Maste         }
561b952cd58SEd Maste     }
562ac7ddfbfSEd Maste 
563ac7ddfbfSEd Maste     return false;
564ac7ddfbfSEd Maste }
565ac7ddfbfSEd Maste 
5660127ef0fSEd Maste void
5670127ef0fSEd Maste ThreadPlanCallFunction::SetStopOthers (bool new_value)
5680127ef0fSEd Maste {
5690127ef0fSEd Maste     m_subplan_sp->SetStopOthers(new_value);
5700127ef0fSEd Maste }
5710127ef0fSEd Maste 
572ac7ddfbfSEd Maste bool
573ac7ddfbfSEd Maste ThreadPlanCallFunction::RestoreThreadState()
574ac7ddfbfSEd Maste {
575ac7ddfbfSEd Maste     return GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state);
576ac7ddfbfSEd Maste }
577ac7ddfbfSEd Maste 
578b91a7dfcSDimitry Andric void
579b91a7dfcSDimitry Andric ThreadPlanCallFunction::SetReturnValue()
580b91a7dfcSDimitry Andric {
581b91a7dfcSDimitry Andric     ProcessSP process_sp(m_thread.GetProcess());
5829f2f44ceSEd Maste     const ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
583b91a7dfcSDimitry Andric     if (abi && m_return_type.IsValid())
584b91a7dfcSDimitry Andric     {
585b91a7dfcSDimitry Andric         const bool persistent = false;
586b91a7dfcSDimitry Andric         m_return_valobj_sp = abi->GetReturnValueObject(m_thread, m_return_type, persistent);
587b91a7dfcSDimitry Andric     }
588b91a7dfcSDimitry Andric }
589