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