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