Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elf: Add kprobe EventNameOption for unique eventName per Module #293

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 34 additions & 4 deletions elf/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ type Module struct {
schedPrograms map[string]*SchedProgram
xdpPrograms map[string]*XDPProgram

kprobeEvOpts EventNameOption

compatProbe bool // try to be automatically convert function names depending on kernel versions (SyS_ and __x64_sys_)
}

Expand All @@ -150,6 +152,10 @@ type Kprobe struct {
efd int
}

// EventNameOption is a function type which take eventName
// then return the new eventName string
type EventNameOption func(eventName string) string

type Uprobe struct {
Name string
insns *C.struct_bpf_insn
Expand Down Expand Up @@ -331,6 +337,30 @@ func (b *Module) EnableOptionCompatProbe() {
b.compatProbe = true
}

// TrySetKprobeEventNameOpts Try to set kprobe EventNameOption function.
// If Module.kprobeEvOpts already set, return error.
func (b *Module) TrySetKprobeEventNameOpts(evOpts EventNameOption) error {
if b.kprobeEvOpts != nil {
return fmt.Errorf("kprobe eventname option is already set. this might corrupt")
}
// To prevent any error, detour safeEventName function to remove illegal characters.
b.kprobeEvOpts = func(eventName string) string {
return safeEventName(evOpts(eventName))
}
return nil
}

func (b *Module) kprobeEventName(originalEventName string) string {
var eventName string
if b.kprobeEvOpts != nil {
eventName = b.kprobeEvOpts(originalEventName)
} else {
eventName = originalEventName
}
return eventName
}


// EnableKprobe enables a kprobe/kretprobe identified by secName.
// For kretprobes, you can configure the maximum number of instances
// of the function that can be probed simultaneously with maxactive.
Expand All @@ -356,7 +386,7 @@ func (b *Module) EnableKprobe(secName string, maxactive int) error {
probeType = "p"
funcName = strings.TrimPrefix(secName, "kprobe/")
}
eventName := probeType + funcName
eventName := b.kprobeEventName(probeType + funcName)

kprobeId, err := writeKprobeEvent(probeType, eventName, funcName, maxactiveStr)
// fallback without maxactive
Expand Down Expand Up @@ -745,10 +775,10 @@ func (b *Module) closeProbes() error {
var err error
if isKretprobe {
funcName = strings.TrimPrefix(name, "kretprobe/")
err = disableKprobe("r" + funcName)
err = disableKprobe(b.kprobeEventName("r" + funcName))
} else {
funcName = strings.TrimPrefix(name, "kprobe/")
err = disableKprobe("p" + funcName)
err = disableKprobe(b.kprobeEventName("p" + funcName))
}
if err != nil {
return fmt.Errorf("error clearing probe: %v", err)
Expand Down Expand Up @@ -923,4 +953,4 @@ func (b *Module) CloseExt(options map[string]CloseOptions) error {
return err
}
return nil
}
}
4 changes: 4 additions & 0 deletions elf/module_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func (b *Module) EnableTracepoint(secName string) error {
return errNotSupported
}

func (b *Module) TrySetKprobeEventNameOpts(evOpts EventNameOption) error {
return errNotSupported
}

func (b *Module) IterMaps() <-chan *Map {
return nil
}
Expand Down