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