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 a value which is a reference to the value. */
242 valpy_reference_value (PyObject
*self
, PyObject
*args
)
244 PyObject
*result
= NULL
;
248 struct value
*self_val
;
249 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
251 self_val
= ((value_object
*) self
)->value
;
252 result
= value_to_value_object (value_ref (self_val
));
254 do_cleanups (cleanup
);
256 CATCH (except
, RETURN_MASK_ALL
)
258 GDB_PY_HANDLE_EXCEPTION (except
);
265 /* Return a "const" qualified version of the value. */
268 valpy_const_value (PyObject
*self
, PyObject
*args
)
270 PyObject
*result
= NULL
;
274 struct value
*self_val
, *res_val
;
275 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
277 self_val
= ((value_object
*) self
)->value
;
278 res_val
= make_cv_value (1, 0, self_val
);
279 result
= value_to_value_object (res_val
);
281 do_cleanups (cleanup
);
283 CATCH (except
, RETURN_MASK_ALL
)
285 GDB_PY_HANDLE_EXCEPTION (except
);
292 /* Return "&value". */
294 valpy_get_address (PyObject
*self
, void *closure
)
296 value_object
*val_obj
= (value_object
*) self
;
298 if (!val_obj
->address
)
302 struct value
*res_val
;
303 struct cleanup
*cleanup
304 = make_cleanup_value_free_to_mark (value_mark ());
306 res_val
= value_addr (val_obj
->value
);
307 val_obj
->address
= value_to_value_object (res_val
);
308 do_cleanups (cleanup
);
310 CATCH (except
, RETURN_MASK_ALL
)
312 val_obj
->address
= Py_None
;
318 Py_XINCREF (val_obj
->address
);
320 return val_obj
->address
;
323 /* Return type of the value. */
325 valpy_get_type (PyObject
*self
, void *closure
)
327 value_object
*obj
= (value_object
*) self
;
331 obj
->type
= type_to_type_object (value_type (obj
->value
));
335 Py_INCREF (obj
->type
);
339 /* Return dynamic type of the value. */
342 valpy_get_dynamic_type (PyObject
*self
, void *closure
)
344 value_object
*obj
= (value_object
*) self
;
345 struct type
*type
= NULL
;
347 if (obj
->dynamic_type
!= NULL
)
349 Py_INCREF (obj
->dynamic_type
);
350 return obj
->dynamic_type
;
355 struct value
*val
= obj
->value
;
356 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
358 type
= value_type (val
);
359 type
= check_typedef (type
);
361 if (((TYPE_CODE (type
) == TYPE_CODE_PTR
)
362 || (TYPE_CODE (type
) == TYPE_CODE_REF
))
363 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_STRUCT
))
365 struct value
*target
;
366 int was_pointer
= TYPE_CODE (type
) == TYPE_CODE_PTR
;
369 target
= value_ind (val
);
371 target
= coerce_ref (val
);
372 type
= value_rtti_type (target
, NULL
, NULL
, NULL
);
377 type
= lookup_pointer_type (type
);
379 type
= lookup_reference_type (type
);
382 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
383 type
= value_rtti_type (val
, NULL
, NULL
, NULL
);
386 /* Re-use object's static type. */
390 do_cleanups (cleanup
);
392 CATCH (except
, RETURN_MASK_ALL
)
394 GDB_PY_HANDLE_EXCEPTION (except
);
399 obj
->dynamic_type
= valpy_get_type (self
, NULL
);
401 obj
->dynamic_type
= type_to_type_object (type
);
403 Py_XINCREF (obj
->dynamic_type
);
404 return obj
->dynamic_type
;
407 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
408 string. Return a PyObject representing a lazy_string_object type.
409 A lazy string is a pointer to a string with an optional encoding and
410 length. If ENCODING is not given, encoding is set to None. If an
411 ENCODING is provided the encoding parameter is set to ENCODING, but
412 the string is not encoded. If LENGTH is provided then the length
413 parameter is set to LENGTH, otherwise length will be set to -1 (first
414 null of appropriate with). */
416 valpy_lazy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
418 gdb_py_longest length
= -1;
419 struct value
*value
= ((value_object
*) self
)->value
;
420 const char *user_encoding
= NULL
;
421 static char *keywords
[] = { "encoding", "length", NULL
};
422 PyObject
*str_obj
= NULL
;
424 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|s" GDB_PY_LL_ARG
, keywords
,
425 &user_encoding
, &length
))
430 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
432 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_PTR
)
433 value
= value_ind (value
);
435 str_obj
= gdbpy_create_lazy_string_object (value_address (value
), length
,
439 do_cleanups (cleanup
);
441 CATCH (except
, RETURN_MASK_ALL
)
443 GDB_PY_HANDLE_EXCEPTION (except
);
450 /* Implementation of gdb.Value.string ([encoding] [, errors]
451 [, length]) -> string. Return Unicode string with value contents.
452 If ENCODING is not given, the string is assumed to be encoded in
453 the target's charset. If LENGTH is provided, only fetch string to
454 the length provided. */
457 valpy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
461 struct value
*value
= ((value_object
*) self
)->value
;
463 const char *encoding
= NULL
;
464 const char *errors
= NULL
;
465 const char *user_encoding
= NULL
;
466 const char *la_encoding
= NULL
;
467 struct type
*char_type
;
468 static char *keywords
[] = { "encoding", "errors", "length", NULL
};
470 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|ssi", keywords
,
471 &user_encoding
, &errors
, &length
))
476 LA_GET_STRING (value
, &buffer
, &length
, &char_type
, &la_encoding
);
478 CATCH (except
, RETURN_MASK_ALL
)
480 GDB_PY_HANDLE_EXCEPTION (except
);
484 encoding
= (user_encoding
&& *user_encoding
) ? user_encoding
: la_encoding
;
485 unicode
= PyUnicode_Decode ((const char *) buffer
,
486 length
* TYPE_LENGTH (char_type
),
493 /* A helper function that implements the various cast operators. */
496 valpy_do_cast (PyObject
*self
, PyObject
*args
, enum exp_opcode op
)
498 PyObject
*type_obj
, *result
= NULL
;
501 if (! PyArg_ParseTuple (args
, "O", &type_obj
))
504 type
= type_object_to_type (type_obj
);
507 PyErr_SetString (PyExc_RuntimeError
,
508 _("Argument must be a type."));
514 struct value
*val
= ((value_object
*) self
)->value
;
515 struct value
*res_val
;
516 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
518 if (op
== UNOP_DYNAMIC_CAST
)
519 res_val
= value_dynamic_cast (type
, val
);
520 else if (op
== UNOP_REINTERPRET_CAST
)
521 res_val
= value_reinterpret_cast (type
, val
);
524 gdb_assert (op
== UNOP_CAST
);
525 res_val
= value_cast (type
, val
);
528 result
= value_to_value_object (res_val
);
529 do_cleanups (cleanup
);
531 CATCH (except
, RETURN_MASK_ALL
)
533 GDB_PY_HANDLE_EXCEPTION (except
);
540 /* Implementation of the "cast" method. */
543 valpy_cast (PyObject
*self
, PyObject
*args
)
545 return valpy_do_cast (self
, args
, UNOP_CAST
);
548 /* Implementation of the "dynamic_cast" method. */
551 valpy_dynamic_cast (PyObject
*self
, PyObject
*args
)
553 return valpy_do_cast (self
, args
, UNOP_DYNAMIC_CAST
);
556 /* Implementation of the "reinterpret_cast" method. */
559 valpy_reinterpret_cast (PyObject
*self
, PyObject
*args
)
561 return valpy_do_cast (self
, args
, UNOP_REINTERPRET_CAST
);
565 valpy_length (PyObject
*self
)
567 /* We don't support getting the number of elements in a struct / class. */
568 PyErr_SetString (PyExc_NotImplementedError
,
569 _("Invalid operation on gdb.Value."));
573 /* Return 1 if the gdb.Field object FIELD is present in the value V.
574 Returns 0 otherwise. If any Python error occurs, -1 is returned. */
577 value_has_field (struct value
*v
, PyObject
*field
)
579 struct type
*parent_type
, *val_type
;
580 enum type_code type_code
;
581 PyObject
*type_object
= PyObject_GetAttrString (field
, "parent_type");
584 if (type_object
== NULL
)
587 parent_type
= type_object_to_type (type_object
);
588 Py_DECREF (type_object
);
589 if (parent_type
== NULL
)
591 PyErr_SetString (PyExc_TypeError
,
592 _("'parent_type' attribute of gdb.Field object is not a"
593 "gdb.Type object."));
599 val_type
= value_type (v
);
600 val_type
= check_typedef (val_type
);
601 if (TYPE_CODE (val_type
) == TYPE_CODE_REF
602 || TYPE_CODE (val_type
) == TYPE_CODE_PTR
)
603 val_type
= check_typedef (TYPE_TARGET_TYPE (val_type
));
605 type_code
= TYPE_CODE (val_type
);
606 if ((type_code
== TYPE_CODE_STRUCT
|| type_code
== TYPE_CODE_UNION
)
607 && types_equal (val_type
, parent_type
))
612 CATCH (except
, RETURN_MASK_ALL
)
614 GDB_PY_SET_HANDLE_EXCEPTION (except
);
621 /* Return the value of a flag FLAG_NAME in a gdb.Field object FIELD.
622 Returns 1 if the flag value is true, 0 if it is false, and -1 if
623 a Python error occurs. */
626 get_field_flag (PyObject
*field
, const char *flag_name
)
629 PyObject
*flag_object
= PyObject_GetAttrString (field
, flag_name
);
631 if (flag_object
== NULL
)
634 flag_value
= PyObject_IsTrue (flag_object
);
635 Py_DECREF (flag_object
);
640 /* Return the "type" attribute of a gdb.Field object.
641 Returns NULL on error, with a Python exception set. */
644 get_field_type (PyObject
*field
)
646 PyObject
*ftype_obj
= PyObject_GetAttrString (field
, "type");
649 if (ftype_obj
== NULL
)
651 ftype
= type_object_to_type (ftype_obj
);
652 Py_DECREF (ftype_obj
);
654 PyErr_SetString (PyExc_TypeError
,
655 _("'type' attribute of gdb.Field object is not a "
656 "gdb.Type object."));
661 /* Given string name or a gdb.Field object corresponding to an element inside
662 a structure, return its value object. Returns NULL on error, with a python
666 valpy_getitem (PyObject
*self
, PyObject
*key
)
668 struct gdb_exception except
= exception_none
;
669 value_object
*self_value
= (value_object
*) self
;
671 struct type
*base_class_type
= NULL
, *field_type
= NULL
;
673 PyObject
*result
= NULL
;
675 if (gdbpy_is_string (key
))
677 field
= python_string_to_host_string (key
);
681 else if (gdbpy_is_field (key
))
683 int is_base_class
, valid_field
;
685 valid_field
= value_has_field (self_value
->value
, key
);
688 else if (valid_field
== 0)
690 PyErr_SetString (PyExc_TypeError
,
691 _("Invalid lookup for a field not contained in "
697 is_base_class
= get_field_flag (key
, "is_base_class");
698 if (is_base_class
< 0)
700 else if (is_base_class
> 0)
702 base_class_type
= get_field_type (key
);
703 if (base_class_type
== NULL
)
708 PyObject
*name_obj
= PyObject_GetAttrString (key
, "name");
710 if (name_obj
== NULL
)
713 if (name_obj
!= Py_None
)
715 field
= python_string_to_host_string (name_obj
);
716 Py_DECREF (name_obj
);
722 PyObject
*bitpos_obj
;
725 Py_DECREF (name_obj
);
727 if (!PyObject_HasAttrString (key
, "bitpos"))
729 PyErr_SetString (PyExc_AttributeError
,
730 _("gdb.Field object has no name and no "
731 "'bitpos' attribute."));
735 bitpos_obj
= PyObject_GetAttrString (key
, "bitpos");
736 if (bitpos_obj
== NULL
)
738 valid
= gdb_py_int_as_long (bitpos_obj
, &bitpos
);
739 Py_DECREF (bitpos_obj
);
743 field_type
= get_field_type (key
);
744 if (field_type
== NULL
)
752 struct value
*tmp
= self_value
->value
;
753 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
754 struct value
*res_val
= NULL
;
757 res_val
= value_struct_elt (&tmp
, NULL
, field
, 0, NULL
);
758 else if (bitpos
>= 0)
759 res_val
= value_struct_elt_bitpos (&tmp
, bitpos
, field_type
,
760 "struct/class/union");
761 else if (base_class_type
!= NULL
)
763 struct type
*val_type
;
765 val_type
= check_typedef (value_type (tmp
));
766 if (TYPE_CODE (val_type
) == TYPE_CODE_PTR
)
767 res_val
= value_cast (lookup_pointer_type (base_class_type
), tmp
);
768 else if (TYPE_CODE (val_type
) == TYPE_CODE_REF
)
769 res_val
= value_cast (lookup_reference_type (base_class_type
), tmp
);
771 res_val
= value_cast (base_class_type
, tmp
);
775 /* Assume we are attempting an array access, and let the
776 value code throw an exception if the index has an invalid
778 struct value
*idx
= convert_value_from_python (key
);
782 /* Check the value's type is something that can be accessed via
786 tmp
= coerce_ref (tmp
);
787 type
= check_typedef (value_type (tmp
));
788 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
789 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
790 error (_("Cannot subscript requested type."));
792 res_val
= value_subscript (tmp
, value_as_long (idx
));
797 result
= value_to_value_object (res_val
);
798 do_cleanups (cleanup
);
800 CATCH (ex
, RETURN_MASK_ALL
)
807 GDB_PY_HANDLE_EXCEPTION (except
);
813 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
815 PyErr_Format (PyExc_NotImplementedError
,
816 _("Setting of struct elements is not currently supported."));
820 /* Called by the Python interpreter to perform an inferior function
821 call on the value. Returns NULL on error, with a python exception set. */
823 valpy_call (PyObject
*self
, PyObject
*args
, PyObject
*keywords
)
825 Py_ssize_t args_count
;
826 struct value
*function
= ((value_object
*) self
)->value
;
827 struct value
**vargs
= NULL
;
828 struct type
*ftype
= NULL
;
829 struct value
*mark
= value_mark ();
830 PyObject
*result
= NULL
;
834 ftype
= check_typedef (value_type (function
));
836 CATCH (except
, RETURN_MASK_ALL
)
838 GDB_PY_HANDLE_EXCEPTION (except
);
842 if (TYPE_CODE (ftype
) != TYPE_CODE_FUNC
)
844 PyErr_SetString (PyExc_RuntimeError
,
845 _("Value is not callable (not TYPE_CODE_FUNC)."));
849 if (! PyTuple_Check (args
))
851 PyErr_SetString (PyExc_TypeError
,
852 _("Inferior arguments must be provided in a tuple."));
856 args_count
= PyTuple_Size (args
);
861 vargs
= alloca (sizeof (struct value
*) * args_count
);
862 for (i
= 0; i
< args_count
; i
++)
864 PyObject
*item
= PyTuple_GetItem (args
, i
);
869 vargs
[i
] = convert_value_from_python (item
);
870 if (vargs
[i
] == NULL
)
877 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (mark
);
878 struct value
*return_value
;
880 return_value
= call_function_by_hand (function
, args_count
, vargs
);
881 result
= value_to_value_object (return_value
);
882 do_cleanups (cleanup
);
884 CATCH (except
, RETURN_MASK_ALL
)
886 GDB_PY_HANDLE_EXCEPTION (except
);
893 /* Called by the Python interpreter to obtain string representation
896 valpy_str (PyObject
*self
)
900 struct value_print_options opts
;
902 get_user_print_options (&opts
);
907 struct ui_file
*stb
= mem_fileopen ();
908 struct cleanup
*old_chain
= make_cleanup_ui_file_delete (stb
);
910 common_val_print (((value_object
*) self
)->value
, stb
, 0,
911 &opts
, python_language
);
912 s
= ui_file_xstrdup (stb
, NULL
);
914 do_cleanups (old_chain
);
916 CATCH (except
, RETURN_MASK_ALL
)
918 GDB_PY_HANDLE_EXCEPTION (except
);
922 result
= PyUnicode_Decode (s
, strlen (s
), host_charset (), NULL
);
928 /* Implements gdb.Value.is_optimized_out. */
930 valpy_get_is_optimized_out (PyObject
*self
, void *closure
)
932 struct value
*value
= ((value_object
*) self
)->value
;
937 opt
= value_optimized_out (value
);
939 CATCH (except
, RETURN_MASK_ALL
)
941 GDB_PY_HANDLE_EXCEPTION (except
);
951 /* Implements gdb.Value.is_lazy. */
953 valpy_get_is_lazy (PyObject
*self
, void *closure
)
955 struct value
*value
= ((value_object
*) self
)->value
;
960 opt
= value_lazy (value
);
962 CATCH (except
, RETURN_MASK_ALL
)
964 GDB_PY_HANDLE_EXCEPTION (except
);
974 /* Implements gdb.Value.fetch_lazy (). */
976 valpy_fetch_lazy (PyObject
*self
, PyObject
*args
)
978 struct value
*value
= ((value_object
*) self
)->value
;
982 if (value_lazy (value
))
983 value_fetch_lazy (value
);
985 CATCH (except
, RETURN_MASK_ALL
)
987 GDB_PY_HANDLE_EXCEPTION (except
);
994 /* Calculate and return the address of the PyObject as the value of
995 the builtin __hash__ call. */
997 valpy_hash (PyObject
*self
)
999 return (intptr_t) self
;
1017 /* If TYPE is a reference, return the target; otherwise return TYPE. */
1018 #define STRIP_REFERENCE(TYPE) \
1019 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
1021 /* Returns a value object which is the result of applying the operation
1022 specified by OPCODE to the given arguments. Returns NULL on error, with
1023 a python exception set. */
1025 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
1027 PyObject
*result
= NULL
;
1031 struct value
*arg1
, *arg2
;
1032 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
1033 struct value
*res_val
= NULL
;
1034 enum exp_opcode op
= OP_NULL
;
1037 /* If the gdb.Value object is the second operand, then it will be passed
1038 to us as the OTHER argument, and SELF will be an entirely different
1039 kind of object, altogether. Because of this, we can't assume self is
1040 a gdb.Value object and need to convert it from python as well. */
1041 arg1
= convert_value_from_python (self
);
1044 do_cleanups (cleanup
);
1048 arg2
= convert_value_from_python (other
);
1051 do_cleanups (cleanup
);
1059 struct type
*ltype
= value_type (arg1
);
1060 struct type
*rtype
= value_type (arg2
);
1062 ltype
= check_typedef (ltype
);
1063 ltype
= STRIP_REFERENCE (ltype
);
1064 rtype
= check_typedef (rtype
);
1065 rtype
= STRIP_REFERENCE (rtype
);
1068 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
1069 && is_integral_type (rtype
))
1070 res_val
= value_ptradd (arg1
, value_as_long (arg2
));
1071 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
1072 && is_integral_type (ltype
))
1073 res_val
= value_ptradd (arg2
, value_as_long (arg1
));
1083 struct type
*ltype
= value_type (arg1
);
1084 struct type
*rtype
= value_type (arg2
);
1086 ltype
= check_typedef (ltype
);
1087 ltype
= STRIP_REFERENCE (ltype
);
1088 rtype
= check_typedef (rtype
);
1089 rtype
= STRIP_REFERENCE (rtype
);
1092 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
1093 && TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
1094 /* A ptrdiff_t for the target would be preferable here. */
1095 res_val
= value_from_longest (builtin_type_pyint
,
1096 value_ptrdiff (arg1
, arg2
));
1097 else if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
1098 && is_integral_type (rtype
))
1099 res_val
= value_ptradd (arg1
, - value_as_long (arg2
));
1126 op
= BINOP_BITWISE_AND
;
1129 op
= BINOP_BITWISE_IOR
;
1132 op
= BINOP_BITWISE_XOR
;
1138 if (binop_user_defined_p (op
, arg1
, arg2
))
1139 res_val
= value_x_binop (arg1
, arg2
, op
, OP_NULL
, EVAL_NORMAL
);
1141 res_val
= value_binop (arg1
, arg2
, op
);
1145 result
= value_to_value_object (res_val
);
1147 do_cleanups (cleanup
);
1149 CATCH (except
, RETURN_MASK_ALL
)
1151 GDB_PY_HANDLE_EXCEPTION (except
);
1159 valpy_add (PyObject
*self
, PyObject
*other
)
1161 return valpy_binop (VALPY_ADD
, self
, other
);
1165 valpy_subtract (PyObject
*self
, PyObject
*other
)
1167 return valpy_binop (VALPY_SUB
, self
, other
);
1171 valpy_multiply (PyObject
*self
, PyObject
*other
)
1173 return valpy_binop (VALPY_MUL
, self
, other
);
1177 valpy_divide (PyObject
*self
, PyObject
*other
)
1179 return valpy_binop (VALPY_DIV
, self
, other
);
1183 valpy_remainder (PyObject
*self
, PyObject
*other
)
1185 return valpy_binop (VALPY_REM
, self
, other
);
1189 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
1191 /* We don't support the ternary form of pow. I don't know how to express
1192 that, so let's just throw NotImplementedError to at least do something
1194 if (unused
!= Py_None
)
1196 PyErr_SetString (PyExc_NotImplementedError
,
1197 "Invalid operation on gdb.Value.");
1201 return valpy_binop (VALPY_POW
, self
, other
);
1205 valpy_negative (PyObject
*self
)
1207 PyObject
*result
= NULL
;
1211 /* Perhaps overkill, but consistency has some virtue. */
1212 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
1215 val
= value_neg (((value_object
*) self
)->value
);
1216 result
= value_to_value_object (val
);
1217 do_cleanups (cleanup
);
1219 CATCH (except
, RETURN_MASK_ALL
)
1221 GDB_PY_HANDLE_EXCEPTION (except
);
1229 valpy_positive (PyObject
*self
)
1231 return value_to_value_object (((value_object
*) self
)->value
);
1235 valpy_absolute (PyObject
*self
)
1237 struct value
*value
= ((value_object
*) self
)->value
;
1242 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
1244 if (value_less (value
, value_zero (value_type (value
), not_lval
)))
1247 do_cleanups (cleanup
);
1249 CATCH (except
, RETURN_MASK_ALL
)
1251 GDB_PY_HANDLE_EXCEPTION (except
);
1256 return valpy_positive (self
);
1258 return valpy_negative (self
);
1261 /* Implements boolean evaluation of gdb.Value. */
1263 valpy_nonzero (PyObject
*self
)
1265 struct gdb_exception except
= exception_none
;
1266 value_object
*self_value
= (value_object
*) self
;
1268 int nonzero
= 0; /* Appease GCC warning. */
1272 type
= check_typedef (value_type (self_value
->value
));
1274 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
1275 nonzero
= !!value_as_long (self_value
->value
);
1276 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1277 nonzero
= value_as_double (self_value
->value
) != 0;
1278 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1279 nonzero
= !decimal_is_zero (value_contents (self_value
->value
),
1281 gdbarch_byte_order (get_type_arch (type
)));
1283 /* All other values are True. */
1286 CATCH (ex
, RETURN_MASK_ALL
)
1292 /* This is not documented in the Python documentation, but if this
1293 function fails, return -1 as slot_nb_nonzero does (the default
1294 Python nonzero function). */
1295 GDB_PY_SET_HANDLE_EXCEPTION (except
);
1300 /* Implements ~ for value objects. */
1302 valpy_invert (PyObject
*self
)
1304 struct value
*val
= NULL
;
1308 val
= value_complement (((value_object
*) self
)->value
);
1310 CATCH (except
, RETURN_MASK_ALL
)
1312 GDB_PY_HANDLE_EXCEPTION (except
);
1316 return value_to_value_object (val
);
1319 /* Implements left shift for value objects. */
1321 valpy_lsh (PyObject
*self
, PyObject
*other
)
1323 return valpy_binop (VALPY_LSH
, self
, other
);
1326 /* Implements right shift for value objects. */
1328 valpy_rsh (PyObject
*self
, PyObject
*other
)
1330 return valpy_binop (VALPY_RSH
, self
, other
);
1333 /* Implements bitwise and for value objects. */
1335 valpy_and (PyObject
*self
, PyObject
*other
)
1337 return valpy_binop (VALPY_BITAND
, self
, other
);
1340 /* Implements bitwise or for value objects. */
1342 valpy_or (PyObject
*self
, PyObject
*other
)
1344 return valpy_binop (VALPY_BITOR
, self
, other
);
1347 /* Implements bitwise xor for value objects. */
1349 valpy_xor (PyObject
*self
, PyObject
*other
)
1351 return valpy_binop (VALPY_BITXOR
, self
, other
);
1354 /* Implements comparison operations for value objects. Returns NULL on error,
1355 with a python exception set. */
1357 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
1361 if (other
== Py_None
)
1362 /* Comparing with None is special. From what I can tell, in Python
1363 None is smaller than anything else. */
1375 PyErr_SetString (PyExc_NotImplementedError
,
1376 _("Invalid operation on gdb.Value."));
1382 struct value
*value_other
, *mark
= value_mark ();
1383 struct cleanup
*cleanup
;
1385 value_other
= convert_value_from_python (other
);
1386 if (value_other
== NULL
)
1392 cleanup
= make_cleanup_value_free_to_mark (mark
);
1396 result
= value_less (((value_object
*) self
)->value
, value_other
);
1399 result
= value_less (((value_object
*) self
)->value
, value_other
)
1400 || value_equal (((value_object
*) self
)->value
, value_other
);
1403 result
= value_equal (((value_object
*) self
)->value
, value_other
);
1406 result
= !value_equal (((value_object
*) self
)->value
, value_other
);
1409 result
= value_less (value_other
, ((value_object
*) self
)->value
);
1412 result
= value_less (value_other
, ((value_object
*) self
)->value
)
1413 || value_equal (((value_object
*) self
)->value
, value_other
);
1417 PyErr_SetString (PyExc_NotImplementedError
,
1418 _("Invalid operation on gdb.Value."));
1423 do_cleanups (cleanup
);
1425 CATCH (except
, RETURN_MASK_ALL
)
1427 GDB_PY_HANDLE_EXCEPTION (except
);
1431 /* In this case, the Python exception has already been set. */
1442 /* Implements conversion to int. */
1444 valpy_int (PyObject
*self
)
1446 struct value
*value
= ((value_object
*) self
)->value
;
1447 struct type
*type
= value_type (value
);
1452 if (!is_integral_type (type
))
1453 error (_("Cannot convert value to int."));
1455 l
= value_as_long (value
);
1457 CATCH (except
, RETURN_MASK_ALL
)
1459 GDB_PY_HANDLE_EXCEPTION (except
);
1463 return gdb_py_object_from_longest (l
);
1467 /* Implements conversion to long. */
1469 valpy_long (PyObject
*self
)
1471 struct value
*value
= ((value_object
*) self
)->value
;
1472 struct type
*type
= value_type (value
);
1477 type
= check_typedef (type
);
1479 if (!is_integral_type (type
)
1480 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
1481 error (_("Cannot convert value to long."));
1483 l
= value_as_long (value
);
1485 CATCH (except
, RETURN_MASK_ALL
)
1487 GDB_PY_HANDLE_EXCEPTION (except
);
1491 return gdb_py_long_from_longest (l
);
1494 /* Implements conversion to float. */
1496 valpy_float (PyObject
*self
)
1498 struct value
*value
= ((value_object
*) self
)->value
;
1499 struct type
*type
= value_type (value
);
1504 type
= check_typedef (type
);
1506 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
1507 error (_("Cannot convert value to float."));
1509 d
= value_as_double (value
);
1511 CATCH (except
, RETURN_MASK_ALL
)
1513 GDB_PY_HANDLE_EXCEPTION (except
);
1517 return PyFloat_FromDouble (d
);
1520 /* Returns an object for a value which is released from the all_values chain,
1521 so its lifetime is not bound to the execution of a command. */
1523 value_to_value_object (struct value
*val
)
1525 value_object
*val_obj
;
1527 val_obj
= PyObject_New (value_object
, &value_object_type
);
1528 if (val_obj
!= NULL
)
1530 val_obj
->value
= val
;
1531 release_value_or_incref (val
);
1532 val_obj
->address
= NULL
;
1533 val_obj
->type
= NULL
;
1534 val_obj
->dynamic_type
= NULL
;
1535 note_value (val_obj
);
1538 return (PyObject
*) val_obj
;
1541 /* Returns a borrowed reference to the struct value corresponding to
1542 the given value object. */
1544 value_object_to_value (PyObject
*self
)
1548 if (! PyObject_TypeCheck (self
, &value_object_type
))
1550 real
= (value_object
*) self
;
1554 /* Try to convert a Python value to a gdb value. If the value cannot
1555 be converted, set a Python exception and return NULL. Returns a
1556 reference to a new value on the all_values chain. */
1559 convert_value_from_python (PyObject
*obj
)
1561 struct value
*value
= NULL
; /* -Wall */
1564 gdb_assert (obj
!= NULL
);
1568 if (PyBool_Check (obj
))
1570 cmp
= PyObject_IsTrue (obj
);
1572 value
= value_from_longest (builtin_type_pybool
, cmp
);
1574 /* Make a long logic check first. In Python 3.x, internally,
1575 all integers are represented as longs. In Python 2.x, there
1576 is still a differentiation internally between a PyInt and a
1577 PyLong. Explicitly do this long check conversion first. In
1578 GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
1579 to be done first to ensure we do not lose information in the
1580 conversion process. */
1581 else if (PyLong_Check (obj
))
1583 LONGEST l
= PyLong_AsLongLong (obj
);
1585 if (PyErr_Occurred ())
1587 /* If the error was an overflow, we can try converting to
1588 ULONGEST instead. */
1589 if (PyErr_ExceptionMatches (PyExc_OverflowError
))
1591 PyObject
*etype
, *evalue
, *etraceback
, *zero
;
1593 PyErr_Fetch (&etype
, &evalue
, &etraceback
);
1594 zero
= PyInt_FromLong (0);
1596 /* Check whether obj is positive. */
1597 if (PyObject_RichCompareBool (obj
, zero
, Py_GT
) > 0)
1601 ul
= PyLong_AsUnsignedLongLong (obj
);
1602 if (! PyErr_Occurred ())
1603 value
= value_from_ulongest (builtin_type_upylong
, ul
);
1606 /* There's nothing we can do. */
1607 PyErr_Restore (etype
, evalue
, etraceback
);
1613 value
= value_from_longest (builtin_type_pylong
, l
);
1615 else if (PyInt_Check (obj
))
1617 long l
= PyInt_AsLong (obj
);
1619 if (! PyErr_Occurred ())
1620 value
= value_from_longest (builtin_type_pyint
, l
);
1622 else if (PyFloat_Check (obj
))
1624 double d
= PyFloat_AsDouble (obj
);
1626 if (! PyErr_Occurred ())
1627 value
= value_from_double (builtin_type_pyfloat
, d
);
1629 else if (gdbpy_is_string (obj
))
1633 s
= python_string_to_target_string (obj
);
1636 struct cleanup
*old
;
1638 old
= make_cleanup (xfree
, s
);
1639 value
= value_cstring (s
, strlen (s
), builtin_type_pychar
);
1643 else if (PyObject_TypeCheck (obj
, &value_object_type
))
1644 value
= value_copy (((value_object
*) obj
)->value
);
1645 else if (gdbpy_is_lazy_string (obj
))
1649 result
= PyObject_CallMethodObjArgs (obj
, gdbpy_value_cst
, NULL
);
1650 value
= value_copy (((value_object
*) result
)->value
);
1654 PyErr_Format (PyExc_TypeError
,
1655 _("Could not convert Python object: %S."), obj
);
1657 PyErr_Format (PyExc_TypeError
,
1658 _("Could not convert Python object: %s."),
1659 PyString_AsString (PyObject_Str (obj
)));
1662 CATCH (except
, RETURN_MASK_ALL
)
1664 PyErr_Format (except
.reason
== RETURN_QUIT
1665 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
1666 "%s", except
.message
);
1674 /* Returns value object in the ARGth position in GDB's history. */
1676 gdbpy_history (PyObject
*self
, PyObject
*args
)
1679 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
1681 if (!PyArg_ParseTuple (args
, "i", &i
))
1686 res_val
= access_value_history (i
);
1688 CATCH (except
, RETURN_MASK_ALL
)
1690 GDB_PY_HANDLE_EXCEPTION (except
);
1694 return value_to_value_object (res_val
);
1697 /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
1700 gdbpy_is_value_object (PyObject
*obj
)
1702 return PyObject_TypeCheck (obj
, &value_object_type
);
1706 gdbpy_initialize_values (void)
1708 if (PyType_Ready (&value_object_type
) < 0)
1711 return gdb_pymodule_addobject (gdb_module
, "Value",
1712 (PyObject
*) &value_object_type
);
1717 static PyGetSetDef value_object_getset
[] = {
1718 { "address", valpy_get_address
, NULL
, "The address of the value.",
1720 { "is_optimized_out", valpy_get_is_optimized_out
, NULL
,
1721 "Boolean telling whether the value is optimized "
1722 "out (i.e., not available).",
1724 { "type", valpy_get_type
, NULL
, "Type of the value.", NULL
},
1725 { "dynamic_type", valpy_get_dynamic_type
, NULL
,
1726 "Dynamic type of the value.", NULL
},
1727 { "is_lazy", valpy_get_is_lazy
, NULL
,
1728 "Boolean telling whether the value is lazy (not fetched yet\n\
1729 from the inferior). A lazy value is fetched when needed, or when\n\
1730 the \"fetch_lazy()\" method is called.", NULL
},
1731 {NULL
} /* Sentinel */
1734 static PyMethodDef value_object_methods
[] = {
1735 { "cast", valpy_cast
, METH_VARARGS
, "Cast the value to the supplied type." },
1736 { "dynamic_cast", valpy_dynamic_cast
, METH_VARARGS
,
1737 "dynamic_cast (gdb.Type) -> gdb.Value\n\
1738 Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
1740 { "reinterpret_cast", valpy_reinterpret_cast
, METH_VARARGS
,
1741 "reinterpret_cast (gdb.Type) -> gdb.Value\n\
1742 Cast the value to the supplied type, as if by the C++\n\
1743 reinterpret_cast operator."
1745 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
1746 { "referenced_value", valpy_referenced_value
, METH_NOARGS
,
1747 "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." },
1748 { "reference_value", valpy_reference_value
, METH_NOARGS
,
1749 "Return a value of type TYPE_CODE_REF referencing this value." },
1750 { "const_value", valpy_const_value
, METH_NOARGS
,
1751 "Return a 'const' qualied version of the same value." },
1752 { "lazy_string", (PyCFunction
) valpy_lazy_string
,
1753 METH_VARARGS
| METH_KEYWORDS
,
1754 "lazy_string ([encoding] [, length]) -> lazy_string\n\
1755 Return a lazy string representation of the value." },
1756 { "string", (PyCFunction
) valpy_string
, METH_VARARGS
| METH_KEYWORDS
,
1757 "string ([encoding] [, errors] [, length]) -> string\n\
1758 Return Unicode string representation of the value." },
1759 { "fetch_lazy", valpy_fetch_lazy
, METH_NOARGS
,
1760 "Fetches the value from the inferior, if it was lazy." },
1761 {NULL
} /* Sentinel */
1764 static PyNumberMethods value_object_as_number
= {
1772 NULL
, /* nb_divmod */
1773 valpy_power
, /* nb_power */
1774 valpy_negative
, /* nb_negative */
1775 valpy_positive
, /* nb_positive */
1776 valpy_absolute
, /* nb_absolute */
1777 valpy_nonzero
, /* nb_nonzero */
1778 valpy_invert
, /* nb_invert */
1779 valpy_lsh
, /* nb_lshift */
1780 valpy_rsh
, /* nb_rshift */
1781 valpy_and
, /* nb_and */
1782 valpy_xor
, /* nb_xor */
1783 valpy_or
, /* nb_or */
1785 valpy_long
, /* nb_int */
1786 NULL
, /* reserved */
1788 NULL
, /* nb_coerce */
1789 valpy_int
, /* nb_int */
1790 valpy_long
, /* nb_long */
1792 valpy_float
, /* nb_float */
1797 NULL
, /* nb_inplace_add */
1798 NULL
, /* nb_inplace_subtract */
1799 NULL
, /* nb_inplace_multiply */
1800 NULL
, /* nb_inplace_remainder */
1801 NULL
, /* nb_inplace_power */
1802 NULL
, /* nb_inplace_lshift */
1803 NULL
, /* nb_inplace_rshift */
1804 NULL
, /* nb_inplace_and */
1805 NULL
, /* nb_inplace_xor */
1806 NULL
, /* nb_inplace_or */
1807 NULL
, /* nb_floor_divide */
1808 valpy_divide
/* nb_true_divide */
1811 static PyMappingMethods value_object_as_mapping
= {
1817 PyTypeObject value_object_type
= {
1818 PyVarObject_HEAD_INIT (NULL
, 0)
1819 "gdb.Value", /*tp_name*/
1820 sizeof (value_object
), /*tp_basicsize*/
1822 valpy_dealloc
, /*tp_dealloc*/
1828 &value_object_as_number
, /*tp_as_number*/
1829 0, /*tp_as_sequence*/
1830 &value_object_as_mapping
, /*tp_as_mapping*/
1831 valpy_hash
, /*tp_hash*/
1832 valpy_call
, /*tp_call*/
1833 valpy_str
, /*tp_str*/
1837 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
1838 | Py_TPFLAGS_BASETYPE
, /*tp_flags*/
1839 "GDB value object", /* tp_doc */
1840 0, /* tp_traverse */
1842 valpy_richcompare
, /* tp_richcompare */
1843 0, /* tp_weaklistoffset */
1845 0, /* tp_iternext */
1846 value_object_methods
, /* tp_methods */
1848 value_object_getset
, /* tp_getset */
1851 0, /* tp_descr_get */
1852 0, /* tp_descr_set */
1853 0, /* tp_dictoffset */
1856 valpy_new
/* tp_new */