Coverage Report

Created: 2023-09-21 18:56

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/UnwindAssembly/x86/UnwindAssembly-x86.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- UnwindAssembly-x86.cpp --------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "UnwindAssembly-x86.h"
10
#include "x86AssemblyInspectionEngine.h"
11
12
#include "llvm-c/Disassembler.h"
13
#include "llvm/ADT/STLExtras.h"
14
#include "llvm/Support/TargetSelect.h"
15
16
#include "lldb/Core/Address.h"
17
#include "lldb/Core/PluginManager.h"
18
#include "lldb/Symbol/UnwindPlan.h"
19
#include "lldb/Target/ABI.h"
20
#include "lldb/Target/ExecutionContext.h"
21
#include "lldb/Target/Process.h"
22
#include "lldb/Target/RegisterContext.h"
23
#include "lldb/Target/RegisterNumber.h"
24
#include "lldb/Target/Target.h"
25
#include "lldb/Target/Thread.h"
26
#include "lldb/Target/UnwindAssembly.h"
27
#include "lldb/Utility/ArchSpec.h"
28
#include "lldb/Utility/Status.h"
29
30
using namespace lldb;
31
using namespace lldb_private;
32
33
LLDB_PLUGIN_DEFINE_ADV(UnwindAssembly_x86, UnwindAssemblyX86)
34
35
//  UnwindAssemblyParser_x86 method definitions
36
37
UnwindAssembly_x86::UnwindAssembly_x86(const ArchSpec &arch)
38
16.7k
    : lldb_private::UnwindAssembly(arch),
39
16.7k
      m_assembly_inspection_engine(new x86AssemblyInspectionEngine(arch)) {}
