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

I'd like to find out what files are used by shell-commands. The purpose is for automatic dependency detection on a make-like system. One approach is to wrap the command with strace:
use POSIX 'tmpnam'; my $cmd = "cat /dev/null"; my $trace_file = tmpnam; system "strace -f -e trace=open -o $trace_file $cmd"; my %files = (); open TRACE, "<$trace_file"; while (<TRACE>) { /^open\("(.*)", O_/ and $files{$1}++; } close TRACE; unlink $trace_file; print join "\n", sort keys %files;
Even after a perfect cleanup, this code would leave much to be desired: it is not portable. It doesn't even work under Cygwin! Another problem is that strace can't be invoked recursively, which could be an issue with recursive makefiles. I was wondering if, hidden inside one of the various Run::* modules (or elsewhere), there is simpler, more portable, way?

Dave.

Replies are listed 'Best First'.
Re: Detecting Files used by a shell command
by TVSET (Chaplain) on Jul 14, 2003 at 22:35 UTC
    Take a look at:

    • man ldd
    • man strings

    ldd will give you the idea of shared library dependancies, while strings might be used on a file to get the ASCII staff out, which is grepable afterwords.

    Also, RedHat Package Manager (RPM) sources can be interesting for you, since the functionality you seek is somewhat implemented there. (Check the /usr/lib/rpm directory if you have RPM-based Linux machine handy).

    Leonid Mamtchenkov aka TVSET

      Thanks for the reply, but those things don't really help: they aren't run-time mechanisms. Imagine gcc didn't have the "-MD" option: how would you know what header files are used (without preprocessing the C files)? strace tells you precisely which files gcc reads. Other programs don't have a -MD equivalent, hence the desire to trace system-calls at run time. Anyone who's used Clearcase (clearmake) should understand how nice it is to write makefiles without dependencies. Dave.