mirror of
https://github.com/go-delve/delve.git
synced 2025-11-02 12:59:01 +08:00
proc/native/linux: try to use process_vm_readv/writev
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.
This commit is contained in:
@ -90,7 +90,14 @@ func (t *Thread) WriteMemory(addr uintptr, data []byte) (written int, err error)
|
||||
if len(data) == 0 {
|
||||
return
|
||||
}
|
||||
t.dbp.execPtraceFunc(func() { written, err = sys.PtracePokeData(t.ID, addr, data) })
|
||||
// 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
|
||||
}
|
||||
|
||||
@ -101,9 +108,9 @@ func (t *Thread) ReadMemory(data []byte, addr uintptr) (n int, err error) {
|
||||
if len(data) == 0 {
|
||||
return
|
||||
}
|
||||
t.dbp.execPtraceFunc(func() { _, err = sys.PtracePeekData(t.ID, addr, data) })
|
||||
if err == nil {
|
||||
n = len(data)
|
||||
n, _ = ProcessVmRead(t.ID, addr, data)
|
||||
if n == 0 {
|
||||
t.dbp.execPtraceFunc(func() { n, err = sys.PtracePeekData(t.ID, addr, data) })
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user