40
41
16.7k
UnwindAssembly_x86::~UnwindAssembly_x86() {
42
16.7k
  delete m_assembly_inspection_engine;
43
16.7k
}
44
45
bool UnwindAssembly_x86::GetNonCallSiteUnwindPlanFromAssembly(
46
8.80k
    AddressRange &func, Thread &thread, UnwindPlan &unwind_plan) {
47
8.80k
  if (!func.GetBaseAddress().IsValid() || func.GetByteSize() == 0)
48
0
    return false;
49
8.80k
  if (m_assembly_inspection_engine == nullptr)
50
0
    return false;
51
8.80k
  ProcessSP process_sp(thread.GetProcess());
52
8.80k
  if (process_sp.get() == nullptr)
53
0
    return false;
54
8.80k
  std::vector<uint8_t> function_text(func.GetByteSize());
55
8.80k
  Status error;
56
8.80k
  if (process_sp->GetTarget().ReadMemory(
57
8.80k
          func.GetBaseAddress(), function_text.data(), func.GetByteSize(),
58
8.80k
          error) == func.GetByteSize()) {
59
8.77k
    RegisterContextSP reg_ctx(thread.GetRegisterContext());
60
8.77k
    m_assembly_inspection_engine->Initialize(reg_ctx);
61
8.77k
    return m_assembly_inspection_engine->GetNonCallSiteUnwindPlanFromAssembly(
62
8.77k
        function_text.data(), func.GetByteSize(), func, unwind_plan);
63
8.77k
  }
64
23
  return false;
65
8.80k
}
66
67
bool UnwindAssembly_x86::AugmentUnwindPlanFromCallSite(
68
21
    AddressRange &func, Thread &thread, UnwindPlan &unwind_plan) {
69
21
  bool do_augment_unwindplan = true;
70
71
21
  UnwindPlan::RowSP first_row = unwind_plan.GetRowForFunctionOffset(0);
72
21
  UnwindPlan::RowSP last_row = unwind_plan.GetRowForFunctionOffset(-1);
73
74
21
  int wordsize = 8;
75
21
  ProcessSP process_sp(thread.GetProcess());
76
21
  if (process_sp.get() == nullptr)
77
0
    return false;
78
79
21
  wordsize = process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
80
81
21
  RegisterNumber sp_regnum(thread, eRegisterKindGeneric,
82
21
                           LLDB_REGNUM_GENERIC_SP);
83
21
  RegisterNumber pc_regnum(thread, eRegisterKindGeneric,
84
21
                           LLDB_REGNUM_GENERIC_PC);
85
86
  // Does this UnwindPlan describe the prologue?  I want to see that the CFA is
87
  // set in terms of the stack pointer plus an offset, and I want to see that
88
  // rip is retrieved at the CFA-wordsize. If there is no description of the
89
  // prologue, don't try to augment this eh_frame unwinder code, fall back to
90
  // assembly parsing instead.
91
92
21
  if (first_row->GetCFAValue().GetValueType() !=
93
21
          UnwindPlan::Row::FAValue::isRegisterPlusOffset ||
94
21
      RegisterNumber(thread, unwind_plan.GetRegisterKind(),
95
21
                     first_row->GetCFAValue().GetRegisterNumber()) !=
96
21
          sp_regnum ||
97
21
      first_row->GetCFAValue().GetOffset() != wordsize) {
98
1
    return false;
99
1
  }
100
20
  UnwindPlan::Row::RegisterLocation first_row_pc_loc;
101
20
  if (!first_row->GetRegisterInfo(
102
20
          pc_regnum.GetAsKind(unwind_plan.GetRegisterKind()),
103
20
          first_row_pc_loc) ||
104
20
      !first_row_pc_loc.IsAtCFAPlusOffset() ||
105
20
      
first_row_pc_loc.GetOffset() != -wordsize19
) {
106
1
    return false;
107
1
  }
108
109
  // It looks like the prologue is described. Is the epilogue described?  If it
110
  // is, no need to do any augmentation.
111
112
19
  if (first_row != last_row &&
113
19
      
first_row->GetOffset() != last_row->GetOffset()16
) {
114
    // The first & last row have the same CFA register and the same CFA offset
115
    // value and the CFA register is esp/rsp (the stack pointer).
116
117
    // We're checking that both of them have an unwind rule like "CFA=esp+4" or
118
    // CFA+rsp+8".
119
120
16
    if (first_row->GetCFAValue().GetValueType() ==
121
16
            last_row->GetCFAValue().GetValueType() &&
122
16
        first_row->GetCFAValue().GetRegisterNumber() ==
123
16
            last_row->GetCFAValue().GetRegisterNumber() &&
124
16
        first_row->GetCFAValue().GetOffset() ==
125
11
            last_row->GetCFAValue().GetOffset()) {
126
      // Get the register locations for eip/rip from the first & last rows. Are
127
      // they both CFA plus an offset?  Is it the same offset?
128
129
11
      UnwindPlan::Row::RegisterLocation last_row_pc_loc;
130
11
      if (last_row->GetRegisterInfo(
131
11
              pc_regnum.GetAsKind(unwind_plan.GetRegisterKind()),
132
11
              last_row_pc_loc)) {
133
11
        if (last_row_pc_loc.IsAtCFAPlusOffset() &&
134
11
            first_row_pc_loc.GetOffset() == last_row_pc_loc.GetOffset()) {
135
136
          // One last sanity check:  Is the unwind rule for getting the caller
137
          // pc value "deref the CFA-4" or "deref the CFA-8"?
138
139
          // If so, we have an UnwindPlan that already describes the epilogue
140
          // and we don't need to modify it at all.
141
142
11
          if (first_row_pc_loc.GetOffset() == -wordsize) {
143
11
            return true;
144
11
          }
145
11
        }
146
11
      }
147
11
    }
148
16
  }
149
150
8
  if (do_augment_unwindplan) {
151
8
    if (!func.GetBaseAddress().IsValid() || func.GetByteSize() == 0)
152
0
      return false;
153
8
    if (m_assembly_inspection_engine == nullptr)
154
0
      return false;
155
8
    std::vector<uint8_t> function_text(func.GetByteSize());
156
8
    Status error;
157
8
    if (process_sp->GetTarget().ReadMemory(
158
8
            func.GetBaseAddress(), function_text.data(), func.GetByteSize(),
159
8
            error) == func.GetByteSize()) {
160
8
      RegisterContextSP reg_ctx(thread.GetRegisterContext());
161
8
      m_assembly_inspection_engine->Initialize(reg_ctx);
162
8
      return m_assembly_inspection_engine->AugmentUnwindPlanFromCallSite(
163
8
          function_text.data(), func.GetByteSize(), func, unwind_plan, reg_ctx);
164
8
    }
165
8
  }
166
167
0
  return false;
168
8
}
169
170
bool UnwindAssembly_x86::GetFastUnwindPlan(AddressRange &func, Thread &thread,
171
7.97k
                                           UnwindPlan &unwind_plan) {
172
  // if prologue is
173
  //   55     pushl %ebp
174
  //   89 e5  movl %esp, %ebp
175
  //  or
176
  //   55        pushq %rbp
177
  //   48 89 e5  movq %rsp, %rbp
178
179
  // We should pull in the ABI architecture default unwind plan and return that
180
181
7.97k
  llvm::SmallVector<uint8_t, 4> opcode_data;
182
183
7.97k
  ProcessSP process_sp = thread.GetProcess();
184
7.97k
  if (process_sp) {
185
7.97k
    Target &target(process_sp->GetTarget());
186
7.97k
    Status error;
187
7.97k
    if (target.ReadMemory(func.GetBaseAddress(), opcode_data.data(), 4,
188
7.97k
                          error) == 4) {
189
7.94k
      uint8_t i386_push_mov[] = {0x55, 0x89, 0xe5};
190
7.94k
      uint8_t x86_64_push_mov[] = {0x55, 0x48, 0x89, 0xe5};
191
192
7.94k
      if (memcmp(opcode_data.data(), i386_push_mov, sizeof(i386_push_mov)) ==
193
7.94k
              0 ||
194
7.94k
          memcmp(opcode_data.data(), x86_64_push_mov,
195
7.94k
                 sizeof(x86_64_push_mov)) == 0) {
196
5.96k
        ABISP abi_sp = process_sp->GetABI();
197
5.96k
        if (abi_sp) {
198
5.96k
          return abi_sp->CreateDefaultUnwindPlan(unwind_plan);
199
5.96k
        }
200
5.96k
      }
201
7.94k
    }
202
7.97k
  }
203
2.00k
  return false;
204
7.97k
}
205
206
bool UnwindAssembly_x86::FirstNonPrologueInsn(
207
    AddressRange &func, const ExecutionContext &exe_ctx,
208
0
    Address &first_non_prologue_insn) {
209
210
0
  if (!func.GetBaseAddress().IsValid())
211
0
    return false;
212
213
0
  Target *target = exe_ctx.GetTargetPtr();
214
0
  if (target == nullptr)
215
0
    return false;
216
217
0
  if (m_assembly_inspection_engine == nullptr)
218
0
    return false;
219
220
0
  std::vector<uint8_t> function_text(func.GetByteSize());
221
0
  Status error;
222
0
  if (target->ReadMemory(func.GetBaseAddress(), function_text.data(),
223
0
                         func.GetByteSize(), error) == func.GetByteSize()) {
224
0
    size_t offset;
225
0
    if (m_assembly_inspection_engine->FindFirstNonPrologueInstruction(
226
0
            function_text.data(), func.GetByteSize(), offset)) {
227
0
      first_non_prologue_insn = func.GetBaseAddress();
228
0
      first_non_prologue_insn.Slide(offset);
229
0
    }
230
0
    return true;
231
0
  }
232
0
  return false;
233
0
}
234
235
16.7k
UnwindAssembly *UnwindAssembly_x86::CreateInstance(const ArchSpec &arch) {
236
16.7k
  const llvm::Triple::ArchType cpu = arch.GetMachine();
237
16.7k
  if (cpu == llvm::Triple::x86 || 
cpu == llvm::Triple::x86_6416.7k
)
238
16.7k
    return new UnwindAssembly_x86(arch);
239
0
  return nullptr;
240
16.7k
}
241
242
3.92k
void UnwindAssembly_x86::Initialize() {
243
3.92k
  PluginManager::RegisterPlugin(GetPluginNameStatic(),
244
3.92k
                                GetPluginDescriptionStatic(), CreateInstance);
245
3.92k
}
246
247
3.92k
void UnwindAssembly_x86::Terminate() {
248
3.92k
  PluginManager::UnregisterPlugin(CreateInstance);
249
3.92k
}
250
251
3.92k
llvm::StringRef UnwindAssembly_x86::GetPluginDescriptionStatic() {
252
3.92k
  return "i386 and x86_64 assembly language profiler plugin.";
253
3.92k
}