9

I'm trying to view the filename via kgdb, so I cannot call functions and macros to get it programatically. I need to find it by manually inspecting data structures.

Like if I had a breakpoint here in gdb, how could I look around with gdb and find the filename?

I've tried looking around in filp.f_path, filp.f_inode, etc. I cannot see the filename anywhere.

ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
{
     struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
     struct kiocb kiocb;
     ssize_t ret;

     init_sync_kiocb(&kiocb, filp);
     kiocb.ki_pos = *ppos;
     kiocb.ki_left = len;
     kiocb.ki_nbytes = len;

     ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
     if (-EIOCBQUEUED == ret)
             ret = wait_on_sync_kiocb(&kiocb);
     *ppos = kiocb.ki_pos;
     return ret;
}
4

4 回答 4

30

You can get the filename from struct file *filp with filp->f_path.dentry->d_iname.

To get the full path call dentry_path_raw(filp->f_path.dentry,buf,buflen).

于 2013-12-16T15:39:59.947 回答
4

In the Linux kernel, the file structure is essentially how the kernel "sees" the file. The kernel is not interested in the file name, just the inode of the open file. This means that all of the other information which is important to the user is lost.


EDIT: This answer is wrong. You can get the dentry using filp->f_path.dentry. From there you can get the name of the dentry or the full path using the relevant FS flags.

于 2013-07-28T05:47:43.437 回答
1

The path is stored in the file->f_path structure as it's name implies. Just not in a plain-text form, but parsed into objects that are more useful for kernel operation, namely a chain of dentry structures, and the vfsmount structure pointing to the root of the current subtree.

You can use the d_path function to regenerate a human-readable path name for a struct path like file->f_path. Note that however this is not a cheap operation and it may slow down your workload significantly.

The above mentioned issues about open but unlinked files, multiple hardlinks and similar are valid for mapping from and inode to a pathname, and open file always has a path associated with it. If the file has been unlinked d_path will prepend a " (deleted)" to the name, and if the filename it has been opened with has been changed to something else using rename since it was opened d_path will not print the original name, but the current name of the entry that was used for opening it.

于 2013-08-08T08:39:08.137 回答
1

filp->f_path.dentry->d_name.name This worked for me

于 2021-08-31T12:39:49.823 回答