Added CROSS_COMPILING flag to rbconfig.
[rbx.git] / tools / tracer.rb
blobb7236c3a507c48e6769f4e6a47bc82d3cadbcd63
2 # Example Usage:
4 # ruby -Itools -rtracer blah.rb > spec/profiles/blah.yaml
6 require 'yaml'
8 class Tracer
9   @@stdout = STDOUT
11   EVENT_SYMBOL = {
12     :call       => ">",
13     :class      => "C",
14     :"c-call"   => ">",
15   }
17   def initialize
18     # @counts[klass][method] += 1
19     @counts = Hash.new { |h,k| h[k] = Hash.new 0 }
20   end
22   def on
23     set_trace_func method(:trace_func).to_proc
25     at_exit {
26       off
27       print_calls
28     }
29   end
31   def off
32     set_trace_func nil
33   end
35   def print_calls
36     # @counts["klass[.|#]"] = [methods]
37     counts = Hash.new { |h,k| h[k] = [] }
39     @counts.each do |klass, methods|
40       pim = klass.public_instance_methods(false).map { |s| s.to_sym }
41       methods.each do |method, _|
42         name = if pim.include? method then
43                  "#{klass}#"
44                else
45                  "#{klass}."
46                end
47         counts[name] << method
48       end
49     end
51     y counts
52   end
54   def trace_func(event, file, line, id, binding, klass, *)
55     return if file == __FILE__
56     return unless EVENT_SYMBOL[event.to_sym]
57     return if klass.nil? or id.nil?
59     saved_crit = Thread.critical
60     Thread.critical = true
62     @counts[klass][id] += 1
64     Thread.critical = saved_crit
65   end
67   Single = new
68   def Tracer.on
69     Single.on
70   end
72   def Tracer.off
73     Single.off
74   end
75 end
77 if $0 == __FILE__
78   # direct call
80   $0 = ARGV[0]
81   ARGV.shift
82   Tracer.on
83   require $0
84 elsif caller(0).size == 1
85   Tracer.on
86 end