ruby_tracer 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ed2853558a9d7a761617ee215d7f46c9b146cdb4fcb431564052f0330d84069a
4
- data.tar.gz: ad54ea0f5958215ea2271f782354539cf74907af3a53bb16cc6ce7412007eceb
3
+ metadata.gz: 0ac708e995302930fed59fa3b9396dc5b3c45ab76d4306c8bbac65fe69c49377
4
+ data.tar.gz: 99f23f4e5fb2d5f8ae694ff1203524707c757fd5a91cd29131ac61e79c5cb15f
5
5
  SHA512:
6
- metadata.gz: e38ef5d999c9a01a32148cc6f5709fac2b6d19430ab345823dfa7896ec37a6349f759552623ae47e6c12d61dabddde71cc9647d1ffd3213d2457bacbdff90363
7
- data.tar.gz: ae263df3f94d33ae886e4d1966fc06265349839f05b9fd8538e48f81e3b7d9cb4ab7c84ae0c8299567f031480d0d53c03d645e2e97351fc7f41b64c3ad14de58
6
+ metadata.gz: dd1e5c1aef7115227db143c95939ad7ce180c65924334f30540dbed8f9d2f5cd468f0e3e989af1e7d6dd4e49e023798d5a1133c26cf49ccef54080eceb6f925c
7
+ data.tar.gz: 0dd0e49cf41d3e9fe4fd880b68364f1727d4d29bb4fb8c35b76e37ed97549979130d2678f2f2a0c720282ad17bd0abb15b0694478dea24b40dc57baf452b434b
data/Gemfile CHANGED
@@ -6,6 +6,7 @@ source "https://rubygems.org"
6
6
  gemspec
7
7
 
8
8
  gem "rake", "~> 13.0"
9
+ gem "irb"
9
10
 
10
11
  gem "test-unit", "~> 3.0"
11
12
 
