-
Notifications
You must be signed in to change notification settings - Fork 320
/
stacktracetree.go
76 lines (63 loc) · 1.83 KB
/
stacktracetree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Hubble
package stacktracetree
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/cilium/tetragon/api/v1/tetragon"
"github.com/cilium/tetragon/cmd/tetra/common"
"github.com/spf13/cobra"
)
func New() *cobra.Command {
sttCmd := &cobra.Command{
Use: "stacktrace-tree",
Short: "Manage stacktrace trees",
}
sttPrintCmd := &cobra.Command{
Use: "print <tree-name>",
Short: "Print stacktrace tree",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
stt := args[0]
common.CliRun(func(ctx context.Context, cli tetragon.FineGuidanceSensorsClient) {
sttPrint(ctx, cli, stt)
})
},
}
sttCmd.AddCommand(sttPrintCmd)
return sttCmd
}
func sttPrint(ctx context.Context, client tetragon.FineGuidanceSensorsClient, stt string) {
res, err := client.GetStackTraceTree(ctx, &tetragon.GetStackTraceTreeRequest{Name: stt})
if err != nil {
fmt.Printf("error printing stt %s: %s\n", stt, err)
return
}
// NB: leave this here, in case we want to add a json option at some point
if false {
res_json, err := json.Marshal(res)
if err != nil {
fmt.Printf("error marshaling stt %s: %s\n", stt, err)
return
}
fmt.Printf("%s\n", string(res_json))
}
sttPrintNodeTree(res.Root, 0)
}
func sttPrintNodeTree(node *tetragon.StackTraceNode, level int) {
indent_space := " "
indent := strings.Repeat(indent_space, level)
fmt.Printf("%s0x%x (%s) count:%d\n", indent, node.Address.Address, node.Address.Symbol, node.Count)
nchildren := len(node.Children)
for _, child := range node.Children {
sttPrintNodeTree(child, level+1)
}
// This is a leaf, so we also print label counters
if nchildren == 0 {
for _, label := range node.Labels {
fmt.Printf("%s%s%s count:%d\n", indent, indent_space, label.Key, label.Count)
}
}
}