| Greg Clayton | 078daac | 2011-04-25 21:07:40 +0000 | [diff] [blame] | 1 | //===-- UnwindAssembly-x86.cpp ----------------------------------*- C++ -*-===// |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 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 | |
| Greg Clayton | 078daac | 2011-04-25 21:07:40 +0000 | [diff] [blame] | 10 | #include "UnwindAssembly-x86.h" |
| Jason Molenda | 74b8fbc | 2016-09-29 01:00:16 +0000 | [diff] [blame] | 11 | #include "x86AssemblyInspectionEngine.h" |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 12 | |
| Jason Molenda | a8399a8 | 2012-10-10 01:45:33 +0000 | [diff] [blame] | 13 | #include "llvm-c/Disassembler.h" |
| Zachary Turner | af0f45f | 2015-03-03 21:05:17 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/STLExtras.h" |
| Johnny Chen | 62212f0 | 2011-11-29 01:09:49 +0000 | [diff] [blame] | 15 | #include "llvm/Support/TargetSelect.h" |
| Greg Clayton | dc5eb69 | 2011-04-25 18:36:36 +0000 | [diff] [blame] | 16 | |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Address.h" |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 18 | #include "lldb/Core/PluginManager.h" |
| Greg Clayton | dc5eb69 | 2011-04-25 18:36:36 +0000 | [diff] [blame] | 19 | #include "lldb/Symbol/UnwindPlan.h" |
| Zachary Turner | 32abc6e | 2015-03-03 19:23:09 +0000 | [diff] [blame] | 20 | #include "lldb/Target/ABI.h" |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 21 | #include "lldb/Target/ExecutionContext.h" |
| 22 | #include "lldb/Target/Process.h" |
| 23 | #include "lldb/Target/RegisterContext.h" |
| Zachary Turner | 01c3243 | 2017-02-14 19:06:07 +0000 | [diff] [blame] | 24 | #include "lldb/Target/RegisterNumber.h" |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 25 | #include "lldb/Target/Target.h" |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 26 | #include "lldb/Target/Thread.h" |
| Greg Clayton | 7be2542 | 2011-04-25 21:14:26 +0000 | [diff] [blame] | 27 | #include "lldb/Target/UnwindAssembly.h" |
| Pavel Labath | 5f19b90 | 2017-11-13 16:16:33 +0000 | [diff] [blame] | 28 | #include "lldb/Utility/ArchSpec.h" |
| Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 29 | #include "lldb/Utility/Status.h" |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace lldb; |
| 32 | using namespace lldb_private; |
| 33 | |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 34 | //----------------------------------------------------------------------------------------------- |
| Jason Molenda | 7b967f1 | 2014-08-25 23:46:06 +0000 | [diff] [blame] | 35 | // UnwindAssemblyParser_x86 method definitions |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 36 | //----------------------------------------------------------------------------------------------- |
| 37 | |
| Jason Molenda | 74b8fbc | 2016-09-29 01:00:16 +0000 | [diff] [blame] | 38 | UnwindAssembly_x86::UnwindAssembly_x86(const ArchSpec &arch) |
| 39 | : lldb_private::UnwindAssembly(arch), |
| 40 | m_assembly_inspection_engine(new x86AssemblyInspectionEngine(arch)) {} |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | |
| Jason Molenda | 74b8fbc | 2016-09-29 01:00:16 +0000 | [diff] [blame] | 42 | UnwindAssembly_x86::~UnwindAssembly_x86() { |
| 43 | delete m_assembly_inspection_engine; |
| 44 | } |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 45 | |
| 46 | bool UnwindAssembly_x86::GetNonCallSiteUnwindPlanFromAssembly( |
| 47 | AddressRange &func, Thread &thread, UnwindPlan &unwind_plan) { |
| Jason Molenda | 74b8fbc | 2016-09-29 01:00:16 +0000 | [diff] [blame] | 48 | if (!func.GetBaseAddress().IsValid() || func.GetByteSize() == 0) |
| 49 | return false; |
| 50 | if (m_assembly_inspection_engine == nullptr) |
| 51 | return false; |
| 52 | ProcessSP process_sp(thread.GetProcess()); |
| 53 | if (process_sp.get() == nullptr) |
| 54 | return false; |
| 55 | const bool prefer_file_cache = true; |
| 56 | std::vector<uint8_t> function_text(func.GetByteSize()); |
| Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 57 | Status error; |
| Jason Molenda | 74b8fbc | 2016-09-29 01:00:16 +0000 | [diff] [blame] | 58 | if (process_sp->GetTarget().ReadMemory( |
| 59 | func.GetBaseAddress(), prefer_file_cache, function_text.data(), |
| 60 | func.GetByteSize(), error) == func.GetByteSize()) { |
| 61 | RegisterContextSP reg_ctx(thread.GetRegisterContext()); |
| 62 | m_assembly_inspection_engine->Initialize(reg_ctx); |
| 63 | return m_assembly_inspection_engine->GetNonCallSiteUnwindPlanFromAssembly( |
| 64 | function_text.data(), func.GetByteSize(), func, unwind_plan); |
| 65 | } |
| 66 | return false; |
| Greg Clayton | 2ed751b | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | bool UnwindAssembly_x86::AugmentUnwindPlanFromCallSite( |
| 70 | AddressRange &func, Thread &thread, UnwindPlan &unwind_plan) { |
| 71 | bool do_augment_unwindplan = true; |
| Greg Clayton | 2ed751b | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 72 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | UnwindPlan::RowSP first_row = unwind_plan.GetRowForFunctionOffset(0); |
| 74 | UnwindPlan::RowSP last_row = unwind_plan.GetRowForFunctionOffset(-1); |
| Greg Clayton | 2ed751b | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 75 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | int wordsize = 8; |
| 77 | ProcessSP process_sp(thread.GetProcess()); |
| Jason Molenda | 74b8fbc | 2016-09-29 01:00:16 +0000 | [diff] [blame] | 78 | if (process_sp.get() == nullptr) |
| 79 | return false; |
| 80 | |
| 81 | wordsize = process_sp->GetTarget().GetArchitecture().GetAddressByteSize(); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 82 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 83 | RegisterNumber sp_regnum(thread, eRegisterKindGeneric, |
| 84 | LLDB_REGNUM_GENERIC_SP); |
| 85 | RegisterNumber pc_regnum(thread, eRegisterKindGeneric, |
| 86 | LLDB_REGNUM_GENERIC_PC); |
| Jason Molenda | a056771 | 2015-01-10 04:01:03 +0000 | [diff] [blame] | 87 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 88 | // Does this UnwindPlan describe the prologue? I want to see that the CFA is |
| Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 89 | // set in terms of the stack pointer plus an offset, and I want to see that |
| 90 | // rip is retrieved at the CFA-wordsize. If there is no description of the |
| 91 | // prologue, don't try to augment this eh_frame unwinder code, fall back to |
| 92 | // assembly parsing instead. |
| Jason Molenda | a056771 | 2015-01-10 04:01:03 +0000 | [diff] [blame] | 93 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 94 | if (first_row->GetCFAValue().GetValueType() != |
| Aleksandr Urakov | 4538ed3 | 2018-10-30 10:07:08 +0000 | [diff] [blame] | 95 | UnwindPlan::Row::FAValue::isRegisterPlusOffset || |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | RegisterNumber(thread, unwind_plan.GetRegisterKind(), |
| 97 | first_row->GetCFAValue().GetRegisterNumber()) != |
| 98 | sp_regnum || |
| 99 | first_row->GetCFAValue().GetOffset() != wordsize) { |
| Jason Molenda | a056771 | 2015-01-10 04:01:03 +0000 | [diff] [blame] | 100 | return false; |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | } |
| 102 | UnwindPlan::Row::RegisterLocation first_row_pc_loc; |
| 103 | if (first_row->GetRegisterInfo( |
| 104 | pc_regnum.GetAsKind(unwind_plan.GetRegisterKind()), |
| 105 | first_row_pc_loc) == false || |
| 106 | first_row_pc_loc.IsAtCFAPlusOffset() == false || |
| 107 | first_row_pc_loc.GetOffset() != -wordsize) { |
| Jason Molenda | 5c45c54 | 2014-12-21 10:44:54 +0000 | [diff] [blame] | 108 | return false; |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | } |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 110 | |
| Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 111 | // It looks like the prologue is described. Is the epilogue described? If it |
| 112 | // is, no need to do any augmentation. |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 113 | |
| 114 | if (first_row != last_row && |
| 115 | first_row->GetOffset() != last_row->GetOffset()) { |
| Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 116 | // The first & last row have the same CFA register and the same CFA offset |
| 117 | // value and the CFA register is esp/rsp (the stack pointer). |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | |
| 119 | // We're checking that both of them have an unwind rule like "CFA=esp+4" or |
| 120 | // CFA+rsp+8". |
| 121 | |
| 122 | if (first_row->GetCFAValue().GetValueType() == |
| 123 | last_row->GetCFAValue().GetValueType() && |
| 124 | first_row->GetCFAValue().GetRegisterNumber() == |
| 125 | last_row->GetCFAValue().GetRegisterNumber() && |
| 126 | first_row->GetCFAValue().GetOffset() == |
| 127 | last_row->GetCFAValue().GetOffset()) { |
| Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 128 | // Get the register locations for eip/rip from the first & last rows. Are |
| 129 | // they both CFA plus an offset? Is it the same offset? |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 130 | |
| 131 | UnwindPlan::Row::RegisterLocation last_row_pc_loc; |
| 132 | if (last_row->GetRegisterInfo( |
| 133 | pc_regnum.GetAsKind(unwind_plan.GetRegisterKind()), |
| 134 | last_row_pc_loc)) { |
| 135 | if (last_row_pc_loc.IsAtCFAPlusOffset() && |
| 136 | first_row_pc_loc.GetOffset() == last_row_pc_loc.GetOffset()) { |
| 137 | |
| 138 | // One last sanity check: Is the unwind rule for getting the caller |
| Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 139 | // pc value "deref the CFA-4" or "deref the CFA-8"? |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 140 | |
| 141 | // If so, we have an UnwindPlan that already describes the epilogue |
| Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 142 | // and we don't need to modify it at all. |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 143 | |
| 144 | if (first_row_pc_loc.GetOffset() == -wordsize) { |
| 145 | do_augment_unwindplan = false; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (do_augment_unwindplan) { |
| Jason Molenda | 74b8fbc | 2016-09-29 01:00:16 +0000 | [diff] [blame] | 153 | if (!func.GetBaseAddress().IsValid() || func.GetByteSize() == 0) |
| 154 | return false; |
| 155 | if (m_assembly_inspection_engine == nullptr) |
| 156 | return false; |
| 157 | const bool prefer_file_cache = true; |
| 158 | std::vector<uint8_t> function_text(func.GetByteSize()); |
| Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 159 | Status error; |
| Jason Molenda | 74b8fbc | 2016-09-29 01:00:16 +0000 | [diff] [blame] | 160 | if (process_sp->GetTarget().ReadMemory( |
| 161 | func.GetBaseAddress(), prefer_file_cache, function_text.data(), |
| 162 | func.GetByteSize(), error) == func.GetByteSize()) { |
| 163 | RegisterContextSP reg_ctx(thread.GetRegisterContext()); |
| 164 | m_assembly_inspection_engine->Initialize(reg_ctx); |
| 165 | return m_assembly_inspection_engine->AugmentUnwindPlanFromCallSite( |
| 166 | function_text.data(), func.GetByteSize(), func, unwind_plan, reg_ctx); |
| 167 | } |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | return false; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 173 | bool UnwindAssembly_x86::GetFastUnwindPlan(AddressRange &func, Thread &thread, |
| 174 | UnwindPlan &unwind_plan) { |
| 175 | // if prologue is |
| 176 | // 55 pushl %ebp |
| 177 | // 89 e5 movl %esp, %ebp |
| 178 | // or |
| 179 | // 55 pushq %rbp |
| 180 | // 48 89 e5 movq %rsp, %rbp |
| 181 | |
| 182 | // We should pull in the ABI architecture default unwind plan and return that |
| 183 | |
| 184 | llvm::SmallVector<uint8_t, 4> opcode_data; |
| 185 | |
| 186 | ProcessSP process_sp = thread.GetProcess(); |
| 187 | if (process_sp) { |
| 188 | Target &target(process_sp->GetTarget()); |
| 189 | const bool prefer_file_cache = true; |
| Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 190 | Status error; |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 191 | if (target.ReadMemory(func.GetBaseAddress(), prefer_file_cache, |
| 192 | opcode_data.data(), 4, error) == 4) { |
| 193 | uint8_t i386_push_mov[] = {0x55, 0x89, 0xe5}; |
| 194 | uint8_t x86_64_push_mov[] = {0x55, 0x48, 0x89, 0xe5}; |
| 195 | |
| 196 | if (memcmp(opcode_data.data(), i386_push_mov, sizeof(i386_push_mov)) == |
| 197 | 0 || |
| 198 | memcmp(opcode_data.data(), x86_64_push_mov, |
| 199 | sizeof(x86_64_push_mov)) == 0) { |
| 200 | ABISP abi_sp = process_sp->GetABI(); |
| 201 | if (abi_sp) { |
| 202 | return abi_sp->CreateDefaultUnwindPlan(unwind_plan); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | return false; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 210 | bool UnwindAssembly_x86::FirstNonPrologueInsn( |
| 211 | AddressRange &func, const ExecutionContext &exe_ctx, |
| 212 | Address &first_non_prologue_insn) { |
| Jason Molenda | 74b8fbc | 2016-09-29 01:00:16 +0000 | [diff] [blame] | 213 | |
| 214 | if (!func.GetBaseAddress().IsValid()) |
| 215 | return false; |
| 216 | |
| 217 | Target *target = exe_ctx.GetTargetPtr(); |
| 218 | if (target == nullptr) |
| 219 | return false; |
| 220 | |
| 221 | if (m_assembly_inspection_engine == nullptr) |
| 222 | return false; |
| 223 | |
| 224 | const bool prefer_file_cache = true; |
| 225 | std::vector<uint8_t> function_text(func.GetByteSize()); |
| Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 226 | Status error; |
| Jason Molenda | 74b8fbc | 2016-09-29 01:00:16 +0000 | [diff] [blame] | 227 | if (target->ReadMemory(func.GetBaseAddress(), prefer_file_cache, |
| 228 | function_text.data(), func.GetByteSize(), |
| 229 | error) == func.GetByteSize()) { |
| 230 | size_t offset; |
| 231 | if (m_assembly_inspection_engine->FindFirstNonPrologueInstruction( |
| 232 | function_text.data(), func.GetByteSize(), offset)) { |
| 233 | first_non_prologue_insn = func.GetBaseAddress(); |
| 234 | first_non_prologue_insn.Slide(offset); |
| 235 | } |
| 236 | return true; |
| 237 | } |
| 238 | return false; |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | UnwindAssembly *UnwindAssembly_x86::CreateInstance(const ArchSpec &arch) { |
| 242 | const llvm::Triple::ArchType cpu = arch.GetMachine(); |
| Jason Molenda | 74b8fbc | 2016-09-29 01:00:16 +0000 | [diff] [blame] | 243 | if (cpu == llvm::Triple::x86 || cpu == llvm::Triple::x86_64) |
| 244 | return new UnwindAssembly_x86(arch); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 245 | return NULL; |
| 246 | } |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 247 | |
| 248 | //------------------------------------------------------------------ |
| 249 | // PluginInterface protocol in UnwindAssemblyParser_x86 |
| 250 | //------------------------------------------------------------------ |
| 251 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 252 | ConstString UnwindAssembly_x86::GetPluginName() { |
| 253 | return GetPluginNameStatic(); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 256 | uint32_t UnwindAssembly_x86::GetPluginVersion() { return 1; } |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 257 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 258 | void UnwindAssembly_x86::Initialize() { |
| 259 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 260 | GetPluginDescriptionStatic(), CreateInstance); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 263 | void UnwindAssembly_x86::Terminate() { |
| 264 | PluginManager::UnregisterPlugin(CreateInstance); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 267 | lldb_private::ConstString UnwindAssembly_x86::GetPluginNameStatic() { |
| 268 | static ConstString g_name("x86"); |
| 269 | return g_name; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 272 | const char *UnwindAssembly_x86::GetPluginDescriptionStatic() { |
| 273 | return "i386 and x86_64 assembly language profiler plugin."; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 274 | } |