1 /* Python interface to values.
3 Copyright (C) 2008-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
27 #include "expression.h"
31 #include "python-internal.h"
33 /* Even though Python scalar types directly map to host types, we use
34 target types here to remain consistent with the values system in
35 GDB (which uses target arithmetic). */
37 /* Python's integer type corresponds to C's long type. */
38 #define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long
40 /* Python's float type corresponds to C's double type. */
41 #define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double
43 /* Python's long type corresponds to C's long long type. */
44 #define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long
46 /* Python's long type corresponds to C's long long type. Unsigned version. */
47 #define builtin_type_upylong builtin_type \
48 (python_gdbarch)->builtin_unsigned_long_long
50 #define builtin_type_pybool \
51 language_bool_type (python_language, python_gdbarch)
53 #define builtin_type_pychar \
54 language_string_char_type (python_language, python_gdbarch)
56 typedef struct value_object
{
58 struct value_object
*next
;
59 struct value_object
*prev
;
63 PyObject
*dynamic_type
;
66 /* List of all values which are currently exposed to Python. It is
67 maintained so that when an objfile is discarded, preserve_values
68 can copy the values' types if needed. */
69 /* This variable is unnecessarily initialized to NULL in order to
70 work around a linker bug on MacOS. */
71 static value_object
*values_in_python
= NULL
;
73 /* Called by the Python interpreter when deallocating a value object. */
75 valpy_dealloc (PyObject
*obj
)
77 value_object
*self
= (value_object
*) obj
;
79 /* Remove SELF from the global list. */
81 self
->prev
->next
= self
->next
;
84 gdb_assert (values_in_python
== self
);
85 values_in_python
= self
->next
;
88 self
->next
->prev
= self
->prev
;
90 value_free (self
->value
);
93 /* Use braces to appease gcc warning. *sigh* */
95 Py_DECREF (self
->address
);
100 Py_DECREF (self
->type
);
103 Py_XDECREF (self
->dynamic_type
);
105 Py_TYPE (self
)->tp_free (self
);
108 /* Helper to push a Value object on the global list. */
110 note_value (value_object
*value_obj
)
112 value_obj
->next
= values_in_python
;
114 value_obj
->next
->prev
= value_obj
;
115 value_obj
->prev
= NULL
;
116 values_in_python
= value_obj
;
119 /* Called when a new gdb.Value object needs to be allocated. Returns NULL on
120 error, with a python exception set. */
122 valpy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*keywords
)
124 struct value
*value
= NULL
; /* Initialize to appease gcc warning. */
125 value_object
*value_obj
;
127 if (PyTuple_Size (args
) != 1)
129 PyErr_SetString (PyExc_TypeError
, _("Value object creation takes only "
134 value_obj
= (value_object
*) subtype
->tp_alloc (subtype
, 1);
135 if (value_obj
== NULL
)
137 PyErr_SetString (PyExc_MemoryError
, _("Could not allocate memory to "
138 "create Value object."));
142 value
= convert_value_from_python (PyTuple_GetItem (args
, 0));
145 subtype
->tp_free (value_obj
);
149 value_obj
->value
= value
;
150 release_value_or_incref (value
);
151 value_obj
->address
= NULL
;
152 value_obj
->type
= NULL
;
153 value_obj
->dynamic_type
= NULL
;
154 note_value (value_obj
);
156 return (PyObject
*) value_obj
;
159 /* Iterate over all the Value objects, calling preserve_one_value on
162 gdbpy_preserve_values (const struct extension_language_defn
*extlang
,
163 struct objfile
*objfile
, htab_t copied_types
)
167 for (iter
= values_in_python
; iter
; iter
= iter
->next
)
168 preserve_one_value (iter
->value
, objfile
, copied_types
);
171 /* Given a value of a pointer type, apply the C unary * operator to it. */
173 valpy_dereference (PyObject
*self
, PyObject
*args
)
175 PyObject
*result
= NULL
;
179 struct value
*res_val
;
180 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
182 res_val
= value_ind (((value_object
*) self
)->value
);
183 result
= value_to_value_object (res_val
);
184 do_cleanups (cleanup
);
186 CATCH (except
, RETURN_MASK_ALL
)
188 GDB_PY_HANDLE_EXCEPTION (except
);
195 /* Given a value of a pointer type or a reference type, return the value
196 referenced. The difference between this function and valpy_dereference is
197 that the latter applies * unary operator to a value, which need not always
198 result in the value referenced. For example, for a value which is a reference
199 to an 'int' pointer ('int *'), valpy_dereference will result in a value of
200 type 'int' while valpy_referenced_value will result in a value of type
204 valpy_referenced_value (PyObject
*self
, PyObject
*args
)
206 PyObject
*result
= NULL
;
210 struct value
*self_val
, *res_val
;
211 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
213 self_val
= ((value_object
*) self
)->value
;
214 switch (TYPE_CODE (check_typedef (value_type (self_val
))))
217 res_val
= value_ind (self_val
);
220 res_val
= coerce_ref (self_val
);
223 error(_("Trying to get the referenced value from a value which is "
224 "neither a pointer nor a reference."));
227 result
= value_to_value_object (res_val
);
228 do_cleanups (cleanup
);
230 CATCH (except
, RETURN_MASK_ALL
)
232 GDB_PY_HANDLE_EXCEPTION (except
);
239 /* Return "&value". */
241 valpy_get_address (PyObject
*self
, void *closure
)
243 value_object
*val_obj
= (value_object
*) self
;
245 if (!val_obj
->address
)
249 struct value
*res_val
;
250 struct cleanup
*cleanup
251 = make_cleanup_value_free_to_mark (value_mark ());
253 res_val
= value_addr (val_obj
->value
);
254 val_obj
->address
= value_to_value_object (res_val
);
255 do_cleanups (cleanup
);
257 CATCH (except
, RETURN_MASK_ALL
)
259 val_obj
->address
= Py_None
;
265 Py_XINCREF (val_obj
->address
);
267 return val_obj
->address
;
270 /* Return type of the value. */
272 valpy_get_type (PyObject
*self
, void *closure
)
274 value_object
*obj
= (value_object
*) self
;
278 obj
->type
= type_to_type_object (value_type (obj
->value
));
282 Py_INCREF (obj
->type
);
286 /* Return dynamic type of the value. */
289 valpy_get_dynamic_type (PyObject
*self
, void *closure
)
291 value_object
*obj
= (value_object
*) self
;
292 struct type
*type
= NULL
;
294 if (obj
->dynamic_type
!= NULL
)
296 Py_INCREF (obj
->dynamic_type
);
297 return obj
->dynamic_type
;
302 struct value
*val
= obj
->value
;
303 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
305 type
= value_type (val
);
306 CHECK_TYPEDEF (type
);
308 if (((TYPE_CODE (type
) == TYPE_CODE_PTR
)
309 || (TYPE_CODE (type
) == TYPE_CODE_REF
))
310 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_STRUCT
))
312 struct value
*target
;
313 int was_pointer
= TYPE_CODE (type
) == TYPE_CODE_PTR
;
316 target
= value_ind (val
);
318 target
= coerce_ref (val
);
319 type
= value_rtti_type (target
, NULL
, NULL
, NULL
);
324 type
= lookup_pointer_type (type
);
326 type
= lookup_reference_type (type
);
329 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
330 type
= value_rtti_type (val
, NULL
, NULL
, NULL
);
333 /* Re-use object's static type. */
337 do_cleanups (cleanup
);
339 CATCH (except
, RETURN_MASK_ALL
)
341 GDB_PY_HANDLE_EXCEPTION (except
);
346 obj
->dynamic_type
= valpy_get_type (self
, NULL
);
348 obj
->dynamic_type
= type_to_type_object (type
);
350 Py_XINCREF (obj
->dynamic_type
);
351 return obj
->dynamic_type
;
354 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
355 string. Return a PyObject representing a lazy_string_object type.
356 A lazy string is a pointer to a string with an optional encoding and
357 length. If ENCODING is not given, encoding is set to None. If an
358 ENCODING is provided the encoding parameter is set to ENCODING, but
359 the string is not encoded. If LENGTH is provided then the length
360 parameter is set to LENGTH, otherwise length will be set to -1 (first
361 null of appropriate with). */
363 valpy_lazy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
365 gdb_py_longest length
= -1;
366 struct value
*value
= ((value_object
*) self
)->value
;
367 const char *user_encoding
= NULL
;
368 static char *keywords
[] = { "encoding", "length", NULL
};
369 PyObject
*str_obj
= NULL
;
371 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|s" GDB_PY_LL_ARG
, keywords
,
372 &user_encoding
, &length
))
377 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
379 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_PTR
)
380 value
= value_ind (value
);
382 str_obj
= gdbpy_create_lazy_string_object (value_address (value
), length
,
386 do_cleanups (cleanup
);
388 CATCH (except
, RETURN_MASK_ALL
)
390 GDB_PY_HANDLE_EXCEPTION (except
);
397 /* Implementation of gdb.Value.string ([encoding] [, errors]
398 [, length]) -> string. Return Unicode string with value contents.
399 If ENCODING is not given, the string is assumed to be encoded in
400 the target's charset. If LENGTH is provided, only fetch string to
401 the length provided. */
404 valpy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
408 struct value
*value
= ((value_object
*) self
)->value
;
410 const char *encoding
= NULL
;
411 const char *errors
= NULL
;
412 const char *user_encoding
= NULL
;
413 const char *la_encoding
= NULL
;
414 struct type
*char_type
;
415 static char *keywords
[] = { "encoding", "errors", "length", NULL
};
417 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|ssi", keywords
,
418 &user_encoding
, &errors
, &length
))
423 LA_GET_STRING (value
, &buffer
, &length
, &char_type
, &la_encoding
);
425 CATCH (except
, RETURN_MASK_ALL
)
427 GDB_PY_HANDLE_EXCEPTION (except
);
431 encoding
= (user_encoding
&& *user_encoding
) ? user_encoding
: la_encoding
;
432 unicode
= PyUnicode_Decode ((const char *) buffer
,
433 length
* TYPE_LENGTH (char_type
),
440 /* A helper function that implements the various cast operators. */
443 valpy_do_cast (PyObject
*self
, PyObject
*args
, enum exp_opcode op
)
445 PyObject
*type_obj
, *result
= NULL
;
448 if (! PyArg_ParseTuple (args
, "O", &type_obj
))
451 type
= type_object_to_type (type_obj
);
454 PyErr_SetString (PyExc_RuntimeError
,
455 _("Argument must be a type."));
461 struct value
*val
= ((value_object
*) self
)->value
;
462 struct value
*res_val
;
463 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
465 if (op
== UNOP_DYNAMIC_CAST
)
466 res_val
= value_dynamic_cast (type
, val
);
467 else if (op
== UNOP_REINTERPRET_CAST
)
468 res_val
= value_reinterpret_cast (type
, val
);
471 gdb_assert (op
== UNOP_CAST
);
472 res_val
= value_cast (type
, val
);
475 result
= value_to_value_object (res_val
);
476 do_cleanups (cleanup
);
478 CATCH (except
, RETURN_MASK_ALL
)
480 GDB_PY_HANDLE_EXCEPTION (except
);
487 /* Implementation of the "cast" method. */
490 valpy_cast (PyObject
*self
, PyObject
*args
)
492 return valpy_do_cast (self
, args
, UNOP_CAST
);
495 /* Implementation of the "dynamic_cast" method. */
498 valpy_dynamic_cast (PyObject
*self
, PyObject
*args
)
500 return valpy_do_cast (self
, args
, UNOP_DYNAMIC_CAST
);
503 /* Implementation of the "reinterpret_cast" method. */
506 valpy_reinterpret_cast (PyObject
*self
, PyObject
*args
)
508 return valpy_do_cast (self
, args
, UNOP_REINTERPRET_CAST
);
512 valpy_length (PyObject
*self
)
514 /* We don't support getting the number of elements in a struct / class. */
515 PyErr_SetString (PyExc_NotImplementedError
,
516 _("Invalid operation on gdb.Value."));
520 /* Return 1 if the gdb.Field object FIELD is present in the value V.
521 Returns 0 otherwise. If any Python error occurs, -1 is returned. */
524 value_has_field (struct value
*v
, PyObject
*field
)
526 struct type
*parent_type
, *val_type
;
527 enum type_code type_code
;
528 PyObject
*type_object
= PyObject_GetAttrString (field
, "parent_type");
531 if (type_object
== NULL
)
534 parent_type
= type_object_to_type (type_object
);
535 Py_DECREF (type_object
);
536 if (parent_type
== NULL
)
538 PyErr_SetString (PyExc_TypeError
,
539 _("'parent_type' attribute of gdb.Field object is not a"
540 "gdb.Type object."));
546 val_type
= value_type (v
);
547 val_type
= check_typedef (val_type
);
548 if (TYPE_CODE (val_type
) == TYPE_CODE_REF
549 || TYPE_CODE (val_type
) == TYPE_CODE_PTR
)
550 val_type
= check_typedef (TYPE_TARGET_TYPE (val_type
));
552 type_code
= TYPE_CODE (val_type
);
553 if ((type_code
== TYPE_CODE_STRUCT
|| type_code
== TYPE_CODE_UNION
)
554 && types_equal (val_type
, parent_type
))
559 CATCH (except
, RETURN_MASK_ALL
)
561 GDB_PY_SET_HANDLE_EXCEPTION (except
);
568 /* Return the value of a flag FLAG_NAME in a gdb.Field object FIELD.
569 Returns 1 if the flag value is true, 0 if it is false, and -1 if
570 a Python error occurs. */
573 get_field_flag (PyObject
*field
, const char *flag_name
)
576 PyObject
*flag_object
= PyObject_GetAttrString (field
, flag_name
);
578 if (flag_object
== NULL
)
581 flag_value
= PyObject_IsTrue (flag_object
);
582 Py_DECREF (flag_object
);
587 /* Return the "type" attribute of a gdb.Field object.
588 Returns NULL on error, with a Python exception set. */
591 get_field_type (PyObject
*field
)
593 PyObject
*ftype_obj
= PyObject_GetAttrString (field
, "type");
596 if (ftype_obj
== NULL
)
598 ftype
= type_object_to_type (ftype_obj
);
599 Py_DECREF (ftype_obj
);
601 PyErr_SetString (PyExc_TypeError
,
602 _("'type' attribute of gdb.Field object is not a "
603 "gdb.Type object."));
608 /* Given string name or a gdb.Field object corresponding to an element inside
609 a structure, return its value object. Returns NULL on error, with a python
613 valpy_getitem (PyObject
*self
, PyObject
*key
)
615 struct gdb_exception except
= exception_none
;
616 value_object
*self_value
= (value_object
*) self
;
618 struct type
*base_class_type
= NULL
, *field_type
= NULL
;
620 PyObject
*result
= NULL
;
622 if (gdbpy_is_string (key
))
624 field
= python_string_to_host_string (key
);
628 else if (gdbpy_is_field (key
))
630 int is_base_class
, valid_field
;
632 valid_field
= value_has_field (self_value
->value
, key
);
635 else if (valid_field
== 0)
637 PyErr_SetString (PyExc_TypeError
,
638 _("Invalid lookup for a field not contained in "
644 is_base_class
= get_field_flag (key
, "is_base_class");
645 if (is_base_class
< 0)
647 else if (is_base_class
> 0)
649 base_class_type
= get_field_type (key
);
650 if (base_class_type
== NULL
)
655 PyObject
*name_obj
= PyObject_GetAttrString (key
, "name");
657 if (name_obj
== NULL
)
660 if (name_obj
!= Py_None
)
662 field
= python_string_to_host_string (name_obj
);
663 Py_DECREF (name_obj
);
669 PyObject
*bitpos_obj
;
672 Py_DECREF (name_obj
);
674 if (!PyObject_HasAttrString (key
, "bitpos"))
676 PyErr_SetString (PyExc_AttributeError
,
677 _("gdb.Field object has no name and no "
678 "'bitpos' attribute."));
682 bitpos_obj
= PyObject_GetAttrString (key
, "bitpos");
683 if (bitpos_obj
== NULL
)
685 valid
= gdb_py_int_as_long (bitpos_obj
, &bitpos
);
686 Py_DECREF (bitpos_obj
);
690 field_type
= get_field_type (key
);
691 if (field_type
== NULL
)
699 struct value
*tmp
= self_value
->value
;
700 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
701 struct value
*res_val
= NULL
;
704 res_val
= value_struct_elt (&tmp
, NULL
, field
, 0, NULL
);
705 else if (bitpos
>= 0)
706 res_val
= value_struct_elt_bitpos (&tmp
, bitpos
, field_type
,
707 "struct/class/union");
708 else if (base_class_type
!= NULL
)
710 struct type
*val_type
;
712 val_type
= check_typedef (value_type (tmp
));
713 if (TYPE_CODE (val_type
) == TYPE_CODE_PTR
)
714 res_val
= value_cast (lookup_pointer_type (base_class_type
), tmp
);
715 else if (TYPE_CODE (val_type
) == TYPE_CODE_REF
)
716 res_val
= value_cast (lookup_reference_type (base_class_type
), tmp
);
718 res_val
= value_cast (base_class_type
, tmp
);
722 /* Assume we are attempting an array access, and let the
723 value code throw an exception if the index has an invalid
725 struct value
*idx
= convert_value_from_python (key
);
729 /* Check the value's type is something that can be accessed via
733 tmp
= coerce_ref (tmp
);
734 type
= check_typedef (value_type (tmp
));
735 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
736 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
737 error (_("Cannot subscript requested type."));
739 res_val
= value_subscript (tmp
, value_as_long (idx
));
744 result
= value_to_value_object (res_val
);
745 do_cleanups (cleanup
);
747 CATCH (ex
, RETURN_MASK_ALL
)
754 GDB_PY_HANDLE_EXCEPTION (except
);
760 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
762 PyErr_Format (PyExc_NotImplementedError
,
763 _("Setting of struct elements is not currently supported."));
767 /* Called by the Python interpreter to perform an inferior function
768 call on the value. Returns NULL on error, with a python exception set. */
770 valpy_call (PyObject
*self
, PyObject
*args
, PyObject
*keywords
)
772 Py_ssize_t args_count
;
773 struct value
*function
= ((value_object
*) self
)->value
;
774 struct value
**vargs
= NULL
;
775 struct type
*ftype
= NULL
;
776 struct value
*mark
= value_mark ();
777 PyObject
*result
= NULL
;
781 ftype
= check_typedef (value_type (function
));
783 CATCH (except
, RETURN_MASK_ALL
)
785 GDB_PY_HANDLE_EXCEPTION (except
);
789 if (TYPE_CODE (ftype
) != TYPE_CODE_FUNC
)
791 PyErr_SetString (PyExc_RuntimeError
,
792 _("Value is not callable (not TYPE_CODE_FUNC)."));
796 if (! PyTuple_Check (args
))
798 PyErr_SetString (PyExc_TypeError
,
799 _("Inferior arguments must be provided in a tuple."));
803 args_count
= PyTuple_Size (args
);
808 vargs
= alloca (sizeof (struct value
*) * args_count
);
809 for (i
= 0; i
< args_count
; i
++)
811 PyObject
*item
= PyTuple_GetItem (args
, i
);
816 vargs
[i
] = convert_value_from_python (item
);
817 if (vargs
[i
] == NULL
)
824 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (mark
);
825 struct value
*return_value
;
827 return_value
= call_function_by_hand (function
, args_count
, vargs
);
828 result
= value_to_value_object (return_value
);
829 do_cleanups (cleanup
);
831 CATCH (except
, RETURN_MASK_ALL
)
833 GDB_PY_HANDLE_EXCEPTION (except
);
840 /* Called by the Python interpreter to obtain string representation
843 valpy_str (PyObject
*self
)
847 struct value_print_options opts
;
849 get_user_print_options (&opts
);
854 struct ui_file
*stb
= mem_fileopen ();
855 struct cleanup
*old_chain
= make_cleanup_ui_file_delete (stb
);
857 common_val_print (((value_object
*) self
)->value
, stb
, 0,
858 &opts
, python_language
);
859 s
= ui_file_xstrdup (stb
, NULL
);
861 do_cleanups (old_chain
);
863 CATCH (except
, RETURN_MASK_ALL
)
865 GDB_PY_HANDLE_EXCEPTION (except
);
869 result
= PyUnicode_Decode (s
, strlen (s
), host_charset (), NULL
);
875 /* Implements gdb.Value.is_optimized_out. */
877 valpy_get_is_optimized_out (PyObject
*self
, void *closure
)
879 struct value
*value
= ((value_object
*) self
)->value
;
884 opt
= value_optimized_out (value
);
886 CATCH (except
, RETURN_MASK_ALL
)
888 GDB_PY_HANDLE_EXCEPTION (except
);
898 /* Implements gdb.Value.is_lazy. */
900 valpy_get_is_lazy (PyObject
*self
, void *closure
)
902 struct value
*value
= ((value_object
*) self
)->value
;
907 opt
= value_lazy (value
);
909 CATCH (except
, RETURN_MASK_ALL
)
911 GDB_PY_HANDLE_EXCEPTION (except
);
921 /* Implements gdb.Value.fetch_lazy (). */
923 valpy_fetch_lazy (PyObject
*self
, PyObject
*args
)
925 struct value
*value
= ((value_object
*) self
)->value
;
929 if (value_lazy (value
))
930 value_fetch_lazy (value
);
932 CATCH (except
, RETURN_MASK_ALL
)
934 GDB_PY_HANDLE_EXCEPTION (except
);
941 /* Calculate and return the address of the PyObject as the value of
942 the builtin __hash__ call. */
944 valpy_hash (PyObject
*self
)
946 return (intptr_t) self
;
964 /* If TYPE is a reference, return the target; otherwise return TYPE. */
965 #define STRIP_REFERENCE(TYPE) \
966 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
968 /* Returns a value object which is the result of applying the operation
969 specified by OPCODE to the given arguments. Returns NULL on error, with
970 a python exception set. */
972 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
974 PyObject
*result
= NULL
;
978 struct value
*arg1
, *arg2
;
979 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
980 struct value
*res_val
= NULL
;
981 enum exp_opcode op
= OP_NULL
;
984 /* If the gdb.Value object is the second operand, then it will be passed
985 to us as the OTHER argument, and SELF will be an entirely different
986 kind of object, altogether. Because of this, we can't assume self is
987 a gdb.Value object and need to convert it from python as well. */
988 arg1
= convert_value_from_python (self
);
991 do_cleanups (cleanup
);
995 arg2
= convert_value_from_python (other
);
998 do_cleanups (cleanup
);
1006 struct type
*ltype
= value_type (arg1
);
1007 struct type
*rtype
= value_type (arg2
);
1009 CHECK_TYPEDEF (ltype
);
1010 ltype
= STRIP_REFERENCE (ltype
);
1011 CHECK_TYPEDEF (rtype
);
1012 rtype
= STRIP_REFERENCE (rtype
);
1015 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
1016 && is_integral_type (rtype
))
1017 res_val
= value_ptradd (arg1
, value_as_long (arg2
));
1018 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
1019 && is_integral_type (ltype
))
1020 res_val
= value_ptradd (arg2
, value_as_long (arg1
));
1030 struct type
*ltype
= value_type (arg1
);
1031 struct type
*rtype
= value_type (arg2
);
1033 CHECK_TYPEDEF (ltype
);
1034 ltype
= STRIP_REFERENCE (ltype
);
1035 CHECK_TYPEDEF (rtype
);
1036 rtype
= STRIP_REFERENCE (rtype
);
1039 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
1040 && TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
1041 /* A ptrdiff_t for the target would be preferable here. */
1042 res_val
= value_from_longest (builtin_type_pyint
,
1043 value_ptrdiff (arg1
, arg2
));
1044 else if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
1045 && is_integral_type (rtype
))
1046 res_val
= value_ptradd (arg1
, - value_as_long (arg2
));
1073 op
= BINOP_BITWISE_AND
;
1076 op
= BINOP_BITWISE_IOR
;
1079 op
= BINOP_BITWISE_XOR
;
1085 if (binop_user_defined_p (op
, arg1
, arg2
))
1086 res_val
= value_x_binop (arg1
, arg2
, op
, OP_NULL
, EVAL_NORMAL
);
1088 res_val
= value_binop (arg1
, arg2
, op
);
1092 result
= value_to_value_object (res_val
);
1094 do_cleanups (cleanup
);
1096 CATCH (except
, RETURN_MASK_ALL
)
1098 GDB_PY_HANDLE_EXCEPTION (except
);
1106 valpy_add (PyObject
*self
, PyObject
*other
)
1108 return valpy_binop (VALPY_ADD
, self
, other
);
1112 valpy_subtract (PyObject
*self
, PyObject
*other
)
1114 return valpy_binop (VALPY_SUB
, self
, other
);
1118 valpy_multiply (PyObject
*self
, PyObject
*other
)
1120 return valpy_binop (VALPY_MUL
, self
, other
);
1124 valpy_divide (PyObject
*self
, PyObject
*other
)
1126 return valpy_binop (VALPY_DIV
, self
, other
);
1130 valpy_remainder (PyObject
*self
, PyObject
*other
)
1132 return valpy_binop (VALPY_REM
, self
, other
);
1136 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
1138 /* We don't support the ternary form of pow. I don't know how to express
1139 that, so let's just throw NotImplementedError to at least do something
1141 if (unused
!= Py_None
)
1143 PyErr_SetString (PyExc_NotImplementedError
,
1144 "Invalid operation on gdb.Value.");
1148 return valpy_binop (VALPY_POW
, self
, other
);
1152 valpy_negative (PyObject
*self
)
1154 PyObject
*result
= NULL
;
1158 /* Perhaps overkill, but consistency has some virtue. */
1159 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
1162 val
= value_neg (((value_object
*) self
)->value
);
1163 result
= value_to_value_object (val
);
1164 do_cleanups (cleanup
);
1166 CATCH (except
, RETURN_MASK_ALL
)
1168 GDB_PY_HANDLE_EXCEPTION (except
);
1176 valpy_positive (PyObject
*self
)
1178 return value_to_value_object (((value_object
*) self
)->value
);
1182 valpy_absolute (PyObject
*self
)
1184 struct value
*value
= ((value_object
*) self
)->value
;
1189 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
1191 if (value_less (value
, value_zero (value_type (value
), not_lval
)))
1194 do_cleanups (cleanup
);
1196 CATCH (except
, RETURN_MASK_ALL
)
1198 GDB_PY_HANDLE_EXCEPTION (except
);
1203 return valpy_positive (self
);
1205 return valpy_negative (self
);
1208 /* Implements boolean evaluation of gdb.Value. */
1210 valpy_nonzero (PyObject
*self
)
1212 struct gdb_exception except
= exception_none
;
1213 value_object
*self_value
= (value_object
*) self
;
1215 int nonzero
= 0; /* Appease GCC warning. */
1219 type
= check_typedef (value_type (self_value
->value
));
1221 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
1222 nonzero
= !!value_as_long (self_value
->value
);
1223 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1224 nonzero
= value_as_double (self_value
->value
) != 0;
1225 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1226 nonzero
= !decimal_is_zero (value_contents (self_value
->value
),
1228 gdbarch_byte_order (get_type_arch (type
)));
1230 /* All other values are True. */
1233 CATCH (ex
, RETURN_MASK_ALL
)
1239 /* This is not documented in the Python documentation, but if this
1240 function fails, return -1 as slot_nb_nonzero does (the default
1241 Python nonzero function). */
1242 GDB_PY_SET_HANDLE_EXCEPTION (except
);
1247 /* Implements ~ for value objects. */
1249 valpy_invert (PyObject
*self
)
1251 struct value
*val
= NULL
;
1255 val
= value_complement (((value_object
*) self
)->value
);
1257 CATCH (except
, RETURN_MASK_ALL
)
1259 GDB_PY_HANDLE_EXCEPTION (except
);
1263 return value_to_value_object (val
);
1266 /* Implements left shift for value objects. */
1268 valpy_lsh (PyObject
*self
, PyObject
*other
)
1270 return valpy_binop (VALPY_LSH
, self
, other
);
1273 /* Implements right shift for value objects. */
1275 valpy_rsh (PyObject
*self
, PyObject
*other
)
1277 return valpy_binop (VALPY_RSH
, self
, other
);
1280 /* Implements bitwise and for value objects. */
1282 valpy_and (PyObject
*self
, PyObject
*other
)
1284 return valpy_binop (VALPY_BITAND
, self
, other
);
1287 /* Implements bitwise or for value objects. */
1289 valpy_or (PyObject
*self
, PyObject
*other
)
1291 return valpy_binop (VALPY_BITOR
, self
, other
);
1294 /* Implements bitwise xor for value objects. */
1296 valpy_xor (PyObject
*self
, PyObject
*other
)
1298 return valpy_binop (VALPY_BITXOR
, self
, other
);
1301 /* Implements comparison operations for value objects. Returns NULL on error,
1302 with a python exception set. */
1304 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
1308 if (other
== Py_None
)
1309 /* Comparing with None is special. From what I can tell, in Python
1310 None is smaller than anything else. */
1322 PyErr_SetString (PyExc_NotImplementedError
,
1323 _("Invalid operation on gdb.Value."));
1329 struct value
*value_other
, *mark
= value_mark ();
1330 struct cleanup
*cleanup
;
1332 value_other
= convert_value_from_python (other
);
1333 if (value_other
== NULL
)
1339 cleanup
= make_cleanup_value_free_to_mark (mark
);
1343 result
= value_less (((value_object
*) self
)->value
, value_other
);
1346 result
= value_less (((value_object
*) self
)->value
, value_other
)
1347 || value_equal (((value_object
*) self
)->value
, value_other
);
1350 result
= value_equal (((value_object
*) self
)->value
, value_other
);
1353 result
= !value_equal (((value_object
*) self
)->value
, value_other
);
1356 result
= value_less (value_other
, ((value_object
*) self
)->value
);
1359 result
= value_less (value_other
, ((value_object
*) self
)->value
)
1360 || value_equal (((value_object
*) self
)->value
, value_other
);
1364 PyErr_SetString (PyExc_NotImplementedError
,
1365 _("Invalid operation on gdb.Value."));
1370 do_cleanups (cleanup
);
1372 CATCH (except
, RETURN_MASK_ALL
)
1374 GDB_PY_HANDLE_EXCEPTION (except
);
1378 /* In this case, the Python exception has already been set. */
1389 /* Implements conversion to int. */
1391 valpy_int (PyObject
*self
)
1393 struct value
*value
= ((value_object
*) self
)->value
;
1394 struct type
*type
= value_type (value
);
1399 if (!is_integral_type (type
))
1400 error (_("Cannot convert value to int."));
1402 l
= value_as_long (value
);
1404 CATCH (except
, RETURN_MASK_ALL
)
1406 GDB_PY_HANDLE_EXCEPTION (except
);
1410 return gdb_py_object_from_longest (l
);
1414 /* Implements conversion to long. */
1416 valpy_long (PyObject
*self
)
1418 struct value
*value
= ((value_object
*) self
)->value
;
1419 struct type
*type
= value_type (value
);
1424 CHECK_TYPEDEF (type
);
1426 if (!is_integral_type (type
)
1427 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
1428 error (_("Cannot convert value to long."));
1430 l
= value_as_long (value
);
1432 CATCH (except
, RETURN_MASK_ALL
)
1434 GDB_PY_HANDLE_EXCEPTION (except
);
1438 return gdb_py_long_from_longest (l
);
1441 /* Implements conversion to float. */
1443 valpy_float (PyObject
*self
)
1445 struct value
*value
= ((value_object
*) self
)->value
;
1446 struct type
*type
= value_type (value
);
1451 CHECK_TYPEDEF (type
);
1453 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
1454 error (_("Cannot convert value to float."));
1456 d
= value_as_double (value
);
1458 CATCH (except
, RETURN_MASK_ALL
)
1460 GDB_PY_HANDLE_EXCEPTION (except
);
1464 return PyFloat_FromDouble (d
);
1467 /* Returns an object for a value which is released from the all_values chain,
1468 so its lifetime is not bound to the execution of a command. */
1470 value_to_value_object (struct value
*val
)
1472 value_object
*val_obj
;
1474 val_obj
= PyObject_New (value_object
, &value_object_type
);
1475 if (val_obj
!= NULL
)
1477 val_obj
->value
= val
;
1478 release_value_or_incref (val
);
1479 val_obj
->address
= NULL
;
1480 val_obj
->type
= NULL
;
1481 val_obj
->dynamic_type
= NULL
;
1482 note_value (val_obj
);
1485 return (PyObject
*) val_obj
;
1488 /* Returns a borrowed reference to the struct value corresponding to
1489 the given value object. */
1491 value_object_to_value (PyObject
*self
)
1495 if (! PyObject_TypeCheck (self
, &value_object_type
))
1497 real
= (value_object
*) self
;
1501 /* Try to convert a Python value to a gdb value. If the value cannot
1502 be converted, set a Python exception and return NULL. Returns a
1503 reference to a new value on the all_values chain. */
1506 convert_value_from_python (PyObject
*obj
)
1508 struct value
*value
= NULL
; /* -Wall */
1511 gdb_assert (obj
!= NULL
);
1515 if (PyBool_Check (obj
))
1517 cmp
= PyObject_IsTrue (obj
);
1519 value
= value_from_longest (builtin_type_pybool
, cmp
);
1521 /* Make a long logic check first. In Python 3.x, internally,
1522 all integers are represented as longs. In Python 2.x, there
1523 is still a differentiation internally between a PyInt and a
1524 PyLong. Explicitly do this long check conversion first. In
1525 GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
1526 to be done first to ensure we do not lose information in the
1527 conversion process. */
1528 else if (PyLong_Check (obj
))
1530 LONGEST l
= PyLong_AsLongLong (obj
);
1532 if (PyErr_Occurred ())
1534 /* If the error was an overflow, we can try converting to
1535 ULONGEST instead. */
1536 if (PyErr_ExceptionMatches (PyExc_OverflowError
))
1538 PyObject
*etype
, *evalue
, *etraceback
, *zero
;
1540 PyErr_Fetch (&etype
, &evalue
, &etraceback
);
1541 zero
= PyInt_FromLong (0);
1543 /* Check whether obj is positive. */
1544 if (PyObject_RichCompareBool (obj
, zero
, Py_GT
) > 0)
1548 ul
= PyLong_AsUnsignedLongLong (obj
);
1549 if (! PyErr_Occurred ())
1550 value
= value_from_ulongest (builtin_type_upylong
, ul
);
1553 /* There's nothing we can do. */
1554 PyErr_Restore (etype
, evalue
, etraceback
);
1560 value
= value_from_longest (builtin_type_pylong
, l
);
1562 else if (PyInt_Check (obj
))
1564 long l
= PyInt_AsLong (obj
);
1566 if (! PyErr_Occurred ())
1567 value
= value_from_longest (builtin_type_pyint
, l
);
1569 else if (PyFloat_Check (obj
))
1571 double d
= PyFloat_AsDouble (obj
);
1573 if (! PyErr_Occurred ())
1574 value
= value_from_double (builtin_type_pyfloat
, d
);
1576 else if (gdbpy_is_string (obj
))
1580 s
= python_string_to_target_string (obj
);
1583 struct cleanup
*old
;
1585 old
= make_cleanup (xfree
, s
);
1586 value
= value_cstring (s
, strlen (s
), builtin_type_pychar
);
1590 else if (PyObject_TypeCheck (obj
, &value_object_type
))
1591 value
= value_copy (((value_object
*) obj
)->value
);
1592 else if (gdbpy_is_lazy_string (obj
))
1596 result
= PyObject_CallMethodObjArgs (obj
, gdbpy_value_cst
, NULL
);
1597 value
= value_copy (((value_object
*) result
)->value
);
1601 PyErr_Format (PyExc_TypeError
,
1602 _("Could not convert Python object: %S."), obj
);
1604 PyErr_Format (PyExc_TypeError
,
1605 _("Could not convert Python object: %s."),
1606 PyString_AsString (PyObject_Str (obj
)));
1609 CATCH (except
, RETURN_MASK_ALL
)
1611 PyErr_Format (except
.reason
== RETURN_QUIT
1612 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
1613 "%s", except
.message
);
1621 /* Returns value object in the ARGth position in GDB's history. */
1623 gdbpy_history (PyObject
*self
, PyObject
*args
)
1626 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
1628 if (!PyArg_ParseTuple (args
, "i", &i
))
1633 res_val
= access_value_history (i
);
1635 CATCH (except
, RETURN_MASK_ALL
)
1637 GDB_PY_HANDLE_EXCEPTION (except
);
1641 return value_to_value_object (res_val
);
1644 /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
1647 gdbpy_is_value_object (PyObject
*obj
)
1649 return PyObject_TypeCheck (obj
, &value_object_type
);
1653 gdbpy_initialize_values (void)
1655 if (PyType_Ready (&value_object_type
) < 0)
1658 return gdb_pymodule_addobject (gdb_module
, "Value",
1659 (PyObject
*) &value_object_type
);
1664 static PyGetSetDef value_object_getset
[] = {
1665 { "address", valpy_get_address
, NULL
, "The address of the value.",
1667 { "is_optimized_out", valpy_get_is_optimized_out
, NULL
,
1668 "Boolean telling whether the value is optimized "
1669 "out (i.e., not available).",
1671 { "type", valpy_get_type
, NULL
, "Type of the value.", NULL
},
1672 { "dynamic_type", valpy_get_dynamic_type
, NULL
,
1673 "Dynamic type of the value.", NULL
},
1674 { "is_lazy", valpy_get_is_lazy
, NULL
,
1675 "Boolean telling whether the value is lazy (not fetched yet\n\
1676 from the inferior). A lazy value is fetched when needed, or when\n\
1677 the \"fetch_lazy()\" method is called.", NULL
},
1678 {NULL
} /* Sentinel */
1681 static PyMethodDef value_object_methods
[] = {
1682 { "cast", valpy_cast
, METH_VARARGS
, "Cast the value to the supplied type." },
1683 { "dynamic_cast", valpy_dynamic_cast
, METH_VARARGS
,
1684 "dynamic_cast (gdb.Type) -> gdb.Value\n\
1685 Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
1687 { "reinterpret_cast", valpy_reinterpret_cast
, METH_VARARGS
,
1688 "reinterpret_cast (gdb.Type) -> gdb.Value\n\
1689 Cast the value to the supplied type, as if by the C++\n\
1690 reinterpret_cast operator."
1692 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
1693 { "referenced_value", valpy_referenced_value
, METH_NOARGS
,
1694 "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." },
1695 { "lazy_string", (PyCFunction
) valpy_lazy_string
,
1696 METH_VARARGS
| METH_KEYWORDS
,
1697 "lazy_string ([encoding] [, length]) -> lazy_string\n\
1698 Return a lazy string representation of the value." },
1699 { "string", (PyCFunction
) valpy_string
, METH_VARARGS
| METH_KEYWORDS
,
1700 "string ([encoding] [, errors] [, length]) -> string\n\
1701 Return Unicode string representation of the value." },
1702 { "fetch_lazy", valpy_fetch_lazy
, METH_NOARGS
,
1703 "Fetches the value from the inferior, if it was lazy." },
1704 {NULL
} /* Sentinel */
1707 static PyNumberMethods value_object_as_number
= {
1715 NULL
, /* nb_divmod */
1716 valpy_power
, /* nb_power */
1717 valpy_negative
, /* nb_negative */
1718 valpy_positive
, /* nb_positive */
1719 valpy_absolute
, /* nb_absolute */
1720 valpy_nonzero
, /* nb_nonzero */
1721 valpy_invert
, /* nb_invert */
1722 valpy_lsh
, /* nb_lshift */
1723 valpy_rsh
, /* nb_rshift */
1724 valpy_and
, /* nb_and */
1725 valpy_xor
, /* nb_xor */
1726 valpy_or
, /* nb_or */
1728 valpy_long
, /* nb_int */
1729 NULL
, /* reserved */
1731 NULL
, /* nb_coerce */
1732 valpy_int
, /* nb_int */
1733 valpy_long
, /* nb_long */
1735 valpy_float
, /* nb_float */
1740 NULL
, /* nb_inplace_add */
1741 NULL
, /* nb_inplace_subtract */
1742 NULL
, /* nb_inplace_multiply */
1743 NULL
, /* nb_inplace_remainder */
1744 NULL
, /* nb_inplace_power */
1745 NULL
, /* nb_inplace_lshift */
1746 NULL
, /* nb_inplace_rshift */
1747 NULL
, /* nb_inplace_and */
1748 NULL
, /* nb_inplace_xor */
1749 NULL
, /* nb_inplace_or */
1750 NULL
, /* nb_floor_divide */
1751 valpy_divide
/* nb_true_divide */
1754 static PyMappingMethods value_object_as_mapping
= {
1760 PyTypeObject value_object_type
= {
1761 PyVarObject_HEAD_INIT (NULL
, 0)
1762 "gdb.Value", /*tp_name*/
1763 sizeof (value_object
), /*tp_basicsize*/
1765 valpy_dealloc
, /*tp_dealloc*/
1771 &value_object_as_number
, /*tp_as_number*/
1772 0, /*tp_as_sequence*/
1773 &value_object_as_mapping
, /*tp_as_mapping*/
1774 valpy_hash
, /*tp_hash*/
1775 valpy_call
, /*tp_call*/
1776 valpy_str
, /*tp_str*/
1780 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
1781 | Py_TPFLAGS_BASETYPE
, /*tp_flags*/
1782 "GDB value object", /* tp_doc */
1783 0, /* tp_traverse */
1785 valpy_richcompare
, /* tp_richcompare */
1786 0, /* tp_weaklistoffset */
1788 0, /* tp_iternext */
1789 value_object_methods
, /* tp_methods */
1791 value_object_getset
, /* tp_getset */
1794 0, /* tp_descr_get */
1795 0, /* tp_descr_set */
1796 0, /* tp_dictoffset */
1799 valpy_new
/* tp_new */