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