Skip to content

Helper function bpf_get_current_task

v4.8

Definition

Copyright (c) 2015 The Libbpf Authors. All rights reserved.

Get the current task.

Returns

A pointer to the current task struct.

static __u64 (* const bpf_get_current_task)(void) = (void *) 35;

Usage

This helper function returns a BPF‐safe pointer to the kernel’s task_struct (thread execution context) for the currently executing process or thread. This helper can be used to compute the on-CPU time for a process, identify kernel threads, get the current CPU's run queue, or retrieve many other pieces of information.

Program types

This helper call can be used in the following program types:

Example

#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>

SEC("tp/syscalls/sys_enter_open")
int sys_open_trace(void *ctx) {
    struct task_struct *task = (void *)bpf_get_current_task();
    int tgid = BPF_CORE_READ(task, tgid);
    bpf_printk("Hello from PID %d", tgid);
    return 0;
}