Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contrib/bpf_inspect.py: show used maps and subprogs for bpf prog #408

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 58 additions & 26 deletions contrib/bpf_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,38 @@ def get_progs(self):
return


class BpfMap(object):
def __init__(self, bpf_map):
self.map = bpf_map

@staticmethod
def inspect_owner(owner):
type_ = BpfProgType(owner.type).name
jited = " JITed" if owner.jited.value_() else ""
return f"{type_:32}{jited}"

def get_owner(self):
try:
owner = self.map.member_("owner")
return self.inspect_owner(owner)
except LookupError:
return ""

def __repr__(self):
id_ = self.map.id.value_()
type_ = BpfMapType(self.map.map_type).name
name = self.map.name.string_().decode()

return f"{id_:>6}: {type_:32} {name:32}"


class BpfProg(object):
def __init__(self, bpf_prog):
self.prog = bpf_prog

def is_subprog(self):
return self.prog.aux.func_idx.value_() != 0

@staticmethod
def __get_btf_name(btf, btf_id):
type_ = btf.types[btf_id]
Expand All @@ -83,9 +111,26 @@ def get_btf_name(self):
return self.__get_btf_name(aux.btf, aux.func_info[0].type_id)
return ""

def get_ksym_name(self):
try:
ksym = self.prog.aux.member_("ksym")
return ksym.name.string_().decode()[26:]
except LookupError:
return ""

def get_prog_name(self):
if self.is_subprog():
return self.get_ksym_name() or self.prog.aux.name.string_().decode()
return self.get_btf_name() or self.prog.aux.name.string_().decode()

def get_used_maps(self):
for i in range(0, self.prog.aux.used_map_cnt.value_()):
yield BpfMap(self.prog.aux.used_maps[i])

def get_subprogs(self):
for i in range(0, self.prog.aux.func_cnt.value_()):
yield i, BpfProg(self.prog.aux.func[i])

def get_linked_func(self):
kind = bpf_attach_type_to_tramp(self.prog.expected_attach_type)

Expand Down Expand Up @@ -141,44 +186,28 @@ def __repr__(self):
return f"{id_:>6}: {type_:32} {name:32}{tail_call_desc}"


def list_bpf_progs():
def list_bpf_progs(show_details=False):
for bpf_prog_ in bpf_prog_for_each(prog):
bpf_prog = BpfProg(bpf_prog_)
print(f"{bpf_prog}")

if not show_details:
continue

linked_progs = bpf_prog.get_tramp_progs()
if linked_progs:
for linked_prog in linked_progs:
print(f"\tlinked: {BpfProg(linked_prog)}")

for map_ in bpf_prog.get_used_maps():
print(f"\t{"used map:":9} {map_}")

def __list_bpf_progs(args):
list_bpf_progs()

for index, subprog in bpf_prog.get_subprogs():
print(f"\t{f"func[{index:>2}]:":9} {subprog}")

class BpfMap(object):
def __init__(self, bpf_map):
self.map = bpf_map

@staticmethod
def inspect_owner(owner):
type_ = BpfProgType(owner.type).name
jited = " JITed" if owner.jited.value_() else ""
return f"{type_:32}{jited}"

def get_owner(self):
try:
owner = self.map.member_("owner")
return self.inspect_owner(owner)
except LookupError:
return ""

def __repr__(self):
id_ = self.map.id.value_()
type_ = BpfMapType(self.map.map_type).name
name = self.map.name.string_().decode()

return f"{id_:>6}: {type_:32} {name:32}"
def __list_bpf_progs(args):
list_bpf_progs(args.show_details)


class BpfProgArrayMap(BpfMap):
Expand Down Expand Up @@ -384,6 +413,9 @@ def main():

prog_parser = subparsers.add_parser("prog", aliases=["p"], help="list BPF programs")
prog_parser.set_defaults(func=__list_bpf_progs)
prog_parser.add_argument(
"--show-details", action="store_true", help="show program internal details"
)

map_parser = subparsers.add_parser("map", aliases=["m"], help="list BPF maps")
map_parser.set_defaults(func=__list_bpf_maps)
Expand Down