From 619bb441ec12d85d72405acb88f188256d9c7f27 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Mon, 2 May 2022 11:05:54 +0100 Subject: [PATCH] contrib/gdb.py: add a pretty-printer for ptrlen. I mostly really like the use of 'ptrlen' in place of zero-terminated strings in PuTTY, but one place it's awkward is when debuggging through string-handling code, because gdb won't automatically show me exactly what a ptrlen points to. Now it does. --- contrib/gdb.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/contrib/gdb.py b/contrib/gdb.py index 4c41edcc..fb7413ec 100644 --- a/contrib/gdb.py +++ b/contrib/gdb.py @@ -30,12 +30,29 @@ class PuTTYMpintPrettyPrinter(gdb.printing.PrettyPrinter): return "mp_int(NULL)".format(address) return "mp_int(invalid @ {:#x})".format(address) +class PuTTYPtrlenPrettyPrinter(gdb.printing.PrettyPrinter): + "Pretty-print strings in PuTTY's ptrlen type." + name = "ptrlen" + + def __init__(self, val): + super(PuTTYPtrlenPrettyPrinter, self).__init__(self.name) + self.val = val + + def to_string(self): + length = int(self.val["len"]) + char_array_ptr_type = gdb.lookup_type( + "char").const().array(length).pointer() + line = self.val["ptr"].cast(char_array_ptr_type).dereference() + return repr(bytes(int(line[i]) for i in range(length))).lstrip('b') + class PuTTYPrinterSelector(gdb.printing.PrettyPrinter): def __init__(self): super(PuTTYPrinterSelector, self).__init__("PuTTY") def __call__(self, val): if str(val.type) == "mp_int *": return PuTTYMpintPrettyPrinter(val) + if str(val.type) == "ptrlen": + return PuTTYPtrlenPrettyPrinter(val) return None gdb.printing.register_pretty_printer(None, PuTTYPrinterSelector())