-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Makefile: generate fileids.json from C file
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
Showing
7 changed files
with
201 additions
and
3 deletions.
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
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
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) | ||
} | ||
} |
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,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 | ||
}) |
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,6 @@ | ||
[ | ||
{ | ||
"id": 112, | ||
"filename": "cgroup/bpf_cgtracker.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
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 | ||
} |
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