Author: Jason Molenda Date: 2025-07-02T10:21:38-07:00 New Revision: dfcef35ff1d30d112362645ec2cd0d5e99952b0f
URL: https://github.com/llvm/llvm-project/commit/dfcef35ff1d30d112362645ec2cd0d5e99952b0f DIFF: https://github.com/llvm/llvm-project/commit/dfcef35ff1d30d112362645ec2cd0d5e99952b0f.diff LOG: [lldb][NFC][MachO] Clean up LC_THREAD reading code, remove i386 corefile (#146480) While fixing bugs in the x86_64 LC_THREAD parser in ObjectFileMachO, I noticed that the other LC_THREAD parsers are all less clear than they should be. To recap, a Mach-O LC_THREAD load command has a byte size for the entire payload. Within the payload, there will be one or more register sets provided. A register set starts with a UInt32 "flavor", the type of register set defined in the system headers, and a UInt32 "count", the number of UInt32 words of memory for this register set. After one register set, there may be additional sets. A parser can skip an unknown register set flavor by using the count field to get to the next register set. When the total byte size of the LC_THREAD load command has been parsed, it is completed. This patch fixes the riscv/arm/arm64 LC_THREAD parsers to use the total byte size as the exit condition, and to skip past unrecognized register sets, instead of stopping parsing. Instead of fixing the i386 corefile support, I removed it. The last macOS that supported 32-bit Intel code was macOS 10.14 in 2018. I also removed i386 KDP support, 32-bit intel kernel debugging hasn't been supported for even longer than that. It would be preferable to do these things separately, but I couldn't bring myself to update the i386 LC_THREAD parser, and it required very few changes to remove this support entirely. Added: Modified: lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp lldb/source/Plugins/Process/MacOSX-Kernel/CMakeLists.txt lldb/source/Plugins/Process/MacOSX-Kernel/ThreadKDP.cpp lldb/source/Plugins/Process/Utility/CMakeLists.txt llvm/utils/gn/secondary/lldb/source/Plugins/Process/Utility/BUILD.gn Removed: lldb/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_i386.cpp lldb/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_i386.h lldb/source/Plugins/Process/Utility/RegisterContextDarwin_i386.cpp lldb/source/Plugins/Process/Utility/RegisterContextDarwin_i386.h lldb/source/Plugins/Process/Utility/RegisterContextMach_i386.cpp lldb/source/Plugins/Process/Utility/RegisterContextMach_i386.h ################################################################################ diff --git a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp index 70f954cd5413f..dfff473ac1777 100644 --- a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp +++ b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp @@ -11,7 +11,6 @@ #include "Plugins/Process/Utility/RegisterContextDarwin_arm.h" #include "Plugins/Process/Utility/RegisterContextDarwin_arm64.h" -#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h" #include "Plugins/Process/Utility/RegisterContextDarwin_riscv32.h" #include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h" #include "lldb/Core/Debugger.h" @@ -81,9 +80,6 @@ #ifdef CPU_TYPE_ARM64_32 #undef CPU_TYPE_ARM64_32 #endif -#ifdef CPU_TYPE_I386 -#undef CPU_TYPE_I386 -#endif #ifdef CPU_TYPE_X86_64 #undef CPU_TYPE_X86_64 #endif @@ -358,122 +354,6 @@ class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64 { } }; -class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386 { -public: - RegisterContextDarwin_i386_Mach(lldb_private::Thread &thread, - const DataExtractor &data) - : RegisterContextDarwin_i386(thread, 0) { - SetRegisterDataFrom_LC_THREAD(data); - } - - void InvalidateAllRegisters() override { - // Do nothing... registers are always valid... - } - - void SetRegisterDataFrom_LC_THREAD(const DataExtractor &data) { - lldb::offset_t offset = 0; - SetError(GPRRegSet, Read, -1); - SetError(FPURegSet, Read, -1); - SetError(EXCRegSet, Read, -1); - bool done = false; - - while (!done) { - int flavor = data.GetU32(&offset); - if (flavor == 0) - done = true; - else { - uint32_t i; - uint32_t count = data.GetU32(&offset); - switch (flavor) { - case GPRRegSet: - for (i = 0; i < count; ++i) - (&gpr.eax)[i] = data.GetU32(&offset); - SetError(GPRRegSet, Read, 0); - done = true; - - break; - case FPURegSet: - // TODO: fill in FPU regs.... - // SetError (FPURegSet, Read, -1); - done = true; - - break; - case EXCRegSet: - exc.trapno = data.GetU32(&offset); - exc.err = data.GetU32(&offset); - exc.faultvaddr = data.GetU32(&offset); - SetError(EXCRegSet, Read, 0); - done = true; - break; - case 7: - case 8: - case 9: - // fancy flavors that encapsulate of the above flavors... - break; - - default: - done = true; - break; - } - } - } - } - - static bool Create_LC_THREAD(Thread *thread, Stream &data) { - RegisterContextSP reg_ctx_sp(thread->GetRegisterContext()); - if (reg_ctx_sp) { - RegisterContext *reg_ctx = reg_ctx_sp.get(); - - data.PutHex32(GPRRegSet); // Flavor - data.PutHex32(GPRWordCount); - PrintRegisterValue(reg_ctx, "eax", nullptr, 4, data); - PrintRegisterValue(reg_ctx, "ebx", nullptr, 4, data); - PrintRegisterValue(reg_ctx, "ecx", nullptr, 4, data); - PrintRegisterValue(reg_ctx, "edx", nullptr, 4, data); - PrintRegisterValue(reg_ctx, "edi", nullptr, 4, data); - PrintRegisterValue(reg_ctx, "esi", nullptr, 4, data); - PrintRegisterValue(reg_ctx, "ebp", nullptr, 4, data); - PrintRegisterValue(reg_ctx, "esp", nullptr, 4, data); - PrintRegisterValue(reg_ctx, "ss", nullptr, 4, data); - PrintRegisterValue(reg_ctx, "eflags", nullptr, 4, data); - PrintRegisterValue(reg_ctx, "eip", nullptr, 4, data); - PrintRegisterValue(reg_ctx, "cs", nullptr, 4, data); - PrintRegisterValue(reg_ctx, "ds", nullptr, 4, data); - PrintRegisterValue(reg_ctx, "es", nullptr, 4, data); - PrintRegisterValue(reg_ctx, "fs", nullptr, 4, data); - PrintRegisterValue(reg_ctx, "gs", nullptr, 4, data); - - // Write out the EXC registers - data.PutHex32(EXCRegSet); - data.PutHex32(EXCWordCount); - PrintRegisterValue(reg_ctx, "trapno", nullptr, 4, data); - PrintRegisterValue(reg_ctx, "err", nullptr, 4, data); - PrintRegisterValue(reg_ctx, "faultvaddr", nullptr, 4, data); - return true; - } - return false; - } - -protected: - int DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override { return 0; } - - int DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override { return 0; } - - int DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override { return 0; } - - int DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override { - return 0; - } - - int DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override { - return 0; - } - - int DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override { - return 0; - } -}; - class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm { public: RegisterContextDarwin_arm_Mach(lldb_private::Thread &thread, @@ -491,12 +371,11 @@ class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm { SetError(GPRRegSet, Read, -1); SetError(FPURegSet, Read, -1); SetError(EXCRegSet, Read, -1); - bool done = false; - while (!done) { + while (offset < data.GetByteSize()) { int flavor = data.GetU32(&offset); uint32_t count = data.GetU32(&offset); - lldb::offset_t next_thread_state = offset + (count * 4); + offset_t next_thread_state = offset + (count * 4); switch (flavor) { case GPRAltRegSet: case GPRRegSet: { @@ -510,9 +389,7 @@ class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm { SetError(GPRRegSet, Read, 0); } - } - offset = next_thread_state; - break; + } break; case FPURegSet: { uint8_t *fpu_reg_buf = (uint8_t *)&fpu.floats; @@ -522,12 +399,8 @@ class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm { offset += fpu_reg_buf_size; fpu.fpscr = data.GetU32(&offset); SetError(FPURegSet, Read, 0); - } else { - done = true; } - } - offset = next_thread_state; - break; + } break; case EXCRegSet: if (count == 3) { @@ -536,14 +409,11 @@ class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm { exc.far = data.GetU32(&offset); SetError(EXCRegSet, Read, 0); } - done = true; - offset = next_thread_state; break; - // Unknown register set flavor, stop trying to parse. default: - done = true; } + offset = next_thread_state; } } @@ -626,11 +496,10 @@ class RegisterContextDarwin_arm64_Mach : public RegisterContextDarwin_arm64 { SetError(GPRRegSet, Read, -1); SetError(FPURegSet, Read, -1); SetError(EXCRegSet, Read, -1); - bool done = false; - while (!done) { + while (offset < data.GetByteSize()) { int flavor = data.GetU32(&offset); uint32_t count = data.GetU32(&offset); - lldb::offset_t next_thread_state = offset + (count * 4); + offset_t next_thread_state = offset + (count * 4); switch (flavor) { case GPRRegSet: // x0-x29 + fp + lr + sp + pc (== 33 64-bit registers) plus cpsr (1 @@ -645,7 +514,6 @@ class RegisterContextDarwin_arm64_Mach : public RegisterContextDarwin_arm64 { gpr.cpsr = data.GetU32(&offset); SetError(GPRRegSet, Read, 0); } - offset = next_thread_state; break; case FPURegSet: { uint8_t *fpu_reg_buf = (uint8_t *)&fpu.v[0]; @@ -654,12 +522,8 @@ class RegisterContextDarwin_arm64_Mach : public RegisterContextDarwin_arm64 { data.ExtractBytes(offset, fpu_reg_buf_size, eByteOrderLittle, fpu_reg_buf) == fpu_reg_buf_size) { SetError(FPURegSet, Read, 0); - } else { - done = true; } - } - offset = next_thread_state; - break; + } break; case EXCRegSet: if (count == 4) { exc.far = data.GetU64(&offset); @@ -667,12 +531,10 @@ class RegisterContextDarwin_arm64_Mach : public RegisterContextDarwin_arm64 { exc.exception = data.GetU32(&offset); SetError(EXCRegSet, Read, 0); } - offset = next_thread_state; break; default: - done = true; - break; } + offset = next_thread_state; } } @@ -775,11 +637,10 @@ class RegisterContextDarwin_riscv32_Mach SetError(FPURegSet, Read, -1); SetError(EXCRegSet, Read, -1); SetError(CSRRegSet, Read, -1); - bool done = false; - while (!done) { + while (offset < data.GetByteSize()) { int flavor = data.GetU32(&offset); uint32_t count = data.GetU32(&offset); - lldb::offset_t next_thread_state = offset + (count * 4); + offset_t next_thread_state = offset + (count * 4); switch (flavor) { case GPRRegSet: // x0-x31 + pc @@ -789,7 +650,6 @@ class RegisterContextDarwin_riscv32_Mach gpr.pc = data.GetU32(&offset); SetError(GPRRegSet, Read, 0); } - offset = next_thread_state; break; case FPURegSet: { // f0-f31 + fcsr @@ -799,9 +659,7 @@ class RegisterContextDarwin_riscv32_Mach fpr.fcsr = data.GetU32(&offset); SetError(FPURegSet, Read, 0); } - } - offset = next_thread_state; - break; + } break; case EXCRegSet: if (count == 3) { exc.exception = data.GetU32(&offset); @@ -809,12 +667,10 @@ class RegisterContextDarwin_riscv32_Mach exc.far = data.GetU32(&offset); SetError(EXCRegSet, Read, 0); } - offset = next_thread_state; break; default: - done = true; - break; } + offset = next_thread_state; } } @@ -5408,16 +5264,6 @@ lldb_private::Address ObjectFileMachO::GetEntryPointAddress() { done = true; } break; - case llvm::MachO::CPU_TYPE_I386: - if (flavor == - 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h - { - offset += 40; // This is the offset of eip in the GPR thread state - // data structure. - start_address = m_data.GetU32(&offset); - done = true; - } - break; case llvm::MachO::CPU_TYPE_X86_64: if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h @@ -5897,11 +5743,6 @@ ObjectFileMachO::GetThreadContextAtIndex(uint32_t idx, std::make_shared<RegisterContextDarwin_arm_Mach>(thread, data); break; - case llvm::MachO::CPU_TYPE_I386: - reg_ctx_sp = - std::make_shared<RegisterContextDarwin_i386_Mach>(thread, data); - break; - case llvm::MachO::CPU_TYPE_X86_64: reg_ctx_sp = std::make_shared<RegisterContextDarwin_x86_64_Mach>(thread, data); @@ -6769,11 +6610,6 @@ bool ObjectFileMachO::SaveCore(const lldb::ProcessSP &process_sp, thread_sp.get(), LC_THREAD_datas[thread_idx]); break; - case llvm::MachO::CPU_TYPE_I386: - RegisterContextDarwin_i386_Mach::Create_LC_THREAD( - thread_sp.get(), LC_THREAD_datas[thread_idx]); - break; - case llvm::MachO::CPU_TYPE_X86_64: RegisterContextDarwin_x86_64_Mach::Create_LC_THREAD( thread_sp.get(), LC_THREAD_datas[thread_idx]); diff --git a/lldb/source/Plugins/Process/MacOSX-Kernel/CMakeLists.txt b/lldb/source/Plugins/Process/MacOSX-Kernel/CMakeLists.txt index ddce25c62046a..f26e14f580cb7 100644 --- a/lldb/source/Plugins/Process/MacOSX-Kernel/CMakeLists.txt +++ b/lldb/source/Plugins/Process/MacOSX-Kernel/CMakeLists.txt @@ -12,7 +12,6 @@ add_lldb_library(lldbPluginProcessMacOSXKernel PLUGIN ProcessKDPLog.cpp RegisterContextKDP_arm.cpp RegisterContextKDP_arm64.cpp - RegisterContextKDP_i386.cpp RegisterContextKDP_x86_64.cpp ThreadKDP.cpp diff --git a/lldb/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_i386.cpp b/lldb/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_i386.cpp deleted file mode 100644 index 61dfeae6ddf43..0000000000000 --- a/lldb/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_i386.cpp +++ /dev/null @@ -1,114 +0,0 @@ -//===-- RegisterContextKDP_i386.cpp ---------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "RegisterContextKDP_i386.h" -#include "ProcessKDP.h" -#include "ThreadKDP.h" - -using namespace lldb; -using namespace lldb_private; - -RegisterContextKDP_i386::RegisterContextKDP_i386(ThreadKDP &thread, - uint32_t concrete_frame_idx) - : RegisterContextDarwin_i386(thread, concrete_frame_idx), - m_kdp_thread(thread) {} - -RegisterContextKDP_i386::~RegisterContextKDP_i386() = default; - -int RegisterContextKDP_i386::DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) { - ProcessSP process_sp(CalculateProcess()); - if (process_sp) { - Status error; - if (static_cast<ProcessKDP *>(process_sp.get()) - ->GetCommunication() - .SendRequestReadRegisters(tid, GPRRegSet, &gpr, sizeof(gpr), - error)) { - if (error.Success()) - return 0; - } - } - return -1; -} - -int RegisterContextKDP_i386::DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) { - ProcessSP process_sp(CalculateProcess()); - if (process_sp) { - Status error; - if (static_cast<ProcessKDP *>(process_sp.get()) - ->GetCommunication() - .SendRequestReadRegisters(tid, FPURegSet, &fpu, sizeof(fpu), - error)) { - if (error.Success()) - return 0; - } - } - return -1; -} - -int RegisterContextKDP_i386::DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) { - ProcessSP process_sp(CalculateProcess()); - if (process_sp) { - Status error; - if (static_cast<ProcessKDP *>(process_sp.get()) - ->GetCommunication() - .SendRequestReadRegisters(tid, EXCRegSet, &exc, sizeof(exc), - error)) { - if (error.Success()) - return 0; - } - } - return -1; -} - -int RegisterContextKDP_i386::DoWriteGPR(lldb::tid_t tid, int flavor, - const GPR &gpr) { - ProcessSP process_sp(CalculateProcess()); - if (process_sp) { - Status error; - if (static_cast<ProcessKDP *>(process_sp.get()) - ->GetCommunication() - .SendRequestWriteRegisters(tid, GPRRegSet, &gpr, sizeof(gpr), - error)) { - if (error.Success()) - return 0; - } - } - return -1; -} - -int RegisterContextKDP_i386::DoWriteFPU(lldb::tid_t tid, int flavor, - const FPU &fpu) { - ProcessSP process_sp(CalculateProcess()); - if (process_sp) { - Status error; - if (static_cast<ProcessKDP *>(process_sp.get()) - ->GetCommunication() - .SendRequestWriteRegisters(tid, FPURegSet, &fpu, sizeof(fpu), - error)) { - if (error.Success()) - return 0; - } - } - return -1; -} - -int RegisterContextKDP_i386::DoWriteEXC(lldb::tid_t tid, int flavor, - const EXC &exc) { - ProcessSP process_sp(CalculateProcess()); - if (process_sp) { - Status error; - if (static_cast<ProcessKDP *>(process_sp.get()) - ->GetCommunication() - .SendRequestWriteRegisters(tid, EXCRegSet, &exc, sizeof(exc), - error)) { - if (error.Success()) - return 0; - } - } - return -1; -} diff --git a/lldb/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_i386.h b/lldb/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_i386.h deleted file mode 100644 index 04868e96191fd..0000000000000 --- a/lldb/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_i386.h +++ /dev/null @@ -1,38 +0,0 @@ -//===-- RegisterContextKDP_i386.h -------------------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#ifndef LLDB_SOURCE_PLUGINS_PROCESS_MACOSX_KERNEL_REGISTERCONTEXTKDP_I386_H -#define LLDB_SOURCE_PLUGINS_PROCESS_MACOSX_KERNEL_REGISTERCONTEXTKDP_I386_H - -#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h" - -class ThreadKDP; - -class RegisterContextKDP_i386 : public RegisterContextDarwin_i386 { -public: - RegisterContextKDP_i386(ThreadKDP &thread, uint32_t concrete_frame_idx); - - ~RegisterContextKDP_i386() override; - -protected: - int DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override; - - int DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override; - - int DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override; - - int DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override; - - int DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override; - - int DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override; - - ThreadKDP &m_kdp_thread; -}; - -#endif // LLDB_SOURCE_PLUGINS_PROCESS_MACOSX_KERNEL_REGISTERCONTEXTKDP_I386_H diff --git a/lldb/source/Plugins/Process/MacOSX-Kernel/ThreadKDP.cpp b/lldb/source/Plugins/Process/MacOSX-Kernel/ThreadKDP.cpp index 1349de879c69a..69d4cfdae4ad5 100644 --- a/lldb/source/Plugins/Process/MacOSX-Kernel/ThreadKDP.cpp +++ b/lldb/source/Plugins/Process/MacOSX-Kernel/ThreadKDP.cpp @@ -26,7 +26,6 @@ #include "ProcessKDPLog.h" #include "RegisterContextKDP_arm.h" #include "RegisterContextKDP_arm64.h" -#include "RegisterContextKDP_i386.h" #include "RegisterContextKDP_x86_64.h" #include <memory> @@ -105,10 +104,6 @@ ThreadKDP::CreateRegisterContextForFrame(StackFrame *frame) { reg_ctx_sp = std::make_shared<RegisterContextKDP_arm64>( *this, concrete_frame_idx); break; - case llvm::MachO::CPU_TYPE_I386: - reg_ctx_sp = std::make_shared<RegisterContextKDP_i386>( - *this, concrete_frame_idx); - break; case llvm::MachO::CPU_TYPE_X86_64: reg_ctx_sp = std::make_shared<RegisterContextKDP_x86_64>( *this, concrete_frame_idx); diff --git a/lldb/source/Plugins/Process/Utility/CMakeLists.txt b/lldb/source/Plugins/Process/Utility/CMakeLists.txt index 48646b784f931..5d99c22dafe15 100644 --- a/lldb/source/Plugins/Process/Utility/CMakeLists.txt +++ b/lldb/source/Plugins/Process/Utility/CMakeLists.txt @@ -22,7 +22,6 @@ add_lldb_library(lldbPluginProcessUtility RegisterContext_x86.cpp RegisterContextDarwin_arm.cpp RegisterContextDarwin_arm64.cpp - RegisterContextDarwin_i386.cpp RegisterContextDarwin_riscv32.cpp RegisterContextDarwin_x86_64.cpp RegisterContextDummy.cpp @@ -35,7 +34,6 @@ add_lldb_library(lldbPluginProcessUtility RegisterContextLinux_x86_64.cpp RegisterContextLinux_s390x.cpp RegisterContextMach_arm.cpp - RegisterContextMach_i386.cpp RegisterContextMach_x86_64.cpp RegisterContextMemory.cpp RegisterContextNetBSD_i386.cpp diff --git a/lldb/source/Plugins/Process/Utility/RegisterContextDarwin_i386.cpp b/lldb/source/Plugins/Process/Utility/RegisterContextDarwin_i386.cpp deleted file mode 100644 index 174a5a567d2dd..0000000000000 --- a/lldb/source/Plugins/Process/Utility/RegisterContextDarwin_i386.cpp +++ /dev/null @@ -1,958 +0,0 @@ -//===-- RegisterContextDarwin_i386.cpp ------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "lldb/Utility/DataBufferHeap.h" -#include "lldb/Utility/DataExtractor.h" -#include "lldb/Utility/Endian.h" -#include "lldb/Utility/Log.h" -#include "lldb/Utility/RegisterValue.h" -#include "lldb/Utility/Scalar.h" -#include "llvm/ADT/STLExtras.h" -#include "llvm/Support/Compiler.h" - -#include <cstddef> - -#include <memory> - -#include "RegisterContextDarwin_i386.h" - -using namespace lldb; -using namespace lldb_private; - -enum { - gpr_eax = 0, - gpr_ebx, - gpr_ecx, - gpr_edx, - gpr_edi, - gpr_esi, - gpr_ebp, - gpr_esp, - gpr_ss, - gpr_eflags, - gpr_eip, - gpr_cs, - gpr_ds, - gpr_es, - gpr_fs, - gpr_gs, - - fpu_fcw, - fpu_fsw, - fpu_ftw, - fpu_fop, - fpu_ip, - fpu_cs, - fpu_dp, - fpu_ds, - fpu_mxcsr, - fpu_mxcsrmask, - fpu_stmm0, - fpu_stmm1, - fpu_stmm2, - fpu_stmm3, - fpu_stmm4, - fpu_stmm5, - fpu_stmm6, - fpu_stmm7, - fpu_xmm0, - fpu_xmm1, - fpu_xmm2, - fpu_xmm3, - fpu_xmm4, - fpu_xmm5, - fpu_xmm6, - fpu_xmm7, - - exc_trapno, - exc_err, - exc_faultvaddr, - - k_num_registers, - - // Aliases - fpu_fctrl = fpu_fcw, - fpu_fstat = fpu_fsw, - fpu_ftag = fpu_ftw, - fpu_fiseg = fpu_cs, - fpu_fioff = fpu_ip, - fpu_foseg = fpu_ds, - fpu_fooff = fpu_dp -}; - -enum { - ehframe_eax = 0, - ehframe_ecx, - ehframe_edx, - ehframe_ebx, - ehframe_ebp, - ehframe_esp, - ehframe_esi, - ehframe_edi, - ehframe_eip, - ehframe_eflags -}; - -enum { - dwarf_eax = 0, - dwarf_ecx, - dwarf_edx, - dwarf_ebx, - dwarf_esp, - dwarf_ebp, - dwarf_esi, - dwarf_edi, - dwarf_eip, - dwarf_eflags, - dwarf_stmm0 = 11, - dwarf_stmm1, - dwarf_stmm2, - dwarf_stmm3, - dwarf_stmm4, - dwarf_stmm5, - dwarf_stmm6, - dwarf_stmm7, - dwarf_xmm0 = 21, - dwarf_xmm1, - dwarf_xmm2, - dwarf_xmm3, - dwarf_xmm4, - dwarf_xmm5, - dwarf_xmm6, - dwarf_xmm7 -}; - -#define GPR_OFFSET(reg) \ - (LLVM_EXTENSION offsetof(RegisterContextDarwin_i386::GPR, reg)) -#define FPU_OFFSET(reg) \ - (LLVM_EXTENSION offsetof(RegisterContextDarwin_i386::FPU, reg) + \ - sizeof(RegisterContextDarwin_i386::GPR)) -#define EXC_OFFSET(reg) \ - (LLVM_EXTENSION offsetof(RegisterContextDarwin_i386::EXC, reg) + \ - sizeof(RegisterContextDarwin_i386::GPR) + \ - sizeof(RegisterContextDarwin_i386::FPU)) - -// These macros will auto define the register name, alt name, register size, -// register offset, encoding, format and native register. This ensures that the -// register state structures are defined correctly and have the correct sizes -// and offsets. -#define DEFINE_GPR(reg, alt) \ - #reg, alt, sizeof(((RegisterContextDarwin_i386::GPR *) NULL)->reg), \ - GPR_OFFSET(reg), eEncodingUint, eFormatHex -#define DEFINE_FPU_UINT(reg) \ - #reg, NULL, sizeof(((RegisterContextDarwin_i386::FPU *) NULL)->reg), \ - FPU_OFFSET(reg), eEncodingUint, eFormatHex -#define DEFINE_FPU_VECT(reg, i) \ - #reg #i, NULL, \ - sizeof(((RegisterContextDarwin_i386::FPU *) NULL)->reg[i].bytes), \ - FPU_OFFSET(reg[i]), eEncodingVector, eFormatVectorOfUInt8, \ - {LLDB_INVALID_REGNUM, dwarf_##reg##i, \ - LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, \ - fpu_##reg##i }, \ - nullptr, nullptr, nullptr, - -#define DEFINE_EXC(reg) \ - #reg, NULL, sizeof(((RegisterContextDarwin_i386::EXC *) NULL)->reg), \ - EXC_OFFSET(reg), eEncodingUint, eFormatHex -#define REG_CONTEXT_SIZE \ - (sizeof(RegisterContextDarwin_i386::GPR) + \ - sizeof(RegisterContextDarwin_i386::FPU) + \ - sizeof(RegisterContextDarwin_i386::EXC)) - -static RegisterInfo g_register_infos[] = { - {DEFINE_GPR(eax, nullptr), - {ehframe_eax, dwarf_eax, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - gpr_eax}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_GPR(ebx, nullptr), - {ehframe_ebx, dwarf_ebx, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - gpr_ebx}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_GPR(ecx, nullptr), - {ehframe_ecx, dwarf_ecx, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - gpr_ecx}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_GPR(edx, nullptr), - {ehframe_edx, dwarf_edx, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - gpr_edx}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_GPR(edi, nullptr), - {ehframe_edi, dwarf_edi, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - gpr_edi}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_GPR(esi, nullptr), - {ehframe_esi, dwarf_esi, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - gpr_esi}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_GPR(ebp, "fp"), - {ehframe_ebp, dwarf_ebp, LLDB_REGNUM_GENERIC_FP, LLDB_INVALID_REGNUM, - gpr_ebp}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_GPR(esp, "sp"), - {ehframe_esp, dwarf_esp, LLDB_REGNUM_GENERIC_SP, LLDB_INVALID_REGNUM, - gpr_esp}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_GPR(ss, nullptr), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, gpr_ss}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_GPR(eflags, "flags"), - {ehframe_eflags, dwarf_eflags, LLDB_REGNUM_GENERIC_FLAGS, - LLDB_INVALID_REGNUM, gpr_eflags}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_GPR(eip, "pc"), - {ehframe_eip, dwarf_eip, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_REGNUM, - gpr_eip}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_GPR(cs, nullptr), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, gpr_cs}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_GPR(ds, nullptr), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, gpr_ds}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_GPR(es, nullptr), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, gpr_es}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_GPR(fs, nullptr), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, gpr_fs}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_GPR(gs, nullptr), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, gpr_gs}, - nullptr, - nullptr, - nullptr, - }, - - {DEFINE_FPU_UINT(fcw), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, fpu_fcw}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_FPU_UINT(fsw), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, fpu_fsw}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_FPU_UINT(ftw), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, fpu_ftw}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_FPU_UINT(fop), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, fpu_fop}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_FPU_UINT(ip), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, fpu_ip}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_FPU_UINT(cs), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, fpu_cs}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_FPU_UINT(dp), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, fpu_dp}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_FPU_UINT(ds), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, fpu_ds}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_FPU_UINT(mxcsr), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, fpu_mxcsr}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_FPU_UINT(mxcsrmask), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, fpu_mxcsrmask}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_FPU_VECT(stmm, 0)}, - {DEFINE_FPU_VECT(stmm, 1)}, - {DEFINE_FPU_VECT(stmm, 2)}, - {DEFINE_FPU_VECT(stmm, 3)}, - {DEFINE_FPU_VECT(stmm, 4)}, - {DEFINE_FPU_VECT(stmm, 5)}, - {DEFINE_FPU_VECT(stmm, 6)}, - {DEFINE_FPU_VECT(stmm, 7)}, - {DEFINE_FPU_VECT(xmm, 0)}, - {DEFINE_FPU_VECT(xmm, 1)}, - {DEFINE_FPU_VECT(xmm, 2)}, - {DEFINE_FPU_VECT(xmm, 3)}, - {DEFINE_FPU_VECT(xmm, 4)}, - {DEFINE_FPU_VECT(xmm, 5)}, - {DEFINE_FPU_VECT(xmm, 6)}, - {DEFINE_FPU_VECT(xmm, 7)}, - - {DEFINE_EXC(trapno), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, exc_trapno}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_EXC(err), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, exc_err}, - nullptr, - nullptr, - nullptr, - }, - {DEFINE_EXC(faultvaddr), - {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, - LLDB_INVALID_REGNUM, exc_faultvaddr}, - nullptr, - nullptr, - nullptr, - }}; - -static size_t k_num_register_infos = std::size(g_register_infos); - -RegisterContextDarwin_i386::RegisterContextDarwin_i386( - Thread &thread, uint32_t concrete_frame_idx) - : RegisterContext(thread, concrete_frame_idx), gpr(), fpu(), exc() { - uint32_t i; - for (i = 0; i < kNumErrors; i++) { - gpr_errs[i] = -1; - fpu_errs[i] = -1; - exc_errs[i] = -1; - } -} - -RegisterContextDarwin_i386::~RegisterContextDarwin_i386() = default; - -void RegisterContextDarwin_i386::InvalidateAllRegisters() { - InvalidateAllRegisterStates(); -} - -size_t RegisterContextDarwin_i386::GetRegisterCount() { - assert(k_num_register_infos == k_num_registers); - return k_num_registers; -} - -const RegisterInfo * -RegisterContextDarwin_i386::GetRegisterInfoAtIndex(size_t reg) { - assert(k_num_register_infos == k_num_registers); - if (reg < k_num_registers) - return &g_register_infos[reg]; - return nullptr; -} - -size_t RegisterContextDarwin_i386::GetRegisterInfosCount() { - return k_num_register_infos; -} - -const RegisterInfo *RegisterContextDarwin_i386::GetRegisterInfos() { - return g_register_infos; -} - -// General purpose registers -static uint32_t g_gpr_regnums[] = { - gpr_eax, gpr_ebx, gpr_ecx, gpr_edx, gpr_edi, gpr_esi, gpr_ebp, gpr_esp, - gpr_ss, gpr_eflags, gpr_eip, gpr_cs, gpr_ds, gpr_es, gpr_fs, gpr_gs}; - -// Floating point registers -static uint32_t g_fpu_regnums[] = { - fpu_fcw, fpu_fsw, fpu_ftw, fpu_fop, fpu_ip, fpu_cs, - fpu_dp, fpu_ds, fpu_mxcsr, fpu_mxcsrmask, fpu_stmm0, fpu_stmm1, - fpu_stmm2, fpu_stmm3, fpu_stmm4, fpu_stmm5, fpu_stmm6, fpu_stmm7, - fpu_xmm0, fpu_xmm1, fpu_xmm2, fpu_xmm3, fpu_xmm4, fpu_xmm5, - fpu_xmm6, fpu_xmm7}; - -// Exception registers - -static uint32_t g_exc_regnums[] = {exc_trapno, exc_err, exc_faultvaddr}; - -// Number of registers in each register set -const size_t k_num_gpr_registers = std::size(g_gpr_regnums); -const size_t k_num_fpu_registers = std::size(g_fpu_regnums); -const size_t k_num_exc_registers = std::size(g_exc_regnums); - -// Register set definitions. The first definitions at register set index of -// zero is for all registers, followed by other registers sets. The register -// information for the all register set need not be filled in. -static const RegisterSet g_reg_sets[] = { - { - "General Purpose Registers", "gpr", k_num_gpr_registers, g_gpr_regnums, - }, - {"Floating Point Registers", "fpu", k_num_fpu_registers, g_fpu_regnums}, - {"Exception State Registers", "exc", k_num_exc_registers, g_exc_regnums}}; - -const size_t k_num_regsets = std::size(g_reg_sets); - -size_t RegisterContextDarwin_i386::GetRegisterSetCount() { - return k_num_regsets; -} - -const RegisterSet *RegisterContextDarwin_i386::GetRegisterSet(size_t reg_set) { - if (reg_set < k_num_regsets) - return &g_reg_sets[reg_set]; - return nullptr; -} - -// Register information definitions for 32 bit i386. -int RegisterContextDarwin_i386::GetSetForNativeRegNum(int reg_num) { - if (reg_num < fpu_fcw) - return GPRRegSet; - else if (reg_num < exc_trapno) - return FPURegSet; - else if (reg_num < k_num_registers) - return EXCRegSet; - return -1; -} - -void RegisterContextDarwin_i386::LogGPR(Log *log, const char *title) { - if (log) { - if (title) - LLDB_LOGF(log, "%s", title); - for (uint32_t i = 0; i < k_num_gpr_registers; i++) { - uint32_t reg = gpr_eax + i; - LLDB_LOGF(log, "%12s = 0x%8.8x", g_register_infos[reg].name, - (&gpr.eax)[reg]); - } - } -} - -int RegisterContextDarwin_i386::ReadGPR(bool force) { - int set = GPRRegSet; - if (force || !RegisterSetIsCached(set)) { - SetError(set, Read, DoReadGPR(GetThreadID(), set, gpr)); - } - return GetError(set, Read); -} - -int RegisterContextDarwin_i386::ReadFPU(bool force) { - int set = FPURegSet; - if (force || !RegisterSetIsCached(set)) { - SetError(set, Read, DoReadFPU(GetThreadID(), set, fpu)); - } - return GetError(set, Read); -} - -int RegisterContextDarwin_i386::ReadEXC(bool force) { - int set = EXCRegSet; - if (force || !RegisterSetIsCached(set)) { - SetError(set, Read, DoReadEXC(GetThreadID(), set, exc)); - } - return GetError(set, Read); -} - -int RegisterContextDarwin_i386::WriteGPR() { - int set = GPRRegSet; - if (!RegisterSetIsCached(set)) { - SetError(set, Write, -1); - return -1; - } - SetError(set, Write, DoWriteGPR(GetThreadID(), set, gpr)); - SetError(set, Read, -1); - return GetError(set, Write); -} - -int RegisterContextDarwin_i386::WriteFPU() { - int set = FPURegSet; - if (!RegisterSetIsCached(set)) { - SetError(set, Write, -1); - return -1; - } - SetError(set, Write, DoWriteFPU(GetThreadID(), set, fpu)); - SetError(set, Read, -1); - return GetError(set, Write); -} - -int RegisterContextDarwin_i386::WriteEXC() { - int set = EXCRegSet; - if (!RegisterSetIsCached(set)) { - SetError(set, Write, -1); - return -1; - } - SetError(set, Write, DoWriteEXC(GetThreadID(), set, exc)); - SetError(set, Read, -1); - return GetError(set, Write); -} - -int RegisterContextDarwin_i386::ReadRegisterSet(uint32_t set, bool force) { - switch (set) { - case GPRRegSet: - return ReadGPR(force); - case FPURegSet: - return ReadFPU(force); - case EXCRegSet: - return ReadEXC(force); - default: - break; - } - return -1; -} - -int RegisterContextDarwin_i386::WriteRegisterSet(uint32_t set) { - // Make sure we have a valid context to set. - if (RegisterSetIsCached(set)) { - switch (set) { - case GPRRegSet: - return WriteGPR(); - case FPURegSet: - return WriteFPU(); - case EXCRegSet: - return WriteEXC(); - default: - break; - } - } - return -1; -} - -bool RegisterContextDarwin_i386::ReadRegister(const RegisterInfo *reg_info, - RegisterValue &value) { - const uint32_t reg = reg_info->kinds[eRegisterKindLLDB]; - int set = RegisterContextDarwin_i386::GetSetForNativeRegNum(reg); - - if (set == -1) - return false; - - if (ReadRegisterSet(set, false) != 0) - return false; - - switch (reg) { - case gpr_eax: - case gpr_ebx: - case gpr_ecx: - case gpr_edx: - case gpr_edi: - case gpr_esi: - case gpr_ebp: - case gpr_esp: - case gpr_ss: - case gpr_eflags: - case gpr_eip: - case gpr_cs: - case gpr_ds: - case gpr_es: - case gpr_fs: - case gpr_gs: - value = (&gpr.eax)[reg - gpr_eax]; - break; - - case fpu_fcw: - value = fpu.fcw; - break; - - case fpu_fsw: - value = fpu.fsw; - break; - - case fpu_ftw: - value = fpu.ftw; - break; - - case fpu_fop: - value = fpu.fop; - break; - - case fpu_ip: - value = fpu.ip; - break; - - case fpu_cs: - value = fpu.cs; - break; - - case fpu_dp: - value = fpu.dp; - break; - - case fpu_ds: - value = fpu.ds; - break; - - case fpu_mxcsr: - value = fpu.mxcsr; - break; - - case fpu_mxcsrmask: - value = fpu.mxcsrmask; - break; - - case fpu_stmm0: - case fpu_stmm1: - case fpu_stmm2: - case fpu_stmm3: - case fpu_stmm4: - case fpu_stmm5: - case fpu_stmm6: - case fpu_stmm7: - // These values don't fit into scalar types, - // RegisterContext::ReadRegisterBytes() must be used for these registers - //::memcpy (reg_value.value.vector.uint8, fpu.stmm[reg - fpu_stmm0].bytes, - //10); - return false; - - case fpu_xmm0: - case fpu_xmm1: - case fpu_xmm2: - case fpu_xmm3: - case fpu_xmm4: - case fpu_xmm5: - case fpu_xmm6: - case fpu_xmm7: - // These values don't fit into scalar types, - // RegisterContext::ReadRegisterBytes() must be used for these registers - //::memcpy (reg_value.value.vector.uint8, fpu.xmm[reg - fpu_xmm0].bytes, - //16); - return false; - - case exc_trapno: - value = exc.trapno; - break; - - case exc_err: - value = exc.err; - break; - - case exc_faultvaddr: - value = exc.faultvaddr; - break; - - default: - return false; - } - return true; -} - -bool RegisterContextDarwin_i386::WriteRegister(const RegisterInfo *reg_info, - const RegisterValue &value) { - const uint32_t reg = reg_info->kinds[eRegisterKindLLDB]; - int set = GetSetForNativeRegNum(reg); - - if (set == -1) - return false; - - if (ReadRegisterSet(set, false) != 0) - return false; - - switch (reg) { - case gpr_eax: - case gpr_ebx: - case gpr_ecx: - case gpr_edx: - case gpr_edi: - case gpr_esi: - case gpr_ebp: - case gpr_esp: - case gpr_ss: - case gpr_eflags: - case gpr_eip: - case gpr_cs: - case gpr_ds: - case gpr_es: - case gpr_fs: - case gpr_gs: - (&gpr.eax)[reg - gpr_eax] = value.GetAsUInt32(); - break; - - case fpu_fcw: - fpu.fcw = value.GetAsUInt16(); - break; - - case fpu_fsw: - fpu.fsw = value.GetAsUInt16(); - break; - - case fpu_ftw: - fpu.ftw = value.GetAsUInt8(); - break; - - case fpu_fop: - fpu.fop = value.GetAsUInt16(); - break; - - case fpu_ip: - fpu.ip = value.GetAsUInt32(); - break; - - case fpu_cs: - fpu.cs = value.GetAsUInt16(); - break; - - case fpu_dp: - fpu.dp = value.GetAsUInt32(); - break; - - case fpu_ds: - fpu.ds = value.GetAsUInt16(); - break; - - case fpu_mxcsr: - fpu.mxcsr = value.GetAsUInt32(); - break; - - case fpu_mxcsrmask: - fpu.mxcsrmask = value.GetAsUInt32(); - break; - - case fpu_stmm0: - case fpu_stmm1: - case fpu_stmm2: - case fpu_stmm3: - case fpu_stmm4: - case fpu_stmm5: - case fpu_stmm6: - case fpu_stmm7: - // These values don't fit into scalar types, - // RegisterContext::ReadRegisterBytes() must be used for these registers - ::memcpy(fpu.stmm[reg - fpu_stmm0].bytes, value.GetBytes(), - value.GetByteSize()); - return false; - - case fpu_xmm0: - case fpu_xmm1: - case fpu_xmm2: - case fpu_xmm3: - case fpu_xmm4: - case fpu_xmm5: - case fpu_xmm6: - case fpu_xmm7: - // These values don't fit into scalar types, - // RegisterContext::ReadRegisterBytes() must be used for these registers - ::memcpy(fpu.xmm[reg - fpu_xmm0].bytes, value.GetBytes(), - value.GetByteSize()); - return false; - - case exc_trapno: - exc.trapno = value.GetAsUInt32(); - break; - - case exc_err: - exc.err = value.GetAsUInt32(); - break; - - case exc_faultvaddr: - exc.faultvaddr = value.GetAsUInt32(); - break; - - default: - return false; - } - return WriteRegisterSet(set) == 0; -} - -bool RegisterContextDarwin_i386::ReadAllRegisterValues( - lldb::WritableDataBufferSP &data_sp) { - data_sp = std::make_shared<DataBufferHeap>(REG_CONTEXT_SIZE, 0); - if (ReadGPR(false) == 0 && ReadFPU(false) == 0 && ReadEXC(false) == 0) { - uint8_t *dst = data_sp->GetBytes(); - ::memcpy(dst, &gpr, sizeof(gpr)); - dst += sizeof(gpr); - - ::memcpy(dst, &fpu, sizeof(fpu)); - dst += sizeof(gpr); - - ::memcpy(dst, &exc, sizeof(exc)); - return true; - } - return false; -} - -bool RegisterContextDarwin_i386::WriteAllRegisterValues( - const lldb::DataBufferSP &data_sp) { - if (data_sp && data_sp->GetByteSize() == REG_CONTEXT_SIZE) { - const uint8_t *src = data_sp->GetBytes(); - ::memcpy(&gpr, src, sizeof(gpr)); - src += sizeof(gpr); - - ::memcpy(&fpu, src, sizeof(fpu)); - src += sizeof(gpr); - - ::memcpy(&exc, src, sizeof(exc)); - uint32_t success_count = 0; - if (WriteGPR() == 0) - ++success_count; - if (WriteFPU() == 0) - ++success_count; - if (WriteEXC() == 0) - ++success_count; - return success_count == 3; - } - return false; -} - -uint32_t RegisterContextDarwin_i386::ConvertRegisterKindToRegisterNumber( - lldb::RegisterKind kind, uint32_t reg) { - if (kind == eRegisterKindGeneric) { - switch (reg) { - case LLDB_REGNUM_GENERIC_PC: - return gpr_eip; - case LLDB_REGNUM_GENERIC_SP: - return gpr_esp; - case LLDB_REGNUM_GENERIC_FP: - return gpr_ebp; - case LLDB_REGNUM_GENERIC_FLAGS: - return gpr_eflags; - case LLDB_REGNUM_GENERIC_RA: - default: - break; - } - } else if (kind == eRegisterKindEHFrame || kind == eRegisterKindDWARF) { - switch (reg) { - case dwarf_eax: - return gpr_eax; - case dwarf_ecx: - return gpr_ecx; - case dwarf_edx: - return gpr_edx; - case dwarf_ebx: - return gpr_ebx; - case dwarf_esp: - return gpr_esp; - case dwarf_ebp: - return gpr_ebp; - case dwarf_esi: - return gpr_esi; - case dwarf_edi: - return gpr_edi; - case dwarf_eip: - return gpr_eip; - case dwarf_eflags: - return gpr_eflags; - case dwarf_stmm0: - return fpu_stmm0; - case dwarf_stmm1: - return fpu_stmm1; - case dwarf_stmm2: - return fpu_stmm2; - case dwarf_stmm3: - return fpu_stmm3; - case dwarf_stmm4: - return fpu_stmm4; - case dwarf_stmm5: - return fpu_stmm5; - case dwarf_stmm6: - return fpu_stmm6; - case dwarf_stmm7: - return fpu_stmm7; - case dwarf_xmm0: - return fpu_xmm0; - case dwarf_xmm1: - return fpu_xmm1; - case dwarf_xmm2: - return fpu_xmm2; - case dwarf_xmm3: - return fpu_xmm3; - case dwarf_xmm4: - return fpu_xmm4; - case dwarf_xmm5: - return fpu_xmm5; - case dwarf_xmm6: - return fpu_xmm6; - case dwarf_xmm7: - return fpu_xmm7; - default: - break; - } - } else if (kind == eRegisterKindLLDB) { - return reg; - } - return LLDB_INVALID_REGNUM; -} - -bool RegisterContextDarwin_i386::HardwareSingleStep(bool enable) { - if (ReadGPR(false) != 0) - return false; - - const uint32_t trace_bit = 0x100u; - if (enable) { - // If the trace bit is already set, there is nothing to do - if (gpr.eflags & trace_bit) - return true; - else - gpr.eflags |= trace_bit; - } else { - // If the trace bit is already cleared, there is nothing to do - if (gpr.eflags & trace_bit) - gpr.eflags &= ~trace_bit; - else - return true; - } - - return WriteGPR() == 0; -} diff --git a/lldb/source/Plugins/Process/Utility/RegisterContextDarwin_i386.h b/lldb/source/Plugins/Process/Utility/RegisterContextDarwin_i386.h deleted file mode 100644 index be933f3be2661..0000000000000 --- a/lldb/source/Plugins/Process/Utility/RegisterContextDarwin_i386.h +++ /dev/null @@ -1,208 +0,0 @@ -//===-- RegisterContextDarwin_i386.h ----------------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#ifndef LLDB_SOURCE_PLUGINS_PROCESS_UTILITY_REGISTERCONTEXTDARWIN_I386_H -#define LLDB_SOURCE_PLUGINS_PROCESS_UTILITY_REGISTERCONTEXTDARWIN_I386_H - -#include "lldb/Target/RegisterContext.h" -#include "lldb/lldb-private.h" - -class RegisterContextDarwin_i386 : public lldb_private::RegisterContext { -public: - RegisterContextDarwin_i386(lldb_private::Thread &thread, - uint32_t concrete_frame_idx); - - ~RegisterContextDarwin_i386() override; - - void InvalidateAllRegisters() override; - - size_t GetRegisterCount() override; - - const lldb_private::RegisterInfo *GetRegisterInfoAtIndex(size_t reg) override; - - size_t GetRegisterSetCount() override; - - const lldb_private::RegisterSet *GetRegisterSet(size_t set) override; - - bool ReadRegister(const lldb_private::RegisterInfo *reg_info, - lldb_private::RegisterValue &value) override; - - bool WriteRegister(const lldb_private::RegisterInfo *reg_info, - const lldb_private::RegisterValue &value) override; - - bool ReadAllRegisterValues(lldb::WritableDataBufferSP &data_sp) override; - - bool WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) override; - - uint32_t ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind, - uint32_t num) override; - - bool HardwareSingleStep(bool enable) override; - - struct GPR { - uint32_t eax; - uint32_t ebx; - uint32_t ecx; - uint32_t edx; - uint32_t edi; - uint32_t esi; - uint32_t ebp; - uint32_t esp; - uint32_t ss; - uint32_t eflags; - uint32_t eip; - uint32_t cs; - uint32_t ds; - uint32_t es; - uint32_t fs; - uint32_t gs; - }; - - struct MMSReg { - uint8_t bytes[10]; - uint8_t pad[6]; - }; - - struct XMMReg { - uint8_t bytes[16]; - }; - - struct FPU { - uint32_t pad[2]; - uint16_t fcw; - uint16_t fsw; - uint8_t ftw; - uint8_t pad1; - uint16_t fop; - uint32_t ip; - uint16_t cs; - uint16_t pad2; - uint32_t dp; - uint16_t ds; - uint16_t pad3; - uint32_t mxcsr; - uint32_t mxcsrmask; - MMSReg stmm[8]; - XMMReg xmm[8]; - uint8_t pad4[14 * 16]; - int pad5; - }; - - struct EXC { - uint32_t trapno; - uint32_t err; - uint32_t faultvaddr; - }; - -protected: - enum { GPRRegSet = 1, FPURegSet = 2, EXCRegSet = 3 }; - - enum { - GPRWordCount = sizeof(GPR) / sizeof(uint32_t), - FPUWordCount = sizeof(FPU) / sizeof(uint32_t), - EXCWordCount = sizeof(EXC) / sizeof(uint32_t) - }; - - enum { Read = 0, Write = 1, kNumErrors = 2 }; - - GPR gpr; - FPU fpu; - EXC exc; - int gpr_errs[2]; // Read/Write errors - int fpu_errs[2]; // Read/Write errors - int exc_errs[2]; // Read/Write errors - - void InvalidateAllRegisterStates() { - SetError(GPRRegSet, Read, -1); - SetError(FPURegSet, Read, -1); - SetError(EXCRegSet, Read, -1); - } - - int GetError(int flavor, uint32_t err_idx) const { - if (err_idx < kNumErrors) { - switch (flavor) { - // When getting all errors, just OR all values together to see if - // we got any kind of error. - case GPRRegSet: - return gpr_errs[err_idx]; - case FPURegSet: - return fpu_errs[err_idx]; - case EXCRegSet: - return exc_errs[err_idx]; - default: - break; - } - } - return -1; - } - - bool SetError(int flavor, uint32_t err_idx, int err) { - if (err_idx < kNumErrors) { - switch (flavor) { - case GPRRegSet: - gpr_errs[err_idx] = err; - return true; - - case FPURegSet: - fpu_errs[err_idx] = err; - return true; - - case EXCRegSet: - exc_errs[err_idx] = err; - return true; - - default: - break; - } - } - return false; - } - - bool RegisterSetIsCached(int set) const { return GetError(set, Read) == 0; } - - void LogGPR(lldb_private::Log *log, const char *title); - - int ReadGPR(bool force); - - int ReadFPU(bool force); - - int ReadEXC(bool force); - - int WriteGPR(); - - int WriteFPU(); - - int WriteEXC(); - - // Subclasses override these to do the actual reading. - virtual int DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) = 0; - - virtual int DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) = 0; - - virtual int DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) = 0; - - virtual int DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) = 0; - - virtual int DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) = 0; - - virtual int DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) = 0; - - int ReadRegisterSet(uint32_t set, bool force); - - int WriteRegisterSet(uint32_t set); - - static uint32_t GetRegisterNumber(uint32_t reg_kind, uint32_t reg_num); - - static int GetSetForNativeRegNum(int reg_num); - - static size_t GetRegisterInfosCount(); - - static const lldb_private::RegisterInfo *GetRegisterInfos(); -}; - -#endif // LLDB_SOURCE_PLUGINS_PROCESS_UTILITY_REGISTERCONTEXTDARWIN_I386_H diff --git a/lldb/source/Plugins/Process/Utility/RegisterContextMach_i386.cpp b/lldb/source/Plugins/Process/Utility/RegisterContextMach_i386.cpp deleted file mode 100644 index fe5cecef1b0c7..0000000000000 --- a/lldb/source/Plugins/Process/Utility/RegisterContextMach_i386.cpp +++ /dev/null @@ -1,60 +0,0 @@ -//===-- RegisterContextMach_i386.cpp --------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#if defined(__APPLE__) - -#include <mach/thread_act.h> - -#include "RegisterContextMach_i386.h" - -using namespace lldb; -using namespace lldb_private; - -RegisterContextMach_i386::RegisterContextMach_i386(Thread &thread, - uint32_t concrete_frame_idx) - : RegisterContextDarwin_i386(thread, concrete_frame_idx) {} - -RegisterContextMach_i386::~RegisterContextMach_i386() = default; - -int RegisterContextMach_i386::DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) { - mach_msg_type_number_t count = GPRWordCount; - return ::thread_get_state(tid, flavor, (thread_state_t)&gpr, &count); -} - -int RegisterContextMach_i386::DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) { - mach_msg_type_number_t count = FPUWordCount; - return ::thread_get_state(tid, flavor, (thread_state_t)&fpu, &count); -} - -int RegisterContextMach_i386::DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) { - mach_msg_type_number_t count = EXCWordCount; - return ::thread_get_state(tid, flavor, (thread_state_t)&exc, &count); -} - -int RegisterContextMach_i386::DoWriteGPR(lldb::tid_t tid, int flavor, - const GPR &gpr) { - return ::thread_set_state( - tid, flavor, reinterpret_cast<thread_state_t>(const_cast<GPR *>(&gpr)), - GPRWordCount); -} - -int RegisterContextMach_i386::DoWriteFPU(lldb::tid_t tid, int flavor, - const FPU &fpu) { - return ::thread_set_state( - tid, flavor, reinterpret_cast<thread_state_t>(const_cast<FPU *>(&fpu)), - FPUWordCount); -} - -int RegisterContextMach_i386::DoWriteEXC(lldb::tid_t tid, int flavor, - const EXC &exc) { - return ::thread_set_state( - tid, flavor, reinterpret_cast<thread_state_t>(const_cast<EXC *>(&exc)), - EXCWordCount); -} - -#endif diff --git a/lldb/source/Plugins/Process/Utility/RegisterContextMach_i386.h b/lldb/source/Plugins/Process/Utility/RegisterContextMach_i386.h deleted file mode 100644 index 8bdac083863d4..0000000000000 --- a/lldb/source/Plugins/Process/Utility/RegisterContextMach_i386.h +++ /dev/null @@ -1,35 +0,0 @@ -//===-- RegisterContextMach_i386.h ------------------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#ifndef LLDB_SOURCE_PLUGINS_PROCESS_UTILITY_REGISTERCONTEXTMACH_I386_H -#define LLDB_SOURCE_PLUGINS_PROCESS_UTILITY_REGISTERCONTEXTMACH_I386_H - -#include "RegisterContextDarwin_i386.h" - -class RegisterContextMach_i386 : public RegisterContextDarwin_i386 { -public: - RegisterContextMach_i386(lldb_private::Thread &thread, - uint32_t concrete_frame_idx); - - ~RegisterContextMach_i386() override; - -protected: - int DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override; - - int DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override; - - int DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override; - - int DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override; - - int DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override; - - int DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override; -}; - -#endif // LLDB_SOURCE_PLUGINS_PROCESS_UTILITY_REGISTERCONTEXTMACH_I386_H diff --git a/llvm/utils/gn/secondary/lldb/source/Plugins/Process/Utility/BUILD.gn b/llvm/utils/gn/secondary/lldb/source/Plugins/Process/Utility/BUILD.gn index 1b4ab1df0a353..672770ef8ed51 100644 --- a/llvm/utils/gn/secondary/lldb/source/Plugins/Process/Utility/BUILD.gn +++ b/llvm/utils/gn/secondary/lldb/source/Plugins/Process/Utility/BUILD.gn @@ -38,7 +38,6 @@ static_library("Utility") { "OpenBSDSignals.cpp", "RegisterContextDarwin_arm.cpp", "RegisterContextDarwin_arm64.cpp", - "RegisterContextDarwin_i386.cpp", "RegisterContextDarwin_riscv32.cpp", "RegisterContextDarwin_x86_64.cpp", "RegisterContextDummy.cpp", @@ -51,7 +50,6 @@ static_library("Utility") { "RegisterContextLinux_s390x.cpp", "RegisterContextLinux_x86_64.cpp", "RegisterContextMach_arm.cpp", - "RegisterContextMach_i386.cpp", "RegisterContextMach_x86_64.cpp", "RegisterContextMemory.cpp", "RegisterContextNetBSD_i386.cpp", _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits