PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/test/hrc/forth/trace.fth

https://github.com/mediogre/colorite
Forth | 409 lines | 378 code | 31 blank | 0 comment | 10 complexity | e7c48f3ec8bde272227710d996dd5b95 MD5 | raw file
  1. \ @(#) trace.fth 98/01/28 1.2
  2. \ TRACE ( <name> -- , trace pForth word )
  3. \
  4. \ Single step debugger.
  5. \ TRACE ( i*x <name> -- , setup trace for Forth word )
  6. \ S ( -- , step over )
  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 )
  11. \
  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
  18. \ tested.
  19. \
  20. \ Copyright 1997 Phil Burk
  21. anew task-trace.fth
  22. : SPACE.TO.COLUMN ( col -- ) out @ - spaces ;
  23. : IS.PRIMITIVE? ( xt -- flag , true if kernel primitive )
  24. ['] first_colon < ;
  25. 0 value TRACE-BEGWORD_IP \ Instruction pointer begining of words
  26. 0 value TRACE_IP \ instruction pointer
  27. 0 value TRACE_LEVEL \ level of descent for inner interpreter
  28. 0 value TRACE_LEVEL_MAX \ maximum level of descent
  29. private{
  30. \ use fake return stack
  31. 128 cells constant TRACE_RETURN_SIZE \ size of return stack in bytes
  32. create TRACE-RETURN-STACK TRACE_RETURN_SIZE 16 + allot
  33. variable TRACE-RSP
  34. : TRACE.>R ( n -- ) trace-rsp @ cell- dup trace-rsp ! ! ; \ *(--rsp) = n
  35. : TRACE.R> ( -- n ) trace-rsp @ dup @ swap cell+ trace-rsp ! ; \ n = *rsp++
  36. : TRACE.R@ ( -- n ) trace-rsp @ @ ; \ n = *rsp
  37. : TRACE.RPICK ( index -- n ) cells trace-rsp @ + @ ; \ n = rsp[index]
  38. : TRACE.0RP ( -- n ) trace-return-stack trace_return_size + 8 + trace-rsp ! ;
  39. : TRACE.RDROP ( -- ) cell trace-rsp +! ;
  40. : TRACE.RCHECK ( -- , abort if return stack out of range )
  41. trace-rsp @ trace-return-stack u<
  42. abort" TRACE return stack OVERFLOW!"
  43. trace-rsp @ trace-return-stack trace_return_size + 12 + u>
  44. abort" TRACE return stack UNDERFLOW!" ;
  45. \ save and restore several state variables
  46. 10 cells constant TRACE_STATE_SIZE
  47. create TRACE-STATE-1 TRACE_STATE_SIZE allot
  48. create TRACE-STATE-2 TRACE_STATE_SIZE allot
  49. variable TRACE-STATE-PTR
  50. : TRACE.SAVE++ ( addr -- , save next thing )
  51. @ trace-state-ptr @ !
  52. cell trace-state-ptr +! ;
  53. : TRACE.SAVE.STATE ( -- )
  54. state trace.save++
  55. hld trace.save++
  56. base trace.save++ ;
  57. : TRACE.SAVE.STATE1 ( -- , save normal state )
  58. trace-state-1 trace-state-ptr !
  59. trace.save.state ;
  60. : TRACE.SAVE.STATE2 ( -- , save state of word being debugged )
  61. trace-state-2 trace-state-ptr !
  62. trace.save.state ;
  63. : TRACE.RESTORE++ ( addr -- , restore next thing )
  64. trace-state-ptr @ @ swap !
  65. cell trace-state-ptr +! ;
  66. : TRACE.RESTORE.STATE ( -- )
  67. state trace.restore++
  68. hld trace.restore++
  69. base trace.restore++ ;
  70. : TRACE.RESTORE.STATE1 ( -- )
  71. trace-state-1 trace-state-ptr !
  72. trace.restore.state ;
  73. : TRACE.RESTORE.STATE2 ( -- )
  74. trace-state-2 trace-state-ptr !
  75. trace.restore.state ;
  76. \ The implementation of these pForth primitives is specific to pForth.
  77. variable TRACE-LOCALS-PTR \ point to top of local frame
  78. \ create a return stack frame for NUM local variables
  79. : TRACE.(LOCAL.ENTRY) ( x0 x1 ... xn n -- ) { num | lp -- nothink }
  80. trace-locals-ptr @ trace.>r
  81. trace-rsp @ trace-locals-ptr !
  82. trace-rsp @ num cells - trace-rsp ! \ make room for locals
  83. trace-rsp @ -> lp
  84. num 0
  85. DO lp ! cell +-> lp \ move data into locals frame on return stack
  86. LOOP ;
  87. : TRACE.(LOCAL.EXIT) ( -- )
  88. trace-locals-ptr @ trace-rsp !
  89. trace.r> trace-locals-ptr ! ;
  90. : TRACE.(LOCAL@) ( l# -- n , fetch from local frame )
  91. trace-locals-ptr @ swap cells - @ ;
  92. : TRACE.(1_LOCAL@) ( -- n ) 1 trace.(local@) ;
  93. : TRACE.(2_LOCAL@) ( -- n ) 2 trace.(local@) ;
  94. : TRACE.(3_LOCAL@) ( -- n ) 3 trace.(local@) ;
  95. : TRACE.(4_LOCAL@) ( -- n ) 4 trace.(local@) ;
  96. : TRACE.(5_LOCAL@) ( -- n ) 5 trace.(local@) ;
  97. : TRACE.(6_LOCAL@) ( -- n ) 6 trace.(local@) ;
  98. : TRACE.(7_LOCAL@) ( -- n ) 7 trace.(local@) ;
  99. : TRACE.(8_LOCAL@) ( -- n ) 8 trace.(local@) ;
  100. : TRACE.(LOCAL!) ( n l# -- , store into local frame )
  101. trace-locals-ptr @ swap cells - ! ;
  102. : TRACE.(1_LOCAL!) ( -- n ) 1 trace.(local!) ;
  103. : TRACE.(2_LOCAL!) ( -- n ) 2 trace.(local!) ;
  104. : TRACE.(3_LOCAL!) ( -- n ) 3 trace.(local!) ;
  105. : TRACE.(4_LOCAL!) ( -- n ) 4 trace.(local!) ;
  106. : TRACE.(5_LOCAL!) ( -- n ) 5 trace.(local!) ;
  107. : TRACE.(6_LOCAL!) ( -- n ) 6 trace.(local!) ;
  108. : TRACE.(7_LOCAL!) ( -- n ) 7 trace.(local!) ;
  109. : TRACE.(8_LOCAL!) ( -- n ) 8 trace.(local!) ;
  110. : TRACE.(LOCAL+!) ( n l# -- , store into local frame )
  111. trace-locals-ptr @ swap cells - +! ;
  112. : TRACE.(?DO) { limit start ip -- ip' }
  113. limit start =
  114. IF ip @ +-> ip \ BRANCH
  115. ELSE
  116. start trace.>r
  117. limit trace.>r
  118. cell +-> ip
  119. THEN ip ;
  120. : TRACE.(LOOP) { ip | limit indx -- ip' }
  121. trace.r> -> limit
  122. trace.r> 1+ -> indx
  123. limit indx =
  124. IF
  125. cell +-> ip
  126. ELSE
  127. indx trace.>r
  128. limit trace.>r
  129. ip @ +-> ip
  130. THEN
  131. ip ;
  132. : TRACE.(+LOOP) { delta ip | limit indx oldindx -- ip' }
  133. trace.r> -> limit
  134. trace.r> -> oldindx
  135. oldindx delta + -> indx
  136. \ /* Do indices cross boundary between LIMIT-1 and LIMIT ? */
  137. \ if( ( (OldIndex - Limit) & ((Limit-1) - NewIndex) & 0x80000000 ) ||
  138. \ ( (NewIndex - Limit) & ((Limit-1) - OldIndex) & 0x80000000 ) )
  139. oldindx limit - limit 1- indx - AND $ 80000000 AND
  140. indx limit - limit 1- oldindx - AND $ 80000000 AND OR
  141. IF
  142. cell +-> ip
  143. ELSE
  144. indx trace.>r
  145. limit trace.>r
  146. ip @ +-> ip
  147. THEN ip ;
  148. : TRACE.CHECK.IP { ip -- }
  149. ip ['] first_colon u<
  150. ip here u> OR
  151. IF
  152. ." TRACE - IP out of range = " ip .hex cr abort
  153. THEN ;
  154. : TRACE.SHOW.IP { ip -- , print name and offset }
  155. ip ( >code code> ) ( code@ ) name> >name dup id.
  156. name> ip code> swap - 4 / ." +" .dec ." 's words" ;
  157. : TRACE.SHOW.STACK { | mdepth -- }
  158. base @ >r
  159. ." <" base @ decimal 1 .r ." :"
  160. depth 1 .r ." > "
  161. r> base !
  162. depth 5 min -> mdepth
  163. depth mdepth -
  164. IF
  165. ." ... " \ if we don't show entire stack
  166. THEN
  167. mdepth 0
  168. ?DO
  169. mdepth i 1+ - pick . \ show numbers in current base
  170. LOOP ;
  171. : TRACE.SHOW.NEXT { ip -- }
  172. >newline
  173. ip trace.check.ip
  174. \ show word name and offset
  175. ." << "
  176. trace_ip trace.show.ip
  177. 30 space.to.column
  178. \ show data stack
  179. trace.show.stack
  180. 80 space.to.column ." || Next word - "
  181. trace_level 2* spaces
  182. ip code@
  183. cell +-> ip
  184. \ show primitive about to be executed
  185. dup .xt space
  186. \ trap any primitives that are followed by inline data
  187. CASE
  188. ['] (LITERAL) OF ip @ dup . space 255 and emit ENDOF
  189. ['] (ALITERAL) OF ip a@ dup . space 255 and emit ENDOF
  190. [ exists? (FLITERAL) [IF] ]
  191. ['] (FLITERAL) OF ip f@ f. ENDOF
  192. [ [THEN] ]
  193. ['] BRANCH OF ip @ 4 / . ENDOF
  194. ['] 0BRANCH OF ip @ 4 / . ENDOF
  195. ['] (.") OF ip count type .' "' ENDOF
  196. ['] (C") OF ip count type .' "' ENDOF
  197. ['] (S") OF ip count type .' "' ENDOF
  198. ENDCASE
  199. 100 space.to.column ." >> " ;
  200. : TRACE.DO.PRIMITIVE { ip xt | oldhere -- ip' , perform code at ip }
  201. xt
  202. CASE
  203. 0 OF -1 +-> trace_level trace.r> -> ip ENDOF \ EXIT
  204. ['] (CREATE) OF ip cell- body_offset + ENDOF
  205. ['] (LITERAL) OF ip @ cell +-> ip ENDOF
  206. ['] (ALITERAL) OF ip a@ cell +-> ip ENDOF
  207. [ exists? (FLITERAL) [IF] ]
  208. ['] (FLITERAL) OF ip f@ 1 floats +-> ip ENDOF
  209. [ [THEN] ]
  210. ['] BRANCH OF ip @ +-> ip ENDOF
  211. ['] 0BRANCH OF 0= IF ip @ +-> ip ELSE cell +-> ip THEN ENDOF
  212. ['] >R OF trace.>r ENDOF
  213. ['] R> OF trace.r> ENDOF
  214. ['] R@ OF trace.r@ ENDOF
  215. ['] RDROP OF trace.rdrop ENDOF
  216. ['] 2>R OF trace.>r trace.>r ENDOF
  217. ['] 2R> OF trace.r> trace.r> ENDOF
  218. ['] 2R@ OF trace.r@ 1 trace.rpick ENDOF
  219. ['] i OF 1 trace.rpick ENDOF
  220. ['] j OF 3 trace.rpick ENDOF
  221. ['] (LEAVE) OF trace.rdrop trace.rdrop ip @ +-> ip ENDOF
  222. ['] (LOOP) OF ip trace.(loop) -> ip ENDOF
  223. ['] (+LOOP) OF ip trace.(+loop) -> ip ENDOF
  224. ['] (DO) OF trace.>r trace.>r ENDOF
  225. ['] (?DO) OF ip trace.(?do) -> ip ENDOF
  226. ['] (.") OF ip count type ip count + aligned -> ip ENDOF
  227. ['] (C") OF ip ip count + aligned -> ip ENDOF
  228. ['] (S") OF ip count ip count + aligned -> ip ENDOF
  229. ['] (LOCAL.ENTRY) OF trace.(local.entry) ENDOF
  230. ['] (LOCAL.EXIT) OF trace.(local.exit) ENDOF
  231. ['] (LOCAL@) OF trace.(local@) ENDOF
  232. ['] (1_LOCAL@) OF trace.(1_local@) ENDOF
  233. ['] (2_LOCAL@) OF trace.(2_local@) ENDOF
  234. ['] (3_LOCAL@) OF trace.(3_local@) ENDOF
  235. ['] (4_LOCAL@) OF trace.(4_local@) ENDOF
  236. ['] (5_LOCAL@) OF trace.(5_local@) ENDOF
  237. ['] (6_LOCAL@) OF trace.(6_local@) ENDOF
  238. ['] (7_LOCAL@) OF trace.(7_local@) ENDOF
  239. ['] (8_LOCAL@) OF trace.(8_local@) ENDOF
  240. ['] (LOCAL!) OF trace.(local!) ENDOF
  241. ['] (1_LOCAL!) OF trace.(1_local!) ENDOF
  242. ['] (2_LOCAL!) OF trace.(2_local!) ENDOF
  243. ['] (3_LOCAL!) OF trace.(3_local!) ENDOF
  244. ['] (4_LOCAL!) OF trace.(4_local!) ENDOF
  245. ['] (5_LOCAL!) OF trace.(5_local!) ENDOF
  246. ['] (6_LOCAL!) OF trace.(6_local!) ENDOF
  247. ['] (7_LOCAL!) OF trace.(7_local!) ENDOF
  248. ['] (8_LOCAL!) OF trace.(8_local!) ENDOF
  249. ['] (LOCAL+!) OF trace.(local+!) ENDOF
  250. >r xt EXECUTE r>
  251. ENDCASE
  252. ip ;
  253. : TRACE.DO.NEXT { ip | xt oldhere -- ip' , perform code at ip }
  254. ip trace.check.ip
  255. \ set context for word under test
  256. trace.save.state1
  257. here -> oldhere
  258. trace.restore.state2
  259. oldhere 256 + dp !
  260. \ get execution token
  261. ip code@ -> xt
  262. cell +-> ip
  263. \ execute token
  264. xt is.primitive?
  265. IF \ primitive
  266. ip xt trace.do.primitive -> ip
  267. ELSE \ secondary
  268. trace_level trace_level_max <
  269. IF
  270. ip trace.>r \ threaded execution
  271. 1 +-> trace_level
  272. xt codebase + -> ip
  273. ELSE
  274. \ treat it as a primitive
  275. ip xt trace.do.primitive -> ip
  276. THEN
  277. THEN
  278. \ restore original context
  279. trace.rcheck
  280. trace.save.state2
  281. trace.restore.state1
  282. oldhere dp !
  283. ip ;
  284. : TRACE.NEXT { ip | xt -- ip' }
  285. trace_level 0>
  286. IF
  287. ip trace.do.next -> ip
  288. THEN
  289. trace_level 0>
  290. IF
  291. ip trace.show.next
  292. ELSE
  293. ." Finished." cr
  294. THEN
  295. ip
  296. ;
  297. }private
  298. : TRACE ( i*x <name> -- i*x , setup trace environment )
  299. ' dup is.primitive?
  300. IF
  301. drop ." Sorry. You can't trace a primitive." cr
  302. ELSE
  303. 1 -> trace_level
  304. trace_level -> trace_level_max
  305. trace.0rp
  306. dup -> TRACE-BEGWORD_IP
  307. >code -> trace_ip
  308. trace_ip trace.show.next
  309. trace-stack off
  310. trace.save.state2
  311. THEN ;
  312. : s ( -- , step over )
  313. trace_level -> trace_level_max
  314. trace_ip trace.next -> trace_ip ;
  315. : sd ( -- , step down )
  316. trace_level 1+ -> trace_level_max
  317. trace_ip trace.next -> trace_ip ;
  318. : sm ( many -- , step many times )
  319. trace_level -> trace_level_max
  320. 0
  321. ?DO
  322. trace_ip trace.next -> trace_ip
  323. LOOP ;
  324. defer trace.user ( IP -- stop? )
  325. ' 0= is trace.user
  326. : gd { more_levels | stop_level userflag -- }
  327. here what's trace.user u< \ has it been forgotten?
  328. IF
  329. ." Resetting TRACE.USER !!!" cr
  330. ['] 0= is trace.user
  331. THEN
  332. more_levels 0<
  333. more_levels 10 >
  334. IF
  335. ." GD level out of range (0-10), = " more_levels . cr
  336. ELSE
  337. trace_level more_levels + -> trace_level_max
  338. trace_level 1- -> stop_level
  339. BEGIN
  340. trace_ip trace.user \ call deferred user word
  341. dup \ leave flag for UNTIL
  342. IF
  343. ." TRACE.USER returned " dup . ." so stopping execution." cr
  344. ELSE
  345. -> userflag drop
  346. \ cr ." before=" .s
  347. trace_ip
  348. trace.next
  349. -> trace_ip
  350. \ ." after=" .s key drop
  351. userflag
  352. trace_level stop_level > not
  353. THEN
  354. UNTIL
  355. drop
  356. THEN ;
  357. : g ( -- , execute until end of word ) 0 gd ;
  358. : TRACE.HELP ( -- )
  359. ." TRACE ( i*x <name> -- , setup trace for Forth word )" cr
  360. ." S ( -- , step over )" cr
  361. ." SM ( many -- , step over many times )" cr
  362. ." SD ( -- , step down )" cr
  363. ." G ( -- , go to end of word )" cr
  364. ." GD ( n -- , go down N levels from current level," cr
  365. ." stop at end of this level )" cr ;
  366. privatize
  367. 0 [IF]
  368. variable var1 100 var1 !
  369. : FOO dup IF 1 + . THEN 77 var1 @ + . ;
  370. : ZOO 29 foo 99 22 + . ;
  371. : ROO 92 >r 1 r@ + . r> . ;
  372. : MOO c" hello" count type ." This is a message." cr s" another message" type cr ;
  373. : KOO 7 FOO ." DONE" ;
  374. : TR.DO 4 0 DO i . LOOP ;
  375. : TR.?DO 0 ?DO i . LOOP ;
  376. : TR.LOC1 { aa bb } aa bb + . ;
  377. : TR.LOC2 789 >r 4 5 tr.loc1 r> . ;
  378. [THEN]