summaryrefslogtreecommitdiff
path: root/internal/c/generated/tracepoints.raku
blob: de801a77ab7fb0a4273eb1c6663a2ca68c43a652 (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
#!/usr/bin/env raku

use v6.d;
#use Grammar::Debugger;

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;
}

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

    has Str $.name is rw;
    has Int $.id is rw;

    # file descriptor passed to syscalls.
    has Bool $.has-fd is rw = False;
    # Tracepoint has oldname/newname
    has Bool $.has-name is rw = False;
    # Tracepoint has pathname
    has Bool $.has-path is rw = False;
    # Syscall returns with a long value (e.g. bytes read/written)
    has Bool $.has-long-ret is rw = False;

    method push(Field \field) {
        # External fields start from this field name.
        $!is-external = True if field.name eq '__syscall_nr'; 

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

        if (field.name eq 'fd' && field.type eq 'unsigned int') {
            $!has-fd = True;
        } elsif (field.name eq 'newname' && field.type eq 'const char *') {
            $!has-name = True;
        } elsif (field.name eq 'pathname' && field.type eq 'const char *') {
            $!has-path = True;
        } elsif (field.name eq 'ret' && field.type eq 'long') {
            $.has-long-ret = True;
        }
    }

    method !field-number(Str \field-name) {
        @!external-fields.first(*.name eq field-name, :k) - 1;
    }

    method generate-constant returns Str {
        "#define {$!name.uc} {$!id}";
    }

    method generate-probe returns Str {
        my \is-enter = $!name.split('_')[1] eq 'enter';
        my \ctx-struct = is-enter ?? 'trace_event_raw_sys_enter'
                                  !! 'trace_event_raw_sys_exit';
        my \event-struct = do if $!has-fd { 'fd_event' }
                           elsif $!has-long-ret { 'ret_event' }
                           elsif $!has-name { 'name_event' }
                           elsif $!has-path { 'path_event' }
                           else { 'null_event' };
        my \extra-data = do if $!has-fd { 'ev->fd = (__s32)ctx->args[0];' }
                         elsif $!has-long-ret { 'ev->ret = ctx->ret;' }
                         elsif $!has-name {
                           my Int \oldname-index = self!field-number('oldname');
                           my Int \newname-index = self!field-number('newname');
                           qq:to/END/.trim-trailing;
                           __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-index}]);
                               bpf_probe_read_user_str(ev->newname, sizeof(ev->newname), (void*)ctx->args[{newname-index}]);
                           END
                         } elsif $!has-path {
                           my Int \pathname-index = self!field-number('pathname');
                           qq:to/END/.trim-trailing;
                           __builtin_memset(\&(ev->pathname), 0, sizeof(ev->pathname));
                               bpf_probe_read_user_str(ev->pathname, sizeof(ev->pathname), (void*)ctx->args[{pathname-index}]);
                           END
                         }
                         else { '' };
        qq:to/END/;
        SEC("tracepoint/syscalls/{$!name}")
        int handle_{$!name.lc}(struct {ctx-struct} *ctx) \{
            __u32 pid, tid;
            if (filter(&pid, &tid))
                return 0;

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

            ev->event_type = {(is-enter ?? 'ENTER_' !! 'EXIT_') ~ event-struct.uc};
            ev->trace_id = {$!name.uc};
            ev->pid = pid;
            ev->tid = tid;
            ev->time = bpf_ktime_get_ns() / 1000;
            {extra-data}

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

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

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

    method whole-format-section($/) {
        push @!formats: $!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 }
}

my Format @formats = gather for SysTraceFormat
    .parse($*IN.slurp,:actions(SysTraceFormatActions.new)).made
    # For each enter there is an exit tracepoint. E.g. sys_enter_open and sys_exit_open
    .classify(*.name.split('_').tail).values
    .grep({ $_.grep(*.has-fd) || $_.grep(*.has-name) || $_.grep(*.has-path) }) -> @_ { .take for @_ }

@formats .= sort(*.id);

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

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

{@formats.map(*.generate-probe).join("\n")}
END