scripts/gdb/linux/bpf.py

Source file repositories/reference/linux-study-clean/scripts/gdb/linux/bpf.py

File Facts

System
Linux kernel
Corpus path
scripts/gdb/linux/bpf.py
Extension
.py
Size
7806 bytes
Lines
254
Domain
Support Tooling And Documentation
Bucket
scripts
Inferred role
Support Tooling And Documentation: scripts
Status
atlas-only

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

# SPDX-License-Identifier: GPL-2.0

import json
import subprocess
import tempfile

import gdb

from linux import constants, lists, radixtree, utils


if constants.LX_CONFIG_BPF and constants.LX_CONFIG_BPF_JIT:
    bpf_ksym_type = utils.CachedType("struct bpf_ksym")
if constants.LX_CONFIG_BPF_SYSCALL:
    bpf_prog_type = utils.CachedType("struct bpf_prog")


def get_ksym_name(ksym):
    name = ksym["name"].bytes
    end = name.find(b"\x00")
    if end != -1:
        name = name[:end]
    return name.decode()


def list_ksyms():
    if not (constants.LX_CONFIG_BPF and constants.LX_CONFIG_BPF_JIT):
        return []
    bpf_kallsyms = gdb.parse_and_eval("&bpf_kallsyms")
    bpf_ksym_ptr_type = bpf_ksym_type.get_type().pointer()
    return list(lists.list_for_each_entry(bpf_kallsyms,
                                          bpf_ksym_ptr_type,
                                          "lnode"))


class KsymAddBreakpoint(gdb.Breakpoint):
    def __init__(self, monitor):
        super(KsymAddBreakpoint, self).__init__("bpf_ksym_add", internal=True)
        self.silent = True
        self.monitor = monitor

    def stop(self):
        self.monitor.add(gdb.parse_and_eval("ksym"))
        return False


class KsymRemoveBreakpoint(gdb.Breakpoint):
    def __init__(self, monitor):
        super(KsymRemoveBreakpoint, self).__init__("bpf_ksym_del",
                                                   internal=True)
        self.silent = True
        self.monitor = monitor

    def stop(self):
        self.monitor.remove(gdb.parse_and_eval("ksym"))
        return False


class KsymMonitor:
    def __init__(self, add, remove):
        self.add = add
        self.remove = remove

        self.add_bp = KsymAddBreakpoint(self)
        self.remove_bp = KsymRemoveBreakpoint(self)

        self.notify_initial()

    def notify_initial(self):
        for ksym in list_ksyms():

Annotation

Implementation Notes