[ruby/irb] Change debug test workaround only enabled when output is
[ruby.git] / error.c
blobdc032df6512cefa33c221ea0360285b430aa442b
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(file, line, fmt) \
390 VALUE str; \
391 va_list args; \
392 va_start(args, fmt); \
393 str = warn_vsprintf(NULL, 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(file, line, fmt) {
401 rb_write_warning_str(str);
406 /* rb_compile_warning() reports only in verbose mode */
407 void
408 rb_compile_warning(const char *file, int line, const char *fmt, ...)
410 if (RTEST(ruby_verbose)) {
411 with_warn_vsprintf(file, line, fmt) {
412 rb_write_warning_str(str);
417 void
418 rb_category_compile_warn(rb_warning_category_t category, const char *file, int line, const char *fmt, ...)
420 if (!NIL_P(ruby_verbose)) {
421 with_warn_vsprintf(file, line, fmt) {
422 rb_warn_category(str, rb_warning_category_to_name(category));
427 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 0)
428 static VALUE
429 warning_string(rb_encoding *enc, const char *fmt, va_list args)
431 int line;
432 const char *file = rb_source_location_cstr(&line);
433 return warn_vsprintf(enc, file, line, fmt, args);
436 #define with_warning_string(mesg, enc, fmt) \
437 with_warning_string_from(mesg, enc, fmt, fmt)
438 #define with_warning_string_from(mesg, enc, fmt, last_arg) \
439 VALUE mesg; \
440 va_list args; va_start(args, last_arg); \
441 mesg = warning_string(enc, fmt, args); \
442 va_end(args);
444 void
445 rb_warn(const char *fmt, ...)
447 if (!NIL_P(ruby_verbose)) {
448 with_warning_string(mesg, 0, fmt) {
449 rb_write_warning_str(mesg);
454 void
455 rb_category_warn(rb_warning_category_t category, const char *fmt, ...)
457 if (!NIL_P(ruby_verbose)) {
458 with_warning_string(mesg, 0, fmt) {
459 rb_warn_category(mesg, rb_warning_category_to_name(category));
464 void
465 rb_enc_warn(rb_encoding *enc, const char *fmt, ...)
467 if (!NIL_P(ruby_verbose)) {
468 with_warning_string(mesg, enc, fmt) {
469 rb_write_warning_str(mesg);
474 /* rb_warning() reports only in verbose mode */
475 void
476 rb_warning(const char *fmt, ...)
478 if (RTEST(ruby_verbose)) {
479 with_warning_string(mesg, 0, fmt) {
480 rb_write_warning_str(mesg);
485 /* rb_category_warning() reports only in verbose mode */
486 void
487 rb_category_warning(rb_warning_category_t category, const char *fmt, ...)
489 if (RTEST(ruby_verbose)) {
490 with_warning_string(mesg, 0, fmt) {
491 rb_warn_category(mesg, rb_warning_category_to_name(category));
496 VALUE
497 rb_warning_string(const char *fmt, ...)
499 with_warning_string(mesg, 0, fmt) {
501 return mesg;
504 #if 0
505 void
506 rb_enc_warning(rb_encoding *enc, const char *fmt, ...)
508 if (RTEST(ruby_verbose)) {
509 with_warning_string(mesg, enc, fmt) {
510 rb_write_warning_str(mesg);
514 #endif
516 static bool
517 deprecation_warning_enabled(void)
519 if (NIL_P(ruby_verbose)) return false;
520 if (!rb_warning_category_enabled_p(RB_WARN_CATEGORY_DEPRECATED)) return false;
521 return true;
524 static void
525 warn_deprecated(VALUE mesg, const char *removal, const char *suggest)
527 rb_str_set_len(mesg, RSTRING_LEN(mesg) - 1);
528 rb_str_cat_cstr(mesg, " is deprecated");
529 if (removal) {
530 rb_str_catf(mesg, " and will be removed in Ruby %s", removal);
532 if (suggest) rb_str_catf(mesg, "; use %s instead", suggest);
533 rb_str_cat_cstr(mesg, "\n");
534 rb_warn_category(mesg, ID2SYM(id_deprecated));
537 void
538 rb_warn_deprecated(const char *fmt, const char *suggest, ...)
540 if (!deprecation_warning_enabled()) return;
542 with_warning_string_from(mesg, 0, fmt, suggest) {
543 warn_deprecated(mesg, NULL, suggest);
547 void
548 rb_warn_deprecated_to_remove(const char *removal, const char *fmt, const char *suggest, ...)
550 if (!deprecation_warning_enabled()) return;
552 with_warning_string_from(mesg, 0, fmt, suggest) {
553 warn_deprecated(mesg, removal, suggest);
557 static inline int
558 end_with_asciichar(VALUE str, int c)
560 return RB_TYPE_P(str, T_STRING) &&
561 rb_str_end_with_asciichar(str, c);
564 /* :nodoc: */
565 static VALUE
566 warning_write(int argc, VALUE *argv, VALUE buf)
568 while (argc-- > 0) {
569 rb_str_append(buf, *argv++);
571 return buf;
574 VALUE rb_ec_backtrace_location_ary(const rb_execution_context_t *ec, long lev, long n, bool skip_internal);
576 static VALUE
577 rb_warn_m(rb_execution_context_t *ec, VALUE exc, VALUE msgs, VALUE uplevel, VALUE category)
579 VALUE location = Qnil;
580 int argc = RARRAY_LENINT(msgs);
581 const VALUE *argv = RARRAY_CONST_PTR(msgs);
583 if (!NIL_P(ruby_verbose) && argc > 0) {
584 VALUE str = argv[0];
585 if (!NIL_P(uplevel)) {
586 long lev = NUM2LONG(uplevel);
587 if (lev < 0) {
588 rb_raise(rb_eArgError, "negative level (%ld)", lev);
590 location = rb_ec_backtrace_location_ary(ec, lev + 1, 1, TRUE);
591 if (!NIL_P(location)) {
592 location = rb_ary_entry(location, 0);
595 if (argc > 1 || !NIL_P(uplevel) || !end_with_asciichar(str, '\n')) {
596 VALUE path;
597 if (NIL_P(uplevel)) {
598 str = rb_str_tmp_new(0);
600 else if (NIL_P(location) ||
601 NIL_P(path = rb_funcall(location, rb_intern("path"), 0))) {
602 str = rb_str_new_cstr("warning: ");
604 else {
605 str = rb_sprintf("%s:%ld: warning: ",
606 rb_string_value_ptr(&path),
607 NUM2LONG(rb_funcall(location, rb_intern("lineno"), 0)));
609 RBASIC_SET_CLASS(str, rb_cWarningBuffer);
610 rb_io_puts(argc, argv, str);
611 RBASIC_SET_CLASS(str, rb_cString);
614 if (!NIL_P(category)) {
615 category = rb_to_symbol_type(category);
616 rb_warning_category_from_name(category);
619 if (exc == rb_mWarning) {
620 rb_must_asciicompat(str);
621 rb_write_error_str(str);
623 else {
624 rb_warn_category(str, category);
627 return Qnil;
630 #define MAX_BUG_REPORTERS 0x100
632 static struct bug_reporters {
633 void (*func)(FILE *out, void *data);
634 void *data;
635 } bug_reporters[MAX_BUG_REPORTERS];
637 static int bug_reporters_size;
640 rb_bug_reporter_add(void (*func)(FILE *, void *), void *data)
642 struct bug_reporters *reporter;
643 if (bug_reporters_size >= MAX_BUG_REPORTERS) {
644 return 0; /* failed to register */
646 reporter = &bug_reporters[bug_reporters_size++];
647 reporter->func = func;
648 reporter->data = data;
650 return 1;
653 /* returns true if x can not be used as file name */
654 static bool
655 path_sep_p(char x)
657 #if defined __CYGWIN__ || defined DOSISH
658 # define PATH_SEP_ENCODING 1
659 // Assume that "/" is only the first byte in any encoding.
660 if (x == ':') return true; // drive letter or ADS
661 if (x == '\\') return true;
662 #endif
663 return x == '/';
666 struct path_string {
667 const char *ptr;
668 size_t len;
671 static const char PATHSEP_REPLACE = '!';
673 static char *
674 append_pathname(char *p, const char *pe, VALUE str)
676 #ifdef PATH_SEP_ENCODING
677 rb_encoding *enc = rb_enc_get(str);
678 #endif
679 const char *s = RSTRING_PTR(str);
680 const char *const se = s + RSTRING_LEN(str);
681 char c;
683 --pe; // for terminator
685 while (p < pe && s < se && (c = *s) != '\0') {
686 if (c == '.') {
687 if (s == se || !*s) break; // chomp "." basename
688 if (path_sep_p(s[1])) goto skipsep; // skip "./"
690 else if (path_sep_p(c)) {
691 // squeeze successive separators
692 *p++ = PATHSEP_REPLACE;
693 skipsep:
694 while (++s < se && path_sep_p(*s));
695 continue;
697 const char *const ss = s;
698 while (p < pe && s < se && *s && !path_sep_p(*s)) {
699 #ifdef PATH_SEP_ENCODING
700 int n = rb_enc_mbclen(s, se, enc);
701 #else
702 const int n = 1;
703 #endif
704 p += n;
705 s += n;
707 if (s > ss) memcpy(p - (s - ss), ss, s - ss);
710 return p;
713 static char *
714 append_basename(char *p, const char *pe, struct path_string *path, VALUE str)
716 if (!path->ptr) {
717 #ifdef PATH_SEP_ENCODING
718 rb_encoding *enc = rb_enc_get(str);
719 #endif
720 const char *const b = RSTRING_PTR(str), *const e = RSTRING_END(str), *p = e;
722 while (p > b) {
723 if (path_sep_p(p[-1])) {
724 #ifdef PATH_SEP_ENCODING
725 const char *t = rb_enc_prev_char(b, p, e, enc);
726 if (t == p-1) break;
727 p = t;
728 #else
729 break;
730 #endif
732 else {
733 --p;
737 path->ptr = p;
738 path->len = e - p;
740 size_t n = path->len;
741 if (p + n > pe) n = pe - p;
742 memcpy(p, path->ptr, n);
743 return p + n;
746 static void
747 finish_report(FILE *out, rb_pid_t pid)
749 if (out != stdout && out != stderr) fclose(out);
750 #ifdef HAVE_WORKING_FORK
751 if (pid > 0) waitpid(pid, NULL, 0);
752 #endif
755 struct report_expansion {
756 struct path_string exe, script;
757 rb_pid_t pid;
758 time_t time;
762 * Open a bug report file to write. The `RUBY_CRASH_REPORT`
763 * environment variable can be set to define a template that is used
764 * to name bug report files. The template can contain % specifiers
765 * which are substituted by the following values when a bug report
766 * file is created:
768 * %% A single % character.
769 * %e The base name of the executable filename.
770 * %E Pathname of executable, with slashes ('/') replaced by
771 * exclamation marks ('!').
772 * %f Similar to %e with the main script filename.
773 * %F Similar to %E with the main script filename.
774 * %p PID of dumped process in decimal.
775 * %t Time of dump, expressed as seconds since the Epoch,
776 * 1970-01-01 00:00:00 +0000 (UTC).
777 * %NNN Octal char code, upto 3 digits.
779 static char *
780 expand_report_argument(const char **input_template, struct report_expansion *values,
781 char *buf, size_t size, bool word)
783 char *p = buf;
784 char *end = buf + size;
785 const char *template = *input_template;
786 bool store = true;
788 if (p >= end-1 || !*template) return NULL;
789 do {
790 char c = *template++;
791 if (word && ISSPACE(c)) break;
792 if (!store) continue;
793 if (c == '%') {
794 size_t n;
795 switch (c = *template++) {
796 case 'e':
797 p = append_basename(p, end, &values->exe, rb_argv0);
798 continue;
799 case 'E':
800 p = append_pathname(p, end, rb_argv0);
801 continue;
802 case 'f':
803 p = append_basename(p, end, &values->script, GET_VM()->orig_progname);
804 continue;
805 case 'F':
806 p = append_pathname(p, end, GET_VM()->orig_progname);
807 continue;
808 case 'p':
809 if (!values->pid) values->pid = getpid();
810 snprintf(p, end-p, "%" PRI_PIDT_PREFIX "d", values->pid);
811 p += strlen(p);
812 continue;
813 case 't':
814 if (!values->time) values->time = time(NULL);
815 snprintf(p, end-p, "%" PRI_TIMET_PREFIX "d", values->time);
816 p += strlen(p);
817 continue;
818 default:
819 if (c >= '0' && c <= '7') {
820 c = (unsigned char)ruby_scan_oct(template-1, 3, &n);
821 template += n - 1;
822 if (!c) store = false;
824 break;
827 if (p < end-1) *p++ = c;
828 } while (*template);
829 *input_template = template;
830 *p = '\0';
831 return ++p;
834 FILE *ruby_popen_writer(char *const *argv, rb_pid_t *pid);
836 static FILE *
837 open_report_path(const char *template, char *buf, size_t size, rb_pid_t *pid)
839 struct report_expansion values = {{0}};
841 if (!template) return NULL;
842 if (0) fprintf(stderr, "RUBY_CRASH_REPORT=%s\n", buf);
843 if (*template == '|') {
844 char *argv[16], *bufend = buf + size, *p;
845 int argc;
846 template++;
847 for (argc = 0; argc < numberof(argv) - 1; ++argc) {
848 while (*template && ISSPACE(*template)) template++;
849 p = expand_report_argument(&template, &values, buf, bufend-buf, true);
850 if (!p) break;
851 argv[argc] = buf;
852 buf = p;
854 argv[argc] = NULL;
855 if (!p) return ruby_popen_writer(argv, pid);
857 else if (*template) {
858 expand_report_argument(&template, &values, buf, size, false);
859 return fopen(buf, "w");
861 return NULL;
864 static const char *crash_report;
866 /* SIGSEGV handler might have a very small stack. Thus we need to use it carefully. */
867 #define REPORT_BUG_BUFSIZ 256
868 static FILE *
869 bug_report_file(const char *file, int line, rb_pid_t *pid)
871 char buf[REPORT_BUG_BUFSIZ];
872 const char *report = crash_report;
873 if (!report) report = getenv("RUBY_CRASH_REPORT");
874 FILE *out = open_report_path(report, buf, sizeof(buf), pid);
875 int len = err_position_0(buf, sizeof(buf), file, line);
877 if (out) {
878 if ((ssize_t)fwrite(buf, 1, len, out) == (ssize_t)len) return out;
879 fclose(out);
881 if ((ssize_t)fwrite(buf, 1, len, stderr) == (ssize_t)len) {
882 return stderr;
884 if ((ssize_t)fwrite(buf, 1, len, stdout) == (ssize_t)len) {
885 return stdout;
888 return NULL;
891 FUNC_MINIMIZED(static void bug_important_message(FILE *out, const char *const msg, size_t len));
893 static void
894 bug_important_message(FILE *out, const char *const msg, size_t len)
896 const char *const endmsg = msg + len;
897 const char *p = msg;
899 if (!len) return;
900 if (isatty(fileno(out))) {
901 static const char red[] = "\033[;31;1;7m";
902 static const char green[] = "\033[;32;7m";
903 static const char reset[] = "\033[m";
904 const char *e = strchr(p, '\n');
905 const int w = (int)(e - p);
906 do {
907 int i = (int)(e - p);
908 fputs(*p == ' ' ? green : red, out);
909 fwrite(p, 1, e - p, out);
910 for (; i < w; ++i) fputc(' ', out);
911 fputs(reset, out);
912 fputc('\n', out);
913 } while ((p = e + 1) < endmsg && (e = strchr(p, '\n')) != 0 && e > p + 1);
915 fwrite(p, 1, endmsg - p, out);
918 #undef CRASH_REPORTER_MAY_BE_CREATED
919 #if defined(__APPLE__) && \
920 (!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 */
921 # define CRASH_REPORTER_MAY_BE_CREATED
922 #endif
923 static void
924 preface_dump(FILE *out)
926 #if defined __APPLE__
927 static const char msg[] = ""
928 "-- Crash Report log information "
929 "--------------------------------------------\n"
930 " See Crash Report log file in one of the following locations:\n"
931 # ifdef CRASH_REPORTER_MAY_BE_CREATED
932 " * ~/Library/Logs/CrashReporter\n"
933 " * /Library/Logs/CrashReporter\n"
934 # endif
935 " * ~/Library/Logs/DiagnosticReports\n"
936 " * /Library/Logs/DiagnosticReports\n"
937 " for more details.\n"
938 "Don't forget to include the above Crash Report log file in bug reports.\n"
939 "\n";
940 const size_t msglen = sizeof(msg) - 1;
941 #else
942 const char *msg = NULL;
943 const size_t msglen = 0;
944 #endif
945 bug_important_message(out, msg, msglen);
948 static void
949 postscript_dump(FILE *out)
951 #if defined __APPLE__
952 static const char msg[] = ""
953 "[IMPORTANT]"
954 /*" ------------------------------------------------"*/
955 "\n""Don't forget to include the Crash Report log file under\n"
956 # ifdef CRASH_REPORTER_MAY_BE_CREATED
957 "CrashReporter or "
958 # endif
959 "DiagnosticReports directory in bug reports.\n"
960 /*"------------------------------------------------------------\n"*/
961 "\n";
962 const size_t msglen = sizeof(msg) - 1;
963 #else
964 const char *msg = NULL;
965 const size_t msglen = 0;
966 #endif
967 bug_important_message(out, msg, msglen);
970 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 0)
971 static void
972 bug_report_begin_valist(FILE *out, const char *fmt, va_list args)
974 char buf[REPORT_BUG_BUFSIZ];
976 fputs("[BUG] ", out);
977 vsnprintf(buf, sizeof(buf), fmt, args);
978 fputs(buf, out);
979 snprintf(buf, sizeof(buf), "\n%s\n\n", rb_dynamic_description);
980 fputs(buf, out);
981 preface_dump(out);
984 #define bug_report_begin(out, fmt) do { \
985 va_list args; \
986 va_start(args, fmt); \
987 bug_report_begin_valist(out, fmt, args); \
988 va_end(args); \
989 } while (0)
991 static void
992 bug_report_end(FILE *out, rb_pid_t pid)
994 /* call additional bug reporters */
996 int i;
997 for (i=0; i<bug_reporters_size; i++) {
998 struct bug_reporters *reporter = &bug_reporters[i];
999 (*reporter->func)(out, reporter->data);
1002 postscript_dump(out);
1003 finish_report(out, pid);
1006 #define report_bug(file, line, fmt, ctx) do { \
1007 rb_pid_t pid = -1; \
1008 FILE *out = bug_report_file(file, line, &pid); \
1009 if (out) { \
1010 bug_report_begin(out, fmt); \
1011 rb_vm_bugreport(ctx, out); \
1012 bug_report_end(out, pid); \
1014 } while (0) \
1016 #define report_bug_valist(file, line, fmt, ctx, args) do { \
1017 rb_pid_t pid = -1; \
1018 FILE *out = bug_report_file(file, line, &pid); \
1019 if (out) { \
1020 bug_report_begin_valist(out, fmt, args); \
1021 rb_vm_bugreport(ctx, out); \
1022 bug_report_end(out, pid); \
1024 } while (0) \
1026 void
1027 ruby_set_crash_report(const char *template)
1029 crash_report = template;
1030 #if RUBY_DEBUG
1031 rb_pid_t pid = -1;
1032 char buf[REPORT_BUG_BUFSIZ];
1033 FILE *out = open_report_path(template, buf, sizeof(buf), &pid);
1034 if (out) {
1035 time_t t = time(NULL);
1036 fprintf(out, "ruby_test_bug_report: %s", ctime(&t));
1037 finish_report(out, pid);
1039 #endif
1042 NORETURN(static void die(void));
1043 static void
1044 die(void)
1046 #if defined(_WIN32) && defined(RUBY_MSVCRT_VERSION) && RUBY_MSVCRT_VERSION >= 80
1047 _set_abort_behavior( 0, _CALL_REPORTFAULT);
1048 #endif
1050 abort();
1053 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 1, 0)
1054 void
1055 rb_bug_without_die(const char *fmt, va_list args)
1057 const char *file = NULL;
1058 int line = 0;
1060 if (GET_EC()) {
1061 file = rb_source_location_cstr(&line);
1064 report_bug_valist(file, line, fmt, NULL, args);
1067 void
1068 rb_bug(const char *fmt, ...)
1070 va_list args;
1071 va_start(args, fmt);
1072 rb_bug_without_die(fmt, args);
1073 va_end(args);
1074 die();
1077 void
1078 rb_bug_for_fatal_signal(ruby_sighandler_t default_sighandler, int sig, const void *ctx, const char *fmt, ...)
1080 const char *file = NULL;
1081 int line = 0;
1083 if (GET_EC()) {
1084 file = rb_source_location_cstr(&line);
1087 report_bug(file, line, fmt, ctx);
1089 if (default_sighandler) default_sighandler(sig);
1091 ruby_default_signal(sig);
1092 die();
1096 void
1097 rb_bug_errno(const char *mesg, int errno_arg)
1099 if (errno_arg == 0)
1100 rb_bug("%s: errno == 0 (NOERROR)", mesg);
1101 else {
1102 const char *errno_str = rb_strerrno(errno_arg);
1103 if (errno_str)
1104 rb_bug("%s: %s (%s)", mesg, strerror(errno_arg), errno_str);
1105 else
1106 rb_bug("%s: %s (%d)", mesg, strerror(errno_arg), errno_arg);
1111 * this is safe to call inside signal handler and timer thread
1112 * (which isn't a Ruby Thread object)
1114 #define write_or_abort(fd, str, len) (write((fd), (str), (len)) < 0 ? abort() : (void)0)
1115 #define WRITE_CONST(fd,str) write_or_abort((fd),(str),sizeof(str) - 1)
1117 void
1118 rb_async_bug_errno(const char *mesg, int errno_arg)
1120 WRITE_CONST(2, "[ASYNC BUG] ");
1121 write_or_abort(2, mesg, strlen(mesg));
1122 WRITE_CONST(2, "\n");
1124 if (errno_arg == 0) {
1125 WRITE_CONST(2, "errno == 0 (NOERROR)\n");
1127 else {
1128 const char *errno_str = rb_strerrno(errno_arg);
1130 if (!errno_str)
1131 errno_str = "undefined errno";
1132 write_or_abort(2, errno_str, strlen(errno_str));
1134 WRITE_CONST(2, "\n\n");
1135 write_or_abort(2, rb_dynamic_description, strlen(rb_dynamic_description));
1136 abort();
1139 void
1140 rb_report_bug_valist(VALUE file, int line, const char *fmt, va_list args)
1142 report_bug_valist(RSTRING_PTR(file), line, fmt, NULL, args);
1145 void
1146 rb_assert_failure(const char *file, int line, const char *name, const char *expr)
1148 rb_assert_failure_detail(file, line, name, expr, NULL);
1151 void
1152 rb_assert_failure_detail(const char *file, int line, const char *name, const char *expr,
1153 const char *fmt, ...)
1155 FILE *out = stderr;
1156 fprintf(out, "Assertion Failed: %s:%d:", file, line);
1157 if (name) fprintf(out, "%s:", name);
1158 fprintf(out, "%s\n%s\n\n", expr, rb_dynamic_description);
1160 if (fmt && *fmt) {
1161 va_list args;
1162 va_start(args, fmt);
1163 vfprintf(out, fmt, args);
1164 va_end(args);
1167 preface_dump(out);
1168 rb_vm_bugreport(NULL, out);
1169 bug_report_end(out, -1);
1170 die();
1173 static const char builtin_types[][10] = {
1174 "", /* 0x00, */
1175 "Object",
1176 "Class",
1177 "Module",
1178 "Float",
1179 "String",
1180 "Regexp",
1181 "Array",
1182 "Hash",
1183 "Struct",
1184 "Integer",
1185 "File",
1186 "Data", /* internal use: wrapped C pointers */
1187 "MatchData", /* data of $~ */
1188 "Complex",
1189 "Rational",
1190 "", /* 0x10 */
1191 "nil",
1192 "true",
1193 "false",
1194 "Symbol", /* :symbol */
1195 "Integer",
1196 "undef", /* internal use: #undef; should not happen */
1197 "", /* 0x17 */
1198 "", /* 0x18 */
1199 "", /* 0x19 */
1200 "<Memo>", /* internal use: general memo */
1201 "<Node>", /* internal use: syntax tree node */
1202 "<iClass>", /* internal use: mixed-in module holder */
1205 const char *
1206 rb_builtin_type_name(int t)
1208 const char *name;
1209 if ((unsigned int)t >= numberof(builtin_types)) return 0;
1210 name = builtin_types[t];
1211 if (*name) return name;
1212 return 0;
1215 static VALUE
1216 displaying_class_of(VALUE x)
1218 switch (x) {
1219 case Qfalse: return rb_fstring_cstr("false");
1220 case Qnil: return rb_fstring_cstr("nil");
1221 case Qtrue: return rb_fstring_cstr("true");
1222 default: return rb_obj_class(x);
1226 static const char *
1227 builtin_class_name(VALUE x)
1229 const char *etype;
1231 if (NIL_P(x)) {
1232 etype = "nil";
1234 else if (FIXNUM_P(x)) {
1235 etype = "Integer";
1237 else if (SYMBOL_P(x)) {
1238 etype = "Symbol";
1240 else if (RB_TYPE_P(x, T_TRUE)) {
1241 etype = "true";
1243 else if (RB_TYPE_P(x, T_FALSE)) {
1244 etype = "false";
1246 else {
1247 etype = NULL;
1249 return etype;
1252 const char *
1253 rb_builtin_class_name(VALUE x)
1255 const char *etype = builtin_class_name(x);
1257 if (!etype) {
1258 etype = rb_obj_classname(x);
1260 return etype;
1263 COLDFUNC NORETURN(static void unexpected_type(VALUE, int, int));
1264 #define UNDEF_LEAKED "undef leaked to the Ruby space"
1266 static void
1267 unexpected_type(VALUE x, int xt, int t)
1269 const char *tname = rb_builtin_type_name(t);
1270 VALUE mesg, exc = rb_eFatal;
1272 if (tname) {
1273 mesg = rb_sprintf("wrong argument type %"PRIsVALUE" (expected %s)",
1274 displaying_class_of(x), tname);
1275 exc = rb_eTypeError;
1277 else if (xt > T_MASK && xt <= 0x3f) {
1278 mesg = rb_sprintf("unknown type 0x%x (0x%x given, probably comes"
1279 " from extension library for ruby 1.8)", t, xt);
1281 else {
1282 mesg = rb_sprintf("unknown type 0x%x (0x%x given)", t, xt);
1284 rb_exc_raise(rb_exc_new_str(exc, mesg));
1287 void
1288 rb_check_type(VALUE x, int t)
1290 int xt;
1292 if (RB_UNLIKELY(UNDEF_P(x))) {
1293 rb_bug(UNDEF_LEAKED);
1296 xt = TYPE(x);
1297 if (xt != t || (xt == T_DATA && rbimpl_rtypeddata_p(x))) {
1299 * Typed data is not simple `T_DATA`, but in a sense an
1300 * extension of `struct RVALUE`, which are incompatible with
1301 * each other except when inherited.
1303 * So it is not enough to just check `T_DATA`, it must be
1304 * identified by its `type` using `Check_TypedStruct` instead.
1306 unexpected_type(x, xt, t);
1310 void
1311 rb_unexpected_type(VALUE x, int t)
1313 if (RB_UNLIKELY(UNDEF_P(x))) {
1314 rb_bug(UNDEF_LEAKED);
1317 unexpected_type(x, TYPE(x), t);
1321 rb_typeddata_inherited_p(const rb_data_type_t *child, const rb_data_type_t *parent)
1323 while (child) {
1324 if (child == parent) return 1;
1325 child = child->parent;
1327 return 0;
1331 rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
1333 if (!RB_TYPE_P(obj, T_DATA) ||
1334 !RTYPEDDATA_P(obj) || !rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) {
1335 return 0;
1337 return 1;
1340 #undef rb_typeddata_is_instance_of
1342 rb_typeddata_is_instance_of(VALUE obj, const rb_data_type_t *data_type)
1344 return rb_typeddata_is_instance_of_inline(obj, data_type);
1347 void *
1348 rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
1350 VALUE actual;
1352 if (!RB_TYPE_P(obj, T_DATA)) {
1353 actual = displaying_class_of(obj);
1355 else if (!RTYPEDDATA_P(obj)) {
1356 actual = displaying_class_of(obj);
1358 else if (!rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) {
1359 const char *name = RTYPEDDATA_TYPE(obj)->wrap_struct_name;
1360 actual = rb_str_new_cstr(name); /* or rb_fstring_cstr? not sure... */
1362 else {
1363 return RTYPEDDATA_GET_DATA(obj);
1366 const char *expected = data_type->wrap_struct_name;
1367 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected %s)",
1368 actual, expected);
1369 UNREACHABLE_RETURN(NULL);
1372 /* exception classes */
1373 VALUE rb_eException;
1374 VALUE rb_eSystemExit;
1375 VALUE rb_eInterrupt;
1376 VALUE rb_eSignal;
1377 VALUE rb_eFatal;
1378 VALUE rb_eStandardError;
1379 VALUE rb_eRuntimeError;
1380 VALUE rb_eFrozenError;
1381 VALUE rb_eTypeError;
1382 VALUE rb_eArgError;
1383 VALUE rb_eIndexError;
1384 VALUE rb_eKeyError;
1385 VALUE rb_eRangeError;
1386 VALUE rb_eNameError;
1387 VALUE rb_eEncodingError;
1388 VALUE rb_eEncCompatError;
1389 VALUE rb_eNoMethodError;
1390 VALUE rb_eSecurityError;
1391 VALUE rb_eNotImpError;
1392 VALUE rb_eNoMemError;
1393 VALUE rb_cNameErrorMesg;
1394 VALUE rb_eNoMatchingPatternError;
1395 VALUE rb_eNoMatchingPatternKeyError;
1397 VALUE rb_eScriptError;
1398 VALUE rb_eSyntaxError;
1399 VALUE rb_eLoadError;
1401 VALUE rb_eSystemCallError;
1402 VALUE rb_mErrno;
1403 static VALUE rb_eNOERROR;
1405 ID ruby_static_id_cause;
1406 #define id_cause ruby_static_id_cause
1407 static ID id_message, id_detailed_message, id_backtrace;
1408 static ID id_key, id_matchee, id_args, id_Errno, id_errno, id_i_path;
1409 static ID id_receiver, id_recv, id_iseq, id_local_variables;
1410 static ID id_private_call_p, id_top, id_bottom;
1411 #define id_bt idBt
1412 #define id_bt_locations idBt_locations
1413 #define id_mesg idMesg
1414 #define id_name idName
1416 #undef rb_exc_new_cstr
1418 VALUE
1419 rb_exc_new(VALUE etype, const char *ptr, long len)
1421 VALUE mesg = rb_str_new(ptr, len);
1422 return rb_class_new_instance(1, &mesg, etype);
1425 VALUE
1426 rb_exc_new_cstr(VALUE etype, const char *s)
1428 return rb_exc_new(etype, s, strlen(s));
1431 VALUE
1432 rb_exc_new_str(VALUE etype, VALUE str)
1434 rb_yjit_lazy_push_frame(GET_EC()->cfp->pc);
1435 StringValue(str);
1436 return rb_class_new_instance(1, &str, etype);
1439 static VALUE
1440 exc_init(VALUE exc, VALUE mesg)
1442 rb_ivar_set(exc, id_mesg, mesg);
1443 rb_ivar_set(exc, id_bt, Qnil);
1445 return exc;
1449 * call-seq:
1450 * Exception.new(msg = nil) -> exception
1451 * Exception.exception(msg = nil) -> exception
1453 * Construct a new Exception object, optionally passing in
1454 * a message.
1457 static VALUE
1458 exc_initialize(int argc, VALUE *argv, VALUE exc)
1460 VALUE arg;
1462 arg = (!rb_check_arity(argc, 0, 1) ? Qnil : argv[0]);
1463 return exc_init(exc, arg);
1467 * Document-method: exception
1469 * call-seq:
1470 * exc.exception([string]) -> an_exception or exc
1472 * With no argument, or if the argument is the same as the receiver,
1473 * return the receiver. Otherwise, create a new
1474 * exception object of the same class as the receiver, but with a
1475 * message equal to <code>string.to_str</code>.
1479 static VALUE
1480 exc_exception(int argc, VALUE *argv, VALUE self)
1482 VALUE exc;
1484 argc = rb_check_arity(argc, 0, 1);
1485 if (argc == 0) return self;
1486 if (argc == 1 && self == argv[0]) return self;
1487 exc = rb_obj_clone(self);
1488 rb_ivar_set(exc, id_mesg, argv[0]);
1489 return exc;
1493 * call-seq:
1494 * exception.to_s -> string
1496 * Returns exception's message (or the name of the exception if
1497 * no message is set).
1500 static VALUE
1501 exc_to_s(VALUE exc)
1503 VALUE mesg = rb_attr_get(exc, idMesg);
1505 if (NIL_P(mesg)) return rb_class_name(CLASS_OF(exc));
1506 return rb_String(mesg);
1509 /* FIXME: Include eval_error.c */
1510 void rb_error_write(VALUE errinfo, VALUE emesg, VALUE errat, VALUE str, VALUE opt, VALUE highlight, VALUE reverse);
1512 VALUE
1513 rb_get_message(VALUE exc)
1515 VALUE e = rb_check_funcall(exc, id_message, 0, 0);
1516 if (UNDEF_P(e)) return Qnil;
1517 if (!RB_TYPE_P(e, T_STRING)) e = rb_check_string_type(e);
1518 return e;
1521 VALUE
1522 rb_get_detailed_message(VALUE exc, VALUE opt)
1524 VALUE e;
1525 if (NIL_P(opt)) {
1526 e = rb_check_funcall(exc, id_detailed_message, 0, 0);
1528 else {
1529 e = rb_check_funcall_kw(exc, id_detailed_message, 1, &opt, 1);
1531 if (UNDEF_P(e)) return Qnil;
1532 if (!RB_TYPE_P(e, T_STRING)) e = rb_check_string_type(e);
1533 return e;
1537 * call-seq:
1538 * Exception.to_tty? -> true or false
1540 * Returns +true+ if exception messages will be sent to a tty.
1542 static VALUE
1543 exc_s_to_tty_p(VALUE self)
1545 return RBOOL(rb_stderr_tty_p());
1548 static VALUE
1549 check_highlight_keyword(VALUE opt, int auto_tty_detect)
1551 VALUE highlight = Qnil;
1553 if (!NIL_P(opt)) {
1554 highlight = rb_hash_lookup(opt, sym_highlight);
1556 switch (highlight) {
1557 default:
1558 rb_bool_expected(highlight, "highlight", TRUE);
1559 UNREACHABLE;
1560 case Qtrue: case Qfalse: case Qnil: break;
1564 if (NIL_P(highlight)) {
1565 highlight = RBOOL(auto_tty_detect && rb_stderr_tty_p());
1568 return highlight;
1571 static VALUE
1572 check_order_keyword(VALUE opt)
1574 VALUE order = Qnil;
1576 if (!NIL_P(opt)) {
1577 static VALUE kw_order;
1578 if (!kw_order) kw_order = ID2SYM(rb_intern_const("order"));
1580 order = rb_hash_lookup(opt, kw_order);
1582 if (order != Qnil) {
1583 ID id = rb_check_id(&order);
1584 if (id == id_bottom) order = Qtrue;
1585 else if (id == id_top) order = Qfalse;
1586 else {
1587 rb_raise(rb_eArgError, "expected :top or :bottom as "
1588 "order: %+"PRIsVALUE, order);
1593 if (NIL_P(order)) order = Qfalse;
1595 return order;
1599 * call-seq:
1600 * exception.full_message(highlight: bool, order: [:top or :bottom]) -> string
1602 * Returns formatted string of _exception_.
1603 * The returned string is formatted using the same format that Ruby uses
1604 * when printing an uncaught exceptions to stderr.
1606 * If _highlight_ is +true+ the default error handler will send the
1607 * messages to a tty.
1609 * _order_ must be either of +:top+ or +:bottom+, and places the error
1610 * message and the innermost backtrace come at the top or the bottom.
1612 * The default values of these options depend on <code>$stderr</code>
1613 * and its +tty?+ at the timing of a call.
1616 static VALUE
1617 exc_full_message(int argc, VALUE *argv, VALUE exc)
1619 VALUE opt, str, emesg, errat;
1620 VALUE highlight, order;
1622 rb_scan_args(argc, argv, "0:", &opt);
1624 highlight = check_highlight_keyword(opt, 1);
1625 order = check_order_keyword(opt);
1628 if (NIL_P(opt)) opt = rb_hash_new();
1629 rb_hash_aset(opt, sym_highlight, highlight);
1632 str = rb_str_new2("");
1633 errat = rb_get_backtrace(exc);
1634 emesg = rb_get_detailed_message(exc, opt);
1636 rb_error_write(exc, emesg, errat, str, opt, highlight, order);
1637 return str;
1641 * call-seq:
1642 * exception.message -> string
1644 * Returns the result of invoking <code>exception.to_s</code>.
1645 * Normally this returns the exception's message or name.
1648 static VALUE
1649 exc_message(VALUE exc)
1651 return rb_funcallv(exc, idTo_s, 0, 0);
1655 * call-seq:
1656 * exception.detailed_message(highlight: bool, **opt) -> string
1658 * Processes a string returned by #message.
1660 * It may add the class name of the exception to the end of the first line.
1661 * Also, when +highlight+ keyword is true, it adds ANSI escape sequences to
1662 * make the message bold.
1664 * If you override this method, it must be tolerant for unknown keyword
1665 * arguments. All keyword arguments passed to #full_message are delegated
1666 * to this method.
1668 * This method is overridden by did_you_mean and error_highlight to add
1669 * their information.
1671 * A user-defined exception class can also define their own
1672 * +detailed_message+ method to add supplemental information.
1673 * When +highlight+ is true, it can return a string containing escape
1674 * sequences, but use widely-supported ones. It is recommended to limit
1675 * the following codes:
1677 * - Reset (+\e[0m+)
1678 * - Bold (+\e[1m+)
1679 * - Underline (+\e[4m+)
1680 * - Foreground color except white and black
1681 * - Red (+\e[31m+)
1682 * - Green (+\e[32m+)
1683 * - Yellow (+\e[33m+)
1684 * - Blue (+\e[34m+)
1685 * - Magenta (+\e[35m+)
1686 * - Cyan (+\e[36m+)
1688 * Use escape sequences carefully even if +highlight+ is true.
1689 * Do not use escape sequences to express essential information;
1690 * the message should be readable even if all escape sequences are
1691 * ignored.
1694 static VALUE
1695 exc_detailed_message(int argc, VALUE *argv, VALUE exc)
1697 VALUE opt;
1699 rb_scan_args(argc, argv, "0:", &opt);
1701 VALUE highlight = check_highlight_keyword(opt, 0);
1703 extern VALUE rb_decorate_message(const VALUE eclass, VALUE emesg, int highlight);
1705 return rb_decorate_message(CLASS_OF(exc), rb_get_message(exc), RTEST(highlight));
1709 * call-seq:
1710 * exception.inspect -> string
1712 * Return this exception's class name and message.
1715 static VALUE
1716 exc_inspect(VALUE exc)
1718 VALUE str, klass;
1720 klass = CLASS_OF(exc);
1721 exc = rb_obj_as_string(exc);
1722 if (RSTRING_LEN(exc) == 0) {
1723 return rb_class_name(klass);
1726 str = rb_str_buf_new2("#<");
1727 klass = rb_class_name(klass);
1728 rb_str_buf_append(str, klass);
1730 if (RTEST(rb_str_include(exc, rb_str_new2("\n")))) {
1731 rb_str_catf(str, ":%+"PRIsVALUE, exc);
1733 else {
1734 rb_str_buf_cat(str, ": ", 2);
1735 rb_str_buf_append(str, exc);
1738 rb_str_buf_cat(str, ">", 1);
1740 return str;
1744 * call-seq:
1745 * exception.backtrace -> array or nil
1747 * Returns any backtrace associated with the exception. The backtrace
1748 * is an array of strings, each containing either ``filename:lineNo: in
1749 * `method''' or ``filename:lineNo.''
1751 * def a
1752 * raise "boom"
1753 * end
1755 * def b
1756 * a()
1757 * end
1759 * begin
1760 * b()
1761 * rescue => detail
1762 * print detail.backtrace.join("\n")
1763 * end
1765 * <em>produces:</em>
1767 * prog.rb:2:in `a'
1768 * prog.rb:6:in `b'
1769 * prog.rb:10
1771 * In the case no backtrace has been set, +nil+ is returned
1773 * ex = StandardError.new
1774 * ex.backtrace
1775 * #=> nil
1778 static VALUE
1779 exc_backtrace(VALUE exc)
1781 VALUE obj;
1783 obj = rb_attr_get(exc, id_bt);
1785 if (rb_backtrace_p(obj)) {
1786 obj = rb_backtrace_to_str_ary(obj);
1787 /* rb_ivar_set(exc, id_bt, obj); */
1790 return obj;
1793 static VALUE rb_check_backtrace(VALUE);
1795 VALUE
1796 rb_get_backtrace(VALUE exc)
1798 ID mid = id_backtrace;
1799 VALUE info;
1800 if (rb_method_basic_definition_p(CLASS_OF(exc), id_backtrace)) {
1801 VALUE klass = rb_eException;
1802 rb_execution_context_t *ec = GET_EC();
1803 if (NIL_P(exc))
1804 return Qnil;
1805 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_CALL, exc, mid, mid, klass, Qundef);
1806 info = exc_backtrace(exc);
1807 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, exc, mid, mid, klass, info);
1809 else {
1810 info = rb_funcallv(exc, mid, 0, 0);
1812 if (NIL_P(info)) return Qnil;
1813 return rb_check_backtrace(info);
1817 * call-seq:
1818 * exception.backtrace_locations -> array or nil
1820 * Returns any backtrace associated with the exception. This method is
1821 * similar to Exception#backtrace, but the backtrace is an array of
1822 * Thread::Backtrace::Location.
1824 * This method is not affected by Exception#set_backtrace().
1826 static VALUE
1827 exc_backtrace_locations(VALUE exc)
1829 VALUE obj;
1831 obj = rb_attr_get(exc, id_bt_locations);
1832 if (!NIL_P(obj)) {
1833 obj = rb_backtrace_to_location_ary(obj);
1835 return obj;
1838 static VALUE
1839 rb_check_backtrace(VALUE bt)
1841 long i;
1842 static const char err[] = "backtrace must be an Array of String or an Array of Thread::Backtrace::Location";
1844 if (!NIL_P(bt)) {
1845 if (RB_TYPE_P(bt, T_STRING)) return rb_ary_new3(1, bt);
1846 if (rb_backtrace_p(bt)) return bt;
1847 if (!RB_TYPE_P(bt, T_ARRAY)) {
1848 rb_raise(rb_eTypeError, err);
1850 for (i=0;i<RARRAY_LEN(bt);i++) {
1851 VALUE e = RARRAY_AREF(bt, i);
1852 if (!RB_TYPE_P(e, T_STRING)) {
1853 rb_raise(rb_eTypeError, err);
1857 return bt;
1861 * call-seq:
1862 * exc.set_backtrace(backtrace) -> array
1864 * Sets the backtrace information associated with +exc+. The +backtrace+ must
1865 * be an array of Thread::Backtrace::Location objects or an array of String objects
1866 * or a single String in the format described in Exception#backtrace.
1870 static VALUE
1871 exc_set_backtrace(VALUE exc, VALUE bt)
1873 VALUE btobj = rb_location_ary_to_backtrace(bt);
1874 if (RTEST(btobj)) {
1875 rb_ivar_set(exc, id_bt, btobj);
1876 rb_ivar_set(exc, id_bt_locations, btobj);
1877 return bt;
1879 else {
1880 return rb_ivar_set(exc, id_bt, rb_check_backtrace(bt));
1884 VALUE
1885 rb_exc_set_backtrace(VALUE exc, VALUE bt)
1887 return exc_set_backtrace(exc, bt);
1891 * call-seq:
1892 * exception.cause -> an_exception or nil
1894 * Returns the previous exception ($!) at the time this exception was raised.
1895 * This is useful for wrapping exceptions and retaining the original exception
1896 * information.
1899 static VALUE
1900 exc_cause(VALUE exc)
1902 return rb_attr_get(exc, id_cause);
1905 static VALUE
1906 try_convert_to_exception(VALUE obj)
1908 return rb_check_funcall(obj, idException, 0, 0);
1912 * call-seq:
1913 * exc == obj -> true or false
1915 * Equality---If <i>obj</i> is not an Exception, returns
1916 * <code>false</code>. Otherwise, returns <code>true</code> if <i>exc</i> and
1917 * <i>obj</i> share same class, messages, and backtrace.
1920 static VALUE
1921 exc_equal(VALUE exc, VALUE obj)
1923 VALUE mesg, backtrace;
1925 if (exc == obj) return Qtrue;
1927 if (rb_obj_class(exc) != rb_obj_class(obj)) {
1928 int state;
1930 obj = rb_protect(try_convert_to_exception, obj, &state);
1931 if (state || UNDEF_P(obj)) {
1932 rb_set_errinfo(Qnil);
1933 return Qfalse;
1935 if (rb_obj_class(exc) != rb_obj_class(obj)) return Qfalse;
1936 mesg = rb_check_funcall(obj, id_message, 0, 0);
1937 if (UNDEF_P(mesg)) return Qfalse;
1938 backtrace = rb_check_funcall(obj, id_backtrace, 0, 0);
1939 if (UNDEF_P(backtrace)) return Qfalse;
1941 else {
1942 mesg = rb_attr_get(obj, id_mesg);
1943 backtrace = exc_backtrace(obj);
1946 if (!rb_equal(rb_attr_get(exc, id_mesg), mesg))
1947 return Qfalse;
1948 return rb_equal(exc_backtrace(exc), backtrace);
1952 * call-seq:
1953 * SystemExit.new -> system_exit
1954 * SystemExit.new(status) -> system_exit
1955 * SystemExit.new(status, msg) -> system_exit
1956 * SystemExit.new(msg) -> system_exit
1958 * Create a new +SystemExit+ exception with the given status and message.
1959 * Status is true, false, or an integer.
1960 * If status is not given, true is used.
1963 static VALUE
1964 exit_initialize(int argc, VALUE *argv, VALUE exc)
1966 VALUE status;
1967 if (argc > 0) {
1968 status = *argv;
1970 switch (status) {
1971 case Qtrue:
1972 status = INT2FIX(EXIT_SUCCESS);
1973 ++argv;
1974 --argc;
1975 break;
1976 case Qfalse:
1977 status = INT2FIX(EXIT_FAILURE);
1978 ++argv;
1979 --argc;
1980 break;
1981 default:
1982 status = rb_check_to_int(status);
1983 if (NIL_P(status)) {
1984 status = INT2FIX(EXIT_SUCCESS);
1986 else {
1987 #if EXIT_SUCCESS != 0
1988 if (status == INT2FIX(0))
1989 status = INT2FIX(EXIT_SUCCESS);
1990 #endif
1991 ++argv;
1992 --argc;
1994 break;
1997 else {
1998 status = INT2FIX(EXIT_SUCCESS);
2000 rb_call_super(argc, argv);
2001 rb_ivar_set(exc, id_status, status);
2002 return exc;
2007 * call-seq:
2008 * system_exit.status -> integer
2010 * Return the status value associated with this system exit.
2013 static VALUE
2014 exit_status(VALUE exc)
2016 return rb_attr_get(exc, id_status);
2021 * call-seq:
2022 * system_exit.success? -> true or false
2024 * Returns +true+ if exiting successful, +false+ if not.
2027 static VALUE
2028 exit_success_p(VALUE exc)
2030 VALUE status_val = rb_attr_get(exc, id_status);
2031 int status;
2033 if (NIL_P(status_val))
2034 return Qtrue;
2035 status = NUM2INT(status_val);
2036 return RBOOL(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
2039 static VALUE
2040 err_init_recv(VALUE exc, VALUE recv)
2042 if (!UNDEF_P(recv)) rb_ivar_set(exc, id_recv, recv);
2043 return exc;
2047 * call-seq:
2048 * FrozenError.new(msg=nil, receiver: nil) -> frozen_error
2050 * Construct a new FrozenError exception. If given the <i>receiver</i>
2051 * parameter may subsequently be examined using the FrozenError#receiver
2052 * method.
2054 * a = [].freeze
2055 * raise FrozenError.new("can't modify frozen array", receiver: a)
2058 static VALUE
2059 frozen_err_initialize(int argc, VALUE *argv, VALUE self)
2061 ID keywords[1];
2062 VALUE values[numberof(keywords)], options;
2064 argc = rb_scan_args(argc, argv, "*:", NULL, &options);
2065 keywords[0] = id_receiver;
2066 rb_get_kwargs(options, keywords, 0, numberof(values), values);
2067 rb_call_super(argc, argv);
2068 err_init_recv(self, values[0]);
2069 return self;
2073 * Document-method: FrozenError#receiver
2074 * call-seq:
2075 * frozen_error.receiver -> object
2077 * Return the receiver associated with this FrozenError exception.
2080 #define frozen_err_receiver name_err_receiver
2082 void
2083 rb_name_error(ID id, const char *fmt, ...)
2085 VALUE exc, argv[2];
2086 va_list args;
2088 va_start(args, fmt);
2089 argv[0] = rb_vsprintf(fmt, args);
2090 va_end(args);
2092 argv[1] = ID2SYM(id);
2093 exc = rb_class_new_instance(2, argv, rb_eNameError);
2094 rb_exc_raise(exc);
2097 void
2098 rb_name_error_str(VALUE str, const char *fmt, ...)
2100 VALUE exc, argv[2];
2101 va_list args;
2103 va_start(args, fmt);
2104 argv[0] = rb_vsprintf(fmt, args);
2105 va_end(args);
2107 argv[1] = str;
2108 exc = rb_class_new_instance(2, argv, rb_eNameError);
2109 rb_exc_raise(exc);
2112 static VALUE
2113 name_err_init_attr(VALUE exc, VALUE recv, VALUE method)
2115 const rb_execution_context_t *ec = GET_EC();
2116 rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp);
2117 cfp = rb_vm_get_ruby_level_next_cfp(ec, cfp);
2118 rb_ivar_set(exc, id_name, method);
2119 err_init_recv(exc, recv);
2120 if (cfp && VM_FRAME_TYPE(cfp) != VM_FRAME_MAGIC_DUMMY) {
2121 rb_ivar_set(exc, id_iseq, rb_iseqw_new(cfp->iseq));
2123 return exc;
2127 * call-seq:
2128 * NameError.new(msg=nil, name=nil, receiver: nil) -> name_error
2130 * Construct a new NameError exception. If given the <i>name</i>
2131 * parameter may subsequently be examined using the NameError#name
2132 * method. <i>receiver</i> parameter allows to pass object in
2133 * context of which the error happened. Example:
2135 * [1, 2, 3].method(:rject) # NameError with name "rject" and receiver: Array
2136 * [1, 2, 3].singleton_method(:rject) # NameError with name "rject" and receiver: [1, 2, 3]
2139 static VALUE
2140 name_err_initialize(int argc, VALUE *argv, VALUE self)
2142 ID keywords[1];
2143 VALUE values[numberof(keywords)], name, options;
2145 argc = rb_scan_args(argc, argv, "*:", NULL, &options);
2146 keywords[0] = id_receiver;
2147 rb_get_kwargs(options, keywords, 0, numberof(values), values);
2148 name = (argc > 1) ? argv[--argc] : Qnil;
2149 rb_call_super(argc, argv);
2150 name_err_init_attr(self, values[0], name);
2151 return self;
2154 static VALUE rb_name_err_mesg_new(VALUE mesg, VALUE recv, VALUE method);
2156 static VALUE
2157 name_err_init(VALUE exc, VALUE mesg, VALUE recv, VALUE method)
2159 exc_init(exc, rb_name_err_mesg_new(mesg, recv, method));
2160 return name_err_init_attr(exc, recv, method);
2163 VALUE
2164 rb_name_err_new(VALUE mesg, VALUE recv, VALUE method)
2166 VALUE exc = rb_obj_alloc(rb_eNameError);
2167 return name_err_init(exc, mesg, recv, method);
2171 * call-seq:
2172 * name_error.name -> string or nil
2174 * Return the name associated with this NameError exception.
2177 static VALUE
2178 name_err_name(VALUE self)
2180 return rb_attr_get(self, id_name);
2184 * call-seq:
2185 * name_error.local_variables -> array
2187 * Return a list of the local variable names defined where this
2188 * NameError exception was raised.
2190 * Internal use only.
2193 static VALUE
2194 name_err_local_variables(VALUE self)
2196 VALUE vars = rb_attr_get(self, id_local_variables);
2198 if (NIL_P(vars)) {
2199 VALUE iseqw = rb_attr_get(self, id_iseq);
2200 if (!NIL_P(iseqw)) vars = rb_iseqw_local_variables(iseqw);
2201 if (NIL_P(vars)) vars = rb_ary_new();
2202 rb_ivar_set(self, id_local_variables, vars);
2204 return vars;
2207 static VALUE
2208 nometh_err_init_attr(VALUE exc, VALUE args, int priv)
2210 rb_ivar_set(exc, id_args, args);
2211 rb_ivar_set(exc, id_private_call_p, RBOOL(priv));
2212 return exc;
2216 * call-seq:
2217 * NoMethodError.new(msg=nil, name=nil, args=nil, private=false, receiver: nil) -> no_method_error
2219 * Construct a NoMethodError exception for a method of the given name
2220 * called with the given arguments. The name may be accessed using
2221 * the <code>#name</code> method on the resulting object, and the
2222 * arguments using the <code>#args</code> method.
2224 * If <i>private</i> argument were passed, it designates method was
2225 * attempted to call in private context, and can be accessed with
2226 * <code>#private_call?</code> method.
2228 * <i>receiver</i> argument stores an object whose method was called.
2231 static VALUE
2232 nometh_err_initialize(int argc, VALUE *argv, VALUE self)
2234 int priv;
2235 VALUE args, options;
2236 argc = rb_scan_args(argc, argv, "*:", NULL, &options);
2237 priv = (argc > 3) && (--argc, RTEST(argv[argc]));
2238 args = (argc > 2) ? argv[--argc] : Qnil;
2239 if (!NIL_P(options)) argv[argc++] = options;
2240 rb_call_super_kw(argc, argv, RB_PASS_CALLED_KEYWORDS);
2241 return nometh_err_init_attr(self, args, priv);
2244 VALUE
2245 rb_nomethod_err_new(VALUE mesg, VALUE recv, VALUE method, VALUE args, int priv)
2247 VALUE exc = rb_obj_alloc(rb_eNoMethodError);
2248 name_err_init(exc, mesg, recv, method);
2249 return nometh_err_init_attr(exc, args, priv);
2252 typedef struct name_error_message_struct {
2253 VALUE mesg;
2254 VALUE recv;
2255 VALUE name;
2256 } name_error_message_t;
2258 static void
2259 name_err_mesg_mark(void *p)
2261 name_error_message_t *ptr = (name_error_message_t *)p;
2262 rb_gc_mark_movable(ptr->mesg);
2263 rb_gc_mark_movable(ptr->recv);
2264 rb_gc_mark_movable(ptr->name);
2267 static void
2268 name_err_mesg_update(void *p)
2270 name_error_message_t *ptr = (name_error_message_t *)p;
2271 ptr->mesg = rb_gc_location(ptr->mesg);
2272 ptr->recv = rb_gc_location(ptr->recv);
2273 ptr->name = rb_gc_location(ptr->name);
2276 static const rb_data_type_t name_err_mesg_data_type = {
2277 "name_err_mesg",
2279 name_err_mesg_mark,
2280 RUBY_TYPED_DEFAULT_FREE,
2281 NULL, // No external memory to report,
2282 name_err_mesg_update,
2284 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
2287 /* :nodoc: */
2288 static VALUE
2289 rb_name_err_mesg_init(VALUE klass, VALUE mesg, VALUE recv, VALUE name)
2291 name_error_message_t *message;
2292 VALUE result = TypedData_Make_Struct(klass, name_error_message_t, &name_err_mesg_data_type, message);
2293 RB_OBJ_WRITE(result, &message->mesg, mesg);
2294 RB_OBJ_WRITE(result, &message->recv, recv);
2295 RB_OBJ_WRITE(result, &message->name, name);
2296 return result;
2299 /* :nodoc: */
2300 static VALUE
2301 rb_name_err_mesg_new(VALUE mesg, VALUE recv, VALUE method)
2303 return rb_name_err_mesg_init(rb_cNameErrorMesg, mesg, recv, method);
2306 /* :nodoc: */
2307 static VALUE
2308 name_err_mesg_alloc(VALUE klass)
2310 return rb_name_err_mesg_init(klass, Qnil, Qnil, Qnil);
2313 /* :nodoc: */
2314 static VALUE
2315 name_err_mesg_init_copy(VALUE obj1, VALUE obj2)
2317 if (obj1 == obj2) return obj1;
2318 rb_obj_init_copy(obj1, obj2);
2320 name_error_message_t *ptr1, *ptr2;
2321 TypedData_Get_Struct(obj1, name_error_message_t, &name_err_mesg_data_type, ptr1);
2322 TypedData_Get_Struct(obj2, name_error_message_t, &name_err_mesg_data_type, ptr2);
2324 RB_OBJ_WRITE(obj1, &ptr1->mesg, ptr2->mesg);
2325 RB_OBJ_WRITE(obj1, &ptr1->recv, ptr2->recv);
2326 RB_OBJ_WRITE(obj1, &ptr1->name, ptr2->name);
2327 return obj1;
2330 /* :nodoc: */
2331 static VALUE
2332 name_err_mesg_equal(VALUE obj1, VALUE obj2)
2334 if (obj1 == obj2) return Qtrue;
2336 if (rb_obj_class(obj2) != rb_cNameErrorMesg)
2337 return Qfalse;
2339 name_error_message_t *ptr1, *ptr2;
2340 TypedData_Get_Struct(obj1, name_error_message_t, &name_err_mesg_data_type, ptr1);
2341 TypedData_Get_Struct(obj2, name_error_message_t, &name_err_mesg_data_type, ptr2);
2343 if (!rb_equal(ptr1->mesg, ptr2->mesg)) return Qfalse;
2344 if (!rb_equal(ptr1->recv, ptr2->recv)) return Qfalse;
2345 if (!rb_equal(ptr1->name, ptr2->name)) return Qfalse;
2346 return Qtrue;
2349 /* :nodoc: */
2350 static VALUE
2351 name_err_mesg_receiver_name(VALUE obj)
2353 if (RB_SPECIAL_CONST_P(obj)) return Qundef;
2354 if (RB_BUILTIN_TYPE(obj) == T_MODULE || RB_BUILTIN_TYPE(obj) == T_CLASS) {
2355 return rb_check_funcall(obj, rb_intern("name"), 0, 0);
2357 return Qundef;
2360 /* :nodoc: */
2361 static VALUE
2362 name_err_mesg_to_str(VALUE obj)
2364 name_error_message_t *ptr;
2365 TypedData_Get_Struct(obj, name_error_message_t, &name_err_mesg_data_type, ptr);
2367 VALUE mesg = ptr->mesg;
2368 if (NIL_P(mesg)) return Qnil;
2369 else {
2370 struct RString s_str, c_str, d_str;
2371 VALUE c, s, d = 0, args[4], c2;
2372 int state = 0;
2373 rb_encoding *usascii = rb_usascii_encoding();
2375 #define FAKE_CSTR(v, str) rb_setup_fake_str((v), (str), rb_strlen_lit(str), usascii)
2376 c = s = FAKE_CSTR(&s_str, "");
2377 obj = ptr->recv;
2378 switch (obj) {
2379 case Qnil:
2380 c = d = FAKE_CSTR(&d_str, "nil");
2381 break;
2382 case Qtrue:
2383 c = d = FAKE_CSTR(&d_str, "true");
2384 break;
2385 case Qfalse:
2386 c = d = FAKE_CSTR(&d_str, "false");
2387 break;
2388 default:
2389 if (strstr(RSTRING_PTR(mesg), "%2$s")) {
2390 d = rb_protect(name_err_mesg_receiver_name, obj, &state);
2391 if (state || NIL_OR_UNDEF_P(d))
2392 d = rb_protect(rb_inspect, obj, &state);
2393 if (state) {
2394 rb_set_errinfo(Qnil);
2396 d = rb_check_string_type(d);
2397 if (NIL_P(d)) {
2398 d = rb_any_to_s(obj);
2402 if (!RB_SPECIAL_CONST_P(obj)) {
2403 switch (RB_BUILTIN_TYPE(obj)) {
2404 case T_MODULE:
2405 s = FAKE_CSTR(&s_str, "module ");
2406 c = obj;
2407 break;
2408 case T_CLASS:
2409 s = FAKE_CSTR(&s_str, "class ");
2410 c = obj;
2411 break;
2412 default:
2413 goto object;
2416 else {
2417 VALUE klass;
2418 object:
2419 klass = CLASS_OF(obj);
2420 if (RB_TYPE_P(klass, T_CLASS) && RCLASS_SINGLETON_P(klass)) {
2421 s = FAKE_CSTR(&s_str, "");
2422 if (obj == rb_vm_top_self()) {
2423 c = FAKE_CSTR(&c_str, "main");
2425 else {
2426 c = rb_any_to_s(obj);
2428 break;
2430 else {
2431 s = FAKE_CSTR(&s_str, "an instance of ");
2432 c = rb_class_real(klass);
2435 c2 = rb_protect(name_err_mesg_receiver_name, c, &state);
2436 if (state || NIL_OR_UNDEF_P(c2))
2437 c2 = rb_protect(rb_inspect, c, &state);
2438 if (state) {
2439 rb_set_errinfo(Qnil);
2441 c2 = rb_check_string_type(c2);
2442 if (NIL_P(c2)) {
2443 c2 = rb_any_to_s(c);
2445 c = c2;
2446 break;
2448 args[0] = rb_obj_as_string(ptr->name);
2449 args[1] = d;
2450 args[2] = s;
2451 args[3] = c;
2452 mesg = rb_str_format(4, args, mesg);
2454 return mesg;
2457 /* :nodoc: */
2458 static VALUE
2459 name_err_mesg_dump(VALUE obj, VALUE limit)
2461 return name_err_mesg_to_str(obj);
2464 /* :nodoc: */
2465 static VALUE
2466 name_err_mesg_load(VALUE klass, VALUE str)
2468 return str;
2472 * call-seq:
2473 * name_error.receiver -> object
2475 * Return the receiver associated with this NameError exception.
2478 static VALUE
2479 name_err_receiver(VALUE self)
2481 VALUE recv = rb_ivar_lookup(self, id_recv, Qundef);
2482 if (!UNDEF_P(recv)) return recv;
2484 VALUE mesg = rb_attr_get(self, id_mesg);
2485 if (!rb_typeddata_is_kind_of(mesg, &name_err_mesg_data_type)) {
2486 rb_raise(rb_eArgError, "no receiver is available");
2489 name_error_message_t *ptr;
2490 TypedData_Get_Struct(mesg, name_error_message_t, &name_err_mesg_data_type, ptr);
2491 return ptr->recv;
2495 * call-seq:
2496 * no_method_error.args -> obj
2498 * Return the arguments passed in as the third parameter to
2499 * the constructor.
2502 static VALUE
2503 nometh_err_args(VALUE self)
2505 return rb_attr_get(self, id_args);
2509 * call-seq:
2510 * no_method_error.private_call? -> true or false
2512 * Return true if the caused method was called as private.
2515 static VALUE
2516 nometh_err_private_call_p(VALUE self)
2518 return rb_attr_get(self, id_private_call_p);
2521 void
2522 rb_invalid_str(const char *str, const char *type)
2524 VALUE s = rb_str_new2(str);
2526 rb_raise(rb_eArgError, "invalid value for %s: %+"PRIsVALUE, type, s);
2530 * call-seq:
2531 * key_error.receiver -> object
2533 * Return the receiver associated with this KeyError exception.
2536 static VALUE
2537 key_err_receiver(VALUE self)
2539 VALUE recv;
2541 recv = rb_ivar_lookup(self, id_receiver, Qundef);
2542 if (!UNDEF_P(recv)) return recv;
2543 rb_raise(rb_eArgError, "no receiver is available");
2547 * call-seq:
2548 * key_error.key -> object
2550 * Return the key caused this KeyError exception.
2553 static VALUE
2554 key_err_key(VALUE self)
2556 VALUE key;
2558 key = rb_ivar_lookup(self, id_key, Qundef);
2559 if (!UNDEF_P(key)) return key;
2560 rb_raise(rb_eArgError, "no key is available");
2563 VALUE
2564 rb_key_err_new(VALUE mesg, VALUE recv, VALUE key)
2566 VALUE exc = rb_obj_alloc(rb_eKeyError);
2567 rb_ivar_set(exc, id_mesg, mesg);
2568 rb_ivar_set(exc, id_bt, Qnil);
2569 rb_ivar_set(exc, id_key, key);
2570 rb_ivar_set(exc, id_receiver, recv);
2571 return exc;
2575 * call-seq:
2576 * KeyError.new(message=nil, receiver: nil, key: nil) -> key_error
2578 * Construct a new +KeyError+ exception with the given message,
2579 * receiver and key.
2582 static VALUE
2583 key_err_initialize(int argc, VALUE *argv, VALUE self)
2585 VALUE options;
2587 rb_call_super(rb_scan_args(argc, argv, "01:", NULL, &options), argv);
2589 if (!NIL_P(options)) {
2590 ID keywords[2];
2591 VALUE values[numberof(keywords)];
2592 int i;
2593 keywords[0] = id_receiver;
2594 keywords[1] = id_key;
2595 rb_get_kwargs(options, keywords, 0, numberof(values), values);
2596 for (i = 0; i < numberof(values); ++i) {
2597 if (!UNDEF_P(values[i])) {
2598 rb_ivar_set(self, keywords[i], values[i]);
2603 return self;
2607 * call-seq:
2608 * no_matching_pattern_key_error.matchee -> object
2610 * Return the matchee associated with this NoMatchingPatternKeyError exception.
2613 static VALUE
2614 no_matching_pattern_key_err_matchee(VALUE self)
2616 VALUE matchee;
2618 matchee = rb_ivar_lookup(self, id_matchee, Qundef);
2619 if (!UNDEF_P(matchee)) return matchee;
2620 rb_raise(rb_eArgError, "no matchee is available");
2624 * call-seq:
2625 * no_matching_pattern_key_error.key -> object
2627 * Return the key caused this NoMatchingPatternKeyError exception.
2630 static VALUE
2631 no_matching_pattern_key_err_key(VALUE self)
2633 VALUE key;
2635 key = rb_ivar_lookup(self, id_key, Qundef);
2636 if (!UNDEF_P(key)) return key;
2637 rb_raise(rb_eArgError, "no key is available");
2641 * call-seq:
2642 * NoMatchingPatternKeyError.new(message=nil, matchee: nil, key: nil) -> no_matching_pattern_key_error
2644 * Construct a new +NoMatchingPatternKeyError+ exception with the given message,
2645 * matchee and key.
2648 static VALUE
2649 no_matching_pattern_key_err_initialize(int argc, VALUE *argv, VALUE self)
2651 VALUE options;
2653 rb_call_super(rb_scan_args(argc, argv, "01:", NULL, &options), argv);
2655 if (!NIL_P(options)) {
2656 ID keywords[2];
2657 VALUE values[numberof(keywords)];
2658 int i;
2659 keywords[0] = id_matchee;
2660 keywords[1] = id_key;
2661 rb_get_kwargs(options, keywords, 0, numberof(values), values);
2662 for (i = 0; i < numberof(values); ++i) {
2663 if (!UNDEF_P(values[i])) {
2664 rb_ivar_set(self, keywords[i], values[i]);
2669 return self;
2674 * call-seq:
2675 * SyntaxError.new([msg]) -> syntax_error
2677 * Construct a SyntaxError exception.
2680 static VALUE
2681 syntax_error_initialize(int argc, VALUE *argv, VALUE self)
2683 VALUE mesg;
2684 if (argc == 0) {
2685 mesg = rb_fstring_lit("compile error");
2686 argc = 1;
2687 argv = &mesg;
2689 return rb_call_super(argc, argv);
2692 static VALUE
2693 syntax_error_with_path(VALUE exc, VALUE path, VALUE *mesg, rb_encoding *enc)
2695 if (NIL_P(exc)) {
2696 *mesg = rb_enc_str_new(0, 0, enc);
2697 exc = rb_class_new_instance(1, mesg, rb_eSyntaxError);
2698 rb_ivar_set(exc, id_i_path, path);
2700 else {
2701 if (rb_attr_get(exc, id_i_path) != path) {
2702 rb_raise(rb_eArgError, "SyntaxError#path changed");
2704 VALUE s = *mesg = rb_attr_get(exc, idMesg);
2705 if (RSTRING_LEN(s) > 0 && *(RSTRING_END(s)-1) != '\n')
2706 rb_str_cat_cstr(s, "\n");
2708 return exc;
2712 * Document-module: Errno
2714 * Ruby exception objects are subclasses of Exception. However,
2715 * operating systems typically report errors using plain
2716 * integers. Module Errno is created dynamically to map these
2717 * operating system errors to Ruby classes, with each error number
2718 * generating its own subclass of SystemCallError. As the subclass
2719 * is created in module Errno, its name will start
2720 * <code>Errno::</code>.
2722 * The names of the <code>Errno::</code> classes depend on the
2723 * environment in which Ruby runs. On a typical Unix or Windows
2724 * platform, there are Errno classes such as Errno::EACCES,
2725 * Errno::EAGAIN, Errno::EINTR, and so on.
2727 * The integer operating system error number corresponding to a
2728 * particular error is available as the class constant
2729 * <code>Errno::</code><em>error</em><code>::Errno</code>.
2731 * Errno::EACCES::Errno #=> 13
2732 * Errno::EAGAIN::Errno #=> 11
2733 * Errno::EINTR::Errno #=> 4
2735 * The full list of operating system errors on your particular platform
2736 * are available as the constants of Errno.
2738 * Errno.constants #=> :E2BIG, :EACCES, :EADDRINUSE, :EADDRNOTAVAIL, ...
2741 static st_table *syserr_tbl;
2743 void
2744 rb_free_warning(void)
2746 st_free_table(warning_categories.id2enum);
2747 st_free_table(warning_categories.enum2id);
2748 st_free_table(syserr_tbl);
2751 static VALUE
2752 setup_syserr(int n, const char *name)
2754 VALUE error = rb_define_class_under(rb_mErrno, name, rb_eSystemCallError);
2756 /* capture nonblock errnos for WaitReadable/WaitWritable subclasses */
2757 switch (n) {
2758 case EAGAIN:
2759 rb_eEAGAIN = error;
2761 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
2762 break;
2763 case EWOULDBLOCK:
2764 #endif
2766 rb_eEWOULDBLOCK = error;
2767 break;
2768 case EINPROGRESS:
2769 rb_eEINPROGRESS = error;
2770 break;
2773 rb_define_const(error, "Errno", INT2NUM(n));
2774 st_add_direct(syserr_tbl, n, (st_data_t)error);
2775 return error;
2778 static VALUE
2779 set_syserr(int n, const char *name)
2781 st_data_t error;
2783 if (!st_lookup(syserr_tbl, n, &error)) {
2784 return setup_syserr(n, name);
2786 else {
2787 VALUE errclass = (VALUE)error;
2788 rb_define_const(rb_mErrno, name, errclass);
2789 return errclass;
2793 static VALUE
2794 get_syserr(int n)
2796 st_data_t error;
2798 if (!st_lookup(syserr_tbl, n, &error)) {
2799 char name[DECIMAL_SIZE_OF(n) + sizeof("E-")];
2801 snprintf(name, sizeof(name), "E%03d", n);
2802 return setup_syserr(n, name);
2804 return (VALUE)error;
2808 * call-seq:
2809 * SystemCallError.new(msg, errno) -> system_call_error_subclass
2811 * If _errno_ corresponds to a known system error code, constructs the
2812 * appropriate Errno class for that error, otherwise constructs a
2813 * generic SystemCallError object. The error number is subsequently
2814 * available via the #errno method.
2817 static VALUE
2818 syserr_initialize(int argc, VALUE *argv, VALUE self)
2820 const char *err;
2821 VALUE mesg, error, func, errmsg;
2822 VALUE klass = rb_obj_class(self);
2824 if (klass == rb_eSystemCallError) {
2825 st_data_t data = (st_data_t)klass;
2826 rb_scan_args(argc, argv, "12", &mesg, &error, &func);
2827 if (argc == 1 && FIXNUM_P(mesg)) {
2828 error = mesg; mesg = Qnil;
2830 if (!NIL_P(error) && st_lookup(syserr_tbl, NUM2LONG(error), &data)) {
2831 klass = (VALUE)data;
2832 /* change class */
2833 if (!RB_TYPE_P(self, T_OBJECT)) { /* insurance to avoid type crash */
2834 rb_raise(rb_eTypeError, "invalid instance type");
2836 RBASIC_SET_CLASS(self, klass);
2839 else {
2840 rb_scan_args(argc, argv, "02", &mesg, &func);
2841 error = rb_const_get(klass, id_Errno);
2843 if (!NIL_P(error)) err = strerror(NUM2INT(error));
2844 else err = "unknown error";
2846 errmsg = rb_enc_str_new_cstr(err, rb_locale_encoding());
2847 if (!NIL_P(mesg)) {
2848 VALUE str = StringValue(mesg);
2850 if (!NIL_P(func)) rb_str_catf(errmsg, " @ %"PRIsVALUE, func);
2851 rb_str_catf(errmsg, " - %"PRIsVALUE, str);
2853 mesg = errmsg;
2855 rb_call_super(1, &mesg);
2856 rb_ivar_set(self, id_errno, error);
2857 return self;
2861 * call-seq:
2862 * system_call_error.errno -> integer
2864 * Return this SystemCallError's error number.
2867 static VALUE
2868 syserr_errno(VALUE self)
2870 return rb_attr_get(self, id_errno);
2874 * call-seq:
2875 * system_call_error === other -> true or false
2877 * Return +true+ if the receiver is a generic +SystemCallError+, or
2878 * if the error numbers +self+ and _other_ are the same.
2881 static VALUE
2882 syserr_eqq(VALUE self, VALUE exc)
2884 VALUE num, e;
2886 if (!rb_obj_is_kind_of(exc, rb_eSystemCallError)) {
2887 if (!rb_respond_to(exc, id_errno)) return Qfalse;
2889 else if (self == rb_eSystemCallError) return Qtrue;
2891 num = rb_attr_get(exc, id_errno);
2892 if (NIL_P(num)) {
2893 num = rb_funcallv(exc, id_errno, 0, 0);
2895 e = rb_const_get(self, id_Errno);
2896 return RBOOL(FIXNUM_P(num) ? num == e : rb_equal(num, e));
2901 * Document-class: StandardError
2903 * The most standard error types are subclasses of StandardError. A
2904 * rescue clause without an explicit Exception class will rescue all
2905 * StandardErrors (and only those).
2907 * def foo
2908 * raise "Oups"
2909 * end
2910 * foo rescue "Hello" #=> "Hello"
2912 * On the other hand:
2914 * require 'does/not/exist' rescue "Hi"
2916 * <em>raises the exception:</em>
2918 * LoadError: no such file to load -- does/not/exist
2923 * Document-class: SystemExit
2925 * Raised by +exit+ to initiate the termination of the script.
2929 * Document-class: SignalException
2931 * Raised when a signal is received.
2933 * begin
2934 * Process.kill('HUP',Process.pid)
2935 * sleep # wait for receiver to handle signal sent by Process.kill
2936 * rescue SignalException => e
2937 * puts "received Exception #{e}"
2938 * end
2940 * <em>produces:</em>
2942 * received Exception SIGHUP
2946 * Document-class: Interrupt
2948 * Raised when the interrupt signal is received, typically because the
2949 * user has pressed Control-C (on most posix platforms). As such, it is a
2950 * subclass of +SignalException+.
2952 * begin
2953 * puts "Press ctrl-C when you get bored"
2954 * loop {}
2955 * rescue Interrupt => e
2956 * puts "Note: You will typically use Signal.trap instead."
2957 * end
2959 * <em>produces:</em>
2961 * Press ctrl-C when you get bored
2963 * <em>then waits until it is interrupted with Control-C and then prints:</em>
2965 * Note: You will typically use Signal.trap instead.
2969 * Document-class: TypeError
2971 * Raised when encountering an object that is not of the expected type.
2973 * [1, 2, 3].first("two")
2975 * <em>raises the exception:</em>
2977 * TypeError: no implicit conversion of String into Integer
2982 * Document-class: ArgumentError
2984 * Raised when the arguments are wrong and there isn't a more specific
2985 * Exception class.
2987 * Ex: passing the wrong number of arguments
2989 * [1, 2, 3].first(4, 5)
2991 * <em>raises the exception:</em>
2993 * ArgumentError: wrong number of arguments (given 2, expected 1)
2995 * Ex: passing an argument that is not acceptable:
2997 * [1, 2, 3].first(-4)
2999 * <em>raises the exception:</em>
3001 * ArgumentError: negative array size
3005 * Document-class: IndexError
3007 * Raised when the given index is invalid.
3009 * a = [:foo, :bar]
3010 * a.fetch(0) #=> :foo
3011 * a[4] #=> nil
3012 * a.fetch(4) #=> IndexError: index 4 outside of array bounds: -2...2
3017 * Document-class: KeyError
3019 * Raised when the specified key is not found. It is a subclass of
3020 * IndexError.
3022 * h = {"foo" => :bar}
3023 * h.fetch("foo") #=> :bar
3024 * h.fetch("baz") #=> KeyError: key not found: "baz"
3029 * Document-class: RangeError
3031 * Raised when a given numerical value is out of range.
3033 * [1, 2, 3].drop(1 << 100)
3035 * <em>raises the exception:</em>
3037 * RangeError: bignum too big to convert into `long'
3041 * Document-class: ScriptError
3043 * ScriptError is the superclass for errors raised when a script
3044 * can not be executed because of a +LoadError+,
3045 * +NotImplementedError+ or a +SyntaxError+. Note these type of
3046 * +ScriptErrors+ are not +StandardError+ and will not be
3047 * rescued unless it is specified explicitly (or its ancestor
3048 * +Exception+).
3052 * Document-class: SyntaxError
3054 * Raised when encountering Ruby code with an invalid syntax.
3056 * eval("1+1=2")
3058 * <em>raises the exception:</em>
3060 * SyntaxError: (eval):1: syntax error, unexpected '=', expecting $end
3064 * Document-class: LoadError
3066 * Raised when a file required (a Ruby script, extension library, ...)
3067 * fails to load.
3069 * require 'this/file/does/not/exist'
3071 * <em>raises the exception:</em>
3073 * LoadError: no such file to load -- this/file/does/not/exist
3077 * Document-class: NotImplementedError
3079 * Raised when a feature is not implemented on the current platform. For
3080 * example, methods depending on the +fsync+ or +fork+ system calls may
3081 * raise this exception if the underlying operating system or Ruby
3082 * runtime does not support them.
3084 * Note that if +fork+ raises a +NotImplementedError+, then
3085 * <code>respond_to?(:fork)</code> returns +false+.
3089 * Document-class: NameError
3091 * Raised when a given name is invalid or undefined.
3093 * puts foo
3095 * <em>raises the exception:</em>
3097 * NameError: undefined local variable or method `foo' for main:Object
3099 * Since constant names must start with a capital:
3101 * Integer.const_set :answer, 42
3103 * <em>raises the exception:</em>
3105 * NameError: wrong constant name answer
3109 * Document-class: NoMethodError
3111 * Raised when a method is called on a receiver which doesn't have it
3112 * defined and also fails to respond with +method_missing+.
3114 * "hello".to_ary
3116 * <em>raises the exception:</em>
3118 * NoMethodError: undefined method `to_ary' for an instance of String
3122 * Document-class: FrozenError
3124 * Raised when there is an attempt to modify a frozen object.
3126 * [1, 2, 3].freeze << 4
3128 * <em>raises the exception:</em>
3130 * FrozenError: can't modify frozen Array
3134 * Document-class: RuntimeError
3136 * A generic error class raised when an invalid operation is attempted.
3137 * Kernel#raise will raise a RuntimeError if no Exception class is
3138 * specified.
3140 * raise "ouch"
3142 * <em>raises the exception:</em>
3144 * RuntimeError: ouch
3148 * Document-class: SecurityError
3150 * No longer used by internal code.
3154 * Document-class: NoMemoryError
3156 * Raised when memory allocation fails.
3160 * Document-class: SystemCallError
3162 * SystemCallError is the base class for all low-level
3163 * platform-dependent errors.
3165 * The errors available on the current platform are subclasses of
3166 * SystemCallError and are defined in the Errno module.
3168 * File.open("does/not/exist")
3170 * <em>raises the exception:</em>
3172 * Errno::ENOENT: No such file or directory - does/not/exist
3176 * Document-class: EncodingError
3178 * EncodingError is the base class for encoding errors.
3182 * Document-class: Encoding::CompatibilityError
3184 * Raised by Encoding and String methods when the source encoding is
3185 * incompatible with the target encoding.
3189 * Document-class: fatal
3191 * +fatal+ is an Exception that is raised when Ruby has encountered a fatal
3192 * error and must exit.
3196 * Document-class: NameError::message
3197 * :nodoc:
3201 * Document-class: Exception
3203 * \Class Exception and its subclasses are used to communicate between
3204 * Kernel#raise and +rescue+ statements in <code>begin ... end</code> blocks.
3206 * An Exception object carries information about an exception:
3207 * - Its type (the exception's class).
3208 * - An optional descriptive message.
3209 * - Optional backtrace information.
3211 * Some built-in subclasses of Exception have additional methods: e.g., NameError#name.
3213 * == Defaults
3215 * Two Ruby statements have default exception classes:
3216 * - +raise+: defaults to RuntimeError.
3217 * - +rescue+: defaults to StandardError.
3219 * == Global Variables
3221 * When an exception has been raised but not yet handled (in +rescue+,
3222 * +ensure+, +at_exit+ and +END+ blocks), two global variables are set:
3223 * - <code>$!</code> contains the current exception.
3224 * - <code>$@</code> contains its backtrace.
3226 * == Custom Exceptions
3228 * To provide additional or alternate information,
3229 * a program may create custom exception classes
3230 * that derive from the built-in exception classes.
3232 * A good practice is for a library to create a single "generic" exception class
3233 * (typically a subclass of StandardError or RuntimeError)
3234 * and have its other exception classes derive from that class.
3235 * This allows the user to rescue the generic exception, thus catching all exceptions
3236 * the library may raise even if future versions of the library add new
3237 * exception subclasses.
3239 * For example:
3241 * class MyLibrary
3242 * class Error < ::StandardError
3243 * end
3245 * class WidgetError < Error
3246 * end
3248 * class FrobError < Error
3249 * end
3251 * end
3253 * To handle both MyLibrary::WidgetError and MyLibrary::FrobError the library
3254 * user can rescue MyLibrary::Error.
3256 * == Built-In Exception Classes
3258 * The built-in subclasses of Exception are:
3260 * * NoMemoryError
3261 * * ScriptError
3262 * * LoadError
3263 * * NotImplementedError
3264 * * SyntaxError
3265 * * SecurityError
3266 * * SignalException
3267 * * Interrupt
3268 * * StandardError
3269 * * ArgumentError
3270 * * UncaughtThrowError
3271 * * EncodingError
3272 * * FiberError
3273 * * IOError
3274 * * EOFError
3275 * * IndexError
3276 * * KeyError
3277 * * StopIteration
3278 * * ClosedQueueError
3279 * * LocalJumpError
3280 * * NameError
3281 * * NoMethodError
3282 * * RangeError
3283 * * FloatDomainError
3284 * * RegexpError
3285 * * RuntimeError
3286 * * FrozenError
3287 * * SystemCallError
3288 * * Errno::*
3289 * * ThreadError
3290 * * TypeError
3291 * * ZeroDivisionError
3292 * * SystemExit
3293 * * SystemStackError
3294 * * fatal
3297 static VALUE
3298 exception_alloc(VALUE klass)
3300 return rb_class_allocate_instance(klass);
3303 static VALUE
3304 exception_dumper(VALUE exc)
3306 // TODO: Currently, the instance variables "bt" and "bt_locations"
3307 // refers to the same object (Array of String). But "bt_locations"
3308 // should have an Array of Thread::Backtrace::Locations.
3310 return exc;
3313 static int
3314 ivar_copy_i(ID key, VALUE val, st_data_t exc)
3316 rb_ivar_set((VALUE)exc, key, val);
3317 return ST_CONTINUE;
3320 void rb_exc_check_circular_cause(VALUE exc);
3322 static VALUE
3323 exception_loader(VALUE exc, VALUE obj)
3325 // The loader function of rb_marshal_define_compat seems to be called for two events:
3326 // one is for fixup (r_fixup_compat), the other is for TYPE_USERDEF.
3327 // In the former case, the first argument is an instance of Exception (because
3328 // we pass rb_eException to rb_marshal_define_compat). In the latter case, the first
3329 // argument is a class object (see TYPE_USERDEF case in r_object0).
3330 // We want to copy all instance variables (but "bt_locations") from obj to exc.
3331 // But we do not want to do so in the second case, so the following branch is for that.
3332 if (RB_TYPE_P(exc, T_CLASS)) return obj; // maybe called from Marshal's TYPE_USERDEF
3334 rb_ivar_foreach(obj, ivar_copy_i, exc);
3336 rb_exc_check_circular_cause(exc);
3338 if (rb_attr_get(exc, id_bt) == rb_attr_get(exc, id_bt_locations)) {
3339 rb_ivar_set(exc, id_bt_locations, Qnil);
3342 return exc;
3345 void
3346 Init_Exception(void)
3348 rb_eException = rb_define_class("Exception", rb_cObject);
3349 rb_define_alloc_func(rb_eException, exception_alloc);
3350 rb_marshal_define_compat(rb_eException, rb_eException, exception_dumper, exception_loader);
3351 rb_define_singleton_method(rb_eException, "exception", rb_class_new_instance, -1);
3352 rb_define_singleton_method(rb_eException, "to_tty?", exc_s_to_tty_p, 0);
3353 rb_define_method(rb_eException, "exception", exc_exception, -1);
3354 rb_define_method(rb_eException, "initialize", exc_initialize, -1);
3355 rb_define_method(rb_eException, "==", exc_equal, 1);
3356 rb_define_method(rb_eException, "to_s", exc_to_s, 0);
3357 rb_define_method(rb_eException, "message", exc_message, 0);
3358 rb_define_method(rb_eException, "detailed_message", exc_detailed_message, -1);
3359 rb_define_method(rb_eException, "full_message", exc_full_message, -1);
3360 rb_define_method(rb_eException, "inspect", exc_inspect, 0);
3361 rb_define_method(rb_eException, "backtrace", exc_backtrace, 0);
3362 rb_define_method(rb_eException, "backtrace_locations", exc_backtrace_locations, 0);
3363 rb_define_method(rb_eException, "set_backtrace", exc_set_backtrace, 1);
3364 rb_define_method(rb_eException, "cause", exc_cause, 0);
3366 rb_eSystemExit = rb_define_class("SystemExit", rb_eException);
3367 rb_define_method(rb_eSystemExit, "initialize", exit_initialize, -1);
3368 rb_define_method(rb_eSystemExit, "status", exit_status, 0);
3369 rb_define_method(rb_eSystemExit, "success?", exit_success_p, 0);
3371 rb_eFatal = rb_define_class("fatal", rb_eException);
3372 rb_eSignal = rb_define_class("SignalException", rb_eException);
3373 rb_eInterrupt = rb_define_class("Interrupt", rb_eSignal);
3375 rb_eStandardError = rb_define_class("StandardError", rb_eException);
3376 rb_eTypeError = rb_define_class("TypeError", rb_eStandardError);
3377 rb_eArgError = rb_define_class("ArgumentError", rb_eStandardError);
3378 rb_eIndexError = rb_define_class("IndexError", rb_eStandardError);
3379 rb_eKeyError = rb_define_class("KeyError", rb_eIndexError);
3380 rb_define_method(rb_eKeyError, "initialize", key_err_initialize, -1);
3381 rb_define_method(rb_eKeyError, "receiver", key_err_receiver, 0);
3382 rb_define_method(rb_eKeyError, "key", key_err_key, 0);
3383 rb_eRangeError = rb_define_class("RangeError", rb_eStandardError);
3385 rb_eScriptError = rb_define_class("ScriptError", rb_eException);
3386 rb_eSyntaxError = rb_define_class("SyntaxError", rb_eScriptError);
3387 rb_define_method(rb_eSyntaxError, "initialize", syntax_error_initialize, -1);
3389 /* RDoc will use literal name value while parsing rb_attr,
3390 * and will render `idPath` as an attribute name without this trick */
3391 ID path = idPath;
3393 /* the path failed to parse */
3394 rb_attr(rb_eSyntaxError, path, TRUE, FALSE, FALSE);
3396 rb_eLoadError = rb_define_class("LoadError", rb_eScriptError);
3397 /* the path failed to load */
3398 rb_attr(rb_eLoadError, path, TRUE, FALSE, FALSE);
3400 rb_eNotImpError = rb_define_class("NotImplementedError", rb_eScriptError);
3402 rb_eNameError = rb_define_class("NameError", rb_eStandardError);
3403 rb_define_method(rb_eNameError, "initialize", name_err_initialize, -1);
3404 rb_define_method(rb_eNameError, "name", name_err_name, 0);
3405 rb_define_method(rb_eNameError, "receiver", name_err_receiver, 0);
3406 rb_define_method(rb_eNameError, "local_variables", name_err_local_variables, 0);
3407 rb_cNameErrorMesg = rb_define_class_under(rb_eNameError, "message", rb_cObject);
3408 rb_define_alloc_func(rb_cNameErrorMesg, name_err_mesg_alloc);
3409 rb_define_method(rb_cNameErrorMesg, "initialize_copy", name_err_mesg_init_copy, 1);
3410 rb_define_method(rb_cNameErrorMesg, "==", name_err_mesg_equal, 1);
3411 rb_define_method(rb_cNameErrorMesg, "to_str", name_err_mesg_to_str, 0);
3412 rb_define_method(rb_cNameErrorMesg, "_dump", name_err_mesg_dump, 1);
3413 rb_define_singleton_method(rb_cNameErrorMesg, "_load", name_err_mesg_load, 1);
3414 rb_eNoMethodError = rb_define_class("NoMethodError", rb_eNameError);
3415 rb_define_method(rb_eNoMethodError, "initialize", nometh_err_initialize, -1);
3416 rb_define_method(rb_eNoMethodError, "args", nometh_err_args, 0);
3417 rb_define_method(rb_eNoMethodError, "private_call?", nometh_err_private_call_p, 0);
3419 rb_eRuntimeError = rb_define_class("RuntimeError", rb_eStandardError);
3420 rb_eFrozenError = rb_define_class("FrozenError", rb_eRuntimeError);
3421 rb_define_method(rb_eFrozenError, "initialize", frozen_err_initialize, -1);
3422 rb_define_method(rb_eFrozenError, "receiver", frozen_err_receiver, 0);
3423 rb_eSecurityError = rb_define_class("SecurityError", rb_eException);
3424 rb_eNoMemError = rb_define_class("NoMemoryError", rb_eException);
3425 rb_eEncodingError = rb_define_class("EncodingError", rb_eStandardError);
3426 rb_eEncCompatError = rb_define_class_under(rb_cEncoding, "CompatibilityError", rb_eEncodingError);
3427 rb_eNoMatchingPatternError = rb_define_class("NoMatchingPatternError", rb_eStandardError);
3428 rb_eNoMatchingPatternKeyError = rb_define_class("NoMatchingPatternKeyError", rb_eNoMatchingPatternError);
3429 rb_define_method(rb_eNoMatchingPatternKeyError, "initialize", no_matching_pattern_key_err_initialize, -1);
3430 rb_define_method(rb_eNoMatchingPatternKeyError, "matchee", no_matching_pattern_key_err_matchee, 0);
3431 rb_define_method(rb_eNoMatchingPatternKeyError, "key", no_matching_pattern_key_err_key, 0);
3433 syserr_tbl = st_init_numtable();
3434 rb_eSystemCallError = rb_define_class("SystemCallError", rb_eStandardError);
3435 rb_define_method(rb_eSystemCallError, "initialize", syserr_initialize, -1);
3436 rb_define_method(rb_eSystemCallError, "errno", syserr_errno, 0);
3437 rb_define_singleton_method(rb_eSystemCallError, "===", syserr_eqq, 1);
3439 rb_mErrno = rb_define_module("Errno");
3441 rb_mWarning = rb_define_module("Warning");
3442 rb_define_singleton_method(rb_mWarning, "[]", rb_warning_s_aref, 1);
3443 rb_define_singleton_method(rb_mWarning, "[]=", rb_warning_s_aset, 2);
3444 rb_define_singleton_method(rb_mWarning, "categories", rb_warning_s_categories, 0);
3445 rb_define_method(rb_mWarning, "warn", rb_warning_s_warn, -1);
3446 rb_extend_object(rb_mWarning, rb_mWarning);
3448 /* :nodoc: */
3449 rb_cWarningBuffer = rb_define_class_under(rb_mWarning, "buffer", rb_cString);
3450 rb_define_method(rb_cWarningBuffer, "write", warning_write, -1);
3452 id_cause = rb_intern_const("cause");
3453 id_message = rb_intern_const("message");
3454 id_detailed_message = rb_intern_const("detailed_message");
3455 id_backtrace = rb_intern_const("backtrace");
3456 id_key = rb_intern_const("key");
3457 id_matchee = rb_intern_const("matchee");
3458 id_args = rb_intern_const("args");
3459 id_receiver = rb_intern_const("receiver");
3460 id_private_call_p = rb_intern_const("private_call?");
3461 id_local_variables = rb_intern_const("local_variables");
3462 id_Errno = rb_intern_const("Errno");
3463 id_errno = rb_intern_const("errno");
3464 id_i_path = rb_intern_const("@path");
3465 id_warn = rb_intern_const("warn");
3466 id_category = rb_intern_const("category");
3467 id_deprecated = rb_intern_const("deprecated");
3468 id_experimental = rb_intern_const("experimental");
3469 id_performance = rb_intern_const("performance");
3470 id_top = rb_intern_const("top");
3471 id_bottom = rb_intern_const("bottom");
3472 id_iseq = rb_make_internal_id();
3473 id_recv = rb_make_internal_id();
3475 sym_category = ID2SYM(id_category);
3476 sym_highlight = ID2SYM(rb_intern_const("highlight"));
3478 warning_categories.id2enum = rb_init_identtable();
3479 st_add_direct(warning_categories.id2enum, id_deprecated, RB_WARN_CATEGORY_DEPRECATED);
3480 st_add_direct(warning_categories.id2enum, id_experimental, RB_WARN_CATEGORY_EXPERIMENTAL);
3481 st_add_direct(warning_categories.id2enum, id_performance, RB_WARN_CATEGORY_PERFORMANCE);
3483 warning_categories.enum2id = rb_init_identtable();
3484 st_add_direct(warning_categories.enum2id, RB_WARN_CATEGORY_NONE, 0);
3485 st_add_direct(warning_categories.enum2id, RB_WARN_CATEGORY_DEPRECATED, id_deprecated);
3486 st_add_direct(warning_categories.enum2id, RB_WARN_CATEGORY_EXPERIMENTAL, id_experimental);
3487 st_add_direct(warning_categories.enum2id, RB_WARN_CATEGORY_PERFORMANCE, id_performance);
3490 void
3491 rb_enc_raise(rb_encoding *enc, VALUE exc, const char *fmt, ...)
3493 va_list args;
3494 VALUE mesg;
3496 va_start(args, fmt);
3497 mesg = rb_enc_vsprintf(enc, fmt, args);
3498 va_end(args);
3500 rb_exc_raise(rb_exc_new3(exc, mesg));
3503 void
3504 rb_vraise(VALUE exc, const char *fmt, va_list ap)
3506 rb_exc_raise(rb_exc_new3(exc, rb_vsprintf(fmt, ap)));
3509 void
3510 rb_raise(VALUE exc, const char *fmt, ...)
3512 va_list args;
3513 va_start(args, fmt);
3514 rb_vraise(exc, fmt, args);
3515 va_end(args);
3518 NORETURN(static void raise_loaderror(VALUE path, VALUE mesg));
3520 static void
3521 raise_loaderror(VALUE path, VALUE mesg)
3523 VALUE err = rb_exc_new3(rb_eLoadError, mesg);
3524 rb_ivar_set(err, id_i_path, path);
3525 rb_exc_raise(err);
3528 void
3529 rb_loaderror(const char *fmt, ...)
3531 va_list args;
3532 VALUE mesg;
3534 va_start(args, fmt);
3535 mesg = rb_enc_vsprintf(rb_locale_encoding(), fmt, args);
3536 va_end(args);
3537 raise_loaderror(Qnil, mesg);
3540 void
3541 rb_loaderror_with_path(VALUE path, const char *fmt, ...)
3543 va_list args;
3544 VALUE mesg;
3546 va_start(args, fmt);
3547 mesg = rb_enc_vsprintf(rb_locale_encoding(), fmt, args);
3548 va_end(args);
3549 raise_loaderror(path, mesg);
3552 void
3553 rb_notimplement(void)
3555 rb_raise(rb_eNotImpError,
3556 "%"PRIsVALUE"() function is unimplemented on this machine",
3557 rb_id2str(rb_frame_this_func()));
3560 void
3561 rb_fatal(const char *fmt, ...)
3563 va_list args;
3564 VALUE mesg;
3566 if (! ruby_thread_has_gvl_p()) {
3567 /* The thread has no GVL. Object allocation impossible (cant run GC),
3568 * thus no message can be printed out. */
3569 fprintf(stderr, "[FATAL] rb_fatal() outside of GVL\n");
3570 rb_print_backtrace(stderr);
3571 die();
3574 va_start(args, fmt);
3575 mesg = rb_vsprintf(fmt, args);
3576 va_end(args);
3578 rb_exc_fatal(rb_exc_new3(rb_eFatal, mesg));
3581 static VALUE
3582 make_errno_exc(const char *mesg)
3584 int n = errno;
3586 errno = 0;
3587 if (n == 0) {
3588 rb_bug("rb_sys_fail(%s) - errno == 0", mesg ? mesg : "");
3590 return rb_syserr_new(n, mesg);
3593 static VALUE
3594 make_errno_exc_str(VALUE mesg)
3596 int n = errno;
3598 errno = 0;
3599 if (!mesg) mesg = Qnil;
3600 if (n == 0) {
3601 const char *s = !NIL_P(mesg) ? RSTRING_PTR(mesg) : "";
3602 rb_bug("rb_sys_fail_str(%s) - errno == 0", s);
3604 return rb_syserr_new_str(n, mesg);
3607 VALUE
3608 rb_syserr_new(int n, const char *mesg)
3610 VALUE arg;
3611 arg = mesg ? rb_str_new2(mesg) : Qnil;
3612 return rb_syserr_new_str(n, arg);
3615 VALUE
3616 rb_syserr_new_str(int n, VALUE arg)
3618 return rb_class_new_instance(1, &arg, get_syserr(n));
3621 void
3622 rb_syserr_fail(int e, const char *mesg)
3624 rb_exc_raise(rb_syserr_new(e, mesg));
3627 void
3628 rb_syserr_fail_str(int e, VALUE mesg)
3630 rb_exc_raise(rb_syserr_new_str(e, mesg));
3633 #undef rb_sys_fail
3634 void
3635 rb_sys_fail(const char *mesg)
3637 rb_exc_raise(make_errno_exc(mesg));
3640 #undef rb_sys_fail_str
3641 void
3642 rb_sys_fail_str(VALUE mesg)
3644 rb_exc_raise(make_errno_exc_str(mesg));
3647 #ifdef RUBY_FUNCTION_NAME_STRING
3648 void
3649 rb_sys_fail_path_in(const char *func_name, VALUE path)
3651 int n = errno;
3653 errno = 0;
3654 rb_syserr_fail_path_in(func_name, n, path);
3657 void
3658 rb_syserr_fail_path_in(const char *func_name, int n, VALUE path)
3660 rb_exc_raise(rb_syserr_new_path_in(func_name, n, path));
3663 VALUE
3664 rb_syserr_new_path_in(const char *func_name, int n, VALUE path)
3666 VALUE args[2];
3668 if (!path) path = Qnil;
3669 if (n == 0) {
3670 const char *s = !NIL_P(path) ? RSTRING_PTR(path) : "";
3671 if (!func_name) func_name = "(null)";
3672 rb_bug("rb_sys_fail_path_in(%s, %s) - errno == 0",
3673 func_name, s);
3675 args[0] = path;
3676 args[1] = rb_str_new_cstr(func_name);
3677 return rb_class_new_instance(2, args, get_syserr(n));
3679 #endif
3681 NORETURN(static void rb_mod_exc_raise(VALUE exc, VALUE mod));
3683 static void
3684 rb_mod_exc_raise(VALUE exc, VALUE mod)
3686 rb_extend_object(exc, mod);
3687 rb_exc_raise(exc);
3690 void
3691 rb_mod_sys_fail(VALUE mod, const char *mesg)
3693 VALUE exc = make_errno_exc(mesg);
3694 rb_mod_exc_raise(exc, mod);
3697 void
3698 rb_mod_sys_fail_str(VALUE mod, VALUE mesg)
3700 VALUE exc = make_errno_exc_str(mesg);
3701 rb_mod_exc_raise(exc, mod);
3704 void
3705 rb_mod_syserr_fail(VALUE mod, int e, const char *mesg)
3707 VALUE exc = rb_syserr_new(e, mesg);
3708 rb_mod_exc_raise(exc, mod);
3711 void
3712 rb_mod_syserr_fail_str(VALUE mod, int e, VALUE mesg)
3714 VALUE exc = rb_syserr_new_str(e, mesg);
3715 rb_mod_exc_raise(exc, mod);
3718 static void
3719 syserr_warning(VALUE mesg, int err)
3721 rb_str_set_len(mesg, RSTRING_LEN(mesg)-1);
3722 rb_str_catf(mesg, ": %s\n", strerror(err));
3723 rb_write_warning_str(mesg);
3726 #if 0
3727 void
3728 rb_sys_warn(const char *fmt, ...)
3730 if (!NIL_P(ruby_verbose)) {
3731 int errno_save = errno;
3732 with_warning_string(mesg, 0, fmt) {
3733 syserr_warning(mesg, errno_save);
3735 errno = errno_save;
3739 void
3740 rb_syserr_warn(int err, const char *fmt, ...)
3742 if (!NIL_P(ruby_verbose)) {
3743 with_warning_string(mesg, 0, fmt) {
3744 syserr_warning(mesg, err);
3749 void
3750 rb_sys_enc_warn(rb_encoding *enc, const char *fmt, ...)
3752 if (!NIL_P(ruby_verbose)) {
3753 int errno_save = errno;
3754 with_warning_string(mesg, enc, fmt) {
3755 syserr_warning(mesg, errno_save);
3757 errno = errno_save;
3761 void
3762 rb_syserr_enc_warn(int err, rb_encoding *enc, const char *fmt, ...)
3764 if (!NIL_P(ruby_verbose)) {
3765 with_warning_string(mesg, enc, fmt) {
3766 syserr_warning(mesg, err);
3770 #endif
3772 void
3773 rb_sys_warning(const char *fmt, ...)
3775 if (RTEST(ruby_verbose)) {
3776 int errno_save = errno;
3777 with_warning_string(mesg, 0, fmt) {
3778 syserr_warning(mesg, errno_save);
3780 errno = errno_save;
3784 #if 0
3785 void
3786 rb_syserr_warning(int err, const char *fmt, ...)
3788 if (RTEST(ruby_verbose)) {
3789 with_warning_string(mesg, 0, fmt) {
3790 syserr_warning(mesg, err);
3794 #endif
3796 void
3797 rb_sys_enc_warning(rb_encoding *enc, const char *fmt, ...)
3799 if (RTEST(ruby_verbose)) {
3800 int errno_save = errno;
3801 with_warning_string(mesg, enc, fmt) {
3802 syserr_warning(mesg, errno_save);
3804 errno = errno_save;
3808 void
3809 rb_syserr_enc_warning(int err, rb_encoding *enc, const char *fmt, ...)
3811 if (RTEST(ruby_verbose)) {
3812 with_warning_string(mesg, enc, fmt) {
3813 syserr_warning(mesg, err);
3818 void
3819 rb_load_fail(VALUE path, const char *err)
3821 VALUE mesg = rb_str_buf_new_cstr(err);
3822 rb_str_cat2(mesg, " -- ");
3823 rb_str_append(mesg, path); /* should be ASCII compatible */
3824 raise_loaderror(path, mesg);
3827 void
3828 rb_error_frozen(const char *what)
3830 rb_raise(rb_eFrozenError, "can't modify frozen %s", what);
3833 void
3834 rb_frozen_error_raise(VALUE frozen_obj, const char *fmt, ...)
3836 va_list args;
3837 VALUE exc, mesg;
3839 va_start(args, fmt);
3840 mesg = rb_vsprintf(fmt, args);
3841 va_end(args);
3842 exc = rb_exc_new3(rb_eFrozenError, mesg);
3843 rb_ivar_set(exc, id_recv, frozen_obj);
3844 rb_exc_raise(exc);
3847 static VALUE
3848 inspect_frozen_obj(VALUE obj, VALUE mesg, int recur)
3850 if (recur) {
3851 rb_str_cat_cstr(mesg, " ...");
3853 else {
3854 rb_str_append(mesg, rb_inspect(obj));
3856 return mesg;
3859 void
3860 rb_error_frozen_object(VALUE frozen_obj)
3862 rb_yjit_lazy_push_frame(GET_EC()->cfp->pc);
3864 if (CHILLED_STRING_P(frozen_obj)) {
3865 CHILLED_STRING_MUTATED(frozen_obj);
3866 return;
3869 VALUE debug_info;
3870 const ID created_info = id_debug_created_info;
3871 VALUE mesg = rb_sprintf("can't modify frozen %"PRIsVALUE": ",
3872 CLASS_OF(frozen_obj));
3873 VALUE exc = rb_exc_new_str(rb_eFrozenError, mesg);
3875 rb_ivar_set(exc, id_recv, frozen_obj);
3876 rb_exec_recursive(inspect_frozen_obj, frozen_obj, mesg);
3878 if (!NIL_P(debug_info = rb_attr_get(frozen_obj, created_info))) {
3879 VALUE path = rb_ary_entry(debug_info, 0);
3880 VALUE line = rb_ary_entry(debug_info, 1);
3882 rb_str_catf(mesg, ", created at %"PRIsVALUE":%"PRIsVALUE, path, line);
3884 rb_exc_raise(exc);
3887 #undef rb_check_frozen
3888 void
3889 rb_check_frozen(VALUE obj)
3891 rb_check_frozen_internal(obj);
3894 void
3895 rb_check_copyable(VALUE obj, VALUE orig)
3897 if (!FL_ABLE(obj)) return;
3898 rb_check_frozen_internal(obj);
3899 if (!FL_ABLE(orig)) return;
3902 void
3903 Init_syserr(void)
3905 rb_eNOERROR = setup_syserr(0, "NOERROR");
3906 #if 0
3907 /* No error */
3908 rb_define_const(rb_mErrno, "NOERROR", rb_eNOERROR);
3909 #endif
3910 #define defined_error(name, num) set_syserr((num), (name));
3911 #define undefined_error(name) rb_define_const(rb_mErrno, (name), rb_eNOERROR);
3912 #include "known_errors.inc"
3913 #undef defined_error
3914 #undef undefined_error
3917 #include "warning.rbinc"
3920 * \}