py-value.c revision 1.1.1.10 1 /* Python interface to values.
2
3 Copyright (C) 2008-2024 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 "top.h"
21 #include "charset.h"
22 #include "value.h"
23 #include "language.h"
24 #include "target-float.h"
25 #include "valprint.h"
26 #include "infcall.h"
27 #include "expression.h"
28 #include "cp-abi.h"
29 #include "python.h"
30 #include "ada-lang.h"
31
32 #include "python-internal.h"
33
34 /* Even though Python scalar types directly map to host types, we use
35 target types here to remain consistent with the values system in
36 GDB (which uses target arithmetic). */
37
38 /* Python's integer type corresponds to C's long type. */
39 #define builtin_type_pyint \
40 builtin_type (gdbpy_enter::get_gdbarch ())->builtin_long
41
42 /* Python's float type corresponds to C's double type. */
43 #define builtin_type_pyfloat \
44 builtin_type (gdbpy_enter::get_gdbarch ())->builtin_double
45
46 /* Python's long type corresponds to C's long long type. */
47 #define builtin_type_pylong \
48 builtin_type (gdbpy_enter::get_gdbarch ())->builtin_long_long
49
50 /* Python's long type corresponds to C's long long type. Unsigned version. */
51 #define builtin_type_upylong builtin_type \
52 (gdbpy_enter::get_gdbarch ())->builtin_unsigned_long_long
53
54 #define builtin_type_pybool \
55 language_bool_type (current_language, gdbpy_enter::get_gdbarch ())
56
57 struct value_object {
58 PyObject_HEAD
59 struct value_object *next;
60 struct value_object *prev;
61 struct value *value;
62 PyObject *address;
63 PyObject *type;
64 PyObject *dynamic_type;
65 PyObject *content_bytes;
66 };
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 /* Clear out an old GDB value stored within SELF, and reset the fields to
76 nullptr. This should be called when a gdb.Value is deallocated, and
77 also if a gdb.Value is reinitialized with a new value. */
78
79 static void
80 valpy_clear_value (value_object *self)
81 {
82 /* Indicate we are no longer interested in the value object. */
83 self->value->decref ();
84 self->value = nullptr;
85
86 Py_CLEAR (self->address);
87 Py_CLEAR (self->type);
88 Py_CLEAR (self->dynamic_type);
89 Py_CLEAR (self->content_bytes);
90 }
91
92 /* Called by the Python interpreter when deallocating a value object. */
93 static void
94 valpy_dealloc (PyObject *obj)
95 {
96 value_object *self = (value_object *) obj;
97
98 /* If SELF failed to initialize correctly then it may not have a value
99 contained within it. */
100 if (self->value != nullptr)
101 {
102 /* Remove SELF from the global list of values. */
103 if (self->prev != nullptr)
104 self->prev->next = self->next;
105 else
106 {
107 gdb_assert (values_in_python == self);
108 values_in_python = self->next;
109 }
110 if (self->next != nullptr)
111 self->next->prev = self->prev;
112
113 /* Release the value object and any cached Python objects. */
114 valpy_clear_value (self);
115 }
116
117 Py_TYPE (self)->tp_free (self);
118 }
119
120 /* Helper to push a gdb.Value object on to the global list of values. If
121 VALUE_OBJ is already on the lit then this does nothing. */
122
123 static void
124 note_value (value_object *value_obj)
125 {
126 if (value_obj->next == nullptr)
127 {
128 gdb_assert (value_obj->prev == nullptr);
129 value_obj->next = values_in_python;
130 if (value_obj->next != nullptr)
131 value_obj->next->prev = value_obj;
132 values_in_python = value_obj;
133 }
134 }
135
136 /* Convert a python object OBJ with type TYPE to a gdb value. The
137 python object in question must conform to the python buffer
138 protocol. On success, return the converted value, otherwise
139 nullptr. When REQUIRE_EXACT_SIZE_P is true the buffer OBJ must be the
140 exact length of TYPE. When REQUIRE_EXACT_SIZE_P is false then the
141 buffer OBJ can be longer than TYPE, in which case only the least
142 significant bytes from the buffer are used. */
143
144 static struct value *
145 convert_buffer_and_type_to_value (PyObject *obj, struct type *type,
146 bool require_exact_size_p)
147 {
148 Py_buffer_up buffer_up;
149 Py_buffer py_buf;
150
151 if (PyObject_CheckBuffer (obj)
152 && PyObject_GetBuffer (obj, &py_buf, PyBUF_SIMPLE) == 0)
153 {
154 /* Got a buffer, py_buf, out of obj. Cause it to be released
155 when it goes out of scope. */
156 buffer_up.reset (&py_buf);
157 }
158 else
159 {
160 PyErr_SetString (PyExc_TypeError,
161 _("Object must support the python buffer protocol."));
162 return nullptr;
163 }
164
165 if (require_exact_size_p && type->length () != py_buf.len)
166 {
167 PyErr_SetString (PyExc_ValueError,
168 _("Size of type is not equal to that of buffer object."));
169 return nullptr;
170 }
171 else if (!require_exact_size_p && type->length () > py_buf.len)
172 {
173 PyErr_SetString (PyExc_ValueError,
174 _("Size of type is larger than that of buffer object."));
175 return nullptr;
176 }
177
178 return value_from_contents (type, (const gdb_byte *) py_buf.buf);
179 }
180
181 /* Implement gdb.Value.__init__. */
182
183 static int
184 valpy_init (PyObject *self, PyObject *args, PyObject *kwds)
185 {
186 static const char *keywords[] = { "val", "type", NULL };
187 PyObject *val_obj = nullptr;
188 PyObject *type_obj = nullptr;
189
190 if (!gdb_PyArg_ParseTupleAndKeywords (args, kwds, "O|O", keywords,
191 &val_obj, &type_obj))
192 return -1;
193
194 struct type *type = nullptr;
195 if (type_obj != nullptr && type_obj != Py_None)
196 {
197 type = type_object_to_type (type_obj);
198 if (type == nullptr)
199 {
200 PyErr_SetString (PyExc_TypeError,
201 _("type argument must be a gdb.Type."));
202 return -1;
203 }
204 }
205
206 struct value *value;
207 if (type == nullptr)
208 value = convert_value_from_python (val_obj);
209 else
210 value = convert_buffer_and_type_to_value (val_obj, type, false);
211 if (value == nullptr)
212 {
213 gdb_assert (PyErr_Occurred ());
214 return -1;
215 }
216
217 /* There might be a previous value here. */
218 value_object *value_obj = (value_object *) self;
219 if (value_obj->value != nullptr)
220 valpy_clear_value (value_obj);
221
222 /* Store the value into this Python object. */
223 value_obj->value = release_value (value).release ();
224
225 /* Ensure that this gdb.Value is in the set of all gdb.Value objects. If
226 we are already in the set then this is call does nothing. */
227 note_value (value_obj);
228
229 return 0;
230 }
231
232 /* Iterate over all the Value objects, calling preserve_one_value on
233 each. */
234 void
235 gdbpy_preserve_values (const struct extension_language_defn *extlang,
236 struct objfile *objfile,
237 copied_types_hash_t &copied_types)
238 {
239 value_object *iter;
240
241 for (iter = values_in_python; iter; iter = iter->next)
242 iter->value->preserve (objfile, copied_types);
243 }
244
245 /* Given a value of a pointer type, apply the C unary * operator to it. */
246 static PyObject *
247 valpy_dereference (PyObject *self, PyObject *args)
248 {
249 PyObject *result = NULL;
250
251 try
252 {
253 struct value *res_val;
254 scoped_value_mark free_values;
255
256 res_val = value_ind (((value_object *) self)->value);
257 result = value_to_value_object (res_val);
258 }
259 catch (const gdb_exception &except)
260 {
261 return gdbpy_handle_gdb_exception (nullptr, except);
262 }
263
264 return result;
265 }
266
267 /* Given a value of a pointer type or a reference type, return the value
268 referenced. The difference between this function and valpy_dereference is
269 that the latter applies * unary operator to a value, which need not always
270 result in the value referenced. For example, for a value which is a reference
271 to an 'int' pointer ('int *'), valpy_dereference will result in a value of
272 type 'int' while valpy_referenced_value will result in a value of type
273 'int *'. */
274
275 static PyObject *
276 valpy_referenced_value (PyObject *self, PyObject *args)
277 {
278 PyObject *result = NULL;
279
280 try
281 {
282 struct value *self_val, *res_val;
283 scoped_value_mark free_values;
284
285 self_val = ((value_object *) self)->value;
286 switch (check_typedef (self_val->type ())->code ())
287 {
288 case TYPE_CODE_PTR:
289 res_val = value_ind (self_val);
290 break;
291 case TYPE_CODE_REF:
292 case TYPE_CODE_RVALUE_REF:
293 res_val = coerce_ref (self_val);
294 break;
295 default:
296 error(_("Trying to get the referenced value from a value which is "
297 "neither a pointer nor a reference."));
298 }
299
300 result = value_to_value_object (res_val);
301 }
302 catch (const gdb_exception &except)
303 {
304 return gdbpy_handle_gdb_exception (nullptr, except);
305 }
306
307 return result;
308 }
309
310 /* Return a value which is a reference to the value. */
311
312 static PyObject *
313 valpy_reference_value (PyObject *self, PyObject *args, enum type_code refcode)
314 {
315 PyObject *result = NULL;
316
317 try
318 {
319 struct value *self_val;
320 scoped_value_mark free_values;
321
322 self_val = ((value_object *) self)->value;
323 result = value_to_value_object (value_ref (self_val, refcode));
324 }
325 catch (const gdb_exception &except)
326 {
327 return gdbpy_handle_gdb_exception (nullptr, except);
328 }
329
330 return result;
331 }
332
333 static PyObject *
334 valpy_lvalue_reference_value (PyObject *self, PyObject *args)
335 {
336 return valpy_reference_value (self, args, TYPE_CODE_REF);
337 }
338
339 static PyObject *
340 valpy_rvalue_reference_value (PyObject *self, PyObject *args)
341 {
342 return valpy_reference_value (self, args, TYPE_CODE_RVALUE_REF);
343 }
344
345 /* Implement Value.to_array. */
346
347 static PyObject *
348 valpy_to_array (PyObject *self, PyObject *args)
349 {
350 PyObject *result = nullptr;
351
352 try
353 {
354 struct value *val = ((value_object *) self)->value;
355 struct type *type = check_typedef (val->type ());
356
357 if (type->code () == TYPE_CODE_ARRAY)
358 {
359 result = self;
360 Py_INCREF (result);
361 }
362 else
363 {
364 val = value_to_array (val);
365 if (val == nullptr)
366 PyErr_SetString (PyExc_TypeError, _("Value is not array-like."));
367 else
368 result = value_to_value_object (val);
369 }
370 }
371 catch (const gdb_exception &except)
372 {
373 return gdbpy_handle_gdb_exception (nullptr, except);
374 }
375
376 return result;
377 }
378
379 /* Return a "const" qualified version of the value. */
380
381 static PyObject *
382 valpy_const_value (PyObject *self, PyObject *args)
383 {
384 PyObject *result = NULL;
385
386 try
387 {
388 struct value *self_val, *res_val;
389 scoped_value_mark free_values;
390
391 self_val = ((value_object *) self)->value;
392 res_val = make_cv_value (1, 0, self_val);
393 result = value_to_value_object (res_val);
394 }
395 catch (const gdb_exception &except)
396 {
397 return gdbpy_handle_gdb_exception (nullptr, except);
398 }
399
400 return result;
401 }
402
403 /* Return "&value". */
404 static PyObject *
405 valpy_get_address (PyObject *self, void *closure)
406 {
407 value_object *val_obj = (value_object *) self;
408
409 if (!val_obj->address)
410 {
411 try
412 {
413 struct value *res_val;
414 scoped_value_mark free_values;
415
416 res_val = value_addr (val_obj->value);
417 val_obj->address = value_to_value_object (res_val);
418 }
419 catch (const gdb_exception_forced_quit &except)
420 {
421 quit_force (NULL, 0);
422 }
423 catch (const gdb_exception &except)
424 {
425 val_obj->address = Py_None;
426 Py_INCREF (Py_None);
427 }
428 }
429
430 Py_XINCREF (val_obj->address);
431
432 return val_obj->address;
433 }
434
435 /* Return type of the value. */
436 static PyObject *
437 valpy_get_type (PyObject *self, void *closure)
438 {
439 value_object *obj = (value_object *) self;
440
441 if (!obj->type)
442 {
443 obj->type = type_to_type_object (obj->value->type ());
444 if (!obj->type)
445 return NULL;
446 }
447 Py_INCREF (obj->type);
448 return obj->type;
449 }
450
451 /* Return dynamic type of the value. */
452
453 static PyObject *
454 valpy_get_dynamic_type (PyObject *self, void *closure)
455 {
456 value_object *obj = (value_object *) self;
457 struct type *type = NULL;
458
459 if (obj->dynamic_type != NULL)
460 {
461 Py_INCREF (obj->dynamic_type);
462 return obj->dynamic_type;
463 }
464
465 try
466 {
467 struct value *val = obj->value;
468 scoped_value_mark free_values;
469
470 type = val->type ();
471 type = check_typedef (type);
472
473 if (type->is_pointer_or_reference ()
474 && (type->target_type ()->code () == TYPE_CODE_STRUCT))
475 {
476 struct value *target;
477 int was_pointer = type->code () == TYPE_CODE_PTR;
478
479 if (was_pointer)
480 target = value_ind (val);
481 else
482 target = coerce_ref (val);
483 type = value_rtti_type (target, NULL, NULL, NULL);
484
485 if (type)
486 {
487 if (was_pointer)
488 type = lookup_pointer_type (type);
489 else
490 type = lookup_lvalue_reference_type (type);
491 }
492 }
493 else if (type->code () == TYPE_CODE_STRUCT)
494 type = value_rtti_type (val, NULL, NULL, NULL);
495 else
496 {
497 /* Re-use object's static type. */
498 type = NULL;
499 }
500 }
501 catch (const gdb_exception &except)
502 {
503 return gdbpy_handle_gdb_exception (nullptr, except);
504 }
505
506 if (type == NULL)
507 obj->dynamic_type = valpy_get_type (self, NULL);
508 else
509 obj->dynamic_type = type_to_type_object (type);
510
511 Py_XINCREF (obj->dynamic_type);
512 return obj->dynamic_type;
513 }
514
515 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
516 string. Return a PyObject representing a lazy_string_object type.
517 A lazy string is a pointer to a string with an optional encoding and
518 length. If ENCODING is not given, encoding is set to None. If an
519 ENCODING is provided the encoding parameter is set to ENCODING, but
520 the string is not encoded.
521 If LENGTH is provided then the length parameter is set to LENGTH.
522 Otherwise if the value is an array of known length then the array's length
523 is used. Otherwise the length will be set to -1 (meaning first null of
524 appropriate with). */
525
526 static PyObject *
527 valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw)
528 {
529 gdb_py_longest length = -1;
530 struct value *value = ((value_object *) self)->value;
531 const char *user_encoding = NULL;
532 static const char *keywords[] = { "encoding", "length", NULL };
533 PyObject *str_obj = NULL;
534
535 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG,
536 keywords, &user_encoding, &length))
537 return NULL;
538
539 if (length < -1)
540 {
541 PyErr_SetString (PyExc_ValueError, _("Invalid length."));
542 return NULL;
543 }
544
545 try
546 {
547 scoped_value_mark free_values;
548 struct type *type, *realtype;
549 CORE_ADDR addr;
550
551 type = value->type ();
552 realtype = check_typedef (type);
553
554 switch (realtype->code ())
555 {
556 case TYPE_CODE_ARRAY:
557 {
558 LONGEST array_length = -1;
559 LONGEST low_bound, high_bound;
560
561 /* PR 20786: There's no way to specify an array of length zero.
562 Record a length of [0,-1] which is how Ada does it. Anything
563 we do is broken, but this one possible solution. */
564 if (get_array_bounds (realtype, &low_bound, &high_bound))
565 array_length = high_bound - low_bound + 1;
566 if (length == -1)
567 length = array_length;
568 else if (array_length == -1)
569 {
570 type = lookup_array_range_type (realtype->target_type (),
571 0, length - 1);
572 }
573 else if (length != array_length)
574 {
575 /* We need to create a new array type with the
576 specified length. */
577 if (length > array_length)
578 error (_("Length is larger than array size."));
579 type = lookup_array_range_type (realtype->target_type (),
580 low_bound,
581 low_bound + length - 1);
582 }
583 addr = value->address ();
584 break;
585 }
586 case TYPE_CODE_PTR:
587 /* If a length is specified we defer creating an array of the
588 specified width until we need to. */
589 addr = value_as_address (value);
590 break;
591 default:
592 error (_("Cannot make lazy string from this object"));
593 }
594
595 str_obj = gdbpy_create_lazy_string_object (addr, length, user_encoding,
596 type);
597 }
598 catch (const gdb_exception &except)
599 {
600 return gdbpy_handle_gdb_exception (nullptr, except);
601 }
602
603 return str_obj;
604 }
605
606 /* Implementation of gdb.Value.string ([encoding] [, errors]
607 [, length]) -> string. Return Unicode string with value contents.
608 If ENCODING is not given, the string is assumed to be encoded in
609 the target's charset. If LENGTH is provided, only fetch string to
610 the length provided. */
611
612 static PyObject *
613 valpy_string (PyObject *self, PyObject *args, PyObject *kw)
614 {
615 int length = -1;
616 gdb::unique_xmalloc_ptr<gdb_byte> buffer;
617 struct value *value = ((value_object *) self)->value;
618 const char *encoding = NULL;
619 const char *errors = NULL;
620 const char *user_encoding = NULL;
621 const char *la_encoding = NULL;
622 struct type *char_type;
623 static const char *keywords[] = { "encoding", "errors", "length", NULL };
624
625 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords,
626 &user_encoding, &errors, &length))
627 return NULL;
628
629 try
630 {
631 c_get_string (value, &buffer, &length, &char_type, &la_encoding);
632 }
633 catch (const gdb_exception &except)
634 {
635 return gdbpy_handle_gdb_exception (nullptr, except);
636 }
637
638 encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding;
639 return PyUnicode_Decode ((const char *) buffer.get (),
640 length * char_type->length (),
641 encoding, errors);
642 }
643
644 /* Given a Python object, copy its truth value to a C bool (the value
645 pointed by dest).
646 If src_obj is NULL, then *dest is not modified.
647
648 Return true in case of success (including src_obj being NULL), false
649 in case of error. */
650
651 static bool
652 copy_py_bool_obj (bool *dest, PyObject *src_obj)
653 {
654 if (src_obj)
655 {
656 int cmp = PyObject_IsTrue (src_obj);
657 if (cmp < 0)
658 return false;
659 *dest = cmp;
660 }
661
662 return true;
663 }
664
665 /* Implementation of gdb.Value.format_string (...) -> string.
666 Return Unicode string with value contents formatted using the
667 keyword-only arguments. */
668
669 static PyObject *
670 valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
671 {
672 static const char *keywords[] =
673 {
674 /* Basic C/C++ options. */
675 "raw", /* See the /r option to print. */
676 "pretty_arrays", /* See set print array on|off. */
677 "pretty_structs", /* See set print pretty on|off. */
678 "array_indexes", /* See set print array-indexes on|off. */
679 "symbols", /* See set print symbol on|off. */
680 "unions", /* See set print union on|off. */
681 "address", /* See set print address on|off. */
682 "styling", /* Should we apply styling. */
683 "nibbles", /* See set print nibbles on|off. */
684 "summary", /* Summary mode for non-scalars. */
685 /* C++ options. */
686 "deref_refs", /* No corresponding setting. */
687 "actual_objects", /* See set print object on|off. */
688 "static_members", /* See set print static-members on|off. */
689 /* C non-bool options. */
690 "max_characters", /* See set print characters N. */
691 "max_elements", /* See set print elements N. */
692 "max_depth", /* See set print max-depth N. */
693 "repeat_threshold", /* See set print repeats. */
694 "format", /* The format passed to the print command. */
695 NULL
696 };
697
698 /* This function has too many arguments to be useful as positionals, so
699 the user should specify them all as keyword arguments.
700 Python 3.3 and later have a way to specify it (both in C and Python
701 itself), but we could be compiled with older versions, so we just
702 check that the args tuple is empty. */
703 Py_ssize_t positional_count = PyObject_Length (args);
704 if (positional_count < 0)
705 return NULL;
706 else if (positional_count > 0)
707 {
708 /* This matches the error message that Python 3.3 raises when
709 passing positionals to functions expecting keyword-only
710 arguments. */
711 PyErr_Format (PyExc_TypeError,
712 "format_string() takes 0 positional arguments but %zu were given",
713 positional_count);
714 return NULL;
715 }
716
717 struct value_print_options opts;
718 gdbpy_get_print_options (&opts);
719 opts.deref_ref = false;
720
721 /* We need objects for booleans as the "p" flag for bools is new in
722 Python 3.3. */
723 PyObject *raw_obj = NULL;
724 PyObject *pretty_arrays_obj = NULL;
725 PyObject *pretty_structs_obj = NULL;
726 PyObject *array_indexes_obj = NULL;
727 PyObject *symbols_obj = NULL;
728 PyObject *unions_obj = NULL;
729 PyObject *address_obj = NULL;
730 PyObject *styling_obj = Py_False;
731 PyObject *nibbles_obj = NULL;
732 PyObject *deref_refs_obj = NULL;
733 PyObject *actual_objects_obj = NULL;
734 PyObject *static_members_obj = NULL;
735 PyObject *summary_obj = NULL;
736 char *format = NULL;
737 if (!gdb_PyArg_ParseTupleAndKeywords (args,
738 kw,
739 "|O!O!O!O!O!O!O!O!O!O!O!O!O!IIIIs",
740 keywords,
741 &PyBool_Type, &raw_obj,
742 &PyBool_Type, &pretty_arrays_obj,
743 &PyBool_Type, &pretty_structs_obj,
744 &PyBool_Type, &array_indexes_obj,
745 &PyBool_Type, &symbols_obj,
746 &PyBool_Type, &unions_obj,
747 &PyBool_Type, &address_obj,
748 &PyBool_Type, &styling_obj,
749 &PyBool_Type, &nibbles_obj,
750 &PyBool_Type, &summary_obj,
751 &PyBool_Type, &deref_refs_obj,
752 &PyBool_Type, &actual_objects_obj,
753 &PyBool_Type, &static_members_obj,
754 &opts.print_max_chars,
755 &opts.print_max,
756 &opts.max_depth,
757 &opts.repeat_count_threshold,
758 &format))
759 return NULL;
760
761 /* Set boolean arguments. */
762 if (!copy_py_bool_obj (&opts.raw, raw_obj))
763 return NULL;
764 if (!copy_py_bool_obj (&opts.prettyformat_arrays, pretty_arrays_obj))
765 return NULL;
766 if (!copy_py_bool_obj (&opts.prettyformat_structs, pretty_structs_obj))
767 return NULL;
768 if (!copy_py_bool_obj (&opts.print_array_indexes, array_indexes_obj))
769 return NULL;
770 if (!copy_py_bool_obj (&opts.symbol_print, symbols_obj))
771 return NULL;
772 if (!copy_py_bool_obj (&opts.unionprint, unions_obj))
773 return NULL;
774 if (!copy_py_bool_obj (&opts.addressprint, address_obj))
775 return NULL;
776 if (!copy_py_bool_obj (&opts.nibblesprint, nibbles_obj))
777 return NULL;
778 if (!copy_py_bool_obj (&opts.deref_ref, deref_refs_obj))
779 return NULL;
780 if (!copy_py_bool_obj (&opts.objectprint, actual_objects_obj))
781 return NULL;
782 if (!copy_py_bool_obj (&opts.static_field_print, static_members_obj))
783 return NULL;
784 if (!copy_py_bool_obj (&opts.summary, summary_obj))
785 return nullptr;
786
787 /* Numeric arguments for which 0 means unlimited (which we represent as
788 UINT_MAX). Note that the max-depth numeric argument uses -1 as
789 unlimited, and 0 is a valid choice. */
790 if (opts.print_max == 0)
791 opts.print_max = UINT_MAX;
792 if (opts.repeat_count_threshold == 0)
793 opts.repeat_count_threshold = UINT_MAX;
794
795 /* Other arguments. */
796 if (format != NULL)
797 {
798 if (strlen (format) == 1)
799 opts.format = format[0];
800 else
801 {
802 /* Mimic the message on standard Python ones for similar
803 errors. */
804 PyErr_SetString (PyExc_ValueError,
805 "a single character is required");
806 return NULL;
807 }
808 }
809
810 /* We force styling_obj to be a 'bool' when we parse the args above. */
811 gdb_assert (PyBool_Check (styling_obj));
812 string_file stb (styling_obj == Py_True);
813
814 try
815 {
816 common_val_print (((value_object *) self)->value, &stb, 0,
817 &opts, current_language);
818 }
819 catch (const gdb_exception &except)
820 {
821 return gdbpy_handle_gdb_exception (nullptr, except);
822 }
823
824 return PyUnicode_Decode (stb.c_str (), stb.size (), host_charset (), NULL);
825 }
826
827 /* A helper function that implements the various cast operators. */
828
829 static PyObject *
830 valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op)
831 {
832 PyObject *type_obj, *result = NULL;
833 struct type *type;
834
835 if (! PyArg_ParseTuple (args, "O", &type_obj))
836 return NULL;
837
838 type = type_object_to_type (type_obj);
839 if (! type)
840 {
841 PyErr_SetString (PyExc_RuntimeError,
842 _("Argument must be a type."));
843 return NULL;
844 }
845
846 try
847 {
848 struct value *val = ((value_object *) self)->value;
849 struct value *res_val;
850 scoped_value_mark free_values;
851
852 if (op == UNOP_DYNAMIC_CAST)
853 res_val = value_dynamic_cast (type, val);
854 else if (op == UNOP_REINTERPRET_CAST)
855 res_val = value_reinterpret_cast (type, val);
856 else
857 {
858 gdb_assert (op == UNOP_CAST);
859 res_val = value_cast (type, val);
860 }
861
862 result = value_to_value_object (res_val);
863 }
864 catch (const gdb_exception &except)
865 {
866 return gdbpy_handle_gdb_exception (nullptr, except);
867 }
868
869 return result;
870 }
871
872 /* Implementation of the "cast" method. */
873
874 static PyObject *
875 valpy_cast (PyObject *self, PyObject *args)
876 {
877 return valpy_do_cast (self, args, UNOP_CAST);
878 }
879
880 /* Implementation of the "dynamic_cast" method. */
881
882 static PyObject *
883 valpy_dynamic_cast (PyObject *self, PyObject *args)
884 {
885 return valpy_do_cast (self, args, UNOP_DYNAMIC_CAST);
886 }
887
888 /* Implementation of the "reinterpret_cast" method. */
889
890 static PyObject *
891 valpy_reinterpret_cast (PyObject *self, PyObject *args)
892 {
893 return valpy_do_cast (self, args, UNOP_REINTERPRET_CAST);
894 }
895
896 /* Assign NEW_VALUE into SELF, handles 'struct value' reference counting,
897 and also clearing the bytes data cached within SELF. Return true if
898 the assignment was successful, otherwise return false, in which case a
899 Python exception will be set. */
900
901 static bool
902 valpy_assign_core (value_object *self, struct value *new_value)
903 {
904 try
905 {
906 new_value = value_assign (self->value, new_value);
907
908 /* value_as_address returns a new value with the same location
909 as the old one. Ensure that this gdb.Value is updated to
910 reflect the new value. */
911 new_value->incref ();
912 self->value->decref ();
913 Py_CLEAR (self->content_bytes);
914 self->value = new_value;
915 }
916 catch (const gdb_exception &except)
917 {
918 return gdbpy_handle_gdb_exception (false, except);
919 }
920
921 return true;
922 }
923
924 /* Implementation of the "assign" method. */
925
926 static PyObject *
927 valpy_assign (PyObject *self_obj, PyObject *args)
928 {
929 PyObject *val_obj;
930
931 if (! PyArg_ParseTuple (args, "O", &val_obj))
932 return nullptr;
933
934 struct value *val = convert_value_from_python (val_obj);
935 if (val == nullptr)
936 return nullptr;
937
938 value_object *self = (value_object *) self_obj;
939 if (!valpy_assign_core (self, val))
940 return nullptr;
941
942 Py_RETURN_NONE;
943 }
944
945 static Py_ssize_t
946 valpy_length (PyObject *self)
947 {
948 /* We don't support getting the number of elements in a struct / class. */
949 PyErr_SetString (PyExc_NotImplementedError,
950 _("Invalid operation on gdb.Value."));
951 return -1;
952 }
953
954 /* Return 1 if the gdb.Field object FIELD is present in the value V.
955 Returns 0 otherwise. If any Python error occurs, -1 is returned. */
956
957 static int
958 value_has_field (struct value *v, PyObject *field)
959 {
960 struct type *parent_type, *val_type;
961 enum type_code type_code;
962 gdbpy_ref<> type_object (PyObject_GetAttrString (field, "parent_type"));
963 int has_field = 0;
964
965 if (type_object == NULL)
966 return -1;
967
968 parent_type = type_object_to_type (type_object.get ());
969 if (parent_type == NULL)
970 {
971 PyErr_SetString (PyExc_TypeError,
972 _("'parent_type' attribute of gdb.Field object is not a"
973 "gdb.Type object."));
974 return -1;
975 }
976
977 try
978 {
979 val_type = v->type ();
980 val_type = check_typedef (val_type);
981 if (val_type->is_pointer_or_reference ())
982 val_type = check_typedef (val_type->target_type ());
983
984 type_code = val_type->code ();
985 if ((type_code == TYPE_CODE_STRUCT || type_code == TYPE_CODE_UNION)
986 && types_equal (val_type, parent_type))
987 has_field = 1;
988 else
989 has_field = 0;
990 }
991 catch (const gdb_exception &except)
992 {
993 return gdbpy_handle_gdb_exception (-1, except);
994 }
995
996 return has_field;
997 }
998
999 /* Return the value of a flag FLAG_NAME in a gdb.Field object FIELD.
1000 Returns 1 if the flag value is true, 0 if it is false, and -1 if
1001 a Python error occurs. */
1002
1003 static int
1004 get_field_flag (PyObject *field, const char *flag_name)
1005 {
1006 gdbpy_ref<> flag_object (PyObject_GetAttrString (field, flag_name));
1007
1008 if (flag_object == NULL)
1009 return -1;
1010
1011 return PyObject_IsTrue (flag_object.get ());
1012 }
1013
1014 /* Return the "type" attribute of a gdb.Field object.
1015 Returns NULL on error, with a Python exception set. */
1016
1017 static struct type *
1018 get_field_type (PyObject *field)
1019 {
1020 gdbpy_ref<> ftype_obj (PyObject_GetAttrString (field, "type"));
1021 struct type *ftype;
1022
1023 if (ftype_obj == NULL)
1024 return NULL;
1025 ftype = type_object_to_type (ftype_obj.get ());
1026 if (ftype == NULL)
1027 PyErr_SetString (PyExc_TypeError,
1028 _("'type' attribute of gdb.Field object is not a "
1029 "gdb.Type object."));
1030
1031 return ftype;
1032 }
1033
1034 /* Given string name or a gdb.Field object corresponding to an element inside
1035 a structure, return its value object. Returns NULL on error, with a python
1036 exception set. */
1037
1038 static PyObject *
1039 valpy_getitem (PyObject *self, PyObject *key)
1040 {
1041 value_object *self_value = (value_object *) self;
1042 gdb::unique_xmalloc_ptr<char> field;
1043 struct type *base_class_type = NULL, *field_type = NULL;
1044 long bitpos = -1;
1045 PyObject *result = NULL;
1046
1047 if (gdbpy_is_string (key))
1048 {
1049 field = python_string_to_host_string (key);
1050 if (field == NULL)
1051 return NULL;
1052 }
1053 else if (gdbpy_is_field (key))
1054 {
1055 int is_base_class, valid_field;
1056
1057 valid_field = value_has_field (self_value->value, key);
1058 if (valid_field < 0)
1059 return NULL;
1060 else if (valid_field == 0)
1061 {
1062 PyErr_SetString (PyExc_TypeError,
1063 _("Invalid lookup for a field not contained in "
1064 "the value."));
1065
1066 return NULL;
1067 }
1068
1069 is_base_class = get_field_flag (key, "is_base_class");
1070 if (is_base_class < 0)
1071 return NULL;
1072 else if (is_base_class > 0)
1073 {
1074 base_class_type = get_field_type (key);
1075 if (base_class_type == NULL)
1076 return NULL;
1077 }
1078 else
1079 {
1080 gdbpy_ref<> name_obj (PyObject_GetAttrString (key, "name"));
1081
1082 if (name_obj == NULL)
1083 return NULL;
1084
1085 if (name_obj != Py_None)
1086 {
1087 field = python_string_to_host_string (name_obj.get ());
1088 if (field == NULL)
1089 return NULL;
1090 }
1091 else
1092 {
1093 if (!PyObject_HasAttrString (key, "bitpos"))
1094 {
1095 PyErr_SetString (PyExc_AttributeError,
1096 _("gdb.Field object has no name and no "
1097 "'bitpos' attribute."));
1098
1099 return NULL;
1100 }
1101 gdbpy_ref<> bitpos_obj (PyObject_GetAttrString (key, "bitpos"));
1102 if (bitpos_obj == NULL)
1103 return NULL;
1104 if (!gdb_py_int_as_long (bitpos_obj.get (), &bitpos))
1105 return NULL;
1106
1107 field_type = get_field_type (key);
1108 if (field_type == NULL)
1109 return NULL;
1110 }
1111 }
1112 }
1113
1114 try
1115 {
1116 struct value *tmp = self_value->value;
1117 struct value *res_val = NULL;
1118 scoped_value_mark free_values;
1119
1120 if (field)
1121 res_val = value_struct_elt (&tmp, {}, field.get (), NULL,
1122 "struct/class/union");
1123 else if (bitpos >= 0)
1124 res_val = value_struct_elt_bitpos (&tmp, bitpos, field_type,
1125 "struct/class/union");
1126 else if (base_class_type != NULL)
1127 {
1128 struct type *val_type;
1129
1130 val_type = check_typedef (tmp->type ());
1131 if (val_type->code () == TYPE_CODE_PTR)
1132 res_val = value_cast (lookup_pointer_type (base_class_type), tmp);
1133 else if (val_type->code () == TYPE_CODE_REF)
1134 res_val = value_cast (lookup_lvalue_reference_type (base_class_type),
1135 tmp);
1136 else if (val_type->code () == TYPE_CODE_RVALUE_REF)
1137 res_val = value_cast (lookup_rvalue_reference_type (base_class_type),
1138 tmp);
1139 else
1140 res_val = value_cast (base_class_type, tmp);
1141 }
1142 else
1143 {
1144 /* Assume we are attempting an array access, and let the
1145 value code throw an exception if the index has an invalid
1146 type. */
1147 struct value *idx = convert_value_from_python (key);
1148
1149 if (idx != NULL
1150 && binop_user_defined_p (BINOP_SUBSCRIPT, tmp, idx))
1151 res_val = value_x_binop (tmp, idx, BINOP_SUBSCRIPT,
1152 OP_NULL, EVAL_NORMAL);
1153 else if (idx != NULL)
1154 {
1155 /* Check the value's type is something that can be accessed via
1156 a subscript. */
1157 struct type *type;
1158
1159 tmp = coerce_ref (tmp);
1160 type = check_typedef (tmp->type ());
1161 if (type->code () != TYPE_CODE_ARRAY
1162 && type->code () != TYPE_CODE_PTR)
1163 error (_("Cannot subscript requested type."));
1164 else if (ADA_TYPE_P (type))
1165 res_val = ada_value_subscript (tmp, 1, &idx);
1166 else
1167 res_val = value_subscript (tmp, value_as_long (idx));
1168 }
1169 }
1170
1171 if (res_val)
1172 result = value_to_value_object (res_val);
1173 }
1174 catch (const gdb_exception &ex)
1175 {
1176 return gdbpy_handle_gdb_exception (nullptr, ex);
1177 }
1178
1179 return result;
1180 }
1181
1182 static int
1183 valpy_setitem (PyObject *self, PyObject *key, PyObject *value)
1184 {
1185 PyErr_Format (PyExc_NotImplementedError,
1186 _("Setting of struct elements is not currently supported."));
1187 return -1;
1188 }
1189
1190 /* Called by the Python interpreter to perform an inferior function
1191 call on the value. Returns NULL on error, with a python exception set. */
1192 static PyObject *
1193 valpy_call (PyObject *self, PyObject *args, PyObject *keywords)
1194 {
1195 Py_ssize_t args_count;
1196 struct value *function = ((value_object *) self)->value;
1197 struct value **vargs = NULL;
1198 struct type *ftype = NULL;
1199 PyObject *result = NULL;
1200
1201 try
1202 {
1203 ftype = check_typedef (function->type ());
1204 }
1205 catch (const gdb_exception &except)
1206 {
1207 return gdbpy_handle_gdb_exception (nullptr, except);
1208 }
1209
1210 if (ftype->code () != TYPE_CODE_FUNC && ftype->code () != TYPE_CODE_METHOD
1211 && ftype->code () != TYPE_CODE_INTERNAL_FUNCTION)
1212 {
1213 PyErr_SetString (PyExc_RuntimeError,
1214 _("Value is not callable (not TYPE_CODE_FUNC"
1215 " or TYPE_CODE_METHOD"
1216 " or TYPE_CODE_INTERNAL_FUNCTION)."));
1217 return NULL;
1218 }
1219
1220 if (! PyTuple_Check (args))
1221 {
1222 PyErr_SetString (PyExc_TypeError,
1223 _("Inferior arguments must be provided in a tuple."));
1224 return NULL;
1225 }
1226
1227 args_count = PyTuple_Size (args);
1228 if (args_count > 0)
1229 {
1230 int i;
1231
1232 vargs = XALLOCAVEC (struct value *, args_count);
1233 for (i = 0; i < args_count; i++)
1234 {
1235 PyObject *item = PyTuple_GetItem (args, i);
1236
1237 if (item == NULL)
1238 return NULL;
1239
1240 vargs[i] = convert_value_from_python (item);
1241 if (vargs[i] == NULL)
1242 return NULL;
1243 }
1244 }
1245
1246 try
1247 {
1248 scoped_value_mark free_values;
1249
1250 value *return_value;
1251 if (ftype->code () == TYPE_CODE_INTERNAL_FUNCTION)
1252 return_value = call_internal_function (gdbpy_enter::get_gdbarch (),
1253 current_language,
1254 function, args_count, vargs,
1255 EVAL_NORMAL);
1256 else
1257 return_value
1258 = call_function_by_hand (function, NULL,
1259 gdb::make_array_view (vargs, args_count));
1260 result = value_to_value_object (return_value);
1261 }
1262 catch (const gdb_exception &except)
1263 {
1264 return gdbpy_handle_gdb_exception (nullptr, except);
1265 }
1266
1267 return result;
1268 }
1269
1270 /* Called by the Python interpreter to obtain string representation
1271 of the object. */
1272 static PyObject *
1273 valpy_str (PyObject *self)
1274 {
1275 struct value_print_options opts;
1276
1277 gdbpy_get_print_options (&opts);
1278 opts.deref_ref = false;
1279
1280 string_file stb;
1281
1282 try
1283 {
1284 common_val_print (((value_object *) self)->value, &stb, 0,
1285 &opts, current_language);
1286 }
1287 catch (const gdb_exception &except)
1288 {
1289 return gdbpy_handle_gdb_exception (nullptr, except);
1290 }
1291
1292 return PyUnicode_Decode (stb.c_str (), stb.size (), host_charset (), NULL);
1293 }
1294
1295 /* Implements gdb.Value.is_optimized_out. */
1296 static PyObject *
1297 valpy_get_is_optimized_out (PyObject *self, void *closure)
1298 {
1299 struct value *value = ((value_object *) self)->value;
1300 int opt = 0;
1301
1302 try
1303 {
1304 opt = value->optimized_out ();
1305 }
1306 catch (const gdb_exception &except)
1307 {
1308 return gdbpy_handle_gdb_exception (nullptr, except);
1309 }
1310
1311 if (opt)
1312 Py_RETURN_TRUE;
1313
1314 Py_RETURN_FALSE;
1315 }
1316
1317 /* Implements gdb.Value.is_lazy. */
1318 static PyObject *
1319 valpy_get_is_lazy (PyObject *self, void *closure)
1320 {
1321 struct value *value = ((value_object *) self)->value;
1322 int opt = 0;
1323
1324 try
1325 {
1326 opt = value->lazy ();
1327 }
1328 catch (const gdb_exception &except)
1329 {
1330 return gdbpy_handle_gdb_exception (nullptr, except);
1331 }
1332
1333 if (opt)
1334 Py_RETURN_TRUE;
1335
1336 Py_RETURN_FALSE;
1337 }
1338
1339 /* Get gdb.Value.bytes attribute. */
1340
1341 static PyObject *
1342 valpy_get_bytes (PyObject *self, void *closure)
1343 {
1344 value_object *value_obj = (value_object *) self;
1345 struct value *value = value_obj->value;
1346
1347 if (value_obj->content_bytes != nullptr)
1348 {
1349 Py_INCREF (value_obj->content_bytes);
1350 return value_obj->content_bytes;
1351 }
1352
1353 gdb::array_view<const gdb_byte> contents;
1354 try
1355 {
1356 contents = value->contents ();
1357 }
1358 catch (const gdb_exception &except)
1359 {
1360 return gdbpy_handle_gdb_exception (nullptr, except);
1361 }
1362
1363 value_obj->content_bytes
1364 = PyBytes_FromStringAndSize ((const char *) contents.data (),
1365 contents.size ());
1366 Py_XINCREF (value_obj->content_bytes);
1367 return value_obj->content_bytes;
1368 }
1369
1370 /* Set gdb.Value.bytes attribute. */
1371
1372 static int
1373 valpy_set_bytes (PyObject *self_obj, PyObject *new_value_obj, void *closure)
1374 {
1375 value_object *self = (value_object *) self_obj;
1376
1377 /* Create a new value from the buffer NEW_VALUE_OBJ. We pass true here
1378 to indicate that NEW_VALUE_OBJ must match exactly the type length. */
1379 struct value *new_value
1380 = convert_buffer_and_type_to_value (new_value_obj, self->value->type (),
1381 true);
1382 if (new_value == nullptr)
1383 return -1;
1384
1385 if (!valpy_assign_core (self, new_value))
1386 return -1;
1387
1388 return 0;
1389 }
1390
1391 /* Implements gdb.Value.fetch_lazy (). */
1392 static PyObject *
1393 valpy_fetch_lazy (PyObject *self, PyObject *args)
1394 {
1395 struct value *value = ((value_object *) self)->value;
1396
1397 try
1398 {
1399 if (value->lazy ())
1400 value->fetch_lazy ();
1401 }
1402 catch (const gdb_exception &except)
1403 {
1404 return gdbpy_handle_gdb_exception (nullptr, except);
1405 }
1406
1407 Py_RETURN_NONE;
1408 }
1409
1410 /* Calculate and return the address of the PyObject as the value of
1411 the builtin __hash__ call. */
1412 static Py_hash_t
1413 valpy_hash (PyObject *self)
1414 {
1415 return (intptr_t) self;
1416 }
1417
1418 enum valpy_opcode
1419 {
1420 VALPY_ADD,
1421 VALPY_SUB,
1422 VALPY_MUL,
1423 VALPY_DIV,
1424 VALPY_REM,
1425 VALPY_POW,
1426 VALPY_LSH,
1427 VALPY_RSH,
1428 VALPY_BITAND,
1429 VALPY_BITOR,
1430 VALPY_BITXOR
1431 };
1432
1433 /* If TYPE is a reference, return the target; otherwise return TYPE. */
1434 #define STRIP_REFERENCE(TYPE) \
1435 (TYPE_IS_REFERENCE (TYPE) ? ((TYPE)->target_type ()) : (TYPE))
1436
1437 /* Helper for valpy_binop. Returns a value object which is the result
1438 of applying the operation specified by OPCODE to the given
1439 arguments. Throws a GDB exception on error. */
1440
1441 static PyObject *
1442 valpy_binop_throw (enum valpy_opcode opcode, PyObject *self, PyObject *other)
1443 {
1444 PyObject *result = NULL;
1445
1446 struct value *arg1, *arg2;
1447 struct value *res_val = NULL;
1448 enum exp_opcode op = OP_NULL;
1449 int handled = 0;
1450
1451 scoped_value_mark free_values;
1452
1453 /* If the gdb.Value object is the second operand, then it will be
1454 passed to us as the OTHER argument, and SELF will be an entirely
1455 different kind of object, altogether. Because of this, we can't
1456 assume self is a gdb.Value object and need to convert it from
1457 python as well. */
1458 arg1 = convert_value_from_python (self);
1459 if (arg1 == NULL)
1460 return NULL;
1461
1462 arg2 = convert_value_from_python (other);
1463 if (arg2 == NULL)
1464 return NULL;
1465
1466 switch (opcode)
1467 {
1468 case VALPY_ADD:
1469 {
1470 struct type *ltype = arg1->type ();
1471 struct type *rtype = arg2->type ();
1472
1473 ltype = check_typedef (ltype);
1474 ltype = STRIP_REFERENCE (ltype);
1475 rtype = check_typedef (rtype);
1476 rtype = STRIP_REFERENCE (rtype);
1477
1478 handled = 1;
1479 if (ltype->code () == TYPE_CODE_PTR
1480 && is_integral_type (rtype))
1481 res_val = value_ptradd (arg1, value_as_long (arg2));
1482 else if (rtype->code () == TYPE_CODE_PTR
1483 && is_integral_type (ltype))
1484 res_val = value_ptradd (arg2, value_as_long (arg1));
1485 else
1486 {
1487 handled = 0;
1488 op = BINOP_ADD;
1489 }
1490 }
1491 break;
1492 case VALPY_SUB:
1493 {
1494 struct type *ltype = arg1->type ();
1495 struct type *rtype = arg2->type ();
1496
1497 ltype = check_typedef (ltype);
1498 ltype = STRIP_REFERENCE (ltype);
1499 rtype = check_typedef (rtype);
1500 rtype = STRIP_REFERENCE (rtype);
1501
1502 handled = 1;
1503 if (ltype->code () == TYPE_CODE_PTR
1504 && rtype->code () == TYPE_CODE_PTR)
1505 /* A ptrdiff_t for the target would be preferable here. */
1506 res_val = value_from_longest (builtin_type_pyint,
1507 value_ptrdiff (arg1, arg2));
1508 else if (ltype->code () == TYPE_CODE_PTR
1509 && is_integral_type (rtype))
1510 res_val = value_ptradd (arg1, - value_as_long (arg2));
1511 else
1512 {
1513 handled = 0;
1514 op = BINOP_SUB;
1515 }
1516 }
1517 break;
1518 case VALPY_MUL:
1519 op = BINOP_MUL;
1520 break;
1521 case VALPY_DIV:
1522 op = BINOP_DIV;
1523 break;
1524 case VALPY_REM:
1525 op = BINOP_REM;
1526 break;
1527 case VALPY_POW:
1528 op = BINOP_EXP;
1529 break;
1530 case VALPY_LSH:
1531 op = BINOP_LSH;
1532 break;
1533 case VALPY_RSH:
1534 op = BINOP_RSH;
1535 break;
1536 case VALPY_BITAND:
1537 op = BINOP_BITWISE_AND;
1538 break;
1539 case VALPY_BITOR:
1540 op = BINOP_BITWISE_IOR;
1541 break;
1542 case VALPY_BITXOR:
1543 op = BINOP_BITWISE_XOR;
1544 break;
1545 }
1546
1547 if (!handled)
1548 {
1549 if (binop_user_defined_p (op, arg1, arg2))
1550 res_val = value_x_binop (arg1, arg2, op, OP_NULL, EVAL_NORMAL);
1551 else
1552 res_val = value_binop (arg1, arg2, op);
1553 }
1554
1555 if (res_val)
1556 result = value_to_value_object (res_val);
1557
1558 return result;
1559 }
1560
1561 /* Returns a value object which is the result of applying the operation
1562 specified by OPCODE to the given arguments. Returns NULL on error, with
1563 a python exception set. */
1564 static PyObject *
1565 valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
1566 {
1567 PyObject *result = NULL;
1568
1569 try
1570 {
1571 result = valpy_binop_throw (opcode, self, other);
1572 }
1573 catch (const gdb_exception &except)
1574 {
1575 return gdbpy_handle_gdb_exception (nullptr, except);
1576 }
1577
1578 return result;
1579 }
1580
1581 static PyObject *
1582 valpy_add (PyObject *self, PyObject *other)
1583 {
1584 return valpy_binop (VALPY_ADD, self, other);
1585 }
1586
1587 static PyObject *
1588 valpy_subtract (PyObject *self, PyObject *other)
1589 {
1590 return valpy_binop (VALPY_SUB, self, other);
1591 }
1592
1593 static PyObject *
1594 valpy_multiply (PyObject *self, PyObject *other)
1595 {
1596 return valpy_binop (VALPY_MUL, self, other);
1597 }
1598
1599 static PyObject *
1600 valpy_divide (PyObject *self, PyObject *other)
1601 {
1602 return valpy_binop (VALPY_DIV, self, other);
1603 }
1604
1605 static PyObject *
1606 valpy_remainder (PyObject *self, PyObject *other)
1607 {
1608 return valpy_binop (VALPY_REM, self, other);
1609 }
1610
1611 static PyObject *
1612 valpy_power (PyObject *self, PyObject *other, PyObject *unused)
1613 {
1614 /* We don't support the ternary form of pow. I don't know how to express
1615 that, so let's just throw NotImplementedError to at least do something
1616 about it. */
1617 if (unused != Py_None)
1618 {
1619 PyErr_SetString (PyExc_NotImplementedError,
1620 "Invalid operation on gdb.Value.");
1621 return NULL;
1622 }
1623
1624 return valpy_binop (VALPY_POW, self, other);
1625 }
1626
1627 static PyObject *
1628 valpy_negative (PyObject *self)
1629 {
1630 PyObject *result = NULL;
1631
1632 try
1633 {
1634 /* Perhaps overkill, but consistency has some virtue. */
1635 scoped_value_mark free_values;
1636 struct value *val;
1637
1638 val = value_neg (((value_object *) self)->value);
1639 result = value_to_value_object (val);
1640 }
1641 catch (const gdb_exception &except)
1642 {
1643 return gdbpy_handle_gdb_exception (nullptr, except);
1644 }
1645
1646 return result;
1647 }
1648
1649 static PyObject *
1650 valpy_positive (PyObject *self)
1651 {
1652 return value_to_value_object (((value_object *) self)->value);
1653 }
1654
1655 static PyObject *
1656 valpy_absolute (PyObject *self)
1657 {
1658 struct value *value = ((value_object *) self)->value;
1659 int isabs = 1;
1660
1661 try
1662 {
1663 scoped_value_mark free_values;
1664
1665 if (value_less (value, value::zero (value->type (), not_lval)))
1666 isabs = 0;
1667 }
1668 catch (const gdb_exception &except)
1669 {
1670 return gdbpy_handle_gdb_exception (nullptr, except);
1671 }
1672
1673 if (isabs)
1674 return valpy_positive (self);
1675 else
1676 return valpy_negative (self);
1677 }
1678
1679 /* Implements boolean evaluation of gdb.Value. */
1680 static int
1681 valpy_nonzero (PyObject *self)
1682 {
1683 value_object *self_value = (value_object *) self;
1684 struct type *type;
1685 int nonzero = 0; /* Appease GCC warning. */
1686
1687 try
1688 {
1689 type = check_typedef (self_value->value->type ());
1690
1691 if (is_integral_type (type) || type->code () == TYPE_CODE_PTR)
1692 nonzero = !!value_as_long (self_value->value);
1693 else if (is_floating_value (self_value->value))
1694 nonzero = !target_float_is_zero
1695 (self_value->value->contents ().data (), type);
1696 else
1697 /* All other values are True. */
1698 nonzero = 1;
1699 }
1700 catch (const gdb_exception &ex)
1701 {
1702 /* This is not documented in the Python documentation, but if
1703 this function fails, return -1 as slot_nb_nonzero does (the
1704 default Python nonzero function). */
1705 return gdbpy_handle_gdb_exception (-1, ex);
1706 }
1707
1708 return nonzero;
1709 }
1710
1711 /* Implements ~ for value objects. */
1712 static PyObject *
1713 valpy_invert (PyObject *self)
1714 {
1715 PyObject *result = nullptr;
1716
1717 try
1718 {
1719 scoped_value_mark free_values;
1720 struct value *val = value_complement (((value_object *) self)->value);
1721 result = value_to_value_object (val);
1722 }
1723 catch (const gdb_exception &except)
1724 {
1725 return gdbpy_handle_gdb_exception (nullptr, except);
1726 }
1727
1728 return result;
1729 }
1730
1731 /* Implements left shift for value objects. */
1732 static PyObject *
1733 valpy_lsh (PyObject *self, PyObject *other)
1734 {
1735 return valpy_binop (VALPY_LSH, self, other);
1736 }
1737
1738 /* Implements right shift for value objects. */
1739 static PyObject *
1740 valpy_rsh (PyObject *self, PyObject *other)
1741 {
1742 return valpy_binop (VALPY_RSH, self, other);
1743 }
1744
1745 /* Implements bitwise and for value objects. */
1746 static PyObject *
1747 valpy_and (PyObject *self, PyObject *other)
1748 {
1749 return valpy_binop (VALPY_BITAND, self, other);
1750 }
1751
1752 /* Implements bitwise or for value objects. */
1753 static PyObject *
1754 valpy_or (PyObject *self, PyObject *other)
1755 {
1756 return valpy_binop (VALPY_BITOR, self, other);
1757 }
1758
1759 /* Implements bitwise xor for value objects. */
1760 static PyObject *
1761 valpy_xor (PyObject *self, PyObject *other)
1762 {
1763 return valpy_binop (VALPY_BITXOR, self, other);
1764 }
1765
1766 /* Helper for valpy_richcompare. Implements comparison operations for
1767 value objects. Returns true/false on success. Returns -1 with a
1768 Python exception set if a Python error is detected. Throws a GDB
1769 exception on other errors (memory error, etc.). */
1770
1771 static int
1772 valpy_richcompare_throw (PyObject *self, PyObject *other, int op)
1773 {
1774 int result;
1775 struct value *value_other;
1776 struct value *value_self;
1777
1778 scoped_value_mark free_values;
1779
1780 value_other = convert_value_from_python (other);
1781 if (value_other == NULL)
1782 return -1;
1783
1784 value_self = ((value_object *) self)->value;
1785
1786 switch (op)
1787 {
1788 case Py_LT:
1789 result = value_less (value_self, value_other);
1790 break;
1791 case Py_LE:
1792 result = value_less (value_self, value_other)
1793 || value_equal (value_self, value_other);
1794 break;
1795 case Py_EQ:
1796 result = value_equal (value_self, value_other);
1797 break;
1798 case Py_NE:
1799 result = !value_equal (value_self, value_other);
1800 break;
1801 case Py_GT:
1802 result = value_less (value_other, value_self);
1803 break;
1804 case Py_GE:
1805 result = (value_less (value_other, value_self)
1806 || value_equal (value_self, value_other));
1807 break;
1808 default:
1809 /* Can't happen. */
1810 PyErr_SetString (PyExc_NotImplementedError,
1811 _("Invalid operation on gdb.Value."));
1812 result = -1;
1813 break;
1814 }
1815
1816 return result;
1817 }
1818
1819
1820 /* Implements comparison operations for value objects. Returns NULL on error,
1821 with a python exception set. */
1822 static PyObject *
1823 valpy_richcompare (PyObject *self, PyObject *other, int op)
1824 {
1825 int result = 0;
1826
1827 if (other == Py_None)
1828 /* Comparing with None is special. From what I can tell, in Python
1829 None is smaller than anything else. */
1830 switch (op) {
1831 case Py_LT:
1832 case Py_LE:
1833 case Py_EQ:
1834 Py_RETURN_FALSE;
1835 case Py_NE:
1836 case Py_GT:
1837 case Py_GE:
1838 Py_RETURN_TRUE;
1839 default:
1840 /* Can't happen. */
1841 PyErr_SetString (PyExc_NotImplementedError,
1842 _("Invalid operation on gdb.Value."));
1843 return NULL;
1844 }
1845
1846 try
1847 {
1848 result = valpy_richcompare_throw (self, other, op);
1849 }
1850 catch (const gdb_exception &except)
1851 {
1852 return gdbpy_handle_gdb_exception (nullptr, except);
1853 }
1854
1855 /* In this case, the Python exception has already been set. */
1856 if (result < 0)
1857 return NULL;
1858
1859 if (result == 1)
1860 Py_RETURN_TRUE;
1861
1862 Py_RETURN_FALSE;
1863 }
1864
1865 /* Implements conversion to long. */
1866 static PyObject *
1867 valpy_long (PyObject *self)
1868 {
1869 struct value *value = ((value_object *) self)->value;
1870 struct type *type = value->type ();
1871 LONGEST l = 0;
1872
1873 try
1874 {
1875 if (is_floating_value (value))
1876 {
1877 type = builtin_type_pylong;
1878 value = value_cast (type, value);
1879 }
1880
1881 type = check_typedef (type);
1882
1883 if (!is_integral_type (type)
1884 && type->code () != TYPE_CODE_PTR)
1885 error (_("Cannot convert value to long."));
1886
1887 l = value_as_long (value);
1888 }
1889 catch (const gdb_exception &except)
1890 {
1891 return gdbpy_handle_gdb_exception (nullptr, except);
1892 }
1893
1894 if (type->is_unsigned ())
1895 return gdb_py_object_from_ulongest (l).release ();
1896 else
1897 return gdb_py_object_from_longest (l).release ();
1898 }
1899
1900 /* Implements conversion to float. */
1901 static PyObject *
1902 valpy_float (PyObject *self)
1903 {
1904 struct value *value = ((value_object *) self)->value;
1905 struct type *type = value->type ();
1906 double d = 0;
1907
1908 try
1909 {
1910 type = check_typedef (type);
1911
1912 if (type->code () == TYPE_CODE_FLT && is_floating_value (value))
1913 d = target_float_to_host_double (value->contents ().data (), type);
1914 else if (type->code () == TYPE_CODE_INT)
1915 {
1916 /* Note that valpy_long accepts TYPE_CODE_PTR and some
1917 others here here -- but casting a pointer or bool to a
1918 float seems wrong. */
1919 d = value_as_long (value);
1920 }
1921 else
1922 error (_("Cannot convert value to float."));
1923 }
1924 catch (const gdb_exception &except)
1925 {
1926 return gdbpy_handle_gdb_exception (nullptr, except);
1927 }
1928
1929 return PyFloat_FromDouble (d);
1930 }
1931
1932 /* Returns an object for a value, without releasing it from the
1933 all_values chain. */
1934 PyObject *
1935 value_to_value_object (struct value *val)
1936 {
1937 value_object *val_obj;
1938
1939 val_obj = PyObject_New (value_object, &value_object_type);
1940 if (val_obj != NULL)
1941 {
1942 val->incref ();
1943 val_obj->value = val;
1944 val_obj->next = nullptr;
1945 val_obj->prev = nullptr;
1946 val_obj->address = NULL;
1947 val_obj->type = NULL;
1948 val_obj->dynamic_type = NULL;
1949 val_obj->content_bytes = nullptr;
1950 note_value (val_obj);
1951 }
1952
1953 return (PyObject *) val_obj;
1954 }
1955
1956 /* Returns a borrowed reference to the struct value corresponding to
1957 the given value object. */
1958 struct value *
1959 value_object_to_value (PyObject *self)
1960 {
1961 value_object *real;
1962
1963 if (! PyObject_TypeCheck (self, &value_object_type))
1964 return NULL;
1965 real = (value_object *) self;
1966 return real->value;
1967 }
1968
1969 /* Try to convert a Python value to a gdb value. If the value cannot
1970 be converted, set a Python exception and return NULL. Returns a
1971 reference to a new value on the all_values chain. */
1972
1973 struct value *
1974 convert_value_from_python (PyObject *obj)
1975 {
1976 struct value *value = NULL; /* -Wall */
1977 int cmp;
1978
1979 gdb_assert (obj != NULL);
1980
1981 try
1982 {
1983 if (PyBool_Check (obj))
1984 {
1985 cmp = obj == Py_True ? 1 : 0;
1986 value = value_from_longest (builtin_type_pybool, cmp);
1987 }
1988 else if (PyLong_Check (obj))
1989 {
1990 LONGEST l = PyLong_AsLongLong (obj);
1991
1992 if (PyErr_Occurred ())
1993 {
1994 /* If the error was an overflow, we can try converting to
1995 ULONGEST instead. */
1996 if (PyErr_ExceptionMatches (PyExc_OverflowError))
1997 {
1998 gdbpy_err_fetch fetched_error;
1999 gdbpy_ref<> zero = gdb_py_object_from_longest (0);
2000
2001 /* Check whether obj is positive. */
2002 if (PyObject_RichCompareBool (obj, zero.get (), Py_GT) > 0)
2003 {
2004 ULONGEST ul;
2005
2006 ul = PyLong_AsUnsignedLongLong (obj);
2007 if (! PyErr_Occurred ())
2008 value = value_from_ulongest (builtin_type_upylong, ul);
2009 }
2010 else
2011 {
2012 /* There's nothing we can do. */
2013 fetched_error.restore ();
2014 }
2015 }
2016 }
2017 else
2018 value = value_from_longest (builtin_type_pylong, l);
2019 }
2020 else if (PyFloat_Check (obj))
2021 {
2022 double d = PyFloat_AsDouble (obj);
2023
2024 if (! PyErr_Occurred ())
2025 value = value_from_host_double (builtin_type_pyfloat, d);
2026 }
2027 else if (gdbpy_is_string (obj))
2028 {
2029 gdb::unique_xmalloc_ptr<char> s
2030 = python_string_to_target_string (obj);
2031 if (s != NULL)
2032 value
2033 = current_language->value_string (gdbpy_enter::get_gdbarch (),
2034 s.get (), strlen (s.get ()));
2035 }
2036 else if (PyObject_TypeCheck (obj, &value_object_type))
2037 value = ((value_object *) obj)->value->copy ();
2038 else if (gdbpy_is_lazy_string (obj))
2039 {
2040 PyObject *result;
2041
2042 result = PyObject_CallMethodObjArgs (obj, gdbpy_value_cst, NULL);
2043 value = ((value_object *) result)->value->copy ();
2044 }
2045 else
2046 PyErr_Format (PyExc_TypeError,
2047 _("Could not convert Python object: %S."), obj);
2048 }
2049 catch (const gdb_exception &except)
2050 {
2051 return gdbpy_handle_gdb_exception (nullptr, except);
2052 }
2053
2054 return value;
2055 }
2056
2057 /* Returns value object in the ARGth position in GDB's history. */
2058 PyObject *
2059 gdbpy_history (PyObject *self, PyObject *args)
2060 {
2061 int i;
2062
2063 if (!PyArg_ParseTuple (args, "i", &i))
2064 return NULL;
2065
2066 PyObject *result = nullptr;
2067 try
2068 {
2069 scoped_value_mark free_values;
2070 struct value *res_val = access_value_history (i);
2071 result = value_to_value_object (res_val);
2072 }
2073 catch (const gdb_exception &except)
2074 {
2075 return gdbpy_handle_gdb_exception (nullptr, except);
2076 }
2077
2078 return result;
2079 }
2080
2081 /* Add a gdb.Value into GDB's history, and return (as an integer) the
2082 position of the newly added value. */
2083 PyObject *
2084 gdbpy_add_history (PyObject *self, PyObject *args)
2085 {
2086 PyObject *value_obj;
2087
2088 if (!PyArg_ParseTuple (args, "O", &value_obj))
2089 return nullptr;
2090
2091 struct value *value = convert_value_from_python (value_obj);
2092 if (value == nullptr)
2093 return nullptr;
2094
2095 try
2096 {
2097 int idx = value->record_latest ();
2098 return gdb_py_object_from_longest (idx).release ();
2099 }
2100 catch (const gdb_exception &except)
2101 {
2102 return gdbpy_handle_gdb_exception (nullptr, except);
2103 }
2104
2105 return nullptr;
2106 }
2107
2108 /* Return an integer, the number of items in GDB's history. */
2109
2110 PyObject *
2111 gdbpy_history_count (PyObject *self, PyObject *args)
2112 {
2113 return gdb_py_object_from_ulongest (value_history_count ()).release ();
2114 }
2115
2116 /* Return the value of a convenience variable. */
2117 PyObject *
2118 gdbpy_convenience_variable (PyObject *self, PyObject *args)
2119 {
2120 const char *varname;
2121 struct value *res_val = NULL;
2122
2123 if (!PyArg_ParseTuple (args, "s", &varname))
2124 return NULL;
2125
2126 PyObject *result = nullptr;
2127 bool found = false;
2128 try
2129 {
2130 struct internalvar *var = lookup_only_internalvar (varname);
2131
2132 if (var != NULL)
2133 {
2134 scoped_value_mark free_values;
2135 res_val = value_of_internalvar (gdbpy_enter::get_gdbarch (), var);
2136 if (res_val->type ()->code () == TYPE_CODE_VOID)
2137 res_val = NULL;
2138 else
2139 {
2140 found = true;
2141 result = value_to_value_object (res_val);
2142 }
2143 }
2144 }
2145 catch (const gdb_exception &except)
2146 {
2147 return gdbpy_handle_gdb_exception (nullptr, except);
2148 }
2149
2150 if (result == nullptr && !found)
2151 Py_RETURN_NONE;
2152
2153 return result;
2154 }
2155
2156 /* Set the value of a convenience variable. */
2157 PyObject *
2158 gdbpy_set_convenience_variable (PyObject *self, PyObject *args)
2159 {
2160 const char *varname;
2161 PyObject *value_obj;
2162 struct value *value = NULL;
2163
2164 if (!PyArg_ParseTuple (args, "sO", &varname, &value_obj))
2165 return NULL;
2166
2167 /* None means to clear the variable. */
2168 if (value_obj != Py_None)
2169 {
2170 value = convert_value_from_python (value_obj);
2171 if (value == NULL)
2172 return NULL;
2173 }
2174
2175 try
2176 {
2177 if (value == NULL)
2178 {
2179 struct internalvar *var = lookup_only_internalvar (varname);
2180
2181 if (var != NULL)
2182 clear_internalvar (var);
2183 }
2184 else
2185 {
2186 struct internalvar *var = lookup_internalvar (varname);
2187
2188 set_internalvar (var, value);
2189 }
2190 }
2191 catch (const gdb_exception &except)
2192 {
2193 return gdbpy_handle_gdb_exception (nullptr, except);
2194 }
2195
2196 Py_RETURN_NONE;
2197 }
2198
2199 /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
2200
2201 int
2202 gdbpy_is_value_object (PyObject *obj)
2203 {
2204 return PyObject_TypeCheck (obj, &value_object_type);
2205 }
2206
2207 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
2208 gdbpy_initialize_values (void)
2209 {
2210 return gdbpy_type_ready (&value_object_type);
2211 }
2212
2213 GDBPY_INITIALIZE_FILE (gdbpy_initialize_values);
2214
2215
2216
2218 static gdb_PyGetSetDef value_object_getset[] = {
2219 { "address", valpy_get_address, NULL, "The address of the value.",
2220 NULL },
2221 { "is_optimized_out", valpy_get_is_optimized_out, NULL,
2222 "Boolean telling whether the value is optimized "
2223 "out (i.e., not available).",
2224 NULL },
2225 { "type", valpy_get_type, NULL, "Type of the value.", NULL },
2226 { "dynamic_type", valpy_get_dynamic_type, NULL,
2227 "Dynamic type of the value.", NULL },
2228 { "is_lazy", valpy_get_is_lazy, NULL,
2229 "Boolean telling whether the value is lazy (not fetched yet\n\
2230 from the inferior). A lazy value is fetched when needed, or when\n\
2231 the \"fetch_lazy()\" method is called.", NULL },
2232 { "bytes", valpy_get_bytes, valpy_set_bytes,
2233 "Return a bytearray containing the bytes of this value.", nullptr },
2234 {NULL} /* Sentinel */
2235 };
2236
2237 static PyMethodDef value_object_methods[] = {
2238 { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." },
2239 { "dynamic_cast", valpy_dynamic_cast, METH_VARARGS,
2240 "dynamic_cast (gdb.Type) -> gdb.Value\n\
2241 Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
2242 },
2243 { "reinterpret_cast", valpy_reinterpret_cast, METH_VARARGS,
2244 "reinterpret_cast (gdb.Type) -> gdb.Value\n\
2245 Cast the value to the supplied type, as if by the C++\n\
2246 reinterpret_cast operator."
2247 },
2248 { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
2249 { "referenced_value", valpy_referenced_value, METH_NOARGS,
2250 "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." },
2251 { "reference_value", valpy_lvalue_reference_value, METH_NOARGS,
2252 "Return a value of type TYPE_CODE_REF referencing this value." },
2253 { "rvalue_reference_value", valpy_rvalue_reference_value, METH_NOARGS,
2254 "Return a value of type TYPE_CODE_RVALUE_REF referencing this value." },
2255 { "const_value", valpy_const_value, METH_NOARGS,
2256 "Return a 'const' qualified version of the same value." },
2257 { "lazy_string", (PyCFunction) valpy_lazy_string,
2258 METH_VARARGS | METH_KEYWORDS,
2259 "lazy_string ([encoding] [, length]) -> lazy_string\n\
2260 Return a lazy string representation of the value." },
2261 { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS,
2262 "string ([encoding] [, errors] [, length]) -> string\n\
2263 Return Unicode string representation of the value." },
2264 { "fetch_lazy", valpy_fetch_lazy, METH_NOARGS,
2265 "Fetches the value from the inferior, if it was lazy." },
2266 { "format_string", (PyCFunction) valpy_format_string,
2267 METH_VARARGS | METH_KEYWORDS,
2268 "format_string (...) -> string\n\
2269 Return a string representation of the value using the specified\n\
2270 formatting options" },
2271 { "assign", (PyCFunction) valpy_assign, METH_VARARGS,
2272 "assign (VAL) -> None\n\
2273 Assign VAL to this value." },
2274 { "to_array", valpy_to_array, METH_NOARGS,
2275 "to_array () -> Value\n\
2276 Return value as an array, if possible." },
2277 {NULL} /* Sentinel */
2278 };
2279
2280 static PyNumberMethods value_object_as_number = {
2281 valpy_add,
2282 valpy_subtract,
2283 valpy_multiply,
2284 valpy_remainder,
2285 NULL, /* nb_divmod */
2286 valpy_power, /* nb_power */
2287 valpy_negative, /* nb_negative */
2288 valpy_positive, /* nb_positive */
2289 valpy_absolute, /* nb_absolute */
2290 valpy_nonzero, /* nb_nonzero */
2291 valpy_invert, /* nb_invert */
2292 valpy_lsh, /* nb_lshift */
2293 valpy_rsh, /* nb_rshift */
2294 valpy_and, /* nb_and */
2295 valpy_xor, /* nb_xor */
2296 valpy_or, /* nb_or */
2297 valpy_long, /* nb_int */
2298 NULL, /* reserved */
2299 valpy_float, /* nb_float */
2300 NULL, /* nb_inplace_add */
2301 NULL, /* nb_inplace_subtract */
2302 NULL, /* nb_inplace_multiply */
2303 NULL, /* nb_inplace_remainder */
2304 NULL, /* nb_inplace_power */
2305 NULL, /* nb_inplace_lshift */
2306 NULL, /* nb_inplace_rshift */
2307 NULL, /* nb_inplace_and */
2308 NULL, /* nb_inplace_xor */
2309 NULL, /* nb_inplace_or */
2310 NULL, /* nb_floor_divide */
2311 valpy_divide, /* nb_true_divide */
2312 NULL, /* nb_inplace_floor_divide */
2313 NULL, /* nb_inplace_true_divide */
2314 valpy_long, /* nb_index */
2315 };
2316
2317 static PyMappingMethods value_object_as_mapping = {
2318 valpy_length,
2319 valpy_getitem,
2320 valpy_setitem
2321 };
2322
2323 PyTypeObject value_object_type = {
2324 PyVarObject_HEAD_INIT (NULL, 0)
2325 "gdb.Value", /*tp_name*/
2326 sizeof (value_object), /*tp_basicsize*/
2327 0, /*tp_itemsize*/
2328 valpy_dealloc, /*tp_dealloc*/
2329 0, /*tp_print*/
2330 0, /*tp_getattr*/
2331 0, /*tp_setattr*/
2332 0, /*tp_compare*/
2333 0, /*tp_repr*/
2334 &value_object_as_number, /*tp_as_number*/
2335 0, /*tp_as_sequence*/
2336 &value_object_as_mapping, /*tp_as_mapping*/
2337 valpy_hash, /*tp_hash*/
2338 valpy_call, /*tp_call*/
2339 valpy_str, /*tp_str*/
2340 0, /*tp_getattro*/
2341 0, /*tp_setattro*/
2342 0, /*tp_as_buffer*/
2343 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES
2344 | Py_TPFLAGS_BASETYPE, /*tp_flags*/
2345 "GDB value object", /* tp_doc */
2346 0, /* tp_traverse */
2347 0, /* tp_clear */
2348 valpy_richcompare, /* tp_richcompare */
2349 0, /* tp_weaklistoffset */
2350 0, /* tp_iter */
2351 0, /* tp_iternext */
2352 value_object_methods, /* tp_methods */
2353 0, /* tp_members */
2354 value_object_getset, /* tp_getset */
2355 0, /* tp_base */
2356 0, /* tp_dict */
2357 0, /* tp_descr_get */
2358 0, /* tp_descr_set */
2359 0, /* tp_dictoffset */
2360 valpy_init, /* tp_init */
2361 0, /* tp_alloc */
2362 PyType_GenericNew, /* tp_new */
2363 };
2364