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

source code of linux/tools/testing/selftests/bpf/progs/bpf_iter_task_file.c