func determineRetprobeBit(typ probeType) (uint64, error) {
	p := filepath.Join("/sys/bus/event_source/devices/", typ.String(), "/format/retprobe")
	data, err := ioutil.ReadFile(p)
	if err != nil {
		return 0, err
	}
	var rp uint64
	n, err := fmt.Sscanf(string(bytes.TrimSpace(data)), "config:%d", &rp)
	if err != nil {
		return 0, fmt.Errorf("parse retprobe bit: %w", err)
	}
	if n != 1 {
		return 0, fmt.Errorf("parse retprobe bit: expected 1 item, got %d", n)
	}
	return rp, nil
}
determineRetprobeBit reads a Performance Monitoring Unit's retprobe bit
from /sys/bus/event_source/devices/<pmu>/format/retprobe.
package link
import (
	"bytes"
	"crypto/rand"
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"runtime"
	"sync"
	"unsafe"
	"github.com/cilium/ebpf"
	"github.com/cilium/ebpf/internal"
	"github.com/cilium/ebpf/internal/unix"
)
var (
	kprobeEventsPath = filepath.Join(tracefsPath, "kprobe_events")
	kprobeRetprobeBit = struct {
		once  sync.Once
		value uint64
		err   error
	}{}
)
type probeType uint8
const (
	kprobeType probeType = iota
	uprobeType
)
func (pt probeType) String() string {
	if pt == kprobeType {
		return "kprobe"
	}
	return "uprobe"
}
func (pt probeType) EventsPath() string {
	if pt == kprobeType {
		return kprobeEventsPath
	}
	return uprobeEventsPath
}
func (pt probeType) PerfEventType(ret bool) perfEventType {
	if pt == kprobeType {
		if ret {
			return kretprobeEvent
		}
		return kprobeEvent
	}
	if ret {
		return uretprobeEvent
	}
	return uprobeEvent
}
func (pt probeType) RetprobeBit() (uint64, error) {
	if pt == kprobeType {
		return kretprobeBit()
	}
	return uretprobeBit()
}
func Kprobe(symbol string, prog *ebpf.Program) (Link, error) {
	k, err := kprobe(symbol, prog, false)
	if err != nil {
		return nil, err
	}
	err = k.attach(prog)
	if err != nil {
		k.Close()
		return nil, err
	}
	return k, nil
}
func Kretprobe(symbol string, prog *ebpf.Program) (Link, error) {
	k, err := kprobe(symbol, prog, true)
	if err != nil {
		return nil, err
	}
	err = k.attach(prog)
	if err != nil {
		k.Close()
		return nil, err
	}
	return k, nil
}
func kprobe(symbol string, prog *ebpf.Program, ret bool) (*perfEvent, error) {
	if symbol == "" {
		return nil, fmt.Errorf("symbol name cannot be empty: %w", errInvalidInput)
	}
	if prog == nil {
		return nil, fmt.Errorf("prog cannot be nil: %w", errInvalidInput)
	}
	if !rgxTraceEvent.MatchString(symbol) {
		return nil, fmt.Errorf("symbol '%s' must be alphanumeric or underscore: %w", symbol, errInvalidInput)
	}
	if prog.Type() != ebpf.Kprobe {
		return nil, fmt.Errorf("eBPF program type %s is not a Kprobe: %w", prog.Type(), errInvalidInput)
	}
	tp, err := pmuKprobe(platformPrefix(symbol), ret)
	if errors.Is(err, os.ErrNotExist) {
		tp, err = pmuKprobe(symbol, ret)
	}
	if err == nil {
		return tp, nil
	}
	if err != nil && !errors.Is(err, ErrNotSupported) {
		return nil, fmt.Errorf("creating perf_kprobe PMU: %w", err)
	}
	tp, err = tracefsKprobe(platformPrefix(symbol), ret)
	if errors.Is(err, os.ErrNotExist) {
		tp, err = tracefsKprobe(symbol, ret)
	}
	if err != nil {
		return nil, fmt.Errorf("creating trace event '%s' in tracefs: %w", symbol, err)
	}
	return tp, nil
}
func pmuKprobe(symbol string, ret bool) (*perfEvent, error) {
	return pmuProbe(kprobeType, symbol, "", 0, ret)
}
func pmuProbe(typ probeType, symbol, path string, offset uint64, ret bool) (*perfEvent, error) {
	et, err := getPMUEventType(typ)
	if err != nil {
		return nil, err
	}
	var config uint64
	if ret {
		bit, err := typ.RetprobeBit()
		if err != nil {
			return nil, err
		}
		config |= 1 << bit
	}
	var (
		attr unix.PerfEventAttr
		sp   unsafe.Pointer
	)
	switch typ {
	case kprobeType:
		sp, err := unsafeStringPtr(symbol)
		if err != nil {
			return nil, err
		}
		attr = unix.PerfEventAttr{
			Type:   uint32(et),           
			Ext1:   uint64(uintptr(sp)),  
			Config: config,               
		}
	case uprobeType:
		sp, err := unsafeStringPtr(path)
		if err != nil {
			return nil, err
		}
		attr = unix.PerfEventAttr{
			Size:   unix.PERF_ATTR_SIZE_VER1,
			Type:   uint32(et),           
			Ext1:   uint64(uintptr(sp)),  
			Ext2:   offset,               
			Config: config,               
		}
	}
	fd, err := unix.PerfEventOpen(&attr, perfAllThreads, 0, -1, unix.PERF_FLAG_FD_CLOEXEC)
	if errors.Is(err, os.ErrNotExist) || errors.Is(err, unix.EINVAL) {
		return nil, fmt.Errorf("symbol '%s' not found: %w", symbol, os.ErrNotExist)
	}
	if err != nil {
		return nil, fmt.Errorf("opening perf event: %w", err)
	}
	runtime.KeepAlive(sp)
	return &perfEvent{
		fd:    internal.NewFD(uint32(fd)),
		pmuID: et,
		name:  symbol,
		typ:   typ.PerfEventType(ret),
	}, nil
}
func tracefsKprobe(symbol string, ret bool) (*perfEvent, error) {
	return tracefsProbe(kprobeType, symbol, "", 0, ret)
}
func tracefsProbe(typ probeType, symbol, path string, offset uint64, ret bool) (*perfEvent, error) {
	group, err := randomGroup("ebpf")
	if err != nil {
		return nil, fmt.Errorf("randomizing group name: %w", err)
	}
	_, err = getTraceEventID(group, symbol)
	if err == nil {
		return nil, fmt.Errorf("trace event already exists: %s/%s", group, symbol)
	}
	if err != nil && !errors.Is(err, os.ErrNotExist) {
		return nil, fmt.Errorf("checking trace event %s/%s: %w", group, symbol, err)
	}
	if err := createTraceFSProbeEvent(typ, group, symbol, path, offset, ret); err != nil {
		return nil, fmt.Errorf("creating probe entry on tracefs: %w", err)
	}
	tid, err := getTraceEventID(group, symbol)
	if err != nil {
		return nil, fmt.Errorf("getting trace event id: %w", err)
	}
	fd, err := openTracepointPerfEvent(tid)
	if err != nil {
		return nil, err
	}
	return &perfEvent{
		fd:        fd,
		group:     group,
		name:      symbol,
		tracefsID: tid,
		typ:       typ.PerfEventType(ret),
	}, nil
}
func createTraceFSProbeEvent(typ probeType, group, symbol, path string, offset uint64, ret bool) error {
	f, err := os.OpenFile(typ.EventsPath(), os.O_APPEND|os.O_WRONLY, 0666)
	if err != nil {
		return fmt.Errorf("error opening '%s': %w", typ.EventsPath(), err)
	}
	defer f.Close()
	var pe string
	switch typ {
	case kprobeType:
		pe = fmt.Sprintf("%s:%s/%s %s", probePrefix(ret), group, symbol, symbol)
	case uprobeType:
		pathOffset := uprobePathOffset(path, offset)
		pe = fmt.Sprintf("%s:%s/%s %s", probePrefix(ret), group, symbol, pathOffset)
	}
	_, err = f.WriteString(pe)
	if errors.Is(err, os.ErrNotExist) || errors.Is(err, unix.EINVAL) {
		return fmt.Errorf("symbol %s not found: %w", symbol, os.ErrNotExist)
	}
	if err != nil {
		return fmt.Errorf("writing '%s' to '%s': %w", pe, typ.EventsPath(), err)
	}
	return nil
}
func closeTraceFSProbeEvent(typ probeType, group, symbol string) error {
	f, err := os.OpenFile(typ.EventsPath(), os.O_APPEND|os.O_WRONLY, 0666)
	if err != nil {
		return fmt.Errorf("error opening %s: %w", typ.EventsPath(), err)
	}
	defer f.Close()
	pe := fmt.Sprintf("-:%s/%s", group, symbol)
	if _, err = f.WriteString(pe); err != nil {
		return fmt.Errorf("writing '%s' to '%s': %w", pe, typ.EventsPath(), err)
	}
	return nil
}
func randomGroup(prefix string) (string, error) {
	if !rgxTraceEvent.MatchString(prefix) {
		return "", fmt.Errorf("prefix '%s' must be alphanumeric or underscore: %w", prefix, errInvalidInput)
	}
	b := make([]byte, 8)
	if _, err := rand.Read(b); err != nil {
		return "", fmt.Errorf("reading random bytes: %w", err)
	}
	group := fmt.Sprintf("%s_%x", prefix, b)
	if len(group) > 63 {
		return "", fmt.Errorf("group name '%s' cannot be longer than 63 characters: %w", group, errInvalidInput)
	}
	return group, nil
}
func probePrefix(ret bool) string {
	if ret {
		return "r"
	}
	return "p"
}