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