Home > OS >  Running strace with a -o (redirect the trace output) parse the trace output while printing the stdou
Running strace with a -o (redirect the trace output) parse the trace output while printing the stdou

Time:12-30

I am trying to run some strace command as follows:

my @cmd = shell_quote(@ARGV);

strace -f -o trace.log -e trace=file @cmd

I want to go over the trace.log and parse it while it is running and also print the @cmd stdout to the screen.

Any idea how I should do that ... HELP :)

sub shell_unquote {

    my @strings = (@_);
    my $arg;
    for my $arg (@strings) {
        if(not defined $arg) {
            $arg = "";
        }
        $arg =~ s/'\n'/\n/g; # filenames with '\n' is quoted using \'
        $arg =~ s/\\([\002-\011\013-\032])/$1/g;
        $arg =~ s/\\([\#\?\`\(\)\{\}\*\>\<\~\|\; \"\!\$\&\'])/$1/g;
        $arg =~ s/\\\\/\\/g;
    }
    return wantarray ? @strings : "@strings";
}

CodePudding user response:

use IPC::Run qw( run );

run \@cmd,
   '2>', sub {
      # Do something with chunk in $_[0]...
   };

or

use IPC::Run qw( run );

run \@cmd,
   '2>', new_chunker, sub {
      # Do something with line in $_[0]...
   };

CodePudding user response:

Instead of starting cmd from perl, you could synchronize cmd with log parser using named pipe:

mknod mypipe p
strace -o mypipe cmd arg arg2 ... argN

and in another shell

perl -nle '/interesting/ and print' mypipe
  • Related