1 //===-- 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
15 #include "llvm/Support/MachO.h"
16 // Project includes
17 #include "lldb/lldb-private-log.h"
18 #include "lldb/Breakpoint/Breakpoint.h"
19 #include "lldb/Breakpoint/BreakpointLocation.h"
20 #include "lldb/Core/Address.h"
21 #include "lldb/Core/Log.h"
22 #include "lldb/Core/Stream.h"
23 #include "lldb/Target/LanguageRuntime.h"
24 #include "lldb/Target/Process.h"
25 #include "lldb/Target/RegisterContext.h"
26 #include "lldb/Target/StopInfo.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Target/Thread.h"
29 #include "lldb/Target/ThreadPlanRunToAddress.h"
30 
31 using namespace lldb;
32 using namespace lldb_private;
33 
34 //----------------------------------------------------------------------
35 // ThreadPlanCallFunction: Plan to call a single function
36 //----------------------------------------------------------------------
37 
38 ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
39                                                 Address &function,
40                                                 lldb::addr_t arg,
41                                                 bool stop_other_threads,
42                                                 bool discard_on_error,
43                                                 lldb::addr_t *this_arg,
44                                                 lldb::addr_t *cmd_arg) :
45     ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
46     m_valid (false),
47     m_stop_other_threads (stop_other_threads),
48     m_arg_addr (arg),
49     m_args (NULL),
50     m_process (thread.GetProcess()),
51     m_thread (thread),
52     m_takedown_done (false)
53 {
54     SetOkayToDiscard (discard_on_error);
55 
56     Process& process = thread.GetProcess();
57     Target& target = process.GetTarget();
58     const ABI *abi = process.GetABI();
59 
60     if (!abi)
61         return;
62 
63     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
64 
65     SetBreakpoints();
66 
67     lldb::addr_t spBelowRedZone = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
68 
69     SymbolContextList contexts;
70     SymbolContext context;
71     ModuleSP executableModuleSP (target.GetExecutableModule());
72 
73     if (!executableModuleSP ||
74         !executableModuleSP->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
75         return;
76 
77     contexts.GetContextAtIndex(0, context);
78 
79     m_start_addr = context.symbol->GetValue();
80     lldb::addr_t StartLoadAddr = m_start_addr.GetLoadAddress(&target);
81 
82     // Checkpoint the thread state so we can restore it later.
83     if (log && log->GetVerbose())
84         ReportRegisterState ("About to checkpoint thread before function call.  Original register state was:");
85 
86     if (!thread.CheckpointThreadState (m_stored_thread_state))
87     {
88         if (log)
89             log->Printf ("Setting up ThreadPlanCallFunction, failed to checkpoint thread state.");
90         return;
91     }
92     // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
93     thread.SetStopInfoToNothing();
94 
95     m_function_addr = function;
96     lldb::addr_t FunctionLoadAddr = m_function_addr.GetLoadAddress(&target);
97 
98     if (!abi->PrepareTrivialCall(thread,
99                                  spBelowRedZone,
100                                  FunctionLoadAddr,
101                                  StartLoadAddr,
102                                  m_arg_addr,
103                                  this_arg,
104                                  cmd_arg))
105         return;
106 
107     ReportRegisterState ("Function call was set up.  Register state was:");
108 
109     m_valid = true;
110 }
111 
112 ThreadPlanCallFunction::~ThreadPlanCallFunction ()
113 {
114 }
115 
116 void
117 ThreadPlanCallFunction::ReportRegisterState (const char *message)
118 {
119     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
120     if (log)
121     {
122         RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
123 
124         log->PutCString(message);
125 
126         for (uint32_t register_index = 0, num_registers = reg_ctx->GetRegisterCount();
127              register_index < num_registers;
128              ++register_index)
129         {
130             const char *register_name = reg_ctx->GetRegisterName(register_index);
131             uint64_t register_value = reg_ctx->ReadRegisterAsUnsigned(register_index, LLDB_INVALID_ADDRESS);
132 
133             log->Printf("  %s = 0x%llx", register_name, register_value);
134         }
135     }
136 }
137 
138 void
139 ThreadPlanCallFunction::DoTakedown ()
140 {
141     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
142     if (!m_takedown_done)
143     {
144         if (log)
145             log->Printf ("DoTakedown called for thread 0x%4.4x, m_valid: %d complete: %d.\n", m_thread.GetID(), m_valid, IsPlanComplete());
146         m_takedown_done = true;
147         m_thread.RestoreThreadStateFromCheckpoint(m_stored_thread_state);
148         SetPlanComplete();
149         ClearBreakpoints();
150         if (log && log->GetVerbose())
151             ReportRegisterState ("Restoring thread state after function call.  Restored register state:");
152 
153     }
154     else
155     {
156         if (log)
157             log->Printf ("DoTakedown called as no-op for thread 0x%4.4x, m_valid: %d complete: %d.\n", m_thread.GetID(), m_valid, IsPlanComplete());
158     }
159 }
160 
161 void
162 ThreadPlanCallFunction::WillPop ()
163 {
164     DoTakedown();
165 }
166 
167 void
168 ThreadPlanCallFunction::GetDescription (Stream *s, lldb::DescriptionLevel level)
169 {
170     if (level == lldb::eDescriptionLevelBrief)
171     {
172         s->Printf("Function call thread plan");
173     }
174     else
175     {
176         if (m_args)
177             s->Printf("Thread plan to call 0x%llx with parsed arguments", m_function_addr.GetLoadAddress(&m_process.GetTarget()), m_arg_addr);
178         else
179             s->Printf("Thread plan to call 0x%llx void * argument at: 0x%llx", m_function_addr.GetLoadAddress(&m_process.GetTarget()), m_arg_addr);
180     }
181 }
182 
183 bool
184 ThreadPlanCallFunction::ValidatePlan (Stream *error)
185 {
186     if (!m_valid)
187         return false;
188 
189     return true;
190 }
191 
192 bool
193 ThreadPlanCallFunction::PlanExplainsStop ()
194 {
195     // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
196     // we answer yes.
197     if(m_subplan_sp.get() != NULL && m_subplan_sp->PlanExplainsStop())
198         return true;
199 
200     // Check if the breakpoint is one of ours.
201 
202     if (BreakpointsExplainStop())
203         return true;
204 
205     // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
206     if (!OkayToDiscard())
207         return false;
208 
209     // Otherwise, check the case where we stopped for an internal breakpoint, in that case, continue on.
210     // If it is not an internal breakpoint, consult OkayToDiscard.
211     lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
212 
213     if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
214     {
215         uint64_t break_site_id = stop_info_sp->GetValue();
216         lldb::BreakpointSiteSP bp_site_sp = m_thread.GetProcess().GetBreakpointSiteList().FindByID(break_site_id);
217         if (bp_site_sp)
218         {
219             uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
220             bool is_internal = true;
221             for (uint32_t i = 0; i < num_owners; i++)
222             {
223                 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
224 
225                 if (!bp.IsInternal())
226                 {
227                     is_internal = false;
228                     break;
229                 }
230             }
231             if (is_internal)
232                 return false;
233         }
234 
235         return OkayToDiscard();
236     }
237     else
238     {
239         // If the subplan is running, any crashes are attributable to us.
240         // If we want to discard the plan, then we say we explain the stop
241         // but if we are going to be discarded, let whoever is above us
242         // explain the stop.
243         return ((m_subplan_sp.get() != NULL) && !OkayToDiscard());
244     }
245 }
246 
247 bool
248 ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
249 {
250     if (PlanExplainsStop())
251     {
252         ReportRegisterState ("Function completed.  Register state was:");
253 
254         DoTakedown();
255 
256         return true;
257     }
258     else
259     {
260         return false;
261     }
262 }
263 
264 bool
265 ThreadPlanCallFunction::StopOthers ()
266 {
267     return m_stop_other_threads;
268 }
269 
270 void
271 ThreadPlanCallFunction::SetStopOthers (bool new_value)
272 {
273     if (m_subplan_sp)
274     {
275         ThreadPlanRunToAddress *address_plan = static_cast<ThreadPlanRunToAddress *>(m_subplan_sp.get());
276         address_plan->SetStopOthers(new_value);
277     }
278     m_stop_other_threads = new_value;
279 }
280 
281 StateType
282 ThreadPlanCallFunction::GetPlanRunState ()
283 {
284     return eStateRunning;
285 }
286 
287 void
288 ThreadPlanCallFunction::DidPush ()
289 {
290 //#define SINGLE_STEP_EXPRESSIONS
291 
292 #ifndef SINGLE_STEP_EXPRESSIONS
293     m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
294 
295     m_thread.QueueThreadPlan(m_subplan_sp, false);
296     m_subplan_sp->SetPrivate (true);
297 #endif
298 }
299 
300 bool
301 ThreadPlanCallFunction::WillStop ()
302 {
303     return true;
304 }
305 
306 bool
307 ThreadPlanCallFunction::MischiefManaged ()
308 {
309     if (IsPlanComplete())
310     {
311         LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
312 
313         if (log)
314             log->Printf("Completed call function plan.");
315 
316         ThreadPlan::MischiefManaged ();
317         return true;
318     }
319     else
320     {
321         return false;
322     }
323 }
324 
325 void
326 ThreadPlanCallFunction::SetBreakpoints ()
327 {
328     m_cxx_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeC_plus_plus);
329     m_objc_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeObjC);
330 
331     if (m_cxx_language_runtime)
332         m_cxx_language_runtime->SetExceptionBreakpoints();
333     if (m_objc_language_runtime)
334         m_objc_language_runtime->SetExceptionBreakpoints();
335 }
336 
337 void
338 ThreadPlanCallFunction::ClearBreakpoints ()
339 {
340     if (m_cxx_language_runtime)
341         m_cxx_language_runtime->ClearExceptionBreakpoints();
342     if (m_objc_language_runtime)
343         m_objc_language_runtime->ClearExceptionBreakpoints();
344 }
345 
346 bool
347 ThreadPlanCallFunction::BreakpointsExplainStop()
348 {
349     lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
350 
351     if (m_cxx_language_runtime &&
352         m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
353         return true;
354 
355     if (m_objc_language_runtime &&
356         m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
357         return true;
358 
359     return false;
360 }
361