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