Skip to content

Commit

Permalink
Make it possible to get AST::Node from Thread::Backtrace::Location
Browse files Browse the repository at this point in the history
RubyVM::AST.of(Thread::Backtrace::Location) returns a node that
corresponds to the location. Typically, the node is a method call, but
not always.

This change also includes iseq's dump/load support of node_ids for each
instructions.
  • Loading branch information
mame committed Jun 17, 2021
1 parent ea60628 commit dfba87c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 21 deletions.
27 changes: 15 additions & 12 deletions ast.c
Expand Up @@ -197,23 +197,26 @@ ast_s_of(rb_execution_context_t *ec, VALUE module, VALUE body, VALUE save_script
{
VALUE path, node, lines;
int node_id;
const rb_iseq_t *iseq = NULL;

if (rb_obj_is_proc(body)) {
iseq = vm_proc_iseq(body);

if (!rb_obj_is_iseq((VALUE)iseq)) {
iseq = NULL;
}
if (rb_frame_info_p(body)) {
rb_frame_info_get(body, &path, &node_id);
if (NIL_P(path)) return Qnil;
}
else {
iseq = rb_method_iseq(body);
}
const rb_iseq_t *iseq = NULL;

if (rb_obj_is_proc(body)) {
iseq = vm_proc_iseq(body);

if (!iseq) return Qnil;
if (!rb_obj_is_iseq((VALUE)iseq)) return Qnil;
}
else {
iseq = rb_method_iseq(body);
}
path = rb_iseq_path(iseq);
node_id = iseq->body->location.node_id;
}

path = rb_iseq_path(iseq);
node_id = iseq->body->location.node_id;
if (!NIL_P(lines = script_lines(path))) {
node = rb_ast_parse_array(lines, save_script_lines);
}
Expand Down
28 changes: 23 additions & 5 deletions compile.c
Expand Up @@ -9704,13 +9704,13 @@ event_name_to_flag(VALUE sym)

static int
iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *const anchor,
VALUE body, VALUE labels_wrapper)
VALUE body, VALUE node_ids, VALUE labels_wrapper)
{
/* TODO: body should be frozen */
long i, len = RARRAY_LEN(body);
struct st_table *labels_table = DATA_PTR(labels_wrapper);
int j;
int line_no = 0;
int line_no = 0, node_id = -1, insn_idx = 0;
int ret = COMPILE_OK;

/*
Expand Down Expand Up @@ -9744,6 +9744,10 @@ iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *const anchor,
st_data_t insn_id;
VALUE insn;

if (node_ids) {
node_id = NUM2INT(rb_ary_entry(node_ids, insn_idx++));
}

insn = (argc < 0) ? Qnil : RARRAY_AREF(obj, 0);
if (st_lookup(insn_table, (st_data_t)insn, &insn_id) == 0) {
/* TODO: exception */
Expand All @@ -9764,7 +9768,7 @@ iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *const anchor,
argv = compile_data_calloc2(iseq, sizeof(VALUE), argc);

// add element before operand setup to make GC root
NODE dummy_line_node = generate_dummy_line_node(line_no, -1);
NODE dummy_line_node = generate_dummy_line_node(line_no, node_id);
ADD_ELEM(anchor,
(LINK_ELEMENT*)new_insn_core(iseq, &dummy_line_node,
(enum ruby_vminsn_type)insn_id, argc, argv));
Expand Down Expand Up @@ -9848,7 +9852,7 @@ iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *const anchor,
}
}
else {
NODE dummy_line_node = generate_dummy_line_node(line_no, -1);
NODE dummy_line_node = generate_dummy_line_node(line_no, node_id);
ADD_ELEM(anchor,
(LINK_ELEMENT*)new_insn_core(iseq, &dummy_line_node,
(enum ruby_vminsn_type)insn_id, argc, NULL));
Expand Down Expand Up @@ -10054,6 +10058,14 @@ rb_iseq_build_from_ary(rb_iseq_t *iseq, VALUE misc, VALUE locals, VALUE params,
#undef INT_PARAM
}

VALUE node_ids = Qfalse;
#ifdef EXPERIMENTAL_ISEQ_NODE_ID
node_ids = rb_hash_aref(misc, ID2SYM(rb_intern("node_ids")));
if (!RB_TYPE_P(node_ids, T_ARRAY)) {
rb_raise(rb_eTypeError, "node_ids is not an array");
}
#endif

if (RB_TYPE_P(arg_opt_labels, T_ARRAY)) {
len = RARRAY_LENINT(arg_opt_labels);
iseq->body->param.flags.has_opt = !!(len - 1 >= 0);
Expand Down Expand Up @@ -10103,7 +10115,7 @@ rb_iseq_build_from_ary(rb_iseq_t *iseq, VALUE misc, VALUE locals, VALUE params,
iseq_build_from_ary_exception(iseq, labels_table, exception);

/* body */
iseq_build_from_ary_body(iseq, anchor, body, labels_wrapper);
iseq_build_from_ary_body(iseq, anchor, body, node_ids, labels_wrapper);

iseq->body->param.size = arg_size;
iseq->body->local_table_size = local_size;
Expand Down Expand Up @@ -10928,6 +10940,9 @@ ibf_dump_insns_info_body(struct ibf_dump *dump, const rb_iseq_t *iseq)
unsigned int i;
for (i = 0; i < iseq->body->insns_info.size; i++) {
ibf_dump_write_small_value(dump, entries[i].line_no);
#ifdef EXPERIMENTAL_ISEQ_NODE_ID
ibf_dump_write_small_value(dump, entries[i].node_id);
#endif
ibf_dump_write_small_value(dump, entries[i].events);
}

Expand All @@ -10943,6 +10958,9 @@ ibf_load_insns_info_body(const struct ibf_load *load, ibf_offset_t body_offset,
unsigned int i;
for (i = 0; i < size; i++) {
entries[i].line_no = (int)ibf_load_small_value(load, &reading_pos);
#ifdef EXPERIMENTAL_ISEQ_NODE_ID
entries[i].node_id = (int)ibf_load_small_value(load, &reading_pos);
#endif
entries[i].events = (rb_event_flag_t)ibf_load_small_value(load, &reading_pos);
}

Expand Down
2 changes: 2 additions & 0 deletions internal/vm.h
Expand Up @@ -111,6 +111,8 @@ int rb_backtrace_p(VALUE obj);
VALUE rb_backtrace_to_str_ary(VALUE obj);
VALUE rb_backtrace_to_location_ary(VALUE obj);
void rb_backtrace_each(VALUE (*iter)(VALUE recv, VALUE str), VALUE output);
int rb_frame_info_p(VALUE obj);
void rb_frame_info_get(VALUE obj, VALUE *path, int *node_id);

MJIT_SYMBOL_EXPORT_BEGIN
VALUE rb_ec_backtrace_object(const struct rb_execution_context_struct *ec);
Expand Down
2 changes: 1 addition & 1 deletion iseq.c
Expand Up @@ -2998,7 +2998,7 @@ iseq_data_to_ary(const rb_iseq_t *iseq)
INT2FIX(iseq_body->location.code_location.end_pos.lineno),
INT2FIX(iseq_body->location.code_location.end_pos.column)));
#ifdef EXPERIMENTAL_ISEQ_NODE_ID
rb_hash_aset(misc, ID2SYM(rb_intern("node_ids_for_each_insn")), node_ids);
rb_hash_aset(misc, ID2SYM(rb_intern("node_ids")), node_ids);
#endif

/*
Expand Down
69 changes: 66 additions & 3 deletions vm_backtrace.c
Expand Up @@ -34,7 +34,7 @@ id2str(ID id)
#define ALL_BACKTRACE_LINES -1

inline static int
calc_lineno(const rb_iseq_t *iseq, const VALUE *pc)
calc_pos(const rb_iseq_t *iseq, const VALUE *pc, int *lineno, int *node_id)
{
VM_ASSERT(iseq);
VM_ASSERT(iseq->body);
Expand All @@ -46,7 +46,11 @@ calc_lineno(const rb_iseq_t *iseq, const VALUE *pc)
VM_ASSERT(! iseq->body->local_table_size);
return 0;
}
return FIX2INT(iseq->body->location.first_lineno);
if (lineno) *lineno = FIX2INT(iseq->body->location.first_lineno);
#ifdef EXPERIMENTAL_ISEQ_NODE_ID
if (node_id) *node_id = -1;
#endif
return 1;
}
else {
ptrdiff_t n = pc - iseq->body->iseq_encoded;
Expand All @@ -65,10 +69,32 @@ calc_lineno(const rb_iseq_t *iseq, const VALUE *pc)
__builtin_trap();
}
#endif
return rb_iseq_line_no(iseq, pos);
if (lineno) *lineno = rb_iseq_line_no(iseq, pos);
#ifdef EXPERIMENTAL_ISEQ_NODE_ID
if (node_id) *node_id = rb_iseq_node_id(iseq, pos);
#endif
return 1;
}
}

inline static int
calc_lineno(const rb_iseq_t *iseq, const VALUE *pc)
{
int lineno;
if (calc_pos(iseq, pc, &lineno, NULL)) return lineno;
return 0;
}

#ifdef EXPERIMENTAL_ISEQ_NODE_ID
inline static int
calc_node_id(const rb_iseq_t *iseq, const VALUE *pc)
{
int node_id;
if (calc_pos(iseq, pc, NULL, &node_id)) return node_id;
return -1;
}
#endif

int
rb_vm_get_sourceline(const rb_control_frame_t *cfp)
{
Expand Down Expand Up @@ -143,6 +169,12 @@ static const rb_data_type_t location_data_type = {
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
};

int
rb_frame_info_p(VALUE obj)
{
return rb_typeddata_is_kind_of(obj, &location_data_type);
}

static inline rb_backtrace_location_t *
location_ptr(VALUE locobj)
{
Expand Down Expand Up @@ -287,6 +319,37 @@ location_path_m(VALUE self)
return location_path(location_ptr(self));
}

#ifdef EXPERIMENTAL_ISEQ_NODE_ID
static int
location_node_id(rb_backtrace_location_t *loc)
{
switch (loc->type) {
case LOCATION_TYPE_ISEQ:
return calc_node_id(loc->body.iseq.iseq, loc->body.iseq.pc);
case LOCATION_TYPE_CFUNC:
if (loc->body.cfunc.prev_loc) {
return location_node_id(loc->body.cfunc.prev_loc);
}
return -1;
default:
rb_bug("location_node_id: unreachable");
UNREACHABLE;
}
}
#endif

void
rb_frame_info_get(VALUE obj, VALUE *path, int *node_id)
{
#ifdef EXPERIMENTAL_ISEQ_NODE_ID
rb_backtrace_location_t *loc = location_ptr(obj);
*path = location_path(loc);
*node_id = location_node_id(loc);
#else
*path = Qnil;
#endif
}

static VALUE
location_realpath(rb_backtrace_location_t *loc)
{
Expand Down

0 comments on commit dfba87c

Please sign in to comment.