NAME ^

docs/debug.pod - Debugging Parrot

ABSTRACT ^

This document describes ways to debug various parts of Parrot.

THE PARROT AND IMCC BINARIES ^

First, you'll want to use the --debugging flag with Configure.pl to generate debugging symbols for the parrot binary:

    % perl Configure.pl --debugging
    % make

You can then run the parrot binary under gdb.

You could also run all the tests with valgrind, by running

    % make test IMCC="valgrind -q languages/imcc/imcc"

MEMORY MANAGEMENT ^

Some of the more frequent and exasperating parrot bugs are related to memory management in general, and garbage collection in particular.

Infant mortality

See docs/dev/infant.dev for details of one frequent problem: infant mortality. Infant mortality is when you create a Parrot object, but the garbage collector runs before you put it into a Parrot register or in something else that is itself within a Parrot register.

To help in resolving these issues, the parrot binary accepts a --gc-debug flag that makes such problems much more likely to occur (it makes garbage collection occur as frequently as possible, to maximize the probability that any newborn objects will run afoul of the garbage collector if they are improperly coded.)

Within the --gc-debug mode, there is another tool to help narrow down the problem. If you edit src/dod.c and #define the GC_VERBOSE flag to 1, then after the garbage collector has traced all objects to find which ones are still alive, it will scan through all of the dead objects to see if any of them believe they are alive (which will happen for infants, since they come into existence marked live.) If it finds any, it will print them out. You can then re-run the program with a breakpoint set on the routine that allocated the object (e.g. get_free_object in src/smallobject.c). You'll probably want to make the breakpoint conditional on the object having the version number that was reported, because the same memory location will probably hold many different objects over the lifetime of the program.

IMCC AND PASM CODE ^

Let's say you have written (or generated) a huge .pasm or .imc file. It's not working. You'd like some help in figuring out why.

pdb ^

One possible tool is pdb, the Parrot Debugger. See docs/debugger.pod for details on it.

stabs ^

If you are running on a jit-capable machine, you can also try using GDB by having the JIT generate stabs metadata and then stepping through the code with GDB as if it were any other language.

To use this, you'll want to use imcc to generate your bytecode (.pbc file). It is not strictly necessary, but you'll get more information into the bytecode this way.

Let's say your file is named test.pasm. (Note: these instructions will also work if you use test.imc everywhere test.pasm occurs.)

Step 1: Generate the .pbc file with extra debugging information.

  % imcc -d -o test.pbc test.pasm

Step 2: Start up parrot under GDB

  % gdb parrot

or

  % emacs &
  (in emacs) M-x gdb
  (in emacs) type "parrot" so it says "gdb parrot"

Step 3: Set a breakpoint on runops_jit

  gdb> b runops_jit

Step 4: Run your program under GDB with JIT and debugging on

  gdb> run -j -d test.pbc

Step 5: GDB will stop at the beginning of runops_jit. Step through the lines until just before the JITted code is executed (the line will be something like (jit_code)(interpreter,pc).

  gdb> n
  gdb> n
  .
  .
  .

Step 6: load in the debugging information from the symbol file that the jit just generated.

  gdb> add-symbol-file test.o 0

Step 7: Step into the JITted code

  gdb> s

At this point, you can step through the instructions, or print out the various Parrot registers. GDB will know about I0-I31, N0-N31, S0-S31, and P0-P31.

WARNING: Stepping too far

One thing to watch out for is that GDB gets confused when attempting to step over certain instructions. The only ones that I have noticed having problems is keyed operations. With my version of GDB, if I do 'n' to step over the instruction, GDB will start running and only stop when the entire parrot program has finished. To work around this, do 'si' twice just before executing any keyed op. For some reason, GDB can then figure out when it's supposed to stop next. If you know of a better technique, please let the mailing list know (perl6-internals@perl.org).

IMCC CODE GENERATION ^

The imcc binary has a bunch of debugging flags for spewing out information about various aspects of its processing. See languages/imcc/docs/running.pod for a list of flags.

HISTORY ^

Version 1.0

First version by Steve Fink <steve@fink.com>


parrot