Skip to content

Commit

Permalink
tetragon: Add support to generate uprobe policy from binary
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Olsa <[email protected]>
  • Loading branch information
olsajiri committed Dec 25, 2023
1 parent 4589222 commit 496f82b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
51 changes: 50 additions & 1 deletion cmd/tetra/tracingpolicy/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package generate

import (
"debug/elf"
"errors"
"log"
"os"

Expand Down Expand Up @@ -124,13 +126,60 @@ func New() *cobra.Command {
ftraceFlags := ftraceList.Flags()
ftraceFlags.StringVarP(&ftraceRegex, "regex", "r", "", "Use regex to limit the generated symbols")

var uprobesBinary string
uprobes := &cobra.Command{
Use: "uprobes",
Short: "all binary symbols",
Run: func(cmd *cobra.Command, _ []string) {
file, err := elf.Open(uprobesBinary)
if err != nil {
log.Fatalf("failed to open '%s': %s", uprobesBinary, err)
}

syms, err := file.Symbols()
if err != nil && !errors.Is(err, elf.ErrNoSymbols) {
log.Fatalf("failed to get symtab for open '%s': %s", uprobesBinary, err)
}

dynsyms, err := file.DynamicSymbols()
if err != nil && !errors.Is(err, elf.ErrNoSymbols) {
log.Fatalf("failed to get dynsym for open '%s': %s", uprobesBinary, err)
}

syms = append(syms, dynsyms...)

tp := generate.NewTracingPolicy("uprobes")
uprobe := generate.AddUprobe(tp)

for _, sym := range syms {
if elf.ST_TYPE(sym.Info) != elf.STT_FUNC {
continue
}
if sym.Value == 0 {
continue
}
uprobe.Symbols = append(uprobe.Symbols, sym.Name)
}

uprobe.Path = uprobesBinary
b, err := yaml.Marshal(tp)
if err != nil {
log.Fatal(err)
}
os.Stdout.Write(b)
},
}

uprobesFlags := uprobes.Flags()
uprobesFlags.StringVarP(&uprobesBinary, "binary", "b", "", "Binary path")

cmd := &cobra.Command{
Use: "generate",
Short: "generate tracing policies",
}
pflags := cmd.PersistentFlags()
pflags.StringVarP(&matchBinary, "match-binary", "m", "", "Add binary to matchBinaries selector")

cmd.AddCommand(empty, allSyscalls, allSyscallsList, ftraceList)
cmd.AddCommand(empty, allSyscalls, allSyscallsList, ftraceList, uprobes)
return cmd
}
9 changes: 8 additions & 1 deletion pkg/tracingpolicy/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
func NewTracingPolicy(name string) *v1alpha1.TracingPolicy {
ret := v1alpha1.TracingPolicy{
TypeMeta: metav1.TypeMeta{
Kind: "TracingPolicy",
Kind: "TracingPolicy",
APIVersion: "cilium.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand All @@ -31,3 +32,9 @@ func AddKprobe(tp *v1alpha1.TracingPolicy) *v1alpha1.KProbeSpec {
tp.Spec.KProbes = append(tp.Spec.KProbes, v1alpha1.KProbeSpec{})
return &tp.Spec.KProbes[idx]
}

func AddUprobe(tp *v1alpha1.TracingPolicy) *v1alpha1.UProbeSpec {
idx := len(tp.Spec.UProbes)
tp.Spec.UProbes = append(tp.Spec.UProbes, v1alpha1.UProbeSpec{})
return &tp.Spec.UProbes[idx]
}

0 comments on commit 496f82b

Please sign in to comment.