TraceTree
Print TracePoint(:b_call, :b_return, :c_call, :c_return, :call, :return, :class, :end, :thread_begin, :thread_end) in tree view, to console or html.
Notice: it does not trace :raise, which can be represented by Kernel#raise(:c_call)
Installation
Add this line to your application's Gemfile:
gem 'trace_tree'
And then execute:
$ bundle
Or install it yourself as:
$ gem install trace_tree
Usage
Just call the method you want to trace in the block passed to binding.trace_tree:
things_done = binding.trace_tree do
somebody.do_something
end
Parameters
You may pass optional parameters while invoking binding.trace_tree, for example:
binding.trace_tree(file, color: false, gem: false) do
somebody.do_something
end
file == STDOUTby default. You can give it a File object or anything responds toputs.:color => trueby default. It makes method names have different color than source_location in output. When you print the output to file, you may want to set it false to discard those color ANSI escape sequences.:gem => trueby default. Replace the gem paths in source_location with $GemPathN, which shorten lines. To see what are replaced, inspectTraceTree::GemPaths.:html => nilby default. Set it true to generate a html in which a tree constructed with<ul>,<li>. (No need to setcolor).:tmp => nilby default. Set it true or a string or an array of string to specify a tmp file under the default tmp dir of your system. (No need to providefileargument. It makes parent directories as needed). The tmp dir location can be found withTraceTree.tmp.:htmp => nilby default. It is combination of:htmland:tmp.:return => trueby default. It stores return values of functions in generated html. Hover function call and pressrto print return value in console.:args => falseby default. Set it true to puts arguments of:callfunctions into html. Since arguments are always return values of other functions, so this option is not necessary.:in => //, :out => nilby default. Give them regexp(s) to include/exclude methods defined in files match that regexp(s). Notice thread-calls and methods defined bydefine_methodare always included. Also, once you set any of these two options, the code to trace should not be in same line withbinding.trace_tree() do.:no_methods => nilby default. Give it regexp(s) to hide stack under matching methods. Useful when you want to dump stack of a rack middleware but lower middlewares.:warm => nilby default. Set it something unique to the code block so that the code block will be traced only when it's called second time, in case we dump lots of code loading and initialization.:timer => nilby default. Set it true if you want to know how much time spent in tracing and drawing tree. Notice thefileshould be appendable, otherwise the time will overwrite the tree.:debug => nilby default. Give itSTDOUT/STDERRor anything responds to:putsto output a whole list of TracePoints. Or give it a file name in the default tmp dir of your system.transcode => falseby default. Set it true to convert unknown character into"?"when you seeEncoding::UndefinedConversionError.
Example 1: Output to HTML
Try to remove a non-existing index:
[4] pry(main)> binding.trace_tree(htmp: 'migrate_rm_index'){ ActiveRecord::Migration.new.remove_index "cars", [:online_at] }
-- remove_index("cars", [:online_at])
ArgumentError: Index name 'index_cars_on_online_at' on table 'cars' does not exist
Then find the result HTML in tmp dir. Move your mouse on any method name, and press f/u to fold/unfold it's callee, press p/n to jump to it's previous/next sibling call, press r to print return value in console.
You may type group_by_file() in console, to group callees defined in same file, under additional li tag. Type group_by_file() once again to switch back.

Example 2: Output to STDOUT
Want to know what Sinatra::Base#call does? Wrap it with binding.trace_tree:
require 'sinatra'
require 'trace_tree'
get '/' do
'welcome'
end
class Sinatra::Base
alias_method :o_call, :call
def call env
binding.trace_tree do
o_call env
end
end
end
Execute that sinatra script as normal, then you will see in console: