PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/fth/utils/trace.fth

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