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