gri6507 has asked for the wisdom of the Perl Monks concerning the following question:

Wise monks,

Allow me to demonstrate what I'd like to do in terms of compiled C programs. First off, my environment is a Sun Solaris 8 box with Sun Workshop installed. Thus, my compiler is cc (used with the -g option). When compiled like this, I can use the workshop debugger to step through programs, evaluate variables, etc. Moreover, I can use workshop to attach to a running process (say, one that's in an infinite loop, or one that's hung) and see where in the code this running process is currently at and proceed with the standard debugging steps.

I'd like to be able to do the same with perl. In particular, I have a Tk GUI with a forked process (in effect a while(1) loop with brains). I'd like to attach a debugger to this forked process to see where it is during a particular time of execution, to see values of variables, etc. Is there any such tool? Free tools are, of course, prefered.

Thanks in advance.

Replies are listed 'Best First'.
Re: debugging a running perl script
by Fletch (Bishop) on Jun 18, 2004 at 14:08 UTC

    I can't remember ever using it personally, but if you look through perldoc perldebug for "TTY" you can find how to tell perl to use a specific TTY device for debugger IO. I believe what you do is run something like xterm -e sh -c 'tty ; sleep 99999999' and then point the perl debugger at that pty device. Suffice to say you'd have to set this stuff up before the process is forked as I'm fairly sure that you can't do something akin to gdb's attaching to an already running process.

Re: debugging a running perl script
by eXile (Priest) on Jun 18, 2004 at 15:07 UTC

    One trick I use in the perl debugger ('perl -d') is to have it execute until it receives a certain signal. This example shows how to do this for the USR1-signal:

    shark$ perl -d ./fork.pl Loading DB routines from perl5db.pl version 1.22 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(./fork.pl:5): $|=1; DB<1> sub break {} DB<2> $SIG{USR1}=\&break DB<3> b break DB<4> c
    After you do this, the program continues to run in the debugger until you send it the USR1-signal (ie. 'kill -USR1 <pid>'). When receiving this signal the program stops executing and is controlled again from the debugger prompt.

    I just found out that you can use this as wel with forked commands. Just send the USR1-signal to the child-process you want to debug. In my X-windows environment the debugger opened a child-debugger terminal, I don't know how smart the debugger is in other environments.

Re: debugging a running perl script
by matija (Priest) on Jun 18, 2004 at 14:25 UTC
    As long as you start the debugger beforehand....

    Run the program with the debugger enabled, pass c as the command to the debugger. The program now runs normaly.

    When you reach the point in your program where you want the debugger activated, press ^C in your console window. Instead of aborting (like it would if the debugger were not activated), the program will drop you to the debugger prompt.

    I don't think there's a way to do this for processes where the debugger was NOT activated first.

Re: debugging a running perl script
by coreolyn (Parson) on Jun 18, 2004 at 16:10 UTC
      I've been playing with EPIC (the perl plugin) for Eclipse for the past couple weeks. The Debugger **seems** to be broken. By "seems" I mean I, and many others in the EPIC forum have been unable to get it to work but the EPIC authors have it working. Perhaps my problem is trying to use it on Windows :-)

      Otherwise, Eclipse and EPIC are pretty spiffy.

      I did post on the EPIC forum how to hook in the Tk Debugger and the GUI tkTestRunner as external applications. Not quite as nice as the built in debugger and built in unit test available to the Java types, but I am sure the EPIC team will get there eventually...

      P.S. Both of these can be used w/o Eclipse, of course. The CPAN modules are Devel::ptkdb and Test::Unit

Re: debugging a running perl script
by Roger (Parson) on Jun 19, 2004 at 02:26 UTC
    Use the ddd visual debugger to debug your Perl scripts under Solaris 8.