blob: 44f44ed944a0ced9aaf1edd368084bf1257b0829 [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:");
152 }
153 else
154 {
155 if (log)
156 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 +0000157 }
Chris Lattner24943d22010-06-08 16:52:24 +0000158}
159
160void
Jim Ingham6c9662e2011-01-18 01:58:06 +0000161ThreadPlanCallFunction::WillPop ()
162{
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000163 DoTakedown();
Jim Ingham6c9662e2011-01-18 01:58:06 +0000164}
165
166void
Chris Lattner24943d22010-06-08 16:52:24 +0000167ThreadPlanCallFunction::GetDescription (Stream *s, lldb::DescriptionLevel level)
168{
169 if (level == lldb::eDescriptionLevelBrief)
170 {
171 s->Printf("Function call thread plan");
172 }
173 else
174 {
175 if (m_args)
Greg Claytoneea26402010-09-14 23:36:40 +0000176 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 +0000177 else
Greg Claytoneea26402010-09-14 23:36:40 +0000178 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 +0000179 }
180}
181
182bool
183ThreadPlanCallFunction::ValidatePlan (Stream *error)
184{
185 if (!m_valid)
186 return false;
187
188 return true;
189}
190
191bool
192ThreadPlanCallFunction::PlanExplainsStop ()
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000193{
Jim Ingham988ddbc2010-10-26 00:27:45 +0000194 // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
195 // we answer yes.
196 if(m_subplan_sp.get() != NULL && m_subplan_sp->PlanExplainsStop())
197 return true;
Sean Callananba8547d2010-10-19 22:24:06 +0000198
Sean Callanan94fb5432010-11-03 19:36:28 +0000199 // Check if the breakpoint is one of ours.
200
201 if (BreakpointsExplainStop())
202 return true;
203
Jim Ingham988ddbc2010-10-26 00:27:45 +0000204 // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
205 if (!OkayToDiscard())
206 return false;
207
208 // Otherwise, check the case where we stopped for an internal breakpoint, in that case, continue on.
209 // If it is not an internal breakpoint, consult OkayToDiscard.
210 lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000211
Jim Ingham988ddbc2010-10-26 00:27:45 +0000212 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
213 {
214 uint64_t break_site_id = stop_info_sp->GetValue();
215 lldb::BreakpointSiteSP bp_site_sp = m_thread.GetProcess().GetBreakpointSiteList().FindByID(break_site_id);
216 if (bp_site_sp)
217 {
218 uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
219 bool is_internal = true;
220 for (uint32_t i = 0; i < num_owners; i++)
221 {
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000222 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000223
224 if (!bp.IsInternal())
Jim Ingham988ddbc2010-10-26 00:27:45 +0000225 {
226 is_internal = false;
227 break;
228 }
229 }
230 if (is_internal)
231 return false;
232 }
233
234 return OkayToDiscard();
235 }
236 else
237 {
238 // If the subplan is running, any crashes are attributable to us.
239 return (m_subplan_sp.get() != NULL);
240 }
Chris Lattner24943d22010-06-08 16:52:24 +0000241}
242
243bool
244ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
245{
246 if (PlanExplainsStop())
247 {
Jim Ingham2f6267f2011-01-22 01:27:23 +0000248 ReportRegisterState ("Function completed. Register state was:");
Sean Callananf5857a02010-07-31 01:32:05 +0000249
Sean Callanan14a97ff2010-11-04 01:51:38 +0000250 DoTakedown();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000251
Chris Lattner24943d22010-06-08 16:52:24 +0000252 return true;
253 }
254 else
255 {
256 return false;
257 }
258}
259
260bool
261ThreadPlanCallFunction::StopOthers ()
262{
263 return m_stop_other_threads;
264}
265
266void
267ThreadPlanCallFunction::SetStopOthers (bool new_value)
268{
269 if (m_subplan_sp)
270 {
271 ThreadPlanRunToAddress *address_plan = static_cast<ThreadPlanRunToAddress *>(m_subplan_sp.get());
272 address_plan->SetStopOthers(new_value);
273 }
274 m_stop_other_threads = new_value;
275}
276
277StateType
Jim Ingham745ac7a2010-11-11 19:26:09 +0000278ThreadPlanCallFunction::GetPlanRunState ()
Chris Lattner24943d22010-06-08 16:52:24 +0000279{
280 return eStateRunning;
281}
282
283void
284ThreadPlanCallFunction::DidPush ()
285{
Sean Callananc2c6f772010-10-26 00:31:56 +0000286//#define SINGLE_STEP_EXPRESSIONS
287
288#ifndef SINGLE_STEP_EXPRESSIONS
Chris Lattner24943d22010-06-08 16:52:24 +0000289 m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
290
291 m_thread.QueueThreadPlan(m_subplan_sp, false);
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000292 m_subplan_sp->SetPrivate (true);
Sean Callananc2c6f772010-10-26 00:31:56 +0000293#endif
Chris Lattner24943d22010-06-08 16:52:24 +0000294}
295
296bool
297ThreadPlanCallFunction::WillStop ()
298{
299 return true;
300}
301
302bool
303ThreadPlanCallFunction::MischiefManaged ()
304{
305 if (IsPlanComplete())
306 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000307 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000308
309 if (log)
310 log->Printf("Completed call function plan.");
311
312 ThreadPlan::MischiefManaged ();
313 return true;
314 }
315 else
316 {
317 return false;
318 }
319}
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000320
321void
322ThreadPlanCallFunction::SetBreakpoints ()
323{
Sean Callanan29756d42010-11-03 22:19:38 +0000324 m_cxx_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeC_plus_plus);
325 m_objc_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeObjC);
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000326
Sean Callanan29756d42010-11-03 22:19:38 +0000327 if (m_cxx_language_runtime)
328 m_cxx_language_runtime->SetExceptionBreakpoints();
329 if (m_objc_language_runtime)
330 m_objc_language_runtime->SetExceptionBreakpoints();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000331}
332
333void
334ThreadPlanCallFunction::ClearBreakpoints ()
335{
Sean Callanan29756d42010-11-03 22:19:38 +0000336 if (m_cxx_language_runtime)
337 m_cxx_language_runtime->ClearExceptionBreakpoints();
338 if (m_objc_language_runtime)
339 m_objc_language_runtime->ClearExceptionBreakpoints();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000340}
Sean Callanan94fb5432010-11-03 19:36:28 +0000341
342bool
343ThreadPlanCallFunction::BreakpointsExplainStop()
344{
Sean Callanan94fb5432010-11-03 19:36:28 +0000345 lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
346
Sean Callanan29756d42010-11-03 22:19:38 +0000347 if (m_cxx_language_runtime &&
348 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
349 return true;
Sean Callanan94fb5432010-11-03 19:36:28 +0000350
Sean Callanan29756d42010-11-03 22:19:38 +0000351 if (m_objc_language_runtime &&
352 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
353 return true;
Sean Callanan94fb5432010-11-03 19:36:28 +0000354
355 return false;
356}