1 /* Python interface to values.
3 Copyright (C) 2008, 2009, 2010 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"
34 #include "python-internal.h"
36 /* Even though Python scalar types directly map to host types, we use
37 target types here to remain consistent with the the values system in
38 GDB (which uses target arithmetic). */
40 /* Python's integer type corresponds to C's long type. */
41 #define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long
43 /* Python's float type corresponds to C's double type. */
44 #define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double
46 /* Python's long type corresponds to C's long long type. */
47 #define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long
49 /* Python's long type corresponds to C's long long type. Unsigned version. */
50 #define builtin_type_upylong builtin_type \
51 (python_gdbarch)->builtin_unsigned_long_long
53 #define builtin_type_pybool \
54 language_bool_type (python_language, python_gdbarch)
56 #define builtin_type_pychar \
57 language_string_char_type (python_language, python_gdbarch)
59 typedef struct value_object
{
61 struct value_object
*next
;
62 struct value_object
*prev
;
66 PyObject
*dynamic_type
;
69 /* List of all values which are currently exposed to Python. It is
70 maintained so that when an objfile is discarded, preserve_values
71 can copy the values' types if needed. */
72 /* This variable is unnecessarily initialized to NULL in order to
73 work around a linker bug on MacOS. */
74 static value_object
*values_in_python
= NULL
;
76 /* Called by the Python interpreter when deallocating a value object. */
78 valpy_dealloc (PyObject
*obj
)
80 value_object
*self
= (value_object
*) obj
;
82 /* Remove SELF from the global list. */
84 self
->prev
->next
= self
->next
;
87 gdb_assert (values_in_python
== self
);
88 values_in_python
= self
->next
;
91 self
->next
->prev
= self
->prev
;
93 value_free (self
->value
);
96 /* Use braces to appease gcc warning. *sigh* */
98 Py_DECREF (self
->address
);
103 Py_DECREF (self
->type
);
106 Py_XDECREF (self
->dynamic_type
);
108 self
->ob_type
->tp_free (self
);
111 /* Helper to push a Value object on the global list. */
113 note_value (value_object
*value_obj
)
115 value_obj
->next
= values_in_python
;
117 value_obj
->next
->prev
= value_obj
;
118 value_obj
->prev
= NULL
;
119 values_in_python
= value_obj
;
122 /* Called when a new gdb.Value object needs to be allocated. */
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 value_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 preserve_python_values (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 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
177 volatile struct gdb_exception except
;
179 TRY_CATCH (except
, RETURN_MASK_ALL
)
181 res_val
= value_ind (((value_object
*) self
)->value
);
183 GDB_PY_HANDLE_EXCEPTION (except
);
185 return value_to_value_object (res_val
);
188 /* Return "&value". */
190 valpy_get_address (PyObject
*self
, void *closure
)
192 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
193 value_object
*val_obj
= (value_object
*) self
;
194 volatile struct gdb_exception except
;
196 if (!val_obj
->address
)
198 TRY_CATCH (except
, RETURN_MASK_ALL
)
200 res_val
= value_addr (val_obj
->value
);
202 if (except
.reason
< 0)
204 val_obj
->address
= Py_None
;
208 val_obj
->address
= value_to_value_object (res_val
);
211 Py_INCREF (val_obj
->address
);
213 return val_obj
->address
;
216 /* Return type of the value. */
218 valpy_get_type (PyObject
*self
, void *closure
)
220 value_object
*obj
= (value_object
*) self
;
224 obj
->type
= type_to_type_object (value_type (obj
->value
));
228 Py_INCREF (obj
->type
);
232 /* Return dynamic type of the value. */
235 valpy_get_dynamic_type (PyObject
*self
, void *closure
)
237 value_object
*obj
= (value_object
*) self
;
238 volatile struct gdb_exception except
;
239 struct type
*type
= NULL
;
241 if (obj
->dynamic_type
!= NULL
)
243 Py_INCREF (obj
->dynamic_type
);
244 return obj
->dynamic_type
;
247 TRY_CATCH (except
, RETURN_MASK_ALL
)
249 struct value
*val
= obj
->value
;
251 type
= value_type (val
);
252 CHECK_TYPEDEF (type
);
254 if (((TYPE_CODE (type
) == TYPE_CODE_PTR
)
255 || (TYPE_CODE (type
) == TYPE_CODE_REF
))
256 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_CLASS
))
258 struct value
*target
;
259 int was_pointer
= TYPE_CODE (type
) == TYPE_CODE_PTR
;
261 target
= value_ind (val
);
262 type
= value_rtti_type (target
, NULL
, NULL
, NULL
);
267 type
= lookup_pointer_type (type
);
269 type
= lookup_reference_type (type
);
272 else if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
273 type
= value_rtti_type (val
, NULL
, NULL
, NULL
);
276 /* Re-use object's static type. */
280 GDB_PY_HANDLE_EXCEPTION (except
);
284 /* Ensure that the TYPE field is ready. */
285 if (!valpy_get_type (self
, NULL
))
287 /* We don't need to incref here, because valpy_get_type already
289 obj
->dynamic_type
= obj
->type
;
292 obj
->dynamic_type
= type_to_type_object (type
);
294 Py_INCREF (obj
->dynamic_type
);
295 return obj
->dynamic_type
;
298 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
299 string. Return a PyObject representing a lazy_string_object type.
300 A lazy string is a pointer to a string with an optional encoding and
301 length. If ENCODING is not given, encoding is set to None. If an
302 ENCODING is provided the encoding parameter is set to ENCODING, but
303 the string is not encoded. If LENGTH is provided then the length
304 parameter is set to LENGTH, otherwise length will be set to -1 (first
305 null of appropriate with). */
307 valpy_lazy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
310 struct value
*value
= ((value_object
*) self
)->value
;
311 const char *user_encoding
= NULL
;
312 static char *keywords
[] = { "encoding", "length", NULL
};
315 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|si", keywords
,
316 &user_encoding
, &length
))
319 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_PTR
)
320 value
= value_ind (value
);
322 str_obj
= gdbpy_create_lazy_string_object (value_address (value
), length
,
323 user_encoding
, value_type (value
));
325 return (PyObject
*) str_obj
;
328 /* Implementation of gdb.Value.string ([encoding] [, errors]
329 [, length]) -> string. Return Unicode string with value contents.
330 If ENCODING is not given, the string is assumed to be encoded in
331 the target's charset. If LENGTH is provided, only fetch string to
332 the length provided. */
335 valpy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
339 struct value
*value
= ((value_object
*) self
)->value
;
340 volatile struct gdb_exception except
;
342 const char *encoding
= NULL
;
343 const char *errors
= NULL
;
344 const char *user_encoding
= NULL
;
345 const char *la_encoding
= NULL
;
346 struct type
*char_type
;
347 static char *keywords
[] = { "encoding", "errors", "length", NULL
};
349 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|ssi", keywords
,
350 &user_encoding
, &errors
, &length
))
353 TRY_CATCH (except
, RETURN_MASK_ALL
)
355 LA_GET_STRING (value
, &buffer
, &length
, &char_type
, &la_encoding
);
357 GDB_PY_HANDLE_EXCEPTION (except
);
359 encoding
= (user_encoding
&& *user_encoding
) ? user_encoding
: la_encoding
;
360 unicode
= PyUnicode_Decode (buffer
, length
* TYPE_LENGTH (char_type
),
367 /* A helper function that implements the various cast operators. */
370 valpy_do_cast (PyObject
*self
, PyObject
*args
, enum exp_opcode op
)
374 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
375 volatile struct gdb_exception except
;
377 if (! PyArg_ParseTuple (args
, "O", &type_obj
))
380 type
= type_object_to_type (type_obj
);
383 PyErr_SetString (PyExc_RuntimeError
,
384 _("Argument must be a type."));
388 TRY_CATCH (except
, RETURN_MASK_ALL
)
390 struct value
*val
= ((value_object
*) self
)->value
;
392 if (op
== UNOP_DYNAMIC_CAST
)
393 res_val
= value_dynamic_cast (type
, val
);
394 else if (op
== UNOP_REINTERPRET_CAST
)
395 res_val
= value_reinterpret_cast (type
, val
);
398 gdb_assert (op
== UNOP_CAST
);
399 res_val
= value_cast (type
, val
);
402 GDB_PY_HANDLE_EXCEPTION (except
);
404 return value_to_value_object (res_val
);
407 /* Implementation of the "cast" method. */
410 valpy_cast (PyObject
*self
, PyObject
*args
)
412 return valpy_do_cast (self
, args
, UNOP_CAST
);
415 /* Implementation of the "dynamic_cast" method. */
418 valpy_dynamic_cast (PyObject
*self
, PyObject
*args
)
420 return valpy_do_cast (self
, args
, UNOP_DYNAMIC_CAST
);
423 /* Implementation of the "reinterpret_cast" method. */
426 valpy_reinterpret_cast (PyObject
*self
, PyObject
*args
)
428 return valpy_do_cast (self
, args
, UNOP_REINTERPRET_CAST
);
432 valpy_length (PyObject
*self
)
434 /* We don't support getting the number of elements in a struct / class. */
435 PyErr_SetString (PyExc_NotImplementedError
,
436 _("Invalid operation on gdb.Value."));
440 /* Given string name of an element inside structure, return its value
443 valpy_getitem (PyObject
*self
, PyObject
*key
)
445 value_object
*self_value
= (value_object
*) self
;
447 struct value
*res_val
= NULL
;
448 volatile struct gdb_exception except
;
450 if (gdbpy_is_string (key
))
452 field
= python_string_to_host_string (key
);
457 TRY_CATCH (except
, RETURN_MASK_ALL
)
459 struct value
*tmp
= self_value
->value
;
462 res_val
= value_struct_elt (&tmp
, NULL
, field
, 0, NULL
);
465 /* Assume we are attempting an array access, and let the
466 value code throw an exception if the index has an invalid
468 struct value
*idx
= convert_value_from_python (key
);
472 /* Check the value's type is something that can be accessed via
476 tmp
= coerce_ref (tmp
);
477 type
= check_typedef (value_type (tmp
));
478 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
479 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
480 error( _("Cannot subscript requested type."));
482 res_val
= value_subscript (tmp
, value_as_long (idx
));
488 GDB_PY_HANDLE_EXCEPTION (except
);
490 return res_val
? value_to_value_object (res_val
) : NULL
;
494 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
496 PyErr_Format (PyExc_NotImplementedError
,
497 _("Setting of struct elements is not currently supported."));
501 /* Called by the Python interpreter to perform an inferior function
502 call on the value. */
504 valpy_call (PyObject
*self
, PyObject
*args
, PyObject
*keywords
)
506 struct value
*return_value
= NULL
;
507 Py_ssize_t args_count
;
508 volatile struct gdb_exception except
;
509 struct value
*function
= ((value_object
*) self
)->value
;
510 struct value
**vargs
= NULL
;
511 struct type
*ftype
= check_typedef (value_type (function
));
513 if (TYPE_CODE (ftype
) != TYPE_CODE_FUNC
)
515 PyErr_SetString (PyExc_RuntimeError
,
516 _("Value is not callable (not TYPE_CODE_FUNC)."));
520 args_count
= PyTuple_Size (args
);
525 vargs
= alloca (sizeof (struct value
*) * args_count
);
526 for (i
= 0; i
< args_count
; i
++)
528 PyObject
*item
= PyTuple_GetItem (args
, i
);
533 vargs
[i
] = convert_value_from_python (item
);
534 if (vargs
[i
] == NULL
)
539 TRY_CATCH (except
, RETURN_MASK_ALL
)
541 return_value
= call_function_by_hand (function
, args_count
, vargs
);
543 GDB_PY_HANDLE_EXCEPTION (except
);
545 return value_to_value_object (return_value
);
548 /* Called by the Python interpreter to obtain string representation
551 valpy_str (PyObject
*self
)
555 struct cleanup
*old_chain
;
557 struct value_print_options opts
;
558 volatile struct gdb_exception except
;
560 get_user_print_options (&opts
);
563 stb
= mem_fileopen ();
564 old_chain
= make_cleanup_ui_file_delete (stb
);
566 TRY_CATCH (except
, RETURN_MASK_ALL
)
568 common_val_print (((value_object
*) self
)->value
, stb
, 0,
569 &opts
, python_language
);
570 s
= ui_file_xstrdup (stb
, NULL
);
572 GDB_PY_HANDLE_EXCEPTION (except
);
574 do_cleanups (old_chain
);
576 result
= PyUnicode_Decode (s
, strlen (s
), host_charset (), NULL
);
582 /* Implements gdb.Value.is_optimized_out. */
584 valpy_get_is_optimized_out (PyObject
*self
, void *closure
)
586 struct value
*value
= ((value_object
*) self
)->value
;
588 if (value_optimized_out (value
))
594 /* Calculate and return the address of the PyObject as the value of
595 the builtin __hash__ call. */
597 valpy_hash (PyObject
*self
)
599 return (long) (intptr_t) self
;
617 /* If TYPE is a reference, return the target; otherwise return TYPE. */
618 #define STRIP_REFERENCE(TYPE) \
619 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
621 /* Returns a value object which is the result of applying the operation
622 specified by OPCODE to the given arguments. */
624 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
626 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
627 volatile struct gdb_exception except
;
629 TRY_CATCH (except
, RETURN_MASK_ALL
)
631 struct value
*arg1
, *arg2
;
633 /* If the gdb.Value object is the second operand, then it will be passed
634 to us as the OTHER argument, and SELF will be an entirely different
635 kind of object, altogether. Because of this, we can't assume self is
636 a gdb.Value object and need to convert it from python as well. */
637 arg1
= convert_value_from_python (self
);
641 arg2
= convert_value_from_python (other
);
649 struct type
*ltype
= value_type (arg1
);
650 struct type
*rtype
= value_type (arg2
);
652 CHECK_TYPEDEF (ltype
);
653 ltype
= STRIP_REFERENCE (ltype
);
654 CHECK_TYPEDEF (rtype
);
655 rtype
= STRIP_REFERENCE (rtype
);
657 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
658 && is_integral_type (rtype
))
659 res_val
= value_ptradd (arg1
, value_as_long (arg2
));
660 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
661 && is_integral_type (ltype
))
662 res_val
= value_ptradd (arg2
, value_as_long (arg1
));
664 res_val
= value_binop (arg1
, arg2
, BINOP_ADD
);
669 struct type
*ltype
= value_type (arg1
);
670 struct type
*rtype
= value_type (arg2
);
672 CHECK_TYPEDEF (ltype
);
673 ltype
= STRIP_REFERENCE (ltype
);
674 CHECK_TYPEDEF (rtype
);
675 rtype
= STRIP_REFERENCE (rtype
);
677 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
678 && TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
679 /* A ptrdiff_t for the target would be preferable here. */
680 res_val
= value_from_longest (builtin_type_pyint
,
681 value_ptrdiff (arg1
, arg2
));
682 else if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
683 && is_integral_type (rtype
))
684 res_val
= value_ptradd (arg1
, - value_as_long (arg2
));
686 res_val
= value_binop (arg1
, arg2
, BINOP_SUB
);
690 res_val
= value_binop (arg1
, arg2
, BINOP_MUL
);
693 res_val
= value_binop (arg1
, arg2
, BINOP_DIV
);
696 res_val
= value_binop (arg1
, arg2
, BINOP_REM
);
699 res_val
= value_binop (arg1
, arg2
, BINOP_EXP
);
702 res_val
= value_binop (arg1
, arg2
, BINOP_LSH
);
705 res_val
= value_binop (arg1
, arg2
, BINOP_RSH
);
708 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_AND
);
711 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_IOR
);
714 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_XOR
);
718 GDB_PY_HANDLE_EXCEPTION (except
);
720 return res_val
? value_to_value_object (res_val
) : NULL
;
724 valpy_add (PyObject
*self
, PyObject
*other
)
726 return valpy_binop (VALPY_ADD
, self
, other
);
730 valpy_subtract (PyObject
*self
, PyObject
*other
)
732 return valpy_binop (VALPY_SUB
, self
, other
);
736 valpy_multiply (PyObject
*self
, PyObject
*other
)
738 return valpy_binop (VALPY_MUL
, self
, other
);
742 valpy_divide (PyObject
*self
, PyObject
*other
)
744 return valpy_binop (VALPY_DIV
, self
, other
);
748 valpy_remainder (PyObject
*self
, PyObject
*other
)
750 return valpy_binop (VALPY_REM
, self
, other
);
754 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
756 /* We don't support the ternary form of pow. I don't know how to express
757 that, so let's just throw NotImplementedError to at least do something
759 if (unused
!= Py_None
)
761 PyErr_SetString (PyExc_NotImplementedError
,
762 "Invalid operation on gdb.Value.");
766 return valpy_binop (VALPY_POW
, self
, other
);
770 valpy_negative (PyObject
*self
)
772 struct value
*val
= NULL
;
773 volatile struct gdb_exception except
;
775 TRY_CATCH (except
, RETURN_MASK_ALL
)
777 val
= value_neg (((value_object
*) self
)->value
);
779 GDB_PY_HANDLE_EXCEPTION (except
);
781 return value_to_value_object (val
);
785 valpy_positive (PyObject
*self
)
787 return value_to_value_object (((value_object
*) self
)->value
);
791 valpy_absolute (PyObject
*self
)
793 struct value
*value
= ((value_object
*) self
)->value
;
795 if (value_less (value
, value_zero (value_type (value
), not_lval
)))
796 return valpy_negative (self
);
798 return valpy_positive (self
);
801 /* Implements boolean evaluation of gdb.Value. */
803 valpy_nonzero (PyObject
*self
)
805 value_object
*self_value
= (value_object
*) self
;
808 type
= check_typedef (value_type (self_value
->value
));
810 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
811 return !!value_as_long (self_value
->value
);
812 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
813 return value_as_double (self_value
->value
) != 0;
814 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
815 return !decimal_is_zero (value_contents (self_value
->value
),
817 gdbarch_byte_order (get_type_arch (type
)));
819 /* All other values are True. */
823 /* Implements ~ for value objects. */
825 valpy_invert (PyObject
*self
)
827 struct value
*val
= NULL
;
828 volatile struct gdb_exception except
;
830 TRY_CATCH (except
, RETURN_MASK_ALL
)
832 val
= value_complement (((value_object
*) self
)->value
);
834 GDB_PY_HANDLE_EXCEPTION (except
);
836 return value_to_value_object (val
);
839 /* Implements left shift for value objects. */
841 valpy_lsh (PyObject
*self
, PyObject
*other
)
843 return valpy_binop (VALPY_LSH
, self
, other
);
846 /* Implements right shift for value objects. */
848 valpy_rsh (PyObject
*self
, PyObject
*other
)
850 return valpy_binop (VALPY_RSH
, self
, other
);
853 /* Implements bitwise and for value objects. */
855 valpy_and (PyObject
*self
, PyObject
*other
)
857 return valpy_binop (VALPY_BITAND
, self
, other
);
860 /* Implements bitwise or for value objects. */
862 valpy_or (PyObject
*self
, PyObject
*other
)
864 return valpy_binop (VALPY_BITOR
, self
, other
);
867 /* Implements bitwise xor for value objects. */
869 valpy_xor (PyObject
*self
, PyObject
*other
)
871 return valpy_binop (VALPY_BITXOR
, self
, other
);
874 /* Implements comparison operations for value objects. */
876 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
879 struct value
*value_other
;
880 volatile struct gdb_exception except
;
882 if (other
== Py_None
)
883 /* Comparing with None is special. From what I can tell, in Python
884 None is smaller than anything else. */
896 PyErr_SetString (PyExc_NotImplementedError
,
897 _("Invalid operation on gdb.Value."));
901 TRY_CATCH (except
, RETURN_MASK_ALL
)
903 value_other
= convert_value_from_python (other
);
904 if (value_other
== NULL
)
912 result
= value_less (((value_object
*) self
)->value
, value_other
);
915 result
= value_less (((value_object
*) self
)->value
, value_other
)
916 || value_equal (((value_object
*) self
)->value
, value_other
);
919 result
= value_equal (((value_object
*) self
)->value
, value_other
);
922 result
= !value_equal (((value_object
*) self
)->value
, value_other
);
925 result
= value_less (value_other
, ((value_object
*) self
)->value
);
928 result
= value_less (value_other
, ((value_object
*) self
)->value
)
929 || value_equal (((value_object
*) self
)->value
, value_other
);
933 PyErr_SetString (PyExc_NotImplementedError
,
934 _("Invalid operation on gdb.Value."));
939 GDB_PY_HANDLE_EXCEPTION (except
);
941 /* In this case, the Python exception has already been set. */
951 /* Helper function to determine if a type is "int-like". */
953 is_intlike (struct type
*type
, int ptr_ok
)
955 CHECK_TYPEDEF (type
);
956 return (TYPE_CODE (type
) == TYPE_CODE_INT
957 || TYPE_CODE (type
) == TYPE_CODE_ENUM
958 || TYPE_CODE (type
) == TYPE_CODE_BOOL
959 || TYPE_CODE (type
) == TYPE_CODE_CHAR
960 || (ptr_ok
&& TYPE_CODE (type
) == TYPE_CODE_PTR
));
963 /* Implements conversion to int. */
965 valpy_int (PyObject
*self
)
967 struct value
*value
= ((value_object
*) self
)->value
;
968 struct type
*type
= value_type (value
);
970 volatile struct gdb_exception except
;
972 CHECK_TYPEDEF (type
);
973 if (!is_intlike (type
, 0))
975 PyErr_SetString (PyExc_RuntimeError
,
976 _("Cannot convert value to int."));
980 TRY_CATCH (except
, RETURN_MASK_ALL
)
982 l
= value_as_long (value
);
984 GDB_PY_HANDLE_EXCEPTION (except
);
986 #ifdef HAVE_LONG_LONG /* Defined by Python. */
987 /* If we have 'long long', and the value overflows a 'long', use a
988 Python Long; otherwise use a Python Int. */
989 if (sizeof (l
) > sizeof (long) && (l
> PyInt_GetMax ()
990 || l
< (- (LONGEST
) PyInt_GetMax ()) - 1))
991 return PyLong_FromLongLong (l
);
993 return PyInt_FromLong (l
);
996 /* Implements conversion to long. */
998 valpy_long (PyObject
*self
)
1000 struct value
*value
= ((value_object
*) self
)->value
;
1001 struct type
*type
= value_type (value
);
1003 volatile struct gdb_exception except
;
1005 if (!is_intlike (type
, 1))
1007 PyErr_SetString (PyExc_RuntimeError
,
1008 _("Cannot convert value to long."));
1012 TRY_CATCH (except
, RETURN_MASK_ALL
)
1014 l
= value_as_long (value
);
1016 GDB_PY_HANDLE_EXCEPTION (except
);
1018 #ifdef HAVE_LONG_LONG /* Defined by Python. */
1019 return PyLong_FromLongLong (l
);
1021 return PyLong_FromLong (l
);
1025 /* Implements conversion to float. */
1027 valpy_float (PyObject
*self
)
1029 struct value
*value
= ((value_object
*) self
)->value
;
1030 struct type
*type
= value_type (value
);
1032 volatile struct gdb_exception except
;
1034 CHECK_TYPEDEF (type
);
1035 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
1037 PyErr_SetString (PyExc_RuntimeError
,
1038 _("Cannot convert value to float."));
1042 TRY_CATCH (except
, RETURN_MASK_ALL
)
1044 d
= value_as_double (value
);
1046 GDB_PY_HANDLE_EXCEPTION (except
);
1048 return PyFloat_FromDouble (d
);
1051 /* Returns an object for a value which is released from the all_values chain,
1052 so its lifetime is not bound to the execution of a command. */
1054 value_to_value_object (struct value
*val
)
1056 value_object
*val_obj
;
1058 val_obj
= PyObject_New (value_object
, &value_object_type
);
1059 if (val_obj
!= NULL
)
1061 val_obj
->value
= val
;
1063 val_obj
->address
= NULL
;
1064 val_obj
->type
= NULL
;
1065 val_obj
->dynamic_type
= NULL
;
1066 note_value (val_obj
);
1069 return (PyObject
*) val_obj
;
1072 /* Returns a borrowed reference to the struct value corresponding to
1073 the given value object. */
1075 value_object_to_value (PyObject
*self
)
1079 if (! PyObject_TypeCheck (self
, &value_object_type
))
1081 real
= (value_object
*) self
;
1085 /* Try to convert a Python value to a gdb value. If the value cannot
1086 be converted, set a Python exception and return NULL. Returns a
1087 reference to a new value on the all_values chain. */
1090 convert_value_from_python (PyObject
*obj
)
1092 struct value
*value
= NULL
; /* -Wall */
1093 struct cleanup
*old
;
1094 volatile struct gdb_exception except
;
1097 gdb_assert (obj
!= NULL
);
1099 TRY_CATCH (except
, RETURN_MASK_ALL
)
1101 if (PyBool_Check (obj
))
1103 cmp
= PyObject_IsTrue (obj
);
1105 value
= value_from_longest (builtin_type_pybool
, cmp
);
1107 else if (PyInt_Check (obj
))
1109 long l
= PyInt_AsLong (obj
);
1111 if (! PyErr_Occurred ())
1112 value
= value_from_longest (builtin_type_pyint
, l
);
1114 else if (PyLong_Check (obj
))
1116 LONGEST l
= PyLong_AsLongLong (obj
);
1118 if (PyErr_Occurred ())
1120 /* If the error was an overflow, we can try converting to
1121 ULONGEST instead. */
1122 if (PyErr_ExceptionMatches (PyExc_OverflowError
))
1124 PyObject
*etype
, *evalue
, *etraceback
, *zero
;
1126 PyErr_Fetch (&etype
, &evalue
, &etraceback
);
1127 zero
= PyInt_FromLong (0);
1129 /* Check whether obj is positive. */
1130 if (PyObject_RichCompareBool (obj
, zero
, Py_GT
) > 0)
1134 ul
= PyLong_AsUnsignedLongLong (obj
);
1135 if (! PyErr_Occurred ())
1136 value
= value_from_ulongest (builtin_type_upylong
, ul
);
1139 /* There's nothing we can do. */
1140 PyErr_Restore (etype
, evalue
, etraceback
);
1146 value
= value_from_longest (builtin_type_pylong
, l
);
1148 else if (PyFloat_Check (obj
))
1150 double d
= PyFloat_AsDouble (obj
);
1152 if (! PyErr_Occurred ())
1153 value
= value_from_double (builtin_type_pyfloat
, d
);
1155 else if (gdbpy_is_string (obj
))
1159 s
= python_string_to_target_string (obj
);
1162 old
= make_cleanup (xfree
, s
);
1163 value
= value_cstring (s
, strlen (s
), builtin_type_pychar
);
1167 else if (PyObject_TypeCheck (obj
, &value_object_type
))
1168 value
= value_copy (((value_object
*) obj
)->value
);
1169 else if (gdbpy_is_lazy_string (obj
))
1172 PyObject
*function
= PyString_FromString ("value");
1174 result
= PyObject_CallMethodObjArgs (obj
, function
, NULL
);
1175 value
= value_copy (((value_object
*) result
)->value
);
1178 PyErr_Format (PyExc_TypeError
, _("Could not convert Python object: %s."),
1179 PyString_AsString (PyObject_Str (obj
)));
1181 if (except
.reason
< 0)
1183 PyErr_Format (except
.reason
== RETURN_QUIT
1184 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
1185 "%s", except
.message
);
1192 /* Returns value object in the ARGth position in GDB's history. */
1194 gdbpy_history (PyObject
*self
, PyObject
*args
)
1197 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
1198 volatile struct gdb_exception except
;
1200 if (!PyArg_ParseTuple (args
, "i", &i
))
1203 TRY_CATCH (except
, RETURN_MASK_ALL
)
1205 res_val
= access_value_history (i
);
1207 GDB_PY_HANDLE_EXCEPTION (except
);
1209 return value_to_value_object (res_val
);
1212 /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
1215 gdbpy_is_value_object (PyObject
*obj
)
1217 return PyObject_TypeCheck (obj
, &value_object_type
);
1221 gdbpy_initialize_values (void)
1223 if (PyType_Ready (&value_object_type
) < 0)
1226 Py_INCREF (&value_object_type
);
1227 PyModule_AddObject (gdb_module
, "Value", (PyObject
*) &value_object_type
);
1229 values_in_python
= NULL
;
1234 static PyGetSetDef value_object_getset
[] = {
1235 { "address", valpy_get_address
, NULL
, "The address of the value.",
1237 { "is_optimized_out", valpy_get_is_optimized_out
, NULL
,
1238 "Boolean telling whether the value is optimized out (i.e., not available).",
1240 { "type", valpy_get_type
, NULL
, "Type of the value.", NULL
},
1241 { "dynamic_type", valpy_get_dynamic_type
, NULL
,
1242 "Dynamic type of the value.", NULL
},
1243 {NULL
} /* Sentinel */
1246 static PyMethodDef value_object_methods
[] = {
1247 { "cast", valpy_cast
, METH_VARARGS
, "Cast the value to the supplied type." },
1248 { "dynamic_cast", valpy_dynamic_cast
, METH_VARARGS
,
1249 "dynamic_cast (gdb.Type) -> gdb.Value\n\
1250 Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
1252 { "reinterpret_cast", valpy_reinterpret_cast
, METH_VARARGS
,
1253 "reinterpret_cast (gdb.Type) -> gdb.Value\n\
1254 Cast the value to the supplied type, as if by the C++\n\
1255 reinterpret_cast operator."
1257 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
1258 { "lazy_string", (PyCFunction
) valpy_lazy_string
, METH_VARARGS
| METH_KEYWORDS
,
1259 "lazy_string ([encoding] [, length]) -> lazy_string\n\
1260 Return a lazy string representation of the value." },
1261 { "string", (PyCFunction
) valpy_string
, METH_VARARGS
| METH_KEYWORDS
,
1262 "string ([encoding] [, errors] [, length]) -> string\n\
1263 Return Unicode string representation of the value." },
1264 {NULL
} /* Sentinel */
1267 static PyNumberMethods value_object_as_number
= {
1273 NULL
, /* nb_divmod */
1274 valpy_power
, /* nb_power */
1275 valpy_negative
, /* nb_negative */
1276 valpy_positive
, /* nb_positive */
1277 valpy_absolute
, /* nb_absolute */
1278 valpy_nonzero
, /* nb_nonzero */
1279 valpy_invert
, /* nb_invert */
1280 valpy_lsh
, /* nb_lshift */
1281 valpy_rsh
, /* nb_rshift */
1282 valpy_and
, /* nb_and */
1283 valpy_xor
, /* nb_xor */
1284 valpy_or
, /* nb_or */
1285 NULL
, /* nb_coerce */
1286 valpy_int
, /* nb_int */
1287 valpy_long
, /* nb_long */
1288 valpy_float
, /* nb_float */
1293 static PyMappingMethods value_object_as_mapping
= {
1299 PyTypeObject value_object_type
= {
1300 PyObject_HEAD_INIT (NULL
)
1302 "gdb.Value", /*tp_name*/
1303 sizeof (value_object
), /*tp_basicsize*/
1305 valpy_dealloc
, /*tp_dealloc*/
1311 &value_object_as_number
, /*tp_as_number*/
1312 0, /*tp_as_sequence*/
1313 &value_object_as_mapping
, /*tp_as_mapping*/
1314 valpy_hash
, /*tp_hash*/
1315 valpy_call
, /*tp_call*/
1316 valpy_str
, /*tp_str*/
1320 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
, /*tp_flags*/
1321 "GDB value object", /* tp_doc */
1322 0, /* tp_traverse */
1324 valpy_richcompare
, /* tp_richcompare */
1325 0, /* tp_weaklistoffset */
1327 0, /* tp_iternext */
1328 value_object_methods
, /* tp_methods */
1330 value_object_getset
, /* tp_getset */
1333 0, /* tp_descr_get */
1334 0, /* tp_descr_set */
1335 0, /* tp_dictoffset */
1338 valpy_new
/* tp_new */
1344 preserve_python_values (struct objfile
*objfile
, htab_t copied_types
)
1349 #endif /* HAVE_PYTHON */