Skip to content

Commit

Permalink
tetragon: Add LoadMultiUprobeProgram function
Browse files Browse the repository at this point in the history
Adding LoadMultiUprobeProgram function to load uprobe multi
programs.

Signed-off-by: Jiri Olsa <[email protected]>
  • Loading branch information
olsajiri committed Dec 25, 2023
1 parent 29f9625 commit 85349d6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
54 changes: 54 additions & 0 deletions pkg/sensors/program/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,51 @@ func UprobeAttach(load *Program) AttachFunc {
}
}

func MultiUprobeAttach(load *Program) AttachFunc {
return func(coll *ebpf.Collection, collSpec *ebpf.CollectionSpec,
prog *ebpf.Program, spec *ebpf.ProgramSpec) (unloader.Unloader, error) {

data, ok := load.AttachData.(*MultiUprobeAttachData)
if !ok {
return nil, fmt.Errorf("attaching '%s' failed: wrong attach data", spec.Name)
}

linkFn := func() ([]link.Link, error) {
var links []link.Link
var lnk link.Link

for path, attach := range data.Attach {
exec, err := link.OpenExecutable(path)
if err != nil {
return nil, err
}
lnk, err = exec.UprobeMulti(attach.Symbols, prog, &link.UprobeMultiOptions{Cookies: attach.Cookies})
if err != nil {
return nil, err
}
links = append(links, lnk)
}
return links, nil
}

links, err := linkFn()
if err != nil {
return nil, fmt.Errorf("attaching '%s' failed: %w", spec.Name, err)
}

return &unloader.MultiRelinkUnloader{
UnloadProg: unloader.ChainUnloader{
unloader.PinUnloader{
Prog: prog,
},
}.Unload,
IsLinked: true,
Links: links,
RelinkFn: linkFn,
}, nil
}
}

func NoAttach() AttachFunc {
return func(coll *ebpf.Collection, collSpec *ebpf.CollectionSpec,
prog *ebpf.Program, spec *ebpf.ProgramSpec) (unloader.Unloader, error) {
Expand Down Expand Up @@ -542,6 +587,15 @@ func LoadLSMProgram(bpfDir, mapDir string, load *Program, verbose int) error {
return loadProgram(bpfDir, []string{mapDir}, load, opts, verbose)
}

func LoadMultiUprobeProgram(bpfDir, mapDir string, load *Program, verbose int) error {
ci := &customInstall{fmt.Sprintf("%s-up_calls", load.PinPath), "uprobe"}
opts := &loadOpts{
attach: MultiUprobeAttach(load),
ci: ci,
}
return loadProgram(bpfDir, []string{mapDir}, load, opts, verbose)
}

func slimVerifierError(errStr string) string {
// The error is potentially up to 'verifierLogBufferSize' bytes long,
// and most of it is not interesting. For a user-friendly output, we'll
Expand Down
10 changes: 10 additions & 0 deletions pkg/sensors/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ type UprobeAttachData struct {
Symbol string
}

type MultiUprobeAttachSymbolsCookies struct {
Symbols []string
Cookies []uint64
}

type MultiUprobeAttachData struct {
// Path -> []{Symbol,Cookie}
Attach map[string]*MultiUprobeAttachSymbolsCookies
}

// Program reprents a BPF program.
type Program struct {
// Name is the name of the BPF object file.
Expand Down

0 comments on commit 85349d6

Please sign in to comment.