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