ruby-changes:67866
From: Nobuyoshi <ko1@a...>
Date: Sat, 11 Sep 2021 08:41:47 +0900 (JST)
Subject: [ruby-changes:67866] cd829bb078 (master): Remove printf family from the mjit header
https://git.ruby-lang.org/ruby.git/commit/?id=cd829bb078 From cd829bb078e6a3486d9b5ea57fc5111d289c1860 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Thu, 9 Sep 2021 23:21:06 +0900 Subject: Remove printf family from the mjit header Linking printf family functions makes mjit objects to link unnecessary code. --- gc.h | 6 +++--- include/ruby/internal/core/rstring.h | 24 ++++++++++++------------ internal.h | 4 ++-- ractor_core.h | 14 +++++++++----- string.c | 10 ++++++++++ vm.c | 20 ++++++++++---------- vm_args.c | 2 +- vm_callinfo.h | 6 +++--- vm_exec.h | 16 +++++++++------- vm_insnhelper.c | 27 +++++++++++++-------------- 10 files changed, 72 insertions(+), 57 deletions(-) diff --git a/gc.h b/gc.h index 5d113ca..63c60f5 100644 --- a/gc.h +++ b/gc.h @@ -35,7 +35,7 @@ extern int ruby_gc_debug_indent; https://github.com/ruby/ruby/blob/trunk/gc.h#L35 static inline void rb_gc_debug_indent(void) { - printf("%*s", ruby_gc_debug_indent, ""); + ruby_debug_printf("%*s", ruby_gc_debug_indent, ""); } static inline void @@ -45,7 +45,7 @@ rb_gc_debug_body(const char *mode, const char *msg, int st, void *ptr) https://github.com/ruby/ruby/blob/trunk/gc.h#L45 ruby_gc_debug_indent--; } rb_gc_debug_indent(); - printf("%s: %s %s (%p)\n", mode, st ? "->" : "<-", msg, ptr); + ruby_debug_printf("%s: %s %s (%p)\n", mode, st ? "->" : "<-", msg, ptr); if (st) { ruby_gc_debug_indent++; @@ -58,7 +58,7 @@ rb_gc_debug_body(const char *mode, const char *msg, int st, void *ptr) https://github.com/ruby/ruby/blob/trunk/gc.h#L58 #define RUBY_MARK_LEAVE(msg) rb_gc_debug_body("mark", (msg), 0, ptr) #define RUBY_FREE_ENTER(msg) rb_gc_debug_body("free", (msg), 1, ptr) #define RUBY_FREE_LEAVE(msg) rb_gc_debug_body("free", (msg), 0, ptr) -#define RUBY_GC_INFO rb_gc_debug_indent(); printf +#define RUBY_GC_INFO rb_gc_debug_indent(), ruby_debug_printf #else #define RUBY_MARK_ENTER(msg) diff --git a/include/ruby/internal/core/rstring.h b/include/ruby/internal/core/rstring.h index e14753a..d16a57b 100644 --- a/include/ruby/internal/core/rstring.h +++ b/include/ruby/internal/core/rstring.h @@ -371,6 +371,16 @@ void rb_check_safe_str(VALUE); https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/core/rstring.h#L371 * only. You can safely forget about it. */ #define Check_SafeStr(v) rb_check_safe_str(RBIMPL_CAST((VALUE)(v))) + +/** + * @private + * + * Prints diagnostic message to stderr when RSTRING_PTR or RSTRING_END + * is NULL. + * + * @param[in] func The function name where encountered NULL pointer. + */ +void rb_debug_rstring_null_ptr(const char *func); RBIMPL_SYMBOL_EXPORT_END() RBIMPL_ATTR_PURE_UNLESS_DEBUG() @@ -476,12 +486,7 @@ RSTRING_PTR(VALUE str) https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/core/rstring.h#L486 * Also, this is not rb_warn() because RSTRING_PTR() can be called * during GC (see what obj_info() does). rb_warn() needs to allocate * Ruby objects. That is not possible at this moment. */ - fprintf(stderr, "%s\n", - "RSTRING_PTR is returning NULL!! " - "SIGSEGV is highly expected to follow immediately. " - "If you could reproduce, attach your debugger here, " - "and look at the passed string." - ); + rb_debug_rstring_null_ptr("RSTRING_PTR"); } return ptr; @@ -502,12 +507,7 @@ RSTRING_END(VALUE str) https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/core/rstring.h#L507 if (RB_UNLIKELY(! buf.as.heap.ptr)) { /* Ditto. */ - fprintf(stderr, "%s\n", - "RSTRING_END is returning NULL!! " - "SIGSEGV is highly expected to follow immediately. " - "If you could reproduce, attach your debugger here, " - "and look at the passed string." - ); + rb_debug_rstring_null_ptr("RSTRING_END"); } return &buf.as.heap.ptr[buf.as.heap.len]; diff --git a/internal.h b/internal.h index 8e5183a..8c116f8 100644 --- a/internal.h +++ b/internal.h @@ -95,8 +95,8 @@ RUBY_SYMBOL_EXPORT_END https://github.com/ruby/ruby/blob/trunk/internal.h#L95 // same as rp, but add message header #define rp_m(msg, obj) do { \ - fprintf(stderr, "%s", (msg)); \ - rb_obj_info_dump((VALUE)obj); \ + fputs((msg), stderr); \ + rb_obj_info_dump((VALUE)(obj)); \ } while (0) // `ruby_debug_breakpoint()` does nothing, diff --git a/ractor_core.h b/ractor_core.h index 879d868..a2dc99d 100644 --- a/ractor_core.h +++ b/ractor_core.h @@ -237,9 +237,11 @@ rb_ractor_sleeper_thread_num(rb_ractor_t *r) https://github.com/ruby/ruby/blob/trunk/ractor_core.h#L237 static inline void rb_ractor_thread_switch(rb_ractor_t *cr, rb_thread_t *th) { - if (cr->threads.running_ec != th->ec) { - if (0) fprintf(stderr, "rb_ractor_thread_switch ec:%p->%p\n", - (void *)cr->threads.running_ec, (void *)th->ec); + if (cr->threads.running_ec != th->ec) { + if (0) { + ruby_debug_printf("rb_ractor_thread_switch ec:%p->%p\n", + (void *)cr->threads.running_ec, (void *)th->ec); + } } else { return; @@ -268,8 +270,10 @@ rb_ractor_set_current_ec(rb_ractor_t *cr, rb_execution_context_t *ec) https://github.com/ruby/ruby/blob/trunk/ractor_core.h#L270 #endif if (cr->threads.running_ec != ec) { - if (0) fprintf(stderr, "rb_ractor_set_current_ec ec:%p->%p\n", - (void *)cr->threads.running_ec, (void *)ec); + if (0) { + ruby_debug_printf("rb_ractor_set_current_ec ec:%p->%p\n", + (void *)cr->threads.running_ec, (void *)ec); + } } else { VM_ASSERT(0); // should be different diff --git a/string.c b/string.c index cbec890..a0cf94c 100644 --- a/string.c +++ b/string.c @@ -223,6 +223,16 @@ rb_str_make_independent(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L223 } } +void +rb_debug_rstring_null_ptr(const char *func) +{ + fprintf(stderr, "%s is returning NULL!! " + "SIGSEGV is highly expected to follow immediately. " + "If you could reproduce, attach your debugger here, " + "and look at the passed string.", + func); +} + /* symbols for [up|down|swap]case/capitalize options */ static VALUE sym_ascii, sym_turkic, sym_lithuanian, sym_fold; diff --git a/vm.c b/vm.c index 3ee0dd0..ba5ad66 100644 --- a/vm.c +++ b/vm.c @@ -315,10 +315,10 @@ rb_vm_cref_new_toplevel(void) https://github.com/ruby/ruby/blob/trunk/vm.c#L315 static void vm_cref_dump(const char *mesg, const rb_cref_t *cref) { - fprintf(stderr, "vm_cref_dump: %s (%p)\n", mesg, (void *)cref); + ruby_debug_printf("vm_cref_dump: %s (%p)\n", mesg, (void *)cref); while (cref) { - fprintf(stderr, "= cref| klass: %s\n", RSTRING_PTR(rb_class_path(CREF_CLASS(cref)))); + ruby_debug_printf("= cref| klass: %s\n", RSTRING_PTR(rb_class_path(CREF_CLASS(cref)))); cref = CREF_NEXT(cref); } } @@ -686,15 +686,15 @@ static VALUE check_env_value(const rb_env_t *env); https://github.com/ruby/ruby/blob/trunk/vm.c#L686 static int check_env(const rb_env_t *env) { - fprintf(stderr, "---\n"); - fprintf(stderr, "envptr: %p\n", (void *)&env->ep[0]); - fprintf(stderr, "envval: %10p ", (void *)env->ep[1]); + fputs("---\n", stderr); + ruby_debug_printf("envptr: %p\n", (void *)&env->ep[0]); + ruby_debug_printf("envval: %10p ", (void *)env->ep[1]); dp(env->ep[1]); - fprintf(stderr, "ep: %10p\n", (void *)env->ep); + ruby_debug_printf("ep: %10p\n", (void *)env->ep); if (rb_vm_env_prev_env(env)) { - fprintf(stderr, ">>\n"); + fputs(">>\n", stderr); check_env_value(rb_vm_env_prev_env(env)); - fprintf(stderr, "<<\n"); + fputs("<<\n", stderr); } return 1; } @@ -2719,7 +2719,7 @@ get_param(const char *name, size_t default_value, size_t min_value) https://github.com/ruby/ruby/blob/trunk/vm.c#L2719 } result = (size_t)(((val -1 + RUBY_VM_SIZE_ALIGN) / RUBY_VM_SIZE_ALIGN) * RUBY_VM_SIZE_ALIGN); } - if (0) fprintf(stderr, "%s: %"PRIuSIZE"\n", name, result); /* debug print */ + if (0) ruby_debug_printf("%s: %"PRIuSIZE"\n", name, result); /* debug print */ return result; } @@ -3706,7 +3706,7 @@ Init_BareVM(void) https://github.com/ruby/ruby/blob/trunk/vm.c#L3706 rb_vm_t * vm = ruby_mimmalloc(sizeof(*vm)); rb_thread_t * th = ruby_mimmalloc(sizeof(*th)); if (!vm || !th) { - fprintf(stderr, "[FATAL] failed to allocate memory\n"); + fputs("[FATAL] failed to allocate memory\n", stderr); exit(EXIT_FAILURE); } MEMZERO(th, rb_thread_t, 1); diff --git a/vm_args.c b/vm_args.c index 9ee2832..0233671 100644 --- a/vm_args.c +++ b/vm_args.c @@ -701,7 +701,7 @@ setup_parameters_complex(rb_execution_context_t * const ec, const rb_iseq_t * co https://github.com/ruby/ruby/blob/trunk/vm_args.c#L701 { int i; for (i=0; i<iseq->body->param.size; i++) { - fprintf(stderr, "local[%d] = %p\n", i, (void *)locals[i]); + ruby_debug_printf("local[%d] = %p\n", i, (void *)locals[i]); } } #endif diff --git a/vm_callinfo.h b/vm_callinfo.h index 959b6e9..d5f4388 100644 --- a/vm_callinfo.h +++ b/vm_callinfo.h @@ -168,8 +168,8 @@ static inline void https://github.com/ruby/ruby/blob/trunk/vm_callinfo.h#L168 vm_ci_dump(const struct rb_callinfo *ci) { if (vm_ci_packed_p(ci)) { - fprintf(stderr, "packed_ci ID:%s flag:%x argc:%u\n", - rb_id2name(vm_ci_mid(ci)), vm_ci_flag(ci), vm_ci_argc(ci)); + ruby_debug_printf("packed_ci ID:%s flag:%x argc:%u\n", + rb_id2name(vm_ci_mid(ci)), vm_ci_flag(ci), vm_ci_argc(ci)); } (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/