/
cmd_graph.go
206 lines (170 loc) · 4.98 KB
/
cmd_graph.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package cmd
import (
"fmt"
"io"
"os"
"os/exec"
"strings"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
"github.com/dylandreimerink/edb/analyse"
"github.com/emicklei/dot"
"github.com/pkg/browser"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"
)
func graphCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "graph {ELF} {program name}",
Short: "Generate a control-flow graph for an eBPF program",
Long: "This command reads the provided ELF file and creates a control-flow graph for the given program. " +
"The program is broken up into 'blocks' of code by BPF-to-BPF function calls and branching instructions. " +
"Red arrows indicate the non-branching path, green arrows indicate the branching path, yellow arrows " +
"indicate bpf-to-bpf function calls(which will return and then follow the non-branching path).\n\n" +
"If no flags are specified the command will attempt to render the graph as SVG and open it in the browser.",
RunE: runGraph,
Args: cobra.ExactArgs(2),
}
f := cmd.Flags()
f.StringVarP(&graphOutput, "output", "o", "", "output to given file path or - for stdout, instread of opening "+
"in browser")
f.StringVarP(&graphOutputFormat, "format", "f", "svg", "The output format: dot, svg, pdf or png")
return cmd
}
var (
graphOutput string
graphOutputFormat string
)
func runGraph(cmd *cobra.Command, args []string) error {
// Load collection from ELF
spec, err := ebpf.LoadCollectionSpec(args[0])
if err != nil {
return fmt.Errorf("load collection: %w", err)
}
prog := spec.Programs[args[1]]
if prog == nil {
return fmt.Errorf(
"no program with name '%s', pick from: %s",
args[1],
strings.Join(maps.Keys(spec.Programs), ", "),
)
}
graph := ProgramToGraph(prog)
switch graphOutputFormat {
case "dot":
if graphOutput == "-" {
fmt.Println(graph.String())
return nil
}
var f *os.File
if graphOutput == "" {
f, err = os.CreateTemp(os.TempDir(), "edb-graph-*.dot.txt")
if err != nil {
return fmt.Errorf("create tmp: %w", err)
}
} else {
f, err = os.Create(graphOutput)
if err != nil {
return fmt.Errorf("create file: %w", err)
}
}
_, err = io.Copy(f, strings.NewReader(graph.String()))
if err != nil {
return fmt.Errorf("copy: %w", err)
}
if graphOutput == "" {
browser.OpenFile(f.Name())
}
case "png", "svg", "pdf":
dotF, err := os.CreateTemp(os.TempDir(), "edb-graph-*.dot")
if err != nil {
return fmt.Errorf("create tmp: %w", err)
}
_, err = io.Copy(dotF, strings.NewReader(graph.String()))
if err != nil {
return fmt.Errorf("copy: %w", err)
}
var (
cmd *exec.Cmd
imgF *os.File
)
switch graphOutput {
case "-":
cmd = exec.Command("dot", fmt.Sprintf("-T%s", graphOutputFormat), dotF.Name())
cmd.Stdout = os.Stdout
case "":
imgF, err = os.CreateTemp(os.TempDir(), fmt.Sprintf("edb-graph-*.%s", graphOutputFormat))
if err != nil {
return fmt.Errorf("create tmp: %w", err)
}
cmd = exec.Command(
"dot",
fmt.Sprintf("-T%s", graphOutputFormat),
fmt.Sprintf("-o%s", imgF.Name()),
dotF.Name(),
)
default:
cmd = exec.Command(
"dot",
fmt.Sprintf("-T%s", graphOutputFormat),
fmt.Sprintf("-o%s", graphOutput),
dotF.Name(),
)
}
err = cmd.Run()
if err != nil {
return fmt.Errorf("dot: %w", err)
}
if graphOutput == "" {
browser.OpenFile(imgF.Name())
}
}
return nil
}
func ProgramToGraph(prog *ebpf.ProgramSpec) *dot.Graph {
functions := prog.Instructions.FunctionReferences()
blocks := analyse.ProgramBlocks(prog.Instructions)
graph := dot.NewGraph(dot.Directed)
graph.Attr("splines", "ortho")
graph.Attr("nodesep", "0.5")
graph.Attr("ranksep", "0.3")
funcSubGraph := graph.Subgraph(prog.Instructions[0].Symbol(), dot.ClusterOption{})
funcSubGraph.Attr("color", "blue")
var instCnt int
blockNodes := make(map[*analyse.ProgBlock]dot.Node)
for _, block := range blocks {
if functions[block.Block.Name()] {
funcSubGraph = graph.Subgraph(block.Block.Name(), dot.ClusterOption{})
funcSubGraph.Attr("color", "blue")
}
var label strings.Builder
label.WriteString("\"")
for _, inst := range block.Block {
// Remove references from jump ops since they are not needed when we have edges
if inst.OpCode.JumpOp() != asm.InvalidJumpOp {
inst = inst.WithReference("")
}
label.WriteString(fmt.Sprintf("%d %v\\l", instCnt, inst))
instCnt++
}
label.WriteString("\"")
blockNode := funcSubGraph.Node(fmt.Sprintf("Block %d", block.Index))
blockNode.Attr("label", dot.Literal(label.String()))
blockNode.Attr("shape", "box")
blockNodes[block] = blockNode
}
for _, block := range blocks {
if block.Branch != nil {
edge := graph.Edge(blockNodes[block], blockNodes[block.Branch]).
Attr("color", "darkgreen")
if block.Block[len(block.Block)-1].OpCode.JumpOp() == asm.Call {
edge.Attr("color", "orange")
}
}
if block.NoBranch != nil {
graph.Edge(blockNodes[block], blockNodes[block.NoBranch]).
Attr("color", "red")
}
}
return graph
}