blob: a5e1c1a0e794f993486e08acb7226be09c23d150 [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
Chris Lattner24943d22010-06-08 16:52:24 +000069 ModuleSP executableModuleSP (target.GetExecutableModule());
70
Jim Ingham28775942011-03-07 23:44:08 +000071 if (!executableModuleSP)
72 {
73 log->Printf ("Can't execute code without an executable module.");
Chris Lattner24943d22010-06-08 16:52:24 +000074 return;
Jim Ingham28775942011-03-07 23:44:08 +000075 }
76 else
77 {
78 ObjectFile *objectFile = executableModuleSP->GetObjectFile();
79 if (!objectFile)
80 {
81 log->Printf ("Could not find object file for module \"%s\".",
82 executableModuleSP->GetFileSpec().GetFilename().AsCString());
83 return;
84 }
85 m_start_addr = objectFile->GetEntryPointAddress();
86 if (!m_start_addr.IsValid())
87 {
88 log->Printf ("Could not find entry point address for executable module \"%s\".",
89 executableModuleSP->GetFileSpec().GetFilename().AsCString());
90 return;
91 }
92 }
Chris Lattner24943d22010-06-08 16:52:24 +000093
Greg Claytoneea26402010-09-14 23:36:40 +000094 lldb::addr_t StartLoadAddr = m_start_addr.GetLoadAddress(&target);
Jim Ingham28775942011-03-07 23:44:08 +000095
Jim Ingham15dcb7c2011-01-20 02:03:18 +000096 // Checkpoint the thread state so we can restore it later.
Jim Ingham2f6267f2011-01-22 01:27:23 +000097 if (log && log->GetVerbose())
98 ReportRegisterState ("About to checkpoint thread before function call. Original register state was:");
99
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000100 if (!thread.CheckpointThreadState (m_stored_thread_state))
101 {
102 if (log)
103 log->Printf ("Setting up ThreadPlanCallFunction, failed to checkpoint thread state.");
Chris Lattner24943d22010-06-08 16:52:24 +0000104 return;
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000105 }
106 // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
107 thread.SetStopInfoToNothing();
108
Chris Lattner24943d22010-06-08 16:52:24 +0000109 m_function_addr = function;
Greg Claytoneea26402010-09-14 23:36:40 +0000110 lldb::addr_t FunctionLoadAddr = m_function_addr.GetLoadAddress(&target);
Chris Lattner24943d22010-06-08 16:52:24 +0000111
112 if (!abi->PrepareTrivialCall(thread,
113 spBelowRedZone,
114 FunctionLoadAddr,
115 StartLoadAddr,
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000116 m_arg_addr,
Sean Callanan3aa7da52010-12-13 22:46:15 +0000117 this_arg,
118 cmd_arg))
Chris Lattner24943d22010-06-08 16:52:24 +0000119 return;
120
Jim Ingham2f6267f2011-01-22 01:27:23 +0000121 ReportRegisterState ("Function call was set up. Register state was:");
122
123 m_valid = true;
124}
125
126ThreadPlanCallFunction::~ThreadPlanCallFunction ()
127{
128}
129
130void
131ThreadPlanCallFunction::ReportRegisterState (const char *message)
132{
133 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Sean Callanan6dff8272010-11-08 03:49:50 +0000134 if (log)
135 {
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000136 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
Jim Ingham2f6267f2011-01-22 01:27:23 +0000137
138 log->PutCString(message);
139
Sean Callanan6dff8272010-11-08 03:49:50 +0000140 for (uint32_t register_index = 0, num_registers = reg_ctx->GetRegisterCount();
141 register_index < num_registers;
142 ++register_index)
143 {
144 const char *register_name = reg_ctx->GetRegisterName(register_index);
145 uint64_t register_value = reg_ctx->ReadRegisterAsUnsigned(register_index, LLDB_INVALID_ADDRESS);
146
147 log->Printf(" %s = 0x%llx", register_name, register_value);
148 }
149 }
Sean Callanan14a97ff2010-11-04 01:51:38 +0000150}
151
152void
153ThreadPlanCallFunction::DoTakedown ()
154{
Jim Ingham2f6267f2011-01-22 01:27:23 +0000155 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
156 if (!m_takedown_done)
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000157 {
Jim Ingham2f6267f2011-01-22 01:27:23 +0000158 if (log)
159 log->Printf ("DoTakedown called for thread 0x%4.4x, m_valid: %d complete: %d.\n", m_thread.GetID(), m_valid, IsPlanComplete());
160 m_takedown_done = true;
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000161 m_thread.RestoreThreadStateFromCheckpoint(m_stored_thread_state);
162 SetPlanComplete();
163 ClearBreakpoints();
Jim Ingham2f6267f2011-01-22 01:27:23 +0000164 if (log && log->GetVerbose())
165 ReportRegisterState ("Restoring thread state after function call. Restored register state:");
Jim Ingham78108e62011-01-26 19:13:09 +0000166
Jim Ingham2f6267f2011-01-22 01:27:23 +0000167 }
168 else
169 {
170 if (log)
171 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 +0000172 }
Chris Lattner24943d22010-06-08 16:52:24 +0000173}
174
175void
Jim Ingham6c9662e2011-01-18 01:58:06 +0000176ThreadPlanCallFunction::WillPop ()
177{
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000178 DoTakedown();
Jim Ingham6c9662e2011-01-18 01:58:06 +0000179}
180
181void
Chris Lattner24943d22010-06-08 16:52:24 +0000182ThreadPlanCallFunction::GetDescription (Stream *s, lldb::DescriptionLevel level)
183{
184 if (level == lldb::eDescriptionLevelBrief)
185 {
186 s->Printf("Function call thread plan");
187 }
188 else
189 {
190 if (m_args)
Greg Claytoneea26402010-09-14 23:36:40 +0000191 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 +0000192 else
Greg Claytoneea26402010-09-14 23:36:40 +0000193 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 +0000194 }
195}
196
197bool
198ThreadPlanCallFunction::ValidatePlan (Stream *error)
199{
200 if (!m_valid)
201 return false;
202
203 return true;
204}
205
206bool
207ThreadPlanCallFunction::PlanExplainsStop ()
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000208{
Jim Ingham988ddbc2010-10-26 00:27:45 +0000209 // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
210 // we answer yes.
211 if(m_subplan_sp.get() != NULL && m_subplan_sp->PlanExplainsStop())
212 return true;
Sean Callananba8547d2010-10-19 22:24:06 +0000213
Sean Callanan94fb5432010-11-03 19:36:28 +0000214 // Check if the breakpoint is one of ours.
215
216 if (BreakpointsExplainStop())
217 return true;
218
Jim Ingham988ddbc2010-10-26 00:27:45 +0000219 // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
220 if (!OkayToDiscard())
221 return false;
222
223 // Otherwise, check the case where we stopped for an internal breakpoint, in that case, continue on.
224 // If it is not an internal breakpoint, consult OkayToDiscard.
225 lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000226
Jim Ingham988ddbc2010-10-26 00:27:45 +0000227 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
228 {
229 uint64_t break_site_id = stop_info_sp->GetValue();
230 lldb::BreakpointSiteSP bp_site_sp = m_thread.GetProcess().GetBreakpointSiteList().FindByID(break_site_id);
231 if (bp_site_sp)
232 {
233 uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
234 bool is_internal = true;
235 for (uint32_t i = 0; i < num_owners; i++)
236 {
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000237 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000238
239 if (!bp.IsInternal())
Jim Ingham988ddbc2010-10-26 00:27:45 +0000240 {
241 is_internal = false;
242 break;
243 }
244 }
245 if (is_internal)
246 return false;
247 }
248
249 return OkayToDiscard();
250 }
251 else
252 {
253 // If the subplan is running, any crashes are attributable to us.
Jim Ingham78108e62011-01-26 19:13:09 +0000254 // If we want to discard the plan, then we say we explain the stop
255 // but if we are going to be discarded, let whoever is above us
256 // explain the stop.
257 return ((m_subplan_sp.get() != NULL) && !OkayToDiscard());
Jim Ingham988ddbc2010-10-26 00:27:45 +0000258 }
Chris Lattner24943d22010-06-08 16:52:24 +0000259}
260
261bool
262ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
263{
264 if (PlanExplainsStop())
265 {
Jim Ingham2f6267f2011-01-22 01:27:23 +0000266 ReportRegisterState ("Function completed. Register state was:");
Sean Callananf5857a02010-07-31 01:32:05 +0000267
Sean Callanan14a97ff2010-11-04 01:51:38 +0000268 DoTakedown();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000269
Chris Lattner24943d22010-06-08 16:52:24 +0000270 return true;
271 }
272 else
273 {
274 return false;
275 }
276}
277
278bool
279ThreadPlanCallFunction::StopOthers ()
280{
281 return m_stop_other_threads;
282}
283
284void
285ThreadPlanCallFunction::SetStopOthers (bool new_value)
286{
287 if (m_subplan_sp)
288 {
289 ThreadPlanRunToAddress *address_plan = static_cast<ThreadPlanRunToAddress *>(m_subplan_sp.get());
290 address_plan->SetStopOthers(new_value);
291 }
292 m_stop_other_threads = new_value;
293}
294
295StateType
Jim Ingham745ac7a2010-11-11 19:26:09 +0000296ThreadPlanCallFunction::GetPlanRunState ()
Chris Lattner24943d22010-06-08 16:52:24 +0000297{
298 return eStateRunning;
299}
300
301void
302ThreadPlanCallFunction::DidPush ()
303{
Sean Callananc2c6f772010-10-26 00:31:56 +0000304//#define SINGLE_STEP_EXPRESSIONS
305
306#ifndef SINGLE_STEP_EXPRESSIONS
Chris Lattner24943d22010-06-08 16:52:24 +0000307 m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
308
309 m_thread.QueueThreadPlan(m_subplan_sp, false);
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000310 m_subplan_sp->SetPrivate (true);
Sean Callananc2c6f772010-10-26 00:31:56 +0000311#endif
Chris Lattner24943d22010-06-08 16:52:24 +0000312}
313
314bool
315ThreadPlanCallFunction::WillStop ()
316{
317 return true;
318}
319
320bool
321ThreadPlanCallFunction::MischiefManaged ()
322{
323 if (IsPlanComplete())
324 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000325 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000326
327 if (log)
328 log->Printf("Completed call function plan.");
329
330 ThreadPlan::MischiefManaged ();
331 return true;
332 }
333 else
334 {
335 return false;
336 }
337}
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000338
339void
340ThreadPlanCallFunction::SetBreakpoints ()
341{
Sean Callanan29756d42010-11-03 22:19:38 +0000342 m_cxx_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeC_plus_plus);
343 m_objc_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeObjC);
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000344
Sean Callanan29756d42010-11-03 22:19:38 +0000345 if (m_cxx_language_runtime)
346 m_cxx_language_runtime->SetExceptionBreakpoints();
347 if (m_objc_language_runtime)
348 m_objc_language_runtime->SetExceptionBreakpoints();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000349}
350
351void
352ThreadPlanCallFunction::ClearBreakpoints ()
353{
Sean Callanan29756d42010-11-03 22:19:38 +0000354 if (m_cxx_language_runtime)
355 m_cxx_language_runtime->ClearExceptionBreakpoints();
356 if (m_objc_language_runtime)
357 m_objc_language_runtime->ClearExceptionBreakpoints();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000358}
Sean Callanan94fb5432010-11-03 19:36:28 +0000359
360bool
361ThreadPlanCallFunction::BreakpointsExplainStop()
362{
Sean Callanan94fb5432010-11-03 19:36:28 +0000363 lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
364
Sean Callanan29756d42010-11-03 22:19:38 +0000365 if (m_cxx_language_runtime &&
366 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
367 return true;
Sean Callanan94fb5432010-11-03 19:36:28 +0000368
Sean Callanan29756d42010-11-03 22:19:38 +0000369 if (m_objc_language_runtime &&
370 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
371 return true;
Sean Callanan94fb5432010-11-03 19:36:28 +0000372
373 return false;
374}