Class: Tracer

Inherits:
Object
  • Object
show all
Defined in:
lib/shared.rb,
lib/tracerr.rb,
lib/core_backtracer.rb

Overview

tracer main class

Constant Summary collapse

EVENT_SYMBOL =
{
  "line" => "-",
  "call" => ">",
  "return" => "<",
  "class" => "C",
  "end" => "E",
  "c-call" => ">",
  "c-return" => "<",
  "raise" => "R"
}
Single =
new
@@depths =

I think I added this [rdp]

{}
@@last_line =
nil
@@last_file =
nil
@@last_symbol =
nil

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTracer

Returns a new instance of Tracer.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tracerr.rb', line 37

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

.stdoutObject

Returns the value of attribute stdout.



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

def stdout
  @stdout
end

.verboseObject Also known as: verbose?

Returns the value of attribute verbose.



22
23
24
# File 'lib/tracerr.rb', line 22

def verbose
  @verbose
end

Class Method Details

.add_filter(p = proc) ⇒ Object



151
152
153
# File 'lib/tracerr.rb', line 151

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

.get_line(file, line) ⇒ Object



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

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

  unless list = SCRIPT_LINES__[file]
    begin
        raise 'might be a .so file' if file =~ /\.so$/
 f = 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

.offObject



143
144
145
# File 'lib/tracerr.rb', line 143

def Tracer.off
  Single.off
end

.onObject



135
136
137
138
139
140
141
# File 'lib/tracerr.rb', line 135

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

.output_locals(previous_binding, prefix = "\t\t") ⇒ Object



190
191
192
193
194
195
196
197
198
# File 'lib/core_backtracer.rb', line 190

def Tracer.output_locals(previous_binding, prefix="\t\t")
  locals_name = Kernel.eval("local_variables", previous_binding)
  locals = {}
  for name in locals_name do
	locals[name] = Kernel.eval(name, previous_binding)
  end
  
  puts "#{prefix}locals: " + locals.inspect
end

.set_get_line_procs(file_name, p = proc) ⇒ Object



147
148
149
# File 'lib/tracerr.rb', line 147

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



73
74
75
# File 'lib/tracerr.rb', line 73

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

#get_line(file, line) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/tracerr.rb', line 81

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

  unless list = SCRIPT_LINES__[file]
    begin
      raise 'binary file' if file =~ /\.so$/
	f = 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



107
108
109
110
111
112
113
# File 'lib/tracerr.rb', line 107

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

#offObject



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

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

#onObject



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

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



77
78
79
# File 'lib/tracerr.rb', line 77

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

#stdoutObject



50
51
52
# File 'lib/tracerr.rb', line 50

def stdout
  Tracer.stdout
end

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/tracerr.rb', line 115

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
  
  (saved_crit = Thread.critical) rescue nil
  (Thread.critical = true) rescue nil
  stdout.printf("#%d:%s:%d:%s:%s: %s",
    get_thread_no,
    file,
    line,
    klass || '',
    EVENT_SYMBOL[event],
    get_line(file, line))
  (Thread.critical = saved_crit) rescue nil
end