[gforth] / gforth / debug.fs  

gforth: gforth/debug.fs


1 : anton 1.1 \ DEBUG.FS Debugger 12jun93jaw
2 :    
3 : anton 1.9 \ Copyright (C) 1995,1996,1997 Free Software Foundation, Inc.
4 : anton 1.4
5 :     \ This file is part of Gforth.
6 :    
7 :     \ Gforth is free software; you can redistribute it and/or
8 :     \ modify it under the terms of the GNU General Public License
9 :     \ as published by the Free Software Foundation; either version 2
10 :     \ of the License, or (at your option) any later version.
11 :    
12 :     \ This program is distributed in the hope that it will be useful,
13 :     \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14 :     \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 :     \ GNU General Public License for more details.
16 :    
17 :     \ You should have received a copy of the GNU General Public License
18 :     \ along with this program; if not, write to the Free Software
19 :     \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 :    
21 : anton 1.1 decimal
22 :    
23 : jwilke 1.8 VARIABLE dbg-ip \ istruction pointer for debugger
24 : anton 1.1
25 :     : scanword ( body -- )
26 :     c-init C-Output off
27 :     ScanMode c-pass !
28 :     dup MakePass
29 :     0 Level !
30 :     0 XPos !
31 :     DisplayMode c-pass !
32 :     MakePass
33 :     C-Output on ;
34 :    
35 :     : .n 0 <# # # # # #S #> ctype bl cemit ;
36 :    
37 :     : d.s ." [ " depth . ." ] "
38 :     depth 4 min dup 0 ?DO dup i - pick .n LOOP drop ;
39 :    
40 :     : NoFine XPos off YPos off
41 :     NLFlag off Level off
42 :     C-Formated off
43 :     ;
44 :    
45 :     : disp-step
46 :     DisplayMode c-pass ! \ change to displaymode
47 :     cr
48 :     c-stop off
49 : jwilke 1.8 Base @ hex dbg-ip @ 8 u.r space dbg-ip @ @ 8 u.r space
50 : anton 1.1 Base !
51 :     NoFine 10 XPos !
52 : jwilke 1.8 dbg-ip @ DisplayMode c-pass ! Analyse drop
53 : anton 1.1 25 XPos @ - 0 max spaces ." -> " ;
54 :    
55 :     : get-next ( -- n | n n )
56 :     DebugMode c-pass !
57 : jwilke 1.8 dbg-ip @ Analyse ;
58 : anton 1.1
59 :     : jump ( addr -- )
60 :     r> drop \ discard last ip
61 :     >r ;
62 :    
63 :     AVARIABLE DebugLoop
64 :    
65 : jwilke 1.8 : breaker r> 1 cells - dbg-ip ! DebugLoop @ jump ;
66 : anton 1.1
67 :     CREATE BP 0 , 0 ,
68 :     CREATE DT 0 , 0 ,
69 :    
70 :     : set-bp ( 0 n | 0 n n -- )
71 :     0. BP 2!
72 :     ?dup IF dup BP ! dup @ DT !
73 :     ['] Breaker swap !
74 :     ?dup IF dup BP cell+ ! dup @ DT cell+ !
75 :     ['] Breaker swap ! drop THEN
76 :     THEN ;
77 :    
78 :     : restore-bp ( -- )
79 :     BP @ ?dup IF DT @ swap ! THEN
80 :     BP cell+ @ ?dup IF DT cell+ @ swap ! THEN ;
81 :    
82 :     VARIABLE Body
83 :    
84 :     : NestXT ( xt -- true | body false )
85 : jwilke 1.10 \ special deal for create does> words
86 :     \ leaves body address on the stack
87 :     dup >does-code IF dup >body swap THEN
88 :    
89 : anton 1.1 DebugMode c-pass ! C-Output off
90 : anton 1.5 xt-see C-Output on
91 : anton 1.1 c-pass @ DebugMode = dup
92 :     IF ." Cannot debug" cr
93 :     THEN ;
94 :    
95 :     VARIABLE Nesting
96 :    
97 :     : Leave-D
98 :     C-Formated on
99 :     C-Output on ;
100 :    
101 :     VARIABLE Unnest
102 :    
103 :     : D-KEY ( -- flag )
104 :     BEGIN
105 :     Unnest @ IF 0 ELSE key THEN
106 : jwilke 1.8 CASE [char] n OF dbg-ip @ @ NestXT EXIT ENDOF
107 : anton 1.1 [char] s OF Leave-D
108 :     -128 THROW ENDOF
109 :     [char] a OF Leave-D
110 :     -128 THROW ENDOF
111 :     [char] d OF Leave-D
112 :     cr ." Done..." cr
113 :     Nesting off
114 : jwilke 1.8 r> drop dbg-ip @ >r
115 : anton 1.1 EXIT ENDOF
116 :     [char] ? OF cr ." Nest Stop Done Unnest" cr
117 :     ENDOF
118 :     [char] u OF Unnest on true EXIT ENDOF
119 :     drop true EXIT
120 :     ENDCASE
121 :     AGAIN ;
122 :    
123 :     : (debug) ( body -- )
124 :     0 Nesting !
125 :     BEGIN Unnest off
126 :     cr ." Scanning code..." cr C-Formated on
127 : jwilke 1.8 dup scanword dbg-ip !
128 : anton 1.1 cr ." Nesting debugger ready!" cr
129 : jwilke 1.10 BEGIN d.s disp-step D-Key
130 : anton 1.1 WHILE C-Stop @ 0=
131 :     WHILE 0 get-next set-bp
132 : jwilke 1.8 dbg-ip @ jump
133 : anton 1.1 [ here DebugLoop ! ]
134 :     restore-bp
135 :     REPEAT
136 : jwilke 1.8 Nesting @ 0= IF EXIT THEN
137 : anton 1.1 -1 Nesting +! r>
138 :     ELSE
139 : jwilke 1.8 dbg-ip @ 1 cells + >r 1 Nesting +!
140 : anton 1.1 THEN
141 :     AGAIN ;
142 :    
143 : crook 1.11 : dbg \ gforth
144 :     ' NestXT IF EXIT THEN (debug) Leave-D ;
145 : anton 1.1
146 : jwilke 1.12 has? compiler [IF]
147 : crook 1.11 : break: \ gforth
148 :     r> ['] (debug) >body >r ;
149 : anton 1.6
150 : jwilke 1.7 : (break")
151 : crook 1.11 cr
152 :     ." BREAK AT: " type cr
153 :     r> ['] (debug) >body >r ;
154 : anton 1.6
155 : crook 1.11 : break" \ gforth
156 :     postpone s"
157 :     postpone (break") ; immediate
158 : jwilke 1.12 [THEN]

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help