Skip to content

Commit

Permalink
Adapt the uprobe attacher
Browse files Browse the repository at this point in the history
  • Loading branch information
gjulianm committed Sep 13, 2024
1 parent 004c367 commit c5b78e9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 29 deletions.
39 changes: 13 additions & 26 deletions pkg/ebpf/uprobes/attacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ func (ua *UprobeAttacher) handlesExecutables() bool {
// Start starts the attacher, attaching to the processes and libraries as needed
func (ua *UprobeAttacher) Start() error {
var cleanupExec, cleanupExit func()
var cleanupSharedLibs func()

procMonitor := monitor.GetProcessMonitor()
err := procMonitor.Initialize(ua.config.ProcessMonitorEventStream)
if err != nil {
Expand All @@ -397,16 +399,14 @@ func (ua *UprobeAttacher) Start() error {
return errors.New("shared libraries tracing not supported for this platform")
}

ua.soWatcher = sharedlibraries.NewEBPFProgram(ua.config.EbpfConfig)
ua.soWatcher = sharedlibraries.GetEBPFProgram(ua.config.EbpfConfig)

err := ua.soWatcher.Init()
if err != nil {
return fmt.Errorf("error initializing shared library program: %w", err)
}
err = ua.soWatcher.Start()
if err != nil {
return fmt.Errorf("error starting shared library program: %w", err)
}

cleanupSharedLibs = ua.soWatcher.Subscribe(ua.handleLibraryOpen)
}

if ua.config.PerformInitialScan {
Expand All @@ -433,33 +433,20 @@ func (ua *UprobeAttacher) Start() error {
if ua.soWatcher != nil {
ua.soWatcher.Stop()
}
if cleanupSharedLibs != nil {
cleanupSharedLibs()
}
ua.wg.Done()
log.Infof("uprobe attacher %s stopped", ua.name)
}()

var sharedLibDataChan <-chan ebpf.DataEvent
var sharedLibLostChan <-chan uint64

if ua.soWatcher != nil {
sharedLibDataChan = ua.soWatcher.GetPerfHandler().DataChannel()
sharedLibLostChan = ua.soWatcher.GetPerfHandler().LostChannel()
}

for {
select {
case <-ua.done:
return
case <-processSync.C:
// We always track process deletions in the scan, to avoid memory leaks.
_ = ua.Sync(ua.config.EnablePeriodicScanNewProcesses, true)
case event, ok := <-sharedLibDataChan:
if !ok {
return
}
_ = ua.handleLibraryOpen(&event)
case <-sharedLibLostChan:
// Nothing to do in this case
break
}
}
}()
Expand Down Expand Up @@ -533,13 +520,13 @@ func (ua *UprobeAttacher) handleProcessExit(pid uint32) {
_ = ua.DetachPID(pid)
}

func (ua *UprobeAttacher) handleLibraryOpen(event *ebpf.DataEvent) error {
defer event.Done()

libpath := sharedlibraries.ToLibPath(event.Data)
func (ua *UprobeAttacher) handleLibraryOpen(libpath sharedlibraries.LibPath) {
path := sharedlibraries.ToBytes(&libpath)

return ua.AttachLibrary(string(path), libpath.Pid)
err := ua.AttachLibrary(string(path), libpath.Pid)
if err != nil {
log.Errorf("error attaching to library %s (PID %d): %v", path, libpath.Pid, err)
}
}

func (ua *UprobeAttacher) buildRegisterCallbacks(matchingRules []*AttachRule, procInfo *ProcInfo) (func(utils.FilePath) error, func(utils.FilePath) error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/network/usm/sharedlibraries/ebpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func IsSupported(cfg *ddebpf.Config) bool {
return kversion >= kernel.VersionCode(4, 14, 0)
}

// NewEBPFProgram returns an instance of the shared libraries eBPF program singleton
func NewEBPFProgram(cfg *ddebpf.Config) *EbpfProgram {
// GetEBPFProgram returns an instance of the shared libraries eBPF program singleton
func GetEBPFProgram(cfg *ddebpf.Config) *EbpfProgram {
progSingletonOnce.Do(func() {
progSingleton = newEBPFProgram(cfg)
})
Expand Down Expand Up @@ -185,7 +185,7 @@ func (e *EbpfProgram) start() error {
if !ok {
return
}
e.handleEvent(event)
e.handleEvent(&event)
case <-lostChan:
// Nothing to do in this case
break
Expand Down

0 comments on commit c5b78e9

Please sign in to comment.