Simplify gdbpy_stop_recording
[external/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   pspace_object *self = (pspace_object *) o;
168
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.  */
184   gdbpy_ref<> tmp (self->printers);
185   Py_INCREF (value);
186   self->printers = value;
187
188   return 0;
189 }
190
191 /* Return the Python dictionary attribute containing frame filters for
192    this program space.  */
193 PyObject *
194 pspy_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.  */
203 static int
204 pspy_set_frame_filters (PyObject *o, PyObject *frame, void *ignore)
205 {
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.  */
223   gdbpy_ref<> tmp (self->frame_filters);
224   Py_INCREF (frame);
225   self->frame_filters = frame;
226
227   return 0;
228 }
229
230 /* Return the list of the frame unwinders for this program space.  */
231
232 PyObject *
233 pspy_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
243 static int
244 pspy_set_frame_unwinders (PyObject *o, PyObject *unwinders, void *ignore)
245 {
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.  */
263   gdbpy_ref<> tmp (self->frame_unwinders);
264   Py_INCREF (unwinders);
265   self->frame_unwinders = unwinders;
266
267   return 0;
268 }
269
270 /* Get the 'type_printers' attribute.  */
271
272 static PyObject *
273 pspy_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
281 /* Get the 'xmethods' attribute.  */
282
283 PyObject *
284 pspy_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
292 /* Set the 'type_printers' attribute.  */
293
294 static int
295 pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
296 {
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.  */
314   gdbpy_ref<> tmp (self->type_printers);
315   Py_INCREF (value);
316   self->type_printers = value;
317
318   return 0;
319 }
320
321 /* Implement the objfiles method.  */
322
323 static PyObject *
324 pspy_get_objfiles (PyObject *self_, PyObject *args)
325 {
326   pspace_object *self = (pspace_object *) self_;
327
328   PSPY_REQUIRE_VALID (self);
329
330   gdbpy_ref<> list (PyList_New (0));
331   if (list == NULL)
332     return NULL;
333
334   if (self->pspace != NULL)
335     {
336       struct objfile *objf;
337
338       ALL_PSPACE_OBJFILES (self->pspace, objf)
339         {
340           gdbpy_ref<> item = objfile_to_objfile_object (objf);
341
342           if (item == nullptr
343               || PyList_Append (list.get (), item.get ()) == -1)
344             return NULL;
345         }
346     }
347
348   return list.release ();
349 }
350
351 /* Implementation of solib_name (Long) -> String.
352    Returns the name of the shared library holding a given address, or None.  */
353
354 static PyObject *
355 pspy_solib_name (PyObject *o, PyObject *args)
356 {
357   char *soname;
358   gdb_py_ulongest pc;
359   pspace_object *self = (pspace_object *) o;
360
361   PSPY_REQUIRE_VALID (self);
362
363   if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc))
364     return NULL;
365
366   soname = solib_name_from_address (self->pspace, pc);
367   if (soname == nullptr)
368     Py_RETURN_NONE;
369   return host_string_to_python_string (soname).release ();
370 }
371
372 /* Return the innermost lexical block containing the specified pc value,
373    or 0 if there is none.  */
374 static PyObject *
375 pspy_block_for_pc (PyObject *o, PyObject *args)
376 {
377   pspace_object *self = (pspace_object *) o;
378   gdb_py_ulongest pc;
379   const struct block *block = NULL;
380   struct compunit_symtab *cust = NULL;
381
382   PSPY_REQUIRE_VALID (self);
383
384   if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc))
385     return NULL;
386
387   TRY
388     {
389       scoped_restore_current_program_space saver;
390
391       set_current_program_space (self->pspace);
392       cust = find_pc_compunit_symtab (pc);
393
394       if (cust != NULL && COMPUNIT_OBJFILE (cust) != NULL)
395         block = block_for_pc (pc);
396     }
397   CATCH (except, RETURN_MASK_ALL)
398     {
399       GDB_PY_HANDLE_EXCEPTION (except);
400     }
401   END_CATCH
402
403   if (cust == NULL || COMPUNIT_OBJFILE (cust) == NULL)
404     {
405       PyErr_SetString (PyExc_RuntimeError,
406                        _("Cannot locate object file for block."));
407       return NULL;
408     }
409
410   if (block)
411     return block_to_block_object (block, COMPUNIT_OBJFILE (cust));
412
413   Py_RETURN_NONE;
414 }
415
416 /* Implementation of the find_pc_line function.
417    Returns the gdb.Symtab_and_line object corresponding to a PC value.  */
418
419 static PyObject *
420 pspy_find_pc_line (PyObject *o, PyObject *args)
421 {
422   gdb_py_ulongest pc_llu;
423   PyObject *result = NULL; /* init for gcc -Wall */
424   pspace_object *self = (pspace_object *) o;
425
426   PSPY_REQUIRE_VALID (self);
427
428   if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc_llu))
429     return NULL;
430
431   TRY
432     {
433       struct symtab_and_line sal;
434       CORE_ADDR pc;
435       scoped_restore_current_program_space saver;
436
437       set_current_program_space (self->pspace);
438
439       pc = (CORE_ADDR) pc_llu;
440       sal = find_pc_line (pc, 0);
441       result = symtab_and_line_to_sal_object (sal);
442     }
443   CATCH (except, RETURN_MASK_ALL)
444     {
445       GDB_PY_HANDLE_EXCEPTION (except);
446     }
447   END_CATCH
448
449   return result;
450 }
451
452 /* Implementation of is_valid (self) -> Boolean.
453    Returns True if this program space still exists in GDB.  */
454
455 static PyObject *
456 pspy_is_valid (PyObject *o, PyObject *args)
457 {
458   pspace_object *self = (pspace_object *) o;
459
460   if (self->pspace == NULL)
461     Py_RETURN_FALSE;
462
463   Py_RETURN_TRUE;
464 }
465
466 \f
467
468 /* Clear the PSPACE pointer in a Pspace object and remove the reference.  */
469
470 static void
471 py_free_pspace (struct program_space *pspace, void *datum)
472 {
473   /* This is a fiction, but we're in a nasty spot: The pspace is in the
474      process of being deleted, we can't rely on anything in it.  Plus
475      this is one time when the current program space and current inferior
476      are not in sync: All inferiors that use PSPACE may no longer exist.
477      We don't need to do much here, and since "there is always an inferior"
478      using target_gdbarch suffices.
479      Note: We cannot call get_current_arch because it may try to access
480      the target, which may involve accessing data in the pspace currently
481      being deleted.  */
482   struct gdbarch *arch = target_gdbarch ();
483
484   gdbpy_enter enter_py (arch, current_language);
485   gdbpy_ref<pspace_object> object ((pspace_object *) datum);
486   object->pspace = NULL;
487 }
488
489 /* Return a new reference to the Python object of type Pspace
490    representing PSPACE.  If the object has already been created,
491    return it.  Otherwise, create it.  Return NULL and set the Python
492    error on failure.  */
493
494 gdbpy_ref<>
495 pspace_to_pspace_object (struct program_space *pspace)
496 {
497   PyObject *result
498     ((PyObject *) program_space_data (pspace, pspy_pspace_data_key));
499   if (result == NULL)
500     {
501       gdbpy_ref<pspace_object> object
502         ((pspace_object *) PyObject_New (pspace_object, &pspace_object_type));
503       if (object == NULL)
504         return NULL;
505       if (!pspy_initialize (object.get ()))
506         return NULL;
507
508       object->pspace = pspace;
509       set_program_space_data (pspace, pspy_pspace_data_key, object.get ());
510       result = (PyObject *) object.release ();
511     }
512
513   return gdbpy_ref<>::new_reference (result);
514 }
515
516 int
517 gdbpy_initialize_pspace (void)
518 {
519   pspy_pspace_data_key
520     = register_program_space_data_with_cleanup (NULL, py_free_pspace);
521
522   if (PyType_Ready (&pspace_object_type) < 0)
523     return -1;
524
525   return gdb_pymodule_addobject (gdb_module, "Progspace",
526                                  (PyObject *) &pspace_object_type);
527 }
528
529 \f
530
531 static gdb_PyGetSetDef pspace_getset[] =
532 {
533   { "__dict__", gdb_py_generic_dict, NULL,
534     "The __dict__ for this progspace.", &pspace_object_type },
535   { "filename", pspy_get_filename, NULL,
536     "The progspace's main filename, or None.", NULL },
537   { "pretty_printers", pspy_get_printers, pspy_set_printers,
538     "Pretty printers.", NULL },
539   { "frame_filters", pspy_get_frame_filters, pspy_set_frame_filters,
540     "Frame filters.", NULL },
541   { "frame_unwinders", pspy_get_frame_unwinders, pspy_set_frame_unwinders,
542     "Frame unwinders.", NULL },
543   { "type_printers", pspy_get_type_printers, pspy_set_type_printers,
544     "Type printers.", NULL },
545   { "xmethods", pspy_get_xmethods, NULL,
546     "Debug methods.", NULL },
547   { NULL }
548 };
549
550 static PyMethodDef progspace_object_methods[] =
551 {
552   { "objfiles", pspy_get_objfiles, METH_NOARGS,
553     "Return a sequence of objfiles associated to this program space." },
554   { "solib_name", pspy_solib_name, METH_VARARGS,
555     "solib_name (Long) -> String.\n\
556 Return the name of the shared library holding a given address, or None." },
557   { "block_for_pc", pspy_block_for_pc, METH_VARARGS,
558     "Return the block containing the given pc value, or None." },
559   { "find_pc_line", pspy_find_pc_line, METH_VARARGS,
560     "find_pc_line (pc) -> Symtab_and_line.\n\
561 Return the gdb.Symtab_and_line object corresponding to the pc value." },
562   { "is_valid", pspy_is_valid, METH_NOARGS,
563     "is_valid () -> Boolean.\n\
564 Return true if this program space is valid, false if not." },
565   { NULL }
566 };
567
568 PyTypeObject pspace_object_type =
569 {
570   PyVarObject_HEAD_INIT (NULL, 0)
571   "gdb.Progspace",                /*tp_name*/
572   sizeof (pspace_object),         /*tp_basicsize*/
573   0,                              /*tp_itemsize*/
574   pspy_dealloc,                   /*tp_dealloc*/
575   0,                              /*tp_print*/
576   0,                              /*tp_getattr*/
577   0,                              /*tp_setattr*/
578   0,                              /*tp_compare*/
579   0,                              /*tp_repr*/
580   0,                              /*tp_as_number*/
581   0,                              /*tp_as_sequence*/
582   0,                              /*tp_as_mapping*/
583   0,                              /*tp_hash */
584   0,                              /*tp_call*/
585   0,                              /*tp_str*/
586   0,                              /*tp_getattro*/
587   0,                              /*tp_setattro*/
588   0,                              /*tp_as_buffer*/
589   Py_TPFLAGS_DEFAULT,             /*tp_flags*/
590   "GDB progspace object",         /* tp_doc */
591   0,                              /* tp_traverse */
592   0,                              /* tp_clear */
593   0,                              /* tp_richcompare */
594   0,                              /* tp_weaklistoffset */
595   0,                              /* tp_iter */
596   0,                              /* tp_iternext */
597   progspace_object_methods,       /* tp_methods */
598   0,                              /* tp_members */
599   pspace_getset,                  /* tp_getset */
600   0,                              /* tp_base */
601   0,                              /* tp_dict */
602   0,                              /* tp_descr_get */
603   0,                              /* tp_descr_set */
604   offsetof (pspace_object, dict), /* tp_dictoffset */
605   0,                              /* tp_init */
606   0,                              /* tp_alloc */
607   pspy_new,                       /* tp_new */
608 };