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