drgn/examples/linux/bpf.py
Andrey Ignatov 421bda23f9 examples: Print bpf programs and maps
Add example to print basic info about bpf programs and maps (id, type,
name).

Example output:

  # python36 -m drgn examples/linux/bpf.py
  Usage: examples/linux/bpf.py { prog | map }
  # python36 -m drgn examples/linux/bpf.py p | head
       9: BPF_PROG_TYPE_CGROUP_SKB              name
      10: BPF_PROG_TYPE_CGROUP_SKB              name
   23368: BPF_PROG_TYPE_CGROUP_SOCK_ADDR        name tw_ipt_bind
   23369: BPF_PROG_TYPE_CGROUP_SOCK_ADDR        name tw_ipt_connect
   23370: BPF_PROG_TYPE_SOCK_OPS                name tw_ipt_listen
   23371: BPF_PROG_TYPE_CGROUP_SOCK_ADDR        name tw_ipt_sendmsg
   59538: BPF_PROG_TYPE_CGROUP_SYSCTL           name tw_sysctl
   97104: BPF_PROG_TYPE_SCHED_CLS               name fbflow_egress
   97105: BPF_PROG_TYPE_SCHED_CLS               name fbflow_ingress
   97106: BPF_PROG_TYPE_KPROBE                  name fbflow_tcp_conn
  # python36 -m drgn examples/linux/bpf.py m | head
       8: BPF_MAP_TYPE_LPM_TRIE                 name
       9: BPF_MAP_TYPE_LPM_TRIE                 name
      12: BPF_MAP_TYPE_PERCPU_ARRAY             name fbflow_pkt_info
   16305: BPF_MAP_TYPE_ARRAY                    name tw_ipt_cfg
   16306: BPF_MAP_TYPE_ARRAY                    name tw_tcp_ports_ma
   16307: BPF_MAP_TYPE_LRU_HASH                 name tw_ipt_lo_sk
   42606: BPF_MAP_TYPE_HASH                     name tw_sysctl_ro
   55338: BPF_MAP_TYPE_HASH                     name hash_min
   55341: BPF_MAP_TYPE_HASH                     name hash_8k_1
   55342: BPF_MAP_TYPE_HASH                     name hash_8k_8

Signed-off-by: Andrey Ignatov <rdna@fb.com>
2020-01-24 10:53:47 -08:00

27 lines
922 B
Python

"""List all BPF maps or programs"""
import sys
from drgn.helpers import enum_type_to_class
from drgn.helpers.linux import bpf_map_for_each, bpf_prog_for_each
BpfMapType = enum_type_to_class(prog.type("enum bpf_map_type"), "BpfMapType")
BpfProgType = enum_type_to_class(prog.type("enum bpf_prog_type"), "BpfProgType")
if "prog".startswith(sys.argv[-1]):
for prog in bpf_prog_for_each(prog):
id_ = prog.aux.id.value_()
type_ = BpfProgType(prog.type).name
name_ = prog.aux.name.string_().decode()
print("{:>6}: {:37} name {}".format(id_, type_, name_))
elif "map".startswith(sys.argv[-1]):
for map_ in bpf_map_for_each(prog):
id_ = map_.id.value_()
type_ = BpfMapType(map_.map_type).name
name_ = map_.name.string_().decode()
print("{:>6}: {:37} name {}".format(id_, type_, name_))
else:
print("Usage: {} {{ prog | map }}".format(sys.argv[0]))