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