|
11 | 11 | # - render exception events properly |
12 | 12 | # - limit execution to N steps -- use instruction_limit_reached event type |
13 | 13 | # - 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. |
14 | 17 |
|
15 | 18 | # style guide: https://github.com/styleguide/ruby |
16 | 19 |
|
17 | 20 | require 'json' |
| 21 | +require 'debug_inspector' # gem install debug_inspector, use on Ruby 2.X |
18 | 22 |
|
19 | 23 | script_name = ARGV[0] |
20 | 24 | cod = File.open(script_name).read |
|
53 | 57 | entry['line'] = tp.lineno |
54 | 58 | entry['event'] = evt_type |
55 | 59 |
|
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 |
59 | 64 | 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 | + |
60 | 104 | puts |
61 | 105 |
|
62 | 106 | cur_trace << entry |
|
0 commit comments