tracer 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c764c7aa6783d3fb081718acd01a2cac909ea95bc8295206560469658cee5a80
4
- data.tar.gz: 748f9c168bca973deee9b27e6b29b87e26e09c1bf190fdd09e8c38703498c8f6
3
+ metadata.gz: 2384a62a824cb7906cd027700d16844a6b666e5dc7b04749fcb2d69ece9a2a85
4
+ data.tar.gz: 762568e03d81d9dafa2ad6735f9901d53507aaeaa350aac53ef3989325136757
5
5
  SHA512:
6
- metadata.gz: 0ce73f5cfcdaa82595d3abead931fa0d400025f87f7bf3e6f46bdc40856d9310b573f453c6096c40f0d47e326542801f4be849be9643bb877d03a0cfb5774abb
7
- data.tar.gz: fe2b4c36c2b4ea19421fea09732dbc8c70323d7957fe9122d364f63c8cd453e0adb9b806856fed5f537994187c5f5df382623e244963ccb2f57da670a1dc5b92
6
+ metadata.gz: bbdccfb4cf5054d554a76e22f9bbffd21aa8f3e9a13e366cbfb41d076803c4de4b019697b81d8dd1278655390f7c1949190e7d7c72b2fcdc7ffa83622b89a71d
7
+ data.tar.gz: b0a8142e250b24964d9fbcbc674d052107061725b6673b7d573553faf55afc722559396c0b878f3639c051dcfd1a9794e285116ba38e2f493d4be9b8d17c11ae
data/README.md CHANGED
@@ -8,6 +8,7 @@ The `tracer` gem provides helpful tracing utilities to help users observe their
8
8
  The currently supported tracers are:
9
9
 
