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

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