| |   |
| 1 | /* Python interface to values. |
| 2 | |
| 3 | Copyright (C) 2008 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 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. |
| 11 | |
| 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. |
| 16 | |
| 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/>. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "value.h" |
| 22 | #include "exceptions.h" |
| 23 | #include "python-internal.h" |
| 24 | |
| 25 | typedef struct { |
| 26 | PyObject_HEAD |
| 27 | struct value *value; |
| 28 | int owned_by_gdb; |
| 29 | } value_object; |
| 30 | |
| 31 | /* List of all values which are currently exposed to Python. It is maintained |
| 32 | so that when an objfile is discarded, preserve_values can copy the values' |
| 33 | types if needed. */ |
| 34 | struct value *values_in_python; |
| 35 | |
| 36 | static void valpy_dealloc (PyObject *obj); |
| 37 | static PyObject *valpy_dereference (PyObject *self, PyObject *args); |
| 38 | static PyObject *valpy_get_element (PyObject *self, PyObject *args); |
| 39 | static PyObject *valpy_str (PyObject *self); |
| 40 | static PyObject *valpy_increment (PyObject *self, PyObject *args); |
| 41 | static PyObject *valpy_equal_p (PyObject *self, PyObject *args); |
| 42 | static PyObject * valpy_lazy_p (PyObject *self, PyObject *args); |
| 43 | static PyObject * valpy_fetch_lazy (PyObject *self, PyObject *args); |
| 44 | static PyObject * valpy_common_print (PyObject *self, PyObject *args); |
| 45 | |
| 46 | static PyMethodDef value_object_methods[] = { |
| 47 | { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." }, |
| 48 | { "get_element", valpy_get_element, METH_VARARGS, |
| 49 | "Obtains element inside value." }, |
| 50 | { "increment", valpy_increment, METH_VARARGS, |
| 51 | "Increment value by the given amount." }, |
| 52 | { "equals", valpy_equal_p, METH_VARARGS, "Compare values." }, |
| 53 | { "is_lazy", valpy_lazy_p, METH_NOARGS, |
| 54 | "Returns True if the value is lazy, False if not." }, |
| 55 | { "fetch_lazy", valpy_fetch_lazy, METH_NOARGS, |
| 56 | "Fetches value from inferior memory." }, |
| 57 | { "common_print", valpy_common_print, METH_VARARGS, "Prints value." }, |
| 58 | {NULL} /* Sentinel */ |
| 59 | }; |
| 60 | |
| 61 | PyTypeObject value_object_type = { |
| 62 | PyObject_HEAD_INIT (NULL) |
| 63 | 0, /*ob_size*/ |
| 64 | "gdb.Value", /*tp_name*/ |
| 65 | sizeof (value_object), /*tp_basicsize*/ |
| 66 | 0, /*tp_itemsize*/ |
| 67 | valpy_dealloc, /*tp_dealloc*/ |
| 68 | 0, /*tp_print*/ |
| 69 | 0, /*tp_getattr*/ |
| 70 | 0, /*tp_setattr*/ |
| 71 | 0, /*tp_compare*/ |
| 72 | 0, /*tp_repr*/ |
| 73 | 0, /*tp_as_number*/ |
| 74 | 0, /*tp_as_sequence*/ |
| 75 | 0, /*tp_as_mapping*/ |
| 76 | 0, /*tp_hash */ |
| 77 | 0, /*tp_call*/ |
| 78 | valpy_str, /*tp_str*/ |
| 79 | 0, /*tp_getattro*/ |
| 80 | 0, /*tp_setattro*/ |
| 81 | 0, /*tp_as_buffer*/ |
| 82 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 83 | "GDB value object", /* tp_doc */ |
| 84 | 0, /* tp_traverse */ |
| 85 | 0, /* tp_clear */ |
| 86 | 0, /* tp_richcompare */ |
| 87 | 0, /* tp_weaklistoffset */ |
| 88 | 0, /* tp_iter */ |
| 89 | 0, /* tp_iternext */ |
| 90 | value_object_methods /* tp_methods */ |
| 91 | }; |
| 92 | |
| 93 | /* Mantra used to return value object wrapping value struct. */ |
| 94 | #define VALPY_RETURN_VALUE(valpy_obj, value_st) \ |
| 95 | do { \ |
| 96 | valpy_obj = PyObject_New (value_object, &value_object_type); \ |
| 97 | if (valpy_obj != NULL) \ |
| 98 | { \ |
| 99 | valpy_obj->value = value_st; \ |
| 100 | release_value (value_st); \ |
| 101 | value_prepend_to_list (&values_in_python, value_st); \ |
| 102 | } \ |
| 103 | return (PyObject *) valpy_obj; \ |
| 104 | } while (0) |
| 105 | |
| 106 | |
| 107 | static void |
| 108 | valpy_dealloc (PyObject *obj) |
| 109 | { |
| 110 | value_object *self = (value_object *) obj; |
| 111 | |
| 112 | value_remove_from_list (&values_in_python, self->value); |
| 113 | |
| 114 | if (!self->owned_by_gdb) |
| 115 | value_free (self->value); |
| 116 | self->ob_type->tp_free (self); |
| 117 | } |
| 118 | |
| 119 | static PyObject * |
| 120 | valpy_dereference (PyObject *self, PyObject *args) |
| 121 | { |
| 122 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ |
| 123 | value_object *result; |
| 124 | volatile struct gdb_exception except; |
| 125 | |
| 126 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 127 | { |
| 128 | res_val = value_ind (((value_object *) self)->value); |
| 129 | } |
| 130 | GDB_PY_HANDLE_EXCEPTION (except); |
| 131 | |
| 132 | VALPY_RETURN_VALUE (result, res_val); |
| 133 | } |
| 134 | |
| 135 | /* Takes value and string name of element inside that value. */ |
| 136 | static PyObject * |
| 137 | valpy_get_element (PyObject *self, PyObject *args) |
| 138 | { |
| 139 | value_object *self_value = (value_object *) self; |
| 140 | char *field; |
| 141 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ |
| 142 | value_object *result; |
| 143 | volatile struct gdb_exception except; |
| 144 | |
| 145 | if (!PyArg_ParseTuple (args, "s", &field)) |
| 146 | return NULL; |
| 147 | |
| 148 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 149 | { |
| 150 | res_val = value_struct_elt (&self_value->value, NULL, field, 0, NULL); |
| 151 | } |
| 152 | GDB_PY_HANDLE_EXCEPTION (except); |
| 153 | |
| 154 | VALPY_RETURN_VALUE (result, res_val); |
| 155 | } |
| 156 | |
| 157 | /* FIXME: copy paste from varobj.c */ |
| 158 | static char * |
| 159 | value_get_print_value (struct value *value) |
| 160 | { |
| 161 | long dummy; |
| 162 | struct ui_file *stb; |
| 163 | struct cleanup *old_chain; |
| 164 | char *thevalue; |
| 165 | |
| 166 | if (value == NULL) |
| 167 | return NULL; |
| 168 | |
| 169 | stb = mem_fileopen (); |
| 170 | old_chain = make_cleanup_ui_file_delete (stb); |
| 171 | |
| 172 | common_val_print (value, stb, 0, 1, 0, 0); |
| 173 | thevalue = ui_file_xstrdup (stb, &dummy); |
| 174 | |
| 175 | do_cleanups (old_chain); |
| 176 | return thevalue; |
| 177 | } |
| 178 | |
| 179 | static PyObject * |
| 180 | valpy_str (PyObject *self) |
| 181 | { |
| 182 | char *s; |
| 183 | PyObject *result; |
| 184 | |
| 185 | s = value_get_print_value (((value_object *) self)->value); |
| 186 | result = PyString_FromString (s); |
| 187 | xfree (s); |
| 188 | |
| 189 | return result; |
| 190 | } |
| 191 | |
| 192 | static PyObject * |
| 193 | valpy_increment (PyObject *self, PyObject *args) |
| 194 | { |
| 195 | int i; |
| 196 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ |
| 197 | struct value *inc; |
| 198 | value_object *result; |
| 199 | volatile struct gdb_exception except; |
| 200 | |
| 201 | if (!PyArg_ParseTuple (args, "i", &i)) |
| 202 | return NULL; |
| 203 | |
| 204 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 205 | { |
| 206 | /* FIXME: clear everything. */ |
| 207 | inc = value_from_longest (builtin_type_int, i); |
| 208 | res_val = value_add (((value_object *) self)->value, inc); |
| 209 | } |
| 210 | GDB_PY_HANDLE_EXCEPTION (except); |
| 211 | |
| 212 | VALPY_RETURN_VALUE (result, res_val); |
| 213 | } |
| 214 | |
| 215 | static PyObject * |
| 216 | valpy_equal_p (PyObject *self, PyObject *args) |
| 217 | { |
| 218 | int equalp = 0; /* Initialize to appease gcc warning. */ |
| 219 | value_object *other; |
| 220 | volatile struct gdb_exception except; |
| 221 | |
| 222 | if (!PyArg_ParseTuple (args, "O!", &value_object_type, &other)) |
| 223 | return NULL; |
| 224 | |
| 225 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 226 | { |
| 227 | equalp = value_equal (((value_object *) self)->value, other->value); |
| 228 | } |
| 229 | GDB_PY_HANDLE_EXCEPTION (except); |
| 230 | |
| 231 | if (equalp == 1) |
| 232 | Py_RETURN_TRUE; |
| 233 | |
| 234 | Py_RETURN_FALSE; |
| 235 | } |
| 236 | |
| 237 | static PyObject * |
| 238 | valpy_lazy_p (PyObject *self, PyObject *args) |
| 239 | { |
| 240 | if (value_lazy (((value_object *) self)->value)) |
| 241 | Py_RETURN_TRUE; |
| 242 | |
| 243 | Py_RETURN_FALSE; |
| 244 | } |
| 245 | |
| 246 | static PyObject * |
| 247 | valpy_fetch_lazy (PyObject *self, PyObject *args) |
| 248 | { |
| 249 | volatile struct gdb_exception except; |
| 250 | |
| 251 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 252 | { |
| 253 | value_fetch_lazy (((value_object *) self)->value); |
| 254 | } |
| 255 | GDB_PY_HANDLE_EXCEPTION (except); |
| 256 | |
| 257 | Py_RETURN_NONE; |
| 258 | } |
| 259 | |
| 260 | static PyObject * |
| 261 | valpy_common_print (PyObject *self, PyObject *args) |
| 262 | { |
| 263 | value_object *self_value = (value_object *) self; |
| 264 | int ret = 0, format, recurse, pretty; |
| 265 | PyObject *format_obj, *deref_obj; |
| 266 | struct ui_stream *stb; |
| 267 | struct cleanup *chain; |
| 268 | volatile struct gdb_exception except; |
| 269 | |
| 270 | if (!PyArg_ParseTuple (args, "OO!ii", &format_obj, &PyBool_Type, &deref_obj, |
| 271 | &recurse, &pretty)) |
| 272 | return NULL; |
| 273 | |
| 274 | if (format_obj == Py_None) |
| 275 | format = 0; |
| 276 | else |
| 277 | { |
| 278 | if (!PyString_Check (format_obj) || PyString_Size (format_obj) != 1) |
| 279 | { |
| 280 | PyErr_SetString (PyExc_TypeError, "Argument 1 must be char or None."); |
| 281 | return NULL; |
| 282 | } |
| 283 | |
| 284 | format = *PyString_AsString (format_obj); |
| 285 | } |
| 286 | |
| 287 | stb = ui_out_stream_new (uiout); |
| 288 | chain = make_cleanup_ui_out_stream_delete (stb); |
| 289 | |
| 290 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 291 | { |
| 292 | ret = common_val_print (self_value->value, stb->stream, format, |
| 293 | deref_obj == Py_True, recurse, pretty); |
| 294 | /* FIXME: should I really use ui_out_field_stream? */ |
| 295 | ui_out_field_stream (uiout, "value", stb); |
| 296 | } |
| 297 | GDB_PY_HANDLE_EXCEPTION (except); |
| 298 | |
| 299 | do_cleanups (chain); |
| 300 | |
| 301 | return PyInt_FromLong (ret); |
| 302 | } |
| 303 | |
| 304 | /* A value owned by GDB is in the all_values chain, so it will be freed |
| 305 | automatically when not needed anymore (i.e., before the current command |
| 306 | completes). */ |
| 307 | PyObject * |
| 308 | gdb_owned_value_to_value_object (struct value *v) |
| 309 | { |
| 310 | value_object *result = PyObject_New (value_object, &value_object_type); |
| 311 | if (result != NULL) |
| 312 | { |
| 313 | result->value = v; |
| 314 | result->owned_by_gdb = 1; |
| 315 | /* FIXME: should we do it? What is it? */ |
| 316 | /* I don't think it is needed, since a GDB owned value has a very short |
| 317 | lifetime. The purpose of the list is explained in the comment above |
| 318 | its declaration. -- bauermann */ |
| 319 | value_prepend_to_list (&values_in_python, v); |
| 320 | } |
| 321 | return (PyObject *)result; |
| 322 | } |
| 323 | |
| 324 | /* Returns an object for a value which is released from the all_values chain, |
| 325 | so its lifetime is not bound to the execution of a command. */ |
| 326 | PyObject * |
| 327 | value_to_value_object (struct value *v) |
| 328 | { |
| 329 | value_object *val_obj; |
| 330 | |
| 331 | VALPY_RETURN_VALUE (val_obj, v); |
| 332 | } |
| 333 | |
| 334 | struct value * |
| 335 | value_object_to_value (PyObject *self) |
| 336 | { |
| 337 | value_object *real = (value_object *)self; |
| 338 | return real->value; |
| 339 | } |
| 340 | |
| 341 | PyObject * |
| 342 | gdbpy_make_value_from_int (PyObject *self, PyObject *args) |
| 343 | { |
| 344 | int i; |
| 345 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ |
| 346 | value_object *result; |
| 347 | volatile struct gdb_exception except; |
| 348 | |
| 349 | if (!PyArg_ParseTuple (args, "i", &i)) |
| 350 | return NULL; |
| 351 | |
| 352 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 353 | { |
| 354 | res_val = value_from_longest (builtin_type_int, i); |
| 355 | } |
| 356 | GDB_PY_HANDLE_EXCEPTION (except); |
| 357 | |
| 358 | VALPY_RETURN_VALUE (result, res_val); |
| 359 | } |
| 360 | |
| 361 | PyObject * |
| 362 | gdbpy_get_value_from_history (PyObject *self, PyObject *args) |
| 363 | { |
| 364 | int i; |
| 365 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ |
| 366 | value_object *result; |
| 367 | volatile struct gdb_exception except; |
| 368 | |
| 369 | if (!PyArg_ParseTuple (args, "i", &i)) |
| 370 | return NULL; |
| 371 | |
| 372 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 373 | { |
| 374 | res_val = access_value_history (i); |
| 375 | } |
| 376 | GDB_PY_HANDLE_EXCEPTION (except); |
| 377 | |
| 378 | VALPY_RETURN_VALUE (result, res_val); |
| 379 | } |
| 380 | |
| 381 | void |
| 382 | gdbpy_initialize_values (void) |
| 383 | { |
| 384 | value_object_type.tp_new = PyType_GenericNew; |
| 385 | if (PyType_Ready (&value_object_type) < 0) |
| 386 | return; |
| 387 | |
| 388 | Py_INCREF (&value_object_type); |
| 389 | PyModule_AddObject (gdb_module, "Value", (PyObject *) &value_object_type); |
| 390 | |
| 391 | values_in_python = NULL; |
| 392 | } |
| toggle raw diff |
--- /dev/null
+++ b/gdb/python/value.c
@@ -0,0 +1,392 @@
+/* Python interface to values.
+
+ Copyright (C) 2008 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "defs.h"
+#include "value.h"
+#include "exceptions.h"
+#include "python-internal.h"
+
+typedef struct {
+ PyObject_HEAD
+ struct value *value;
+ int owned_by_gdb;
+} value_object;
+
+/* List of all values which are currently exposed to Python. It is maintained
+ so that when an objfile is discarded, preserve_values can copy the values'
+ types if needed. */
+struct value *values_in_python;
+
+static void valpy_dealloc (PyObject *obj);
+static PyObject *valpy_dereference (PyObject *self, PyObject *args);
+static PyObject *valpy_get_element (PyObject *self, PyObject *args);
+static PyObject *valpy_str (PyObject *self);
+static PyObject *valpy_increment (PyObject *self, PyObject *args);
+static PyObject *valpy_equal_p (PyObject *self, PyObject *args);
+static PyObject * valpy_lazy_p (PyObject *self, PyObject *args);
+static PyObject * valpy_fetch_lazy (PyObject *self, PyObject *args);
+static PyObject * valpy_common_print (PyObject *self, PyObject *args);
+
+static PyMethodDef value_object_methods[] = {
+ { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
+ { "get_element", valpy_get_element, METH_VARARGS,
+ "Obtains element inside value." },
+ { "increment", valpy_increment, METH_VARARGS,
+ "Increment value by the given amount." },
+ { "equals", valpy_equal_p, METH_VARARGS, "Compare values." },
+ { "is_lazy", valpy_lazy_p, METH_NOARGS,
+ "Returns True if the value is lazy, False if not." },
+ { "fetch_lazy", valpy_fetch_lazy, METH_NOARGS,
+ "Fetches value from inferior memory." },
+ { "common_print", valpy_common_print, METH_VARARGS, "Prints value." },
+ {NULL} /* Sentinel */
+};
+
+PyTypeObject value_object_type = {
+ PyObject_HEAD_INIT (NULL)
+ 0, /*ob_size*/
+ "gdb.Value", /*tp_name*/
+ sizeof (value_object), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ valpy_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_compare*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash */
+ 0, /*tp_call*/
+ valpy_str, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
+ "GDB value object", /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ value_object_methods /* tp_methods */
+};
+
+/* Mantra used to return value object wrapping value struct. */
+#define VALPY_RETURN_VALUE(valpy_obj, value_st) \
+ do { \
+ valpy_obj = PyObject_New (value_object, &value_object_type); \
+ if (valpy_obj != NULL) \
+ { \
+ valpy_obj->value = value_st; \
+ release_value (value_st); \
+ value_prepend_to_list (&values_in_python, value_st); \
+ } \
+ return (PyObject *) valpy_obj; \
+ } while (0)
+
+
+static void
+valpy_dealloc (PyObject *obj)
+{
+ value_object *self = (value_object *) obj;
+
+ value_remove_from_list (&values_in_python, self->value);
+
+ if (!self->owned_by_gdb)
+ value_free (self->value);
+ self->ob_type->tp_free (self);
+}
+
+static PyObject *
+valpy_dereference (PyObject *self, PyObject *args)
+{
+ struct value *res_val = NULL; /* Initialize to appease gcc warning. */
+ value_object *result;
+ volatile struct gdb_exception except;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ res_val = value_ind (((value_object *) self)->value);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);
+
+ VALPY_RETURN_VALUE (result, res_val);
+}
+
+/* Takes value and string name of element inside that value. */
+static PyObject *
+valpy_get_element (PyObject *self, PyObject *args)
+{
+ value_object *self_value = (value_object *) self;
+ char *field;
+ struct value *res_val = NULL; /* Initialize to appease gcc warning. */
+ value_object *result;
+ volatile struct gdb_exception except;
+
+ if (!PyArg_ParseTuple (args, "s", &field))
+ return NULL;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ res_val = value_struct_elt (&self_value->value, NULL, field, 0, NULL);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);
+
+ VALPY_RETURN_VALUE (result, res_val);
+}
+
+/* FIXME: copy paste from varobj.c */
+static char *
+value_get_print_value (struct value *value)
+{
+ long dummy;
+ struct ui_file *stb;
+ struct cleanup *old_chain;
+ char *thevalue;
+
+ if (value == NULL)
+ return NULL;
+
+ stb = mem_fileopen ();
+ old_chain = make_cleanup_ui_file_delete (stb);
+
+ common_val_print (value, stb, 0, 1, 0, 0);
+ thevalue = ui_file_xstrdup (stb, &dummy);
+
+ do_cleanups (old_chain);
+ return thevalue;
+}
+
+static PyObject *
+valpy_str (PyObject *self)
+{
+ char *s;
+ PyObject *result;
+
+ s = value_get_print_value (((value_object *) self)->value);
+ result = PyString_FromString (s);
+ xfree (s);
+
+ return result;
+}
+
+static PyObject *
+valpy_increment (PyObject *self, PyObject *args)
+{
+ int i;
+ struct value *res_val = NULL; /* Initialize to appease gcc warning. */
+ struct value *inc;
+ value_object *result;
+ volatile struct gdb_exception except;
+
+ if (!PyArg_ParseTuple (args, "i", &i))
+ return NULL;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ /* FIXME: clear everything. */
+ inc = value_from_longest (builtin_type_int, i);
+ res_val = value_add (((value_object *) self)->value, inc);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);
+
+ VALPY_RETURN_VALUE (result, res_val);
+}
+
+static PyObject *
+valpy_equal_p (PyObject *self, PyObject *args)
+{
+ int equalp = 0; /* Initialize to appease gcc warning. */
+ value_object *other;
+ volatile struct gdb_exception except;
+
+ if (!PyArg_ParseTuple (args, "O!", &value_object_type, &other))
+ return NULL;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ equalp = value_equal (((value_object *) self)->value, other->value);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);
+
+ if (equalp == 1)
+ Py_RETURN_TRUE;
+
+ Py_RETURN_FALSE;
+}
+
+static PyObject *
+valpy_lazy_p (PyObject *self, PyObject *args)
+{
+ if (value_lazy (((value_object *) self)->value))
+ Py_RETURN_TRUE;
+
+ Py_RETURN_FALSE;
+}
+
+static PyObject *
+valpy_fetch_lazy (PyObject *self, PyObject *args)
+{
+ volatile struct gdb_exception except;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ value_fetch_lazy (((value_object *) self)->value);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);
+
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+valpy_common_print (PyObject *self, PyObject *args)
+{
+ value_object *self_value = (value_object *) self;
+ int ret = 0, format, recurse, pretty;
+ PyObject *format_obj, *deref_obj;
+ struct ui_stream *stb;
+ struct cleanup *chain;
+ volatile struct gdb_exception except;
+
+ if (!PyArg_ParseTuple (args, "OO!ii", &format_obj, &PyBool_Type, &deref_obj,
+ &recurse, &pretty))
+ return NULL;
+
+ if (format_obj == Py_None)
+ format = 0;
+ else
+ {
+ if (!PyString_Check (format_obj) || PyString_Size (format_obj) != 1)
+ {
+ PyErr_SetString (PyExc_TypeError, "Argument 1 must be char or None.");
+ return NULL;
+ }
+
+ format = *PyString_AsString (format_obj);
+ }
+
+ stb = ui_out_stream_new (uiout);
+ chain = make_cleanup_ui_out_stream_delete (stb);
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ ret = common_val_print (self_value->value, stb->stream, format,
+ deref_obj == Py_True, recurse, pretty);
+ /* FIXME: should I really use ui_out_field_stream? */
+ ui_out_field_stream (uiout, "value", stb);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);
+
+ do_cleanups (chain);
+
+ return PyInt_FromLong (ret);
+}
+
+/* A value owned by GDB is in the all_values chain, so it will be freed
+ automatically when not needed anymore (i.e., before the current command
+ completes). */
+PyObject *
+gdb_owned_value_to_value_object (struct value *v)
+{
+ value_object *result = PyObject_New (value_object, &value_object_type);
+ if (result != NULL)
+ {
+ result->value = v;
+ result->owned_by_gdb = 1;
+ /* FIXME: should we do it? What is it? */
+ /* I don't think it is needed, since a GDB owned value has a very short
+ lifetime. The purpose of the list is explained in the comment above
+ its declaration. -- bauermann */
+ value_prepend_to_list (&values_in_python, v);
+ }
+ return (PyObject *)result;
+}
+
+/* Returns an object for a value which is released from the all_values chain,
+ so its lifetime is not bound to the execution of a command. */
+PyObject *
+value_to_value_object (struct value *v)
+{
+ value_object *val_obj;
+
+ VALPY_RETURN_VALUE (val_obj, v);
+}
+
+struct value *
+value_object_to_value (PyObject *self)
+{
+ value_object *real = (value_object *)self;
+ return real->value;
+}
+
+PyObject *
+gdbpy_make_value_from_int (PyObject *self, PyObject *args)
+{
+ int i;
+ struct value *res_val = NULL; /* Initialize to appease gcc warning. */
+ value_object *result;
+ volatile struct gdb_exception except;
+
+ if (!PyArg_ParseTuple (args, "i", &i))
+ return NULL;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ res_val = value_from_longest (builtin_type_int, i);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);
+
+ VALPY_RETURN_VALUE (result, res_val);
+}
+
+PyObject *
+gdbpy_get_value_from_history (PyObject *self, PyObject *args)
+{
+ int i;
+ struct value *res_val = NULL; /* Initialize to appease gcc warning. */
+ value_object *result;
+ volatile struct gdb_exception except;
+
+ if (!PyArg_ParseTuple (args, "i", &i))
+ return NULL;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ res_val = access_value_history (i);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);
+
+ VALPY_RETURN_VALUE (result, res_val);
+}
+
+void
+gdbpy_initialize_values (void)
+{
+ value_object_type.tp_new = PyType_GenericNew;
+ if (PyType_Ready (&value_object_type) < 0)
+ return;
+
+ Py_INCREF (&value_object_type);
+ PyModule_AddObject (gdb_module, "Value", (PyObject *) &value_object_type);
+
+ values_in_python = NULL;
+} |