Class: RbBCC::BCC

Inherits:
Object
  • Object
show all
Defined in:
lib/rbbcc/bcc.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text: "", src_file: nil, hdr_file: nil, debug: 0, cflags: [], usdt_contexts: [], allow_rlimit: 0, dev_name: nil) ⇒ BCC

Returns a new instance of BCC.



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/rbbcc/bcc.rb', line 240

def initialize(text: "", src_file: nil, hdr_file: nil, debug: 0, cflags: [], usdt_contexts: [], allow_rlimit: 0, dev_name: nil)
  @kprobe_fds = {}
  @uprobe_fds = {}
  @tracepoint_fds = {}
  @raw_tracepoint_fds = {}
  @lsm_fds = {}

  if src_file
    src_file = BCC._find_file(src_file)
    hdr_file = BCC._find_file(hdr_file)
  end

  if src_file
    text = File.read(src_file)
  end

  @usdt_contexts = usdt_contexts
  if code = gen_args_from_usdt
    text = code + text
  end


  cflags_safe = if cflags.empty? or !cflags[-1].nil?
                  cflags + [nil]
                else
                  cflags
                end

  @module = Clib.do_bpf_module_create_c_from_string(
    text,
    debug,
    cflags_safe.pack("p*"),
    cflags_safe.size,
    allow_rlimit,
    dev_name
  )

  @funcs = {}
  @tables = {}
  @perf_buffers = {}
  @_ringbuf_manager = nil

  unless @module
    raise "BPF module not created"
  end

  trace_autoload!

  @usdt_contexts.each do |usdt|
    usdt.enumerate_active_probes.each do |probe|
      attach_uprobe(name: probe.binpath, fn_name: probe.fn_name, addr: probe.addr, pid: probe.pid)
    end
  end

  at_exit { self.cleanup }
end

Instance Attribute Details

#moduleObject (readonly)

Returns the value of attribute module.



296
297
298
# File 'lib/rbbcc/bcc.rb', line 296

def module
  @module
end

#perf_buffersObject (readonly)

Returns the value of attribute perf_buffers.



296
297
298
# File 'lib/rbbcc/bcc.rb', line 296

def perf_buffers
  @perf_buffers
end

#tablesObject (readonly)

Returns the value of attribute tables.



573
574
575
# File 'lib/rbbcc/bcc.rb', line 573

def tables
  @tables
end

Class Method Details

._find_file(filename) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rbbcc/bcc.rb', line 18

def _find_file(filename)
  if filename
    unless File.exist?(filename)
      t = File.expand_path "../#{filename}", $0
      if File.exist?(t)
        filename = t
      else
        raise "Could not find file #{filename}"
      end
    end
  end
  return filename
end

.attach_raw_socket(fn, dev) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/rbbcc/bcc.rb', line 205

def attach_raw_socket(fn, dev)
  unless fn.is_a?(Hash)
    raise "arg 1 must be of BPF.Function Hash"
  end
  sock = Clib.bpf_open_raw_sock(dev)
  if sock < 0
    raise SystemCallError.new("Failed to open raw device %s" % dev, Fiddle.last_error)
  end

  res = Clib.bpf_attach_socket(sock, fn[:fd])
  if res < 0
    raise SystemCallError.new("Failed to attach BPF to device %s" % dev, Fiddle.last_error)
  end
  fn[:sock] = sock
  fn
end

.check_path_symbol(module_, symname, addr, pid) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rbbcc/bcc.rb', line 110

def check_path_symbol(module_, symname, addr, pid)
  sym = Clib::BCCSymbol.malloc
  c_pid = pid == -1 ? 0 : pid
  if Clib.bcc_resolve_symname(module_, symname, (addr || 0x0), c_pid, nil, sym) < 0
    raise("could not determine address of symbol %s" % symname)
  end
  module_path = Clib.__extract_char sym.module
  # XXX: need to free char* in ruby ffi?
  Clib.bcc_procutils_free(sym.module)
  return module_path, sym.offset
end

.decode_table_type(desc) ⇒ Object



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

