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