blob: 65349ddaeb75466916d2b7f7e8102310323da736 [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,
43 lldb::addr_t *this_arg) :
Jim Ingham5a47e8b2010-06-19 04:45:32 +000044 ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
Benjamin Kramer36a08102010-07-16 12:32:33 +000045 m_valid (false),
46 m_stop_other_threads (stop_other_threads),
Chris Lattner24943d22010-06-08 16:52:24 +000047 m_arg_addr (arg),
48 m_args (NULL),
Benjamin Kramer36a08102010-07-16 12:32:33 +000049 m_process (thread.GetProcess()),
50 m_thread (thread)
Chris Lattner24943d22010-06-08 16:52:24 +000051{
Chris Lattner24943d22010-06-08 16:52:24 +000052 SetOkayToDiscard (discard_on_error);
53
54 Process& process = thread.GetProcess();
55 Target& target = process.GetTarget();
56 const ABI *abi = process.GetABI();
Sean Callanan07f3d8d2010-11-03 01:37:52 +000057
Chris Lattner24943d22010-06-08 16:52:24 +000058 if (!abi)
59 return;
Sean Callanan07f3d8d2010-11-03 01:37:52 +000060
61 SetBreakpoints();
62
Chris Lattner24943d22010-06-08 16:52:24 +000063 lldb::addr_t spBelowRedZone = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
64
65 SymbolContextList contexts;
66 SymbolContext context;
67 ModuleSP executableModuleSP (target.GetExecutableModule());
68
69 if (!executableModuleSP ||
70 !executableModuleSP->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
71 return;
72
73 contexts.GetContextAtIndex(0, context);
74
75 m_start_addr = context.symbol->GetValue();
Greg Claytoneea26402010-09-14 23:36:40 +000076 lldb::addr_t StartLoadAddr = m_start_addr.GetLoadAddress(&target);
Chris Lattner24943d22010-06-08 16:52:24 +000077
78 if (!thread.SaveFrameZeroState(m_register_backup))
79 return;
80
81 m_function_addr = function;
Greg Claytoneea26402010-09-14 23:36:40 +000082 lldb::addr_t FunctionLoadAddr = m_function_addr.GetLoadAddress(&target);
Chris Lattner24943d22010-06-08 16:52:24 +000083
84 if (!abi->PrepareTrivialCall(thread,
85 spBelowRedZone,
86 FunctionLoadAddr,
87 StartLoadAddr,
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000088 m_arg_addr,
89 this_arg))
Chris Lattner24943d22010-06-08 16:52:24 +000090 return;
91
92 m_valid = true;
93}
94
95ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
96 Address &function,
97 ValueList &args,
98 bool stop_other_threads,
99 bool discard_on_error) :
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000100 ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
Benjamin Kramer36a08102010-07-16 12:32:33 +0000101 m_valid (false),
102 m_stop_other_threads (stop_other_threads),
Chris Lattner24943d22010-06-08 16:52:24 +0000103 m_arg_addr (0),
104 m_args (&args),
Benjamin Kramer36a08102010-07-16 12:32:33 +0000105 m_process (thread.GetProcess()),
106 m_thread (thread)
Chris Lattner24943d22010-06-08 16:52:24 +0000107{
108
109 SetOkayToDiscard (discard_on_error);
110
111 Process& process = thread.GetProcess();
112 Target& target = process.GetTarget();
113 const ABI *abi = process.GetABI();
114
115 if(!abi)
116 return;
117
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000118 SetBreakpoints();
119
Chris Lattner24943d22010-06-08 16:52:24 +0000120 lldb::addr_t spBelowRedZone = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
121
122 SymbolContextList contexts;
123 SymbolContext context;
124 ModuleSP executableModuleSP (target.GetExecutableModule());
125
126 if (!executableModuleSP ||
127 !executableModuleSP->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
128 return;
129
130 contexts.GetContextAtIndex(0, context);
131
132 m_start_addr = context.symbol->GetValue();
Greg Claytoneea26402010-09-14 23:36:40 +0000133 lldb::addr_t StartLoadAddr = m_start_addr.GetLoadAddress(&target);
Chris Lattner24943d22010-06-08 16:52:24 +0000134
135 if(!thread.SaveFrameZeroState(m_register_backup))
136 return;
137
138 m_function_addr = function;
Greg Claytoneea26402010-09-14 23:36:40 +0000139 lldb::addr_t FunctionLoadAddr = m_function_addr.GetLoadAddress(&target);
Chris Lattner24943d22010-06-08 16:52:24 +0000140
141 if (!abi->PrepareNormalCall(thread,
142 spBelowRedZone,
143 FunctionLoadAddr,
144 StartLoadAddr,
145 *m_args))
146 return;
147
148 m_valid = true;
149}
150
151ThreadPlanCallFunction::~ThreadPlanCallFunction ()
152{
153}
154
155void
156ThreadPlanCallFunction::GetDescription (Stream *s, lldb::DescriptionLevel level)
157{
158 if (level == lldb::eDescriptionLevelBrief)
159 {
160 s->Printf("Function call thread plan");
161 }
162 else
163 {
164 if (m_args)
Greg Claytoneea26402010-09-14 23:36:40 +0000165 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 +0000166 else
Greg Claytoneea26402010-09-14 23:36:40 +0000167 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 +0000168 }
169}
170
171bool
172ThreadPlanCallFunction::ValidatePlan (Stream *error)
173{
174 if (!m_valid)
175 return false;
176
177 return true;
178}
179
180bool
181ThreadPlanCallFunction::PlanExplainsStop ()
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000182{
Jim Ingham988ddbc2010-10-26 00:27:45 +0000183 // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
184 // we answer yes.
185 if(m_subplan_sp.get() != NULL && m_subplan_sp->PlanExplainsStop())
186 return true;
Sean Callananba8547d2010-10-19 22:24:06 +0000187
Sean Callanan94fb5432010-11-03 19:36:28 +0000188 // Check if the breakpoint is one of ours.
189
190 if (BreakpointsExplainStop())
191 return true;
192
Jim Ingham988ddbc2010-10-26 00:27:45 +0000193 // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
194 if (!OkayToDiscard())
195 return false;
196
197 // Otherwise, check the case where we stopped for an internal breakpoint, in that case, continue on.
198 // If it is not an internal breakpoint, consult OkayToDiscard.
199 lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000200
Jim Ingham988ddbc2010-10-26 00:27:45 +0000201 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
202 {
203 uint64_t break_site_id = stop_info_sp->GetValue();
204 lldb::BreakpointSiteSP bp_site_sp = m_thread.GetProcess().GetBreakpointSiteList().FindByID(break_site_id);
205 if (bp_site_sp)
206 {
207 uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
208 bool is_internal = true;
209 for (uint32_t i = 0; i < num_owners; i++)
210 {
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000211 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000212
213 if (!bp.IsInternal())
Jim Ingham988ddbc2010-10-26 00:27:45 +0000214 {
215 is_internal = false;
216 break;
217 }
218 }
219 if (is_internal)
220 return false;
221 }
222
223 return OkayToDiscard();
224 }
225 else
226 {
227 // If the subplan is running, any crashes are attributable to us.
228 return (m_subplan_sp.get() != NULL);
229 }
Chris Lattner24943d22010-06-08 16:52:24 +0000230}
231
232bool
233ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
234{
235 if (PlanExplainsStop())
236 {
Sean Callananf5857a02010-07-31 01:32:05 +0000237 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
238
239 if (log)
240 {
241 RegisterContext *reg_ctx = m_thread.GetRegisterContext();
242
243 log->PutCString("Function completed. Register state was:");
244
245 for (uint32_t register_index = 0, num_registers = reg_ctx->GetRegisterCount();
246 register_index < num_registers;
247 ++register_index)
248 {
249 const char *register_name = reg_ctx->GetRegisterName(register_index);
250 uint64_t register_value = reg_ctx->ReadRegisterAsUnsigned(register_index, LLDB_INVALID_ADDRESS);
251
252 log->Printf(" %s = 0x%llx", register_name, register_value);
253 }
254 }
255
Chris Lattner24943d22010-06-08 16:52:24 +0000256 m_thread.RestoreSaveFrameZero(m_register_backup);
257 m_thread.ClearStackFrames();
258 SetPlanComplete();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000259
260 ClearBreakpoints();
Chris Lattner24943d22010-06-08 16:52:24 +0000261 return true;
262 }
263 else
264 {
265 return false;
266 }
267}
268
269bool
270ThreadPlanCallFunction::StopOthers ()
271{
272 return m_stop_other_threads;
273}
274
275void
276ThreadPlanCallFunction::SetStopOthers (bool new_value)
277{
278 if (m_subplan_sp)
279 {
280 ThreadPlanRunToAddress *address_plan = static_cast<ThreadPlanRunToAddress *>(m_subplan_sp.get());
281 address_plan->SetStopOthers(new_value);
282 }
283 m_stop_other_threads = new_value;
284}
285
286StateType
287ThreadPlanCallFunction::RunState ()
288{
289 return eStateRunning;
290}
291
292void
293ThreadPlanCallFunction::DidPush ()
294{
Sean Callananc2c6f772010-10-26 00:31:56 +0000295//#define SINGLE_STEP_EXPRESSIONS
296
297#ifndef SINGLE_STEP_EXPRESSIONS
Chris Lattner24943d22010-06-08 16:52:24 +0000298 m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
299
300 m_thread.QueueThreadPlan(m_subplan_sp, false);
Sean Callananc2c6f772010-10-26 00:31:56 +0000301#endif
Chris Lattner24943d22010-06-08 16:52:24 +0000302}
303
304bool
305ThreadPlanCallFunction::WillStop ()
306{
307 return true;
308}
309
310bool
311ThreadPlanCallFunction::MischiefManaged ()
312{
313 if (IsPlanComplete())
314 {
315 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
316
317 if (log)
318 log->Printf("Completed call function plan.");
319
320 ThreadPlan::MischiefManaged ();
321 return true;
322 }
323 else
324 {
325 return false;
326 }
327}
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000328
329void
330ThreadPlanCallFunction::SetBreakpoints ()
331{
Sean Callanan29756d42010-11-03 22:19:38 +0000332 m_cxx_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeC_plus_plus);
333 m_objc_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeObjC);
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000334
Sean Callanan29756d42010-11-03 22:19:38 +0000335 if (m_cxx_language_runtime)
336 m_cxx_language_runtime->SetExceptionBreakpoints();
337 if (m_objc_language_runtime)
338 m_objc_language_runtime->SetExceptionBreakpoints();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000339}
340
341void
342ThreadPlanCallFunction::ClearBreakpoints ()
343{
Sean Callanan29756d42010-11-03 22:19:38 +0000344 if (m_cxx_language_runtime)
345 m_cxx_language_runtime->ClearExceptionBreakpoints();
346 if (m_objc_language_runtime)
347 m_objc_language_runtime->ClearExceptionBreakpoints();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000348}
Sean Callanan94fb5432010-11-03 19:36:28 +0000349
350bool
351ThreadPlanCallFunction::BreakpointsExplainStop()
352{
Sean Callanan94fb5432010-11-03 19:36:28 +0000353 lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
354
Sean Callanan29756d42010-11-03 22:19:38 +0000355 if (m_cxx_language_runtime &&
356 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
357 return true;
Sean Callanan94fb5432010-11-03 19:36:28 +0000358
Sean Callanan29756d42010-11-03 22:19:38 +0000359 if (m_objc_language_runtime &&
360 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
361 return true;
Sean Callanan94fb5432010-11-03 19:36:28 +0000362
363 return false;
364}