https://github.com/DavidSpickett created 
https://github.com/llvm/llvm-project/pull/209483

NativeRegisterContextDBReg_arm64::AdjustWatchpoint has a way of adjusting the 
range of a watchpoint to fit hardware expectations but it took me a while to 
figure out how it worked.

I've rewritten it in a way I think is easier to follow, with more explanatory 
comments.

>From 765ae437a29c80143744a53632e8bf339710f586 Mon Sep 17 00:00:00 2001
From: David Spickett <[email protected]>
Date: Mon, 13 Jul 2026 15:46:52 +0000
Subject: [PATCH] [lldb][AArch64][NFC] Make watchpoint adjustment more clear

NativeRegisterContextDBReg_arm64::AdjustWatchpoint has a way of
adjusting the range of a watchpoint to fit hardware expectations
but it took me a while to figure out how it worked.

I've rewritten it in a way I think is easier to follow, with
more explanatory comments.
---
 .../NativeRegisterContextDBReg_arm64.cpp      | 65 +++++++++++++------
 1 file changed, 46 insertions(+), 19 deletions(-)

diff --git 
a/lldb/source/Plugins/Process/Utility/NativeRegisterContextDBReg_arm64.cpp 
b/lldb/source/Plugins/Process/Utility/NativeRegisterContextDBReg_arm64.cpp
index f2f772ad615c9..fff11c47c1395 100644
--- a/lldb/source/Plugins/Process/Utility/NativeRegisterContextDBReg_arm64.cpp
+++ b/lldb/source/Plugins/Process/Utility/NativeRegisterContextDBReg_arm64.cpp
@@ -12,6 +12,8 @@
 #include "lldb/Utility/Log.h"
 #include "lldb/Utility/RegisterValue.h"
 
+#include "llvm/Support/MathExtras.h"
+
 using namespace lldb_private;
 
 uint32_t
@@ -36,30 +38,55 @@ 
NativeRegisterContextDBReg_arm64::GetWatchpointSize(uint32_t wp_index) {
 std::optional<NativeRegisterContextDBReg::WatchpointDetails>
 NativeRegisterContextDBReg_arm64::AdjustWatchpoint(
     const WatchpointDetails &details) {
+  // A BAS watchpoint's size must be a power of 2 that is no greater than 8.
+  const size_t max_size = 8;
   size_t size = details.size;
-  lldb::addr_t addr = details.addr;
-  // Check if size has a valid hardware watchpoint length.
-  if (size != 1 && size != 2 && size != 4 && size != 8)
+  if (!llvm::isPowerOf2_64(size) || size > max_size)
     return std::nullopt;
 
-  // Check 8-byte alignment for hardware watchpoint target address. Below is a
-  // hack to recalculate address and size in order to make sure we can watch
-  // non 8-byte aligned addresses as well.
-  if (addr & 0x07) {
-    uint8_t watch_mask = (addr & 0x07) + size;
-
-    if (watch_mask > 0x08)
-      return std::nullopt;
+  // The start address must be aligned to 8 bytes.
+  lldb::addr_t addr = details.addr;
+  const size_t misalignment = addr & (max_size - 1);
+  if (misalignment == 0)
+    return details;
+
+  // The start address is not aligned, but we might be able to expand the
+  // watched range backwards to the previous 8 byte aligned address while
+  // keeping the size <= 8 bytes.
+  //
+  //  Aligned Address
+  //  |     /--------- Start Address
+  // [ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
+  //  |     |<-------- Size ------->|
+  //  |<----- Aligned Size -------->|
+  //
+  size_t aligned_size = misalignment + size;
+  if (aligned_size > max_size) {
+    // The range does not fit within a single BAS watchpoint, reject it.
+    return std::nullopt;
+  }
 
-    if (watch_mask <= 0x02)
-      size = 2;
-    else if (watch_mask <= 0x04)
-      size = 4;
-    else
-      size = 8;
+  // Size must still be a power of 2. After correcting this, sometimes we will
+  // watch bytes before and after the intended range. Stops in these extra 
bytes
+  // will be filtered out.
+  //
+  // For example, A is an aligned address and S is the start address. You want
+  // to watch the 1 byte at address S.
+  // [A][ ][S][ ]
+  //       { }
+  // This is misaligned by 2, so our aligned size is 3.
+  // [A][ ][S][ ]
+  // {       }
+  // We cannot watch 3 bytes but we can watch 4.
+  // [A][ ][S][ ]
+  // {          }
+  // The watched range is expanded before and after S.
+
+  // Round up to a power of 2 size.
+  size = llvm::PowerOf2Ceil(aligned_size);
+  // Align the address down.
+  addr -= misalignment;
 
-    addr = addr & (~0x07);
-  }
   return WatchpointDetails{size, addr};
 }
 

_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to