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