Path: blob/main/contrib/llvm-project/lldb/source/Plugins/Process/Utility/NativeRegisterContextDBReg_x86.cpp
96380 views
//===-- NativeRegisterContextDBReg_x86.cpp --------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "NativeRegisterContextDBReg_x86.h"9#include "lldb/Utility/LLDBLog.h"10#include "lldb/Utility/RegisterValue.h"1112#include "Plugins/Process/Utility/lldb-x86-register-enums.h"1314using namespace lldb_private;1516// Returns mask/value for status bit of wp_index in DR617static inline uint64_t GetStatusBit(uint32_t wp_index) {18// DR6: ...BBBB19// 3210 <- status bits for bp./wp. i; 1 if hit20return 1ULL << wp_index;21}2223// Returns mask/value for global enable bit of wp_index in DR724static inline uint64_t GetEnableBit(uint32_t wp_index) {25// DR7: ...GLGLGLGL26// 33221100 <- global/local enable for bp./wp.; 1 if enabled27// we use global bits because NetBSD kernel does not preserve local28// bits reliably; Linux seems fine with either29return 1ULL << (2 * wp_index + 1);30}3132// Returns mask for both enable bits of wp_index in DR733static inline uint64_t GetBothEnableBitMask(uint32_t wp_index) {34// DR7: ...GLGLGLGL35// 33221100 <- global/local enable for bp./wp.; 1 if enabled36return 3ULL << (2 * wp_index + 1);37}3839// Returns value for type bits of wp_index in DR740static inline uint64_t GetWatchTypeBits(uint32_t watch_flags,41uint32_t wp_index) {42// DR7:43// bit: 3322222222221111...44// 1098765432109876...45// val: SSTTSSTTSSTTSSTT...46// wp.: 3333222211110000...47//48// where T - type is 01 for write, 11 for r/w49return static_cast<uint64_t>(watch_flags) << (16 + 4 * wp_index);50}5152// Returns value for size bits of wp_index in DR753static inline uint64_t GetWatchSizeBits(uint32_t size, uint32_t wp_index) {54// DR7:55// bit: 3322222222221111...56// 1098765432109876...57// val: SSTTSSTTSSTTSSTT...58// wp.: 3333222211110000...59//60// where S - size is:61// 00 for 1 byte62// 01 for 2 bytes63// 10 for 8 bytes64// 11 for 4 bytes65return static_cast<uint64_t>(size == 8 ? 0x2 : size - 1)66<< (18 + 4 * wp_index);67}6869// Returns bitmask for all bits controlling wp_index in DR770static inline uint64_t GetWatchControlBitmask(uint32_t wp_index) {71// DR7:72// bit: 3322222222221111111111000000000073// 1098765432109876543210987654321074// val: SSTTSSTTSSTTSSTTxxxxxxGLGLGLGLGL75// wp.: 3333222211110000xxxxxxEE3322110076return GetBothEnableBitMask(wp_index) | (0xF << (16 + 4 * wp_index));77}7879// Bit mask for control bits regarding all watchpoints.80static constexpr uint64_t watchpoint_all_control_bit_mask = 0xFFFF00FF;8182const RegisterInfo *NativeRegisterContextDBReg_x86::GetDR(int num) const {83assert(num >= 0 && num <= 7);84switch (GetRegisterInfoInterface().GetTargetArchitecture().GetMachine()) {85case llvm::Triple::x86:86return GetRegisterInfoAtIndex(lldb_dr0_i386 + num);87case llvm::Triple::x86_64:88return GetRegisterInfoAtIndex(lldb_dr0_x86_64 + num);89default:90llvm_unreachable("Unhandled target architecture.");91}92}9394Status NativeRegisterContextDBReg_x86::IsWatchpointHit(uint32_t wp_index,95bool &is_hit) {96if (wp_index >= NumSupportedHardwareWatchpoints())97return Status("Watchpoint index out of range");9899RegisterValue dr6;100Status error = ReadRegister(GetDR(6), dr6);101if (error.Fail())102is_hit = false;103else104is_hit = dr6.GetAsUInt64() & GetStatusBit(wp_index);105106return error;107}108109Status110NativeRegisterContextDBReg_x86::GetWatchpointHitIndex(uint32_t &wp_index,111lldb::addr_t trap_addr) {112uint32_t num_hw_wps = NumSupportedHardwareWatchpoints();113for (wp_index = 0; wp_index < num_hw_wps; ++wp_index) {114bool is_hit;115Status error = IsWatchpointHit(wp_index, is_hit);116if (error.Fail()) {117wp_index = LLDB_INVALID_INDEX32;118return error;119} else if (is_hit) {120return error;121}122}123wp_index = LLDB_INVALID_INDEX32;124return Status();125}126127Status NativeRegisterContextDBReg_x86::IsWatchpointVacant(uint32_t wp_index,128bool &is_vacant) {129if (wp_index >= NumSupportedHardwareWatchpoints())130return Status("Watchpoint index out of range");131132RegisterValue dr7;133Status error = ReadRegister(GetDR(7), dr7);134if (error.Fail())135is_vacant = false;136else137is_vacant = !(dr7.GetAsUInt64() & GetEnableBit(wp_index));138139return error;140}141142Status NativeRegisterContextDBReg_x86::SetHardwareWatchpointWithIndex(143lldb::addr_t addr, size_t size, uint32_t watch_flags, uint32_t wp_index) {144145if (wp_index >= NumSupportedHardwareWatchpoints())146return Status("Watchpoint index out of range");147148// Read only watchpoints aren't supported on x86_64. Fall back to read/write149// waitchpoints instead.150// TODO: Add logic to detect when a write happens and ignore that watchpoint151// hit.152if (watch_flags == 2)153watch_flags = 3;154155if (watch_flags != 1 && watch_flags != 3)156return Status("Invalid read/write bits for watchpoint");157if (size != 1 && size != 2 && size != 4 && size != 8)158return Status("Invalid size for watchpoint");159160bool is_vacant;161Status error = IsWatchpointVacant(wp_index, is_vacant);162if (error.Fail())163return error;164if (!is_vacant)165return Status("Watchpoint index not vacant");166167RegisterValue dr7, drN;168error = ReadRegister(GetDR(7), dr7);169if (error.Fail())170return error;171error = ReadRegister(GetDR(wp_index), drN);172if (error.Fail())173return error;174175uint64_t control_bits = dr7.GetAsUInt64() & ~GetWatchControlBitmask(wp_index);176control_bits |= GetEnableBit(wp_index) |177GetWatchTypeBits(watch_flags, wp_index) |178GetWatchSizeBits(size, wp_index);179180// Clear dr6 if address or bits changed (i.e. we're not reenabling the same181// watchpoint). This can not be done when clearing watchpoints since182// the gdb-remote protocol repeatedly clears and readds watchpoints on all183// program threads, effectively clearing pending events on NetBSD.184// NB: enable bits in dr7 are always 0 here since we're (re)adding it185if (drN.GetAsUInt64() != addr ||186(dr7.GetAsUInt64() & GetWatchControlBitmask(wp_index)) !=187(GetWatchTypeBits(watch_flags, wp_index) |188GetWatchSizeBits(size, wp_index))) {189ClearWatchpointHit(wp_index);190191// We skip update to drN if neither address nor mode changed.192error = WriteRegister(GetDR(wp_index), RegisterValue(addr));193if (error.Fail())194return error;195}196197error = WriteRegister(GetDR(7), RegisterValue(control_bits));198if (error.Fail())199return error;200201return error;202}203204bool NativeRegisterContextDBReg_x86::ClearHardwareWatchpoint(205uint32_t wp_index) {206if (wp_index >= NumSupportedHardwareWatchpoints())207return false;208209RegisterValue dr7;210Status error = ReadRegister(GetDR(7), dr7);211if (error.Fail())212return false;213214return WriteRegister(GetDR(7), RegisterValue(dr7.GetAsUInt64() &215~GetBothEnableBitMask(wp_index)))216.Success();217}218219Status NativeRegisterContextDBReg_x86::ClearWatchpointHit(uint32_t wp_index) {220if (wp_index >= NumSupportedHardwareWatchpoints())221return Status("Watchpoint index out of range");222223RegisterValue dr6;224Status error = ReadRegister(GetDR(6), dr6);225if (error.Fail())226return error;227228return WriteRegister(229GetDR(6), RegisterValue(dr6.GetAsUInt64() & ~GetStatusBit(wp_index)));230}231232Status NativeRegisterContextDBReg_x86::ClearAllHardwareWatchpoints() {233RegisterValue dr7;234Status error = ReadRegister(GetDR(7), dr7);235if (error.Fail())236return error;237return WriteRegister(238GetDR(7),239RegisterValue(dr7.GetAsUInt64() & ~watchpoint_all_control_bit_mask));240}241242uint32_t NativeRegisterContextDBReg_x86::SetHardwareWatchpoint(243lldb::addr_t addr, size_t size, uint32_t watch_flags) {244Log *log = GetLog(LLDBLog::Watchpoints);245const uint32_t num_hw_watchpoints = NumSupportedHardwareWatchpoints();246for (uint32_t wp_index = 0; wp_index < num_hw_watchpoints; ++wp_index) {247bool is_vacant;248Status error = IsWatchpointVacant(wp_index, is_vacant);249if (is_vacant) {250error = SetHardwareWatchpointWithIndex(addr, size, watch_flags, wp_index);251if (error.Success())252return wp_index;253}254if (error.Fail() && log) {255LLDB_LOGF(log, "NativeRegisterContextDBReg_x86::%s Error: %s",256__FUNCTION__, error.AsCString());257}258}259return LLDB_INVALID_INDEX32;260}261262lldb::addr_t263NativeRegisterContextDBReg_x86::GetWatchpointAddress(uint32_t wp_index) {264if (wp_index >= NumSupportedHardwareWatchpoints())265return LLDB_INVALID_ADDRESS;266RegisterValue drN;267if (ReadRegister(GetDR(wp_index), drN).Fail())268return LLDB_INVALID_ADDRESS;269return drN.GetAsUInt64();270}271272uint32_t NativeRegisterContextDBReg_x86::NumSupportedHardwareWatchpoints() {273// Available debug address registers: dr0, dr1, dr2, dr3274return 4;275}276277278