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 /* Called by the Python interpreter when deallocating a value object. */
62 valpy_dealloc (PyObject
*obj
)
64 value_object
*self
= (value_object
*) obj
;
66 value_remove_from_list (&values_in_python
, self
->value
);
68 if (!self
->owned_by_gdb
)
69 value_free (self
->value
);
70 self
->ob_type
->tp_free (self
);
73 /* Called when a new gdb.Value object needs to be allocated. */
75 valpy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*keywords
)
77 struct value
*value
= NULL
; /* Initialize to appease gcc warning. */
78 value_object
*value_obj
;
79 volatile struct gdb_exception except
;
81 if (PyTuple_Size (args
) != 1)
83 PyErr_SetString (PyExc_TypeError
, _("Value object creation takes only "
88 value_obj
= (value_object
*) subtype
->tp_alloc (subtype
, 1);
89 if (value_obj
== NULL
)
91 PyErr_SetString (PyExc_MemoryError
, _("Could not allocate memory to "
92 "create Value object."));
96 TRY_CATCH (except
, RETURN_MASK_ALL
)
98 value
= convert_value_from_python (PyTuple_GetItem (args
, 0));
100 if (except
.reason
< 0)
102 subtype
->tp_free (value_obj
);
103 return PyErr_Format (except
.reason
== RETURN_QUIT
104 ? PyExc_KeyboardInterrupt
: PyExc_TypeError
,
105 "%s", except
.message
);
108 value_obj
->value
= value
;
109 release_value (value
);
110 value_prepend_to_list (&values_in_python
, value
);
112 return (PyObject
*) value_obj
;
115 /* Given a value of a pointer type, apply the C unary * operator to it. */
117 valpy_dereference (PyObject
*self
, PyObject
*args
)
119 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
120 volatile struct gdb_exception except
;
122 TRY_CATCH (except
, RETURN_MASK_ALL
)
124 res_val
= value_ind (((value_object
*) self
)->value
);
126 GDB_PY_HANDLE_EXCEPTION (except
);
128 return value_to_value_object (res_val
);
131 #ifdef HAVE_LIBPYTHON2_4
136 valpy_length (PyObject
*self
)
138 /* We don't support getting the number of elements in a struct / class. */
139 PyErr_SetString (PyExc_NotImplementedError
,
140 "Invalid operation on gdb.Value.");
144 /* Given string name of an element inside structure, return its value
147 valpy_getitem (PyObject
*self
, PyObject
*key
)
149 value_object
*self_value
= (value_object
*) self
;
151 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
153 volatile struct gdb_exception except
;
155 field
= python_string_to_target_string (key
);
159 old
= make_cleanup (xfree
, field
);
161 TRY_CATCH (except
, RETURN_MASK_ALL
)
163 res_val
= value_struct_elt (&self_value
->value
, NULL
, field
, 0, NULL
);
165 GDB_PY_HANDLE_EXCEPTION (except
);
169 return value_to_value_object (res_val
);
173 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
175 PyErr_Format (PyExc_NotImplementedError
,
176 _("Setting of struct elements is not currently supported."));
180 /* Called by the Python interpreter to obtain string representation
183 valpy_str (PyObject
*self
)
188 struct cleanup
*old_chain
;
190 volatile struct gdb_exception except
;
192 stb
= mem_fileopen ();
193 old_chain
= make_cleanup_ui_file_delete (stb
);
195 TRY_CATCH (except
, RETURN_MASK_ALL
)
197 common_val_print (((value_object
*) self
)->value
, stb
, 0, 0, 0,
198 Val_pretty_default
, current_language
);
199 s
= ui_file_xstrdup (stb
, &dummy
);
201 GDB_PY_HANDLE_EXCEPTION (except
);
203 do_cleanups (old_chain
);
205 result
= PyUnicode_Decode (s
, strlen (s
), host_charset (), NULL
);
221 /* If TYPE is a reference, return the target; otherwise return TYPE. */
222 #define STRIP_REFERENCE(TYPE) \
223 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
225 /* Returns a value object which is the result of applying the operation
226 specified by OPCODE to the given arguments. */
228 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
230 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
231 volatile struct gdb_exception except
;
233 TRY_CATCH (except
, RETURN_MASK_ALL
)
235 struct value
*arg1
, *arg2
;
237 /* If the gdb.Value object is the second operand, then it will be passed
238 to us as the OTHER argument, and SELF will be an entirely different
239 kind of object, altogether. Because of this, we can't assume self is
240 a gdb.Value object and need to convert it from python as well. */
241 arg1
= convert_value_from_python (self
);
242 arg2
= convert_value_from_python (other
);
248 struct type
*ltype
= value_type (arg1
);
249 struct type
*rtype
= value_type (arg2
);
251 CHECK_TYPEDEF (ltype
);
252 ltype
= STRIP_REFERENCE (ltype
);
253 CHECK_TYPEDEF (rtype
);
254 rtype
= STRIP_REFERENCE (rtype
);
256 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
)
257 res_val
= value_ptradd (arg1
, arg2
);
258 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
259 res_val
= value_ptradd (arg2
, arg1
);
261 res_val
= value_binop (arg1
, arg2
, BINOP_ADD
);
266 struct type
*ltype
= value_type (arg1
);
267 struct type
*rtype
= value_type (arg2
);
269 CHECK_TYPEDEF (ltype
);
270 ltype
= STRIP_REFERENCE (ltype
);
271 CHECK_TYPEDEF (rtype
);
272 rtype
= STRIP_REFERENCE (rtype
);
274 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
)
276 if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
277 /* A ptrdiff_t for the target would be preferable
279 res_val
= value_from_longest (builtin_type_pyint
,
280 value_ptrdiff (arg1
, arg2
));
282 res_val
= value_ptrsub (arg1
, arg2
);
285 res_val
= value_binop (arg1
, arg2
, BINOP_SUB
);
289 res_val
= value_binop (arg1
, arg2
, BINOP_MUL
);
292 res_val
= value_binop (arg1
, arg2
, BINOP_DIV
);
295 res_val
= value_binop (arg1
, arg2
, BINOP_REM
);
298 res_val
= value_binop (arg1
, arg2
, BINOP_EXP
);
302 GDB_PY_HANDLE_EXCEPTION (except
);
304 return value_to_value_object (res_val
);
308 valpy_add (PyObject
*self
, PyObject
*other
)
310 return valpy_binop (VALPY_ADD
, self
, other
);
314 valpy_subtract (PyObject
*self
, PyObject
*other
)
316 return valpy_binop (VALPY_SUB
, self
, other
);
320 valpy_multiply (PyObject
*self
, PyObject
*other
)
322 return valpy_binop (VALPY_MUL
, self
, other
);
326 valpy_divide (PyObject
*self
, PyObject
*other
)
328 return valpy_binop (VALPY_DIV
, self
, other
);
332 valpy_remainder (PyObject
*self
, PyObject
*other
)
334 return valpy_binop (VALPY_REM
, self
, other
);
338 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
340 /* We don't support the ternary form of pow. I don't know how to express
341 that, so let's just throw NotImplementedError to at least do something
343 if (unused
!= Py_None
)
345 PyErr_SetString (PyExc_NotImplementedError
,
346 "Invalid operation on gdb.Value.");
350 return valpy_binop (VALPY_POW
, self
, other
);
354 valpy_negative (PyObject
*self
)
356 struct value
*val
= NULL
;
357 volatile struct gdb_exception except
;
359 TRY_CATCH (except
, RETURN_MASK_ALL
)
361 val
= value_neg (((value_object
*) self
)->value
);
363 GDB_PY_HANDLE_EXCEPTION (except
);
365 return value_to_value_object (val
);
369 valpy_positive (PyObject
*self
)
371 struct value
*copy
= value_copy (((value_object
*) self
)->value
);
373 return value_to_value_object (copy
);
377 valpy_absolute (PyObject
*self
)
379 if (value_less (((value_object
*) self
)->value
,
380 value_from_longest (builtin_type_int8
, 0)))
381 return valpy_negative (self
);
383 return valpy_positive (self
);
386 /* Implements boolean evaluation of gdb.Value. */
388 valpy_nonzero (PyObject
*self
)
390 value_object
*self_value
= (value_object
*) self
;
393 type
= check_typedef (value_type (self_value
->value
));
395 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
396 return !!value_as_long (self_value
->value
);
397 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
398 return value_as_double (self_value
->value
) != 0;
399 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
400 return !decimal_is_zero (value_contents (self_value
->value
),
404 PyErr_SetString (PyExc_TypeError
, _("Attempted truth testing on invalid "
410 /* Implements comparison operations for value objects. */
412 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
415 struct value
*value_self
, *value_other
;
416 volatile struct gdb_exception except
;
418 if (PyObject_TypeCheck (other
, &value_object_type
))
419 value_other
= ((value_object
*) other
)->value
;
420 else if (PyInt_Check (other
))
424 l
= PyInt_AsLong (other
);
425 if (PyErr_Occurred ())
428 value_other
= value_from_longest (builtin_type_pyint
, l
);
430 else if (PyFloat_Check (other
))
434 d
= PyFloat_AsDouble (other
);
435 if (PyErr_Occurred ())
438 value_other
= value_from_double (builtin_type_pyfloat
, d
);
440 else if (PyString_Check (other
) || PyUnicode_Check (other
))
444 str
= python_string_to_target_string (other
);
445 value_other
= value_from_string (str
);
448 else if (other
== Py_None
)
449 /* Comparing with None is special. From what I can tell, in Python
450 None is smaller than anything else. */
462 PyErr_SetString (PyExc_NotImplementedError
,
463 "Invalid operation on gdb.Value.");
468 PyErr_SetString (PyExc_NotImplementedError
,
469 "Operation not supported on gdb.Value of this type.");
473 TRY_CATCH (except
, RETURN_MASK_ALL
)
477 result
= value_less (((value_object
*) self
)->value
, value_other
);
480 result
= value_less (((value_object
*) self
)->value
, value_other
)
481 || value_equal (((value_object
*) self
)->value
, value_other
);
484 result
= value_equal (((value_object
*) self
)->value
, value_other
);
487 result
= !value_equal (((value_object
*) self
)->value
, value_other
);
490 result
= value_less (value_other
, ((value_object
*) self
)->value
);
493 result
= value_less (value_other
, ((value_object
*) self
)->value
)
494 || value_equal (((value_object
*) self
)->value
, value_other
);
498 PyErr_SetString (PyExc_NotImplementedError
,
499 "Invalid operation on gdb.Value.");
503 GDB_PY_HANDLE_EXCEPTION (except
);
511 /* Returns an object for a value which is released from the all_values chain,
512 so its lifetime is not bound to the execution of a command. */
514 value_to_value_object (struct value
*val
)
516 value_object
*val_obj
;
518 val_obj
= PyObject_New (value_object
, &value_object_type
);
521 val_obj
->value
= val
;
523 value_prepend_to_list (&values_in_python
, val
);
526 return (PyObject
*) val_obj
;
529 /* Try to convert a Python value to a gdb value. If the value cannot
530 be converted, throw a gdb exception. */
533 convert_value_from_python (PyObject
*obj
)
535 struct value
*value
= NULL
; /* -Wall */
536 PyObject
*target_str
, *unicode_str
;
540 error (_("Internal error while converting Python value."));
542 if (PyBool_Check (obj
))
543 value
= value_from_longest (builtin_type_pybool
, obj
== Py_True
);
544 else if (PyInt_Check (obj
))
545 value
= value_from_longest (builtin_type_pyint
, PyInt_AsLong (obj
));
546 else if (PyLong_Check (obj
))
548 LONGEST l
= PyLong_AsLongLong (obj
);
549 if (! PyErr_Occurred ())
550 value
= value_from_longest (builtin_type_pylong
, l
);
552 else if (PyFloat_Check (obj
))
554 double d
= PyFloat_AsDouble (obj
);
555 if (! PyErr_Occurred ())
556 value
= value_from_double (builtin_type_pyfloat
, d
);
558 else if (PyString_Check (obj
) || PyUnicode_Check (obj
))
562 s
= python_string_to_target_string (obj
);
566 old
= make_cleanup (xfree
, s
);
567 value
= value_from_string (s
);
570 else if (PyObject_TypeCheck (obj
, &value_object_type
))
571 value
= ((value_object
*) obj
)->value
;
573 error (_("Could not convert Python object: %s"),
574 PyString_AsString (PyObject_Str (obj
)));
576 if (PyErr_Occurred ())
577 error (_("Error converting Python value."));
582 /* Returns value object in the ARGth position in GDB's history. */
584 gdbpy_get_value_from_history (PyObject
*self
, PyObject
*args
)
587 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
588 volatile struct gdb_exception except
;
590 if (!PyArg_ParseTuple (args
, "i", &i
))
593 TRY_CATCH (except
, RETURN_MASK_ALL
)
595 res_val
= access_value_history (i
);
597 GDB_PY_HANDLE_EXCEPTION (except
);
599 return value_to_value_object (res_val
);
603 gdbpy_initialize_values (void)
605 value_object_type
.tp_new
= valpy_new
;
606 if (PyType_Ready (&value_object_type
) < 0)
609 Py_INCREF (&value_object_type
);
610 PyModule_AddObject (gdb_module
, "Value", (PyObject
*) &value_object_type
);
612 values_in_python
= NULL
;
615 static PyMethodDef value_object_methods
[] = {
616 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
617 {NULL
} /* Sentinel */
620 static PyNumberMethods value_object_as_number
= {
626 NULL
, /* nb_divmod */
627 valpy_power
, /* nb_power */
628 valpy_negative
, /* nb_negative */
629 valpy_positive
, /* nb_positive */
630 valpy_absolute
, /* nb_absolute */
631 valpy_nonzero
/* nb_nonzero */
634 static PyMappingMethods value_object_as_mapping
= {
640 PyTypeObject value_object_type
= {
641 PyObject_HEAD_INIT (NULL
)
643 "gdb.Value", /*tp_name*/
644 sizeof (value_object
), /*tp_basicsize*/
646 valpy_dealloc
, /*tp_dealloc*/
652 &value_object_as_number
, /*tp_as_number*/
653 0, /*tp_as_sequence*/
654 &value_object_as_mapping
, /*tp_as_mapping*/
657 valpy_str
, /*tp_str*/
661 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
, /*tp_flags*/
662 "GDB value object", /* tp_doc */
665 valpy_richcompare
, /* tp_richcompare */
666 0, /* tp_weaklistoffset */
669 value_object_methods
/* tp_methods */
672 #endif /* HAVE_PYTHON */