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"
33 #include "python-internal.h"
35 /* Even though Python scalar types directly map to host types, we use
36 target types here to remain consistent with the the values system in
37 GDB (which uses target arithmetic). */
39 /* Python's integer type corresponds to C's long type. */
40 #define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long
42 /* Python's float type corresponds to C's double type. */
43 #define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double
45 /* Python's long type corresponds to C's long long type. */
46 #define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long
48 /* Python's long type corresponds to C's long long type. Unsigned version. */
49 #define builtin_type_upylong builtin_type \
50 (python_gdbarch)->builtin_unsigned_long_long
52 #define builtin_type_pybool \
53 language_bool_type (python_language, python_gdbarch)
55 #define builtin_type_pychar \
56 language_string_char_type (python_language, python_gdbarch)
58 typedef struct value_object
{
60 struct value_object
*next
;
61 struct value_object
*prev
;
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 self
->ob_type
->tp_free (self
);
107 /* Helper to push a Value object on the global list. */
109 note_value (value_object
*value_obj
)
111 value_obj
->next
= values_in_python
;
113 value_obj
->next
->prev
= value_obj
;
114 value_obj
->prev
= NULL
;
115 values_in_python
= value_obj
;
118 /* Called when a new gdb.Value object needs to be allocated. */
120 valpy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*keywords
)
122 struct value
*value
= NULL
; /* Initialize to appease gcc warning. */
123 value_object
*value_obj
;
125 if (PyTuple_Size (args
) != 1)
127 PyErr_SetString (PyExc_TypeError
, _("Value object creation takes only "
132 value_obj
= (value_object
*) subtype
->tp_alloc (subtype
, 1);
133 if (value_obj
== NULL
)
135 PyErr_SetString (PyExc_MemoryError
, _("Could not allocate memory to "
136 "create Value object."));
140 value
= convert_value_from_python (PyTuple_GetItem (args
, 0));
143 subtype
->tp_free (value_obj
);
147 value_obj
->value
= value
;
148 value_incref (value
);
149 value_obj
->address
= NULL
;
150 value_obj
->type
= NULL
;
151 note_value (value_obj
);
153 return (PyObject
*) value_obj
;
156 /* Iterate over all the Value objects, calling preserve_one_value on
159 preserve_python_values (struct objfile
*objfile
, htab_t copied_types
)
163 for (iter
= values_in_python
; iter
; iter
= iter
->next
)
164 preserve_one_value (iter
->value
, objfile
, copied_types
);
167 /* Given a value of a pointer type, apply the C unary * operator to it. */
169 valpy_dereference (PyObject
*self
, PyObject
*args
)
171 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
172 volatile struct gdb_exception except
;
174 TRY_CATCH (except
, RETURN_MASK_ALL
)
176 res_val
= value_ind (((value_object
*) self
)->value
);
178 GDB_PY_HANDLE_EXCEPTION (except
);
180 return value_to_value_object (res_val
);
183 /* Return "&value". */
185 valpy_get_address (PyObject
*self
, void *closure
)
187 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
188 value_object
*val_obj
= (value_object
*) self
;
189 volatile struct gdb_exception except
;
191 if (!val_obj
->address
)
193 TRY_CATCH (except
, RETURN_MASK_ALL
)
195 res_val
= value_addr (val_obj
->value
);
197 if (except
.reason
< 0)
199 val_obj
->address
= Py_None
;
203 val_obj
->address
= value_to_value_object (res_val
);
206 Py_INCREF (val_obj
->address
);
208 return val_obj
->address
;
211 /* Return type of the value. */
213 valpy_get_type (PyObject
*self
, void *closure
)
215 value_object
*obj
= (value_object
*) self
;
219 obj
->type
= type_to_type_object (value_type (obj
->value
));
223 Py_INCREF (obj
->type
);
226 Py_INCREF (obj
->type
);
230 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
231 string. Return a PyObject representing a lazy_string_object type.
232 A lazy string is a pointer to a string with an optional encoding and
233 length. If ENCODING is not given, encoding is set to None. If an
234 ENCODING is provided the encoding parameter is set to ENCODING, but
235 the string is not encoded. If LENGTH is provided then the length
236 parameter is set to LENGTH, otherwise length will be set to -1 (first
237 null of appropriate with). */
239 valpy_lazy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
242 struct value
*value
= ((value_object
*) self
)->value
;
243 const char *user_encoding
= NULL
;
244 static char *keywords
[] = { "encoding", "length", NULL
};
247 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|si", keywords
,
248 &user_encoding
, &length
))
251 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_PTR
)
252 value
= value_ind (value
);
254 str_obj
= gdbpy_create_lazy_string_object (value_address (value
), length
,
255 user_encoding
, value_type (value
));
257 return (PyObject
*) str_obj
;
260 /* Implementation of gdb.Value.string ([encoding] [, errors]
261 [, length]) -> string. Return Unicode string with value contents.
262 If ENCODING is not given, the string is assumed to be encoded in
263 the target's charset. If LENGTH is provided, only fetch string to
264 the length provided. */
267 valpy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
271 struct value
*value
= ((value_object
*) self
)->value
;
272 volatile struct gdb_exception except
;
274 const char *encoding
= NULL
;
275 const char *errors
= NULL
;
276 const char *user_encoding
= NULL
;
277 const char *la_encoding
= NULL
;
278 struct type
*char_type
;
279 static char *keywords
[] = { "encoding", "errors", "length", NULL
};
281 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|ssi", keywords
,
282 &user_encoding
, &errors
, &length
))
285 TRY_CATCH (except
, RETURN_MASK_ALL
)
287 LA_GET_STRING (value
, &buffer
, &length
, &char_type
, &la_encoding
);
289 GDB_PY_HANDLE_EXCEPTION (except
);
291 encoding
= (user_encoding
&& *user_encoding
) ? user_encoding
: la_encoding
;
292 unicode
= PyUnicode_Decode (buffer
, length
* TYPE_LENGTH (char_type
),
299 /* A helper function that implements the various cast operators. */
302 valpy_do_cast (PyObject
*self
, PyObject
*args
, enum exp_opcode op
)
306 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
307 volatile struct gdb_exception except
;
309 if (! PyArg_ParseTuple (args
, "O", &type_obj
))
312 type
= type_object_to_type (type_obj
);
315 PyErr_SetString (PyExc_RuntimeError
,
316 _("Argument must be a type."));
320 TRY_CATCH (except
, RETURN_MASK_ALL
)
322 struct value
*val
= ((value_object
*) self
)->value
;
324 if (op
== UNOP_DYNAMIC_CAST
)
325 res_val
= value_dynamic_cast (type
, val
);
326 else if (op
== UNOP_REINTERPRET_CAST
)
327 res_val
= value_reinterpret_cast (type
, val
);
330 gdb_assert (op
== UNOP_CAST
);
331 res_val
= value_cast (type
, val
);
334 GDB_PY_HANDLE_EXCEPTION (except
);
336 return value_to_value_object (res_val
);
339 /* Implementation of the "cast" method. */
342 valpy_cast (PyObject
*self
, PyObject
*args
)
344 return valpy_do_cast (self
, args
, UNOP_CAST
);
347 /* Implementation of the "dynamic_cast" method. */
350 valpy_dynamic_cast (PyObject
*self
, PyObject
*args
)
352 return valpy_do_cast (self
, args
, UNOP_DYNAMIC_CAST
);
355 /* Implementation of the "reinterpret_cast" method. */
358 valpy_reinterpret_cast (PyObject
*self
, PyObject
*args
)
360 return valpy_do_cast (self
, args
, UNOP_REINTERPRET_CAST
);
364 valpy_length (PyObject
*self
)
366 /* We don't support getting the number of elements in a struct / class. */
367 PyErr_SetString (PyExc_NotImplementedError
,
368 _("Invalid operation on gdb.Value."));
372 /* Given string name of an element inside structure, return its value
375 valpy_getitem (PyObject
*self
, PyObject
*key
)
377 value_object
*self_value
= (value_object
*) self
;
379 struct value
*res_val
= NULL
;
380 volatile struct gdb_exception except
;
382 if (gdbpy_is_string (key
))
384 field
= python_string_to_host_string (key
);
389 TRY_CATCH (except
, RETURN_MASK_ALL
)
391 struct value
*tmp
= self_value
->value
;
394 res_val
= value_struct_elt (&tmp
, NULL
, field
, 0, NULL
);
397 /* Assume we are attempting an array access, and let the
398 value code throw an exception if the index has an invalid
400 struct value
*idx
= convert_value_from_python (key
);
404 /* Check the value's type is something that can be accessed via
408 tmp
= coerce_ref (tmp
);
409 type
= check_typedef (value_type (tmp
));
410 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
411 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
412 error( _("Cannot subscript requested type."));
414 res_val
= value_subscript (tmp
, value_as_long (idx
));
420 GDB_PY_HANDLE_EXCEPTION (except
);
422 return res_val
? value_to_value_object (res_val
) : NULL
;
426 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
428 PyErr_Format (PyExc_NotImplementedError
,
429 _("Setting of struct elements is not currently supported."));
433 /* Called by the Python interpreter to perform an inferior function
434 call on the value. */
436 valpy_call (PyObject
*self
, PyObject
*args
, PyObject
*keywords
)
438 struct value
*return_value
= NULL
;
439 Py_ssize_t args_count
;
440 volatile struct gdb_exception except
;
441 struct value
*function
= ((value_object
*) self
)->value
;
442 struct value
**vargs
= NULL
;
443 struct type
*ftype
= check_typedef (value_type (function
));
445 if (TYPE_CODE (ftype
) != TYPE_CODE_FUNC
)
447 PyErr_SetString (PyExc_RuntimeError
,
448 _("Value is not callable (not TYPE_CODE_FUNC)."));
452 args_count
= PyTuple_Size (args
);
457 vargs
= alloca (sizeof (struct value
*) * args_count
);
458 for (i
= 0; i
< args_count
; i
++)
460 PyObject
*item
= PyTuple_GetItem (args
, i
);
465 vargs
[i
] = convert_value_from_python (item
);
466 if (vargs
[i
] == NULL
)
471 TRY_CATCH (except
, RETURN_MASK_ALL
)
473 return_value
= call_function_by_hand (function
, args_count
, vargs
);
475 GDB_PY_HANDLE_EXCEPTION (except
);
477 return value_to_value_object (return_value
);
480 /* Called by the Python interpreter to obtain string representation
483 valpy_str (PyObject
*self
)
487 struct cleanup
*old_chain
;
489 struct value_print_options opts
;
490 volatile struct gdb_exception except
;
492 get_user_print_options (&opts
);
495 stb
= mem_fileopen ();
496 old_chain
= make_cleanup_ui_file_delete (stb
);
498 TRY_CATCH (except
, RETURN_MASK_ALL
)
500 common_val_print (((value_object
*) self
)->value
, stb
, 0,
501 &opts
, python_language
);
502 s
= ui_file_xstrdup (stb
, NULL
);
504 GDB_PY_HANDLE_EXCEPTION (except
);
506 do_cleanups (old_chain
);
508 result
= PyUnicode_Decode (s
, strlen (s
), host_charset (), NULL
);
514 /* Implements gdb.Value.is_optimized_out. */
516 valpy_get_is_optimized_out (PyObject
*self
, void *closure
)
518 struct value
*value
= ((value_object
*) self
)->value
;
520 if (value_optimized_out (value
))
526 /* Calculate and return the address of the PyObject as the value of
527 the builtin __hash__ call. */
529 valpy_hash (PyObject
*self
)
531 return (long) (intptr_t) self
;
549 /* If TYPE is a reference, return the target; otherwise return TYPE. */
550 #define STRIP_REFERENCE(TYPE) \
551 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
553 /* Returns a value object which is the result of applying the operation
554 specified by OPCODE to the given arguments. */
556 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
558 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
559 volatile struct gdb_exception except
;
561 TRY_CATCH (except
, RETURN_MASK_ALL
)
563 struct value
*arg1
, *arg2
;
565 /* If the gdb.Value object is the second operand, then it will be passed
566 to us as the OTHER argument, and SELF will be an entirely different
567 kind of object, altogether. Because of this, we can't assume self is
568 a gdb.Value object and need to convert it from python as well. */
569 arg1
= convert_value_from_python (self
);
573 arg2
= convert_value_from_python (other
);
581 struct type
*ltype
= value_type (arg1
);
582 struct type
*rtype
= value_type (arg2
);
584 CHECK_TYPEDEF (ltype
);
585 ltype
= STRIP_REFERENCE (ltype
);
586 CHECK_TYPEDEF (rtype
);
587 rtype
= STRIP_REFERENCE (rtype
);
589 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
590 && is_integral_type (rtype
))
591 res_val
= value_ptradd (arg1
, value_as_long (arg2
));
592 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
593 && is_integral_type (ltype
))
594 res_val
= value_ptradd (arg2
, value_as_long (arg1
));
596 res_val
= value_binop (arg1
, arg2
, BINOP_ADD
);
601 struct type
*ltype
= value_type (arg1
);
602 struct type
*rtype
= value_type (arg2
);
604 CHECK_TYPEDEF (ltype
);
605 ltype
= STRIP_REFERENCE (ltype
);
606 CHECK_TYPEDEF (rtype
);
607 rtype
= STRIP_REFERENCE (rtype
);
609 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
610 && TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
611 /* A ptrdiff_t for the target would be preferable here. */
612 res_val
= value_from_longest (builtin_type_pyint
,
613 value_ptrdiff (arg1
, arg2
));
614 else if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
615 && is_integral_type (rtype
))
616 res_val
= value_ptradd (arg1
, - value_as_long (arg2
));
618 res_val
= value_binop (arg1
, arg2
, BINOP_SUB
);
622 res_val
= value_binop (arg1
, arg2
, BINOP_MUL
);
625 res_val
= value_binop (arg1
, arg2
, BINOP_DIV
);
628 res_val
= value_binop (arg1
, arg2
, BINOP_REM
);
631 res_val
= value_binop (arg1
, arg2
, BINOP_EXP
);
634 res_val
= value_binop (arg1
, arg2
, BINOP_LSH
);
637 res_val
= value_binop (arg1
, arg2
, BINOP_RSH
);
640 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_AND
);
643 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_IOR
);
646 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_XOR
);
650 GDB_PY_HANDLE_EXCEPTION (except
);
652 return res_val
? value_to_value_object (res_val
) : NULL
;
656 valpy_add (PyObject
*self
, PyObject
*other
)
658 return valpy_binop (VALPY_ADD
, self
, other
);
662 valpy_subtract (PyObject
*self
, PyObject
*other
)
664 return valpy_binop (VALPY_SUB
, self
, other
);
668 valpy_multiply (PyObject
*self
, PyObject
*other
)
670 return valpy_binop (VALPY_MUL
, self
, other
);
674 valpy_divide (PyObject
*self
, PyObject
*other
)
676 return valpy_binop (VALPY_DIV
, self
, other
);
680 valpy_remainder (PyObject
*self
, PyObject
*other
)
682 return valpy_binop (VALPY_REM
, self
, other
);
686 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
688 /* We don't support the ternary form of pow. I don't know how to express
689 that, so let's just throw NotImplementedError to at least do something
691 if (unused
!= Py_None
)
693 PyErr_SetString (PyExc_NotImplementedError
,
694 "Invalid operation on gdb.Value.");
698 return valpy_binop (VALPY_POW
, self
, other
);
702 valpy_negative (PyObject
*self
)
704 struct value
*val
= NULL
;
705 volatile struct gdb_exception except
;
707 TRY_CATCH (except
, RETURN_MASK_ALL
)
709 val
= value_neg (((value_object
*) self
)->value
);
711 GDB_PY_HANDLE_EXCEPTION (except
);
713 return value_to_value_object (val
);
717 valpy_positive (PyObject
*self
)
719 return value_to_value_object (((value_object
*) self
)->value
);
723 valpy_absolute (PyObject
*self
)
725 struct value
*value
= ((value_object
*) self
)->value
;
727 if (value_less (value
, value_zero (value_type (value
), not_lval
)))
728 return valpy_negative (self
);
730 return valpy_positive (self
);
733 /* Implements boolean evaluation of gdb.Value. */
735 valpy_nonzero (PyObject
*self
)
737 value_object
*self_value
= (value_object
*) self
;
740 type
= check_typedef (value_type (self_value
->value
));
742 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
743 return !!value_as_long (self_value
->value
);
744 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
745 return value_as_double (self_value
->value
) != 0;
746 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
747 return !decimal_is_zero (value_contents (self_value
->value
),
749 gdbarch_byte_order (get_type_arch (type
)));
751 /* All other values are True. */
755 /* Implements ~ for value objects. */
757 valpy_invert (PyObject
*self
)
759 struct value
*val
= NULL
;
760 volatile struct gdb_exception except
;
762 TRY_CATCH (except
, RETURN_MASK_ALL
)
764 val
= value_complement (((value_object
*) self
)->value
);
766 GDB_PY_HANDLE_EXCEPTION (except
);
768 return value_to_value_object (val
);
771 /* Implements left shift for value objects. */
773 valpy_lsh (PyObject
*self
, PyObject
*other
)
775 return valpy_binop (VALPY_LSH
, self
, other
);
778 /* Implements right shift for value objects. */
780 valpy_rsh (PyObject
*self
, PyObject
*other
)
782 return valpy_binop (VALPY_RSH
, self
, other
);
785 /* Implements bitwise and for value objects. */
787 valpy_and (PyObject
*self
, PyObject
*other
)
789 return valpy_binop (VALPY_BITAND
, self
, other
);
792 /* Implements bitwise or for value objects. */
794 valpy_or (PyObject
*self
, PyObject
*other
)
796 return valpy_binop (VALPY_BITOR
, self
, other
);
799 /* Implements bitwise xor for value objects. */
801 valpy_xor (PyObject
*self
, PyObject
*other
)
803 return valpy_binop (VALPY_BITXOR
, self
, other
);
806 /* Implements comparison operations for value objects. */
808 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
811 struct value
*value_other
;
812 volatile struct gdb_exception except
;
814 if (other
== Py_None
)
815 /* Comparing with None is special. From what I can tell, in Python
816 None is smaller than anything else. */
828 PyErr_SetString (PyExc_NotImplementedError
,
829 _("Invalid operation on gdb.Value."));
833 TRY_CATCH (except
, RETURN_MASK_ALL
)
835 value_other
= convert_value_from_python (other
);
836 if (value_other
== NULL
)
844 result
= value_less (((value_object
*) self
)->value
, value_other
);
847 result
= value_less (((value_object
*) self
)->value
, value_other
)
848 || value_equal (((value_object
*) self
)->value
, value_other
);
851 result
= value_equal (((value_object
*) self
)->value
, value_other
);
854 result
= !value_equal (((value_object
*) self
)->value
, value_other
);
857 result
= value_less (value_other
, ((value_object
*) self
)->value
);
860 result
= value_less (value_other
, ((value_object
*) self
)->value
)
861 || value_equal (((value_object
*) self
)->value
, value_other
);
865 PyErr_SetString (PyExc_NotImplementedError
,
866 _("Invalid operation on gdb.Value."));
871 GDB_PY_HANDLE_EXCEPTION (except
);
873 /* In this case, the Python exception has already been set. */
883 /* Helper function to determine if a type is "int-like". */
885 is_intlike (struct type
*type
, int ptr_ok
)
887 CHECK_TYPEDEF (type
);
888 return (TYPE_CODE (type
) == TYPE_CODE_INT
889 || TYPE_CODE (type
) == TYPE_CODE_ENUM
890 || TYPE_CODE (type
) == TYPE_CODE_BOOL
891 || TYPE_CODE (type
) == TYPE_CODE_CHAR
892 || (ptr_ok
&& TYPE_CODE (type
) == TYPE_CODE_PTR
));
895 /* Implements conversion to int. */
897 valpy_int (PyObject
*self
)
899 struct value
*value
= ((value_object
*) self
)->value
;
900 struct type
*type
= value_type (value
);
902 volatile struct gdb_exception except
;
904 CHECK_TYPEDEF (type
);
905 if (!is_intlike (type
, 0))
907 PyErr_SetString (PyExc_RuntimeError
,
908 _("Cannot convert value to int."));
912 TRY_CATCH (except
, RETURN_MASK_ALL
)
914 l
= value_as_long (value
);
916 GDB_PY_HANDLE_EXCEPTION (except
);
918 #ifdef HAVE_LONG_LONG /* Defined by Python. */
919 /* If we have 'long long', and the value overflows a 'long', use a
920 Python Long; otherwise use a Python Int. */
921 if (sizeof (l
) > sizeof (long) && (l
> PyInt_GetMax ()
922 || l
< (- (LONGEST
) PyInt_GetMax ()) - 1))
923 return PyLong_FromLongLong (l
);
925 return PyInt_FromLong (l
);
928 /* Implements conversion to long. */
930 valpy_long (PyObject
*self
)
932 struct value
*value
= ((value_object
*) self
)->value
;
933 struct type
*type
= value_type (value
);
935 volatile struct gdb_exception except
;
937 if (!is_intlike (type
, 1))
939 PyErr_SetString (PyExc_RuntimeError
,
940 _("Cannot convert value to long."));
944 TRY_CATCH (except
, RETURN_MASK_ALL
)
946 l
= value_as_long (value
);
948 GDB_PY_HANDLE_EXCEPTION (except
);
950 #ifdef HAVE_LONG_LONG /* Defined by Python. */
951 return PyLong_FromLongLong (l
);
953 return PyLong_FromLong (l
);
957 /* Implements conversion to float. */
959 valpy_float (PyObject
*self
)
961 struct value
*value
= ((value_object
*) self
)->value
;
962 struct type
*type
= value_type (value
);
964 volatile struct gdb_exception except
;
966 CHECK_TYPEDEF (type
);
967 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
969 PyErr_SetString (PyExc_RuntimeError
,
970 _("Cannot convert value to float."));
974 TRY_CATCH (except
, RETURN_MASK_ALL
)
976 d
= value_as_double (value
);
978 GDB_PY_HANDLE_EXCEPTION (except
);
980 return PyFloat_FromDouble (d
);
983 /* Returns an object for a value which is released from the all_values chain,
984 so its lifetime is not bound to the execution of a command. */
986 value_to_value_object (struct value
*val
)
988 value_object
*val_obj
;
990 val_obj
= PyObject_New (value_object
, &value_object_type
);
993 val_obj
->value
= val
;
995 val_obj
->address
= NULL
;
996 val_obj
->type
= NULL
;
997 note_value (val_obj
);
1000 return (PyObject
*) val_obj
;
1003 /* Returns a borrowed reference to the struct value corresponding to
1004 the given value object. */
1006 value_object_to_value (PyObject
*self
)
1010 if (! PyObject_TypeCheck (self
, &value_object_type
))
1012 real
= (value_object
*) self
;
1016 /* Try to convert a Python value to a gdb value. If the value cannot
1017 be converted, set a Python exception and return NULL. Returns a
1018 reference to a new value on the all_values chain. */
1021 convert_value_from_python (PyObject
*obj
)
1023 struct value
*value
= NULL
; /* -Wall */
1024 struct cleanup
*old
;
1025 volatile struct gdb_exception except
;
1028 gdb_assert (obj
!= NULL
);
1030 TRY_CATCH (except
, RETURN_MASK_ALL
)
1032 if (PyBool_Check (obj
))
1034 cmp
= PyObject_IsTrue (obj
);
1036 value
= value_from_longest (builtin_type_pybool
, cmp
);
1038 else if (PyInt_Check (obj
))
1040 long l
= PyInt_AsLong (obj
);
1042 if (! PyErr_Occurred ())
1043 value
= value_from_longest (builtin_type_pyint
, l
);
1045 else if (PyLong_Check (obj
))
1047 LONGEST l
= PyLong_AsLongLong (obj
);
1049 if (PyErr_Occurred ())
1051 /* If the error was an overflow, we can try converting to
1052 ULONGEST instead. */
1053 if (PyErr_ExceptionMatches (PyExc_OverflowError
))
1055 PyObject
*etype
, *evalue
, *etraceback
, *zero
;
1057 PyErr_Fetch (&etype
, &evalue
, &etraceback
);
1058 zero
= PyInt_FromLong (0);
1060 /* Check whether obj is positive. */
1061 if (PyObject_RichCompareBool (obj
, zero
, Py_GT
) > 0)
1065 ul
= PyLong_AsUnsignedLongLong (obj
);
1066 if (! PyErr_Occurred ())
1067 value
= value_from_ulongest (builtin_type_upylong
, ul
);
1070 /* There's nothing we can do. */
1071 PyErr_Restore (etype
, evalue
, etraceback
);
1077 value
= value_from_longest (builtin_type_pylong
, l
);
1079 else if (PyFloat_Check (obj
))
1081 double d
= PyFloat_AsDouble (obj
);
1083 if (! PyErr_Occurred ())
1084 value
= value_from_double (builtin_type_pyfloat
, d
);
1086 else if (gdbpy_is_string (obj
))
1090 s
= python_string_to_target_string (obj
);
1093 old
= make_cleanup (xfree
, s
);
1094 value
= value_cstring (s
, strlen (s
), builtin_type_pychar
);
1098 else if (PyObject_TypeCheck (obj
, &value_object_type
))
1099 value
= value_copy (((value_object
*) obj
)->value
);
1100 else if (gdbpy_is_lazy_string (obj
))
1103 PyObject
*function
= PyString_FromString ("value");
1105 result
= PyObject_CallMethodObjArgs (obj
, function
, NULL
);
1106 value
= value_copy (((value_object
*) result
)->value
);
1109 PyErr_Format (PyExc_TypeError
, _("Could not convert Python object: %s."),
1110 PyString_AsString (PyObject_Str (obj
)));
1112 if (except
.reason
< 0)
1114 PyErr_Format (except
.reason
== RETURN_QUIT
1115 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
1116 "%s", except
.message
);
1123 /* Returns value object in the ARGth position in GDB's history. */
1125 gdbpy_history (PyObject
*self
, PyObject
*args
)
1128 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
1129 volatile struct gdb_exception except
;
1131 if (!PyArg_ParseTuple (args
, "i", &i
))
1134 TRY_CATCH (except
, RETURN_MASK_ALL
)
1136 res_val
= access_value_history (i
);
1138 GDB_PY_HANDLE_EXCEPTION (except
);
1140 return value_to_value_object (res_val
);
1143 /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
1146 gdbpy_is_value_object (PyObject
*obj
)
1148 return PyObject_TypeCheck (obj
, &value_object_type
);
1152 gdbpy_initialize_values (void)
1154 if (PyType_Ready (&value_object_type
) < 0)
1157 Py_INCREF (&value_object_type
);
1158 PyModule_AddObject (gdb_module
, "Value", (PyObject
*) &value_object_type
);
1160 values_in_python
= NULL
;
1165 static PyGetSetDef value_object_getset
[] = {
1166 { "address", valpy_get_address
, NULL
, "The address of the value.",
1168 { "is_optimized_out", valpy_get_is_optimized_out
, NULL
,
1169 "Boolean telling whether the value is optimized out (i.e., not available).",
1171 { "type", valpy_get_type
, NULL
, "Type of the value.", NULL
},
1172 {NULL
} /* Sentinel */
1175 static PyMethodDef value_object_methods
[] = {
1176 { "cast", valpy_cast
, METH_VARARGS
, "Cast the value to the supplied type." },
1177 { "dynamic_cast", valpy_dynamic_cast
, METH_VARARGS
,
1178 "dynamic_cast (gdb.Type) -> gdb.Value\n\
1179 Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
1181 { "reinterpret_cast", valpy_reinterpret_cast
, METH_VARARGS
,
1182 "reinterpret_cast (gdb.Type) -> gdb.Value\n\
1183 Cast the value to the supplied type, as if by the C++\n\
1184 reinterpret_cast operator."
1186 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
1187 { "lazy_string", (PyCFunction
) valpy_lazy_string
, METH_VARARGS
| METH_KEYWORDS
,
1188 "lazy_string ([encoding] [, length]) -> lazy_string\n\
1189 Return a lazy string representation of the value." },
1190 { "string", (PyCFunction
) valpy_string
, METH_VARARGS
| METH_KEYWORDS
,
1191 "string ([encoding] [, errors] [, length]) -> string\n\
1192 Return Unicode string representation of the value." },
1193 {NULL
} /* Sentinel */
1196 static PyNumberMethods value_object_as_number
= {
1202 NULL
, /* nb_divmod */
1203 valpy_power
, /* nb_power */
1204 valpy_negative
, /* nb_negative */
1205 valpy_positive
, /* nb_positive */
1206 valpy_absolute
, /* nb_absolute */
1207 valpy_nonzero
, /* nb_nonzero */
1208 valpy_invert
, /* nb_invert */
1209 valpy_lsh
, /* nb_lshift */
1210 valpy_rsh
, /* nb_rshift */
1211 valpy_and
, /* nb_and */
1212 valpy_xor
, /* nb_xor */
1213 valpy_or
, /* nb_or */
1214 NULL
, /* nb_coerce */
1215 valpy_int
, /* nb_int */
1216 valpy_long
, /* nb_long */
1217 valpy_float
, /* nb_float */
1222 static PyMappingMethods value_object_as_mapping
= {
1228 PyTypeObject value_object_type
= {
1229 PyObject_HEAD_INIT (NULL
)
1231 "gdb.Value", /*tp_name*/
1232 sizeof (value_object
), /*tp_basicsize*/
1234 valpy_dealloc
, /*tp_dealloc*/
1240 &value_object_as_number
, /*tp_as_number*/
1241 0, /*tp_as_sequence*/
1242 &value_object_as_mapping
, /*tp_as_mapping*/
1243 valpy_hash
, /*tp_hash*/
1244 valpy_call
, /*tp_call*/
1245 valpy_str
, /*tp_str*/
1249 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
, /*tp_flags*/
1250 "GDB value object", /* tp_doc */
1251 0, /* tp_traverse */
1253 valpy_richcompare
, /* tp_richcompare */
1254 0, /* tp_weaklistoffset */
1256 0, /* tp_iternext */
1257 value_object_methods
, /* tp_methods */
1259 value_object_getset
, /* tp_getset */
1262 0, /* tp_descr_get */
1263 0, /* tp_descr_set */
1264 0, /* tp_dictoffset */
1267 valpy_new
/* tp_new */
1273 preserve_python_values (struct objfile
*objfile
, htab_t copied_types
)
1278 #endif /* HAVE_PYTHON */