From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id aB0UFtJ6XmT8cggAWB0awg (envelope-from ) for ; Fri, 12 May 2023 13:43:46 -0400 Received: by simark.ca (Postfix, from userid 112) id 578A61E11D; Fri, 12 May 2023 13:43:46 -0400 (EDT) Authentication-Results: simark.ca; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=iqKIpjds; dkim-atps=neutral X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RDNS_DYNAMIC,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id C01381E0D6 for ; Fri, 12 May 2023 13:43:45 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E88D43855591 for ; Fri, 12 May 2023 17:43:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E88D43855591 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1683913424; bh=tpar+i8OX0TMO/ocOcL03xBgd94DKTzj6QBBLHKjkyY=; h=To:Subject:In-Reply-To:References:Date:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=iqKIpjdsqwDp5UC15zqMBr4cjGiWTVfarNvKX6Xfc8qm33ar1Ovr+rmIbcHEsqb+o 7ZlhD6OjcdrBHNPcyYx3p215MRwPlQojvn5/ZnQxgpPr1TE1kNnNP5GGeYHlGRNnp7 s/ZNAOan8/c0pJyrhKoQa5VsD1i3foxsL/qYNQUk= Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 4512B3856089 for ; Fri, 12 May 2023 17:43:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4512B3856089 Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-395-xO7k9DETN5OteULkLndTVg-1; Fri, 12 May 2023 13:43:23 -0400 X-MC-Unique: xO7k9DETN5OteULkLndTVg-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-3f4245ffb03so41143835e9.3 for ; Fri, 12 May 2023 10:43:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683913402; x=1686505402; h=mime-version:message-id:date:references:in-reply-to:subject:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tpar+i8OX0TMO/ocOcL03xBgd94DKTzj6QBBLHKjkyY=; b=cLP1kcriaXmp4F73iQEXJKzwhumpsRMwyv96IIcLX4leJwN3+IBD6N8QbMHTosEfaf /Ks+knG9rZhM0e051powaVuLJxLcO6Jqqs+hyW3Q0T5a7yDzDzxwhNpFfEIS95aw0c+s uoV2Z3ZdRFRnOTx3slxt3cgd1nrndb1xFR1G9zaS6uY+n2oDg32PkaTfb1KW+h9awfNE h3h4UTtE9Av1bXrNY9E8Qj71eBUCwhs2y803RmzAHSX3sxtWZID1JGI/JU6iXyF2KDtk x7H7RT6kHOb1JAAaFK5Ls9z+KT89cbfjRWazooIFSW8LasVVLSKgsP3AZG7LfXGIq6iJ v8pg== X-Gm-Message-State: AC+VfDw5fbLkK3krLyJyD0LpNhMlMpAT9MjLbyA1AxxvtXrfOHkI5u4b qI+EPhndAOHAUbjZ/QBjNNdl57kUoO3jdnvMojijXzT2fUyr4YwZwNc6WFEYcY2OSQhZX53Thke t2xlVHopf/LafwTANNADsdrHUhDxqi5Fabzf4mDT8ray5T5Ph9fBoml7RpjJMnsFHtg9M14MSiM 96SA2sOw== X-Received: by 2002:a05:600c:c5:b0:3f4:2610:5cd0 with SMTP id u5-20020a05600c00c500b003f426105cd0mr11979015wmm.7.1683913401884; Fri, 12 May 2023 10:43:21 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6Z7Q49g3xyrihKtViUsd5v6a4i5/oTY0ceuOiz7wchGvnx/5hu+IxlalAWx/anGLi+XktMIA== X-Received: by 2002:a05:600c:c5:b0:3f4:2610:5cd0 with SMTP id u5-20020a05600c00c500b003f426105cd0mr11978999wmm.7.1683913401472; Fri, 12 May 2023 10:43:21 -0700 (PDT) Received: from localhost (92.40.219.54.threembb.co.uk. [92.40.219.54]) by smtp.gmail.com with ESMTPSA id f1-20020a1cc901000000b003f25b40fc24sm29567873wmb.6.2023.05.12.10.43.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 May 2023 10:43:21 -0700 (PDT) To: gdb-patches@sourceware.org Subject: Re: [PATCH 2/5] gdb/python: implement __repr__ methods for py-disasm.c types In-Reply-To: <3c73ee1a5e45e740be722fe731d28bcb4a485355.1680596378.git.aburgess@redhat.com> References: <3c73ee1a5e45e740be722fe731d28bcb4a485355.1680596378.git.aburgess@redhat.com> Date: Fri, 12 May 2023 18:43:19 +0100 Message-ID: <87ilcx5tu0.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Andrew Burgess via Gdb-patches Reply-To: Andrew Burgess Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" Andrew Burgess writes: > Add a __repr__ method for the DisassembleInfo and DisassemblerResult > types, and add some tests for these new methods. I've gone ahead and pushed this patch. Thanks, Andrew > --- > gdb/python/py-disasm.c | 31 ++++++++++++++-- > gdb/testsuite/gdb.python/py-disasm.exp | 3 ++ > gdb/testsuite/gdb.python/py-disasm.py | 49 ++++++++++++++++++++++++++ > 3 files changed, 81 insertions(+), 2 deletions(-) > > diff --git a/gdb/python/py-disasm.c b/gdb/python/py-disasm.c > index 2dabec0013d..999d0e0192c 100644 > --- a/gdb/python/py-disasm.c > +++ b/gdb/python/py-disasm.c > @@ -247,6 +247,18 @@ disasm_info_dealloc (PyObject *self) > Py_TYPE (self)->tp_free (self); > } > > +/* Implement __repr__ for the DisassembleInfo type. */ > + > +static PyObject * > +disasmpy_info_repr (PyObject *self) > +{ > + disasm_info_object *obj = (disasm_info_object *) self; > + > + return PyUnicode_FromFormat ("<%s address=%s>", > + Py_TYPE (obj)->tp_name, > + core_addr_to_string_nz (obj->address)); > +} > + > /* Implement DisassembleInfo.is_valid(), really just a wrapper around the > disasm_info_object_is_valid function above. */ > > @@ -653,6 +665,21 @@ disasmpy_result_init (PyObject *self, PyObject *args, PyObject *kwargs) > return 0; > } > > +/* Implement __repr__ for the DisassemblerResult type. */ > + > +static PyObject * > +disasmpy_result_repr (PyObject *self) > +{ > + disasm_result_object *obj = (disasm_result_object *) self; > + > + gdb_assert (obj->content != nullptr); > + > + return PyUnicode_FromFormat ("<%s length=%d string=\"%s\">", > + Py_TYPE (obj)->tp_name, > + obj->length, > + obj->content->string ().c_str ()); > +} > + > /* Implement memory_error_func callback for disassemble_info. Extract the > underlying DisassembleInfo Python object, and set a memory error on > it. */ > @@ -1065,7 +1092,7 @@ PyTypeObject disasm_info_object_type = { > 0, /*tp_getattr*/ > 0, /*tp_setattr*/ > 0, /*tp_compare*/ > - 0, /*tp_repr*/ > + disasmpy_info_repr, /*tp_repr*/ > 0, /*tp_as_number*/ > 0, /*tp_as_sequence*/ > 0, /*tp_as_mapping*/ > @@ -1107,7 +1134,7 @@ PyTypeObject disasm_result_object_type = { > 0, /*tp_getattr*/ > 0, /*tp_setattr*/ > 0, /*tp_compare*/ > - 0, /*tp_repr*/ > + disasmpy_result_repr, /*tp_repr*/ > 0, /*tp_as_number*/ > 0, /*tp_as_sequence*/ > 0, /*tp_as_mapping*/ > diff --git a/gdb/testsuite/gdb.python/py-disasm.exp b/gdb/testsuite/gdb.python/py-disasm.exp > index 38a2b766320..44b8398bf21 100644 > --- a/gdb/testsuite/gdb.python/py-disasm.exp > +++ b/gdb/testsuite/gdb.python/py-disasm.exp > @@ -73,6 +73,9 @@ set test_plans \ > [list \ > [list "" "${base_pattern}\r\n.*"] \ > [list "GlobalNullDisassembler" "${base_pattern}\r\n.*"] \ > + [list "ShowInfoRepr" "${base_pattern}\\s+## \r\n.*"] \ > + [list "ShowInfoSubClassRepr" "${base_pattern}\\s+## \r\n.*"] \ > + [list "ShowResultRepr" "${base_pattern}\\s+## \r\n.*"] \ > [list "GlobalPreInfoDisassembler" "${base_pattern}\\s+## ad = $hex, ar = ${curr_arch}\r\n.*"] \ > [list "GlobalPostInfoDisassembler" "${base_pattern}\\s+## ad = $hex, ar = ${curr_arch}\r\n.*"] \ > [list "GlobalReadDisassembler" "${base_pattern}\\s+## bytes =( $hex)+\r\n.*"] \ > diff --git a/gdb/testsuite/gdb.python/py-disasm.py b/gdb/testsuite/gdb.python/py-disasm.py > index 0ee883a5e5e..977cdbf3c37 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.""" > > -- > 2.25.4