Skip to content

Commit 633e218

Browse files
committed
starting to use debug_inspector
1 parent 3d3559e commit 633e218

2 files changed

Lines changed: 53 additions & 10 deletions

File tree

‎v4-cokapi/backends/ruby/notes.txt‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,13 @@ maybe use eval as well?
9696
http://polishinggems.blogspot.com/2011/06/how-to-evaluate-ruby-script-file-from.html
9797

9898
---
99-
2015-07-05
99+
2015-07-06
100100

101-
trying out Rubinius instead of MRI since it contains more detailed
102-
debugging objects for inspecting variable scopes, backtraces, etc.
101+
to get debugger inspector functionality in Ruby 2.x, use:
103102

104-
ugh, can't get installed on Mac, so trying Linux.
103+
gem install debug_inspector
104+
gem install binding_of_caller
105105

106-
rvm install rbx
107-
108-
seems to work on my Linode
106+
Also see here for inspirational example code for getting a pretty backtrace:
107+
https://github.com/ko1/pretty_backtrace/blob/master/lib/pretty_backtrace.rb
109108

‎v4-cokapi/backends/ruby/pg_logger.rb‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
# - render exception events properly
1212
# - limit execution to N steps -- use instruction_limit_reached event type
1313
# - catch the execution of the LAST line in a user's script
14+
#
15+
# Limitation: no support for (lexical) environment pointers, since MRI
16+
# doesn't seem to expose them. We can see only the current (dynamic) stack.
1417

1518
# style guide: https://github.com/styleguide/ruby
1619

1720
require 'json'
21+
require 'debug_inspector' # gem install debug_inspector, use on Ruby 2.X
1822

1923
script_name = ARGV[0]
2024
cod = File.open(script_name).read
@@ -53,10 +57,50 @@
5357
entry['line'] = tp.lineno
5458
entry['event'] = evt_type
5559

56-
# TODO: see http://ruby-doc.org/core-2.2.2/Thread/Backtrace/Location.html
57-
caller_locations.each do |c|
58-
puts c
60+
# adapted from https://github.com/ko1/pretty_backtrace/blob/master/lib/pretty_backtrace.rb
61+
def self.iseq_local_variables iseq
62+
_,_,_,_,arg_info,name,path,a_path,_,type,lvs, * = iseq.to_a
63+
lvs
5964
end
65+
66+
# adapted from https://github.com/ko1/pretty_backtrace/blob/master/lib/pretty_backtrace.rb
67+
RubyVM::DebugInspector.open{|dc|
68+
locs = dc.backtrace_locations
69+
70+
effective_lines = locs.size - 2
71+
72+
pretty_backtrace = locs.map.with_index{|loc, i|
73+
next if i < 2
74+
next loc.to_s unless (effective_lines -= 1) >= 0
75+
76+
iseq = dc.frame_iseq(i)
77+
78+
if iseq
79+
b = dc.frame_binding(i)
80+
lvs = iseq_local_variables(iseq)
81+
lvs_val = lvs.inject({}){|r, lv|
82+
begin
83+
v = b.local_variable_get(lv).inspect
84+
r[lv] = v
85+
rescue NameError
86+
# ignore
87+
end
88+
r
89+
}
90+
else
91+
lvs_val = {}
92+
end
93+
94+
#modify_trace_line loc, loc.absolute_path, loc.lineno, lvs_val
95+
if loc.lineno == 113 # HACKY STENT FOR FILTERING!
96+
puts
97+
else
98+
print loc, ' ', lvs_val
99+
puts
100+
end
101+
}.compact
102+
}
103+
60104
puts
61105

62106
cur_trace << entry

0 commit comments

Comments
 (0)