]> gitweb.factorcode.org Git - factor.git/blob - basis/debugger/debugger.factor
VM: removes the OBJ-ERROR special object in favor of a constant
[factor.git] / basis / debugger / debugger.factor
1 ! Copyright (C) 2004, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.strings arrays assocs classes
4 classes.builtin classes.mixin classes.tuple classes.tuple.parser
5 combinators combinators.short-circuit compiler.errors
6 compiler.units continuations definitions destructors
7 effects.parser fry generic generic.math generic.parser
8 generic.single grouping io io.encodings io.styles kernel
9 kernel.private lexer libc make math math.order math.parser
10 math.ratios namespaces parser prettyprint sequences
11 sequences.private slots source-files.errors strings
12 strings.parser summary system vocabs vocabs.loader vocabs.parser
13 words ;
14 IN: debugger
15
16 GENERIC: error-help ( error -- topic )
17
18 M: object error-help drop f ;
19
20 M: tuple error-help class-of ;
21
22 M: source-file-error error-help error>> error-help ;
23
24 GENERIC: error. ( error -- )
25
26 M: object error. short. ;
27
28 M: string error. print ;
29
30 : traceback-link. ( continuation -- )
31     "[" write [ "Traceback" ] dip write-object "]" print ;
32
33 : :s ( -- )
34     error-continuation get data>> stack. ;
35
36 : :r ( -- )
37     error-continuation get retain>> stack. ;
38
39 : :c ( -- )
40     error-continuation get call>> callstack. ;
41
42 : :get ( variable -- value )
43     error-continuation get name>> assoc-stack ;
44
45 : :res ( n -- * )
46     1 - restarts [ nth f ] change-global continue-restart ;
47
48 : :1 ( -- * ) 1 :res ;
49 : :2 ( -- * ) 2 :res ;
50 : :3 ( -- * ) 3 :res ;
51
52 : restart. ( restart n -- )
53     [
54         1 + dup 3 <= [ ":" % # "      " % ] [ # " :res  " % ] if
55         name>> %
56     ] "" make print ;
57
58 : restarts. ( -- )
59     restarts get [
60         nl
61         "The following restarts are available:" print
62         nl
63         [ restart. ] each-index
64     ] unless-empty ;
65
66 : print-error ( error -- )
67     [ error. flush ] curry
68     [ [ "Error in print-error!" print drop ] with-global ]
69     recover ;
70
71 : :error ( -- )
72     error get print-error ;
73
74 : print-error-and-restarts ( error -- )
75     print-error
76     restarts.
77     nl
78     "Type :help for debugging help." print flush ;
79
80 : try ( quot -- )
81     [ print-error-and-restarts ] recover ; inline
82
83 : expired-error. ( obj -- )
84     "Object did not survive image save/load: " write third . ;
85
86 : io-error. ( error -- )
87     "I/O error #" write third [ . ] [ strerror print ] bi ;
88
89 : type-check-error. ( obj -- )
90     "Type check error" print
91     "Object: " write dup fourth short.
92     "Object type: " write dup fourth class-of .
93     "Expected type: " write third type>class . ;
94
95 : divide-by-zero-error. ( obj -- )
96     "Division by zero" print drop ;
97
98 HOOK: signal-error. os ( obj -- )
99
100 : array-size-error. ( obj -- )
101     "Invalid array size: " write dup third .
102     "Maximum: " write fourth 1 - . ;
103
104 : fixnum-range-error. ( obj -- )
105     "Cannot convert to fixnum: " write third . ;
106
107 : ffi-error. ( obj -- )
108     "FFI error" print drop ;
109
110 : find-ffi-error ( string -- error )
111     [ linkage-errors get ] dip
112     '[ nip asset>> name>> _ = ] assoc-find drop nip
113     [ error>> message>> ] [ "none" ] if* ;
114
115 : undefined-symbol-error. ( obj -- )
116     "Cannot resolve C library function" print
117     "Library: " write dup fourth .
118     third symbol>string
119     [ "Symbol: " write print ]
120     [ "DlError: " write find-ffi-error print ] bi
121     "See http://concatenative.org/wiki/view/Factor/Requirements" print ;
122
123 : stack-underflow. ( obj name -- )
124     write " stack underflow" print drop ;
125
126 : stack-overflow. ( obj name -- )
127     write " stack overflow" print drop ;
128
129 : datastack-underflow. ( obj -- ) "Data" stack-underflow. ;
130 : datastack-overflow. ( obj -- ) "Data" stack-overflow. ;
131 : retainstack-underflow. ( obj -- ) "Retain" stack-underflow. ;
132 : retainstack-overflow. ( obj -- ) "Retain" stack-overflow. ;
133 : callstack-underflow. ( obj -- ) "Call" stack-underflow. ;
134 : callstack-overflow. ( obj -- ) "Call" stack-overflow. ;
135
136 : memory-error. ( error -- )
137     "Memory protection fault at address " write third .h ;
138
139 : primitive-error. ( error -- )
140     "Unimplemented primitive" print drop ;
141
142 : fp-trap-error. ( error -- )
143     "Floating point trap" print drop ;
144
145 : interrupt-error. ( error -- )
146     "Interrupt" print drop ;
147
148 : callback-space-overflow. ( error -- )
149     "Callback space overflow" print drop ;
150
151 PREDICATE: vm-error < array
152     dup length 2 < [ drop f ] [
153         {
154             [ first-unsafe KERNEL-ERROR = ]
155             [ second-unsafe 0 kernel-error-count 1 - between? ]
156         } 1&&
157     ] if ;
158
159 : vm-errors ( error -- n errors )
160     second {
161         [ expired-error.           ]
162         [ io-error.                ]
163         [ primitive-error.         ]
164         [ type-check-error.        ]
165         [ divide-by-zero-error.    ]
166         [ signal-error.            ]
167         [ array-size-error.        ]
168         [ fixnum-range-error.      ]
169         [ ffi-error.               ]
170         [ undefined-symbol-error.  ]
171         [ datastack-underflow.     ]
172         [ datastack-overflow.      ]
173         [ retainstack-underflow.   ]
174         [ retainstack-overflow.    ]
175         [ callstack-underflow.     ]
176         [ callstack-overflow.      ]
177         [ memory-error.            ]
178         [ fp-trap-error.           ]
179         [ interrupt-error.         ]
180         [ callback-space-overflow. ]
181     } ; inline
182
183 M: vm-error summary drop "VM error" ;
184
185 M: vm-error error. dup vm-errors dispatch ;
186
187 M: vm-error error-help vm-errors nth first ;
188
189 M: division-by-zero summary
190     drop "Division by zero" ;
191
192 M: no-method summary
193     drop "No suitable method" ;
194
195 M: no-method error.
196     "Generic word " write
197     dup generic>> pprint
198     " does not define a method for the " write
199     dup object>> class-of pprint
200     " class." print
201     "Dispatching on object: " write object>> short. ;
202
203 M: bad-slot-value summary drop "Bad store to specialized slot" ;
204
205 M: bad-slot-name summary drop "Bad slot name in object literal" ;
206
207 M: bad-vocab-name summary drop "Vocab name cannot contain \":/\\ \"" ;
208
209 M: no-math-method summary
210     drop "No suitable arithmetic method" ;
211
212 M: no-next-method summary
213     drop "Executing call-next-method from least-specific method" ;
214
215 M: inconsistent-next-method summary
216     drop "Executing call-next-method with inconsistent parameters" ;
217
218 M: check-method-error summary
219     drop "Invalid parameters for create-method" ;
220
221 M: not-a-tuple summary
222     drop "Not a tuple" ;
223
224 M: bad-superclass summary
225     drop "Tuple classes can only inherit from non-final tuple classes" ;
226
227 M: bad-initial-value summary
228     drop "Incompatible initial value" ;
229
230 M: no-cond summary
231     drop "Fall-through in cond" ;
232
233 M: no-case summary
234     drop "Fall-through in case" ;
235
236 M: slice-error summary
237     "Cannot create slice" swap {
238         { [ dup from>> 0 < ] [ ": from < 0" ] }
239         { [ dup [ to>> ] [ seq>> length ] bi > ] [ ": to > length" ] }
240         { [ dup [ from>> ] [ to>> ] bi > ] [ ": from > to" ] }
241         [ f ]
242     } cond nip append ;
243
244 M: bounds-error summary drop "Sequence index out of bounds" ;
245
246 M: groups-error summary drop "Non positive group size" ;
247
248 M: condition error. error>> error. ;
249
250 M: condition summary error>> summary ;
251
252 M: condition error-help error>> error-help ;
253
254 M: assert summary drop "Assertion failed" ;
255
256 M: assert-sequence summary drop "Assertion failed" ;
257
258 M: assert-sequence error.
259     standard-table-style [
260         [ "=== Expected:" print expected>> stack. ]
261         [ "=== Got:" print got>> stack. ] bi
262     ] tabular-output ;
263
264 M: immutable summary drop "Sequence is immutable" ;
265
266 M: redefine-error error.
267     "Re-definition of " write
268     def>> . ;
269
270 M: undefined-word summary
271     word>> undefined-word?
272     "Cannot execute a deferred word before it has been defined"
273     "Cannot execute a word before it has been compiled"
274     ? ;
275
276 M: no-compilation-unit error.
277     "Attempting to define " write
278     definition>> pprint
279     " outside of a compilation unit" print ;
280
281 M: no-vocab summary
282     drop "Vocabulary does not exist" ;
283
284 M: encode-error summary drop "Character encoding error" ;
285
286 M: decode-error summary drop "Character decoding error" ;
287
288 M: bad-create summary drop "Bad parameters to create" ;
289
290 M: cannot-be-inline summary drop "This type of word cannot be inlined" ;
291
292 M: attempt-all-error summary drop "Nothing to attempt" ;
293
294 M: already-disposed summary drop "Attempting to operate on disposed object" ;
295
296 M: no-current-vocab-error summary
297     drop "Not in a vocabulary; IN: form required" ;
298
299 M: no-word-error summary
300     name>>
301     "No word named “"
302     "” found in current vocabulary search path" surround ;
303
304 M: no-word-error error. summary print ;
305
306 M: no-word-in-vocab summary
307     [ vocab>> ] [ word>> ] bi
308     [ "No word named “" % % "” found in “" % % "” vocabulary" % ] "" make ;
309
310 M: no-word-in-vocab error. summary print ;
311
312 M: ambiguous-use-error summary
313     words>> first name>>
314     "More than one vocabulary defines a word named “" "”" surround ;
315
316 M: ambiguous-use-error error. summary print ;
317
318 M: staging-violation summary
319     drop
320     "A parsing word cannot be used in the same file it is defined in." ;
321
322 M: bad-number summary
323     drop "Bad number literal" ;
324
325 M: duplicate-slot-names summary
326     drop "Duplicate slot names" ;
327
328 M: invalid-slot-name summary
329     drop "Invalid slot name" ;
330
331 M: bad-inheritance summary
332     drop "Circularity in inheritance chain" ;
333
334 M: not-in-a-method-error summary
335     drop "call-next-method can only be called in a method definition" ;
336
337 M: version-control-merge-conflict summary
338     drop "Version control merge conflict in source code" ;
339
340 GENERIC: expected>string ( obj -- str )
341
342 M: f expected>string drop "end of input" ;
343 M: word expected>string name>> ;
344 M: string expected>string ;
345
346 M: unexpected error.
347     "Expected " write
348     dup want>> expected>string write
349     " but got " write
350     got>> expected>string print ;
351
352 M: lexer-error error.
353     [ lexer-dump ] [ error>> error. ] bi ;
354
355 M: lexer-error summary
356     error>> summary ;
357
358 M: lexer-error compute-restarts
359     error>> compute-restarts ;
360
361 M: lexer-error error-help
362     error>> error-help ;
363
364 M: bad-effect summary
365     drop "Bad stack effect declaration" ;
366
367 M: invalid-row-variable summary
368     drop "Stack effect row variables can only occur as the first input or output" ;
369
370 M: row-variable-can't-have-type summary
371     drop "Stack effect row variables cannot have a declared type" ;
372
373 M: bad-escape error.
374     "Bad escape code: \\" write char>> write nl ;
375
376 M: bad-literal-tuple summary drop "Bad literal tuple" ;
377
378 M: check-mixin-class-error summary drop "Not a mixin class" ;
379
380 M: not-found-in-roots summary
381     path>> "Cannot resolve vocab: " prepend ;
382
383 M: wrong-values summary drop "Quotation's stack effect does not match call site" ;
384
385 M: stack-effect-omits-dashes summary drop "Stack effect must contain “--”" ;
386
387 { "threads" "debugger" } "debugger.threads" require-when