Hi! I am trying to execute strace from within a perl program. Basically, I want to strace a process, and see if it is blocking while waiting for a ("flock") lock on a file. Since I'm looking for blocking processes, strace will just hang on these -- so I use "alarm(1)" to prevent my script from hanging.
The first step is figuring out how to capture the output of strace, and I'm getting stuck. Here's what I have so far, which doesn't work:
#!/usr/bin/perl -w
use strict;
my $pid=$ARGV[0];
my $trace_out;
my $strace_pid;
eval {
local $SIG{ALRM} = sub {
kill 2, $strace_pid || warn "couldn't kill $strace_pid: $!";
close STRACE or die "couldn't close strace";
die "exiting eval";
};
$strace_pid = open (STRACE,"strace -q -p$pid 2>&1|") or die "couldn'
+t open strace";
alarm (1);
undef $/;
$trace_out=<STRACE> ;
};
print "traceout is $trace_out";
I have a test script that takes an exclusive lock on a file, and then just goes into an infinite loop:
#!/usr/bin/perl
use Fcntl ':flock';
open FH, "file.txt" ;
flock FH, LOCK_EX or die "couldn't lock";
while (true) {};
If I run two instances of this, (the first instance gets the lock, the 2nd blocks while waiting for a lock), and attach strace to them, here's what I see:
>./locker.pl &
[1] 17289
>./locker.pl &
[2] 17290
>strace -q -p17289
>strace -q -p17290
flock(3, LOCK_EX <unfinished ...>
As expected, pid 17289 has no strace output, since it's just looping. And 17290 is blocking attempting to take a lock.
But, when I run my script on pid 17290, it's not capturing the "flock(3, LOCK_EX" text:
./hang_finder 17290
traceout is
My intention is that the output should be:
traceout is flock(3, LOCK_EX
Obviously,I'm doing something wrong, but I can't figure out what . . .
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.