1 | #
|
---|
2 | # tracer.rb -
|
---|
3 | # $Release Version: 0.2$
|
---|
4 | # $Revision: 1.8 $
|
---|
5 | # $Date: 1998/05/19 03:42:49 $
|
---|
6 | # by Keiju ISHITSUKA(Nippon Rational Inc.)
|
---|
7 | #
|
---|
8 | # --
|
---|
9 | #
|
---|
10 | #
|
---|
11 | #
|
---|
12 |
|
---|
13 | #
|
---|
14 | # tracer main class
|
---|
15 | #
|
---|
16 | class Tracer
|
---|
17 | @RCS_ID='-$Id: tracer.rb,v 1.8 1998/05/19 03:42:49 keiju Exp keiju $-'
|
---|
18 |
|
---|
19 | @stdout = STDOUT
|
---|
20 | @verbose = false
|
---|
21 | class << self
|
---|
22 | attr :verbose, true
|
---|
23 | alias verbose? verbose
|
---|
24 | attr :stdout, true
|
---|
25 | end
|
---|
26 |
|
---|
27 | EVENT_SYMBOL = {
|
---|
28 | "line" => "-",
|
---|
29 | "call" => ">",
|
---|
30 | "return" => "<",
|
---|
31 | "class" => "C",
|
---|
32 | "end" => "E",
|
---|
33 | "c-call" => ">",
|
---|
34 | "c-return" => "<",
|
---|
35 | }
|
---|
36 |
|
---|
37 | def initialize
|
---|
38 | @threads = Hash.new
|
---|
39 | if defined? Thread.main
|
---|
40 | @threads[Thread.main.object_id] = 0
|
---|
41 | else
|
---|
42 | @threads[Thread.current.object_id] = 0
|
---|
43 | end
|
---|
44 |
|
---|
45 | @get_line_procs = {}
|
---|
46 |
|
---|
47 | @filters = []
|
---|
48 | end
|
---|
49 |
|
---|
50 | def stdout
|
---|
51 | Tracer.stdout
|
---|
52 | end
|
---|
53 |
|
---|
54 | def on
|
---|
55 | if block_given?
|
---|
56 | on
|
---|
57 | begin
|
---|
58 | yield
|
---|
59 | ensure
|
---|
60 | off
|
---|
61 | end
|
---|
62 | else
|
---|
63 | set_trace_func method(:trace_func).to_proc
|
---|
64 | stdout.print "Trace on\n" if Tracer.verbose?
|
---|
65 | end
|
---|
66 | end
|
---|
67 |
|
---|
68 | def off
|
---|
69 | set_trace_func nil
|
---|
70 | stdout.print "Trace off\n" if Tracer.verbose?
|
---|
71 | end
|
---|
72 |
|
---|
73 | def add_filter(p = proc)
|
---|
74 | @filters.push p
|
---|
75 | end
|
---|
76 |
|
---|
77 | def set_get_line_procs(file, p = proc)
|
---|
78 | @get_line_procs[file] = p
|
---|
79 | end
|
---|
80 |
|
---|
81 | def get_line(file, line)
|
---|
82 | if p = @get_line_procs[file]
|
---|
83 | return p.call(line)
|
---|
84 | end
|
---|
85 |
|
---|
86 | unless list = SCRIPT_LINES__[file]
|
---|
87 | begin
|
---|
88 | f = open(file)
|
---|
89 | begin
|
---|
90 | SCRIPT_LINES__[file] = list = f.readlines
|
---|
91 | ensure
|
---|
92 | f.close
|
---|
93 | end
|
---|
94 | rescue
|
---|
95 | SCRIPT_LINES__[file] = list = []
|
---|
96 | end
|
---|
97 | end
|
---|
98 |
|
---|
99 | if l = list[line - 1]
|
---|
100 | l
|
---|
101 | else
|
---|
102 | "-\n"
|
---|
103 | end
|
---|
104 | end
|
---|
105 |
|
---|
106 | def get_thread_no
|
---|
107 | if no = @threads[Thread.current.object_id]
|
---|
108 | no
|
---|
109 | else
|
---|
110 | @threads[Thread.current.object_id] = @threads.size
|
---|
111 | end
|
---|
112 | end
|
---|
113 |
|
---|
114 | def trace_func(event, file, line, id, binding, klass, *)
|
---|
115 | return if file == __FILE__
|
---|
116 |
|
---|
117 | for p in @filters
|
---|
118 | return unless p.call event, file, line, id, binding, klass
|
---|
119 | end
|
---|
120 |
|
---|
121 | saved_crit = Thread.critical
|
---|
122 | Thread.critical = true
|
---|
123 | stdout.printf("#%d:%s:%d:%s:%s: %s",
|
---|
124 | get_thread_no,
|
---|
125 | file,
|
---|
126 | line,
|
---|
127 | klass || '',
|
---|
128 | EVENT_SYMBOL[event],
|
---|
129 | get_line(file, line))
|
---|
130 | Thread.critical = saved_crit
|
---|
131 | end
|
---|
132 |
|
---|
133 | Single = new
|
---|
134 | def Tracer.on
|
---|
135 | if block_given?
|
---|
136 | Single.on{yield}
|
---|
137 | else
|
---|
138 | Single.on
|
---|
139 | end
|
---|
140 | end
|
---|
141 |
|
---|
142 | def Tracer.off
|
---|
143 | Single.off
|
---|
144 | end
|
---|
145 |
|
---|
146 | def Tracer.set_get_line_procs(file_name, p = proc)
|
---|
147 | Single.set_get_line_procs(file_name, p)
|
---|
148 | end
|
---|
149 |
|
---|
150 | def Tracer.add_filter(p = proc)
|
---|
151 | Single.add_filter(p)
|
---|
152 | end
|
---|
153 |
|
---|
154 | end
|
---|
155 |
|
---|
156 | SCRIPT_LINES__ = {} unless defined? SCRIPT_LINES__
|
---|
157 |
|
---|
158 | if $0 == __FILE__
|
---|
159 | # direct call
|
---|
160 |
|
---|
161 | $0 = ARGV[0]
|
---|
162 | ARGV.shift
|
---|
163 | Tracer.on
|
---|
164 | require $0
|
---|
165 | elsif caller(0).size == 1
|
---|
166 | Tracer.on
|
---|
167 | end
|
---|