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 definitions ;
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) ( true false ? -- ) ? (step-into-quot) ;
69 : (step-into-dispatch) ( array n -- ) 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 hook-generic? ] [ effective-method (step-into-execute) ] }
76 { [ dup uses \ suspend swap member? ] [ execute break ] }
77 { [ dup primitive? ] [ execute break ] }
78 [ word-def (step-into-quot) ]
81 \ (step-into-execute) t "step-into?" set-word-prop
83 : (step-into-continuation) ( -- )
84 continuation callstack >>call break ;
86 ! Messages sent to walker thread
100 : change-frame ( continuation quot -- continuation' )
101 #! Applies quot to innermost call frame of the
107 [ innermost-frame-scan 1+ ]
108 [ innermost-frame-quot ] bi
111 [ drop set-innermost-frame-quot ]
114 ] curry change-call ; inline
116 : step-msg ( continuation -- continuation' )
121 swap 1+ cut [ break ] swap 3append
125 : step-out-msg ( continuation -- continuation' )
126 [ nip \ break suffix ] change-frame ;
129 { call [ (step-into-quot) ] }
130 { (throw) [ drop (step-into-quot) ] }
131 { execute [ (step-into-execute) ] }
132 { if [ (step-into-if) ] }
133 { dispatch [ (step-into-dispatch) ] }
134 { continuation [ (step-into-continuation) ] }
135 } [ "step-into" set-word-prop ] assoc-each
139 continue continue-with
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) , ] }
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 ( -- )
171 +stopped+ set-status ;
173 : step-into-all-loop ( -- )
175 [ status +running+ eq? ] [
182 { step-into-all [ f ] }
184 { f [ +stopped+ set-status f ] }
186 [ walker-continuation tget set-model ]
193 : step-back-msg ( continuation -- continuation' )
196 [ dup empty? [ drop ] [ nip pop ] if ] bi ;
198 : walker-suspended ( continuation -- continuation' )
199 +suspended+ set-status
200 [ status +suspended+ eq? ] [
201 dup walker-history tget push
202 dup walker-continuation tget set-model
205 ! These are sent by the walker tool. We reply
207 { step [ step-msg keep-running ] }
208 { step-out [ step-out-msg keep-running ] }
209 { step-into [ step-into-msg keep-running ] }
210 { step-all [ keep-running ] }
211 { step-into-all [ step-into-all-loop ] }
212 { abandon [ drop f keep-running ] }
213 ! Pass quotation to debugged thread
214 { call-in [ nip keep-running ] }
215 ! Pass previous continuation to debugged thread
216 { step-back [ step-back-msg ] }
223 [ status +stopped+ eq? not ] [
226 ! ignore these commands while the thread is
232 { step-into-all [ step-into-all-loop f ] }
235 { f [ walker-stopped f ] }
236 ! thread hit a breakpoint and sent us the
237 ! continuation, so we modify it and send it
244 : associate-thread ( walker -- )
246 [ f walker-thread tget send-synchronous drop ]
247 self set-thread-exit-handler ;
249 : start-walker-thread ( status continuation -- thread' )
252 walker-continuation tset
254 V{ } clone walker-history tset
257 "Walker on " self thread-name append spawn
258 [ associate-thread ] keep ;