12 |
Tag: <post-commit CVS tag> |
Tag: <post-commit CVS tag> |
13 |
Description: |
Description: |
14 |
---------------------------------------------------------------------- |
---------------------------------------------------------------------- |
15 |
|
Name: Matthias Blume |
16 |
|
Date: 2000/06/23 18:20:00 JST |
17 |
|
Tag: blume-20000623-btrace |
18 |
|
Description: |
19 |
|
|
20 |
|
This updates adds a backtrace facility to aid programmers in debugging |
21 |
|
their programs. This involves the following changes: |
22 |
|
|
23 |
|
1. Module system/smlnj/init/core.sml (structure _Core) now has hooks for |
24 |
|
keeping track of the current call stack. When programs are compiled |
25 |
|
in a special mode, the compiler will insert calls to these hooks |
26 |
|
into the user program. |
27 |
|
"Hook" means that it is possible for different implementations of |
28 |
|
back-tracing to register themselves (at different times). |
29 |
|
|
30 |
|
2. compiler/MiscUtil/profile/btrace.sml implements the annotation phase |
31 |
|
as an Absyn.dec->Absyn.dec rewrite. Normally this phase is turned off. |
32 |
|
It can be turned on using this call: |
33 |
|
SMLofNJ.Internals.BTrace.mode (SOME true); |
34 |
|
Turning it off again: |
35 |
|
SMLofNJ.Internals.BTrace.mode (SOME false); |
36 |
|
Querying the current status: |
37 |
|
SMLofNJ.Internals.BTrace.mode NONE; |
38 |
|
Annotated programs are about twice as big as normal ones, and they |
39 |
|
run a factor of 2 to 4 slower with a dummy back-trace plugin (one |
40 |
|
where all hooks do nothing). The slowdown with a plugin that is |
41 |
|
actually useful (such as the one supplied by default) is even greater, |
42 |
|
but in the case of the default plugin it is still only an constant |
43 |
|
factor (amortized). |
44 |
|
|
45 |
|
3. system/Basis/Implementation/NJ/internals.{sig,sml} have been augmented |
46 |
|
with a sub-structure BTrace for controlling back-tracing. In particular, |
47 |
|
the above-mentioned function "mode" controls whether the annotation |
48 |
|
phase is invoked by the compiler. Another important function is |
49 |
|
"trigger": when called it aborts the current execution and causes |
50 |
|
the top-level loop to print a full back-trace. |
51 |
|
|
52 |
|
4. compiler/MiscUtil/profile/btimp.sml is the current default plugin |
53 |
|
for back-tracing. It keeps track of the dynamic call stack and in |
54 |
|
addition to that it keeps a partial history at each "level" of that |
55 |
|
stack. For example, if a tail-calls b, b tail-calls c, and c tail-calls |
56 |
|
d and b (at separate times, dynamically), then the report will show: |
57 |
|
|
58 |
|
GOTO d |
59 |
|
/c |
60 |
|
GOTO \b |
61 |
|
CALL a |
62 |
|
|
63 |
|
This shows that there was an initial non-tail call of a, then a |
64 |
|
tail-call to b or c, looping behavior in a cluster of functions that |
65 |
|
consist of b and c, and then a goto from that cluster (i.e., either from |
66 |
|
b or from c) to d. |
67 |
|
|
68 |
|
Note that (depending on the user program) the amount of information |
69 |
|
that the back-trace module has to keep track of at each level is bounded |
70 |
|
by a constant. Thus, the whole implementation has the same asymptotical |
71 |
|
complexity as the original program (both in space and in time). |
72 |
|
|
73 |
|
5. compiler/TopLevel/interact/evalloop.sml has been modified to |
74 |
|
handle the special exception SMLofNJ.Internals.BTrace.BTrace |
75 |
|
which is raised by the "trigger" function mentioned above. |
76 |
|
|
77 |
|
Notes on usage: |
78 |
|
|
79 |
|
- Annotated code works well together with unannotated code: |
80 |
|
Unannotated calls simply do not show up at all in the backtrace. |
81 |
|
|
82 |
|
- It is not a good idea to let modules that were annotated during |
83 |
|
different sessions run at the same time. This is because the compiler |
84 |
|
chooses small integers to identify individual functions, and there |
85 |
|
will be clashes if different modules were compiled in separate sessions. |
86 |
|
(Nothing will crash, and you will even be told about the clashes, but |
87 |
|
back-trace information will in general not be useful.) |
88 |
|
|
89 |
|
- Back-tracing can be confused by callcc and capture. |
90 |
|
|
91 |
|
- The only way of getting a back-trace right now is to explicitly |
92 |
|
invoke the "trigger" function from your user program. Eventually, we |
93 |
|
should make every exception carry back-trace information (if |
94 |
|
available). But since this creates more overhead at "raise"-time |
95 |
|
(similar to the current exnHistory overhead), I have not yet |
96 |
|
implemented this. (The implementation will be rather easy.) With |
97 |
|
exceptions carrying back-trace information, this facility will be even |
98 |
|
more useful because users don't need to modify their programs... |
99 |
|
|
100 |
|
- While it is possible to compile the compiler with back-trace |
101 |
|
annotations turned on (I did it to get some confidence in |
102 |
|
correctness), you must make absolutely sure that core.sml and |
103 |
|
btimp.sml are compiled WITHOUT annotation! (core.sml cannot actually |
104 |
|
be compiled with annotation because there is no core access yet, but |
105 |
|
if you compile btimp.sml with annotation, then the system will go into |
106 |
|
an infinite recursion and crash.) |
107 |
|
Since CM currently does not know about BTrace, the only way to turn |
108 |
|
annotations on and off for different modules of the compiler is to |
109 |
|
interrupt CMB.make, change the settings, and re-invoke it. Of course, |
110 |
|
this is awkward and clumsy. |
111 |
|
|
112 |
|
Sample sessions: |
113 |
|
|
114 |
|
Standard ML of New Jersey v110.28.1 [FLINT v1.5], June 5, 2000 |
115 |
|
- SMLofNJ.Internals.BTrace.mode (SOME true); |
116 |
|
[autoloading] |
117 |
|
[autoloading done] |
118 |
|
val it = false : bool |
119 |
|
- structure X = struct |
120 |
|
- fun main n = let |
121 |
|
- fun a (x, 0) = d x |
122 |
|
- | a (x, n) = b (x, n - 1) |
123 |
|
- and b (x, n) = c (x, n) |
124 |
|
- and c (x, n) = a (x, n) |
125 |
|
- and d x = e (x, 3) |
126 |
|
- and e (x, 0) = f x |
127 |
|
- | e (x, n) = e (x, n - 1) |
128 |
|
- and f 0 = SMLofNJ.Internals.BTrace.trigger () |
129 |
|
- | f n = n * g (n - 1) |
130 |
|
- and g n = a (n, 3) |
131 |
|
- in |
132 |
|
- f n |
133 |
|
- end |
134 |
|
- end; |
135 |
|
structure X : sig val main : int -> int end |
136 |
|
- X.main 3; |
137 |
|
*** BACK-TRACE *** |
138 |
|
GOTO stdIn:4.2-13.20: X.main[2].f |
139 |
|
GOTO-( stdIn:4.2-13.20: X.main[2].e |
140 |
|
GOTO stdIn:4.2-13.20: X.main[2].d |
141 |
|
/ stdIn:4.2-13.20: X.main[2].a |
142 |
|
| stdIn:4.2-13.20: X.main[2].b |
143 |
|
GOTO-\ stdIn:4.2-13.20: X.main[2].c |
144 |
|
CALL stdIn:4.2-13.20: X.main[2].g |
145 |
|
GOTO stdIn:4.2-13.20: X.main[2].f |
146 |
|
GOTO-( stdIn:4.2-13.20: X.main[2].e |
147 |
|
GOTO stdIn:4.2-13.20: X.main[2].d |
148 |
|
/ stdIn:4.2-13.20: X.main[2].a |
149 |
|
| stdIn:4.2-13.20: X.main[2].b |
150 |
|
GOTO-\ stdIn:4.2-13.20: X.main[2].c |
151 |
|
CALL stdIn:4.2-13.20: X.main[2].g |
152 |
|
GOTO stdIn:4.2-13.20: X.main[2].f |
153 |
|
GOTO-( stdIn:4.2-13.20: X.main[2].e |
154 |
|
GOTO stdIn:4.2-13.20: X.main[2].d |
155 |
|
/ stdIn:4.2-13.20: X.main[2].a |
156 |
|
| stdIn:4.2-13.20: X.main[2].b |
157 |
|
GOTO-\ stdIn:4.2-13.20: X.main[2].c |
158 |
|
CALL stdIn:4.2-13.20: X.main[2].g |
159 |
|
GOTO stdIn:4.2-13.20: X.main[2].f |
160 |
|
CALL stdIn:2.15-17.4: X.main[2] |
161 |
|
- |
162 |
|
|
163 |
|
(Note that because of a FLINt bug the above code currently does not |
164 |
|
compile without BTrace turned on.) |
165 |
|
|
166 |
|
Here is another example, using my modified Tiger compiler: |
167 |
|
|
168 |
|
Standard ML of New Jersey v110.28.1 [FLINT v1.5], June 5, 2000 |
169 |
|
- SMLofNJ.Internals.BTrace.mode (SOME true); |
170 |
|
[autoloading] |
171 |
|
[autoloading done] |
172 |
|
val it = false : bool |
173 |
|
- CM.make "sources.cm"; |
174 |
|
[autoloading] |
175 |
|
... |
176 |
|
[autoloading done] |
177 |
|
[scanning sources.cm] |
178 |
|
[parsing (sources.cm):parse.sml] |
179 |
|
[creating directory CM/SKEL ...] |
180 |
|
[parsing (sources.cm):tiger.lex.sml] |
181 |
|
... |
182 |
|
[wrote CM/sparc-unix/semant.sml] |
183 |
|
[compiling (sources.cm):main.sml] |
184 |
|
[wrote CM/sparc-unix/main.sml] |
185 |
|
[New bindings added.] |
186 |
|
val it = true : bool |
187 |
|
- Main.compile ("../testcases/merge.tig", "foo.out"); |
188 |
|
*** BACK-TRACE *** |
189 |
|
CALL lib/semant.sml:99.2-396.21: SemantFun[2].transExp.trvar |
190 |
|
CALL lib/semant.sml:99.2-396.21: SemantFun[2].transExp.trexp |
191 |
|
CALL lib/semant.sml:289.3-295.22: SemantFun[2].transExp.trexp.check[2] |
192 |
|
GOTO lib/semant.sml:289.3-295.22: SemantFun[2].transExp.trexp.check[2] |
193 |
|
CALL lib/semant.sml:99.2-396.21: SemantFun[2].transExp.trexp |
194 |
|
CALL lib/semant.sml:99.2-396.21: SemantFun[2].transExp.trexp |
195 |
|
CALL lib/semant.sml:488.3-505.6: SemantFun[2].transDec.trdec[2].transBody[2] |
196 |
|
/ lib/semant.sml:411.65-543.8: SemantFun[2].transDec |
197 |
|
CALL-\ lib/semant.sml:413.2-540.9: SemantFun[2].transDec.trdec[2] |
198 |
|
CALL lib/semant.sml:99.2-396.21: SemantFun[2].transExp.trexp |
199 |
|
CALL lib/semant.sml:8.52-558.4: SemantFun[2].transProg[2] |
200 |
|
CALL main.sml:1.18-118.4: Main.compile[2] |
201 |
|
- |
202 |
|
|
203 |
|
---------------------------------------------------------------------- |
204 |
Name: Matthias Blumen |
Name: Matthias Blumen |
205 |
Date: 2000/06/21 18:00:00 JST |
Date: 2000/06/21 18:00:00 JST |
206 |
Tag: blume-20000621-manual |
Tag: blume-20000621-manual |