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     ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
45     m_valid (false),
46     m_stop_other_threads (stop_other_threads),
47     m_arg_addr (arg),
48     m_args (NULL),
49     m_process (thread.GetProcess()),
50     m_thread (thread)
51 {
52     SetOkayToDiscard (discard_on_error);
53 
54     Process& process = thread.GetProcess();
55     Target& target = process.GetTarget();
56     const ABI *abi = process.GetABI();
57 
58     if (!abi)
59         return;
60 
61     SetBreakpoints();
62 
63     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();
76     lldb::addr_t StartLoadAddr = m_start_addr.GetLoadAddress(&target);
77 
78     if (!thread.SaveFrameZeroState(m_register_backup))
79         return;
80 
81     m_function_addr = function;
82     lldb::addr_t FunctionLoadAddr = m_function_addr.GetLoadAddress(&target);
83 
84     if (!abi->PrepareTrivialCall(thread,
85                                  spBelowRedZone,
86                                  FunctionLoadAddr,
87                                  StartLoadAddr,
88                                  m_arg_addr,
89                                  this_arg))
90         return;
91 
92     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
93 
94     if (log)
95     {
96         RegisterContext *reg_ctx = m_thread.GetRegisterContext();
97 
98         log->PutCString("Function call was set up.  Register state was:");
99 
100         for (uint32_t register_index = 0, num_registers = reg_ctx->GetRegisterCount();
101              register_index < num_registers;
102              ++register_index)
103         {
104             const char *register_name = reg_ctx->GetRegisterName(register_index);
105             uint64_t register_value = reg_ctx->ReadRegisterAsUnsigned(register_index, LLDB_INVALID_ADDRESS);
106 
107             log->Printf("  %s = 0x%llx", register_name, register_value);
108         }
109     }
110 
111     m_valid = true;
112 }
113 
114 ThreadPlanCallFunction::~ThreadPlanCallFunction ()
115 {
116     if (m_valid && !IsPlanComplete())
117         DoTakedown();
118 }
119 
120 void
121 ThreadPlanCallFunction::DoTakedown ()
122 {
123     m_thread.RestoreSaveFrameZero(m_register_backup);
124     m_thread.ClearStackFrames();
125     SetPlanComplete();
126     ClearBreakpoints();
127 }
128 
129 void
130 ThreadPlanCallFunction::GetDescription (Stream *s, lldb::DescriptionLevel level)
131 {
132     if (level == lldb::eDescriptionLevelBrief)
133     {
134         s->Printf("Function call thread plan");
135     }
136     else
137     {
138         if (m_args)
139             s->Printf("Thread plan to call 0x%llx with parsed arguments", m_function_addr.GetLoadAddress(&m_process.GetTarget()), m_arg_addr);
140         else
141             s->Printf("Thread plan to call 0x%llx void * argument at: 0x%llx", m_function_addr.GetLoadAddress(&m_process.GetTarget()), m_arg_addr);
142     }
143 }
144 
145 bool
146 ThreadPlanCallFunction::ValidatePlan (Stream *error)
147 {
148     if (!m_valid)
149         return false;
150 
151     return true;
152 }
153 
154 bool
155 ThreadPlanCallFunction::PlanExplainsStop ()
156 {
157     // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
158     // we answer yes.
159     if(m_subplan_sp.get() != NULL && m_subplan_sp->PlanExplainsStop())
160         return true;
161 
162     // Check if the breakpoint is one of ours.
163 
164     if (BreakpointsExplainStop())
165         return true;
166 
167     // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
168     if (!OkayToDiscard())
169         return false;
170 
171     // Otherwise, check the case where we stopped for an internal breakpoint, in that case, continue on.
172     // If it is not an internal breakpoint, consult OkayToDiscard.
173     lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
174 
175     if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
176     {
177         uint64_t break_site_id = stop_info_sp->GetValue();
178         lldb::BreakpointSiteSP bp_site_sp = m_thread.GetProcess().GetBreakpointSiteList().FindByID(break_site_id);
179         if (bp_site_sp)
180         {
181             uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
182             bool is_internal = true;
183             for (uint32_t i = 0; i < num_owners; i++)
184             {
185                 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
186 
187                 if (!bp.IsInternal())
188                 {
189                     is_internal = false;
190                     break;
191                 }
192             }
193             if (is_internal)
194                 return false;
195         }
196 
197         return OkayToDiscard();
198     }
199     else
200     {
201         // If the subplan is running, any crashes are attributable to us.
202         return (m_subplan_sp.get() != NULL);
203     }
204 }
205 
206 bool
207 ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
208 {
209     if (PlanExplainsStop())
210     {
211         LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
212 
213         if (log)
214         {
215             RegisterContext *reg_ctx = m_thread.GetRegisterContext();
216 
217             log->PutCString("Function completed.  Register state was:");
218 
219             for (uint32_t register_index = 0, num_registers = reg_ctx->GetRegisterCount();
220                  register_index < num_registers;
221                  ++register_index)
222             {
223                 const char *register_name = reg_ctx->GetRegisterName(register_index);
224                 uint64_t register_value = reg_ctx->ReadRegisterAsUnsigned(register_index, LLDB_INVALID_ADDRESS);
225 
226                 log->Printf("  %s = 0x%llx", register_name, register_value);
227             }
228         }
229 
230         DoTakedown();
231 
232         return true;
233     }
234     else
235     {
236         return false;
237     }
238 }
239 
240 bool
241 ThreadPlanCallFunction::StopOthers ()
242 {
243     return m_stop_other_threads;
244 }
245 
246 void
247 ThreadPlanCallFunction::SetStopOthers (bool new_value)
248 {
249     if (m_subplan_sp)
250     {
251         ThreadPlanRunToAddress *address_plan = static_cast<ThreadPlanRunToAddress *>(m_subplan_sp.get());
252         address_plan->SetStopOthers(new_value);
253     }
254     m_stop_other_threads = new_value;
255 }
256 
257 StateType
258 ThreadPlanCallFunction::GetPlanRunState ()
259 {
260     return eStateRunning;
261 }
262 
263 void
264 ThreadPlanCallFunction::DidPush ()
265 {
266 //#define SINGLE_STEP_EXPRESSIONS
267 
268 #ifndef SINGLE_STEP_EXPRESSIONS
269     m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
270 
271     m_thread.QueueThreadPlan(m_subplan_sp, false);
272 #endif
273 }
274 
275 bool
276 ThreadPlanCallFunction::WillStop ()
277 {
278     return true;
279 }
280 
281 bool
282 ThreadPlanCallFunction::MischiefManaged ()
283 {
284     if (IsPlanComplete())
285     {
286         LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
287 
288         if (log)
289             log->Printf("Completed call function plan.");
290 
291         ThreadPlan::MischiefManaged ();
292         return true;
293     }
294     else
295     {
296         return false;
297     }
298 }
299 
300 void
301 ThreadPlanCallFunction::SetBreakpoints ()
302 {
303     m_cxx_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeC_plus_plus);
304     m_objc_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeObjC);
305 
306     if (m_cxx_language_runtime)
307         m_cxx_language_runtime->SetExceptionBreakpoints();
308     if (m_objc_language_runtime)
309         m_objc_language_runtime->SetExceptionBreakpoints();
310 }
311 
312 void
313 ThreadPlanCallFunction::ClearBreakpoints ()
314 {
315     if (m_cxx_language_runtime)
316         m_cxx_language_runtime->ClearExceptionBreakpoints();
317     if (m_objc_language_runtime)
318         m_objc_language_runtime->ClearExceptionBreakpoints();
319 }
320 
321 bool
322 ThreadPlanCallFunction::BreakpointsExplainStop()
323 {
324     lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
325 
326     if (m_cxx_language_runtime &&
327         m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
328         return true;
329 
330     if (m_objc_language_runtime &&
331         m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
332         return true;
333 
334     return false;
335 }
336