github.com/undoio/delve@v1.9.0/service/debugger/debugger_linux.go (about)

     1  package debugger
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"syscall"
     8  
     9  	sys "golang.org/x/sys/unix"
    10  )
    11  
    12  //lint:file-ignore ST1005 errors here can be capitalized
    13  
    14  func attachErrorMessage(pid int, err error) error {
    15  	fallbackerr := fmt.Errorf("could not attach to pid %d: %s", pid, err)
    16  	if serr, ok := err.(syscall.Errno); ok {
    17  		switch serr {
    18  		case syscall.EPERM:
    19  			bs, err := ioutil.ReadFile("/proc/sys/kernel/yama/ptrace_scope")
    20  			if err == nil && len(bs) >= 1 && bs[0] != '0' {
    21  				// Yama documentation: https://www.kernel.org/doc/Documentation/security/Yama.txt
    22  				return fmt.Errorf("Could not attach to pid %d: this could be caused by a kernel security setting, try writing \"0\" to /proc/sys/kernel/yama/ptrace_scope", pid)
    23  			}
    24  			fi, err := os.Stat(fmt.Sprintf("/proc/%d", pid))
    25  			if err != nil {
    26  				return fallbackerr
    27  			}
    28  			if fi.Sys().(*syscall.Stat_t).Uid != uint32(os.Getuid()) {
    29  				return fmt.Errorf("Could not attach to pid %d: current user does not own the process", pid)
    30  			}
    31  		}
    32  	}
    33  	return fallbackerr
    34  }
    35  
    36  func stopProcess(pid int) error {
    37  	return sys.Kill(pid, sys.SIGSTOP)
    38  }