Class: Tracer

Inherits:
Object
  • Object
show all
Defined in:
lib/tracer.rb

Overview

tracer main class

Constant Summary collapse

EVENT_SYMBOL =
{
  "line" => "-",
  "call" => ">",
  "return" => "<",
  "class" => "C",
  "end" => "E",
  "raise" => "^",
  "c-call" => "}",
  "c-return" => "{",
  "unknown" => "?"
}
Single =
new

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTracer

Returns a new instance of Tracer.



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tracer.rb', line 55

def initialize
  @threads = Hash.new
  if defined? Thread.main
    @threads[Thread.main.object_id] = 0
  else
    @threads[Thread.current.object_id] = 0
  end

  @get_line_procs = {}

  @filters = []
end

Class Attribute Details

.display_c_callObject Also known as: display_c_call?

display builtin method call?



32
33
34
# File 'lib/tracer.rb', line 32

def display_c_call
  @display_c_call
end

.display_process_idObject Also known as: display_process_id?

display process id?



24
25
26
# File 'lib/tracer.rb', line 24

def display_process_id
  @display_process_id
end

.display_thread_idObject Also known as: display_thread_id?

display thread id?



28
29
30
# File 'lib/tracer.rb', line 28

def display_thread_id
  @display_thread_id
end

.stdoutObject

Returns the value of attribute stdout



20
21
22
# File 'lib/tracer.rb', line 20

def stdout
  @stdout
end

.stdout_mutexObject (readonly)

Returns the value of attribute stdout_mutex



21
22
23
# File 'lib/tracer.rb', line 21

def stdout_mutex
  @stdout_mutex
end

.verboseObject Also known as: verbose?

Returns the value of attribute verbose



17
18
19
# File 'lib/tracer.rb', line 17

def verbose
  @verbose
end

Class Method Details

.add_filter(p = proc) ⇒ Object



179
180
181
# File 'lib/tracer.rb', line 179

def Tracer.add_filter(p = proc)
  Single.add_filter(p)
end

.offObject



171
172
173
# File 'lib/tracer.rb', line 171

def Tracer.off
  Single.off
end

.onObject



163
164
165
166
167
168
169
# File 'lib/tracer.rb', line 163

def Tracer.on
  if block_given?
    Single.on{yield}
  else
    Single.on
  end
end

.set_get_line_procs(file_name, p = proc) ⇒ Object



175
176
177
# File 'lib/tracer.rb', line 175

def Tracer.set_get_line_procs(file_name, p = proc)
  Single.set_get_line_procs(file_name, p)
end

Instance Method Details

#add_filter(p = proc) ⇒ Object



91
92
93
# File 'lib/tracer.rb', line 91

def add_filter(p = proc)
  @filters.push p
end

#get_line(file, line) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/tracer.rb', line 99

def get_line(file, line)
  if p = @get_line_procs[file]
    return p.call(line)
  end

  unless list = SCRIPT_LINES__[file]
    begin
	f = File::open(file)
	begin
 SCRIPT_LINES__[file] = list = f.readlines
	ensure
 f.close
	end
    rescue
	SCRIPT_LINES__[file] = list = []
    end
  end

  if l = list[line - 1]
    l
  else
    "-\n"
  end
end

#get_thread_noObject



124
125
126
127
128
129
130
# File 'lib/tracer.rb', line 124

def get_thread_no
  if no = @threads[Thread.current.object_id]
    no
  else
    @threads[Thread.current.object_id] = @threads.size
  end
end

#offObject



86
87
88
89
# File 'lib/tracer.rb', line 86

def off
  set_trace_func nil
  stdout.print "Trace off\n" if Tracer.verbose?
end

#onObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/tracer.rb', line 72

def on
  if block_given?
    on
    begin
	yield
    ensure
	off
    end
  else
    set_trace_func method(:trace_func).to_proc
    stdout.print "Trace on\n" if Tracer.verbose?
  end
end

#set_get_line_procs(file, p = proc) ⇒ Object



95
96
97
# File 'lib/tracer.rb', line 95

def set_get_line_procs(file, p = proc)
  @get_line_procs[file] = p
end

#stdoutObject



68
69
70
# File 'lib/tracer.rb', line 68

def stdout
  Tracer.stdout
end

#trace_func(event, file, line, id, binding, klass) ⇒ Object



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
# File 'lib/tracer.rb', line 132

def trace_func(event, file, line, id, binding, klass, *)
  return if file == __FILE__

  for p in @filters
    return unless p.call event, file, line, id, binding, klass
  end

  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

end