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