blob: 5a3ebd7b1284b9205d38f8581a8bfebd4d42419f [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ThreadPlanCallFunction.cpp ------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/ThreadPlanCallFunction.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
Jim Ingham46d005d2014-04-02 22:53:21 +000015
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016// Project includes
17#include "lldb/lldb-private-log.h"
Jim Ingham40d871f2010-10-26 00:27:45 +000018#include "lldb/Breakpoint/Breakpoint.h"
19#include "lldb/Breakpoint/BreakpointLocation.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Address.h"
21#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000022#include "lldb/Core/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/Stream.h"
Greg Clayton1f746072012-08-29 21:13:06 +000024#include "lldb/Symbol/ObjectFile.h"
Sean Callananf2115102010-11-03 22:19:38 +000025#include "lldb/Target/LanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Target/Process.h"
27#include "lldb/Target/RegisterContext.h"
Jim Ingham40d871f2010-10-26 00:27:45 +000028#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Target/Target.h"
30#include "lldb/Target/Thread.h"
31#include "lldb/Target/ThreadPlanRunToAddress.h"
32
33using namespace lldb;
34using namespace lldb_private;
35
36//----------------------------------------------------------------------
37// ThreadPlanCallFunction: Plan to call a single function
38//----------------------------------------------------------------------
Jim Ingham0092c8e2012-04-13 20:38:13 +000039bool
40ThreadPlanCallFunction::ConstructorSetup (Thread &thread,
Jim Ingham0092c8e2012-04-13 20:38:13 +000041 ABI *& abi,
42 lldb::addr_t &start_load_addr,
43 lldb::addr_t &function_load_addr)
44{
Jim Ingham0092c8e2012-04-13 20:38:13 +000045 SetIsMasterPlan (true);
Jim Ingham923886c2012-05-11 18:43:38 +000046 SetOkayToDiscard (false);
Andrew Kaylor327c2672012-12-07 17:56:31 +000047 SetPrivate (true);
Jim Ingham0092c8e2012-04-13 20:38:13 +000048
49 ProcessSP process_sp (thread.GetProcess());
50 if (!process_sp)
51 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000052
Jim Ingham0092c8e2012-04-13 20:38:13 +000053 abi = process_sp->GetABI().get();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000054
Jim Ingham0092c8e2012-04-13 20:38:13 +000055 if (!abi)
56 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000057
Greg Clayton5160ce52013-03-27 23:08:40 +000058 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000059
Jim Ingham0092c8e2012-04-13 20:38:13 +000060 SetBreakpoints();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000061
Jim Ingham0092c8e2012-04-13 20:38:13 +000062 m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
63 // If we can't read memory at the point of the process where we are planning to put our function, we're
64 // not going to get any further...
65 Error error;
66 process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error);
67 if (!error.Success())
68 {
Jim Inghamea06f3b2013-03-28 00:04:05 +000069 m_constructor_errors.Printf ("Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".", m_function_sp);
Jim Ingham0092c8e2012-04-13 20:38:13 +000070 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000071 log->Printf ("ThreadPlanCallFunction(%p): %s.",
72 static_cast<void*>(this),
73 m_constructor_errors.GetData());
Jim Ingham0092c8e2012-04-13 20:38:13 +000074 return false;
75 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000076
Jim Ingham6fbc48b2013-11-07 00:11:47 +000077 Module *exe_module = GetTarget().GetExecutableModulePointer();
Jim Ingham0092c8e2012-04-13 20:38:13 +000078
79 if (exe_module == NULL)
80 {
Jim Inghamea06f3b2013-03-28 00:04:05 +000081 m_constructor_errors.Printf ("Can't execute code without an executable module.");
Jim Ingham0092c8e2012-04-13 20:38:13 +000082 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000083 log->Printf ("ThreadPlanCallFunction(%p): %s.",
84 static_cast<void*>(this),
85 m_constructor_errors.GetData());
Jim Ingham0092c8e2012-04-13 20:38:13 +000086 return false;
87 }
88 else
89 {
90 ObjectFile *objectFile = exe_module->GetObjectFile();
91 if (!objectFile)
92 {
Jim Inghamea06f3b2013-03-28 00:04:05 +000093 m_constructor_errors.Printf ("Could not find object file for module \"%s\".",
94 exe_module->GetFileSpec().GetFilename().AsCString());
95
Jim Ingham0092c8e2012-04-13 20:38:13 +000096 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000097 log->Printf ("ThreadPlanCallFunction(%p): %s.",
98 static_cast<void*>(this),
99 m_constructor_errors.GetData());
Jim Ingham0092c8e2012-04-13 20:38:13 +0000100 return false;
101 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000102
Jim Ingham0092c8e2012-04-13 20:38:13 +0000103 m_start_addr = objectFile->GetEntryPointAddress();
104 if (!m_start_addr.IsValid())
105 {
Jim Inghamea06f3b2013-03-28 00:04:05 +0000106 m_constructor_errors.Printf ("Could not find entry point address for executable module \"%s\".",
107 exe_module->GetFileSpec().GetFilename().AsCString());
Jim Ingham0092c8e2012-04-13 20:38:13 +0000108 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000109 log->Printf ("ThreadPlanCallFunction(%p): %s.",
110 static_cast<void*>(this),
111 m_constructor_errors.GetData());
Jim Ingham0092c8e2012-04-13 20:38:13 +0000112 return false;
113 }
114 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000115
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000116 start_load_addr = m_start_addr.GetLoadAddress (&GetTarget());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000117
Jim Ingham0092c8e2012-04-13 20:38:13 +0000118 // Checkpoint the thread state so we can restore it later.
119 if (log && log->GetVerbose())
120 ReportRegisterState ("About to checkpoint thread before function call. Original register state was:");
121
122 if (!thread.CheckpointThreadState (m_stored_thread_state))
123 {
Jim Inghamea06f3b2013-03-28 00:04:05 +0000124 m_constructor_errors.Printf ("Setting up ThreadPlanCallFunction, failed to checkpoint thread state.");
Jim Ingham0092c8e2012-04-13 20:38:13 +0000125 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000126 log->Printf ("ThreadPlanCallFunction(%p): %s.",
127 static_cast<void*>(this),
128 m_constructor_errors.GetData());
Jim Ingham0092c8e2012-04-13 20:38:13 +0000129 return false;
130 }
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000131 function_load_addr = m_function_addr.GetLoadAddress (&GetTarget());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000132
Jim Ingham0092c8e2012-04-13 20:38:13 +0000133 return true;
134}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135
136ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
Matt Kopec00049b82013-02-27 20:13:38 +0000137 const Address &function,
Jim Inghamef651602011-12-22 19:12:40 +0000138 const ClangASTType &return_type,
Sean Callanana464f3d2013-11-08 01:14:26 +0000139 llvm::ArrayRef<addr_t> args,
140 const EvaluateExpressionOptions &options) :
Jim Inghamb01e7422010-06-19 04:45:32 +0000141 ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
Benjamin Kramer1ee0d4f2010-07-16 12:32:33 +0000142 m_valid (false),
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000143 m_stop_other_threads (options.GetStopOthers()),
144 m_unwind_on_error (options.DoesUnwindOnError()),
145 m_ignore_breakpoints (options.DoesIgnoreBreakpoints()),
146 m_debug_execution (options.GetDebug()),
147 m_trap_exceptions (options.GetTrapExceptions()),
Jim Ingham16e0c682011-08-12 23:34:31 +0000148 m_function_addr (function),
Filipe Cabecinhasb4cb0be2012-09-11 18:11:07 +0000149 m_function_sp (0),
Jim Inghamef651602011-12-22 19:12:40 +0000150 m_return_type (return_type),
Jim Inghamce553d82011-11-01 02:46:54 +0000151 m_takedown_done (false),
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000152 m_should_clear_objc_exception_bp(false),
153 m_should_clear_cxx_exception_bp (false),
154 m_stop_address (LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155{
Jim Ingham0092c8e2012-04-13 20:38:13 +0000156 lldb::addr_t start_load_addr;
157 ABI *abi;
158 lldb::addr_t function_load_addr;
Jim Ingham923886c2012-05-11 18:43:38 +0000159 if (!ConstructorSetup (thread, abi, start_load_addr, function_load_addr))
Greg Clayton1ac04c32012-02-21 00:09:25 +0000160 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000161
Sean Callanana464f3d2013-11-08 01:14:26 +0000162 if (!abi->PrepareTrivialCall(thread,
163 m_function_sp,
164 function_load_addr,
165 start_load_addr,
166 args))
Greg Clayton1ac04c32012-02-21 00:09:25 +0000167 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000168
Jim Ingham9da36832011-01-22 01:27:23 +0000169 ReportRegisterState ("Function call was set up. Register state was:");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000170
Jim Ingham9da36832011-01-22 01:27:23 +0000171 m_valid = true;
172}
173
174ThreadPlanCallFunction::~ThreadPlanCallFunction ()
175{
Jim Ingham0161b492013-02-09 01:29:05 +0000176 DoTakedown(PlanSucceeded());
Jim Ingham9da36832011-01-22 01:27:23 +0000177}
178
179void
180ThreadPlanCallFunction::ReportRegisterState (const char *message)
181{
Greg Clayton5160ce52013-03-27 23:08:40 +0000182 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_VERBOSE));
Sean Callananece96492010-11-08 03:49:50 +0000183 if (log)
184 {
Greg Claytonaf247d72011-05-19 03:54:16 +0000185 StreamString strm;
Greg Clayton5ccbd292011-01-06 22:15:06 +0000186 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
Jim Ingham9da36832011-01-22 01:27:23 +0000187
188 log->PutCString(message);
189
Greg Claytonaf247d72011-05-19 03:54:16 +0000190 RegisterValue reg_value;
191
192 for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
193 reg_idx < num_registers;
194 ++reg_idx)
Sean Callananece96492010-11-08 03:49:50 +0000195 {
Greg Claytonaf247d72011-05-19 03:54:16 +0000196 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_idx);
197 if (reg_ctx->ReadRegister(reg_info, reg_value))
198 {
199 reg_value.Dump(&strm, reg_info, true, false, eFormatDefault);
200 strm.EOL();
201 }
Sean Callananece96492010-11-08 03:49:50 +0000202 }
Greg Claytonaf247d72011-05-19 03:54:16 +0000203 log->PutCString(strm.GetData());
Sean Callananece96492010-11-08 03:49:50 +0000204 }
Sean Callanan10af7c42010-11-04 01:51:38 +0000205}
206
207void
Jim Ingham18de2fd2012-05-10 01:35:39 +0000208ThreadPlanCallFunction::DoTakedown (bool success)
Sean Callanan10af7c42010-11-04 01:51:38 +0000209{
Greg Clayton5160ce52013-03-27 23:08:40 +0000210 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000211
Jim Ingham87d0e612012-04-13 23:11:52 +0000212 if (!m_valid)
213 {
214 //Don't call DoTakedown if we were never valid to begin with.
215 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000216 log->Printf ("ThreadPlanCallFunction(%p): Log called on ThreadPlanCallFunction that was never valid.",
217 static_cast<void*>(this));
Jim Ingham87d0e612012-04-13 23:11:52 +0000218 return;
219 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000220
Jim Ingham9da36832011-01-22 01:27:23 +0000221 if (!m_takedown_done)
Jim Ingham77787032011-01-20 02:03:18 +0000222 {
Jim Ingham18de2fd2012-05-10 01:35:39 +0000223 if (success)
Greg Clayton70b57652011-05-15 01:25:55 +0000224 {
Jim Ingham18de2fd2012-05-10 01:35:39 +0000225 ProcessSP process_sp (m_thread.GetProcess());
226 const ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
227 if (abi && m_return_type.IsValid())
228 {
229 const bool persistent = false;
230 m_return_valobj_sp = abi->GetReturnValueObject (m_thread, m_return_type, persistent);
231 }
Greg Clayton70b57652011-05-15 01:25:55 +0000232 }
Jim Ingham9da36832011-01-22 01:27:23 +0000233 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000234 log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
235 static_cast<void*>(this), m_thread.GetID(), m_valid,
236 IsPlanComplete());
Jim Ingham9da36832011-01-22 01:27:23 +0000237 m_takedown_done = true;
Jim Inghamce553d82011-11-01 02:46:54 +0000238 m_stop_address = m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
Jim Ingham60c41182013-06-04 01:40:51 +0000239 m_real_stop_info_sp = GetPrivateStopInfo ();
Ed Maste7b24e952013-11-12 16:47:08 +0000240 if (!m_thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state))
241 {
242 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000243 log->Printf("ThreadPlanCallFunction(%p): DoTakedown failed to restore register state",
244 static_cast<void*>(this));
Ed Maste7b24e952013-11-12 16:47:08 +0000245 }
Jim Ingham18de2fd2012-05-10 01:35:39 +0000246 SetPlanComplete(success);
Jim Ingham77787032011-01-20 02:03:18 +0000247 ClearBreakpoints();
Jim Ingham9da36832011-01-22 01:27:23 +0000248 if (log && log->GetVerbose())
249 ReportRegisterState ("Restoring thread state after function call. Restored register state:");
Jim Ingham2c364392011-01-26 19:13:09 +0000250
Jim Ingham9da36832011-01-22 01:27:23 +0000251 }
252 else
253 {
254 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000255 log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called as no-op for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
256 static_cast<void*>(this), m_thread.GetID(), m_valid,
257 IsPlanComplete());
Jim Ingham77787032011-01-20 02:03:18 +0000258 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259}
260
261void
Jim Inghambda4e5eb2011-01-18 01:58:06 +0000262ThreadPlanCallFunction::WillPop ()
263{
Jim Ingham0161b492013-02-09 01:29:05 +0000264 DoTakedown(PlanSucceeded());
Jim Inghambda4e5eb2011-01-18 01:58:06 +0000265}
266
267void
Greg Clayton2a48f522011-05-14 01:50:35 +0000268ThreadPlanCallFunction::GetDescription (Stream *s, DescriptionLevel level)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269{
Greg Clayton2a48f522011-05-14 01:50:35 +0000270 if (level == eDescriptionLevelBrief)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271 {
272 s->Printf("Function call thread plan");
273 }
274 else
275 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000276 TargetSP target_sp (m_thread.CalculateTarget());
Daniel Malead01b2952012-11-29 21:49:15 +0000277 s->Printf("Thread plan to call 0x%" PRIx64, m_function_addr.GetLoadAddress(target_sp.get()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278 }
279}
280
281bool
282ThreadPlanCallFunction::ValidatePlan (Stream *error)
283{
284 if (!m_valid)
Jim Inghamea06f3b2013-03-28 00:04:05 +0000285 {
286 if (error)
287 {
288 if (m_constructor_errors.GetSize() > 0)
289 error->PutCString (m_constructor_errors.GetData());
290 else
291 error->PutCString ("Unknown error");
292 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293 return false;
Jim Inghamea06f3b2013-03-28 00:04:05 +0000294 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295
296 return true;
297}
298
Jim Ingham0161b492013-02-09 01:29:05 +0000299
300Vote
301ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr)
302{
303 if (m_takedown_done || IsPlanComplete())
304 return eVoteYes;
305 else
306 return ThreadPlan::ShouldReportStop(event_ptr);
307}
308
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000310ThreadPlanCallFunction::DoPlanExplainsStop (Event *event_ptr)
Sean Callanan6db73ca2010-11-03 01:37:52 +0000311{
Greg Clayton5160ce52013-03-27 23:08:40 +0000312 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP|LIBLLDB_LOG_PROCESS));
Jim Ingham60c41182013-06-04 01:40:51 +0000313 m_real_stop_info_sp = GetPrivateStopInfo ();
Jim Ingham160f78c2011-05-17 01:10:11 +0000314
Jim Ingham40d871f2010-10-26 00:27:45 +0000315 // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
316 // we answer yes.
Jim Ingham0161b492013-02-09 01:29:05 +0000317 if (m_subplan_sp.get() != NULL && m_subplan_sp->PlanExplainsStop(event_ptr))
Jim Ingham184e9812013-01-15 02:47:48 +0000318 {
319 SetPlanComplete();
Jim Ingham40d871f2010-10-26 00:27:45 +0000320 return true;
Jim Ingham184e9812013-01-15 02:47:48 +0000321 }
Sean Callanan3e6fedc2010-10-19 22:24:06 +0000322
Sean Callananc98aca62010-11-03 19:36:28 +0000323 // Check if the breakpoint is one of ours.
324
Jim Ingham18de2fd2012-05-10 01:35:39 +0000325 StopReason stop_reason;
326 if (!m_real_stop_info_sp)
327 stop_reason = eStopReasonNone;
328 else
329 stop_reason = m_real_stop_info_sp->GetStopReason();
Jim Ingham0161b492013-02-09 01:29:05 +0000330 if (log)
331 log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - %s.", Thread::StopReasonAsCString(stop_reason));
Jim Ingham18de2fd2012-05-10 01:35:39 +0000332
333 if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
Sean Callananc98aca62010-11-03 19:36:28 +0000334 return true;
335
Jim Ingham35878c42014-04-08 21:33:21 +0000336 // One more quirk here. If this event was from Halt interrupting the target, then we should not consider
337 // ourselves complete. Return true to acknowledge the stop.
338 if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr))
339 {
340 if (log)
341 log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: The event is an Interrupt, returning true.");
342 return true;
343 }
Jim Ingham0161b492013-02-09 01:29:05 +0000344 // We control breakpoints separately from other "stop reasons." So first,
345 // check the case where we stopped for an internal breakpoint, in that case, continue on.
346 // If it is not an internal breakpoint, consult m_ignore_breakpoints.
Sean Callanan6db73ca2010-11-03 01:37:52 +0000347
Jim Ingham18de2fd2012-05-10 01:35:39 +0000348
349 if (stop_reason == eStopReasonBreakpoint)
Jim Ingham40d871f2010-10-26 00:27:45 +0000350 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000351 ProcessSP process_sp (m_thread.CalculateProcess());
Jim Ingham160f78c2011-05-17 01:10:11 +0000352 uint64_t break_site_id = m_real_stop_info_sp->GetValue();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000353 BreakpointSiteSP bp_site_sp;
354 if (process_sp)
355 bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
Jim Ingham40d871f2010-10-26 00:27:45 +0000356 if (bp_site_sp)
357 {
358 uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
359 bool is_internal = true;
360 for (uint32_t i = 0; i < num_owners; i++)
361 {
Sean Callanan6db73ca2010-11-03 01:37:52 +0000362 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
Jim Ingham0161b492013-02-09 01:29:05 +0000363 if (log)
364 log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: hit breakpoint %d while calling function", bp.GetID());
Sean Callanan6db73ca2010-11-03 01:37:52 +0000365
366 if (!bp.IsInternal())
Jim Ingham40d871f2010-10-26 00:27:45 +0000367 {
368 is_internal = false;
369 break;
370 }
371 }
372 if (is_internal)
Jim Ingham0161b492013-02-09 01:29:05 +0000373 {
374 if (log)
375 log->Printf ("ThreadPlanCallFunction::PlanExplainsStop hit an internal breakpoint, not stopping.");
Jim Ingham40d871f2010-10-26 00:27:45 +0000376 return false;
Jim Ingham0161b492013-02-09 01:29:05 +0000377 }
Jim Ingham40d871f2010-10-26 00:27:45 +0000378 }
Jim Ingham0161b492013-02-09 01:29:05 +0000379
Jim Ingham184e9812013-01-15 02:47:48 +0000380 if (m_ignore_breakpoints)
Jim Ingham923886c2012-05-11 18:43:38 +0000381 {
Jim Ingham0161b492013-02-09 01:29:05 +0000382 if (log)
383 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
384 m_real_stop_info_sp->OverrideShouldStop(false);
Jim Ingham923886c2012-05-11 18:43:38 +0000385 return true;
386 }
387 else
Jim Ingham0161b492013-02-09 01:29:05 +0000388 {
389 if (log)
390 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are not ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
391 m_real_stop_info_sp->OverrideShouldStop(true);
Jim Ingham923886c2012-05-11 18:43:38 +0000392 return false;
Jim Ingham0161b492013-02-09 01:29:05 +0000393 }
394 }
395 else if (!m_unwind_on_error)
396 {
397 // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
398 return false;
Jim Ingham40d871f2010-10-26 00:27:45 +0000399 }
400 else
401 {
402 // If the subplan is running, any crashes are attributable to us.
Jim Ingham2c364392011-01-26 19:13:09 +0000403 // If we want to discard the plan, then we say we explain the stop
404 // but if we are going to be discarded, let whoever is above us
405 // explain the stop.
Jim Ingham0161b492013-02-09 01:29:05 +0000406 // But don't discard the plan if the stop would restart itself (for instance if it is a
407 // signal that is set not to stop. Check that here first. We just say we explain the stop
408 // but aren't done and everything will continue on from there.
409
410 if (m_real_stop_info_sp->ShouldStopSynchronous(event_ptr))
Jim Ingham18de2fd2012-05-10 01:35:39 +0000411 {
Jim Ingham0161b492013-02-09 01:29:05 +0000412 SetPlanComplete(false);
413 if (m_subplan_sp)
Jim Ingham18de2fd2012-05-10 01:35:39 +0000414 {
Jim Ingham0161b492013-02-09 01:29:05 +0000415 if (m_unwind_on_error)
416 return true;
417 else
418 return false;
Jim Ingham18de2fd2012-05-10 01:35:39 +0000419 }
420 else
421 return false;
422 }
423 else
Jim Ingham0161b492013-02-09 01:29:05 +0000424 return true;
Jim Ingham40d871f2010-10-26 00:27:45 +0000425 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426}
427
428bool
429ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
430{
Jim Ingham221d51c2013-05-08 00:35:16 +0000431 // We do some computation in DoPlanExplainsStop that may or may not set the plan as complete.
Jim Ingham184e9812013-01-15 02:47:48 +0000432 // We need to do that here to make sure our state is correct.
Jim Ingham221d51c2013-05-08 00:35:16 +0000433 DoPlanExplainsStop(event_ptr);
Jim Ingham184e9812013-01-15 02:47:48 +0000434
435 if (IsPlanComplete())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436 {
Jim Ingham9da36832011-01-22 01:27:23 +0000437 ReportRegisterState ("Function completed. Register state was:");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438 return true;
439 }
440 else
441 {
442 return false;
443 }
444}
445
446bool
447ThreadPlanCallFunction::StopOthers ()
448{
449 return m_stop_other_threads;
450}
451
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452StateType
Jim Ingham06e827c2010-11-11 19:26:09 +0000453ThreadPlanCallFunction::GetPlanRunState ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454{
455 return eStateRunning;
456}
457
458void
459ThreadPlanCallFunction::DidPush ()
460{
Sean Callananbe3a1b12010-10-26 00:31:56 +0000461//#define SINGLE_STEP_EXPRESSIONS
462
Jim Ingham8559a352012-11-26 23:52:18 +0000463 // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
464 // Wait till the plan is pushed so we aren't changing the stop info till we're about to run.
465
466 GetThread().SetStopInfoToNothing();
467
Sean Callananbe3a1b12010-10-26 00:31:56 +0000468#ifndef SINGLE_STEP_EXPRESSIONS
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469 m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
470
471 m_thread.QueueThreadPlan(m_subplan_sp, false);
Jim Ingham77787032011-01-20 02:03:18 +0000472 m_subplan_sp->SetPrivate (true);
Sean Callananbe3a1b12010-10-26 00:31:56 +0000473#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474}
475
476bool
477ThreadPlanCallFunction::WillStop ()
478{
479 return true;
480}
481
482bool
483ThreadPlanCallFunction::MischiefManaged ()
484{
Greg Clayton5160ce52013-03-27 23:08:40 +0000485 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000486
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 if (IsPlanComplete())
488 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000490 log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.",
491 static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492
493 ThreadPlan::MischiefManaged ();
494 return true;
495 }
496 else
497 {
498 return false;
499 }
500}
Sean Callanan6db73ca2010-11-03 01:37:52 +0000501
502void
503ThreadPlanCallFunction::SetBreakpoints ()
504{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000505 ProcessSP process_sp (m_thread.CalculateProcess());
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000506 if (m_trap_exceptions && process_sp)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000507 {
508 m_cxx_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus);
509 m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC);
Sean Callanan6db73ca2010-11-03 01:37:52 +0000510
Greg Clayton1ac04c32012-02-21 00:09:25 +0000511 if (m_cxx_language_runtime)
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000512 {
513 m_should_clear_cxx_exception_bp = !m_cxx_language_runtime->ExceptionBreakpointsAreSet();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000514 m_cxx_language_runtime->SetExceptionBreakpoints();
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000515 }
Greg Clayton1ac04c32012-02-21 00:09:25 +0000516 if (m_objc_language_runtime)
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000517 {
518 m_should_clear_objc_exception_bp = !m_objc_language_runtime->ExceptionBreakpointsAreSet();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000519 m_objc_language_runtime->SetExceptionBreakpoints();
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000520 }
Greg Clayton1ac04c32012-02-21 00:09:25 +0000521 }
Sean Callanan6db73ca2010-11-03 01:37:52 +0000522}
523
524void
525ThreadPlanCallFunction::ClearBreakpoints ()
526{
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000527 if (m_trap_exceptions)
528 {
529 if (m_cxx_language_runtime && m_should_clear_cxx_exception_bp)
530 m_cxx_language_runtime->ClearExceptionBreakpoints();
531 if (m_objc_language_runtime && m_should_clear_objc_exception_bp)
532 m_objc_language_runtime->ClearExceptionBreakpoints();
533 }
Sean Callanan6db73ca2010-11-03 01:37:52 +0000534}
Sean Callananc98aca62010-11-03 19:36:28 +0000535
536bool
537ThreadPlanCallFunction::BreakpointsExplainStop()
538{
Jim Ingham60c41182013-06-04 01:40:51 +0000539 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
Sean Callananc98aca62010-11-03 19:36:28 +0000540
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000541 if (m_trap_exceptions)
Jim Ingham184e9812013-01-15 02:47:48 +0000542 {
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000543 if ((m_cxx_language_runtime &&
544 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
545 ||(m_objc_language_runtime &&
546 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp)))
547 {
548 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
549 if (log)
550 log->Printf ("ThreadPlanCallFunction::BreakpointsExplainStop - Hit an exception breakpoint, setting plan complete.");
551
552 SetPlanComplete(false);
553
554 // If the user has set the ObjC language breakpoint, it would normally get priority over our internal
555 // catcher breakpoint, but in this case we can't let that happen, so force the ShouldStop here.
556 stop_info_sp->OverrideShouldStop (true);
557 return true;
558 }
Jim Ingham184e9812013-01-15 02:47:48 +0000559 }
Sean Callananc98aca62010-11-03 19:36:28 +0000560
Sean Callananc98aca62010-11-03 19:36:28 +0000561 return false;
562}
Jim Ingham8559a352012-11-26 23:52:18 +0000563
Jim Ingham0ff099f2014-03-07 11:16:37 +0000564void
565ThreadPlanCallFunction::SetStopOthers (bool new_value)
566{
567 m_subplan_sp->SetStopOthers(new_value);
568}
569
570
Jim Ingham8559a352012-11-26 23:52:18 +0000571bool
572ThreadPlanCallFunction::RestoreThreadState()
573{
574 return GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state);
575}
576