1 /* Python interface to values.
3 Copyright (C) 2008, 2009 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"
31 #include "python-internal.h"
33 /* Even though Python scalar types directly map to host types, we use
34 target types here to remain consistent with the the values system in
35 GDB (which uses target arithmetic). */
37 /* Python's integer type corresponds to C's long type. */
38 #define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long
40 /* Python's float type corresponds to C's double type. */
41 #define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double
43 /* Python's long type corresponds to C's long long type. */
44 #define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long
46 #define builtin_type_pybool \
47 language_bool_type (python_language, python_gdbarch)
49 #define builtin_type_pychar \
50 language_string_char_type (python_language, python_gdbarch)
52 typedef struct value_object
{
54 struct value_object
*next
;
55 struct value_object
*prev
;
61 /* List of all values which are currently exposed to Python. It is
62 maintained so that when an objfile is discarded, preserve_values
63 can copy the values' types if needed. */
64 /* This variable is unnecessarily initialized to NULL in order to
65 work around a linker bug on MacOS. */
66 static value_object
*values_in_python
= NULL
;
68 /* Called by the Python interpreter when deallocating a value object. */
70 valpy_dealloc (PyObject
*obj
)
72 value_object
*self
= (value_object
*) obj
;
74 /* Remove SELF from the global list. */
76 self
->prev
->next
= self
->next
;
79 gdb_assert (values_in_python
== self
);
80 values_in_python
= self
->next
;
83 self
->next
->prev
= self
->prev
;
85 value_free (self
->value
);
88 /* Use braces to appease gcc warning. *sigh* */
90 Py_DECREF (self
->address
);
95 Py_DECREF (self
->type
);
98 self
->ob_type
->tp_free (self
);
101 /* Helper to push a Value object on the global list. */
103 note_value (value_object
*value_obj
)
105 value_obj
->next
= values_in_python
;
107 value_obj
->next
->prev
= value_obj
;
108 value_obj
->prev
= NULL
;
109 values_in_python
= value_obj
;
112 /* Called when a new gdb.Value object needs to be allocated. */
114 valpy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*keywords
)
116 struct value
*value
= NULL
; /* Initialize to appease gcc warning. */
117 value_object
*value_obj
;
119 if (PyTuple_Size (args
) != 1)
121 PyErr_SetString (PyExc_TypeError
, _("Value object creation takes only "
126 value_obj
= (value_object
*) subtype
->tp_alloc (subtype
, 1);
127 if (value_obj
== NULL
)
129 PyErr_SetString (PyExc_MemoryError
, _("Could not allocate memory to "
130 "create Value object."));
134 value
= convert_value_from_python (PyTuple_GetItem (args
, 0));
137 subtype
->tp_free (value_obj
);
141 value_obj
->value
= value
;
142 value_incref (value
);
143 value_obj
->address
= NULL
;
144 value_obj
->type
= NULL
;
145 note_value (value_obj
);
147 return (PyObject
*) value_obj
;
150 /* Iterate over all the Value objects, calling preserve_one_value on
153 preserve_python_values (struct objfile
*objfile
, htab_t copied_types
)
157 for (iter
= values_in_python
; iter
; iter
= iter
->next
)
158 preserve_one_value (iter
->value
, objfile
, copied_types
);
161 /* Given a value of a pointer type, apply the C unary * operator to it. */
163 valpy_dereference (PyObject
*self
, PyObject
*args
)
165 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
166 volatile struct gdb_exception except
;
168 TRY_CATCH (except
, RETURN_MASK_ALL
)
170 res_val
= value_ind (((value_object
*) self
)->value
);
172 GDB_PY_HANDLE_EXCEPTION (except
);
174 return value_to_value_object (res_val
);
177 /* Return "&value". */
179 valpy_get_address (PyObject
*self
, void *closure
)
181 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
182 value_object
*val_obj
= (value_object
*) self
;
183 volatile struct gdb_exception except
;
185 if (!val_obj
->address
)
187 TRY_CATCH (except
, RETURN_MASK_ALL
)
189 res_val
= value_addr (val_obj
->value
);
191 if (except
.reason
< 0)
193 val_obj
->address
= Py_None
;
197 val_obj
->address
= value_to_value_object (res_val
);
200 Py_INCREF (val_obj
->address
);
202 return val_obj
->address
;
205 /* Return type of the value. */
207 valpy_get_type (PyObject
*self
, void *closure
)
209 value_object
*obj
= (value_object
*) self
;
212 obj
->type
= type_to_type_object (value_type (obj
->value
));
216 Py_INCREF (obj
->type
);
219 Py_INCREF (obj
->type
);
223 /* Implementation of gdb.Value.string ([encoding] [, errors]
224 [, length]) -> string. Return Unicode string with value contents.
225 If ENCODING is not given, the string is assumed to be encoded in
226 the target's charset. If LENGTH is provided, only fetch string to
227 the length provided. */
230 valpy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
232 int length
= -1, ret
= 0;
234 struct value
*value
= ((value_object
*) self
)->value
;
235 volatile struct gdb_exception except
;
237 const char *encoding
= NULL
;
238 const char *errors
= NULL
;
239 const char *user_encoding
= NULL
;
240 const char *la_encoding
= NULL
;
241 struct type
*char_type
;
242 static char *keywords
[] = { "encoding", "errors", "length", NULL
};
244 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|ssi", keywords
,
245 &user_encoding
, &errors
, &length
))
248 TRY_CATCH (except
, RETURN_MASK_ALL
)
250 LA_GET_STRING (value
, &buffer
, &length
, &char_type
, &la_encoding
);
252 GDB_PY_HANDLE_EXCEPTION (except
);
254 encoding
= (user_encoding
&& *user_encoding
) ? user_encoding
: la_encoding
;
255 unicode
= PyUnicode_Decode (buffer
, length
* TYPE_LENGTH (char_type
),
262 /* Cast a value to a given type. */
264 valpy_cast (PyObject
*self
, PyObject
*args
)
268 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
269 volatile struct gdb_exception except
;
271 if (! PyArg_ParseTuple (args
, "O", &type_obj
))
274 type
= type_object_to_type (type_obj
);
277 PyErr_SetString (PyExc_RuntimeError
, "argument must be a Type");
281 TRY_CATCH (except
, RETURN_MASK_ALL
)
283 res_val
= value_cast (type
, ((value_object
*) self
)->value
);
285 GDB_PY_HANDLE_EXCEPTION (except
);
287 return value_to_value_object (res_val
);
291 valpy_length (PyObject
*self
)
293 /* We don't support getting the number of elements in a struct / class. */
294 PyErr_SetString (PyExc_NotImplementedError
,
295 "Invalid operation on gdb.Value.");
299 /* Given string name of an element inside structure, return its value
302 valpy_getitem (PyObject
*self
, PyObject
*key
)
304 value_object
*self_value
= (value_object
*) self
;
306 struct value
*res_val
= NULL
;
307 volatile struct gdb_exception except
;
309 if (gdbpy_is_string (key
))
311 field
= python_string_to_host_string (key
);
316 TRY_CATCH (except
, RETURN_MASK_ALL
)
318 struct value
*tmp
= self_value
->value
;
321 res_val
= value_struct_elt (&tmp
, NULL
, field
, 0, NULL
);
324 /* Assume we are attempting an array access, and let the
325 value code throw an exception if the index has an invalid
327 struct value
*idx
= convert_value_from_python (key
);
330 /* Check the value's type is something that can be accessed via
333 tmp
= coerce_ref (tmp
);
334 type
= check_typedef (value_type (tmp
));
335 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
336 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
337 error( _("Cannot subscript requested type"));
339 res_val
= value_subscript (tmp
, value_as_long (idx
));
345 GDB_PY_HANDLE_EXCEPTION (except
);
347 return res_val
? value_to_value_object (res_val
) : NULL
;
351 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
353 PyErr_Format (PyExc_NotImplementedError
,
354 _("Setting of struct elements is not currently supported."));
358 /* Called by the Python interpreter to obtain string representation
361 valpy_str (PyObject
*self
)
365 struct cleanup
*old_chain
;
367 struct value_print_options opts
;
368 volatile struct gdb_exception except
;
370 get_user_print_options (&opts
);
373 stb
= mem_fileopen ();
374 old_chain
= make_cleanup_ui_file_delete (stb
);
376 TRY_CATCH (except
, RETURN_MASK_ALL
)
378 common_val_print (((value_object
*) self
)->value
, stb
, 0,
379 &opts
, python_language
);
380 s
= ui_file_xstrdup (stb
, NULL
);
382 GDB_PY_HANDLE_EXCEPTION (except
);
384 do_cleanups (old_chain
);
386 result
= PyUnicode_Decode (s
, strlen (s
), host_charset (), NULL
);
392 /* Implements gdb.Value.is_optimized_out. */
394 valpy_get_is_optimized_out (PyObject
*self
, void *closure
)
396 struct value
*value
= ((value_object
*) self
)->value
;
398 if (value_optimized_out (value
))
419 /* If TYPE is a reference, return the target; otherwise return TYPE. */
420 #define STRIP_REFERENCE(TYPE) \
421 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
423 /* Returns a value object which is the result of applying the operation
424 specified by OPCODE to the given arguments. */
426 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
428 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
429 volatile struct gdb_exception except
;
431 TRY_CATCH (except
, RETURN_MASK_ALL
)
433 struct value
*arg1
, *arg2
;
435 /* If the gdb.Value object is the second operand, then it will be passed
436 to us as the OTHER argument, and SELF will be an entirely different
437 kind of object, altogether. Because of this, we can't assume self is
438 a gdb.Value object and need to convert it from python as well. */
439 arg1
= convert_value_from_python (self
);
443 arg2
= convert_value_from_python (other
);
451 struct type
*ltype
= value_type (arg1
);
452 struct type
*rtype
= value_type (arg2
);
454 CHECK_TYPEDEF (ltype
);
455 ltype
= STRIP_REFERENCE (ltype
);
456 CHECK_TYPEDEF (rtype
);
457 rtype
= STRIP_REFERENCE (rtype
);
459 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
460 && is_integral_type (rtype
))
461 res_val
= value_ptradd (arg1
, value_as_long (arg2
));
462 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
463 && is_integral_type (ltype
))
464 res_val
= value_ptradd (arg2
, value_as_long (arg1
));
466 res_val
= value_binop (arg1
, arg2
, BINOP_ADD
);
471 struct type
*ltype
= value_type (arg1
);
472 struct type
*rtype
= value_type (arg2
);
474 CHECK_TYPEDEF (ltype
);
475 ltype
= STRIP_REFERENCE (ltype
);
476 CHECK_TYPEDEF (rtype
);
477 rtype
= STRIP_REFERENCE (rtype
);
479 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
480 && TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
481 /* A ptrdiff_t for the target would be preferable here. */
482 res_val
= value_from_longest (builtin_type_pyint
,
483 value_ptrdiff (arg1
, arg2
));
484 else if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
485 && is_integral_type (rtype
))
486 res_val
= value_ptradd (arg1
, - value_as_long (arg2
));
488 res_val
= value_binop (arg1
, arg2
, BINOP_SUB
);
492 res_val
= value_binop (arg1
, arg2
, BINOP_MUL
);
495 res_val
= value_binop (arg1
, arg2
, BINOP_DIV
);
498 res_val
= value_binop (arg1
, arg2
, BINOP_REM
);
501 res_val
= value_binop (arg1
, arg2
, BINOP_EXP
);
504 res_val
= value_binop (arg1
, arg2
, BINOP_LSH
);
507 res_val
= value_binop (arg1
, arg2
, BINOP_RSH
);
510 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_AND
);
513 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_IOR
);
516 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_XOR
);
520 GDB_PY_HANDLE_EXCEPTION (except
);
522 return res_val
? value_to_value_object (res_val
) : NULL
;
526 valpy_add (PyObject
*self
, PyObject
*other
)
528 return valpy_binop (VALPY_ADD
, self
, other
);
532 valpy_subtract (PyObject
*self
, PyObject
*other
)
534 return valpy_binop (VALPY_SUB
, self
, other
);
538 valpy_multiply (PyObject
*self
, PyObject
*other
)
540 return valpy_binop (VALPY_MUL
, self
, other
);
544 valpy_divide (PyObject
*self
, PyObject
*other
)
546 return valpy_binop (VALPY_DIV
, self
, other
);
550 valpy_remainder (PyObject
*self
, PyObject
*other
)
552 return valpy_binop (VALPY_REM
, self
, other
);
556 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
558 /* We don't support the ternary form of pow. I don't know how to express
559 that, so let's just throw NotImplementedError to at least do something
561 if (unused
!= Py_None
)
563 PyErr_SetString (PyExc_NotImplementedError
,
564 "Invalid operation on gdb.Value.");
568 return valpy_binop (VALPY_POW
, self
, other
);
572 valpy_negative (PyObject
*self
)
574 struct value
*val
= NULL
;
575 volatile struct gdb_exception except
;
577 TRY_CATCH (except
, RETURN_MASK_ALL
)
579 val
= value_neg (((value_object
*) self
)->value
);
581 GDB_PY_HANDLE_EXCEPTION (except
);
583 return value_to_value_object (val
);
587 valpy_positive (PyObject
*self
)
589 return value_to_value_object (((value_object
*) self
)->value
);
593 valpy_absolute (PyObject
*self
)
595 struct value
*value
= ((value_object
*) self
)->value
;
596 if (value_less (value
, value_zero (value_type (value
), not_lval
)))
597 return valpy_negative (self
);
599 return valpy_positive (self
);
602 /* Implements boolean evaluation of gdb.Value. */
604 valpy_nonzero (PyObject
*self
)
606 value_object
*self_value
= (value_object
*) self
;
609 type
= check_typedef (value_type (self_value
->value
));
611 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
612 return !!value_as_long (self_value
->value
);
613 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
614 return value_as_double (self_value
->value
) != 0;
615 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
616 return !decimal_is_zero (value_contents (self_value
->value
),
618 gdbarch_byte_order (get_type_arch (type
)));
621 PyErr_SetString (PyExc_TypeError
, _("Attempted truth testing on invalid "
627 /* Implements ~ for value objects. */
629 valpy_invert (PyObject
*self
)
631 struct value
*val
= NULL
;
632 volatile struct gdb_exception except
;
634 TRY_CATCH (except
, RETURN_MASK_ALL
)
636 val
= value_complement (((value_object
*) self
)->value
);
638 GDB_PY_HANDLE_EXCEPTION (except
);
640 return value_to_value_object (val
);
643 /* Implements left shift for value objects. */
645 valpy_lsh (PyObject
*self
, PyObject
*other
)
647 return valpy_binop (VALPY_LSH
, self
, other
);
650 /* Implements right shift for value objects. */
652 valpy_rsh (PyObject
*self
, PyObject
*other
)
654 return valpy_binop (VALPY_RSH
, self
, other
);
657 /* Implements bitwise and for value objects. */
659 valpy_and (PyObject
*self
, PyObject
*other
)
661 return valpy_binop (VALPY_BITAND
, self
, other
);
664 /* Implements bitwise or for value objects. */
666 valpy_or (PyObject
*self
, PyObject
*other
)
668 return valpy_binop (VALPY_BITOR
, self
, other
);
671 /* Implements bitwise xor for value objects. */
673 valpy_xor (PyObject
*self
, PyObject
*other
)
675 return valpy_binop (VALPY_BITXOR
, self
, other
);
678 /* Implements comparison operations for value objects. */
680 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
683 struct value
*value_other
;
684 volatile struct gdb_exception except
;
686 if (other
== Py_None
)
687 /* Comparing with None is special. From what I can tell, in Python
688 None is smaller than anything else. */
700 PyErr_SetString (PyExc_NotImplementedError
,
701 "Invalid operation on gdb.Value.");
705 TRY_CATCH (except
, RETURN_MASK_ALL
)
707 value_other
= convert_value_from_python (other
);
708 if (value_other
== NULL
)
716 result
= value_less (((value_object
*) self
)->value
, value_other
);
719 result
= value_less (((value_object
*) self
)->value
, value_other
)
720 || value_equal (((value_object
*) self
)->value
, value_other
);
723 result
= value_equal (((value_object
*) self
)->value
, value_other
);
726 result
= !value_equal (((value_object
*) self
)->value
, value_other
);
729 result
= value_less (value_other
, ((value_object
*) self
)->value
);
732 result
= value_less (value_other
, ((value_object
*) self
)->value
)
733 || value_equal (((value_object
*) self
)->value
, value_other
);
737 PyErr_SetString (PyExc_NotImplementedError
,
738 "Invalid operation on gdb.Value.");
743 GDB_PY_HANDLE_EXCEPTION (except
);
745 /* In this case, the Python exception has already been set. */
755 /* Helper function to determine if a type is "int-like". */
757 is_intlike (struct type
*type
, int ptr_ok
)
759 CHECK_TYPEDEF (type
);
760 return (TYPE_CODE (type
) == TYPE_CODE_INT
761 || TYPE_CODE (type
) == TYPE_CODE_ENUM
762 || TYPE_CODE (type
) == TYPE_CODE_BOOL
763 || TYPE_CODE (type
) == TYPE_CODE_CHAR
764 || (ptr_ok
&& TYPE_CODE (type
) == TYPE_CODE_PTR
));
767 /* Implements conversion to int. */
769 valpy_int (PyObject
*self
)
771 struct value
*value
= ((value_object
*) self
)->value
;
772 struct type
*type
= value_type (value
);
774 volatile struct gdb_exception except
;
776 CHECK_TYPEDEF (type
);
777 if (!is_intlike (type
, 0))
779 PyErr_SetString (PyExc_RuntimeError
, "cannot convert value to int");
783 TRY_CATCH (except
, RETURN_MASK_ALL
)
785 l
= value_as_long (value
);
787 GDB_PY_HANDLE_EXCEPTION (except
);
789 #ifdef HAVE_LONG_LONG /* Defined by Python. */
790 /* If we have 'long long', and the value overflows a 'long', use a
791 Python Long; otherwise use a Python Int. */
792 if (sizeof (l
) > sizeof (long) && (l
> PyInt_GetMax ()
793 || l
< (- (LONGEST
) PyInt_GetMax ()) - 1))
794 return PyLong_FromLongLong (l
);
796 return PyInt_FromLong (l
);
799 /* Implements conversion to long. */
801 valpy_long (PyObject
*self
)
803 struct value
*value
= ((value_object
*) self
)->value
;
804 struct type
*type
= value_type (value
);
806 volatile struct gdb_exception except
;
808 if (!is_intlike (type
, 1))
810 PyErr_SetString (PyExc_RuntimeError
, "cannot convert value to long");
814 TRY_CATCH (except
, RETURN_MASK_ALL
)
816 l
= value_as_long (value
);
818 GDB_PY_HANDLE_EXCEPTION (except
);
820 #ifdef HAVE_LONG_LONG /* Defined by Python. */
821 return PyLong_FromLongLong (l
);
823 return PyLong_FromLong (l
);
827 /* Implements conversion to float. */
829 valpy_float (PyObject
*self
)
831 struct value
*value
= ((value_object
*) self
)->value
;
832 struct type
*type
= value_type (value
);
834 volatile struct gdb_exception except
;
836 CHECK_TYPEDEF (type
);
837 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
839 PyErr_SetString (PyExc_RuntimeError
, "cannot convert value to float");
843 TRY_CATCH (except
, RETURN_MASK_ALL
)
845 d
= value_as_double (value
);
847 GDB_PY_HANDLE_EXCEPTION (except
);
849 return PyFloat_FromDouble (d
);
852 /* Returns an object for a value which is released from the all_values chain,
853 so its lifetime is not bound to the execution of a command. */
855 value_to_value_object (struct value
*val
)
857 value_object
*val_obj
;
859 val_obj
= PyObject_New (value_object
, &value_object_type
);
862 val_obj
->value
= val
;
864 val_obj
->address
= NULL
;
865 val_obj
->type
= NULL
;
866 note_value (val_obj
);
869 return (PyObject
*) val_obj
;
872 /* Returns a borrowed reference to the struct value corresponding to
873 the given value object. */
875 value_object_to_value (PyObject
*self
)
878 if (! PyObject_TypeCheck (self
, &value_object_type
))
880 real
= (value_object
*) self
;
884 /* Try to convert a Python value to a gdb value. If the value cannot
885 be converted, set a Python exception and return NULL. Returns a
886 reference to a new value on the all_values chain. */
889 convert_value_from_python (PyObject
*obj
)
891 struct value
*value
= NULL
; /* -Wall */
892 PyObject
*target_str
, *unicode_str
;
894 volatile struct gdb_exception except
;
897 gdb_assert (obj
!= NULL
);
899 TRY_CATCH (except
, RETURN_MASK_ALL
)
901 if (PyBool_Check (obj
))
903 cmp
= PyObject_IsTrue (obj
);
905 value
= value_from_longest (builtin_type_pybool
, cmp
);
907 else if (PyInt_Check (obj
))
909 long l
= PyInt_AsLong (obj
);
911 if (! PyErr_Occurred ())
912 value
= value_from_longest (builtin_type_pyint
, l
);
914 else if (PyLong_Check (obj
))
916 LONGEST l
= PyLong_AsLongLong (obj
);
918 if (! PyErr_Occurred ())
919 value
= value_from_longest (builtin_type_pylong
, l
);
921 else if (PyFloat_Check (obj
))
923 double d
= PyFloat_AsDouble (obj
);
925 if (! PyErr_Occurred ())
926 value
= value_from_double (builtin_type_pyfloat
, d
);
928 else if (gdbpy_is_string (obj
))
932 s
= python_string_to_target_string (obj
);
935 old
= make_cleanup (xfree
, s
);
936 value
= value_cstring (s
, strlen (s
), builtin_type_pychar
);
940 else if (PyObject_TypeCheck (obj
, &value_object_type
))
941 value
= value_copy (((value_object
*) obj
)->value
);
943 PyErr_Format (PyExc_TypeError
, _("Could not convert Python object: %s"),
944 PyString_AsString (PyObject_Str (obj
)));
946 if (except
.reason
< 0)
948 PyErr_Format (except
.reason
== RETURN_QUIT
949 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
950 "%s", except
.message
);
957 /* Returns value object in the ARGth position in GDB's history. */
959 gdbpy_history (PyObject
*self
, PyObject
*args
)
962 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
963 volatile struct gdb_exception except
;
965 if (!PyArg_ParseTuple (args
, "i", &i
))
968 TRY_CATCH (except
, RETURN_MASK_ALL
)
970 res_val
= access_value_history (i
);
972 GDB_PY_HANDLE_EXCEPTION (except
);
974 return value_to_value_object (res_val
);
978 gdbpy_initialize_values (void)
980 if (PyType_Ready (&value_object_type
) < 0)
983 Py_INCREF (&value_object_type
);
984 PyModule_AddObject (gdb_module
, "Value", (PyObject
*) &value_object_type
);
986 values_in_python
= NULL
;
991 static PyGetSetDef value_object_getset
[] = {
992 { "address", valpy_get_address
, NULL
, "The address of the value.",
994 { "is_optimized_out", valpy_get_is_optimized_out
, NULL
,
995 "Boolean telling whether the value is optimized out (i.e., not available).",
997 { "type", valpy_get_type
, NULL
, "Type of the value.", NULL
},
998 {NULL
} /* Sentinel */
1001 static PyMethodDef value_object_methods
[] = {
1002 { "cast", valpy_cast
, METH_VARARGS
, "Cast the value to the supplied type." },
1003 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
1004 { "string", (PyCFunction
) valpy_string
, METH_VARARGS
| METH_KEYWORDS
,
1005 "string ([encoding] [, errors] [, length]) -> string\n\
1006 Return Unicode string representation of the value." },
1007 {NULL
} /* Sentinel */
1010 static PyNumberMethods value_object_as_number
= {
1016 NULL
, /* nb_divmod */
1017 valpy_power
, /* nb_power */
1018 valpy_negative
, /* nb_negative */
1019 valpy_positive
, /* nb_positive */
1020 valpy_absolute
, /* nb_absolute */
1021 valpy_nonzero
, /* nb_nonzero */
1022 valpy_invert
, /* nb_invert */
1023 valpy_lsh
, /* nb_lshift */
1024 valpy_rsh
, /* nb_rshift */
1025 valpy_and
, /* nb_and */
1026 valpy_xor
, /* nb_xor */
1027 valpy_or
, /* nb_or */
1028 NULL
, /* nb_coerce */
1029 valpy_int
, /* nb_int */
1030 valpy_long
, /* nb_long */
1031 valpy_float
, /* nb_float */
1036 static PyMappingMethods value_object_as_mapping
= {
1042 PyTypeObject value_object_type
= {
1043 PyObject_HEAD_INIT (NULL
)
1045 "gdb.Value", /*tp_name*/
1046 sizeof (value_object
), /*tp_basicsize*/
1048 valpy_dealloc
, /*tp_dealloc*/
1054 &value_object_as_number
, /*tp_as_number*/
1055 0, /*tp_as_sequence*/
1056 &value_object_as_mapping
, /*tp_as_mapping*/
1059 valpy_str
, /*tp_str*/
1063 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
, /*tp_flags*/
1064 "GDB value object", /* tp_doc */
1065 0, /* tp_traverse */
1067 valpy_richcompare
, /* tp_richcompare */
1068 0, /* tp_weaklistoffset */
1070 0, /* tp_iternext */
1071 value_object_methods
, /* tp_methods */
1073 value_object_getset
, /* tp_getset */
1076 0, /* tp_descr_get */
1077 0, /* tp_descr_set */
1078 0, /* tp_dictoffset */
1081 valpy_new
/* tp_new */
1087 preserve_python_values (struct objfile
*objfile
, htab_t copied_types
)
1092 #endif /* HAVE_PYTHON */