Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby27/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
[62] Fix | Delete
class << self
[63] Fix | Delete
# display additional debug information (defaults to false)
[64] Fix | Delete
attr_accessor :verbose
[65] Fix | Delete
alias verbose? verbose
[66] Fix | Delete
[67] Fix | Delete
# output stream used to output trace (defaults to STDOUT)
[68] Fix | Delete
attr_accessor :stdout
[69] Fix | Delete
[70] Fix | Delete
# mutex lock used by tracer for displaying trace output
[71] Fix | Delete
attr_reader :stdout_mutex
[72] Fix | Delete
[73] Fix | Delete
# display process id in trace output (defaults to false)
[74] Fix | Delete
attr_accessor :display_process_id
[75] Fix | Delete
alias display_process_id? display_process_id
[76] Fix | Delete
[77] Fix | Delete
# display thread id in trace output (defaults to true)
[78] Fix | Delete
attr_accessor :display_thread_id
[79] Fix | Delete
alias display_thread_id? display_thread_id
[80] Fix | Delete
[81] Fix | Delete
# display C-routine calls in trace output (defaults to false)
[82] Fix | Delete
attr_accessor :display_c_call
[83] Fix | Delete
alias display_c_call? display_c_call
[84] Fix | Delete
end
[85] Fix | Delete
[86] Fix | Delete
Tracer::stdout = STDOUT
[87] Fix | Delete
Tracer::verbose = false
[88] Fix | Delete
Tracer::display_process_id = false
[89] Fix | Delete
Tracer::display_thread_id = true
[90] Fix | Delete
Tracer::display_c_call = false
[91] Fix | Delete
[92] Fix | Delete
@stdout_mutex = Thread::Mutex.new
[93] Fix | Delete
[94] Fix | Delete
# Symbol table used for displaying trace information
[95] Fix | Delete
EVENT_SYMBOL = {
[96] Fix | Delete
"line" => "-",
[97] Fix | Delete
"call" => ">",
[98] Fix | Delete
"return" => "<",
[99] Fix | Delete
"class" => "C",
[100] Fix | Delete
"end" => "E",
[101] Fix | Delete
"raise" => "^",
[102] Fix | Delete
"c-call" => "}",
[103] Fix | Delete
"c-return" => "{",
[104] Fix | Delete
"unknown" => "?"
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
def initialize # :nodoc:
[108] Fix | Delete
@threads = Hash.new
[109] Fix | Delete
if defined? Thread.main
[110] Fix | Delete
@threads[Thread.main.object_id] = 0
[111] Fix | Delete
else
[112] Fix | Delete
@threads[Thread.current.object_id] = 0
[113] Fix | Delete
end
[114] Fix | Delete
[115] Fix | Delete
@get_line_procs = {}
[116] Fix | Delete
[117] Fix | Delete
@filters = []
[118] Fix | Delete
end
[119] Fix | Delete
[120] Fix | Delete
def stdout # :nodoc:
[121] Fix | Delete
Tracer.stdout
[122] Fix | Delete
end
[123] Fix | Delete
[124] Fix | Delete
def on # :nodoc:
[125] Fix | Delete
if block_given?
[126] Fix | Delete
on
[127] Fix | Delete
begin
[128] Fix | Delete
yield
[129] Fix | Delete
ensure
[130] Fix | Delete
off
[131] Fix | Delete
end
[132] Fix | Delete
else
[133] Fix | Delete
set_trace_func method(:trace_func).to_proc
[134] Fix | Delete
stdout.print "Trace on\n" if Tracer.verbose?
[135] Fix | Delete
end
[136] Fix | Delete
end
[137] Fix | Delete
[138] Fix | Delete
def off # :nodoc:
[139] Fix | Delete
set_trace_func nil
[140] Fix | Delete
stdout.print "Trace off\n" if Tracer.verbose?
[141] Fix | Delete
end
[142] Fix | Delete
[143] Fix | Delete
def add_filter(p = nil, &b) # :nodoc:
[144] Fix | Delete
p ||= b
[145] Fix | Delete
@filters.push p
[146] Fix | Delete
end
[147] Fix | Delete
[148] Fix | Delete
def set_get_line_procs(file, p = nil, &b) # :nodoc:
[149] Fix | Delete
p ||= b
[150] Fix | Delete
@get_line_procs[file] = p
[151] Fix | Delete
end
[152] Fix | Delete
[153] Fix | Delete
def get_line(file, line) # :nodoc:
[154] Fix | Delete
if p = @get_line_procs[file]
[155] Fix | Delete
return p.call(line)
[156] Fix | Delete
end
[157] Fix | Delete
[158] Fix | Delete
unless list = SCRIPT_LINES__[file]
[159] Fix | Delete
list = File.readlines(file) rescue []
[160] Fix | Delete
SCRIPT_LINES__[file] = list
[161] Fix | Delete
end
[162] Fix | Delete
[163] Fix | Delete
if l = list[line - 1]
[164] Fix | Delete
l
[165] Fix | Delete
else
[166] Fix | Delete
"-\n"
[167] Fix | Delete
end
[168] Fix | Delete
end
[169] Fix | Delete
[170] Fix | Delete
def get_thread_no # :nodoc:
[171] Fix | Delete
if no = @threads[Thread.current.object_id]
[172] Fix | Delete
no
[173] Fix | Delete
else
[174] Fix | Delete
@threads[Thread.current.object_id] = @threads.size
[175] Fix | Delete
end
[176] Fix | Delete
end
[177] Fix | Delete
[178] Fix | Delete
def trace_func(event, file, line, id, binding, klass, *) # :nodoc:
[179] Fix | Delete
return if file == __FILE__
[180] Fix | Delete
[181] Fix | Delete
for p in @filters
[182] Fix | Delete
return unless p.call event, file, line, id, binding, klass
[183] Fix | Delete
end
[184] Fix | Delete
[185] Fix | Delete
return unless Tracer::display_c_call? or
[186] Fix | Delete
event != "c-call" && event != "c-return"
[187] Fix | Delete
[188] Fix | Delete
Tracer::stdout_mutex.synchronize do
[189] Fix | Delete
if EVENT_SYMBOL[event]
[190] Fix | Delete
stdout.printf("<%d>", $$) if Tracer::display_process_id?
[191] Fix | Delete
stdout.printf("#%d:", get_thread_no) if Tracer::display_thread_id?
[192] Fix | Delete
if line == 0
[193] Fix | Delete
source = "?\n"
[194] Fix | Delete
else
[195] Fix | Delete
source = get_line(file, line)
[196] Fix | Delete
end
[197] Fix | Delete
stdout.printf("%s:%d:%s:%s: %s",
[198] Fix | Delete
file,
[199] Fix | Delete
line,
[200] Fix | Delete
klass || '',
[201] Fix | Delete
EVENT_SYMBOL[event],
[202] Fix | Delete
source)
[203] Fix | Delete
end
[204] Fix | Delete
end
[205] Fix | Delete
[206] Fix | Delete
end
[207] Fix | Delete
[208] Fix | Delete
# Reference to singleton instance of Tracer
[209] Fix | Delete
Single = new
[210] Fix | Delete
[211] Fix | Delete
##
[212] Fix | Delete
# Start tracing
[213] Fix | Delete
#
[214] Fix | Delete
# === Example
[215] Fix | Delete
#
[216] Fix | Delete
# Tracer.on
[217] Fix | Delete
# # code to trace here
[218] Fix | Delete
# Tracer.off
[219] Fix | Delete
#
[220] Fix | Delete
# You can also pass a block:
[221] Fix | Delete
#
[222] Fix | Delete
# Tracer.on {
[223] Fix | Delete
# # trace everything in this block
[224] Fix | Delete
# }
[225] Fix | Delete
[226] Fix | Delete
def Tracer.on
[227] Fix | Delete
if block_given?
[228] Fix | Delete
Single.on{yield}
[229] Fix | Delete
else
[230] Fix | Delete
Single.on
[231] Fix | Delete
end
[232] Fix | Delete
end
[233] Fix | Delete
[234] Fix | Delete
##
[235] Fix | Delete
# Disable tracing
[236] Fix | Delete
[237] Fix | Delete
def Tracer.off
[238] Fix | Delete
Single.off
[239] Fix | Delete
end
[240] Fix | Delete
[241] Fix | Delete
##
[242] Fix | Delete
# Register an event handler <code>p</code> which is called every time a line
[243] Fix | Delete
# in +file_name+ is executed.
[244] Fix | Delete
#
[245] Fix | Delete
# Example:
[246] Fix | Delete
#
[247] Fix | Delete
# Tracer.set_get_line_procs("example.rb", lambda { |line|
[248] Fix | Delete
# puts "line number executed is #{line}"
[249] Fix | Delete
# })
[250] Fix | Delete
[251] Fix | Delete
def Tracer.set_get_line_procs(file_name, p = nil, &b)
[252] Fix | Delete
p ||= b
[253] Fix | Delete
Single.set_get_line_procs(file_name, p)
[254] Fix | Delete
end
[255] Fix | Delete
[256] Fix | Delete
##
[257] Fix | Delete
# Used to filter unwanted trace output
[258] Fix | Delete
#
[259] Fix | Delete
# Example which only outputs lines of code executed within the Kernel class:
[260] Fix | Delete
#
[261] Fix | Delete
# Tracer.add_filter do |event, file, line, id, binding, klass, *rest|
[262] Fix | Delete
# "Kernel" == klass.to_s
[263] Fix | Delete
# end
[264] Fix | Delete
[265] Fix | Delete
def Tracer.add_filter(p = nil, &b)
[266] Fix | Delete
p ||= b
[267] Fix | Delete
Single.add_filter(p)
[268] Fix | Delete
end
[269] Fix | Delete
end
[270] Fix | Delete
[271] Fix | Delete
# :stopdoc:
[272] Fix | Delete
SCRIPT_LINES__ = {} unless defined? SCRIPT_LINES__
[273] Fix | Delete
[274] Fix | Delete
if $0 == __FILE__
[275] Fix | Delete
# direct call
[276] Fix | Delete
[277] Fix | Delete
$0 = ARGV[0]
[278] Fix | Delete
ARGV.shift
[279] Fix | Delete
Tracer.on
[280] Fix | Delete
require $0
[281] Fix | Delete
else
[282] Fix | Delete
# call Tracer.on only if required by -r command-line option
[283] Fix | Delete
count = caller.count {|bt| %r%/rubygems/core_ext/kernel_require\.rb:% !~ bt}
[284] Fix | Delete
if (defined?(Gem) and count == 0) or
[285] Fix | Delete
(!defined?(Gem) and count <= 1)
[286] Fix | Delete
Tracer.on
[287] Fix | Delete
end
[288] Fix | Delete
end
[289] Fix | Delete
# :startdoc:
[290] Fix | Delete
[291] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function