6622d11e8dc877f29d17d253f45545f12ea50814
[deliverable/binutils-gdb.git] / gdb / python / py-value.c
1 /* Python interface to values.
2
3 Copyright (C) 2008-2015 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 "charset.h"
22 #include "value.h"
23 #include "language.h"
24 #include "dfp.h"
25 #include "valprint.h"
26 #include "infcall.h"
27 #include "expression.h"
28 #include "cp-abi.h"
29 #include "python.h"
30
31 #include "python-internal.h"
32
33 /* Even though Python scalar types directly map to host types, we use
34 target types here to remain consistent with the values system in
35 GDB (which uses target arithmetic). */
36
37 /* Python's integer type corresponds to C's long type. */
38 #define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long
39
40 /* Python's float type corresponds to C's double type. */
41 #define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double
42
43 /* Python's long type corresponds to C's long long type. */
44 #define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long
45
46 /* Python's long type corresponds to C's long long type. Unsigned version. */
47 #define builtin_type_upylong builtin_type \
48 (python_gdbarch)->builtin_unsigned_long_long
49
50 #define builtin_type_pybool \
51 language_bool_type (python_language, python_gdbarch)
52
53 #define builtin_type_pychar \
54 language_string_char_type (python_language, python_gdbarch)
55
56 typedef struct value_object {
57 PyObject_HEAD
58 struct value_object *next;
59 struct value_object *prev;
60 struct value *value;
61 PyObject *address;
62 PyObject *type;
63 PyObject *dynamic_type;
64 } value_object;
65
66 /* List of all values which are currently exposed to Python. It is
67 maintained so that when an objfile is discarded, preserve_values
68 can copy the values' types if needed. */
69 /* This variable is unnecessarily initialized to NULL in order to
70 work around a linker bug on MacOS. */
71 static value_object *values_in_python = NULL;
72
73 /* Called by the Python interpreter when deallocating a value object. */
74 static void
75 valpy_dealloc (PyObject *obj)
76 {
77 value_object *self = (value_object *) obj;
78
79 /* Remove SELF from the global list. */
80 if (self->prev)
81 self->prev->next = self->next;
82 else
83 {
84 gdb_assert (values_in_python == self);
85 values_in_python = self->next;
86 }
87 if (self->next)
88 self->next->prev = self->prev;
89
90 value_free (self->value);
91
92 if (self->address)
93 /* Use braces to appease gcc warning. *sigh* */
94 {
95 Py_DECREF (self->address);
96 }
97
98 if (self->type)
99 {
100 Py_DECREF (self->type);
101 }
102
103 Py_XDECREF (self->dynamic_type);
104
105 Py_TYPE (self)->tp_free (self);
106 }
107
108 /* Helper to push a Value object on the global list. */
109 static void
110 note_value (value_object *value_obj)
111 {
112 value_obj->next = values_in_python;
113 if (value_obj->next)
114 value_obj->next->prev = value_obj;
115 value_obj->prev = NULL;
116 values_in_python = value_obj;
117 }
118
119 /* Called when a new gdb.Value object needs to be allocated. Returns NULL on
120 error, with a python exception set. */
121 static PyObject *
122 valpy_new (PyTypeObject *subtype, PyObject *args, PyObject *keywords)
123 {
124 struct value *value = NULL; /* Initialize to appease gcc warning. */
125 value_object *value_obj;
126
127 if (PyTuple_Size (args) != 1)
128 {
129 PyErr_SetString (PyExc_TypeError, _("Value object creation takes only "
130 "1 argument"));
131 return NULL;
132 }
133
134 value_obj = (value_object *) subtype->tp_alloc (subtype, 1);
135 if (value_obj == NULL)
136 {
137 PyErr_SetString (PyExc_MemoryError, _("Could not allocate memory to "
138 "create Value object."));
139 return NULL;
140 }
141
142 value = convert_value_from_python (PyTuple_GetItem (args, 0));
143 if (value == NULL)
144 {
145 subtype->tp_free (value_obj);
146 return NULL;
147 }
148
149 value_obj->value = value;
150 release_value_or_incref (value);
151 value_obj->address = NULL;
152 value_obj->type = NULL;
153 value_obj->dynamic_type = NULL;
154 note_value (value_obj);
155
156 return (PyObject *) value_obj;
157 }
158
159 /* Iterate over all the Value objects, calling preserve_one_value on
160 each. */
161 void
162 gdbpy_preserve_values (const struct extension_language_defn *extlang,
163 struct objfile *objfile, htab_t copied_types)
164 {
165 value_object *iter;
166
167 for (iter = values_in_python; iter; iter = iter->next)
168 preserve_one_value (iter->value, objfile, copied_types);
169 }
170
171 /* Given a value of a pointer type, apply the C unary * operator to it. */
172 static PyObject *
173 valpy_dereference (PyObject *self, PyObject *args)
174 {
175 PyObject *result = NULL;
176
177 TRY
178 {
179 struct value *res_val;
180 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
181
182 res_val = value_ind (((value_object *) self)->value);
183 result = value_to_value_object (res_val);
184 do_cleanups (cleanup);
185 }
186 CATCH (except, RETURN_MASK_ALL)
187 {
188 GDB_PY_HANDLE_EXCEPTION (except);
189 }
190 END_CATCH
191
192 return result;
193 }
194
195 /* Given a value of a pointer type or a reference type, return the value
196 referenced. The difference between this function and valpy_dereference is
197 that the latter applies * unary operator to a value, which need not always
198 result in the value referenced. For example, for a value which is a reference
199 to an 'int' pointer ('int *'), valpy_dereference will result in a value of
200 type 'int' while valpy_referenced_value will result in a value of type
201 'int *'. */
202
203 static PyObject *
204 valpy_referenced_value (PyObject *self, PyObject *args)
205 {
206 PyObject *result = NULL;
207
208 TRY
209 {
210 struct value *self_val, *res_val;
211 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
212
213 self_val = ((value_object *) self)->value;
214 switch (TYPE_CODE (check_typedef (value_type (self_val))))
215 {
216 case TYPE_CODE_PTR:
217 res_val = value_ind (self_val);
218 break;
219 case TYPE_CODE_REF:
220 res_val = coerce_ref (self_val);
221 break;
222 default:
223 error(_("Trying to get the referenced value from a value which is "
224 "neither a pointer nor a reference."));
225 }
226
227 result = value_to_value_object (res_val);
228 do_cleanups (cleanup);
229 }
230 CATCH (except, RETURN_MASK_ALL)
231 {
232 GDB_PY_HANDLE_EXCEPTION (except);
233 }
234 END_CATCH
235
236 return result;
237 }
238
239 /* Return "&value". */
240 static PyObject *
241 valpy_get_address (PyObject *self, void *closure)
242 {
243 value_object *val_obj = (value_object *) self;
244
245 if (!val_obj->address)
246 {
247 TRY
248 {
249 struct value *res_val;
250 struct cleanup *cleanup
251 = make_cleanup_value_free_to_mark (value_mark ());
252
253 res_val = value_addr (val_obj->value);
254 val_obj->address = value_to_value_object (res_val);
255 do_cleanups (cleanup);
256 }
257 CATCH (except, RETURN_MASK_ALL)
258 {
259 val_obj->address = Py_None;
260 Py_INCREF (Py_None);
261 }
262 END_CATCH
263 }
264
265 Py_XINCREF (val_obj->address);
266
267 return val_obj->address;
268 }
269
270 /* Return type of the value. */
271 static PyObject *
272 valpy_get_type (PyObject *self, void *closure)
273 {
274 value_object *obj = (value_object *) self;
275
276 if (!obj->type)
277 {
278 obj->type = type_to_type_object (value_type (obj->value));
279 if (!obj->type)
280 return NULL;
281 }
282 Py_INCREF (obj->type);
283 return obj->type;
284 }
285
286 /* Return dynamic type of the value. */
287
288 static PyObject *
289 valpy_get_dynamic_type (PyObject *self, void *closure)
290 {
291 value_object *obj = (value_object *) self;
292 struct type *type = NULL;
293
294 if (obj->dynamic_type != NULL)
295 {
296 Py_INCREF (obj->dynamic_type);
297 return obj->dynamic_type;
298 }
299
300 TRY
301 {
302 struct value *val = obj->value;
303 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
304
305 type = value_type (val);
306 CHECK_TYPEDEF (type);
307
308 if (((TYPE_CODE (type) == TYPE_CODE_PTR)
309 || (TYPE_CODE (type) == TYPE_CODE_REF))
310 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT))
311 {
312 struct value *target;
313 int was_pointer = TYPE_CODE (type) == TYPE_CODE_PTR;
314
315 if (was_pointer)
316 target = value_ind (val);
317 else
318 target = coerce_ref (val);
319 type = value_rtti_type (target, NULL, NULL, NULL);
320
321 if (type)
322 {
323 if (was_pointer)
324 type = lookup_pointer_type (type);
325 else
326 type = lookup_reference_type (type);
327 }
328 }
329 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
330 type = value_rtti_type (val, NULL, NULL, NULL);
331 else
332 {
333 /* Re-use object's static type. */
334 type = NULL;
335 }
336
337 do_cleanups (cleanup);
338 }
339 CATCH (except, RETURN_MASK_ALL)
340 {
341 GDB_PY_HANDLE_EXCEPTION (except);
342 }
343 END_CATCH
344
345 if (type == NULL)
346 obj->dynamic_type = valpy_get_type (self, NULL);
347 else
348 obj->dynamic_type = type_to_type_object (type);
349
350 Py_XINCREF (obj->dynamic_type);
351 return obj->dynamic_type;
352 }
353
354 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
355 string. Return a PyObject representing a lazy_string_object type.
356 A lazy string is a pointer to a string with an optional encoding and
357 length. If ENCODING is not given, encoding is set to None. If an
358 ENCODING is provided the encoding parameter is set to ENCODING, but
359 the string is not encoded. If LENGTH is provided then the length
360 parameter is set to LENGTH, otherwise length will be set to -1 (first
361 null of appropriate with). */
362 static PyObject *
363 valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw)
364 {
365 gdb_py_longest length = -1;
366 struct value *value = ((value_object *) self)->value;
367 const char *user_encoding = NULL;
368 static char *keywords[] = { "encoding", "length", NULL };
369 PyObject *str_obj = NULL;
370
371 if (!PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG, keywords,
372 &user_encoding, &length))
373 return NULL;
374
375 TRY
376 {
377 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
378
379 if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR)
380 value = value_ind (value);
381
382 str_obj = gdbpy_create_lazy_string_object (value_address (value), length,
383 user_encoding,
384 value_type (value));
385
386 do_cleanups (cleanup);
387 }
388 CATCH (except, RETURN_MASK_ALL)
389 {
390 GDB_PY_HANDLE_EXCEPTION (except);
391 }
392 END_CATCH
393
394 return str_obj;
395 }
396
397 /* Implementation of gdb.Value.string ([encoding] [, errors]
398 [, length]) -> string. Return Unicode string with value contents.
399 If ENCODING is not given, the string is assumed to be encoded in
400 the target's charset. If LENGTH is provided, only fetch string to
401 the length provided. */
402
403 static PyObject *
404 valpy_string (PyObject *self, PyObject *args, PyObject *kw)
405 {
406 int length = -1;
407 gdb_byte *buffer;
408 struct value *value = ((value_object *) self)->value;
409 PyObject *unicode;
410 const char *encoding = NULL;
411 const char *errors = NULL;
412 const char *user_encoding = NULL;
413 const char *la_encoding = NULL;
414 struct type *char_type;
415 static char *keywords[] = { "encoding", "errors", "length", NULL };
416
417 if (!PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords,
418 &user_encoding, &errors, &length))
419 return NULL;
420
421 TRY
422 {
423 LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding);
424 }
425 CATCH (except, RETURN_MASK_ALL)
426 {
427 GDB_PY_HANDLE_EXCEPTION (except);
428 }
429 END_CATCH
430
431 encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding;
432 unicode = PyUnicode_Decode ((const char *) buffer,
433 length * TYPE_LENGTH (char_type),
434 encoding, errors);
435 xfree (buffer);
436
437 return unicode;
438 }
439
440 /* A helper function that implements the various cast operators. */
441
442 static PyObject *
443 valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op)
444 {
445 PyObject *type_obj, *result = NULL;
446 struct type *type;
447
448 if (! PyArg_ParseTuple (args, "O", &type_obj))
449 return NULL;
450
451 type = type_object_to_type (type_obj);
452 if (! type)
453 {
454 PyErr_SetString (PyExc_RuntimeError,
455 _("Argument must be a type."));
456 return NULL;
457 }
458
459 TRY
460 {
461 struct value *val = ((value_object *) self)->value;
462 struct value *res_val;
463 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
464
465 if (op == UNOP_DYNAMIC_CAST)
466 res_val = value_dynamic_cast (type, val);
467 else if (op == UNOP_REINTERPRET_CAST)
468 res_val = value_reinterpret_cast (type, val);
469 else
470 {
471 gdb_assert (op == UNOP_CAST);
472 res_val = value_cast (type, val);
473 }
474
475 result = value_to_value_object (res_val);
476 do_cleanups (cleanup);
477 }
478 CATCH (except, RETURN_MASK_ALL)
479 {
480 GDB_PY_HANDLE_EXCEPTION (except);
481 }
482 END_CATCH
483
484 return result;
485 }
486
487 /* Implementation of the "cast" method. */
488
489 static PyObject *
490 valpy_cast (PyObject *self, PyObject *args)
491 {
492 return valpy_do_cast (self, args, UNOP_CAST);
493 }
494
495 /* Implementation of the "dynamic_cast" method. */
496
497 static PyObject *
498 valpy_dynamic_cast (PyObject *self, PyObject *args)
499 {
500 return valpy_do_cast (self, args, UNOP_DYNAMIC_CAST);
501 }
502
503 /* Implementation of the "reinterpret_cast" method. */
504
505 static PyObject *
506 valpy_reinterpret_cast (PyObject *self, PyObject *args)
507 {
508 return valpy_do_cast (self, args, UNOP_REINTERPRET_CAST);
509 }
510
511 static Py_ssize_t
512 valpy_length (PyObject *self)
513 {
514 /* We don't support getting the number of elements in a struct / class. */
515 PyErr_SetString (PyExc_NotImplementedError,
516 _("Invalid operation on gdb.Value."));
517 return -1;
518 }
519
520 /* Return 1 if the gdb.Field object FIELD is present in the value V.
521 Returns 0 otherwise. If any Python error occurs, -1 is returned. */
522
523 static int
524 value_has_field (struct value *v, PyObject *field)
525 {
526 struct type *parent_type, *val_type;
527 enum type_code type_code;
528 PyObject *type_object = PyObject_GetAttrString (field, "parent_type");
529 int has_field = 0;
530
531 if (type_object == NULL)
532 return -1;
533
534 parent_type = type_object_to_type (type_object);
535 Py_DECREF (type_object);
536 if (parent_type == NULL)
537 {
538 PyErr_SetString (PyExc_TypeError,
539 _("'parent_type' attribute of gdb.Field object is not a"
540 "gdb.Type object."));
541 return -1;
542 }
543
544 TRY
545 {
546 val_type = value_type (v);
547 val_type = check_typedef (val_type);
548 if (TYPE_CODE (val_type) == TYPE_CODE_REF
549 || TYPE_CODE (val_type) == TYPE_CODE_PTR)
550 val_type = check_typedef (TYPE_TARGET_TYPE (val_type));
551
552 type_code = TYPE_CODE (val_type);
553 if ((type_code == TYPE_CODE_STRUCT || type_code == TYPE_CODE_UNION)
554 && types_equal (val_type, parent_type))
555 has_field = 1;
556 else
557 has_field = 0;
558 }
559 CATCH (except, RETURN_MASK_ALL)
560 {
561 GDB_PY_SET_HANDLE_EXCEPTION (except);
562 }
563 END_CATCH
564
565 return has_field;
566 }
567
568 /* Return the value of a flag FLAG_NAME in a gdb.Field object FIELD.
569 Returns 1 if the flag value is true, 0 if it is false, and -1 if
570 a Python error occurs. */
571
572 static int
573 get_field_flag (PyObject *field, const char *flag_name)
574 {
575 int flag_value;
576 PyObject *flag_object = PyObject_GetAttrString (field, flag_name);
577
578 if (flag_object == NULL)
579 return -1;
580
581 flag_value = PyObject_IsTrue (flag_object);
582 Py_DECREF (flag_object);
583
584 return flag_value;
585 }
586
587 /* Return the "type" attribute of a gdb.Field object.
588 Returns NULL on error, with a Python exception set. */
589
590 static struct type *
591 get_field_type (PyObject *field)
592 {
593 PyObject *ftype_obj = PyObject_GetAttrString (field, "type");
594 struct type *ftype;
595
596 if (ftype_obj == NULL)
597 return NULL;
598 ftype = type_object_to_type (ftype_obj);
599 Py_DECREF (ftype_obj);
600 if (ftype == NULL)
601 PyErr_SetString (PyExc_TypeError,
602 _("'type' attribute of gdb.Field object is not a "
603 "gdb.Type object."));
604
605 return ftype;
606 }
607
608 /* Given string name or a gdb.Field object corresponding to an element inside
609 a structure, return its value object. Returns NULL on error, with a python
610 exception set. */
611
612 static PyObject *
613 valpy_getitem (PyObject *self, PyObject *key)
614 {
615 struct gdb_exception except = exception_none;
616 value_object *self_value = (value_object *) self;
617 char *field = NULL;
618 struct type *base_class_type = NULL, *field_type = NULL;
619 long bitpos = -1;
620 PyObject *result = NULL;
621
622 if (gdbpy_is_string (key))
623 {
624 field = python_string_to_host_string (key);
625 if (field == NULL)
626 return NULL;
627 }
628 else if (gdbpy_is_field (key))
629 {
630 int is_base_class, valid_field;
631
632 valid_field = value_has_field (self_value->value, key);
633 if (valid_field < 0)
634 return NULL;
635 else if (valid_field == 0)
636 {
637 PyErr_SetString (PyExc_TypeError,
638 _("Invalid lookup for a field not contained in "
639 "the value."));
640
641 return NULL;
642 }
643
644 is_base_class = get_field_flag (key, "is_base_class");
645 if (is_base_class < 0)
646 return NULL;
647 else if (is_base_class > 0)
648 {
649 base_class_type = get_field_type (key);
650 if (base_class_type == NULL)
651 return NULL;
652 }
653 else
654 {
655 PyObject *name_obj = PyObject_GetAttrString (key, "name");
656
657 if (name_obj == NULL)
658 return NULL;
659
660 if (name_obj != Py_None)
661 {
662 field = python_string_to_host_string (name_obj);
663 Py_DECREF (name_obj);
664 if (field == NULL)
665 return NULL;
666 }
667 else
668 {
669 PyObject *bitpos_obj;
670 int valid;
671
672 Py_DECREF (name_obj);
673
674 if (!PyObject_HasAttrString (key, "bitpos"))
675 {
676 PyErr_SetString (PyExc_AttributeError,
677 _("gdb.Field object has no name and no "
678 "'bitpos' attribute."));
679
680 return NULL;
681 }
682 bitpos_obj = PyObject_GetAttrString (key, "bitpos");
683 if (bitpos_obj == NULL)
684 return NULL;
685 valid = gdb_py_int_as_long (bitpos_obj, &bitpos);
686 Py_DECREF (bitpos_obj);
687 if (!valid)
688 return NULL;
689
690 field_type = get_field_type (key);
691 if (field_type == NULL)
692 return NULL;
693 }
694 }
695 }
696
697 TRY
698 {
699 struct value *tmp = self_value->value;
700 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
701 struct value *res_val = NULL;
702
703 if (field)
704 res_val = value_struct_elt (&tmp, NULL, field, 0, NULL);
705 else if (bitpos >= 0)
706 res_val = value_struct_elt_bitpos (&tmp, bitpos, field_type,
707 "struct/class/union");
708 else if (base_class_type != NULL)
709 {
710 struct type *val_type;
711
712 val_type = check_typedef (value_type (tmp));
713 if (TYPE_CODE (val_type) == TYPE_CODE_PTR)
714 res_val = value_cast (lookup_pointer_type (base_class_type), tmp);
715 else if (TYPE_CODE (val_type) == TYPE_CODE_REF)
716 res_val = value_cast (lookup_reference_type (base_class_type), tmp);
717 else
718 res_val = value_cast (base_class_type, tmp);
719 }
720 else
721 {
722 /* Assume we are attempting an array access, and let the
723 value code throw an exception if the index has an invalid
724 type. */
725 struct value *idx = convert_value_from_python (key);
726
727 if (idx != NULL)
728 {
729 /* Check the value's type is something that can be accessed via
730 a subscript. */
731 struct type *type;
732
733 tmp = coerce_ref (tmp);
734 type = check_typedef (value_type (tmp));
735 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
736 && TYPE_CODE (type) != TYPE_CODE_PTR)
737 error (_("Cannot subscript requested type."));
738 else
739 res_val = value_subscript (tmp, value_as_long (idx));
740 }
741 }
742
743 if (res_val)
744 result = value_to_value_object (res_val);
745 do_cleanups (cleanup);
746 }
747 CATCH (ex, RETURN_MASK_ALL)
748 {
749 except = ex;
750 }
751 END_CATCH
752
753 xfree (field);
754 GDB_PY_HANDLE_EXCEPTION (except);
755
756 return result;
757 }
758
759 static int
760 valpy_setitem (PyObject *self, PyObject *key, PyObject *value)
761 {
762 PyErr_Format (PyExc_NotImplementedError,
763 _("Setting of struct elements is not currently supported."));
764 return -1;
765 }
766
767 /* Called by the Python interpreter to perform an inferior function
768 call on the value. Returns NULL on error, with a python exception set. */
769 static PyObject *
770 valpy_call (PyObject *self, PyObject *args, PyObject *keywords)
771 {
772 Py_ssize_t args_count;
773 struct value *function = ((value_object *) self)->value;
774 struct value **vargs = NULL;
775 struct type *ftype = NULL;
776 struct value *mark = value_mark ();
777 PyObject *result = NULL;
778
779 TRY
780 {
781 ftype = check_typedef (value_type (function));
782 }
783 CATCH (except, RETURN_MASK_ALL)
784 {
785 GDB_PY_HANDLE_EXCEPTION (except);
786 }
787 END_CATCH
788
789 if (TYPE_CODE (ftype) != TYPE_CODE_FUNC)
790 {
791 PyErr_SetString (PyExc_RuntimeError,
792 _("Value is not callable (not TYPE_CODE_FUNC)."));
793 return NULL;
794 }
795
796 if (! PyTuple_Check (args))
797 {
798 PyErr_SetString (PyExc_TypeError,
799 _("Inferior arguments must be provided in a tuple."));
800 return NULL;
801 }
802
803 args_count = PyTuple_Size (args);
804 if (args_count > 0)
805 {
806 int i;
807
808 vargs = alloca (sizeof (struct value *) * args_count);
809 for (i = 0; i < args_count; i++)
810 {
811 PyObject *item = PyTuple_GetItem (args, i);
812
813 if (item == NULL)
814 return NULL;
815
816 vargs[i] = convert_value_from_python (item);
817 if (vargs[i] == NULL)
818 return NULL;
819 }
820 }
821
822 TRY
823 {
824 struct cleanup *cleanup = make_cleanup_value_free_to_mark (mark);
825 struct value *return_value;
826
827 return_value = call_function_by_hand (function, args_count, vargs);
828 result = value_to_value_object (return_value);
829 do_cleanups (cleanup);
830 }
831 CATCH (except, RETURN_MASK_ALL)
832 {
833 GDB_PY_HANDLE_EXCEPTION (except);
834 }
835 END_CATCH
836
837 return result;
838 }
839
840 /* Called by the Python interpreter to obtain string representation
841 of the object. */
842 static PyObject *
843 valpy_str (PyObject *self)
844 {
845 char *s = NULL;
846 PyObject *result;
847 struct value_print_options opts;
848
849 get_user_print_options (&opts);
850 opts.deref_ref = 0;
851
852 TRY
853 {
854 struct ui_file *stb = mem_fileopen ();
855 struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
856
857 common_val_print (((value_object *) self)->value, stb, 0,
858 &opts, python_language);
859 s = ui_file_xstrdup (stb, NULL);
860
861 do_cleanups (old_chain);
862 }
863 CATCH (except, RETURN_MASK_ALL)
864 {
865 GDB_PY_HANDLE_EXCEPTION (except);
866 }
867 END_CATCH
868
869 result = PyUnicode_Decode (s, strlen (s), host_charset (), NULL);
870 xfree (s);
871
872 return result;
873 }
874
875 /* Implements gdb.Value.is_optimized_out. */
876 static PyObject *
877 valpy_get_is_optimized_out (PyObject *self, void *closure)
878 {
879 struct value *value = ((value_object *) self)->value;
880 int opt = 0;
881
882 TRY
883 {
884 opt = value_optimized_out (value);
885 }
886 CATCH (except, RETURN_MASK_ALL)
887 {
888 GDB_PY_HANDLE_EXCEPTION (except);
889 }
890 END_CATCH
891
892 if (opt)
893 Py_RETURN_TRUE;
894
895 Py_RETURN_FALSE;
896 }
897
898 /* Implements gdb.Value.is_lazy. */
899 static PyObject *
900 valpy_get_is_lazy (PyObject *self, void *closure)
901 {
902 struct value *value = ((value_object *) self)->value;
903 int opt = 0;
904
905 TRY
906 {
907 opt = value_lazy (value);
908 }
909 CATCH (except, RETURN_MASK_ALL)
910 {
911 GDB_PY_HANDLE_EXCEPTION (except);
912 }
913 END_CATCH
914
915 if (opt)
916 Py_RETURN_TRUE;
917
918 Py_RETURN_FALSE;
919 }
920
921 /* Implements gdb.Value.fetch_lazy (). */
922 static PyObject *
923 valpy_fetch_lazy (PyObject *self, PyObject *args)
924 {
925 struct value *value = ((value_object *) self)->value;
926
927 TRY
928 {
929 if (value_lazy (value))
930 value_fetch_lazy (value);
931 }
932 CATCH (except, RETURN_MASK_ALL)
933 {
934 GDB_PY_HANDLE_EXCEPTION (except);
935 }
936 END_CATCH
937
938 Py_RETURN_NONE;
939 }
940
941 /* Calculate and return the address of the PyObject as the value of
942 the builtin __hash__ call. */
943 static Py_hash_t
944 valpy_hash (PyObject *self)
945 {
946 return (intptr_t) self;
947 }
948
949 enum valpy_opcode
950 {
951 VALPY_ADD,
952 VALPY_SUB,
953 VALPY_MUL,
954 VALPY_DIV,
955 VALPY_REM,
956 VALPY_POW,
957 VALPY_LSH,
958 VALPY_RSH,
959 VALPY_BITAND,
960 VALPY_BITOR,
961 VALPY_BITXOR
962 };
963
964 /* If TYPE is a reference, return the target; otherwise return TYPE. */
965 #define STRIP_REFERENCE(TYPE) \
966 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
967
968 /* Returns a value object which is the result of applying the operation
969 specified by OPCODE to the given arguments. Returns NULL on error, with
970 a python exception set. */
971 static PyObject *
972 valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
973 {
974 PyObject *result = NULL;
975
976 TRY
977 {
978 struct value *arg1, *arg2;
979 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
980 struct value *res_val = NULL;
981 enum exp_opcode op = OP_NULL;
982 int handled = 0;
983
984 /* If the gdb.Value object is the second operand, then it will be passed
985 to us as the OTHER argument, and SELF will be an entirely different
986 kind of object, altogether. Because of this, we can't assume self is
987 a gdb.Value object and need to convert it from python as well. */
988 arg1 = convert_value_from_python (self);
989 if (arg1 == NULL)
990 {
991 do_cleanups (cleanup);
992 break;
993 }
994
995 arg2 = convert_value_from_python (other);
996 if (arg2 == NULL)
997 {
998 do_cleanups (cleanup);
999 break;
1000 }
1001
1002 switch (opcode)
1003 {
1004 case VALPY_ADD:
1005 {
1006 struct type *ltype = value_type (arg1);
1007 struct type *rtype = value_type (arg2);
1008
1009 CHECK_TYPEDEF (ltype);
1010 ltype = STRIP_REFERENCE (ltype);
1011 CHECK_TYPEDEF (rtype);
1012 rtype = STRIP_REFERENCE (rtype);
1013
1014 handled = 1;
1015 if (TYPE_CODE (ltype) == TYPE_CODE_PTR
1016 && is_integral_type (rtype))
1017 res_val = value_ptradd (arg1, value_as_long (arg2));
1018 else if (TYPE_CODE (rtype) == TYPE_CODE_PTR
1019 && is_integral_type (ltype))
1020 res_val = value_ptradd (arg2, value_as_long (arg1));
1021 else
1022 {
1023 handled = 0;
1024 op = BINOP_ADD;
1025 }
1026 }
1027 break;
1028 case VALPY_SUB:
1029 {
1030 struct type *ltype = value_type (arg1);
1031 struct type *rtype = value_type (arg2);
1032
1033 CHECK_TYPEDEF (ltype);
1034 ltype = STRIP_REFERENCE (ltype);
1035 CHECK_TYPEDEF (rtype);
1036 rtype = STRIP_REFERENCE (rtype);
1037
1038 handled = 1;
1039 if (TYPE_CODE (ltype) == TYPE_CODE_PTR
1040 && TYPE_CODE (rtype) == TYPE_CODE_PTR)
1041 /* A ptrdiff_t for the target would be preferable here. */
1042 res_val = value_from_longest (builtin_type_pyint,
1043 value_ptrdiff (arg1, arg2));
1044 else if (TYPE_CODE (ltype) == TYPE_CODE_PTR
1045 && is_integral_type (rtype))
1046 res_val = value_ptradd (arg1, - value_as_long (arg2));
1047 else
1048 {
1049 handled = 0;
1050 op = BINOP_SUB;
1051 }
1052 }
1053 break;
1054 case VALPY_MUL:
1055 op = BINOP_MUL;
1056 break;
1057 case VALPY_DIV:
1058 op = BINOP_DIV;
1059 break;
1060 case VALPY_REM:
1061 op = BINOP_REM;
1062 break;
1063 case VALPY_POW:
1064 op = BINOP_EXP;
1065 break;
1066 case VALPY_LSH:
1067 op = BINOP_LSH;
1068 break;
1069 case VALPY_RSH:
1070 op = BINOP_RSH;
1071 break;
1072 case VALPY_BITAND:
1073 op = BINOP_BITWISE_AND;
1074 break;
1075 case VALPY_BITOR:
1076 op = BINOP_BITWISE_IOR;
1077 break;
1078 case VALPY_BITXOR:
1079 op = BINOP_BITWISE_XOR;
1080 break;
1081 }
1082
1083 if (!handled)
1084 {
1085 if (binop_user_defined_p (op, arg1, arg2))
1086 res_val = value_x_binop (arg1, arg2, op, OP_NULL, EVAL_NORMAL);
1087 else
1088 res_val = value_binop (arg1, arg2, op);
1089 }
1090
1091 if (res_val)
1092 result = value_to_value_object (res_val);
1093
1094 do_cleanups (cleanup);
1095 }
1096 CATCH (except, RETURN_MASK_ALL)
1097 {
1098 GDB_PY_HANDLE_EXCEPTION (except);
1099 }
1100 END_CATCH
1101
1102 return result;
1103 }
1104
1105 static PyObject *
1106 valpy_add (PyObject *self, PyObject *other)
1107 {
1108 return valpy_binop (VALPY_ADD, self, other);
1109 }
1110
1111 static PyObject *
1112 valpy_subtract (PyObject *self, PyObject *other)
1113 {
1114 return valpy_binop (VALPY_SUB, self, other);
1115 }
1116
1117 static PyObject *
1118 valpy_multiply (PyObject *self, PyObject *other)
1119 {
1120 return valpy_binop (VALPY_MUL, self, other);
1121 }
1122
1123 static PyObject *
1124 valpy_divide (PyObject *self, PyObject *other)
1125 {
1126 return valpy_binop (VALPY_DIV, self, other);
1127 }
1128
1129 static PyObject *
1130 valpy_remainder (PyObject *self, PyObject *other)
1131 {
1132 return valpy_binop (VALPY_REM, self, other);
1133 }
1134
1135 static PyObject *
1136 valpy_power (PyObject *self, PyObject *other, PyObject *unused)
1137 {
1138 /* We don't support the ternary form of pow. I don't know how to express
1139 that, so let's just throw NotImplementedError to at least do something
1140 about it. */
1141 if (unused != Py_None)
1142 {
1143 PyErr_SetString (PyExc_NotImplementedError,
1144 "Invalid operation on gdb.Value.");
1145 return NULL;
1146 }
1147
1148 return valpy_binop (VALPY_POW, self, other);
1149 }
1150
1151 static PyObject *
1152 valpy_negative (PyObject *self)
1153 {
1154 PyObject *result = NULL;
1155
1156 TRY
1157 {
1158 /* Perhaps overkill, but consistency has some virtue. */
1159 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
1160 struct value *val;
1161
1162 val = value_neg (((value_object *) self)->value);
1163 result = value_to_value_object (val);
1164 do_cleanups (cleanup);
1165 }
1166 CATCH (except, RETURN_MASK_ALL)
1167 {
1168 GDB_PY_HANDLE_EXCEPTION (except);
1169 }
1170 END_CATCH
1171
1172 return result;
1173 }
1174
1175 static PyObject *
1176 valpy_positive (PyObject *self)
1177 {
1178 return value_to_value_object (((value_object *) self)->value);
1179 }
1180
1181 static PyObject *
1182 valpy_absolute (PyObject *self)
1183 {
1184 struct value *value = ((value_object *) self)->value;
1185 int isabs = 1;
1186
1187 TRY
1188 {
1189 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
1190
1191 if (value_less (value, value_zero (value_type (value), not_lval)))
1192 isabs = 0;
1193
1194 do_cleanups (cleanup);
1195 }
1196 CATCH (except, RETURN_MASK_ALL)
1197 {
1198 GDB_PY_HANDLE_EXCEPTION (except);
1199 }
1200 END_CATCH
1201
1202 if (isabs)
1203 return valpy_positive (self);
1204 else
1205 return valpy_negative (self);
1206 }
1207
1208 /* Implements boolean evaluation of gdb.Value. */
1209 static int
1210 valpy_nonzero (PyObject *self)
1211 {
1212 struct gdb_exception except = exception_none;
1213 value_object *self_value = (value_object *) self;
1214 struct type *type;
1215 int nonzero = 0; /* Appease GCC warning. */
1216
1217 TRY
1218 {
1219 type = check_typedef (value_type (self_value->value));
1220
1221 if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR)
1222 nonzero = !!value_as_long (self_value->value);
1223 else if (TYPE_CODE (type) == TYPE_CODE_FLT)
1224 nonzero = value_as_double (self_value->value) != 0;
1225 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1226 nonzero = !decimal_is_zero (value_contents (self_value->value),
1227 TYPE_LENGTH (type),
1228 gdbarch_byte_order (get_type_arch (type)));
1229 else
1230 /* All other values are True. */
1231 nonzero = 1;
1232 }
1233 CATCH (ex, RETURN_MASK_ALL)
1234 {
1235 except = ex;
1236 }
1237 END_CATCH
1238
1239 /* This is not documented in the Python documentation, but if this
1240 function fails, return -1 as slot_nb_nonzero does (the default
1241 Python nonzero function). */
1242 GDB_PY_SET_HANDLE_EXCEPTION (except);
1243
1244 return nonzero;
1245 }
1246
1247 /* Implements ~ for value objects. */
1248 static PyObject *
1249 valpy_invert (PyObject *self)
1250 {
1251 struct value *val = NULL;
1252
1253 TRY
1254 {
1255 val = value_complement (((value_object *) self)->value);
1256 }
1257 CATCH (except, RETURN_MASK_ALL)
1258 {
1259 GDB_PY_HANDLE_EXCEPTION (except);
1260 }
1261 END_CATCH
1262
1263 return value_to_value_object (val);
1264 }
1265
1266 /* Implements left shift for value objects. */
1267 static PyObject *
1268 valpy_lsh (PyObject *self, PyObject *other)
1269 {
1270 return valpy_binop (VALPY_LSH, self, other);
1271 }
1272
1273 /* Implements right shift for value objects. */
1274 static PyObject *
1275 valpy_rsh (PyObject *self, PyObject *other)
1276 {
1277 return valpy_binop (VALPY_RSH, self, other);
1278 }
1279
1280 /* Implements bitwise and for value objects. */
1281 static PyObject *
1282 valpy_and (PyObject *self, PyObject *other)
1283 {
1284 return valpy_binop (VALPY_BITAND, self, other);
1285 }
1286
1287 /* Implements bitwise or for value objects. */
1288 static PyObject *
1289 valpy_or (PyObject *self, PyObject *other)
1290 {
1291 return valpy_binop (VALPY_BITOR, self, other);
1292 }
1293
1294 /* Implements bitwise xor for value objects. */
1295 static PyObject *
1296 valpy_xor (PyObject *self, PyObject *other)
1297 {
1298 return valpy_binop (VALPY_BITXOR, self, other);
1299 }
1300
1301 /* Implements comparison operations for value objects. Returns NULL on error,
1302 with a python exception set. */
1303 static PyObject *
1304 valpy_richcompare (PyObject *self, PyObject *other, int op)
1305 {
1306 int result = 0;
1307
1308 if (other == Py_None)
1309 /* Comparing with None is special. From what I can tell, in Python
1310 None is smaller than anything else. */
1311 switch (op) {
1312 case Py_LT:
1313 case Py_LE:
1314 case Py_EQ:
1315 Py_RETURN_FALSE;
1316 case Py_NE:
1317 case Py_GT:
1318 case Py_GE:
1319 Py_RETURN_TRUE;
1320 default:
1321 /* Can't happen. */
1322 PyErr_SetString (PyExc_NotImplementedError,
1323 _("Invalid operation on gdb.Value."));
1324 return NULL;
1325 }
1326
1327 TRY
1328 {
1329 struct value *value_other, *mark = value_mark ();
1330 struct cleanup *cleanup;
1331
1332 value_other = convert_value_from_python (other);
1333 if (value_other == NULL)
1334 {
1335 result = -1;
1336 break;
1337 }
1338
1339 cleanup = make_cleanup_value_free_to_mark (mark);
1340
1341 switch (op) {
1342 case Py_LT:
1343 result = value_less (((value_object *) self)->value, value_other);
1344 break;
1345 case Py_LE:
1346 result = value_less (((value_object *) self)->value, value_other)
1347 || value_equal (((value_object *) self)->value, value_other);
1348 break;
1349 case Py_EQ:
1350 result = value_equal (((value_object *) self)->value, value_other);
1351 break;
1352 case Py_NE:
1353 result = !value_equal (((value_object *) self)->value, value_other);
1354 break;
1355 case Py_GT:
1356 result = value_less (value_other, ((value_object *) self)->value);
1357 break;
1358 case Py_GE:
1359 result = value_less (value_other, ((value_object *) self)->value)
1360 || value_equal (((value_object *) self)->value, value_other);
1361 break;
1362 default:
1363 /* Can't happen. */
1364 PyErr_SetString (PyExc_NotImplementedError,
1365 _("Invalid operation on gdb.Value."));
1366 result = -1;
1367 break;
1368 }
1369
1370 do_cleanups (cleanup);
1371 }
1372 CATCH (except, RETURN_MASK_ALL)
1373 {
1374 GDB_PY_HANDLE_EXCEPTION (except);
1375 }
1376 END_CATCH
1377
1378 /* In this case, the Python exception has already been set. */
1379 if (result < 0)
1380 return NULL;
1381
1382 if (result == 1)
1383 Py_RETURN_TRUE;
1384
1385 Py_RETURN_FALSE;
1386 }
1387
1388 #ifndef IS_PY3K
1389 /* Implements conversion to int. */
1390 static PyObject *
1391 valpy_int (PyObject *self)
1392 {
1393 struct value *value = ((value_object *) self)->value;
1394 struct type *type = value_type (value);
1395 LONGEST l = 0;
1396
1397 TRY
1398 {
1399 if (!is_integral_type (type))
1400 error (_("Cannot convert value to int."));
1401
1402 l = value_as_long (value);
1403 }
1404 CATCH (except, RETURN_MASK_ALL)
1405 {
1406 GDB_PY_HANDLE_EXCEPTION (except);
1407 }
1408 END_CATCH
1409
1410 return gdb_py_object_from_longest (l);
1411 }
1412 #endif
1413
1414 /* Implements conversion to long. */
1415 static PyObject *
1416 valpy_long (PyObject *self)
1417 {
1418 struct value *value = ((value_object *) self)->value;
1419 struct type *type = value_type (value);
1420 LONGEST l = 0;
1421
1422 TRY
1423 {
1424 CHECK_TYPEDEF (type);
1425
1426 if (!is_integral_type (type)
1427 && TYPE_CODE (type) != TYPE_CODE_PTR)
1428 error (_("Cannot convert value to long."));
1429
1430 l = value_as_long (value);
1431 }
1432 CATCH (except, RETURN_MASK_ALL)
1433 {
1434 GDB_PY_HANDLE_EXCEPTION (except);
1435 }
1436 END_CATCH
1437
1438 return gdb_py_long_from_longest (l);
1439 }
1440
1441 /* Implements conversion to float. */
1442 static PyObject *
1443 valpy_float (PyObject *self)
1444 {
1445 struct value *value = ((value_object *) self)->value;
1446 struct type *type = value_type (value);
1447 double d = 0;
1448
1449 TRY
1450 {
1451 CHECK_TYPEDEF (type);
1452
1453 if (TYPE_CODE (type) != TYPE_CODE_FLT)
1454 error (_("Cannot convert value to float."));
1455
1456 d = value_as_double (value);
1457 }
1458 CATCH (except, RETURN_MASK_ALL)
1459 {
1460 GDB_PY_HANDLE_EXCEPTION (except);
1461 }
1462 END_CATCH
1463
1464 return PyFloat_FromDouble (d);
1465 }
1466
1467 /* Returns an object for a value which is released from the all_values chain,
1468 so its lifetime is not bound to the execution of a command. */
1469 PyObject *
1470 value_to_value_object (struct value *val)
1471 {
1472 value_object *val_obj;
1473
1474 val_obj = PyObject_New (value_object, &value_object_type);
1475 if (val_obj != NULL)
1476 {
1477 val_obj->value = val;
1478 release_value_or_incref (val);
1479 val_obj->address = NULL;
1480 val_obj->type = NULL;
1481 val_obj->dynamic_type = NULL;
1482 note_value (val_obj);
1483 }
1484
1485 return (PyObject *) val_obj;
1486 }
1487
1488 /* Returns a borrowed reference to the struct value corresponding to
1489 the given value object. */
1490 struct value *
1491 value_object_to_value (PyObject *self)
1492 {
1493 value_object *real;
1494
1495 if (! PyObject_TypeCheck (self, &value_object_type))
1496 return NULL;
1497 real = (value_object *) self;
1498 return real->value;
1499 }
1500
1501 /* Try to convert a Python value to a gdb value. If the value cannot
1502 be converted, set a Python exception and return NULL. Returns a
1503 reference to a new value on the all_values chain. */
1504
1505 struct value *
1506 convert_value_from_python (PyObject *obj)
1507 {
1508 struct value *value = NULL; /* -Wall */
1509 int cmp;
1510
1511 gdb_assert (obj != NULL);
1512
1513 TRY
1514 {
1515 if (PyBool_Check (obj))
1516 {
1517 cmp = PyObject_IsTrue (obj);
1518 if (cmp >= 0)
1519 value = value_from_longest (builtin_type_pybool, cmp);
1520 }
1521 /* Make a long logic check first. In Python 3.x, internally,
1522 all integers are represented as longs. In Python 2.x, there
1523 is still a differentiation internally between a PyInt and a
1524 PyLong. Explicitly do this long check conversion first. In
1525 GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
1526 to be done first to ensure we do not lose information in the
1527 conversion process. */
1528 else if (PyLong_Check (obj))
1529 {
1530 LONGEST l = PyLong_AsLongLong (obj);
1531
1532 if (PyErr_Occurred ())
1533 {
1534 /* If the error was an overflow, we can try converting to
1535 ULONGEST instead. */
1536 if (PyErr_ExceptionMatches (PyExc_OverflowError))
1537 {
1538 PyObject *etype, *evalue, *etraceback, *zero;
1539
1540 PyErr_Fetch (&etype, &evalue, &etraceback);
1541 zero = PyInt_FromLong (0);
1542
1543 /* Check whether obj is positive. */
1544 if (PyObject_RichCompareBool (obj, zero, Py_GT) > 0)
1545 {
1546 ULONGEST ul;
1547
1548 ul = PyLong_AsUnsignedLongLong (obj);
1549 if (! PyErr_Occurred ())
1550 value = value_from_ulongest (builtin_type_upylong, ul);
1551 }
1552 else
1553 /* There's nothing we can do. */
1554 PyErr_Restore (etype, evalue, etraceback);
1555
1556 Py_DECREF (zero);
1557 }
1558 }
1559 else
1560 value = value_from_longest (builtin_type_pylong, l);
1561 }
1562 else if (PyInt_Check (obj))
1563 {
1564 long l = PyInt_AsLong (obj);
1565
1566 if (! PyErr_Occurred ())
1567 value = value_from_longest (builtin_type_pyint, l);
1568 }
1569 else if (PyFloat_Check (obj))
1570 {
1571 double d = PyFloat_AsDouble (obj);
1572
1573 if (! PyErr_Occurred ())
1574 value = value_from_double (builtin_type_pyfloat, d);
1575 }
1576 else if (gdbpy_is_string (obj))
1577 {
1578 char *s;
1579
1580 s = python_string_to_target_string (obj);
1581 if (s != NULL)
1582 {
1583 struct cleanup *old;
1584
1585 old = make_cleanup (xfree, s);
1586 value = value_cstring (s, strlen (s), builtin_type_pychar);
1587 do_cleanups (old);
1588 }
1589 }
1590 else if (PyObject_TypeCheck (obj, &value_object_type))
1591 value = value_copy (((value_object *) obj)->value);
1592 else if (gdbpy_is_lazy_string (obj))
1593 {
1594 PyObject *result;
1595
1596 result = PyObject_CallMethodObjArgs (obj, gdbpy_value_cst, NULL);
1597 value = value_copy (((value_object *) result)->value);
1598 }
1599 else
1600 #ifdef IS_PY3K
1601 PyErr_Format (PyExc_TypeError,
1602 _("Could not convert Python object: %S."), obj);
1603 #else
1604 PyErr_Format (PyExc_TypeError,
1605 _("Could not convert Python object: %s."),
1606 PyString_AsString (PyObject_Str (obj)));
1607 #endif
1608 }
1609 CATCH (except, RETURN_MASK_ALL)
1610 {
1611 PyErr_Format (except.reason == RETURN_QUIT
1612 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
1613 "%s", except.message);
1614 return NULL;
1615 }
1616 END_CATCH
1617
1618 return value;
1619 }
1620
1621 /* Returns value object in the ARGth position in GDB's history. */
1622 PyObject *
1623 gdbpy_history (PyObject *self, PyObject *args)
1624 {
1625 int i;
1626 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
1627
1628 if (!PyArg_ParseTuple (args, "i", &i))
1629 return NULL;
1630
1631 TRY
1632 {
1633 res_val = access_value_history (i);
1634 }
1635 CATCH (except, RETURN_MASK_ALL)
1636 {
1637 GDB_PY_HANDLE_EXCEPTION (except);
1638 }
1639 END_CATCH
1640
1641 return value_to_value_object (res_val);
1642 }
1643
1644 /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
1645
1646 int
1647 gdbpy_is_value_object (PyObject *obj)
1648 {
1649 return PyObject_TypeCheck (obj, &value_object_type);
1650 }
1651
1652 int
1653 gdbpy_initialize_values (void)
1654 {
1655 if (PyType_Ready (&value_object_type) < 0)
1656 return -1;
1657
1658 return gdb_pymodule_addobject (gdb_module, "Value",
1659 (PyObject *) &value_object_type);
1660 }
1661
1662 \f
1663
1664 static PyGetSetDef value_object_getset[] = {
1665 { "address", valpy_get_address, NULL, "The address of the value.",
1666 NULL },
1667 { "is_optimized_out", valpy_get_is_optimized_out, NULL,
1668 "Boolean telling whether the value is optimized "
1669 "out (i.e., not available).",
1670 NULL },
1671 { "type", valpy_get_type, NULL, "Type of the value.", NULL },
1672 { "dynamic_type", valpy_get_dynamic_type, NULL,
1673 "Dynamic type of the value.", NULL },
1674 { "is_lazy", valpy_get_is_lazy, NULL,
1675 "Boolean telling whether the value is lazy (not fetched yet\n\
1676 from the inferior). A lazy value is fetched when needed, or when\n\
1677 the \"fetch_lazy()\" method is called.", NULL },
1678 {NULL} /* Sentinel */
1679 };
1680
1681 static PyMethodDef value_object_methods[] = {
1682 { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." },
1683 { "dynamic_cast", valpy_dynamic_cast, METH_VARARGS,
1684 "dynamic_cast (gdb.Type) -> gdb.Value\n\
1685 Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
1686 },
1687 { "reinterpret_cast", valpy_reinterpret_cast, METH_VARARGS,
1688 "reinterpret_cast (gdb.Type) -> gdb.Value\n\
1689 Cast the value to the supplied type, as if by the C++\n\
1690 reinterpret_cast operator."
1691 },
1692 { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
1693 { "referenced_value", valpy_referenced_value, METH_NOARGS,
1694 "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." },
1695 { "lazy_string", (PyCFunction) valpy_lazy_string,
1696 METH_VARARGS | METH_KEYWORDS,
1697 "lazy_string ([encoding] [, length]) -> lazy_string\n\
1698 Return a lazy string representation of the value." },
1699 { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS,
1700 "string ([encoding] [, errors] [, length]) -> string\n\
1701 Return Unicode string representation of the value." },
1702 { "fetch_lazy", valpy_fetch_lazy, METH_NOARGS,
1703 "Fetches the value from the inferior, if it was lazy." },
1704 {NULL} /* Sentinel */
1705 };
1706
1707 static PyNumberMethods value_object_as_number = {
1708 valpy_add,
1709 valpy_subtract,
1710 valpy_multiply,
1711 #ifndef IS_PY3K
1712 valpy_divide,
1713 #endif
1714 valpy_remainder,
1715 NULL, /* nb_divmod */
1716 valpy_power, /* nb_power */
1717 valpy_negative, /* nb_negative */
1718 valpy_positive, /* nb_positive */
1719 valpy_absolute, /* nb_absolute */
1720 valpy_nonzero, /* nb_nonzero */
1721 valpy_invert, /* nb_invert */
1722 valpy_lsh, /* nb_lshift */
1723 valpy_rsh, /* nb_rshift */
1724 valpy_and, /* nb_and */
1725 valpy_xor, /* nb_xor */
1726 valpy_or, /* nb_or */
1727 #ifdef IS_PY3K
1728 valpy_long, /* nb_int */
1729 NULL, /* reserved */
1730 #else
1731 NULL, /* nb_coerce */
1732 valpy_int, /* nb_int */
1733 valpy_long, /* nb_long */
1734 #endif
1735 valpy_float, /* nb_float */
1736 #ifndef IS_PY3K
1737 NULL, /* nb_oct */
1738 NULL, /* nb_hex */
1739 #endif
1740 NULL, /* nb_inplace_add */
1741 NULL, /* nb_inplace_subtract */
1742 NULL, /* nb_inplace_multiply */
1743 NULL, /* nb_inplace_remainder */
1744 NULL, /* nb_inplace_power */
1745 NULL, /* nb_inplace_lshift */
1746 NULL, /* nb_inplace_rshift */
1747 NULL, /* nb_inplace_and */
1748 NULL, /* nb_inplace_xor */
1749 NULL, /* nb_inplace_or */
1750 NULL, /* nb_floor_divide */
1751 valpy_divide /* nb_true_divide */
1752 };
1753
1754 static PyMappingMethods value_object_as_mapping = {
1755 valpy_length,
1756 valpy_getitem,
1757 valpy_setitem
1758 };
1759
1760 PyTypeObject value_object_type = {
1761 PyVarObject_HEAD_INIT (NULL, 0)
1762 "gdb.Value", /*tp_name*/
1763 sizeof (value_object), /*tp_basicsize*/
1764 0, /*tp_itemsize*/
1765 valpy_dealloc, /*tp_dealloc*/
1766 0, /*tp_print*/
1767 0, /*tp_getattr*/
1768 0, /*tp_setattr*/
1769 0, /*tp_compare*/
1770 0, /*tp_repr*/
1771 &value_object_as_number, /*tp_as_number*/
1772 0, /*tp_as_sequence*/
1773 &value_object_as_mapping, /*tp_as_mapping*/
1774 valpy_hash, /*tp_hash*/
1775 valpy_call, /*tp_call*/
1776 valpy_str, /*tp_str*/
1777 0, /*tp_getattro*/
1778 0, /*tp_setattro*/
1779 0, /*tp_as_buffer*/
1780 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES
1781 | Py_TPFLAGS_BASETYPE, /*tp_flags*/
1782 "GDB value object", /* tp_doc */
1783 0, /* tp_traverse */
1784 0, /* tp_clear */
1785 valpy_richcompare, /* tp_richcompare */
1786 0, /* tp_weaklistoffset */
1787 0, /* tp_iter */
1788 0, /* tp_iternext */
1789 value_object_methods, /* tp_methods */
1790 0, /* tp_members */
1791 value_object_getset, /* tp_getset */
1792 0, /* tp_base */
1793 0, /* tp_dict */
1794 0, /* tp_descr_get */
1795 0, /* tp_descr_set */
1796 0, /* tp_dictoffset */
1797 0, /* tp_init */
1798 0, /* tp_alloc */
1799 valpy_new /* tp_new */
1800 };
This page took 0.096183 seconds and 3 git commands to generate.