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;
56 gdb_assert (worker_data->worker != NULL && worker_data->this_type != NULL);
58 /* We don't do much here, but we still need the GIL. */
59 gdbpy_enter enter_py (get_current_arch (), current_language);
61 Py_DECREF (worker_data->worker);
62 Py_DECREF (worker_data->this_type);
66 /* Implementation of clone_xmethod_worker_data for Python. */
69 gdbpy_clone_xmethod_worker_data (const struct extension_language_defn *extlang,
72 struct gdbpy_worker_data *worker_data
73 = (struct gdbpy_worker_data *) data, *new_data;
75 gdb_assert (worker_data->worker != NULL && worker_data->this_type != NULL);
77 /* We don't do much here, but we still need the GIL. */
78 gdbpy_enter enter_py (get_current_arch (), current_language);
80 new_data = XCNEW (struct gdbpy_worker_data);
81 new_data->worker = worker_data->worker;
82 new_data->this_type = worker_data->this_type;
83 Py_INCREF (new_data->worker);
84 Py_INCREF (new_data->this_type);
89 /* Invoke the "match" method of the MATCHER and return a new reference
90 to the result. Returns NULL on error. */
93 invoke_match_method (PyObject *matcher, PyObject *py_obj_type,
94 const char *xmethod_name)
96 PyObject *py_xmethod_name;
97 PyObject *match_method, *enabled_field, *match_result;
98 struct cleanup *cleanups;
101 cleanups = make_cleanup (null_cleanup, NULL);
103 enabled_field = PyObject_GetAttrString (matcher, enabled_field_name);
104 if (enabled_field == NULL)
106 do_cleanups (cleanups);
109 make_cleanup_py_decref (enabled_field);
111 enabled = PyObject_IsTrue (enabled_field);
114 do_cleanups (cleanups);
119 /* Return 'None' if the matcher is not enabled. */
120 do_cleanups (cleanups);
124 match_method = PyObject_GetAttrString (matcher, match_method_name);
125 if (match_method == NULL)
127 do_cleanups (cleanups);
130 make_cleanup_py_decref (match_method);
132 py_xmethod_name = PyString_FromString (xmethod_name);
133 if (py_xmethod_name == NULL)
135 do_cleanups (cleanups);
138 make_cleanup_py_decref (py_xmethod_name);
140 match_result = PyObject_CallMethodObjArgs (matcher,
141 py_match_method_name,
146 do_cleanups (cleanups);
151 /* Implementation of get_matching_xmethod_workers for Python. */
154 gdbpy_get_matching_xmethod_workers
155 (const struct extension_language_defn *extlang,
156 struct type *obj_type, const char *method_name,
157 xmethod_worker_vec **dm_vec)
159 struct cleanup *cleanups;
160 struct objfile *objfile;
161 VEC (xmethod_worker_ptr) *worker_vec = NULL;
162 PyObject *py_type, *py_progspace;
163 PyObject *py_xmethod_matcher_list = NULL, *list_iter, *matcher;
165 gdb_assert (obj_type != NULL && method_name != NULL);
167 cleanups = ensure_python_env (get_current_arch (), current_language);
169 py_type = type_to_type_object (obj_type);
172 gdbpy_print_stack ();
173 do_cleanups (cleanups);
175 return EXT_LANG_RC_ERROR;
177 make_cleanup_py_decref (py_type);
179 /* Create an empty list of debug methods. */
180 py_xmethod_matcher_list = PyList_New (0);
181 if (py_xmethod_matcher_list == NULL)
183 gdbpy_print_stack ();
184 do_cleanups (cleanups);
186 return EXT_LANG_RC_ERROR;
189 /* Gather debug method matchers registered with the object files.
190 This could be done differently by iterating over each objfile's matcher
191 list individually, but there's no data yet to show it's needed. */
192 ALL_OBJFILES (objfile)
194 PyObject *py_objfile = objfile_to_objfile_object (objfile);
195 PyObject *objfile_matchers, *temp = py_xmethod_matcher_list;
197 if (py_objfile == NULL)
199 gdbpy_print_stack ();
200 Py_DECREF (py_xmethod_matcher_list);
201 do_cleanups (cleanups);
203 return EXT_LANG_RC_ERROR;
206 objfile_matchers = objfpy_get_xmethods (py_objfile, NULL);
207 py_xmethod_matcher_list = PySequence_Concat (temp, objfile_matchers);
209 Py_DECREF (objfile_matchers);
210 if (py_xmethod_matcher_list == NULL)
212 gdbpy_print_stack ();
213 do_cleanups (cleanups);
215 return EXT_LANG_RC_ERROR;
219 /* Gather debug methods matchers registered with the current program
221 py_progspace = pspace_to_pspace_object (current_program_space);
222 if (py_progspace != NULL)
224 PyObject *temp = py_xmethod_matcher_list;
225 PyObject *pspace_matchers = pspy_get_xmethods (py_progspace, NULL);
227 py_xmethod_matcher_list = PySequence_Concat (temp, pspace_matchers);
229 Py_DECREF (pspace_matchers);
230 if (py_xmethod_matcher_list == NULL)
232 gdbpy_print_stack ();
233 do_cleanups (cleanups);
235 return EXT_LANG_RC_ERROR;
240 gdbpy_print_stack ();
241 Py_DECREF (py_xmethod_matcher_list);
242 do_cleanups (cleanups);
244 return EXT_LANG_RC_ERROR;
247 /* Gather debug method matchers registered globally. */
248 if (gdb_python_module != NULL
249 && PyObject_HasAttrString (gdb_python_module, matchers_attr_str))
251 PyObject *gdb_matchers;
252 PyObject *temp = py_xmethod_matcher_list;
254 gdb_matchers = PyObject_GetAttrString (gdb_python_module,
256 if (gdb_matchers != NULL)
258 py_xmethod_matcher_list = PySequence_Concat (temp, gdb_matchers);
260 Py_DECREF (gdb_matchers);
261 if (py_xmethod_matcher_list == NULL)
263 gdbpy_print_stack ();
264 do_cleanups (cleanups);
266 return EXT_LANG_RC_ERROR;
271 gdbpy_print_stack ();
272 Py_DECREF (py_xmethod_matcher_list);
273 do_cleanups (cleanups);
275 return EXT_LANG_RC_ERROR;
279 /* Safe to make a cleanup for py_xmethod_matcher_list now as it
280 will not change any more. */
281 make_cleanup_py_decref (py_xmethod_matcher_list);
283 list_iter = PyObject_GetIter (py_xmethod_matcher_list);
284 if (list_iter == NULL)
286 gdbpy_print_stack ();
287 do_cleanups (cleanups);
289 return EXT_LANG_RC_ERROR;
291 while ((matcher = PyIter_Next (list_iter)) != NULL)
293 PyObject *match_result = invoke_match_method (matcher, py_type,
296 if (match_result == NULL)
298 gdbpy_print_stack ();
300 do_cleanups (cleanups);
302 return EXT_LANG_RC_ERROR;
304 if (match_result == Py_None)
305 ; /* This means there was no match. */
306 else if (PySequence_Check (match_result))
308 PyObject *iter = PyObject_GetIter (match_result);
313 gdbpy_print_stack ();
315 Py_DECREF (match_result);
316 do_cleanups (cleanups);
318 return EXT_LANG_RC_ERROR;
320 while ((py_worker = PyIter_Next (iter)) != NULL)
322 struct xmethod_worker *worker;
324 worker = new_python_xmethod_worker (py_worker, py_type);
325 VEC_safe_push (xmethod_worker_ptr, worker_vec, worker);
326 Py_DECREF (py_worker);
329 /* Report any error that could have occurred while iterating. */
330 if (PyErr_Occurred ())
332 gdbpy_print_stack ();
334 Py_DECREF (match_result);
335 do_cleanups (cleanups);
337 return EXT_LANG_RC_ERROR;
342 struct xmethod_worker *worker;
344 worker = new_python_xmethod_worker (match_result, py_type);
345 VEC_safe_push (xmethod_worker_ptr, worker_vec, worker);
348 Py_DECREF (match_result);
351 Py_DECREF (list_iter);
352 /* Report any error that could have occurred while iterating. */
353 if (PyErr_Occurred ())
355 gdbpy_print_stack ();
356 do_cleanups (cleanups);
358 return EXT_LANG_RC_ERROR;
361 do_cleanups (cleanups);
362 *dm_vec = worker_vec;
364 return EXT_LANG_RC_OK;
367 /* Implementation of get_xmethod_arg_types for Python. */
370 gdbpy_get_xmethod_arg_types (const struct extension_language_defn *extlang,
371 struct xmethod_worker *worker,
372 int *nargs, struct type ***arg_types)
374 struct gdbpy_worker_data *worker_data
375 = (struct gdbpy_worker_data *) worker->data;
376 PyObject *py_worker = worker_data->worker;
377 PyObject *get_arg_types_method;
378 PyObject *py_argtype_list, *list_iter = NULL, *item;
379 struct cleanup *cleanups;
380 struct type **type_array, *obj_type;
381 int i = 1, arg_count;
383 /* Set nargs to -1 so that any premature return from this function returns
384 an invalid/unusable number of arg types. */
387 cleanups = ensure_python_env (get_current_arch (), current_language);
389 get_arg_types_method = PyObject_GetAttrString (py_worker,
390 get_arg_types_method_name);
391 if (get_arg_types_method == NULL)
393 gdbpy_print_stack ();
394 do_cleanups (cleanups);
396 return EXT_LANG_RC_ERROR;
398 make_cleanup_py_decref (get_arg_types_method);
400 py_argtype_list = PyObject_CallMethodObjArgs (py_worker,
401 py_get_arg_types_method_name,
403 if (py_argtype_list == NULL)
405 gdbpy_print_stack ();
406 do_cleanups (cleanups);
408 return EXT_LANG_RC_ERROR;
410 make_cleanup_py_decref (py_argtype_list);
411 if (py_argtype_list == Py_None)
413 else if (PySequence_Check (py_argtype_list))
415 arg_count = PySequence_Size (py_argtype_list);
418 gdbpy_print_stack ();
419 do_cleanups (cleanups);
421 return EXT_LANG_RC_ERROR;
424 list_iter = PyObject_GetIter (py_argtype_list);
425 if (list_iter == NULL)
427 gdbpy_print_stack ();
428 do_cleanups (cleanups);
430 return EXT_LANG_RC_ERROR;
432 make_cleanup_py_decref (list_iter);
437 /* Include the 'this' argument in the size. */
438 type_array = XCNEWVEC (struct type *, arg_count + 1);
440 if (list_iter != NULL)
442 while ((item = PyIter_Next (list_iter)) != NULL)
444 struct type *arg_type = type_object_to_type (item);
447 if (arg_type == NULL)
449 PyErr_SetString (PyExc_TypeError,
450 _("Arg type returned by the get_arg_types "
451 "method of a debug method worker object is "
452 "not a gdb.Type object."));
456 type_array[i] = arg_type;
460 else if (arg_count == 1)
462 /* py_argtype_list is not actually a list but a single gdb.Type
464 struct type *arg_type = type_object_to_type (py_argtype_list);
466 if (arg_type == NULL)
468 PyErr_SetString (PyExc_TypeError,
469 _("Arg type returned by the get_arg_types method "
470 "of an xmethod worker object is not a gdb.Type "
475 type_array[i] = arg_type;
479 if (PyErr_Occurred ())
481 gdbpy_print_stack ();
482 do_cleanups (cleanups);
485 return EXT_LANG_RC_ERROR;
488 /* Add the type of 'this' as the first argument. The 'this' pointer should
489 be a 'const' value. Hence, create a 'const' variant of the 'this' pointer
491 obj_type = type_object_to_type (worker_data->this_type);
492 type_array[0] = make_cv_type (1, 0, lookup_pointer_type (obj_type), NULL);
494 *arg_types = type_array;
495 do_cleanups (cleanups);
497 return EXT_LANG_RC_OK;
500 /* Implementation of get_xmethod_result_type for Python. */
503 gdbpy_get_xmethod_result_type (const struct extension_language_defn *extlang,
504 struct xmethod_worker *worker,
506 struct value **args, int nargs,
507 struct type **result_type_ptr)
509 struct gdbpy_worker_data *worker_data
510 = (struct gdbpy_worker_data *) worker->data;
511 PyObject *py_worker = worker_data->worker;
512 PyObject *py_value_obj, *py_arg_tuple, *py_result_type;
513 PyObject *get_result_type_method;
514 struct type *obj_type, *this_type;
515 struct cleanup *cleanups;
518 cleanups = ensure_python_env (get_current_arch (), current_language);
520 /* First see if there is a get_result_type method.
521 If not this could be an old xmethod (pre 7.9.1). */
522 get_result_type_method
523 = PyObject_GetAttrString (py_worker, get_result_type_method_name);
524 if (get_result_type_method == NULL)
527 do_cleanups (cleanups);
528 *result_type_ptr = NULL;
529 return EXT_LANG_RC_OK;
531 make_cleanup_py_decref (get_result_type_method);
533 obj_type = check_typedef (value_type (obj));
534 this_type = check_typedef (type_object_to_type (worker_data->this_type));
535 if (TYPE_CODE (obj_type) == TYPE_CODE_PTR)
537 struct type *this_ptr = lookup_pointer_type (this_type);
539 if (!types_equal (obj_type, this_ptr))
540 obj = value_cast (this_ptr, obj);
542 else if (TYPE_CODE (obj_type) == TYPE_CODE_REF)
544 struct type *this_ref = lookup_reference_type (this_type);
546 if (!types_equal (obj_type, this_ref))
547 obj = value_cast (this_ref, obj);
551 if (!types_equal (obj_type, this_type))
552 obj = value_cast (this_type, obj);
554 py_value_obj = value_to_value_object (obj);
555 if (py_value_obj == NULL)
557 make_cleanup_py_decref (py_value_obj);
559 py_arg_tuple = PyTuple_New (nargs + 1);
560 if (py_arg_tuple == NULL)
562 make_cleanup_py_decref (py_arg_tuple);
564 /* PyTuple_SET_ITEM steals the reference of the element. Hence INCREF the
565 reference to the 'this' object as we have a cleanup to DECREF it. */
566 Py_INCREF (py_value_obj);
567 PyTuple_SET_ITEM (py_arg_tuple, 0, py_value_obj);
569 for (i = 0; i < nargs; i++)
571 PyObject *py_value_arg = value_to_value_object (args[i]);
573 if (py_value_arg == NULL)
575 PyTuple_SET_ITEM (py_arg_tuple, i + 1, py_value_arg);
578 py_result_type = PyObject_CallObject (get_result_type_method, py_arg_tuple);
579 if (py_result_type == NULL)
581 make_cleanup_py_decref (py_result_type);
583 *result_type_ptr = type_object_to_type (py_result_type);
584 if (*result_type_ptr == NULL)
586 PyErr_SetString (PyExc_TypeError,
587 _("Type returned by the get_result_type method of an"
588 " xmethod worker object is not a gdb.Type object."));
592 do_cleanups (cleanups);
593 return EXT_LANG_RC_OK;
596 gdbpy_print_stack ();
597 do_cleanups (cleanups);
598 return EXT_LANG_RC_ERROR;
601 /* Implementation of invoke_xmethod for Python. */
604 gdbpy_invoke_xmethod (const struct extension_language_defn *extlang,
605 struct xmethod_worker *worker,
606 struct value *obj, struct value **args, int nargs)
609 struct cleanup *cleanups;
610 PyObject *py_value_obj, *py_arg_tuple, *py_result;
611 struct type *obj_type, *this_type;
612 struct value *res = NULL;
613 struct gdbpy_worker_data *worker_data
614 = (struct gdbpy_worker_data *) worker->data;
615 PyObject *xmethod_worker = worker_data->worker;
617 cleanups = ensure_python_env (get_current_arch (), current_language);
619 obj_type = check_typedef (value_type (obj));
620 this_type = check_typedef (type_object_to_type (worker_data->this_type));
621 if (TYPE_CODE (obj_type) == TYPE_CODE_PTR)
623 struct type *this_ptr = lookup_pointer_type (this_type);
625 if (!types_equal (obj_type, this_ptr))
626 obj = value_cast (this_ptr, obj);
628 else if (TYPE_CODE (obj_type) == TYPE_CODE_REF)
630 struct type *this_ref = lookup_reference_type (this_type);
632 if (!types_equal (obj_type, this_ref))
633 obj = value_cast (this_ref, obj);
637 if (!types_equal (obj_type, this_type))
638 obj = value_cast (this_type, obj);
640 py_value_obj = value_to_value_object (obj);
641 if (py_value_obj == NULL)
643 gdbpy_print_stack ();
644 error (_("Error while executing Python code."));
646 make_cleanup_py_decref (py_value_obj);
648 py_arg_tuple = PyTuple_New (nargs + 1);
649 if (py_arg_tuple == NULL)
651 gdbpy_print_stack ();
652 error (_("Error while executing Python code."));
654 make_cleanup_py_decref (py_arg_tuple);
656 /* PyTuple_SET_ITEM steals the reference of the element. Hence INCREF the
657 reference to the 'this' object as we have a cleanup to DECREF it. */
658 Py_INCREF (py_value_obj);
659 PyTuple_SET_ITEM (py_arg_tuple, 0, py_value_obj);
661 for (i = 0; i < nargs; i++)
663 PyObject *py_value_arg = value_to_value_object (args[i]);
665 if (py_value_arg == NULL)
667 gdbpy_print_stack ();
668 error (_("Error while executing Python code."));
671 PyTuple_SET_ITEM (py_arg_tuple, i + 1, py_value_arg);
674 py_result = PyObject_CallObject (xmethod_worker, py_arg_tuple);
675 if (py_result == NULL)
677 gdbpy_print_stack ();
678 error (_("Error while executing Python code."));
680 make_cleanup_py_decref (py_result);
682 if (py_result != Py_None)
684 res = convert_value_from_python (py_result);
687 gdbpy_print_stack ();
688 error (_("Error while executing Python code."));
693 res = allocate_value (lookup_typename (python_language, python_gdbarch,
697 do_cleanups (cleanups);
702 /* Creates a new Python xmethod_worker object.
703 The new object has data of type 'struct gdbpy_worker_data' composed
704 with the components PY_WORKER and THIS_TYPE. */
706 static struct xmethod_worker *
707 new_python_xmethod_worker (PyObject *py_worker, PyObject *this_type)
709 struct gdbpy_worker_data *data;
711 gdb_assert (py_worker != NULL && this_type != NULL);
713 data = XCNEW (struct gdbpy_worker_data);
714 data->worker = py_worker;
715 data->this_type = this_type;
716 Py_INCREF (py_worker);
717 Py_INCREF (this_type);
719 return new_xmethod_worker (&extension_language_python, data);
723 gdbpy_initialize_xmethods (void)
725 py_match_method_name = PyString_FromString (match_method_name);
726 if (py_match_method_name == NULL)
729 py_get_arg_types_method_name
730 = PyString_FromString (get_arg_types_method_name);
731 if (py_get_arg_types_method_name == NULL)