Get rid of type-punning pointer casts
[ruby.git] / error.c
blob9890fddcef3777181f47d2719d0b5fa2e60f6472
1 /**********************************************************************
3 error.c -
5 $Author$
6 created at: Mon Aug 9 16:11:34 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/internal/config.h"
14 #include <errno.h>
15 #include <stdarg.h>
16 #include <stdio.h>
18 #ifdef HAVE_STDLIB_H
19 # include <stdlib.h>
20 #endif
22 #ifdef HAVE_UNISTD_H
23 # include <unistd.h>
24 #endif
26 #ifdef HAVE_SYS_WAIT_H
27 # include <sys/wait.h>
28 #endif
30 #if defined __APPLE__
31 # include <AvailabilityMacros.h>
32 #endif
34 #include "internal.h"
35 #include "internal/class.h"
36 #include "internal/error.h"
37 #include "internal/eval.h"
38 #include "internal/hash.h"
39 #include "internal/io.h"
40 #include "internal/load.h"
41 #include "internal/object.h"
42 #include "internal/process.h"
43 #include "internal/string.h"
44 #include "internal/symbol.h"
45 #include "internal/thread.h"
46 #include "internal/variable.h"
47 #include "ruby/encoding.h"
48 #include "ruby/st.h"
49 #include "ruby/util.h"
50 #include "ruby_assert.h"
51 #include "vm_core.h"
52 #include "yjit.h"
54 #include "builtin.h"
56 /*!
57 * \addtogroup exception
58 * \{
61 #ifndef EXIT_SUCCESS
62 #define EXIT_SUCCESS 0
63 #endif
65 #ifndef WIFEXITED
66 #define WIFEXITED(status) 1
67 #endif
69 #ifndef WEXITSTATUS
70 #define WEXITSTATUS(status) (status)
71 #endif
73 VALUE rb_iseqw_local_variables(VALUE iseqval);
74 VALUE rb_iseqw_new(const rb_iseq_t *);
75 int rb_str_end_with_asciichar(VALUE str, int c);
77 long rb_backtrace_length_limit = -1;
78 VALUE rb_eEAGAIN;
79 VALUE rb_eEWOULDBLOCK;
80 VALUE rb_eEINPROGRESS;
81 static VALUE rb_mWarning;
82 static VALUE rb_cWarningBuffer;
84 static ID id_warn;
85 static ID id_category;
86 static ID id_deprecated;
87 static ID id_experimental;
88 static ID id_performance;
89 static VALUE sym_category;
90 static VALUE sym_highlight;
91 static struct {
92 st_table *id2enum, *enum2id;
93 } warning_categories;
95 extern const char *rb_dynamic_description;
97 static const char *
98 rb_strerrno(int err)
100 #define defined_error(name, num) if (err == (num)) return (name);
101 #define undefined_error(name)
102 #include "known_errors.inc"
103 #undef defined_error
104 #undef undefined_error
105 return NULL;
108 static int
109 err_position_0(char *buf, long len, const char *file, int line)
111 if (!file) {
112 return 0;
114 else if (line == 0) {
115 return snprintf(buf, len, "%s: ", file);
117 else {
118 return snprintf(buf, len, "%s:%d: ", file, line);
122 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 5, 0)
123 static VALUE
124 err_vcatf(VALUE str, const char *pre, const char *file, int line,
125 const char *fmt, va_list args)
127 if (file) {
128 rb_str_cat2(str, file);
129 if (line) rb_str_catf(str, ":%d", line);
130 rb_str_cat2(str, ": ");
132 if (pre) rb_str_cat2(str, pre);
133 rb_str_vcatf(str, fmt, args);
134 return str;
137 static VALUE syntax_error_with_path(VALUE, VALUE, VALUE*, rb_encoding*);
139 VALUE
140 rb_syntax_error_append(VALUE exc, VALUE file, int line, int column,
141 rb_encoding *enc, const char *fmt, va_list args)
143 const char *fn = NIL_P(file) ? NULL : RSTRING_PTR(file);
144 if (!exc) {
145 VALUE mesg = rb_enc_str_new(0, 0, enc);
146 err_vcatf(mesg, NULL, fn, line, fmt, args);
147 rb_str_cat2(mesg, "\n");
148 rb_write_error_str(mesg);
150 else {
151 VALUE mesg;
152 exc = syntax_error_with_path(exc, file, &mesg, enc);
153 err_vcatf(mesg, NULL, fn, line, fmt, args);
156 return exc;
159 static unsigned int warning_disabled_categories = (
160 (1U << RB_WARN_CATEGORY_DEPRECATED) |
161 ~RB_WARN_CATEGORY_DEFAULT_BITS);
163 static unsigned int
164 rb_warning_category_mask(VALUE category)
166 return 1U << rb_warning_category_from_name(category);
169 rb_warning_category_t
170 rb_warning_category_from_name(VALUE category)
172 st_data_t cat_value;
173 ID cat_id;
174 Check_Type(category, T_SYMBOL);
175 if (!(cat_id = rb_check_id(&category)) ||
176 !st_lookup(warning_categories.id2enum, cat_id, &cat_value)) {
177 rb_raise(rb_eArgError, "unknown category: %"PRIsVALUE, category);
179 return (rb_warning_category_t)cat_value;
182 static VALUE
183 rb_warning_category_to_name(rb_warning_category_t category)
185 st_data_t id;
186 if (!st_lookup(warning_categories.enum2id, category, &id)) {
187 rb_raise(rb_eArgError, "invalid category: %d", (int)category);
189 return id ? ID2SYM(id) : Qnil;
192 void
193 rb_warning_category_update(unsigned int mask, unsigned int bits)
195 warning_disabled_categories &= ~mask;
196 warning_disabled_categories |= mask & ~bits;
199 bool
200 rb_warning_category_enabled_p(rb_warning_category_t category)
202 return !(warning_disabled_categories & (1U << category));
206 * call-seq:
207 * Warning[category] -> true or false
209 * Returns the flag to show the warning messages for +category+.
210 * Supported categories are:
212 * +:deprecated+ ::
213 * deprecation warnings
214 * * assignment of non-nil value to <code>$,</code> and <code>$;</code>
215 * * keyword arguments
216 * etc.
218 * +:experimental+ ::
219 * experimental features
220 * * Pattern matching
222 * +:performance+ ::
223 * performance hints
224 * * Shape variation limit
227 static VALUE
228 rb_warning_s_aref(VALUE mod, VALUE category)
230 rb_warning_category_t cat = rb_warning_category_from_name(category);
231 return RBOOL(rb_warning_category_enabled_p(cat));
235 * call-seq:
236 * Warning[category] = flag -> flag
238 * Sets the warning flags for +category+.
239 * See Warning.[] for the categories.
242 static VALUE
243 rb_warning_s_aset(VALUE mod, VALUE category, VALUE flag)
245 unsigned int mask = rb_warning_category_mask(category);
246 unsigned int disabled = warning_disabled_categories;
247 if (!RTEST(flag))
248 disabled |= mask;
249 else
250 disabled &= ~mask;
251 warning_disabled_categories = disabled;
252 return flag;
256 * call-seq:
257 * categories -> array
259 * Returns a list of the supported category symbols.
262 static VALUE
263 rb_warning_s_categories(VALUE mod)
265 st_index_t num = warning_categories.id2enum->num_entries;
266 ID *ids = ALLOCA_N(ID, num);
267 num = st_keys(warning_categories.id2enum, ids, num);
268 VALUE ary = rb_ary_new_capa(num);
269 for (st_index_t i = 0; i < num; ++i) {
270 rb_ary_push(ary, ID2SYM(ids[i]));
272 return rb_ary_freeze(ary);
276 * call-seq:
277 * warn(msg, category: nil) -> nil
279 * Writes warning message +msg+ to $stderr. This method is called by
280 * Ruby for all emitted warnings. A +category+ may be included with
281 * the warning.
283 * See the documentation of the Warning module for how to customize this.
286 static VALUE
287 rb_warning_s_warn(int argc, VALUE *argv, VALUE mod)
289 VALUE str;
290 VALUE opt;
291 VALUE category = Qnil;
293 rb_scan_args(argc, argv, "1:", &str, &opt);
294 if (!NIL_P(opt)) rb_get_kwargs(opt, &id_category, 0, 1, &category);
296 Check_Type(str, T_STRING);
297 rb_must_asciicompat(str);
298 if (!NIL_P(category)) {
299 rb_warning_category_t cat = rb_warning_category_from_name(category);
300 if (!rb_warning_category_enabled_p(cat)) return Qnil;
302 rb_write_error_str(str);
303 return Qnil;
307 * Document-module: Warning
309 * The Warning module contains a single method named #warn, and the
310 * module extends itself, making Warning.warn available.
311 * Warning.warn is called for all warnings issued by Ruby.
312 * By default, warnings are printed to $stderr.
314 * Changing the behavior of Warning.warn is useful to customize how warnings are
315 * handled by Ruby, for instance by filtering some warnings, and/or outputting
316 * warnings somewhere other than <tt>$stderr</tt>.
318 * If you want to change the behavior of Warning.warn you should use
319 * <tt>Warning.extend(MyNewModuleWithWarnMethod)</tt> and you can use +super+
320 * to get the default behavior of printing the warning to <tt>$stderr</tt>.
322 * Example:
323 * module MyWarningFilter
324 * def warn(message, category: nil, **kwargs)
325 * if /some warning I want to ignore/.match?(message)
326 * # ignore
327 * else
328 * super
329 * end
330 * end
331 * end
332 * Warning.extend MyWarningFilter
334 * You should never redefine Warning#warn (the instance method), as that will
335 * then no longer provide a way to use the default behavior.
337 * The warning[https://rubygems.org/gems/warning] gem provides convenient ways to customize Warning.warn.
340 static VALUE
341 rb_warning_warn(VALUE mod, VALUE str)
343 return rb_funcallv(mod, id_warn, 1, &str);
347 static int
348 rb_warning_warn_arity(void)
350 const rb_method_entry_t *me = rb_method_entry(rb_singleton_class(rb_mWarning), id_warn);
351 return me ? rb_method_entry_arity(me) : 1;
354 static VALUE
355 rb_warn_category(VALUE str, VALUE category)
357 if (RUBY_DEBUG && !NIL_P(category)) {
358 rb_warning_category_from_name(category);
361 if (rb_warning_warn_arity() == 1) {
362 return rb_warning_warn(rb_mWarning, str);
364 else {
365 VALUE args[2];
366 args[0] = str;
367 args[1] = rb_hash_new();
368 rb_hash_aset(args[1], sym_category, category);
369 return rb_funcallv_kw(rb_mWarning, id_warn, 2, args, RB_PASS_KEYWORDS);
373 static void
374 rb_write_warning_str(VALUE str)
376 rb_warning_warn(rb_mWarning, str);
379 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 4, 0)
380 static VALUE
381 warn_vsprintf(rb_encoding *enc, const char *file, int line, const char *fmt, va_list args)
383 VALUE str = rb_enc_str_new(0, 0, enc);
385 err_vcatf(str, "warning: ", file, line, fmt, args);
386 return rb_str_cat2(str, "\n");
389 #define with_warn_vsprintf(enc, file, line, fmt) \
390 VALUE str; \
391 va_list args; \
392 va_start(args, fmt); \
393 str = warn_vsprintf(enc, file, line, fmt, args); \
394 va_end(args);
396 void
397 rb_compile_warn(const char *file, int line, const char *fmt, ...)
399 if (!NIL_P(ruby_verbose)) {
400 with_warn_vsprintf(NULL, file, line, fmt) {
401 rb_write_warning_str(str);
406 void
407 rb_enc_compile_warn(rb_encoding *enc, const char *file, int line, const char *fmt, ...)
409 if (!NIL_P(ruby_verbose)) {
410 with_warn_vsprintf(enc, file, line, fmt) {
411 rb_write_warning_str(str);
416 /* rb_compile_warning() reports only in verbose mode */
417 void
418 rb_compile_warning(const char *file, int line, const char *fmt, ...)
420 if (RTEST(ruby_verbose)) {
421 with_warn_vsprintf(NULL, file, line, fmt) {
422 rb_write_warning_str(str);
427 /* rb_enc_compile_warning() reports only in verbose mode */
428 void
429 rb_enc_compile_warning(rb_encoding *enc, const char *file, int line, const char *fmt, ...)
431 if (RTEST(ruby_verbose)) {
432 with_warn_vsprintf(enc, file, line, fmt) {
433 rb_write_warning_str(str);
438 void
439 rb_category_compile_warn(rb_warning_category_t category, const char *file, int line, const char *fmt, ...)
441 if (!NIL_P(ruby_verbose)) {
442 with_warn_vsprintf(NULL, file, line, fmt) {
443 rb_warn_category(str, rb_warning_category_to_name(category));
448 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 0)
449 static VALUE
450 warning_string(rb_encoding *enc, const char *fmt, va_list args)
452 int line;
453 const char *file = rb_source_location_cstr(&line);
454 return warn_vsprintf(enc, file, line, fmt, args);
457 #define with_warning_string(mesg, enc, fmt) \
458 with_warning_string_from(mesg, enc, fmt, fmt)
459 #define with_warning_string_from(mesg, enc, fmt, last_arg) \
460 VALUE mesg; \
461 va_list args; va_start(args, last_arg); \
462 mesg = warning_string(enc, fmt, args); \
463 va_end(args);
465 void
466 rb_warn(const char *fmt, ...)
468 if (!NIL_P(ruby_verbose)) {
469 with_warning_string(mesg, 0, fmt) {
470 rb_write_warning_str(mesg);
475 void
476 rb_category_warn(rb_warning_category_t category, const char *fmt, ...)
478 if (!NIL_P(ruby_verbose)) {
479 with_warning_string(mesg, 0, fmt) {
480 rb_warn_category(mesg, rb_warning_category_to_name(category));
485 void
486 rb_enc_warn(rb_encoding *enc, const char *fmt, ...)
488 if (!NIL_P(ruby_verbose)) {
489 with_warning_string(mesg, enc, fmt) {
490 rb_write_warning_str(mesg);
495 /* rb_warning() reports only in verbose mode */
496 void
497 rb_warning(const char *fmt, ...)
499 if (RTEST(ruby_verbose)) {
500 with_warning_string(mesg, 0, fmt) {
501 rb_write_warning_str(mesg);
506 /* rb_category_warning() reports only in verbose mode */
507 void
508 rb_category_warning(rb_warning_category_t category, const char *fmt, ...)
510 if (RTEST(ruby_verbose)) {
511 with_warning_string(mesg, 0, fmt) {
512 rb_warn_category(mesg, rb_warning_category_to_name(category));
517 VALUE
518 rb_warning_string(const char *fmt, ...)
520 with_warning_string(mesg, 0, fmt) {
522 return mesg;
525 #if 0
526 void
527 rb_enc_warning(rb_encoding *enc, const char *fmt, ...)
529 if (RTEST(ruby_verbose)) {
530 with_warning_string(mesg, enc, fmt) {
531 rb_write_warning_str(mesg);
535 #endif
537 static bool
538 deprecation_warning_enabled(void)
540 if (NIL_P(ruby_verbose)) return false;
541 if (!rb_warning_category_enabled_p(RB_WARN_CATEGORY_DEPRECATED)) return false;
542 return true;
545 static void
546 warn_deprecated(VALUE mesg, const char *removal, const char *suggest)
548 rb_str_set_len(mesg, RSTRING_LEN(mesg) - 1);
549 rb_str_cat_cstr(mesg, " is deprecated");
550 if (removal) {
551 rb_str_catf(mesg, " and will be removed in Ruby %s", removal);
553 if (suggest) rb_str_catf(mesg, "; use %s instead", suggest);
554 rb_str_cat_cstr(mesg, "\n");
555 rb_warn_category(mesg, ID2SYM(id_deprecated));
558 void
559 rb_warn_deprecated(const char *fmt, const char *suggest, ...)
561 if (!deprecation_warning_enabled()) return;
563 with_warning_string_from(mesg, 0, fmt, suggest) {
564 warn_deprecated(mesg, NULL, suggest);
568 void
569 rb_warn_deprecated_to_remove(const char *removal, const char *fmt, const char *suggest, ...)
571 if (!deprecation_warning_enabled()) return;
573 with_warning_string_from(mesg, 0, fmt, suggest) {
574 warn_deprecated(mesg, removal, suggest);
578 static inline int
579 end_with_asciichar(VALUE str, int c)
581 return RB_TYPE_P(str, T_STRING) &&
582 rb_str_end_with_asciichar(str, c);
585 /* :nodoc: */
586 static VALUE
587 warning_write(int argc, VALUE *argv, VALUE buf)
589 while (argc-- > 0) {
590 rb_str_append(buf, *argv++);
592 return buf;
595 VALUE rb_ec_backtrace_location_ary(const rb_execution_context_t *ec, long lev, long n, bool skip_internal);
597 static VALUE
598 rb_warn_m(rb_execution_context_t *ec, VALUE exc, VALUE msgs, VALUE uplevel, VALUE category)
600 VALUE location = Qnil;
601 int argc = RARRAY_LENINT(msgs);
602 const VALUE *argv = RARRAY_CONST_PTR(msgs);
604 if (!NIL_P(ruby_verbose) && argc > 0) {
605 VALUE str = argv[0];
606 if (!NIL_P(uplevel)) {
607 long lev = NUM2LONG(uplevel);
608 if (lev < 0) {
609 rb_raise(rb_eArgError, "negative level (%ld)", lev);
611 location = rb_ec_backtrace_location_ary(ec, lev + 1, 1, TRUE);
612 if (!NIL_P(location)) {
613 location = rb_ary_entry(location, 0);
616 if (argc > 1 || !NIL_P(uplevel) || !end_with_asciichar(str, '\n')) {
617 VALUE path;
618 if (NIL_P(uplevel)) {
619 str = rb_str_tmp_new(0);
621 else if (NIL_P(location) ||
622 NIL_P(path = rb_funcall(location, rb_intern("path"), 0))) {
623 str = rb_str_new_cstr("warning: ");
625 else {
626 str = rb_sprintf("%s:%ld: warning: ",
627 rb_string_value_ptr(&path),
628 NUM2LONG(rb_funcall(location, rb_intern("lineno"), 0)));
630 RBASIC_SET_CLASS(str, rb_cWarningBuffer);
631 rb_io_puts(argc, argv, str);
632 RBASIC_SET_CLASS(str, rb_cString);
635 if (!NIL_P(category)) {
636 category = rb_to_symbol_type(category);
637 rb_warning_category_from_name(category);
640 if (exc == rb_mWarning) {
641 rb_must_asciicompat(str);
642 rb_write_error_str(str);
644 else {
645 rb_warn_category(str, category);
648 return Qnil;
651 #define MAX_BUG_REPORTERS 0x100
653 static struct bug_reporters {
654 void (*func)(FILE *out, void *data);
655 void *data;
656 } bug_reporters[MAX_BUG_REPORTERS];
658 static int bug_reporters_size;
661 rb_bug_reporter_add(void (*func)(FILE *, void *), void *data)
663 struct bug_reporters *reporter;
664 if (bug_reporters_size >= MAX_BUG_REPORTERS) {
665 return 0; /* failed to register */
667 reporter = &bug_reporters[bug_reporters_size++];
668 reporter->func = func;
669 reporter->data = data;
671 return 1;
674 /* returns true if x can not be used as file name */
675 static bool
676 path_sep_p(char x)
678 #if defined __CYGWIN__ || defined DOSISH
679 # define PATH_SEP_ENCODING 1
680 // Assume that "/" is only the first byte in any encoding.
681 if (x == ':') return true; // drive letter or ADS
682 if (x == '\\') return true;
683 #endif
684 return x == '/';
687 struct path_string {
688 const char *ptr;
689 size_t len;
692 static const char PATHSEP_REPLACE = '!';
694 static char *
695 append_pathname(char *p, const char *pe, VALUE str)
697 #ifdef PATH_SEP_ENCODING
698 rb_encoding *enc = rb_enc_get(str);
699 #endif
700 const char *s = RSTRING_PTR(str);
701 const char *const se = s + RSTRING_LEN(str);
702 char c;
704 --pe; // for terminator
706 while (p < pe && s < se && (c = *s) != '\0') {
707 if (c == '.') {
708 if (s == se || !*s) break; // chomp "." basename
709 if (path_sep_p(s[1])) goto skipsep; // skip "./"
711 else if (path_sep_p(c)) {
712 // squeeze successive separators
713 *p++ = PATHSEP_REPLACE;
714 skipsep:
715 while (++s < se && path_sep_p(*s));
716 continue;
718 const char *const ss = s;
719 while (p < pe && s < se && *s && !path_sep_p(*s)) {
720 #ifdef PATH_SEP_ENCODING
721 int n = rb_enc_mbclen(s, se, enc);
722 #else
723 const int n = 1;
724 #endif
725 p += n;
726 s += n;
728 if (s > ss) memcpy(p - (s - ss), ss, s - ss);
731 return p;
734 static char *
735 append_basename(char *p, const char *pe, struct path_string *path, VALUE str)
737 if (!path->ptr) {
738 #ifdef PATH_SEP_ENCODING
739 rb_encoding *enc = rb_enc_get(str);
740 #endif
741 const char *const b = RSTRING_PTR(str), *const e = RSTRING_END(str), *p = e;
743 while (p > b) {
744 if (path_sep_p(p[-1])) {
745 #ifdef PATH_SEP_ENCODING
746 const char *t = rb_enc_prev_char(b, p, e, enc);
747 if (t == p-1) break;
748 p = t;
749 #else
750 break;
751 #endif
753 else {
754 --p;
758 path->ptr = p;
759 path->len = e - p;
761 size_t n = path->len;
762 if (p + n > pe) n = pe - p;
763 memcpy(p, path->ptr, n);
764 return p + n;
767 static void
768 finish_report(FILE *out, rb_pid_t pid)
770 if (out != stdout && out != stderr) fclose(out);
771 #ifdef HAVE_WORKING_FORK
772 if (pid > 0) waitpid(pid, NULL, 0);
773 #endif
776 struct report_expansion {
777 struct path_string exe, script;
778 rb_pid_t pid;
779 time_t time;
783 * Open a bug report file to write. The `RUBY_CRASH_REPORT`
784 * environment variable can be set to define a template that is used
785 * to name bug report files. The template can contain % specifiers
786 * which are substituted by the following values when a bug report
787 * file is created:
789 * %% A single % character.
790 * %e The base name of the executable filename.
791 * %E Pathname of executable, with slashes ('/') replaced by
792 * exclamation marks ('!').
793 * %f Similar to %e with the main script filename.
794 * %F Similar to %E with the main script filename.
795 * %p PID of dumped process in decimal.
796 * %t Time of dump, expressed as seconds since the Epoch,
797 * 1970-01-01 00:00:00 +0000 (UTC).
798 * %NNN Octal char code, upto 3 digits.
800 static char *
801 expand_report_argument(const char **input_template, struct report_expansion *values,
802 char *buf, size_t size, bool word)
804 char *p = buf;
805 char *end = buf + size;
806 const char *template = *input_template;
807 bool store = true;
809 if (p >= end-1 || !*template) return NULL;
810 do {
811 char c = *template++;
812 if (word && ISSPACE(c)) break;
813 if (!store) continue;
814 if (c == '%') {
815 size_t n;
816 switch (c = *template++) {
817 case 'e':
818 p = append_basename(p, end, &values->exe, rb_argv0);
819 continue;
820 case 'E':
821 p = append_pathname(p, end, rb_argv0);
822 continue;
823 case 'f':
824 p = append_basename(p, end, &values->script, GET_VM()->orig_progname);
825 continue;
826 case 'F':
827 p = append_pathname(p, end, GET_VM()->orig_progname);
828 continue;
829 case 'p':
830 if (!values->pid) values->pid = getpid();
831 snprintf(p, end-p, "%" PRI_PIDT_PREFIX "d", values->pid);
832 p += strlen(p);
833 continue;
834 case 't':
835 if (!values->time) values->time = time(NULL);
836 snprintf(p, end-p, "%" PRI_TIMET_PREFIX "d", values->time);
837 p += strlen(p);
838 continue;
839 default:
840 if (c >= '0' && c <= '7') {
841 c = (unsigned char)ruby_scan_oct(template-1, 3, &n);
842 template += n - 1;
843 if (!c) store = false;
845 break;
848 if (p < end-1) *p++ = c;
849 } while (*template);
850 *input_template = template;
851 *p = '\0';
852 return ++p;
855 FILE *ruby_popen_writer(char *const *argv, rb_pid_t *pid);
857 static FILE *
858 open_report_path(const char *template, char *buf, size_t size, rb_pid_t *pid)
860 struct report_expansion values = {{0}};
862 if (!template) return NULL;
863 if (0) fprintf(stderr, "RUBY_CRASH_REPORT=%s\n", buf);
864 if (*template == '|') {
865 char *argv[16], *bufend = buf + size, *p;
866 int argc;
867 template++;
868 for (argc = 0; argc < numberof(argv) - 1; ++argc) {
869 while (*template && ISSPACE(*template)) template++;
870 p = expand_report_argument(&template, &values, buf, bufend-buf, true);
871 if (!p) break;
872 argv[argc] = buf;
873 buf = p;
875 argv[argc] = NULL;
876 if (!p) return ruby_popen_writer(argv, pid);
878 else if (*template) {
879 expand_report_argument(&template, &values, buf, size, false);
880 return fopen(buf, "w");
882 return NULL;
885 static const char *crash_report;
887 /* SIGSEGV handler might have a very small stack. Thus we need to use it carefully. */
888 #define REPORT_BUG_BUFSIZ 256
889 static FILE *
890 bug_report_file(const char *file, int line, rb_pid_t *pid)
892 char buf[REPORT_BUG_BUFSIZ];
893 const char *report = crash_report;
894 if (!report) report = getenv("RUBY_CRASH_REPORT");
895 FILE *out = open_report_path(report, buf, sizeof(buf), pid);
896 int len = err_position_0(buf, sizeof(buf), file, line);
898 if (out) {
899 if ((ssize_t)fwrite(buf, 1, len, out) == (ssize_t)len) return out;
900 fclose(out);
902 if ((ssize_t)fwrite(buf, 1, len, stderr) == (ssize_t)len) {
903 return stderr;
905 if ((ssize_t)fwrite(buf, 1, len, stdout) == (ssize_t)len) {
906 return stdout;
909 return NULL;
912 FUNC_MINIMIZED(static void bug_important_message(FILE *out, const char *const msg, size_t len));
914 static void
915 bug_important_message(FILE *out, const char *const msg, size_t len)
917 const char *const endmsg = msg + len;
918 const char *p = msg;
920 if (!len) return;
921 if (isatty(fileno(out))) {
922 static const char red[] = "\033[;31;1;7m";
923 static const char green[] = "\033[;32;7m";
924 static const char reset[] = "\033[m";
925 const char *e = strchr(p, '\n');
926 const int w = (int)(e - p);
927 do {
928 int i = (int)(e - p);
929 fputs(*p == ' ' ? green : red, out);
930 fwrite(p, 1, e - p, out);
931 for (; i < w; ++i) fputc(' ', out);
932 fputs(reset, out);
933 fputc('\n', out);
934 } while ((p = e + 1) < endmsg && (e = strchr(p, '\n')) != 0 && e > p + 1);
936 fwrite(p, 1, endmsg - p, out);
939 #undef CRASH_REPORTER_MAY_BE_CREATED
940 #if defined(__APPLE__) && \
941 (!defined(MAC_OS_X_VERSION_10_6) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6 || defined(__POWERPC__)) /* 10.6 PPC case */
942 # define CRASH_REPORTER_MAY_BE_CREATED
943 #endif
944 static void
945 preface_dump(FILE *out)
947 #if defined __APPLE__
948 static const char msg[] = ""
949 "-- Crash Report log information "
950 "--------------------------------------------\n"
951 " See Crash Report log file in one of the following locations:\n"
952 # ifdef CRASH_REPORTER_MAY_BE_CREATED
953 " * ~/Library/Logs/CrashReporter\n"
954 " * /Library/Logs/CrashReporter\n"
955 # endif
956 " * ~/Library/Logs/DiagnosticReports\n"
957 " * /Library/Logs/DiagnosticReports\n"
958 " for more details.\n"
959 "Don't forget to include the above Crash Report log file in bug reports.\n"
960 "\n";
961 const size_t msglen = sizeof(msg) - 1;
962 #else
963 const char *msg = NULL;
964 const size_t msglen = 0;
965 #endif
966 bug_important_message(out, msg, msglen);
969 static void
970 postscript_dump(FILE *out)
972 #if defined __APPLE__
973 static const char msg[] = ""
974 "[IMPORTANT]"
975 /*" ------------------------------------------------"*/
976 "\n""Don't forget to include the Crash Report log file under\n"
977 # ifdef CRASH_REPORTER_MAY_BE_CREATED
978 "CrashReporter or "
979 # endif
980 "DiagnosticReports directory in bug reports.\n"
981 /*"------------------------------------------------------------\n"*/
982 "\n";
983 const size_t msglen = sizeof(msg) - 1;
984 #else
985 const char *msg = NULL;
986 const size_t msglen = 0;
987 #endif
988 bug_important_message(out, msg, msglen);
991 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 0)
992 static void
993 bug_report_begin_valist(FILE *out, const char *fmt, va_list args)
995 char buf[REPORT_BUG_BUFSIZ];
997 fputs("[BUG] ", out);
998 vsnprintf(buf, sizeof(buf), fmt, args);
999 fputs(buf, out);
1000 snprintf(buf, sizeof(buf), "\n%s\n\n", rb_dynamic_description);
1001 fputs(buf, out);
1002 preface_dump(out);
1005 #define bug_report_begin(out, fmt) do { \
1006 va_list args; \
1007 va_start(args, fmt); \
1008 bug_report_begin_valist(out, fmt, args); \
1009 va_end(args); \
1010 } while (0)
1012 static void
1013 bug_report_end(FILE *out, rb_pid_t pid)
1015 /* call additional bug reporters */
1017 int i;
1018 for (i=0; i<bug_reporters_size; i++) {
1019 struct bug_reporters *reporter = &bug_reporters[i];
1020 (*reporter->func)(out, reporter->data);
1023 postscript_dump(out);
1024 finish_report(out, pid);
1027 #define report_bug(file, line, fmt, ctx) do { \
1028 rb_pid_t pid = -1; \
1029 FILE *out = bug_report_file(file, line, &pid); \
1030 if (out) { \
1031 bug_report_begin(out, fmt); \
1032 rb_vm_bugreport(ctx, out); \
1033 bug_report_end(out, pid); \
1035 } while (0) \
1037 #define report_bug_valist(file, line, fmt, ctx, args) do { \
1038 rb_pid_t pid = -1; \
1039 FILE *out = bug_report_file(file, line, &pid); \
1040 if (out) { \
1041 bug_report_begin_valist(out, fmt, args); \
1042 rb_vm_bugreport(ctx, out); \
1043 bug_report_end(out, pid); \
1045 } while (0) \
1047 void
1048 ruby_set_crash_report(const char *template)
1050 crash_report = template;
1051 #if RUBY_DEBUG
1052 rb_pid_t pid = -1;
1053 char buf[REPORT_BUG_BUFSIZ];
1054 FILE *out = open_report_path(template, buf, sizeof(buf), &pid);
1055 if (out) {
1056 time_t t = time(NULL);
1057 fprintf(out, "ruby_test_bug_report: %s", ctime(&t));
1058 finish_report(out, pid);
1060 #endif
1063 NORETURN(static void die(void));
1064 static void
1065 die(void)
1067 #if defined(_WIN32) && defined(RUBY_MSVCRT_VERSION) && RUBY_MSVCRT_VERSION >= 80
1068 _set_abort_behavior( 0, _CALL_REPORTFAULT);
1069 #endif
1071 abort();
1074 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 1, 0)
1075 void
1076 rb_bug_without_die(const char *fmt, va_list args)
1078 const char *file = NULL;
1079 int line = 0;
1081 if (GET_EC()) {
1082 file = rb_source_location_cstr(&line);
1085 report_bug_valist(file, line, fmt, NULL, args);
1088 void
1089 rb_bug(const char *fmt, ...)
1091 va_list args;
1092 va_start(args, fmt);
1093 rb_bug_without_die(fmt, args);
1094 va_end(args);
1095 die();
1098 void
1099 rb_bug_for_fatal_signal(ruby_sighandler_t default_sighandler, int sig, const void *ctx, const char *fmt, ...)
1101 const char *file = NULL;
1102 int line = 0;
1104 if (GET_EC()) {
1105 file = rb_source_location_cstr(&line);
1108 report_bug(file, line, fmt, ctx);
1110 if (default_sighandler) default_sighandler(sig);
1112 ruby_default_signal(sig);
1113 die();
1117 void
1118 rb_bug_errno(const char *mesg, int errno_arg)
1120 if (errno_arg == 0)
1121 rb_bug("%s: errno == 0 (NOERROR)", mesg);
1122 else {
1123 const char *errno_str = rb_strerrno(errno_arg);
1124 if (errno_str)
1125 rb_bug("%s: %s (%s)", mesg, strerror(errno_arg), errno_str);
1126 else
1127 rb_bug("%s: %s (%d)", mesg, strerror(errno_arg), errno_arg);
1132 * this is safe to call inside signal handler and timer thread
1133 * (which isn't a Ruby Thread object)
1135 #define write_or_abort(fd, str, len) (write((fd), (str), (len)) < 0 ? abort() : (void)0)
1136 #define WRITE_CONST(fd,str) write_or_abort((fd),(str),sizeof(str) - 1)
1138 void
1139 rb_async_bug_errno(const char *mesg, int errno_arg)
1141 WRITE_CONST(2, "[ASYNC BUG] ");
1142 write_or_abort(2, mesg, strlen(mesg));
1143 WRITE_CONST(2, "\n");
1145 if (errno_arg == 0) {
1146 WRITE_CONST(2, "errno == 0 (NOERROR)\n");
1148 else {
1149 const char *errno_str = rb_strerrno(errno_arg);
1151 if (!errno_str)
1152 errno_str = "undefined errno";
1153 write_or_abort(2, errno_str, strlen(errno_str));
1155 WRITE_CONST(2, "\n\n");
1156 write_or_abort(2, rb_dynamic_description, strlen(rb_dynamic_description));
1157 abort();
1160 void
1161 rb_report_bug_valist(VALUE file, int line, const char *fmt, va_list args)
1163 report_bug_valist(RSTRING_PTR(file), line, fmt, NULL, args);
1166 void
1167 rb_assert_failure(const char *file, int line, const char *name, const char *expr)
1169 rb_assert_failure_detail(file, line, name, expr, NULL);
1172 void
1173 rb_assert_failure_detail(const char *file, int line, const char *name, const char *expr,
1174 const char *fmt, ...)
1176 FILE *out = stderr;
1177 fprintf(out, "Assertion Failed: %s:%d:", file, line);
1178 if (name) fprintf(out, "%s:", name);
1179 fprintf(out, "%s\n%s\n\n", expr, rb_dynamic_description);
1181 if (fmt && *fmt) {
1182 va_list args;
1183 va_start(args, fmt);
1184 vfprintf(out, fmt, args);
1185 va_end(args);
1188 preface_dump(out);
1189 rb_vm_bugreport(NULL, out);
1190 bug_report_end(out, -1);
1191 die();
1194 static const char builtin_types[][10] = {
1195 "", /* 0x00, */
1196 "Object",
1197 "Class",
1198 "Module",
1199 "Float",
1200 "String",
1201 "Regexp",
1202 "Array",
1203 "Hash",
1204 "Struct",
1205 "Integer",
1206 "File",
1207 "Data", /* internal use: wrapped C pointers */
1208 "MatchData", /* data of $~ */
1209 "Complex",
1210 "Rational",
1211 "", /* 0x10 */
1212 "nil",
1213 "true",
1214 "false",
1215 "Symbol", /* :symbol */
1216 "Integer",
1217 "undef", /* internal use: #undef; should not happen */
1218 "", /* 0x17 */
1219 "", /* 0x18 */
1220 "", /* 0x19 */
1221 "<Memo>", /* internal use: general memo */
1222 "<Node>", /* internal use: syntax tree node */
1223 "<iClass>", /* internal use: mixed-in module holder */
1226 const char *
1227 rb_builtin_type_name(int t)
1229 const char *name;
1230 if ((unsigned int)t >= numberof(builtin_types)) return 0;
1231 name = builtin_types[t];
1232 if (*name) return name;
1233 return 0;
1236 static VALUE
1237 displaying_class_of(VALUE x)
1239 switch (x) {
1240 case Qfalse: return rb_fstring_cstr("false");
1241 case Qnil: return rb_fstring_cstr("nil");
1242 case Qtrue: return rb_fstring_cstr("true");
1243 default: return rb_obj_class(x);
1247 static const char *
1248 builtin_class_name(VALUE x)
1250 const char *etype;
1252 if (NIL_P(x)) {
1253 etype = "nil";
1255 else if (FIXNUM_P(x)) {
1256 etype = "Integer";
1258 else if (SYMBOL_P(x)) {
1259 etype = "Symbol";
1261 else if (RB_TYPE_P(x, T_TRUE)) {
1262 etype = "true";
1264 else if (RB_TYPE_P(x, T_FALSE)) {
1265 etype = "false";
1267 else {
1268 etype = NULL;
1270 return etype;
1273 const char *
1274 rb_builtin_class_name(VALUE x)
1276 const char *etype = builtin_class_name(x);
1278 if (!etype) {
1279 etype = rb_obj_classname(x);
1281 return etype;
1284 COLDFUNC NORETURN(static void unexpected_type(VALUE, int, int));
1285 #define UNDEF_LEAKED "undef leaked to the Ruby space"
1287 static void
1288 unexpected_type(VALUE x, int xt, int t)
1290 const char *tname = rb_builtin_type_name(t);
1291 VALUE mesg, exc = rb_eFatal;
1293 if (tname) {
1294 mesg = rb_sprintf("wrong argument type %"PRIsVALUE" (expected %s)",
1295 displaying_class_of(x), tname);
1296 exc = rb_eTypeError;
1298 else if (xt > T_MASK && xt <= 0x3f) {
1299 mesg = rb_sprintf("unknown type 0x%x (0x%x given, probably comes"
1300 " from extension library for ruby 1.8)", t, xt);
1302 else {
1303 mesg = rb_sprintf("unknown type 0x%x (0x%x given)", t, xt);
1305 rb_exc_raise(rb_exc_new_str(exc, mesg));
1308 void
1309 rb_check_type(VALUE x, int t)
1311 int xt;
1313 if (RB_UNLIKELY(UNDEF_P(x))) {
1314 rb_bug(UNDEF_LEAKED);
1317 xt = TYPE(x);
1318 if (xt != t || (xt == T_DATA && rbimpl_rtypeddata_p(x))) {
1320 * Typed data is not simple `T_DATA`, but in a sense an
1321 * extension of `struct RVALUE`, which are incompatible with
1322 * each other except when inherited.
1324 * So it is not enough to just check `T_DATA`, it must be
1325 * identified by its `type` using `Check_TypedStruct` instead.
1327 unexpected_type(x, xt, t);
1331 void
1332 rb_unexpected_type(VALUE x, int t)
1334 if (RB_UNLIKELY(UNDEF_P(x))) {
1335 rb_bug(UNDEF_LEAKED);
1338 unexpected_type(x, TYPE(x), t);
1342 rb_typeddata_inherited_p(const rb_data_type_t *child, const rb_data_type_t *parent)
1344 while (child) {
1345 if (child == parent) return 1;
1346 child = child->parent;
1348 return 0;
1352 rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
1354 if (!RB_TYPE_P(obj, T_DATA) ||
1355 !RTYPEDDATA_P(obj) || !rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) {
1356 return 0;
1358 return 1;
1361 #undef rb_typeddata_is_instance_of
1363 rb_typeddata_is_instance_of(VALUE obj, const rb_data_type_t *data_type)
1365 return rb_typeddata_is_instance_of_inline(obj, data_type);
1368 void *
1369 rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
1371 VALUE actual;
1373 if (!RB_TYPE_P(obj, T_DATA)) {
1374 actual = displaying_class_of(obj);
1376 else if (!RTYPEDDATA_P(obj)) {
1377 actual = displaying_class_of(obj);
1379 else if (!rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) {
1380 const char *name = RTYPEDDATA_TYPE(obj)->wrap_struct_name;
1381 actual = rb_str_new_cstr(name); /* or rb_fstring_cstr? not sure... */
1383 else {
1384 return RTYPEDDATA_GET_DATA(obj);
1387 const char *expected = data_type->wrap_struct_name;
1388 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected %s)",
1389 actual, expected);
1390 UNREACHABLE_RETURN(NULL);
1393 /* exception classes */
1394 VALUE rb_eException;
1395 VALUE rb_eSystemExit;
1396 VALUE rb_eInterrupt;
1397 VALUE rb_eSignal;
1398 VALUE rb_eFatal;
1399 VALUE rb_eStandardError;
1400 VALUE rb_eRuntimeError;
1401 VALUE rb_eFrozenError;
1402 VALUE rb_eTypeError;
1403 VALUE rb_eArgError;
1404 VALUE rb_eIndexError;
1405 VALUE rb_eKeyError;
1406 VALUE rb_eRangeError;
1407 VALUE rb_eNameError;
1408 VALUE rb_eEncodingError;
1409 VALUE rb_eEncCompatError;
1410 VALUE rb_eNoMethodError;
1411 VALUE rb_eSecurityError;
1412 VALUE rb_eNotImpError;
1413 VALUE rb_eNoMemError;
1414 VALUE rb_cNameErrorMesg;
1415 VALUE rb_eNoMatchingPatternError;
1416 VALUE rb_eNoMatchingPatternKeyError;
1418 VALUE rb_eScriptError;
1419 VALUE rb_eSyntaxError;
1420 VALUE rb_eLoadError;
1422 VALUE rb_eSystemCallError;
1423 VALUE rb_mErrno;
1424 static VALUE rb_eNOERROR;
1426 ID ruby_static_id_cause;
1427 #define id_cause ruby_static_id_cause
1428 static ID id_message, id_detailed_message, id_backtrace;
1429 static ID id_key, id_matchee, id_args, id_Errno, id_errno, id_i_path;
1430 static ID id_receiver, id_recv, id_iseq, id_local_variables;
1431 static ID id_private_call_p, id_top, id_bottom;
1432 #define id_bt idBt
1433 #define id_bt_locations idBt_locations
1434 #define id_mesg idMesg
1435 #define id_name idName
1437 #undef rb_exc_new_cstr
1439 VALUE
1440 rb_exc_new(VALUE etype, const char *ptr, long len)
1442 VALUE mesg = rb_str_new(ptr, len);
1443 return rb_class_new_instance(1, &mesg, etype);
1446 VALUE
1447 rb_exc_new_cstr(VALUE etype, const char *s)
1449 return rb_exc_new(etype, s, strlen(s));
1452 VALUE
1453 rb_exc_new_str(VALUE etype, VALUE str)
1455 rb_yjit_lazy_push_frame(GET_EC()->cfp->pc);
1456 StringValue(str);
1457 return rb_class_new_instance(1, &str, etype);
1460 static VALUE
1461 exc_init(VALUE exc, VALUE mesg)
1463 rb_ivar_set(exc, id_mesg, mesg);
1464 rb_ivar_set(exc, id_bt, Qnil);
1466 return exc;
1470 * call-seq:
1471 * Exception.new(msg = nil) -> exception
1472 * Exception.exception(msg = nil) -> exception
1474 * Construct a new Exception object, optionally passing in
1475 * a message.
1478 static VALUE
1479 exc_initialize(int argc, VALUE *argv, VALUE exc)
1481 VALUE arg;
1483 arg = (!rb_check_arity(argc, 0, 1) ? Qnil : argv[0]);
1484 return exc_init(exc, arg);
1488 * Document-method: exception
1490 * call-seq:
1491 * exc.exception([string]) -> an_exception or exc
1493 * With no argument, or if the argument is the same as the receiver,
1494 * return the receiver. Otherwise, create a new
1495 * exception object of the same class as the receiver, but with a
1496 * message equal to <code>string.to_str</code>.
1500 static VALUE
1501 exc_exception(int argc, VALUE *argv, VALUE self)
1503 VALUE exc;
1505 argc = rb_check_arity(argc, 0, 1);
1506 if (argc == 0) return self;
1507 if (argc == 1 && self == argv[0]) return self;
1508 exc = rb_obj_clone(self);
1509 rb_ivar_set(exc, id_mesg, argv[0]);
1510 return exc;
1514 * call-seq:
1515 * exception.to_s -> string
1517 * Returns exception's message (or the name of the exception if
1518 * no message is set).
1521 static VALUE
1522 exc_to_s(VALUE exc)
1524 VALUE mesg = rb_attr_get(exc, idMesg);
1526 if (NIL_P(mesg)) return rb_class_name(CLASS_OF(exc));
1527 return rb_String(mesg);
1530 /* FIXME: Include eval_error.c */
1531 void rb_error_write(VALUE errinfo, VALUE emesg, VALUE errat, VALUE str, VALUE opt, VALUE highlight, VALUE reverse);
1533 VALUE
1534 rb_get_message(VALUE exc)
1536 VALUE e = rb_check_funcall(exc, id_message, 0, 0);
1537 if (UNDEF_P(e)) return Qnil;
1538 if (!RB_TYPE_P(e, T_STRING)) e = rb_check_string_type(e);
1539 return e;
1542 VALUE
1543 rb_get_detailed_message(VALUE exc, VALUE opt)
1545 VALUE e;
1546 if (NIL_P(opt)) {
1547 e = rb_check_funcall(exc, id_detailed_message, 0, 0);
1549 else {
1550 e = rb_check_funcall_kw(exc, id_detailed_message, 1, &opt, 1);
1552 if (UNDEF_P(e)) return Qnil;
1553 if (!RB_TYPE_P(e, T_STRING)) e = rb_check_string_type(e);
1554 return e;
1558 * call-seq:
1559 * Exception.to_tty? -> true or false
1561 * Returns +true+ if exception messages will be sent to a tty.
1563 static VALUE
1564 exc_s_to_tty_p(VALUE self)
1566 return RBOOL(rb_stderr_tty_p());
1569 static VALUE
1570 check_highlight_keyword(VALUE opt, int auto_tty_detect)
1572 VALUE highlight = Qnil;
1574 if (!NIL_P(opt)) {
1575 highlight = rb_hash_lookup(opt, sym_highlight);
1577 switch (highlight) {
1578 default:
1579 rb_bool_expected(highlight, "highlight", TRUE);
1580 UNREACHABLE;
1581 case Qtrue: case Qfalse: case Qnil: break;
1585 if (NIL_P(highlight)) {
1586 highlight = RBOOL(auto_tty_detect && rb_stderr_tty_p());
1589 return highlight;
1592 static VALUE
1593 check_order_keyword(VALUE opt)
1595 VALUE order = Qnil;
1597 if (!NIL_P(opt)) {
1598 static VALUE kw_order;
1599 if (!kw_order) kw_order = ID2SYM(rb_intern_const("order"));
1601 order = rb_hash_lookup(opt, kw_order);
1603 if (order != Qnil) {
1604 ID id = rb_check_id(&order);
1605 if (id == id_bottom) order = Qtrue;
1606 else if (id == id_top) order = Qfalse;
1607 else {
1608 rb_raise(rb_eArgError, "expected :top or :bottom as "
1609 "order: %+"PRIsVALUE, order);
1614 if (NIL_P(order)) order = Qfalse;
1616 return order;
1620 * call-seq:
1621 * exception.full_message(highlight: bool, order: [:top or :bottom]) -> string
1623 * Returns formatted string of _exception_.
1624 * The returned string is formatted using the same format that Ruby uses
1625 * when printing an uncaught exceptions to stderr.
1627 * If _highlight_ is +true+ the default error handler will send the
1628 * messages to a tty.
1630 * _order_ must be either of +:top+ or +:bottom+, and places the error
1631 * message and the innermost backtrace come at the top or the bottom.
1633 * The default values of these options depend on <code>$stderr</code>
1634 * and its +tty?+ at the timing of a call.
1637 static VALUE
1638 exc_full_message(int argc, VALUE *argv, VALUE exc)
1640 VALUE opt, str, emesg, errat;
1641 VALUE highlight, order;
1643 rb_scan_args(argc, argv, "0:", &opt);
1645 highlight = check_highlight_keyword(opt, 1);
1646 order = check_order_keyword(opt);
1649 if (NIL_P(opt)) opt = rb_hash_new();
1650 rb_hash_aset(opt, sym_highlight, highlight);
1653 str = rb_str_new2("");
1654 errat = rb_get_backtrace(exc);
1655 emesg = rb_get_detailed_message(exc, opt);
1657 rb_error_write(exc, emesg, errat, str, opt, highlight, order);
1658 return str;
1662 * call-seq:
1663 * exception.message -> string
1665 * Returns the result of invoking <code>exception.to_s</code>.
1666 * Normally this returns the exception's message or name.
1669 static VALUE
1670 exc_message(VALUE exc)
1672 return rb_funcallv(exc, idTo_s, 0, 0);
1676 * call-seq:
1677 * exception.detailed_message(highlight: bool, **opt) -> string
1679 * Processes a string returned by #message.
1681 * It may add the class name of the exception to the end of the first line.
1682 * Also, when +highlight+ keyword is true, it adds ANSI escape sequences to
1683 * make the message bold.
1685 * If you override this method, it must be tolerant for unknown keyword
1686 * arguments. All keyword arguments passed to #full_message are delegated
1687 * to this method.
1689 * This method is overridden by did_you_mean and error_highlight to add
1690 * their information.
1692 * A user-defined exception class can also define their own
1693 * +detailed_message+ method to add supplemental information.
1694 * When +highlight+ is true, it can return a string containing escape
1695 * sequences, but use widely-supported ones. It is recommended to limit
1696 * the following codes:
1698 * - Reset (+\e[0m+)
1699 * - Bold (+\e[1m+)
1700 * - Underline (+\e[4m+)
1701 * - Foreground color except white and black
1702 * - Red (+\e[31m+)
1703 * - Green (+\e[32m+)
1704 * - Yellow (+\e[33m+)
1705 * - Blue (+\e[34m+)
1706 * - Magenta (+\e[35m+)
1707 * - Cyan (+\e[36m+)
1709 * Use escape sequences carefully even if +highlight+ is true.
1710 * Do not use escape sequences to express essential information;
1711 * the message should be readable even if all escape sequences are
1712 * ignored.
1715 static VALUE
1716 exc_detailed_message(int argc, VALUE *argv, VALUE exc)
1718 VALUE opt;
1720 rb_scan_args(argc, argv, "0:", &opt);
1722 VALUE highlight = check_highlight_keyword(opt, 0);
1724 extern VALUE rb_decorate_message(const VALUE eclass, VALUE emesg, int highlight);
1726 return rb_decorate_message(CLASS_OF(exc), rb_get_message(exc), RTEST(highlight));
1730 * call-seq:
1731 * exception.inspect -> string
1733 * Return this exception's class name and message.
1736 static VALUE
1737 exc_inspect(VALUE exc)
1739 VALUE str, klass;
1741 klass = CLASS_OF(exc);
1742 exc = rb_obj_as_string(exc);
1743 if (RSTRING_LEN(exc) == 0) {
1744 return rb_class_name(klass);
1747 str = rb_str_buf_new2("#<");
1748 klass = rb_class_name(klass);
1749 rb_str_buf_append(str, klass);
1751 if (RTEST(rb_str_include(exc, rb_str_new2("\n")))) {
1752 rb_str_catf(str, ":%+"PRIsVALUE, exc);
1754 else {
1755 rb_str_buf_cat(str, ": ", 2);
1756 rb_str_buf_append(str, exc);
1759 rb_str_buf_cat(str, ">", 1);
1761 return str;
1765 * call-seq:
1766 * exception.backtrace -> array or nil
1768 * Returns any backtrace associated with the exception. The backtrace
1769 * is an array of strings, each containing either ``filename:lineNo: in
1770 * `method''' or ``filename:lineNo.''
1772 * def a
1773 * raise "boom"
1774 * end
1776 * def b
1777 * a()
1778 * end
1780 * begin
1781 * b()
1782 * rescue => detail
1783 * print detail.backtrace.join("\n")
1784 * end
1786 * <em>produces:</em>
1788 * prog.rb:2:in `a'
1789 * prog.rb:6:in `b'
1790 * prog.rb:10
1792 * In the case no backtrace has been set, +nil+ is returned
1794 * ex = StandardError.new
1795 * ex.backtrace
1796 * #=> nil
1799 static VALUE
1800 exc_backtrace(VALUE exc)
1802 VALUE obj;
1804 obj = rb_attr_get(exc, id_bt);
1806 if (rb_backtrace_p(obj)) {
1807 obj = rb_backtrace_to_str_ary(obj);
1808 /* rb_ivar_set(exc, id_bt, obj); */
1811 return obj;
1814 static VALUE rb_check_backtrace(VALUE);
1816 VALUE
1817 rb_get_backtrace(VALUE exc)
1819 ID mid = id_backtrace;
1820 VALUE info;
1821 if (rb_method_basic_definition_p(CLASS_OF(exc), id_backtrace)) {
1822 VALUE klass = rb_eException;
1823 rb_execution_context_t *ec = GET_EC();
1824 if (NIL_P(exc))
1825 return Qnil;
1826 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_CALL, exc, mid, mid, klass, Qundef);
1827 info = exc_backtrace(exc);
1828 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, exc, mid, mid, klass, info);
1830 else {
1831 info = rb_funcallv(exc, mid, 0, 0);
1833 if (NIL_P(info)) return Qnil;
1834 return rb_check_backtrace(info);
1838 * call-seq:
1839 * exception.backtrace_locations -> array or nil
1841 * Returns any backtrace associated with the exception. This method is
1842 * similar to Exception#backtrace, but the backtrace is an array of
1843 * Thread::Backtrace::Location.
1845 * This method is not affected by Exception#set_backtrace().
1847 static VALUE
1848 exc_backtrace_locations(VALUE exc)
1850 VALUE obj;
1852 obj = rb_attr_get(exc, id_bt_locations);
1853 if (!NIL_P(obj)) {
1854 obj = rb_backtrace_to_location_ary(obj);
1856 return obj;
1859 static VALUE
1860 rb_check_backtrace(VALUE bt)
1862 long i;
1863 static const char err[] = "backtrace must be an Array of String or an Array of Thread::Backtrace::Location";
1865 if (!NIL_P(bt)) {
1866 if (RB_TYPE_P(bt, T_STRING)) return rb_ary_new3(1, bt);
1867 if (rb_backtrace_p(bt)) return bt;
1868 if (!RB_TYPE_P(bt, T_ARRAY)) {
1869 rb_raise(rb_eTypeError, err);
1871 for (i=0;i<RARRAY_LEN(bt);i++) {
1872 VALUE e = RARRAY_AREF(bt, i);
1873 if (!RB_TYPE_P(e, T_STRING)) {
1874 rb_raise(rb_eTypeError, err);
1878 return bt;
1882 * call-seq:
1883 * exc.set_backtrace(backtrace) -> array
1885 * Sets the backtrace information associated with +exc+. The +backtrace+ must
1886 * be an array of Thread::Backtrace::Location objects or an array of String objects
1887 * or a single String in the format described in Exception#backtrace.
1891 static VALUE
1892 exc_set_backtrace(VALUE exc, VALUE bt)
1894 VALUE btobj = rb_location_ary_to_backtrace(bt);
1895 if (RTEST(btobj)) {
1896 rb_ivar_set(exc, id_bt, btobj);
1897 rb_ivar_set(exc, id_bt_locations, btobj);
1898 return bt;
1900 else {
1901 return rb_ivar_set(exc, id_bt, rb_check_backtrace(bt));
1905 VALUE
1906 rb_exc_set_backtrace(VALUE exc, VALUE bt)
1908 return exc_set_backtrace(exc, bt);
1912 * call-seq:
1913 * exception.cause -> an_exception or nil
1915 * Returns the previous exception ($!) at the time this exception was raised.
1916 * This is useful for wrapping exceptions and retaining the original exception
1917 * information.
1920 static VALUE
1921 exc_cause(VALUE exc)
1923 return rb_attr_get(exc, id_cause);
1926 static VALUE
1927 try_convert_to_exception(VALUE obj)
1929 return rb_check_funcall(obj, idException, 0, 0);
1933 * call-seq:
1934 * exc == obj -> true or false
1936 * Equality---If <i>obj</i> is not an Exception, returns
1937 * <code>false</code>. Otherwise, returns <code>true</code> if <i>exc</i> and
1938 * <i>obj</i> share same class, messages, and backtrace.
1941 static VALUE
1942 exc_equal(VALUE exc, VALUE obj)
1944 VALUE mesg, backtrace;
1946 if (exc == obj) return Qtrue;
1948 if (rb_obj_class(exc) != rb_obj_class(obj)) {
1949 int state;
1951 obj = rb_protect(try_convert_to_exception, obj, &state);
1952 if (state || UNDEF_P(obj)) {
1953 rb_set_errinfo(Qnil);
1954 return Qfalse;
1956 if (rb_obj_class(exc) != rb_obj_class(obj)) return Qfalse;
1957 mesg = rb_check_funcall(obj, id_message, 0, 0);
1958 if (UNDEF_P(mesg)) return Qfalse;
1959 backtrace = rb_check_funcall(obj, id_backtrace, 0, 0);
1960 if (UNDEF_P(backtrace)) return Qfalse;
1962 else {
1963 mesg = rb_attr_get(obj, id_mesg);
1964 backtrace = exc_backtrace(obj);
1967 if (!rb_equal(rb_attr_get(exc, id_mesg), mesg))
1968 return Qfalse;
1969 return rb_equal(exc_backtrace(exc), backtrace);
1973 * call-seq:
1974 * SystemExit.new -> system_exit
1975 * SystemExit.new(status) -> system_exit
1976 * SystemExit.new(status, msg) -> system_exit
1977 * SystemExit.new(msg) -> system_exit
1979 * Create a new +SystemExit+ exception with the given status and message.
1980 * Status is true, false, or an integer.
1981 * If status is not given, true is used.
1984 static VALUE
1985 exit_initialize(int argc, VALUE *argv, VALUE exc)
1987 VALUE status;
1988 if (argc > 0) {
1989 status = *argv;
1991 switch (status) {
1992 case Qtrue:
1993 status = INT2FIX(EXIT_SUCCESS);
1994 ++argv;
1995 --argc;
1996 break;
1997 case Qfalse:
1998 status = INT2FIX(EXIT_FAILURE);
1999 ++argv;
2000 --argc;
2001 break;
2002 default:
2003 status = rb_check_to_int(status);
2004 if (NIL_P(status)) {
2005 status = INT2FIX(EXIT_SUCCESS);
2007 else {
2008 #if EXIT_SUCCESS != 0
2009 if (status == INT2FIX(0))
2010 status = INT2FIX(EXIT_SUCCESS);
2011 #endif
2012 ++argv;
2013 --argc;
2015 break;
2018 else {
2019 status = INT2FIX(EXIT_SUCCESS);
2021 rb_call_super(argc, argv);
2022 rb_ivar_set(exc, id_status, status);
2023 return exc;
2028 * call-seq:
2029 * system_exit.status -> integer
2031 * Return the status value associated with this system exit.
2034 static VALUE
2035 exit_status(VALUE exc)
2037 return rb_attr_get(exc, id_status);
2042 * call-seq:
2043 * system_exit.success? -> true or false
2045 * Returns +true+ if exiting successful, +false+ if not.
2048 static VALUE
2049 exit_success_p(VALUE exc)
2051 VALUE status_val = rb_attr_get(exc, id_status);
2052 int status;
2054 if (NIL_P(status_val))
2055 return Qtrue;
2056 status = NUM2INT(status_val);
2057 return RBOOL(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
2060 static VALUE
2061 err_init_recv(VALUE exc, VALUE recv)
2063 if (!UNDEF_P(recv)) rb_ivar_set(exc, id_recv, recv);
2064 return exc;
2068 * call-seq:
2069 * FrozenError.new(msg=nil, receiver: nil) -> frozen_error
2071 * Construct a new FrozenError exception. If given the <i>receiver</i>
2072 * parameter may subsequently be examined using the FrozenError#receiver
2073 * method.
2075 * a = [].freeze
2076 * raise FrozenError.new("can't modify frozen array", receiver: a)
2079 static VALUE
2080 frozen_err_initialize(int argc, VALUE *argv, VALUE self)
2082 ID keywords[1];
2083 VALUE values[numberof(keywords)], options;
2085 argc = rb_scan_args(argc, argv, "*:", NULL, &options);
2086 keywords[0] = id_receiver;
2087 rb_get_kwargs(options, keywords, 0, numberof(values), values);
2088 rb_call_super(argc, argv);
2089 err_init_recv(self, values[0]);
2090 return self;
2094 * Document-method: FrozenError#receiver
2095 * call-seq:
2096 * frozen_error.receiver -> object
2098 * Return the receiver associated with this FrozenError exception.
2101 #define frozen_err_receiver name_err_receiver
2103 void
2104 rb_name_error(ID id, const char *fmt, ...)
2106 VALUE exc, argv[2];
2107 va_list args;
2109 va_start(args, fmt);
2110 argv[0] = rb_vsprintf(fmt, args);
2111 va_end(args);
2113 argv[1] = ID2SYM(id);
2114 exc = rb_class_new_instance(2, argv, rb_eNameError);
2115 rb_exc_raise(exc);
2118 void
2119 rb_name_error_str(VALUE str, const char *fmt, ...)
2121 VALUE exc, argv[2];
2122 va_list args;
2124 va_start(args, fmt);
2125 argv[0] = rb_vsprintf(fmt, args);
2126 va_end(args);
2128 argv[1] = str;
2129 exc = rb_class_new_instance(2, argv, rb_eNameError);
2130 rb_exc_raise(exc);
2133 static VALUE
2134 name_err_init_attr(VALUE exc, VALUE recv, VALUE method)
2136 const rb_execution_context_t *ec = GET_EC();
2137 rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp);
2138 cfp = rb_vm_get_ruby_level_next_cfp(ec, cfp);
2139 rb_ivar_set(exc, id_name, method);
2140 err_init_recv(exc, recv);
2141 if (cfp && VM_FRAME_TYPE(cfp) != VM_FRAME_MAGIC_DUMMY) {
2142 rb_ivar_set(exc, id_iseq, rb_iseqw_new(cfp->iseq));
2144 return exc;
2148 * call-seq:
2149 * NameError.new(msg=nil, name=nil, receiver: nil) -> name_error
2151 * Construct a new NameError exception. If given the <i>name</i>
2152 * parameter may subsequently be examined using the NameError#name
2153 * method. <i>receiver</i> parameter allows to pass object in
2154 * context of which the error happened. Example:
2156 * [1, 2, 3].method(:rject) # NameError with name "rject" and receiver: Array
2157 * [1, 2, 3].singleton_method(:rject) # NameError with name "rject" and receiver: [1, 2, 3]
2160 static VALUE
2161 name_err_initialize(int argc, VALUE *argv, VALUE self)
2163 ID keywords[1];
2164 VALUE values[numberof(keywords)], name, options;
2166 argc = rb_scan_args(argc, argv, "*:", NULL, &options);
2167 keywords[0] = id_receiver;
2168 rb_get_kwargs(options, keywords, 0, numberof(values), values);
2169 name = (argc > 1) ? argv[--argc] : Qnil;
2170 rb_call_super(argc, argv);
2171 name_err_init_attr(self, values[0], name);
2172 return self;
2175 static VALUE rb_name_err_mesg_new(VALUE mesg, VALUE recv, VALUE method);
2177 static VALUE
2178 name_err_init(VALUE exc, VALUE mesg, VALUE recv, VALUE method)
2180 exc_init(exc, rb_name_err_mesg_new(mesg, recv, method));
2181 return name_err_init_attr(exc, recv, method);
2184 VALUE
2185 rb_name_err_new(VALUE mesg, VALUE recv, VALUE method)
2187 VALUE exc = rb_obj_alloc(rb_eNameError);
2188 return name_err_init(exc, mesg, recv, method);
2192 * call-seq:
2193 * name_error.name -> string or nil
2195 * Return the name associated with this NameError exception.
2198 static VALUE
2199 name_err_name(VALUE self)
2201 return rb_attr_get(self, id_name);
2205 * call-seq:
2206 * name_error.local_variables -> array
2208 * Return a list of the local variable names defined where this
2209 * NameError exception was raised.
2211 * Internal use only.
2214 static VALUE
2215 name_err_local_variables(VALUE self)
2217 VALUE vars = rb_attr_get(self, id_local_variables);
2219 if (NIL_P(vars)) {
2220 VALUE iseqw = rb_attr_get(self, id_iseq);
2221 if (!NIL_P(iseqw)) vars = rb_iseqw_local_variables(iseqw);
2222 if (NIL_P(vars)) vars = rb_ary_new();
2223 rb_ivar_set(self, id_local_variables, vars);
2225 return vars;
2228 static VALUE
2229 nometh_err_init_attr(VALUE exc, VALUE args, int priv)
2231 rb_ivar_set(exc, id_args, args);
2232 rb_ivar_set(exc, id_private_call_p, RBOOL(priv));
2233 return exc;
2237 * call-seq:
2238 * NoMethodError.new(msg=nil, name=nil, args=nil, private=false, receiver: nil) -> no_method_error
2240 * Construct a NoMethodError exception for a method of the given name
2241 * called with the given arguments. The name may be accessed using
2242 * the <code>#name</code> method on the resulting object, and the
2243 * arguments using the <code>#args</code> method.
2245 * If <i>private</i> argument were passed, it designates method was
2246 * attempted to call in private context, and can be accessed with
2247 * <code>#private_call?</code> method.
2249 * <i>receiver</i> argument stores an object whose method was called.
2252 static VALUE
2253 nometh_err_initialize(int argc, VALUE *argv, VALUE self)
2255 int priv;
2256 VALUE args, options;
2257 argc = rb_scan_args(argc, argv, "*:", NULL, &options);
2258 priv = (argc > 3) && (--argc, RTEST(argv[argc]));
2259 args = (argc > 2) ? argv[--argc] : Qnil;
2260 if (!NIL_P(options)) argv[argc++] = options;
2261 rb_call_super_kw(argc, argv, RB_PASS_CALLED_KEYWORDS);
2262 return nometh_err_init_attr(self, args, priv);
2265 VALUE
2266 rb_nomethod_err_new(VALUE mesg, VALUE recv, VALUE method, VALUE args, int priv)
2268 VALUE exc = rb_obj_alloc(rb_eNoMethodError);
2269 name_err_init(exc, mesg, recv, method);
2270 return nometh_err_init_attr(exc, args, priv);
2273 typedef struct name_error_message_struct {
2274 VALUE mesg;
2275 VALUE recv;
2276 VALUE name;
2277 } name_error_message_t;
2279 static void
2280 name_err_mesg_mark(void *p)
2282 name_error_message_t *ptr = (name_error_message_t *)p;
2283 rb_gc_mark_movable(ptr->mesg);
2284 rb_gc_mark_movable(ptr->recv);
2285 rb_gc_mark_movable(ptr->name);
2288 static void
2289 name_err_mesg_update(void *p)
2291 name_error_message_t *ptr = (name_error_message_t *)p;
2292 ptr->mesg = rb_gc_location(ptr->mesg);
2293 ptr->recv = rb_gc_location(ptr->recv);
2294 ptr->name = rb_gc_location(ptr->name);
2297 static const rb_data_type_t name_err_mesg_data_type = {
2298 "name_err_mesg",
2300 name_err_mesg_mark,
2301 RUBY_TYPED_DEFAULT_FREE,
2302 NULL, // No external memory to report,
2303 name_err_mesg_update,
2305 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
2308 /* :nodoc: */
2309 static VALUE
2310 rb_name_err_mesg_init(VALUE klass, VALUE mesg, VALUE recv, VALUE name)
2312 name_error_message_t *message;
2313 VALUE result = TypedData_Make_Struct(klass, name_error_message_t, &name_err_mesg_data_type, message);
2314 RB_OBJ_WRITE(result, &message->mesg, mesg);
2315 RB_OBJ_WRITE(result, &message->recv, recv);
2316 RB_OBJ_WRITE(result, &message->name, name);
2317 return result;
2320 /* :nodoc: */
2321 static VALUE
2322 rb_name_err_mesg_new(VALUE mesg, VALUE recv, VALUE method)
2324 return rb_name_err_mesg_init(rb_cNameErrorMesg, mesg, recv, method);
2327 /* :nodoc: */
2328 static VALUE
2329 name_err_mesg_alloc(VALUE klass)
2331 return rb_name_err_mesg_init(klass, Qnil, Qnil, Qnil);
2334 /* :nodoc: */
2335 static VALUE
2336 name_err_mesg_init_copy(VALUE obj1, VALUE obj2)
2338 if (obj1 == obj2) return obj1;
2339 rb_obj_init_copy(obj1, obj2);
2341 name_error_message_t *ptr1, *ptr2;
2342 TypedData_Get_Struct(obj1, name_error_message_t, &name_err_mesg_data_type, ptr1);
2343 TypedData_Get_Struct(obj2, name_error_message_t, &name_err_mesg_data_type, ptr2);
2345 RB_OBJ_WRITE(obj1, &ptr1->mesg, ptr2->mesg);
2346 RB_OBJ_WRITE(obj1, &ptr1->recv, ptr2->recv);
2347 RB_OBJ_WRITE(obj1, &ptr1->name, ptr2->name);
2348 return obj1;
2351 /* :nodoc: */
2352 static VALUE
2353 name_err_mesg_equal(VALUE obj1, VALUE obj2)
2355 if (obj1 == obj2) return Qtrue;
2357 if (rb_obj_class(obj2) != rb_cNameErrorMesg)
2358 return Qfalse;
2360 name_error_message_t *ptr1, *ptr2;
2361 TypedData_Get_Struct(obj1, name_error_message_t, &name_err_mesg_data_type, ptr1);
2362 TypedData_Get_Struct(obj2, name_error_message_t, &name_err_mesg_data_type, ptr2);
2364 if (!rb_equal(ptr1->mesg, ptr2->mesg)) return Qfalse;
2365 if (!rb_equal(ptr1->recv, ptr2->recv)) return Qfalse;
2366 if (!rb_equal(ptr1->name, ptr2->name)) return Qfalse;
2367 return Qtrue;
2370 /* :nodoc: */
2371 static VALUE
2372 name_err_mesg_receiver_name(VALUE obj)
2374 if (RB_SPECIAL_CONST_P(obj)) return Qundef;
2375 if (RB_BUILTIN_TYPE(obj) == T_MODULE || RB_BUILTIN_TYPE(obj) == T_CLASS) {
2376 return rb_check_funcall(obj, rb_intern("name"), 0, 0);
2378 return Qundef;
2381 /* :nodoc: */
2382 static VALUE
2383 name_err_mesg_to_str(VALUE obj)
2385 name_error_message_t *ptr;
2386 TypedData_Get_Struct(obj, name_error_message_t, &name_err_mesg_data_type, ptr);
2388 VALUE mesg = ptr->mesg;
2389 if (NIL_P(mesg)) return Qnil;
2390 else {
2391 struct RString s_str, c_str, d_str;
2392 VALUE c, s, d = 0, args[4], c2;
2393 int state = 0;
2394 rb_encoding *usascii = rb_usascii_encoding();
2396 #define FAKE_CSTR(v, str) rb_setup_fake_str((v), (str), rb_strlen_lit(str), usascii)
2397 c = s = FAKE_CSTR(&s_str, "");
2398 obj = ptr->recv;
2399 switch (obj) {
2400 case Qnil:
2401 c = d = FAKE_CSTR(&d_str, "nil");
2402 break;
2403 case Qtrue:
2404 c = d = FAKE_CSTR(&d_str, "true");
2405 break;
2406 case Qfalse:
2407 c = d = FAKE_CSTR(&d_str, "false");
2408 break;
2409 default:
2410 if (strstr(RSTRING_PTR(mesg), "%2$s")) {
2411 d = rb_protect(name_err_mesg_receiver_name, obj, &state);
2412 if (state || NIL_OR_UNDEF_P(d))
2413 d = rb_protect(rb_inspect, obj, &state);
2414 if (state) {
2415 rb_set_errinfo(Qnil);
2417 d = rb_check_string_type(d);
2418 if (NIL_P(d)) {
2419 d = rb_any_to_s(obj);
2423 if (!RB_SPECIAL_CONST_P(obj)) {
2424 switch (RB_BUILTIN_TYPE(obj)) {
2425 case T_MODULE:
2426 s = FAKE_CSTR(&s_str, "module ");
2427 c = obj;
2428 break;
2429 case T_CLASS:
2430 s = FAKE_CSTR(&s_str, "class ");
2431 c = obj;
2432 break;
2433 default:
2434 goto object;
2437 else {
2438 VALUE klass;
2439 object:
2440 klass = CLASS_OF(obj);
2441 if (RB_TYPE_P(klass, T_CLASS) && RCLASS_SINGLETON_P(klass)) {
2442 s = FAKE_CSTR(&s_str, "");
2443 if (obj == rb_vm_top_self()) {
2444 c = FAKE_CSTR(&c_str, "main");
2446 else {
2447 c = rb_any_to_s(obj);
2449 break;
2451 else {
2452 s = FAKE_CSTR(&s_str, "an instance of ");
2453 c = rb_class_real(klass);
2456 c2 = rb_protect(name_err_mesg_receiver_name, c, &state);
2457 if (state || NIL_OR_UNDEF_P(c2))
2458 c2 = rb_protect(rb_inspect, c, &state);
2459 if (state) {
2460 rb_set_errinfo(Qnil);
2462 c2 = rb_check_string_type(c2);
2463 if (NIL_P(c2)) {
2464 c2 = rb_any_to_s(c);
2466 c = c2;
2467 break;
2469 args[0] = rb_obj_as_string(ptr->name);
2470 args[1] = d;
2471 args[2] = s;
2472 args[3] = c;
2473 mesg = rb_str_format(4, args, mesg);
2475 return mesg;
2478 /* :nodoc: */
2479 static VALUE
2480 name_err_mesg_dump(VALUE obj, VALUE limit)
2482 return name_err_mesg_to_str(obj);
2485 /* :nodoc: */
2486 static VALUE
2487 name_err_mesg_load(VALUE klass, VALUE str)
2489 return str;
2493 * call-seq:
2494 * name_error.receiver -> object
2496 * Return the receiver associated with this NameError exception.
2499 static VALUE
2500 name_err_receiver(VALUE self)
2502 VALUE recv = rb_ivar_lookup(self, id_recv, Qundef);
2503 if (!UNDEF_P(recv)) return recv;
2505 VALUE mesg = rb_attr_get(self, id_mesg);
2506 if (!rb_typeddata_is_kind_of(mesg, &name_err_mesg_data_type)) {
2507 rb_raise(rb_eArgError, "no receiver is available");
2510 name_error_message_t *ptr;
2511 TypedData_Get_Struct(mesg, name_error_message_t, &name_err_mesg_data_type, ptr);
2512 return ptr->recv;
2516 * call-seq:
2517 * no_method_error.args -> obj
2519 * Return the arguments passed in as the third parameter to
2520 * the constructor.
2523 static VALUE
2524 nometh_err_args(VALUE self)
2526 return rb_attr_get(self, id_args);
2530 * call-seq:
2531 * no_method_error.private_call? -> true or false
2533 * Return true if the caused method was called as private.
2536 static VALUE
2537 nometh_err_private_call_p(VALUE self)
2539 return rb_attr_get(self, id_private_call_p);
2542 void
2543 rb_invalid_str(const char *str, const char *type)
2545 VALUE s = rb_str_new2(str);
2547 rb_raise(rb_eArgError, "invalid value for %s: %+"PRIsVALUE, type, s);
2551 * call-seq:
2552 * key_error.receiver -> object
2554 * Return the receiver associated with this KeyError exception.
2557 static VALUE
2558 key_err_receiver(VALUE self)
2560 VALUE recv;
2562 recv = rb_ivar_lookup(self, id_receiver, Qundef);
2563 if (!UNDEF_P(recv)) return recv;
2564 rb_raise(rb_eArgError, "no receiver is available");
2568 * call-seq:
2569 * key_error.key -> object
2571 * Return the key caused this KeyError exception.
2574 static VALUE
2575 key_err_key(VALUE self)
2577 VALUE key;
2579 key = rb_ivar_lookup(self, id_key, Qundef);
2580 if (!UNDEF_P(key)) return key;
2581 rb_raise(rb_eArgError, "no key is available");
2584 VALUE
2585 rb_key_err_new(VALUE mesg, VALUE recv, VALUE key)
2587 VALUE exc = rb_obj_alloc(rb_eKeyError);
2588 rb_ivar_set(exc, id_mesg, mesg);
2589 rb_ivar_set(exc, id_bt, Qnil);
2590 rb_ivar_set(exc, id_key, key);
2591 rb_ivar_set(exc, id_receiver, recv);
2592 return exc;
2596 * call-seq:
2597 * KeyError.new(message=nil, receiver: nil, key: nil) -> key_error
2599 * Construct a new +KeyError+ exception with the given message,
2600 * receiver and key.
2603 static VALUE
2604 key_err_initialize(int argc, VALUE *argv, VALUE self)
2606 VALUE options;
2608 rb_call_super(rb_scan_args(argc, argv, "01:", NULL, &options), argv);
2610 if (!NIL_P(options)) {
2611 ID keywords[2];
2612 VALUE values[numberof(keywords)];
2613 int i;
2614 keywords[0] = id_receiver;
2615 keywords[1] = id_key;
2616 rb_get_kwargs(options, keywords, 0, numberof(values), values);
2617 for (i = 0; i < numberof(values); ++i) {
2618 if (!UNDEF_P(values[i])) {
2619 rb_ivar_set(self, keywords[i], values[i]);
2624 return self;
2628 * call-seq:
2629 * no_matching_pattern_key_error.matchee -> object
2631 * Return the matchee associated with this NoMatchingPatternKeyError exception.
2634 static VALUE
2635 no_matching_pattern_key_err_matchee(VALUE self)
2637 VALUE matchee;
2639 matchee = rb_ivar_lookup(self, id_matchee, Qundef);
2640 if (!UNDEF_P(matchee)) return matchee;
2641 rb_raise(rb_eArgError, "no matchee is available");
2645 * call-seq:
2646 * no_matching_pattern_key_error.key -> object
2648 * Return the key caused this NoMatchingPatternKeyError exception.
2651 static VALUE
2652 no_matching_pattern_key_err_key(VALUE self)
2654 VALUE key;
2656 key = rb_ivar_lookup(self, id_key, Qundef);
2657 if (!UNDEF_P(key)) return key;
2658 rb_raise(rb_eArgError, "no key is available");
2662 * call-seq:
2663 * NoMatchingPatternKeyError.new(message=nil, matchee: nil, key: nil) -> no_matching_pattern_key_error
2665 * Construct a new +NoMatchingPatternKeyError+ exception with the given message,
2666 * matchee and key.
2669 static VALUE
2670 no_matching_pattern_key_err_initialize(int argc, VALUE *argv, VALUE self)
2672 VALUE options;
2674 rb_call_super(rb_scan_args(argc, argv, "01:", NULL, &options), argv);
2676 if (!NIL_P(options)) {
2677 ID keywords[2];
2678 VALUE values[numberof(keywords)];
2679 int i;
2680 keywords[0] = id_matchee;
2681 keywords[1] = id_key;
2682 rb_get_kwargs(options, keywords, 0, numberof(values), values);
2683 for (i = 0; i < numberof(values); ++i) {
2684 if (!UNDEF_P(values[i])) {
2685 rb_ivar_set(self, keywords[i], values[i]);
2690 return self;
2695 * call-seq:
2696 * SyntaxError.new([msg]) -> syntax_error
2698 * Construct a SyntaxError exception.
2701 static VALUE
2702 syntax_error_initialize(int argc, VALUE *argv, VALUE self)
2704 VALUE mesg;
2705 if (argc == 0) {
2706 mesg = rb_fstring_lit("compile error");
2707 argc = 1;
2708 argv = &mesg;
2710 return rb_call_super(argc, argv);
2713 static VALUE
2714 syntax_error_with_path(VALUE exc, VALUE path, VALUE *mesg, rb_encoding *enc)
2716 if (NIL_P(exc)) {
2717 *mesg = rb_enc_str_new(0, 0, enc);
2718 exc = rb_class_new_instance(1, mesg, rb_eSyntaxError);
2719 rb_ivar_set(exc, id_i_path, path);
2721 else {
2722 VALUE old_path = rb_attr_get(exc, id_i_path);
2723 if (old_path != path) {
2724 if (rb_str_equal(path, old_path)) {
2725 rb_raise(rb_eArgError, "SyntaxError#path changed: %+"PRIsVALUE" (%p->%p)",
2726 old_path, (void *)old_path, (void *)path);
2728 else {
2729 rb_raise(rb_eArgError, "SyntaxError#path changed: %+"PRIsVALUE"(%s%s)->%+"PRIsVALUE"(%s)",
2730 old_path, rb_enc_name(rb_enc_get(old_path)),
2731 (FL_TEST(old_path, RSTRING_FSTR) ? ":FSTR" : ""),
2732 path, rb_enc_name(rb_enc_get(path)));
2735 VALUE s = *mesg = rb_attr_get(exc, idMesg);
2736 if (RSTRING_LEN(s) > 0 && *(RSTRING_END(s)-1) != '\n')
2737 rb_str_cat_cstr(s, "\n");
2739 return exc;
2743 * Document-module: Errno
2745 * Ruby exception objects are subclasses of Exception. However,
2746 * operating systems typically report errors using plain
2747 * integers. Module Errno is created dynamically to map these
2748 * operating system errors to Ruby classes, with each error number
2749 * generating its own subclass of SystemCallError. As the subclass
2750 * is created in module Errno, its name will start
2751 * <code>Errno::</code>.
2753 * The names of the <code>Errno::</code> classes depend on the
2754 * environment in which Ruby runs. On a typical Unix or Windows
2755 * platform, there are Errno classes such as Errno::EACCES,
2756 * Errno::EAGAIN, Errno::EINTR, and so on.
2758 * The integer operating system error number corresponding to a
2759 * particular error is available as the class constant
2760 * <code>Errno::</code><em>error</em><code>::Errno</code>.
2762 * Errno::EACCES::Errno #=> 13
2763 * Errno::EAGAIN::Errno #=> 11
2764 * Errno::EINTR::Errno #=> 4
2766 * The full list of operating system errors on your particular platform
2767 * are available as the constants of Errno.
2769 * Errno.constants #=> :E2BIG, :EACCES, :EADDRINUSE, :EADDRNOTAVAIL, ...
2772 static st_table *syserr_tbl;
2774 void
2775 rb_free_warning(void)
2777 st_free_table(warning_categories.id2enum);
2778 st_free_table(warning_categories.enum2id);
2779 st_free_table(syserr_tbl);
2782 static VALUE
2783 setup_syserr(int n, const char *name)
2785 VALUE error = rb_define_class_under(rb_mErrno, name, rb_eSystemCallError);
2787 /* capture nonblock errnos for WaitReadable/WaitWritable subclasses */
2788 switch (n) {
2789 case EAGAIN:
2790 rb_eEAGAIN = error;
2792 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
2793 break;
2794 case EWOULDBLOCK:
2795 #endif
2797 rb_eEWOULDBLOCK = error;
2798 break;
2799 case EINPROGRESS:
2800 rb_eEINPROGRESS = error;
2801 break;
2804 rb_define_const(error, "Errno", INT2NUM(n));
2805 st_add_direct(syserr_tbl, n, (st_data_t)error);
2806 return error;
2809 static VALUE
2810 set_syserr(int n, const char *name)
2812 st_data_t error;
2814 if (!st_lookup(syserr_tbl, n, &error)) {
2815 return setup_syserr(n, name);
2817 else {
2818 VALUE errclass = (VALUE)error;
2819 rb_define_const(rb_mErrno, name, errclass);
2820 return errclass;
2824 static VALUE
2825 get_syserr(int n)
2827 st_data_t error;
2829 if (!st_lookup(syserr_tbl, n, &error)) {
2830 char name[DECIMAL_SIZE_OF(n) + sizeof("E-")];
2832 snprintf(name, sizeof(name), "E%03d", n);
2833 return setup_syserr(n, name);
2835 return (VALUE)error;
2839 * call-seq:
2840 * SystemCallError.new(msg, errno) -> system_call_error_subclass
2842 * If _errno_ corresponds to a known system error code, constructs the
2843 * appropriate Errno class for that error, otherwise constructs a
2844 * generic SystemCallError object. The error number is subsequently
2845 * available via the #errno method.
2848 static VALUE
2849 syserr_initialize(int argc, VALUE *argv, VALUE self)
2851 const char *err;
2852 VALUE mesg, error, func, errmsg;
2853 VALUE klass = rb_obj_class(self);
2855 if (klass == rb_eSystemCallError) {
2856 st_data_t data = (st_data_t)klass;
2857 rb_scan_args(argc, argv, "12", &mesg, &error, &func);
2858 if (argc == 1 && FIXNUM_P(mesg)) {
2859 error = mesg; mesg = Qnil;
2861 if (!NIL_P(error) && st_lookup(syserr_tbl, NUM2LONG(error), &data)) {
2862 klass = (VALUE)data;
2863 /* change class */
2864 if (!RB_TYPE_P(self, T_OBJECT)) { /* insurance to avoid type crash */
2865 rb_raise(rb_eTypeError, "invalid instance type");
2867 RBASIC_SET_CLASS(self, klass);
2870 else {
2871 rb_scan_args(argc, argv, "02", &mesg, &func);
2872 error = rb_const_get(klass, id_Errno);
2874 if (!NIL_P(error)) err = strerror(NUM2INT(error));
2875 else err = "unknown error";
2877 errmsg = rb_enc_str_new_cstr(err, rb_locale_encoding());
2878 if (!NIL_P(mesg)) {
2879 VALUE str = StringValue(mesg);
2881 if (!NIL_P(func)) rb_str_catf(errmsg, " @ %"PRIsVALUE, func);
2882 rb_str_catf(errmsg, " - %"PRIsVALUE, str);
2884 mesg = errmsg;
2886 rb_call_super(1, &mesg);
2887 rb_ivar_set(self, id_errno, error);
2888 return self;
2892 * call-seq:
2893 * system_call_error.errno -> integer
2895 * Return this SystemCallError's error number.
2898 static VALUE
2899 syserr_errno(VALUE self)
2901 return rb_attr_get(self, id_errno);
2905 * call-seq:
2906 * system_call_error === other -> true or false
2908 * Return +true+ if the receiver is a generic +SystemCallError+, or
2909 * if the error numbers +self+ and _other_ are the same.
2912 static VALUE
2913 syserr_eqq(VALUE self, VALUE exc)
2915 VALUE num, e;
2917 if (!rb_obj_is_kind_of(exc, rb_eSystemCallError)) {
2918 if (!rb_respond_to(exc, id_errno)) return Qfalse;
2920 else if (self == rb_eSystemCallError) return Qtrue;
2922 num = rb_attr_get(exc, id_errno);
2923 if (NIL_P(num)) {
2924 num = rb_funcallv(exc, id_errno, 0, 0);
2926 e = rb_const_get(self, id_Errno);
2927 return RBOOL(FIXNUM_P(num) ? num == e : rb_equal(num, e));
2932 * Document-class: StandardError
2934 * The most standard error types are subclasses of StandardError. A
2935 * rescue clause without an explicit Exception class will rescue all
2936 * StandardErrors (and only those).
2938 * def foo
2939 * raise "Oups"
2940 * end
2941 * foo rescue "Hello" #=> "Hello"
2943 * On the other hand:
2945 * require 'does/not/exist' rescue "Hi"
2947 * <em>raises the exception:</em>
2949 * LoadError: no such file to load -- does/not/exist
2954 * Document-class: SystemExit
2956 * Raised by +exit+ to initiate the termination of the script.
2960 * Document-class: SignalException
2962 * Raised when a signal is received.
2964 * begin
2965 * Process.kill('HUP',Process.pid)
2966 * sleep # wait for receiver to handle signal sent by Process.kill
2967 * rescue SignalException => e
2968 * puts "received Exception #{e}"
2969 * end
2971 * <em>produces:</em>
2973 * received Exception SIGHUP
2977 * Document-class: Interrupt
2979 * Raised when the interrupt signal is received, typically because the
2980 * user has pressed Control-C (on most posix platforms). As such, it is a
2981 * subclass of +SignalException+.
2983 * begin
2984 * puts "Press ctrl-C when you get bored"
2985 * loop {}
2986 * rescue Interrupt => e
2987 * puts "Note: You will typically use Signal.trap instead."
2988 * end
2990 * <em>produces:</em>
2992 * Press ctrl-C when you get bored
2994 * <em>then waits until it is interrupted with Control-C and then prints:</em>
2996 * Note: You will typically use Signal.trap instead.
3000 * Document-class: TypeError
3002 * Raised when encountering an object that is not of the expected type.
3004 * [1, 2, 3].first("two")
3006 * <em>raises the exception:</em>
3008 * TypeError: no implicit conversion of String into Integer
3013 * Document-class: ArgumentError
3015 * Raised when the arguments are wrong and there isn't a more specific
3016 * Exception class.
3018 * Ex: passing the wrong number of arguments
3020 * [1, 2, 3].first(4, 5)
3022 * <em>raises the exception:</em>
3024 * ArgumentError: wrong number of arguments (given 2, expected 1)
3026 * Ex: passing an argument that is not acceptable:
3028 * [1, 2, 3].first(-4)
3030 * <em>raises the exception:</em>
3032 * ArgumentError: negative array size
3036 * Document-class: IndexError
3038 * Raised when the given index is invalid.
3040 * a = [:foo, :bar]
3041 * a.fetch(0) #=> :foo
3042 * a[4] #=> nil
3043 * a.fetch(4) #=> IndexError: index 4 outside of array bounds: -2...2
3048 * Document-class: KeyError
3050 * Raised when the specified key is not found. It is a subclass of
3051 * IndexError.
3053 * h = {"foo" => :bar}
3054 * h.fetch("foo") #=> :bar
3055 * h.fetch("baz") #=> KeyError: key not found: "baz"
3060 * Document-class: RangeError
3062 * Raised when a given numerical value is out of range.
3064 * [1, 2, 3].drop(1 << 100)
3066 * <em>raises the exception:</em>
3068 * RangeError: bignum too big to convert into `long'
3072 * Document-class: ScriptError
3074 * ScriptError is the superclass for errors raised when a script
3075 * can not be executed because of a +LoadError+,
3076 * +NotImplementedError+ or a +SyntaxError+. Note these type of
3077 * +ScriptErrors+ are not +StandardError+ and will not be
3078 * rescued unless it is specified explicitly (or its ancestor
3079 * +Exception+).
3083 * Document-class: SyntaxError
3085 * Raised when encountering Ruby code with an invalid syntax.
3087 * eval("1+1=2")
3089 * <em>raises the exception:</em>
3091 * SyntaxError: (eval):1: syntax error, unexpected '=', expecting $end
3095 * Document-class: LoadError
3097 * Raised when a file required (a Ruby script, extension library, ...)
3098 * fails to load.
3100 * require 'this/file/does/not/exist'
3102 * <em>raises the exception:</em>
3104 * LoadError: no such file to load -- this/file/does/not/exist
3108 * Document-class: NotImplementedError
3110 * Raised when a feature is not implemented on the current platform. For
3111 * example, methods depending on the +fsync+ or +fork+ system calls may
3112 * raise this exception if the underlying operating system or Ruby
3113 * runtime does not support them.
3115 * Note that if +fork+ raises a +NotImplementedError+, then
3116 * <code>respond_to?(:fork)</code> returns +false+.
3120 * Document-class: NameError
3122 * Raised when a given name is invalid or undefined.
3124 * puts foo
3126 * <em>raises the exception:</em>
3128 * NameError: undefined local variable or method `foo' for main:Object
3130 * Since constant names must start with a capital:
3132 * Integer.const_set :answer, 42
3134 * <em>raises the exception:</em>
3136 * NameError: wrong constant name answer
3140 * Document-class: NoMethodError
3142 * Raised when a method is called on a receiver which doesn't have it
3143 * defined and also fails to respond with +method_missing+.
3145 * "hello".to_ary
3147 * <em>raises the exception:</em>
3149 * NoMethodError: undefined method `to_ary' for an instance of String
3153 * Document-class: FrozenError
3155 * Raised when there is an attempt to modify a frozen object.
3157 * [1, 2, 3].freeze << 4
3159 * <em>raises the exception:</em>
3161 * FrozenError: can't modify frozen Array
3165 * Document-class: RuntimeError
3167 * A generic error class raised when an invalid operation is attempted.
3168 * Kernel#raise will raise a RuntimeError if no Exception class is
3169 * specified.
3171 * raise "ouch"
3173 * <em>raises the exception:</em>
3175 * RuntimeError: ouch
3179 * Document-class: SecurityError
3181 * No longer used by internal code.
3185 * Document-class: NoMemoryError
3187 * Raised when memory allocation fails.
3191 * Document-class: SystemCallError
3193 * SystemCallError is the base class for all low-level
3194 * platform-dependent errors.
3196 * The errors available on the current platform are subclasses of
3197 * SystemCallError and are defined in the Errno module.
3199 * File.open("does/not/exist")
3201 * <em>raises the exception:</em>
3203 * Errno::ENOENT: No such file or directory - does/not/exist
3207 * Document-class: EncodingError
3209 * EncodingError is the base class for encoding errors.
3213 * Document-class: Encoding::CompatibilityError
3215 * Raised by Encoding and String methods when the source encoding is
3216 * incompatible with the target encoding.
3220 * Document-class: fatal
3222 * +fatal+ is an Exception that is raised when Ruby has encountered a fatal
3223 * error and must exit.
3227 * Document-class: NameError::message
3228 * :nodoc:
3232 * Document-class: Exception
3234 * \Class Exception and its subclasses are used to communicate between
3235 * Kernel#raise and +rescue+ statements in <code>begin ... end</code> blocks.
3237 * An Exception object carries information about an exception:
3238 * - Its type (the exception's class).
3239 * - An optional descriptive message.
3240 * - Optional backtrace information.
3242 * Some built-in subclasses of Exception have additional methods: e.g., NameError#name.
3244 * == Defaults
3246 * Two Ruby statements have default exception classes:
3247 * - +raise+: defaults to RuntimeError.
3248 * - +rescue+: defaults to StandardError.
3250 * == Global Variables
3252 * When an exception has been raised but not yet handled (in +rescue+,
3253 * +ensure+, +at_exit+ and +END+ blocks), two global variables are set:
3254 * - <code>$!</code> contains the current exception.
3255 * - <code>$@</code> contains its backtrace.
3257 * == Custom Exceptions
3259 * To provide additional or alternate information,
3260 * a program may create custom exception classes
3261 * that derive from the built-in exception classes.
3263 * A good practice is for a library to create a single "generic" exception class
3264 * (typically a subclass of StandardError or RuntimeError)
3265 * and have its other exception classes derive from that class.
3266 * This allows the user to rescue the generic exception, thus catching all exceptions
3267 * the library may raise even if future versions of the library add new
3268 * exception subclasses.
3270 * For example:
3272 * class MyLibrary
3273 * class Error < ::StandardError
3274 * end
3276 * class WidgetError < Error
3277 * end
3279 * class FrobError < Error
3280 * end
3282 * end
3284 * To handle both MyLibrary::WidgetError and MyLibrary::FrobError the library
3285 * user can rescue MyLibrary::Error.
3287 * == Built-In Exception Classes
3289 * The built-in subclasses of Exception are:
3291 * * NoMemoryError
3292 * * ScriptError
3293 * * LoadError
3294 * * NotImplementedError
3295 * * SyntaxError
3296 * * SecurityError
3297 * * SignalException
3298 * * Interrupt
3299 * * StandardError
3300 * * ArgumentError
3301 * * UncaughtThrowError
3302 * * EncodingError
3303 * * FiberError
3304 * * IOError
3305 * * EOFError
3306 * * IndexError
3307 * * KeyError
3308 * * StopIteration
3309 * * ClosedQueueError
3310 * * LocalJumpError
3311 * * NameError
3312 * * NoMethodError
3313 * * RangeError
3314 * * FloatDomainError
3315 * * RegexpError
3316 * * RuntimeError
3317 * * FrozenError
3318 * * SystemCallError
3319 * * Errno::*
3320 * * ThreadError
3321 * * TypeError
3322 * * ZeroDivisionError
3323 * * SystemExit
3324 * * SystemStackError
3325 * * fatal
3328 static VALUE
3329 exception_alloc(VALUE klass)
3331 return rb_class_allocate_instance(klass);
3334 static VALUE
3335 exception_dumper(VALUE exc)
3337 // TODO: Currently, the instance variables "bt" and "bt_locations"
3338 // refers to the same object (Array of String). But "bt_locations"
3339 // should have an Array of Thread::Backtrace::Locations.
3341 return exc;
3344 static int
3345 ivar_copy_i(ID key, VALUE val, st_data_t exc)
3347 rb_ivar_set((VALUE)exc, key, val);
3348 return ST_CONTINUE;
3351 void rb_exc_check_circular_cause(VALUE exc);
3353 static VALUE
3354 exception_loader(VALUE exc, VALUE obj)
3356 // The loader function of rb_marshal_define_compat seems to be called for two events:
3357 // one is for fixup (r_fixup_compat), the other is for TYPE_USERDEF.
3358 // In the former case, the first argument is an instance of Exception (because
3359 // we pass rb_eException to rb_marshal_define_compat). In the latter case, the first
3360 // argument is a class object (see TYPE_USERDEF case in r_object0).
3361 // We want to copy all instance variables (but "bt_locations") from obj to exc.
3362 // But we do not want to do so in the second case, so the following branch is for that.
3363 if (RB_TYPE_P(exc, T_CLASS)) return obj; // maybe called from Marshal's TYPE_USERDEF
3365 rb_ivar_foreach(obj, ivar_copy_i, exc);
3367 rb_exc_check_circular_cause(exc);
3369 if (rb_attr_get(exc, id_bt) == rb_attr_get(exc, id_bt_locations)) {
3370 rb_ivar_set(exc, id_bt_locations, Qnil);
3373 return exc;
3376 void
3377 Init_Exception(void)
3379 rb_eException = rb_define_class("Exception", rb_cObject);
3380 rb_define_alloc_func(rb_eException, exception_alloc);
3381 rb_marshal_define_compat(rb_eException, rb_eException, exception_dumper, exception_loader);
3382 rb_define_singleton_method(rb_eException, "exception", rb_class_new_instance, -1);
3383 rb_define_singleton_method(rb_eException, "to_tty?", exc_s_to_tty_p, 0);
3384 rb_define_method(rb_eException, "exception", exc_exception, -1);
3385 rb_define_method(rb_eException, "initialize", exc_initialize, -1);
3386 rb_define_method(rb_eException, "==", exc_equal, 1);
3387 rb_define_method(rb_eException, "to_s", exc_to_s, 0);
3388 rb_define_method(rb_eException, "message", exc_message, 0);
3389 rb_define_method(rb_eException, "detailed_message", exc_detailed_message, -1);
3390 rb_define_method(rb_eException, "full_message", exc_full_message, -1);
3391 rb_define_method(rb_eException, "inspect", exc_inspect, 0);
3392 rb_define_method(rb_eException, "backtrace", exc_backtrace, 0);
3393 rb_define_method(rb_eException, "backtrace_locations", exc_backtrace_locations, 0);
3394 rb_define_method(rb_eException, "set_backtrace", exc_set_backtrace, 1);
3395 rb_define_method(rb_eException, "cause", exc_cause, 0);
3397 rb_eSystemExit = rb_define_class("SystemExit", rb_eException);
3398 rb_define_method(rb_eSystemExit, "initialize", exit_initialize, -1);
3399 rb_define_method(rb_eSystemExit, "status", exit_status, 0);
3400 rb_define_method(rb_eSystemExit, "success?", exit_success_p, 0);
3402 rb_eFatal = rb_define_class("fatal", rb_eException);
3403 rb_eSignal = rb_define_class("SignalException", rb_eException);
3404 rb_eInterrupt = rb_define_class("Interrupt", rb_eSignal);
3406 rb_eStandardError = rb_define_class("StandardError", rb_eException);
3407 rb_eTypeError = rb_define_class("TypeError", rb_eStandardError);
3408 rb_eArgError = rb_define_class("ArgumentError", rb_eStandardError);
3409 rb_eIndexError = rb_define_class("IndexError", rb_eStandardError);
3410 rb_eKeyError = rb_define_class("KeyError", rb_eIndexError);
3411 rb_define_method(rb_eKeyError, "initialize", key_err_initialize, -1);
3412 rb_define_method(rb_eKeyError, "receiver", key_err_receiver, 0);
3413 rb_define_method(rb_eKeyError, "key", key_err_key, 0);
3414 rb_eRangeError = rb_define_class("RangeError", rb_eStandardError);
3416 rb_eScriptError = rb_define_class("ScriptError", rb_eException);
3417 rb_eSyntaxError = rb_define_class("SyntaxError", rb_eScriptError);
3418 rb_define_method(rb_eSyntaxError, "initialize", syntax_error_initialize, -1);
3420 /* RDoc will use literal name value while parsing rb_attr,
3421 * and will render `idPath` as an attribute name without this trick */
3422 ID path = idPath;
3424 /* the path failed to parse */
3425 rb_attr(rb_eSyntaxError, path, TRUE, FALSE, FALSE);
3427 rb_eLoadError = rb_define_class("LoadError", rb_eScriptError);
3428 /* the path failed to load */
3429 rb_attr(rb_eLoadError, path, TRUE, FALSE, FALSE);
3431 rb_eNotImpError = rb_define_class("NotImplementedError", rb_eScriptError);
3433 rb_eNameError = rb_define_class("NameError", rb_eStandardError);
3434 rb_define_method(rb_eNameError, "initialize", name_err_initialize, -1);
3435 rb_define_method(rb_eNameError, "name", name_err_name, 0);
3436 rb_define_method(rb_eNameError, "receiver", name_err_receiver, 0);
3437 rb_define_method(rb_eNameError, "local_variables", name_err_local_variables, 0);
3438 rb_cNameErrorMesg = rb_define_class_under(rb_eNameError, "message", rb_cObject);
3439 rb_define_alloc_func(rb_cNameErrorMesg, name_err_mesg_alloc);
3440 rb_define_method(rb_cNameErrorMesg, "initialize_copy", name_err_mesg_init_copy, 1);
3441 rb_define_method(rb_cNameErrorMesg, "==", name_err_mesg_equal, 1);
3442 rb_define_method(rb_cNameErrorMesg, "to_str", name_err_mesg_to_str, 0);
3443 rb_define_method(rb_cNameErrorMesg, "_dump", name_err_mesg_dump, 1);
3444 rb_define_singleton_method(rb_cNameErrorMesg, "_load", name_err_mesg_load, 1);
3445 rb_eNoMethodError = rb_define_class("NoMethodError", rb_eNameError);
3446 rb_define_method(rb_eNoMethodError, "initialize", nometh_err_initialize, -1);
3447 rb_define_method(rb_eNoMethodError, "args", nometh_err_args, 0);
3448 rb_define_method(rb_eNoMethodError, "private_call?", nometh_err_private_call_p, 0);
3450 rb_eRuntimeError = rb_define_class("RuntimeError", rb_eStandardError);
3451 rb_eFrozenError = rb_define_class("FrozenError", rb_eRuntimeError);
3452 rb_define_method(rb_eFrozenError, "initialize", frozen_err_initialize, -1);
3453 rb_define_method(rb_eFrozenError, "receiver", frozen_err_receiver, 0);
3454 rb_eSecurityError = rb_define_class("SecurityError", rb_eException);
3455 rb_eNoMemError = rb_define_class("NoMemoryError", rb_eException);
3456 rb_eEncodingError = rb_define_class("EncodingError", rb_eStandardError);
3457 rb_eEncCompatError = rb_define_class_under(rb_cEncoding, "CompatibilityError", rb_eEncodingError);
3458 rb_eNoMatchingPatternError = rb_define_class("NoMatchingPatternError", rb_eStandardError);
3459 rb_eNoMatchingPatternKeyError = rb_define_class("NoMatchingPatternKeyError", rb_eNoMatchingPatternError);
3460 rb_define_method(rb_eNoMatchingPatternKeyError, "initialize", no_matching_pattern_key_err_initialize, -1);
3461 rb_define_method(rb_eNoMatchingPatternKeyError, "matchee", no_matching_pattern_key_err_matchee, 0);
3462 rb_define_method(rb_eNoMatchingPatternKeyError, "key", no_matching_pattern_key_err_key, 0);
3464 syserr_tbl = st_init_numtable();
3465 rb_eSystemCallError = rb_define_class("SystemCallError", rb_eStandardError);
3466 rb_define_method(rb_eSystemCallError, "initialize", syserr_initialize, -1);
3467 rb_define_method(rb_eSystemCallError, "errno", syserr_errno, 0);
3468 rb_define_singleton_method(rb_eSystemCallError, "===", syserr_eqq, 1);
3470 rb_mErrno = rb_define_module("Errno");
3472 rb_mWarning = rb_define_module("Warning");
3473 rb_define_singleton_method(rb_mWarning, "[]", rb_warning_s_aref, 1);
3474 rb_define_singleton_method(rb_mWarning, "[]=", rb_warning_s_aset, 2);
3475 rb_define_singleton_method(rb_mWarning, "categories", rb_warning_s_categories, 0);
3476 rb_define_method(rb_mWarning, "warn", rb_warning_s_warn, -1);
3477 rb_extend_object(rb_mWarning, rb_mWarning);
3479 /* :nodoc: */
3480 rb_cWarningBuffer = rb_define_class_under(rb_mWarning, "buffer", rb_cString);
3481 rb_define_method(rb_cWarningBuffer, "write", warning_write, -1);
3483 id_cause = rb_intern_const("cause");
3484 id_message = rb_intern_const("message");
3485 id_detailed_message = rb_intern_const("detailed_message");
3486 id_backtrace = rb_intern_const("backtrace");
3487 id_key = rb_intern_const("key");
3488 id_matchee = rb_intern_const("matchee");
3489 id_args = rb_intern_const("args");
3490 id_receiver = rb_intern_const("receiver");
3491 id_private_call_p = rb_intern_const("private_call?");
3492 id_local_variables = rb_intern_const("local_variables");
3493 id_Errno = rb_intern_const("Errno");
3494 id_errno = rb_intern_const("errno");
3495 id_i_path = rb_intern_const("@path");
3496 id_warn = rb_intern_const("warn");
3497 id_category = rb_intern_const("category");
3498 id_deprecated = rb_intern_const("deprecated");
3499 id_experimental = rb_intern_const("experimental");
3500 id_performance = rb_intern_const("performance");
3501 id_top = rb_intern_const("top");
3502 id_bottom = rb_intern_const("bottom");
3503 id_iseq = rb_make_internal_id();
3504 id_recv = rb_make_internal_id();
3506 sym_category = ID2SYM(id_category);
3507 sym_highlight = ID2SYM(rb_intern_const("highlight"));
3509 warning_categories.id2enum = rb_init_identtable();
3510 st_add_direct(warning_categories.id2enum, id_deprecated, RB_WARN_CATEGORY_DEPRECATED);
3511 st_add_direct(warning_categories.id2enum, id_experimental, RB_WARN_CATEGORY_EXPERIMENTAL);
3512 st_add_direct(warning_categories.id2enum, id_performance, RB_WARN_CATEGORY_PERFORMANCE);
3514 warning_categories.enum2id = rb_init_identtable();
3515 st_add_direct(warning_categories.enum2id, RB_WARN_CATEGORY_NONE, 0);
3516 st_add_direct(warning_categories.enum2id, RB_WARN_CATEGORY_DEPRECATED, id_deprecated);
3517 st_add_direct(warning_categories.enum2id, RB_WARN_CATEGORY_EXPERIMENTAL, id_experimental);
3518 st_add_direct(warning_categories.enum2id, RB_WARN_CATEGORY_PERFORMANCE, id_performance);
3521 void
3522 rb_enc_raise(rb_encoding *enc, VALUE exc, const char *fmt, ...)
3524 va_list args;
3525 VALUE mesg;
3527 va_start(args, fmt);
3528 mesg = rb_enc_vsprintf(enc, fmt, args);
3529 va_end(args);
3531 rb_exc_raise(rb_exc_new3(exc, mesg));
3534 void
3535 rb_vraise(VALUE exc, const char *fmt, va_list ap)
3537 rb_exc_raise(rb_exc_new3(exc, rb_vsprintf(fmt, ap)));
3540 void
3541 rb_raise(VALUE exc, const char *fmt, ...)
3543 va_list args;
3544 va_start(args, fmt);
3545 rb_vraise(exc, fmt, args);
3546 va_end(args);
3549 NORETURN(static void raise_loaderror(VALUE path, VALUE mesg));
3551 static void
3552 raise_loaderror(VALUE path, VALUE mesg)
3554 VALUE err = rb_exc_new3(rb_eLoadError, mesg);
3555 rb_ivar_set(err, id_i_path, path);
3556 rb_exc_raise(err);
3559 void
3560 rb_loaderror(const char *fmt, ...)
3562 va_list args;
3563 VALUE mesg;
3565 va_start(args, fmt);
3566 mesg = rb_enc_vsprintf(rb_locale_encoding(), fmt, args);
3567 va_end(args);
3568 raise_loaderror(Qnil, mesg);
3571 void
3572 rb_loaderror_with_path(VALUE path, const char *fmt, ...)
3574 va_list args;
3575 VALUE mesg;
3577 va_start(args, fmt);
3578 mesg = rb_enc_vsprintf(rb_locale_encoding(), fmt, args);
3579 va_end(args);
3580 raise_loaderror(path, mesg);
3583 void
3584 rb_notimplement(void)
3586 rb_raise(rb_eNotImpError,
3587 "%"PRIsVALUE"() function is unimplemented on this machine",
3588 rb_id2str(rb_frame_this_func()));
3591 void
3592 rb_fatal(const char *fmt, ...)
3594 va_list args;
3595 VALUE mesg;
3597 if (! ruby_thread_has_gvl_p()) {
3598 /* The thread has no GVL. Object allocation impossible (cant run GC),
3599 * thus no message can be printed out. */
3600 fprintf(stderr, "[FATAL] rb_fatal() outside of GVL\n");
3601 rb_print_backtrace(stderr);
3602 die();
3605 va_start(args, fmt);
3606 mesg = rb_vsprintf(fmt, args);
3607 va_end(args);
3609 rb_exc_fatal(rb_exc_new3(rb_eFatal, mesg));
3612 static VALUE
3613 make_errno_exc(const char *mesg)
3615 int n = errno;
3617 errno = 0;
3618 if (n == 0) {
3619 rb_bug("rb_sys_fail(%s) - errno == 0", mesg ? mesg : "");
3621 return rb_syserr_new(n, mesg);
3624 static VALUE
3625 make_errno_exc_str(VALUE mesg)
3627 int n = errno;
3629 errno = 0;
3630 if (!mesg) mesg = Qnil;
3631 if (n == 0) {
3632 const char *s = !NIL_P(mesg) ? RSTRING_PTR(mesg) : "";
3633 rb_bug("rb_sys_fail_str(%s) - errno == 0", s);
3635 return rb_syserr_new_str(n, mesg);
3638 VALUE
3639 rb_syserr_new(int n, const char *mesg)
3641 VALUE arg;
3642 arg = mesg ? rb_str_new2(mesg) : Qnil;
3643 return rb_syserr_new_str(n, arg);
3646 VALUE
3647 rb_syserr_new_str(int n, VALUE arg)
3649 return rb_class_new_instance(1, &arg, get_syserr(n));
3652 void
3653 rb_syserr_fail(int e, const char *mesg)
3655 rb_exc_raise(rb_syserr_new(e, mesg));
3658 void
3659 rb_syserr_fail_str(int e, VALUE mesg)
3661 rb_exc_raise(rb_syserr_new_str(e, mesg));
3664 #undef rb_sys_fail
3665 void
3666 rb_sys_fail(const char *mesg)
3668 rb_exc_raise(make_errno_exc(mesg));
3671 #undef rb_sys_fail_str
3672 void
3673 rb_sys_fail_str(VALUE mesg)
3675 rb_exc_raise(make_errno_exc_str(mesg));
3678 #ifdef RUBY_FUNCTION_NAME_STRING
3679 void
3680 rb_sys_fail_path_in(const char *func_name, VALUE path)
3682 int n = errno;
3684 errno = 0;
3685 rb_syserr_fail_path_in(func_name, n, path);
3688 void
3689 rb_syserr_fail_path_in(const char *func_name, int n, VALUE path)
3691 rb_exc_raise(rb_syserr_new_path_in(func_name, n, path));
3694 VALUE
3695 rb_syserr_new_path_in(const char *func_name, int n, VALUE path)
3697 VALUE args[2];
3699 if (!path) path = Qnil;
3700 if (n == 0) {
3701 const char *s = !NIL_P(path) ? RSTRING_PTR(path) : "";
3702 if (!func_name) func_name = "(null)";
3703 rb_bug("rb_sys_fail_path_in(%s, %s) - errno == 0",
3704 func_name, s);
3706 args[0] = path;
3707 args[1] = rb_str_new_cstr(func_name);
3708 return rb_class_new_instance(2, args, get_syserr(n));
3710 #endif
3712 NORETURN(static void rb_mod_exc_raise(VALUE exc, VALUE mod));
3714 static void
3715 rb_mod_exc_raise(VALUE exc, VALUE mod)
3717 rb_extend_object(exc, mod);
3718 rb_exc_raise(exc);
3721 void
3722 rb_mod_sys_fail(VALUE mod, const char *mesg)
3724 VALUE exc = make_errno_exc(mesg);
3725 rb_mod_exc_raise(exc, mod);
3728 void
3729 rb_mod_sys_fail_str(VALUE mod, VALUE mesg)
3731 VALUE exc = make_errno_exc_str(mesg);
3732 rb_mod_exc_raise(exc, mod);
3735 void
3736 rb_mod_syserr_fail(VALUE mod, int e, const char *mesg)
3738 VALUE exc = rb_syserr_new(e, mesg);
3739 rb_mod_exc_raise(exc, mod);
3742 void
3743 rb_mod_syserr_fail_str(VALUE mod, int e, VALUE mesg)
3745 VALUE exc = rb_syserr_new_str(e, mesg);
3746 rb_mod_exc_raise(exc, mod);
3749 static void
3750 syserr_warning(VALUE mesg, int err)
3752 rb_str_set_len(mesg, RSTRING_LEN(mesg)-1);
3753 rb_str_catf(mesg, ": %s\n", strerror(err));
3754 rb_write_warning_str(mesg);
3757 #if 0
3758 void
3759 rb_sys_warn(const char *fmt, ...)
3761 if (!NIL_P(ruby_verbose)) {
3762 int errno_save = errno;
3763 with_warning_string(mesg, 0, fmt) {
3764 syserr_warning(mesg, errno_save);
3766 errno = errno_save;
3770 void
3771 rb_syserr_warn(int err, const char *fmt, ...)
3773 if (!NIL_P(ruby_verbose)) {
3774 with_warning_string(mesg, 0, fmt) {
3775 syserr_warning(mesg, err);
3780 void
3781 rb_sys_enc_warn(rb_encoding *enc, const char *fmt, ...)
3783 if (!NIL_P(ruby_verbose)) {
3784 int errno_save = errno;
3785 with_warning_string(mesg, enc, fmt) {
3786 syserr_warning(mesg, errno_save);
3788 errno = errno_save;
3792 void
3793 rb_syserr_enc_warn(int err, rb_encoding *enc, const char *fmt, ...)
3795 if (!NIL_P(ruby_verbose)) {
3796 with_warning_string(mesg, enc, fmt) {
3797 syserr_warning(mesg, err);
3801 #endif
3803 void
3804 rb_sys_warning(const char *fmt, ...)
3806 if (RTEST(ruby_verbose)) {
3807 int errno_save = errno;
3808 with_warning_string(mesg, 0, fmt) {
3809 syserr_warning(mesg, errno_save);
3811 errno = errno_save;
3815 #if 0
3816 void
3817 rb_syserr_warning(int err, const char *fmt, ...)
3819 if (RTEST(ruby_verbose)) {
3820 with_warning_string(mesg, 0, fmt) {
3821 syserr_warning(mesg, err);
3825 #endif
3827 void
3828 rb_sys_enc_warning(rb_encoding *enc, const char *fmt, ...)
3830 if (RTEST(ruby_verbose)) {
3831 int errno_save = errno;
3832 with_warning_string(mesg, enc, fmt) {
3833 syserr_warning(mesg, errno_save);
3835 errno = errno_save;
3839 void
3840 rb_syserr_enc_warning(int err, rb_encoding *enc, const char *fmt, ...)
3842 if (RTEST(ruby_verbose)) {
3843 with_warning_string(mesg, enc, fmt) {
3844 syserr_warning(mesg, err);
3849 void
3850 rb_load_fail(VALUE path, const char *err)
3852 VALUE mesg = rb_str_buf_new_cstr(err);
3853 rb_str_cat2(mesg, " -- ");
3854 rb_str_append(mesg, path); /* should be ASCII compatible */
3855 raise_loaderror(path, mesg);
3858 void
3859 rb_error_frozen(const char *what)
3861 rb_raise(rb_eFrozenError, "can't modify frozen %s", what);
3864 void
3865 rb_frozen_error_raise(VALUE frozen_obj, const char *fmt, ...)
3867 va_list args;
3868 VALUE exc, mesg;
3870 va_start(args, fmt);
3871 mesg = rb_vsprintf(fmt, args);
3872 va_end(args);
3873 exc = rb_exc_new3(rb_eFrozenError, mesg);
3874 rb_ivar_set(exc, id_recv, frozen_obj);
3875 rb_exc_raise(exc);
3878 static VALUE
3879 inspect_frozen_obj(VALUE obj, VALUE mesg, int recur)
3881 if (recur) {
3882 rb_str_cat_cstr(mesg, " ...");
3884 else {
3885 rb_str_append(mesg, rb_inspect(obj));
3887 return mesg;
3890 void
3891 rb_error_frozen_object(VALUE frozen_obj)
3893 rb_yjit_lazy_push_frame(GET_EC()->cfp->pc);
3895 VALUE debug_info;
3896 const ID created_info = id_debug_created_info;
3897 VALUE mesg = rb_sprintf("can't modify frozen %"PRIsVALUE": ",
3898 CLASS_OF(frozen_obj));
3899 VALUE exc = rb_exc_new_str(rb_eFrozenError, mesg);
3901 rb_ivar_set(exc, id_recv, frozen_obj);
3902 rb_exec_recursive(inspect_frozen_obj, frozen_obj, mesg);
3904 if (!NIL_P(debug_info = rb_attr_get(frozen_obj, created_info))) {
3905 VALUE path = rb_ary_entry(debug_info, 0);
3906 VALUE line = rb_ary_entry(debug_info, 1);
3908 rb_str_catf(mesg, ", created at %"PRIsVALUE":%"PRIsVALUE, path, line);
3910 rb_exc_raise(exc);
3913 #undef rb_check_frozen
3914 void
3915 rb_check_frozen(VALUE obj)
3917 rb_check_frozen_internal(obj);
3920 void
3921 rb_check_copyable(VALUE obj, VALUE orig)
3923 if (!FL_ABLE(obj)) return;
3924 rb_check_frozen_internal(obj);
3925 if (!FL_ABLE(orig)) return;
3928 void
3929 Init_syserr(void)
3931 rb_eNOERROR = setup_syserr(0, "NOERROR");
3932 #if 0
3933 /* No error */
3934 rb_define_const(rb_mErrno, "NOERROR", rb_eNOERROR);
3935 #endif
3936 #define defined_error(name, num) set_syserr((num), (name));
3937 #define undefined_error(name) rb_define_const(rb_mErrno, (name), rb_eNOERROR);
3938 #include "known_errors.inc"
3939 #undef defined_error
3940 #undef undefined_error
3943 #include "warning.rbinc"
3946 * \}