Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../proc/self/root/usr/share/ruby
File: tracer.rb
# frozen_string_literal: false
[0] Fix | Delete
#--
[1] Fix | Delete
# $Release Version: 0.3$
[2] Fix | Delete
# $Revision: 1.12 $
[3] Fix | Delete
[4] Fix | Delete
##
[5] Fix | Delete
# Outputs a source level execution trace of a Ruby program.
[6] Fix | Delete
#
[7] Fix | Delete
# It does this by registering an event handler with Kernel#set_trace_func for
[8] Fix | Delete
# processing incoming events. It also provides methods for filtering unwanted
[9] Fix | Delete
# trace output (see Tracer.add_filter, Tracer.on, and Tracer.off).
[10] Fix | Delete
#
[11] Fix | Delete
# == Example
[12] Fix | Delete
#
[13] Fix | Delete
# Consider the following Ruby script
[14] Fix | Delete
#
[15] Fix | Delete
# class A
[16] Fix | Delete
# def square(a)
[17] Fix | Delete
# return a*a
[18] Fix | Delete
# end
[19] Fix | Delete
# end
[20] Fix | Delete
#
[21] Fix | Delete
# a = A.new
[22] Fix | Delete
# a.square(5)
[23] Fix | Delete
#
[24] Fix | Delete
# Running the above script using <code>ruby -r tracer example.rb</code> will
[25] Fix | Delete
# output the following trace to STDOUT (Note you can also explicitly
[26] Fix | Delete
# <code>require 'tracer'</code>)
[27] Fix | Delete
#
[28] Fix | Delete
# #0:<internal:lib/rubygems/custom_require>:38:Kernel:<: -
[29] Fix | Delete
# #0:example.rb:3::-: class A
[30] Fix | Delete
# #0:example.rb:3::C: class A
[31] Fix | Delete
# #0:example.rb:4::-: def square(a)
[32] Fix | Delete
# #0:example.rb:7::E: end
[33] Fix | Delete
# #0:example.rb:9::-: a = A.new
[34] Fix | Delete
# #0:example.rb:10::-: a.square(5)
[35] Fix | Delete
# #0:example.rb:4:A:>: def square(a)
[36] Fix | Delete
# #0:example.rb:5:A:-: return a*a
[37] Fix | Delete
# #0:example.rb:6:A:<: end
[38] Fix | Delete
# | | | | |
[39] Fix | Delete
# | | | | ---------------------+ event
[40] Fix | Delete
# | | | ------------------------+ class
[41] Fix | Delete
# | | --------------------------+ line
[42] Fix | Delete
# | ------------------------------------+ filename
[43] Fix | Delete
# ---------------------------------------+ thread
[44] Fix | Delete
#
[45] Fix | Delete
# Symbol table used for displaying incoming events:
[46] Fix | Delete
#
[47] Fix | Delete
# +}+:: call a C-language routine
[48] Fix | Delete
# +{+:: return from a C-language routine
[49] Fix | Delete
# +>+:: call a Ruby method
[50] Fix | Delete
# +C+:: start a class or module definition
[51] Fix | Delete
# +E+:: finish a class or module definition
[52] Fix | Delete
# +-+:: execute code on a new line
[53] Fix | Delete
# +^+:: raise an exception
[54] Fix | Delete
# +<+:: return from a Ruby method
[55] Fix | Delete
#
[56] Fix | Delete
# == Copyright
[57] Fix | Delete
#
[58] Fix | Delete
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
[59] Fix | Delete
#
[60] Fix | Delete
class Tracer
[61] Fix | Delete
class << self
[62] Fix | Delete
# display additional debug information (defaults to false)
[63] Fix | Delete
attr_accessor :verbose
[64] Fix | Delete
alias verbose? verbose
[65] Fix | Delete
[66] Fix | Delete
# output stream used to output trace (defaults to STDOUT)
[67] Fix | Delete
attr_accessor :stdout
[68] Fix | Delete
[69] Fix | Delete
# mutex lock used by tracer for displaying trace output
[70] Fix | Delete
attr_reader :stdout_mutex
[71] Fix | Delete
[72] Fix | Delete
# display process id in trace output (defaults to false)
[73] Fix | Delete
attr_accessor :display_process_id
[74] Fix | Delete
alias display_process_id? display_process_id
[75] Fix | Delete
[76] Fix | Delete
# display thread id in trace output (defaults to true)
[77] Fix | Delete
attr_accessor :display_thread_id
[78] Fix | Delete
alias display_thread_id? display_thread_id
[79] Fix | Delete
[80] Fix | Delete
# display C-routine calls in trace output (defaults to false)
[81] Fix | Delete
attr_accessor :display_c_call
[82] Fix | Delete
alias display_c_call? display_c_call
[83] Fix | Delete
end
[84] Fix | Delete
[85] Fix | Delete
Tracer::stdout = STDOUT
[86] Fix | Delete
Tracer::verbose = false
[87] Fix | Delete
Tracer::display_process_id = false
[88] Fix | Delete
Tracer::display_thread_id = true
[89] Fix | Delete
Tracer::display_c_call = false
[90] Fix | Delete
[91] Fix | Delete
@stdout_mutex = Thread::Mutex.new
[92] Fix | Delete
[93] Fix | Delete
# Symbol table used for displaying trace information
[94] Fix | Delete
EVENT_SYMBOL = {
[95] Fix | Delete
"line" => "-",
[96] Fix | Delete
"call" => ">",
[97] Fix | Delete
"return" => "<",
[98] Fix | Delete
"class" => "C",
[99] Fix | Delete
"end" => "E",
[100] Fix | Delete
"raise" => "^",
[101] Fix | Delete
"c-call" => "}",
[102] Fix | Delete
"c-return" => "{",
[103] Fix | Delete
"unknown" => "?"
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
def initialize # :nodoc:
[107] Fix | Delete
@threads = Hash.new
[108] Fix | Delete
if defined? Thread.main
[109] Fix | Delete
@threads[Thread.main.object_id] = 0
[110] Fix | Delete
else
[111] Fix | Delete
@threads[Thread.current.object_id] = 0
[112] Fix | Delete
end
[113] Fix | Delete
[114] Fix | Delete
@get_line_procs = {}
[115] Fix | Delete
[116] Fix | Delete
@filters = []
[117] Fix | Delete
end
[118] Fix | Delete
[119] Fix | Delete
def stdout # :nodoc:
[120] Fix | Delete
Tracer.stdout
[121] Fix | Delete
end
[122] Fix | Delete
[123] Fix | Delete
def on # :nodoc:
[124] Fix | Delete
if block_given?
[125] Fix | Delete
on
[126] Fix | Delete
begin
[127] Fix | Delete
yield
[128] Fix | Delete
ensure
[129] Fix | Delete
off
[130] Fix | Delete
end
[131] Fix | Delete
else
[132] Fix | Delete
set_trace_func method(:trace_func).to_proc
[133] Fix | Delete
stdout.print "Trace on\n" if Tracer.verbose?
[134] Fix | Delete
end
[135] Fix | Delete
end
[136] Fix | Delete
[137] Fix | Delete
def off # :nodoc:
[138] Fix | Delete
set_trace_func nil
[139] Fix | Delete
stdout.print "Trace off\n" if Tracer.verbose?
[140] Fix | Delete
end
[141] Fix | Delete
[142] Fix | Delete
def add_filter(p = proc) # :nodoc:
[143] Fix | Delete
@filters.push p
[144] Fix | Delete
end
[145] Fix | Delete
[146] Fix | Delete
def set_get_line_procs(file, p = proc) # :nodoc:
[147] Fix | Delete
@get_line_procs[file] = p
[148] Fix | Delete
end
[149] Fix | Delete
[150] Fix | Delete
def get_line(file, line) # :nodoc:
[151] Fix | Delete
if p = @get_line_procs[file]
[152] Fix | Delete
return p.call(line)
[153] Fix | Delete
end
[154] Fix | Delete
[155] Fix | Delete
unless list = SCRIPT_LINES__[file]
[156] Fix | Delete
list = File.readlines(file) rescue []
[157] Fix | Delete
SCRIPT_LINES__[file] = list
[158] Fix | Delete
end
[159] Fix | Delete
[160] Fix | Delete
if l = list[line - 1]
[161] Fix | Delete
l
[162] Fix | Delete
else
[163] Fix | Delete
"-\n"
[164] Fix | Delete
end
[165] Fix | Delete
end
[166] Fix | Delete
[167] Fix | Delete
def get_thread_no # :nodoc:
[168] Fix | Delete
if no = @threads[Thread.current.object_id]
[169] Fix | Delete
no
[170] Fix | Delete
else
[171] Fix | Delete
@threads[Thread.current.object_id] = @threads.size
[172] Fix | Delete
end
[173] Fix | Delete
end
[174] Fix | Delete
[175] Fix | Delete
def trace_func(event, file, line, id, binding, klass, *) # :nodoc:
[176] Fix | Delete
return if file == __FILE__
[177] Fix | Delete
[178] Fix | Delete
for p in @filters
[179] Fix | Delete
return unless p.call event, file, line, id, binding, klass
[180] Fix | Delete
end
[181] Fix | Delete
[182] Fix | Delete
return unless Tracer::display_c_call? or
[183] Fix | Delete
event != "c-call" && event != "c-return"
[184] Fix | Delete
[185] Fix | Delete
Tracer::stdout_mutex.synchronize do
[186] Fix | Delete
if EVENT_SYMBOL[event]
[187] Fix | Delete
stdout.printf("<%d>", $$) if Tracer::display_process_id?
[188] Fix | Delete
stdout.printf("#%d:", get_thread_no) if Tracer::display_thread_id?
[189] Fix | Delete
if line == 0
[190] Fix | Delete
source = "?\n"
[191] Fix | Delete
else
[192] Fix | Delete
source = get_line(file, line)
[193] Fix | Delete
end
[194] Fix | Delete
stdout.printf("%s:%d:%s:%s: %s",
[195] Fix | Delete
file,
[196] Fix | Delete
line,
[197] Fix | Delete
klass || '',
[198] Fix | Delete
EVENT_SYMBOL[event],
[199] Fix | Delete
source)
[200] Fix | Delete
end
[201] Fix | Delete
end
[202] Fix | Delete
[203] Fix | Delete
end
[204] Fix | Delete
[205] Fix | Delete
# Reference to singleton instance of Tracer
[206] Fix | Delete
Single = new
[207] Fix | Delete
[208] Fix | Delete
##
[209] Fix | Delete
# Start tracing
[210] Fix | Delete
#
[211] Fix | Delete
# === Example
[212] Fix | Delete
#
[213] Fix | Delete
# Tracer.on
[214] Fix | Delete
# # code to trace here
[215] Fix | Delete
# Tracer.off
[216] Fix | Delete
#
[217] Fix | Delete
# You can also pass a block:
[218] Fix | Delete
#
[219] Fix | Delete
# Tracer.on {
[220] Fix | Delete
# # trace everything in this block
[221] Fix | Delete
# }
[222] Fix | Delete
[223] Fix | Delete
def Tracer.on
[224] Fix | Delete
if block_given?
[225] Fix | Delete
Single.on{yield}
[226] Fix | Delete
else
[227] Fix | Delete
Single.on
[228] Fix | Delete
end
[229] Fix | Delete
end
[230] Fix | Delete
[231] Fix | Delete
##
[232] Fix | Delete
# Disable tracing
[233] Fix | Delete
[234] Fix | Delete
def Tracer.off
[235] Fix | Delete
Single.off
[236] Fix | Delete
end
[237] Fix | Delete
[238] Fix | Delete
##
[239] Fix | Delete
# Register an event handler <code>p</code> which is called everytime a line
[240] Fix | Delete
# in +file_name+ is executed.
[241] Fix | Delete
#
[242] Fix | Delete
# Example:
[243] Fix | Delete
#
[244] Fix | Delete
# Tracer.set_get_line_procs("example.rb", lambda { |line|
[245] Fix | Delete
# puts "line number executed is #{line}"
[246] Fix | Delete
# })
[247] Fix | Delete
[248] Fix | Delete
def Tracer.set_get_line_procs(file_name, p = proc)
[249] Fix | Delete
Single.set_get_line_procs(file_name, p)
[250] Fix | Delete
end
[251] Fix | Delete
[252] Fix | Delete
##
[253] Fix | Delete
# Used to filter unwanted trace output
[254] Fix | Delete
#
[255] Fix | Delete
# Example which only outputs lines of code executed within the Kernel class:
[256] Fix | Delete
#
[257] Fix | Delete
# Tracer.add_filter do |event, file, line, id, binding, klass, *rest|
[258] Fix | Delete
# "Kernel" == klass.to_s
[259] Fix | Delete
# end
[260] Fix | Delete
[261] Fix | Delete
def Tracer.add_filter(p = proc)
[262] Fix | Delete
Single.add_filter(p)
[263] Fix | Delete
end
[264] Fix | Delete
end
[265] Fix | Delete
[266] Fix | Delete
# :stopdoc:
[267] Fix | Delete
SCRIPT_LINES__ = {} unless defined? SCRIPT_LINES__
[268] Fix | Delete
[269] Fix | Delete
if $0 == __FILE__
[270] Fix | Delete
# direct call
[271] Fix | Delete
[272] Fix | Delete
$0 = ARGV[0]
[273] Fix | Delete
ARGV.shift
[274] Fix | Delete
Tracer.on
[275] Fix | Delete
require $0
[276] Fix | Delete
else
[277] Fix | Delete
# call Tracer.on only if required by -r command-line option
[278] Fix | Delete
count = caller.count {|bt| %r%/rubygems/core_ext/kernel_require\.rb:% !~ bt}
[279] Fix | Delete
if (defined?(Gem) and count == 0) or
[280] Fix | Delete
(!defined?(Gem) and count <= 1)
[281] Fix | Delete
Tracer.on
[282] Fix | Delete
end
[283] Fix | Delete
end
[284] Fix | Delete
# :startdoc:
[285] Fix | Delete
[286] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function