2019-07-13 01:28:04 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -lutil
|
|
|
|
//#include <sys/types.h>
|
|
|
|
//#include <sys/ptrace.h>
|
|
|
|
//
|
|
|
|
// #include <stdlib.h>
|
|
|
|
// #include "ptrace_freebsd_amd64.h"
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
sys "golang.org/x/sys/unix"
|
|
|
|
|
2020-12-14 17:39:01 +00:00
|
|
|
"github.com/go-delve/delve/pkg/proc/amd64util"
|
2019-07-13 01:28:04 +00:00
|
|
|
)
|
|
|
|
|
2020-03-26 12:05:09 +00:00
|
|
|
// ptraceAttach executes the sys.PtraceAttach call.
|
2019-07-13 01:28:04 +00:00
|
|
|
// pid must be a PID, not a LWPID
|
2020-03-26 12:05:09 +00:00
|
|
|
func ptraceAttach(pid int) error {
|
2019-07-13 01:28:04 +00:00
|
|
|
return sys.PtraceAttach(pid)
|
|
|
|
}
|
|
|
|
|
2020-03-26 12:05:09 +00:00
|
|
|
// ptraceDetach calls ptrace(PTRACE_DETACH).
|
|
|
|
func ptraceDetach(pid int) error {
|
2019-07-13 01:28:04 +00:00
|
|
|
return sys.PtraceDetach(pid)
|
|
|
|
}
|
|
|
|
|
2020-03-26 12:05:09 +00:00
|
|
|
// ptraceCont executes ptrace PTRACE_CONT
|
2019-07-13 01:28:04 +00:00
|
|
|
// id may be a PID or an LWPID
|
2020-03-26 12:05:09 +00:00
|
|
|
func ptraceCont(id, sig int) error {
|
2019-07-13 01:28:04 +00:00
|
|
|
return sys.PtraceCont(id, sig)
|
|
|
|
}
|
|
|
|
|
2020-03-26 12:05:09 +00:00
|
|
|
// ptraceSingleStep executes ptrace PTRACE_SINGLE_STEP.
|
2019-07-13 01:28:04 +00:00
|
|
|
// id may be a PID or an LWPID
|
2020-03-26 12:05:09 +00:00
|
|
|
func ptraceSingleStep(id int) error {
|
2019-07-13 01:28:04 +00:00
|
|
|
return sys.PtraceSingleStep(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a list of the thread ids of a process
|
2020-03-26 12:05:09 +00:00
|
|
|
func ptraceGetLwpList(pid int) (tids []int32) {
|
2019-07-13 01:28:04 +00:00
|
|
|
num_lwps, _ := C.ptrace_get_num_lwps(C.int(pid))
|
|
|
|
tids = make([]int32, num_lwps)
|
|
|
|
n, _ := C.ptrace_get_lwp_list(C.int(pid), (*C.int)(unsafe.Pointer(&tids[0])), C.size_t(num_lwps))
|
|
|
|
return tids[0:n]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get info of the thread that caused wpid's process to stop.
|
|
|
|
func ptraceGetLwpInfo(wpid int) (info sys.PtraceLwpInfoStruct, err error) {
|
|
|
|
err = sys.PtraceLwpInfo(wpid, uintptr(unsafe.Pointer(&info)))
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
2020-12-14 17:39:01 +00:00
|
|
|
func ptraceGetRegset(id int) (regset amd64util.AMD64Xstate, err error) {
|
2019-07-13 01:28:04 +00:00
|
|
|
_, _, err = syscall.Syscall6(syscall.SYS_PTRACE, sys.PTRACE_GETFPREGS, uintptr(id), uintptr(unsafe.Pointer(®set.AMD64PtraceFpRegs)), 0, 0, 0)
|
|
|
|
if err == syscall.Errno(0) || err == syscall.ENODEV {
|
|
|
|
var xsave_len C.size_t
|
|
|
|
xsave, _ := C.ptrace_get_xsave(C.int(id), &xsave_len)
|
|
|
|
defer C.free(unsafe.Pointer(xsave))
|
|
|
|
if xsave != nil {
|
|
|
|
xsave_sl := C.GoBytes(unsafe.Pointer(xsave), C.int(xsave_len))
|
2020-12-14 17:39:01 +00:00
|
|
|
err = amd64util.AMD64XstateRead(xsave_sl, false, ®set)
|
2019-07-13 01:28:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// id may be a PID or an LWPID
|
|
|
|
func ptraceReadData(id int, addr uintptr, data []byte) (n int, err error) {
|
|
|
|
return sys.PtraceIO(sys.PIOD_READ_D, id, addr, data, len(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
// id may be a PID or an LWPID
|
|
|
|
func ptraceWriteData(id int, addr uintptr, data []byte) (n int, err error) {
|
|
|
|
return sys.PtraceIO(sys.PIOD_WRITE_D, id, addr, data, len(data))
|
|
|
|
}
|