1acf61631SYonghong Song // SPDX-License-Identifier: GPL-2.0
2acf61631SYonghong Song /* Copyright (c) 2020 Facebook */
384544f56SYonghong Song #include "bpf_iter.h"
4acf61631SYonghong Song #include <bpf/bpf_helpers.h>
5acf61631SYonghong Song 
6acf61631SYonghong Song char _license[] SEC("license") = "GPL";
7acf61631SYonghong Song 
8858e8b2eSYonghong Song int count = 0;
9858e8b2eSYonghong Song int tgid = 0;
10*b3e1331eSKui-Feng Lee int last_tgid = 0;
11*b3e1331eSKui-Feng Lee int unique_tgid_count = 0;
12858e8b2eSYonghong Song 
13acf61631SYonghong Song SEC("iter/task_file")
dump_task_file(struct bpf_iter__task_file * ctx)14acf61631SYonghong Song int dump_task_file(struct bpf_iter__task_file *ctx)
15acf61631SYonghong Song {
16acf61631SYonghong Song 	struct seq_file *seq = ctx->meta->seq;
17acf61631SYonghong Song 	struct task_struct *task = ctx->task;
18acf61631SYonghong Song 	struct file *file = ctx->file;
19*b3e1331eSKui-Feng Lee 	__u32 fd = ctx->fd;
20acf61631SYonghong Song 
21acf61631SYonghong Song 	if (task == (void *)0 || file == (void *)0)
22acf61631SYonghong Song 		return 0;
23acf61631SYonghong Song 
24858e8b2eSYonghong Song 	if (ctx->meta->seq_num == 0) {
25858e8b2eSYonghong Song 		count = 0;
26acf61631SYonghong Song 		BPF_SEQ_PRINTF(seq, "    tgid      gid       fd      file\n");
27858e8b2eSYonghong Song 	}
28858e8b2eSYonghong Song 
29858e8b2eSYonghong Song 	if (tgid == task->tgid && task->tgid != task->pid)
30858e8b2eSYonghong Song 		count++;
31acf61631SYonghong Song 
32*b3e1331eSKui-Feng Lee 	if (last_tgid != task->tgid) {
33*b3e1331eSKui-Feng Lee 		last_tgid = task->tgid;
34*b3e1331eSKui-Feng Lee 		unique_tgid_count++;
35*b3e1331eSKui-Feng Lee 	}
36*b3e1331eSKui-Feng Lee 
37acf61631SYonghong Song 	BPF_SEQ_PRINTF(seq, "%8d %8d %8d %lx\n", task->tgid, task->pid, fd,
38acf61631SYonghong Song 		       (long)file->f_op);
39acf61631SYonghong Song 	return 0;
40acf61631SYonghong Song }
41