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 value_obj
->owned_by_gdb
= 0;
110 release_value (value
);
111 value_prepend_to_list (&values_in_python
, value
);
113 return (PyObject
*) value_obj
;
116 /* Given a value of a pointer type, apply the C unary * operator to it. */
118 valpy_dereference (PyObject
*self
, PyObject
*args
)
120 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
121 volatile struct gdb_exception except
;
123 TRY_CATCH (except
, RETURN_MASK_ALL
)
125 res_val
= value_ind (((value_object
*) self
)->value
);
127 GDB_PY_HANDLE_EXCEPTION (except
);
129 return value_to_value_object (res_val
);
132 #ifdef HAVE_LIBPYTHON2_4
137 valpy_length (PyObject
*self
)
139 /* We don't support getting the number of elements in a struct / class. */
140 PyErr_SetString (PyExc_NotImplementedError
,
141 "Invalid operation on gdb.Value.");
145 /* Given string name of an element inside structure, return its value
148 valpy_getitem (PyObject
*self
, PyObject
*key
)
150 value_object
*self_value
= (value_object
*) self
;
152 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
154 volatile struct gdb_exception except
;
156 field
= python_string_to_target_string (key
);
160 old
= make_cleanup (xfree
, field
);
162 TRY_CATCH (except
, RETURN_MASK_ALL
)
164 struct value
*tmp
= self_value
->value
;
165 res_val
= value_struct_elt (&tmp
, NULL
, field
, 0, NULL
);
167 GDB_PY_HANDLE_EXCEPTION (except
);
171 return value_to_value_object (res_val
);
175 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
177 PyErr_Format (PyExc_NotImplementedError
,
178 _("Setting of struct elements is not currently supported."));
182 /* Called by the Python interpreter to obtain string representation
185 valpy_str (PyObject
*self
)
190 struct cleanup
*old_chain
;
192 volatile struct gdb_exception except
;
194 stb
= mem_fileopen ();
195 old_chain
= make_cleanup_ui_file_delete (stb
);
197 TRY_CATCH (except
, RETURN_MASK_ALL
)
199 common_val_print (((value_object
*) self
)->value
, stb
, 0, 0, 0,
200 Val_pretty_default
, current_language
);
201 s
= ui_file_xstrdup (stb
, &dummy
);
203 GDB_PY_HANDLE_EXCEPTION (except
);
205 do_cleanups (old_chain
);
207 result
= PyUnicode_Decode (s
, strlen (s
), host_charset (), NULL
);
223 /* If TYPE is a reference, return the target; otherwise return TYPE. */
224 #define STRIP_REFERENCE(TYPE) \
225 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
227 /* Returns a value object which is the result of applying the operation
228 specified by OPCODE to the given arguments. */
230 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
232 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
233 volatile struct gdb_exception except
;
235 TRY_CATCH (except
, RETURN_MASK_ALL
)
237 struct value
*arg1
, *arg2
;
239 /* If the gdb.Value object is the second operand, then it will be passed
240 to us as the OTHER argument, and SELF will be an entirely different
241 kind of object, altogether. Because of this, we can't assume self is
242 a gdb.Value object and need to convert it from python as well. */
243 arg1
= convert_value_from_python (self
);
244 arg2
= convert_value_from_python (other
);
250 struct type
*ltype
= value_type (arg1
);
251 struct type
*rtype
= value_type (arg2
);
253 CHECK_TYPEDEF (ltype
);
254 ltype
= STRIP_REFERENCE (ltype
);
255 CHECK_TYPEDEF (rtype
);
256 rtype
= STRIP_REFERENCE (rtype
);
258 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
)
259 res_val
= value_ptradd (arg1
, arg2
);
260 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
261 res_val
= value_ptradd (arg2
, arg1
);
263 res_val
= value_binop (arg1
, arg2
, BINOP_ADD
);
268 struct type
*ltype
= value_type (arg1
);
269 struct type
*rtype
= value_type (arg2
);
271 CHECK_TYPEDEF (ltype
);
272 ltype
= STRIP_REFERENCE (ltype
);
273 CHECK_TYPEDEF (rtype
);
274 rtype
= STRIP_REFERENCE (rtype
);
276 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
)
278 if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
279 /* A ptrdiff_t for the target would be preferable
281 res_val
= value_from_longest (builtin_type_pyint
,
282 value_ptrdiff (arg1
, arg2
));
284 res_val
= value_ptrsub (arg1
, arg2
);
287 res_val
= value_binop (arg1
, arg2
, BINOP_SUB
);
291 res_val
= value_binop (arg1
, arg2
, BINOP_MUL
);
294 res_val
= value_binop (arg1
, arg2
, BINOP_DIV
);
297 res_val
= value_binop (arg1
, arg2
, BINOP_REM
);
300 res_val
= value_binop (arg1
, arg2
, BINOP_EXP
);
304 GDB_PY_HANDLE_EXCEPTION (except
);
306 return value_to_value_object (res_val
);
310 valpy_add (PyObject
*self
, PyObject
*other
)
312 return valpy_binop (VALPY_ADD
, self
, other
);
316 valpy_subtract (PyObject
*self
, PyObject
*other
)
318 return valpy_binop (VALPY_SUB
, self
, other
);
322 valpy_multiply (PyObject
*self
, PyObject
*other
)
324 return valpy_binop (VALPY_MUL
, self
, other
);
328 valpy_divide (PyObject
*self
, PyObject
*other
)
330 return valpy_binop (VALPY_DIV
, self
, other
);
334 valpy_remainder (PyObject
*self
, PyObject
*other
)
336 return valpy_binop (VALPY_REM
, self
, other
);
340 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
342 /* We don't support the ternary form of pow. I don't know how to express
343 that, so let's just throw NotImplementedError to at least do something
345 if (unused
!= Py_None
)
347 PyErr_SetString (PyExc_NotImplementedError
,
348 "Invalid operation on gdb.Value.");
352 return valpy_binop (VALPY_POW
, self
, other
);
356 valpy_negative (PyObject
*self
)
358 struct value
*val
= NULL
;
359 volatile struct gdb_exception except
;
361 TRY_CATCH (except
, RETURN_MASK_ALL
)
363 val
= value_neg (((value_object
*) self
)->value
);
365 GDB_PY_HANDLE_EXCEPTION (except
);
367 return value_to_value_object (val
);
371 valpy_positive (PyObject
*self
)
373 struct value
*copy
= value_copy (((value_object
*) self
)->value
);
375 return value_to_value_object (copy
);
379 valpy_absolute (PyObject
*self
)
381 if (value_less (((value_object
*) self
)->value
,
382 value_from_longest (builtin_type_int8
, 0)))
383 return valpy_negative (self
);
385 return valpy_positive (self
);
388 /* Implements boolean evaluation of gdb.Value. */
390 valpy_nonzero (PyObject
*self
)
392 value_object
*self_value
= (value_object
*) self
;
395 type
= check_typedef (value_type (self_value
->value
));
397 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
398 return !!value_as_long (self_value
->value
);
399 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
400 return value_as_double (self_value
->value
) != 0;
401 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
402 return !decimal_is_zero (value_contents (self_value
->value
),
406 PyErr_SetString (PyExc_TypeError
, _("Attempted truth testing on invalid "
412 /* Implements comparison operations for value objects. */
414 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
417 struct value
*value_self
, *value_other
;
418 volatile struct gdb_exception except
;
420 if (PyObject_TypeCheck (other
, &value_object_type
))
421 value_other
= ((value_object
*) other
)->value
;
422 else if (PyInt_Check (other
))
426 l
= PyInt_AsLong (other
);
427 if (PyErr_Occurred ())
430 value_other
= value_from_longest (builtin_type_pyint
, l
);
432 else if (PyFloat_Check (other
))
436 d
= PyFloat_AsDouble (other
);
437 if (PyErr_Occurred ())
440 value_other
= value_from_double (builtin_type_pyfloat
, d
);
442 else if (PyString_Check (other
) || PyUnicode_Check (other
))
446 str
= python_string_to_target_string (other
);
447 value_other
= value_from_string (str
);
450 else if (other
== Py_None
)
451 /* Comparing with None is special. From what I can tell, in Python
452 None is smaller than anything else. */
464 PyErr_SetString (PyExc_NotImplementedError
,
465 "Invalid operation on gdb.Value.");
470 PyErr_SetString (PyExc_NotImplementedError
,
471 "Operation not supported on gdb.Value of this type.");
475 TRY_CATCH (except
, RETURN_MASK_ALL
)
479 result
= value_less (((value_object
*) self
)->value
, value_other
);
482 result
= value_less (((value_object
*) self
)->value
, value_other
)
483 || value_equal (((value_object
*) self
)->value
, value_other
);
486 result
= value_equal (((value_object
*) self
)->value
, value_other
);
489 result
= !value_equal (((value_object
*) self
)->value
, value_other
);
492 result
= value_less (value_other
, ((value_object
*) self
)->value
);
495 result
= value_less (value_other
, ((value_object
*) self
)->value
)
496 || value_equal (((value_object
*) self
)->value
, value_other
);
500 PyErr_SetString (PyExc_NotImplementedError
,
501 "Invalid operation on gdb.Value.");
505 GDB_PY_HANDLE_EXCEPTION (except
);
513 /* Returns an object for a value which is released from the all_values chain,
514 so its lifetime is not bound to the execution of a command. */
516 value_to_value_object (struct value
*val
)
518 value_object
*val_obj
;
520 val_obj
= PyObject_New (value_object
, &value_object_type
);
523 val_obj
->value
= val
;
524 val_obj
->owned_by_gdb
= 0;
526 value_prepend_to_list (&values_in_python
, val
);
529 return (PyObject
*) val_obj
;
532 /* Try to convert a Python value to a gdb value. If the value cannot
533 be converted, throw a gdb exception. */
536 convert_value_from_python (PyObject
*obj
)
538 struct value
*value
= NULL
; /* -Wall */
539 PyObject
*target_str
, *unicode_str
;
543 error (_("Internal error while converting Python value."));
545 if (PyBool_Check (obj
))
546 value
= value_from_longest (builtin_type_pybool
, obj
== Py_True
);
547 else if (PyInt_Check (obj
))
548 value
= value_from_longest (builtin_type_pyint
, PyInt_AsLong (obj
));
549 else if (PyLong_Check (obj
))
551 LONGEST l
= PyLong_AsLongLong (obj
);
552 if (! PyErr_Occurred ())
553 value
= value_from_longest (builtin_type_pylong
, l
);
555 else if (PyFloat_Check (obj
))
557 double d
= PyFloat_AsDouble (obj
);
558 if (! PyErr_Occurred ())
559 value
= value_from_double (builtin_type_pyfloat
, d
);
561 else if (PyString_Check (obj
) || PyUnicode_Check (obj
))
565 s
= python_string_to_target_string (obj
);
569 old
= make_cleanup (xfree
, s
);
570 value
= value_from_string (s
);
573 else if (PyObject_TypeCheck (obj
, &value_object_type
))
574 value
= ((value_object
*) obj
)->value
;
576 error (_("Could not convert Python object: %s"),
577 PyString_AsString (PyObject_Str (obj
)));
579 if (PyErr_Occurred ())
580 error (_("Error converting Python value."));
585 /* Returns value object in the ARGth position in GDB's history. */
587 gdbpy_get_value_from_history (PyObject
*self
, PyObject
*args
)
590 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
591 volatile struct gdb_exception except
;
593 if (!PyArg_ParseTuple (args
, "i", &i
))
596 TRY_CATCH (except
, RETURN_MASK_ALL
)
598 res_val
= access_value_history (i
);
600 GDB_PY_HANDLE_EXCEPTION (except
);
602 return value_to_value_object (res_val
);
606 gdbpy_initialize_values (void)
608 value_object_type
.tp_new
= valpy_new
;
609 if (PyType_Ready (&value_object_type
) < 0)
612 Py_INCREF (&value_object_type
);
613 PyModule_AddObject (gdb_module
, "Value", (PyObject
*) &value_object_type
);
615 values_in_python
= NULL
;
618 static PyMethodDef value_object_methods
[] = {
619 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
620 {NULL
} /* Sentinel */
623 static PyNumberMethods value_object_as_number
= {
629 NULL
, /* nb_divmod */
630 valpy_power
, /* nb_power */
631 valpy_negative
, /* nb_negative */
632 valpy_positive
, /* nb_positive */
633 valpy_absolute
, /* nb_absolute */
634 valpy_nonzero
/* nb_nonzero */
637 static PyMappingMethods value_object_as_mapping
= {
643 PyTypeObject value_object_type
= {
644 PyObject_HEAD_INIT (NULL
)
646 "gdb.Value", /*tp_name*/
647 sizeof (value_object
), /*tp_basicsize*/
649 valpy_dealloc
, /*tp_dealloc*/
655 &value_object_as_number
, /*tp_as_number*/
656 0, /*tp_as_sequence*/
657 &value_object_as_mapping
, /*tp_as_mapping*/
660 valpy_str
, /*tp_str*/
664 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
, /*tp_flags*/
665 "GDB value object", /* tp_doc */
668 valpy_richcompare
, /* tp_richcompare */
669 0, /* tp_weaklistoffset */
672 value_object_methods
/* tp_methods */
675 #endif /* HAVE_PYTHON */