summaryrefslogtreecommitdiff
path: root/internal/flags/flags.go
blob: 4d375fcaa2fee883aebf4e442822b1a4282f103b (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
58
59
60
61
62
63
64
65
66
67
68
69
70
package flags

import (
	"flag"
	"fmt"

	bpf "github.com/aquasecurity/libbpfgo"
)

// TODO: Filter by syscall (tracepoint names)
type Flags struct {
	PidFilter        int
	TidFilter        int
	EventMapSize     int
	CommFilter       string
	PathFilter       string
	PprofEnable      bool
	FlamegraphEnable bool
	Duration         int
}

func New() (flags Flags) {
	flag.IntVar(&flags.PidFilter, "pid", -1, "Filter for processes ID")
	flag.IntVar(&flags.TidFilter, "tid", -1, "Filter for thread ID")
	flag.IntVar(&flags.EventMapSize, "mapSize", 4096*16, "BPF FD event ring buffer map size")
	flag.IntVar(&flags.Duration, "duration", 60, "Probe duration in seconds")
	flag.StringVar(&flags.CommFilter, "comm", "", "Command to filter for")
	flag.StringVar(&flags.PathFilter, "path", "", "Path to filter for")
	flag.BoolVar(&flags.PprofEnable, "pprof", false, "Enable profiling")
	flag.BoolVar(&flags.FlamegraphEnable, "flamegraph", false, "Enable flamegraph builder")
	flag.Parse()

	return flags
}

func (flags Flags) SetBPF(bpfModule *bpf.Module) error {
	fmt.Println("Setting PID_FILTER to", flags.PidFilter)
	if err := bpfModule.InitGlobalVariable("PID_FILTER", uint32(flags.PidFilter)); err != nil {
		return fmt.Errorf("unable to set up PID_FILTER global variable: %w", err)
	}
	fmt.Println("Setting TID_FILTER to", flags.TidFilter)
	if err := bpfModule.InitGlobalVariable("TID_FILTER", uint32(flags.TidFilter)); err != nil {
		return fmt.Errorf("unable to set up TID_FILTER global variable: %w", err)
	}
	return nil
}

func (flags Flags) ResizeBPFMaps(bpfModule *bpf.Module) error {
	if err := resizeBPFMap(bpfModule, "event_map", uint32(flags.EventMapSize)); err != nil {
		return fmt.Errorf("event_map: %w", err)
	}
	return nil
}

func resizeBPFMap(module *bpf.Module, name string, size uint32) error {
	m, err := module.GetMap(name)
	if err != nil {
		return err
	}

	if err = m.SetMaxEntries(size); err != nil {
		return err
	}

	if actual := m.MaxEntries(); actual != size {
		return fmt.Errorf("map resize to %d failed, expected %v, actual %v", size, size, actual)
	}

	return nil
}