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