blob: 8c9b3afa0d3f12d1be3edeaed0ea9b2fedf0279c [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()),
51 m_thread (thread)
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();
57 const ABI *abi = process.GetABI();
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
Chris Lattner24943d22010-06-08 16:52:24 +000066 lldb::addr_t spBelowRedZone = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
67
68 SymbolContextList contexts;
69 SymbolContext context;
70 ModuleSP executableModuleSP (target.GetExecutableModule());
71
72 if (!executableModuleSP ||
73 !executableModuleSP->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
74 return;
75
76 contexts.GetContextAtIndex(0, context);
77
78 m_start_addr = context.symbol->GetValue();
Greg Claytoneea26402010-09-14 23:36:40 +000079 lldb::addr_t StartLoadAddr = m_start_addr.GetLoadAddress(&target);
Chris Lattner24943d22010-06-08 16:52:24 +000080
Jim Ingham15dcb7c2011-01-20 02:03:18 +000081 // Checkpoint the thread state so we can restore it later.
82 if (!thread.CheckpointThreadState (m_stored_thread_state))
83 {
84 if (log)
85 log->Printf ("Setting up ThreadPlanCallFunction, failed to checkpoint thread state.");
Chris Lattner24943d22010-06-08 16:52:24 +000086 return;
Jim Ingham15dcb7c2011-01-20 02:03:18 +000087 }
88 // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
89 thread.SetStopInfoToNothing();
90
Chris Lattner24943d22010-06-08 16:52:24 +000091 m_function_addr = function;
Greg Claytoneea26402010-09-14 23:36:40 +000092 lldb::addr_t FunctionLoadAddr = m_function_addr.GetLoadAddress(&target);
Chris Lattner24943d22010-06-08 16:52:24 +000093
94 if (!abi->PrepareTrivialCall(thread,
95 spBelowRedZone,
96 FunctionLoadAddr,
97 StartLoadAddr,
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000098 m_arg_addr,
Sean Callanan3aa7da52010-12-13 22:46:15 +000099 this_arg,
100 cmd_arg))
Chris Lattner24943d22010-06-08 16:52:24 +0000101 return;
102
Sean Callanan6dff8272010-11-08 03:49:50 +0000103 if (log)
104 {
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000105 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
Sean Callanan6dff8272010-11-08 03:49:50 +0000106
107 log->PutCString("Function call was set up. Register state was:");
108
109 for (uint32_t register_index = 0, num_registers = reg_ctx->GetRegisterCount();
110 register_index < num_registers;
111 ++register_index)
112 {
113 const char *register_name = reg_ctx->GetRegisterName(register_index);
114 uint64_t register_value = reg_ctx->ReadRegisterAsUnsigned(register_index, LLDB_INVALID_ADDRESS);
115
116 log->Printf(" %s = 0x%llx", register_name, register_value);
117 }
118 }
119
Chris Lattner24943d22010-06-08 16:52:24 +0000120 m_valid = true;
121}
122
Chris Lattner24943d22010-06-08 16:52:24 +0000123ThreadPlanCallFunction::~ThreadPlanCallFunction ()
124{
Sean Callanan14a97ff2010-11-04 01:51:38 +0000125 DoTakedown();
126}
127
128void
129ThreadPlanCallFunction::DoTakedown ()
130{
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000131 if (m_valid && !IsPlanComplete())
132 {
133 m_thread.RestoreThreadStateFromCheckpoint(m_stored_thread_state);
134 SetPlanComplete();
135 ClearBreakpoints();
136 }
Chris Lattner24943d22010-06-08 16:52:24 +0000137}
138
139void
Jim Ingham6c9662e2011-01-18 01:58:06 +0000140ThreadPlanCallFunction::WillPop ()
141{
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000142 DoTakedown();
Jim Ingham6c9662e2011-01-18 01:58:06 +0000143}
144
145void
Chris Lattner24943d22010-06-08 16:52:24 +0000146ThreadPlanCallFunction::GetDescription (Stream *s, lldb::DescriptionLevel level)
147{
148 if (level == lldb::eDescriptionLevelBrief)
149 {
150 s->Printf("Function call thread plan");
151 }
152 else
153 {
154 if (m_args)
Greg Claytoneea26402010-09-14 23:36:40 +0000155 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 +0000156 else
Greg Claytoneea26402010-09-14 23:36:40 +0000157 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 +0000158 }
159}
160
161bool
162ThreadPlanCallFunction::ValidatePlan (Stream *error)
163{
164 if (!m_valid)
165 return false;
166
167 return true;
168}
169
170bool
171ThreadPlanCallFunction::PlanExplainsStop ()
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000172{
Jim Ingham988ddbc2010-10-26 00:27:45 +0000173 // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
174 // we answer yes.
175 if(m_subplan_sp.get() != NULL && m_subplan_sp->PlanExplainsStop())
176 return true;
Sean Callananba8547d2010-10-19 22:24:06 +0000177
Sean Callanan94fb5432010-11-03 19:36:28 +0000178 // Check if the breakpoint is one of ours.
179
180 if (BreakpointsExplainStop())
181 return true;
182
Jim Ingham988ddbc2010-10-26 00:27:45 +0000183 // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
184 if (!OkayToDiscard())
185 return false;
186
187 // Otherwise, check the case where we stopped for an internal breakpoint, in that case, continue on.
188 // If it is not an internal breakpoint, consult OkayToDiscard.
189 lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000190
Jim Ingham988ddbc2010-10-26 00:27:45 +0000191 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
192 {
193 uint64_t break_site_id = stop_info_sp->GetValue();
194 lldb::BreakpointSiteSP bp_site_sp = m_thread.GetProcess().GetBreakpointSiteList().FindByID(break_site_id);
195 if (bp_site_sp)
196 {
197 uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
198 bool is_internal = true;
199 for (uint32_t i = 0; i < num_owners; i++)
200 {
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000201 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000202
203 if (!bp.IsInternal())
Jim Ingham988ddbc2010-10-26 00:27:45 +0000204 {
205 is_internal = false;
206 break;
207 }
208 }
209 if (is_internal)
210 return false;
211 }
212
213 return OkayToDiscard();
214 }
215 else
216 {
217 // If the subplan is running, any crashes are attributable to us.
218 return (m_subplan_sp.get() != NULL);
219 }
Chris Lattner24943d22010-06-08 16:52:24 +0000220}
221
222bool
223ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
224{
225 if (PlanExplainsStop())
226 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000227 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Sean Callananf5857a02010-07-31 01:32:05 +0000228
229 if (log)
230 {
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000231 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
Sean Callananf5857a02010-07-31 01:32:05 +0000232
233 log->PutCString("Function completed. Register state was:");
234
235 for (uint32_t register_index = 0, num_registers = reg_ctx->GetRegisterCount();
236 register_index < num_registers;
237 ++register_index)
238 {
239 const char *register_name = reg_ctx->GetRegisterName(register_index);
240 uint64_t register_value = reg_ctx->ReadRegisterAsUnsigned(register_index, LLDB_INVALID_ADDRESS);
241
242 log->Printf(" %s = 0x%llx", register_name, register_value);
243 }
244 }
245
Sean Callanan14a97ff2010-11-04 01:51:38 +0000246 DoTakedown();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000247
Chris Lattner24943d22010-06-08 16:52:24 +0000248 return true;
249 }
250 else
251 {
252 return false;
253 }
254}
255
256bool
257ThreadPlanCallFunction::StopOthers ()
258{
259 return m_stop_other_threads;
260}
261
262void
263ThreadPlanCallFunction::SetStopOthers (bool new_value)
264{
265 if (m_subplan_sp)
266 {
267 ThreadPlanRunToAddress *address_plan = static_cast<ThreadPlanRunToAddress *>(m_subplan_sp.get());
268 address_plan->SetStopOthers(new_value);
269 }
270 m_stop_other_threads = new_value;
271}
272
273StateType
Jim Ingham745ac7a2010-11-11 19:26:09 +0000274ThreadPlanCallFunction::GetPlanRunState ()
Chris Lattner24943d22010-06-08 16:52:24 +0000275{
276 return eStateRunning;
277}
278
279void
280ThreadPlanCallFunction::DidPush ()
281{
Sean Callananc2c6f772010-10-26 00:31:56 +0000282//#define SINGLE_STEP_EXPRESSIONS
283
284#ifndef SINGLE_STEP_EXPRESSIONS
Chris Lattner24943d22010-06-08 16:52:24 +0000285 m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
286
287 m_thread.QueueThreadPlan(m_subplan_sp, false);
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000288 m_subplan_sp->SetPrivate (true);
Sean Callananc2c6f772010-10-26 00:31:56 +0000289#endif
Chris Lattner24943d22010-06-08 16:52:24 +0000290}
291
292bool
293ThreadPlanCallFunction::WillStop ()
294{
295 return true;
296}
297
298bool
299ThreadPlanCallFunction::MischiefManaged ()
300{
301 if (IsPlanComplete())
302 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000303 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000304
305 if (log)
306 log->Printf("Completed call function plan.");
307
308 ThreadPlan::MischiefManaged ();
309 return true;
310 }
311 else
312 {
313 return false;
314 }
315}
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000316
317void
318ThreadPlanCallFunction::SetBreakpoints ()
319{
Sean Callanan29756d42010-11-03 22:19:38 +0000320 m_cxx_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeC_plus_plus);
321 m_objc_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeObjC);
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000322
Sean Callanan29756d42010-11-03 22:19:38 +0000323 if (m_cxx_language_runtime)
324 m_cxx_language_runtime->SetExceptionBreakpoints();
325 if (m_objc_language_runtime)
326 m_objc_language_runtime->SetExceptionBreakpoints();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000327}
328
329void
330ThreadPlanCallFunction::ClearBreakpoints ()
331{
Sean Callanan29756d42010-11-03 22:19:38 +0000332 if (m_cxx_language_runtime)
333 m_cxx_language_runtime->ClearExceptionBreakpoints();
334 if (m_objc_language_runtime)
335 m_objc_language_runtime->ClearExceptionBreakpoints();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000336}
Sean Callanan94fb5432010-11-03 19:36:28 +0000337
338bool
339ThreadPlanCallFunction::BreakpointsExplainStop()
340{
Sean Callanan94fb5432010-11-03 19:36:28 +0000341 lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
342
Sean Callanan29756d42010-11-03 22:19:38 +0000343 if (m_cxx_language_runtime &&
344 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
345 return true;
Sean Callanan94fb5432010-11-03 19:36:28 +0000346
Sean Callanan29756d42010-11-03 22:19:38 +0000347 if (m_objc_language_runtime &&
348 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
349 return true;
Sean Callanan94fb5432010-11-03 19:36:28 +0000350
351 return false;
352}