1 /* Python interface to values.
3 Copyright (C) 2008-2017 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"
34 /* Even though Python scalar types directly map to host types, we use
35 target types here to remain consistent with the values system in
36 GDB (which uses target arithmetic). */
38 /* Python's integer type corresponds to C's long type. */
39 #define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long
41 /* Python's float type corresponds to C's double type. */
42 #define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double
44 /* Python's long type corresponds to C's long long type. */
45 #define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long
47 /* Python's long type corresponds to C's long long type. Unsigned version. */
48 #define builtin_type_upylong builtin_type \
49 (python_gdbarch)->builtin_unsigned_long_long
51 #define builtin_type_pybool \
52 language_bool_type (python_language, python_gdbarch)
54 #define builtin_type_pychar \
55 language_string_char_type (python_language, python_gdbarch)
57 typedef struct value_object
{
59 struct value_object
*next
;
60 struct value_object
*prev
;
64 PyObject
*dynamic_type
;
67 /* List of all values which are currently exposed to Python. It is
68 maintained so that when an objfile is discarded, preserve_values
69 can copy the values' types if needed. */
70 /* This variable is unnecessarily initialized to NULL in order to
71 work around a linker bug on MacOS. */
72 static value_object
*values_in_python
= NULL
;
74 /* Called by the Python interpreter when deallocating a value object. */
76 valpy_dealloc (PyObject
*obj
)
78 value_object
*self
= (value_object
*) obj
;
80 /* Remove SELF from the global list. */
82 self
->prev
->next
= self
->next
;
85 gdb_assert (values_in_python
== self
);
86 values_in_python
= self
->next
;
89 self
->next
->prev
= self
->prev
;
91 value_free (self
->value
);
94 /* Use braces to appease gcc warning. *sigh* */
96 Py_DECREF (self
->address
);
101 Py_DECREF (self
->type
);
104 Py_XDECREF (self
->dynamic_type
);
106 Py_TYPE (self
)->tp_free (self
);
109 /* Helper to push a Value object on the global list. */
111 note_value (value_object
*value_obj
)
113 value_obj
->next
= values_in_python
;
115 value_obj
->next
->prev
= value_obj
;
116 value_obj
->prev
= NULL
;
117 values_in_python
= value_obj
;
120 /* Called when a new gdb.Value object needs to be allocated. Returns NULL on
121 error, with a python exception set. */
123 valpy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*keywords
)
125 struct value
*value
= NULL
; /* Initialize to appease gcc warning. */
126 value_object
*value_obj
;
128 if (PyTuple_Size (args
) != 1)
130 PyErr_SetString (PyExc_TypeError
, _("Value object creation takes only "
135 value_obj
= (value_object
*) subtype
->tp_alloc (subtype
, 1);
136 if (value_obj
== NULL
)
138 PyErr_SetString (PyExc_MemoryError
, _("Could not allocate memory to "
139 "create Value object."));
143 value
= convert_value_from_python (PyTuple_GetItem (args
, 0));
146 subtype
->tp_free (value_obj
);
150 value_obj
->value
= value
;
151 release_value_or_incref (value
);
152 value_obj
->address
= NULL
;
153 value_obj
->type
= NULL
;
154 value_obj
->dynamic_type
= NULL
;
155 note_value (value_obj
);
157 return (PyObject
*) value_obj
;
160 /* Iterate over all the Value objects, calling preserve_one_value on
163 gdbpy_preserve_values (const struct extension_language_defn
*extlang
,
164 struct objfile
*objfile
, htab_t copied_types
)
168 for (iter
= values_in_python
; iter
; iter
= iter
->next
)
169 preserve_one_value (iter
->value
, objfile
, copied_types
);
172 /* Given a value of a pointer type, apply the C unary * operator to it. */
174 valpy_dereference (PyObject
*self
, PyObject
*args
)
176 PyObject
*result
= NULL
;
180 struct value
*res_val
;
181 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
183 res_val
= value_ind (((value_object
*) self
)->value
);
184 result
= value_to_value_object (res_val
);
185 do_cleanups (cleanup
);
187 CATCH (except
, RETURN_MASK_ALL
)
189 GDB_PY_HANDLE_EXCEPTION (except
);
196 /* Given a value of a pointer type or a reference type, return the value
197 referenced. The difference between this function and valpy_dereference is
198 that the latter applies * unary operator to a value, which need not always
199 result in the value referenced. For example, for a value which is a reference
200 to an 'int' pointer ('int *'), valpy_dereference will result in a value of
201 type 'int' while valpy_referenced_value will result in a value of type
205 valpy_referenced_value (PyObject
*self
, PyObject
*args
)
207 PyObject
*result
= NULL
;
211 struct value
*self_val
, *res_val
;
212 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
214 self_val
= ((value_object
*) self
)->value
;
215 switch (TYPE_CODE (check_typedef (value_type (self_val
))))
218 res_val
= value_ind (self_val
);
221 res_val
= coerce_ref (self_val
);
224 error(_("Trying to get the referenced value from a value which is "
225 "neither a pointer nor a reference."));
228 result
= value_to_value_object (res_val
);
229 do_cleanups (cleanup
);
231 CATCH (except
, RETURN_MASK_ALL
)
233 GDB_PY_HANDLE_EXCEPTION (except
);
240 /* Return a value which is a reference to the value. */
243 valpy_reference_value (PyObject
*self
, PyObject
*args
)
245 PyObject
*result
= NULL
;
249 struct value
*self_val
;
250 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
252 self_val
= ((value_object
*) self
)->value
;
253 result
= value_to_value_object (value_ref (self_val
));
255 do_cleanups (cleanup
);
257 CATCH (except
, RETURN_MASK_ALL
)
259 GDB_PY_HANDLE_EXCEPTION (except
);
266 /* Return a "const" qualified version of the value. */
269 valpy_const_value (PyObject
*self
, PyObject
*args
)
271 PyObject
*result
= NULL
;
275 struct value
*self_val
, *res_val
;
276 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
278 self_val
= ((value_object
*) self
)->value
;
279 res_val
= make_cv_value (1, 0, self_val
);
280 result
= value_to_value_object (res_val
);
282 do_cleanups (cleanup
);
284 CATCH (except
, RETURN_MASK_ALL
)
286 GDB_PY_HANDLE_EXCEPTION (except
);
293 /* Return "&value". */
295 valpy_get_address (PyObject
*self
, void *closure
)
297 value_object
*val_obj
= (value_object
*) self
;
299 if (!val_obj
->address
)
303 struct value
*res_val
;
304 struct cleanup
*cleanup
305 = make_cleanup_value_free_to_mark (value_mark ());
307 res_val
= value_addr (val_obj
->value
);
308 val_obj
->address
= value_to_value_object (res_val
);
309 do_cleanups (cleanup
);
311 CATCH (except
, RETURN_MASK_ALL
)
313 val_obj
->address
= Py_None
;
319 Py_XINCREF (val_obj
->address
);
321 return val_obj
->address
;
324 /* Return type of the value. */
326 valpy_get_type (PyObject
*self
, void *closure
)
328 value_object
*obj
= (value_object
*) self
;
332 obj
->type
= type_to_type_object (value_type (obj
->value
));
336 Py_INCREF (obj
->type
);
340 /* Return dynamic type of the value. */
343 valpy_get_dynamic_type (PyObject
*self
, void *closure
)
345 value_object
*obj
= (value_object
*) self
;
346 struct type
*type
= NULL
;
348 if (obj
->dynamic_type
!= NULL
)
350 Py_INCREF (obj
->dynamic_type
);
351 return obj
->dynamic_type
;
356 struct value
*val
= obj
->value
;
357 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
359 type
= value_type (val
);
360 type
= check_typedef (type
);
362 if (((TYPE_CODE (type
) == TYPE_CODE_PTR
)
363 || (TYPE_CODE (type
) == TYPE_CODE_REF
))
364 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_STRUCT
))
366 struct value
*target
;
367 int was_pointer
= TYPE_CODE (type
) == TYPE_CODE_PTR
;
370 target
= value_ind (val
);
372 target
= coerce_ref (val
);
373 type
= value_rtti_type (target
, NULL
, NULL
, NULL
);
378 type
= lookup_pointer_type (type
);
380 type
= lookup_reference_type (type
);
383 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
384 type
= value_rtti_type (val
, NULL
, NULL
, NULL
);
387 /* Re-use object's static type. */
391 do_cleanups (cleanup
);
393 CATCH (except
, RETURN_MASK_ALL
)
395 GDB_PY_HANDLE_EXCEPTION (except
);
400 obj
->dynamic_type
= valpy_get_type (self
, NULL
);
402 obj
->dynamic_type
= type_to_type_object (type
);
404 Py_XINCREF (obj
->dynamic_type
);
405 return obj
->dynamic_type
;
408 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
409 string. Return a PyObject representing a lazy_string_object type.
410 A lazy string is a pointer to a string with an optional encoding and
411 length. If ENCODING is not given, encoding is set to None. If an
412 ENCODING is provided the encoding parameter is set to ENCODING, but
413 the string is not encoded. If LENGTH is provided then the length
414 parameter is set to LENGTH, otherwise length will be set to -1 (first
415 null of appropriate with). */
417 valpy_lazy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
419 gdb_py_longest length
= -1;
420 struct value
*value
= ((value_object
*) self
)->value
;
421 const char *user_encoding
= NULL
;
422 static char *keywords
[] = { "encoding", "length", NULL
};
423 PyObject
*str_obj
= NULL
;
425 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|s" GDB_PY_LL_ARG
, keywords
,
426 &user_encoding
, &length
))
431 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
433 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_PTR
)
434 value
= value_ind (value
);
436 str_obj
= gdbpy_create_lazy_string_object (value_address (value
), length
,
440 do_cleanups (cleanup
);
442 CATCH (except
, RETURN_MASK_ALL
)
444 GDB_PY_HANDLE_EXCEPTION (except
);
451 /* Implementation of gdb.Value.string ([encoding] [, errors]
452 [, length]) -> string. Return Unicode string with value contents.
453 If ENCODING is not given, the string is assumed to be encoded in
454 the target's charset. If LENGTH is provided, only fetch string to
455 the length provided. */
458 valpy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
462 struct value
*value
= ((value_object
*) self
)->value
;
464 const char *encoding
= NULL
;
465 const char *errors
= NULL
;
466 const char *user_encoding
= NULL
;
467 const char *la_encoding
= NULL
;
468 struct type
*char_type
;
469 static char *keywords
[] = { "encoding", "errors", "length", NULL
};
471 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|ssi", keywords
,
472 &user_encoding
, &errors
, &length
))
477 LA_GET_STRING (value
, &buffer
, &length
, &char_type
, &la_encoding
);
479 CATCH (except
, RETURN_MASK_ALL
)
481 GDB_PY_HANDLE_EXCEPTION (except
);
485 encoding
= (user_encoding
&& *user_encoding
) ? user_encoding
: la_encoding
;
486 unicode
= PyUnicode_Decode ((const char *) buffer
,
487 length
* TYPE_LENGTH (char_type
),
494 /* A helper function that implements the various cast operators. */
497 valpy_do_cast (PyObject
*self
, PyObject
*args
, enum exp_opcode op
)
499 PyObject
*type_obj
, *result
= NULL
;
502 if (! PyArg_ParseTuple (args
, "O", &type_obj
))
505 type
= type_object_to_type (type_obj
);
508 PyErr_SetString (PyExc_RuntimeError
,
509 _("Argument must be a type."));
515 struct value
*val
= ((value_object
*) self
)->value
;
516 struct value
*res_val
;
517 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
519 if (op
== UNOP_DYNAMIC_CAST
)
520 res_val
= value_dynamic_cast (type
, val
);
521 else if (op
== UNOP_REINTERPRET_CAST
)
522 res_val
= value_reinterpret_cast (type
, val
);
525 gdb_assert (op
== UNOP_CAST
);
526 res_val
= value_cast (type
, val
);
529 result
= value_to_value_object (res_val
);
530 do_cleanups (cleanup
);
532 CATCH (except
, RETURN_MASK_ALL
)
534 GDB_PY_HANDLE_EXCEPTION (except
);
541 /* Implementation of the "cast" method. */
544 valpy_cast (PyObject
*self
, PyObject
*args
)
546 return valpy_do_cast (self
, args
, UNOP_CAST
);
549 /* Implementation of the "dynamic_cast" method. */
552 valpy_dynamic_cast (PyObject
*self
, PyObject
*args
)
554 return valpy_do_cast (self
, args
, UNOP_DYNAMIC_CAST
);
557 /* Implementation of the "reinterpret_cast" method. */
560 valpy_reinterpret_cast (PyObject
*self
, PyObject
*args
)
562 return valpy_do_cast (self
, args
, UNOP_REINTERPRET_CAST
);
566 valpy_length (PyObject
*self
)
568 /* We don't support getting the number of elements in a struct / class. */
569 PyErr_SetString (PyExc_NotImplementedError
,
570 _("Invalid operation on gdb.Value."));
574 /* Return 1 if the gdb.Field object FIELD is present in the value V.
575 Returns 0 otherwise. If any Python error occurs, -1 is returned. */
578 value_has_field (struct value
*v
, PyObject
*field
)
580 struct type
*parent_type
, *val_type
;
581 enum type_code type_code
;
582 gdbpy_ref
type_object (PyObject_GetAttrString (field
, "parent_type"));
585 if (type_object
== NULL
)
588 parent_type
= type_object_to_type (type_object
.get ());
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
)
628 gdbpy_ref
flag_object (PyObject_GetAttrString (field
, flag_name
));
630 if (flag_object
== NULL
)
633 return PyObject_IsTrue (flag_object
.get ());
636 /* Return the "type" attribute of a gdb.Field object.
637 Returns NULL on error, with a Python exception set. */
640 get_field_type (PyObject
*field
)
642 gdbpy_ref
ftype_obj (PyObject_GetAttrString (field
, "type"));
645 if (ftype_obj
== NULL
)
647 ftype
= type_object_to_type (ftype_obj
.get ());
649 PyErr_SetString (PyExc_TypeError
,
650 _("'type' attribute of gdb.Field object is not a "
651 "gdb.Type object."));
656 /* Given string name or a gdb.Field object corresponding to an element inside
657 a structure, return its value object. Returns NULL on error, with a python
661 valpy_getitem (PyObject
*self
, PyObject
*key
)
663 struct gdb_exception except
= exception_none
;
664 value_object
*self_value
= (value_object
*) self
;
665 gdb::unique_xmalloc_ptr
<char> field
;
666 struct type
*base_class_type
= NULL
, *field_type
= NULL
;
668 PyObject
*result
= NULL
;
670 if (gdbpy_is_string (key
))
672 field
= python_string_to_host_string (key
);
676 else if (gdbpy_is_field (key
))
678 int is_base_class
, valid_field
;
680 valid_field
= value_has_field (self_value
->value
, key
);
683 else if (valid_field
== 0)
685 PyErr_SetString (PyExc_TypeError
,
686 _("Invalid lookup for a field not contained in "
692 is_base_class
= get_field_flag (key
, "is_base_class");
693 if (is_base_class
< 0)
695 else if (is_base_class
> 0)
697 base_class_type
= get_field_type (key
);
698 if (base_class_type
== NULL
)
703 gdbpy_ref
name_obj (PyObject_GetAttrString (key
, "name"));
705 if (name_obj
== NULL
)
708 if (name_obj
!= Py_None
)
710 field
= python_string_to_host_string (name_obj
.get ());
716 if (!PyObject_HasAttrString (key
, "bitpos"))
718 PyErr_SetString (PyExc_AttributeError
,
719 _("gdb.Field object has no name and no "
720 "'bitpos' attribute."));
724 gdbpy_ref
bitpos_obj (PyObject_GetAttrString (key
, "bitpos"));
725 if (bitpos_obj
== NULL
)
727 if (!gdb_py_int_as_long (bitpos_obj
.get (), &bitpos
))
730 field_type
= get_field_type (key
);
731 if (field_type
== NULL
)
739 struct value
*tmp
= self_value
->value
;
740 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
741 struct value
*res_val
= NULL
;
744 res_val
= value_struct_elt (&tmp
, NULL
, field
.get (), NULL
,
745 "struct/class/union");
746 else if (bitpos
>= 0)
747 res_val
= value_struct_elt_bitpos (&tmp
, bitpos
, field_type
,
748 "struct/class/union");
749 else if (base_class_type
!= NULL
)
751 struct type
*val_type
;
753 val_type
= check_typedef (value_type (tmp
));
754 if (TYPE_CODE (val_type
) == TYPE_CODE_PTR
)
755 res_val
= value_cast (lookup_pointer_type (base_class_type
), tmp
);
756 else if (TYPE_CODE (val_type
) == TYPE_CODE_REF
)
757 res_val
= value_cast (lookup_reference_type (base_class_type
), tmp
);
759 res_val
= value_cast (base_class_type
, tmp
);
763 /* Assume we are attempting an array access, and let the
764 value code throw an exception if the index has an invalid
766 struct value
*idx
= convert_value_from_python (key
);
770 /* Check the value's type is something that can be accessed via
774 tmp
= coerce_ref (tmp
);
775 type
= check_typedef (value_type (tmp
));
776 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
777 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
778 error (_("Cannot subscript requested type."));
780 res_val
= value_subscript (tmp
, value_as_long (idx
));
785 result
= value_to_value_object (res_val
);
786 do_cleanups (cleanup
);
788 CATCH (ex
, RETURN_MASK_ALL
)
794 GDB_PY_HANDLE_EXCEPTION (except
);
800 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
802 PyErr_Format (PyExc_NotImplementedError
,
803 _("Setting of struct elements is not currently supported."));
807 /* Called by the Python interpreter to perform an inferior function
808 call on the value. Returns NULL on error, with a python exception set. */
810 valpy_call (PyObject
*self
, PyObject
*args
, PyObject
*keywords
)
812 Py_ssize_t args_count
;
813 struct value
*function
= ((value_object
*) self
)->value
;
814 struct value
**vargs
= NULL
;
815 struct type
*ftype
= NULL
;
816 struct value
*mark
= value_mark ();
817 PyObject
*result
= NULL
;
821 ftype
= check_typedef (value_type (function
));
823 CATCH (except
, RETURN_MASK_ALL
)
825 GDB_PY_HANDLE_EXCEPTION (except
);
829 if (TYPE_CODE (ftype
) != TYPE_CODE_FUNC
)
831 PyErr_SetString (PyExc_RuntimeError
,
832 _("Value is not callable (not TYPE_CODE_FUNC)."));
836 if (! PyTuple_Check (args
))
838 PyErr_SetString (PyExc_TypeError
,
839 _("Inferior arguments must be provided in a tuple."));
843 args_count
= PyTuple_Size (args
);
848 vargs
= XALLOCAVEC (struct value
*, args_count
);
849 for (i
= 0; i
< args_count
; i
++)
851 PyObject
*item
= PyTuple_GetItem (args
, i
);
856 vargs
[i
] = convert_value_from_python (item
);
857 if (vargs
[i
] == NULL
)
864 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (mark
);
865 struct value
*return_value
;
867 return_value
= call_function_by_hand (function
, args_count
, vargs
);
868 result
= value_to_value_object (return_value
);
869 do_cleanups (cleanup
);
871 CATCH (except
, RETURN_MASK_ALL
)
873 GDB_PY_HANDLE_EXCEPTION (except
);
880 /* Called by the Python interpreter to obtain string representation
883 valpy_str (PyObject
*self
)
887 struct value_print_options opts
;
889 get_user_print_options (&opts
);
894 struct ui_file
*stb
= mem_fileopen ();
895 struct cleanup
*old_chain
= make_cleanup_ui_file_delete (stb
);
897 common_val_print (((value_object
*) self
)->value
, stb
, 0,
898 &opts
, python_language
);
899 s
= ui_file_as_string (stb
);
901 do_cleanups (old_chain
);
903 CATCH (except
, RETURN_MASK_ALL
)
905 GDB_PY_HANDLE_EXCEPTION (except
);
909 result
= PyUnicode_Decode (s
.c_str (), s
.length (), host_charset (), NULL
);
914 /* Implements gdb.Value.is_optimized_out. */
916 valpy_get_is_optimized_out (PyObject
*self
, void *closure
)
918 struct value
*value
= ((value_object
*) self
)->value
;
923 opt
= value_optimized_out (value
);
925 CATCH (except
, RETURN_MASK_ALL
)
927 GDB_PY_HANDLE_EXCEPTION (except
);
937 /* Implements gdb.Value.is_lazy. */
939 valpy_get_is_lazy (PyObject
*self
, void *closure
)
941 struct value
*value
= ((value_object
*) self
)->value
;
946 opt
= value_lazy (value
);
948 CATCH (except
, RETURN_MASK_ALL
)
950 GDB_PY_HANDLE_EXCEPTION (except
);
960 /* Implements gdb.Value.fetch_lazy (). */
962 valpy_fetch_lazy (PyObject
*self
, PyObject
*args
)
964 struct value
*value
= ((value_object
*) self
)->value
;
968 if (value_lazy (value
))
969 value_fetch_lazy (value
);
971 CATCH (except
, RETURN_MASK_ALL
)
973 GDB_PY_HANDLE_EXCEPTION (except
);
980 /* Calculate and return the address of the PyObject as the value of
981 the builtin __hash__ call. */
983 valpy_hash (PyObject
*self
)
985 return (intptr_t) self
;
1003 /* If TYPE is a reference, return the target; otherwise return TYPE. */
1004 #define STRIP_REFERENCE(TYPE) \
1005 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
1007 /* Helper for valpy_binop. Returns a value object which is the result
1008 of applying the operation specified by OPCODE to the given
1009 arguments. Throws a GDB exception on error. */
1012 valpy_binop_throw (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
1014 PyObject
*result
= NULL
;
1016 struct value
*arg1
, *arg2
;
1017 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
1018 struct value
*res_val
= NULL
;
1019 enum exp_opcode op
= OP_NULL
;
1022 /* If the gdb.Value object is the second operand, then it will be
1023 passed to us as the OTHER argument, and SELF will be an entirely
1024 different kind of object, altogether. Because of this, we can't
1025 assume self is a gdb.Value object and need to convert it from
1027 arg1
= convert_value_from_python (self
);
1030 do_cleanups (cleanup
);
1034 arg2
= convert_value_from_python (other
);
1037 do_cleanups (cleanup
);
1045 struct type
*ltype
= value_type (arg1
);
1046 struct type
*rtype
= value_type (arg2
);
1048 ltype
= check_typedef (ltype
);
1049 ltype
= STRIP_REFERENCE (ltype
);
1050 rtype
= check_typedef (rtype
);
1051 rtype
= STRIP_REFERENCE (rtype
);
1054 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
1055 && is_integral_type (rtype
))
1056 res_val
= value_ptradd (arg1
, value_as_long (arg2
));
1057 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
1058 && is_integral_type (ltype
))
1059 res_val
= value_ptradd (arg2
, value_as_long (arg1
));
1069 struct type
*ltype
= value_type (arg1
);
1070 struct type
*rtype
= value_type (arg2
);
1072 ltype
= check_typedef (ltype
);
1073 ltype
= STRIP_REFERENCE (ltype
);
1074 rtype
= check_typedef (rtype
);
1075 rtype
= STRIP_REFERENCE (rtype
);
1078 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
1079 && TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
1080 /* A ptrdiff_t for the target would be preferable here. */
1081 res_val
= value_from_longest (builtin_type_pyint
,
1082 value_ptrdiff (arg1
, arg2
));
1083 else if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
1084 && is_integral_type (rtype
))
1085 res_val
= value_ptradd (arg1
, - value_as_long (arg2
));
1112 op
= BINOP_BITWISE_AND
;
1115 op
= BINOP_BITWISE_IOR
;
1118 op
= BINOP_BITWISE_XOR
;
1124 if (binop_user_defined_p (op
, arg1
, arg2
))
1125 res_val
= value_x_binop (arg1
, arg2
, op
, OP_NULL
, EVAL_NORMAL
);
1127 res_val
= value_binop (arg1
, arg2
, op
);
1131 result
= value_to_value_object (res_val
);
1133 do_cleanups (cleanup
);
1137 /* Returns a value object which is the result of applying the operation
1138 specified by OPCODE to the given arguments. Returns NULL on error, with
1139 a python exception set. */
1141 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
1143 PyObject
*result
= NULL
;
1147 result
= valpy_binop_throw (opcode
, self
, other
);
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 /* Helper for valpy_richcompare. Implements comparison operations for
1355 value objects. Returns true/false on success. Returns -1 with a
1356 Python exception set if a Python error is detected. Throws a GDB
1357 exception on other errors (memory error, etc.). */
1360 valpy_richcompare_throw (PyObject
*self
, PyObject
*other
, int op
)
1363 struct value
*value_other
;
1364 struct value
*value_self
;
1365 struct value
*mark
= value_mark ();
1366 struct cleanup
*cleanup
;
1368 value_other
= convert_value_from_python (other
);
1369 if (value_other
== NULL
)
1372 cleanup
= make_cleanup_value_free_to_mark (mark
);
1374 value_self
= ((value_object
*) self
)->value
;
1379 result
= value_less (value_self
, value_other
);
1382 result
= value_less (value_self
, value_other
)
1383 || value_equal (value_self
, value_other
);
1386 result
= value_equal (value_self
, value_other
);
1389 result
= !value_equal (value_self
, value_other
);
1392 result
= value_less (value_other
, value_self
);
1395 result
= (value_less (value_other
, value_self
)
1396 || value_equal (value_self
, value_other
));
1400 PyErr_SetString (PyExc_NotImplementedError
,
1401 _("Invalid operation on gdb.Value."));
1406 do_cleanups (cleanup
);
1411 /* Implements comparison operations for value objects. Returns NULL on error,
1412 with a python exception set. */
1414 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
1418 if (other
== Py_None
)
1419 /* Comparing with None is special. From what I can tell, in Python
1420 None is smaller than anything else. */
1432 PyErr_SetString (PyExc_NotImplementedError
,
1433 _("Invalid operation on gdb.Value."));
1439 result
= valpy_richcompare_throw (self
, other
, op
);
1441 CATCH (except
, RETURN_MASK_ALL
)
1443 GDB_PY_HANDLE_EXCEPTION (except
);
1447 /* In this case, the Python exception has already been set. */
1458 /* Implements conversion to int. */
1460 valpy_int (PyObject
*self
)
1462 struct value
*value
= ((value_object
*) self
)->value
;
1463 struct type
*type
= value_type (value
);
1468 if (!is_integral_type (type
))
1469 error (_("Cannot convert value to int."));
1471 l
= value_as_long (value
);
1473 CATCH (except
, RETURN_MASK_ALL
)
1475 GDB_PY_HANDLE_EXCEPTION (except
);
1479 return gdb_py_object_from_longest (l
);
1483 /* Implements conversion to long. */
1485 valpy_long (PyObject
*self
)
1487 struct value
*value
= ((value_object
*) self
)->value
;
1488 struct type
*type
= value_type (value
);
1493 type
= check_typedef (type
);
1495 if (!is_integral_type (type
)
1496 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
1497 error (_("Cannot convert value to long."));
1499 l
= value_as_long (value
);
1501 CATCH (except
, RETURN_MASK_ALL
)
1503 GDB_PY_HANDLE_EXCEPTION (except
);
1507 if (TYPE_UNSIGNED (type
))
1508 return gdb_py_long_from_ulongest (l
);
1510 return gdb_py_long_from_longest (l
);
1513 /* Implements conversion to float. */
1515 valpy_float (PyObject
*self
)
1517 struct value
*value
= ((value_object
*) self
)->value
;
1518 struct type
*type
= value_type (value
);
1523 type
= check_typedef (type
);
1525 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
1526 error (_("Cannot convert value to float."));
1528 d
= value_as_double (value
);
1530 CATCH (except
, RETURN_MASK_ALL
)
1532 GDB_PY_HANDLE_EXCEPTION (except
);
1536 return PyFloat_FromDouble (d
);
1539 /* Returns an object for a value which is released from the all_values chain,
1540 so its lifetime is not bound to the execution of a command. */
1542 value_to_value_object (struct value
*val
)
1544 value_object
*val_obj
;
1546 val_obj
= PyObject_New (value_object
, &value_object_type
);
1547 if (val_obj
!= NULL
)
1549 val_obj
->value
= val
;
1550 release_value_or_incref (val
);
1551 val_obj
->address
= NULL
;
1552 val_obj
->type
= NULL
;
1553 val_obj
->dynamic_type
= NULL
;
1554 note_value (val_obj
);
1557 return (PyObject
*) val_obj
;
1560 /* Returns a borrowed reference to the struct value corresponding to
1561 the given value object. */
1563 value_object_to_value (PyObject
*self
)
1567 if (! PyObject_TypeCheck (self
, &value_object_type
))
1569 real
= (value_object
*) self
;
1573 /* Try to convert a Python value to a gdb value. If the value cannot
1574 be converted, set a Python exception and return NULL. Returns a
1575 reference to a new value on the all_values chain. */
1578 convert_value_from_python (PyObject
*obj
)
1580 struct value
*value
= NULL
; /* -Wall */
1583 gdb_assert (obj
!= NULL
);
1587 if (PyBool_Check (obj
))
1589 cmp
= PyObject_IsTrue (obj
);
1591 value
= value_from_longest (builtin_type_pybool
, cmp
);
1593 /* Make a long logic check first. In Python 3.x, internally,
1594 all integers are represented as longs. In Python 2.x, there
1595 is still a differentiation internally between a PyInt and a
1596 PyLong. Explicitly do this long check conversion first. In
1597 GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
1598 to be done first to ensure we do not lose information in the
1599 conversion process. */
1600 else if (PyLong_Check (obj
))
1602 LONGEST l
= PyLong_AsLongLong (obj
);
1604 if (PyErr_Occurred ())
1606 /* If the error was an overflow, we can try converting to
1607 ULONGEST instead. */
1608 if (PyErr_ExceptionMatches (PyExc_OverflowError
))
1610 PyObject
*etype
, *evalue
, *etraceback
;
1612 PyErr_Fetch (&etype
, &evalue
, &etraceback
);
1613 gdbpy_ref
zero (PyInt_FromLong (0));
1615 /* Check whether obj is positive. */
1616 if (PyObject_RichCompareBool (obj
, zero
.get (), Py_GT
) > 0)
1620 ul
= PyLong_AsUnsignedLongLong (obj
);
1621 if (! PyErr_Occurred ())
1622 value
= value_from_ulongest (builtin_type_upylong
, ul
);
1625 /* There's nothing we can do. */
1626 PyErr_Restore (etype
, evalue
, etraceback
);
1630 value
= value_from_longest (builtin_type_pylong
, l
);
1632 #if PY_MAJOR_VERSION == 2
1633 else if (PyInt_Check (obj
))
1635 long l
= PyInt_AsLong (obj
);
1637 if (! PyErr_Occurred ())
1638 value
= value_from_longest (builtin_type_pyint
, l
);
1641 else if (PyFloat_Check (obj
))
1643 double d
= PyFloat_AsDouble (obj
);
1645 if (! PyErr_Occurred ())
1646 value
= value_from_double (builtin_type_pyfloat
, d
);
1648 else if (gdbpy_is_string (obj
))
1650 gdb::unique_xmalloc_ptr
<char> s
1651 = python_string_to_target_string (obj
);
1653 value
= value_cstring (s
.get (), strlen (s
.get ()),
1654 builtin_type_pychar
);
1656 else if (PyObject_TypeCheck (obj
, &value_object_type
))
1657 value
= value_copy (((value_object
*) obj
)->value
);
1658 else if (gdbpy_is_lazy_string (obj
))
1662 result
= PyObject_CallMethodObjArgs (obj
, gdbpy_value_cst
, NULL
);
1663 value
= value_copy (((value_object
*) result
)->value
);
1667 PyErr_Format (PyExc_TypeError
,
1668 _("Could not convert Python object: %S."), obj
);
1670 PyErr_Format (PyExc_TypeError
,
1671 _("Could not convert Python object: %s."),
1672 PyString_AsString (PyObject_Str (obj
)));
1675 CATCH (except
, RETURN_MASK_ALL
)
1677 PyErr_Format (except
.reason
== RETURN_QUIT
1678 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
1679 "%s", except
.message
);
1687 /* Returns value object in the ARGth position in GDB's history. */
1689 gdbpy_history (PyObject
*self
, PyObject
*args
)
1692 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
1694 if (!PyArg_ParseTuple (args
, "i", &i
))
1699 res_val
= access_value_history (i
);
1701 CATCH (except
, RETURN_MASK_ALL
)
1703 GDB_PY_HANDLE_EXCEPTION (except
);
1707 return value_to_value_object (res_val
);
1710 /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
1713 gdbpy_is_value_object (PyObject
*obj
)
1715 return PyObject_TypeCheck (obj
, &value_object_type
);
1719 gdbpy_initialize_values (void)
1721 if (PyType_Ready (&value_object_type
) < 0)
1724 return gdb_pymodule_addobject (gdb_module
, "Value",
1725 (PyObject
*) &value_object_type
);
1730 static PyGetSetDef value_object_getset
[] = {
1731 { "address", valpy_get_address
, NULL
, "The address of the value.",
1733 { "is_optimized_out", valpy_get_is_optimized_out
, NULL
,
1734 "Boolean telling whether the value is optimized "
1735 "out (i.e., not available).",
1737 { "type", valpy_get_type
, NULL
, "Type of the value.", NULL
},
1738 { "dynamic_type", valpy_get_dynamic_type
, NULL
,
1739 "Dynamic type of the value.", NULL
},
1740 { "is_lazy", valpy_get_is_lazy
, NULL
,
1741 "Boolean telling whether the value is lazy (not fetched yet\n\
1742 from the inferior). A lazy value is fetched when needed, or when\n\
1743 the \"fetch_lazy()\" method is called.", NULL
},
1744 {NULL
} /* Sentinel */
1747 static PyMethodDef value_object_methods
[] = {
1748 { "cast", valpy_cast
, METH_VARARGS
, "Cast the value to the supplied type." },
1749 { "dynamic_cast", valpy_dynamic_cast
, METH_VARARGS
,
1750 "dynamic_cast (gdb.Type) -> gdb.Value\n\
1751 Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
1753 { "reinterpret_cast", valpy_reinterpret_cast
, METH_VARARGS
,
1754 "reinterpret_cast (gdb.Type) -> gdb.Value\n\
1755 Cast the value to the supplied type, as if by the C++\n\
1756 reinterpret_cast operator."
1758 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
1759 { "referenced_value", valpy_referenced_value
, METH_NOARGS
,
1760 "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." },
1761 { "reference_value", valpy_reference_value
, METH_NOARGS
,
1762 "Return a value of type TYPE_CODE_REF referencing this value." },
1763 { "const_value", valpy_const_value
, METH_NOARGS
,
1764 "Return a 'const' qualied version of the same value." },
1765 { "lazy_string", (PyCFunction
) valpy_lazy_string
,
1766 METH_VARARGS
| METH_KEYWORDS
,
1767 "lazy_string ([encoding] [, length]) -> lazy_string\n\
1768 Return a lazy string representation of the value." },
1769 { "string", (PyCFunction
) valpy_string
, METH_VARARGS
| METH_KEYWORDS
,
1770 "string ([encoding] [, errors] [, length]) -> string\n\
1771 Return Unicode string representation of the value." },
1772 { "fetch_lazy", valpy_fetch_lazy
, METH_NOARGS
,
1773 "Fetches the value from the inferior, if it was lazy." },
1774 {NULL
} /* Sentinel */
1777 static PyNumberMethods value_object_as_number
= {
1785 NULL
, /* nb_divmod */
1786 valpy_power
, /* nb_power */
1787 valpy_negative
, /* nb_negative */
1788 valpy_positive
, /* nb_positive */
1789 valpy_absolute
, /* nb_absolute */
1790 valpy_nonzero
, /* nb_nonzero */
1791 valpy_invert
, /* nb_invert */
1792 valpy_lsh
, /* nb_lshift */
1793 valpy_rsh
, /* nb_rshift */
1794 valpy_and
, /* nb_and */
1795 valpy_xor
, /* nb_xor */
1796 valpy_or
, /* nb_or */
1798 valpy_long
, /* nb_int */
1799 NULL
, /* reserved */
1801 NULL
, /* nb_coerce */
1802 valpy_int
, /* nb_int */
1803 valpy_long
, /* nb_long */
1805 valpy_float
, /* nb_float */
1810 NULL
, /* nb_inplace_add */
1811 NULL
, /* nb_inplace_subtract */
1812 NULL
, /* nb_inplace_multiply */
1814 NULL
, /* nb_inplace_divide */
1816 NULL
, /* nb_inplace_remainder */
1817 NULL
, /* nb_inplace_power */
1818 NULL
, /* nb_inplace_lshift */
1819 NULL
, /* nb_inplace_rshift */
1820 NULL
, /* nb_inplace_and */
1821 NULL
, /* nb_inplace_xor */
1822 NULL
, /* nb_inplace_or */
1823 NULL
, /* nb_floor_divide */
1824 valpy_divide
, /* nb_true_divide */
1825 NULL
, /* nb_inplace_floor_divide */
1826 NULL
, /* nb_inplace_true_divide */
1827 #ifndef HAVE_LIBPYTHON2_4
1828 /* This was added in Python 2.5. */
1829 valpy_long
, /* nb_index */
1830 #endif /* HAVE_LIBPYTHON2_4 */
1833 static PyMappingMethods value_object_as_mapping
= {
1839 PyTypeObject value_object_type
= {
1840 PyVarObject_HEAD_INIT (NULL
, 0)
1841 "gdb.Value", /*tp_name*/
1842 sizeof (value_object
), /*tp_basicsize*/
1844 valpy_dealloc
, /*tp_dealloc*/
1850 &value_object_as_number
, /*tp_as_number*/
1851 0, /*tp_as_sequence*/
1852 &value_object_as_mapping
, /*tp_as_mapping*/
1853 valpy_hash
, /*tp_hash*/
1854 valpy_call
, /*tp_call*/
1855 valpy_str
, /*tp_str*/
1859 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
1860 | Py_TPFLAGS_BASETYPE
, /*tp_flags*/
1861 "GDB value object", /* tp_doc */
1862 0, /* tp_traverse */
1864 valpy_richcompare
, /* tp_richcompare */
1865 0, /* tp_weaklistoffset */
1867 0, /* tp_iternext */
1868 value_object_methods
, /* tp_methods */
1870 value_object_getset
, /* tp_getset */
1873 0, /* tp_descr_get */
1874 0, /* tp_descr_set */
1875 0, /* tp_dictoffset */
1878 valpy_new
/* tp_new */