def decode_table_type(desc)
  return desc if desc.is_a?(String)
  anon = []
  fields = []
  # e.g. ["bpf_stacktrace", [["ip", "unsigned long long", [127]]], "struct_packed"]
  name, typedefs, data_type = desc
  typedefs.each do |field|
    case field.size
    when 2
      fields << "#{decode_table_type(field[1])} #{field[0]}"
    when 3
      ftype = field.last
      if ftype.is_a?(Array)
        fields << "#{decode_table_type(field[1])}[#{ftype[0]}] #{field[0]}"
      elsif ftype.is_a?(Integer)
        warn("Warning: Ruby fiddle does not support bitfield member, ignoring")
        warn("Adding member `#{field[1]} #{field[0]}:#{ftype}'")
        fields << "#{decode_table_type(field[1])} #{field[0]}"
      elsif %w(union struct struct_packed).in?(ftype)
        name = field[0]
        if name.empty?
          name = "__anon%d" % anon.size
          anon << name
        end
        # FIXME: nested struct
        fields << "#{decode_table_type(field)} #{name}"
      else
        raise("Failed to decode type #{field.inspect}")
      end
    else
      raise("Failed to decode type #{field.inspect}")
    end
  end
  c = nil
  if data_type == "union"
    c = Fiddle::Importer.union(fields)
  else
    c = Fiddle::Importer.struct(fields)
  end

  fields.each do |field|
    md = /^char\[(\d+)\] ([_a-zA-Z0-9]+)/.match(field)
    if md
      c.alias_method "__super_#{md[2]}", md[2]
      c.define_method md[2] do
        # Split the char[] in the place where the first \0 appears
        raw = __send__("__super_#{md[2]}")
        raw = raw[0...raw.index(0)] if raw.index(0)
        raw.pack("c*")
      end
    end
  end

  c.define_singleton_method :original_desc do
    desc
  end
  c.define_singleton_method :fields do
    fields
  end
  orig_name = c.inspect
  c.define_singleton_method :inspect do
    orig_name.sub(/(?=>$)/, " original_desc=#{desc.inspect}") rescue super
  end
  c
end

.get_kprobe_functions(event_re) ⇒ Object



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

def get_kprobe_functions(event_re)
  blacklist = []
  fns = []
  File.open(File.expand_path("../kprobes/blacklist", TRACEFS), "rb") do |blacklist_f|
    blacklist = blacklist_f.each_line.map { |line|
      line.rstrip.split[1]
    }.uniq
  end
  in_init_section = 0
  in_irq_section = 0
  File.open("/proc/kallsyms", "rb") do |avail_file|
    avail_file.each_line do |line|
      t, fn = line.rstrip.split[1,2]
      # Skip all functions defined between __init_begin and
      # __init_end
      if in_init_section == 0
        if fn == '__init_begin'
          in_init_section = 1
          next
        elsif in_init_section == 1
          if fn == '__init_end'
            in_init_section = 2
            next
          end
        end
        # Skip all functions defined between __irqentry_text_start and
        # __irqentry_text_end
        if in_irq_section == 0
          if fn == '__irqentry_text_start'
            in_irq_section = 1
            next
          elsif in_irq_section == 1
            if fn == '__irqentry_text_end'
              in_irq_section = 2
              next
            end
          end
        end
        # All functions defined as NOKPROBE_SYMBOL() start with the
        # prefix _kbl_addr_*, blacklisting them by looking at the name
        # allows to catch also those symbols that are defined in kernel
        # modules.
        if fn.start_with?('_kbl_addr_')
          next
        # Explicitly blacklist perf-related functions, they are all
        # non-attachable.
        elsif fn.start_with?('__perf') || fn.start_with?('perf_')
          next
        # Exclude all gcc 8's extra .cold functions
        elsif fn =~ /^.*\.cold\.\d+$/
          next
        end
        if %w(t w).include?(t.downcase) \
           && /#{event_re}/ =~ fn \
           && !blacklist.include?(fn)
          fns << fn
        end
      end
    end
  end
  fns = fns.uniq
  return fns.empty? ? nil : fns
end

.ksym(addr, show_module: false, show_offset: false) ⇒ Object



32
33
34
# File 'lib/rbbcc/bcc.rb', line 32

def ksym(addr, show_module: false, show_offset: false)
  self.sym(addr, -1, show_module: show_module, show_offset: show_offset, demangle: false)
end

.ksymname(name) ⇒ Object



36
37
38
# File 'lib/rbbcc/bcc.rb', line 36

def ksymname(name)
  SymbolCache.resolve_global(name)
end

