1 \ @(#) trace.fth 98/01/28 1.2
2 \ TRACE ( <name> -- , trace pForth word )
4 \ Single step debugger.
5 \ TRACE ( i*x <name> -- , setup trace for Forth word )
7 \ SM ( many -- , step over many times )
8 \ SD ( -- , step down )
9 \ G ( -- , go to end of word )
10 \ GD ( n -- , go down N levels from current level, stop at end of this level )
12 \ This debugger works by emulating the inner interpreter of pForth.
13 \ It executes code and maintains a separate return stack for the
14 \ program under test. Thus all primitives that operate on the return
15 \ stack, such as DO and R> must be trapped. Local variables must
16 \ also be handled specially. Several state variables are also
17 \ saved and restored to establish the context for the program being
20 \ Copyright 1997 Phil Burk
23 \ 19990930 John Providenza - Fixed stack bugs in GD
27 : SPACE.TO.COLUMN ( col -- )
31 : IS.PRIMITIVE? ( xt -- flag , true if kernel primitive )
35 0 value TRACE_IP \ instruction pointer
36 0 value TRACE_LEVEL \ level of descent for inner interpreter
37 0 value TRACE_LEVEL_MAX \ maximum level of descent
41 \ use fake return stack
42 128 cells constant TRACE_RETURN_SIZE \ size of return stack in bytes
43 create TRACE-RETURN-STACK TRACE_RETURN_SIZE 16 + allot
45 : TRACE.>R ( n -- ) trace-rsp @ cell- dup trace-rsp ! ! ; \ *(--rsp) = n
46 : TRACE.R> ( -- n ) trace-rsp @ dup @ swap cell+ trace-rsp ! ; \ n = *rsp++
47 : TRACE.R@ ( -- n ) trace-rsp @ @ ; ; \ n = *rsp
48 : TRACE.RPICK ( index -- n ) cells trace-rsp @ + @ ; ; \ n = rsp[index]
49 : TRACE.0RP ( -- n ) trace-return-stack trace_return_size + 8 + trace-rsp ! ;
50 : TRACE.RDROP ( -- ) cell trace-rsp +! ;
51 : TRACE.RCHECK ( -- , abort if return stack out of range )
52 trace-rsp @ trace-return-stack u<
53 abort" TRACE return stack OVERFLOW!"
54 trace-rsp @ trace-return-stack trace_return_size + 12 + u>
55 abort" TRACE return stack UNDERFLOW!"
58 \ save and restore several state variables
59 10 cells constant TRACE_STATE_SIZE
60 create TRACE-STATE-1 TRACE_STATE_SIZE allot
61 create TRACE-STATE-2 TRACE_STATE_SIZE allot
63 variable TRACE-STATE-PTR
64 : TRACE.SAVE++ ( addr -- , save next thing )
66 cell trace-state-ptr +!
69 : TRACE.SAVE.STATE ( -- )
75 : TRACE.SAVE.STATE1 ( -- , save normal state )
76 trace-state-1 trace-state-ptr !
79 : TRACE.SAVE.STATE2 ( -- , save state of word being debugged )
80 trace-state-2 trace-state-ptr !
85 : TRACE.RESTORE++ ( addr -- , restore next thing )
86 trace-state-ptr @ @ swap !
87 cell trace-state-ptr +!
90 : TRACE.RESTORE.STATE ( -- )
96 : TRACE.RESTORE.STATE1 ( -- )
97 trace-state-1 trace-state-ptr !
100 : TRACE.RESTORE.STATE2 ( -- )
101 trace-state-2 trace-state-ptr !
105 \ The implementation of these pForth primitives is specific to pForth.
107 variable TRACE-LOCALS-PTR \ point to top of local frame
109 \ create a return stack frame for NUM local variables
110 : TRACE.(LOCAL.ENTRY) ( x0 x1 ... xn n -- ) { num | lp -- }
111 trace-locals-ptr @ trace.>r
112 trace-rsp @ trace-locals-ptr !
113 trace-rsp @ num cells - trace-rsp ! \ make room for locals
118 cell +-> lp \ move data into locals frame on return stack
122 : TRACE.(LOCAL.EXIT) ( -- )
123 trace-locals-ptr @ trace-rsp !
124 trace.r> trace-locals-ptr !
126 : TRACE.(LOCAL@) ( l# -- n , fetch from local frame )
127 trace-locals-ptr @ swap cells - @
129 : TRACE.(1_LOCAL@) ( -- n ) 1 trace.(local@) ;
130 : TRACE.(2_LOCAL@) ( -- n ) 2 trace.(local@) ;
131 : TRACE.(3_LOCAL@) ( -- n ) 3 trace.(local@) ;
132 : TRACE.(4_LOCAL@) ( -- n ) 4 trace.(local@) ;
133 : TRACE.(5_LOCAL@) ( -- n ) 5 trace.(local@) ;
134 : TRACE.(6_LOCAL@) ( -- n ) 6 trace.(local@) ;
135 : TRACE.(7_LOCAL@) ( -- n ) 7 trace.(local@) ;
136 : TRACE.(8_LOCAL@) ( -- n ) 8 trace.(local@) ;
138 : TRACE.(LOCAL!) ( n l# -- , store into local frame )
139 trace-locals-ptr @ swap cells - !
141 : TRACE.(1_LOCAL!) ( -- n ) 1 trace.(local!) ;
142 : TRACE.(2_LOCAL!) ( -- n ) 2 trace.(local!) ;
143 : TRACE.(3_LOCAL!) ( -- n ) 3 trace.(local!) ;
144 : TRACE.(4_LOCAL!) ( -- n ) 4 trace.(local!) ;
145 : TRACE.(5_LOCAL!) ( -- n ) 5 trace.(local!) ;
146 : TRACE.(6_LOCAL!) ( -- n ) 6 trace.(local!) ;
147 : TRACE.(7_LOCAL!) ( -- n ) 7 trace.(local!) ;
148 : TRACE.(8_LOCAL!) ( -- n ) 8 trace.(local!) ;
150 : TRACE.(LOCAL+!) ( n l# -- , store into local frame )
151 trace-locals-ptr @ swap cells - +!
153 : TRACE.(?DO) { limit start ip -- ip' }
165 : TRACE.(LOOP) { ip | limit indx -- ip' }
179 : TRACE.(+LOOP) { delta ip | limit indx oldindx -- ip' }
182 oldindx delta + -> indx
183 \ /* Do indices cross boundary between LIMIT-1 and LIMIT ? */
184 \ if( ( (OldIndex - Limit) & ((Limit-1) - NewIndex) & 0x80000000 ) ||
185 \ ( (NewIndex - Limit) & ((Limit-1) - OldIndex) & 0x80000000 ) )
186 oldindx limit - limit 1- indx - AND $ 80000000 AND
187 indx limit - limit 1- oldindx - AND $ 80000000 AND OR
198 : TRACE.CHECK.IP { ip -- }
199 ip ['] first_colon u<
202 ." TRACE - IP out of range = " ip .hex cr
207 : TRACE.SHOW.IP { ip -- , print name and offset }
208 ip code> >name dup id.
209 name> >code ip swap - ." +" .
212 : TRACE.SHOW.STACK { | mdepth -- }
214 ." <" base @ decimal 1 .r ." :"
217 depth 5 min -> mdepth
220 ." ... " \ if we don't show entire stack
224 mdepth i 1+ - pick . \ show numbers in current base
228 : TRACE.SHOW.NEXT { ip -- }
231 \ show word name and offset
237 40 space.to.column ." ||"
238 trace_level 2* spaces
241 \ show primitive about to be executed
243 \ trap any primitives that are followed by inline data
245 ['] (LITERAL) OF ip @ . ENDOF
246 ['] (ALITERAL) OF ip a@ . ENDOF
247 [ exists? (FLITERAL) [IF] ]
248 ['] (FLITERAL) OF ip f@ f. ENDOF
250 ['] BRANCH OF ip @ . ENDOF
251 ['] 0BRANCH OF ip @ . ENDOF
252 ['] (.") OF ip count type .' "' ENDOF
253 ['] (C") OF ip count type .' "' ENDOF
254 ['] (S") OF ip count type .' "' ENDOF
256 65 space.to.column ." >> "
259 : TRACE.DO.PRIMITIVE { ip xt | oldhere -- ip' , perform code at ip }
262 0 OF -1 +-> trace_level trace.r> -> ip ENDOF \ EXIT
263 ['] (CREATE) OF ip cell- body_offset + ENDOF
264 ['] (LITERAL) OF ip @ cell +-> ip ENDOF
265 ['] (ALITERAL) OF ip a@ cell +-> ip ENDOF
266 [ exists? (FLITERAL) [IF] ]
267 ['] (FLITERAL) OF ip f@ 1 floats +-> ip ENDOF
269 ['] BRANCH OF ip @ +-> ip ENDOF
270 ['] 0BRANCH OF 0= IF ip @ +-> ip ELSE cell +-> ip THEN ENDOF
271 ['] >R OF trace.>r ENDOF
272 ['] R> OF trace.r> ENDOF
273 ['] R@ OF trace.r@ ENDOF
274 ['] RDROP OF trace.rdrop ENDOF
275 ['] 2>R OF trace.>r trace.>r ENDOF
276 ['] 2R> OF trace.r> trace.r> ENDOF
277 ['] 2R@ OF trace.r@ 1 trace.rpick ENDOF
278 ['] i OF 1 trace.rpick ENDOF
279 ['] j OF 3 trace.rpick ENDOF
280 ['] (LEAVE) OF trace.rdrop trace.rdrop ip @ +-> ip ENDOF
281 ['] (LOOP) OF ip trace.(loop) -> ip ENDOF
282 ['] (+LOOP) OF ip trace.(+loop) -> ip ENDOF
283 ['] (DO) OF trace.>r trace.>r ENDOF
284 ['] (?DO) OF ip trace.(?do) -> ip ENDOF
285 ['] (.") OF ip count type ip count + aligned -> ip ENDOF
286 ['] (C") OF ip ip count + aligned -> ip ENDOF
287 ['] (S") OF ip count ip count + aligned -> ip ENDOF
288 ['] (LOCAL.ENTRY) OF trace.(local.entry) ENDOF
289 ['] (LOCAL.EXIT) OF trace.(local.exit) ENDOF
290 ['] (LOCAL@) OF trace.(local@) ENDOF
291 ['] (1_LOCAL@) OF trace.(1_local@) ENDOF
292 ['] (2_LOCAL@) OF trace.(2_local@) ENDOF
293 ['] (3_LOCAL@) OF trace.(3_local@) ENDOF
294 ['] (4_LOCAL@) OF trace.(4_local@) ENDOF
295 ['] (5_LOCAL@) OF trace.(5_local@) ENDOF
296 ['] (6_LOCAL@) OF trace.(6_local@) ENDOF
297 ['] (7_LOCAL@) OF trace.(7_local@) ENDOF
298 ['] (8_LOCAL@) OF trace.(8_local@) ENDOF
299 ['] (LOCAL!) OF trace.(local!) ENDOF
300 ['] (1_LOCAL!) OF trace.(1_local!) ENDOF
301 ['] (2_LOCAL!) OF trace.(2_local!) ENDOF
302 ['] (3_LOCAL!) OF trace.(3_local!) ENDOF
303 ['] (4_LOCAL!) OF trace.(4_local!) ENDOF
304 ['] (5_LOCAL!) OF trace.(5_local!) ENDOF
305 ['] (6_LOCAL!) OF trace.(6_local!) ENDOF
306 ['] (7_LOCAL!) OF trace.(7_local!) ENDOF
307 ['] (8_LOCAL!) OF trace.(8_local!) ENDOF
308 ['] (LOCAL+!) OF trace.(local+!) ENDOF
314 : TRACE.DO.NEXT { ip | xt oldhere -- ip' , perform code at ip }
316 \ set context for word under test
321 \ get execution token
327 ip xt trace.do.primitive -> ip
329 trace_level trace_level_max <
331 ip trace.>r \ threaded execution
335 \ treat it as a primitive
336 ip xt trace.do.primitive -> ip
339 \ restore original context
347 : TRACE.NEXT { ip | xt -- ip' }
350 ip trace.do.next -> ip
364 : TRACE ( i*x <name> -- i*x , setup trace environment )
367 drop ." Sorry. You can't trace a primitive." cr
370 trace_level -> trace_level_max
373 trace_ip trace.show.next
379 : s ( -- , step over )
380 trace_level -> trace_level_max
381 trace_ip trace.next -> trace_ip
384 : sd ( -- , step down )
385 trace_level 1+ -> trace_level_max
386 trace_ip trace.next -> trace_ip
389 : sm ( many -- , step many times )
390 trace_level -> trace_level_max
393 trace_ip trace.next -> trace_ip
397 defer trace.user ( IP -- stop? )
400 : gd { more_levels | stop_level -- }
401 here what's trace.user u< \ has it been forgotten?
403 ." Resetting TRACE.USER !!!" cr
409 or \ 19990930 - OR was missing
411 ." GD level out of range (0-10), = " more_levels . cr
413 trace_level more_levels + -> trace_level_max
414 trace_level 1- -> stop_level
416 trace_ip trace.user \ call deferred user word
417 ?dup \ leave flag for UNTIL \ 19990930 - was DUP
419 ." TRACE.USER returned " dup . ." so stopping execution." cr
421 trace_ip trace.next -> trace_ip
422 trace_level stop_level > not
428 : g ( -- , execute until end of word )
433 ." TRACE ( i*x <name> -- , setup trace for Forth word )" cr
434 ." S ( -- , step over )" cr
435 ." SM ( many -- , step over many times )" cr
436 ." SD ( -- , step down )" cr
437 ." G ( -- , go to end of word )" cr
438 ." GD ( n -- , go down N levels from current level," cr
439 ." stop at end of this level )" cr
447 : FOO dup IF 1 + . THEN 77 var1 @ + . ;
448 : ZOO 29 foo 99 22 + . ;
449 : ROO 92 >r 1 r@ + . r> . ;
450 : MOO c" hello" count type
451 ." This is a message." cr
452 s" another message" type cr
454 : KOO 7 FOO ." DONE" ;
455 : TR.DO 4 0 DO i . LOOP ;
456 : TR.?DO 0 ?DO i . LOOP ;
457 : TR.LOC1 { aa bb } aa bb + . ;
458 : TR.LOC2 789 >r 4 5 tr.loc1 r> . ;