The Go runtime can migrate the debuggee's target-mode goroutine off the process leader before PTRACE_TRACEME, which left the trace relation on a thread the session never addressed: its stops starved the waits on the leader, and a kill sequence that resumed nothing and then blocked in Wait4 hung the whole package. The debuggee now reports the traced thread in the launch handshake and parks with a thread-directed stop, every ptrace request and wait addresses that thread, a SIGURG arriving on a single-step resumes it as a single-step again instead of letting the tracee run uncontrolled, a resume rejected with ESRCH lifts a group-stop with SIGCONT and retries once, and Kill resumes, kills and reaps through non-blocking waits so it returns for a tracee in any state. Assisted-by: GLM 5.3
112 lines
2.8 KiB
Go
112 lines
2.8 KiB
Go
// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
//go:build linux
|
|
|
|
package debug
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
"unsafe"
|
|
)
|
|
|
|
// mapRWX maps code into a read-write-execute region.
|
|
func mapRWX(code []byte) ([]byte, error) {
|
|
const pageSize = 4096
|
|
size := (len(code) + pageSize - 1) &^ (pageSize - 1)
|
|
mem, err := syscall.Mmap(-1, 0, size,
|
|
syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC,
|
|
syscall.MAP_PRIVATE|syscall.MAP_ANON)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
copy(mem, code)
|
|
return mem, nil
|
|
}
|
|
|
|
// parkSelf stops the calling thread for the launch handshake. The stop is
|
|
// thread-directed on purpose: a process-directed SIGSTOP may be queued on
|
|
// any of the debuggee's untraced runtime threads, and delivering it there
|
|
// establishes a group-stop over the whole thread group, which leaves the
|
|
// traced leader in a plain stop the tracer cannot resume (PTRACE_CONT fails
|
|
// ESRCH) and the siblings stopped with nobody to continue them. Directed
|
|
// at the traced thread, the stop always surfaces as the signal-delivery-stop
|
|
// the handshake consumes.
|
|
func parkSelf() {
|
|
syscall.Tgkill(syscall.Getpid(), syscall.Gettid(), syscall.SIGSTOP)
|
|
}
|
|
|
|
// setupBuffers allocates buffers in the debuggee's memory.
|
|
func setupBuffers(spec string, args []byte, tmpDir string) ([]byte, error) {
|
|
type bufSpec struct {
|
|
name string
|
|
size int
|
|
pattern string
|
|
}
|
|
var specs []bufSpec
|
|
for part := range strings.SplitSeq(spec, ",") {
|
|
fields := strings.SplitN(part, ":", 3)
|
|
if len(fields) != 3 {
|
|
continue
|
|
}
|
|
size, err := strconv.Atoi(fields[1])
|
|
if err != nil || size <= 0 {
|
|
continue
|
|
}
|
|
specs = append(specs, bufSpec{name: fields[0], size: size, pattern: fields[2]})
|
|
}
|
|
|
|
if len(specs) == 0 {
|
|
return args, nil
|
|
}
|
|
|
|
var bufAddrs []uint64
|
|
for _, s := range specs {
|
|
buf, err := syscall.Mmap(-1, 0, s.size,
|
|
syscall.PROT_READ|syscall.PROT_WRITE,
|
|
syscall.MAP_PRIVATE|syscall.MAP_ANON)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("mmap buffer %s: %w", s.name, err)
|
|
}
|
|
fillBuffer(buf, s.pattern)
|
|
bufAddrs = append(bufAddrs, uint64(uintptr(unsafe.Pointer(&buf[0]))))
|
|
}
|
|
|
|
addrFile, err := os.Create(tmpDir + "/bufaddrs")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, addr := range bufAddrs {
|
|
fmt.Fprintf(addrFile, "%d\n", addr)
|
|
}
|
|
addrFile.Close()
|
|
|
|
return args, nil
|
|
}
|
|
|
|
// fillBuffer fills a buffer with the specified pattern.
|
|
func fillBuffer(buf []byte, pattern string) {
|
|
switch pattern {
|
|
case "zero":
|
|
case "ones":
|
|
for i := range buf {
|
|
buf[i] = 0xFF
|
|
}
|
|
case "seq":
|
|
for i := range buf {
|
|
buf[i] = byte(i)
|
|
}
|
|
default:
|
|
if data, err := hex.DecodeString(pattern); err == nil && len(data) > 0 {
|
|
for i := range buf {
|
|
buf[i] = data[i%len(data)]
|
|
}
|
|
}
|
|
}
|
|
}
|