|
| 1 | +import gdb |
| 2 | +import fmt_helpers |
| 3 | +import math |
| 4 | + |
| 5 | +DVEC_DEBUG_MAGIC = 0x5645435F444247 |
| 6 | + |
| 7 | + |
| 8 | +class DVecPrinter: |
| 9 | + def __init__(self, val): |
| 10 | + self.val = fmt_helpers.strip_ptr_and_typedefs(val) |
| 11 | + |
| 12 | + try: |
| 13 | + self.length = int(self.val["len"]) |
| 14 | + except Exception: |
| 15 | + self.length = 0 |
| 16 | + |
| 17 | + try: |
| 18 | + self.num_blocks = int(self.val["allocated_blocks"]) |
| 19 | + except Exception: |
| 20 | + self.num_blocks = 0 |
| 21 | + |
| 22 | + try: |
| 23 | + self.blocks = self.val["blocks"] |
| 24 | + except Exception: |
| 25 | + self.blocks = None |
| 26 | + |
| 27 | + def to_string(self): |
| 28 | + return f"dvec[{self.length}] ({self.num_blocks} blocks)" |
| 29 | + |
| 30 | + def children(self): |
| 31 | + if not self.blocks: |
| 32 | + return |
| 33 | + |
| 34 | + try: |
| 35 | + block_type = self.blocks.type |
| 36 | + |
| 37 | + if self.length < 0: |
| 38 | + return |
| 39 | + for idx in range(self.length): |
| 40 | + block_idx = math.floor(math.log2(idx + 1)) |
| 41 | + idx_in_block = idx - (2**block_idx - 1) |
| 42 | + |
| 43 | + if block_idx >= self.num_blocks or idx < 0: |
| 44 | + yield (f"[{idx}]", "shit") |
| 45 | + else: |
| 46 | + try: |
| 47 | + yield (f"[{idx}]", self.blocks[block_idx][idx_in_block]) |
| 48 | + except Exception: |
| 49 | + yield (f"[{idx}]", "<error reading element>") |
| 50 | + except Exception: |
| 51 | + return |
| 52 | + |
| 53 | + def display_hint(self): |
| 54 | + return "array" |
| 55 | + |
| 56 | + |
| 57 | +def dvec_printer_lookup(val): |
| 58 | + try: |
| 59 | + candidate = fmt_helpers.strip_ptr_and_typedefs(val) |
| 60 | + magic_field = candidate["dbg_magic"] |
| 61 | + magic = int(magic_field) |
| 62 | + except Exception: |
| 63 | + return None |
| 64 | + |
| 65 | + if magic == DVEC_DEBUG_MAGIC: |
| 66 | + return DVecPrinter(val) |
| 67 | + |
| 68 | + return None |
| 69 | + |
| 70 | + |
| 71 | +def register_dvec_printers(objfile=None): |
| 72 | + if objfile is None: |
| 73 | + gdb.pretty_printers.append(dvec_printer_lookup) |
| 74 | + else: |
| 75 | + gdb.printing.register_pretty_printer_for_object_file(objfile, dvec_printer_lookup) |
| 76 | + |
| 77 | + |
| 78 | +register_dvec_printers() |
0 commit comments