Fix latent bug in target_pass_ctrlc
[binutils-gdb.git] / gdb / varobj.c
blob3358be4e77b48886f66301a592dad6e01d10938a
1 /* Implementation of the GDB variable objects API.
3 Copyright (C) 1999-2020 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #include "defs.h"
19 #include "value.h"
20 #include "expression.h"
21 #include "frame.h"
22 #include "language.h"
23 #include "gdbcmd.h"
24 #include "block.h"
25 #include "valprint.h"
26 #include "gdb_regex.h"
28 #include "varobj.h"
29 #include "gdbthread.h"
30 #include "inferior.h"
31 #include "varobj-iter.h"
32 #include "parser-defs.h"
33 #include "gdbarch.h"
35 #if HAVE_PYTHON
36 #include "python/python.h"
37 #include "python/python-internal.h"
38 #else
39 typedef int PyObject;
40 #endif
42 /* See varobj.h. */
44 unsigned int varobjdebug = 0;
45 static void
46 show_varobjdebug (struct ui_file *file, int from_tty,
47 struct cmd_list_element *c, const char *value)
49 fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
52 /* String representations of gdb's format codes. */
53 const char *varobj_format_string[] =
54 { "natural", "binary", "decimal", "hexadecimal", "octal", "zero-hexadecimal" };
56 /* True if we want to allow Python-based pretty-printing. */
57 static bool pretty_printing = false;
59 void
60 varobj_enable_pretty_printing (void)
62 pretty_printing = true;
65 /* Data structures */
67 /* Every root variable has one of these structures saved in its
68 varobj. */
69 struct varobj_root
71 /* The expression for this parent. */
72 expression_up exp;
74 /* Block for which this expression is valid. */
75 const struct block *valid_block = NULL;
77 /* The frame for this expression. This field is set iff valid_block is
78 not NULL. */
79 struct frame_id frame = null_frame_id;
81 /* The global thread ID that this varobj_root belongs to. This field
82 is only valid if valid_block is not NULL.
83 When not 0, indicates which thread 'frame' belongs to.
84 When 0, indicates that the thread list was empty when the varobj_root
85 was created. */
86 int thread_id = 0;
88 /* If true, the -var-update always recomputes the value in the
89 current thread and frame. Otherwise, variable object is
90 always updated in the specific scope/thread/frame. */
91 bool floating = false;
93 /* Flag that indicates validity: set to false when this varobj_root refers
94 to symbols that do not exist anymore. */
95 bool is_valid = true;
97 /* Language-related operations for this variable and its
98 children. */
99 const struct lang_varobj_ops *lang_ops = NULL;
101 /* The varobj for this root node. */
102 struct varobj *rootvar = NULL;
104 /* Next root variable */
105 struct varobj_root *next = NULL;
108 /* Dynamic part of varobj. */
110 struct varobj_dynamic
112 /* Whether the children of this varobj were requested. This field is
113 used to decide if dynamic varobj should recompute their children.
114 In the event that the frontend never asked for the children, we
115 can avoid that. */
116 bool children_requested = false;
118 /* The pretty-printer constructor. If NULL, then the default
119 pretty-printer will be looked up. If None, then no
120 pretty-printer will be installed. */
121 PyObject *constructor = NULL;
123 /* The pretty-printer that has been constructed. If NULL, then a
124 new printer object is needed, and one will be constructed. */
125 PyObject *pretty_printer = NULL;
127 /* The iterator returned by the printer's 'children' method, or NULL
128 if not available. */
129 struct varobj_iter *child_iter = NULL;
131 /* We request one extra item from the iterator, so that we can
132 report to the caller whether there are more items than we have
133 already reported. However, we don't want to install this value
134 when we read it, because that will mess up future updates. So,
135 we stash it here instead. */
136 varobj_item *saved_item = NULL;
139 /* A list of varobjs */
141 struct vlist
143 struct varobj *var;
144 struct vlist *next;
147 /* Private function prototypes */
149 /* Helper functions for the above subcommands. */
151 static int delete_variable (struct varobj *, bool);
153 static void delete_variable_1 (int *, struct varobj *, bool, bool);
155 static bool install_variable (struct varobj *);
157 static void uninstall_variable (struct varobj *);
159 static struct varobj *create_child (struct varobj *, int, std::string &);
161 static struct varobj *
162 create_child_with_value (struct varobj *parent, int index,
163 struct varobj_item *item);
165 /* Utility routines */
167 static enum varobj_display_formats variable_default_display (struct varobj *);
169 static bool update_type_if_necessary (struct varobj *var,
170 struct value *new_value);
172 static bool install_new_value (struct varobj *var, struct value *value,
173 bool initial);
175 /* Language-specific routines. */
177 static int number_of_children (const struct varobj *);
179 static std::string name_of_variable (const struct varobj *);
181 static std::string name_of_child (struct varobj *, int);
183 static struct value *value_of_root (struct varobj **var_handle, bool *);
185 static struct value *value_of_child (const struct varobj *parent, int index);
187 static std::string my_value_of_variable (struct varobj *var,
188 enum varobj_display_formats format);
190 static bool is_root_p (const struct varobj *var);
192 static struct varobj *varobj_add_child (struct varobj *var,
193 struct varobj_item *item);
195 /* Private data */
197 /* Mappings of varobj_display_formats enums to gdb's format codes. */
198 static int format_code[] = { 0, 't', 'd', 'x', 'o', 'z' };
200 /* Header of the list of root variable objects. */
201 static struct varobj_root *rootlist;
203 /* Prime number indicating the number of buckets in the hash table. */
204 /* A prime large enough to avoid too many collisions. */
205 #define VAROBJ_TABLE_SIZE 227
207 /* Pointer to the varobj hash table (built at run time). */
208 static struct vlist **varobj_table;
212 /* API Implementation */
213 static bool
214 is_root_p (const struct varobj *var)
216 return (var->root->rootvar == var);
219 #ifdef HAVE_PYTHON
221 /* See python-internal.h. */
222 gdbpy_enter_varobj::gdbpy_enter_varobj (const struct varobj *var)
223 : gdbpy_enter (var->root->exp->gdbarch, var->root->exp->language_defn)
227 #endif
229 /* Return the full FRAME which corresponds to the given CORE_ADDR
230 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
232 static struct frame_info *
233 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
235 struct frame_info *frame = NULL;
237 if (frame_addr == (CORE_ADDR) 0)
238 return NULL;
240 for (frame = get_current_frame ();
241 frame != NULL;
242 frame = get_prev_frame (frame))
244 /* The CORE_ADDR we get as argument was parsed from a string GDB
245 output as $fp. This output got truncated to gdbarch_addr_bit.
246 Truncate the frame base address in the same manner before
247 comparing it against our argument. */
248 CORE_ADDR frame_base = get_frame_base_address (frame);
249 int addr_bit = gdbarch_addr_bit (get_frame_arch (frame));
251 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
252 frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1;
254 if (frame_base == frame_addr)
255 return frame;
258 return NULL;
261 /* Creates a varobj (not its children). */
263 struct varobj *
264 varobj_create (const char *objname,
265 const char *expression, CORE_ADDR frame, enum varobj_type type)
267 /* Fill out a varobj structure for the (root) variable being constructed. */
268 std::unique_ptr<varobj> var (new varobj (new varobj_root));
270 if (expression != NULL)
272 struct frame_info *fi;
273 struct frame_id old_id = null_frame_id;
274 const struct block *block;
275 const char *p;
276 struct value *value = NULL;
277 CORE_ADDR pc;
279 /* Parse and evaluate the expression, filling in as much of the
280 variable's data as possible. */
282 if (has_stack_frames ())
284 /* Allow creator to specify context of variable. */
285 if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
286 fi = get_selected_frame (NULL);
287 else
288 /* FIXME: cagney/2002-11-23: This code should be doing a
289 lookup using the frame ID and not just the frame's
290 ``address''. This, of course, means an interface
291 change. However, with out that interface change ISAs,
292 such as the ia64 with its two stacks, won't work.
293 Similar goes for the case where there is a frameless
294 function. */
295 fi = find_frame_addr_in_frame_chain (frame);
297 else
298 fi = NULL;
300 if (type == USE_SELECTED_FRAME)
301 var->root->floating = true;
303 pc = 0;
304 block = NULL;
305 if (fi != NULL)
307 block = get_frame_block (fi, 0);
308 pc = get_frame_pc (fi);
311 p = expression;
313 innermost_block_tracker tracker (INNERMOST_BLOCK_FOR_SYMBOLS
314 | INNERMOST_BLOCK_FOR_REGISTERS);
315 /* Wrap the call to parse expression, so we can
316 return a sensible error. */
319 var->root->exp = parse_exp_1 (&p, pc, block, 0, &tracker);
322 catch (const gdb_exception_error &except)
324 return NULL;
327 /* Don't allow variables to be created for types. */
328 if (var->root->exp->elts[0].opcode == OP_TYPE
329 || var->root->exp->elts[0].opcode == OP_TYPEOF
330 || var->root->exp->elts[0].opcode == OP_DECLTYPE)
332 fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
333 " as an expression.\n");
334 return NULL;
337 var->format = variable_default_display (var.get ());
338 var->root->valid_block =
339 var->root->floating ? NULL : tracker.block ();
340 var->name = expression;
341 /* For a root var, the name and the expr are the same. */
342 var->path_expr = expression;
344 /* When the frame is different from the current frame,
345 we must select the appropriate frame before parsing
346 the expression, otherwise the value will not be current.
347 Since select_frame is so benign, just call it for all cases. */
348 if (var->root->valid_block)
350 /* User could specify explicit FRAME-ADDR which was not found but
351 EXPRESSION is frame specific and we would not be able to evaluate
352 it correctly next time. With VALID_BLOCK set we must also set
353 FRAME and THREAD_ID. */
354 if (fi == NULL)
355 error (_("Failed to find the specified frame"));
357 var->root->frame = get_frame_id (fi);
358 var->root->thread_id = inferior_thread ()->global_num;
359 old_id = get_frame_id (get_selected_frame (NULL));
360 select_frame (fi);
363 /* We definitely need to catch errors here.
364 If evaluate_expression succeeds we got the value we wanted.
365 But if it fails, we still go on with a call to evaluate_type(). */
368 value = evaluate_expression (var->root->exp.get ());
370 catch (const gdb_exception_error &except)
372 /* Error getting the value. Try to at least get the
373 right type. */
374 struct value *type_only_value = evaluate_type (var->root->exp.get ());
376 var->type = value_type (type_only_value);
379 if (value != NULL)
381 int real_type_found = 0;
383 var->type = value_actual_type (value, 0, &real_type_found);
384 if (real_type_found)
385 value = value_cast (var->type, value);
388 /* Set language info */
389 var->root->lang_ops = var->root->exp->language_defn->la_varobj_ops;
391 install_new_value (var.get (), value, 1 /* Initial assignment */);
393 /* Set ourselves as our root. */
394 var->root->rootvar = var.get ();
396 /* Reset the selected frame. */
397 if (frame_id_p (old_id))
398 select_frame (frame_find_by_id (old_id));
401 /* If the variable object name is null, that means this
402 is a temporary variable, so don't install it. */
404 if ((var != NULL) && (objname != NULL))
406 var->obj_name = objname;
408 /* If a varobj name is duplicated, the install will fail so
409 we must cleanup. */
410 if (!install_variable (var.get ()))
411 return NULL;
414 return var.release ();
417 /* Generates an unique name that can be used for a varobj. */
419 std::string
420 varobj_gen_name (void)
422 static int id = 0;
424 /* Generate a name for this object. */
425 id++;
426 return string_printf ("var%d", id);
429 /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
430 error if OBJNAME cannot be found. */
432 struct varobj *
433 varobj_get_handle (const char *objname)
435 struct vlist *cv;
436 const char *chp;
437 unsigned int index = 0;
438 unsigned int i = 1;
440 for (chp = objname; *chp; chp++)
442 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
445 cv = *(varobj_table + index);
446 while (cv != NULL && cv->var->obj_name != objname)
447 cv = cv->next;
449 if (cv == NULL)
450 error (_("Variable object not found"));
452 return cv->var;
455 /* Given the handle, return the name of the object. */
457 const char *
458 varobj_get_objname (const struct varobj *var)
460 return var->obj_name.c_str ();
463 /* Given the handle, return the expression represented by the
464 object. */
466 std::string
467 varobj_get_expression (const struct varobj *var)
469 return name_of_variable (var);
472 /* See varobj.h. */
475 varobj_delete (struct varobj *var, bool only_children)
477 return delete_variable (var, only_children);
480 #if HAVE_PYTHON
482 /* Convenience function for varobj_set_visualizer. Instantiate a
483 pretty-printer for a given value. */
484 static PyObject *
485 instantiate_pretty_printer (PyObject *constructor, struct value *value)
487 PyObject *val_obj = NULL;
488 PyObject *printer;
490 val_obj = value_to_value_object (value);
491 if (! val_obj)
492 return NULL;
494 printer = PyObject_CallFunctionObjArgs (constructor, val_obj, NULL);
495 Py_DECREF (val_obj);
496 return printer;
499 #endif
501 /* Set/Get variable object display format. */
503 enum varobj_display_formats
504 varobj_set_display_format (struct varobj *var,
505 enum varobj_display_formats format)
507 switch (format)
509 case FORMAT_NATURAL:
510 case FORMAT_BINARY:
511 case FORMAT_DECIMAL:
512 case FORMAT_HEXADECIMAL:
513 case FORMAT_OCTAL:
514 case FORMAT_ZHEXADECIMAL:
515 var->format = format;
516 break;
518 default:
519 var->format = variable_default_display (var);
522 if (varobj_value_is_changeable_p (var)
523 && var->value != nullptr && !value_lazy (var->value.get ()))
525 var->print_value = varobj_value_get_print_value (var->value.get (),
526 var->format, var);
529 return var->format;
532 enum varobj_display_formats
533 varobj_get_display_format (const struct varobj *var)
535 return var->format;
538 gdb::unique_xmalloc_ptr<char>
539 varobj_get_display_hint (const struct varobj *var)
541 gdb::unique_xmalloc_ptr<char> result;
543 #if HAVE_PYTHON
544 if (!gdb_python_initialized)
545 return NULL;
547 gdbpy_enter_varobj enter_py (var);
549 if (var->dynamic->pretty_printer != NULL)
550 result = gdbpy_get_display_hint (var->dynamic->pretty_printer);
551 #endif
553 return result;
556 /* Return true if the varobj has items after TO, false otherwise. */
558 bool
559 varobj_has_more (const struct varobj *var, int to)
561 if (var->children.size () > to)
562 return true;
564 return ((to == -1 || var->children.size () == to)
565 && (var->dynamic->saved_item != NULL));
568 /* If the variable object is bound to a specific thread, that
569 is its evaluation can always be done in context of a frame
570 inside that thread, returns GDB id of the thread -- which
571 is always positive. Otherwise, returns -1. */
573 varobj_get_thread_id (const struct varobj *var)
575 if (var->root->valid_block && var->root->thread_id > 0)
576 return var->root->thread_id;
577 else
578 return -1;
581 void
582 varobj_set_frozen (struct varobj *var, bool frozen)
584 /* When a variable is unfrozen, we don't fetch its value.
585 The 'not_fetched' flag remains set, so next -var-update
586 won't complain.
588 We don't fetch the value, because for structures the client
589 should do -var-update anyway. It would be bad to have different
590 client-size logic for structure and other types. */
591 var->frozen = frozen;
594 bool
595 varobj_get_frozen (const struct varobj *var)
597 return var->frozen;
600 /* A helper function that updates the contents of FROM and TO based on the
601 size of the vector CHILDREN. If the contents of either FROM or TO are
602 negative the entire range is used. */
604 void
605 varobj_restrict_range (const std::vector<varobj *> &children,
606 int *from, int *to)
608 int len = children.size ();
610 if (*from < 0 || *to < 0)
612 *from = 0;
613 *to = len;
615 else
617 if (*from > len)
618 *from = len;
619 if (*to > len)
620 *to = len;
621 if (*from > *to)
622 *from = *to;
626 /* A helper for update_dynamic_varobj_children that installs a new
627 child when needed. */
629 static void
630 install_dynamic_child (struct varobj *var,
631 std::vector<varobj *> *changed,
632 std::vector<varobj *> *type_changed,
633 std::vector<varobj *> *newobj,
634 std::vector<varobj *> *unchanged,
635 bool *cchanged,
636 int index,
637 struct varobj_item *item)
639 if (var->children.size () < index + 1)
641 /* There's no child yet. */
642 struct varobj *child = varobj_add_child (var, item);
644 if (newobj != NULL)
646 newobj->push_back (child);
647 *cchanged = true;
650 else
652 varobj *existing = var->children[index];
653 bool type_updated = update_type_if_necessary (existing, item->value);
655 if (type_updated)
657 if (type_changed != NULL)
658 type_changed->push_back (existing);
660 if (install_new_value (existing, item->value, 0))
662 if (!type_updated && changed != NULL)
663 changed->push_back (existing);
665 else if (!type_updated && unchanged != NULL)
666 unchanged->push_back (existing);
670 #if HAVE_PYTHON
672 static bool
673 dynamic_varobj_has_child_method (const struct varobj *var)
675 PyObject *printer = var->dynamic->pretty_printer;
677 if (!gdb_python_initialized)
678 return false;
680 gdbpy_enter_varobj enter_py (var);
681 return PyObject_HasAttr (printer, gdbpy_children_cst);
683 #endif
685 /* A factory for creating dynamic varobj's iterators. Returns an
686 iterator object suitable for iterating over VAR's children. */
688 static struct varobj_iter *
689 varobj_get_iterator (struct varobj *var)
691 #if HAVE_PYTHON
692 if (var->dynamic->pretty_printer)
693 return py_varobj_get_iterator (var, var->dynamic->pretty_printer);
694 #endif
696 gdb_assert_not_reached (_("\
697 requested an iterator from a non-dynamic varobj"));
700 /* Release and clear VAR's saved item, if any. */
702 static void
703 varobj_clear_saved_item (struct varobj_dynamic *var)
705 if (var->saved_item != NULL)
707 value_decref (var->saved_item->value);
708 delete var->saved_item;
709 var->saved_item = NULL;
713 static bool
714 update_dynamic_varobj_children (struct varobj *var,
715 std::vector<varobj *> *changed,
716 std::vector<varobj *> *type_changed,
717 std::vector<varobj *> *newobj,
718 std::vector<varobj *> *unchanged,
719 bool *cchanged,
720 bool update_children,
721 int from,
722 int to)
724 int i;
726 *cchanged = false;
728 if (update_children || var->dynamic->child_iter == NULL)
730 varobj_iter_delete (var->dynamic->child_iter);
731 var->dynamic->child_iter = varobj_get_iterator (var);
733 varobj_clear_saved_item (var->dynamic);
735 i = 0;
737 if (var->dynamic->child_iter == NULL)
738 return false;
740 else
741 i = var->children.size ();
743 /* We ask for one extra child, so that MI can report whether there
744 are more children. */
745 for (; to < 0 || i < to + 1; ++i)
747 varobj_item *item;
749 /* See if there was a leftover from last time. */
750 if (var->dynamic->saved_item != NULL)
752 item = var->dynamic->saved_item;
753 var->dynamic->saved_item = NULL;
755 else
757 item = varobj_iter_next (var->dynamic->child_iter);
758 /* Release vitem->value so its lifetime is not bound to the
759 execution of a command. */
760 if (item != NULL && item->value != NULL)
761 item->value = release_value (item->value).release ();
764 if (item == NULL)
766 /* Iteration is done. Remove iterator from VAR. */
767 varobj_iter_delete (var->dynamic->child_iter);
768 var->dynamic->child_iter = NULL;
769 break;
771 /* We don't want to push the extra child on any report list. */
772 if (to < 0 || i < to)
774 bool can_mention = from < 0 || i >= from;
776 install_dynamic_child (var, can_mention ? changed : NULL,
777 can_mention ? type_changed : NULL,
778 can_mention ? newobj : NULL,
779 can_mention ? unchanged : NULL,
780 can_mention ? cchanged : NULL, i,
781 item);
783 delete item;
785 else
787 var->dynamic->saved_item = item;
789 /* We want to truncate the child list just before this
790 element. */
791 break;
795 if (i < var->children.size ())
797 *cchanged = true;
798 for (int j = i; j < var->children.size (); ++j)
799 varobj_delete (var->children[j], 0);
801 var->children.resize (i);
804 /* If there are fewer children than requested, note that the list of
805 children changed. */
806 if (to >= 0 && var->children.size () < to)
807 *cchanged = true;
809 var->num_children = var->children.size ();
811 return true;
815 varobj_get_num_children (struct varobj *var)
817 if (var->num_children == -1)
819 if (varobj_is_dynamic_p (var))
821 bool dummy;
823 /* If we have a dynamic varobj, don't report -1 children.
824 So, try to fetch some children first. */
825 update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL, &dummy,
826 false, 0, 0);
828 else
829 var->num_children = number_of_children (var);
832 return var->num_children >= 0 ? var->num_children : 0;
835 /* Creates a list of the immediate children of a variable object;
836 the return code is the number of such children or -1 on error. */
838 const std::vector<varobj *> &
839 varobj_list_children (struct varobj *var, int *from, int *to)
841 var->dynamic->children_requested = true;
843 if (varobj_is_dynamic_p (var))
845 bool children_changed;
847 /* This, in theory, can result in the number of children changing without
848 frontend noticing. But well, calling -var-list-children on the same
849 varobj twice is not something a sane frontend would do. */
850 update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL,
851 &children_changed, false, 0, *to);
852 varobj_restrict_range (var->children, from, to);
853 return var->children;
856 if (var->num_children == -1)
857 var->num_children = number_of_children (var);
859 /* If that failed, give up. */
860 if (var->num_children == -1)
861 return var->children;
863 /* If we're called when the list of children is not yet initialized,
864 allocate enough elements in it. */
865 while (var->children.size () < var->num_children)
866 var->children.push_back (NULL);
868 for (int i = 0; i < var->num_children; i++)
870 if (var->children[i] == NULL)
872 /* Either it's the first call to varobj_list_children for
873 this variable object, and the child was never created,
874 or it was explicitly deleted by the client. */
875 std::string name = name_of_child (var, i);
876 var->children[i] = create_child (var, i, name);
880 varobj_restrict_range (var->children, from, to);
881 return var->children;
884 static struct varobj *
885 varobj_add_child (struct varobj *var, struct varobj_item *item)
887 varobj *v = create_child_with_value (var, var->children.size (), item);
889 var->children.push_back (v);
891 return v;
894 /* Obtain the type of an object Variable as a string similar to the one gdb
895 prints on the console. The caller is responsible for freeing the string.
898 std::string
899 varobj_get_type (struct varobj *var)
901 /* For the "fake" variables, do not return a type. (Its type is
902 NULL, too.)
903 Do not return a type for invalid variables as well. */
904 if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
905 return std::string ();
907 return type_to_string (var->type);
910 /* Obtain the type of an object variable. */
912 struct type *
913 varobj_get_gdb_type (const struct varobj *var)
915 return var->type;
918 /* Is VAR a path expression parent, i.e., can it be used to construct
919 a valid path expression? */
921 static bool
922 is_path_expr_parent (const struct varobj *var)
924 gdb_assert (var->root->lang_ops->is_path_expr_parent != NULL);
925 return var->root->lang_ops->is_path_expr_parent (var);
928 /* Is VAR a path expression parent, i.e., can it be used to construct
929 a valid path expression? By default we assume any VAR can be a path
930 parent. */
932 bool
933 varobj_default_is_path_expr_parent (const struct varobj *var)
935 return true;
938 /* Return the path expression parent for VAR. */
940 const struct varobj *
941 varobj_get_path_expr_parent (const struct varobj *var)
943 const struct varobj *parent = var;
945 while (!is_root_p (parent) && !is_path_expr_parent (parent))
946 parent = parent->parent;
948 /* Computation of full rooted expression for children of dynamic
949 varobjs is not supported. */
950 if (varobj_is_dynamic_p (parent))
951 error (_("Invalid variable object (child of a dynamic varobj)"));
953 return parent;
956 /* Return a pointer to the full rooted expression of varobj VAR.
957 If it has not been computed yet, compute it. */
959 const char *
960 varobj_get_path_expr (const struct varobj *var)
962 if (var->path_expr.empty ())
964 /* For root varobjs, we initialize path_expr
965 when creating varobj, so here it should be
966 child varobj. */
967 struct varobj *mutable_var = (struct varobj *) var;
968 gdb_assert (!is_root_p (var));
970 mutable_var->path_expr = (*var->root->lang_ops->path_expr_of_child) (var);
973 return var->path_expr.c_str ();
976 const struct language_defn *
977 varobj_get_language (const struct varobj *var)
979 return var->root->exp->language_defn;
983 varobj_get_attributes (const struct varobj *var)
985 int attributes = 0;
987 if (varobj_editable_p (var))
988 /* FIXME: define masks for attributes. */
989 attributes |= 0x00000001; /* Editable */
991 return attributes;
994 /* Return true if VAR is a dynamic varobj. */
996 bool
997 varobj_is_dynamic_p (const struct varobj *var)
999 return var->dynamic->pretty_printer != NULL;
1002 std::string
1003 varobj_get_formatted_value (struct varobj *var,
1004 enum varobj_display_formats format)
1006 return my_value_of_variable (var, format);
1009 std::string
1010 varobj_get_value (struct varobj *var)
1012 return my_value_of_variable (var, var->format);
1015 /* Set the value of an object variable (if it is editable) to the
1016 value of the given expression. */
1017 /* Note: Invokes functions that can call error(). */
1019 bool
1020 varobj_set_value (struct varobj *var, const char *expression)
1022 struct value *val = NULL; /* Initialize to keep gcc happy. */
1023 /* The argument "expression" contains the variable's new value.
1024 We need to first construct a legal expression for this -- ugh! */
1025 /* Does this cover all the bases? */
1026 struct value *value = NULL; /* Initialize to keep gcc happy. */
1027 int saved_input_radix = input_radix;
1028 const char *s = expression;
1030 gdb_assert (varobj_editable_p (var));
1032 input_radix = 10; /* ALWAYS reset to decimal temporarily. */
1033 expression_up exp = parse_exp_1 (&s, 0, 0, 0);
1036 value = evaluate_expression (exp.get ());
1039 catch (const gdb_exception_error &except)
1041 /* We cannot proceed without a valid expression. */
1042 return false;
1045 /* All types that are editable must also be changeable. */
1046 gdb_assert (varobj_value_is_changeable_p (var));
1048 /* The value of a changeable variable object must not be lazy. */
1049 gdb_assert (!value_lazy (var->value.get ()));
1051 /* Need to coerce the input. We want to check if the
1052 value of the variable object will be different
1053 after assignment, and the first thing value_assign
1054 does is coerce the input.
1055 For example, if we are assigning an array to a pointer variable we
1056 should compare the pointer with the array's address, not with the
1057 array's content. */
1058 value = coerce_array (value);
1060 /* The new value may be lazy. value_assign, or
1061 rather value_contents, will take care of this. */
1064 val = value_assign (var->value.get (), value);
1067 catch (const gdb_exception_error &except)
1069 return false;
1072 /* If the value has changed, record it, so that next -var-update can
1073 report this change. If a variable had a value of '1', we've set it
1074 to '333' and then set again to '1', when -var-update will report this
1075 variable as changed -- because the first assignment has set the
1076 'updated' flag. There's no need to optimize that, because return value
1077 of -var-update should be considered an approximation. */
1078 var->updated = install_new_value (var, val, false /* Compare values. */);
1079 input_radix = saved_input_radix;
1080 return true;
1083 #if HAVE_PYTHON
1085 /* A helper function to install a constructor function and visualizer
1086 in a varobj_dynamic. */
1088 static void
1089 install_visualizer (struct varobj_dynamic *var, PyObject *constructor,
1090 PyObject *visualizer)
1092 Py_XDECREF (var->constructor);
1093 var->constructor = constructor;
1095 Py_XDECREF (var->pretty_printer);
1096 var->pretty_printer = visualizer;
1098 varobj_iter_delete (var->child_iter);
1099 var->child_iter = NULL;
1102 /* Install the default visualizer for VAR. */
1104 static void
1105 install_default_visualizer (struct varobj *var)
1107 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1108 if (CPLUS_FAKE_CHILD (var))
1109 return;
1111 if (pretty_printing)
1113 gdbpy_ref<> pretty_printer;
1115 if (var->value != nullptr)
1117 pretty_printer = gdbpy_get_varobj_pretty_printer (var->value.get ());
1118 if (pretty_printer == nullptr)
1120 gdbpy_print_stack ();
1121 error (_("Cannot instantiate printer for default visualizer"));
1125 if (pretty_printer == Py_None)
1126 pretty_printer.reset (nullptr);
1128 install_visualizer (var->dynamic, NULL, pretty_printer.release ());
1132 /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
1133 make a new object. */
1135 static void
1136 construct_visualizer (struct varobj *var, PyObject *constructor)
1138 PyObject *pretty_printer;
1140 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1141 if (CPLUS_FAKE_CHILD (var))
1142 return;
1144 Py_INCREF (constructor);
1145 if (constructor == Py_None)
1146 pretty_printer = NULL;
1147 else
1149 pretty_printer = instantiate_pretty_printer (constructor,
1150 var->value.get ());
1151 if (! pretty_printer)
1153 gdbpy_print_stack ();
1154 Py_DECREF (constructor);
1155 constructor = Py_None;
1156 Py_INCREF (constructor);
1159 if (pretty_printer == Py_None)
1161 Py_DECREF (pretty_printer);
1162 pretty_printer = NULL;
1166 install_visualizer (var->dynamic, constructor, pretty_printer);
1169 #endif /* HAVE_PYTHON */
1171 /* A helper function for install_new_value. This creates and installs
1172 a visualizer for VAR, if appropriate. */
1174 static void
1175 install_new_value_visualizer (struct varobj *var)
1177 #if HAVE_PYTHON
1178 /* If the constructor is None, then we want the raw value. If VAR
1179 does not have a value, just skip this. */
1180 if (!gdb_python_initialized)
1181 return;
1183 if (var->dynamic->constructor != Py_None && var->value != NULL)
1185 gdbpy_enter_varobj enter_py (var);
1187 if (var->dynamic->constructor == NULL)
1188 install_default_visualizer (var);
1189 else
1190 construct_visualizer (var, var->dynamic->constructor);
1192 #else
1193 /* Do nothing. */
1194 #endif
1197 /* When using RTTI to determine variable type it may be changed in runtime when
1198 the variable value is changed. This function checks whether type of varobj
1199 VAR will change when a new value NEW_VALUE is assigned and if it is so
1200 updates the type of VAR. */
1202 static bool
1203 update_type_if_necessary (struct varobj *var, struct value *new_value)
1205 if (new_value)
1207 struct value_print_options opts;
1209 get_user_print_options (&opts);
1210 if (opts.objectprint)
1212 struct type *new_type = value_actual_type (new_value, 0, 0);
1213 std::string new_type_str = type_to_string (new_type);
1214 std::string curr_type_str = varobj_get_type (var);
1216 /* Did the type name change? */
1217 if (curr_type_str != new_type_str)
1219 var->type = new_type;
1221 /* This information may be not valid for a new type. */
1222 varobj_delete (var, 1);
1223 var->children.clear ();
1224 var->num_children = -1;
1225 return true;
1230 return false;
1233 /* Assign a new value to a variable object. If INITIAL is true,
1234 this is the first assignment after the variable object was just
1235 created, or changed type. In that case, just assign the value
1236 and return false.
1237 Otherwise, assign the new value, and return true if the value is
1238 different from the current one, false otherwise. The comparison is
1239 done on textual representation of value. Therefore, some types
1240 need not be compared. E.g. for structures the reported value is
1241 always "{...}", so no comparison is necessary here. If the old
1242 value was NULL and new one is not, or vice versa, we always return true.
1244 The VALUE parameter should not be released -- the function will
1245 take care of releasing it when needed. */
1246 static bool
1247 install_new_value (struct varobj *var, struct value *value, bool initial)
1249 bool changeable;
1250 bool need_to_fetch;
1251 bool changed = false;
1252 bool intentionally_not_fetched = false;
1254 /* We need to know the varobj's type to decide if the value should
1255 be fetched or not. C++ fake children (public/protected/private)
1256 don't have a type. */
1257 gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
1258 changeable = varobj_value_is_changeable_p (var);
1260 /* If the type has custom visualizer, we consider it to be always
1261 changeable. FIXME: need to make sure this behaviour will not
1262 mess up read-sensitive values. */
1263 if (var->dynamic->pretty_printer != NULL)
1264 changeable = true;
1266 need_to_fetch = changeable;
1268 /* We are not interested in the address of references, and given
1269 that in C++ a reference is not rebindable, it cannot
1270 meaningfully change. So, get hold of the real value. */
1271 if (value)
1272 value = coerce_ref (value);
1274 if (var->type && var->type->code () == TYPE_CODE_UNION)
1275 /* For unions, we need to fetch the value implicitly because
1276 of implementation of union member fetch. When gdb
1277 creates a value for a field and the value of the enclosing
1278 structure is not lazy, it immediately copies the necessary
1279 bytes from the enclosing values. If the enclosing value is
1280 lazy, the call to value_fetch_lazy on the field will read
1281 the data from memory. For unions, that means we'll read the
1282 same memory more than once, which is not desirable. So
1283 fetch now. */
1284 need_to_fetch = true;
1286 /* The new value might be lazy. If the type is changeable,
1287 that is we'll be comparing values of this type, fetch the
1288 value now. Otherwise, on the next update the old value
1289 will be lazy, which means we've lost that old value. */
1290 if (need_to_fetch && value && value_lazy (value))
1292 const struct varobj *parent = var->parent;
1293 bool frozen = var->frozen;
1295 for (; !frozen && parent; parent = parent->parent)
1296 frozen |= parent->frozen;
1298 if (frozen && initial)
1300 /* For variables that are frozen, or are children of frozen
1301 variables, we don't do fetch on initial assignment.
1302 For non-initial assignment we do the fetch, since it means we're
1303 explicitly asked to compare the new value with the old one. */
1304 intentionally_not_fetched = true;
1306 else
1311 value_fetch_lazy (value);
1314 catch (const gdb_exception_error &except)
1316 /* Set the value to NULL, so that for the next -var-update,
1317 we don't try to compare the new value with this value,
1318 that we couldn't even read. */
1319 value = NULL;
1324 /* Get a reference now, before possibly passing it to any Python
1325 code that might release it. */
1326 value_ref_ptr value_holder;
1327 if (value != NULL)
1328 value_holder = value_ref_ptr::new_reference (value);
1330 /* Below, we'll be comparing string rendering of old and new
1331 values. Don't get string rendering if the value is
1332 lazy -- if it is, the code above has decided that the value
1333 should not be fetched. */
1334 std::string print_value;
1335 if (value != NULL && !value_lazy (value)
1336 && var->dynamic->pretty_printer == NULL)
1337 print_value = varobj_value_get_print_value (value, var->format, var);
1339 /* If the type is changeable, compare the old and the new values.
1340 If this is the initial assignment, we don't have any old value
1341 to compare with. */
1342 if (!initial && changeable)
1344 /* If the value of the varobj was changed by -var-set-value,
1345 then the value in the varobj and in the target is the same.
1346 However, that value is different from the value that the
1347 varobj had after the previous -var-update. So need to the
1348 varobj as changed. */
1349 if (var->updated)
1350 changed = true;
1351 else if (var->dynamic->pretty_printer == NULL)
1353 /* Try to compare the values. That requires that both
1354 values are non-lazy. */
1355 if (var->not_fetched && value_lazy (var->value.get ()))
1357 /* This is a frozen varobj and the value was never read.
1358 Presumably, UI shows some "never read" indicator.
1359 Now that we've fetched the real value, we need to report
1360 this varobj as changed so that UI can show the real
1361 value. */
1362 changed = true;
1364 else if (var->value == NULL && value == NULL)
1365 /* Equal. */
1367 else if (var->value == NULL || value == NULL)
1369 changed = true;
1371 else
1373 gdb_assert (!value_lazy (var->value.get ()));
1374 gdb_assert (!value_lazy (value));
1376 gdb_assert (!var->print_value.empty () && !print_value.empty ());
1377 if (var->print_value != print_value)
1378 changed = true;
1383 if (!initial && !changeable)
1385 /* For values that are not changeable, we don't compare the values.
1386 However, we want to notice if a value was not NULL and now is NULL,
1387 or vise versa, so that we report when top-level varobjs come in scope
1388 and leave the scope. */
1389 changed = (var->value != NULL) != (value != NULL);
1392 /* We must always keep the new value, since children depend on it. */
1393 var->value = value_holder;
1394 if (value && value_lazy (value) && intentionally_not_fetched)
1395 var->not_fetched = true;
1396 else
1397 var->not_fetched = false;
1398 var->updated = false;
1400 install_new_value_visualizer (var);
1402 /* If we installed a pretty-printer, re-compare the printed version
1403 to see if the variable changed. */
1404 if (var->dynamic->pretty_printer != NULL)
1406 print_value = varobj_value_get_print_value (var->value.get (),
1407 var->format, var);
1408 if ((var->print_value.empty () && !print_value.empty ())
1409 || (!var->print_value.empty () && print_value.empty ())
1410 || (!var->print_value.empty () && !print_value.empty ()
1411 && var->print_value != print_value))
1412 changed = true;
1414 var->print_value = print_value;
1416 gdb_assert (var->value == nullptr || value_type (var->value.get ()));
1418 return changed;
1421 /* Return the requested range for a varobj. VAR is the varobj. FROM
1422 and TO are out parameters; *FROM and *TO will be set to the
1423 selected sub-range of VAR. If no range was selected using
1424 -var-set-update-range, then both will be -1. */
1425 void
1426 varobj_get_child_range (const struct varobj *var, int *from, int *to)
1428 *from = var->from;
1429 *to = var->to;
1432 /* Set the selected sub-range of children of VAR to start at index
1433 FROM and end at index TO. If either FROM or TO is less than zero,
1434 this is interpreted as a request for all children. */
1435 void
1436 varobj_set_child_range (struct varobj *var, int from, int to)
1438 var->from = from;
1439 var->to = to;
1442 void
1443 varobj_set_visualizer (struct varobj *var, const char *visualizer)
1445 #if HAVE_PYTHON
1446 PyObject *mainmod;
1448 if (!gdb_python_initialized)
1449 return;
1451 gdbpy_enter_varobj enter_py (var);
1453 mainmod = PyImport_AddModule ("__main__");
1454 gdbpy_ref<> globals
1455 = gdbpy_ref<>::new_reference (PyModule_GetDict (mainmod));
1456 gdbpy_ref<> constructor (PyRun_String (visualizer, Py_eval_input,
1457 globals.get (), globals.get ()));
1459 if (constructor == NULL)
1461 gdbpy_print_stack ();
1462 error (_("Could not evaluate visualizer expression: %s"), visualizer);
1465 construct_visualizer (var, constructor.get ());
1467 /* If there are any children now, wipe them. */
1468 varobj_delete (var, 1 /* children only */);
1469 var->num_children = -1;
1470 #else
1471 error (_("Python support required"));
1472 #endif
1475 /* If NEW_VALUE is the new value of the given varobj (var), return
1476 true if var has mutated. In other words, if the type of
1477 the new value is different from the type of the varobj's old
1478 value.
1480 NEW_VALUE may be NULL, if the varobj is now out of scope. */
1482 static bool
1483 varobj_value_has_mutated (const struct varobj *var, struct value *new_value,
1484 struct type *new_type)
1486 /* If we haven't previously computed the number of children in var,
1487 it does not matter from the front-end's perspective whether
1488 the type has mutated or not. For all intents and purposes,
1489 it has not mutated. */
1490 if (var->num_children < 0)
1491 return false;
1493 if (var->root->lang_ops->value_has_mutated != NULL)
1495 /* The varobj module, when installing new values, explicitly strips
1496 references, saying that we're not interested in those addresses.
1497 But detection of mutation happens before installing the new
1498 value, so our value may be a reference that we need to strip
1499 in order to remain consistent. */
1500 if (new_value != NULL)
1501 new_value = coerce_ref (new_value);
1502 return var->root->lang_ops->value_has_mutated (var, new_value, new_type);
1504 else
1505 return false;
1508 /* Update the values for a variable and its children. This is a
1509 two-pronged attack. First, re-parse the value for the root's
1510 expression to see if it's changed. Then go all the way
1511 through its children, reconstructing them and noting if they've
1512 changed.
1514 The IS_EXPLICIT parameter specifies if this call is result
1515 of MI request to update this specific variable, or
1516 result of implicit -var-update *. For implicit request, we don't
1517 update frozen variables.
1519 NOTE: This function may delete the caller's varobj. If it
1520 returns TYPE_CHANGED, then it has done this and VARP will be modified
1521 to point to the new varobj. */
1523 std::vector<varobj_update_result>
1524 varobj_update (struct varobj **varp, bool is_explicit)
1526 bool type_changed = false;
1527 struct value *newobj;
1528 std::vector<varobj_update_result> stack;
1529 std::vector<varobj_update_result> result;
1531 /* Frozen means frozen -- we don't check for any change in
1532 this varobj, including its going out of scope, or
1533 changing type. One use case for frozen varobjs is
1534 retaining previously evaluated expressions, and we don't
1535 want them to be reevaluated at all. */
1536 if (!is_explicit && (*varp)->frozen)
1537 return result;
1539 if (!(*varp)->root->is_valid)
1541 result.emplace_back (*varp, VAROBJ_INVALID);
1542 return result;
1545 if ((*varp)->root->rootvar == *varp)
1547 varobj_update_result r (*varp);
1549 /* Update the root variable. value_of_root can return NULL
1550 if the variable is no longer around, i.e. we stepped out of
1551 the frame in which a local existed. We are letting the
1552 value_of_root variable dispose of the varobj if the type
1553 has changed. */
1554 newobj = value_of_root (varp, &type_changed);
1555 if (update_type_if_necessary (*varp, newobj))
1556 type_changed = true;
1557 r.varobj = *varp;
1558 r.type_changed = type_changed;
1559 if (install_new_value ((*varp), newobj, type_changed))
1560 r.changed = true;
1562 if (newobj == NULL)
1563 r.status = VAROBJ_NOT_IN_SCOPE;
1564 r.value_installed = true;
1566 if (r.status == VAROBJ_NOT_IN_SCOPE)
1568 if (r.type_changed || r.changed)
1569 result.push_back (std::move (r));
1571 return result;
1574 stack.push_back (std::move (r));
1576 else
1577 stack.emplace_back (*varp);
1579 /* Walk through the children, reconstructing them all. */
1580 while (!stack.empty ())
1582 varobj_update_result r = std::move (stack.back ());
1583 stack.pop_back ();
1584 struct varobj *v = r.varobj;
1586 /* Update this variable, unless it's a root, which is already
1587 updated. */
1588 if (!r.value_installed)
1590 struct type *new_type;
1592 newobj = value_of_child (v->parent, v->index);
1593 if (update_type_if_necessary (v, newobj))
1594 r.type_changed = true;
1595 if (newobj)
1596 new_type = value_type (newobj);
1597 else
1598 new_type = v->root->lang_ops->type_of_child (v->parent, v->index);
1600 if (varobj_value_has_mutated (v, newobj, new_type))
1602 /* The children are no longer valid; delete them now.
1603 Report the fact that its type changed as well. */
1604 varobj_delete (v, 1 /* only_children */);
1605 v->num_children = -1;
1606 v->to = -1;
1607 v->from = -1;
1608 v->type = new_type;
1609 r.type_changed = true;
1612 if (install_new_value (v, newobj, r.type_changed))
1614 r.changed = true;
1615 v->updated = false;
1619 /* We probably should not get children of a dynamic varobj, but
1620 for which -var-list-children was never invoked. */
1621 if (varobj_is_dynamic_p (v))
1623 std::vector<varobj *> changed, type_changed_vec, unchanged, newobj_vec;
1624 bool children_changed = false;
1626 if (v->frozen)
1627 continue;
1629 if (!v->dynamic->children_requested)
1631 bool dummy;
1633 /* If we initially did not have potential children, but
1634 now we do, consider the varobj as changed.
1635 Otherwise, if children were never requested, consider
1636 it as unchanged -- presumably, such varobj is not yet
1637 expanded in the UI, so we need not bother getting
1638 it. */
1639 if (!varobj_has_more (v, 0))
1641 update_dynamic_varobj_children (v, NULL, NULL, NULL, NULL,
1642 &dummy, false, 0, 0);
1643 if (varobj_has_more (v, 0))
1644 r.changed = true;
1647 if (r.changed)
1648 result.push_back (std::move (r));
1650 continue;
1653 /* If update_dynamic_varobj_children returns false, then we have
1654 a non-conforming pretty-printer, so we skip it. */
1655 if (update_dynamic_varobj_children (v, &changed, &type_changed_vec,
1656 &newobj_vec,
1657 &unchanged, &children_changed,
1658 true, v->from, v->to))
1660 if (children_changed || !newobj_vec.empty ())
1662 r.children_changed = true;
1663 r.newobj = std::move (newobj_vec);
1665 /* Push in reverse order so that the first child is
1666 popped from the work stack first, and so will be
1667 added to result first. This does not affect
1668 correctness, just "nicer". */
1669 for (int i = type_changed_vec.size () - 1; i >= 0; --i)
1671 varobj_update_result item (type_changed_vec[i]);
1673 /* Type may change only if value was changed. */
1674 item.changed = true;
1675 item.type_changed = true;
1676 item.value_installed = true;
1678 stack.push_back (std::move (item));
1680 for (int i = changed.size () - 1; i >= 0; --i)
1682 varobj_update_result item (changed[i]);
1684 item.changed = true;
1685 item.value_installed = true;
1687 stack.push_back (std::move (item));
1689 for (int i = unchanged.size () - 1; i >= 0; --i)
1691 if (!unchanged[i]->frozen)
1693 varobj_update_result item (unchanged[i]);
1695 item.value_installed = true;
1697 stack.push_back (std::move (item));
1700 if (r.changed || r.children_changed)
1701 result.push_back (std::move (r));
1703 continue;
1707 /* Push any children. Use reverse order so that the first
1708 child is popped from the work stack first, and so
1709 will be added to result first. This does not
1710 affect correctness, just "nicer". */
1711 for (int i = v->children.size () - 1; i >= 0; --i)
1713 varobj *c = v->children[i];
1715 /* Child may be NULL if explicitly deleted by -var-delete. */
1716 if (c != NULL && !c->frozen)
1717 stack.emplace_back (c);
1720 if (r.changed || r.type_changed)
1721 result.push_back (std::move (r));
1724 return result;
1727 /* Helper functions */
1730 * Variable object construction/destruction
1733 static int
1734 delete_variable (struct varobj *var, bool only_children_p)
1736 int delcount = 0;
1738 delete_variable_1 (&delcount, var, only_children_p,
1739 true /* remove_from_parent_p */ );
1741 return delcount;
1744 /* Delete the variable object VAR and its children. */
1745 /* IMPORTANT NOTE: If we delete a variable which is a child
1746 and the parent is not removed we dump core. It must be always
1747 initially called with remove_from_parent_p set. */
1748 static void
1749 delete_variable_1 (int *delcountp, struct varobj *var, bool only_children_p,
1750 bool remove_from_parent_p)
1752 /* Delete any children of this variable, too. */
1753 for (varobj *child : var->children)
1755 if (!child)
1756 continue;
1758 if (!remove_from_parent_p)
1759 child->parent = NULL;
1761 delete_variable_1 (delcountp, child, false, only_children_p);
1763 var->children.clear ();
1765 /* if we were called to delete only the children we are done here. */
1766 if (only_children_p)
1767 return;
1769 /* Otherwise, add it to the list of deleted ones and proceed to do so. */
1770 /* If the name is empty, this is a temporary variable, that has not
1771 yet been installed, don't report it, it belongs to the caller... */
1772 if (!var->obj_name.empty ())
1774 *delcountp = *delcountp + 1;
1777 /* If this variable has a parent, remove it from its parent's list. */
1778 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1779 (as indicated by remove_from_parent_p) we don't bother doing an
1780 expensive list search to find the element to remove when we are
1781 discarding the list afterwards. */
1782 if ((remove_from_parent_p) && (var->parent != NULL))
1783 var->parent->children[var->index] = NULL;
1785 if (!var->obj_name.empty ())
1786 uninstall_variable (var);
1788 /* Free memory associated with this variable. */
1789 delete var;
1792 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1793 static bool
1794 install_variable (struct varobj *var)
1796 struct vlist *cv;
1797 struct vlist *newvl;
1798 const char *chp;
1799 unsigned int index = 0;
1800 unsigned int i = 1;
1802 for (chp = var->obj_name.c_str (); *chp; chp++)
1804 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1807 cv = *(varobj_table + index);
1808 while (cv != NULL && cv->var->obj_name != var->obj_name)
1809 cv = cv->next;
1811 if (cv != NULL)
1812 error (_("Duplicate variable object name"));
1814 /* Add varobj to hash table. */
1815 newvl = XNEW (struct vlist);
1816 newvl->next = *(varobj_table + index);
1817 newvl->var = var;
1818 *(varobj_table + index) = newvl;
1820 /* If root, add varobj to root list. */
1821 if (is_root_p (var))
1823 /* Add to list of root variables. */
1824 if (rootlist == NULL)
1825 var->root->next = NULL;
1826 else
1827 var->root->next = rootlist;
1828 rootlist = var->root;
1831 return true; /* OK */
1834 /* Uninstall the object VAR. */
1835 static void
1836 uninstall_variable (struct varobj *var)
1838 struct vlist *cv;
1839 struct vlist *prev;
1840 struct varobj_root *cr;
1841 struct varobj_root *prer;
1842 const char *chp;
1843 unsigned int index = 0;
1844 unsigned int i = 1;
1846 /* Remove varobj from hash table. */
1847 for (chp = var->obj_name.c_str (); *chp; chp++)
1849 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1852 cv = *(varobj_table + index);
1853 prev = NULL;
1854 while (cv != NULL && cv->var->obj_name != var->obj_name)
1856 prev = cv;
1857 cv = cv->next;
1860 if (varobjdebug)
1861 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name.c_str ());
1863 if (cv == NULL)
1865 warning
1866 ("Assertion failed: Could not find variable object \"%s\" to delete",
1867 var->obj_name.c_str ());
1868 return;
1871 if (prev == NULL)
1872 *(varobj_table + index) = cv->next;
1873 else
1874 prev->next = cv->next;
1876 xfree (cv);
1878 /* If root, remove varobj from root list. */
1879 if (is_root_p (var))
1881 /* Remove from list of root variables. */
1882 if (rootlist == var->root)
1883 rootlist = var->root->next;
1884 else
1886 prer = NULL;
1887 cr = rootlist;
1888 while ((cr != NULL) && (cr->rootvar != var))
1890 prer = cr;
1891 cr = cr->next;
1893 if (cr == NULL)
1895 warning (_("Assertion failed: Could not find "
1896 "varobj \"%s\" in root list"),
1897 var->obj_name.c_str ());
1898 return;
1900 if (prer == NULL)
1901 rootlist = NULL;
1902 else
1903 prer->next = cr->next;
1909 /* Create and install a child of the parent of the given name.
1911 The created VAROBJ takes ownership of the allocated NAME. */
1913 static struct varobj *
1914 create_child (struct varobj *parent, int index, std::string &name)
1916 struct varobj_item item;
1918 std::swap (item.name, name);
1919 item.value = value_of_child (parent, index);
1921 return create_child_with_value (parent, index, &item);
1924 static struct varobj *
1925 create_child_with_value (struct varobj *parent, int index,
1926 struct varobj_item *item)
1928 varobj *child = new varobj (parent->root);
1930 /* NAME is allocated by caller. */
1931 std::swap (child->name, item->name);
1932 child->index = index;
1933 child->parent = parent;
1935 if (varobj_is_anonymous_child (child))
1936 child->obj_name = string_printf ("%s.%d_anonymous",
1937 parent->obj_name.c_str (), index);
1938 else
1939 child->obj_name = string_printf ("%s.%s",
1940 parent->obj_name.c_str (),
1941 child->name.c_str ());
1943 install_variable (child);
1945 /* Compute the type of the child. Must do this before
1946 calling install_new_value. */
1947 if (item->value != NULL)
1948 /* If the child had no evaluation errors, var->value
1949 will be non-NULL and contain a valid type. */
1950 child->type = value_actual_type (item->value, 0, NULL);
1951 else
1952 /* Otherwise, we must compute the type. */
1953 child->type = (*child->root->lang_ops->type_of_child) (child->parent,
1954 child->index);
1955 install_new_value (child, item->value, 1);
1957 return child;
1962 * Miscellaneous utility functions.
1965 /* Allocate memory and initialize a new variable. */
1966 varobj::varobj (varobj_root *root_)
1967 : root (root_), dynamic (new varobj_dynamic)
1971 /* Free any allocated memory associated with VAR. */
1973 varobj::~varobj ()
1975 varobj *var = this;
1977 #if HAVE_PYTHON
1978 if (var->dynamic->pretty_printer != NULL)
1980 gdbpy_enter_varobj enter_py (var);
1982 Py_XDECREF (var->dynamic->constructor);
1983 Py_XDECREF (var->dynamic->pretty_printer);
1985 #endif
1987 varobj_iter_delete (var->dynamic->child_iter);
1988 varobj_clear_saved_item (var->dynamic);
1990 if (is_root_p (var))
1991 delete var->root;
1993 delete var->dynamic;
1996 /* Return the type of the value that's stored in VAR,
1997 or that would have being stored there if the
1998 value were accessible.
2000 This differs from VAR->type in that VAR->type is always
2001 the true type of the expression in the source language.
2002 The return value of this function is the type we're
2003 actually storing in varobj, and using for displaying
2004 the values and for comparing previous and new values.
2006 For example, top-level references are always stripped. */
2007 struct type *
2008 varobj_get_value_type (const struct varobj *var)
2010 struct type *type;
2012 if (var->value != nullptr)
2013 type = value_type (var->value.get ());
2014 else
2015 type = var->type;
2017 type = check_typedef (type);
2019 if (TYPE_IS_REFERENCE (type))
2020 type = get_target_type (type);
2022 type = check_typedef (type);
2024 return type;
2027 /* What is the default display for this variable? We assume that
2028 everything is "natural". Any exceptions? */
2029 static enum varobj_display_formats
2030 variable_default_display (struct varobj *var)
2032 return FORMAT_NATURAL;
2036 * Language-dependencies
2039 /* Common entry points */
2041 /* Return the number of children for a given variable.
2042 The result of this function is defined by the language
2043 implementation. The number of children returned by this function
2044 is the number of children that the user will see in the variable
2045 display. */
2046 static int
2047 number_of_children (const struct varobj *var)
2049 return (*var->root->lang_ops->number_of_children) (var);
2052 /* What is the expression for the root varobj VAR? */
2054 static std::string
2055 name_of_variable (const struct varobj *var)
2057 return (*var->root->lang_ops->name_of_variable) (var);
2060 /* What is the name of the INDEX'th child of VAR? */
2062 static std::string
2063 name_of_child (struct varobj *var, int index)
2065 return (*var->root->lang_ops->name_of_child) (var, index);
2068 /* If frame associated with VAR can be found, switch
2069 to it and return true. Otherwise, return false. */
2071 static bool
2072 check_scope (const struct varobj *var)
2074 struct frame_info *fi;
2075 bool scope;
2077 fi = frame_find_by_id (var->root->frame);
2078 scope = fi != NULL;
2080 if (fi)
2082 CORE_ADDR pc = get_frame_pc (fi);
2084 if (pc < BLOCK_START (var->root->valid_block) ||
2085 pc >= BLOCK_END (var->root->valid_block))
2086 scope = false;
2087 else
2088 select_frame (fi);
2090 return scope;
2093 /* Helper function to value_of_root. */
2095 static struct value *
2096 value_of_root_1 (struct varobj **var_handle)
2098 struct value *new_val = NULL;
2099 struct varobj *var = *var_handle;
2100 bool within_scope = false;
2102 /* Only root variables can be updated... */
2103 if (!is_root_p (var))
2104 /* Not a root var. */
2105 return NULL;
2107 scoped_restore_current_thread restore_thread;
2109 /* Determine whether the variable is still around. */
2110 if (var->root->valid_block == NULL || var->root->floating)
2111 within_scope = true;
2112 else if (var->root->thread_id == 0)
2114 /* The program was single-threaded when the variable object was
2115 created. Technically, it's possible that the program became
2116 multi-threaded since then, but we don't support such
2117 scenario yet. */
2118 within_scope = check_scope (var);
2120 else
2122 thread_info *thread = find_thread_global_id (var->root->thread_id);
2124 if (thread != NULL)
2126 switch_to_thread (thread);
2127 within_scope = check_scope (var);
2131 if (within_scope)
2134 /* We need to catch errors here, because if evaluate
2135 expression fails we want to just return NULL. */
2138 new_val = evaluate_expression (var->root->exp.get ());
2140 catch (const gdb_exception_error &except)
2145 return new_val;
2148 /* What is the ``struct value *'' of the root variable VAR?
2149 For floating variable object, evaluation can get us a value
2150 of different type from what is stored in varobj already. In
2151 that case:
2152 - *type_changed will be set to 1
2153 - old varobj will be freed, and new one will be
2154 created, with the same name.
2155 - *var_handle will be set to the new varobj
2156 Otherwise, *type_changed will be set to 0. */
2157 static struct value *
2158 value_of_root (struct varobj **var_handle, bool *type_changed)
2160 struct varobj *var;
2162 if (var_handle == NULL)
2163 return NULL;
2165 var = *var_handle;
2167 /* This should really be an exception, since this should
2168 only get called with a root variable. */
2170 if (!is_root_p (var))
2171 return NULL;
2173 if (var->root->floating)
2175 struct varobj *tmp_var;
2177 tmp_var = varobj_create (NULL, var->name.c_str (), (CORE_ADDR) 0,
2178 USE_SELECTED_FRAME);
2179 if (tmp_var == NULL)
2181 return NULL;
2183 std::string old_type = varobj_get_type (var);
2184 std::string new_type = varobj_get_type (tmp_var);
2185 if (old_type == new_type)
2187 /* The expression presently stored inside var->root->exp
2188 remembers the locations of local variables relatively to
2189 the frame where the expression was created (in DWARF location
2190 button, for example). Naturally, those locations are not
2191 correct in other frames, so update the expression. */
2193 std::swap (var->root->exp, tmp_var->root->exp);
2195 varobj_delete (tmp_var, 0);
2196 *type_changed = 0;
2198 else
2200 tmp_var->obj_name = var->obj_name;
2201 tmp_var->from = var->from;
2202 tmp_var->to = var->to;
2203 varobj_delete (var, 0);
2205 install_variable (tmp_var);
2206 *var_handle = tmp_var;
2207 var = *var_handle;
2208 *type_changed = true;
2211 else
2213 *type_changed = 0;
2217 struct value *value;
2219 value = value_of_root_1 (var_handle);
2220 if (var->value == NULL || value == NULL)
2222 /* For root varobj-s, a NULL value indicates a scoping issue.
2223 So, nothing to do in terms of checking for mutations. */
2225 else if (varobj_value_has_mutated (var, value, value_type (value)))
2227 /* The type has mutated, so the children are no longer valid.
2228 Just delete them, and tell our caller that the type has
2229 changed. */
2230 varobj_delete (var, 1 /* only_children */);
2231 var->num_children = -1;
2232 var->to = -1;
2233 var->from = -1;
2234 *type_changed = true;
2236 return value;
2240 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
2241 static struct value *
2242 value_of_child (const struct varobj *parent, int index)
2244 struct value *value;
2246 value = (*parent->root->lang_ops->value_of_child) (parent, index);
2248 return value;
2251 /* GDB already has a command called "value_of_variable". Sigh. */
2252 static std::string
2253 my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
2255 if (var->root->is_valid)
2257 if (var->dynamic->pretty_printer != NULL)
2258 return varobj_value_get_print_value (var->value.get (), var->format,
2259 var);
2260 return (*var->root->lang_ops->value_of_variable) (var, format);
2262 else
2263 return std::string ();
2266 void
2267 varobj_formatted_print_options (struct value_print_options *opts,
2268 enum varobj_display_formats format)
2270 get_formatted_print_options (opts, format_code[(int) format]);
2271 opts->deref_ref = 0;
2272 opts->raw = !pretty_printing;
2275 std::string
2276 varobj_value_get_print_value (struct value *value,
2277 enum varobj_display_formats format,
2278 const struct varobj *var)
2280 struct value_print_options opts;
2281 struct type *type = NULL;
2282 long len = 0;
2283 gdb::unique_xmalloc_ptr<char> encoding;
2284 /* Initialize it just to avoid a GCC false warning. */
2285 CORE_ADDR str_addr = 0;
2286 bool string_print = false;
2288 if (value == NULL)
2289 return std::string ();
2291 string_file stb;
2292 std::string thevalue;
2294 #if HAVE_PYTHON
2295 if (gdb_python_initialized)
2297 PyObject *value_formatter = var->dynamic->pretty_printer;
2299 gdbpy_enter_varobj enter_py (var);
2301 if (value_formatter)
2303 /* First check to see if we have any children at all. If so,
2304 we simply return {...}. */
2305 if (dynamic_varobj_has_child_method (var))
2306 return "{...}";
2308 if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
2310 struct value *replacement;
2312 gdbpy_ref<> output = apply_varobj_pretty_printer (value_formatter,
2313 &replacement,
2314 &stb);
2316 /* If we have string like output ... */
2317 if (output != NULL)
2319 /* If this is a lazy string, extract it. For lazy
2320 strings we always print as a string, so set
2321 string_print. */
2322 if (gdbpy_is_lazy_string (output.get ()))
2324 gdbpy_extract_lazy_string (output.get (), &str_addr,
2325 &type, &len, &encoding);
2326 string_print = true;
2328 else
2330 /* If it is a regular (non-lazy) string, extract
2331 it and copy the contents into THEVALUE. If the
2332 hint says to print it as a string, set
2333 string_print. Otherwise just return the extracted
2334 string as a value. */
2336 gdb::unique_xmalloc_ptr<char> s
2337 = python_string_to_target_string (output.get ());
2339 if (s)
2341 struct gdbarch *gdbarch;
2343 gdb::unique_xmalloc_ptr<char> hint
2344 = gdbpy_get_display_hint (value_formatter);
2345 if (hint)
2347 if (!strcmp (hint.get (), "string"))
2348 string_print = true;
2351 thevalue = std::string (s.get ());
2352 len = thevalue.size ();
2353 gdbarch = get_type_arch (value_type (value));
2354 type = builtin_type (gdbarch)->builtin_char;
2356 if (!string_print)
2357 return thevalue;
2359 else
2360 gdbpy_print_stack ();
2363 /* If the printer returned a replacement value, set VALUE
2364 to REPLACEMENT. If there is not a replacement value,
2365 just use the value passed to this function. */
2366 if (replacement)
2367 value = replacement;
2371 #endif
2373 varobj_formatted_print_options (&opts, format);
2375 /* If the THEVALUE has contents, it is a regular string. */
2376 if (!thevalue.empty ())
2377 LA_PRINT_STRING (&stb, type, (gdb_byte *) thevalue.c_str (),
2378 len, encoding.get (), 0, &opts);
2379 else if (string_print)
2380 /* Otherwise, if string_print is set, and it is not a regular
2381 string, it is a lazy string. */
2382 val_print_string (type, encoding.get (), str_addr, len, &stb, &opts);
2383 else
2384 /* All other cases. */
2385 common_val_print (value, &stb, 0, &opts, current_language);
2387 return std::move (stb.string ());
2390 bool
2391 varobj_editable_p (const struct varobj *var)
2393 struct type *type;
2395 if (!(var->root->is_valid && var->value != nullptr
2396 && VALUE_LVAL (var->value.get ())))
2397 return false;
2399 type = varobj_get_value_type (var);
2401 switch (type->code ())
2403 case TYPE_CODE_STRUCT:
2404 case TYPE_CODE_UNION:
2405 case TYPE_CODE_ARRAY:
2406 case TYPE_CODE_FUNC:
2407 case TYPE_CODE_METHOD:
2408 return false;
2409 break;
2411 default:
2412 return true;
2413 break;
2417 /* Call VAR's value_is_changeable_p language-specific callback. */
2419 bool
2420 varobj_value_is_changeable_p (const struct varobj *var)
2422 return var->root->lang_ops->value_is_changeable_p (var);
2425 /* Return true if that varobj is floating, that is is always evaluated in the
2426 selected frame, and not bound to thread/frame. Such variable objects
2427 are created using '@' as frame specifier to -var-create. */
2428 bool
2429 varobj_floating_p (const struct varobj *var)
2431 return var->root->floating;
2434 /* Implement the "value_is_changeable_p" varobj callback for most
2435 languages. */
2437 bool
2438 varobj_default_value_is_changeable_p (const struct varobj *var)
2440 bool r;
2441 struct type *type;
2443 if (CPLUS_FAKE_CHILD (var))
2444 return false;
2446 type = varobj_get_value_type (var);
2448 switch (type->code ())
2450 case TYPE_CODE_STRUCT:
2451 case TYPE_CODE_UNION:
2452 case TYPE_CODE_ARRAY:
2453 r = false;
2454 break;
2456 default:
2457 r = true;
2460 return r;
2463 /* Iterate all the existing _root_ VAROBJs and call the FUNC callback for them
2464 with an arbitrary caller supplied DATA pointer. */
2466 void
2467 all_root_varobjs (void (*func) (struct varobj *var, void *data), void *data)
2469 struct varobj_root *var_root, *var_root_next;
2471 /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
2473 for (var_root = rootlist; var_root != NULL; var_root = var_root_next)
2475 var_root_next = var_root->next;
2477 (*func) (var_root->rootvar, data);
2481 /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
2482 defined on globals. It is a helper for varobj_invalidate.
2484 This function is called after changing the symbol file, in this case the
2485 pointers to "struct type" stored by the varobj are no longer valid. All
2486 varobj must be either re-evaluated, or marked as invalid here. */
2488 static void
2489 varobj_invalidate_iter (struct varobj *var, void *unused)
2491 /* global and floating var must be re-evaluated. */
2492 if (var->root->floating || var->root->valid_block == NULL)
2494 struct varobj *tmp_var;
2496 /* Try to create a varobj with same expression. If we succeed
2497 replace the old varobj, otherwise invalidate it. */
2498 tmp_var = varobj_create (NULL, var->name.c_str (), (CORE_ADDR) 0,
2499 USE_CURRENT_FRAME);
2500 if (tmp_var != NULL)
2502 tmp_var->obj_name = var->obj_name;
2503 varobj_delete (var, 0);
2504 install_variable (tmp_var);
2506 else
2507 var->root->is_valid = false;
2509 else /* locals must be invalidated. */
2510 var->root->is_valid = false;
2513 /* Invalidate the varobjs that are tied to locals and re-create the ones that
2514 are defined on globals.
2515 Invalidated varobjs will be always printed in_scope="invalid". */
2517 void
2518 varobj_invalidate (void)
2520 all_root_varobjs (varobj_invalidate_iter, NULL);
2523 void _initialize_varobj ();
2524 void
2525 _initialize_varobj ()
2527 varobj_table = XCNEWVEC (struct vlist *, VAROBJ_TABLE_SIZE);
2529 add_setshow_zuinteger_cmd ("varobj", class_maintenance,
2530 &varobjdebug,
2531 _("Set varobj debugging."),
2532 _("Show varobj debugging."),
2533 _("When non-zero, varobj debugging is enabled."),
2534 NULL, show_varobjdebug,
2535 &setdebuglist, &showdebuglist);