10
10
  - [`ObjectTracer`](#objecttracer)
11
+ - [`IvarTracer`](#ivartracer)
11
12
  - [`CallTracer`](#calltracer)
12
13
  - [`ExceptionTracer`](#exceptiontracer)
13
14
  - [`LineTracer`](#linetracer)
@@ -103,6 +104,30 @@ end
103
104
  #depth:4 #<User:0x000000010696cad8 @name="John"> receives #name (User#name) at test.rb:8:in `authorized?'
104
105
  ```
105
106
 
107
+ #### IvarTracer
108
+
109
+ > **Note**
110
+ > Ruby 3.0 and below's accessor calls don't trigger TracePoint properly so the result may be inaccurate with those versions.
111
+
112
+ ```rb
113
+ require "tracer"
114
+
115
+ class Cat
116
+ attr_accessor :name
117
+ end
118
+
119
+ cat = Cat.new
120
+
121
+ tracer = IvarTracer.new(cat, :@name)
122
+ tracer.start do
123
+ cat.name = "Kitty"
124
+ cat.instance_variable_set(:@name, "Ketty")
125
+ end
126
+
127
+ #depth:3 Cat#name= sets @name = "Kitty" at test.rb:11
128
+ #depth:3 Kernel#instance_variable_set sets @name = "Ketty" at test.rb:12
129
+ ```
130
+
106
131
  #### ExceptionTracer
107
132
 
108
133
  ```rb
@@ -114,7 +139,8 @@ rescue StandardError
114
139
  nil
115
140
  end
116
141
 
117
- #depth:1 #<RuntimeError: boom> at test.rb:4
142
+ #depth:0 #<RuntimeError: boom> raised at test.rb:4
143
+ #depth:1 #<RuntimeError: boom> rescued at test.rb:6
118
144
  ```
119
145
 
120
146
  #### CallTracer
data/lib/tracer/base.rb CHANGED
@@ -10,6 +10,7 @@ module Tracer
10
10
  M_INSPECT = Object.instance_method(:inspect)
11
11
  M_CLASS = Object.instance_method(:class)
12
12
  M_IS_A = Object.instance_method(:is_a?)
13
+ M_INSTANCE_VARIABLE_GET = Object.instance_method(:instance_variable_get)
13
14
  HOME = ENV["HOME"] ? (ENV["HOME"] + "/") : nil
14
15
 
15
16
  include Color
@@ -4,16 +4,32 @@ require_relative "base"
4
4
 
5
5
  class ExceptionTracer < Tracer::Base
6
6
  def setup_tp
7
- TracePoint.new(:raise) do |tp|
8
- next if skip?(tp)
7
+ if RUBY_VERSION >= "3.3.0"
8
+ TracePoint.new(:raise, :rescue) do |tp|
9
+ next if skip?(tp)
9
10
 
10
- exc = tp.raised_exception
11
+ exc = tp.raised_exception
11
12
 
12
- out tp,
13
- " #{colorize_magenta(exc.inspect)}",
14
- depth: caller.size - (1 + @depth_offset)
15
- rescue Exception => e
16
- p e
13
+ action = tp.event == :raise ? "raised" : "rescued"
14
+
15
+ out tp,
16
+ " #{colorize_magenta(exc.inspect)} #{action}",
17
+ depth: caller.size - (1 + @depth_offset)
18
+ rescue Exception => e
19
+ p e
20
+ end
21
+ else
22
+ TracePoint.new(:raise) do |tp|
23
+ next if skip?(tp)
24
+
25
+ exc = tp.raised_exception
26
+
27
+ out tp,
28
+ " #{colorize_magenta(exc.inspect)} raised",
29
+ depth: caller.size - (1 + @depth_offset)
30
+ rescue Exception => e
31
+ p e
32
+ end
17
33
  end
18
34
  end
19
35
 
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+
5
+ class IvarTracer < Tracer::Base
6
+ def initialize(target, var_name, **kw)
7
+ @target = target
8
+ @var_name = var_name
9
+ @original_value = M_INSTANCE_VARIABLE_GET.bind_call(target, var_name)
10
+ super(**kw)
11
+ end
12
+
13
+ def key
14
+ [@type, @target, @var_name, @pattern, @into].freeze
15
+ end
16
+
17
+ def description
18
+ "for #{@var_name} of #{@target} #{super}"
19
+ end
20
+
21
+ def setup_tp
22
+ TracePoint.new(:a_return) do |tp|
23
+ next if skip?(tp)
24
+
25
+ if tp.self == @target &&
26
+ value = M_INSTANCE_VARIABLE_GET.bind_call(@target, @var_name)
27
+ if tp.event == :c_return
28
+ location = nil
29
+ else
30
+ location = caller_locations(2, 1).first.to_s
31
+ next if location.match?(DIR) || location.match?(/<internal:/)
32
+ end
33
+
34
+ depth = caller.size
35
+ call_identifier_str = (tp.defined_class ? minfo(tp) : "block")
36
+ call_identifier_str = colorize_blue(call_identifier_str)
37
+ depth += 1 if tp.event == :c_return
38
+ value = safe_inspect(value)
39
+
40
+ if value != @original_value
41
+ out tp,
42
+ "#{call_identifier_str} sets #{colorize_cyan(@var_name)} = #{colorize_magenta(value)}",
43
+ depth: depth - 2 - @depth_offset,
44
+ location: location
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tracer
4
- VERSION = "0.2.2"
4
+ VERSION = "0.2.3"
5
5
  end
data/lib/tracer.rb CHANGED
@@ -5,6 +5,7 @@ require_relative "tracer/line_tracer"
5
5
  require_relative "tracer/call_tracer"
6
6
  require_relative "tracer/exception_tracer"
7
7
  require_relative "tracer/object_tracer"
8
+ require_relative "tracer/ivar_tracer"
8
9
 
9
10
  module Tracer
10
11
  module Helper
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tracer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stan Lo
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2023-05-28 00:00:00.000000000 Z
12
+ date: 2024-03-22 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A Ruby tracer
15
15
  email:
@@ -21,11 +21,8 @@ extra_rdoc_files: []
21
21
  files:
22
22
  - CHANGELOG.md
23
23
  - CODE_OF_CONDUCT.md
24
- - Gemfile
25
- - Gemfile.lock
26
24
  - LICENSE.txt
27
25
  - README.md
28
- - Rakefile
29
26
  - lib/tracer.rb
30
27
  - lib/tracer/base.rb
31
28
  - lib/tracer/call_tracer.rb
@@ -33,10 +30,10 @@ files:
33
30
  - lib/tracer/exception_tracer.rb
34
31
  - lib/tracer/helper.rb
35
32
  - lib/tracer/irb.rb
33
+ - lib/tracer/ivar_tracer.rb
36
34
  - lib/tracer/line_tracer.rb
37
35
  - lib/tracer/object_tracer.rb
38
36
  - lib/tracer/version.rb
39
- - tracer.gemspec
40
37
  homepage: https://github.com/ruby/tracer
41
38
  licenses:
42
39
  - Ruby
@@ -60,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
57
  - !ruby/object:Gem::Version
61
58
  version: '0'
62
59
  requirements: []
63
- rubygems_version: 3.4.10
60
+ rubygems_version: 3.5.4
64
61
  signing_key:
65
62
  specification_version: 4
66
63
  summary: A Ruby tracer
data/Gemfile DELETED
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source "https://rubygems.org"
4
-
5
- # Specify your gem's dependencies in ruby-tracer.gemspec
6
- gemspec
7
-
8
- gem "rake", "~> 13.0"
9
- gem "irb"
10
-
11
- gem "test-unit", "~> 3.0"
data/Gemfile.lock DELETED
@@ -1,31 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- tracer (0.2.2)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- io-console (0.6.0)
10
- irb (1.6.2)
11
- reline (>= 0.3.0)
12
- power_assert (2.0.2)
13
- rake (13.0.6)
14
- reline (0.3.2)
15
- io-console (~> 0.5)
16
- test-unit (3.5.5)
17
- power_assert
18
-
19
- PLATFORMS
20
- arm64-darwin-21
21
- arm64-darwin-22
22
- x86_64-linux
23
-
24
- DEPENDENCIES
25
- irb
26
- rake (~> 13.0)
27
- test-unit (~> 3.0)
28
- tracer!
29
-
30
- BUNDLED WITH
31
- 2.4.2
data/Rakefile DELETED
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
- require "rake/testtask"
5
-
6
- Rake::TestTask.new(:test) do |t|
7
- t.libs << "test"
8
- t.libs << "lib"
9
- t.test_files = FileList["test/**/*_test.rb"]
10
- end
11
-
12
- task default: :test
data/tracer.gemspec DELETED
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/tracer/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "tracer"
7
- spec.version = Tracer::VERSION
8
- spec.authors = ["Stan Lo", "Keiju ISHITSUKA"]
9
- spec.email = %w[stan001212@gmail.com keiju@ruby-lang.org]
10
-
11
- spec.summary = "A Ruby tracer"
12
- spec.description = "A Ruby tracer"
13
- spec.homepage = "https://github.com/ruby/tracer"
14
- spec.licenses = %w[Ruby BSD-2-Clause]
15
- spec.required_ruby_version = ">= 2.7.0"
16
-
17
- spec.metadata["homepage_uri"] = spec.homepage
18
- spec.metadata["source_code_uri"] = "https://github.com/ruby/tracer"
19
- spec.metadata[
20
- "changelog_uri"
21
- ] = "https://github.com/ruby/tracer/blob/main/CHANGELOG.md"
22
-
23
- # Specify which files should be added to the gem when it is released.
24
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
- spec.files =
26
- Dir.chdir(__dir__) do
27
- `git ls-files -z`.split("\x0")
28
- .reject do |f|
29
- (f == __FILE__) ||
30
- f.match(
31
- %r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)}
32
- )
33
- end
34
- end
35
- spec.bindir = "exe"
36
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
37
- spec.require_paths = ["lib"]
38
-
39
- # Uncomment to register a new dependency of your gem
40
-
41
- # For more information and examples about making a new gem, check out our
42
- # guide at: https://bundler.io/guides/creating_gem.html
43
- end