xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/python/py-progspace.c (revision a5a4af3bd380a7b58b758d9b311cef9f7c34aeb4)
1 /* Python interface to program spaces.
2 
3    Copyright (C) 2010-2015 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   /* Dictionary holding user-added attributes.
36      This is the __dict__ attribute of the object.  */
37   PyObject *dict;
38 
39   /* The pretty-printer list of functions.  */
40   PyObject *printers;
41 
42   /* The frame filter list of functions.  */
43   PyObject *frame_filters;
44 
45   /* The frame unwinder list.  */
46   PyObject *frame_unwinders;
47 
48   /* The type-printer list.  */
49   PyObject *type_printers;
50 
51   /* The debug method list.  */
52   PyObject *xmethods;
53 } pspace_object;
54 
55 extern PyTypeObject pspace_object_type
56     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object");
57 
58 static const struct program_space_data *pspy_pspace_data_key;
59 
60 
61 
62 /* An Objfile method which returns the objfile's file name, or None.  */
63 
64 static PyObject *
65 pspy_get_filename (PyObject *self, void *closure)
66 {
67   pspace_object *obj = (pspace_object *) self;
68 
69   if (obj->pspace)
70     {
71       struct objfile *objfile = obj->pspace->symfile_object_file;
72 
73       if (objfile)
74 	return PyString_Decode (objfile_name (objfile),
75 				strlen (objfile_name (objfile)),
76 				host_charset (), NULL);
77     }
78   Py_RETURN_NONE;
79 }
80 
81 static void
82 pspy_dealloc (PyObject *self)
83 {
84   pspace_object *ps_self = (pspace_object *) self;
85 
86   Py_XDECREF (ps_self->dict);
87   Py_XDECREF (ps_self->printers);
88   Py_XDECREF (ps_self->frame_filters);
89   Py_XDECREF (ps_self->frame_unwinders);
90   Py_XDECREF (ps_self->type_printers);
91   Py_XDECREF (ps_self->xmethods);
92   Py_TYPE (self)->tp_free (self);
93 }
94 
95 /* Initialize a pspace_object.
96    The result is a boolean indicating success.  */
97 
98 static int
99 pspy_initialize (pspace_object *self)
100 {
101   self->pspace = NULL;
102   self->dict = NULL;
103 
104   self->printers = PyList_New (0);
105   if (self->printers == NULL)
106     return 0;
107 
108   self->frame_filters = PyDict_New ();
109   if (self->frame_filters == NULL)
110     return 0;
111 
112   self->frame_unwinders = PyList_New (0);
113   if (self->frame_unwinders == NULL)
114     return 0;
115 
116   self->type_printers = PyList_New (0);
117   if (self->type_printers == NULL)
118     return 0;
119 
120   self->xmethods = PyList_New (0);
121   if (self->xmethods == NULL)
122     return 0;
123 
124   return 1;
125 }
126 
127 static PyObject *
128 pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
129 {
130   pspace_object *self = (pspace_object *) type->tp_alloc (type, 0);
131 
132   if (self)
133     {
134       if (!pspy_initialize (self))
135 	{
136 	  Py_DECREF (self);
137 	  return NULL;
138 	}
139     }
140 
141   return (PyObject *) self;
142 }
143 
144 PyObject *
145 pspy_get_printers (PyObject *o, void *ignore)
146 {
147   pspace_object *self = (pspace_object *) o;
148 
149   Py_INCREF (self->printers);
150   return self->printers;
151 }
152 
153 static int
154 pspy_set_printers (PyObject *o, PyObject *value, void *ignore)
155 {
156   PyObject *tmp;
157   pspace_object *self = (pspace_object *) o;
158 
159   if (! value)
160     {
161       PyErr_SetString (PyExc_TypeError,
162 		       "cannot delete the pretty_printers attribute");
163       return -1;
164     }
165 
166   if (! PyList_Check (value))
167     {
168       PyErr_SetString (PyExc_TypeError,
169 		       "the pretty_printers attribute must be a list");
170       return -1;
171     }
172 
173   /* Take care in case the LHS and RHS are related somehow.  */
174   tmp = self->printers;
175   Py_INCREF (value);
176   self->printers = value;
177   Py_XDECREF (tmp);
178 
179   return 0;
180 }
181 
182 /* Return the Python dictionary attribute containing frame filters for
183    this program space.  */
184 PyObject *
185 pspy_get_frame_filters (PyObject *o, void *ignore)
186 {
187   pspace_object *self = (pspace_object *) o;
188 
189   Py_INCREF (self->frame_filters);
190   return self->frame_filters;
191 }
192 
193 /* Set this object file's frame filters dictionary to FILTERS.  */
194 static int
195 pspy_set_frame_filters (PyObject *o, PyObject *frame, void *ignore)
196 {
197   PyObject *tmp;
198   pspace_object *self = (pspace_object *) o;
199 
200   if (! frame)
201     {
202       PyErr_SetString (PyExc_TypeError,
203 		       "cannot delete the frame filter attribute");
204       return -1;
205     }
206 
207   if (! PyDict_Check (frame))
208     {
209       PyErr_SetString (PyExc_TypeError,
210 		       "the frame filter attribute must be a dictionary");
211       return -1;
212     }
213 
214   /* Take care in case the LHS and RHS are related somehow.  */
215   tmp = self->frame_filters;
216   Py_INCREF (frame);
217   self->frame_filters = frame;
218   Py_XDECREF (tmp);
219 
220   return 0;
221 }
222 
223 /* Return the list of the frame unwinders for this program space.  */
224 
225 PyObject *
226 pspy_get_frame_unwinders (PyObject *o, void *ignore)
227 {
228   pspace_object *self = (pspace_object *) o;
229 
230   Py_INCREF (self->frame_unwinders);
231   return self->frame_unwinders;
232 }
233 
234 /* Set this program space's list of the unwinders to UNWINDERS.  */
235 
236 static int
237 pspy_set_frame_unwinders (PyObject *o, PyObject *unwinders, void *ignore)
238 {
239   PyObject *tmp;
240   pspace_object *self = (pspace_object *) o;
241 
242   if (!unwinders)
243     {
244       PyErr_SetString (PyExc_TypeError,
245 		       "cannot delete the frame unwinders list");
246       return -1;
247     }
248 
249   if (!PyList_Check (unwinders))
250     {
251       PyErr_SetString (PyExc_TypeError,
252 		       "the frame unwinders attribute must be a list");
253       return -1;
254     }
255 
256   /* Take care in case the LHS and RHS are related somehow.  */
257   tmp = self->frame_unwinders;
258   Py_INCREF (unwinders);
259   self->frame_unwinders = unwinders;
260   Py_XDECREF (tmp);
261 
262   return 0;
263 }
264 
265 /* Get the 'type_printers' attribute.  */
266 
267 static PyObject *
268 pspy_get_type_printers (PyObject *o, void *ignore)
269 {
270   pspace_object *self = (pspace_object *) o;
271 
272   Py_INCREF (self->type_printers);
273   return self->type_printers;
274 }
275 
276 /* Get the 'xmethods' attribute.  */
277 
278 PyObject *
279 pspy_get_xmethods (PyObject *o, void *ignore)
280 {
281   pspace_object *self = (pspace_object *) o;
282 
283   Py_INCREF (self->xmethods);
284   return self->xmethods;
285 }
286 
287 /* Set the 'type_printers' attribute.  */
288 
289 static int
290 pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
291 {
292   PyObject *tmp;
293   pspace_object *self = (pspace_object *) o;
294 
295   if (! value)
296     {
297       PyErr_SetString (PyExc_TypeError,
298 		       "cannot delete the type_printers attribute");
299       return -1;
300     }
301 
302   if (! PyList_Check (value))
303     {
304       PyErr_SetString (PyExc_TypeError,
305 		       "the type_printers attribute must be a list");
306       return -1;
307     }
308 
309   /* Take care in case the LHS and RHS are related somehow.  */
310   tmp = self->type_printers;
311   Py_INCREF (value);
312   self->type_printers = value;
313   Py_XDECREF (tmp);
314 
315   return 0;
316 }
317 
318 
319 
320 /* Clear the PSPACE pointer in a Pspace object and remove the reference.  */
321 
322 static void
323 py_free_pspace (struct program_space *pspace, void *datum)
324 {
325   struct cleanup *cleanup;
326   pspace_object *object = datum;
327   /* This is a fiction, but we're in a nasty spot: The pspace is in the
328      process of being deleted, we can't rely on anything in it.  Plus
329      this is one time when the current program space and current inferior
330      are not in sync: All inferiors that use PSPACE may no longer exist.
331      We don't need to do much here, and since "there is always an inferior"
332      using target_gdbarch suffices.
333      Note: We cannot call get_current_arch because it may try to access
334      the target, which may involve accessing data in the pspace currently
335      being deleted.  */
336   struct gdbarch *arch = target_gdbarch ();
337 
338   cleanup = ensure_python_env (arch, current_language);
339   object->pspace = NULL;
340   Py_DECREF ((PyObject *) object);
341   do_cleanups (cleanup);
342 }
343 
344 /* Return a borrowed reference to the Python object of type Pspace
345    representing PSPACE.  If the object has already been created,
346    return it.  Otherwise, create it.  Return NULL and set the Python
347    error on failure.  */
348 
349 PyObject *
350 pspace_to_pspace_object (struct program_space *pspace)
351 {
352   pspace_object *object;
353 
354   object = program_space_data (pspace, pspy_pspace_data_key);
355   if (!object)
356     {
357       object = PyObject_New (pspace_object, &pspace_object_type);
358       if (object)
359 	{
360 	  if (!pspy_initialize (object))
361 	    {
362 	      Py_DECREF (object);
363 	      return NULL;
364 	    }
365 
366 	  object->pspace = pspace;
367 	  set_program_space_data (pspace, pspy_pspace_data_key, object);
368 	}
369     }
370 
371   return (PyObject *) object;
372 }
373 
374 int
375 gdbpy_initialize_pspace (void)
376 {
377   pspy_pspace_data_key
378     = register_program_space_data_with_cleanup (NULL, py_free_pspace);
379 
380   if (PyType_Ready (&pspace_object_type) < 0)
381     return -1;
382 
383   return gdb_pymodule_addobject (gdb_module, "Progspace",
384 				 (PyObject *) &pspace_object_type);
385 }
386 
387 
388 
389 static PyGetSetDef pspace_getset[] =
390 {
391   { "__dict__", gdb_py_generic_dict, NULL,
392     "The __dict__ for this progspace.", &pspace_object_type },
393   { "filename", pspy_get_filename, NULL,
394     "The progspace's main filename, or None.", NULL },
395   { "pretty_printers", pspy_get_printers, pspy_set_printers,
396     "Pretty printers.", NULL },
397   { "frame_filters", pspy_get_frame_filters, pspy_set_frame_filters,
398     "Frame filters.", NULL },
399   { "frame_unwinders", pspy_get_frame_unwinders, pspy_set_frame_unwinders,
400     "Frame unwinders.", NULL },
401   { "type_printers", pspy_get_type_printers, pspy_set_type_printers,
402     "Type printers.", NULL },
403   { "xmethods", pspy_get_xmethods, NULL,
404     "Debug methods.", NULL },
405   { NULL }
406 };
407 
408 PyTypeObject pspace_object_type =
409 {
410   PyVarObject_HEAD_INIT (NULL, 0)
411   "gdb.Progspace",		  /*tp_name*/
412   sizeof (pspace_object),	  /*tp_basicsize*/
413   0,				  /*tp_itemsize*/
414   pspy_dealloc,			  /*tp_dealloc*/
415   0,				  /*tp_print*/
416   0,				  /*tp_getattr*/
417   0,				  /*tp_setattr*/
418   0,				  /*tp_compare*/
419   0,				  /*tp_repr*/
420   0,				  /*tp_as_number*/
421   0,				  /*tp_as_sequence*/
422   0,				  /*tp_as_mapping*/
423   0,				  /*tp_hash */
424   0,				  /*tp_call*/
425   0,				  /*tp_str*/
426   0,				  /*tp_getattro*/
427   0,				  /*tp_setattro*/
428   0,				  /*tp_as_buffer*/
429   Py_TPFLAGS_DEFAULT,		  /*tp_flags*/
430   "GDB progspace object",	  /* tp_doc */
431   0,				  /* tp_traverse */
432   0,				  /* tp_clear */
433   0,				  /* tp_richcompare */
434   0,				  /* tp_weaklistoffset */
435   0,				  /* tp_iter */
436   0,				  /* tp_iternext */
437   0,				  /* tp_methods */
438   0,				  /* tp_members */
439   pspace_getset,		  /* tp_getset */
440   0,				  /* tp_base */
441   0,				  /* tp_dict */
442   0,				  /* tp_descr_get */
443   0,				  /* tp_descr_set */
444   offsetof (pspace_object, dict), /* tp_dictoffset */
445   0,				  /* tp_init */
446   0,				  /* tp_alloc */
447   pspy_new,			  /* tp_new */
448 };
449