1 /* Support for debug methods in Python.
3 Copyright (C) 2013-2016 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"
30 static const char enabled_field_name
[] = "enabled";
31 static const char match_method_name
[] = "match";
32 static const char get_arg_types_method_name
[] = "get_arg_types";
33 static const char get_result_type_method_name
[] = "get_result_type";
34 static const char invoke_method_name
[] = "invoke";
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
;
39 static PyObject
*py_get_result_type_method_name
= NULL
;
40 static PyObject
*py_invoke_method_name
= NULL
;
42 struct gdbpy_worker_data
48 static struct xmethod_worker
*new_python_xmethod_worker (PyObject
*item
,
49 PyObject
*py_obj_type
);
51 /* Implementation of free_xmethod_worker_data for Python. */
54 gdbpy_free_xmethod_worker_data (const struct extension_language_defn
*extlang
,
57 struct gdbpy_worker_data
*worker_data
= (struct gdbpy_worker_data
*) data
;
58 struct cleanup
*cleanups
;
60 gdb_assert (worker_data
->worker
!= NULL
&& worker_data
->this_type
!= NULL
);
62 /* We don't do much here, but we still need the GIL. */
63 cleanups
= ensure_python_env (get_current_arch (), current_language
);
65 Py_DECREF (worker_data
->worker
);
66 Py_DECREF (worker_data
->this_type
);
69 do_cleanups (cleanups
);
72 /* Implementation of clone_xmethod_worker_data for Python. */
75 gdbpy_clone_xmethod_worker_data (const struct extension_language_defn
*extlang
,
78 struct gdbpy_worker_data
*worker_data
79 = (struct gdbpy_worker_data
*) data
, *new_data
;
80 struct cleanup
*cleanups
;
82 gdb_assert (worker_data
->worker
!= NULL
&& worker_data
->this_type
!= NULL
);
84 /* We don't do much here, but we still need the GIL. */
85 cleanups
= ensure_python_env (get_current_arch (), current_language
);
87 new_data
= XCNEW (struct gdbpy_worker_data
);
88 new_data
->worker
= worker_data
->worker
;
89 new_data
->this_type
= worker_data
->this_type
;
90 Py_INCREF (new_data
->worker
);
91 Py_INCREF (new_data
->this_type
);
93 do_cleanups (cleanups
);
98 /* Invoke the "match" method of the MATCHER and return a new reference
99 to the result. Returns NULL on error. */
102 invoke_match_method (PyObject
*matcher
, PyObject
*py_obj_type
,
103 const char *xmethod_name
)
105 PyObject
*py_xmethod_name
;
106 PyObject
*match_method
, *enabled_field
, *match_result
;
107 struct cleanup
*cleanups
;
110 cleanups
= make_cleanup (null_cleanup
, NULL
);
112 enabled_field
= PyObject_GetAttrString (matcher
, enabled_field_name
);
113 if (enabled_field
== NULL
)
115 do_cleanups (cleanups
);
118 make_cleanup_py_decref (enabled_field
);
120 enabled
= PyObject_IsTrue (enabled_field
);
123 do_cleanups (cleanups
);
128 /* Return 'None' if the matcher is not enabled. */
129 do_cleanups (cleanups
);
133 match_method
= PyObject_GetAttrString (matcher
, match_method_name
);
134 if (match_method
== NULL
)
136 do_cleanups (cleanups
);
139 make_cleanup_py_decref (match_method
);
141 py_xmethod_name
= PyString_FromString (xmethod_name
);
142 if (py_xmethod_name
== NULL
)
144 do_cleanups (cleanups
);
147 make_cleanup_py_decref (py_xmethod_name
);
149 match_result
= PyObject_CallMethodObjArgs (matcher
,
150 py_match_method_name
,
155 do_cleanups (cleanups
);
160 /* Implementation of get_matching_xmethod_workers for Python. */
163 gdbpy_get_matching_xmethod_workers
164 (const struct extension_language_defn
*extlang
,
165 struct type
*obj_type
, const char *method_name
,
166 xmethod_worker_vec
**dm_vec
)
168 struct cleanup
*cleanups
;
169 struct objfile
*objfile
;
170 VEC (xmethod_worker_ptr
) *worker_vec
= NULL
;
171 PyObject
*py_type
, *py_progspace
;
172 PyObject
*py_xmethod_matcher_list
= NULL
, *list_iter
, *matcher
;
174 gdb_assert (obj_type
!= NULL
&& method_name
!= NULL
);
176 cleanups
= ensure_python_env (get_current_arch (), current_language
);
178 py_type
= type_to_type_object (obj_type
);
181 gdbpy_print_stack ();
182 do_cleanups (cleanups
);
184 return EXT_LANG_RC_ERROR
;
186 make_cleanup_py_decref (py_type
);
188 /* Create an empty list of debug methods. */
189 py_xmethod_matcher_list
= PyList_New (0);
190 if (py_xmethod_matcher_list
== NULL
)
192 gdbpy_print_stack ();
193 do_cleanups (cleanups
);
195 return EXT_LANG_RC_ERROR
;
198 /* Gather debug method matchers registered with the object files.
199 This could be done differently by iterating over each objfile's matcher
200 list individually, but there's no data yet to show it's needed. */
201 ALL_OBJFILES (objfile
)
203 PyObject
*py_objfile
= objfile_to_objfile_object (objfile
);
204 PyObject
*objfile_matchers
, *temp
= py_xmethod_matcher_list
;
206 if (py_objfile
== NULL
)
208 gdbpy_print_stack ();
209 Py_DECREF (py_xmethod_matcher_list
);
210 do_cleanups (cleanups
);
212 return EXT_LANG_RC_ERROR
;
215 objfile_matchers
= objfpy_get_xmethods (py_objfile
, NULL
);
216 py_xmethod_matcher_list
= PySequence_Concat (temp
, objfile_matchers
);
218 Py_DECREF (objfile_matchers
);
219 if (py_xmethod_matcher_list
== NULL
)
221 gdbpy_print_stack ();
222 do_cleanups (cleanups
);
224 return EXT_LANG_RC_ERROR
;
228 /* Gather debug methods matchers registered with the current program
230 py_progspace
= pspace_to_pspace_object (current_program_space
);
231 if (py_progspace
!= NULL
)
233 PyObject
*temp
= py_xmethod_matcher_list
;
234 PyObject
*pspace_matchers
= pspy_get_xmethods (py_progspace
, NULL
);
236 py_xmethod_matcher_list
= PySequence_Concat (temp
, pspace_matchers
);
238 Py_DECREF (pspace_matchers
);
239 if (py_xmethod_matcher_list
== NULL
)
241 gdbpy_print_stack ();
242 do_cleanups (cleanups
);
244 return EXT_LANG_RC_ERROR
;
249 gdbpy_print_stack ();
250 Py_DECREF (py_xmethod_matcher_list
);
251 do_cleanups (cleanups
);
253 return EXT_LANG_RC_ERROR
;
256 /* Gather debug method matchers registered globally. */
257 if (gdb_python_module
!= NULL
258 && PyObject_HasAttrString (gdb_python_module
, matchers_attr_str
))
260 PyObject
*gdb_matchers
;
261 PyObject
*temp
= py_xmethod_matcher_list
;
263 gdb_matchers
= PyObject_GetAttrString (gdb_python_module
,
265 if (gdb_matchers
!= NULL
)
267 py_xmethod_matcher_list
= PySequence_Concat (temp
, gdb_matchers
);
269 Py_DECREF (gdb_matchers
);
270 if (py_xmethod_matcher_list
== NULL
)
272 gdbpy_print_stack ();
273 do_cleanups (cleanups
);
275 return EXT_LANG_RC_ERROR
;
280 gdbpy_print_stack ();
281 Py_DECREF (py_xmethod_matcher_list
);
282 do_cleanups (cleanups
);
284 return EXT_LANG_RC_ERROR
;
288 /* Safe to make a cleanup for py_xmethod_matcher_list now as it
289 will not change any more. */
290 make_cleanup_py_decref (py_xmethod_matcher_list
);
292 list_iter
= PyObject_GetIter (py_xmethod_matcher_list
);
293 if (list_iter
== NULL
)
295 gdbpy_print_stack ();
296 do_cleanups (cleanups
);
298 return EXT_LANG_RC_ERROR
;
300 while ((matcher
= PyIter_Next (list_iter
)) != NULL
)
302 PyObject
*match_result
= invoke_match_method (matcher
, py_type
,
305 if (match_result
== NULL
)
307 gdbpy_print_stack ();
309 do_cleanups (cleanups
);
311 return EXT_LANG_RC_ERROR
;
313 if (match_result
== Py_None
)
314 ; /* This means there was no match. */
315 else if (PySequence_Check (match_result
))
317 PyObject
*iter
= PyObject_GetIter (match_result
);
322 gdbpy_print_stack ();
324 Py_DECREF (match_result
);
325 do_cleanups (cleanups
);
327 return EXT_LANG_RC_ERROR
;
329 while ((py_worker
= PyIter_Next (iter
)) != NULL
)
331 struct xmethod_worker
*worker
;
333 worker
= new_python_xmethod_worker (py_worker
, py_type
);
334 VEC_safe_push (xmethod_worker_ptr
, worker_vec
, worker
);
335 Py_DECREF (py_worker
);
338 /* Report any error that could have occurred while iterating. */
339 if (PyErr_Occurred ())
341 gdbpy_print_stack ();
343 Py_DECREF (match_result
);
344 do_cleanups (cleanups
);
346 return EXT_LANG_RC_ERROR
;
351 struct xmethod_worker
*worker
;
353 worker
= new_python_xmethod_worker (match_result
, py_type
);
354 VEC_safe_push (xmethod_worker_ptr
, worker_vec
, worker
);
357 Py_DECREF (match_result
);
360 Py_DECREF (list_iter
);
361 /* Report any error that could have occurred while iterating. */
362 if (PyErr_Occurred ())
364 gdbpy_print_stack ();
365 do_cleanups (cleanups
);
367 return EXT_LANG_RC_ERROR
;
370 do_cleanups (cleanups
);
371 *dm_vec
= worker_vec
;
373 return EXT_LANG_RC_OK
;
376 /* Implementation of get_xmethod_arg_types for Python. */
379 gdbpy_get_xmethod_arg_types (const struct extension_language_defn
*extlang
,
380 struct xmethod_worker
*worker
,
381 int *nargs
, struct type
***arg_types
)
383 struct gdbpy_worker_data
*worker_data
384 = (struct gdbpy_worker_data
*) worker
->data
;
385 PyObject
*py_worker
= worker_data
->worker
;
386 PyObject
*get_arg_types_method
;
387 PyObject
*py_argtype_list
, *list_iter
= NULL
, *item
;
388 struct cleanup
*cleanups
;
389 struct type
**type_array
, *obj_type
;
390 int i
= 1, arg_count
;
392 /* Set nargs to -1 so that any premature return from this function returns
393 an invalid/unusable number of arg types. */
396 cleanups
= ensure_python_env (get_current_arch (), current_language
);
398 get_arg_types_method
= PyObject_GetAttrString (py_worker
,
399 get_arg_types_method_name
);
400 if (get_arg_types_method
== NULL
)
402 gdbpy_print_stack ();
403 do_cleanups (cleanups
);
405 return EXT_LANG_RC_ERROR
;
407 make_cleanup_py_decref (get_arg_types_method
);
409 py_argtype_list
= PyObject_CallMethodObjArgs (py_worker
,
410 py_get_arg_types_method_name
,
412 if (py_argtype_list
== NULL
)
414 gdbpy_print_stack ();
415 do_cleanups (cleanups
);
417 return EXT_LANG_RC_ERROR
;
419 make_cleanup_py_decref (py_argtype_list
);
420 if (py_argtype_list
== Py_None
)
422 else if (PySequence_Check (py_argtype_list
))
424 arg_count
= PySequence_Size (py_argtype_list
);
427 gdbpy_print_stack ();
428 do_cleanups (cleanups
);
430 return EXT_LANG_RC_ERROR
;
433 list_iter
= PyObject_GetIter (py_argtype_list
);
434 if (list_iter
== NULL
)
436 gdbpy_print_stack ();
437 do_cleanups (cleanups
);
439 return EXT_LANG_RC_ERROR
;
441 make_cleanup_py_decref (list_iter
);
446 /* Include the 'this' argument in the size. */
447 type_array
= XCNEWVEC (struct type
*, arg_count
+ 1);
449 if (list_iter
!= NULL
)
451 while ((item
= PyIter_Next (list_iter
)) != NULL
)
453 struct type
*arg_type
= type_object_to_type (item
);
456 if (arg_type
== NULL
)
458 PyErr_SetString (PyExc_TypeError
,
459 _("Arg type returned by the get_arg_types "
460 "method of a debug method worker object is "
461 "not a gdb.Type object."));
465 type_array
[i
] = arg_type
;
469 else if (arg_count
== 1)
471 /* py_argtype_list is not actually a list but a single gdb.Type
473 struct type
*arg_type
= type_object_to_type (py_argtype_list
);
475 if (arg_type
== NULL
)
477 PyErr_SetString (PyExc_TypeError
,
478 _("Arg type returned by the get_arg_types method "
479 "of an xmethod worker object is not a gdb.Type "
484 type_array
[i
] = arg_type
;
488 if (PyErr_Occurred ())
490 gdbpy_print_stack ();
491 do_cleanups (cleanups
);
494 return EXT_LANG_RC_ERROR
;
497 /* Add the type of 'this' as the first argument. The 'this' pointer should
498 be a 'const' value. Hence, create a 'const' variant of the 'this' pointer
500 obj_type
= type_object_to_type (worker_data
->this_type
);
501 type_array
[0] = make_cv_type (1, 0, lookup_pointer_type (obj_type
), NULL
);
503 *arg_types
= type_array
;
504 do_cleanups (cleanups
);
506 return EXT_LANG_RC_OK
;
509 /* Implementation of get_xmethod_result_type for Python. */
512 gdbpy_get_xmethod_result_type (const struct extension_language_defn
*extlang
,
513 struct xmethod_worker
*worker
,
515 struct value
**args
, int nargs
,
516 struct type
**result_type_ptr
)
518 struct gdbpy_worker_data
*worker_data
519 = (struct gdbpy_worker_data
*) worker
->data
;
520 PyObject
*py_worker
= worker_data
->worker
;
521 PyObject
*py_value_obj
, *py_arg_tuple
, *py_result_type
;
522 PyObject
*get_result_type_method
;
523 struct type
*obj_type
, *this_type
;
524 struct cleanup
*cleanups
;
527 cleanups
= ensure_python_env (get_current_arch (), current_language
);
529 /* First see if there is a get_result_type method.
530 If not this could be an old xmethod (pre 7.9.1). */
531 get_result_type_method
532 = PyObject_GetAttrString (py_worker
, get_result_type_method_name
);
533 if (get_result_type_method
== NULL
)
536 do_cleanups (cleanups
);
537 *result_type_ptr
= NULL
;
538 return EXT_LANG_RC_OK
;
540 make_cleanup_py_decref (get_result_type_method
);
542 obj_type
= check_typedef (value_type (obj
));
543 this_type
= check_typedef (type_object_to_type (worker_data
->this_type
));
544 if (TYPE_CODE (obj_type
) == TYPE_CODE_PTR
)
546 struct type
*this_ptr
= lookup_pointer_type (this_type
);
548 if (!types_equal (obj_type
, this_ptr
))
549 obj
= value_cast (this_ptr
, obj
);
551 else if (TYPE_CODE (obj_type
) == TYPE_CODE_REF
)
553 struct type
*this_ref
= lookup_reference_type (this_type
);
555 if (!types_equal (obj_type
, this_ref
))
556 obj
= value_cast (this_ref
, obj
);
560 if (!types_equal (obj_type
, this_type
))
561 obj
= value_cast (this_type
, obj
);
563 py_value_obj
= value_to_value_object (obj
);
564 if (py_value_obj
== NULL
)
566 make_cleanup_py_decref (py_value_obj
);
568 py_arg_tuple
= PyTuple_New (nargs
+ 1);
569 if (py_arg_tuple
== NULL
)
571 make_cleanup_py_decref (py_arg_tuple
);
573 /* PyTuple_SET_ITEM steals the reference of the element. Hence INCREF the
574 reference to the 'this' object as we have a cleanup to DECREF it. */
575 Py_INCREF (py_value_obj
);
576 PyTuple_SET_ITEM (py_arg_tuple
, 0, py_value_obj
);
578 for (i
= 0; i
< nargs
; i
++)
580 PyObject
*py_value_arg
= value_to_value_object (args
[i
]);
582 if (py_value_arg
== NULL
)
584 PyTuple_SET_ITEM (py_arg_tuple
, i
+ 1, py_value_arg
);
587 py_result_type
= PyObject_CallObject (get_result_type_method
, py_arg_tuple
);
588 if (py_result_type
== NULL
)
590 make_cleanup_py_decref (py_result_type
);
592 *result_type_ptr
= type_object_to_type (py_result_type
);
593 if (*result_type_ptr
== NULL
)
595 PyErr_SetString (PyExc_TypeError
,
596 _("Type returned by the get_result_type method of an"
597 " xmethod worker object is not a gdb.Type object."));
601 do_cleanups (cleanups
);
602 return EXT_LANG_RC_OK
;
605 gdbpy_print_stack ();
606 do_cleanups (cleanups
);
607 return EXT_LANG_RC_ERROR
;
610 /* Implementation of invoke_xmethod for Python. */
613 gdbpy_invoke_xmethod (const struct extension_language_defn
*extlang
,
614 struct xmethod_worker
*worker
,
615 struct value
*obj
, struct value
**args
, int nargs
)
618 struct cleanup
*cleanups
;
619 PyObject
*py_value_obj
, *py_arg_tuple
, *py_result
;
620 struct type
*obj_type
, *this_type
;
621 struct value
*res
= NULL
;
622 struct gdbpy_worker_data
*worker_data
623 = (struct gdbpy_worker_data
*) worker
->data
;
624 PyObject
*xmethod_worker
= worker_data
->worker
;
626 cleanups
= ensure_python_env (get_current_arch (), current_language
);
628 obj_type
= check_typedef (value_type (obj
));
629 this_type
= check_typedef (type_object_to_type (worker_data
->this_type
));
630 if (TYPE_CODE (obj_type
) == TYPE_CODE_PTR
)
632 struct type
*this_ptr
= lookup_pointer_type (this_type
);
634 if (!types_equal (obj_type
, this_ptr
))
635 obj
= value_cast (this_ptr
, obj
);
637 else if (TYPE_CODE (obj_type
) == TYPE_CODE_REF
)
639 struct type
*this_ref
= lookup_reference_type (this_type
);
641 if (!types_equal (obj_type
, this_ref
))
642 obj
= value_cast (this_ref
, obj
);
646 if (!types_equal (obj_type
, this_type
))
647 obj
= value_cast (this_type
, obj
);
649 py_value_obj
= value_to_value_object (obj
);
650 if (py_value_obj
== NULL
)
652 gdbpy_print_stack ();
653 error (_("Error while executing Python code."));
655 make_cleanup_py_decref (py_value_obj
);
657 py_arg_tuple
= PyTuple_New (nargs
+ 1);
658 if (py_arg_tuple
== NULL
)
660 gdbpy_print_stack ();
661 error (_("Error while executing Python code."));
663 make_cleanup_py_decref (py_arg_tuple
);
665 /* PyTuple_SET_ITEM steals the reference of the element. Hence INCREF the
666 reference to the 'this' object as we have a cleanup to DECREF it. */
667 Py_INCREF (py_value_obj
);
668 PyTuple_SET_ITEM (py_arg_tuple
, 0, py_value_obj
);
670 for (i
= 0; i
< nargs
; i
++)
672 PyObject
*py_value_arg
= value_to_value_object (args
[i
]);
674 if (py_value_arg
== NULL
)
676 gdbpy_print_stack ();
677 error (_("Error while executing Python code."));
680 PyTuple_SET_ITEM (py_arg_tuple
, i
+ 1, py_value_arg
);
683 py_result
= PyObject_CallObject (xmethod_worker
, py_arg_tuple
);
684 if (py_result
== NULL
)
686 gdbpy_print_stack ();
687 error (_("Error while executing Python code."));
689 make_cleanup_py_decref (py_result
);
691 if (py_result
!= Py_None
)
693 res
= convert_value_from_python (py_result
);
696 gdbpy_print_stack ();
697 error (_("Error while executing Python code."));
702 res
= allocate_value (lookup_typename (python_language
, python_gdbarch
,
706 do_cleanups (cleanups
);
711 /* Creates a new Python xmethod_worker object.
712 The new object has data of type 'struct gdbpy_worker_data' composed
713 with the components PY_WORKER and THIS_TYPE. */
715 static struct xmethod_worker
*
716 new_python_xmethod_worker (PyObject
*py_worker
, PyObject
*this_type
)
718 struct gdbpy_worker_data
*data
;
720 gdb_assert (py_worker
!= NULL
&& this_type
!= NULL
);
722 data
= XCNEW (struct gdbpy_worker_data
);
723 data
->worker
= py_worker
;
724 data
->this_type
= this_type
;
725 Py_INCREF (py_worker
);
726 Py_INCREF (this_type
);
728 return new_xmethod_worker (&extension_language_python
, data
);
732 gdbpy_initialize_xmethods (void)
734 py_match_method_name
= PyString_FromString (match_method_name
);
735 if (py_match_method_name
== NULL
)
738 py_invoke_method_name
= PyString_FromString (invoke_method_name
);
739 if (py_invoke_method_name
== NULL
)
742 py_get_arg_types_method_name
743 = PyString_FromString (get_arg_types_method_name
);
744 if (py_get_arg_types_method_name
== NULL
)
747 py_get_result_type_method_name
748 = PyString_FromString (get_result_type_method_name
);
749 if (py_get_result_type_method_name
== NULL
)