1 /* Python interface to values.
3 Copyright (C) 2008 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/>. */
23 #include "exceptions.h"
27 /* List of all values which are currently exposed to Python. It is
28 maintained so that when an objfile is discarded, preserve_values
29 can copy the values' types if needed. This is declared
30 unconditionally to reduce the number of uses of HAVE_PYTHON in the
32 struct value
*values_in_python
;
36 #include "python-internal.h"
38 /* Even though Python scalar types directly map to host types, we use
39 target types here to remain consistent with the the values system in
40 GDB (which uses target arithmetic). */
42 /* Python's integer type corresponds to C's long type. */
43 #define builtin_type_pyint builtin_type (current_gdbarch)->builtin_long
45 /* Python's float type corresponds to C's double type. */
46 #define builtin_type_pyfloat builtin_type (current_gdbarch)->builtin_double
48 /* Python's long type corresponds to C's long long type. */
49 #define builtin_type_pylong builtin_type (current_gdbarch)->builtin_long_long
51 #define builtin_type_pybool \
52 language_bool_type (current_language, current_gdbarch)
60 static void valpy_dealloc (PyObject
*obj
);
61 static PyObject
*valpy_new (PyTypeObject
*subtype
, PyObject
*args
,
63 static Py_ssize_t
valpy_length (PyObject
*self
);
64 static PyObject
*valpy_getitem (PyObject
*self
, PyObject
*key
);
65 static int valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
);
66 static PyObject
*valpy_str (PyObject
*self
);
67 static PyObject
*valpy_add (PyObject
*self
, PyObject
*other
);
68 static PyObject
*valpy_subtract (PyObject
*self
, PyObject
*other
);
69 static PyObject
*valpy_multiply (PyObject
*self
, PyObject
*other
);
70 static PyObject
*valpy_divide (PyObject
*self
, PyObject
*other
);
71 static PyObject
*valpy_remainder (PyObject
*self
, PyObject
*other
);
72 static PyObject
*valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
);
73 static PyObject
*valpy_negative (PyObject
*self
);
74 static PyObject
*valpy_positive (PyObject
*self
);
75 static PyObject
*valpy_absolute (PyObject
*self
);
76 static int valpy_nonzero (PyObject
*self
);
77 static PyObject
*valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
);
78 static PyObject
*valpy_dereference (PyObject
*self
, PyObject
*args
);
80 static PyMethodDef value_object_methods
[] = {
81 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
85 static PyNumberMethods value_object_as_number
= {
92 valpy_power
, /* nb_power */
93 valpy_negative
, /* nb_negative */
94 valpy_positive
, /* nb_positive */
95 valpy_absolute
, /* nb_absolute */
96 valpy_nonzero
/* nb_nonzero */
99 static PyMappingMethods value_object_as_mapping
= {
105 PyTypeObject value_object_type
= {
106 PyObject_HEAD_INIT (NULL
)
108 "gdb.Value", /*tp_name*/
109 sizeof (value_object
), /*tp_basicsize*/
111 valpy_dealloc
, /*tp_dealloc*/
117 &value_object_as_number
, /*tp_as_number*/
118 0, /*tp_as_sequence*/
119 &value_object_as_mapping
, /*tp_as_mapping*/
122 valpy_str
, /*tp_str*/
126 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
, /*tp_flags*/
127 "GDB value object", /* tp_doc */
130 valpy_richcompare
, /* tp_richcompare */
131 0, /* tp_weaklistoffset */
134 value_object_methods
/* tp_methods */
138 /* Called by the Python interpreter when deallocating a value object. */
140 valpy_dealloc (PyObject
*obj
)
142 value_object
*self
= (value_object
*) obj
;
144 value_remove_from_list (&values_in_python
, self
->value
);
146 if (!self
->owned_by_gdb
)
147 value_free (self
->value
);
148 self
->ob_type
->tp_free (self
);
151 /* Called when a new gdb.Value object needs to be allocated. */
153 valpy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*keywords
)
155 struct value
*value
= NULL
; /* Initialize to appease gcc warning. */
156 value_object
*value_obj
;
157 volatile struct gdb_exception except
;
159 if (PyTuple_Size (args
) != 1)
161 PyErr_SetString (PyExc_TypeError
, _("Value object creation takes only "
166 value_obj
= (value_object
*) subtype
->tp_alloc (subtype
, 1);
167 if (value_obj
== NULL
)
169 PyErr_SetString (PyExc_MemoryError
, _("Could not allocate memory to "
170 "create Value object."));
174 TRY_CATCH (except
, RETURN_MASK_ALL
)
176 value
= convert_value_from_python (PyTuple_GetItem (args
, 0));
178 if (except
.reason
< 0)
180 subtype
->tp_free (value_obj
);
181 return PyErr_Format (except
.reason
== RETURN_QUIT
182 ? PyExc_KeyboardInterrupt
: PyExc_TypeError
,
183 "%s", except
.message
);
186 value_obj
->value
= value
;
187 release_value (value
);
188 value_prepend_to_list (&values_in_python
, value
);
190 return (PyObject
*) value_obj
;
193 /* Given a value of a pointer type, apply the C unary * operator to it. */
195 valpy_dereference (PyObject
*self
, PyObject
*args
)
197 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
198 volatile struct gdb_exception except
;
200 TRY_CATCH (except
, RETURN_MASK_ALL
)
202 res_val
= value_ind (((value_object
*) self
)->value
);
204 GDB_PY_HANDLE_EXCEPTION (except
);
206 return value_to_value_object (res_val
);
210 valpy_length (PyObject
*self
)
212 /* We don't support getting the number of elements in a struct / class. */
213 PyErr_SetString (PyExc_NotImplementedError
,
214 "Invalid operation on gdb.Value.");
218 /* Given string name of an element inside structure, return its value
221 valpy_getitem (PyObject
*self
, PyObject
*key
)
223 value_object
*self_value
= (value_object
*) self
;
225 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
227 volatile struct gdb_exception except
;
229 field
= python_string_to_target_string (key
);
233 old
= make_cleanup (xfree
, field
);
235 TRY_CATCH (except
, RETURN_MASK_ALL
)
237 res_val
= value_struct_elt (&self_value
->value
, NULL
, field
, 0, NULL
);
239 GDB_PY_HANDLE_EXCEPTION (except
);
243 return value_to_value_object (res_val
);
247 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
249 PyErr_Format (PyExc_NotImplementedError
,
250 _("Setting of struct elements is not currently supported."));
254 /* Called by the Python interpreter to obtain string representation
257 valpy_str (PyObject
*self
)
262 struct cleanup
*old_chain
;
264 volatile struct gdb_exception except
;
266 stb
= mem_fileopen ();
267 old_chain
= make_cleanup_ui_file_delete (stb
);
269 TRY_CATCH (except
, RETURN_MASK_ALL
)
271 common_val_print (((value_object
*) self
)->value
, stb
, 0, 0, 0,
272 Val_pretty_default
, current_language
);
273 s
= ui_file_xstrdup (stb
, &dummy
);
275 GDB_PY_HANDLE_EXCEPTION (except
);
277 do_cleanups (old_chain
);
279 result
= PyUnicode_Decode (s
, strlen (s
), host_charset (), NULL
);
295 /* If TYPE is a reference, return the target; otherwise return TYPE. */
296 #define STRIP_REFERENCE(TYPE) \
297 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
299 /* Returns a value object which is the result of applying the operation
300 specified by OPCODE to the given arguments. */
302 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
304 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
305 volatile struct gdb_exception except
;
307 TRY_CATCH (except
, RETURN_MASK_ALL
)
309 struct value
*arg1
, *arg2
;
311 /* If the gdb.Value object is the second operand, then it will be passed
312 to us as the OTHER argument, and SELF will be an entirely different
313 kind of object, altogether. Because of this, we can't assume self is
314 a gdb.Value object and need to convert it from python as well. */
315 arg1
= convert_value_from_python (self
);
316 arg2
= convert_value_from_python (other
);
322 struct type
*ltype
= value_type (arg1
);
323 struct type
*rtype
= value_type (arg2
);
325 CHECK_TYPEDEF (ltype
);
326 ltype
= STRIP_REFERENCE (ltype
);
327 CHECK_TYPEDEF (rtype
);
328 rtype
= STRIP_REFERENCE (rtype
);
330 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
)
331 res_val
= value_ptradd (arg1
, arg2
);
332 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
333 res_val
= value_ptradd (arg2
, arg1
);
335 res_val
= value_binop (arg1
, arg2
, BINOP_ADD
);
340 struct type
*ltype
= value_type (arg1
);
341 struct type
*rtype
= value_type (arg2
);
343 CHECK_TYPEDEF (ltype
);
344 ltype
= STRIP_REFERENCE (ltype
);
345 CHECK_TYPEDEF (rtype
);
346 rtype
= STRIP_REFERENCE (rtype
);
348 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
)
350 if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
351 /* A ptrdiff_t for the target would be preferable
353 res_val
= value_from_longest (builtin_type_pyint
,
354 value_ptrdiff (arg1
, arg2
));
356 res_val
= value_ptrsub (arg1
, arg2
);
359 res_val
= value_binop (arg1
, arg2
, BINOP_SUB
);
363 res_val
= value_binop (arg1
, arg2
, BINOP_MUL
);
366 res_val
= value_binop (arg1
, arg2
, BINOP_DIV
);
369 res_val
= value_binop (arg1
, arg2
, BINOP_REM
);
372 res_val
= value_binop (arg1
, arg2
, BINOP_EXP
);
376 GDB_PY_HANDLE_EXCEPTION (except
);
378 return value_to_value_object (res_val
);
382 valpy_add (PyObject
*self
, PyObject
*other
)
384 return valpy_binop (VALPY_ADD
, self
, other
);
388 valpy_subtract (PyObject
*self
, PyObject
*other
)
390 return valpy_binop (VALPY_SUB
, self
, other
);
394 valpy_multiply (PyObject
*self
, PyObject
*other
)
396 return valpy_binop (VALPY_MUL
, self
, other
);
400 valpy_divide (PyObject
*self
, PyObject
*other
)
402 return valpy_binop (VALPY_DIV
, self
, other
);
406 valpy_remainder (PyObject
*self
, PyObject
*other
)
408 return valpy_binop (VALPY_REM
, self
, other
);
412 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
414 /* We don't support the ternary form of pow. I don't know how to express
415 that, so let's just throw NotImplementedError to at least do something
417 if (unused
!= Py_None
)
419 PyErr_SetString (PyExc_NotImplementedError
,
420 "Invalid operation on gdb.Value.");
424 return valpy_binop (VALPY_POW
, self
, other
);
428 valpy_negative (PyObject
*self
)
430 struct value
*val
= NULL
;
431 volatile struct gdb_exception except
;
433 TRY_CATCH (except
, RETURN_MASK_ALL
)
435 val
= value_neg (((value_object
*) self
)->value
);
437 GDB_PY_HANDLE_EXCEPTION (except
);
439 return value_to_value_object (val
);
443 valpy_positive (PyObject
*self
)
445 struct value
*copy
= value_copy (((value_object
*) self
)->value
);
447 return value_to_value_object (copy
);
451 valpy_absolute (PyObject
*self
)
453 if (value_less (((value_object
*) self
)->value
,
454 value_from_longest (builtin_type_int8
, 0)))
455 return valpy_negative (self
);
457 return valpy_positive (self
);
460 /* Implements boolean evaluation of gdb.Value. */
462 valpy_nonzero (PyObject
*self
)
464 value_object
*self_value
= (value_object
*) self
;
467 type
= check_typedef (value_type (self_value
->value
));
469 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
470 return !!value_as_long (self_value
->value
);
471 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
472 return value_as_double (self_value
->value
) != 0;
473 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
474 return !decimal_is_zero (value_contents (self_value
->value
),
478 PyErr_SetString (PyExc_TypeError
, _("Attempted truth testing on invalid "
484 /* Implements comparison operations for value objects. */
486 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
489 struct value
*value_self
, *value_other
;
490 volatile struct gdb_exception except
;
492 if (PyObject_TypeCheck (other
, &value_object_type
))
493 value_other
= ((value_object
*) other
)->value
;
494 else if (PyInt_Check (other
))
498 l
= PyInt_AsLong (other
);
499 if (PyErr_Occurred ())
502 value_other
= value_from_longest (builtin_type_pyint
, l
);
504 else if (PyFloat_Check (other
))
508 d
= PyFloat_AsDouble (other
);
509 if (PyErr_Occurred ())
512 value_other
= value_from_double (builtin_type_pyfloat
, d
);
514 else if (PyString_Check (other
) || PyUnicode_Check (other
))
518 str
= python_string_to_target_string (other
);
519 value_other
= value_from_string (str
);
522 else if (other
== Py_None
)
523 /* Comparing with None is special. From what I can tell, in Python
524 None is smaller than anything else. */
536 PyErr_SetString (PyExc_NotImplementedError
,
537 "Invalid operation on gdb.Value.");
542 PyErr_SetString (PyExc_NotImplementedError
,
543 "Operation not supported on gdb.Value of this type.");
547 TRY_CATCH (except
, RETURN_MASK_ALL
)
551 result
= value_less (((value_object
*) self
)->value
, value_other
);
554 result
= value_less (((value_object
*) self
)->value
, value_other
)
555 || value_equal (((value_object
*) self
)->value
, value_other
);
558 result
= value_equal (((value_object
*) self
)->value
, value_other
);
561 result
= !value_equal (((value_object
*) self
)->value
, value_other
);
564 result
= value_less (value_other
, ((value_object
*) self
)->value
);
567 result
= value_less (value_other
, ((value_object
*) self
)->value
)
568 || value_equal (((value_object
*) self
)->value
, value_other
);
572 PyErr_SetString (PyExc_NotImplementedError
,
573 "Invalid operation on gdb.Value.");
577 GDB_PY_HANDLE_EXCEPTION (except
);
585 /* Returns an object for a value which is released from the all_values chain,
586 so its lifetime is not bound to the execution of a command. */
588 value_to_value_object (struct value
*val
)
590 value_object
*val_obj
;
592 val_obj
= PyObject_New (value_object
, &value_object_type
);
595 val_obj
->value
= val
;
597 value_prepend_to_list (&values_in_python
, val
);
600 return (PyObject
*) val_obj
;
603 /* Try to convert a Python value to a gdb value. If the value cannot
604 be converted, throw a gdb exception. */
607 convert_value_from_python (PyObject
*obj
)
609 struct value
*value
= NULL
; /* -Wall */
610 PyObject
*target_str
, *unicode_str
;
614 error (_("Internal error while converting Python value."));
616 if (PyBool_Check (obj
))
617 value
= value_from_longest (builtin_type_pybool
, obj
== Py_True
);
618 else if (PyInt_Check (obj
))
619 value
= value_from_longest (builtin_type_pyint
, PyInt_AsLong (obj
));
620 else if (PyLong_Check (obj
))
622 LONGEST l
= PyLong_AsLongLong (obj
);
623 if (! PyErr_Occurred ())
624 value
= value_from_longest (builtin_type_pylong
, l
);
626 else if (PyFloat_Check (obj
))
628 double d
= PyFloat_AsDouble (obj
);
629 if (! PyErr_Occurred ())
630 value
= value_from_double (builtin_type_pyfloat
, d
);
632 else if (PyString_Check (obj
) || PyUnicode_Check (obj
))
636 s
= python_string_to_target_string (obj
);
640 old
= make_cleanup (xfree
, s
);
641 value
= value_from_string (s
);
644 else if (PyObject_TypeCheck (obj
, &value_object_type
))
645 value
= ((value_object
*) obj
)->value
;
647 error (_("Could not convert Python object: %s"),
648 PyString_AsString (PyObject_Str (obj
)));
650 if (PyErr_Occurred ())
651 error (_("Error converting Python value."));
656 /* Returns value object in the ARGth position in GDB's history. */
658 gdbpy_get_value_from_history (PyObject
*self
, PyObject
*args
)
661 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
662 volatile struct gdb_exception except
;
664 if (!PyArg_ParseTuple (args
, "i", &i
))
667 TRY_CATCH (except
, RETURN_MASK_ALL
)
669 res_val
= access_value_history (i
);
671 GDB_PY_HANDLE_EXCEPTION (except
);
673 return value_to_value_object (res_val
);
677 gdbpy_initialize_values (void)
679 value_object_type
.tp_new
= valpy_new
;
680 if (PyType_Ready (&value_object_type
) < 0)
683 Py_INCREF (&value_object_type
);
684 PyModule_AddObject (gdb_module
, "Value", (PyObject
*) &value_object_type
);
686 values_in_python
= NULL
;
689 #endif /* HAVE_PYTHON */