-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
184 lines (160 loc) · 4.19 KB
/
main.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
package main
import (
"embed"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/stevenjohnstone/go-bpf-gen/abi"
"github.com/stevenjohnstone/go-bpf-gen/ret"
)
//go:embed templates
var templates embed.FS
type Target struct {
ExePath string
Arguments func(string) []string
RegsABI bool
offsets map[string][]int
}
func (t Target) SymbolReturns(symbol string) ([]int, error) {
v, ok := t.offsets[symbol]
if ok {
return v, nil
}
f, err := os.Open(t.ExePath)
if err != nil {
return nil, err
}
defer f.Close()
offsets, err := ret.FindOffsets(f, symbol)
if err != nil {
return nil, err
}
t.offsets[symbol] = offsets
return offsets, nil
}
func (t Target) SymbolReturnsNoFail(symbol string) []int {
v, err := t.SymbolReturns(symbol)
if err != nil {
return []int{1}
}
return v
}
func regsabi(exe string) (bool, error) {
f, err := os.Open(exe)
if err != nil {
return false, err
}
defer f.Close()
return abi.Regs(f)
}
var regs = [...]string{"ax", "bx", "cx", "di", "si", "r8", "r9", "r10", "r11"}
// Arg maps argument indices to bpftrace built-ins taking into account which ABI
// is in use
func (t Target) Arg(i int) string {
if t.RegsABI {
if i >= len(regs) {
return fmt.Sprintf(`*(reg("sp") + %d)`, 8*(i-8))
}
// rax, rbx, rcx, rdi, rsi, r8, r9, r10, r11 should do
// Refer: https://go.googlesource.com/go/+/refs/heads/dev.regabi/src/cmd/compile/internal-abi.md#amd64-architecture
if i < 0 {
panic("argument out of bounds. roll your own")
}
return fmt.Sprintf("reg(\"%s\")", regs[i])
}
// FIX: WARNING: sarg* is deprecated and will be removed in the future. Use *(reg("sp") + <stack_offset>) instead.
return fmt.Sprintf(`*(reg("sp") + %d)`, 8*(i+1))
}
// RetArg maps return argument indices to bpftrace built-ins taking into account which ABI
// is in use
func (t Target) RetArg(i, startOffset int) string {
if t.RegsABI {
// ret args is on stack
if i >= len(regs) {
// calc offset of args
var argsOffset int
if startOffset >= len(regs) {
argsOffset = 8 * (startOffset - 8)
}
return fmt.Sprintf(`*(reg("sp") + %d)`, 8*(i-8)+argsOffset)
}
// rax, rbx, rcx, rdi, rsi, r8, r9, r10, r11 should do
if i < 0 {
panic("return argument out of bounds. roll your own")
}
return fmt.Sprintf("reg(\"%s\")", regs[i])
}
return fmt.Sprintf(`*(reg("sp") + %d)`, 8*(i+1+startOffset))
}
func NewTarget(exe string, arguments func(string) []string) (*Target, error) {
exe, err := filepath.Abs(exe)
if err != nil {
return nil, err
}
f, err := os.Open(exe)
if err != nil {
return nil, err
}
defer f.Close()
regsAbi, err := regsabi(exe)
if err != nil {
log.Printf("couldn't get regs abi (%s). falling back to stack calling convention", err)
}
return &Target{
ExePath: exe,
Arguments: arguments,
RegsABI: regsAbi,
offsets: map[string][]int{},
}, nil
}
func parseArguments(args []string) (scriptFile, targetExe string, kv map[string][]string, err error) {
kv = map[string][]string{}
if len(args) < 3 {
err = fmt.Errorf("usage %s <template file> <target file>", args[0])
return
}
scriptFile, targetExe = args[1], args[2]
args = args[3:]
for _, arg := range args {
s := strings.Split(arg, "=")
if len(s) != 2 {
err = fmt.Errorf("malformed argument %s, must be of form key=value", arg)
return
}
k, v := s[0], s[1]
kv[k] = append(kv[k], v)
}
return
}
func main() {
scriptFile, targetExe, kv, err := parseArguments(os.Args)
if err != nil {
log.Fatal(err)
}
scriptTemplate, err := os.ReadFile(scriptFile)
if err != nil {
// try embedded files
f, err1 := templates.Open(scriptFile)
if err1 != nil {
log.Fatalf("failed to open %s on filesystem: (%s), tried embedded files got %s", scriptFile, err, err1)
}
scriptTemplate, err = io.ReadAll(f)
if err != nil {
panic(err)
}
}
target, err := NewTarget(targetExe, func(key string) []string {
return kv[key]
})
if err != nil {
log.Fatalf("failed to process target: %s", err)
}
tmpl := template.Must(template.New("bpf").Funcs(template.FuncMap{"panic": func(s string) string { panic(s) }}).Parse(string(scriptTemplate)))
if err := tmpl.Execute(os.Stdout, target); err != nil {
log.Fatalf("failed to process template: %s", err)
}
}