blob: 7d798fc6b2064ce7887a3267ebe8ea5c1fd169f8 [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"
22#include "lldb/Core/Stream.h"
Sean Callanan29756d42010-11-03 22:19:38 +000023#include "lldb/Target/LanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Target/Process.h"
25#include "lldb/Target/RegisterContext.h"
Jim Ingham988ddbc2010-10-26 00:27:45 +000026#include "lldb/Target/StopInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Target/Target.h"
28#include "lldb/Target/Thread.h"
29#include "lldb/Target/ThreadPlanRunToAddress.h"
30
31using namespace lldb;
32using namespace lldb_private;
33
34//----------------------------------------------------------------------
35// ThreadPlanCallFunction: Plan to call a single function
36//----------------------------------------------------------------------
37
38ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
39 Address &function,
Greg Clayton989816b2011-05-14 01:50:35 +000040 addr_t arg,
Chris Lattner24943d22010-06-08 16:52:24 +000041 bool stop_other_threads,
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000042 bool discard_on_error,
Greg Clayton989816b2011-05-14 01:50:35 +000043 addr_t *this_arg,
44 addr_t *cmd_arg) :
Jim Ingham5a47e8b2010-06-19 04:45:32 +000045 ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
Benjamin Kramer36a08102010-07-16 12:32:33 +000046 m_valid (false),
47 m_stop_other_threads (stop_other_threads),
Benjamin Kramer36a08102010-07-16 12:32:33 +000048 m_process (thread.GetProcess()),
Jim Ingham2f6267f2011-01-22 01:27:23 +000049 m_thread (thread),
Sean Callanan0ddf8062011-05-09 22:04:36 +000050 m_takedown_done (false),
Greg Clayton989816b2011-05-14 01:50:35 +000051 m_function_sp (NULL)
Chris Lattner24943d22010-06-08 16:52:24 +000052{
Chris Lattner24943d22010-06-08 16:52:24 +000053 SetOkayToDiscard (discard_on_error);
54
55 Process& process = thread.GetProcess();
56 Target& target = process.GetTarget();
Greg Clayton75906e42011-05-11 18:39:18 +000057 const ABI *abi = process.GetABI().get();
Sean Callanan07f3d8d2010-11-03 01:37:52 +000058
Chris Lattner24943d22010-06-08 16:52:24 +000059 if (!abi)
60 return;
Sean Callanan07f3d8d2010-11-03 01:37:52 +000061
Jim Ingham15dcb7c2011-01-20 02:03:18 +000062 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
63
Sean Callanan07f3d8d2010-11-03 01:37:52 +000064 SetBreakpoints();
65
Sean Callanan0ddf8062011-05-09 22:04:36 +000066 m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
Chris Lattner24943d22010-06-08 16:52:24 +000067
Chris Lattner24943d22010-06-08 16:52:24 +000068 ModuleSP executableModuleSP (target.GetExecutableModule());
69
Jim Ingham28775942011-03-07 23:44:08 +000070 if (!executableModuleSP)
71 {
72 log->Printf ("Can't execute code without an executable module.");
Chris Lattner24943d22010-06-08 16:52:24 +000073 return;
Jim Ingham28775942011-03-07 23:44:08 +000074 }
75 else
76 {
77 ObjectFile *objectFile = executableModuleSP->GetObjectFile();
78 if (!objectFile)
79 {
80 log->Printf ("Could not find object file for module \"%s\".",
81 executableModuleSP->GetFileSpec().GetFilename().AsCString());
82 return;
83 }
84 m_start_addr = objectFile->GetEntryPointAddress();
85 if (!m_start_addr.IsValid())
86 {
87 log->Printf ("Could not find entry point address for executable module \"%s\".",
88 executableModuleSP->GetFileSpec().GetFilename().AsCString());
89 return;
90 }
91 }
Chris Lattner24943d22010-06-08 16:52:24 +000092
Greg Clayton989816b2011-05-14 01:50:35 +000093 addr_t start_load_addr = m_start_addr.GetLoadAddress(&target);
Jim Ingham28775942011-03-07 23:44:08 +000094
Jim Ingham15dcb7c2011-01-20 02:03:18 +000095 // Checkpoint the thread state so we can restore it later.
Jim Ingham2f6267f2011-01-22 01:27:23 +000096 if (log && log->GetVerbose())
97 ReportRegisterState ("About to checkpoint thread before function call. Original register state was:");
98
Jim Ingham15dcb7c2011-01-20 02:03:18 +000099 if (!thread.CheckpointThreadState (m_stored_thread_state))
100 {
101 if (log)
102 log->Printf ("Setting up ThreadPlanCallFunction, failed to checkpoint thread state.");
Chris Lattner24943d22010-06-08 16:52:24 +0000103 return;
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000104 }
105 // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
106 thread.SetStopInfoToNothing();
107
Chris Lattner24943d22010-06-08 16:52:24 +0000108 m_function_addr = function;
Greg Clayton989816b2011-05-14 01:50:35 +0000109 addr_t FunctionLoadAddr = m_function_addr.GetLoadAddress(&target);
Chris Lattner24943d22010-06-08 16:52:24 +0000110
Greg Clayton61d4f7a2011-05-12 02:14:56 +0000111 if (this_arg && cmd_arg)
112 {
113 if (!abi->PrepareTrivialCall (thread,
114 m_function_sp,
115 FunctionLoadAddr,
Greg Clayton989816b2011-05-14 01:50:35 +0000116 start_load_addr,
Greg Clayton61d4f7a2011-05-12 02:14:56 +0000117 this_arg,
118 cmd_arg,
Greg Clayton989816b2011-05-14 01:50:35 +0000119 &arg))
Greg Clayton61d4f7a2011-05-12 02:14:56 +0000120 return;
121 }
122 else if (this_arg)
123 {
124 if (!abi->PrepareTrivialCall (thread,
125 m_function_sp,
126 FunctionLoadAddr,
Greg Clayton989816b2011-05-14 01:50:35 +0000127 start_load_addr,
Greg Clayton61d4f7a2011-05-12 02:14:56 +0000128 this_arg,
Greg Clayton989816b2011-05-14 01:50:35 +0000129 &arg))
Greg Clayton61d4f7a2011-05-12 02:14:56 +0000130 return;
131 }
132 else
133 {
134 if (!abi->PrepareTrivialCall (thread,
135 m_function_sp,
136 FunctionLoadAddr,
Greg Clayton989816b2011-05-14 01:50:35 +0000137 start_load_addr,
138 &arg))
139 return;
140 }
141
142 ReportRegisterState ("Function call was set up. Register state was:");
143
144 m_valid = true;
145}
146
147
148ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
149 Address &function,
150 bool stop_other_threads,
151 bool discard_on_error,
152 addr_t *arg1_ptr,
153 addr_t *arg2_ptr,
154 addr_t *arg3_ptr,
155 addr_t *arg4_ptr,
156 addr_t *arg5_ptr,
157 addr_t *arg6_ptr) :
158 ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
159 m_valid (false),
160 m_stop_other_threads (stop_other_threads),
161 m_process (thread.GetProcess()),
162 m_thread (thread),
163 m_takedown_done (false),
164 m_function_sp(NULL)
165{
166 SetOkayToDiscard (discard_on_error);
167
168 Process& process = thread.GetProcess();
169 Target& target = process.GetTarget();
170 const ABI *abi = process.GetABI().get();
171
172 if (!abi)
173 return;
174
175 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
176
177 SetBreakpoints();
178
179 m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
180
181 ModuleSP executableModuleSP (target.GetExecutableModule());
182
183 if (!executableModuleSP)
184 {
185 log->Printf ("Can't execute code without an executable module.");
186 return;
187 }
188 else
189 {
190 ObjectFile *objectFile = executableModuleSP->GetObjectFile();
191 if (!objectFile)
192 {
193 log->Printf ("Could not find object file for module \"%s\".",
194 executableModuleSP->GetFileSpec().GetFilename().AsCString());
195 return;
196 }
197 m_start_addr = objectFile->GetEntryPointAddress();
198 if (!m_start_addr.IsValid())
199 {
200 log->Printf ("Could not find entry point address for executable module \"%s\".",
201 executableModuleSP->GetFileSpec().GetFilename().AsCString());
202 return;
203 }
204 }
205
206 addr_t start_load_addr = m_start_addr.GetLoadAddress(&target);
207
208 // Checkpoint the thread state so we can restore it later.
209 if (log && log->GetVerbose())
210 ReportRegisterState ("About to checkpoint thread before function call. Original register state was:");
211
212 if (!thread.CheckpointThreadState (m_stored_thread_state))
213 {
214 if (log)
215 log->Printf ("Setting up ThreadPlanCallFunction, failed to checkpoint thread state.");
216 return;
217 }
218 // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
219 thread.SetStopInfoToNothing();
220
221 m_function_addr = function;
222 addr_t FunctionLoadAddr = m_function_addr.GetLoadAddress(&target);
223
224 if (!abi->PrepareTrivialCall (thread,
225 m_function_sp,
226 FunctionLoadAddr,
227 start_load_addr,
228 arg1_ptr,
229 arg2_ptr,
230 arg3_ptr,
231 arg4_ptr,
232 arg5_ptr,
233 arg6_ptr))
234 {
Greg Clayton61d4f7a2011-05-12 02:14:56 +0000235 return;
236 }
Chris Lattner24943d22010-06-08 16:52:24 +0000237
Jim Ingham2f6267f2011-01-22 01:27:23 +0000238 ReportRegisterState ("Function call was set up. Register state was:");
239
240 m_valid = true;
241}
242
243ThreadPlanCallFunction::~ThreadPlanCallFunction ()
244{
245}
246
247void
248ThreadPlanCallFunction::ReportRegisterState (const char *message)
249{
250 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Sean Callanan6dff8272010-11-08 03:49:50 +0000251 if (log)
252 {
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000253 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
Jim Ingham2f6267f2011-01-22 01:27:23 +0000254
255 log->PutCString(message);
256
Sean Callanan6dff8272010-11-08 03:49:50 +0000257 for (uint32_t register_index = 0, num_registers = reg_ctx->GetRegisterCount();
258 register_index < num_registers;
259 ++register_index)
260 {
261 const char *register_name = reg_ctx->GetRegisterName(register_index);
262 uint64_t register_value = reg_ctx->ReadRegisterAsUnsigned(register_index, LLDB_INVALID_ADDRESS);
263
264 log->Printf(" %s = 0x%llx", register_name, register_value);
265 }
266 }
Sean Callanan14a97ff2010-11-04 01:51:38 +0000267}
268
269void
270ThreadPlanCallFunction::DoTakedown ()
271{
Jim Ingham2f6267f2011-01-22 01:27:23 +0000272 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
273 if (!m_takedown_done)
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000274 {
Greg Clayton2f085c62011-05-15 01:25:55 +0000275 // TODO: how do we tell if all went well?
276 if (m_return_value_sp)
277 {
278 const ABI *abi = m_thread.GetProcess().GetABI().get();
279 if (abi)
280 abi->GetReturnValue(m_thread, *m_return_value_sp);
281 }
Jim Ingham2f6267f2011-01-22 01:27:23 +0000282 if (log)
283 log->Printf ("DoTakedown called for thread 0x%4.4x, m_valid: %d complete: %d.\n", m_thread.GetID(), m_valid, IsPlanComplete());
284 m_takedown_done = true;
Jim Ingham2370a972011-05-17 01:10:11 +0000285 m_real_stop_info_sp = GetPrivateStopReason();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000286 m_thread.RestoreThreadStateFromCheckpoint(m_stored_thread_state);
287 SetPlanComplete();
288 ClearBreakpoints();
Jim Ingham2f6267f2011-01-22 01:27:23 +0000289 if (log && log->GetVerbose())
290 ReportRegisterState ("Restoring thread state after function call. Restored register state:");
Jim Ingham78108e62011-01-26 19:13:09 +0000291
Jim Ingham2f6267f2011-01-22 01:27:23 +0000292 }
293 else
294 {
295 if (log)
296 log->Printf ("DoTakedown called as no-op for thread 0x%4.4x, m_valid: %d complete: %d.\n", m_thread.GetID(), m_valid, IsPlanComplete());
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000297 }
Chris Lattner24943d22010-06-08 16:52:24 +0000298}
299
300void
Jim Ingham6c9662e2011-01-18 01:58:06 +0000301ThreadPlanCallFunction::WillPop ()
302{
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000303 DoTakedown();
Jim Ingham6c9662e2011-01-18 01:58:06 +0000304}
305
306void
Greg Clayton989816b2011-05-14 01:50:35 +0000307ThreadPlanCallFunction::GetDescription (Stream *s, DescriptionLevel level)
Chris Lattner24943d22010-06-08 16:52:24 +0000308{
Greg Clayton989816b2011-05-14 01:50:35 +0000309 if (level == eDescriptionLevelBrief)
Chris Lattner24943d22010-06-08 16:52:24 +0000310 {
311 s->Printf("Function call thread plan");
312 }
313 else
314 {
Greg Clayton989816b2011-05-14 01:50:35 +0000315 s->Printf("Thread plan to call 0x%llx", m_function_addr.GetLoadAddress(&m_process.GetTarget()));
Chris Lattner24943d22010-06-08 16:52:24 +0000316 }
317}
318
319bool
320ThreadPlanCallFunction::ValidatePlan (Stream *error)
321{
322 if (!m_valid)
323 return false;
324
325 return true;
326}
327
328bool
329ThreadPlanCallFunction::PlanExplainsStop ()
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000330{
Jim Ingham2370a972011-05-17 01:10:11 +0000331 m_real_stop_info_sp = GetPrivateStopReason();
332
Jim Ingham988ddbc2010-10-26 00:27:45 +0000333 // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
334 // we answer yes.
335 if(m_subplan_sp.get() != NULL && m_subplan_sp->PlanExplainsStop())
336 return true;
Sean Callananba8547d2010-10-19 22:24:06 +0000337
Sean Callanan94fb5432010-11-03 19:36:28 +0000338 // Check if the breakpoint is one of ours.
339
340 if (BreakpointsExplainStop())
341 return true;
342
Jim Ingham988ddbc2010-10-26 00:27:45 +0000343 // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
344 if (!OkayToDiscard())
345 return false;
346
347 // Otherwise, check the case where we stopped for an internal breakpoint, in that case, continue on.
348 // If it is not an internal breakpoint, consult OkayToDiscard.
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000349
Jim Ingham2370a972011-05-17 01:10:11 +0000350 if (m_real_stop_info_sp && m_real_stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
Jim Ingham988ddbc2010-10-26 00:27:45 +0000351 {
Jim Ingham2370a972011-05-17 01:10:11 +0000352 uint64_t break_site_id = m_real_stop_info_sp->GetValue();
Greg Clayton989816b2011-05-14 01:50:35 +0000353 BreakpointSiteSP bp_site_sp = m_thread.GetProcess().GetBreakpointSiteList().FindByID(break_site_id);
Jim Ingham988ddbc2010-10-26 00:27:45 +0000354 if (bp_site_sp)
355 {
356 uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
357 bool is_internal = true;
358 for (uint32_t i = 0; i < num_owners; i++)
359 {
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000360 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000361
362 if (!bp.IsInternal())
Jim Ingham988ddbc2010-10-26 00:27:45 +0000363 {
364 is_internal = false;
365 break;
366 }
367 }
368 if (is_internal)
369 return false;
370 }
371
372 return OkayToDiscard();
373 }
374 else
375 {
376 // If the subplan is running, any crashes are attributable to us.
Jim Ingham78108e62011-01-26 19:13:09 +0000377 // If we want to discard the plan, then we say we explain the stop
378 // but if we are going to be discarded, let whoever is above us
379 // explain the stop.
380 return ((m_subplan_sp.get() != NULL) && !OkayToDiscard());
Jim Ingham988ddbc2010-10-26 00:27:45 +0000381 }
Chris Lattner24943d22010-06-08 16:52:24 +0000382}
383
384bool
385ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
386{
387 if (PlanExplainsStop())
388 {
Jim Ingham2f6267f2011-01-22 01:27:23 +0000389 ReportRegisterState ("Function completed. Register state was:");
Sean Callananf5857a02010-07-31 01:32:05 +0000390
Sean Callanan14a97ff2010-11-04 01:51:38 +0000391 DoTakedown();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000392
Chris Lattner24943d22010-06-08 16:52:24 +0000393 return true;
394 }
395 else
396 {
397 return false;
398 }
399}
400
401bool
402ThreadPlanCallFunction::StopOthers ()
403{
404 return m_stop_other_threads;
405}
406
407void
408ThreadPlanCallFunction::SetStopOthers (bool new_value)
409{
410 if (m_subplan_sp)
411 {
412 ThreadPlanRunToAddress *address_plan = static_cast<ThreadPlanRunToAddress *>(m_subplan_sp.get());
413 address_plan->SetStopOthers(new_value);
414 }
415 m_stop_other_threads = new_value;
416}
417
418StateType
Jim Ingham745ac7a2010-11-11 19:26:09 +0000419ThreadPlanCallFunction::GetPlanRunState ()
Chris Lattner24943d22010-06-08 16:52:24 +0000420{
421 return eStateRunning;
422}
423
424void
425ThreadPlanCallFunction::DidPush ()
426{
Sean Callananc2c6f772010-10-26 00:31:56 +0000427//#define SINGLE_STEP_EXPRESSIONS
428
429#ifndef SINGLE_STEP_EXPRESSIONS
Chris Lattner24943d22010-06-08 16:52:24 +0000430 m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
431
432 m_thread.QueueThreadPlan(m_subplan_sp, false);
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000433 m_subplan_sp->SetPrivate (true);
Sean Callananc2c6f772010-10-26 00:31:56 +0000434#endif
Chris Lattner24943d22010-06-08 16:52:24 +0000435}
436
437bool
438ThreadPlanCallFunction::WillStop ()
439{
440 return true;
441}
442
443bool
444ThreadPlanCallFunction::MischiefManaged ()
445{
446 if (IsPlanComplete())
447 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000448 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000449
450 if (log)
451 log->Printf("Completed call function plan.");
452
453 ThreadPlan::MischiefManaged ();
454 return true;
455 }
456 else
457 {
458 return false;
459 }
460}
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000461
462void
463ThreadPlanCallFunction::SetBreakpoints ()
464{
Sean Callanan29756d42010-11-03 22:19:38 +0000465 m_cxx_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeC_plus_plus);
466 m_objc_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeObjC);
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000467
Sean Callanan29756d42010-11-03 22:19:38 +0000468 if (m_cxx_language_runtime)
469 m_cxx_language_runtime->SetExceptionBreakpoints();
470 if (m_objc_language_runtime)
471 m_objc_language_runtime->SetExceptionBreakpoints();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000472}
473
474void
475ThreadPlanCallFunction::ClearBreakpoints ()
476{
Sean Callanan29756d42010-11-03 22:19:38 +0000477 if (m_cxx_language_runtime)
478 m_cxx_language_runtime->ClearExceptionBreakpoints();
479 if (m_objc_language_runtime)
480 m_objc_language_runtime->ClearExceptionBreakpoints();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000481}
Sean Callanan94fb5432010-11-03 19:36:28 +0000482
483bool
484ThreadPlanCallFunction::BreakpointsExplainStop()
485{
Greg Clayton989816b2011-05-14 01:50:35 +0000486 StopInfoSP stop_info_sp = GetPrivateStopReason();
Sean Callanan94fb5432010-11-03 19:36:28 +0000487
Sean Callanan29756d42010-11-03 22:19:38 +0000488 if (m_cxx_language_runtime &&
489 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
490 return true;
Sean Callanan94fb5432010-11-03 19:36:28 +0000491
Sean Callanan29756d42010-11-03 22:19:38 +0000492 if (m_objc_language_runtime &&
493 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
494 return true;
Sean Callanan94fb5432010-11-03 19:36:28 +0000495
496 return false;
497}