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"
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 python_xmethod_worker
: xmethod_worker
41 python_xmethod_worker (PyObject
*worker
, PyObject
*this_type
);
42 ~python_xmethod_worker ();
44 DISABLE_COPY_AND_ASSIGN (python_xmethod_worker
);
46 /* Implementation of xmethod_worker::invoke for Python. */
48 value
*invoke (value
*obj
, gdb::array_view
<value
*> args
) override
;
50 /* Implementation of xmethod_worker::do_get_arg_types for Python. */
52 ext_lang_rc
do_get_arg_types (std::vector
<type
*> *type_args
) override
;
54 /* Implementation of xmethod_worker::do_get_result_type for Python.
56 For backward compatibility with 7.9, which did not support getting the
57 result type, if the get_result_type operation is not provided by WORKER
58 then EXT_LANG_RC_OK is returned and NULL is returned in *RESULT_TYPE. */
60 ext_lang_rc
do_get_result_type (value
*obj
, gdb::array_view
<value
*> args
,
61 type
**result_type_ptr
) override
;
65 PyObject
*m_py_worker
;
66 PyObject
*m_this_type
;
69 python_xmethod_worker::~python_xmethod_worker ()
71 /* We don't do much here, but we still need the GIL. */
72 gdbpy_enter
enter_py (get_current_arch (), current_language
);
74 Py_DECREF (m_py_worker
);
75 Py_DECREF (m_this_type
);
78 /* Invoke the "match" method of the MATCHER and return a new reference
79 to the result. Returns NULL on error. */
82 invoke_match_method (PyObject
*matcher
, PyObject
*py_obj_type
,
83 const char *xmethod_name
)
87 gdbpy_ref
<> enabled_field (PyObject_GetAttrString (matcher
,
89 if (enabled_field
== NULL
)
92 enabled
= PyObject_IsTrue (enabled_field
.get ());
97 /* Return 'None' if the matcher is not enabled. */
101 gdbpy_ref
<> match_method (PyObject_GetAttrString (matcher
,
103 if (match_method
== NULL
)
106 gdbpy_ref
<> py_xmethod_name (PyString_FromString (xmethod_name
));
107 if (py_xmethod_name
== NULL
)
110 return PyObject_CallMethodObjArgs (matcher
, py_match_method_name
,
111 py_obj_type
, py_xmethod_name
.get (),
115 /* Implementation of get_matching_xmethod_workers for Python. */
118 gdbpy_get_matching_xmethod_workers
119 (const struct extension_language_defn
*extlang
,
120 struct type
*obj_type
, const char *method_name
,
121 std::vector
<xmethod_worker_up
> *dm_vec
)
123 gdb_assert (obj_type
!= NULL
&& method_name
!= NULL
);
125 gdbpy_enter
enter_py (get_current_arch (), current_language
);
127 gdbpy_ref
<> py_type (type_to_type_object (obj_type
));
130 gdbpy_print_stack ();
131 return EXT_LANG_RC_ERROR
;
134 /* Create an empty list of debug methods. */
135 gdbpy_ref
<> py_xmethod_matcher_list (PyList_New (0));
136 if (py_xmethod_matcher_list
== NULL
)
138 gdbpy_print_stack ();
139 return EXT_LANG_RC_ERROR
;
142 /* Gather debug method matchers registered with the object files.
143 This could be done differently by iterating over each objfile's matcher
144 list individually, but there's no data yet to show it's needed. */
145 for (objfile
*objfile
: current_program_space
->objfiles ())
147 gdbpy_ref
<> py_objfile
= objfile_to_objfile_object (objfile
);
149 if (py_objfile
== NULL
)
151 gdbpy_print_stack ();
152 return EXT_LANG_RC_ERROR
;
155 gdbpy_ref
<> objfile_matchers (objfpy_get_xmethods (py_objfile
.get (),
157 gdbpy_ref
<> temp (PySequence_Concat (py_xmethod_matcher_list
.get (),
158 objfile_matchers
.get ()));
161 gdbpy_print_stack ();
162 return EXT_LANG_RC_ERROR
;
165 py_xmethod_matcher_list
= std::move (temp
);
168 /* Gather debug methods matchers registered with the current program
170 gdbpy_ref
<> py_progspace
= pspace_to_pspace_object (current_program_space
);
171 if (py_progspace
!= NULL
)
173 gdbpy_ref
<> pspace_matchers (pspy_get_xmethods (py_progspace
.get (),
176 gdbpy_ref
<> temp (PySequence_Concat (py_xmethod_matcher_list
.get (),
177 pspace_matchers
.get ()));
180 gdbpy_print_stack ();
181 return EXT_LANG_RC_ERROR
;
184 py_xmethod_matcher_list
= std::move (temp
);
188 gdbpy_print_stack ();
189 return EXT_LANG_RC_ERROR
;
192 /* Gather debug method matchers registered globally. */
193 if (gdb_python_module
!= NULL
194 && PyObject_HasAttrString (gdb_python_module
, matchers_attr_str
))
196 gdbpy_ref
<> gdb_matchers (PyObject_GetAttrString (gdb_python_module
,
198 if (gdb_matchers
!= NULL
)
200 gdbpy_ref
<> temp (PySequence_Concat (py_xmethod_matcher_list
.get (),
201 gdb_matchers
.get ()));
204 gdbpy_print_stack ();
205 return EXT_LANG_RC_ERROR
;
208 py_xmethod_matcher_list
= std::move (temp
);
212 gdbpy_print_stack ();
213 return EXT_LANG_RC_ERROR
;
217 gdbpy_ref
<> list_iter (PyObject_GetIter (py_xmethod_matcher_list
.get ()));
218 if (list_iter
== NULL
)
220 gdbpy_print_stack ();
221 return EXT_LANG_RC_ERROR
;
225 gdbpy_ref
<> matcher (PyIter_Next (list_iter
.get ()));
228 if (PyErr_Occurred ())
230 gdbpy_print_stack ();
231 return EXT_LANG_RC_ERROR
;
236 gdbpy_ref
<> match_result (invoke_match_method (matcher
.get (),
240 if (match_result
== NULL
)
242 gdbpy_print_stack ();
243 return EXT_LANG_RC_ERROR
;
245 if (match_result
== Py_None
)
246 ; /* This means there was no match. */
247 else if (PySequence_Check (match_result
.get ()))
249 gdbpy_ref
<> iter (PyObject_GetIter (match_result
.get ()));
253 gdbpy_print_stack ();
254 return EXT_LANG_RC_ERROR
;
258 struct xmethod_worker
*worker
;
260 gdbpy_ref
<> py_worker (PyIter_Next (iter
.get ()));
261 if (py_worker
== NULL
)
263 if (PyErr_Occurred ())
265 gdbpy_print_stack ();
266 return EXT_LANG_RC_ERROR
;
271 worker
= new python_xmethod_worker (py_worker
.get (),
274 dm_vec
->emplace_back (worker
);
279 struct xmethod_worker
*worker
;
281 worker
= new python_xmethod_worker (match_result
.get (),
283 dm_vec
->emplace_back (worker
);
287 return EXT_LANG_RC_OK
;
290 /* See declaration. */
293 python_xmethod_worker::do_get_arg_types (std::vector
<type
*> *arg_types
)
295 /* The gdbpy_enter object needs to be placed first, so that it's the last to
297 gdbpy_enter
enter_py (get_current_arch (), current_language
);
298 struct type
*obj_type
;
299 int i
= 1, arg_count
;
300 gdbpy_ref
<> list_iter
;
302 gdbpy_ref
<> get_arg_types_method
303 (PyObject_GetAttrString (m_py_worker
, get_arg_types_method_name
));
304 if (get_arg_types_method
== NULL
)
306 gdbpy_print_stack ();
307 return EXT_LANG_RC_ERROR
;
310 gdbpy_ref
<> py_argtype_list
311 (PyObject_CallMethodObjArgs (m_py_worker
, py_get_arg_types_method_name
,
313 if (py_argtype_list
== NULL
)
315 gdbpy_print_stack ();
316 return EXT_LANG_RC_ERROR
;
319 if (py_argtype_list
== Py_None
)
321 else if (PySequence_Check (py_argtype_list
.get ()))
323 arg_count
= PySequence_Size (py_argtype_list
.get ());
326 gdbpy_print_stack ();
327 return EXT_LANG_RC_ERROR
;
330 list_iter
.reset (PyObject_GetIter (py_argtype_list
.get ()));
331 if (list_iter
== NULL
)
333 gdbpy_print_stack ();
334 return EXT_LANG_RC_ERROR
;
340 /* Include the 'this' argument in the size. */
341 arg_types
->resize (arg_count
+ 1);
343 if (list_iter
!= NULL
)
347 gdbpy_ref
<> item (PyIter_Next (list_iter
.get ()));
350 if (PyErr_Occurred ())
352 gdbpy_print_stack ();
353 return EXT_LANG_RC_ERROR
;
358 struct type
*arg_type
= type_object_to_type (item
.get ());
359 if (arg_type
== NULL
)
361 PyErr_SetString (PyExc_TypeError
,
362 _("Arg type returned by the get_arg_types "
363 "method of a debug method worker object is "
364 "not a gdb.Type object."));
365 return EXT_LANG_RC_ERROR
;
368 (*arg_types
)[i
] = arg_type
;
372 else if (arg_count
== 1)
374 /* py_argtype_list is not actually a list but a single gdb.Type
376 struct type
*arg_type
= type_object_to_type (py_argtype_list
.get ());
378 if (arg_type
== NULL
)
380 PyErr_SetString (PyExc_TypeError
,
381 _("Arg type returned by the get_arg_types method "
382 "of an xmethod worker object is not a gdb.Type "
384 return EXT_LANG_RC_ERROR
;
388 (*arg_types
)[i
] = arg_type
;
393 /* Add the type of 'this' as the first argument. The 'this' pointer should
394 be a 'const' value. Hence, create a 'const' variant of the 'this' pointer
396 obj_type
= type_object_to_type (m_this_type
);
397 (*arg_types
)[0] = make_cv_type (1, 0, lookup_pointer_type (obj_type
),
400 return EXT_LANG_RC_OK
;
403 /* See declaration. */
406 python_xmethod_worker::do_get_result_type (value
*obj
,
407 gdb::array_view
<value
*> args
,
408 type
**result_type_ptr
)
410 struct type
*obj_type
, *this_type
;
413 gdbpy_enter
enter_py (get_current_arch (), current_language
);
415 /* First see if there is a get_result_type method.
416 If not this could be an old xmethod (pre 7.9.1). */
417 gdbpy_ref
<> get_result_type_method
418 (PyObject_GetAttrString (m_py_worker
, get_result_type_method_name
));
419 if (get_result_type_method
== NULL
)
422 *result_type_ptr
= NULL
;
423 return EXT_LANG_RC_OK
;
426 obj_type
= check_typedef (value_type (obj
));
427 this_type
= check_typedef (type_object_to_type (m_this_type
));
428 if (TYPE_CODE (obj_type
) == TYPE_CODE_PTR
)
430 struct type
*this_ptr
= lookup_pointer_type (this_type
);
432 if (!types_equal (obj_type
, this_ptr
))
433 obj
= value_cast (this_ptr
, obj
);
435 else if (TYPE_IS_REFERENCE (obj_type
))
437 struct type
*this_ref
438 = lookup_reference_type (this_type
, TYPE_CODE (obj_type
));
440 if (!types_equal (obj_type
, this_ref
))
441 obj
= value_cast (this_ref
, obj
);
445 if (!types_equal (obj_type
, this_type
))
446 obj
= value_cast (this_type
, obj
);
448 gdbpy_ref
<> py_value_obj (value_to_value_object (obj
));
449 if (py_value_obj
== NULL
)
451 gdbpy_print_stack ();
452 return EXT_LANG_RC_ERROR
;
455 gdbpy_ref
<> py_arg_tuple (PyTuple_New (args
.size () + 1));
456 if (py_arg_tuple
== NULL
)
458 gdbpy_print_stack ();
459 return EXT_LANG_RC_ERROR
;
462 /* PyTuple_SET_ITEM steals the reference of the element, hence the
464 PyTuple_SET_ITEM (py_arg_tuple
.get (), 0, py_value_obj
.release ());
466 for (i
= 0; i
< args
.size (); i
++)
468 PyObject
*py_value_arg
= value_to_value_object (args
[i
]);
470 if (py_value_arg
== NULL
)
472 gdbpy_print_stack ();
473 return EXT_LANG_RC_ERROR
;
475 PyTuple_SET_ITEM (py_arg_tuple
.get (), i
+ 1, py_value_arg
);
478 gdbpy_ref
<> py_result_type
479 (PyObject_CallObject (get_result_type_method
.get (), py_arg_tuple
.get ()));
480 if (py_result_type
== NULL
)
482 gdbpy_print_stack ();
483 return EXT_LANG_RC_ERROR
;
486 *result_type_ptr
= type_object_to_type (py_result_type
.get ());
487 if (*result_type_ptr
== NULL
)
489 PyErr_SetString (PyExc_TypeError
,
490 _("Type returned by the get_result_type method of an"
491 " xmethod worker object is not a gdb.Type object."));
492 gdbpy_print_stack ();
493 return EXT_LANG_RC_ERROR
;
496 return EXT_LANG_RC_OK
;
499 /* See declaration. */
502 python_xmethod_worker::invoke (struct value
*obj
,
503 gdb::array_view
<value
*> args
)
505 gdbpy_enter
enter_py (get_current_arch (), current_language
);
508 struct type
*obj_type
, *this_type
;
509 struct value
*res
= NULL
;
511 obj_type
= check_typedef (value_type (obj
));
512 this_type
= check_typedef (type_object_to_type (m_this_type
));
513 if (TYPE_CODE (obj_type
) == TYPE_CODE_PTR
)
515 struct type
*this_ptr
= lookup_pointer_type (this_type
);
517 if (!types_equal (obj_type
, this_ptr
))
518 obj
= value_cast (this_ptr
, obj
);
520 else if (TYPE_IS_REFERENCE (obj_type
))
522 struct type
*this_ref
523 = lookup_reference_type (this_type
, TYPE_CODE (obj_type
));
525 if (!types_equal (obj_type
, this_ref
))
526 obj
= value_cast (this_ref
, obj
);
530 if (!types_equal (obj_type
, this_type
))
531 obj
= value_cast (this_type
, obj
);
533 gdbpy_ref
<> py_value_obj (value_to_value_object (obj
));
534 if (py_value_obj
== NULL
)
536 gdbpy_print_stack ();
537 error (_("Error while executing Python code."));
540 gdbpy_ref
<> py_arg_tuple (PyTuple_New (args
.size () + 1));
541 if (py_arg_tuple
== NULL
)
543 gdbpy_print_stack ();
544 error (_("Error while executing Python code."));
547 /* PyTuple_SET_ITEM steals the reference of the element, hence the
549 PyTuple_SET_ITEM (py_arg_tuple
.get (), 0, py_value_obj
.release ());
551 for (i
= 0; i
< args
.size (); i
++)
553 PyObject
*py_value_arg
= value_to_value_object (args
[i
]);
555 if (py_value_arg
== NULL
)
557 gdbpy_print_stack ();
558 error (_("Error while executing Python code."));
561 PyTuple_SET_ITEM (py_arg_tuple
.get (), i
+ 1, py_value_arg
);
564 gdbpy_ref
<> py_result (PyObject_CallObject (m_py_worker
,
565 py_arg_tuple
.get ()));
566 if (py_result
== NULL
)
568 gdbpy_print_stack ();
569 error (_("Error while executing Python code."));
572 if (py_result
!= Py_None
)
574 res
= convert_value_from_python (py_result
.get ());
577 gdbpy_print_stack ();
578 error (_("Error while executing Python code."));
583 res
= allocate_value (lookup_typename (python_language
,
590 python_xmethod_worker::python_xmethod_worker (PyObject
*py_worker
,
592 : xmethod_worker (&extension_language_python
),
593 m_py_worker (py_worker
), m_this_type (this_type
)
595 gdb_assert (m_py_worker
!= NULL
&& m_this_type
!= NULL
);
597 Py_INCREF (py_worker
);
598 Py_INCREF (this_type
);
602 gdbpy_initialize_xmethods (void)
604 py_match_method_name
= PyString_FromString (match_method_name
);
605 if (py_match_method_name
== NULL
)
608 py_get_arg_types_method_name
609 = PyString_FromString (get_arg_types_method_name
);
610 if (py_get_arg_types_method_name
== NULL
)