| 1 |
|
| 2 |
|
| 3 |
package link |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
|
| 8 |
"github.com/cilium/ebpf" |
| 9 |
"github.com/cilium/ebpf/internal/tracefs" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
type TracepointOptions struct { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
Cookie uint64 |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
func Tracepoint(group, name string, prog *ebpf.Program, opts *TracepointOptions) (Link, error) { |
| 38 |
if group == "" || name == "" { |
| 39 |
return nil, fmt.Errorf("group and name cannot be empty: %w", errInvalidInput) |
| 40 |
} |
| 41 |
if prog == nil { |
| 42 |
return nil, fmt.Errorf("prog cannot be nil: %w", errInvalidInput) |
| 43 |
} |
| 44 |
if prog.Type() != ebpf.TracePoint { |
| 45 |
return nil, fmt.Errorf("eBPF program type %s is not a Tracepoint: %w", prog.Type(), errInvalidInput) |
| 46 |
} |
| 47 |
|
| 48 |
tid, err := tracefs.EventID(group, name) |
| 49 |
if err != nil { |
| 50 |
return nil, err |
| 51 |
} |
| 52 |
|
| 53 |
fd, err := openTracepointPerfEvent(tid, perfAllThreads) |
| 54 |
if err != nil { |
| 55 |
return nil, err |
| 56 |
} |
| 57 |
|
| 58 |
var cookie uint64 |
| 59 |
if opts != nil { |
| 60 |
cookie = opts.Cookie |
| 61 |
} |
| 62 |
|
| 63 |
pe := newPerfEvent(fd, nil) |
| 64 |
|
| 65 |
lnk, err := attachPerfEvent(pe, prog, cookie) |
| 66 |
if err != nil { |
| 67 |
pe.Close() |
| 68 |
return nil, err |
| 69 |
} |
| 70 |
|
| 71 |
return lnk, nil |
| 72 |
} |