github.com/criyle/go-sandbox@v0.10.3/ptracer/ptrace_linux.go (about)

     1  package ptracer
     2  
     3  import (
     4  	"syscall"
     5  	"unsafe"
     6  )
     7  
     8  // ptrace constants
     9  const (
    10  	NT_PRSTATUS        = 1
    11  	NT_ARM_SYSTEM_CALL = 0x404
    12  
    13  	PTRACE_SET_SYSCALL = 23
    14  )
    15  
    16  func ptrace(request int, pid int, addr uintptr, data uintptr) (err error) {
    17  	_, _, e1 := syscall.Syscall6(syscall.SYS_PTRACE, uintptr(request), uintptr(pid), uintptr(addr), uintptr(data), 0, 0)
    18  	if e1 != 0 {
    19  		err = e1
    20  	}
    21  	return
    22  }
    23  
    24  func ptraceGetRegSet(pid int, regs *syscall.PtraceRegs) error {
    25  	iov := getIovec((*byte)(unsafe.Pointer(regs)), int(unsafe.Sizeof(*regs)))
    26  	return ptrace(syscall.PTRACE_GETREGSET, pid, NT_PRSTATUS, uintptr(unsafe.Pointer(&iov)))
    27  }
    28  
    29  func ptraceSetRegSet(pid int, regs *syscall.PtraceRegs) error {
    30  	iov := getIovec((*byte)(unsafe.Pointer(regs)), int(unsafe.Sizeof(*regs)))
    31  	return ptrace(syscall.PTRACE_SETREGSET, pid, NT_PRSTATUS, uintptr(unsafe.Pointer(&iov)))
    32  }
    33  
    34  func ptraceArm64SetSyscall(pid int, syscallNo int) error {
    35  	iov := getIovec((*byte)(unsafe.Pointer(&syscallNo)), int(unsafe.Sizeof(syscallNo)))
    36  	return ptrace(syscall.PTRACE_SETREGSET, pid, NT_ARM_SYSTEM_CALL, uintptr(unsafe.Pointer(&iov)))
    37  }
    38  
    39  func ptraceArmSetSyscall(pid int, syscallNo int) error {
    40  	return ptrace(PTRACE_SET_SYSCALL, pid, 0, uintptr(syscallNo))
    41  }