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