.pin!(fd, path) ⇒ Object

: (Integer | Hash[Symbol, untyped] fd, String path) -> String



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/rbbcc/bcc.rb', line 223

def pin!(fd, path)
  fd = fd[:fd] if fd.is_a?(Hash)
  unless fd.is_a?(Integer) && fd >= 0
    raise ArgumentError, "fd must exist and be a non-negative Integer"
  end
  unless path.is_a?(String) && !path.empty?
    raise ArgumentError, "path must be a non-empty String"
  end

  res = Clib.bpf_obj_pin(fd, path)
  if res < 0
    raise SystemCallError.new("Failed to pin BPF object to %s" % path, Fiddle.last_error)
  end
  path
end

.support_raw_tracepointObject



40
41
42
43
44
# File 'lib/rbbcc/bcc.rb', line 40

def support_raw_tracepoint
  # kernel symbol "bpf_find_raw_tracepoint"
  # indicates raw_tracepint support
  ksymname("bpf_find_raw_tracepoint") || ksymname("bpf_get_raw_tracepoint")
end

.sym(addr, pid, show_module: false, show_offset: false, demangle: true) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/rbbcc/bcc.rb', line 188

def sym(addr, pid, show_module: false, show_offset: false, demangle: true)
  # FIXME: case of typeofaddr.find('bpf_stack_build_id')
  #s = Clib::BCCSymbol.malloc
  #b = Clib::BCCStacktraceBuildID.malloc
  #b.status = addr.status
  #b.build_id = addr.build_id
  #b.u = addr.offset
  #Clib.bcc_buildsymcache_resolve(BPF.bsymcache, b, s)

  name, offset_, module_ = SymbolCache.cache(pid).resolve(addr, demangle)
  offset = (show_offset && name) ? ("+0x%x" % offset_) : ""
  name = name || "[unknown]"
  name = name + offset
  module_ = (show_module && module_) ? " [#{File.basename.basename(module_)}]" : ""
  return name + module_
end

Instance Method Details

#[](key) ⇒ Object



594
595
596
# File 'lib/rbbcc/bcc.rb', line 594

def [](key)
  self.tables[key] ||= get_table(key)
end

#[]=(key, value) ⇒ Object



598
599
600
# File 'lib/rbbcc/bcc.rb', line 598

def []=(key, value)
  self.tables[key] = value
end

#_open_ring_buffer(map_fd, fn, ctx) ⇒ Object



609
610
611
612
613
614
615
# File 'lib/rbbcc/bcc.rb', line 609

def _open_ring_buffer(map_fd, fn, ctx)
  buf = Clib.bpf_new_ringbuf(map_fd, fn, ctx)
  if !buf
    raise "Could not open ring buffer"
  end
  @_ringbuf_manager ||= buf
end

#attach_kprobe(event:, fn_name:, event_off: 0) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
# File 'lib/rbbcc/bcc.rb', line 354

def attach_kprobe(event:, fn_name:, event_off: 0)
  fn = load_func(fn_name, BPF::KPROBE)
  ev_name = "p_" + event.gsub(/[\+\.]/, "_")
  fd = Clib.bpf_attach_kprobe(fn[:fd], 0, ev_name, event, event_off, 0)
  if fd < 0
    raise SystemCallError.new("Failed to attach BPF program #{fn_name} to kprobe #{event}", Fiddle.last_error)
  end
  Util.debug "Attach: #{ev_name}"
  @kprobe_fds[ev_name] = fd
  [ev_name, fd]
end

#attach_kretprobe(event:, fn_name:, event_re: nil, maxactive: 0) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/rbbcc/bcc.rb', line 366

def attach_kretprobe(event:, fn_name:, event_re: nil, maxactive: 0)
  # TODO: if event_re ...
  fn = load_func(fn_name, BPF::KPROBE)
  ev_name = "r_" + event.gsub(/[\+\.]/, "_")
  fd = Clib.bpf_attach_kprobe(fn[:fd], 1, ev_name, event, 0, maxactive)
  if fd < 0
    raise SystemCallError.new("Failed to attach BPF program #{fn_name} to kretprobe #{event}", Fiddle.last_error)
  end
  Util.debug "Attach: #{ev_name}"
  @kprobe_fds[ev_name] = fd
  [ev_name, fd]
end

#attach_lsm(fn_name: "") ⇒ Object



