Path: blob/master/tools/bpf/bpftool/skeleton/pid_iter.bpf.c
26295 views
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)1/* Copyright (c) 2020 Facebook */2#include <vmlinux.h>3#include <bpf/bpf_helpers.h>4#include <bpf/bpf_core_read.h>5#include <bpf/bpf_tracing.h>6#include "pid_iter.h"78/* keep in sync with the definition in main.h */9enum bpf_obj_type {10BPF_OBJ_UNKNOWN,11BPF_OBJ_PROG,12BPF_OBJ_MAP,13BPF_OBJ_LINK,14BPF_OBJ_BTF,15};1617struct bpf_perf_link___local {18struct bpf_link link;19struct file *perf_file;20} __attribute__((preserve_access_index));2122struct perf_event___local {23u64 bpf_cookie;24} __attribute__((preserve_access_index));2526enum bpf_link_type___local {27BPF_LINK_TYPE_PERF_EVENT___local = 7,28};2930extern const void bpf_link_fops __ksym;31extern const void bpf_link_fops_poll __ksym __weak;32extern const void bpf_map_fops __ksym;33extern const void bpf_prog_fops __ksym;34extern const void btf_fops __ksym;3536const volatile enum bpf_obj_type obj_type = BPF_OBJ_UNKNOWN;3738static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type)39{40switch (type) {41case BPF_OBJ_PROG:42return BPF_CORE_READ((struct bpf_prog *)ent, aux, id);43case BPF_OBJ_MAP:44return BPF_CORE_READ((struct bpf_map *)ent, id);45case BPF_OBJ_BTF:46return BPF_CORE_READ((struct btf *)ent, id);47case BPF_OBJ_LINK:48return BPF_CORE_READ((struct bpf_link *)ent, id);49default:50return 0;51}52}5354/* could be used only with BPF_LINK_TYPE_PERF_EVENT links */55static __u64 get_bpf_cookie(struct bpf_link *link)56{57struct bpf_perf_link___local *perf_link;58struct perf_event___local *event;5960perf_link = container_of(link, struct bpf_perf_link___local, link);61event = BPF_CORE_READ(perf_link, perf_file, private_data);62return BPF_CORE_READ(event, bpf_cookie);63}6465SEC("iter/task_file")66int iter(struct bpf_iter__task_file *ctx)67{68struct file *file = ctx->file;69struct task_struct *task = ctx->task;70struct pid_iter_entry e;71const void *fops;7273if (!file || !task)74return 0;7576switch (obj_type) {77case BPF_OBJ_PROG:78fops = &bpf_prog_fops;79break;80case BPF_OBJ_MAP:81fops = &bpf_map_fops;82break;83case BPF_OBJ_BTF:84fops = &btf_fops;85break;86case BPF_OBJ_LINK:87if (&bpf_link_fops_poll &&88file->f_op == &bpf_link_fops_poll)89fops = &bpf_link_fops_poll;90else91fops = &bpf_link_fops;92break;93default:94return 0;95}9697if (file->f_op != fops)98return 0;99100__builtin_memset(&e, 0, sizeof(e));101e.pid = task->tgid;102e.id = get_obj_id(file->private_data, obj_type);103104if (obj_type == BPF_OBJ_LINK &&105bpf_core_enum_value_exists(enum bpf_link_type___local,106BPF_LINK_TYPE_PERF_EVENT___local)) {107struct bpf_link *link = (struct bpf_link *) file->private_data;108109if (BPF_CORE_READ(link, type) == bpf_core_enum_value(enum bpf_link_type___local,110BPF_LINK_TYPE_PERF_EVENT___local)) {111e.has_bpf_cookie = true;112e.bpf_cookie = get_bpf_cookie(link);113}114}115116bpf_probe_read_kernel_str(&e.comm, sizeof(e.comm),117task->group_leader->comm);118bpf_seq_write(ctx->meta->seq, &e, sizeof(e));119120return 0;121}122123char LICENSE[] SEC("license") = "Dual BSD/GPL";124125126