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