Skip to content

Commit 73674ca

Browse files
authored
YJIT: log the names of methods we call to in disasm (#7231)
* YJIT: log the names of methods we call to in disasm * Assert that pointer is not null * Handle case where UTF8 conversion not possible
1 parent 92ac5f6 commit 73674ca

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

yjit/bindgen/src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ fn main() {
7777
// From encindex.h
7878
.allowlist_type("ruby_preserved_encindex")
7979

80+
// From include/ruby/ruby.h
81+
.allowlist_function("rb_class2name")
82+
8083
// This struct is public to Ruby C extensions
8184
// From include/ruby/internal/core/rbasic.h
8285
.allowlist_type("RBasic")
@@ -202,6 +205,7 @@ fn main() {
202205
// From include/ruby/internal/symbol.h
203206
.allowlist_function("rb_intern")
204207
.allowlist_function("rb_id2sym")
208+
.allowlist_function("rb_id2name")
205209
.allowlist_function("rb_sym2id")
206210
.allowlist_function("rb_str_intern")
207211

yjit/src/codegen.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5668,9 +5668,6 @@ fn gen_send_iseq(
56685668
},
56695669
);
56705670

5671-
//print_str(cb, "calling Ruby func:");
5672-
//print_str(cb, rb_id2name(vm_ci_mid(ci)));
5673-
56745671
// Directly jump to the entry point of the callee
56755672
gen_direct_jump(
56765673
jit,
@@ -5831,6 +5828,19 @@ fn gen_send_general(
58315828
let comptime_recv = jit_peek_at_stack(jit, ctx, recv_idx as isize);
58325829
let comptime_recv_klass = comptime_recv.class_of();
58335830

5831+
// Log the name of the method we're calling to
5832+
#[cfg(feature = "disasm")]
5833+
{
5834+
let class_name = unsafe { cstr_to_rust_string(rb_class2name(comptime_recv_klass)) };
5835+
let method_name = unsafe { cstr_to_rust_string(rb_id2name(mid)) };
5836+
match (class_name, method_name) {
5837+
(Some(class_name), Some(method_name)) => {
5838+
asm.comment(&format!("call to {}#{}", class_name, method_name))
5839+
}
5840+
_ => {}
5841+
}
5842+
}
5843+
58345844
// Guard that the receiver has the same class as the one from compile time
58355845
let side_exit = get_side_exit(jit, ocb, ctx);
58365846

yjit/src/cruby.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,10 +564,23 @@ pub fn rust_str_to_ruby(str: &str) -> VALUE {
564564
pub fn rust_str_to_sym(str: &str) -> VALUE {
565565
let c_str = CString::new(str).unwrap();
566566
let c_ptr: *const c_char = c_str.as_ptr();
567-
568567
unsafe { rb_id2sym(rb_intern(c_ptr)) }
569568
}
570569

570+
/// Produce an owned Rust String from a C char pointer
571+
#[cfg(feature = "disasm")]
572+
pub fn cstr_to_rust_string(c_char_ptr: *const c_char) -> Option<String> {
573+
assert!(c_char_ptr != std::ptr::null());
574+
575+
use std::ffi::CStr;
576+
let c_str: &CStr = unsafe { CStr::from_ptr(c_char_ptr) };
577+
578+
match c_str.to_str() {
579+
Ok(rust_str) => Some(rust_str.to_string()),
580+
Err(_) => None
581+
}
582+
}
583+
571584
/// A location in Rust code for integrating with debugging facilities defined in C.
572585
/// Use the [src_loc!] macro to crate an instance.
573586
pub struct SourceLocation {

yjit/src/cruby_bindings.inc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,8 @@ extern "C" {
11011101
pub fn rb_sym2id(obj: VALUE) -> ID;
11021102
pub fn rb_id2sym(id: ID) -> VALUE;
11031103
pub fn rb_intern(name: *const ::std::os::raw::c_char) -> ID;
1104+
pub fn rb_id2name(id: ID) -> *const ::std::os::raw::c_char;
1105+
pub fn rb_class2name(klass: VALUE) -> *const ::std::os::raw::c_char;
11041106
pub fn rb_gc_mark(obj: VALUE);
11051107
pub fn rb_gc_mark_movable(obj: VALUE);
11061108
pub fn rb_gc_location(obj: VALUE) -> VALUE;

0 commit comments

Comments
 (0)