public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH 3/5] gdb/python: implement DisassemblerResult.__str__ method
Date: Tue,  4 Apr 2023 09:21:05 +0100	[thread overview]
Message-ID: <2e5f2835f1a8ed0ae57255cb29e505261db74826.1680596378.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1680596378.git.aburgess@redhat.com>

Add the DisassemblerResult.__str__ method.  This gives the same result
as the DisassemblerResult.string attribute, but can be useful sometime
where a user just wants to print the DisassemblerResult object and
have GDB do something sane.

There's a test for the new functionality.
---
 gdb/python/py-disasm.c                 | 26 +++++++++++++++++---------
 gdb/testsuite/gdb.python/py-disasm.exp |  1 +
 gdb/testsuite/gdb.python/py-disasm.py  | 12 ++++++++++++
 3 files changed, 30 insertions(+), 9 deletions(-)

diff --git a/gdb/python/py-disasm.c b/gdb/python/py-disasm.c
index 999d0e0192c..c61fe70c26b 100644
--- a/gdb/python/py-disasm.c
+++ b/gdb/python/py-disasm.c
@@ -605,6 +605,21 @@ gdbpy_disassembler::read_memory_func (bfd_vma memaddr, gdb_byte *buff,
   return 0;
 }
 
+/* Implement __str__ for the DisassemblerResult type.  */
+
+static PyObject *
+disasmpy_result_str (PyObject *self)
+{
+  disasm_result_object *obj = (disasm_result_object *) self;
+
+  gdb_assert (obj->content != nullptr);
+  gdb_assert (obj->content->size () > 0);
+  gdb_assert (obj->length > 0);
+  return PyUnicode_Decode (obj->content->c_str (),
+			   obj->content->size (),
+			   host_charset (), nullptr);
+}
+
 /* Implement DisassemblerResult.length attribute, return the length of the
    disassembled instruction.  */
 
@@ -621,14 +636,7 @@ disasmpy_result_length (PyObject *self, void *closure)
 static PyObject *
 disasmpy_result_string (PyObject *self, void *closure)
 {
-  disasm_result_object *obj = (disasm_result_object *) self;
-
-  gdb_assert (obj->content != nullptr);
-  gdb_assert (obj->content->size () > 0);
-  gdb_assert (obj->length > 0);
-  return PyUnicode_Decode (obj->content->c_str (),
-			   obj->content->size (),
-			   host_charset (), nullptr);
+  return disasmpy_result_str (self);
 }
 
 /* Implement DisassemblerResult.__init__.  Takes two arguments, an
@@ -1140,7 +1148,7 @@ PyTypeObject disasm_result_object_type = {
   0,						/*tp_as_mapping*/
   0,						/*tp_hash */
   0,						/*tp_call*/
-  0,						/*tp_str*/
+  disasmpy_result_str,				/*tp_str*/
   0,						/*tp_getattro*/
   0,						/*tp_setattro*/
   0,						/*tp_as_buffer*/
diff --git a/gdb/testsuite/gdb.python/py-disasm.exp b/gdb/testsuite/gdb.python/py-disasm.exp
index 44b8398bf21..66ed39f048b 100644
--- a/gdb/testsuite/gdb.python/py-disasm.exp
+++ b/gdb/testsuite/gdb.python/py-disasm.exp
@@ -76,6 +76,7 @@ set test_plans \
 	 [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 "ShowResultStr" "${base_pattern}\\s+## ${nop}\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 977cdbf3c37..435a3bf5339 100644
--- a/gdb/testsuite/gdb.python/py-disasm.py
+++ b/gdb/testsuite/gdb.python/py-disasm.py
@@ -113,6 +113,18 @@ class ShowResultRepr(TestDisassembler):
         return DisassemblerResult(length=length, string=string)
 
 
+class ShowResultStr(TestDisassembler):
+    """Call the __str__ method on a DisassemblerResult object, incude the
+    resulting string in a comment within the disassembler output."""
+
+    def disassemble(self, info):
+        result = gdb.disassembler.builtin_disassemble(info)
+        comment = "\t## " + str(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


  parent reply	other threads:[~2023-04-04  8:21 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
2023-04-04  8:21 ` Andrew Burgess [this message]
2023-05-12 17:43   ` [PATCH 3/5] gdb/python: implement DisassemblerResult.__str__ method 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=2e5f2835f1a8ed0ae57255cb29e505261db74826.1680596378.git.aburgess@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).