Skip to content

Commit

Permalink
monkeypatch load_iseq for debugger to work along with bootsnap (#61)
Browse files Browse the repository at this point in the history
* monkey patch InstructionSequence#load_iseq to set tracing flags

* workaround for ruby versions older than 2.5.0
  • Loading branch information
ViugiNick authored and valich committed Jun 18, 2018
1 parent 54f0d8f commit 85493d7
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 6 deletions.
8 changes: 7 additions & 1 deletion ext/attach/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@
require "debase/ruby_core_source"

hdrs = proc {
have_header("vm_core.h")
have_header("vm_core.h") and
have_header("iseq.h") and
have_header("version.h") and
have_header("vm_core.h") and
have_header("vm_insnhelper.h") and
have_header("vm_core.h") and
have_header("method.h")
}

# Allow use customization of compile options. For example, the
Expand Down
31 changes: 31 additions & 0 deletions ext/debase_internals.c
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,36 @@ Debase_enable_file_filtering(VALUE self, VALUE value)
return value;
}

#if RUBY_API_VERSION_CODE >= 20500
static const rb_iseq_t *
my_iseqw_check(VALUE iseqw)
{
rb_iseq_t *iseq = DATA_PTR(iseqw);

if (!iseq->body) {
ibf_load_iseq_complete(iseq);
}

if (!iseq->body->location.label) {
rb_raise(rb_eTypeError, "uninitialized InstructionSequence");
}
return iseq;
}

static void
Debase_set_trace_flag_to_iseq(VALUE self, VALUE rb_iseq)
{
if (!SPECIAL_CONST_P(rb_iseq) && RBASIC_CLASS(rb_iseq) == rb_cISeq) {
rb_iseq_t *iseq = my_iseqw_check(rb_iseq);
rb_iseq_trace_set(iseq, RUBY_EVENT_TRACEPOINT_ALL);
}
}
#else
static void
Debase_set_trace_flag_to_iseq(VALUE self, VALUE rb_iseq) {
}
#endif

static VALUE
Debase_init_variables()
{
Expand Down Expand Up @@ -680,6 +710,7 @@ Init_debase_internals()
rb_define_module_function(mDebase, "enable_trace_points", Debase_enable_trace_points, 0);
rb_define_module_function(mDebase, "prepare_context", Debase_prepare_context, 0);
rb_define_module_function(mDebase, "init_variables", Debase_init_variables, 0);
rb_define_module_function(mDebase, "set_trace_flag_to_iseq", Debase_set_trace_flag_to_iseq, 1);

idAlive = rb_intern("alive?");
idAtLine = rb_intern("at_line");
Expand Down
7 changes: 6 additions & 1 deletion ext/debase_internals.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#ifndef DEBASE_INTERNALS
#define DEBASE_INTERNALS

#include <ruby.h>
#include "ruby.h"
#include "vm_core.h"
#include "version.h"
#include "iseq.h"
#include "vm_insnhelper.h"
#include "method.h"
#include <ruby/debug.h>

typedef struct rb_trace_arg_struct rb_trace_point_t;
Expand Down
9 changes: 7 additions & 2 deletions ext/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@
require "debase/ruby_core_source"

hdrs = proc {
have_header("vm_core.h")
have_header("version.h")
have_header("vm_core.h") and
have_header("iseq.h") and
have_header("version.h") and
have_header("vm_core.h") and
have_header("vm_insnhelper.h") and
have_header("vm_core.h") and
have_header("method.h")
}

# Allow use customization of compile options. For example, the
Expand Down
31 changes: 30 additions & 1 deletion lib/debase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,21 @@ def start(options={}, &block)
Debugger.const_set('ARGV', ARGV.clone) unless defined? Debugger::ARGV
Debugger.const_set('PROG_SCRIPT', $0) unless defined? Debugger::PROG_SCRIPT
Debugger.const_set('INITIAL_DIR', Dir.pwd) unless defined? Debugger::INITIAL_DIR
return Debugger.started? ? block && block.call(self) : Debugger.start_(&block)

monkey_patch_prepend

Debugger.started? ? block && block.call(self) : Debugger.start_(&block)
end

def monkey_patch_prepend
class << RubyVM::InstructionSequence
def self.prepend(mod, *smth)
super
if mod.to_s.include? 'Bootsnap'
prepend InstructionSequenceMixin
end
end
end
end

# @param [String] file
Expand Down Expand Up @@ -81,6 +95,21 @@ def last_context
def file_filter
@file_filter ||= FileFilter.new
end

module InstructionSequenceMixin
def load_iseq(path)
iseq = super(path)

do_set_flags(iseq)

iseq
end

def do_set_flags(iseq)
Debugger.set_trace_flag_to_iseq(iseq)
iseq.each_child{|child_iseq| do_set_flags(child_iseq)}
end
end
end

class FileFilter
Expand Down
2 changes: 1 addition & 1 deletion lib/debase/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Debase
VERSION = "0.2.3.beta1" unless defined? VERSION
VERSION = "0.2.3.beta2" unless defined? VERSION
end

0 comments on commit 85493d7

Please sign in to comment.