437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/rbbcc/bcc.rb', line 437

def attach_lsm(fn_name: "")
  if @lsm_fds.keys.include?(fn_name)
    raise "LSM #{fn_name} has been attached"
  end

  fn = load_func(fn_name, BPF::LSM)
  fd = Clib.bpf_attach_lsm(fn[:fd])
  if fd < 0
    raise SystemCallError.new("Failed to attach LSM #{fn_name}", Fiddle.last_error)
  end
  Util.debug "Attach: #{fn_name}"
  @lsm_fds[fn_name] = fd
  self
end

#attach_raw_tracepoint(tp: "", fn_name: "") ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/rbbcc/bcc.rb', line 339

def attach_raw_tracepoint(tp: "", fn_name: "")
  if @raw_tracepoint_fds.keys.include?(tp)
    raise "Raw tracepoint #{tp} has been attached"
  end

  fn = load_func(fn_name, BPF::RAW_TRACEPOINT)
  fd = Clib.bpf_attach_raw_tracepoint(fn[:fd], tp)
  if fd < 0
    raise SystemCallError.new("Failed to attach BPF program #{fn_name} to raw tracepoint #{tp}", Fiddle.last_error)
  end
  Util.debug "Attach: #{tp}"
  @raw_tracepoint_fds[tp] = fd
  self
end

#attach_tracepoint(tp: "", tp_re: "", fn_name: "") ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
# File 'lib/rbbcc/bcc.rb', line 327

def attach_tracepoint(tp: "", tp_re: "", fn_name: "")
  fn = load_func(fn_name, BPF::TRACEPOINT)
  tp_category, tp_name = tp.split(':')
  fd = Clib.bpf_attach_tracepoint(fn[:fd], tp_category, tp_name)
  if fd < 0
    raise SystemCallError.new("Failed to attach BPF program #{fn_name} to tracepoint #{tp}", Fiddle.last_error)
  end
  Util.debug "Attach: #{tp}"
  @tracepoint_fds[tp] = fd
  self
end

#attach_uprobe(name: "", sym: "", addr: nil, fn_name: "", pid: -1)) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/rbbcc/bcc.rb', line 379

def attach_uprobe(name: "", sym: "", addr: nil, fn_name: "", pid: -1)
  path, addr = BCC.check_path_symbol(name, sym, addr, pid)

  fn = load_func(fn_name, BPF::KPROBE)
  ev_name = to_uprobe_evname("p", path, addr, pid)
  fd = Clib.bpf_attach_uprobe(fn[:fd], 0, ev_name, path, addr, pid)
  if fd < 0
    raise SystemCallError.new(Fiddle.last_error)
  end
  Util.debug "Attach: #{ev_name}"

  @uprobe_fds[ev_name] = fd
  [ev_name, fd]
end

#attach_uretprobe(name: "", sym: "", addr: nil, fn_name: "", pid: -1)) ⇒ Object



394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/rbbcc/bcc.rb', line 394

def attach_uretprobe(name: "", sym: "", addr: nil, fn_name: "", pid: -1)
  path, addr = BCC.check_path_symbol(name, sym, addr, pid)

  fn = load_func(fn_name, BPF::KPROBE)
  ev_name = to_uprobe_evname("r", path, addr, pid)
  fd = Clib.bpf_attach_uprobe(fn[:fd], 1, ev_name, path, addr, pid)
  if fd < 0
    raise SystemCallError.new(Fiddle.last_error)
  end
  Util.debug "Attach: #{ev_name}"

  @uprobe_fds[ev_name] = fd
  [ev_name, fd]
end

#cleanupObject



547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/rbbcc/bcc.rb', line 547

def cleanup
  @kprobe_fds.each do |k, v|
    detach_kprobe_event(k)
  end

  @uprobe_fds.each do |k, v|
    detach_uprobe_event(k)
  end

  @tracepoint_fds.each do |k, v|
    detach_tracepoint(k)
  end

  @raw_tracepoint_fds.each do |k, v|
    detach_raw_tracepoint(k)
  end

  @lsm_fds.each do |k, v|
    detach_lsm(k)
  end

  if @module
    Clib.bpf_module_destroy(@module)
  end
end

#detach_kprobe_event(ev_name) ⇒ Object



465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/rbbcc/bcc.rb', line 465

