1 /* Support for debug methods in Python.
3 Copyright (C) 2013-2019 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 python_xmethod_worker : xmethod_worker
42 python_xmethod_worker (PyObject *worker, PyObject *this_type);
43 ~python_xmethod_worker ();
45 DISABLE_COPY_AND_ASSIGN (python_xmethod_worker);
47 /* Implementation of xmethod_worker::invoke for Python. */
49 value *invoke (value *obj, gdb::array_view<value *> args) override;
51 /* Implementation of xmethod_worker::do_get_arg_types for Python. */
53 ext_lang_rc do_get_arg_types (std::vector<type *> *type_args) override;
55 /* Implementation of xmethod_worker::do_get_result_type for Python.
57 For backward compatibility with 7.9, which did not support getting the
58 result type, if the get_result_type operation is not provided by WORKER
59 then EXT_LANG_RC_OK is returned and NULL is returned in *RESULT_TYPE. */
61 ext_lang_rc do_get_result_type (value *obj, gdb::array_view<value *> args,
62 type **result_type_ptr) override;
66 PyObject *m_py_worker;
67 PyObject *m_this_type;
70 python_xmethod_worker::~python_xmethod_worker ()
72 /* We don't do much here, but we still need the GIL. */
73 gdbpy_enter enter_py (get_current_arch (), current_language);
75 Py_DECREF (m_py_worker);
76 Py_DECREF (m_this_type);
79 /* Invoke the "match" method of the MATCHER and return a new reference
80 to the result. Returns NULL on error. */
83 invoke_match_method (PyObject *matcher, PyObject *py_obj_type,
84 const char *xmethod_name)
88 gdbpy_ref<> enabled_field (PyObject_GetAttrString (matcher,
90 if (enabled_field == NULL)
93 enabled = PyObject_IsTrue (enabled_field.get ());
98 /* Return 'None' if the matcher is not enabled. */
102 gdbpy_ref<> match_method (PyObject_GetAttrString (matcher,
104 if (match_method == NULL)
107 gdbpy_ref<> py_xmethod_name (PyString_FromString (xmethod_name));
108 if (py_xmethod_name == NULL)
111 return PyObject_CallMethodObjArgs (matcher, py_match_method_name,
112 py_obj_type, py_xmethod_name.get (),
116 /* Implementation of get_matching_xmethod_workers for Python. */
119 gdbpy_get_matching_xmethod_workers
120 (const struct extension_language_defn *extlang,
121 struct type *obj_type, const char *method_name,
122 std::vector<xmethod_worker_up> *dm_vec)
124 struct objfile *objfile;
126 gdb_assert (obj_type != NULL && method_name != NULL);
128 gdbpy_enter enter_py (get_current_arch (), current_language);
130 gdbpy_ref<> py_type (type_to_type_object (obj_type));
133 gdbpy_print_stack ();
134 return EXT_LANG_RC_ERROR;
137 /* Create an empty list of debug methods. */
138 gdbpy_ref<> py_xmethod_matcher_list (PyList_New (0));
139 if (py_xmethod_matcher_list == NULL)
141 gdbpy_print_stack ();
142 return EXT_LANG_RC_ERROR;
145 /* Gather debug method matchers registered with the object files.
146 This could be done differently by iterating over each objfile's matcher
147 list individually, but there's no data yet to show it's needed. */
148 ALL_OBJFILES (objfile)
150 gdbpy_ref<> py_objfile = objfile_to_objfile_object (objfile);
152 if (py_objfile == NULL)
154 gdbpy_print_stack ();
155 return EXT_LANG_RC_ERROR;
158 gdbpy_ref<> objfile_matchers (objfpy_get_xmethods (py_objfile.get (),
160 gdbpy_ref<> temp (PySequence_Concat (py_xmethod_matcher_list.get (),
161 objfile_matchers.get ()));
164 gdbpy_print_stack ();
165 return EXT_LANG_RC_ERROR;
168 py_xmethod_matcher_list = std::move (temp);
171 /* Gather debug methods matchers registered with the current program
173 gdbpy_ref<> py_progspace = pspace_to_pspace_object (current_program_space);
174 if (py_progspace != NULL)
176 gdbpy_ref<> pspace_matchers (pspy_get_xmethods (py_progspace.get (),
179 gdbpy_ref<> temp (PySequence_Concat (py_xmethod_matcher_list.get (),
180 pspace_matchers.get ()));
183 gdbpy_print_stack ();
184 return EXT_LANG_RC_ERROR;
187 py_xmethod_matcher_list = std::move (temp);
191 gdbpy_print_stack ();
192 return EXT_LANG_RC_ERROR;
195 /* Gather debug method matchers registered globally. */
196 if (gdb_python_module != NULL
197 && PyObject_HasAttrString (gdb_python_module, matchers_attr_str))
199 gdbpy_ref<> gdb_matchers (PyObject_GetAttrString (gdb_python_module,
201 if (gdb_matchers != NULL)
203 gdbpy_ref<> temp (PySequence_Concat (py_xmethod_matcher_list.get (),
204 gdb_matchers.get ()));
207 gdbpy_print_stack ();
208 return EXT_LANG_RC_ERROR;
211 py_xmethod_matcher_list = std::move (temp);
215 gdbpy_print_stack ();
216 return EXT_LANG_RC_ERROR;
220 gdbpy_ref<> list_iter (PyObject_GetIter (py_xmethod_matcher_list.get ()));
221 if (list_iter == NULL)
223 gdbpy_print_stack ();
224 return EXT_LANG_RC_ERROR;
228 gdbpy_ref<> matcher (PyIter_Next (list_iter.get ()));
231 if (PyErr_Occurred ())
233 gdbpy_print_stack ();
234 return EXT_LANG_RC_ERROR;
239 gdbpy_ref<> match_result (invoke_match_method (matcher.get (),
243 if (match_result == NULL)
245 gdbpy_print_stack ();
246 return EXT_LANG_RC_ERROR;
248 if (match_result == Py_None)
249 ; /* This means there was no match. */
250 else if (PySequence_Check (match_result.get ()))
252 gdbpy_ref<> iter (PyObject_GetIter (match_result.get ()));
256 gdbpy_print_stack ();
257 return EXT_LANG_RC_ERROR;
261 struct xmethod_worker *worker;
263 gdbpy_ref<> py_worker (PyIter_Next (iter.get ()));
264 if (py_worker == NULL)
266 if (PyErr_Occurred ())
268 gdbpy_print_stack ();
269 return EXT_LANG_RC_ERROR;
274 worker = new python_xmethod_worker (py_worker.get (),
277 dm_vec->emplace_back (worker);
282 struct xmethod_worker *worker;
284 worker = new python_xmethod_worker (match_result.get (),
286 dm_vec->emplace_back (worker);
290 return EXT_LANG_RC_OK;
293 /* See declaration. */
296 python_xmethod_worker::do_get_arg_types (std::vector<type *> *arg_types)
298 /* The gdbpy_enter object needs to be placed first, so that it's the last to
300 gdbpy_enter enter_py (get_current_arch (), current_language);
301 struct type *obj_type;
302 int i = 1, arg_count;
303 gdbpy_ref<> list_iter;
305 gdbpy_ref<> get_arg_types_method
306 (PyObject_GetAttrString (m_py_worker, get_arg_types_method_name));
307 if (get_arg_types_method == NULL)
309 gdbpy_print_stack ();
310 return EXT_LANG_RC_ERROR;
313 gdbpy_ref<> py_argtype_list
314 (PyObject_CallMethodObjArgs (m_py_worker, py_get_arg_types_method_name,
316 if (py_argtype_list == NULL)
318 gdbpy_print_stack ();
319 return EXT_LANG_RC_ERROR;
322 if (py_argtype_list == Py_None)
324 else if (PySequence_Check (py_argtype_list.get ()))
326 arg_count = PySequence_Size (py_argtype_list.get ());
329 gdbpy_print_stack ();
330 return EXT_LANG_RC_ERROR;
333 list_iter.reset (PyObject_GetIter (py_argtype_list.get ()));
334 if (list_iter == NULL)
336 gdbpy_print_stack ();
337 return EXT_LANG_RC_ERROR;
343 /* Include the 'this' argument in the size. */
344 arg_types->resize (arg_count + 1);
346 if (list_iter != NULL)
350 gdbpy_ref<> item (PyIter_Next (list_iter.get ()));
353 if (PyErr_Occurred ())
355 gdbpy_print_stack ();
356 return EXT_LANG_RC_ERROR;
361 struct type *arg_type = type_object_to_type (item.get ());
362 if (arg_type == NULL)
364 PyErr_SetString (PyExc_TypeError,
365 _("Arg type returned by the get_arg_types "
366 "method of a debug method worker object is "
367 "not a gdb.Type object."));
368 return EXT_LANG_RC_ERROR;
371 (*arg_types)[i] = arg_type;
375 else if (arg_count == 1)
377 /* py_argtype_list is not actually a list but a single gdb.Type
379 struct type *arg_type = type_object_to_type (py_argtype_list.get ());
381 if (arg_type == NULL)
383 PyErr_SetString (PyExc_TypeError,
384 _("Arg type returned by the get_arg_types method "
385 "of an xmethod worker object is not a gdb.Type "
387 return EXT_LANG_RC_ERROR;
391 (*arg_types)[i] = arg_type;
396 /* Add the type of 'this' as the first argument. The 'this' pointer should
397 be a 'const' value. Hence, create a 'const' variant of the 'this' pointer
399 obj_type = type_object_to_type (m_this_type);
400 (*arg_types)[0] = make_cv_type (1, 0, lookup_pointer_type (obj_type),
403 return EXT_LANG_RC_OK;
406 /* See declaration. */
409 python_xmethod_worker::do_get_result_type (value *obj,
410 gdb::array_view<value *> args,
411 type **result_type_ptr)
413 struct type *obj_type, *this_type;
416 gdbpy_enter enter_py (get_current_arch (), current_language);
418 /* First see if there is a get_result_type method.
419 If not this could be an old xmethod (pre 7.9.1). */
420 gdbpy_ref<> get_result_type_method
421 (PyObject_GetAttrString (m_py_worker, get_result_type_method_name));
422 if (get_result_type_method == NULL)
425 *result_type_ptr = NULL;
426 return EXT_LANG_RC_OK;
429 obj_type = check_typedef (value_type (obj));
430 this_type = check_typedef (type_object_to_type (m_this_type));
431 if (TYPE_CODE (obj_type) == TYPE_CODE_PTR)
433 struct type *this_ptr = lookup_pointer_type (this_type);
435 if (!types_equal (obj_type, this_ptr))
436 obj = value_cast (this_ptr, obj);
438 else if (TYPE_IS_REFERENCE (obj_type))
440 struct type *this_ref
441 = lookup_reference_type (this_type, TYPE_CODE (obj_type));
443 if (!types_equal (obj_type, this_ref))
444 obj = value_cast (this_ref, obj);
448 if (!types_equal (obj_type, this_type))
449 obj = value_cast (this_type, obj);
451 gdbpy_ref<> py_value_obj (value_to_value_object (obj));
452 if (py_value_obj == NULL)
454 gdbpy_print_stack ();
455 return EXT_LANG_RC_ERROR;
458 gdbpy_ref<> py_arg_tuple (PyTuple_New (args.size () + 1));
459 if (py_arg_tuple == NULL)
461 gdbpy_print_stack ();
462 return EXT_LANG_RC_ERROR;
465 /* PyTuple_SET_ITEM steals the reference of the element, hence the
467 PyTuple_SET_ITEM (py_arg_tuple.get (), 0, py_value_obj.release ());
469 for (i = 0; i < args.size (); i++)
471 PyObject *py_value_arg = value_to_value_object (args[i]);
473 if (py_value_arg == NULL)
475 gdbpy_print_stack ();
476 return EXT_LANG_RC_ERROR;
478 PyTuple_SET_ITEM (py_arg_tuple.get (), i + 1, py_value_arg);
481 gdbpy_ref<> py_result_type
482 (PyObject_CallObject (get_result_type_method.get (), py_arg_tuple.get ()));
483 if (py_result_type == NULL)
485 gdbpy_print_stack ();
486 return EXT_LANG_RC_ERROR;
489 *result_type_ptr = type_object_to_type (py_result_type.get ());
490 if (*result_type_ptr == NULL)
492 PyErr_SetString (PyExc_TypeError,
493 _("Type returned by the get_result_type method of an"
494 " xmethod worker object is not a gdb.Type object."));
495 gdbpy_print_stack ();
496 return EXT_LANG_RC_ERROR;
499 return EXT_LANG_RC_OK;
502 /* See declaration. */
505 python_xmethod_worker::invoke (struct value *obj,
506 gdb::array_view<value *> args)
508 gdbpy_enter enter_py (get_current_arch (), current_language);
511 struct type *obj_type, *this_type;
512 struct value *res = NULL;
514 obj_type = check_typedef (value_type (obj));
515 this_type = check_typedef (type_object_to_type (m_this_type));
516 if (TYPE_CODE (obj_type) == TYPE_CODE_PTR)
518 struct type *this_ptr = lookup_pointer_type (this_type);
520 if (!types_equal (obj_type, this_ptr))
521 obj = value_cast (this_ptr, obj);
523 else if (TYPE_IS_REFERENCE (obj_type))
525 struct type *this_ref
526 = lookup_reference_type (this_type, TYPE_CODE (obj_type));
528 if (!types_equal (obj_type, this_ref))
529 obj = value_cast (this_ref, obj);
533 if (!types_equal (obj_type, this_type))
534 obj = value_cast (this_type, obj);
536 gdbpy_ref<> py_value_obj (value_to_value_object (obj));
537 if (py_value_obj == NULL)
539 gdbpy_print_stack ();
540 error (_("Error while executing Python code."));
543 gdbpy_ref<> py_arg_tuple (PyTuple_New (args.size () + 1));
544 if (py_arg_tuple == NULL)
546 gdbpy_print_stack ();
547 error (_("Error while executing Python code."));
550 /* PyTuple_SET_ITEM steals the reference of the element, hence the
552 PyTuple_SET_ITEM (py_arg_tuple.get (), 0, py_value_obj.release ());
554 for (i = 0; i < args.size (); i++)
556 PyObject *py_value_arg = value_to_value_object (args[i]);
558 if (py_value_arg == NULL)
560 gdbpy_print_stack ();
561 error (_("Error while executing Python code."));
564 PyTuple_SET_ITEM (py_arg_tuple.get (), i + 1, py_value_arg);
567 gdbpy_ref<> py_result (PyObject_CallObject (m_py_worker,
568 py_arg_tuple.get ()));
569 if (py_result == NULL)
571 gdbpy_print_stack ();
572 error (_("Error while executing Python code."));
575 if (py_result != Py_None)
577 res = convert_value_from_python (py_result.get ());
580 gdbpy_print_stack ();
581 error (_("Error while executing Python code."));
586 res = allocate_value (lookup_typename (python_language, python_gdbarch,
593 python_xmethod_worker::python_xmethod_worker (PyObject *py_worker,
595 : xmethod_worker (&extension_language_python),
596 m_py_worker (py_worker), m_this_type (this_type)
598 gdb_assert (m_py_worker != NULL && m_this_type != NULL);
600 Py_INCREF (py_worker);
601 Py_INCREF (this_type);
605 gdbpy_initialize_xmethods (void)
607 py_match_method_name = PyString_FromString (match_method_name);
608 if (py_match_method_name == NULL)
611 py_get_arg_types_method_name
612 = PyString_FromString (get_arg_types_method_name);
613 if (py_get_arg_types_method_name == NULL)