blob: 85de672a63db34ebe7df8c81107a8a7153afe021 [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{
Sean Callanan14a97ff2010-11-04 01:51:38 +0000153 if (m_valid && !IsPlanComplete())
154 DoTakedown();
155}
156
157void
158ThreadPlanCallFunction::DoTakedown ()
159{
160 m_thread.RestoreSaveFrameZero(m_register_backup);
161 m_thread.ClearStackFrames();
162 SetPlanComplete();
163 ClearBreakpoints();
Chris Lattner24943d22010-06-08 16:52:24 +0000164}
165
166void
167ThreadPlanCallFunction::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 {
Sean Callananf5857a02010-07-31 01:32:05 +0000248 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
249
250 if (log)
251 {
252 RegisterContext *reg_ctx = m_thread.GetRegisterContext();
253
254 log->PutCString("Function completed. Register state was:");
255
256 for (uint32_t register_index = 0, num_registers = reg_ctx->GetRegisterCount();
257 register_index < num_registers;
258 ++register_index)
259 {
260 const char *register_name = reg_ctx->GetRegisterName(register_index);
261 uint64_t register_value = reg_ctx->ReadRegisterAsUnsigned(register_index, LLDB_INVALID_ADDRESS);
262
263 log->Printf(" %s = 0x%llx", register_name, register_value);
264 }
265 }
266
Sean Callanan14a97ff2010-11-04 01:51:38 +0000267 DoTakedown();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000268
Chris Lattner24943d22010-06-08 16:52:24 +0000269 return true;
270 }
271 else
272 {
273 return false;
274 }
275}
276
277bool
278ThreadPlanCallFunction::StopOthers ()
279{
280 return m_stop_other_threads;
281}
282
283void
284ThreadPlanCallFunction::SetStopOthers (bool new_value)
285{
286 if (m_subplan_sp)
287 {
288 ThreadPlanRunToAddress *address_plan = static_cast<ThreadPlanRunToAddress *>(m_subplan_sp.get());
289 address_plan->SetStopOthers(new_value);
290 }
291 m_stop_other_threads = new_value;
292}
293
294StateType
295ThreadPlanCallFunction::RunState ()
296{
297 return eStateRunning;
298}
299
300void
301ThreadPlanCallFunction::DidPush ()
302{
Sean Callananc2c6f772010-10-26 00:31:56 +0000303//#define SINGLE_STEP_EXPRESSIONS
304
305#ifndef SINGLE_STEP_EXPRESSIONS
Chris Lattner24943d22010-06-08 16:52:24 +0000306 m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
307
308 m_thread.QueueThreadPlan(m_subplan_sp, false);
Sean Callananc2c6f772010-10-26 00:31:56 +0000309#endif
Chris Lattner24943d22010-06-08 16:52:24 +0000310}
311
312bool
313ThreadPlanCallFunction::WillStop ()
314{
315 return true;
316}
317
318bool
319ThreadPlanCallFunction::MischiefManaged ()
320{
321 if (IsPlanComplete())
322 {
323 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
324
325 if (log)
326 log->Printf("Completed call function plan.");
327
328 ThreadPlan::MischiefManaged ();
329 return true;
330 }
331 else
332 {
333 return false;
334 }
335}
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000336
337void
338ThreadPlanCallFunction::SetBreakpoints ()
339{
Sean Callanan29756d42010-11-03 22:19:38 +0000340 m_cxx_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeC_plus_plus);
341 m_objc_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeObjC);
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000342
Sean Callanan29756d42010-11-03 22:19:38 +0000343 if (m_cxx_language_runtime)
344 m_cxx_language_runtime->SetExceptionBreakpoints();
345 if (m_objc_language_runtime)
346 m_objc_language_runtime->SetExceptionBreakpoints();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000347}
348
349void
350ThreadPlanCallFunction::ClearBreakpoints ()
351{
Sean Callanan29756d42010-11-03 22:19:38 +0000352 if (m_cxx_language_runtime)
353 m_cxx_language_runtime->ClearExceptionBreakpoints();
354 if (m_objc_language_runtime)
355 m_objc_language_runtime->ClearExceptionBreakpoints();
Sean Callanan07f3d8d2010-11-03 01:37:52 +0000356}
Sean Callanan94fb5432010-11-03 19:36:28 +0000357
358bool
359ThreadPlanCallFunction::BreakpointsExplainStop()
360{
Sean Callanan94fb5432010-11-03 19:36:28 +0000361 lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
362
Sean Callanan29756d42010-11-03 22:19:38 +0000363 if (m_cxx_language_runtime &&
364 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
365 return true;
Sean Callanan94fb5432010-11-03 19:36:28 +0000366
Sean Callanan29756d42010-11-03 22:19:38 +0000367 if (m_objc_language_runtime &&
368 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
369 return true;
Sean Callanan94fb5432010-11-03 19:36:28 +0000370
371 return false;
372}