Git Product home page Git Product logo

tracer's Introduction

Tracer

Ruby Gem Version

The tracer gem provides helpful tracing utilities to help users observe their program's runtime behaviour.

The currently supported tracers are:

It also comes with experimental IRB integration to allow quick access from REPL.

Installation

$ bundle add tracer --group=development,test

Or directly add it to your Gemfile

group :development, :test do
  gem "tracer"
end

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install tracer

Usage

Tracer.trace(object) { ... } # trace object's activities in the given block
Tracer.trace_call { ... } # trace method calls in the given block
Tracer.trace_exception { ... } # trace exceptions in the given block

Example

require "tracer"

obj = Object.new

def obj.foo
  100
end

def bar(obj)
  obj.foo
end

Tracer.trace(obj) { bar(obj) }
 #depth:1  #<Object:0x000000010903c190> is used as a parameter obj of Object#bar at test.rb:13:in `block in <main>'
 #depth:2  #<Object:0x000000010903c190> receives .foo at test.rb:10:in `bar'

tracer/helper

If you want to avoid the Tracer namespace, you can do require "tracer/helper" instead:

require "tracer/helper"

trace(object) { ... } # trace object's activities in the given block
trace_call { ... } # trace method calls in the given block
trace_exception { ... } # trace exceptions in the given block

Tracer Classes

If you want to have more control over individual traces, you can use individual tracer classes:

ObjectTracer

class User
  def initialize(name) = (@name = name)

  def name() = @name
end

def authorized?(user)
  user.name == "John"
end

user = User.new("John")
tracer = ObjectTracer.new(user)
tracer.start do
  user.name
  authorized?(user)
end

 #depth:3  #<User:0x000000010696cad8 @name="John"> receives #name (User#name) at test.rb:14:in `block in <main>'
 #depth:3  #<User:0x000000010696cad8 @name="John"> is used as a parameter user of Object#authorized? at test.rb:15:in `block in <main>'
 #depth:4  #<User:0x000000010696cad8 @name="John"> receives #name (User#name) at test.rb:8:in `authorized?'

IvarTracer

Note

Ruby 3.0 and below's accessor calls don't trigger TracePoint properly so the result may be inaccurate with those versions.

require "tracer"

class Cat
  attr_accessor :name
end

cat = Cat.new

tracer = IvarTracer.new(cat, :@name)
tracer.start do
  cat.name = "Kitty"
  cat.instance_variable_set(:@name, "Ketty")
end

#depth:3 Cat#name= sets @name = "Kitty" at test.rb:11
#depth:3 Kernel#instance_variable_set sets @name = "Ketty" at test.rb:12

ExceptionTracer

ExceptionTracer.new.start

begin
  raise "boom"
rescue StandardError
  nil
end

#depth:0  #<RuntimeError: boom> raised at test.rb:4
#depth:1  #<RuntimeError: boom> rescued at test.rb:6

CallTracer

class User
  def initialize(name) = (@name = name)

  def name() = @name
end

def authorized?(user)
  user.name == "John"
end

user = User.new("John")
tracer = CallTracer.new
tracer.start do
  user.name
  authorized?(user)
end

 #depth:4 >    block at test.rb:13
 #depth:5 >     User#name at test.rb:4
 #depth:5 <     User#name #=> "John" at test.rb:4
 #depth:5 >     Object#authorized? at test.rb:7
 #depth:6 >      User#name at test.rb:4
 #depth:6 <      User#name #=> "John" at test.rb:4
 #depth:6 >      String#== at test.rb:8
 #depth:6 <      String#== #=> true at test.rb:8
 #depth:5 <     Object#authorized? #=> true at test.rb:9
 #depth:4 <    block #=> true at test.rb:16

LineTracer

class User
  def initialize(name) = (@name = name)

  def name() = @name
end

def authorized?(user)
  user.name == "John"
end

user = User.new("John")
tracer = LineTracer.new
tracer.start do
  user.name
  authorized?(user)
end

 #depth:4  at test.rb:14
 #depth:4  at test.rb:15
 #depth:5  at test.rb:8

IRB-integration

Once required, tracer registers a few IRB commands to help you trace Ruby expressions:

trace              Trace the target object (or self) in the given expression. Usage: `trace [target,] <expression>`
trace_call         Trace method calls in the given expression. Usage: `trace_call <expression>`
trace_exception    Trace exceptions in the given expression. Usage: `trace_exception <expression>`

Example

# test.rb
require "tracer"

obj = Object.new

def obj.foo
  100
end

def bar(obj)
  obj.foo
end

binding.irb
irb(main):001:0> trace obj, bar(obj)
 #depth:23 #<Object:0x0000000107a86648> is used as a parameter obj of Object#bar at (eval):1:in `<main>'
 #depth:24 #<Object:0x0000000107a86648> receives .foo at test.rb:10:in `bar'
=> 100
irb(main):002:0> trace_call bar(obj)
 #depth:23>                            Object#bar at (eval):1:in `<main>'
 #depth:24>                             #<Object:0x0000000107a86648>.foo at test.rb:10:in `bar'
 #depth:24<                             #<Object:0x0000000107a86648>.foo #=> 100 at test.rb:10:in `bar'
 #depth:23<                            Object#bar #=> 100 at (eval):1:in `<main>'
=> 100

Customization

TBD

Acknowledgements

A big shout-out to @ko1 (Koichi Sasada) for his awesome work on ruby/debug. The tracers in ruby/debug were an inspiration and laid the groundwork for this project.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test-unit to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/tracer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the 2-Clause BSD License.

Code of Conduct

Everyone interacting in the Ruby::Tracer project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

tracer's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tracer's Issues

tracer 0.1.0 is included with ruby but doesn't exist on rubygems.org

On a fresh ruby 2.6.1 installation, we can see tracer 0.1.0 is a default gem

$ gem list tracer

*** LOCAL GEMS ***

tracer (default: 0.1.0)

However, this version does not exist on rubygems.org

gem search ^tracer$ --all

*** REMOTE GEMS ***

tracer (0.0.1)

tracer 0.1.0 should probably be pushed to rubygems.org.

Same thing happens with the prime gem, but I didn't report it because issues are closed on https://github.com/ruby/prime.

Extend/replace implementation with ruby/debug's tracers?

ruby/debug has 4 types of tracers:

  • LineTracer
  • CallTracer
  • ExceptionTracer
  • ObjectTracer

All of them are super powerful and the implementation is mostly debugger-independent. With a few tweaks they can be completely extracted. So I'm thinking maybe we can move those tracers here so users can use it without the debugger? With API like

Tracer.trace_line # LineTracer
Tracer.trace_call # CallTracer
Tracer.trace_exception # ExceptionTracer
Tracer.trace_object(obj) # ObjectTracer

And we can support 2 ways to turn it on/off

tracer = Tracer.trace_line
# code
tracer.off

# or

Tracer.trace_line do
  # code
end

And the debugger can depend on this gem, similar to it depends on irb and reline.

Benefits

  • Users can use those tracers without the debugger
  • Users can extend/build their tracers based on the standardized tracer APIs
  • Other debugging tools can also require it
  • irb --tracer can be more powerful

We need to think about coloring more carefully though. Ideally, the updated tracer should support out-of-box coloring. But it means we need to replicate the irb's coloring logic because we can't depend on irb. Perhaps extracting that to a colorable utility gem will be better in the long-term?

Wdyt? @ko1 @hsbt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.