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