aboutsummaryrefslogtreecommitdiff
path: root/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleThreadPlanStepThroughObjCTrampoline.cpp
blob: a2101c927b4df1cb9bd5b2e9d9631c2d919e80c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//===-- AppleThreadPlanStepThroughObjCTrampoline.cpp --------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "AppleThreadPlanStepThroughObjCTrampoline.h"
#include "AppleObjCTrampolineHandler.h"
#include "lldb/Core/Log.h"
#include "lldb/Expression/DiagnosticManager.h"
#include "lldb/Expression/FunctionCaller.h"
#include "lldb/Expression/UtilityFunction.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/ObjCLanguageRuntime.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Thread.h"
#include "lldb/Target/ThreadPlanRunToAddress.h"
#include "lldb/Target/ThreadPlanStepOut.h"

using namespace lldb;
using namespace lldb_private;

//----------------------------------------------------------------------
// ThreadPlanStepThroughObjCTrampoline constructor
//----------------------------------------------------------------------
AppleThreadPlanStepThroughObjCTrampoline::AppleThreadPlanStepThroughObjCTrampoline
(
    Thread &thread, 
    AppleObjCTrampolineHandler *trampoline_handler,
    ValueList &input_values,
    lldb::addr_t isa_addr,
    lldb::addr_t sel_addr,
    bool stop_others
) :
    ThreadPlan (ThreadPlan::eKindGeneric, 
                "MacOSX Step through ObjC Trampoline", 
                thread, 
                eVoteNoOpinion, 
                eVoteNoOpinion),
    m_trampoline_handler (trampoline_handler),
    m_args_addr (LLDB_INVALID_ADDRESS),
    m_input_values (input_values),
    m_isa_addr(isa_addr),
    m_sel_addr(sel_addr),
    m_impl_function (NULL),
    m_stop_others (stop_others)
{
    
}

//----------------------------------------------------------------------
// Destructor
//----------------------------------------------------------------------
AppleThreadPlanStepThroughObjCTrampoline::~AppleThreadPlanStepThroughObjCTrampoline()
{
}

void
AppleThreadPlanStepThroughObjCTrampoline::DidPush ()
{
    // Setting up the memory space for the called function text might require allocations,
    // i.e. a nested function call.  This needs to be done as a PreResumeAction.
    m_thread.GetProcess()->AddPreResumeAction (PreResumeInitializeFunctionCaller, (void *) this);
}

bool
AppleThreadPlanStepThroughObjCTrampoline::InitializeFunctionCaller ()
{
    if (!m_func_sp)
    {
        DiagnosticManager diagnostics;
        m_args_addr = m_trampoline_handler->SetupDispatchFunction(m_thread, m_input_values);

        if (m_args_addr == LLDB_INVALID_ADDRESS)
        {
            return false;
        }
        m_impl_function = m_trampoline_handler->GetLookupImplementationFunctionCaller();
        ExecutionContext exc_ctx;
        EvaluateExpressionOptions options;
        options.SetUnwindOnError(true);
        options.SetIgnoreBreakpoints(true);
        options.SetStopOthers(m_stop_others);
        m_thread.CalculateExecutionContext(exc_ctx);
        m_func_sp = m_impl_function->GetThreadPlanToCallFunction(exc_ctx, m_args_addr, options, diagnostics);
        m_func_sp->SetOkayToDiscard(true);
        m_thread.QueueThreadPlan(m_func_sp, false);
    }
    return true;
}

bool
AppleThreadPlanStepThroughObjCTrampoline::PreResumeInitializeFunctionCaller(void *void_myself)
{
    AppleThreadPlanStepThroughObjCTrampoline *myself = static_cast<AppleThreadPlanStepThroughObjCTrampoline *>(void_myself);
    return myself->InitializeFunctionCaller();
}

void
AppleThreadPlanStepThroughObjCTrampoline::GetDescription (Stream *s,
                                                          lldb::DescriptionLevel level)
{
    if (level == lldb::eDescriptionLevelBrief)
        s->Printf("Step through ObjC trampoline");
    else
    {
        s->Printf ("Stepping to implementation of ObjC method - obj: 0x%llx, isa: 0x%" PRIx64 ", sel: 0x%" PRIx64,
                   m_input_values.GetValueAtIndex(0)->GetScalar().ULongLong(), m_isa_addr, m_sel_addr);
    }
}
                
