0

Within the linux kernel (4.5.5), I have added a printk() to mm/filemap.c.

The code that I have added is inside of __generic_file_write_iter():

if(io_tracing_on) {
        ssize_t write_size = iov_length(from->iov, from->nr_segs);
        printk(KERN_INFO "write size=%zu, pid=%d, inode=%lu\n", write_size, task_pid_nr(current), inode->i_ino);
}

When io_tracing_on evaluates to true, I get a continuous stream of output (discussed in a different question) as follows:

Jun 27 15:00:41 malka kernel: [  463.424155] write size=168, pid=715, inode=7864653
Jun 27 15:00:41 malka kernel: [  463.428064] write size=168, pid=715, inode=7864354

During this continuous stream of output, I have tried to map the pid (715) to my the output of ps -ef, to no avail -- the pid does not exist in the process list. Is there a way for me to determine what process task_pid_nr(current) is referring to?

Community
  • 1
  • 1
buratino
  • 1,408
  • 2
  • 17
  • 40
  • You might want to break some lines in your code - The important part of the `printk()` line only shows up in edit mode on my screen – tofro Jun 28 '16 at 11:07
  • @tofro Interesting. Do you not also get a horizontal scroll bar for that block of code? – buratino Jun 28 '16 at 12:07
  • 1
    Does the virtual PID returned by `task_pid_vnr(current)` appear in the output of `ps`? – Ian Abbott Jun 28 '16 at 13:44
  • 1
    The pid in kernel have a little bit different meaning, you should use tgid to match pid from userspace perspective http://stackoverflow.com/questions/9305992/linux-threads-and-process – Alex Hoppus Jun 28 '16 at 17:33
  • @IanAbbott it does not. The vpid matches the pid in all of my output. Under what circumstances would the output of `task_pid_vnr(current)` differ from `task_pid_nr(current)` for a calling process from userspace? – buratino Jun 29 '16 at 09:51
  • @AlexHoppus using `task_tgid_nr(current)` gave me the userspace pid that I was looking for. If you want to formalize that comment as an answer, I can mark this question as answered. – buratino Jun 29 '16 at 09:53
  • 1
    @buratino, `task_pid_vnr(current)` would only differ from `task_pid_nr(current)` if a process namespace was being used, say in a containerized environment. – Ian Abbott Jun 29 '16 at 11:56

1 Answers1

1

The pid in kernel have a little bit different meaning, you should use tgid to match pid from userspace perspective

Community
  • 1
  • 1
Alex Hoppus
  • 3,821
  • 4
  • 28
  • 47