-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
178 lines (147 loc) · 3.55 KB
/
magefile.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
//go:build mage
// +build mage
package main
import (
"io/ioutil"
"os"
"runtime"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
type Builder struct{}
func (self *Builder) Env() map[string]string {
env := make(map[string]string)
env["GOPACKAGE"] = "ebpf"
return env
}
func (self *Builder) cwd(dir string) (func(), error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
err = os.Chdir(dir)
if err != nil {
return nil, err
}
return func() {
os.Chdir(cwd)
}, nil
}
func (self *Builder) Bin() error {
return sh.RunWith(self.Env(), mg.GoCmd(), "build",
"-o", "./test",
"./userspace/cmd/",
)
}
func (self *Builder) Race() error {
return sh.RunWith(self.Env(), mg.GoCmd(), "build",
"-o", "./test", "-race",
"./userspace/cmd/",
)
}
func (self *Builder) Generate() error {
err := self.generate()
if err != nil {
return err
}
return self.fixAssets()
}
func (self *Builder) generate() error {
closer, err := self.cwd("userspace/ebpf")
if err != nil {
return err
}
defer closer()
if runtime.GOARCH == "amd64" {
return sh.RunWith(self.Env(), mg.GoCmd(), "run",
"github.com/cilium/ebpf/cmd/bpf2go",
"-type", "config_entry_t",
"-type", "event_context_t",
"-type", "event_config_t",
"-no-global-types",
"-target", "bpfel",
"ebpf", "../../c/tracee.bpf.c",
"--", "-I../../c/", "-D__TARGET_ARCH_x86", "-DDEBUG_K",
)
} else if runtime.GOARCH == "arm64" {
return sh.RunWith(self.Env(), mg.GoCmd(), "run",
"github.com/cilium/ebpf/cmd/bpf2go",
"-type", "config_entry_t",
"-type", "event_context_t",
"-type", "event_config_t",
"-no-global-types",
"-target", "bpfel",
"ebpf", "../../c/tracee.bpf.c",
"--", "-I../../c/", "-D__TARGET_ARCH_arm64", "-DDEBUG_K",
)
} else {
panic("Architecture not supported!")
}
}
func (self *Builder) fixAssets() error {
// We only use little endian for the moment
for _, f := range []string{
"userspace/ebpf/ebpf_bpfel.go",
} {
replace_string_in_file(f, `//go:embed `, "//")
replace_string_in_file(f, `bytes.NewReader(_EbpfBytes)`,
`bytes.NewReader(getEbpfBytes())`)
}
if runtime.GOARCH == "amd64" {
err := fileb0x("userspace/ebpf/b0x_bpfel_amd64.yaml")
if err != nil {
return err
}
err = replace_string_in_file("userspace/ebpf/ab0x_amd64.go", "func init()", "func Init()")
if err != nil {
return err
}
} else if runtime.GOARCH == "arm64" {
err := fileb0x("userspace/ebpf/b0x_bpfel_arm64.yaml")
if err != nil {
return err
}
err = replace_string_in_file("userspace/ebpf/ab0x_arm64.go", "func init()", "func Init()")
if err != nil {
return err
}
}
return nil
}
// Build ebpf files.
//
// This needs to only be run if the ebpf C code changes! We normally
// check the compiled EBPF module into the tree, so you do not need to
// rebuild it.
func Generate() error {
builder := Builder{}
return builder.Generate()
}
func Bin() error {
builder := Builder{}
return builder.Bin()
}
func Race() error {
builder := Builder{}
return builder.Race()
}
func replace_string_in_file(filename string, old string, new string) error {
read, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
newContents := strings.Replace(string(read), old, new, -1)
return ioutil.WriteFile(filename, []byte(newContents), 0644)
}
func fileb0x(asset string) error {
err := sh.Run("fileb0x", asset)
if err != nil {
err = sh.Run(mg.GoCmd(), "install", "github.com/Velocidex/fileb0x@d54f4040016051dd9657ce04d0ae6f31eab99bc6")
if err != nil {
return err
}
err = sh.Run("fileb0x", asset)
}
return err
}