bool
AppleThreadPlanStepThroughObjCTrampoline::ValidatePlan (Stream *error)
{
    return true;
}

bool
AppleThreadPlanStepThroughObjCTrampoline::DoPlanExplainsStop (Event *event_ptr)
{
    // If we get asked to explain the stop it will be because something went
    // wrong (like the implementation for selector function crashed...  We're going
    // to figure out what to do about that, so we do explain the stop.
    return true;
}

lldb::StateType
AppleThreadPlanStepThroughObjCTrampoline::GetPlanRunState ()
{
    return eStateRunning;
}

bool
AppleThreadPlanStepThroughObjCTrampoline::ShouldStop (Event *event_ptr)
{
    // First stage: we are still handling the "call a function to get the target of the dispatch"
    if (m_func_sp)
    {
        if (!m_func_sp->IsPlanComplete())
        {
            return false;
        }
        else
        {
            if (!m_func_sp->PlanSucceeded())
            {
                SetPlanComplete(false);
                return true;
            }
            m_func_sp.reset();
        }
    }
    
    // Second stage, if all went well with the function calling, then fetch the target address, and
    // queue up a "run to that address" plan.
    if (!m_run_to_sp) 
    {
        Value target_addr_value;
        ExecutionContext exc_ctx;
        m_thread.CalculateExecutionContext(exc_ctx);
        m_impl_function->FetchFunctionResults (exc_ctx, m_args_addr, target_addr_value);
        m_impl_function->DeallocateFunctionResults(exc_ctx, m_args_addr);
        lldb::addr_t target_addr = target_addr_value.GetScalar().ULongLong();
        Address target_so_addr;
        target_so_addr.SetOpcodeLoadAddress(target_addr, exc_ctx.GetTargetPtr());
        Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
        if (target_addr == 0)
        {
            if (log)
                log->Printf("Got target implementation of 0x0, stopping.");
            SetPlanComplete();
            return true;
        }
        if (m_trampoline_handler->AddrIsMsgForward(target_addr))
        {
            if (log)
                log->Printf ("Implementation lookup returned msgForward function: 0x%" PRIx64 ", stopping.", target_addr);

            SymbolContext sc = m_thread.GetStackFrameAtIndex(0)->GetSymbolContext(eSymbolContextEverything);
            const bool abort_other_plans = false;
            const bool first_insn = true;
            const uint32_t frame_idx = 0;
            m_run_to_sp = m_thread.QueueThreadPlanForStepOutNoShouldStop (abort_other_plans,
                                           &sc,
                                           first_insn,
                                           m_stop_others,
                                           eVoteNoOpinion,
                                           eVoteNoOpinion,
                                           frame_idx);
            m_run_to_sp->SetPrivate(true);
            return false;
        }
        
        if (log)
            log->Printf("Running to ObjC method implementation: 0x%" PRIx64, target_addr);
        
        ObjCLanguageRuntime *objc_runtime = GetThread().GetProcess()->GetObjCLanguageRuntime();
        assert (objc_runtime != NULL);
        objc_runtime->AddToMethodCache (m_isa_addr, m_sel_addr, target_addr);
        if (log)
            log->Printf("Adding {isa-addr=0x%" PRIx64 ", sel-addr=0x%" PRIx64 "} = addr=0x%" PRIx64 " to cache.", m_isa_addr, m_sel_addr, target_addr);

        // Extract the target address from the value:
        
        m_run_to_sp.reset(new ThreadPlanRunToAddress(m_thread, target_so_addr, m_stop_others));
        m_thread.QueueThreadPlan(m_run_to_sp, false);
        m_run_to_sp->SetPrivate(true);
        return false;
    }
    else if (m_thread.IsThreadPlanDone(m_run_to_sp.get()))
    {
        // Third stage, work the run to target plan.
        SetPlanComplete();
        return true;
    }
    return false;
}

// The base class MischiefManaged does some cleanup - so you have to call it
// in your MischiefManaged derived class.
bool
AppleThreadPlanStepThroughObjCTrampoline::MischiefManaged ()
{
    if (IsPlanComplete())
        return true;
    else
        return false;
}

bool
AppleThreadPlanStepThroughObjCTrampoline::WillStop()
{
    return true;
}