Remove some code duplication in py-objfile.c, py-progspace.c.
[binutils-gdb.git] / gdb / python / py-objfile.c
1 /* Python interface to objfiles.
2
3 Copyright (C) 2008-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 "objfiles.h"
24 #include "language.h"
25
26 typedef struct
27 {
28 PyObject_HEAD
29
30 /* The corresponding objfile. */
31 struct objfile *objfile;
32
33 /* The pretty-printer list of functions. */
34 PyObject *printers;
35
36 /* The frame filter list of functions. */
37 PyObject *frame_filters;
38 /* The type-printer list. */
39 PyObject *type_printers;
40
41 /* The debug method matcher list. */
42 PyObject *xmethods;
43 } objfile_object;
44
45 static PyTypeObject objfile_object_type
46 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("objfile_object");
47
48 static const struct objfile_data *objfpy_objfile_data_key;
49
50 \f
51
52 /* An Objfile method which returns the objfile's file name, or None. */
53 static PyObject *
54 objfpy_get_filename (PyObject *self, void *closure)
55 {
56 objfile_object *obj = (objfile_object *) self;
57
58 if (obj->objfile)
59 return PyString_Decode (objfile_name (obj->objfile),
60 strlen (objfile_name (obj->objfile)),
61 host_charset (), NULL);
62 Py_RETURN_NONE;
63 }
64
65 static void
66 objfpy_dealloc (PyObject *o)
67 {
68 objfile_object *self = (objfile_object *) o;
69
70 Py_XDECREF (self->printers);
71 Py_XDECREF (self->frame_filters);
72 Py_XDECREF (self->type_printers);
73 Py_XDECREF (self->xmethods);
74 Py_TYPE (self)->tp_free (self);
75 }
76
77 /* Initialize an objfile_object.
78 The result is a boolean indicating success. */
79
80 static int
81 objfpy_initialize (objfile_object *self)
82 {
83 self->objfile = NULL;
84
85 self->printers = PyList_New (0);
86 if (self->printers == NULL)
87 return 0;
88
89 self->frame_filters = PyDict_New ();
90 if (self->frame_filters == NULL)
91 return 0;
92
93 self->type_printers = PyList_New (0);
94 if (self->type_printers == NULL)
95 return 0;
96
97 self->xmethods = PyList_New (0);
98 if (self->xmethods == NULL)
99 return 0;
100
101 return 1;
102 }
103
104 static PyObject *
105 objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
106 {
107 objfile_object *self = (objfile_object *) type->tp_alloc (type, 0);
108
109 if (self)
110 {
111 if (!objfpy_initialize (self))
112 {
113 Py_DECREF (self);
114 return NULL;
115 }
116 }
117
118 return (PyObject *) self;
119 }
120
121 PyObject *
122 objfpy_get_printers (PyObject *o, void *ignore)
123 {
124 objfile_object *self = (objfile_object *) o;
125
126 Py_INCREF (self->printers);
127 return self->printers;
128 }
129
130 static int
131 objfpy_set_printers (PyObject *o, PyObject *value, void *ignore)
132 {
133 PyObject *tmp;
134 objfile_object *self = (objfile_object *) o;
135
136 if (! value)
137 {
138 PyErr_SetString (PyExc_TypeError,
139 _("Cannot delete the pretty_printers attribute."));
140 return -1;
141 }
142
143 if (! PyList_Check (value))
144 {
145 PyErr_SetString (PyExc_TypeError,
146 _("The pretty_printers attribute must be a list."));
147 return -1;
148 }
149
150 /* Take care in case the LHS and RHS are related somehow. */
151 tmp = self->printers;
152 Py_INCREF (value);
153 self->printers = value;
154 Py_XDECREF (tmp);
155
156 return 0;
157 }
158
159 /* Return the Python dictionary attribute containing frame filters for
160 this object file. */
161 PyObject *
162 objfpy_get_frame_filters (PyObject *o, void *ignore)
163 {
164 objfile_object *self = (objfile_object *) o;
165
166 Py_INCREF (self->frame_filters);
167 return self->frame_filters;
168 }
169
170 /* Set this object file's frame filters dictionary to FILTERS. */
171 static int
172 objfpy_set_frame_filters (PyObject *o, PyObject *filters, void *ignore)
173 {
174 PyObject *tmp;
175 objfile_object *self = (objfile_object *) o;
176
177 if (! filters)
178 {
179 PyErr_SetString (PyExc_TypeError,
180 _("Cannot delete the frame filters attribute."));
181 return -1;
182 }
183
184 if (! PyDict_Check (filters))
185 {
186 PyErr_SetString (PyExc_TypeError,
187 _("The frame_filters attribute must be a dictionary."));
188 return -1;
189 }
190
191 /* Take care in case the LHS and RHS are related somehow. */
192 tmp = self->frame_filters;
193 Py_INCREF (filters);
194 self->frame_filters = filters;
195 Py_XDECREF (tmp);
196
197 return 0;
198 }
199
200 /* Get the 'type_printers' attribute. */
201
202 static PyObject *
203 objfpy_get_type_printers (PyObject *o, void *ignore)
204 {
205 objfile_object *self = (objfile_object *) o;
206
207 Py_INCREF (self->type_printers);
208 return self->type_printers;
209 }
210
211 /* Get the 'xmethods' attribute. */
212
213 PyObject *
214 objfpy_get_xmethods (PyObject *o, void *ignore)
215 {
216 objfile_object *self = (objfile_object *) o;
217
218 Py_INCREF (self->xmethods);
219 return self->xmethods;
220 }
221
222 /* Set the 'type_printers' attribute. */
223
224 static int
225 objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
226 {
227 PyObject *tmp;
228 objfile_object *self = (objfile_object *) o;
229
230 if (! value)
231 {
232 PyErr_SetString (PyExc_TypeError,
233 _("Cannot delete the type_printers attribute."));
234 return -1;
235 }
236
237 if (! PyList_Check (value))
238 {
239 PyErr_SetString (PyExc_TypeError,
240 _("The type_printers attribute must be a list."));
241 return -1;
242 }
243
244 /* Take care in case the LHS and RHS are related somehow. */
245 tmp = self->type_printers;
246 Py_INCREF (value);
247 self->type_printers = value;
248 Py_XDECREF (tmp);
249
250 return 0;
251 }
252
253 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
254 Returns True if this object file still exists in GDB. */
255
256 static PyObject *
257 objfpy_is_valid (PyObject *self, PyObject *args)
258 {
259 objfile_object *obj = (objfile_object *) self;
260
261 if (! obj->objfile)
262 Py_RETURN_FALSE;
263
264 Py_RETURN_TRUE;
265 }
266
267 \f
268
269 /* Clear the OBJFILE pointer in an Objfile object and remove the
270 reference. */
271 static void
272 py_free_objfile (struct objfile *objfile, void *datum)
273 {
274 struct cleanup *cleanup;
275 objfile_object *object = datum;
276
277 cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
278 object->objfile = NULL;
279 Py_DECREF ((PyObject *) object);
280 do_cleanups (cleanup);
281 }
282
283 /* Return a borrowed reference to the Python object of type Objfile
284 representing OBJFILE. If the object has already been created,
285 return it. Otherwise, create it. Return NULL and set the Python
286 error on failure. */
287
288 PyObject *
289 objfile_to_objfile_object (struct objfile *objfile)
290 {
291 objfile_object *object;
292
293 object = objfile_data (objfile, objfpy_objfile_data_key);
294 if (!object)
295 {
296 object = PyObject_New (objfile_object, &objfile_object_type);
297 if (object)
298 {
299 if (!objfpy_initialize (object))
300 {
301 Py_DECREF (object);
302 return NULL;
303 }
304
305 object->objfile = objfile;
306 set_objfile_data (objfile, objfpy_objfile_data_key, object);
307 }
308 }
309
310 return (PyObject *) object;
311 }
312
313 int
314 gdbpy_initialize_objfile (void)
315 {
316 objfpy_objfile_data_key
317 = register_objfile_data_with_cleanup (NULL, py_free_objfile);
318
319 if (PyType_Ready (&objfile_object_type) < 0)
320 return -1;
321
322 return gdb_pymodule_addobject (gdb_module, "Objfile",
323 (PyObject *) &objfile_object_type);
324 }
325
326 \f
327
328 static PyMethodDef objfile_object_methods[] =
329 {
330 { "is_valid", objfpy_is_valid, METH_NOARGS,
331 "is_valid () -> Boolean.\n\
332 Return true if this object file is valid, false if not." },
333
334 { NULL }
335 };
336
337 static PyGetSetDef objfile_getset[] =
338 {
339 { "filename", objfpy_get_filename, NULL,
340 "The objfile's filename, or None.", NULL },
341 { "pretty_printers", objfpy_get_printers, objfpy_set_printers,
342 "Pretty printers.", NULL },
343 { "frame_filters", objfpy_get_frame_filters,
344 objfpy_set_frame_filters, "Frame Filters.", NULL },
345 { "type_printers", objfpy_get_type_printers, objfpy_set_type_printers,
346 "Type printers.", NULL },
347 { "xmethods", objfpy_get_xmethods, NULL,
348 "Debug methods.", NULL },
349 { NULL }
350 };
351
352 static PyTypeObject objfile_object_type =
353 {
354 PyVarObject_HEAD_INIT (NULL, 0)
355 "gdb.Objfile", /*tp_name*/
356 sizeof (objfile_object), /*tp_basicsize*/
357 0, /*tp_itemsize*/
358 objfpy_dealloc, /*tp_dealloc*/
359 0, /*tp_print*/
360 0, /*tp_getattr*/
361 0, /*tp_setattr*/
362 0, /*tp_compare*/
363 0, /*tp_repr*/
364 0, /*tp_as_number*/
365 0, /*tp_as_sequence*/
366 0, /*tp_as_mapping*/
367 0, /*tp_hash */
368 0, /*tp_call*/
369 0, /*tp_str*/
370 0, /*tp_getattro*/
371 0, /*tp_setattro*/
372 0, /*tp_as_buffer*/
373 Py_TPFLAGS_DEFAULT, /*tp_flags*/
374 "GDB objfile object", /* tp_doc */
375 0, /* tp_traverse */
376 0, /* tp_clear */
377 0, /* tp_richcompare */
378 0, /* tp_weaklistoffset */
379 0, /* tp_iter */
380 0, /* tp_iternext */
381 objfile_object_methods, /* tp_methods */
382 0, /* tp_members */
383 objfile_getset, /* tp_getset */
384 0, /* tp_base */
385 0, /* tp_dict */
386 0, /* tp_descr_get */
387 0, /* tp_descr_set */
388 0, /* tp_dictoffset */
389 0, /* tp_init */
390 0, /* tp_alloc */
391 objfpy_new, /* tp_new */
392 };