]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/walker/walker.factor
Merge branch 'master' of git://projects.elasticdog.com/git/factor
[factor.git] / basis / tools / walker / walker.factor
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 models.filter arrays accessors
7 generic generic.standard definitions make ;
8 IN: tools.walker
9
10 SYMBOL: show-walker-hook ! ( status continuation thread -- )
11
12 ! Thread local in thread being walked
13 SYMBOL: walker-thread
14
15 ! Thread local in walker thread
16 SYMBOL: walking-thread
17 SYMBOL: walker-status
18 SYMBOL: walker-continuation
19 SYMBOL: walker-history
20
21 DEFER: start-walker-thread
22
23 : get-walker-thread ( -- status continuation thread )
24     walker-thread tget [
25         [ variables>> walker-status swap at ]
26         [ variables>> walker-continuation swap at ]
27         [ ] tri
28     ] [
29         f <model>
30         f <model>
31         2dup start-walker-thread
32     ] if* ;
33
34 : show-walker ( -- thread )
35     get-walker-thread
36     [ show-walker-hook get call ] keep ;
37
38 : after-break ( object -- )
39     {
40         { [ dup continuation? ] [ (continue) ] }
41         { [ dup quotation? ] [ call ] }
42         { [ dup not ] [ "Single stepping abandoned" rethrow ] }
43     } cond ;
44
45 : break ( -- )
46     continuation callstack >>call
47     show-walker send-synchronous
48     after-break ;
49
50 \ break t "break?" set-word-prop
51
52 : walk ( quot -- quot' )
53     \ break prefix [ break rethrow ] recover ;
54
55 GENERIC: add-breakpoint ( quot -- quot' )
56
57 M: callable add-breakpoint
58     dup [ break ] head? [ \ break prefix ] unless ;
59
60 M: array add-breakpoint
61     [ add-breakpoint ] map ;
62
63 M: object add-breakpoint ;
64
65 : (step-into-quot) ( quot -- ) add-breakpoint call ;
66
67 : (step-into-dip) ( quot -- ) add-breakpoint dip ;
68
69 : (step-into-2dip) ( quot -- ) add-breakpoint 2dip ;
70
71 : (step-into-3dip) ( quot -- ) add-breakpoint 3dip ;
72
73 : (step-into-if) ( true false ? -- ) ? (step-into-quot) ;
74
75 : (step-into-dispatch) ( array n -- ) nth (step-into-quot) ;
76
77 : (step-into-execute) ( word -- )
78     {
79         { [ dup "step-into" word-prop ] [ "step-into" word-prop call ] }
80         { [ dup standard-generic? ] [ effective-method (step-into-execute) ] }
81         { [ dup hook-generic? ] [ effective-method (step-into-execute) ] }
82         { [ dup uses \ suspend swap member? ] [ execute break ] }
83         { [ dup primitive? ] [ execute break ] }
84         [ def>> (step-into-quot) ]
85     } cond ;
86
87 \ (step-into-execute) t "step-into?" set-word-prop
88
89 : (step-into-continuation) ( -- )
90     continuation callstack >>call break ;
91
92 : (step-into-call-next-method) ( method -- )
93     next-method-quot (step-into-quot) ;
94
95 ! Messages sent to walker thread
96 SYMBOL: step
97 SYMBOL: step-out
98 SYMBOL: step-into
99 SYMBOL: step-all
100 SYMBOL: step-into-all
101 SYMBOL: step-back
102 SYMBOL: abandon
103 SYMBOL: call-in
104
105 SYMBOL: +running+
106 SYMBOL: +suspended+
107 SYMBOL: +stopped+
108
109 : change-frame ( continuation quot -- continuation' )
110     #! Applies quot to innermost call frame of the
111     #! continuation.
112     [ clone ] dip [
113         [ clone ] dip
114         [
115             [
116                 [ innermost-frame-scan 1+ ]
117                 [ innermost-frame-quot ] bi
118             ] dip call
119         ]
120         [ drop set-innermost-frame-quot ]
121         [ drop ]
122         2tri
123     ] curry change-call ; inline
124
125 : step-msg ( continuation -- continuation' ) USE: io
126     [
127         2dup length = [ nip [ break ] append ] [
128             2dup nth \ break = [ nip ] [
129                 swap 1+ cut [ break ] glue 
130             ] if
131         ] if
132     ] change-frame ;
133
134 : step-out-msg ( continuation -- continuation' )
135     [ nip \ break suffix ] change-frame ;
136
137 {
138     { call [ (step-into-quot) ] }
139     { dip [ (step-into-dip) ] }
140     { 2dip [ (step-into-2dip) ] }
141     { 3dip [ (step-into-3dip) ] }
142     { (throw) [ drop (step-into-quot) ] }
143     { execute [ (step-into-execute) ] }
144     { if [ (step-into-if) ] }
145     { dispatch [ (step-into-dispatch) ] }
146     { continuation [ (step-into-continuation) ] }
147     { (call-next-method) [ (step-into-call-next-method) ] }
148 } [ "step-into" set-word-prop ] assoc-each
149
150 {
151     >n ndrop >c c>
152     continue continue-with
153     stop suspend (spawn)
154 } [
155     dup [ execute break ] curry
156     "step-into" set-word-prop
157 ] each
158
159 \ break [ break ] "step-into" set-word-prop
160
161 : step-into-msg ( continuation -- continuation' )
162     [
163         swap cut [
164             swap %
165             [ \ break , ] [
166                 unclip {
167                     { [ dup \ break eq? ] [ , ] }
168                     { [ dup quotation? ] [ add-breakpoint , \ break , ] }
169                     { [ dup array? ] [ add-breakpoint , \ break , ] }
170                     { [ dup word? ] [ literalize , \ (step-into-execute) , ] }
171                     [ , \ break , ]
172                 } cond %
173             ] if-empty
174         ] [ ] make
175     ] change-frame ;
176
177 : status ( -- symbol )
178     walker-status tget value>> ;
179
180 : set-status ( symbol -- )
181     walker-status tget set-model ;
182
183 : keep-running ( -- )
184     +running+ set-status ;
185
186 : walker-stopped ( -- )
187     +stopped+ set-status ;
188
189 : step-into-all-loop ( -- )
190     +running+ set-status
191     [ status +running+ eq? ] [
192         [
193             {
194                 { step [ f ] }
195                 { step-out [ f ] }
196                 { step-into [ f ] }
197                 { step-all [ f ] }
198                 { step-into-all [ f ] }
199                 { step-back [ f ] }
200                 { f [ +stopped+ set-status f ] }
201                 [
202                     [ walker-continuation tget set-model ]
203                     [ step-into-msg ] bi
204                 ]
205             } case
206         ] handle-synchronous
207     ] [ ] while ;
208
209 : step-back-msg ( continuation -- continuation' )
210     walker-history tget
211     [ pop* ]
212     [ [ nip pop ] unless-empty ] bi ;
213
214 : walker-suspended ( continuation -- continuation' )
215     +suspended+ set-status
216     [ status +suspended+ eq? ] [
217         dup walker-history tget push
218         dup walker-continuation tget set-model
219         [
220             {
221                 ! These are sent by the walker tool. We reply
222                 ! and keep cycling.
223                 { step [ step-msg keep-running ] }
224                 { step-out [ step-out-msg keep-running ] }
225                 { step-into [ step-into-msg keep-running ] }
226                 { step-all [ keep-running ] }
227                 { step-into-all [ step-into-all-loop ] }
228                 { abandon [ drop f keep-running ] }
229                 ! Pass quotation to debugged thread
230                 { call-in [ nip keep-running ] }
231                 ! Pass previous continuation to debugged thread
232                 { step-back [ step-back-msg ] }
233             } case f
234         ] handle-synchronous
235     ] [ ] while ;
236
237 : walker-loop ( -- )
238     +running+ set-status
239     [ status +stopped+ eq? not ] [
240         [
241             {
242                 ! ignore these commands while the thread is
243                 ! running
244                 { step [ f ] }
245                 { step-out [ f ] }
246                 { step-into [ f ] }
247                 { step-all [ f ] }
248                 { step-into-all [ step-into-all-loop f ] }
249                 { step-back [ f ] }
250                 { abandon [ f ] }
251                 { f [ walker-stopped f ] }
252                 ! thread hit a breakpoint and sent us the
253                 ! continuation, so we modify it and send it
254                 ! back.
255                 [ walker-suspended ]
256             } case
257         ] handle-synchronous
258     ] [ ] while ;
259
260 : associate-thread ( walker -- )
261     walker-thread tset
262     [ f walker-thread tget send-synchronous drop ]
263     self (>>exit-handler) ;
264
265 : start-walker-thread ( status continuation -- thread' )
266     self [
267         walking-thread tset
268         walker-continuation tset
269         walker-status tset
270         V{ } clone walker-history tset
271         walker-loop
272     ] 3curry
273     "Walker on " self name>> append spawn
274     [ associate-thread ] keep ;
275
276 ! For convenience
277 IN: syntax
278
279 : B ( -- ) break ;