Module: RbBCC::EventTypeSupported

Included in:
PerfEventArray, RingBuf
Defined in:
lib/rbbcc/table.rb

Instance Method Summary collapse

Instance Method Details

#get_event_classObject



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
# File 'lib/rbbcc/table.rb', line 9

def get_event_class
  ct_mapping = {
    's8': 'char',
    'u8': 'unsined char',
    's8 *': 'char *',
    's16': 'short',
    'u16': 'unsigned short',
    's32': 'int',
    'u32': 'unsigned int',
    's64': 'long long',
    'u64': 'unsigned long long'
  }

  array_type = /(.+) \[([0-9]+)\]$/
  fields = []
  num_fields = Clib.bpf_perf_event_fields(self.bpf.module, @name)
  num_fields.times do |i|
    field = Clib.__extract_char(Clib.bpf_perf_event_field(self.bpf.module, @name, i))
    field_name, field_type = *field.split('#')
    if field_type =~ /enum .*/
      field_type = "int" #it is indeed enum...
    end
    if _field_type = ct_mapping[field_type.to_sym]
      field_type = _field_type
    end

    m = array_type.match(field_type)
    if m
      field_type = "#{m[1]}[#{m[2]}]"
      fields << [field_type, field_name].join(" ")
    else
      fields << [field_type, field_name].join(" ")
    end
  end
  klass = Fiddle::Importer.struct(fields)
  char_ps = fields.select {|f| f =~ /^char\[(\d+)\] ([_a-zA-Z0-9]+)/ }
  unless char_ps.empty?
    m = Module.new do
      char_ps.each do |char_p|
        md = /^char\[(\d+)\] ([_a-zA-Z0-9]+)/.match(char_p)
        define_method md[2] do
          # Split the char[] in the place where the first \0 appears
          raw = super()
          raw = raw[0...raw.index(0)] if raw.index(0)
          raw.pack("c*")
        end
      end
    end
    klass.prepend m
  end
  klass
end