factor/basis/tools/walker/walker.factor

175 lines
4.8 KiB
Factor
Raw Normal View History

2009-04-08 07:23:07 -04:00
! Copyright (C) 2004, 2009 Slava Pestov.
2008-02-21 00:13:22 -05:00
! See http://factorcode.org/license.txt for BSD license.
USING: threads kernel namespaces continuations combinators
sequences math namespaces.private continuations.private
concurrency.messaging quotations kernel.private words
2009-02-26 17:33:00 -05:00
sequences.private assocs models models.arrow arrays accessors
2009-04-08 07:23:07 -04:00
generic generic.standard definitions make sbufs
tools.continuations parser tools.annotations fry ;
2008-02-21 00:13:22 -05:00
IN: tools.walker
2007-12-31 22:16:21 -05:00
2008-03-18 02:26:09 -04:00
SYMBOL: show-walker-hook ! ( status continuation thread -- )
2008-02-20 00:26:07 -05:00
2008-03-18 02:26:09 -04:00
! Thread local in thread being walked
2008-02-21 00:13:22 -05:00
SYMBOL: walker-thread
2008-03-18 02:26:09 -04:00
! Thread local in walker thread
2008-02-21 00:13:31 -05:00
SYMBOL: walking-thread
2008-03-18 02:26:09 -04:00
SYMBOL: walker-status
SYMBOL: walker-continuation
SYMBOL: walker-history
DEFER: start-walker-thread
2008-02-20 00:26:07 -05:00
2008-03-18 02:26:09 -04:00
: get-walker-thread ( -- status continuation thread )
2008-02-21 00:13:22 -05:00
walker-thread tget [
[ variables>> walker-status of ]
[ variables>> walker-continuation of ]
2008-03-18 02:26:09 -04:00
[ ] tri
2008-02-21 00:13:31 -05:00
] [
2008-03-18 02:26:09 -04:00
f <model>
f <model>
2dup start-walker-thread
2008-02-21 00:13:31 -05:00
] if* ;
2007-10-04 18:45:19 -04:00
: walk ( quot -- quot' )
\ break prefix [ break rethrow ] recover ;
2009-04-17 00:14:26 -04:00
<< \ walk t "no-compile" set-word-prop >>
2009-04-08 07:23:07 -04:00
break-hook [
[
get-walker-thread
[ show-walker-hook get call ] keep
send-synchronous
]
] initialize
2008-02-21 00:13:22 -05:00
! Messages sent to walker thread
2008-02-20 00:26:07 -05:00
SYMBOL: step
SYMBOL: step-out
SYMBOL: step-into
SYMBOL: step-all
2008-02-21 00:13:22 -05:00
SYMBOL: step-into-all
2008-02-20 00:26:07 -05:00
SYMBOL: step-back
SYMBOL: abandon
SYMBOL: call-in
2007-09-20 18:09:08 -04:00
2008-02-21 00:13:22 -05:00
SYMBOL: +running+
SYMBOL: +suspended+
SYMBOL: +stopped+
2008-02-20 00:26:07 -05:00
2008-02-21 00:13:22 -05:00
: status ( -- symbol )
2008-08-31 03:51:16 -04:00
walker-status tget value>> ;
2008-02-21 00:13:22 -05:00
: set-status ( symbol -- )
walker-status tget set-model ;
2008-02-20 00:26:07 -05:00
2008-02-21 00:13:31 -05:00
: keep-running ( -- )
+running+ set-status ;
2008-02-20 00:26:07 -05:00
2008-02-21 00:13:22 -05:00
: walker-stopped ( -- )
+stopped+ set-status ;
2008-02-20 00:26:07 -05:00
2008-02-21 00:13:22 -05:00
: step-into-all-loop ( -- )
+running+ set-status
2008-02-21 00:13:31 -05:00
[ status +running+ eq? ] [
2008-02-21 00:13:22 -05:00
[
{
2008-02-21 02:25:59 -05:00
{ step [ f ] }
{ step-out [ f ] }
{ step-into [ f ] }
{ step-all [ f ] }
{ step-into-all [ f ] }
{ step-back [ f ] }
{ f [ +stopped+ set-status f ] }
[
[ walker-continuation tget set-model ]
2009-04-08 07:23:07 -04:00
[ continuation-step-into ] bi
2008-02-21 02:25:59 -05:00
]
} case
2008-02-21 00:13:22 -05:00
] handle-synchronous
] while ;
2008-02-20 00:26:07 -05:00
2009-04-08 07:23:07 -04:00
: continuation-step-back ( continuation -- continuation' )
walker-history tget
[ pop* ]
2008-09-06 20:13:59 -04:00
[ [ nip pop ] unless-empty ] bi ;
2008-02-21 00:13:31 -05:00
2008-02-21 00:13:22 -05:00
: walker-suspended ( continuation -- continuation' )
+suspended+ set-status
[ status +suspended+ eq? ] [
2008-02-21 00:13:31 -05:00
dup walker-history tget push
dup walker-continuation tget set-model
2008-02-20 00:26:07 -05:00
[
{
2008-02-21 00:13:22 -05:00
! These are sent by the walker tool. We reply
! and keep cycling.
2009-04-08 07:23:07 -04:00
{ step [ continuation-step keep-running ] }
{ step-out [ continuation-step-out keep-running ] }
{ step-into [ continuation-step-into keep-running ] }
2008-02-20 00:26:07 -05:00
{ step-all [ keep-running ] }
2008-02-21 00:13:22 -05:00
{ step-into-all [ step-into-all-loop ] }
2008-02-20 00:26:07 -05:00
{ abandon [ drop f keep-running ] }
! Pass quotation to debugged thread
2009-02-10 18:11:38 -05:00
{ call-in [ keep-running ] }
2008-02-20 00:26:07 -05:00
! Pass previous continuation to debugged thread
2009-04-08 07:23:07 -04:00
{ step-back [ continuation-step-back ] }
2008-02-21 00:13:31 -05:00
} case f
2008-02-20 00:26:07 -05:00
] handle-synchronous
] while ;
2009-04-08 07:23:07 -04:00
2008-02-21 00:13:22 -05:00
: walker-loop ( -- )
+running+ set-status
[ status +stopped+ eq? ] [
2008-02-20 00:26:07 -05:00
[
{
! ignore these commands while the thread is
! running
{ step [ f ] }
{ step-out [ f ] }
{ step-into [ f ] }
{ step-all [ f ] }
2008-02-21 00:13:31 -05:00
{ step-into-all [ step-into-all-loop f ] }
2008-02-20 00:26:07 -05:00
{ step-back [ f ] }
2008-02-21 00:13:31 -05:00
{ abandon [ f ] }
2008-02-21 00:13:22 -05:00
{ f [ walker-stopped f ] }
2008-02-20 00:26:07 -05:00
! thread hit a breakpoint and sent us the
2008-02-21 00:13:22 -05:00
! continuation, so we modify it and send it
! back.
[ walker-suspended ]
2008-02-20 00:26:07 -05:00
} case
] handle-synchronous
] until ;
2008-02-20 00:26:07 -05:00
2008-02-21 00:13:22 -05:00
: associate-thread ( walker -- )
2008-02-21 00:13:31 -05:00
walker-thread tset
[ f walker-thread tget send-synchronous drop ]
self exit-handler<< ;
2008-02-20 00:26:07 -05:00
2008-02-21 00:13:22 -05:00
: start-walker-thread ( status continuation -- thread' )
2008-02-21 00:13:31 -05:00
self [
walking-thread tset
2008-02-21 00:13:22 -05:00
walker-continuation tset
walker-status tset
V{ } clone walker-history tset
walker-loop
2008-02-21 00:13:31 -05:00
] 3curry
2008-08-30 13:35:14 -04:00
"Walker on " self name>> append spawn
2008-02-21 00:13:22 -05:00
[ associate-thread ] keep ;
: breakpoint ( word -- )
[ add-breakpoint ] annotate ;
: breakpoint-if ( word quot -- )
'[ [ _ [ [ break ] when ] ] dip 3append ] annotate ;
! For convenience
IN: syntax
2009-10-28 14:38:27 -04:00
SYNTAX: B \ break suffix! ;
SYNTAX: B: scan-word definition
[ break "now press O I to land inside the parsing word" drop ]
prepose call( accum -- accum ) ;