* python/py-cmd.c (cmdpy_completer): Use explicit decref.
[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
62eec1a5
TT
44static PyTypeObject pspace_object_type
45 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object");
fa33c3cd
DE
46
47static 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
53static PyObject *
54pspy_get_filename (PyObject *self, void *closure)
55{
56 pspace_object *obj = (pspace_object *) self;
d59b6f6c 57
fa33c3cd
DE
58 if (obj->pspace)
59 {
60 struct objfile *objfile = obj->pspace->symfile_object_file;
d59b6f6c 61
d31d2fc3 62 if (objfile)
fa33c3cd
DE
63 return PyString_Decode (objfile->name, strlen (objfile->name),
64 host_charset (), NULL);
65 }
66 Py_RETURN_NONE;
67}
68
69static void
70pspy_dealloc (PyObject *self)
71{
72 pspace_object *ps_self = (pspace_object *) self;
d59b6f6c 73
fa33c3cd 74 Py_XDECREF (ps_self->printers);
1e611234 75 Py_XDECREF (ps_self->frame_filters);
18a9fc12 76 Py_XDECREF (ps_self->type_printers);
9a27f2c6 77 Py_TYPE (self)->tp_free (self);
fa33c3cd
DE
78}
79
80static PyObject *
81pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
82{
83 pspace_object *self = (pspace_object *) type->tp_alloc (type, 0);
d59b6f6c 84
fa33c3cd
DE
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 }
18a9fc12 95
1e611234
PM
96 self->frame_filters = PyDict_New ();
97 if (!self->frame_filters)
98 {
99 Py_DECREF (self);
100 return NULL;
101 }
102
18a9fc12
TT
103 self->type_printers = PyList_New (0);
104 if (!self->type_printers)
105 {
106 Py_DECREF (self);
107 return NULL;
108 }
fa33c3cd
DE
109 }
110 return (PyObject *) self;
111}
112
113PyObject *
114pspy_get_printers (PyObject *o, void *ignore)
115{
116 pspace_object *self = (pspace_object *) o;
d59b6f6c 117
fa33c3cd
DE
118 Py_INCREF (self->printers);
119 return self->printers;
120}
121
122static int
123pspy_set_printers (PyObject *o, PyObject *value, void *ignore)
124{
125 PyObject *tmp;
126 pspace_object *self = (pspace_object *) o;
d59b6f6c 127
fa33c3cd
DE
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
1e611234
PM
151/* Return the Python dictionary attribute containing frame filters for
152 this program space. */
153PyObject *
154pspy_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. */
163static int
164pspy_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
18a9fc12
TT
192/* Get the 'type_printers' attribute. */
193
194static PyObject *
195pspy_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
205static int
206pspy_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
fa33c3cd
DE
234\f
235
236/* Clear the PSPACE pointer in a Pspace object and remove the reference. */
237
238static void
239py_free_pspace (struct program_space *pspace, void *datum)
240{
241 struct cleanup *cleanup;
242 pspace_object *object = datum;
b3422a0d 243 struct gdbarch *arch = get_current_arch ();
fa33c3cd
DE
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
256PyObject *
257pspace_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 {
fa33c3cd
DE
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
1e611234
PM
276 object->frame_filters = PyDict_New ();
277 if (!object->frame_filters)
278 {
279 Py_DECREF (object);
280 return NULL;
281 }
282
18a9fc12
TT
283 object->type_printers = PyList_New (0);
284 if (!object->type_printers)
285 {
286 Py_DECREF (object);
287 return NULL;
288 }
289
fa33c3cd
DE
290 set_program_space_data (pspace, pspy_pspace_data_key, object);
291 }
292 }
293
294 return (PyObject *) object;
295}
296
999633ed 297int
fa33c3cd
DE
298gdbpy_initialize_pspace (void)
299{
300 pspy_pspace_data_key
8e260fc0 301 = register_program_space_data_with_cleanup (NULL, py_free_pspace);
fa33c3cd
DE
302
303 if (PyType_Ready (&pspace_object_type) < 0)
999633ed 304 return -1;
fa33c3cd
DE
305
306 Py_INCREF (&pspace_object_type);
999633ed
TT
307 return PyModule_AddObject (gdb_module, "Progspace",
308 (PyObject *) &pspace_object_type);
fa33c3cd
DE
309}
310
311\f
312
313static 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 },
1e611234
PM
319 { "frame_filters", pspy_get_frame_filters, pspy_set_frame_filters,
320 "Frame filters.", NULL },
18a9fc12
TT
321 { "type_printers", pspy_get_type_printers, pspy_set_type_printers,
322 "Type printers.", NULL },
fa33c3cd
DE
323 { NULL }
324};
325
326static PyTypeObject pspace_object_type =
327{
9a27f2c6 328 PyVarObject_HEAD_INIT (NULL, 0)
fa33c3cd
DE
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};
This page took 0.361899 seconds and 4 git commands to generate.