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