gdb/python/py-progspace.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Python interface to program spaces.

  2.    Copyright (C) 2010-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 "python-internal.h"
  16. #include "charset.h"
  17. #include "progspace.h"
  18. #include "objfiles.h"
  19. #include "language.h"
  20. #include "arch-utils.h"

  21. typedef struct
  22. {
  23.   PyObject_HEAD

  24.   /* The corresponding pspace.  */
  25.   struct program_space *pspace;

  26.   /* Dictionary holding user-added attributes.
  27.      This is the __dict__ attribute of the object.  */
  28.   PyObject *dict;

  29.   /* The pretty-printer list of functions.  */
  30.   PyObject *printers;

  31.   /* The frame filter list of functions.  */
  32.   PyObject *frame_filters;
  33.   /* The type-printer list.  */
  34.   PyObject *type_printers;

  35.   /* The debug method list.  */
  36.   PyObject *xmethods;
  37. } pspace_object;

  38. static PyTypeObject pspace_object_type
  39.     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object");

  40. static const struct program_space_data *pspy_pspace_data_key;



  41. /* An Objfile method which returns the objfile's file name, or None.  */

  42. static PyObject *
  43. pspy_get_filename (PyObject *self, void *closure)
  44. {
  45.   pspace_object *obj = (pspace_object *) self;

  46.   if (obj->pspace)
  47.     {
  48.       struct objfile *objfile = obj->pspace->symfile_object_file;

  49.       if (objfile)
  50.         return PyString_Decode (objfile_name (objfile),
  51.                                 strlen (objfile_name (objfile)),
  52.                                 host_charset (), NULL);
  53.     }
  54.   Py_RETURN_NONE;
  55. }

  56. static void
  57. pspy_dealloc (PyObject *self)
  58. {
  59.   pspace_object *ps_self = (pspace_object *) self;

  60.   Py_XDECREF (ps_self->dict);
  61.   Py_XDECREF (ps_self->printers);
  62.   Py_XDECREF (ps_self->frame_filters);
  63.   Py_XDECREF (ps_self->type_printers);
  64.   Py_XDECREF (ps_self->xmethods);
  65.   Py_TYPE (self)->tp_free (self);
  66. }

  67. /* Initialize a pspace_object.
  68.    The result is a boolean indicating success.  */

  69. static int
  70. pspy_initialize (pspace_object *self)
  71. {
  72.   self->pspace = NULL;
  73.   self->dict = NULL;

  74.   self->printers = PyList_New (0);
  75.   if (self->printers == NULL)
  76.     return 0;

  77.   self->frame_filters = PyDict_New ();
  78.   if (self->frame_filters == NULL)
  79.     return 0;

  80.   self->type_printers = PyList_New (0);
  81.   if (self->type_printers == NULL)
  82.     return 0;

  83.   self->xmethods = PyList_New (0);
  84.   if (self->xmethods == NULL)
  85.     return 0;

  86.   return 1;
  87. }

  88. static PyObject *
  89. pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
  90. {
  91.   pspace_object *self = (pspace_object *) type->tp_alloc (type, 0);

  92.   if (self)
  93.     {
  94.       if (!pspy_initialize (self))
  95.         {
  96.           Py_DECREF (self);
  97.           return NULL;
  98.         }
  99.     }

  100.   return (PyObject *) self;
  101. }

  102. PyObject *
  103. pspy_get_printers (PyObject *o, void *ignore)
  104. {
  105.   pspace_object *self = (pspace_object *) o;

  106.   Py_INCREF (self->printers);
  107.   return self->printers;
  108. }

  109. static int
  110. pspy_set_printers (PyObject *o, PyObject *value, void *ignore)
  111. {
  112.   PyObject *tmp;
  113.   pspace_object *self = (pspace_object *) o;

  114.   if (! value)
  115.     {
  116.       PyErr_SetString (PyExc_TypeError,
  117.                        "cannot delete the pretty_printers attribute");
  118.       return -1;
  119.     }

  120.   if (! PyList_Check (value))
  121.     {
  122.       PyErr_SetString (PyExc_TypeError,
  123.                        "the pretty_printers attribute must be a list");
  124.       return -1;
  125.     }

  126.   /* Take care in case the LHS and RHS are related somehow.  */
  127.   tmp = self->printers;
  128.   Py_INCREF (value);
  129.   self->printers = value;
  130.   Py_XDECREF (tmp);

  131.   return 0;
  132. }

  133. /* Return the Python dictionary attribute containing frame filters for
  134.    this program space.  */
  135. PyObject *
  136. pspy_get_frame_filters (PyObject *o, void *ignore)
  137. {
  138.   pspace_object *self = (pspace_object *) o;

  139.   Py_INCREF (self->frame_filters);
  140.   return self->frame_filters;
  141. }

  142. /* Set this object file's frame filters dictionary to FILTERS.  */
  143. static int
  144. pspy_set_frame_filters (PyObject *o, PyObject *frame, void *ignore)
  145. {
  146.   PyObject *tmp;
  147.   pspace_object *self = (pspace_object *) o;

  148.   if (! frame)
  149.     {
  150.       PyErr_SetString (PyExc_TypeError,
  151.                        "cannot delete the frame filter attribute");
  152.       return -1;
  153.     }

  154.   if (! PyDict_Check (frame))
  155.     {
  156.       PyErr_SetString (PyExc_TypeError,
  157.                        "the frame filter attribute must be a dictionary");
  158.       return -1;
  159.     }

  160.   /* Take care in case the LHS and RHS are related somehow.  */
  161.   tmp = self->frame_filters;
  162.   Py_INCREF (frame);
  163.   self->frame_filters = frame;
  164.   Py_XDECREF (tmp);

  165.   return 0;
  166. }

  167. /* Get the 'type_printers' attribute.  */

  168. static PyObject *
  169. pspy_get_type_printers (PyObject *o, void *ignore)
  170. {
  171.   pspace_object *self = (pspace_object *) o;

  172.   Py_INCREF (self->type_printers);
  173.   return self->type_printers;
  174. }

  175. /* Get the 'xmethods' attribute.  */

  176. PyObject *
  177. pspy_get_xmethods (PyObject *o, void *ignore)
  178. {
  179.   pspace_object *self = (pspace_object *) o;

  180.   Py_INCREF (self->xmethods);
  181.   return self->xmethods;
  182. }

  183. /* Set the 'type_printers' attribute.  */

  184. static int
  185. pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
  186. {
  187.   PyObject *tmp;
  188.   pspace_object *self = (pspace_object *) o;

  189.   if (! value)
  190.     {
  191.       PyErr_SetString (PyExc_TypeError,
  192.                        "cannot delete the type_printers attribute");
  193.       return -1;
  194.     }

  195.   if (! PyList_Check (value))
  196.     {
  197.       PyErr_SetString (PyExc_TypeError,
  198.                        "the type_printers attribute must be a list");
  199.       return -1;
  200.     }

  201.   /* Take care in case the LHS and RHS are related somehow.  */
  202.   tmp = self->type_printers;
  203.   Py_INCREF (value);
  204.   self->type_printers = value;
  205.   Py_XDECREF (tmp);

  206.   return 0;
  207. }



  208. /* Clear the PSPACE pointer in a Pspace object and remove the reference.  */

  209. static void
  210. py_free_pspace (struct program_space *pspace, void *datum)
  211. {
  212.   struct cleanup *cleanup;
  213.   pspace_object *object = datum;
  214.   /* This is a fiction, but we're in a nasty spot: The pspace is in the
  215.      process of being deleted, we can't rely on anything in it.  Plus
  216.      this is one time when the current program space and current inferior
  217.      are not in sync: All inferiors that use PSPACE may no longer exist.
  218.      We don't need to do much here, and since "there is always an inferior"
  219.      using target_gdbarch suffices.
  220.      Note: We cannot call get_current_arch because it may try to access
  221.      the target, which may involve accessing data in the pspace currently
  222.      being deleted.  */
  223.   struct gdbarch *arch = target_gdbarch ();

  224.   cleanup = ensure_python_env (arch, current_language);
  225.   object->pspace = NULL;
  226.   Py_DECREF ((PyObject *) object);
  227.   do_cleanups (cleanup);
  228. }

  229. /* Return a borrowed reference to the Python object of type Pspace
  230.    representing PSPACE.  If the object has already been created,
  231.    return it.  Otherwise, create it.  Return NULL and set the Python
  232.    error on failure.  */

  233. PyObject *
  234. pspace_to_pspace_object (struct program_space *pspace)
  235. {
  236.   pspace_object *object;

  237.   object = program_space_data (pspace, pspy_pspace_data_key);
  238.   if (!object)
  239.     {
  240.       object = PyObject_New (pspace_object, &pspace_object_type);
  241.       if (object)
  242.         {
  243.           if (!pspy_initialize (object))
  244.             {
  245.               Py_DECREF (object);
  246.               return NULL;
  247.             }

  248.           object->pspace = pspace;
  249.           set_program_space_data (pspace, pspy_pspace_data_key, object);
  250.         }
  251.     }

  252.   return (PyObject *) object;
  253. }

  254. int
  255. gdbpy_initialize_pspace (void)
  256. {
  257.   pspy_pspace_data_key
  258.     = register_program_space_data_with_cleanup (NULL, py_free_pspace);

  259.   if (PyType_Ready (&pspace_object_type) < 0)
  260.     return -1;

  261.   return gdb_pymodule_addobject (gdb_module, "Progspace",
  262.                                  (PyObject *) &pspace_object_type);
  263. }



  264. static PyGetSetDef pspace_getset[] =
  265. {
  266.   { "__dict__", gdb_py_generic_dict, NULL,
  267.     "The __dict__ for this progspace.", &pspace_object_type },
  268.   { "filename", pspy_get_filename, NULL,
  269.     "The progspace's main filename, or None.", NULL },
  270.   { "pretty_printers", pspy_get_printers, pspy_set_printers,
  271.     "Pretty printers.", NULL },
  272.   { "frame_filters", pspy_get_frame_filters, pspy_set_frame_filters,
  273.     "Frame filters.", NULL },
  274.   { "type_printers", pspy_get_type_printers, pspy_set_type_printers,
  275.     "Type printers.", NULL },
  276.   { "xmethods", pspy_get_xmethods, NULL,
  277.     "Debug methods.", NULL },
  278.   { NULL }
  279. };

  280. static PyTypeObject pspace_object_type =
  281. {
  282.   PyVarObject_HEAD_INIT (NULL, 0)
  283.   "gdb.Progspace",                  /*tp_name*/
  284.   sizeof (pspace_object),          /*tp_basicsize*/
  285.   0,                                  /*tp_itemsize*/
  286.   pspy_dealloc,                          /*tp_dealloc*/
  287.   0,                                  /*tp_print*/
  288.   0,                                  /*tp_getattr*/
  289.   0,                                  /*tp_setattr*/
  290.   0,                                  /*tp_compare*/
  291.   0,                                  /*tp_repr*/
  292.   0,                                  /*tp_as_number*/
  293.   0,                                  /*tp_as_sequence*/
  294.   0,                                  /*tp_as_mapping*/
  295.   0,                                  /*tp_hash */
  296.   0,                                  /*tp_call*/
  297.   0,                                  /*tp_str*/
  298.   0,                                  /*tp_getattro*/
  299.   0,                                  /*tp_setattro*/
  300.   0,                                  /*tp_as_buffer*/
  301.   Py_TPFLAGS_DEFAULT,                  /*tp_flags*/
  302.   "GDB progspace object",          /* tp_doc */
  303.   0,                                  /* tp_traverse */
  304.   0,                                  /* tp_clear */
  305.   0,                                  /* tp_richcompare */
  306.   0,                                  /* tp_weaklistoffset */
  307.   0,                                  /* tp_iter */
  308.   0,                                  /* tp_iternext */
  309.   0,                                  /* tp_methods */
  310.   0,                                  /* tp_members */
  311.   pspace_getset,                  /* tp_getset */
  312.   0,                                  /* tp_base */
  313.   0,                                  /* tp_dict */
  314.   0,                                  /* tp_descr_get */
  315.   0,                                  /* tp_descr_set */
  316.   offsetof (pspace_object, dict), /* tp_dictoffset */
  317.   0,                                  /* tp_init */
  318.   0,                                  /* tp_alloc */
  319.   pspy_new,                          /* tp_new */
  320. };