]>
Commit | Line | Data |
---|---|---|
1 | /* Python interface to program spaces. | |
2 | ||
3 | Copyright (C) 2010-2025 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 "python-internal.h" | |
21 | #include "charset.h" | |
22 | #include "progspace.h" | |
23 | #include "objfiles.h" | |
24 | #include "language.h" | |
25 | #include "arch-utils.h" | |
26 | #include "solib.h" | |
27 | #include "block.h" | |
28 | #include "py-event.h" | |
29 | #include "observable.h" | |
30 | #include "inferior.h" | |
31 | ||
32 | struct pspace_object | |
33 | { | |
34 | PyObject_HEAD | |
35 | ||
36 | /* The corresponding pspace. */ | |
37 | struct program_space *pspace; | |
38 | ||
39 | /* Dictionary holding user-added attributes. | |
40 | This is the __dict__ attribute of the object. */ | |
41 | PyObject *dict; | |
42 | ||
43 | /* The pretty-printer list of functions. */ | |
44 | PyObject *printers; | |
45 | ||
46 | /* The frame filter list of functions. */ | |
47 | PyObject *frame_filters; | |
48 | ||
49 | /* The frame unwinder list. */ | |
50 | PyObject *frame_unwinders; | |
51 | ||
52 | /* The type-printer list. */ | |
53 | PyObject *type_printers; | |
54 | ||
55 | /* The debug method list. */ | |
56 | PyObject *xmethods; | |
57 | ||
58 | /* The missing file handler list. */ | |
59 | PyObject *missing_file_handlers; | |
60 | }; | |
61 | ||
62 | extern PyTypeObject pspace_object_type | |
63 | CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object"); | |
64 | ||
65 | /* Clear the PSPACE pointer in a Pspace object and remove the reference. */ | |
66 | struct pspace_deleter | |
67 | { | |
68 | void operator() (pspace_object *obj) | |
69 | { | |
70 | /* This is a fiction, but we're in a nasty spot: The pspace is in the | |
71 | process of being deleted, we can't rely on anything in it. Plus | |
72 | this is one time when the current program space and current inferior | |
73 | are not in sync: All inferiors that use PSPACE may no longer exist. | |
74 | We don't need to do much here, and since "there is always an inferior" | |
75 | using the current inferior's arch suffices. | |
76 | Note: We cannot call get_current_arch because it may try to access | |
77 | the target, which may involve accessing data in the pspace currently | |
78 | being deleted. */ | |
79 | gdbarch *arch = current_inferior ()->arch (); | |
80 | ||
81 | gdbpy_enter enter_py (arch); | |
82 | gdbpy_ref<pspace_object> object (obj); | |
83 | object->pspace = NULL; | |
84 | } | |
85 | }; | |
86 | ||
87 | static const registry<program_space>::key<pspace_object, pspace_deleter> | |
88 | pspy_pspace_data_key; | |
89 | ||
90 | /* Require that PSPACE_OBJ be a valid program space ID. */ | |
91 | #define PSPY_REQUIRE_VALID(pspace_obj) \ | |
92 | do { \ | |
93 | if (pspace_obj->pspace == nullptr) \ | |
94 | { \ | |
95 | PyErr_SetString (PyExc_RuntimeError, \ | |
96 | _("Program space no longer exists.")); \ | |
97 | return NULL; \ | |
98 | } \ | |
99 | } while (0) | |
100 | ||
101 | /* An Objfile method which returns the objfile's file name, or None. */ | |
102 | ||
103 | static PyObject * | |
104 | pspy_get_filename (PyObject *self, void *closure) | |
105 | { | |
106 | pspace_object *obj = (pspace_object *) self; | |
107 | ||
108 | if (obj->pspace) | |
109 | { | |
110 | struct objfile *objfile = obj->pspace->symfile_object_file; | |
111 | ||
112 | if (objfile) | |
113 | return (host_string_to_python_string (objfile_name (objfile)) | |
114 | .release ()); | |
115 | } | |
116 | Py_RETURN_NONE; | |
117 | } | |
118 | ||
119 | /* Implement the gdb.Progspace.symbol_file attribute. Return the | |
120 | gdb.Objfile corresponding to the currently loaded symbol-file, or None | |
121 | if no symbol-file is loaded. If the Progspace is invalid then raise an | |
122 | exception. */ | |
123 | ||
124 | static PyObject * | |
125 | pspy_get_symbol_file (PyObject *self, void *closure) | |
126 | { | |
127 | pspace_object *obj = (pspace_object *) self; | |
128 | ||
129 | PSPY_REQUIRE_VALID (obj); | |
130 | ||
131 | struct objfile *objfile = obj->pspace->symfile_object_file; | |
132 | ||
133 | if (objfile != nullptr) | |
134 | return objfile_to_objfile_object (objfile).release (); | |
135 | ||
136 | Py_RETURN_NONE; | |
137 | } | |
138 | ||
139 | /* Implement the gdb.Progspace.executable_filename attribute. Return a | |
140 | string containing the name of the current executable, or None if no | |
141 | executable is currently set. If the Progspace is invalid then raise an | |
142 | exception. */ | |
143 | ||
144 | static PyObject * | |
145 | pspy_get_exec_file (PyObject *self, void *closure) | |
146 | { | |
147 | pspace_object *obj = (pspace_object *) self; | |
148 | ||
149 | PSPY_REQUIRE_VALID (obj); | |
150 | ||
151 | const char *filename = obj->pspace->exec_filename (); | |
152 | if (filename != nullptr) | |
153 | return host_string_to_python_string (filename).release (); | |
154 | ||
155 | Py_RETURN_NONE; | |
156 | } | |
157 | ||
158 | static void | |
159 | pspy_dealloc (PyObject *self) | |
160 | { | |
161 | pspace_object *ps_self = (pspace_object *) self; | |
162 | ||
163 | Py_XDECREF (ps_self->dict); | |
164 | Py_XDECREF (ps_self->printers); | |
165 | Py_XDECREF (ps_self->frame_filters); | |
166 | Py_XDECREF (ps_self->frame_unwinders); | |
167 | Py_XDECREF (ps_self->type_printers); | |
168 | Py_XDECREF (ps_self->xmethods); | |
169 | Py_XDECREF (ps_self->missing_file_handlers); | |
170 | Py_TYPE (self)->tp_free (self); | |
171 | } | |
172 | ||
173 | /* Initialize a pspace_object. | |
174 | The result is a boolean indicating success. */ | |
175 | ||
176 | static int | |
177 | pspy_initialize (pspace_object *self) | |
178 | { | |
179 | self->pspace = NULL; | |
180 | ||
181 | self->dict = PyDict_New (); | |
182 | if (self->dict == NULL) | |
183 | return 0; | |
184 | ||
185 | self->printers = PyList_New (0); | |
186 | if (self->printers == NULL) | |
187 | return 0; | |
188 | ||
189 | self->frame_filters = PyDict_New (); | |
190 | if (self->frame_filters == NULL) | |
191 | return 0; | |
192 | ||
193 | self->frame_unwinders = PyList_New (0); | |
194 | if (self->frame_unwinders == NULL) | |
195 | return 0; | |
196 | ||
197 | self->type_printers = PyList_New (0); | |
198 | if (self->type_printers == NULL) | |
199 | return 0; | |
200 | ||
201 | self->xmethods = PyList_New (0); | |
202 | if (self->xmethods == NULL) | |
203 | return 0; | |
204 | ||
205 | self->missing_file_handlers = PyList_New (0); | |
206 | if (self->missing_file_handlers == nullptr) | |
207 | return 0; | |
208 | ||
209 | return 1; | |
210 | } | |
211 | ||
212 | PyObject * | |
213 | pspy_get_printers (PyObject *o, void *ignore) | |
214 | { | |
215 | pspace_object *self = (pspace_object *) o; | |
216 | ||
217 | Py_INCREF (self->printers); | |
218 | return self->printers; | |
219 | } | |
220 | ||
221 | static int | |
222 | pspy_set_printers (PyObject *o, PyObject *value, void *ignore) | |
223 | { | |
224 | pspace_object *self = (pspace_object *) o; | |
225 | ||
226 | if (! value) | |
227 | { | |
228 | PyErr_SetString (PyExc_TypeError, | |
229 | "cannot delete the pretty_printers attribute"); | |
230 | return -1; | |
231 | } | |
232 | ||
233 | if (! PyList_Check (value)) | |
234 | { | |
235 | PyErr_SetString (PyExc_TypeError, | |
236 | "the pretty_printers attribute must be a list"); | |
237 | return -1; | |
238 | } | |
239 | ||
240 | /* Take care in case the LHS and RHS are related somehow. */ | |
241 | gdbpy_ref<> tmp (self->printers); | |
242 | Py_INCREF (value); | |
243 | self->printers = value; | |
244 | ||
245 | return 0; | |
246 | } | |
247 | ||
248 | /* Return the Python dictionary attribute containing frame filters for | |
249 | this program space. */ | |
250 | PyObject * | |
251 | pspy_get_frame_filters (PyObject *o, void *ignore) | |
252 | { | |
253 | pspace_object *self = (pspace_object *) o; | |
254 | ||
255 | Py_INCREF (self->frame_filters); | |
256 | return self->frame_filters; | |
257 | } | |
258 | ||
259 | /* Set this object file's frame filters dictionary to FILTERS. */ | |
260 | static int | |
261 | pspy_set_frame_filters (PyObject *o, PyObject *frame, void *ignore) | |
262 | { | |
263 | pspace_object *self = (pspace_object *) o; | |
264 | ||
265 | if (! frame) | |
266 | { | |
267 | PyErr_SetString (PyExc_TypeError, | |
268 | "cannot delete the frame filter attribute"); | |
269 | return -1; | |
270 | } | |
271 | ||
272 | if (! PyDict_Check (frame)) | |
273 | { | |
274 | PyErr_SetString (PyExc_TypeError, | |
275 | "the frame filter attribute must be a dictionary"); | |
276 | return -1; | |
277 | } | |
278 | ||
279 | /* Take care in case the LHS and RHS are related somehow. */ | |
280 | gdbpy_ref<> tmp (self->frame_filters); | |
281 | Py_INCREF (frame); | |
282 | self->frame_filters = frame; | |
283 | ||
284 | return 0; | |
285 | } | |
286 | ||
287 | /* Return the list of the frame unwinders for this program space. */ | |
288 | ||
289 | PyObject * | |
290 | pspy_get_frame_unwinders (PyObject *o, void *ignore) | |
291 | { | |
292 | pspace_object *self = (pspace_object *) o; | |
293 | ||
294 | Py_INCREF (self->frame_unwinders); | |
295 | return self->frame_unwinders; | |
296 | } | |
297 | ||
298 | /* Set this program space's list of the unwinders to UNWINDERS. */ | |
299 | ||
300 | static int | |
301 | pspy_set_frame_unwinders (PyObject *o, PyObject *unwinders, void *ignore) | |
302 | { | |
303 | pspace_object *self = (pspace_object *) o; | |
304 | ||
305 | if (!unwinders) | |
306 | { | |
307 | PyErr_SetString (PyExc_TypeError, | |
308 | "cannot delete the frame unwinders list"); | |
309 | return -1; | |
310 | } | |
311 | ||
312 | if (!PyList_Check (unwinders)) | |
313 | { | |
314 | PyErr_SetString (PyExc_TypeError, | |
315 | "the frame unwinders attribute must be a list"); | |
316 | return -1; | |
317 | } | |
318 | ||
319 | /* Take care in case the LHS and RHS are related somehow. */ | |
320 | gdbpy_ref<> tmp (self->frame_unwinders); | |
321 | Py_INCREF (unwinders); | |
322 | self->frame_unwinders = unwinders; | |
323 | ||
324 | return 0; | |
325 | } | |
326 | ||
327 | /* Get the 'type_printers' attribute. */ | |
328 | ||
329 | static PyObject * | |
330 | pspy_get_type_printers (PyObject *o, void *ignore) | |
331 | { | |
332 | pspace_object *self = (pspace_object *) o; | |
333 | ||
334 | Py_INCREF (self->type_printers); | |
335 | return self->type_printers; | |
336 | } | |
337 | ||
338 | /* Get the 'xmethods' attribute. */ | |
339 | ||
340 | PyObject * | |
341 | pspy_get_xmethods (PyObject *o, void *ignore) | |
342 | { | |
343 | pspace_object *self = (pspace_object *) o; | |
344 | ||
345 | Py_INCREF (self->xmethods); | |
346 | return self->xmethods; | |
347 | } | |
348 | ||
349 | /* Return the list of missing debug handlers for this program space. */ | |
350 | ||
351 | static PyObject * | |
352 | pspy_get_missing_file_handlers (PyObject *o, void *ignore) | |
353 | { | |
354 | pspace_object *self = (pspace_object *) o; | |
355 | ||
356 | Py_INCREF (self->missing_file_handlers); | |
357 | return self->missing_file_handlers; | |
358 | } | |
359 | ||
360 | /* Set this program space's list of missing debug handlers to HANDLERS. */ | |
361 | ||
362 | static int | |
363 | pspy_set_missing_file_handlers (PyObject *o, PyObject *handlers, | |
364 | void *ignore) | |
365 | { | |
366 | pspace_object *self = (pspace_object *) o; | |
367 | ||
368 | if (handlers == nullptr) | |
369 | { | |
370 | PyErr_SetString (PyExc_TypeError, | |
371 | "cannot delete the missing debug handlers list"); | |
372 | return -1; | |
373 | } | |
374 | ||
375 | if (!PyList_Check (handlers)) | |
376 | { | |
377 | PyErr_SetString (PyExc_TypeError, | |
378 | "the missing debug handlers attribute must be a list"); | |
379 | return -1; | |
380 | } | |
381 | ||
382 | /* Take care in case the LHS and RHS are related somehow. */ | |
383 | gdbpy_ref<> tmp (self->missing_file_handlers); | |
384 | Py_INCREF (handlers); | |
385 | self->missing_file_handlers = handlers; | |
386 | ||
387 | return 0; | |
388 | } | |
389 | ||
390 | /* Set the 'type_printers' attribute. */ | |
391 | ||
392 | static int | |
393 | pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore) | |
394 | { | |
395 | pspace_object *self = (pspace_object *) o; | |
396 | ||
397 | if (! value) | |
398 | { | |
399 | PyErr_SetString (PyExc_TypeError, | |
400 | "cannot delete the type_printers attribute"); | |
401 | return -1; | |
402 | } | |
403 | ||
404 | if (! PyList_Check (value)) | |
405 | { | |
406 | PyErr_SetString (PyExc_TypeError, | |
407 | "the type_printers attribute must be a list"); | |
408 | return -1; | |
409 | } | |
410 | ||
411 | /* Take care in case the LHS and RHS are related somehow. */ | |
412 | gdbpy_ref<> tmp (self->type_printers); | |
413 | Py_INCREF (value); | |
414 | self->type_printers = value; | |
415 | ||
416 | return 0; | |
417 | } | |
418 | ||
419 | /* Implement the objfiles method. */ | |
420 | ||
421 | static PyObject * | |
422 | pspy_get_objfiles (PyObject *self_, PyObject *args) | |
423 | { | |
424 | pspace_object *self = (pspace_object *) self_; | |
425 | ||
426 | PSPY_REQUIRE_VALID (self); | |
427 | ||
428 | gdbpy_ref<> list (PyList_New (0)); | |
429 | if (list == NULL) | |
430 | return NULL; | |
431 | ||
432 | if (self->pspace != NULL) | |
433 | { | |
434 | for (objfile *objf : self->pspace->objfiles ()) | |
435 | { | |
436 | gdbpy_ref<> item = objfile_to_objfile_object (objf); | |
437 | ||
438 | if (item == nullptr | |
439 | || PyList_Append (list.get (), item.get ()) == -1) | |
440 | return NULL; | |
441 | } | |
442 | } | |
443 | ||
444 | return list.release (); | |
445 | } | |
446 | ||
447 | /* Implementation of solib_name (Long) -> String. | |
448 | Returns the name of the shared library holding a given address, or None. */ | |
449 | ||
450 | static PyObject * | |
451 | pspy_solib_name (PyObject *o, PyObject *args) | |
452 | { | |
453 | CORE_ADDR pc; | |
454 | PyObject *pc_obj; | |
455 | ||
456 | pspace_object *self = (pspace_object *) o; | |
457 | ||
458 | PSPY_REQUIRE_VALID (self); | |
459 | ||
460 | if (!PyArg_ParseTuple (args, "O", &pc_obj)) | |
461 | return NULL; | |
462 | if (get_addr_from_python (pc_obj, &pc) < 0) | |
463 | return nullptr; | |
464 | ||
465 | const char *soname = solib_name_from_address (self->pspace, pc); | |
466 | if (soname == nullptr) | |
467 | Py_RETURN_NONE; | |
468 | return host_string_to_python_string (soname).release (); | |
469 | } | |
470 | ||
471 | /* Implement objfile_for_address. */ | |
472 | ||
473 | static PyObject * | |
474 | pspy_objfile_for_address (PyObject *o, PyObject *args) | |
475 | { | |
476 | CORE_ADDR addr; | |
477 | PyObject *addr_obj; | |
478 | ||
479 | pspace_object *self = (pspace_object *) o; | |
480 | ||
481 | PSPY_REQUIRE_VALID (self); | |
482 | ||
483 | if (!PyArg_ParseTuple (args, "O", &addr_obj)) | |
484 | return nullptr; | |
485 | if (get_addr_from_python (addr_obj, &addr) < 0) | |
486 | return nullptr; | |
487 | ||
488 | struct objfile *objf = self->pspace->objfile_for_address (addr); | |
489 | if (objf == nullptr) | |
490 | Py_RETURN_NONE; | |
491 | ||
492 | return objfile_to_objfile_object (objf).release (); | |
493 | } | |
494 | ||
495 | /* Return the innermost lexical block containing the specified pc value, | |
496 | or 0 if there is none. */ | |
497 | static PyObject * | |
498 | pspy_block_for_pc (PyObject *o, PyObject *args) | |
499 | { | |
500 | pspace_object *self = (pspace_object *) o; | |
501 | CORE_ADDR pc; | |
502 | PyObject *pc_obj; | |
503 | const struct block *block = NULL; | |
504 | struct compunit_symtab *cust = NULL; | |
505 | ||
506 | PSPY_REQUIRE_VALID (self); | |
507 | ||
508 | if (!PyArg_ParseTuple (args, "O", &pc_obj)) | |
509 | return NULL; | |
510 | if (get_addr_from_python (pc_obj, &pc) < 0) | |
511 | return nullptr; | |
512 | ||
513 | try | |
514 | { | |
515 | scoped_restore_current_program_space saver; | |
516 | ||
517 | set_current_program_space (self->pspace); | |
518 | cust = find_pc_compunit_symtab (pc); | |
519 | ||
520 | if (cust != NULL && cust->objfile () != NULL) | |
521 | block = block_for_pc (pc); | |
522 | } | |
523 | catch (const gdb_exception &except) | |
524 | { | |
525 | return gdbpy_handle_gdb_exception (nullptr, except); | |
526 | } | |
527 | ||
528 | if (cust == NULL || cust->objfile () == NULL) | |
529 | Py_RETURN_NONE; | |
530 | ||
531 | if (block) | |
532 | return block_to_block_object (block, cust->objfile ()); | |
533 | ||
534 | Py_RETURN_NONE; | |
535 | } | |
536 | ||
537 | /* Implementation of the find_pc_line function. | |
538 | Returns the gdb.Symtab_and_line object corresponding to a PC value. */ | |
539 | ||
540 | static PyObject * | |
541 | pspy_find_pc_line (PyObject *o, PyObject *args) | |
542 | { | |
543 | CORE_ADDR pc; | |
544 | PyObject *result = NULL; /* init for gcc -Wall */ | |
545 | PyObject *pc_obj; | |
546 | pspace_object *self = (pspace_object *) o; | |
547 | ||
548 | PSPY_REQUIRE_VALID (self); | |
549 | ||
550 | if (!PyArg_ParseTuple (args, "O", &pc_obj)) | |
551 | return NULL; | |
552 | if (get_addr_from_python (pc_obj, &pc) < 0) | |
553 | return nullptr; | |
554 | ||
555 | try | |
556 | { | |
557 | struct symtab_and_line sal; | |
558 | scoped_restore_current_program_space saver; | |
559 | ||
560 | set_current_program_space (self->pspace); | |
561 | ||
562 | sal = find_pc_line (pc, 0); | |
563 | result = symtab_and_line_to_sal_object (sal); | |
564 | } | |
565 | catch (const gdb_exception &except) | |
566 | { | |
567 | return gdbpy_handle_gdb_exception (nullptr, except); | |
568 | } | |
569 | ||
570 | return result; | |
571 | } | |
572 | ||
573 | /* Implementation of is_valid (self) -> Boolean. | |
574 | Returns True if this program space still exists in GDB. */ | |
575 | ||
576 | static PyObject * | |
577 | pspy_is_valid (PyObject *o, PyObject *args) | |
578 | { | |
579 | pspace_object *self = (pspace_object *) o; | |
580 | ||
581 | if (self->pspace == NULL) | |
582 | Py_RETURN_FALSE; | |
583 | ||
584 | Py_RETURN_TRUE; | |
585 | } | |
586 | ||
587 | \f | |
588 | ||
589 | /* Return a new reference to the Python object of type Pspace | |
590 | representing PSPACE. If the object has already been created, | |
591 | return it. Otherwise, create it. Return NULL and set the Python | |
592 | error on failure. */ | |
593 | ||
594 | gdbpy_ref<> | |
595 | pspace_to_pspace_object (struct program_space *pspace) | |
596 | { | |
597 | PyObject *result = (PyObject *) pspy_pspace_data_key.get (pspace); | |
598 | if (result == NULL) | |
599 | { | |
600 | gdbpy_ref<pspace_object> object | |
601 | ((pspace_object *) PyObject_New (pspace_object, &pspace_object_type)); | |
602 | if (object == NULL) | |
603 | return NULL; | |
604 | if (!pspy_initialize (object.get ())) | |
605 | return NULL; | |
606 | ||
607 | object->pspace = pspace; | |
608 | pspy_pspace_data_key.set (pspace, object.get ()); | |
609 | result = (PyObject *) object.release (); | |
610 | } | |
611 | ||
612 | return gdbpy_ref<>::new_reference (result); | |
613 | } | |
614 | ||
615 | /* See python-internal.h. */ | |
616 | ||
617 | struct program_space * | |
618 | progspace_object_to_program_space (PyObject *obj) | |
619 | { | |
620 | gdb_assert (gdbpy_is_progspace (obj)); | |
621 | return ((pspace_object *) obj)->pspace; | |
622 | } | |
623 | ||
624 | /* See python-internal.h. */ | |
625 | ||
626 | bool | |
627 | gdbpy_is_progspace (PyObject *obj) | |
628 | { | |
629 | return PyObject_TypeCheck (obj, &pspace_object_type); | |
630 | } | |
631 | ||
632 | /* Emit an ExecutableChangedEvent event to REGISTRY. Return 0 on success, | |
633 | or a negative value on error. PSPACE is the program_space in which the | |
634 | current executable has changed, and RELOAD_P is true if the executable | |
635 | path stayed the same, but the file on disk changed, or false if the | |
636 | executable path actually changed. */ | |
637 | ||
638 | static int | |
639 | emit_executable_changed_event (eventregistry_object *registry, | |
640 | struct program_space *pspace, bool reload_p) | |
641 | { | |
642 | gdbpy_ref<> event_obj | |
643 | = create_event_object (&executable_changed_event_object_type); | |
644 | if (event_obj == nullptr) | |
645 | return -1; | |
646 | ||
647 | gdbpy_ref<> py_pspace = pspace_to_pspace_object (pspace); | |
648 | if (py_pspace == nullptr | |
649 | || evpy_add_attribute (event_obj.get (), "progspace", | |
650 | py_pspace.get ()) < 0) | |
651 | return -1; | |
652 | ||
653 | gdbpy_ref<> py_reload_p (PyBool_FromLong (reload_p ? 1 : 0)); | |
654 | if (py_reload_p == nullptr | |
655 | || evpy_add_attribute (event_obj.get (), "reload", | |
656 | py_reload_p.get ()) < 0) | |
657 | return -1; | |
658 | ||
659 | return evpy_emit_event (event_obj.get (), registry); | |
660 | } | |
661 | ||
662 | /* Listener for the executable_changed observable, this is called when the | |
663 | current executable within PSPACE changes. RELOAD_P is true if the | |
664 | executable path stayed the same but the file changed on disk. RELOAD_P | |
665 | is false if the executable path was changed. */ | |
666 | ||
667 | static void | |
668 | gdbpy_executable_changed (struct program_space *pspace, bool reload_p) | |
669 | { | |
670 | if (!gdb_python_initialized) | |
671 | return; | |
672 | ||
673 | gdbpy_enter enter_py; | |
674 | ||
675 | if (!evregpy_no_listeners_p (gdb_py_events.executable_changed)) | |
676 | if (emit_executable_changed_event (gdb_py_events.executable_changed, | |
677 | pspace, reload_p) < 0) | |
678 | gdbpy_print_stack (); | |
679 | } | |
680 | ||
681 | /* Helper function to emit NewProgspaceEvent (when ADDING_P is true) or | |
682 | FreeProgspaceEvent events (when ADDING_P is false). */ | |
683 | ||
684 | static void | |
685 | gdbpy_program_space_event (program_space *pspace, bool adding_p) | |
686 | { | |
687 | if (!gdb_python_initialized) | |
688 | return; | |
689 | ||
690 | gdbpy_enter enter_py; | |
691 | ||
692 | eventregistry_object *registry; | |
693 | PyTypeObject *event_type; | |
694 | if (adding_p) | |
695 | { | |
696 | registry = gdb_py_events.new_progspace; | |
697 | event_type = &new_progspace_event_object_type; | |
698 | } | |
699 | else | |
700 | { | |
701 | registry = gdb_py_events.free_progspace; | |
702 | event_type = &free_progspace_event_object_type; | |
703 | } | |
704 | ||
705 | if (evregpy_no_listeners_p (registry)) | |
706 | return; | |
707 | ||
708 | gdbpy_ref<> pspace_obj = pspace_to_pspace_object (pspace); | |
709 | if (pspace_obj == nullptr) | |
710 | { | |
711 | gdbpy_print_stack (); | |
712 | return; | |
713 | } | |
714 | ||
715 | gdbpy_ref<> event = create_event_object (event_type); | |
716 | if (event == nullptr | |
717 | || evpy_add_attribute (event.get (), "progspace", | |
718 | pspace_obj.get ()) < 0 | |
719 | || evpy_emit_event (event.get (), registry) < 0) | |
720 | gdbpy_print_stack (); | |
721 | } | |
722 | ||
723 | /* Emit a NewProgspaceEvent to indicate PSPACE has been created. */ | |
724 | ||
725 | static void | |
726 | gdbpy_new_program_space_event (program_space *pspace) | |
727 | { | |
728 | gdbpy_program_space_event (pspace, true); | |
729 | } | |
730 | ||
731 | /* Emit a FreeProgspaceEvent to indicate PSPACE is just about to be removed | |
732 | from GDB. */ | |
733 | ||
734 | static void | |
735 | gdbpy_free_program_space_event (program_space *pspace) | |
736 | { | |
737 | gdbpy_program_space_event (pspace, false); | |
738 | } | |
739 | ||
740 | static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION | |
741 | gdbpy_initialize_pspace (void) | |
742 | { | |
743 | gdb::observers::executable_changed.attach (gdbpy_executable_changed, | |
744 | "py-progspace"); | |
745 | gdb::observers::new_program_space.attach (gdbpy_new_program_space_event, | |
746 | "py-progspace"); | |
747 | gdb::observers::free_program_space.attach (gdbpy_free_program_space_event, | |
748 | "py-progspace"); | |
749 | ||
750 | if (gdbpy_type_ready (&pspace_object_type) < 0) | |
751 | return -1; | |
752 | ||
753 | return 0; | |
754 | } | |
755 | ||
756 | GDBPY_INITIALIZE_FILE (gdbpy_initialize_pspace); | |
757 | ||
758 | \f | |
759 | ||
760 | static gdb_PyGetSetDef pspace_getset[] = | |
761 | { | |
762 | { "__dict__", gdb_py_generic_dict, NULL, | |
763 | "The __dict__ for this progspace.", &pspace_object_type }, | |
764 | { "filename", pspy_get_filename, NULL, | |
765 | "The filename of the progspace's main symbol file, or None.", nullptr }, | |
766 | { "symbol_file", pspy_get_symbol_file, nullptr, | |
767 | "The gdb.Objfile for the progspace's main symbol file, or None.", | |
768 | nullptr}, | |
769 | { "executable_filename", pspy_get_exec_file, nullptr, | |
770 | "The filename for the progspace's executable, or None.", nullptr}, | |
771 | { "pretty_printers", pspy_get_printers, pspy_set_printers, | |
772 | "Pretty printers.", NULL }, | |
773 | { "frame_filters", pspy_get_frame_filters, pspy_set_frame_filters, | |
774 | "Frame filters.", NULL }, | |
775 | { "frame_unwinders", pspy_get_frame_unwinders, pspy_set_frame_unwinders, | |
776 | "Frame unwinders.", NULL }, | |
777 | { "type_printers", pspy_get_type_printers, pspy_set_type_printers, | |
778 | "Type printers.", NULL }, | |
779 | { "xmethods", pspy_get_xmethods, NULL, | |
780 | "Debug methods.", NULL }, | |
781 | { "missing_file_handlers", pspy_get_missing_file_handlers, | |
782 | pspy_set_missing_file_handlers, "Missing file handlers.", NULL }, | |
783 | { NULL } | |
784 | }; | |
785 | ||
786 | static PyMethodDef progspace_object_methods[] = | |
787 | { | |
788 | { "objfiles", pspy_get_objfiles, METH_NOARGS, | |
789 | "Return a sequence of objfiles associated to this program space." }, | |
790 | { "solib_name", pspy_solib_name, METH_VARARGS, | |
791 | "solib_name (Long) -> String.\n\ | |
792 | Return the name of the shared library holding a given address, or None." }, | |
793 | { "objfile_for_address", pspy_objfile_for_address, METH_VARARGS, | |
794 | "objfile_for_address (int) -> gdb.Objfile\n\ | |
795 | Return the objfile containing the given address, or None." }, | |
796 | { "block_for_pc", pspy_block_for_pc, METH_VARARGS, | |
797 | "Return the block containing the given pc value, or None." }, | |
798 | { "find_pc_line", pspy_find_pc_line, METH_VARARGS, | |
799 | "find_pc_line (pc) -> Symtab_and_line.\n\ | |
800 | Return the gdb.Symtab_and_line object corresponding to the pc value." }, | |
801 | { "is_valid", pspy_is_valid, METH_NOARGS, | |
802 | "is_valid () -> Boolean.\n\ | |
803 | Return true if this program space is valid, false if not." }, | |
804 | { NULL } | |
805 | }; | |
806 | ||
807 | PyTypeObject pspace_object_type = | |
808 | { | |
809 | PyVarObject_HEAD_INIT (NULL, 0) | |
810 | "gdb.Progspace", /*tp_name*/ | |
811 | sizeof (pspace_object), /*tp_basicsize*/ | |
812 | 0, /*tp_itemsize*/ | |
813 | pspy_dealloc, /*tp_dealloc*/ | |
814 | 0, /*tp_print*/ | |
815 | 0, /*tp_getattr*/ | |
816 | 0, /*tp_setattr*/ | |
817 | 0, /*tp_compare*/ | |
818 | 0, /*tp_repr*/ | |
819 | 0, /*tp_as_number*/ | |
820 | 0, /*tp_as_sequence*/ | |
821 | 0, /*tp_as_mapping*/ | |
822 | 0, /*tp_hash */ | |
823 | 0, /*tp_call*/ | |
824 | 0, /*tp_str*/ | |
825 | 0, /*tp_getattro*/ | |
826 | 0, /*tp_setattro*/ | |
827 | 0, /*tp_as_buffer*/ | |
828 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ | |
829 | "GDB progspace object", /* tp_doc */ | |
830 | 0, /* tp_traverse */ | |
831 | 0, /* tp_clear */ | |
832 | 0, /* tp_richcompare */ | |
833 | 0, /* tp_weaklistoffset */ | |
834 | 0, /* tp_iter */ | |
835 | 0, /* tp_iternext */ | |
836 | progspace_object_methods, /* tp_methods */ | |
837 | 0, /* tp_members */ | |
838 | pspace_getset, /* tp_getset */ | |
839 | 0, /* tp_base */ | |
840 | 0, /* tp_dict */ | |
841 | 0, /* tp_descr_get */ | |
842 | 0, /* tp_descr_set */ | |
843 | offsetof (pspace_object, dict), /* tp_dictoffset */ | |
844 | 0, /* tp_init */ | |
845 | 0, /* tp_alloc */ | |
846 | 0, /* tp_new */ | |
847 | }; |