style: modernize to splitseq, cut, min, maps.copy and range-over-int

This commit is contained in:
2026-08-29 16:04:32 +02:00
parent eacf33d0f7
commit 9beff4ae85
40 changed files with 90 additions and 234 deletions
+5 -3
View File
@@ -5,6 +5,8 @@
package debug
import "strings"
import "fmt"
// Breakpoint is one INT3 breakpoint in the debuggee.
@@ -119,7 +121,7 @@ func (bm *Breakpoints) Info() string {
if len(bm.bps) == 0 {
return "no breakpoints set\n"
}
result := ""
var result strings.Builder
i := 0
for _, bp := range bm.bps {
i++
@@ -135,9 +137,9 @@ func (bm *Breakpoints) Info() string {
if bp.Cond != nil {
cond = fmt.Sprintf(" if %s %s %#x", bp.Cond.Reg, bp.Cond.Op, bp.Cond.Value)
}
result += fmt.Sprintf(" %d: %s at %#x [%s, %d hits]%s\n", i, label, bp.Addr, status, bp.hits, cond)
result.WriteString(fmt.Sprintf(" %d: %s at %#x [%s, %d hits]%s\n", i, label, bp.Addr, status, bp.hits, cond))
}
return result
return result.String()
}
// Clear removes the breakpoint at addr, restoring the original byte.
+2 -2
View File
@@ -279,7 +279,7 @@ func TestWatchpointSlotTracking(t *testing.T) {
s := &Session{}
// All four slots are free initially.
for i := 0; i < 4; i++ {
for i := range 4 {
if s.IsWatchpointSlotUsed(i) {
t.Errorf("slot %d should be free initially", i)
}
@@ -317,7 +317,7 @@ func TestWatchpointSlotTracking(t *testing.T) {
}
// Mark all slots used: FindFreeWatchpointSlot returns -1.
for i := 0; i < 4; i++ {
for i := range 4 {
wpSlots[i] = true
}
if got := s.FindFreeWatchpointSlot(); got != -1 {
+5 -4
View File
@@ -7,6 +7,7 @@ package debug
import (
"fmt"
"strings"
"golang.org/x/arch/x86/x86asm"
)
@@ -34,19 +35,19 @@ func (s *Session) Disassemble(addr uint64) (string, int, error) {
// DisassembleN decodes up to n instructions starting at addr and returns
// them as a formatted string with addresses and byte offsets.
func (s *Session) DisassembleN(addr uint64, n int) string {
var result string
var result strings.Builder
pc := addr
for range n {
text, length, err := s.Disassemble(pc)
if err != nil {
result += fmt.Sprintf(" %#08x: <error: %v>\n", pc, err)
result.WriteString(fmt.Sprintf(" %#08x: <error: %v>\n", pc, err))
break
}
result += fmt.Sprintf(" %#08x: %s\n", pc, text)
result.WriteString(fmt.Sprintf(" %#08x: %s\n", pc, text))
if length == 0 {
length = 1
}
pc += uint64(length)
}
return result
return result.String()
}
+5 -8
View File
@@ -72,7 +72,7 @@ func LaunchWithBuffers(gasmBin, asmPath, funcName string, args []byte, bufSpec s
s := &Session{pid: cmd.Process.Pid, cmd: cmd}
readyFile := filepath.Join(tmpDir, "ready")
for i := 0; i < 500; i++ {
for range 500 {
if _, err := os.Stat(readyFile); err == nil {
break
}
@@ -86,7 +86,7 @@ func LaunchWithBuffers(gasmBin, asmPath, funcName string, args []byte, bufSpec s
}
entryFile := filepath.Join(tmpDir, "entry")
for i := 0; i < 500; i++ {
for range 500 {
if _, err := os.Stat(entryFile); err == nil {
break
}
@@ -115,7 +115,7 @@ func LaunchWithBuffers(gasmBin, asmPath, funcName string, args []byte, bufSpec s
if bufSpec != "" {
addrFile := filepath.Join(tmpDir, "bufaddrs")
if data, err := os.ReadFile(addrFile); err == nil {
for _, line := range strings.Split(strings.TrimSpace(string(data)), "\n") {
for line := range strings.SplitSeq(strings.TrimSpace(string(data)), "\n") {
var addr uint64
if _, err := fmt.Sscanf(line, "%d", &addr); err == nil {
bufAddrs = append(bufAddrs, addr)
@@ -190,10 +190,7 @@ func (s *Session) ReadMemory(addr uint64, length int) ([]byte, error) {
// WriteMemory writes bytes to the debuggee's memory at addr.
func (s *Session) WriteMemory(addr uint64, data []byte) error {
for i := 0; i < len(data); i += 8 {
end := i + 8
if end > len(data) {
end = len(data)
}
end := min(i+8, len(data))
var word uint64
for j := 0; j < end-i; j++ {
word |= uint64(data[i+j]) << (8 * j)
@@ -275,7 +272,7 @@ func findRWXMapping(pid int) uint64 {
if err != nil {
return 0
}
for _, line := range strings.Split(string(data), "\n") {
for line := range strings.SplitSeq(string(data), "\n") {
fields := strings.Fields(line)
if len(fields) < 2 {
continue
+2 -2
View File
@@ -89,8 +89,8 @@ func (s *Session) GetVectorRegs() (VectorRegs, error) {
if err != nil {
return v, err
}
for i := 0; i < 16; i++ {
for j := 0; j < 16; j++ {
for i := range 16 {
for j := range 16 {
v.YMM[i][j] = fp.XMM[i][j]
}
}
+2 -5
View File
@@ -297,7 +297,7 @@ func REPL(s *Session, bm *Breakpoints, codeBase uint64, funcOffset, funcSize, ar
continue
}
if v > 255 {
for j := 0; j < 8; j++ {
for j := range 8 {
bytes = append(bytes, byte(v>>(8*j)))
}
} else {
@@ -482,10 +482,7 @@ func REPL(s *Session, bm *Breakpoints, codeBase uint64, funcOffset, funcSize, ar
func hexDump(addr uint64, data []byte) {
for i := 0; i < len(data); i += 16 {
end := i + 16
if end > len(data) {
end = len(data)
}
end := min(i+16, len(data))
fmt.Printf(" %#08x:", addr+uint64(i))
for j := i; j < i+16; j++ {
if j < end {
+1 -1
View File
@@ -61,7 +61,7 @@ func (s *Session) StopInfo() (StopReason, uint64) {
case trapBRKPT:
return StopBreakpoint, 0
case trapHWBRKPT:
addr := *(*uint64)(unsafe.Pointer(uintptr(unsafe.Pointer(&info)) + 16))
addr := *(*uint64)(unsafe.Add(unsafe.Pointer(&info), 16))
return StopWatchpoint, addr
default:
return StopSingleStep, 0
+1 -1
View File
@@ -37,7 +37,7 @@ func setupBuffers(spec string, args []byte, argSize int, tmpDir string) ([]byte,
pattern string
}
var specs []bufSpec
for _, part := range strings.Split(spec, ",") {
for part := range strings.SplitSeq(spec, ",") {
fields := strings.SplitN(part, ":", 3)
if len(fields) != 3 {
continue
+2 -2
View File
@@ -36,7 +36,7 @@ func newMockTracer() *mockTracer {
func (m *mockTracer) Peek(addr uint64) (uint64, error) {
m.peeks = append(m.peeks, addr)
var val uint64
for i := uint64(0); i < 8; i++ {
for i := range uint64(8) {
val |= uint64(m.mem[addr+i]) << (i * 8)
}
return val, nil
@@ -47,7 +47,7 @@ func (m *mockTracer) Poke(addr uint64, val uint64) error {
addr uint64
val uint64
}{addr, val})
for i := uint64(0); i < 8; i++ {
for i := range uint64(8) {
m.mem[addr+i] = byte(val >> (i * 8))
}
return nil
+2 -2
View File
@@ -26,7 +26,7 @@ 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++ {
for i := range 4 {
if !wpSlots[i] {
return i
}
@@ -122,7 +122,7 @@ func (s *Session) ClearWatchpoint(slot int) error {
// ClearAllWatchpoints removes all hardware watchpoints.
func (s *Session) ClearAllWatchpoints() error {
for slot := 0; slot < 4; slot++ {
for slot := range 4 {
if wpSlots[slot] {
if err := s.ClearWatchpoint(slot); err != nil {
return err