github.com/zeebo/goof@v0.0.0-20230907150950-e9457bc94477/process_go18.go (about)

     1  //go:build go1.8
     2  // +build go1.8
     3  
     4  package goof
     5  
     6  import (
     7  	"debug/dwarf"
     8  	"debug/elf"
     9  	"debug/macho"
    10  	"debug/pe"
    11  	"io"
    12  	"os"
    13  	"runtime"
    14  
    15  	"github.com/zeebo/errs"
    16  )
    17  
    18  func openProc() (*dwarf.Data, error) {
    19  	path, err := os.Executable()
    20  	if err != nil {
    21  		return nil, errs.Wrap(err)
    22  	}
    23  
    24  	var fh interface {
    25  		io.Closer
    26  		DWARF() (*dwarf.Data, error)
    27  	}
    28  
    29  	switch runtime.GOOS {
    30  	case "linux":
    31  		fh, err = elf.Open(path)
    32  	case "darwin":
    33  		fh, err = macho.Open(path)
    34  	case "windows":
    35  		fh, err = pe.Open(path)
    36  	default:
    37  		return nil, errs.New("unknown goos: %q", runtime.GOOS)
    38  	}
    39  	if err != nil {
    40  		return nil, errs.Wrap(err)
    41  	}
    42  	defer fh.Close()
    43  
    44  	data, err := fh.DWARF()
    45  	return data, errs.Wrap(err)
    46  }