github.com/undoio/delve@v1.9.0/pkg/proc/native/ptrace_linux_386.go (about)

     1  package native
     2  
     3  import "C"
     4  
     5  import (
     6  	"fmt"
     7  	"syscall"
     8  	"unsafe"
     9  
    10  	sys "golang.org/x/sys/unix"
    11  
    12  	"github.com/undoio/delve/pkg/proc/amd64util"
    13  )
    14  
    15  // ptraceGetRegset returns floating point registers of the specified thread
    16  // using PTRACE.
    17  // See i386_linux_fetch_inferior_registers in gdb/i386-linux-nat.c.html
    18  // and i386_supply_xsave in gdb/i386-tdep.c.html
    19  // and Section 13.1 (and following) of Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 1: Basic Architecture
    20  func ptraceGetRegset(tid int) (regset amd64util.AMD64Xstate, err error) {
    21  	_, _, err = syscall.Syscall6(syscall.SYS_PTRACE, sys.PTRACE_GETFPREGS, uintptr(tid), uintptr(0), uintptr(unsafe.Pointer(&regset.AMD64PtraceFpRegs)), 0, 0)
    22  	if err == syscall.Errno(0) || err == syscall.ENODEV {
    23  		// ignore ENODEV, it just means this CPU doesn't have X87 registers (??)
    24  		err = nil
    25  	}
    26  
    27  	xstateargs := make([]byte, amd64util.AMD64XstateMaxSize())
    28  	iov := sys.Iovec{Base: &xstateargs[0], Len: uint32(len(xstateargs))}
    29  	_, _, err = syscall.Syscall6(syscall.SYS_PTRACE, sys.PTRACE_GETREGSET, uintptr(tid), _NT_X86_XSTATE, uintptr(unsafe.Pointer(&iov)), 0, 0)
    30  	if err != syscall.Errno(0) {
    31  		if err == syscall.ENODEV || err == syscall.EIO || err == syscall.EINVAL {
    32  			// ignore ENODEV, it just means this CPU or kernel doesn't support XSTATE, see https://github.com/go-delve/delve/issues/1022
    33  			// also ignore EIO, it means that we are running on an old kernel (pre 2.6.34) and PTRACE_GETREGSET is not implemented
    34  			// also ignore EINVAL, it means the kernel itself does not support the NT_X86_XSTATE argument (but does support PTRACE_GETREGSET)
    35  			err = nil
    36  		}
    37  		return
    38  	} else {
    39  		err = nil
    40  	}
    41  
    42  	regset.Xsave = xstateargs[:iov.Len]
    43  	err = amd64util.AMD64XstateRead(regset.Xsave, false, &regset)
    44  	return
    45  }
    46  
    47  // ptraceGetTls return the addr of tls by PTRACE_GET_THREAD_AREA for specify thread.
    48  // See http://man7.org/linux/man-pages/man2/ptrace.2.html for detail about PTRACE_GET_THREAD_AREA.
    49  // struct user_desc at https://golang.org/src/runtime/sys_linux_386.s
    50  //
    51  //	type UserDesc struct {
    52  //		 EntryNumber uint32
    53  //		 BaseAddr    uint32
    54  //		 Limit       uint32
    55  //		 Flag        uint32
    56  //	}
    57  func ptraceGetTls(gs int32, tid int) (uint32, error) {
    58  	ud := [4]uint32{}
    59  
    60  	// Gs usually is 0x33
    61  	_, _, err := syscall.Syscall6(syscall.SYS_PTRACE, sys.PTRACE_GET_THREAD_AREA, uintptr(tid), uintptr(gs>>3), uintptr(unsafe.Pointer(&ud)), 0, 0)
    62  	if err == syscall.ENODEV || err == syscall.EIO {
    63  		return 0, fmt.Errorf("%s", err)
    64  	}
    65  
    66  	return uint32(ud[1]), nil
    67  }
    68  
    69  // processVmRead calls process_vm_readv
    70  func processVmRead(tid int, addr uintptr, data []byte) (int, error) {
    71  	len_iov := uint32(len(data))
    72  	local_iov := sys.Iovec{Base: &data[0], Len: len_iov}
    73  	remote_iov := remoteIovec{base: addr, len: uintptr(len_iov)}
    74  	p_local := uintptr(unsafe.Pointer(&local_iov))
    75  	p_remote := uintptr(unsafe.Pointer(&remote_iov))
    76  	n, _, err := syscall.Syscall6(sys.SYS_PROCESS_VM_READV, uintptr(tid), p_local, 1, p_remote, 1, 0)
    77  	if err != syscall.Errno(0) {
    78  		return 0, err
    79  	}
    80  	return int(n), nil
    81  }
    82  
    83  // processVmWrite calls process_vm_writev
    84  func processVmWrite(tid int, addr uintptr, data []byte) (int, error) {
    85  	len_iov := uint32(len(data))
    86  	local_iov := sys.Iovec{Base: &data[0], Len: len_iov}
    87  	remote_iov := remoteIovec{base: addr, len: uintptr(len_iov)}
    88  	p_local := uintptr(unsafe.Pointer(&local_iov))
    89  	p_remote := uintptr(unsafe.Pointer(&remote_iov))
    90  	n, _, err := syscall.Syscall6(sys.SYS_PROCESS_VM_WRITEV, uintptr(tid), p_local, 1, p_remote, 1, 0)
    91  	if err != syscall.Errno(0) {
    92  		return 0, err
    93  	}
    94  	return int(n), nil
    95  }