Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a program which I am trying to run, however when I run it; it just complains that it can't find a particular file.

However I have no idea which folder it is trying to find this particular file in. I have a copy of the required file, I just need to know which folder to copy it too.

Is there any way to show in real time which files are being accessed or which files are trying and failing to be accessed?

I am using Ext4 filesystem if that helps.

Thanks

share|improve this question
    
Please add more details about the problem. What was the particular program you were trying to run and how you tried it? –  Anwar Shah Sep 23 '12 at 4:23
    
It's an in-house program. I've had this problem with various other programs too in the past so I'm looking for a general solution. –  user9069 Sep 23 '12 at 5:44
add comment

3 Answers

up vote 6 down vote accepted

(I first posted my answer at this askubuntu question, but removed it from there and posted it here, as it is more relevant.)

There are several ways you can investigate what files and libraries processes have opened; the main tools are lsof, strace and ltrace. Sometimes it is necesary to run them with sudo, so the program has access to everything it needs to gain an accurate snapshot of what a program is calling.

1) With lsof you need to find the process id you want to query, so use, for example:

lsof -c firefox

which will list all the files firefox has open and all its calls to system shared libraries. (Firefox currently has a pid of 3310 on my system, so you could also use lsof -p 3310, but you would have had to look up the process id first with ps aux | grep [f]irefox ). More information on lsof is detailed in this useful IBM article.

2) ltrace and strace have very similar functions and options, and are very useful for finding out detailed information about what calls a process is making. The main difference is that ltrace usually only traces library calls (although it can trace system calls with the -S option), while strace traces both library and other system calls. More information on tracing processes is available in this IBM article. Lsof is probably more useful for you if you want to examine an already running process but strace can also do this and monitor the calls in real time when given a process pid (sudo is always used when one attaches to a process):

sudo strace -p 3310

but it is much more useful to launch the program with strace and see what was called, as in the following example:

strace -f -e trace=open /usr/bin/firefox

You can just run strace with the target process and no options, but the switches here mean that child processes are traced (-f) and that all open system calls are traced (-e trace=open) If you want to save the output to file you can specify -o ~/firefox.trace before specifying /usr/bin/firefox.

If you want a summary list of library calls, for example, you could use

ltrace -c /usr/bin/leafpad

and then exit the program and the list would be produced. Omit the -c option to view the calls in real time.

The results from strace and ltrace may not be greatly useful to you, as they are difficult to decipher unless you know what you are looking for, but lsof should provide some basic useful information.

share|improve this answer
add comment

Easiest way to do this seems to be to run the program through strace and watch for stat64() calls (which get file info) or open() calls which open files.

So you do:

strace -e stat64 -e open [program name]

And in the resulting output look for:

(No such file or directory)

The path it was trying to open should be to the left of this error.

share|improve this answer
add comment

It is very hard to say where this in-house program is trying to access a file. It really depends on the program.

For example, You could build a program which may try to access a file in /usr/share or ~/.local/share. It completely depends on the source code and the developer who developed the program. For the same reason, It is really hard to give a general solution as per user request

I suppose, you are trying to build a C program. Most of the time, C programs first try to find the file in the directory where it starts execution (This also depends on code. If you use a complete path name, the program will look at the specified path, not in the current execution directory)

If you are running your program in an IDE, such as Netbeans or Eclipse, the execution path is often the root directory of the project's folder.

share|improve this answer
    
There must be a way to do this on a lower level? Like see systems calls that are trying to access files? –  user9069 Sep 23 '12 at 18:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.