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