proc: detect when Launching non-executable files

This provides a better error message when the user tries to run dlv
debug on a directory that does not contain a main package, when `dlv
exec` is used with a source file.

Additionally the architecture of the executable is checked as suggested
by @alexbrainman in #443.

Fixes #509
This commit is contained in:
aarzilli
2016-04-21 12:19:21 +02:00
parent 5714aa548c
commit 51c39ed171
10 changed files with 133 additions and 14 deletions

View File

@ -49,6 +49,10 @@ func Launch(cmd []string) (*Process, error) {
proc *exec.Cmd
err error
)
// check that the argument to Launch is an executable file
if fi, staterr := os.Stat(cmd[0]); staterr == nil && (fi.Mode()&0111) == 0 {
return nil, NotExecutableErr
}
dbp := New(0)
dbp.execPtraceFunc(func() {
proc = exec.Command(cmd[0])
@ -162,6 +166,8 @@ func (dbp *Process) updateThreadList() error {
return nil
}
var UnsupportedArchErr = errors.New("unsupported architecture - only linux/amd64 is supported")
func (dbp *Process) findExecutable(path string) (*elf.File, error) {
if path == "" {
path = fmt.Sprintf("/proc/%d/exe", dbp.Pid)
@ -174,6 +180,9 @@ func (dbp *Process) findExecutable(path string) (*elf.File, error) {
if err != nil {
return nil, err
}
if elfFile.Machine != elf.EM_X86_64 {
return nil, UnsupportedArchErr
}
dbp.dwarf, err = elfFile.DWARF()
if err != nil {
return nil, err