What options were used to open a file?

Problem

It's easy to see what files a process is accessing by looking at /proc/<pid>/fd/. However it doesn't seem to tell me about all options that were passed to open(2). In particular I'm keen to see whether files were opened with O_DIRECT set. Is there no way to make this inference after the file has been opened?

Scripts

list_flags.stp

# list_flags.stp
# Copyright (C) 2007 Red Hat, Inc., Eugene Teo <eteo@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#

%{ 
#include <linux/file.h>
%}

function list_flags:long (pid:long, fd:long) %{
        struct task_struct *p;
        struct list_head *_p, *_n;

        list_for_each_safe(_p, _n, &current->tasks) {
                p = list_entry(_p, struct task_struct, tasks);
                if (p->pid == (int)THIS->pid) {
                        struct file *filp;
                        struct files_struct *files = p->files;
                        spin_lock(&files->file_lock);
                        filp = fcheck_files(files, (int)THIS->fd);
                        THIS->__retvalue = (!filp ? -1 : filp->f_flags);
                        spin_unlock(&files->file_lock);
                        break;
                }
        }
%}

probe begin {
        flag_str = ( (flags = list_flags($1, $2)) ? _sys_open_flag_str(flags) : "???");
        printf("pid: %d, fd: %d: %s\n", $1, $2, flag_str)
        exit()
}

Output

Syntax: list_flags.stp [pid] [fd]

[eteo@kerndev ~]$ stap -vg list_flags.stp $$ 3 2>&1 | grep O_DIRECT
pid: 30830, fd: 3:
O_RDONLY|O_APPEND|O_CREAT|O_DIRECT|O_DIRECTORY|O_EXCL|O_LARGEFILE|O_NOATIME|O_NOCTTY|O_NOFOLLOW|O_NONBLOCK|O_SYNC|O_TRUNC
[eteo@kerndev ~]$ stap -V
SystemTap translator/driver (version 0.5.14 built 2007-04-17)
(Using Red Hat elfutils 0.125 libraries.)
Copyright (C) 2005-2007 Red Hat, Inc. and others
This is free software; see the source for copying conditions.

You can also do this:

[eteo@kerndev ~]$ stap -vg list_flags.stp $$ 3 -k -m list_flags
[...]
Keeping temporary directory "/tmp/staph3XLtC"
[eteo@kerndev ~]$ sudo staprun -L /tmp/staplcUmIS/list_flags.ko
pid: 30830, fd: 3:
O_RDONLY|O_APPEND|O_CREAT|O_DIRECT|O_DIRECTORY|O_EXCL|O_LARGEFILE|O_NOATIME|O_NOCTTY|O_NOFOLLOW|O_NONBLOCK|O_SYNC|O_TRUNC

Disconnecting from systemtap module.
To reconnect, type "staprun -A list_flags"
[eteo@kerndev ~]$ sudo staprun -A list_flags

Lessons

SystemTap allows you to access the open flags of a file descriptor easily, so that debugging an issue would be so much easier without the need to modify and recompile the kernel.


WarStories

None: WSListFlags (last edited 2008-01-10 19:47:34 by localhost)