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"
35 #include "python-internal.h"
37 /* Even though Python scalar types directly map to host types, we use
38 target types here to remain consistent with the values system in
39 GDB (which uses target arithmetic). */
41 /* Python's integer type corresponds to C's long type. */
42 #define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long
44 /* Python's float type corresponds to C's double type. */
45 #define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double
47 /* Python's long type corresponds to C's long long type. */
48 #define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long
50 /* Python's long type corresponds to C's long long type. Unsigned version. */
51 #define builtin_type_upylong builtin_type \
52 (python_gdbarch)->builtin_unsigned_long_long
54 #define builtin_type_pybool \
55 language_bool_type (python_language, python_gdbarch)
57 #define builtin_type_pychar \
58 language_string_char_type (python_language, python_gdbarch)
60 typedef struct value_object
{
62 struct value_object
*next
;
63 struct value_object
*prev
;
67 PyObject
*dynamic_type
;
70 /* List of all values which are currently exposed to Python. It is
71 maintained so that when an objfile is discarded, preserve_values
72 can copy the values' types if needed. */
73 /* This variable is unnecessarily initialized to NULL in order to
74 work around a linker bug on MacOS. */
75 static value_object
*values_in_python
= NULL
;
77 /* Called by the Python interpreter when deallocating a value object. */
79 valpy_dealloc (PyObject
*obj
)
81 value_object
*self
= (value_object
*) obj
;
83 /* Remove SELF from the global list. */
85 self
->prev
->next
= self
->next
;
88 gdb_assert (values_in_python
== self
);
89 values_in_python
= self
->next
;
92 self
->next
->prev
= self
->prev
;
94 value_free (self
->value
);
97 /* Use braces to appease gcc warning. *sigh* */
99 Py_DECREF (self
->address
);
104 Py_DECREF (self
->type
);
107 Py_XDECREF (self
->dynamic_type
);
109 Py_TYPE (self
)->tp_free (self
);
112 /* Helper to push a Value object on the global list. */
114 note_value (value_object
*value_obj
)
116 value_obj
->next
= values_in_python
;
118 value_obj
->next
->prev
= value_obj
;
119 value_obj
->prev
= NULL
;
120 values_in_python
= value_obj
;
123 /* Called when a new gdb.Value object needs to be allocated. Returns NULL on
124 error, with a python exception set. */
126 valpy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*keywords
)
128 struct value
*value
= NULL
; /* Initialize to appease gcc warning. */
129 value_object
*value_obj
;
131 if (PyTuple_Size (args
) != 1)
133 PyErr_SetString (PyExc_TypeError
, _("Value object creation takes only "
138 value_obj
= (value_object
*) subtype
->tp_alloc (subtype
, 1);
139 if (value_obj
== NULL
)
141 PyErr_SetString (PyExc_MemoryError
, _("Could not allocate memory to "
142 "create Value object."));
146 value
= convert_value_from_python (PyTuple_GetItem (args
, 0));
149 subtype
->tp_free (value_obj
);
153 value_obj
->value
= value
;
154 release_value_or_incref (value
);
155 value_obj
->address
= NULL
;
156 value_obj
->type
= NULL
;
157 value_obj
->dynamic_type
= NULL
;
158 note_value (value_obj
);
160 return (PyObject
*) value_obj
;
163 /* Iterate over all the Value objects, calling preserve_one_value on
166 preserve_python_values (struct objfile
*objfile
, htab_t copied_types
)
170 for (iter
= values_in_python
; iter
; iter
= iter
->next
)
171 preserve_one_value (iter
->value
, objfile
, copied_types
);
174 /* Given a value of a pointer type, apply the C unary * operator to it. */
176 valpy_dereference (PyObject
*self
, PyObject
*args
)
178 volatile struct gdb_exception except
;
179 PyObject
*result
= NULL
;
181 TRY_CATCH (except
, RETURN_MASK_ALL
)
183 struct value
*res_val
;
184 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
186 res_val
= value_ind (((value_object
*) self
)->value
);
187 result
= value_to_value_object (res_val
);
188 do_cleanups (cleanup
);
190 GDB_PY_HANDLE_EXCEPTION (except
);
195 /* Given a value of a pointer type or a reference type, return the value
196 referenced. The difference between this function and valpy_dereference is
197 that the latter applies * unary operator to a value, which need not always
198 result in the value referenced. For example, for a value which is a reference
199 to an 'int' pointer ('int *'), valpy_dereference will result in a value of
200 type 'int' while valpy_referenced_value will result in a value of type
204 valpy_referenced_value (PyObject
*self
, PyObject
*args
)
206 volatile struct gdb_exception except
;
207 PyObject
*result
= NULL
;
209 TRY_CATCH (except
, RETURN_MASK_ALL
)
211 struct value
*self_val
, *res_val
;
212 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
214 self_val
= ((value_object
*) self
)->value
;
215 switch (TYPE_CODE (check_typedef (value_type (self_val
))))
218 res_val
= value_ind (self_val
);
221 res_val
= coerce_ref (self_val
);
224 error(_("Trying to get the referenced value from a value which is "
225 "neither a pointer nor a reference."));
228 result
= value_to_value_object (res_val
);
229 do_cleanups (cleanup
);
231 GDB_PY_HANDLE_EXCEPTION (except
);
236 /* Return "&value". */
238 valpy_get_address (PyObject
*self
, void *closure
)
240 value_object
*val_obj
= (value_object
*) self
;
241 volatile struct gdb_exception except
;
243 if (!val_obj
->address
)
245 TRY_CATCH (except
, RETURN_MASK_ALL
)
247 struct value
*res_val
;
248 struct cleanup
*cleanup
249 = make_cleanup_value_free_to_mark (value_mark ());
251 res_val
= value_addr (val_obj
->value
);
252 val_obj
->address
= value_to_value_object (res_val
);
253 do_cleanups (cleanup
);
255 if (except
.reason
< 0)
257 val_obj
->address
= Py_None
;
262 Py_XINCREF (val_obj
->address
);
264 return val_obj
->address
;
267 /* Return type of the value. */
269 valpy_get_type (PyObject
*self
, void *closure
)
271 value_object
*obj
= (value_object
*) self
;
275 obj
->type
= type_to_type_object (value_type (obj
->value
));
279 Py_INCREF (obj
->type
);
283 /* Return dynamic type of the value. */
286 valpy_get_dynamic_type (PyObject
*self
, void *closure
)
288 value_object
*obj
= (value_object
*) self
;
289 volatile struct gdb_exception except
;
290 struct type
*type
= NULL
;
292 if (obj
->dynamic_type
!= NULL
)
294 Py_INCREF (obj
->dynamic_type
);
295 return obj
->dynamic_type
;
298 TRY_CATCH (except
, RETURN_MASK_ALL
)
300 struct value
*val
= obj
->value
;
301 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
303 type
= value_type (val
);
304 CHECK_TYPEDEF (type
);
306 if (((TYPE_CODE (type
) == TYPE_CODE_PTR
)
307 || (TYPE_CODE (type
) == TYPE_CODE_REF
))
308 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_CLASS
))
310 struct value
*target
;
311 int was_pointer
= TYPE_CODE (type
) == TYPE_CODE_PTR
;
313 target
= value_ind (val
);
314 type
= value_rtti_type (target
, NULL
, NULL
, NULL
);
319 type
= lookup_pointer_type (type
);
321 type
= lookup_reference_type (type
);
324 else if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
325 type
= value_rtti_type (val
, NULL
, NULL
, NULL
);
328 /* Re-use object's static type. */
332 do_cleanups (cleanup
);
334 GDB_PY_HANDLE_EXCEPTION (except
);
338 /* Ensure that the TYPE field is ready. */
339 if (!valpy_get_type (self
, NULL
))
341 /* We don't need to incref here, because valpy_get_type already
343 obj
->dynamic_type
= obj
->type
;
346 obj
->dynamic_type
= type_to_type_object (type
);
348 Py_INCREF (obj
->dynamic_type
);
349 return obj
->dynamic_type
;
352 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
353 string. Return a PyObject representing a lazy_string_object type.
354 A lazy string is a pointer to a string with an optional encoding and
355 length. If ENCODING is not given, encoding is set to None. If an
356 ENCODING is provided the encoding parameter is set to ENCODING, but
357 the string is not encoded. If LENGTH is provided then the length
358 parameter is set to LENGTH, otherwise length will be set to -1 (first
359 null of appropriate with). */
361 valpy_lazy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
363 gdb_py_longest length
= -1;
364 struct value
*value
= ((value_object
*) self
)->value
;
365 const char *user_encoding
= NULL
;
366 static char *keywords
[] = { "encoding", "length", NULL
};
367 PyObject
*str_obj
= NULL
;
368 volatile struct gdb_exception except
;
370 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|s" GDB_PY_LL_ARG
, keywords
,
371 &user_encoding
, &length
))
374 TRY_CATCH (except
, RETURN_MASK_ALL
)
376 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
378 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_PTR
)
379 value
= value_ind (value
);
381 str_obj
= gdbpy_create_lazy_string_object (value_address (value
), length
,
385 do_cleanups (cleanup
);
387 GDB_PY_HANDLE_EXCEPTION (except
);
392 /* Implementation of gdb.Value.string ([encoding] [, errors]
393 [, length]) -> string. Return Unicode string with value contents.
394 If ENCODING is not given, the string is assumed to be encoded in
395 the target's charset. If LENGTH is provided, only fetch string to
396 the length provided. */
399 valpy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
403 struct value
*value
= ((value_object
*) self
)->value
;
404 volatile struct gdb_exception except
;
406 const char *encoding
= NULL
;
407 const char *errors
= NULL
;
408 const char *user_encoding
= NULL
;
409 const char *la_encoding
= NULL
;
410 struct type
*char_type
;
411 static char *keywords
[] = { "encoding", "errors", "length", NULL
};
413 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|ssi", keywords
,
414 &user_encoding
, &errors
, &length
))
417 TRY_CATCH (except
, RETURN_MASK_ALL
)
419 LA_GET_STRING (value
, &buffer
, &length
, &char_type
, &la_encoding
);
421 GDB_PY_HANDLE_EXCEPTION (except
);
423 encoding
= (user_encoding
&& *user_encoding
) ? user_encoding
: la_encoding
;
424 unicode
= PyUnicode_Decode (buffer
, length
* TYPE_LENGTH (char_type
),
431 /* A helper function that implements the various cast operators. */
434 valpy_do_cast (PyObject
*self
, PyObject
*args
, enum exp_opcode op
)
436 PyObject
*type_obj
, *result
= NULL
;
438 volatile struct gdb_exception except
;
440 if (! PyArg_ParseTuple (args
, "O", &type_obj
))
443 type
= type_object_to_type (type_obj
);
446 PyErr_SetString (PyExc_RuntimeError
,
447 _("Argument must be a type."));
451 TRY_CATCH (except
, RETURN_MASK_ALL
)
453 struct value
*val
= ((value_object
*) self
)->value
;
454 struct value
*res_val
;
455 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
457 if (op
== UNOP_DYNAMIC_CAST
)
458 res_val
= value_dynamic_cast (type
, val
);
459 else if (op
== UNOP_REINTERPRET_CAST
)
460 res_val
= value_reinterpret_cast (type
, val
);
463 gdb_assert (op
== UNOP_CAST
);
464 res_val
= value_cast (type
, val
);
467 result
= value_to_value_object (res_val
);
468 do_cleanups (cleanup
);
470 GDB_PY_HANDLE_EXCEPTION (except
);
475 /* Implementation of the "cast" method. */
478 valpy_cast (PyObject
*self
, PyObject
*args
)
480 return valpy_do_cast (self
, args
, UNOP_CAST
);
483 /* Implementation of the "dynamic_cast" method. */
486 valpy_dynamic_cast (PyObject
*self
, PyObject
*args
)
488 return valpy_do_cast (self
, args
, UNOP_DYNAMIC_CAST
);
491 /* Implementation of the "reinterpret_cast" method. */
494 valpy_reinterpret_cast (PyObject
*self
, PyObject
*args
)
496 return valpy_do_cast (self
, args
, UNOP_REINTERPRET_CAST
);
500 valpy_length (PyObject
*self
)
502 /* We don't support getting the number of elements in a struct / class. */
503 PyErr_SetString (PyExc_NotImplementedError
,
504 _("Invalid operation on gdb.Value."));
508 /* Given string name of an element inside structure, return its value
509 object. Returns NULL on error, with a python exception set. */
511 valpy_getitem (PyObject
*self
, PyObject
*key
)
513 value_object
*self_value
= (value_object
*) self
;
515 volatile struct gdb_exception except
;
516 PyObject
*result
= NULL
;
518 if (gdbpy_is_string (key
))
520 field
= python_string_to_host_string (key
);
525 TRY_CATCH (except
, RETURN_MASK_ALL
)
527 struct value
*tmp
= self_value
->value
;
528 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
529 struct value
*res_val
= NULL
;
532 res_val
= value_struct_elt (&tmp
, NULL
, field
, 0, NULL
);
535 /* Assume we are attempting an array access, and let the
536 value code throw an exception if the index has an invalid
538 struct value
*idx
= convert_value_from_python (key
);
542 /* Check the value's type is something that can be accessed via
546 tmp
= coerce_ref (tmp
);
547 type
= check_typedef (value_type (tmp
));
548 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
549 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
550 error (_("Cannot subscript requested type."));
552 res_val
= value_subscript (tmp
, value_as_long (idx
));
557 result
= value_to_value_object (res_val
);
558 do_cleanups (cleanup
);
562 GDB_PY_HANDLE_EXCEPTION (except
);
568 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
570 PyErr_Format (PyExc_NotImplementedError
,
571 _("Setting of struct elements is not currently supported."));
575 /* Called by the Python interpreter to perform an inferior function
576 call on the value. Returns NULL on error, with a python exception set. */
578 valpy_call (PyObject
*self
, PyObject
*args
, PyObject
*keywords
)
580 Py_ssize_t args_count
;
581 volatile struct gdb_exception except
;
582 struct value
*function
= ((value_object
*) self
)->value
;
583 struct value
**vargs
= NULL
;
584 struct type
*ftype
= NULL
;
585 struct value
*mark
= value_mark ();
586 PyObject
*result
= NULL
;
588 TRY_CATCH (except
, RETURN_MASK_ALL
)
590 ftype
= check_typedef (value_type (function
));
592 GDB_PY_HANDLE_EXCEPTION (except
);
594 if (TYPE_CODE (ftype
) != TYPE_CODE_FUNC
)
596 PyErr_SetString (PyExc_RuntimeError
,
597 _("Value is not callable (not TYPE_CODE_FUNC)."));
601 if (! PyTuple_Check (args
))
603 PyErr_SetString (PyExc_TypeError
,
604 _("Inferior arguments must be provided in a tuple."));
608 args_count
= PyTuple_Size (args
);
613 vargs
= alloca (sizeof (struct value
*) * args_count
);
614 for (i
= 0; i
< args_count
; i
++)
616 PyObject
*item
= PyTuple_GetItem (args
, i
);
621 vargs
[i
] = convert_value_from_python (item
);
622 if (vargs
[i
] == NULL
)
627 TRY_CATCH (except
, RETURN_MASK_ALL
)
629 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (mark
);
630 struct value
*return_value
;
632 return_value
= call_function_by_hand (function
, args_count
, vargs
);
633 result
= value_to_value_object (return_value
);
634 do_cleanups (cleanup
);
636 GDB_PY_HANDLE_EXCEPTION (except
);
641 /* Called by the Python interpreter to obtain string representation
644 valpy_str (PyObject
*self
)
648 struct value_print_options opts
;
649 volatile struct gdb_exception except
;
651 get_user_print_options (&opts
);
654 TRY_CATCH (except
, RETURN_MASK_ALL
)
656 struct ui_file
*stb
= mem_fileopen ();
657 struct cleanup
*old_chain
= make_cleanup_ui_file_delete (stb
);
659 common_val_print (((value_object
*) self
)->value
, stb
, 0,
660 &opts
, python_language
);
661 s
= ui_file_xstrdup (stb
, NULL
);
663 do_cleanups (old_chain
);
665 GDB_PY_HANDLE_EXCEPTION (except
);
667 result
= PyUnicode_Decode (s
, strlen (s
), host_charset (), NULL
);
673 /* Implements gdb.Value.is_optimized_out. */
675 valpy_get_is_optimized_out (PyObject
*self
, void *closure
)
677 struct value
*value
= ((value_object
*) self
)->value
;
679 volatile struct gdb_exception except
;
681 TRY_CATCH (except
, RETURN_MASK_ALL
)
683 opt
= value_optimized_out (value
);
685 GDB_PY_HANDLE_EXCEPTION (except
);
693 /* Implements gdb.Value.is_lazy. */
695 valpy_get_is_lazy (PyObject
*self
, void *closure
)
697 struct value
*value
= ((value_object
*) self
)->value
;
699 volatile struct gdb_exception except
;
701 TRY_CATCH (except
, RETURN_MASK_ALL
)
703 opt
= value_lazy (value
);
705 GDB_PY_HANDLE_EXCEPTION (except
);
713 /* Implements gdb.Value.fetch_lazy (). */
715 valpy_fetch_lazy (PyObject
*self
, PyObject
*args
)
717 struct value
*value
= ((value_object
*) self
)->value
;
718 volatile struct gdb_exception except
;
720 TRY_CATCH (except
, RETURN_MASK_ALL
)
722 if (value_lazy (value
))
723 value_fetch_lazy (value
);
725 GDB_PY_HANDLE_EXCEPTION (except
);
730 /* Calculate and return the address of the PyObject as the value of
731 the builtin __hash__ call. */
733 valpy_hash (PyObject
*self
)
735 return (long) (intptr_t) self
;
753 /* If TYPE is a reference, return the target; otherwise return TYPE. */
754 #define STRIP_REFERENCE(TYPE) \
755 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
757 /* Returns a value object which is the result of applying the operation
758 specified by OPCODE to the given arguments. Returns NULL on error, with
759 a python exception set. */
761 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
763 volatile struct gdb_exception except
;
764 PyObject
*result
= NULL
;
766 TRY_CATCH (except
, RETURN_MASK_ALL
)
768 struct value
*arg1
, *arg2
;
769 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
770 struct value
*res_val
= NULL
;
772 /* If the gdb.Value object is the second operand, then it will be passed
773 to us as the OTHER argument, and SELF will be an entirely different
774 kind of object, altogether. Because of this, we can't assume self is
775 a gdb.Value object and need to convert it from python as well. */
776 arg1
= convert_value_from_python (self
);
780 arg2
= convert_value_from_python (other
);
788 struct type
*ltype
= value_type (arg1
);
789 struct type
*rtype
= value_type (arg2
);
791 CHECK_TYPEDEF (ltype
);
792 ltype
= STRIP_REFERENCE (ltype
);
793 CHECK_TYPEDEF (rtype
);
794 rtype
= STRIP_REFERENCE (rtype
);
796 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
797 && is_integral_type (rtype
))
798 res_val
= value_ptradd (arg1
, value_as_long (arg2
));
799 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
800 && is_integral_type (ltype
))
801 res_val
= value_ptradd (arg2
, value_as_long (arg1
));
803 res_val
= value_binop (arg1
, arg2
, BINOP_ADD
);
808 struct type
*ltype
= value_type (arg1
);
809 struct type
*rtype
= value_type (arg2
);
811 CHECK_TYPEDEF (ltype
);
812 ltype
= STRIP_REFERENCE (ltype
);
813 CHECK_TYPEDEF (rtype
);
814 rtype
= STRIP_REFERENCE (rtype
);
816 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
817 && TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
818 /* A ptrdiff_t for the target would be preferable here. */
819 res_val
= value_from_longest (builtin_type_pyint
,
820 value_ptrdiff (arg1
, arg2
));
821 else if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
822 && is_integral_type (rtype
))
823 res_val
= value_ptradd (arg1
, - value_as_long (arg2
));
825 res_val
= value_binop (arg1
, arg2
, BINOP_SUB
);
829 res_val
= value_binop (arg1
, arg2
, BINOP_MUL
);
832 res_val
= value_binop (arg1
, arg2
, BINOP_DIV
);
835 res_val
= value_binop (arg1
, arg2
, BINOP_REM
);
838 res_val
= value_binop (arg1
, arg2
, BINOP_EXP
);
841 res_val
= value_binop (arg1
, arg2
, BINOP_LSH
);
844 res_val
= value_binop (arg1
, arg2
, BINOP_RSH
);
847 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_AND
);
850 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_IOR
);
853 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_XOR
);
858 result
= value_to_value_object (res_val
);
860 do_cleanups (cleanup
);
862 GDB_PY_HANDLE_EXCEPTION (except
);
868 valpy_add (PyObject
*self
, PyObject
*other
)
870 return valpy_binop (VALPY_ADD
, self
, other
);
874 valpy_subtract (PyObject
*self
, PyObject
*other
)
876 return valpy_binop (VALPY_SUB
, self
, other
);
880 valpy_multiply (PyObject
*self
, PyObject
*other
)
882 return valpy_binop (VALPY_MUL
, self
, other
);
886 valpy_divide (PyObject
*self
, PyObject
*other
)
888 return valpy_binop (VALPY_DIV
, self
, other
);
892 valpy_remainder (PyObject
*self
, PyObject
*other
)
894 return valpy_binop (VALPY_REM
, self
, other
);
898 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
900 /* We don't support the ternary form of pow. I don't know how to express
901 that, so let's just throw NotImplementedError to at least do something
903 if (unused
!= Py_None
)
905 PyErr_SetString (PyExc_NotImplementedError
,
906 "Invalid operation on gdb.Value.");
910 return valpy_binop (VALPY_POW
, self
, other
);
914 valpy_negative (PyObject
*self
)
916 volatile struct gdb_exception except
;
917 PyObject
*result
= NULL
;
919 TRY_CATCH (except
, RETURN_MASK_ALL
)
921 /* Perhaps overkill, but consistency has some virtue. */
922 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
925 val
= value_neg (((value_object
*) self
)->value
);
926 result
= value_to_value_object (val
);
927 do_cleanups (cleanup
);
929 GDB_PY_HANDLE_EXCEPTION (except
);
935 valpy_positive (PyObject
*self
)
937 return value_to_value_object (((value_object
*) self
)->value
);
941 valpy_absolute (PyObject
*self
)
943 struct value
*value
= ((value_object
*) self
)->value
;
944 volatile struct gdb_exception except
;
947 TRY_CATCH (except
, RETURN_MASK_ALL
)
949 struct cleanup
*cleanup
= make_cleanup_value_free_to_mark (value_mark ());
951 if (value_less (value
, value_zero (value_type (value
), not_lval
)))
954 do_cleanups (cleanup
);
956 GDB_PY_HANDLE_EXCEPTION (except
);
959 return valpy_positive (self
);
961 return valpy_negative (self
);
964 /* Implements boolean evaluation of gdb.Value. */
966 valpy_nonzero (PyObject
*self
)
968 volatile struct gdb_exception except
;
969 value_object
*self_value
= (value_object
*) self
;
971 int nonzero
= 0; /* Appease GCC warning. */
973 TRY_CATCH (except
, RETURN_MASK_ALL
)
975 type
= check_typedef (value_type (self_value
->value
));
977 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
978 nonzero
= !!value_as_long (self_value
->value
);
979 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
980 nonzero
= value_as_double (self_value
->value
) != 0;
981 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
982 nonzero
= !decimal_is_zero (value_contents (self_value
->value
),
984 gdbarch_byte_order (get_type_arch (type
)));
986 /* All other values are True. */
989 /* This is not documented in the Python documentation, but if this
990 function fails, return -1 as slot_nb_nonzero does (the default
991 Python nonzero function). */
992 GDB_PY_SET_HANDLE_EXCEPTION (except
);
997 /* Implements ~ for value objects. */
999 valpy_invert (PyObject
*self
)
1001 struct value
*val
= NULL
;
1002 volatile struct gdb_exception except
;
1004 TRY_CATCH (except
, RETURN_MASK_ALL
)
1006 val
= value_complement (((value_object
*) self
)->value
);
1008 GDB_PY_HANDLE_EXCEPTION (except
);
1010 return value_to_value_object (val
);
1013 /* Implements left shift for value objects. */
1015 valpy_lsh (PyObject
*self
, PyObject
*other
)
1017 return valpy_binop (VALPY_LSH
, self
, other
);
1020 /* Implements right shift for value objects. */
1022 valpy_rsh (PyObject
*self
, PyObject
*other
)
1024 return valpy_binop (VALPY_RSH
, self
, other
);
1027 /* Implements bitwise and for value objects. */
1029 valpy_and (PyObject
*self
, PyObject
*other
)
1031 return valpy_binop (VALPY_BITAND
, self
, other
);
1034 /* Implements bitwise or for value objects. */
1036 valpy_or (PyObject
*self
, PyObject
*other
)
1038 return valpy_binop (VALPY_BITOR
, self
, other
);
1041 /* Implements bitwise xor for value objects. */
1043 valpy_xor (PyObject
*self
, PyObject
*other
)
1045 return valpy_binop (VALPY_BITXOR
, self
, other
);
1048 /* Implements comparison operations for value objects. Returns NULL on error,
1049 with a python exception set. */
1051 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
1054 volatile struct gdb_exception except
;
1056 if (other
== Py_None
)
1057 /* Comparing with None is special. From what I can tell, in Python
1058 None is smaller than anything else. */
1070 PyErr_SetString (PyExc_NotImplementedError
,
1071 _("Invalid operation on gdb.Value."));
1075 TRY_CATCH (except
, RETURN_MASK_ALL
)
1077 struct value
*value_other
, *mark
= value_mark ();
1078 struct cleanup
*cleanup
;
1080 value_other
= convert_value_from_python (other
);
1081 if (value_other
== NULL
)
1087 cleanup
= make_cleanup_value_free_to_mark (mark
);
1091 result
= value_less (((value_object
*) self
)->value
, value_other
);
1094 result
= value_less (((value_object
*) self
)->value
, value_other
)
1095 || value_equal (((value_object
*) self
)->value
, value_other
);
1098 result
= value_equal (((value_object
*) self
)->value
, value_other
);
1101 result
= !value_equal (((value_object
*) self
)->value
, value_other
);
1104 result
= value_less (value_other
, ((value_object
*) self
)->value
);
1107 result
= value_less (value_other
, ((value_object
*) self
)->value
)
1108 || value_equal (((value_object
*) self
)->value
, value_other
);
1112 PyErr_SetString (PyExc_NotImplementedError
,
1113 _("Invalid operation on gdb.Value."));
1118 do_cleanups (cleanup
);
1120 GDB_PY_HANDLE_EXCEPTION (except
);
1122 /* In this case, the Python exception has already been set. */
1132 /* Helper function to determine if a type is "int-like". */
1134 is_intlike (struct type
*type
, int ptr_ok
)
1136 return (TYPE_CODE (type
) == TYPE_CODE_INT
1137 || TYPE_CODE (type
) == TYPE_CODE_ENUM
1138 || TYPE_CODE (type
) == TYPE_CODE_BOOL
1139 || TYPE_CODE (type
) == TYPE_CODE_CHAR
1140 || (ptr_ok
&& TYPE_CODE (type
) == TYPE_CODE_PTR
));
1144 /* Implements conversion to int. */
1146 valpy_int (PyObject
*self
)
1148 struct value
*value
= ((value_object
*) self
)->value
;
1149 struct type
*type
= value_type (value
);
1151 volatile struct gdb_exception except
;
1153 TRY_CATCH (except
, RETURN_MASK_ALL
)
1155 CHECK_TYPEDEF (type
);
1156 if (!is_intlike (type
, 0))
1157 error (_("Cannot convert value to int."));
1159 l
= value_as_long (value
);
1161 GDB_PY_HANDLE_EXCEPTION (except
);
1163 return gdb_py_object_from_longest (l
);
1167 /* Implements conversion to long. */
1169 valpy_long (PyObject
*self
)
1171 struct value
*value
= ((value_object
*) self
)->value
;
1172 struct type
*type
= value_type (value
);
1174 volatile struct gdb_exception except
;
1176 TRY_CATCH (except
, RETURN_MASK_ALL
)
1178 CHECK_TYPEDEF (type
);
1180 if (!is_intlike (type
, 1))
1181 error (_("Cannot convert value to long."));
1183 l
= value_as_long (value
);
1185 GDB_PY_HANDLE_EXCEPTION (except
);
1187 return gdb_py_long_from_longest (l
);
1190 /* Implements conversion to float. */
1192 valpy_float (PyObject
*self
)
1194 struct value
*value
= ((value_object
*) self
)->value
;
1195 struct type
*type
= value_type (value
);
1197 volatile struct gdb_exception except
;
1199 TRY_CATCH (except
, RETURN_MASK_ALL
)
1201 CHECK_TYPEDEF (type
);
1203 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
1204 error (_("Cannot convert value to float."));
1206 d
= value_as_double (value
);
1208 GDB_PY_HANDLE_EXCEPTION (except
);
1210 return PyFloat_FromDouble (d
);
1213 /* Returns an object for a value which is released from the all_values chain,
1214 so its lifetime is not bound to the execution of a command. */
1216 value_to_value_object (struct value
*val
)
1218 value_object
*val_obj
;
1220 val_obj
= PyObject_New (value_object
, &value_object_type
);
1221 if (val_obj
!= NULL
)
1223 val_obj
->value
= val
;
1224 release_value_or_incref (val
);
1225 val_obj
->address
= NULL
;
1226 val_obj
->type
= NULL
;
1227 val_obj
->dynamic_type
= NULL
;
1228 note_value (val_obj
);
1231 return (PyObject
*) val_obj
;
1234 /* Returns a borrowed reference to the struct value corresponding to
1235 the given value object. */
1237 value_object_to_value (PyObject
*self
)
1241 if (! PyObject_TypeCheck (self
, &value_object_type
))
1243 real
= (value_object
*) self
;
1247 /* Try to convert a Python value to a gdb value. If the value cannot
1248 be converted, set a Python exception and return NULL. Returns a
1249 reference to a new value on the all_values chain. */
1252 convert_value_from_python (PyObject
*obj
)
1254 struct value
*value
= NULL
; /* -Wall */
1255 volatile struct gdb_exception except
;
1258 gdb_assert (obj
!= NULL
);
1260 TRY_CATCH (except
, RETURN_MASK_ALL
)
1262 if (PyBool_Check (obj
))
1264 cmp
= PyObject_IsTrue (obj
);
1266 value
= value_from_longest (builtin_type_pybool
, cmp
);
1268 else if (PyInt_Check (obj
))
1270 long l
= PyInt_AsLong (obj
);
1272 if (! PyErr_Occurred ())
1273 value
= value_from_longest (builtin_type_pyint
, l
);
1275 else if (PyLong_Check (obj
))
1277 LONGEST l
= PyLong_AsLongLong (obj
);
1279 if (PyErr_Occurred ())
1281 /* If the error was an overflow, we can try converting to
1282 ULONGEST instead. */
1283 if (PyErr_ExceptionMatches (PyExc_OverflowError
))
1285 PyObject
*etype
, *evalue
, *etraceback
, *zero
;
1287 PyErr_Fetch (&etype
, &evalue
, &etraceback
);
1288 zero
= PyInt_FromLong (0);
1290 /* Check whether obj is positive. */
1291 if (PyObject_RichCompareBool (obj
, zero
, Py_GT
) > 0)
1295 ul
= PyLong_AsUnsignedLongLong (obj
);
1296 if (! PyErr_Occurred ())
1297 value
= value_from_ulongest (builtin_type_upylong
, ul
);
1300 /* There's nothing we can do. */
1301 PyErr_Restore (etype
, evalue
, etraceback
);
1307 value
= value_from_longest (builtin_type_pylong
, l
);
1309 else if (PyFloat_Check (obj
))
1311 double d
= PyFloat_AsDouble (obj
);
1313 if (! PyErr_Occurred ())
1314 value
= value_from_double (builtin_type_pyfloat
, d
);
1316 else if (gdbpy_is_string (obj
))
1320 s
= python_string_to_target_string (obj
);
1323 struct cleanup
*old
;
1325 old
= make_cleanup (xfree
, s
);
1326 value
= value_cstring (s
, strlen (s
), builtin_type_pychar
);
1330 else if (PyObject_TypeCheck (obj
, &value_object_type
))
1331 value
= value_copy (((value_object
*) obj
)->value
);
1332 else if (gdbpy_is_lazy_string (obj
))
1336 result
= PyObject_CallMethodObjArgs (obj
, gdbpy_value_cst
, NULL
);
1337 value
= value_copy (((value_object
*) result
)->value
);
1341 PyErr_Format (PyExc_TypeError
,
1342 _("Could not convert Python object: %S."), obj
);
1344 PyErr_Format (PyExc_TypeError
,
1345 _("Could not convert Python object: %s."),
1346 PyString_AsString (PyObject_Str (obj
)));
1349 if (except
.reason
< 0)
1351 PyErr_Format (except
.reason
== RETURN_QUIT
1352 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
1353 "%s", except
.message
);
1360 /* Returns value object in the ARGth position in GDB's history. */
1362 gdbpy_history (PyObject
*self
, PyObject
*args
)
1365 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
1366 volatile struct gdb_exception except
;
1368 if (!PyArg_ParseTuple (args
, "i", &i
))
1371 TRY_CATCH (except
, RETURN_MASK_ALL
)
1373 res_val
= access_value_history (i
);
1375 GDB_PY_HANDLE_EXCEPTION (except
);
1377 return value_to_value_object (res_val
);
1380 /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
1383 gdbpy_is_value_object (PyObject
*obj
)
1385 return PyObject_TypeCheck (obj
, &value_object_type
);
1389 gdbpy_initialize_values (void)
1391 if (PyType_Ready (&value_object_type
) < 0)
1394 Py_INCREF (&value_object_type
);
1395 PyModule_AddObject (gdb_module
, "Value", (PyObject
*) &value_object_type
);
1397 values_in_python
= NULL
;
1402 static PyGetSetDef value_object_getset
[] = {
1403 { "address", valpy_get_address
, NULL
, "The address of the value.",
1405 { "is_optimized_out", valpy_get_is_optimized_out
, NULL
,
1406 "Boolean telling whether the value is optimized "
1407 "out (i.e., not available).",
1409 { "type", valpy_get_type
, NULL
, "Type of the value.", NULL
},
1410 { "dynamic_type", valpy_get_dynamic_type
, NULL
,
1411 "Dynamic type of the value.", NULL
},
1412 { "is_lazy", valpy_get_is_lazy
, NULL
,
1413 "Boolean telling whether the value is lazy (not fetched yet\n\
1414 from the inferior). A lazy value is fetched when needed, or when\n\
1415 the \"fetch_lazy()\" method is called.", NULL
},
1416 {NULL
} /* Sentinel */
1419 static PyMethodDef value_object_methods
[] = {
1420 { "cast", valpy_cast
, METH_VARARGS
, "Cast the value to the supplied type." },
1421 { "dynamic_cast", valpy_dynamic_cast
, METH_VARARGS
,
1422 "dynamic_cast (gdb.Type) -> gdb.Value\n\
1423 Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
1425 { "reinterpret_cast", valpy_reinterpret_cast
, METH_VARARGS
,
1426 "reinterpret_cast (gdb.Type) -> gdb.Value\n\
1427 Cast the value to the supplied type, as if by the C++\n\
1428 reinterpret_cast operator."
1430 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
1431 { "referenced_value", valpy_referenced_value
, METH_NOARGS
,
1432 "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." },
1433 { "lazy_string", (PyCFunction
) valpy_lazy_string
,
1434 METH_VARARGS
| METH_KEYWORDS
,
1435 "lazy_string ([encoding] [, length]) -> lazy_string\n\
1436 Return a lazy string representation of the value." },
1437 { "string", (PyCFunction
) valpy_string
, METH_VARARGS
| METH_KEYWORDS
,
1438 "string ([encoding] [, errors] [, length]) -> string\n\
1439 Return Unicode string representation of the value." },
1440 { "fetch_lazy", valpy_fetch_lazy
, METH_NOARGS
,
1441 "Fetches the value from the inferior, if it was lazy." },
1442 {NULL
} /* Sentinel */
1445 static PyNumberMethods value_object_as_number
= {
1453 NULL
, /* nb_divmod */
1454 valpy_power
, /* nb_power */
1455 valpy_negative
, /* nb_negative */
1456 valpy_positive
, /* nb_positive */
1457 valpy_absolute
, /* nb_absolute */
1458 valpy_nonzero
, /* nb_nonzero */
1459 valpy_invert
, /* nb_invert */
1460 valpy_lsh
, /* nb_lshift */
1461 valpy_rsh
, /* nb_rshift */
1462 valpy_and
, /* nb_and */
1463 valpy_xor
, /* nb_xor */
1464 valpy_or
, /* nb_or */
1466 valpy_long
, /* nb_int */
1467 NULL
, /* reserved */
1469 NULL
, /* nb_coerce */
1470 valpy_int
, /* nb_int */
1471 valpy_long
, /* nb_long */
1473 valpy_float
, /* nb_float */
1478 NULL
, /* nb_inplace_add */
1479 NULL
, /* nb_inplace_subtract */
1480 NULL
, /* nb_inplace_multiply */
1481 NULL
, /* nb_inplace_remainder */
1482 NULL
, /* nb_inplace_power */
1483 NULL
, /* nb_inplace_lshift */
1484 NULL
, /* nb_inplace_rshift */
1485 NULL
, /* nb_inplace_and */
1486 NULL
, /* nb_inplace_xor */
1487 NULL
, /* nb_inplace_or */
1488 NULL
, /* nb_floor_divide */
1489 valpy_divide
/* nb_true_divide */
1492 static PyMappingMethods value_object_as_mapping
= {
1498 PyTypeObject value_object_type
= {
1499 PyVarObject_HEAD_INIT (NULL
, 0)
1500 "gdb.Value", /*tp_name*/
1501 sizeof (value_object
), /*tp_basicsize*/
1503 valpy_dealloc
, /*tp_dealloc*/
1509 &value_object_as_number
, /*tp_as_number*/
1510 0, /*tp_as_sequence*/
1511 &value_object_as_mapping
, /*tp_as_mapping*/
1512 valpy_hash
, /*tp_hash*/
1513 valpy_call
, /*tp_call*/
1514 valpy_str
, /*tp_str*/
1518 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
1519 | Py_TPFLAGS_BASETYPE
, /*tp_flags*/
1520 "GDB value object", /* tp_doc */
1521 0, /* tp_traverse */
1523 valpy_richcompare
, /* tp_richcompare */
1524 0, /* tp_weaklistoffset */
1526 0, /* tp_iternext */
1527 value_object_methods
, /* tp_methods */
1529 value_object_getset
, /* tp_getset */
1532 0, /* tp_descr_get */
1533 0, /* tp_descr_set */
1534 0, /* tp_dictoffset */
1537 valpy_new
/* tp_new */
1543 preserve_python_values (struct objfile
*objfile
, htab_t copied_types
)
1548 #endif /* HAVE_PYTHON */