Skip to content

Commit 3eb97c9

Browse files
committed
bam
1 parent 37043bd commit 3eb97c9

1 file changed

Lines changed: 55 additions & 2 deletions

File tree

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

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,72 @@
66
# simple Ruby idiom for assert:
77
# raise "error msg" unless <condition to assert>
88

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+
917
require 'json'
1018

1119
script_name = ARGV[0]
20+
cod = File.open(script_name).read
21+
22+
trace_output_fn = ARGV[1] || "../../../v3/test-trace.js"
1223

1324
cur_trace = []
25+
res = {'code' => cod, 'trace' => cur_trace}
1426

1527
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+
1730
p [tp.event, tp.lineno, tp.path, tp.defined_class, tp.method_id]
1831
# TODO: look into tp.defined_class and tp.method_id attrs
1932
# 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
2063
end
2164

2265
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)'
2467
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

Comments
 (0)