ThreadPlanCallFunction.cpp revision 14a97ff7ccb8d40fee3c6ff136a2c602819174dd
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
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,
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    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) :
100    ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
101    m_valid (false),
102    m_stop_other_threads (stop_other_threads),
103    m_arg_addr (0),
104    m_args (&args),
105    m_process (thread.GetProcess()),
106    m_thread (thread)
107{
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
118    SetBreakpoints();
119
120    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();
133    lldb::addr_t StartLoadAddr = m_start_addr.GetLoadAddress(&target);
134
135    if(!thread.SaveFrameZeroState(m_register_backup))
136        return;
137
138    m_function_addr = function;
139    lldb::addr_t FunctionLoadAddr = m_function_addr.GetLoadAddress(&target);
140
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    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();
164}
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)
176            s->Printf("Thread plan to call 0x%llx with parsed arguments", m_function_addr.GetLoadAddress(&m_process.GetTarget()), m_arg_addr);
177        else
178            s->Printf("Thread plan to call 0x%llx void * argument at: 0x%llx", m_function_addr.GetLoadAddress(&m_process.GetTarget()), m_arg_addr);
179    }
180}
181
182bool
183ThreadPlanCallFunction::ValidatePlan (Stream *error)
184{
185    if (!m_valid)
186        return false;
187
188    return true;
189}
190
191bool
192ThreadPlanCallFunction::PlanExplainsStop ()
193{
194    // 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;
198
199    // Check if the breakpoint is one of ours.
200
201    if (BreakpointsExplainStop())
202        return true;
203
204    // 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();
211
212    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            {
222                Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
223
224                if (!bp.IsInternal())
225                {
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    }
241}
242
243bool
244ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
245{
246    if (PlanExplainsStop())
247    {
248        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
267        DoTakedown();
268
269        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{
303//#define SINGLE_STEP_EXPRESSIONS
304
305#ifndef SINGLE_STEP_EXPRESSIONS
306    m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
307
308    m_thread.QueueThreadPlan(m_subplan_sp, false);
309#endif
310}
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}
336
337void
338ThreadPlanCallFunction::SetBreakpoints ()
339{
340    m_cxx_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeC_plus_plus);
341    m_objc_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeObjC);
342
343    if (m_cxx_language_runtime)
344        m_cxx_language_runtime->SetExceptionBreakpoints();
345    if (m_objc_language_runtime)
346        m_objc_language_runtime->SetExceptionBreakpoints();
347}
348
349void
350ThreadPlanCallFunction::ClearBreakpoints ()
351{
352    if (m_cxx_language_runtime)
353        m_cxx_language_runtime->ClearExceptionBreakpoints();
354    if (m_objc_language_runtime)
355        m_objc_language_runtime->ClearExceptionBreakpoints();
356}
357
358bool
359ThreadPlanCallFunction::BreakpointsExplainStop()
360{
361    lldb::StopInfoSP stop_info_sp = GetPrivateStopReason();
362
363    if (m_cxx_language_runtime &&
364        m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
365        return true;
366
367    if (m_objc_language_runtime &&
368        m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
369        return true;
370
371    return false;
372}
373