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

[Dynamic Instrumentation] Fix stability issues #34340

Open
wants to merge 7 commits into
base: main
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pkg/dynamicinstrumentation/diconfig/binary_inspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ import (
// configEvent maps service names to info about the service and their configurations
func inspectGoBinaries(configEvent ditypes.DIProcs) error {
var err error
var inspectedAtLeastOneBinary bool
for i := range configEvent {
err = AnalyzeBinary(configEvent[i])
if err != nil {
return fmt.Errorf("inspection of PID %d (path=%s) failed: %w", configEvent[i].PID, configEvent[i].BinaryPath, err)
log.Info("inspection of PID %d (path=%s) failed: %w", configEvent[i].PID, configEvent[i].BinaryPath, err)
} else {
inspectedAtLeastOneBinary = true
}
}

if !inspectedAtLeastOneBinary {
return fmt.Errorf("failed to inspect all tracked go binaries")
}

return nil
}

Expand Down
13 changes: 7 additions & 6 deletions pkg/dynamicinstrumentation/diconfig/config_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ func (cm *RCConfigManager) readConfigs(r *ringbuf.Reader, procInfo *ditypes.Proc

// Check hash to see if the configuration changed
if configPath.Hash != probe.InstrumentationInfo.ConfigurationHash {
err := AnalyzeBinary(procInfo)
if err != nil {
log.Errorf("couldn't inspect binary: %v\n", err)
continue
}

probe.InstrumentationInfo.ConfigurationHash = configPath.Hash
applyConfigUpdate(procInfo, probe)
}
Expand All @@ -244,14 +250,9 @@ func (cm *RCConfigManager) readConfigs(r *ringbuf.Reader, procInfo *ditypes.Proc

func applyConfigUpdate(procInfo *ditypes.ProcessInfo, probe *ditypes.Probe) {
log.Tracef("Applying config update: %v\n", probe)
err := AnalyzeBinary(procInfo)
if err != nil {
log.Errorf("couldn't inspect binary: %v\n", err)
return
}

generateCompileAttach:
err = codegen.GenerateBPFParamsCode(procInfo, probe)
err := codegen.GenerateBPFParamsCode(procInfo, probe)
if err != nil {
log.Info("Couldn't generate BPF programs", err)
if !probe.InstrumentationInfo.AttemptedRebuild {
Expand Down
10 changes: 10 additions & 0 deletions pkg/dynamicinstrumentation/diconfig/location_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ func GenerateLocationExpression(limitsInfo *ditypes.InstrumentationInfo, param *
}
slicePointer := elementParam.ParameterPieces[0]
sliceLength := elementParam.ParameterPieces[1]

if slicePointer == nil || sliceLength == nil {
continue
}

sliceLength.LocationExpressions = append(sliceLength.LocationExpressions,
ditypes.PrintStatement("%s", "Reading the length of slice"),
)
Expand All @@ -188,6 +193,11 @@ func GenerateLocationExpression(limitsInfo *ditypes.InstrumentationInfo, param *
// Generate and collect the location expressions for collecting an individual
// element of this slice
sliceElementType := slicePointer.ParameterPieces[0]

if sliceElementType == nil {
continue
}

sliceIdentifier := randomLabel()
labelName := randomLabel()

Expand Down
14 changes: 11 additions & 3 deletions pkg/dynamicinstrumentation/diconfig/mem_config_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,17 @@ func (cm *ReaderConfigManager) update() error {
for pid, proc := range cm.ConfigWriter.Processes {
// If a config exists relevant to this proc
if proc.ServiceName == serviceName {
procCopy := *proc
updatedState[pid] = &procCopy
updatedState[pid].ProbesByID = convert(serviceName, configsByID)
updatedState[pid] = &ditypes.ProcessInfo{
PID: proc.PID,
ServiceName: proc.ServiceName,
RuntimeID: proc.RuntimeID,
BinaryPath: proc.BinaryPath,
TypeMap: proc.TypeMap,
ConfigurationUprobe: proc.ConfigurationUprobe,
InstrumentationUprobes: proc.InstrumentationUprobes,
InstrumentationObjects: proc.InstrumentationObjects,
ProbesByID: convert(serviceName, configsByID),
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/dynamicinstrumentation/ditypes/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"io"
"strconv"
"strings"
"sync"

"github.com/DataDog/datadog-agent/pkg/util/log"

Expand Down Expand Up @@ -138,6 +139,7 @@ type ProcessInfo struct {
ProbesByID ProbesByID
InstrumentationUprobes map[ProbeID]*link.Link
InstrumentationObjects map[ProbeID]*ebpf.Collection
mu sync.RWMutex
}

// SetupConfigUprobe sets the configuration probe for the process
Expand Down Expand Up @@ -172,12 +174,16 @@ func (pi *ProcessInfo) CloseConfigUprobe() error {
// SetUprobeLink associates the uprobe link with the specified probe
// in the tracked process
func (pi *ProcessInfo) SetUprobeLink(probeID ProbeID, l *link.Link) {
pi.mu.Lock()
defer pi.mu.Unlock()
pi.InstrumentationUprobes[probeID] = l
}

// CloseUprobeLink closes the probe and deletes the link for the probe
// in the tracked process
func (pi *ProcessInfo) CloseUprobeLink(probeID ProbeID) error {
pi.mu.Lock()
Copy link
Contributor

@tylfin tylfin Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably utilize a sync.Map here to ensure InstrumentationUprobes is thread-safe. Does the use-case fit the criteria?

The sync.Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys

Otherwise, you'll want to switch this to an RW mutex and ensure all usages (e.g. L198) are using it appropriately.

defer pi.mu.Unlock()
if l, ok := pi.InstrumentationUprobes[probeID]; ok {
err := (*l).Close()
delete(pi.InstrumentationUprobes, probeID)
Expand Down