refactor(debug): move watchpoint slot state into the session

This commit is contained in:
2026-09-14 18:22:00 +02:00
parent 909f874797
commit 4f190ee1a2
7 changed files with 38 additions and 51 deletions
+7 -10
View File
@@ -20,14 +20,11 @@ const (
WatchRead WatchpointType = 3 // trigger on read or write
)
// wpSlots tracks watchpoint slot occupancy (DR0-DR3).
var wpSlots [4]bool
// FindFreeWatchpointSlot returns the index of the first free watchpoint slot
// (0-3), or -1 if all four hardware watchpoints are in use.
func (s *Session) FindFreeWatchpointSlot() int {
for i := range 4 {
if !wpSlots[i] {
if !s.wpSlots[i] {
return i
}
}
@@ -39,7 +36,7 @@ func (s *Session) IsWatchpointSlotUsed(slot int) bool {
if slot < 0 || slot > 3 {
return false
}
return wpSlots[slot]
return s.wpSlots[slot]
}
// SetWatchpoint installs a hardware watchpoint on the given address.
@@ -47,7 +44,7 @@ func (s *Session) SetWatchpoint(slot int, addr uint64, typ WatchpointType, size
if slot < 0 || slot > 3 {
return fmt.Errorf("debug: watchpoint slot must be 0-3")
}
if wpSlots[slot] {
if s.wpSlots[slot] {
return fmt.Errorf("debug: watchpoint slot %d already in use", slot)
}
@@ -96,7 +93,7 @@ func (s *Session) SetWatchpoint(slot int, addr uint64, typ WatchpointType, size
if err := ptracePokeUser(s.pid, 0x38, dr7); err != nil {
return fmt.Errorf("debug: set DR7: %w", err)
}
wpSlots[slot] = true
s.wpSlots[slot] = true
return nil
}
@@ -105,7 +102,7 @@ func (s *Session) ClearWatchpoint(slot int) error {
if slot < 0 || slot > 3 {
return fmt.Errorf("debug: watchpoint slot must be 0-3")
}
if !wpSlots[slot] {
if !s.wpSlots[slot] {
return fmt.Errorf("debug: watchpoint slot %d is not in use", slot)
}
dr7, err := ptracePeekUser(s.pid, 0x38)
@@ -116,14 +113,14 @@ func (s *Session) ClearWatchpoint(slot int) error {
if err := ptracePokeUser(s.pid, 0x38, dr7); err != nil {
return err
}
wpSlots[slot] = false
s.wpSlots[slot] = false
return nil
}
// ClearAllWatchpoints removes all hardware watchpoints.
func (s *Session) ClearAllWatchpoints() error {
for slot := range 4 {
if wpSlots[slot] {
if s.wpSlots[slot] {
if err := s.ClearWatchpoint(slot); err != nil {
return err
}