]> gitweb.factorcode.org Git - factor.git/blob - basis/debugger/debugger.factor
885e2e303c39a8312f5645795f653c73f8f55606
[factor.git] / basis / debugger / debugger.factor
1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: slots arrays definitions generic hashtables summary io
4 kernel math namespaces make prettyprint prettyprint.config
5 sequences assocs sequences.private strings io.styles
6 io.pathnames vectors words system splitting math.parser
7 classes.mixin classes.tuple continuations continuations.private
8 combinators generic.math classes.builtin classes compiler.units
9 generic.standard vocabs init kernel.private io.encodings
10 accessors math.order destructors source-files parser
11 classes.tuple.parser effects.parser lexer compiler.errors
12 generic.parser strings.parser ;
13 IN: debugger
14
15 GENERIC: error. ( error -- )
16 GENERIC: error-help ( error -- topic )
17
18 M: object error. . ;
19 M: object error-help drop f ;
20
21 M: tuple error-help class ;
22
23 M: string error. print ;
24
25 : :s ( -- )
26     error-continuation get data>> stack. ;
27
28 : :r ( -- )
29     error-continuation get retain>> stack. ;
30
31 : :c ( -- )
32     error-continuation get call>> callstack. ;
33
34 : :get ( variable -- value )
35     error-continuation get name>> assoc-stack ;
36
37 : :res ( n -- * )
38     1- restarts get-global nth f restarts set-global restart ;
39
40 : :1 ( -- * ) 1 :res ;
41 : :2 ( -- * ) 2 :res ;
42 : :3 ( -- * ) 3 :res ;
43
44 : restart. ( restart n -- )
45     [
46         1+ dup 3 <= [ ":" % # "    " % ] [ # " :res  " % ] if
47         name>> %
48     ] "" make print ;
49
50 : restarts. ( -- )
51     restarts get [
52         nl
53         "The following restarts are available:" print
54         nl
55         [ restart. ] each-index
56     ] unless-empty ;
57
58 : print-error ( error -- )
59     [ error. flush ] curry
60     [ global [ "Error in print-error!" print drop ] bind ]
61     recover ;
62
63 : :error ( -- )
64     error get print-error ;
65
66 : print-error-and-restarts ( error -- )
67     print-error
68     restarts.
69     nl
70     "Type :help for debugging help." print flush ;
71
72 : try ( quot -- )
73     [ print-error-and-restarts ] recover ;
74
75 : expired-error. ( obj -- )
76     "Object did not survive image save/load: " write third . ;
77
78 : io-error. ( error -- )
79     "I/O error: " write third print ;
80
81 : type-check-error. ( obj -- )
82     "Type check error" print
83     "Object: " write dup fourth short.
84     "Object type: " write dup fourth class .
85     "Expected type: " write third type>class . ;
86
87 : divide-by-zero-error. ( obj -- )
88     "Division by zero" print drop ;
89
90 : signal-error. ( obj -- )
91     "Operating system signal " write third . ;
92
93 : array-size-error. ( obj -- )
94     "Invalid array size: " write dup third .
95     "Maximum: " write fourth 1- . ;
96
97 : c-string-error. ( obj -- )
98     "Cannot convert to C string: " write third . ;
99
100 : ffi-error. ( obj -- )
101     "FFI: " write
102     dup third [ write ": " write ] when*
103     fourth print ;
104
105 : heap-scan-error. ( obj -- )
106     "Cannot do next-object outside begin/end-scan" print drop ;
107
108 : undefined-symbol-error. ( obj -- )
109     "The image refers to a library or symbol that was not found"
110     " at load time" append print drop ;
111
112 : stack-underflow. ( obj name -- )
113     write " stack underflow" print drop ;
114
115 : stack-overflow. ( obj name -- )
116     write " stack overflow" print drop ;
117
118 : datastack-underflow. ( obj -- ) "Data" stack-underflow. ;
119 : datastack-overflow. ( obj -- ) "Data" stack-overflow. ;
120 : retainstack-underflow. ( obj -- ) "Retain" stack-underflow. ;
121 : retainstack-overflow. ( obj -- ) "Retain" stack-overflow. ;
122
123 : memory-error. ( error -- )
124     "Memory protection fault at address " write third .h ;
125
126 : primitive-error. ( error -- ) 
127     "Unimplemented primitive" print drop ;
128
129 PREDICATE: kernel-error < array
130     {
131         { [ dup empty? ] [ drop f ] }
132         { [ dup first "kernel-error" = not ] [ drop f ] }
133         [ second 0 15 between? ]
134     } cond ;
135
136 : kernel-errors ( error -- n errors )
137     second {
138         { 0  [ expired-error.          ] }
139         { 1  [ io-error.               ] }
140         { 2  [ primitive-error.        ] }
141         { 3  [ type-check-error.       ] }
142         { 4  [ divide-by-zero-error.   ] }
143         { 5  [ signal-error.           ] }
144         { 6  [ array-size-error.       ] }
145         { 7  [ c-string-error.         ] }
146         { 8  [ ffi-error.              ] }
147         { 9  [ heap-scan-error.        ] }
148         { 10 [ undefined-symbol-error. ] }
149         { 11 [ datastack-underflow.    ] }
150         { 12 [ datastack-overflow.     ] }
151         { 13 [ retainstack-underflow.  ] }
152         { 14 [ retainstack-overflow.   ] }
153         { 15 [ memory-error.           ] }
154     } ; inline
155
156 M: kernel-error error. dup kernel-errors case ;
157
158 M: kernel-error error-help kernel-errors at first ;
159
160 M: no-method summary
161     drop "No suitable method" ;
162
163 M: no-method error.
164     "Generic word " write
165     dup generic>> pprint
166     " does not define a method for the " write
167     dup object>> class pprint
168     " class." print
169     "Dispatching on object: " write object>> short. ;
170
171 M: bad-slot-value summary drop "Bad store to specialized slot" ;
172
173 M: no-math-method summary
174     drop "No suitable arithmetic method" ;
175
176 M: no-next-method summary
177     drop "Executing call-next-method from least-specific method" ;
178
179 M: inconsistent-next-method summary
180     drop "Executing call-next-method with inconsistent parameters" ;
181
182 M: check-method summary
183     drop "Invalid parameters for create-method" ;
184
185 M: not-a-tuple summary
186     drop "Not a tuple" ;
187
188 M: bad-superclass summary
189     drop "Tuple classes can only inherit from other tuple classes" ;
190
191 M: no-initial-value summary
192     drop "Initial value must be provided for slots specialized to this class" ;
193
194 M: bad-initial-value summary
195     drop "Incompatible initial value" ;
196
197 M: no-cond summary
198     drop "Fall-through in cond" ;
199
200 M: no-case summary
201     drop "Fall-through in case" ;
202
203 M: slice-error summary
204     drop "Cannot create slice" ;
205
206 M: bounds-error summary drop "Sequence index out of bounds" ;
207
208 M: condition error. error>> error. ;
209
210 M: condition summary error>> summary ;
211
212 M: condition error-help error>> error-help ;
213
214 M: assert summary drop "Assertion failed" ;
215
216 M: assert error.
217     "Assertion failed" print
218     standard-table-style [
219         15 length-limit set
220         5 line-limit set
221         [ expect>> [ [ "Expect:" write ] with-cell pprint-cell ] with-row ]
222         [ got>> [ [ "Got:" write ] with-cell pprint-cell ] with-row ] bi
223     ] tabular-output ;
224
225 M: immutable summary drop "Sequence is immutable" ;
226
227 M: redefine-error error.
228     "Re-definition of " write
229     def>> . ;
230
231 M: undefined summary
232     drop "Calling a deferred word before it has been defined" ;
233
234 M: no-compilation-unit error.
235     "Attempting to define " write
236     definition>> pprint
237     " outside of a compilation unit" print ;
238
239 M: no-vocab summary
240     drop "Vocabulary does not exist" ;
241
242 M: encode-error summary drop "Character encoding error" ;
243
244 M: decode-error summary drop "Character decoding error" ;
245
246 M: bad-create summary drop "Bad parameters to create" ;
247
248 M: attempt-all-error summary drop "Nothing to attempt" ;
249
250 M: already-disposed summary drop "Attempting to operate on disposed object" ;
251
252 M: no-current-vocab summary
253     drop "Not in a vocabulary; IN: form required" ;
254
255 M: no-word-error summary
256     drop "Word not found in current vocabulary search path" ;
257
258 M: staging-violation summary
259     drop
260     "A parsing word cannot be used in the same file it is defined in." ;
261
262 M: bad-number summary
263     drop "Bad number literal" ;
264
265 M: duplicate-slot-names summary
266     drop "Duplicate slot names" ;
267
268 M: invalid-slot-name summary
269     drop "Invalid slot name" ;
270
271 : file. ( file -- ) path>> <pathname> . ;
272
273 M: source-file-error error.
274     [ file>> file. ] [ error>> error. ] bi ;
275
276 M: source-file-error summary
277     error>> summary ;
278
279 M: source-file-error compute-restarts
280     error>> compute-restarts ;
281
282 M: source-file-error error-help
283     error>> error-help ;
284
285 M: not-in-a-method-error summary
286     drop "call-next-method can only be called in a method definition" ;
287
288 GENERIC: expected>string ( obj -- str )
289
290 M: f expected>string drop "end of input" ;
291 M: word expected>string name>> ;
292 M: string expected>string ;
293
294 M: unexpected error.
295     "Expected " write
296     dup want>> expected>string write
297     " but got " write
298     got>> expected>string print ;
299
300 M: lexer-error error.
301     [ lexer-dump ] [ error>> error. ] bi ;
302
303 M: lexer-error summary
304     error>> summary ;
305
306 M: lexer-error compute-restarts
307     error>> compute-restarts ;
308
309 M: lexer-error error-help
310     error>> error-help ;
311
312 M: object compiler-error. ( error word -- )
313     nl
314     "While compiling " write pprint ": " print
315     nl
316     print-error ;
317
318 M: bad-effect summary
319     drop "Bad stack effect declaration" ;
320
321 M: bad-escape summary drop "Bad escape code" ;
322
323 M: bad-literal-tuple summary drop "Bad literal tuple" ;
324
325 M: check-mixin-class summary drop "Not a mixin class" ;