How to trace files opened or closed by program

Problem

This is a typical problem in scientific computing programs. The code is huge and hairy. The program opens, reads or writes hundreds of files. You want to figure out what files the program is opening, reading or writing.

Solution

Thanks to Rohan for suggesting to use strace for this. It captures the system calls called by your program during execution. The system calls related to files is open and close. This information can be captured by this command:

$ strace -e trace=open,close -o trace.log ./foobar

The output can be found in the file trace.log after the program exits.

If you just need the files opened:

$ strace -e trace=open -o trace.log ./foobar

Tried with: Ubuntu 14.04

Advertisements

2 thoughts on “How to trace files opened or closed by program”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s