blob: d3b62e6c7aaaf1e74cb35dd39cb4c22fb17324f4 [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,
40 lldb::addr_t arg,
41 bool stop_other_threads,
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000042 bool discard_on_error,
Sean Callanan3aa7da52010-12-13 22:46:15 +000043 lldb::addr_t *this_arg,
44 lldb::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),
Chris Lattner24943d22010-06-08 16:52:24 +000048 m_arg_addr (arg),
49 m_args (NULL),
Benjamin Kramer36a08102010-07-16 12:32:33 +000050 m_process (thread.GetProcess()),
Jim Ingham2f6267f2011-01-22 01:27:23 +000051 m_thread (thread),
52 m_takedown_done (false)
Chris Lattner24943d22010-06-08 16:52:24 +000053{
Chris Lattner24943d22010-06-08 16:52:24 +000054 SetOkayToDiscard (discard_on_error);
55
56 Process& process = thread.GetProcess();
57 Target& target = process.GetTarget();
58 const ABI *abi = process.GetABI();
Sean Callanan07f3d8d2010-11-03 01:37:52 +000059
Chris Lattner24943d22010-06-08 16:52:24 +000060 if (!abi)
61 return;
Sean Callanan07f3d8d2010-11-03 01:37:52 +000062
Jim Ingham15dcb7c2011-01-20 02:03:18 +000063 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
64
Sean Callanan07f3d8d2010-11-03 01:37:52 +000065 SetBreakpoints();
66
Chris Lattner24943d22010-06-08 16:52:24 +000067 lldb::addr_t spBelowRedZone = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
68
69 SymbolContextList contexts;
70 SymbolContext context;
71 ModuleSP executableModuleSP (target.GetExecutableModule());
72
73 if (!executableModuleSP ||
74 !executableModuleSP->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
75 return;
76
77 contexts.GetContextAtIndex(0, context);
78
79 m_start_addr = context.symbol->GetValue();
Greg Claytoneea26402010-09-14 23:36:40 +000080 lldb::addr_t StartLoadAddr = m_start_addr.GetLoadAddress(&target);
Chris Lattner24943d22010-06-08 16:52:24 +000081
Jim Ingham15dcb7c2011-01-20 02:03:18 +000082 // Checkpoint the thread state so we can restore it later.
Jim Ingham2f6267f2011-01-22 01:27:23 +000083 if (log && log->GetVerbose())
84 ReportRegisterState ("About to checkpoint thread before function call. Original register state was:");
85
Jim Ingham15dcb7c2011-01-20 02:03:18 +000086 if (!thread.CheckpointThreadState (m_stored_thread_state))
87 {
88 if (log)
89 log->Printf ("Setting up ThreadPlanCallFunction, failed to checkpoint thread state.");
Chris Lattner24943d22010-06-08 16:52:24 +000090 return;
Jim Ingham15dcb7c2011-01-20 02:03:18 +000091 }
92 // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
93 thread.SetStopInfoToNothing();
94
Chris Lattner24943d22010-06-08 16:52:24 +000095 m_function_addr = function;
Greg Claytoneea26402010-09-14 23:36:40 +000096 lldb::addr_t FunctionLoadAddr = m_function_addr.GetLoadAddress(&target);
Chris Lattner24943d22010-06-08 16:52:24 +000097
98 if (!abi->PrepareTrivialCall(thread,
99 spBelowRedZone,
100 FunctionLoadAddr,
101 StartLoadAddr,
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000102 m_arg_addr,
Sean Callanan3aa7da52010-12-13 22:46:15 +0000103 this_arg,
104 cmd_arg))
Chris Lattner24943d22010-06-08 16:52:24 +0000105 return;
106
Jim Ingham2f6267f2011-01-22 01:27:23 +0000107 ReportRegisterState ("Function call was set up. Register state was:");
108
109 m_valid = true;
110}
111
112ThreadPlanCallFunction::~ThreadPlanCallFunction ()
113{
114}
115
116void
117ThreadPlanCallFunction::ReportRegisterState (const char *message)
118{
119 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Sean Callanan6dff8272010-11-08 03:49:50 +0000120 if (log)
121 {
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000122 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
Jim Ingham2f6267f2011-01-22 01:27:23 +0000123
124 log->PutCString(message);
125
Sean Callanan6dff8272010-11-08 03:49:50 +0000126 for (uint32_t register_index = 0, num_registers = reg_ctx->GetRegisterCount();
127 register_index < num_registers;
128 ++register_index)
129 {
130 const char *register_name = reg_ctx->GetRegisterName(register_index);
131 uint64_t register_value = reg_ctx->ReadRegisterAsUnsigned(register_index, LLDB_INVALID_ADDRESS);
132
133 log->Printf(" %s = 0x%llx", register_name, register_value);
134 }
135 }
Sean Callanan14a97ff2010-11-04 01:51:38 +0000136}
137
138void
139ThreadPlanCallFunction::DoTakedown ()
140{
Jim Ingham2f6267f2011-01-22 01:27:23 +0000141 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
142 if (!m_takedown_done)
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000143 {
Jim Ingham2f6267f2011-01-22 01:27:23 +0000144 if (log)
145 log->Printf ("DoTakedown called for thread 0x%4.4x, m_valid: %d complete: %d.\n", m_thread.GetID(), m_valid, IsPlanComplete());
146 m_takedown_done = true;
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000147 m_thread.RestoreThreadStateFromCheckpoint(m_stored_thread_state);
148 SetPlanComplete();
149 ClearBreakpoints();
Jim Ingham2f6267f2011-01-22 01:27:23 +0000150 if (log && log->GetVerbose())
151 ReportRegisterState ("Restoring thread state after function call. Restored register state:");
Jim Ingham78108e62011-01-26 19:13:09 +0000152
Jim Ingham2f6267f2011-01-22 01:27:23 +0000153 }
154 else
155 {
156 if (log)
157 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 +0000158 }
Chris Lattner24943d22010-06-08 16:52:24 +0000159}
160
161void
Jim Ingham6c9662e2011-01-18 01:58:06 +0000162ThreadPlanCallFunction::WillPop ()
163{
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000164 DoTakedown();
Jim Ingham6c9662e2011-01-18 01:58:06 +0000165}
166
167void
Chris Lattner24943d22010-06-08 16:52:24 +0000168ThreadPlanCallFunction::GetDescription (Stream *s, lldb::DescriptionLevel level)
169{
170 if (level == lldb::eDescriptionLevelBrief)
171 {
172 s->Printf("Function call thread plan");
173 }
174 else
175 {
176 if (m_args)
Greg Claytoneea26402010-09-14 23:36:40 +0000177 s->Printf("Thread plan to call 0x%llx with parsed arguments", m_function_addr.GetLoadAddress(&m_process.GetTarget()), m_arg_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000178 else
Greg Claytoneea26402010-09-14 23:36:40 +0000179 s->Printf("Thread plan to call 0x%llx void * argument at: 0x%llx", m_function_addr.GetLoadAddress(&m_process.GetTarget()), m_arg_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000180 }
181}
182
183bool
184ThreadPlanCallFunction::ValidatePlan (Stream *error)
185{
186 if (!m_valid)
187 return false;
188
189 return true;
190}
191
192bool
193ThreadPlanCallFunction::PlanExplainsStop ()
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000194{
Jim Ingham988ddbc2010-10-26 00:27:45 +0000195 // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
196 // we answer yes.
197 if(m_subplan_sp.get() != NULL && m_subplan_sp->PlanExplainsStop())
198 return true;
Sean Callananba8547d2010-10-19 22:24:06 +0000199
Sean Callanan94fb5432010-11-03 19:36:28 +0000200 // Check if the breakpoint is one of ours.
201
202 if (BreakpointsExplainStop())
203 return true;
204
Jim Ingham988ddbc2010-10-26 00:27:45 +0000205 // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
206 if (!OkayToDiscard())
207 return false;
208
209 // Otherwise, check the case where we stopped for an internal breakpoint, in that case, continue on.
210 // If it is not an internal breakpoint, consult OkayToDiscard.
211 lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000212
Jim Ingham988ddbc2010-10-26 00:27:45 +0000213 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
214 {
215 uint64_t break_site_id = stop_info_sp->GetValue();
216 lldb::BreakpointSiteSP bp_site_sp = m_thread.GetProcess().GetBreakpointSiteList().FindByID(break_site_id);
217 if (bp_site_sp)
218 {
219 uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
220 bool is_internal = true;
221 for (uint32_t i = 0; i < num_owners; i++)
222 {
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000223 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000224
225 if (!bp.IsInternal())
Jim Ingham988ddbc2010-10-26 00:27:45 +0000226 {
227 is_internal = false;
228 break;
229 }
230 }
231 if (is_internal)
232 return false;
233 }
234
235 return OkayToDiscard();
236 }
237 else
238 {
239 // If the subplan is running, any crashes are attributable to us.
Jim Ingham78108e62011-01-26 19:13:09 +0000240 // If we want to discard the plan, then we say we explain the stop
241 // but if we are going to be discarded, let whoever is above us
242 // explain the stop.
243 return ((m_subplan_sp.get() != NULL) && !OkayToDiscard());
Jim Ingham988ddbc2010-10-26 00:27:45 +0000244 }
Chris Lattner24943d22010-06-08 16:52:24 +0000245}
246
247bool
248ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
249{
250 if (PlanExplainsStop())
251 {
Jim Ingham2f6267f2011-01-22 01:27:23 +0000252 ReportRegisterState ("Function completed. Register state was:");
Sean Callananf5857a02010-07-31 01:32:05 +0000253
Sean Callanan14a97ff2010-11-04 01:51:38 +0000254 DoTakedown();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000255
Chris Lattner24943d22010-06-08 16:52:24 +0000256 return true;
257 }
258 else
259 {
260 return false;
261 }
262}
263
264bool
265ThreadPlanCallFunction::StopOthers ()
266{
267 return m_stop_other_threads;
268}
269
270void
271ThreadPlanCallFunction::SetStopOthers (bool new_value)
272{
273 if (m_subplan_sp)
274 {
275 ThreadPlanRunToAddress *address_plan = static_cast<ThreadPlanRunToAddress *>(m_subplan_sp.get());
276 address_plan->SetStopOthers(new_value);
277 }
278 m_stop_other_threads = new_value;
279}
280
281StateType
Jim Ingham745ac7a2010-11-11 19:26:09 +0000282ThreadPlanCallFunction::GetPlanRunState ()
Chris Lattner24943d22010-06-08 16:52:24 +0000283{
284 return eStateRunning;
285}
286
287void
288ThreadPlanCallFunction::DidPush ()
289{
Sean Callananc2c6f772010-10-26 00:31:56 +0000290//#define SINGLE_STEP_EXPRESSIONS
291
292#ifndef SINGLE_STEP_EXPRESSIONS
Chris Lattner24943d22010-06-08 16:52:24 +0000293 m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
294
295 m_thread.QueueThreadPlan(m_subplan_sp, false);
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000296 m_subplan_sp->SetPrivate (true);
Sean Callananc2c6f772010-10-26 00:31:56 +0000297#endif
Chris Lattner24943d22010-06-08 16:52:24 +0000298}
299
300bool
301ThreadPlanCallFunction::WillStop ()
302{
303 return true;
304}
305
306bool
307ThreadPlanCallFunction::MischiefManaged ()
308{
309 if (IsPlanComplete())
310 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000311 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000312
313 if (log)
314 log->Printf("Completed call function plan.");
315
316 ThreadPlan::MischiefManaged ();
317 return true;
318 }
319 else
320 {
321 return false;
322 }
323}
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000324
325void
326ThreadPlanCallFunction::SetBreakpoints ()
327{
Sean Callanan29756d42010-11-03 22:19:38 +0000328 m_cxx_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeC_plus_plus);
329 m_objc_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeObjC);
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000330
Sean Callanan29756d42010-11-03 22:19:38 +0000331 if (m_cxx_language_runtime)
332 m_cxx_language_runtime->SetExceptionBreakpoints();
333 if (m_objc_language_runtime)
334 m_objc_language_runtime->SetExceptionBreakpoints();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000335}
336
337void
338ThreadPlanCallFunction::ClearBreakpoints ()
339{
Sean Callanan29756d42010-11-03 22:19:38 +0000340 if (m_cxx_language_runtime)
341 m_cxx_language_runtime->ClearExceptionBreakpoints();
342 if (m_objc_language_runtime)
343 m_objc_language_runtime->ClearExceptionBreakpoints();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000344}
Sean Callanan94fb5432010-11-03 19:36:28 +0000345
346bool
347ThreadPlanCallFunction::BreakpointsExplainStop()
348{
Sean Callanan94fb5432010-11-03 19:36:28 +0000349 lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
350
Sean Callanan29756d42010-11-03 22:19:38 +0000351 if (m_cxx_language_runtime &&
352 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
353 return true;
Sean Callanan94fb5432010-11-03 19:36:28 +0000354
Sean Callanan29756d42010-11-03 22:19:38 +0000355 if (m_objc_language_runtime &&
356 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
357 return true;
Sean Callanan94fb5432010-11-03 19:36:28 +0000358
359 return false;
360}