github.com/cnboonhan/delve@v0.0.0-20230908061759-363f2388c2fb/service/debugger/debugger_linux.go (about)

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