blob: a662ea0589dcdb1b9e1deca1440fc8996dc646f2 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:241//===-- 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 Callanan6db73ca2010-11-03 01:37:5215#include "llvm/Support/MachO.h"
Chris Lattner30fdc8d2010-06-08 16:52:2416// Project includes
17#include "lldb/lldb-private-log.h"
Jim Ingham40d871f2010-10-26 00:27:4518#include "lldb/Breakpoint/Breakpoint.h"
19#include "lldb/Breakpoint/BreakpointLocation.h"
Chris Lattner30fdc8d2010-06-08 16:52:2420#include "lldb/Core/Address.h"
21#include "lldb/Core/Log.h"
22#include "lldb/Core/Stream.h"
Sean Callananf2115102010-11-03 22:19:3823#include "lldb/Target/LanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:2424#include "lldb/Target/Process.h"
25#include "lldb/Target/RegisterContext.h"
Jim Ingham40d871f2010-10-26 00:27:4526#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:2427#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 Callananfc55f5d2010-09-21 00:44:1242 bool discard_on_error,
Sean Callanan17827832010-12-13 22:46:1543 lldb::addr_t *this_arg,
44 lldb::addr_t *cmd_arg) :
Jim Inghamb01e742a2010-06-19 04:45:3245 ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
Benjamin Kramer1ee0d4f2010-07-16 12:32:3346 m_valid (false),
47 m_stop_other_threads (stop_other_threads),
Chris Lattner30fdc8d2010-06-08 16:52:2448 m_arg_addr (arg),
49 m_args (NULL),
Benjamin Kramer1ee0d4f2010-07-16 12:32:3350 m_process (thread.GetProcess()),
51 m_thread (thread)
Chris Lattner30fdc8d2010-06-08 16:52:2452{
Chris Lattner30fdc8d2010-06-08 16:52:2453 SetOkayToDiscard (discard_on_error);
54
55 Process& process = thread.GetProcess();
56 Target& target = process.GetTarget();
57 const ABI *abi = process.GetABI();
Sean Callanan6db73ca2010-11-03 01:37:5258
Chris Lattner30fdc8d2010-06-08 16:52:2459 if (!abi)
60 return;
Sean Callanan6db73ca2010-11-03 01:37:5261
62 SetBreakpoints();
63
Chris Lattner30fdc8d2010-06-08 16:52:2464 lldb::addr_t spBelowRedZone = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
65
66 SymbolContextList contexts;
67 SymbolContext context;
68 ModuleSP executableModuleSP (target.GetExecutableModule());
69
70 if (!executableModuleSP ||
71 !executableModuleSP->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
72 return;
73
74 contexts.GetContextAtIndex(0, context);
75
76 m_start_addr = context.symbol->GetValue();
Greg Claytonf5e56de2010-09-14 23:36:4077 lldb::addr_t StartLoadAddr = m_start_addr.GetLoadAddress(&target);
Chris Lattner30fdc8d2010-06-08 16:52:2478
79 if (!thread.SaveFrameZeroState(m_register_backup))
80 return;
81
82 m_function_addr = function;
Greg Claytonf5e56de2010-09-14 23:36:4083 lldb::addr_t FunctionLoadAddr = m_function_addr.GetLoadAddress(&target);
Chris Lattner30fdc8d2010-06-08 16:52:2484
85 if (!abi->PrepareTrivialCall(thread,
86 spBelowRedZone,
87 FunctionLoadAddr,
88 StartLoadAddr,
Sean Callananfc55f5d2010-09-21 00:44:1289 m_arg_addr,
Sean Callanan17827832010-12-13 22:46:1590 this_arg,
91 cmd_arg))
Chris Lattner30fdc8d2010-06-08 16:52:2492 return;
93
Sean Callananece96492010-11-08 03:49:5094 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
95
96 if (log)
97 {
Greg Clayton5ccbd292011-01-06 22:15:0698 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
Sean Callananece96492010-11-08 03:49:5099
100 log->PutCString("Function call was set up. Register state was:");
101
102 for (uint32_t register_index = 0, num_registers = reg_ctx->GetRegisterCount();
103 register_index < num_registers;
104 ++register_index)
105 {
106 const char *register_name = reg_ctx->GetRegisterName(register_index);
107 uint64_t register_value = reg_ctx->ReadRegisterAsUnsigned(register_index, LLDB_INVALID_ADDRESS);
108
109 log->Printf(" %s = 0x%llx", register_name, register_value);
110 }
111 }
112
Chris Lattner30fdc8d2010-06-08 16:52:24113 m_valid = true;
114}
115
Chris Lattner30fdc8d2010-06-08 16:52:24116ThreadPlanCallFunction::~ThreadPlanCallFunction ()
117{
Sean Callanan10af7c42010-11-04 01:51:38118 if (m_valid && !IsPlanComplete())
119 DoTakedown();
120}
121
122void
123ThreadPlanCallFunction::DoTakedown ()
124{
125 m_thread.RestoreSaveFrameZero(m_register_backup);
126 m_thread.ClearStackFrames();
127 SetPlanComplete();
128 ClearBreakpoints();
Chris Lattner30fdc8d2010-06-08 16:52:24129}
130
131void
132ThreadPlanCallFunction::GetDescription (Stream *s, lldb::DescriptionLevel level)
133{
134 if (level == lldb::eDescriptionLevelBrief)
135 {
136 s->Printf("Function call thread plan");
137 }
138 else
139 {
140 if (m_args)
Greg Claytonf5e56de2010-09-14 23:36:40141 s->Printf("Thread plan to call 0x%llx with parsed arguments", m_function_addr.GetLoadAddress(&m_process.GetTarget()), m_arg_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24142 else
Greg Claytonf5e56de2010-09-14 23:36:40143 s->Printf("Thread plan to call 0x%llx void * argument at: 0x%llx", m_function_addr.GetLoadAddress(&m_process.GetTarget()), m_arg_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24144 }
145}
146
147bool
148ThreadPlanCallFunction::ValidatePlan (Stream *error)
149{
150 if (!m_valid)
151 return false;
152
153 return true;
154}
155
156bool
157ThreadPlanCallFunction::PlanExplainsStop ()
Sean Callanan6db73ca2010-11-03 01:37:52158{
Jim Ingham40d871f2010-10-26 00:27:45159 // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
160 // we answer yes.
161 if(m_subplan_sp.get() != NULL && m_subplan_sp->PlanExplainsStop())
162 return true;
Sean Callanan3e6fedc2010-10-19 22:24:06163
Sean Callananc98aca62010-11-03 19:36:28164 // Check if the breakpoint is one of ours.
165
166 if (BreakpointsExplainStop())
167 return true;
168
Jim Ingham40d871f2010-10-26 00:27:45169 // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
170 if (!OkayToDiscard())
171 return false;
172
173 // Otherwise, check the case where we stopped for an internal breakpoint, in that case, continue on.
174 // If it is not an internal breakpoint, consult OkayToDiscard.
175 lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
Sean Callanan6db73ca2010-11-03 01:37:52176
Jim Ingham40d871f2010-10-26 00:27:45177 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
178 {
179 uint64_t break_site_id = stop_info_sp->GetValue();
180 lldb::BreakpointSiteSP bp_site_sp = m_thread.GetProcess().GetBreakpointSiteList().FindByID(break_site_id);
181 if (bp_site_sp)
182 {
183 uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
184 bool is_internal = true;
185 for (uint32_t i = 0; i < num_owners; i++)
186 {
Sean Callanan6db73ca2010-11-03 01:37:52187 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
Sean Callanan6db73ca2010-11-03 01:37:52188
189 if (!bp.IsInternal())
Jim Ingham40d871f2010-10-26 00:27:45190 {
191 is_internal = false;
192 break;
193 }
194 }
195 if (is_internal)
196 return false;
197 }
198
199 return OkayToDiscard();
200 }
201 else
202 {
203 // If the subplan is running, any crashes are attributable to us.
204 return (m_subplan_sp.get() != NULL);
205 }
Chris Lattner30fdc8d2010-06-08 16:52:24206}
207
208bool
209ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
210{
211 if (PlanExplainsStop())
212 {
Greg Clayton2d4edfb2010-11-06 01:53:30213 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Sean Callanan5300d372010-07-31 01:32:05214
215 if (log)
216 {
Greg Clayton5ccbd292011-01-06 22:15:06217 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
Sean Callanan5300d372010-07-31 01:32:05218
219 log->PutCString("Function completed. Register state was:");
220
221 for (uint32_t register_index = 0, num_registers = reg_ctx->GetRegisterCount();
222 register_index < num_registers;
223 ++register_index)
224 {
225 const char *register_name = reg_ctx->GetRegisterName(register_index);
226 uint64_t register_value = reg_ctx->ReadRegisterAsUnsigned(register_index, LLDB_INVALID_ADDRESS);
227
228 log->Printf(" %s = 0x%llx", register_name, register_value);
229 }
230 }
231
Sean Callanan10af7c42010-11-04 01:51:38232 DoTakedown();
Sean Callanan6db73ca2010-11-03 01:37:52233
Chris Lattner30fdc8d2010-06-08 16:52:24234 return true;
235 }
236 else
237 {
238 return false;
239 }
240}
241
242bool
243ThreadPlanCallFunction::StopOthers ()
244{
245 return m_stop_other_threads;
246}
247
248void
249ThreadPlanCallFunction::SetStopOthers (bool new_value)
250{
251 if (m_subplan_sp)
252 {
253 ThreadPlanRunToAddress *address_plan = static_cast<ThreadPlanRunToAddress *>(m_subplan_sp.get());
254 address_plan->SetStopOthers(new_value);
255 }
256 m_stop_other_threads = new_value;
257}
258
259StateType
Jim Ingham06e827c2010-11-11 19:26:09260ThreadPlanCallFunction::GetPlanRunState ()
Chris Lattner30fdc8d2010-06-08 16:52:24261{
262 return eStateRunning;
263}
264
265void
266ThreadPlanCallFunction::DidPush ()
267{
Sean Callananbe3a1b12010-10-26 00:31:56268//#define SINGLE_STEP_EXPRESSIONS
269
270#ifndef SINGLE_STEP_EXPRESSIONS
Chris Lattner30fdc8d2010-06-08 16:52:24271 m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
272
273 m_thread.QueueThreadPlan(m_subplan_sp, false);
Sean Callananbe3a1b12010-10-26 00:31:56274#endif
Chris Lattner30fdc8d2010-06-08 16:52:24275}
276
277bool
278ThreadPlanCallFunction::WillStop ()
279{
280 return true;
281}
282
283bool
284ThreadPlanCallFunction::MischiefManaged ()
285{
286 if (IsPlanComplete())
287 {
Greg Clayton2d4edfb2010-11-06 01:53:30288 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24289
290 if (log)
291 log->Printf("Completed call function plan.");
292
293 ThreadPlan::MischiefManaged ();
294 return true;
295 }
296 else
297 {
298 return false;
299 }
300}
Sean Callanan6db73ca2010-11-03 01:37:52301
302void
303ThreadPlanCallFunction::SetBreakpoints ()
304{
Sean Callananf2115102010-11-03 22:19:38305 m_cxx_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeC_plus_plus);
306 m_objc_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeObjC);
Sean Callanan6db73ca2010-11-03 01:37:52307
Sean Callananf2115102010-11-03 22:19:38308 if (m_cxx_language_runtime)
309 m_cxx_language_runtime->SetExceptionBreakpoints();
310 if (m_objc_language_runtime)
311 m_objc_language_runtime->SetExceptionBreakpoints();
Sean Callanan6db73ca2010-11-03 01:37:52312}
313
314void
315ThreadPlanCallFunction::ClearBreakpoints ()
316{
Sean Callananf2115102010-11-03 22:19:38317 if (m_cxx_language_runtime)
318 m_cxx_language_runtime->ClearExceptionBreakpoints();
319 if (m_objc_language_runtime)
320 m_objc_language_runtime->ClearExceptionBreakpoints();
Sean Callanan6db73ca2010-11-03 01:37:52321}
Sean Callananc98aca62010-11-03 19:36:28322
323bool
324ThreadPlanCallFunction::BreakpointsExplainStop()
325{
Sean Callananc98aca62010-11-03 19:36:28326 lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
327
Sean Callananf2115102010-11-03 22:19:38328 if (m_cxx_language_runtime &&
329 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
330 return true;
Sean Callananc98aca62010-11-03 19:36:28331
Sean Callananf2115102010-11-03 22:19:38332 if (m_objc_language_runtime &&
333 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
334 return true;
Sean Callananc98aca62010-11-03 19:36:28335
336 return false;
337}