gdb/python/py-function.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Convenience functions implemented in Python.

  2.    Copyright (C) 2008-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 "value.h"
  16. #include "python-internal.h"
  17. #include "charset.h"
  18. #include "gdbcmd.h"
  19. #include "cli/cli-decode.h"
  20. #include "completer.h"
  21. #include "expression.h"
  22. #include "language.h"

  23. static PyTypeObject fnpy_object_type
  24.     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("PyObject");



  25. static PyObject *
  26. convert_values_to_python (int argc, struct value **argv)
  27. {
  28.   int i;
  29.   PyObject *result = PyTuple_New (argc);

  30.   if (! result)
  31.     return NULL;

  32.   for (i = 0; i < argc; ++i)
  33.     {
  34.       PyObject *elt = value_to_value_object (argv[i]);
  35.       if (! elt)
  36.         {
  37.           Py_DECREF (result);
  38.           return NULL;
  39.         }
  40.       PyTuple_SetItem (result, i, elt);
  41.     }
  42.   return result;
  43. }

  44. /* Call a Python function object's invoke method.  */

  45. static struct value *
  46. fnpy_call (struct gdbarch *gdbarch, const struct language_defn *language,
  47.            void *cookie, int argc, struct value **argv)
  48. {
  49.   struct value *value = NULL;
  50.   /* 'result' must be set to NULL, this initially indicates whether
  51.      the function was called, or not.  */
  52.   PyObject *result = NULL;
  53.   PyObject *callable, *args;
  54.   struct cleanup *cleanup;

  55.   cleanup = ensure_python_env (gdbarch, language);

  56.   args = convert_values_to_python (argc, argv);
  57.   /* convert_values_to_python can return NULL on error.  If we
  58.      encounter this, do not call the function, but allow the Python ->
  59.      error code conversion below to deal with the Python exception.
  60.      Note, that this is different if the function simply does not
  61.      have arguments.  */

  62.   if (args)
  63.     {
  64.       callable = PyObject_GetAttrString ((PyObject *) cookie, "invoke");
  65.       if (! callable)
  66.         {
  67.           Py_DECREF (args);
  68.           error (_("No method named 'invoke' in object."));
  69.         }

  70.       result = PyObject_Call (callable, args, NULL);
  71.       Py_DECREF (callable);
  72.       Py_DECREF (args);
  73.     }

  74.   if (!result)
  75.     {
  76.       PyObject *ptype, *pvalue, *ptraceback;
  77.       char *msg;

  78.       PyErr_Fetch (&ptype, &pvalue, &ptraceback);

  79.       /* Try to fetch an error message contained within ptype, pvalue.
  80.          When fetching the error message we need to make our own copy,
  81.          we no longer own ptype, pvalue after the call to PyErr_Restore.  */

  82.       msg = gdbpy_exception_to_string (ptype, pvalue);
  83.       make_cleanup (xfree, msg);

  84.       if (msg == NULL)
  85.         {
  86.           /* An error occurred computing the string representation of the
  87.              error message.  This is rare, but we should inform the user.  */

  88.           printf_filtered (_("An error occurred in a Python "
  89.                              "convenience function\n"
  90.                              "and then another occurred computing the "
  91.                              "error message.\n"));
  92.           gdbpy_print_stack ();
  93.         }

  94.       /* Don't print the stack for gdb.GdbError exceptions.
  95.          It is generally used to flag user errors.

  96.          We also don't want to print "Error occurred in Python command"
  97.          for user errors.  However, a missing message for gdb.GdbError
  98.          exceptions is arguably a bug, so we flag it as such.  */

  99.       if (!PyErr_GivenExceptionMatches (ptype, gdbpy_gdberror_exc)
  100.           || msg == NULL || *msg == '\0')
  101.         {
  102.           PyErr_Restore (ptype, pvalue, ptraceback);
  103.           gdbpy_print_stack ();
  104.           if (msg != NULL && *msg != '\0')
  105.             error (_("Error occurred in Python convenience function: %s"),
  106.                    msg);
  107.           else
  108.             error (_("Error occurred in Python convenience function."));
  109.         }
  110.       else
  111.         {
  112.           Py_XDECREF (ptype);
  113.           Py_XDECREF (pvalue);
  114.           Py_XDECREF (ptraceback);
  115.           error ("%s", msg);
  116.         }
  117.     }

  118.   value = convert_value_from_python (result);
  119.   if (value == NULL)
  120.     {
  121.       Py_DECREF (result);
  122.       gdbpy_print_stack ();
  123.       error (_("Error while executing Python code."));
  124.     }

  125.   Py_DECREF (result);
  126.   do_cleanups (cleanup);

  127.   return value;
  128. }

  129. /* Initializer for a Function object.  It takes one argument, the name
  130.    of the function.  */

  131. static int
  132. fnpy_init (PyObject *self, PyObject *args, PyObject *kwds)
  133. {
  134.   const char *name;
  135.   char *docstring = NULL;

  136.   if (! PyArg_ParseTuple (args, "s", &name))
  137.     return -1;
  138.   Py_INCREF (self);

  139.   if (PyObject_HasAttrString (self, "__doc__"))
  140.     {
  141.       PyObject *ds_obj = PyObject_GetAttrString (self, "__doc__");
  142.       if (ds_obj != NULL)
  143.         {
  144.           if (gdbpy_is_string (ds_obj))
  145.             {
  146.               docstring = python_string_to_host_string (ds_obj);
  147.               if (docstring == NULL)
  148.                 {
  149.                   Py_DECREF (self);
  150.                   Py_DECREF (ds_obj);
  151.                   return -1;
  152.                 }
  153.             }

  154.           Py_DECREF (ds_obj);
  155.         }
  156.     }
  157.   if (! docstring)
  158.     docstring = xstrdup (_("This function is not documented."));

  159.   add_internal_function (name, docstring, fnpy_call, self);
  160.   return 0;
  161. }

  162. /* Initialize internal function support.  */

  163. int
  164. gdbpy_initialize_functions (void)
  165. {
  166.   fnpy_object_type.tp_new = PyType_GenericNew;
  167.   if (PyType_Ready (&fnpy_object_type) < 0)
  168.     return -1;

  169.   return gdb_pymodule_addobject (gdb_module, "Function",
  170.                                  (PyObject *) &fnpy_object_type);
  171. }



  172. static PyTypeObject fnpy_object_type =
  173. {
  174.   PyVarObject_HEAD_INIT (NULL, 0)
  175.   "gdb.Function",                  /*tp_name*/
  176.   sizeof (PyObject),                  /*tp_basicsize*/
  177.   0,                                  /*tp_itemsize*/
  178.   0,                                  /*tp_dealloc*/
  179.   0,                                  /*tp_print*/
  180.   0,                                  /*tp_getattr*/
  181.   0,                                  /*tp_setattr*/
  182.   0,                                  /*tp_compare*/
  183.   0,                                  /*tp_repr*/
  184.   0,                                  /*tp_as_number*/
  185.   0,                                  /*tp_as_sequence*/
  186.   0,                                  /*tp_as_mapping*/
  187.   0,                                  /*tp_hash */
  188.   0,                                  /*tp_call*/
  189.   0,                                  /*tp_str*/
  190.   0,                                  /*tp_getattro*/
  191.   0,                                  /*tp_setattro*/
  192.   0,                                  /*tp_as_buffer*/
  193.   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
  194.   "GDB function object",          /* tp_doc */
  195.   0,                                  /* tp_traverse */
  196.   0,                                  /* tp_clear */
  197.   0,                                  /* tp_richcompare */
  198.   0,                                  /* tp_weaklistoffset */
  199.   0,                                  /* tp_iter */
  200.   0,                                  /* tp_iternext */
  201.   0,                                  /* tp_methods */
  202.   0,                                  /* tp_members */
  203.   0,                                  /* tp_getset */
  204.   0,                                  /* tp_base */
  205.   0,                                  /* tp_dict */
  206.   0,                                  /* tp_descr_get */
  207.   0,                                  /* tp_descr_set */
  208.   0,                                  /* tp_dictoffset */
  209.   fnpy_init,                          /* tp_init */
  210.   0,                                  /* tp_alloc */
  211. };