]>
Commit | Line | Data |
---|---|---|
fa33c3cd DE |
1 | /* Python interface to program spaces. |
2 | ||
d01e8234 | 3 | Copyright (C) 2010-2025 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 | ||
fa33c3cd DE |
20 | #include "python-internal.h" |
21 | #include "charset.h" | |
22 | #include "progspace.h" | |
23 | #include "objfiles.h" | |
24 | #include "language.h" | |
b3422a0d | 25 | #include "arch-utils.h" |
8743a9cd TT |
26 | #include "solib.h" |
27 | #include "block.h" | |
42f297ad AB |
28 | #include "py-event.h" |
29 | #include "observable.h" | |
99d9c3b9 | 30 | #include "inferior.h" |
fa33c3cd | 31 | |
f99b5177 | 32 | struct pspace_object |
fa33c3cd DE |
33 | { |
34 | PyObject_HEAD | |
35 | ||
36 | /* The corresponding pspace. */ | |
37 | struct program_space *pspace; | |
38 | ||
02be9a71 DE |
39 | /* Dictionary holding user-added attributes. |
40 | This is the __dict__ attribute of the object. */ | |
41 | PyObject *dict; | |
42 | ||
fa33c3cd DE |
43 | /* The pretty-printer list of functions. */ |
44 | PyObject *printers; | |
18a9fc12 | 45 | |
1e611234 PM |
46 | /* The frame filter list of functions. */ |
47 | PyObject *frame_filters; | |
d11916aa SS |
48 | |
49 | /* The frame unwinder list. */ | |
50 | PyObject *frame_unwinders; | |
51 | ||
18a9fc12 TT |
52 | /* The type-printer list. */ |
53 | PyObject *type_printers; | |
883964a7 SC |
54 | |
55 | /* The debug method list. */ | |
56 | PyObject *xmethods; | |
8f6c452b | 57 | |
5cabc809 AB |
58 | /* The missing file handler list. */ |
59 | PyObject *missing_file_handlers; | |
f99b5177 | 60 | }; |
fa33c3cd | 61 | |
e36122e9 | 62 | extern PyTypeObject pspace_object_type |
62eec1a5 | 63 | CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object"); |
fa33c3cd | 64 | |
08b8a139 TT |
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" | |
99d9c3b9 | 75 | using the current inferior's arch suffices. |
08b8a139 TT |
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. */ | |
99d9c3b9 | 79 | gdbarch *arch = current_inferior ()->arch (); |
08b8a139 TT |
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; | |
fa33c3cd | 89 | |
0ae1a321 SM |
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) | |
fa33c3cd DE |
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; | |
d59b6f6c | 107 | |
fa33c3cd DE |
108 | if (obj->pspace) |
109 | { | |
110 | struct objfile *objfile = obj->pspace->symfile_object_file; | |
d59b6f6c | 111 | |
d31d2fc3 | 112 | if (objfile) |
833d985d TT |
113 | return (host_string_to_python_string (objfile_name (objfile)) |
114 | .release ()); | |
fa33c3cd DE |
115 | } |
116 | Py_RETURN_NONE; | |
117 | } | |
118 | ||
586ccf78 | 119 | /* Implement the gdb.Progspace.symbol_file attribute. Return the |
5ce85461 AB |
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 | ||
586ccf78 | 139 | /* Implement the gdb.Progspace.executable_filename attribute. Return a |
4e02aca0 AB |
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 | ||
9ad8c583 | 151 | const char *filename = obj->pspace->exec_filename (); |
4e02aca0 AB |
152 | if (filename != nullptr) |
153 | return host_string_to_python_string (filename).release (); | |
154 | ||
155 | Py_RETURN_NONE; | |
156 | } | |
157 | ||
fa33c3cd DE |
158 | static void |
159 | pspy_dealloc (PyObject *self) | |
160 | { | |
161 | pspace_object *ps_self = (pspace_object *) self; | |
d59b6f6c | 162 | |
02be9a71 | 163 | Py_XDECREF (ps_self->dict); |
fa33c3cd | 164 | Py_XDECREF (ps_self->printers); |
1e611234 | 165 | Py_XDECREF (ps_self->frame_filters); |
d11916aa | 166 | Py_XDECREF (ps_self->frame_unwinders); |
18a9fc12 | 167 | Py_XDECREF (ps_self->type_printers); |
883964a7 | 168 | Py_XDECREF (ps_self->xmethods); |
5cabc809 | 169 | Py_XDECREF (ps_self->missing_file_handlers); |
9a27f2c6 | 170 | Py_TYPE (self)->tp_free (self); |
fa33c3cd DE |
171 | } |
172 | ||
4e1bbde0 DE |
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; | |
0f6ed0e0 TT |
180 | |
181 | self->dict = PyDict_New (); | |
182 | if (self->dict == NULL) | |
183 | return 0; | |
4e1bbde0 DE |
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 | ||
d11916aa SS |
193 | self->frame_unwinders = PyList_New (0); |
194 | if (self->frame_unwinders == NULL) | |
195 | return 0; | |
196 | ||
4e1bbde0 DE |
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 | ||
5cabc809 AB |
205 | self->missing_file_handlers = PyList_New (0); |
206 | if (self->missing_file_handlers == nullptr) | |
8f6c452b AB |
207 | return 0; |
208 | ||
4e1bbde0 DE |
209 | return 1; |
210 | } | |
211 | ||
fa33c3cd DE |
212 | PyObject * |
213 | pspy_get_printers (PyObject *o, void *ignore) | |
214 | { | |
215 | pspace_object *self = (pspace_object *) o; | |
d59b6f6c | 216 | |
fa33c3cd DE |
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 | { | |
fa33c3cd | 224 | pspace_object *self = (pspace_object *) o; |
d59b6f6c | 225 | |
fa33c3cd DE |
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. */ | |
2a3c71d6 | 241 | gdbpy_ref<> tmp (self->printers); |
fa33c3cd DE |
242 | Py_INCREF (value); |
243 | self->printers = value; | |
fa33c3cd DE |
244 | |
245 | return 0; | |
246 | } | |
247 | ||
1e611234 PM |
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 | { | |
1e611234 PM |
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. */ | |
2a3c71d6 | 280 | gdbpy_ref<> tmp (self->frame_filters); |
1e611234 PM |
281 | Py_INCREF (frame); |
282 | self->frame_filters = frame; | |
1e611234 PM |
283 | |
284 | return 0; | |
285 | } | |
286 | ||
d11916aa SS |
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 | { | |
d11916aa SS |
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. */ | |
2a3c71d6 | 320 | gdbpy_ref<> tmp (self->frame_unwinders); |
d11916aa SS |
321 | Py_INCREF (unwinders); |
322 | self->frame_unwinders = unwinders; | |
d11916aa SS |
323 | |
324 | return 0; | |
325 | } | |
326 | ||
18a9fc12 TT |
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 | ||
883964a7 SC |
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 | ||
8f6c452b AB |
349 | /* Return the list of missing debug handlers for this program space. */ |
350 | ||
351 | static PyObject * | |
5cabc809 | 352 | pspy_get_missing_file_handlers (PyObject *o, void *ignore) |
8f6c452b AB |
353 | { |
354 | pspace_object *self = (pspace_object *) o; | |
355 | ||
5cabc809 AB |
356 | Py_INCREF (self->missing_file_handlers); |
357 | return self->missing_file_handlers; | |
8f6c452b AB |
358 | } |
359 | ||
360 | /* Set this program space's list of missing debug handlers to HANDLERS. */ | |
361 | ||
362 | static int | |
5cabc809 | 363 | pspy_set_missing_file_handlers (PyObject *o, PyObject *handlers, |
8f6c452b AB |
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. */ | |
5cabc809 | 383 | gdbpy_ref<> tmp (self->missing_file_handlers); |
8f6c452b | 384 | Py_INCREF (handlers); |
5cabc809 | 385 | self->missing_file_handlers = handlers; |
8f6c452b AB |
386 | |
387 | return 0; | |
388 | } | |
389 | ||
18a9fc12 TT |
390 | /* Set the 'type_printers' attribute. */ |
391 | ||
392 | static int | |
393 | pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore) | |
394 | { | |
18a9fc12 TT |
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. */ | |
2a3c71d6 | 412 | gdbpy_ref<> tmp (self->type_printers); |
18a9fc12 TT |
413 | Py_INCREF (value); |
414 | self->type_printers = value; | |
18a9fc12 TT |
415 | |
416 | return 0; | |
417 | } | |
418 | ||
0ae1a321 SM |
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 | ||
8743a9cd TT |
428 | gdbpy_ref<> list (PyList_New (0)); |
429 | if (list == NULL) | |
430 | return NULL; | |
431 | ||
432 | if (self->pspace != NULL) | |
433 | { | |
2030c079 | 434 | for (objfile *objf : self->pspace->objfiles ()) |
8743a9cd | 435 | { |
0a9db5ad | 436 | gdbpy_ref<> item = objfile_to_objfile_object (objf); |
8743a9cd | 437 | |
0a9db5ad TT |
438 | if (item == nullptr |
439 | || PyList_Append (list.get (), item.get ()) == -1) | |
8743a9cd TT |
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 | { | |
d19ca0b3 TT |
453 | CORE_ADDR pc; |
454 | PyObject *pc_obj; | |
455 | ||
8743a9cd TT |
456 | pspace_object *self = (pspace_object *) o; |
457 | ||
458 | PSPY_REQUIRE_VALID (self); | |
459 | ||
d19ca0b3 | 460 | if (!PyArg_ParseTuple (args, "O", &pc_obj)) |
8743a9cd | 461 | return NULL; |
d19ca0b3 TT |
462 | if (get_addr_from_python (pc_obj, &pc) < 0) |
463 | return nullptr; | |
8743a9cd | 464 | |
6d08aed3 | 465 | const char *soname = solib_name_from_address (self->pspace, pc); |
8743a9cd TT |
466 | if (soname == nullptr) |
467 | Py_RETURN_NONE; | |
833d985d | 468 | return host_string_to_python_string (soname).release (); |
0ae1a321 | 469 | } |
fa33c3cd | 470 | |
27b2eff1 TT |
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 | ||
8743a9cd TT |
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; | |
d19ca0b3 TT |
501 | CORE_ADDR pc; |
502 | PyObject *pc_obj; | |
8743a9cd TT |
503 | const struct block *block = NULL; |
504 | struct compunit_symtab *cust = NULL; | |
505 | ||
506 | PSPY_REQUIRE_VALID (self); | |
507 | ||
d19ca0b3 | 508 | if (!PyArg_ParseTuple (args, "O", &pc_obj)) |
8743a9cd | 509 | return NULL; |
d19ca0b3 TT |
510 | if (get_addr_from_python (pc_obj, &pc) < 0) |
511 | return nullptr; | |
8743a9cd | 512 | |
a70b8144 | 513 | try |
8743a9cd TT |
514 | { |
515 | scoped_restore_current_program_space saver; | |
516 | ||
517 | set_current_program_space (self->pspace); | |
518 | cust = find_pc_compunit_symtab (pc); | |
519 | ||
9821f3fa | 520 | if (cust != NULL && cust->objfile () != NULL) |
8743a9cd TT |
521 | block = block_for_pc (pc); |
522 | } | |
230d2906 | 523 | catch (const gdb_exception &except) |
8743a9cd | 524 | { |
1ccb6f10 | 525 | return gdbpy_handle_gdb_exception (nullptr, except); |
8743a9cd | 526 | } |
8743a9cd | 527 | |
9821f3fa | 528 | if (cust == NULL || cust->objfile () == NULL) |
33d569b7 | 529 | Py_RETURN_NONE; |
8743a9cd TT |
530 | |
531 | if (block) | |
9821f3fa | 532 | return block_to_block_object (block, cust->objfile ()); |
8743a9cd TT |
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 | { | |
d19ca0b3 | 543 | CORE_ADDR pc; |
8743a9cd | 544 | PyObject *result = NULL; /* init for gcc -Wall */ |
d19ca0b3 | 545 | PyObject *pc_obj; |
8743a9cd TT |
546 | pspace_object *self = (pspace_object *) o; |
547 | ||
548 | PSPY_REQUIRE_VALID (self); | |
549 | ||
d19ca0b3 | 550 | if (!PyArg_ParseTuple (args, "O", &pc_obj)) |
8743a9cd | 551 | return NULL; |
d19ca0b3 TT |
552 | if (get_addr_from_python (pc_obj, &pc) < 0) |
553 | return nullptr; | |
8743a9cd | 554 | |
a70b8144 | 555 | try |
8743a9cd TT |
556 | { |
557 | struct symtab_and_line sal; | |
8743a9cd TT |
558 | scoped_restore_current_program_space saver; |
559 | ||
560 | set_current_program_space (self->pspace); | |
561 | ||
8743a9cd TT |
562 | sal = find_pc_line (pc, 0); |
563 | result = symtab_and_line_to_sal_object (sal); | |
564 | } | |
230d2906 | 565 | catch (const gdb_exception &except) |
8743a9cd | 566 | { |
1ccb6f10 | 567 | return gdbpy_handle_gdb_exception (nullptr, except); |
8743a9cd | 568 | } |
8743a9cd TT |
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 | ||
3c7aa307 | 589 | /* Return a new reference to the Python object of type Pspace |
fa33c3cd DE |
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 | ||
3c7aa307 | 594 | gdbpy_ref<> |
fa33c3cd DE |
595 | pspace_to_pspace_object (struct program_space *pspace) |
596 | { | |
08b8a139 | 597 | PyObject *result = (PyObject *) pspy_pspace_data_key.get (pspace); |
3c7aa307 | 598 | if (result == NULL) |
fa33c3cd | 599 | { |
3c7aa307 TT |
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; | |
883964a7 | 606 | |
3c7aa307 | 607 | object->pspace = pspace; |
08b8a139 | 608 | pspy_pspace_data_key.set (pspace, object.get ()); |
3c7aa307 | 609 | result = (PyObject *) object.release (); |
fa33c3cd DE |
610 | } |
611 | ||
3c7aa307 | 612 | return gdbpy_ref<>::new_reference (result); |
fa33c3cd DE |
613 | } |
614 | ||
25209e2c AB |
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 | ||
42f297ad AB |
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 | ||
59912fb2 AB |
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 | ||
3965bff5 | 740 | static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION |
8e3685bf AB |
741 | gdbpy_initialize_pspace (void) |
742 | { | |
42f297ad AB |
743 | gdb::observers::executable_changed.attach (gdbpy_executable_changed, |
744 | "py-progspace"); | |
59912fb2 AB |
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"); | |
42f297ad | 749 | |
336bb2a1 | 750 | if (gdbpy_type_ready (&pspace_object_type) < 0) |
999633ed | 751 | return -1; |
fa33c3cd | 752 | |
336bb2a1 | 753 | return 0; |
fa33c3cd DE |
754 | } |
755 | ||
3965bff5 AB |
756 | GDBPY_INITIALIZE_FILE (gdbpy_initialize_pspace); |
757 | ||
fa33c3cd DE |
758 | \f |
759 | ||
0d1f4ceb | 760 | static gdb_PyGetSetDef pspace_getset[] = |
fa33c3cd | 761 | { |
02be9a71 DE |
762 | { "__dict__", gdb_py_generic_dict, NULL, |
763 | "The __dict__ for this progspace.", &pspace_object_type }, | |
fa33c3cd | 764 | { "filename", pspy_get_filename, NULL, |
ce65386d | 765 | "The filename of the progspace's main symbol file, or None.", nullptr }, |
5ce85461 AB |
766 | { "symbol_file", pspy_get_symbol_file, nullptr, |
767 | "The gdb.Objfile for the progspace's main symbol file, or None.", | |
768 | nullptr}, | |
4e02aca0 AB |
769 | { "executable_filename", pspy_get_exec_file, nullptr, |
770 | "The filename for the progspace's executable, or None.", nullptr}, | |
fa33c3cd DE |
771 | { "pretty_printers", pspy_get_printers, pspy_set_printers, |
772 | "Pretty printers.", NULL }, | |
1e611234 PM |
773 | { "frame_filters", pspy_get_frame_filters, pspy_set_frame_filters, |
774 | "Frame filters.", NULL }, | |
d11916aa SS |
775 | { "frame_unwinders", pspy_get_frame_unwinders, pspy_set_frame_unwinders, |
776 | "Frame unwinders.", NULL }, | |
18a9fc12 TT |
777 | { "type_printers", pspy_get_type_printers, pspy_set_type_printers, |
778 | "Type printers.", NULL }, | |
883964a7 SC |
779 | { "xmethods", pspy_get_xmethods, NULL, |
780 | "Debug methods.", NULL }, | |
5cabc809 AB |
781 | { "missing_file_handlers", pspy_get_missing_file_handlers, |
782 | pspy_set_missing_file_handlers, "Missing file handlers.", NULL }, | |
fa33c3cd DE |
783 | { NULL } |
784 | }; | |
785 | ||
0ae1a321 SM |
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." }, | |
8743a9cd TT |
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." }, | |
27b2eff1 TT |
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." }, | |
8743a9cd TT |
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." }, | |
0ae1a321 SM |
804 | { NULL } |
805 | }; | |
806 | ||
e36122e9 | 807 | PyTypeObject pspace_object_type = |
fa33c3cd | 808 | { |
9a27f2c6 | 809 | PyVarObject_HEAD_INIT (NULL, 0) |
fa33c3cd DE |
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 */ | |
0ae1a321 | 836 | progspace_object_methods, /* tp_methods */ |
fa33c3cd DE |
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 */ | |
02be9a71 | 843 | offsetof (pspace_object, dict), /* tp_dictoffset */ |
fa33c3cd DE |
844 | 0, /* tp_init */ |
845 | 0, /* tp_alloc */ | |
2f47f48f | 846 | 0, /* tp_new */ |
fa33c3cd | 847 | }; |