130fdc8d8SChris Lattner //===-- ThreadPlanCallFunction.cpp ------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1030fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanCallFunction.h"
1130fdc8d8SChris Lattner 
1230fdc8d8SChris Lattner // C Includes
1330fdc8d8SChris Lattner // C++ Includes
1430fdc8d8SChris Lattner // Other libraries and framework includes
1546d005dbSJim Ingham 
1630fdc8d8SChris Lattner // Project includes
1730fdc8d8SChris Lattner #include "lldb/lldb-private-log.h"
1840d871faSJim Ingham #include "lldb/Breakpoint/Breakpoint.h"
1940d871faSJim Ingham #include "lldb/Breakpoint/BreakpointLocation.h"
2030fdc8d8SChris Lattner #include "lldb/Core/Address.h"
2130fdc8d8SChris Lattner #include "lldb/Core/Log.h"
221f746071SGreg Clayton #include "lldb/Core/Module.h"
2330fdc8d8SChris Lattner #include "lldb/Core/Stream.h"
241f746071SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
25f211510fSSean Callanan #include "lldb/Target/LanguageRuntime.h"
2630fdc8d8SChris Lattner #include "lldb/Target/Process.h"
2730fdc8d8SChris Lattner #include "lldb/Target/RegisterContext.h"
2840d871faSJim Ingham #include "lldb/Target/StopInfo.h"
2930fdc8d8SChris Lattner #include "lldb/Target/Target.h"
3030fdc8d8SChris Lattner #include "lldb/Target/Thread.h"
3130fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanRunToAddress.h"
3230fdc8d8SChris Lattner 
3330fdc8d8SChris Lattner using namespace lldb;
3430fdc8d8SChris Lattner using namespace lldb_private;
3530fdc8d8SChris Lattner 
3630fdc8d8SChris Lattner //----------------------------------------------------------------------
3730fdc8d8SChris Lattner // ThreadPlanCallFunction: Plan to call a single function
3830fdc8d8SChris Lattner //----------------------------------------------------------------------
390092c8ebSJim Ingham bool
400092c8ebSJim Ingham ThreadPlanCallFunction::ConstructorSetup (Thread &thread,
410092c8ebSJim Ingham                                           ABI *& abi,
420092c8ebSJim Ingham                                           lldb::addr_t &start_load_addr,
430092c8ebSJim Ingham                                           lldb::addr_t &function_load_addr)
440092c8ebSJim Ingham {
450092c8ebSJim Ingham     SetIsMasterPlan (true);
46923886ceSJim Ingham     SetOkayToDiscard (false);
47327c267aSAndrew Kaylor     SetPrivate (true);
480092c8ebSJim Ingham 
490092c8ebSJim Ingham     ProcessSP process_sp (thread.GetProcess());
500092c8ebSJim Ingham     if (!process_sp)
510092c8ebSJim Ingham         return false;
520092c8ebSJim Ingham 
530092c8ebSJim Ingham     abi = process_sp->GetABI().get();
540092c8ebSJim Ingham 
550092c8ebSJim Ingham     if (!abi)
560092c8ebSJim Ingham         return false;
570092c8ebSJim Ingham 
585160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
590092c8ebSJim Ingham 
600092c8ebSJim Ingham     SetBreakpoints();
610092c8ebSJim Ingham 
620092c8ebSJim Ingham     m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
630092c8ebSJim Ingham     // If we can't read memory at the point of the process where we are planning to put our function, we're
640092c8ebSJim Ingham     // not going to get any further...
650092c8ebSJim Ingham     Error error;
660092c8ebSJim Ingham     process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error);
670092c8ebSJim Ingham     if (!error.Success())
680092c8ebSJim Ingham     {
69ea06f3bfSJim Ingham         m_constructor_errors.Printf ("Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".", m_function_sp);
700092c8ebSJim Ingham         if (log)
71*324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): %s.",
72*324a1036SSaleem Abdulrasool                          static_cast<void*>(this),
73*324a1036SSaleem Abdulrasool                          m_constructor_errors.GetData());
740092c8ebSJim Ingham         return false;
750092c8ebSJim Ingham     }
760092c8ebSJim Ingham 
776fbc48bcSJim Ingham     Module *exe_module = GetTarget().GetExecutableModulePointer();
780092c8ebSJim Ingham 
790092c8ebSJim Ingham     if (exe_module == NULL)
800092c8ebSJim Ingham     {
81ea06f3bfSJim Ingham         m_constructor_errors.Printf ("Can't execute code without an executable module.");
820092c8ebSJim Ingham         if (log)
83*324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): %s.",
84*324a1036SSaleem Abdulrasool                          static_cast<void*>(this),
85*324a1036SSaleem Abdulrasool                          m_constructor_errors.GetData());
860092c8ebSJim Ingham         return false;
870092c8ebSJim Ingham     }
880092c8ebSJim Ingham     else
890092c8ebSJim Ingham     {
900092c8ebSJim Ingham         ObjectFile *objectFile = exe_module->GetObjectFile();
910092c8ebSJim Ingham         if (!objectFile)
920092c8ebSJim Ingham         {
93ea06f3bfSJim Ingham             m_constructor_errors.Printf ("Could not find object file for module \"%s\".",
94ea06f3bfSJim Ingham                                          exe_module->GetFileSpec().GetFilename().AsCString());
95ea06f3bfSJim Ingham 
960092c8ebSJim Ingham             if (log)
97*324a1036SSaleem Abdulrasool                 log->Printf ("ThreadPlanCallFunction(%p): %s.",
98*324a1036SSaleem Abdulrasool                              static_cast<void*>(this),
99*324a1036SSaleem Abdulrasool                              m_constructor_errors.GetData());
1000092c8ebSJim Ingham             return false;
1010092c8ebSJim Ingham         }
102ea06f3bfSJim Ingham 
1030092c8ebSJim Ingham         m_start_addr = objectFile->GetEntryPointAddress();
1040092c8ebSJim Ingham         if (!m_start_addr.IsValid())
1050092c8ebSJim Ingham         {
106ea06f3bfSJim Ingham             m_constructor_errors.Printf ("Could not find entry point address for executable module \"%s\".",
107ea06f3bfSJim Ingham                                          exe_module->GetFileSpec().GetFilename().AsCString());
1080092c8ebSJim Ingham             if (log)
109*324a1036SSaleem Abdulrasool                 log->Printf ("ThreadPlanCallFunction(%p): %s.",
110*324a1036SSaleem Abdulrasool                              static_cast<void*>(this),
111*324a1036SSaleem Abdulrasool                              m_constructor_errors.GetData());
1120092c8ebSJim Ingham             return false;
1130092c8ebSJim Ingham         }
1140092c8ebSJim Ingham     }
1150092c8ebSJim Ingham 
1166fbc48bcSJim Ingham     start_load_addr = m_start_addr.GetLoadAddress (&GetTarget());
1170092c8ebSJim Ingham 
1180092c8ebSJim Ingham     // Checkpoint the thread state so we can restore it later.
1190092c8ebSJim Ingham     if (log && log->GetVerbose())
1200092c8ebSJim Ingham         ReportRegisterState ("About to checkpoint thread before function call.  Original register state was:");
1210092c8ebSJim Ingham 
1220092c8ebSJim Ingham     if (!thread.CheckpointThreadState (m_stored_thread_state))
1230092c8ebSJim Ingham     {
124ea06f3bfSJim Ingham         m_constructor_errors.Printf ("Setting up ThreadPlanCallFunction, failed to checkpoint thread state.");
1250092c8ebSJim Ingham         if (log)
126*324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): %s.",
127*324a1036SSaleem Abdulrasool                          static_cast<void*>(this),
128*324a1036SSaleem Abdulrasool                          m_constructor_errors.GetData());
1290092c8ebSJim Ingham         return false;
1300092c8ebSJim Ingham     }
1316fbc48bcSJim Ingham     function_load_addr = m_function_addr.GetLoadAddress (&GetTarget());
1320092c8ebSJim Ingham 
1330092c8ebSJim Ingham     return true;
1340092c8ebSJim Ingham }
13530fdc8d8SChris Lattner 
13630fdc8d8SChris Lattner ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
13700049b8bSMatt Kopec                                                 const Address &function,
138ef651600SJim Ingham                                                 const ClangASTType &return_type,
139a464f3d4SSean Callanan                                                 llvm::ArrayRef<addr_t> args,
140a464f3d4SSean Callanan                                                 const EvaluateExpressionOptions &options) :
141b01e742aSJim Ingham     ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
14230fdc8d8SChris Lattner     m_valid (false),
1436fbc48bcSJim Ingham     m_stop_other_threads (options.GetStopOthers()),
1446fbc48bcSJim Ingham     m_unwind_on_error (options.DoesUnwindOnError()),
1456fbc48bcSJim Ingham     m_ignore_breakpoints (options.DoesIgnoreBreakpoints()),
1466fbc48bcSJim Ingham     m_debug_execution (options.GetDebug()),
1476fbc48bcSJim Ingham     m_trap_exceptions (options.GetTrapExceptions()),
14816e0c686SJim Ingham     m_function_addr (function),
149b4cb0be3SFilipe Cabecinhas     m_function_sp (0),
150ef651600SJim Ingham     m_return_type (return_type),
151ce553d88SJim Ingham     m_takedown_done (false),
1526fbc48bcSJim Ingham     m_should_clear_objc_exception_bp(false),
1536fbc48bcSJim Ingham     m_should_clear_cxx_exception_bp (false),
1546fbc48bcSJim Ingham     m_stop_address (LLDB_INVALID_ADDRESS)
15530fdc8d8SChris Lattner {
1560092c8ebSJim Ingham     lldb::addr_t start_load_addr;
1570092c8ebSJim Ingham     ABI *abi;
1580092c8ebSJim Ingham     lldb::addr_t function_load_addr;
159923886ceSJim Ingham     if (!ConstructorSetup (thread, abi, start_load_addr, function_load_addr))
1601ac04c30SGreg Clayton         return;
1611ac04c30SGreg Clayton 
16230fdc8d8SChris Lattner     if (!abi->PrepareTrivialCall(thread,
163e359d9b7SSean Callanan                                  m_function_sp,
1640092c8ebSJim Ingham                                  function_load_addr,
1652a48f525SGreg Clayton                                  start_load_addr,
166a464f3d4SSean Callanan                                  args))
16730fdc8d8SChris Lattner         return;
16830fdc8d8SChris Lattner 
1699da3683cSJim Ingham     ReportRegisterState ("Function call was set up.  Register state was:");
1709da3683cSJim Ingham 
1719da3683cSJim Ingham     m_valid = true;
1729da3683cSJim Ingham }
1739da3683cSJim Ingham 
1749da3683cSJim Ingham ThreadPlanCallFunction::~ThreadPlanCallFunction ()
1759da3683cSJim Ingham {
1760161b49cSJim Ingham     DoTakedown(PlanSucceeded());
1779da3683cSJim Ingham }
1789da3683cSJim Ingham 
1799da3683cSJim Ingham void
1809da3683cSJim Ingham ThreadPlanCallFunction::ReportRegisterState (const char *message)
1819da3683cSJim Ingham {
1825160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_VERBOSE));
183ece96492SSean Callanan     if (log)
184ece96492SSean Callanan     {
185af247d7bSGreg Clayton         StreamString strm;
1865ccbd294SGreg Clayton         RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
187ece96492SSean Callanan 
1889da3683cSJim Ingham         log->PutCString(message);
189ece96492SSean Callanan 
190af247d7bSGreg Clayton         RegisterValue reg_value;
191ece96492SSean Callanan 
192af247d7bSGreg Clayton         for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
193af247d7bSGreg Clayton              reg_idx < num_registers;
194af247d7bSGreg Clayton              ++reg_idx)
195af247d7bSGreg Clayton         {
196af247d7bSGreg Clayton             const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_idx);
197af247d7bSGreg Clayton             if (reg_ctx->ReadRegister(reg_info, reg_value))
198af247d7bSGreg Clayton             {
199af247d7bSGreg Clayton                 reg_value.Dump(&strm, reg_info, true, false, eFormatDefault);
200af247d7bSGreg Clayton                 strm.EOL();
201ece96492SSean Callanan             }
202ece96492SSean Callanan         }
203af247d7bSGreg Clayton         log->PutCString(strm.GetData());
204af247d7bSGreg Clayton     }
20510af7c43SSean Callanan }
20610af7c43SSean Callanan 
20710af7c43SSean Callanan void
20818de2fdcSJim Ingham ThreadPlanCallFunction::DoTakedown (bool success)
20910af7c43SSean Callanan {
2105160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
21187d0e618SJim Ingham 
21287d0e618SJim Ingham     if (!m_valid)
21387d0e618SJim Ingham     {
21487d0e618SJim Ingham         //Don't call DoTakedown if we were never valid to begin with.
21587d0e618SJim Ingham         if (log)
216*324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): Log called on ThreadPlanCallFunction that was never valid.",
217*324a1036SSaleem Abdulrasool                          static_cast<void*>(this));
21887d0e618SJim Ingham         return;
21987d0e618SJim Ingham     }
22087d0e618SJim Ingham 
2219da3683cSJim Ingham     if (!m_takedown_done)
22277787033SJim Ingham     {
22318de2fdcSJim Ingham         if (success)
22418de2fdcSJim Ingham         {
2251ac04c30SGreg Clayton             ProcessSP process_sp (m_thread.GetProcess());
2261ac04c30SGreg Clayton             const ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
227ef651600SJim Ingham             if (abi && m_return_type.IsValid())
228ef651600SJim Ingham             {
2296153c518SSean Callanan                 const bool persistent = false;
2306153c518SSean Callanan                 m_return_valobj_sp = abi->GetReturnValueObject (m_thread, m_return_type, persistent);
23170b57657SGreg Clayton             }
23218de2fdcSJim Ingham         }
2339da3683cSJim Ingham         if (log)
234*324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
235*324a1036SSaleem Abdulrasool                          static_cast<void*>(this), m_thread.GetID(), m_valid,
236*324a1036SSaleem Abdulrasool                          IsPlanComplete());
2379da3683cSJim Ingham         m_takedown_done = true;
238ce553d88SJim Ingham         m_stop_address = m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
23960c4118cSJim Ingham         m_real_stop_info_sp = GetPrivateStopInfo ();
2407b24e95eSEd Maste         if (!m_thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state))
2417b24e95eSEd Maste         {
2427b24e95eSEd Maste             if (log)
243*324a1036SSaleem Abdulrasool                 log->Printf("ThreadPlanCallFunction(%p): DoTakedown failed to restore register state",
244*324a1036SSaleem Abdulrasool                             static_cast<void*>(this));
2457b24e95eSEd Maste         }
24618de2fdcSJim Ingham         SetPlanComplete(success);
24710af7c43SSean Callanan         ClearBreakpoints();
2489da3683cSJim Ingham         if (log && log->GetVerbose())
2499da3683cSJim Ingham             ReportRegisterState ("Restoring thread state after function call.  Restored register state:");
2502c36439cSJim Ingham 
2519da3683cSJim Ingham     }
2529da3683cSJim Ingham     else
2539da3683cSJim Ingham     {
2549da3683cSJim Ingham         if (log)
255*324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called as no-op for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
256*324a1036SSaleem Abdulrasool                          static_cast<void*>(this), m_thread.GetID(), m_valid,
257*324a1036SSaleem Abdulrasool                          IsPlanComplete());
25830fdc8d8SChris Lattner     }
25977787033SJim Ingham }
26030fdc8d8SChris Lattner 
26130fdc8d8SChris Lattner void
262bda4e5ebSJim Ingham ThreadPlanCallFunction::WillPop ()
263bda4e5ebSJim Ingham {
2640161b49cSJim Ingham     DoTakedown(PlanSucceeded());
265bda4e5ebSJim Ingham }
266bda4e5ebSJim Ingham 
267bda4e5ebSJim Ingham void
2682a48f525SGreg Clayton ThreadPlanCallFunction::GetDescription (Stream *s, DescriptionLevel level)
26930fdc8d8SChris Lattner {
2702a48f525SGreg Clayton     if (level == eDescriptionLevelBrief)
27130fdc8d8SChris Lattner     {
27230fdc8d8SChris Lattner         s->Printf("Function call thread plan");
27330fdc8d8SChris Lattner     }
27430fdc8d8SChris Lattner     else
27530fdc8d8SChris Lattner     {
2761ac04c30SGreg Clayton         TargetSP target_sp (m_thread.CalculateTarget());
277d01b2953SDaniel Malea         s->Printf("Thread plan to call 0x%" PRIx64, m_function_addr.GetLoadAddress(target_sp.get()));
27830fdc8d8SChris Lattner     }
27930fdc8d8SChris Lattner }
28030fdc8d8SChris Lattner 
28130fdc8d8SChris Lattner bool
28230fdc8d8SChris Lattner ThreadPlanCallFunction::ValidatePlan (Stream *error)
28330fdc8d8SChris Lattner {
28430fdc8d8SChris Lattner     if (!m_valid)
285ea06f3bfSJim Ingham     {
286ea06f3bfSJim Ingham         if (error)
287ea06f3bfSJim Ingham         {
288ea06f3bfSJim Ingham             if (m_constructor_errors.GetSize() > 0)
289ea06f3bfSJim Ingham                 error->PutCString (m_constructor_errors.GetData());
290ea06f3bfSJim Ingham             else
291ea06f3bfSJim Ingham                 error->PutCString ("Unknown error");
292ea06f3bfSJim Ingham         }
29330fdc8d8SChris Lattner         return false;
294ea06f3bfSJim Ingham     }
29530fdc8d8SChris Lattner 
29630fdc8d8SChris Lattner     return true;
29730fdc8d8SChris Lattner }
29830fdc8d8SChris Lattner 
2990161b49cSJim Ingham 
3000161b49cSJim Ingham Vote
3010161b49cSJim Ingham ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr)
30230fdc8d8SChris Lattner {
3030161b49cSJim Ingham     if (m_takedown_done || IsPlanComplete())
3040161b49cSJim Ingham         return eVoteYes;
3050161b49cSJim Ingham     else
3060161b49cSJim Ingham         return ThreadPlan::ShouldReportStop(event_ptr);
3070161b49cSJim Ingham }
3080161b49cSJim Ingham 
3090161b49cSJim Ingham bool
310221d51cfSJim Ingham ThreadPlanCallFunction::DoPlanExplainsStop (Event *event_ptr)
3110161b49cSJim Ingham {
3125160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP|LIBLLDB_LOG_PROCESS));
31360c4118cSJim Ingham     m_real_stop_info_sp = GetPrivateStopInfo ();
314160f78c5SJim Ingham 
31540d871faSJim Ingham     // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
31640d871faSJim Ingham     // we answer yes.
3170161b49cSJim Ingham     if (m_subplan_sp.get() != NULL && m_subplan_sp->PlanExplainsStop(event_ptr))
318184e9811SJim Ingham     {
319184e9811SJim Ingham         SetPlanComplete();
32040d871faSJim Ingham         return true;
321184e9811SJim Ingham     }
3223e6fedcaSSean Callanan 
323c98aca60SSean Callanan     // Check if the breakpoint is one of ours.
324c98aca60SSean Callanan 
32518de2fdcSJim Ingham     StopReason stop_reason;
32618de2fdcSJim Ingham     if (!m_real_stop_info_sp)
32718de2fdcSJim Ingham         stop_reason = eStopReasonNone;
32818de2fdcSJim Ingham     else
32918de2fdcSJim Ingham         stop_reason = m_real_stop_info_sp->GetStopReason();
3300161b49cSJim Ingham     if (log)
3310161b49cSJim Ingham         log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - %s.", Thread::StopReasonAsCString(stop_reason));
33218de2fdcSJim Ingham 
33318de2fdcSJim Ingham     if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
334c98aca60SSean Callanan         return true;
335c98aca60SSean Callanan 
3360161b49cSJim Ingham     // We control breakpoints separately from other "stop reasons."  So first,
3370161b49cSJim Ingham     // check the case where we stopped for an internal breakpoint, in that case, continue on.
3380161b49cSJim Ingham     // If it is not an internal breakpoint, consult m_ignore_breakpoints.
3396db73ca5SSean Callanan 
34018de2fdcSJim Ingham 
34118de2fdcSJim Ingham     if (stop_reason == eStopReasonBreakpoint)
34240d871faSJim Ingham     {
3431ac04c30SGreg Clayton         ProcessSP process_sp (m_thread.CalculateProcess());
344160f78c5SJim Ingham         uint64_t break_site_id = m_real_stop_info_sp->GetValue();
3451ac04c30SGreg Clayton         BreakpointSiteSP bp_site_sp;
3461ac04c30SGreg Clayton         if (process_sp)
3471ac04c30SGreg Clayton             bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
34840d871faSJim Ingham         if (bp_site_sp)
34940d871faSJim Ingham         {
35040d871faSJim Ingham             uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
35140d871faSJim Ingham             bool is_internal = true;
35240d871faSJim Ingham             for (uint32_t i = 0; i < num_owners; i++)
35340d871faSJim Ingham             {
3546db73ca5SSean Callanan                 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
3550161b49cSJim Ingham                 if (log)
3560161b49cSJim Ingham                     log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: hit breakpoint %d while calling function", bp.GetID());
3576db73ca5SSean Callanan 
3586db73ca5SSean Callanan                 if (!bp.IsInternal())
35940d871faSJim Ingham                 {
36040d871faSJim Ingham                     is_internal = false;
36140d871faSJim Ingham                     break;
36240d871faSJim Ingham                 }
36340d871faSJim Ingham             }
36440d871faSJim Ingham             if (is_internal)
3650161b49cSJim Ingham             {
3660161b49cSJim Ingham                 if (log)
3670161b49cSJim Ingham                     log->Printf ("ThreadPlanCallFunction::PlanExplainsStop hit an internal breakpoint, not stopping.");
36840d871faSJim Ingham                 return false;
36940d871faSJim Ingham             }
3700161b49cSJim Ingham         }
37140d871faSJim Ingham 
372184e9811SJim Ingham         if (m_ignore_breakpoints)
373923886ceSJim Ingham         {
3740161b49cSJim Ingham             if (log)
3750161b49cSJim Ingham                 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
3760161b49cSJim Ingham             m_real_stop_info_sp->OverrideShouldStop(false);
377923886ceSJim Ingham             return true;
378923886ceSJim Ingham         }
379923886ceSJim Ingham         else
3800161b49cSJim Ingham         {
3810161b49cSJim Ingham             if (log)
3820161b49cSJim Ingham                 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are not ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
3830161b49cSJim Ingham             m_real_stop_info_sp->OverrideShouldStop(true);
3840161b49cSJim Ingham             return false;
3850161b49cSJim Ingham         }
3860161b49cSJim Ingham     }
3870161b49cSJim Ingham     else if (!m_unwind_on_error)
3880161b49cSJim Ingham     {
3890161b49cSJim Ingham         // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
390923886ceSJim Ingham         return false;
39140d871faSJim Ingham     }
39240d871faSJim Ingham     else
39340d871faSJim Ingham     {
39440d871faSJim Ingham         // If the subplan is running, any crashes are attributable to us.
3952c36439cSJim Ingham         // If we want to discard the plan, then we say we explain the stop
3962c36439cSJim Ingham         // but if we are going to be discarded, let whoever is above us
3972c36439cSJim Ingham         // explain the stop.
3980161b49cSJim Ingham         // But don't discard the plan if the stop would restart itself (for instance if it is a
3990161b49cSJim Ingham         // signal that is set not to stop.  Check that here first.  We just say we explain the stop
4000161b49cSJim Ingham         // but aren't done and everything will continue on from there.
4010161b49cSJim Ingham 
4020161b49cSJim Ingham         if (m_real_stop_info_sp->ShouldStopSynchronous(event_ptr))
4030161b49cSJim Ingham         {
404184e9811SJim Ingham             SetPlanComplete(false);
4059a028519SSean Callanan             if (m_subplan_sp)
40618de2fdcSJim Ingham             {
407184e9811SJim Ingham                 if (m_unwind_on_error)
40818de2fdcSJim Ingham                     return true;
40918de2fdcSJim Ingham                 else
41018de2fdcSJim Ingham                     return false;
41118de2fdcSJim Ingham             }
41218de2fdcSJim Ingham             else
41318de2fdcSJim Ingham                 return false;
41430fdc8d8SChris Lattner         }
4150161b49cSJim Ingham         else
4160161b49cSJim Ingham             return true;
4170161b49cSJim Ingham     }
41840d871faSJim Ingham }
41930fdc8d8SChris Lattner 
42030fdc8d8SChris Lattner bool
42130fdc8d8SChris Lattner ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
42230fdc8d8SChris Lattner {
423221d51cfSJim Ingham     // We do some computation in DoPlanExplainsStop that may or may not set the plan as complete.
424184e9811SJim Ingham     // We need to do that here to make sure our state is correct.
425221d51cfSJim Ingham     DoPlanExplainsStop(event_ptr);
426184e9811SJim Ingham 
427184e9811SJim Ingham     if (IsPlanComplete())
42830fdc8d8SChris Lattner     {
4299da3683cSJim Ingham         ReportRegisterState ("Function completed.  Register state was:");
43030fdc8d8SChris Lattner         return true;
43130fdc8d8SChris Lattner     }
43230fdc8d8SChris Lattner     else
43330fdc8d8SChris Lattner     {
43430fdc8d8SChris Lattner         return false;
43530fdc8d8SChris Lattner     }
43630fdc8d8SChris Lattner }
43730fdc8d8SChris Lattner 
43830fdc8d8SChris Lattner bool
43930fdc8d8SChris Lattner ThreadPlanCallFunction::StopOthers ()
44030fdc8d8SChris Lattner {
44130fdc8d8SChris Lattner     return m_stop_other_threads;
44230fdc8d8SChris Lattner }
44330fdc8d8SChris Lattner 
44430fdc8d8SChris Lattner StateType
44506e827ccSJim Ingham ThreadPlanCallFunction::GetPlanRunState ()
44630fdc8d8SChris Lattner {
44730fdc8d8SChris Lattner     return eStateRunning;
44830fdc8d8SChris Lattner }
44930fdc8d8SChris Lattner 
45030fdc8d8SChris Lattner void
45130fdc8d8SChris Lattner ThreadPlanCallFunction::DidPush ()
45230fdc8d8SChris Lattner {
453be3a1b14SSean Callanan //#define SINGLE_STEP_EXPRESSIONS
454be3a1b14SSean Callanan 
4558559a355SJim Ingham     // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
4568559a355SJim Ingham     // Wait till the plan is pushed so we aren't changing the stop info till we're about to run.
4578559a355SJim Ingham 
4588559a355SJim Ingham     GetThread().SetStopInfoToNothing();
4598559a355SJim Ingham 
460be3a1b14SSean Callanan #ifndef SINGLE_STEP_EXPRESSIONS
46130fdc8d8SChris Lattner     m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
46230fdc8d8SChris Lattner 
46330fdc8d8SChris Lattner     m_thread.QueueThreadPlan(m_subplan_sp, false);
46477787033SJim Ingham     m_subplan_sp->SetPrivate (true);
465be3a1b14SSean Callanan #endif
46630fdc8d8SChris Lattner }
46730fdc8d8SChris Lattner 
46830fdc8d8SChris Lattner bool
46930fdc8d8SChris Lattner ThreadPlanCallFunction::WillStop ()
47030fdc8d8SChris Lattner {
47130fdc8d8SChris Lattner     return true;
47230fdc8d8SChris Lattner }
47330fdc8d8SChris Lattner 
47430fdc8d8SChris Lattner bool
47530fdc8d8SChris Lattner ThreadPlanCallFunction::MischiefManaged ()
47630fdc8d8SChris Lattner {
4775160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
47830fdc8d8SChris Lattner 
479184e9811SJim Ingham     if (IsPlanComplete())
480184e9811SJim Ingham     {
48130fdc8d8SChris Lattner         if (log)
482*324a1036SSaleem Abdulrasool             log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.",
483*324a1036SSaleem Abdulrasool                         static_cast<void*>(this));
48430fdc8d8SChris Lattner 
48530fdc8d8SChris Lattner         ThreadPlan::MischiefManaged ();
48630fdc8d8SChris Lattner         return true;
48730fdc8d8SChris Lattner     }
48830fdc8d8SChris Lattner     else
48930fdc8d8SChris Lattner     {
49030fdc8d8SChris Lattner         return false;
49130fdc8d8SChris Lattner     }
49230fdc8d8SChris Lattner }
4936db73ca5SSean Callanan 
4946db73ca5SSean Callanan void
4956db73ca5SSean Callanan ThreadPlanCallFunction::SetBreakpoints ()
4966db73ca5SSean Callanan {
4971ac04c30SGreg Clayton     ProcessSP process_sp (m_thread.CalculateProcess());
4986fbc48bcSJim Ingham     if (m_trap_exceptions && process_sp)
4991ac04c30SGreg Clayton     {
5001ac04c30SGreg Clayton         m_cxx_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus);
5011ac04c30SGreg Clayton         m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC);
5026db73ca5SSean Callanan 
503f211510fSSean Callanan         if (m_cxx_language_runtime)
5046fbc48bcSJim Ingham         {
5056fbc48bcSJim Ingham             m_should_clear_cxx_exception_bp = !m_cxx_language_runtime->ExceptionBreakpointsAreSet();
506f211510fSSean Callanan             m_cxx_language_runtime->SetExceptionBreakpoints();
5076fbc48bcSJim Ingham         }
508f211510fSSean Callanan         if (m_objc_language_runtime)
5096fbc48bcSJim Ingham         {
5106fbc48bcSJim Ingham             m_should_clear_objc_exception_bp = !m_objc_language_runtime->ExceptionBreakpointsAreSet();
511f211510fSSean Callanan             m_objc_language_runtime->SetExceptionBreakpoints();
5126db73ca5SSean Callanan         }
5131ac04c30SGreg Clayton     }
5146fbc48bcSJim Ingham }
5156db73ca5SSean Callanan 
5166db73ca5SSean Callanan void
5176db73ca5SSean Callanan ThreadPlanCallFunction::ClearBreakpoints ()
5186db73ca5SSean Callanan {
5196fbc48bcSJim Ingham     if (m_trap_exceptions)
5206fbc48bcSJim Ingham     {
5216fbc48bcSJim Ingham         if (m_cxx_language_runtime && m_should_clear_cxx_exception_bp)
522f211510fSSean Callanan             m_cxx_language_runtime->ClearExceptionBreakpoints();
5236fbc48bcSJim Ingham         if (m_objc_language_runtime && m_should_clear_objc_exception_bp)
524f211510fSSean Callanan             m_objc_language_runtime->ClearExceptionBreakpoints();
5256db73ca5SSean Callanan     }
5266fbc48bcSJim Ingham }
527c98aca60SSean Callanan 
528c98aca60SSean Callanan bool
529c98aca60SSean Callanan ThreadPlanCallFunction::BreakpointsExplainStop()
530c98aca60SSean Callanan {
53160c4118cSJim Ingham     StopInfoSP stop_info_sp = GetPrivateStopInfo ();
532c98aca60SSean Callanan 
5336fbc48bcSJim Ingham     if (m_trap_exceptions)
5346fbc48bcSJim Ingham     {
535184e9811SJim Ingham         if ((m_cxx_language_runtime &&
536f211510fSSean Callanan                 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
537184e9811SJim Ingham            ||(m_objc_language_runtime &&
538184e9811SJim Ingham                 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp)))
539184e9811SJim Ingham         {
540641a67ceSJim Ingham             Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
541641a67ceSJim Ingham             if (log)
542641a67ceSJim Ingham                 log->Printf ("ThreadPlanCallFunction::BreakpointsExplainStop - Hit an exception breakpoint, setting plan complete.");
543641a67ceSJim Ingham 
544184e9811SJim Ingham             SetPlanComplete(false);
545641a67ceSJim Ingham 
546641a67ceSJim Ingham             // If the user has set the ObjC language breakpoint, it would normally get priority over our internal
547641a67ceSJim Ingham             // catcher breakpoint, but in this case we can't let that happen, so force the ShouldStop here.
548641a67ceSJim Ingham             stop_info_sp->OverrideShouldStop (true);
549c98aca60SSean Callanan             return true;
550184e9811SJim Ingham         }
5516fbc48bcSJim Ingham     }
552f211510fSSean Callanan 
553c98aca60SSean Callanan     return false;
554c98aca60SSean Callanan }
5558559a355SJim Ingham 
5560ff099f1SJim Ingham void
5570ff099f1SJim Ingham ThreadPlanCallFunction::SetStopOthers (bool new_value)
5580ff099f1SJim Ingham {
5590ff099f1SJim Ingham     m_subplan_sp->SetStopOthers(new_value);
5600ff099f1SJim Ingham }
5610ff099f1SJim Ingham 
5620ff099f1SJim Ingham 
5638559a355SJim Ingham bool
5648559a355SJim Ingham ThreadPlanCallFunction::RestoreThreadState()
5658559a355SJim Ingham {
5668559a355SJim Ingham     return GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state);
5678559a355SJim Ingham }
5688559a355SJim Ingham 
569