Change all_objfiles adapter to be a method on program_space
[deliverable/binutils-gdb.git] / gdb / python / py-progspace.c
CommitLineData
fa33c3cd
DE
1/* Python interface to program spaces.
2
42a4f53d 3 Copyright (C) 2010-2019 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"
8743a9cd
TT
28#include "solib.h"
29#include "block.h"
fa33c3cd
DE
30
31typedef struct
32{
33 PyObject_HEAD
34
35 /* The corresponding pspace. */
36 struct program_space *pspace;
37
02be9a71
DE
38 /* Dictionary holding user-added attributes.
39 This is the __dict__ attribute of the object. */
40 PyObject *dict;
41
fa33c3cd
DE
42 /* The pretty-printer list of functions. */
43 PyObject *printers;
18a9fc12 44
1e611234
PM
45 /* The frame filter list of functions. */
46 PyObject *frame_filters;
d11916aa
SS
47
48 /* The frame unwinder list. */
49 PyObject *frame_unwinders;
50
18a9fc12
TT
51 /* The type-printer list. */
52 PyObject *type_printers;
883964a7
SC
53
54 /* The debug method list. */
55 PyObject *xmethods;
fa33c3cd
DE
56} pspace_object;
57
e36122e9 58extern PyTypeObject pspace_object_type
62eec1a5 59 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object");
fa33c3cd
DE
60
61static const struct program_space_data *pspy_pspace_data_key;
62
0ae1a321
SM
63/* Require that PSPACE_OBJ be a valid program space ID. */
64#define PSPY_REQUIRE_VALID(pspace_obj) \
65 do { \
66 if (pspace_obj->pspace == nullptr) \
67 { \
68 PyErr_SetString (PyExc_RuntimeError, \
69 _("Program space no longer exists.")); \
70 return NULL; \
71 } \
72 } while (0)
fa33c3cd
DE
73
74/* An Objfile method which returns the objfile's file name, or None. */
75
76static PyObject *
77pspy_get_filename (PyObject *self, void *closure)
78{
79 pspace_object *obj = (pspace_object *) self;
d59b6f6c 80
fa33c3cd
DE
81 if (obj->pspace)
82 {
83 struct objfile *objfile = obj->pspace->symfile_object_file;
d59b6f6c 84
d31d2fc3 85 if (objfile)
833d985d
TT
86 return (host_string_to_python_string (objfile_name (objfile))
87 .release ());
fa33c3cd
DE
88 }
89 Py_RETURN_NONE;
90}
91
92static void
93pspy_dealloc (PyObject *self)
94{
95 pspace_object *ps_self = (pspace_object *) self;
d59b6f6c 96
02be9a71 97 Py_XDECREF (ps_self->dict);
fa33c3cd 98 Py_XDECREF (ps_self->printers);
1e611234 99 Py_XDECREF (ps_self->frame_filters);
d11916aa 100 Py_XDECREF (ps_self->frame_unwinders);
18a9fc12 101 Py_XDECREF (ps_self->type_printers);
883964a7 102 Py_XDECREF (ps_self->xmethods);
9a27f2c6 103 Py_TYPE (self)->tp_free (self);
fa33c3cd
DE
104}
105
4e1bbde0
DE
106/* Initialize a pspace_object.
107 The result is a boolean indicating success. */
108
109static int
110pspy_initialize (pspace_object *self)
111{
112 self->pspace = NULL;
0f6ed0e0
TT
113
114 self->dict = PyDict_New ();
115 if (self->dict == NULL)
116 return 0;
4e1bbde0
DE
117
118 self->printers = PyList_New (0);
119 if (self->printers == NULL)
120 return 0;
121
122 self->frame_filters = PyDict_New ();
123 if (self->frame_filters == NULL)
124 return 0;
125
d11916aa
SS
126 self->frame_unwinders = PyList_New (0);
127 if (self->frame_unwinders == NULL)
128 return 0;
129
4e1bbde0
DE
130 self->type_printers = PyList_New (0);
131 if (self->type_printers == NULL)
132 return 0;
133
134 self->xmethods = PyList_New (0);
135 if (self->xmethods == NULL)
136 return 0;
137
138 return 1;
139}
140
fa33c3cd
DE
141static PyObject *
142pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
143{
88b6faea 144 gdbpy_ref<pspace_object> self ((pspace_object *) type->tp_alloc (type, 0));
d59b6f6c 145
88b6faea 146 if (self != NULL)
fa33c3cd 147 {
88b6faea
TT
148 if (!pspy_initialize (self.get ()))
149 return NULL;
fa33c3cd 150 }
4e1bbde0 151
88b6faea 152 return (PyObject *) self.release ();
fa33c3cd
DE
153}
154
155PyObject *
156pspy_get_printers (PyObject *o, void *ignore)
157{
158 pspace_object *self = (pspace_object *) o;
d59b6f6c 159
fa33c3cd
DE
160 Py_INCREF (self->printers);
161 return self->printers;
162}
163
164static int
165pspy_set_printers (PyObject *o, PyObject *value, void *ignore)
166{
fa33c3cd 167 pspace_object *self = (pspace_object *) o;
d59b6f6c 168
fa33c3cd
DE
169 if (! value)
170 {
171 PyErr_SetString (PyExc_TypeError,
172 "cannot delete the pretty_printers attribute");
173 return -1;
174 }
175
176 if (! PyList_Check (value))
177 {
178 PyErr_SetString (PyExc_TypeError,
179 "the pretty_printers attribute must be a list");
180 return -1;
181 }
182
183 /* Take care in case the LHS and RHS are related somehow. */
2a3c71d6 184 gdbpy_ref<> tmp (self->printers);
fa33c3cd
DE
185 Py_INCREF (value);
186 self->printers = value;
fa33c3cd
DE
187
188 return 0;
189}
190
1e611234
PM
191/* Return the Python dictionary attribute containing frame filters for
192 this program space. */
193PyObject *
194pspy_get_frame_filters (PyObject *o, void *ignore)
195{
196 pspace_object *self = (pspace_object *) o;
197
198 Py_INCREF (self->frame_filters);
199 return self->frame_filters;
200}
201
202/* Set this object file's frame filters dictionary to FILTERS. */
203static int
204pspy_set_frame_filters (PyObject *o, PyObject *frame, void *ignore)
205{
1e611234
PM
206 pspace_object *self = (pspace_object *) o;
207
208 if (! frame)
209 {
210 PyErr_SetString (PyExc_TypeError,
211 "cannot delete the frame filter attribute");
212 return -1;
213 }
214
215 if (! PyDict_Check (frame))
216 {
217 PyErr_SetString (PyExc_TypeError,
218 "the frame filter attribute must be a dictionary");
219 return -1;
220 }
221
222 /* Take care in case the LHS and RHS are related somehow. */
2a3c71d6 223 gdbpy_ref<> tmp (self->frame_filters);
1e611234
PM
224 Py_INCREF (frame);
225 self->frame_filters = frame;
1e611234
PM
226
227 return 0;
228}
229
d11916aa
SS
230/* Return the list of the frame unwinders for this program space. */
231
232PyObject *
233pspy_get_frame_unwinders (PyObject *o, void *ignore)
234{
235 pspace_object *self = (pspace_object *) o;
236
237 Py_INCREF (self->frame_unwinders);
238 return self->frame_unwinders;
239}
240
241/* Set this program space's list of the unwinders to UNWINDERS. */
242
243static int
244pspy_set_frame_unwinders (PyObject *o, PyObject *unwinders, void *ignore)
245{
d11916aa
SS
246 pspace_object *self = (pspace_object *) o;
247
248 if (!unwinders)
249 {
250 PyErr_SetString (PyExc_TypeError,
251 "cannot delete the frame unwinders list");
252 return -1;
253 }
254
255 if (!PyList_Check (unwinders))
256 {
257 PyErr_SetString (PyExc_TypeError,
258 "the frame unwinders attribute must be a list");
259 return -1;
260 }
261
262 /* Take care in case the LHS and RHS are related somehow. */
2a3c71d6 263 gdbpy_ref<> tmp (self->frame_unwinders);
d11916aa
SS
264 Py_INCREF (unwinders);
265 self->frame_unwinders = unwinders;
d11916aa
SS
266
267 return 0;
268}
269
18a9fc12
TT
270/* Get the 'type_printers' attribute. */
271
272static PyObject *
273pspy_get_type_printers (PyObject *o, void *ignore)
274{
275 pspace_object *self = (pspace_object *) o;
276
277 Py_INCREF (self->type_printers);
278 return self->type_printers;
279}
280
883964a7
SC
281/* Get the 'xmethods' attribute. */
282
283PyObject *
284pspy_get_xmethods (PyObject *o, void *ignore)
285{
286 pspace_object *self = (pspace_object *) o;
287
288 Py_INCREF (self->xmethods);
289 return self->xmethods;
290}
291
18a9fc12
TT
292/* Set the 'type_printers' attribute. */
293
294static int
295pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
296{
18a9fc12
TT
297 pspace_object *self = (pspace_object *) o;
298
299 if (! value)
300 {
301 PyErr_SetString (PyExc_TypeError,
302 "cannot delete the type_printers attribute");
303 return -1;
304 }
305
306 if (! PyList_Check (value))
307 {
308 PyErr_SetString (PyExc_TypeError,
309 "the type_printers attribute must be a list");
310 return -1;
311 }
312
313 /* Take care in case the LHS and RHS are related somehow. */
2a3c71d6 314 gdbpy_ref<> tmp (self->type_printers);
18a9fc12
TT
315 Py_INCREF (value);
316 self->type_printers = value;
18a9fc12
TT
317
318 return 0;
319}
320
0ae1a321
SM
321/* Implement the objfiles method. */
322
323static PyObject *
324pspy_get_objfiles (PyObject *self_, PyObject *args)
325{
326 pspace_object *self = (pspace_object *) self_;
327
328 PSPY_REQUIRE_VALID (self);
329
8743a9cd
TT
330 gdbpy_ref<> list (PyList_New (0));
331 if (list == NULL)
332 return NULL;
333
334 if (self->pspace != NULL)
335 {
2030c079 336 for (objfile *objf : self->pspace->objfiles ())
8743a9cd 337 {
0a9db5ad 338 gdbpy_ref<> item = objfile_to_objfile_object (objf);
8743a9cd 339
0a9db5ad
TT
340 if (item == nullptr
341 || PyList_Append (list.get (), item.get ()) == -1)
8743a9cd
TT
342 return NULL;
343 }
344 }
345
346 return list.release ();
347}
348
349/* Implementation of solib_name (Long) -> String.
350 Returns the name of the shared library holding a given address, or None. */
351
352static PyObject *
353pspy_solib_name (PyObject *o, PyObject *args)
354{
355 char *soname;
5c4481cc 356 gdb_py_ulongest pc;
8743a9cd
TT
357 pspace_object *self = (pspace_object *) o;
358
359 PSPY_REQUIRE_VALID (self);
360
361 if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc))
362 return NULL;
363
364 soname = solib_name_from_address (self->pspace, pc);
365 if (soname == nullptr)
366 Py_RETURN_NONE;
833d985d 367 return host_string_to_python_string (soname).release ();
0ae1a321 368}
fa33c3cd 369
8743a9cd
TT
370/* Return the innermost lexical block containing the specified pc value,
371 or 0 if there is none. */
372static PyObject *
373pspy_block_for_pc (PyObject *o, PyObject *args)
374{
375 pspace_object *self = (pspace_object *) o;
376 gdb_py_ulongest pc;
377 const struct block *block = NULL;
378 struct compunit_symtab *cust = NULL;
379
380 PSPY_REQUIRE_VALID (self);
381
382 if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc))
383 return NULL;
384
385 TRY
386 {
387 scoped_restore_current_program_space saver;
388
389 set_current_program_space (self->pspace);
390 cust = find_pc_compunit_symtab (pc);
391
392 if (cust != NULL && COMPUNIT_OBJFILE (cust) != NULL)
393 block = block_for_pc (pc);
394 }
395 CATCH (except, RETURN_MASK_ALL)
396 {
397 GDB_PY_HANDLE_EXCEPTION (except);
398 }
399 END_CATCH
400
401 if (cust == NULL || COMPUNIT_OBJFILE (cust) == NULL)
402 {
403 PyErr_SetString (PyExc_RuntimeError,
404 _("Cannot locate object file for block."));
405 return NULL;
406 }
407
408 if (block)
409 return block_to_block_object (block, COMPUNIT_OBJFILE (cust));
410
411 Py_RETURN_NONE;
412}
413
414/* Implementation of the find_pc_line function.
415 Returns the gdb.Symtab_and_line object corresponding to a PC value. */
416
417static PyObject *
418pspy_find_pc_line (PyObject *o, PyObject *args)
419{
420 gdb_py_ulongest pc_llu;
421 PyObject *result = NULL; /* init for gcc -Wall */
422 pspace_object *self = (pspace_object *) o;
423
424 PSPY_REQUIRE_VALID (self);
425
426 if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc_llu))
427 return NULL;
428
429 TRY
430 {
431 struct symtab_and_line sal;
432 CORE_ADDR pc;
433 scoped_restore_current_program_space saver;
434
435 set_current_program_space (self->pspace);
436
437 pc = (CORE_ADDR) pc_llu;
438 sal = find_pc_line (pc, 0);
439 result = symtab_and_line_to_sal_object (sal);
440 }
441 CATCH (except, RETURN_MASK_ALL)
442 {
443 GDB_PY_HANDLE_EXCEPTION (except);
444 }
445 END_CATCH
446
447 return result;
448}
449
450/* Implementation of is_valid (self) -> Boolean.
451 Returns True if this program space still exists in GDB. */
452
453static PyObject *
454pspy_is_valid (PyObject *o, PyObject *args)
455{
456 pspace_object *self = (pspace_object *) o;
457
458 if (self->pspace == NULL)
459 Py_RETURN_FALSE;
460
461 Py_RETURN_TRUE;
462}
463
464\f
465
fa33c3cd
DE
466/* Clear the PSPACE pointer in a Pspace object and remove the reference. */
467
468static void
469py_free_pspace (struct program_space *pspace, void *datum)
470{
227533ac
DE
471 /* This is a fiction, but we're in a nasty spot: The pspace is in the
472 process of being deleted, we can't rely on anything in it. Plus
473 this is one time when the current program space and current inferior
474 are not in sync: All inferiors that use PSPACE may no longer exist.
475 We don't need to do much here, and since "there is always an inferior"
476 using target_gdbarch suffices.
477 Note: We cannot call get_current_arch because it may try to access
478 the target, which may involve accessing data in the pspace currently
479 being deleted. */
480 struct gdbarch *arch = target_gdbarch ();
fa33c3cd 481
bf7da5b0 482 gdbpy_enter enter_py (arch, current_language);
88b6faea 483 gdbpy_ref<pspace_object> object ((pspace_object *) datum);
fa33c3cd 484 object->pspace = NULL;
fa33c3cd
DE
485}
486
3c7aa307 487/* Return a new reference to the Python object of type Pspace
fa33c3cd
DE
488 representing PSPACE. If the object has already been created,
489 return it. Otherwise, create it. Return NULL and set the Python
490 error on failure. */
491
3c7aa307 492gdbpy_ref<>
fa33c3cd
DE
493pspace_to_pspace_object (struct program_space *pspace)
494{
3c7aa307
TT
495 PyObject *result
496 ((PyObject *) program_space_data (pspace, pspy_pspace_data_key));
497 if (result == NULL)
fa33c3cd 498 {
3c7aa307
TT
499 gdbpy_ref<pspace_object> object
500 ((pspace_object *) PyObject_New (pspace_object, &pspace_object_type));
501 if (object == NULL)
502 return NULL;
503 if (!pspy_initialize (object.get ()))
504 return NULL;
883964a7 505
3c7aa307
TT
506 object->pspace = pspace;
507 set_program_space_data (pspace, pspy_pspace_data_key, object.get ());
508 result = (PyObject *) object.release ();
fa33c3cd
DE
509 }
510
3c7aa307 511 return gdbpy_ref<>::new_reference (result);
fa33c3cd
DE
512}
513
999633ed 514int
fa33c3cd
DE
515gdbpy_initialize_pspace (void)
516{
517 pspy_pspace_data_key
8e260fc0 518 = register_program_space_data_with_cleanup (NULL, py_free_pspace);
fa33c3cd
DE
519
520 if (PyType_Ready (&pspace_object_type) < 0)
999633ed 521 return -1;
fa33c3cd 522
aa36459a
TT
523 return gdb_pymodule_addobject (gdb_module, "Progspace",
524 (PyObject *) &pspace_object_type);
fa33c3cd
DE
525}
526
527\f
528
0d1f4ceb 529static gdb_PyGetSetDef pspace_getset[] =
fa33c3cd 530{
02be9a71
DE
531 { "__dict__", gdb_py_generic_dict, NULL,
532 "The __dict__ for this progspace.", &pspace_object_type },
fa33c3cd
DE
533 { "filename", pspy_get_filename, NULL,
534 "The progspace's main filename, or None.", NULL },
535 { "pretty_printers", pspy_get_printers, pspy_set_printers,
536 "Pretty printers.", NULL },
1e611234
PM
537 { "frame_filters", pspy_get_frame_filters, pspy_set_frame_filters,
538 "Frame filters.", NULL },
d11916aa
SS
539 { "frame_unwinders", pspy_get_frame_unwinders, pspy_set_frame_unwinders,
540 "Frame unwinders.", NULL },
18a9fc12
TT
541 { "type_printers", pspy_get_type_printers, pspy_set_type_printers,
542 "Type printers.", NULL },
883964a7
SC
543 { "xmethods", pspy_get_xmethods, NULL,
544 "Debug methods.", NULL },
fa33c3cd
DE
545 { NULL }
546};
547
0ae1a321
SM
548static PyMethodDef progspace_object_methods[] =
549{
550 { "objfiles", pspy_get_objfiles, METH_NOARGS,
551 "Return a sequence of objfiles associated to this program space." },
8743a9cd
TT
552 { "solib_name", pspy_solib_name, METH_VARARGS,
553 "solib_name (Long) -> String.\n\
554Return the name of the shared library holding a given address, or None." },
555 { "block_for_pc", pspy_block_for_pc, METH_VARARGS,
556 "Return the block containing the given pc value, or None." },
557 { "find_pc_line", pspy_find_pc_line, METH_VARARGS,
558 "find_pc_line (pc) -> Symtab_and_line.\n\
559Return the gdb.Symtab_and_line object corresponding to the pc value." },
560 { "is_valid", pspy_is_valid, METH_NOARGS,
561 "is_valid () -> Boolean.\n\
562Return true if this program space is valid, false if not." },
0ae1a321
SM
563 { NULL }
564};
565
e36122e9 566PyTypeObject pspace_object_type =
fa33c3cd 567{
9a27f2c6 568 PyVarObject_HEAD_INIT (NULL, 0)
fa33c3cd
DE
569 "gdb.Progspace", /*tp_name*/
570 sizeof (pspace_object), /*tp_basicsize*/
571 0, /*tp_itemsize*/
572 pspy_dealloc, /*tp_dealloc*/
573 0, /*tp_print*/
574 0, /*tp_getattr*/
575 0, /*tp_setattr*/
576 0, /*tp_compare*/
577 0, /*tp_repr*/
578 0, /*tp_as_number*/
579 0, /*tp_as_sequence*/
580 0, /*tp_as_mapping*/
581 0, /*tp_hash */
582 0, /*tp_call*/
583 0, /*tp_str*/
584 0, /*tp_getattro*/
585 0, /*tp_setattro*/
586 0, /*tp_as_buffer*/
587 Py_TPFLAGS_DEFAULT, /*tp_flags*/
588 "GDB progspace object", /* tp_doc */
589 0, /* tp_traverse */
590 0, /* tp_clear */
591 0, /* tp_richcompare */
592 0, /* tp_weaklistoffset */
593 0, /* tp_iter */
594 0, /* tp_iternext */
0ae1a321 595 progspace_object_methods, /* tp_methods */
fa33c3cd
DE
596 0, /* tp_members */
597 pspace_getset, /* tp_getset */
598 0, /* tp_base */
599 0, /* tp_dict */
600 0, /* tp_descr_get */
601 0, /* tp_descr_set */
02be9a71 602 offsetof (pspace_object, dict), /* tp_dictoffset */
fa33c3cd
DE
603 0, /* tp_init */
604 0, /* tp_alloc */
605 pspy_new, /* tp_new */
606};
This page took 0.883045 seconds and 4 git commands to generate.