1 /* Support for debug methods in Python.
3 Copyright (C) 2013-2018 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
, value
**args
, int nargs
) override
;
51 /* Implementation of xmethod_worker::clone for Python. */
53 xmethod_worker_up
clone () override
;
55 /* Implementation of xmethod_worker::do_get_arg_types for Python. */
57 ext_lang_rc
do_get_arg_types (int *nargs
, type
***arg_types
) override
;
59 /* Implementation of xmethod_worker::do_get_result_type for Python.
61 For backward compatibility with 7.9, which did not support getting the
62 result type, if the get_result_type operation is not provided by WORKER
63 then EXT_LANG_RC_OK is returned and NULL is returned in *RESULT_TYPE. */
65 ext_lang_rc
do_get_result_type (value
*obj
, value
**args
, int nargs
,
66 type
**result_type_ptr
) override
;
70 PyObject
*m_py_worker
;
71 PyObject
*m_this_type
;
74 python_xmethod_worker::~python_xmethod_worker ()
76 /* We don't do much here, but we still need the GIL. */
77 gdbpy_enter
enter_py (get_current_arch (), current_language
);
79 Py_DECREF (m_py_worker
);
80 Py_DECREF (m_this_type
);
83 /* See declaration. */
86 python_xmethod_worker::clone ()
88 /* We don't do much here, but we still need the GIL. */
89 gdbpy_enter
enter_py (get_current_arch (), current_language
);
91 xmethod_worker
*worker
= new python_xmethod_worker (m_py_worker
, m_this_type
);
93 return xmethod_worker_up (worker
);
96 /* Invoke the "match" method of the MATCHER and return a new reference
97 to the result. Returns NULL on error. */
100 invoke_match_method (PyObject
*matcher
, PyObject
*py_obj_type
,
101 const char *xmethod_name
)
105 gdbpy_ref
<> enabled_field (PyObject_GetAttrString (matcher
,
106 enabled_field_name
));
107 if (enabled_field
== NULL
)
110 enabled
= PyObject_IsTrue (enabled_field
.get ());
115 /* Return 'None' if the matcher is not enabled. */
119 gdbpy_ref
<> match_method (PyObject_GetAttrString (matcher
,
121 if (match_method
== NULL
)
124 gdbpy_ref
<> py_xmethod_name (PyString_FromString (xmethod_name
));
125 if (py_xmethod_name
== NULL
)
128 return PyObject_CallMethodObjArgs (matcher
, py_match_method_name
,
129 py_obj_type
, py_xmethod_name
.get (),
133 /* Implementation of get_matching_xmethod_workers for Python. */
136 gdbpy_get_matching_xmethod_workers
137 (const struct extension_language_defn
*extlang
,
138 struct type
*obj_type
, const char *method_name
,
139 std::vector
<xmethod_worker_up
> *dm_vec
)
141 struct objfile
*objfile
;
142 PyObject
*py_progspace
;
144 gdb_assert (obj_type
!= NULL
&& method_name
!= NULL
);
146 gdbpy_enter
enter_py (get_current_arch (), current_language
);
148 gdbpy_ref
<> py_type (type_to_type_object (obj_type
));
151 gdbpy_print_stack ();
152 return EXT_LANG_RC_ERROR
;
155 /* Create an empty list of debug methods. */
156 gdbpy_ref
<> py_xmethod_matcher_list (PyList_New (0));
157 if (py_xmethod_matcher_list
== NULL
)
159 gdbpy_print_stack ();
160 return EXT_LANG_RC_ERROR
;
163 /* Gather debug method matchers registered with the object files.
164 This could be done differently by iterating over each objfile's matcher
165 list individually, but there's no data yet to show it's needed. */
166 ALL_OBJFILES (objfile
)
168 PyObject
*py_objfile
= objfile_to_objfile_object (objfile
);
170 if (py_objfile
== NULL
)
172 gdbpy_print_stack ();
173 return EXT_LANG_RC_ERROR
;
176 gdbpy_ref
<> objfile_matchers (objfpy_get_xmethods (py_objfile
, NULL
));
177 gdbpy_ref
<> temp (PySequence_Concat (py_xmethod_matcher_list
.get (),
178 objfile_matchers
.get ()));
181 gdbpy_print_stack ();
182 return EXT_LANG_RC_ERROR
;
185 py_xmethod_matcher_list
= std::move (temp
);
188 /* Gather debug methods matchers registered with the current program
190 py_progspace
= pspace_to_pspace_object (current_program_space
);
191 if (py_progspace
!= NULL
)
193 gdbpy_ref
<> pspace_matchers (pspy_get_xmethods (py_progspace
, NULL
));
195 gdbpy_ref
<> temp (PySequence_Concat (py_xmethod_matcher_list
.get (),
196 pspace_matchers
.get ()));
199 gdbpy_print_stack ();
200 return EXT_LANG_RC_ERROR
;
203 py_xmethod_matcher_list
= std::move (temp
);
207 gdbpy_print_stack ();
208 return EXT_LANG_RC_ERROR
;
211 /* Gather debug method matchers registered globally. */
212 if (gdb_python_module
!= NULL
213 && PyObject_HasAttrString (gdb_python_module
, matchers_attr_str
))
215 gdbpy_ref
<> gdb_matchers (PyObject_GetAttrString (gdb_python_module
,
217 if (gdb_matchers
!= NULL
)
219 gdbpy_ref
<> temp (PySequence_Concat (py_xmethod_matcher_list
.get (),
220 gdb_matchers
.get ()));
223 gdbpy_print_stack ();
224 return EXT_LANG_RC_ERROR
;
227 py_xmethod_matcher_list
= std::move (temp
);
231 gdbpy_print_stack ();
232 return EXT_LANG_RC_ERROR
;
236 gdbpy_ref
<> list_iter (PyObject_GetIter (py_xmethod_matcher_list
.get ()));
237 if (list_iter
== NULL
)
239 gdbpy_print_stack ();
240 return EXT_LANG_RC_ERROR
;
244 gdbpy_ref
<> matcher (PyIter_Next (list_iter
.get ()));
247 if (PyErr_Occurred ())
249 gdbpy_print_stack ();
250 return EXT_LANG_RC_ERROR
;
255 gdbpy_ref
<> match_result (invoke_match_method (matcher
.get (),
259 if (match_result
== NULL
)
261 gdbpy_print_stack ();
262 return EXT_LANG_RC_ERROR
;
264 if (match_result
== Py_None
)
265 ; /* This means there was no match. */
266 else if (PySequence_Check (match_result
.get ()))
268 gdbpy_ref
<> iter (PyObject_GetIter (match_result
.get ()));
272 gdbpy_print_stack ();
273 return EXT_LANG_RC_ERROR
;
277 struct xmethod_worker
*worker
;
279 gdbpy_ref
<> py_worker (PyIter_Next (iter
.get ()));
280 if (py_worker
== NULL
)
282 if (PyErr_Occurred ())
284 gdbpy_print_stack ();
285 return EXT_LANG_RC_ERROR
;
290 worker
= new python_xmethod_worker (py_worker
.get (),
293 dm_vec
->emplace_back (worker
);
298 struct xmethod_worker
*worker
;
300 worker
= new python_xmethod_worker (match_result
.get (),
302 dm_vec
->emplace_back (worker
);
306 return EXT_LANG_RC_OK
;
309 /* See declaration. */
312 python_xmethod_worker::do_get_arg_types (int *nargs
, type
***arg_types
)
314 /* The gdbpy_enter object needs to be placed first, so that it's the last to
316 gdbpy_enter
enter_py (get_current_arch (), current_language
);
317 struct type
*obj_type
;
318 int i
= 1, arg_count
;
319 gdbpy_ref
<> list_iter
;
321 /* Set nargs to -1 so that any premature return from this function returns
322 an invalid/unusable number of arg types. */
325 gdbpy_ref
<> get_arg_types_method
326 (PyObject_GetAttrString (m_py_worker
, get_arg_types_method_name
));
327 if (get_arg_types_method
== NULL
)
329 gdbpy_print_stack ();
330 return EXT_LANG_RC_ERROR
;
333 gdbpy_ref
<> py_argtype_list
334 (PyObject_CallMethodObjArgs (m_py_worker
, py_get_arg_types_method_name
,
336 if (py_argtype_list
== NULL
)
338 gdbpy_print_stack ();
339 return EXT_LANG_RC_ERROR
;
342 if (py_argtype_list
== Py_None
)
344 else if (PySequence_Check (py_argtype_list
.get ()))
346 arg_count
= PySequence_Size (py_argtype_list
.get ());
349 gdbpy_print_stack ();
350 return EXT_LANG_RC_ERROR
;
353 list_iter
.reset (PyObject_GetIter (py_argtype_list
.get ()));
354 if (list_iter
== NULL
)
356 gdbpy_print_stack ();
357 return EXT_LANG_RC_ERROR
;
363 /* Include the 'this' argument in the size. */
364 gdb::unique_xmalloc_ptr
<struct type
*> type_array
365 (XCNEWVEC (struct type
*, arg_count
+ 1));
367 if (list_iter
!= NULL
)
371 gdbpy_ref
<> item (PyIter_Next (list_iter
.get ()));
374 if (PyErr_Occurred ())
376 gdbpy_print_stack ();
377 return EXT_LANG_RC_ERROR
;
382 struct type
*arg_type
= type_object_to_type (item
.get ());
383 if (arg_type
== NULL
)
385 PyErr_SetString (PyExc_TypeError
,
386 _("Arg type returned by the get_arg_types "
387 "method of a debug method worker object is "
388 "not a gdb.Type object."));
389 return EXT_LANG_RC_ERROR
;
392 (type_array
.get ())[i
] = arg_type
;
396 else if (arg_count
== 1)
398 /* py_argtype_list is not actually a list but a single gdb.Type
400 struct type
*arg_type
= type_object_to_type (py_argtype_list
.get ());
402 if (arg_type
== NULL
)
404 PyErr_SetString (PyExc_TypeError
,
405 _("Arg type returned by the get_arg_types method "
406 "of an xmethod worker object is not a gdb.Type "
408 return EXT_LANG_RC_ERROR
;
412 (type_array
.get ())[i
] = arg_type
;
417 /* Add the type of 'this' as the first argument. The 'this' pointer should
418 be a 'const' value. Hence, create a 'const' variant of the 'this' pointer
420 obj_type
= type_object_to_type (m_this_type
);
421 (type_array
.get ())[0] = make_cv_type (1, 0, lookup_pointer_type (obj_type
),
424 *arg_types
= type_array
.release ();
426 return EXT_LANG_RC_OK
;
429 /* See declaration. */
432 python_xmethod_worker::do_get_result_type (value
*obj
, value
**args
, int nargs
,
433 type
**result_type_ptr
)
435 struct type
*obj_type
, *this_type
;
438 gdbpy_enter
enter_py (get_current_arch (), current_language
);
440 /* First see if there is a get_result_type method.
441 If not this could be an old xmethod (pre 7.9.1). */
442 gdbpy_ref
<> get_result_type_method
443 (PyObject_GetAttrString (m_py_worker
, get_result_type_method_name
));
444 if (get_result_type_method
== NULL
)
447 *result_type_ptr
= NULL
;
448 return EXT_LANG_RC_OK
;
451 obj_type
= check_typedef (value_type (obj
));
452 this_type
= check_typedef (type_object_to_type (m_this_type
));
453 if (TYPE_CODE (obj_type
) == TYPE_CODE_PTR
)
455 struct type
*this_ptr
= lookup_pointer_type (this_type
);
457 if (!types_equal (obj_type
, this_ptr
))
458 obj
= value_cast (this_ptr
, obj
);
460 else if (TYPE_IS_REFERENCE (obj_type
))
462 struct type
*this_ref
463 = lookup_reference_type (this_type
, TYPE_CODE (obj_type
));
465 if (!types_equal (obj_type
, this_ref
))
466 obj
= value_cast (this_ref
, obj
);
470 if (!types_equal (obj_type
, this_type
))
471 obj
= value_cast (this_type
, obj
);
473 gdbpy_ref
<> py_value_obj (value_to_value_object (obj
));
474 if (py_value_obj
== NULL
)
476 gdbpy_print_stack ();
477 return EXT_LANG_RC_ERROR
;
480 gdbpy_ref
<> py_arg_tuple (PyTuple_New (nargs
+ 1));
481 if (py_arg_tuple
== NULL
)
483 gdbpy_print_stack ();
484 return EXT_LANG_RC_ERROR
;
487 /* PyTuple_SET_ITEM steals the reference of the element, hence the
489 PyTuple_SET_ITEM (py_arg_tuple
.get (), 0, py_value_obj
.release ());
491 for (i
= 0; i
< nargs
; i
++)
493 PyObject
*py_value_arg
= value_to_value_object (args
[i
]);
495 if (py_value_arg
== NULL
)
497 gdbpy_print_stack ();
498 return EXT_LANG_RC_ERROR
;
500 PyTuple_SET_ITEM (py_arg_tuple
.get (), i
+ 1, py_value_arg
);
503 gdbpy_ref
<> py_result_type
504 (PyObject_CallObject (get_result_type_method
.get (), py_arg_tuple
.get ()));
505 if (py_result_type
== NULL
)
507 gdbpy_print_stack ();
508 return EXT_LANG_RC_ERROR
;
511 *result_type_ptr
= type_object_to_type (py_result_type
.get ());
512 if (*result_type_ptr
== NULL
)
514 PyErr_SetString (PyExc_TypeError
,
515 _("Type returned by the get_result_type method of an"
516 " xmethod worker object is not a gdb.Type object."));
517 gdbpy_print_stack ();
518 return EXT_LANG_RC_ERROR
;
521 return EXT_LANG_RC_OK
;
524 /* See declaration. */
527 python_xmethod_worker::invoke (struct value
*obj
, struct value
**args
,
530 gdbpy_enter
enter_py (get_current_arch (), current_language
);
533 struct type
*obj_type
, *this_type
;
534 struct value
*res
= NULL
;
536 obj_type
= check_typedef (value_type (obj
));
537 this_type
= check_typedef (type_object_to_type (m_this_type
));
538 if (TYPE_CODE (obj_type
) == TYPE_CODE_PTR
)
540 struct type
*this_ptr
= lookup_pointer_type (this_type
);
542 if (!types_equal (obj_type
, this_ptr
))
543 obj
= value_cast (this_ptr
, obj
);
545 else if (TYPE_IS_REFERENCE (obj_type
))
547 struct type
*this_ref
548 = lookup_reference_type (this_type
, TYPE_CODE (obj_type
));
550 if (!types_equal (obj_type
, this_ref
))
551 obj
= value_cast (this_ref
, obj
);
555 if (!types_equal (obj_type
, this_type
))
556 obj
= value_cast (this_type
, obj
);
558 gdbpy_ref
<> py_value_obj (value_to_value_object (obj
));
559 if (py_value_obj
== NULL
)
561 gdbpy_print_stack ();
562 error (_("Error while executing Python code."));
565 gdbpy_ref
<> py_arg_tuple (PyTuple_New (nargs
+ 1));
566 if (py_arg_tuple
== NULL
)
568 gdbpy_print_stack ();
569 error (_("Error while executing Python code."));
572 /* PyTuple_SET_ITEM steals the reference of the element, hence the
574 PyTuple_SET_ITEM (py_arg_tuple
.get (), 0, py_value_obj
.release ());
576 for (i
= 0; i
< nargs
; i
++)
578 PyObject
*py_value_arg
= value_to_value_object (args
[i
]);
580 if (py_value_arg
== NULL
)
582 gdbpy_print_stack ();
583 error (_("Error while executing Python code."));
586 PyTuple_SET_ITEM (py_arg_tuple
.get (), i
+ 1, py_value_arg
);
589 gdbpy_ref
<> py_result (PyObject_CallObject (m_py_worker
,
590 py_arg_tuple
.get ()));
591 if (py_result
== NULL
)
593 gdbpy_print_stack ();
594 error (_("Error while executing Python code."));
597 if (py_result
!= Py_None
)
599 res
= convert_value_from_python (py_result
.get ());
602 gdbpy_print_stack ();
603 error (_("Error while executing Python code."));
608 res
= allocate_value (lookup_typename (python_language
, python_gdbarch
,
615 python_xmethod_worker::python_xmethod_worker (PyObject
*py_worker
,
617 : xmethod_worker (&extension_language_python
),
618 m_py_worker (py_worker
), m_this_type (this_type
)
620 gdb_assert (m_py_worker
!= NULL
&& m_this_type
!= NULL
);
622 Py_INCREF (py_worker
);
623 Py_INCREF (this_type
);
627 gdbpy_initialize_xmethods (void)
629 py_match_method_name
= PyString_FromString (match_method_name
);
630 if (py_match_method_name
== NULL
)
633 py_get_arg_types_method_name
634 = PyString_FromString (get_arg_types_method_name
);
635 if (py_get_arg_types_method_name
== NULL
)