2009-07-21 07:45:24 -04:00
|
|
|
# tracer.rb -
|
|
|
|
# $Release Version: 0.3$
|
|
|
|
# $Revision: 1.12 $
|
|
|
|
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
1999-01-19 23:59:39 -05:00
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2009-07-21 07:45:24 -04:00
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2009-07-21 07:45:24 -04:00
|
|
|
require "thread"
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
#
|
|
|
|
# tracer main class
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
class Tracer
|
1999-01-19 23:59:39 -05:00
|
|
|
class << self
|
2006-07-20 13:36:36 -04:00
|
|
|
attr_accessor :verbose
|
1999-01-19 23:59:39 -05:00
|
|
|
alias verbose? verbose
|
2009-07-21 07:45:24 -04:00
|
|
|
|
2006-07-20 13:36:36 -04:00
|
|
|
attr_accessor :stdout
|
2009-07-21 07:45:24 -04:00
|
|
|
attr_reader :stdout_mutex
|
|
|
|
|
|
|
|
# display process id?
|
|
|
|
attr_accessor :display_process_id
|
|
|
|
alias display_process_id? display_process_id
|
|
|
|
|
|
|
|
# display thread id?
|
|
|
|
attr_accessor :display_thread_id
|
|
|
|
alias display_thread_id? display_thread_id
|
|
|
|
|
|
|
|
# display builtin method call?
|
|
|
|
attr_accessor :display_c_call
|
|
|
|
alias display_c_call? display_c_call
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
2009-07-21 07:45:24 -04:00
|
|
|
Tracer::stdout = STDOUT
|
|
|
|
Tracer::verbose = false
|
|
|
|
Tracer::display_process_id = false
|
|
|
|
Tracer::display_thread_id = true
|
|
|
|
Tracer::display_c_call = false
|
|
|
|
|
|
|
|
@stdout_mutex = Mutex.new
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
EVENT_SYMBOL = {
|
|
|
|
"line" => "-",
|
|
|
|
"call" => ">",
|
|
|
|
"return" => "<",
|
|
|
|
"class" => "C",
|
2000-12-21 22:22:25 -05:00
|
|
|
"end" => "E",
|
2009-07-21 07:45:24 -04:00
|
|
|
"raise" => "^",
|
|
|
|
"c-call" => "}",
|
|
|
|
"c-return" => "{",
|
|
|
|
"unknown" => "?"
|
2000-12-21 22:22:25 -05:00
|
|
|
}
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
def initialize
|
|
|
|
@threads = Hash.new
|
|
|
|
if defined? Thread.main
|
2002-11-07 14:18:16 -05:00
|
|
|
@threads[Thread.main.object_id] = 0
|
1999-01-19 23:59:39 -05:00
|
|
|
else
|
2002-11-07 14:18:16 -05:00
|
|
|
@threads[Thread.current.object_id] = 0
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@get_line_procs = {}
|
|
|
|
|
|
|
|
@filters = []
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-10-11 02:29:16 -04:00
|
|
|
def stdout
|
|
|
|
Tracer.stdout
|
|
|
|
end
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
def on
|
2000-06-28 04:31:35 -04:00
|
|
|
if block_given?
|
1999-01-19 23:59:39 -05:00
|
|
|
on
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
off
|
|
|
|
end
|
|
|
|
else
|
2003-06-16 03:14:50 -04:00
|
|
|
set_trace_func method(:trace_func).to_proc
|
2000-10-11 02:29:16 -04:00
|
|
|
stdout.print "Trace on\n" if Tracer.verbose?
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
def off
|
|
|
|
set_trace_func nil
|
2000-10-11 02:29:16 -04:00
|
|
|
stdout.print "Trace off\n" if Tracer.verbose?
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
def add_filter(p = proc)
|
|
|
|
@filters.push p
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_get_line_procs(file, p = proc)
|
|
|
|
@get_line_procs[file] = p
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
def get_line(file, line)
|
1999-01-19 23:59:39 -05:00
|
|
|
if p = @get_line_procs[file]
|
2002-06-28 10:42:46 -04:00
|
|
|
return p.call(line)
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
|
|
|
|
2001-12-21 04:23:28 -05:00
|
|
|
unless list = SCRIPT_LINES__[file]
|
1999-01-19 23:59:39 -05:00
|
|
|
begin
|
2009-07-21 07:45:24 -04:00
|
|
|
f = File::open(file)
|
2009-03-05 22:56:38 -05:00
|
|
|
begin
|
2001-12-21 04:23:28 -05:00
|
|
|
SCRIPT_LINES__[file] = list = f.readlines
|
1999-01-19 23:59:39 -05:00
|
|
|
ensure
|
|
|
|
f.close
|
|
|
|
end
|
|
|
|
rescue
|
2001-12-21 04:23:28 -05:00
|
|
|
SCRIPT_LINES__[file] = list = []
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
2004-10-20 02:53:42 -04:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if l = list[line - 1]
|
|
|
|
l
|
|
|
|
else
|
|
|
|
"-\n"
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
def get_thread_no
|
2002-11-07 14:18:16 -05:00
|
|
|
if no = @threads[Thread.current.object_id]
|
1999-01-19 23:59:39 -05:00
|
|
|
no
|
|
|
|
else
|
2002-11-07 14:18:16 -05:00
|
|
|
@threads[Thread.current.object_id] = @threads.size
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2002-10-02 12:45:35 -04:00
|
|
|
def trace_func(event, file, line, id, binding, klass, *)
|
2006-02-17 10:40:32 -05:00
|
|
|
return if file == __FILE__
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
for p in @filters
|
2000-12-21 22:22:25 -05:00
|
|
|
return unless p.call event, file, line, id, binding, klass
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2009-07-21 07:45:24 -04:00
|
|
|
return unless Tracer::display_c_call? or
|
|
|
|
event != "c-call" && event != "c-return"
|
|
|
|
|
|
|
|
Tracer::stdout_mutex.synchronize do
|
|
|
|
if EVENT_SYMBOL[event]
|
|
|
|
stdout.printf("<%d>", $$) if Tracer::display_process_id?
|
|
|
|
stdout.printf("#%d:", get_thread_no) if Tracer::display_thread_id?
|
|
|
|
if line == 0
|
|
|
|
source = "?\n"
|
|
|
|
else
|
|
|
|
source = get_line(file, line)
|
|
|
|
end
|
|
|
|
printf("%s:%d:%s:%s: %s",
|
|
|
|
file,
|
|
|
|
line,
|
|
|
|
klass || '',
|
|
|
|
EVENT_SYMBOL[event],
|
|
|
|
source)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
Single = new
|
|
|
|
def Tracer.on
|
2000-06-28 04:31:35 -04:00
|
|
|
if block_given?
|
1999-01-19 23:59:39 -05:00
|
|
|
Single.on{yield}
|
|
|
|
else
|
|
|
|
Single.on
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
def Tracer.off
|
|
|
|
Single.off
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
def Tracer.set_get_line_procs(file_name, p = proc)
|
|
|
|
Single.set_get_line_procs(file_name, p)
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
def Tracer.add_filter(p = proc)
|
|
|
|
Single.add_filter(p)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2001-10-29 00:07:26 -05:00
|
|
|
SCRIPT_LINES__ = {} unless defined? SCRIPT_LINES__
|
1999-10-15 04:52:18 -04:00
|
|
|
|
2006-02-17 10:40:32 -05:00
|
|
|
if $0 == __FILE__
|
|
|
|
# direct call
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2006-02-17 10:40:32 -05:00
|
|
|
$0 = ARGV[0]
|
|
|
|
ARGV.shift
|
|
|
|
Tracer.on
|
|
|
|
require $0
|
2010-09-16 19:30:30 -04:00
|
|
|
elsif caller.count {|bt| /\A<internal:[^<>]+>:/ !~ bt} <= 1
|
2006-02-17 10:40:32 -05:00
|
|
|
Tracer.on
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|