]>
Commit | Line | Data |
---|---|---|
89c73ade TT |
1 | /* Python interface to objfiles. |
2 | ||
4a94e368 | 3 | Copyright (C) 2008-2022 Free Software Foundation, Inc. |
89c73ade TT |
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 "objfiles.h" | |
d452c4bc | 24 | #include "language.h" |
7c50a931 | 25 | #include "build-id.h" |
6dddd6a5 | 26 | #include "symtab.h" |
85933f7c | 27 | #include "python.h" |
89c73ade | 28 | |
f99b5177 | 29 | struct objfile_object |
89c73ade TT |
30 | { |
31 | PyObject_HEAD | |
32 | ||
33 | /* The corresponding objfile. */ | |
34 | struct objfile *objfile; | |
35 | ||
02be9a71 DE |
36 | /* Dictionary holding user-added attributes. |
37 | This is the __dict__ attribute of the object. */ | |
38 | PyObject *dict; | |
39 | ||
89c73ade TT |
40 | /* The pretty-printer list of functions. */ |
41 | PyObject *printers; | |
18a9fc12 | 42 | |
1e611234 PM |
43 | /* The frame filter list of functions. */ |
44 | PyObject *frame_filters; | |
d11916aa SS |
45 | |
46 | /* The list of frame unwinders. */ | |
47 | PyObject *frame_unwinders; | |
48 | ||
18a9fc12 TT |
49 | /* The type-printer list. */ |
50 | PyObject *type_printers; | |
883964a7 SC |
51 | |
52 | /* The debug method matcher list. */ | |
53 | PyObject *xmethods; | |
f99b5177 | 54 | }; |
89c73ade | 55 | |
e36122e9 | 56 | extern PyTypeObject objfile_object_type |
62eec1a5 | 57 | CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("objfile_object"); |
89c73ade | 58 | |
08b8a139 TT |
59 | /* Clear the OBJFILE pointer in an Objfile object and remove the |
60 | reference. */ | |
61 | struct objfpy_deleter | |
62 | { | |
63 | void operator() (objfile_object *obj) | |
64 | { | |
65 | gdbpy_enter enter_py; | |
66 | gdbpy_ref<objfile_object> object (obj); | |
67 | object->objfile = nullptr; | |
68 | } | |
69 | }; | |
70 | ||
71 | static const registry<objfile>::key<objfile_object, objfpy_deleter> | |
72 | objfpy_objfile_data_key; | |
89c73ade | 73 | |
7c50a931 DE |
74 | /* Require that OBJF be a valid objfile. */ |
75 | #define OBJFPY_REQUIRE_VALID(obj) \ | |
76 | do { \ | |
77 | if (!(obj)->objfile) \ | |
78 | { \ | |
79 | PyErr_SetString (PyExc_RuntimeError, \ | |
80 | _("Objfile no longer exists.")); \ | |
81 | return NULL; \ | |
82 | } \ | |
83 | } while (0) | |
84 | ||
89c73ade TT |
85 | \f |
86 | ||
87 | /* An Objfile method which returns the objfile's file name, or None. */ | |
7c50a931 | 88 | |
89c73ade TT |
89 | static PyObject * |
90 | objfpy_get_filename (PyObject *self, void *closure) | |
91 | { | |
92 | objfile_object *obj = (objfile_object *) self; | |
d59b6f6c | 93 | |
d31d2fc3 | 94 | if (obj->objfile) |
833d985d TT |
95 | return (host_string_to_python_string (objfile_name (obj->objfile)) |
96 | .release ()); | |
89c73ade TT |
97 | Py_RETURN_NONE; |
98 | } | |
99 | ||
3a8b707a DE |
100 | /* An Objfile method which returns the objfile's file name, as specified |
101 | by the user, or None. */ | |
102 | ||
103 | static PyObject * | |
104 | objfpy_get_username (PyObject *self, void *closure) | |
105 | { | |
106 | objfile_object *obj = (objfile_object *) self; | |
107 | ||
108 | if (obj->objfile) | |
109 | { | |
110 | const char *username = obj->objfile->original_name; | |
111 | ||
833d985d | 112 | return host_string_to_python_string (username).release (); |
3a8b707a DE |
113 | } |
114 | ||
115 | Py_RETURN_NONE; | |
116 | } | |
117 | ||
99298c95 TT |
118 | /* Get the 'is_file' attribute. */ |
119 | ||
120 | static PyObject * | |
121 | objfpy_get_is_file (PyObject *o, void *ignore) | |
122 | { | |
123 | objfile_object *self = (objfile_object *) o; | |
124 | ||
125 | if (self->objfile != nullptr) | |
126 | return PyBool_FromLong ((self->objfile->flags & OBJF_NOT_FILENAME) == 0); | |
127 | Py_RETURN_NONE; | |
128 | } | |
129 | ||
a0be3e44 DE |
130 | /* If SELF is a separate debug-info file, return the "backlink" field. |
131 | Otherwise return None. */ | |
132 | ||
133 | static PyObject * | |
134 | objfpy_get_owner (PyObject *self, void *closure) | |
135 | { | |
136 | objfile_object *obj = (objfile_object *) self; | |
137 | struct objfile *objfile = obj->objfile; | |
138 | struct objfile *owner; | |
139 | ||
140 | OBJFPY_REQUIRE_VALID (obj); | |
141 | ||
142 | owner = objfile->separate_debug_objfile_backlink; | |
a0be3e44 | 143 | if (owner != NULL) |
0a9db5ad | 144 | return objfile_to_objfile_object (owner).release (); |
a0be3e44 DE |
145 | Py_RETURN_NONE; |
146 | } | |
147 | ||
7c50a931 DE |
148 | /* An Objfile method which returns the objfile's build id, or None. */ |
149 | ||
150 | static PyObject * | |
151 | objfpy_get_build_id (PyObject *self, void *closure) | |
152 | { | |
153 | objfile_object *obj = (objfile_object *) self; | |
154 | struct objfile *objfile = obj->objfile; | |
c74f7d1c | 155 | const struct bfd_build_id *build_id = NULL; |
7c50a931 DE |
156 | |
157 | OBJFPY_REQUIRE_VALID (obj); | |
158 | ||
a70b8144 | 159 | try |
7c50a931 | 160 | { |
98badbfd | 161 | build_id = build_id_bfd_get (objfile->obfd.get ()); |
7c50a931 | 162 | } |
230d2906 | 163 | catch (const gdb_exception &except) |
492d29ea PA |
164 | { |
165 | GDB_PY_HANDLE_EXCEPTION (except); | |
166 | } | |
7c50a931 DE |
167 | |
168 | if (build_id != NULL) | |
169 | { | |
858f25f0 | 170 | std::string hex_form = bin2hex (build_id->data, build_id->size); |
7c50a931 | 171 | |
858f25f0 | 172 | return host_string_to_python_string (hex_form.c_str ()).release (); |
7c50a931 DE |
173 | } |
174 | ||
175 | Py_RETURN_NONE; | |
176 | } | |
177 | ||
d096d8c1 DE |
178 | /* An Objfile method which returns the objfile's progspace, or None. */ |
179 | ||
180 | static PyObject * | |
181 | objfpy_get_progspace (PyObject *self, void *closure) | |
182 | { | |
183 | objfile_object *obj = (objfile_object *) self; | |
184 | ||
185 | if (obj->objfile) | |
3c7aa307 | 186 | return pspace_to_pspace_object (obj->objfile->pspace).release (); |
d096d8c1 DE |
187 | |
188 | Py_RETURN_NONE; | |
189 | } | |
190 | ||
89c73ade TT |
191 | static void |
192 | objfpy_dealloc (PyObject *o) | |
193 | { | |
194 | objfile_object *self = (objfile_object *) o; | |
d59b6f6c | 195 | |
02be9a71 | 196 | Py_XDECREF (self->dict); |
89c73ade | 197 | Py_XDECREF (self->printers); |
1e611234 | 198 | Py_XDECREF (self->frame_filters); |
d11916aa | 199 | Py_XDECREF (self->frame_unwinders); |
18a9fc12 | 200 | Py_XDECREF (self->type_printers); |
883964a7 | 201 | Py_XDECREF (self->xmethods); |
9a27f2c6 | 202 | Py_TYPE (self)->tp_free (self); |
89c73ade TT |
203 | } |
204 | ||
4e1bbde0 DE |
205 | /* Initialize an objfile_object. |
206 | The result is a boolean indicating success. */ | |
207 | ||
208 | static int | |
209 | objfpy_initialize (objfile_object *self) | |
210 | { | |
211 | self->objfile = NULL; | |
0f6ed0e0 TT |
212 | |
213 | self->dict = PyDict_New (); | |
214 | if (self->dict == NULL) | |
215 | return 0; | |
4e1bbde0 DE |
216 | |
217 | self->printers = PyList_New (0); | |
218 | if (self->printers == NULL) | |
219 | return 0; | |
220 | ||
221 | self->frame_filters = PyDict_New (); | |
222 | if (self->frame_filters == NULL) | |
223 | return 0; | |
224 | ||
d11916aa SS |
225 | self->frame_unwinders = PyList_New (0); |
226 | if (self->frame_unwinders == NULL) | |
227 | return 0; | |
228 | ||
4e1bbde0 DE |
229 | self->type_printers = PyList_New (0); |
230 | if (self->type_printers == NULL) | |
231 | return 0; | |
232 | ||
233 | self->xmethods = PyList_New (0); | |
234 | if (self->xmethods == NULL) | |
235 | return 0; | |
236 | ||
237 | return 1; | |
238 | } | |
239 | ||
89c73ade TT |
240 | static PyObject * |
241 | objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords) | |
242 | { | |
88b6faea | 243 | gdbpy_ref<objfile_object> self ((objfile_object *) type->tp_alloc (type, 0)); |
d59b6f6c | 244 | |
88b6faea | 245 | if (self != NULL) |
89c73ade | 246 | { |
88b6faea TT |
247 | if (!objfpy_initialize (self.get ())) |
248 | return NULL; | |
89c73ade | 249 | } |
4e1bbde0 | 250 | |
88b6faea | 251 | return (PyObject *) self.release (); |
89c73ade TT |
252 | } |
253 | ||
254 | PyObject * | |
255 | objfpy_get_printers (PyObject *o, void *ignore) | |
256 | { | |
257 | objfile_object *self = (objfile_object *) o; | |
d59b6f6c | 258 | |
89c73ade TT |
259 | Py_INCREF (self->printers); |
260 | return self->printers; | |
261 | } | |
262 | ||
263 | static int | |
264 | objfpy_set_printers (PyObject *o, PyObject *value, void *ignore) | |
265 | { | |
89c73ade | 266 | objfile_object *self = (objfile_object *) o; |
d59b6f6c | 267 | |
89c73ade TT |
268 | if (! value) |
269 | { | |
270 | PyErr_SetString (PyExc_TypeError, | |
044c0f87 | 271 | _("Cannot delete the pretty_printers attribute.")); |
89c73ade TT |
272 | return -1; |
273 | } | |
274 | ||
275 | if (! PyList_Check (value)) | |
276 | { | |
277 | PyErr_SetString (PyExc_TypeError, | |
044c0f87 | 278 | _("The pretty_printers attribute must be a list.")); |
89c73ade TT |
279 | return -1; |
280 | } | |
281 | ||
282 | /* Take care in case the LHS and RHS are related somehow. */ | |
2a3c71d6 | 283 | gdbpy_ref<> tmp (self->printers); |
89c73ade TT |
284 | Py_INCREF (value); |
285 | self->printers = value; | |
89c73ade TT |
286 | |
287 | return 0; | |
288 | } | |
289 | ||
1e611234 PM |
290 | /* Return the Python dictionary attribute containing frame filters for |
291 | this object file. */ | |
292 | PyObject * | |
293 | objfpy_get_frame_filters (PyObject *o, void *ignore) | |
294 | { | |
295 | objfile_object *self = (objfile_object *) o; | |
296 | ||
297 | Py_INCREF (self->frame_filters); | |
298 | return self->frame_filters; | |
299 | } | |
300 | ||
301 | /* Set this object file's frame filters dictionary to FILTERS. */ | |
302 | static int | |
303 | objfpy_set_frame_filters (PyObject *o, PyObject *filters, void *ignore) | |
304 | { | |
1e611234 PM |
305 | objfile_object *self = (objfile_object *) o; |
306 | ||
307 | if (! filters) | |
308 | { | |
309 | PyErr_SetString (PyExc_TypeError, | |
310 | _("Cannot delete the frame filters attribute.")); | |
311 | return -1; | |
312 | } | |
313 | ||
314 | if (! PyDict_Check (filters)) | |
315 | { | |
316 | PyErr_SetString (PyExc_TypeError, | |
317 | _("The frame_filters attribute must be a dictionary.")); | |
318 | return -1; | |
319 | } | |
320 | ||
321 | /* Take care in case the LHS and RHS are related somehow. */ | |
2a3c71d6 | 322 | gdbpy_ref<> tmp (self->frame_filters); |
1e611234 PM |
323 | Py_INCREF (filters); |
324 | self->frame_filters = filters; | |
1e611234 PM |
325 | |
326 | return 0; | |
327 | } | |
328 | ||
d11916aa SS |
329 | /* Return the frame unwinders attribute for this object file. */ |
330 | ||
331 | PyObject * | |
332 | objfpy_get_frame_unwinders (PyObject *o, void *ignore) | |
333 | { | |
334 | objfile_object *self = (objfile_object *) o; | |
335 | ||
336 | Py_INCREF (self->frame_unwinders); | |
337 | return self->frame_unwinders; | |
338 | } | |
339 | ||
340 | /* Set this object file's frame unwinders list to UNWINDERS. */ | |
341 | ||
342 | static int | |
343 | objfpy_set_frame_unwinders (PyObject *o, PyObject *unwinders, void *ignore) | |
344 | { | |
d11916aa SS |
345 | objfile_object *self = (objfile_object *) o; |
346 | ||
347 | if (!unwinders) | |
348 | { | |
349 | PyErr_SetString (PyExc_TypeError, | |
350 | _("Cannot delete the frame unwinders attribute.")); | |
351 | return -1; | |
352 | } | |
353 | ||
354 | if (!PyList_Check (unwinders)) | |
355 | { | |
356 | PyErr_SetString (PyExc_TypeError, | |
357 | _("The frame_unwinders attribute must be a list.")); | |
358 | return -1; | |
359 | } | |
360 | ||
361 | /* Take care in case the LHS and RHS are related somehow. */ | |
2a3c71d6 | 362 | gdbpy_ref<> tmp (self->frame_unwinders); |
d11916aa SS |
363 | Py_INCREF (unwinders); |
364 | self->frame_unwinders = unwinders; | |
d11916aa SS |
365 | |
366 | return 0; | |
367 | } | |
368 | ||
18a9fc12 TT |
369 | /* Get the 'type_printers' attribute. */ |
370 | ||
371 | static PyObject * | |
372 | objfpy_get_type_printers (PyObject *o, void *ignore) | |
373 | { | |
374 | objfile_object *self = (objfile_object *) o; | |
375 | ||
376 | Py_INCREF (self->type_printers); | |
377 | return self->type_printers; | |
378 | } | |
379 | ||
883964a7 SC |
380 | /* Get the 'xmethods' attribute. */ |
381 | ||
382 | PyObject * | |
383 | objfpy_get_xmethods (PyObject *o, void *ignore) | |
384 | { | |
385 | objfile_object *self = (objfile_object *) o; | |
386 | ||
387 | Py_INCREF (self->xmethods); | |
388 | return self->xmethods; | |
389 | } | |
390 | ||
18a9fc12 TT |
391 | /* Set the 'type_printers' attribute. */ |
392 | ||
393 | static int | |
394 | objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore) | |
395 | { | |
18a9fc12 TT |
396 | objfile_object *self = (objfile_object *) o; |
397 | ||
398 | if (! value) | |
399 | { | |
400 | PyErr_SetString (PyExc_TypeError, | |
401 | _("Cannot delete the type_printers attribute.")); | |
402 | return -1; | |
403 | } | |
404 | ||
405 | if (! PyList_Check (value)) | |
406 | { | |
407 | PyErr_SetString (PyExc_TypeError, | |
408 | _("The type_printers attribute must be a list.")); | |
409 | return -1; | |
410 | } | |
411 | ||
412 | /* Take care in case the LHS and RHS are related somehow. */ | |
2a3c71d6 | 413 | gdbpy_ref<> tmp (self->type_printers); |
18a9fc12 TT |
414 | Py_INCREF (value); |
415 | self->type_printers = value; | |
18a9fc12 TT |
416 | |
417 | return 0; | |
418 | } | |
419 | ||
29703da4 PM |
420 | /* Implementation of gdb.Objfile.is_valid (self) -> Boolean. |
421 | Returns True if this object file still exists in GDB. */ | |
422 | ||
423 | static PyObject * | |
424 | objfpy_is_valid (PyObject *self, PyObject *args) | |
425 | { | |
426 | objfile_object *obj = (objfile_object *) self; | |
427 | ||
428 | if (! obj->objfile) | |
429 | Py_RETURN_FALSE; | |
430 | ||
431 | Py_RETURN_TRUE; | |
432 | } | |
433 | ||
f32feb4a | 434 | /* Implementation of gdb.Objfile.add_separate_debug_file (self, string). */ |
86e4ed39 DE |
435 | |
436 | static PyObject * | |
437 | objfpy_add_separate_debug_file (PyObject *self, PyObject *args, PyObject *kw) | |
438 | { | |
2adadf51 | 439 | static const char *keywords[] = { "file_name", NULL }; |
86e4ed39 DE |
440 | objfile_object *obj = (objfile_object *) self; |
441 | const char *file_name; | |
86e4ed39 DE |
442 | |
443 | OBJFPY_REQUIRE_VALID (obj); | |
444 | ||
2adadf51 | 445 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s", keywords, &file_name)) |
86e4ed39 DE |
446 | return NULL; |
447 | ||
a70b8144 | 448 | try |
86e4ed39 | 449 | { |
192b62ce | 450 | gdb_bfd_ref_ptr abfd (symfile_bfd_open (file_name)); |
86e4ed39 | 451 | |
98badbfd | 452 | symbol_file_add_separate (abfd, file_name, 0, obj->objfile); |
86e4ed39 | 453 | } |
230d2906 | 454 | catch (const gdb_exception &except) |
492d29ea PA |
455 | { |
456 | GDB_PY_HANDLE_EXCEPTION (except); | |
457 | } | |
86e4ed39 DE |
458 | |
459 | Py_RETURN_NONE; | |
460 | } | |
461 | ||
c620ed88 CB |
462 | /* Implementation of |
463 | gdb.Objfile.lookup_global_symbol (self, string [, domain]) -> gdb.Symbol. */ | |
464 | ||
465 | static PyObject * | |
466 | objfpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw) | |
467 | { | |
468 | static const char *keywords[] = { "name", "domain", NULL }; | |
469 | objfile_object *obj = (objfile_object *) self; | |
470 | const char *symbol_name; | |
471 | int domain = VAR_DOMAIN; | |
472 | ||
473 | OBJFPY_REQUIRE_VALID (obj); | |
474 | ||
475 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &symbol_name, | |
476 | &domain)) | |
477 | return nullptr; | |
478 | ||
479 | try | |
480 | { | |
481 | struct symbol *sym = lookup_global_symbol_from_objfile | |
dda83cd7 | 482 | (obj->objfile, GLOBAL_BLOCK, symbol_name, (domain_enum) domain).symbol; |
c620ed88 CB |
483 | if (sym == nullptr) |
484 | Py_RETURN_NONE; | |
485 | ||
486 | return symbol_to_symbol_object (sym); | |
487 | } | |
488 | catch (const gdb_exception &except) | |
489 | { | |
490 | GDB_PY_HANDLE_EXCEPTION (except); | |
491 | } | |
492 | ||
493 | Py_RETURN_NONE; | |
494 | } | |
495 | ||
496 | /* Implementation of | |
497 | gdb.Objfile.lookup_static_symbol (self, string [, domain]) -> gdb.Symbol. */ | |
498 | ||
499 | static PyObject * | |
500 | objfpy_lookup_static_symbol (PyObject *self, PyObject *args, PyObject *kw) | |
501 | { | |
502 | static const char *keywords[] = { "name", "domain", NULL }; | |
503 | objfile_object *obj = (objfile_object *) self; | |
504 | const char *symbol_name; | |
505 | int domain = VAR_DOMAIN; | |
506 | ||
507 | OBJFPY_REQUIRE_VALID (obj); | |
508 | ||
509 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &symbol_name, | |
510 | &domain)) | |
511 | return nullptr; | |
512 | ||
513 | try | |
514 | { | |
515 | struct symbol *sym = lookup_global_symbol_from_objfile | |
dda83cd7 | 516 | (obj->objfile, STATIC_BLOCK, symbol_name, (domain_enum) domain).symbol; |
c620ed88 CB |
517 | if (sym == nullptr) |
518 | Py_RETURN_NONE; | |
519 | ||
520 | return symbol_to_symbol_object (sym); | |
521 | } | |
522 | catch (const gdb_exception &except) | |
523 | { | |
524 | GDB_PY_HANDLE_EXCEPTION (except); | |
525 | } | |
526 | ||
527 | Py_RETURN_NONE; | |
528 | } | |
529 | ||
1256af7d SM |
530 | /* Implement repr() for gdb.Objfile. */ |
531 | ||
532 | static PyObject * | |
533 | objfpy_repr (PyObject *self_) | |
534 | { | |
535 | objfile_object *self = (objfile_object *) self_; | |
536 | objfile *obj = self->objfile; | |
537 | ||
538 | if (obj == nullptr) | |
5aee4587 | 539 | return PyUnicode_FromString ("<gdb.Objfile (invalid)>"); |
1256af7d | 540 | |
5aee4587 SM |
541 | return PyUnicode_FromFormat ("<gdb.Objfile filename=%s>", |
542 | objfile_name (obj)); | |
1256af7d SM |
543 | } |
544 | ||
6dddd6a5 DE |
545 | /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it. |
546 | Return non-zero if STRING is a potentially valid build id. */ | |
547 | ||
548 | static int | |
549 | objfpy_build_id_ok (const char *string) | |
550 | { | |
551 | size_t i, n = strlen (string); | |
552 | ||
553 | if (n % 2 != 0) | |
554 | return 0; | |
555 | for (i = 0; i < n; ++i) | |
556 | { | |
557 | if (!isxdigit (string[i])) | |
558 | return 0; | |
559 | } | |
560 | return 1; | |
561 | } | |
562 | ||
563 | /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it. | |
564 | Returns non-zero if BUILD_ID matches STRING. | |
565 | It is assumed that objfpy_build_id_ok (string) returns TRUE. */ | |
566 | ||
567 | static int | |
c74f7d1c | 568 | objfpy_build_id_matches (const struct bfd_build_id *build_id, |
6dddd6a5 DE |
569 | const char *string) |
570 | { | |
571 | size_t i; | |
572 | ||
573 | if (strlen (string) != 2 * build_id->size) | |
574 | return 0; | |
575 | ||
576 | for (i = 0; i < build_id->size; ++i) | |
577 | { | |
578 | char c1 = string[i * 2], c2 = string[i * 2 + 1]; | |
2b531492 | 579 | int byte = (fromhex (c1) << 4) | fromhex (c2); |
6dddd6a5 DE |
580 | |
581 | if (byte != build_id->data[i]) | |
582 | return 0; | |
583 | } | |
584 | ||
585 | return 1; | |
586 | } | |
587 | ||
6dddd6a5 DE |
588 | /* Implementation of gdb.lookup_objfile. */ |
589 | ||
590 | PyObject * | |
591 | gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw) | |
592 | { | |
2adadf51 | 593 | static const char *keywords[] = { "name", "by_build_id", NULL }; |
6dddd6a5 DE |
594 | const char *name; |
595 | PyObject *by_build_id_obj = NULL; | |
596 | int by_build_id; | |
6dddd6a5 | 597 | |
2adadf51 PA |
598 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!", keywords, |
599 | &name, &PyBool_Type, &by_build_id_obj)) | |
6dddd6a5 DE |
600 | return NULL; |
601 | ||
602 | by_build_id = 0; | |
603 | if (by_build_id_obj != NULL) | |
604 | { | |
605 | int cmp = PyObject_IsTrue (by_build_id_obj); | |
606 | ||
607 | if (cmp < 0) | |
608 | return NULL; | |
609 | by_build_id = cmp; | |
610 | } | |
611 | ||
85933f7c | 612 | if (by_build_id && !objfpy_build_id_ok (name)) |
6dddd6a5 | 613 | { |
85933f7c MM |
614 | PyErr_SetString (PyExc_TypeError, _("Not a valid build id.")); |
615 | return NULL; | |
6dddd6a5 | 616 | } |
85933f7c MM |
617 | |
618 | struct objfile *objfile = nullptr; | |
619 | if (by_build_id) | |
620 | gdbarch_iterate_over_objfiles_in_search_order | |
621 | (target_gdbarch (), | |
622 | [&objfile, name] (struct objfile *obj) | |
623 | { | |
624 | /* Don't return separate debug files. */ | |
625 | if (obj->separate_debug_objfile_backlink != nullptr) | |
626 | return 0; | |
627 | ||
628 | bfd *obfd = obj->obfd.get (); | |
629 | if (obfd == nullptr) | |
630 | return 0; | |
631 | ||
632 | const bfd_build_id *obfd_build_id = build_id_bfd_get (obfd); | |
633 | if (obfd_build_id == nullptr) | |
634 | return 0; | |
635 | ||
636 | if (!objfpy_build_id_matches (obfd_build_id, name)) | |
637 | return 0; | |
638 | ||
639 | objfile = obj; | |
640 | return 1; | |
641 | }, gdbpy_current_objfile); | |
6dddd6a5 | 642 | else |
85933f7c MM |
643 | gdbarch_iterate_over_objfiles_in_search_order |
644 | (target_gdbarch (), | |
645 | [&objfile, name] (struct objfile *obj) | |
646 | { | |
647 | /* Don't return separate debug files. */ | |
648 | if (obj->separate_debug_objfile_backlink != nullptr) | |
649 | return 0; | |
650 | ||
651 | if ((obj->flags & OBJF_NOT_FILENAME) != 0) | |
652 | return 0; | |
653 | ||
654 | const char *filename = objfile_filename (obj); | |
655 | if (filename != NULL | |
656 | && compare_filenames_for_search (filename, name)) | |
657 | { | |
658 | objfile = obj; | |
659 | return 1; | |
660 | } | |
661 | ||
662 | if (compare_filenames_for_search (obj->original_name, name)) | |
663 | { | |
664 | objfile = obj; | |
665 | return 1; | |
666 | } | |
667 | ||
668 | return 0; | |
669 | }, gdbpy_current_objfile); | |
6dddd6a5 DE |
670 | |
671 | if (objfile != NULL) | |
0a9db5ad | 672 | return objfile_to_objfile_object (objfile).release (); |
6dddd6a5 DE |
673 | |
674 | PyErr_SetString (PyExc_ValueError, _("Objfile not found.")); | |
675 | return NULL; | |
676 | } | |
677 | ||
89c73ade TT |
678 | \f |
679 | ||
0a9db5ad | 680 | /* Return a new reference to the Python object of type Objfile |
89c73ade TT |
681 | representing OBJFILE. If the object has already been created, |
682 | return it. Otherwise, create it. Return NULL and set the Python | |
683 | error on failure. */ | |
4e1bbde0 | 684 | |
0a9db5ad | 685 | gdbpy_ref<> |
89c73ade TT |
686 | objfile_to_objfile_object (struct objfile *objfile) |
687 | { | |
0a9db5ad | 688 | PyObject *result |
08b8a139 | 689 | = (PyObject *) objfpy_objfile_data_key.get (objfile); |
0a9db5ad | 690 | if (result == NULL) |
89c73ade | 691 | { |
0a9db5ad TT |
692 | gdbpy_ref<objfile_object> object |
693 | ((objfile_object *) PyObject_New (objfile_object, &objfile_object_type)); | |
694 | if (object == NULL) | |
695 | return NULL; | |
696 | if (!objfpy_initialize (object.get ())) | |
697 | return NULL; | |
883964a7 | 698 | |
0a9db5ad | 699 | object->objfile = objfile; |
08b8a139 | 700 | objfpy_objfile_data_key.set (objfile, object.get ()); |
0a9db5ad | 701 | result = (PyObject *) object.release (); |
89c73ade TT |
702 | } |
703 | ||
0a9db5ad | 704 | return gdbpy_ref<>::new_reference (result); |
89c73ade TT |
705 | } |
706 | ||
8e3685bf AB |
707 | int |
708 | gdbpy_initialize_objfile (void) | |
709 | { | |
89c73ade | 710 | if (PyType_Ready (&objfile_object_type) < 0) |
999633ed | 711 | return -1; |
89c73ade | 712 | |
aa36459a TT |
713 | return gdb_pymodule_addobject (gdb_module, "Objfile", |
714 | (PyObject *) &objfile_object_type); | |
89c73ade TT |
715 | } |
716 | ||
717 | \f | |
718 | ||
29703da4 PM |
719 | static PyMethodDef objfile_object_methods[] = |
720 | { | |
721 | { "is_valid", objfpy_is_valid, METH_NOARGS, | |
722 | "is_valid () -> Boolean.\n\ | |
723 | Return true if this object file is valid, false if not." }, | |
724 | ||
86e4ed39 DE |
725 | { "add_separate_debug_file", (PyCFunction) objfpy_add_separate_debug_file, |
726 | METH_VARARGS | METH_KEYWORDS, | |
727 | "add_separate_debug_file (file_name).\n\ | |
728 | Add FILE_NAME to the list of files containing debug info for the objfile." }, | |
729 | ||
c620ed88 CB |
730 | { "lookup_global_symbol", (PyCFunction) objfpy_lookup_global_symbol, |
731 | METH_VARARGS | METH_KEYWORDS, | |
732 | "lookup_global_symbol (name [, domain]).\n\ | |
733 | Look up a global symbol in this objfile and return it." }, | |
734 | ||
735 | { "lookup_static_symbol", (PyCFunction) objfpy_lookup_static_symbol, | |
736 | METH_VARARGS | METH_KEYWORDS, | |
737 | "lookup_static_symbol (name [, domain]).\n\ | |
738 | Look up a static-linkage global symbol in this objfile and return it." }, | |
739 | ||
29703da4 PM |
740 | { NULL } |
741 | }; | |
742 | ||
0d1f4ceb | 743 | static gdb_PyGetSetDef objfile_getset[] = |
89c73ade | 744 | { |
02be9a71 DE |
745 | { "__dict__", gdb_py_generic_dict, NULL, |
746 | "The __dict__ for this objfile.", &objfile_object_type }, | |
89c73ade TT |
747 | { "filename", objfpy_get_filename, NULL, |
748 | "The objfile's filename, or None.", NULL }, | |
3a8b707a DE |
749 | { "username", objfpy_get_username, NULL, |
750 | "The name of the objfile as provided by the user, or None.", NULL }, | |
a0be3e44 DE |
751 | { "owner", objfpy_get_owner, NULL, |
752 | "The objfile owner of separate debug info objfiles, or None.", | |
753 | NULL }, | |
7c50a931 DE |
754 | { "build_id", objfpy_get_build_id, NULL, |
755 | "The objfile's build id, or None.", NULL }, | |
d096d8c1 DE |
756 | { "progspace", objfpy_get_progspace, NULL, |
757 | "The objfile's progspace, or None.", NULL }, | |
89c73ade TT |
758 | { "pretty_printers", objfpy_get_printers, objfpy_set_printers, |
759 | "Pretty printers.", NULL }, | |
1e611234 PM |
760 | { "frame_filters", objfpy_get_frame_filters, |
761 | objfpy_set_frame_filters, "Frame Filters.", NULL }, | |
d11916aa SS |
762 | { "frame_unwinders", objfpy_get_frame_unwinders, |
763 | objfpy_set_frame_unwinders, "Frame Unwinders", NULL }, | |
18a9fc12 TT |
764 | { "type_printers", objfpy_get_type_printers, objfpy_set_type_printers, |
765 | "Type printers.", NULL }, | |
883964a7 SC |
766 | { "xmethods", objfpy_get_xmethods, NULL, |
767 | "Debug methods.", NULL }, | |
99298c95 TT |
768 | { "is_file", objfpy_get_is_file, nullptr, |
769 | "Whether this objfile came from a file.", nullptr }, | |
89c73ade TT |
770 | { NULL } |
771 | }; | |
772 | ||
e36122e9 | 773 | PyTypeObject objfile_object_type = |
89c73ade | 774 | { |
9a27f2c6 | 775 | PyVarObject_HEAD_INIT (NULL, 0) |
89c73ade TT |
776 | "gdb.Objfile", /*tp_name*/ |
777 | sizeof (objfile_object), /*tp_basicsize*/ | |
778 | 0, /*tp_itemsize*/ | |
779 | objfpy_dealloc, /*tp_dealloc*/ | |
780 | 0, /*tp_print*/ | |
781 | 0, /*tp_getattr*/ | |
782 | 0, /*tp_setattr*/ | |
783 | 0, /*tp_compare*/ | |
1256af7d | 784 | objfpy_repr, /*tp_repr*/ |
89c73ade TT |
785 | 0, /*tp_as_number*/ |
786 | 0, /*tp_as_sequence*/ | |
787 | 0, /*tp_as_mapping*/ | |
788 | 0, /*tp_hash */ | |
789 | 0, /*tp_call*/ | |
790 | 0, /*tp_str*/ | |
791 | 0, /*tp_getattro*/ | |
792 | 0, /*tp_setattro*/ | |
793 | 0, /*tp_as_buffer*/ | |
794 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ | |
795 | "GDB objfile object", /* tp_doc */ | |
796 | 0, /* tp_traverse */ | |
797 | 0, /* tp_clear */ | |
798 | 0, /* tp_richcompare */ | |
799 | 0, /* tp_weaklistoffset */ | |
800 | 0, /* tp_iter */ | |
801 | 0, /* tp_iternext */ | |
29703da4 | 802 | objfile_object_methods, /* tp_methods */ |
89c73ade TT |
803 | 0, /* tp_members */ |
804 | objfile_getset, /* tp_getset */ | |
805 | 0, /* tp_base */ | |
806 | 0, /* tp_dict */ | |
807 | 0, /* tp_descr_get */ | |
808 | 0, /* tp_descr_set */ | |
02be9a71 | 809 | offsetof (objfile_object, dict), /* tp_dictoffset */ |
89c73ade TT |
810 | 0, /* tp_init */ |
811 | 0, /* tp_alloc */ | |
812 | objfpy_new, /* tp_new */ | |
813 | }; |