| 1 | /* General utility routines for GDB/Python. |
| 2 | |
| 3 | Copyright (C) 2008-2019 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "charset.h" |
| 22 | #include "value.h" |
| 23 | #include "python-internal.h" |
| 24 | |
| 25 | /* Converts a Python 8-bit string to a unicode string object. Assumes the |
| 26 | 8-bit string is in the host charset. If an error occurs during conversion, |
| 27 | returns NULL with a python exception set. |
| 28 | |
| 29 | As an added bonus, the functions accepts a unicode string and returns it |
| 30 | right away, so callers don't need to check which kind of string they've |
| 31 | got. In Python 3, all strings are Unicode so this case is always the |
| 32 | one that applies. |
| 33 | |
| 34 | If the given object is not one of the mentioned string types, NULL is |
| 35 | returned, with the TypeError python exception set. */ |
| 36 | gdbpy_ref<> |
| 37 | python_string_to_unicode (PyObject *obj) |
| 38 | { |
| 39 | PyObject *unicode_str; |
| 40 | |
| 41 | /* If obj is already a unicode string, just return it. |
| 42 | I wish life was always that simple... */ |
| 43 | if (PyUnicode_Check (obj)) |
| 44 | { |
| 45 | unicode_str = obj; |
| 46 | Py_INCREF (obj); |
| 47 | } |
| 48 | #ifndef IS_PY3K |
| 49 | else if (PyString_Check (obj)) |
| 50 | unicode_str = PyUnicode_FromEncodedObject (obj, host_charset (), NULL); |
| 51 | #endif |
| 52 | else |
| 53 | { |
| 54 | PyErr_SetString (PyExc_TypeError, |
| 55 | _("Expected a string or unicode object.")); |
| 56 | unicode_str = NULL; |
| 57 | } |
| 58 | |
| 59 | return gdbpy_ref<> (unicode_str); |
| 60 | } |
| 61 | |
| 62 | /* Returns a newly allocated string with the contents of the given unicode |
| 63 | string object converted to CHARSET. If an error occurs during the |
| 64 | conversion, NULL will be returned and a python exception will be |
| 65 | set. */ |
| 66 | static gdb::unique_xmalloc_ptr<char> |
| 67 | unicode_to_encoded_string (PyObject *unicode_str, const char *charset) |
| 68 | { |
| 69 | /* Translate string to named charset. */ |
| 70 | gdbpy_ref<> string (PyUnicode_AsEncodedString (unicode_str, charset, NULL)); |
| 71 | if (string == NULL) |
| 72 | return NULL; |
| 73 | |
| 74 | return gdb::unique_xmalloc_ptr<char> |
| 75 | (xstrdup (PyBytes_AsString (string.get ()))); |
| 76 | } |
| 77 | |
| 78 | /* Returns a PyObject with the contents of the given unicode string |
| 79 | object converted to a named charset. If an error occurs during |
| 80 | the conversion, NULL will be returned and a python exception will |
| 81 | be set. */ |
| 82 | static gdbpy_ref<> |
| 83 | unicode_to_encoded_python_string (PyObject *unicode_str, const char *charset) |
| 84 | { |
| 85 | /* Translate string to named charset. */ |
| 86 | return gdbpy_ref<> (PyUnicode_AsEncodedString (unicode_str, charset, NULL)); |
| 87 | } |
| 88 | |
| 89 | /* Returns a newly allocated string with the contents of the given |
| 90 | unicode string object converted to the target's charset. If an |
| 91 | error occurs during the conversion, NULL will be returned and a |
| 92 | python exception will be set. */ |
| 93 | gdb::unique_xmalloc_ptr<char> |
| 94 | unicode_to_target_string (PyObject *unicode_str) |
| 95 | { |
| 96 | return unicode_to_encoded_string (unicode_str, |
| 97 | target_charset (python_gdbarch)); |
| 98 | } |
| 99 | |
| 100 | /* Returns a PyObject with the contents of the given unicode string |
| 101 | object converted to the target's charset. If an error occurs |
| 102 | during the conversion, NULL will be returned and a python exception |
| 103 | will be set. */ |
| 104 | static gdbpy_ref<> |
| 105 | unicode_to_target_python_string (PyObject *unicode_str) |
| 106 | { |
| 107 | return unicode_to_encoded_python_string (unicode_str, |
| 108 | target_charset (python_gdbarch)); |
| 109 | } |
| 110 | |
| 111 | /* Converts a python string (8-bit or unicode) to a target string in |
| 112 | the target's charset. Returns NULL on error, with a python |
| 113 | exception set. */ |
| 114 | gdb::unique_xmalloc_ptr<char> |
| 115 | python_string_to_target_string (PyObject *obj) |
| 116 | { |
| 117 | gdbpy_ref<> str = python_string_to_unicode (obj); |
| 118 | if (str == NULL) |
| 119 | return NULL; |
| 120 | |
| 121 | return unicode_to_target_string (str.get ()); |
| 122 | } |
| 123 | |
| 124 | /* Converts a python string (8-bit or unicode) to a target string in the |
| 125 | target's charset. Returns NULL on error, with a python exception |
| 126 | set. |
| 127 | |
| 128 | In Python 3, the returned object is a "bytes" object (not a string). */ |
| 129 | gdbpy_ref<> |
| 130 | python_string_to_target_python_string (PyObject *obj) |
| 131 | { |
| 132 | gdbpy_ref<> str = python_string_to_unicode (obj); |
| 133 | if (str == NULL) |
| 134 | return str; |
| 135 | |
| 136 | return unicode_to_target_python_string (str.get ()); |
| 137 | } |
| 138 | |
| 139 | /* Converts a python string (8-bit or unicode) to a target string in |
| 140 | the host's charset. Returns NULL on error, with a python exception |
| 141 | set. */ |
| 142 | gdb::unique_xmalloc_ptr<char> |
| 143 | python_string_to_host_string (PyObject *obj) |
| 144 | { |
| 145 | gdbpy_ref<> str = python_string_to_unicode (obj); |
| 146 | if (str == NULL) |
| 147 | return NULL; |
| 148 | |
| 149 | return unicode_to_encoded_string (str.get (), host_charset ()); |
| 150 | } |
| 151 | |
| 152 | /* Convert a host string to a python string. */ |
| 153 | |
| 154 | gdbpy_ref<> |
| 155 | host_string_to_python_string (const char *str) |
| 156 | { |
| 157 | return gdbpy_ref<> (PyString_Decode (str, strlen (str), host_charset (), |
| 158 | NULL)); |
| 159 | } |
| 160 | |
| 161 | /* Return true if OBJ is a Python string or unicode object, false |
| 162 | otherwise. */ |
| 163 | |
| 164 | int |
| 165 | gdbpy_is_string (PyObject *obj) |
| 166 | { |
| 167 | #ifdef IS_PY3K |
| 168 | return PyUnicode_Check (obj); |
| 169 | #else |
| 170 | return PyString_Check (obj) || PyUnicode_Check (obj); |
| 171 | #endif |
| 172 | } |
| 173 | |
| 174 | /* Return the string representation of OBJ, i.e., str (obj). |
| 175 | If the result is NULL a python error occurred, the caller must clear it. */ |
| 176 | |
| 177 | gdb::unique_xmalloc_ptr<char> |
| 178 | gdbpy_obj_to_string (PyObject *obj) |
| 179 | { |
| 180 | gdbpy_ref<> str_obj (PyObject_Str (obj)); |
| 181 | |
| 182 | if (str_obj != NULL) |
| 183 | { |
| 184 | gdb::unique_xmalloc_ptr<char> msg; |
| 185 | |
| 186 | #ifdef IS_PY3K |
| 187 | msg = python_string_to_host_string (str_obj.get ()); |
| 188 | #else |
| 189 | msg.reset (xstrdup (PyString_AsString (str_obj.get ()))); |
| 190 | #endif |
| 191 | |
| 192 | return msg; |
| 193 | } |
| 194 | |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | /* See python-internal.h. */ |
| 199 | |
| 200 | gdb::unique_xmalloc_ptr<char> |
| 201 | gdbpy_err_fetch::to_string () const |
| 202 | { |
| 203 | /* There are a few cases to consider. |
| 204 | For example: |
| 205 | value is a string when PyErr_SetString is used. |
| 206 | value is not a string when raise "foo" is used, instead it is None |
| 207 | and type is "foo". |
| 208 | So the algorithm we use is to print `str (value)' if it's not |
| 209 | None, otherwise we print `str (type)'. |
| 210 | Using str (aka PyObject_Str) will fetch the error message from |
| 211 | gdb.GdbError ("message"). */ |
| 212 | |
| 213 | if (m_error_value && m_error_value != Py_None) |
| 214 | return gdbpy_obj_to_string (m_error_value); |
| 215 | else |
| 216 | return gdbpy_obj_to_string (m_error_type); |
| 217 | } |
| 218 | |
| 219 | /* See python-internal.h. */ |
| 220 | |
| 221 | gdb::unique_xmalloc_ptr<char> |
| 222 | gdbpy_err_fetch::type_to_string () const |
| 223 | { |
| 224 | return gdbpy_obj_to_string (m_error_type); |
| 225 | } |
| 226 | |
| 227 | /* Convert a GDB exception to the appropriate Python exception. |
| 228 | |
| 229 | This sets the Python error indicator. */ |
| 230 | |
| 231 | void |
| 232 | gdbpy_convert_exception (const struct gdb_exception &exception) |
| 233 | { |
| 234 | PyObject *exc_class; |
| 235 | |
| 236 | if (exception.reason == RETURN_QUIT) |
| 237 | exc_class = PyExc_KeyboardInterrupt; |
| 238 | else if (exception.error == MEMORY_ERROR) |
| 239 | exc_class = gdbpy_gdb_memory_error; |
| 240 | else |
| 241 | exc_class = gdbpy_gdb_error; |
| 242 | |
| 243 | PyErr_Format (exc_class, "%s", exception.what ()); |
| 244 | } |
| 245 | |
| 246 | /* Converts OBJ to a CORE_ADDR value. |
| 247 | |
| 248 | Returns 0 on success or -1 on failure, with a Python exception set. |
| 249 | */ |
| 250 | |
| 251 | int |
| 252 | get_addr_from_python (PyObject *obj, CORE_ADDR *addr) |
| 253 | { |
| 254 | if (gdbpy_is_value_object (obj)) |
| 255 | { |
| 256 | |
| 257 | try |
| 258 | { |
| 259 | *addr = value_as_address (value_object_to_value (obj)); |
| 260 | } |
| 261 | catch (const gdb_exception &except) |
| 262 | { |
| 263 | GDB_PY_SET_HANDLE_EXCEPTION (except); |
| 264 | } |
| 265 | } |
| 266 | else |
| 267 | { |
| 268 | gdbpy_ref<> num (PyNumber_Long (obj)); |
| 269 | gdb_py_ulongest val; |
| 270 | |
| 271 | if (num == NULL) |
| 272 | return -1; |
| 273 | |
| 274 | val = gdb_py_long_as_ulongest (num.get ()); |
| 275 | if (PyErr_Occurred ()) |
| 276 | return -1; |
| 277 | |
| 278 | if (sizeof (val) > sizeof (CORE_ADDR) && ((CORE_ADDR) val) != val) |
| 279 | { |
| 280 | PyErr_SetString (PyExc_ValueError, |
| 281 | _("Overflow converting to address.")); |
| 282 | return -1; |
| 283 | } |
| 284 | |
| 285 | *addr = val; |
| 286 | } |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | /* Convert a LONGEST to the appropriate Python object -- either an |
| 292 | integer object or a long object, depending on its value. */ |
| 293 | |
| 294 | gdbpy_ref<> |
| 295 | gdb_py_object_from_longest (LONGEST l) |
| 296 | { |
| 297 | #ifdef IS_PY3K |
| 298 | if (sizeof (l) > sizeof (long)) |
| 299 | return gdbpy_ref<> (PyLong_FromLongLong (l)); |
| 300 | return gdbpy_ref<> (PyLong_FromLong (l)); |
| 301 | #else |
| 302 | #ifdef HAVE_LONG_LONG /* Defined by Python. */ |
| 303 | /* If we have 'long long', and the value overflows a 'long', use a |
| 304 | Python Long; otherwise use a Python Int. */ |
| 305 | if (sizeof (l) > sizeof (long) |
| 306 | && (l > PyInt_GetMax () || l < (- (LONGEST) PyInt_GetMax ()) - 1)) |
| 307 | return gdbpy_ref<> (PyLong_FromLongLong (l)); |
| 308 | #endif |
| 309 | return gdbpy_ref<> (PyInt_FromLong (l)); |
| 310 | #endif |
| 311 | } |
| 312 | |
| 313 | /* Convert a ULONGEST to the appropriate Python object -- either an |
| 314 | integer object or a long object, depending on its value. */ |
| 315 | |
| 316 | gdbpy_ref<> |
| 317 | gdb_py_object_from_ulongest (ULONGEST l) |
| 318 | { |
| 319 | #ifdef IS_PY3K |
| 320 | if (sizeof (l) > sizeof (unsigned long)) |
| 321 | return gdbpy_ref<> (PyLong_FromUnsignedLongLong (l)); |
| 322 | return gdbpy_ref<> (PyLong_FromUnsignedLong (l)); |
| 323 | #else |
| 324 | #ifdef HAVE_LONG_LONG /* Defined by Python. */ |
| 325 | /* If we have 'long long', and the value overflows a 'long', use a |
| 326 | Python Long; otherwise use a Python Int. */ |
| 327 | if (sizeof (l) > sizeof (unsigned long) && l > PyInt_GetMax ()) |
| 328 | return gdbpy_ref<> (PyLong_FromUnsignedLongLong (l)); |
| 329 | #endif |
| 330 | |
| 331 | if (l > PyInt_GetMax ()) |
| 332 | return gdbpy_ref<> (PyLong_FromUnsignedLong (l)); |
| 333 | |
| 334 | return gdbpy_ref<> (PyInt_FromLong (l)); |
| 335 | #endif |
| 336 | } |
| 337 | |
| 338 | /* Like PyInt_AsLong, but returns 0 on failure, 1 on success, and puts |
| 339 | the value into an out parameter. */ |
| 340 | |
| 341 | int |
| 342 | gdb_py_int_as_long (PyObject *obj, long *result) |
| 343 | { |
| 344 | *result = PyInt_AsLong (obj); |
| 345 | return ! (*result == -1 && PyErr_Occurred ()); |
| 346 | } |
| 347 | |
| 348 | \f |
| 349 | |
| 350 | /* Generic implementation of the __dict__ attribute for objects that |
| 351 | have a dictionary. The CLOSURE argument should be the type object. |
| 352 | This only handles positive values for tp_dictoffset. */ |
| 353 | |
| 354 | PyObject * |
| 355 | gdb_py_generic_dict (PyObject *self, void *closure) |
| 356 | { |
| 357 | PyObject *result; |
| 358 | PyTypeObject *type_obj = (PyTypeObject *) closure; |
| 359 | char *raw_ptr; |
| 360 | |
| 361 | raw_ptr = (char *) self + type_obj->tp_dictoffset; |
| 362 | result = * (PyObject **) raw_ptr; |
| 363 | |
| 364 | Py_INCREF (result); |
| 365 | return result; |
| 366 | } |
| 367 | |
| 368 | /* Like PyModule_AddObject, but does not steal a reference to |
| 369 | OBJECT. */ |
| 370 | |
| 371 | int |
| 372 | gdb_pymodule_addobject (PyObject *module, const char *name, PyObject *object) |
| 373 | { |
| 374 | int result; |
| 375 | |
| 376 | Py_INCREF (object); |
| 377 | result = PyModule_AddObject (module, name, object); |
| 378 | if (result < 0) |
| 379 | Py_DECREF (object); |
| 380 | return result; |
| 381 | } |
| 382 | |
| 383 | /* Handle a Python exception when the special gdb.GdbError treatment |
| 384 | is desired. This should only be called when an exception is set. |
| 385 | If the exception is a gdb.GdbError, throw a gdb exception with the |
| 386 | exception text. For other exceptions, print the Python stack and |
| 387 | then throw a gdb exception. */ |
| 388 | |
| 389 | void |
| 390 | gdbpy_handle_exception () |
| 391 | { |
| 392 | gdbpy_err_fetch fetched_error; |
| 393 | gdb::unique_xmalloc_ptr<char> msg = fetched_error.to_string (); |
| 394 | |
| 395 | if (msg == NULL) |
| 396 | { |
| 397 | /* An error occurred computing the string representation of the |
| 398 | error message. This is rare, but we should inform the user. */ |
| 399 | printf_filtered (_("An error occurred in Python " |
| 400 | "and then another occurred computing the " |
| 401 | "error message.\n")); |
| 402 | gdbpy_print_stack (); |
| 403 | } |
| 404 | |
| 405 | /* Don't print the stack for gdb.GdbError exceptions. |
| 406 | It is generally used to flag user errors. |
| 407 | |
| 408 | We also don't want to print "Error occurred in Python command" |
| 409 | for user errors. However, a missing message for gdb.GdbError |
| 410 | exceptions is arguably a bug, so we flag it as such. */ |
| 411 | |
| 412 | if (fetched_error.type_matches (PyExc_KeyboardInterrupt)) |
| 413 | throw_quit ("Quit"); |
| 414 | else if (! fetched_error.type_matches (gdbpy_gdberror_exc) |
| 415 | || msg == NULL || *msg == '\0') |
| 416 | { |
| 417 | fetched_error.restore (); |
| 418 | gdbpy_print_stack (); |
| 419 | if (msg != NULL && *msg != '\0') |
| 420 | error (_("Error occurred in Python: %s"), msg.get ()); |
| 421 | else |
| 422 | error (_("Error occurred in Python.")); |
| 423 | } |
| 424 | else |
| 425 | error ("%s", msg.get ()); |
| 426 | } |