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
Best Answer
(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
andltrace
. Sometimes it is necesary to run them withsudo
, 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: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 uselsof -p 3310
, but you would have had to look up the process id first withps aux | grep [f]irefox
). More information onlsof
is detailed in this useful IBM article.2)
ltrace
andstrace
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 thatltrace
usually only traceslibrary
calls (although it can trace system calls with the-S
option), whilestrace
traces bothlibrary
and othersystem
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 butstrace
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):but it is much more useful to launch the program with
strace
and see what was called, as in the following example: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
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
andltrace
may not be greatly useful to you, as they are difficult to decipher unless you know what you are looking for, butlsof
should provide some basic useful information.