Home | History | Annotate | Line # | Download | only in python
py-xmethods.c revision 1.1
      1  1.1  christos /* Support for debug methods in Python.
      2  1.1  christos 
      3  1.1  christos    Copyright (C) 2013-2015 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 "defs.h"
     21  1.1  christos #include "arch-utils.h"
     22  1.1  christos #include "extension-priv.h"
     23  1.1  christos #include "objfiles.h"
     24  1.1  christos #include "value.h"
     25  1.1  christos #include "language.h"
     26  1.1  christos 
     27  1.1  christos #include "python.h"
     28  1.1  christos #include "python-internal.h"
     29  1.1  christos 
     30  1.1  christos static const char enabled_field_name[] = "enabled";
     31  1.1  christos static const char match_method_name[] = "match";
     32  1.1  christos static const char get_arg_types_method_name[] = "get_arg_types";
     33  1.1  christos static const char get_result_type_method_name[] = "get_result_type";
     34  1.1  christos static const char invoke_method_name[] = "invoke";
     35  1.1  christos static const char matchers_attr_str[] = "xmethods";
     36  1.1  christos 
     37  1.1  christos static PyObject *py_match_method_name = NULL;
     38  1.1  christos static PyObject *py_get_arg_types_method_name = NULL;
     39  1.1  christos static PyObject *py_get_result_type_method_name = NULL;
     40  1.1  christos static PyObject *py_invoke_method_name = NULL;
     41  1.1  christos 
     42  1.1  christos struct gdbpy_worker_data
     43  1.1  christos {
     44  1.1  christos   PyObject *worker;
     45  1.1  christos   PyObject *this_type;
     46  1.1  christos };
     47  1.1  christos 
     48  1.1  christos static struct xmethod_worker *new_python_xmethod_worker (PyObject *item,
     49  1.1  christos 							 PyObject *py_obj_type);
     50  1.1  christos 
     51  1.1  christos /* Implementation of free_xmethod_worker_data for Python.  */
     52  1.1  christos 
     53  1.1  christos void
     54  1.1  christos gdbpy_free_xmethod_worker_data (const struct extension_language_defn *extlang,
     55  1.1  christos 				void *data)
     56  1.1  christos {
     57  1.1  christos   struct gdbpy_worker_data *worker_data = data;
     58  1.1  christos   struct cleanup *cleanups;
     59  1.1  christos 
     60  1.1  christos   gdb_assert (worker_data->worker != NULL && worker_data->this_type != NULL);
     61  1.1  christos 
     62  1.1  christos   /* We don't do much here, but we still need the GIL.  */
     63  1.1  christos   cleanups = ensure_python_env (get_current_arch (), current_language);
     64  1.1  christos 
     65  1.1  christos   Py_DECREF (worker_data->worker);
     66  1.1  christos   Py_DECREF (worker_data->this_type);
     67  1.1  christos   xfree (worker_data);
     68  1.1  christos 
     69  1.1  christos   do_cleanups (cleanups);
     70  1.1  christos }
     71  1.1  christos 
     72  1.1  christos /* Implementation of clone_xmethod_worker_data for Python.  */
     73  1.1  christos 
     74  1.1  christos void *
     75  1.1  christos gdbpy_clone_xmethod_worker_data (const struct extension_language_defn *extlang,
     76  1.1  christos 				 void *data)
     77  1.1  christos {
     78  1.1  christos   struct gdbpy_worker_data *worker_data = data, *new_data;
     79  1.1  christos   struct cleanup *cleanups;
     80  1.1  christos 
     81  1.1  christos   gdb_assert (worker_data->worker != NULL && worker_data->this_type != NULL);
     82  1.1  christos 
     83  1.1  christos   /* We don't do much here, but we still need the GIL.  */
     84  1.1  christos   cleanups = ensure_python_env (get_current_arch (), current_language);
     85  1.1  christos 
     86  1.1  christos   new_data = XCNEW (struct gdbpy_worker_data);
     87  1.1  christos   new_data->worker = worker_data->worker;
     88  1.1  christos   new_data->this_type = worker_data->this_type;
     89  1.1  christos   Py_INCREF (new_data->worker);
     90  1.1  christos   Py_INCREF (new_data->this_type);
     91  1.1  christos 
     92  1.1  christos   do_cleanups (cleanups);
     93  1.1  christos 
     94  1.1  christos   return new_data;
     95  1.1  christos }
     96  1.1  christos 
     97  1.1  christos /* Invoke the "match" method of the MATCHER and return a new reference
     98  1.1  christos    to the result.  Returns NULL on error.  */
     99  1.1  christos 
    100  1.1  christos static PyObject *
    101  1.1  christos invoke_match_method (PyObject *matcher, PyObject *py_obj_type,
    102  1.1  christos 		     const char *xmethod_name)
    103  1.1  christos {
    104  1.1  christos   PyObject *py_xmethod_name;
    105  1.1  christos   PyObject *match_method, *enabled_field, *match_result;
    106  1.1  christos   struct cleanup *cleanups;
    107  1.1  christos   int enabled;
    108  1.1  christos 
    109  1.1  christos   cleanups = make_cleanup (null_cleanup, NULL);
    110  1.1  christos 
    111  1.1  christos   enabled_field = PyObject_GetAttrString (matcher, enabled_field_name);
    112  1.1  christos   if (enabled_field == NULL)
    113  1.1  christos     {
    114  1.1  christos       do_cleanups (cleanups);
    115  1.1  christos       return NULL;
    116  1.1  christos     }
    117  1.1  christos   make_cleanup_py_decref (enabled_field);
    118  1.1  christos 
    119  1.1  christos   enabled = PyObject_IsTrue (enabled_field);
    120  1.1  christos   if (enabled == -1)
    121  1.1  christos     {
    122  1.1  christos       do_cleanups (cleanups);
    123  1.1  christos       return NULL;
    124  1.1  christos     }
    125  1.1  christos   if (enabled == 0)
    126  1.1  christos     {
    127  1.1  christos       /* Return 'None' if the matcher is not enabled.  */
    128  1.1  christos       do_cleanups (cleanups);
    129  1.1  christos       Py_RETURN_NONE;
    130  1.1  christos     }
    131  1.1  christos 
    132  1.1  christos   match_method = PyObject_GetAttrString (matcher, match_method_name);
    133  1.1  christos   if (match_method == NULL)
    134  1.1  christos     {
    135  1.1  christos       do_cleanups (cleanups);
    136  1.1  christos       return NULL;
    137  1.1  christos     }
    138  1.1  christos   make_cleanup_py_decref (match_method);
    139  1.1  christos 
    140  1.1  christos   py_xmethod_name = PyString_FromString (xmethod_name);
    141  1.1  christos   if (py_xmethod_name == NULL)
    142  1.1  christos     {
    143  1.1  christos       do_cleanups (cleanups);
    144  1.1  christos       return NULL;
    145  1.1  christos     }
    146  1.1  christos   make_cleanup_py_decref (py_xmethod_name);
    147  1.1  christos 
    148  1.1  christos   match_result = PyObject_CallMethodObjArgs (matcher,
    149  1.1  christos 					     py_match_method_name,
    150  1.1  christos 					     py_obj_type,
    151  1.1  christos 					     py_xmethod_name,
    152  1.1  christos 					     NULL);
    153  1.1  christos 
    154  1.1  christos   do_cleanups (cleanups);
    155  1.1  christos 
    156  1.1  christos   return match_result;
    157  1.1  christos }
    158  1.1  christos 
    159  1.1  christos /* Implementation of get_matching_xmethod_workers for Python.  */
    160  1.1  christos 
    161  1.1  christos enum ext_lang_rc
    162  1.1  christos gdbpy_get_matching_xmethod_workers
    163  1.1  christos   (const struct extension_language_defn *extlang,
    164  1.1  christos    struct type *obj_type, const char *method_name,
    165  1.1  christos    xmethod_worker_vec **dm_vec)
    166  1.1  christos {
    167  1.1  christos   struct cleanup *cleanups;
    168  1.1  christos   struct objfile *objfile;
    169  1.1  christos   VEC (xmethod_worker_ptr) *worker_vec = NULL;
    170  1.1  christos   PyObject *py_type, *py_progspace;
    171  1.1  christos   PyObject *py_xmethod_matcher_list = NULL, *list_iter, *matcher;
    172  1.1  christos 
    173  1.1  christos   gdb_assert (obj_type != NULL && method_name != NULL);
    174  1.1  christos 
    175  1.1  christos   cleanups = ensure_python_env (get_current_arch (), current_language);
    176  1.1  christos 
    177  1.1  christos   py_type = type_to_type_object (obj_type);
    178  1.1  christos   if (py_type == NULL)
    179  1.1  christos     {
    180  1.1  christos       gdbpy_print_stack ();
    181  1.1  christos       do_cleanups (cleanups);
    182  1.1  christos 
    183  1.1  christos       return EXT_LANG_RC_ERROR;
    184  1.1  christos     }
    185  1.1  christos   make_cleanup_py_decref (py_type);
    186  1.1  christos 
    187  1.1  christos   /* Create an empty list of debug methods.  */
    188  1.1  christos   py_xmethod_matcher_list = PyList_New (0);
    189  1.1  christos   if (py_xmethod_matcher_list == NULL)
    190  1.1  christos     {
    191  1.1  christos       gdbpy_print_stack ();
    192  1.1  christos       do_cleanups (cleanups);
    193  1.1  christos 
    194  1.1  christos       return EXT_LANG_RC_ERROR;
    195  1.1  christos     }
    196  1.1  christos 
    197  1.1  christos   /* Gather debug method matchers registered with the object files.
    198  1.1  christos      This could be done differently by iterating over each objfile's matcher
    199  1.1  christos      list individually, but there's no data yet to show it's needed.  */
    200  1.1  christos   ALL_OBJFILES (objfile)
    201  1.1  christos     {
    202  1.1  christos       PyObject *py_objfile = objfile_to_objfile_object (objfile);
    203  1.1  christos       PyObject *objfile_matchers, *temp = py_xmethod_matcher_list;
    204  1.1  christos 
    205  1.1  christos       if (py_objfile == NULL)
    206  1.1  christos 	{
    207  1.1  christos 	  gdbpy_print_stack ();
    208  1.1  christos 	  Py_DECREF (py_xmethod_matcher_list);
    209  1.1  christos 	  do_cleanups (cleanups);
    210  1.1  christos 
    211  1.1  christos 	  return EXT_LANG_RC_ERROR;
    212  1.1  christos 	}
    213  1.1  christos 
    214  1.1  christos       objfile_matchers = objfpy_get_xmethods (py_objfile, NULL);
    215  1.1  christos       py_xmethod_matcher_list = PySequence_Concat (temp, objfile_matchers);
    216  1.1  christos       Py_DECREF (temp);
    217  1.1  christos       Py_DECREF (objfile_matchers);
    218  1.1  christos       if (py_xmethod_matcher_list == NULL)
    219  1.1  christos 	{
    220  1.1  christos 	  gdbpy_print_stack ();
    221  1.1  christos 	  do_cleanups (cleanups);
    222  1.1  christos 
    223  1.1  christos 	  return EXT_LANG_RC_ERROR;
    224  1.1  christos 	}
    225  1.1  christos     }
    226  1.1  christos 
    227  1.1  christos   /* Gather debug methods matchers registered with the current program
    228  1.1  christos      space.  */
    229  1.1  christos   py_progspace = pspace_to_pspace_object (current_program_space);
    230  1.1  christos   if (py_progspace != NULL)
    231  1.1  christos     {
    232  1.1  christos       PyObject *temp = py_xmethod_matcher_list;
    233  1.1  christos       PyObject *pspace_matchers = pspy_get_xmethods (py_progspace, NULL);
    234  1.1  christos 
    235  1.1  christos       py_xmethod_matcher_list = PySequence_Concat (temp, pspace_matchers);
    236  1.1  christos       Py_DECREF (temp);
    237  1.1  christos       Py_DECREF (pspace_matchers);
    238  1.1  christos       if (py_xmethod_matcher_list == NULL)
    239  1.1  christos 	{
    240  1.1  christos 	  gdbpy_print_stack ();
    241  1.1  christos 	  do_cleanups (cleanups);
    242  1.1  christos 
    243  1.1  christos 	  return EXT_LANG_RC_ERROR;
    244  1.1  christos 	}
    245  1.1  christos     }
    246  1.1  christos   else
    247  1.1  christos     {
    248  1.1  christos       gdbpy_print_stack ();
    249  1.1  christos       Py_DECREF (py_xmethod_matcher_list);
    250  1.1  christos       do_cleanups (cleanups);
    251  1.1  christos 
    252  1.1  christos       return EXT_LANG_RC_ERROR;
    253  1.1  christos     }
    254  1.1  christos 
    255  1.1  christos   /* Gather debug method matchers registered globally.  */
    256  1.1  christos   if (gdb_python_module != NULL
    257  1.1  christos       && PyObject_HasAttrString (gdb_python_module, matchers_attr_str))
    258  1.1  christos     {
    259  1.1  christos       PyObject *gdb_matchers;
    260  1.1  christos       PyObject *temp = py_xmethod_matcher_list;
    261  1.1  christos 
    262  1.1  christos       gdb_matchers = PyObject_GetAttrString (gdb_python_module,
    263  1.1  christos 					     matchers_attr_str);
    264  1.1  christos       if (gdb_matchers != NULL)
    265  1.1  christos 	{
    266  1.1  christos 	  py_xmethod_matcher_list = PySequence_Concat (temp, gdb_matchers);
    267  1.1  christos 	  Py_DECREF (temp);
    268  1.1  christos 	  Py_DECREF (gdb_matchers);
    269  1.1  christos 	  if (py_xmethod_matcher_list == NULL)
    270  1.1  christos 	    {
    271  1.1  christos 	      gdbpy_print_stack ();
    272  1.1  christos 	      do_cleanups (cleanups);
    273  1.1  christos 
    274  1.1  christos 	      return EXT_LANG_RC_ERROR;
    275  1.1  christos 	    }
    276  1.1  christos 	}
    277  1.1  christos       else
    278  1.1  christos 	{
    279  1.1  christos 	  gdbpy_print_stack ();
    280  1.1  christos 	  Py_DECREF (py_xmethod_matcher_list);
    281  1.1  christos 	  do_cleanups (cleanups);
    282  1.1  christos 
    283  1.1  christos 	  return EXT_LANG_RC_ERROR;
    284  1.1  christos 	}
    285  1.1  christos     }
    286  1.1  christos 
    287  1.1  christos   /* Safe to make a cleanup for py_xmethod_matcher_list now as it
    288  1.1  christos      will not change any more.  */
    289  1.1  christos   make_cleanup_py_decref (py_xmethod_matcher_list);
    290  1.1  christos 
    291  1.1  christos   list_iter = PyObject_GetIter (py_xmethod_matcher_list);
    292  1.1  christos   if (list_iter == NULL)
    293  1.1  christos     {
    294  1.1  christos       gdbpy_print_stack ();
    295  1.1  christos       do_cleanups (cleanups);
    296  1.1  christos 
    297  1.1  christos       return EXT_LANG_RC_ERROR;
    298  1.1  christos     }
    299  1.1  christos   while ((matcher = PyIter_Next (list_iter)) != NULL)
    300  1.1  christos     {
    301  1.1  christos       PyObject *match_result = invoke_match_method (matcher, py_type,
    302  1.1  christos 						    method_name);
    303  1.1  christos 
    304  1.1  christos       if (match_result == NULL)
    305  1.1  christos 	{
    306  1.1  christos 	  gdbpy_print_stack ();
    307  1.1  christos 	  Py_DECREF (matcher);
    308  1.1  christos 	  do_cleanups (cleanups);
    309  1.1  christos 
    310  1.1  christos 	  return EXT_LANG_RC_ERROR;
    311  1.1  christos 	}
    312  1.1  christos       if (match_result == Py_None)
    313  1.1  christos 	; /* This means there was no match.  */
    314  1.1  christos       else if (PySequence_Check (match_result))
    315  1.1  christos 	{
    316  1.1  christos 	  PyObject *iter = PyObject_GetIter (match_result);
    317  1.1  christos 	  PyObject *py_worker;
    318  1.1  christos 
    319  1.1  christos 	  if (iter == NULL)
    320  1.1  christos 	    {
    321  1.1  christos 	      gdbpy_print_stack ();
    322  1.1  christos 	      Py_DECREF (matcher);
    323  1.1  christos 	      Py_DECREF (match_result);
    324  1.1  christos 	      do_cleanups (cleanups);
    325  1.1  christos 
    326  1.1  christos 	      return EXT_LANG_RC_ERROR;
    327  1.1  christos 	    }
    328  1.1  christos 	  while ((py_worker = PyIter_Next (iter)) != NULL)
    329  1.1  christos 	    {
    330  1.1  christos 	      struct xmethod_worker *worker;
    331  1.1  christos 
    332  1.1  christos 	      worker = new_python_xmethod_worker (py_worker, py_type);
    333  1.1  christos 	      VEC_safe_push (xmethod_worker_ptr, worker_vec, worker);
    334  1.1  christos 	      Py_DECREF (py_worker);
    335  1.1  christos 	    }
    336  1.1  christos 	  Py_DECREF (iter);
    337  1.1  christos 	  /* Report any error that could have occurred while iterating.  */
    338  1.1  christos 	  if (PyErr_Occurred ())
    339  1.1  christos 	    {
    340  1.1  christos 	      gdbpy_print_stack ();
    341  1.1  christos 	      Py_DECREF (matcher);
    342  1.1  christos 	      Py_DECREF (match_result);
    343  1.1  christos 	      do_cleanups (cleanups);
    344  1.1  christos 
    345  1.1  christos 	      return EXT_LANG_RC_ERROR;
    346  1.1  christos 	    }
    347  1.1  christos 	}
    348  1.1  christos       else
    349  1.1  christos 	{
    350  1.1  christos 	  struct xmethod_worker *worker;
    351  1.1  christos 
    352  1.1  christos 	  worker = new_python_xmethod_worker (match_result, py_type);
    353  1.1  christos 	  VEC_safe_push (xmethod_worker_ptr, worker_vec, worker);
    354  1.1  christos 	}
    355  1.1  christos 
    356  1.1  christos       Py_DECREF (match_result);
    357  1.1  christos       Py_DECREF (matcher);
    358  1.1  christos     }
    359  1.1  christos   Py_DECREF (list_iter);
    360  1.1  christos   /* Report any error that could have occurred while iterating.  */
    361  1.1  christos   if (PyErr_Occurred ())
    362  1.1  christos     {
    363  1.1  christos       gdbpy_print_stack ();
    364  1.1  christos       do_cleanups (cleanups);
    365  1.1  christos 
    366  1.1  christos       return EXT_LANG_RC_ERROR;
    367  1.1  christos     }
    368  1.1  christos 
    369  1.1  christos   do_cleanups (cleanups);
    370  1.1  christos   *dm_vec = worker_vec;
    371  1.1  christos 
    372  1.1  christos   return EXT_LANG_RC_OK;
    373  1.1  christos }
    374  1.1  christos 
    375  1.1  christos /* Implementation of get_xmethod_arg_types for Python.  */
    376  1.1  christos 
    377  1.1  christos enum ext_lang_rc
    378  1.1  christos gdbpy_get_xmethod_arg_types (const struct extension_language_defn *extlang,
    379  1.1  christos 			     struct xmethod_worker *worker,
    380  1.1  christos 			     int *nargs, struct type ***arg_types)
    381  1.1  christos {
    382  1.1  christos   struct gdbpy_worker_data *worker_data = worker->data;
    383  1.1  christos   PyObject *py_worker = worker_data->worker;
    384  1.1  christos   PyObject *get_arg_types_method;
    385  1.1  christos   PyObject *py_argtype_list, *list_iter = NULL, *item;
    386  1.1  christos   struct cleanup *cleanups;
    387  1.1  christos   struct type **type_array, *obj_type;
    388  1.1  christos   int i = 1, arg_count;
    389  1.1  christos 
    390  1.1  christos   /* Set nargs to -1 so that any premature return from this function returns
    391  1.1  christos      an invalid/unusable number of arg types.  */
    392  1.1  christos   *nargs = -1;
    393  1.1  christos 
    394  1.1  christos   cleanups = ensure_python_env (get_current_arch (), current_language);
    395  1.1  christos 
    396  1.1  christos   get_arg_types_method =  PyObject_GetAttrString (py_worker,
    397  1.1  christos 						  get_arg_types_method_name);
    398  1.1  christos   if (get_arg_types_method == NULL)
    399  1.1  christos     {
    400  1.1  christos       gdbpy_print_stack ();
    401  1.1  christos       do_cleanups (cleanups);
    402  1.1  christos 
    403  1.1  christos       return EXT_LANG_RC_ERROR;
    404  1.1  christos     }
    405  1.1  christos   make_cleanup_py_decref (get_arg_types_method);
    406  1.1  christos 
    407  1.1  christos   py_argtype_list = PyObject_CallMethodObjArgs (py_worker,
    408  1.1  christos 						py_get_arg_types_method_name,
    409  1.1  christos 						NULL);
    410  1.1  christos   if (py_argtype_list == NULL)
    411  1.1  christos     {
    412  1.1  christos       gdbpy_print_stack ();
    413  1.1  christos       do_cleanups (cleanups);
    414  1.1  christos 
    415  1.1  christos       return EXT_LANG_RC_ERROR;
    416  1.1  christos     }
    417  1.1  christos   make_cleanup_py_decref (py_argtype_list);
    418  1.1  christos   if (py_argtype_list == Py_None)
    419  1.1  christos     arg_count = 0;
    420  1.1  christos   else if (PySequence_Check (py_argtype_list))
    421  1.1  christos     {
    422  1.1  christos       arg_count = PySequence_Size (py_argtype_list);
    423  1.1  christos       if (arg_count == -1)
    424  1.1  christos 	{
    425  1.1  christos 	  gdbpy_print_stack ();
    426  1.1  christos 	  do_cleanups (cleanups);
    427  1.1  christos 
    428  1.1  christos 	  return EXT_LANG_RC_ERROR;
    429  1.1  christos 	}
    430  1.1  christos 
    431  1.1  christos       list_iter = PyObject_GetIter (py_argtype_list);
    432  1.1  christos       if (list_iter == NULL)
    433  1.1  christos 	{
    434  1.1  christos 	  gdbpy_print_stack ();
    435  1.1  christos 	  do_cleanups (cleanups);
    436  1.1  christos 
    437  1.1  christos 	  return EXT_LANG_RC_ERROR;
    438  1.1  christos 	}
    439  1.1  christos       make_cleanup_py_decref (list_iter);
    440  1.1  christos     }
    441  1.1  christos   else
    442  1.1  christos     arg_count = 1;
    443  1.1  christos 
    444  1.1  christos   /* Include the 'this' argument in the size.  */
    445  1.1  christos   type_array = XCNEWVEC (struct type *, arg_count + 1);
    446  1.1  christos   i = 1;
    447  1.1  christos   if (list_iter != NULL)
    448  1.1  christos     {
    449  1.1  christos       while ((item = PyIter_Next (list_iter)) != NULL)
    450  1.1  christos 	{
    451  1.1  christos 	  struct type *arg_type = type_object_to_type (item);
    452  1.1  christos 
    453  1.1  christos 	  Py_DECREF (item);
    454  1.1  christos 	  if (arg_type == NULL)
    455  1.1  christos 	    {
    456  1.1  christos 	      PyErr_SetString (PyExc_TypeError,
    457  1.1  christos 			       _("Arg type returned by the get_arg_types "
    458  1.1  christos 				 "method of a debug method worker object is "
    459  1.1  christos 				 "not a gdb.Type object."));
    460  1.1  christos 	      break;
    461  1.1  christos 	    }
    462  1.1  christos 
    463  1.1  christos 	  type_array[i] = arg_type;
    464  1.1  christos 	  i++;
    465  1.1  christos 	}
    466  1.1  christos     }
    467  1.1  christos   else if (arg_count == 1)
    468  1.1  christos     {
    469  1.1  christos       /* py_argtype_list is not actually a list but a single gdb.Type
    470  1.1  christos 	 object.  */
    471  1.1  christos       struct type *arg_type = type_object_to_type (py_argtype_list);
    472  1.1  christos 
    473  1.1  christos       if (arg_type == NULL)
    474  1.1  christos 	{
    475  1.1  christos 	  PyErr_SetString (PyExc_TypeError,
    476  1.1  christos 			   _("Arg type returned by the get_arg_types method "
    477  1.1  christos 			     "of an xmethod worker object is not a gdb.Type "
    478  1.1  christos 			     "object."));
    479  1.1  christos 	}
    480  1.1  christos       else
    481  1.1  christos 	{
    482  1.1  christos 	  type_array[i] = arg_type;
    483  1.1  christos 	  i++;
    484  1.1  christos 	}
    485  1.1  christos     }
    486  1.1  christos   if (PyErr_Occurred ())
    487  1.1  christos     {
    488  1.1  christos       gdbpy_print_stack ();
    489  1.1  christos       do_cleanups (cleanups);
    490  1.1  christos       xfree (type_array);
    491  1.1  christos 
    492  1.1  christos       return EXT_LANG_RC_ERROR;
    493  1.1  christos     }
    494  1.1  christos 
    495  1.1  christos   /* Add the type of 'this' as the first argument.  The 'this' pointer should
    496  1.1  christos      be a 'const' value.  Hence, create a 'const' variant of the 'this' pointer
    497  1.1  christos      type.  */
    498  1.1  christos   obj_type = type_object_to_type (worker_data->this_type);
    499  1.1  christos   type_array[0] = make_cv_type (1, 0, lookup_pointer_type (obj_type), NULL);
    500  1.1  christos   *nargs = i;
    501  1.1  christos   *arg_types = type_array;
    502  1.1  christos   do_cleanups (cleanups);
    503  1.1  christos 
    504  1.1  christos   return EXT_LANG_RC_OK;
    505  1.1  christos }
    506  1.1  christos 
    507  1.1  christos /* Implementation of get_xmethod_result_type for Python.  */
    508  1.1  christos 
    509  1.1  christos enum ext_lang_rc
    510  1.1  christos gdbpy_get_xmethod_result_type (const struct extension_language_defn *extlang,
    511  1.1  christos 			       struct xmethod_worker *worker,
    512  1.1  christos 			       struct value *obj,
    513  1.1  christos 			       struct value **args, int nargs,
    514  1.1  christos 			       struct type **result_type_ptr)
    515  1.1  christos {
    516  1.1  christos   struct gdbpy_worker_data *worker_data = worker->data;
    517  1.1  christos   PyObject *py_worker = worker_data->worker;
    518  1.1  christos   PyObject *py_value_obj, *py_arg_tuple, *py_result_type;
    519  1.1  christos   PyObject *get_result_type_method;
    520  1.1  christos   struct type *obj_type, *this_type;
    521  1.1  christos   struct cleanup *cleanups;
    522  1.1  christos   int i;
    523  1.1  christos 
    524  1.1  christos   cleanups = ensure_python_env (get_current_arch (), current_language);
    525  1.1  christos 
    526  1.1  christos   /* First see if there is a get_result_type method.
    527  1.1  christos      If not this could be an old xmethod (pre 7.9.1).  */
    528  1.1  christos   get_result_type_method
    529  1.1  christos     = PyObject_GetAttrString (py_worker, get_result_type_method_name);
    530  1.1  christos   if (get_result_type_method == NULL)
    531  1.1  christos     {
    532  1.1  christos       PyErr_Clear ();
    533  1.1  christos       do_cleanups (cleanups);
    534  1.1  christos       *result_type_ptr = NULL;
    535  1.1  christos       return EXT_LANG_RC_OK;
    536  1.1  christos     }
    537  1.1  christos   make_cleanup_py_decref (get_result_type_method);
    538  1.1  christos 
    539  1.1  christos   obj_type = check_typedef (value_type (obj));
    540  1.1  christos   this_type = check_typedef (type_object_to_type (worker_data->this_type));
    541  1.1  christos   if (TYPE_CODE (obj_type) == TYPE_CODE_PTR)
    542  1.1  christos     {
    543  1.1  christos       struct type *this_ptr = lookup_pointer_type (this_type);
    544  1.1  christos 
    545  1.1  christos       if (!types_equal (obj_type, this_ptr))
    546  1.1  christos 	obj = value_cast (this_ptr, obj);
    547  1.1  christos     }
    548  1.1  christos   else if (TYPE_CODE (obj_type) == TYPE_CODE_REF)
    549  1.1  christos     {
    550  1.1  christos       struct type *this_ref = lookup_reference_type (this_type);
    551  1.1  christos 
    552  1.1  christos       if (!types_equal (obj_type, this_ref))
    553  1.1  christos 	obj = value_cast (this_ref, obj);
    554  1.1  christos     }
    555  1.1  christos   else
    556  1.1  christos     {
    557  1.1  christos       if (!types_equal (obj_type, this_type))
    558  1.1  christos 	obj = value_cast (this_type, obj);
    559  1.1  christos     }
    560  1.1  christos   py_value_obj = value_to_value_object (obj);
    561  1.1  christos   if (py_value_obj == NULL)
    562  1.1  christos     goto Fail;
    563  1.1  christos   make_cleanup_py_decref (py_value_obj);
    564  1.1  christos 
    565  1.1  christos   py_arg_tuple = PyTuple_New (nargs + 1);
    566  1.1  christos   if (py_arg_tuple == NULL)
    567  1.1  christos     goto Fail;
    568  1.1  christos   make_cleanup_py_decref (py_arg_tuple);
    569  1.1  christos 
    570  1.1  christos   /* PyTuple_SET_ITEM steals the reference of the element.  Hence INCREF the
    571  1.1  christos      reference to the 'this' object as we have a cleanup to DECREF it.  */
    572  1.1  christos   Py_INCREF (py_value_obj);
    573  1.1  christos   PyTuple_SET_ITEM (py_arg_tuple, 0, py_value_obj);
    574  1.1  christos 
    575  1.1  christos   for (i = 0; i < nargs; i++)
    576  1.1  christos     {
    577  1.1  christos       PyObject *py_value_arg = value_to_value_object (args[i]);
    578  1.1  christos 
    579  1.1  christos       if (py_value_arg == NULL)
    580  1.1  christos 	goto Fail;
    581  1.1  christos       PyTuple_SET_ITEM (py_arg_tuple, i + 1, py_value_arg);
    582  1.1  christos     }
    583  1.1  christos 
    584  1.1  christos   py_result_type = PyObject_CallObject (get_result_type_method, py_arg_tuple);
    585  1.1  christos   if (py_result_type == NULL)
    586  1.1  christos     goto Fail;
    587  1.1  christos   make_cleanup_py_decref (py_result_type);
    588  1.1  christos 
    589  1.1  christos   *result_type_ptr = type_object_to_type (py_result_type);
    590  1.1  christos   if (*result_type_ptr == NULL)
    591  1.1  christos     {
    592  1.1  christos       PyErr_SetString (PyExc_TypeError,
    593  1.1  christos 		       _("Type returned by the get_result_type method of an"
    594  1.1  christos 			 " xmethod worker object is not a gdb.Type object."));
    595  1.1  christos       goto Fail;
    596  1.1  christos     }
    597  1.1  christos 
    598  1.1  christos   do_cleanups (cleanups);
    599  1.1  christos   return EXT_LANG_RC_OK;
    600  1.1  christos 
    601  1.1  christos  Fail:
    602  1.1  christos   gdbpy_print_stack ();
    603  1.1  christos   do_cleanups (cleanups);
    604  1.1  christos   return EXT_LANG_RC_ERROR;
    605  1.1  christos }
    606  1.1  christos 
    607  1.1  christos /* Implementation of invoke_xmethod for Python.  */
    608  1.1  christos 
    609  1.1  christos struct value *
    610  1.1  christos gdbpy_invoke_xmethod (const struct extension_language_defn *extlang,
    611  1.1  christos 		      struct xmethod_worker *worker,
    612  1.1  christos 		      struct value *obj, struct value **args, int nargs)
    613  1.1  christos {
    614  1.1  christos   int i;
    615  1.1  christos   struct cleanup *cleanups;
    616  1.1  christos   PyObject *py_value_obj, *py_arg_tuple, *py_result;
    617  1.1  christos   struct type *obj_type, *this_type;
    618  1.1  christos   struct value *res = NULL;
    619  1.1  christos   struct gdbpy_worker_data *worker_data = worker->data;
    620  1.1  christos   PyObject *xmethod_worker = worker_data->worker;
    621  1.1  christos 
    622  1.1  christos   cleanups = ensure_python_env (get_current_arch (), current_language);
    623  1.1  christos 
    624  1.1  christos   obj_type = check_typedef (value_type (obj));
    625  1.1  christos   this_type = check_typedef (type_object_to_type (worker_data->this_type));
    626  1.1  christos   if (TYPE_CODE (obj_type) == TYPE_CODE_PTR)
    627  1.1  christos     {
    628  1.1  christos       struct type *this_ptr = lookup_pointer_type (this_type);
    629  1.1  christos 
    630  1.1  christos       if (!types_equal (obj_type, this_ptr))
    631  1.1  christos 	obj = value_cast (this_ptr, obj);
    632  1.1  christos     }
    633  1.1  christos   else if (TYPE_CODE (obj_type) == TYPE_CODE_REF)
    634  1.1  christos     {
    635  1.1  christos       struct type *this_ref = lookup_reference_type (this_type);
    636  1.1  christos 
    637  1.1  christos       if (!types_equal (obj_type, this_ref))
    638  1.1  christos 	obj = value_cast (this_ref, obj);
    639  1.1  christos     }
    640  1.1  christos   else
    641  1.1  christos     {
    642  1.1  christos       if (!types_equal (obj_type, this_type))
    643  1.1  christos 	obj = value_cast (this_type, obj);
    644  1.1  christos     }
    645  1.1  christos   py_value_obj = value_to_value_object (obj);
    646  1.1  christos   if (py_value_obj == NULL)
    647  1.1  christos     {
    648  1.1  christos       gdbpy_print_stack ();
    649  1.1  christos       error (_("Error while executing Python code."));
    650  1.1  christos     }
    651  1.1  christos   make_cleanup_py_decref (py_value_obj);
    652  1.1  christos 
    653  1.1  christos   py_arg_tuple = PyTuple_New (nargs + 1);
    654  1.1  christos   if (py_arg_tuple == NULL)
    655  1.1  christos     {
    656  1.1  christos       gdbpy_print_stack ();
    657  1.1  christos       error (_("Error while executing Python code."));
    658  1.1  christos     }
    659  1.1  christos   make_cleanup_py_decref (py_arg_tuple);
    660  1.1  christos 
    661  1.1  christos   /* PyTuple_SET_ITEM steals the reference of the element.  Hence INCREF the
    662  1.1  christos      reference to the 'this' object as we have a cleanup to DECREF it.  */
    663  1.1  christos   Py_INCREF (py_value_obj);
    664  1.1  christos   PyTuple_SET_ITEM (py_arg_tuple, 0, py_value_obj);
    665  1.1  christos 
    666  1.1  christos   for (i = 0; i < nargs; i++)
    667  1.1  christos     {
    668  1.1  christos       PyObject *py_value_arg = value_to_value_object (args[i]);
    669  1.1  christos 
    670  1.1  christos       if (py_value_arg == NULL)
    671  1.1  christos 	{
    672  1.1  christos 	  gdbpy_print_stack ();
    673  1.1  christos 	  error (_("Error while executing Python code."));
    674  1.1  christos 	}
    675  1.1  christos 
    676  1.1  christos       PyTuple_SET_ITEM (py_arg_tuple, i + 1, py_value_arg);
    677  1.1  christos     }
    678  1.1  christos 
    679  1.1  christos   py_result = PyObject_CallObject (xmethod_worker, py_arg_tuple);
    680  1.1  christos   if (py_result == NULL)
    681  1.1  christos     {
    682  1.1  christos       gdbpy_print_stack ();
    683  1.1  christos       error (_("Error while executing Python code."));
    684  1.1  christos     }
    685  1.1  christos   make_cleanup_py_decref (py_result);
    686  1.1  christos 
    687  1.1  christos   if (py_result != Py_None)
    688  1.1  christos     {
    689  1.1  christos       res = convert_value_from_python (py_result);
    690  1.1  christos       if (res == NULL)
    691  1.1  christos 	{
    692  1.1  christos 	  gdbpy_print_stack ();
    693  1.1  christos 	  error (_("Error while executing Python code."));
    694  1.1  christos 	}
    695  1.1  christos     }
    696  1.1  christos   else
    697  1.1  christos     {
    698  1.1  christos       res = allocate_value (lookup_typename (python_language, python_gdbarch,
    699  1.1  christos 					     "void", NULL, 0));
    700  1.1  christos     }
    701  1.1  christos 
    702  1.1  christos   do_cleanups (cleanups);
    703  1.1  christos 
    704  1.1  christos   return res;
    705  1.1  christos }
    706  1.1  christos 
    707  1.1  christos /* Creates a new Python xmethod_worker object.
    708  1.1  christos    The new object has data of type 'struct gdbpy_worker_data' composed
    709  1.1  christos    with the components PY_WORKER and THIS_TYPE.  */
    710  1.1  christos 
    711  1.1  christos static struct xmethod_worker *
    712  1.1  christos new_python_xmethod_worker (PyObject *py_worker, PyObject *this_type)
    713  1.1  christos {
    714  1.1  christos   struct gdbpy_worker_data *data;
    715  1.1  christos 
    716  1.1  christos   gdb_assert (py_worker != NULL && this_type != NULL);
    717  1.1  christos 
    718  1.1  christos   data = XCNEW (struct gdbpy_worker_data);
    719  1.1  christos   data->worker = py_worker;
    720  1.1  christos   data->this_type = this_type;
    721  1.1  christos   Py_INCREF (py_worker);
    722  1.1  christos   Py_INCREF (this_type);
    723  1.1  christos 
    724  1.1  christos   return new_xmethod_worker (&extension_language_python, data);
    725  1.1  christos }
    726  1.1  christos 
    727  1.1  christos int
    728  1.1  christos gdbpy_initialize_xmethods (void)
    729  1.1  christos {
    730  1.1  christos   py_match_method_name = PyString_FromString (match_method_name);
    731  1.1  christos   if (py_match_method_name == NULL)
    732  1.1  christos     return -1;
    733  1.1  christos 
    734  1.1  christos   py_invoke_method_name = PyString_FromString (invoke_method_name);
    735  1.1  christos   if (py_invoke_method_name == NULL)
    736  1.1  christos     return -1;
    737  1.1  christos 
    738  1.1  christos   py_get_arg_types_method_name
    739  1.1  christos     = PyString_FromString (get_arg_types_method_name);
    740  1.1  christos   if (py_get_arg_types_method_name == NULL)
    741  1.1  christos     return -1;
    742  1.1  christos 
    743  1.1  christos   py_get_result_type_method_name
    744  1.1  christos     = PyString_FromString (get_result_type_method_name);
    745  1.1  christos   if (py_get_result_type_method_name == NULL)
    746  1.1  christos     return -1;
    747  1.1  christos 
    748  1.1  christos   return 1;
    749  1.1  christos }
    750