aboutsummaryrefslogtreecommitdiff
path: root/source/Symbol/FuncUnwinders.cpp
blob: 95fc817478598fad13cad4c4b055828844d84c1a (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//===-- FuncUnwinders.cpp ----------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Core/AddressRange.h"
#include "lldb/Core/Address.h"
#include "lldb/Symbol/FuncUnwinders.h"
#include "lldb/Symbol/DWARFCallFrameInfo.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/UnwindPlan.h"
#include "lldb/Symbol/UnwindTable.h"
#include "lldb/Target/ABI.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Thread.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/UnwindAssembly.h"

using namespace lldb;
using namespace lldb_private;


FuncUnwinders::FuncUnwinders
(
    UnwindTable& unwind_table, 
    AddressRange range
) : 
    m_unwind_table(unwind_table), 
    m_range(range), 
    m_mutex (Mutex::eMutexTypeRecursive),
    m_unwind_plan_call_site_sp (), 
    m_unwind_plan_non_call_site_sp (), 
    m_unwind_plan_fast_sp (), 
    m_unwind_plan_arch_default_sp (), 
    m_tried_unwind_at_call_site (false),
    m_tried_unwind_at_non_call_site (false),
    m_tried_unwind_fast (false),
    m_tried_unwind_arch_default (false),
    m_tried_unwind_arch_default_at_func_entry (false),
    m_first_non_prologue_insn() 
{
}

FuncUnwinders::~FuncUnwinders () 
{ 
}

UnwindPlanSP
FuncUnwinders::GetUnwindPlanAtCallSite (int current_offset)
{
    // Lock the mutex to ensure we can always give out the most appropriate
    // information. We want to make sure if someone requests a call site unwind
    // plan, that they get one and don't run into a race condition where one
    // thread has started to create the unwind plan and has put it into 
    // m_unwind_plan_call_site_sp, and have another thread enter this function
    // and return the partially filled in m_unwind_plan_call_site_sp pointer.
    // We also want to make sure that we lock out other unwind plans from
    // being accessed until this one is done creating itself in case someone
    // had some code like:
    //  UnwindPlan *best_unwind_plan = ...GetUnwindPlanAtCallSite (...)
    //  if (best_unwind_plan == NULL)
    //      best_unwind_plan = GetUnwindPlanAtNonCallSite (...)
    Mutex::Locker locker (m_mutex);
    if (m_tried_unwind_at_call_site == false && m_unwind_plan_call_site_sp.get() == nullptr)
    {
        m_tried_unwind_at_call_site = true;
        // We have cases (e.g. with _sigtramp on Mac OS X) where the hand-written eh_frame unwind info for a
        // function does not cover the entire range of the function and so the FDE only lists a subset of the
        // address range.  If we try to look up the unwind info by the starting address of the function 
        // (i.e. m_range.GetBaseAddress()) we may not find the eh_frame FDE.  We need to use the actual byte offset
        // into the function when looking it up.

        if (m_range.GetBaseAddress().IsValid())
        {
            Address current_pc (m_range.GetBaseAddress ());
            if (current_offset != -1)
                current_pc.SetOffset (current_pc.GetOffset() + current_offset);

            DWARFCallFrameInfo *eh_frame = m_unwind_table.GetEHFrameInfo();
            if (eh_frame)
            {
                m_unwind_plan_call_site_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
                if (!eh_frame->GetUnwindPlan (current_pc, *m_unwind_plan_call_site_sp))
                    m_unwind_plan_call_site_sp.reset();
            }
        }
    }
    return m_unwind_plan_call_site_sp;
}

UnwindPlanSP
FuncUnwinders::GetUnwindPlanAtNonCallSite (Target& target, Thread& thread, int current_offset)
{
    // Lock the mutex to ensure we can always give out the most appropriate
    // information. We want to make sure if someone requests an unwind
    // plan, that they get one and don't run into a race condition where one
    // thread has started to create the unwind plan and has put it into 
    // the unique pointer member variable, and have another thread enter this function
    // and return the partially filled pointer contained in the unique pointer.
    // We also want to make sure that we lock out other unwind plans from
    // being accessed until this one is done creating itself in case someone
    // had some code like:
    //  UnwindPlan *best_unwind_plan = ...GetUnwindPlanAtCallSite (...)
    //  if (best_unwind_plan == NULL)
    //      best_unwind_plan = GetUnwindPlanAtNonCallSite (...)
    Mutex::Locker locker (m_mutex);
    if (m_tried_unwind_at_non_call_site == false && m_unwind_plan_non_call_site_sp.get() == nullptr)
    {
        UnwindAssemblySP assembly_profiler_sp (GetUnwindAssemblyProfiler());
        if (assembly_profiler_sp)
        {
            if (target.GetArchitecture().GetCore() == ArchSpec::eCore_x86_32_i386
                || target.GetArchitecture().GetCore() == ArchSpec::eCore_x86_64_x86_64
                || target.GetArchitecture().GetCore() == ArchSpec::eCore_x86_64_x86_64h)
            {
                // For 0th frame on i386 & x86_64, we fetch eh_frame and try using assembly profiler
                // to augment it into asynchronous unwind table.
                GetUnwindPlanAtCallSite(current_offset);
                if (m_unwind_plan_call_site_sp) {
                    UnwindPlan* plan = new UnwindPlan (*m_unwind_plan_call_site_sp);
                    if (assembly_profiler_sp->AugmentUnwindPlanFromCallSite (m_range, thread, *plan)) {
                        m_unwind_plan_non_call_site_sp.reset (plan);
                        return m_unwind_plan_non_call_site_sp;
                    }
                }
            }

            m_unwind_plan_non_call_site_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
            if (!assembly_profiler_sp->GetNonCallSiteUnwindPlanFromAssembly (m_range, thread, *m_unwind_plan_non_call_site_sp))
                m_unwind_plan_non_call_site_sp.reset();
        }
    }
    return m_unwind_plan_non_call_site_sp;
}

UnwindPlanSP
FuncUnwinders::GetUnwindPlanFastUnwind (Thread& thread)
{
    // Lock the mutex to ensure we can always give out the most appropriate
    // information. We want to make sure if someone requests an unwind
    // plan, that they get one and don't run into a race condition where one
    // thread has started to create the unwind plan and has put it into 
    // the unique pointer member variable, and have another thread enter this function
    // and return the partially filled pointer contained in the unique pointer.
    // We also want to make sure that we lock out other unwind plans from
    // being accessed until this one is done creating itself in case someone
    // had some code like:
    //  UnwindPlan *best_unwind_plan = ...GetUnwindPlanAtCallSite (...)
    //  if (best_unwind_plan == NULL)
    //      best_unwind_plan = GetUnwindPlanAtNonCallSite (...)
    Mutex::Locker locker (m_mutex);
    if (m_tried_unwind_fast == false && m_unwind_plan_fast_sp.get() == nullptr)
    {
        m_tried_unwind_fast = true;
        UnwindAssemblySP assembly_profiler_sp (GetUnwindAssemblyProfiler());
        if (assembly_profiler_sp)
        {
            m_unwind_plan_fast_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
            if (!assembly_profiler_sp->GetFastUnwindPlan (m_range, thread, *m_unwind_plan_fast_sp))
                m_unwind_plan_fast_sp.reset();
        }
    }
    return m_unwind_plan_fast_sp;
}

UnwindPlanSP
FuncUnwinders::GetUnwindPlanArchitectureDefault (Thread& thread)
{
    // Lock the mutex to ensure we can always give out the most appropriate
    // information. We want to make sure if someone requests an unwind
    // plan, that they get one and don't run into a race condition where one
    // thread has started to create the unwind plan and has put it into 
    // the unique pointer member variable, and have another thread enter this function
    // and return the partially filled pointer contained in the unique pointer.
    // We also want to make sure that we lock out other unwind plans from
    // being accessed until this one is done creating itself in case someone
    // had some code like:
    //  UnwindPlan *best_unwind_plan = ...GetUnwindPlanAtCallSite (...)
    //  if (best_unwind_plan == NULL)
    //      best_unwind_plan = GetUnwindPlanAtNonCallSite (...)
    Mutex::Locker locker (m_mutex);
    if (m_tried_unwind_arch_default == false && m_unwind_plan_arch_default_sp.get() == nullptr)
    {
        m_tried_unwind_arch_default = true;
        Address current_pc;
        ProcessSP process_sp (thread.CalculateProcess());
        if (process_sp)
        {
            ABI *abi = process_sp->GetABI().get();
            if (abi)
            {
                m_unwind_plan_arch_default_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
                if (m_unwind_plan_arch_default_sp)
                    abi->CreateDefaultUnwindPlan(*m_unwind_plan_arch_default_sp);
            }
        }
    }

    return m_unwind_plan_arch_default_sp;
}

UnwindPlanSP
FuncUnwinders::GetUnwindPlanArchitectureDefaultAtFunctionEntry (Thread& thread)
{
    // Lock the mutex to ensure we can always give out the most appropriate
    // information. We want to make sure if someone requests an unwind
    // plan, that they get one and don't run into a race condition where one
    // thread has started to create the unwind plan and has put it into 
    // the unique pointer member variable, and have another thread enter this function
    // and return the partially filled pointer contained in the unique pointer.
    // We also want to make sure that we lock out other unwind plans from
    // being accessed until this one is done creating itself in case someone
    // had some code like:
    //  UnwindPlan *best_unwind_plan = ...GetUnwindPlanAtCallSite (...)
    //  if (best_unwind_plan == NULL)
    //      best_unwind_plan = GetUnwindPlanAtNonCallSite (...)
    Mutex::Locker locker (m_mutex);
    if (m_tried_unwind_arch_default_at_func_entry == false && m_unwind_plan_arch_default_at_func_entry_sp.get() == nullptr)
    {
        m_tried_unwind_arch_default_at_func_entry = true;
        Address current_pc;
        ProcessSP process_sp (thread.CalculateProcess());
        if (process_sp)
        {
            ABI *abi = process_sp->GetABI().get();
            if (abi)
            {
                m_unwind_plan_arch_default_at_func_entry_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
                if (m_unwind_plan_arch_default_at_func_entry_sp)
                    abi->CreateFunctionEntryUnwindPlan(*m_unwind_plan_arch_default_at_func_entry_sp);
            }
        }
    }

    return m_unwind_plan_arch_default_at_func_entry_sp;
}


Address&
FuncUnwinders::GetFirstNonPrologueInsn (Target& target)
{
    if (m_first_non_prologue_insn.IsValid())
        return m_first_non_prologue_insn;
    ExecutionContext exe_ctx (target.shared_from_this(), false);
    UnwindAssemblySP assembly_profiler_sp (GetUnwindAssemblyProfiler());
    if (assembly_profiler_sp)
    if (assembly_profiler_sp)
        assembly_profiler_sp->FirstNonPrologueInsn (m_range, exe_ctx, m_first_non_prologue_insn);
    return m_first_non_prologue_insn;
}

const Address&
FuncUnwinders::GetFunctionStartAddress () const
{
    return m_range.GetBaseAddress();
}

void
FuncUnwinders::InvalidateNonCallSiteUnwindPlan (lldb_private::Thread& thread)
{
    UnwindPlanSP arch_default = GetUnwindPlanArchitectureDefault (thread);
    if (arch_default && m_tried_unwind_at_call_site)
    {
        m_unwind_plan_call_site_sp = arch_default;
    }
}

lldb::UnwindAssemblySP
FuncUnwinders::GetUnwindAssemblyProfiler ()
{
    UnwindAssemblySP assembly_profiler_sp;
    ArchSpec arch;
    if (m_unwind_table.GetArchitecture (arch))
    {
        assembly_profiler_sp = UnwindAssembly::FindPlugin (arch);
    }
    return assembly_profiler_sp;
}