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"
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 matchers_attr_str
[] = "xmethods";
36 static PyObject
*py_match_method_name
= NULL
;
37 static PyObject
*py_get_arg_types_method_name
= NULL
;
39 struct gdbpy_worker_data
45 static struct xmethod_worker
*new_python_xmethod_worker (PyObject
*item
,
46 PyObject
*py_obj_type
);
48 /* Implementation of free_xmethod_worker_data for Python. */
51 gdbpy_free_xmethod_worker_data (const struct extension_language_defn
*extlang
,
54 struct gdbpy_worker_data
*worker_data
= (struct gdbpy_worker_data
*) data
;
55 struct cleanup
*cleanups
;
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 cleanups
= ensure_python_env (get_current_arch (), current_language
);
62 Py_DECREF (worker_data
->worker
);
63 Py_DECREF (worker_data
->this_type
);
66 do_cleanups (cleanups
);
69 /* Implementation of clone_xmethod_worker_data for Python. */
72 gdbpy_clone_xmethod_worker_data (const struct extension_language_defn
*extlang
,
75 struct gdbpy_worker_data
*worker_data
76 = (struct gdbpy_worker_data
*) data
, *new_data
;
77 struct cleanup
*cleanups
;
79 gdb_assert (worker_data
->worker
!= NULL
&& worker_data
->this_type
!= NULL
);
81 /* We don't do much here, but we still need the GIL. */
82 cleanups
= ensure_python_env (get_current_arch (), current_language
);
84 new_data
= XCNEW (struct gdbpy_worker_data
);
85 new_data
->worker
= worker_data
->worker
;
86 new_data
->this_type
= worker_data
->this_type
;
87 Py_INCREF (new_data
->worker
);
88 Py_INCREF (new_data
->this_type
);
90 do_cleanups (cleanups
);
95 /* Invoke the "match" method of the MATCHER and return a new reference
96 to the result. Returns NULL on error. */
99 invoke_match_method (PyObject
*matcher
, PyObject
*py_obj_type
,
100 const char *xmethod_name
)
102 PyObject
*py_xmethod_name
;
103 PyObject
*match_method
, *enabled_field
, *match_result
;
104 struct cleanup
*cleanups
;
107 cleanups
= make_cleanup (null_cleanup
, NULL
);
109 enabled_field
= PyObject_GetAttrString (matcher
, enabled_field_name
);
110 if (enabled_field
== NULL
)
112 do_cleanups (cleanups
);
115 make_cleanup_py_decref (enabled_field
);
117 enabled
= PyObject_IsTrue (enabled_field
);
120 do_cleanups (cleanups
);
125 /* Return 'None' if the matcher is not enabled. */
126 do_cleanups (cleanups
);
130 match_method
= PyObject_GetAttrString (matcher
, match_method_name
);
131 if (match_method
== NULL
)
133 do_cleanups (cleanups
);
136 make_cleanup_py_decref (match_method
);
138 py_xmethod_name
= PyString_FromString (xmethod_name
);
139 if (py_xmethod_name
== NULL
)
141 do_cleanups (cleanups
);
144 make_cleanup_py_decref (py_xmethod_name
);
146 match_result
= PyObject_CallMethodObjArgs (matcher
,
147 py_match_method_name
,
152 do_cleanups (cleanups
);
157 /* Implementation of get_matching_xmethod_workers for Python. */
160 gdbpy_get_matching_xmethod_workers
161 (const struct extension_language_defn
*extlang
,
162 struct type
*obj_type
, const char *method_name
,
163 xmethod_worker_vec
**dm_vec
)
165 struct cleanup
*cleanups
;
166 struct objfile
*objfile
;
167 VEC (xmethod_worker_ptr
) *worker_vec
= NULL
;
168 PyObject
*py_type
, *py_progspace
;
169 PyObject
*py_xmethod_matcher_list
= NULL
, *list_iter
, *matcher
;
171 gdb_assert (obj_type
!= NULL
&& method_name
!= NULL
);
173 cleanups
= ensure_python_env (get_current_arch (), current_language
);
175 py_type
= type_to_type_object (obj_type
);
178 gdbpy_print_stack ();
179 do_cleanups (cleanups
);
181 return EXT_LANG_RC_ERROR
;
183 make_cleanup_py_decref (py_type
);
185 /* Create an empty list of debug methods. */
186 py_xmethod_matcher_list
= PyList_New (0);
187 if (py_xmethod_matcher_list
== NULL
)
189 gdbpy_print_stack ();
190 do_cleanups (cleanups
);
192 return EXT_LANG_RC_ERROR
;
195 /* Gather debug method matchers registered with the object files.
196 This could be done differently by iterating over each objfile's matcher
197 list individually, but there's no data yet to show it's needed. */
198 ALL_OBJFILES (objfile
)
200 PyObject
*py_objfile
= objfile_to_objfile_object (objfile
);
201 PyObject
*objfile_matchers
, *temp
= py_xmethod_matcher_list
;
203 if (py_objfile
== NULL
)
205 gdbpy_print_stack ();
206 Py_DECREF (py_xmethod_matcher_list
);
207 do_cleanups (cleanups
);
209 return EXT_LANG_RC_ERROR
;
212 objfile_matchers
= objfpy_get_xmethods (py_objfile
, NULL
);
213 py_xmethod_matcher_list
= PySequence_Concat (temp
, objfile_matchers
);
215 Py_DECREF (objfile_matchers
);
216 if (py_xmethod_matcher_list
== NULL
)
218 gdbpy_print_stack ();
219 do_cleanups (cleanups
);
221 return EXT_LANG_RC_ERROR
;
225 /* Gather debug methods matchers registered with the current program
227 py_progspace
= pspace_to_pspace_object (current_program_space
);
228 if (py_progspace
!= NULL
)
230 PyObject
*temp
= py_xmethod_matcher_list
;
231 PyObject
*pspace_matchers
= pspy_get_xmethods (py_progspace
, NULL
);
233 py_xmethod_matcher_list
= PySequence_Concat (temp
, pspace_matchers
);
235 Py_DECREF (pspace_matchers
);
236 if (py_xmethod_matcher_list
== NULL
)
238 gdbpy_print_stack ();
239 do_cleanups (cleanups
);
241 return EXT_LANG_RC_ERROR
;
246 gdbpy_print_stack ();
247 Py_DECREF (py_xmethod_matcher_list
);
248 do_cleanups (cleanups
);
250 return EXT_LANG_RC_ERROR
;
253 /* Gather debug method matchers registered globally. */
254 if (gdb_python_module
!= NULL
255 && PyObject_HasAttrString (gdb_python_module
, matchers_attr_str
))
257 PyObject
*gdb_matchers
;
258 PyObject
*temp
= py_xmethod_matcher_list
;
260 gdb_matchers
= PyObject_GetAttrString (gdb_python_module
,
262 if (gdb_matchers
!= NULL
)
264 py_xmethod_matcher_list
= PySequence_Concat (temp
, gdb_matchers
);
266 Py_DECREF (gdb_matchers
);
267 if (py_xmethod_matcher_list
== NULL
)
269 gdbpy_print_stack ();
270 do_cleanups (cleanups
);
272 return EXT_LANG_RC_ERROR
;
277 gdbpy_print_stack ();
278 Py_DECREF (py_xmethod_matcher_list
);
279 do_cleanups (cleanups
);
281 return EXT_LANG_RC_ERROR
;
285 /* Safe to make a cleanup for py_xmethod_matcher_list now as it
286 will not change any more. */
287 make_cleanup_py_decref (py_xmethod_matcher_list
);
289 list_iter
= PyObject_GetIter (py_xmethod_matcher_list
);
290 if (list_iter
== NULL
)
292 gdbpy_print_stack ();
293 do_cleanups (cleanups
);
295 return EXT_LANG_RC_ERROR
;
297 while ((matcher
= PyIter_Next (list_iter
)) != NULL
)
299 PyObject
*match_result
= invoke_match_method (matcher
, py_type
,
302 if (match_result
== NULL
)
304 gdbpy_print_stack ();
306 do_cleanups (cleanups
);
308 return EXT_LANG_RC_ERROR
;
310 if (match_result
== Py_None
)
311 ; /* This means there was no match. */
312 else if (PySequence_Check (match_result
))
314 PyObject
*iter
= PyObject_GetIter (match_result
);
319 gdbpy_print_stack ();
321 Py_DECREF (match_result
);
322 do_cleanups (cleanups
);
324 return EXT_LANG_RC_ERROR
;
326 while ((py_worker
= PyIter_Next (iter
)) != NULL
)
328 struct xmethod_worker
*worker
;
330 worker
= new_python_xmethod_worker (py_worker
, py_type
);
331 VEC_safe_push (xmethod_worker_ptr
, worker_vec
, worker
);
332 Py_DECREF (py_worker
);
335 /* Report any error that could have occurred while iterating. */
336 if (PyErr_Occurred ())
338 gdbpy_print_stack ();
340 Py_DECREF (match_result
);
341 do_cleanups (cleanups
);
343 return EXT_LANG_RC_ERROR
;
348 struct xmethod_worker
*worker
;
350 worker
= new_python_xmethod_worker (match_result
, py_type
);
351 VEC_safe_push (xmethod_worker_ptr
, worker_vec
, worker
);
354 Py_DECREF (match_result
);
357 Py_DECREF (list_iter
);
358 /* Report any error that could have occurred while iterating. */
359 if (PyErr_Occurred ())
361 gdbpy_print_stack ();
362 do_cleanups (cleanups
);
364 return EXT_LANG_RC_ERROR
;
367 do_cleanups (cleanups
);
368 *dm_vec
= worker_vec
;
370 return EXT_LANG_RC_OK
;
373 /* Implementation of get_xmethod_arg_types for Python. */
376 gdbpy_get_xmethod_arg_types (const struct extension_language_defn
*extlang
,
377 struct xmethod_worker
*worker
,
378 int *nargs
, struct type
***arg_types
)
380 struct gdbpy_worker_data
*worker_data
381 = (struct gdbpy_worker_data
*) worker
->data
;
382 PyObject
*py_worker
= worker_data
->worker
;
383 PyObject
*get_arg_types_method
;
384 PyObject
*py_argtype_list
, *list_iter
= NULL
, *item
;
385 struct cleanup
*cleanups
;
386 struct type
**type_array
, *obj_type
;
387 int i
= 1, arg_count
;
389 /* Set nargs to -1 so that any premature return from this function returns
390 an invalid/unusable number of arg types. */
393 cleanups
= ensure_python_env (get_current_arch (), current_language
);
395 get_arg_types_method
= PyObject_GetAttrString (py_worker
,
396 get_arg_types_method_name
);
397 if (get_arg_types_method
== NULL
)
399 gdbpy_print_stack ();
400 do_cleanups (cleanups
);
402 return EXT_LANG_RC_ERROR
;
404 make_cleanup_py_decref (get_arg_types_method
);
406 py_argtype_list
= PyObject_CallMethodObjArgs (py_worker
,
407 py_get_arg_types_method_name
,
409 if (py_argtype_list
== NULL
)
411 gdbpy_print_stack ();
412 do_cleanups (cleanups
);
414 return EXT_LANG_RC_ERROR
;
416 make_cleanup_py_decref (py_argtype_list
);
417 if (py_argtype_list
== Py_None
)
419 else if (PySequence_Check (py_argtype_list
))
421 arg_count
= PySequence_Size (py_argtype_list
);
424 gdbpy_print_stack ();
425 do_cleanups (cleanups
);
427 return EXT_LANG_RC_ERROR
;
430 list_iter
= PyObject_GetIter (py_argtype_list
);
431 if (list_iter
== NULL
)
433 gdbpy_print_stack ();
434 do_cleanups (cleanups
);
436 return EXT_LANG_RC_ERROR
;
438 make_cleanup_py_decref (list_iter
);
443 /* Include the 'this' argument in the size. */
444 type_array
= XCNEWVEC (struct type
*, arg_count
+ 1);
446 if (list_iter
!= NULL
)
448 while ((item
= PyIter_Next (list_iter
)) != NULL
)
450 struct type
*arg_type
= type_object_to_type (item
);
453 if (arg_type
== NULL
)
455 PyErr_SetString (PyExc_TypeError
,
456 _("Arg type returned by the get_arg_types "
457 "method of a debug method worker object is "
458 "not a gdb.Type object."));
462 type_array
[i
] = arg_type
;
466 else if (arg_count
== 1)
468 /* py_argtype_list is not actually a list but a single gdb.Type
470 struct type
*arg_type
= type_object_to_type (py_argtype_list
);
472 if (arg_type
== NULL
)
474 PyErr_SetString (PyExc_TypeError
,
475 _("Arg type returned by the get_arg_types method "
476 "of an xmethod worker object is not a gdb.Type "
481 type_array
[i
] = arg_type
;
485 if (PyErr_Occurred ())
487 gdbpy_print_stack ();
488 do_cleanups (cleanups
);
491 return EXT_LANG_RC_ERROR
;
494 /* Add the type of 'this' as the first argument. The 'this' pointer should
495 be a 'const' value. Hence, create a 'const' variant of the 'this' pointer
497 obj_type
= type_object_to_type (worker_data
->this_type
);
498 type_array
[0] = make_cv_type (1, 0, lookup_pointer_type (obj_type
), NULL
);
500 *arg_types
= type_array
;
501 do_cleanups (cleanups
);
503 return EXT_LANG_RC_OK
;
506 /* Implementation of get_xmethod_result_type for Python. */
509 gdbpy_get_xmethod_result_type (const struct extension_language_defn
*extlang
,
510 struct xmethod_worker
*worker
,
512 struct value
**args
, int nargs
,
513 struct type
**result_type_ptr
)
515 struct gdbpy_worker_data
*worker_data
516 = (struct gdbpy_worker_data
*) worker
->data
;
517 PyObject
*py_worker
= worker_data
->worker
;
518 PyObject
*py_value_obj
, *py_arg_tuple
, *py_result_type
;
519 PyObject
*get_result_type_method
;
520 struct type
*obj_type
, *this_type
;
521 struct cleanup
*cleanups
;
524 cleanups
= ensure_python_env (get_current_arch (), current_language
);
526 /* First see if there is a get_result_type method.
527 If not this could be an old xmethod (pre 7.9.1). */
528 get_result_type_method
529 = PyObject_GetAttrString (py_worker
, get_result_type_method_name
);
530 if (get_result_type_method
== NULL
)
533 do_cleanups (cleanups
);
534 *result_type_ptr
= NULL
;
535 return EXT_LANG_RC_OK
;
537 make_cleanup_py_decref (get_result_type_method
);
539 obj_type
= check_typedef (value_type (obj
));
540 this_type
= check_typedef (type_object_to_type (worker_data
->this_type
));
541 if (TYPE_CODE (obj_type
) == TYPE_CODE_PTR
)
543 struct type
*this_ptr
= lookup_pointer_type (this_type
);
545 if (!types_equal (obj_type
, this_ptr
))
546 obj
= value_cast (this_ptr
, obj
);
548 else if (TYPE_CODE (obj_type
) == TYPE_CODE_REF
)
550 struct type
*this_ref
= lookup_reference_type (this_type
);
552 if (!types_equal (obj_type
, this_ref
))
553 obj
= value_cast (this_ref
, obj
);
557 if (!types_equal (obj_type
, this_type
))
558 obj
= value_cast (this_type
, obj
);
560 py_value_obj
= value_to_value_object (obj
);
561 if (py_value_obj
== NULL
)
563 make_cleanup_py_decref (py_value_obj
);
565 py_arg_tuple
= PyTuple_New (nargs
+ 1);
566 if (py_arg_tuple
== NULL
)
568 make_cleanup_py_decref (py_arg_tuple
);
570 /* PyTuple_SET_ITEM steals the reference of the element. Hence INCREF the
571 reference to the 'this' object as we have a cleanup to DECREF it. */
572 Py_INCREF (py_value_obj
);
573 PyTuple_SET_ITEM (py_arg_tuple
, 0, py_value_obj
);
575 for (i
= 0; i
< nargs
; i
++)
577 PyObject
*py_value_arg
= value_to_value_object (args
[i
]);
579 if (py_value_arg
== NULL
)
581 PyTuple_SET_ITEM (py_arg_tuple
, i
+ 1, py_value_arg
);
584 py_result_type
= PyObject_CallObject (get_result_type_method
, py_arg_tuple
);
585 if (py_result_type
== NULL
)
587 make_cleanup_py_decref (py_result_type
);
589 *result_type_ptr
= type_object_to_type (py_result_type
);
590 if (*result_type_ptr
== NULL
)
592 PyErr_SetString (PyExc_TypeError
,
593 _("Type returned by the get_result_type method of an"
594 " xmethod worker object is not a gdb.Type object."));
598 do_cleanups (cleanups
);
599 return EXT_LANG_RC_OK
;
602 gdbpy_print_stack ();
603 do_cleanups (cleanups
);
604 return EXT_LANG_RC_ERROR
;
607 /* Implementation of invoke_xmethod for Python. */
610 gdbpy_invoke_xmethod (const struct extension_language_defn
*extlang
,
611 struct xmethod_worker
*worker
,
612 struct value
*obj
, struct value
**args
, int nargs
)
615 struct cleanup
*cleanups
;
616 PyObject
*py_value_obj
, *py_arg_tuple
, *py_result
;
617 struct type
*obj_type
, *this_type
;
618 struct value
*res
= NULL
;
619 struct gdbpy_worker_data
*worker_data
620 = (struct gdbpy_worker_data
*) worker
->data
;
621 PyObject
*xmethod_worker
= worker_data
->worker
;
623 cleanups
= ensure_python_env (get_current_arch (), current_language
);
625 obj_type
= check_typedef (value_type (obj
));
626 this_type
= check_typedef (type_object_to_type (worker_data
->this_type
));
627 if (TYPE_CODE (obj_type
) == TYPE_CODE_PTR
)
629 struct type
*this_ptr
= lookup_pointer_type (this_type
);
631 if (!types_equal (obj_type
, this_ptr
))
632 obj
= value_cast (this_ptr
, obj
);
634 else if (TYPE_CODE (obj_type
) == TYPE_CODE_REF
)
636 struct type
*this_ref
= lookup_reference_type (this_type
);
638 if (!types_equal (obj_type
, this_ref
))
639 obj
= value_cast (this_ref
, obj
);
643 if (!types_equal (obj_type
, this_type
))
644 obj
= value_cast (this_type
, obj
);
646 py_value_obj
= value_to_value_object (obj
);
647 if (py_value_obj
== NULL
)
649 gdbpy_print_stack ();
650 error (_("Error while executing Python code."));
652 make_cleanup_py_decref (py_value_obj
);
654 py_arg_tuple
= PyTuple_New (nargs
+ 1);
655 if (py_arg_tuple
== NULL
)
657 gdbpy_print_stack ();
658 error (_("Error while executing Python code."));
660 make_cleanup_py_decref (py_arg_tuple
);
662 /* PyTuple_SET_ITEM steals the reference of the element. Hence INCREF the
663 reference to the 'this' object as we have a cleanup to DECREF it. */
664 Py_INCREF (py_value_obj
);
665 PyTuple_SET_ITEM (py_arg_tuple
, 0, py_value_obj
);
667 for (i
= 0; i
< nargs
; i
++)
669 PyObject
*py_value_arg
= value_to_value_object (args
[i
]);
671 if (py_value_arg
== NULL
)
673 gdbpy_print_stack ();
674 error (_("Error while executing Python code."));
677 PyTuple_SET_ITEM (py_arg_tuple
, i
+ 1, py_value_arg
);
680 py_result
= PyObject_CallObject (xmethod_worker
, py_arg_tuple
);
681 if (py_result
== NULL
)
683 gdbpy_print_stack ();
684 error (_("Error while executing Python code."));
686 make_cleanup_py_decref (py_result
);
688 if (py_result
!= Py_None
)
690 res
= convert_value_from_python (py_result
);
693 gdbpy_print_stack ();
694 error (_("Error while executing Python code."));
699 res
= allocate_value (lookup_typename (python_language
, python_gdbarch
,
703 do_cleanups (cleanups
);
708 /* Creates a new Python xmethod_worker object.
709 The new object has data of type 'struct gdbpy_worker_data' composed
710 with the components PY_WORKER and THIS_TYPE. */
712 static struct xmethod_worker
*
713 new_python_xmethod_worker (PyObject
*py_worker
, PyObject
*this_type
)
715 struct gdbpy_worker_data
*data
;
717 gdb_assert (py_worker
!= NULL
&& this_type
!= NULL
);
719 data
= XCNEW (struct gdbpy_worker_data
);
720 data
->worker
= py_worker
;
721 data
->this_type
= this_type
;
722 Py_INCREF (py_worker
);
723 Py_INCREF (this_type
);
725 return new_xmethod_worker (&extension_language_python
, data
);
729 gdbpy_initialize_xmethods (void)
731 py_match_method_name
= PyString_FromString (match_method_name
);
732 if (py_match_method_name
== NULL
)
735 py_get_arg_types_method_name
736 = PyString_FromString (get_arg_types_method_name
);
737 if (py_get_arg_types_method_name
== NULL
)