feat(cmd): add gasm dis and share the decoder with the debugger

This commit is contained in:
2026-09-14 18:47:08 +02:00
parent 50db6615b2
commit edc20ffa97
7 changed files with 405 additions and 26 deletions
+6 -11
View File
@@ -9,27 +9,22 @@ import (
"fmt"
"strings"
"golang.org/x/arch/x86/x86asm"
"sourcedock.dev/petrbalvin/gasm-devkit/arch"
"sourcedock.dev/petrbalvin/gasm-devkit/disasm"
)
// Disassemble decodes the instruction at the given address in the debuggee's
// memory and returns its text representation and length in bytes.
func (s *Session) Disassemble(addr uint64) (string, int, error) {
// Read up to 15 bytes (max x86 instruction length).
mem, err := s.ReadMemory(addr, 15)
if err != nil {
// Try a shorter read if we're near a page boundary.
mem, err = s.ReadMemory(addr, 1)
if err != nil {
return "", 0, err
}
return "", 0, err
}
inst, err := x86asm.Decode(mem, 64)
ins, err := disasm.Decode(arch.AMD64, mem, addr)
if err != nil {
return "???", 1, nil
return "", 0, err
}
text := x86asm.IntelSyntax(inst, addr, nil)
return text, inst.Len, nil
return ins.Text, ins.Len, nil
}
// DisassembleN decodes up to n instructions starting at addr and returns
+5 -5
View File
@@ -8,7 +8,8 @@ package debug
import (
"fmt"
"golang.org/x/arch/arm64/arm64asm"
"sourcedock.dev/petrbalvin/gasm-devkit/arch"
"sourcedock.dev/petrbalvin/gasm-devkit/disasm"
)
// Disassemble decodes the instruction at the given address in the debuggee's
@@ -18,12 +19,11 @@ func (s *Session) Disassemble(addr uint64) (string, int, error) {
if err != nil {
return "", 0, err
}
inst, err := arm64asm.Decode(mem)
ins, err := disasm.Decode(arch.ARM64, mem, addr)
if err != nil {
return "???", 4, nil
return "", 0, err
}
text := arm64asm.GoSyntax(inst, addr, nil, nil)
return text, 4, nil
return ins.Text, ins.Len, nil
}
// DisassembleN decodes up to n instructions starting at addr.
+5 -5
View File
@@ -8,7 +8,8 @@ package debug
import (
"fmt"
"golang.org/x/arch/loong64/loong64asm"
"sourcedock.dev/petrbalvin/gasm-devkit/arch"
"sourcedock.dev/petrbalvin/gasm-devkit/disasm"
)
// Disassemble decodes the instruction at the given address in the debuggee's
@@ -18,12 +19,11 @@ func (s *Session) Disassemble(addr uint64) (string, int, error) {
if err != nil {
return "", 0, err
}
inst, err := loong64asm.Decode(mem)
ins, err := disasm.Decode(arch.LOONG64, mem, addr)
if err != nil {
return "???", 4, nil
return "", 0, err
}
text := loong64asm.GoSyntax(inst, addr, nil)
return text, 4, nil
return ins.Text, ins.Len, nil
}
// DisassembleN decodes up to n instructions starting at addr.
+5 -5
View File
@@ -8,7 +8,8 @@ package debug
import (
"fmt"
"golang.org/x/arch/riscv64/riscv64asm"
"sourcedock.dev/petrbalvin/gasm-devkit/arch"
"sourcedock.dev/petrbalvin/gasm-devkit/disasm"
)
// Disassemble decodes the instruction at the given address in the debuggee's
@@ -18,12 +19,11 @@ func (s *Session) Disassemble(addr uint64) (string, int, error) {
if err != nil {
return "", 0, err
}
inst, err := riscv64asm.Decode(mem)
ins, err := disasm.Decode(arch.RISCV, mem, addr)
if err != nil {
return "???", 4, nil
return "", 0, err
}
text := riscv64asm.GoSyntax(inst, addr, nil, nil)
return text, inst.Len, nil
return ins.Text, ins.Len, nil
}
// DisassembleN decodes up to n instructions starting at addr.