Home | History | Annotate | Line # | Download | only in python
      1  1.1  christos /* Support for debug methods in Python.
      2  1.1  christos 
      3  1.9  christos    Copyright (C) 2013-2024 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    This file is part of GDB.
      6  1.1  christos 
      7  1.1  christos    This program is free software; you can redistribute it and/or modify
      8  1.1  christos    it under the terms of the GNU General Public License as published by
      9  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10  1.1  christos    (at your option) any later version.
     11  1.1  christos 
     12  1.1  christos    This program is distributed in the hope that it will be useful,
     13  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  christos    GNU General Public License for more details.
     16  1.1  christos 
     17  1.1  christos    You should have received a copy of the GNU General Public License
     18  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19  1.1  christos 
     20  1.1  christos #include "arch-utils.h"
     21  1.1  christos #include "extension-priv.h"
     22  1.1  christos #include "objfiles.h"
     23  1.1  christos #include "value.h"
     24  1.1  christos #include "language.h"
     25  1.1  christos 
     26  1.1  christos #include "python.h"
     27  1.1  christos #include "python-internal.h"
     28  1.1  christos 
     29  1.1  christos static const char enabled_field_name[] = "enabled";
     30  1.1  christos static const char match_method_name[] = "match";
     31  1.1  christos static const char get_arg_types_method_name[] = "get_arg_types";
     32  1.1  christos static const char get_result_type_method_name[] = "get_result_type";
     33  1.1  christos static const char matchers_attr_str[] = "xmethods";
     34  1.1  christos 
     35  1.1  christos static PyObject *py_match_method_name = NULL;
     36  1.1  christos static PyObject *py_get_arg_types_method_name = NULL;
     37  1.1  christos 
     38  1.6  christos struct python_xmethod_worker : xmethod_worker
     39  1.1  christos {
     40  1.6  christos   python_xmethod_worker (PyObject *worker, PyObject *this_type);
     41  1.6  christos   ~python_xmethod_worker ();
     42  1.6  christos 
     43  1.6  christos   DISABLE_COPY_AND_ASSIGN (python_xmethod_worker);
     44  1.1  christos 
     45  1.6  christos   /* Implementation of xmethod_worker::invoke for Python.  */
     46  1.1  christos 
     47  1.6  christos   value *invoke (value *obj, gdb::array_view<value *> args) override;
     48  1.1  christos 
     49  1.6  christos   /* Implementation of xmethod_worker::do_get_arg_types for Python.  */
     50  1.1  christos 
     51  1.6  christos   ext_lang_rc do_get_arg_types (std::vector<type *> *type_args) override;
     52  1.1  christos 
     53  1.6  christos   /* Implementation of xmethod_worker::do_get_result_type for Python.
     54  1.1  christos 
     55  1.6  christos      For backward compatibility with 7.9, which did not support getting the
     56  1.6  christos      result type, if the get_result_type operation is not provided by WORKER
     57  1.6  christos      then EXT_LANG_RC_OK is returned and NULL is returned in *RESULT_TYPE.  */
     58  1.1  christos 
     59  1.6  christos   ext_lang_rc do_get_result_type (value *obj, gdb::array_view<value *> args,
     60  1.6  christos 				  type **result_type_ptr) override;
     61  1.1  christos 
     62  1.6  christos private:
     63  1.1  christos 
     64  1.6  christos   PyObject *m_py_worker;
     65  1.6  christos   PyObject *m_this_type;
     66  1.6  christos };
     67  1.1  christos 
     68  1.6  christos python_xmethod_worker::~python_xmethod_worker ()
     69  1.6  christos {
     70  1.1  christos   /* We don't do much here, but we still need the GIL.  */
     71  1.8  christos   gdbpy_enter enter_py;
     72  1.1  christos 
     73  1.6  christos   Py_DECREF (m_py_worker);
     74  1.6  christos   Py_DECREF (m_this_type);
     75  1.1  christos }
     76  1.1  christos 
     77  1.1  christos /* Invoke the "match" method of the MATCHER and return a new reference
     78  1.1  christos    to the result.  Returns NULL on error.  */
     79  1.1  christos 
     80  1.1  christos static PyObject *
     81  1.1  christos invoke_match_method (PyObject *matcher, PyObject *py_obj_type,
     82  1.1  christos 		     const char *xmethod_name)
     83  1.1  christos {
     84  1.1  christos   int enabled;
     85  1.1  christos 
     86  1.5  christos   gdbpy_ref<> enabled_field (PyObject_GetAttrString (matcher,
     87  1.5  christos 						     enabled_field_name));
     88  1.1  christos   if (enabled_field == NULL)
     89  1.5  christos     return NULL;
     90  1.1  christos 
     91  1.5  christos   enabled = PyObject_IsTrue (enabled_field.get ());
     92  1.1  christos   if (enabled == -1)
     93  1.5  christos     return NULL;
     94  1.1  christos   if (enabled == 0)
     95  1.1  christos     {
     96  1.1  christos       /* Return 'None' if the matcher is not enabled.  */
     97  1.1  christos       Py_RETURN_NONE;
     98  1.1  christos     }
     99  1.1  christos 
    100  1.5  christos   gdbpy_ref<> match_method (PyObject_GetAttrString (matcher,
    101  1.5  christos 						    match_method_name));
    102  1.1  christos   if (match_method == NULL)
    103  1.5  christos     return NULL;
    104  1.1  christos 
    105  1.8  christos   gdbpy_ref<> py_xmethod_name (PyUnicode_FromString (xmethod_name));
    106  1.1  christos   if (py_xmethod_name == NULL)
    107  1.5  christos     return NULL;
    108  1.1  christos 
    109  1.5  christos   return PyObject_CallMethodObjArgs (matcher, py_match_method_name,
    110  1.5  christos 				     py_obj_type, py_xmethod_name.get (),
    111  1.5  christos 				     NULL);
    112  1.1  christos }
    113  1.1  christos 
    114  1.1  christos /* Implementation of get_matching_xmethod_workers for Python.  */
    115  1.1  christos 
    116  1.1  christos enum ext_lang_rc
    117  1.1  christos gdbpy_get_matching_xmethod_workers
    118  1.1  christos   (const struct extension_language_defn *extlang,
    119  1.1  christos    struct type *obj_type, const char *method_name,
    120  1.6  christos    std::vector<xmethod_worker_up> *dm_vec)
    121  1.1  christos {
    122  1.1  christos   gdb_assert (obj_type != NULL && method_name != NULL);
    123  1.1  christos 
    124  1.8  christos   gdbpy_enter enter_py;
    125  1.1  christos 
    126  1.5  christos   gdbpy_ref<> py_type (type_to_type_object (obj_type));
    127  1.1  christos   if (py_type == NULL)
    128  1.1  christos     {
    129  1.1  christos       gdbpy_print_stack ();
    130  1.1  christos       return EXT_LANG_RC_ERROR;
    131  1.1  christos     }
    132  1.1  christos 
    133  1.1  christos   /* Create an empty list of debug methods.  */
    134  1.5  christos   gdbpy_ref<> py_xmethod_matcher_list (PyList_New (0));
    135  1.1  christos   if (py_xmethod_matcher_list == NULL)
    136  1.1  christos     {
    137  1.1  christos       gdbpy_print_stack ();
    138  1.1  christos       return EXT_LANG_RC_ERROR;
    139  1.1  christos     }
    140  1.1  christos 
    141  1.1  christos   /* Gather debug method matchers registered with the object files.
    142  1.1  christos      This could be done differently by iterating over each objfile's matcher
    143  1.1  christos      list individually, but there's no data yet to show it's needed.  */
    144  1.6  christos   for (objfile *objfile : current_program_space->objfiles ())
    145  1.1  christos     {
    146  1.6  christos       gdbpy_ref<> py_objfile = objfile_to_objfile_object (objfile);
    147  1.1  christos 
    148  1.1  christos       if (py_objfile == NULL)
    149  1.1  christos 	{
    150  1.1  christos 	  gdbpy_print_stack ();
    151  1.1  christos 	  return EXT_LANG_RC_ERROR;
    152  1.1  christos 	}
    153  1.1  christos 
    154  1.6  christos       gdbpy_ref<> objfile_matchers (objfpy_get_xmethods (py_objfile.get (),
    155  1.6  christos 							 NULL));
    156  1.5  christos       gdbpy_ref<> temp (PySequence_Concat (py_xmethod_matcher_list.get (),
    157  1.5  christos 					   objfile_matchers.get ()));
    158  1.5  christos       if (temp == NULL)
    159  1.1  christos 	{
    160  1.1  christos 	  gdbpy_print_stack ();
    161  1.1  christos 	  return EXT_LANG_RC_ERROR;
    162  1.1  christos 	}
    163  1.5  christos 
    164  1.5  christos       py_xmethod_matcher_list = std::move (temp);
    165  1.1  christos     }
    166  1.1  christos 
    167  1.1  christos   /* Gather debug methods matchers registered with the current program
    168  1.1  christos      space.  */
    169  1.6  christos   gdbpy_ref<> py_progspace = pspace_to_pspace_object (current_program_space);
    170  1.1  christos   if (py_progspace != NULL)
    171  1.1  christos     {
    172  1.6  christos       gdbpy_ref<> pspace_matchers (pspy_get_xmethods (py_progspace.get (),
    173  1.6  christos 						      NULL));
    174  1.1  christos 
    175  1.5  christos       gdbpy_ref<> temp (PySequence_Concat (py_xmethod_matcher_list.get (),
    176  1.5  christos 					   pspace_matchers.get ()));
    177  1.5  christos       if (temp == NULL)
    178  1.1  christos 	{
    179  1.1  christos 	  gdbpy_print_stack ();
    180  1.1  christos 	  return EXT_LANG_RC_ERROR;
    181  1.1  christos 	}
    182  1.5  christos 
    183  1.5  christos       py_xmethod_matcher_list = std::move (temp);
    184  1.1  christos     }
    185  1.1  christos   else
    186  1.1  christos     {
    187  1.1  christos       gdbpy_print_stack ();
    188  1.1  christos       return EXT_LANG_RC_ERROR;
    189  1.1  christos     }
    190  1.1  christos 
    191  1.1  christos   /* Gather debug method matchers registered globally.  */
    192  1.1  christos   if (gdb_python_module != NULL
    193  1.1  christos       && PyObject_HasAttrString (gdb_python_module, matchers_attr_str))
    194  1.1  christos     {
    195  1.5  christos       gdbpy_ref<> gdb_matchers (PyObject_GetAttrString (gdb_python_module,
    196  1.5  christos 							matchers_attr_str));
    197  1.1  christos       if (gdb_matchers != NULL)
    198  1.1  christos 	{
    199  1.5  christos 	  gdbpy_ref<> temp (PySequence_Concat (py_xmethod_matcher_list.get (),
    200  1.5  christos 					       gdb_matchers.get ()));
    201  1.5  christos 	  if (temp == NULL)
    202  1.1  christos 	    {
    203  1.1  christos 	      gdbpy_print_stack ();
    204  1.1  christos 	      return EXT_LANG_RC_ERROR;
    205  1.1  christos 	    }
    206  1.5  christos 
    207  1.5  christos 	  py_xmethod_matcher_list = std::move (temp);
    208  1.1  christos 	}
    209  1.1  christos       else
    210  1.1  christos 	{
    211  1.1  christos 	  gdbpy_print_stack ();
    212  1.1  christos 	  return EXT_LANG_RC_ERROR;
    213  1.1  christos 	}
    214  1.1  christos     }
    215  1.1  christos 
    216  1.5  christos   gdbpy_ref<> list_iter (PyObject_GetIter (py_xmethod_matcher_list.get ()));
    217  1.1  christos   if (list_iter == NULL)
    218  1.1  christos     {
    219  1.1  christos       gdbpy_print_stack ();
    220  1.1  christos       return EXT_LANG_RC_ERROR;
    221  1.1  christos     }
    222  1.5  christos   while (true)
    223  1.1  christos     {
    224  1.5  christos       gdbpy_ref<> matcher (PyIter_Next (list_iter.get ()));
    225  1.5  christos       if (matcher == NULL)
    226  1.5  christos 	{
    227  1.5  christos 	  if (PyErr_Occurred ())
    228  1.5  christos 	    {
    229  1.5  christos 	      gdbpy_print_stack ();
    230  1.5  christos 	      return EXT_LANG_RC_ERROR;
    231  1.5  christos 	    }
    232  1.5  christos 	  break;
    233  1.5  christos 	}
    234  1.5  christos 
    235  1.5  christos       gdbpy_ref<> match_result (invoke_match_method (matcher.get (),
    236  1.5  christos 						     py_type.get (),
    237  1.5  christos 						     method_name));
    238  1.1  christos 
    239  1.1  christos       if (match_result == NULL)
    240  1.1  christos 	{
    241  1.1  christos 	  gdbpy_print_stack ();
    242  1.1  christos 	  return EXT_LANG_RC_ERROR;
    243  1.1  christos 	}
    244  1.1  christos       if (match_result == Py_None)
    245  1.1  christos 	; /* This means there was no match.  */
    246  1.5  christos       else if (PySequence_Check (match_result.get ()))
    247  1.1  christos 	{
    248  1.5  christos 	  gdbpy_ref<> iter (PyObject_GetIter (match_result.get ()));
    249  1.1  christos 
    250  1.1  christos 	  if (iter == NULL)
    251  1.1  christos 	    {
    252  1.1  christos 	      gdbpy_print_stack ();
    253  1.1  christos 	      return EXT_LANG_RC_ERROR;
    254  1.1  christos 	    }
    255  1.5  christos 	  while (true)
    256  1.1  christos 	    {
    257  1.1  christos 	      struct xmethod_worker *worker;
    258  1.1  christos 
    259  1.5  christos 	      gdbpy_ref<> py_worker (PyIter_Next (iter.get ()));
    260  1.5  christos 	      if (py_worker == NULL)
    261  1.5  christos 		{
    262  1.5  christos 		  if (PyErr_Occurred ())
    263  1.5  christos 		    {
    264  1.5  christos 		      gdbpy_print_stack ();
    265  1.5  christos 		      return EXT_LANG_RC_ERROR;
    266  1.5  christos 		    }
    267  1.5  christos 		  break;
    268  1.5  christos 		}
    269  1.5  christos 
    270  1.6  christos 	      worker = new python_xmethod_worker (py_worker.get (),
    271  1.5  christos 						  py_type.get ());
    272  1.6  christos 
    273  1.6  christos 	      dm_vec->emplace_back (worker);
    274  1.1  christos 	    }
    275  1.1  christos 	}
    276  1.1  christos       else
    277  1.1  christos 	{
    278  1.1  christos 	  struct xmethod_worker *worker;
    279  1.1  christos 
    280  1.6  christos 	  worker = new python_xmethod_worker (match_result.get (),
    281  1.5  christos 					      py_type.get ());
    282  1.6  christos 	  dm_vec->emplace_back (worker);
    283  1.1  christos 	}
    284  1.1  christos     }
    285  1.1  christos 
    286  1.1  christos   return EXT_LANG_RC_OK;
    287  1.1  christos }
    288  1.1  christos 
    289  1.6  christos /* See declaration.  */
    290  1.1  christos 
    291  1.6  christos ext_lang_rc
    292  1.6  christos python_xmethod_worker::do_get_arg_types (std::vector<type *> *arg_types)
    293  1.1  christos {
    294  1.5  christos   /* The gdbpy_enter object needs to be placed first, so that it's the last to
    295  1.5  christos      be destroyed.  */
    296  1.8  christos   gdbpy_enter enter_py;
    297  1.5  christos   struct type *obj_type;
    298  1.1  christos   int i = 1, arg_count;
    299  1.5  christos   gdbpy_ref<> list_iter;
    300  1.1  christos 
    301  1.5  christos   gdbpy_ref<> get_arg_types_method
    302  1.6  christos     (PyObject_GetAttrString (m_py_worker, get_arg_types_method_name));
    303  1.1  christos   if (get_arg_types_method == NULL)
    304  1.1  christos     {
    305  1.1  christos       gdbpy_print_stack ();
    306  1.1  christos       return EXT_LANG_RC_ERROR;
    307  1.1  christos     }
    308  1.1  christos 
    309  1.5  christos   gdbpy_ref<> py_argtype_list
    310  1.6  christos     (PyObject_CallMethodObjArgs (m_py_worker, py_get_arg_types_method_name,
    311  1.5  christos 				 NULL));
    312  1.1  christos   if (py_argtype_list == NULL)
    313  1.1  christos     {
    314  1.1  christos       gdbpy_print_stack ();
    315  1.1  christos       return EXT_LANG_RC_ERROR;
    316  1.1  christos     }
    317  1.5  christos 
    318  1.1  christos   if (py_argtype_list == Py_None)
    319  1.1  christos     arg_count = 0;
    320  1.5  christos   else if (PySequence_Check (py_argtype_list.get ()))
    321  1.1  christos     {
    322  1.5  christos       arg_count = PySequence_Size (py_argtype_list.get ());
    323  1.1  christos       if (arg_count == -1)
    324  1.1  christos 	{
    325  1.1  christos 	  gdbpy_print_stack ();
    326  1.1  christos 	  return EXT_LANG_RC_ERROR;
    327  1.1  christos 	}
    328  1.1  christos 
    329  1.5  christos       list_iter.reset (PyObject_GetIter (py_argtype_list.get ()));
    330  1.1  christos       if (list_iter == NULL)
    331  1.1  christos 	{
    332  1.1  christos 	  gdbpy_print_stack ();
    333  1.1  christos 	  return EXT_LANG_RC_ERROR;
    334  1.1  christos 	}
    335  1.1  christos     }
    336  1.1  christos   else
    337  1.1  christos     arg_count = 1;
    338  1.1  christos 
    339  1.1  christos   /* Include the 'this' argument in the size.  */
    340  1.6  christos   arg_types->resize (arg_count + 1);
    341  1.1  christos   i = 1;
    342  1.1  christos   if (list_iter != NULL)
    343  1.1  christos     {
    344  1.5  christos       while (true)
    345  1.1  christos 	{
    346  1.5  christos 	  gdbpy_ref<> item (PyIter_Next (list_iter.get ()));
    347  1.5  christos 	  if (item == NULL)
    348  1.5  christos 	    {
    349  1.5  christos 	      if (PyErr_Occurred ())
    350  1.5  christos 		{
    351  1.5  christos 		  gdbpy_print_stack ();
    352  1.5  christos 		  return EXT_LANG_RC_ERROR;
    353  1.5  christos 		}
    354  1.5  christos 	      break;
    355  1.5  christos 	    }
    356  1.1  christos 
    357  1.5  christos 	  struct type *arg_type = type_object_to_type (item.get ());
    358  1.1  christos 	  if (arg_type == NULL)
    359  1.1  christos 	    {
    360  1.1  christos 	      PyErr_SetString (PyExc_TypeError,
    361  1.1  christos 			       _("Arg type returned by the get_arg_types "
    362  1.1  christos 				 "method of a debug method worker object is "
    363  1.1  christos 				 "not a gdb.Type object."));
    364  1.5  christos 	      return EXT_LANG_RC_ERROR;
    365  1.1  christos 	    }
    366  1.1  christos 
    367  1.6  christos 	  (*arg_types)[i] = arg_type;
    368  1.1  christos 	  i++;
    369  1.1  christos 	}
    370  1.1  christos     }
    371  1.1  christos   else if (arg_count == 1)
    372  1.1  christos     {
    373  1.1  christos       /* py_argtype_list is not actually a list but a single gdb.Type
    374  1.1  christos 	 object.  */
    375  1.5  christos       struct type *arg_type = type_object_to_type (py_argtype_list.get ());
    376  1.1  christos 
    377  1.1  christos       if (arg_type == NULL)
    378  1.1  christos 	{
    379  1.1  christos 	  PyErr_SetString (PyExc_TypeError,
    380  1.1  christos 			   _("Arg type returned by the get_arg_types method "
    381  1.1  christos 			     "of an xmethod worker object is not a gdb.Type "
    382  1.1  christos 			     "object."));
    383  1.5  christos 	  return EXT_LANG_RC_ERROR;
    384  1.1  christos 	}
    385  1.1  christos       else
    386  1.1  christos 	{
    387  1.6  christos 	  (*arg_types)[i] = arg_type;
    388  1.1  christos 	  i++;
    389  1.1  christos 	}
    390  1.1  christos     }
    391  1.1  christos 
    392  1.1  christos   /* Add the type of 'this' as the first argument.  The 'this' pointer should
    393  1.1  christos      be a 'const' value.  Hence, create a 'const' variant of the 'this' pointer
    394  1.1  christos      type.  */
    395  1.6  christos   obj_type = type_object_to_type (m_this_type);
    396  1.6  christos   (*arg_types)[0] = make_cv_type (1, 0, lookup_pointer_type (obj_type),
    397  1.6  christos 				  NULL);
    398  1.1  christos 
    399  1.1  christos   return EXT_LANG_RC_OK;
    400  1.1  christos }
    401  1.1  christos 
    402  1.6  christos /* See declaration.  */
    403  1.1  christos 
    404  1.6  christos ext_lang_rc
    405  1.6  christos python_xmethod_worker::do_get_result_type (value *obj,
    406  1.6  christos 					   gdb::array_view<value *> args,
    407  1.6  christos 					   type **result_type_ptr)
    408  1.1  christos {
    409  1.1  christos   struct type *obj_type, *this_type;
    410  1.1  christos   int i;
    411  1.1  christos 
    412  1.8  christos   gdbpy_enter enter_py;
    413  1.1  christos 
    414  1.1  christos   /* First see if there is a get_result_type method.
    415  1.1  christos      If not this could be an old xmethod (pre 7.9.1).  */
    416  1.5  christos   gdbpy_ref<> get_result_type_method
    417  1.6  christos     (PyObject_GetAttrString (m_py_worker, get_result_type_method_name));
    418  1.1  christos   if (get_result_type_method == NULL)
    419  1.1  christos     {
    420  1.1  christos       PyErr_Clear ();
    421  1.1  christos       *result_type_ptr = NULL;
    422  1.1  christos       return EXT_LANG_RC_OK;
    423  1.1  christos     }
    424  1.1  christos 
    425  1.9  christos   scoped_value_mark free_values;
    426  1.9  christos   obj_type = check_typedef (obj->type ());
    427  1.6  christos   this_type = check_typedef (type_object_to_type (m_this_type));
    428  1.7  christos   if (obj_type->code () == TYPE_CODE_PTR)
    429  1.1  christos     {
    430  1.1  christos       struct type *this_ptr = lookup_pointer_type (this_type);
    431  1.1  christos 
    432  1.1  christos       if (!types_equal (obj_type, this_ptr))
    433  1.1  christos 	obj = value_cast (this_ptr, obj);
    434  1.1  christos     }
    435  1.5  christos   else if (TYPE_IS_REFERENCE (obj_type))
    436  1.1  christos     {
    437  1.5  christos       struct type *this_ref
    438  1.8  christos 	= lookup_reference_type (this_type, obj_type->code ());
    439  1.1  christos 
    440  1.1  christos       if (!types_equal (obj_type, this_ref))
    441  1.1  christos 	obj = value_cast (this_ref, obj);
    442  1.1  christos     }
    443  1.1  christos   else
    444  1.1  christos     {
    445  1.1  christos       if (!types_equal (obj_type, this_type))
    446  1.1  christos 	obj = value_cast (this_type, obj);
    447  1.1  christos     }
    448  1.5  christos   gdbpy_ref<> py_value_obj (value_to_value_object (obj));
    449  1.1  christos   if (py_value_obj == NULL)
    450  1.5  christos     {
    451  1.5  christos       gdbpy_print_stack ();
    452  1.5  christos       return EXT_LANG_RC_ERROR;
    453  1.5  christos     }
    454  1.1  christos 
    455  1.6  christos   gdbpy_ref<> py_arg_tuple (PyTuple_New (args.size () + 1));
    456  1.1  christos   if (py_arg_tuple == NULL)
    457  1.5  christos     {
    458  1.5  christos       gdbpy_print_stack ();
    459  1.5  christos       return EXT_LANG_RC_ERROR;
    460  1.5  christos     }
    461  1.1  christos 
    462  1.5  christos   /* PyTuple_SET_ITEM steals the reference of the element, hence the
    463  1.5  christos      release.  */
    464  1.5  christos   PyTuple_SET_ITEM (py_arg_tuple.get (), 0, py_value_obj.release ());
    465  1.1  christos 
    466  1.6  christos   for (i = 0; i < args.size (); i++)
    467  1.1  christos     {
    468  1.1  christos       PyObject *py_value_arg = value_to_value_object (args[i]);
    469  1.1  christos 
    470  1.1  christos       if (py_value_arg == NULL)
    471  1.5  christos 	{
    472  1.5  christos 	  gdbpy_print_stack ();
    473  1.5  christos 	  return EXT_LANG_RC_ERROR;
    474  1.5  christos 	}
    475  1.5  christos       PyTuple_SET_ITEM (py_arg_tuple.get (), i + 1, py_value_arg);
    476  1.1  christos     }
    477  1.1  christos 
    478  1.5  christos   gdbpy_ref<> py_result_type
    479  1.5  christos     (PyObject_CallObject (get_result_type_method.get (), py_arg_tuple.get ()));
    480  1.1  christos   if (py_result_type == NULL)
    481  1.5  christos     {
    482  1.5  christos       gdbpy_print_stack ();
    483  1.5  christos       return EXT_LANG_RC_ERROR;
    484  1.5  christos     }
    485  1.1  christos 
    486  1.5  christos   *result_type_ptr = type_object_to_type (py_result_type.get ());
    487  1.1  christos   if (*result_type_ptr == NULL)
    488  1.1  christos     {
    489  1.1  christos       PyErr_SetString (PyExc_TypeError,
    490  1.1  christos 		       _("Type returned by the get_result_type method of an"
    491  1.1  christos 			 " xmethod worker object is not a gdb.Type object."));
    492  1.5  christos       gdbpy_print_stack ();
    493  1.5  christos       return EXT_LANG_RC_ERROR;
    494  1.1  christos     }
    495  1.1  christos 
    496  1.1  christos   return EXT_LANG_RC_OK;
    497  1.1  christos }
    498  1.1  christos 
    499  1.6  christos /* See declaration.  */
    500  1.1  christos 
    501  1.1  christos struct value *
    502  1.6  christos python_xmethod_worker::invoke (struct value *obj,
    503  1.6  christos 			       gdb::array_view<value *> args)
    504  1.1  christos {
    505  1.8  christos   gdbpy_enter enter_py;
    506  1.6  christos 
    507  1.1  christos   int i;
    508  1.1  christos   struct type *obj_type, *this_type;
    509  1.1  christos   struct value *res = NULL;
    510  1.1  christos 
    511  1.9  christos   obj_type = check_typedef (obj->type ());
    512  1.6  christos   this_type = check_typedef (type_object_to_type (m_this_type));
    513  1.7  christos   if (obj_type->code () == TYPE_CODE_PTR)
    514  1.1  christos     {
    515  1.1  christos       struct type *this_ptr = lookup_pointer_type (this_type);
    516  1.1  christos 
    517  1.1  christos       if (!types_equal (obj_type, this_ptr))
    518  1.1  christos 	obj = value_cast (this_ptr, obj);
    519  1.1  christos     }
    520  1.5  christos   else if (TYPE_IS_REFERENCE (obj_type))
    521  1.1  christos     {
    522  1.5  christos       struct type *this_ref
    523  1.7  christos 	= lookup_reference_type (this_type, obj_type->code ());
    524  1.1  christos 
    525  1.1  christos       if (!types_equal (obj_type, this_ref))
    526  1.1  christos 	obj = value_cast (this_ref, obj);
    527  1.1  christos     }
    528  1.1  christos   else
    529  1.1  christos     {
    530  1.1  christos       if (!types_equal (obj_type, this_type))
    531  1.1  christos 	obj = value_cast (this_type, obj);
    532  1.1  christos     }
    533  1.5  christos   gdbpy_ref<> py_value_obj (value_to_value_object (obj));
    534  1.1  christos   if (py_value_obj == NULL)
    535  1.1  christos     {
    536  1.1  christos       gdbpy_print_stack ();
    537  1.1  christos       error (_("Error while executing Python code."));
    538  1.1  christos     }
    539  1.1  christos 
    540  1.6  christos   gdbpy_ref<> py_arg_tuple (PyTuple_New (args.size () + 1));
    541  1.1  christos   if (py_arg_tuple == NULL)
    542  1.1  christos     {
    543  1.1  christos       gdbpy_print_stack ();
    544  1.1  christos       error (_("Error while executing Python code."));
    545  1.1  christos     }
    546  1.1  christos 
    547  1.5  christos   /* PyTuple_SET_ITEM steals the reference of the element, hence the
    548  1.5  christos      release.  */
    549  1.5  christos   PyTuple_SET_ITEM (py_arg_tuple.get (), 0, py_value_obj.release ());
    550  1.1  christos 
    551  1.6  christos   for (i = 0; i < args.size (); i++)
    552  1.1  christos     {
    553  1.1  christos       PyObject *py_value_arg = value_to_value_object (args[i]);
    554  1.1  christos 
    555  1.1  christos       if (py_value_arg == NULL)
    556  1.1  christos 	{
    557  1.1  christos 	  gdbpy_print_stack ();
    558  1.1  christos 	  error (_("Error while executing Python code."));
    559  1.1  christos 	}
    560  1.1  christos 
    561  1.5  christos       PyTuple_SET_ITEM (py_arg_tuple.get (), i + 1, py_value_arg);
    562  1.1  christos     }
    563  1.1  christos 
    564  1.6  christos   gdbpy_ref<> py_result (PyObject_CallObject (m_py_worker,
    565  1.5  christos 					      py_arg_tuple.get ()));
    566  1.1  christos   if (py_result == NULL)
    567  1.1  christos     {
    568  1.1  christos       gdbpy_print_stack ();
    569  1.1  christos       error (_("Error while executing Python code."));
    570  1.1  christos     }
    571  1.1  christos 
    572  1.1  christos   if (py_result != Py_None)
    573  1.1  christos     {
    574  1.5  christos       res = convert_value_from_python (py_result.get ());
    575  1.1  christos       if (res == NULL)
    576  1.1  christos 	{
    577  1.1  christos 	  gdbpy_print_stack ();
    578  1.1  christos 	  error (_("Error while executing Python code."));
    579  1.1  christos 	}
    580  1.1  christos     }
    581  1.1  christos   else
    582  1.1  christos     {
    583  1.9  christos       res = value::allocate (lookup_typename (current_language,
    584  1.1  christos 					     "void", NULL, 0));
    585  1.1  christos     }
    586  1.1  christos 
    587  1.1  christos   return res;
    588  1.1  christos }
    589  1.1  christos 
    590  1.6  christos python_xmethod_worker::python_xmethod_worker (PyObject *py_worker,
    591  1.6  christos 					       PyObject *this_type)
    592  1.6  christos : xmethod_worker (&extension_language_python),
    593  1.6  christos   m_py_worker (py_worker), m_this_type (this_type)
    594  1.1  christos {
    595  1.6  christos   gdb_assert (m_py_worker != NULL && m_this_type != NULL);
    596  1.1  christos 
    597  1.1  christos   Py_INCREF (py_worker);
    598  1.1  christos   Py_INCREF (this_type);
    599  1.1  christos }
    600  1.1  christos 
    601  1.9  christos static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
    602  1.1  christos gdbpy_initialize_xmethods (void)
    603  1.1  christos {
    604  1.8  christos   py_match_method_name = PyUnicode_FromString (match_method_name);
    605  1.1  christos   if (py_match_method_name == NULL)
    606  1.1  christos     return -1;
    607  1.1  christos 
    608  1.1  christos   py_get_arg_types_method_name
    609  1.8  christos     = PyUnicode_FromString (get_arg_types_method_name);
    610  1.1  christos   if (py_get_arg_types_method_name == NULL)
    611  1.1  christos     return -1;
    612  1.1  christos 
    613  1.1  christos   return 1;
    614  1.1  christos }
    615  1.9  christos 
    616  1.9  christos GDBPY_INITIALIZE_FILE (gdbpy_initialize_xmethods);
    617