CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Problem opening cmd output as file using xargs


New User

Nov 29, 2016, 9:30 AM

Post #1 of 4 (11731 views)
Problem opening cmd output as file using xargs Can't Post

I'm trying to debug a program that uses xargs to feed a file of hex addresses (one per line) to addr2line. The output is piped to an open command to be read in and used later.

I didn't write the program but after days of debugging, I realize I need some help.

Here's the subroutine in question:

sub src_line_info { 
my ($addr2line, $exec) = @_;
my ($cmd) = "xargs $addr2line -e $exec";
my ($tmpfile) = tmpnam();
my (@a2loutput);

open(ADDRS, ">$tmpfile") or die "open($tmpfile) failed: $!\n";
foreach my $ra (@trace_addrs) {
print ADDRS "$ra->[TADDR_ADDRESS]\n";

$cmd = "$cmd < $tmpfile |";

open(LINES, $cmd) or die "open($cmd) failed: $!\n";
while (<LINES>) {
push(@a2loutput, [split(/:/, $_)]);

The problem is that either the "open(LINES, $cmd)" or "while(<LINES>)" is returning the following error message:

"sh: /sw/packages/findutils/4.1/bin/xargs: Permission denied"

I've verified that:
- addr2line is executable
- tmpfile is readable
- the $exec file is rwx
- xargs is -rwxr-xr-x. 1 4294967294 4294967294 64484 Oct 28 2003 /sw/packages/findutils/4.1/bin/xargs
- the open command error message is not output
- I can execute the command line in $cmd with out the "|" manually

I tried executing xargs with -p to make it prompt so I could see if there was a specific error message from the temp file that wasn't coming back that I could check, but it didn't work.

I've used different files to ultimately generate the tmpfile, so it isn't the tmpfile itself that is the problem.

I'm out of ideas. Any help is appreciated.

One final thing. This code is from a build tree and will be invoked mechanically by a tool, so I can't really modify it directly. I will have to find ways to modify the code it calls (addr2line/xargs versions, etc). But I can modify it to debug it and figure out what's going on if you have any ideas on that front.

Many thanks!!

(This post was edited by FishMonger on Nov 29, 2016, 10:00 AM)


Nov 29, 2016, 8:45 PM

Post #2 of 4 (11714 views)
Re: [cben] Problem opening cmd output as file using xargs [In reply to] Can't Post

We cannot help you. You have not shown us the value of any of the arguments to this function or of the global variables that it references. We do not know what program $addr2line refers to and cannot begin to guess what arguments it requires.

The following line is probably not valid:
<code>print ADDRS "$ra->[TADDR_ADDRESS\n";</code>

(Does $ra contain a hash reference? If so, you need braces rather than square brackets.)

I recommend that you use debug to examine the value of $cmd that you are passing to open. Is it exactly the same as the command that you ran in the shell? At the same time you can check the contents and permission of your temporary file. Are they exactly what you expect? If you are not comfortable with debug, add temporary code to display the same information. Let us know what you discover.
Good Luck,

Veteran / Moderator

Nov 30, 2016, 6:28 AM

Post #3 of 4 (11704 views)
Re: [BillKSmith] Problem opening cmd output as file using xargs [In reply to] Can't Post

Cross posted on perlmonks where the OP provided more info.


Dec 29, 2016, 10:51 AM

Post #4 of 4 (10860 views)
Re: [cben] Problem opening cmd output as file using xargs [In reply to] Can't Post

First, you use some very antiquated notations which are deprecated. See this page for more info:

You have also used fewer lines to do the task, when more lines helps with debugging. Here's how I would rewrite the code.

sub src_line_info {  
my ($addr2line, $exec) = @_;
my ($cmd) = "xargs $addr2line -e $exec";
my ($tmpfile) = tmpnam();
my (@a2loutput);

open($ADDRS, ">", $tmpfile) or die "open($tmpfile) failed: $!\n";
foreach my $ra (@trace_addrs) {
print $ADDRS "$ra->[TADDR_ADDRESS]\n";

$cmd = "$cmd < $tmpfile |";

open($LINES, $cmd) or die "open($cmd) failed: $!\n";
while (my $line = <$LINES>)
my @a=split(':',$line);
push(@a2loutput, YOURSTUFFHERE);

Well, close enough. Fiddle with your code, make a couple more variables to make debugging easier, and try that.

Also if you want to capture the output of a system command to an array just to this:

my @out=`ls *.txt`;

The output will be one line of text per position in @out.

(This post was edited by bulrush on Dec 29, 2016, 10:54 AM)


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives