-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add USM debugger #32325
Merged
Merged
Add USM debugger #32325
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# USM debugger | ||
|
||
A minimal, self-contained build of USM for faster iterations of debugging eBPF | ||
code on remote machines. | ||
|
||
Prepare the build for the target architectures you need (the `foo` KMT stack | ||
does not need to exist): | ||
|
||
``` | ||
inv -e kmt.prepare system-probe --compile-only --stack=foo --arch=x86_64 | ||
inv -e kmt.prepare system-probe --compile-only --stack=foo --arch=arm64 | ||
``` | ||
|
||
Build the binary with one of the following commands for the architecture of | ||
your target host: | ||
|
||
``` | ||
inv -e system-probe.build-usm-debugger --arch=x86_64 | ||
inv -e system-probe.build-usm-debugger --arch=arm64 | ||
``` | ||
|
||
Copy the `bin/usm-debugger` to the `system-probe` container in the | ||
`datadog-agent` pod on your target host. Open a shell in that container and | ||
execute the binary. | ||
|
||
The eBPF programs are always built with debug logs enabled so you can view them | ||
with `/sys/kernel/tracing/trace_pipe`. | ||
|
||
If you need to change the system-probe config, edit `cmd/usm_debugger.go` and | ||
rebuild. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// 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. | ||
|
||
//go:build usm_debugger | ||
|
||
package main | ||
|
||
import ( | ||
_ "embed" | ||
"os" | ||
"path" | ||
|
||
"github.com/DataDog/datadog-agent/pkg/util/log" | ||
) | ||
|
||
// The code beloe is essentially responsible for embedding the CO-RE artifacts | ||
vitkyrka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// during compilation time and writing them to a temporary folder during | ||
// runtime, so they can be loaded by the `usm.Monitor` as regular compilation | ||
// assets. | ||
|
||
//go:embed usm-debug.o | ||
var usmProgram []byte | ||
|
||
//go:embed shared-libraries-debug.o | ||
var sharedLibrariesProgram []byte | ||
|
||
func setupBytecode() func() { | ||
type program struct { | ||
filePath string | ||
bytecode []byte | ||
} | ||
|
||
var ( | ||
bytecodeDir = os.TempDir() | ||
coreDir = path.Join(bytecodeDir, "co-re") | ||
) | ||
|
||
os.Setenv("DD_SYSTEM_PROBE_BPF_DIR", bytecodeDir) | ||
err := os.MkdirAll(coreDir, os.ModePerm) | ||
checkError(err) | ||
|
||
programs := []program{ | ||
{path.Join(coreDir, "usm-debug.o"), usmProgram}, | ||
{path.Join(coreDir, "shared-libraries-debug.o"), sharedLibrariesProgram}, | ||
} | ||
|
||
for _, p := range programs { | ||
f, err := os.Create(p.filePath) | ||
checkError(err) | ||
_, err = f.Write(p.bytecode) | ||
checkError(err) | ||
log.Debugf("writing ebpf bytecode to %s", p.filePath) | ||
} | ||
|
||
return func() { | ||
for _, p := range programs { | ||
os.Remove(p.filePath) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// 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. | ||
|
||
//go:build usm_debugger | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/DataDog/datadog-agent/cmd/system-probe/config" | ||
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup" | ||
networkconfig "github.com/DataDog/datadog-agent/pkg/network/config" | ||
"github.com/DataDog/datadog-agent/pkg/network/usm" | ||
pkglogsetup "github.com/DataDog/datadog-agent/pkg/util/log/setup" | ||
) | ||
|
||
func main() { | ||
err := pkglogsetup.SetupLogger( | ||
"usm-debugger", | ||
"debug", | ||
"", | ||
pkglogsetup.GetSyslogURI(pkgconfigsetup.Datadog()), | ||
false, | ||
true, | ||
false, | ||
pkgconfigsetup.Datadog(), | ||
) | ||
checkError(err) | ||
|
||
cleanupFn := setupBytecode() | ||
defer cleanupFn() | ||
|
||
monitor, err := usm.NewMonitor(getConfiguration(), nil) | ||
checkError(err) | ||
|
||
err = monitor.Start() | ||
checkError(err) | ||
|
||
go func() { | ||
t := time.NewTicker(10 * time.Second) | ||
for range t.C { | ||
_ = monitor.GetProtocolStats() | ||
} | ||
}() | ||
|
||
defer monitor.Stop() | ||
done := make(chan os.Signal, 1) | ||
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM) | ||
<-done | ||
} | ||
|
||
func checkError(err error) { | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "%s", err) | ||
os.Exit(-1) | ||
} | ||
} | ||
|
||
func getConfiguration() *networkconfig.Config { | ||
// done for the purposes of initializing the configuration values | ||
_, err := config.New("", "") | ||
checkError(err) | ||
|
||
c := networkconfig.New() | ||
|
||
// run debug version of the eBPF program | ||
c.BPFDebug = true | ||
c.EnableUSMEventStream = false | ||
|
||
// don't buffer data in userspace | ||
// this is to ensure that we won't inadvertently trigger an OOM kill | ||
// by enabling the debugger inside a system-probe container. | ||
c.MaxHTTPStatsBuffered = 0 | ||
c.MaxKafkaStatsBuffered = 0 | ||
|
||
// make sure we use the CO-RE compilation artifact embedded | ||
// in this build (see `ebpf_bytecode.go`) | ||
c.EnableCORE = true | ||
c.EnableRuntimeCompiler = false | ||
|
||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add a comment here that you need this for the eBPF object files, specially with cross-arch builds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.