public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Subject: Re: [PATCH 2/5] gdb/python: implement __repr__ methods for py-disasm.c types
Date: Fri, 12 May 2023 18:43:19 +0100	[thread overview]
Message-ID: <87ilcx5tu0.fsf@redhat.com> (raw)
In-Reply-To: <3c73ee1a5e45e740be722fe731d28bcb4a485355.1680596378.git.aburgess@redhat.com>

Andrew Burgess <aburgess@redhat.com> 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+## <gdb.disassembler.DisassembleInfo address=$hex>\r\n.*"] \
> +	 [list "ShowInfoSubClassRepr" "${base_pattern}\\s+## <MyInfo address=$hex>\r\n.*"] \
> +	 [list "ShowResultRepr" "${base_pattern}\\s+## <gdb.disassembler.DisassemblerResult length=$decimal string=\"\[^\r\n\]+\">\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


  reply	other threads:[~2023-05-12 17:43 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-04  8:21 [PATCH 0/5] Disassembler Styling And The Python API Andrew Burgess
2023-04-04  8:21 ` [PATCH 1/5] gdb/doc: improve Python Disassembler API documentation Andrew Burgess
2023-04-04 11:36   ` Eli Zaretskii
2023-04-28 22:49     ` Andrew Burgess
2023-05-12 17:42       ` Andrew Burgess
2023-04-04  8:21 ` [PATCH 2/5] gdb/python: implement __repr__ methods for py-disasm.c types Andrew Burgess
2023-05-12 17:43   ` Andrew Burgess [this message]
2023-04-04  8:21 ` [PATCH 3/5] gdb/python: implement DisassemblerResult.__str__ method Andrew Burgess
2023-05-12 17:43   ` Andrew Burgess
2023-04-04  8:21 ` [PATCH 4/5] gdb/python: rework how the disassembler API reads the result object Andrew Burgess
2023-04-04 11:38   ` Eli Zaretskii
2023-04-04  8:21 ` [PATCH 5/5] gdb/python: extend the Python Disassembler API to allow for styling Andrew Burgess
2023-04-04 12:01   ` Eli Zaretskii
2023-04-28 23:11     ` Andrew Burgess
2023-04-17 16:25   ` Tom Tromey
2023-04-28 23:09     ` [PATCHv2 " Andrew Burgess

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ilcx5tu0.fsf@redhat.com \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).