To Debug a Hanging Perl Script
Anonymous Monk
created: 2006-03-02 09:36:49
Dear Guru,
I have a perl script that "usually" works well. It is executed in this way.The code prints to STDOUT:
$ perl mycode.pl input_file.txt > output.txt
Now one time when it take certain file (smaller than usual one), my code above just "hang in there". It doesn't die off and not giving segfault message or any error message as per normal error message we encounter.

My question is what's is the best way to debug find out the cause of this hanging problem?
Re: To Debug a Hanging Perl Script
created: 2006-03-02 09:54:58
  1. make sure you've added "or die" (or "&& die" for system() calls) clauses to every instance where system interface functions are called (open, chdir, etc.)
  2. use strict; use warnings;
  3. change to 'perl mycode.pl input_file.txt 2&>output.txt' and run 'tail -f output.txt' from another console window to see where it stops
  4. post your code here

Don Wilde
"There's more than one level to any answer."
Re: To Debug a Hanging Perl Script
created: 2006-03-02 10:02:48
Auto-flush STDOUT ($| = 1;) so you can see everything that's printed as it is printed. Then you can add print statements (or warns) to track the progress of your script.
Re^2: To Debug a Hanging Perl Script
created: 2006-03-03 04:16:31

Along the same lines you could change your print statements to warn, which will give you information as to where you are in the script.

Re^2: To Debug a Hanging Perl Script
created: 2006-03-03 04:32:01
Hi [ikegami],
Where and how should I put this line in my script?
Auto-flush STDOUT ($| = 1;)
Re^3: To Debug a Hanging Perl Script
created: 2006-03-03 10:43:03
Anywhere. Presumably near the top so it gets executed soon. And it's:
$| = 1;  # Auto-flush STDOUT
Re: To Debug a Hanging Perl Script
created: 2006-03-02 10:25:10

If you're on Linux, use the strace utilitiy to tell you what your script is doing while it hangs. Either run

strace -f perl mycode.pl input_file.txt > output.txt

or start your script normally, wait till it hangs, find out the PID of the script process and execute

strace -f -p

strace prints to STDERR, so you'll see the strace output normally in your terminal while STDOUT still gets written to the output file. It will tell you what the process is doing on a system call level. To find out where it hangs at the perl level you could execute it in the debugger and step through until it hangs.

perl -d mycode.pl input_file.txt > output.txt

All dogma is stupid.
Re^2: To Debug a Hanging Perl Script
created: 2006-03-02 11:36:35

Similar utilities for other platforms are ktrace and kdump for BSDen (including OS X), and truss for Solaris.

They're all handy if you're really stumped, but go for tossing in a few debugging prints to STDERR and running with -d first.

perlmonks.org content © perlmonks.org and Anonymous Monk, dwildesnl, Fletch, ikegami, spiritway, tirwhan

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03