As almut suggested, there are a number of tools (varying by flavour of Un*x) which let you watch the system calls made by a running program, regardless of what language it may be.

For Linux, the program is strace, and you do something like this:

$ strace perl -e 'open FOO, ">/tmp/foo"; print FOO "bar";' ...snip... open("/tmp/foo", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3 ioctl(3, SNDCTL_TMR_TIMEBASE, 0xbfffe2f0) = -1 ENOTTY (Inappropriate i +octl for device) _llseek(3, 0, [0], SEEK_CUR) = 0 fstat64(3, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0 fcntl64(3, F_SETFD, FD_CLOEXEC) = 0 brk(0) = 0x8143000 brk(0x8144000) = 0x8144000 write(3, "bar", 3) = 3 close(3) = 0 exit_group(0) = ?
See the open in the first line? That's a call to the system function open. You can find out what all the arguments mean by running man 2 open in your shell. The 3 at the end of that line is the return of that function - it is the file handle for that opened file. You can then follow the write and close functions that relate to that file.

You can use strace to watch what a running program is doing with the -p option, but be careful of doing this on a production box as some older versions of the linux kernel have a bug that could cause your program to halt when you exit strace.

The equivalent of strace on Solaris is truss. On MacOS X you need a combination of ktrace and kdump (read both man pages).


In reply to Logging system file calls from any Unix program by aufflick
in thread (OT) Logging open calls from a C program by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.