blob: 8c9b3afa0d3f12d1be3edeaed0ea9b2fedf0279c [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
Jim Ingham77787032011-01-20 02:03:1862 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
63
Sean Callanan6db73ca2010-11-03 01:37:5264 SetBreakpoints();
65
Chris Lattner30fdc8d2010-06-08 16:52:2466 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 Claytonf5e56de2010-09-14 23:36:4079 lldb::addr_t StartLoadAddr = m_start_addr.GetLoadAddress(&target);
Chris Lattner30fdc8d2010-06-08 16:52:2480
Jim Ingham77787032011-01-20 02:03:1881 // 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 Lattner30fdc8d2010-06-08 16:52:2486 return;
Jim Ingham77787032011-01-20 02:03:1887 }
88 // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
89 thread.SetStopInfoToNothing();
90
Chris Lattner30fdc8d2010-06-08 16:52:2491 m_function_addr = function;
Greg Claytonf5e56de2010-09-14 23:36:4092 lldb::addr_t FunctionLoadAddr = m_function_addr.GetLoadAddress(&target);
Chris Lattner30fdc8d2010-06-08 16:52:2493
94 if (!abi->PrepareTrivialCall(thread,
95 spBelowRedZone,
96 FunctionLoadAddr,
97 StartLoadAddr,
Sean Callananfc55f5d2010-09-21 00:44:1298 m_arg_addr,
Sean Callanan17827832010-12-13 22:46:1599 this_arg,
100 cmd_arg))
Chris Lattner30fdc8d2010-06-08 16:52:24101 return;
102
Sean Callananece96492010-11-08 03:49:50103 if (log)
104 {
Greg Clayton5ccbd292011-01-06 22:15:06105 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
Sean Callananece96492010-11-08 03:49:50106
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 Lattner30fdc8d2010-06-08 16:52:24120 m_valid = true;
121}
122
Chris Lattner30fdc8d2010-06-08 16:52:24123ThreadPlanCallFunction::~ThreadPlanCallFunction ()
124{
Sean Callanan10af7c42010-11-04 01:51:38125 DoTakedown();
126}
127
128void
129ThreadPlanCallFunction::DoTakedown ()
130{
Jim Ingham77787032011-01-20 02:03:18131 if (m_valid && !IsPlanComplete())
132 {
133 m_thread.RestoreThreadStateFromCheckpoint(m_stored_thread_state);
134 SetPlanComplete();
135 ClearBreakpoints();
136 }
Chris Lattner30fdc8d2010-06-08 16:52:24137}
138
139void
Jim Inghambda4e5eb2011-01-18 01:58:06140ThreadPlanCallFunction::WillPop ()
141{
Jim Ingham77787032011-01-20 02:03:18142 DoTakedown();
Jim Inghambda4e5eb2011-01-18 01:58:06143}
144
145void
Chris Lattner30fdc8d2010-06-08 16:52:24146ThreadPlanCallFunction::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 Claytonf5e56de2010-09-14 23:36:40155 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:24156 else
Greg Claytonf5e56de2010-09-14 23:36:40157 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:24158 }
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 Callanan6db73ca2010-11-03 01:37:52172{
Jim Ingham40d871f2010-10-26 00:27:45173 // 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 Callanan3e6fedc2010-10-19 22:24:06177
Sean Callananc98aca62010-11-03 19:36:28178 // Check if the breakpoint is one of ours.
179
180 if (BreakpointsExplainStop())
181 return true;
182
Jim Ingham40d871f2010-10-26 00:27:45183 // 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 Callanan6db73ca2010-11-03 01:37:52190
Jim Ingham40d871f2010-10-26 00:27:45191 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 Callanan6db73ca2010-11-03 01:37:52201 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
Sean Callanan6db73ca2010-11-03 01:37:52202
203 if (!bp.IsInternal())
Jim Ingham40d871f2010-10-26 00:27:45204 {
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 Lattner30fdc8d2010-06-08 16:52:24220}
221
222bool
223ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
224{
225 if (PlanExplainsStop())
226 {
Greg Clayton2d4edfb2010-11-06 01:53:30227 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Sean Callanan5300d372010-07-31 01:32:05228
229 if (log)
230 {
Greg Clayton5ccbd292011-01-06 22:15:06231 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
Sean Callanan5300d372010-07-31 01:32:05232
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 Callanan10af7c42010-11-04 01:51:38246 DoTakedown();
Sean Callanan6db73ca2010-11-03 01:37:52247
Chris Lattner30fdc8d2010-06-08 16:52:24248 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 Ingham06e827c2010-11-11 19:26:09274ThreadPlanCallFunction::GetPlanRunState ()
Chris Lattner30fdc8d2010-06-08 16:52:24275{
276 return eStateRunning;
277}
278
279void
280ThreadPlanCallFunction::DidPush ()
281{
Sean Callananbe3a1b12010-10-26 00:31:56282//#define SINGLE_STEP_EXPRESSIONS
283
284#ifndef SINGLE_STEP_EXPRESSIONS
Chris Lattner30fdc8d2010-06-08 16:52:24285 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 Ingham77787032011-01-20 02:03:18288 m_subplan_sp->SetPrivate (true);
Sean Callananbe3a1b12010-10-26 00:31:56289#endif
Chris Lattner30fdc8d2010-06-08 16:52:24290}
291
292bool
293ThreadPlanCallFunction::WillStop ()
294{
295 return true;
296}
297
298bool
299ThreadPlanCallFunction::MischiefManaged ()
300{
301 if (IsPlanComplete())
302 {
Greg Clayton2d4edfb2010-11-06 01:53:30303 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24304
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 Callanan6db73ca2010-11-03 01:37:52316
317void
318ThreadPlanCallFunction::SetBreakpoints ()
319{
Sean Callananf2115102010-11-03 22:19:38320 m_cxx_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeC_plus_plus);
321 m_objc_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeObjC);
Sean Callanan6db73ca2010-11-03 01:37:52322
Sean Callananf2115102010-11-03 22:19:38323 if (m_cxx_language_runtime)
324 m_cxx_language_runtime->SetExceptionBreakpoints();
325 if (m_objc_language_runtime)
326 m_objc_language_runtime->SetExceptionBreakpoints();
Sean Callanan6db73ca2010-11-03 01:37:52327}
328
329void
330ThreadPlanCallFunction::ClearBreakpoints ()
331{
Sean Callananf2115102010-11-03 22:19:38332 if (m_cxx_language_runtime)
333 m_cxx_language_runtime->ClearExceptionBreakpoints();
334 if (m_objc_language_runtime)
335 m_objc_language_runtime->ClearExceptionBreakpoints();
Sean Callanan6db73ca2010-11-03 01:37:52336}
Sean Callananc98aca62010-11-03 19:36:28337
338bool
339ThreadPlanCallFunction::BreakpointsExplainStop()
340{
Sean Callananc98aca62010-11-03 19:36:28341 lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
342
Sean Callananf2115102010-11-03 22:19:38343 if (m_cxx_language_runtime &&
344 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
345 return true;
Sean Callananc98aca62010-11-03 19:36:28346
Sean Callananf2115102010-11-03 22:19:38347 if (m_objc_language_runtime &&
348 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
349 return true;
Sean Callananc98aca62010-11-03 19:36:28350
351 return false;
352}