]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/varobj.c
2.41 Release sources
[thirdparty/binutils-gdb.git] / gdb / varobj.c
1 /* Implementation of the GDB variable objects API.
2
3 Copyright (C) 1999-2023 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>. */
17
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 "gdbsupport/gdb_regex.h"
27
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"
34 #include <algorithm>
35 #include "observable.h"
36
37 #if HAVE_PYTHON
38 #include "python/python.h"
39 #include "python/python-internal.h"
40 #else
41 typedef int PyObject;
42 #endif
43
44 /* See varobj.h. */
45
46 unsigned int varobjdebug = 0;
47 static void
48 show_varobjdebug (struct ui_file *file, int from_tty,
49 struct cmd_list_element *c, const char *value)
50 {
51 gdb_printf (file, _("Varobj debugging is %s.\n"), value);
52 }
53
54 /* String representations of gdb's format codes. */
55 const char *varobj_format_string[] =
56 { "natural", "binary", "decimal", "hexadecimal", "octal", "zero-hexadecimal" };
57
58 /* True if we want to allow Python-based pretty-printing. */
59 static bool pretty_printing = false;
60
61 void
62 varobj_enable_pretty_printing (void)
63 {
64 pretty_printing = true;
65 }
66
67 /* Data structures */
68
69 /* Every root variable has one of these structures saved in its
70 varobj. */
71 struct varobj_root
72 {
73 /* The expression for this parent. */
74 expression_up exp;
75
76 /* Cached arch from exp, for use in case exp gets invalidated. */
77 struct gdbarch *gdbarch = nullptr;
78
79 /* Cached language from exp, for use in case exp gets invalidated. */
80 const struct language_defn *language_defn = nullptr;
81
82 /* Block for which this expression is valid. */
83 const struct block *valid_block = NULL;
84
85 /* The frame for this expression. This field is set iff valid_block is
86 not NULL. */
87 struct frame_id frame = null_frame_id;
88
89 /* The global thread ID that this varobj_root belongs to. This field
90 is only valid if valid_block is not NULL.
91 When not 0, indicates which thread 'frame' belongs to.
92 When 0, indicates that the thread list was empty when the varobj_root
93 was created. */
94 int thread_id = 0;
95
96 /* If true, the -var-update always recomputes the value in the
97 current thread and frame. Otherwise, variable object is
98 always updated in the specific scope/thread/frame. */
99 bool floating = false;
100
101 /* Flag that indicates validity: set to false when this varobj_root refers
102 to symbols that do not exist anymore. */
103 bool is_valid = true;
104
105 /* Set to true if the varobj was created as tracking a global. */
106 bool global = false;
107
108 /* Language-related operations for this variable and its
109 children. */
110 const struct lang_varobj_ops *lang_ops = NULL;
111
112 /* The varobj for this root node. */
113 struct varobj *rootvar = NULL;
114 };
115
116 /* Dynamic part of varobj. */
117
118 struct varobj_dynamic
119 {
120 /* Whether the children of this varobj were requested. This field is
121 used to decide if dynamic varobj should recompute their children.
122 In the event that the frontend never asked for the children, we
123 can avoid that. */
124 bool children_requested = false;
125
126 /* The pretty-printer constructor. If NULL, then the default
127 pretty-printer will be looked up. If None, then no
128 pretty-printer will be installed. */
129 PyObject *constructor = NULL;
130
131 /* The pretty-printer that has been constructed. If NULL, then a
132 new printer object is needed, and one will be constructed. */
133 PyObject *pretty_printer = NULL;
134
135 /* The iterator returned by the printer's 'children' method, or NULL
136 if not available. */
137 std::unique_ptr<varobj_iter> child_iter;
138
139 /* We request one extra item from the iterator, so that we can
140 report to the caller whether there are more items than we have
141 already reported. However, we don't want to install this value
142 when we read it, because that will mess up future updates. So,
143 we stash it here instead. */
144 std::unique_ptr<varobj_item> saved_item;
145 };
146
147 /* Private function prototypes */
148
149 /* Helper functions for the above subcommands. */
150
151 static int delete_variable (struct varobj *, bool);
152
153 static void delete_variable_1 (int *, struct varobj *, bool, bool);
154
155 static void install_variable (struct varobj *);
156
157 static void uninstall_variable (struct varobj *);
158
159 static struct varobj *create_child (struct varobj *, int, std::string &);
160
161 static struct varobj *
162 create_child_with_value (struct varobj *parent, int index,
163 struct varobj_item *item);
164
165 /* Utility routines */
166
167 static enum varobj_display_formats variable_default_display (struct varobj *);
168
169 static bool update_type_if_necessary (struct varobj *var,
170 struct value *new_value);
171
172 static bool install_new_value (struct varobj *var, struct value *value,
173 bool initial);
174
175 /* Language-specific routines. */
176
177 static int number_of_children (const struct varobj *);
178
179 static std::string name_of_variable (const struct varobj *);
180
181 static std::string name_of_child (struct varobj *, int);
182
183 static struct value *value_of_root (struct varobj **var_handle, bool *);
184
185 static struct value *value_of_child (const struct varobj *parent, int index);
186
187 static std::string my_value_of_variable (struct varobj *var,
188 enum varobj_display_formats format);
189
190 static bool is_root_p (const struct varobj *var);
191
192 static struct varobj *varobj_add_child (struct varobj *var,
193 struct varobj_item *item);
194
195 /* Private data */
196
197 /* Mappings of varobj_display_formats enums to gdb's format codes. */
198 static int format_code[] = { 0, 't', 'd', 'x', 'o', 'z' };
199
200 /* List of root variable objects. */
201 static std::list<struct varobj_root *> rootlist;
202
203 /* Pointer to the varobj hash table (built at run time). */
204 static htab_t varobj_table;
205
206 \f
207
208 /* API Implementation */
209 static bool
210 is_root_p (const struct varobj *var)
211 {
212 return (var->root->rootvar == var);
213 }
214
215 #ifdef HAVE_PYTHON
216
217 /* See python-internal.h. */
218 gdbpy_enter_varobj::gdbpy_enter_varobj (const struct varobj *var)
219 : gdbpy_enter (var->root->gdbarch, var->root->language_defn)
220 {
221 }
222
223 #endif
224
225 /* Return the full FRAME which corresponds to the given CORE_ADDR
226 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
227
228 static frame_info_ptr
229 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
230 {
231 frame_info_ptr frame = NULL;
232
233 if (frame_addr == (CORE_ADDR) 0)
234 return NULL;
235
236 for (frame = get_current_frame ();
237 frame != NULL;
238 frame = get_prev_frame (frame))
239 {
240 /* The CORE_ADDR we get as argument was parsed from a string GDB
241 output as $fp. This output got truncated to gdbarch_addr_bit.
242 Truncate the frame base address in the same manner before
243 comparing it against our argument. */
244 CORE_ADDR frame_base = get_frame_base_address (frame);
245 int addr_bit = gdbarch_addr_bit (get_frame_arch (frame));
246
247 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
248 frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1;
249
250 if (frame_base == frame_addr)
251 return frame;
252 }
253
254 return NULL;
255 }
256
257 /* Creates a varobj (not its children). */
258
259 struct varobj *
260 varobj_create (const char *objname,
261 const char *expression, CORE_ADDR frame, enum varobj_type type)
262 {
263 /* Fill out a varobj structure for the (root) variable being constructed. */
264 std::unique_ptr<varobj> var (new varobj (new varobj_root));
265
266 if (expression != NULL)
267 {
268 frame_info_ptr fi;
269 struct frame_id old_id = null_frame_id;
270 const struct block *block;
271 const char *p;
272 struct value *value = NULL;
273 CORE_ADDR pc;
274
275 /* Parse and evaluate the expression, filling in as much of the
276 variable's data as possible. */
277
278 if (has_stack_frames ())
279 {
280 /* Allow creator to specify context of variable. */
281 if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
282 fi = get_selected_frame (NULL);
283 else
284 /* FIXME: cagney/2002-11-23: This code should be doing a
285 lookup using the frame ID and not just the frame's
286 ``address''. This, of course, means an interface
287 change. However, with out that interface change ISAs,
288 such as the ia64 with its two stacks, won't work.
289 Similar goes for the case where there is a frameless
290 function. */
291 fi = find_frame_addr_in_frame_chain (frame);
292 }
293 else
294 fi = NULL;
295
296 if (type == USE_SELECTED_FRAME)
297 var->root->floating = true;
298
299 pc = 0;
300 block = NULL;
301 if (fi != NULL)
302 {
303 block = get_frame_block (fi, 0);
304 pc = get_frame_pc (fi);
305 }
306
307 p = expression;
308
309 innermost_block_tracker tracker (INNERMOST_BLOCK_FOR_SYMBOLS
310 | INNERMOST_BLOCK_FOR_REGISTERS);
311 /* Wrap the call to parse expression, so we can
312 return a sensible error. */
313 try
314 {
315 var->root->exp = parse_exp_1 (&p, pc, block, 0, &tracker);
316
317 /* Cache gdbarch and language_defn as they might be used even
318 after var is invalidated and var->root->exp cleared. */
319 var->root->gdbarch = var->root->exp->gdbarch;
320 var->root->language_defn = var->root->exp->language_defn;
321 }
322
323 catch (const gdb_exception_error &except)
324 {
325 return NULL;
326 }
327
328 /* Don't allow variables to be created for types. */
329 enum exp_opcode opcode = var->root->exp->first_opcode ();
330 if (opcode == OP_TYPE
331 || opcode == OP_TYPEOF
332 || opcode == OP_DECLTYPE)
333 {
334 gdb_printf (gdb_stderr, "Attempt to use a type name"
335 " as an expression.\n");
336 return NULL;
337 }
338
339 var->format = variable_default_display (var.get ());
340 var->root->valid_block =
341 var->root->floating ? NULL : tracker.block ();
342 var->root->global
343 = var->root->floating ? false : var->root->valid_block == nullptr;
344 var->name = expression;
345 /* For a root var, the name and the expr are the same. */
346 var->path_expr = expression;
347
348 /* When the frame is different from the current frame,
349 we must select the appropriate frame before parsing
350 the expression, otherwise the value will not be current.
351 Since select_frame is so benign, just call it for all cases. */
352 if (var->root->valid_block)
353 {
354 /* User could specify explicit FRAME-ADDR which was not found but
355 EXPRESSION is frame specific and we would not be able to evaluate
356 it correctly next time. With VALID_BLOCK set we must also set
357 FRAME and THREAD_ID. */
358 if (fi == NULL)
359 error (_("Failed to find the specified frame"));
360
361 var->root->frame = get_frame_id (fi);
362 var->root->thread_id = inferior_thread ()->global_num;
363 old_id = get_frame_id (get_selected_frame (NULL));
364 select_frame (fi);
365 }
366
367 /* We definitely need to catch errors here. If evaluation of
368 the expression succeeds, we got the value we wanted. But if
369 it fails, we still go on with a call to evaluate_type(). */
370 try
371 {
372 value = var->root->exp->evaluate ();
373 }
374 catch (const gdb_exception_error &except)
375 {
376 /* Error getting the value. Try to at least get the
377 right type. */
378 struct value *type_only_value = var->root->exp->evaluate_type ();
379
380 var->type = type_only_value->type ();
381 }
382
383 if (value != NULL)
384 {
385 int real_type_found = 0;
386
387 var->type = value_actual_type (value, 0, &real_type_found);
388 if (real_type_found)
389 value = value_cast (var->type, value);
390 }
391
392 /* Set language info */
393 var->root->lang_ops = var->root->exp->language_defn->varobj_ops ();
394
395 install_new_value (var.get (), value, 1 /* Initial assignment */);
396
397 /* Set ourselves as our root. */
398 var->root->rootvar = var.get ();
399
400 /* Reset the selected frame. */
401 if (frame_id_p (old_id))
402 select_frame (frame_find_by_id (old_id));
403 }
404
405 /* If the variable object name is null, that means this
406 is a temporary variable, so don't install it. */
407
408 if ((var != NULL) && (objname != NULL))
409 {
410 var->obj_name = objname;
411 install_variable (var.get ());
412 }
413
414 return var.release ();
415 }
416
417 /* Generates an unique name that can be used for a varobj. */
418
419 std::string
420 varobj_gen_name (void)
421 {
422 static int id = 0;
423
424 /* Generate a name for this object. */
425 id++;
426 return string_printf ("var%d", id);
427 }
428
429 /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
430 error if OBJNAME cannot be found. */
431
432 struct varobj *
433 varobj_get_handle (const char *objname)
434 {
435 varobj *var = (varobj *) htab_find_with_hash (varobj_table, objname,
436 htab_hash_string (objname));
437
438 if (var == NULL)
439 error (_("Variable object not found"));
440
441 return var;
442 }
443
444 /* Given the handle, return the name of the object. */
445
446 const char *
447 varobj_get_objname (const struct varobj *var)
448 {
449 return var->obj_name.c_str ();
450 }
451
452 /* Given the handle, return the expression represented by the
453 object. */
454
455 std::string
456 varobj_get_expression (const struct varobj *var)
457 {
458 return name_of_variable (var);
459 }
460
461 /* See varobj.h. */
462
463 int
464 varobj_delete (struct varobj *var, bool only_children)
465 {
466 return delete_variable (var, only_children);
467 }
468
469 #if HAVE_PYTHON
470
471 /* Convenience function for varobj_set_visualizer. Instantiate a
472 pretty-printer for a given value. */
473 static PyObject *
474 instantiate_pretty_printer (PyObject *constructor, struct value *value)
475 {
476 gdbpy_ref<> val_obj (value_to_value_object (value));
477 if (val_obj == nullptr)
478 return NULL;
479
480 return PyObject_CallFunctionObjArgs (constructor, val_obj.get (), NULL);
481 }
482
483 #endif
484
485 /* Set/Get variable object display format. */
486
487 enum varobj_display_formats
488 varobj_set_display_format (struct varobj *var,
489 enum varobj_display_formats format)
490 {
491 switch (format)
492 {
493 case FORMAT_NATURAL:
494 case FORMAT_BINARY:
495 case FORMAT_DECIMAL:
496 case FORMAT_HEXADECIMAL:
497 case FORMAT_OCTAL:
498 case FORMAT_ZHEXADECIMAL:
499 var->format = format;
500 break;
501
502 default:
503 var->format = variable_default_display (var);
504 }
505
506 if (varobj_value_is_changeable_p (var)
507 && var->value != nullptr && !var->value->lazy ())
508 {
509 var->print_value = varobj_value_get_print_value (var->value.get (),
510 var->format, var);
511 }
512
513 return var->format;
514 }
515
516 enum varobj_display_formats
517 varobj_get_display_format (const struct varobj *var)
518 {
519 return var->format;
520 }
521
522 gdb::unique_xmalloc_ptr<char>
523 varobj_get_display_hint (const struct varobj *var)
524 {
525 gdb::unique_xmalloc_ptr<char> result;
526
527 #if HAVE_PYTHON
528 if (!gdb_python_initialized)
529 return NULL;
530
531 gdbpy_enter_varobj enter_py (var);
532
533 if (var->dynamic->pretty_printer != NULL)
534 result = gdbpy_get_display_hint (var->dynamic->pretty_printer);
535 #endif
536
537 return result;
538 }
539
540 /* Return true if the varobj has items after TO, false otherwise. */
541
542 bool
543 varobj_has_more (const struct varobj *var, int to)
544 {
545 if (var->children.size () > to)
546 return true;
547
548 return ((to == -1 || var->children.size () == to)
549 && (var->dynamic->saved_item != NULL));
550 }
551
552 /* If the variable object is bound to a specific thread, that
553 is its evaluation can always be done in context of a frame
554 inside that thread, returns GDB id of the thread -- which
555 is always positive. Otherwise, returns -1. */
556 int
557 varobj_get_thread_id (const struct varobj *var)
558 {
559 if (var->root->valid_block && var->root->thread_id > 0)
560 return var->root->thread_id;
561 else
562 return -1;
563 }
564
565 void
566 varobj_set_frozen (struct varobj *var, bool frozen)
567 {
568 /* When a variable is unfrozen, we don't fetch its value.
569 The 'not_fetched' flag remains set, so next -var-update
570 won't complain.
571
572 We don't fetch the value, because for structures the client
573 should do -var-update anyway. It would be bad to have different
574 client-size logic for structure and other types. */
575 var->frozen = frozen;
576 }
577
578 bool
579 varobj_get_frozen (const struct varobj *var)
580 {
581 return var->frozen;
582 }
583
584 /* A helper function that updates the contents of FROM and TO based on the
585 size of the vector CHILDREN. If the contents of either FROM or TO are
586 negative the entire range is used. */
587
588 void
589 varobj_restrict_range (const std::vector<varobj *> &children,
590 int *from, int *to)
591 {
592 int len = children.size ();
593
594 if (*from < 0 || *to < 0)
595 {
596 *from = 0;
597 *to = len;
598 }
599 else
600 {
601 if (*from > len)
602 *from = len;
603 if (*to > len)
604 *to = len;
605 if (*from > *to)
606 *from = *to;
607 }
608 }
609
610 /* A helper for update_dynamic_varobj_children that installs a new
611 child when needed. */
612
613 static void
614 install_dynamic_child (struct varobj *var,
615 std::vector<varobj *> *changed,
616 std::vector<varobj *> *type_changed,
617 std::vector<varobj *> *newobj,
618 std::vector<varobj *> *unchanged,
619 bool *cchanged,
620 int index,
621 struct varobj_item *item)
622 {
623 if (var->children.size () < index + 1)
624 {
625 /* There's no child yet. */
626 struct varobj *child = varobj_add_child (var, item);
627
628 if (newobj != NULL)
629 {
630 newobj->push_back (child);
631 *cchanged = true;
632 }
633 }
634 else
635 {
636 varobj *existing = var->children[index];
637 bool type_updated = update_type_if_necessary (existing,
638 item->value.get ());
639
640 if (type_updated)
641 {
642 if (type_changed != NULL)
643 type_changed->push_back (existing);
644 }
645 if (install_new_value (existing, item->value.get (), 0))
646 {
647 if (!type_updated && changed != NULL)
648 changed->push_back (existing);
649 }
650 else if (!type_updated && unchanged != NULL)
651 unchanged->push_back (existing);
652 }
653 }
654
655 /* A factory for creating dynamic varobj's iterators. Returns an
656 iterator object suitable for iterating over VAR's children. */
657
658 static std::unique_ptr<varobj_iter>
659 varobj_get_iterator (struct varobj *var)
660 {
661 #if HAVE_PYTHON
662 if (var->dynamic->pretty_printer)
663 {
664 value_print_options opts;
665 varobj_formatted_print_options (&opts, var->format);
666 return py_varobj_get_iterator (var, var->dynamic->pretty_printer, &opts);
667 }
668 #endif
669
670 gdb_assert_not_reached ("requested an iterator from a non-dynamic varobj");
671 }
672
673 static bool
674 update_dynamic_varobj_children (struct varobj *var,
675 std::vector<varobj *> *changed,
676 std::vector<varobj *> *type_changed,
677 std::vector<varobj *> *newobj,
678 std::vector<varobj *> *unchanged,
679 bool *cchanged,
680 bool update_children,
681 int from,
682 int to)
683 {
684 int i;
685
686 *cchanged = false;
687
688 if (update_children || var->dynamic->child_iter == NULL)
689 {
690 var->dynamic->child_iter = varobj_get_iterator (var);
691 var->dynamic->saved_item.reset (nullptr);
692
693 i = 0;
694
695 if (var->dynamic->child_iter == NULL)
696 return false;
697 }
698 else
699 i = var->children.size ();
700
701 /* We ask for one extra child, so that MI can report whether there
702 are more children. */
703 for (; to < 0 || i < to + 1; ++i)
704 {
705 std::unique_ptr<varobj_item> item;
706
707 /* See if there was a leftover from last time. */
708 if (var->dynamic->saved_item != NULL)
709 item = std::move (var->dynamic->saved_item);
710 else
711 item = var->dynamic->child_iter->next ();
712
713 if (item == NULL)
714 {
715 /* Iteration is done. Remove iterator from VAR. */
716 var->dynamic->child_iter.reset (nullptr);
717 break;
718 }
719 /* We don't want to push the extra child on any report list. */
720 if (to < 0 || i < to)
721 {
722 bool can_mention = from < 0 || i >= from;
723
724 install_dynamic_child (var, can_mention ? changed : NULL,
725 can_mention ? type_changed : NULL,
726 can_mention ? newobj : NULL,
727 can_mention ? unchanged : NULL,
728 can_mention ? cchanged : NULL, i,
729 item.get ());
730 }
731 else
732 {
733 var->dynamic->saved_item = std::move (item);
734
735 /* We want to truncate the child list just before this
736 element. */
737 break;
738 }
739 }
740
741 if (i < var->children.size ())
742 {
743 *cchanged = true;
744 for (int j = i; j < var->children.size (); ++j)
745 varobj_delete (var->children[j], 0);
746
747 var->children.resize (i);
748 }
749
750 /* If there are fewer children than requested, note that the list of
751 children changed. */
752 if (to >= 0 && var->children.size () < to)
753 *cchanged = true;
754
755 var->num_children = var->children.size ();
756
757 return true;
758 }
759
760 int
761 varobj_get_num_children (struct varobj *var)
762 {
763 if (var->num_children == -1)
764 {
765 if (varobj_is_dynamic_p (var))
766 {
767 bool dummy;
768
769 /* If we have a dynamic varobj, don't report -1 children.
770 So, try to fetch some children first. */
771 update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL, &dummy,
772 false, 0, 0);
773 }
774 else
775 var->num_children = number_of_children (var);
776 }
777
778 return var->num_children >= 0 ? var->num_children : 0;
779 }
780
781 /* Creates a list of the immediate children of a variable object;
782 the return code is the number of such children or -1 on error. */
783
784 const std::vector<varobj *> &
785 varobj_list_children (struct varobj *var, int *from, int *to)
786 {
787 var->dynamic->children_requested = true;
788
789 if (varobj_is_dynamic_p (var))
790 {
791 bool children_changed;
792
793 /* This, in theory, can result in the number of children changing without
794 frontend noticing. But well, calling -var-list-children on the same
795 varobj twice is not something a sane frontend would do. */
796 update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL,
797 &children_changed, false, 0, *to);
798 varobj_restrict_range (var->children, from, to);
799 return var->children;
800 }
801
802 if (var->num_children == -1)
803 var->num_children = number_of_children (var);
804
805 /* If that failed, give up. */
806 if (var->num_children == -1)
807 return var->children;
808
809 /* If we're called when the list of children is not yet initialized,
810 allocate enough elements in it. */
811 while (var->children.size () < var->num_children)
812 var->children.push_back (NULL);
813
814 for (int i = 0; i < var->num_children; i++)
815 {
816 if (var->children[i] == NULL)
817 {
818 /* Either it's the first call to varobj_list_children for
819 this variable object, and the child was never created,
820 or it was explicitly deleted by the client. */
821 std::string name = name_of_child (var, i);
822 var->children[i] = create_child (var, i, name);
823 }
824 }
825
826 varobj_restrict_range (var->children, from, to);
827 return var->children;
828 }
829
830 static struct varobj *
831 varobj_add_child (struct varobj *var, struct varobj_item *item)
832 {
833 varobj *v = create_child_with_value (var, var->children.size (), item);
834
835 var->children.push_back (v);
836
837 return v;
838 }
839
840 /* Obtain the type of an object Variable as a string similar to the one gdb
841 prints on the console. The caller is responsible for freeing the string.
842 */
843
844 std::string
845 varobj_get_type (struct varobj *var)
846 {
847 /* For the "fake" variables, do not return a type. (Its type is
848 NULL, too.)
849 Do not return a type for invalid variables as well. */
850 if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
851 return std::string ();
852
853 return type_to_string (var->type);
854 }
855
856 /* Obtain the type of an object variable. */
857
858 struct type *
859 varobj_get_gdb_type (const struct varobj *var)
860 {
861 return var->type;
862 }
863
864 /* Is VAR a path expression parent, i.e., can it be used to construct
865 a valid path expression? */
866
867 static bool
868 is_path_expr_parent (const struct varobj *var)
869 {
870 gdb_assert (var->root->lang_ops->is_path_expr_parent != NULL);
871 return var->root->lang_ops->is_path_expr_parent (var);
872 }
873
874 /* Is VAR a path expression parent, i.e., can it be used to construct
875 a valid path expression? By default we assume any VAR can be a path
876 parent. */
877
878 bool
879 varobj_default_is_path_expr_parent (const struct varobj *var)
880 {
881 return true;
882 }
883
884 /* Return the path expression parent for VAR. */
885
886 const struct varobj *
887 varobj_get_path_expr_parent (const struct varobj *var)
888 {
889 const struct varobj *parent = var;
890
891 while (!is_root_p (parent) && !is_path_expr_parent (parent))
892 parent = parent->parent;
893
894 /* Computation of full rooted expression for children of dynamic
895 varobjs is not supported. */
896 if (varobj_is_dynamic_p (parent))
897 error (_("Invalid variable object (child of a dynamic varobj)"));
898
899 return parent;
900 }
901
902 /* Return a pointer to the full rooted expression of varobj VAR.
903 If it has not been computed yet, compute it. */
904
905 const char *
906 varobj_get_path_expr (const struct varobj *var)
907 {
908 if (var->path_expr.empty ())
909 {
910 /* For root varobjs, we initialize path_expr
911 when creating varobj, so here it should be
912 child varobj. */
913 struct varobj *mutable_var = (struct varobj *) var;
914 gdb_assert (!is_root_p (var));
915
916 mutable_var->path_expr = (*var->root->lang_ops->path_expr_of_child) (var);
917 }
918
919 return var->path_expr.c_str ();
920 }
921
922 const struct language_defn *
923 varobj_get_language (const struct varobj *var)
924 {
925 return var->root->exp->language_defn;
926 }
927
928 int
929 varobj_get_attributes (const struct varobj *var)
930 {
931 int attributes = 0;
932
933 if (varobj_editable_p (var))
934 /* FIXME: define masks for attributes. */
935 attributes |= 0x00000001; /* Editable */
936
937 return attributes;
938 }
939
940 /* Return true if VAR is a dynamic varobj. */
941
942 bool
943 varobj_is_dynamic_p (const struct varobj *var)
944 {
945 return var->dynamic->pretty_printer != NULL;
946 }
947
948 std::string
949 varobj_get_formatted_value (struct varobj *var,
950 enum varobj_display_formats format)
951 {
952 return my_value_of_variable (var, format);
953 }
954
955 std::string
956 varobj_get_value (struct varobj *var)
957 {
958 return my_value_of_variable (var, var->format);
959 }
960
961 /* Set the value of an object variable (if it is editable) to the
962 value of the given expression. */
963 /* Note: Invokes functions that can call error(). */
964
965 bool
966 varobj_set_value (struct varobj *var, const char *expression)
967 {
968 struct value *val = NULL; /* Initialize to keep gcc happy. */
969 /* The argument "expression" contains the variable's new value.
970 We need to first construct a legal expression for this -- ugh! */
971 /* Does this cover all the bases? */
972 struct value *value = NULL; /* Initialize to keep gcc happy. */
973 const char *s = expression;
974
975 gdb_assert (varobj_editable_p (var));
976
977 /* ALWAYS reset to decimal temporarily. */
978 auto save_input_radix = make_scoped_restore (&input_radix, 10);
979 expression_up exp = parse_exp_1 (&s, 0, 0, 0);
980 try
981 {
982 value = exp->evaluate ();
983 }
984
985 catch (const gdb_exception_error &except)
986 {
987 /* We cannot proceed without a valid expression. */
988 return false;
989 }
990
991 /* All types that are editable must also be changeable. */
992 gdb_assert (varobj_value_is_changeable_p (var));
993
994 /* The value of a changeable variable object must not be lazy. */
995 gdb_assert (!var->value->lazy ());
996
997 /* Need to coerce the input. We want to check if the
998 value of the variable object will be different
999 after assignment, and the first thing value_assign
1000 does is coerce the input.
1001 For example, if we are assigning an array to a pointer variable we
1002 should compare the pointer with the array's address, not with the
1003 array's content. */
1004 value = coerce_array (value);
1005
1006 /* The new value may be lazy. value_assign, or
1007 rather value_contents, will take care of this. */
1008 try
1009 {
1010 val = value_assign (var->value.get (), value);
1011 }
1012
1013 catch (const gdb_exception_error &except)
1014 {
1015 return false;
1016 }
1017
1018 /* If the value has changed, record it, so that next -var-update can
1019 report this change. If a variable had a value of '1', we've set it
1020 to '333' and then set again to '1', when -var-update will report this
1021 variable as changed -- because the first assignment has set the
1022 'updated' flag. There's no need to optimize that, because return value
1023 of -var-update should be considered an approximation. */
1024 var->updated = install_new_value (var, val, false /* Compare values. */);
1025 return true;
1026 }
1027
1028 #if HAVE_PYTHON
1029
1030 /* A helper function to install a constructor function and visualizer
1031 in a varobj_dynamic. */
1032
1033 static void
1034 install_visualizer (struct varobj_dynamic *var, PyObject *constructor,
1035 PyObject *visualizer)
1036 {
1037 Py_XDECREF (var->constructor);
1038 var->constructor = constructor;
1039
1040 Py_XDECREF (var->pretty_printer);
1041 var->pretty_printer = visualizer;
1042
1043 var->child_iter.reset (nullptr);
1044 }
1045
1046 /* Install the default visualizer for VAR. */
1047
1048 static void
1049 install_default_visualizer (struct varobj *var)
1050 {
1051 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1052 if (CPLUS_FAKE_CHILD (var))
1053 return;
1054
1055 if (pretty_printing)
1056 {
1057 gdbpy_ref<> pretty_printer;
1058
1059 if (var->value != nullptr)
1060 {
1061 pretty_printer = gdbpy_get_varobj_pretty_printer (var->value.get ());
1062 if (pretty_printer == nullptr)
1063 {
1064 gdbpy_print_stack ();
1065 error (_("Cannot instantiate printer for default visualizer"));
1066 }
1067 }
1068
1069 if (pretty_printer == Py_None)
1070 pretty_printer.reset (nullptr);
1071
1072 install_visualizer (var->dynamic, NULL, pretty_printer.release ());
1073 }
1074 }
1075
1076 /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
1077 make a new object. */
1078
1079 static void
1080 construct_visualizer (struct varobj *var, PyObject *constructor)
1081 {
1082 PyObject *pretty_printer;
1083
1084 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1085 if (CPLUS_FAKE_CHILD (var))
1086 return;
1087
1088 Py_INCREF (constructor);
1089 if (constructor == Py_None)
1090 pretty_printer = NULL;
1091 else
1092 {
1093 pretty_printer = instantiate_pretty_printer (constructor,
1094 var->value.get ());
1095 if (! pretty_printer)
1096 {
1097 gdbpy_print_stack ();
1098 Py_DECREF (constructor);
1099 constructor = Py_None;
1100 Py_INCREF (constructor);
1101 }
1102
1103 if (pretty_printer == Py_None)
1104 {
1105 Py_DECREF (pretty_printer);
1106 pretty_printer = NULL;
1107 }
1108 }
1109
1110 install_visualizer (var->dynamic, constructor, pretty_printer);
1111 }
1112
1113 #endif /* HAVE_PYTHON */
1114
1115 /* A helper function for install_new_value. This creates and installs
1116 a visualizer for VAR, if appropriate. */
1117
1118 static void
1119 install_new_value_visualizer (struct varobj *var)
1120 {
1121 #if HAVE_PYTHON
1122 /* If the constructor is None, then we want the raw value. If VAR
1123 does not have a value, just skip this. */
1124 if (!gdb_python_initialized)
1125 return;
1126
1127 if (var->dynamic->constructor != Py_None && var->value != NULL)
1128 {
1129 gdbpy_enter_varobj enter_py (var);
1130
1131 if (var->dynamic->constructor == NULL)
1132 install_default_visualizer (var);
1133 else
1134 construct_visualizer (var, var->dynamic->constructor);
1135 }
1136 #else
1137 /* Do nothing. */
1138 #endif
1139 }
1140
1141 /* When using RTTI to determine variable type it may be changed in runtime when
1142 the variable value is changed. This function checks whether type of varobj
1143 VAR will change when a new value NEW_VALUE is assigned and if it is so
1144 updates the type of VAR. */
1145
1146 static bool
1147 update_type_if_necessary (struct varobj *var, struct value *new_value)
1148 {
1149 if (new_value)
1150 {
1151 struct value_print_options opts;
1152
1153 get_user_print_options (&opts);
1154 if (opts.objectprint)
1155 {
1156 struct type *new_type = value_actual_type (new_value, 0, 0);
1157 std::string new_type_str = type_to_string (new_type);
1158 std::string curr_type_str = varobj_get_type (var);
1159
1160 /* Did the type name change? */
1161 if (curr_type_str != new_type_str)
1162 {
1163 var->type = new_type;
1164
1165 /* This information may be not valid for a new type. */
1166 varobj_delete (var, 1);
1167 var->children.clear ();
1168 var->num_children = -1;
1169 return true;
1170 }
1171 }
1172 }
1173
1174 return false;
1175 }
1176
1177 /* Assign a new value to a variable object. If INITIAL is true,
1178 this is the first assignment after the variable object was just
1179 created, or changed type. In that case, just assign the value
1180 and return false.
1181 Otherwise, assign the new value, and return true if the value is
1182 different from the current one, false otherwise. The comparison is
1183 done on textual representation of value. Therefore, some types
1184 need not be compared. E.g. for structures the reported value is
1185 always "{...}", so no comparison is necessary here. If the old
1186 value was NULL and new one is not, or vice versa, we always return true.
1187
1188 The VALUE parameter should not be released -- the function will
1189 take care of releasing it when needed. */
1190 static bool
1191 install_new_value (struct varobj *var, struct value *value, bool initial)
1192 {
1193 bool changeable;
1194 bool need_to_fetch;
1195 bool changed = false;
1196 bool intentionally_not_fetched = false;
1197
1198 /* We need to know the varobj's type to decide if the value should
1199 be fetched or not. C++ fake children (public/protected/private)
1200 don't have a type. */
1201 gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
1202 changeable = varobj_value_is_changeable_p (var);
1203
1204 /* If the type has custom visualizer, we consider it to be always
1205 changeable. FIXME: need to make sure this behaviour will not
1206 mess up read-sensitive values. */
1207 if (var->dynamic->pretty_printer != NULL)
1208 changeable = true;
1209
1210 need_to_fetch = changeable;
1211
1212 /* We are not interested in the address of references, and given
1213 that in C++ a reference is not rebindable, it cannot
1214 meaningfully change. So, get hold of the real value. */
1215 if (value)
1216 value = coerce_ref (value);
1217
1218 if (var->type && var->type->code () == TYPE_CODE_UNION)
1219 /* For unions, we need to fetch the value implicitly because
1220 of implementation of union member fetch. When gdb
1221 creates a value for a field and the value of the enclosing
1222 structure is not lazy, it immediately copies the necessary
1223 bytes from the enclosing values. If the enclosing value is
1224 lazy, the call to value_fetch_lazy on the field will read
1225 the data from memory. For unions, that means we'll read the
1226 same memory more than once, which is not desirable. So
1227 fetch now. */
1228 need_to_fetch = true;
1229
1230 /* The new value might be lazy. If the type is changeable,
1231 that is we'll be comparing values of this type, fetch the
1232 value now. Otherwise, on the next update the old value
1233 will be lazy, which means we've lost that old value. */
1234 if (need_to_fetch && value && value->lazy ())
1235 {
1236 const struct varobj *parent = var->parent;
1237 bool frozen = var->frozen;
1238
1239 for (; !frozen && parent; parent = parent->parent)
1240 frozen |= parent->frozen;
1241
1242 if (frozen && initial)
1243 {
1244 /* For variables that are frozen, or are children of frozen
1245 variables, we don't do fetch on initial assignment.
1246 For non-initial assignment we do the fetch, since it means we're
1247 explicitly asked to compare the new value with the old one. */
1248 intentionally_not_fetched = true;
1249 }
1250 else
1251 {
1252
1253 try
1254 {
1255 value->fetch_lazy ();
1256 }
1257
1258 catch (const gdb_exception_error &except)
1259 {
1260 /* Set the value to NULL, so that for the next -var-update,
1261 we don't try to compare the new value with this value,
1262 that we couldn't even read. */
1263 value = NULL;
1264 }
1265 }
1266 }
1267
1268 /* Get a reference now, before possibly passing it to any Python
1269 code that might release it. */
1270 value_ref_ptr value_holder;
1271 if (value != NULL)
1272 value_holder = value_ref_ptr::new_reference (value);
1273
1274 /* Below, we'll be comparing string rendering of old and new
1275 values. Don't get string rendering if the value is
1276 lazy -- if it is, the code above has decided that the value
1277 should not be fetched. */
1278 std::string print_value;
1279 if (value != NULL && !value->lazy ()
1280 && var->dynamic->pretty_printer == NULL)
1281 print_value = varobj_value_get_print_value (value, var->format, var);
1282
1283 /* If the type is changeable, compare the old and the new values.
1284 If this is the initial assignment, we don't have any old value
1285 to compare with. */
1286 if (!initial && changeable)
1287 {
1288 /* If the value of the varobj was changed by -var-set-value,
1289 then the value in the varobj and in the target is the same.
1290 However, that value is different from the value that the
1291 varobj had after the previous -var-update. So need to the
1292 varobj as changed. */
1293 if (var->updated)
1294 changed = true;
1295 else if (var->dynamic->pretty_printer == NULL)
1296 {
1297 /* Try to compare the values. That requires that both
1298 values are non-lazy. */
1299 if (var->not_fetched && var->value->lazy ())
1300 {
1301 /* This is a frozen varobj and the value was never read.
1302 Presumably, UI shows some "never read" indicator.
1303 Now that we've fetched the real value, we need to report
1304 this varobj as changed so that UI can show the real
1305 value. */
1306 changed = true;
1307 }
1308 else if (var->value == NULL && value == NULL)
1309 /* Equal. */
1310 ;
1311 else if (var->value == NULL || value == NULL)
1312 {
1313 changed = true;
1314 }
1315 else
1316 {
1317 gdb_assert (!var->value->lazy ());
1318 gdb_assert (!value->lazy ());
1319
1320 gdb_assert (!var->print_value.empty () && !print_value.empty ());
1321 if (var->print_value != print_value)
1322 changed = true;
1323 }
1324 }
1325 }
1326
1327 if (!initial && !changeable)
1328 {
1329 /* For values that are not changeable, we don't compare the values.
1330 However, we want to notice if a value was not NULL and now is NULL,
1331 or vise versa, so that we report when top-level varobjs come in scope
1332 and leave the scope. */
1333 changed = (var->value != NULL) != (value != NULL);
1334 }
1335
1336 /* We must always keep the new value, since children depend on it. */
1337 var->value = value_holder;
1338 if (value && value->lazy () && intentionally_not_fetched)
1339 var->not_fetched = true;
1340 else
1341 var->not_fetched = false;
1342 var->updated = false;
1343
1344 install_new_value_visualizer (var);
1345
1346 /* If we installed a pretty-printer, re-compare the printed version
1347 to see if the variable changed. */
1348 if (var->dynamic->pretty_printer != NULL)
1349 {
1350 print_value = varobj_value_get_print_value (var->value.get (),
1351 var->format, var);
1352 if (var->print_value != print_value)
1353 changed = true;
1354 }
1355 var->print_value = print_value;
1356
1357 gdb_assert (var->value == nullptr || var->value->type ());
1358
1359 return changed;
1360 }
1361
1362 /* Return the requested range for a varobj. VAR is the varobj. FROM
1363 and TO are out parameters; *FROM and *TO will be set to the
1364 selected sub-range of VAR. If no range was selected using
1365 -var-set-update-range, then both will be -1. */
1366 void
1367 varobj_get_child_range (const struct varobj *var, int *from, int *to)
1368 {
1369 *from = var->from;
1370 *to = var->to;
1371 }
1372
1373 /* Set the selected sub-range of children of VAR to start at index
1374 FROM and end at index TO. If either FROM or TO is less than zero,
1375 this is interpreted as a request for all children. */
1376 void
1377 varobj_set_child_range (struct varobj *var, int from, int to)
1378 {
1379 var->from = from;
1380 var->to = to;
1381 }
1382
1383 void
1384 varobj_set_visualizer (struct varobj *var, const char *visualizer)
1385 {
1386 #if HAVE_PYTHON
1387 PyObject *mainmod;
1388
1389 if (!gdb_python_initialized)
1390 return;
1391
1392 gdbpy_enter_varobj enter_py (var);
1393
1394 mainmod = PyImport_AddModule ("__main__");
1395 gdbpy_ref<> globals
1396 = gdbpy_ref<>::new_reference (PyModule_GetDict (mainmod));
1397 gdbpy_ref<> constructor (PyRun_String (visualizer, Py_eval_input,
1398 globals.get (), globals.get ()));
1399
1400 if (constructor == NULL)
1401 {
1402 gdbpy_print_stack ();
1403 error (_("Could not evaluate visualizer expression: %s"), visualizer);
1404 }
1405
1406 construct_visualizer (var, constructor.get ());
1407
1408 /* If there are any children now, wipe them. */
1409 varobj_delete (var, 1 /* children only */);
1410 var->num_children = -1;
1411 #else
1412 error (_("Python support required"));
1413 #endif
1414 }
1415
1416 /* If NEW_VALUE is the new value of the given varobj (var), return
1417 true if var has mutated. In other words, if the type of
1418 the new value is different from the type of the varobj's old
1419 value.
1420
1421 NEW_VALUE may be NULL, if the varobj is now out of scope. */
1422
1423 static bool
1424 varobj_value_has_mutated (const struct varobj *var, struct value *new_value,
1425 struct type *new_type)
1426 {
1427 /* If we haven't previously computed the number of children in var,
1428 it does not matter from the front-end's perspective whether
1429 the type has mutated or not. For all intents and purposes,
1430 it has not mutated. */
1431 if (var->num_children < 0)
1432 return false;
1433
1434 if (var->root->lang_ops->value_has_mutated != NULL)
1435 {
1436 /* The varobj module, when installing new values, explicitly strips
1437 references, saying that we're not interested in those addresses.
1438 But detection of mutation happens before installing the new
1439 value, so our value may be a reference that we need to strip
1440 in order to remain consistent. */
1441 if (new_value != NULL)
1442 new_value = coerce_ref (new_value);
1443 return var->root->lang_ops->value_has_mutated (var, new_value, new_type);
1444 }
1445 else
1446 return false;
1447 }
1448
1449 /* Update the values for a variable and its children. This is a
1450 two-pronged attack. First, re-parse the value for the root's
1451 expression to see if it's changed. Then go all the way
1452 through its children, reconstructing them and noting if they've
1453 changed.
1454
1455 The IS_EXPLICIT parameter specifies if this call is result
1456 of MI request to update this specific variable, or
1457 result of implicit -var-update *. For implicit request, we don't
1458 update frozen variables.
1459
1460 NOTE: This function may delete the caller's varobj. If it
1461 returns TYPE_CHANGED, then it has done this and VARP will be modified
1462 to point to the new varobj. */
1463
1464 std::vector<varobj_update_result>
1465 varobj_update (struct varobj **varp, bool is_explicit)
1466 {
1467 bool type_changed = false;
1468 struct value *newobj;
1469 std::vector<varobj_update_result> stack;
1470 std::vector<varobj_update_result> result;
1471
1472 /* Frozen means frozen -- we don't check for any change in
1473 this varobj, including its going out of scope, or
1474 changing type. One use case for frozen varobjs is
1475 retaining previously evaluated expressions, and we don't
1476 want them to be reevaluated at all. */
1477 if (!is_explicit && (*varp)->frozen)
1478 return result;
1479
1480 if (!(*varp)->root->is_valid)
1481 {
1482 result.emplace_back (*varp, VAROBJ_INVALID);
1483 return result;
1484 }
1485
1486 if ((*varp)->root->rootvar == *varp)
1487 {
1488 varobj_update_result r (*varp);
1489
1490 /* Update the root variable. value_of_root can return NULL
1491 if the variable is no longer around, i.e. we stepped out of
1492 the frame in which a local existed. We are letting the
1493 value_of_root variable dispose of the varobj if the type
1494 has changed. */
1495 newobj = value_of_root (varp, &type_changed);
1496 if (update_type_if_necessary (*varp, newobj))
1497 type_changed = true;
1498 r.varobj = *varp;
1499 r.type_changed = type_changed;
1500 if (install_new_value ((*varp), newobj, type_changed))
1501 r.changed = true;
1502
1503 if (newobj == NULL)
1504 r.status = VAROBJ_NOT_IN_SCOPE;
1505 r.value_installed = true;
1506
1507 if (r.status == VAROBJ_NOT_IN_SCOPE)
1508 {
1509 if (r.type_changed || r.changed)
1510 result.push_back (std::move (r));
1511
1512 return result;
1513 }
1514
1515 stack.push_back (std::move (r));
1516 }
1517 else
1518 stack.emplace_back (*varp);
1519
1520 /* Walk through the children, reconstructing them all. */
1521 while (!stack.empty ())
1522 {
1523 varobj_update_result r = std::move (stack.back ());
1524 stack.pop_back ();
1525 struct varobj *v = r.varobj;
1526
1527 /* Update this variable, unless it's a root, which is already
1528 updated. */
1529 if (!r.value_installed)
1530 {
1531 struct type *new_type;
1532
1533 newobj = value_of_child (v->parent, v->index);
1534 if (update_type_if_necessary (v, newobj))
1535 r.type_changed = true;
1536 if (newobj)
1537 new_type = newobj->type ();
1538 else
1539 new_type = v->root->lang_ops->type_of_child (v->parent, v->index);
1540
1541 if (varobj_value_has_mutated (v, newobj, new_type))
1542 {
1543 /* The children are no longer valid; delete them now.
1544 Report the fact that its type changed as well. */
1545 varobj_delete (v, 1 /* only_children */);
1546 v->num_children = -1;
1547 v->to = -1;
1548 v->from = -1;
1549 v->type = new_type;
1550 r.type_changed = true;
1551 }
1552
1553 if (install_new_value (v, newobj, r.type_changed))
1554 {
1555 r.changed = true;
1556 v->updated = false;
1557 }
1558 }
1559
1560 /* We probably should not get children of a dynamic varobj, but
1561 for which -var-list-children was never invoked. */
1562 if (varobj_is_dynamic_p (v))
1563 {
1564 std::vector<varobj *> changed, type_changed_vec, unchanged, newobj_vec;
1565 bool children_changed = false;
1566
1567 if (v->frozen)
1568 continue;
1569
1570 if (!v->dynamic->children_requested)
1571 {
1572 bool dummy;
1573
1574 /* If we initially did not have potential children, but
1575 now we do, consider the varobj as changed.
1576 Otherwise, if children were never requested, consider
1577 it as unchanged -- presumably, such varobj is not yet
1578 expanded in the UI, so we need not bother getting
1579 it. */
1580 if (!varobj_has_more (v, 0))
1581 {
1582 update_dynamic_varobj_children (v, NULL, NULL, NULL, NULL,
1583 &dummy, false, 0, 0);
1584 if (varobj_has_more (v, 0))
1585 r.changed = true;
1586 }
1587
1588 if (r.changed)
1589 result.push_back (std::move (r));
1590
1591 continue;
1592 }
1593
1594 /* If update_dynamic_varobj_children returns false, then we have
1595 a non-conforming pretty-printer, so we skip it. */
1596 if (update_dynamic_varobj_children (v, &changed, &type_changed_vec,
1597 &newobj_vec,
1598 &unchanged, &children_changed,
1599 true, v->from, v->to))
1600 {
1601 if (children_changed || !newobj_vec.empty ())
1602 {
1603 r.children_changed = true;
1604 r.newobj = std::move (newobj_vec);
1605 }
1606 /* Push in reverse order so that the first child is
1607 popped from the work stack first, and so will be
1608 added to result first. This does not affect
1609 correctness, just "nicer". */
1610 for (int i = type_changed_vec.size () - 1; i >= 0; --i)
1611 {
1612 varobj_update_result item (type_changed_vec[i]);
1613
1614 /* Type may change only if value was changed. */
1615 item.changed = true;
1616 item.type_changed = true;
1617 item.value_installed = true;
1618
1619 stack.push_back (std::move (item));
1620 }
1621 for (int i = changed.size () - 1; i >= 0; --i)
1622 {
1623 varobj_update_result item (changed[i]);
1624
1625 item.changed = true;
1626 item.value_installed = true;
1627
1628 stack.push_back (std::move (item));
1629 }
1630 for (int i = unchanged.size () - 1; i >= 0; --i)
1631 {
1632 if (!unchanged[i]->frozen)
1633 {
1634 varobj_update_result item (unchanged[i]);
1635
1636 item.value_installed = true;
1637
1638 stack.push_back (std::move (item));
1639 }
1640 }
1641 if (r.changed || r.children_changed)
1642 result.push_back (std::move (r));
1643
1644 continue;
1645 }
1646 }
1647
1648 /* Push any children. Use reverse order so that the first
1649 child is popped from the work stack first, and so
1650 will be added to result first. This does not
1651 affect correctness, just "nicer". */
1652 for (int i = v->children.size () - 1; i >= 0; --i)
1653 {
1654 varobj *c = v->children[i];
1655
1656 /* Child may be NULL if explicitly deleted by -var-delete. */
1657 if (c != NULL && !c->frozen)
1658 stack.emplace_back (c);
1659 }
1660
1661 if (r.changed || r.type_changed)
1662 result.push_back (std::move (r));
1663 }
1664
1665 return result;
1666 }
1667
1668 /* Helper functions */
1669
1670 /*
1671 * Variable object construction/destruction
1672 */
1673
1674 static int
1675 delete_variable (struct varobj *var, bool only_children_p)
1676 {
1677 int delcount = 0;
1678
1679 delete_variable_1 (&delcount, var, only_children_p,
1680 true /* remove_from_parent_p */ );
1681
1682 return delcount;
1683 }
1684
1685 /* Delete the variable object VAR and its children. */
1686 /* IMPORTANT NOTE: If we delete a variable which is a child
1687 and the parent is not removed we dump core. It must be always
1688 initially called with remove_from_parent_p set. */
1689 static void
1690 delete_variable_1 (int *delcountp, struct varobj *var, bool only_children_p,
1691 bool remove_from_parent_p)
1692 {
1693 /* Delete any children of this variable, too. */
1694 for (varobj *child : var->children)
1695 {
1696 if (!child)
1697 continue;
1698
1699 if (!remove_from_parent_p)
1700 child->parent = NULL;
1701
1702 delete_variable_1 (delcountp, child, false, only_children_p);
1703 }
1704 var->children.clear ();
1705
1706 /* if we were called to delete only the children we are done here. */
1707 if (only_children_p)
1708 return;
1709
1710 /* Otherwise, add it to the list of deleted ones and proceed to do so. */
1711 /* If the name is empty, this is a temporary variable, that has not
1712 yet been installed, don't report it, it belongs to the caller... */
1713 if (!var->obj_name.empty ())
1714 {
1715 *delcountp = *delcountp + 1;
1716 }
1717
1718 /* If this variable has a parent, remove it from its parent's list. */
1719 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1720 (as indicated by remove_from_parent_p) we don't bother doing an
1721 expensive list search to find the element to remove when we are
1722 discarding the list afterwards. */
1723 if ((remove_from_parent_p) && (var->parent != NULL))
1724 var->parent->children[var->index] = NULL;
1725
1726 if (!var->obj_name.empty ())
1727 uninstall_variable (var);
1728
1729 /* Free memory associated with this variable. */
1730 delete var;
1731 }
1732
1733 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1734 static void
1735 install_variable (struct varobj *var)
1736 {
1737 hashval_t hash = htab_hash_string (var->obj_name.c_str ());
1738 void **slot = htab_find_slot_with_hash (varobj_table,
1739 var->obj_name.c_str (),
1740 hash, INSERT);
1741 if (*slot != nullptr)
1742 error (_("Duplicate variable object name"));
1743
1744 /* Add varobj to hash table. */
1745 *slot = var;
1746
1747 /* If root, add varobj to root list. */
1748 if (is_root_p (var))
1749 rootlist.push_front (var->root);
1750 }
1751
1752 /* Uninstall the object VAR. */
1753 static void
1754 uninstall_variable (struct varobj *var)
1755 {
1756 hashval_t hash = htab_hash_string (var->obj_name.c_str ());
1757 htab_remove_elt_with_hash (varobj_table, var->obj_name.c_str (), hash);
1758
1759 if (varobjdebug)
1760 gdb_printf (gdb_stdlog, "Deleting %s\n", var->obj_name.c_str ());
1761
1762 /* If root, remove varobj from root list. */
1763 if (is_root_p (var))
1764 {
1765 auto iter = std::find (rootlist.begin (), rootlist.end (), var->root);
1766 rootlist.erase (iter);
1767 }
1768 }
1769
1770 /* Create and install a child of the parent of the given name.
1771
1772 The created VAROBJ takes ownership of the allocated NAME. */
1773
1774 static struct varobj *
1775 create_child (struct varobj *parent, int index, std::string &name)
1776 {
1777 struct varobj_item item;
1778
1779 std::swap (item.name, name);
1780 item.value = release_value (value_of_child (parent, index));
1781
1782 return create_child_with_value (parent, index, &item);
1783 }
1784
1785 static struct varobj *
1786 create_child_with_value (struct varobj *parent, int index,
1787 struct varobj_item *item)
1788 {
1789 varobj *child = new varobj (parent->root);
1790
1791 /* NAME is allocated by caller. */
1792 std::swap (child->name, item->name);
1793 child->index = index;
1794 child->parent = parent;
1795
1796 if (varobj_is_anonymous_child (child))
1797 child->obj_name = string_printf ("%s.%d_anonymous",
1798 parent->obj_name.c_str (), index);
1799 else
1800 child->obj_name = string_printf ("%s.%s",
1801 parent->obj_name.c_str (),
1802 child->name.c_str ());
1803
1804 install_variable (child);
1805
1806 /* Compute the type of the child. Must do this before
1807 calling install_new_value. */
1808 if (item->value != NULL)
1809 /* If the child had no evaluation errors, var->value
1810 will be non-NULL and contain a valid type. */
1811 child->type = value_actual_type (item->value.get (), 0, NULL);
1812 else
1813 /* Otherwise, we must compute the type. */
1814 child->type = (*child->root->lang_ops->type_of_child) (child->parent,
1815 child->index);
1816 install_new_value (child, item->value.get (), 1);
1817
1818 return child;
1819 }
1820 \f
1821
1822 /*
1823 * Miscellaneous utility functions.
1824 */
1825
1826 /* Allocate memory and initialize a new variable. */
1827 varobj::varobj (varobj_root *root_)
1828 : root (root_), dynamic (new varobj_dynamic)
1829 {
1830 }
1831
1832 /* Free any allocated memory associated with VAR. */
1833
1834 varobj::~varobj ()
1835 {
1836 varobj *var = this;
1837
1838 #if HAVE_PYTHON
1839 if (var->dynamic->pretty_printer != NULL)
1840 {
1841 gdbpy_enter_varobj enter_py (var);
1842
1843 Py_XDECREF (var->dynamic->constructor);
1844 Py_XDECREF (var->dynamic->pretty_printer);
1845 }
1846 #endif
1847
1848 /* This must be deleted before the root object, because Python-based
1849 destructors need access to some components. */
1850 delete var->dynamic;
1851
1852 if (is_root_p (var))
1853 delete var->root;
1854 }
1855
1856 /* Return the type of the value that's stored in VAR,
1857 or that would have being stored there if the
1858 value were accessible.
1859
1860 This differs from VAR->type in that VAR->type is always
1861 the true type of the expression in the source language.
1862 The return value of this function is the type we're
1863 actually storing in varobj, and using for displaying
1864 the values and for comparing previous and new values.
1865
1866 For example, top-level references are always stripped. */
1867 struct type *
1868 varobj_get_value_type (const struct varobj *var)
1869 {
1870 struct type *type;
1871
1872 if (var->value != nullptr)
1873 type = var->value->type ();
1874 else
1875 type = var->type;
1876
1877 type = check_typedef (type);
1878
1879 if (TYPE_IS_REFERENCE (type))
1880 type = get_target_type (type);
1881
1882 type = check_typedef (type);
1883
1884 return type;
1885 }
1886
1887 /* What is the default display for this variable? We assume that
1888 everything is "natural". Any exceptions? */
1889 static enum varobj_display_formats
1890 variable_default_display (struct varobj *var)
1891 {
1892 return FORMAT_NATURAL;
1893 }
1894
1895 /*
1896 * Language-dependencies
1897 */
1898
1899 /* Common entry points */
1900
1901 /* Return the number of children for a given variable.
1902 The result of this function is defined by the language
1903 implementation. The number of children returned by this function
1904 is the number of children that the user will see in the variable
1905 display. */
1906 static int
1907 number_of_children (const struct varobj *var)
1908 {
1909 return (*var->root->lang_ops->number_of_children) (var);
1910 }
1911
1912 /* What is the expression for the root varobj VAR? */
1913
1914 static std::string
1915 name_of_variable (const struct varobj *var)
1916 {
1917 return (*var->root->lang_ops->name_of_variable) (var);
1918 }
1919
1920 /* What is the name of the INDEX'th child of VAR? */
1921
1922 static std::string
1923 name_of_child (struct varobj *var, int index)
1924 {
1925 return (*var->root->lang_ops->name_of_child) (var, index);
1926 }
1927
1928 /* If frame associated with VAR can be found, switch
1929 to it and return true. Otherwise, return false. */
1930
1931 static bool
1932 check_scope (const struct varobj *var)
1933 {
1934 frame_info_ptr fi;
1935 bool scope;
1936
1937 fi = frame_find_by_id (var->root->frame);
1938 scope = fi != NULL;
1939
1940 if (fi)
1941 {
1942 CORE_ADDR pc = get_frame_pc (fi);
1943
1944 if (pc < var->root->valid_block->start () ||
1945 pc >= var->root->valid_block->end ())
1946 scope = false;
1947 else
1948 select_frame (fi);
1949 }
1950 return scope;
1951 }
1952
1953 /* Helper function to value_of_root. */
1954
1955 static struct value *
1956 value_of_root_1 (struct varobj **var_handle)
1957 {
1958 struct value *new_val = NULL;
1959 struct varobj *var = *var_handle;
1960 bool within_scope = false;
1961
1962 /* Only root variables can be updated... */
1963 if (!is_root_p (var))
1964 /* Not a root var. */
1965 return NULL;
1966
1967 scoped_restore_current_thread restore_thread;
1968
1969 /* Determine whether the variable is still around. */
1970 if (var->root->valid_block == NULL || var->root->floating)
1971 within_scope = true;
1972 else if (var->root->thread_id == 0)
1973 {
1974 /* The program was single-threaded when the variable object was
1975 created. Technically, it's possible that the program became
1976 multi-threaded since then, but we don't support such
1977 scenario yet. */
1978 within_scope = check_scope (var);
1979 }
1980 else
1981 {
1982 thread_info *thread = find_thread_global_id (var->root->thread_id);
1983
1984 if (thread != NULL)
1985 {
1986 switch_to_thread (thread);
1987 within_scope = check_scope (var);
1988 }
1989 }
1990
1991 if (within_scope)
1992 {
1993
1994 /* We need to catch errors here, because if evaluate
1995 expression fails we want to just return NULL. */
1996 try
1997 {
1998 new_val = var->root->exp->evaluate ();
1999 }
2000 catch (const gdb_exception_error &except)
2001 {
2002 }
2003 }
2004
2005 return new_val;
2006 }
2007
2008 /* What is the ``struct value *'' of the root variable VAR?
2009 For floating variable object, evaluation can get us a value
2010 of different type from what is stored in varobj already. In
2011 that case:
2012 - *type_changed will be set to 1
2013 - old varobj will be freed, and new one will be
2014 created, with the same name.
2015 - *var_handle will be set to the new varobj
2016 Otherwise, *type_changed will be set to 0. */
2017 static struct value *
2018 value_of_root (struct varobj **var_handle, bool *type_changed)
2019 {
2020 struct varobj *var;
2021
2022 if (var_handle == NULL)
2023 return NULL;
2024
2025 var = *var_handle;
2026
2027 /* This should really be an exception, since this should
2028 only get called with a root variable. */
2029
2030 if (!is_root_p (var))
2031 return NULL;
2032
2033 if (var->root->floating)
2034 {
2035 struct varobj *tmp_var;
2036
2037 tmp_var = varobj_create (NULL, var->name.c_str (), (CORE_ADDR) 0,
2038 USE_SELECTED_FRAME);
2039 if (tmp_var == NULL)
2040 {
2041 return NULL;
2042 }
2043 std::string old_type = varobj_get_type (var);
2044 std::string new_type = varobj_get_type (tmp_var);
2045 if (old_type == new_type)
2046 {
2047 /* The expression presently stored inside var->root->exp
2048 remembers the locations of local variables relatively to
2049 the frame where the expression was created (in DWARF location
2050 button, for example). Naturally, those locations are not
2051 correct in other frames, so update the expression. */
2052
2053 std::swap (var->root->exp, tmp_var->root->exp);
2054
2055 varobj_delete (tmp_var, 0);
2056 *type_changed = 0;
2057 }
2058 else
2059 {
2060 tmp_var->obj_name = var->obj_name;
2061 tmp_var->from = var->from;
2062 tmp_var->to = var->to;
2063 varobj_delete (var, 0);
2064
2065 install_variable (tmp_var);
2066 *var_handle = tmp_var;
2067 var = *var_handle;
2068 *type_changed = true;
2069 }
2070 }
2071 else
2072 {
2073 *type_changed = 0;
2074 }
2075
2076 {
2077 struct value *value;
2078
2079 value = value_of_root_1 (var_handle);
2080 if (var->value == NULL || value == NULL)
2081 {
2082 /* For root varobj-s, a NULL value indicates a scoping issue.
2083 So, nothing to do in terms of checking for mutations. */
2084 }
2085 else if (varobj_value_has_mutated (var, value, value->type ()))
2086 {
2087 /* The type has mutated, so the children are no longer valid.
2088 Just delete them, and tell our caller that the type has
2089 changed. */
2090 varobj_delete (var, 1 /* only_children */);
2091 var->num_children = -1;
2092 var->to = -1;
2093 var->from = -1;
2094 *type_changed = true;
2095 }
2096 return value;
2097 }
2098 }
2099
2100 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
2101 static struct value *
2102 value_of_child (const struct varobj *parent, int index)
2103 {
2104 struct value *value;
2105
2106 value = (*parent->root->lang_ops->value_of_child) (parent, index);
2107
2108 return value;
2109 }
2110
2111 /* GDB already has a command called "value_of_variable". Sigh. */
2112 static std::string
2113 my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
2114 {
2115 if (var->root->is_valid)
2116 {
2117 if (var->dynamic->pretty_printer != NULL)
2118 return varobj_value_get_print_value (var->value.get (), var->format,
2119 var);
2120 return (*var->root->lang_ops->value_of_variable) (var, format);
2121 }
2122 else
2123 return std::string ();
2124 }
2125
2126 void
2127 varobj_formatted_print_options (struct value_print_options *opts,
2128 enum varobj_display_formats format)
2129 {
2130 get_formatted_print_options (opts, format_code[(int) format]);
2131 opts->deref_ref = false;
2132 opts->raw = !pretty_printing;
2133 }
2134
2135 std::string
2136 varobj_value_get_print_value (struct value *value,
2137 enum varobj_display_formats format,
2138 const struct varobj *var)
2139 {
2140 struct value_print_options opts;
2141 struct type *type = NULL;
2142 long len = 0;
2143 gdb::unique_xmalloc_ptr<char> encoding;
2144 /* Initialize it just to avoid a GCC false warning. */
2145 CORE_ADDR str_addr = 0;
2146 bool string_print = false;
2147
2148 if (value == NULL)
2149 return std::string ();
2150
2151 string_file stb;
2152 std::string thevalue;
2153
2154 varobj_formatted_print_options (&opts, format);
2155
2156 #if HAVE_PYTHON
2157 if (gdb_python_initialized)
2158 {
2159 PyObject *value_formatter = var->dynamic->pretty_printer;
2160
2161 gdbpy_enter_varobj enter_py (var);
2162
2163 if (value_formatter)
2164 {
2165 if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
2166 {
2167 struct value *replacement;
2168
2169 gdbpy_ref<> output = apply_varobj_pretty_printer (value_formatter,
2170 &replacement,
2171 &stb,
2172 &opts);
2173
2174 /* If we have string like output ... */
2175 if (output != nullptr && output != Py_None)
2176 {
2177 /* If this is a lazy string, extract it. For lazy
2178 strings we always print as a string, so set
2179 string_print. */
2180 if (gdbpy_is_lazy_string (output.get ()))
2181 {
2182 gdbpy_extract_lazy_string (output.get (), &str_addr,
2183 &type, &len, &encoding);
2184 string_print = true;
2185 }
2186 else
2187 {
2188 /* If it is a regular (non-lazy) string, extract
2189 it and copy the contents into THEVALUE. If the
2190 hint says to print it as a string, set
2191 string_print. Otherwise just return the extracted
2192 string as a value. */
2193
2194 gdb::unique_xmalloc_ptr<char> s
2195 = python_string_to_target_string (output.get ());
2196
2197 if (s)
2198 {
2199 struct gdbarch *gdbarch;
2200
2201 gdb::unique_xmalloc_ptr<char> hint
2202 = gdbpy_get_display_hint (value_formatter);
2203 if (hint)
2204 {
2205 if (!strcmp (hint.get (), "string"))
2206 string_print = true;
2207 }
2208
2209 thevalue = std::string (s.get ());
2210 len = thevalue.size ();
2211 gdbarch = value->type ()->arch ();
2212 type = builtin_type (gdbarch)->builtin_char;
2213
2214 if (!string_print)
2215 return thevalue;
2216 }
2217 else
2218 gdbpy_print_stack ();
2219 }
2220 }
2221 /* If the printer returned a replacement value, set VALUE
2222 to REPLACEMENT. If there is not a replacement value,
2223 just use the value passed to this function. */
2224 if (replacement)
2225 value = replacement;
2226 }
2227 else
2228 {
2229 /* No to_string method, so if there is a 'children'
2230 method, return the default. */
2231 if (PyObject_HasAttr (value_formatter, gdbpy_children_cst))
2232 return "{...}";
2233 }
2234 }
2235 }
2236 #endif
2237
2238 /* If the THEVALUE has contents, it is a regular string. */
2239 if (!thevalue.empty ())
2240 current_language->printstr (&stb, type, (gdb_byte *) thevalue.c_str (),
2241 len, encoding.get (), 0, &opts);
2242 else if (string_print)
2243 /* Otherwise, if string_print is set, and it is not a regular
2244 string, it is a lazy string. */
2245 val_print_string (type, encoding.get (), str_addr, len, &stb, &opts);
2246 else
2247 /* All other cases. */
2248 common_val_print (value, &stb, 0, &opts, current_language);
2249
2250 return stb.release ();
2251 }
2252
2253 bool
2254 varobj_editable_p (const struct varobj *var)
2255 {
2256 struct type *type;
2257
2258 if (!(var->root->is_valid && var->value != nullptr
2259 && var->value->lval ()))
2260 return false;
2261
2262 type = varobj_get_value_type (var);
2263
2264 switch (type->code ())
2265 {
2266 case TYPE_CODE_STRUCT:
2267 case TYPE_CODE_UNION:
2268 case TYPE_CODE_ARRAY:
2269 case TYPE_CODE_FUNC:
2270 case TYPE_CODE_METHOD:
2271 return false;
2272 break;
2273
2274 default:
2275 return true;
2276 break;
2277 }
2278 }
2279
2280 /* Call VAR's value_is_changeable_p language-specific callback. */
2281
2282 bool
2283 varobj_value_is_changeable_p (const struct varobj *var)
2284 {
2285 return var->root->lang_ops->value_is_changeable_p (var);
2286 }
2287
2288 /* Return true if that varobj is floating, that is is always evaluated in the
2289 selected frame, and not bound to thread/frame. Such variable objects
2290 are created using '@' as frame specifier to -var-create. */
2291 bool
2292 varobj_floating_p (const struct varobj *var)
2293 {
2294 return var->root->floating;
2295 }
2296
2297 /* Implement the "value_is_changeable_p" varobj callback for most
2298 languages. */
2299
2300 bool
2301 varobj_default_value_is_changeable_p (const struct varobj *var)
2302 {
2303 bool r;
2304 struct type *type;
2305
2306 if (CPLUS_FAKE_CHILD (var))
2307 return false;
2308
2309 type = varobj_get_value_type (var);
2310
2311 switch (type->code ())
2312 {
2313 case TYPE_CODE_STRUCT:
2314 case TYPE_CODE_UNION:
2315 case TYPE_CODE_ARRAY:
2316 r = false;
2317 break;
2318
2319 default:
2320 r = true;
2321 }
2322
2323 return r;
2324 }
2325
2326 /* Iterate all the existing _root_ VAROBJs and call the FUNC callback
2327 for each one. */
2328
2329 void
2330 all_root_varobjs (gdb::function_view<void (struct varobj *var)> func)
2331 {
2332 /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
2333 auto iter = rootlist.begin ();
2334 auto end = rootlist.end ();
2335 while (iter != end)
2336 {
2337 auto self = iter++;
2338 func ((*self)->rootvar);
2339 }
2340 }
2341
2342 /* Try to recreate the varobj VAR if it is a global or floating. This is a
2343 helper function for varobj_re_set. */
2344
2345 static void
2346 varobj_re_set_iter (struct varobj *var)
2347 {
2348 /* Invalidated global varobjs must be re-evaluated. */
2349 if (!var->root->is_valid && var->root->global)
2350 {
2351 struct varobj *tmp_var;
2352
2353 /* Try to create a varobj with same expression. If we succeed
2354 and have a global replace the old varobj. */
2355 tmp_var = varobj_create (nullptr, var->name.c_str (), (CORE_ADDR) 0,
2356 USE_CURRENT_FRAME);
2357 if (tmp_var != nullptr && tmp_var->root->global)
2358 {
2359 tmp_var->obj_name = var->obj_name;
2360 varobj_delete (var, 0);
2361 install_variable (tmp_var);
2362 }
2363 }
2364 }
2365
2366 /* See varobj.h. */
2367
2368 void
2369 varobj_re_set (void)
2370 {
2371 all_root_varobjs (varobj_re_set_iter);
2372 }
2373
2374 /* Ensure that no varobj keep references to OBJFILE. */
2375
2376 static void
2377 varobj_invalidate_if_uses_objfile (struct objfile *objfile)
2378 {
2379 if (objfile->separate_debug_objfile_backlink != nullptr)
2380 objfile = objfile->separate_debug_objfile_backlink;
2381
2382 all_root_varobjs ([objfile] (struct varobj *var)
2383 {
2384 if (var->root->valid_block != nullptr)
2385 {
2386 struct objfile *bl_objfile = var->root->valid_block->objfile ();
2387 if (bl_objfile->separate_debug_objfile_backlink != nullptr)
2388 bl_objfile = bl_objfile->separate_debug_objfile_backlink;
2389
2390 if (bl_objfile == objfile)
2391 {
2392 /* The varobj is tied to a block which is going away. There is
2393 no way to reconstruct something later, so invalidate the
2394 varobj completely and drop the reference to the block which is
2395 being freed. */
2396 var->root->is_valid = false;
2397 var->root->valid_block = nullptr;
2398 }
2399 }
2400
2401 if (var->root->exp != nullptr && var->root->exp->uses_objfile (objfile))
2402 {
2403 /* The varobj's current expression references the objfile. For
2404 globals and floating, it is possible that when we try to
2405 re-evaluate the expression later it is still valid with
2406 whatever is in scope at that moment. Just invalidate the
2407 expression for now. */
2408 var->root->exp.reset ();
2409
2410 /* It only makes sense to keep a floating varobj around. */
2411 if (!var->root->floating)
2412 var->root->is_valid = false;
2413 }
2414
2415 /* var->value->type and var->type might also reference the objfile.
2416 This is taken care of in value.c:preserve_values which deals with
2417 making sure that objfile-owned types are replaced with
2418 gdbarch-owned equivalents. */
2419 });
2420 }
2421
2422 /* A hash function for a varobj. */
2423
2424 static hashval_t
2425 hash_varobj (const void *a)
2426 {
2427 const varobj *obj = (const varobj *) a;
2428 return htab_hash_string (obj->obj_name.c_str ());
2429 }
2430
2431 /* A hash table equality function for varobjs. */
2432
2433 static int
2434 eq_varobj_and_string (const void *a, const void *b)
2435 {
2436 const varobj *obj = (const varobj *) a;
2437 const char *name = (const char *) b;
2438
2439 return obj->obj_name == name;
2440 }
2441
2442 void _initialize_varobj ();
2443 void
2444 _initialize_varobj ()
2445 {
2446 varobj_table = htab_create_alloc (5, hash_varobj, eq_varobj_and_string,
2447 nullptr, xcalloc, xfree);
2448
2449 add_setshow_zuinteger_cmd ("varobj", class_maintenance,
2450 &varobjdebug,
2451 _("Set varobj debugging."),
2452 _("Show varobj debugging."),
2453 _("When non-zero, varobj debugging is enabled."),
2454 NULL, show_varobjdebug,
2455 &setdebuglist, &showdebuglist);
2456
2457 gdb::observers::free_objfile.attach (varobj_invalidate_if_uses_objfile,
2458 "varobj");
2459 }