gdb/python: implement DisassemblerResult.__str__ method
authorAndrew Burgess <aburgess@redhat.com>
Tue, 24 Jan 2023 15:12:34 +0000 (15:12 +0000)
committerAndrew Burgess <aburgess@redhat.com>
Fri, 12 May 2023 17:24:24 +0000 (18:24 +0100)
Add the DisassemblerResult.__str__ method.  This gives the same result
as the DisassemblerResult.string attribute, but can be useful
sometimes depending on how the user is trying to print the object.

There's a test for the new functionality.

gdb/python/py-disasm.c
gdb/testsuite/gdb.python/py-disasm.exp
gdb/testsuite/gdb.python/py-disasm.py

index eeb16d8650cbd0663c4b4cb21bf64af1ed6f925e..9c281f6e089cd312e10a9d03dd590f2168c62ae3 100644 (file)
@@ -608,6 +608,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.  */
 
@@ -624,14 +639,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
@@ -1147,7 +1155,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*/
index 854cdbbce1e5dafcdf47abc367ea923da15538a5..2550e60111e092f9d0277bd04ed03db65a742b6d 100644 (file)
@@ -76,6 +76,7 @@ set test_plans \
         [list "ShowInfoRepr" "${base_pattern}\\s+## <gdb.disassembler.DisassembleInfo address=$hex architecture=\[^>\]+>\r\n.*"] \
         [list "ShowInfoSubClassRepr" "${base_pattern}\\s+## <MyInfo address=$hex architecture=\[^>\]+>\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.*"] \
index 977cdbf3c37f7c613159208c9ee22b5471fcf85a..435a3bf53391f1ff1b2eb3bc9c49255f5e1b7243 100644 (file)
@@ -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."""