data/Gemfile.lock CHANGED
@@ -1,15 +1,20 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby_tracer (0.2.0)
4
+ ruby_tracer (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ io-console (0.6.0)
10
+ irb (1.6.2)
11
+ reline (>= 0.3.0)
9
12
  language_server-protocol (3.17.0.3)
10
13
  power_assert (2.0.2)
11
14
  prettier_print (1.2.0)
12
15
  rake (13.0.6)
16
+ reline (0.3.2)
17
+ io-console (~> 0.5)
13
18
  ruby-lsp (0.3.8)
14
19
  language_server-protocol (~> 3.17.0)
15
20
  sorbet-runtime
@@ -25,6 +30,7 @@ PLATFORMS
25
30
  x86_64-linux
26
31
 
27
32
  DEPENDENCIES
33
+ irb
28
34
  rake (~> 13.0)
29
35
  ruby-lsp
30
36
  ruby_tracer!
data/README.md CHANGED
@@ -1,14 +1,6 @@
1
1
  # ruby_tracer
2
2
 
3
- ruby_tracer is an extraction of [`ruby/debug`](https://github.com/ruby/debug)'s [powerful tracers](https://github.com/ruby/debug/blob/master/lib/debug/tracer.rb), with user-facing APIs and some improvements on accuracy.
4
-
5
- Its goal is to help users understand their Ruby programss activities by emitting useful trace information, such us:
6
-
7
- - How and where is the target object is being used (`ObjectTracer`)
8
- - What exceptions are raised during the execution (`ExceptionTracer`)
9
- - When method calls are being performed (`CallTracer`)
10
- - Line execution (`LineTracer`)
11
-
3
+ ruby_tracer is an extraction of [`ruby/debug`](https://github.com/ruby/debug)'s [powerful tracers](https://github.com/ruby/debug/blob/master/lib/debug/tracer.rb), with user-facing APIs, IRB-integration, and improvements on accuracy.
12
4
 
13
5
  ## Installation
14
6
 
@@ -62,9 +54,54 @@ trace_call { ... } # trace method calls in the given block
62
54
  trace_exception { ... } # trace exceptions in the given block
63
55
  ```
64
56
 
57
+ ### IRB-integration
58
+
59
+ Once required, `ruby_tracer` registers a few IRB commands to help you trace Ruby expressions:
60
+
61
+ ```
62
+ trace Trace the target object (or self) in the given expression. Usage: `trace [target,] <expression>`
63
+ trace_call Trace method calls in the given expression. Usage: `trace_call <expression>`
64
+ trace_exception Trace exceptions in the given expression. Usage: `trace_exception <expression>`
65
+ ```
66
+
67
+ **Example**
68
+
69
+ ```rb
70
+ # test.rb
71
+ require "ruby_tracer"
72
+
73
+ obj = Object.new
74
+
75
+ def obj.foo
76
+ 100
77
+ end
78
+
79
+ def bar(obj)
80
+ obj.foo
81
+ end
82
+
83
+ binding.irb
84
+ ```
85
+
86
+
87
+ ```
88
+ irb(main):001:0> trace obj, bar(obj)
89
+ #depth:23 #<Object:0x0000000107a86648> is used as a parameter obj of Object#bar at (eval):1:in `<main>'
90
+ #depth:24 #<Object:0x0000000107a86648> receives .foo at test.rb:10:in `bar'
91
+ => 100
92
+ irb(main):002:0> trace_call bar(obj)
93
+ #depth:23> Object#bar at (eval):1:in `<main>'
94
+ #depth:24> #<Object:0x0000000107a86648>.foo at test.rb:10:in `bar'
95
+ #depth:24< #<Object:0x0000000107a86648>.foo #=> 100 at test.rb:10:in `bar'
96
+ #depth:23< Object#bar #=> 100 at (eval):1:in `<main>'
97
+ => 100
98
+ ```
99
+
100
+ ### Tracer Classes
101
+
65
102
  If you want to have more control over individual traces, you can use individual tracer classes:
66
103
 
67
- ### ObjectTracer
104
+ #### ObjectTracer
68
105
 
69
106
  ```rb
70
107
  class User
@@ -89,7 +126,7 @@ end
89
126
  #depth:4 #<User:0x000000010696cad8 @name="John"> receives #name (User#name) at test.rb:8:in `authorized?'
90
127
  ```
91
128
 
92
- ### ExceptionTracer
129
+ #### ExceptionTracer
93
130
 
94
131
  ```rb
95
132
  ExceptionTracer.new.start
@@ -103,7 +140,7 @@ end
103
140
  #depth:1 #<RuntimeError: boom> at test.rb:4
104
141
  ```
105
142
 
106
- ### CallTracer
143
+ #### CallTracer
107
144
 
108
145
  ```rb
109
146
  class User
@@ -135,7 +172,7 @@ end
135
172
  #depth:4 < block #=> true at test.rb:16
136
173
  ```
137
174
 
138
- ### LineTracer
175
+ #### LineTracer
139
176
 
140
177
  ```rb
141
178
  class User
@@ -7,8 +7,8 @@ class CallTracer < Tracer::Base
7
7
  TracePoint.new(:a_call, :a_return) do |tp|
8
8
  next if skip?(tp)
9
9
 
10
- location = caller_locations(2, 1).first
11
- next if location.to_s.match?(/<internal:/)
10
+ location = caller_locations(2, 1).first.to_s
11
+ next if location.match?(DIR) || location.match?(/<internal:/)
12
12
 
13
13
  depth = caller.size
14
14
 
@@ -0,0 +1,90 @@
1
+ require "irb/cmd/nop"
2
+ require "irb"
3
+
4
+ module Tracer
5
+ def self.register_irb_commands
6
+ ec = IRB::ExtendCommandBundle.instance_variable_get(:@EXTEND_COMMANDS)
7
+
8
+ [
9
+ [:trace, :Trace, nil, [:trace, IRB::ExtendCommandBundle::OVERRIDE_ALL]],
10
+ [
11
+ :trace_call,
12
+ :TraceCall,
13
+ nil,
14
+ [:trace_call, IRB::ExtendCommandBundle::OVERRIDE_ALL]
15
+ ],
16
+ [
17
+ :trace_exception,
18
+ :TraceException,
19
+ nil,
20
+ [:trace_exception, IRB::ExtendCommandBundle::OVERRIDE_ALL]
21
+ ]
22
+ ].each do |ecconfig|
23
+ ec.push(ecconfig)
24
+ IRB::ExtendCommandBundle.def_extend_command(*ecconfig)
25
+ end
26
+ end
27
+ end
28
+
29
+ module IRB
30
+ module ExtendCommand
31
+ class TraceCommand < Nop
32
+ class << self
33
+ def transform_args(args)
34
+ # Return a string literal as is for backward compatibility
35
+ if args.empty? || string_literal?(args)
36
+ args
37
+ else # Otherwise, consider the input as a String for convenience
38
+ args.strip.dump
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ class Trace < TraceCommand
45
+ category "Tracing"
46
+ description "Trace the target object (or self) in the given expression. Usage: `trace [target,] <expression>`"
47
+
48
+ def execute(*args)
49
+ args = args.first.split(/,/, 2)
50
+
51
+ case args.size
52
+ when 1
53
+ target = irb_context.workspace.main
54
+ expression = args.first
55
+ when 2
56
+ target = eval(args.first, irb_context.workspace.binding)
57
+ expression = args.last
58
+ else
59
+ raise ArgumentError,
60
+ "wrong number of arguments (given #{args.size}, expected 1..2)"
61
+ end
62
+
63
+ b = irb_context.workspace.binding
64
+ Tracer.trace(target) { eval(expression, b) }
65
+ end
66
+ end
67
+
68
+ class TraceCall < TraceCommand
69
+ category "Tracing"
70
+ description "Trace method calls in the given expression. Usage: `trace_call <expression>`"
71
+
72
+ def execute(expression)
73
+ b = irb_context.workspace.binding
74
+ Tracer.trace_call { eval(expression, b) }
75
+ end
76
+ end
77
+
78
+ class TraceException < TraceCommand
79
+ category "Tracing"
80
+ description "Trace exceptions in the given expression. Usage: `trace_exception <expression>`"
81
+
82
+ def execute(expression)
83
+ b = irb_context.workspace.binding
84
+ Tracer.trace_exception { eval(expression, b) }
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ Tracer.register_irb_commands
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tracer
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/ruby_tracer.rb CHANGED
@@ -28,3 +28,5 @@ module Tracer
28
28
 
29
29
  extend Helper
30
30
  end
31
+
32
+ require_relative "ruby_tracer/irb"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_tracer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stan Lo
@@ -30,6 +30,7 @@ files:
30
30
  - lib/ruby_tracer/color.rb
31
31
  - lib/ruby_tracer/exception_tracer.rb
32
32
  - lib/ruby_tracer/helper.rb
33
+ - lib/ruby_tracer/irb.rb
33
34
  - lib/ruby_tracer/line_tracer.rb
34
35
  - lib/ruby_tracer/object_tracer.rb
35
36
  - lib/ruby_tracer/version.rb