feat(debug): multi-architecture debugger support for arm64, riscv64, loong64

Assisted-by: MiMo V2.5 Pro
This commit is contained in:
2026-08-21 00:35:21 +02:00
parent 3acbdd6533
commit de9e211ff1
32 changed files with 2276 additions and 659 deletions
+21 -32
View File
@@ -11,11 +11,6 @@ import (
)
// Hardware watchpoint support via x86-64 debug registers (DR0-DR3, DR7).
//
// DR0-DR3 hold the watched addresses. DR7 is the control register:
// bits 0,2,4,6: local enable for DR0-DR3
// bits 16-17,20-21,24-25,28-29: R/W type (00=exec, 01=write, 11=read/write)
// bits 18-19,22-23,26-27,30-31: length (00=1, 01=2, 10=8, 11=4)
// WatchpointType selects what triggers the watchpoint.
type WatchpointType int
@@ -25,11 +20,14 @@ 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 := 0; i < 4; i++ {
if !s.wpSlots[i] {
if !wpSlots[i] {
return i
}
}
@@ -41,20 +39,18 @@ func (s *Session) IsWatchpointSlotUsed(slot int) bool {
if slot < 0 || slot > 3 {
return false
}
return s.wpSlots[slot]
return wpSlots[slot]
}
// SetWatchpoint installs a hardware watchpoint on the given address.
// slot is 0-3 (four hardware watchpoints available); the slot must be free.
func (s *Session) SetWatchpoint(slot int, addr uint64, typ WatchpointType, size int) error {
if slot < 0 || slot > 3 {
return fmt.Errorf("debug: watchpoint slot must be 0-3")
}
if s.wpSlots[slot] {
if wpSlots[slot] {
return fmt.Errorf("debug: watchpoint slot %d already in use", slot)
}
// Determine the length encoding.
var lenBits uint64
switch size {
case 1:
@@ -69,42 +65,38 @@ func (s *Session) SetWatchpoint(slot int, addr uint64, typ WatchpointType, size
return fmt.Errorf("debug: watchpoint size must be 1, 2, 4, or 8")
}
// Write the watched address to DR0-DR3.
var drAddr uintptr
switch slot {
case 0:
drAddr = 0x0 // DR0 offset in user_regs_struct
drAddr = 0x0
case 1:
drAddr = 0x8 // DR1
drAddr = 0x8
case 2:
drAddr = 0x10 // DR2
drAddr = 0x10
case 3:
drAddr = 0x18 // DR3
drAddr = 0x18
}
// PTRACE_POKEUSER writes to the debuggee's user area (includes debug regs).
if err := ptracePokeUser(s.pid, drAddr, addr); err != nil {
return fmt.Errorf("debug: set DR%d: %w", slot, err)
}
// Read the current DR7, set the enable and type bits, write it back.
dr7, err := ptracePeekUser(s.pid, 0x38) // DR7 offset
dr7, err := ptracePeekUser(s.pid, 0x38)
if err != nil {
return fmt.Errorf("debug: read DR7: %w", err)
}
enableBit := uint64(1) << (2 * slot) // local enable
rwBits := uint64(typ) << (16 + 4*slot) // R/W type
lenField := lenBits << (18 + 4*slot) // length
enableBit := uint64(1) << (2 * slot)
rwBits := uint64(typ) << (16 + 4*slot)
lenField := lenBits << (18 + 4*slot)
// Clear the existing bits for this slot, then set the new ones.
mask := ^((uint64(1) << (2 * slot)) | (uint64(3) << (16 + 4*slot)) | (uint64(3) << (18 + 4*slot)))
dr7 = (dr7 & mask) | enableBit | rwBits | lenField
if err := ptracePokeUser(s.pid, 0x38, dr7); err != nil {
return fmt.Errorf("debug: set DR7: %w", err)
}
s.wpSlots[slot] = true
wpSlots[slot] = true
return nil
}
@@ -113,26 +105,25 @@ func (s *Session) ClearWatchpoint(slot int) error {
if slot < 0 || slot > 3 {
return fmt.Errorf("debug: watchpoint slot must be 0-3")
}
if !s.wpSlots[slot] {
if !wpSlots[slot] {
return fmt.Errorf("debug: watchpoint slot %d is not in use", slot)
}
// Read DR7, clear the enable bit for this slot.
dr7, err := ptracePeekUser(s.pid, 0x38)
if err != nil {
return err
}
dr7 &^= uint64(1) << (2 * slot) // disable
dr7 &^= uint64(1) << (2 * slot)
if err := ptracePokeUser(s.pid, 0x38, dr7); err != nil {
return err
}
s.wpSlots[slot] = false
wpSlots[slot] = false
return nil
}
// ClearAllWatchpoints removes all hardware watchpoints.
func (s *Session) ClearAllWatchpoints() error {
for slot := 0; slot < 4; slot++ {
if s.wpSlots[slot] {
if wpSlots[slot] {
if err := s.ClearWatchpoint(slot); err != nil {
return err
}
@@ -141,9 +132,8 @@ func (s *Session) ClearAllWatchpoints() error {
return nil
}
// ptracePokeUser writes a value to the debuggee's user area at the given offset.
func ptracePokeUser(pid int, offset uintptr, val uint64) error {
const ptracePokeuser = 6 // PTRACE_POKEUSER
const ptracePokeuser = 6
_, _, errno := syscall.Syscall6(
syscall.SYS_PTRACE,
uintptr(ptracePokeuser),
@@ -158,9 +148,8 @@ func ptracePokeUser(pid int, offset uintptr, val uint64) error {
return nil
}
// ptracePeekUser reads a value from the debuggee's user area at the given offset.
func ptracePeekUser(pid int, offset uintptr) (uint64, error) {
const ptracePeekuser = 3 // PTRACE_PEEKUSER
const ptracePeekuser = 3
val, _, errno := syscall.Syscall6(
syscall.SYS_PTRACE,
uintptr(ptracePeekuser),