1
2
3
4
5
6
7
8
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
class Tracer
MY_FILE_NAME_PATTERN = /^tracer\.(rb)?/
Threads = Hash.new
Sources = Hash.new
EVENT_SYMBOL = {
"line" => "-",
"call" => ">",
"return" => "<",
"class" => "C",
"end" => "E"}
def on
set_trace_func proc{|event, file, line, id, binding|
trace_func event, file, line, id, binding
}
print "Trace on\n"
end
def off
set_trace_func nil
print "Trace off\n"
end
def get_thread_no
unless no = Threads[Thread.current.id]
Threads[Thread.current.id] = no = Threads.size
end
no
end
def get_line(file, line)
unless list = Sources[file]
f =open(file)
begin
Sources[file] = list = f.readlines
ensure
f.close
end
end
list[line - 1]
end
def trace_func(event, file, line, id, binding)
return if File.basename(file) =~ MY_FILE_NAME_PATTERN
Thread.critical = TRUE
printf("#%d:%s:%d:%s: %s",
get_thread_no,
file,
line,
EVENT_SYMBOL[event],
get_line(file, line))
Thread.critical = FALSE
end
Single = new
def Tracer.on
Single.on
end
def Tracer.off
Single.off
end
end
if File.basename($0) =~ Tracer::MY_FILE_NAME_PATTERN
$0 = ARGV.shift
Tracer.on
load $0
else
Tracer.on
end
|