Skip to content

Commit eccbd57

Browse files
viuginick1valich
authored andcommitted
Revert "revert old Bootsnap hack"
This reverts commit 5ece023
1 parent 119382b commit eccbd57

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

ext/debase_internals.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,47 @@ Debase_enable_file_filtering(VALUE self, VALUE value)
637637
return value;
638638
}
639639

640+
#if RUBY_API_VERSION_CODE >= 20500 && !(RUBY_RELEASE_YEAR == 2017 && RUBY_RELEASE_MONTH == 10 && RUBY_RELEASE_DAY == 10)
641+
static const rb_iseq_t *
642+
my_iseqw_check(VALUE iseqw)
643+
{
644+
rb_iseq_t *iseq = DATA_PTR(iseqw);
645+
646+
if (!iseq->body) {
647+
ibf_load_iseq_complete(iseq);
648+
}
649+
650+
if (!iseq->body->location.label) {
651+
rb_raise(rb_eTypeError, "uninitialized InstructionSequence");
652+
}
653+
return iseq;
654+
}
655+
656+
static void
657+
Debase_set_trace_flag_to_iseq(VALUE self, VALUE rb_iseq) {
658+
if (!SPECIAL_CONST_P(rb_iseq) && RBASIC_CLASS(rb_iseq) == rb_cISeq) {
659+
rb_iseq_t *iseq = my_iseqw_check(rb_iseq);
660+
rb_iseq_trace_set(iseq, RUBY_EVENT_TRACEPOINT_ALL);
661+
}
662+
}
663+
664+
static void
665+
Debase_unset_trace_flags(VALUE self, VALUE rb_iseq) {
666+
if (!SPECIAL_CONST_P(rb_iseq) && RBASIC_CLASS(rb_iseq) == rb_cISeq) {
667+
rb_iseq_t *iseq = my_iseqw_check(rb_iseq);
668+
rb_iseq_trace_set(iseq, RUBY_EVENT_NONE);
669+
}
670+
}
671+
#else
672+
static void
673+
Debase_set_trace_flag_to_iseq(VALUE self, VALUE rb_iseq) {
674+
}
675+
676+
static void
677+
Debase_unset_trace_flags(VALUE self, VALUE rb_iseq) {
678+
}
679+
#endif
680+
640681
static VALUE
641682
Debase_init_variables()
642683
{
@@ -680,6 +721,10 @@ Init_debase_internals()
680721
rb_define_module_function(mDebase, "enable_trace_points", Debase_enable_trace_points, 0);
681722
rb_define_module_function(mDebase, "prepare_context", Debase_prepare_context, 0);
682723
rb_define_module_function(mDebase, "init_variables", Debase_init_variables, 0);
724+
rb_define_module_function(mDebase, "set_trace_flag_to_iseq", Debase_set_trace_flag_to_iseq, 1);
725+
726+
//use only for tests
727+
rb_define_module_function(mDebase, "unset_iseq_flags", Debase_unset_trace_flags, 1);
683728

684729
idAlive = rb_intern("alive?");
685730
idAtLine = rb_intern("at_line");

lib/debase.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,22 @@ def start(options={}, &block)
2121
Debugger.const_set('PROG_SCRIPT', $0) unless defined? Debugger::PROG_SCRIPT
2222
Debugger.const_set('INITIAL_DIR', Dir.pwd) unless defined? Debugger::INITIAL_DIR
2323

24+
monkey_patch_prepend
25+
2426
Debugger.started? ? block && block.call(self) : Debugger.start_(&block)
2527
end
2628

29+
def monkey_patch_prepend
30+
class << RubyVM::InstructionSequence
31+
def self.prepend(mod, *smth)
32+
super
33+
if mod.to_s.include?('Bootsnap') && RUBY_VERSION >= "2.5"
34+
prepend InstructionSequenceMixin
35+
end
36+
end
37+
end
38+
end
39+
2740
# @param [String] file
2841
# @param [Fixnum] line
2942
# @param [String] expr
@@ -82,6 +95,21 @@ def last_context
8295
def file_filter
8396
@file_filter ||= FileFilter.new
8497
end
98+
99+
module InstructionSequenceMixin
100+
def load_iseq(path)
101+
iseq = super(path)
102+
103+
do_set_flags(iseq)
104+
105+
iseq
106+
end
107+
108+
def do_set_flags(iseq)
109+
Debugger.set_trace_flag_to_iseq(iseq)
110+
iseq.each_child { |child_iseq| do_set_flags(child_iseq) } if iseq.respond_to? :each_child
111+
end
112+
end
85113
end
86114

87115
class FileFilter

test/test_load.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,35 @@ def test_debug_load
4444
ensure
4545
Debugger.stop if Debugger.started?
4646
end
47+
48+
module MyBootsnap
49+
def load_iseq(path)
50+
iseq = RubyVM::InstructionSequence.compile_file(path)
51+
52+
Debugger.unset_iseq_flags(iseq)
53+
iseq
54+
end
55+
end
56+
57+
def test_bootsnap
58+
@@at_line = nil
59+
src_dir = File.dirname(__FILE__)
60+
prog_script = File.join(src_dir, 'example', 'bootsnap', 'bootsnap.rb')
61+
62+
class << RubyVM::InstructionSequence
63+
prepend MyBootsnap
64+
end
65+
bt = Debugger.debug_load(prog_script, true)
66+
assert_equal(nil, bt)
67+
assert_not_nil(@@at_line)
68+
if RUBY_VERSION >= '2.5'
69+
assert_equal(['debase.rb', 101], @@at_line)
70+
end
71+
72+
assert(Debugger.started?)
73+
Debugger.stop
74+
75+
class << RubyVM::InstructionSequence; self end.class_eval { undef_method :load_iseq }
76+
77+
end
4778
end

0 commit comments

Comments
 (0)