diff options
Diffstat (limited to 'eval_error.c')
| -rw-r--r-- | eval_error.c | 543 |
1 files changed, 385 insertions, 158 deletions
diff --git a/eval_error.c b/eval_error.c index 76140fcded..1baa4484a0 100644 --- a/eval_error.c +++ b/eval_error.c @@ -3,60 +3,59 @@ * included by eval.c */ -static void -warn_printf(const char *fmt, ...) -{ - char buf[BUFSIZ]; - va_list args; - - va_init_list(args, fmt); - vsnprintf(buf, BUFSIZ, fmt, args); - va_end(args); - rb_write_error(buf); -} - +#define write_warn(str, x) \ + (NIL_P(str) ? warn_print(x) : (void)rb_str_cat_cstr(str, x)) +#define write_warn2(str, x, l) \ + (NIL_P(str) ? warn_print2(x, l) : (void)rb_str_cat(str, x, l)) +#ifdef HAVE_BUILTIN___BUILTIN_CONSTANT_P +#define warn_print(x) RB_GNUC_EXTENSION_BLOCK( \ + (__builtin_constant_p(x)) ? \ + rb_write_error2((x), (long)strlen(x)) : \ + rb_write_error(x) \ +) +#else #define warn_print(x) rb_write_error(x) +#endif + #define warn_print2(x,l) rb_write_error2((x),(l)) +#define write_warn_str(str,x) NIL_P(str) ? rb_write_error_str(x) : (void)rb_str_concat((str), (x)) +#define warn_print_str(x) rb_write_error_str(x) + +static VALUE error_pos_str(void); + static void -error_pos(void) +error_pos(const VALUE str) +{ + VALUE pos = error_pos_str(); + if (!NIL_P(pos)) { + write_warn_str(str, pos); + } +} + +static VALUE +error_pos_str(void) { - const char *sourcefile = rb_sourcefile(); - int sourceline = rb_sourceline(); + int sourceline; + VALUE sourcefile = rb_source_location(&sourceline); - if (sourcefile) { + if (!NIL_P(sourcefile)) { + ID caller_name; if (sourceline == 0) { - warn_printf("%s", sourcefile); + return rb_sprintf("%"PRIsVALUE": ", sourcefile); } - else if (rb_frame_callee()) { - warn_printf("%s:%d:in `%s'", sourcefile, sourceline, - rb_id2name(rb_frame_callee())); + else if ((caller_name = rb_frame_callee()) != 0) { + return rb_sprintf("%"PRIsVALUE":%d:in `%"PRIsVALUE"': ", + sourcefile, sourceline, + rb_id2str(caller_name)); } else { - warn_printf("%s:%d", sourcefile, sourceline); + return rb_sprintf("%"PRIsVALUE":%d: ", sourcefile, sourceline); } } + return Qnil; } -static VALUE -get_backtrace(VALUE info) -{ - if (NIL_P(info)) - return Qnil; - info = rb_funcall(info, rb_intern("backtrace"), 0); - if (NIL_P(info)) - return Qnil; - return rb_check_backtrace(info); -} - -VALUE -rb_get_backtrace(VALUE info) -{ - return get_backtrace(info); -} - -VALUE rb_exc_set_backtrace(VALUE exc, VALUE bt); - static void set_backtrace(VALUE info, VALUE bt) { @@ -71,177 +70,400 @@ set_backtrace(VALUE info, VALUE bt) bt = rb_backtrace_to_str_ary(bt); } } - rb_funcall(info, rb_intern("set_backtrace"), 1, bt); + rb_check_funcall(info, set_backtrace, 1, &bt); } static void -error_print(void) +error_print(rb_execution_context_t *ec) { - volatile VALUE errat = Qnil; /* OK */ - rb_thread_t *th = GET_THREAD(); - VALUE errinfo = th->errinfo; - int raised_flag = th->raised_flag; - volatile VALUE eclass, e; - const char *volatile einfo; - volatile long elen; - - if (NIL_P(errinfo)) - return; - rb_thread_raised_clear(th); + rb_ec_error_print(ec, ec->errinfo); +} - TH_PUSH_TAG(th); - if (TH_EXEC_TAG() == 0) { - errat = get_backtrace(errinfo); +static void +write_warnq(VALUE out, VALUE str, const char *ptr, long len) +{ + if (NIL_P(out)) { + const char *beg = ptr; + const long olen = len; + for (; len > 0; --len, ++ptr) { + unsigned char c = *ptr; + switch (c) { + case '\n': case '\t': continue; + } + if (rb_iscntrl(c)) { + char buf[5]; + const char *cc = 0; + if (ptr > beg) rb_write_error2(beg, ptr - beg); + beg = ptr + 1; + cc = ruby_escaped_char(c); + if (cc) { + rb_write_error2(cc, strlen(cc)); + } + else { + rb_write_error2(buf, snprintf(buf, sizeof(buf), "\\x%02X", c)); + } + } + else if (c == '\\') { + rb_write_error2(beg, ptr - beg + 1); + beg = ptr; + } + } + if (ptr > beg) { + if (beg == RSTRING_PTR(str) && olen == RSTRING_LEN(str)) + rb_write_error_str(str); + else + rb_write_error2(beg, ptr - beg); + } } else { - errat = Qnil; - } - if (TH_EXEC_TAG()) - goto error; - if (NIL_P(errat)) { - const char *file = rb_sourcefile(); - int line = rb_sourceline(); - if (!file) - warn_printf("%d", line); - else if (!line) - warn_printf("%s", file); - else - warn_printf("%s:%d", file, line); + rb_str_cat(out, ptr, len); } - else if (RARRAY_LEN(errat) == 0) { - error_pos(); - } - else { - VALUE mesg = RARRAY_PTR(errat)[0]; +} - if (NIL_P(mesg)) - error_pos(); +#define CSI_BEGIN "\033[" +#define CSI_SGR "m" + +static const char underline[] = CSI_BEGIN"1;4"CSI_SGR; +static const char bold[] = CSI_BEGIN"1"CSI_SGR; +static const char reset[] = CSI_BEGIN""CSI_SGR; + +static void +print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg, const VALUE str, int highlight) +{ + const char *einfo = ""; + long elen = 0; + VALUE mesg; + + if (emesg != Qundef) { + if (NIL_P(errat) || RARRAY_LEN(errat) == 0 || + NIL_P(mesg = RARRAY_AREF(errat, 0))) { + error_pos(str); + } else { - warn_print2(RSTRING_PTR(mesg), RSTRING_LEN(mesg)); + write_warn_str(str, mesg); + write_warn(str, ": "); } - } - eclass = CLASS_OF(errinfo); - if (TH_EXEC_TAG() == 0) { - e = rb_funcall(errinfo, rb_intern("message"), 0, 0); - StringValue(e); - einfo = RSTRING_PTR(e); - elen = RSTRING_LEN(e); - } - else { - einfo = ""; - elen = 0; + if (!NIL_P(emesg)) { + einfo = RSTRING_PTR(emesg); + elen = RSTRING_LEN(emesg); + } } - if (TH_EXEC_TAG()) - goto error; + if (eclass == rb_eRuntimeError && elen == 0) { - warn_print(": unhandled exception\n"); + if (highlight) write_warn(str, underline); + write_warn(str, "unhandled exception"); + if (highlight) write_warn(str, reset); + write_warn2(str, "\n", 1); } else { VALUE epath; epath = rb_class_name(eclass); if (elen == 0) { - warn_print(": "); - warn_print2(RSTRING_PTR(epath), RSTRING_LEN(epath)); - warn_print("\n"); + if (highlight) write_warn(str, underline); + write_warn_str(str, epath); + if (highlight) write_warn(str, reset); + write_warn(str, "\n"); } else { - char *tail = 0; - long len = elen; + /* emesg is a String instance */ + const char *tail = 0; + if (highlight) write_warn(str, bold); if (RSTRING_PTR(epath)[0] == '#') epath = 0; if ((tail = memchr(einfo, '\n', elen)) != 0) { - len = tail - einfo; + write_warnq(str, emesg, einfo, tail - einfo); tail++; /* skip newline */ } - warn_print(": "); - warn_print2(einfo, len); + else { + write_warnq(str, emesg, einfo, elen); + } if (epath) { - warn_print(" ("); - warn_print2(RSTRING_PTR(epath), RSTRING_LEN(epath)); - warn_print(")\n"); + write_warn(str, " ("); + if (highlight) write_warn(str, underline); + write_warn_str(str, epath); + if (highlight) { + write_warn(str, reset); + write_warn(str, bold); + } + write_warn2(str, ")", 1); + if (highlight) write_warn(str, reset); + write_warn2(str, "\n", 1); + } + if (tail && einfo+elen > tail) { + if (!highlight) { + write_warnq(str, emesg, tail, einfo+elen-tail); + if (einfo[elen-1] != '\n') write_warn2(str, "\n", 1); + } + else { + elen -= tail - einfo; + einfo = tail; + while (elen > 0) { + tail = memchr(einfo, '\n', elen); + if (!tail || tail > einfo) { + write_warn(str, bold); + write_warnq(str, emesg, einfo, tail ? tail-einfo : elen); + write_warn(str, reset); + if (!tail) { + write_warn2(str, "\n", 1); + break; + } + } + elen -= tail - einfo; + einfo = tail; + do ++tail; while (tail < einfo+elen && *tail == '\n'); + write_warnq(str, emesg, einfo, tail-einfo); + elen -= tail - einfo; + einfo = tail; + } + } } - if (tail) { - warn_print2(tail, elen - len - 1); - if (einfo[elen-1] != '\n') warn_print2("\n", 1); + else if (!epath) { + write_warn2(str, "\n", 1); } } } +} +static void +print_backtrace(const VALUE eclass, const VALUE errat, const VALUE str, int reverse, long backtrace_limit) +{ if (!NIL_P(errat)) { long i; long len = RARRAY_LEN(errat); - VALUE *ptr = RARRAY_PTR(errat); - int skip = eclass == rb_eSysStackError; + const int threshold = 1000000000; + int width = (len <= 1) ? INT_MIN : ((int)log10((double)(len > threshold ? + ((len - 1) / threshold) : + len - 1)) + + (len < threshold ? 0 : 9) + 1); + + long skip_start = -1, skip_len = 0; -#define TRACE_MAX (TRACE_HEAD+TRACE_TAIL+5) -#define TRACE_HEAD 8 -#define TRACE_TAIL 5 + // skip for stackoverflow + if (eclass == rb_eSysStackError) { + long trace_head = 9; + long trace_tail = 4; + long trace_max = trace_head + trace_tail + 5; + if (len > trace_max) { + skip_start = trace_head; + skip_len = len - trace_max + 5; + } + } + + // skip for explicit limit + if (backtrace_limit >= 0 && len > backtrace_limit + 2) { + skip_start = backtrace_limit + 1; + skip_len = len - skip_start; + } for (i = 1; i < len; i++) { - if (RB_TYPE_P(ptr[i], T_STRING)) { - warn_printf("\tfrom %s\n", RSTRING_PTR(ptr[i])); + if (i == skip_start) { + write_warn_str(str, rb_sprintf("\t ... %ld levels...\n", skip_len)); + i += skip_len; + if (i >= len) break; } - if (skip && i == TRACE_HEAD && len > TRACE_MAX) { - warn_printf("\t ... %ld levels...\n", - len - TRACE_HEAD - TRACE_TAIL); - i = len - TRACE_TAIL; + VALUE line = RARRAY_AREF(errat, reverse ? len - i : i); + if (RB_TYPE_P(line, T_STRING)) { + VALUE bt = rb_str_new_cstr("\t"); + if (reverse) rb_str_catf(bt, "%*ld: ", width, len - i); + write_warn_str(str, rb_str_catf(bt, "from %"PRIsVALUE"\n", line)); } } } - error: - TH_POP_TAG(); - rb_thread_raised_set(th, raised_flag); +} + +VALUE rb_get_message(VALUE exc); + +static int +shown_cause_p(VALUE cause, VALUE *shown_causes) +{ + VALUE shown = *shown_causes; + if (!shown) { + *shown_causes = shown = rb_obj_hide(rb_ident_hash_new()); + } + if (rb_hash_has_key(shown, cause)) return TRUE; + rb_hash_aset(shown, cause, Qtrue); + return FALSE; +} + +static void +show_cause(VALUE errinfo, VALUE str, VALUE highlight, VALUE reverse, long backtrace_limit, VALUE *shown_causes) +{ + VALUE cause = rb_attr_get(errinfo, id_cause); + if (!NIL_P(cause) && rb_obj_is_kind_of(cause, rb_eException) && + !shown_cause_p(cause, shown_causes)) { + volatile VALUE eclass = CLASS_OF(cause); + VALUE errat = rb_get_backtrace(cause); + VALUE emesg = rb_get_message(cause); + if (reverse) { + show_cause(cause, str, highlight, reverse, backtrace_limit, shown_causes); + print_backtrace(eclass, errat, str, TRUE, backtrace_limit); + print_errinfo(eclass, errat, emesg, str, highlight!=0); + } + else { + print_errinfo(eclass, errat, emesg, str, highlight!=0); + print_backtrace(eclass, errat, str, FALSE, backtrace_limit); + show_cause(cause, str, highlight, reverse, backtrace_limit, shown_causes); + } + } } void -ruby_error_print(void) +rb_exc_check_circular_cause(VALUE exc) { - error_print(); + VALUE cause = exc, shown_causes = 0; + do { + if (shown_cause_p(cause, &shown_causes)) { + rb_raise(rb_eArgError, "circular causes"); + } + } while (!NIL_P(cause = rb_attr_get(cause, id_cause))); } void -rb_print_undef(VALUE klass, ID id, int scope) +rb_error_write(VALUE errinfo, VALUE emesg, VALUE errat, VALUE str, VALUE highlight, VALUE reverse) { - const char *v; + volatile VALUE eclass; + VALUE shown_causes = 0; + long backtrace_limit = rb_backtrace_length_limit; - switch (scope) { - default: - case NOEX_PUBLIC: v = ""; break; - case NOEX_PRIVATE: v = " private"; break; - case NOEX_PROTECTED: v = " protected"; break; + if (NIL_P(errinfo)) + return; + + if (errat == Qundef) { + errat = Qnil; + } + eclass = CLASS_OF(errinfo); + if (NIL_P(reverse)) reverse = Qfalse; + if (NIL_P(highlight)) { + VALUE tty = (VALUE)rb_stderr_tty_p(); + if (NIL_P(highlight)) highlight = tty; + } + if (reverse) { + static const char traceback[] = "Traceback " + "(most recent call last):\n"; + const int bold_part = rb_strlen_lit("Traceback"); + char buff[sizeof(traceback)+sizeof(bold)+sizeof(reset)-2], *p = buff; + const char *msg = traceback; + long len = sizeof(traceback) - 1; + if (highlight) { +#define APPEND(s, l) (memcpy(p, s, l), p += (l)) + APPEND(bold, sizeof(bold)-1); + APPEND(traceback, bold_part); + APPEND(reset, sizeof(reset)-1); + APPEND(traceback + bold_part, sizeof(traceback)-bold_part-1); +#undef APPEND + len = p - (msg = buff); + } + write_warn2(str, msg, len); + show_cause(errinfo, str, highlight, reverse, backtrace_limit, &shown_causes); + print_backtrace(eclass, errat, str, TRUE, backtrace_limit); + print_errinfo(eclass, errat, emesg, str, highlight!=0); + } + else { + print_errinfo(eclass, errat, emesg, str, highlight!=0); + print_backtrace(eclass, errat, str, FALSE, backtrace_limit); + show_cause(errinfo, str, highlight, reverse, backtrace_limit, &shown_causes); } - rb_name_error(id, "undefined%s method `%s' for %s `%s'", v, - rb_id2name(id), - (RB_TYPE_P(klass, T_MODULE)) ? "module" : "class", - rb_class2name(klass)); +} + +void +rb_ec_error_print(rb_execution_context_t * volatile ec, volatile VALUE errinfo) +{ + volatile uint8_t raised_flag = ec->raised_flag; + volatile VALUE errat = Qundef; + volatile VALUE emesg = Qundef; + volatile bool written = false; + + if (NIL_P(errinfo)) + return; + rb_ec_raised_clear(ec); + + EC_PUSH_TAG(ec); + if (EC_EXEC_TAG() == TAG_NONE) { + errat = rb_get_backtrace(errinfo); + } + if (emesg == Qundef) { + emesg = Qnil; + emesg = rb_get_message(errinfo); + } + + if (!written) { + written = true; + rb_error_write(errinfo, emesg, errat, Qnil, Qnil, Qfalse); + } + + EC_POP_TAG(); + ec->errinfo = errinfo; + rb_ec_raised_set(ec, raised_flag); +} + +#define undef_mesg_for(v, k) rb_fstring_lit("undefined"v" method `%1$s' for "k" `%2$s'") +#define undef_mesg(v) ( \ + is_mod ? \ + undef_mesg_for(v, "module") : \ + undef_mesg_for(v, "class")) + +void +rb_print_undef(VALUE klass, ID id, rb_method_visibility_t visi) +{ + const int is_mod = RB_TYPE_P(klass, T_MODULE); + VALUE mesg; + switch (visi & METHOD_VISI_MASK) { + case METHOD_VISI_UNDEF: + case METHOD_VISI_PUBLIC: mesg = undef_mesg(""); break; + case METHOD_VISI_PRIVATE: mesg = undef_mesg(" private"); break; + case METHOD_VISI_PROTECTED: mesg = undef_mesg(" protected"); break; + default: UNREACHABLE; + } + rb_name_err_raise_str(mesg, klass, ID2SYM(id)); } void rb_print_undef_str(VALUE klass, VALUE name) { - rb_name_error_str(name, "undefined method `%s' for %s `%s'", - RSTRING_PTR(name), - (RB_TYPE_P(klass, T_MODULE)) ? "module" : "class", - rb_class2name(klass)); + const int is_mod = RB_TYPE_P(klass, T_MODULE); + rb_name_err_raise_str(undef_mesg(""), klass, name); +} + +#define inaccessible_mesg_for(v, k) rb_fstring_lit("method `%1$s' for "k" `%2$s' is "v) +#define inaccessible_mesg(v) ( \ + is_mod ? \ + inaccessible_mesg_for(v, "module") : \ + inaccessible_mesg_for(v, "class")) + +void +rb_print_inaccessible(VALUE klass, ID id, rb_method_visibility_t visi) +{ + const int is_mod = RB_TYPE_P(klass, T_MODULE); + VALUE mesg; + switch (visi & METHOD_VISI_MASK) { + case METHOD_VISI_UNDEF: + case METHOD_VISI_PUBLIC: mesg = inaccessible_mesg(""); break; + case METHOD_VISI_PRIVATE: mesg = inaccessible_mesg("private"); break; + case METHOD_VISI_PROTECTED: mesg = inaccessible_mesg("protected"); break; + default: UNREACHABLE; + } + rb_name_err_raise_str(mesg, klass, ID2SYM(id)); } static int sysexit_status(VALUE err) { - VALUE st = rb_iv_get(err, "status"); + VALUE st = rb_ivar_get(err, id_status); return NUM2INT(st); } +#define unknown_longjmp_status(status) \ + rb_bug("Unknown longjmp status %d", status) + static int -error_handle(int ex) +error_handle(rb_execution_context_t *ec, int ex) { int status = EXIT_FAILURE; - rb_thread_t *th = GET_THREAD(); - if (rb_threadptr_set_raised(th)) + if (rb_ec_set_raised(ec)) return EXIT_FAILURE; switch (ex & TAG_MASK) { case 0: @@ -249,50 +471,55 @@ error_handle(int ex) break; case TAG_RETURN: - error_pos(); - warn_print(": unexpected return\n"); + error_pos(Qnil); + warn_print("unexpected return\n"); break; case TAG_NEXT: - error_pos(); - warn_print(": unexpected next\n"); + error_pos(Qnil); + warn_print("unexpected next\n"); break; case TAG_BREAK: - error_pos(); - warn_print(": unexpected break\n"); + error_pos(Qnil); + warn_print("unexpected break\n"); break; case TAG_REDO: - error_pos(); - warn_print(": unexpected redo\n"); + error_pos(Qnil); + warn_print("unexpected redo\n"); break; case TAG_RETRY: - error_pos(); - warn_print(": retry outside of rescue clause\n"); + error_pos(Qnil); + warn_print("retry outside of rescue clause\n"); break; case TAG_THROW: /* TODO: fix me */ - error_pos(); - warn_printf(": unexpected throw\n"); + error_pos(Qnil); + warn_print("unexpected throw\n"); break; case TAG_RAISE: { - VALUE errinfo = GET_THREAD()->errinfo; + VALUE errinfo = ec->errinfo; if (rb_obj_is_kind_of(errinfo, rb_eSystemExit)) { status = sysexit_status(errinfo); } - else if (rb_obj_is_instance_of(errinfo, rb_eSignal)) { + else if (rb_obj_is_instance_of(errinfo, rb_eSignal) && + rb_ivar_get(errinfo, id_signo) != INT2FIX(SIGSEGV)) { /* no message when exiting by signal */ } + else if (rb_obj_is_kind_of(errinfo, rb_eSystemCallError) && + FIXNUM_P(rb_attr_get(errinfo, id_signo))) { + /* no message when exiting by error to be mapped to signal */ + } else { - error_print(); + rb_ec_error_print(ec, errinfo); } break; } case TAG_FATAL: - error_print(); + error_print(ec); break; default: - rb_bug("Unknown longjmp status %d", ex); + unknown_longjmp_status(ex); break; } - rb_threadptr_reset_raised(th); + rb_ec_reset_raised(ec); return status; } |
