From 15ccb5e393f7c4032a8ee005a8183830a9c6accb Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Tue, 24 Jan 2023 15:03:25 +0000 Subject: gdb/python: implement __repr__ methods for py-disasm.c types Add a __repr__ method for the DisassembleInfo and DisassemblerResult types, and add some tests for these new methods. --- gdb/testsuite/gdb.python/py-disasm.py | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'gdb/testsuite/gdb.python/py-disasm.py') diff --git a/gdb/testsuite/gdb.python/py-disasm.py b/gdb/testsuite/gdb.python/py-disasm.py index 0ee883a..977cdbf 100644 --- a/gdb/testsuite/gdb.python/py-disasm.py +++ b/gdb/testsuite/gdb.python/py-disasm.py @@ -64,6 +64,55 @@ class TestDisassembler(Disassembler): raise NotImplementedError("override the disassemble method") +class ShowInfoRepr(TestDisassembler): + """Call the __repr__ method on the DisassembleInfo, convert the result + to a string, and incude it in a comment in the disassembler output.""" + + def disassemble(self, info): + comment = "\t## " + repr(info) + result = gdb.disassembler.builtin_disassemble(info) + string = result.string + comment + length = result.length + return DisassemblerResult(length=length, string=string) + + +class ShowInfoSubClassRepr(TestDisassembler): + """Create a sub-class of DisassembleInfo. Create an instace of this + sub-class and call the __repr__ method on it. Convert the result + to a string, and incude it in a comment in the disassembler + output. The DisassembleInfo sub-class does not override __repr__ + so we are calling the implementation on the parent class.""" + + class MyInfo(gdb.disassembler.DisassembleInfo): + """A wrapper around DisassembleInfo, doesn't add any new + functionality, just gives a new name in order to check the + __repr__ functionality.""" + + def __init__(self, info): + super().__init__(info) + + def disassemble(self, info): + info = self.MyInfo(info) + comment = "\t## " + repr(info) + result = gdb.disassembler.builtin_disassemble(info) + string = result.string + comment + length = result.length + return DisassemblerResult(length=length, string=string) + + +class ShowResultRepr(TestDisassembler): + """Call the __repr__ method on the DisassemblerResult, convert the + result to a string, and incude it in a comment in the disassembler + output.""" + + def disassemble(self, info): + result = gdb.disassembler.builtin_disassemble(info) + comment = "\t## " + repr(result) + string = result.string + comment + length = result.length + return DisassemblerResult(length=length, string=string) + + class GlobalPreInfoDisassembler(TestDisassembler): """Check the attributes of DisassembleInfo before disassembly has occurred.""" -- cgit v1.1