2026-08-20 23:57:10 +02:00
|
|
|
// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
|
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
|
|
//go:build linux && loong64
|
|
|
|
|
|
|
|
|
|
package debug
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2026-09-19 23:49:19 +02:00
|
|
|
"strings"
|
2026-08-20 23:57:10 +02:00
|
|
|
|
2026-09-26 11:08:43 +02:00
|
|
|
"sourcedock.dev/petrbalvin/gasm-sdk/arch"
|
|
|
|
|
"sourcedock.dev/petrbalvin/gasm-sdk/disasm"
|
2026-08-20 23:57:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
|
mem, err := s.ReadMemory(addr, 4)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", 0, err
|
|
|
|
|
}
|
2026-09-14 18:47:08 +02:00
|
|
|
ins, err := disasm.Decode(arch.LOONG64, mem, addr)
|
2026-08-20 23:57:10 +02:00
|
|
|
if err != nil {
|
2026-09-14 18:47:08 +02:00
|
|
|
return "", 0, err
|
2026-08-20 23:57:10 +02:00
|
|
|
}
|
2026-09-14 18:47:08 +02:00
|
|
|
return ins.Text, ins.Len, nil
|
2026-08-20 23:57:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DisassembleN decodes up to n instructions starting at addr.
|
|
|
|
|
func (s *Session) DisassembleN(addr uint64, n int) string {
|
|
|
|
|
var result string
|
|
|
|
|
pc := addr
|
2026-08-30 11:27:54 +02:00
|
|
|
for range n {
|
2026-08-20 23:57:10 +02:00
|
|
|
text, length, err := s.Disassemble(pc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
result += fmt.Sprintf(" %#08x: <error: %v>\n", pc, err)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
result += fmt.Sprintf(" %#08x: %s\n", pc, text)
|
|
|
|
|
if length == 0 {
|
|
|
|
|
length = 4
|
|
|
|
|
}
|
|
|
|
|
pc += uint64(length)
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
2026-09-19 23:49:19 +02:00
|
|
|
|
|
|
|
|
// isCallInsn reports whether disassembled text (loong64asm.GoSyntax) is a
|
|
|
|
|
// call. GoSyntax renders bl and jirl calls as CALL (jirl returns print
|
|
|
|
|
// RET); the native mnemonics are accepted too. The first token must match
|
|
|
|
|
// exactly: a "bl" prefix would catch bltz and other branches.
|
|
|
|
|
func isCallInsn(text string) bool {
|
|
|
|
|
m, _, _ := strings.Cut(text, " ")
|
|
|
|
|
switch strings.ToLower(m) {
|
|
|
|
|
case "call", "bl", "jirl":
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|