4f8b0caa977ea2156f18ef69e0f661a6c1cbcc3e
[ghc-hetmet.git] / ghc / docs / users_guide / debugging.vsgml
1 %************************************************************************
2 %*                                                                      *
3 <sect1>Debugging the compiler
4 <label id="options-debugging">
5 <p>
6 <nidx>debugging options (for GHC)</nidx>
7 %*                                                                      *
8 %************************************************************************
9
10 HACKER TERRITORY. HACKER TERRITORY.
11 (You were warned.)
12
13 %----------------------------------------------------------------------
14 <sect2>Replacing the program for one or more phases.
15 <label id="replacing-phases">
16 <p>
17 <nidx>GHC phases, changing</nidx>
18 <nidx>phases, changing GHC</nidx>
19
20 You may specify that a different program be used for one of the phases
21 of the compilation system, in place of whatever the driver @ghc@ has
22 wired into it.  For example, you might want to try a different
23 assembler.  The
24 @-pgm<phase-code><program-name>@<nidx>-pgm&lt;phase&gt;&lt;stuff&gt;
25 option</nidx> option to @ghc@ will cause it to use @<program-name>@
26 for phase @<phase-code>@, where the codes to indicate the phases are:
27
28 <tabular ca="ll">
29 <bf>code</bf> | <bf>phase</bf> @@
30 @@
31 L    | literate pre-processor @@
32 P    | C pre-processor (if -cpp only) @@
33 C    | Haskell compiler @@
34 c    | C compiler@@
35 a    | assembler @@
36 l    | linker @@
37 dep  | Makefile dependency generator @@
38 </tabular>
39
40 %----------------------------------------------------------------------
41 <sect2>Forcing options to a particular phase.
42 <label id="forcing-options-through">
43 <p>
44 <nidx>forcing GHC-phase options</nidx>
45
46 The preceding sections describe driver options that are mostly
47 applicable to one particular phase.  You may also <em>force</em> a
48 specific option @<option>@ to be passed to a particular phase
49 @<phase-code>@ by feeding the driver the option
50 @-opt<phase-code><option>@.<nidx>-opt&lt;phase&gt;&lt;stuff&gt;
51 option</nidx> The codes to indicate the phases are the same as in the
52 previous section.
53
54 So, for example, to force an @-Ewurble@ option to the assembler, you
55 would tell the driver @-opta-Ewurble@ (the dash before the E is
56 required).
57
58 Besides getting options to the Haskell compiler with @-optC<blah>@,
59 you can get options through to its runtime system with
60 @-optCrts<blah>@<nidx>-optCrts&lt;blah&gt; option</nidx>.
61
62 So, for example: when I want to use my normal driver but with my
63 profiled compiler binary, I use this script:
64 <tscreen><verb>
65 #! /bin/sh
66 exec /local/grasp_tmp3/simonpj/ghc-BUILDS/working-alpha/ghc/driver/ghc \
67      -pgmC/local/grasp_tmp3/simonpj/ghc-BUILDS/working-hsc-prof/hsc \
68      -optCrts-i0.5 \
69      -optCrts-PT \
70      "$@"
71 </verb></tscreen>
72
73 %----------------------------------------------------------------------
74 <sect2>Dumping out compiler intermediate structures
75 <label id="dumping-output">
76 <p>
77 <nidx>dumping GHC intermediates</nidx>
78 <nidx>intermediate passes, output</nidx>
79
80 <descrip>
81 <tag>@-noC@:</tag>
82 <nidx>-noC option</nidx>
83 Don't bother generating C output <em>or</em> an interface file.  Usually
84 used in conjunction with one or more of the @-ddump-*@ options; for
85 example: @ghc -noC -ddump-simpl Foo.hs@
86
87 <tag>@-hi@:</tag>
88 <nidx>-hi option</nidx>
89 <em>Do</em> generate an interface file.  This would normally be used in
90 conjunction with @-noC@, which turns off interface generation;
91 thus: @-noC -hi@.
92
93 <tag>@-dshow-passes@:</tag>
94 <nidx>-dshow-passes option</nidx>
95 Prints a message to stderr as each pass starts.  Gives a warm but
96 undoubtedly misleading feeling that GHC is telling you what's
97 happening.
98
99 <tag>@-ddump-<pass>@:</tag>
100 <nidx>-ddump-&lt;pass&gt; options</nidx>
101 Make a debugging dump after pass @<pass>@ (may be common enough to
102 need a short form...).  You can get all of these at once (<em/lots/ of
103 output) by using @-ddump-all@, or most of them with @-ddump-most@.
104 Some of the most useful ones are:
105
106 <descrip>
107 <tag>@-ddump-rdr@:</tag> reader output (earliest stuff in the compiler)
108 <tag>@-ddump-rn@:</tag>  renamer output
109 <tag>@-ddump-tc@:</tag>  typechecker output
110 <tag>@-ddump-deriv@:</tag>  derived instances
111 <tag>@-ddump-ds@:</tag>  desugarer output
112 <tag>@-ddump-spec@:</tag>  output of specialisation pass
113 <tag>@-ddump-rules@:</tag>  dumps all rewrite rules (including those generated by the specialisation pass)
114 <tag>@-ddump-simpl@:</tag>  simplifer output (Core-to-Core passes)
115 <tag>@-ddump-usagesp@:</tag> UsageSP inference pre-inf and output
116 <tag>@-ddump-cpranal@:</tag>  CPR analyser output
117 <tag>@-ddump-stranal@:</tag>  strictness analyser output
118 <tag>@-ddump-workwrap@:</tag>  worker/wrapper split output
119 <tag>@-ddump-occur-anal@:</tag>  `occurrence analysis' output
120 <tag>@-ddump-stg@:</tag>  output of STG-to-STG passes
121 <tag>@-ddump-absC@:</tag>  <em>un</em>flattened Abstract~C
122 <tag>@-ddump-flatC@:</tag>  <em>flattened</em> Abstract~C
123 <tag>@-ddump-realC@:</tag>  same as what goes to the C compiler
124 <tag>@-ddump-asm@:</tag>  assembly language from the native-code generator
125 </descrip>
126
127 <nidx>-ddump-all option</nidx>%
128 <nidx>-ddump-most option</nidx>%
129 <nidx>-ddump-rdr option</nidx>%
130 <nidx>-ddump-rn option</nidx>%
131 <nidx>-ddump-tc option</nidx>%
132 <nidx>-ddump-deriv option</nidx>%
133 <nidx>-ddump-ds option</nidx>%
134 <nidx>-ddump-simpl option</nidx>%
135 <nidx>-ddump-cpranal option</nidx>%
136 <nidx>-ddump-workwrap option</nidx>%
137 <nidx>-ddump-rules option</nidx>%
138 <nidx>-ddump-usagesp option</nidx>%
139 <nidx>-ddump-stranal option</nidx>%
140 <nidx>-ddump-occur-anal option</nidx>%
141 <nidx>-ddump-spec option</nidx>%
142 <nidx>-ddump-stg option</nidx>%
143 <nidx>-ddump-absC option</nidx>%
144 <nidx>-ddump-flatC option</nidx>%
145 <nidx>-ddump-realC option</nidx>%
146 <nidx>-ddump-asm option</nidx>
147
148 %For any other @-ddump-*@ options: consult the source, notably
149 %@ghc/compiler/main/CmdLineOpts.lhs@.
150
151 <tag>@-dverbose-simpl@ and @-dverbose-stg@:</tag>
152 <nidx>-dverbose-simpl option</nidx>
153 <nidx>-dverbose-stg option</nidx>
154 Show the output of the intermediate Core-to-Core and STG-to-STG
155 passes, respectively.  (<em>Lots</em> of output!) So: when we're 
156 really desperate:
157 <tscreen><verb>
158 % ghc -noC -O -ddump-simpl -dverbose-simpl -dcore-lint Foo.hs
159 </verb></tscreen>
160
161 <tag>@-ddump-simpl-iterations@:</tag>
162 <nidx>-ddump-simpl-iterations option</nidx>
163 Show the output of each <em/iteration/ of the simplifier (each run of
164 the simplifier has a maximum number of iterations, normally 4).  Used
165 when even @-dverbose-simpl@ doesn't cut it.
166
167 <tag>@-dppr-{user,debug@}:</tag>
168 <nidx>-dppr-user option</nidx>
169 <nidx>-dppr-debug option</nidx>
170 Debugging output is in one of several ``styles.''  Take the printing
171 of types, for example.  In the ``user'' style, the compiler's internal
172 ideas about types are presented in Haskell source-level syntax,
173 insofar as possible.  In the ``debug'' style (which is the default for
174 debugging output), the types are printed in with
175 explicit foralls, and variables have their unique-id attached (so you
176 can check for things that look the same but aren't).
177
178 <tag>@-ddump-simpl-stats@:</tag>
179 <nidx>-ddump-simpl-stats option</nidx>
180 Dump statistics about how many of each kind
181 of transformation too place.  If you add @-dppr-debug@ you get more detailed information.
182
183 <tag>@-ddump-raw-asm@:</tag>
184 <nidx>-ddump-raw-asm option</nidx>
185 Dump out the assembly-language stuff, before the ``mangler'' gets it.
186
187 <tag>@-ddump-rn-trace@:</tag>
188 <nidx>-ddump-rn-trace</nidx>
189 Make the renamer be *real* chatty about what it is upto.
190
191 <tag>@-dshow-rn-stats@:</tag>
192 <nidx>-dshow-rn-stats</nidx>
193 Print out summary of what kind of information the renamer had to bring
194 in.
195 <tag>@-dshow-unused-imports@:</tag>
196 <nidx>-dshow-unused-imports</nidx>
197 Have the renamer report what imports does not contribute.
198
199 %
200 %<tag>@-dgc-debug@:</tag>
201 %<nidx>-dgc-debug option</nidx>
202 %Enables some debugging code related to the garbage-collector.
203 </descrip>
204
205 %ToDo: -ddump-asm-insn-counts
206 %-ddump-asm-globals-info
207
208 %----------------------------------------------------------------------
209 <sect2>Checking for consistency
210 <label id="checking-consistency">
211 <p>
212 <nidx>consistency checks</nidx>
213 <nidx>lint</nidx>
214
215 <descrip>
216 <tag>@-dcore-lint@:</tag>
217 <nidx>-dcore-lint option</nidx>
218 Turn on heavyweight intra-pass sanity-checking within GHC, at Core
219 level.  (It checks GHC's sanity, not yours.)
220
221 <tag>@-dstg-lint@:</tag>
222 <nidx>-dstg-lint option</nidx>
223 Ditto for STG level.
224
225 <tag>@-dusagesp-lint@:</tag>
226 <nidx>-dstg-lint option</nidx>
227 Turn on checks around UsageSP inference (@-fusagesp@).  This verifies
228 various simple properties of the results of the inference, and also
229 warns if any identifier with a used-once annotation before the
230 inference has a used-many annotation afterwards; this could indicate a
231 non-worksafe transformation is being applied.
232 </descrip>
233
234 %----------------------------------------------------------------------
235 <sect2>How to read Core syntax (from some @-ddump-*@ flags)
236 <p>
237 <nidx>reading Core syntax</nidx>
238 <nidx>Core syntax, how to read</nidx>
239
240 Let's do this by commenting an example.  It's from doing
241 @-ddump-ds@ on this code:
242 <tscreen><verb>
243 skip2 m = m : skip2 (m+2)
244 </verb></tscreen>
245
246 Before we jump in, a word about names of things.  Within GHC,
247 variables, type constructors, etc., are identified by their
248 ``Uniques.''  These are of the form `letter' plus `number' (both
249 loosely interpreted).  The `letter' gives some idea of where the
250 Unique came from; e.g., @_@ means ``built-in type variable'';
251 @t@ means ``from the typechecker''; @s@ means ``from the
252 simplifier''; and so on.  The `number' is printed fairly compactly in
253 a `base-62' format, which everyone hates except me (WDP).
254
255 Remember, everything has a ``Unique'' and it is usually printed out
256 when debugging, in some form or another.  So here we go...
257
258 <tscreen><verb>
259 Desugared:
260 Main.skip2{-r1L6-} :: _forall_ a$_4 =>{{Num a$_4}} -> a$_4 -> [a$_4]
261
262 --# `r1L6' is the Unique for Main.skip2;
263 --# `_4' is the Unique for the type-variable (template) `a'
264 --# `{{Num a$_4}}' is a dictionary argument
265
266 _NI_
267
268 --# `_NI_' means "no (pragmatic) information" yet; it will later
269 --# evolve into the GHC_PRAGMA info that goes into interface files.
270
271 Main.skip2{-r1L6-} =
272     /\ _4 -> \ d.Num.t4Gt ->
273         let {
274           {- CoRec -}
275           +.t4Hg :: _4 -> _4 -> _4
276           _NI_
277           +.t4Hg = (+{-r3JH-} _4) d.Num.t4Gt
278
279           fromInt.t4GS :: Int{-2i-} -> _4
280           _NI_
281           fromInt.t4GS = (fromInt{-r3JX-} _4) d.Num.t4Gt
282
283 --# The `+' class method (Unique: r3JH) selects the addition code
284 --# from a `Num' dictionary (now an explicit lamba'd argument).
285 --# Because Core is 2nd-order lambda-calculus, type applications
286 --# and lambdas (/\) are explicit.  So `+' is first applied to a
287 --# type (`_4'), then to a dictionary, yielding the actual addition
288 --# function that we will use subsequently...
289
290 --# We play the exact same game with the (non-standard) class method
291 --# `fromInt'.  Unsurprisingly, the type `Int' is wired into the
292 --# compiler.
293
294           lit.t4Hb :: _4
295           _NI_
296           lit.t4Hb =
297               let {
298                 ds.d4Qz :: Int{-2i-}
299                 _NI_
300                 ds.d4Qz = I#! 2#
301               } in  fromInt.t4GS ds.d4Qz
302
303 --# `I# 2#' is just the literal Int `2'; it reflects the fact that
304 --# GHC defines `data Int = I# Int#', where Int# is the primitive
305 --# unboxed type.  (see relevant info about unboxed types elsewhere...)
306
307 --# The `!' after `I#' indicates that this is a *saturated*
308 --# application of the `I#' data constructor (i.e., not partially
309 --# applied).
310
311           skip2.t3Ja :: _4 -> [_4]
312           _NI_
313           skip2.t3Ja =
314               \ m.r1H4 ->
315                   let { ds.d4QQ :: [_4]
316                         _NI_
317                         ds.d4QQ =
318                     let {
319                       ds.d4QY :: _4
320                       _NI_
321                       ds.d4QY = +.t4Hg m.r1H4 lit.t4Hb
322                     } in  skip2.t3Ja ds.d4QY
323                   } in
324                   :! _4 m.r1H4 ds.d4QQ
325
326           {- end CoRec -}
327         } in  skip2.t3Ja
328 </verb></tscreen>
329
330 (``It's just a simple functional language'' is an unregisterised
331 trademark of Peyton Jones Enterprises, plc.)
332
333 %----------------------------------------------------------------------
334 <sect2>Command line options in source files
335 <label id="source-file-options">
336 <p>
337 <nidx>source-file options</nidx>
338
339 Sometimes it is useful to make the connection between a source file
340 and the command-line options it requires quite tight. For instance,
341 if a (Glasgow) Haskell source file uses @casm@s, the C back-end
342 often needs to be told about which header files to include. Rather than
343 maintaining the list of files the source depends on in a
344 @Makefile@ (using the @-#include@ command-line option), it is
345 possible to do this directly in the source file using the @OPTIONS@
346 pragma <nidx>OPTIONS pragma</nidx>: 
347
348 <tscreen><verb>
349 {-# OPTIONS -#include "foo.h" #-}
350 module X where
351
352 ...
353 </verb></tscreen>
354
355 @OPTIONS@ pragmas are only looked for at the top of your source
356 files, upto the first (non-literate,non-empty) line not containing
357 @OPTIONS@. Multiple @OPTIONS@ pragmas are recognised. Note
358 that your command shell does not get to the source file options, they
359 are just included literally in the array of command-line arguments
360 the compiler driver maintains internally, so you'll be desperately
361 disappointed if you try to glob etc. inside @OPTIONS@.
362
363 NOTE: the contents of OPTIONS are prepended to the command-line
364 options, so you *do* have the ability to override OPTIONS settings
365 via the command line.
366
367 It is not recommended to move all the contents of your Makefiles into
368 your source files, but in some circumstances, the @OPTIONS@ pragma
369 is the Right Thing. (If you use @-keep-hc-file-too@ and have OPTION
370 flags in your module, the OPTIONS will get put into the generated .hc
371 file).
372
373 %----------------------------------------------------------------------