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