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/>. */
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
);
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 struct value
*tmp
= self_value
->value
;
164 res_val
= value_struct_elt (&tmp
, NULL
, field
, 0, NULL
);
166 GDB_PY_HANDLE_EXCEPTION (except
);
170 return value_to_value_object (res_val
);
174 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
176 PyErr_Format (PyExc_NotImplementedError
,
177 _("Setting of struct elements is not currently supported."));
181 /* Called by the Python interpreter to obtain string representation
184 valpy_str (PyObject
*self
)
189 struct cleanup
*old_chain
;
191 struct value_print_options opts
;
192 volatile struct gdb_exception except
;
194 get_user_print_options (&opts
);
197 stb
= mem_fileopen ();
198 old_chain
= make_cleanup_ui_file_delete (stb
);
200 TRY_CATCH (except
, RETURN_MASK_ALL
)
202 common_val_print (((value_object
*) self
)->value
, stb
, 0,
203 &opts
, current_language
);
204 s
= ui_file_xstrdup (stb
, &dummy
);
206 GDB_PY_HANDLE_EXCEPTION (except
);
208 do_cleanups (old_chain
);
210 result
= PyUnicode_Decode (s
, strlen (s
), host_charset (), NULL
);
226 /* If TYPE is a reference, return the target; otherwise return TYPE. */
227 #define STRIP_REFERENCE(TYPE) \
228 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
230 /* Returns a value object which is the result of applying the operation
231 specified by OPCODE to the given arguments. */
233 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
235 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
236 volatile struct gdb_exception except
;
238 TRY_CATCH (except
, RETURN_MASK_ALL
)
240 struct value
*arg1
, *arg2
;
242 /* If the gdb.Value object is the second operand, then it will be passed
243 to us as the OTHER argument, and SELF will be an entirely different
244 kind of object, altogether. Because of this, we can't assume self is
245 a gdb.Value object and need to convert it from python as well. */
246 arg1
= convert_value_from_python (self
);
247 arg2
= convert_value_from_python (other
);
253 struct type
*ltype
= value_type (arg1
);
254 struct type
*rtype
= value_type (arg2
);
256 CHECK_TYPEDEF (ltype
);
257 ltype
= STRIP_REFERENCE (ltype
);
258 CHECK_TYPEDEF (rtype
);
259 rtype
= STRIP_REFERENCE (rtype
);
261 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
)
262 res_val
= value_ptradd (arg1
, arg2
);
263 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
264 res_val
= value_ptradd (arg2
, arg1
);
266 res_val
= value_binop (arg1
, arg2
, BINOP_ADD
);
271 struct type
*ltype
= value_type (arg1
);
272 struct type
*rtype
= value_type (arg2
);
274 CHECK_TYPEDEF (ltype
);
275 ltype
= STRIP_REFERENCE (ltype
);
276 CHECK_TYPEDEF (rtype
);
277 rtype
= STRIP_REFERENCE (rtype
);
279 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
)
281 if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
282 /* A ptrdiff_t for the target would be preferable
284 res_val
= value_from_longest (builtin_type_pyint
,
285 value_ptrdiff (arg1
, arg2
));
287 res_val
= value_ptrsub (arg1
, arg2
);
290 res_val
= value_binop (arg1
, arg2
, BINOP_SUB
);
294 res_val
= value_binop (arg1
, arg2
, BINOP_MUL
);
297 res_val
= value_binop (arg1
, arg2
, BINOP_DIV
);
300 res_val
= value_binop (arg1
, arg2
, BINOP_REM
);
303 res_val
= value_binop (arg1
, arg2
, BINOP_EXP
);
307 GDB_PY_HANDLE_EXCEPTION (except
);
309 return value_to_value_object (res_val
);
313 valpy_add (PyObject
*self
, PyObject
*other
)
315 return valpy_binop (VALPY_ADD
, self
, other
);
319 valpy_subtract (PyObject
*self
, PyObject
*other
)
321 return valpy_binop (VALPY_SUB
, self
, other
);
325 valpy_multiply (PyObject
*self
, PyObject
*other
)
327 return valpy_binop (VALPY_MUL
, self
, other
);
331 valpy_divide (PyObject
*self
, PyObject
*other
)
333 return valpy_binop (VALPY_DIV
, self
, other
);
337 valpy_remainder (PyObject
*self
, PyObject
*other
)
339 return valpy_binop (VALPY_REM
, self
, other
);
343 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
345 /* We don't support the ternary form of pow. I don't know how to express
346 that, so let's just throw NotImplementedError to at least do something
348 if (unused
!= Py_None
)
350 PyErr_SetString (PyExc_NotImplementedError
,
351 "Invalid operation on gdb.Value.");
355 return valpy_binop (VALPY_POW
, self
, other
);
359 valpy_negative (PyObject
*self
)
361 struct value
*val
= NULL
;
362 volatile struct gdb_exception except
;
364 TRY_CATCH (except
, RETURN_MASK_ALL
)
366 val
= value_neg (((value_object
*) self
)->value
);
368 GDB_PY_HANDLE_EXCEPTION (except
);
370 return value_to_value_object (val
);
374 valpy_positive (PyObject
*self
)
376 struct value
*copy
= value_copy (((value_object
*) self
)->value
);
378 return value_to_value_object (copy
);
382 valpy_absolute (PyObject
*self
)
384 if (value_less (((value_object
*) self
)->value
,
385 value_from_longest (builtin_type_int8
, 0)))
386 return valpy_negative (self
);
388 return valpy_positive (self
);
391 /* Implements boolean evaluation of gdb.Value. */
393 valpy_nonzero (PyObject
*self
)
395 value_object
*self_value
= (value_object
*) self
;
398 type
= check_typedef (value_type (self_value
->value
));
400 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
401 return !!value_as_long (self_value
->value
);
402 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
403 return value_as_double (self_value
->value
) != 0;
404 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
405 return !decimal_is_zero (value_contents (self_value
->value
),
409 PyErr_SetString (PyExc_TypeError
, _("Attempted truth testing on invalid "
415 /* Implements comparison operations for value objects. */
417 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
420 struct value
*value_self
, *value_other
;
421 volatile struct gdb_exception except
;
423 if (PyObject_TypeCheck (other
, &value_object_type
))
424 value_other
= ((value_object
*) other
)->value
;
425 else if (PyInt_Check (other
))
429 l
= PyInt_AsLong (other
);
430 if (PyErr_Occurred ())
433 value_other
= value_from_longest (builtin_type_pyint
, l
);
435 else if (PyFloat_Check (other
))
439 d
= PyFloat_AsDouble (other
);
440 if (PyErr_Occurred ())
443 value_other
= value_from_double (builtin_type_pyfloat
, d
);
445 else if (PyString_Check (other
) || PyUnicode_Check (other
))
449 str
= python_string_to_target_string (other
);
450 value_other
= value_from_string (str
);
453 else if (other
== Py_None
)
454 /* Comparing with None is special. From what I can tell, in Python
455 None is smaller than anything else. */
467 PyErr_SetString (PyExc_NotImplementedError
,
468 "Invalid operation on gdb.Value.");
473 PyErr_SetString (PyExc_NotImplementedError
,
474 "Operation not supported on gdb.Value of this type.");
478 TRY_CATCH (except
, RETURN_MASK_ALL
)
482 result
= value_less (((value_object
*) self
)->value
, value_other
);
485 result
= value_less (((value_object
*) self
)->value
, value_other
)
486 || value_equal (((value_object
*) self
)->value
, value_other
);
489 result
= value_equal (((value_object
*) self
)->value
, value_other
);
492 result
= !value_equal (((value_object
*) self
)->value
, value_other
);
495 result
= value_less (value_other
, ((value_object
*) self
)->value
);
498 result
= value_less (value_other
, ((value_object
*) self
)->value
)
499 || value_equal (((value_object
*) self
)->value
, value_other
);
503 PyErr_SetString (PyExc_NotImplementedError
,
504 "Invalid operation on gdb.Value.");
508 GDB_PY_HANDLE_EXCEPTION (except
);
516 /* Returns an object for a value which is released from the all_values chain,
517 so its lifetime is not bound to the execution of a command. */
519 value_to_value_object (struct value
*val
)
521 value_object
*val_obj
;
523 val_obj
= PyObject_New (value_object
, &value_object_type
);
526 val_obj
->value
= val
;
527 val_obj
->owned_by_gdb
= 0;
529 value_prepend_to_list (&values_in_python
, val
);
532 return (PyObject
*) val_obj
;
535 /* Try to convert a Python value to a gdb value. If the value cannot
536 be converted, throw a gdb exception. */
539 convert_value_from_python (PyObject
*obj
)
541 struct value
*value
= NULL
; /* -Wall */
542 PyObject
*target_str
, *unicode_str
;
546 error (_("Internal error while converting Python value."));
548 if (PyBool_Check (obj
))
549 value
= value_from_longest (builtin_type_pybool
, obj
== Py_True
);
550 else if (PyInt_Check (obj
))
551 value
= value_from_longest (builtin_type_pyint
, PyInt_AsLong (obj
));
552 else if (PyLong_Check (obj
))
554 LONGEST l
= PyLong_AsLongLong (obj
);
555 if (! PyErr_Occurred ())
556 value
= value_from_longest (builtin_type_pylong
, l
);
558 else if (PyFloat_Check (obj
))
560 double d
= PyFloat_AsDouble (obj
);
561 if (! PyErr_Occurred ())
562 value
= value_from_double (builtin_type_pyfloat
, d
);
564 else if (PyString_Check (obj
) || PyUnicode_Check (obj
))
568 s
= python_string_to_target_string (obj
);
572 old
= make_cleanup (xfree
, s
);
573 value
= value_from_string (s
);
576 else if (PyObject_TypeCheck (obj
, &value_object_type
))
577 value
= ((value_object
*) obj
)->value
;
579 error (_("Could not convert Python object: %s"),
580 PyString_AsString (PyObject_Str (obj
)));
582 if (PyErr_Occurred ())
583 error (_("Error converting Python value."));
588 /* Returns value object in the ARGth position in GDB's history. */
590 gdbpy_get_value_from_history (PyObject
*self
, PyObject
*args
)
593 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
594 volatile struct gdb_exception except
;
596 if (!PyArg_ParseTuple (args
, "i", &i
))
599 TRY_CATCH (except
, RETURN_MASK_ALL
)
601 res_val
= access_value_history (i
);
603 GDB_PY_HANDLE_EXCEPTION (except
);
605 return value_to_value_object (res_val
);
609 gdbpy_initialize_values (void)
611 value_object_type
.tp_new
= valpy_new
;
612 if (PyType_Ready (&value_object_type
) < 0)
615 Py_INCREF (&value_object_type
);
616 PyModule_AddObject (gdb_module
, "Value", (PyObject
*) &value_object_type
);
618 values_in_python
= NULL
;
621 static PyMethodDef value_object_methods
[] = {
622 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
623 {NULL
} /* Sentinel */
626 static PyNumberMethods value_object_as_number
= {
632 NULL
, /* nb_divmod */
633 valpy_power
, /* nb_power */
634 valpy_negative
, /* nb_negative */
635 valpy_positive
, /* nb_positive */
636 valpy_absolute
, /* nb_absolute */
637 valpy_nonzero
/* nb_nonzero */
640 static PyMappingMethods value_object_as_mapping
= {
646 PyTypeObject value_object_type
= {
647 PyObject_HEAD_INIT (NULL
)
649 "gdb.Value", /*tp_name*/
650 sizeof (value_object
), /*tp_basicsize*/
652 valpy_dealloc
, /*tp_dealloc*/
658 &value_object_as_number
, /*tp_as_number*/
659 0, /*tp_as_sequence*/
660 &value_object_as_mapping
, /*tp_as_mapping*/
663 valpy_str
, /*tp_str*/
667 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
, /*tp_flags*/
668 "GDB value object", /* tp_doc */
671 valpy_richcompare
, /* tp_richcompare */
672 0, /* tp_weaklistoffset */
675 value_object_methods
/* tp_methods */
678 #endif /* HAVE_PYTHON */