1 /* Implementation of the GDB variable objects API.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 2009 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "exceptions.h"
22 #include "expression.h"
30 #include "gdb_assert.h"
31 #include "gdb_string.h"
35 #include "gdbthread.h"
39 #include "python/python.h"
40 #include "python/python-internal.h"
45 /* Non-zero if we want to see trace of varobj level stuff. */
49 show_varobjdebug (struct ui_file
*file
, int from_tty
,
50 struct cmd_list_element
*c
, const char *value
)
52 fprintf_filtered (file
, _("Varobj debugging is %s.\n"), value
);
55 /* String representations of gdb's format codes */
56 char *varobj_format_string
[] =
57 { "natural", "binary", "decimal", "hexadecimal", "octal" };
59 /* String representations of gdb's known languages */
60 char *varobj_language_string
[] = { "unknown", "C", "C++", "Java" };
64 /* Every root variable has one of these structures saved in its
65 varobj. Members which must be free'd are noted. */
69 /* Alloc'd expression for this parent. */
70 struct expression
*exp
;
72 /* Block for which this expression is valid */
73 struct block
*valid_block
;
75 /* The frame for this expression. This field is set iff valid_block is
77 struct frame_id frame
;
79 /* The thread ID that this varobj_root belong to. This field
80 is only valid if valid_block is not NULL.
81 When not 0, indicates which thread 'frame' belongs to.
82 When 0, indicates that the thread list was empty when the varobj_root
86 /* If 1, the -var-update always recomputes the value in the
87 current thread and frame. Otherwise, variable object is
88 always updated in the specific scope/thread/frame */
91 /* Flag that indicates validity: set to 0 when this varobj_root refers
92 to symbols that do not exist anymore. */
95 /* Language info for this variable and its children */
96 struct language_specific
*lang
;
98 /* The varobj for this root node. */
99 struct varobj
*rootvar
;
101 /* Next root variable */
102 struct varobj_root
*next
;
105 /* Every variable in the system has a structure of this type defined
106 for it. This structure holds all information necessary to manipulate
107 a particular object variable. Members which must be freed are noted. */
111 /* Alloc'd name of the variable for this object.. If this variable is a
112 child, then this name will be the child's source name.
113 (bar, not foo.bar) */
114 /* NOTE: This is the "expression" */
117 /* Alloc'd expression for this child. Can be used to create a
118 root variable corresponding to this child. */
121 /* The alloc'd name for this variable's object. This is here for
122 convenience when constructing this object's children. */
125 /* Index of this variable in its parent or -1 */
128 /* The type of this variable. This can be NULL
129 for artifial variable objects -- currently, the "accessibility"
130 variable objects in C++. */
133 /* The value of this expression or subexpression. A NULL value
134 indicates there was an error getting this value.
135 Invariant: if varobj_value_is_changeable_p (this) is non-zero,
136 the value is either NULL, or not lazy. */
139 /* The number of (immediate) children this variable has */
142 /* If this object is a child, this points to its immediate parent. */
143 struct varobj
*parent
;
145 /* Children of this object. */
146 VEC (varobj_p
) *children
;
148 /* Whether the children of this varobj were requested. This field is
149 used to decide if dynamic varobj should recompute their children.
150 In the event that the frontend never asked for the children, we
152 int children_requested
;
154 /* Description of the root variable. Points to root variable for children. */
155 struct varobj_root
*root
;
157 /* The format of the output for this object */
158 enum varobj_display_formats format
;
160 /* Was this variable updated via a varobj_set_value operation */
163 /* Last print value. */
166 /* Is this variable frozen. Frozen variables are never implicitly
167 updated by -var-update *
168 or -var-update <direct-or-indirect-parent>. */
171 /* Is the value of this variable intentionally not fetched? It is
172 not fetched if either the variable is frozen, or any parents is
176 /* The pretty-printer that has been constructed. If NULL, then a
177 new printer object is needed, and one will be constructed. */
178 PyObject
*pretty_printer
;
184 struct cpstack
*next
;
187 /* A list of varobjs */
195 /* Private function prototypes */
197 /* Helper functions for the above subcommands. */
199 static int delete_variable (struct cpstack
**, struct varobj
*, int);
201 static void delete_variable_1 (struct cpstack
**, int *,
202 struct varobj
*, int, int);
204 static int install_variable (struct varobj
*);
206 static void uninstall_variable (struct varobj
*);
208 static struct varobj
*create_child (struct varobj
*, int, char *);
210 static struct varobj
*
211 create_child_with_value (struct varobj
*parent
, int index
, const char *name
,
212 struct value
*value
);
214 /* Utility routines */
216 static struct varobj
*new_variable (void);
218 static struct varobj
*new_root_variable (void);
220 static void free_variable (struct varobj
*var
);
222 static struct cleanup
*make_cleanup_free_variable (struct varobj
*var
);
224 static struct type
*get_type (struct varobj
*var
);
226 static struct type
*get_value_type (struct varobj
*var
);
228 static struct type
*get_target_type (struct type
*);
230 static enum varobj_display_formats
variable_default_display (struct varobj
*);
232 static void cppush (struct cpstack
**pstack
, char *name
);
234 static char *cppop (struct cpstack
**pstack
);
236 static int install_new_value (struct varobj
*var
, struct value
*value
,
239 static void install_default_visualizer (struct varobj
*var
);
241 /* Language-specific routines. */
243 static enum varobj_languages
variable_language (struct varobj
*var
);
245 static int number_of_children (struct varobj
*);
247 static char *name_of_variable (struct varobj
*);
249 static char *name_of_child (struct varobj
*, int);
251 static struct value
*value_of_root (struct varobj
**var_handle
, int *);
253 static struct value
*value_of_child (struct varobj
*parent
, int index
);
255 static char *my_value_of_variable (struct varobj
*var
,
256 enum varobj_display_formats format
);
258 static char *value_get_print_value (struct value
*value
,
259 enum varobj_display_formats format
,
262 static int varobj_value_is_changeable_p (struct varobj
*var
);
264 static int is_root_p (struct varobj
*var
);
266 static struct varobj
*
267 varobj_add_child (struct varobj
*var
, const char *name
, struct value
*value
);
269 /* C implementation */
271 static int c_number_of_children (struct varobj
*var
);
273 static char *c_name_of_variable (struct varobj
*parent
);
275 static char *c_name_of_child (struct varobj
*parent
, int index
);
277 static char *c_path_expr_of_child (struct varobj
*child
);
279 static struct value
*c_value_of_root (struct varobj
**var_handle
);
281 static struct value
*c_value_of_child (struct varobj
*parent
, int index
);
283 static struct type
*c_type_of_child (struct varobj
*parent
, int index
);
285 static char *c_value_of_variable (struct varobj
*var
,
286 enum varobj_display_formats format
);
288 /* C++ implementation */
290 static int cplus_number_of_children (struct varobj
*var
);
292 static void cplus_class_num_children (struct type
*type
, int children
[3]);
294 static char *cplus_name_of_variable (struct varobj
*parent
);
296 static char *cplus_name_of_child (struct varobj
*parent
, int index
);
298 static char *cplus_path_expr_of_child (struct varobj
*child
);
300 static struct value
*cplus_value_of_root (struct varobj
**var_handle
);
302 static struct value
*cplus_value_of_child (struct varobj
*parent
, int index
);
304 static struct type
*cplus_type_of_child (struct varobj
*parent
, int index
);
306 static char *cplus_value_of_variable (struct varobj
*var
,
307 enum varobj_display_formats format
);
309 /* Java implementation */
311 static int java_number_of_children (struct varobj
*var
);
313 static char *java_name_of_variable (struct varobj
*parent
);
315 static char *java_name_of_child (struct varobj
*parent
, int index
);
317 static char *java_path_expr_of_child (struct varobj
*child
);
319 static struct value
*java_value_of_root (struct varobj
**var_handle
);
321 static struct value
*java_value_of_child (struct varobj
*parent
, int index
);
323 static struct type
*java_type_of_child (struct varobj
*parent
, int index
);
325 static char *java_value_of_variable (struct varobj
*var
,
326 enum varobj_display_formats format
);
328 /* The language specific vector */
330 struct language_specific
333 /* The language of this variable */
334 enum varobj_languages language
;
336 /* The number of children of PARENT. */
337 int (*number_of_children
) (struct varobj
* parent
);
339 /* The name (expression) of a root varobj. */
340 char *(*name_of_variable
) (struct varobj
* parent
);
342 /* The name of the INDEX'th child of PARENT. */
343 char *(*name_of_child
) (struct varobj
* parent
, int index
);
345 /* Returns the rooted expression of CHILD, which is a variable
346 obtain that has some parent. */
347 char *(*path_expr_of_child
) (struct varobj
* child
);
349 /* The ``struct value *'' of the root variable ROOT. */
350 struct value
*(*value_of_root
) (struct varobj
** root_handle
);
352 /* The ``struct value *'' of the INDEX'th child of PARENT. */
353 struct value
*(*value_of_child
) (struct varobj
* parent
, int index
);
355 /* The type of the INDEX'th child of PARENT. */
356 struct type
*(*type_of_child
) (struct varobj
* parent
, int index
);
358 /* The current value of VAR. */
359 char *(*value_of_variable
) (struct varobj
* var
,
360 enum varobj_display_formats format
);
363 /* Array of known source language routines. */
364 static struct language_specific languages
[vlang_end
] = {
365 /* Unknown (try treating as C */
368 c_number_of_children
,
371 c_path_expr_of_child
,
380 c_number_of_children
,
383 c_path_expr_of_child
,
392 cplus_number_of_children
,
393 cplus_name_of_variable
,
395 cplus_path_expr_of_child
,
397 cplus_value_of_child
,
399 cplus_value_of_variable
}
404 java_number_of_children
,
405 java_name_of_variable
,
407 java_path_expr_of_child
,
411 java_value_of_variable
}
414 /* A little convenience enum for dealing with C++/Java */
417 v_public
= 0, v_private
, v_protected
422 /* Mappings of varobj_display_formats enums to gdb's format codes */
423 static int format_code
[] = { 0, 't', 'd', 'x', 'o' };
425 /* Header of the list of root variable objects */
426 static struct varobj_root
*rootlist
;
427 static int rootcount
= 0; /* number of root varobjs in the list */
429 /* Prime number indicating the number of buckets in the hash table */
430 /* A prime large enough to avoid too many colisions */
431 #define VAROBJ_TABLE_SIZE 227
433 /* Pointer to the varobj hash table (built at run time) */
434 static struct vlist
**varobj_table
;
436 /* Is the variable X one of our "fake" children? */
437 #define CPLUS_FAKE_CHILD(x) \
438 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
441 /* API Implementation */
443 is_root_p (struct varobj
*var
)
445 return (var
->root
->rootvar
== var
);
449 /* Helper function to install a Python environment suitable for
450 use during operations on VAR. */
452 varobj_ensure_python_env (struct varobj
*var
)
454 return ensure_python_env (var
->root
->exp
->gdbarch
,
455 var
->root
->exp
->language_defn
);
459 /* Creates a varobj (not its children) */
461 /* Return the full FRAME which corresponds to the given CORE_ADDR
462 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
464 static struct frame_info
*
465 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr
)
467 struct frame_info
*frame
= NULL
;
469 if (frame_addr
== (CORE_ADDR
) 0)
472 for (frame
= get_current_frame ();
474 frame
= get_prev_frame (frame
))
476 /* The CORE_ADDR we get as argument was parsed from a string GDB
477 output as $fp. This output got truncated to gdbarch_addr_bit.
478 Truncate the frame base address in the same manner before
479 comparing it against our argument. */
480 CORE_ADDR frame_base
= get_frame_base_address (frame
);
481 int addr_bit
= gdbarch_addr_bit (get_frame_arch (frame
));
482 if (addr_bit
< (sizeof (CORE_ADDR
) * HOST_CHAR_BIT
))
483 frame_base
&= ((CORE_ADDR
) 1 << addr_bit
) - 1;
485 if (frame_base
== frame_addr
)
493 varobj_create (char *objname
,
494 char *expression
, CORE_ADDR frame
, enum varobj_type type
)
497 struct frame_info
*fi
;
498 struct frame_info
*old_fi
= NULL
;
500 struct cleanup
*old_chain
;
502 /* Fill out a varobj structure for the (root) variable being constructed. */
503 var
= new_root_variable ();
504 old_chain
= make_cleanup_free_variable (var
);
506 if (expression
!= NULL
)
509 enum varobj_languages lang
;
510 struct value
*value
= NULL
;
512 /* Parse and evaluate the expression, filling in as much of the
513 variable's data as possible. */
515 if (has_stack_frames ())
517 /* Allow creator to specify context of variable */
518 if ((type
== USE_CURRENT_FRAME
) || (type
== USE_SELECTED_FRAME
))
519 fi
= get_selected_frame (NULL
);
521 /* FIXME: cagney/2002-11-23: This code should be doing a
522 lookup using the frame ID and not just the frame's
523 ``address''. This, of course, means an interface
524 change. However, with out that interface change ISAs,
525 such as the ia64 with its two stacks, won't work.
526 Similar goes for the case where there is a frameless
528 fi
= find_frame_addr_in_frame_chain (frame
);
533 /* frame = -2 means always use selected frame */
534 if (type
== USE_SELECTED_FRAME
)
535 var
->root
->floating
= 1;
539 block
= get_frame_block (fi
, 0);
542 innermost_block
= NULL
;
543 /* Wrap the call to parse expression, so we can
544 return a sensible error. */
545 if (!gdb_parse_exp_1 (&p
, block
, 0, &var
->root
->exp
))
550 /* Don't allow variables to be created for types. */
551 if (var
->root
->exp
->elts
[0].opcode
== OP_TYPE
)
553 do_cleanups (old_chain
);
554 fprintf_unfiltered (gdb_stderr
, "Attempt to use a type name"
555 " as an expression.\n");
559 var
->format
= variable_default_display (var
);
560 var
->root
->valid_block
= innermost_block
;
561 var
->name
= xstrdup (expression
);
562 /* For a root var, the name and the expr are the same. */
563 var
->path_expr
= xstrdup (expression
);
565 /* When the frame is different from the current frame,
566 we must select the appropriate frame before parsing
567 the expression, otherwise the value will not be current.
568 Since select_frame is so benign, just call it for all cases. */
569 if (innermost_block
&& fi
!= NULL
)
571 var
->root
->frame
= get_frame_id (fi
);
572 var
->root
->thread_id
= pid_to_thread_id (inferior_ptid
);
573 old_fi
= get_selected_frame (NULL
);
577 /* We definitely need to catch errors here.
578 If evaluate_expression succeeds we got the value we wanted.
579 But if it fails, we still go on with a call to evaluate_type() */
580 if (!gdb_evaluate_expression (var
->root
->exp
, &value
))
582 /* Error getting the value. Try to at least get the
584 struct value
*type_only_value
= evaluate_type (var
->root
->exp
);
585 var
->type
= value_type (type_only_value
);
588 var
->type
= value_type (value
);
590 install_new_value (var
, value
, 1 /* Initial assignment */);
592 /* Set language info */
593 lang
= variable_language (var
);
594 var
->root
->lang
= &languages
[lang
];
596 /* Set ourselves as our root */
597 var
->root
->rootvar
= var
;
599 /* Reset the selected frame */
601 select_frame (old_fi
);
604 /* If the variable object name is null, that means this
605 is a temporary variable, so don't install it. */
607 if ((var
!= NULL
) && (objname
!= NULL
))
609 var
->obj_name
= xstrdup (objname
);
611 /* If a varobj name is duplicated, the install will fail so
613 if (!install_variable (var
))
615 do_cleanups (old_chain
);
620 install_default_visualizer (var
);
621 discard_cleanups (old_chain
);
625 /* Generates an unique name that can be used for a varobj */
628 varobj_gen_name (void)
633 /* generate a name for this object */
635 obj_name
= xstrprintf ("var%d", id
);
640 /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
641 error if OBJNAME cannot be found. */
644 varobj_get_handle (char *objname
)
648 unsigned int index
= 0;
651 for (chp
= objname
; *chp
; chp
++)
653 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
656 cv
= *(varobj_table
+ index
);
657 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, objname
) != 0))
661 error (_("Variable object not found"));
666 /* Given the handle, return the name of the object */
669 varobj_get_objname (struct varobj
*var
)
671 return var
->obj_name
;
674 /* Given the handle, return the expression represented by the object */
677 varobj_get_expression (struct varobj
*var
)
679 return name_of_variable (var
);
682 /* Deletes a varobj and all its children if only_children == 0,
683 otherwise deletes only the children; returns a malloc'ed list of all the
684 (malloc'ed) names of the variables that have been deleted (NULL terminated) */
687 varobj_delete (struct varobj
*var
, char ***dellist
, int only_children
)
691 struct cpstack
*result
= NULL
;
694 /* Initialize a stack for temporary results */
695 cppush (&result
, NULL
);
698 /* Delete only the variable children */
699 delcount
= delete_variable (&result
, var
, 1 /* only the children */ );
701 /* Delete the variable and all its children */
702 delcount
= delete_variable (&result
, var
, 0 /* parent+children */ );
704 /* We may have been asked to return a list of what has been deleted */
707 *dellist
= xmalloc ((delcount
+ 1) * sizeof (char *));
711 *cp
= cppop (&result
);
712 while ((*cp
!= NULL
) && (mycount
> 0))
716 *cp
= cppop (&result
);
719 if (mycount
|| (*cp
!= NULL
))
720 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
727 /* Convenience function for varobj_set_visualizer. Instantiate a
728 pretty-printer for a given value. */
730 instantiate_pretty_printer (PyObject
*constructor
, struct value
*value
)
733 PyObject
*val_obj
= NULL
;
735 volatile struct gdb_exception except
;
737 TRY_CATCH (except
, RETURN_MASK_ALL
)
739 value
= value_copy (value
);
741 GDB_PY_HANDLE_EXCEPTION (except
);
742 val_obj
= value_to_value_object (value
);
747 printer
= PyObject_CallFunctionObjArgs (constructor
, val_obj
, NULL
);
754 /* Set/Get variable object display format */
756 enum varobj_display_formats
757 varobj_set_display_format (struct varobj
*var
,
758 enum varobj_display_formats format
)
765 case FORMAT_HEXADECIMAL
:
767 var
->format
= format
;
771 var
->format
= variable_default_display (var
);
774 if (varobj_value_is_changeable_p (var
)
775 && var
->value
&& !value_lazy (var
->value
))
777 xfree (var
->print_value
);
778 var
->print_value
= value_get_print_value (var
->value
, var
->format
, var
);
784 enum varobj_display_formats
785 varobj_get_display_format (struct varobj
*var
)
791 varobj_get_display_hint (struct varobj
*var
)
796 struct cleanup
*back_to
= varobj_ensure_python_env (var
);
798 if (var
->pretty_printer
)
799 result
= gdbpy_get_display_hint (var
->pretty_printer
);
801 do_cleanups (back_to
);
807 /* If the variable object is bound to a specific thread, that
808 is its evaluation can always be done in context of a frame
809 inside that thread, returns GDB id of the thread -- which
810 is always positive. Otherwise, returns -1. */
812 varobj_get_thread_id (struct varobj
*var
)
814 if (var
->root
->valid_block
&& var
->root
->thread_id
> 0)
815 return var
->root
->thread_id
;
821 varobj_set_frozen (struct varobj
*var
, int frozen
)
823 /* When a variable is unfrozen, we don't fetch its value.
824 The 'not_fetched' flag remains set, so next -var-update
827 We don't fetch the value, because for structures the client
828 should do -var-update anyway. It would be bad to have different
829 client-size logic for structure and other types. */
830 var
->frozen
= frozen
;
834 varobj_get_frozen (struct varobj
*var
)
840 update_dynamic_varobj_children (struct varobj
*var
,
841 VEC (varobj_p
) **changed
,
842 VEC (varobj_p
) **new_and_unchanged
,
847 /* FIXME: we *might* want to provide this functionality as
848 a standalone function, so that other interested parties
849 than varobj code can benefit for this. */
850 struct cleanup
*back_to
;
854 int children_changed
= 0;
855 PyObject
*printer
= var
->pretty_printer
;
857 back_to
= varobj_ensure_python_env (var
);
860 if (!PyObject_HasAttr (printer
, gdbpy_children_cst
))
862 do_cleanups (back_to
);
866 children
= PyObject_CallMethodObjArgs (printer
, gdbpy_children_cst
,
871 gdbpy_print_stack ();
872 error (_("Null value returned for children"));
875 make_cleanup_py_decref (children
);
877 if (!PyIter_Check (children
))
878 error (_("Returned value is not iterable"));
880 iterator
= PyObject_GetIter (children
);
883 gdbpy_print_stack ();
884 error (_("Could not get children iterator"));
886 make_cleanup_py_decref (iterator
);
890 PyObject
*item
= PyIter_Next (iterator
);
894 struct cleanup
*inner
;
898 inner
= make_cleanup_py_decref (item
);
900 if (!PyArg_ParseTuple (item
, "sO", &name
, &py_v
))
901 error (_("Invalid item from the child list"));
903 if (PyObject_TypeCheck (py_v
, &value_object_type
))
905 /* If we just call convert_value_from_python for this type,
906 we won't know who owns the result. For this one case we
907 need to copy the resulting value. */
908 v
= value_object_to_value (py_v
);
912 v
= convert_value_from_python (py_v
);
914 /* TODO: This assume the name of the i-th child never changes. */
916 /* Now see what to do here. */
917 if (VEC_length (varobj_p
, var
->children
) < i
+ 1)
919 /* There's no child yet. */
920 struct varobj
*child
= varobj_add_child (var
, name
, v
);
921 if (new_and_unchanged
)
922 VEC_safe_push (varobj_p
, *new_and_unchanged
, child
);
923 children_changed
= 1;
927 varobj_p existing
= VEC_index (varobj_p
, var
->children
, i
);
928 if (install_new_value (existing
, v
, 0) && changed
)
931 VEC_safe_push (varobj_p
, *changed
, existing
);
935 if (new_and_unchanged
)
936 VEC_safe_push (varobj_p
, *new_and_unchanged
, existing
);
943 if (i
< VEC_length (varobj_p
, var
->children
))
946 children_changed
= 1;
947 for (i
= 0; i
< VEC_length (varobj_p
, var
->children
); ++i
)
948 varobj_delete (VEC_index (varobj_p
, var
->children
, i
), NULL
, 0);
950 VEC_truncate (varobj_p
, var
->children
, i
);
951 var
->num_children
= VEC_length (varobj_p
, var
->children
);
953 do_cleanups (back_to
);
955 *cchanged
= children_changed
;
958 gdb_assert (0 && "should never be called if Python is not enabled");
963 varobj_get_num_children (struct varobj
*var
)
965 if (var
->num_children
== -1)
968 if (!var
->pretty_printer
969 || !update_dynamic_varobj_children (var
, NULL
, NULL
, &changed
))
970 var
->num_children
= number_of_children (var
);
973 return var
->num_children
;
976 /* Creates a list of the immediate children of a variable object;
977 the return code is the number of such children or -1 on error */
980 varobj_list_children (struct varobj
*var
)
982 struct varobj
*child
;
984 int i
, children_changed
;
986 var
->children_requested
= 1;
988 if (var
->pretty_printer
989 /* This, in theory, can result in the number of children changing without
990 frontend noticing. But well, calling -var-list-children on the same
991 varobj twice is not something a sane frontend would do. */
992 && update_dynamic_varobj_children (var
, NULL
, NULL
, &children_changed
))
993 return var
->children
;
995 if (var
->num_children
== -1)
996 var
->num_children
= number_of_children (var
);
998 /* If that failed, give up. */
999 if (var
->num_children
== -1)
1000 return var
->children
;
1002 /* If we're called when the list of children is not yet initialized,
1003 allocate enough elements in it. */
1004 while (VEC_length (varobj_p
, var
->children
) < var
->num_children
)
1005 VEC_safe_push (varobj_p
, var
->children
, NULL
);
1007 for (i
= 0; i
< var
->num_children
; i
++)
1009 varobj_p existing
= VEC_index (varobj_p
, var
->children
, i
);
1011 if (existing
== NULL
)
1013 /* Either it's the first call to varobj_list_children for
1014 this variable object, and the child was never created,
1015 or it was explicitly deleted by the client. */
1016 name
= name_of_child (var
, i
);
1017 existing
= create_child (var
, i
, name
);
1018 VEC_replace (varobj_p
, var
->children
, i
, existing
);
1019 install_default_visualizer (existing
);
1023 return var
->children
;
1026 static struct varobj
*
1027 varobj_add_child (struct varobj
*var
, const char *name
, struct value
*value
)
1029 varobj_p v
= create_child_with_value (var
,
1030 VEC_length (varobj_p
, var
->children
),
1032 VEC_safe_push (varobj_p
, var
->children
, v
);
1033 install_default_visualizer (v
);
1037 /* Obtain the type of an object Variable as a string similar to the one gdb
1038 prints on the console */
1041 varobj_get_type (struct varobj
*var
)
1043 /* For the "fake" variables, do not return a type. (It's type is
1045 Do not return a type for invalid variables as well. */
1046 if (CPLUS_FAKE_CHILD (var
) || !var
->root
->is_valid
)
1049 return type_to_string (var
->type
);
1052 /* Obtain the type of an object variable. */
1055 varobj_get_gdb_type (struct varobj
*var
)
1060 /* Return a pointer to the full rooted expression of varobj VAR.
1061 If it has not been computed yet, compute it. */
1063 varobj_get_path_expr (struct varobj
*var
)
1065 if (var
->path_expr
!= NULL
)
1066 return var
->path_expr
;
1069 /* For root varobjs, we initialize path_expr
1070 when creating varobj, so here it should be
1072 gdb_assert (!is_root_p (var
));
1073 return (*var
->root
->lang
->path_expr_of_child
) (var
);
1077 enum varobj_languages
1078 varobj_get_language (struct varobj
*var
)
1080 return variable_language (var
);
1084 varobj_get_attributes (struct varobj
*var
)
1088 if (varobj_editable_p (var
))
1089 /* FIXME: define masks for attributes */
1090 attributes
|= 0x00000001; /* Editable */
1096 varobj_get_formatted_value (struct varobj
*var
,
1097 enum varobj_display_formats format
)
1099 return my_value_of_variable (var
, format
);
1103 varobj_get_value (struct varobj
*var
)
1105 return my_value_of_variable (var
, var
->format
);
1108 /* Set the value of an object variable (if it is editable) to the
1109 value of the given expression */
1110 /* Note: Invokes functions that can call error() */
1113 varobj_set_value (struct varobj
*var
, char *expression
)
1119 /* The argument "expression" contains the variable's new value.
1120 We need to first construct a legal expression for this -- ugh! */
1121 /* Does this cover all the bases? */
1122 struct expression
*exp
;
1123 struct value
*value
;
1124 int saved_input_radix
= input_radix
;
1125 char *s
= expression
;
1128 gdb_assert (varobj_editable_p (var
));
1130 input_radix
= 10; /* ALWAYS reset to decimal temporarily */
1131 exp
= parse_exp_1 (&s
, 0, 0);
1132 if (!gdb_evaluate_expression (exp
, &value
))
1134 /* We cannot proceed without a valid expression. */
1139 /* All types that are editable must also be changeable. */
1140 gdb_assert (varobj_value_is_changeable_p (var
));
1142 /* The value of a changeable variable object must not be lazy. */
1143 gdb_assert (!value_lazy (var
->value
));
1145 /* Need to coerce the input. We want to check if the
1146 value of the variable object will be different
1147 after assignment, and the first thing value_assign
1148 does is coerce the input.
1149 For example, if we are assigning an array to a pointer variable we
1150 should compare the pointer with the the array's address, not with the
1152 value
= coerce_array (value
);
1154 /* The new value may be lazy. gdb_value_assign, or
1155 rather value_contents, will take care of this.
1156 If fetching of the new value will fail, gdb_value_assign
1157 with catch the exception. */
1158 if (!gdb_value_assign (var
->value
, value
, &val
))
1161 /* If the value has changed, record it, so that next -var-update can
1162 report this change. If a variable had a value of '1', we've set it
1163 to '333' and then set again to '1', when -var-update will report this
1164 variable as changed -- because the first assignment has set the
1165 'updated' flag. There's no need to optimize that, because return value
1166 of -var-update should be considered an approximation. */
1167 var
->updated
= install_new_value (var
, val
, 0 /* Compare values. */);
1168 input_radix
= saved_input_radix
;
1172 /* Returns a malloc'ed list with all root variable objects */
1174 varobj_list (struct varobj
***varlist
)
1177 struct varobj_root
*croot
;
1178 int mycount
= rootcount
;
1180 /* Alloc (rootcount + 1) entries for the result */
1181 *varlist
= xmalloc ((rootcount
+ 1) * sizeof (struct varobj
*));
1185 while ((croot
!= NULL
) && (mycount
> 0))
1187 *cv
= croot
->rootvar
;
1190 croot
= croot
->next
;
1192 /* Mark the end of the list */
1195 if (mycount
|| (croot
!= NULL
))
1197 ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
1198 rootcount
, mycount
);
1203 /* Assign a new value to a variable object. If INITIAL is non-zero,
1204 this is the first assignement after the variable object was just
1205 created, or changed type. In that case, just assign the value
1207 Otherwise, assign the new value, and return 1 if the value is different
1208 from the current one, 0 otherwise. The comparison is done on textual
1209 representation of value. Therefore, some types need not be compared. E.g.
1210 for structures the reported value is always "{...}", so no comparison is
1211 necessary here. If the old value was NULL and new one is not, or vice versa,
1214 The VALUE parameter should not be released -- the function will
1215 take care of releasing it when needed. */
1217 install_new_value (struct varobj
*var
, struct value
*value
, int initial
)
1222 int intentionally_not_fetched
= 0;
1223 char *print_value
= NULL
;
1225 /* We need to know the varobj's type to decide if the value should
1226 be fetched or not. C++ fake children (public/protected/private) don't have
1228 gdb_assert (var
->type
|| CPLUS_FAKE_CHILD (var
));
1229 changeable
= varobj_value_is_changeable_p (var
);
1231 /* If the type has custom visualizer, we consider it to be always
1232 changeable. FIXME: need to make sure this behaviour will not
1233 mess up read-sensitive values. */
1234 if (var
->pretty_printer
)
1237 need_to_fetch
= changeable
;
1239 /* We are not interested in the address of references, and given
1240 that in C++ a reference is not rebindable, it cannot
1241 meaningfully change. So, get hold of the real value. */
1244 value
= coerce_ref (value
);
1245 release_value (value
);
1248 if (var
->type
&& TYPE_CODE (var
->type
) == TYPE_CODE_UNION
)
1249 /* For unions, we need to fetch the value implicitly because
1250 of implementation of union member fetch. When gdb
1251 creates a value for a field and the value of the enclosing
1252 structure is not lazy, it immediately copies the necessary
1253 bytes from the enclosing values. If the enclosing value is
1254 lazy, the call to value_fetch_lazy on the field will read
1255 the data from memory. For unions, that means we'll read the
1256 same memory more than once, which is not desirable. So
1260 /* The new value might be lazy. If the type is changeable,
1261 that is we'll be comparing values of this type, fetch the
1262 value now. Otherwise, on the next update the old value
1263 will be lazy, which means we've lost that old value. */
1264 if (need_to_fetch
&& value
&& value_lazy (value
))
1266 struct varobj
*parent
= var
->parent
;
1267 int frozen
= var
->frozen
;
1268 for (; !frozen
&& parent
; parent
= parent
->parent
)
1269 frozen
|= parent
->frozen
;
1271 if (frozen
&& initial
)
1273 /* For variables that are frozen, or are children of frozen
1274 variables, we don't do fetch on initial assignment.
1275 For non-initial assignemnt we do the fetch, since it means we're
1276 explicitly asked to compare the new value with the old one. */
1277 intentionally_not_fetched
= 1;
1279 else if (!gdb_value_fetch_lazy (value
))
1281 /* Set the value to NULL, so that for the next -var-update,
1282 we don't try to compare the new value with this value,
1283 that we couldn't even read. */
1289 /* Below, we'll be comparing string rendering of old and new
1290 values. Don't get string rendering if the value is
1291 lazy -- if it is, the code above has decided that the value
1292 should not be fetched. */
1293 if (value
&& !value_lazy (value
))
1294 print_value
= value_get_print_value (value
, var
->format
, var
);
1296 /* If the type is changeable, compare the old and the new values.
1297 If this is the initial assignment, we don't have any old value
1299 if (!initial
&& changeable
)
1301 /* If the value of the varobj was changed by -var-set-value, then the
1302 value in the varobj and in the target is the same. However, that value
1303 is different from the value that the varobj had after the previous
1304 -var-update. So need to the varobj as changed. */
1311 /* Try to compare the values. That requires that both
1312 values are non-lazy. */
1313 if (var
->not_fetched
&& value_lazy (var
->value
))
1315 /* This is a frozen varobj and the value was never read.
1316 Presumably, UI shows some "never read" indicator.
1317 Now that we've fetched the real value, we need to report
1318 this varobj as changed so that UI can show the real
1322 else if (var
->value
== NULL
&& value
== NULL
)
1325 else if (var
->value
== NULL
|| value
== NULL
)
1331 gdb_assert (!value_lazy (var
->value
));
1332 gdb_assert (!value_lazy (value
));
1334 gdb_assert (var
->print_value
!= NULL
&& print_value
!= NULL
);
1335 if (strcmp (var
->print_value
, print_value
) != 0)
1341 if (!initial
&& !changeable
)
1343 /* For values that are not changeable, we don't compare the values.
1344 However, we want to notice if a value was not NULL and now is NULL,
1345 or vise versa, so that we report when top-level varobjs come in scope
1346 and leave the scope. */
1347 changed
= (var
->value
!= NULL
) != (value
!= NULL
);
1350 /* We must always keep the new value, since children depend on it. */
1351 if (var
->value
!= NULL
&& var
->value
!= value
)
1352 value_free (var
->value
);
1354 if (var
->print_value
)
1355 xfree (var
->print_value
);
1356 var
->print_value
= print_value
;
1357 if (value
&& value_lazy (value
) && intentionally_not_fetched
)
1358 var
->not_fetched
= 1;
1360 var
->not_fetched
= 0;
1363 gdb_assert (!var
->value
|| value_type (var
->value
));
1369 install_visualizer (struct varobj
*var
, PyObject
*visualizer
)
1372 /* If there are any children now, wipe them. */
1373 varobj_delete (var
, NULL
, 1 /* children only */);
1374 var
->num_children
= -1;
1376 Py_XDECREF (var
->pretty_printer
);
1377 var
->pretty_printer
= visualizer
;
1379 install_new_value (var
, var
->value
, 1);
1381 /* If we removed the visualizer, and the user ever requested the
1382 object's children, then we must compute the list of children.
1383 Note that we needn't do this when installing a visualizer,
1384 because updating will recompute dynamic children. */
1385 if (!visualizer
&& var
->children_requested
)
1386 varobj_list_children (var
);
1388 error (_("Python support required"));
1393 install_default_visualizer (struct varobj
*var
)
1396 struct cleanup
*cleanup
;
1397 PyObject
*pretty_printer
= NULL
;
1399 cleanup
= varobj_ensure_python_env (var
);
1403 pretty_printer
= gdbpy_get_varobj_pretty_printer (var
->value
);
1404 if (! pretty_printer
)
1406 gdbpy_print_stack ();
1407 error (_("Cannot instantiate printer for default visualizer"));
1411 if (pretty_printer
== Py_None
)
1413 Py_DECREF (pretty_printer
);
1414 pretty_printer
= NULL
;
1417 install_visualizer (var
, pretty_printer
);
1418 do_cleanups (cleanup
);
1420 /* No error is right as this function is inserted just as a hook. */
1425 varobj_set_visualizer (struct varobj
*var
, const char *visualizer
)
1428 PyObject
*mainmod
, *globals
, *pretty_printer
, *constructor
;
1429 struct cleanup
*back_to
, *value
;
1431 back_to
= varobj_ensure_python_env (var
);
1433 mainmod
= PyImport_AddModule ("__main__");
1434 globals
= PyModule_GetDict (mainmod
);
1435 Py_INCREF (globals
);
1436 make_cleanup_py_decref (globals
);
1438 constructor
= PyRun_String (visualizer
, Py_eval_input
, globals
, globals
);
1440 /* Do not instantiate NoneType. */
1441 if (constructor
== Py_None
)
1443 pretty_printer
= Py_None
;
1444 Py_INCREF (pretty_printer
);
1447 pretty_printer
= instantiate_pretty_printer (constructor
, var
->value
);
1449 Py_XDECREF (constructor
);
1451 if (! pretty_printer
)
1453 gdbpy_print_stack ();
1454 error (_("Could not evaluate visualizer expression: %s"), visualizer
);
1457 if (pretty_printer
== Py_None
)
1459 Py_DECREF (pretty_printer
);
1460 pretty_printer
= NULL
;
1463 install_visualizer (var
, pretty_printer
);
1465 do_cleanups (back_to
);
1467 error (_("Python support required"));
1471 /* Update the values for a variable and its children. This is a
1472 two-pronged attack. First, re-parse the value for the root's
1473 expression to see if it's changed. Then go all the way
1474 through its children, reconstructing them and noting if they've
1477 The EXPLICIT parameter specifies if this call is result
1478 of MI request to update this specific variable, or
1479 result of implicit -var-update *. For implicit request, we don't
1480 update frozen variables.
1482 NOTE: This function may delete the caller's varobj. If it
1483 returns TYPE_CHANGED, then it has done this and VARP will be modified
1484 to point to the new varobj. */
1486 VEC(varobj_update_result
) *varobj_update (struct varobj
**varp
, int explicit)
1489 int type_changed
= 0;
1494 struct varobj
**templist
= NULL
;
1496 VEC (varobj_update_result
) *stack
= NULL
;
1497 VEC (varobj_update_result
) *result
= NULL
;
1498 struct frame_info
*fi
;
1500 /* Frozen means frozen -- we don't check for any change in
1501 this varobj, including its going out of scope, or
1502 changing type. One use case for frozen varobjs is
1503 retaining previously evaluated expressions, and we don't
1504 want them to be reevaluated at all. */
1505 if (!explicit && (*varp
)->frozen
)
1508 if (!(*varp
)->root
->is_valid
)
1510 varobj_update_result r
= {*varp
};
1511 r
.status
= VAROBJ_INVALID
;
1512 VEC_safe_push (varobj_update_result
, result
, &r
);
1516 if ((*varp
)->root
->rootvar
== *varp
)
1518 varobj_update_result r
= {*varp
};
1519 r
.status
= VAROBJ_IN_SCOPE
;
1521 /* Update the root variable. value_of_root can return NULL
1522 if the variable is no longer around, i.e. we stepped out of
1523 the frame in which a local existed. We are letting the
1524 value_of_root variable dispose of the varobj if the type
1526 new = value_of_root (varp
, &type_changed
);
1529 r
.type_changed
= type_changed
;
1530 if (install_new_value ((*varp
), new, type_changed
))
1534 r
.status
= VAROBJ_NOT_IN_SCOPE
;
1535 r
.value_installed
= 1;
1537 if (r
.status
== VAROBJ_NOT_IN_SCOPE
)
1539 if (r
.type_changed
|| r
.changed
)
1540 VEC_safe_push (varobj_update_result
, result
, &r
);
1544 VEC_safe_push (varobj_update_result
, stack
, &r
);
1548 varobj_update_result r
= {*varp
};
1549 VEC_safe_push (varobj_update_result
, stack
, &r
);
1552 /* Walk through the children, reconstructing them all. */
1553 while (!VEC_empty (varobj_update_result
, stack
))
1555 varobj_update_result r
= *(VEC_last (varobj_update_result
, stack
));
1556 struct varobj
*v
= r
.varobj
;
1558 VEC_pop (varobj_update_result
, stack
);
1560 /* Update this variable, unless it's a root, which is already
1562 if (!r
.value_installed
)
1564 new = value_of_child (v
->parent
, v
->index
);
1565 if (install_new_value (v
, new, 0 /* type not changed */))
1572 /* We probably should not get children of a varobj that has a
1573 pretty-printer, but for which -var-list-children was never
1574 invoked. Presumably, such varobj is not yet expanded in the
1575 UI, so we need not bother getting it. */
1576 if (v
->pretty_printer
)
1578 VEC (varobj_p
) *changed
= 0, *new_and_unchanged
= 0;
1579 int i
, children_changed
;
1582 if (!v
->children_requested
)
1588 /* If update_dynamic_varobj_children returns 0, then we have
1589 a non-conforming pretty-printer, so we skip it. */
1590 if (update_dynamic_varobj_children (v
, &changed
, &new_and_unchanged
,
1593 if (children_changed
)
1594 r
.children_changed
= 1;
1595 for (i
= 0; VEC_iterate (varobj_p
, changed
, i
, tmp
); ++i
)
1597 varobj_update_result r
= {tmp
};
1599 r
.value_installed
= 1;
1600 VEC_safe_push (varobj_update_result
, stack
, &r
);
1603 VEC_iterate (varobj_p
, new_and_unchanged
, i
, tmp
);
1606 varobj_update_result r
= {tmp
};
1607 r
.value_installed
= 1;
1608 VEC_safe_push (varobj_update_result
, stack
, &r
);
1610 if (r
.changed
|| r
.children_changed
)
1611 VEC_safe_push (varobj_update_result
, result
, &r
);
1616 /* Push any children. Use reverse order so that the first
1617 child is popped from the work stack first, and so
1618 will be added to result first. This does not
1619 affect correctness, just "nicer". */
1620 for (i
= VEC_length (varobj_p
, v
->children
)-1; i
>= 0; --i
)
1622 varobj_p c
= VEC_index (varobj_p
, v
->children
, i
);
1623 /* Child may be NULL if explicitly deleted by -var-delete. */
1624 if (c
!= NULL
&& !c
->frozen
)
1626 varobj_update_result r
= {c
};
1627 VEC_safe_push (varobj_update_result
, stack
, &r
);
1631 if (r
.changed
|| r
.type_changed
)
1632 VEC_safe_push (varobj_update_result
, result
, &r
);
1635 VEC_free (varobj_update_result
, stack
);
1641 /* Helper functions */
1644 * Variable object construction/destruction
1648 delete_variable (struct cpstack
**resultp
, struct varobj
*var
,
1649 int only_children_p
)
1653 delete_variable_1 (resultp
, &delcount
, var
,
1654 only_children_p
, 1 /* remove_from_parent_p */ );
1659 /* Delete the variable object VAR and its children */
1660 /* IMPORTANT NOTE: If we delete a variable which is a child
1661 and the parent is not removed we dump core. It must be always
1662 initially called with remove_from_parent_p set */
1664 delete_variable_1 (struct cpstack
**resultp
, int *delcountp
,
1665 struct varobj
*var
, int only_children_p
,
1666 int remove_from_parent_p
)
1670 /* Delete any children of this variable, too. */
1671 for (i
= 0; i
< VEC_length (varobj_p
, var
->children
); ++i
)
1673 varobj_p child
= VEC_index (varobj_p
, var
->children
, i
);
1676 if (!remove_from_parent_p
)
1677 child
->parent
= NULL
;
1678 delete_variable_1 (resultp
, delcountp
, child
, 0, only_children_p
);
1680 VEC_free (varobj_p
, var
->children
);
1682 /* if we were called to delete only the children we are done here */
1683 if (only_children_p
)
1686 /* Otherwise, add it to the list of deleted ones and proceed to do so */
1687 /* If the name is null, this is a temporary variable, that has not
1688 yet been installed, don't report it, it belongs to the caller... */
1689 if (var
->obj_name
!= NULL
)
1691 cppush (resultp
, xstrdup (var
->obj_name
));
1692 *delcountp
= *delcountp
+ 1;
1695 /* If this variable has a parent, remove it from its parent's list */
1696 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1697 (as indicated by remove_from_parent_p) we don't bother doing an
1698 expensive list search to find the element to remove when we are
1699 discarding the list afterwards */
1700 if ((remove_from_parent_p
) && (var
->parent
!= NULL
))
1702 VEC_replace (varobj_p
, var
->parent
->children
, var
->index
, NULL
);
1705 if (var
->obj_name
!= NULL
)
1706 uninstall_variable (var
);
1708 /* Free memory associated with this variable */
1709 free_variable (var
);
1712 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1714 install_variable (struct varobj
*var
)
1717 struct vlist
*newvl
;
1719 unsigned int index
= 0;
1722 for (chp
= var
->obj_name
; *chp
; chp
++)
1724 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1727 cv
= *(varobj_table
+ index
);
1728 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
1732 error (_("Duplicate variable object name"));
1734 /* Add varobj to hash table */
1735 newvl
= xmalloc (sizeof (struct vlist
));
1736 newvl
->next
= *(varobj_table
+ index
);
1738 *(varobj_table
+ index
) = newvl
;
1740 /* If root, add varobj to root list */
1741 if (is_root_p (var
))
1743 /* Add to list of root variables */
1744 if (rootlist
== NULL
)
1745 var
->root
->next
= NULL
;
1747 var
->root
->next
= rootlist
;
1748 rootlist
= var
->root
;
1755 /* Unistall the object VAR. */
1757 uninstall_variable (struct varobj
*var
)
1761 struct varobj_root
*cr
;
1762 struct varobj_root
*prer
;
1764 unsigned int index
= 0;
1767 /* Remove varobj from hash table */
1768 for (chp
= var
->obj_name
; *chp
; chp
++)
1770 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1773 cv
= *(varobj_table
+ index
);
1775 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
1782 fprintf_unfiltered (gdb_stdlog
, "Deleting %s\n", var
->obj_name
);
1787 ("Assertion failed: Could not find variable object \"%s\" to delete",
1793 *(varobj_table
+ index
) = cv
->next
;
1795 prev
->next
= cv
->next
;
1799 /* If root, remove varobj from root list */
1800 if (is_root_p (var
))
1802 /* Remove from list of root variables */
1803 if (rootlist
== var
->root
)
1804 rootlist
= var
->root
->next
;
1809 while ((cr
!= NULL
) && (cr
->rootvar
!= var
))
1817 ("Assertion failed: Could not find varobj \"%s\" in root list",
1824 prer
->next
= cr
->next
;
1831 /* Create and install a child of the parent of the given name */
1832 static struct varobj
*
1833 create_child (struct varobj
*parent
, int index
, char *name
)
1835 return create_child_with_value (parent
, index
, name
,
1836 value_of_child (parent
, index
));
1839 static struct varobj
*
1840 create_child_with_value (struct varobj
*parent
, int index
, const char *name
,
1841 struct value
*value
)
1843 struct varobj
*child
;
1846 child
= new_variable ();
1848 /* name is allocated by name_of_child */
1849 /* FIXME: xstrdup should not be here. */
1850 child
->name
= xstrdup (name
);
1851 child
->index
= index
;
1852 child
->parent
= parent
;
1853 child
->root
= parent
->root
;
1854 childs_name
= xstrprintf ("%s.%s", parent
->obj_name
, name
);
1855 child
->obj_name
= childs_name
;
1856 install_variable (child
);
1858 /* Compute the type of the child. Must do this before
1859 calling install_new_value. */
1861 /* If the child had no evaluation errors, var->value
1862 will be non-NULL and contain a valid type. */
1863 child
->type
= value_type (value
);
1865 /* Otherwise, we must compute the type. */
1866 child
->type
= (*child
->root
->lang
->type_of_child
) (child
->parent
,
1868 install_new_value (child
, value
, 1);
1875 * Miscellaneous utility functions.
1878 /* Allocate memory and initialize a new variable */
1879 static struct varobj
*
1884 var
= (struct varobj
*) xmalloc (sizeof (struct varobj
));
1886 var
->path_expr
= NULL
;
1887 var
->obj_name
= NULL
;
1891 var
->num_children
= -1;
1893 var
->children
= NULL
;
1897 var
->print_value
= NULL
;
1899 var
->not_fetched
= 0;
1900 var
->children_requested
= 0;
1901 var
->pretty_printer
= 0;
1906 /* Allocate memory and initialize a new root variable */
1907 static struct varobj
*
1908 new_root_variable (void)
1910 struct varobj
*var
= new_variable ();
1911 var
->root
= (struct varobj_root
*) xmalloc (sizeof (struct varobj_root
));;
1912 var
->root
->lang
= NULL
;
1913 var
->root
->exp
= NULL
;
1914 var
->root
->valid_block
= NULL
;
1915 var
->root
->frame
= null_frame_id
;
1916 var
->root
->floating
= 0;
1917 var
->root
->rootvar
= NULL
;
1918 var
->root
->is_valid
= 1;
1923 /* Free any allocated memory associated with VAR. */
1925 free_variable (struct varobj
*var
)
1928 if (var
->pretty_printer
)
1930 struct cleanup
*cleanup
= varobj_ensure_python_env (var
);
1931 Py_DECREF (var
->pretty_printer
);
1932 do_cleanups (cleanup
);
1936 value_free (var
->value
);
1938 /* Free the expression if this is a root variable. */
1939 if (is_root_p (var
))
1941 xfree (var
->root
->exp
);
1946 xfree (var
->obj_name
);
1947 xfree (var
->print_value
);
1948 xfree (var
->path_expr
);
1953 do_free_variable_cleanup (void *var
)
1955 free_variable (var
);
1958 static struct cleanup
*
1959 make_cleanup_free_variable (struct varobj
*var
)
1961 return make_cleanup (do_free_variable_cleanup
, var
);
1964 /* This returns the type of the variable. It also skips past typedefs
1965 to return the real type of the variable.
1967 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1968 except within get_target_type and get_type. */
1969 static struct type
*
1970 get_type (struct varobj
*var
)
1976 type
= check_typedef (type
);
1981 /* Return the type of the value that's stored in VAR,
1982 or that would have being stored there if the
1983 value were accessible.
1985 This differs from VAR->type in that VAR->type is always
1986 the true type of the expession in the source language.
1987 The return value of this function is the type we're
1988 actually storing in varobj, and using for displaying
1989 the values and for comparing previous and new values.
1991 For example, top-level references are always stripped. */
1992 static struct type
*
1993 get_value_type (struct varobj
*var
)
1998 type
= value_type (var
->value
);
2002 type
= check_typedef (type
);
2004 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
2005 type
= get_target_type (type
);
2007 type
= check_typedef (type
);
2012 /* This returns the target type (or NULL) of TYPE, also skipping
2013 past typedefs, just like get_type ().
2015 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
2016 except within get_target_type and get_type. */
2017 static struct type
*
2018 get_target_type (struct type
*type
)
2022 type
= TYPE_TARGET_TYPE (type
);
2024 type
= check_typedef (type
);
2030 /* What is the default display for this variable? We assume that
2031 everything is "natural". Any exceptions? */
2032 static enum varobj_display_formats
2033 variable_default_display (struct varobj
*var
)
2035 return FORMAT_NATURAL
;
2038 /* FIXME: The following should be generic for any pointer */
2040 cppush (struct cpstack
**pstack
, char *name
)
2044 s
= (struct cpstack
*) xmalloc (sizeof (struct cpstack
));
2050 /* FIXME: The following should be generic for any pointer */
2052 cppop (struct cpstack
**pstack
)
2057 if ((*pstack
)->name
== NULL
&& (*pstack
)->next
== NULL
)
2062 *pstack
= (*pstack
)->next
;
2069 * Language-dependencies
2072 /* Common entry points */
2074 /* Get the language of variable VAR. */
2075 static enum varobj_languages
2076 variable_language (struct varobj
*var
)
2078 enum varobj_languages lang
;
2080 switch (var
->root
->exp
->language_defn
->la_language
)
2086 case language_cplus
:
2097 /* Return the number of children for a given variable.
2098 The result of this function is defined by the language
2099 implementation. The number of children returned by this function
2100 is the number of children that the user will see in the variable
2103 number_of_children (struct varobj
*var
)
2105 return (*var
->root
->lang
->number_of_children
) (var
);;
2108 /* What is the expression for the root varobj VAR? Returns a malloc'd string. */
2110 name_of_variable (struct varobj
*var
)
2112 return (*var
->root
->lang
->name_of_variable
) (var
);
2115 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
2117 name_of_child (struct varobj
*var
, int index
)
2119 return (*var
->root
->lang
->name_of_child
) (var
, index
);
2122 /* What is the ``struct value *'' of the root variable VAR?
2123 For floating variable object, evaluation can get us a value
2124 of different type from what is stored in varobj already. In
2126 - *type_changed will be set to 1
2127 - old varobj will be freed, and new one will be
2128 created, with the same name.
2129 - *var_handle will be set to the new varobj
2130 Otherwise, *type_changed will be set to 0. */
2131 static struct value
*
2132 value_of_root (struct varobj
**var_handle
, int *type_changed
)
2136 if (var_handle
== NULL
)
2141 /* This should really be an exception, since this should
2142 only get called with a root variable. */
2144 if (!is_root_p (var
))
2147 if (var
->root
->floating
)
2149 struct varobj
*tmp_var
;
2150 char *old_type
, *new_type
;
2152 tmp_var
= varobj_create (NULL
, var
->name
, (CORE_ADDR
) 0,
2153 USE_SELECTED_FRAME
);
2154 if (tmp_var
== NULL
)
2158 old_type
= varobj_get_type (var
);
2159 new_type
= varobj_get_type (tmp_var
);
2160 if (strcmp (old_type
, new_type
) == 0)
2162 /* The expression presently stored inside var->root->exp
2163 remembers the locations of local variables relatively to
2164 the frame where the expression was created (in DWARF location
2165 button, for example). Naturally, those locations are not
2166 correct in other frames, so update the expression. */
2168 struct expression
*tmp_exp
= var
->root
->exp
;
2169 var
->root
->exp
= tmp_var
->root
->exp
;
2170 tmp_var
->root
->exp
= tmp_exp
;
2172 varobj_delete (tmp_var
, NULL
, 0);
2177 tmp_var
->obj_name
= xstrdup (var
->obj_name
);
2178 varobj_delete (var
, NULL
, 0);
2180 install_variable (tmp_var
);
2181 *var_handle
= tmp_var
;
2193 return (*var
->root
->lang
->value_of_root
) (var_handle
);
2196 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
2197 static struct value
*
2198 value_of_child (struct varobj
*parent
, int index
)
2200 struct value
*value
;
2202 value
= (*parent
->root
->lang
->value_of_child
) (parent
, index
);
2207 /* GDB already has a command called "value_of_variable". Sigh. */
2209 my_value_of_variable (struct varobj
*var
, enum varobj_display_formats format
)
2211 if (var
->root
->is_valid
)
2212 return (*var
->root
->lang
->value_of_variable
) (var
, format
);
2218 value_get_print_value (struct value
*value
, enum varobj_display_formats format
,
2222 struct ui_file
*stb
;
2223 struct cleanup
*old_chain
;
2224 char *thevalue
= NULL
;
2225 struct value_print_options opts
;
2232 struct cleanup
*back_to
= varobj_ensure_python_env (var
);
2233 PyObject
*value_formatter
= var
->pretty_printer
;
2235 if (value_formatter
&& PyObject_HasAttr (value_formatter
,
2236 gdbpy_to_string_cst
))
2239 struct value
*replacement
;
2240 int string_print
= 0;
2242 hint
= gdbpy_get_display_hint (value_formatter
);
2245 if (!strcmp (hint
, "string"))
2250 thevalue
= apply_varobj_pretty_printer (value_formatter
,
2252 if (thevalue
&& !string_print
)
2254 do_cleanups (back_to
);
2258 value
= replacement
;
2260 do_cleanups (back_to
);
2264 stb
= mem_fileopen ();
2265 old_chain
= make_cleanup_ui_file_delete (stb
);
2267 get_formatted_print_options (&opts
, format_code
[(int) format
]);
2272 struct gdbarch
*gdbarch
= get_type_arch (value_type (value
));
2273 make_cleanup (xfree
, thevalue
);
2274 LA_PRINT_STRING (stb
, builtin_type (gdbarch
)->builtin_char
,
2275 (gdb_byte
*) thevalue
, strlen (thevalue
),
2279 common_val_print (value
, stb
, 0, &opts
, current_language
);
2280 thevalue
= ui_file_xstrdup (stb
, &dummy
);
2282 do_cleanups (old_chain
);
2287 varobj_editable_p (struct varobj
*var
)
2290 struct value
*value
;
2292 if (!(var
->root
->is_valid
&& var
->value
&& VALUE_LVAL (var
->value
)))
2295 type
= get_value_type (var
);
2297 switch (TYPE_CODE (type
))
2299 case TYPE_CODE_STRUCT
:
2300 case TYPE_CODE_UNION
:
2301 case TYPE_CODE_ARRAY
:
2302 case TYPE_CODE_FUNC
:
2303 case TYPE_CODE_METHOD
:
2313 /* Return non-zero if changes in value of VAR
2314 must be detected and reported by -var-update.
2315 Return zero is -var-update should never report
2316 changes of such values. This makes sense for structures
2317 (since the changes in children values will be reported separately),
2318 or for artifical objects (like 'public' pseudo-field in C++).
2320 Return value of 0 means that gdb need not call value_fetch_lazy
2321 for the value of this variable object. */
2323 varobj_value_is_changeable_p (struct varobj
*var
)
2328 if (CPLUS_FAKE_CHILD (var
))
2331 type
= get_value_type (var
);
2333 switch (TYPE_CODE (type
))
2335 case TYPE_CODE_STRUCT
:
2336 case TYPE_CODE_UNION
:
2337 case TYPE_CODE_ARRAY
:
2348 /* Return 1 if that varobj is floating, that is is always evaluated in the
2349 selected frame, and not bound to thread/frame. Such variable objects
2350 are created using '@' as frame specifier to -var-create. */
2352 varobj_floating_p (struct varobj
*var
)
2354 return var
->root
->floating
;
2357 /* Given the value and the type of a variable object,
2358 adjust the value and type to those necessary
2359 for getting children of the variable object.
2360 This includes dereferencing top-level references
2361 to all types and dereferencing pointers to
2364 Both TYPE and *TYPE should be non-null. VALUE
2365 can be null if we want to only translate type.
2366 *VALUE can be null as well -- if the parent
2369 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
2370 depending on whether pointer was dereferenced
2371 in this function. */
2373 adjust_value_for_child_access (struct value
**value
,
2377 gdb_assert (type
&& *type
);
2382 *type
= check_typedef (*type
);
2384 /* The type of value stored in varobj, that is passed
2385 to us, is already supposed to be
2386 reference-stripped. */
2388 gdb_assert (TYPE_CODE (*type
) != TYPE_CODE_REF
);
2390 /* Pointers to structures are treated just like
2391 structures when accessing children. Don't
2392 dererences pointers to other types. */
2393 if (TYPE_CODE (*type
) == TYPE_CODE_PTR
)
2395 struct type
*target_type
= get_target_type (*type
);
2396 if (TYPE_CODE (target_type
) == TYPE_CODE_STRUCT
2397 || TYPE_CODE (target_type
) == TYPE_CODE_UNION
)
2399 if (value
&& *value
)
2401 int success
= gdb_value_ind (*value
, value
);
2405 *type
= target_type
;
2411 /* The 'get_target_type' function calls check_typedef on
2412 result, so we can immediately check type code. No
2413 need to call check_typedef here. */
2418 c_number_of_children (struct varobj
*var
)
2420 struct type
*type
= get_value_type (var
);
2422 struct type
*target
;
2424 adjust_value_for_child_access (NULL
, &type
, NULL
);
2425 target
= get_target_type (type
);
2427 switch (TYPE_CODE (type
))
2429 case TYPE_CODE_ARRAY
:
2430 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (target
) > 0
2431 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type
))
2432 children
= TYPE_LENGTH (type
) / TYPE_LENGTH (target
);
2434 /* If we don't know how many elements there are, don't display
2439 case TYPE_CODE_STRUCT
:
2440 case TYPE_CODE_UNION
:
2441 children
= TYPE_NFIELDS (type
);
2445 /* The type here is a pointer to non-struct. Typically, pointers
2446 have one child, except for function ptrs, which have no children,
2447 and except for void*, as we don't know what to show.
2449 We can show char* so we allow it to be dereferenced. If you decide
2450 to test for it, please mind that a little magic is necessary to
2451 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
2452 TYPE_NAME == "char" */
2453 if (TYPE_CODE (target
) == TYPE_CODE_FUNC
2454 || TYPE_CODE (target
) == TYPE_CODE_VOID
)
2461 /* Other types have no children */
2469 c_name_of_variable (struct varobj
*parent
)
2471 return xstrdup (parent
->name
);
2474 /* Return the value of element TYPE_INDEX of a structure
2475 value VALUE. VALUE's type should be a structure,
2476 or union, or a typedef to struct/union.
2478 Returns NULL if getting the value fails. Never throws. */
2479 static struct value
*
2480 value_struct_element_index (struct value
*value
, int type_index
)
2482 struct value
*result
= NULL
;
2483 volatile struct gdb_exception e
;
2485 struct type
*type
= value_type (value
);
2486 type
= check_typedef (type
);
2488 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_STRUCT
2489 || TYPE_CODE (type
) == TYPE_CODE_UNION
);
2491 TRY_CATCH (e
, RETURN_MASK_ERROR
)
2493 if (field_is_static (&TYPE_FIELD (type
, type_index
)))
2494 result
= value_static_field (type
, type_index
);
2496 result
= value_primitive_field (value
, 0, type_index
, type
);
2508 /* Obtain the information about child INDEX of the variable
2510 If CNAME is not null, sets *CNAME to the name of the child relative
2512 If CVALUE is not null, sets *CVALUE to the value of the child.
2513 If CTYPE is not null, sets *CTYPE to the type of the child.
2515 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
2516 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
2519 c_describe_child (struct varobj
*parent
, int index
,
2520 char **cname
, struct value
**cvalue
, struct type
**ctype
,
2521 char **cfull_expression
)
2523 struct value
*value
= parent
->value
;
2524 struct type
*type
= get_value_type (parent
);
2525 char *parent_expression
= NULL
;
2534 if (cfull_expression
)
2536 *cfull_expression
= NULL
;
2537 parent_expression
= varobj_get_path_expr (parent
);
2539 adjust_value_for_child_access (&value
, &type
, &was_ptr
);
2541 switch (TYPE_CODE (type
))
2543 case TYPE_CODE_ARRAY
:
2545 *cname
= xstrprintf ("%d", index
2546 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
)));
2548 if (cvalue
&& value
)
2550 int real_index
= index
+ TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
2551 gdb_value_subscript (value
, real_index
, cvalue
);
2555 *ctype
= get_target_type (type
);
2557 if (cfull_expression
)
2558 *cfull_expression
= xstrprintf ("(%s)[%d]", parent_expression
,
2560 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
)));
2565 case TYPE_CODE_STRUCT
:
2566 case TYPE_CODE_UNION
:
2568 *cname
= xstrdup (TYPE_FIELD_NAME (type
, index
));
2570 if (cvalue
&& value
)
2572 /* For C, varobj index is the same as type index. */
2573 *cvalue
= value_struct_element_index (value
, index
);
2577 *ctype
= TYPE_FIELD_TYPE (type
, index
);
2579 if (cfull_expression
)
2581 char *join
= was_ptr
? "->" : ".";
2582 *cfull_expression
= xstrprintf ("(%s)%s%s", parent_expression
, join
,
2583 TYPE_FIELD_NAME (type
, index
));
2590 *cname
= xstrprintf ("*%s", parent
->name
);
2592 if (cvalue
&& value
)
2594 int success
= gdb_value_ind (value
, cvalue
);
2599 /* Don't use get_target_type because it calls
2600 check_typedef and here, we want to show the true
2601 declared type of the variable. */
2603 *ctype
= TYPE_TARGET_TYPE (type
);
2605 if (cfull_expression
)
2606 *cfull_expression
= xstrprintf ("*(%s)", parent_expression
);
2611 /* This should not happen */
2613 *cname
= xstrdup ("???");
2614 if (cfull_expression
)
2615 *cfull_expression
= xstrdup ("???");
2616 /* Don't set value and type, we don't know then. */
2621 c_name_of_child (struct varobj
*parent
, int index
)
2624 c_describe_child (parent
, index
, &name
, NULL
, NULL
, NULL
);
2629 c_path_expr_of_child (struct varobj
*child
)
2631 c_describe_child (child
->parent
, child
->index
, NULL
, NULL
, NULL
,
2633 return child
->path_expr
;
2636 /* If frame associated with VAR can be found, switch
2637 to it and return 1. Otherwise, return 0. */
2639 check_scope (struct varobj
*var
)
2641 struct frame_info
*fi
;
2644 fi
= frame_find_by_id (var
->root
->frame
);
2649 CORE_ADDR pc
= get_frame_pc (fi
);
2650 if (pc
< BLOCK_START (var
->root
->valid_block
) ||
2651 pc
>= BLOCK_END (var
->root
->valid_block
))
2659 static struct value
*
2660 c_value_of_root (struct varobj
**var_handle
)
2662 struct value
*new_val
= NULL
;
2663 struct varobj
*var
= *var_handle
;
2664 struct frame_info
*fi
;
2665 int within_scope
= 0;
2666 struct cleanup
*back_to
;
2668 /* Only root variables can be updated... */
2669 if (!is_root_p (var
))
2670 /* Not a root var */
2673 back_to
= make_cleanup_restore_current_thread ();
2675 /* Determine whether the variable is still around. */
2676 if (var
->root
->valid_block
== NULL
|| var
->root
->floating
)
2678 else if (var
->root
->thread_id
== 0)
2680 /* The program was single-threaded when the variable object was
2681 created. Technically, it's possible that the program became
2682 multi-threaded since then, but we don't support such
2684 within_scope
= check_scope (var
);
2688 ptid_t ptid
= thread_id_to_pid (var
->root
->thread_id
);
2689 if (in_thread_list (ptid
))
2691 switch_to_thread (ptid
);
2692 within_scope
= check_scope (var
);
2698 /* We need to catch errors here, because if evaluate
2699 expression fails we want to just return NULL. */
2700 gdb_evaluate_expression (var
->root
->exp
, &new_val
);
2704 do_cleanups (back_to
);
2709 static struct value
*
2710 c_value_of_child (struct varobj
*parent
, int index
)
2712 struct value
*value
= NULL
;
2713 c_describe_child (parent
, index
, NULL
, &value
, NULL
, NULL
);
2718 static struct type
*
2719 c_type_of_child (struct varobj
*parent
, int index
)
2721 struct type
*type
= NULL
;
2722 c_describe_child (parent
, index
, NULL
, NULL
, &type
, NULL
);
2727 c_value_of_variable (struct varobj
*var
, enum varobj_display_formats format
)
2729 /* BOGUS: if val_print sees a struct/class, or a reference to one,
2730 it will print out its children instead of "{...}". So we need to
2731 catch that case explicitly. */
2732 struct type
*type
= get_type (var
);
2734 /* If we have a custom formatter, return whatever string it has
2736 if (var
->pretty_printer
&& var
->print_value
)
2737 return xstrdup (var
->print_value
);
2739 /* Strip top-level references. */
2740 while (TYPE_CODE (type
) == TYPE_CODE_REF
)
2741 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2743 switch (TYPE_CODE (type
))
2745 case TYPE_CODE_STRUCT
:
2746 case TYPE_CODE_UNION
:
2747 return xstrdup ("{...}");
2750 case TYPE_CODE_ARRAY
:
2753 number
= xstrprintf ("[%d]", var
->num_children
);
2760 if (var
->value
== NULL
)
2762 /* This can happen if we attempt to get the value of a struct
2763 member when the parent is an invalid pointer. This is an
2764 error condition, so we should tell the caller. */
2769 if (var
->not_fetched
&& value_lazy (var
->value
))
2770 /* Frozen variable and no value yet. We don't
2771 implicitly fetch the value. MI response will
2772 use empty string for the value, which is OK. */
2775 gdb_assert (varobj_value_is_changeable_p (var
));
2776 gdb_assert (!value_lazy (var
->value
));
2778 /* If the specified format is the current one,
2779 we can reuse print_value */
2780 if (format
== var
->format
)
2781 return xstrdup (var
->print_value
);
2783 return value_get_print_value (var
->value
, format
, var
);
2793 cplus_number_of_children (struct varobj
*var
)
2796 int children
, dont_know
;
2801 if (!CPLUS_FAKE_CHILD (var
))
2803 type
= get_value_type (var
);
2804 adjust_value_for_child_access (NULL
, &type
, NULL
);
2806 if (((TYPE_CODE (type
)) == TYPE_CODE_STRUCT
) ||
2807 ((TYPE_CODE (type
)) == TYPE_CODE_UNION
))
2811 cplus_class_num_children (type
, kids
);
2812 if (kids
[v_public
] != 0)
2814 if (kids
[v_private
] != 0)
2816 if (kids
[v_protected
] != 0)
2819 /* Add any baseclasses */
2820 children
+= TYPE_N_BASECLASSES (type
);
2823 /* FIXME: save children in var */
2830 type
= get_value_type (var
->parent
);
2831 adjust_value_for_child_access (NULL
, &type
, NULL
);
2833 cplus_class_num_children (type
, kids
);
2834 if (strcmp (var
->name
, "public") == 0)
2835 children
= kids
[v_public
];
2836 else if (strcmp (var
->name
, "private") == 0)
2837 children
= kids
[v_private
];
2839 children
= kids
[v_protected
];
2844 children
= c_number_of_children (var
);
2849 /* Compute # of public, private, and protected variables in this class.
2850 That means we need to descend into all baseclasses and find out
2851 how many are there, too. */
2853 cplus_class_num_children (struct type
*type
, int children
[3])
2857 children
[v_public
] = 0;
2858 children
[v_private
] = 0;
2859 children
[v_protected
] = 0;
2861 for (i
= TYPE_N_BASECLASSES (type
); i
< TYPE_NFIELDS (type
); i
++)
2863 /* If we have a virtual table pointer, omit it. */
2864 if (TYPE_VPTR_BASETYPE (type
) == type
&& TYPE_VPTR_FIELDNO (type
) == i
)
2867 if (TYPE_FIELD_PROTECTED (type
, i
))
2868 children
[v_protected
]++;
2869 else if (TYPE_FIELD_PRIVATE (type
, i
))
2870 children
[v_private
]++;
2872 children
[v_public
]++;
2877 cplus_name_of_variable (struct varobj
*parent
)
2879 return c_name_of_variable (parent
);
2882 enum accessibility
{ private_field
, protected_field
, public_field
};
2884 /* Check if field INDEX of TYPE has the specified accessibility.
2885 Return 0 if so and 1 otherwise. */
2887 match_accessibility (struct type
*type
, int index
, enum accessibility acc
)
2889 if (acc
== private_field
&& TYPE_FIELD_PRIVATE (type
, index
))
2891 else if (acc
== protected_field
&& TYPE_FIELD_PROTECTED (type
, index
))
2893 else if (acc
== public_field
&& !TYPE_FIELD_PRIVATE (type
, index
)
2894 && !TYPE_FIELD_PROTECTED (type
, index
))
2901 cplus_describe_child (struct varobj
*parent
, int index
,
2902 char **cname
, struct value
**cvalue
, struct type
**ctype
,
2903 char **cfull_expression
)
2906 struct value
*value
;
2909 char *parent_expression
= NULL
;
2917 if (cfull_expression
)
2918 *cfull_expression
= NULL
;
2920 if (CPLUS_FAKE_CHILD (parent
))
2922 value
= parent
->parent
->value
;
2923 type
= get_value_type (parent
->parent
);
2924 if (cfull_expression
)
2925 parent_expression
= varobj_get_path_expr (parent
->parent
);
2929 value
= parent
->value
;
2930 type
= get_value_type (parent
);
2931 if (cfull_expression
)
2932 parent_expression
= varobj_get_path_expr (parent
);
2935 adjust_value_for_child_access (&value
, &type
, &was_ptr
);
2937 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
2938 || TYPE_CODE (type
) == TYPE_CODE_UNION
)
2940 char *join
= was_ptr
? "->" : ".";
2941 if (CPLUS_FAKE_CHILD (parent
))
2943 /* The fields of the class type are ordered as they
2944 appear in the class. We are given an index for a
2945 particular access control type ("public","protected",
2946 or "private"). We must skip over fields that don't
2947 have the access control we are looking for to properly
2948 find the indexed field. */
2949 int type_index
= TYPE_N_BASECLASSES (type
);
2950 enum accessibility acc
= public_field
;
2951 if (strcmp (parent
->name
, "private") == 0)
2952 acc
= private_field
;
2953 else if (strcmp (parent
->name
, "protected") == 0)
2954 acc
= protected_field
;
2958 if (TYPE_VPTR_BASETYPE (type
) == type
2959 && type_index
== TYPE_VPTR_FIELDNO (type
))
2961 else if (match_accessibility (type
, type_index
, acc
))
2968 *cname
= xstrdup (TYPE_FIELD_NAME (type
, type_index
));
2970 if (cvalue
&& value
)
2971 *cvalue
= value_struct_element_index (value
, type_index
);
2974 *ctype
= TYPE_FIELD_TYPE (type
, type_index
);
2976 if (cfull_expression
)
2977 *cfull_expression
= xstrprintf ("((%s)%s%s)", parent_expression
,
2979 TYPE_FIELD_NAME (type
, type_index
));
2981 else if (index
< TYPE_N_BASECLASSES (type
))
2983 /* This is a baseclass. */
2985 *cname
= xstrdup (TYPE_FIELD_NAME (type
, index
));
2987 if (cvalue
&& value
)
2989 *cvalue
= value_cast (TYPE_FIELD_TYPE (type
, index
), value
);
2990 release_value (*cvalue
);
2995 *ctype
= TYPE_FIELD_TYPE (type
, index
);
2998 if (cfull_expression
)
3000 char *ptr
= was_ptr
? "*" : "";
3001 /* Cast the parent to the base' type. Note that in gdb,
3004 will create an lvalue, for all appearences, so we don't
3005 need to use more fancy:
3008 *cfull_expression
= xstrprintf ("(%s(%s%s) %s)",
3010 TYPE_FIELD_NAME (type
, index
),
3017 char *access
= NULL
;
3019 cplus_class_num_children (type
, children
);
3021 /* Everything beyond the baseclasses can
3022 only be "public", "private", or "protected"
3024 The special "fake" children are always output by varobj in
3025 this order. So if INDEX == 2, it MUST be "protected". */
3026 index
-= TYPE_N_BASECLASSES (type
);
3030 if (children
[v_public
] > 0)
3032 else if (children
[v_private
] > 0)
3035 access
= "protected";
3038 if (children
[v_public
] > 0)
3040 if (children
[v_private
] > 0)
3043 access
= "protected";
3045 else if (children
[v_private
] > 0)
3046 access
= "protected";
3049 /* Must be protected */
3050 access
= "protected";
3057 gdb_assert (access
);
3059 *cname
= xstrdup (access
);
3061 /* Value and type and full expression are null here. */
3066 c_describe_child (parent
, index
, cname
, cvalue
, ctype
, cfull_expression
);
3071 cplus_name_of_child (struct varobj
*parent
, int index
)
3074 cplus_describe_child (parent
, index
, &name
, NULL
, NULL
, NULL
);
3079 cplus_path_expr_of_child (struct varobj
*child
)
3081 cplus_describe_child (child
->parent
, child
->index
, NULL
, NULL
, NULL
,
3083 return child
->path_expr
;
3086 static struct value
*
3087 cplus_value_of_root (struct varobj
**var_handle
)
3089 return c_value_of_root (var_handle
);
3092 static struct value
*
3093 cplus_value_of_child (struct varobj
*parent
, int index
)
3095 struct value
*value
= NULL
;
3096 cplus_describe_child (parent
, index
, NULL
, &value
, NULL
, NULL
);
3100 static struct type
*
3101 cplus_type_of_child (struct varobj
*parent
, int index
)
3103 struct type
*type
= NULL
;
3104 cplus_describe_child (parent
, index
, NULL
, NULL
, &type
, NULL
);
3109 cplus_value_of_variable (struct varobj
*var
, enum varobj_display_formats format
)
3112 /* If we have one of our special types, don't print out
3114 if (CPLUS_FAKE_CHILD (var
))
3115 return xstrdup ("");
3117 return c_value_of_variable (var
, format
);
3123 java_number_of_children (struct varobj
*var
)
3125 return cplus_number_of_children (var
);
3129 java_name_of_variable (struct varobj
*parent
)
3133 name
= cplus_name_of_variable (parent
);
3134 /* If the name has "-" in it, it is because we
3135 needed to escape periods in the name... */
3138 while (*p
!= '\000')
3149 java_name_of_child (struct varobj
*parent
, int index
)
3153 name
= cplus_name_of_child (parent
, index
);
3154 /* Escape any periods in the name... */
3157 while (*p
!= '\000')
3168 java_path_expr_of_child (struct varobj
*child
)
3173 static struct value
*
3174 java_value_of_root (struct varobj
**var_handle
)
3176 return cplus_value_of_root (var_handle
);
3179 static struct value
*
3180 java_value_of_child (struct varobj
*parent
, int index
)
3182 return cplus_value_of_child (parent
, index
);
3185 static struct type
*
3186 java_type_of_child (struct varobj
*parent
, int index
)
3188 return cplus_type_of_child (parent
, index
);
3192 java_value_of_variable (struct varobj
*var
, enum varobj_display_formats format
)
3194 return cplus_value_of_variable (var
, format
);
3197 extern void _initialize_varobj (void);
3199 _initialize_varobj (void)
3201 int sizeof_table
= sizeof (struct vlist
*) * VAROBJ_TABLE_SIZE
;
3203 varobj_table
= xmalloc (sizeof_table
);
3204 memset (varobj_table
, 0, sizeof_table
);
3206 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance
,
3208 Set varobj debugging."), _("\
3209 Show varobj debugging."), _("\
3210 When non-zero, varobj debugging is enabled."),
3213 &setlist
, &showlist
);
3216 /* Invalidate the varobjs that are tied to locals and re-create the ones that
3217 are defined on globals.
3218 Invalidated varobjs will be always printed in_scope="invalid". */
3221 varobj_invalidate (void)
3223 struct varobj
**all_rootvarobj
;
3224 struct varobj
**varp
;
3226 if (varobj_list (&all_rootvarobj
) > 0)
3228 for (varp
= all_rootvarobj
; *varp
!= NULL
; varp
++)
3230 /* Floating varobjs are reparsed on each stop, so we don't care if
3231 the presently parsed expression refers to something that's gone.
3233 if ((*varp
)->root
->floating
)
3236 /* global var must be re-evaluated. */
3237 if ((*varp
)->root
->valid_block
== NULL
)
3239 struct varobj
*tmp_var
;
3241 /* Try to create a varobj with same expression. If we succeed
3242 replace the old varobj, otherwise invalidate it. */
3243 tmp_var
= varobj_create (NULL
, (*varp
)->name
, (CORE_ADDR
) 0,
3245 if (tmp_var
!= NULL
)
3247 tmp_var
->obj_name
= xstrdup ((*varp
)->obj_name
);
3248 varobj_delete (*varp
, NULL
, 0);
3249 install_variable (tmp_var
);
3252 (*varp
)->root
->is_valid
= 0;
3254 else /* locals must be invalidated. */
3255 (*varp
)->root
->is_valid
= 0;
3258 xfree (all_rootvarobj
);