Commit | Line | Data |
---|---|---|
89c73ade TT |
1 | /* Python interface to objfiles. |
2 | ||
42a4f53d | 3 | Copyright (C) 2008-2019 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 TT |
27 | |
28 | typedef struct | |
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; | |
89c73ade TT |
53 | } objfile_object; |
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 | ||
492d29ea | 133 | TRY |
7c50a931 DE |
134 | { |
135 | build_id = build_id_bfd_get (objfile->obfd); | |
136 | } | |
492d29ea PA |
137 | CATCH (except, RETURN_MASK_ALL) |
138 | { | |
139 | GDB_PY_HANDLE_EXCEPTION (except); | |
140 | } | |
141 | END_CATCH | |
7c50a931 DE |
142 | |
143 | if (build_id != NULL) | |
144 | { | |
075c55e0 TT |
145 | gdb::unique_xmalloc_ptr<char> hex_form |
146 | (make_hex_string (build_id->data, build_id->size)); | |
7c50a931 | 147 | |
075c55e0 | 148 | return host_string_to_python_string (hex_form.get ()).release (); |
7c50a931 DE |
149 | } |
150 | ||
151 | Py_RETURN_NONE; | |
152 | } | |
153 | ||
d096d8c1 DE |
154 | /* An Objfile method which returns the objfile's progspace, or None. */ |
155 | ||
156 | static PyObject * | |
157 | objfpy_get_progspace (PyObject *self, void *closure) | |
158 | { | |
159 | objfile_object *obj = (objfile_object *) self; | |
160 | ||
161 | if (obj->objfile) | |
3c7aa307 | 162 | return pspace_to_pspace_object (obj->objfile->pspace).release (); |
d096d8c1 DE |
163 | |
164 | Py_RETURN_NONE; | |
165 | } | |
166 | ||
89c73ade TT |
167 | static void |
168 | objfpy_dealloc (PyObject *o) | |
169 | { | |
170 | objfile_object *self = (objfile_object *) o; | |
d59b6f6c | 171 | |
02be9a71 | 172 | Py_XDECREF (self->dict); |
89c73ade | 173 | Py_XDECREF (self->printers); |
1e611234 | 174 | Py_XDECREF (self->frame_filters); |
d11916aa | 175 | Py_XDECREF (self->frame_unwinders); |
18a9fc12 | 176 | Py_XDECREF (self->type_printers); |
883964a7 | 177 | Py_XDECREF (self->xmethods); |
9a27f2c6 | 178 | Py_TYPE (self)->tp_free (self); |
89c73ade TT |
179 | } |
180 | ||
4e1bbde0 DE |
181 | /* Initialize an objfile_object. |
182 | The result is a boolean indicating success. */ | |
183 | ||
184 | static int | |
185 | objfpy_initialize (objfile_object *self) | |
186 | { | |
187 | self->objfile = NULL; | |
0f6ed0e0 TT |
188 | |
189 | self->dict = PyDict_New (); | |
190 | if (self->dict == NULL) | |
191 | return 0; | |
4e1bbde0 DE |
192 | |
193 | self->printers = PyList_New (0); | |
194 | if (self->printers == NULL) | |
195 | return 0; | |
196 | ||
197 | self->frame_filters = PyDict_New (); | |
198 | if (self->frame_filters == NULL) | |
199 | return 0; | |
200 | ||
d11916aa SS |
201 | self->frame_unwinders = PyList_New (0); |
202 | if (self->frame_unwinders == NULL) | |
203 | return 0; | |
204 | ||
4e1bbde0 DE |
205 | self->type_printers = PyList_New (0); |
206 | if (self->type_printers == NULL) | |
207 | return 0; | |
208 | ||
209 | self->xmethods = PyList_New (0); | |
210 | if (self->xmethods == NULL) | |
211 | return 0; | |
212 | ||
213 | return 1; | |
214 | } | |
215 | ||
89c73ade TT |
216 | static PyObject * |
217 | objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords) | |
218 | { | |
88b6faea | 219 | gdbpy_ref<objfile_object> self ((objfile_object *) type->tp_alloc (type, 0)); |
d59b6f6c | 220 | |
88b6faea | 221 | if (self != NULL) |
89c73ade | 222 | { |
88b6faea TT |
223 | if (!objfpy_initialize (self.get ())) |
224 | return NULL; | |
89c73ade | 225 | } |
4e1bbde0 | 226 | |
88b6faea | 227 | return (PyObject *) self.release (); |
89c73ade TT |
228 | } |
229 | ||
230 | PyObject * | |
231 | objfpy_get_printers (PyObject *o, void *ignore) | |
232 | { | |
233 | objfile_object *self = (objfile_object *) o; | |
d59b6f6c | 234 | |
89c73ade TT |
235 | Py_INCREF (self->printers); |
236 | return self->printers; | |
237 | } | |
238 | ||
239 | static int | |
240 | objfpy_set_printers (PyObject *o, PyObject *value, void *ignore) | |
241 | { | |
89c73ade | 242 | objfile_object *self = (objfile_object *) o; |
d59b6f6c | 243 | |
89c73ade TT |
244 | if (! value) |
245 | { | |
246 | PyErr_SetString (PyExc_TypeError, | |
044c0f87 | 247 | _("Cannot delete the pretty_printers attribute.")); |
89c73ade TT |
248 | return -1; |
249 | } | |
250 | ||
251 | if (! PyList_Check (value)) | |
252 | { | |
253 | PyErr_SetString (PyExc_TypeError, | |
044c0f87 | 254 | _("The pretty_printers attribute must be a list.")); |
89c73ade TT |
255 | return -1; |
256 | } | |
257 | ||
258 | /* Take care in case the LHS and RHS are related somehow. */ | |
2a3c71d6 | 259 | gdbpy_ref<> tmp (self->printers); |
89c73ade TT |
260 | Py_INCREF (value); |
261 | self->printers = value; | |
89c73ade TT |
262 | |
263 | return 0; | |
264 | } | |
265 | ||
1e611234 PM |
266 | /* Return the Python dictionary attribute containing frame filters for |
267 | this object file. */ | |
268 | PyObject * | |
269 | objfpy_get_frame_filters (PyObject *o, void *ignore) | |
270 | { | |
271 | objfile_object *self = (objfile_object *) o; | |
272 | ||
273 | Py_INCREF (self->frame_filters); | |
274 | return self->frame_filters; | |
275 | } | |
276 | ||
277 | /* Set this object file's frame filters dictionary to FILTERS. */ | |
278 | static int | |
279 | objfpy_set_frame_filters (PyObject *o, PyObject *filters, void *ignore) | |
280 | { | |
1e611234 PM |
281 | objfile_object *self = (objfile_object *) o; |
282 | ||
283 | if (! filters) | |
284 | { | |
285 | PyErr_SetString (PyExc_TypeError, | |
286 | _("Cannot delete the frame filters attribute.")); | |
287 | return -1; | |
288 | } | |
289 | ||
290 | if (! PyDict_Check (filters)) | |
291 | { | |
292 | PyErr_SetString (PyExc_TypeError, | |
293 | _("The frame_filters attribute must be a dictionary.")); | |
294 | return -1; | |
295 | } | |
296 | ||
297 | /* Take care in case the LHS and RHS are related somehow. */ | |
2a3c71d6 | 298 | gdbpy_ref<> tmp (self->frame_filters); |
1e611234 PM |
299 | Py_INCREF (filters); |
300 | self->frame_filters = filters; | |
1e611234 PM |
301 | |
302 | return 0; | |
303 | } | |
304 | ||
d11916aa SS |
305 | /* Return the frame unwinders attribute for this object file. */ |
306 | ||
307 | PyObject * | |
308 | objfpy_get_frame_unwinders (PyObject *o, void *ignore) | |
309 | { | |
310 | objfile_object *self = (objfile_object *) o; | |
311 | ||
312 | Py_INCREF (self->frame_unwinders); | |
313 | return self->frame_unwinders; | |
314 | } | |
315 | ||
316 | /* Set this object file's frame unwinders list to UNWINDERS. */ | |
317 | ||
318 | static int | |
319 | objfpy_set_frame_unwinders (PyObject *o, PyObject *unwinders, void *ignore) | |
320 | { | |
d11916aa SS |
321 | objfile_object *self = (objfile_object *) o; |
322 | ||
323 | if (!unwinders) | |
324 | { | |
325 | PyErr_SetString (PyExc_TypeError, | |
326 | _("Cannot delete the frame unwinders attribute.")); | |
327 | return -1; | |
328 | } | |
329 | ||
330 | if (!PyList_Check (unwinders)) | |
331 | { | |
332 | PyErr_SetString (PyExc_TypeError, | |
333 | _("The frame_unwinders attribute must be a list.")); | |
334 | return -1; | |
335 | } | |
336 | ||
337 | /* Take care in case the LHS and RHS are related somehow. */ | |
2a3c71d6 | 338 | gdbpy_ref<> tmp (self->frame_unwinders); |
d11916aa SS |
339 | Py_INCREF (unwinders); |
340 | self->frame_unwinders = unwinders; | |
d11916aa SS |
341 | |
342 | return 0; | |
343 | } | |
344 | ||
18a9fc12 TT |
345 | /* Get the 'type_printers' attribute. */ |
346 | ||
347 | static PyObject * | |
348 | objfpy_get_type_printers (PyObject *o, void *ignore) | |
349 | { | |
350 | objfile_object *self = (objfile_object *) o; | |
351 | ||
352 | Py_INCREF (self->type_printers); | |
353 | return self->type_printers; | |
354 | } | |
355 | ||
883964a7 SC |
356 | /* Get the 'xmethods' attribute. */ |
357 | ||
358 | PyObject * | |
359 | objfpy_get_xmethods (PyObject *o, void *ignore) | |
360 | { | |
361 | objfile_object *self = (objfile_object *) o; | |
362 | ||
363 | Py_INCREF (self->xmethods); | |
364 | return self->xmethods; | |
365 | } | |
366 | ||
18a9fc12 TT |
367 | /* Set the 'type_printers' attribute. */ |
368 | ||
369 | static int | |
370 | objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore) | |
371 | { | |
18a9fc12 TT |
372 | objfile_object *self = (objfile_object *) o; |
373 | ||
374 | if (! value) | |
375 | { | |
376 | PyErr_SetString (PyExc_TypeError, | |
377 | _("Cannot delete the type_printers attribute.")); | |
378 | return -1; | |
379 | } | |
380 | ||
381 | if (! PyList_Check (value)) | |
382 | { | |
383 | PyErr_SetString (PyExc_TypeError, | |
384 | _("The type_printers attribute must be a list.")); | |
385 | return -1; | |
386 | } | |
387 | ||
388 | /* Take care in case the LHS and RHS are related somehow. */ | |
2a3c71d6 | 389 | gdbpy_ref<> tmp (self->type_printers); |
18a9fc12 TT |
390 | Py_INCREF (value); |
391 | self->type_printers = value; | |
18a9fc12 TT |
392 | |
393 | return 0; | |
394 | } | |
395 | ||
29703da4 PM |
396 | /* Implementation of gdb.Objfile.is_valid (self) -> Boolean. |
397 | Returns True if this object file still exists in GDB. */ | |
398 | ||
399 | static PyObject * | |
400 | objfpy_is_valid (PyObject *self, PyObject *args) | |
401 | { | |
402 | objfile_object *obj = (objfile_object *) self; | |
403 | ||
404 | if (! obj->objfile) | |
405 | Py_RETURN_FALSE; | |
406 | ||
407 | Py_RETURN_TRUE; | |
408 | } | |
409 | ||
86e4ed39 DE |
410 | /* Implementation of gdb.Objfile.add_separate_debug_file (self) -> Boolean. */ |
411 | ||
412 | static PyObject * | |
413 | objfpy_add_separate_debug_file (PyObject *self, PyObject *args, PyObject *kw) | |
414 | { | |
2adadf51 | 415 | static const char *keywords[] = { "file_name", NULL }; |
86e4ed39 DE |
416 | objfile_object *obj = (objfile_object *) self; |
417 | const char *file_name; | |
86e4ed39 DE |
418 | |
419 | OBJFPY_REQUIRE_VALID (obj); | |
420 | ||
2adadf51 | 421 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s", keywords, &file_name)) |
86e4ed39 DE |
422 | return NULL; |
423 | ||
492d29ea | 424 | TRY |
86e4ed39 | 425 | { |
192b62ce | 426 | gdb_bfd_ref_ptr abfd (symfile_bfd_open (file_name)); |
86e4ed39 | 427 | |
192b62ce | 428 | symbol_file_add_separate (abfd.get (), file_name, 0, obj->objfile); |
86e4ed39 | 429 | } |
492d29ea PA |
430 | CATCH (except, RETURN_MASK_ALL) |
431 | { | |
432 | GDB_PY_HANDLE_EXCEPTION (except); | |
433 | } | |
434 | END_CATCH | |
86e4ed39 DE |
435 | |
436 | Py_RETURN_NONE; | |
437 | } | |
438 | ||
1256af7d SM |
439 | /* Implement repr() for gdb.Objfile. */ |
440 | ||
441 | static PyObject * | |
442 | objfpy_repr (PyObject *self_) | |
443 | { | |
444 | objfile_object *self = (objfile_object *) self_; | |
445 | objfile *obj = self->objfile; | |
446 | ||
447 | if (obj == nullptr) | |
448 | return PyString_FromString ("<gdb.Objfile (invalid)>"); | |
449 | ||
450 | return PyString_FromFormat ("<gdb.Objfile filename=%s>", | |
451 | objfile_filename (obj)); | |
452 | } | |
453 | ||
6dddd6a5 DE |
454 | /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it. |
455 | Return non-zero if STRING is a potentially valid build id. */ | |
456 | ||
457 | static int | |
458 | objfpy_build_id_ok (const char *string) | |
459 | { | |
460 | size_t i, n = strlen (string); | |
461 | ||
462 | if (n % 2 != 0) | |
463 | return 0; | |
464 | for (i = 0; i < n; ++i) | |
465 | { | |
466 | if (!isxdigit (string[i])) | |
467 | return 0; | |
468 | } | |
469 | return 1; | |
470 | } | |
471 | ||
472 | /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it. | |
473 | Returns non-zero if BUILD_ID matches STRING. | |
474 | It is assumed that objfpy_build_id_ok (string) returns TRUE. */ | |
475 | ||
476 | static int | |
c74f7d1c | 477 | objfpy_build_id_matches (const struct bfd_build_id *build_id, |
6dddd6a5 DE |
478 | const char *string) |
479 | { | |
480 | size_t i; | |
481 | ||
482 | if (strlen (string) != 2 * build_id->size) | |
483 | return 0; | |
484 | ||
485 | for (i = 0; i < build_id->size; ++i) | |
486 | { | |
487 | char c1 = string[i * 2], c2 = string[i * 2 + 1]; | |
488 | int byte = (host_hex_value (c1) << 4) | host_hex_value (c2); | |
489 | ||
490 | if (byte != build_id->data[i]) | |
491 | return 0; | |
492 | } | |
493 | ||
494 | return 1; | |
495 | } | |
496 | ||
497 | /* Subroutine of gdbpy_lookup_objfile to simplify it. | |
498 | Look up an objfile by its file name. */ | |
499 | ||
500 | static struct objfile * | |
501 | objfpy_lookup_objfile_by_name (const char *name) | |
502 | { | |
2030c079 | 503 | for (objfile *objfile : current_program_space->objfiles ()) |
6dddd6a5 | 504 | { |
e02c96a7 DE |
505 | const char *filename; |
506 | ||
6dddd6a5 DE |
507 | if ((objfile->flags & OBJF_NOT_FILENAME) != 0) |
508 | continue; | |
509 | /* Don't return separate debug files. */ | |
510 | if (objfile->separate_debug_objfile_backlink != NULL) | |
511 | continue; | |
e02c96a7 DE |
512 | |
513 | filename = objfile_filename (objfile); | |
514 | if (filename != NULL && compare_filenames_for_search (filename, name)) | |
515 | return objfile; | |
516 | if (compare_filenames_for_search (objfile->original_name, name)) | |
6dddd6a5 DE |
517 | return objfile; |
518 | } | |
519 | ||
520 | return NULL; | |
521 | } | |
522 | ||
523 | /* Subroutine of gdbpy_lookup_objfile to simplify it. | |
524 | Look up an objfile by its build id. */ | |
525 | ||
526 | static struct objfile * | |
527 | objfpy_lookup_objfile_by_build_id (const char *build_id) | |
528 | { | |
2030c079 | 529 | for (objfile *objfile : current_program_space->objfiles ()) |
6dddd6a5 | 530 | { |
c74f7d1c | 531 | const struct bfd_build_id *obfd_build_id; |
6dddd6a5 DE |
532 | |
533 | if (objfile->obfd == NULL) | |
534 | continue; | |
535 | /* Don't return separate debug files. */ | |
536 | if (objfile->separate_debug_objfile_backlink != NULL) | |
537 | continue; | |
538 | obfd_build_id = build_id_bfd_get (objfile->obfd); | |
539 | if (obfd_build_id == NULL) | |
540 | continue; | |
541 | if (objfpy_build_id_matches (obfd_build_id, build_id)) | |
542 | return objfile; | |
543 | } | |
544 | ||
545 | return NULL; | |
546 | } | |
547 | ||
548 | /* Implementation of gdb.lookup_objfile. */ | |
549 | ||
550 | PyObject * | |
551 | gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw) | |
552 | { | |
2adadf51 | 553 | static const char *keywords[] = { "name", "by_build_id", NULL }; |
6dddd6a5 DE |
554 | const char *name; |
555 | PyObject *by_build_id_obj = NULL; | |
556 | int by_build_id; | |
557 | struct objfile *objfile; | |
558 | ||
2adadf51 PA |
559 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!", keywords, |
560 | &name, &PyBool_Type, &by_build_id_obj)) | |
6dddd6a5 DE |
561 | return NULL; |
562 | ||
563 | by_build_id = 0; | |
564 | if (by_build_id_obj != NULL) | |
565 | { | |
566 | int cmp = PyObject_IsTrue (by_build_id_obj); | |
567 | ||
568 | if (cmp < 0) | |
569 | return NULL; | |
570 | by_build_id = cmp; | |
571 | } | |
572 | ||
573 | if (by_build_id) | |
574 | { | |
575 | if (!objfpy_build_id_ok (name)) | |
576 | { | |
577 | PyErr_SetString (PyExc_TypeError, _("Not a valid build id.")); | |
578 | return NULL; | |
579 | } | |
580 | objfile = objfpy_lookup_objfile_by_build_id (name); | |
581 | } | |
582 | else | |
583 | objfile = objfpy_lookup_objfile_by_name (name); | |
584 | ||
585 | if (objfile != NULL) | |
0a9db5ad | 586 | return objfile_to_objfile_object (objfile).release (); |
6dddd6a5 DE |
587 | |
588 | PyErr_SetString (PyExc_ValueError, _("Objfile not found.")); | |
589 | return NULL; | |
590 | } | |
591 | ||
89c73ade TT |
592 | \f |
593 | ||
594 | /* Clear the OBJFILE pointer in an Objfile object and remove the | |
595 | reference. */ | |
596 | static void | |
c1bd65d0 | 597 | py_free_objfile (struct objfile *objfile, void *datum) |
89c73ade | 598 | { |
2d38bced | 599 | gdbpy_enter enter_py (get_objfile_arch (objfile), current_language); |
88b6faea | 600 | gdbpy_ref<objfile_object> object ((objfile_object *) datum); |
89c73ade | 601 | object->objfile = NULL; |
89c73ade TT |
602 | } |
603 | ||
0a9db5ad | 604 | /* Return a new reference to the Python object of type Objfile |
89c73ade TT |
605 | representing OBJFILE. If the object has already been created, |
606 | return it. Otherwise, create it. Return NULL and set the Python | |
607 | error on failure. */ | |
4e1bbde0 | 608 | |
0a9db5ad | 609 | gdbpy_ref<> |
89c73ade TT |
610 | objfile_to_objfile_object (struct objfile *objfile) |
611 | { | |
0a9db5ad TT |
612 | PyObject *result |
613 | = ((PyObject *) objfile_data (objfile, objfpy_objfile_data_key)); | |
614 | if (result == NULL) | |
89c73ade | 615 | { |
0a9db5ad TT |
616 | gdbpy_ref<objfile_object> object |
617 | ((objfile_object *) PyObject_New (objfile_object, &objfile_object_type)); | |
618 | if (object == NULL) | |
619 | return NULL; | |
620 | if (!objfpy_initialize (object.get ())) | |
621 | return NULL; | |
883964a7 | 622 | |
0a9db5ad TT |
623 | object->objfile = objfile; |
624 | set_objfile_data (objfile, objfpy_objfile_data_key, object.get ()); | |
625 | result = (PyObject *) object.release (); | |
89c73ade TT |
626 | } |
627 | ||
0a9db5ad | 628 | return gdbpy_ref<>::new_reference (result); |
89c73ade TT |
629 | } |
630 | ||
999633ed | 631 | int |
89c73ade TT |
632 | gdbpy_initialize_objfile (void) |
633 | { | |
634 | objfpy_objfile_data_key | |
c1bd65d0 | 635 | = register_objfile_data_with_cleanup (NULL, py_free_objfile); |
89c73ade TT |
636 | |
637 | if (PyType_Ready (&objfile_object_type) < 0) | |
999633ed | 638 | return -1; |
89c73ade | 639 | |
aa36459a TT |
640 | return gdb_pymodule_addobject (gdb_module, "Objfile", |
641 | (PyObject *) &objfile_object_type); | |
89c73ade TT |
642 | } |
643 | ||
644 | \f | |
645 | ||
29703da4 PM |
646 | static PyMethodDef objfile_object_methods[] = |
647 | { | |
648 | { "is_valid", objfpy_is_valid, METH_NOARGS, | |
649 | "is_valid () -> Boolean.\n\ | |
650 | Return true if this object file is valid, false if not." }, | |
651 | ||
86e4ed39 DE |
652 | { "add_separate_debug_file", (PyCFunction) objfpy_add_separate_debug_file, |
653 | METH_VARARGS | METH_KEYWORDS, | |
654 | "add_separate_debug_file (file_name).\n\ | |
655 | Add FILE_NAME to the list of files containing debug info for the objfile." }, | |
656 | ||
29703da4 PM |
657 | { NULL } |
658 | }; | |
659 | ||
0d1f4ceb | 660 | static gdb_PyGetSetDef objfile_getset[] = |
89c73ade | 661 | { |
02be9a71 DE |
662 | { "__dict__", gdb_py_generic_dict, NULL, |
663 | "The __dict__ for this objfile.", &objfile_object_type }, | |
89c73ade TT |
664 | { "filename", objfpy_get_filename, NULL, |
665 | "The objfile's filename, or None.", NULL }, | |
3a8b707a DE |
666 | { "username", objfpy_get_username, NULL, |
667 | "The name of the objfile as provided by the user, or None.", NULL }, | |
a0be3e44 DE |
668 | { "owner", objfpy_get_owner, NULL, |
669 | "The objfile owner of separate debug info objfiles, or None.", | |
670 | NULL }, | |
7c50a931 DE |
671 | { "build_id", objfpy_get_build_id, NULL, |
672 | "The objfile's build id, or None.", NULL }, | |
d096d8c1 DE |
673 | { "progspace", objfpy_get_progspace, NULL, |
674 | "The objfile's progspace, or None.", NULL }, | |
89c73ade TT |
675 | { "pretty_printers", objfpy_get_printers, objfpy_set_printers, |
676 | "Pretty printers.", NULL }, | |
1e611234 PM |
677 | { "frame_filters", objfpy_get_frame_filters, |
678 | objfpy_set_frame_filters, "Frame Filters.", NULL }, | |
d11916aa SS |
679 | { "frame_unwinders", objfpy_get_frame_unwinders, |
680 | objfpy_set_frame_unwinders, "Frame Unwinders", NULL }, | |
18a9fc12 TT |
681 | { "type_printers", objfpy_get_type_printers, objfpy_set_type_printers, |
682 | "Type printers.", NULL }, | |
883964a7 SC |
683 | { "xmethods", objfpy_get_xmethods, NULL, |
684 | "Debug methods.", NULL }, | |
89c73ade TT |
685 | { NULL } |
686 | }; | |
687 | ||
e36122e9 | 688 | PyTypeObject objfile_object_type = |
89c73ade | 689 | { |
9a27f2c6 | 690 | PyVarObject_HEAD_INIT (NULL, 0) |
89c73ade TT |
691 | "gdb.Objfile", /*tp_name*/ |
692 | sizeof (objfile_object), /*tp_basicsize*/ | |
693 | 0, /*tp_itemsize*/ | |
694 | objfpy_dealloc, /*tp_dealloc*/ | |
695 | 0, /*tp_print*/ | |
696 | 0, /*tp_getattr*/ | |
697 | 0, /*tp_setattr*/ | |
698 | 0, /*tp_compare*/ | |
1256af7d | 699 | objfpy_repr, /*tp_repr*/ |
89c73ade TT |
700 | 0, /*tp_as_number*/ |
701 | 0, /*tp_as_sequence*/ | |
702 | 0, /*tp_as_mapping*/ | |
703 | 0, /*tp_hash */ | |
704 | 0, /*tp_call*/ | |
705 | 0, /*tp_str*/ | |
706 | 0, /*tp_getattro*/ | |
707 | 0, /*tp_setattro*/ | |
708 | 0, /*tp_as_buffer*/ | |
709 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ | |
710 | "GDB objfile object", /* tp_doc */ | |
711 | 0, /* tp_traverse */ | |
712 | 0, /* tp_clear */ | |
713 | 0, /* tp_richcompare */ | |
714 | 0, /* tp_weaklistoffset */ | |
715 | 0, /* tp_iter */ | |
716 | 0, /* tp_iternext */ | |
29703da4 | 717 | objfile_object_methods, /* tp_methods */ |
89c73ade TT |
718 | 0, /* tp_members */ |
719 | objfile_getset, /* tp_getset */ | |
720 | 0, /* tp_base */ | |
721 | 0, /* tp_dict */ | |
722 | 0, /* tp_descr_get */ | |
723 | 0, /* tp_descr_set */ | |
02be9a71 | 724 | offsetof (objfile_object, dict), /* tp_dictoffset */ |
89c73ade TT |
725 | 0, /* tp_init */ |
726 | 0, /* tp_alloc */ | |
727 | objfpy_new, /* tp_new */ | |
728 | }; |