diff --git a/ext/rbtrace.c b/ext/rbtrace.c index db2e184..bce73b1 100644 --- a/ext/rbtrace.c +++ b/ext/rbtrace.c @@ -1143,20 +1143,6 @@ Init_rbtrace() rb_define_singleton_method(output, "write", send_write, 1); - rb_eval_string( - "module RBTrace\n" - " def self.eval_context\n" - " @eval_context ||= binding\n" - " end\n" - - " def self.eval_and_inspect(code)\n" - " t = Thread.new { Thread.current[:output] = eval_context.eval(code).inspect }\n" - " t.join\n" - " t[:output]\n" - " end\n" - "end\n" - ); - // hook into the gc gc_hook = Data_Wrap_Struct(rb_cObject, rbtrace_gc_mark, NULL, NULL); rb_global_variable(&gc_hook); diff --git a/lib/rbtrace.rb b/lib/rbtrace.rb new file mode 100644 index 0000000..73c698a --- /dev/null +++ b/lib/rbtrace.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module RBTrace + class << self + def eval_and_inspect(code) + t = Thread.new do + Thread.current.name = '__RBTrace__' + Thread.current[:output] = eval_context.eval(code).inspect + end + t.join + t[:output] + end + + private + + def eval_context + @eval_context ||= binding + end + end +end + +require 'rbtrace.so' diff --git a/lib/rbtrace/cli.rb b/lib/rbtrace/cli.rb index 99ee812..6be619d 100644 --- a/lib/rbtrace/cli.rb +++ b/lib/rbtrace/cli.rb @@ -449,7 +449,7 @@ def self.run delim = "146621c9d681409aa" - code = "Thread.list.map{|t| t.backtrace[0...#{num}].join(\"#{delim}\")}.join(\"#{delim*2}\")" + code = "Thread.list.reject { |t| t.name == '__RBTrace__' }.map{ |t| t.backtrace[0...#{num}].join(\"#{delim}\")}.join(\"#{delim*2}\")" if res = tracer.eval(code) tracer.puts res.split(delim).join("\n")