gdb/python/py-arch.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Python interface to architecture

  2.    Copyright (C) 2013-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "gdbarch.h"
  16. #include "arch-utils.h"
  17. #include "disasm.h"
  18. #include "python-internal.h"

  19. typedef struct arch_object_type_object {
  20.   PyObject_HEAD
  21.   struct gdbarch *gdbarch;
  22. } arch_object;

  23. static struct gdbarch_data *arch_object_data = NULL;

  24. /* Require a valid Architecture.  */
  25. #define ARCHPY_REQUIRE_VALID(arch_obj, arch)                        \
  26.   do {                                                                \
  27.     arch = arch_object_to_gdbarch (arch_obj);                        \
  28.     if (arch == NULL)                                                \
  29.       {                                                                \
  30.         PyErr_SetString (PyExc_RuntimeError,                        \
  31.                          _("Architecture is invalid."));        \
  32.         return NULL;                                                \
  33.       }                                                                \
  34.   } while (0)

  35. static PyTypeObject arch_object_type
  36.     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("arch_object");

  37. /* Associates an arch_object with GDBARCH as gdbarch_data via the gdbarch
  38.    post init registration mechanism (gdbarch_data_register_post_init).  */

  39. static void *
  40. arch_object_data_init (struct gdbarch *gdbarch)
  41. {
  42.   arch_object *arch_obj = PyObject_New (arch_object, &arch_object_type);

  43.   if (arch_obj == NULL)
  44.     return NULL;

  45.   arch_obj->gdbarch = gdbarch;

  46.   return (void *) arch_obj;
  47. }

  48. /* Returns the struct gdbarch value corresponding to the given Python
  49.    architecture object OBJ.  */

  50. struct gdbarch *
  51. arch_object_to_gdbarch (PyObject *obj)
  52. {
  53.   arch_object *py_arch = (arch_object *) obj;

  54.   return py_arch->gdbarch;
  55. }

  56. /* Returns the Python architecture object corresponding to GDBARCH.
  57.    Returns a new reference to the arch_object associated as data with
  58.    GDBARCH.  */

  59. PyObject *
  60. gdbarch_to_arch_object (struct gdbarch *gdbarch)
  61. {
  62.   PyObject *new_ref = (PyObject *) gdbarch_data (gdbarch, arch_object_data);

  63.   /* new_ref could be NULL if registration of arch_object with GDBARCH failed
  64.      in arch_object_data_init.  */
  65.   Py_XINCREF (new_ref);

  66.   return new_ref;
  67. }

  68. /* Implementation of gdb.Architecture.name (self) -> String.
  69.    Returns the name of the architecture as a string value.  */

  70. static PyObject *
  71. archpy_name (PyObject *self, PyObject *args)
  72. {
  73.   struct gdbarch *gdbarch = NULL;
  74.   const char *name;
  75.   PyObject *py_name;

  76.   ARCHPY_REQUIRE_VALID (self, gdbarch);

  77.   name = (gdbarch_bfd_arch_info (gdbarch))->printable_name;
  78.   py_name = PyString_FromString (name);

  79.   return py_name;
  80. }

  81. /* Implementation of
  82.    gdb.Architecture.disassemble (self, start_pc [, end_pc [,count]]) -> List.
  83.    Returns a list of instructions in a memory address range.  Each instruction
  84.    in the list is a Python dict object.
  85. */

  86. static PyObject *
  87. archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw)
  88. {
  89.   static char *keywords[] = { "start_pc", "end_pc", "count", NULL };
  90.   CORE_ADDR start, end = 0;
  91.   CORE_ADDR pc;
  92.   gdb_py_ulongest start_temp;
  93.   long count = 0, i;
  94.   PyObject *result_list, *end_obj = NULL, *count_obj = NULL;
  95.   struct gdbarch *gdbarch = NULL;

  96.   ARCHPY_REQUIRE_VALID (self, gdbarch);

  97.   if (!PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG "|OO", keywords,
  98.                                     &start_temp, &end_obj, &count_obj))
  99.     return NULL;

  100.   start = start_temp;
  101.   if (end_obj)
  102.     {
  103.       /* Make a long logic check first.  In Python 3.x, internally,
  104.          all integers are represented as longs.  In Python 2.x, there
  105.          is still a differentiation internally between a PyInt and a
  106.          PyLong.  Explicitly do this long check conversion first. In
  107.          GDB, for Python 3.x, we #ifdef PyInt = PyLong.  This check has
  108.          to be done first to ensure we do not lose information in the
  109.          conversion process.  */
  110.       if (PyLong_Check (end_obj))
  111.         end = PyLong_AsUnsignedLongLong (end_obj);
  112.       else if (PyInt_Check (end_obj))
  113.         /* If the end_pc value is specified without a trailing 'L', end_obj will
  114.            be an integer and not a long integer.  */
  115.         end = PyInt_AsLong (end_obj);
  116.       else
  117.         {
  118.           Py_DECREF (end_obj);
  119.           Py_XDECREF (count_obj);
  120.           PyErr_SetString (PyExc_TypeError,
  121.                            _("Argument 'end_pc' should be a (long) integer."));

  122.           return NULL;
  123.         }

  124.       if (end < start)
  125.         {
  126.           Py_DECREF (end_obj);
  127.           Py_XDECREF (count_obj);
  128.           PyErr_SetString (PyExc_ValueError,
  129.                            _("Argument 'end_pc' should be greater than or "
  130.                              "equal to the argument 'start_pc'."));

  131.           return NULL;
  132.         }
  133.     }
  134.   if (count_obj)
  135.     {
  136.       count = PyInt_AsLong (count_obj);
  137.       if (PyErr_Occurred () || count < 0)
  138.         {
  139.           Py_DECREF (count_obj);
  140.           Py_XDECREF (end_obj);
  141.           PyErr_SetString (PyExc_TypeError,
  142.                            _("Argument 'count' should be an non-negative "
  143.                              "integer."));

  144.           return NULL;
  145.         }
  146.     }

  147.   result_list = PyList_New (0);
  148.   if (result_list == NULL)
  149.     return NULL;

  150.   for (pc = start, i = 0;
  151.        /* All args are specified.  */
  152.        (end_obj && count_obj && pc <= end && i < count)
  153.        /* end_pc is specified, but no count.  */
  154.        || (end_obj && count_obj == NULL && pc <= end)
  155.        /* end_pc is not specified, but a count is.  */
  156.        || (end_obj == NULL && count_obj && i < count)
  157.        /* Both end_pc and count are not specified.  */
  158.        || (end_obj == NULL && count_obj == NULL && pc == start);)
  159.     {
  160.       int insn_len = 0;
  161.       char *as = NULL;
  162.       struct ui_file *memfile = mem_fileopen ();
  163.       PyObject *insn_dict = PyDict_New ();
  164.       volatile struct gdb_exception except;

  165.       if (insn_dict == NULL)
  166.         {
  167.           Py_DECREF (result_list);
  168.           ui_file_delete (memfile);

  169.           return NULL;
  170.         }
  171.       if (PyList_Append (result_list, insn_dict))
  172.         {
  173.           Py_DECREF (result_list);
  174.           Py_DECREF (insn_dict);
  175.           ui_file_delete (memfile);

  176.           return NULL/* PyList_Append Sets the exception.  */
  177.         }

  178.       TRY_CATCH (except, RETURN_MASK_ALL)
  179.         {
  180.           insn_len = gdb_print_insn (gdbarch, pc, memfile, NULL);
  181.         }
  182.       if (except.reason < 0)
  183.         {
  184.           Py_DECREF (result_list);
  185.           ui_file_delete (memfile);

  186.           gdbpy_convert_exception (except);
  187.           return NULL;
  188.         }

  189.       as = ui_file_xstrdup (memfile, NULL);
  190.       if (PyDict_SetItemString (insn_dict, "addr",
  191.                                 gdb_py_long_from_ulongest (pc))
  192.           || PyDict_SetItemString (insn_dict, "asm",
  193.                                    PyString_FromString (*as ? as : "<unknown>"))
  194.           || PyDict_SetItemString (insn_dict, "length",
  195.                                    PyInt_FromLong (insn_len)))
  196.         {
  197.           Py_DECREF (result_list);

  198.           ui_file_delete (memfile);
  199.           xfree (as);

  200.           return NULL;
  201.         }

  202.       pc += insn_len;
  203.       i++;
  204.       ui_file_delete (memfile);
  205.       xfree (as);
  206.     }

  207.   return result_list;
  208. }

  209. /* Initializes the Architecture class in the gdb module.  */

  210. int
  211. gdbpy_initialize_arch (void)
  212. {
  213.   arch_object_data = gdbarch_data_register_post_init (arch_object_data_init);
  214.   arch_object_type.tp_new = PyType_GenericNew;
  215.   if (PyType_Ready (&arch_object_type) < 0)
  216.     return -1;

  217.   return gdb_pymodule_addobject (gdb_module, "Architecture",
  218.                                  (PyObject *) &arch_object_type);
  219. }

  220. static PyMethodDef arch_object_methods [] = {
  221.   { "name", archpy_name, METH_NOARGS,
  222.     "name () -> String.\n\
  223. Return the name of the architecture as a string value." },
  224.   { "disassemble", (PyCFunction) archpy_disassemble,
  225.     METH_VARARGS | METH_KEYWORDS,
  226.     "disassemble (start_pc [, end_pc [, count]]) -> List.\n\
  227. Return a list of at most COUNT disassembled instructions from START_PC to\n\
  228. END_PC." },
  229.   {NULL/* Sentinel */
  230. };

  231. static PyTypeObject arch_object_type = {
  232.   PyVarObject_HEAD_INIT (NULL, 0)
  233.   "gdb.Architecture",                 /* tp_name */
  234.   sizeof (arch_object),               /* tp_basicsize */
  235.   0,                                  /* tp_itemsize */
  236.   0,                                  /* tp_dealloc */
  237.   0,                                  /* tp_print */
  238.   0,                                  /* tp_getattr */
  239.   0,                                  /* tp_setattr */
  240.   0,                                  /* tp_compare */
  241.   0,                                  /* tp_repr */
  242.   0,                                  /* tp_as_number */
  243.   0,                                  /* tp_as_sequence */
  244.   0,                                  /* tp_as_mapping */
  245.   0,                                  /* tp_hash  */
  246.   0,                                  /* tp_call */
  247.   0,                                  /* tp_str */
  248.   0,                                  /* tp_getattro */
  249.   0,                                  /* tp_setattro */
  250.   0,                                  /* tp_as_buffer */
  251.   Py_TPFLAGS_DEFAULT,                 /* tp_flags */
  252.   "GDB architecture object",          /* tp_doc */
  253.   0,                                  /* tp_traverse */
  254.   0,                                  /* tp_clear */
  255.   0,                                  /* tp_richcompare */
  256.   0,                                  /* tp_weaklistoffset */
  257.   0,                                  /* tp_iter */
  258.   0,                                  /* tp_iternext */
  259.   arch_object_methods,                /* tp_methods */
  260.   0,                                  /* tp_members */
  261.   0,                                  /* tp_getset */
  262.   0,                                  /* tp_base */
  263.   0,                                  /* tp_dict */
  264.   0,                                  /* tp_descr_get */
  265.   0,                                  /* tp_descr_set */
  266.   0,                                  /* tp_dictoffset */
  267.   0,                                  /* tp_init */
  268.   0,                                  /* tp_alloc */
  269. };