Skip to content

Commit

Permalink
Makefile: generate fileids.json from C file
Browse files Browse the repository at this point in the history
Add a simple utility to add a json file with the filename to fileid
mapping so that userspace can translate ids to names. This is meant to
be used by tetra, so add it as a dependency in tetra's build.

Signed-off-by: Kornilios Kourtis <[email protected]>
  • Loading branch information
kkourt committed Dec 11, 2024
1 parent 494cbe5 commit 038e3ec
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 3 deletions.
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ clean: cli-clean tarball-clean

##@ Build and install

pkg/errmetrics/fileids.json: bpf/lib/fileids.h
go run ./cmd/bpf-fileids bpf/lib/fileids.h pkg/errmetrics/fileids.json

.PHONY: fileids
fileids: pkg/errmetrics/fileids.json


.PHONY: tetragon
tetragon: ## Compile the Tetragon agent.
$(GO_BUILD) ./cmd/tetragon/
Expand All @@ -114,7 +121,7 @@ tetragon-operator: ## Compile the Tetragon operator.
$(GO_BUILD) -o $@ ./operator

.PHONY: tetra
tetra: ## Compile the Tetragon gRPC client.
tetra: fileids ## Compile the Tetragon gRPC client.
$(GO_BUILD) ./cmd/tetra/

.PHONY: tetragon-bpf
Expand Down
3 changes: 2 additions & 1 deletion bpf/lib/bpf_errmetrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "compiler.h"

// should match: pkg/errmetrics/map.go:MapKey
struct errmetrics_key {
__u16 error;
__u8 file_id;
Expand All @@ -29,7 +30,7 @@ struct {
__uint(type, BPF_MAP_TYPE_LRU_PERCPU_HASH);
__uint(max_entries, 1024);
__type(key, struct errmetrics_key);
__type(value, __u32);
__type(value, __u32); // counter, should match pkg/errmetrics/map.go:MapVal
} tg_errmetrics_map SEC(".maps");

FUNC_INLINE void
Expand Down
73 changes: 73 additions & 0 deletions cmd/bpf-fileids/fileids.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon

package main

import (
"bufio"
"encoding/json"
"fmt"
"os"
"regexp"
"strconv"
"strings"
)

// json entry
type Entry struct {
ID int `json:"id"`
Filename string `json:"filename"`
}

func initFileIDs(fname string) []Entry {
entryRe := regexp.MustCompile(`fileid__\("([^"]+)", *([0-9]+)\)`)
f, err := os.Open(fname)
if err != nil {
fmt.Fprintf(os.Stderr, "error opening '%s': %v", fname, err)
os.Exit(1)
}
defer f.Close()

ret := []Entry{}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "//") {
continue
}

m := entryRe.FindStringSubmatch(line)
if len(m) == 3 {
id, err := strconv.ParseInt(m[2], 0, 32)
if err != nil {
continue
}
ret = append(ret, Entry{int(id), m[1]})
}

}
// NB: no need to check scanner.Err()
return ret
}

func main() {
if len(os.Args) < 3 {
fmt.Fprintf(os.Stderr, "Usage: %s <fileids.h> <out.json>\n", os.Args[0])
os.Exit(1)
}

fname := os.Args[1]
fileIDs := initFileIDs(fname)
b, err := json.MarshalIndent(fileIDs, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "error marshalling file ids: %v", err)
os.Exit(1)
}

outFname := os.Args[2]
err = os.WriteFile(outFname, b, 0622)
if err != nil {
fmt.Fprintf(os.Stderr, "error writing file: %v", err)
os.Exit(1)
}
}
36 changes: 36 additions & 0 deletions pkg/errmetrics/errmetrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon

package errmetrics

import (
_ "embed"
"encoding/json"
"sync"
)

var (
MapName = "tg_errmetrics_map"
)

// json entry
type Entry struct {
ID int `json:"id"`
Filename string `json:"filename"`
}

//go:embed fileids.json
var fileIDsJSON []byte

var getFileIDs = sync.OnceValues(func() (map[int]string, error) {
var entries []Entry
if err := json.Unmarshal(fileIDsJSON, &entries); err != nil {
return nil, err
}

ret := map[int]string{}
for _, e := range entries {
ret[e.ID] = e.Filename
}
return ret, nil
})
6 changes: 6 additions & 0 deletions pkg/errmetrics/fileids.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"id": 112,
"filename": "cgroup/bpf_cgtracker.c"
}
]
74 changes: 74 additions & 0 deletions pkg/errmetrics/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon

package errmetrics

import (
"fmt"
"syscall"

"github.com/cilium/ebpf"
"golang.org/x/sys/unix"
)

type Map struct {
*ebpf.Map
}

func OpenMap(fname string) (Map, error) {
m, err := ebpf.LoadPinnedMap(fname, &ebpf.LoadPinOptions{
ReadOnly: true,
})

if err != nil {
return Map{}, err
}

return Map{m}, err
}

// NB: should match bpf/lib/bpf_errmetrics.h:errmetrics_key
type MapKey struct {
Err uint16
FileID uint8
Pad1 uint8
LineNR uint16
Pad2 uint16
}

type MapVal = uint32

type DumpEntry struct {
Location string
Error string
Count int
}

func (m Map) Dump() ([]DumpEntry, error) {
var key MapKey
var val []MapVal
var ret []DumpEntry

fileIDs, err := getFileIDs()
if err != nil {
return nil, err
}
iter := m.Iterate()
for iter.Next(&key, &val) {
fname, ok := fileIDs[int(key.FileID)]
if !ok {
fname = "<unknown>"
}
count := 0
for _, v := range val {
count += int(v)
}
ret = append(ret, DumpEntry{
Location: fmt.Sprintf("bpf/%s:%d", fname, key.LineNR),
Error: fmt.Sprintf("%s (%d)", unix.ErrnoName(syscall.Errno(key.Err)), key.Err),
Count: count,
})
}

return ret, nil
}
3 changes: 2 additions & 1 deletion pkg/sensors/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"
"testing"

"github.com/cilium/tetragon/pkg/errmetrics"
"github.com/cilium/tetragon/pkg/ksyms"
"github.com/cilium/tetragon/pkg/logger"
"github.com/cilium/tetragon/pkg/mbset"
Expand Down Expand Up @@ -69,7 +70,7 @@ var (

MatchBinariesSetMap = program.MapBuilder(mbset.MapName, Execve)

ErrMetricsMap = program.MapBuilder("tg_errmetrics_map", Execve)
ErrMetricsMap = program.MapBuilder(errmetrics.MapName, Execve)
)

func setupPrograms() {
Expand Down

0 comments on commit 038e3ec

Please sign in to comment.