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