Commit | Line | Data |
---|---|---|
89c73ade TT |
1 | /* Python interface to objfiles. |
2 | ||
0b302171 | 3 | Copyright (C) 2008-2012 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 TT |
35 | |
36 | /* The type-printer list. */ | |
37 | PyObject *type_printers; | |
89c73ade TT |
38 | } objfile_object; |
39 | ||
40 | static PyTypeObject objfile_object_type; | |
41 | ||
42 | static const struct objfile_data *objfpy_objfile_data_key; | |
43 | ||
44 | \f | |
45 | ||
46 | /* An Objfile method which returns the objfile's file name, or None. */ | |
47 | static PyObject * | |
48 | objfpy_get_filename (PyObject *self, void *closure) | |
49 | { | |
50 | objfile_object *obj = (objfile_object *) self; | |
d59b6f6c | 51 | |
d31d2fc3 | 52 | if (obj->objfile) |
89c73ade TT |
53 | return PyString_Decode (obj->objfile->name, strlen (obj->objfile->name), |
54 | host_charset (), NULL); | |
55 | Py_RETURN_NONE; | |
56 | } | |
57 | ||
58 | static void | |
59 | objfpy_dealloc (PyObject *o) | |
60 | { | |
61 | objfile_object *self = (objfile_object *) o; | |
d59b6f6c | 62 | |
89c73ade | 63 | Py_XDECREF (self->printers); |
18a9fc12 | 64 | Py_XDECREF (self->type_printers); |
89c73ade TT |
65 | self->ob_type->tp_free ((PyObject *) self); |
66 | } | |
67 | ||
68 | static PyObject * | |
69 | objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords) | |
70 | { | |
71 | objfile_object *self = (objfile_object *) type->tp_alloc (type, 0); | |
d59b6f6c | 72 | |
89c73ade TT |
73 | if (self) |
74 | { | |
75 | self->objfile = NULL; | |
76 | ||
77 | self->printers = PyList_New (0); | |
78 | if (!self->printers) | |
79 | { | |
80 | Py_DECREF (self); | |
81 | return NULL; | |
82 | } | |
18a9fc12 TT |
83 | |
84 | self->type_printers = PyList_New (0); | |
85 | if (!self->type_printers) | |
86 | { | |
87 | Py_DECREF (self); | |
88 | return NULL; | |
89 | } | |
89c73ade TT |
90 | } |
91 | return (PyObject *) self; | |
92 | } | |
93 | ||
94 | PyObject * | |
95 | objfpy_get_printers (PyObject *o, void *ignore) | |
96 | { | |
97 | objfile_object *self = (objfile_object *) o; | |
d59b6f6c | 98 | |
89c73ade TT |
99 | Py_INCREF (self->printers); |
100 | return self->printers; | |
101 | } | |
102 | ||
103 | static int | |
104 | objfpy_set_printers (PyObject *o, PyObject *value, void *ignore) | |
105 | { | |
106 | PyObject *tmp; | |
107 | objfile_object *self = (objfile_object *) o; | |
d59b6f6c | 108 | |
89c73ade TT |
109 | if (! value) |
110 | { | |
111 | PyErr_SetString (PyExc_TypeError, | |
044c0f87 | 112 | _("Cannot delete the pretty_printers attribute.")); |
89c73ade TT |
113 | return -1; |
114 | } | |
115 | ||
116 | if (! PyList_Check (value)) | |
117 | { | |
118 | PyErr_SetString (PyExc_TypeError, | |
044c0f87 | 119 | _("The pretty_printers attribute must be a list.")); |
89c73ade TT |
120 | return -1; |
121 | } | |
122 | ||
123 | /* Take care in case the LHS and RHS are related somehow. */ | |
124 | tmp = self->printers; | |
125 | Py_INCREF (value); | |
126 | self->printers = value; | |
127 | Py_XDECREF (tmp); | |
128 | ||
129 | return 0; | |
130 | } | |
131 | ||
18a9fc12 TT |
132 | /* Get the 'type_printers' attribute. */ |
133 | ||
134 | static PyObject * | |
135 | objfpy_get_type_printers (PyObject *o, void *ignore) | |
136 | { | |
137 | objfile_object *self = (objfile_object *) o; | |
138 | ||
139 | Py_INCREF (self->type_printers); | |
140 | return self->type_printers; | |
141 | } | |
142 | ||
143 | /* Set the 'type_printers' attribute. */ | |
144 | ||
145 | static int | |
146 | objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore) | |
147 | { | |
148 | PyObject *tmp; | |
149 | objfile_object *self = (objfile_object *) o; | |
150 | ||
151 | if (! value) | |
152 | { | |
153 | PyErr_SetString (PyExc_TypeError, | |
154 | _("Cannot delete the type_printers attribute.")); | |
155 | return -1; | |
156 | } | |
157 | ||
158 | if (! PyList_Check (value)) | |
159 | { | |
160 | PyErr_SetString (PyExc_TypeError, | |
161 | _("The type_printers attribute must be a list.")); | |
162 | return -1; | |
163 | } | |
164 | ||
165 | /* Take care in case the LHS and RHS are related somehow. */ | |
166 | tmp = self->type_printers; | |
167 | Py_INCREF (value); | |
168 | self->type_printers = value; | |
169 | Py_XDECREF (tmp); | |
170 | ||
171 | return 0; | |
172 | } | |
173 | ||
29703da4 PM |
174 | /* Implementation of gdb.Objfile.is_valid (self) -> Boolean. |
175 | Returns True if this object file still exists in GDB. */ | |
176 | ||
177 | static PyObject * | |
178 | objfpy_is_valid (PyObject *self, PyObject *args) | |
179 | { | |
180 | objfile_object *obj = (objfile_object *) self; | |
181 | ||
182 | if (! obj->objfile) | |
183 | Py_RETURN_FALSE; | |
184 | ||
185 | Py_RETURN_TRUE; | |
186 | } | |
187 | ||
89c73ade TT |
188 | \f |
189 | ||
190 | /* Clear the OBJFILE pointer in an Objfile object and remove the | |
191 | reference. */ | |
192 | static void | |
c1bd65d0 | 193 | py_free_objfile (struct objfile *objfile, void *datum) |
89c73ade | 194 | { |
d452c4bc | 195 | struct cleanup *cleanup; |
89c73ade TT |
196 | objfile_object *object = datum; |
197 | ||
d452c4bc | 198 | cleanup = ensure_python_env (get_objfile_arch (objfile), current_language); |
89c73ade TT |
199 | object->objfile = NULL; |
200 | Py_DECREF ((PyObject *) object); | |
d452c4bc | 201 | do_cleanups (cleanup); |
89c73ade TT |
202 | } |
203 | ||
204 | /* Return a borrowed reference to the Python object of type Objfile | |
205 | representing OBJFILE. If the object has already been created, | |
206 | return it. Otherwise, create it. Return NULL and set the Python | |
207 | error on failure. */ | |
208 | PyObject * | |
209 | objfile_to_objfile_object (struct objfile *objfile) | |
210 | { | |
211 | objfile_object *object; | |
212 | ||
213 | object = objfile_data (objfile, objfpy_objfile_data_key); | |
214 | if (!object) | |
215 | { | |
216 | object = PyObject_New (objfile_object, &objfile_object_type); | |
217 | if (object) | |
218 | { | |
89c73ade TT |
219 | object->objfile = objfile; |
220 | ||
221 | object->printers = PyList_New (0); | |
222 | if (!object->printers) | |
223 | { | |
224 | Py_DECREF (object); | |
225 | return NULL; | |
226 | } | |
227 | ||
18a9fc12 TT |
228 | object->type_printers = PyList_New (0); |
229 | if (!object->type_printers) | |
230 | { | |
231 | Py_DECREF (object); | |
232 | return NULL; | |
233 | } | |
234 | ||
89c73ade TT |
235 | set_objfile_data (objfile, objfpy_objfile_data_key, object); |
236 | } | |
237 | } | |
238 | ||
239 | return (PyObject *) object; | |
240 | } | |
241 | ||
242 | void | |
243 | gdbpy_initialize_objfile (void) | |
244 | { | |
245 | objfpy_objfile_data_key | |
c1bd65d0 | 246 | = register_objfile_data_with_cleanup (NULL, py_free_objfile); |
89c73ade TT |
247 | |
248 | if (PyType_Ready (&objfile_object_type) < 0) | |
249 | return; | |
250 | ||
251 | Py_INCREF (&objfile_object_type); | |
9a2b4c1b MS |
252 | PyModule_AddObject (gdb_module, "Objfile", |
253 | (PyObject *) &objfile_object_type); | |
89c73ade TT |
254 | } |
255 | ||
256 | \f | |
257 | ||
29703da4 PM |
258 | static PyMethodDef objfile_object_methods[] = |
259 | { | |
260 | { "is_valid", objfpy_is_valid, METH_NOARGS, | |
261 | "is_valid () -> Boolean.\n\ | |
262 | Return true if this object file is valid, false if not." }, | |
263 | ||
264 | { NULL } | |
265 | }; | |
266 | ||
89c73ade TT |
267 | static PyGetSetDef objfile_getset[] = |
268 | { | |
269 | { "filename", objfpy_get_filename, NULL, | |
270 | "The objfile's filename, or None.", NULL }, | |
271 | { "pretty_printers", objfpy_get_printers, objfpy_set_printers, | |
272 | "Pretty printers.", NULL }, | |
18a9fc12 TT |
273 | { "type_printers", objfpy_get_type_printers, objfpy_set_type_printers, |
274 | "Type printers.", NULL }, | |
89c73ade TT |
275 | { NULL } |
276 | }; | |
277 | ||
278 | static PyTypeObject objfile_object_type = | |
279 | { | |
280 | PyObject_HEAD_INIT (NULL) | |
281 | 0, /*ob_size*/ | |
282 | "gdb.Objfile", /*tp_name*/ | |
283 | sizeof (objfile_object), /*tp_basicsize*/ | |
284 | 0, /*tp_itemsize*/ | |
285 | objfpy_dealloc, /*tp_dealloc*/ | |
286 | 0, /*tp_print*/ | |
287 | 0, /*tp_getattr*/ | |
288 | 0, /*tp_setattr*/ | |
289 | 0, /*tp_compare*/ | |
290 | 0, /*tp_repr*/ | |
291 | 0, /*tp_as_number*/ | |
292 | 0, /*tp_as_sequence*/ | |
293 | 0, /*tp_as_mapping*/ | |
294 | 0, /*tp_hash */ | |
295 | 0, /*tp_call*/ | |
296 | 0, /*tp_str*/ | |
297 | 0, /*tp_getattro*/ | |
298 | 0, /*tp_setattro*/ | |
299 | 0, /*tp_as_buffer*/ | |
300 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ | |
301 | "GDB objfile object", /* tp_doc */ | |
302 | 0, /* tp_traverse */ | |
303 | 0, /* tp_clear */ | |
304 | 0, /* tp_richcompare */ | |
305 | 0, /* tp_weaklistoffset */ | |
306 | 0, /* tp_iter */ | |
307 | 0, /* tp_iternext */ | |
29703da4 | 308 | objfile_object_methods, /* tp_methods */ |
89c73ade TT |
309 | 0, /* tp_members */ |
310 | objfile_getset, /* tp_getset */ | |
311 | 0, /* tp_base */ | |
312 | 0, /* tp_dict */ | |
313 | 0, /* tp_descr_get */ | |
314 | 0, /* tp_descr_set */ | |
315 | 0, /* tp_dictoffset */ | |
316 | 0, /* tp_init */ | |
317 | 0, /* tp_alloc */ | |
318 | objfpy_new, /* tp_new */ | |
319 | }; |