1 # Copyright (C) 2001-2004, The Perl Foundation.
6 docs/debug.pod - Debugging Parrot
10 This document describes how to debug various parts of Parrot.
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
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
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 --log-file=/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.
50 See F<docs/dev/infant.pod> 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
60 Within the C<--gc-debug> mode, there is another tool to help narrow down the
61 problem. You can edit F<src/gc/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/gc/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 parrot_debugger
80 One possible tool is C<parrot_debugger>, the Parrot Debugger.
81 See F<docs/debugger.pod> for details on it.
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
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>
108 (in emacs) type "parrot" so it says "gdb parrot"
110 Step 3: Set a breakpoint on 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)>.
128 Step 6: load in the debugging information from the symbol file that the jit
131 gdb> add-symbol-file test.o 0
133 Step 7: Step into the JITed code
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:
163 shell> parrot --help-debug
170 If Parrot is built on a system with GNU libc it is capable of automatically
171 generating a backtrace on C<stderr> for debugging purposes. Currently these
172 automatically backtraces are only generated by assertion failures but in the
173 future they also be produced by other bad events (for example, C<SEGV>).
175 Here is an example of a what a backtrace might look like:
177 Backtrace - Obtained 15 stack frames (max trace depth is 32).
180 Parrot_make_COW_reference
181 Parrot_String_get_string
187 Parrot_runops_fromc_args
195 It must be noted that glibc's backtraces are not without limitation. It's
196 method depends completely on information that is available at run time.
200 =item * Functions marked as C<static> can only be identified by address as they
201 have no "symbol name" for dynamic linking in the executable's symbol table.
202 Static functions will appears as C<(unknown)>.
204 =item * There must be some means available for walking the stack at runtime.
205 On x86(-64)? the "stack pointer" must be in C<[re]sp> register. For example,
206 this C<gcc> compliiation flag would break backtracing (except for functions
207 that do dynamic allocation on the stack as this optimization can no be allied
208 to them). C<perl Configure.pl --ccflags=-fomit-frame-pointer>
210 =item * Some platforms may require extra linker flags in order to get all of
211 the required symbols exported in the symbol table. C<Configure.pl
214 =item * Any debugging information embedded in the object is not accessible. So
215 file and line number can not be included as part of the backtrace information.
217 =item * Be warned that signals may cause incorrect backtraces!
223 On systems not equipped with libc, one will need to use an external debugger to
224 get backtrace information. This method is actually more capable then the
225 L<auto-magical> approach as most debuggers will use debugging information if
226 it's available in the object code (for example, if parrot was built with
229 Since the C<Parrot_confess> symbol is I<always> compiled into parrot it can be
230 used as a break point to obtain a backtrace. Here is an example of doing this
231 with gdb and a version of parrot compiled with C<gcc> and the C<-g> flag.
235 Copyright (C) 2006 Free Software Foundation, Inc.
236 GDB is free software, covered by the GNU General Public License, and you are
237 welcome to change it and/or distribute copies of it under certain conditions.
238 Type "show copying" to see the conditions.
239 There is absolutely no warranty for GDB. Type "show warranty" for details.
240 This GDB was configured as "i686-pc-linux-gnu"...
241 Using host libthread_db library "/lib/libthread_db.so.1".
243 Breakpoint 1 at 0x80488a0: file src/main.c, line 38.
245 Starting program: /home/moanui/jhoblitt/parrot/parrot foo.pir
246 Failed to read a valid object file image from memory.
247 [Thread debugging using libthread_db enabled]
248 [New Thread -1213900128 (LWP 23148)]
249 [Switching to Thread -1213900128 (LWP 23148)]
251 Breakpoint 1, main (argc=-400292727, argv=0x159a0) at src/main.c:38
253 (gdb) b Parrot_confess
254 Breakpoint 2 at 0xb7c542a0: file src/exceptions.c, line 767.
257 [New Thread -1214039152 (LWP 23151)]
258 [New Thread -1222431856 (LWP 23152)]
261 Breakpoint 2, Parrot_confess (cond=0xb7eeda65 "s",
262 file=0xb7eeda58 "src/string.c", line=129) at src/exceptions.c:767
263 warning: Source file is more recent than executable.
266 #0 Parrot_confess (cond=0xb7eeda65 "s", file=0xb7eeda58 "src/string.c",
267 line=129) at src/exceptions.c:767
269 #1 0xb7c433b1 in Parrot_make_COW_reference (interp=0x804e008, s=0x0)
271 d = (STRING *) 0x81c21b8
272 __PRETTY_FUNCTION__ = "Parrot_make_COW_reference"
273 #2 0xb7e40db3 in Parrot_String_get_string (interp=0x804e008, pmc=0x81c8578)
274 at src/pmc/string.c:310
276 #3 0xb7cc7d41 in Parrot_set_s_p (cur_opcode=0x825d470, interp=0x804e008)
277 at src/ops/set.ops:159
279 #4 0xb7c9da32 in runops_slow_core (interp=0x804e008, pc=0x825d470)
280 at src/runops_cores.c:184
282 #5 0xb7c67acf in runops_int (interp=0x804e008, offset=0)
283 at src/interpreter.c:816
284 pc = (opcode_t * const) 0x8239730
285 lo_var_ptr = 134537224
286 core = (opcode_t *(*)(Parrot_Interp,
287 opcode_t *)) 0xb7c9d940 <runops_slow_core at src/runops_cores.c:169>
288 #6 0xb7c6854e in runops (interp=0x804e008, offs=0) at src/inter_run.c:100
291 our_runloop_level = 1
293 #7 0xb7c687da in runops_args (interp=0x804e008, sub=0x8204d58, obj=0x80912d8,
294 meth_unused=0x0, sig=0xb7eefca6 "vP",
295 ap=0xbfec614c "@M \b�b��P�\222K\230�\004\b@\236\"\b@M \bXM
296 \b\004����t��\b�\004\b\001") at src/inter_run.c:216
298 dest = (opcode_t *) 0x8239730
299 ctx = (parrot_context_t *) 0x822a3b0
301 sig_p = 0xb7eefca7 "P"
302 old_ctx = (parrot_context_t * const) 0x804e298
303 #8 0xb7c688fb in Parrot_runops_fromc_args (interp=0x804e008, sub=0x8204d58,
304 sig=0xb7eefca6 "vP") at src/inter_run.c:293
305 args = 0xbfec614c "@M \b�b��P�\222K\230�\004\b@\236\"\b@M
306 \bXM \b\004����t��\b�\004\b\001"
307 ctx = (parrot_context_t *) 0xb7fa1548
308 #9 0xb7c50c51 in Parrot_runcode (interp=0x804e008, argc=1, argv=0xbfec62e8)
310 userargv = (PMC *) 0x8204d40
311 main_sub = (PMC *) 0x8204d58
312 #10 0xb7ed74a1 in imcc_run_pbc (interp=0x804e008, obj_file=0, output_file=0x0,
313 argc=1, argv=0xbfec62e8) at compilers/imcc/main.c:614
315 #11 0xb7ed7d90 in imcc_run (interp=0x804e008, sourcefile=0xbfec6e0a "foo.pir",
316 argc=1, argv=0xbfec62e8) at compilers/imcc/main.c:815
318 yyscanner = (yyscan_t) 0x822a090
320 #12 0x080489b7 in main (argc=136704448, argv=0x825f220) at src/main.c:62
321 sourcefile = 0xbfec6e0a "foo.pir"
322 interp = (Interp *) 0x804e008
323 executable_name = (STRING *) 0x821b8e4
324 executable_name_pmc = (PMC *) 0x8204d70