service/debugger: merge attachErrorMessage, verifyBinaryFormat (#3142)

These functions don't need to be conditionally compiled and they share
substanatial amounts of code in all their OS-specific versions.
This commit is contained in:
Alessandro Arzilli 2022-09-21 18:48:35 +02:00 committed by GitHub
parent 1add32b4d9
commit 8337b5a8a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 102 deletions

@ -3,11 +3,15 @@ package debugger
import (
"bytes"
"debug/dwarf"
"debug/elf"
"debug/macho"
"debug/pe"
"errors"
"fmt"
"go/parser"
"go/token"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
@ -2304,3 +2308,55 @@ func noDebugErrorWarning(err error) error {
}
return err
}
func verifyBinaryFormat(exePath string) error {
f, err := os.Open(exePath)
if err != nil {
return err
}
defer f.Close()
switch runtime.GOOS {
case "windows":
// Make sure the binary exists and is an executable file
if filepath.Base(exePath) == exePath {
if _, err := exec.LookPath(exePath); err != nil {
return err
}
}
default:
fi, err := f.Stat()
if err != nil {
return err
}
if (fi.Mode() & 0111) == 0 {
return api.ErrNotExecutable
}
}
// check that the binary format is what we expect for the host system
var exe interface{ Close() error }
switch runtime.GOOS {
case "darwin":
exe, err = macho.NewFile(f)
case "linux", "freebsd":
exe, err = elf.NewFile(f)
case "windows":
exe, err = pe.NewFile(f)
default:
panic("attempting to open file Delve cannot parse")
}
if err != nil {
return api.ErrNotExecutable
}
exe.Close()
return nil
}
var attachErrorMessage = attachErrorMessageDefault
func attachErrorMessageDefault(pid int, err error) error {
return fmt.Errorf("could not attach to pid %d: %s", pid, err)
}

@ -1,10 +0,0 @@
package debugger
import (
"fmt"
)
func attachErrorMessage(pid int, err error) error {
//TODO: mention certificates?
return fmt.Errorf("could not attach to pid %d: %s", pid, err)
}

@ -1,9 +0,0 @@
package debugger
import (
"fmt"
)
func attachErrorMessage(pid int, err error) error {
return fmt.Errorf("could not attach to pid %d: %s", pid, err)
}

@ -7,9 +7,13 @@ import (
"syscall"
)
func init() {
attachErrorMessage = attachErrorMessageLinux
}
//lint:file-ignore ST1005 errors here can be capitalized
func attachErrorMessage(pid int, err error) error {
func attachErrorMessageLinux(pid int, err error) error {
fallbackerr := fmt.Errorf("could not attach to pid %d: %s", pid, err)
if serr, ok := err.(syscall.Errno); ok {
switch serr {

@ -1,45 +0,0 @@
//go:build !windows
// +build !windows
package debugger
import (
"debug/elf"
"debug/macho"
"os"
"runtime"
"github.com/go-delve/delve/service/api"
)
func verifyBinaryFormat(exePath string) error {
f, err := os.Open(exePath)
if err != nil {
return err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return err
}
if (fi.Mode() & 0111) == 0 {
return api.ErrNotExecutable
}
// check that the binary format is what we expect for the host system
var exe interface{ Close() error }
switch runtime.GOOS {
case "darwin":
exe, err = macho.NewFile(f)
case "linux", "freebsd":
exe, err = elf.NewFile(f)
default:
panic("attempting to open file Delve cannot parse")
}
if err != nil {
return api.ErrNotExecutable
}
exe.Close()
return nil
}

@ -1,37 +0,0 @@
package debugger
import (
"debug/pe"
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/go-delve/delve/service/api"
)
func attachErrorMessage(pid int, err error) error {
return fmt.Errorf("could not attach to pid %d: %s", pid, err)
}
func verifyBinaryFormat(exePath string) error {
f, err := os.Open(exePath)
if err != nil {
return err
}
defer f.Close()
// Make sure the binary exists and is an executable file
if filepath.Base(exePath) == exePath {
if _, err := exec.LookPath(exePath); err != nil {
return err
}
}
exe, err := pe.NewFile(f)
if err != nil {
return api.ErrNotExecutable
}
exe.Close()
return nil
}