2021-04-05 22:35:39 +01:00
|
|
|
#!/usr/bin/env drgn
|
2021-11-21 23:59:44 +00:00
|
|
|
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2022-11-02 00:05:16 +00:00
|
|
|
# SPDX-License-Identifier: LGPL-2.1-or-later
|
2020-05-15 23:13:02 +01:00
|
|
|
|
2020-01-04 01:53:51 +00:00
|
|
|
"""List the paths of all descendants of a cgroup v2"""
|
|
|
|
|
2020-08-20 22:27:51 +01:00
|
|
|
from contextlib import contextmanager
|
2020-01-04 01:53:51 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from drgn import cast
|
2022-09-13 19:01:02 +01:00
|
|
|
from drgn.helpers.common.type import enum_type_to_class
|
2020-01-04 01:53:51 +00:00
|
|
|
from drgn.helpers.linux import (
|
2020-01-08 00:19:49 +00:00
|
|
|
cgroup_bpf_prog_for_each,
|
2020-01-04 01:53:51 +00:00
|
|
|
cgroup_path,
|
|
|
|
css_for_each_descendant_pre,
|
|
|
|
fget,
|
|
|
|
find_task,
|
|
|
|
)
|
|
|
|
|
2020-01-08 00:19:49 +00:00
|
|
|
BpfAttachType = enum_type_to_class(
|
|
|
|
prog.type("enum bpf_attach_type"),
|
|
|
|
"BpfAttachType",
|
|
|
|
exclude=("__MAX_BPF_ATTACH_TYPE",),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-01-04 01:53:51 +00:00
|
|
|
@contextmanager
|
|
|
|
def open_dir(*args, **kwds):
|
|
|
|
# Built-in open() context manager can't deal with directories.
|
|
|
|
fd = os.open(*args, **kwds)
|
|
|
|
try:
|
|
|
|
yield fd
|
|
|
|
finally:
|
|
|
|
os.close(fd)
|
|
|
|
|
|
|
|
|
|
|
|
def get_cgroup():
|
|
|
|
if len(sys.argv) == 1:
|
|
|
|
return prog["cgrp_dfl_root"].cgrp
|
|
|
|
task = find_task(prog, os.getpid())
|
|
|
|
with open_dir(sys.argv[1], os.O_RDONLY) as fd:
|
|
|
|
file_ = fget(task, fd)
|
|
|
|
kn = cast("struct kernfs_node *", file_.f_path.dentry.d_inode.i_private)
|
|
|
|
return cast("struct cgroup *", kn.priv)
|
|
|
|
|
|
|
|
|
2020-01-08 00:19:49 +00:00
|
|
|
def print_cgroup_bpf_progs(cgrp):
|
|
|
|
cgroup_printed = False
|
|
|
|
for attach_type in BpfAttachType:
|
|
|
|
attach_flags = cgrp.bpf.flags[attach_type.value].value_()
|
|
|
|
for prog in cgroup_bpf_prog_for_each(cgrp, attach_type.value):
|
|
|
|
prog_id = prog.aux.id.value_()
|
|
|
|
prog_name = prog.aux.name.string_().decode()
|
|
|
|
if not cgroup_printed:
|
|
|
|
print(cgroup_path(cgrp).decode())
|
|
|
|
cgroup_printed = True
|
|
|
|
print(
|
|
|
|
" {:<8} {:<30} {:<15} {:<15}".format(
|
|
|
|
prog_id, attach_type.name, attach_flags, prog_name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-01-04 01:53:51 +00:00
|
|
|
css = get_cgroup().self.address_of_()
|
|
|
|
|
|
|
|
for pos in css_for_each_descendant_pre(css):
|
2020-01-08 00:19:49 +00:00
|
|
|
if sys.argv[-1] == "bpf":
|
|
|
|
print_cgroup_bpf_progs(pos.cgroup)
|
|
|
|
else:
|
|
|
|
print(cgroup_path(pos.cgroup).decode())
|