Commit | Line | Data |
---|---|---|
fa33c3cd DE |
1 | /* Python interface to program spaces. |
2 | ||
61baf725 | 3 | Copyright (C) 2010-2017 Free Software Foundation, Inc. |
fa33c3cd DE |
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 "progspace.h" | |
24 | #include "objfiles.h" | |
25 | #include "language.h" | |
b3422a0d | 26 | #include "arch-utils.h" |
88b6faea | 27 | #include "py-ref.h" |
fa33c3cd DE |
28 | |
29 | typedef struct | |
30 | { | |
31 | PyObject_HEAD | |
32 | ||
33 | /* The corresponding pspace. */ | |
34 | struct program_space *pspace; | |
35 | ||
02be9a71 DE |
36 | /* Dictionary holding user-added attributes. |
37 | This is the __dict__ attribute of the object. */ | |
38 | PyObject *dict; | |
39 | ||
fa33c3cd DE |
40 | /* The pretty-printer list of functions. */ |
41 | PyObject *printers; | |
18a9fc12 | 42 | |
1e611234 PM |
43 | /* The frame filter list of functions. */ |
44 | PyObject *frame_filters; | |
d11916aa SS |
45 | |
46 | /* The frame unwinder list. */ | |
47 | PyObject *frame_unwinders; | |
48 | ||
18a9fc12 TT |
49 | /* The type-printer list. */ |
50 | PyObject *type_printers; | |
883964a7 SC |
51 | |
52 | /* The debug method list. */ | |
53 | PyObject *xmethods; | |
fa33c3cd DE |
54 | } pspace_object; |
55 | ||
e36122e9 | 56 | extern PyTypeObject pspace_object_type |
62eec1a5 | 57 | CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object"); |
fa33c3cd DE |
58 | |
59 | static const struct program_space_data *pspy_pspace_data_key; | |
60 | ||
61 | \f | |
62 | ||
63 | /* An Objfile method which returns the objfile's file name, or None. */ | |
64 | ||
65 | static PyObject * | |
66 | pspy_get_filename (PyObject *self, void *closure) | |
67 | { | |
68 | pspace_object *obj = (pspace_object *) self; | |
d59b6f6c | 69 | |
fa33c3cd DE |
70 | if (obj->pspace) |
71 | { | |
72 | struct objfile *objfile = obj->pspace->symfile_object_file; | |
d59b6f6c | 73 | |
d31d2fc3 | 74 | if (objfile) |
4ae6cc19 | 75 | return host_string_to_python_string (objfile_name (objfile)); |
fa33c3cd DE |
76 | } |
77 | Py_RETURN_NONE; | |
78 | } | |
79 | ||
80 | static void | |
81 | pspy_dealloc (PyObject *self) | |
82 | { | |
83 | pspace_object *ps_self = (pspace_object *) self; | |
d59b6f6c | 84 | |
02be9a71 | 85 | Py_XDECREF (ps_self->dict); |
fa33c3cd | 86 | Py_XDECREF (ps_self->printers); |
1e611234 | 87 | Py_XDECREF (ps_self->frame_filters); |
d11916aa | 88 | Py_XDECREF (ps_self->frame_unwinders); |
18a9fc12 | 89 | Py_XDECREF (ps_self->type_printers); |
883964a7 | 90 | Py_XDECREF (ps_self->xmethods); |
9a27f2c6 | 91 | Py_TYPE (self)->tp_free (self); |
fa33c3cd DE |
92 | } |
93 | ||
4e1bbde0 DE |
94 | /* Initialize a pspace_object. |
95 | The result is a boolean indicating success. */ | |
96 | ||
97 | static int | |
98 | pspy_initialize (pspace_object *self) | |
99 | { | |
100 | self->pspace = NULL; | |
0f6ed0e0 TT |
101 | |
102 | self->dict = PyDict_New (); | |
103 | if (self->dict == NULL) | |
104 | return 0; | |
4e1bbde0 DE |
105 | |
106 | self->printers = PyList_New (0); | |
107 | if (self->printers == NULL) | |
108 | return 0; | |
109 | ||
110 | self->frame_filters = PyDict_New (); | |
111 | if (self->frame_filters == NULL) | |
112 | return 0; | |
113 | ||
d11916aa SS |
114 | self->frame_unwinders = PyList_New (0); |
115 | if (self->frame_unwinders == NULL) | |
116 | return 0; | |
117 | ||
4e1bbde0 DE |
118 | self->type_printers = PyList_New (0); |
119 | if (self->type_printers == NULL) | |
120 | return 0; | |
121 | ||
122 | self->xmethods = PyList_New (0); | |
123 | if (self->xmethods == NULL) | |
124 | return 0; | |
125 | ||
126 | return 1; | |
127 | } | |
128 | ||
fa33c3cd DE |
129 | static PyObject * |
130 | pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords) | |
131 | { | |
88b6faea | 132 | gdbpy_ref<pspace_object> self ((pspace_object *) type->tp_alloc (type, 0)); |
d59b6f6c | 133 | |
88b6faea | 134 | if (self != NULL) |
fa33c3cd | 135 | { |
88b6faea TT |
136 | if (!pspy_initialize (self.get ())) |
137 | return NULL; | |
fa33c3cd | 138 | } |
4e1bbde0 | 139 | |
88b6faea | 140 | return (PyObject *) self.release (); |
fa33c3cd DE |
141 | } |
142 | ||
143 | PyObject * | |
144 | pspy_get_printers (PyObject *o, void *ignore) | |
145 | { | |
146 | pspace_object *self = (pspace_object *) o; | |
d59b6f6c | 147 | |
fa33c3cd DE |
148 | Py_INCREF (self->printers); |
149 | return self->printers; | |
150 | } | |
151 | ||
152 | static int | |
153 | pspy_set_printers (PyObject *o, PyObject *value, void *ignore) | |
154 | { | |
155 | PyObject *tmp; | |
156 | pspace_object *self = (pspace_object *) o; | |
d59b6f6c | 157 | |
fa33c3cd DE |
158 | if (! value) |
159 | { | |
160 | PyErr_SetString (PyExc_TypeError, | |
161 | "cannot delete the pretty_printers attribute"); | |
162 | return -1; | |
163 | } | |
164 | ||
165 | if (! PyList_Check (value)) | |
166 | { | |
167 | PyErr_SetString (PyExc_TypeError, | |
168 | "the pretty_printers attribute must be a list"); | |
169 | return -1; | |
170 | } | |
171 | ||
172 | /* Take care in case the LHS and RHS are related somehow. */ | |
173 | tmp = self->printers; | |
174 | Py_INCREF (value); | |
175 | self->printers = value; | |
176 | Py_XDECREF (tmp); | |
177 | ||
178 | return 0; | |
179 | } | |
180 | ||
1e611234 PM |
181 | /* Return the Python dictionary attribute containing frame filters for |
182 | this program space. */ | |
183 | PyObject * | |
184 | pspy_get_frame_filters (PyObject *o, void *ignore) | |
185 | { | |
186 | pspace_object *self = (pspace_object *) o; | |
187 | ||
188 | Py_INCREF (self->frame_filters); | |
189 | return self->frame_filters; | |
190 | } | |
191 | ||
192 | /* Set this object file's frame filters dictionary to FILTERS. */ | |
193 | static int | |
194 | pspy_set_frame_filters (PyObject *o, PyObject *frame, void *ignore) | |
195 | { | |
196 | PyObject *tmp; | |
197 | pspace_object *self = (pspace_object *) o; | |
198 | ||
199 | if (! frame) | |
200 | { | |
201 | PyErr_SetString (PyExc_TypeError, | |
202 | "cannot delete the frame filter attribute"); | |
203 | return -1; | |
204 | } | |
205 | ||
206 | if (! PyDict_Check (frame)) | |
207 | { | |
208 | PyErr_SetString (PyExc_TypeError, | |
209 | "the frame filter attribute must be a dictionary"); | |
210 | return -1; | |
211 | } | |
212 | ||
213 | /* Take care in case the LHS and RHS are related somehow. */ | |
214 | tmp = self->frame_filters; | |
215 | Py_INCREF (frame); | |
216 | self->frame_filters = frame; | |
217 | Py_XDECREF (tmp); | |
218 | ||
219 | return 0; | |
220 | } | |
221 | ||
d11916aa SS |
222 | /* Return the list of the frame unwinders for this program space. */ |
223 | ||
224 | PyObject * | |
225 | pspy_get_frame_unwinders (PyObject *o, void *ignore) | |
226 | { | |
227 | pspace_object *self = (pspace_object *) o; | |
228 | ||
229 | Py_INCREF (self->frame_unwinders); | |
230 | return self->frame_unwinders; | |
231 | } | |
232 | ||
233 | /* Set this program space's list of the unwinders to UNWINDERS. */ | |
234 | ||
235 | static int | |
236 | pspy_set_frame_unwinders (PyObject *o, PyObject *unwinders, void *ignore) | |
237 | { | |
238 | PyObject *tmp; | |
239 | pspace_object *self = (pspace_object *) o; | |
240 | ||
241 | if (!unwinders) | |
242 | { | |
243 | PyErr_SetString (PyExc_TypeError, | |
244 | "cannot delete the frame unwinders list"); | |
245 | return -1; | |
246 | } | |
247 | ||
248 | if (!PyList_Check (unwinders)) | |
249 | { | |
250 | PyErr_SetString (PyExc_TypeError, | |
251 | "the frame unwinders attribute must be a list"); | |
252 | return -1; | |
253 | } | |
254 | ||
255 | /* Take care in case the LHS and RHS are related somehow. */ | |
256 | tmp = self->frame_unwinders; | |
257 | Py_INCREF (unwinders); | |
258 | self->frame_unwinders = unwinders; | |
259 | Py_XDECREF (tmp); | |
260 | ||
261 | return 0; | |
262 | } | |
263 | ||
18a9fc12 TT |
264 | /* Get the 'type_printers' attribute. */ |
265 | ||
266 | static PyObject * | |
267 | pspy_get_type_printers (PyObject *o, void *ignore) | |
268 | { | |
269 | pspace_object *self = (pspace_object *) o; | |
270 | ||
271 | Py_INCREF (self->type_printers); | |
272 | return self->type_printers; | |
273 | } | |
274 | ||
883964a7 SC |
275 | /* Get the 'xmethods' attribute. */ |
276 | ||
277 | PyObject * | |
278 | pspy_get_xmethods (PyObject *o, void *ignore) | |
279 | { | |
280 | pspace_object *self = (pspace_object *) o; | |
281 | ||
282 | Py_INCREF (self->xmethods); | |
283 | return self->xmethods; | |
284 | } | |
285 | ||
18a9fc12 TT |
286 | /* Set the 'type_printers' attribute. */ |
287 | ||
288 | static int | |
289 | pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore) | |
290 | { | |
291 | PyObject *tmp; | |
292 | pspace_object *self = (pspace_object *) o; | |
293 | ||
294 | if (! value) | |
295 | { | |
296 | PyErr_SetString (PyExc_TypeError, | |
297 | "cannot delete the type_printers attribute"); | |
298 | return -1; | |
299 | } | |
300 | ||
301 | if (! PyList_Check (value)) | |
302 | { | |
303 | PyErr_SetString (PyExc_TypeError, | |
304 | "the type_printers attribute must be a list"); | |
305 | return -1; | |
306 | } | |
307 | ||
308 | /* Take care in case the LHS and RHS are related somehow. */ | |
309 | tmp = self->type_printers; | |
310 | Py_INCREF (value); | |
311 | self->type_printers = value; | |
312 | Py_XDECREF (tmp); | |
313 | ||
314 | return 0; | |
315 | } | |
316 | ||
fa33c3cd DE |
317 | \f |
318 | ||
319 | /* Clear the PSPACE pointer in a Pspace object and remove the reference. */ | |
320 | ||
321 | static void | |
322 | py_free_pspace (struct program_space *pspace, void *datum) | |
323 | { | |
227533ac DE |
324 | /* This is a fiction, but we're in a nasty spot: The pspace is in the |
325 | process of being deleted, we can't rely on anything in it. Plus | |
326 | this is one time when the current program space and current inferior | |
327 | are not in sync: All inferiors that use PSPACE may no longer exist. | |
328 | We don't need to do much here, and since "there is always an inferior" | |
329 | using target_gdbarch suffices. | |
330 | Note: We cannot call get_current_arch because it may try to access | |
331 | the target, which may involve accessing data in the pspace currently | |
332 | being deleted. */ | |
333 | struct gdbarch *arch = target_gdbarch (); | |
fa33c3cd | 334 | |
bf7da5b0 | 335 | gdbpy_enter enter_py (arch, current_language); |
88b6faea | 336 | gdbpy_ref<pspace_object> object ((pspace_object *) datum); |
fa33c3cd | 337 | object->pspace = NULL; |
fa33c3cd DE |
338 | } |
339 | ||
340 | /* Return a borrowed reference to the Python object of type Pspace | |
341 | representing PSPACE. If the object has already been created, | |
342 | return it. Otherwise, create it. Return NULL and set the Python | |
343 | error on failure. */ | |
344 | ||
345 | PyObject * | |
346 | pspace_to_pspace_object (struct program_space *pspace) | |
347 | { | |
88b6faea TT |
348 | gdbpy_ref<pspace_object> object |
349 | ((pspace_object *) program_space_data (pspace, pspy_pspace_data_key)); | |
350 | if (object == NULL) | |
fa33c3cd | 351 | { |
88b6faea TT |
352 | object.reset (PyObject_New (pspace_object, &pspace_object_type)); |
353 | if (object != NULL) | |
fa33c3cd | 354 | { |
88b6faea TT |
355 | if (!pspy_initialize (object.get ())) |
356 | return NULL; | |
883964a7 | 357 | |
4e1bbde0 | 358 | object->pspace = pspace; |
88b6faea | 359 | set_program_space_data (pspace, pspy_pspace_data_key, object.get ()); |
fa33c3cd DE |
360 | } |
361 | } | |
362 | ||
88b6faea | 363 | return (PyObject *) object.release (); |
fa33c3cd DE |
364 | } |
365 | ||
999633ed | 366 | int |
fa33c3cd DE |
367 | gdbpy_initialize_pspace (void) |
368 | { | |
369 | pspy_pspace_data_key | |
8e260fc0 | 370 | = register_program_space_data_with_cleanup (NULL, py_free_pspace); |
fa33c3cd DE |
371 | |
372 | if (PyType_Ready (&pspace_object_type) < 0) | |
999633ed | 373 | return -1; |
fa33c3cd | 374 | |
aa36459a TT |
375 | return gdb_pymodule_addobject (gdb_module, "Progspace", |
376 | (PyObject *) &pspace_object_type); | |
fa33c3cd DE |
377 | } |
378 | ||
379 | \f | |
380 | ||
0d1f4ceb | 381 | static gdb_PyGetSetDef pspace_getset[] = |
fa33c3cd | 382 | { |
02be9a71 DE |
383 | { "__dict__", gdb_py_generic_dict, NULL, |
384 | "The __dict__ for this progspace.", &pspace_object_type }, | |
fa33c3cd DE |
385 | { "filename", pspy_get_filename, NULL, |
386 | "The progspace's main filename, or None.", NULL }, | |
387 | { "pretty_printers", pspy_get_printers, pspy_set_printers, | |
388 | "Pretty printers.", NULL }, | |
1e611234 PM |
389 | { "frame_filters", pspy_get_frame_filters, pspy_set_frame_filters, |
390 | "Frame filters.", NULL }, | |
d11916aa SS |
391 | { "frame_unwinders", pspy_get_frame_unwinders, pspy_set_frame_unwinders, |
392 | "Frame unwinders.", NULL }, | |
18a9fc12 TT |
393 | { "type_printers", pspy_get_type_printers, pspy_set_type_printers, |
394 | "Type printers.", NULL }, | |
883964a7 SC |
395 | { "xmethods", pspy_get_xmethods, NULL, |
396 | "Debug methods.", NULL }, | |
fa33c3cd DE |
397 | { NULL } |
398 | }; | |
399 | ||
e36122e9 | 400 | PyTypeObject pspace_object_type = |
fa33c3cd | 401 | { |
9a27f2c6 | 402 | PyVarObject_HEAD_INIT (NULL, 0) |
fa33c3cd DE |
403 | "gdb.Progspace", /*tp_name*/ |
404 | sizeof (pspace_object), /*tp_basicsize*/ | |
405 | 0, /*tp_itemsize*/ | |
406 | pspy_dealloc, /*tp_dealloc*/ | |
407 | 0, /*tp_print*/ | |
408 | 0, /*tp_getattr*/ | |
409 | 0, /*tp_setattr*/ | |
410 | 0, /*tp_compare*/ | |
411 | 0, /*tp_repr*/ | |
412 | 0, /*tp_as_number*/ | |
413 | 0, /*tp_as_sequence*/ | |
414 | 0, /*tp_as_mapping*/ | |
415 | 0, /*tp_hash */ | |
416 | 0, /*tp_call*/ | |
417 | 0, /*tp_str*/ | |
418 | 0, /*tp_getattro*/ | |
419 | 0, /*tp_setattro*/ | |
420 | 0, /*tp_as_buffer*/ | |
421 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ | |
422 | "GDB progspace object", /* tp_doc */ | |
423 | 0, /* tp_traverse */ | |
424 | 0, /* tp_clear */ | |
425 | 0, /* tp_richcompare */ | |
426 | 0, /* tp_weaklistoffset */ | |
427 | 0, /* tp_iter */ | |
428 | 0, /* tp_iternext */ | |
429 | 0, /* tp_methods */ | |
430 | 0, /* tp_members */ | |
431 | pspace_getset, /* tp_getset */ | |
432 | 0, /* tp_base */ | |
433 | 0, /* tp_dict */ | |
434 | 0, /* tp_descr_get */ | |
435 | 0, /* tp_descr_set */ | |
02be9a71 | 436 | offsetof (pspace_object, dict), /* tp_dictoffset */ |
fa33c3cd DE |
437 | 0, /* tp_init */ |
438 | 0, /* tp_alloc */ | |
439 | pspy_new, /* tp_new */ | |
440 | }; |