def detach_kprobe_event(ev_name)
  unless @kprobe_fds.keys.include?(ev_name)
    raise "Event #{ev_name} not registered"
  end
  if Clib.bpf_close_perf_event_fd(@kprobe_fds[ev_name]) < 0
    raise SystemCallError.new(Fiddle.last_error)
  end
  if Clib.bpf_detach_kprobe(ev_name) < 0
    raise SystemCallError.new(Fiddle.last_error)
  end
  @kprobe_fds.delete(ev_name)
end

#detach_lsm(fn_name) ⇒ Object



452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/rbbcc/bcc.rb', line 452

def detach_lsm(fn_name)
  unless @lsm_fds.keys.include?(fn_name)
    raise "LSM #{fn_name} is not attached"
  end

  begin
    File.for_fd(@lsm_fds[fn_name]).close
  rescue => e
    warn "Closing fd failed: #{e.inspect}. Ignore and skip"
  end
  @lsm_fds.delete(fn_name)
end

#detach_raw_tracepoint(tp) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
# File 'lib/rbbcc/bcc.rb', line 425

def detach_raw_tracepoint(tp)
  unless @raw_tracepoint_fds.keys.include?(tp)
    raise "Raw tracepoint #{tp} is not attached"
  end
  begin
    File.for_fd(@raw_tracepoint_fds[tp]).close
  rescue => e
    warn "Closing fd failed: #{e.inspect}. Ignore and skip"
  end
  @tracepoint_fds.delete(tp)
end

#detach_tracepoint(tp) ⇒ Object



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/rbbcc/bcc.rb', line 409

def detach_tracepoint(tp)
  unless @tracepoint_fds.keys.include?(tp)
    raise "Tracepoint #{tp} is not attached"
  end
  res = Clib.bpf_close_perf_event_fd(@tracepoint_fds[tp])
  if res < 0
    raise "Failed to detach BPF from tracepoint"
  end
  tp_category, tp_name = tp.split(':')
  res = Clib.bpf_detach_tracepoint(tp_category, tp_name)
  if res < 0
    raise "Failed to detach BPF from tracepoint"
  end
  @tracepoint_fds.delete(tp)
end

#detach_uprobe_event(ev_name) ⇒ Object



478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/rbbcc/bcc.rb', line 478

def detach_uprobe_event(ev_name)
  unless @uprobe_fds.keys.include?(ev_name)
    raise "Event #{ev_name} not registered"
  end
  if Clib.bpf_close_perf_event_fd(@uprobe_fds[ev_name]) < 0
    raise SystemCallError.new(Fiddle.last_error)
  end
  if Clib.bpf_detach_uprobe(ev_name) < 0
    raise SystemCallError.new(Fiddle.last_error)
  end
  @uprobe_fds.delete(ev_name)
end

#gen_args_from_usdtObject



298
299
300
301
302
303
304
305
# File 'lib/rbbcc/bcc.rb', line 298

def gen_args_from_usdt
  ptr = Clib.bcc_usdt_genargs(@usdt_contexts.map(&:context).pack('J*'), @usdt_contexts.size)
  if !ptr || ptr.null?
    return nil
  end

  Clib.__extract_char ptr
end

#get_syscall_fnname(name) ⇒ Object



637
638
639
# File 'lib/rbbcc/bcc.rb', line 637

def get_syscall_fnname(name)
  get_syscall_prefix + name
end

#get_syscall_prefixObject



628
629
630
631
632
633
634
635
# File 'lib/rbbcc/bcc.rb', line 628

def get_syscall_prefix
  SYSCALL_PREFIXES.each do |prefix|
    if ksymname("%sbpf" % prefix)
      return prefix
    end
  end
  SYSCALL_PREFIXES[0]
end

#get_table(name, keytype: nil, leaftype: nil, reducer: nil) ⇒ Object

Raises:

  • (KeyError)


574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# File 'lib/rbbcc/bcc.rb', line 574

