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

[usm] add module name to debugger #31003

Merged
merged 15 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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: 5 additions & 5 deletions cmd/system-probe/modules/network_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,11 @@ func (nt *networkTracer) Register(httpMux *module.Router) error {
})

httpMux.HandleFunc("/debug/usm_telemetry", telemetry.Handler)
httpMux.HandleFunc("/debug/usm/traced_programs", usm.TracedProgramsEndpoint)
httpMux.HandleFunc("/debug/usm/blocked_processes", usm.BlockedPathIDEndpoint)
httpMux.HandleFunc("/debug/usm/clear_blocked", usm.ClearBlockedEndpoint)
httpMux.HandleFunc("/debug/usm/attach-pid", usm.AttachPIDEndpoint)
httpMux.HandleFunc("/debug/usm/detach-pid", usm.DetachPIDEndpoint)
httpMux.HandleFunc("/debug/usm/traced_programs", usm.GetTracedProgramsEndpoint("usm"))
httpMux.HandleFunc("/debug/usm/blocked_processes", usm.GetBlockedPathIDEndpoint("usm"))
httpMux.HandleFunc("/debug/usm/clear_blocked", usm.GetClearBlockedEndpoint("usm"))
httpMux.HandleFunc("/debug/usm/attach-pid", usm.GetAttachPIDEndpoint("usm"))
httpMux.HandleFunc("/debug/usm/detach-pid", usm.GetDetachPIDEndpoint("usm"))

