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"
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 gdbpy_worker_data
46 static struct xmethod_worker
*new_python_xmethod_worker (PyObject
*item
,
47 PyObject
*py_obj_type
);
49 /* Implementation of free_xmethod_worker_data for Python. */
52 gdbpy_free_xmethod_worker_data (const struct extension_language_defn
*extlang
,
55 struct gdbpy_worker_data
*worker_data
= (struct gdbpy_worker_data
*) data
;
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 gdbpy_enter
enter_py (get_current_arch (), current_language
);
62 Py_DECREF (worker_data
->worker
);
63 Py_DECREF (worker_data
->this_type
);
67 /* Implementation of clone_xmethod_worker_data for Python. */
70 gdbpy_clone_xmethod_worker_data (const struct extension_language_defn
*extlang
,
73 struct gdbpy_worker_data
*worker_data
74 = (struct gdbpy_worker_data
*) data
, *new_data
;
76 gdb_assert (worker_data
->worker
!= NULL
&& worker_data
->this_type
!= NULL
);
78 /* We don't do much here, but we still need the GIL. */
79 gdbpy_enter
enter_py (get_current_arch (), current_language
);
81 new_data
= XCNEW (struct gdbpy_worker_data
);
82 new_data
->worker
= worker_data
->worker
;
83 new_data
->this_type
= worker_data
->this_type
;
84 Py_INCREF (new_data
->worker
);
85 Py_INCREF (new_data
->this_type
);
90 /* Invoke the "match" method of the MATCHER and return a new reference
91 to the result. Returns NULL on error. */
94 invoke_match_method (PyObject
*matcher
, PyObject
*py_obj_type
,
95 const char *xmethod_name
)
99 gdbpy_ref
<> enabled_field (PyObject_GetAttrString (matcher
,
100 enabled_field_name
));
101 if (enabled_field
== NULL
)
104 enabled
= PyObject_IsTrue (enabled_field
.get ());
109 /* Return 'None' if the matcher is not enabled. */
113 gdbpy_ref
<> match_method (PyObject_GetAttrString (matcher
,
115 if (match_method
== NULL
)
118 gdbpy_ref
<> py_xmethod_name (PyString_FromString (xmethod_name
));
119 if (py_xmethod_name
== NULL
)
122 return PyObject_CallMethodObjArgs (matcher
, py_match_method_name
,
123 py_obj_type
, py_xmethod_name
.get (),
127 /* Implementation of get_matching_xmethod_workers for Python. */
130 gdbpy_get_matching_xmethod_workers
131 (const struct extension_language_defn
*extlang
,
132 struct type
*obj_type
, const char *method_name
,
133 xmethod_worker_vec
**dm_vec
)
135 struct objfile
*objfile
;
136 VEC (xmethod_worker_ptr
) *worker_vec
= NULL
;
137 PyObject
*py_progspace
;
139 gdb_assert (obj_type
!= NULL
&& method_name
!= NULL
);
141 gdbpy_enter
enter_py (get_current_arch (), current_language
);
143 gdbpy_ref
<> py_type (type_to_type_object (obj_type
));
146 gdbpy_print_stack ();
147 return EXT_LANG_RC_ERROR
;
150 /* Create an empty list of debug methods. */
151 gdbpy_ref
<> py_xmethod_matcher_list (PyList_New (0));
152 if (py_xmethod_matcher_list
== NULL
)
154 gdbpy_print_stack ();
155 return EXT_LANG_RC_ERROR
;
158 /* Gather debug method matchers registered with the object files.
159 This could be done differently by iterating over each objfile's matcher
160 list individually, but there's no data yet to show it's needed. */
161 ALL_OBJFILES (objfile
)
163 PyObject
*py_objfile
= objfile_to_objfile_object (objfile
);
165 if (py_objfile
== NULL
)
167 gdbpy_print_stack ();
168 return EXT_LANG_RC_ERROR
;
171 gdbpy_ref
<> objfile_matchers (objfpy_get_xmethods (py_objfile
, NULL
));
172 gdbpy_ref
<> temp (PySequence_Concat (py_xmethod_matcher_list
.get (),
173 objfile_matchers
.get ()));
176 gdbpy_print_stack ();
177 return EXT_LANG_RC_ERROR
;
180 py_xmethod_matcher_list
= std::move (temp
);
183 /* Gather debug methods matchers registered with the current program
185 py_progspace
= pspace_to_pspace_object (current_program_space
);
186 if (py_progspace
!= NULL
)
188 gdbpy_ref
<> pspace_matchers (pspy_get_xmethods (py_progspace
, NULL
));
190 gdbpy_ref
<> temp (PySequence_Concat (py_xmethod_matcher_list
.get (),
191 pspace_matchers
.get ()));
194 gdbpy_print_stack ();
195 return EXT_LANG_RC_ERROR
;
198 py_xmethod_matcher_list
= std::move (temp
);
202 gdbpy_print_stack ();
203 return EXT_LANG_RC_ERROR
;
206 /* Gather debug method matchers registered globally. */
207 if (gdb_python_module
!= NULL
208 && PyObject_HasAttrString (gdb_python_module
, matchers_attr_str
))
210 gdbpy_ref
<> gdb_matchers (PyObject_GetAttrString (gdb_python_module
,
212 if (gdb_matchers
!= NULL
)
214 gdbpy_ref
<> temp (PySequence_Concat (py_xmethod_matcher_list
.get (),
215 gdb_matchers
.get ()));
218 gdbpy_print_stack ();
219 return EXT_LANG_RC_ERROR
;
222 py_xmethod_matcher_list
= std::move (temp
);
226 gdbpy_print_stack ();
227 return EXT_LANG_RC_ERROR
;
231 gdbpy_ref
<> list_iter (PyObject_GetIter (py_xmethod_matcher_list
.get ()));
232 if (list_iter
== NULL
)
234 gdbpy_print_stack ();
235 return EXT_LANG_RC_ERROR
;
239 gdbpy_ref
<> matcher (PyIter_Next (list_iter
.get ()));
242 if (PyErr_Occurred ())
244 gdbpy_print_stack ();
245 return EXT_LANG_RC_ERROR
;
250 gdbpy_ref
<> match_result (invoke_match_method (matcher
.get (),
254 if (match_result
== NULL
)
256 gdbpy_print_stack ();
257 return EXT_LANG_RC_ERROR
;
259 if (match_result
== Py_None
)
260 ; /* This means there was no match. */
261 else if (PySequence_Check (match_result
.get ()))
263 gdbpy_ref
<> iter (PyObject_GetIter (match_result
.get ()));
267 gdbpy_print_stack ();
268 return EXT_LANG_RC_ERROR
;
272 struct xmethod_worker
*worker
;
274 gdbpy_ref
<> py_worker (PyIter_Next (iter
.get ()));
275 if (py_worker
== NULL
)
277 if (PyErr_Occurred ())
279 gdbpy_print_stack ();
280 return EXT_LANG_RC_ERROR
;
285 worker
= new_python_xmethod_worker (py_worker
.get (),
287 VEC_safe_push (xmethod_worker_ptr
, worker_vec
, worker
);
292 struct xmethod_worker
*worker
;
294 worker
= new_python_xmethod_worker (match_result
.get (),
296 VEC_safe_push (xmethod_worker_ptr
, worker_vec
, worker
);
300 *dm_vec
= worker_vec
;
302 return EXT_LANG_RC_OK
;
305 /* Implementation of get_xmethod_arg_types for Python. */
308 gdbpy_get_xmethod_arg_types (const struct extension_language_defn
*extlang
,
309 struct xmethod_worker
*worker
,
310 int *nargs
, struct type
***arg_types
)
312 /* The gdbpy_enter object needs to be placed first, so that it's the last to
314 gdbpy_enter
enter_py (get_current_arch (), current_language
);
315 struct gdbpy_worker_data
*worker_data
316 = (struct gdbpy_worker_data
*) worker
->data
;
317 PyObject
*py_worker
= worker_data
->worker
;
318 struct type
*obj_type
;
319 int i
= 1, arg_count
;
320 gdbpy_ref
<> list_iter
;
322 /* Set nargs to -1 so that any premature return from this function returns
323 an invalid/unusable number of arg types. */
326 gdbpy_ref
<> get_arg_types_method
327 (PyObject_GetAttrString (py_worker
, get_arg_types_method_name
));
328 if (get_arg_types_method
== NULL
)
330 gdbpy_print_stack ();
331 return EXT_LANG_RC_ERROR
;
334 gdbpy_ref
<> py_argtype_list
335 (PyObject_CallMethodObjArgs (py_worker
, py_get_arg_types_method_name
,
337 if (py_argtype_list
== NULL
)
339 gdbpy_print_stack ();
340 return EXT_LANG_RC_ERROR
;
343 if (py_argtype_list
== Py_None
)
345 else if (PySequence_Check (py_argtype_list
.get ()))
347 arg_count
= PySequence_Size (py_argtype_list
.get ());
350 gdbpy_print_stack ();
351 return EXT_LANG_RC_ERROR
;
354 list_iter
.reset (PyObject_GetIter (py_argtype_list
.get ()));
355 if (list_iter
== NULL
)
357 gdbpy_print_stack ();
358 return EXT_LANG_RC_ERROR
;
364 /* Include the 'this' argument in the size. */
365 gdb::unique_xmalloc_ptr
<struct type
*> type_array
366 (XCNEWVEC (struct type
*, arg_count
+ 1));
368 if (list_iter
!= NULL
)
372 gdbpy_ref
<> item (PyIter_Next (list_iter
.get ()));
375 if (PyErr_Occurred ())
377 gdbpy_print_stack ();
378 return EXT_LANG_RC_ERROR
;
383 struct type
*arg_type
= type_object_to_type (item
.get ());
384 if (arg_type
== NULL
)
386 PyErr_SetString (PyExc_TypeError
,
387 _("Arg type returned by the get_arg_types "
388 "method of a debug method worker object is "
389 "not a gdb.Type object."));
390 return EXT_LANG_RC_ERROR
;
393 (type_array
.get ())[i
] = arg_type
;
397 else if (arg_count
== 1)
399 /* py_argtype_list is not actually a list but a single gdb.Type
401 struct type
*arg_type
= type_object_to_type (py_argtype_list
.get ());
403 if (arg_type
== NULL
)
405 PyErr_SetString (PyExc_TypeError
,
406 _("Arg type returned by the get_arg_types method "
407 "of an xmethod worker object is not a gdb.Type "
409 return EXT_LANG_RC_ERROR
;
413 (type_array
.get ())[i
] = arg_type
;
418 /* Add the type of 'this' as the first argument. The 'this' pointer should
419 be a 'const' value. Hence, create a 'const' variant of the 'this' pointer
421 obj_type
= type_object_to_type (worker_data
->this_type
);
422 (type_array
.get ())[0] = make_cv_type (1, 0, lookup_pointer_type (obj_type
),
425 *arg_types
= type_array
.release ();
427 return EXT_LANG_RC_OK
;
430 /* Implementation of get_xmethod_result_type for Python. */
433 gdbpy_get_xmethod_result_type (const struct extension_language_defn
*extlang
,
434 struct xmethod_worker
*worker
,
436 struct value
**args
, int nargs
,
437 struct type
**result_type_ptr
)
439 struct gdbpy_worker_data
*worker_data
440 = (struct gdbpy_worker_data
*) worker
->data
;
441 PyObject
*py_worker
= worker_data
->worker
;
442 struct type
*obj_type
, *this_type
;
445 gdbpy_enter
enter_py (get_current_arch (), current_language
);
447 /* First see if there is a get_result_type method.
448 If not this could be an old xmethod (pre 7.9.1). */
449 gdbpy_ref
<> get_result_type_method
450 (PyObject_GetAttrString (py_worker
, get_result_type_method_name
));
451 if (get_result_type_method
== NULL
)
454 *result_type_ptr
= NULL
;
455 return EXT_LANG_RC_OK
;
458 obj_type
= check_typedef (value_type (obj
));
459 this_type
= check_typedef (type_object_to_type (worker_data
->this_type
));
460 if (TYPE_CODE (obj_type
) == TYPE_CODE_PTR
)
462 struct type
*this_ptr
= lookup_pointer_type (this_type
);
464 if (!types_equal (obj_type
, this_ptr
))
465 obj
= value_cast (this_ptr
, obj
);
467 else if (TYPE_IS_REFERENCE (obj_type
))
469 struct type
*this_ref
470 = lookup_reference_type (this_type
, TYPE_CODE (obj_type
));
472 if (!types_equal (obj_type
, this_ref
))
473 obj
= value_cast (this_ref
, obj
);
477 if (!types_equal (obj_type
, this_type
))
478 obj
= value_cast (this_type
, obj
);
480 gdbpy_ref
<> py_value_obj (value_to_value_object (obj
));
481 if (py_value_obj
== NULL
)
483 gdbpy_print_stack ();
484 return EXT_LANG_RC_ERROR
;
487 gdbpy_ref
<> py_arg_tuple (PyTuple_New (nargs
+ 1));
488 if (py_arg_tuple
== NULL
)
490 gdbpy_print_stack ();
491 return EXT_LANG_RC_ERROR
;
494 /* PyTuple_SET_ITEM steals the reference of the element, hence the
496 PyTuple_SET_ITEM (py_arg_tuple
.get (), 0, py_value_obj
.release ());
498 for (i
= 0; i
< nargs
; i
++)
500 PyObject
*py_value_arg
= value_to_value_object (args
[i
]);
502 if (py_value_arg
== NULL
)
504 gdbpy_print_stack ();
505 return EXT_LANG_RC_ERROR
;
507 PyTuple_SET_ITEM (py_arg_tuple
.get (), i
+ 1, py_value_arg
);
510 gdbpy_ref
<> py_result_type
511 (PyObject_CallObject (get_result_type_method
.get (), py_arg_tuple
.get ()));
512 if (py_result_type
== NULL
)
514 gdbpy_print_stack ();
515 return EXT_LANG_RC_ERROR
;
518 *result_type_ptr
= type_object_to_type (py_result_type
.get ());
519 if (*result_type_ptr
== NULL
)
521 PyErr_SetString (PyExc_TypeError
,
522 _("Type returned by the get_result_type method of an"
523 " xmethod worker object is not a gdb.Type object."));
524 gdbpy_print_stack ();
525 return EXT_LANG_RC_ERROR
;
528 return EXT_LANG_RC_OK
;
531 /* Implementation of invoke_xmethod for Python. */
534 gdbpy_invoke_xmethod (const struct extension_language_defn
*extlang
,
535 struct xmethod_worker
*worker
,
536 struct value
*obj
, struct value
**args
, int nargs
)
539 struct type
*obj_type
, *this_type
;
540 struct value
*res
= NULL
;
541 struct gdbpy_worker_data
*worker_data
542 = (struct gdbpy_worker_data
*) worker
->data
;
543 PyObject
*xmethod_worker
= worker_data
->worker
;
545 gdbpy_enter
enter_py (get_current_arch (), current_language
);
547 obj_type
= check_typedef (value_type (obj
));
548 this_type
= check_typedef (type_object_to_type (worker_data
->this_type
));
549 if (TYPE_CODE (obj_type
) == TYPE_CODE_PTR
)
551 struct type
*this_ptr
= lookup_pointer_type (this_type
);
553 if (!types_equal (obj_type
, this_ptr
))
554 obj
= value_cast (this_ptr
, obj
);
556 else if (TYPE_IS_REFERENCE (obj_type
))
558 struct type
*this_ref
559 = lookup_reference_type (this_type
, TYPE_CODE (obj_type
));
561 if (!types_equal (obj_type
, this_ref
))
562 obj
= value_cast (this_ref
, obj
);
566 if (!types_equal (obj_type
, this_type
))
567 obj
= value_cast (this_type
, obj
);
569 gdbpy_ref
<> py_value_obj (value_to_value_object (obj
));
570 if (py_value_obj
== NULL
)
572 gdbpy_print_stack ();
573 error (_("Error while executing Python code."));
576 gdbpy_ref
<> py_arg_tuple (PyTuple_New (nargs
+ 1));
577 if (py_arg_tuple
== NULL
)
579 gdbpy_print_stack ();
580 error (_("Error while executing Python code."));
583 /* PyTuple_SET_ITEM steals the reference of the element, hence the
585 PyTuple_SET_ITEM (py_arg_tuple
.get (), 0, py_value_obj
.release ());
587 for (i
= 0; i
< nargs
; i
++)
589 PyObject
*py_value_arg
= value_to_value_object (args
[i
]);
591 if (py_value_arg
== NULL
)
593 gdbpy_print_stack ();
594 error (_("Error while executing Python code."));
597 PyTuple_SET_ITEM (py_arg_tuple
.get (), i
+ 1, py_value_arg
);
600 gdbpy_ref
<> py_result (PyObject_CallObject (xmethod_worker
,
601 py_arg_tuple
.get ()));
602 if (py_result
== NULL
)
604 gdbpy_print_stack ();
605 error (_("Error while executing Python code."));
608 if (py_result
!= Py_None
)
610 res
= convert_value_from_python (py_result
.get ());
613 gdbpy_print_stack ();
614 error (_("Error while executing Python code."));
619 res
= allocate_value (lookup_typename (python_language
, python_gdbarch
,
626 /* Creates a new Python xmethod_worker object.
627 The new object has data of type 'struct gdbpy_worker_data' composed
628 with the components PY_WORKER and THIS_TYPE. */
630 static struct xmethod_worker
*
631 new_python_xmethod_worker (PyObject
*py_worker
, PyObject
*this_type
)
633 struct gdbpy_worker_data
*data
;
635 gdb_assert (py_worker
!= NULL
&& this_type
!= NULL
);
637 data
= XCNEW (struct gdbpy_worker_data
);
638 data
->worker
= py_worker
;
639 data
->this_type
= this_type
;
640 Py_INCREF (py_worker
);
641 Py_INCREF (this_type
);
643 return new_xmethod_worker (&extension_language_python
, data
);
647 gdbpy_initialize_xmethods (void)
649 py_match_method_name
= PyString_FromString (match_method_name
);
650 if (py_match_method_name
== NULL
)
653 py_get_arg_types_method_name
654 = PyString_FromString (get_arg_types_method_name
);
655 if (py_get_arg_types_method_name
== NULL
)