def get_table(name, keytype: nil, leaftype: nil, reducer: nil)
  map_id = Clib.bpf_table_id(@module, name)
  map_fd = Clib.bpf_table_fd(@module, name)

  raise KeyError, "map not found" if map_fd < 0
  unless keytype
    key_desc = Clib.bpf_table_key_desc(@module, name)
    raise("Failed to load BPF Table #{name} key desc") if key_desc.null?
    keytype = BCC.decode_table_type(eval(key_desc.to_extracted_char_ptr))
  end

  unless leaftype
    leaf_desc = Clib.bpf_table_leaf_desc(@module, name)
    raise("Failed to load BPF Table #{name} leaf desc") if leaf_desc.null?

    leaftype = BCC.decode_table_type(eval(leaf_desc.to_extracted_char_ptr))
  end
  return Table.new(self, map_id, map_fd, keytype, leaftype, name, reducer: reducer)
end

#ksymname(name) ⇒ Object



624
625
626
# File 'lib/rbbcc/bcc.rb', line 624

def ksymname(name)
  SymbolCache.resolve_global(name)
end

#load_func(func_name, prog_type, device: nil) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/rbbcc/bcc.rb', line 307

def load_func(func_name, prog_type, device: nil)
  if @funcs.keys.include?(func_name)
    return @funcs[func_name]
  end

  log_level = 0
  fd = Clib.do_bcc_func_load(@module, prog_type, func_name,
         Clib.bpf_function_start(@module, func_name),
         Clib.bpf_function_size(@module, func_name),
         Clib.bpf_module_license(@module),
         Clib.bpf_module_kern_version(@module),
         log_level, nil, 0, device);
  if fd < 0
    raise SystemCallError.new(Fiddle.last_error)
  end
  fnobj = {fd: fd, name: func_name}
  @funcs[func_name] = fnobj
  return fnobj
end

#num_open_kprobesObject



491
492
493
# File 'lib/rbbcc/bcc.rb', line 491

def num_open_kprobes
  @kprobe_fds.size
end

#num_open_lsmsObject



503
504
505
# File 'lib/rbbcc/bcc.rb', line 503

def num_open_lsms
  @lsm_fds.size
end

#num_open_tracepointsObject



499
500
501
# File 'lib/rbbcc/bcc.rb', line 499

def num_open_tracepoints
  @tracepoint_fds.size
end

#num_open_uprobesObject



495
496
497
# File 'lib/rbbcc/bcc.rb', line 495

def num_open_uprobes
  @uprobe_fds.size
end

#perf_buffer_poll(timeout = -1)) ⇒ Object



602
603
604
605
606
607
# File 'lib/rbbcc/bcc.rb', line 602

def perf_buffer_poll(timeout=-1)
  readers = self.perf_buffers.values
  readers.each {|r| r.size = Clib::PerfReader.size }
  pack = readers.map{|r| r[0, Clib::PerfReader.size] }.pack('p*')
  Clib.perf_reader_poll(readers.size, pack, timeout)
end

#ring_buffer_poll(timeout = -1)) ⇒ Object



617
618
619
620
621
622
# File 'lib/rbbcc/bcc.rb', line 617

def ring_buffer_poll(timeout=-1)
  unless @_ringbuf_manager
    raise "No ring buffers to poll"
  end
  Clib.bpf_poll_ringbuf(@_ringbuf_manager, timeout)
end

#trace_fields(&do_each_line) ⇒ Object



527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/rbbcc/bcc.rb', line 527

def trace_fields(&do_each_line)
  ret = []
  while buf = trace_readline
    next if buf.chomp.empty?
    next if buf.start_with? "CPU:"
    task = buf[0..15].lstrip()
    meta, _addr, msg = buf[17..-1].split(": ", 3)
    pid, cpu, flags, ts = meta.split(" ")
    cpu = cpu[1..-2]

    if do_each_line
      do_each_line.call(task, pid, cpu, flags, ts, msg)
    else
      ret = [task, pid, cpu, flags, ts, msg]
      break
    end
  end
  ret
end

#trace_print(fmt: nil) ⇒ Object



515
516
517
518
519
520
521
522
523
524
525
# File 'lib/rbbcc/bcc.rb', line 515

def trace_print(fmt: nil)
  loop do
    if fmt
    # TBD
    else
      line = trace_readline
    end
    puts line
    $stdout.flush
  end
end

#trace_readlineObject



511
512
513
# File 'lib/rbbcc/bcc.rb', line 511

def trace_readline
  tracefile.readline(1024)
end

#tracefileObject



507
508
509
# File 'lib/rbbcc/bcc.rb', line 507

def tracefile
  @tracefile ||= File.open("#{TRACEFS}/trace_pipe", "rb")
end