// Convenience logging if nothing has made any requests to the system-probe in some time, let's log something.
// This should be helpful for customers + support to debug the underlying issue.
Expand Down
6 changes: 3 additions & 3 deletions pkg/ebpf/uprobes/attacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ type UprobeAttacher struct {
// way).
// - The process monitor to be used to subscribe to process start and exit events. The lifecycle of the process monitor is managed by the caller, the attacher
// will not stop the monitor when it stops.
func NewUprobeAttacher(name string, config AttacherConfig, mgr ProbeManager, onAttachCallback AttachCallback, inspector BinaryInspector, processMonitor ProcessMonitor) (*UprobeAttacher, error) {
func NewUprobeAttacher(moduleName, name string, config AttacherConfig, mgr ProbeManager, onAttachCallback AttachCallback, inspector BinaryInspector, processMonitor ProcessMonitor) (*UprobeAttacher, error) {
config.SetDefaults()

if err := config.Validate(); err != nil {
Expand All @@ -346,7 +346,7 @@ func NewUprobeAttacher(name string, config AttacherConfig, mgr ProbeManager, onA
ua := &UprobeAttacher{
name: name,
config: config,
fileRegistry: utils.NewFileRegistry(name),
fileRegistry: utils.NewFileRegistry(moduleName, name),
manager: mgr,
onAttachCallback: onAttachCallback,
pathToAttachedProbes: make(map[string][]manager.ProbeIdentificationPair),
Expand All @@ -355,7 +355,7 @@ func NewUprobeAttacher(name string, config AttacherConfig, mgr ProbeManager, onA
processMonitor: processMonitor,
}

utils.AddAttacher(name, ua)
utils.AddAttacher(moduleName, name, ua)

return ua, nil
}
Expand Down
42 changes: 24 additions & 18 deletions pkg/ebpf/uprobes/attacher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ import (

// === Tests

const (
testModuleName = "mock-module"
testAttacherName = "mock"
)

func TestCanCreateAttacher(t *testing.T) {
ua, err := NewUprobeAttacher("mock", AttacherConfig{}, &MockManager{}, nil, nil, newMockProcessMonitor())
ua, err := NewUprobeAttacher(testModuleName, testAttacherName, AttacherConfig{}, &MockManager{}, nil, nil, newMockProcessMonitor())
require.NoError(t, err)
require.NotNil(t, ua)
}
Expand All @@ -51,7 +56,7 @@ func TestAttachPidExcludesInternal(t *testing.T) {
ExcludeTargets: ExcludeInternal,
ProcRoot: procRoot,
}
ua, err := NewUprobeAttacher("mock", config, &MockManager{}, nil, nil, newMockProcessMonitor())
ua, err := NewUprobeAttacher(testModuleName, testAttacherName, config, &MockManager{}, nil, nil, newMockProcessMonitor())
require.NoError(t, err)
require.NotNil(t, ua)

Expand All @@ -63,7 +68,7 @@ func TestAttachPidExcludesSelf(t *testing.T) {
config := AttacherConfig{
ExcludeTargets: ExcludeSelf,
}
ua, err := NewUprobeAttacher("mock", config, &MockManager{}, nil, nil, newMockProcessMonitor())
ua, err := NewUprobeAttacher(testModuleName, testAttacherName, config, &MockManager{}, nil, nil, newMockProcessMonitor())
require.NoError(t, err)
require.NotNil(t, ua)

Expand All @@ -77,7 +82,7 @@ func TestGetExecutablePath(t *testing.T) {
config := AttacherConfig{
ProcRoot: procRoot,
}
ua, err := NewUprobeAttacher("mock", config, &MockManager{}, nil, nil, newMockProcessMonitor())
ua, err := NewUprobeAttacher(testModuleName, testAttacherName, config, &MockManager{}, nil, nil, newMockProcessMonitor())
require.NoError(t, err)
require.NotNil(t, ua)

Expand Down Expand Up @@ -120,7 +125,7 @@ func TestGetLibrariesFromMapsFile(t *testing.T) {
config := AttacherConfig{
ProcRoot: procRoot,
}
ua, err := NewUprobeAttacher("mock", config, &MockManager{}, nil, nil, newMockProcessMonitor())
ua, err := NewUprobeAttacher(testModuleName, testAttacherName, config, &MockManager{}, nil, nil, newMockProcessMonitor())
require.NoError(t, err)
require.NotNil(t, ua)

Expand All @@ -132,7 +137,7 @@ func TestGetLibrariesFromMapsFile(t *testing.T) {
}

func TestComputeRequestedSymbols(t *testing.T) {
ua, err := NewUprobeAttacher("mock", AttacherConfig{}, &MockManager{}, nil, nil, newMockProcessMonitor())
ua, err := NewUprobeAttacher(testModuleName, testAttacherName, AttacherConfig{}, &MockManager{}, nil, nil, newMockProcessMonitor())
require.NoError(t, err)
require.NotNil(t, ua)

Expand Down Expand Up @@ -204,7 +209,7 @@ func TestComputeRequestedSymbols(t *testing.T) {
}

func TestStartAndStopWithoutLibraryWatcher(t *testing.T) {
ua, err := NewUprobeAttacher("mock", AttacherConfig{}, &MockManager{}, nil, nil, newMockProcessMonitor())
ua, err := NewUprobeAttacher(testModuleName, testAttacherName, AttacherConfig{}, &MockManager{}, nil, nil, newMockProcessMonitor())
require.NoError(t, err)
require.NotNil(t, ua)

Expand All @@ -223,7 +228,7 @@ func TestStartAndStopWithLibraryWatcher(t *testing.T) {
}

rules := []*AttachRule{{LibraryNameRegex: regexp.MustCompile(`libssl.so`), Targets: AttachToSharedLibraries}}
ua, err := NewUprobeAttacher("mock", AttacherConfig{Rules: rules, EbpfConfig: ebpfCfg}, &MockManager{}, nil, nil, newMockProcessMonitor())
ua, err := NewUprobeAttacher(testModuleName, testAttacherName, AttacherConfig{Rules: rules, EbpfConfig: ebpfCfg}, &MockManager{}, nil, nil, newMockProcessMonitor())
require.NoError(t, err)
require.NotNil(t, ua)
require.True(t, ua.handlesLibraries())
Expand Down Expand Up @@ -283,7 +288,7 @@ func TestMonitor(t *testing.T) {
}},
EbpfConfig: ebpfCfg,
}
ua, err := NewUprobeAttacher("mock", config, &MockManager{}, nil, nil, procMon)
ua, err := NewUprobeAttacher(testModuleName, testAttacherName, config, &MockManager{}, nil, nil, procMon)
require.NoError(t, err)
require.NotNil(t, ua)

Expand All @@ -305,8 +310,9 @@ func TestMonitor(t *testing.T) {
return methodHasBeenCalledAtLeastTimes(mockRegistry, "Register", 2)
}, 1500*time.Millisecond, 10*time.Millisecond, "received calls %v", mockRegistry.Calls)

mockRegistry.AssertCalled(t, "Register", lib, uint32(cmd.Process.Pid), mock.Anything, mock.Anything, mock.Anything)
mockRegistry.AssertCalled(t, "Register", cmd.Path, uint32(cmd.Process.Pid), mock.Anything, mock.Anything, mock.Anything)
mockRegistry.AssertCalled(t, "Register", lib, uint32(cmd.Process.Pid), mock.Anything, mock.Anything, mock.Anything)

}

func TestSync(t *testing.T) {
Expand All @@ -333,7 +339,7 @@ func TestSync(t *testing.T) {
EnablePeriodicScanNewProcesses: true,
}

ua, err := NewUprobeAttacher("mock", config, &MockManager{}, nil, nil, newMockProcessMonitor())
ua, err := NewUprobeAttacher(testModuleName, testAttacherName, config, &MockManager{}, nil, nil, newMockProcessMonitor())
require.NoError(tt, err)
require.NotNil(tt, ua)

Expand Down Expand Up @@ -365,7 +371,7 @@ func TestSync(t *testing.T) {
EnablePeriodicScanNewProcesses: true,
}

ua, err := NewUprobeAttacher("mock", config, &MockManager{}, nil, nil, newMockProcessMonitor())
ua, err := NewUprobeAttacher(testModuleName, testAttacherName, config, &MockManager{}, nil, nil, newMockProcessMonitor())
require.NoError(tt, err)
require.NotNil(tt, ua)

Expand Down Expand Up @@ -441,7 +447,7 @@ func TestAttachToBinaryAndDetach(t *testing.T) {

mockMan := &MockManager{}
inspector := &MockBinaryInspector{}
ua, err := NewUprobeAttacher("mock", config, mockMan, nil, inspector, newMockProcessMonitor())
ua, err := NewUprobeAttacher(testModuleName, testAttacherName, config, mockMan, nil, inspector, newMockProcessMonitor())
require.NoError(t, err)
require.NotNil(t, ua)

Expand Down Expand Up @@ -502,7 +508,7 @@ func TestAttachToBinaryAtReturnLocation(t *testing.T) {

mockMan := &MockManager{}
inspector := &MockBinaryInspector{}
ua, err := NewUprobeAttacher("mock", config, mockMan, nil, inspector, newMockProcessMonitor())
ua, err := NewUprobeAttacher(testModuleName, testAttacherName, config, mockMan, nil, inspector, newMockProcessMonitor())
require.NoError(t, err)
require.NotNil(t, ua)

Expand Down Expand Up @@ -583,7 +589,7 @@ func TestAttachToLibrariesOfPid(t *testing.T) {
mockMan := &MockManager{}
inspector := &MockBinaryInspector{}
registry := &MockFileRegistry{}
ua, err := NewUprobeAttacher("mock", config, mockMan, nil, inspector, newMockProcessMonitor())
ua, err := NewUprobeAttacher(testModuleName, testAttacherName, config, mockMan, nil, inspector, newMockProcessMonitor())
require.NoError(t, err)
require.NotNil(t, ua)
ua.fileRegistry = registry
Expand Down Expand Up @@ -700,7 +706,7 @@ func TestUprobeAttacher(t *testing.T) {
attachedProbes = append(attachedProbes, attachedProbe{probe: probe, fpath: fpath})
}

ua, err := NewUprobeAttacher("test", attacherCfg, &mgr, callback, &NativeBinaryInspector{}, procMon)
ua, err := NewUprobeAttacher(testModuleName, testAttacherName, attacherCfg, &mgr, callback, &NativeBinaryInspector{}, procMon)
require.NoError(t, err)
require.NotNil(t, ua)

Expand Down Expand Up @@ -808,7 +814,7 @@ func (s *SharedLibrarySuite) TestSingleFile() {
PerformInitialScan: false,
}

ua, err := NewUprobeAttacher("test", attachCfg, &MockManager{}, nil, nil, s.procMonitor)
ua, err := NewUprobeAttacher(testModuleName, testAttacherName, attachCfg, &MockManager{}, nil, nil, s.procMonitor)
require.NoError(t, err)

mockRegistry := &MockFileRegistry{}
Expand Down Expand Up @@ -885,7 +891,7 @@ func (s *SharedLibrarySuite) TestDetectionWithPIDAndRootNamespace() {
EbpfConfig: ebpfCfg,
}

ua, err := NewUprobeAttacher("test", attachCfg, &MockManager{}, nil, nil, s.procMonitor)
ua, err := NewUprobeAttacher(testModuleName, testAttacherName, attachCfg, &MockManager{}, nil, nil, s.procMonitor)
require.NoError(t, err)

mockRegistry := &MockFileRegistry{}
Expand Down
3 changes: 2 additions & 1 deletion pkg/gpu/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

const (
gpuAttacherName = "gpu"
gpuModuleName = gpuAttacherName
val06 marked this conversation as resolved.
Show resolved Hide resolved

// consumerChannelSize controls the size of the go channel that buffers ringbuffer
// events (*ddebpf.RingBufferHandler).
Expand Down Expand Up @@ -139,7 +140,7 @@ func NewProbe(cfg *config.Config, deps ProbeDependencies) (*Probe, error) {
}
}

p.attacher, err = uprobes.NewUprobeAttacher(gpuAttacherName, attachCfg, p.m, nil, &uprobes.NativeBinaryInspector{}, deps.ProcessMonitor)
p.attacher, err = uprobes.NewUprobeAttacher(gpuModuleName, gpuAttacherName, attachCfg, p.m, nil, &uprobes.NativeBinaryInspector{}, deps.ProcessMonitor)
if err != nil {
return nil, fmt.Errorf("error creating uprobes attacher: %w", err)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/gpu/probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (s *probeTestSuite) TestCanReceiveEvents() {
probe := s.getProbe()
cmd := testutil.RunSample(t, testutil.CudaSample)

utils.WaitForProgramsToBeTraced(t, gpuAttacherName, cmd.Process.Pid, utils.ManualTracingFallbackDisabled)
utils.WaitForProgramsToBeTraced(t, gpuModuleName, gpuAttacherName, cmd.Process.Pid, utils.ManualTracingFallbackDisabled)

var handlerStream, handlerGlobal *StreamHandler
require.Eventually(t, func() bool {
Expand Down Expand Up @@ -108,12 +108,12 @@ func (s *probeTestSuite) TestCanGenerateStats() {

cmd := testutil.RunSample(t, testutil.CudaSample)

utils.WaitForProgramsToBeTraced(t, gpuAttacherName, cmd.Process.Pid, utils.ManualTracingFallbackDisabled)
utils.WaitForProgramsToBeTraced(t, gpuModuleName, gpuAttacherName, cmd.Process.Pid, utils.ManualTracingFallbackDisabled)

// Wait until the process finishes and we can get the stats. Run this instead of waiting for the process to finish
// so that we can time out correctly
require.Eventually(t, func() bool {
return !utils.IsProgramTraced(gpuAttacherName, cmd.Process.Pid)
return !utils.IsProgramTraced(gpuModuleName, gpuAttacherName, cmd.Process.Pid)
}, 20*time.Second, 500*time.Millisecond, "process not stopped")

stats, err := probe.GetAndFlush()
Expand All @@ -139,12 +139,12 @@ func (s *probeTestSuite) TestDetectsContainer() {
args.EndWaitTimeSec = 1
pid, cid := testutil.RunSampleInDockerWithArgs(t, testutil.CudaSample, testutil.MinimalDockerImage, args)

utils.WaitForProgramsToBeTraced(t, gpuAttacherName, pid, utils.ManualTracingFallbackDisabled)
utils.WaitForProgramsToBeTraced(t, gpuModuleName, gpuAttacherName, pid, utils.ManualTracingFallbackDisabled)

// Wait until the process finishes and we can get the stats. Run this instead of waiting for the process to finish
// so that we can time out correctly
require.Eventually(t, func() bool {
return !utils.IsProgramTraced(gpuAttacherName, pid)
return !utils.IsProgramTraced(gpuModuleName, gpuAttacherName, pid)
}, 20*time.Second, 500*time.Millisecond, "process not stopped")

// Check that the stream handlers have the correct container ID assigned
Expand Down
12 changes: 12 additions & 0 deletions pkg/network/usm/consts/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024-present Datadog, Inc.

// Package consts contains constants used by the USM package.
package consts

const (
// USMModuleName is the name of the USM module, that is being used for registering attachers.
USMModuleName = "usm"
)
3 changes: 2 additions & 1 deletion pkg/network/usm/ebpf_gotls.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
libtelemetry "github.com/DataDog/datadog-agent/pkg/network/protocols/telemetry"
"github.com/DataDog/datadog-agent/pkg/network/usm/buildmode"
usmconfig "github.com/DataDog/datadog-agent/pkg/network/usm/config"
"github.com/DataDog/datadog-agent/pkg/network/usm/consts"
"github.com/DataDog/datadog-agent/pkg/network/usm/utils"
"github.com/DataDog/datadog-agent/pkg/process/monitor"
)
Expand Down Expand Up @@ -148,7 +149,7 @@ func newGoTLSProgramProtocolFactory(m *manager.Manager) protocols.ProtocolFactor
}

procMon := monitor.GetProcessMonitor()
attacher, err := uprobes.NewUprobeAttacher(GoTLSAttacherName, attacherCfg, m, nil, inspector, procMon)
attacher, err := uprobes.NewUprobeAttacher(consts.USMModuleName, GoTLSAttacherName, attacherCfg, m, nil, inspector, procMon)
if err != nil {
return nil, fmt.Errorf("cannot create uprobe attacher: %w", err)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/network/usm/ebpf_ssl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/network/config"
"github.com/DataDog/datadog-agent/pkg/network/protocols/http/testutil"
usmconfig "github.com/DataDog/datadog-agent/pkg/network/usm/config"
"github.com/DataDog/datadog-agent/pkg/network/usm/consts"
fileopener "github.com/DataDog/datadog-agent/pkg/network/usm/sharedlibraries/testutil"
"github.com/DataDog/datadog-agent/pkg/network/usm/utils"
)
Expand All @@ -45,9 +46,9 @@ func testArch(t *testing.T, arch string) {
require.NoError(t, err)

if arch == runtime.GOARCH {
utils.WaitForProgramsToBeTraced(t, "shared_libraries", cmd.Process.Pid, utils.ManualTracingFallbackDisabled)
utils.WaitForProgramsToBeTraced(t, consts.USMModuleName, "shared_libraries", cmd.Process.Pid, utils.ManualTracingFallbackDisabled)
} else {
utils.WaitForPathToBeBlocked(t, "shared_libraries", lib)
utils.WaitForPathToBeBlocked(t, consts.USMModuleName, "shared_libraries", lib)
}
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/network/usm/istio.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/DataDog/datadog-agent/pkg/network/config"
"github.com/DataDog/datadog-agent/pkg/network/usm/consts"
"github.com/DataDog/datadog-agent/pkg/network/usm/utils"
"github.com/DataDog/datadog-agent/pkg/process/monitor"
"github.com/DataDog/datadog-agent/pkg/util/kernel"
Expand Down Expand Up @@ -103,7 +104,7 @@ func newIstioMonitor(c *config.Config, mgr *manager.Manager) *istioMonitor {

procRoot := kernel.ProcFSRoot()
return &istioMonitor{
registry: utils.NewFileRegistry("istio"),
registry: utils.NewFileRegistry(consts.USMModuleName, "istio"),
procRoot: procRoot,
envoyCmd: c.EnvoyPath,
done: make(chan struct{}),
Expand Down Expand Up @@ -187,7 +188,7 @@ func (m *istioMonitor) Start() {
}
}()

utils.AddAttacher("istio", m)
utils.AddAttacher(consts.USMModuleName, "istio", m)
log.Info("Istio monitoring enabled")
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/network/usm/kafka_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
gotlsutils "github.com/DataDog/datadog-agent/pkg/network/protocols/tls/gotls/testutil"
"github.com/DataDog/datadog-agent/pkg/network/tracer/testutil/proxy"
usmconfig "github.com/DataDog/datadog-agent/pkg/network/usm/config"
"github.com/DataDog/datadog-agent/pkg/network/usm/consts"
"github.com/DataDog/datadog-agent/pkg/network/usm/utils"
"github.com/DataDog/datadog-agent/pkg/util/kernel"
)
Expand Down Expand Up @@ -560,7 +561,7 @@ func (s *KafkaProtocolParsingSuite) testKafkaProtocolParsing(t *testing.T, tls b
})
monitor := newKafkaMonitor(t, cfg)
if tls && cfg.EnableGoTLSSupport {
utils.WaitForProgramsToBeTraced(t, GoTLSAttacherName, proxyProcess.Process.Pid, utils.ManualTracingFallbackEnabled)
utils.WaitForProgramsToBeTraced(t, consts.USMModuleName, GoTLSAttacherName, proxyProcess.Process.Pid, utils.ManualTracingFallbackEnabled)
}
tt.testBody(t, &tt.context, monitor)
})
Expand Down Expand Up @@ -1158,7 +1159,7 @@ func testKafkaFetchRaw(t *testing.T, tls bool, apiVersion int) {

monitor := newKafkaMonitor(t, getDefaultTestConfiguration(tls))
if tls {
utils.WaitForProgramsToBeTraced(t, GoTLSAttacherName, proxyPid, utils.ManualTracingFallbackEnabled)
utils.WaitForProgramsToBeTraced(t, consts.USMModuleName, GoTLSAttacherName, proxyPid, utils.ManualTracingFallbackEnabled)
}

for _, tt := range tests {
Expand Down Expand Up @@ -1386,7 +1387,7 @@ func testKafkaProduceRaw(t *testing.T, tls bool, apiVersion int) {

monitor := newKafkaMonitor(t, getDefaultTestConfiguration(tls))
if tls {
utils.WaitForProgramsToBeTraced(t, GoTLSAttacherName, proxyPid, utils.ManualTracingFallbackEnabled)
utils.WaitForProgramsToBeTraced(t, consts.USMModuleName, GoTLSAttacherName, proxyPid, utils.ManualTracingFallbackEnabled)
}

for _, tt := range tests {
Expand Down
Loading
Loading