gdb/python/py-xmethods.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Support for debug methods in Python.

  2.    Copyright (C) 2013-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "arch-utils.h"
  16. #include "extension-priv.h"
  17. #include "objfiles.h"
  18. #include "value.h"
  19. #include "language.h"

  20. #include "python.h"
  21. #include "python-internal.h"

  22. static const char enabled_field_name[] = "enabled";
  23. static const char match_method_name[] = "match";
  24. static const char get_arg_types_method_name[] = "get_arg_types";
  25. static const char invoke_method_name[] = "invoke";
  26. static const char matchers_attr_str[] = "xmethods";

  27. static PyObject *py_match_method_name = NULL;
  28. static PyObject *py_get_arg_types_method_name = NULL;
  29. static PyObject *py_invoke_method_name = NULL;

  30. struct gdbpy_worker_data
  31. {
  32.   PyObject *worker;
  33.   PyObject *this_type;
  34. };

  35. static struct xmethod_worker *new_python_xmethod_worker (PyObject *item,
  36.                                                          PyObject *py_obj_type);

  37. /* Implementation of free_xmethod_worker_data for Python.  */

  38. void
  39. gdbpy_free_xmethod_worker_data (const struct extension_language_defn *extlang,
  40.                                 void *data)
  41. {
  42.   struct gdbpy_worker_data *worker_data = data;
  43.   struct cleanup *cleanups;

  44.   gdb_assert (worker_data->worker != NULL && worker_data->this_type != NULL);

  45.   /* We don't do much here, but we still need the GIL.  */
  46.   cleanups = ensure_python_env (get_current_arch (), current_language);

  47.   Py_DECREF (worker_data->worker);
  48.   Py_DECREF (worker_data->this_type);
  49.   xfree (worker_data);

  50.   do_cleanups (cleanups);
  51. }

  52. /* Implementation of clone_xmethod_worker_data for Python.  */

  53. void *
  54. gdbpy_clone_xmethod_worker_data (const struct extension_language_defn *extlang,
  55.                                  void *data)
  56. {
  57.   struct gdbpy_worker_data *worker_data = data, *new_data;
  58.   struct cleanup *cleanups;

  59.   gdb_assert (worker_data->worker != NULL && worker_data->this_type != NULL);

  60.   /* We don't do much here, but we still need the GIL.  */
  61.   cleanups = ensure_python_env (get_current_arch (), current_language);

  62.   new_data = XCNEW (struct gdbpy_worker_data);
  63.   new_data->worker = worker_data->worker;
  64.   new_data->this_type = worker_data->this_type;
  65.   Py_INCREF (new_data->worker);
  66.   Py_INCREF (new_data->this_type);

  67.   do_cleanups (cleanups);

  68.   return new_data;
  69. }

  70. /* Invoke the "match" method of the MATCHER and return a new reference
  71.    to the result.  Returns NULL on error.  */

  72. static PyObject *
  73. invoke_match_method (PyObject *matcher, PyObject *py_obj_type,
  74.                      const char *xmethod_name)
  75. {
  76.   PyObject *py_xmethod_name;
  77.   PyObject *match_method, *enabled_field, *match_result;
  78.   struct cleanup *cleanups;
  79.   int enabled;

  80.   cleanups = make_cleanup (null_cleanup, NULL);

  81.   enabled_field = PyObject_GetAttrString (matcher, enabled_field_name);
  82.   if (enabled_field == NULL)
  83.     {
  84.       do_cleanups (cleanups);
  85.       return NULL;
  86.     }
  87.   make_cleanup_py_decref (enabled_field);

  88.   enabled = PyObject_IsTrue (enabled_field);
  89.   if (enabled == -1)
  90.     {
  91.       do_cleanups (cleanups);
  92.       return NULL;
  93.     }
  94.   if (enabled == 0)
  95.     {
  96.       /* Return 'None' if the matcher is not enabled.  */
  97.       do_cleanups (cleanups);
  98.       Py_RETURN_NONE;
  99.     }

  100.   match_method = PyObject_GetAttrString (matcher, match_method_name);
  101.   if (match_method == NULL)
  102.     {
  103.       do_cleanups (cleanups);
  104.       return NULL;
  105.     }
  106.   make_cleanup_py_decref (match_method);

  107.   py_xmethod_name = PyString_FromString (xmethod_name);
  108.   if (py_xmethod_name == NULL)
  109.     {
  110.       do_cleanups (cleanups);
  111.       return NULL;
  112.     }
  113.   make_cleanup_py_decref (py_xmethod_name);

  114.   match_result = PyObject_CallMethodObjArgs (matcher,
  115.                                              py_match_method_name,
  116.                                              py_obj_type,
  117.                                              py_xmethod_name,
  118.                                              NULL);

  119.   do_cleanups (cleanups);

  120.   return match_result;
  121. }

  122. /* Implementation of get_matching_xmethod_workers for Python.  */

  123. enum ext_lang_rc
  124. gdbpy_get_matching_xmethod_workers
  125.   (const struct extension_language_defn *extlang,
  126.    struct type *obj_type, const char *method_name,
  127.    xmethod_worker_vec **dm_vec)
  128. {
  129.   struct cleanup *cleanups;
  130.   struct objfile *objfile;
  131.   VEC (xmethod_worker_ptr) *worker_vec = NULL;
  132.   PyObject *py_type, *py_progspace;
  133.   PyObject *py_xmethod_matcher_list = NULL, *list_iter, *matcher;

  134.   gdb_assert (obj_type != NULL && method_name != NULL);

  135.   cleanups = ensure_python_env (get_current_arch (), current_language);

  136.   py_type = type_to_type_object (obj_type);
  137.   if (py_type == NULL)
  138.     {
  139.       gdbpy_print_stack ();
  140.       do_cleanups (cleanups);

  141.       return EXT_LANG_RC_ERROR;
  142.     }
  143.   make_cleanup_py_decref (py_type);

  144.   /* Create an empty list of debug methods.  */
  145.   py_xmethod_matcher_list = PyList_New (0);
  146.   if (py_xmethod_matcher_list == NULL)
  147.     {
  148.       gdbpy_print_stack ();
  149.       do_cleanups (cleanups);

  150.       return EXT_LANG_RC_ERROR;
  151.     }

  152.   /* Gather debug method matchers registered with the object files.
  153.      This could be done differently by iterating over each objfile's matcher
  154.      list individually, but there's no data yet to show it's needed.  */
  155.   ALL_OBJFILES (objfile)
  156.     {
  157.       PyObject *py_objfile = objfile_to_objfile_object (objfile);
  158.       PyObject *objfile_matchers, *temp = py_xmethod_matcher_list;

  159.       if (py_objfile == NULL)
  160.         {
  161.           gdbpy_print_stack ();
  162.           Py_DECREF (py_xmethod_matcher_list);
  163.           do_cleanups (cleanups);

  164.           return EXT_LANG_RC_ERROR;
  165.         }

  166.       objfile_matchers = objfpy_get_xmethods (py_objfile, NULL);
  167.       py_xmethod_matcher_list = PySequence_Concat (temp, objfile_matchers);
  168.       Py_DECREF (temp);
  169.       Py_DECREF (objfile_matchers);
  170.       if (py_xmethod_matcher_list == NULL)
  171.         {
  172.           gdbpy_print_stack ();
  173.           do_cleanups (cleanups);

  174.           return EXT_LANG_RC_ERROR;
  175.         }
  176.     }

  177.   /* Gather debug methods matchers registered with the current program
  178.      space.  */
  179.   py_progspace = pspace_to_pspace_object (current_program_space);
  180.   if (py_progspace != NULL)
  181.     {
  182.       PyObject *temp = py_xmethod_matcher_list;
  183.       PyObject *pspace_matchers = pspy_get_xmethods (py_progspace, NULL);

  184.       py_xmethod_matcher_list = PySequence_Concat (temp, pspace_matchers);
  185.       Py_DECREF (temp);
  186.       Py_DECREF (pspace_matchers);
  187.       if (py_xmethod_matcher_list == NULL)
  188.         {
  189.           gdbpy_print_stack ();
  190.           do_cleanups (cleanups);

  191.           return EXT_LANG_RC_ERROR;
  192.         }
  193.     }
  194.   else
  195.     {
  196.       gdbpy_print_stack ();
  197.       Py_DECREF (py_xmethod_matcher_list);
  198.       do_cleanups (cleanups);

  199.       return EXT_LANG_RC_ERROR;
  200.     }

  201.   /* Gather debug method matchers registered globally.  */
  202.   if (gdb_python_module != NULL
  203.       && PyObject_HasAttrString (gdb_python_module, matchers_attr_str))
  204.     {
  205.       PyObject *gdb_matchers;
  206.       PyObject *temp = py_xmethod_matcher_list;

  207.       gdb_matchers = PyObject_GetAttrString (gdb_python_module,
  208.                                              matchers_attr_str);
  209.       if (gdb_matchers != NULL)
  210.         {
  211.           py_xmethod_matcher_list = PySequence_Concat (temp, gdb_matchers);
  212.           Py_DECREF (temp);
  213.           Py_DECREF (gdb_matchers);
  214.           if (py_xmethod_matcher_list == NULL)
  215.             {
  216.               gdbpy_print_stack ();
  217.               do_cleanups (cleanups);

  218.               return EXT_LANG_RC_ERROR;
  219.             }
  220.         }
  221.       else
  222.         {
  223.           gdbpy_print_stack ();
  224.           Py_DECREF (py_xmethod_matcher_list);
  225.           do_cleanups (cleanups);

  226.           return EXT_LANG_RC_ERROR;
  227.         }
  228.     }

  229.   /* Safe to make a cleanup for py_xmethod_matcher_list now as it
  230.      will not change any more.  */
  231.   make_cleanup_py_decref (py_xmethod_matcher_list);

  232.   list_iter = PyObject_GetIter (py_xmethod_matcher_list);
  233.   if (list_iter == NULL)
  234.     {
  235.       gdbpy_print_stack ();
  236.       do_cleanups (cleanups);

  237.       return EXT_LANG_RC_ERROR;
  238.     }
  239.   while ((matcher = PyIter_Next (list_iter)) != NULL)
  240.     {
  241.       PyObject *match_result = invoke_match_method (matcher, py_type,
  242.                                                     method_name);

  243.       if (match_result == NULL)
  244.         {
  245.           gdbpy_print_stack ();
  246.           Py_DECREF (matcher);
  247.           do_cleanups (cleanups);

  248.           return EXT_LANG_RC_ERROR;
  249.         }
  250.       if (match_result == Py_None)
  251.         ; /* This means there was no match.  */
  252.       else if (PySequence_Check (match_result))
  253.         {
  254.           PyObject *iter = PyObject_GetIter (match_result);
  255.           PyObject *py_worker;

  256.           if (iter == NULL)
  257.             {
  258.               gdbpy_print_stack ();
  259.               Py_DECREF (matcher);
  260.               Py_DECREF (match_result);
  261.               do_cleanups (cleanups);

  262.               return EXT_LANG_RC_ERROR;
  263.             }
  264.           while ((py_worker = PyIter_Next (iter)) != NULL)
  265.             {
  266.               struct xmethod_worker *worker;

  267.               worker = new_python_xmethod_worker (py_worker, py_type);
  268.               VEC_safe_push (xmethod_worker_ptr, worker_vec, worker);
  269.               Py_DECREF (py_worker);
  270.             }
  271.           Py_DECREF (iter);
  272.           /* Report any error that could have occurred while iterating.  */
  273.           if (PyErr_Occurred ())
  274.             {
  275.               gdbpy_print_stack ();
  276.               Py_DECREF (matcher);
  277.               Py_DECREF (match_result);
  278.               do_cleanups (cleanups);

  279.               return EXT_LANG_RC_ERROR;
  280.             }
  281.         }
  282.       else
  283.         {
  284.           struct xmethod_worker *worker;

  285.           worker = new_python_xmethod_worker (match_result, py_type);
  286.           VEC_safe_push (xmethod_worker_ptr, worker_vec, worker);
  287.         }

  288.       Py_DECREF (match_result);
  289.       Py_DECREF (matcher);
  290.     }
  291.   Py_DECREF (list_iter);
  292.   /* Report any error that could have occurred while iterating.  */
  293.   if (PyErr_Occurred ())
  294.     {
  295.       gdbpy_print_stack ();
  296.       do_cleanups (cleanups);

  297.       return EXT_LANG_RC_ERROR;
  298.     }

  299.   do_cleanups (cleanups);
  300.   *dm_vec = worker_vec;

  301.   return EXT_LANG_RC_OK;
  302. }

  303. /* Implementation of get_xmethod_arg_types for Python.  */

  304. enum ext_lang_rc
  305. gdbpy_get_xmethod_arg_types (const struct extension_language_defn *extlang,
  306.                              struct xmethod_worker *worker,
  307.                              int *nargs, struct type ***arg_types)
  308. {
  309.   struct gdbpy_worker_data *worker_data = worker->data;
  310.   PyObject *py_worker = worker_data->worker;
  311.   PyObject *get_arg_types_method;
  312.   PyObject *py_argtype_list, *list_iter = NULL, *item;
  313.   struct cleanup *cleanups;
  314.   struct type **type_array, *obj_type;
  315.   int i = 1, arg_count;

  316.   /* Set nargs to -1 so that any premature return from this function returns
  317.      an invalid/unusable number of arg types.  */
  318.   *nargs = -1;

  319.   cleanups = ensure_python_env (get_current_arch (), current_language);

  320.   get_arg_types_method =  PyObject_GetAttrString (py_worker,
  321.                                                   get_arg_types_method_name);
  322.   if (get_arg_types_method == NULL)
  323.     {
  324.       gdbpy_print_stack ();
  325.       do_cleanups (cleanups);

  326.       return EXT_LANG_RC_ERROR;
  327.     }
  328.   make_cleanup_py_decref (get_arg_types_method);

  329.   py_argtype_list = PyObject_CallMethodObjArgs (py_worker,
  330.                                                 py_get_arg_types_method_name,
  331.                                                 NULL);
  332.   if (py_argtype_list == NULL)
  333.     {
  334.       gdbpy_print_stack ();
  335.       do_cleanups (cleanups);

  336.       return EXT_LANG_RC_ERROR;
  337.     }
  338.   make_cleanup_py_decref (py_argtype_list);
  339.   if (py_argtype_list == Py_None)
  340.     arg_count = 0;
  341.   else if (PySequence_Check (py_argtype_list))
  342.     {
  343.       arg_count = PySequence_Size (py_argtype_list);
  344.       if (arg_count == -1)
  345.         {
  346.           gdbpy_print_stack ();
  347.           do_cleanups (cleanups);

  348.           return EXT_LANG_RC_ERROR;
  349.         }

  350.       list_iter = PyObject_GetIter (py_argtype_list);
  351.       if (list_iter == NULL)
  352.         {
  353.           gdbpy_print_stack ();
  354.           do_cleanups (cleanups);

  355.           return EXT_LANG_RC_ERROR;
  356.         }
  357.       make_cleanup_py_decref (list_iter);
  358.     }
  359.   else
  360.     arg_count = 1;

  361.   /* Include the 'this' argument in the size.  */
  362.   type_array = XCNEWVEC (struct type *, arg_count + 1);
  363.   i = 1;
  364.   if (list_iter != NULL)
  365.     {
  366.       while ((item = PyIter_Next (list_iter)) != NULL)
  367.         {
  368.           struct type *arg_type = type_object_to_type (item);

  369.           Py_DECREF (item);
  370.           if (arg_type == NULL)
  371.             {
  372.               PyErr_SetString (PyExc_TypeError,
  373.                                _("Arg type returned by the get_arg_types "
  374.                                  "method of a debug method worker object is "
  375.                                  "not a gdb.Type object."));
  376.               break;
  377.             }

  378.           type_array[i] = arg_type;
  379.           i++;
  380.         }
  381.     }
  382.   else if (arg_count == 1)
  383.     {
  384.       /* py_argtype_list is not actually a list but a single gdb.Type
  385.          object.  */
  386.       struct type *arg_type = type_object_to_type (py_argtype_list);

  387.       if (arg_type == NULL)
  388.         {
  389.           PyErr_SetString (PyExc_TypeError,
  390.                            _("Arg type returned by the get_arg_types method "
  391.                              "of an xmethod worker object is not a gdb.Type "
  392.                              "object."));
  393.         }
  394.       else
  395.         {
  396.           type_array[i] = arg_type;
  397.           i++;
  398.         }
  399.     }
  400.   if (PyErr_Occurred ())
  401.     {
  402.       gdbpy_print_stack ();
  403.       do_cleanups (cleanups);
  404.       xfree (type_array);

  405.       return EXT_LANG_RC_ERROR;
  406.     }

  407.   /* Add the type of 'this' as the first argument.  The 'this' pointer should
  408.      be a 'const' value.  Hence, create a 'const' variant of the 'this' pointer
  409.      type.  */
  410.   obj_type = type_object_to_type (worker_data->this_type);
  411.   type_array[0] = make_cv_type (1, 0, lookup_pointer_type (obj_type), NULL);
  412.   *nargs = i;
  413.   *arg_types = type_array;
  414.   do_cleanups (cleanups);

  415.   return EXT_LANG_RC_OK;
  416. }

  417. /* Implementation of invoke_xmethod for Python.  */

  418. struct value *
  419. gdbpy_invoke_xmethod (const struct extension_language_defn *extlang,
  420.                       struct xmethod_worker *worker,
  421.                       struct value *obj, struct value **args, int nargs)
  422. {
  423.   int i;
  424.   struct cleanup *cleanups;
  425.   PyObject *py_value_obj, *py_arg_tuple, *py_result;
  426.   struct type *obj_type, *this_type;
  427.   struct value *res = NULL;
  428.   struct gdbpy_worker_data *worker_data = worker->data;
  429.   PyObject *xmethod_worker = worker_data->worker;

  430.   cleanups = ensure_python_env (get_current_arch (), current_language);

  431.   obj_type = check_typedef (value_type (obj));
  432.   this_type = check_typedef (type_object_to_type (worker_data->this_type));
  433.   if (TYPE_CODE (obj_type) == TYPE_CODE_PTR)
  434.     {
  435.       struct type *this_ptr = lookup_pointer_type (this_type);

  436.       if (!types_equal (obj_type, this_ptr))
  437.         obj = value_cast (this_ptr, obj);
  438.     }
  439.   else if (TYPE_CODE (obj_type) == TYPE_CODE_REF)
  440.     {
  441.       struct type *this_ref = lookup_reference_type (this_type);

  442.       if (!types_equal (obj_type, this_ref))
  443.         obj = value_cast (this_ref, obj);
  444.     }
  445.   else
  446.     {
  447.       if (!types_equal (obj_type, this_type))
  448.         obj = value_cast (this_type, obj);
  449.     }
  450.   py_value_obj = value_to_value_object (obj);
  451.   if (py_value_obj == NULL)
  452.     {
  453.       gdbpy_print_stack ();
  454.       error (_("Error while executing Python code."));
  455.     }
  456.   make_cleanup_py_decref (py_value_obj);

  457.   py_arg_tuple = PyTuple_New (nargs + 1);
  458.   if (py_arg_tuple == NULL)
  459.     {
  460.       gdbpy_print_stack ();
  461.       error (_("Error while executing Python code."));
  462.     }
  463.   make_cleanup_py_decref (py_arg_tuple);

  464.   /* PyTuple_SET_ITEM steals the reference of the element.  Hence INCREF the
  465.      reference to the 'this' object as we have a cleanup to DECREF it.  */
  466.   Py_INCREF (py_value_obj);
  467.   PyTuple_SET_ITEM (py_arg_tuple, 0, py_value_obj);

  468.   for (i = 0; i < nargs; i++)
  469.     {
  470.       PyObject *py_value_arg = value_to_value_object (args[i]);

  471.       if (py_value_arg == NULL)
  472.         {
  473.           gdbpy_print_stack ();
  474.           error (_("Error while executing Python code."));
  475.         }

  476.       PyTuple_SET_ITEM (py_arg_tuple, i + 1, py_value_arg);
  477.     }

  478.   py_result = PyObject_CallObject (xmethod_worker, py_arg_tuple);
  479.   if (py_result == NULL)
  480.     {
  481.       gdbpy_print_stack ();
  482.       error (_("Error while executing Python code."));
  483.     }
  484.   make_cleanup_py_decref (py_result);

  485.   if (py_result != Py_None)
  486.     {
  487.       res = convert_value_from_python (py_result);
  488.       if (res == NULL)
  489.         {
  490.           gdbpy_print_stack ();
  491.           error (_("Error while executing Python code."));
  492.         }
  493.     }
  494.   else
  495.     {
  496.       res = allocate_value (lookup_typename (python_language, python_gdbarch,
  497.                                              "void", NULL, 0));
  498.     }

  499.   do_cleanups (cleanups);

  500.   return res;
  501. }

  502. /* Creates a new Python xmethod_worker object.
  503.    The new object has data of type 'struct gdbpy_worker_data' composed
  504.    with the components PY_WORKER and THIS_TYPE.  */

  505. static struct xmethod_worker *
  506. new_python_xmethod_worker (PyObject *py_worker, PyObject *this_type)
  507. {
  508.   struct gdbpy_worker_data *data;

  509.   gdb_assert (py_worker != NULL && this_type != NULL);

  510.   data = XCNEW (struct gdbpy_worker_data);
  511.   data->worker = py_worker;
  512.   data->this_type = this_type;
  513.   Py_INCREF (py_worker);
  514.   Py_INCREF (this_type);

  515.   return new_xmethod_worker (&extension_language_python, data);
  516. }

  517. int
  518. gdbpy_initialize_xmethods (void)
  519. {
  520.   py_match_method_name = PyString_FromString (match_method_name);
  521.   if (py_match_method_name == NULL)
  522.     return -1;

  523.   py_invoke_method_name = PyString_FromString (invoke_method_name);
  524.   if (py_invoke_method_name == NULL)
  525.     return -1;

  526.   py_get_arg_types_method_name
  527.     = PyString_FromString (get_arg_types_method_name);
  528.   if (py_get_arg_types_method_name == NULL)
  529.     return -1;

  530.   return 1;
  531. }