github.com/zeebo/goof@v0.0.0-20190312211016-1ee209ef0510/process_go17_darwin.go (about)

     1  // +build !go1.8,darwin
     2  
     3  package goof
     4  
     5  import (
     6  	"debug/dwarf"
     7  	"debug/macho"
     8  	"unsafe"
     9  
    10  	"github.com/zeebo/errs"
    11  )
    12  
    13  // #include <mach-o/dyld.h>
    14  // #include <stdlib.h>
    15  import "C"
    16  
    17  func openProc() (*dwarf.Data, error) {
    18  	const bufsize = 4096
    19  
    20  	buf := (*C.char)(C.malloc(bufsize))
    21  	defer C.free(unsafe.Pointer(buf))
    22  
    23  	size := C.uint32_t(bufsize)
    24  	if rc := C._NSGetExecutablePath(buf, &size); rc != 0 {
    25  		return nil, errs.New("error in cgo call to get path: %d", rc)
    26  	}
    27  
    28  	fh, err := macho.Open(C.GoString(buf))
    29  	if err != nil {
    30  		return nil, errs.Wrap(err)
    31  	}
    32  	defer fh.Close()
    33  
    34  	dwarf, err := fh.DWARF()
    35  	return dwarf, errs.Wrap(err)
    36  }