1 /* Support for debug methods in Python.
3 Copyright (C) 2013-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "arch-utils.h"
22 #include "extension-priv.h"
28 #include "python-internal.h"
31 static const char enabled_field_name
[] = "enabled";
32 static const char match_method_name
[] = "match";
33 static const char get_arg_types_method_name
[] = "get_arg_types";
34 static const char get_result_type_method_name
[] = "get_result_type";
35 static const char matchers_attr_str
[] = "xmethods";
37 static PyObject
*py_match_method_name
= NULL
;
38 static PyObject
*py_get_arg_types_method_name
= NULL
;
40 struct gdbpy_worker_data
46 static struct xmethod_worker
*new_python_xmethod_worker (PyObject
*item
,
47 PyObject
*py_obj_type
);
49 /* Implementation of free_xmethod_worker_data for Python. */
52 gdbpy_free_xmethod_worker_data (const struct extension_language_defn
*extlang
,
55 struct gdbpy_worker_data
*worker_data
= (struct gdbpy_worker_data
*) data
;
57 gdb_assert (worker_data
->worker
!= NULL
&& worker_data
->this_type
!= NULL
);
59 /* We don't do much here, but we still need the GIL. */
60 gdbpy_enter
enter_py (get_current_arch (), current_language
);
62 Py_DECREF (worker_data
->worker
);
63 Py_DECREF (worker_data
->this_type
);
67 /* Implementation of clone_xmethod_worker_data for Python. */
70 gdbpy_clone_xmethod_worker_data (const struct extension_language_defn
*extlang
,
73 struct gdbpy_worker_data
*worker_data
74 = (struct gdbpy_worker_data
*) data
, *new_data
;
76 gdb_assert (worker_data
->worker
!= NULL
&& worker_data
->this_type
!= NULL
);
78 /* We don't do much here, but we still need the GIL. */
79 gdbpy_enter
enter_py (get_current_arch (), current_language
);
81 new_data
= XCNEW (struct gdbpy_worker_data
);
82 new_data
->worker
= worker_data
->worker
;
83 new_data
->this_type
= worker_data
->this_type
;
84 Py_INCREF (new_data
->worker
);
85 Py_INCREF (new_data
->this_type
);
90 /* Invoke the "match" method of the MATCHER and return a new reference
91 to the result. Returns NULL on error. */
94 invoke_match_method (PyObject
*matcher
, PyObject
*py_obj_type
,
95 const char *xmethod_name
)
99 gdbpy_ref
enabled_field (PyObject_GetAttrString (matcher
,
100 enabled_field_name
));
101 if (enabled_field
== NULL
)
104 enabled
= PyObject_IsTrue (enabled_field
.get ());
109 /* Return 'None' if the matcher is not enabled. */
113 gdbpy_ref
match_method (PyObject_GetAttrString (matcher
, match_method_name
));
114 if (match_method
== NULL
)
117 gdbpy_ref
py_xmethod_name (PyString_FromString (xmethod_name
));
118 if (py_xmethod_name
== NULL
)
121 return PyObject_CallMethodObjArgs (matcher
, py_match_method_name
,
122 py_obj_type
, py_xmethod_name
.get (),
126 /* Implementation of get_matching_xmethod_workers for Python. */
129 gdbpy_get_matching_xmethod_workers
130 (const struct extension_language_defn
*extlang
,
131 struct type
*obj_type
, const char *method_name
,
132 xmethod_worker_vec
**dm_vec
)
134 struct objfile
*objfile
;
135 VEC (xmethod_worker_ptr
) *worker_vec
= NULL
;
136 PyObject
*py_progspace
;
138 gdb_assert (obj_type
!= NULL
&& method_name
!= NULL
);
140 gdbpy_enter
enter_py (get_current_arch (), current_language
);
142 gdbpy_ref
py_type (type_to_type_object (obj_type
));
145 gdbpy_print_stack ();
146 return EXT_LANG_RC_ERROR
;
149 /* Create an empty list of debug methods. */
150 gdbpy_ref
py_xmethod_matcher_list (PyList_New (0));
151 if (py_xmethod_matcher_list
== NULL
)
153 gdbpy_print_stack ();
154 return EXT_LANG_RC_ERROR
;
157 /* Gather debug method matchers registered with the object files.
158 This could be done differently by iterating over each objfile's matcher
159 list individually, but there's no data yet to show it's needed. */
160 ALL_OBJFILES (objfile
)
162 PyObject
*py_objfile
= objfile_to_objfile_object (objfile
);
164 if (py_objfile
== NULL
)
166 gdbpy_print_stack ();
167 return EXT_LANG_RC_ERROR
;
170 gdbpy_ref
objfile_matchers (objfpy_get_xmethods (py_objfile
, NULL
));
171 gdbpy_ref
temp (PySequence_Concat (py_xmethod_matcher_list
.get (),
172 objfile_matchers
.get ()));
175 gdbpy_print_stack ();
176 return EXT_LANG_RC_ERROR
;
179 py_xmethod_matcher_list
= std::move (temp
);
182 /* Gather debug methods matchers registered with the current program
184 py_progspace
= pspace_to_pspace_object (current_program_space
);
185 if (py_progspace
!= NULL
)
187 gdbpy_ref
pspace_matchers (pspy_get_xmethods (py_progspace
, NULL
));
189 gdbpy_ref
temp (PySequence_Concat (py_xmethod_matcher_list
.get (),
190 pspace_matchers
.get ()));
193 gdbpy_print_stack ();
194 return EXT_LANG_RC_ERROR
;
197 py_xmethod_matcher_list
= std::move (temp
);
201 gdbpy_print_stack ();
202 return EXT_LANG_RC_ERROR
;
205 /* Gather debug method matchers registered globally. */
206 if (gdb_python_module
!= NULL
207 && PyObject_HasAttrString (gdb_python_module
, matchers_attr_str
))
209 gdbpy_ref
gdb_matchers (PyObject_GetAttrString (gdb_python_module
,
211 if (gdb_matchers
!= NULL
)
213 gdbpy_ref
temp (PySequence_Concat (py_xmethod_matcher_list
.get (),
214 gdb_matchers
.get ()));
217 gdbpy_print_stack ();
218 return EXT_LANG_RC_ERROR
;
221 py_xmethod_matcher_list
= std::move (temp
);
225 gdbpy_print_stack ();
226 return EXT_LANG_RC_ERROR
;
230 gdbpy_ref
list_iter (PyObject_GetIter (py_xmethod_matcher_list
.get ()));
231 if (list_iter
== NULL
)
233 gdbpy_print_stack ();
234 return EXT_LANG_RC_ERROR
;
238 gdbpy_ref
matcher (PyIter_Next (list_iter
.get ()));
241 if (PyErr_Occurred ())
243 gdbpy_print_stack ();
244 return EXT_LANG_RC_ERROR
;
249 gdbpy_ref
match_result (invoke_match_method (matcher
.get (),
253 if (match_result
== NULL
)
255 gdbpy_print_stack ();
256 return EXT_LANG_RC_ERROR
;
258 if (match_result
== Py_None
)
259 ; /* This means there was no match. */
260 else if (PySequence_Check (match_result
.get ()))
262 gdbpy_ref
iter (PyObject_GetIter (match_result
.get ()));
266 gdbpy_print_stack ();
267 return EXT_LANG_RC_ERROR
;
271 struct xmethod_worker
*worker
;
273 gdbpy_ref
py_worker (PyIter_Next (iter
.get ()));
274 if (py_worker
== NULL
)
276 if (PyErr_Occurred ())
278 gdbpy_print_stack ();
279 return EXT_LANG_RC_ERROR
;
284 worker
= new_python_xmethod_worker (py_worker
.get (),
286 VEC_safe_push (xmethod_worker_ptr
, worker_vec
, worker
);
291 struct xmethod_worker
*worker
;
293 worker
= new_python_xmethod_worker (match_result
.get (),
295 VEC_safe_push (xmethod_worker_ptr
, worker_vec
, worker
);
299 *dm_vec
= worker_vec
;
301 return EXT_LANG_RC_OK
;
304 /* Implementation of get_xmethod_arg_types for Python. */
307 gdbpy_get_xmethod_arg_types (const struct extension_language_defn
*extlang
,
308 struct xmethod_worker
*worker
,
309 int *nargs
, struct type
***arg_types
)
311 struct gdbpy_worker_data
*worker_data
312 = (struct gdbpy_worker_data
*) worker
->data
;
313 PyObject
*py_worker
= worker_data
->worker
;
314 PyObject
*get_arg_types_method
;
315 PyObject
*py_argtype_list
, *list_iter
= NULL
, *item
;
316 struct cleanup
*cleanups
;
317 struct type
**type_array
, *obj_type
;
318 int i
= 1, arg_count
;
320 /* Set nargs to -1 so that any premature return from this function returns
321 an invalid/unusable number of arg types. */
324 cleanups
= ensure_python_env (get_current_arch (), current_language
);
326 get_arg_types_method
= PyObject_GetAttrString (py_worker
,
327 get_arg_types_method_name
);
328 if (get_arg_types_method
== NULL
)
330 gdbpy_print_stack ();
331 do_cleanups (cleanups
);
333 return EXT_LANG_RC_ERROR
;
335 make_cleanup_py_decref (get_arg_types_method
);
337 py_argtype_list
= PyObject_CallMethodObjArgs (py_worker
,
338 py_get_arg_types_method_name
,
340 if (py_argtype_list
== NULL
)
342 gdbpy_print_stack ();
343 do_cleanups (cleanups
);
345 return EXT_LANG_RC_ERROR
;
347 make_cleanup_py_decref (py_argtype_list
);
348 if (py_argtype_list
== Py_None
)
350 else if (PySequence_Check (py_argtype_list
))
352 arg_count
= PySequence_Size (py_argtype_list
);
355 gdbpy_print_stack ();
356 do_cleanups (cleanups
);
358 return EXT_LANG_RC_ERROR
;
361 list_iter
= PyObject_GetIter (py_argtype_list
);
362 if (list_iter
== NULL
)
364 gdbpy_print_stack ();
365 do_cleanups (cleanups
);
367 return EXT_LANG_RC_ERROR
;
369 make_cleanup_py_decref (list_iter
);
374 /* Include the 'this' argument in the size. */
375 type_array
= XCNEWVEC (struct type
*, arg_count
+ 1);
377 if (list_iter
!= NULL
)
379 while ((item
= PyIter_Next (list_iter
)) != NULL
)
381 struct type
*arg_type
= type_object_to_type (item
);
384 if (arg_type
== NULL
)
386 PyErr_SetString (PyExc_TypeError
,
387 _("Arg type returned by the get_arg_types "
388 "method of a debug method worker object is "
389 "not a gdb.Type object."));
393 type_array
[i
] = arg_type
;
397 else if (arg_count
== 1)
399 /* py_argtype_list is not actually a list but a single gdb.Type
401 struct type
*arg_type
= type_object_to_type (py_argtype_list
);
403 if (arg_type
== NULL
)
405 PyErr_SetString (PyExc_TypeError
,
406 _("Arg type returned by the get_arg_types method "
407 "of an xmethod worker object is not a gdb.Type "
412 type_array
[i
] = arg_type
;
416 if (PyErr_Occurred ())
418 gdbpy_print_stack ();
419 do_cleanups (cleanups
);
422 return EXT_LANG_RC_ERROR
;
425 /* Add the type of 'this' as the first argument. The 'this' pointer should
426 be a 'const' value. Hence, create a 'const' variant of the 'this' pointer
428 obj_type
= type_object_to_type (worker_data
->this_type
);
429 type_array
[0] = make_cv_type (1, 0, lookup_pointer_type (obj_type
), NULL
);
431 *arg_types
= type_array
;
432 do_cleanups (cleanups
);
434 return EXT_LANG_RC_OK
;
437 /* Implementation of get_xmethod_result_type for Python. */
440 gdbpy_get_xmethod_result_type (const struct extension_language_defn
*extlang
,
441 struct xmethod_worker
*worker
,
443 struct value
**args
, int nargs
,
444 struct type
**result_type_ptr
)
446 struct gdbpy_worker_data
*worker_data
447 = (struct gdbpy_worker_data
*) worker
->data
;
448 PyObject
*py_worker
= worker_data
->worker
;
449 PyObject
*py_value_obj
, *py_arg_tuple
, *py_result_type
;
450 PyObject
*get_result_type_method
;
451 struct type
*obj_type
, *this_type
;
452 struct cleanup
*cleanups
;
455 cleanups
= ensure_python_env (get_current_arch (), current_language
);
457 /* First see if there is a get_result_type method.
458 If not this could be an old xmethod (pre 7.9.1). */
459 get_result_type_method
460 = PyObject_GetAttrString (py_worker
, get_result_type_method_name
);
461 if (get_result_type_method
== NULL
)
464 do_cleanups (cleanups
);
465 *result_type_ptr
= NULL
;
466 return EXT_LANG_RC_OK
;
468 make_cleanup_py_decref (get_result_type_method
);
470 obj_type
= check_typedef (value_type (obj
));
471 this_type
= check_typedef (type_object_to_type (worker_data
->this_type
));
472 if (TYPE_CODE (obj_type
) == TYPE_CODE_PTR
)
474 struct type
*this_ptr
= lookup_pointer_type (this_type
);
476 if (!types_equal (obj_type
, this_ptr
))
477 obj
= value_cast (this_ptr
, obj
);
479 else if (TYPE_CODE (obj_type
) == TYPE_CODE_REF
)
481 struct type
*this_ref
= lookup_reference_type (this_type
);
483 if (!types_equal (obj_type
, this_ref
))
484 obj
= value_cast (this_ref
, obj
);
488 if (!types_equal (obj_type
, this_type
))
489 obj
= value_cast (this_type
, obj
);
491 py_value_obj
= value_to_value_object (obj
);
492 if (py_value_obj
== NULL
)
494 make_cleanup_py_decref (py_value_obj
);
496 py_arg_tuple
= PyTuple_New (nargs
+ 1);
497 if (py_arg_tuple
== NULL
)
499 make_cleanup_py_decref (py_arg_tuple
);
501 /* PyTuple_SET_ITEM steals the reference of the element. Hence INCREF the
502 reference to the 'this' object as we have a cleanup to DECREF it. */
503 Py_INCREF (py_value_obj
);
504 PyTuple_SET_ITEM (py_arg_tuple
, 0, py_value_obj
);
506 for (i
= 0; i
< nargs
; i
++)
508 PyObject
*py_value_arg
= value_to_value_object (args
[i
]);
510 if (py_value_arg
== NULL
)
512 PyTuple_SET_ITEM (py_arg_tuple
, i
+ 1, py_value_arg
);
515 py_result_type
= PyObject_CallObject (get_result_type_method
, py_arg_tuple
);
516 if (py_result_type
== NULL
)
518 make_cleanup_py_decref (py_result_type
);
520 *result_type_ptr
= type_object_to_type (py_result_type
);
521 if (*result_type_ptr
== NULL
)
523 PyErr_SetString (PyExc_TypeError
,
524 _("Type returned by the get_result_type method of an"
525 " xmethod worker object is not a gdb.Type object."));
529 do_cleanups (cleanups
);
530 return EXT_LANG_RC_OK
;
533 gdbpy_print_stack ();
534 do_cleanups (cleanups
);
535 return EXT_LANG_RC_ERROR
;
538 /* Implementation of invoke_xmethod for Python. */
541 gdbpy_invoke_xmethod (const struct extension_language_defn
*extlang
,
542 struct xmethod_worker
*worker
,
543 struct value
*obj
, struct value
**args
, int nargs
)
546 struct cleanup
*cleanups
;
547 PyObject
*py_value_obj
, *py_arg_tuple
, *py_result
;
548 struct type
*obj_type
, *this_type
;
549 struct value
*res
= NULL
;
550 struct gdbpy_worker_data
*worker_data
551 = (struct gdbpy_worker_data
*) worker
->data
;
552 PyObject
*xmethod_worker
= worker_data
->worker
;
554 cleanups
= ensure_python_env (get_current_arch (), current_language
);
556 obj_type
= check_typedef (value_type (obj
));
557 this_type
= check_typedef (type_object_to_type (worker_data
->this_type
));
558 if (TYPE_CODE (obj_type
) == TYPE_CODE_PTR
)
560 struct type
*this_ptr
= lookup_pointer_type (this_type
);
562 if (!types_equal (obj_type
, this_ptr
))
563 obj
= value_cast (this_ptr
, obj
);
565 else if (TYPE_CODE (obj_type
) == TYPE_CODE_REF
)
567 struct type
*this_ref
= lookup_reference_type (this_type
);
569 if (!types_equal (obj_type
, this_ref
))
570 obj
= value_cast (this_ref
, obj
);
574 if (!types_equal (obj_type
, this_type
))
575 obj
= value_cast (this_type
, obj
);
577 py_value_obj
= value_to_value_object (obj
);
578 if (py_value_obj
== NULL
)
580 gdbpy_print_stack ();
581 error (_("Error while executing Python code."));
583 make_cleanup_py_decref (py_value_obj
);
585 py_arg_tuple
= PyTuple_New (nargs
+ 1);
586 if (py_arg_tuple
== NULL
)
588 gdbpy_print_stack ();
589 error (_("Error while executing Python code."));
591 make_cleanup_py_decref (py_arg_tuple
);
593 /* PyTuple_SET_ITEM steals the reference of the element. Hence INCREF the
594 reference to the 'this' object as we have a cleanup to DECREF it. */
595 Py_INCREF (py_value_obj
);
596 PyTuple_SET_ITEM (py_arg_tuple
, 0, py_value_obj
);
598 for (i
= 0; i
< nargs
; i
++)
600 PyObject
*py_value_arg
= value_to_value_object (args
[i
]);
602 if (py_value_arg
== NULL
)
604 gdbpy_print_stack ();
605 error (_("Error while executing Python code."));
608 PyTuple_SET_ITEM (py_arg_tuple
, i
+ 1, py_value_arg
);
611 py_result
= PyObject_CallObject (xmethod_worker
, py_arg_tuple
);
612 if (py_result
== NULL
)
614 gdbpy_print_stack ();
615 error (_("Error while executing Python code."));
617 make_cleanup_py_decref (py_result
);
619 if (py_result
!= Py_None
)
621 res
= convert_value_from_python (py_result
);
624 gdbpy_print_stack ();
625 error (_("Error while executing Python code."));
630 res
= allocate_value (lookup_typename (python_language
, python_gdbarch
,
634 do_cleanups (cleanups
);
639 /* Creates a new Python xmethod_worker object.
640 The new object has data of type 'struct gdbpy_worker_data' composed
641 with the components PY_WORKER and THIS_TYPE. */
643 static struct xmethod_worker
*
644 new_python_xmethod_worker (PyObject
*py_worker
, PyObject
*this_type
)
646 struct gdbpy_worker_data
*data
;
648 gdb_assert (py_worker
!= NULL
&& this_type
!= NULL
);
650 data
= XCNEW (struct gdbpy_worker_data
);
651 data
->worker
= py_worker
;
652 data
->this_type
= this_type
;
653 Py_INCREF (py_worker
);
654 Py_INCREF (this_type
);
656 return new_xmethod_worker (&extension_language_python
, data
);
660 gdbpy_initialize_xmethods (void)
662 py_match_method_name
= PyString_FromString (match_method_name
);
663 if (py_match_method_name
== NULL
)
666 py_get_arg_types_method_name
667 = PyString_FromString (get_arg_types_method_name
);
668 if (py_get_arg_types_method_name
== NULL
)