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