blob: 9f7bd8a0c24614b4e37e8da6794e40dad2f28d7e [file] [log] [blame]
Chris Lattner24943d22010-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
Sean Callanan07f3d8d2010-11-03 01:37:52 +000015#include "llvm/Support/MachO.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016// Project includes
17#include "lldb/lldb-private-log.h"
Jim Ingham988ddbc2010-10-26 00:27:45 +000018#include "lldb/Breakpoint/Breakpoint.h"
19#include "lldb/Breakpoint/BreakpointLocation.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Core/Address.h"
21#include "lldb/Core/Log.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000022#include "lldb/Core/Module.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Core/Stream.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000024#include "lldb/Symbol/ObjectFile.h"
Sean Callanan29756d42010-11-03 22:19:38 +000025#include "lldb/Target/LanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Target/Process.h"
27#include "lldb/Target/RegisterContext.h"
Jim Ingham988ddbc2010-10-26 00:27:45 +000028#include "lldb/Target/StopInfo.h"
Chris Lattner24943d22010-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 Ingham1e58cef2012-04-13 20:38:13 +000039bool
40ThreadPlanCallFunction::ConstructorSetup (Thread &thread,
Jim Ingham1e58cef2012-04-13 20:38:13 +000041 ABI *& abi,
42 lldb::addr_t &start_load_addr,
43 lldb::addr_t &function_load_addr)
44{
Jim Ingham1e58cef2012-04-13 20:38:13 +000045 SetIsMasterPlan (true);
Jim Ingham110b55f2012-05-11 18:43:38 +000046 SetOkayToDiscard (false);
Andrew Kaylorae0f59c2012-12-07 17:56:31 +000047 SetPrivate (true);
Jim Ingham1e58cef2012-04-13 20:38:13 +000048
49 ProcessSP process_sp (thread.GetProcess());
50 if (!process_sp)
51 return false;
52
53 abi = process_sp->GetABI().get();
54
55 if (!abi)
56 return false;
57
58 TargetSP target_sp (thread.CalculateTarget());
59
Greg Clayton952e9dc2013-03-27 23:08:40 +000060 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham1e58cef2012-04-13 20:38:13 +000061
62 SetBreakpoints();
63
64 m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
65 // If we can't read memory at the point of the process where we are planning to put our function, we're
66 // not going to get any further...
67 Error error;
68 process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error);
69 if (!error.Success())
70 {
Jim Ingham845970e2013-03-28 00:04:05 +000071 m_constructor_errors.Printf ("Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".", m_function_sp);
Jim Ingham1e58cef2012-04-13 20:38:13 +000072 if (log)
Jim Ingham845970e2013-03-28 00:04:05 +000073 log->Printf ("ThreadPlanCallFunction(%p): %s.", this, m_constructor_errors.GetData());
Jim Ingham1e58cef2012-04-13 20:38:13 +000074 return false;
75 }
76
77 Module *exe_module = target_sp->GetExecutableModulePointer();
78
79 if (exe_module == NULL)
80 {
Jim Ingham845970e2013-03-28 00:04:05 +000081 m_constructor_errors.Printf ("Can't execute code without an executable module.");
Jim Ingham1e58cef2012-04-13 20:38:13 +000082 if (log)
Jim Ingham845970e2013-03-28 00:04:05 +000083 log->Printf ("ThreadPlanCallFunction(%p): %s.", this, m_constructor_errors.GetData());
Jim Ingham1e58cef2012-04-13 20:38:13 +000084 return false;
85 }
86 else
87 {
88 ObjectFile *objectFile = exe_module->GetObjectFile();
89 if (!objectFile)
90 {
Jim Ingham845970e2013-03-28 00:04:05 +000091 m_constructor_errors.Printf ("Could not find object file for module \"%s\".",
92 exe_module->GetFileSpec().GetFilename().AsCString());
93
Jim Ingham1e58cef2012-04-13 20:38:13 +000094 if (log)
Jim Ingham845970e2013-03-28 00:04:05 +000095 log->Printf ("ThreadPlanCallFunction(%p): %s.", this, m_constructor_errors.GetData());
Jim Ingham1e58cef2012-04-13 20:38:13 +000096 return false;
97 }
Jim Ingham845970e2013-03-28 00:04:05 +000098
Jim Ingham1e58cef2012-04-13 20:38:13 +000099 m_start_addr = objectFile->GetEntryPointAddress();
100 if (!m_start_addr.IsValid())
101 {
Jim Ingham845970e2013-03-28 00:04:05 +0000102 m_constructor_errors.Printf ("Could not find entry point address for executable module \"%s\".",
103 exe_module->GetFileSpec().GetFilename().AsCString());
Jim Ingham1e58cef2012-04-13 20:38:13 +0000104 if (log)
Jim Ingham845970e2013-03-28 00:04:05 +0000105 log->Printf ("ThreadPlanCallFunction(%p): %s.", this, m_constructor_errors.GetData());
Jim Ingham1e58cef2012-04-13 20:38:13 +0000106 return false;
107 }
108 }
109
110 start_load_addr = m_start_addr.GetLoadAddress (target_sp.get());
111
112 // Checkpoint the thread state so we can restore it later.
113 if (log && log->GetVerbose())
114 ReportRegisterState ("About to checkpoint thread before function call. Original register state was:");
115
116 if (!thread.CheckpointThreadState (m_stored_thread_state))
117 {
Jim Ingham845970e2013-03-28 00:04:05 +0000118 m_constructor_errors.Printf ("Setting up ThreadPlanCallFunction, failed to checkpoint thread state.");
Jim Ingham1e58cef2012-04-13 20:38:13 +0000119 if (log)
Jim Ingham845970e2013-03-28 00:04:05 +0000120 log->Printf ("ThreadPlanCallFunction(%p): %s.", this, m_constructor_errors.GetData());
Jim Ingham1e58cef2012-04-13 20:38:13 +0000121 return false;
122 }
Jim Ingham1e58cef2012-04-13 20:38:13 +0000123 function_load_addr = m_function_addr.GetLoadAddress (target_sp.get());
124
125 return true;
126}
Chris Lattner24943d22010-06-08 16:52:24 +0000127
128ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
Matt Kopec4f9103f2013-02-27 20:13:38 +0000129 const Address &function,
Jim Ingham016ef882011-12-22 19:12:40 +0000130 const ClangASTType &return_type,
Greg Clayton989816b2011-05-14 01:50:35 +0000131 addr_t arg,
Chris Lattner24943d22010-06-08 16:52:24 +0000132 bool stop_other_threads,
Jim Inghamb7940202013-01-15 02:47:48 +0000133 bool unwind_on_error,
134 bool ignore_breakpoints,
Greg Clayton989816b2011-05-14 01:50:35 +0000135 addr_t *this_arg,
136 addr_t *cmd_arg) :
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000137 ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
Benjamin Kramer36a08102010-07-16 12:32:33 +0000138 m_valid (false),
139 m_stop_other_threads (stop_other_threads),
Jim Ingham574c3d62011-08-12 23:34:31 +0000140 m_function_addr (function),
Filipe Cabecinhas5ebd51f2012-09-11 18:11:07 +0000141 m_function_sp (0),
Jim Ingham016ef882011-12-22 19:12:40 +0000142 m_return_type (return_type),
Jim Inghamba560cc2011-11-01 02:46:54 +0000143 m_takedown_done (false),
Jim Ingham110b55f2012-05-11 18:43:38 +0000144 m_stop_address (LLDB_INVALID_ADDRESS),
Jim Inghamb7940202013-01-15 02:47:48 +0000145 m_unwind_on_error (unwind_on_error),
146 m_ignore_breakpoints (ignore_breakpoints)
Chris Lattner24943d22010-06-08 16:52:24 +0000147{
Jim Ingham1e58cef2012-04-13 20:38:13 +0000148 lldb::addr_t start_load_addr;
149 ABI *abi;
150 lldb::addr_t function_load_addr;
Jim Ingham110b55f2012-05-11 18:43:38 +0000151 if (!ConstructorSetup (thread, abi, start_load_addr, function_load_addr))
Greg Claytonf4124de2012-02-21 00:09:25 +0000152 return;
Chris Lattner24943d22010-06-08 16:52:24 +0000153
Greg Clayton61d4f7a2011-05-12 02:14:56 +0000154 if (this_arg && cmd_arg)
155 {
156 if (!abi->PrepareTrivialCall (thread,
157 m_function_sp,
Jim Ingham1e58cef2012-04-13 20:38:13 +0000158 function_load_addr,
Greg Clayton989816b2011-05-14 01:50:35 +0000159 start_load_addr,
Greg Clayton61d4f7a2011-05-12 02:14:56 +0000160 this_arg,
161 cmd_arg,
Greg Clayton989816b2011-05-14 01:50:35 +0000162 &arg))
Greg Clayton61d4f7a2011-05-12 02:14:56 +0000163 return;
164 }
165 else if (this_arg)
166 {
167 if (!abi->PrepareTrivialCall (thread,
168 m_function_sp,
Jim Ingham1e58cef2012-04-13 20:38:13 +0000169 function_load_addr,
Greg Clayton989816b2011-05-14 01:50:35 +0000170 start_load_addr,
Greg Clayton61d4f7a2011-05-12 02:14:56 +0000171 this_arg,
Greg Clayton989816b2011-05-14 01:50:35 +0000172 &arg))
Greg Clayton61d4f7a2011-05-12 02:14:56 +0000173 return;
174 }
175 else
176 {
177 if (!abi->PrepareTrivialCall (thread,
178 m_function_sp,
Jim Ingham1e58cef2012-04-13 20:38:13 +0000179 function_load_addr,
Greg Clayton989816b2011-05-14 01:50:35 +0000180 start_load_addr,
181 &arg))
182 return;
183 }
184
185 ReportRegisterState ("Function call was set up. Register state was:");
186
187 m_valid = true;
188}
189
190
191ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
Matt Kopec4f9103f2013-02-27 20:13:38 +0000192 const Address &function,
Jim Ingham016ef882011-12-22 19:12:40 +0000193 const ClangASTType &return_type,
Greg Clayton989816b2011-05-14 01:50:35 +0000194 bool stop_other_threads,
Jim Inghamb7940202013-01-15 02:47:48 +0000195 bool unwind_on_error,
196 bool ignore_breakpoints,
Greg Clayton989816b2011-05-14 01:50:35 +0000197 addr_t *arg1_ptr,
198 addr_t *arg2_ptr,
199 addr_t *arg3_ptr,
200 addr_t *arg4_ptr,
201 addr_t *arg5_ptr,
202 addr_t *arg6_ptr) :
203 ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
204 m_valid (false),
205 m_stop_other_threads (stop_other_threads),
Jim Ingham574c3d62011-08-12 23:34:31 +0000206 m_function_addr (function),
Jim Ingham76b258d2012-11-26 23:52:18 +0000207 m_function_sp (0),
Jim Ingham016ef882011-12-22 19:12:40 +0000208 m_return_type (return_type),
Jim Ingham1e58cef2012-04-13 20:38:13 +0000209 m_takedown_done (false),
Jim Ingham76b258d2012-11-26 23:52:18 +0000210 m_stop_address (LLDB_INVALID_ADDRESS),
Jim Inghamb7940202013-01-15 02:47:48 +0000211 m_unwind_on_error (unwind_on_error),
212 m_ignore_breakpoints (ignore_breakpoints)
Greg Clayton989816b2011-05-14 01:50:35 +0000213{
Jim Ingham1e58cef2012-04-13 20:38:13 +0000214 lldb::addr_t start_load_addr;
215 ABI *abi;
216 lldb::addr_t function_load_addr;
Jim Ingham110b55f2012-05-11 18:43:38 +0000217 if (!ConstructorSetup (thread, abi, start_load_addr, function_load_addr))
Greg Claytonf4124de2012-02-21 00:09:25 +0000218 return;
219
Greg Clayton989816b2011-05-14 01:50:35 +0000220 if (!abi->PrepareTrivialCall (thread,
Jim Ingham1e58cef2012-04-13 20:38:13 +0000221 m_function_sp,
222 function_load_addr,
Greg Clayton989816b2011-05-14 01:50:35 +0000223 start_load_addr,
224 arg1_ptr,
225 arg2_ptr,
226 arg3_ptr,
227 arg4_ptr,
228 arg5_ptr,
229 arg6_ptr))
230 {
Greg Clayton61d4f7a2011-05-12 02:14:56 +0000231 return;
232 }
Chris Lattner24943d22010-06-08 16:52:24 +0000233
Jim Ingham2f6267f2011-01-22 01:27:23 +0000234 ReportRegisterState ("Function call was set up. Register state was:");
235
236 m_valid = true;
237}
238
239ThreadPlanCallFunction::~ThreadPlanCallFunction ()
240{
Jim Ingham89e248f2013-02-09 01:29:05 +0000241 DoTakedown(PlanSucceeded());
Jim Ingham2f6267f2011-01-22 01:27:23 +0000242}
243
244void
245ThreadPlanCallFunction::ReportRegisterState (const char *message)
246{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000247 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_VERBOSE));
Sean Callanan6dff8272010-11-08 03:49:50 +0000248 if (log)
249 {
Greg Clayton628cead2011-05-19 03:54:16 +0000250 StreamString strm;
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000251 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
Jim Ingham2f6267f2011-01-22 01:27:23 +0000252
253 log->PutCString(message);
254
Greg Clayton628cead2011-05-19 03:54:16 +0000255 RegisterValue reg_value;
256
257 for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
258 reg_idx < num_registers;
259 ++reg_idx)
Sean Callanan6dff8272010-11-08 03:49:50 +0000260 {
Greg Clayton628cead2011-05-19 03:54:16 +0000261 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_idx);
262 if (reg_ctx->ReadRegister(reg_info, reg_value))
263 {
264 reg_value.Dump(&strm, reg_info, true, false, eFormatDefault);
265 strm.EOL();
266 }
Sean Callanan6dff8272010-11-08 03:49:50 +0000267 }
Greg Clayton628cead2011-05-19 03:54:16 +0000268 log->PutCString(strm.GetData());
Sean Callanan6dff8272010-11-08 03:49:50 +0000269 }
Sean Callanan14a97ff2010-11-04 01:51:38 +0000270}
271
272void
Jim Ingham038fa8e2012-05-10 01:35:39 +0000273ThreadPlanCallFunction::DoTakedown (bool success)
Sean Callanan14a97ff2010-11-04 01:51:38 +0000274{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000275 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghama8d26eb2012-04-13 23:11:52 +0000276
277 if (!m_valid)
278 {
279 //Don't call DoTakedown if we were never valid to begin with.
280 if (log)
281 log->Printf ("ThreadPlanCallFunction(%p): Log called on ThreadPlanCallFunction that was never valid.", this);
282 return;
283 }
284
Jim Ingham2f6267f2011-01-22 01:27:23 +0000285 if (!m_takedown_done)
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000286 {
Jim Ingham038fa8e2012-05-10 01:35:39 +0000287 if (success)
Greg Clayton2f085c62011-05-15 01:25:55 +0000288 {
Jim Ingham038fa8e2012-05-10 01:35:39 +0000289 ProcessSP process_sp (m_thread.GetProcess());
290 const ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
291 if (abi && m_return_type.IsValid())
292 {
293 const bool persistent = false;
294 m_return_valobj_sp = abi->GetReturnValueObject (m_thread, m_return_type, persistent);
295 }
Greg Clayton2f085c62011-05-15 01:25:55 +0000296 }
Jim Ingham2f6267f2011-01-22 01:27:23 +0000297 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000298 log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n", this, m_thread.GetID(), m_valid, IsPlanComplete());
Jim Ingham2f6267f2011-01-22 01:27:23 +0000299 m_takedown_done = true;
Jim Inghamba560cc2011-11-01 02:46:54 +0000300 m_stop_address = m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
Jim Ingham2370a972011-05-17 01:10:11 +0000301 m_real_stop_info_sp = GetPrivateStopReason();
Jim Ingham76b258d2012-11-26 23:52:18 +0000302 m_thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state);
Jim Ingham038fa8e2012-05-10 01:35:39 +0000303 SetPlanComplete(success);
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000304 ClearBreakpoints();
Jim Ingham2f6267f2011-01-22 01:27:23 +0000305 if (log && log->GetVerbose())
306 ReportRegisterState ("Restoring thread state after function call. Restored register state:");
Jim Ingham78108e62011-01-26 19:13:09 +0000307
Jim Ingham2f6267f2011-01-22 01:27:23 +0000308 }
309 else
310 {
311 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000312 log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called as no-op for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n", this, m_thread.GetID(), m_valid, IsPlanComplete());
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000313 }
Chris Lattner24943d22010-06-08 16:52:24 +0000314}
315
316void
Jim Ingham6c9662e2011-01-18 01:58:06 +0000317ThreadPlanCallFunction::WillPop ()
318{
Jim Ingham89e248f2013-02-09 01:29:05 +0000319 DoTakedown(PlanSucceeded());
Jim Ingham6c9662e2011-01-18 01:58:06 +0000320}
321
322void
Greg Clayton989816b2011-05-14 01:50:35 +0000323ThreadPlanCallFunction::GetDescription (Stream *s, DescriptionLevel level)
Chris Lattner24943d22010-06-08 16:52:24 +0000324{
Greg Clayton989816b2011-05-14 01:50:35 +0000325 if (level == eDescriptionLevelBrief)
Chris Lattner24943d22010-06-08 16:52:24 +0000326 {
327 s->Printf("Function call thread plan");
328 }
329 else
330 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000331 TargetSP target_sp (m_thread.CalculateTarget());
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000332 s->Printf("Thread plan to call 0x%" PRIx64, m_function_addr.GetLoadAddress(target_sp.get()));
Chris Lattner24943d22010-06-08 16:52:24 +0000333 }
334}
335
336bool
337ThreadPlanCallFunction::ValidatePlan (Stream *error)
338{
339 if (!m_valid)
Jim Ingham845970e2013-03-28 00:04:05 +0000340 {
341 if (error)
342 {
343 if (m_constructor_errors.GetSize() > 0)
344 error->PutCString (m_constructor_errors.GetData());
345 else
346 error->PutCString ("Unknown error");
347 }
Chris Lattner24943d22010-06-08 16:52:24 +0000348 return false;
Jim Ingham845970e2013-03-28 00:04:05 +0000349 }
Chris Lattner24943d22010-06-08 16:52:24 +0000350
351 return true;
352}
353
Jim Ingham89e248f2013-02-09 01:29:05 +0000354
355Vote
356ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr)
357{
358 if (m_takedown_done || IsPlanComplete())
359 return eVoteYes;
360 else
361 return ThreadPlan::ShouldReportStop(event_ptr);
362}
363
Chris Lattner24943d22010-06-08 16:52:24 +0000364bool
Jim Ingham89e248f2013-02-09 01:29:05 +0000365ThreadPlanCallFunction::PlanExplainsStop (Event *event_ptr)
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000366{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000367 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP|LIBLLDB_LOG_PROCESS));
Jim Ingham2370a972011-05-17 01:10:11 +0000368 m_real_stop_info_sp = GetPrivateStopReason();
369
Jim Ingham988ddbc2010-10-26 00:27:45 +0000370 // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
371 // we answer yes.
Jim Ingham89e248f2013-02-09 01:29:05 +0000372 if (m_subplan_sp.get() != NULL && m_subplan_sp->PlanExplainsStop(event_ptr))
Jim Inghamb7940202013-01-15 02:47:48 +0000373 {
374 SetPlanComplete();
Jim Ingham988ddbc2010-10-26 00:27:45 +0000375 return true;
Jim Inghamb7940202013-01-15 02:47:48 +0000376 }
Sean Callananba8547d2010-10-19 22:24:06 +0000377
Sean Callanan94fb5432010-11-03 19:36:28 +0000378 // Check if the breakpoint is one of ours.
379
Jim Ingham038fa8e2012-05-10 01:35:39 +0000380 StopReason stop_reason;
381 if (!m_real_stop_info_sp)
382 stop_reason = eStopReasonNone;
383 else
384 stop_reason = m_real_stop_info_sp->GetStopReason();
Jim Ingham89e248f2013-02-09 01:29:05 +0000385 if (log)
386 log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - %s.", Thread::StopReasonAsCString(stop_reason));
Jim Ingham038fa8e2012-05-10 01:35:39 +0000387
388 if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
Sean Callanan94fb5432010-11-03 19:36:28 +0000389 return true;
390
Jim Ingham89e248f2013-02-09 01:29:05 +0000391 // We control breakpoints separately from other "stop reasons." So first,
392 // check the case where we stopped for an internal breakpoint, in that case, continue on.
393 // If it is not an internal breakpoint, consult m_ignore_breakpoints.
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000394
Jim Ingham038fa8e2012-05-10 01:35:39 +0000395
396 if (stop_reason == eStopReasonBreakpoint)
Jim Ingham988ddbc2010-10-26 00:27:45 +0000397 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000398 ProcessSP process_sp (m_thread.CalculateProcess());
Jim Ingham2370a972011-05-17 01:10:11 +0000399 uint64_t break_site_id = m_real_stop_info_sp->GetValue();
Greg Claytonf4124de2012-02-21 00:09:25 +0000400 BreakpointSiteSP bp_site_sp;
401 if (process_sp)
402 bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
Jim Ingham988ddbc2010-10-26 00:27:45 +0000403 if (bp_site_sp)
404 {
405 uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
406 bool is_internal = true;
407 for (uint32_t i = 0; i < num_owners; i++)
408 {
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000409 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
Jim Ingham89e248f2013-02-09 01:29:05 +0000410 if (log)
411 log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: hit breakpoint %d while calling function", bp.GetID());
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000412
413 if (!bp.IsInternal())
Jim Ingham988ddbc2010-10-26 00:27:45 +0000414 {
415 is_internal = false;
416 break;
417 }
418 }
419 if (is_internal)
Jim Ingham89e248f2013-02-09 01:29:05 +0000420 {
421 if (log)
422 log->Printf ("ThreadPlanCallFunction::PlanExplainsStop hit an internal breakpoint, not stopping.");
Jim Ingham988ddbc2010-10-26 00:27:45 +0000423 return false;
Jim Ingham89e248f2013-02-09 01:29:05 +0000424 }
Jim Ingham988ddbc2010-10-26 00:27:45 +0000425 }
Jim Ingham89e248f2013-02-09 01:29:05 +0000426
Jim Inghamb7940202013-01-15 02:47:48 +0000427 if (m_ignore_breakpoints)
Jim Ingham110b55f2012-05-11 18:43:38 +0000428 {
Jim Ingham89e248f2013-02-09 01:29:05 +0000429 if (log)
430 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
431 m_real_stop_info_sp->OverrideShouldStop(false);
Jim Ingham110b55f2012-05-11 18:43:38 +0000432 return true;
433 }
434 else
Jim Ingham89e248f2013-02-09 01:29:05 +0000435 {
436 if (log)
437 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are not ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
438 m_real_stop_info_sp->OverrideShouldStop(true);
Jim Ingham110b55f2012-05-11 18:43:38 +0000439 return false;
Jim Ingham89e248f2013-02-09 01:29:05 +0000440 }
441 }
442 else if (!m_unwind_on_error)
443 {
444 // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
445 return false;
Jim Ingham988ddbc2010-10-26 00:27:45 +0000446 }
447 else
448 {
449 // If the subplan is running, any crashes are attributable to us.
Jim Ingham78108e62011-01-26 19:13:09 +0000450 // If we want to discard the plan, then we say we explain the stop
451 // but if we are going to be discarded, let whoever is above us
452 // explain the stop.
Jim Ingham89e248f2013-02-09 01:29:05 +0000453 // But don't discard the plan if the stop would restart itself (for instance if it is a
454 // signal that is set not to stop. Check that here first. We just say we explain the stop
455 // but aren't done and everything will continue on from there.
456
457 if (m_real_stop_info_sp->ShouldStopSynchronous(event_ptr))
Jim Ingham038fa8e2012-05-10 01:35:39 +0000458 {
Jim Ingham89e248f2013-02-09 01:29:05 +0000459 SetPlanComplete(false);
460 if (m_subplan_sp)
Jim Ingham038fa8e2012-05-10 01:35:39 +0000461 {
Jim Ingham89e248f2013-02-09 01:29:05 +0000462 if (m_unwind_on_error)
463 return true;
464 else
465 return false;
Jim Ingham038fa8e2012-05-10 01:35:39 +0000466 }
467 else
468 return false;
469 }
470 else
Jim Ingham89e248f2013-02-09 01:29:05 +0000471 return true;
Jim Ingham988ddbc2010-10-26 00:27:45 +0000472 }
Chris Lattner24943d22010-06-08 16:52:24 +0000473}
474
475bool
476ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
477{
Jim Inghamb7940202013-01-15 02:47:48 +0000478 // We do some computation in PlanExplainsStop that may or may not set the plan as complete.
479 // We need to do that here to make sure our state is correct.
Jim Ingham89e248f2013-02-09 01:29:05 +0000480 PlanExplainsStop(event_ptr);
Jim Inghamb7940202013-01-15 02:47:48 +0000481
482 if (IsPlanComplete())
Chris Lattner24943d22010-06-08 16:52:24 +0000483 {
Jim Ingham2f6267f2011-01-22 01:27:23 +0000484 ReportRegisterState ("Function completed. Register state was:");
Chris Lattner24943d22010-06-08 16:52:24 +0000485 return true;
486 }
487 else
488 {
489 return false;
490 }
491}
492
493bool
494ThreadPlanCallFunction::StopOthers ()
495{
496 return m_stop_other_threads;
497}
498
499void
500ThreadPlanCallFunction::SetStopOthers (bool new_value)
501{
502 if (m_subplan_sp)
503 {
504 ThreadPlanRunToAddress *address_plan = static_cast<ThreadPlanRunToAddress *>(m_subplan_sp.get());
505 address_plan->SetStopOthers(new_value);
506 }
507 m_stop_other_threads = new_value;
508}
509
510StateType
Jim Ingham745ac7a2010-11-11 19:26:09 +0000511ThreadPlanCallFunction::GetPlanRunState ()
Chris Lattner24943d22010-06-08 16:52:24 +0000512{
513 return eStateRunning;
514}
515
516void
517ThreadPlanCallFunction::DidPush ()
518{
Sean Callananc2c6f772010-10-26 00:31:56 +0000519//#define SINGLE_STEP_EXPRESSIONS
520
Jim Ingham76b258d2012-11-26 23:52:18 +0000521 // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
522 // Wait till the plan is pushed so we aren't changing the stop info till we're about to run.
523
524 GetThread().SetStopInfoToNothing();
525
Sean Callananc2c6f772010-10-26 00:31:56 +0000526#ifndef SINGLE_STEP_EXPRESSIONS
Chris Lattner24943d22010-06-08 16:52:24 +0000527 m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
528
529 m_thread.QueueThreadPlan(m_subplan_sp, false);
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000530 m_subplan_sp->SetPrivate (true);
Sean Callananc2c6f772010-10-26 00:31:56 +0000531#endif
Chris Lattner24943d22010-06-08 16:52:24 +0000532}
533
534bool
535ThreadPlanCallFunction::WillStop ()
536{
537 return true;
538}
539
540bool
541ThreadPlanCallFunction::MischiefManaged ()
542{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000543 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamb7940202013-01-15 02:47:48 +0000544
Chris Lattner24943d22010-06-08 16:52:24 +0000545 if (IsPlanComplete())
546 {
Chris Lattner24943d22010-06-08 16:52:24 +0000547 if (log)
Jim Inghama8d26eb2012-04-13 23:11:52 +0000548 log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.", this);
Chris Lattner24943d22010-06-08 16:52:24 +0000549
550 ThreadPlan::MischiefManaged ();
551 return true;
552 }
553 else
554 {
555 return false;
556 }
557}
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000558
559void
560ThreadPlanCallFunction::SetBreakpoints ()
561{
Greg Claytonf4124de2012-02-21 00:09:25 +0000562 ProcessSP process_sp (m_thread.CalculateProcess());
563 if (process_sp)
564 {
565 m_cxx_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus);
566 m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC);
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000567
Greg Claytonf4124de2012-02-21 00:09:25 +0000568 if (m_cxx_language_runtime)
569 m_cxx_language_runtime->SetExceptionBreakpoints();
570 if (m_objc_language_runtime)
571 m_objc_language_runtime->SetExceptionBreakpoints();
572 }
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000573}
574
575void
576ThreadPlanCallFunction::ClearBreakpoints ()
577{
Sean Callanan29756d42010-11-03 22:19:38 +0000578 if (m_cxx_language_runtime)
579 m_cxx_language_runtime->ClearExceptionBreakpoints();
580 if (m_objc_language_runtime)
581 m_objc_language_runtime->ClearExceptionBreakpoints();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000582}
Sean Callanan94fb5432010-11-03 19:36:28 +0000583
584bool
585ThreadPlanCallFunction::BreakpointsExplainStop()
586{
Greg Clayton989816b2011-05-14 01:50:35 +0000587 StopInfoSP stop_info_sp = GetPrivateStopReason();
Sean Callanan94fb5432010-11-03 19:36:28 +0000588
Jim Inghamb7940202013-01-15 02:47:48 +0000589 if ((m_cxx_language_runtime &&
590 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
591 ||(m_objc_language_runtime &&
592 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp)))
593 {
594 SetPlanComplete(false);
Sean Callanan29756d42010-11-03 22:19:38 +0000595 return true;
Jim Inghamb7940202013-01-15 02:47:48 +0000596 }
Sean Callanan94fb5432010-11-03 19:36:28 +0000597
Sean Callanan94fb5432010-11-03 19:36:28 +0000598 return false;
599}
Jim Ingham76b258d2012-11-26 23:52:18 +0000600
601bool
602ThreadPlanCallFunction::RestoreThreadState()
603{
604 return GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state);
605}
606