]> gitweb.factorcode.org Git - factor.git/blob - vm/errors.cpp
5e27ffd24c55501e21b0ebecf7f6de9f34803816
[factor.git] / vm / errors.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 void fatal_error(const char *msg, cell tagged)
7 {
8         std::cout << "fatal_error: " << msg;
9         std::cout << ": " << std::hex << tagged << std::dec;
10         std::cout << std::endl;
11         exit(1);
12 }
13
14 void critical_error(const char *msg, cell tagged)
15 {
16         std::cout << "You have triggered a bug in Factor. Please report.\n";
17         std::cout << "critical_error: " << msg;
18         std::cout << ": " << std::hex << tagged << std::dec;
19         std::cout << std::endl;
20         current_vm()->factorbug();
21 }
22
23 void out_of_memory()
24 {
25         std::cout << "Out of memory\n\n";
26         current_vm()->dump_generations();
27         exit(1);
28 }
29
30 void factor_vm::general_error(vm_error_type error, cell arg1, cell arg2)
31 {
32         faulting_p = true;
33
34         /* Reset local roots before allocating anything */
35         data_roots.clear();
36         bignum_roots.clear();
37         code_roots.clear();
38
39         /* If we had an underflow or overflow, data or retain stack
40         pointers might be out of bounds, so fix them before allocating
41         anything */
42         ctx->fix_stacks();
43
44         /* If error was thrown during heap scan, we re-enable the GC */
45         gc_off = false;
46
47         /* If the error handler is set, we rewind any C stack frames and
48         pass the error to user-space. */
49         if(!current_gc && to_boolean(special_objects[ERROR_HANDLER_QUOT]))
50         {
51 #ifdef FACTOR_DEBUG
52                 /* Doing a GC here triggers all kinds of funny errors */
53                 primitive_compact_gc();
54 #endif
55
56                 /* Now its safe to allocate and GC */
57                 cell error_object = allot_array_4(special_objects[OBJ_ERROR],
58                         tag_fixnum(error),arg1,arg2);
59
60                 ctx->push(error_object);
61
62                 faulting_p = false;
63                 unwind_native_frames(special_objects[ERROR_HANDLER_QUOT],
64                         ctx->callstack_top);
65         }
66         /* Error was thrown in early startup before error handler is set, just
67         crash. */
68         else
69         {
70                 std::cout << "You have triggered a bug in Factor. Please report.\n";
71                 std::cout << "error: " << error << std::endl;
72                 std::cout << "arg 1: "; print_obj(arg1); std::cout << std::endl;
73                 std::cout << "arg 2: "; print_obj(arg2); std::cout << std::endl;
74                 faulting_p = false;
75                 factorbug();
76         }
77 }
78
79 void factor_vm::type_error(cell type, cell tagged)
80 {
81         general_error(ERROR_TYPE,tag_fixnum(type),tagged);
82 }
83
84 void factor_vm::not_implemented_error()
85 {
86         general_error(ERROR_NOT_IMPLEMENTED,false_object,false_object);
87 }
88
89 void factor_vm::memory_protection_error(cell addr)
90 {
91         if(code->safepoint_p(addr))
92                 handle_safepoint();
93         else if(faulting_p)
94                 fatal_error("Double fault", 0);
95         else if(ctx->datastack_seg->underflow_p(addr))
96                 general_error(ERROR_DATASTACK_UNDERFLOW,false_object,false_object);
97         else if(ctx->datastack_seg->overflow_p(addr))
98                 general_error(ERROR_DATASTACK_OVERFLOW,false_object,false_object);
99         else if(ctx->retainstack_seg->underflow_p(addr))
100                 general_error(ERROR_RETAINSTACK_UNDERFLOW,false_object,false_object);
101         else if(ctx->retainstack_seg->overflow_p(addr))
102                 general_error(ERROR_RETAINSTACK_OVERFLOW,false_object,false_object);
103         else if(ctx->callstack_seg->underflow_p(addr))
104                 general_error(ERROR_CALLSTACK_OVERFLOW,false_object,false_object);
105         else if(ctx->callstack_seg->overflow_p(addr))
106                 general_error(ERROR_CALLSTACK_UNDERFLOW,false_object,false_object);
107         else
108                 general_error(ERROR_MEMORY,from_unsigned_cell(addr),false_object);
109 }
110
111 void factor_vm::signal_error(cell signal)
112 {
113         general_error(ERROR_SIGNAL,from_unsigned_cell(signal),false_object);
114 }
115
116 void factor_vm::divide_by_zero_error()
117 {
118         general_error(ERROR_DIVIDE_BY_ZERO,false_object,false_object);
119 }
120
121 void factor_vm::fp_trap_error(unsigned int fpu_status)
122 {
123         general_error(ERROR_FP_TRAP,tag_fixnum(fpu_status),false_object);
124 }
125
126 /* For testing purposes */
127 void factor_vm::primitive_unimplemented()
128 {
129         not_implemented_error();
130 }
131
132 void factor_vm::memory_signal_handler_impl()
133 {
134         memory_protection_error(signal_fault_addr);
135         if (!signal_resumable)
136         {
137                 /* In theory we should only get here if the callstack overflowed during a
138                 safepoint */
139                 general_error(ERROR_CALLSTACK_OVERFLOW,false_object,false_object);
140         }
141 }
142
143 void memory_signal_handler_impl()
144 {
145         current_vm()->memory_signal_handler_impl();
146 }
147
148 void factor_vm::synchronous_signal_handler_impl()
149 {
150         signal_error(signal_number);
151 }
152
153 void synchronous_signal_handler_impl()
154 {
155         current_vm()->synchronous_signal_handler_impl();
156 }
157
158 void factor_vm::fp_signal_handler_impl()
159 {
160         /* Clear pending exceptions to avoid getting stuck in a loop */
161         set_fpu_state(get_fpu_state());
162
163         fp_trap_error(signal_fpu_status);
164 }
165
166 void fp_signal_handler_impl()
167 {
168         current_vm()->fp_signal_handler_impl();
169 }
170
171 void factor_vm::enqueue_safepoint_fep()
172 {
173         if (fep_p)
174                 fatal_error("Low-level debugger interrupted", 0);
175         atomic::store(&safepoint_fep_p, true);
176         code->guard_safepoint();
177 }
178
179 void factor_vm::handle_safepoint()
180 {
181         code->unguard_safepoint();
182         if (atomic::load(&safepoint_fep_p))
183         {
184                 if (atomic::load(&sampling_profiler_p))
185                         end_sampling_profiler();
186                 std::cout << "Interrupted\n";
187                 factorbug();
188                 atomic::store(&safepoint_fep_p, false);
189         }
190         else if (sampling_profiler_p)
191                 record_sample();
192 }
193
194 }