GDB (API)
|
00001 /* Convenience functions implemented in Python. 00002 00003 Copyright (C) 2008-2013 Free Software Foundation, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 00021 #include "defs.h" 00022 #include "value.h" 00023 #include "exceptions.h" 00024 #include "python-internal.h" 00025 #include "charset.h" 00026 #include "gdbcmd.h" 00027 #include "cli/cli-decode.h" 00028 #include "completer.h" 00029 #include "expression.h" 00030 #include "language.h" 00031 00032 static PyTypeObject fnpy_object_type 00033 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("PyObject"); 00034 00035 00036 00037 static PyObject * 00038 convert_values_to_python (int argc, struct value **argv) 00039 { 00040 int i; 00041 PyObject *result = PyTuple_New (argc); 00042 00043 if (! result) 00044 return NULL; 00045 00046 for (i = 0; i < argc; ++i) 00047 { 00048 PyObject *elt = value_to_value_object (argv[i]); 00049 if (! elt) 00050 { 00051 Py_DECREF (result); 00052 return NULL; 00053 } 00054 PyTuple_SetItem (result, i, elt); 00055 } 00056 return result; 00057 } 00058 00059 /* Call a Python function object's invoke method. */ 00060 00061 static struct value * 00062 fnpy_call (struct gdbarch *gdbarch, const struct language_defn *language, 00063 void *cookie, int argc, struct value **argv) 00064 { 00065 struct value *value = NULL; 00066 /* 'result' must be set to NULL, this initially indicates whether 00067 the function was called, or not. */ 00068 PyObject *result = NULL; 00069 PyObject *callable, *args; 00070 struct cleanup *cleanup; 00071 00072 cleanup = ensure_python_env (gdbarch, language); 00073 00074 args = convert_values_to_python (argc, argv); 00075 /* convert_values_to_python can return NULL on error. If we 00076 encounter this, do not call the function, but allow the Python -> 00077 error code conversion below to deal with the Python exception. 00078 Note, that this is different if the function simply does not 00079 have arguments. */ 00080 00081 if (args) 00082 { 00083 callable = PyObject_GetAttrString ((PyObject *) cookie, "invoke"); 00084 if (! callable) 00085 { 00086 Py_DECREF (args); 00087 error (_("No method named 'invoke' in object.")); 00088 } 00089 00090 result = PyObject_Call (callable, args, NULL); 00091 Py_DECREF (callable); 00092 Py_DECREF (args); 00093 } 00094 00095 if (!result) 00096 { 00097 PyObject *ptype, *pvalue, *ptraceback; 00098 char *msg; 00099 00100 PyErr_Fetch (&ptype, &pvalue, &ptraceback); 00101 00102 /* Try to fetch an error message contained within ptype, pvalue. 00103 When fetching the error message we need to make our own copy, 00104 we no longer own ptype, pvalue after the call to PyErr_Restore. */ 00105 00106 msg = gdbpy_exception_to_string (ptype, pvalue); 00107 make_cleanup (xfree, msg); 00108 00109 if (msg == NULL) 00110 { 00111 /* An error occurred computing the string representation of the 00112 error message. This is rare, but we should inform the user. */ 00113 00114 printf_filtered (_("An error occurred in a Python " 00115 "convenience function\n" 00116 "and then another occurred computing the " 00117 "error message.\n")); 00118 gdbpy_print_stack (); 00119 } 00120 00121 /* Don't print the stack for gdb.GdbError exceptions. 00122 It is generally used to flag user errors. 00123 00124 We also don't want to print "Error occurred in Python command" 00125 for user errors. However, a missing message for gdb.GdbError 00126 exceptions is arguably a bug, so we flag it as such. */ 00127 00128 if (!PyErr_GivenExceptionMatches (ptype, gdbpy_gdberror_exc) 00129 || msg == NULL || *msg == '\0') 00130 { 00131 PyErr_Restore (ptype, pvalue, ptraceback); 00132 gdbpy_print_stack (); 00133 if (msg != NULL && *msg != '\0') 00134 error (_("Error occurred in Python convenience function: %s"), 00135 msg); 00136 else 00137 error (_("Error occurred in Python convenience function.")); 00138 } 00139 else 00140 { 00141 Py_XDECREF (ptype); 00142 Py_XDECREF (pvalue); 00143 Py_XDECREF (ptraceback); 00144 error ("%s", msg); 00145 } 00146 } 00147 00148 value = convert_value_from_python (result); 00149 if (value == NULL) 00150 { 00151 Py_DECREF (result); 00152 gdbpy_print_stack (); 00153 error (_("Error while executing Python code.")); 00154 } 00155 00156 Py_DECREF (result); 00157 do_cleanups (cleanup); 00158 00159 return value; 00160 } 00161 00162 /* Initializer for a Function object. It takes one argument, the name 00163 of the function. */ 00164 00165 static int 00166 fnpy_init (PyObject *self, PyObject *args, PyObject *kwds) 00167 { 00168 const char *name; 00169 char *docstring = NULL; 00170 00171 if (! PyArg_ParseTuple (args, "s", &name)) 00172 return -1; 00173 Py_INCREF (self); 00174 00175 if (PyObject_HasAttrString (self, "__doc__")) 00176 { 00177 PyObject *ds_obj = PyObject_GetAttrString (self, "__doc__"); 00178 if (ds_obj != NULL) 00179 { 00180 if (gdbpy_is_string (ds_obj)) 00181 { 00182 docstring = python_string_to_host_string (ds_obj); 00183 if (docstring == NULL) 00184 { 00185 Py_DECREF (self); 00186 Py_DECREF (ds_obj); 00187 return -1; 00188 } 00189 } 00190 00191 Py_DECREF (ds_obj); 00192 } 00193 } 00194 if (! docstring) 00195 docstring = xstrdup (_("This function is not documented.")); 00196 00197 add_internal_function (name, docstring, fnpy_call, self); 00198 return 0; 00199 } 00200 00201 /* Initialize internal function support. */ 00202 00203 int 00204 gdbpy_initialize_functions (void) 00205 { 00206 fnpy_object_type.tp_new = PyType_GenericNew; 00207 if (PyType_Ready (&fnpy_object_type) < 0) 00208 return -1; 00209 00210 return gdb_pymodule_addobject (gdb_module, "Function", 00211 (PyObject *) &fnpy_object_type); 00212 } 00213 00214 00215 00216 static PyTypeObject fnpy_object_type = 00217 { 00218 PyVarObject_HEAD_INIT (NULL, 0) 00219 "gdb.Function", /*tp_name*/ 00220 sizeof (PyObject), /*tp_basicsize*/ 00221 0, /*tp_itemsize*/ 00222 0, /*tp_dealloc*/ 00223 0, /*tp_print*/ 00224 0, /*tp_getattr*/ 00225 0, /*tp_setattr*/ 00226 0, /*tp_compare*/ 00227 0, /*tp_repr*/ 00228 0, /*tp_as_number*/ 00229 0, /*tp_as_sequence*/ 00230 0, /*tp_as_mapping*/ 00231 0, /*tp_hash */ 00232 0, /*tp_call*/ 00233 0, /*tp_str*/ 00234 0, /*tp_getattro*/ 00235 0, /*tp_setattro*/ 00236 0, /*tp_as_buffer*/ 00237 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ 00238 "GDB function object", /* tp_doc */ 00239 0, /* tp_traverse */ 00240 0, /* tp_clear */ 00241 0, /* tp_richcompare */ 00242 0, /* tp_weaklistoffset */ 00243 0, /* tp_iter */ 00244 0, /* tp_iternext */ 00245 0, /* tp_methods */ 00246 0, /* tp_members */ 00247 0, /* tp_getset */ 00248 0, /* tp_base */ 00249 0, /* tp_dict */ 00250 0, /* tp_descr_get */ 00251 0, /* tp_descr_set */ 00252 0, /* tp_dictoffset */ 00253 fnpy_init, /* tp_init */ 00254 0, /* tp_alloc */ 00255 };