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 struct value
*values_in_python
;
37 #include "python-internal.h"
39 /* Even though Python scalar types directly map to host types, we use
40 target types here to remain consistent with the the values system in
41 GDB (which uses target arithmetic). */
43 /* Python's integer type corresponds to C's long type. */
44 #define builtin_type_pyint builtin_type (current_gdbarch)->builtin_long
46 /* Python's float type corresponds to C's double type. */
47 #define builtin_type_pyfloat builtin_type (current_gdbarch)->builtin_double
49 /* Python's long type corresponds to C's long long type. */
50 #define builtin_type_pylong builtin_type (current_gdbarch)->builtin_long_long
52 #define builtin_type_pybool \
53 language_bool_type (current_language, current_gdbarch)
61 /* Called by the Python interpreter when deallocating a value object. */
63 valpy_dealloc (PyObject
*obj
)
65 value_object
*self
= (value_object
*) obj
;
67 value_remove_from_list (&values_in_python
, self
->value
);
69 if (!self
->owned_by_gdb
)
70 value_free (self
->value
);
71 self
->ob_type
->tp_free (self
);
74 /* Called when a new gdb.Value object needs to be allocated. */
76 valpy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*keywords
)
78 struct value
*value
= NULL
; /* Initialize to appease gcc warning. */
79 value_object
*value_obj
;
80 volatile struct gdb_exception except
;
82 if (PyTuple_Size (args
) != 1)
84 PyErr_SetString (PyExc_TypeError
, _("Value object creation takes only "
89 value_obj
= (value_object
*) subtype
->tp_alloc (subtype
, 1);
90 if (value_obj
== NULL
)
92 PyErr_SetString (PyExc_MemoryError
, _("Could not allocate memory to "
93 "create Value object."));
97 TRY_CATCH (except
, RETURN_MASK_ALL
)
99 value
= convert_value_from_python (PyTuple_GetItem (args
, 0));
101 if (except
.reason
< 0)
103 subtype
->tp_free (value_obj
);
104 return PyErr_Format (except
.reason
== RETURN_QUIT
105 ? PyExc_KeyboardInterrupt
: PyExc_TypeError
,
106 "%s", except
.message
);
109 value_obj
->value
= value
;
110 value_obj
->owned_by_gdb
= 0;
111 release_value (value
);
112 value_prepend_to_list (&values_in_python
, value
);
114 return (PyObject
*) value_obj
;
117 /* Given a value of a pointer type, apply the C unary * operator to it. */
119 valpy_dereference (PyObject
*self
, PyObject
*args
)
121 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
122 volatile struct gdb_exception except
;
124 TRY_CATCH (except
, RETURN_MASK_ALL
)
126 res_val
= value_ind (((value_object
*) self
)->value
);
128 GDB_PY_HANDLE_EXCEPTION (except
);
130 return value_to_value_object (res_val
);
133 #ifdef HAVE_LIBPYTHON2_4
138 valpy_length (PyObject
*self
)
140 /* We don't support getting the number of elements in a struct / class. */
141 PyErr_SetString (PyExc_NotImplementedError
,
142 "Invalid operation on gdb.Value.");
146 /* Given string name of an element inside structure, return its value
149 valpy_getitem (PyObject
*self
, PyObject
*key
)
151 value_object
*self_value
= (value_object
*) self
;
153 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
155 volatile struct gdb_exception except
;
157 field
= python_string_to_target_string (key
);
161 old
= make_cleanup (xfree
, field
);
163 TRY_CATCH (except
, RETURN_MASK_ALL
)
165 struct value
*tmp
= self_value
->value
;
166 res_val
= value_struct_elt (&tmp
, NULL
, field
, 0, NULL
);
168 GDB_PY_HANDLE_EXCEPTION (except
);
172 return value_to_value_object (res_val
);
176 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
178 PyErr_Format (PyExc_NotImplementedError
,
179 _("Setting of struct elements is not currently supported."));
183 /* Called by the Python interpreter to obtain string representation
186 valpy_str (PyObject
*self
)
191 struct cleanup
*old_chain
;
193 struct value_print_options opts
;
194 volatile struct gdb_exception except
;
196 get_user_print_options (&opts
);
199 stb
= mem_fileopen ();
200 old_chain
= make_cleanup_ui_file_delete (stb
);
202 TRY_CATCH (except
, RETURN_MASK_ALL
)
204 common_val_print (((value_object
*) self
)->value
, stb
, 0,
205 &opts
, current_language
);
206 s
= ui_file_xstrdup (stb
, &dummy
);
208 GDB_PY_HANDLE_EXCEPTION (except
);
210 do_cleanups (old_chain
);
212 result
= PyUnicode_Decode (s
, strlen (s
), host_charset (), NULL
);
228 /* If TYPE is a reference, return the target; otherwise return TYPE. */
229 #define STRIP_REFERENCE(TYPE) \
230 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
232 /* Returns a value object which is the result of applying the operation
233 specified by OPCODE to the given arguments. */
235 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
237 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
238 volatile struct gdb_exception except
;
240 TRY_CATCH (except
, RETURN_MASK_ALL
)
242 struct value
*arg1
, *arg2
;
244 /* If the gdb.Value object is the second operand, then it will be passed
245 to us as the OTHER argument, and SELF will be an entirely different
246 kind of object, altogether. Because of this, we can't assume self is
247 a gdb.Value object and need to convert it from python as well. */
248 arg1
= convert_value_from_python (self
);
249 arg2
= convert_value_from_python (other
);
255 struct type
*ltype
= value_type (arg1
);
256 struct type
*rtype
= value_type (arg2
);
258 CHECK_TYPEDEF (ltype
);
259 ltype
= STRIP_REFERENCE (ltype
);
260 CHECK_TYPEDEF (rtype
);
261 rtype
= STRIP_REFERENCE (rtype
);
263 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
)
264 res_val
= value_ptradd (arg1
, arg2
);
265 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
266 res_val
= value_ptradd (arg2
, arg1
);
268 res_val
= value_binop (arg1
, arg2
, BINOP_ADD
);
273 struct type
*ltype
= value_type (arg1
);
274 struct type
*rtype
= value_type (arg2
);
276 CHECK_TYPEDEF (ltype
);
277 ltype
= STRIP_REFERENCE (ltype
);
278 CHECK_TYPEDEF (rtype
);
279 rtype
= STRIP_REFERENCE (rtype
);
281 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
)
283 if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
284 /* A ptrdiff_t for the target would be preferable
286 res_val
= value_from_longest (builtin_type_pyint
,
287 value_ptrdiff (arg1
, arg2
));
289 res_val
= value_ptrsub (arg1
, arg2
);
292 res_val
= value_binop (arg1
, arg2
, BINOP_SUB
);
296 res_val
= value_binop (arg1
, arg2
, BINOP_MUL
);
299 res_val
= value_binop (arg1
, arg2
, BINOP_DIV
);
302 res_val
= value_binop (arg1
, arg2
, BINOP_REM
);
305 res_val
= value_binop (arg1
, arg2
, BINOP_EXP
);
309 GDB_PY_HANDLE_EXCEPTION (except
);
311 return value_to_value_object (res_val
);
315 valpy_add (PyObject
*self
, PyObject
*other
)
317 return valpy_binop (VALPY_ADD
, self
, other
);
321 valpy_subtract (PyObject
*self
, PyObject
*other
)
323 return valpy_binop (VALPY_SUB
, self
, other
);
327 valpy_multiply (PyObject
*self
, PyObject
*other
)
329 return valpy_binop (VALPY_MUL
, self
, other
);
333 valpy_divide (PyObject
*self
, PyObject
*other
)
335 return valpy_binop (VALPY_DIV
, self
, other
);
339 valpy_remainder (PyObject
*self
, PyObject
*other
)
341 return valpy_binop (VALPY_REM
, self
, other
);
345 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
347 /* We don't support the ternary form of pow. I don't know how to express
348 that, so let's just throw NotImplementedError to at least do something
350 if (unused
!= Py_None
)
352 PyErr_SetString (PyExc_NotImplementedError
,
353 "Invalid operation on gdb.Value.");
357 return valpy_binop (VALPY_POW
, self
, other
);
361 valpy_negative (PyObject
*self
)
363 struct value
*val
= NULL
;
364 volatile struct gdb_exception except
;
366 TRY_CATCH (except
, RETURN_MASK_ALL
)
368 val
= value_neg (((value_object
*) self
)->value
);
370 GDB_PY_HANDLE_EXCEPTION (except
);
372 return value_to_value_object (val
);
376 valpy_positive (PyObject
*self
)
378 struct value
*copy
= value_copy (((value_object
*) self
)->value
);
380 return value_to_value_object (copy
);
384 valpy_absolute (PyObject
*self
)
386 if (value_less (((value_object
*) self
)->value
,
387 value_from_longest (builtin_type_int8
, 0)))
388 return valpy_negative (self
);
390 return valpy_positive (self
);
393 /* Implements boolean evaluation of gdb.Value. */
395 valpy_nonzero (PyObject
*self
)
397 value_object
*self_value
= (value_object
*) self
;
400 type
= check_typedef (value_type (self_value
->value
));
402 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
403 return !!value_as_long (self_value
->value
);
404 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
405 return value_as_double (self_value
->value
) != 0;
406 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
407 return !decimal_is_zero (value_contents (self_value
->value
),
411 PyErr_SetString (PyExc_TypeError
, _("Attempted truth testing on invalid "
417 /* Implements comparison operations for value objects. */
419 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
422 struct value
*value_self
, *value_other
;
423 volatile struct gdb_exception except
;
425 if (PyObject_TypeCheck (other
, &value_object_type
))
426 value_other
= ((value_object
*) other
)->value
;
427 else if (PyInt_Check (other
))
431 l
= PyInt_AsLong (other
);
432 if (PyErr_Occurred ())
435 value_other
= value_from_longest (builtin_type_pyint
, l
);
437 else if (PyFloat_Check (other
))
441 d
= PyFloat_AsDouble (other
);
442 if (PyErr_Occurred ())
445 value_other
= value_from_double (builtin_type_pyfloat
, d
);
447 else if (PyString_Check (other
) || PyUnicode_Check (other
))
451 str
= python_string_to_target_string (other
);
452 value_other
= value_from_string (str
);
455 else if (other
== Py_None
)
456 /* Comparing with None is special. From what I can tell, in Python
457 None is smaller than anything else. */
469 PyErr_SetString (PyExc_NotImplementedError
,
470 "Invalid operation on gdb.Value.");
475 PyErr_SetString (PyExc_NotImplementedError
,
476 "Operation not supported on gdb.Value of this type.");
480 TRY_CATCH (except
, RETURN_MASK_ALL
)
484 result
= value_less (((value_object
*) self
)->value
, value_other
);
487 result
= value_less (((value_object
*) self
)->value
, value_other
)
488 || value_equal (((value_object
*) self
)->value
, value_other
);
491 result
= value_equal (((value_object
*) self
)->value
, value_other
);
494 result
= !value_equal (((value_object
*) self
)->value
, value_other
);
497 result
= value_less (value_other
, ((value_object
*) self
)->value
);
500 result
= value_less (value_other
, ((value_object
*) self
)->value
)
501 || value_equal (((value_object
*) self
)->value
, value_other
);
505 PyErr_SetString (PyExc_NotImplementedError
,
506 "Invalid operation on gdb.Value.");
510 GDB_PY_HANDLE_EXCEPTION (except
);
518 /* Returns an object for a value which is released from the all_values chain,
519 so its lifetime is not bound to the execution of a command. */
521 value_to_value_object (struct value
*val
)
523 value_object
*val_obj
;
525 val_obj
= PyObject_New (value_object
, &value_object_type
);
528 val_obj
->value
= val
;
529 val_obj
->owned_by_gdb
= 0;
531 value_prepend_to_list (&values_in_python
, val
);
534 return (PyObject
*) val_obj
;
537 /* Try to convert a Python value to a gdb value. If the value cannot
538 be converted, throw a gdb exception. */
541 convert_value_from_python (PyObject
*obj
)
543 struct value
*value
= NULL
; /* -Wall */
544 PyObject
*target_str
, *unicode_str
;
548 error (_("Internal error while converting Python value."));
550 if (PyBool_Check (obj
))
551 value
= value_from_longest (builtin_type_pybool
, obj
== Py_True
);
552 else if (PyInt_Check (obj
))
553 value
= value_from_longest (builtin_type_pyint
, PyInt_AsLong (obj
));
554 else if (PyLong_Check (obj
))
556 LONGEST l
= PyLong_AsLongLong (obj
);
557 if (! PyErr_Occurred ())
558 value
= value_from_longest (builtin_type_pylong
, l
);
560 else if (PyFloat_Check (obj
))
562 double d
= PyFloat_AsDouble (obj
);
563 if (! PyErr_Occurred ())
564 value
= value_from_double (builtin_type_pyfloat
, d
);
566 else if (PyString_Check (obj
) || PyUnicode_Check (obj
))
570 s
= python_string_to_target_string (obj
);
574 old
= make_cleanup (xfree
, s
);
575 value
= value_from_string (s
);
578 else if (PyObject_TypeCheck (obj
, &value_object_type
))
579 value
= ((value_object
*) obj
)->value
;
581 error (_("Could not convert Python object: %s"),
582 PyString_AsString (PyObject_Str (obj
)));
584 if (PyErr_Occurred ())
585 error (_("Error converting Python value."));
590 /* Returns value object in the ARGth position in GDB's history. */
592 gdbpy_get_value_from_history (PyObject
*self
, PyObject
*args
)
595 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
596 volatile struct gdb_exception except
;
598 if (!PyArg_ParseTuple (args
, "i", &i
))
601 TRY_CATCH (except
, RETURN_MASK_ALL
)
603 res_val
= access_value_history (i
);
605 GDB_PY_HANDLE_EXCEPTION (except
);
607 return value_to_value_object (res_val
);
611 gdbpy_initialize_values (void)
613 value_object_type
.tp_new
= valpy_new
;
614 if (PyType_Ready (&value_object_type
) < 0)
617 Py_INCREF (&value_object_type
);
618 PyModule_AddObject (gdb_module
, "Value", (PyObject
*) &value_object_type
);
620 values_in_python
= NULL
;
623 static PyMethodDef value_object_methods
[] = {
624 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
625 {NULL
} /* Sentinel */
628 static PyNumberMethods value_object_as_number
= {
634 NULL
, /* nb_divmod */
635 valpy_power
, /* nb_power */
636 valpy_negative
, /* nb_negative */
637 valpy_positive
, /* nb_positive */
638 valpy_absolute
, /* nb_absolute */
639 valpy_nonzero
/* nb_nonzero */
642 static PyMappingMethods value_object_as_mapping
= {
648 PyTypeObject value_object_type
= {
649 PyObject_HEAD_INIT (NULL
)
651 "gdb.Value", /*tp_name*/
652 sizeof (value_object
), /*tp_basicsize*/
654 valpy_dealloc
, /*tp_dealloc*/
660 &value_object_as_number
, /*tp_as_number*/
661 0, /*tp_as_sequence*/
662 &value_object_as_mapping
, /*tp_as_mapping*/
665 valpy_str
, /*tp_str*/
669 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
, /*tp_flags*/
670 "GDB value object", /* tp_doc */
673 valpy_richcompare
, /* tp_richcompare */
674 0, /* tp_weaklistoffset */
677 value_object_methods
/* tp_methods */
680 #endif /* HAVE_PYTHON */