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