|
6 | 6 | # simple Ruby idiom for assert: |
7 | 7 | # raise "error msg" unless <condition to assert> |
8 | 8 |
|
| 9 | +# TODO |
| 10 | +# - catch exceptions so that the tracer doesn't crash on an exception |
| 11 | +# - render exception events properly |
| 12 | +# - limit execution to N steps -- use instruction_limit_reached event type |
| 13 | +# - catch the execution of the LAST line in a user's script |
| 14 | + |
| 15 | +# style guide: https://github.com/styleguide/ruby |
| 16 | + |
9 | 17 | require 'json' |
10 | 18 |
|
11 | 19 | script_name = ARGV[0] |
| 20 | +cod = File.open(script_name).read |
| 21 | + |
| 22 | +trace_output_fn = ARGV[1] || "../../../v3/test-trace.js" |
12 | 23 |
|
13 | 24 | cur_trace = [] |
| 25 | +res = {'code' => cod, 'trace' => cur_trace} |
14 | 26 |
|
15 | 27 | pg_tracer = TracePoint.new(:line,:class,:end,:call,:return,:raise,:b_call,:b_return) do |tp| |
16 | | - if tp.path != '(eval)' then next end # 'next' is a 'return' from a block |
| 28 | + next if tp.path != '(eval)' # 'next' is a 'return' from a block |
| 29 | + |
17 | 30 | p [tp.event, tp.lineno, tp.path, tp.defined_class, tp.method_id] |
18 | 31 | # TODO: look into tp.defined_class and tp.method_id attrs |
19 | 32 | # TODO: look into tp.raised_exception and tp.return_value attrs |
| 33 | + |
| 34 | + evt_type = case tp.event |
| 35 | + when :line then "step_line" |
| 36 | + when :call, :b_call, :class then "call" |
| 37 | + when :return, :b_return, :end then "return" |
| 38 | + when :raise then "exception" |
| 39 | + end |
| 40 | + |
| 41 | + ordered_globals = [] |
| 42 | + globals = {} |
| 43 | + stack = [] |
| 44 | + heap = {} |
| 45 | + |
| 46 | + entry = {} |
| 47 | + entry['func_name'] = tp.method_id |
| 48 | + entry['stdout'] = '' # TODO: xxx - look into StringIO |
| 49 | + entry['ordered_globals'] = ordered_globals |
| 50 | + entry['globals'] = globals |
| 51 | + entry['stack_to_render'] = stack # TODO: xxx |
| 52 | + entry['heap'] = heap |
| 53 | + entry['line'] = tp.lineno |
| 54 | + entry['event'] = evt_type |
| 55 | + |
| 56 | + # TODO: see http://ruby-doc.org/core-2.2.2/Thread/Backtrace/Location.html |
| 57 | + caller_locations.each do |c| |
| 58 | + puts c |
| 59 | + end |
| 60 | + puts |
| 61 | + |
| 62 | + cur_trace << entry |
20 | 63 | end |
21 | 64 |
|
22 | 65 | pg_tracer.enable |
23 | | -eval(File.open(script_name).read) # filename is called '(eval)' |
| 66 | +eval(cod) # the filename of the user's code is '(eval)' |
24 | 67 | pg_tracer.disable |
| 68 | + |
| 69 | +#puts JSON.pretty_generate(cur_trace) # pretty-print hack |
| 70 | + |
| 71 | +# postprocessing into a trace |
| 72 | +trace_json = JSON.generate(res) |
| 73 | +File.open(trace_output_fn, 'w') do |f| |
| 74 | + f.write('var trace = ' + trace_json) |
| 75 | +end |
| 76 | + |
| 77 | +puts "Trace written to " + trace_output_fn |
0 commit comments