1 /* Python interface to values.
3 Copyright (C) 2008, 2009, 2010, 2011 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 value_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 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
178 volatile struct gdb_exception except
;
180 TRY_CATCH (except
, RETURN_MASK_ALL
)
182 res_val
= value_ind (((value_object
*) self
)->value
);
184 GDB_PY_HANDLE_EXCEPTION (except
);
186 return value_to_value_object (res_val
);
189 /* Return "&value". */
191 valpy_get_address (PyObject
*self
, void *closure
)
193 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
194 value_object
*val_obj
= (value_object
*) self
;
195 volatile struct gdb_exception except
;
197 if (!val_obj
->address
)
199 TRY_CATCH (except
, RETURN_MASK_ALL
)
201 res_val
= value_addr (val_obj
->value
);
203 if (except
.reason
< 0)
205 val_obj
->address
= Py_None
;
209 val_obj
->address
= value_to_value_object (res_val
);
212 Py_XINCREF (val_obj
->address
);
214 return val_obj
->address
;
217 /* Return type of the value. */
219 valpy_get_type (PyObject
*self
, void *closure
)
221 value_object
*obj
= (value_object
*) self
;
225 obj
->type
= type_to_type_object (value_type (obj
->value
));
229 Py_INCREF (obj
->type
);
233 /* Return dynamic type of the value. */
236 valpy_get_dynamic_type (PyObject
*self
, void *closure
)
238 value_object
*obj
= (value_object
*) self
;
239 volatile struct gdb_exception except
;
240 struct type
*type
= NULL
;
242 if (obj
->dynamic_type
!= NULL
)
244 Py_INCREF (obj
->dynamic_type
);
245 return obj
->dynamic_type
;
248 TRY_CATCH (except
, RETURN_MASK_ALL
)
250 struct value
*val
= obj
->value
;
252 type
= value_type (val
);
253 CHECK_TYPEDEF (type
);
255 if (((TYPE_CODE (type
) == TYPE_CODE_PTR
)
256 || (TYPE_CODE (type
) == TYPE_CODE_REF
))
257 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_CLASS
))
259 struct value
*target
;
260 int was_pointer
= TYPE_CODE (type
) == TYPE_CODE_PTR
;
262 target
= value_ind (val
);
263 type
= value_rtti_type (target
, NULL
, NULL
, NULL
);
268 type
= lookup_pointer_type (type
);
270 type
= lookup_reference_type (type
);
273 else if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
274 type
= value_rtti_type (val
, NULL
, NULL
, NULL
);
277 /* Re-use object's static type. */
281 GDB_PY_HANDLE_EXCEPTION (except
);
285 /* Ensure that the TYPE field is ready. */
286 if (!valpy_get_type (self
, NULL
))
288 /* We don't need to incref here, because valpy_get_type already
290 obj
->dynamic_type
= obj
->type
;
293 obj
->dynamic_type
= type_to_type_object (type
);
295 Py_INCREF (obj
->dynamic_type
);
296 return obj
->dynamic_type
;
299 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
300 string. Return a PyObject representing a lazy_string_object type.
301 A lazy string is a pointer to a string with an optional encoding and
302 length. If ENCODING is not given, encoding is set to None. If an
303 ENCODING is provided the encoding parameter is set to ENCODING, but
304 the string is not encoded. If LENGTH is provided then the length
305 parameter is set to LENGTH, otherwise length will be set to -1 (first
306 null of appropriate with). */
308 valpy_lazy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
310 gdb_py_longest length
= -1;
311 struct value
*value
= ((value_object
*) self
)->value
;
312 const char *user_encoding
= NULL
;
313 static char *keywords
[] = { "encoding", "length", NULL
};
315 volatile struct gdb_exception except
;
317 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|s" GDB_PY_LL_ARG
, keywords
,
318 &user_encoding
, &length
))
321 TRY_CATCH (except
, RETURN_MASK_ALL
)
323 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_PTR
)
324 value
= value_ind (value
);
326 GDB_PY_HANDLE_EXCEPTION (except
);
328 str_obj
= gdbpy_create_lazy_string_object (value_address (value
), length
,
332 return (PyObject
*) str_obj
;
335 /* Implementation of gdb.Value.string ([encoding] [, errors]
336 [, length]) -> string. Return Unicode string with value contents.
337 If ENCODING is not given, the string is assumed to be encoded in
338 the target's charset. If LENGTH is provided, only fetch string to
339 the length provided. */
342 valpy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
346 struct value
*value
= ((value_object
*) self
)->value
;
347 volatile struct gdb_exception except
;
349 const char *encoding
= NULL
;
350 const char *errors
= NULL
;
351 const char *user_encoding
= NULL
;
352 const char *la_encoding
= NULL
;
353 struct type
*char_type
;
354 static char *keywords
[] = { "encoding", "errors", "length", NULL
};
356 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|ssi", keywords
,
357 &user_encoding
, &errors
, &length
))
360 TRY_CATCH (except
, RETURN_MASK_ALL
)
362 LA_GET_STRING (value
, &buffer
, &length
, &char_type
, &la_encoding
);
364 GDB_PY_HANDLE_EXCEPTION (except
);
366 encoding
= (user_encoding
&& *user_encoding
) ? user_encoding
: la_encoding
;
367 unicode
= PyUnicode_Decode (buffer
, length
* TYPE_LENGTH (char_type
),
374 /* A helper function that implements the various cast operators. */
377 valpy_do_cast (PyObject
*self
, PyObject
*args
, enum exp_opcode op
)
381 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
382 volatile struct gdb_exception except
;
384 if (! PyArg_ParseTuple (args
, "O", &type_obj
))
387 type
= type_object_to_type (type_obj
);
390 PyErr_SetString (PyExc_RuntimeError
,
391 _("Argument must be a type."));
395 TRY_CATCH (except
, RETURN_MASK_ALL
)
397 struct value
*val
= ((value_object
*) self
)->value
;
399 if (op
== UNOP_DYNAMIC_CAST
)
400 res_val
= value_dynamic_cast (type
, val
);
401 else if (op
== UNOP_REINTERPRET_CAST
)
402 res_val
= value_reinterpret_cast (type
, val
);
405 gdb_assert (op
== UNOP_CAST
);
406 res_val
= value_cast (type
, val
);
409 GDB_PY_HANDLE_EXCEPTION (except
);
411 return value_to_value_object (res_val
);
414 /* Implementation of the "cast" method. */
417 valpy_cast (PyObject
*self
, PyObject
*args
)
419 return valpy_do_cast (self
, args
, UNOP_CAST
);
422 /* Implementation of the "dynamic_cast" method. */
425 valpy_dynamic_cast (PyObject
*self
, PyObject
*args
)
427 return valpy_do_cast (self
, args
, UNOP_DYNAMIC_CAST
);
430 /* Implementation of the "reinterpret_cast" method. */
433 valpy_reinterpret_cast (PyObject
*self
, PyObject
*args
)
435 return valpy_do_cast (self
, args
, UNOP_REINTERPRET_CAST
);
439 valpy_length (PyObject
*self
)
441 /* We don't support getting the number of elements in a struct / class. */
442 PyErr_SetString (PyExc_NotImplementedError
,
443 _("Invalid operation on gdb.Value."));
447 /* Given string name of an element inside structure, return its value
448 object. Returns NULL on error, with a python exception set. */
450 valpy_getitem (PyObject
*self
, PyObject
*key
)
452 value_object
*self_value
= (value_object
*) self
;
454 struct value
*res_val
= NULL
;
455 volatile struct gdb_exception except
;
457 if (gdbpy_is_string (key
))
459 field
= python_string_to_host_string (key
);
464 TRY_CATCH (except
, RETURN_MASK_ALL
)
466 struct value
*tmp
= self_value
->value
;
469 res_val
= value_struct_elt (&tmp
, NULL
, field
, 0, NULL
);
472 /* Assume we are attempting an array access, and let the
473 value code throw an exception if the index has an invalid
475 struct value
*idx
= convert_value_from_python (key
);
479 /* Check the value's type is something that can be accessed via
483 tmp
= coerce_ref (tmp
);
484 type
= check_typedef (value_type (tmp
));
485 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
486 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
487 error (_("Cannot subscript requested type."));
489 res_val
= value_subscript (tmp
, value_as_long (idx
));
495 GDB_PY_HANDLE_EXCEPTION (except
);
497 return res_val
? value_to_value_object (res_val
) : NULL
;
501 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
503 PyErr_Format (PyExc_NotImplementedError
,
504 _("Setting of struct elements is not currently supported."));
508 /* Called by the Python interpreter to perform an inferior function
509 call on the value. Returns NULL on error, with a python exception set. */
511 valpy_call (PyObject
*self
, PyObject
*args
, PyObject
*keywords
)
513 struct value
*return_value
= NULL
;
514 Py_ssize_t args_count
;
515 volatile struct gdb_exception except
;
516 struct value
*function
= ((value_object
*) self
)->value
;
517 struct value
**vargs
= NULL
;
520 TRY_CATCH (except
, RETURN_MASK_ALL
)
522 ftype
= check_typedef (value_type (function
));
524 GDB_PY_HANDLE_EXCEPTION (except
);
526 if (TYPE_CODE (ftype
) != TYPE_CODE_FUNC
)
528 PyErr_SetString (PyExc_RuntimeError
,
529 _("Value is not callable (not TYPE_CODE_FUNC)."));
533 if (! PyTuple_Check (args
))
535 PyErr_SetString (PyExc_TypeError
,
536 _("Inferior arguments must be provided in a tuple."));
540 args_count
= PyTuple_Size (args
);
545 vargs
= alloca (sizeof (struct value
*) * args_count
);
546 for (i
= 0; i
< args_count
; i
++)
548 PyObject
*item
= PyTuple_GetItem (args
, i
);
553 vargs
[i
] = convert_value_from_python (item
);
554 if (vargs
[i
] == NULL
)
559 TRY_CATCH (except
, RETURN_MASK_ALL
)
561 return_value
= call_function_by_hand (function
, args_count
, vargs
);
563 GDB_PY_HANDLE_EXCEPTION (except
);
565 return value_to_value_object (return_value
);
568 /* Called by the Python interpreter to obtain string representation
571 valpy_str (PyObject
*self
)
575 struct value_print_options opts
;
576 volatile struct gdb_exception except
;
578 get_user_print_options (&opts
);
581 TRY_CATCH (except
, RETURN_MASK_ALL
)
583 struct ui_file
*stb
= mem_fileopen ();
584 struct cleanup
*old_chain
= make_cleanup_ui_file_delete (stb
);
586 common_val_print (((value_object
*) self
)->value
, stb
, 0,
587 &opts
, python_language
);
588 s
= ui_file_xstrdup (stb
, NULL
);
590 do_cleanups (old_chain
);
592 GDB_PY_HANDLE_EXCEPTION (except
);
594 result
= PyUnicode_Decode (s
, strlen (s
), host_charset (), NULL
);
600 /* Implements gdb.Value.is_optimized_out. */
602 valpy_get_is_optimized_out (PyObject
*self
, void *closure
)
604 struct value
*value
= ((value_object
*) self
)->value
;
606 volatile struct gdb_exception except
;
608 TRY_CATCH (except
, RETURN_MASK_ALL
)
610 opt
= value_optimized_out (value
);
612 GDB_PY_HANDLE_EXCEPTION (except
);
620 /* Calculate and return the address of the PyObject as the value of
621 the builtin __hash__ call. */
623 valpy_hash (PyObject
*self
)
625 return (long) (intptr_t) self
;
643 /* If TYPE is a reference, return the target; otherwise return TYPE. */
644 #define STRIP_REFERENCE(TYPE) \
645 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
647 /* Returns a value object which is the result of applying the operation
648 specified by OPCODE to the given arguments. Returns NULL on error, with
649 a python exception set. */
651 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
653 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
654 volatile struct gdb_exception except
;
656 TRY_CATCH (except
, RETURN_MASK_ALL
)
658 struct value
*arg1
, *arg2
;
660 /* If the gdb.Value object is the second operand, then it will be passed
661 to us as the OTHER argument, and SELF will be an entirely different
662 kind of object, altogether. Because of this, we can't assume self is
663 a gdb.Value object and need to convert it from python as well. */
664 arg1
= convert_value_from_python (self
);
668 arg2
= convert_value_from_python (other
);
676 struct type
*ltype
= value_type (arg1
);
677 struct type
*rtype
= value_type (arg2
);
679 CHECK_TYPEDEF (ltype
);
680 ltype
= STRIP_REFERENCE (ltype
);
681 CHECK_TYPEDEF (rtype
);
682 rtype
= STRIP_REFERENCE (rtype
);
684 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
685 && is_integral_type (rtype
))
686 res_val
= value_ptradd (arg1
, value_as_long (arg2
));
687 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
688 && is_integral_type (ltype
))
689 res_val
= value_ptradd (arg2
, value_as_long (arg1
));
691 res_val
= value_binop (arg1
, arg2
, BINOP_ADD
);
696 struct type
*ltype
= value_type (arg1
);
697 struct type
*rtype
= value_type (arg2
);
699 CHECK_TYPEDEF (ltype
);
700 ltype
= STRIP_REFERENCE (ltype
);
701 CHECK_TYPEDEF (rtype
);
702 rtype
= STRIP_REFERENCE (rtype
);
704 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
705 && TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
706 /* A ptrdiff_t for the target would be preferable here. */
707 res_val
= value_from_longest (builtin_type_pyint
,
708 value_ptrdiff (arg1
, arg2
));
709 else if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
710 && is_integral_type (rtype
))
711 res_val
= value_ptradd (arg1
, - value_as_long (arg2
));
713 res_val
= value_binop (arg1
, arg2
, BINOP_SUB
);
717 res_val
= value_binop (arg1
, arg2
, BINOP_MUL
);
720 res_val
= value_binop (arg1
, arg2
, BINOP_DIV
);
723 res_val
= value_binop (arg1
, arg2
, BINOP_REM
);
726 res_val
= value_binop (arg1
, arg2
, BINOP_EXP
);
729 res_val
= value_binop (arg1
, arg2
, BINOP_LSH
);
732 res_val
= value_binop (arg1
, arg2
, BINOP_RSH
);
735 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_AND
);
738 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_IOR
);
741 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_XOR
);
745 GDB_PY_HANDLE_EXCEPTION (except
);
747 return res_val
? value_to_value_object (res_val
) : NULL
;
751 valpy_add (PyObject
*self
, PyObject
*other
)
753 return valpy_binop (VALPY_ADD
, self
, other
);
757 valpy_subtract (PyObject
*self
, PyObject
*other
)
759 return valpy_binop (VALPY_SUB
, self
, other
);
763 valpy_multiply (PyObject
*self
, PyObject
*other
)
765 return valpy_binop (VALPY_MUL
, self
, other
);
769 valpy_divide (PyObject
*self
, PyObject
*other
)
771 return valpy_binop (VALPY_DIV
, self
, other
);
775 valpy_remainder (PyObject
*self
, PyObject
*other
)
777 return valpy_binop (VALPY_REM
, self
, other
);
781 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
783 /* We don't support the ternary form of pow. I don't know how to express
784 that, so let's just throw NotImplementedError to at least do something
786 if (unused
!= Py_None
)
788 PyErr_SetString (PyExc_NotImplementedError
,
789 "Invalid operation on gdb.Value.");
793 return valpy_binop (VALPY_POW
, self
, other
);
797 valpy_negative (PyObject
*self
)
799 struct value
*val
= NULL
;
800 volatile struct gdb_exception except
;
802 TRY_CATCH (except
, RETURN_MASK_ALL
)
804 val
= value_neg (((value_object
*) self
)->value
);
806 GDB_PY_HANDLE_EXCEPTION (except
);
808 return value_to_value_object (val
);
812 valpy_positive (PyObject
*self
)
814 return value_to_value_object (((value_object
*) self
)->value
);
818 valpy_absolute (PyObject
*self
)
820 struct value
*value
= ((value_object
*) self
)->value
;
821 volatile struct gdb_exception except
;
824 TRY_CATCH (except
, RETURN_MASK_ALL
)
826 if (value_less (value
, value_zero (value_type (value
), not_lval
)))
829 GDB_PY_HANDLE_EXCEPTION (except
);
832 return valpy_positive (self
);
834 return valpy_negative (self
);
837 /* Implements boolean evaluation of gdb.Value. */
839 valpy_nonzero (PyObject
*self
)
841 volatile struct gdb_exception except
;
842 value_object
*self_value
= (value_object
*) self
;
844 int nonzero
= 0; /* Appease GCC warning. */
846 type
= check_typedef (value_type (self_value
->value
));
848 TRY_CATCH (except
, RETURN_MASK_ALL
)
850 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
851 nonzero
= !!value_as_long (self_value
->value
);
852 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
853 nonzero
= value_as_double (self_value
->value
) != 0;
854 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
855 nonzero
= !decimal_is_zero (value_contents (self_value
->value
),
857 gdbarch_byte_order (get_type_arch (type
)));
859 /* All other values are True. */
862 /* This is not documented in the Python documentation, but if this
863 function fails, return -1 as slot_nb_nonzero does (the default
864 Python nonzero function). */
865 GDB_PY_SET_HANDLE_EXCEPTION (except
);
870 /* Implements ~ for value objects. */
872 valpy_invert (PyObject
*self
)
874 struct value
*val
= NULL
;
875 volatile struct gdb_exception except
;
877 TRY_CATCH (except
, RETURN_MASK_ALL
)
879 val
= value_complement (((value_object
*) self
)->value
);
881 GDB_PY_HANDLE_EXCEPTION (except
);
883 return value_to_value_object (val
);
886 /* Implements left shift for value objects. */
888 valpy_lsh (PyObject
*self
, PyObject
*other
)
890 return valpy_binop (VALPY_LSH
, self
, other
);
893 /* Implements right shift for value objects. */
895 valpy_rsh (PyObject
*self
, PyObject
*other
)
897 return valpy_binop (VALPY_RSH
, self
, other
);
900 /* Implements bitwise and for value objects. */
902 valpy_and (PyObject
*self
, PyObject
*other
)
904 return valpy_binop (VALPY_BITAND
, self
, other
);
907 /* Implements bitwise or for value objects. */
909 valpy_or (PyObject
*self
, PyObject
*other
)
911 return valpy_binop (VALPY_BITOR
, self
, other
);
914 /* Implements bitwise xor for value objects. */
916 valpy_xor (PyObject
*self
, PyObject
*other
)
918 return valpy_binop (VALPY_BITXOR
, self
, other
);
921 /* Implements comparison operations for value objects. Returns NULL on error,
922 with a python exception set. */
924 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
927 struct value
*value_other
;
928 volatile struct gdb_exception except
;
930 if (other
== Py_None
)
931 /* Comparing with None is special. From what I can tell, in Python
932 None is smaller than anything else. */
944 PyErr_SetString (PyExc_NotImplementedError
,
945 _("Invalid operation on gdb.Value."));
949 TRY_CATCH (except
, RETURN_MASK_ALL
)
951 value_other
= convert_value_from_python (other
);
952 if (value_other
== NULL
)
960 result
= value_less (((value_object
*) self
)->value
, value_other
);
963 result
= value_less (((value_object
*) self
)->value
, value_other
)
964 || value_equal (((value_object
*) self
)->value
, value_other
);
967 result
= value_equal (((value_object
*) self
)->value
, value_other
);
970 result
= !value_equal (((value_object
*) self
)->value
, value_other
);
973 result
= value_less (value_other
, ((value_object
*) self
)->value
);
976 result
= value_less (value_other
, ((value_object
*) self
)->value
)
977 || value_equal (((value_object
*) self
)->value
, value_other
);
981 PyErr_SetString (PyExc_NotImplementedError
,
982 _("Invalid operation on gdb.Value."));
987 GDB_PY_HANDLE_EXCEPTION (except
);
989 /* In this case, the Python exception has already been set. */
999 /* Helper function to determine if a type is "int-like". */
1001 is_intlike (struct type
*type
, int ptr_ok
)
1003 return (TYPE_CODE (type
) == TYPE_CODE_INT
1004 || TYPE_CODE (type
) == TYPE_CODE_ENUM
1005 || TYPE_CODE (type
) == TYPE_CODE_BOOL
1006 || TYPE_CODE (type
) == TYPE_CODE_CHAR
1007 || (ptr_ok
&& TYPE_CODE (type
) == TYPE_CODE_PTR
));
1010 /* Implements conversion to int. */
1012 valpy_int (PyObject
*self
)
1014 struct value
*value
= ((value_object
*) self
)->value
;
1015 struct type
*type
= value_type (value
);
1017 volatile struct gdb_exception except
;
1019 TRY_CATCH (except
, RETURN_MASK_ALL
)
1021 CHECK_TYPEDEF (type
);
1022 if (!is_intlike (type
, 0))
1023 error (_("Cannot convert value to int."));
1025 l
= value_as_long (value
);
1027 GDB_PY_HANDLE_EXCEPTION (except
);
1029 return gdb_py_object_from_longest (l
);
1032 /* Implements conversion to long. */
1034 valpy_long (PyObject
*self
)
1036 struct value
*value
= ((value_object
*) self
)->value
;
1037 struct type
*type
= value_type (value
);
1039 volatile struct gdb_exception except
;
1041 TRY_CATCH (except
, RETURN_MASK_ALL
)
1043 CHECK_TYPEDEF (type
);
1045 if (!is_intlike (type
, 1))
1046 error (_("Cannot convert value to long."));
1048 l
= value_as_long (value
);
1050 GDB_PY_HANDLE_EXCEPTION (except
);
1052 return gdb_py_long_from_longest (l
);
1055 /* Implements conversion to float. */
1057 valpy_float (PyObject
*self
)
1059 struct value
*value
= ((value_object
*) self
)->value
;
1060 struct type
*type
= value_type (value
);
1062 volatile struct gdb_exception except
;
1064 TRY_CATCH (except
, RETURN_MASK_ALL
)
1066 CHECK_TYPEDEF (type
);
1068 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
1069 error (_("Cannot convert value to float."));
1071 d
= value_as_double (value
);
1073 GDB_PY_HANDLE_EXCEPTION (except
);
1075 return PyFloat_FromDouble (d
);
1078 /* Returns an object for a value which is released from the all_values chain,
1079 so its lifetime is not bound to the execution of a command. */
1081 value_to_value_object (struct value
*val
)
1083 value_object
*val_obj
;
1084 volatile struct gdb_exception except
;
1086 TRY_CATCH (except
, RETURN_MASK_ALL
)
1088 if (value_lazy (val
))
1089 value_fetch_lazy (val
);
1091 GDB_PY_HANDLE_EXCEPTION (except
);
1093 val_obj
= PyObject_New (value_object
, &value_object_type
);
1094 if (val_obj
!= NULL
)
1096 val_obj
->value
= val
;
1098 val_obj
->address
= NULL
;
1099 val_obj
->type
= NULL
;
1100 val_obj
->dynamic_type
= NULL
;
1101 note_value (val_obj
);
1104 return (PyObject
*) val_obj
;
1107 /* Returns a borrowed reference to the struct value corresponding to
1108 the given value object. */
1110 value_object_to_value (PyObject
*self
)
1114 if (! PyObject_TypeCheck (self
, &value_object_type
))
1116 real
= (value_object
*) self
;
1120 /* Try to convert a Python value to a gdb value. If the value cannot
1121 be converted, set a Python exception and return NULL. Returns a
1122 reference to a new value on the all_values chain. */
1125 convert_value_from_python (PyObject
*obj
)
1127 struct value
*value
= NULL
; /* -Wall */
1128 struct cleanup
*old
;
1129 volatile struct gdb_exception except
;
1132 gdb_assert (obj
!= NULL
);
1134 TRY_CATCH (except
, RETURN_MASK_ALL
)
1136 if (PyBool_Check (obj
))
1138 cmp
= PyObject_IsTrue (obj
);
1140 value
= value_from_longest (builtin_type_pybool
, cmp
);
1142 else if (PyInt_Check (obj
))
1144 long l
= PyInt_AsLong (obj
);
1146 if (! PyErr_Occurred ())
1147 value
= value_from_longest (builtin_type_pyint
, l
);
1149 else if (PyLong_Check (obj
))
1151 LONGEST l
= PyLong_AsLongLong (obj
);
1153 if (PyErr_Occurred ())
1155 /* If the error was an overflow, we can try converting to
1156 ULONGEST instead. */
1157 if (PyErr_ExceptionMatches (PyExc_OverflowError
))
1159 PyObject
*etype
, *evalue
, *etraceback
, *zero
;
1161 PyErr_Fetch (&etype
, &evalue
, &etraceback
);
1162 zero
= PyInt_FromLong (0);
1164 /* Check whether obj is positive. */
1165 if (PyObject_RichCompareBool (obj
, zero
, Py_GT
) > 0)
1169 ul
= PyLong_AsUnsignedLongLong (obj
);
1170 if (! PyErr_Occurred ())
1171 value
= value_from_ulongest (builtin_type_upylong
, ul
);
1174 /* There's nothing we can do. */
1175 PyErr_Restore (etype
, evalue
, etraceback
);
1181 value
= value_from_longest (builtin_type_pylong
, l
);
1183 else if (PyFloat_Check (obj
))
1185 double d
= PyFloat_AsDouble (obj
);
1187 if (! PyErr_Occurred ())
1188 value
= value_from_double (builtin_type_pyfloat
, d
);
1190 else if (gdbpy_is_string (obj
))
1194 s
= python_string_to_target_string (obj
);
1197 old
= make_cleanup (xfree
, s
);
1198 value
= value_cstring (s
, strlen (s
), builtin_type_pychar
);
1202 else if (PyObject_TypeCheck (obj
, &value_object_type
))
1203 value
= value_copy (((value_object
*) obj
)->value
);
1204 else if (gdbpy_is_lazy_string (obj
))
1208 result
= PyObject_CallMethodObjArgs (obj
, gdbpy_value_cst
, NULL
);
1209 value
= value_copy (((value_object
*) result
)->value
);
1212 PyErr_Format (PyExc_TypeError
,
1213 _("Could not convert Python object: %s."),
1214 PyString_AsString (PyObject_Str (obj
)));
1216 if (except
.reason
< 0)
1218 PyErr_Format (except
.reason
== RETURN_QUIT
1219 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
1220 "%s", except
.message
);
1227 /* Returns value object in the ARGth position in GDB's history. */
1229 gdbpy_history (PyObject
*self
, PyObject
*args
)
1232 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
1233 volatile struct gdb_exception except
;
1235 if (!PyArg_ParseTuple (args
, "i", &i
))
1238 TRY_CATCH (except
, RETURN_MASK_ALL
)
1240 res_val
= access_value_history (i
);
1242 GDB_PY_HANDLE_EXCEPTION (except
);
1244 return value_to_value_object (res_val
);
1247 /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
1250 gdbpy_is_value_object (PyObject
*obj
)
1252 return PyObject_TypeCheck (obj
, &value_object_type
);
1256 gdbpy_initialize_values (void)
1258 if (PyType_Ready (&value_object_type
) < 0)
1261 Py_INCREF (&value_object_type
);
1262 PyModule_AddObject (gdb_module
, "Value", (PyObject
*) &value_object_type
);
1264 values_in_python
= NULL
;
1269 static PyGetSetDef value_object_getset
[] = {
1270 { "address", valpy_get_address
, NULL
, "The address of the value.",
1272 { "is_optimized_out", valpy_get_is_optimized_out
, NULL
,
1273 "Boolean telling whether the value is optimized "
1274 "out (i.e., not available).",
1276 { "type", valpy_get_type
, NULL
, "Type of the value.", NULL
},
1277 { "dynamic_type", valpy_get_dynamic_type
, NULL
,
1278 "Dynamic type of the value.", NULL
},
1279 {NULL
} /* Sentinel */
1282 static PyMethodDef value_object_methods
[] = {
1283 { "cast", valpy_cast
, METH_VARARGS
, "Cast the value to the supplied type." },
1284 { "dynamic_cast", valpy_dynamic_cast
, METH_VARARGS
,
1285 "dynamic_cast (gdb.Type) -> gdb.Value\n\
1286 Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
1288 { "reinterpret_cast", valpy_reinterpret_cast
, METH_VARARGS
,
1289 "reinterpret_cast (gdb.Type) -> gdb.Value\n\
1290 Cast the value to the supplied type, as if by the C++\n\
1291 reinterpret_cast operator."
1293 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
1294 { "lazy_string", (PyCFunction
) valpy_lazy_string
,
1295 METH_VARARGS
| METH_KEYWORDS
,
1296 "lazy_string ([encoding] [, length]) -> lazy_string\n\
1297 Return a lazy string representation of the value." },
1298 { "string", (PyCFunction
) valpy_string
, METH_VARARGS
| METH_KEYWORDS
,
1299 "string ([encoding] [, errors] [, length]) -> string\n\
1300 Return Unicode string representation of the value." },
1301 {NULL
} /* Sentinel */
1304 static PyNumberMethods value_object_as_number
= {
1310 NULL
, /* nb_divmod */
1311 valpy_power
, /* nb_power */
1312 valpy_negative
, /* nb_negative */
1313 valpy_positive
, /* nb_positive */
1314 valpy_absolute
, /* nb_absolute */
1315 valpy_nonzero
, /* nb_nonzero */
1316 valpy_invert
, /* nb_invert */
1317 valpy_lsh
, /* nb_lshift */
1318 valpy_rsh
, /* nb_rshift */
1319 valpy_and
, /* nb_and */
1320 valpy_xor
, /* nb_xor */
1321 valpy_or
, /* nb_or */
1322 NULL
, /* nb_coerce */
1323 valpy_int
, /* nb_int */
1324 valpy_long
, /* nb_long */
1325 valpy_float
, /* nb_float */
1330 static PyMappingMethods value_object_as_mapping
= {
1336 PyTypeObject value_object_type
= {
1337 PyObject_HEAD_INIT (NULL
)
1339 "gdb.Value", /*tp_name*/
1340 sizeof (value_object
), /*tp_basicsize*/
1342 valpy_dealloc
, /*tp_dealloc*/
1348 &value_object_as_number
, /*tp_as_number*/
1349 0, /*tp_as_sequence*/
1350 &value_object_as_mapping
, /*tp_as_mapping*/
1351 valpy_hash
, /*tp_hash*/
1352 valpy_call
, /*tp_call*/
1353 valpy_str
, /*tp_str*/
1357 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
1358 | Py_TPFLAGS_BASETYPE
, /*tp_flags*/
1359 "GDB value object", /* tp_doc */
1360 0, /* tp_traverse */
1362 valpy_richcompare
, /* tp_richcompare */
1363 0, /* tp_weaklistoffset */
1365 0, /* tp_iternext */
1366 value_object_methods
, /* tp_methods */
1368 value_object_getset
, /* tp_getset */
1371 0, /* tp_descr_get */
1372 0, /* tp_descr_set */
1373 0, /* tp_dictoffset */
1376 valpy_new
/* tp_new */
1382 preserve_python_values (struct objfile
*objfile
, htab_t copied_types
)
1387 #endif /* HAVE_PYTHON */