summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaria Matejka <mq@jmq.cz>2021-11-23 18:04:16 +0000
committerMaria Matejka <mq@jmq.cz>2021-11-24 15:48:13 +0000
commit57d0ecb9b739afec9a050178ed664ff5ec9cf42e (patch)
tree58b81e72f704f02488e9fe810222aeb75d4dbee5
parent5395880908fdb264b5b78ccc455dced49eb29915 (diff)
GDB pretty printer for resource pools
-rw-r--r--bird-gdb.py211
1 files changed, 209 insertions, 2 deletions
diff --git a/bird-gdb.py b/bird-gdb.py
index c7351d6b..fe7827fe 100644
--- a/bird-gdb.py
+++ b/bird-gdb.py
@@ -4,9 +4,10 @@ class BIRDPrinter:
@classmethod
def lookup(cls, val):
- if val.type.code != cls.typeCode:
+ t = val.type.strip_typedefs()
+ if t.code != cls.typeCode:
return None
- if val.type.tag != cls.typeTag:
+ if t.tag != cls.typeTag:
return None
return cls(val)
@@ -252,6 +253,211 @@ class BIRDListItem(gdb.Function):
BIRDListItem()
+class BIRDResourceSize():
+ def __init__(self, netto, overhead, free):
+ self.netto = netto
+ self.overhead = overhead
+ self.free = free
+
+ def __str__(self):
+ ns = str(self.netto)
+ os = str(self.overhead)
+ fs = str(self.free)
+
+ return "{: >12s} | {: >12s} | {: >12s}".format(ns, os, fs)
+
+ def __add__(self, val):
+ return BIRDResourceSize(self.netto + val.netto, self.overhead + val.overhead, self.free + val.free)
+
+class BIRDResource():
+ def __init__(self, val):
+ self.val = val
+
+ def __str__(self):
+ return f"Item {self.val.address} of class \"{self.val['class']['name'].string()}\""
+
+ def memsize(self):
+ if str(self.val["class"]["memsize"]) == '0x0':
+ size = self.val["class"]["size"]
+ ressize = gdb.lookup_type("struct resource").sizeof
+ return BIRDResourceSize(size - ressize, ressize, 0)
+ else:
+ raise Exception(f"Resource class {self.val['class']['name']} with defined memsize() not known by Python")
+
+ def parse(self):
+ pass
+
+class BIRDMBResource(BIRDResource):
+ def __init__(self, val):
+ self.mbtype = gdb.lookup_type("struct mblock")
+ self.val = val.cast(self.mbtype)
+
+ def memsize(self):
+ return BIRDResourceSize(self.val["size"], 8 + self.mbtype.sizeof, 0)
+
+ def __str__(self):
+ return f"Standalone memory block {self.val.address} of size {self.val['size']}, data at {self.val['data'].address}"
+
+class BIRDLinPoolResource(BIRDResource):
+ def __init__(self, val):
+ self.lptype = gdb.lookup_type("struct linpool")
+ self.val = val.cast(self.lptype)
+ self.info = None
+
+ def count_chunk(self, which):
+ cnt = 0
+ chunk = self.val[which]
+ while str(chunk) != '0x0':
+ cnt += 1
+ chunk = chunk.dereference()["next"]
+ return cnt
+
+ def parse(self):
+ self.info = {
+ "std_chunks": self.count_chunk("first"),
+ "large_chunks": self.count_chunk("first_large"),
+ }
+
+ def memsize(self):
+ if self.info is None:
+ self.parse()
+
+ overhead = (8 - 8*self.val["use_pages"]) + gdb.lookup_type("struct lp_chunk").sizeof
+ return BIRDResourceSize(
+ self.val["total"] + self.val["total_large"],
+ (self.info["std_chunks"] + self.info["large_chunks"]) * overhead,
+ 0)
+
+ def __str__(self):
+ if self.info is None:
+ self.parse()
+
+ return f"Linpool {self.val.address} with {self.info['std_chunks']} standard chunks of size {self.val['chunk_size']} and {self.info['large_chunks']} large chunks"
+
+class BIRDSlabResource(BIRDResource):
+ def __init__(self, val):
+ self.slabtype = gdb.lookup_type("struct slab")
+ self.val = val.cast(self.slabtype)
+ self.info = None
+
+ def count_heads_item(self, item):
+ self.hcnt += 1
+ self.used += item.dereference().cast(self.slheadtype)["num_full"]
+
+ def count_heads(self, which):
+ self.hcnt = 0
+ self.used = 0
+ BIRDList(self.val[which + "_heads"]).walk(self.count_heads_item)
+ self.info[which + "_heads"] = self.hcnt
+ self.info[which + "_used"] = self.used
+ return (self.hcnt, self.used)
+
+ def parse(self):
+ self.page_size = gdb.lookup_symbol("page_size")[0].value()
+ self.slheadtype = gdb.lookup_type("struct sl_head")
+ self.info = {}
+ self.count_heads("empty")
+ self.count_heads("partial")
+ self.count_heads("full")
+
+ def memsize(self):
+ if self.info is None:
+ self.parse()
+
+ total_used = self.info["empty_used"] + self.info["partial_used"] + self.info["full_used"]
+ total_heads = self.info["empty_heads"] + self.info["partial_heads"] + self.info["full_heads"]
+
+ eff_size = total_used * self.val["obj_size"]
+ free_size = self.info["empty_heads"] * self.page_size
+ total_size = total_heads * self.page_size + self.slabtype.sizeof
+
+ return BIRDResourceSize( eff_size, total_size - free_size - eff_size, free_size)
+
+ def __str__(self):
+ if self.info is None:
+ self.parse()
+
+ return f"Slab {self.val.address} " + ", ".join([
+ f"{self.info[x + '_heads']} {x} heads" for x in [ "empty", "partial", "full" ]]) + \
+ f", {self.val['objs_per_slab']} objects of size {self.val['obj_size']} per head"
+
+
+class BIRDPoolResource(BIRDResource):
+ def __init__(self, val):
+ self.pooltype = gdb.lookup_type("struct pool")
+ self.resptrtype = gdb.lookup_type("struct resource").pointer()
+ self.page_size = gdb.lookup_symbol("page_size")[0].value()
+ self.val = val.cast(self.pooltype)
+ self.items = None
+
+ def parse_inside(self, val):
+ self.items.append(BIRDNewResource(val.cast(self.resptrtype).dereference()))
+
+ def parse(self):
+ self.items = []
+ BIRDList(self.val["inside"]).walk(self.parse_inside)
+
+ def free_pages(self):
+ if str(self.val['pages']) == '0x0':
+ return 0
+ else:
+ return self.val['pages'].dereference()['free']
+
+ def memsize(self):
+ if self.items is None:
+ self.parse()
+
+ sum = BIRDResourceSize(0, self.pooltype.sizeof, self.free_pages() * self.page_size)
+# for i in self.items:
+# sum += i.memsize()
+
+ return sum
+
+ def __str__(self):
+ if self.items is None:
+ self.parse()
+
+# for i in self.items:
+# print(i)
+
+ return f"Resource pool {self.val.address} \"{self.val['name'].string()}\" containing {len(self.items)} items and {self.free_pages()} free pages"
+
+BIRDResourceMap = {
+ "mbl_memsize": BIRDMBResource,
+ "pool_memsize": BIRDPoolResource,
+ "lp_memsize": BIRDLinPoolResource,
+ "slab_memsize": BIRDSlabResource,
+ }
+
+def BIRDNewResource(res):
+ cms = res["class"].dereference()["memsize"]
+ for cx in BIRDResourceMap:
+ if cms == gdb.lookup_symbol(cx)[0].value():
+ return BIRDResourceMap[cx](res)
+
+ return BIRDResource(res)
+
+
+class BIRDResourcePrinter(BIRDPrinter):
+ "Print BIRD's resource"
+ typeCode = gdb.TYPE_CODE_STRUCT
+ typeTag = "resource"
+
+ def __init__(self, val):
+ super(BIRDResourcePrinter, self).__init__(val)
+ self.resource = BIRDNewResource(val)
+ self.resource.parse()
+ self.resourcetype = gdb.lookup_type("struct resource")
+
+ if type(self.resource) == BIRDPoolResource:
+ self.children = self.pool_children
+
+ def pool_children(self):
+ return iter([ ("\n", i.val.cast(self.resourcetype)) for i in self.resource.items ])
+
+ def to_string(self):
+ return f"[ {str(self.resource.memsize())} ] {str(self.resource)}"
+
def register_printers(objfile):
objfile.pretty_printers.append(BIRDFInstPrinter.lookup)
@@ -260,6 +466,7 @@ def register_printers(objfile):
objfile.pretty_printers.append(BIRDFLineItemPrinter.lookup)
objfile.pretty_printers.append(BIRDFLinePrinter.lookup)
objfile.pretty_printers.append(BIRDFExecStackPrinter.lookup)
+ objfile.pretty_printers.append(BIRDResourcePrinter.lookup)
register_printers(gdb.current_objfile())