Skip to content
This repository
Newer
Older
100644 180 lines (144 sloc) 4.362 kb
a1704be0 »
2008-10-01 Rework exceptions in the VM.
1 #include "builtin/exception.hpp"
2
3 #include "vm.hpp"
096c8b6c »
2008-08-15 Add ability to have C++ backtraces.
4 #include "prelude.hpp"
53f9ee13 »
2008-08-23 Moved C++ exceptions classes to their own file.
5 #include "exception.hpp"
612f8a9d »
2008-10-20 Make execinfo (and backtrace) optional
6 #include "detection.hpp"
a1704be0 »
2008-10-01 Rework exceptions in the VM.
7
af099dd7 »
2009-03-20 Finish removing bytes_ from ObjectHeader
8 #include "builtin/string.hpp"
9 #include "builtin/class.hpp"
10 #include "builtin/symbol.hpp"
11
0489e93f »
2011-01-11 Always use #include <xxx.h> rather than <cxxx>.
12 #include <ctype.h>
096c8b6c »
2008-08-15 Add ability to have C++ backtraces.
13 #include <vector>
14 #include <iostream>
15 #include <cxxabi.h>
0489e93f »
2011-01-11 Always use #include <xxx.h> rather than <cxxx>.
16 #include <assert.h>
096c8b6c »
2008-08-15 Add ability to have C++ backtraces.
17
612f8a9d »
2008-10-20 Make execinfo (and backtrace) optional
18 #ifdef USE_EXECINFO
19 #include <execinfo.h>
20 #endif
21
096c8b6c »
2008-08-15 Add ability to have C++ backtraces.
22 namespace rubinius {
23
1fdd151d »
2008-10-19 Changed all OBJECT, SYMBOL, FIXNUM, INTEGER to normal syntax.
24 void TypeError::raise(object_type type, Object* obj, const char* reason) {
2adb68af »
2009-02-17 throw and catch TypeError
25 throw TypeError(type, obj, reason);
31ad5c22 »
2009-01-05 Introduces the dynamic interpreter
26 // Not reached.
cbeddb89 »
2008-08-24 Added ArgumentError::raise and other raise tests.
27 }
28
a1704be0 »
2008-10-01 Rework exceptions in the VM.
29 void Assertion::raise(const char* reason) {
3ec6b24e »
2009-03-12 The return of the C++ tests
30 throw Assertion(reason);
31ad5c22 »
2009-01-05 Introduces the dynamic interpreter
31 // Not reached.
8a25128b »
2008-08-21 Clean up exception usage.
32 }
33
a1704be0 »
2008-10-01 Rework exceptions in the VM.
34 RubyException::RubyException(Exception* exception, bool make_backtrace)
35 : VMException(make_backtrace), exception(exception) {
d0cb4be1 »
2008-08-24 Added ArgumentError::raise taking a reason.
36 }
37
a1704be0 »
2008-10-01 Rework exceptions in the VM.
38 void RubyException::raise(Exception* exception, bool make_backtrace) {
12aeac9a »
2009-02-12 Fix return-in-ensure, a stack problem, and more
39 throw RubyException(exception, make_backtrace);
31ad5c22 »
2009-01-05 Introduces the dynamic interpreter
40 // Not reached.
8a25128b »
2008-08-21 Clean up exception usage.
41 }
42
a1704be0 »
2008-10-01 Rework exceptions in the VM.
43 void RubyException::show(STATE) {
63ae26da »
2012-02-24 Fixed Module#name.
44 std::cout << exception->message_c_str(state) <<
45 " (" <<
46 exception->class_object(state)->debug_str(state) <<
47 ") \n";
a1704be0 »
2008-10-01 Rework exceptions in the VM.
48 print_backtrace();
8a25128b »
2008-08-21 Clean up exception usage.
49 }
50
f7c05682 »
2009-11-18 Expose backtrace max level in rubinius::print_backtrace
51 static VMException::Backtrace get_trace(size_t skip, size_t max_depth=100) {
612f8a9d »
2008-10-20 Make execinfo (and backtrace) optional
52 #ifdef USE_EXECINFO
096c8b6c »
2008-08-15 Add ability to have C++ backtraces.
53 size_t stack_depth;
54 char **stack_strings;
55
6f92669c »
2010-05-22 Initial work to enable compiling with clang
56 void **stack_addrs = (void **) calloc(max_depth, sizeof(void*));
57
096c8b6c »
2008-08-15 Add ability to have C++ backtraces.
58 stack_depth = backtrace(stack_addrs, max_depth);
59 stack_strings = backtrace_symbols(stack_addrs, stack_depth);
60
61 VMException::Backtrace s;
62
63 for (size_t i = skip; i < stack_depth; i++) {
64 s.push_back(std::string(stack_strings[i]));
65 }
66 free(stack_strings); // malloc()ed by backtrace_symbols
6f92669c »
2010-05-22 Initial work to enable compiling with clang
67 free(stack_addrs);
612f8a9d »
2008-10-20 Make execinfo (and backtrace) optional
68 #else
69 VMException::Backtrace s;
70 s.push_back(std::string("C++ backtrace not available"));
71 #endif
096c8b6c »
2008-08-15 Add ability to have C++ backtraces.
72
73 return s;
74 }
75
c25aac53 »
2009-01-16 Added alpha stage to bootstrap process.
76 static void squeeze_space(std::string& str) {
77 std::string ws = " ";
78
79 std::string::size_type pos = str.find(ws);
80 if(pos == std::string::npos) return;
81
82 std::string::size_type i = pos + 4;
83 while(std::isspace(str[i])) i++;
84 str.erase(pos + 2, i - pos - 2);
85 }
096c8b6c »
2008-08-15 Add ability to have C++ backtraces.
86
87 static void demangle(VMException::Backtrace& s) {
88 for (size_t i = 0; i < s.size(); i++) {
c25aac53 »
2009-01-16 Added alpha stage to bootstrap process.
89 squeeze_space(s[i]);
096c8b6c »
2008-08-15 Add ability to have C++ backtraces.
90 const char* str = s[i].c_str();
91 const char* pos = strstr(str, " _Z");
92 /* Found a mangle. */
93 if(pos) {
94 size_t sz = 1024;
95 char *cpp_name = 0;
96 char* name = strdup(pos + 1);
97 char* end = strstr(name, " + ");
98 *end = 0;
99
100 int status;
101 cpp_name = abi::__cxa_demangle(name, cpp_name, &sz, &status);
102
103 if(!status) {
104 std::string full_cpp = std::string(str, pos - str) + " " + cpp_name +
105 " " + (++end);
106 s[i] = full_cpp;
107 }
108 if(cpp_name) free(cpp_name);
109 free(name);
110 }
111 }
112 }
113
a1249250 »
2011-04-08 Add additional debugging logic for world states
114
115 static bool hard_abort = true;
116
096c8b6c »
2008-08-15 Add ability to have C++ backtraces.
117 void abort() {
118 std::cout << "Abort!" << std::endl;
119 print_backtrace();
a1249250 »
2011-04-08 Add additional debugging logic for world states
120 if(hard_abort) ::abort();
096c8b6c »
2008-08-15 Add ability to have C++ backtraces.
121 }
122
f9e3f53b »
2009-10-22 Introduce and use rubinius::bug()
123 void bug(const char* message) {
124 std::cerr << "[BUG: " << message << "]\n";
125 print_backtrace();
126 ::abort();
127 }
128
67c963ad »
2009-10-29 rubinius::bug() API for errors with args
129 void bug(const char* message, const char* arg) {
130 std::cerr << "[BUG: " << message
131 << ": " << arg
132 << "]\n";
133 print_backtrace();
134 ::abort();
135 }
136
2ad86215 »
2011-10-20 Add rubinius::warn(msg)
137 void warn(const char* message) {
138 std::cerr << "[WARNING: " << message << "]\n";
139 print_backtrace();
140 }
141
f7c05682 »
2009-11-18 Expose backtrace max level in rubinius::print_backtrace
142 void print_backtrace(size_t max) {
143 VMException::Backtrace s = get_trace(2, max);
096c8b6c »
2008-08-15 Add ability to have C++ backtraces.
144 demangle(s);
145
146 for(size_t i = 0; i < s.size(); i++) {
147 std::cout << s[i] << std::endl;
148 }
149 }
150
151 static VMException::Backtrace get_cpp_backtrace() {
152 VMException::Backtrace s = get_trace(2);
153 demangle(s);
154 return s;
155 }
156
a1704be0 »
2008-10-01 Rework exceptions in the VM.
157 VMException::VMException(bool make_backtrace)
158 : backtrace(NULL), reason(NULL) {
159 if(make_backtrace) {
160 backtrace = new VMException::Backtrace(get_cpp_backtrace());
161 }
096c8b6c »
2008-08-15 Add ability to have C++ backtraces.
162 }
163
a1704be0 »
2008-10-01 Rework exceptions in the VM.
164 VMException::VMException(const char* reason, bool make_backtrace)
165 : backtrace(NULL), reason(NULL) {
166 if(make_backtrace) {
167 backtrace = new VMException::Backtrace(get_cpp_backtrace());
168 }
f7c9c157 »
2008-09-30 Some cleanups for VMException and subclasses.
169 if(reason) this->reason = strdup(reason);
170 }
171
096c8b6c »
2008-08-15 Add ability to have C++ backtraces.
172 void VMException::print_backtrace() {
173 if(!backtrace) return;
174
175 for(size_t i = 0; i < backtrace->size(); i++) {
176 std::cout << backtrace->at(i) << std::endl;
177 }
178 }
179 }
Something went wrong with that request. Please try again.