1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 /* "undefine" structs in vmlinux.h, because we "override" them below */
4 #define bpf_iter_meta bpf_iter_meta___not_used
5 #define bpf_iter__task_file bpf_iter__task_file___not_used
6 #include "vmlinux.h"
7 #undef bpf_iter_meta
8 #undef bpf_iter__task_file
9 #include <bpf/bpf_helpers.h>
10 #include <bpf/bpf_tracing.h>
11 
12 char _license[] SEC("license") = "GPL";
13 
14 struct bpf_iter_meta {
15 	struct seq_file *seq;
16 	__u64 session_id;
17 	__u64 seq_num;
18 } __attribute__((preserve_access_index));
19 
20 struct bpf_iter__task_file {
21 	struct bpf_iter_meta *meta;
22 	struct task_struct *task;
23 	__u32 fd;
24 	struct file *file;
25 } __attribute__((preserve_access_index));
26 
27 SEC("iter/task_file")
28 int dump_task_file(struct bpf_iter__task_file *ctx)
29 {
30 	struct seq_file *seq = ctx->meta->seq;
31 	struct task_struct *task = ctx->task;
32 	__u32 fd = ctx->fd;
33 	struct file *file = ctx->file;
34 
35 	if (task == (void *)0 || file == (void *)0)
36 		return 0;
37 
38 	if (ctx->meta->seq_num == 0)
39 		BPF_SEQ_PRINTF(seq, "    tgid      gid       fd      file\n");
40 
41 	BPF_SEQ_PRINTF(seq, "%8d %8d %8d %lx\n", task->tgid, task->pid, fd,
42 		       (long)file->f_op);
43 	return 0;
44 }
45