gitlab.com/Raven-IO/raven-delve@v1.22.4/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  	"syscall"
    13  	"unsafe"
    14  
    15  	sys "golang.org/x/sys/unix"
    16  )
    17  
    18  // ptraceAttach executes the sys.PtraceAttach call.
    19  // pid must be a PID, not a LWPID
    20  func ptraceAttach(pid int) error {
    21  	return sys.PtraceAttach(pid)
    22  }
    23  
    24  // ptraceDetach calls ptrace(PTRACE_DETACH).
    25  func ptraceDetach(pid int) error {
    26  	return sys.PtraceDetach(pid)
    27  }
    28  
    29  // ptraceCont executes ptrace PTRACE_CONT
    30  // id may be a PID or an LWPID
    31  func ptraceCont(id, sig int) error {
    32  	return sys.PtraceCont(id, sig)
    33  }
    34  
    35  // ptraceSingleStep executes ptrace PTRACE_SINGLE_STEP.
    36  // id may be a PID or an LWPID
    37  func ptraceSingleStep(id int) error {
    38  	return sys.PtraceSingleStep(id)
    39  }
    40  
    41  // Get a list of the thread ids of a process
    42  func ptraceGetLwpList(pid int) (tids []int32) {
    43  	numLWPS := C.ptrace(C.PT_GETNUMLWPS, C.pid_t(pid), C.caddr_t(unsafe.Pointer(uintptr(0))), C.int(0))
    44  	if numLWPS < 0 {
    45  		panic("PT_GETNUMLWPS failed")
    46  	}
    47  	tids = make([]int32, numLWPS)
    48  	n := C.ptrace(C.PT_GETLWPLIST, C.pid_t(pid), C.caddr_t(unsafe.Pointer(&tids[0])), C.int(numLWPS))
    49  	if n < 0 {
    50  		panic("PT_GETLWPLIST failed")
    51  	}
    52  	return tids[0:n]
    53  }
    54  
    55  // Get info of the thread that caused wpid's process to stop.
    56  func ptraceGetLwpInfo(wpid int) (info sys.PtraceLwpInfoStruct, err error) {
    57  	err = sys.PtraceLwpInfo(wpid, &info)
    58  	return info, err
    59  }
    60  
    61  // id may be a PID or an LWPID
    62  func ptraceReadData(id int, addr uintptr, data []byte) (n int, err error) {
    63  	return sys.PtraceIO(sys.PIOD_READ_D, id, addr, data, len(data))
    64  }
    65  
    66  // id may be a PID or an LWPID
    67  func ptraceWriteData(id int, addr uintptr, data []byte) (n int, err error) {
    68  	return sys.PtraceIO(sys.PIOD_WRITE_D, id, addr, data, len(data))
    69  }
    70  
    71  func ptraceSuspend(id int) error {
    72  	_, _, e1 := sys.Syscall6(sys.SYS_PTRACE, uintptr(C.PT_SUSPEND), uintptr(id), uintptr(1), uintptr(0), 0, 0)
    73  	if e1 != 0 {
    74  		return syscall.Errno(e1)
    75  	}
    76  	return nil
    77  }
    78  
    79  func ptraceResume(id int) error {
    80  	_, _, e1 := sys.Syscall6(sys.SYS_PTRACE, uintptr(C.PT_RESUME), uintptr(id), uintptr(1), uintptr(0), 0, 0)
    81  	if e1 != 0 {
    82  		return syscall.Errno(e1)
    83  	}
    84  	return nil
    85  }
    86  
    87  func ptraceSetStep(id int) error {
    88  	_, _, e1 := sys.Syscall6(sys.SYS_PTRACE, uintptr(C.PT_SETSTEP), uintptr(id), uintptr(0), uintptr(0), 0, 0)
    89  	if e1 != 0 {
    90  		return syscall.Errno(e1)
    91  	}
    92  	return nil
    93  }
    94  
    95  func ptraceClearStep(id int) error {
    96  	_, _, e1 := sys.Syscall6(sys.SYS_PTRACE, uintptr(C.PT_CLEARSTEP), uintptr(id), uintptr(0), uintptr(0), 0, 0)
    97  	if e1 != 0 {
    98  		return syscall.Errno(e1)
    99  	}
   100  	return nil
   101  }