-
Notifications
You must be signed in to change notification settings - Fork 716
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
link: implement uprobe_multi link type
This commit adds support for this through the UprobeMulti() and UretprobeMulti() APIs in package link. Signed-off-by: Jiri Olsa <jolsa@kernel.org>
- Loading branch information
Showing
7 changed files
with
329 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package link | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"unsafe" | ||
|
||
"github.com/cilium/ebpf" | ||
"github.com/cilium/ebpf/asm" | ||
"github.com/cilium/ebpf/internal" | ||
"github.com/cilium/ebpf/internal/sys" | ||
"github.com/cilium/ebpf/internal/unix" | ||
) | ||
|
||
type UprobeMultiOptions struct { | ||
Path string | ||
Offsets []uint64 | ||
RefCtrOffsets []uint64 | ||
Cookies []uint64 | ||
} | ||
|
||
func (ex *Executable) UprobeMulti(symbols []string, prog *ebpf.Program, opts *UprobeMultiOptions) (Link, error) { | ||
if opts == nil { | ||
opts = &UprobeMultiOptions{} | ||
} | ||
var err error | ||
if len(symbols) != 0 { | ||
opts.Offsets, err = ex.addressMulti(symbols) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return ex.uprobeMulti(prog, opts, 0) | ||
} | ||
|
||
func (ex *Executable) UretprobeMulti(symbols []string, prog *ebpf.Program, opts *UprobeMultiOptions) (Link, error) { | ||
return ex.uprobeMulti(prog, opts, unix.BPF_F_UPROBE_MULTI_RETURN) | ||
} | ||
|
||
func (ex *Executable) uprobeMulti(prog *ebpf.Program, opts *UprobeMultiOptions, flags uint32) (Link, error) { | ||
if prog == nil { | ||
return nil, errors.New("cannot attach a nil program") | ||
} | ||
|
||
offsets := uint32(len(opts.Offsets)) | ||
refCtrOffsets := uint32(len(opts.RefCtrOffsets)) | ||
cookies := uint32(len(opts.Cookies)) | ||
|
||
if offsets == 0 { | ||
return nil, fmt.Errorf("Offsets is required: %w", errInvalidInput) | ||
} | ||
if refCtrOffsets > 0 && refCtrOffsets != offsets { | ||
return nil, fmt.Errorf("RefCtrOffsets must be exactly Offsets in length: %w", errInvalidInput) | ||
} | ||
if cookies > 0 && cookies != offsets { | ||
return nil, fmt.Errorf("Cookies must be exactly Offsets in length: %w", errInvalidInput) | ||
} | ||
|
||
attr := &sys.LinkCreateUprobeMultiAttr{ | ||
Path: sys.NewStringPointer(ex.path), | ||
ProgFd: uint32(prog.FD()), | ||
AttachType: sys.BPF_TRACE_UPROBE_MULTI, | ||
UprobeMultiFlags: flags, | ||
Count: offsets, | ||
Offsets: sys.NewPointer(unsafe.Pointer(&opts.Offsets[0])), | ||
} | ||
|
||
if refCtrOffsets != 0 { | ||
attr.RefCtrOffsets = sys.NewPointer(unsafe.Pointer(&opts.RefCtrOffsets[0])) | ||
} | ||
if cookies != 0 { | ||
attr.Cookies = sys.NewPointer(unsafe.Pointer(&opts.Cookies[0])) | ||
} | ||
|
||
fd, err := sys.LinkCreateUprobeMulti(attr) | ||
if errors.Is(err, unix.ESRCH) { | ||
return nil, fmt.Errorf("XXX: %w", os.ErrNotExist) | ||
} | ||
if errors.Is(err, unix.EINVAL) { | ||
return nil, fmt.Errorf("%w (missing kernel symbol or prog's AttachType not AttachTraceUprobeMulti?)", err) | ||
} | ||
|
||
if err != nil { | ||
if haveFeatErr := haveBPFLinkUprobeMulti(); haveFeatErr != nil { | ||
return nil, haveFeatErr | ||
} | ||
return nil, err | ||
} | ||
|
||
return &uprobeMultiLink{RawLink{fd, ""}}, nil | ||
} | ||
|
||
type uprobeMultiLink struct { | ||
RawLink | ||
} | ||
|
||
var _ Link = (*uprobeMultiLink)(nil) | ||
|
||
func (kml *uprobeMultiLink) Update(prog *ebpf.Program) error { | ||
return fmt.Errorf("update uprobe_multi: %w", ErrNotSupported) | ||
} | ||
|
||
func (kml *uprobeMultiLink) Pin(string) error { | ||
return fmt.Errorf("pin uprobe_multi: %w", ErrNotSupported) | ||
} | ||
|
||
func (kml *uprobeMultiLink) Unpin() error { | ||
return fmt.Errorf("unpin uprobe_multi: %w", ErrNotSupported) | ||
} | ||
|
||
var haveBPFLinkUprobeMulti = internal.NewFeatureTest("bpf_link_uprobe_multi", "6.6", func() error { | ||
prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{ | ||
Name: "probe_upm_link", | ||
Type: ebpf.Kprobe, | ||
Instructions: asm.Instructions{ | ||
asm.Mov.Imm(asm.R0, 0), | ||
asm.Return(), | ||
}, | ||
AttachType: ebpf.AttachTraceUprobeMulti, | ||
License: "MIT", | ||
}) | ||
if errors.Is(err, unix.E2BIG) { | ||
// Kernel doesn't support AttachType field. | ||
return internal.ErrNotSupported | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
defer prog.Close() | ||
|
||
fd, err := sys.LinkCreateUprobeMulti(&sys.LinkCreateUprobeMultiAttr{ | ||
ProgFd: uint32(prog.FD()), | ||
AttachType: sys.BPF_TRACE_UPROBE_MULTI, | ||
Path: sys.NewStringPointer("/"), | ||
Offsets: sys.NewPointer(unsafe.Pointer(&[]uint64{0})), | ||
Count: 1, | ||
}) | ||
switch { | ||
case errors.Is(err, unix.EBADF): | ||
return nil | ||
case err != nil: | ||
return internal.ErrNotSupported | ||
} | ||
// should not happen | ||
fd.Close() | ||
return internal.ErrNotSupported | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package link | ||
|
||
import ( | ||
"errors" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/cilium/ebpf" | ||
"github.com/cilium/ebpf/internal/testutils" | ||
"github.com/cilium/ebpf/internal/unix" | ||
) | ||
|
||
func TestUprobeMulti(t *testing.T) { | ||
testutils.SkipIfNotSupported(t, haveBPFLinkUprobeMulti()) | ||
|
||
prog := mustLoadProgram(t, ebpf.Kprobe, ebpf.AttachTraceUprobeMulti, "") | ||
|
||
um, err := bashEx.UprobeMulti([]string{"bash_logout", "main"}, prog, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer um.Close() | ||
|
||
testLink(t, um, prog) | ||
} | ||
|
||
func TestUprobeMultiInput(t *testing.T) { | ||
testutils.SkipIfNotSupported(t, haveBPFLinkUprobeMulti()) | ||
|
||
prog := mustLoadProgram(t, ebpf.Kprobe, ebpf.AttachTraceUprobeMulti, "") | ||
|
||
// One of Symbols or Addresses must be given. | ||
_, err := bashEx.UprobeMulti([]string{}, prog, nil) | ||
if !errors.Is(err, errInvalidInput) { | ||
t.Fatalf("expected errInvalidInput, got: %v", err) | ||
} | ||
|
||
// One Symbol, two cookies.. | ||
_, err = bashEx.UprobeMulti([]string{}, prog, &UprobeMultiOptions{ | ||
Offsets: []uint64{1}, | ||
Cookies: []uint64{2, 3}, | ||
}) | ||
if !errors.Is(err, errInvalidInput) { | ||
t.Fatalf("expected errInvalidInput, got: %v", err) | ||
} | ||
} | ||
|
||
func TestUprobeMultiErrors(t *testing.T) { | ||
testutils.SkipIfNotSupported(t, haveBPFLinkUprobeMulti()) | ||
|
||
prog := mustLoadProgram(t, ebpf.Kprobe, ebpf.AttachTraceUprobeMulti, "") | ||
|
||
// Wrong offset | ||
um, err := bashEx.UprobeMulti([]string{}, prog, &UprobeMultiOptions{Offsets: []uint64{1<<64 - 1}}) | ||
if !errors.Is(err, unix.EINVAL) { | ||
um.Close() | ||
t.Skipf("need kernel change, skipping for now, expected EINVAL, got: %s", err) | ||
} | ||
} | ||
|
||
func TestUprobeMultiCookie(t *testing.T) { | ||
testutils.SkipIfNotSupported(t, haveBPFLinkUprobeMulti()) | ||
|
||
prog := mustLoadProgram(t, ebpf.Kprobe, ebpf.AttachTraceUprobeMulti, "") | ||
|
||
um, err := bashEx.UprobeMulti([]string{"bash_logout", "main"}, prog, | ||
&UprobeMultiOptions{ | ||
Cookies: []uint64{1, 2}, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
_ = um.Close() | ||
} | ||
|
||
func TestUprobeMultiProgramCall(t *testing.T) { | ||
testutils.SkipIfNotSupported(t, haveBPFLinkUprobeMulti()) | ||
|
||
args := []string{"--help"} | ||
elf := "/bin/bash" | ||
|
||
m, p := newUpdaterMapProg(t, ebpf.Kprobe, ebpf.AttachTraceUprobeMulti) | ||
|
||
// Load the executable. | ||
ex, err := OpenExecutable(elf) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Open Uprobe on the executable for the given symbol | ||
// and attach it to the ebpf program created above. | ||
um, err := ex.UprobeMulti([]string{"main", "_start"}, p, nil) | ||
if errors.Is(err, ErrNoSymbol) { | ||
// Assume bash::main and go::main.main always exists | ||
// and skip the test if the symbol can't be found as | ||
// certain OS (eg. Debian) strip binaries. | ||
t.Skipf("executable %s appear to be stripped, skipping", elf) | ||
} | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Trigger ebpf program call. | ||
trigger := func(t *testing.T) { | ||
if err := exec.Command(elf, args...).Run(); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
trigger(t) | ||
|
||
// Assert that the value at index 0 has been updated to 1. | ||
assertMapValue(t, m, 0, 1) | ||
|
||
// Detach the Uprobe. | ||
if err := um.Close(); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Reset map value to 0 at index 0. | ||
if err := m.Update(uint32(0), uint32(0), ebpf.UpdateExist); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Retrigger the ebpf program call. | ||
trigger(t) | ||
|
||
// Assert that this time the value has not been updated. | ||
assertMapValue(t, m, 0, 0) | ||
} | ||
|
||
func TestHaveBPFLinkUprobeMulti(t *testing.T) { | ||
testutils.CheckFeatureTest(t, haveBPFLinkUprobeMulti) | ||
} |