GDB (API)
/home/stan/gdb/src/gdb/python/py-objfile.c
Go to the documentation of this file.
00001 /* Python interface to objfiles.
00002 
00003    Copyright (C) 2008-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "python-internal.h"
00022 #include "charset.h"
00023 #include "objfiles.h"
00024 #include "language.h"
00025 
00026 typedef struct
00027 {
00028   PyObject_HEAD
00029 
00030   /* The corresponding objfile.  */
00031   struct objfile *objfile;
00032 
00033   /* The pretty-printer list of functions.  */
00034   PyObject *printers;
00035 
00036   /* The frame filter list of functions.  */
00037   PyObject *frame_filters;
00038   /* The type-printer list.  */
00039   PyObject *type_printers;
00040 } objfile_object;
00041 
00042 static PyTypeObject objfile_object_type
00043     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("objfile_object");
00044 
00045 static const struct objfile_data *objfpy_objfile_data_key;
00046 
00047 
00048 
00049 /* An Objfile method which returns the objfile's file name, or None.  */
00050 static PyObject *
00051 objfpy_get_filename (PyObject *self, void *closure)
00052 {
00053   objfile_object *obj = (objfile_object *) self;
00054 
00055   if (obj->objfile)
00056     return PyString_Decode (objfile_name (obj->objfile),
00057                             strlen (objfile_name (obj->objfile)),
00058                             host_charset (), NULL);
00059   Py_RETURN_NONE;
00060 }
00061 
00062 static void
00063 objfpy_dealloc (PyObject *o)
00064 {
00065   objfile_object *self = (objfile_object *) o;
00066 
00067   Py_XDECREF (self->printers);
00068   Py_XDECREF (self->frame_filters);
00069   Py_XDECREF (self->type_printers);
00070   Py_TYPE (self)->tp_free (self);
00071 }
00072 
00073 static PyObject *
00074 objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
00075 {
00076   objfile_object *self = (objfile_object *) type->tp_alloc (type, 0);
00077 
00078   if (self)
00079     {
00080       self->objfile = NULL;
00081 
00082       self->printers = PyList_New (0);
00083       if (!self->printers)
00084         {
00085           Py_DECREF (self);
00086           return NULL;
00087         }
00088 
00089       self->frame_filters = PyDict_New ();
00090       if (!self->frame_filters)
00091         {
00092           Py_DECREF (self);
00093           return NULL;
00094         }
00095 
00096       self->type_printers = PyList_New (0);
00097       if (!self->type_printers)
00098         {
00099           Py_DECREF (self);
00100           return NULL;
00101         }
00102     }
00103   return (PyObject *) self;
00104 }
00105 
00106 PyObject *
00107 objfpy_get_printers (PyObject *o, void *ignore)
00108 {
00109   objfile_object *self = (objfile_object *) o;
00110 
00111   Py_INCREF (self->printers);
00112   return self->printers;
00113 }
00114 
00115 static int
00116 objfpy_set_printers (PyObject *o, PyObject *value, void *ignore)
00117 {
00118   PyObject *tmp;
00119   objfile_object *self = (objfile_object *) o;
00120 
00121   if (! value)
00122     {
00123       PyErr_SetString (PyExc_TypeError,
00124                        _("Cannot delete the pretty_printers attribute."));
00125       return -1;
00126     }
00127 
00128   if (! PyList_Check (value))
00129     {
00130       PyErr_SetString (PyExc_TypeError,
00131                        _("The pretty_printers attribute must be a list."));
00132       return -1;
00133     }
00134 
00135   /* Take care in case the LHS and RHS are related somehow.  */
00136   tmp = self->printers;
00137   Py_INCREF (value);
00138   self->printers = value;
00139   Py_XDECREF (tmp);
00140 
00141   return 0;
00142 }
00143 
00144 /* Return the Python dictionary attribute containing frame filters for
00145    this object file.  */
00146 PyObject *
00147 objfpy_get_frame_filters (PyObject *o, void *ignore)
00148 {
00149   objfile_object *self = (objfile_object *) o;
00150 
00151   Py_INCREF (self->frame_filters);
00152   return self->frame_filters;
00153 }
00154 
00155 /* Set this object file's frame filters dictionary to FILTERS.  */
00156 static int
00157 objfpy_set_frame_filters (PyObject *o, PyObject *filters, void *ignore)
00158 {
00159   PyObject *tmp;
00160   objfile_object *self = (objfile_object *) o;
00161 
00162   if (! filters)
00163     {
00164       PyErr_SetString (PyExc_TypeError,
00165                        _("Cannot delete the frame filters attribute."));
00166       return -1;
00167     }
00168 
00169   if (! PyDict_Check (filters))
00170     {
00171       PyErr_SetString (PyExc_TypeError,
00172                        _("The frame_filters attribute must be a dictionary."));
00173       return -1;
00174     }
00175 
00176   /* Take care in case the LHS and RHS are related somehow.  */
00177   tmp = self->frame_filters;
00178   Py_INCREF (filters);
00179   self->frame_filters = filters;
00180   Py_XDECREF (tmp);
00181 
00182   return 0;
00183 }
00184 
00185 /* Get the 'type_printers' attribute.  */
00186 
00187 static PyObject *
00188 objfpy_get_type_printers (PyObject *o, void *ignore)
00189 {
00190   objfile_object *self = (objfile_object *) o;
00191 
00192   Py_INCREF (self->type_printers);
00193   return self->type_printers;
00194 }
00195 
00196 /* Set the 'type_printers' attribute.  */
00197 
00198 static int
00199 objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
00200 {
00201   PyObject *tmp;
00202   objfile_object *self = (objfile_object *) o;
00203 
00204   if (! value)
00205     {
00206       PyErr_SetString (PyExc_TypeError,
00207                        _("Cannot delete the type_printers attribute."));
00208       return -1;
00209     }
00210 
00211   if (! PyList_Check (value))
00212     {
00213       PyErr_SetString (PyExc_TypeError,
00214                        _("The type_printers attribute must be a list."));
00215       return -1;
00216     }
00217 
00218   /* Take care in case the LHS and RHS are related somehow.  */
00219   tmp = self->type_printers;
00220   Py_INCREF (value);
00221   self->type_printers = value;
00222   Py_XDECREF (tmp);
00223 
00224   return 0;
00225 }
00226 
00227 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
00228    Returns True if this object file still exists in GDB.  */
00229 
00230 static PyObject *
00231 objfpy_is_valid (PyObject *self, PyObject *args)
00232 {
00233   objfile_object *obj = (objfile_object *) self;
00234 
00235   if (! obj->objfile)
00236     Py_RETURN_FALSE;
00237 
00238   Py_RETURN_TRUE;
00239 }
00240 
00241 
00242 
00243 /* Clear the OBJFILE pointer in an Objfile object and remove the
00244    reference.  */
00245 static void
00246 py_free_objfile (struct objfile *objfile, void *datum)
00247 {
00248   struct cleanup *cleanup;
00249   objfile_object *object = datum;
00250 
00251   cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
00252   object->objfile = NULL;
00253   Py_DECREF ((PyObject *) object);
00254   do_cleanups (cleanup);
00255 }
00256 
00257 /* Return a borrowed reference to the Python object of type Objfile
00258    representing OBJFILE.  If the object has already been created,
00259    return it.  Otherwise, create it.  Return NULL and set the Python
00260    error on failure.  */
00261 PyObject *
00262 objfile_to_objfile_object (struct objfile *objfile)
00263 {
00264   objfile_object *object;
00265 
00266   object = objfile_data (objfile, objfpy_objfile_data_key);
00267   if (!object)
00268     {
00269       object = PyObject_New (objfile_object, &objfile_object_type);
00270       if (object)
00271         {
00272           object->objfile = objfile;
00273 
00274           object->printers = PyList_New (0);
00275           if (!object->printers)
00276             {
00277               Py_DECREF (object);
00278               return NULL;
00279             }
00280 
00281           object->frame_filters = PyDict_New ();
00282           if (!object->frame_filters)
00283             {
00284               Py_DECREF (object);
00285               return NULL;
00286             }
00287 
00288           object->type_printers = PyList_New (0);
00289           if (!object->type_printers)
00290             {
00291               Py_DECREF (object);
00292               return NULL;
00293             }
00294 
00295           set_objfile_data (objfile, objfpy_objfile_data_key, object);
00296         }
00297     }
00298 
00299   return (PyObject *) object;
00300 }
00301 
00302 int
00303 gdbpy_initialize_objfile (void)
00304 {
00305   objfpy_objfile_data_key
00306     = register_objfile_data_with_cleanup (NULL, py_free_objfile);
00307 
00308   if (PyType_Ready (&objfile_object_type) < 0)
00309     return -1;
00310 
00311   return gdb_pymodule_addobject (gdb_module, "Objfile",
00312                                  (PyObject *) &objfile_object_type);
00313 }
00314 
00315 
00316 
00317 static PyMethodDef objfile_object_methods[] =
00318 {
00319   { "is_valid", objfpy_is_valid, METH_NOARGS,
00320     "is_valid () -> Boolean.\n\
00321 Return true if this object file is valid, false if not." },
00322 
00323   { NULL }
00324 };
00325 
00326 static PyGetSetDef objfile_getset[] =
00327 {
00328   { "filename", objfpy_get_filename, NULL,
00329     "The objfile's filename, or None.", NULL },
00330   { "pretty_printers", objfpy_get_printers, objfpy_set_printers,
00331     "Pretty printers.", NULL },
00332   { "frame_filters", objfpy_get_frame_filters,
00333     objfpy_set_frame_filters, "Frame Filters.", NULL },
00334   { "type_printers", objfpy_get_type_printers, objfpy_set_type_printers,
00335     "Type printers.", NULL },
00336   { NULL }
00337 };
00338 
00339 static PyTypeObject objfile_object_type =
00340 {
00341   PyVarObject_HEAD_INIT (NULL, 0)
00342   "gdb.Objfile",                  /*tp_name*/
00343   sizeof (objfile_object),        /*tp_basicsize*/
00344   0,                              /*tp_itemsize*/
00345   objfpy_dealloc,                 /*tp_dealloc*/
00346   0,                              /*tp_print*/
00347   0,                              /*tp_getattr*/
00348   0,                              /*tp_setattr*/
00349   0,                              /*tp_compare*/
00350   0,                              /*tp_repr*/
00351   0,                              /*tp_as_number*/
00352   0,                              /*tp_as_sequence*/
00353   0,                              /*tp_as_mapping*/
00354   0,                              /*tp_hash */
00355   0,                              /*tp_call*/
00356   0,                              /*tp_str*/
00357   0,                              /*tp_getattro*/
00358   0,                              /*tp_setattro*/
00359   0,                              /*tp_as_buffer*/
00360   Py_TPFLAGS_DEFAULT,             /*tp_flags*/
00361   "GDB objfile object",           /* tp_doc */
00362   0,                              /* tp_traverse */
00363   0,                              /* tp_clear */
00364   0,                              /* tp_richcompare */
00365   0,                              /* tp_weaklistoffset */
00366   0,                              /* tp_iter */
00367   0,                              /* tp_iternext */
00368   objfile_object_methods,         /* tp_methods */
00369   0,                              /* tp_members */
00370   objfile_getset,                 /* tp_getset */
00371   0,                              /* tp_base */
00372   0,                              /* tp_dict */
00373   0,                              /* tp_descr_get */
00374   0,                              /* tp_descr_set */
00375   0,                              /* tp_dictoffset */
00376   0,                              /* tp_init */
00377   0,                              /* tp_alloc */
00378   objfpy_new,                     /* tp_new */
00379 };
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines