1 /* Python interface to values.
3 Copyright (C) 2008-2019 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/>. */
24 #include "target-float.h"
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_decref (self
->value
);
92 Py_XDECREF (self
->address
);
93 Py_XDECREF (self
->type
);
94 Py_XDECREF (self
->dynamic_type
);
96 Py_TYPE (self
)->tp_free (self
);
99 /* Helper to push a Value object on the global list. */
101 note_value (value_object
*value_obj
)
103 value_obj
->next
= values_in_python
;
105 value_obj
->next
->prev
= value_obj
;
106 value_obj
->prev
= NULL
;
107 values_in_python
= value_obj
;
110 /* Convert a python object OBJ with type TYPE to a gdb value. The
111 python object in question must conform to the python buffer
112 protocol. On success, return the converted value, otherwise
115 static struct value
*
116 convert_buffer_and_type_to_value (PyObject
*obj
, struct type
*type
)
118 Py_buffer_up buffer_up
;
121 if (PyObject_CheckBuffer (obj
)
122 && PyObject_GetBuffer (obj
, &py_buf
, PyBUF_SIMPLE
) == 0)
124 /* Got a buffer, py_buf, out of obj. Cause it to be released
125 when it goes out of scope. */
126 buffer_up
.reset (&py_buf
);
130 PyErr_SetString (PyExc_TypeError
,
131 _("Object must support the python buffer protocol."));
135 if (TYPE_LENGTH (type
) > py_buf
.len
)
137 PyErr_SetString (PyExc_ValueError
,
138 _("Size of type is larger than that of buffer object."));
142 return value_from_contents (type
, (const gdb_byte
*) py_buf
.buf
);
145 /* Called when a new gdb.Value object needs to be allocated. Returns NULL on
146 error, with a python exception set. */
148 valpy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*kwargs
)
150 static const char *keywords
[] = { "val", "type", NULL
};
151 PyObject
*val_obj
= nullptr;
152 PyObject
*type_obj
= nullptr;
154 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kwargs
, "O|O", keywords
,
155 &val_obj
, &type_obj
))
158 struct type
*type
= nullptr;
160 if (type_obj
!= nullptr)
162 type
= type_object_to_type (type_obj
);
165 PyErr_SetString (PyExc_TypeError
,
166 _("type argument must be a gdb.Type."));
171 value_object
*value_obj
= (value_object
*) subtype
->tp_alloc (subtype
, 1);
172 if (value_obj
== NULL
)
174 PyErr_SetString (PyExc_MemoryError
, _("Could not allocate memory to "
175 "create Value object."));
182 value
= convert_value_from_python (val_obj
);
184 value
= convert_buffer_and_type_to_value (val_obj
, type
);
186 if (value
== nullptr)
188 subtype
->tp_free (value_obj
);
192 value_obj
->value
= release_value (value
).release ();
193 value_obj
->address
= NULL
;
194 value_obj
->type
= NULL
;
195 value_obj
->dynamic_type
= NULL
;
196 note_value (value_obj
);
198 return (PyObject
*) value_obj
;
201 /* Iterate over all the Value objects, calling preserve_one_value on
204 gdbpy_preserve_values (const struct extension_language_defn
*extlang
,
205 struct objfile
*objfile
, htab_t copied_types
)
209 for (iter
= values_in_python
; iter
; iter
= iter
->next
)
210 preserve_one_value (iter
->value
, objfile
, copied_types
);
213 /* Given a value of a pointer type, apply the C unary * operator to it. */
215 valpy_dereference (PyObject
*self
, PyObject
*args
)
217 PyObject
*result
= NULL
;
221 struct value
*res_val
;
222 scoped_value_mark free_values
;
224 res_val
= value_ind (((value_object
*) self
)->value
);
225 result
= value_to_value_object (res_val
);
227 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
229 GDB_PY_HANDLE_EXCEPTION (except
);
235 /* Given a value of a pointer type or a reference type, return the value
236 referenced. The difference between this function and valpy_dereference is
237 that the latter applies * unary operator to a value, which need not always
238 result in the value referenced. For example, for a value which is a reference
239 to an 'int' pointer ('int *'), valpy_dereference will result in a value of
240 type 'int' while valpy_referenced_value will result in a value of type
244 valpy_referenced_value (PyObject
*self
, PyObject
*args
)
246 PyObject
*result
= NULL
;
250 struct value
*self_val
, *res_val
;
251 scoped_value_mark free_values
;
253 self_val
= ((value_object
*) self
)->value
;
254 switch (TYPE_CODE (check_typedef (value_type (self_val
))))
257 res_val
= value_ind (self_val
);
260 case TYPE_CODE_RVALUE_REF
:
261 res_val
= coerce_ref (self_val
);
264 error(_("Trying to get the referenced value from a value which is "
265 "neither a pointer nor a reference."));
268 result
= value_to_value_object (res_val
);
270 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
272 GDB_PY_HANDLE_EXCEPTION (except
);
278 /* Return a value which is a reference to the value. */
281 valpy_reference_value (PyObject
*self
, PyObject
*args
, enum type_code refcode
)
283 PyObject
*result
= NULL
;
287 struct value
*self_val
;
288 scoped_value_mark free_values
;
290 self_val
= ((value_object
*) self
)->value
;
291 result
= value_to_value_object (value_ref (self_val
, refcode
));
293 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
295 GDB_PY_HANDLE_EXCEPTION (except
);
302 valpy_lvalue_reference_value (PyObject
*self
, PyObject
*args
)
304 return valpy_reference_value (self
, args
, TYPE_CODE_REF
);
308 valpy_rvalue_reference_value (PyObject
*self
, PyObject
*args
)
310 return valpy_reference_value (self
, args
, TYPE_CODE_RVALUE_REF
);
313 /* Return a "const" qualified version of the value. */
316 valpy_const_value (PyObject
*self
, PyObject
*args
)
318 PyObject
*result
= NULL
;
322 struct value
*self_val
, *res_val
;
323 scoped_value_mark free_values
;
325 self_val
= ((value_object
*) self
)->value
;
326 res_val
= make_cv_value (1, 0, self_val
);
327 result
= value_to_value_object (res_val
);
329 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
331 GDB_PY_HANDLE_EXCEPTION (except
);
337 /* Return "&value". */
339 valpy_get_address (PyObject
*self
, void *closure
)
341 value_object
*val_obj
= (value_object
*) self
;
343 if (!val_obj
->address
)
347 struct value
*res_val
;
348 scoped_value_mark free_values
;
350 res_val
= value_addr (val_obj
->value
);
351 val_obj
->address
= value_to_value_object (res_val
);
353 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
355 val_obj
->address
= Py_None
;
360 Py_XINCREF (val_obj
->address
);
362 return val_obj
->address
;
365 /* Return type of the value. */
367 valpy_get_type (PyObject
*self
, void *closure
)
369 value_object
*obj
= (value_object
*) self
;
373 obj
->type
= type_to_type_object (value_type (obj
->value
));
377 Py_INCREF (obj
->type
);
381 /* Return dynamic type of the value. */
384 valpy_get_dynamic_type (PyObject
*self
, void *closure
)
386 value_object
*obj
= (value_object
*) self
;
387 struct type
*type
= NULL
;
389 if (obj
->dynamic_type
!= NULL
)
391 Py_INCREF (obj
->dynamic_type
);
392 return obj
->dynamic_type
;
397 struct value
*val
= obj
->value
;
398 scoped_value_mark free_values
;
400 type
= value_type (val
);
401 type
= check_typedef (type
);
403 if (((TYPE_CODE (type
) == TYPE_CODE_PTR
) || TYPE_IS_REFERENCE (type
))
404 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_STRUCT
))
406 struct value
*target
;
407 int was_pointer
= TYPE_CODE (type
) == TYPE_CODE_PTR
;
410 target
= value_ind (val
);
412 target
= coerce_ref (val
);
413 type
= value_rtti_type (target
, NULL
, NULL
, NULL
);
418 type
= lookup_pointer_type (type
);
420 type
= lookup_lvalue_reference_type (type
);
423 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
424 type
= value_rtti_type (val
, NULL
, NULL
, NULL
);
427 /* Re-use object's static type. */
431 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
433 GDB_PY_HANDLE_EXCEPTION (except
);
437 obj
->dynamic_type
= valpy_get_type (self
, NULL
);
439 obj
->dynamic_type
= type_to_type_object (type
);
441 Py_XINCREF (obj
->dynamic_type
);
442 return obj
->dynamic_type
;
445 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
446 string. Return a PyObject representing a lazy_string_object type.
447 A lazy string is a pointer to a string with an optional encoding and
448 length. If ENCODING is not given, encoding is set to None. If an
449 ENCODING is provided the encoding parameter is set to ENCODING, but
450 the string is not encoded.
451 If LENGTH is provided then the length parameter is set to LENGTH.
452 Otherwise if the value is an array of known length then the array's length
453 is used. Otherwise the length will be set to -1 (meaning first null of
456 Note: In order to not break any existing uses this allows creating
457 lazy strings from anything. PR 20769. E.g.,
458 gdb.parse_and_eval("my_int_variable").lazy_string().
459 "It's easier to relax restrictions than it is to impose them after the
460 fact." So we should be flagging any unintended uses as errors, but it's
461 perhaps too late for that. */
464 valpy_lazy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
466 gdb_py_longest length
= -1;
467 struct value
*value
= ((value_object
*) self
)->value
;
468 const char *user_encoding
= NULL
;
469 static const char *keywords
[] = { "encoding", "length", NULL
};
470 PyObject
*str_obj
= NULL
;
472 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "|s" GDB_PY_LL_ARG
,
473 keywords
, &user_encoding
, &length
))
478 PyErr_SetString (PyExc_ValueError
, _("Invalid length."));
484 scoped_value_mark free_values
;
485 struct type
*type
, *realtype
;
488 type
= value_type (value
);
489 realtype
= check_typedef (type
);
491 switch (TYPE_CODE (realtype
))
493 case TYPE_CODE_ARRAY
:
495 LONGEST array_length
= -1;
496 LONGEST low_bound
, high_bound
;
498 /* PR 20786: There's no way to specify an array of length zero.
499 Record a length of [0,-1] which is how Ada does it. Anything
500 we do is broken, but this one possible solution. */
501 if (get_array_bounds (realtype
, &low_bound
, &high_bound
))
502 array_length
= high_bound
- low_bound
+ 1;
504 length
= array_length
;
505 else if (array_length
== -1)
507 type
= lookup_array_range_type (TYPE_TARGET_TYPE (realtype
),
510 else if (length
!= array_length
)
512 /* We need to create a new array type with the
514 if (length
> array_length
)
515 error (_("Length is larger than array size."));
516 type
= lookup_array_range_type (TYPE_TARGET_TYPE (realtype
),
518 low_bound
+ length
- 1);
520 addr
= value_address (value
);
524 /* If a length is specified we defer creating an array of the
525 specified width until we need to. */
526 addr
= value_as_address (value
);
529 /* Should flag an error here. PR 20769. */
530 addr
= value_address (value
);
534 str_obj
= gdbpy_create_lazy_string_object (addr
, length
, user_encoding
,
537 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
539 GDB_PY_HANDLE_EXCEPTION (except
);
545 /* Implementation of gdb.Value.string ([encoding] [, errors]
546 [, length]) -> string. Return Unicode string with value contents.
547 If ENCODING is not given, the string is assumed to be encoded in
548 the target's charset. If LENGTH is provided, only fetch string to
549 the length provided. */
552 valpy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
555 gdb::unique_xmalloc_ptr
<gdb_byte
> buffer
;
556 struct value
*value
= ((value_object
*) self
)->value
;
557 const char *encoding
= NULL
;
558 const char *errors
= NULL
;
559 const char *user_encoding
= NULL
;
560 const char *la_encoding
= NULL
;
561 struct type
*char_type
;
562 static const char *keywords
[] = { "encoding", "errors", "length", NULL
};
564 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "|ssi", keywords
,
565 &user_encoding
, &errors
, &length
))
570 LA_GET_STRING (value
, &buffer
, &length
, &char_type
, &la_encoding
);
572 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
574 GDB_PY_HANDLE_EXCEPTION (except
);
577 encoding
= (user_encoding
&& *user_encoding
) ? user_encoding
: la_encoding
;
578 return PyUnicode_Decode ((const char *) buffer
.get (),
579 length
* TYPE_LENGTH (char_type
),
583 /* Given a Python object, copy its truth value to a C int (the value
585 If src_obj is NULL, then *dest is not modified.
587 Return true in case of success (including src_obj being NULL), false
591 copy_py_bool_obj (int *dest
, PyObject
*src_obj
)
595 int cmp
= PyObject_IsTrue (src_obj
);
604 /* Implementation of gdb.Value.format_string (...) -> string.
605 Return Unicode string with value contents formatted using the
606 keyword-only arguments. */
609 valpy_format_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
611 static const char *keywords
[] =
613 /* Basic C/C++ options. */
614 "raw", /* See the /r option to print. */
615 "pretty_arrays", /* See set print array on|off. */
616 "pretty_structs", /* See set print pretty on|off. */
617 "array_indexes", /* See set print array-indexes on|off. */
618 "symbols", /* See set print symbol on|off. */
619 "unions", /* See set print union on|off. */
621 "deref_refs", /* No corresponding setting. */
622 "actual_objects", /* See set print object on|off. */
623 "static_members", /* See set print static-members on|off. */
624 /* C non-bool options. */
625 "max_elements", /* See set print elements N. */
626 "repeat_threshold", /* See set print repeats. */
627 "format", /* The format passed to the print command. */
631 /* This function has too many arguments to be useful as positionals, so
632 the user should specify them all as keyword arguments.
633 Python 3.3 and later have a way to specify it (both in C and Python
634 itself), but we could be compiled with older versions, so we just
635 check that the args tuple is empty. */
636 Py_ssize_t positional_count
= PyObject_Length (args
);
637 if (positional_count
< 0)
639 else if (positional_count
> 0)
641 /* This matches the error message that Python 3.3 raises when
642 passing positionals to functions expecting keyword-only
644 PyErr_Format (PyExc_TypeError
,
645 "format_string() takes 0 positional arguments but %zu were given",
650 struct value_print_options opts
;
651 get_user_print_options (&opts
);
654 /* We need objects for booleans as the "p" flag for bools is new in
656 PyObject
*raw_obj
= NULL
;
657 PyObject
*pretty_arrays_obj
= NULL
;
658 PyObject
*pretty_structs_obj
= NULL
;
659 PyObject
*array_indexes_obj
= NULL
;
660 PyObject
*symbols_obj
= NULL
;
661 PyObject
*unions_obj
= NULL
;
662 PyObject
*deref_refs_obj
= NULL
;
663 PyObject
*actual_objects_obj
= NULL
;
664 PyObject
*static_members_obj
= NULL
;
666 if (!gdb_PyArg_ParseTupleAndKeywords (args
,
668 "|O!O!O!O!O!O!O!O!O!IIs",
670 &PyBool_Type
, &raw_obj
,
671 &PyBool_Type
, &pretty_arrays_obj
,
672 &PyBool_Type
, &pretty_structs_obj
,
673 &PyBool_Type
, &array_indexes_obj
,
674 &PyBool_Type
, &symbols_obj
,
675 &PyBool_Type
, &unions_obj
,
676 &PyBool_Type
, &deref_refs_obj
,
677 &PyBool_Type
, &actual_objects_obj
,
678 &PyBool_Type
, &static_members_obj
,
680 &opts
.repeat_count_threshold
,
684 /* Set boolean arguments. */
685 if (!copy_py_bool_obj (&opts
.raw
, raw_obj
))
687 if (!copy_py_bool_obj (&opts
.prettyformat_arrays
, pretty_arrays_obj
))
689 if (!copy_py_bool_obj (&opts
.prettyformat_structs
, pretty_structs_obj
))
691 if (!copy_py_bool_obj (&opts
.print_array_indexes
, array_indexes_obj
))
693 if (!copy_py_bool_obj (&opts
.symbol_print
, symbols_obj
))
695 if (!copy_py_bool_obj (&opts
.unionprint
, unions_obj
))
697 if (!copy_py_bool_obj (&opts
.deref_ref
, deref_refs_obj
))
699 if (!copy_py_bool_obj (&opts
.objectprint
, actual_objects_obj
))
701 if (!copy_py_bool_obj (&opts
.static_field_print
, static_members_obj
))
704 /* Numeric arguments for which 0 means unlimited (which we represent as
706 if (opts
.print_max
== 0)
707 opts
.print_max
= UINT_MAX
;
708 if (opts
.repeat_count_threshold
== 0)
709 opts
.repeat_count_threshold
= UINT_MAX
;
711 /* Other arguments. */
714 if (strlen (format
) == 1)
715 opts
.format
= format
[0];
718 /* Mimic the message on standard Python ones for similar
720 PyErr_SetString (PyExc_ValueError
,
721 "a single character is required");
730 common_val_print (((value_object
*) self
)->value
, &stb
, 0,
731 &opts
, python_language
);
733 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
735 GDB_PY_HANDLE_EXCEPTION (except
);
738 return PyUnicode_Decode (stb
.c_str (), stb
.size (), host_charset (), NULL
);
741 /* A helper function that implements the various cast operators. */
744 valpy_do_cast (PyObject
*self
, PyObject
*args
, enum exp_opcode op
)
746 PyObject
*type_obj
, *result
= NULL
;
749 if (! PyArg_ParseTuple (args
, "O", &type_obj
))
752 type
= type_object_to_type (type_obj
);
755 PyErr_SetString (PyExc_RuntimeError
,
756 _("Argument must be a type."));
762 struct value
*val
= ((value_object
*) self
)->value
;
763 struct value
*res_val
;
764 scoped_value_mark free_values
;
766 if (op
== UNOP_DYNAMIC_CAST
)
767 res_val
= value_dynamic_cast (type
, val
);
768 else if (op
== UNOP_REINTERPRET_CAST
)
769 res_val
= value_reinterpret_cast (type
, val
);
772 gdb_assert (op
== UNOP_CAST
);
773 res_val
= value_cast (type
, val
);
776 result
= value_to_value_object (res_val
);
778 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
780 GDB_PY_HANDLE_EXCEPTION (except
);
786 /* Implementation of the "cast" method. */
789 valpy_cast (PyObject
*self
, PyObject
*args
)
791 return valpy_do_cast (self
, args
, UNOP_CAST
);
794 /* Implementation of the "dynamic_cast" method. */
797 valpy_dynamic_cast (PyObject
*self
, PyObject
*args
)
799 return valpy_do_cast (self
, args
, UNOP_DYNAMIC_CAST
);
802 /* Implementation of the "reinterpret_cast" method. */
805 valpy_reinterpret_cast (PyObject
*self
, PyObject
*args
)
807 return valpy_do_cast (self
, args
, UNOP_REINTERPRET_CAST
);
811 valpy_length (PyObject
*self
)
813 /* We don't support getting the number of elements in a struct / class. */
814 PyErr_SetString (PyExc_NotImplementedError
,
815 _("Invalid operation on gdb.Value."));
819 /* Return 1 if the gdb.Field object FIELD is present in the value V.
820 Returns 0 otherwise. If any Python error occurs, -1 is returned. */
823 value_has_field (struct value
*v
, PyObject
*field
)
825 struct type
*parent_type
, *val_type
;
826 enum type_code type_code
;
827 gdbpy_ref
<> type_object (PyObject_GetAttrString (field
, "parent_type"));
830 if (type_object
== NULL
)
833 parent_type
= type_object_to_type (type_object
.get ());
834 if (parent_type
== NULL
)
836 PyErr_SetString (PyExc_TypeError
,
837 _("'parent_type' attribute of gdb.Field object is not a"
838 "gdb.Type object."));
844 val_type
= value_type (v
);
845 val_type
= check_typedef (val_type
);
846 if (TYPE_IS_REFERENCE (val_type
) || TYPE_CODE (val_type
) == TYPE_CODE_PTR
)
847 val_type
= check_typedef (TYPE_TARGET_TYPE (val_type
));
849 type_code
= TYPE_CODE (val_type
);
850 if ((type_code
== TYPE_CODE_STRUCT
|| type_code
== TYPE_CODE_UNION
)
851 && types_equal (val_type
, parent_type
))
856 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
858 GDB_PY_SET_HANDLE_EXCEPTION (except
);
864 /* Return the value of a flag FLAG_NAME in a gdb.Field object FIELD.
865 Returns 1 if the flag value is true, 0 if it is false, and -1 if
866 a Python error occurs. */
869 get_field_flag (PyObject
*field
, const char *flag_name
)
871 gdbpy_ref
<> flag_object (PyObject_GetAttrString (field
, flag_name
));
873 if (flag_object
== NULL
)
876 return PyObject_IsTrue (flag_object
.get ());
879 /* Return the "type" attribute of a gdb.Field object.
880 Returns NULL on error, with a Python exception set. */
883 get_field_type (PyObject
*field
)
885 gdbpy_ref
<> ftype_obj (PyObject_GetAttrString (field
, "type"));
888 if (ftype_obj
== NULL
)
890 ftype
= type_object_to_type (ftype_obj
.get ());
892 PyErr_SetString (PyExc_TypeError
,
893 _("'type' attribute of gdb.Field object is not a "
894 "gdb.Type object."));
899 /* Given string name or a gdb.Field object corresponding to an element inside
900 a structure, return its value object. Returns NULL on error, with a python
904 valpy_getitem (PyObject
*self
, PyObject
*key
)
906 struct gdb_exception except
= exception_none
;
907 value_object
*self_value
= (value_object
*) self
;
908 gdb::unique_xmalloc_ptr
<char> field
;
909 struct type
*base_class_type
= NULL
, *field_type
= NULL
;
911 PyObject
*result
= NULL
;
913 if (gdbpy_is_string (key
))
915 field
= python_string_to_host_string (key
);
919 else if (gdbpy_is_field (key
))
921 int is_base_class
, valid_field
;
923 valid_field
= value_has_field (self_value
->value
, key
);
926 else if (valid_field
== 0)
928 PyErr_SetString (PyExc_TypeError
,
929 _("Invalid lookup for a field not contained in "
935 is_base_class
= get_field_flag (key
, "is_base_class");
936 if (is_base_class
< 0)
938 else if (is_base_class
> 0)
940 base_class_type
= get_field_type (key
);
941 if (base_class_type
== NULL
)
946 gdbpy_ref
<> name_obj (PyObject_GetAttrString (key
, "name"));
948 if (name_obj
== NULL
)
951 if (name_obj
!= Py_None
)
953 field
= python_string_to_host_string (name_obj
.get ());
959 if (!PyObject_HasAttrString (key
, "bitpos"))
961 PyErr_SetString (PyExc_AttributeError
,
962 _("gdb.Field object has no name and no "
963 "'bitpos' attribute."));
967 gdbpy_ref
<> bitpos_obj (PyObject_GetAttrString (key
, "bitpos"));
968 if (bitpos_obj
== NULL
)
970 if (!gdb_py_int_as_long (bitpos_obj
.get (), &bitpos
))
973 field_type
= get_field_type (key
);
974 if (field_type
== NULL
)
982 struct value
*tmp
= self_value
->value
;
983 struct value
*res_val
= NULL
;
984 scoped_value_mark free_values
;
987 res_val
= value_struct_elt (&tmp
, NULL
, field
.get (), NULL
,
988 "struct/class/union");
989 else if (bitpos
>= 0)
990 res_val
= value_struct_elt_bitpos (&tmp
, bitpos
, field_type
,
991 "struct/class/union");
992 else if (base_class_type
!= NULL
)
994 struct type
*val_type
;
996 val_type
= check_typedef (value_type (tmp
));
997 if (TYPE_CODE (val_type
) == TYPE_CODE_PTR
)
998 res_val
= value_cast (lookup_pointer_type (base_class_type
), tmp
);
999 else if (TYPE_CODE (val_type
) == TYPE_CODE_REF
)
1000 res_val
= value_cast (lookup_lvalue_reference_type (base_class_type
),
1002 else if (TYPE_CODE (val_type
) == TYPE_CODE_RVALUE_REF
)
1003 res_val
= value_cast (lookup_rvalue_reference_type (base_class_type
),
1006 res_val
= value_cast (base_class_type
, tmp
);
1010 /* Assume we are attempting an array access, and let the
1011 value code throw an exception if the index has an invalid
1013 struct value
*idx
= convert_value_from_python (key
);
1017 /* Check the value's type is something that can be accessed via
1021 tmp
= coerce_ref (tmp
);
1022 type
= check_typedef (value_type (tmp
));
1023 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
1024 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
1025 error (_("Cannot subscript requested type."));
1027 res_val
= value_subscript (tmp
, value_as_long (idx
));
1032 result
= value_to_value_object (res_val
);
1034 catch (const gdb_exception_RETURN_MASK_ALL
&ex
)
1039 GDB_PY_HANDLE_EXCEPTION (except
);
1045 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
1047 PyErr_Format (PyExc_NotImplementedError
,
1048 _("Setting of struct elements is not currently supported."));
1052 /* Called by the Python interpreter to perform an inferior function
1053 call on the value. Returns NULL on error, with a python exception set. */
1055 valpy_call (PyObject
*self
, PyObject
*args
, PyObject
*keywords
)
1057 Py_ssize_t args_count
;
1058 struct value
*function
= ((value_object
*) self
)->value
;
1059 struct value
**vargs
= NULL
;
1060 struct type
*ftype
= NULL
;
1061 PyObject
*result
= NULL
;
1065 ftype
= check_typedef (value_type (function
));
1067 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
1069 GDB_PY_HANDLE_EXCEPTION (except
);
1072 if (TYPE_CODE (ftype
) != TYPE_CODE_FUNC
)
1074 PyErr_SetString (PyExc_RuntimeError
,
1075 _("Value is not callable (not TYPE_CODE_FUNC)."));
1079 if (! PyTuple_Check (args
))
1081 PyErr_SetString (PyExc_TypeError
,
1082 _("Inferior arguments must be provided in a tuple."));
1086 args_count
= PyTuple_Size (args
);
1091 vargs
= XALLOCAVEC (struct value
*, args_count
);
1092 for (i
= 0; i
< args_count
; i
++)
1094 PyObject
*item
= PyTuple_GetItem (args
, i
);
1099 vargs
[i
] = convert_value_from_python (item
);
1100 if (vargs
[i
] == NULL
)
1107 scoped_value_mark free_values
;
1110 = call_function_by_hand (function
, NULL
,
1111 gdb::make_array_view (vargs
, args_count
));
1112 result
= value_to_value_object (return_value
);
1114 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
1116 GDB_PY_HANDLE_EXCEPTION (except
);
1122 /* Called by the Python interpreter to obtain string representation
1125 valpy_str (PyObject
*self
)
1127 struct value_print_options opts
;
1129 get_user_print_options (&opts
);
1136 common_val_print (((value_object
*) self
)->value
, &stb
, 0,
1137 &opts
, python_language
);
1139 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
1141 GDB_PY_HANDLE_EXCEPTION (except
);
1144 return PyUnicode_Decode (stb
.c_str (), stb
.size (), host_charset (), NULL
);
1147 /* Implements gdb.Value.is_optimized_out. */
1149 valpy_get_is_optimized_out (PyObject
*self
, void *closure
)
1151 struct value
*value
= ((value_object
*) self
)->value
;
1156 opt
= value_optimized_out (value
);
1158 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
1160 GDB_PY_HANDLE_EXCEPTION (except
);
1169 /* Implements gdb.Value.is_lazy. */
1171 valpy_get_is_lazy (PyObject
*self
, void *closure
)
1173 struct value
*value
= ((value_object
*) self
)->value
;
1178 opt
= value_lazy (value
);
1180 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
1182 GDB_PY_HANDLE_EXCEPTION (except
);
1191 /* Implements gdb.Value.fetch_lazy (). */
1193 valpy_fetch_lazy (PyObject
*self
, PyObject
*args
)
1195 struct value
*value
= ((value_object
*) self
)->value
;
1199 if (value_lazy (value
))
1200 value_fetch_lazy (value
);
1202 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
1204 GDB_PY_HANDLE_EXCEPTION (except
);
1210 /* Calculate and return the address of the PyObject as the value of
1211 the builtin __hash__ call. */
1213 valpy_hash (PyObject
*self
)
1215 return (intptr_t) self
;
1233 /* If TYPE is a reference, return the target; otherwise return TYPE. */
1234 #define STRIP_REFERENCE(TYPE) \
1235 (TYPE_IS_REFERENCE (TYPE) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
1237 /* Helper for valpy_binop. Returns a value object which is the result
1238 of applying the operation specified by OPCODE to the given
1239 arguments. Throws a GDB exception on error. */
1242 valpy_binop_throw (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
1244 PyObject
*result
= NULL
;
1246 struct value
*arg1
, *arg2
;
1247 struct value
*res_val
= NULL
;
1248 enum exp_opcode op
= OP_NULL
;
1251 scoped_value_mark free_values
;
1253 /* If the gdb.Value object is the second operand, then it will be
1254 passed to us as the OTHER argument, and SELF will be an entirely
1255 different kind of object, altogether. Because of this, we can't
1256 assume self is a gdb.Value object and need to convert it from
1258 arg1
= convert_value_from_python (self
);
1262 arg2
= convert_value_from_python (other
);
1270 struct type
*ltype
= value_type (arg1
);
1271 struct type
*rtype
= value_type (arg2
);
1273 ltype
= check_typedef (ltype
);
1274 ltype
= STRIP_REFERENCE (ltype
);
1275 rtype
= check_typedef (rtype
);
1276 rtype
= STRIP_REFERENCE (rtype
);
1279 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
1280 && is_integral_type (rtype
))
1281 res_val
= value_ptradd (arg1
, value_as_long (arg2
));
1282 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
1283 && is_integral_type (ltype
))
1284 res_val
= value_ptradd (arg2
, value_as_long (arg1
));
1294 struct type
*ltype
= value_type (arg1
);
1295 struct type
*rtype
= value_type (arg2
);
1297 ltype
= check_typedef (ltype
);
1298 ltype
= STRIP_REFERENCE (ltype
);
1299 rtype
= check_typedef (rtype
);
1300 rtype
= STRIP_REFERENCE (rtype
);
1303 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
1304 && TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
1305 /* A ptrdiff_t for the target would be preferable here. */
1306 res_val
= value_from_longest (builtin_type_pyint
,
1307 value_ptrdiff (arg1
, arg2
));
1308 else if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
1309 && is_integral_type (rtype
))
1310 res_val
= value_ptradd (arg1
, - value_as_long (arg2
));
1337 op
= BINOP_BITWISE_AND
;
1340 op
= BINOP_BITWISE_IOR
;
1343 op
= BINOP_BITWISE_XOR
;
1349 if (binop_user_defined_p (op
, arg1
, arg2
))
1350 res_val
= value_x_binop (arg1
, arg2
, op
, OP_NULL
, EVAL_NORMAL
);
1352 res_val
= value_binop (arg1
, arg2
, op
);
1356 result
= value_to_value_object (res_val
);
1361 /* Returns a value object which is the result of applying the operation
1362 specified by OPCODE to the given arguments. Returns NULL on error, with
1363 a python exception set. */
1365 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
1367 PyObject
*result
= NULL
;
1371 result
= valpy_binop_throw (opcode
, self
, other
);
1373 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
1375 GDB_PY_HANDLE_EXCEPTION (except
);
1382 valpy_add (PyObject
*self
, PyObject
*other
)
1384 return valpy_binop (VALPY_ADD
, self
, other
);
1388 valpy_subtract (PyObject
*self
, PyObject
*other
)
1390 return valpy_binop (VALPY_SUB
, self
, other
);
1394 valpy_multiply (PyObject
*self
, PyObject
*other
)
1396 return valpy_binop (VALPY_MUL
, self
, other
);
1400 valpy_divide (PyObject
*self
, PyObject
*other
)
1402 return valpy_binop (VALPY_DIV
, self
, other
);
1406 valpy_remainder (PyObject
*self
, PyObject
*other
)
1408 return valpy_binop (VALPY_REM
, self
, other
);
1412 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
1414 /* We don't support the ternary form of pow. I don't know how to express
1415 that, so let's just throw NotImplementedError to at least do something
1417 if (unused
!= Py_None
)
1419 PyErr_SetString (PyExc_NotImplementedError
,
1420 "Invalid operation on gdb.Value.");
1424 return valpy_binop (VALPY_POW
, self
, other
);
1428 valpy_negative (PyObject
*self
)
1430 PyObject
*result
= NULL
;
1434 /* Perhaps overkill, but consistency has some virtue. */
1435 scoped_value_mark free_values
;
1438 val
= value_neg (((value_object
*) self
)->value
);
1439 result
= value_to_value_object (val
);
1441 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
1443 GDB_PY_HANDLE_EXCEPTION (except
);
1450 valpy_positive (PyObject
*self
)
1452 return value_to_value_object (((value_object
*) self
)->value
);
1456 valpy_absolute (PyObject
*self
)
1458 struct value
*value
= ((value_object
*) self
)->value
;
1463 scoped_value_mark free_values
;
1465 if (value_less (value
, value_zero (value_type (value
), not_lval
)))
1468 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
1470 GDB_PY_HANDLE_EXCEPTION (except
);
1474 return valpy_positive (self
);
1476 return valpy_negative (self
);
1479 /* Implements boolean evaluation of gdb.Value. */
1481 valpy_nonzero (PyObject
*self
)
1483 struct gdb_exception except
= exception_none
;
1484 value_object
*self_value
= (value_object
*) self
;
1486 int nonzero
= 0; /* Appease GCC warning. */
1490 type
= check_typedef (value_type (self_value
->value
));
1492 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
1493 nonzero
= !!value_as_long (self_value
->value
);
1494 else if (is_floating_value (self_value
->value
))
1495 nonzero
= !target_float_is_zero (value_contents (self_value
->value
),
1498 /* All other values are True. */
1501 catch (const gdb_exception_RETURN_MASK_ALL
&ex
)
1506 /* This is not documented in the Python documentation, but if this
1507 function fails, return -1 as slot_nb_nonzero does (the default
1508 Python nonzero function). */
1509 GDB_PY_SET_HANDLE_EXCEPTION (except
);
1514 /* Implements ~ for value objects. */
1516 valpy_invert (PyObject
*self
)
1518 struct value
*val
= NULL
;
1522 val
= value_complement (((value_object
*) self
)->value
);
1524 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
1526 GDB_PY_HANDLE_EXCEPTION (except
);
1529 return value_to_value_object (val
);
1532 /* Implements left shift for value objects. */
1534 valpy_lsh (PyObject
*self
, PyObject
*other
)
1536 return valpy_binop (VALPY_LSH
, self
, other
);
1539 /* Implements right shift for value objects. */
1541 valpy_rsh (PyObject
*self
, PyObject
*other
)
1543 return valpy_binop (VALPY_RSH
, self
, other
);
1546 /* Implements bitwise and for value objects. */
1548 valpy_and (PyObject
*self
, PyObject
*other
)
1550 return valpy_binop (VALPY_BITAND
, self
, other
);
1553 /* Implements bitwise or for value objects. */
1555 valpy_or (PyObject
*self
, PyObject
*other
)
1557 return valpy_binop (VALPY_BITOR
, self
, other
);
1560 /* Implements bitwise xor for value objects. */
1562 valpy_xor (PyObject
*self
, PyObject
*other
)
1564 return valpy_binop (VALPY_BITXOR
, self
, other
);
1567 /* Helper for valpy_richcompare. Implements comparison operations for
1568 value objects. Returns true/false on success. Returns -1 with a
1569 Python exception set if a Python error is detected. Throws a GDB
1570 exception on other errors (memory error, etc.). */
1573 valpy_richcompare_throw (PyObject
*self
, PyObject
*other
, int op
)
1576 struct value
*value_other
;
1577 struct value
*value_self
;
1579 scoped_value_mark free_values
;
1581 value_other
= convert_value_from_python (other
);
1582 if (value_other
== NULL
)
1585 value_self
= ((value_object
*) self
)->value
;
1590 result
= value_less (value_self
, value_other
);
1593 result
= value_less (value_self
, value_other
)
1594 || value_equal (value_self
, value_other
);
1597 result
= value_equal (value_self
, value_other
);
1600 result
= !value_equal (value_self
, value_other
);
1603 result
= value_less (value_other
, value_self
);
1606 result
= (value_less (value_other
, value_self
)
1607 || value_equal (value_self
, value_other
));
1611 PyErr_SetString (PyExc_NotImplementedError
,
1612 _("Invalid operation on gdb.Value."));
1621 /* Implements comparison operations for value objects. Returns NULL on error,
1622 with a python exception set. */
1624 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
1628 if (other
== Py_None
)
1629 /* Comparing with None is special. From what I can tell, in Python
1630 None is smaller than anything else. */
1642 PyErr_SetString (PyExc_NotImplementedError
,
1643 _("Invalid operation on gdb.Value."));
1649 result
= valpy_richcompare_throw (self
, other
, op
);
1651 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
1653 GDB_PY_HANDLE_EXCEPTION (except
);
1656 /* In this case, the Python exception has already been set. */
1667 /* Implements conversion to int. */
1669 valpy_int (PyObject
*self
)
1671 struct value
*value
= ((value_object
*) self
)->value
;
1672 struct type
*type
= value_type (value
);
1677 if (is_floating_value (value
))
1679 type
= builtin_type_pylong
;
1680 value
= value_cast (type
, value
);
1683 if (!is_integral_type (type
)
1684 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
1685 error (_("Cannot convert value to int."));
1687 l
= value_as_long (value
);
1689 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
1691 GDB_PY_HANDLE_EXCEPTION (except
);
1694 if (TYPE_UNSIGNED (type
))
1695 return gdb_py_object_from_ulongest (l
).release ();
1697 return gdb_py_object_from_longest (l
).release ();
1701 /* Implements conversion to long. */
1703 valpy_long (PyObject
*self
)
1705 struct value
*value
= ((value_object
*) self
)->value
;
1706 struct type
*type
= value_type (value
);
1711 if (is_floating_value (value
))
1713 type
= builtin_type_pylong
;
1714 value
= value_cast (type
, value
);
1717 type
= check_typedef (type
);
1719 if (!is_integral_type (type
)
1720 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
1721 error (_("Cannot convert value to long."));
1723 l
= value_as_long (value
);
1725 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
1727 GDB_PY_HANDLE_EXCEPTION (except
);
1730 if (TYPE_UNSIGNED (type
))
1731 return gdb_py_long_from_ulongest (l
);
1733 return gdb_py_long_from_longest (l
);
1736 /* Implements conversion to float. */
1738 valpy_float (PyObject
*self
)
1740 struct value
*value
= ((value_object
*) self
)->value
;
1741 struct type
*type
= value_type (value
);
1746 type
= check_typedef (type
);
1748 if (TYPE_CODE (type
) == TYPE_CODE_FLT
&& is_floating_value (value
))
1749 d
= target_float_to_host_double (value_contents (value
), type
);
1750 else if (TYPE_CODE (type
) == TYPE_CODE_INT
)
1752 /* Note that valpy_long accepts TYPE_CODE_PTR and some
1753 others here here -- but casting a pointer or bool to a
1754 float seems wrong. */
1755 d
= value_as_long (value
);
1758 error (_("Cannot convert value to float."));
1760 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
1762 GDB_PY_HANDLE_EXCEPTION (except
);
1765 return PyFloat_FromDouble (d
);
1768 /* Returns an object for a value which is released from the all_values chain,
1769 so its lifetime is not bound to the execution of a command. */
1771 value_to_value_object (struct value
*val
)
1773 value_object
*val_obj
;
1775 val_obj
= PyObject_New (value_object
, &value_object_type
);
1776 if (val_obj
!= NULL
)
1778 val_obj
->value
= release_value (val
).release ();
1779 val_obj
->address
= NULL
;
1780 val_obj
->type
= NULL
;
1781 val_obj
->dynamic_type
= NULL
;
1782 note_value (val_obj
);
1785 return (PyObject
*) val_obj
;
1788 /* Returns a borrowed reference to the struct value corresponding to
1789 the given value object. */
1791 value_object_to_value (PyObject
*self
)
1795 if (! PyObject_TypeCheck (self
, &value_object_type
))
1797 real
= (value_object
*) self
;
1801 /* Try to convert a Python value to a gdb value. If the value cannot
1802 be converted, set a Python exception and return NULL. Returns a
1803 reference to a new value on the all_values chain. */
1806 convert_value_from_python (PyObject
*obj
)
1808 struct value
*value
= NULL
; /* -Wall */
1811 gdb_assert (obj
!= NULL
);
1815 if (PyBool_Check (obj
))
1817 cmp
= PyObject_IsTrue (obj
);
1819 value
= value_from_longest (builtin_type_pybool
, cmp
);
1821 /* Make a long logic check first. In Python 3.x, internally,
1822 all integers are represented as longs. In Python 2.x, there
1823 is still a differentiation internally between a PyInt and a
1824 PyLong. Explicitly do this long check conversion first. In
1825 GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
1826 to be done first to ensure we do not lose information in the
1827 conversion process. */
1828 else if (PyLong_Check (obj
))
1830 LONGEST l
= PyLong_AsLongLong (obj
);
1832 if (PyErr_Occurred ())
1834 /* If the error was an overflow, we can try converting to
1835 ULONGEST instead. */
1836 if (PyErr_ExceptionMatches (PyExc_OverflowError
))
1838 gdbpy_err_fetch fetched_error
;
1839 gdbpy_ref
<> zero (PyInt_FromLong (0));
1841 /* Check whether obj is positive. */
1842 if (PyObject_RichCompareBool (obj
, zero
.get (), Py_GT
) > 0)
1846 ul
= PyLong_AsUnsignedLongLong (obj
);
1847 if (! PyErr_Occurred ())
1848 value
= value_from_ulongest (builtin_type_upylong
, ul
);
1852 /* There's nothing we can do. */
1853 fetched_error
.restore ();
1858 value
= value_from_longest (builtin_type_pylong
, l
);
1860 #if PY_MAJOR_VERSION == 2
1861 else if (PyInt_Check (obj
))
1863 long l
= PyInt_AsLong (obj
);
1865 if (! PyErr_Occurred ())
1866 value
= value_from_longest (builtin_type_pyint
, l
);
1869 else if (PyFloat_Check (obj
))
1871 double d
= PyFloat_AsDouble (obj
);
1873 if (! PyErr_Occurred ())
1874 value
= value_from_host_double (builtin_type_pyfloat
, d
);
1876 else if (gdbpy_is_string (obj
))
1878 gdb::unique_xmalloc_ptr
<char> s
1879 = python_string_to_target_string (obj
);
1881 value
= value_cstring (s
.get (), strlen (s
.get ()),
1882 builtin_type_pychar
);
1884 else if (PyObject_TypeCheck (obj
, &value_object_type
))
1885 value
= value_copy (((value_object
*) obj
)->value
);
1886 else if (gdbpy_is_lazy_string (obj
))
1890 result
= PyObject_CallMethodObjArgs (obj
, gdbpy_value_cst
, NULL
);
1891 value
= value_copy (((value_object
*) result
)->value
);
1895 PyErr_Format (PyExc_TypeError
,
1896 _("Could not convert Python object: %S."), obj
);
1898 PyErr_Format (PyExc_TypeError
,
1899 _("Could not convert Python object: %s."),
1900 PyString_AsString (PyObject_Str (obj
)));
1903 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
1905 gdbpy_convert_exception (except
);
1912 /* Returns value object in the ARGth position in GDB's history. */
1914 gdbpy_history (PyObject
*self
, PyObject
*args
)
1917 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
1919 if (!PyArg_ParseTuple (args
, "i", &i
))
1924 res_val
= access_value_history (i
);
1926 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
1928 GDB_PY_HANDLE_EXCEPTION (except
);
1931 return value_to_value_object (res_val
);
1934 /* Return the value of a convenience variable. */
1936 gdbpy_convenience_variable (PyObject
*self
, PyObject
*args
)
1938 const char *varname
;
1939 struct value
*res_val
= NULL
;
1941 if (!PyArg_ParseTuple (args
, "s", &varname
))
1946 struct internalvar
*var
= lookup_only_internalvar (varname
);
1950 res_val
= value_of_internalvar (python_gdbarch
, var
);
1951 if (TYPE_CODE (value_type (res_val
)) == TYPE_CODE_VOID
)
1955 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
1957 GDB_PY_HANDLE_EXCEPTION (except
);
1960 if (res_val
== NULL
)
1963 return value_to_value_object (res_val
);
1966 /* Set the value of a convenience variable. */
1968 gdbpy_set_convenience_variable (PyObject
*self
, PyObject
*args
)
1970 const char *varname
;
1971 PyObject
*value_obj
;
1972 struct value
*value
= NULL
;
1974 if (!PyArg_ParseTuple (args
, "sO", &varname
, &value_obj
))
1977 /* None means to clear the variable. */
1978 if (value_obj
!= Py_None
)
1980 value
= convert_value_from_python (value_obj
);
1989 struct internalvar
*var
= lookup_only_internalvar (varname
);
1992 clear_internalvar (var
);
1996 struct internalvar
*var
= lookup_internalvar (varname
);
1998 set_internalvar (var
, value
);
2001 catch (const gdb_exception_RETURN_MASK_ALL
&except
)
2003 GDB_PY_HANDLE_EXCEPTION (except
);
2009 /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
2012 gdbpy_is_value_object (PyObject
*obj
)
2014 return PyObject_TypeCheck (obj
, &value_object_type
);
2018 gdbpy_initialize_values (void)
2020 if (PyType_Ready (&value_object_type
) < 0)
2023 return gdb_pymodule_addobject (gdb_module
, "Value",
2024 (PyObject
*) &value_object_type
);
2029 static gdb_PyGetSetDef value_object_getset
[] = {
2030 { "address", valpy_get_address
, NULL
, "The address of the value.",
2032 { "is_optimized_out", valpy_get_is_optimized_out
, NULL
,
2033 "Boolean telling whether the value is optimized "
2034 "out (i.e., not available).",
2036 { "type", valpy_get_type
, NULL
, "Type of the value.", NULL
},
2037 { "dynamic_type", valpy_get_dynamic_type
, NULL
,
2038 "Dynamic type of the value.", NULL
},
2039 { "is_lazy", valpy_get_is_lazy
, NULL
,
2040 "Boolean telling whether the value is lazy (not fetched yet\n\
2041 from the inferior). A lazy value is fetched when needed, or when\n\
2042 the \"fetch_lazy()\" method is called.", NULL
},
2043 {NULL
} /* Sentinel */
2046 static PyMethodDef value_object_methods
[] = {
2047 { "cast", valpy_cast
, METH_VARARGS
, "Cast the value to the supplied type." },
2048 { "dynamic_cast", valpy_dynamic_cast
, METH_VARARGS
,
2049 "dynamic_cast (gdb.Type) -> gdb.Value\n\
2050 Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
2052 { "reinterpret_cast", valpy_reinterpret_cast
, METH_VARARGS
,
2053 "reinterpret_cast (gdb.Type) -> gdb.Value\n\
2054 Cast the value to the supplied type, as if by the C++\n\
2055 reinterpret_cast operator."
2057 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
2058 { "referenced_value", valpy_referenced_value
, METH_NOARGS
,
2059 "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." },
2060 { "reference_value", valpy_lvalue_reference_value
, METH_NOARGS
,
2061 "Return a value of type TYPE_CODE_REF referencing this value." },
2062 { "rvalue_reference_value", valpy_rvalue_reference_value
, METH_NOARGS
,
2063 "Return a value of type TYPE_CODE_RVALUE_REF referencing this value." },
2064 { "const_value", valpy_const_value
, METH_NOARGS
,
2065 "Return a 'const' qualied version of the same value." },
2066 { "lazy_string", (PyCFunction
) valpy_lazy_string
,
2067 METH_VARARGS
| METH_KEYWORDS
,
2068 "lazy_string ([encoding] [, length]) -> lazy_string\n\
2069 Return a lazy string representation of the value." },
2070 { "string", (PyCFunction
) valpy_string
, METH_VARARGS
| METH_KEYWORDS
,
2071 "string ([encoding] [, errors] [, length]) -> string\n\
2072 Return Unicode string representation of the value." },
2073 { "fetch_lazy", valpy_fetch_lazy
, METH_NOARGS
,
2074 "Fetches the value from the inferior, if it was lazy." },
2075 { "format_string", (PyCFunction
) valpy_format_string
,
2076 METH_VARARGS
| METH_KEYWORDS
,
2077 "format_string (...) -> string\n\
2078 Return a string representation of the value using the specified\n\
2079 formatting options" },
2080 {NULL
} /* Sentinel */
2083 static PyNumberMethods value_object_as_number
= {
2091 NULL
, /* nb_divmod */
2092 valpy_power
, /* nb_power */
2093 valpy_negative
, /* nb_negative */
2094 valpy_positive
, /* nb_positive */
2095 valpy_absolute
, /* nb_absolute */
2096 valpy_nonzero
, /* nb_nonzero */
2097 valpy_invert
, /* nb_invert */
2098 valpy_lsh
, /* nb_lshift */
2099 valpy_rsh
, /* nb_rshift */
2100 valpy_and
, /* nb_and */
2101 valpy_xor
, /* nb_xor */
2102 valpy_or
, /* nb_or */
2104 valpy_long
, /* nb_int */
2105 NULL
, /* reserved */
2107 NULL
, /* nb_coerce */
2108 valpy_int
, /* nb_int */
2109 valpy_long
, /* nb_long */
2111 valpy_float
, /* nb_float */
2116 NULL
, /* nb_inplace_add */
2117 NULL
, /* nb_inplace_subtract */
2118 NULL
, /* nb_inplace_multiply */
2120 NULL
, /* nb_inplace_divide */
2122 NULL
, /* nb_inplace_remainder */
2123 NULL
, /* nb_inplace_power */
2124 NULL
, /* nb_inplace_lshift */
2125 NULL
, /* nb_inplace_rshift */
2126 NULL
, /* nb_inplace_and */
2127 NULL
, /* nb_inplace_xor */
2128 NULL
, /* nb_inplace_or */
2129 NULL
, /* nb_floor_divide */
2130 valpy_divide
, /* nb_true_divide */
2131 NULL
, /* nb_inplace_floor_divide */
2132 NULL
, /* nb_inplace_true_divide */
2133 valpy_long
, /* nb_index */
2136 static PyMappingMethods value_object_as_mapping
= {
2142 PyTypeObject value_object_type
= {
2143 PyVarObject_HEAD_INIT (NULL
, 0)
2144 "gdb.Value", /*tp_name*/
2145 sizeof (value_object
), /*tp_basicsize*/
2147 valpy_dealloc
, /*tp_dealloc*/
2153 &value_object_as_number
, /*tp_as_number*/
2154 0, /*tp_as_sequence*/
2155 &value_object_as_mapping
, /*tp_as_mapping*/
2156 valpy_hash
, /*tp_hash*/
2157 valpy_call
, /*tp_call*/
2158 valpy_str
, /*tp_str*/
2162 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
2163 | Py_TPFLAGS_BASETYPE
, /*tp_flags*/
2164 "GDB value object", /* tp_doc */
2165 0, /* tp_traverse */
2167 valpy_richcompare
, /* tp_richcompare */
2168 0, /* tp_weaklistoffset */
2170 0, /* tp_iternext */
2171 value_object_methods
, /* tp_methods */
2173 value_object_getset
, /* tp_getset */
2176 0, /* tp_descr_get */
2177 0, /* tp_descr_set */
2178 0, /* tp_dictoffset */
2181 valpy_new
/* tp_new */