[RFC - Python Scripting] New method gdb.Architecture.disassemble

Siva Chandra sivachandra@google.com
Wed Feb 13 14:37:00 GMT 2013


I have addressed all of Tom's comments and added docs and tests in the
attached patch. I did not add a NEWS entry as an entry already exists
for the new gdb.Architecture class.

2013-02-13  Siva Chandra Reddy  <sivachandra@google.com>

        Add a new method 'disassemble' to gdb.Architecture class.
        * python/py-arch.c (archpy_disassmble): Implementation of the
        new method gdb.Architecture.disassemble.
        (arch_object_methods): Add entry for the new method.

doc/

        * gdb.texinfo (Architectures In Python): Add description about
        the new method gdb.Architecture.disassemble.

testsuite/

        * gdb.python/py-arch.c: New test case
        * gdb.python/py-arch.exp: New tests to test
        gdb.Architecture.disassemble
        * gdb.python/Makefile.in: Add py-arch to the list of
        EXECUTABLES.

I have a couple of questions for my knowledge:
1. docs.python.org says that PyList_Append sets an exception when it
fails. It does not say so for PyList_New() and PyDict_New(). However,
Tom commented that even these functions set exception on failure. In
general, how do we know if a certain Python C API function sets an
exception or not?
2. One of Tom's suggestion was to use something like this:

     asm_code == NULL ? unknown_str : asm_code

I have modified my code accordingly, but the concern I have is that
asm_code is of type 'char *', while unknown_str if of type 'const char
*'. This means that the type of result of the above expression is
dynamic! Though the code compiles with GCC, it that standard?

Thanks,
Siva Chandra
-------------- next part --------------
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index e3f336e..b18c409 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -26040,6 +26040,36 @@ A @code{gdb.Architecture} class has the following methods:
 Return the name (string value) of the architecture.
 @end defun
 
