forked from iovisor/gobpf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace_pipe.go
124 lines (113 loc) · 3.05 KB
/
trace_pipe.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
// Copyright 2017 Kinvolk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bpf
import (
"bufio"
"fmt"
"io"
"os"
"regexp"
"strings"
)
const tracePipeFile = "/sys/kernel/debug/tracing/trace_pipe"
// TracePipe to read from /sys/kernel/debug/tracing/trace_pipe
// Note that data can be read only once, i.e. if you have more than
// one tracer / channel, only one will receive an event:
// "Once data is read from this file, it is consumed, and will not be
// read again with a sequential read."
// https://www.kernel.org/doc/Documentation/trace/ftrace.txt
type TracePipe struct {
file *os.File
reader *bufio.Reader
stop chan struct{}
}
// TraceEvent contains the raw event as well as the contents of
// every field as string, as defined under "Output format" in
// https://www.kernel.org/doc/Documentation/trace/ftrace.txt
type TraceEvent struct {
Raw string
Task string
PID string
CPU string
Flags string
Timestamp string
Message string
}
func NewTracePipe() (*TracePipe, error) {
f, err := os.Open(tracePipeFile)
if err != nil {
return nil, err
}
return &TracePipe{
file: f,
reader: bufio.NewReader(f),
stop: make(chan struct{}),
}, nil
}
// A line from trace_pipe looks like (leading spaces included):
// ` chromium-15581 [000] d... 92783.722567: : Hello, World!`
var traceLineRegexp = regexp.MustCompile(`(.{16})-(\d+) +\[(\d{3})\] (.{4}) +(\d+\.\d+)\: \: (.*)`)
func parseTraceLine(raw string) (*TraceEvent, error) {
fields := traceLineRegexp.FindStringSubmatch(raw)
if len(fields) != 7 {
return nil, fmt.Errorf("received unexpected input %q", raw)
}
return &TraceEvent{
Raw: raw,
Task: strings.Trim(fields[1], " "),
PID: fields[2],
CPU: fields[3],
Flags: fields[4],
Timestamp: fields[5],
Message: fields[6],
}, nil
}
func (t *TracePipe) ReadLine() (*TraceEvent, error) {
line, err := t.reader.ReadString('\n')
if err != nil {
return nil, err
}
traceEvent, err := parseTraceLine(line)
if err != nil {
return nil, err
}
return traceEvent, nil
}
func (t *TracePipe) Channel() (<-chan *TraceEvent, <-chan error) {
channelEvents := make(chan *TraceEvent)
channelErrors := make(chan error)
go func() {
for {
select {
case <-t.stop:
return
default:
}
traceEvent, err := t.ReadLine()
if err != nil {
if err == io.EOF {
continue
}
channelErrors <- err
} else {
channelEvents <- traceEvent
}
}
}()
return channelEvents, channelErrors
}
func (t *TracePipe) Close() error {
close(t.stop)
return t.file.Close()
}