Commit | Line | Data |
---|---|---|
89c73ade TT |
1 | /* Python interface to objfiles. |
2 | ||
28e7fd62 | 3 | Copyright (C) 2008-2013 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 | |
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; | |
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 |
42 | static PyTypeObject objfile_object_type |
43 | CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("objfile_object"); | |
89c73ade TT |
44 | |
45 | static 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. */ | |
50 | static PyObject * | |
51 | objfpy_get_filename (PyObject *self, void *closure) | |
52 | { | |
53 | objfile_object *obj = (objfile_object *) self; | |
d59b6f6c | 54 | |
d31d2fc3 | 55 | if (obj->objfile) |
89c73ade TT |
56 | return PyString_Decode (obj->objfile->name, strlen (obj->objfile->name), |
57 | host_charset (), NULL); | |
58 | Py_RETURN_NONE; | |
59 | } | |
60 | ||
61 | static void | |
62 | objfpy_dealloc (PyObject *o) | |
63 | { | |
64 | objfile_object *self = (objfile_object *) o; | |
d59b6f6c | 65 | |
89c73ade | 66 | Py_XDECREF (self->printers); |
1e611234 | 67 | Py_XDECREF (self->frame_filters); |
18a9fc12 | 68 | Py_XDECREF (self->type_printers); |
9a27f2c6 | 69 | Py_TYPE (self)->tp_free (self); |
89c73ade TT |
70 | } |
71 | ||
72 | static PyObject * | |
73 | objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords) | |
74 | { | |
75 | objfile_object *self = (objfile_object *) type->tp_alloc (type, 0); | |
d59b6f6c | 76 | |
89c73ade TT |
77 | if (self) |
78 | { | |
79 | self->objfile = NULL; | |
80 | ||
81 | self->printers = PyList_New (0); | |
82 | if (!self->printers) | |
83 | { | |
84 | Py_DECREF (self); | |
85 | return NULL; | |
86 | } | |
18a9fc12 | 87 | |
1e611234 PM |
88 | self->frame_filters = PyDict_New (); |
89 | if (!self->frame_filters) | |
90 | { | |
91 | Py_DECREF (self); | |
92 | return NULL; | |
93 | } | |
94 | ||
18a9fc12 TT |
95 | self->type_printers = PyList_New (0); |
96 | if (!self->type_printers) | |
97 | { | |
98 | Py_DECREF (self); | |
99 | return NULL; | |
100 | } | |
89c73ade TT |
101 | } |
102 | return (PyObject *) self; | |
103 | } | |
104 | ||
105 | PyObject * | |
106 | objfpy_get_printers (PyObject *o, void *ignore) | |
107 | { | |
108 | objfile_object *self = (objfile_object *) o; | |
d59b6f6c | 109 | |
89c73ade TT |
110 | Py_INCREF (self->printers); |
111 | return self->printers; | |
112 | } | |
113 | ||
114 | static int | |
115 | objfpy_set_printers (PyObject *o, PyObject *value, void *ignore) | |
116 | { | |
117 | PyObject *tmp; | |
118 | objfile_object *self = (objfile_object *) o; | |
d59b6f6c | 119 | |
89c73ade TT |
120 | if (! value) |
121 | { | |
122 | PyErr_SetString (PyExc_TypeError, | |
044c0f87 | 123 | _("Cannot delete the pretty_printers attribute.")); |
89c73ade TT |
124 | return -1; |
125 | } | |
126 | ||
127 | if (! PyList_Check (value)) | |
128 | { | |
129 | PyErr_SetString (PyExc_TypeError, | |
044c0f87 | 130 | _("The pretty_printers attribute must be a list.")); |
89c73ade TT |
131 | return -1; |
132 | } | |
133 | ||
134 | /* Take care in case the LHS and RHS are related somehow. */ | |
135 | tmp = self->printers; | |
136 | Py_INCREF (value); | |
137 | self->printers = value; | |
138 | Py_XDECREF (tmp); | |
139 | ||
140 | return 0; | |
141 | } | |
142 | ||
1e611234 PM |
143 | /* Return the Python dictionary attribute containing frame filters for |
144 | this object file. */ | |
145 | PyObject * | |
146 | objfpy_get_frame_filters (PyObject *o, void *ignore) | |
147 | { | |
148 | objfile_object *self = (objfile_object *) o; | |
149 | ||
150 | Py_INCREF (self->frame_filters); | |
151 | return self->frame_filters; | |
152 | } | |
153 | ||
154 | /* Set this object file's frame filters dictionary to FILTERS. */ | |
155 | static int | |
156 | objfpy_set_frame_filters (PyObject *o, PyObject *filters, void *ignore) | |
157 | { | |
158 | PyObject *tmp; | |
159 | objfile_object *self = (objfile_object *) o; | |
160 | ||
161 | if (! filters) | |
162 | { | |
163 | PyErr_SetString (PyExc_TypeError, | |
164 | _("Cannot delete the frame filters attribute.")); | |
165 | return -1; | |
166 | } | |
167 | ||
168 | if (! PyDict_Check (filters)) | |
169 | { | |
170 | PyErr_SetString (PyExc_TypeError, | |
171 | _("The frame_filters attribute must be a dictionary.")); | |
172 | return -1; | |
173 | } | |
174 | ||
175 | /* Take care in case the LHS and RHS are related somehow. */ | |
176 | tmp = self->frame_filters; | |
177 | Py_INCREF (filters); | |
178 | self->frame_filters = filters; | |
179 | Py_XDECREF (tmp); | |
180 | ||
181 | return 0; | |
182 | } | |
183 | ||
18a9fc12 TT |
184 | /* Get the 'type_printers' attribute. */ |
185 | ||
186 | static PyObject * | |
187 | objfpy_get_type_printers (PyObject *o, void *ignore) | |
188 | { | |
189 | objfile_object *self = (objfile_object *) o; | |
190 | ||
191 | Py_INCREF (self->type_printers); | |
192 | return self->type_printers; | |
193 | } | |
194 | ||
195 | /* Set the 'type_printers' attribute. */ | |
196 | ||
197 | static int | |
198 | objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore) | |
199 | { | |
200 | PyObject *tmp; | |
201 | objfile_object *self = (objfile_object *) o; | |
202 | ||
203 | if (! value) | |
204 | { | |
205 | PyErr_SetString (PyExc_TypeError, | |
206 | _("Cannot delete the type_printers attribute.")); | |
207 | return -1; | |
208 | } | |
209 | ||
210 | if (! PyList_Check (value)) | |
211 | { | |
212 | PyErr_SetString (PyExc_TypeError, | |
213 | _("The type_printers attribute must be a list.")); | |
214 | return -1; | |
215 | } | |
216 | ||
217 | /* Take care in case the LHS and RHS are related somehow. */ | |
218 | tmp = self->type_printers; | |
219 | Py_INCREF (value); | |
220 | self->type_printers = value; | |
221 | Py_XDECREF (tmp); | |
222 | ||
223 | return 0; | |
224 | } | |
225 | ||
29703da4 PM |
226 | /* Implementation of gdb.Objfile.is_valid (self) -> Boolean. |
227 | Returns True if this object file still exists in GDB. */ | |
228 | ||
229 | static PyObject * | |
230 | objfpy_is_valid (PyObject *self, PyObject *args) | |
231 | { | |
232 | objfile_object *obj = (objfile_object *) self; | |
233 | ||
234 | if (! obj->objfile) | |
235 | Py_RETURN_FALSE; | |
236 | ||
237 | Py_RETURN_TRUE; | |
238 | } | |
239 | ||
89c73ade TT |
240 | \f |
241 | ||
242 | /* Clear the OBJFILE pointer in an Objfile object and remove the | |
243 | reference. */ | |
244 | static void | |
c1bd65d0 | 245 | py_free_objfile (struct objfile *objfile, void *datum) |
89c73ade | 246 | { |
d452c4bc | 247 | struct cleanup *cleanup; |
89c73ade TT |
248 | objfile_object *object = datum; |
249 | ||
d452c4bc | 250 | cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); |
89c73ade TT |
251 | object->objfile = NULL; |
252 | Py_DECREF ((PyObject *) object); | |
d452c4bc | 253 | do_cleanups (cleanup); |
89c73ade TT |
254 | } |
255 | ||
256 | /* Return a borrowed reference to the Python object of type Objfile | |
257 | representing OBJFILE. If the object has already been created, | |
258 | return it. Otherwise, create it. Return NULL and set the Python | |
259 | error on failure. */ | |
260 | PyObject * | |
261 | objfile_to_objfile_object (struct objfile *objfile) | |
262 | { | |
263 | objfile_object *object; | |
264 | ||
265 | object = objfile_data (objfile, objfpy_objfile_data_key); | |
266 | if (!object) | |
267 | { | |
268 | object = PyObject_New (objfile_object, &objfile_object_type); | |
269 | if (object) | |
270 | { | |
89c73ade TT |
271 | object->objfile = objfile; |
272 | ||
273 | object->printers = PyList_New (0); | |
274 | if (!object->printers) | |
275 | { | |
276 | Py_DECREF (object); | |
277 | return NULL; | |
278 | } | |
279 | ||
1e611234 PM |
280 | object->frame_filters = PyDict_New (); |
281 | if (!object->frame_filters) | |
282 | { | |
283 | Py_DECREF (object); | |
284 | return NULL; | |
285 | } | |
286 | ||
18a9fc12 TT |
287 | object->type_printers = PyList_New (0); |
288 | if (!object->type_printers) | |
289 | { | |
290 | Py_DECREF (object); | |
291 | return NULL; | |
292 | } | |
293 | ||
89c73ade TT |
294 | set_objfile_data (objfile, objfpy_objfile_data_key, object); |
295 | } | |
296 | } | |
297 | ||
298 | return (PyObject *) object; | |
299 | } | |
300 | ||
999633ed | 301 | int |
89c73ade TT |
302 | gdbpy_initialize_objfile (void) |
303 | { | |
304 | objfpy_objfile_data_key | |
c1bd65d0 | 305 | = register_objfile_data_with_cleanup (NULL, py_free_objfile); |
89c73ade TT |
306 | |
307 | if (PyType_Ready (&objfile_object_type) < 0) | |
999633ed | 308 | return -1; |
89c73ade | 309 | |
aa36459a TT |
310 | return gdb_pymodule_addobject (gdb_module, "Objfile", |
311 | (PyObject *) &objfile_object_type); | |
89c73ade TT |
312 | } |
313 | ||
314 | \f | |
315 | ||
29703da4 PM |
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 | ||
89c73ade TT |
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 }, | |
1e611234 PM |
331 | { "frame_filters", objfpy_get_frame_filters, |
332 | objfpy_set_frame_filters, "Frame Filters.", NULL }, | |
18a9fc12 TT |
333 | { "type_printers", objfpy_get_type_printers, objfpy_set_type_printers, |
334 | "Type printers.", NULL }, | |
89c73ade TT |
335 | { NULL } |
336 | }; | |
337 | ||
338 | static PyTypeObject objfile_object_type = | |
339 | { | |
9a27f2c6 | 340 | PyVarObject_HEAD_INIT (NULL, 0) |
89c73ade TT |
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 */ | |
29703da4 | 367 | objfile_object_methods, /* tp_methods */ |
89c73ade TT |
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 | }; |