2020-03-10 16:34:40 +00:00
|
|
|
|
package native
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"syscall"
|
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
|
|
sys "golang.org/x/sys/unix"
|
|
|
|
|
|
2020-12-14 17:39:01 +00:00
|
|
|
|
"github.com/go-delve/delve/pkg/proc/amd64util"
|
2020-03-10 16:34:40 +00:00
|
|
|
|
)
|
|
|
|
|
|
2020-03-26 12:05:09 +00:00
|
|
|
|
// ptraceGetRegset returns floating point registers of the specified thread
|
2020-03-10 16:34:40 +00:00
|
|
|
|
// using PTRACE.
|
|
|
|
|
// See i386_linux_fetch_inferior_registers in gdb/i386-linux-nat.c.html
|
|
|
|
|
// and i386_supply_xsave in gdb/i386-tdep.c.html
|
|
|
|
|
// and Section 13.1 (and following) of Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 1: Basic Architecture
|
2020-12-14 17:39:01 +00:00
|
|
|
|
func ptraceGetRegset(tid int) (regset amd64util.AMD64Xstate, err error) {
|
|
|
|
|
_, _, err = syscall.Syscall6(syscall.SYS_PTRACE, sys.PTRACE_GETFPREGS, uintptr(tid), uintptr(0), uintptr(unsafe.Pointer(®set.AMD64PtraceFpRegs)), 0, 0)
|
2020-03-10 16:34:40 +00:00
|
|
|
|
if err == syscall.Errno(0) || err == syscall.ENODEV {
|
|
|
|
|
// ignore ENODEV, it just means this CPU doesn't have X87 registers (??)
|
|
|
|
|
err = nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 17:39:01 +00:00
|
|
|
|
xstateargs := make([]byte, amd64util.AMD64XstateMaxSize())
|
|
|
|
|
iov := sys.Iovec{Base: &xstateargs[0], Len: uint32(len(xstateargs))}
|
2020-03-10 16:34:40 +00:00
|
|
|
|
_, _, err = syscall.Syscall6(syscall.SYS_PTRACE, sys.PTRACE_GETREGSET, uintptr(tid), _NT_X86_XSTATE, uintptr(unsafe.Pointer(&iov)), 0, 0)
|
|
|
|
|
if err != syscall.Errno(0) {
|
2021-01-29 21:39:33 +00:00
|
|
|
|
if err == syscall.ENODEV || err == syscall.EIO || err == syscall.EINVAL {
|
2020-03-10 16:34:40 +00:00
|
|
|
|
// ignore ENODEV, it just means this CPU or kernel doesn't support XSTATE, see https://github.com/go-delve/delve/issues/1022
|
|
|
|
|
// also ignore EIO, it means that we are running on an old kernel (pre 2.6.34) and PTRACE_GETREGSET is not implemented
|
2021-01-29 21:39:33 +00:00
|
|
|
|
// also ignore EINVAL, it means the kernel itself does not support the NT_X86_XSTATE argument (but does support PTRACE_GETREGSET)
|
2020-03-10 16:34:40 +00:00
|
|
|
|
err = nil
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
err = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
regset.Xsave = xstateargs[:iov.Len]
|
2020-12-14 17:39:01 +00:00
|
|
|
|
err = amd64util.AMD64XstateRead(regset.Xsave, false, ®set)
|
2020-03-10 16:34:40 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 12:05:09 +00:00
|
|
|
|
// ptraceGetTls return the addr of tls by PTRACE_GET_THREAD_AREA for specify thread.
|
2024-07-11 15:03:32 +00:00
|
|
|
|
// See https://man7.org/linux/man-pages/man2/ptrace.2.html for detail about PTRACE_GET_THREAD_AREA.
|
2024-07-12 19:12:44 +00:00
|
|
|
|
// struct user_desc at https://go.dev/src/runtime/sys_linux_386.s
|
2022-06-17 17:08:11 +00:00
|
|
|
|
//
|
|
|
|
|
// type UserDesc struct {
|
|
|
|
|
// EntryNumber uint32
|
|
|
|
|
// BaseAddr uint32
|
|
|
|
|
// Limit uint32
|
|
|
|
|
// Flag uint32
|
|
|
|
|
// }
|
2020-03-26 12:05:09 +00:00
|
|
|
|
func ptraceGetTls(gs int32, tid int) (uint32, error) {
|
2020-03-10 16:34:40 +00:00
|
|
|
|
ud := [4]uint32{}
|
|
|
|
|
|
|
|
|
|
// Gs usually is 0x33
|
|
|
|
|
_, _, err := syscall.Syscall6(syscall.SYS_PTRACE, sys.PTRACE_GET_THREAD_AREA, uintptr(tid), uintptr(gs>>3), uintptr(unsafe.Pointer(&ud)), 0, 0)
|
|
|
|
|
if err == syscall.ENODEV || err == syscall.EIO {
|
|
|
|
|
return 0, fmt.Errorf("%s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return uint32(ud[1]), nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 12:05:09 +00:00
|
|
|
|
// processVmRead calls process_vm_readv
|
|
|
|
|
func processVmRead(tid int, addr uintptr, data []byte) (int, error) {
|
2020-03-10 16:34:40 +00:00
|
|
|
|
len_iov := uint32(len(data))
|
|
|
|
|
local_iov := sys.Iovec{Base: &data[0], Len: len_iov}
|
2022-03-15 21:33:12 +00:00
|
|
|
|
remote_iov := remoteIovec{base: addr, len: uintptr(len_iov)}
|
2023-02-14 17:32:13 +00:00
|
|
|
|
n, _, err := syscall.Syscall6(sys.SYS_PROCESS_VM_READV, uintptr(tid), uintptr(unsafe.Pointer(&local_iov)), 1, uintptr(unsafe.Pointer(&remote_iov)), 1, 0)
|
2020-03-10 16:34:40 +00:00
|
|
|
|
if err != syscall.Errno(0) {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
return int(n), nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 12:05:09 +00:00
|
|
|
|
// processVmWrite calls process_vm_writev
|
|
|
|
|
func processVmWrite(tid int, addr uintptr, data []byte) (int, error) {
|
2020-03-10 16:34:40 +00:00
|
|
|
|
len_iov := uint32(len(data))
|
|
|
|
|
local_iov := sys.Iovec{Base: &data[0], Len: len_iov}
|
2022-03-15 21:33:12 +00:00
|
|
|
|
remote_iov := remoteIovec{base: addr, len: uintptr(len_iov)}
|
2023-02-14 17:32:13 +00:00
|
|
|
|
n, _, err := syscall.Syscall6(sys.SYS_PROCESS_VM_WRITEV, uintptr(tid), uintptr(unsafe.Pointer(&local_iov)), 1, uintptr(unsafe.Pointer(&remote_iov)), 1, 0)
|
2020-03-10 16:34:40 +00:00
|
|
|
|
if err != syscall.Errno(0) {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
return int(n), nil
|
|
|
|
|
}
|