factor/core/tools/debugger.factor

146 lines
3.6 KiB
Factor
Raw Normal View History

2006-01-09 01:34:23 -05:00
! Copyright (C) 2004, 2006 Slava Pestov.
2006-05-15 00:03:55 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: arrays definitions generic hashtables help tools io
kernel math namespaces parser prettyprint sequences
sequences-internals strings styles vectors words errors ;
IN: kernel-internals
: save-error ( error trace continuation -- )
error-continuation set-global
error-stack-trace set-global
dup error set-global
compute-restarts restarts set-global ;
: error-handler ( error trace -- )
dupd continuation save-error rethrow ;
: init-error-handler ( -- )
V{ } clone set-catchstack
! kernel calls on error
[ error-handler ] 5 setenv
\ kernel-error 12 setenv ;
: code-heap-start 17 getenv ;
: code-heap-end 18 getenv ;
: <xt-map> ( -- xtmap )
[
f code-heap-start 2array ,
all-words [ compiled? ] subset
[ dup word-xt 2array , ] each
f code-heap-end 2array ,
] { } make [ [ second ] 2apply - ] sort ;
: find-xt ( xt xtmap -- word )
[ second - ] binsearch* first ;
: symbolic-stack-trace ( seq -- seq )
<xt-map> swap [ dup pick find-xt 2array ] map nip ;
2004-11-25 21:51:47 -05:00
IN: errors
2006-08-01 17:56:20 -04:00
2006-08-01 04:45:05 -04:00
GENERIC: error. ( error -- )
GENERIC: error-help ( error -- topic )
M: object error. . ;
M: object error-help drop f ;
2006-08-01 04:45:05 -04:00
M: tuple error. describe ;
M: tuple error-help class ;
2006-08-01 04:45:05 -04:00
M: string error. print ;
2006-08-01 17:56:20 -04:00
2006-08-16 21:55:53 -04:00
: :s ( -- )
error-continuation get continuation-data stack. ;
: :r ( -- )
error-continuation get continuation-retain stack. ;
: xt. ( xt -- )
>hex cell 2 * CHAR: 0 pad-left write ;
: word-xt. ( xt word -- )
"Compiled: " write dup pprint bl
"(offset " write word-xt - >hex write ")" write ;
: bare-xt. ( xt -- )
2006-11-30 02:15:42 -05:00
"C code: " write xt. ;
: :trace
error-stack-trace get symbolic-stack-trace <reversed> [
first2 [ word-xt. ] [ bare-xt. ] if* terpri
] each ;
2006-08-16 21:55:53 -04:00
: :c ( -- )
error-continuation get continuation-call callstack. :trace ;
2006-08-16 21:55:53 -04:00
: :get ( variable -- value )
error-continuation get continuation-name hash-stack ;
: :res ( n -- )
2006-11-30 02:15:42 -05:00
restarts get-global nth f restarts set-global restart ;
2006-08-25 00:02:30 -04:00
: :edit ( -- )
2006-11-30 02:15:42 -05:00
error get delegates [ parse-error? ] find-last nip [
2006-11-28 18:44:55 -05:00
dup parse-error-file ?resource-path
swap parse-error-line edit-location
] when* ;
2006-08-25 00:02:30 -04:00
2006-08-01 18:14:22 -04:00
: (:help-multi)
2006-11-08 22:58:01 -05:00
"This error has multiple delegates:" print
help-outliner terpri ;
2006-08-01 18:14:22 -04:00
: (:help-none)
drop "No help for this error. " print ;
2006-08-01 04:45:05 -04:00
: :help ( -- )
error get delegates [ error-help ] map [ ] subset
2006-08-01 18:14:22 -04:00
{
{ [ dup empty? ] [ (:help-none) ] }
{ [ dup length 1 = ] [ first help ] }
{ [ t ] [ (:help-multi) ] }
} cond ;
2006-08-01 04:45:05 -04:00
: restart. ( restart n -- )
2006-11-30 02:15:42 -05:00
[ # " :res " % restart-name % ] "" make print ;
: restarts. ( -- )
restarts get dup empty? [
drop
] [
terpri
"The following restarts are available:" print
terpri
dup length [ restart. ] 2each
] if ;
2004-12-25 21:28:47 -05:00
: debug-help ( -- )
terpri
"Debugger commands:" print
terpri
2006-11-30 02:15:42 -05:00
":help - documentation for this error" print
":s - data stack at exception time" print
":r - retain stack at exception time" print
":c - call stack at exception time" print
2006-08-25 00:02:30 -04:00
error get [ parse-error? ] is? [
2006-11-30 02:15:42 -05:00
":edit - jump to source location" print
2006-08-25 00:02:30 -04:00
] when
":get ( var -- value ) accesses variables at time of the error" print
2005-12-16 21:12:35 -05:00
flush ;
2004-12-25 21:28:47 -05:00
: print-error ( error -- )
[
dup error.
] [
2006-12-04 00:20:05 -05:00
"Error in print-error!" print drop
] recover drop ;
2006-11-30 02:15:42 -05:00
SYMBOL: error-hook
2006-12-04 00:20:05 -05:00
[ print-error restarts. debug-help ] error-hook set-global
2006-11-30 02:15:42 -05:00
: try ( quot -- )
2006-12-04 00:20:05 -05:00
[ error-hook get call ] recover ;