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