5d4911a778428124d33e43ed593743eefedb2d77
[platform/upstream/binutils.git] / gdb / python / py-progspace.c
1 /* Python interface to program spaces.
2
3    Copyright (C) 2010-2013 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "python-internal.h"
22 #include "charset.h"
23 #include "progspace.h"
24 #include "objfiles.h"
25 #include "language.h"
26 #include "arch-utils.h"
27
28 typedef struct
29 {
30   PyObject_HEAD
31
32   /* The corresponding pspace.  */
33   struct program_space *pspace;
34
35   /* The pretty-printer list of functions.  */
36   PyObject *printers;
37
38   /* The frame filter list of functions.  */
39   PyObject *frame_filters;
40   /* The type-printer list.  */
41   PyObject *type_printers;
42 } pspace_object;
43
44 static PyTypeObject pspace_object_type
45     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object");
46
47 static const struct program_space_data *pspy_pspace_data_key;
48
49 \f
50
51 /* An Objfile method which returns the objfile's file name, or None.  */
52
53 static PyObject *
54 pspy_get_filename (PyObject *self, void *closure)
55 {
56   pspace_object *obj = (pspace_object *) self;
57
58   if (obj->pspace)
59     {
60       struct objfile *objfile = obj->pspace->symfile_object_file;
61
62       if (objfile)
63         return PyString_Decode (objfile->name, strlen (objfile->name),
64                                 host_charset (), NULL);
65     }
66   Py_RETURN_NONE;
67 }
68
69 static void
70 pspy_dealloc (PyObject *self)
71 {
72   pspace_object *ps_self = (pspace_object *) self;
73
74   Py_XDECREF (ps_self->printers);
75   Py_XDECREF (ps_self->frame_filters);
76   Py_XDECREF (ps_self->type_printers);
77   Py_TYPE (self)->tp_free (self);
78 }
79
80 static PyObject *
81 pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
82 {
83   pspace_object *self = (pspace_object *) type->tp_alloc (type, 0);
84
85   if (self)
86     {
87       self->pspace = NULL;
88
89       self->printers = PyList_New (0);
90       if (!self->printers)
91         {
92           Py_DECREF (self);
93           return NULL;
94         }
95
96       self->frame_filters = PyDict_New ();
97       if (!self->frame_filters)
98         {
99           Py_DECREF (self);
100           return NULL;
101         }
102
103       self->type_printers = PyList_New (0);
104       if (!self->type_printers)
105         {
106           Py_DECREF (self);
107           return NULL;
108         }
109     }
110   return (PyObject *) self;
111 }
112
113 PyObject *
114 pspy_get_printers (PyObject *o, void *ignore)
115 {
116   pspace_object *self = (pspace_object *) o;
117
118   Py_INCREF (self->printers);
119   return self->printers;
120 }
121
122 static int
123 pspy_set_printers (PyObject *o, PyObject *value, void *ignore)
124 {
125   PyObject *tmp;
126   pspace_object *self = (pspace_object *) o;
127
128   if (! value)
129     {
130       PyErr_SetString (PyExc_TypeError,
131                        "cannot delete the pretty_printers attribute");
132       return -1;
133     }
134
135   if (! PyList_Check (value))
136     {
137       PyErr_SetString (PyExc_TypeError,
138                        "the pretty_printers attribute must be a list");
139       return -1;
140     }
141
142   /* Take care in case the LHS and RHS are related somehow.  */
143   tmp = self->printers;
144   Py_INCREF (value);
145   self->printers = value;
146   Py_XDECREF (tmp);
147
148   return 0;
149 }
150
151 /* Return the Python dictionary attribute containing frame filters for
152    this program space.  */
153 PyObject *
154 pspy_get_frame_filters (PyObject *o, void *ignore)
155 {
156   pspace_object *self = (pspace_object *) o;
157
158   Py_INCREF (self->frame_filters);
159   return self->frame_filters;
160 }
161
162 /* Set this object file's frame filters dictionary to FILTERS.  */
163 static int
164 pspy_set_frame_filters (PyObject *o, PyObject *frame, void *ignore)
165 {
166   PyObject *tmp;
167   pspace_object *self = (pspace_object *) o;
168
169   if (! frame)
170     {
171       PyErr_SetString (PyExc_TypeError,
172                        "cannot delete the frame filter attribute");
173       return -1;
174     }
175
176   if (! PyDict_Check (frame))
177     {
178       PyErr_SetString (PyExc_TypeError,
179                        "the frame filter attribute must be a dictionary");
180       return -1;
181     }
182
183   /* Take care in case the LHS and RHS are related somehow.  */
184   tmp = self->frame_filters;
185   Py_INCREF (frame);
186   self->frame_filters = frame;
187   Py_XDECREF (tmp);
188
189   return 0;
190 }
191
192 /* Get the 'type_printers' attribute.  */
193
194 static PyObject *
195 pspy_get_type_printers (PyObject *o, void *ignore)
196 {
197   pspace_object *self = (pspace_object *) o;
198
199   Py_INCREF (self->type_printers);
200   return self->type_printers;
201 }
202
203 /* Set the 'type_printers' attribute.  */
204
205 static int
206 pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
207 {
208   PyObject *tmp;
209   pspace_object *self = (pspace_object *) o;
210
211   if (! value)
212     {
213       PyErr_SetString (PyExc_TypeError,
214                        "cannot delete the type_printers attribute");
215       return -1;
216     }
217
218   if (! PyList_Check (value))
219     {
220       PyErr_SetString (PyExc_TypeError,
221                        "the type_printers attribute must be a list");
222       return -1;
223     }
224
225   /* Take care in case the LHS and RHS are related somehow.  */
226   tmp = self->type_printers;
227   Py_INCREF (value);
228   self->type_printers = value;
229   Py_XDECREF (tmp);
230
231   return 0;
232 }
233
234 \f
235
236 /* Clear the PSPACE pointer in a Pspace object and remove the reference.  */
237
238 static void
239 py_free_pspace (struct program_space *pspace, void *datum)
240 {
241   struct cleanup *cleanup;
242   pspace_object *object = datum;
243   struct gdbarch *arch = get_current_arch ();
244
245   cleanup = ensure_python_env (arch, current_language);
246   object->pspace = NULL;
247   Py_DECREF ((PyObject *) object);
248   do_cleanups (cleanup);
249 }
250
251 /* Return a borrowed reference to the Python object of type Pspace
252    representing PSPACE.  If the object has already been created,
253    return it.  Otherwise, create it.  Return NULL and set the Python
254    error on failure.  */
255
256 PyObject *
257 pspace_to_pspace_object (struct program_space *pspace)
258 {
259   pspace_object *object;
260
261   object = program_space_data (pspace, pspy_pspace_data_key);
262   if (!object)
263     {
264       object = PyObject_New (pspace_object, &pspace_object_type);
265       if (object)
266         {
267           object->pspace = pspace;
268
269           object->printers = PyList_New (0);
270           if (!object->printers)
271             {
272               Py_DECREF (object);
273               return NULL;
274             }
275
276           object->frame_filters = PyDict_New ();
277           if (!object->frame_filters)
278             {
279               Py_DECREF (object);
280               return NULL;
281             }
282
283           object->type_printers = PyList_New (0);
284           if (!object->type_printers)
285             {
286               Py_DECREF (object);
287               return NULL;
288             }
289
290           set_program_space_data (pspace, pspy_pspace_data_key, object);
291         }
292     }
293
294   return (PyObject *) object;
295 }
296
297 void
298 gdbpy_initialize_pspace (void)
299 {
300   pspy_pspace_data_key
301     = register_program_space_data_with_cleanup (NULL, py_free_pspace);
302
303   if (PyType_Ready (&pspace_object_type) < 0)
304     return;
305
306   Py_INCREF (&pspace_object_type);
307   PyModule_AddObject (gdb_module, "Progspace",
308                       (PyObject *) &pspace_object_type);
309 }
310
311 \f
312
313 static PyGetSetDef pspace_getset[] =
314 {
315   { "filename", pspy_get_filename, NULL,
316     "The progspace's main filename, or None.", NULL },
317   { "pretty_printers", pspy_get_printers, pspy_set_printers,
318     "Pretty printers.", NULL },
319   { "frame_filters", pspy_get_frame_filters, pspy_set_frame_filters,
320     "Frame filters.", NULL },
321   { "type_printers", pspy_get_type_printers, pspy_set_type_printers,
322     "Type printers.", NULL },
323   { NULL }
324 };
325
326 static PyTypeObject pspace_object_type =
327 {
328   PyVarObject_HEAD_INIT (NULL, 0)
329   "gdb.Progspace",                /*tp_name*/
330   sizeof (pspace_object),         /*tp_basicsize*/
331   0,                              /*tp_itemsize*/
332   pspy_dealloc,                   /*tp_dealloc*/
333   0,                              /*tp_print*/
334   0,                              /*tp_getattr*/
335   0,                              /*tp_setattr*/
336   0,                              /*tp_compare*/
337   0,                              /*tp_repr*/
338   0,                              /*tp_as_number*/
339   0,                              /*tp_as_sequence*/
340   0,                              /*tp_as_mapping*/
341   0,                              /*tp_hash */
342   0,                              /*tp_call*/
343   0,                              /*tp_str*/
344   0,                              /*tp_getattro*/
345   0,                              /*tp_setattro*/
346   0,                              /*tp_as_buffer*/
347   Py_TPFLAGS_DEFAULT,             /*tp_flags*/
348   "GDB progspace object",         /* tp_doc */
349   0,                              /* tp_traverse */
350   0,                              /* tp_clear */
351   0,                              /* tp_richcompare */
352   0,                              /* tp_weaklistoffset */
353   0,                              /* tp_iter */
354   0,                              /* tp_iternext */
355   0,                              /* tp_methods */
356   0,                              /* tp_members */
357   pspace_getset,                  /* tp_getset */
358   0,                              /* tp_base */
359   0,                              /* tp_dict */
360   0,                              /* tp_descr_get */
361   0,                              /* tp_descr_set */
362   0,                              /* tp_dictoffset */
363   0,                              /* tp_init */
364   0,                              /* tp_alloc */
365   pspy_new,                       /* tp_new */
366 };