1 //===-- NativeRegisterContextDBReg.h ----------------------------*- C++ -*-===// 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 #ifndef lldb_NativeRegisterContextDBReg_h 10 #define lldb_NativeRegisterContextDBReg_h 11 12 #include "Plugins/Process/Utility/NativeRegisterContextRegisterInfo.h" 13 14 #include <array> 15 16 // Common utilities for hardware breakpoints and hardware watchpoints on AArch64 17 // and LoongArch. 18 19 namespace lldb_private { 20 21 class NativeRegisterContextDBReg 22 : public virtual NativeRegisterContextRegisterInfo { 23 public: NativeRegisterContextDBReg(uint32_t enable_bit)24 explicit NativeRegisterContextDBReg(uint32_t enable_bit) 25 : m_hw_dbg_enable_bit(enable_bit) {} 26 27 uint32_t NumSupportedHardwareBreakpoints() override; 28 29 uint32_t SetHardwareBreakpoint(lldb::addr_t addr, size_t size) override; 30 31 bool ClearHardwareBreakpoint(uint32_t hw_idx) override; 32 33 Status ClearAllHardwareBreakpoints() override; 34 35 Status GetHardwareBreakHitIndex(uint32_t &bp_index, 36 lldb::addr_t trap_addr) override; 37 38 uint32_t NumSupportedHardwareWatchpoints() override; 39 40 uint32_t SetHardwareWatchpoint(lldb::addr_t addr, size_t size, 41 uint32_t watch_flags) override; 42 43 bool ClearHardwareWatchpoint(uint32_t hw_index) override; 44 45 Status ClearAllHardwareWatchpoints() override; 46 47 Status GetWatchpointHitIndex(uint32_t &wp_index, 48 lldb::addr_t trap_addr) override; 49 50 lldb::addr_t GetWatchpointHitAddress(uint32_t wp_index) override; 51 52 lldb::addr_t GetWatchpointAddress(uint32_t wp_index) override; 53 54 protected: 55 // Debug register type select 56 enum DREGType { eDREGTypeWATCH = 0, eDREGTypeBREAK }; 57 58 /// Debug register info for hardware breakpoints and watchpoints management. 59 struct DREG { 60 lldb::addr_t address; // Breakpoint/watchpoint address value. 61 lldb::addr_t hit_addr; // Address at which last watchpoint trigger exception 62 // occurred. 63 lldb::addr_t real_addr; // Address value that should cause target to stop. 64 uint32_t control; // Breakpoint/watchpoint control value. 65 }; 66 67 std::array<struct DREG, 16> m_hbp_regs; // hardware breakpoints 68 std::array<struct DREG, 16> m_hwp_regs; // hardware watchpoints 69 70 uint32_t m_max_hbp_supported; 71 uint32_t m_max_hwp_supported; 72 const uint32_t m_hw_dbg_enable_bit; 73 74 bool WatchpointIsEnabled(uint32_t wp_index); 75 bool BreakpointIsEnabled(uint32_t bp_index); 76 77 // On AArch64 and Loongarch the hardware breakpoint length size is 4, and the 78 // target address must 4-byte alignment. ValidateBreakpoint(size_t size,lldb::addr_t addr)79 bool ValidateBreakpoint(size_t size, lldb::addr_t addr) { 80 return (size == 4) && !(addr & 0x3); 81 } 82 struct WatchpointDetails { 83 size_t size; 84 lldb::addr_t addr; 85 }; 86 virtual std::optional<WatchpointDetails> 87 AdjustWatchpoint(const WatchpointDetails &details) = 0; 88 virtual uint32_t MakeBreakControlValue(size_t size) = 0; 89 virtual uint32_t MakeWatchControlValue(size_t size, uint32_t watch_flags) = 0; 90 virtual uint32_t GetWatchpointSize(uint32_t wp_index) = 0; 91 92 virtual llvm::Error ReadHardwareDebugInfo() = 0; 93 virtual llvm::Error WriteHardwareDebugRegs(DREGType hwbType) = 0; FixWatchpointHitAddress(lldb::addr_t hit_addr)94 virtual lldb::addr_t FixWatchpointHitAddress(lldb::addr_t hit_addr) { 95 return hit_addr; 96 } 97 }; 98 99 } // namespace lldb_private 100 101 #endif // #ifndef lldb_NativeRegisterContextDBReg_h 102