-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
213 lines (191 loc) · 4.68 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
bpf "github.com/aquasecurity/libbpfgo"
"github.com/mozillazg/libbpfgo-tools/common"
flag "github.com/spf13/pflag"
)
const (
TASK_COMM_LEN = 16
NAME_MAX = 255
PERF_BUFFER_PAGES = 64
)
type Event struct {
Ts uint64
Pid uint32
Uid uint32
Ret int32
Flags int32
Comm [TASK_COMM_LEN]byte
Fname [NAME_MAX]byte
}
type Options struct {
bpfObjPath string
verbose bool
duration uint64
extendedFields bool
name string
pid uint32
tid uint32
timestamp bool
uid int32
printUid bool
failed bool
}
var opts = Options{
bpfObjPath: "opensnoop.bpf.o",
verbose: false,
duration: 0,
extendedFields: false,
name: "",
pid: 0,
tid: 0,
timestamp: false,
uid: -1,
printUid: false,
failed: false,
}
func init() {
flag.StringVar(&opts.bpfObjPath, "objpath", opts.bpfObjPath, "Path to the bpf object file")
flag.Uint64VarP(&opts.duration, "duration", "d", opts.duration, "Duration to trace")
flag.BoolVarP(&opts.extendedFields, "extended-fields", "e", opts.extendedFields, "Print extended fields")
flag.StringVarP(&opts.name, "name", "n", opts.name, "Trace process names containing this")
flag.Uint32VarP(&opts.pid, "pid", "p", opts.pid, "Process ID to trace")
flag.Uint32VarP(&opts.tid, "tid", "t", opts.tid, "Thread ID to trace")
flag.Int32VarP(&opts.uid, "uid", "u", opts.uid, "User ID to trace")
flag.BoolVarP(&opts.timestamp, "timestamp", "T", opts.timestamp, "Print timestamp")
flag.BoolVarP(&opts.printUid, "print-uid", "U", opts.printUid, "Print UID")
flag.BoolVarP(&opts.failed, "failed", "x", opts.failed, "Failed opens only")
// flag.BoolVarP(&opts.verbose, "verbose", "v", opts.verbose, "Verbose debug output")
}
func parseArgs() {
flag.Parse()
}
func initGlobalVars(bpfModule *bpf.Module) {
if opts.pid > 0 {
if err := bpfModule.InitGlobalVariable("targ_tgid", opts.pid); err != nil {
log.Fatalln(err)
}
}
if opts.tid > 0 {
if err := bpfModule.InitGlobalVariable("targ_pid", opts.tid); err != nil {
log.Fatalln(err)
}
}
if err := bpfModule.InitGlobalVariable("targ_uid", opts.uid); err != nil {
log.Fatalln(err)
}
if opts.failed {
if err := bpfModule.InitGlobalVariable("targ_failed", true); err != nil {
log.Fatalln(err)
}
}
}
func loadBPFObj(bpfModule *bpf.Module) {
if err := bpfModule.BPFLoadObject(); err != nil {
log.Fatalln(err)
}
}
func applyFilters(bpfModule *bpf.Module) {
}
func attachPrograms(bpfModule *bpf.Module) {
progIter := bpfModule.Iterator()
for {
prog := progIter.NextProgram()
if prog == nil {
break
}
if _, err := prog.AttachGeneric(); err != nil {
log.Fatalln(err)
}
}
}
func printEvent(data []byte) {
var e Event
err := binary.Read(bytes.NewReader(data), binary.LittleEndian, &e)
if err != nil {
log.Fatalf("read data failed: %s\n%v", err, data)
}
if opts.name != "" && !strings.Contains(common.GoString(e.Comm[:]), opts.name) {
return
}
ts := time.Now().Format("15:04:05")
fd := -1
errR := -e.Ret
if e.Ret >= 0 {
fd = int(e.Ret)
errR = 0
}
if opts.timestamp {
fmt.Printf("%-8s ", ts)
}
if opts.printUid {
fmt.Printf("%-6d ", e.Uid)
}
fmt.Printf("%-6d %-16s %3d %3d ", e.Pid, common.GoString(e.Comm[:]), fd, errR)
if opts.extendedFields {
fmt.Printf("%08o ", e.Flags)
}
fmt.Printf("%s\n", common.GoPath(e.Fname[:]))
}
func main() {
parseArgs()
bpfModule, err := bpf.NewModuleFromFile(opts.bpfObjPath)
if err != nil {
log.Fatalln(err)
}
defer bpfModule.Close()
initGlobalVars(bpfModule)
loadBPFObj(bpfModule)
applyFilters(bpfModule)
attachPrograms(bpfModule)
eventsChannel := make(chan []byte)
lostChannel := make(chan uint64)
pb, err := bpfModule.InitPerfBuf("events", eventsChannel, lostChannel, PERF_BUFFER_PAGES)
if err != nil {
log.Fatalln(err)
}
pb.Start()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer func() {
pb.Stop()
pb.Close()
stop()
}()
if opts.duration > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Second*time.Duration(opts.duration))
defer cancel()
}
if opts.timestamp {
fmt.Printf("%-8s ", "TIME")
}
if opts.printUid {
fmt.Printf("%-6s ", "UID")
}
fmt.Printf("%-6s %-16s %3s %3s ", "PID", "COMM", "FD", "ERR")
if opts.extendedFields {
fmt.Printf("%-8s ", "FLAGS")
}
fmt.Printf("%s\n", "PATH")
loop:
for {
select {
case data := <-eventsChannel:
printEvent(data)
case e := <-lostChannel:
log.Printf("lost %d events", e)
case <-ctx.Done():
break loop
}
}
}