PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/qemu/roms/SLOF/slof/fs/debug.fs

https://github.com/javarange/dpdk-ovs
F# | 422 lines | 366 code | 56 blank | 0 comment | 8 complexity | 8634a4842a22f9364ef01d14a9a69bcc MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-2.0, LGPL-2.1, BSD-3-Clause, GPL-3.0
  1. \ *****************************************************************************
  2. \ * Copyright (c) 2004, 2008 IBM Corporation
  3. \ * All rights reserved.
  4. \ * This program and the accompanying materials
  5. \ * are made available under the terms of the BSD License
  6. \ * which accompanies this distribution, and is available at
  7. \ * http://www.opensource.org/licenses/bsd-license.php
  8. \ *
  9. \ * Contributors:
  10. \ * IBM Corporation - initial implementation
  11. \ ****************************************************************************/
  12. \ Get the name of Forth command whose execution token is xt
  13. : xt>name ( xt -- str len )
  14. BEGIN
  15. cell - dup c@ 0 2 within IF
  16. dup 2+ swap 1+ c@ exit
  17. THEN
  18. AGAIN
  19. ;
  20. cell -1 * CONSTANT -cell
  21. : cell- ( n -- n-cell-size )
  22. [ cell -1 * ] LITERAL +
  23. ;
  24. \ Search for xt of given address
  25. : find-xt-addr ( addr -- xt )
  26. BEGIN
  27. dup @ <colon> = IF
  28. EXIT
  29. THEN
  30. cell-
  31. AGAIN
  32. ;
  33. : (.immediate) ( xt -- )
  34. \ is it immediate?
  35. xt>name drop 2 - c@ \ skip len and flags
  36. immediate? IF
  37. ." IMMEDIATE"
  38. THEN
  39. ;
  40. : (.xt) ( xt -- )
  41. xt>name type
  42. ;
  43. \ Trace back on current return stack.
  44. \ Start at 1, since 0 is return of trace-back itself
  45. : trace-back ( )
  46. 1
  47. BEGIN
  48. cr dup dup . ." : " rpick dup . ." : "
  49. ['] tib here within IF
  50. dup rpick find-xt-addr (.xt)
  51. THEN
  52. 1+ dup rdepth 5 - >= IF cr drop EXIT THEN
  53. AGAIN
  54. ;
  55. VARIABLE see-my-type-column
  56. : (see-my-type) ( indent limit xt str len -- indent limit xt )
  57. dup see-my-type-column @ + dup 50 >= IF
  58. -rot over " " comp 0= IF
  59. \ blank causes overflow: just enforce new line with next call
  60. 2drop see-my-type-column !
  61. ELSE
  62. rot drop ( indent limit xt str len )
  63. \ Need to copy string since we use (u.) again (kills internal buffer):
  64. pocket swap 2dup >r >r ( indent limit xt str pk len R: len pk )
  65. move r> r> ( indent limit xt pk len )
  66. 2 pick (u.) dup -rot
  67. cr type ( indent limit xt pk len xt-len )
  68. " :" type 1+ ( indent limit xt pk len prefix-len )
  69. 5 pick dup spaces + ( indent limit xt pk len prefix-len )
  70. over + see-my-type-column ! ( indent limit xt pk len )
  71. type
  72. THEN ( indent limit xt )
  73. ELSE
  74. see-my-type-column ! type ( indent limit xt )
  75. THEN
  76. ;
  77. : (see-my-type-init) ( -- )
  78. ffff see-my-type-column ! \ just enforce a new line
  79. ;
  80. : (see-colon-body) ( indent limit xt -- indent limit xt )
  81. (see-my-type-init) \ enforce new line
  82. BEGIN ( indent limit xt )
  83. cell+ 2dup <>
  84. over @
  85. dup <semicolon> <>
  86. rot and ( indent limit xt @xt flag )
  87. WHILE ( indent limit xt @xt )
  88. xt>name (see-my-type) " " (see-my-type)
  89. dup @ ( indent limit xt @xt)
  90. CASE
  91. <0branch> OF cell+ dup @
  92. over + cell+ dup >r
  93. (u.) (see-my-type) r> ( indent limit xt target)
  94. 2dup < IF
  95. over 4 pick 3 + -rot recurse
  96. nip nip nip cell- ( indent limit xt )
  97. ELSE
  98. drop ( indent limit xt )
  99. THEN
  100. (see-my-type-init) ENDOF \ enforce new line
  101. <branch> OF cell+ dup @ over + cell+ (u.)
  102. (see-my-type) " " (see-my-type) ENDOF
  103. <do?do> OF cell+ dup @ (u.) (see-my-type)
  104. " " (see-my-type) ENDOF
  105. <lit> OF cell+ dup @ (u.) (see-my-type)
  106. " " (see-my-type) ENDOF
  107. <dotick> OF cell+ dup @ xt>name (see-my-type)
  108. " " (see-my-type) ENDOF
  109. <doloop> OF cell+ dup @ (u.) (see-my-type)
  110. " " (see-my-type) ENDOF
  111. <do+loop> OF cell+ dup @ (u.) (see-my-type)
  112. " " (see-my-type) ENDOF
  113. <doleave> OF cell+ dup @ over + cell+ (u.)
  114. (see-my-type) " " (see-my-type) ENDOF
  115. <do?leave> OF cell+ dup @ over + cell+ (u.)
  116. (see-my-type) " " (see-my-type) ENDOF
  117. <sliteral> OF cell+ " """ (see-my-type) dup count dup >r
  118. (see-my-type) " """ (see-my-type)
  119. " " (see-my-type)
  120. r> -cell and + ENDOF
  121. ENDCASE
  122. REPEAT
  123. drop
  124. ;
  125. : (see-colon) ( xt -- )
  126. (see-my-type-init)
  127. 1 swap 0 swap ( indent limit xt )
  128. " : " (see-my-type) dup xt>name (see-my-type)
  129. rot drop 4 -rot (see-colon-body) ( indent limit xt )
  130. rot drop 1 -rot (see-my-type-init) " ;" (see-my-type)
  131. 3drop
  132. ;
  133. \ Create words are a bit tricky. We find out where their code points.
  134. \ If this code is part of SLOF, it is not a user generated CREATE.
  135. : (see-create) ( xt -- )
  136. dup cell+ @
  137. CASE
  138. <2constant> OF
  139. dup cell+ cell+ dup @ swap cell+ @ . . ." 2CONSTANT "
  140. ENDOF
  141. <instancevalue> OF
  142. dup cell+ cell+ @ . ." INSTANCE VALUE "
  143. ENDOF
  144. <instancevariable> OF
  145. ." INSTANCE VARIABLE "
  146. ENDOF
  147. dup OF
  148. ." CREATE "
  149. ENDOF
  150. ENDCASE
  151. (.xt)
  152. ;
  153. \ Decompile Forth command whose execution token is xt
  154. : (see) ( xt -- )
  155. cr dup dup @
  156. CASE
  157. <variable> OF ." VARIABLE " (.xt) ENDOF
  158. <value> OF dup execute . ." VALUE " (.xt) ENDOF
  159. <constant> OF dup execute . ." CONSTANT " (.xt) ENDOF
  160. <defer> OF dup cell+ @ swap ." DEFER " (.xt) ." is " (.xt) ENDOF
  161. <alias> OF dup cell+ @ swap ." ALIAS " (.xt) ." " (.xt) ENDOF
  162. <buffer:> OF ." BUFFER: " (.xt) ENDOF
  163. <create> OF (see-create) ENDOF
  164. <colon> OF (see-colon) ENDOF
  165. dup OF ." ??? PRIM " (.xt) ENDOF
  166. ENDCASE
  167. (.immediate) cr
  168. ;
  169. \ Decompile Forth command old-name
  170. : see ( "old-name<>" -- )
  171. ' (see)
  172. ;
  173. \ Work in progress...
  174. 0 value forth-ip
  175. true value trace>stepping?
  176. true value trace>print?
  177. true value trace>up?
  178. 0 value trace>depth
  179. 0 value trace>rdepth
  180. 0 value trace>recurse
  181. : trace-depth+ ( -- ) trace>depth 1+ to trace>depth ;
  182. : trace-depth- ( -- ) trace>depth 1- to trace>depth ;
  183. : stepping ( -- )
  184. true to trace>stepping?
  185. ;
  186. : tracing ( -- )
  187. false to trace>stepping?
  188. ;
  189. : trace-print-on ( -- )
  190. true to trace>print?
  191. ;
  192. : trace-print-off ( -- )
  193. false to trace>print?
  194. ;
  195. \ Add n to ip
  196. : fip-add ( n -- )
  197. forth-ip + to forth-ip
  198. ;
  199. \ Save execution token address and content
  200. 0 value debug-last-xt
  201. 0 value debug-last-xt-content
  202. : trace-print ( -- )
  203. forth-ip cr u. ." : "
  204. forth-ip @
  205. dup ['] breakpoint = IF drop debug-last-xt-content THEN
  206. xt>name type ." "
  207. ." ( " .s ." ) | "
  208. ;
  209. : trace-interpret ( -- )
  210. rdepth 1- to trace>rdepth
  211. BEGIN
  212. depth . [char] > dup emit emit space
  213. source expect ( str len )
  214. ['] interpret catch print-status
  215. AGAIN
  216. ;
  217. \ Main trace routine, trace a colon definition
  218. : trace-xt ( xt -- )
  219. trace>recurse IF
  220. r> drop \ Drop return of 'trace-xt call
  221. cell+ \ Step over ":"
  222. ELSE
  223. debug-last-xt-content <colon> = IF
  224. \ debug colon-definition
  225. ['] breakpoint @ debug-last-xt ! \ Re-arm break point
  226. r> drop \ Drop return of 'trace-xt call
  227. cell+ \ Step over ":"
  228. ELSE
  229. ['] breakpoint debug-last-xt ! \ Re-arm break point
  230. 2r> 2drop
  231. THEN
  232. THEN
  233. to forth-ip
  234. true to trace>print?
  235. BEGIN
  236. trace>print? IF trace-print THEN
  237. forth-ip ( ip )
  238. trace>stepping? IF
  239. BEGIN
  240. key
  241. CASE
  242. [char] d OF dup @ @ <colon> = IF \ recurse only into colon definitions
  243. trace-depth+
  244. 1 to trace>recurse
  245. dup >r @ recurse
  246. THEN true ENDOF
  247. [char] u OF trace>depth IF tracing trace-print-off true ELSE false THEN ENDOF
  248. [char] f OF drop cr trace-interpret ENDOF \ quit trace and start interpreter FIXME rstack
  249. [char] c OF tracing true ENDOF
  250. [char] t OF trace-back false ENDOF
  251. [char] q OF drop cr quit ENDOF
  252. 20 OF true ENDOF
  253. dup OF cr ." Press d: Down into current word" cr
  254. ." Press u: Up to caller" cr
  255. ." Press f: Switch to forth interpreter, 'resume' will continue tracing" cr
  256. ." Press c: Switch to tracing" cr
  257. ." Press <space>: Execute current word" cr
  258. ." Press q: Abort execution, switch to interpreter" cr
  259. false ENDOF
  260. ENDCASE
  261. UNTIL
  262. THEN ( ip' )
  263. dup to forth-ip @ ( xt )
  264. dup ['] breakpoint = IF drop debug-last-xt-content THEN
  265. dup ( xt xt )
  266. CASE
  267. <sliteral> OF drop forth-ip cell+ dup dup c@ + -cell and to forth-ip ENDOF
  268. <dotick> OF drop forth-ip cell+ @ cell fip-add ENDOF
  269. <lit> OF drop forth-ip cell+ @ cell fip-add ENDOF
  270. <doto> OF drop forth-ip cell+ @ cell+ ! cell fip-add ENDOF
  271. <(doito)> OF drop forth-ip cell+ @ cell+ cell+ @ >instance ! cell fip-add ENDOF
  272. <0branch> OF drop IF
  273. cell fip-add
  274. ELSE
  275. forth-ip cell+ @ cell+ fip-add THEN
  276. ENDOF
  277. <do?do> OF drop 2dup <> IF
  278. swap >r >r cell fip-add
  279. ELSE
  280. forth-ip cell+ @ cell+ fip-add 2drop THEN
  281. ENDOF
  282. <branch> OF drop forth-ip cell+ @ cell+ fip-add ENDOF
  283. <doleave> OF drop r> r> 2drop forth-ip cell+ @ cell+ fip-add ENDOF
  284. <do?leave> OF drop IF
  285. r> r> 2drop forth-ip cell+ @ cell+ fip-add
  286. ELSE
  287. cell fip-add
  288. THEN
  289. ENDOF
  290. <doloop> OF drop r> 1+ r> 2dup = IF
  291. 2drop cell fip-add
  292. ELSE >r >r
  293. forth-ip cell+ @ cell+ fip-add THEN
  294. ENDOF
  295. <do+loop> OF drop r> + r> 2dup >= IF
  296. 2drop cell fip-add
  297. ELSE >r >r
  298. forth-ip cell+ @ cell+ fip-add THEN
  299. ENDOF
  300. <semicolon> OF trace>depth 0> IF
  301. trace-depth- 1 to trace>recurse
  302. stepping drop r> recurse
  303. ELSE
  304. drop exit THEN
  305. ENDOF
  306. <exit> OF trace>depth 0> IF
  307. trace-depth- stepping drop r> recurse
  308. ELSE
  309. drop exit THEN
  310. ENDOF
  311. dup OF execute ENDOF
  312. ENDCASE
  313. forth-ip cell+ to forth-ip
  314. AGAIN
  315. ;
  316. \ Resume execution from tracer
  317. : resume ( -- )
  318. trace>rdepth rdepth!
  319. forth-ip cell - trace-xt
  320. ;
  321. \ Turn debug off, by erasing breakpoint
  322. : debug-off ( -- )
  323. debug-last-xt IF
  324. debug-last-xt-content debug-last-xt ! \ Restore overwriten token
  325. 0 to debug-last-xt
  326. THEN
  327. ;
  328. \ Entry point for debug
  329. : (break-entry) ( -- )
  330. debug-last-xt dup @ ['] breakpoint <> swap ( debug-addr? debug-last-xt )
  331. debug-last-xt-content swap ! \ Restore overwriten token
  332. r> drop \ Don't return to bp, but to caller
  333. debug-last-xt-content <colon> <> and IF \ Execute non colon definition
  334. debug-last-xt cr u. ." : "
  335. debug-last-xt xt>name type ." "
  336. ." ( " .s ." ) | "
  337. key drop
  338. debug-last-xt execute
  339. ELSE
  340. debug-last-xt 0 to trace>depth 0 to trace>recurse trace-xt \ Trace colon definition
  341. THEN
  342. ;
  343. \ Put entry point bp defer
  344. ' (break-entry) to BP
  345. \ Mark an address for debugging
  346. : debug-address ( addr -- )
  347. debug-off ( xt ) \ Remove active breakpoint
  348. dup to debug-last-xt ( xt ) \ Save token for later debug
  349. dup @ to debug-last-xt-content ( xt ) \ Save old value
  350. ['] breakpoint swap !
  351. ;
  352. \ Mark the command indicated by xt for debugging
  353. : (debug ( xt -- )
  354. debug-off ( xt ) \ Remove active breakpoint
  355. dup to debug-last-xt ( xt ) \ Save token for later debug
  356. dup @ to debug-last-xt-content ( xt ) \ Save old value
  357. ['] breakpoint @ swap !
  358. ;
  359. \ Mark the command indicated by xt for debugging
  360. : debug ( "old-name<>" -- )
  361. parse-word $find IF \ Get xt for old-name
  362. (debug
  363. ELSE
  364. ." undefined word " type cr
  365. THEN
  366. ;