1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: threads kernel namespaces continuations combinators
4 sequences math namespaces.private continuations.private
5 concurrency.messaging quotations kernel.private words
6 sequences.private assocs models arrays accessors
7 generic generic.standard ;
10 SYMBOL: show-walker-hook ! ( status continuation thread -- )
12 ! Thread local in thread being walked
15 ! Thread local in walker thread
16 SYMBOL: walking-thread
18 SYMBOL: walker-continuation
19 SYMBOL: walker-history
21 DEFER: start-walker-thread
23 : get-walker-thread ( -- status continuation thread )
25 [ thread-variables walker-status swap at ]
26 [ thread-variables walker-continuation swap at ]
31 2dup start-walker-thread
34 : show-walker ( -- thread )
36 [ show-walker-hook get call ] keep ;
38 : after-break ( object -- )
40 { [ dup continuation? ] [ (continue) ] }
41 { [ dup quotation? ] [ call ] }
42 { [ dup not ] [ "Single stepping abandoned" rethrow ] }
46 continuation callstack over set-continuation-call
47 show-walker send-synchronous
50 \ break t "break?" set-word-prop
52 : walk ( quot -- quot' )
53 \ break prefix [ break rethrow ] recover ;
55 GENERIC: add-breakpoint ( quot -- quot' )
57 M: callable add-breakpoint
58 dup [ break ] head? [ \ break prefix ] unless ;
60 M: array add-breakpoint
61 [ add-breakpoint ] map ;
63 M: object add-breakpoint ;
65 : (step-into-quot) ( quot -- ) add-breakpoint call ;
67 : (step-into-if) ? (step-into-quot) ;
69 : (step-into-dispatch) nth (step-into-quot) ;
71 : (step-into-execute) ( word -- )
73 { [ dup "step-into" word-prop ] [ "step-into" word-prop call ] }
74 { [ dup standard-generic? ] [ effective-method (step-into-execute) ] }
75 { [ dup primitive? ] [ execute break ] }
76 { [ t ] [ word-def (step-into-quot) ] }
79 \ (step-into-execute) t "step-into?" set-word-prop
81 : (step-into-continuation)
82 continuation callstack >>call break ;
84 ! Messages sent to walker thread
99 : change-frame ( continuation quot -- continuation' )
100 #! Applies quot to innermost call frame of the
106 [ innermost-frame-scan 1+ ]
107 [ innermost-frame-quot ] bi
110 [ drop set-innermost-frame-quot ]
113 ] curry change-call ; inline
115 : step-msg ( continuation -- continuation' )
120 swap 1+ cut [ break ] swap 3append
124 : step-out-msg ( continuation -- continuation' )
125 [ nip \ break suffix ] change-frame ;
128 { call [ (step-into-quot) ] }
129 { (throw) [ drop (step-into-quot) ] }
130 { execute [ (step-into-execute) ] }
131 { if [ (step-into-if) ] }
132 { dispatch [ (step-into-dispatch) ] }
133 { continuation [ (step-into-continuation) ] }
134 } [ "step-into" set-word-prop ] assoc-each
138 continue continue-with
139 stop yield suspend sleep (spawn)
142 dup [ execute break ] curry
143 "step-into" set-word-prop
146 \ break [ break ] "step-into" set-word-prop
148 : step-into-msg ( continuation -- continuation' )
152 { [ dup \ break eq? ] [ , ] }
153 { [ dup quotation? ] [ add-breakpoint , \ break , ] }
154 { [ dup array? ] [ add-breakpoint , \ break , ] }
155 { [ dup word? ] [ literalize , \ (step-into-execute) , ] }
156 { [ t ] [ , \ break , ] }
161 : status ( -- symbol )
162 walker-status tget model-value ;
164 : set-status ( symbol -- )
165 walker-status tget set-model ;
167 : keep-running ( -- )
168 +running+ set-status ;
170 : walker-stopped ( -- )
172 [ status +stopped+ eq? ]
173 [ [ drop f ] handle-synchronous ]
176 : step-into-all-loop ( -- )
178 [ status +running+ eq? ] [
185 { step-into-all [ f ] }
187 { f [ +stopped+ set-status f ] }
189 [ walker-continuation tget set-model ]
196 : step-back-msg ( continuation -- continuation' )
199 [ dup empty? [ drop ] [ nip pop ] if ] bi ;
201 : walker-suspended ( continuation -- continuation' )
202 +suspended+ set-status
203 [ status +suspended+ eq? ] [
204 dup walker-history tget push
205 dup walker-continuation tget set-model
208 ! These are sent by the walker tool. We reply
210 { step [ step-msg keep-running ] }
211 { step-out [ step-out-msg keep-running ] }
212 { step-into [ step-into-msg keep-running ] }
213 { step-all [ keep-running ] }
214 { step-into-all [ step-into-all-loop ] }
215 { abandon [ drop f keep-running ] }
216 ! Pass quotation to debugged thread
217 { call-in [ nip keep-running ] }
218 ! Pass previous continuation to debugged thread
219 { step-back [ step-back-msg ] }
226 [ status +stopped+ eq? not ] [
229 ! ignore these commands while the thread is
235 { step-into-all [ step-into-all-loop f ] }
238 { f [ walker-stopped f ] }
239 ! thread hit a breakpoint and sent us the
240 ! continuation, so we modify it and send it
247 : associate-thread ( walker -- )
249 [ f walker-thread tget send-synchronous drop ]
250 self set-thread-exit-handler ;
252 : start-walker-thread ( status continuation -- thread' )
255 walker-continuation tset
257 V{ } clone walker-history tset
260 "Walker on " self thread-name append spawn
261 [ associate-thread ] keep ;