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