2007-06-25 04:45:48 +04:00
|
|
|
/* -*-c-*- */
|
2006-12-31 18:02:22 +03:00
|
|
|
/*
|
|
|
|
* included by eval.c
|
|
|
|
*/
|
|
|
|
|
2018-01-07 07:57:04 +03:00
|
|
|
#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))
|
2016-05-12 21:12:46 +03:00
|
|
|
#ifdef HAVE_BUILTIN___BUILTIN_CONSTANT_P
|
Use RB_GNUC_EXTENSION_BLOCK instead of __extension__
* include/ruby/defines.h (RB_GNUC_EXTENSION, RB_GNUC_EXTENSION_BLOCK):
macros for skipping __extension__ on non-GCC compilers.
* eval_error.c (warn_print): use RB_GNUC_EXTENSION_BLOCK instead of
__extension__ because __extension__ is a GNU extension.
Fix compile error on Solaris 10 with Oracle Solaris Studio 12.x.
[Bug #12397] [ruby-dev:49629].
* internal.h (rb_fstring_cstr, rb_fstring_enc_cstr): ditto
* include/ruby/encoding.h (rb_enc_str_new, rb_enc_str_new_cstr): ditto
* include/ruby/intern.h (rb_str_new, rb_str_new_cstr,
rb_usascii_str_new, rb_utf8_str_new, rb_tainted_str_new_cstr,
rb_usascii_str_new_cstr, rb_utf8_str_new_cstr,
rb_external_str_new_cstr, rb_locale_str_new_cstr,
rb_str_buf_new_cstr, rb_str_cat_cstr, rb_exc_new_cstr): ditto
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55082 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-20 15:29:06 +03:00
|
|
|
#define warn_print(x) RB_GNUC_EXTENSION_BLOCK( \
|
2016-03-06 11:34:50 +03:00
|
|
|
(__builtin_constant_p(x)) ? \
|
|
|
|
rb_write_error2((x), (long)strlen(x)) : \
|
Use RB_GNUC_EXTENSION_BLOCK instead of __extension__
* include/ruby/defines.h (RB_GNUC_EXTENSION, RB_GNUC_EXTENSION_BLOCK):
macros for skipping __extension__ on non-GCC compilers.
* eval_error.c (warn_print): use RB_GNUC_EXTENSION_BLOCK instead of
__extension__ because __extension__ is a GNU extension.
Fix compile error on Solaris 10 with Oracle Solaris Studio 12.x.
[Bug #12397] [ruby-dev:49629].
* internal.h (rb_fstring_cstr, rb_fstring_enc_cstr): ditto
* include/ruby/encoding.h (rb_enc_str_new, rb_enc_str_new_cstr): ditto
* include/ruby/intern.h (rb_str_new, rb_str_new_cstr,
rb_usascii_str_new, rb_utf8_str_new, rb_tainted_str_new_cstr,
rb_usascii_str_new_cstr, rb_utf8_str_new_cstr,
rb_external_str_new_cstr, rb_locale_str_new_cstr,
rb_str_buf_new_cstr, rb_str_cat_cstr, rb_exc_new_cstr): ditto
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55082 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-20 15:29:06 +03:00
|
|
|
rb_write_error(x) \
|
|
|
|
)
|
2016-03-06 11:34:50 +03:00
|
|
|
#else
|
2006-12-31 18:02:22 +03:00
|
|
|
#define warn_print(x) rb_write_error(x)
|
2016-03-06 11:34:50 +03:00
|
|
|
#endif
|
2017-12-12 14:47:16 +03:00
|
|
|
|
2010-12-12 07:28:29 +03:00
|
|
|
#define warn_print2(x,l) rb_write_error2((x),(l))
|
2017-12-12 14:47:16 +03:00
|
|
|
|
2017-12-18 18:46:56 +03:00
|
|
|
#define write_warn_str(str,x) NIL_P(str) ? rb_write_error_str(x) : (void)rb_str_concat((str), (x))
|
2013-06-24 17:03:35 +04:00
|
|
|
#define warn_print_str(x) rb_write_error_str(x)
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2016-02-25 15:36:24 +03:00
|
|
|
static VALUE error_pos_str(void);
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
static void
|
2017-12-12 14:47:16 +03:00
|
|
|
error_pos(const VALUE str)
|
2016-02-25 15:36:24 +03:00
|
|
|
{
|
2017-12-12 14:47:16 +03:00
|
|
|
VALUE pos = error_pos_str();
|
|
|
|
if (!NIL_P(pos)) {
|
|
|
|
write_warn_str(str, pos);
|
2016-02-25 15:36:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
error_pos_str(void)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2015-10-31 04:02:26 +03:00
|
|
|
int sourceline;
|
|
|
|
VALUE sourcefile = rb_source_location(&sourceline);
|
2007-06-25 00:33:04 +04:00
|
|
|
|
2017-11-16 08:35:58 +03:00
|
|
|
if (!NIL_P(sourcefile)) {
|
2013-05-25 02:03:41 +04:00
|
|
|
ID caller_name;
|
2007-06-25 00:33:04 +04:00
|
|
|
if (sourceline == 0) {
|
2016-02-25 15:36:24 +03:00
|
|
|
return rb_sprintf("%"PRIsVALUE": ", sourcefile);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2013-05-25 02:03:41 +04:00
|
|
|
else if ((caller_name = rb_frame_callee()) != 0) {
|
2016-02-25 15:36:24 +03:00
|
|
|
return rb_sprintf("%"PRIsVALUE":%d:in `%"PRIsVALUE"': ",
|
|
|
|
sourcefile, sourceline,
|
|
|
|
rb_id2str(caller_name));
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
else {
|
2016-02-25 15:36:24 +03:00
|
|
|
return rb_sprintf("%"PRIsVALUE":%d: ", sourcefile, sourceline);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
}
|
2016-02-25 15:36:24 +03:00
|
|
|
return Qnil;
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_backtrace(VALUE info, VALUE bt)
|
|
|
|
{
|
2012-05-24 10:09:23 +04:00
|
|
|
ID set_backtrace = rb_intern("set_backtrace");
|
|
|
|
|
|
|
|
if (rb_backtrace_p(bt)) {
|
2012-05-24 10:36:44 +04:00
|
|
|
if (rb_method_basic_definition_p(CLASS_OF(info), set_backtrace)) {
|
2012-05-24 10:09:23 +04:00
|
|
|
rb_exc_set_backtrace(info, bt);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bt = rb_backtrace_to_str_ary(bt);
|
|
|
|
}
|
|
|
|
}
|
2016-11-13 08:25:53 +03:00
|
|
|
rb_check_funcall(info, set_backtrace, 1, &bt);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
2018-02-23 11:39:03 +03:00
|
|
|
#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;
|
|
|
|
|
2017-02-22 11:50:25 +03:00
|
|
|
static void
|
2018-01-07 07:16:31 +03:00
|
|
|
print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg, const VALUE str, int highlight)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2017-02-22 11:50:25 +03:00
|
|
|
long elen = 0;
|
2016-02-25 10:22:01 +03:00
|
|
|
VALUE mesg;
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2022-02-01 11:03:58 +03:00
|
|
|
if (NIL_P(errat) || RARRAY_LEN(errat) == 0 ||
|
|
|
|
NIL_P(mesg = RARRAY_AREF(errat, 0))) {
|
|
|
|
error_pos(str);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
write_warn_str(str, mesg);
|
|
|
|
write_warn(str, ": ");
|
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2022-02-01 11:03:58 +03:00
|
|
|
if (!NIL_P(emesg)) {
|
|
|
|
elen = RSTRING_LEN(emesg);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (eclass == rb_eRuntimeError && elen == 0) {
|
2018-01-07 07:16:31 +03:00
|
|
|
if (highlight) write_warn(str, underline);
|
2018-01-07 08:38:37 +03:00
|
|
|
write_warn(str, "unhandled exception");
|
|
|
|
if (highlight) write_warn(str, reset);
|
|
|
|
write_warn2(str, "\n", 1);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE epath;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
epath = rb_class_name(eclass);
|
|
|
|
if (elen == 0) {
|
2018-01-07 07:16:31 +03:00
|
|
|
if (highlight) write_warn(str, underline);
|
2017-12-12 14:47:16 +03:00
|
|
|
write_warn_str(str, epath);
|
2018-01-07 08:38:37 +03:00
|
|
|
if (highlight) write_warn(str, reset);
|
2017-12-12 14:47:16 +03:00
|
|
|
write_warn(str, "\n");
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2022-02-01 11:03:58 +03:00
|
|
|
else {
|
|
|
|
write_warn_str(str, emesg);
|
|
|
|
write_warn(str, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_decorate_message(const VALUE eclass, const VALUE emesg, int highlight)
|
|
|
|
{
|
|
|
|
const char *einfo = "";
|
|
|
|
long elen = 0;
|
|
|
|
|
|
|
|
VALUE str = rb_str_new2("");
|
|
|
|
|
|
|
|
if (!NIL_P(emesg)) {
|
|
|
|
einfo = RSTRING_PTR(emesg);
|
|
|
|
elen = RSTRING_LEN(emesg);
|
|
|
|
}
|
|
|
|
if (eclass == rb_eRuntimeError && elen == 0) {
|
|
|
|
if (highlight) write_warn(str, underline);
|
|
|
|
write_warn(str, "unhandled exception");
|
|
|
|
if (highlight) write_warn(str, reset);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE epath;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2022-02-01 11:03:58 +03:00
|
|
|
epath = rb_class_name(eclass);
|
|
|
|
if (elen == 0) {
|
|
|
|
if (highlight) write_warn(str, underline);
|
|
|
|
write_warn_str(str, epath);
|
|
|
|
if (highlight) write_warn(str, reset);
|
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
else {
|
2019-07-15 10:51:18 +03:00
|
|
|
/* emesg is a String instance */
|
2016-02-25 15:36:25 +03:00
|
|
|
const char *tail = 0;
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2019-07-15 10:51:18 +03:00
|
|
|
if (highlight) write_warn(str, bold);
|
2006-12-31 18:02:22 +03:00
|
|
|
if (RSTRING_PTR(epath)[0] == '#')
|
|
|
|
epath = 0;
|
2007-08-18 11:44:51 +04:00
|
|
|
if ((tail = memchr(einfo, '\n', elen)) != 0) {
|
2022-02-01 10:32:38 +03:00
|
|
|
write_warn2(str, einfo, tail - einfo);
|
2006-12-31 18:02:22 +03:00
|
|
|
tail++; /* skip newline */
|
2018-01-07 08:38:37 +03:00
|
|
|
}
|
|
|
|
else {
|
2022-02-01 10:32:38 +03:00
|
|
|
write_warn_str(str, emesg);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
if (epath) {
|
2017-12-12 14:47:16 +03:00
|
|
|
write_warn(str, " (");
|
2018-01-07 07:16:31 +03:00
|
|
|
if (highlight) write_warn(str, underline);
|
2017-12-12 14:47:16 +03:00
|
|
|
write_warn_str(str, epath);
|
2018-01-07 08:38:37 +03:00
|
|
|
if (highlight) {
|
|
|
|
write_warn(str, reset);
|
|
|
|
write_warn(str, bold);
|
|
|
|
}
|
|
|
|
write_warn2(str, ")", 1);
|
2018-01-07 07:16:31 +03:00
|
|
|
if (highlight) write_warn(str, reset);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2018-03-13 06:48:56 +03:00
|
|
|
if (tail && einfo+elen > tail) {
|
|
|
|
if (!highlight) {
|
2022-02-01 11:03:58 +03:00
|
|
|
write_warn2(str, "\n", 1);
|
2022-02-01 10:32:38 +03:00
|
|
|
write_warn2(str, tail, einfo+elen-tail);
|
2018-03-13 06:48:56 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
elen -= tail - einfo;
|
|
|
|
einfo = tail;
|
2022-02-01 09:58:39 +03:00
|
|
|
write_warn2(str, "\n", 1);
|
2018-03-13 06:48:56 +03:00
|
|
|
while (elen > 0) {
|
|
|
|
tail = memchr(einfo, '\n', elen);
|
|
|
|
if (!tail || tail > einfo) {
|
|
|
|
write_warn(str, bold);
|
2022-02-01 10:32:38 +03:00
|
|
|
write_warn2(str, einfo, tail ? tail-einfo : elen);
|
2018-03-13 06:48:56 +03:00
|
|
|
write_warn(str, reset);
|
|
|
|
if (!tail) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elen -= tail - einfo;
|
|
|
|
einfo = tail;
|
|
|
|
do ++tail; while (tail < einfo+elen && *tail == '\n');
|
2022-02-01 10:32:38 +03:00
|
|
|
write_warn2(str, einfo, tail-einfo);
|
2018-03-13 06:48:56 +03:00
|
|
|
elen -= tail - einfo;
|
|
|
|
einfo = tail;
|
|
|
|
}
|
2018-01-07 08:38:37 +03:00
|
|
|
}
|
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-01 11:03:58 +03:00
|
|
|
|
|
|
|
return str;
|
2017-02-22 11:50:25 +03:00
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2017-02-22 11:50:25 +03:00
|
|
|
static void
|
2020-12-27 14:50:15 +03:00
|
|
|
print_backtrace(const VALUE eclass, const VALUE errat, const VALUE str, int reverse, long backtrace_limit)
|
2017-02-22 11:50:25 +03:00
|
|
|
{
|
2006-12-31 18:02:22 +03:00
|
|
|
if (!NIL_P(errat)) {
|
|
|
|
long i;
|
|
|
|
long len = RARRAY_LEN(errat);
|
2017-05-19 10:12:45 +03:00
|
|
|
const int threshold = 1000000000;
|
2018-11-15 12:03:03 +03:00
|
|
|
int width = (len <= 1) ? INT_MIN : ((int)log10((double)(len > threshold ?
|
2017-05-19 10:12:45 +03:00
|
|
|
((len - 1) / threshold) :
|
|
|
|
len - 1)) +
|
2017-05-19 10:34:05 +03:00
|
|
|
(len < threshold ? 0 : 9) + 1);
|
2009-02-22 17:23:33 +03:00
|
|
|
|
2020-05-14 19:22:56 +03:00
|
|
|
long skip_start = -1, skip_len = 0;
|
|
|
|
|
|
|
|
// 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
|
2020-12-27 14:50:15 +03:00
|
|
|
if (backtrace_limit >= 0 && len > backtrace_limit + 2) {
|
|
|
|
skip_start = backtrace_limit + 1;
|
2020-12-21 05:33:22 +03:00
|
|
|
skip_len = len - skip_start;
|
2020-05-14 19:22:56 +03:00
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
for (i = 1; i < len; i++) {
|
2020-05-14 19:22:56 +03:00
|
|
|
if (i == skip_start) {
|
|
|
|
write_warn_str(str, rb_sprintf("\t ... %ld levels...\n", skip_len));
|
|
|
|
i += skip_len;
|
|
|
|
if (i >= len) break;
|
|
|
|
}
|
2017-02-22 11:50:25 +03:00
|
|
|
VALUE line = RARRAY_AREF(errat, reverse ? len - i : i);
|
2013-06-11 10:57:04 +04:00
|
|
|
if (RB_TYPE_P(line, T_STRING)) {
|
2017-12-12 14:47:16 +03:00
|
|
|
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));
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-22 11:50:25 +03:00
|
|
|
}
|
|
|
|
|
2022-02-01 11:59:31 +03:00
|
|
|
VALUE rb_get_detailed_message(VALUE exc, VALUE opt);
|
2018-10-28 00:45:30 +03:00
|
|
|
|
2018-12-22 10:14:14 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-28 00:45:30 +03:00
|
|
|
static void
|
2022-02-01 11:59:31 +03:00
|
|
|
show_cause(VALUE errinfo, VALUE str, VALUE opt, VALUE highlight, VALUE reverse, long backtrace_limit, VALUE *shown_causes)
|
2018-10-28 00:45:30 +03:00
|
|
|
{
|
|
|
|
VALUE cause = rb_attr_get(errinfo, id_cause);
|
2018-12-22 10:14:14 +03:00
|
|
|
if (!NIL_P(cause) && rb_obj_is_kind_of(cause, rb_eException) &&
|
|
|
|
!shown_cause_p(cause, shown_causes)) {
|
2018-10-28 00:45:30 +03:00
|
|
|
volatile VALUE eclass = CLASS_OF(cause);
|
|
|
|
VALUE errat = rb_get_backtrace(cause);
|
2022-02-01 11:59:31 +03:00
|
|
|
VALUE emesg = rb_get_detailed_message(cause, opt);
|
2018-10-28 00:45:30 +03:00
|
|
|
if (reverse) {
|
2022-02-01 11:59:31 +03:00
|
|
|
show_cause(cause, str, opt, highlight, reverse, backtrace_limit, shown_causes);
|
2020-12-27 14:50:15 +03:00
|
|
|
print_backtrace(eclass, errat, str, TRUE, backtrace_limit);
|
2022-02-01 09:58:39 +03:00
|
|
|
print_errinfo(eclass, errat, emesg, str, RTEST(highlight));
|
2018-10-28 00:45:30 +03:00
|
|
|
}
|
|
|
|
else {
|
2022-02-01 09:58:39 +03:00
|
|
|
print_errinfo(eclass, errat, emesg, str, RTEST(highlight));
|
2020-12-27 14:50:15 +03:00
|
|
|
print_backtrace(eclass, errat, str, FALSE, backtrace_limit);
|
2022-02-01 11:59:31 +03:00
|
|
|
show_cause(cause, str, opt, highlight, reverse, backtrace_limit, shown_causes);
|
2018-10-28 00:45:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 11:50:25 +03:00
|
|
|
void
|
2022-12-19 08:10:58 +03:00
|
|
|
rb_exc_check_circular_cause(VALUE exc)
|
|
|
|
{
|
|
|
|
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
|
2022-02-01 11:59:31 +03:00
|
|
|
rb_error_write(VALUE errinfo, VALUE emesg, VALUE errat, VALUE str, VALUE opt, VALUE highlight, VALUE reverse)
|
2017-02-22 11:50:25 +03:00
|
|
|
{
|
2018-04-11 11:03:43 +03:00
|
|
|
volatile VALUE eclass;
|
2018-12-22 10:14:14 +03:00
|
|
|
VALUE shown_causes = 0;
|
2020-12-27 14:50:15 +03:00
|
|
|
long backtrace_limit = rb_backtrace_length_limit;
|
2017-02-22 11:50:25 +03:00
|
|
|
|
|
|
|
if (NIL_P(errinfo))
|
|
|
|
return;
|
|
|
|
|
2022-11-15 07:24:08 +03:00
|
|
|
if (UNDEF_P(errat)) {
|
2017-02-22 11:50:25 +03:00
|
|
|
errat = Qnil;
|
|
|
|
}
|
2018-04-11 11:03:43 +03:00
|
|
|
eclass = CLASS_OF(errinfo);
|
2018-02-23 11:39:03 +03:00
|
|
|
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;
|
2022-02-01 09:58:39 +03:00
|
|
|
if (RTEST(highlight)) {
|
2018-02-23 11:39:03 +03:00
|
|
|
#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);
|
2022-02-01 11:59:31 +03:00
|
|
|
show_cause(errinfo, str, opt, highlight, reverse, backtrace_limit, &shown_causes);
|
2020-12-27 14:50:15 +03:00
|
|
|
print_backtrace(eclass, errat, str, TRUE, backtrace_limit);
|
2022-02-01 09:58:39 +03:00
|
|
|
print_errinfo(eclass, errat, emesg, str, RTEST(highlight));
|
2017-02-22 11:50:25 +03:00
|
|
|
}
|
|
|
|
else {
|
2022-02-01 09:58:39 +03:00
|
|
|
print_errinfo(eclass, errat, emesg, str, RTEST(highlight));
|
2020-12-27 14:50:15 +03:00
|
|
|
print_backtrace(eclass, errat, str, FALSE, backtrace_limit);
|
2022-02-01 11:59:31 +03:00
|
|
|
show_cause(errinfo, str, opt, highlight, reverse, backtrace_limit, &shown_causes);
|
2017-02-22 11:50:25 +03:00
|
|
|
}
|
2017-12-12 14:47:16 +03:00
|
|
|
}
|
|
|
|
|
2022-11-11 04:37:34 +03:00
|
|
|
static void
|
2022-11-25 08:20:46 +03:00
|
|
|
rb_ec_error_print_detailed(rb_execution_context_t *const ec, const VALUE errinfo, const VALUE str, VALUE emesg0)
|
2017-12-12 14:47:16 +03:00
|
|
|
{
|
2018-10-19 23:56:10 +03:00
|
|
|
volatile uint8_t raised_flag = ec->raised_flag;
|
2018-01-02 09:42:03 +03:00
|
|
|
volatile VALUE errat = Qundef;
|
2020-07-15 12:46:07 +03:00
|
|
|
volatile bool written = false;
|
2022-11-25 08:20:46 +03:00
|
|
|
volatile VALUE emesg = emesg0;
|
2017-12-12 14:47:16 +03:00
|
|
|
|
2022-02-01 11:59:31 +03:00
|
|
|
VALUE opt = rb_hash_new();
|
|
|
|
VALUE highlight = rb_stderr_tty_p() ? Qtrue : Qfalse;
|
|
|
|
rb_hash_aset(opt, ID2SYM(rb_intern_const("highlight")), highlight);
|
|
|
|
|
2017-12-12 14:47:16 +03:00
|
|
|
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);
|
|
|
|
}
|
2022-11-15 07:24:08 +03:00
|
|
|
if (UNDEF_P(emesg)) {
|
2018-04-11 11:03:43 +03:00
|
|
|
emesg = Qnil;
|
2022-02-01 11:59:31 +03:00
|
|
|
emesg = rb_get_detailed_message(errinfo, opt);
|
2018-04-11 11:03:43 +03:00
|
|
|
}
|
2017-12-12 14:47:16 +03:00
|
|
|
|
2020-07-15 12:46:07 +03:00
|
|
|
if (!written) {
|
|
|
|
written = true;
|
2022-11-11 04:37:34 +03:00
|
|
|
rb_error_write(errinfo, emesg, errat, str, opt, highlight, Qfalse);
|
2020-07-15 12:46:07 +03:00
|
|
|
}
|
2017-12-12 14:47:16 +03:00
|
|
|
|
2017-10-26 14:02:13 +03:00
|
|
|
EC_POP_TAG();
|
2017-10-29 17:06:58 +03:00
|
|
|
ec->errinfo = errinfo;
|
2017-11-07 08:22:09 +03:00
|
|
|
rb_ec_raised_set(ec, raised_flag);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
2022-11-11 04:37:34 +03:00
|
|
|
void
|
|
|
|
rb_ec_error_print(rb_execution_context_t *volatile ec, volatile VALUE errinfo)
|
|
|
|
{
|
|
|
|
rb_ec_error_print_detailed(ec, errinfo, Qnil, Qundef);
|
|
|
|
}
|
|
|
|
|
2018-10-13 12:59:22 +03:00
|
|
|
#define undef_mesg_for(v, k) rb_fstring_lit("undefined"v" method `%1$s' for "k" `%2$s'")
|
2015-10-28 09:24:12 +03:00
|
|
|
#define undef_mesg(v) ( \
|
|
|
|
is_mod ? \
|
|
|
|
undef_mesg_for(v, "module") : \
|
|
|
|
undef_mesg_for(v, "class"))
|
2014-07-26 20:13:21 +04:00
|
|
|
|
|
|
|
void
|
2016-01-03 07:59:54 +03:00
|
|
|
rb_print_undef(VALUE klass, ID id, rb_method_visibility_t visi)
|
2014-07-26 20:13:21 +04:00
|
|
|
{
|
2015-10-28 09:24:12 +03:00
|
|
|
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));
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
2011-07-26 20:05:35 +04:00
|
|
|
void
|
|
|
|
rb_print_undef_str(VALUE klass, VALUE name)
|
|
|
|
{
|
2015-10-28 09:24:12 +03:00
|
|
|
const int is_mod = RB_TYPE_P(klass, T_MODULE);
|
|
|
|
rb_name_err_raise_str(undef_mesg(""), klass, name);
|
2011-07-26 20:05:35 +04:00
|
|
|
}
|
|
|
|
|
2018-10-13 12:59:22 +03:00
|
|
|
#define inaccessible_mesg_for(v, k) rb_fstring_lit("method `%1$s' for "k" `%2$s' is "v)
|
2015-10-28 09:24:12 +03:00
|
|
|
#define inaccessible_mesg(v) ( \
|
|
|
|
is_mod ? \
|
|
|
|
inaccessible_mesg_for(v, "module") : \
|
|
|
|
inaccessible_mesg_for(v, "class"))
|
|
|
|
|
2014-07-26 20:13:21 +04:00
|
|
|
void
|
2015-06-03 04:39:16 +03:00
|
|
|
rb_print_inaccessible(VALUE klass, ID id, rb_method_visibility_t visi)
|
2014-07-26 20:13:21 +04:00
|
|
|
{
|
2015-10-28 09:24:12 +03:00
|
|
|
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;
|
2021-05-03 16:38:51 +03:00
|
|
|
case METHOD_VISI_PRIVATE: mesg = inaccessible_mesg("private"); break;
|
|
|
|
case METHOD_VISI_PROTECTED: mesg = inaccessible_mesg("protected"); break;
|
2015-10-28 09:24:12 +03:00
|
|
|
default: UNREACHABLE;
|
|
|
|
}
|
|
|
|
rb_name_err_raise_str(mesg, klass, ID2SYM(id));
|
2014-07-26 20:13:21 +04:00
|
|
|
}
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
static int
|
|
|
|
sysexit_status(VALUE err)
|
|
|
|
{
|
2015-02-23 10:05:30 +03:00
|
|
|
VALUE st = rb_ivar_get(err, id_status);
|
2006-12-31 18:02:22 +03:00
|
|
|
return NUM2INT(st);
|
|
|
|
}
|
|
|
|
|
2022-11-11 04:37:34 +03:00
|
|
|
enum {
|
|
|
|
EXITING_WITH_MESSAGE = 1,
|
|
|
|
EXITING_WITH_STATUS = 2,
|
|
|
|
EXITING_WITH_SIGNAL = 4
|
|
|
|
};
|
|
|
|
static int
|
|
|
|
exiting_split(VALUE errinfo, volatile int *exitcode, volatile int *sigstatus)
|
|
|
|
{
|
|
|
|
int ex = EXIT_SUCCESS;
|
|
|
|
VALUE signo;
|
|
|
|
int sig = 0;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
if (NIL_P(errinfo)) return 0;
|
|
|
|
|
|
|
|
if (rb_obj_is_kind_of(errinfo, rb_eSystemExit)) {
|
|
|
|
ex = sysexit_status(errinfo);
|
|
|
|
result |= EXITING_WITH_STATUS;
|
|
|
|
}
|
|
|
|
else if (rb_obj_is_kind_of(errinfo, rb_eSignal)) {
|
|
|
|
signo = rb_ivar_get(errinfo, id_signo);
|
|
|
|
sig = FIX2INT(signo);
|
|
|
|
result |= EXITING_WITH_SIGNAL;
|
|
|
|
/* no message when exiting by signal */
|
|
|
|
if (signo == INT2FIX(SIGSEGV) || !rb_obj_is_instance_of(errinfo, rb_eSignal))
|
|
|
|
/* except for SEGV and subclasses */
|
|
|
|
result |= EXITING_WITH_MESSAGE;
|
|
|
|
}
|
|
|
|
else if (rb_obj_is_kind_of(errinfo, rb_eSystemCallError) &&
|
|
|
|
FIXNUM_P(signo = rb_attr_get(errinfo, id_signo))) {
|
|
|
|
sig = FIX2INT(signo);
|
|
|
|
result |= EXITING_WITH_SIGNAL;
|
|
|
|
/* no message when exiting by error to be mapped to signal */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ex = EXIT_FAILURE;
|
|
|
|
result |= EXITING_WITH_STATUS | EXITING_WITH_MESSAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exitcode && (result & EXITING_WITH_STATUS))
|
|
|
|
*exitcode = ex;
|
|
|
|
if (sigstatus && (result & EXITING_WITH_SIGNAL))
|
|
|
|
*sigstatus = sig;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-07-19 03:25:45 +03:00
|
|
|
#define unknown_longjmp_status(status) \
|
|
|
|
rb_bug("Unknown longjmp status %d", status)
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
static int
|
2022-11-11 04:37:34 +03:00
|
|
|
error_handle(rb_execution_context_t *ec, VALUE errinfo, enum ruby_tag_type ex)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
int status = EXIT_FAILURE;
|
|
|
|
|
2017-11-07 08:12:39 +03:00
|
|
|
if (rb_ec_set_raised(ec))
|
2006-12-31 18:02:22 +03:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
switch (ex & TAG_MASK) {
|
* call_cfunc.ci, compile.c, compile.h, debug.h, eval.c,
eval_error.h, eval_jump.h, eval_load.c, eval_thread.c, gc.c,
insnhelper.h, insns.def, iseq.c, main.c, numeric.c, parse.y,
range.c, regenc.h, ruby.h, signal.c, thread.c, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fixed indents and non-C90 comments.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11620 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-02 19:26:04 +03:00
|
|
|
case 0:
|
2006-12-31 18:02:22 +03:00
|
|
|
status = EXIT_SUCCESS;
|
|
|
|
break;
|
|
|
|
|
* call_cfunc.ci, compile.c, compile.h, debug.h, eval.c,
eval_error.h, eval_jump.h, eval_load.c, eval_thread.c, gc.c,
insnhelper.h, insns.def, iseq.c, main.c, numeric.c, parse.y,
range.c, regenc.h, ruby.h, signal.c, thread.c, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fixed indents and non-C90 comments.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11620 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-02 19:26:04 +03:00
|
|
|
case TAG_RETURN:
|
2017-12-12 14:47:16 +03:00
|
|
|
error_pos(Qnil);
|
2016-02-25 07:42:16 +03:00
|
|
|
warn_print("unexpected return\n");
|
2006-12-31 18:02:22 +03:00
|
|
|
break;
|
* call_cfunc.ci, compile.c, compile.h, debug.h, eval.c,
eval_error.h, eval_jump.h, eval_load.c, eval_thread.c, gc.c,
insnhelper.h, insns.def, iseq.c, main.c, numeric.c, parse.y,
range.c, regenc.h, ruby.h, signal.c, thread.c, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fixed indents and non-C90 comments.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11620 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-02 19:26:04 +03:00
|
|
|
case TAG_NEXT:
|
2017-12-12 14:47:16 +03:00
|
|
|
error_pos(Qnil);
|
2016-02-25 07:42:16 +03:00
|
|
|
warn_print("unexpected next\n");
|
2006-12-31 18:02:22 +03:00
|
|
|
break;
|
* call_cfunc.ci, compile.c, compile.h, debug.h, eval.c,
eval_error.h, eval_jump.h, eval_load.c, eval_thread.c, gc.c,
insnhelper.h, insns.def, iseq.c, main.c, numeric.c, parse.y,
range.c, regenc.h, ruby.h, signal.c, thread.c, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fixed indents and non-C90 comments.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11620 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-02 19:26:04 +03:00
|
|
|
case TAG_BREAK:
|
2017-12-12 14:47:16 +03:00
|
|
|
error_pos(Qnil);
|
2016-02-25 07:42:16 +03:00
|
|
|
warn_print("unexpected break\n");
|
2006-12-31 18:02:22 +03:00
|
|
|
break;
|
* call_cfunc.ci, compile.c, compile.h, debug.h, eval.c,
eval_error.h, eval_jump.h, eval_load.c, eval_thread.c, gc.c,
insnhelper.h, insns.def, iseq.c, main.c, numeric.c, parse.y,
range.c, regenc.h, ruby.h, signal.c, thread.c, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fixed indents and non-C90 comments.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11620 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-02 19:26:04 +03:00
|
|
|
case TAG_REDO:
|
2017-12-12 14:47:16 +03:00
|
|
|
error_pos(Qnil);
|
2016-02-25 07:42:16 +03:00
|
|
|
warn_print("unexpected redo\n");
|
2006-12-31 18:02:22 +03:00
|
|
|
break;
|
* call_cfunc.ci, compile.c, compile.h, debug.h, eval.c,
eval_error.h, eval_jump.h, eval_load.c, eval_thread.c, gc.c,
insnhelper.h, insns.def, iseq.c, main.c, numeric.c, parse.y,
range.c, regenc.h, ruby.h, signal.c, thread.c, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fixed indents and non-C90 comments.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11620 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-02 19:26:04 +03:00
|
|
|
case TAG_RETRY:
|
2017-12-12 14:47:16 +03:00
|
|
|
error_pos(Qnil);
|
2016-02-25 07:42:16 +03:00
|
|
|
warn_print("retry outside of rescue clause\n");
|
2006-12-31 18:02:22 +03:00
|
|
|
break;
|
* call_cfunc.ci, compile.c, compile.h, debug.h, eval.c,
eval_error.h, eval_jump.h, eval_load.c, eval_thread.c, gc.c,
insnhelper.h, insns.def, iseq.c, main.c, numeric.c, parse.y,
range.c, regenc.h, ruby.h, signal.c, thread.c, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fixed indents and non-C90 comments.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11620 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-02 19:26:04 +03:00
|
|
|
case TAG_THROW:
|
|
|
|
/* TODO: fix me */
|
2017-12-12 14:47:16 +03:00
|
|
|
error_pos(Qnil);
|
2016-02-25 07:42:16 +03:00
|
|
|
warn_print("unexpected throw\n");
|
2006-12-31 18:02:22 +03:00
|
|
|
break;
|
2022-11-11 04:37:34 +03:00
|
|
|
case TAG_RAISE:
|
|
|
|
if (!(exiting_split(errinfo, &status, NULL) & EXITING_WITH_MESSAGE)) {
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
}
|
2022-11-11 04:37:34 +03:00
|
|
|
/* fallthrough */
|
2008-05-27 17:10:46 +04:00
|
|
|
case TAG_FATAL:
|
2022-11-11 04:37:34 +03:00
|
|
|
rb_ec_error_print(ec, errinfo);
|
2008-05-27 17:10:46 +04:00
|
|
|
break;
|
* call_cfunc.ci, compile.c, compile.h, debug.h, eval.c,
eval_error.h, eval_jump.h, eval_load.c, eval_thread.c, gc.c,
insnhelper.h, insns.def, iseq.c, main.c, numeric.c, parse.y,
range.c, regenc.h, ruby.h, signal.c, thread.c, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fixed indents and non-C90 comments.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11620 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-02 19:26:04 +03:00
|
|
|
default:
|
2015-07-19 03:25:45 +03:00
|
|
|
unknown_longjmp_status(ex);
|
2006-12-31 18:02:22 +03:00
|
|
|
break;
|
|
|
|
}
|
2017-11-07 07:01:13 +03:00
|
|
|
rb_ec_reset_raised(ec);
|
2006-12-31 18:02:22 +03:00
|
|
|
return status;
|
|
|
|
}
|