]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/varobj.c
New varobj language callback: value_is_changeable_p.
[thirdparty/binutils-gdb.git] / gdb / varobj.c
1 /* Implementation of the GDB variable objects API.
2
3 Copyright (C) 1999-2012 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 "gdb_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 #include "ada-varobj.h"
37 #include "ada-lang.h"
38
39 #if HAVE_PYTHON
40 #include "python/python.h"
41 #include "python/python-internal.h"
42 #else
43 typedef int PyObject;
44 #endif
45
46 /* The names of varobjs representing anonymous structs or unions. */
47 #define ANONYMOUS_STRUCT_NAME _("<anonymous struct>")
48 #define ANONYMOUS_UNION_NAME _("<anonymous union>")
49
50 /* Non-zero if we want to see trace of varobj level stuff. */
51
52 int varobjdebug = 0;
53 static void
54 show_varobjdebug (struct ui_file *file, int from_tty,
55 struct cmd_list_element *c, const char *value)
56 {
57 fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
58 }
59
60 /* String representations of gdb's format codes. */
61 char *varobj_format_string[] =
62 { "natural", "binary", "decimal", "hexadecimal", "octal" };
63
64 /* String representations of gdb's known languages. */
65 char *varobj_language_string[] = { "unknown", "C", "C++", "Java" };
66
67 /* True if we want to allow Python-based pretty-printing. */
68 static int pretty_printing = 0;
69
70 void
71 varobj_enable_pretty_printing (void)
72 {
73 pretty_printing = 1;
74 }
75
76 /* Data structures */
77
78 /* Every root variable has one of these structures saved in its
79 varobj. Members which must be free'd are noted. */
80 struct varobj_root
81 {
82
83 /* Alloc'd expression for this parent. */
84 struct expression *exp;
85
86 /* Block for which this expression is valid. */
87 struct block *valid_block;
88
89 /* The frame for this expression. This field is set iff valid_block is
90 not NULL. */
91 struct frame_id frame;
92
93 /* The thread ID that this varobj_root belong to. This field
94 is only valid if valid_block is not NULL.
95 When not 0, indicates which thread 'frame' belongs to.
96 When 0, indicates that the thread list was empty when the varobj_root
97 was created. */
98 int thread_id;
99
100 /* If 1, the -var-update always recomputes the value in the
101 current thread and frame. Otherwise, variable object is
102 always updated in the specific scope/thread/frame. */
103 int floating;
104
105 /* Flag that indicates validity: set to 0 when this varobj_root refers
106 to symbols that do not exist anymore. */
107 int is_valid;
108
109 /* Language info for this variable and its children. */
110 struct language_specific *lang;
111
112 /* The varobj for this root node. */
113 struct varobj *rootvar;
114
115 /* Next root variable */
116 struct varobj_root *next;
117 };
118
119 /* Every variable in the system has a structure of this type defined
120 for it. This structure holds all information necessary to manipulate
121 a particular object variable. Members which must be freed are noted. */
122 struct varobj
123 {
124
125 /* Alloc'd name of the variable for this object. If this variable is a
126 child, then this name will be the child's source name.
127 (bar, not foo.bar). */
128 /* NOTE: This is the "expression". */
129 char *name;
130
131 /* Alloc'd expression for this child. Can be used to create a
132 root variable corresponding to this child. */
133 char *path_expr;
134
135 /* The alloc'd name for this variable's object. This is here for
136 convenience when constructing this object's children. */
137 char *obj_name;
138
139 /* Index of this variable in its parent or -1. */
140 int index;
141
142 /* The type of this variable. This can be NULL
143 for artifial variable objects -- currently, the "accessibility"
144 variable objects in C++. */
145 struct type *type;
146
147 /* The value of this expression or subexpression. A NULL value
148 indicates there was an error getting this value.
149 Invariant: if varobj_value_is_changeable_p (this) is non-zero,
150 the value is either NULL, or not lazy. */
151 struct value *value;
152
153 /* The number of (immediate) children this variable has. */
154 int num_children;
155
156 /* If this object is a child, this points to its immediate parent. */
157 struct varobj *parent;
158
159 /* Children of this object. */
160 VEC (varobj_p) *children;
161
162 /* Whether the children of this varobj were requested. This field is
163 used to decide if dynamic varobj should recompute their children.
164 In the event that the frontend never asked for the children, we
165 can avoid that. */
166 int children_requested;
167
168 /* Description of the root variable. Points to root variable for
169 children. */
170 struct varobj_root *root;
171
172 /* The format of the output for this object. */
173 enum varobj_display_formats format;
174
175 /* Was this variable updated via a varobj_set_value operation. */
176 int updated;
177
178 /* Last print value. */
179 char *print_value;
180
181 /* Is this variable frozen. Frozen variables are never implicitly
182 updated by -var-update *
183 or -var-update <direct-or-indirect-parent>. */
184 int frozen;
185
186 /* Is the value of this variable intentionally not fetched? It is
187 not fetched if either the variable is frozen, or any parents is
188 frozen. */
189 int not_fetched;
190
191 /* Sub-range of children which the MI consumer has requested. If
192 FROM < 0 or TO < 0, means that all children have been
193 requested. */
194 int from;
195 int to;
196
197 /* The pretty-printer constructor. If NULL, then the default
198 pretty-printer will be looked up. If None, then no
199 pretty-printer will be installed. */
200 PyObject *constructor;
201
202 /* The pretty-printer that has been constructed. If NULL, then a
203 new printer object is needed, and one will be constructed. */
204 PyObject *pretty_printer;
205
206 /* The iterator returned by the printer's 'children' method, or NULL
207 if not available. */
208 PyObject *child_iter;
209
210 /* We request one extra item from the iterator, so that we can
211 report to the caller whether there are more items than we have
212 already reported. However, we don't want to install this value
213 when we read it, because that will mess up future updates. So,
214 we stash it here instead. */
215 PyObject *saved_item;
216 };
217
218 struct cpstack
219 {
220 char *name;
221 struct cpstack *next;
222 };
223
224 /* A list of varobjs */
225
226 struct vlist
227 {
228 struct varobj *var;
229 struct vlist *next;
230 };
231
232 /* Private function prototypes */
233
234 /* Helper functions for the above subcommands. */
235
236 static int delete_variable (struct cpstack **, struct varobj *, int);
237
238 static void delete_variable_1 (struct cpstack **, int *,
239 struct varobj *, int, int);
240
241 static int install_variable (struct varobj *);
242
243 static void uninstall_variable (struct varobj *);
244
245 static struct varobj *create_child (struct varobj *, int, char *);
246
247 static struct varobj *
248 create_child_with_value (struct varobj *parent, int index, const char *name,
249 struct value *value);
250
251 /* Utility routines */
252
253 static struct varobj *new_variable (void);
254
255 static struct varobj *new_root_variable (void);
256
257 static void free_variable (struct varobj *var);
258
259 static struct cleanup *make_cleanup_free_variable (struct varobj *var);
260
261 static struct type *get_type (struct varobj *var);
262
263 static struct type *get_value_type (struct varobj *var);
264
265 static struct type *get_target_type (struct type *);
266
267 static enum varobj_display_formats variable_default_display (struct varobj *);
268
269 static void cppush (struct cpstack **pstack, char *name);
270
271 static char *cppop (struct cpstack **pstack);
272
273 static int install_new_value (struct varobj *var, struct value *value,
274 int initial);
275
276 /* Language-specific routines. */
277
278 static enum varobj_languages variable_language (struct varobj *var);
279
280 static int number_of_children (struct varobj *);
281
282 static char *name_of_variable (struct varobj *);
283
284 static char *name_of_child (struct varobj *, int);
285
286 static struct value *value_of_root (struct varobj **var_handle, int *);
287
288 static struct value *value_of_child (struct varobj *parent, int index);
289
290 static char *my_value_of_variable (struct varobj *var,
291 enum varobj_display_formats format);
292
293 static char *value_get_print_value (struct value *value,
294 enum varobj_display_formats format,
295 struct varobj *var);
296
297 static int varobj_value_is_changeable_p (struct varobj *var);
298
299 static int is_root_p (struct varobj *var);
300
301 #if HAVE_PYTHON
302
303 static struct varobj *varobj_add_child (struct varobj *var,
304 const char *name,
305 struct value *value);
306
307 #endif /* HAVE_PYTHON */
308
309 static int default_value_is_changeable_p (struct varobj *var);
310
311 /* C implementation */
312
313 static int c_number_of_children (struct varobj *var);
314
315 static char *c_name_of_variable (struct varobj *parent);
316
317 static char *c_name_of_child (struct varobj *parent, int index);
318
319 static char *c_path_expr_of_child (struct varobj *child);
320
321 static struct value *c_value_of_root (struct varobj **var_handle);
322
323 static struct value *c_value_of_child (struct varobj *parent, int index);
324
325 static struct type *c_type_of_child (struct varobj *parent, int index);
326
327 static char *c_value_of_variable (struct varobj *var,
328 enum varobj_display_formats format);
329
330 /* C++ implementation */
331
332 static int cplus_number_of_children (struct varobj *var);
333
334 static void cplus_class_num_children (struct type *type, int children[3]);
335
336 static char *cplus_name_of_variable (struct varobj *parent);
337
338 static char *cplus_name_of_child (struct varobj *parent, int index);
339
340 static char *cplus_path_expr_of_child (struct varobj *child);
341
342 static struct value *cplus_value_of_root (struct varobj **var_handle);
343
344 static struct value *cplus_value_of_child (struct varobj *parent, int index);
345
346 static struct type *cplus_type_of_child (struct varobj *parent, int index);
347
348 static char *cplus_value_of_variable (struct varobj *var,
349 enum varobj_display_formats format);
350
351 /* Java implementation */
352
353 static int java_number_of_children (struct varobj *var);
354
355 static char *java_name_of_variable (struct varobj *parent);
356
357 static char *java_name_of_child (struct varobj *parent, int index);
358
359 static char *java_path_expr_of_child (struct varobj *child);
360
361 static struct value *java_value_of_root (struct varobj **var_handle);
362
363 static struct value *java_value_of_child (struct varobj *parent, int index);
364
365 static struct type *java_type_of_child (struct varobj *parent, int index);
366
367 static char *java_value_of_variable (struct varobj *var,
368 enum varobj_display_formats format);
369
370 /* Ada implementation */
371
372 static int ada_number_of_children (struct varobj *var);
373
374 static char *ada_name_of_variable (struct varobj *parent);
375
376 static char *ada_name_of_child (struct varobj *parent, int index);
377
378 static char *ada_path_expr_of_child (struct varobj *child);
379
380 static struct value *ada_value_of_root (struct varobj **var_handle);
381
382 static struct value *ada_value_of_child (struct varobj *parent, int index);
383
384 static struct type *ada_type_of_child (struct varobj *parent, int index);
385
386 static char *ada_value_of_variable (struct varobj *var,
387 enum varobj_display_formats format);
388
389 static int ada_value_is_changeable_p (struct varobj *var);
390
391 static int ada_value_has_mutated (struct varobj *var, struct value *new_val,
392 struct type *new_type);
393
394 /* The language specific vector */
395
396 struct language_specific
397 {
398
399 /* The language of this variable. */
400 enum varobj_languages language;
401
402 /* The number of children of PARENT. */
403 int (*number_of_children) (struct varobj * parent);
404
405 /* The name (expression) of a root varobj. */
406 char *(*name_of_variable) (struct varobj * parent);
407
408 /* The name of the INDEX'th child of PARENT. */
409 char *(*name_of_child) (struct varobj * parent, int index);
410
411 /* Returns the rooted expression of CHILD, which is a variable
412 obtain that has some parent. */
413 char *(*path_expr_of_child) (struct varobj * child);
414
415 /* The ``struct value *'' of the root variable ROOT. */
416 struct value *(*value_of_root) (struct varobj ** root_handle);
417
418 /* The ``struct value *'' of the INDEX'th child of PARENT. */
419 struct value *(*value_of_child) (struct varobj * parent, int index);
420
421 /* The type of the INDEX'th child of PARENT. */
422 struct type *(*type_of_child) (struct varobj * parent, int index);
423
424 /* The current value of VAR. */
425 char *(*value_of_variable) (struct varobj * var,
426 enum varobj_display_formats format);
427
428 /* Return non-zero if changes in value of VAR must be detected and
429 reported by -var-update. Return zero if -var-update should never
430 report changes of such values. This makes sense for structures
431 (since the changes in children values will be reported separately),
432 or for artifical objects (like 'public' pseudo-field in C++).
433
434 Return value of 0 means that gdb need not call value_fetch_lazy
435 for the value of this variable object. */
436 int (*value_is_changeable_p) (struct varobj *var);
437
438 /* Return nonzero if the type of VAR has mutated.
439
440 VAR's value is still the varobj's previous value, while NEW_VALUE
441 is VAR's new value and NEW_TYPE is the var's new type. NEW_VALUE
442 may be NULL indicating that there is no value available (the varobj
443 may be out of scope, of may be the child of a null pointer, for
444 instance). NEW_TYPE, on the other hand, must never be NULL.
445
446 This function should also be able to assume that var's number of
447 children is set (not < 0).
448
449 Languages where types do not mutate can set this to NULL. */
450 int (*value_has_mutated) (struct varobj *var, struct value *new_value,
451 struct type *new_type);
452 };
453
454 /* Array of known source language routines. */
455 static struct language_specific languages[vlang_end] = {
456 /* Unknown (try treating as C). */
457 {
458 vlang_unknown,
459 c_number_of_children,
460 c_name_of_variable,
461 c_name_of_child,
462 c_path_expr_of_child,
463 c_value_of_root,
464 c_value_of_child,
465 c_type_of_child,
466 c_value_of_variable,
467 default_value_is_changeable_p,
468 NULL /* value_has_mutated */}
469 ,
470 /* C */
471 {
472 vlang_c,
473 c_number_of_children,
474 c_name_of_variable,
475 c_name_of_child,
476 c_path_expr_of_child,
477 c_value_of_root,
478 c_value_of_child,
479 c_type_of_child,
480 c_value_of_variable,
481 default_value_is_changeable_p,
482 NULL /* value_has_mutated */}
483 ,
484 /* C++ */
485 {
486 vlang_cplus,
487 cplus_number_of_children,
488 cplus_name_of_variable,
489 cplus_name_of_child,
490 cplus_path_expr_of_child,
491 cplus_value_of_root,
492 cplus_value_of_child,
493 cplus_type_of_child,
494 cplus_value_of_variable,
495 default_value_is_changeable_p,
496 NULL /* value_has_mutated */}
497 ,
498 /* Java */
499 {
500 vlang_java,
501 java_number_of_children,
502 java_name_of_variable,
503 java_name_of_child,
504 java_path_expr_of_child,
505 java_value_of_root,
506 java_value_of_child,
507 java_type_of_child,
508 java_value_of_variable,
509 default_value_is_changeable_p,
510 NULL /* value_has_mutated */},
511 /* Ada */
512 {
513 vlang_ada,
514 ada_number_of_children,
515 ada_name_of_variable,
516 ada_name_of_child,
517 ada_path_expr_of_child,
518 ada_value_of_root,
519 ada_value_of_child,
520 ada_type_of_child,
521 ada_value_of_variable,
522 ada_value_is_changeable_p,
523 ada_value_has_mutated}
524 };
525
526 /* A little convenience enum for dealing with C++/Java. */
527 enum vsections
528 {
529 v_public = 0, v_private, v_protected
530 };
531
532 /* Private data */
533
534 /* Mappings of varobj_display_formats enums to gdb's format codes. */
535 static int format_code[] = { 0, 't', 'd', 'x', 'o' };
536
537 /* Header of the list of root variable objects. */
538 static struct varobj_root *rootlist;
539
540 /* Prime number indicating the number of buckets in the hash table. */
541 /* A prime large enough to avoid too many colisions. */
542 #define VAROBJ_TABLE_SIZE 227
543
544 /* Pointer to the varobj hash table (built at run time). */
545 static struct vlist **varobj_table;
546
547 /* Is the variable X one of our "fake" children? */
548 #define CPLUS_FAKE_CHILD(x) \
549 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
550 \f
551
552 /* API Implementation */
553 static int
554 is_root_p (struct varobj *var)
555 {
556 return (var->root->rootvar == var);
557 }
558
559 #ifdef HAVE_PYTHON
560 /* Helper function to install a Python environment suitable for
561 use during operations on VAR. */
562 static struct cleanup *
563 varobj_ensure_python_env (struct varobj *var)
564 {
565 return ensure_python_env (var->root->exp->gdbarch,
566 var->root->exp->language_defn);
567 }
568 #endif
569
570 /* Creates a varobj (not its children). */
571
572 /* Return the full FRAME which corresponds to the given CORE_ADDR
573 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
574
575 static struct frame_info *
576 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
577 {
578 struct frame_info *frame = NULL;
579
580 if (frame_addr == (CORE_ADDR) 0)
581 return NULL;
582
583 for (frame = get_current_frame ();
584 frame != NULL;
585 frame = get_prev_frame (frame))
586 {
587 /* The CORE_ADDR we get as argument was parsed from a string GDB
588 output as $fp. This output got truncated to gdbarch_addr_bit.
589 Truncate the frame base address in the same manner before
590 comparing it against our argument. */
591 CORE_ADDR frame_base = get_frame_base_address (frame);
592 int addr_bit = gdbarch_addr_bit (get_frame_arch (frame));
593
594 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
595 frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1;
596
597 if (frame_base == frame_addr)
598 return frame;
599 }
600
601 return NULL;
602 }
603
604 struct varobj *
605 varobj_create (char *objname,
606 char *expression, CORE_ADDR frame, enum varobj_type type)
607 {
608 struct varobj *var;
609 struct cleanup *old_chain;
610
611 /* Fill out a varobj structure for the (root) variable being constructed. */
612 var = new_root_variable ();
613 old_chain = make_cleanup_free_variable (var);
614
615 if (expression != NULL)
616 {
617 struct frame_info *fi;
618 struct frame_id old_id = null_frame_id;
619 struct block *block;
620 char *p;
621 enum varobj_languages lang;
622 struct value *value = NULL;
623 volatile struct gdb_exception except;
624
625 /* Parse and evaluate the expression, filling in as much of the
626 variable's data as possible. */
627
628 if (has_stack_frames ())
629 {
630 /* Allow creator to specify context of variable. */
631 if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
632 fi = get_selected_frame (NULL);
633 else
634 /* FIXME: cagney/2002-11-23: This code should be doing a
635 lookup using the frame ID and not just the frame's
636 ``address''. This, of course, means an interface
637 change. However, with out that interface change ISAs,
638 such as the ia64 with its two stacks, won't work.
639 Similar goes for the case where there is a frameless
640 function. */
641 fi = find_frame_addr_in_frame_chain (frame);
642 }
643 else
644 fi = NULL;
645
646 /* frame = -2 means always use selected frame. */
647 if (type == USE_SELECTED_FRAME)
648 var->root->floating = 1;
649
650 block = NULL;
651 if (fi != NULL)
652 block = get_frame_block (fi, 0);
653
654 p = expression;
655 innermost_block = NULL;
656 /* Wrap the call to parse expression, so we can
657 return a sensible error. */
658 TRY_CATCH (except, RETURN_MASK_ERROR)
659 {
660 var->root->exp = parse_exp_1 (&p, block, 0);
661 }
662
663 if (except.reason < 0)
664 {
665 do_cleanups (old_chain);
666 return NULL;
667 }
668
669 /* Don't allow variables to be created for types. */
670 if (var->root->exp->elts[0].opcode == OP_TYPE)
671 {
672 do_cleanups (old_chain);
673 fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
674 " as an expression.\n");
675 return NULL;
676 }
677
678 var->format = variable_default_display (var);
679 var->root->valid_block = innermost_block;
680 var->name = xstrdup (expression);
681 /* For a root var, the name and the expr are the same. */
682 var->path_expr = xstrdup (expression);
683
684 /* When the frame is different from the current frame,
685 we must select the appropriate frame before parsing
686 the expression, otherwise the value will not be current.
687 Since select_frame is so benign, just call it for all cases. */
688 if (innermost_block)
689 {
690 /* User could specify explicit FRAME-ADDR which was not found but
691 EXPRESSION is frame specific and we would not be able to evaluate
692 it correctly next time. With VALID_BLOCK set we must also set
693 FRAME and THREAD_ID. */
694 if (fi == NULL)
695 error (_("Failed to find the specified frame"));
696
697 var->root->frame = get_frame_id (fi);
698 var->root->thread_id = pid_to_thread_id (inferior_ptid);
699 old_id = get_frame_id (get_selected_frame (NULL));
700 select_frame (fi);
701 }
702
703 /* We definitely need to catch errors here.
704 If evaluate_expression succeeds we got the value we wanted.
705 But if it fails, we still go on with a call to evaluate_type(). */
706 TRY_CATCH (except, RETURN_MASK_ERROR)
707 {
708 value = evaluate_expression (var->root->exp);
709 }
710
711 if (except.reason < 0)
712 {
713 /* Error getting the value. Try to at least get the
714 right type. */
715 struct value *type_only_value = evaluate_type (var->root->exp);
716
717 var->type = value_type (type_only_value);
718 }
719 else
720 var->type = value_type (value);
721
722 /* Set language info */
723 lang = variable_language (var);
724 var->root->lang = &languages[lang];
725
726 install_new_value (var, value, 1 /* Initial assignment */);
727
728 /* Set ourselves as our root. */
729 var->root->rootvar = var;
730
731 /* Reset the selected frame. */
732 if (frame_id_p (old_id))
733 select_frame (frame_find_by_id (old_id));
734 }
735
736 /* If the variable object name is null, that means this
737 is a temporary variable, so don't install it. */
738
739 if ((var != NULL) && (objname != NULL))
740 {
741 var->obj_name = xstrdup (objname);
742
743 /* If a varobj name is duplicated, the install will fail so
744 we must cleanup. */
745 if (!install_variable (var))
746 {
747 do_cleanups (old_chain);
748 return NULL;
749 }
750 }
751
752 discard_cleanups (old_chain);
753 return var;
754 }
755
756 /* Generates an unique name that can be used for a varobj. */
757
758 char *
759 varobj_gen_name (void)
760 {
761 static int id = 0;
762 char *obj_name;
763
764 /* Generate a name for this object. */
765 id++;
766 obj_name = xstrprintf ("var%d", id);
767
768 return obj_name;
769 }
770
771 /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
772 error if OBJNAME cannot be found. */
773
774 struct varobj *
775 varobj_get_handle (char *objname)
776 {
777 struct vlist *cv;
778 const char *chp;
779 unsigned int index = 0;
780 unsigned int i = 1;
781
782 for (chp = objname; *chp; chp++)
783 {
784 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
785 }
786
787 cv = *(varobj_table + index);
788 while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
789 cv = cv->next;
790
791 if (cv == NULL)
792 error (_("Variable object not found"));
793
794 return cv->var;
795 }
796
797 /* Given the handle, return the name of the object. */
798
799 char *
800 varobj_get_objname (struct varobj *var)
801 {
802 return var->obj_name;
803 }
804
805 /* Given the handle, return the expression represented by the object. */
806
807 char *
808 varobj_get_expression (struct varobj *var)
809 {
810 return name_of_variable (var);
811 }
812
813 /* Deletes a varobj and all its children if only_children == 0,
814 otherwise deletes only the children; returns a malloc'ed list of
815 all the (malloc'ed) names of the variables that have been deleted
816 (NULL terminated). */
817
818 int
819 varobj_delete (struct varobj *var, char ***dellist, int only_children)
820 {
821 int delcount;
822 int mycount;
823 struct cpstack *result = NULL;
824 char **cp;
825
826 /* Initialize a stack for temporary results. */
827 cppush (&result, NULL);
828
829 if (only_children)
830 /* Delete only the variable children. */
831 delcount = delete_variable (&result, var, 1 /* only the children */ );
832 else
833 /* Delete the variable and all its children. */
834 delcount = delete_variable (&result, var, 0 /* parent+children */ );
835
836 /* We may have been asked to return a list of what has been deleted. */
837 if (dellist != NULL)
838 {
839 *dellist = xmalloc ((delcount + 1) * sizeof (char *));
840
841 cp = *dellist;
842 mycount = delcount;
843 *cp = cppop (&result);
844 while ((*cp != NULL) && (mycount > 0))
845 {
846 mycount--;
847 cp++;
848 *cp = cppop (&result);
849 }
850
851 if (mycount || (*cp != NULL))
852 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
853 mycount);
854 }
855
856 return delcount;
857 }
858
859 #if HAVE_PYTHON
860
861 /* Convenience function for varobj_set_visualizer. Instantiate a
862 pretty-printer for a given value. */
863 static PyObject *
864 instantiate_pretty_printer (PyObject *constructor, struct value *value)
865 {
866 PyObject *val_obj = NULL;
867 PyObject *printer;
868
869 val_obj = value_to_value_object (value);
870 if (! val_obj)
871 return NULL;
872
873 printer = PyObject_CallFunctionObjArgs (constructor, val_obj, NULL);
874 Py_DECREF (val_obj);
875 return printer;
876 }
877
878 #endif
879
880 /* Set/Get variable object display format. */
881
882 enum varobj_display_formats
883 varobj_set_display_format (struct varobj *var,
884 enum varobj_display_formats format)
885 {
886 switch (format)
887 {
888 case FORMAT_NATURAL:
889 case FORMAT_BINARY:
890 case FORMAT_DECIMAL:
891 case FORMAT_HEXADECIMAL:
892 case FORMAT_OCTAL:
893 var->format = format;
894 break;
895
896 default:
897 var->format = variable_default_display (var);
898 }
899
900 if (varobj_value_is_changeable_p (var)
901 && var->value && !value_lazy (var->value))
902 {
903 xfree (var->print_value);
904 var->print_value = value_get_print_value (var->value, var->format, var);
905 }
906
907 return var->format;
908 }
909
910 enum varobj_display_formats
911 varobj_get_display_format (struct varobj *var)
912 {
913 return var->format;
914 }
915
916 char *
917 varobj_get_display_hint (struct varobj *var)
918 {
919 char *result = NULL;
920
921 #if HAVE_PYTHON
922 struct cleanup *back_to = varobj_ensure_python_env (var);
923
924 if (var->pretty_printer)
925 result = gdbpy_get_display_hint (var->pretty_printer);
926
927 do_cleanups (back_to);
928 #endif
929
930 return result;
931 }
932
933 /* Return true if the varobj has items after TO, false otherwise. */
934
935 int
936 varobj_has_more (struct varobj *var, int to)
937 {
938 if (VEC_length (varobj_p, var->children) > to)
939 return 1;
940 return ((to == -1 || VEC_length (varobj_p, var->children) == to)
941 && var->saved_item != NULL);
942 }
943
944 /* If the variable object is bound to a specific thread, that
945 is its evaluation can always be done in context of a frame
946 inside that thread, returns GDB id of the thread -- which
947 is always positive. Otherwise, returns -1. */
948 int
949 varobj_get_thread_id (struct varobj *var)
950 {
951 if (var->root->valid_block && var->root->thread_id > 0)
952 return var->root->thread_id;
953 else
954 return -1;
955 }
956
957 void
958 varobj_set_frozen (struct varobj *var, int frozen)
959 {
960 /* When a variable is unfrozen, we don't fetch its value.
961 The 'not_fetched' flag remains set, so next -var-update
962 won't complain.
963
964 We don't fetch the value, because for structures the client
965 should do -var-update anyway. It would be bad to have different
966 client-size logic for structure and other types. */
967 var->frozen = frozen;
968 }
969
970 int
971 varobj_get_frozen (struct varobj *var)
972 {
973 return var->frozen;
974 }
975
976 /* A helper function that restricts a range to what is actually
977 available in a VEC. This follows the usual rules for the meaning
978 of FROM and TO -- if either is negative, the entire range is
979 used. */
980
981 static void
982 restrict_range (VEC (varobj_p) *children, int *from, int *to)
983 {
984 if (*from < 0 || *to < 0)
985 {
986 *from = 0;
987 *to = VEC_length (varobj_p, children);
988 }
989 else
990 {
991 if (*from > VEC_length (varobj_p, children))
992 *from = VEC_length (varobj_p, children);
993 if (*to > VEC_length (varobj_p, children))
994 *to = VEC_length (varobj_p, children);
995 if (*from > *to)
996 *from = *to;
997 }
998 }
999
1000 #if HAVE_PYTHON
1001
1002 /* A helper for update_dynamic_varobj_children that installs a new
1003 child when needed. */
1004
1005 static void
1006 install_dynamic_child (struct varobj *var,
1007 VEC (varobj_p) **changed,
1008 VEC (varobj_p) **new,
1009 VEC (varobj_p) **unchanged,
1010 int *cchanged,
1011 int index,
1012 const char *name,
1013 struct value *value)
1014 {
1015 if (VEC_length (varobj_p, var->children) < index + 1)
1016 {
1017 /* There's no child yet. */
1018 struct varobj *child = varobj_add_child (var, name, value);
1019
1020 if (new)
1021 {
1022 VEC_safe_push (varobj_p, *new, child);
1023 *cchanged = 1;
1024 }
1025 }
1026 else
1027 {
1028 varobj_p existing = VEC_index (varobj_p, var->children, index);
1029
1030 if (install_new_value (existing, value, 0))
1031 {
1032 if (changed)
1033 VEC_safe_push (varobj_p, *changed, existing);
1034 }
1035 else if (unchanged)
1036 VEC_safe_push (varobj_p, *unchanged, existing);
1037 }
1038 }
1039
1040 static int
1041 dynamic_varobj_has_child_method (struct varobj *var)
1042 {
1043 struct cleanup *back_to;
1044 PyObject *printer = var->pretty_printer;
1045 int result;
1046
1047 back_to = varobj_ensure_python_env (var);
1048 result = PyObject_HasAttr (printer, gdbpy_children_cst);
1049 do_cleanups (back_to);
1050 return result;
1051 }
1052
1053 #endif
1054
1055 static int
1056 update_dynamic_varobj_children (struct varobj *var,
1057 VEC (varobj_p) **changed,
1058 VEC (varobj_p) **new,
1059 VEC (varobj_p) **unchanged,
1060 int *cchanged,
1061 int update_children,
1062 int from,
1063 int to)
1064 {
1065 #if HAVE_PYTHON
1066 struct cleanup *back_to;
1067 PyObject *children;
1068 int i;
1069 PyObject *printer = var->pretty_printer;
1070
1071 back_to = varobj_ensure_python_env (var);
1072
1073 *cchanged = 0;
1074 if (!PyObject_HasAttr (printer, gdbpy_children_cst))
1075 {
1076 do_cleanups (back_to);
1077 return 0;
1078 }
1079
1080 if (update_children || !var->child_iter)
1081 {
1082 children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
1083 NULL);
1084
1085 if (!children)
1086 {
1087 gdbpy_print_stack ();
1088 error (_("Null value returned for children"));
1089 }
1090
1091 make_cleanup_py_decref (children);
1092
1093 if (!PyIter_Check (children))
1094 error (_("Returned value is not iterable"));
1095
1096 Py_XDECREF (var->child_iter);
1097 var->child_iter = PyObject_GetIter (children);
1098 if (!var->child_iter)
1099 {
1100 gdbpy_print_stack ();
1101 error (_("Could not get children iterator"));
1102 }
1103
1104 Py_XDECREF (var->saved_item);
1105 var->saved_item = NULL;
1106
1107 i = 0;
1108 }
1109 else
1110 i = VEC_length (varobj_p, var->children);
1111
1112 /* We ask for one extra child, so that MI can report whether there
1113 are more children. */
1114 for (; to < 0 || i < to + 1; ++i)
1115 {
1116 PyObject *item;
1117 int force_done = 0;
1118
1119 /* See if there was a leftover from last time. */
1120 if (var->saved_item)
1121 {
1122 item = var->saved_item;
1123 var->saved_item = NULL;
1124 }
1125 else
1126 item = PyIter_Next (var->child_iter);
1127
1128 if (!item)
1129 {
1130 /* Normal end of iteration. */
1131 if (!PyErr_Occurred ())
1132 break;
1133
1134 /* If we got a memory error, just use the text as the
1135 item. */
1136 if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
1137 {
1138 PyObject *type, *value, *trace;
1139 char *name_str, *value_str;
1140
1141 PyErr_Fetch (&type, &value, &trace);
1142 value_str = gdbpy_exception_to_string (type, value);
1143 Py_XDECREF (type);
1144 Py_XDECREF (value);
1145 Py_XDECREF (trace);
1146 if (!value_str)
1147 {
1148 gdbpy_print_stack ();
1149 break;
1150 }
1151
1152 name_str = xstrprintf ("<error at %d>", i);
1153 item = Py_BuildValue ("(ss)", name_str, value_str);
1154 xfree (name_str);
1155 xfree (value_str);
1156 if (!item)
1157 {
1158 gdbpy_print_stack ();
1159 break;
1160 }
1161
1162 force_done = 1;
1163 }
1164 else
1165 {
1166 /* Any other kind of error. */
1167 gdbpy_print_stack ();
1168 break;
1169 }
1170 }
1171
1172 /* We don't want to push the extra child on any report list. */
1173 if (to < 0 || i < to)
1174 {
1175 PyObject *py_v;
1176 const char *name;
1177 struct value *v;
1178 struct cleanup *inner;
1179 int can_mention = from < 0 || i >= from;
1180
1181 inner = make_cleanup_py_decref (item);
1182
1183 if (!PyArg_ParseTuple (item, "sO", &name, &py_v))
1184 {
1185 gdbpy_print_stack ();
1186 error (_("Invalid item from the child list"));
1187 }
1188
1189 v = convert_value_from_python (py_v);
1190 if (v == NULL)
1191 gdbpy_print_stack ();
1192 install_dynamic_child (var, can_mention ? changed : NULL,
1193 can_mention ? new : NULL,
1194 can_mention ? unchanged : NULL,
1195 can_mention ? cchanged : NULL, i, name, v);
1196 do_cleanups (inner);
1197 }
1198 else
1199 {
1200 Py_XDECREF (var->saved_item);
1201 var->saved_item = item;
1202
1203 /* We want to truncate the child list just before this
1204 element. */
1205 break;
1206 }
1207
1208 if (force_done)
1209 break;
1210 }
1211
1212 if (i < VEC_length (varobj_p, var->children))
1213 {
1214 int j;
1215
1216 *cchanged = 1;
1217 for (j = i; j < VEC_length (varobj_p, var->children); ++j)
1218 varobj_delete (VEC_index (varobj_p, var->children, j), NULL, 0);
1219 VEC_truncate (varobj_p, var->children, i);
1220 }
1221
1222 /* If there are fewer children than requested, note that the list of
1223 children changed. */
1224 if (to >= 0 && VEC_length (varobj_p, var->children) < to)
1225 *cchanged = 1;
1226
1227 var->num_children = VEC_length (varobj_p, var->children);
1228
1229 do_cleanups (back_to);
1230
1231 return 1;
1232 #else
1233 gdb_assert (0 && "should never be called if Python is not enabled");
1234 #endif
1235 }
1236
1237 int
1238 varobj_get_num_children (struct varobj *var)
1239 {
1240 if (var->num_children == -1)
1241 {
1242 if (var->pretty_printer)
1243 {
1244 int dummy;
1245
1246 /* If we have a dynamic varobj, don't report -1 children.
1247 So, try to fetch some children first. */
1248 update_dynamic_varobj_children (var, NULL, NULL, NULL, &dummy,
1249 0, 0, 0);
1250 }
1251 else
1252 var->num_children = number_of_children (var);
1253 }
1254
1255 return var->num_children >= 0 ? var->num_children : 0;
1256 }
1257
1258 /* Creates a list of the immediate children of a variable object;
1259 the return code is the number of such children or -1 on error. */
1260
1261 VEC (varobj_p)*
1262 varobj_list_children (struct varobj *var, int *from, int *to)
1263 {
1264 char *name;
1265 int i, children_changed;
1266
1267 var->children_requested = 1;
1268
1269 if (var->pretty_printer)
1270 {
1271 /* This, in theory, can result in the number of children changing without
1272 frontend noticing. But well, calling -var-list-children on the same
1273 varobj twice is not something a sane frontend would do. */
1274 update_dynamic_varobj_children (var, NULL, NULL, NULL, &children_changed,
1275 0, 0, *to);
1276 restrict_range (var->children, from, to);
1277 return var->children;
1278 }
1279
1280 if (var->num_children == -1)
1281 var->num_children = number_of_children (var);
1282
1283 /* If that failed, give up. */
1284 if (var->num_children == -1)
1285 return var->children;
1286
1287 /* If we're called when the list of children is not yet initialized,
1288 allocate enough elements in it. */
1289 while (VEC_length (varobj_p, var->children) < var->num_children)
1290 VEC_safe_push (varobj_p, var->children, NULL);
1291
1292 for (i = 0; i < var->num_children; i++)
1293 {
1294 varobj_p existing = VEC_index (varobj_p, var->children, i);
1295
1296 if (existing == NULL)
1297 {
1298 /* Either it's the first call to varobj_list_children for
1299 this variable object, and the child was never created,
1300 or it was explicitly deleted by the client. */
1301 name = name_of_child (var, i);
1302 existing = create_child (var, i, name);
1303 VEC_replace (varobj_p, var->children, i, existing);
1304 }
1305 }
1306
1307 restrict_range (var->children, from, to);
1308 return var->children;
1309 }
1310
1311 #if HAVE_PYTHON
1312
1313 static struct varobj *
1314 varobj_add_child (struct varobj *var, const char *name, struct value *value)
1315 {
1316 varobj_p v = create_child_with_value (var,
1317 VEC_length (varobj_p, var->children),
1318 name, value);
1319
1320 VEC_safe_push (varobj_p, var->children, v);
1321 return v;
1322 }
1323
1324 #endif /* HAVE_PYTHON */
1325
1326 /* Obtain the type of an object Variable as a string similar to the one gdb
1327 prints on the console. */
1328
1329 char *
1330 varobj_get_type (struct varobj *var)
1331 {
1332 /* For the "fake" variables, do not return a type. (It's type is
1333 NULL, too.)
1334 Do not return a type for invalid variables as well. */
1335 if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
1336 return NULL;
1337
1338 return type_to_string (var->type);
1339 }
1340
1341 /* Obtain the type of an object variable. */
1342
1343 struct type *
1344 varobj_get_gdb_type (struct varobj *var)
1345 {
1346 return var->type;
1347 }
1348
1349 /* Is VAR a path expression parent, i.e., can it be used to construct
1350 a valid path expression? */
1351
1352 static int
1353 is_path_expr_parent (struct varobj *var)
1354 {
1355 struct type *type;
1356
1357 /* "Fake" children are not path_expr parents. */
1358 if (CPLUS_FAKE_CHILD (var))
1359 return 0;
1360
1361 type = get_value_type (var);
1362
1363 /* Anonymous unions and structs are also not path_expr parents. */
1364 return !((TYPE_CODE (type) == TYPE_CODE_STRUCT
1365 || TYPE_CODE (type) == TYPE_CODE_UNION)
1366 && TYPE_NAME (type) == NULL);
1367 }
1368
1369 /* Return the path expression parent for VAR. */
1370
1371 static struct varobj *
1372 get_path_expr_parent (struct varobj *var)
1373 {
1374 struct varobj *parent = var;
1375
1376 while (!is_root_p (parent) && !is_path_expr_parent (parent))
1377 parent = parent->parent;
1378
1379 return parent;
1380 }
1381
1382 /* Return a pointer to the full rooted expression of varobj VAR.
1383 If it has not been computed yet, compute it. */
1384 char *
1385 varobj_get_path_expr (struct varobj *var)
1386 {
1387 if (var->path_expr != NULL)
1388 return var->path_expr;
1389 else
1390 {
1391 /* For root varobjs, we initialize path_expr
1392 when creating varobj, so here it should be
1393 child varobj. */
1394 gdb_assert (!is_root_p (var));
1395 return (*var->root->lang->path_expr_of_child) (var);
1396 }
1397 }
1398
1399 enum varobj_languages
1400 varobj_get_language (struct varobj *var)
1401 {
1402 return variable_language (var);
1403 }
1404
1405 int
1406 varobj_get_attributes (struct varobj *var)
1407 {
1408 int attributes = 0;
1409
1410 if (varobj_editable_p (var))
1411 /* FIXME: define masks for attributes. */
1412 attributes |= 0x00000001; /* Editable */
1413
1414 return attributes;
1415 }
1416
1417 int
1418 varobj_pretty_printed_p (struct varobj *var)
1419 {
1420 return var->pretty_printer != NULL;
1421 }
1422
1423 char *
1424 varobj_get_formatted_value (struct varobj *var,
1425 enum varobj_display_formats format)
1426 {
1427 return my_value_of_variable (var, format);
1428 }
1429
1430 char *
1431 varobj_get_value (struct varobj *var)
1432 {
1433 return my_value_of_variable (var, var->format);
1434 }
1435
1436 /* Set the value of an object variable (if it is editable) to the
1437 value of the given expression. */
1438 /* Note: Invokes functions that can call error(). */
1439
1440 int
1441 varobj_set_value (struct varobj *var, char *expression)
1442 {
1443 struct value *val = NULL; /* Initialize to keep gcc happy. */
1444 /* The argument "expression" contains the variable's new value.
1445 We need to first construct a legal expression for this -- ugh! */
1446 /* Does this cover all the bases? */
1447 struct expression *exp;
1448 struct value *value = NULL; /* Initialize to keep gcc happy. */
1449 int saved_input_radix = input_radix;
1450 char *s = expression;
1451 volatile struct gdb_exception except;
1452
1453 gdb_assert (varobj_editable_p (var));
1454
1455 input_radix = 10; /* ALWAYS reset to decimal temporarily. */
1456 exp = parse_exp_1 (&s, 0, 0);
1457 TRY_CATCH (except, RETURN_MASK_ERROR)
1458 {
1459 value = evaluate_expression (exp);
1460 }
1461
1462 if (except.reason < 0)
1463 {
1464 /* We cannot proceed without a valid expression. */
1465 xfree (exp);
1466 return 0;
1467 }
1468
1469 /* All types that are editable must also be changeable. */
1470 gdb_assert (varobj_value_is_changeable_p (var));
1471
1472 /* The value of a changeable variable object must not be lazy. */
1473 gdb_assert (!value_lazy (var->value));
1474
1475 /* Need to coerce the input. We want to check if the
1476 value of the variable object will be different
1477 after assignment, and the first thing value_assign
1478 does is coerce the input.
1479 For example, if we are assigning an array to a pointer variable we
1480 should compare the pointer with the array's address, not with the
1481 array's content. */
1482 value = coerce_array (value);
1483
1484 /* The new value may be lazy. value_assign, or
1485 rather value_contents, will take care of this. */
1486 TRY_CATCH (except, RETURN_MASK_ERROR)
1487 {
1488 val = value_assign (var->value, value);
1489 }
1490
1491 if (except.reason < 0)
1492 return 0;
1493
1494 /* If the value has changed, record it, so that next -var-update can
1495 report this change. If a variable had a value of '1', we've set it
1496 to '333' and then set again to '1', when -var-update will report this
1497 variable as changed -- because the first assignment has set the
1498 'updated' flag. There's no need to optimize that, because return value
1499 of -var-update should be considered an approximation. */
1500 var->updated = install_new_value (var, val, 0 /* Compare values. */);
1501 input_radix = saved_input_radix;
1502 return 1;
1503 }
1504
1505 #if HAVE_PYTHON
1506
1507 /* A helper function to install a constructor function and visualizer
1508 in a varobj. */
1509
1510 static void
1511 install_visualizer (struct varobj *var, PyObject *constructor,
1512 PyObject *visualizer)
1513 {
1514 Py_XDECREF (var->constructor);
1515 var->constructor = constructor;
1516
1517 Py_XDECREF (var->pretty_printer);
1518 var->pretty_printer = visualizer;
1519
1520 Py_XDECREF (var->child_iter);
1521 var->child_iter = NULL;
1522 }
1523
1524 /* Install the default visualizer for VAR. */
1525
1526 static void
1527 install_default_visualizer (struct varobj *var)
1528 {
1529 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1530 if (CPLUS_FAKE_CHILD (var))
1531 return;
1532
1533 if (pretty_printing)
1534 {
1535 PyObject *pretty_printer = NULL;
1536
1537 if (var->value)
1538 {
1539 pretty_printer = gdbpy_get_varobj_pretty_printer (var->value);
1540 if (! pretty_printer)
1541 {
1542 gdbpy_print_stack ();
1543 error (_("Cannot instantiate printer for default visualizer"));
1544 }
1545 }
1546
1547 if (pretty_printer == Py_None)
1548 {
1549 Py_DECREF (pretty_printer);
1550 pretty_printer = NULL;
1551 }
1552
1553 install_visualizer (var, NULL, pretty_printer);
1554 }
1555 }
1556
1557 /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
1558 make a new object. */
1559
1560 static void
1561 construct_visualizer (struct varobj *var, PyObject *constructor)
1562 {
1563 PyObject *pretty_printer;
1564
1565 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1566 if (CPLUS_FAKE_CHILD (var))
1567 return;
1568
1569 Py_INCREF (constructor);
1570 if (constructor == Py_None)
1571 pretty_printer = NULL;
1572 else
1573 {
1574 pretty_printer = instantiate_pretty_printer (constructor, var->value);
1575 if (! pretty_printer)
1576 {
1577 gdbpy_print_stack ();
1578 Py_DECREF (constructor);
1579 constructor = Py_None;
1580 Py_INCREF (constructor);
1581 }
1582
1583 if (pretty_printer == Py_None)
1584 {
1585 Py_DECREF (pretty_printer);
1586 pretty_printer = NULL;
1587 }
1588 }
1589
1590 install_visualizer (var, constructor, pretty_printer);
1591 }
1592
1593 #endif /* HAVE_PYTHON */
1594
1595 /* A helper function for install_new_value. This creates and installs
1596 a visualizer for VAR, if appropriate. */
1597
1598 static void
1599 install_new_value_visualizer (struct varobj *var)
1600 {
1601 #if HAVE_PYTHON
1602 /* If the constructor is None, then we want the raw value. If VAR
1603 does not have a value, just skip this. */
1604 if (var->constructor != Py_None && var->value)
1605 {
1606 struct cleanup *cleanup;
1607
1608 cleanup = varobj_ensure_python_env (var);
1609
1610 if (!var->constructor)
1611 install_default_visualizer (var);
1612 else
1613 construct_visualizer (var, var->constructor);
1614
1615 do_cleanups (cleanup);
1616 }
1617 #else
1618 /* Do nothing. */
1619 #endif
1620 }
1621
1622 /* Assign a new value to a variable object. If INITIAL is non-zero,
1623 this is the first assignement after the variable object was just
1624 created, or changed type. In that case, just assign the value
1625 and return 0.
1626 Otherwise, assign the new value, and return 1 if the value is
1627 different from the current one, 0 otherwise. The comparison is
1628 done on textual representation of value. Therefore, some types
1629 need not be compared. E.g. for structures the reported value is
1630 always "{...}", so no comparison is necessary here. If the old
1631 value was NULL and new one is not, or vice versa, we always return 1.
1632
1633 The VALUE parameter should not be released -- the function will
1634 take care of releasing it when needed. */
1635 static int
1636 install_new_value (struct varobj *var, struct value *value, int initial)
1637 {
1638 int changeable;
1639 int need_to_fetch;
1640 int changed = 0;
1641 int intentionally_not_fetched = 0;
1642 char *print_value = NULL;
1643
1644 /* We need to know the varobj's type to decide if the value should
1645 be fetched or not. C++ fake children (public/protected/private)
1646 don't have a type. */
1647 gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
1648 changeable = varobj_value_is_changeable_p (var);
1649
1650 /* If the type has custom visualizer, we consider it to be always
1651 changeable. FIXME: need to make sure this behaviour will not
1652 mess up read-sensitive values. */
1653 if (var->pretty_printer)
1654 changeable = 1;
1655
1656 need_to_fetch = changeable;
1657
1658 /* We are not interested in the address of references, and given
1659 that in C++ a reference is not rebindable, it cannot
1660 meaningfully change. So, get hold of the real value. */
1661 if (value)
1662 value = coerce_ref (value);
1663
1664 if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
1665 /* For unions, we need to fetch the value implicitly because
1666 of implementation of union member fetch. When gdb
1667 creates a value for a field and the value of the enclosing
1668 structure is not lazy, it immediately copies the necessary
1669 bytes from the enclosing values. If the enclosing value is
1670 lazy, the call to value_fetch_lazy on the field will read
1671 the data from memory. For unions, that means we'll read the
1672 same memory more than once, which is not desirable. So
1673 fetch now. */
1674 need_to_fetch = 1;
1675
1676 /* The new value might be lazy. If the type is changeable,
1677 that is we'll be comparing values of this type, fetch the
1678 value now. Otherwise, on the next update the old value
1679 will be lazy, which means we've lost that old value. */
1680 if (need_to_fetch && value && value_lazy (value))
1681 {
1682 struct varobj *parent = var->parent;
1683 int frozen = var->frozen;
1684
1685 for (; !frozen && parent; parent = parent->parent)
1686 frozen |= parent->frozen;
1687
1688 if (frozen && initial)
1689 {
1690 /* For variables that are frozen, or are children of frozen
1691 variables, we don't do fetch on initial assignment.
1692 For non-initial assignemnt we do the fetch, since it means we're
1693 explicitly asked to compare the new value with the old one. */
1694 intentionally_not_fetched = 1;
1695 }
1696 else
1697 {
1698 volatile struct gdb_exception except;
1699
1700 TRY_CATCH (except, RETURN_MASK_ERROR)
1701 {
1702 value_fetch_lazy (value);
1703 }
1704
1705 if (except.reason < 0)
1706 {
1707 /* Set the value to NULL, so that for the next -var-update,
1708 we don't try to compare the new value with this value,
1709 that we couldn't even read. */
1710 value = NULL;
1711 }
1712 }
1713 }
1714
1715 /* Get a reference now, before possibly passing it to any Python
1716 code that might release it. */
1717 if (value != NULL)
1718 value_incref (value);
1719
1720 /* Below, we'll be comparing string rendering of old and new
1721 values. Don't get string rendering if the value is
1722 lazy -- if it is, the code above has decided that the value
1723 should not be fetched. */
1724 if (value && !value_lazy (value) && !var->pretty_printer)
1725 print_value = value_get_print_value (value, var->format, var);
1726
1727 /* If the type is changeable, compare the old and the new values.
1728 If this is the initial assignment, we don't have any old value
1729 to compare with. */
1730 if (!initial && changeable)
1731 {
1732 /* If the value of the varobj was changed by -var-set-value,
1733 then the value in the varobj and in the target is the same.
1734 However, that value is different from the value that the
1735 varobj had after the previous -var-update. So need to the
1736 varobj as changed. */
1737 if (var->updated)
1738 {
1739 changed = 1;
1740 }
1741 else if (! var->pretty_printer)
1742 {
1743 /* Try to compare the values. That requires that both
1744 values are non-lazy. */
1745 if (var->not_fetched && value_lazy (var->value))
1746 {
1747 /* This is a frozen varobj and the value was never read.
1748 Presumably, UI shows some "never read" indicator.
1749 Now that we've fetched the real value, we need to report
1750 this varobj as changed so that UI can show the real
1751 value. */
1752 changed = 1;
1753 }
1754 else if (var->value == NULL && value == NULL)
1755 /* Equal. */
1756 ;
1757 else if (var->value == NULL || value == NULL)
1758 {
1759 changed = 1;
1760 }
1761 else
1762 {
1763 gdb_assert (!value_lazy (var->value));
1764 gdb_assert (!value_lazy (value));
1765
1766 gdb_assert (var->print_value != NULL && print_value != NULL);
1767 if (strcmp (var->print_value, print_value) != 0)
1768 changed = 1;
1769 }
1770 }
1771 }
1772
1773 if (!initial && !changeable)
1774 {
1775 /* For values that are not changeable, we don't compare the values.
1776 However, we want to notice if a value was not NULL and now is NULL,
1777 or vise versa, so that we report when top-level varobjs come in scope
1778 and leave the scope. */
1779 changed = (var->value != NULL) != (value != NULL);
1780 }
1781
1782 /* We must always keep the new value, since children depend on it. */
1783 if (var->value != NULL && var->value != value)
1784 value_free (var->value);
1785 var->value = value;
1786 if (value && value_lazy (value) && intentionally_not_fetched)
1787 var->not_fetched = 1;
1788 else
1789 var->not_fetched = 0;
1790 var->updated = 0;
1791
1792 install_new_value_visualizer (var);
1793
1794 /* If we installed a pretty-printer, re-compare the printed version
1795 to see if the variable changed. */
1796 if (var->pretty_printer)
1797 {
1798 xfree (print_value);
1799 print_value = value_get_print_value (var->value, var->format, var);
1800 if ((var->print_value == NULL && print_value != NULL)
1801 || (var->print_value != NULL && print_value == NULL)
1802 || (var->print_value != NULL && print_value != NULL
1803 && strcmp (var->print_value, print_value) != 0))
1804 changed = 1;
1805 }
1806 if (var->print_value)
1807 xfree (var->print_value);
1808 var->print_value = print_value;
1809
1810 gdb_assert (!var->value || value_type (var->value));
1811
1812 return changed;
1813 }
1814
1815 /* Return the requested range for a varobj. VAR is the varobj. FROM
1816 and TO are out parameters; *FROM and *TO will be set to the
1817 selected sub-range of VAR. If no range was selected using
1818 -var-set-update-range, then both will be -1. */
1819 void
1820 varobj_get_child_range (struct varobj *var, int *from, int *to)
1821 {
1822 *from = var->from;
1823 *to = var->to;
1824 }
1825
1826 /* Set the selected sub-range of children of VAR to start at index
1827 FROM and end at index TO. If either FROM or TO is less than zero,
1828 this is interpreted as a request for all children. */
1829 void
1830 varobj_set_child_range (struct varobj *var, int from, int to)
1831 {
1832 var->from = from;
1833 var->to = to;
1834 }
1835
1836 void
1837 varobj_set_visualizer (struct varobj *var, const char *visualizer)
1838 {
1839 #if HAVE_PYTHON
1840 PyObject *mainmod, *globals, *constructor;
1841 struct cleanup *back_to;
1842
1843 back_to = varobj_ensure_python_env (var);
1844
1845 mainmod = PyImport_AddModule ("__main__");
1846 globals = PyModule_GetDict (mainmod);
1847 Py_INCREF (globals);
1848 make_cleanup_py_decref (globals);
1849
1850 constructor = PyRun_String (visualizer, Py_eval_input, globals, globals);
1851
1852 if (! constructor)
1853 {
1854 gdbpy_print_stack ();
1855 error (_("Could not evaluate visualizer expression: %s"), visualizer);
1856 }
1857
1858 construct_visualizer (var, constructor);
1859 Py_XDECREF (constructor);
1860
1861 /* If there are any children now, wipe them. */
1862 varobj_delete (var, NULL, 1 /* children only */);
1863 var->num_children = -1;
1864
1865 do_cleanups (back_to);
1866 #else
1867 error (_("Python support required"));
1868 #endif
1869 }
1870
1871 /* If NEW_VALUE is the new value of the given varobj (var), return
1872 non-zero if var has mutated. In other words, if the type of
1873 the new value is different from the type of the varobj's old
1874 value.
1875
1876 NEW_VALUE may be NULL, if the varobj is now out of scope. */
1877
1878 static int
1879 varobj_value_has_mutated (struct varobj *var, struct value *new_value,
1880 struct type *new_type)
1881 {
1882 /* If we haven't previously computed the number of children in var,
1883 it does not matter from the front-end's perspective whether
1884 the type has mutated or not. For all intents and purposes,
1885 it has not mutated. */
1886 if (var->num_children < 0)
1887 return 0;
1888
1889 if (var->root->lang->value_has_mutated)
1890 return var->root->lang->value_has_mutated (var, new_value, new_type);
1891 else
1892 return 0;
1893 }
1894
1895 /* Update the values for a variable and its children. This is a
1896 two-pronged attack. First, re-parse the value for the root's
1897 expression to see if it's changed. Then go all the way
1898 through its children, reconstructing them and noting if they've
1899 changed.
1900
1901 The EXPLICIT parameter specifies if this call is result
1902 of MI request to update this specific variable, or
1903 result of implicit -var-update *. For implicit request, we don't
1904 update frozen variables.
1905
1906 NOTE: This function may delete the caller's varobj. If it
1907 returns TYPE_CHANGED, then it has done this and VARP will be modified
1908 to point to the new varobj. */
1909
1910 VEC(varobj_update_result) *
1911 varobj_update (struct varobj **varp, int explicit)
1912 {
1913 int changed = 0;
1914 int type_changed = 0;
1915 int i;
1916 struct value *new;
1917 VEC (varobj_update_result) *stack = NULL;
1918 VEC (varobj_update_result) *result = NULL;
1919
1920 /* Frozen means frozen -- we don't check for any change in
1921 this varobj, including its going out of scope, or
1922 changing type. One use case for frozen varobjs is
1923 retaining previously evaluated expressions, and we don't
1924 want them to be reevaluated at all. */
1925 if (!explicit && (*varp)->frozen)
1926 return result;
1927
1928 if (!(*varp)->root->is_valid)
1929 {
1930 varobj_update_result r = {0};
1931
1932 r.varobj = *varp;
1933 r.status = VAROBJ_INVALID;
1934 VEC_safe_push (varobj_update_result, result, &r);
1935 return result;
1936 }
1937
1938 if ((*varp)->root->rootvar == *varp)
1939 {
1940 varobj_update_result r = {0};
1941
1942 r.varobj = *varp;
1943 r.status = VAROBJ_IN_SCOPE;
1944
1945 /* Update the root variable. value_of_root can return NULL
1946 if the variable is no longer around, i.e. we stepped out of
1947 the frame in which a local existed. We are letting the
1948 value_of_root variable dispose of the varobj if the type
1949 has changed. */
1950 new = value_of_root (varp, &type_changed);
1951 r.varobj = *varp;
1952
1953 r.type_changed = type_changed;
1954 if (install_new_value ((*varp), new, type_changed))
1955 r.changed = 1;
1956
1957 if (new == NULL)
1958 r.status = VAROBJ_NOT_IN_SCOPE;
1959 r.value_installed = 1;
1960
1961 if (r.status == VAROBJ_NOT_IN_SCOPE)
1962 {
1963 if (r.type_changed || r.changed)
1964 VEC_safe_push (varobj_update_result, result, &r);
1965 return result;
1966 }
1967
1968 VEC_safe_push (varobj_update_result, stack, &r);
1969 }
1970 else
1971 {
1972 varobj_update_result r = {0};
1973
1974 r.varobj = *varp;
1975 VEC_safe_push (varobj_update_result, stack, &r);
1976 }
1977
1978 /* Walk through the children, reconstructing them all. */
1979 while (!VEC_empty (varobj_update_result, stack))
1980 {
1981 varobj_update_result r = *(VEC_last (varobj_update_result, stack));
1982 struct varobj *v = r.varobj;
1983
1984 VEC_pop (varobj_update_result, stack);
1985
1986 /* Update this variable, unless it's a root, which is already
1987 updated. */
1988 if (!r.value_installed)
1989 {
1990 struct type *new_type;
1991
1992 new = value_of_child (v->parent, v->index);
1993 if (new)
1994 new_type = value_type (new);
1995 else
1996 new_type = v->root->lang->type_of_child (v->parent, v->index);
1997
1998 if (varobj_value_has_mutated (v, new, new_type))
1999 {
2000 /* The children are no longer valid; delete them now.
2001 Report the fact that its type changed as well. */
2002 varobj_delete (v, NULL, 1 /* only_children */);
2003 v->num_children = -1;
2004 v->to = -1;
2005 v->from = -1;
2006 v->type = new_type;
2007 r.type_changed = 1;
2008 }
2009
2010 if (install_new_value (v, new, r.type_changed))
2011 {
2012 r.changed = 1;
2013 v->updated = 0;
2014 }
2015 }
2016
2017 /* We probably should not get children of a varobj that has a
2018 pretty-printer, but for which -var-list-children was never
2019 invoked. */
2020 if (v->pretty_printer)
2021 {
2022 VEC (varobj_p) *changed = 0, *new = 0, *unchanged = 0;
2023 int i, children_changed = 0;
2024
2025 if (v->frozen)
2026 continue;
2027
2028 if (!v->children_requested)
2029 {
2030 int dummy;
2031
2032 /* If we initially did not have potential children, but
2033 now we do, consider the varobj as changed.
2034 Otherwise, if children were never requested, consider
2035 it as unchanged -- presumably, such varobj is not yet
2036 expanded in the UI, so we need not bother getting
2037 it. */
2038 if (!varobj_has_more (v, 0))
2039 {
2040 update_dynamic_varobj_children (v, NULL, NULL, NULL,
2041 &dummy, 0, 0, 0);
2042 if (varobj_has_more (v, 0))
2043 r.changed = 1;
2044 }
2045
2046 if (r.changed)
2047 VEC_safe_push (varobj_update_result, result, &r);
2048
2049 continue;
2050 }
2051
2052 /* If update_dynamic_varobj_children returns 0, then we have
2053 a non-conforming pretty-printer, so we skip it. */
2054 if (update_dynamic_varobj_children (v, &changed, &new, &unchanged,
2055 &children_changed, 1,
2056 v->from, v->to))
2057 {
2058 if (children_changed || new)
2059 {
2060 r.children_changed = 1;
2061 r.new = new;
2062 }
2063 /* Push in reverse order so that the first child is
2064 popped from the work stack first, and so will be
2065 added to result first. This does not affect
2066 correctness, just "nicer". */
2067 for (i = VEC_length (varobj_p, changed) - 1; i >= 0; --i)
2068 {
2069 varobj_p tmp = VEC_index (varobj_p, changed, i);
2070 varobj_update_result r = {0};
2071
2072 r.varobj = tmp;
2073 r.changed = 1;
2074 r.value_installed = 1;
2075 VEC_safe_push (varobj_update_result, stack, &r);
2076 }
2077 for (i = VEC_length (varobj_p, unchanged) - 1; i >= 0; --i)
2078 {
2079 varobj_p tmp = VEC_index (varobj_p, unchanged, i);
2080
2081 if (!tmp->frozen)
2082 {
2083 varobj_update_result r = {0};
2084
2085 r.varobj = tmp;
2086 r.value_installed = 1;
2087 VEC_safe_push (varobj_update_result, stack, &r);
2088 }
2089 }
2090 if (r.changed || r.children_changed)
2091 VEC_safe_push (varobj_update_result, result, &r);
2092
2093 /* Free CHANGED and UNCHANGED, but not NEW, because NEW
2094 has been put into the result vector. */
2095 VEC_free (varobj_p, changed);
2096 VEC_free (varobj_p, unchanged);
2097
2098 continue;
2099 }
2100 }
2101
2102 /* Push any children. Use reverse order so that the first
2103 child is popped from the work stack first, and so
2104 will be added to result first. This does not
2105 affect correctness, just "nicer". */
2106 for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
2107 {
2108 varobj_p c = VEC_index (varobj_p, v->children, i);
2109
2110 /* Child may be NULL if explicitly deleted by -var-delete. */
2111 if (c != NULL && !c->frozen)
2112 {
2113 varobj_update_result r = {0};
2114
2115 r.varobj = c;
2116 VEC_safe_push (varobj_update_result, stack, &r);
2117 }
2118 }
2119
2120 if (r.changed || r.type_changed)
2121 VEC_safe_push (varobj_update_result, result, &r);
2122 }
2123
2124 VEC_free (varobj_update_result, stack);
2125
2126 return result;
2127 }
2128 \f
2129
2130 /* Helper functions */
2131
2132 /*
2133 * Variable object construction/destruction
2134 */
2135
2136 static int
2137 delete_variable (struct cpstack **resultp, struct varobj *var,
2138 int only_children_p)
2139 {
2140 int delcount = 0;
2141
2142 delete_variable_1 (resultp, &delcount, var,
2143 only_children_p, 1 /* remove_from_parent_p */ );
2144
2145 return delcount;
2146 }
2147
2148 /* Delete the variable object VAR and its children. */
2149 /* IMPORTANT NOTE: If we delete a variable which is a child
2150 and the parent is not removed we dump core. It must be always
2151 initially called with remove_from_parent_p set. */
2152 static void
2153 delete_variable_1 (struct cpstack **resultp, int *delcountp,
2154 struct varobj *var, int only_children_p,
2155 int remove_from_parent_p)
2156 {
2157 int i;
2158
2159 /* Delete any children of this variable, too. */
2160 for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
2161 {
2162 varobj_p child = VEC_index (varobj_p, var->children, i);
2163
2164 if (!child)
2165 continue;
2166 if (!remove_from_parent_p)
2167 child->parent = NULL;
2168 delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
2169 }
2170 VEC_free (varobj_p, var->children);
2171
2172 /* if we were called to delete only the children we are done here. */
2173 if (only_children_p)
2174 return;
2175
2176 /* Otherwise, add it to the list of deleted ones and proceed to do so. */
2177 /* If the name is null, this is a temporary variable, that has not
2178 yet been installed, don't report it, it belongs to the caller... */
2179 if (var->obj_name != NULL)
2180 {
2181 cppush (resultp, xstrdup (var->obj_name));
2182 *delcountp = *delcountp + 1;
2183 }
2184
2185 /* If this variable has a parent, remove it from its parent's list. */
2186 /* OPTIMIZATION: if the parent of this variable is also being deleted,
2187 (as indicated by remove_from_parent_p) we don't bother doing an
2188 expensive list search to find the element to remove when we are
2189 discarding the list afterwards. */
2190 if ((remove_from_parent_p) && (var->parent != NULL))
2191 {
2192 VEC_replace (varobj_p, var->parent->children, var->index, NULL);
2193 }
2194
2195 if (var->obj_name != NULL)
2196 uninstall_variable (var);
2197
2198 /* Free memory associated with this variable. */
2199 free_variable (var);
2200 }
2201
2202 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
2203 static int
2204 install_variable (struct varobj *var)
2205 {
2206 struct vlist *cv;
2207 struct vlist *newvl;
2208 const char *chp;
2209 unsigned int index = 0;
2210 unsigned int i = 1;
2211
2212 for (chp = var->obj_name; *chp; chp++)
2213 {
2214 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
2215 }
2216
2217 cv = *(varobj_table + index);
2218 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
2219 cv = cv->next;
2220
2221 if (cv != NULL)
2222 error (_("Duplicate variable object name"));
2223
2224 /* Add varobj to hash table. */
2225 newvl = xmalloc (sizeof (struct vlist));
2226 newvl->next = *(varobj_table + index);
2227 newvl->var = var;
2228 *(varobj_table + index) = newvl;
2229
2230 /* If root, add varobj to root list. */
2231 if (is_root_p (var))
2232 {
2233 /* Add to list of root variables. */
2234 if (rootlist == NULL)
2235 var->root->next = NULL;
2236 else
2237 var->root->next = rootlist;
2238 rootlist = var->root;
2239 }
2240
2241 return 1; /* OK */
2242 }
2243
2244 /* Unistall the object VAR. */
2245 static void
2246 uninstall_variable (struct varobj *var)
2247 {
2248 struct vlist *cv;
2249 struct vlist *prev;
2250 struct varobj_root *cr;
2251 struct varobj_root *prer;
2252 const char *chp;
2253 unsigned int index = 0;
2254 unsigned int i = 1;
2255
2256 /* Remove varobj from hash table. */
2257 for (chp = var->obj_name; *chp; chp++)
2258 {
2259 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
2260 }
2261
2262 cv = *(varobj_table + index);
2263 prev = NULL;
2264 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
2265 {
2266 prev = cv;
2267 cv = cv->next;
2268 }
2269
2270 if (varobjdebug)
2271 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
2272
2273 if (cv == NULL)
2274 {
2275 warning
2276 ("Assertion failed: Could not find variable object \"%s\" to delete",
2277 var->obj_name);
2278 return;
2279 }
2280
2281 if (prev == NULL)
2282 *(varobj_table + index) = cv->next;
2283 else
2284 prev->next = cv->next;
2285
2286 xfree (cv);
2287
2288 /* If root, remove varobj from root list. */
2289 if (is_root_p (var))
2290 {
2291 /* Remove from list of root variables. */
2292 if (rootlist == var->root)
2293 rootlist = var->root->next;
2294 else
2295 {
2296 prer = NULL;
2297 cr = rootlist;
2298 while ((cr != NULL) && (cr->rootvar != var))
2299 {
2300 prer = cr;
2301 cr = cr->next;
2302 }
2303 if (cr == NULL)
2304 {
2305 warning (_("Assertion failed: Could not find "
2306 "varobj \"%s\" in root list"),
2307 var->obj_name);
2308 return;
2309 }
2310 if (prer == NULL)
2311 rootlist = NULL;
2312 else
2313 prer->next = cr->next;
2314 }
2315 }
2316
2317 }
2318
2319 /* Create and install a child of the parent of the given name. */
2320 static struct varobj *
2321 create_child (struct varobj *parent, int index, char *name)
2322 {
2323 return create_child_with_value (parent, index, name,
2324 value_of_child (parent, index));
2325 }
2326
2327 /* Does CHILD represent a child with no name? This happens when
2328 the child is an anonmous struct or union and it has no field name
2329 in its parent variable.
2330
2331 This has already been determined by *_describe_child. The easiest
2332 thing to do is to compare the child's name with ANONYMOUS_*_NAME. */
2333
2334 static int
2335 is_anonymous_child (struct varobj *child)
2336 {
2337 return (strcmp (child->name, ANONYMOUS_STRUCT_NAME) == 0
2338 || strcmp (child->name, ANONYMOUS_UNION_NAME) == 0);
2339 }
2340
2341 static struct varobj *
2342 create_child_with_value (struct varobj *parent, int index, const char *name,
2343 struct value *value)
2344 {
2345 struct varobj *child;
2346 char *childs_name;
2347
2348 child = new_variable ();
2349
2350 /* Name is allocated by name_of_child. */
2351 /* FIXME: xstrdup should not be here. */
2352 child->name = xstrdup (name);
2353 child->index = index;
2354 child->parent = parent;
2355 child->root = parent->root;
2356
2357 if (is_anonymous_child (child))
2358 childs_name = xstrprintf ("%s.%d_anonymous", parent->obj_name, index);
2359 else
2360 childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
2361 child->obj_name = childs_name;
2362
2363 install_variable (child);
2364
2365 /* Compute the type of the child. Must do this before
2366 calling install_new_value. */
2367 if (value != NULL)
2368 /* If the child had no evaluation errors, var->value
2369 will be non-NULL and contain a valid type. */
2370 child->type = value_type (value);
2371 else
2372 /* Otherwise, we must compute the type. */
2373 child->type = (*child->root->lang->type_of_child) (child->parent,
2374 child->index);
2375 install_new_value (child, value, 1);
2376
2377 return child;
2378 }
2379 \f
2380
2381 /*
2382 * Miscellaneous utility functions.
2383 */
2384
2385 /* Allocate memory and initialize a new variable. */
2386 static struct varobj *
2387 new_variable (void)
2388 {
2389 struct varobj *var;
2390
2391 var = (struct varobj *) xmalloc (sizeof (struct varobj));
2392 var->name = NULL;
2393 var->path_expr = NULL;
2394 var->obj_name = NULL;
2395 var->index = -1;
2396 var->type = NULL;
2397 var->value = NULL;
2398 var->num_children = -1;
2399 var->parent = NULL;
2400 var->children = NULL;
2401 var->format = 0;
2402 var->root = NULL;
2403 var->updated = 0;
2404 var->print_value = NULL;
2405 var->frozen = 0;
2406 var->not_fetched = 0;
2407 var->children_requested = 0;
2408 var->from = -1;
2409 var->to = -1;
2410 var->constructor = 0;
2411 var->pretty_printer = 0;
2412 var->child_iter = 0;
2413 var->saved_item = 0;
2414
2415 return var;
2416 }
2417
2418 /* Allocate memory and initialize a new root variable. */
2419 static struct varobj *
2420 new_root_variable (void)
2421 {
2422 struct varobj *var = new_variable ();
2423
2424 var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));
2425 var->root->lang = NULL;
2426 var->root->exp = NULL;
2427 var->root->valid_block = NULL;
2428 var->root->frame = null_frame_id;
2429 var->root->floating = 0;
2430 var->root->rootvar = NULL;
2431 var->root->is_valid = 1;
2432
2433 return var;
2434 }
2435
2436 /* Free any allocated memory associated with VAR. */
2437 static void
2438 free_variable (struct varobj *var)
2439 {
2440 #if HAVE_PYTHON
2441 if (var->pretty_printer)
2442 {
2443 struct cleanup *cleanup = varobj_ensure_python_env (var);
2444 Py_XDECREF (var->constructor);
2445 Py_XDECREF (var->pretty_printer);
2446 Py_XDECREF (var->child_iter);
2447 Py_XDECREF (var->saved_item);
2448 do_cleanups (cleanup);
2449 }
2450 #endif
2451
2452 value_free (var->value);
2453
2454 /* Free the expression if this is a root variable. */
2455 if (is_root_p (var))
2456 {
2457 xfree (var->root->exp);
2458 xfree (var->root);
2459 }
2460
2461 xfree (var->name);
2462 xfree (var->obj_name);
2463 xfree (var->print_value);
2464 xfree (var->path_expr);
2465 xfree (var);
2466 }
2467
2468 static void
2469 do_free_variable_cleanup (void *var)
2470 {
2471 free_variable (var);
2472 }
2473
2474 static struct cleanup *
2475 make_cleanup_free_variable (struct varobj *var)
2476 {
2477 return make_cleanup (do_free_variable_cleanup, var);
2478 }
2479
2480 /* This returns the type of the variable. It also skips past typedefs
2481 to return the real type of the variable.
2482
2483 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
2484 except within get_target_type and get_type. */
2485 static struct type *
2486 get_type (struct varobj *var)
2487 {
2488 struct type *type;
2489
2490 type = var->type;
2491 if (type != NULL)
2492 type = check_typedef (type);
2493
2494 return type;
2495 }
2496
2497 /* Return the type of the value that's stored in VAR,
2498 or that would have being stored there if the
2499 value were accessible.
2500
2501 This differs from VAR->type in that VAR->type is always
2502 the true type of the expession in the source language.
2503 The return value of this function is the type we're
2504 actually storing in varobj, and using for displaying
2505 the values and for comparing previous and new values.
2506
2507 For example, top-level references are always stripped. */
2508 static struct type *
2509 get_value_type (struct varobj *var)
2510 {
2511 struct type *type;
2512
2513 if (var->value)
2514 type = value_type (var->value);
2515 else
2516 type = var->type;
2517
2518 type = check_typedef (type);
2519
2520 if (TYPE_CODE (type) == TYPE_CODE_REF)
2521 type = get_target_type (type);
2522
2523 type = check_typedef (type);
2524
2525 return type;
2526 }
2527
2528 /* This returns the target type (or NULL) of TYPE, also skipping
2529 past typedefs, just like get_type ().
2530
2531 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
2532 except within get_target_type and get_type. */
2533 static struct type *
2534 get_target_type (struct type *type)
2535 {
2536 if (type != NULL)
2537 {
2538 type = TYPE_TARGET_TYPE (type);
2539 if (type != NULL)
2540 type = check_typedef (type);
2541 }
2542
2543 return type;
2544 }
2545
2546 /* What is the default display for this variable? We assume that
2547 everything is "natural". Any exceptions? */
2548 static enum varobj_display_formats
2549 variable_default_display (struct varobj *var)
2550 {
2551 return FORMAT_NATURAL;
2552 }
2553
2554 /* FIXME: The following should be generic for any pointer. */
2555 static void
2556 cppush (struct cpstack **pstack, char *name)
2557 {
2558 struct cpstack *s;
2559
2560 s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
2561 s->name = name;
2562 s->next = *pstack;
2563 *pstack = s;
2564 }
2565
2566 /* FIXME: The following should be generic for any pointer. */
2567 static char *
2568 cppop (struct cpstack **pstack)
2569 {
2570 struct cpstack *s;
2571 char *v;
2572
2573 if ((*pstack)->name == NULL && (*pstack)->next == NULL)
2574 return NULL;
2575
2576 s = *pstack;
2577 v = s->name;
2578 *pstack = (*pstack)->next;
2579 xfree (s);
2580
2581 return v;
2582 }
2583 \f
2584 /*
2585 * Language-dependencies
2586 */
2587
2588 /* Common entry points */
2589
2590 /* Get the language of variable VAR. */
2591 static enum varobj_languages
2592 variable_language (struct varobj *var)
2593 {
2594 enum varobj_languages lang;
2595
2596 switch (var->root->exp->language_defn->la_language)
2597 {
2598 default:
2599 case language_c:
2600 lang = vlang_c;
2601 break;
2602 case language_cplus:
2603 lang = vlang_cplus;
2604 break;
2605 case language_java:
2606 lang = vlang_java;
2607 break;
2608 case language_ada:
2609 lang = vlang_ada;
2610 break;
2611 }
2612
2613 return lang;
2614 }
2615
2616 /* Return the number of children for a given variable.
2617 The result of this function is defined by the language
2618 implementation. The number of children returned by this function
2619 is the number of children that the user will see in the variable
2620 display. */
2621 static int
2622 number_of_children (struct varobj *var)
2623 {
2624 return (*var->root->lang->number_of_children) (var);
2625 }
2626
2627 /* What is the expression for the root varobj VAR? Returns a malloc'd
2628 string. */
2629 static char *
2630 name_of_variable (struct varobj *var)
2631 {
2632 return (*var->root->lang->name_of_variable) (var);
2633 }
2634
2635 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd
2636 string. */
2637 static char *
2638 name_of_child (struct varobj *var, int index)
2639 {
2640 return (*var->root->lang->name_of_child) (var, index);
2641 }
2642
2643 /* What is the ``struct value *'' of the root variable VAR?
2644 For floating variable object, evaluation can get us a value
2645 of different type from what is stored in varobj already. In
2646 that case:
2647 - *type_changed will be set to 1
2648 - old varobj will be freed, and new one will be
2649 created, with the same name.
2650 - *var_handle will be set to the new varobj
2651 Otherwise, *type_changed will be set to 0. */
2652 static struct value *
2653 value_of_root (struct varobj **var_handle, int *type_changed)
2654 {
2655 struct varobj *var;
2656
2657 if (var_handle == NULL)
2658 return NULL;
2659
2660 var = *var_handle;
2661
2662 /* This should really be an exception, since this should
2663 only get called with a root variable. */
2664
2665 if (!is_root_p (var))
2666 return NULL;
2667
2668 if (var->root->floating)
2669 {
2670 struct varobj *tmp_var;
2671 char *old_type, *new_type;
2672
2673 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
2674 USE_SELECTED_FRAME);
2675 if (tmp_var == NULL)
2676 {
2677 return NULL;
2678 }
2679 old_type = varobj_get_type (var);
2680 new_type = varobj_get_type (tmp_var);
2681 if (strcmp (old_type, new_type) == 0)
2682 {
2683 /* The expression presently stored inside var->root->exp
2684 remembers the locations of local variables relatively to
2685 the frame where the expression was created (in DWARF location
2686 button, for example). Naturally, those locations are not
2687 correct in other frames, so update the expression. */
2688
2689 struct expression *tmp_exp = var->root->exp;
2690
2691 var->root->exp = tmp_var->root->exp;
2692 tmp_var->root->exp = tmp_exp;
2693
2694 varobj_delete (tmp_var, NULL, 0);
2695 *type_changed = 0;
2696 }
2697 else
2698 {
2699 tmp_var->obj_name = xstrdup (var->obj_name);
2700 tmp_var->from = var->from;
2701 tmp_var->to = var->to;
2702 varobj_delete (var, NULL, 0);
2703
2704 install_variable (tmp_var);
2705 *var_handle = tmp_var;
2706 var = *var_handle;
2707 *type_changed = 1;
2708 }
2709 xfree (old_type);
2710 xfree (new_type);
2711 }
2712 else
2713 {
2714 *type_changed = 0;
2715 }
2716
2717 {
2718 struct value *value;
2719
2720 value = (*var->root->lang->value_of_root) (var_handle);
2721 if (var->value == NULL || value == NULL)
2722 {
2723 /* For root varobj-s, a NULL value indicates a scoping issue.
2724 So, nothing to do in terms of checking for mutations. */
2725 }
2726 else if (varobj_value_has_mutated (var, value, value_type (value)))
2727 {
2728 /* The type has mutated, so the children are no longer valid.
2729 Just delete them, and tell our caller that the type has
2730 changed. */
2731 varobj_delete (var, NULL, 1 /* only_children */);
2732 var->num_children = -1;
2733 var->to = -1;
2734 var->from = -1;
2735 *type_changed = 1;
2736 }
2737 return value;
2738 }
2739 }
2740
2741 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
2742 static struct value *
2743 value_of_child (struct varobj *parent, int index)
2744 {
2745 struct value *value;
2746
2747 value = (*parent->root->lang->value_of_child) (parent, index);
2748
2749 return value;
2750 }
2751
2752 /* GDB already has a command called "value_of_variable". Sigh. */
2753 static char *
2754 my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
2755 {
2756 if (var->root->is_valid)
2757 {
2758 if (var->pretty_printer)
2759 return value_get_print_value (var->value, var->format, var);
2760 return (*var->root->lang->value_of_variable) (var, format);
2761 }
2762 else
2763 return NULL;
2764 }
2765
2766 static char *
2767 value_get_print_value (struct value *value, enum varobj_display_formats format,
2768 struct varobj *var)
2769 {
2770 struct ui_file *stb;
2771 struct cleanup *old_chain;
2772 gdb_byte *thevalue = NULL;
2773 struct value_print_options opts;
2774 struct type *type = NULL;
2775 long len = 0;
2776 char *encoding = NULL;
2777 struct gdbarch *gdbarch = NULL;
2778 /* Initialize it just to avoid a GCC false warning. */
2779 CORE_ADDR str_addr = 0;
2780 int string_print = 0;
2781
2782 if (value == NULL)
2783 return NULL;
2784
2785 stb = mem_fileopen ();
2786 old_chain = make_cleanup_ui_file_delete (stb);
2787
2788 gdbarch = get_type_arch (value_type (value));
2789 #if HAVE_PYTHON
2790 {
2791 PyObject *value_formatter = var->pretty_printer;
2792
2793 varobj_ensure_python_env (var);
2794
2795 if (value_formatter)
2796 {
2797 /* First check to see if we have any children at all. If so,
2798 we simply return {...}. */
2799 if (dynamic_varobj_has_child_method (var))
2800 {
2801 do_cleanups (old_chain);
2802 return xstrdup ("{...}");
2803 }
2804
2805 if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
2806 {
2807 struct value *replacement;
2808 PyObject *output = NULL;
2809
2810 output = apply_varobj_pretty_printer (value_formatter,
2811 &replacement,
2812 stb);
2813
2814 /* If we have string like output ... */
2815 if (output)
2816 {
2817 make_cleanup_py_decref (output);
2818
2819 /* If this is a lazy string, extract it. For lazy
2820 strings we always print as a string, so set
2821 string_print. */
2822 if (gdbpy_is_lazy_string (output))
2823 {
2824 gdbpy_extract_lazy_string (output, &str_addr, &type,
2825 &len, &encoding);
2826 make_cleanup (free_current_contents, &encoding);
2827 string_print = 1;
2828 }
2829 else
2830 {
2831 /* If it is a regular (non-lazy) string, extract
2832 it and copy the contents into THEVALUE. If the
2833 hint says to print it as a string, set
2834 string_print. Otherwise just return the extracted
2835 string as a value. */
2836
2837 PyObject *py_str
2838 = python_string_to_target_python_string (output);
2839
2840 if (py_str)
2841 {
2842 char *s = PyString_AsString (py_str);
2843 char *hint;
2844
2845 hint = gdbpy_get_display_hint (value_formatter);
2846 if (hint)
2847 {
2848 if (!strcmp (hint, "string"))
2849 string_print = 1;
2850 xfree (hint);
2851 }
2852
2853 len = PyString_Size (py_str);
2854 thevalue = xmemdup (s, len + 1, len + 1);
2855 type = builtin_type (gdbarch)->builtin_char;
2856 Py_DECREF (py_str);
2857
2858 if (!string_print)
2859 {
2860 do_cleanups (old_chain);
2861 return thevalue;
2862 }
2863
2864 make_cleanup (xfree, thevalue);
2865 }
2866 else
2867 gdbpy_print_stack ();
2868 }
2869 }
2870 /* If the printer returned a replacement value, set VALUE
2871 to REPLACEMENT. If there is not a replacement value,
2872 just use the value passed to this function. */
2873 if (replacement)
2874 value = replacement;
2875 }
2876 }
2877 }
2878 #endif
2879
2880 get_formatted_print_options (&opts, format_code[(int) format]);
2881 opts.deref_ref = 0;
2882 opts.raw = 1;
2883
2884 /* If the THEVALUE has contents, it is a regular string. */
2885 if (thevalue)
2886 LA_PRINT_STRING (stb, type, thevalue, len, encoding, 0, &opts);
2887 else if (string_print)
2888 /* Otherwise, if string_print is set, and it is not a regular
2889 string, it is a lazy string. */
2890 val_print_string (type, encoding, str_addr, len, stb, &opts);
2891 else
2892 /* All other cases. */
2893 common_val_print (value, stb, 0, &opts, current_language);
2894
2895 thevalue = ui_file_xstrdup (stb, NULL);
2896
2897 do_cleanups (old_chain);
2898 return thevalue;
2899 }
2900
2901 int
2902 varobj_editable_p (struct varobj *var)
2903 {
2904 struct type *type;
2905
2906 if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value)))
2907 return 0;
2908
2909 type = get_value_type (var);
2910
2911 switch (TYPE_CODE (type))
2912 {
2913 case TYPE_CODE_STRUCT:
2914 case TYPE_CODE_UNION:
2915 case TYPE_CODE_ARRAY:
2916 case TYPE_CODE_FUNC:
2917 case TYPE_CODE_METHOD:
2918 return 0;
2919 break;
2920
2921 default:
2922 return 1;
2923 break;
2924 }
2925 }
2926
2927 /* Call VAR's value_is_changeable_p language-specific callback. */
2928
2929 static int
2930 varobj_value_is_changeable_p (struct varobj *var)
2931 {
2932 return var->root->lang->value_is_changeable_p (var);
2933 }
2934
2935 /* Return 1 if that varobj is floating, that is is always evaluated in the
2936 selected frame, and not bound to thread/frame. Such variable objects
2937 are created using '@' as frame specifier to -var-create. */
2938 int
2939 varobj_floating_p (struct varobj *var)
2940 {
2941 return var->root->floating;
2942 }
2943
2944 /* Given the value and the type of a variable object,
2945 adjust the value and type to those necessary
2946 for getting children of the variable object.
2947 This includes dereferencing top-level references
2948 to all types and dereferencing pointers to
2949 structures.
2950
2951 Both TYPE and *TYPE should be non-null. VALUE
2952 can be null if we want to only translate type.
2953 *VALUE can be null as well -- if the parent
2954 value is not known.
2955
2956 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
2957 depending on whether pointer was dereferenced
2958 in this function. */
2959 static void
2960 adjust_value_for_child_access (struct value **value,
2961 struct type **type,
2962 int *was_ptr)
2963 {
2964 gdb_assert (type && *type);
2965
2966 if (was_ptr)
2967 *was_ptr = 0;
2968
2969 *type = check_typedef (*type);
2970
2971 /* The type of value stored in varobj, that is passed
2972 to us, is already supposed to be
2973 reference-stripped. */
2974
2975 gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
2976
2977 /* Pointers to structures are treated just like
2978 structures when accessing children. Don't
2979 dererences pointers to other types. */
2980 if (TYPE_CODE (*type) == TYPE_CODE_PTR)
2981 {
2982 struct type *target_type = get_target_type (*type);
2983 if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
2984 || TYPE_CODE (target_type) == TYPE_CODE_UNION)
2985 {
2986 if (value && *value)
2987 {
2988 volatile struct gdb_exception except;
2989
2990 TRY_CATCH (except, RETURN_MASK_ERROR)
2991 {
2992 *value = value_ind (*value);
2993 }
2994
2995 if (except.reason < 0)
2996 *value = NULL;
2997 }
2998 *type = target_type;
2999 if (was_ptr)
3000 *was_ptr = 1;
3001 }
3002 }
3003
3004 /* The 'get_target_type' function calls check_typedef on
3005 result, so we can immediately check type code. No
3006 need to call check_typedef here. */
3007 }
3008
3009 /* Implement the "value_is_changeable_p" varobj callback for most
3010 languages. */
3011
3012 static int
3013 default_value_is_changeable_p (struct varobj *var)
3014 {
3015 int r;
3016 struct type *type;
3017
3018 if (CPLUS_FAKE_CHILD (var))
3019 return 0;
3020
3021 type = get_value_type (var);
3022
3023 switch (TYPE_CODE (type))
3024 {
3025 case TYPE_CODE_STRUCT:
3026 case TYPE_CODE_UNION:
3027 case TYPE_CODE_ARRAY:
3028 r = 0;
3029 break;
3030
3031 default:
3032 r = 1;
3033 }
3034
3035 return r;
3036 }
3037
3038 /* C */
3039
3040 static int
3041 c_number_of_children (struct varobj *var)
3042 {
3043 struct type *type = get_value_type (var);
3044 int children = 0;
3045 struct type *target;
3046
3047 adjust_value_for_child_access (NULL, &type, NULL);
3048 target = get_target_type (type);
3049
3050 switch (TYPE_CODE (type))
3051 {
3052 case TYPE_CODE_ARRAY:
3053 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
3054 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
3055 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
3056 else
3057 /* If we don't know how many elements there are, don't display
3058 any. */
3059 children = 0;
3060 break;
3061
3062 case TYPE_CODE_STRUCT:
3063 case TYPE_CODE_UNION:
3064 children = TYPE_NFIELDS (type);
3065 break;
3066
3067 case TYPE_CODE_PTR:
3068 /* The type here is a pointer to non-struct. Typically, pointers
3069 have one child, except for function ptrs, which have no children,
3070 and except for void*, as we don't know what to show.
3071
3072 We can show char* so we allow it to be dereferenced. If you decide
3073 to test for it, please mind that a little magic is necessary to
3074 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
3075 TYPE_NAME == "char". */
3076 if (TYPE_CODE (target) == TYPE_CODE_FUNC
3077 || TYPE_CODE (target) == TYPE_CODE_VOID)
3078 children = 0;
3079 else
3080 children = 1;
3081 break;
3082
3083 default:
3084 /* Other types have no children. */
3085 break;
3086 }
3087
3088 return children;
3089 }
3090
3091 static char *
3092 c_name_of_variable (struct varobj *parent)
3093 {
3094 return xstrdup (parent->name);
3095 }
3096
3097 /* Return the value of element TYPE_INDEX of a structure
3098 value VALUE. VALUE's type should be a structure,
3099 or union, or a typedef to struct/union.
3100
3101 Returns NULL if getting the value fails. Never throws. */
3102 static struct value *
3103 value_struct_element_index (struct value *value, int type_index)
3104 {
3105 struct value *result = NULL;
3106 volatile struct gdb_exception e;
3107 struct type *type = value_type (value);
3108
3109 type = check_typedef (type);
3110
3111 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
3112 || TYPE_CODE (type) == TYPE_CODE_UNION);
3113
3114 TRY_CATCH (e, RETURN_MASK_ERROR)
3115 {
3116 if (field_is_static (&TYPE_FIELD (type, type_index)))
3117 result = value_static_field (type, type_index);
3118 else
3119 result = value_primitive_field (value, 0, type_index, type);
3120 }
3121 if (e.reason < 0)
3122 {
3123 return NULL;
3124 }
3125 else
3126 {
3127 return result;
3128 }
3129 }
3130
3131 /* Obtain the information about child INDEX of the variable
3132 object PARENT.
3133 If CNAME is not null, sets *CNAME to the name of the child relative
3134 to the parent.
3135 If CVALUE is not null, sets *CVALUE to the value of the child.
3136 If CTYPE is not null, sets *CTYPE to the type of the child.
3137
3138 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
3139 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
3140 to NULL. */
3141 static void
3142 c_describe_child (struct varobj *parent, int index,
3143 char **cname, struct value **cvalue, struct type **ctype,
3144 char **cfull_expression)
3145 {
3146 struct value *value = parent->value;
3147 struct type *type = get_value_type (parent);
3148 char *parent_expression = NULL;
3149 int was_ptr;
3150 volatile struct gdb_exception except;
3151
3152 if (cname)
3153 *cname = NULL;
3154 if (cvalue)
3155 *cvalue = NULL;
3156 if (ctype)
3157 *ctype = NULL;
3158 if (cfull_expression)
3159 {
3160 *cfull_expression = NULL;
3161 parent_expression = varobj_get_path_expr (get_path_expr_parent (parent));
3162 }
3163 adjust_value_for_child_access (&value, &type, &was_ptr);
3164
3165 switch (TYPE_CODE (type))
3166 {
3167 case TYPE_CODE_ARRAY:
3168 if (cname)
3169 *cname
3170 = xstrdup (int_string (index
3171 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
3172 10, 1, 0, 0));
3173
3174 if (cvalue && value)
3175 {
3176 int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
3177
3178 TRY_CATCH (except, RETURN_MASK_ERROR)
3179 {
3180 *cvalue = value_subscript (value, real_index);
3181 }
3182 }
3183
3184 if (ctype)
3185 *ctype = get_target_type (type);
3186
3187 if (cfull_expression)
3188 *cfull_expression =
3189 xstrprintf ("(%s)[%s]", parent_expression,
3190 int_string (index
3191 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
3192 10, 1, 0, 0));
3193
3194
3195 break;
3196
3197 case TYPE_CODE_STRUCT:
3198 case TYPE_CODE_UNION:
3199 {
3200 const char *field_name;
3201
3202 /* If the type is anonymous and the field has no name,
3203 set an appropriate name. */
3204 field_name = TYPE_FIELD_NAME (type, index);
3205 if (field_name == NULL || *field_name == '\0')
3206 {
3207 if (cname)
3208 {
3209 if (TYPE_CODE (TYPE_FIELD_TYPE (type, index))
3210 == TYPE_CODE_STRUCT)
3211 *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
3212 else
3213 *cname = xstrdup (ANONYMOUS_UNION_NAME);
3214 }
3215
3216 if (cfull_expression)
3217 *cfull_expression = xstrdup ("");
3218 }
3219 else
3220 {
3221 if (cname)
3222 *cname = xstrdup (field_name);
3223
3224 if (cfull_expression)
3225 {
3226 char *join = was_ptr ? "->" : ".";
3227
3228 *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression,
3229 join, field_name);
3230 }
3231 }
3232
3233 if (cvalue && value)
3234 {
3235 /* For C, varobj index is the same as type index. */
3236 *cvalue = value_struct_element_index (value, index);
3237 }
3238
3239 if (ctype)
3240 *ctype = TYPE_FIELD_TYPE (type, index);
3241 }
3242 break;
3243
3244 case TYPE_CODE_PTR:
3245 if (cname)
3246 *cname = xstrprintf ("*%s", parent->name);
3247
3248 if (cvalue && value)
3249 {
3250 TRY_CATCH (except, RETURN_MASK_ERROR)
3251 {
3252 *cvalue = value_ind (value);
3253 }
3254
3255 if (except.reason < 0)
3256 *cvalue = NULL;
3257 }
3258
3259 /* Don't use get_target_type because it calls
3260 check_typedef and here, we want to show the true
3261 declared type of the variable. */
3262 if (ctype)
3263 *ctype = TYPE_TARGET_TYPE (type);
3264
3265 if (cfull_expression)
3266 *cfull_expression = xstrprintf ("*(%s)", parent_expression);
3267
3268 break;
3269
3270 default:
3271 /* This should not happen. */
3272 if (cname)
3273 *cname = xstrdup ("???");
3274 if (cfull_expression)
3275 *cfull_expression = xstrdup ("???");
3276 /* Don't set value and type, we don't know then. */
3277 }
3278 }
3279
3280 static char *
3281 c_name_of_child (struct varobj *parent, int index)
3282 {
3283 char *name;
3284
3285 c_describe_child (parent, index, &name, NULL, NULL, NULL);
3286 return name;
3287 }
3288
3289 static char *
3290 c_path_expr_of_child (struct varobj *child)
3291 {
3292 c_describe_child (child->parent, child->index, NULL, NULL, NULL,
3293 &child->path_expr);
3294 return child->path_expr;
3295 }
3296
3297 /* If frame associated with VAR can be found, switch
3298 to it and return 1. Otherwise, return 0. */
3299 static int
3300 check_scope (struct varobj *var)
3301 {
3302 struct frame_info *fi;
3303 int scope;
3304
3305 fi = frame_find_by_id (var->root->frame);
3306 scope = fi != NULL;
3307
3308 if (fi)
3309 {
3310 CORE_ADDR pc = get_frame_pc (fi);
3311
3312 if (pc < BLOCK_START (var->root->valid_block) ||
3313 pc >= BLOCK_END (var->root->valid_block))
3314 scope = 0;
3315 else
3316 select_frame (fi);
3317 }
3318 return scope;
3319 }
3320
3321 static struct value *
3322 c_value_of_root (struct varobj **var_handle)
3323 {
3324 struct value *new_val = NULL;
3325 struct varobj *var = *var_handle;
3326 int within_scope = 0;
3327 struct cleanup *back_to;
3328
3329 /* Only root variables can be updated... */
3330 if (!is_root_p (var))
3331 /* Not a root var. */
3332 return NULL;
3333
3334 back_to = make_cleanup_restore_current_thread ();
3335
3336 /* Determine whether the variable is still around. */
3337 if (var->root->valid_block == NULL || var->root->floating)
3338 within_scope = 1;
3339 else if (var->root->thread_id == 0)
3340 {
3341 /* The program was single-threaded when the variable object was
3342 created. Technically, it's possible that the program became
3343 multi-threaded since then, but we don't support such
3344 scenario yet. */
3345 within_scope = check_scope (var);
3346 }
3347 else
3348 {
3349 ptid_t ptid = thread_id_to_pid (var->root->thread_id);
3350 if (in_thread_list (ptid))
3351 {
3352 switch_to_thread (ptid);
3353 within_scope = check_scope (var);
3354 }
3355 }
3356
3357 if (within_scope)
3358 {
3359 volatile struct gdb_exception except;
3360
3361 /* We need to catch errors here, because if evaluate
3362 expression fails we want to just return NULL. */
3363 TRY_CATCH (except, RETURN_MASK_ERROR)
3364 {
3365 new_val = evaluate_expression (var->root->exp);
3366 }
3367
3368 return new_val;
3369 }
3370
3371 do_cleanups (back_to);
3372
3373 return NULL;
3374 }
3375
3376 static struct value *
3377 c_value_of_child (struct varobj *parent, int index)
3378 {
3379 struct value *value = NULL;
3380
3381 c_describe_child (parent, index, NULL, &value, NULL, NULL);
3382 return value;
3383 }
3384
3385 static struct type *
3386 c_type_of_child (struct varobj *parent, int index)
3387 {
3388 struct type *type = NULL;
3389
3390 c_describe_child (parent, index, NULL, NULL, &type, NULL);
3391 return type;
3392 }
3393
3394 static char *
3395 c_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3396 {
3397 /* BOGUS: if val_print sees a struct/class, or a reference to one,
3398 it will print out its children instead of "{...}". So we need to
3399 catch that case explicitly. */
3400 struct type *type = get_type (var);
3401
3402 /* Strip top-level references. */
3403 while (TYPE_CODE (type) == TYPE_CODE_REF)
3404 type = check_typedef (TYPE_TARGET_TYPE (type));
3405
3406 switch (TYPE_CODE (type))
3407 {
3408 case TYPE_CODE_STRUCT:
3409 case TYPE_CODE_UNION:
3410 return xstrdup ("{...}");
3411 /* break; */
3412
3413 case TYPE_CODE_ARRAY:
3414 {
3415 char *number;
3416
3417 number = xstrprintf ("[%d]", var->num_children);
3418 return (number);
3419 }
3420 /* break; */
3421
3422 default:
3423 {
3424 if (var->value == NULL)
3425 {
3426 /* This can happen if we attempt to get the value of a struct
3427 member when the parent is an invalid pointer. This is an
3428 error condition, so we should tell the caller. */
3429 return NULL;
3430 }
3431 else
3432 {
3433 if (var->not_fetched && value_lazy (var->value))
3434 /* Frozen variable and no value yet. We don't
3435 implicitly fetch the value. MI response will
3436 use empty string for the value, which is OK. */
3437 return NULL;
3438
3439 gdb_assert (varobj_value_is_changeable_p (var));
3440 gdb_assert (!value_lazy (var->value));
3441
3442 /* If the specified format is the current one,
3443 we can reuse print_value. */
3444 if (format == var->format)
3445 return xstrdup (var->print_value);
3446 else
3447 return value_get_print_value (var->value, format, var);
3448 }
3449 }
3450 }
3451 }
3452 \f
3453
3454 /* C++ */
3455
3456 static int
3457 cplus_number_of_children (struct varobj *var)
3458 {
3459 struct type *type;
3460 int children, dont_know;
3461
3462 dont_know = 1;
3463 children = 0;
3464
3465 if (!CPLUS_FAKE_CHILD (var))
3466 {
3467 type = get_value_type (var);
3468 adjust_value_for_child_access (NULL, &type, NULL);
3469
3470 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
3471 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
3472 {
3473 int kids[3];
3474
3475 cplus_class_num_children (type, kids);
3476 if (kids[v_public] != 0)
3477 children++;
3478 if (kids[v_private] != 0)
3479 children++;
3480 if (kids[v_protected] != 0)
3481 children++;
3482
3483 /* Add any baseclasses. */
3484 children += TYPE_N_BASECLASSES (type);
3485 dont_know = 0;
3486
3487 /* FIXME: save children in var. */
3488 }
3489 }
3490 else
3491 {
3492 int kids[3];
3493
3494 type = get_value_type (var->parent);
3495 adjust_value_for_child_access (NULL, &type, NULL);
3496
3497 cplus_class_num_children (type, kids);
3498 if (strcmp (var->name, "public") == 0)
3499 children = kids[v_public];
3500 else if (strcmp (var->name, "private") == 0)
3501 children = kids[v_private];
3502 else
3503 children = kids[v_protected];
3504 dont_know = 0;
3505 }
3506
3507 if (dont_know)
3508 children = c_number_of_children (var);
3509
3510 return children;
3511 }
3512
3513 /* Compute # of public, private, and protected variables in this class.
3514 That means we need to descend into all baseclasses and find out
3515 how many are there, too. */
3516 static void
3517 cplus_class_num_children (struct type *type, int children[3])
3518 {
3519 int i, vptr_fieldno;
3520 struct type *basetype = NULL;
3521
3522 children[v_public] = 0;
3523 children[v_private] = 0;
3524 children[v_protected] = 0;
3525
3526 vptr_fieldno = get_vptr_fieldno (type, &basetype);
3527 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
3528 {
3529 /* If we have a virtual table pointer, omit it. Even if virtual
3530 table pointers are not specifically marked in the debug info,
3531 they should be artificial. */
3532 if ((type == basetype && i == vptr_fieldno)
3533 || TYPE_FIELD_ARTIFICIAL (type, i))
3534 continue;
3535
3536 if (TYPE_FIELD_PROTECTED (type, i))
3537 children[v_protected]++;
3538 else if (TYPE_FIELD_PRIVATE (type, i))
3539 children[v_private]++;
3540 else
3541 children[v_public]++;
3542 }
3543 }
3544
3545 static char *
3546 cplus_name_of_variable (struct varobj *parent)
3547 {
3548 return c_name_of_variable (parent);
3549 }
3550
3551 enum accessibility { private_field, protected_field, public_field };
3552
3553 /* Check if field INDEX of TYPE has the specified accessibility.
3554 Return 0 if so and 1 otherwise. */
3555 static int
3556 match_accessibility (struct type *type, int index, enum accessibility acc)
3557 {
3558 if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
3559 return 1;
3560 else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
3561 return 1;
3562 else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
3563 && !TYPE_FIELD_PROTECTED (type, index))
3564 return 1;
3565 else
3566 return 0;
3567 }
3568
3569 static void
3570 cplus_describe_child (struct varobj *parent, int index,
3571 char **cname, struct value **cvalue, struct type **ctype,
3572 char **cfull_expression)
3573 {
3574 struct value *value;
3575 struct type *type;
3576 int was_ptr;
3577 char *parent_expression = NULL;
3578
3579 if (cname)
3580 *cname = NULL;
3581 if (cvalue)
3582 *cvalue = NULL;
3583 if (ctype)
3584 *ctype = NULL;
3585 if (cfull_expression)
3586 *cfull_expression = NULL;
3587
3588 if (CPLUS_FAKE_CHILD (parent))
3589 {
3590 value = parent->parent->value;
3591 type = get_value_type (parent->parent);
3592 if (cfull_expression)
3593 parent_expression
3594 = varobj_get_path_expr (get_path_expr_parent (parent->parent));
3595 }
3596 else
3597 {
3598 value = parent->value;
3599 type = get_value_type (parent);
3600 if (cfull_expression)
3601 parent_expression
3602 = varobj_get_path_expr (get_path_expr_parent (parent));
3603 }
3604
3605 adjust_value_for_child_access (&value, &type, &was_ptr);
3606
3607 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3608 || TYPE_CODE (type) == TYPE_CODE_UNION)
3609 {
3610 char *join = was_ptr ? "->" : ".";
3611
3612 if (CPLUS_FAKE_CHILD (parent))
3613 {
3614 /* The fields of the class type are ordered as they
3615 appear in the class. We are given an index for a
3616 particular access control type ("public","protected",
3617 or "private"). We must skip over fields that don't
3618 have the access control we are looking for to properly
3619 find the indexed field. */
3620 int type_index = TYPE_N_BASECLASSES (type);
3621 enum accessibility acc = public_field;
3622 int vptr_fieldno;
3623 struct type *basetype = NULL;
3624 const char *field_name;
3625
3626 vptr_fieldno = get_vptr_fieldno (type, &basetype);
3627 if (strcmp (parent->name, "private") == 0)
3628 acc = private_field;
3629 else if (strcmp (parent->name, "protected") == 0)
3630 acc = protected_field;
3631
3632 while (index >= 0)
3633 {
3634 if ((type == basetype && type_index == vptr_fieldno)
3635 || TYPE_FIELD_ARTIFICIAL (type, type_index))
3636 ; /* ignore vptr */
3637 else if (match_accessibility (type, type_index, acc))
3638 --index;
3639 ++type_index;
3640 }
3641 --type_index;
3642
3643 /* If the type is anonymous and the field has no name,
3644 set an appopriate name. */
3645 field_name = TYPE_FIELD_NAME (type, type_index);
3646 if (field_name == NULL || *field_name == '\0')
3647 {
3648 if (cname)
3649 {
3650 if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
3651 == TYPE_CODE_STRUCT)
3652 *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
3653 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
3654 == TYPE_CODE_UNION)
3655 *cname = xstrdup (ANONYMOUS_UNION_NAME);
3656 }
3657
3658 if (cfull_expression)
3659 *cfull_expression = xstrdup ("");
3660 }
3661 else
3662 {
3663 if (cname)
3664 *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
3665
3666 if (cfull_expression)
3667 *cfull_expression
3668 = xstrprintf ("((%s)%s%s)", parent_expression, join,
3669 field_name);
3670 }
3671
3672 if (cvalue && value)
3673 *cvalue = value_struct_element_index (value, type_index);
3674
3675 if (ctype)
3676 *ctype = TYPE_FIELD_TYPE (type, type_index);
3677 }
3678 else if (index < TYPE_N_BASECLASSES (type))
3679 {
3680 /* This is a baseclass. */
3681 if (cname)
3682 *cname = xstrdup (TYPE_FIELD_NAME (type, index));
3683
3684 if (cvalue && value)
3685 *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
3686
3687 if (ctype)
3688 {
3689 *ctype = TYPE_FIELD_TYPE (type, index);
3690 }
3691
3692 if (cfull_expression)
3693 {
3694 char *ptr = was_ptr ? "*" : "";
3695
3696 /* Cast the parent to the base' type. Note that in gdb,
3697 expression like
3698 (Base1)d
3699 will create an lvalue, for all appearences, so we don't
3700 need to use more fancy:
3701 *(Base1*)(&d)
3702 construct.
3703
3704 When we are in the scope of the base class or of one
3705 of its children, the type field name will be interpreted
3706 as a constructor, if it exists. Therefore, we must
3707 indicate that the name is a class name by using the
3708 'class' keyword. See PR mi/11912 */
3709 *cfull_expression = xstrprintf ("(%s(class %s%s) %s)",
3710 ptr,
3711 TYPE_FIELD_NAME (type, index),
3712 ptr,
3713 parent_expression);
3714 }
3715 }
3716 else
3717 {
3718 char *access = NULL;
3719 int children[3];
3720
3721 cplus_class_num_children (type, children);
3722
3723 /* Everything beyond the baseclasses can
3724 only be "public", "private", or "protected"
3725
3726 The special "fake" children are always output by varobj in
3727 this order. So if INDEX == 2, it MUST be "protected". */
3728 index -= TYPE_N_BASECLASSES (type);
3729 switch (index)
3730 {
3731 case 0:
3732 if (children[v_public] > 0)
3733 access = "public";
3734 else if (children[v_private] > 0)
3735 access = "private";
3736 else
3737 access = "protected";
3738 break;
3739 case 1:
3740 if (children[v_public] > 0)
3741 {
3742 if (children[v_private] > 0)
3743 access = "private";
3744 else
3745 access = "protected";
3746 }
3747 else if (children[v_private] > 0)
3748 access = "protected";
3749 break;
3750 case 2:
3751 /* Must be protected. */
3752 access = "protected";
3753 break;
3754 default:
3755 /* error! */
3756 break;
3757 }
3758
3759 gdb_assert (access);
3760 if (cname)
3761 *cname = xstrdup (access);
3762
3763 /* Value and type and full expression are null here. */
3764 }
3765 }
3766 else
3767 {
3768 c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
3769 }
3770 }
3771
3772 static char *
3773 cplus_name_of_child (struct varobj *parent, int index)
3774 {
3775 char *name = NULL;
3776
3777 cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
3778 return name;
3779 }
3780
3781 static char *
3782 cplus_path_expr_of_child (struct varobj *child)
3783 {
3784 cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
3785 &child->path_expr);
3786 return child->path_expr;
3787 }
3788
3789 static struct value *
3790 cplus_value_of_root (struct varobj **var_handle)
3791 {
3792 return c_value_of_root (var_handle);
3793 }
3794
3795 static struct value *
3796 cplus_value_of_child (struct varobj *parent, int index)
3797 {
3798 struct value *value = NULL;
3799
3800 cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
3801 return value;
3802 }
3803
3804 static struct type *
3805 cplus_type_of_child (struct varobj *parent, int index)
3806 {
3807 struct type *type = NULL;
3808
3809 cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
3810 return type;
3811 }
3812
3813 static char *
3814 cplus_value_of_variable (struct varobj *var,
3815 enum varobj_display_formats format)
3816 {
3817
3818 /* If we have one of our special types, don't print out
3819 any value. */
3820 if (CPLUS_FAKE_CHILD (var))
3821 return xstrdup ("");
3822
3823 return c_value_of_variable (var, format);
3824 }
3825 \f
3826 /* Java */
3827
3828 static int
3829 java_number_of_children (struct varobj *var)
3830 {
3831 return cplus_number_of_children (var);
3832 }
3833
3834 static char *
3835 java_name_of_variable (struct varobj *parent)
3836 {
3837 char *p, *name;
3838
3839 name = cplus_name_of_variable (parent);
3840 /* If the name has "-" in it, it is because we
3841 needed to escape periods in the name... */
3842 p = name;
3843
3844 while (*p != '\000')
3845 {
3846 if (*p == '-')
3847 *p = '.';
3848 p++;
3849 }
3850
3851 return name;
3852 }
3853
3854 static char *
3855 java_name_of_child (struct varobj *parent, int index)
3856 {
3857 char *name, *p;
3858
3859 name = cplus_name_of_child (parent, index);
3860 /* Escape any periods in the name... */
3861 p = name;
3862
3863 while (*p != '\000')
3864 {
3865 if (*p == '.')
3866 *p = '-';
3867 p++;
3868 }
3869
3870 return name;
3871 }
3872
3873 static char *
3874 java_path_expr_of_child (struct varobj *child)
3875 {
3876 return NULL;
3877 }
3878
3879 static struct value *
3880 java_value_of_root (struct varobj **var_handle)
3881 {
3882 return cplus_value_of_root (var_handle);
3883 }
3884
3885 static struct value *
3886 java_value_of_child (struct varobj *parent, int index)
3887 {
3888 return cplus_value_of_child (parent, index);
3889 }
3890
3891 static struct type *
3892 java_type_of_child (struct varobj *parent, int index)
3893 {
3894 return cplus_type_of_child (parent, index);
3895 }
3896
3897 static char *
3898 java_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3899 {
3900 return cplus_value_of_variable (var, format);
3901 }
3902
3903 /* Ada specific callbacks for VAROBJs. */
3904
3905 static int
3906 ada_number_of_children (struct varobj *var)
3907 {
3908 return ada_varobj_get_number_of_children (var->value, var->type);
3909 }
3910
3911 static char *
3912 ada_name_of_variable (struct varobj *parent)
3913 {
3914 return c_name_of_variable (parent);
3915 }
3916
3917 static char *
3918 ada_name_of_child (struct varobj *parent, int index)
3919 {
3920 return ada_varobj_get_name_of_child (parent->value, parent->type,
3921 parent->name, index);
3922 }
3923
3924 static char*
3925 ada_path_expr_of_child (struct varobj *child)
3926 {
3927 struct varobj *parent = child->parent;
3928 const char *parent_path_expr = varobj_get_path_expr (parent);
3929
3930 return ada_varobj_get_path_expr_of_child (parent->value,
3931 parent->type,
3932 parent->name,
3933 parent_path_expr,
3934 child->index);
3935 }
3936
3937 static struct value *
3938 ada_value_of_root (struct varobj **var_handle)
3939 {
3940 return c_value_of_root (var_handle);
3941 }
3942
3943 static struct value *
3944 ada_value_of_child (struct varobj *parent, int index)
3945 {
3946 return ada_varobj_get_value_of_child (parent->value, parent->type,
3947 parent->name, index);
3948 }
3949
3950 static struct type *
3951 ada_type_of_child (struct varobj *parent, int index)
3952 {
3953 return ada_varobj_get_type_of_child (parent->value, parent->type,
3954 index);
3955 }
3956
3957 static char *
3958 ada_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3959 {
3960 struct value_print_options opts;
3961
3962 get_formatted_print_options (&opts, format_code[(int) format]);
3963 opts.deref_ref = 0;
3964 opts.raw = 1;
3965
3966 return ada_varobj_get_value_of_variable (var->value, var->type, &opts);
3967 }
3968
3969 /* Implement the "value_is_changeable_p" routine for Ada. */
3970
3971 static int
3972 ada_value_is_changeable_p (struct varobj *var)
3973 {
3974 struct type *type = var->value ? value_type (var->value) : var->type;
3975
3976 if (ada_is_array_descriptor_type (type)
3977 && TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
3978 {
3979 /* This is in reality a pointer to an unconstrained array.
3980 its value is changeable. */
3981 return 1;
3982 }
3983
3984 if (ada_is_string_type (type))
3985 {
3986 /* We display the contents of the string in the array's
3987 "value" field. The contents can change, so consider
3988 that the array is changeable. */
3989 return 1;
3990 }
3991
3992 return default_value_is_changeable_p (var);
3993 }
3994
3995 /* Implement the "value_has_mutated" routine for Ada. */
3996
3997 static int
3998 ada_value_has_mutated (struct varobj *var, struct value *new_val,
3999 struct type *new_type)
4000 {
4001 int i;
4002 int from = -1;
4003 int to = -1;
4004
4005 /* If the number of fields have changed, then for sure the type
4006 has mutated. */
4007 if (ada_varobj_get_number_of_children (new_val, new_type)
4008 != var->num_children)
4009 return 1;
4010
4011 /* If the number of fields have remained the same, then we need
4012 to check the name of each field. If they remain the same,
4013 then chances are the type hasn't mutated. This is technically
4014 an incomplete test, as the child's type might have changed
4015 despite the fact that the name remains the same. But we'll
4016 handle this situation by saying that the child has mutated,
4017 not this value.
4018
4019 If only part (or none!) of the children have been fetched,
4020 then only check the ones we fetched. It does not matter
4021 to the frontend whether a child that it has not fetched yet
4022 has mutated or not. So just assume it hasn't. */
4023
4024 restrict_range (var->children, &from, &to);
4025 for (i = from; i < to; i++)
4026 if (strcmp (ada_varobj_get_name_of_child (new_val, new_type,
4027 var->name, i),
4028 VEC_index (varobj_p, var->children, i)->name) != 0)
4029 return 1;
4030
4031 return 0;
4032 }
4033
4034 /* Iterate all the existing _root_ VAROBJs and call the FUNC callback for them
4035 with an arbitrary caller supplied DATA pointer. */
4036
4037 void
4038 all_root_varobjs (void (*func) (struct varobj *var, void *data), void *data)
4039 {
4040 struct varobj_root *var_root, *var_root_next;
4041
4042 /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
4043
4044 for (var_root = rootlist; var_root != NULL; var_root = var_root_next)
4045 {
4046 var_root_next = var_root->next;
4047
4048 (*func) (var_root->rootvar, data);
4049 }
4050 }
4051 \f
4052 extern void _initialize_varobj (void);
4053 void
4054 _initialize_varobj (void)
4055 {
4056 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
4057
4058 varobj_table = xmalloc (sizeof_table);
4059 memset (varobj_table, 0, sizeof_table);
4060
4061 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
4062 &varobjdebug,
4063 _("Set varobj debugging."),
4064 _("Show varobj debugging."),
4065 _("When non-zero, varobj debugging is enabled."),
4066 NULL, show_varobjdebug,
4067 &setlist, &showlist);
4068 }
4069
4070 /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
4071 defined on globals. It is a helper for varobj_invalidate. */
4072
4073 static void
4074 varobj_invalidate_iter (struct varobj *var, void *unused)
4075 {
4076 /* Floating varobjs are reparsed on each stop, so we don't care if the
4077 presently parsed expression refers to something that's gone. */
4078 if (var->root->floating)
4079 return;
4080
4081 /* global var must be re-evaluated. */
4082 if (var->root->valid_block == NULL)
4083 {
4084 struct varobj *tmp_var;
4085
4086 /* Try to create a varobj with same expression. If we succeed
4087 replace the old varobj, otherwise invalidate it. */
4088 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
4089 USE_CURRENT_FRAME);
4090 if (tmp_var != NULL)
4091 {
4092 tmp_var->obj_name = xstrdup (var->obj_name);
4093 varobj_delete (var, NULL, 0);
4094 install_variable (tmp_var);
4095 }
4096 else
4097 var->root->is_valid = 0;
4098 }
4099 else /* locals must be invalidated. */
4100 var->root->is_valid = 0;
4101 }
4102
4103 /* Invalidate the varobjs that are tied to locals and re-create the ones that
4104 are defined on globals.
4105 Invalidated varobjs will be always printed in_scope="invalid". */
4106
4107 void
4108 varobj_invalidate (void)
4109 {
4110 all_root_varobjs (varobj_invalidate_iter, NULL);
4111 }