Skip to content

Commit

Permalink
Use common code for goprobes (partially)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelroquetto committed Nov 29, 2024
1 parent 1706104 commit 901ae57
Show file tree
Hide file tree
Showing 13 changed files with 357 additions and 286 deletions.
10 changes: 6 additions & 4 deletions pkg/internal/discover/typer.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,13 @@ func (t *typer) loadAllGoFunctionNames() {
uniqueFunctions := map[string]struct{}{}
t.allGoFunctions = nil
for _, p := range newGoTracersGroup(t.cfg, t.metrics) {
for funcName := range p.GoProbes() {
goProbes := p.GoProbes()
for i := range goProbes {
funcProg := &goProbes[i]
// avoid duplicating function names
if _, ok := uniqueFunctions[funcName]; !ok {
uniqueFunctions[funcName] = struct{}{}
t.allGoFunctions = append(t.allGoFunctions, funcName)
if _, ok := uniqueFunctions[funcProg.SymbolName]; !ok {
uniqueFunctions[funcProg.SymbolName] = struct{}{}
t.allGoFunctions = append(t.allGoFunctions, funcProg.SymbolName)
}
}
}
Expand Down
30 changes: 17 additions & 13 deletions pkg/internal/ebpf/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/ringbuf"

"github.com/grafana/beyla/pkg/internal/goexec"
"github.com/grafana/beyla/pkg/internal/request"
)

Expand Down Expand Up @@ -43,15 +42,11 @@ var IntegrityModeOverride = false

var ActiveNamespaces = make(map[uint32]uint32)

// Probe holds the information of the instrumentation points of a given function: its start and end offsets and
// eBPF programs
type Probe struct {
Offsets goexec.FuncOffsets
Programs FunctionPrograms
}

type FunctionPrograms struct {
SymbolName string
// ProbeDesc holds the information of the instrumentation points of a given
// function/symbol
type ProbeDesc struct {
// The symbol being instrumented
SymbolName string // The symbol being instrumented

// Required, if true, will cancel the execution of the eBPF Tracer
// if the function has not been found in the executable
Expand All @@ -65,10 +60,19 @@ type FunctionPrograms struct {
// cannot be used. Be careful though, as RET may not always be present in
// the target code, as a result of compiler optimisations
AttachToOffsets bool
Start *ebpf.Program
End *ebpf.Program

StartOffset uint64
// The eBPF program to attach to the symbol as a uprobe (either to the
// symbol name or to StartOffset)
Start *ebpf.Program

// The eBPF program to attach to the symbol either as a uretprobe or as a
// uprobe to ReturnOffsets
End *ebpf.Program

// Optional offset to the start of the symbol
StartOffset uint64

// Optional list of the offsets of every RET instruction in the symbol
ReturnOffsets []uint64
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/internal/ebpf/generictracer/generictracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,12 @@ func (p *Tracer) AddCloser(c ...io.Closer) {
p.closers = append(p.closers, c...)
}

func (p *Tracer) GoProbes() map[string][]ebpfcommon.FunctionPrograms {
func (p *Tracer) GoProbes() []ebpfcommon.ProbeDesc {
return nil
}

func (p *Tracer) KProbes() map[string]ebpfcommon.FunctionPrograms {
return map[string]ebpfcommon.FunctionPrograms{
func (p *Tracer) KProbes() map[string]ebpfcommon.ProbeDesc {
return map[string]ebpfcommon.ProbeDesc{
// Both sys accept probes use the same kretprobe.
// We could tap into __sys_accept4, but we might be more prone to
// issues with the internal kernel code changing.
Expand Down Expand Up @@ -287,12 +287,12 @@ func (p *Tracer) KProbes() map[string]ebpfcommon.FunctionPrograms {
}
}

func (p *Tracer) Tracepoints() map[string]ebpfcommon.FunctionPrograms {
func (p *Tracer) Tracepoints() map[string]ebpfcommon.ProbeDesc {
return nil
}

func (p *Tracer) UProbes() map[string][]ebpfcommon.FunctionPrograms {
return map[string][]ebpfcommon.FunctionPrograms{
func (p *Tracer) UProbes() map[string][]ebpfcommon.ProbeDesc {
return map[string][]ebpfcommon.ProbeDesc{
"libssl.so": {
{
SymbolName: "SSL_read",
Expand Down
8 changes: 4 additions & 4 deletions pkg/internal/ebpf/generictracer/generictracer_notlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ func (p *Tracer) BlockPID(_, _ uint32) {}
func (p *Tracer) Load() (*ebpf.CollectionSpec, error) { return nil, nil }
func (p *Tracer) BpfObjects() any { return nil }
func (p *Tracer) AddCloser(_ ...io.Closer) {}
func (p *Tracer) GoProbes() map[string][]ebpfcommon.FunctionPrograms { return nil }
func (p *Tracer) KProbes() map[string]ebpfcommon.FunctionPrograms { return nil }
func (p *Tracer) UProbes() map[string][]ebpfcommon.FunctionPrograms { return nil }
func (p *Tracer) Tracepoints() map[string]ebpfcommon.FunctionPrograms { return nil }
func (p *Tracer) GoProbes() []ebpfcommon.ProbeDesc { return nil }
func (p *Tracer) KProbes() map[string]ebpfcommon.ProbeDesc { return nil }
func (p *Tracer) UProbes() map[string][]ebpfcommon.ProbeDesc { return nil }
func (p *Tracer) Tracepoints() map[string]ebpfcommon.ProbeDesc { return nil }
func (p *Tracer) SocketFilters() []*ebpf.Program { return nil }
func (p *Tracer) SockMsgs() []ebpfcommon.SockMsg { return nil }
func (p *Tracer) SockOps() []ebpfcommon.SockOps { return nil }
Expand Down
Loading

0 comments on commit 901ae57

Please sign in to comment.