1 /* Python interface to values.
3 Copyright (C) 2008, 2009, 2010, 2011, 2012 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 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. Returns NULL on
123 error, with a python exception set. */
125 valpy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*keywords
)
127 struct value
*value
= NULL
; /* Initialize to appease gcc warning. */
128 value_object
*value_obj
;
130 if (PyTuple_Size (args
) != 1)
132 PyErr_SetString (PyExc_TypeError
, _("Value object creation takes only "
137 value_obj
= (value_object
*) subtype
->tp_alloc (subtype
, 1);
138 if (value_obj
== NULL
)
140 PyErr_SetString (PyExc_MemoryError
, _("Could not allocate memory to "
141 "create Value object."));
145 value
= convert_value_from_python (PyTuple_GetItem (args
, 0));
148 subtype
->tp_free (value_obj
);
152 value_obj
->value
= value
;
153 release_value_or_incref (value
);
154 value_obj
->address
= NULL
;
155 value_obj
->type
= NULL
;
156 value_obj
->dynamic_type
= NULL
;
157 note_value (value_obj
);
159 return (PyObject
*) value_obj
;
162 /* Iterate over all the Value objects, calling preserve_one_value on
165 preserve_python_values (struct objfile
*objfile
, htab_t copied_types
)
169 for (iter
= values_in_python
; iter
; iter
= iter
->next
)
170 preserve_one_value (iter
->value
, objfile
, copied_types
);
173 /* Given a value of a pointer type, apply the C unary * operator to it. */
175 valpy_dereference (PyObject
*self
, PyObject
*args
)
177 volatile struct gdb_exception except
;
178 PyObject
*result
= NULL
;
180 TRY_CATCH (except
, RETURN_MASK_ALL
)
182 struct value
*res_val
;
183 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
185 res_val
= value_ind (((value_object
*) self
)->value
);
186 result
= value_to_value_object (res_val
);
187 do_cleanups (cleanup
);
189 GDB_PY_HANDLE_EXCEPTION (except
);
194 /* Return "&value". */
196 valpy_get_address (PyObject
*self
, void *closure
)
198 value_object
*val_obj
= (value_object
*) self
;
199 volatile struct gdb_exception except
;
201 if (!val_obj
->address
)
203 TRY_CATCH (except
, RETURN_MASK_ALL
)
205 struct value
*res_val
;
206 struct cleanup
*cleanup
207 = make_cleanup_value_free_to_mark (value_mark ());
209 res_val
= value_addr (val_obj
->value
);
210 val_obj
->address
= value_to_value_object (res_val
);
211 do_cleanups (cleanup
);
213 if (except
.reason
< 0)
215 val_obj
->address
= Py_None
;
220 Py_XINCREF (val_obj
->address
);
222 return val_obj
->address
;
225 /* Return type of the value. */
227 valpy_get_type (PyObject
*self
, void *closure
)
229 value_object
*obj
= (value_object
*) self
;
233 obj
->type
= type_to_type_object (value_type (obj
->value
));
237 Py_INCREF (obj
->type
);
241 /* Return dynamic type of the value. */
244 valpy_get_dynamic_type (PyObject
*self
, void *closure
)
246 value_object
*obj
= (value_object
*) self
;
247 volatile struct gdb_exception except
;
248 struct type
*type
= NULL
;
250 if (obj
->dynamic_type
!= NULL
)
252 Py_INCREF (obj
->dynamic_type
);
253 return obj
->dynamic_type
;
256 TRY_CATCH (except
, RETURN_MASK_ALL
)
258 struct value
*val
= obj
->value
;
259 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
261 type
= value_type (val
);
262 CHECK_TYPEDEF (type
);
264 if (((TYPE_CODE (type
) == TYPE_CODE_PTR
)
265 || (TYPE_CODE (type
) == TYPE_CODE_REF
))
266 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_CLASS
))
268 struct value
*target
;
269 int was_pointer
= TYPE_CODE (type
) == TYPE_CODE_PTR
;
271 target
= value_ind (val
);
272 type
= value_rtti_type (target
, NULL
, NULL
, NULL
);
277 type
= lookup_pointer_type (type
);
279 type
= lookup_reference_type (type
);
282 else if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
283 type
= value_rtti_type (val
, NULL
, NULL
, NULL
);
286 /* Re-use object's static type. */
290 do_cleanups (cleanup
);
292 GDB_PY_HANDLE_EXCEPTION (except
);
296 /* Ensure that the TYPE field is ready. */
297 if (!valpy_get_type (self
, NULL
))
299 /* We don't need to incref here, because valpy_get_type already
301 obj
->dynamic_type
= obj
->type
;
304 obj
->dynamic_type
= type_to_type_object (type
);
306 Py_INCREF (obj
->dynamic_type
);
307 return obj
->dynamic_type
;
310 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
311 string. Return a PyObject representing a lazy_string_object type.
312 A lazy string is a pointer to a string with an optional encoding and
313 length. If ENCODING is not given, encoding is set to None. If an
314 ENCODING is provided the encoding parameter is set to ENCODING, but
315 the string is not encoded. If LENGTH is provided then the length
316 parameter is set to LENGTH, otherwise length will be set to -1 (first
317 null of appropriate with). */
319 valpy_lazy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
321 gdb_py_longest length
= -1;
322 struct value
*value
= ((value_object
*) self
)->value
;
323 const char *user_encoding
= NULL
;
324 static char *keywords
[] = { "encoding", "length", NULL
};
325 PyObject
*str_obj
= NULL
;
326 volatile struct gdb_exception except
;
328 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|s" GDB_PY_LL_ARG
, keywords
,
329 &user_encoding
, &length
))
332 TRY_CATCH (except
, RETURN_MASK_ALL
)
334 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
336 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_PTR
)
337 value
= value_ind (value
);
339 str_obj
= gdbpy_create_lazy_string_object (value_address (value
), length
,
343 do_cleanups (cleanup
);
345 GDB_PY_HANDLE_EXCEPTION (except
);
350 /* Implementation of gdb.Value.string ([encoding] [, errors]
351 [, length]) -> string. Return Unicode string with value contents.
352 If ENCODING is not given, the string is assumed to be encoded in
353 the target's charset. If LENGTH is provided, only fetch string to
354 the length provided. */
357 valpy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
361 struct value
*value
= ((value_object
*) self
)->value
;
362 volatile struct gdb_exception except
;
364 const char *encoding
= NULL
;
365 const char *errors
= NULL
;
366 const char *user_encoding
= NULL
;
367 const char *la_encoding
= NULL
;
368 struct type
*char_type
;
369 static char *keywords
[] = { "encoding", "errors", "length", NULL
};
371 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|ssi", keywords
,
372 &user_encoding
, &errors
, &length
))
375 TRY_CATCH (except
, RETURN_MASK_ALL
)
377 LA_GET_STRING (value
, &buffer
, &length
, &char_type
, &la_encoding
);
379 GDB_PY_HANDLE_EXCEPTION (except
);
381 encoding
= (user_encoding
&& *user_encoding
) ? user_encoding
: la_encoding
;
382 unicode
= PyUnicode_Decode (buffer
, length
* TYPE_LENGTH (char_type
),
389 /* A helper function that implements the various cast operators. */
392 valpy_do_cast (PyObject
*self
, PyObject
*args
, enum exp_opcode op
)
394 PyObject
*type_obj
, *result
= NULL
;
396 volatile struct gdb_exception except
;
398 if (! PyArg_ParseTuple (args
, "O", &type_obj
))
401 type
= type_object_to_type (type_obj
);
404 PyErr_SetString (PyExc_RuntimeError
,
405 _("Argument must be a type."));
409 TRY_CATCH (except
, RETURN_MASK_ALL
)
411 struct value
*val
= ((value_object
*) self
)->value
;
412 struct value
*res_val
;
413 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
415 if (op
== UNOP_DYNAMIC_CAST
)
416 res_val
= value_dynamic_cast (type
, val
);
417 else if (op
== UNOP_REINTERPRET_CAST
)
418 res_val
= value_reinterpret_cast (type
, val
);
421 gdb_assert (op
== UNOP_CAST
);
422 res_val
= value_cast (type
, val
);
425 result
= value_to_value_object (res_val
);
426 do_cleanups (cleanup
);
428 GDB_PY_HANDLE_EXCEPTION (except
);
433 /* Implementation of the "cast" method. */
436 valpy_cast (PyObject
*self
, PyObject
*args
)
438 return valpy_do_cast (self
, args
, UNOP_CAST
);
441 /* Implementation of the "dynamic_cast" method. */
444 valpy_dynamic_cast (PyObject
*self
, PyObject
*args
)
446 return valpy_do_cast (self
, args
, UNOP_DYNAMIC_CAST
);
449 /* Implementation of the "reinterpret_cast" method. */
452 valpy_reinterpret_cast (PyObject
*self
, PyObject
*args
)
454 return valpy_do_cast (self
, args
, UNOP_REINTERPRET_CAST
);
458 valpy_length (PyObject
*self
)
460 /* We don't support getting the number of elements in a struct / class. */
461 PyErr_SetString (PyExc_NotImplementedError
,
462 _("Invalid operation on gdb.Value."));
466 /* Given string name of an element inside structure, return its value
467 object. Returns NULL on error, with a python exception set. */
469 valpy_getitem (PyObject
*self
, PyObject
*key
)
471 value_object
*self_value
= (value_object
*) self
;
473 volatile struct gdb_exception except
;
474 PyObject
*result
= NULL
;
476 if (gdbpy_is_string (key
))
478 field
= python_string_to_host_string (key
);
483 TRY_CATCH (except
, RETURN_MASK_ALL
)
485 struct value
*tmp
= self_value
->value
;
486 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
487 struct value
*res_val
= NULL
;
490 res_val
= value_struct_elt (&tmp
, NULL
, field
, 0, NULL
);
493 /* Assume we are attempting an array access, and let the
494 value code throw an exception if the index has an invalid
496 struct value
*idx
= convert_value_from_python (key
);
500 /* Check the value's type is something that can be accessed via
504 tmp
= coerce_ref (tmp
);
505 type
= check_typedef (value_type (tmp
));
506 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
507 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
508 error (_("Cannot subscript requested type."));
510 res_val
= value_subscript (tmp
, value_as_long (idx
));
515 result
= value_to_value_object (res_val
);
516 do_cleanups (cleanup
);
520 GDB_PY_HANDLE_EXCEPTION (except
);
526 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
528 PyErr_Format (PyExc_NotImplementedError
,
529 _("Setting of struct elements is not currently supported."));
533 /* Called by the Python interpreter to perform an inferior function
534 call on the value. Returns NULL on error, with a python exception set. */
536 valpy_call (PyObject
*self
, PyObject
*args
, PyObject
*keywords
)
538 Py_ssize_t args_count
;
539 volatile struct gdb_exception except
;
540 struct value
*function
= ((value_object
*) self
)->value
;
541 struct value
**vargs
= NULL
;
542 struct type
*ftype
= NULL
;
543 struct value
*mark
= value_mark ();
544 PyObject
*result
= NULL
;
546 TRY_CATCH (except
, RETURN_MASK_ALL
)
548 ftype
= check_typedef (value_type (function
));
550 GDB_PY_HANDLE_EXCEPTION (except
);
552 if (TYPE_CODE (ftype
) != TYPE_CODE_FUNC
)
554 PyErr_SetString (PyExc_RuntimeError
,
555 _("Value is not callable (not TYPE_CODE_FUNC)."));
559 if (! PyTuple_Check (args
))
561 PyErr_SetString (PyExc_TypeError
,
562 _("Inferior arguments must be provided in a tuple."));
566 args_count
= PyTuple_Size (args
);
571 vargs
= alloca (sizeof (struct value
*) * args_count
);
572 for (i
= 0; i
< args_count
; i
++)
574 PyObject
*item
= PyTuple_GetItem (args
, i
);
579 vargs
[i
] = convert_value_from_python (item
);
580 if (vargs
[i
] == NULL
)
585 TRY_CATCH (except
, RETURN_MASK_ALL
)
587 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (mark
);
588 struct value
*return_value
;
590 return_value
= call_function_by_hand (function
, args_count
, vargs
);
591 result
= value_to_value_object (return_value
);
592 do_cleanups (cleanup
);
594 GDB_PY_HANDLE_EXCEPTION (except
);
599 /* Called by the Python interpreter to obtain string representation
602 valpy_str (PyObject
*self
)
606 struct value_print_options opts
;
607 volatile struct gdb_exception except
;
609 get_user_print_options (&opts
);
612 TRY_CATCH (except
, RETURN_MASK_ALL
)
614 struct ui_file
*stb
= mem_fileopen ();
615 struct cleanup
*old_chain
= make_cleanup_ui_file_delete (stb
);
617 common_val_print (((value_object
*) self
)->value
, stb
, 0,
618 &opts
, python_language
);
619 s
= ui_file_xstrdup (stb
, NULL
);
621 do_cleanups (old_chain
);
623 GDB_PY_HANDLE_EXCEPTION (except
);
625 result
= PyUnicode_Decode (s
, strlen (s
), host_charset (), NULL
);
631 /* Implements gdb.Value.is_optimized_out. */
633 valpy_get_is_optimized_out (PyObject
*self
, void *closure
)
635 struct value
*value
= ((value_object
*) self
)->value
;
637 volatile struct gdb_exception except
;
639 TRY_CATCH (except
, RETURN_MASK_ALL
)
641 opt
= value_optimized_out (value
);
643 GDB_PY_HANDLE_EXCEPTION (except
);
651 /* Implements gdb.Value.is_lazy. */
653 valpy_get_is_lazy (PyObject
*self
, void *closure
)
655 struct value
*value
= ((value_object
*) self
)->value
;
657 volatile struct gdb_exception except
;
659 TRY_CATCH (except
, RETURN_MASK_ALL
)
661 opt
= value_lazy (value
);
663 GDB_PY_HANDLE_EXCEPTION (except
);
671 /* Implements gdb.Value.fetch_lazy (). */
673 valpy_fetch_lazy (PyObject
*self
, PyObject
*args
)
675 struct value
*value
= ((value_object
*) self
)->value
;
676 volatile struct gdb_exception except
;
678 TRY_CATCH (except
, RETURN_MASK_ALL
)
680 if (value_lazy (value
))
681 value_fetch_lazy (value
);
683 GDB_PY_HANDLE_EXCEPTION (except
);
688 /* Calculate and return the address of the PyObject as the value of
689 the builtin __hash__ call. */
691 valpy_hash (PyObject
*self
)
693 return (long) (intptr_t) self
;
711 /* If TYPE is a reference, return the target; otherwise return TYPE. */
712 #define STRIP_REFERENCE(TYPE) \
713 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
715 /* Returns a value object which is the result of applying the operation
716 specified by OPCODE to the given arguments. Returns NULL on error, with
717 a python exception set. */
719 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
721 volatile struct gdb_exception except
;
722 PyObject
*result
= NULL
;
724 TRY_CATCH (except
, RETURN_MASK_ALL
)
726 struct value
*arg1
, *arg2
;
727 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
728 struct value
*res_val
= NULL
;
730 /* If the gdb.Value object is the second operand, then it will be passed
731 to us as the OTHER argument, and SELF will be an entirely different
732 kind of object, altogether. Because of this, we can't assume self is
733 a gdb.Value object and need to convert it from python as well. */
734 arg1
= convert_value_from_python (self
);
738 arg2
= convert_value_from_python (other
);
746 struct type
*ltype
= value_type (arg1
);
747 struct type
*rtype
= value_type (arg2
);
749 CHECK_TYPEDEF (ltype
);
750 ltype
= STRIP_REFERENCE (ltype
);
751 CHECK_TYPEDEF (rtype
);
752 rtype
= STRIP_REFERENCE (rtype
);
754 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
755 && is_integral_type (rtype
))
756 res_val
= value_ptradd (arg1
, value_as_long (arg2
));
757 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
758 && is_integral_type (ltype
))
759 res_val
= value_ptradd (arg2
, value_as_long (arg1
));
761 res_val
= value_binop (arg1
, arg2
, BINOP_ADD
);
766 struct type
*ltype
= value_type (arg1
);
767 struct type
*rtype
= value_type (arg2
);
769 CHECK_TYPEDEF (ltype
);
770 ltype
= STRIP_REFERENCE (ltype
);
771 CHECK_TYPEDEF (rtype
);
772 rtype
= STRIP_REFERENCE (rtype
);
774 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
775 && TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
776 /* A ptrdiff_t for the target would be preferable here. */
777 res_val
= value_from_longest (builtin_type_pyint
,
778 value_ptrdiff (arg1
, arg2
));
779 else if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
780 && is_integral_type (rtype
))
781 res_val
= value_ptradd (arg1
, - value_as_long (arg2
));
783 res_val
= value_binop (arg1
, arg2
, BINOP_SUB
);
787 res_val
= value_binop (arg1
, arg2
, BINOP_MUL
);
790 res_val
= value_binop (arg1
, arg2
, BINOP_DIV
);
793 res_val
= value_binop (arg1
, arg2
, BINOP_REM
);
796 res_val
= value_binop (arg1
, arg2
, BINOP_EXP
);
799 res_val
= value_binop (arg1
, arg2
, BINOP_LSH
);
802 res_val
= value_binop (arg1
, arg2
, BINOP_RSH
);
805 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_AND
);
808 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_IOR
);
811 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_XOR
);
816 result
= value_to_value_object (res_val
);
818 do_cleanups (cleanup
);
820 GDB_PY_HANDLE_EXCEPTION (except
);
826 valpy_add (PyObject
*self
, PyObject
*other
)
828 return valpy_binop (VALPY_ADD
, self
, other
);
832 valpy_subtract (PyObject
*self
, PyObject
*other
)
834 return valpy_binop (VALPY_SUB
, self
, other
);
838 valpy_multiply (PyObject
*self
, PyObject
*other
)
840 return valpy_binop (VALPY_MUL
, self
, other
);
844 valpy_divide (PyObject
*self
, PyObject
*other
)
846 return valpy_binop (VALPY_DIV
, self
, other
);
850 valpy_remainder (PyObject
*self
, PyObject
*other
)
852 return valpy_binop (VALPY_REM
, self
, other
);
856 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
858 /* We don't support the ternary form of pow. I don't know how to express
859 that, so let's just throw NotImplementedError to at least do something
861 if (unused
!= Py_None
)
863 PyErr_SetString (PyExc_NotImplementedError
,
864 "Invalid operation on gdb.Value.");
868 return valpy_binop (VALPY_POW
, self
, other
);
872 valpy_negative (PyObject
*self
)
874 volatile struct gdb_exception except
;
875 PyObject
*result
= NULL
;
877 TRY_CATCH (except
, RETURN_MASK_ALL
)
879 /* Perhaps overkill, but consistency has some virtue. */
880 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
883 val
= value_neg (((value_object
*) self
)->value
);
884 result
= value_to_value_object (val
);
885 do_cleanups (cleanup
);
887 GDB_PY_HANDLE_EXCEPTION (except
);
893 valpy_positive (PyObject
*self
)
895 return value_to_value_object (((value_object
*) self
)->value
);
899 valpy_absolute (PyObject
*self
)
901 struct value
*value
= ((value_object
*) self
)->value
;
902 volatile struct gdb_exception except
;
905 TRY_CATCH (except
, RETURN_MASK_ALL
)
907 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
909 if (value_less (value
, value_zero (value_type (value
), not_lval
)))
912 do_cleanups (cleanup
);
914 GDB_PY_HANDLE_EXCEPTION (except
);
917 return valpy_positive (self
);
919 return valpy_negative (self
);
922 /* Implements boolean evaluation of gdb.Value. */
924 valpy_nonzero (PyObject
*self
)
926 volatile struct gdb_exception except
;
927 value_object
*self_value
= (value_object
*) self
;
929 int nonzero
= 0; /* Appease GCC warning. */
931 TRY_CATCH (except
, RETURN_MASK_ALL
)
933 type
= check_typedef (value_type (self_value
->value
));
935 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
936 nonzero
= !!value_as_long (self_value
->value
);
937 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
938 nonzero
= value_as_double (self_value
->value
) != 0;
939 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
940 nonzero
= !decimal_is_zero (value_contents (self_value
->value
),
942 gdbarch_byte_order (get_type_arch (type
)));
944 /* All other values are True. */
947 /* This is not documented in the Python documentation, but if this
948 function fails, return -1 as slot_nb_nonzero does (the default
949 Python nonzero function). */
950 GDB_PY_SET_HANDLE_EXCEPTION (except
);
955 /* Implements ~ for value objects. */
957 valpy_invert (PyObject
*self
)
959 struct value
*val
= NULL
;
960 volatile struct gdb_exception except
;
962 TRY_CATCH (except
, RETURN_MASK_ALL
)
964 val
= value_complement (((value_object
*) self
)->value
);
966 GDB_PY_HANDLE_EXCEPTION (except
);
968 return value_to_value_object (val
);
971 /* Implements left shift for value objects. */
973 valpy_lsh (PyObject
*self
, PyObject
*other
)
975 return valpy_binop (VALPY_LSH
, self
, other
);
978 /* Implements right shift for value objects. */
980 valpy_rsh (PyObject
*self
, PyObject
*other
)
982 return valpy_binop (VALPY_RSH
, self
, other
);
985 /* Implements bitwise and for value objects. */
987 valpy_and (PyObject
*self
, PyObject
*other
)
989 return valpy_binop (VALPY_BITAND
, self
, other
);
992 /* Implements bitwise or for value objects. */
994 valpy_or (PyObject
*self
, PyObject
*other
)
996 return valpy_binop (VALPY_BITOR
, self
, other
);
999 /* Implements bitwise xor for value objects. */
1001 valpy_xor (PyObject
*self
, PyObject
*other
)
1003 return valpy_binop (VALPY_BITXOR
, self
, other
);
1006 /* Implements comparison operations for value objects. Returns NULL on error,
1007 with a python exception set. */
1009 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
1012 volatile struct gdb_exception except
;
1014 if (other
== Py_None
)
1015 /* Comparing with None is special. From what I can tell, in Python
1016 None is smaller than anything else. */
1028 PyErr_SetString (PyExc_NotImplementedError
,
1029 _("Invalid operation on gdb.Value."));
1033 TRY_CATCH (except
, RETURN_MASK_ALL
)
1035 struct value
*value_other
, *mark
= value_mark ();
1036 struct cleanup
*cleanup
;
1038 value_other
= convert_value_from_python (other
);
1039 if (value_other
== NULL
)
1045 cleanup
= make_cleanup_value_free_to_mark (mark
);
1049 result
= value_less (((value_object
*) self
)->value
, value_other
);
1052 result
= value_less (((value_object
*) self
)->value
, value_other
)
1053 || value_equal (((value_object
*) self
)->value
, value_other
);
1056 result
= value_equal (((value_object
*) self
)->value
, value_other
);
1059 result
= !value_equal (((value_object
*) self
)->value
, value_other
);
1062 result
= value_less (value_other
, ((value_object
*) self
)->value
);
1065 result
= value_less (value_other
, ((value_object
*) self
)->value
)
1066 || value_equal (((value_object
*) self
)->value
, value_other
);
1070 PyErr_SetString (PyExc_NotImplementedError
,
1071 _("Invalid operation on gdb.Value."));
1076 do_cleanups (cleanup
);
1078 GDB_PY_HANDLE_EXCEPTION (except
);
1080 /* In this case, the Python exception has already been set. */
1090 /* Helper function to determine if a type is "int-like". */
1092 is_intlike (struct type
*type
, int ptr_ok
)
1094 return (TYPE_CODE (type
) == TYPE_CODE_INT
1095 || TYPE_CODE (type
) == TYPE_CODE_ENUM
1096 || TYPE_CODE (type
) == TYPE_CODE_BOOL
1097 || TYPE_CODE (type
) == TYPE_CODE_CHAR
1098 || (ptr_ok
&& TYPE_CODE (type
) == TYPE_CODE_PTR
));
1101 /* Implements conversion to int. */
1103 valpy_int (PyObject
*self
)
1105 struct value
*value
= ((value_object
*) self
)->value
;
1106 struct type
*type
= value_type (value
);
1108 volatile struct gdb_exception except
;
1110 TRY_CATCH (except
, RETURN_MASK_ALL
)
1112 CHECK_TYPEDEF (type
);
1113 if (!is_intlike (type
, 0))
1114 error (_("Cannot convert value to int."));
1116 l
= value_as_long (value
);
1118 GDB_PY_HANDLE_EXCEPTION (except
);
1120 return gdb_py_object_from_longest (l
);
1123 /* Implements conversion to long. */
1125 valpy_long (PyObject
*self
)
1127 struct value
*value
= ((value_object
*) self
)->value
;
1128 struct type
*type
= value_type (value
);
1130 volatile struct gdb_exception except
;
1132 TRY_CATCH (except
, RETURN_MASK_ALL
)
1134 CHECK_TYPEDEF (type
);
1136 if (!is_intlike (type
, 1))
1137 error (_("Cannot convert value to long."));
1139 l
= value_as_long (value
);
1141 GDB_PY_HANDLE_EXCEPTION (except
);
1143 return gdb_py_long_from_longest (l
);
1146 /* Implements conversion to float. */
1148 valpy_float (PyObject
*self
)
1150 struct value
*value
= ((value_object
*) self
)->value
;
1151 struct type
*type
= value_type (value
);
1153 volatile struct gdb_exception except
;
1155 TRY_CATCH (except
, RETURN_MASK_ALL
)
1157 CHECK_TYPEDEF (type
);
1159 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
1160 error (_("Cannot convert value to float."));
1162 d
= value_as_double (value
);
1164 GDB_PY_HANDLE_EXCEPTION (except
);
1166 return PyFloat_FromDouble (d
);
1169 /* Returns an object for a value which is released from the all_values chain,
1170 so its lifetime is not bound to the execution of a command. */
1172 value_to_value_object (struct value
*val
)
1174 value_object
*val_obj
;
1176 val_obj
= PyObject_New (value_object
, &value_object_type
);
1177 if (val_obj
!= NULL
)
1179 val_obj
->value
= val
;
1180 release_value_or_incref (val
);
1181 val_obj
->address
= NULL
;
1182 val_obj
->type
= NULL
;
1183 val_obj
->dynamic_type
= NULL
;
1184 note_value (val_obj
);
1187 return (PyObject
*) val_obj
;
1190 /* Returns a borrowed reference to the struct value corresponding to
1191 the given value object. */
1193 value_object_to_value (PyObject
*self
)
1197 if (! PyObject_TypeCheck (self
, &value_object_type
))
1199 real
= (value_object
*) self
;
1203 /* Try to convert a Python value to a gdb value. If the value cannot
1204 be converted, set a Python exception and return NULL. Returns a
1205 reference to a new value on the all_values chain. */
1208 convert_value_from_python (PyObject
*obj
)
1210 struct value
*value
= NULL
; /* -Wall */
1211 struct cleanup
*old
;
1212 volatile struct gdb_exception except
;
1215 gdb_assert (obj
!= NULL
);
1217 TRY_CATCH (except
, RETURN_MASK_ALL
)
1219 if (PyBool_Check (obj
))
1221 cmp
= PyObject_IsTrue (obj
);
1223 value
= value_from_longest (builtin_type_pybool
, cmp
);
1225 else if (PyInt_Check (obj
))
1227 long l
= PyInt_AsLong (obj
);
1229 if (! PyErr_Occurred ())
1230 value
= value_from_longest (builtin_type_pyint
, l
);
1232 else if (PyLong_Check (obj
))
1234 LONGEST l
= PyLong_AsLongLong (obj
);
1236 if (PyErr_Occurred ())
1238 /* If the error was an overflow, we can try converting to
1239 ULONGEST instead. */
1240 if (PyErr_ExceptionMatches (PyExc_OverflowError
))
1242 PyObject
*etype
, *evalue
, *etraceback
, *zero
;
1244 PyErr_Fetch (&etype
, &evalue
, &etraceback
);
1245 zero
= PyInt_FromLong (0);
1247 /* Check whether obj is positive. */
1248 if (PyObject_RichCompareBool (obj
, zero
, Py_GT
) > 0)
1252 ul
= PyLong_AsUnsignedLongLong (obj
);
1253 if (! PyErr_Occurred ())
1254 value
= value_from_ulongest (builtin_type_upylong
, ul
);
1257 /* There's nothing we can do. */
1258 PyErr_Restore (etype
, evalue
, etraceback
);
1264 value
= value_from_longest (builtin_type_pylong
, l
);
1266 else if (PyFloat_Check (obj
))
1268 double d
= PyFloat_AsDouble (obj
);
1270 if (! PyErr_Occurred ())
1271 value
= value_from_double (builtin_type_pyfloat
, d
);
1273 else if (gdbpy_is_string (obj
))
1277 s
= python_string_to_target_string (obj
);
1280 old
= make_cleanup (xfree
, s
);
1281 value
= value_cstring (s
, strlen (s
), builtin_type_pychar
);
1285 else if (PyObject_TypeCheck (obj
, &value_object_type
))
1286 value
= value_copy (((value_object
*) obj
)->value
);
1287 else if (gdbpy_is_lazy_string (obj
))
1291 result
= PyObject_CallMethodObjArgs (obj
, gdbpy_value_cst
, NULL
);
1292 value
= value_copy (((value_object
*) result
)->value
);
1295 PyErr_Format (PyExc_TypeError
,
1296 _("Could not convert Python object: %s."),
1297 PyString_AsString (PyObject_Str (obj
)));
1299 if (except
.reason
< 0)
1301 PyErr_Format (except
.reason
== RETURN_QUIT
1302 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
1303 "%s", except
.message
);
1310 /* Returns value object in the ARGth position in GDB's history. */
1312 gdbpy_history (PyObject
*self
, PyObject
*args
)
1315 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
1316 volatile struct gdb_exception except
;
1318 if (!PyArg_ParseTuple (args
, "i", &i
))
1321 TRY_CATCH (except
, RETURN_MASK_ALL
)
1323 res_val
= access_value_history (i
);
1325 GDB_PY_HANDLE_EXCEPTION (except
);
1327 return value_to_value_object (res_val
);
1330 /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
1333 gdbpy_is_value_object (PyObject
*obj
)
1335 return PyObject_TypeCheck (obj
, &value_object_type
);
1339 gdbpy_initialize_values (void)
1341 if (PyType_Ready (&value_object_type
) < 0)
1344 Py_INCREF (&value_object_type
);
1345 PyModule_AddObject (gdb_module
, "Value", (PyObject
*) &value_object_type
);
1347 values_in_python
= NULL
;
1352 static PyGetSetDef value_object_getset
[] = {
1353 { "address", valpy_get_address
, NULL
, "The address of the value.",
1355 { "is_optimized_out", valpy_get_is_optimized_out
, NULL
,
1356 "Boolean telling whether the value is optimized "
1357 "out (i.e., not available).",
1359 { "type", valpy_get_type
, NULL
, "Type of the value.", NULL
},
1360 { "dynamic_type", valpy_get_dynamic_type
, NULL
,
1361 "Dynamic type of the value.", NULL
},
1362 { "is_lazy", valpy_get_is_lazy
, NULL
,
1363 "Boolean telling whether the value is lazy (not fetched yet\n\
1364 from the inferior). A lazy value is fetched when needed, or when\n\
1365 the \"fetch_lazy()\" method is called.", NULL
},
1366 {NULL
} /* Sentinel */
1369 static PyMethodDef value_object_methods
[] = {
1370 { "cast", valpy_cast
, METH_VARARGS
, "Cast the value to the supplied type." },
1371 { "dynamic_cast", valpy_dynamic_cast
, METH_VARARGS
,
1372 "dynamic_cast (gdb.Type) -> gdb.Value\n\
1373 Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
1375 { "reinterpret_cast", valpy_reinterpret_cast
, METH_VARARGS
,
1376 "reinterpret_cast (gdb.Type) -> gdb.Value\n\
1377 Cast the value to the supplied type, as if by the C++\n\
1378 reinterpret_cast operator."
1380 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
1381 { "lazy_string", (PyCFunction
) valpy_lazy_string
,
1382 METH_VARARGS
| METH_KEYWORDS
,
1383 "lazy_string ([encoding] [, length]) -> lazy_string\n\
1384 Return a lazy string representation of the value." },
1385 { "string", (PyCFunction
) valpy_string
, METH_VARARGS
| METH_KEYWORDS
,
1386 "string ([encoding] [, errors] [, length]) -> string\n\
1387 Return Unicode string representation of the value." },
1388 { "fetch_lazy", valpy_fetch_lazy
, METH_NOARGS
,
1389 "Fetches the value from the inferior, if it was lazy." },
1390 {NULL
} /* Sentinel */
1393 static PyNumberMethods value_object_as_number
= {
1399 NULL
, /* nb_divmod */
1400 valpy_power
, /* nb_power */
1401 valpy_negative
, /* nb_negative */
1402 valpy_positive
, /* nb_positive */
1403 valpy_absolute
, /* nb_absolute */
1404 valpy_nonzero
, /* nb_nonzero */
1405 valpy_invert
, /* nb_invert */
1406 valpy_lsh
, /* nb_lshift */
1407 valpy_rsh
, /* nb_rshift */
1408 valpy_and
, /* nb_and */
1409 valpy_xor
, /* nb_xor */
1410 valpy_or
, /* nb_or */
1411 NULL
, /* nb_coerce */
1412 valpy_int
, /* nb_int */
1413 valpy_long
, /* nb_long */
1414 valpy_float
, /* nb_float */
1419 static PyMappingMethods value_object_as_mapping
= {
1425 PyTypeObject value_object_type
= {
1426 PyObject_HEAD_INIT (NULL
)
1428 "gdb.Value", /*tp_name*/
1429 sizeof (value_object
), /*tp_basicsize*/
1431 valpy_dealloc
, /*tp_dealloc*/
1437 &value_object_as_number
, /*tp_as_number*/
1438 0, /*tp_as_sequence*/
1439 &value_object_as_mapping
, /*tp_as_mapping*/
1440 valpy_hash
, /*tp_hash*/
1441 valpy_call
, /*tp_call*/
1442 valpy_str
, /*tp_str*/
1446 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
1447 | Py_TPFLAGS_BASETYPE
, /*tp_flags*/
1448 "GDB value object", /* tp_doc */
1449 0, /* tp_traverse */
1451 valpy_richcompare
, /* tp_richcompare */
1452 0, /* tp_weaklistoffset */
1454 0, /* tp_iternext */
1455 value_object_methods
, /* tp_methods */
1457 value_object_getset
, /* tp_getset */
1460 0, /* tp_descr_get */
1461 0, /* tp_descr_set */
1462 0, /* tp_dictoffset */
1465 valpy_new
/* tp_new */
1471 preserve_python_values (struct objfile
*objfile
, htab_t copied_types
)
1476 #endif /* HAVE_PYTHON */