358be1023567d8e1a895a7f9b0e1c67bed39220a
[ghc-hetmet.git] / docs / users_guide / debugging.xml
1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <sect1 id="options-debugging">
3   <title>Debugging the compiler</title>
4
5   <indexterm><primary>debugging options (for GHC)</primary></indexterm>
6
7   <para>HACKER TERRITORY. HACKER TERRITORY.  (You were warned.)</para>
8
9   <sect2 id="dumping-output">
10     <title>Dumping out compiler intermediate structures</title>
11     
12     <indexterm><primary>dumping GHC intermediates</primary></indexterm>
13     <indexterm><primary>intermediate passes, output</primary></indexterm>
14     
15     <variablelist>
16       <varlistentry>
17         <term>
18           <option>-ddump-</option><replaceable>pass</replaceable>
19           <indexterm><primary><option>-ddump</option> options</primary></indexterm>
20         </term>
21         <listitem>
22           <para>Make a debugging dump after pass
23         <literal>&lt;pass&gt;</literal> (may be common enough to need
24         a short form&hellip;).  You can get all of these at once
25         (<emphasis>lots</emphasis> of output) by using
26         <option>-v5</option>, or most of them with
27         <option>-v4</option>.  Some of the most useful ones
28         are:</para>
29
30           <variablelist>
31             <varlistentry>
32               <term>
33                 <option>-ddump-parsed</option>:
34                 <indexterm><primary><option>-ddump-parsed</option></primary></indexterm>
35               </term>
36               <listitem>
37                 <para>parser output</para>
38               </listitem>
39             </varlistentry>
40
41             <varlistentry>
42               <term>
43                 <option>-ddump-rn</option>:
44                 <indexterm><primary><option>-ddump-rn</option></primary></indexterm>
45               </term>
46               <listitem>
47                 <para>renamer output</para>
48               </listitem>
49             </varlistentry>
50
51             <varlistentry>
52               <term>
53                 <option>-ddump-tc</option>:
54                 <indexterm><primary><option>-ddump-tc</option></primary></indexterm>
55               </term>
56               <listitem>
57                 <para>typechecker output</para>
58               </listitem>
59             </varlistentry>
60
61             <varlistentry>
62               <term>
63                 <option>-ddump-splices</option>:
64                 <indexterm><primary><option>-ddump-splices</option></primary></indexterm>
65               </term>
66               <listitem>
67                 <para>Dump Template Haskell expressions that we splice in,
68                 and what Haskell code the expression evaluates to.</para>
69               </listitem>
70             </varlistentry>
71
72             <varlistentry>
73               <term>
74                 <option>-ddump-types</option>:
75                 <indexterm><primary><option>-ddump-types</option></primary></indexterm>
76               </term>
77               <listitem>
78                 <para>Dump a type signature for each value defined at
79               the top level of the module.  The list is sorted
80               alphabetically.  Using <option>-dppr-debug</option>
81               dumps a type signature for all the imported and
82               system-defined things as well; useful for debugging the
83               compiler.</para>
84               </listitem>
85             </varlistentry>
86
87             <varlistentry>
88               <term>
89                 <option>-ddump-deriv</option>:
90                 <indexterm><primary><option>-ddump-deriv</option></primary></indexterm>
91               </term>
92               <listitem>
93                 <para>derived instances</para>
94               </listitem>
95             </varlistentry>
96
97             <varlistentry>
98               <term>
99                 <option>-ddump-ds</option>:
100                 <indexterm><primary><option>-ddump-ds</option></primary></indexterm>
101               </term>
102               <listitem>
103                 <para>desugarer output</para>
104               </listitem>
105             </varlistentry>
106
107             <varlistentry>
108               <term>
109                 <option>-ddump-spec</option>:
110                 <indexterm><primary><option>-ddump-spec</option></primary></indexterm>
111               </term>
112               <listitem>
113                 <para>output of specialisation pass</para>
114               </listitem>
115             </varlistentry>
116
117             <varlistentry>
118               <term>
119                 <option>-ddump-rules</option>:
120                 <indexterm><primary><option>-ddump-rules</option></primary></indexterm>
121               </term>
122               <listitem>
123                 <para>dumps all rewrite rules specified in this module; 
124                       see <xref linkend="controlling-rules"/>.
125                 </para>
126               </listitem>
127             </varlistentry>
128
129             <varlistentry>
130               <term>
131                 <option>-ddump-rule-firings</option>:
132                 <indexterm><primary><option>-ddump-rule-firings</option></primary></indexterm>
133               </term>
134               <listitem>
135                 <para>dumps the names of all rules that fired in this module</para>
136               </listitem>
137             </varlistentry>
138
139             <varlistentry>
140               <term>
141                 <option>-ddump-rule-rewrites</option>:
142                 <indexterm><primary><option>-ddump-rule-rewrites</option></primary></indexterm>
143               </term>
144               <listitem>
145                 <para>dumps detailed information about all rules that fired in
146                       this module
147                 </para>
148               </listitem>
149             </varlistentry>
150
151             <varlistentry>
152               <term>
153                 <option>-ddump-simpl</option>:
154                 <indexterm><primary><option>-ddump-simpl</option></primary></indexterm>
155               </term>
156               <listitem>
157                 <para>simplifier output (Core-to-Core passes)</para>
158               </listitem>
159             </varlistentry>
160
161             <varlistentry>
162               <term>
163                 <option>-ddump-inlinings</option>:
164                 <indexterm><primary><option>-ddump-inlinings</option></primary></indexterm>
165               </term>
166               <listitem>
167                 <para>inlining info from the simplifier</para>
168               </listitem>
169             </varlistentry>
170
171             <varlistentry>
172               <term>
173                 <option>-ddump-cpranal</option>:
174                 <indexterm><primary><option>-ddump-cpranal</option></primary></indexterm>
175               </term>
176               <listitem>
177                 <para>CPR analyser output</para>
178               </listitem>
179             </varlistentry>
180
181             <varlistentry>
182               <term>
183                 <option>-ddump-stranal</option>:
184                 <indexterm><primary><option>-ddump-stranal</option></primary></indexterm>
185               </term>
186               <listitem>
187                 <para>strictness analyser output</para>
188               </listitem>
189             </varlistentry>
190
191             <varlistentry>
192               <term>
193                 <option>-ddump-cse</option>:
194                 <indexterm><primary><option>-ddump-cse</option></primary></indexterm>
195               </term>
196               <listitem>
197                 <para>CSE pass output</para>
198               </listitem>
199             </varlistentry>
200
201             <varlistentry>
202               <term>
203                 <option>-ddump-worker-wrapper</option>:
204                 <indexterm><primary><option>-ddump-worker-wrapper</option></primary></indexterm>
205               </term>
206               <listitem>
207                 <para>worker/wrapper split output</para>
208               </listitem>
209             </varlistentry>
210
211             <varlistentry>
212               <term>
213                 <option>-ddump-occur-anal</option>:
214                 <indexterm><primary><option>-ddump-occur-anal</option></primary></indexterm>
215               </term>
216               <listitem>
217                 <para>`occurrence analysis' output</para>
218               </listitem>
219             </varlistentry>
220
221             <varlistentry>
222               <term>
223                 <option>-ddump-prep</option>:
224                 <indexterm><primary><option>-ddump-prep</option></primary></indexterm>
225               </term>
226               <listitem>
227                 <para>output of core preparation pass</para>
228               </listitem>
229             </varlistentry>
230
231             <varlistentry>
232               <term>
233                 <option>-ddump-stg</option>:
234                 <indexterm><primary><option>-ddump-stg</option></primary></indexterm>
235               </term>
236               <listitem>
237                 <para>output of STG-to-STG passes</para>
238               </listitem>
239             </varlistentry>
240
241             <varlistentry>
242               <term>
243                 <option>-ddump-flatC</option>:
244                 <indexterm><primary><option>-ddump-flatC</option></primary></indexterm>
245               </term>
246               <listitem>
247                 <para><emphasis>flattened</emphasis> Abstract&nbsp;C</para>
248               </listitem>
249             </varlistentry>
250
251             <varlistentry>
252               <term>
253                 <option>-ddump-cmm</option>:
254                 <indexterm><primary><option>-ddump-cmm</option></primary></indexterm>
255               </term>
256               <listitem>
257                 <para>Print the C-- code out.</para>
258               </listitem>
259             </varlistentry>
260
261             <varlistentry>
262               <term>
263                 <option>-ddump-opt-cmm</option>:
264                 <indexterm><primary><option>-ddump-opt-cmm</option></primary></indexterm>
265               </term>
266               <listitem>
267                 <para>Dump the results of C-- to C-- optimising passes.</para>
268               </listitem>
269             </varlistentry>
270
271             <varlistentry>
272               <term>
273                 <option>-ddump-asm</option>:
274                 <indexterm><primary><option>-ddump-asm</option></primary></indexterm>
275               </term>
276               <listitem>
277                 <para>assembly language from the native-code generator</para>
278               </listitem>
279             </varlistentry>
280
281             <varlistentry>
282               <term>
283                 <option>-ddump-bcos</option>:
284                 <indexterm><primary><option>-ddump-bcos</option></primary></indexterm>
285               </term>
286               <listitem>
287                 <para>byte code compiler output</para>
288               </listitem>
289             </varlistentry>
290
291             <varlistentry>
292               <term>
293                 <option>-ddump-foreign</option>:
294                 <indexterm><primary><option>-ddump-foreign</option></primary></indexterm>
295               </term>
296               <listitem>
297                 <para>dump foreign export stubs</para>
298               </listitem>
299             </varlistentry>
300           </variablelist>
301         </listitem>
302       </varlistentry>
303       
304       <varlistentry>
305         <term>
306           <option>-ddump-simpl-phases</option>:
307           <indexterm><primary><option>-ddump-simpl-phases</option></primary></indexterm>
308         </term>
309         <listitem>
310           <para>Show the output of each run of the simplifier.  Used when even
311         <option>-dverbose-core2core</option> doesn't cut it.</para>
312         </listitem>
313       </varlistentry>
314
315       <varlistentry>
316         <term>
317           <option>-ddump-simpl-iterations</option>:
318           <indexterm><primary><option>-ddump-simpl-iterations</option></primary></indexterm>
319         </term>
320         <listitem>
321           <para>Show the output of each <emphasis>iteration</emphasis>
322         of the simplifier (each run of the simplifier has a maximum
323         number of iterations, normally 4).  This outputs even more information
324         than <option>-ddump-simpl-phases</option>.</para>
325         </listitem>
326       </varlistentry>
327
328       <varlistentry>
329         <term>
330           <option>-ddump-simpl-stats</option>
331           <indexterm><primary><option>-ddump-simpl-stats option</option></primary></indexterm>
332         </term>
333         <listitem>
334           <para>Dump statistics about how many of each kind of
335         transformation too place.  If you add
336         <option>-dppr-debug</option> you get more detailed
337         information.</para>
338         </listitem>
339       </varlistentry>
340
341       <varlistentry>
342         <term>
343           <option>-ddump-if-trace</option>
344           <indexterm><primary><option>-ddump-if-trace</option></primary></indexterm>
345         </term>
346         <listitem>
347           <para>Make the interface loader be *real* chatty about what it is
348         upto.</para>
349         </listitem>
350       </varlistentry>
351
352       <varlistentry>
353         <term>
354           <option>-ddump-tc-trace</option>
355           <indexterm><primary><option>-ddump-tc-trace</option></primary></indexterm>
356         </term>
357         <listitem>
358           <para>Make the type checker be *real* chatty about what it is
359         upto.</para>
360         </listitem>
361       </varlistentry>
362
363       <varlistentry>
364         <term>
365           <option>-ddump-rn-trace</option>
366           <indexterm><primary><option>-ddump-rn-trace</option></primary></indexterm>
367         </term>
368         <listitem>
369           <para>Make the renamer be *real* chatty about what it is
370         upto.</para>
371         </listitem>
372       </varlistentry>
373
374       <varlistentry>
375         <term>
376           <option>-ddump-rn-stats</option>
377           <indexterm><primary><option>-dshow-rn-stats</option></primary></indexterm>
378         </term>
379         <listitem>
380           <para>Print out summary of what kind of information the renamer
381         had to bring in.</para>
382         </listitem>
383       </varlistentry>
384
385       <varlistentry>
386         <term>
387           <option>-dverbose-core2core</option>
388           <indexterm><primary><option>-dverbose-core2core</option></primary></indexterm>
389         </term>
390         <term>
391           <option>-dverbose-stg2stg</option>
392           <indexterm><primary><option>-dverbose-stg2stg</option></primary></indexterm>
393         </term>
394         <listitem>
395           <para>Show the output of the intermediate Core-to-Core and
396         STG-to-STG passes, respectively.  (<emphasis>Lots</emphasis>
397         of output!) So: when we're really desperate:</para>
398
399           <screen>
400 % ghc -noC -O -ddump-simpl -dverbose-core2core -dcore-lint Foo.hs
401 </screen>
402
403         </listitem>
404       </varlistentry>
405       
406       <varlistentry>
407         <term>
408           <option>-dshow-passes</option>
409           <indexterm><primary><option>-dshow-passes</option></primary></indexterm>
410         </term>
411         <listitem>
412           <para>Print out each pass name as it happens.</para>
413         </listitem>
414       </varlistentry>
415
416       <varlistentry>
417         <term>
418           <option>-dfaststring-stats</option>
419           <indexterm><primary><option>-dfaststring-stats</option></primary></indexterm>
420         </term>
421         <listitem>
422           <para>Show statistics for the usage of fast strings by the
423           compiler.</para>
424         </listitem>
425       </varlistentry>
426
427       <varlistentry>
428         <term>
429           <option>-dppr-debug</option>
430           <indexterm><primary><option>-dppr-debug</option></primary></indexterm>
431         </term>
432         <listitem>
433           <para>Debugging output is in one of several
434           &ldquo;styles.&rdquo; Take the printing of types, for
435           example.  In the &ldquo;user&rdquo; style (the default), the
436           compiler's internal ideas about types are presented in
437           Haskell source-level syntax, insofar as possible.  In the
438           &ldquo;debug&rdquo; style (which is the default for
439           debugging output), the types are printed in with explicit
440           foralls, and variables have their unique-id attached (so you
441           can check for things that look the same but aren't).  This
442           flag makes debugging output appear in the more verbose debug
443           style.</para>
444         </listitem>
445       </varlistentry>
446
447       <varlistentry>
448         <term>
449           <option>-dsuppress-uniques</option>
450           <indexterm><primary><option>-dsuppress-uniques</option></primary></indexterm>
451         </term>
452         <listitem>
453           <para>Suppress the printing of uniques in debugging output. This may make 
454           the printout ambiguous (e.g. unclear where an occurrence of 'x' is bound), but
455           it makes the output of two compiler runs have many fewer gratuitous differences,
456             so you can realistically apply <command>diff</command>.  Once <command>diff</command>
457           has shown you where to look, you can try again without <option>-dsuppress-uniques</option></para>
458         </listitem>
459       </varlistentry>
460
461       <varlistentry>
462         <term>
463           <option>-dsuppress-coercions</option>
464           <indexterm><primary><option>-dsuppress-coercions</option></primary></indexterm>
465         </term>
466         <listitem>
467           <para>Suppress the printing of coercions in Core dumps to make them
468 shorter.</para>
469         </listitem>
470       </varlistentry>
471
472       <varlistentry>
473         <term>
474           <option>-dsuppress-module-prefixes</option>
475           <indexterm><primary><option>-dsuppress-module-prefixes</option></primary></indexterm>
476         </term>
477         <listitem>
478           <para>Suppress the printing of module qualification prefixes in Core dumps to make them easier to read.</para>
479         </listitem>
480       </varlistentry>
481
482       <varlistentry>
483         <term>
484           <option>-dppr-user-length</option>
485           <indexterm><primary><option>-dppr-user-length</option></primary></indexterm>
486         </term>
487         <listitem>
488           <para>In error messages, expressions are printed to a
489           certain &ldquo;depth&rdquo;, with subexpressions beyond the
490           depth replaced by ellipses.  This flag sets the
491           depth.  Its default value is 5.</para>
492         </listitem>
493       </varlistentry>
494
495       <varlistentry>
496         <term>
497           <option>-dno-debug-output</option>
498           <indexterm><primary><option>-dno-debug-output</option></primary></indexterm>
499         </term>
500         <listitem>
501           <para>Suppress any unsolicited debugging output.  When GHC
502             has been built with the <literal>DEBUG</literal> option it
503             occasionally emits debug output of interest to developers.
504             The extra output can confuse the testing framework and
505             cause bogus test failures, so this flag is provided to
506             turn it off.</para>
507         </listitem>
508       </varlistentry>
509     </variablelist>
510   </sect2>
511
512   <sect2 id="checking-consistency">
513     <title>Checking for consistency</title>
514
515     <indexterm><primary>consistency checks</primary></indexterm>
516     <indexterm><primary>lint</primary></indexterm>
517
518     <variablelist>
519
520       <varlistentry>
521         <term>
522           <option>-dcore-lint</option>
523           <indexterm><primary><option>-dcore-lint</option></primary></indexterm>
524         </term>
525         <listitem>
526           <para>Turn on heavyweight intra-pass sanity-checking within
527           GHC, at Core level.  (It checks GHC's sanity, not yours.)</para>
528         </listitem>
529       </varlistentry>
530
531       <varlistentry>
532         <term>
533           <option>-dstg-lint</option>:
534           <indexterm><primary><option>-dstg-lint</option></primary></indexterm>
535         </term>
536         <listitem>
537           <para>Ditto for STG level. (NOTE: currently doesn't work).</para>
538         </listitem>
539       </varlistentry>
540
541       <varlistentry>
542         <term>
543           <option>-dcmm-lint</option>:
544           <indexterm><primary><option>-dcmm-lint</option></primary></indexterm>
545         </term>
546         <listitem>
547           <para>Ditto for C-- level.</para>
548         </listitem>
549       </varlistentry>
550
551     </variablelist>
552   </sect2>
553
554   <sect2>
555     <title>How to read Core syntax (from some <option>-ddump</option>
556     flags)</title>
557
558     <indexterm><primary>reading Core syntax</primary></indexterm>
559     <indexterm><primary>Core syntax, how to read</primary></indexterm>
560
561     <para>Let's do this by commenting an example.  It's from doing
562     <option>-ddump-ds</option> on this code:
563
564 <programlisting>
565 skip2 m = m : skip2 (m+2)
566 </programlisting>
567
568     Before we jump in, a word about names of things.  Within GHC,
569     variables, type constructors, etc., are identified by their
570     &ldquo;Uniques.&rdquo; These are of the form `letter' plus
571     `number' (both loosely interpreted).  The `letter' gives some idea
572     of where the Unique came from; e.g., <literal>&lowbar;</literal>
573     means &ldquo;built-in type variable&rdquo;; <literal>t</literal>
574     means &ldquo;from the typechecker&rdquo;; <literal>s</literal>
575     means &ldquo;from the simplifier&rdquo;; and so on.  The `number'
576     is printed fairly compactly in a `base-62' format, which everyone
577     hates except me (WDP).</para>
578
579     <para>Remember, everything has a &ldquo;Unique&rdquo; and it is
580     usually printed out when debugging, in some form or another.  So
581     here we go&hellip;</para>
582
583 <programlisting>
584 Desugared:
585 Main.skip2{-r1L6-} :: _forall_ a$_4 =&#62;{{Num a$_4}} -&#62; a$_4 -&#62; [a$_4]
586
587 --# `r1L6' is the Unique for Main.skip2;
588 --# `_4' is the Unique for the type-variable (template) `a'
589 --# `{{Num a$_4}}' is a dictionary argument
590
591 _NI_
592
593 --# `_NI_' means "no (pragmatic) information" yet; it will later
594 --# evolve into the GHC_PRAGMA info that goes into interface files.
595
596 Main.skip2{-r1L6-} =
597     /\ _4 -&#62; \ d.Num.t4Gt -&#62;
598         let {
599           {- CoRec -}
600           +.t4Hg :: _4 -&#62; _4 -&#62; _4
601           _NI_
602           +.t4Hg = (+{-r3JH-} _4) d.Num.t4Gt
603
604           fromInt.t4GS :: Int{-2i-} -&#62; _4
605           _NI_
606           fromInt.t4GS = (fromInt{-r3JX-} _4) d.Num.t4Gt
607
608 --# The `+' class method (Unique: r3JH) selects the addition code
609 --# from a `Num' dictionary (now an explicit lambda'd argument).
610 --# Because Core is 2nd-order lambda-calculus, type applications
611 --# and lambdas (/\) are explicit.  So `+' is first applied to a
612 --# type (`_4'), then to a dictionary, yielding the actual addition
613 --# function that we will use subsequently...
614
615 --# We play the exact same game with the (non-standard) class method
616 --# `fromInt'.  Unsurprisingly, the type `Int' is wired into the
617 --# compiler.
618
619           lit.t4Hb :: _4
620           _NI_
621           lit.t4Hb =
622               let {
623                 ds.d4Qz :: Int{-2i-}
624                 _NI_
625                 ds.d4Qz = I#! 2#
626               } in  fromInt.t4GS ds.d4Qz
627
628 --# `I# 2#' is just the literal Int `2'; it reflects the fact that
629 --# GHC defines `data Int = I# Int#', where Int# is the primitive
630 --# unboxed type.  (see relevant info about unboxed types elsewhere...)
631
632 --# The `!' after `I#' indicates that this is a *saturated*
633 --# application of the `I#' data constructor (i.e., not partially
634 --# applied).
635
636           skip2.t3Ja :: _4 -&#62; [_4]
637           _NI_
638           skip2.t3Ja =
639               \ m.r1H4 -&#62;
640                   let { ds.d4QQ :: [_4]
641                         _NI_
642                         ds.d4QQ =
643                     let {
644                       ds.d4QY :: _4
645                       _NI_
646                       ds.d4QY = +.t4Hg m.r1H4 lit.t4Hb
647                     } in  skip2.t3Ja ds.d4QY
648                   } in
649                   :! _4 m.r1H4 ds.d4QQ
650
651           {- end CoRec -}
652         } in  skip2.t3Ja
653 </programlisting>
654
655     <para>(&ldquo;It's just a simple functional language&rdquo; is an
656     unregisterised trademark of Peyton Jones Enterprises, plc.)</para>
657
658   </sect2>
659
660   <sect2 id="unreg">
661     <title>Unregisterised compilation</title>
662     <indexterm><primary>unregisterised compilation</primary></indexterm>
663
664     <para>The term "unregisterised" really means "compile via vanilla
665     C", disabling some of the platform-specific tricks that GHC
666     normally uses to make programs go faster.  When compiling
667     unregisterised, GHC simply generates a C file which is compiled
668     via gcc.</para>
669
670     <para>Unregisterised compilation can be useful when porting GHC to
671     a new machine, since it reduces the prerequisite tools to
672     <command>gcc</command>, <command>as</command>, and
673     <command>ld</command> and nothing more, and furthermore the amount
674     of platform-specific code that needs to be written in order to get
675     unregisterised compilation going is usually fairly small.</para>
676
677    <para>Unregisterised compilation cannot be selected at
678    compile-time; you have to build GHC with the appropriate options
679    set.  Consult the GHC Building Guide for details.</para>
680   </sect2>
681
682 </sect1>
683
684 <!-- Emacs stuff:
685      ;;; Local Variables: ***
686      ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter" "sect1") ***
687      ;;; End: ***
688  -->