
This change adds `ProcessVmRead` and `ProcessVmWrite` wrappers around the syscalls `process_vm_readv` and `process_vm_writev`, available since Linux 3.2. These follow the same permission model as `ptrace`, but they don't actually require being attached, which means they can be called directly from any thread in the debugger. They also use `iovec` to write entire blocks at once, rather than having to peek/poke each `uintptr`. These wrappers are used in `Thread.ReadMemory` and `WriteMemory`, still falling back to `ptrace` if that fails for any reason. Notably, `process_vm_writev` respects memory protection, so it can't modify read-only memory like `ptrace`. This frequently occurs when writing breakpoints in read-only `.text`, so to avoid a lot of wasted `EFAULT` calls, we only try `process_vm_writev` for larger writes.
117 lines
2.6 KiB
Go
117 lines
2.6 KiB
Go
package native
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
sys "golang.org/x/sys/unix"
|
|
|
|
"github.com/go-delve/delve/pkg/proc"
|
|
)
|
|
|
|
type WaitStatus sys.WaitStatus
|
|
|
|
// OSSpecificDetails hold Linux specific
|
|
// process details.
|
|
type OSSpecificDetails struct {
|
|
delayedSignal int
|
|
registers sys.PtraceRegs
|
|
running bool
|
|
}
|
|
|
|
func (t *Thread) stop() (err error) {
|
|
err = sys.Tgkill(t.dbp.pid, t.ID, sys.SIGSTOP)
|
|
if err != nil {
|
|
err = fmt.Errorf("stop err %s on thread %d", err, t.ID)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// Stopped returns whether the thread is stopped at
|
|
// the operating system level.
|
|
func (t *Thread) Stopped() bool {
|
|
state := status(t.ID, t.dbp.os.comm)
|
|
return state == StatusTraceStop || state == StatusTraceStopT
|
|
}
|
|
|
|
func (t *Thread) resume() error {
|
|
sig := t.os.delayedSignal
|
|
t.os.delayedSignal = 0
|
|
return t.resumeWithSig(sig)
|
|
}
|
|
|
|
func (t *Thread) resumeWithSig(sig int) (err error) {
|
|
t.os.running = true
|
|
t.dbp.execPtraceFunc(func() { err = PtraceCont(t.ID, sig) })
|
|
return
|
|
}
|
|
|
|
func (t *Thread) singleStep() (err error) {
|
|
for {
|
|
t.dbp.execPtraceFunc(func() { err = sys.PtraceSingleStep(t.ID) })
|
|
if err != nil {
|
|
return err
|
|
}
|
|
wpid, status, err := t.dbp.waitFast(t.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if (status == nil || status.Exited()) && wpid == t.dbp.pid {
|
|
t.dbp.postExit()
|
|
rs := 0
|
|
if status != nil {
|
|
rs = status.ExitStatus()
|
|
}
|
|
return proc.ErrProcessExited{Pid: t.dbp.pid, Status: rs}
|
|
}
|
|
if wpid == t.ID && status.StopSignal() == sys.SIGTRAP {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
func (t *Thread) Blocked() bool {
|
|
regs, err := t.Registers(false)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
pc := regs.PC()
|
|
fn := t.BinInfo().PCToFunc(pc)
|
|
if fn != nil && ((fn.Name == "runtime.futex") || (fn.Name == "runtime.usleep") || (fn.Name == "runtime.clone")) {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (t *Thread) WriteMemory(addr uintptr, data []byte) (written int, err error) {
|
|
if t.dbp.exited {
|
|
return 0, proc.ErrProcessExited{Pid: t.dbp.pid}
|
|
}
|
|
if len(data) == 0 {
|
|
return
|
|
}
|
|
// ProcessVmWrite can't poke read-only memory like ptrace, so don't
|
|
// even bother for small writes -- likely breakpoints and such.
|
|
if len(data) > sys.SizeofPtr {
|
|
written, _ = ProcessVmWrite(t.ID, addr, data)
|
|
}
|
|
if written == 0 {
|
|
t.dbp.execPtraceFunc(func() { written, err = sys.PtracePokeData(t.ID, addr, data) })
|
|
}
|
|
return
|
|
}
|
|
|
|
func (t *Thread) ReadMemory(data []byte, addr uintptr) (n int, err error) {
|
|
if t.dbp.exited {
|
|
return 0, proc.ErrProcessExited{Pid: t.dbp.pid}
|
|
}
|
|
if len(data) == 0 {
|
|
return
|
|
}
|
|
n, _ = ProcessVmRead(t.ID, addr, data)
|
|
if n == 0 {
|
|
t.dbp.execPtraceFunc(func() { n, err = sys.PtracePeekData(t.ID, addr, data) })
|
|
}
|
|
return
|
|
}
|