| 1 | /* Python interface to values. |
| 2 | |
| 3 | Copyright (C) 2008, 2009, 2010, 2011, 2012 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 "gdb_assert.h" |
| 22 | #include "charset.h" |
| 23 | #include "value.h" |
| 24 | #include "exceptions.h" |
| 25 | #include "language.h" |
| 26 | #include "dfp.h" |
| 27 | #include "valprint.h" |
| 28 | #include "infcall.h" |
| 29 | #include "expression.h" |
| 30 | #include "cp-abi.h" |
| 31 | |
| 32 | #ifdef HAVE_PYTHON |
| 33 | |
| 34 | #include "python-internal.h" |
| 35 | |
| 36 | /* Even though Python scalar types directly map to host types, we use |
| 37 | target types here to remain consistent with the values system in |
| 38 | GDB (which uses target arithmetic). */ |
| 39 | |
| 40 | /* Python's integer type corresponds to C's long type. */ |
| 41 | #define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long |
| 42 | |
| 43 | /* Python's float type corresponds to C's double type. */ |
| 44 | #define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double |
| 45 | |
| 46 | /* Python's long type corresponds to C's long long type. */ |
| 47 | #define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long |
| 48 | |
| 49 | /* Python's long type corresponds to C's long long type. Unsigned version. */ |
| 50 | #define builtin_type_upylong builtin_type \ |
| 51 | (python_gdbarch)->builtin_unsigned_long_long |
| 52 | |
| 53 | #define builtin_type_pybool \ |
| 54 | language_bool_type (python_language, python_gdbarch) |
| 55 | |
| 56 | #define builtin_type_pychar \ |
| 57 | language_string_char_type (python_language, python_gdbarch) |
| 58 | |
| 59 | typedef struct value_object { |
| 60 | PyObject_HEAD |
| 61 | struct value_object *next; |
| 62 | struct value_object *prev; |
| 63 | struct value *value; |
| 64 | PyObject *address; |
| 65 | PyObject *type; |
| 66 | PyObject *dynamic_type; |
| 67 | } value_object; |
| 68 | |
| 69 | /* List of all values which are currently exposed to Python. It is |
| 70 | maintained so that when an objfile is discarded, preserve_values |
| 71 | can copy the values' types if needed. */ |
| 72 | /* This variable is unnecessarily initialized to NULL in order to |
| 73 | work around a linker bug on MacOS. */ |
| 74 | static value_object *values_in_python = NULL; |
| 75 | |
| 76 | /* Called by the Python interpreter when deallocating a value object. */ |
| 77 | static void |
| 78 | valpy_dealloc (PyObject *obj) |
| 79 | { |
| 80 | value_object *self = (value_object *) obj; |
| 81 | |
| 82 | /* Remove SELF from the global list. */ |
| 83 | if (self->prev) |
| 84 | self->prev->next = self->next; |
| 85 | else |
| 86 | { |
| 87 | gdb_assert (values_in_python == self); |
| 88 | values_in_python = self->next; |
| 89 | } |
| 90 | if (self->next) |
| 91 | self->next->prev = self->prev; |
| 92 | |
| 93 | value_free (self->value); |
| 94 | |
| 95 | if (self->address) |
| 96 | /* Use braces to appease gcc warning. *sigh* */ |
| 97 | { |
| 98 | Py_DECREF (self->address); |
| 99 | } |
| 100 | |
| 101 | if (self->type) |
| 102 | { |
| 103 | Py_DECREF (self->type); |
| 104 | } |
| 105 | |
| 106 | Py_XDECREF (self->dynamic_type); |
| 107 | |
| 108 | self->ob_type->tp_free (self); |
| 109 | } |
| 110 | |
| 111 | /* Helper to push a Value object on the global list. */ |
| 112 | static void |
| 113 | note_value (value_object *value_obj) |
| 114 | { |
| 115 | value_obj->next = values_in_python; |
| 116 | if (value_obj->next) |
| 117 | value_obj->next->prev = value_obj; |
| 118 | value_obj->prev = NULL; |
| 119 | values_in_python = value_obj; |
| 120 | } |
| 121 | |
| 122 | /* Called when a new gdb.Value object needs to be allocated. Returns NULL on |
| 123 | error, with a python exception set. */ |
| 124 | static PyObject * |
| 125 | valpy_new (PyTypeObject *subtype, PyObject *args, PyObject *keywords) |
| 126 | { |
| 127 | struct value *value = NULL; /* Initialize to appease gcc warning. */ |
| 128 | value_object *value_obj; |
| 129 | |
| 130 | if (PyTuple_Size (args) != 1) |
| 131 | { |
| 132 | PyErr_SetString (PyExc_TypeError, _("Value object creation takes only " |
| 133 | "1 argument")); |
| 134 | return NULL; |
| 135 | } |
| 136 | |
| 137 | value_obj = (value_object *) subtype->tp_alloc (subtype, 1); |
| 138 | if (value_obj == NULL) |
| 139 | { |
| 140 | PyErr_SetString (PyExc_MemoryError, _("Could not allocate memory to " |
| 141 | "create Value object.")); |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | value = convert_value_from_python (PyTuple_GetItem (args, 0)); |
| 146 | if (value == NULL) |
| 147 | { |
| 148 | subtype->tp_free (value_obj); |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | value_obj->value = value; |
| 153 | release_value_or_incref (value); |
| 154 | value_obj->address = NULL; |
| 155 | value_obj->type = NULL; |
| 156 | value_obj->dynamic_type = NULL; |
| 157 | note_value (value_obj); |
| 158 | |
| 159 | return (PyObject *) value_obj; |
| 160 | } |
| 161 | |
| 162 | /* Iterate over all the Value objects, calling preserve_one_value on |
| 163 | each. */ |
| 164 | void |
| 165 | preserve_python_values (struct objfile *objfile, htab_t copied_types) |
| 166 | { |
| 167 | value_object *iter; |
| 168 | |
| 169 | for (iter = values_in_python; iter; iter = iter->next) |
| 170 | preserve_one_value (iter->value, objfile, copied_types); |
| 171 | } |
| 172 | |
| 173 | /* Given a value of a pointer type, apply the C unary * operator to it. */ |
| 174 | static PyObject * |
| 175 | valpy_dereference (PyObject *self, PyObject *args) |
| 176 | { |
| 177 | volatile struct gdb_exception except; |
| 178 | PyObject *result = NULL; |
| 179 | |
| 180 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 181 | { |
| 182 | struct value *res_val; |
| 183 | struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); |
| 184 | |
| 185 | res_val = value_ind (((value_object *) self)->value); |
| 186 | result = value_to_value_object (res_val); |
| 187 | do_cleanups (cleanup); |
| 188 | } |
| 189 | GDB_PY_HANDLE_EXCEPTION (except); |
| 190 | |
| 191 | return result; |
| 192 | } |
| 193 | |
| 194 | /* Return "&value". */ |
| 195 | static PyObject * |
| 196 | valpy_get_address (PyObject *self, void *closure) |
| 197 | { |
| 198 | value_object *val_obj = (value_object *) self; |
| 199 | volatile struct gdb_exception except; |
| 200 | |
| 201 | if (!val_obj->address) |
| 202 | { |
| 203 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 204 | { |
| 205 | struct value *res_val; |
| 206 | struct cleanup *cleanup |
| 207 | = make_cleanup_value_free_to_mark (value_mark ()); |
| 208 | |
| 209 | res_val = value_addr (val_obj->value); |
| 210 | val_obj->address = value_to_value_object (res_val); |
| 211 | do_cleanups (cleanup); |
| 212 | } |
| 213 | if (except.reason < 0) |
| 214 | { |
| 215 | val_obj->address = Py_None; |
| 216 | Py_INCREF (Py_None); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | Py_XINCREF (val_obj->address); |
| 221 | |
| 222 | return val_obj->address; |
| 223 | } |
| 224 | |
| 225 | /* Return type of the value. */ |
| 226 | static PyObject * |
| 227 | valpy_get_type (PyObject *self, void *closure) |
| 228 | { |
| 229 | value_object *obj = (value_object *) self; |
| 230 | |
| 231 | if (!obj->type) |
| 232 | { |
| 233 | obj->type = type_to_type_object (value_type (obj->value)); |
| 234 | if (!obj->type) |
| 235 | return NULL; |
| 236 | } |
| 237 | Py_INCREF (obj->type); |
| 238 | return obj->type; |
| 239 | } |
| 240 | |
| 241 | /* Return dynamic type of the value. */ |
| 242 | |
| 243 | static PyObject * |
| 244 | valpy_get_dynamic_type (PyObject *self, void *closure) |
| 245 | { |
| 246 | value_object *obj = (value_object *) self; |
| 247 | volatile struct gdb_exception except; |
| 248 | struct type *type = NULL; |
| 249 | |
| 250 | if (obj->dynamic_type != NULL) |
| 251 | { |
| 252 | Py_INCREF (obj->dynamic_type); |
| 253 | return obj->dynamic_type; |
| 254 | } |
| 255 | |
| 256 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 257 | { |
| 258 | struct value *val = obj->value; |
| 259 | struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); |
| 260 | |
| 261 | type = value_type (val); |
| 262 | CHECK_TYPEDEF (type); |
| 263 | |
| 264 | if (((TYPE_CODE (type) == TYPE_CODE_PTR) |
| 265 | || (TYPE_CODE (type) == TYPE_CODE_REF)) |
| 266 | && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS)) |
| 267 | { |
| 268 | struct value *target; |
| 269 | int was_pointer = TYPE_CODE (type) == TYPE_CODE_PTR; |
| 270 | |
| 271 | target = value_ind (val); |
| 272 | type = value_rtti_type (target, NULL, NULL, NULL); |
| 273 | |
| 274 | if (type) |
| 275 | { |
| 276 | if (was_pointer) |
| 277 | type = lookup_pointer_type (type); |
| 278 | else |
| 279 | type = lookup_reference_type (type); |
| 280 | } |
| 281 | } |
| 282 | else if (TYPE_CODE (type) == TYPE_CODE_CLASS) |
| 283 | type = value_rtti_type (val, NULL, NULL, NULL); |
| 284 | else |
| 285 | { |
| 286 | /* Re-use object's static type. */ |
| 287 | type = NULL; |
| 288 | } |
| 289 | |
| 290 | do_cleanups (cleanup); |
| 291 | } |
| 292 | GDB_PY_HANDLE_EXCEPTION (except); |
| 293 | |
| 294 | if (type == NULL) |
| 295 | { |
| 296 | /* Ensure that the TYPE field is ready. */ |
| 297 | if (!valpy_get_type (self, NULL)) |
| 298 | return NULL; |
| 299 | /* We don't need to incref here, because valpy_get_type already |
| 300 | did it for us. */ |
| 301 | obj->dynamic_type = obj->type; |
| 302 | } |
| 303 | else |
| 304 | obj->dynamic_type = type_to_type_object (type); |
| 305 | |
| 306 | Py_INCREF (obj->dynamic_type); |
| 307 | return obj->dynamic_type; |
| 308 | } |
| 309 | |
| 310 | /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) -> |
| 311 | string. Return a PyObject representing a lazy_string_object type. |
| 312 | A lazy string is a pointer to a string with an optional encoding and |
| 313 | length. If ENCODING is not given, encoding is set to None. If an |
| 314 | ENCODING is provided the encoding parameter is set to ENCODING, but |
| 315 | the string is not encoded. If LENGTH is provided then the length |
| 316 | parameter is set to LENGTH, otherwise length will be set to -1 (first |
| 317 | null of appropriate with). */ |
| 318 | static PyObject * |
| 319 | valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw) |
| 320 | { |
| 321 | gdb_py_longest length = -1; |
| 322 | struct value *value = ((value_object *) self)->value; |
| 323 | const char *user_encoding = NULL; |
| 324 | static char *keywords[] = { "encoding", "length", NULL }; |
| 325 | PyObject *str_obj = NULL; |
| 326 | volatile struct gdb_exception except; |
| 327 | |
| 328 | if (!PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG, keywords, |
| 329 | &user_encoding, &length)) |
| 330 | return NULL; |
| 331 | |
| 332 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 333 | { |
| 334 | struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); |
| 335 | |
| 336 | if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR) |
| 337 | value = value_ind (value); |
| 338 | |
| 339 | str_obj = gdbpy_create_lazy_string_object (value_address (value), length, |
| 340 | user_encoding, |
| 341 | value_type (value)); |
| 342 | |
| 343 | do_cleanups (cleanup); |
| 344 | } |
| 345 | GDB_PY_HANDLE_EXCEPTION (except); |
| 346 | |
| 347 | return str_obj; |
| 348 | } |
| 349 | |
| 350 | /* Implementation of gdb.Value.string ([encoding] [, errors] |
| 351 | [, length]) -> string. Return Unicode string with value contents. |
| 352 | If ENCODING is not given, the string is assumed to be encoded in |
| 353 | the target's charset. If LENGTH is provided, only fetch string to |
| 354 | the length provided. */ |
| 355 | |
| 356 | static PyObject * |
| 357 | valpy_string (PyObject *self, PyObject *args, PyObject *kw) |
| 358 | { |
| 359 | int length = -1; |
| 360 | gdb_byte *buffer; |
| 361 | struct value *value = ((value_object *) self)->value; |
| 362 | volatile struct gdb_exception except; |
| 363 | PyObject *unicode; |
| 364 | const char *encoding = NULL; |
| 365 | const char *errors = NULL; |
| 366 | const char *user_encoding = NULL; |
| 367 | const char *la_encoding = NULL; |
| 368 | struct type *char_type; |
| 369 | static char *keywords[] = { "encoding", "errors", "length", NULL }; |
| 370 | |
| 371 | if (!PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords, |
| 372 | &user_encoding, &errors, &length)) |
| 373 | return NULL; |
| 374 | |
| 375 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 376 | { |
| 377 | LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding); |
| 378 | } |
| 379 | GDB_PY_HANDLE_EXCEPTION (except); |
| 380 | |
| 381 | encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding; |
| 382 | unicode = PyUnicode_Decode (buffer, length * TYPE_LENGTH (char_type), |
| 383 | encoding, errors); |
| 384 | xfree (buffer); |
| 385 | |
| 386 | return unicode; |
| 387 | } |
| 388 | |
| 389 | /* A helper function that implements the various cast operators. */ |
| 390 | |
| 391 | static PyObject * |
| 392 | valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op) |
| 393 | { |
| 394 | PyObject *type_obj, *result = NULL; |
| 395 | struct type *type; |
| 396 | volatile struct gdb_exception except; |
| 397 | |
| 398 | if (! PyArg_ParseTuple (args, "O", &type_obj)) |
| 399 | return NULL; |
| 400 | |
| 401 | type = type_object_to_type (type_obj); |
| 402 | if (! type) |
| 403 | { |
| 404 | PyErr_SetString (PyExc_RuntimeError, |
| 405 | _("Argument must be a type.")); |
| 406 | return NULL; |
| 407 | } |
| 408 | |
| 409 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 410 | { |
| 411 | struct value *val = ((value_object *) self)->value; |
| 412 | struct value *res_val; |
| 413 | struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); |
| 414 | |
| 415 | if (op == UNOP_DYNAMIC_CAST) |
| 416 | res_val = value_dynamic_cast (type, val); |
| 417 | else if (op == UNOP_REINTERPRET_CAST) |
| 418 | res_val = value_reinterpret_cast (type, val); |
| 419 | else |
| 420 | { |
| 421 | gdb_assert (op == UNOP_CAST); |
| 422 | res_val = value_cast (type, val); |
| 423 | } |
| 424 | |
| 425 | result = value_to_value_object (res_val); |
| 426 | do_cleanups (cleanup); |
| 427 | } |
| 428 | GDB_PY_HANDLE_EXCEPTION (except); |
| 429 | |
| 430 | return result; |
| 431 | } |
| 432 | |
| 433 | /* Implementation of the "cast" method. */ |
| 434 | |
| 435 | static PyObject * |
| 436 | valpy_cast (PyObject *self, PyObject *args) |
| 437 | { |
| 438 | return valpy_do_cast (self, args, UNOP_CAST); |
| 439 | } |
| 440 | |
| 441 | /* Implementation of the "dynamic_cast" method. */ |
| 442 | |
| 443 | static PyObject * |
| 444 | valpy_dynamic_cast (PyObject *self, PyObject *args) |
| 445 | { |
| 446 | return valpy_do_cast (self, args, UNOP_DYNAMIC_CAST); |
| 447 | } |
| 448 | |
| 449 | /* Implementation of the "reinterpret_cast" method. */ |
| 450 | |
| 451 | static PyObject * |
| 452 | valpy_reinterpret_cast (PyObject *self, PyObject *args) |
| 453 | { |
| 454 | return valpy_do_cast (self, args, UNOP_REINTERPRET_CAST); |
| 455 | } |
| 456 | |
| 457 | static Py_ssize_t |
| 458 | valpy_length (PyObject *self) |
| 459 | { |
| 460 | /* We don't support getting the number of elements in a struct / class. */ |
| 461 | PyErr_SetString (PyExc_NotImplementedError, |
| 462 | _("Invalid operation on gdb.Value.")); |
| 463 | return -1; |
| 464 | } |
| 465 | |
| 466 | /* Given string name of an element inside structure, return its value |
| 467 | object. Returns NULL on error, with a python exception set. */ |
| 468 | static PyObject * |
| 469 | valpy_getitem (PyObject *self, PyObject *key) |
| 470 | { |
| 471 | value_object *self_value = (value_object *) self; |
| 472 | char *field = NULL; |
| 473 | volatile struct gdb_exception except; |
| 474 | PyObject *result = NULL; |
| 475 | |
| 476 | if (gdbpy_is_string (key)) |
| 477 | { |
| 478 | field = python_string_to_host_string (key); |
| 479 | if (field == NULL) |
| 480 | return NULL; |
| 481 | } |
| 482 | |
| 483 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 484 | { |
| 485 | struct value *tmp = self_value->value; |
| 486 | struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); |
| 487 | struct value *res_val = NULL; |
| 488 | |
| 489 | if (field) |
| 490 | res_val = value_struct_elt (&tmp, NULL, field, 0, NULL); |
| 491 | else |
| 492 | { |
| 493 | /* Assume we are attempting an array access, and let the |
| 494 | value code throw an exception if the index has an invalid |
| 495 | type. */ |
| 496 | struct value *idx = convert_value_from_python (key); |
| 497 | |
| 498 | if (idx != NULL) |
| 499 | { |
| 500 | /* Check the value's type is something that can be accessed via |
| 501 | a subscript. */ |
| 502 | struct type *type; |
| 503 | |
| 504 | tmp = coerce_ref (tmp); |
| 505 | type = check_typedef (value_type (tmp)); |
| 506 | if (TYPE_CODE (type) != TYPE_CODE_ARRAY |
| 507 | && TYPE_CODE (type) != TYPE_CODE_PTR) |
| 508 | error (_("Cannot subscript requested type.")); |
| 509 | else |
| 510 | res_val = value_subscript (tmp, value_as_long (idx)); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | if (res_val) |
| 515 | result = value_to_value_object (res_val); |
| 516 | do_cleanups (cleanup); |
| 517 | } |
| 518 | |
| 519 | xfree (field); |
| 520 | GDB_PY_HANDLE_EXCEPTION (except); |
| 521 | |
| 522 | return result; |
| 523 | } |
| 524 | |
| 525 | static int |
| 526 | valpy_setitem (PyObject *self, PyObject *key, PyObject *value) |
| 527 | { |
| 528 | PyErr_Format (PyExc_NotImplementedError, |
| 529 | _("Setting of struct elements is not currently supported.")); |
| 530 | return -1; |
| 531 | } |
| 532 | |
| 533 | /* Called by the Python interpreter to perform an inferior function |
| 534 | call on the value. Returns NULL on error, with a python exception set. */ |
| 535 | static PyObject * |
| 536 | valpy_call (PyObject *self, PyObject *args, PyObject *keywords) |
| 537 | { |
| 538 | Py_ssize_t args_count; |
| 539 | volatile struct gdb_exception except; |
| 540 | struct value *function = ((value_object *) self)->value; |
| 541 | struct value **vargs = NULL; |
| 542 | struct type *ftype = NULL; |
| 543 | struct value *mark = value_mark (); |
| 544 | PyObject *result = NULL; |
| 545 | |
| 546 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 547 | { |
| 548 | ftype = check_typedef (value_type (function)); |
| 549 | } |
| 550 | GDB_PY_HANDLE_EXCEPTION (except); |
| 551 | |
| 552 | if (TYPE_CODE (ftype) != TYPE_CODE_FUNC) |
| 553 | { |
| 554 | PyErr_SetString (PyExc_RuntimeError, |
| 555 | _("Value is not callable (not TYPE_CODE_FUNC).")); |
| 556 | return NULL; |
| 557 | } |
| 558 | |
| 559 | if (! PyTuple_Check (args)) |
| 560 | { |
| 561 | PyErr_SetString (PyExc_TypeError, |
| 562 | _("Inferior arguments must be provided in a tuple.")); |
| 563 | return NULL; |
| 564 | } |
| 565 | |
| 566 | args_count = PyTuple_Size (args); |
| 567 | if (args_count > 0) |
| 568 | { |
| 569 | int i; |
| 570 | |
| 571 | vargs = alloca (sizeof (struct value *) * args_count); |
| 572 | for (i = 0; i < args_count; i++) |
| 573 | { |
| 574 | PyObject *item = PyTuple_GetItem (args, i); |
| 575 | |
| 576 | if (item == NULL) |
| 577 | return NULL; |
| 578 | |
| 579 | vargs[i] = convert_value_from_python (item); |
| 580 | if (vargs[i] == NULL) |
| 581 | return NULL; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 586 | { |
| 587 | struct cleanup *cleanup = make_cleanup_value_free_to_mark (mark); |
| 588 | struct value *return_value; |
| 589 | |
| 590 | return_value = call_function_by_hand (function, args_count, vargs); |
| 591 | result = value_to_value_object (return_value); |
| 592 | do_cleanups (cleanup); |
| 593 | } |
| 594 | GDB_PY_HANDLE_EXCEPTION (except); |
| 595 | |
| 596 | return result; |
| 597 | } |
| 598 | |
| 599 | /* Called by the Python interpreter to obtain string representation |
| 600 | of the object. */ |
| 601 | static PyObject * |
| 602 | valpy_str (PyObject *self) |
| 603 | { |
| 604 | char *s = NULL; |
| 605 | PyObject *result; |
| 606 | struct value_print_options opts; |
| 607 | volatile struct gdb_exception except; |
| 608 | |
| 609 | get_user_print_options (&opts); |
| 610 | opts.deref_ref = 0; |
| 611 | |
| 612 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 613 | { |
| 614 | struct ui_file *stb = mem_fileopen (); |
| 615 | struct cleanup *old_chain = make_cleanup_ui_file_delete (stb); |
| 616 | |
| 617 | common_val_print (((value_object *) self)->value, stb, 0, |
| 618 | &opts, python_language); |
| 619 | s = ui_file_xstrdup (stb, NULL); |
| 620 | |
| 621 | do_cleanups (old_chain); |
| 622 | } |
| 623 | GDB_PY_HANDLE_EXCEPTION (except); |
| 624 | |
| 625 | result = PyUnicode_Decode (s, strlen (s), host_charset (), NULL); |
| 626 | xfree (s); |
| 627 | |
| 628 | return result; |
| 629 | } |
| 630 | |
| 631 | /* Implements gdb.Value.is_optimized_out. */ |
| 632 | static PyObject * |
| 633 | valpy_get_is_optimized_out (PyObject *self, void *closure) |
| 634 | { |
| 635 | struct value *value = ((value_object *) self)->value; |
| 636 | int opt = 0; |
| 637 | volatile struct gdb_exception except; |
| 638 | |
| 639 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 640 | { |
| 641 | opt = value_optimized_out (value); |
| 642 | } |
| 643 | GDB_PY_HANDLE_EXCEPTION (except); |
| 644 | |
| 645 | if (opt) |
| 646 | Py_RETURN_TRUE; |
| 647 | |
| 648 | Py_RETURN_FALSE; |
| 649 | } |
| 650 | |
| 651 | /* Implements gdb.Value.is_lazy. */ |
| 652 | static PyObject * |
| 653 | valpy_get_is_lazy (PyObject *self, void *closure) |
| 654 | { |
| 655 | struct value *value = ((value_object *) self)->value; |
| 656 | int opt = 0; |
| 657 | volatile struct gdb_exception except; |
| 658 | |
| 659 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 660 | { |
| 661 | opt = value_lazy (value); |
| 662 | } |
| 663 | GDB_PY_HANDLE_EXCEPTION (except); |
| 664 | |
| 665 | if (opt) |
| 666 | Py_RETURN_TRUE; |
| 667 | |
| 668 | Py_RETURN_FALSE; |
| 669 | } |
| 670 | |
| 671 | /* Implements gdb.Value.fetch_lazy (). */ |
| 672 | static PyObject * |
| 673 | valpy_fetch_lazy (PyObject *self, PyObject *args) |
| 674 | { |
| 675 | struct value *value = ((value_object *) self)->value; |
| 676 | volatile struct gdb_exception except; |
| 677 | |
| 678 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 679 | { |
| 680 | if (value_lazy (value)) |
| 681 | value_fetch_lazy (value); |
| 682 | } |
| 683 | GDB_PY_HANDLE_EXCEPTION (except); |
| 684 | |
| 685 | Py_RETURN_NONE; |
| 686 | } |
| 687 | |
| 688 | /* Calculate and return the address of the PyObject as the value of |
| 689 | the builtin __hash__ call. */ |
| 690 | static long |
| 691 | valpy_hash (PyObject *self) |
| 692 | { |
| 693 | return (long) (intptr_t) self; |
| 694 | } |
| 695 | |
| 696 | enum valpy_opcode |
| 697 | { |
| 698 | VALPY_ADD, |
| 699 | VALPY_SUB, |
| 700 | VALPY_MUL, |
| 701 | VALPY_DIV, |
| 702 | VALPY_REM, |
| 703 | VALPY_POW, |
| 704 | VALPY_LSH, |
| 705 | VALPY_RSH, |
| 706 | VALPY_BITAND, |
| 707 | VALPY_BITOR, |
| 708 | VALPY_BITXOR |
| 709 | }; |
| 710 | |
| 711 | /* If TYPE is a reference, return the target; otherwise return TYPE. */ |
| 712 | #define STRIP_REFERENCE(TYPE) \ |
| 713 | ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE)) |
| 714 | |
| 715 | /* Returns a value object which is the result of applying the operation |
| 716 | specified by OPCODE to the given arguments. Returns NULL on error, with |
| 717 | a python exception set. */ |
| 718 | static PyObject * |
| 719 | valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other) |
| 720 | { |
| 721 | volatile struct gdb_exception except; |
| 722 | PyObject *result = NULL; |
| 723 | |
| 724 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 725 | { |
| 726 | struct value *arg1, *arg2; |
| 727 | struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); |
| 728 | struct value *res_val = NULL; |
| 729 | |
| 730 | /* If the gdb.Value object is the second operand, then it will be passed |
| 731 | to us as the OTHER argument, and SELF will be an entirely different |
| 732 | kind of object, altogether. Because of this, we can't assume self is |
| 733 | a gdb.Value object and need to convert it from python as well. */ |
| 734 | arg1 = convert_value_from_python (self); |
| 735 | if (arg1 == NULL) |
| 736 | break; |
| 737 | |
| 738 | arg2 = convert_value_from_python (other); |
| 739 | if (arg2 == NULL) |
| 740 | break; |
| 741 | |
| 742 | switch (opcode) |
| 743 | { |
| 744 | case VALPY_ADD: |
| 745 | { |
| 746 | struct type *ltype = value_type (arg1); |
| 747 | struct type *rtype = value_type (arg2); |
| 748 | |
| 749 | CHECK_TYPEDEF (ltype); |
| 750 | ltype = STRIP_REFERENCE (ltype); |
| 751 | CHECK_TYPEDEF (rtype); |
| 752 | rtype = STRIP_REFERENCE (rtype); |
| 753 | |
| 754 | if (TYPE_CODE (ltype) == TYPE_CODE_PTR |
| 755 | && is_integral_type (rtype)) |
| 756 | res_val = value_ptradd (arg1, value_as_long (arg2)); |
| 757 | else if (TYPE_CODE (rtype) == TYPE_CODE_PTR |
| 758 | && is_integral_type (ltype)) |
| 759 | res_val = value_ptradd (arg2, value_as_long (arg1)); |
| 760 | else |
| 761 | res_val = value_binop (arg1, arg2, BINOP_ADD); |
| 762 | } |
| 763 | break; |
| 764 | case VALPY_SUB: |
| 765 | { |
| 766 | struct type *ltype = value_type (arg1); |
| 767 | struct type *rtype = value_type (arg2); |
| 768 | |
| 769 | CHECK_TYPEDEF (ltype); |
| 770 | ltype = STRIP_REFERENCE (ltype); |
| 771 | CHECK_TYPEDEF (rtype); |
| 772 | rtype = STRIP_REFERENCE (rtype); |
| 773 | |
| 774 | if (TYPE_CODE (ltype) == TYPE_CODE_PTR |
| 775 | && TYPE_CODE (rtype) == TYPE_CODE_PTR) |
| 776 | /* A ptrdiff_t for the target would be preferable here. */ |
| 777 | res_val = value_from_longest (builtin_type_pyint, |
| 778 | value_ptrdiff (arg1, arg2)); |
| 779 | else if (TYPE_CODE (ltype) == TYPE_CODE_PTR |
| 780 | && is_integral_type (rtype)) |
| 781 | res_val = value_ptradd (arg1, - value_as_long (arg2)); |
| 782 | else |
| 783 | res_val = value_binop (arg1, arg2, BINOP_SUB); |
| 784 | } |
| 785 | break; |
| 786 | case VALPY_MUL: |
| 787 | res_val = value_binop (arg1, arg2, BINOP_MUL); |
| 788 | break; |
| 789 | case VALPY_DIV: |
| 790 | res_val = value_binop (arg1, arg2, BINOP_DIV); |
| 791 | break; |
| 792 | case VALPY_REM: |
| 793 | res_val = value_binop (arg1, arg2, BINOP_REM); |
| 794 | break; |
| 795 | case VALPY_POW: |
| 796 | res_val = value_binop (arg1, arg2, BINOP_EXP); |
| 797 | break; |
| 798 | case VALPY_LSH: |
| 799 | res_val = value_binop (arg1, arg2, BINOP_LSH); |
| 800 | break; |
| 801 | case VALPY_RSH: |
| 802 | res_val = value_binop (arg1, arg2, BINOP_RSH); |
| 803 | break; |
| 804 | case VALPY_BITAND: |
| 805 | res_val = value_binop (arg1, arg2, BINOP_BITWISE_AND); |
| 806 | break; |
| 807 | case VALPY_BITOR: |
| 808 | res_val = value_binop (arg1, arg2, BINOP_BITWISE_IOR); |
| 809 | break; |
| 810 | case VALPY_BITXOR: |
| 811 | res_val = value_binop (arg1, arg2, BINOP_BITWISE_XOR); |
| 812 | break; |
| 813 | } |
| 814 | |
| 815 | if (res_val) |
| 816 | result = value_to_value_object (res_val); |
| 817 | |
| 818 | do_cleanups (cleanup); |
| 819 | } |
| 820 | GDB_PY_HANDLE_EXCEPTION (except); |
| 821 | |
| 822 | return result; |
| 823 | } |
| 824 | |
| 825 | static PyObject * |
| 826 | valpy_add (PyObject *self, PyObject *other) |
| 827 | { |
| 828 | return valpy_binop (VALPY_ADD, self, other); |
| 829 | } |
| 830 | |
| 831 | static PyObject * |
| 832 | valpy_subtract (PyObject *self, PyObject *other) |
| 833 | { |
| 834 | return valpy_binop (VALPY_SUB, self, other); |
| 835 | } |
| 836 | |
| 837 | static PyObject * |
| 838 | valpy_multiply (PyObject *self, PyObject *other) |
| 839 | { |
| 840 | return valpy_binop (VALPY_MUL, self, other); |
| 841 | } |
| 842 | |
| 843 | static PyObject * |
| 844 | valpy_divide (PyObject *self, PyObject *other) |
| 845 | { |
| 846 | return valpy_binop (VALPY_DIV, self, other); |
| 847 | } |
| 848 | |
| 849 | static PyObject * |
| 850 | valpy_remainder (PyObject *self, PyObject *other) |
| 851 | { |
| 852 | return valpy_binop (VALPY_REM, self, other); |
| 853 | } |
| 854 | |
| 855 | static PyObject * |
| 856 | valpy_power (PyObject *self, PyObject *other, PyObject *unused) |
| 857 | { |
| 858 | /* We don't support the ternary form of pow. I don't know how to express |
| 859 | that, so let's just throw NotImplementedError to at least do something |
| 860 | about it. */ |
| 861 | if (unused != Py_None) |
| 862 | { |
| 863 | PyErr_SetString (PyExc_NotImplementedError, |
| 864 | "Invalid operation on gdb.Value."); |
| 865 | return NULL; |
| 866 | } |
| 867 | |
| 868 | return valpy_binop (VALPY_POW, self, other); |
| 869 | } |
| 870 | |
| 871 | static PyObject * |
| 872 | valpy_negative (PyObject *self) |
| 873 | { |
| 874 | volatile struct gdb_exception except; |
| 875 | PyObject *result = NULL; |
| 876 | |
| 877 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 878 | { |
| 879 | /* Perhaps overkill, but consistency has some virtue. */ |
| 880 | struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); |
| 881 | struct value *val; |
| 882 | |
| 883 | val = value_neg (((value_object *) self)->value); |
| 884 | result = value_to_value_object (val); |
| 885 | do_cleanups (cleanup); |
| 886 | } |
| 887 | GDB_PY_HANDLE_EXCEPTION (except); |
| 888 | |
| 889 | return result; |
| 890 | } |
| 891 | |
| 892 | static PyObject * |
| 893 | valpy_positive (PyObject *self) |
| 894 | { |
| 895 | return value_to_value_object (((value_object *) self)->value); |
| 896 | } |
| 897 | |
| 898 | static PyObject * |
| 899 | valpy_absolute (PyObject *self) |
| 900 | { |
| 901 | struct value *value = ((value_object *) self)->value; |
| 902 | volatile struct gdb_exception except; |
| 903 | int isabs = 1; |
| 904 | |
| 905 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 906 | { |
| 907 | struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); |
| 908 | |
| 909 | if (value_less (value, value_zero (value_type (value), not_lval))) |
| 910 | isabs = 0; |
| 911 | |
| 912 | do_cleanups (cleanup); |
| 913 | } |
| 914 | GDB_PY_HANDLE_EXCEPTION (except); |
| 915 | |
| 916 | if (isabs) |
| 917 | return valpy_positive (self); |
| 918 | else |
| 919 | return valpy_negative (self); |
| 920 | } |
| 921 | |
| 922 | /* Implements boolean evaluation of gdb.Value. */ |
| 923 | static int |
| 924 | valpy_nonzero (PyObject *self) |
| 925 | { |
| 926 | volatile struct gdb_exception except; |
| 927 | value_object *self_value = (value_object *) self; |
| 928 | struct type *type; |
| 929 | int nonzero = 0; /* Appease GCC warning. */ |
| 930 | |
| 931 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 932 | { |
| 933 | type = check_typedef (value_type (self_value->value)); |
| 934 | |
| 935 | if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR) |
| 936 | nonzero = !!value_as_long (self_value->value); |
| 937 | else if (TYPE_CODE (type) == TYPE_CODE_FLT) |
| 938 | nonzero = value_as_double (self_value->value) != 0; |
| 939 | else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) |
| 940 | nonzero = !decimal_is_zero (value_contents (self_value->value), |
| 941 | TYPE_LENGTH (type), |
| 942 | gdbarch_byte_order (get_type_arch (type))); |
| 943 | else |
| 944 | /* All other values are True. */ |
| 945 | nonzero = 1; |
| 946 | } |
| 947 | /* This is not documented in the Python documentation, but if this |
| 948 | function fails, return -1 as slot_nb_nonzero does (the default |
| 949 | Python nonzero function). */ |
| 950 | GDB_PY_SET_HANDLE_EXCEPTION (except); |
| 951 | |
| 952 | return nonzero; |
| 953 | } |
| 954 | |
| 955 | /* Implements ~ for value objects. */ |
| 956 | static PyObject * |
| 957 | valpy_invert (PyObject *self) |
| 958 | { |
| 959 | struct value *val = NULL; |
| 960 | volatile struct gdb_exception except; |
| 961 | |
| 962 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 963 | { |
| 964 | val = value_complement (((value_object *) self)->value); |
| 965 | } |
| 966 | GDB_PY_HANDLE_EXCEPTION (except); |
| 967 | |
| 968 | return value_to_value_object (val); |
| 969 | } |
| 970 | |
| 971 | /* Implements left shift for value objects. */ |
| 972 | static PyObject * |
| 973 | valpy_lsh (PyObject *self, PyObject *other) |
| 974 | { |
| 975 | return valpy_binop (VALPY_LSH, self, other); |
| 976 | } |
| 977 | |
| 978 | /* Implements right shift for value objects. */ |
| 979 | static PyObject * |
| 980 | valpy_rsh (PyObject *self, PyObject *other) |
| 981 | { |
| 982 | return valpy_binop (VALPY_RSH, self, other); |
| 983 | } |
| 984 | |
| 985 | /* Implements bitwise and for value objects. */ |
| 986 | static PyObject * |
| 987 | valpy_and (PyObject *self, PyObject *other) |
| 988 | { |
| 989 | return valpy_binop (VALPY_BITAND, self, other); |
| 990 | } |
| 991 | |
| 992 | /* Implements bitwise or for value objects. */ |
| 993 | static PyObject * |
| 994 | valpy_or (PyObject *self, PyObject *other) |
| 995 | { |
| 996 | return valpy_binop (VALPY_BITOR, self, other); |
| 997 | } |
| 998 | |
| 999 | /* Implements bitwise xor for value objects. */ |
| 1000 | static PyObject * |
| 1001 | valpy_xor (PyObject *self, PyObject *other) |
| 1002 | { |
| 1003 | return valpy_binop (VALPY_BITXOR, self, other); |
| 1004 | } |
| 1005 | |
| 1006 | /* Implements comparison operations for value objects. Returns NULL on error, |
| 1007 | with a python exception set. */ |
| 1008 | static PyObject * |
| 1009 | valpy_richcompare (PyObject *self, PyObject *other, int op) |
| 1010 | { |
| 1011 | int result = 0; |
| 1012 | volatile struct gdb_exception except; |
| 1013 | |
| 1014 | if (other == Py_None) |
| 1015 | /* Comparing with None is special. From what I can tell, in Python |
| 1016 | None is smaller than anything else. */ |
| 1017 | switch (op) { |
| 1018 | case Py_LT: |
| 1019 | case Py_LE: |
| 1020 | case Py_EQ: |
| 1021 | Py_RETURN_FALSE; |
| 1022 | case Py_NE: |
| 1023 | case Py_GT: |
| 1024 | case Py_GE: |
| 1025 | Py_RETURN_TRUE; |
| 1026 | default: |
| 1027 | /* Can't happen. */ |
| 1028 | PyErr_SetString (PyExc_NotImplementedError, |
| 1029 | _("Invalid operation on gdb.Value.")); |
| 1030 | return NULL; |
| 1031 | } |
| 1032 | |
| 1033 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 1034 | { |
| 1035 | struct value *value_other, *mark = value_mark (); |
| 1036 | struct cleanup *cleanup; |
| 1037 | |
| 1038 | value_other = convert_value_from_python (other); |
| 1039 | if (value_other == NULL) |
| 1040 | { |
| 1041 | result = -1; |
| 1042 | break; |
| 1043 | } |
| 1044 | |
| 1045 | cleanup = make_cleanup_value_free_to_mark (mark); |
| 1046 | |
| 1047 | switch (op) { |
| 1048 | case Py_LT: |
| 1049 | result = value_less (((value_object *) self)->value, value_other); |
| 1050 | break; |
| 1051 | case Py_LE: |
| 1052 | result = value_less (((value_object *) self)->value, value_other) |
| 1053 | || value_equal (((value_object *) self)->value, value_other); |
| 1054 | break; |
| 1055 | case Py_EQ: |
| 1056 | result = value_equal (((value_object *) self)->value, value_other); |
| 1057 | break; |
| 1058 | case Py_NE: |
| 1059 | result = !value_equal (((value_object *) self)->value, value_other); |
| 1060 | break; |
| 1061 | case Py_GT: |
| 1062 | result = value_less (value_other, ((value_object *) self)->value); |
| 1063 | break; |
| 1064 | case Py_GE: |
| 1065 | result = value_less (value_other, ((value_object *) self)->value) |
| 1066 | || value_equal (((value_object *) self)->value, value_other); |
| 1067 | break; |
| 1068 | default: |
| 1069 | /* Can't happen. */ |
| 1070 | PyErr_SetString (PyExc_NotImplementedError, |
| 1071 | _("Invalid operation on gdb.Value.")); |
| 1072 | result = -1; |
| 1073 | break; |
| 1074 | } |
| 1075 | |
| 1076 | do_cleanups (cleanup); |
| 1077 | } |
| 1078 | GDB_PY_HANDLE_EXCEPTION (except); |
| 1079 | |
| 1080 | /* In this case, the Python exception has already been set. */ |
| 1081 | if (result < 0) |
| 1082 | return NULL; |
| 1083 | |
| 1084 | if (result == 1) |
| 1085 | Py_RETURN_TRUE; |
| 1086 | |
| 1087 | Py_RETURN_FALSE; |
| 1088 | } |
| 1089 | |
| 1090 | /* Helper function to determine if a type is "int-like". */ |
| 1091 | static int |
| 1092 | is_intlike (struct type *type, int ptr_ok) |
| 1093 | { |
| 1094 | return (TYPE_CODE (type) == TYPE_CODE_INT |
| 1095 | || TYPE_CODE (type) == TYPE_CODE_ENUM |
| 1096 | || TYPE_CODE (type) == TYPE_CODE_BOOL |
| 1097 | || TYPE_CODE (type) == TYPE_CODE_CHAR |
| 1098 | || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR)); |
| 1099 | } |
| 1100 | |
| 1101 | /* Implements conversion to int. */ |
| 1102 | static PyObject * |
| 1103 | valpy_int (PyObject *self) |
| 1104 | { |
| 1105 | struct value *value = ((value_object *) self)->value; |
| 1106 | struct type *type = value_type (value); |
| 1107 | LONGEST l = 0; |
| 1108 | volatile struct gdb_exception except; |
| 1109 | |
| 1110 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 1111 | { |
| 1112 | CHECK_TYPEDEF (type); |
| 1113 | if (!is_intlike (type, 0)) |
| 1114 | error (_("Cannot convert value to int.")); |
| 1115 | |
| 1116 | l = value_as_long (value); |
| 1117 | } |
| 1118 | GDB_PY_HANDLE_EXCEPTION (except); |
| 1119 | |
| 1120 | return gdb_py_object_from_longest (l); |
| 1121 | } |
| 1122 | |
| 1123 | /* Implements conversion to long. */ |
| 1124 | static PyObject * |
| 1125 | valpy_long (PyObject *self) |
| 1126 | { |
| 1127 | struct value *value = ((value_object *) self)->value; |
| 1128 | struct type *type = value_type (value); |
| 1129 | LONGEST l = 0; |
| 1130 | volatile struct gdb_exception except; |
| 1131 | |
| 1132 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 1133 | { |
| 1134 | CHECK_TYPEDEF (type); |
| 1135 | |
| 1136 | if (!is_intlike (type, 1)) |
| 1137 | error (_("Cannot convert value to long.")); |
| 1138 | |
| 1139 | l = value_as_long (value); |
| 1140 | } |
| 1141 | GDB_PY_HANDLE_EXCEPTION (except); |
| 1142 | |
| 1143 | return gdb_py_long_from_longest (l); |
| 1144 | } |
| 1145 | |
| 1146 | /* Implements conversion to float. */ |
| 1147 | static PyObject * |
| 1148 | valpy_float (PyObject *self) |
| 1149 | { |
| 1150 | struct value *value = ((value_object *) self)->value; |
| 1151 | struct type *type = value_type (value); |
| 1152 | double d = 0; |
| 1153 | volatile struct gdb_exception except; |
| 1154 | |
| 1155 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 1156 | { |
| 1157 | CHECK_TYPEDEF (type); |
| 1158 | |
| 1159 | if (TYPE_CODE (type) != TYPE_CODE_FLT) |
| 1160 | error (_("Cannot convert value to float.")); |
| 1161 | |
| 1162 | d = value_as_double (value); |
| 1163 | } |
| 1164 | GDB_PY_HANDLE_EXCEPTION (except); |
| 1165 | |
| 1166 | return PyFloat_FromDouble (d); |
| 1167 | } |
| 1168 | |
| 1169 | /* Returns an object for a value which is released from the all_values chain, |
| 1170 | so its lifetime is not bound to the execution of a command. */ |
| 1171 | PyObject * |
| 1172 | value_to_value_object (struct value *val) |
| 1173 | { |
| 1174 | value_object *val_obj; |
| 1175 | |
| 1176 | val_obj = PyObject_New (value_object, &value_object_type); |
| 1177 | if (val_obj != NULL) |
| 1178 | { |
| 1179 | val_obj->value = val; |
| 1180 | release_value_or_incref (val); |
| 1181 | val_obj->address = NULL; |
| 1182 | val_obj->type = NULL; |
| 1183 | val_obj->dynamic_type = NULL; |
| 1184 | note_value (val_obj); |
| 1185 | } |
| 1186 | |
| 1187 | return (PyObject *) val_obj; |
| 1188 | } |
| 1189 | |
| 1190 | /* Returns a borrowed reference to the struct value corresponding to |
| 1191 | the given value object. */ |
| 1192 | struct value * |
| 1193 | value_object_to_value (PyObject *self) |
| 1194 | { |
| 1195 | value_object *real; |
| 1196 | |
| 1197 | if (! PyObject_TypeCheck (self, &value_object_type)) |
| 1198 | return NULL; |
| 1199 | real = (value_object *) self; |
| 1200 | return real->value; |
| 1201 | } |
| 1202 | |
| 1203 | /* Try to convert a Python value to a gdb value. If the value cannot |
| 1204 | be converted, set a Python exception and return NULL. Returns a |
| 1205 | reference to a new value on the all_values chain. */ |
| 1206 | |
| 1207 | struct value * |
| 1208 | convert_value_from_python (PyObject *obj) |
| 1209 | { |
| 1210 | struct value *value = NULL; /* -Wall */ |
| 1211 | struct cleanup *old; |
| 1212 | volatile struct gdb_exception except; |
| 1213 | int cmp; |
| 1214 | |
| 1215 | gdb_assert (obj != NULL); |
| 1216 | |
| 1217 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 1218 | { |
| 1219 | if (PyBool_Check (obj)) |
| 1220 | { |
| 1221 | cmp = PyObject_IsTrue (obj); |
| 1222 | if (cmp >= 0) |
| 1223 | value = value_from_longest (builtin_type_pybool, cmp); |
| 1224 | } |
| 1225 | else if (PyInt_Check (obj)) |
| 1226 | { |
| 1227 | long l = PyInt_AsLong (obj); |
| 1228 | |
| 1229 | if (! PyErr_Occurred ()) |
| 1230 | value = value_from_longest (builtin_type_pyint, l); |
| 1231 | } |
| 1232 | else if (PyLong_Check (obj)) |
| 1233 | { |
| 1234 | LONGEST l = PyLong_AsLongLong (obj); |
| 1235 | |
| 1236 | if (PyErr_Occurred ()) |
| 1237 | { |
| 1238 | /* If the error was an overflow, we can try converting to |
| 1239 | ULONGEST instead. */ |
| 1240 | if (PyErr_ExceptionMatches (PyExc_OverflowError)) |
| 1241 | { |
| 1242 | PyObject *etype, *evalue, *etraceback, *zero; |
| 1243 | |
| 1244 | PyErr_Fetch (&etype, &evalue, &etraceback); |
| 1245 | zero = PyInt_FromLong (0); |
| 1246 | |
| 1247 | /* Check whether obj is positive. */ |
| 1248 | if (PyObject_RichCompareBool (obj, zero, Py_GT) > 0) |
| 1249 | { |
| 1250 | ULONGEST ul; |
| 1251 | |
| 1252 | ul = PyLong_AsUnsignedLongLong (obj); |
| 1253 | if (! PyErr_Occurred ()) |
| 1254 | value = value_from_ulongest (builtin_type_upylong, ul); |
| 1255 | } |
| 1256 | else |
| 1257 | /* There's nothing we can do. */ |
| 1258 | PyErr_Restore (etype, evalue, etraceback); |
| 1259 | |
| 1260 | Py_DECREF (zero); |
| 1261 | } |
| 1262 | } |
| 1263 | else |
| 1264 | value = value_from_longest (builtin_type_pylong, l); |
| 1265 | } |
| 1266 | else if (PyFloat_Check (obj)) |
| 1267 | { |
| 1268 | double d = PyFloat_AsDouble (obj); |
| 1269 | |
| 1270 | if (! PyErr_Occurred ()) |
| 1271 | value = value_from_double (builtin_type_pyfloat, d); |
| 1272 | } |
| 1273 | else if (gdbpy_is_string (obj)) |
| 1274 | { |
| 1275 | char *s; |
| 1276 | |
| 1277 | s = python_string_to_target_string (obj); |
| 1278 | if (s != NULL) |
| 1279 | { |
| 1280 | old = make_cleanup (xfree, s); |
| 1281 | value = value_cstring (s, strlen (s), builtin_type_pychar); |
| 1282 | do_cleanups (old); |
| 1283 | } |
| 1284 | } |
| 1285 | else if (PyObject_TypeCheck (obj, &value_object_type)) |
| 1286 | value = value_copy (((value_object *) obj)->value); |
| 1287 | else if (gdbpy_is_lazy_string (obj)) |
| 1288 | { |
| 1289 | PyObject *result; |
| 1290 | |
| 1291 | result = PyObject_CallMethodObjArgs (obj, gdbpy_value_cst, NULL); |
| 1292 | value = value_copy (((value_object *) result)->value); |
| 1293 | } |
| 1294 | else |
| 1295 | PyErr_Format (PyExc_TypeError, |
| 1296 | _("Could not convert Python object: %s."), |
| 1297 | PyString_AsString (PyObject_Str (obj))); |
| 1298 | } |
| 1299 | if (except.reason < 0) |
| 1300 | { |
| 1301 | PyErr_Format (except.reason == RETURN_QUIT |
| 1302 | ? PyExc_KeyboardInterrupt : PyExc_RuntimeError, |
| 1303 | "%s", except.message); |
| 1304 | return NULL; |
| 1305 | } |
| 1306 | |
| 1307 | return value; |
| 1308 | } |
| 1309 | |
| 1310 | /* Returns value object in the ARGth position in GDB's history. */ |
| 1311 | PyObject * |
| 1312 | gdbpy_history (PyObject *self, PyObject *args) |
| 1313 | { |
| 1314 | int i; |
| 1315 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ |
| 1316 | volatile struct gdb_exception except; |
| 1317 | |
| 1318 | if (!PyArg_ParseTuple (args, "i", &i)) |
| 1319 | return NULL; |
| 1320 | |
| 1321 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 1322 | { |
| 1323 | res_val = access_value_history (i); |
| 1324 | } |
| 1325 | GDB_PY_HANDLE_EXCEPTION (except); |
| 1326 | |
| 1327 | return value_to_value_object (res_val); |
| 1328 | } |
| 1329 | |
| 1330 | /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */ |
| 1331 | |
| 1332 | int |
| 1333 | gdbpy_is_value_object (PyObject *obj) |
| 1334 | { |
| 1335 | return PyObject_TypeCheck (obj, &value_object_type); |
| 1336 | } |
| 1337 | |
| 1338 | void |
| 1339 | gdbpy_initialize_values (void) |
| 1340 | { |
| 1341 | if (PyType_Ready (&value_object_type) < 0) |
| 1342 | return; |
| 1343 | |
| 1344 | Py_INCREF (&value_object_type); |
| 1345 | PyModule_AddObject (gdb_module, "Value", (PyObject *) &value_object_type); |
| 1346 | |
| 1347 | values_in_python = NULL; |
| 1348 | } |
| 1349 | |
| 1350 | \f |
| 1351 | |
| 1352 | static PyGetSetDef value_object_getset[] = { |
| 1353 | { "address", valpy_get_address, NULL, "The address of the value.", |
| 1354 | NULL }, |
| 1355 | { "is_optimized_out", valpy_get_is_optimized_out, NULL, |
| 1356 | "Boolean telling whether the value is optimized " |
| 1357 | "out (i.e., not available).", |
| 1358 | NULL }, |
| 1359 | { "type", valpy_get_type, NULL, "Type of the value.", NULL }, |
| 1360 | { "dynamic_type", valpy_get_dynamic_type, NULL, |
| 1361 | "Dynamic type of the value.", NULL }, |
| 1362 | { "is_lazy", valpy_get_is_lazy, NULL, |
| 1363 | "Boolean telling whether the value is lazy (not fetched yet\n\ |
| 1364 | from the inferior). A lazy value is fetched when needed, or when\n\ |
| 1365 | the \"fetch_lazy()\" method is called.", NULL }, |
| 1366 | {NULL} /* Sentinel */ |
| 1367 | }; |
| 1368 | |
| 1369 | static PyMethodDef value_object_methods[] = { |
| 1370 | { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." }, |
| 1371 | { "dynamic_cast", valpy_dynamic_cast, METH_VARARGS, |
| 1372 | "dynamic_cast (gdb.Type) -> gdb.Value\n\ |
| 1373 | Cast the value to the supplied type, as if by the C++ dynamic_cast operator." |
| 1374 | }, |
| 1375 | { "reinterpret_cast", valpy_reinterpret_cast, METH_VARARGS, |
| 1376 | "reinterpret_cast (gdb.Type) -> gdb.Value\n\ |
| 1377 | Cast the value to the supplied type, as if by the C++\n\ |
| 1378 | reinterpret_cast operator." |
| 1379 | }, |
| 1380 | { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." }, |
| 1381 | { "lazy_string", (PyCFunction) valpy_lazy_string, |
| 1382 | METH_VARARGS | METH_KEYWORDS, |
| 1383 | "lazy_string ([encoding] [, length]) -> lazy_string\n\ |
| 1384 | Return a lazy string representation of the value." }, |
| 1385 | { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS, |
| 1386 | "string ([encoding] [, errors] [, length]) -> string\n\ |
| 1387 | Return Unicode string representation of the value." }, |
| 1388 | { "fetch_lazy", valpy_fetch_lazy, METH_NOARGS, |
| 1389 | "Fetches the value from the inferior, if it was lazy." }, |
| 1390 | {NULL} /* Sentinel */ |
| 1391 | }; |
| 1392 | |
| 1393 | static PyNumberMethods value_object_as_number = { |
| 1394 | valpy_add, |
| 1395 | valpy_subtract, |
| 1396 | valpy_multiply, |
| 1397 | valpy_divide, |
| 1398 | valpy_remainder, |
| 1399 | NULL, /* nb_divmod */ |
| 1400 | valpy_power, /* nb_power */ |
| 1401 | valpy_negative, /* nb_negative */ |
| 1402 | valpy_positive, /* nb_positive */ |
| 1403 | valpy_absolute, /* nb_absolute */ |
| 1404 | valpy_nonzero, /* nb_nonzero */ |
| 1405 | valpy_invert, /* nb_invert */ |
| 1406 | valpy_lsh, /* nb_lshift */ |
| 1407 | valpy_rsh, /* nb_rshift */ |
| 1408 | valpy_and, /* nb_and */ |
| 1409 | valpy_xor, /* nb_xor */ |
| 1410 | valpy_or, /* nb_or */ |
| 1411 | NULL, /* nb_coerce */ |
| 1412 | valpy_int, /* nb_int */ |
| 1413 | valpy_long, /* nb_long */ |
| 1414 | valpy_float, /* nb_float */ |
| 1415 | NULL, /* nb_oct */ |
| 1416 | NULL /* nb_hex */ |
| 1417 | }; |
| 1418 | |
| 1419 | static PyMappingMethods value_object_as_mapping = { |
| 1420 | valpy_length, |
| 1421 | valpy_getitem, |
| 1422 | valpy_setitem |
| 1423 | }; |
| 1424 | |
| 1425 | PyTypeObject value_object_type = { |
| 1426 | PyObject_HEAD_INIT (NULL) |
| 1427 | 0, /*ob_size*/ |
| 1428 | "gdb.Value", /*tp_name*/ |
| 1429 | sizeof (value_object), /*tp_basicsize*/ |
| 1430 | 0, /*tp_itemsize*/ |
| 1431 | valpy_dealloc, /*tp_dealloc*/ |
| 1432 | 0, /*tp_print*/ |
| 1433 | 0, /*tp_getattr*/ |
| 1434 | 0, /*tp_setattr*/ |
| 1435 | 0, /*tp_compare*/ |
| 1436 | 0, /*tp_repr*/ |
| 1437 | &value_object_as_number, /*tp_as_number*/ |
| 1438 | 0, /*tp_as_sequence*/ |
| 1439 | &value_object_as_mapping, /*tp_as_mapping*/ |
| 1440 | valpy_hash, /*tp_hash*/ |
| 1441 | valpy_call, /*tp_call*/ |
| 1442 | valpy_str, /*tp_str*/ |
| 1443 | 0, /*tp_getattro*/ |
| 1444 | 0, /*tp_setattro*/ |
| 1445 | 0, /*tp_as_buffer*/ |
| 1446 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
| 1447 | | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
| 1448 | "GDB value object", /* tp_doc */ |
| 1449 | 0, /* tp_traverse */ |
| 1450 | 0, /* tp_clear */ |
| 1451 | valpy_richcompare, /* tp_richcompare */ |
| 1452 | 0, /* tp_weaklistoffset */ |
| 1453 | 0, /* tp_iter */ |
| 1454 | 0, /* tp_iternext */ |
| 1455 | value_object_methods, /* tp_methods */ |
| 1456 | 0, /* tp_members */ |
| 1457 | value_object_getset, /* tp_getset */ |
| 1458 | 0, /* tp_base */ |
| 1459 | 0, /* tp_dict */ |
| 1460 | 0, /* tp_descr_get */ |
| 1461 | 0, /* tp_descr_set */ |
| 1462 | 0, /* tp_dictoffset */ |
| 1463 | 0, /* tp_init */ |
| 1464 | 0, /* tp_alloc */ |
| 1465 | valpy_new /* tp_new */ |
| 1466 | }; |
| 1467 | |
| 1468 | #else |
| 1469 | |
| 1470 | void |
| 1471 | preserve_python_values (struct objfile *objfile, htab_t copied_types) |
| 1472 | { |
| 1473 | /* Nothing. */ |
| 1474 | } |
| 1475 | |
| 1476 | #endif /* HAVE_PYTHON */ |