summaryrefslogtreecommitdiff
path: root/internal/c/generated/tracepoints.raku
blob: b94270338a4cde068b1b67a243497ece4926c9f5 (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
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
214
215
216
217
218
219
220
221
#!/usr/bin/env raku

use v6.d;

# Grammar to parse  /sys/kernel/tracing/events/syscalls/sys_{enter,exit}_*/format'
grammar SysTraceFormat {
    rule TOP { <whole-format-section>* }
    rule whole-format-section { <name> <id> <format> <print-fmt> }
    rule name { 'name:' <identifier> }
    rule id { 'ID:' <number> }
    rule format { 'format:' <field>* }

    rule field { 'field:' <field-elements> }
    rule field-elements { <field-declaration> <field-offset> <field-size> <field-signed> }
    rule field-declaration { <field-type>+ <identifier> ';' }

    token field-type { <-[ \t]> }
    token field-offset { 'offset:' <number> ';' }
    token field-size { 'size:' <number> ';' }
    token field-signed { 'signed:' <cbool> ';' }

    token identifier { <[a..zA..Z0..9_]>+ }
    token number { \d+ }
    token cbool { '0' | '1' }
    token print-fmt { 'print fmt' <-[\n]>+ "\n" }
}

class Field {
    has Str $.type is rw;
    has Str $.name is rw;
    has Int $.offset is rw;
    has Int $.size is rw;
    has Bool $.signed is rw;
}

role TracepointTemplate {
    method template(%vals --> Str) {
        my Bool \is-enter = %vals<name>.split('_')[1] eq 'enter';
        my Str \ctx-struct = is-enter ?? 'trace_event_raw_sys_enter' !! 'trace_event_raw_sys_exit';
        my Str @parts;

        @parts.push: qq:to/BPF_C_CODE/;
        SEC("tracepoint/syscalls/{%vals<name>}")
        int handle_{%vals<name>.lc}(struct {ctx-struct} *ctx) \{
            __u32 pid, tid;
            if (filter(&pid, &tid))
                return 0;

            struct {%vals<event-struct>} *ev = bpf_ringbuf_reserve(&event_map, sizeof(struct {%vals<event-struct>}), 0);
            if (!ev)
                return 0;

            ev->event_type = {(is-enter ?? 'ENTER_' !! 'EXIT_') ~ %vals<event-struct>.uc};
            ev->trace_id = {%vals<name>.uc};
            ev->pid = pid;
            ev->tid = tid;
            ev->time = bpf_ktime_get_boot_ns();
        BPF_C_CODE

        @parts.push: %vals<extra> if %vals<extra>:exists;

        @parts.push: qq:to/BPF_C_CODE/;

            bpf_ringbuf_submit(ev, 0);
            return 0;
        \}
        BPF_C_CODE

        [~] @parts;
    }
}

class FdTracepoint does TracepointTemplate {
    method generate-bpf-c-tracepoint(%vals --> Str) {
        my Str $extra = qq:to/BPF_C_CODE/;
            ev->fd = (__s32)ctx->args[0];
        BPF_C_CODE
        self.template: %vals.append( ( event-struct => 'fd_event', :$extra ).hash );
    }
}

class NameTracepoint does TracepointTemplate {
    method generate-bpf-c-tracepoint(%vals --> Str) {
        my Int \oldname-field-number = %vals<format>.field-number('oldname');
        my Int \newname-field-number = %vals<format>.field-number('newname');
        my Str $extra = qq:to/BPF_C_CODE/;
            __builtin_memset(\&(ev->oldname), 0, sizeof(ev->oldname) + sizeof(ev->newname));
            bpf_probe_read_user_str(ev->oldname, sizeof(ev->oldname), (void*)ctx->args[{oldname-field-number}]);
            bpf_probe_read_user_str(ev->newname, sizeof(ev->newname), (void*)ctx->args[{newname-field-number}]);
        BPF_C_CODE
        self.template: %vals.append( ( event-struct => 'name_event', :$extra ).hash );
    }
}

class OpenTracepoint does TracepointTemplate {
    method generate-bpf-c-tracepoint(%vals --> Str) {
        my Int \field-number = %vals<format>.field-number('filename');
        my Str $extra = qq:to/BPF_C_CODE/;
            __builtin_memset(\&(ev->filename), 0, sizeof(ev->filename) + sizeof(ev->comm));
            bpf_probe_read_user_str(ev->filename, sizeof(ev->filename), (void *)ctx->args[{field-number}]);
            bpf_get_current_comm(\&ev->comm, sizeof(ev->comm));
        BPF_C_CODE
        self.template: %vals.append( ( event-struct => 'open_event', :$extra ).hash );
    }
}

class PathnameTracepoint does TracepointTemplate {
    method generate-bpf-c-tracepoint(%vals --> Str) {
        my Int \field-number = %vals<format>.field-number('pathname');
        my Str $extra = qq:to/BPF_C_CODE/;
            __builtin_memset(\&(ev->pathname), 0, sizeof(ev->pathname));
            bpf_probe_read_user_str(ev->pathname, sizeof(ev->pathname), (void*)ctx->args[{field-number}]);
        BPF_C_CODE
        self.template: %vals.append( ( event-struct => 'path_event', :$extra ).hash );
    }
}

class RetTracepoint does TracepointTemplate {
    method generate-bpf-c-tracepoint(%vals --> Str) {
        my Str $extra = q:to/BPF_C_CODE/;
            ev->ret = ctx->ret; 
        BPF_C_CODE
        self.template: %vals.append( ( event-struct => 'ret_event', :$extra ).hash );
    }
}

class NullTracepoint does TracepointTemplate {
    method generate-bpf-c-tracepoint(%vals --> Str) {
        self.template: %vals.append( ( event-struct => 'null_event' ).hash );
    }
}

class Format {
    has Field @!internal-fields; # Fields not accessible from raw tracepoints.
    has Field @!external-fields; # Fields accessible from raw tracepoints.
    has Bool $!is-external = False; # Track internal/external field sections.
    has Str $.name is rw;
    has Int $.id is rw;
    has $.format-impl;

    method push(Field \field) {
        $!is-external = True if field.name eq '__syscall_nr'; 

        if $!is-external {
            push @!external-fields: field;
        } else {
            push @!internal-fields: field;
            return;
        }

        self.set-format-impl(field.name, field.type);
    }

    multi method set-format-impl('fd', 'unsigned int') { $!format-impl = FdTracepoint.new }
    multi method set-format-impl('newname', 'const char *') { $!format-impl = NameTracepoint.new }
    multi method set-format-impl('filename', 'const char *') { $!format-impl = OpenTracepoint.new }
    multi method set-format-impl('pathname', 'const char *') { $!format-impl = PathnameTracepoint.new }
    multi method set-format-impl('ret', 'long') { $!format-impl = RetTracepoint.new }
    multi method set-format-impl($, $) { }

    method generate-c-constant returns Str { "#define {$!name.uc} {$!id}" }
    method generate-bpf-c-tracepoint returns Str { $!format-impl.generate-bpf-c-tracepoint: (format => self, :$!name).hash }

    method field-number(Str \field-name) { @!external-fields.first(*.name eq field-name, :k) - 1 }
    method can-generate returns Bool { so $!format-impl.^can('generate-bpf-c-tracepoint') }
    method enter-reject returns Bool { $!format-impl !~~ any(FdTracepoint, NameTracepoint, OpenTracepoint, PathnameTracepoint) }
}

class SysTraceFormatActions {
    has Hash %!formats;
    has Format $!current-format = Format.new;
    has Field $!current-field = Field.new;

    method TOP($/) { make %!formats }

    method whole-format-section($/) {
        my ($, \enter-exit, \what) = $!current-format.name.split('_', 3);
        %!formats{what}{enter-exit} = $!current-format;
        $!current-format = Format.new;
    }

    method name($/) { $!current-format.name = ~$/<identifier> }
    method id($/) { $!current-format.id = +$/<number> }

    method field-declaration($/) {
        $!current-field.name = ~$/<identifier>;
        $!current-field.type = $/<field-type>.join('').trim-trailing;
        $!current-format.push($!current-field);
        $!current-field = Field.new;
    }

    method field-offset($/) { $!current-field.offset = +$/<number> }
    method field-size($/) { $!current-field.size = +$/<number> }
    method field-signed($/) { $!current-field.signed = +$/<cbool> == 0 ?? False !! True }
}

say qq:to/BPF_C_CODE/;
// Code generated - don't change manually!
BPF_C_CODE

my Format @formats = gather for
    SysTraceFormat.parse($*IN.slurp, actions => SysTraceFormatActions.new).made.values -> %syscall {

    if !all(%syscall.values.map(*.can-generate)) {
        say "// Ignoring {%syscall.values.map(*.name).sort} as possibly not file I/O related";
        next;
    } elsif %syscall<enter>.enter-reject {
        say "// Ignoring {%syscall.values.map(*.name).sort} as enter-rejected";
        next;   
    }
    .take for %syscall.values;
}

@formats .= sort({ $^b.id cmp $^a.id });

say qq:to/BPF_C_CODE/;

{@formats.map(*.generate-c-constant).join("\n")}

{@formats.map(*.generate-bpf-c-tracepoint).join("\n")}
BPF_C_CODE