-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
175 lines (146 loc) · 4.6 KB
/
Copy pathmain.go
File metadata and controls
175 lines (146 loc) · 4.6 KB
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
package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"flag"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/ringbuf"
"github.com/cilium/ebpf/rlimit"
"golang.org/x/sys/unix"
"io"
"log"
"log/slog"
"os"
"os/signal"
"time"
)
// https://github.com/iovisor/bcc/blob/master/docs/reference_guide.md
type JsonOutput struct {
uid uint32 `json:"uid"`
Pid uint32 `json:"pid"`
PPid uint32 `json:"ppid"`
Command string `json:"name"`
FileName string `json:"fileName"`
Args []string `json:"args"`
Start uint64 `json:"startTimeNs"`
Duration uint64 `json:"durationNs"`
ExitCode uint8 `json:"exitCode"`
}
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -type CommandEndEvent -type CommandParameterEvent proctracer proc-tracer.c
func main() {
logJson := flag.String("format", "json", "enables the json output of the command")
output := flag.String("output", "", "enables the json output of the command")
flag.Parse()
if *logJson != "json" {
log.Fatal("--format only supports [json]")
}
// Remove resource limits for kernels <5.11.
if err := rlimit.RemoveMemlock(); err != nil {
log.Fatal("Removing memlock:", err)
}
// Load the compiled eBPF ELF and load it into the kernel.
var objs proctracerObjects
if err := loadProctracerObjects(&objs, nil); err != nil {
log.Fatal("Loading eBPF objects:", err)
}
defer objs.Close()
l1, err := link.Tracepoint("syscalls", "sys_enter_execve", objs.HandleExecveEnter, &link.TracepointOptions{Cookie: 122})
if err != nil {
log.Fatal(err)
}
defer l1.Close()
l2, err := link.Tracepoint("sched", "sched_process_exit", objs.HandleExit, &link.TracepointOptions{Cookie: 123})
if err != nil {
log.Fatal(err)
}
defer l2.Close()
stop := make(chan os.Signal, 5)
reader, err := ringbuf.NewReader(objs.proctracerMaps.Events)
if err != nil {
slog.Error("failed to get reader", "error", err)
return
}
defer reader.Close()
go func() {
<-stop
_ = reader.Close()
}()
signal.Notify(stop, os.Interrupt)
// avoid confusion by having a single start print.
slog.Info("starting tracer")
var commandParamterEvent proctracerCommandParameterEvent
var commandEndEvent proctracerCommandEndEvent
// to get the event to arg
commandParameters := map[uint32][]proctracerCommandParameterEvent{}
writer := io.Discard
if *output != "" {
file, err := os.Create(*output)
if err != nil {
log.Fatal(err)
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
slog.Warn("failed to close the output file", "error", err, "file", *output)
}
}(file)
writer = file
}
for {
record, err := reader.Read()
if err != nil {
slog.Error("failed to read event", "error", err)
os.Exit(1)
}
switch record.RawSample[0] {
case 0:
err = binary.Read(bytes.NewBuffer(record.RawSample), binary.LittleEndian, &commandParamterEvent)
if err != nil {
slog.Error("failed to parsing event", "error", err)
os.Exit(1)
}
commandParameters[commandParamterEvent.Pid] = append(commandParameters[commandParamterEvent.Pid], commandParamterEvent)
case 1:
// the args already come in ordered.
err = binary.Read(bytes.NewBuffer(record.RawSample), binary.LittleEndian, &commandEndEvent)
if err != nil {
slog.Error("failed to parsing event", "error", err)
os.Exit(1)
}
commandArgEvents := commandParameters[commandEndEvent.Pid]
args := make([]string, len(commandArgEvents))
command := unix.ByteSliceToString(commandEndEvent.Comm[:])
logger := slog.With("command", command)
for i, c := range commandArgEvents {
args[i] = unix.ByteSliceToString(c.Arg[:])
}
logger.Info("command finished", "parameters", args, "exit-code", commandEndEvent.ExitCode, "start-time", commandEndEvent.StartTimeNs, "end-time", commandEndEvent.EndTimeNs, "duration", time.Duration(commandEndEvent.EndTimeNs-commandEndEvent.StartTimeNs).Seconds())
if *logJson == "json" {
data, err := json.Marshal(JsonOutput{
Pid: commandEndEvent.Pid,
PPid: commandEndEvent.Ppid,
Command: command,
Args: args,
Start: commandEndEvent.StartTimeNs,
Duration: uint64(time.Duration(commandEndEvent.EndTimeNs - commandEndEvent.StartTimeNs).Nanoseconds()),
ExitCode: commandEndEvent.ExitCode,
})
if err != nil {
slog.Error("failed to marshal json", "error", err)
os.Exit(1)
}
_, err = writer.Write(data)
if err != nil {
slog.Error("failed to write json", "error", err)
os.Exit(1)
}
_, err = writer.Write([]byte("\n"))
if err != nil {
slog.Error("failed to write json", "error", err)
os.Exit(1)
}
}
}
}
}