Class: RbBCC::BCC
- Inherits:
-
Object
- Object
- RbBCC::BCC
- Defined in:
- lib/rbbcc/bcc.rb
Instance Attribute Summary collapse
-
#module ⇒ Object
readonly
Returns the value of attribute module.
-
#perf_buffers ⇒ Object
readonly
Returns the value of attribute perf_buffers.
-
#tables ⇒ Object
readonly
Returns the value of attribute tables.
Class Method Summary collapse
- ._find_file(filename) ⇒ Object
- .attach_raw_socket(fn, dev) ⇒ Object
- .check_path_symbol(module_, symname, addr, pid) ⇒ Object
- .decode_table_type(desc) ⇒ Object
- .get_kprobe_functions(event_re) ⇒ Object
- .ksym(addr, show_module: false, show_offset: false) ⇒ Object
- .ksymname(name) ⇒ Object
- .support_raw_tracepoint ⇒ Object
- .sym(addr, pid, show_module: false, show_offset: false, demangle: true) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #_open_ring_buffer(map_fd, fn, ctx) ⇒ Object
- #attach_kprobe(event:, fn_name:, event_off: 0) ⇒ Object
- #attach_kretprobe(event:, fn_name:, event_re: nil, maxactive: 0) ⇒ Object
- #attach_raw_tracepoint(tp: "", fn_name: "") ⇒ Object
- #attach_tracepoint(tp: "", tp_re: "", fn_name: "") ⇒ Object
- #attach_uprobe(name: "", sym: "", addr: nil, fn_name: "", pid: -1)) ⇒ Object
- #attach_uretprobe(name: "", sym: "", addr: nil, fn_name: "", pid: -1)) ⇒ Object
- #cleanup ⇒ Object
- #detach_kprobe_event(ev_name) ⇒ Object
- #detach_raw_tracepoint(tp) ⇒ Object
- #detach_tracepoint(tp) ⇒ Object
- #detach_uprobe_event(ev_name) ⇒ Object
- #gen_args_from_usdt ⇒ Object
- #get_syscall_fnname(name) ⇒ Object
- #get_syscall_prefix ⇒ Object
- #get_table(name, keytype: nil, leaftype: nil, reducer: nil) ⇒ Object
-
#initialize(text: "", src_file: nil, hdr_file: nil, debug: 0, cflags: [], usdt_contexts: [], allow_rlimit: 0, dev_name: nil) ⇒ BCC
constructor
A new instance of BCC.
- #ksymname(name) ⇒ Object
- #load_func(func_name, prog_type, device: nil) ⇒ Object
- #num_open_kprobes ⇒ Object
- #num_open_tracepoints ⇒ Object
- #num_open_uprobes ⇒ Object
- #perf_buffer_poll(timeout = -1)) ⇒ Object
- #ring_buffer_poll(timeout = -1)) ⇒ Object
- #trace_fields(&do_each_line) ⇒ Object
- #trace_print(fmt: nil) ⇒ Object
- #trace_readline ⇒ Object
- #tracefile ⇒ Object
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.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 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 |
# File 'lib/rbbcc/bcc.rb', line 223 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 = {} 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
#module ⇒ Object (readonly)
Returns the value of attribute module.
278 279 280 |
# File 'lib/rbbcc/bcc.rb', line 278 def module @module end |
#perf_buffers ⇒ Object (readonly)
Returns the value of attribute perf_buffers.
278 279 280 |
# File 'lib/rbbcc/bcc.rb', line 278 def perf_buffers @perf_buffers end |
#tables ⇒ Object (readonly)
Returns the value of attribute tables.
520 521 522 |
# File 'lib/rbbcc/bcc.rb', line 520 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. "../#{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.("../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 |
.support_raw_tracepoint ⇒ Object
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
541 542 543 |
# File 'lib/rbbcc/bcc.rb', line 541 def [](key) self.tables[key] ||= get_table(key) end |
#[]=(key, value) ⇒ Object
545 546 547 |
# File 'lib/rbbcc/bcc.rb', line 545 def []=(key, value) self.tables[key] = value end |
#_open_ring_buffer(map_fd, fn, ctx) ⇒ Object
556 557 558 559 560 561 562 |
# File 'lib/rbbcc/bcc.rb', line 556 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
337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/rbbcc/bcc.rb', line 337 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
349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/rbbcc/bcc.rb', line 349 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_raw_tracepoint(tp: "", fn_name: "") ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/rbbcc/bcc.rb', line 322 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
310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/rbbcc/bcc.rb', line 310 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
362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/rbbcc/bcc.rb', line 362 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
377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/rbbcc/bcc.rb', line 377 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 |
#cleanup ⇒ Object
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
# File 'lib/rbbcc/bcc.rb', line 498 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 if @module Clib.bpf_module_destroy(@module) end end |
#detach_kprobe_event(ev_name) ⇒ Object
420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/rbbcc/bcc.rb', line 420 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_raw_tracepoint(tp) ⇒ Object
408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/rbbcc/bcc.rb', line 408 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
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/rbbcc/bcc.rb', line 392 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
433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/rbbcc/bcc.rb', line 433 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_usdt ⇒ Object
280 281 282 283 284 285 286 287 288 |
# File 'lib/rbbcc/bcc.rb', line 280 def gen_args_from_usdt ptr = Clib.bcc_usdt_genargs(@usdt_contexts.map(&:context).pack('J*'), @usdt_contexts.size) code = "" if !ptr || ptr.null? return nil end Clib.__extract_char ptr end |
#get_syscall_fnname(name) ⇒ Object
584 585 586 |
# File 'lib/rbbcc/bcc.rb', line 584 def get_syscall_fnname(name) get_syscall_prefix + name end |
#get_syscall_prefix ⇒ Object
575 576 577 578 579 580 581 582 |
# File 'lib/rbbcc/bcc.rb', line 575 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
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 |
# File 'lib/rbbcc/bcc.rb', line 521 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
571 572 573 |
# File 'lib/rbbcc/bcc.rb', line 571 def ksymname(name) SymbolCache.resolve_global(name) end |
#load_func(func_name, prog_type, device: nil) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/rbbcc/bcc.rb', line 290 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_kprobes ⇒ Object
446 447 448 |
# File 'lib/rbbcc/bcc.rb', line 446 def num_open_kprobes @kprobe_fds.size end |
#num_open_tracepoints ⇒ Object
454 455 456 |
# File 'lib/rbbcc/bcc.rb', line 454 def num_open_tracepoints @tracepoint_fds.size end |
#num_open_uprobes ⇒ Object
450 451 452 |
# File 'lib/rbbcc/bcc.rb', line 450 def num_open_uprobes @uprobe_fds.size end |
#perf_buffer_poll(timeout = -1)) ⇒ Object
549 550 551 552 553 554 |
# File 'lib/rbbcc/bcc.rb', line 549 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
564 565 566 567 568 569 |
# File 'lib/rbbcc/bcc.rb', line 564 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
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 |
# File 'lib/rbbcc/bcc.rb', line 478 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() , _addr, msg = buf[17..-1].split(": ", 3) pid, cpu, flags, ts = .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
466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/rbbcc/bcc.rb', line 466 def trace_print(fmt: nil) loop do if fmt # TBD else line = trace_readline end puts line $stdout.flush end end |
#trace_readline ⇒ Object
462 463 464 |
# File 'lib/rbbcc/bcc.rb', line 462 def trace_readline tracefile.readline(1024) end |
#tracefile ⇒ Object
458 459 460 |
# File 'lib/rbbcc/bcc.rb', line 458 def tracefile @tracefile ||= File.open("#{TRACEFS}/trace_pipe", "rb") end |