summaryrefslogtreecommitdiff
path: root/internal/debugfs/debugfs.go
blob: 80ac436e3da7cf426bb6bdf60c1c2ad74b69be02 (plain)
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
package debugfs

import (
	"bufio"
	"fmt"
	"os"
	"path/filepath"
	"strings"
)

// Return tracepoints with 'unsigned int fd', which are I/O tracepoints on FDs
func TracepointsWithFd() ([]string, error) {
	return tracepointsWith("unsigned int fd")
}

func tracepointsWith(field string) ([]string, error) {
	var tracepoints []string

	matches, err := filepath.Glob("/sys/kernel/debug/tracing/events/syscalls/*/format")
	if err != nil {
		return tracepoints, err
	}
	if len(matches) == 0 {
		return tracepoints, fmt.Errorf("Unable to gather tracepoints with FDs")
	}

	for _, formatPath := range matches {
		has, err := hasField(formatPath, field)
		if err != nil {
			return tracepoints, err
		}
		if !has {
			continue
		}
		tracepoints = append(tracepoints, filepath.Base(filepath.Dir(formatPath)))
	}

	return tracepoints, nil
}

func hasField(formatPath, field string) (bool, error) {
	file, err := os.Open(formatPath)
	if err != nil {
		return false, err
	}
	defer file.Close()

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		line := scanner.Text()
		if strings.Contains(line, fmt.Sprintf("field:%s;", field)) {
			return true, nil
		}
	}

	return false, nil
}