+@defun Architecture.disassemble (@var{low}, @var{high})
+Return a list of disassembled instructions in the memory address range
+@var{low} to @var{high}.  Each element of the list is a Python
+@code{dict} with the following string keys:
+
+@table @asis
+
+@item @code{`addr'}
+The value corresponding to this key is a Python long integer capturing
+the memory address of the instruction.
+
+@item @code{`asm'}
+The value corresponding to this key is a string value capturing the
+assembly language code of the instruction.
+
+@item @code{`func'}
+The value corresponding to this key is the name of the function (string
+value) the instruction belongs to.
+
+@item @code{`length'}
+The value correspoding to this key is the length (integer value) of the
+instruction in bytes.
+
+@item @code{`offset'}
+The value corresponding to this key is the byte offset (integer value)
+of the instruction within the function it belongs to.
+
+@end table
+@end defun
+
 @node Python Auto-loading
 @subsection Python Auto-loading
 @cindex Python auto-loading
diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c
index edd508f..1837bf3 100644
--- a/gdb/python/py-arch.c
+++ b/gdb/python/py-arch.c
@@ -20,6 +20,7 @@
 #include "defs.h"
 #include "gdbarch.h"
 #include "arch-utils.h"
+#include "disasm.h"
 #include "python-internal.h"
 
 typedef struct arch_object_type_object {
@@ -86,6 +87,104 @@ archpy_name (PyObject *self, PyObject *args)
   return py_name;
 }
 
+/* Implementation of gdb.Architecture.disassemble (self, low, high) -> List.
+   Returns a list of instructions in a memory address range.  Each instruction
+   in the list is a Python dict object.
+*/
+
+static PyObject *
+archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw)
+{
+  static char *keywords[] = { "low", "high", NULL };
+  CORE_ADDR low, high;
+  CORE_ADDR pc;
+  PyObject *result_list;
+  struct gdbarch *gdbarch = arch_object_to_gdbarch (self);
+
+  if (!PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG GDB_PY_LLU_ARG,
+                                    keywords, &low, &high))
+    return NULL;
+
+  result_list = PyList_New (0);
+  if (!result_list)
+    return NULL;
+
+  for (pc = low; pc <= high;)
+    {
+      int line = -1, unmapped, offset = -1, insn_len = 0;
+      char *filename = NULL, *fn = NULL, *as = NULL;
+      struct ui_file *memfile = mem_fileopen ();
+      PyObject *insn_dict = PyDict_New ();
+      volatile struct gdb_exception except;
+
+      if (!insn_dict)
+        {
+          Py_DECREF (result_list);
+          ui_file_delete (memfile);
+
+          return NULL;
+        }
+      if (PyList_Append (result_list, insn_dict))
+        {
+          Py_DECREF (result_list);
+          Py_DECREF (insn_dict);
+          ui_file_delete (memfile);
+
+          return NULL;  /* PyList_Append Sets the exception.  */
+        }
+
+      TRY_CATCH (except, RETURN_MASK_ALL)
+        {
+          insn_len = gdb_print_insn (gdbarch, pc, memfile, NULL);
+          /* Even though filename, line and unmapped are passed as arguments,
+             they do not give us any meaningful values currently.  */
+          build_address_symbolic (gdbarch, pc, 0, &fn, &offset, &filename,
+                                  &line, &unmapped);
+        }
+      if (except.reason < 0)
+        {
+          Py_DECREF (result_list);
+          ui_file_delete (memfile);
+          xfree (fn);
+          xfree (filename);
+
+          return gdbpy_convert_exception (except);
+        }
+
+      as = ui_file_xstrdup (memfile, NULL);
+      if (PyDict_SetItemString (insn_dict, "addr",
+                                gdb_py_long_from_ulongest (pc))
+          || PyDict_SetItemString (insn_dict, "asm",
+                                   PyString_FromString (as ? as : "<unknown>"))
+          || PyDict_SetItemString (insn_dict, "func",
+                                   PyString_FromString (fn ? fn : "<unknown>"))
+          || PyDict_SetItemString (insn_dict, "length",
+                                   PyInt_FromLong (insn_len))
+          || PyDict_SetItemString (insn_dict, "offset",
+                                   PyInt_FromLong (offset)))
+        {
+          Py_DECREF (result_list);
+          PyErr_SetString (PyExc_MemoryError,
+                           _("Unable to add fields to instruction dict."));
+
+          ui_file_delete (memfile);
+          xfree (as);
+          xfree (fn);
+          xfree (filename);
+
+          return NULL;
+        }
+
+      pc += insn_len;
+      ui_file_delete (memfile);
+      xfree (as);
+      xfree (fn);
+      xfree (filename);
+    }
+
+  return result_list;
+}
+
 /* Initializes the Architecture class in the gdb module.  */
 
 void
@@ -105,6 +204,10 @@ static PyMethodDef arch_object_methods [] = {
   { "name", archpy_name, METH_NOARGS,
     "name () -> String.\n\
 Return the name of the architecture as a string value." },
+  { "disassemble", (PyCFunction) archpy_disassemble,
+    METH_VARARGS | METH_KEYWORDS,
+    "disassemble (low, high) -> List.\n\
+Return the list of disassembled instructions from LOW to HIGH." },
   {NULL}  /* Sentinel */
 };
 
diff --git a/gdb/testsuite/gdb.python/Makefile.in b/gdb/testsuite/gdb.python/Makefile.in
index 4e286b5..0b81507 100644
--- a/gdb/testsuite/gdb.python/Makefile.in
+++ b/gdb/testsuite/gdb.python/Makefile.in
@@ -6,7 +6,7 @@ EXECUTABLES = py-type py-value py-prettyprint py-template py-block \
 	py-shared python lib-types py-events py-evthreads py-frame \
 	py-mi py-pp-maint py-progspace py-section-script py-objfile \
 	py-finish-breakpoint py-finish-breakpoint2 py-value-cc py-explore \
-	py-explore-cc
+	py-explore-cc py-arch
 
 MISCELLANEOUS = py-shared-sl.sl py-events-shlib.so py-events-shlib-nodebug.so 
 
diff --git a/gdb/testsuite/gdb.python/py-arch.c b/gdb/testsuite/gdb.python/py-arch.c
new file mode 100644
index 0000000..e2fe55c
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-arch.c
@@ -0,0 +1,23 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2013 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see  <http://www.gnu.org/licenses/>.
+*/
+
+int
+main (void)
+{
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.python/py-arch.exp b/gdb/testsuite/gdb.python/py-arch.exp
new file mode 100644
index 0000000..5104619
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-arch.exp
@@ -0,0 +1,45 @@
+# Copyright 2013 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+standard_testfile
+
+if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } {
+    return -1
+}
+
+# Skip all tests if Python scripting is not enabled.
+if { [skip_python_tests] } { continue }
+
+if ![runto_main] {
+   return -1
+}
+
+gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "get frame" 0
+gdb_py_test_silent_cmd "python arch = frame.architecture()" "get arch" 0
+gdb_py_test_silent_cmd "python pc = frame.pc()" "get pc" 0
+gdb_py_test_silent_cmd "python insn_list = arch.disassemble(pc, pc)" "disassemble" 0
+
+gdb_test "python print len(insn_list)" "1" "test number of instructions"
+
+gdb_py_test_silent_cmd "python insn = insn_list\[0\]" "get instruction" 0
+
+gdb_test "python print \"addr\" in insn" "True" "test key addr"
+gdb_test "python print \"asm\" in insn" "True" "test key asm"
+gdb_test "python print \"func\" in insn" "True" "test key func"
+gdb_test "python print \"length\" in insn" "True" "test key length"
+gdb_test "python print \"offset\" in insn" "True" "test key offset"
+
+# Negative test
+gdb_test "python arch.disassemble(0, 0)" ".*gdb\.MemoryError.*" "test exception"


More information about the Gdb-patches mailing list