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

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

grammar SysTraceFormat {
    rule TOP { <wholeformatsection>* }
    rule wholeformatsection { <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 {
    has Str $.name is rw;
    has Int $.id is rw;
    has Field @.fields is rw;
    # file descriptor passed to syscalls.
    has Bool $.has-fd 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) {
        push @!fields: $field;
        if ($field.name eq 'fd' && $field.type eq 'unsigned int') {
            $!has-fd = True;
        } elsif ($field.name eq 'ret' && $field.type eq 'long') {
            $.has-long-ret = True;
        }
    }

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

    method generate-probe returns Str {
        my \is-enter = $!name.split('_')[1] eq 'enter';
        my \is-exit = !is-enter;
        my \ctx-struct = is-enter ?? 'trace_event_raw_sys_enter'
                                  !! 'trace_event_raw_sys_exit';
        my \event-struct = is-enter ?? 'fd_event'
                                    !! ($!has-long-ret ?? 'ret_event' !! 'null_event');
        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->syscall_id = {$!name.uc};
            ev->pid = pid;
            ev->tid = tid;
            ev->time = bpf_ktime_get_ns() / 1000;
            {is-enter ?? 'ev->fd = (int)ctx->args[0];' 
                      !! ($!has-long-ret ?? 'ev->ret = ctx->ret;' !! '') }

            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 wholeformatsection($/) {
        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
    # Check whether one of them (enter or exit) has an fd.
    .grep(*.grep(*.has-fd).elems > 0) -> @_ { .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