github.com/undoio/delve@v1.9.0/pkg/proc/native/ptrace_freebsd.go (about) 1 package native 2 3 // #cgo LDFLAGS: -lutil 4 //#include <sys/types.h> 5 //#include <sys/ptrace.h> 6 // 7 // #include <stdlib.h> 8 // 9 import "C" 10 11 import ( 12 "unsafe" 13 14 sys "golang.org/x/sys/unix" 15 ) 16 17 // ptraceAttach executes the sys.PtraceAttach call. 18 // pid must be a PID, not a LWPID 19 func ptraceAttach(pid int) error { 20 return sys.PtraceAttach(pid) 21 } 22 23 // ptraceDetach calls ptrace(PTRACE_DETACH). 24 func ptraceDetach(pid int) error { 25 return sys.PtraceDetach(pid) 26 } 27 28 // ptraceCont executes ptrace PTRACE_CONT 29 // id may be a PID or an LWPID 30 func ptraceCont(id, sig int) error { 31 return sys.PtraceCont(id, sig) 32 } 33 34 // ptraceSingleStep executes ptrace PTRACE_SINGLE_STEP. 35 // id may be a PID or an LWPID 36 func ptraceSingleStep(id int) error { 37 return sys.PtraceSingleStep(id) 38 } 39 40 // Get a list of the thread ids of a process 41 func ptraceGetLwpList(pid int) (tids []int32) { 42 numLWPS := C.ptrace(C.PT_GETNUMLWPS, C.pid_t(pid), C.caddr_t(unsafe.Pointer(uintptr(0))), C.int(0)) 43 if numLWPS < 0 { 44 panic("PT_GETNUMLWPS failed") 45 } 46 tids = make([]int32, numLWPS) 47 n := C.ptrace(C.PT_GETLWPLIST, C.pid_t(pid), C.caddr_t(unsafe.Pointer(&tids[0])), C.int(numLWPS)) 48 if n < 0 { 49 panic("PT_GETLWPLIST failed") 50 } 51 return tids[0:n] 52 } 53 54 // Get info of the thread that caused wpid's process to stop. 55 func ptraceGetLwpInfo(wpid int) (info sys.PtraceLwpInfoStruct, err error) { 56 err = sys.PtraceLwpInfo(wpid, uintptr(unsafe.Pointer(&info))) 57 return info, err 58 } 59 60 // id may be a PID or an LWPID 61 func ptraceReadData(id int, addr uintptr, data []byte) (n int, err error) { 62 return sys.PtraceIO(sys.PIOD_READ_D, id, addr, data, len(data)) 63 } 64 65 // id may be a PID or an LWPID 66 func ptraceWriteData(id int, addr uintptr, data []byte) (n int, err error) { 67 return sys.PtraceIO(sys.PIOD_WRITE_D, id, addr, data, len(data)) 68 }