1 /* Python interface to values.
3 Copyright (C) 2008-2014 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/>. */
21 #include "gdb_assert.h"
24 #include "exceptions.h"
29 #include "expression.h"
33 #include "python-internal.h"
35 /* Even though Python scalar types directly map to host types, we use
36 target types here to remain consistent with the values system in
37 GDB (which uses target arithmetic). */
39 /* Python's integer type corresponds to C's long type. */
40 #define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long
42 /* Python's float type corresponds to C's double type. */
43 #define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double
45 /* Python's long type corresponds to C's long long type. */
46 #define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long
48 /* Python's long type corresponds to C's long long type. Unsigned version. */
49 #define builtin_type_upylong builtin_type \
50 (python_gdbarch)->builtin_unsigned_long_long
52 #define builtin_type_pybool \
53 language_bool_type (python_language, python_gdbarch)
55 #define builtin_type_pychar \
56 language_string_char_type (python_language, python_gdbarch)
58 typedef struct value_object
{
60 struct value_object
*next
;
61 struct value_object
*prev
;
65 PyObject
*dynamic_type
;
68 /* List of all values which are currently exposed to Python. It is
69 maintained so that when an objfile is discarded, preserve_values
70 can copy the values' types if needed. */
71 /* This variable is unnecessarily initialized to NULL in order to
72 work around a linker bug on MacOS. */
73 static value_object
*values_in_python
= NULL
;
75 /* Called by the Python interpreter when deallocating a value object. */
77 valpy_dealloc (PyObject
*obj
)
79 value_object
*self
= (value_object
*) obj
;
81 /* Remove SELF from the global list. */
83 self
->prev
->next
= self
->next
;
86 gdb_assert (values_in_python
== self
);
87 values_in_python
= self
->next
;
90 self
->next
->prev
= self
->prev
;
92 value_free (self
->value
);
95 /* Use braces to appease gcc warning. *sigh* */
97 Py_DECREF (self
->address
);
102 Py_DECREF (self
->type
);
105 Py_XDECREF (self
->dynamic_type
);
107 Py_TYPE (self
)->tp_free (self
);
110 /* Helper to push a Value object on the global list. */
112 note_value (value_object
*value_obj
)
114 value_obj
->next
= values_in_python
;
116 value_obj
->next
->prev
= value_obj
;
117 value_obj
->prev
= NULL
;
118 values_in_python
= value_obj
;
121 /* Called when a new gdb.Value object needs to be allocated. Returns NULL on
122 error, with a python exception set. */
124 valpy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*keywords
)
126 struct value
*value
= NULL
; /* Initialize to appease gcc warning. */
127 value_object
*value_obj
;
129 if (PyTuple_Size (args
) != 1)
131 PyErr_SetString (PyExc_TypeError
, _("Value object creation takes only "
136 value_obj
= (value_object
*) subtype
->tp_alloc (subtype
, 1);
137 if (value_obj
== NULL
)
139 PyErr_SetString (PyExc_MemoryError
, _("Could not allocate memory to "
140 "create Value object."));
144 value
= convert_value_from_python (PyTuple_GetItem (args
, 0));
147 subtype
->tp_free (value_obj
);
151 value_obj
->value
= value
;
152 release_value_or_incref (value
);
153 value_obj
->address
= NULL
;
154 value_obj
->type
= NULL
;
155 value_obj
->dynamic_type
= NULL
;
156 note_value (value_obj
);
158 return (PyObject
*) value_obj
;
161 /* Iterate over all the Value objects, calling preserve_one_value on
164 gdbpy_preserve_values (const struct extension_language_defn
*extlang
,
165 struct objfile
*objfile
, htab_t copied_types
)
169 for (iter
= values_in_python
; iter
; iter
= iter
->next
)
170 preserve_one_value (iter
->value
, objfile
, copied_types
);
173 /* Given a value of a pointer type, apply the C unary * operator to it. */
175 valpy_dereference (PyObject
*self
, PyObject
*args
)
177 volatile struct gdb_exception except
;
178 PyObject
*result
= NULL
;
180 TRY_CATCH (except
, RETURN_MASK_ALL
)
182 struct value
*res_val
;
183 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
185 res_val
= value_ind (((value_object
*) self
)->value
);
186 result
= value_to_value_object (res_val
);
187 do_cleanups (cleanup
);
189 GDB_PY_HANDLE_EXCEPTION (except
);
194 /* Given a value of a pointer type or a reference type, return the value
195 referenced. The difference between this function and valpy_dereference is
196 that the latter applies * unary operator to a value, which need not always
197 result in the value referenced. For example, for a value which is a reference
198 to an 'int' pointer ('int *'), valpy_dereference will result in a value of
199 type 'int' while valpy_referenced_value will result in a value of type
203 valpy_referenced_value (PyObject
*self
, PyObject
*args
)
205 volatile struct gdb_exception except
;
206 PyObject
*result
= NULL
;
208 TRY_CATCH (except
, RETURN_MASK_ALL
)
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 GDB_PY_HANDLE_EXCEPTION (except
);
235 /* Return "&value". */
237 valpy_get_address (PyObject
*self
, void *closure
)
239 value_object
*val_obj
= (value_object
*) self
;
240 volatile struct gdb_exception except
;
242 if (!val_obj
->address
)
244 TRY_CATCH (except
, RETURN_MASK_ALL
)
246 struct value
*res_val
;
247 struct cleanup
*cleanup
248 = make_cleanup_value_free_to_mark (value_mark ());
250 res_val
= value_addr (val_obj
->value
);
251 val_obj
->address
= value_to_value_object (res_val
);
252 do_cleanups (cleanup
);
254 if (except
.reason
< 0)
256 val_obj
->address
= Py_None
;
261 Py_XINCREF (val_obj
->address
);
263 return val_obj
->address
;
266 /* Return type of the value. */
268 valpy_get_type (PyObject
*self
, void *closure
)
270 value_object
*obj
= (value_object
*) self
;
274 obj
->type
= type_to_type_object (value_type (obj
->value
));
278 Py_INCREF (obj
->type
);
282 /* Return dynamic type of the value. */
285 valpy_get_dynamic_type (PyObject
*self
, void *closure
)
287 value_object
*obj
= (value_object
*) self
;
288 volatile struct gdb_exception except
;
289 struct type
*type
= NULL
;
291 if (obj
->dynamic_type
!= NULL
)
293 Py_INCREF (obj
->dynamic_type
);
294 return obj
->dynamic_type
;
297 TRY_CATCH (except
, RETURN_MASK_ALL
)
299 struct value
*val
= obj
->value
;
300 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
302 type
= value_type (val
);
303 CHECK_TYPEDEF (type
);
305 if (((TYPE_CODE (type
) == TYPE_CODE_PTR
)
306 || (TYPE_CODE (type
) == TYPE_CODE_REF
))
307 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_CLASS
))
309 struct value
*target
;
310 int was_pointer
= TYPE_CODE (type
) == TYPE_CODE_PTR
;
312 target
= value_ind (val
);
313 type
= value_rtti_type (target
, NULL
, NULL
, NULL
);
318 type
= lookup_pointer_type (type
);
320 type
= lookup_reference_type (type
);
323 else if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
324 type
= value_rtti_type (val
, NULL
, NULL
, NULL
);
327 /* Re-use object's static type. */
331 do_cleanups (cleanup
);
333 GDB_PY_HANDLE_EXCEPTION (except
);
336 obj
->dynamic_type
= valpy_get_type (self
, NULL
);
338 obj
->dynamic_type
= type_to_type_object (type
);
340 Py_XINCREF (obj
->dynamic_type
);
341 return obj
->dynamic_type
;
344 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
345 string. Return a PyObject representing a lazy_string_object type.
346 A lazy string is a pointer to a string with an optional encoding and
347 length. If ENCODING is not given, encoding is set to None. If an
348 ENCODING is provided the encoding parameter is set to ENCODING, but
349 the string is not encoded. If LENGTH is provided then the length
350 parameter is set to LENGTH, otherwise length will be set to -1 (first
351 null of appropriate with). */
353 valpy_lazy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
355 gdb_py_longest length
= -1;
356 struct value
*value
= ((value_object
*) self
)->value
;
357 const char *user_encoding
= NULL
;
358 static char *keywords
[] = { "encoding", "length", NULL
};
359 PyObject
*str_obj
= NULL
;
360 volatile struct gdb_exception except
;
362 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|s" GDB_PY_LL_ARG
, keywords
,
363 &user_encoding
, &length
))
366 TRY_CATCH (except
, RETURN_MASK_ALL
)
368 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
370 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_PTR
)
371 value
= value_ind (value
);
373 str_obj
= gdbpy_create_lazy_string_object (value_address (value
), length
,
377 do_cleanups (cleanup
);
379 GDB_PY_HANDLE_EXCEPTION (except
);
384 /* Implementation of gdb.Value.string ([encoding] [, errors]
385 [, length]) -> string. Return Unicode string with value contents.
386 If ENCODING is not given, the string is assumed to be encoded in
387 the target's charset. If LENGTH is provided, only fetch string to
388 the length provided. */
391 valpy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
395 struct value
*value
= ((value_object
*) self
)->value
;
396 volatile struct gdb_exception except
;
398 const char *encoding
= NULL
;
399 const char *errors
= NULL
;
400 const char *user_encoding
= NULL
;
401 const char *la_encoding
= NULL
;
402 struct type
*char_type
;
403 static char *keywords
[] = { "encoding", "errors", "length", NULL
};
405 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|ssi", keywords
,
406 &user_encoding
, &errors
, &length
))
409 TRY_CATCH (except
, RETURN_MASK_ALL
)
411 LA_GET_STRING (value
, &buffer
, &length
, &char_type
, &la_encoding
);
413 GDB_PY_HANDLE_EXCEPTION (except
);
415 encoding
= (user_encoding
&& *user_encoding
) ? user_encoding
: la_encoding
;
416 unicode
= PyUnicode_Decode ((const char *) buffer
,
417 length
* TYPE_LENGTH (char_type
),
424 /* A helper function that implements the various cast operators. */
427 valpy_do_cast (PyObject
*self
, PyObject
*args
, enum exp_opcode op
)
429 PyObject
*type_obj
, *result
= NULL
;
431 volatile struct gdb_exception except
;
433 if (! PyArg_ParseTuple (args
, "O", &type_obj
))
436 type
= type_object_to_type (type_obj
);
439 PyErr_SetString (PyExc_RuntimeError
,
440 _("Argument must be a type."));
444 TRY_CATCH (except
, RETURN_MASK_ALL
)
446 struct value
*val
= ((value_object
*) self
)->value
;
447 struct value
*res_val
;
448 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
450 if (op
== UNOP_DYNAMIC_CAST
)
451 res_val
= value_dynamic_cast (type
, val
);
452 else if (op
== UNOP_REINTERPRET_CAST
)
453 res_val
= value_reinterpret_cast (type
, val
);
456 gdb_assert (op
== UNOP_CAST
);
457 res_val
= value_cast (type
, val
);
460 result
= value_to_value_object (res_val
);
461 do_cleanups (cleanup
);
463 GDB_PY_HANDLE_EXCEPTION (except
);
468 /* Implementation of the "cast" method. */
471 valpy_cast (PyObject
*self
, PyObject
*args
)
473 return valpy_do_cast (self
, args
, UNOP_CAST
);
476 /* Implementation of the "dynamic_cast" method. */
479 valpy_dynamic_cast (PyObject
*self
, PyObject
*args
)
481 return valpy_do_cast (self
, args
, UNOP_DYNAMIC_CAST
);
484 /* Implementation of the "reinterpret_cast" method. */
487 valpy_reinterpret_cast (PyObject
*self
, PyObject
*args
)
489 return valpy_do_cast (self
, args
, UNOP_REINTERPRET_CAST
);
493 valpy_length (PyObject
*self
)
495 /* We don't support getting the number of elements in a struct / class. */
496 PyErr_SetString (PyExc_NotImplementedError
,
497 _("Invalid operation on gdb.Value."));
501 /* Return 1 if the gdb.Field object FIELD is present in the value V.
502 Returns 0 otherwise. If any Python error occurs, -1 is returned. */
505 value_has_field (struct value
*v
, PyObject
*field
)
507 struct type
*parent_type
, *val_type
;
508 enum type_code type_code
;
509 PyObject
*type_object
= PyObject_GetAttrString (field
, "parent_type");
510 volatile struct gdb_exception except
;
513 if (type_object
== NULL
)
516 parent_type
= type_object_to_type (type_object
);
517 Py_DECREF (type_object
);
518 if (parent_type
== NULL
)
520 PyErr_SetString (PyExc_TypeError
,
521 _("'parent_type' attribute of gdb.Field object is not a"
522 "gdb.Type object."));
526 TRY_CATCH (except
, RETURN_MASK_ALL
)
528 val_type
= value_type (v
);
529 val_type
= check_typedef (val_type
);
530 if (TYPE_CODE (val_type
) == TYPE_CODE_REF
531 || TYPE_CODE (val_type
) == TYPE_CODE_PTR
)
532 val_type
= check_typedef (TYPE_TARGET_TYPE (val_type
));
534 type_code
= TYPE_CODE (val_type
);
535 if ((type_code
== TYPE_CODE_STRUCT
|| type_code
== TYPE_CODE_UNION
)
536 && types_equal (val_type
, parent_type
))
541 GDB_PY_SET_HANDLE_EXCEPTION (except
);
546 /* Return the value of a flag FLAG_NAME in a gdb.Field object FIELD.
547 Returns 1 if the flag value is true, 0 if it is false, and -1 if
548 a Python error occurs. */
551 get_field_flag (PyObject
*field
, const char *flag_name
)
554 /* Python 2.4 did not have a 'const' here. */
555 PyObject
*flag_object
= PyObject_GetAttrString (field
, (char *) flag_name
);
557 if (flag_object
== NULL
)
560 flag_value
= PyObject_IsTrue (flag_object
);
561 Py_DECREF (flag_object
);
566 /* Return the "type" attribute of a gdb.Field object.
567 Returns NULL on error, with a Python exception set. */
570 get_field_type (PyObject
*field
)
572 PyObject
*ftype_obj
= PyObject_GetAttrString (field
, "type");
575 if (ftype_obj
== NULL
)
577 ftype
= type_object_to_type (ftype_obj
);
578 Py_DECREF (ftype_obj
);
580 PyErr_SetString (PyExc_TypeError
,
581 _("'type' attribute of gdb.Field object is not a "
582 "gdb.Type object."));
587 /* Given string name or a gdb.Field object corresponding to an element inside
588 a structure, return its value object. Returns NULL on error, with a python
592 valpy_getitem (PyObject
*self
, PyObject
*key
)
594 value_object
*self_value
= (value_object
*) self
;
596 struct type
*base_class_type
= NULL
, *field_type
= NULL
;
598 volatile struct gdb_exception except
;
599 PyObject
*result
= NULL
;
601 if (gdbpy_is_string (key
))
603 field
= python_string_to_host_string (key
);
607 else if (gdbpy_is_field (key
))
609 int is_base_class
, valid_field
;
611 valid_field
= value_has_field (self_value
->value
, key
);
614 else if (valid_field
== 0)
616 PyErr_SetString (PyExc_TypeError
,
617 _("Invalid lookup for a field not contained in "
623 is_base_class
= get_field_flag (key
, "is_base_class");
624 if (is_base_class
< 0)
626 else if (is_base_class
> 0)
628 base_class_type
= get_field_type (key
);
629 if (base_class_type
== NULL
)
634 PyObject
*name_obj
= PyObject_GetAttrString (key
, "name");
636 if (name_obj
== NULL
)
639 if (name_obj
!= Py_None
)
641 field
= python_string_to_host_string (name_obj
);
642 Py_DECREF (name_obj
);
648 PyObject
*bitpos_obj
;
651 Py_DECREF (name_obj
);
653 if (!PyObject_HasAttrString (key
, "bitpos"))
655 PyErr_SetString (PyExc_AttributeError
,
656 _("gdb.Field object has no name and no "
657 "'bitpos' attribute."));
661 bitpos_obj
= PyObject_GetAttrString (key
, "bitpos");
662 if (bitpos_obj
== NULL
)
664 valid
= gdb_py_int_as_long (bitpos_obj
, &bitpos
);
665 Py_DECREF (bitpos_obj
);
669 field_type
= get_field_type (key
);
670 if (field_type
== NULL
)
676 TRY_CATCH (except
, RETURN_MASK_ALL
)
678 struct value
*tmp
= self_value
->value
;
679 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
680 struct value
*res_val
= NULL
;
683 res_val
= value_struct_elt (&tmp
, NULL
, field
, 0, NULL
);
684 else if (bitpos
>= 0)
685 res_val
= value_struct_elt_bitpos (&tmp
, bitpos
, field_type
,
686 "struct/class/union");
687 else if (base_class_type
!= NULL
)
689 struct type
*val_type
;
691 val_type
= check_typedef (value_type (tmp
));
692 if (TYPE_CODE (val_type
) == TYPE_CODE_PTR
)
693 res_val
= value_cast (lookup_pointer_type (base_class_type
), tmp
);
694 else if (TYPE_CODE (val_type
) == TYPE_CODE_REF
)
695 res_val
= value_cast (lookup_reference_type (base_class_type
), tmp
);
697 res_val
= value_cast (base_class_type
, tmp
);
701 /* Assume we are attempting an array access, and let the
702 value code throw an exception if the index has an invalid
704 struct value
*idx
= convert_value_from_python (key
);
708 /* Check the value's type is something that can be accessed via
712 tmp
= coerce_ref (tmp
);
713 type
= check_typedef (value_type (tmp
));
714 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
715 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
716 error (_("Cannot subscript requested type."));
718 res_val
= value_subscript (tmp
, value_as_long (idx
));
723 result
= value_to_value_object (res_val
);
724 do_cleanups (cleanup
);
728 GDB_PY_HANDLE_EXCEPTION (except
);
734 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
736 PyErr_Format (PyExc_NotImplementedError
,
737 _("Setting of struct elements is not currently supported."));
741 /* Called by the Python interpreter to perform an inferior function
742 call on the value. Returns NULL on error, with a python exception set. */
744 valpy_call (PyObject
*self
, PyObject
*args
, PyObject
*keywords
)
746 Py_ssize_t args_count
;
747 volatile struct gdb_exception except
;
748 struct value
*function
= ((value_object
*) self
)->value
;
749 struct value
**vargs
= NULL
;
750 struct type
*ftype
= NULL
;
751 struct value
*mark
= value_mark ();
752 PyObject
*result
= NULL
;
754 TRY_CATCH (except
, RETURN_MASK_ALL
)
756 ftype
= check_typedef (value_type (function
));
758 GDB_PY_HANDLE_EXCEPTION (except
);
760 if (TYPE_CODE (ftype
) != TYPE_CODE_FUNC
)
762 PyErr_SetString (PyExc_RuntimeError
,
763 _("Value is not callable (not TYPE_CODE_FUNC)."));
767 if (! PyTuple_Check (args
))
769 PyErr_SetString (PyExc_TypeError
,
770 _("Inferior arguments must be provided in a tuple."));
774 args_count
= PyTuple_Size (args
);
779 vargs
= alloca (sizeof (struct value
*) * args_count
);
780 for (i
= 0; i
< args_count
; i
++)
782 PyObject
*item
= PyTuple_GetItem (args
, i
);
787 vargs
[i
] = convert_value_from_python (item
);
788 if (vargs
[i
] == NULL
)
793 TRY_CATCH (except
, RETURN_MASK_ALL
)
795 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (mark
);
796 struct value
*return_value
;
798 return_value
= call_function_by_hand (function
, args_count
, vargs
);
799 result
= value_to_value_object (return_value
);
800 do_cleanups (cleanup
);
802 GDB_PY_HANDLE_EXCEPTION (except
);
807 /* Called by the Python interpreter to obtain string representation
810 valpy_str (PyObject
*self
)
814 struct value_print_options opts
;
815 volatile struct gdb_exception except
;
817 get_user_print_options (&opts
);
820 TRY_CATCH (except
, RETURN_MASK_ALL
)
822 struct ui_file
*stb
= mem_fileopen ();
823 struct cleanup
*old_chain
= make_cleanup_ui_file_delete (stb
);
825 common_val_print (((value_object
*) self
)->value
, stb
, 0,
826 &opts
, python_language
);
827 s
= ui_file_xstrdup (stb
, NULL
);
829 do_cleanups (old_chain
);
831 GDB_PY_HANDLE_EXCEPTION (except
);
833 result
= PyUnicode_Decode (s
, strlen (s
), host_charset (), NULL
);
839 /* Implements gdb.Value.is_optimized_out. */
841 valpy_get_is_optimized_out (PyObject
*self
, void *closure
)
843 struct value
*value
= ((value_object
*) self
)->value
;
845 volatile struct gdb_exception except
;
847 TRY_CATCH (except
, RETURN_MASK_ALL
)
849 opt
= value_optimized_out (value
);
851 GDB_PY_HANDLE_EXCEPTION (except
);
859 /* Implements gdb.Value.is_lazy. */
861 valpy_get_is_lazy (PyObject
*self
, void *closure
)
863 struct value
*value
= ((value_object
*) self
)->value
;
865 volatile struct gdb_exception except
;
867 TRY_CATCH (except
, RETURN_MASK_ALL
)
869 opt
= value_lazy (value
);
871 GDB_PY_HANDLE_EXCEPTION (except
);
879 /* Implements gdb.Value.fetch_lazy (). */
881 valpy_fetch_lazy (PyObject
*self
, PyObject
*args
)
883 struct value
*value
= ((value_object
*) self
)->value
;
884 volatile struct gdb_exception except
;
886 TRY_CATCH (except
, RETURN_MASK_ALL
)
888 if (value_lazy (value
))
889 value_fetch_lazy (value
);
891 GDB_PY_HANDLE_EXCEPTION (except
);
896 /* Calculate and return the address of the PyObject as the value of
897 the builtin __hash__ call. */
899 valpy_hash (PyObject
*self
)
901 return (long) (intptr_t) self
;
919 /* If TYPE is a reference, return the target; otherwise return TYPE. */
920 #define STRIP_REFERENCE(TYPE) \
921 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
923 /* Returns a value object which is the result of applying the operation
924 specified by OPCODE to the given arguments. Returns NULL on error, with
925 a python exception set. */
927 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
929 volatile struct gdb_exception except
;
930 PyObject
*result
= NULL
;
932 TRY_CATCH (except
, RETURN_MASK_ALL
)
934 struct value
*arg1
, *arg2
;
935 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
936 struct value
*res_val
= NULL
;
937 enum exp_opcode op
= OP_NULL
;
940 /* If the gdb.Value object is the second operand, then it will be passed
941 to us as the OTHER argument, and SELF will be an entirely different
942 kind of object, altogether. Because of this, we can't assume self is
943 a gdb.Value object and need to convert it from python as well. */
944 arg1
= convert_value_from_python (self
);
947 do_cleanups (cleanup
);
951 arg2
= convert_value_from_python (other
);
954 do_cleanups (cleanup
);
962 struct type
*ltype
= value_type (arg1
);
963 struct type
*rtype
= value_type (arg2
);
965 CHECK_TYPEDEF (ltype
);
966 ltype
= STRIP_REFERENCE (ltype
);
967 CHECK_TYPEDEF (rtype
);
968 rtype
= STRIP_REFERENCE (rtype
);
971 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
972 && is_integral_type (rtype
))
973 res_val
= value_ptradd (arg1
, value_as_long (arg2
));
974 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
975 && is_integral_type (ltype
))
976 res_val
= value_ptradd (arg2
, value_as_long (arg1
));
986 struct type
*ltype
= value_type (arg1
);
987 struct type
*rtype
= value_type (arg2
);
989 CHECK_TYPEDEF (ltype
);
990 ltype
= STRIP_REFERENCE (ltype
);
991 CHECK_TYPEDEF (rtype
);
992 rtype
= STRIP_REFERENCE (rtype
);
995 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
996 && TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
997 /* A ptrdiff_t for the target would be preferable here. */
998 res_val
= value_from_longest (builtin_type_pyint
,
999 value_ptrdiff (arg1
, arg2
));
1000 else if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
1001 && is_integral_type (rtype
))
1002 res_val
= value_ptradd (arg1
, - value_as_long (arg2
));
1029 op
= BINOP_BITWISE_AND
;
1032 op
= BINOP_BITWISE_IOR
;
1035 op
= BINOP_BITWISE_XOR
;
1041 if (binop_user_defined_p (op
, arg1
, arg2
))
1042 res_val
= value_x_binop (arg1
, arg2
, op
, OP_NULL
, EVAL_NORMAL
);
1044 res_val
= value_binop (arg1
, arg2
, op
);
1048 result
= value_to_value_object (res_val
);
1050 do_cleanups (cleanup
);
1052 GDB_PY_HANDLE_EXCEPTION (except
);
1058 valpy_add (PyObject
*self
, PyObject
*other
)
1060 return valpy_binop (VALPY_ADD
, self
, other
);
1064 valpy_subtract (PyObject
*self
, PyObject
*other
)
1066 return valpy_binop (VALPY_SUB
, self
, other
);
1070 valpy_multiply (PyObject
*self
, PyObject
*other
)
1072 return valpy_binop (VALPY_MUL
, self
, other
);
1076 valpy_divide (PyObject
*self
, PyObject
*other
)
1078 return valpy_binop (VALPY_DIV
, self
, other
);
1082 valpy_remainder (PyObject
*self
, PyObject
*other
)
1084 return valpy_binop (VALPY_REM
, self
, other
);
1088 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
1090 /* We don't support the ternary form of pow. I don't know how to express
1091 that, so let's just throw NotImplementedError to at least do something
1093 if (unused
!= Py_None
)
1095 PyErr_SetString (PyExc_NotImplementedError
,
1096 "Invalid operation on gdb.Value.");
1100 return valpy_binop (VALPY_POW
, self
, other
);
1104 valpy_negative (PyObject
*self
)
1106 volatile struct gdb_exception except
;
1107 PyObject
*result
= NULL
;
1109 TRY_CATCH (except
, RETURN_MASK_ALL
)
1111 /* Perhaps overkill, but consistency has some virtue. */
1112 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
1115 val
= value_neg (((value_object
*) self
)->value
);
1116 result
= value_to_value_object (val
);
1117 do_cleanups (cleanup
);
1119 GDB_PY_HANDLE_EXCEPTION (except
);
1125 valpy_positive (PyObject
*self
)
1127 return value_to_value_object (((value_object
*) self
)->value
);
1131 valpy_absolute (PyObject
*self
)
1133 struct value
*value
= ((value_object
*) self
)->value
;
1134 volatile struct gdb_exception except
;
1137 TRY_CATCH (except
, RETURN_MASK_ALL
)
1139 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
1141 if (value_less (value
, value_zero (value_type (value
), not_lval
)))
1144 do_cleanups (cleanup
);
1146 GDB_PY_HANDLE_EXCEPTION (except
);
1149 return valpy_positive (self
);
1151 return valpy_negative (self
);
1154 /* Implements boolean evaluation of gdb.Value. */
1156 valpy_nonzero (PyObject
*self
)
1158 volatile struct gdb_exception except
;
1159 value_object
*self_value
= (value_object
*) self
;
1161 int nonzero
= 0; /* Appease GCC warning. */
1163 TRY_CATCH (except
, RETURN_MASK_ALL
)
1165 type
= check_typedef (value_type (self_value
->value
));
1167 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
1168 nonzero
= !!value_as_long (self_value
->value
);
1169 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1170 nonzero
= value_as_double (self_value
->value
) != 0;
1171 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1172 nonzero
= !decimal_is_zero (value_contents (self_value
->value
),
1174 gdbarch_byte_order (get_type_arch (type
)));
1176 /* All other values are True. */
1179 /* This is not documented in the Python documentation, but if this
1180 function fails, return -1 as slot_nb_nonzero does (the default
1181 Python nonzero function). */
1182 GDB_PY_SET_HANDLE_EXCEPTION (except
);
1187 /* Implements ~ for value objects. */
1189 valpy_invert (PyObject
*self
)
1191 struct value
*val
= NULL
;
1192 volatile struct gdb_exception except
;
1194 TRY_CATCH (except
, RETURN_MASK_ALL
)
1196 val
= value_complement (((value_object
*) self
)->value
);
1198 GDB_PY_HANDLE_EXCEPTION (except
);
1200 return value_to_value_object (val
);
1203 /* Implements left shift for value objects. */
1205 valpy_lsh (PyObject
*self
, PyObject
*other
)
1207 return valpy_binop (VALPY_LSH
, self
, other
);
1210 /* Implements right shift for value objects. */
1212 valpy_rsh (PyObject
*self
, PyObject
*other
)
1214 return valpy_binop (VALPY_RSH
, self
, other
);
1217 /* Implements bitwise and for value objects. */
1219 valpy_and (PyObject
*self
, PyObject
*other
)
1221 return valpy_binop (VALPY_BITAND
, self
, other
);
1224 /* Implements bitwise or for value objects. */
1226 valpy_or (PyObject
*self
, PyObject
*other
)
1228 return valpy_binop (VALPY_BITOR
, self
, other
);
1231 /* Implements bitwise xor for value objects. */
1233 valpy_xor (PyObject
*self
, PyObject
*other
)
1235 return valpy_binop (VALPY_BITXOR
, self
, other
);
1238 /* Implements comparison operations for value objects. Returns NULL on error,
1239 with a python exception set. */
1241 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
1244 volatile struct gdb_exception except
;
1246 if (other
== Py_None
)
1247 /* Comparing with None is special. From what I can tell, in Python
1248 None is smaller than anything else. */
1260 PyErr_SetString (PyExc_NotImplementedError
,
1261 _("Invalid operation on gdb.Value."));
1265 TRY_CATCH (except
, RETURN_MASK_ALL
)
1267 struct value
*value_other
, *mark
= value_mark ();
1268 struct cleanup
*cleanup
;
1270 value_other
= convert_value_from_python (other
);
1271 if (value_other
== NULL
)
1277 cleanup
= make_cleanup_value_free_to_mark (mark
);
1281 result
= value_less (((value_object
*) self
)->value
, value_other
);
1284 result
= value_less (((value_object
*) self
)->value
, value_other
)
1285 || value_equal (((value_object
*) self
)->value
, value_other
);
1288 result
= value_equal (((value_object
*) self
)->value
, value_other
);
1291 result
= !value_equal (((value_object
*) self
)->value
, value_other
);
1294 result
= value_less (value_other
, ((value_object
*) self
)->value
);
1297 result
= value_less (value_other
, ((value_object
*) self
)->value
)
1298 || value_equal (((value_object
*) self
)->value
, value_other
);
1302 PyErr_SetString (PyExc_NotImplementedError
,
1303 _("Invalid operation on gdb.Value."));
1308 do_cleanups (cleanup
);
1310 GDB_PY_HANDLE_EXCEPTION (except
);
1312 /* In this case, the Python exception has already been set. */
1323 /* Implements conversion to int. */
1325 valpy_int (PyObject
*self
)
1327 struct value
*value
= ((value_object
*) self
)->value
;
1328 struct type
*type
= value_type (value
);
1330 volatile struct gdb_exception except
;
1332 TRY_CATCH (except
, RETURN_MASK_ALL
)
1334 if (!is_integral_type (type
))
1335 error (_("Cannot convert value to int."));
1337 l
= value_as_long (value
);
1339 GDB_PY_HANDLE_EXCEPTION (except
);
1341 return gdb_py_object_from_longest (l
);
1345 /* Implements conversion to long. */
1347 valpy_long (PyObject
*self
)
1349 struct value
*value
= ((value_object
*) self
)->value
;
1350 struct type
*type
= value_type (value
);
1352 volatile struct gdb_exception except
;
1354 TRY_CATCH (except
, RETURN_MASK_ALL
)
1356 CHECK_TYPEDEF (type
);
1358 if (!is_integral_type (type
)
1359 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
1360 error (_("Cannot convert value to long."));
1362 l
= value_as_long (value
);
1364 GDB_PY_HANDLE_EXCEPTION (except
);
1366 return gdb_py_long_from_longest (l
);
1369 /* Implements conversion to float. */
1371 valpy_float (PyObject
*self
)
1373 struct value
*value
= ((value_object
*) self
)->value
;
1374 struct type
*type
= value_type (value
);
1376 volatile struct gdb_exception except
;
1378 TRY_CATCH (except
, RETURN_MASK_ALL
)
1380 CHECK_TYPEDEF (type
);
1382 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
1383 error (_("Cannot convert value to float."));
1385 d
= value_as_double (value
);
1387 GDB_PY_HANDLE_EXCEPTION (except
);
1389 return PyFloat_FromDouble (d
);
1392 /* Returns an object for a value which is released from the all_values chain,
1393 so its lifetime is not bound to the execution of a command. */
1395 value_to_value_object (struct value
*val
)
1397 value_object
*val_obj
;
1399 val_obj
= PyObject_New (value_object
, &value_object_type
);
1400 if (val_obj
!= NULL
)
1402 val_obj
->value
= val
;
1403 release_value_or_incref (val
);
1404 val_obj
->address
= NULL
;
1405 val_obj
->type
= NULL
;
1406 val_obj
->dynamic_type
= NULL
;
1407 note_value (val_obj
);
1410 return (PyObject
*) val_obj
;
1413 /* Returns a borrowed reference to the struct value corresponding to
1414 the given value object. */
1416 value_object_to_value (PyObject
*self
)
1420 if (! PyObject_TypeCheck (self
, &value_object_type
))
1422 real
= (value_object
*) self
;
1426 /* Try to convert a Python value to a gdb value. If the value cannot
1427 be converted, set a Python exception and return NULL. Returns a
1428 reference to a new value on the all_values chain. */
1431 convert_value_from_python (PyObject
*obj
)
1433 struct value
*value
= NULL
; /* -Wall */
1434 volatile struct gdb_exception except
;
1437 gdb_assert (obj
!= NULL
);
1439 TRY_CATCH (except
, RETURN_MASK_ALL
)
1441 if (PyBool_Check (obj
))
1443 cmp
= PyObject_IsTrue (obj
);
1445 value
= value_from_longest (builtin_type_pybool
, cmp
);
1447 /* Make a long logic check first. In Python 3.x, internally,
1448 all integers are represented as longs. In Python 2.x, there
1449 is still a differentiation internally between a PyInt and a
1450 PyLong. Explicitly do this long check conversion first. In
1451 GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
1452 to be done first to ensure we do not lose information in the
1453 conversion process. */
1454 else if (PyLong_Check (obj
))
1456 LONGEST l
= PyLong_AsLongLong (obj
);
1458 if (PyErr_Occurred ())
1460 /* If the error was an overflow, we can try converting to
1461 ULONGEST instead. */
1462 if (PyErr_ExceptionMatches (PyExc_OverflowError
))
1464 PyObject
*etype
, *evalue
, *etraceback
, *zero
;
1466 PyErr_Fetch (&etype
, &evalue
, &etraceback
);
1467 zero
= PyInt_FromLong (0);
1469 /* Check whether obj is positive. */
1470 if (PyObject_RichCompareBool (obj
, zero
, Py_GT
) > 0)
1474 ul
= PyLong_AsUnsignedLongLong (obj
);
1475 if (! PyErr_Occurred ())
1476 value
= value_from_ulongest (builtin_type_upylong
, ul
);
1479 /* There's nothing we can do. */
1480 PyErr_Restore (etype
, evalue
, etraceback
);
1486 value
= value_from_longest (builtin_type_pylong
, l
);
1488 else if (PyInt_Check (obj
))
1490 long l
= PyInt_AsLong (obj
);
1492 if (! PyErr_Occurred ())
1493 value
= value_from_longest (builtin_type_pyint
, l
);
1495 else if (PyFloat_Check (obj
))
1497 double d
= PyFloat_AsDouble (obj
);
1499 if (! PyErr_Occurred ())
1500 value
= value_from_double (builtin_type_pyfloat
, d
);
1502 else if (gdbpy_is_string (obj
))
1506 s
= python_string_to_target_string (obj
);
1509 struct cleanup
*old
;
1511 old
= make_cleanup (xfree
, s
);
1512 value
= value_cstring (s
, strlen (s
), builtin_type_pychar
);
1516 else if (PyObject_TypeCheck (obj
, &value_object_type
))
1517 value
= value_copy (((value_object
*) obj
)->value
);
1518 else if (gdbpy_is_lazy_string (obj
))
1522 result
= PyObject_CallMethodObjArgs (obj
, gdbpy_value_cst
, NULL
);
1523 value
= value_copy (((value_object
*) result
)->value
);
1527 PyErr_Format (PyExc_TypeError
,
1528 _("Could not convert Python object: %S."), obj
);
1530 PyErr_Format (PyExc_TypeError
,
1531 _("Could not convert Python object: %s."),
1532 PyString_AsString (PyObject_Str (obj
)));
1535 if (except
.reason
< 0)
1537 PyErr_Format (except
.reason
== RETURN_QUIT
1538 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
1539 "%s", except
.message
);
1546 /* Returns value object in the ARGth position in GDB's history. */
1548 gdbpy_history (PyObject
*self
, PyObject
*args
)
1551 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
1552 volatile struct gdb_exception except
;
1554 if (!PyArg_ParseTuple (args
, "i", &i
))
1557 TRY_CATCH (except
, RETURN_MASK_ALL
)
1559 res_val
= access_value_history (i
);
1561 GDB_PY_HANDLE_EXCEPTION (except
);
1563 return value_to_value_object (res_val
);
1566 /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
1569 gdbpy_is_value_object (PyObject
*obj
)
1571 return PyObject_TypeCheck (obj
, &value_object_type
);
1575 gdbpy_initialize_values (void)
1577 if (PyType_Ready (&value_object_type
) < 0)
1580 return gdb_pymodule_addobject (gdb_module
, "Value",
1581 (PyObject
*) &value_object_type
);
1586 static PyGetSetDef value_object_getset
[] = {
1587 { "address", valpy_get_address
, NULL
, "The address of the value.",
1589 { "is_optimized_out", valpy_get_is_optimized_out
, NULL
,
1590 "Boolean telling whether the value is optimized "
1591 "out (i.e., not available).",
1593 { "type", valpy_get_type
, NULL
, "Type of the value.", NULL
},
1594 { "dynamic_type", valpy_get_dynamic_type
, NULL
,
1595 "Dynamic type of the value.", NULL
},
1596 { "is_lazy", valpy_get_is_lazy
, NULL
,
1597 "Boolean telling whether the value is lazy (not fetched yet\n\
1598 from the inferior). A lazy value is fetched when needed, or when\n\
1599 the \"fetch_lazy()\" method is called.", NULL
},
1600 {NULL
} /* Sentinel */
1603 static PyMethodDef value_object_methods
[] = {
1604 { "cast", valpy_cast
, METH_VARARGS
, "Cast the value to the supplied type." },
1605 { "dynamic_cast", valpy_dynamic_cast
, METH_VARARGS
,
1606 "dynamic_cast (gdb.Type) -> gdb.Value\n\
1607 Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
1609 { "reinterpret_cast", valpy_reinterpret_cast
, METH_VARARGS
,
1610 "reinterpret_cast (gdb.Type) -> gdb.Value\n\
1611 Cast the value to the supplied type, as if by the C++\n\
1612 reinterpret_cast operator."
1614 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
1615 { "referenced_value", valpy_referenced_value
, METH_NOARGS
,
1616 "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." },
1617 { "lazy_string", (PyCFunction
) valpy_lazy_string
,
1618 METH_VARARGS
| METH_KEYWORDS
,
1619 "lazy_string ([encoding] [, length]) -> lazy_string\n\
1620 Return a lazy string representation of the value." },
1621 { "string", (PyCFunction
) valpy_string
, METH_VARARGS
| METH_KEYWORDS
,
1622 "string ([encoding] [, errors] [, length]) -> string\n\
1623 Return Unicode string representation of the value." },
1624 { "fetch_lazy", valpy_fetch_lazy
, METH_NOARGS
,
1625 "Fetches the value from the inferior, if it was lazy." },
1626 {NULL
} /* Sentinel */
1629 static PyNumberMethods value_object_as_number
= {
1637 NULL
, /* nb_divmod */
1638 valpy_power
, /* nb_power */
1639 valpy_negative
, /* nb_negative */
1640 valpy_positive
, /* nb_positive */
1641 valpy_absolute
, /* nb_absolute */
1642 valpy_nonzero
, /* nb_nonzero */
1643 valpy_invert
, /* nb_invert */
1644 valpy_lsh
, /* nb_lshift */
1645 valpy_rsh
, /* nb_rshift */
1646 valpy_and
, /* nb_and */
1647 valpy_xor
, /* nb_xor */
1648 valpy_or
, /* nb_or */
1650 valpy_long
, /* nb_int */
1651 NULL
, /* reserved */
1653 NULL
, /* nb_coerce */
1654 valpy_int
, /* nb_int */
1655 valpy_long
, /* nb_long */
1657 valpy_float
, /* nb_float */
1662 NULL
, /* nb_inplace_add */
1663 NULL
, /* nb_inplace_subtract */
1664 NULL
, /* nb_inplace_multiply */
1665 NULL
, /* nb_inplace_remainder */
1666 NULL
, /* nb_inplace_power */
1667 NULL
, /* nb_inplace_lshift */
1668 NULL
, /* nb_inplace_rshift */
1669 NULL
, /* nb_inplace_and */
1670 NULL
, /* nb_inplace_xor */
1671 NULL
, /* nb_inplace_or */
1672 NULL
, /* nb_floor_divide */
1673 valpy_divide
/* nb_true_divide */
1676 static PyMappingMethods value_object_as_mapping
= {
1682 PyTypeObject value_object_type
= {
1683 PyVarObject_HEAD_INIT (NULL
, 0)
1684 "gdb.Value", /*tp_name*/
1685 sizeof (value_object
), /*tp_basicsize*/
1687 valpy_dealloc
, /*tp_dealloc*/
1693 &value_object_as_number
, /*tp_as_number*/
1694 0, /*tp_as_sequence*/
1695 &value_object_as_mapping
, /*tp_as_mapping*/
1696 valpy_hash
, /*tp_hash*/
1697 valpy_call
, /*tp_call*/
1698 valpy_str
, /*tp_str*/
1702 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
1703 | Py_TPFLAGS_BASETYPE
, /*tp_flags*/
1704 "GDB value object", /* tp_doc */
1705 0, /* tp_traverse */
1707 valpy_richcompare
, /* tp_richcompare */
1708 0, /* tp_weaklistoffset */
1710 0, /* tp_iternext */
1711 value_object_methods
, /* tp_methods */
1713 value_object_getset
, /* tp_getset */
1716 0, /* tp_descr_get */
1717 0, /* tp_descr_set */
1718 0, /* tp_dictoffset */
1721 valpy_new
/* tp_new */