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"
28 /* List of all values which are currently exposed to Python. It is
29 maintained so that when an objfile is discarded, preserve_values
30 can copy the values' types if needed. This is declared
31 unconditionally to reduce the number of uses of HAVE_PYTHON in the
33 /* This variable is unnecessarily initialized to NULL in order to
34 work around a linker bug on MacOS. */
35 struct value
*values_in_python
= NULL
;
39 #include "python-internal.h"
41 /* Even though Python scalar types directly map to host types, we use
42 target types here to remain consistent with the the values system in
43 GDB (which uses target arithmetic). */
45 /* Python's integer type corresponds to C's long type. */
46 #define builtin_type_pyint builtin_type (current_gdbarch)->builtin_long
48 /* Python's float type corresponds to C's double type. */
49 #define builtin_type_pyfloat builtin_type (current_gdbarch)->builtin_double
51 /* Python's long type corresponds to C's long long type. */
52 #define builtin_type_pylong builtin_type (current_gdbarch)->builtin_long_long
54 #define builtin_type_pybool \
55 language_bool_type (current_language, current_gdbarch)
63 /* Called by the Python interpreter when deallocating a value object. */
65 valpy_dealloc (PyObject
*obj
)
67 value_object
*self
= (value_object
*) obj
;
69 value_remove_from_list (&values_in_python
, self
->value
);
71 if (!self
->owned_by_gdb
)
72 value_free (self
->value
);
73 self
->ob_type
->tp_free (self
);
76 /* Called when a new gdb.Value object needs to be allocated. */
78 valpy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*keywords
)
80 struct value
*value
= NULL
; /* Initialize to appease gcc warning. */
81 value_object
*value_obj
;
82 volatile struct gdb_exception except
;
84 if (PyTuple_Size (args
) != 1)
86 PyErr_SetString (PyExc_TypeError
, _("Value object creation takes only "
91 value_obj
= (value_object
*) subtype
->tp_alloc (subtype
, 1);
92 if (value_obj
== NULL
)
94 PyErr_SetString (PyExc_MemoryError
, _("Could not allocate memory to "
95 "create Value object."));
99 TRY_CATCH (except
, RETURN_MASK_ALL
)
101 value
= convert_value_from_python (PyTuple_GetItem (args
, 0));
103 if (except
.reason
< 0)
105 subtype
->tp_free (value_obj
);
106 return PyErr_Format (except
.reason
== RETURN_QUIT
107 ? PyExc_KeyboardInterrupt
: PyExc_TypeError
,
108 "%s", except
.message
);
111 value_obj
->value
= value
;
112 value_obj
->owned_by_gdb
= 0;
113 release_value (value
);
114 value_prepend_to_list (&values_in_python
, value
);
116 return (PyObject
*) value_obj
;
119 /* Given a value of a pointer type, apply the C unary * operator to it. */
121 valpy_dereference (PyObject
*self
, PyObject
*args
)
123 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
124 volatile struct gdb_exception except
;
126 TRY_CATCH (except
, RETURN_MASK_ALL
)
128 res_val
= value_ind (((value_object
*) self
)->value
);
130 GDB_PY_HANDLE_EXCEPTION (except
);
132 return value_to_value_object (res_val
);
135 #ifdef HAVE_LIBPYTHON2_4
140 valpy_length (PyObject
*self
)
142 /* We don't support getting the number of elements in a struct / class. */
143 PyErr_SetString (PyExc_NotImplementedError
,
144 "Invalid operation on gdb.Value.");
148 /* Given string name of an element inside structure, return its value
151 valpy_getitem (PyObject
*self
, PyObject
*key
)
153 value_object
*self_value
= (value_object
*) self
;
155 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
157 volatile struct gdb_exception except
;
159 field
= python_string_to_target_string (key
);
163 old
= make_cleanup (xfree
, field
);
165 TRY_CATCH (except
, RETURN_MASK_ALL
)
167 struct value
*tmp
= self_value
->value
;
168 res_val
= value_struct_elt (&tmp
, NULL
, field
, 0, NULL
);
170 GDB_PY_HANDLE_EXCEPTION (except
);
174 return value_to_value_object (res_val
);
178 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
180 PyErr_Format (PyExc_NotImplementedError
,
181 _("Setting of struct elements is not currently supported."));
185 /* Called by the Python interpreter to obtain string representation
188 valpy_str (PyObject
*self
)
193 struct cleanup
*old_chain
;
195 struct value_print_options opts
;
196 volatile struct gdb_exception except
;
198 get_user_print_options (&opts
);
201 stb
= mem_fileopen ();
202 old_chain
= make_cleanup_ui_file_delete (stb
);
204 TRY_CATCH (except
, RETURN_MASK_ALL
)
206 common_val_print (((value_object
*) self
)->value
, stb
, 0,
207 &opts
, current_language
);
208 s
= ui_file_xstrdup (stb
, &dummy
);
210 GDB_PY_HANDLE_EXCEPTION (except
);
212 do_cleanups (old_chain
);
214 result
= PyUnicode_Decode (s
, strlen (s
), host_charset (), NULL
);
230 /* If TYPE is a reference, return the target; otherwise return TYPE. */
231 #define STRIP_REFERENCE(TYPE) \
232 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
234 /* Returns a value object which is the result of applying the operation
235 specified by OPCODE to the given arguments. */
237 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
239 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
240 volatile struct gdb_exception except
;
242 TRY_CATCH (except
, RETURN_MASK_ALL
)
244 struct value
*arg1
, *arg2
;
246 /* If the gdb.Value object is the second operand, then it will be passed
247 to us as the OTHER argument, and SELF will be an entirely different
248 kind of object, altogether. Because of this, we can't assume self is
249 a gdb.Value object and need to convert it from python as well. */
250 arg1
= convert_value_from_python (self
);
251 arg2
= convert_value_from_python (other
);
257 struct type
*ltype
= value_type (arg1
);
258 struct type
*rtype
= value_type (arg2
);
260 CHECK_TYPEDEF (ltype
);
261 ltype
= STRIP_REFERENCE (ltype
);
262 CHECK_TYPEDEF (rtype
);
263 rtype
= STRIP_REFERENCE (rtype
);
265 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
)
266 res_val
= value_ptradd (arg1
, arg2
);
267 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
268 res_val
= value_ptradd (arg2
, arg1
);
270 res_val
= value_binop (arg1
, arg2
, BINOP_ADD
);
275 struct type
*ltype
= value_type (arg1
);
276 struct type
*rtype
= value_type (arg2
);
278 CHECK_TYPEDEF (ltype
);
279 ltype
= STRIP_REFERENCE (ltype
);
280 CHECK_TYPEDEF (rtype
);
281 rtype
= STRIP_REFERENCE (rtype
);
283 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
)
285 if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
286 /* A ptrdiff_t for the target would be preferable
288 res_val
= value_from_longest (builtin_type_pyint
,
289 value_ptrdiff (arg1
, arg2
));
291 res_val
= value_ptrsub (arg1
, arg2
);
294 res_val
= value_binop (arg1
, arg2
, BINOP_SUB
);
298 res_val
= value_binop (arg1
, arg2
, BINOP_MUL
);
301 res_val
= value_binop (arg1
, arg2
, BINOP_DIV
);
304 res_val
= value_binop (arg1
, arg2
, BINOP_REM
);
307 res_val
= value_binop (arg1
, arg2
, BINOP_EXP
);
311 GDB_PY_HANDLE_EXCEPTION (except
);
313 return value_to_value_object (res_val
);
317 valpy_add (PyObject
*self
, PyObject
*other
)
319 return valpy_binop (VALPY_ADD
, self
, other
);
323 valpy_subtract (PyObject
*self
, PyObject
*other
)
325 return valpy_binop (VALPY_SUB
, self
, other
);
329 valpy_multiply (PyObject
*self
, PyObject
*other
)
331 return valpy_binop (VALPY_MUL
, self
, other
);
335 valpy_divide (PyObject
*self
, PyObject
*other
)
337 return valpy_binop (VALPY_DIV
, self
, other
);
341 valpy_remainder (PyObject
*self
, PyObject
*other
)
343 return valpy_binop (VALPY_REM
, self
, other
);
347 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
349 /* We don't support the ternary form of pow. I don't know how to express
350 that, so let's just throw NotImplementedError to at least do something
352 if (unused
!= Py_None
)
354 PyErr_SetString (PyExc_NotImplementedError
,
355 "Invalid operation on gdb.Value.");
359 return valpy_binop (VALPY_POW
, self
, other
);
363 valpy_negative (PyObject
*self
)
365 struct value
*val
= NULL
;
366 volatile struct gdb_exception except
;
368 TRY_CATCH (except
, RETURN_MASK_ALL
)
370 val
= value_neg (((value_object
*) self
)->value
);
372 GDB_PY_HANDLE_EXCEPTION (except
);
374 return value_to_value_object (val
);
378 valpy_positive (PyObject
*self
)
380 struct value
*copy
= value_copy (((value_object
*) self
)->value
);
382 return value_to_value_object (copy
);
386 valpy_absolute (PyObject
*self
)
388 if (value_less (((value_object
*) self
)->value
,
389 value_from_longest (builtin_type_int8
, 0)))
390 return valpy_negative (self
);
392 return valpy_positive (self
);
395 /* Implements boolean evaluation of gdb.Value. */
397 valpy_nonzero (PyObject
*self
)
399 value_object
*self_value
= (value_object
*) self
;
402 type
= check_typedef (value_type (self_value
->value
));
404 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
405 return !!value_as_long (self_value
->value
);
406 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
407 return value_as_double (self_value
->value
) != 0;
408 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
409 return !decimal_is_zero (value_contents (self_value
->value
),
413 PyErr_SetString (PyExc_TypeError
, _("Attempted truth testing on invalid "
419 /* Implements comparison operations for value objects. */
421 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
424 struct value
*value_self
, *value_other
;
425 volatile struct gdb_exception except
;
427 if (PyObject_TypeCheck (other
, &value_object_type
))
428 value_other
= ((value_object
*) other
)->value
;
429 else if (PyInt_Check (other
))
433 l
= PyInt_AsLong (other
);
434 if (PyErr_Occurred ())
437 value_other
= value_from_longest (builtin_type_pyint
, l
);
439 else if (PyFloat_Check (other
))
443 d
= PyFloat_AsDouble (other
);
444 if (PyErr_Occurred ())
447 value_other
= value_from_double (builtin_type_pyfloat
, d
);
449 else if (PyString_Check (other
) || PyUnicode_Check (other
))
453 str
= python_string_to_target_string (other
);
454 value_other
= value_from_string (str
);
457 else if (other
== Py_None
)
458 /* Comparing with None is special. From what I can tell, in Python
459 None is smaller than anything else. */
471 PyErr_SetString (PyExc_NotImplementedError
,
472 "Invalid operation on gdb.Value.");
477 PyErr_SetString (PyExc_NotImplementedError
,
478 "Operation not supported on gdb.Value of this type.");
482 TRY_CATCH (except
, RETURN_MASK_ALL
)
486 result
= value_less (((value_object
*) self
)->value
, value_other
);
489 result
= value_less (((value_object
*) self
)->value
, value_other
)
490 || value_equal (((value_object
*) self
)->value
, value_other
);
493 result
= value_equal (((value_object
*) self
)->value
, value_other
);
496 result
= !value_equal (((value_object
*) self
)->value
, value_other
);
499 result
= value_less (value_other
, ((value_object
*) self
)->value
);
502 result
= value_less (value_other
, ((value_object
*) self
)->value
)
503 || value_equal (((value_object
*) self
)->value
, value_other
);
507 PyErr_SetString (PyExc_NotImplementedError
,
508 "Invalid operation on gdb.Value.");
512 GDB_PY_HANDLE_EXCEPTION (except
);
520 /* Returns an object for a value which is released from the all_values chain,
521 so its lifetime is not bound to the execution of a command. */
523 value_to_value_object (struct value
*val
)
525 value_object
*val_obj
;
527 val_obj
= PyObject_New (value_object
, &value_object_type
);
530 val_obj
->value
= val
;
531 val_obj
->owned_by_gdb
= 0;
533 value_prepend_to_list (&values_in_python
, val
);
536 return (PyObject
*) val_obj
;
539 /* Try to convert a Python value to a gdb value. If the value cannot
540 be converted, throw a gdb exception. */
543 convert_value_from_python (PyObject
*obj
)
545 struct value
*value
= NULL
; /* -Wall */
546 PyObject
*target_str
, *unicode_str
;
550 error (_("Internal error while converting Python value."));
552 if (PyBool_Check (obj
))
553 value
= value_from_longest (builtin_type_pybool
, obj
== Py_True
);
554 else if (PyInt_Check (obj
))
555 value
= value_from_longest (builtin_type_pyint
, PyInt_AsLong (obj
));
556 else if (PyLong_Check (obj
))
558 LONGEST l
= PyLong_AsLongLong (obj
);
559 if (! PyErr_Occurred ())
560 value
= value_from_longest (builtin_type_pylong
, l
);
562 else if (PyFloat_Check (obj
))
564 double d
= PyFloat_AsDouble (obj
);
565 if (! PyErr_Occurred ())
566 value
= value_from_double (builtin_type_pyfloat
, d
);
568 else if (PyString_Check (obj
) || PyUnicode_Check (obj
))
572 s
= python_string_to_target_string (obj
);
576 old
= make_cleanup (xfree
, s
);
577 value
= value_from_string (s
);
580 else if (PyObject_TypeCheck (obj
, &value_object_type
))
581 value
= ((value_object
*) obj
)->value
;
583 error (_("Could not convert Python object: %s"),
584 PyString_AsString (PyObject_Str (obj
)));
586 if (PyErr_Occurred ())
587 error (_("Error converting Python value."));
592 /* Returns value object in the ARGth position in GDB's history. */
594 gdbpy_get_value_from_history (PyObject
*self
, PyObject
*args
)
597 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
598 volatile struct gdb_exception except
;
600 if (!PyArg_ParseTuple (args
, "i", &i
))
603 TRY_CATCH (except
, RETURN_MASK_ALL
)
605 res_val
= access_value_history (i
);
607 GDB_PY_HANDLE_EXCEPTION (except
);
609 return value_to_value_object (res_val
);
613 gdbpy_initialize_values (void)
615 value_object_type
.tp_new
= valpy_new
;
616 if (PyType_Ready (&value_object_type
) < 0)
619 Py_INCREF (&value_object_type
);
620 PyModule_AddObject (gdb_module
, "Value", (PyObject
*) &value_object_type
);
622 values_in_python
= NULL
;
625 static PyMethodDef value_object_methods
[] = {
626 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
627 {NULL
} /* Sentinel */
630 static PyNumberMethods value_object_as_number
= {
636 NULL
, /* nb_divmod */
637 valpy_power
, /* nb_power */
638 valpy_negative
, /* nb_negative */
639 valpy_positive
, /* nb_positive */
640 valpy_absolute
, /* nb_absolute */
641 valpy_nonzero
/* nb_nonzero */
644 static PyMappingMethods value_object_as_mapping
= {
650 PyTypeObject value_object_type
= {
651 PyObject_HEAD_INIT (NULL
)
653 "gdb.Value", /*tp_name*/
654 sizeof (value_object
), /*tp_basicsize*/
656 valpy_dealloc
, /*tp_dealloc*/
662 &value_object_as_number
, /*tp_as_number*/
663 0, /*tp_as_sequence*/
664 &value_object_as_mapping
, /*tp_as_mapping*/
667 valpy_str
, /*tp_str*/
671 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
, /*tp_flags*/
672 "GDB value object", /* tp_doc */
675 valpy_richcompare
, /* tp_richcompare */
676 0, /* tp_weaklistoffset */
679 value_object_methods
/* tp_methods */
682 #endif /* HAVE_PYTHON */