* include/parrot/sub.h:
[parrot.git] / docs / debug.pod
blob467e92f6ab94558d26a6304bd8409a35e150a850
1 # Copyright (C) 2001-2004, The Perl Foundation.
2 # $Id$
4 =head1 NAME
6 docs/debug.pod - Debugging Parrot
8 =head1 ABSTRACT
10 This document describes how to debug various parts of Parrot.
12 =head1 VERSION
14 $Revision $
16 =head1 THE PARROT BINARY
18 =head2 Using a debugger
20 Per default the C<parrot> binary is being built with debugging symbols. This
21 means that you can run C<parrot> under an debugger like C<gdb>.
23 Debugging support can be explicitly enabled with:
25     shell> perl Configure.pl --debugging
26     shell> make
28 For testing it might be a good idea to make test runs without debug support. So
29 debugging can also be turned off with:
31   shell> perl Configure.pl --debugging=0
32   shell> make
34 =head2 Using a memory checker
36 You could, and should, also run the tests with a memory checker such as
37 C<valgrind>.  You can enable C<valgrind>, by running:
39   shell> make test VALGRIND="valgrind --logfile=/tmp/grind"
41 Another possibility is to use Electric Fence, or ...
43 =head2 MEMORY MANAGEMENT
45 Some of the more frequent and exasperating C<parrot> bugs are related to memory
46 management in general, and garbage collection in particular.
48 Infant mortality
50 See F<docs/dev/infant.dev> for details of one frequent problem: infant
51 mortality. Infant mortality is when you create a Parrot object, but the garbage
52 collector runs before you put it into a Parrot register or in something else
53 that is itself within a Parrot register.
55 To help in resolving these issues, the parrot binary accepts a C<--gc-debug>
56 flag. This flag makes garbage collection occur as frequently as possible, which
57 maximizes the probability that any newborn objects will run afoul of the
58 garbage collector.
60 Within the C<--gc-debug> mode, there is another tool to help narrow down the
61 problem. You can edit F<src/dod.c> and C<#define> the C<GC_VERBOSE> flag to 1.
62 After recompiling C<parrot>, the garbage collector will perform additional
63 checks. After the garbage collector has traced all objects to find which ones
64 are still alive, it will scan through all of the dead objects to see if any of
65 them believe they are alive (which will happen for infants, since they come
66 into existence marked live.) If it finds any, it will print them out. You can
67 then re-run the program with a breakpoint set on the routine that allocated the
68 object (e.g. C<get_free_object> in F<src/smallobject.c>).  You'll probably want
69 to make the breakpoint conditional on the object having the version number that
70 was reported, because the same memory location will probably hold many
71 different objects over the lifetime of the program.
73 =head1 PIR AND PASM CODE
75 Let's say you have written (or generated) a huge .pasm or .pir file.  It's not
76 working. You'd like some help in figuring out why.
78 =head2 pdb
80 One possible tool is C<pdb>, the Parrot Debugger. See F<docs/debugger.pod> for
81 details on it.
83 =head2 stabs
85 If you are running on a jit-capable machine, you can also try using C<gdb> by
86 having the JIT compiler generate C<stabs> metadata and then stepping through
87 the code with C<gdb> as if it were any other language.
89 To use this, you'll want to use C<parrot> to generate your bytecode (.pbc
90 file). It is not strictly necessary, but you'll get more information into the
91 bytecode this way.
93 Let's say your file is named C<test.pasm>. (Note: these instructions will also
94 work if you use C<test.pir> everywhere C<test.pasm> occurs.)
96 Step 1: Generate the .pbc file with extra debugging information.
98   shell> parrot -d -o test.pbc test.pasm
100 Step 2: Start up C<parrot> under C<gdb>
102   % gdb parrot
106   % emacs &
107   (in emacs) M-x gdb
108   (in emacs) type "parrot" so it says "gdb parrot"
110 Step 3: Set a breakpoint on runops_jit
112   gdb> b runops_jit
114 Step 4: Run your program under C<gdb> with JIT and debugging on
116   gdb> run -j -D4 test.pbc
118 Step 5: C<gdb> will stop at the beginning of runops_jit. Step through the lines
119 until just before the JITed code is executed (the line will be something like
120 C<(jit_code)(interpreter,pc)>.
122   gdb> n
123   gdb> n
124   .
125   .
126   .
128 Step 6: load in the debugging information from the symbol file that the jit
129 just generated.
131   gdb> add-symbol-file test.o 0
133 Step 7: Step into the JITed code
135   gdb> s
137 At this point, you can step through the instructions, or print out the
138 various Parrot registers. FIXME: C<gdb> will know about I0-I31,
139 N0-N31, S0-S31, and P0-P31.
142 WARNING: Stepping too far
144 One thing to watch out for is that C<gdb> gets confused when attempting to step
145 over certain instructions. The only ones that I have noticed having problems is
146 keyed operations. With my version of C<gdb>, if I do 'n' to step over the
147 instruction, C<gdb> will start running and only stop when the entire parrot
148 program has finished. To work around this, do 'si' twice just before executing
149 any keyed op. For some reason, C<gdb> can then figure out when it's supposed to
150 stop next. If you know of a better technique, please let the mailing list know
151 (C<parrot-porters@perl.org>).
153 =head1 PIR CODE GENERATION
155 The C<parrot> binary has a bunch of debugging flags for spewing out information
156 about various aspects of its processing. See L<running.pod> for a
157 list of flags. Or have a look at the information provided by:
159   shell> parrot --help
163   shell> parrot --help-debug