]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/varobj.c
* MAINTAINERS: Update my info.
[thirdparty/binutils-gdb.git] / gdb / varobj.c
CommitLineData
8b93c638 1/* Implementation of the GDB variable objects API.
bc8332bb 2
9b254dd1 3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
1ecb4ee0 4 Free Software Foundation, Inc.
8b93c638
JM
5
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
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
8b93c638
JM
9 (at your option) any later version.
10
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.
15
16 You should have received a copy of the GNU General Public License
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
8b93c638
JM
18
19#include "defs.h"
a6c442d8 20#include "exceptions.h"
8b93c638
JM
21#include "value.h"
22#include "expression.h"
23#include "frame.h"
8b93c638
JM
24#include "language.h"
25#include "wrapper.h"
26#include "gdbcmd.h"
d2353924 27#include "block.h"
a6c442d8
MK
28
29#include "gdb_assert.h"
b66d6d2e 30#include "gdb_string.h"
8b93c638
JM
31
32#include "varobj.h"
28335dcc 33#include "vec.h"
8b93c638
JM
34
35/* Non-zero if we want to see trace of varobj level stuff. */
36
37int varobjdebug = 0;
920d2a44
AC
38static void
39show_varobjdebug (struct ui_file *file, int from_tty,
40 struct cmd_list_element *c, const char *value)
41{
42 fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
43}
8b93c638
JM
44
45/* String representations of gdb's format codes */
46char *varobj_format_string[] =
72330bd6 47 { "natural", "binary", "decimal", "hexadecimal", "octal" };
8b93c638
JM
48
49/* String representations of gdb's known languages */
72330bd6 50char *varobj_language_string[] = { "unknown", "C", "C++", "Java" };
8b93c638
JM
51
52/* Data structures */
53
54/* Every root variable has one of these structures saved in its
55 varobj. Members which must be free'd are noted. */
56struct varobj_root
72330bd6 57{
8b93c638 58
72330bd6
AC
59 /* Alloc'd expression for this parent. */
60 struct expression *exp;
8b93c638 61
72330bd6
AC
62 /* Block for which this expression is valid */
63 struct block *valid_block;
8b93c638 64
72330bd6 65 /* The frame for this expression */
e64d9b3d 66 struct frame_id frame;
8b93c638 67
72330bd6
AC
68 /* If 1, "update" always recomputes the frame & valid block
69 using the currently selected frame. */
70 int use_selected_frame;
73a93a32 71
8756216b
DP
72 /* Flag that indicates validity: set to 0 when this varobj_root refers
73 to symbols that do not exist anymore. */
74 int is_valid;
75
72330bd6
AC
76 /* Language info for this variable and its children */
77 struct language_specific *lang;
8b93c638 78
72330bd6
AC
79 /* The varobj for this root node. */
80 struct varobj *rootvar;
8b93c638 81
72330bd6
AC
82 /* Next root variable */
83 struct varobj_root *next;
84};
8b93c638 85
28335dcc
VP
86typedef struct varobj *varobj_p;
87
88DEF_VEC_P (varobj_p);
89
8b93c638
JM
90/* Every variable in the system has a structure of this type defined
91 for it. This structure holds all information necessary to manipulate
92 a particular object variable. Members which must be freed are noted. */
93struct varobj
72330bd6 94{
8b93c638 95
72330bd6
AC
96 /* Alloc'd name of the variable for this object.. If this variable is a
97 child, then this name will be the child's source name.
98 (bar, not foo.bar) */
99 /* NOTE: This is the "expression" */
100 char *name;
8b93c638 101
02142340
VP
102 /* Alloc'd expression for this child. Can be used to create a
103 root variable corresponding to this child. */
104 char *path_expr;
105
72330bd6
AC
106 /* The alloc'd name for this variable's object. This is here for
107 convenience when constructing this object's children. */
108 char *obj_name;
8b93c638 109
72330bd6
AC
110 /* Index of this variable in its parent or -1 */
111 int index;
8b93c638 112
202ddcaa
VP
113 /* The type of this variable. This can be NULL
114 for artifial variable objects -- currently, the "accessibility"
115 variable objects in C++. */
72330bd6 116 struct type *type;
8b93c638 117
b20d8971
VP
118 /* The value of this expression or subexpression. A NULL value
119 indicates there was an error getting this value.
b2c2bd75
VP
120 Invariant: if varobj_value_is_changeable_p (this) is non-zero,
121 the value is either NULL, or not lazy. */
30b28db1 122 struct value *value;
8b93c638 123
72330bd6
AC
124 /* The number of (immediate) children this variable has */
125 int num_children;
8b93c638 126
72330bd6
AC
127 /* If this object is a child, this points to its immediate parent. */
128 struct varobj *parent;
8b93c638 129
28335dcc
VP
130 /* Children of this object. */
131 VEC (varobj_p) *children;
8b93c638 132
72330bd6
AC
133 /* Description of the root variable. Points to root variable for children. */
134 struct varobj_root *root;
8b93c638 135
72330bd6
AC
136 /* The format of the output for this object */
137 enum varobj_display_formats format;
fb9b6b35
JJ
138
139 /* Was this variable updated via a varobj_set_value operation */
140 int updated;
85265413
NR
141
142 /* Last print value. */
143 char *print_value;
25d5ea92
VP
144
145 /* Is this variable frozen. Frozen variables are never implicitly
146 updated by -var-update *
147 or -var-update <direct-or-indirect-parent>. */
148 int frozen;
149
150 /* Is the value of this variable intentionally not fetched? It is
151 not fetched if either the variable is frozen, or any parents is
152 frozen. */
153 int not_fetched;
72330bd6 154};
8b93c638 155
8b93c638 156struct cpstack
72330bd6
AC
157{
158 char *name;
159 struct cpstack *next;
160};
8b93c638
JM
161
162/* A list of varobjs */
163
164struct vlist
72330bd6
AC
165{
166 struct varobj *var;
167 struct vlist *next;
168};
8b93c638
JM
169
170/* Private function prototypes */
171
172/* Helper functions for the above subcommands. */
173
a14ed312 174static int delete_variable (struct cpstack **, struct varobj *, int);
8b93c638 175
a14ed312
KB
176static void delete_variable_1 (struct cpstack **, int *,
177 struct varobj *, int, int);
8b93c638 178
a14ed312 179static int install_variable (struct varobj *);
8b93c638 180
a14ed312 181static void uninstall_variable (struct varobj *);
8b93c638 182
a14ed312 183static struct varobj *create_child (struct varobj *, int, char *);
8b93c638 184
8b93c638
JM
185/* Utility routines */
186
a14ed312 187static struct varobj *new_variable (void);
8b93c638 188
a14ed312 189static struct varobj *new_root_variable (void);
8b93c638 190
a14ed312 191static void free_variable (struct varobj *var);
8b93c638 192
74b7792f
AC
193static struct cleanup *make_cleanup_free_variable (struct varobj *var);
194
a14ed312 195static struct type *get_type (struct varobj *var);
8b93c638 196
6e2a9270
VP
197static struct type *get_value_type (struct varobj *var);
198
a14ed312 199static struct type *get_target_type (struct type *);
8b93c638 200
a14ed312 201static enum varobj_display_formats variable_default_display (struct varobj *);
8b93c638 202
a14ed312 203static void cppush (struct cpstack **pstack, char *name);
8b93c638 204
a14ed312 205static char *cppop (struct cpstack **pstack);
8b93c638 206
acd65feb
VP
207static int install_new_value (struct varobj *var, struct value *value,
208 int initial);
209
8b93c638
JM
210/* Language-specific routines. */
211
a14ed312 212static enum varobj_languages variable_language (struct varobj *var);
8b93c638 213
a14ed312 214static int number_of_children (struct varobj *);
8b93c638 215
a14ed312 216static char *name_of_variable (struct varobj *);
8b93c638 217
a14ed312 218static char *name_of_child (struct varobj *, int);
8b93c638 219
30b28db1 220static struct value *value_of_root (struct varobj **var_handle, int *);
8b93c638 221
30b28db1 222static struct value *value_of_child (struct varobj *parent, int index);
8b93c638 223
a14ed312 224static char *my_value_of_variable (struct varobj *var);
8b93c638 225
85265413
NR
226static char *value_get_print_value (struct value *value,
227 enum varobj_display_formats format);
228
b2c2bd75
VP
229static int varobj_value_is_changeable_p (struct varobj *var);
230
231static int is_root_p (struct varobj *var);
8b93c638
JM
232
233/* C implementation */
234
a14ed312 235static int c_number_of_children (struct varobj *var);
8b93c638 236
a14ed312 237static char *c_name_of_variable (struct varobj *parent);
8b93c638 238
a14ed312 239static char *c_name_of_child (struct varobj *parent, int index);
8b93c638 240
02142340
VP
241static char *c_path_expr_of_child (struct varobj *child);
242
30b28db1 243static struct value *c_value_of_root (struct varobj **var_handle);
8b93c638 244
30b28db1 245static struct value *c_value_of_child (struct varobj *parent, int index);
8b93c638 246
a14ed312 247static struct type *c_type_of_child (struct varobj *parent, int index);
8b93c638 248
a14ed312 249static char *c_value_of_variable (struct varobj *var);
8b93c638
JM
250
251/* C++ implementation */
252
a14ed312 253static int cplus_number_of_children (struct varobj *var);
8b93c638 254
a14ed312 255static void cplus_class_num_children (struct type *type, int children[3]);
8b93c638 256
a14ed312 257static char *cplus_name_of_variable (struct varobj *parent);
8b93c638 258
a14ed312 259static char *cplus_name_of_child (struct varobj *parent, int index);
8b93c638 260
02142340
VP
261static char *cplus_path_expr_of_child (struct varobj *child);
262
30b28db1 263static struct value *cplus_value_of_root (struct varobj **var_handle);
8b93c638 264
30b28db1 265static struct value *cplus_value_of_child (struct varobj *parent, int index);
8b93c638 266
a14ed312 267static struct type *cplus_type_of_child (struct varobj *parent, int index);
8b93c638 268
a14ed312 269static char *cplus_value_of_variable (struct varobj *var);
8b93c638
JM
270
271/* Java implementation */
272
a14ed312 273static int java_number_of_children (struct varobj *var);
8b93c638 274
a14ed312 275static char *java_name_of_variable (struct varobj *parent);
8b93c638 276
a14ed312 277static char *java_name_of_child (struct varobj *parent, int index);
8b93c638 278
02142340
VP
279static char *java_path_expr_of_child (struct varobj *child);
280
30b28db1 281static struct value *java_value_of_root (struct varobj **var_handle);
8b93c638 282
30b28db1 283static struct value *java_value_of_child (struct varobj *parent, int index);
8b93c638 284
a14ed312 285static struct type *java_type_of_child (struct varobj *parent, int index);
8b93c638 286
a14ed312 287static char *java_value_of_variable (struct varobj *var);
8b93c638
JM
288
289/* The language specific vector */
290
291struct language_specific
72330bd6 292{
8b93c638 293
72330bd6
AC
294 /* The language of this variable */
295 enum varobj_languages language;
8b93c638 296
72330bd6
AC
297 /* The number of children of PARENT. */
298 int (*number_of_children) (struct varobj * parent);
8b93c638 299
72330bd6
AC
300 /* The name (expression) of a root varobj. */
301 char *(*name_of_variable) (struct varobj * parent);
8b93c638 302
72330bd6
AC
303 /* The name of the INDEX'th child of PARENT. */
304 char *(*name_of_child) (struct varobj * parent, int index);
8b93c638 305
02142340
VP
306 /* Returns the rooted expression of CHILD, which is a variable
307 obtain that has some parent. */
308 char *(*path_expr_of_child) (struct varobj * child);
309
30b28db1
AC
310 /* The ``struct value *'' of the root variable ROOT. */
311 struct value *(*value_of_root) (struct varobj ** root_handle);
8b93c638 312
30b28db1
AC
313 /* The ``struct value *'' of the INDEX'th child of PARENT. */
314 struct value *(*value_of_child) (struct varobj * parent, int index);
8b93c638 315
72330bd6
AC
316 /* The type of the INDEX'th child of PARENT. */
317 struct type *(*type_of_child) (struct varobj * parent, int index);
8b93c638 318
72330bd6
AC
319 /* The current value of VAR. */
320 char *(*value_of_variable) (struct varobj * var);
321};
8b93c638
JM
322
323/* Array of known source language routines. */
d5d6fca5 324static struct language_specific languages[vlang_end] = {
8b93c638
JM
325 /* Unknown (try treating as C */
326 {
72330bd6
AC
327 vlang_unknown,
328 c_number_of_children,
329 c_name_of_variable,
330 c_name_of_child,
02142340 331 c_path_expr_of_child,
72330bd6
AC
332 c_value_of_root,
333 c_value_of_child,
334 c_type_of_child,
72330bd6 335 c_value_of_variable}
8b93c638
JM
336 ,
337 /* C */
338 {
72330bd6
AC
339 vlang_c,
340 c_number_of_children,
341 c_name_of_variable,
342 c_name_of_child,
02142340 343 c_path_expr_of_child,
72330bd6
AC
344 c_value_of_root,
345 c_value_of_child,
346 c_type_of_child,
72330bd6 347 c_value_of_variable}
8b93c638
JM
348 ,
349 /* C++ */
350 {
72330bd6
AC
351 vlang_cplus,
352 cplus_number_of_children,
353 cplus_name_of_variable,
354 cplus_name_of_child,
02142340 355 cplus_path_expr_of_child,
72330bd6
AC
356 cplus_value_of_root,
357 cplus_value_of_child,
358 cplus_type_of_child,
72330bd6 359 cplus_value_of_variable}
8b93c638
JM
360 ,
361 /* Java */
362 {
72330bd6
AC
363 vlang_java,
364 java_number_of_children,
365 java_name_of_variable,
366 java_name_of_child,
02142340 367 java_path_expr_of_child,
72330bd6
AC
368 java_value_of_root,
369 java_value_of_child,
370 java_type_of_child,
72330bd6 371 java_value_of_variable}
8b93c638
JM
372};
373
374/* A little convenience enum for dealing with C++/Java */
375enum vsections
72330bd6
AC
376{
377 v_public = 0, v_private, v_protected
378};
8b93c638
JM
379
380/* Private data */
381
382/* Mappings of varobj_display_formats enums to gdb's format codes */
72330bd6 383static int format_code[] = { 0, 't', 'd', 'x', 'o' };
8b93c638
JM
384
385/* Header of the list of root variable objects */
386static struct varobj_root *rootlist;
387static int rootcount = 0; /* number of root varobjs in the list */
388
389/* Prime number indicating the number of buckets in the hash table */
390/* A prime large enough to avoid too many colisions */
391#define VAROBJ_TABLE_SIZE 227
392
393/* Pointer to the varobj hash table (built at run time) */
394static struct vlist **varobj_table;
395
8b93c638
JM
396/* Is the variable X one of our "fake" children? */
397#define CPLUS_FAKE_CHILD(x) \
398((x) != NULL && (x)->type == NULL && (x)->value == NULL)
399\f
400
401/* API Implementation */
b2c2bd75
VP
402static int
403is_root_p (struct varobj *var)
404{
405 return (var->root->rootvar == var);
406}
8b93c638
JM
407
408/* Creates a varobj (not its children) */
409
7d8547c9
AC
410/* Return the full FRAME which corresponds to the given CORE_ADDR
411 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
412
413static struct frame_info *
414find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
415{
416 struct frame_info *frame = NULL;
417
418 if (frame_addr == (CORE_ADDR) 0)
419 return NULL;
420
421 while (1)
422 {
423 frame = get_prev_frame (frame);
424 if (frame == NULL)
425 return NULL;
eb5492fa 426 if (get_frame_base_address (frame) == frame_addr)
7d8547c9
AC
427 return frame;
428 }
429}
430
8b93c638
JM
431struct varobj *
432varobj_create (char *objname,
72330bd6 433 char *expression, CORE_ADDR frame, enum varobj_type type)
8b93c638
JM
434{
435 struct varobj *var;
2c67cb8b
AC
436 struct frame_info *fi;
437 struct frame_info *old_fi = NULL;
8b93c638
JM
438 struct block *block;
439 struct cleanup *old_chain;
440
441 /* Fill out a varobj structure for the (root) variable being constructed. */
442 var = new_root_variable ();
74b7792f 443 old_chain = make_cleanup_free_variable (var);
8b93c638
JM
444
445 if (expression != NULL)
446 {
447 char *p;
448 enum varobj_languages lang;
e55dccf0 449 struct value *value = NULL;
02142340 450 int expr_len;
8b93c638
JM
451
452 /* Parse and evaluate the expression, filling in as much
453 of the variable's data as possible */
454
455 /* Allow creator to specify context of variable */
72330bd6 456 if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
206415a3 457 fi = deprecated_safe_get_selected_frame ();
8b93c638 458 else
7d8547c9
AC
459 /* FIXME: cagney/2002-11-23: This code should be doing a
460 lookup using the frame ID and not just the frame's
461 ``address''. This, of course, means an interface change.
462 However, with out that interface change ISAs, such as the
463 ia64 with its two stacks, won't work. Similar goes for the
464 case where there is a frameless function. */
8b93c638
JM
465 fi = find_frame_addr_in_frame_chain (frame);
466
73a93a32
JI
467 /* frame = -2 means always use selected frame */
468 if (type == USE_SELECTED_FRAME)
469 var->root->use_selected_frame = 1;
470
8b93c638
JM
471 block = NULL;
472 if (fi != NULL)
ae767bfb 473 block = get_frame_block (fi, 0);
8b93c638
JM
474
475 p = expression;
476 innermost_block = NULL;
73a93a32
JI
477 /* Wrap the call to parse expression, so we can
478 return a sensible error. */
479 if (!gdb_parse_exp_1 (&p, block, 0, &var->root->exp))
480 {
481 return NULL;
482 }
8b93c638
JM
483
484 /* Don't allow variables to be created for types. */
485 if (var->root->exp->elts[0].opcode == OP_TYPE)
486 {
487 do_cleanups (old_chain);
bc8332bb
AC
488 fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
489 " as an expression.\n");
8b93c638
JM
490 return NULL;
491 }
492
493 var->format = variable_default_display (var);
494 var->root->valid_block = innermost_block;
02142340
VP
495 expr_len = strlen (expression);
496 var->name = savestring (expression, expr_len);
497 /* For a root var, the name and the expr are the same. */
498 var->path_expr = savestring (expression, expr_len);
8b93c638
JM
499
500 /* When the frame is different from the current frame,
501 we must select the appropriate frame before parsing
502 the expression, otherwise the value will not be current.
503 Since select_frame is so benign, just call it for all cases. */
504 if (fi != NULL)
505 {
7a424e99 506 var->root->frame = get_frame_id (fi);
206415a3 507 old_fi = get_selected_frame (NULL);
0f7d239c 508 select_frame (fi);
8b93c638
JM
509 }
510
340a7723 511 /* We definitely need to catch errors here.
8b93c638
JM
512 If evaluate_expression succeeds we got the value we wanted.
513 But if it fails, we still go on with a call to evaluate_type() */
acd65feb 514 if (!gdb_evaluate_expression (var->root->exp, &value))
e55dccf0
VP
515 {
516 /* Error getting the value. Try to at least get the
517 right type. */
518 struct value *type_only_value = evaluate_type (var->root->exp);
519 var->type = value_type (type_only_value);
520 }
521 else
522 var->type = value_type (value);
acd65feb 523
acd65feb 524 install_new_value (var, value, 1 /* Initial assignment */);
8b93c638
JM
525
526 /* Set language info */
527 lang = variable_language (var);
d5d6fca5 528 var->root->lang = &languages[lang];
8b93c638
JM
529
530 /* Set ourselves as our root */
531 var->root->rootvar = var;
532
533 /* Reset the selected frame */
534 if (fi != NULL)
0f7d239c 535 select_frame (old_fi);
8b93c638
JM
536 }
537
73a93a32
JI
538 /* If the variable object name is null, that means this
539 is a temporary variable, so don't install it. */
540
541 if ((var != NULL) && (objname != NULL))
8b93c638
JM
542 {
543 var->obj_name = savestring (objname, strlen (objname));
544
545 /* If a varobj name is duplicated, the install will fail so
546 we must clenup */
547 if (!install_variable (var))
548 {
549 do_cleanups (old_chain);
550 return NULL;
551 }
552 }
553
554 discard_cleanups (old_chain);
555 return var;
556}
557
558/* Generates an unique name that can be used for a varobj */
559
560char *
561varobj_gen_name (void)
562{
563 static int id = 0;
e64d9b3d 564 char *obj_name;
8b93c638
JM
565
566 /* generate a name for this object */
567 id++;
b435e160 568 obj_name = xstrprintf ("var%d", id);
8b93c638 569
e64d9b3d 570 return obj_name;
8b93c638
JM
571}
572
573/* Given an "objname", returns the pointer to the corresponding varobj
574 or NULL if not found */
575
576struct varobj *
577varobj_get_handle (char *objname)
578{
579 struct vlist *cv;
580 const char *chp;
581 unsigned int index = 0;
582 unsigned int i = 1;
583
584 for (chp = objname; *chp; chp++)
585 {
586 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
587 }
588
589 cv = *(varobj_table + index);
590 while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
591 cv = cv->next;
592
593 if (cv == NULL)
8a3fe4f8 594 error (_("Variable object not found"));
8b93c638
JM
595
596 return cv->var;
597}
598
599/* Given the handle, return the name of the object */
600
601char *
602varobj_get_objname (struct varobj *var)
603{
604 return var->obj_name;
605}
606
607/* Given the handle, return the expression represented by the object */
608
609char *
610varobj_get_expression (struct varobj *var)
611{
612 return name_of_variable (var);
613}
614
615/* Deletes a varobj and all its children if only_children == 0,
616 otherwise deletes only the children; returns a malloc'ed list of all the
617 (malloc'ed) names of the variables that have been deleted (NULL terminated) */
618
619int
620varobj_delete (struct varobj *var, char ***dellist, int only_children)
621{
622 int delcount;
623 int mycount;
624 struct cpstack *result = NULL;
625 char **cp;
626
627 /* Initialize a stack for temporary results */
628 cppush (&result, NULL);
629
630 if (only_children)
631 /* Delete only the variable children */
632 delcount = delete_variable (&result, var, 1 /* only the children */ );
633 else
634 /* Delete the variable and all its children */
635 delcount = delete_variable (&result, var, 0 /* parent+children */ );
636
637 /* We may have been asked to return a list of what has been deleted */
638 if (dellist != NULL)
639 {
640 *dellist = xmalloc ((delcount + 1) * sizeof (char *));
641
642 cp = *dellist;
643 mycount = delcount;
644 *cp = cppop (&result);
645 while ((*cp != NULL) && (mycount > 0))
646 {
647 mycount--;
648 cp++;
649 *cp = cppop (&result);
650 }
651
652 if (mycount || (*cp != NULL))
8a3fe4f8 653 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
72330bd6 654 mycount);
8b93c638
JM
655 }
656
657 return delcount;
658}
659
660/* Set/Get variable object display format */
661
662enum varobj_display_formats
663varobj_set_display_format (struct varobj *var,
664 enum varobj_display_formats format)
665{
666 switch (format)
667 {
668 case FORMAT_NATURAL:
669 case FORMAT_BINARY:
670 case FORMAT_DECIMAL:
671 case FORMAT_HEXADECIMAL:
672 case FORMAT_OCTAL:
673 var->format = format;
674 break;
675
676 default:
677 var->format = variable_default_display (var);
678 }
679
680 return var->format;
681}
682
683enum varobj_display_formats
684varobj_get_display_format (struct varobj *var)
685{
686 return var->format;
687}
688
25d5ea92
VP
689void
690varobj_set_frozen (struct varobj *var, int frozen)
691{
692 /* When a variable is unfrozen, we don't fetch its value.
693 The 'not_fetched' flag remains set, so next -var-update
694 won't complain.
695
696 We don't fetch the value, because for structures the client
697 should do -var-update anyway. It would be bad to have different
698 client-size logic for structure and other types. */
699 var->frozen = frozen;
700}
701
702int
703varobj_get_frozen (struct varobj *var)
704{
705 return var->frozen;
706}
707
708
8b93c638
JM
709int
710varobj_get_num_children (struct varobj *var)
711{
712 if (var->num_children == -1)
713 var->num_children = number_of_children (var);
714
715 return var->num_children;
716}
717
718/* Creates a list of the immediate children of a variable object;
719 the return code is the number of such children or -1 on error */
720
721int
722varobj_list_children (struct varobj *var, struct varobj ***childlist)
723{
724 struct varobj *child;
725 char *name;
726 int i;
727
728 /* sanity check: have we been passed a pointer? */
729 if (childlist == NULL)
730 return -1;
731
732 *childlist = NULL;
733
734 if (var->num_children == -1)
735 var->num_children = number_of_children (var);
736
74a44383
DJ
737 /* If that failed, give up. */
738 if (var->num_children == -1)
739 return -1;
740
28335dcc
VP
741 /* If we're called when the list of children is not yet initialized,
742 allocate enough elements in it. */
743 while (VEC_length (varobj_p, var->children) < var->num_children)
744 VEC_safe_push (varobj_p, var->children, NULL);
745
8b93c638
JM
746 /* List of children */
747 *childlist = xmalloc ((var->num_children + 1) * sizeof (struct varobj *));
748
749 for (i = 0; i < var->num_children; i++)
750 {
28335dcc
VP
751 varobj_p existing;
752
8b93c638
JM
753 /* Mark as the end in case we bail out */
754 *((*childlist) + i) = NULL;
755
28335dcc
VP
756 existing = VEC_index (varobj_p, var->children, i);
757
758 if (existing == NULL)
759 {
760 /* Either it's the first call to varobj_list_children for
761 this variable object, and the child was never created,
762 or it was explicitly deleted by the client. */
763 name = name_of_child (var, i);
764 existing = create_child (var, i, name);
765 VEC_replace (varobj_p, var->children, i, existing);
766 }
8b93c638 767
28335dcc 768 *((*childlist) + i) = existing;
8b93c638
JM
769 }
770
771 /* End of list is marked by a NULL pointer */
772 *((*childlist) + i) = NULL;
773
774 return var->num_children;
775}
776
777/* Obtain the type of an object Variable as a string similar to the one gdb
778 prints on the console */
779
780char *
781varobj_get_type (struct varobj *var)
782{
30b28db1 783 struct value *val;
8b93c638
JM
784 struct cleanup *old_chain;
785 struct ui_file *stb;
786 char *thetype;
787 long length;
788
789 /* For the "fake" variables, do not return a type. (It's type is
8756216b
DP
790 NULL, too.)
791 Do not return a type for invalid variables as well. */
792 if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
8b93c638
JM
793 return NULL;
794
795 stb = mem_fileopen ();
796 old_chain = make_cleanup_ui_file_delete (stb);
797
30b28db1 798 /* To print the type, we simply create a zero ``struct value *'' and
8b93c638
JM
799 cast it to our type. We then typeprint this variable. */
800 val = value_zero (var->type, not_lval);
df407dfe 801 type_print (value_type (val), "", stb, -1);
8b93c638
JM
802
803 thetype = ui_file_xstrdup (stb, &length);
804 do_cleanups (old_chain);
805 return thetype;
806}
807
1ecb4ee0
DJ
808/* Obtain the type of an object variable. */
809
810struct type *
811varobj_get_gdb_type (struct varobj *var)
812{
813 return var->type;
814}
815
02142340
VP
816/* Return a pointer to the full rooted expression of varobj VAR.
817 If it has not been computed yet, compute it. */
818char *
819varobj_get_path_expr (struct varobj *var)
820{
821 if (var->path_expr != NULL)
822 return var->path_expr;
823 else
824 {
825 /* For root varobjs, we initialize path_expr
826 when creating varobj, so here it should be
827 child varobj. */
828 gdb_assert (!is_root_p (var));
829 return (*var->root->lang->path_expr_of_child) (var);
830 }
831}
832
8b93c638
JM
833enum varobj_languages
834varobj_get_language (struct varobj *var)
835{
836 return variable_language (var);
837}
838
839int
840varobj_get_attributes (struct varobj *var)
841{
842 int attributes = 0;
843
340a7723 844 if (varobj_editable_p (var))
8b93c638
JM
845 /* FIXME: define masks for attributes */
846 attributes |= 0x00000001; /* Editable */
847
848 return attributes;
849}
850
851char *
852varobj_get_value (struct varobj *var)
853{
854 return my_value_of_variable (var);
855}
856
857/* Set the value of an object variable (if it is editable) to the
858 value of the given expression */
859/* Note: Invokes functions that can call error() */
860
861int
862varobj_set_value (struct varobj *var, char *expression)
863{
30b28db1 864 struct value *val;
8b93c638 865 int offset = 0;
a6c442d8 866 int error = 0;
8b93c638
JM
867
868 /* The argument "expression" contains the variable's new value.
869 We need to first construct a legal expression for this -- ugh! */
870 /* Does this cover all the bases? */
871 struct expression *exp;
30b28db1 872 struct value *value;
8b93c638 873 int saved_input_radix = input_radix;
340a7723
NR
874 char *s = expression;
875 int i;
8b93c638 876
340a7723 877 gdb_assert (varobj_editable_p (var));
8b93c638 878
340a7723
NR
879 input_radix = 10; /* ALWAYS reset to decimal temporarily */
880 exp = parse_exp_1 (&s, 0, 0);
881 if (!gdb_evaluate_expression (exp, &value))
882 {
883 /* We cannot proceed without a valid expression. */
884 xfree (exp);
885 return 0;
8b93c638
JM
886 }
887
340a7723
NR
888 /* All types that are editable must also be changeable. */
889 gdb_assert (varobj_value_is_changeable_p (var));
890
891 /* The value of a changeable variable object must not be lazy. */
892 gdb_assert (!value_lazy (var->value));
893
894 /* Need to coerce the input. We want to check if the
895 value of the variable object will be different
896 after assignment, and the first thing value_assign
897 does is coerce the input.
898 For example, if we are assigning an array to a pointer variable we
899 should compare the pointer with the the array's address, not with the
900 array's content. */
901 value = coerce_array (value);
902
903 /* The new value may be lazy. gdb_value_assign, or
904 rather value_contents, will take care of this.
905 If fetching of the new value will fail, gdb_value_assign
906 with catch the exception. */
907 if (!gdb_value_assign (var->value, value, &val))
908 return 0;
909
910 /* If the value has changed, record it, so that next -var-update can
911 report this change. If a variable had a value of '1', we've set it
912 to '333' and then set again to '1', when -var-update will report this
913 variable as changed -- because the first assignment has set the
914 'updated' flag. There's no need to optimize that, because return value
915 of -var-update should be considered an approximation. */
916 var->updated = install_new_value (var, val, 0 /* Compare values. */);
917 input_radix = saved_input_radix;
918 return 1;
8b93c638
JM
919}
920
921/* Returns a malloc'ed list with all root variable objects */
922int
923varobj_list (struct varobj ***varlist)
924{
925 struct varobj **cv;
926 struct varobj_root *croot;
927 int mycount = rootcount;
928
929 /* Alloc (rootcount + 1) entries for the result */
930 *varlist = xmalloc ((rootcount + 1) * sizeof (struct varobj *));
931
932 cv = *varlist;
933 croot = rootlist;
934 while ((croot != NULL) && (mycount > 0))
935 {
936 *cv = croot->rootvar;
937 mycount--;
938 cv++;
939 croot = croot->next;
940 }
941 /* Mark the end of the list */
942 *cv = NULL;
943
944 if (mycount || (croot != NULL))
72330bd6
AC
945 warning
946 ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
947 rootcount, mycount);
8b93c638
JM
948
949 return rootcount;
950}
951
acd65feb
VP
952/* Assign a new value to a variable object. If INITIAL is non-zero,
953 this is the first assignement after the variable object was just
954 created, or changed type. In that case, just assign the value
955 and return 0.
956 Otherwise, assign the value and if type_changeable returns non-zero,
957 find if the new value is different from the current value.
b26ed50d
VP
958 Return 1 if so, and 0 if the values are equal.
959
960 The VALUE parameter should not be released -- the function will
961 take care of releasing it when needed. */
acd65feb
VP
962static int
963install_new_value (struct varobj *var, struct value *value, int initial)
964{
965 int changeable;
966 int need_to_fetch;
967 int changed = 0;
25d5ea92 968 int intentionally_not_fetched = 0;
7a4d50bf 969 char *print_value = NULL;
acd65feb 970
acd65feb
VP
971 /* We need to know the varobj's type to decide if the value should
972 be fetched or not. C++ fake children (public/protected/private) don't have
973 a type. */
974 gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
b2c2bd75 975 changeable = varobj_value_is_changeable_p (var);
acd65feb
VP
976 need_to_fetch = changeable;
977
b26ed50d
VP
978 /* We are not interested in the address of references, and given
979 that in C++ a reference is not rebindable, it cannot
980 meaningfully change. So, get hold of the real value. */
981 if (value)
982 {
983 value = coerce_ref (value);
984 release_value (value);
985 }
986
acd65feb
VP
987 if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
988 /* For unions, we need to fetch the value implicitly because
989 of implementation of union member fetch. When gdb
990 creates a value for a field and the value of the enclosing
991 structure is not lazy, it immediately copies the necessary
992 bytes from the enclosing values. If the enclosing value is
993 lazy, the call to value_fetch_lazy on the field will read
994 the data from memory. For unions, that means we'll read the
995 same memory more than once, which is not desirable. So
996 fetch now. */
997 need_to_fetch = 1;
998
999 /* The new value might be lazy. If the type is changeable,
1000 that is we'll be comparing values of this type, fetch the
1001 value now. Otherwise, on the next update the old value
1002 will be lazy, which means we've lost that old value. */
1003 if (need_to_fetch && value && value_lazy (value))
1004 {
25d5ea92
VP
1005 struct varobj *parent = var->parent;
1006 int frozen = var->frozen;
1007 for (; !frozen && parent; parent = parent->parent)
1008 frozen |= parent->frozen;
1009
1010 if (frozen && initial)
1011 {
1012 /* For variables that are frozen, or are children of frozen
1013 variables, we don't do fetch on initial assignment.
1014 For non-initial assignemnt we do the fetch, since it means we're
1015 explicitly asked to compare the new value with the old one. */
1016 intentionally_not_fetched = 1;
1017 }
1018 else if (!gdb_value_fetch_lazy (value))
acd65feb 1019 {
acd65feb
VP
1020 /* Set the value to NULL, so that for the next -var-update,
1021 we don't try to compare the new value with this value,
1022 that we couldn't even read. */
1023 value = NULL;
1024 }
acd65feb
VP
1025 }
1026
7a4d50bf
VP
1027 /* Below, we'll be comparing string rendering of old and new
1028 values. Don't get string rendering if the value is
1029 lazy -- if it is, the code above has decided that the value
1030 should not be fetched. */
1031 if (value && !value_lazy (value))
1032 print_value = value_get_print_value (value, var->format);
1033
acd65feb
VP
1034 /* If the type is changeable, compare the old and the new values.
1035 If this is the initial assignment, we don't have any old value
1036 to compare with. */
7a4d50bf 1037 if (!initial && changeable)
acd65feb
VP
1038 {
1039 /* If the value of the varobj was changed by -var-set-value, then the
1040 value in the varobj and in the target is the same. However, that value
1041 is different from the value that the varobj had after the previous
57e66780 1042 -var-update. So need to the varobj as changed. */
acd65feb 1043 if (var->updated)
57e66780 1044 {
57e66780
DJ
1045 changed = 1;
1046 }
acd65feb
VP
1047 else
1048 {
1049 /* Try to compare the values. That requires that both
1050 values are non-lazy. */
25d5ea92
VP
1051 if (var->not_fetched && value_lazy (var->value))
1052 {
1053 /* This is a frozen varobj and the value was never read.
1054 Presumably, UI shows some "never read" indicator.
1055 Now that we've fetched the real value, we need to report
1056 this varobj as changed so that UI can show the real
1057 value. */
1058 changed = 1;
1059 }
1060 else if (var->value == NULL && value == NULL)
acd65feb
VP
1061 /* Equal. */
1062 ;
1063 else if (var->value == NULL || value == NULL)
57e66780 1064 {
57e66780
DJ
1065 changed = 1;
1066 }
acd65feb
VP
1067 else
1068 {
1069 gdb_assert (!value_lazy (var->value));
1070 gdb_assert (!value_lazy (value));
85265413 1071
57e66780 1072 gdb_assert (var->print_value != NULL && print_value != NULL);
85265413 1073 if (strcmp (var->print_value, print_value) != 0)
7a4d50bf 1074 changed = 1;
acd65feb
VP
1075 }
1076 }
1077 }
85265413 1078
acd65feb 1079 /* We must always keep the new value, since children depend on it. */
25d5ea92 1080 if (var->value != NULL && var->value != value)
acd65feb
VP
1081 value_free (var->value);
1082 var->value = value;
7a4d50bf
VP
1083 if (var->print_value)
1084 xfree (var->print_value);
1085 var->print_value = print_value;
25d5ea92
VP
1086 if (value && value_lazy (value) && intentionally_not_fetched)
1087 var->not_fetched = 1;
1088 else
1089 var->not_fetched = 0;
acd65feb 1090 var->updated = 0;
85265413 1091
b26ed50d 1092 gdb_assert (!var->value || value_type (var->value));
acd65feb
VP
1093
1094 return changed;
1095}
acd65feb 1096
8b93c638
JM
1097/* Update the values for a variable and its children. This is a
1098 two-pronged attack. First, re-parse the value for the root's
1099 expression to see if it's changed. Then go all the way
1100 through its children, reconstructing them and noting if they've
1101 changed.
8756216b
DP
1102 Return value:
1103 < 0 for error values, see varobj.h.
1104 Otherwise it is the number of children + parent changed.
8b93c638 1105
25d5ea92
VP
1106 The EXPLICIT parameter specifies if this call is result
1107 of MI request to update this specific variable, or
1108 result of implicit -var-update *. For implicit request, we don't
1109 update frozen variables.
705da579
KS
1110
1111 NOTE: This function may delete the caller's varobj. If it
8756216b
DP
1112 returns TYPE_CHANGED, then it has done this and VARP will be modified
1113 to point to the new varobj. */
8b93c638
JM
1114
1115int
25d5ea92
VP
1116varobj_update (struct varobj **varp, struct varobj ***changelist,
1117 int explicit)
8b93c638
JM
1118{
1119 int changed = 0;
25d5ea92 1120 int type_changed = 0;
8b93c638
JM
1121 int i;
1122 int vleft;
8b93c638
JM
1123 struct varobj *v;
1124 struct varobj **cv;
2c67cb8b 1125 struct varobj **templist = NULL;
30b28db1 1126 struct value *new;
28335dcc
VP
1127 VEC (varobj_p) *stack = NULL;
1128 VEC (varobj_p) *result = NULL;
e64d9b3d
MH
1129 struct frame_id old_fid;
1130 struct frame_info *fi;
8b93c638 1131
8756216b 1132 /* sanity check: have we been passed a pointer? */
a1f42e84 1133 gdb_assert (changelist);
8b93c638 1134
25d5ea92
VP
1135 /* Frozen means frozen -- we don't check for any change in
1136 this varobj, including its going out of scope, or
1137 changing type. One use case for frozen varobjs is
1138 retaining previously evaluated expressions, and we don't
1139 want them to be reevaluated at all. */
1140 if (!explicit && (*varp)->frozen)
1141 return 0;
8756216b
DP
1142
1143 if (!(*varp)->root->is_valid)
1144 return INVALID;
8b93c638 1145
25d5ea92 1146 if ((*varp)->root->rootvar == *varp)
ae093f96 1147 {
25d5ea92
VP
1148 /* Save the selected stack frame, since we will need to change it
1149 in order to evaluate expressions. */
1150 old_fid = get_frame_id (deprecated_safe_get_selected_frame ());
1151
1152 /* Update the root variable. value_of_root can return NULL
1153 if the variable is no longer around, i.e. we stepped out of
1154 the frame in which a local existed. We are letting the
1155 value_of_root variable dispose of the varobj if the type
1156 has changed. */
1157 type_changed = 1;
1158 new = value_of_root (varp, &type_changed);
1159
1160 /* Restore selected frame. */
1161 fi = frame_find_by_id (old_fid);
1162 if (fi)
1163 select_frame (fi);
1164
1165 /* If this is a "use_selected_frame" varobj, and its type has changed,
1166 them note that it's changed. */
1167 if (type_changed)
1168 VEC_safe_push (varobj_p, result, *varp);
1169
1170 if (install_new_value ((*varp), new, type_changed))
1171 {
1172 /* If type_changed is 1, install_new_value will never return
1173 non-zero, so we'll never report the same variable twice. */
1174 gdb_assert (!type_changed);
1175 VEC_safe_push (varobj_p, result, *varp);
1176 }
8b93c638 1177
25d5ea92
VP
1178 if (new == NULL)
1179 {
1180 /* This means the varobj itself is out of scope.
1181 Report it. */
1182 VEC_free (varobj_p, result);
1183 return NOT_IN_SCOPE;
1184 }
b20d8971
VP
1185 }
1186
28335dcc 1187 VEC_safe_push (varobj_p, stack, *varp);
8b93c638 1188
8756216b 1189 /* Walk through the children, reconstructing them all. */
28335dcc 1190 while (!VEC_empty (varobj_p, stack))
8b93c638 1191 {
28335dcc
VP
1192 v = VEC_pop (varobj_p, stack);
1193
1194 /* Push any children. Use reverse order so that the first
1195 child is popped from the work stack first, and so
1196 will be added to result first. This does not
1197 affect correctness, just "nicer". */
1198 for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
8b93c638 1199 {
28335dcc
VP
1200 varobj_p c = VEC_index (varobj_p, v->children, i);
1201 /* Child may be NULL if explicitly deleted by -var-delete. */
25d5ea92 1202 if (c != NULL && !c->frozen)
28335dcc 1203 VEC_safe_push (varobj_p, stack, c);
8b93c638
JM
1204 }
1205
28335dcc
VP
1206 /* Update this variable, unless it's a root, which is already
1207 updated. */
25d5ea92 1208 if (v->root->rootvar != v)
28335dcc
VP
1209 {
1210 new = value_of_child (v->parent, v->index);
1211 if (install_new_value (v, new, 0 /* type not changed */))
1212 {
1213 /* Note that it's changed */
1214 VEC_safe_push (varobj_p, result, v);
1215 v->updated = 0;
1216 }
8b93c638 1217 }
8b93c638
JM
1218 }
1219
8756216b 1220 /* Alloc (changed + 1) list entries. */
28335dcc 1221 changed = VEC_length (varobj_p, result);
8b93c638 1222 *changelist = xmalloc ((changed + 1) * sizeof (struct varobj *));
28335dcc 1223 cv = *changelist;
8b93c638 1224
28335dcc 1225 for (i = 0; i < changed; ++i)
8b93c638 1226 {
28335dcc
VP
1227 *cv = VEC_index (varobj_p, result, i);
1228 gdb_assert (*cv != NULL);
1229 ++cv;
8b93c638 1230 }
28335dcc 1231 *cv = 0;
8b93c638 1232
93b979d6
NR
1233 VEC_free (varobj_p, stack);
1234 VEC_free (varobj_p, result);
1235
73a93a32 1236 if (type_changed)
8756216b 1237 return TYPE_CHANGED;
73a93a32
JI
1238 else
1239 return changed;
8b93c638
JM
1240}
1241\f
1242
1243/* Helper functions */
1244
1245/*
1246 * Variable object construction/destruction
1247 */
1248
1249static int
fba45db2
KB
1250delete_variable (struct cpstack **resultp, struct varobj *var,
1251 int only_children_p)
8b93c638
JM
1252{
1253 int delcount = 0;
1254
1255 delete_variable_1 (resultp, &delcount, var,
1256 only_children_p, 1 /* remove_from_parent_p */ );
1257
1258 return delcount;
1259}
1260
1261/* Delete the variable object VAR and its children */
1262/* IMPORTANT NOTE: If we delete a variable which is a child
1263 and the parent is not removed we dump core. It must be always
1264 initially called with remove_from_parent_p set */
1265static void
72330bd6
AC
1266delete_variable_1 (struct cpstack **resultp, int *delcountp,
1267 struct varobj *var, int only_children_p,
1268 int remove_from_parent_p)
8b93c638 1269{
28335dcc 1270 int i;
8b93c638
JM
1271
1272 /* Delete any children of this variable, too. */
28335dcc
VP
1273 for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
1274 {
1275 varobj_p child = VEC_index (varobj_p, var->children, i);
214270ab
VP
1276 if (!child)
1277 continue;
8b93c638 1278 if (!remove_from_parent_p)
28335dcc
VP
1279 child->parent = NULL;
1280 delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
8b93c638 1281 }
28335dcc 1282 VEC_free (varobj_p, var->children);
8b93c638
JM
1283
1284 /* if we were called to delete only the children we are done here */
1285 if (only_children_p)
1286 return;
1287
1288 /* Otherwise, add it to the list of deleted ones and proceed to do so */
73a93a32
JI
1289 /* If the name is null, this is a temporary variable, that has not
1290 yet been installed, don't report it, it belongs to the caller... */
1291 if (var->obj_name != NULL)
8b93c638 1292 {
5b616ba1 1293 cppush (resultp, xstrdup (var->obj_name));
8b93c638
JM
1294 *delcountp = *delcountp + 1;
1295 }
1296
1297 /* If this variable has a parent, remove it from its parent's list */
1298 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1299 (as indicated by remove_from_parent_p) we don't bother doing an
1300 expensive list search to find the element to remove when we are
1301 discarding the list afterwards */
72330bd6 1302 if ((remove_from_parent_p) && (var->parent != NULL))
8b93c638 1303 {
28335dcc 1304 VEC_replace (varobj_p, var->parent->children, var->index, NULL);
8b93c638 1305 }
72330bd6 1306
73a93a32
JI
1307 if (var->obj_name != NULL)
1308 uninstall_variable (var);
8b93c638
JM
1309
1310 /* Free memory associated with this variable */
1311 free_variable (var);
1312}
1313
1314/* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1315static int
fba45db2 1316install_variable (struct varobj *var)
8b93c638
JM
1317{
1318 struct vlist *cv;
1319 struct vlist *newvl;
1320 const char *chp;
1321 unsigned int index = 0;
1322 unsigned int i = 1;
1323
1324 for (chp = var->obj_name; *chp; chp++)
1325 {
1326 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1327 }
1328
1329 cv = *(varobj_table + index);
1330 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1331 cv = cv->next;
1332
1333 if (cv != NULL)
8a3fe4f8 1334 error (_("Duplicate variable object name"));
8b93c638
JM
1335
1336 /* Add varobj to hash table */
1337 newvl = xmalloc (sizeof (struct vlist));
1338 newvl->next = *(varobj_table + index);
1339 newvl->var = var;
1340 *(varobj_table + index) = newvl;
1341
1342 /* If root, add varobj to root list */
b2c2bd75 1343 if (is_root_p (var))
8b93c638
JM
1344 {
1345 /* Add to list of root variables */
1346 if (rootlist == NULL)
1347 var->root->next = NULL;
1348 else
1349 var->root->next = rootlist;
1350 rootlist = var->root;
1351 rootcount++;
1352 }
1353
1354 return 1; /* OK */
1355}
1356
1357/* Unistall the object VAR. */
1358static void
fba45db2 1359uninstall_variable (struct varobj *var)
8b93c638
JM
1360{
1361 struct vlist *cv;
1362 struct vlist *prev;
1363 struct varobj_root *cr;
1364 struct varobj_root *prer;
1365 const char *chp;
1366 unsigned int index = 0;
1367 unsigned int i = 1;
1368
1369 /* Remove varobj from hash table */
1370 for (chp = var->obj_name; *chp; chp++)
1371 {
1372 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1373 }
1374
1375 cv = *(varobj_table + index);
1376 prev = NULL;
1377 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1378 {
1379 prev = cv;
1380 cv = cv->next;
1381 }
1382
1383 if (varobjdebug)
1384 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
1385
1386 if (cv == NULL)
1387 {
72330bd6
AC
1388 warning
1389 ("Assertion failed: Could not find variable object \"%s\" to delete",
1390 var->obj_name);
8b93c638
JM
1391 return;
1392 }
1393
1394 if (prev == NULL)
1395 *(varobj_table + index) = cv->next;
1396 else
1397 prev->next = cv->next;
1398
b8c9b27d 1399 xfree (cv);
8b93c638
JM
1400
1401 /* If root, remove varobj from root list */
b2c2bd75 1402 if (is_root_p (var))
8b93c638
JM
1403 {
1404 /* Remove from list of root variables */
1405 if (rootlist == var->root)
1406 rootlist = var->root->next;
1407 else
1408 {
1409 prer = NULL;
1410 cr = rootlist;
1411 while ((cr != NULL) && (cr->rootvar != var))
1412 {
1413 prer = cr;
1414 cr = cr->next;
1415 }
1416 if (cr == NULL)
1417 {
72330bd6
AC
1418 warning
1419 ("Assertion failed: Could not find varobj \"%s\" in root list",
1420 var->obj_name);
8b93c638
JM
1421 return;
1422 }
1423 if (prer == NULL)
1424 rootlist = NULL;
1425 else
1426 prer->next = cr->next;
1427 }
1428 rootcount--;
1429 }
1430
1431}
1432
8b93c638
JM
1433/* Create and install a child of the parent of the given name */
1434static struct varobj *
fba45db2 1435create_child (struct varobj *parent, int index, char *name)
8b93c638
JM
1436{
1437 struct varobj *child;
1438 char *childs_name;
acd65feb 1439 struct value *value;
8b93c638
JM
1440
1441 child = new_variable ();
1442
1443 /* name is allocated by name_of_child */
1444 child->name = name;
1445 child->index = index;
acd65feb 1446 value = value_of_child (parent, index);
8b93c638
JM
1447 child->parent = parent;
1448 child->root = parent->root;
b435e160 1449 childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
8b93c638
JM
1450 child->obj_name = childs_name;
1451 install_variable (child);
1452
acd65feb
VP
1453 /* Compute the type of the child. Must do this before
1454 calling install_new_value. */
1455 if (value != NULL)
1456 /* If the child had no evaluation errors, var->value
1457 will be non-NULL and contain a valid type. */
1458 child->type = value_type (value);
1459 else
1460 /* Otherwise, we must compute the type. */
1461 child->type = (*child->root->lang->type_of_child) (child->parent,
1462 child->index);
1463 install_new_value (child, value, 1);
1464
8b93c638
JM
1465 return child;
1466}
8b93c638
JM
1467\f
1468
1469/*
1470 * Miscellaneous utility functions.
1471 */
1472
1473/* Allocate memory and initialize a new variable */
1474static struct varobj *
1475new_variable (void)
1476{
1477 struct varobj *var;
1478
1479 var = (struct varobj *) xmalloc (sizeof (struct varobj));
1480 var->name = NULL;
02142340 1481 var->path_expr = NULL;
8b93c638
JM
1482 var->obj_name = NULL;
1483 var->index = -1;
1484 var->type = NULL;
1485 var->value = NULL;
8b93c638
JM
1486 var->num_children = -1;
1487 var->parent = NULL;
1488 var->children = NULL;
1489 var->format = 0;
1490 var->root = NULL;
fb9b6b35 1491 var->updated = 0;
85265413 1492 var->print_value = NULL;
25d5ea92
VP
1493 var->frozen = 0;
1494 var->not_fetched = 0;
8b93c638
JM
1495
1496 return var;
1497}
1498
1499/* Allocate memory and initialize a new root variable */
1500static struct varobj *
1501new_root_variable (void)
1502{
1503 struct varobj *var = new_variable ();
1504 var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));;
1505 var->root->lang = NULL;
1506 var->root->exp = NULL;
1507 var->root->valid_block = NULL;
7a424e99 1508 var->root->frame = null_frame_id;
73a93a32 1509 var->root->use_selected_frame = 0;
8b93c638 1510 var->root->rootvar = NULL;
8756216b 1511 var->root->is_valid = 1;
8b93c638
JM
1512
1513 return var;
1514}
1515
1516/* Free any allocated memory associated with VAR. */
1517static void
fba45db2 1518free_variable (struct varobj *var)
8b93c638
JM
1519{
1520 /* Free the expression if this is a root variable. */
b2c2bd75 1521 if (is_root_p (var))
8b93c638 1522 {
96c1eda2 1523 free_current_contents (&var->root->exp);
8038e1e2 1524 xfree (var->root);
8b93c638
JM
1525 }
1526
8038e1e2
AC
1527 xfree (var->name);
1528 xfree (var->obj_name);
85265413 1529 xfree (var->print_value);
02142340 1530 xfree (var->path_expr);
8038e1e2 1531 xfree (var);
8b93c638
JM
1532}
1533
74b7792f
AC
1534static void
1535do_free_variable_cleanup (void *var)
1536{
1537 free_variable (var);
1538}
1539
1540static struct cleanup *
1541make_cleanup_free_variable (struct varobj *var)
1542{
1543 return make_cleanup (do_free_variable_cleanup, var);
1544}
1545
6766a268
DJ
1546/* This returns the type of the variable. It also skips past typedefs
1547 to return the real type of the variable.
94b66fa7
KS
1548
1549 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1550 except within get_target_type and get_type. */
8b93c638 1551static struct type *
fba45db2 1552get_type (struct varobj *var)
8b93c638
JM
1553{
1554 struct type *type;
1555 type = var->type;
1556
6766a268
DJ
1557 if (type != NULL)
1558 type = check_typedef (type);
8b93c638
JM
1559
1560 return type;
1561}
1562
6e2a9270
VP
1563/* Return the type of the value that's stored in VAR,
1564 or that would have being stored there if the
1565 value were accessible.
1566
1567 This differs from VAR->type in that VAR->type is always
1568 the true type of the expession in the source language.
1569 The return value of this function is the type we're
1570 actually storing in varobj, and using for displaying
1571 the values and for comparing previous and new values.
1572
1573 For example, top-level references are always stripped. */
1574static struct type *
1575get_value_type (struct varobj *var)
1576{
1577 struct type *type;
1578
1579 if (var->value)
1580 type = value_type (var->value);
1581 else
1582 type = var->type;
1583
1584 type = check_typedef (type);
1585
1586 if (TYPE_CODE (type) == TYPE_CODE_REF)
1587 type = get_target_type (type);
1588
1589 type = check_typedef (type);
1590
1591 return type;
1592}
1593
8b93c638 1594/* This returns the target type (or NULL) of TYPE, also skipping
94b66fa7
KS
1595 past typedefs, just like get_type ().
1596
1597 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1598 except within get_target_type and get_type. */
8b93c638 1599static struct type *
fba45db2 1600get_target_type (struct type *type)
8b93c638
JM
1601{
1602 if (type != NULL)
1603 {
1604 type = TYPE_TARGET_TYPE (type);
6766a268
DJ
1605 if (type != NULL)
1606 type = check_typedef (type);
8b93c638
JM
1607 }
1608
1609 return type;
1610}
1611
1612/* What is the default display for this variable? We assume that
1613 everything is "natural". Any exceptions? */
1614static enum varobj_display_formats
fba45db2 1615variable_default_display (struct varobj *var)
8b93c638
JM
1616{
1617 return FORMAT_NATURAL;
1618}
1619
8b93c638
JM
1620/* FIXME: The following should be generic for any pointer */
1621static void
fba45db2 1622cppush (struct cpstack **pstack, char *name)
8b93c638
JM
1623{
1624 struct cpstack *s;
1625
1626 s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
1627 s->name = name;
1628 s->next = *pstack;
1629 *pstack = s;
1630}
1631
1632/* FIXME: The following should be generic for any pointer */
1633static char *
fba45db2 1634cppop (struct cpstack **pstack)
8b93c638
JM
1635{
1636 struct cpstack *s;
1637 char *v;
1638
1639 if ((*pstack)->name == NULL && (*pstack)->next == NULL)
1640 return NULL;
1641
1642 s = *pstack;
1643 v = s->name;
1644 *pstack = (*pstack)->next;
b8c9b27d 1645 xfree (s);
8b93c638
JM
1646
1647 return v;
1648}
1649\f
1650/*
1651 * Language-dependencies
1652 */
1653
1654/* Common entry points */
1655
1656/* Get the language of variable VAR. */
1657static enum varobj_languages
fba45db2 1658variable_language (struct varobj *var)
8b93c638
JM
1659{
1660 enum varobj_languages lang;
1661
1662 switch (var->root->exp->language_defn->la_language)
1663 {
1664 default:
1665 case language_c:
1666 lang = vlang_c;
1667 break;
1668 case language_cplus:
1669 lang = vlang_cplus;
1670 break;
1671 case language_java:
1672 lang = vlang_java;
1673 break;
1674 }
1675
1676 return lang;
1677}
1678
1679/* Return the number of children for a given variable.
1680 The result of this function is defined by the language
1681 implementation. The number of children returned by this function
1682 is the number of children that the user will see in the variable
1683 display. */
1684static int
fba45db2 1685number_of_children (struct varobj *var)
8b93c638
JM
1686{
1687 return (*var->root->lang->number_of_children) (var);;
1688}
1689
1690/* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1691static char *
fba45db2 1692name_of_variable (struct varobj *var)
8b93c638
JM
1693{
1694 return (*var->root->lang->name_of_variable) (var);
1695}
1696
1697/* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1698static char *
fba45db2 1699name_of_child (struct varobj *var, int index)
8b93c638
JM
1700{
1701 return (*var->root->lang->name_of_child) (var, index);
1702}
1703
30b28db1 1704/* What is the ``struct value *'' of the root variable VAR?
73a93a32
JI
1705 TYPE_CHANGED controls what to do if the type of a
1706 use_selected_frame = 1 variable changes. On input,
1707 TYPE_CHANGED = 1 means discard the old varobj, and replace
1708 it with this one. TYPE_CHANGED = 0 means leave it around.
1709 NB: In both cases, var_handle will point to the new varobj,
1710 so if you use TYPE_CHANGED = 0, you will have to stash the
1711 old varobj pointer away somewhere before calling this.
1712 On return, TYPE_CHANGED will be 1 if the type has changed, and
1713 0 otherwise. */
30b28db1 1714static struct value *
fba45db2 1715value_of_root (struct varobj **var_handle, int *type_changed)
8b93c638 1716{
73a93a32
JI
1717 struct varobj *var;
1718
1719 if (var_handle == NULL)
1720 return NULL;
1721
1722 var = *var_handle;
1723
1724 /* This should really be an exception, since this should
1725 only get called with a root variable. */
1726
b2c2bd75 1727 if (!is_root_p (var))
73a93a32
JI
1728 return NULL;
1729
1730 if (var->root->use_selected_frame)
1731 {
1732 struct varobj *tmp_var;
1733 char *old_type, *new_type;
6225abfa 1734
73a93a32
JI
1735 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
1736 USE_SELECTED_FRAME);
1737 if (tmp_var == NULL)
1738 {
1739 return NULL;
1740 }
6225abfa 1741 old_type = varobj_get_type (var);
73a93a32 1742 new_type = varobj_get_type (tmp_var);
72330bd6 1743 if (strcmp (old_type, new_type) == 0)
73a93a32
JI
1744 {
1745 varobj_delete (tmp_var, NULL, 0);
1746 *type_changed = 0;
1747 }
1748 else
1749 {
1750 if (*type_changed)
1751 {
72330bd6 1752 tmp_var->obj_name =
73a93a32 1753 savestring (var->obj_name, strlen (var->obj_name));
f7635dd9 1754 varobj_delete (var, NULL, 0);
73a93a32
JI
1755 }
1756 else
1757 {
72330bd6 1758 tmp_var->obj_name = varobj_gen_name ();
73a93a32
JI
1759 }
1760 install_variable (tmp_var);
1761 *var_handle = tmp_var;
705da579 1762 var = *var_handle;
73a93a32
JI
1763 *type_changed = 1;
1764 }
74dddad3
MS
1765 xfree (old_type);
1766 xfree (new_type);
73a93a32
JI
1767 }
1768 else
1769 {
1770 *type_changed = 0;
1771 }
1772
1773 return (*var->root->lang->value_of_root) (var_handle);
8b93c638
JM
1774}
1775
30b28db1
AC
1776/* What is the ``struct value *'' for the INDEX'th child of PARENT? */
1777static struct value *
fba45db2 1778value_of_child (struct varobj *parent, int index)
8b93c638 1779{
30b28db1 1780 struct value *value;
8b93c638
JM
1781
1782 value = (*parent->root->lang->value_of_child) (parent, index);
1783
8b93c638
JM
1784 return value;
1785}
1786
8b93c638
JM
1787/* GDB already has a command called "value_of_variable". Sigh. */
1788static char *
fba45db2 1789my_value_of_variable (struct varobj *var)
8b93c638 1790{
8756216b
DP
1791 if (var->root->is_valid)
1792 return (*var->root->lang->value_of_variable) (var);
1793 else
1794 return NULL;
8b93c638
JM
1795}
1796
85265413
NR
1797static char *
1798value_get_print_value (struct value *value, enum varobj_display_formats format)
1799{
1800 long dummy;
57e66780
DJ
1801 struct ui_file *stb;
1802 struct cleanup *old_chain;
85265413 1803 char *thevalue;
57e66780
DJ
1804
1805 if (value == NULL)
1806 return NULL;
1807
1808 stb = mem_fileopen ();
1809 old_chain = make_cleanup_ui_file_delete (stb);
1810
85265413
NR
1811 common_val_print (value, stb, format_code[(int) format], 1, 0, 0);
1812 thevalue = ui_file_xstrdup (stb, &dummy);
57e66780 1813
85265413
NR
1814 do_cleanups (old_chain);
1815 return thevalue;
1816}
1817
340a7723
NR
1818int
1819varobj_editable_p (struct varobj *var)
1820{
1821 struct type *type;
1822 struct value *value;
1823
1824 if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value)))
1825 return 0;
1826
1827 type = get_value_type (var);
1828
1829 switch (TYPE_CODE (type))
1830 {
1831 case TYPE_CODE_STRUCT:
1832 case TYPE_CODE_UNION:
1833 case TYPE_CODE_ARRAY:
1834 case TYPE_CODE_FUNC:
1835 case TYPE_CODE_METHOD:
1836 return 0;
1837 break;
1838
1839 default:
1840 return 1;
1841 break;
1842 }
1843}
1844
acd65feb
VP
1845/* Return non-zero if changes in value of VAR
1846 must be detected and reported by -var-update.
1847 Return zero is -var-update should never report
1848 changes of such values. This makes sense for structures
1849 (since the changes in children values will be reported separately),
1850 or for artifical objects (like 'public' pseudo-field in C++).
1851
1852 Return value of 0 means that gdb need not call value_fetch_lazy
1853 for the value of this variable object. */
8b93c638 1854static int
b2c2bd75 1855varobj_value_is_changeable_p (struct varobj *var)
8b93c638
JM
1856{
1857 int r;
1858 struct type *type;
1859
1860 if (CPLUS_FAKE_CHILD (var))
1861 return 0;
1862
6e2a9270 1863 type = get_value_type (var);
8b93c638
JM
1864
1865 switch (TYPE_CODE (type))
1866 {
72330bd6
AC
1867 case TYPE_CODE_STRUCT:
1868 case TYPE_CODE_UNION:
1869 case TYPE_CODE_ARRAY:
1870 r = 0;
1871 break;
8b93c638 1872
72330bd6
AC
1873 default:
1874 r = 1;
8b93c638
JM
1875 }
1876
1877 return r;
1878}
1879
2024f65a
VP
1880/* Given the value and the type of a variable object,
1881 adjust the value and type to those necessary
1882 for getting children of the variable object.
1883 This includes dereferencing top-level references
1884 to all types and dereferencing pointers to
1885 structures.
1886
1887 Both TYPE and *TYPE should be non-null. VALUE
1888 can be null if we want to only translate type.
1889 *VALUE can be null as well -- if the parent
02142340
VP
1890 value is not known.
1891
1892 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
1893 depending on whether pointer was deferenced
1894 in this function. */
2024f65a
VP
1895static void
1896adjust_value_for_child_access (struct value **value,
02142340
VP
1897 struct type **type,
1898 int *was_ptr)
2024f65a
VP
1899{
1900 gdb_assert (type && *type);
1901
02142340
VP
1902 if (was_ptr)
1903 *was_ptr = 0;
1904
2024f65a
VP
1905 *type = check_typedef (*type);
1906
1907 /* The type of value stored in varobj, that is passed
1908 to us, is already supposed to be
1909 reference-stripped. */
1910
1911 gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
1912
1913 /* Pointers to structures are treated just like
1914 structures when accessing children. Don't
1915 dererences pointers to other types. */
1916 if (TYPE_CODE (*type) == TYPE_CODE_PTR)
1917 {
1918 struct type *target_type = get_target_type (*type);
1919 if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
1920 || TYPE_CODE (target_type) == TYPE_CODE_UNION)
1921 {
1922 if (value && *value)
1923 gdb_value_ind (*value, value);
1924 *type = target_type;
02142340
VP
1925 if (was_ptr)
1926 *was_ptr = 1;
2024f65a
VP
1927 }
1928 }
1929
1930 /* The 'get_target_type' function calls check_typedef on
1931 result, so we can immediately check type code. No
1932 need to call check_typedef here. */
1933}
1934
8b93c638
JM
1935/* C */
1936static int
fba45db2 1937c_number_of_children (struct varobj *var)
8b93c638 1938{
2024f65a
VP
1939 struct type *type = get_value_type (var);
1940 int children = 0;
8b93c638 1941 struct type *target;
8b93c638 1942
02142340 1943 adjust_value_for_child_access (NULL, &type, NULL);
8b93c638 1944 target = get_target_type (type);
8b93c638
JM
1945
1946 switch (TYPE_CODE (type))
1947 {
1948 case TYPE_CODE_ARRAY:
1949 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
72330bd6 1950 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
8b93c638
JM
1951 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
1952 else
74a44383
DJ
1953 /* If we don't know how many elements there are, don't display
1954 any. */
1955 children = 0;
8b93c638
JM
1956 break;
1957
1958 case TYPE_CODE_STRUCT:
1959 case TYPE_CODE_UNION:
1960 children = TYPE_NFIELDS (type);
1961 break;
1962
1963 case TYPE_CODE_PTR:
2024f65a
VP
1964 /* The type here is a pointer to non-struct. Typically, pointers
1965 have one child, except for function ptrs, which have no children,
1966 and except for void*, as we don't know what to show.
1967
0755e6c1
FN
1968 We can show char* so we allow it to be dereferenced. If you decide
1969 to test for it, please mind that a little magic is necessary to
1970 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
1971 TYPE_NAME == "char" */
2024f65a
VP
1972 if (TYPE_CODE (target) == TYPE_CODE_FUNC
1973 || TYPE_CODE (target) == TYPE_CODE_VOID)
1974 children = 0;
1975 else
1976 children = 1;
8b93c638
JM
1977 break;
1978
1979 default:
1980 /* Other types have no children */
1981 break;
1982 }
1983
1984 return children;
1985}
1986
1987static char *
fba45db2 1988c_name_of_variable (struct varobj *parent)
8b93c638
JM
1989{
1990 return savestring (parent->name, strlen (parent->name));
1991}
1992
bbec2603
VP
1993/* Return the value of element TYPE_INDEX of a structure
1994 value VALUE. VALUE's type should be a structure,
1995 or union, or a typedef to struct/union.
1996
1997 Returns NULL if getting the value fails. Never throws. */
1998static struct value *
1999value_struct_element_index (struct value *value, int type_index)
8b93c638 2000{
bbec2603
VP
2001 struct value *result = NULL;
2002 volatile struct gdb_exception e;
8b93c638 2003
bbec2603
VP
2004 struct type *type = value_type (value);
2005 type = check_typedef (type);
2006
2007 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
2008 || TYPE_CODE (type) == TYPE_CODE_UNION);
8b93c638 2009
bbec2603
VP
2010 TRY_CATCH (e, RETURN_MASK_ERROR)
2011 {
2012 if (TYPE_FIELD_STATIC (type, type_index))
2013 result = value_static_field (type, type_index);
2014 else
2015 result = value_primitive_field (value, 0, type_index, type);
2016 }
2017 if (e.reason < 0)
2018 {
2019 return NULL;
2020 }
2021 else
2022 {
2023 return result;
2024 }
2025}
2026
2027/* Obtain the information about child INDEX of the variable
2028 object PARENT.
2029 If CNAME is not null, sets *CNAME to the name of the child relative
2030 to the parent.
2031 If CVALUE is not null, sets *CVALUE to the value of the child.
2032 If CTYPE is not null, sets *CTYPE to the type of the child.
2033
2034 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
2035 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
2036 to NULL. */
2037static void
2038c_describe_child (struct varobj *parent, int index,
02142340
VP
2039 char **cname, struct value **cvalue, struct type **ctype,
2040 char **cfull_expression)
bbec2603
VP
2041{
2042 struct value *value = parent->value;
2024f65a 2043 struct type *type = get_value_type (parent);
02142340
VP
2044 char *parent_expression = NULL;
2045 int was_ptr;
bbec2603
VP
2046
2047 if (cname)
2048 *cname = NULL;
2049 if (cvalue)
2050 *cvalue = NULL;
2051 if (ctype)
2052 *ctype = NULL;
02142340
VP
2053 if (cfull_expression)
2054 {
2055 *cfull_expression = NULL;
2056 parent_expression = varobj_get_path_expr (parent);
2057 }
2058 adjust_value_for_child_access (&value, &type, &was_ptr);
bbec2603 2059
8b93c638
JM
2060 switch (TYPE_CODE (type))
2061 {
2062 case TYPE_CODE_ARRAY:
bbec2603
VP
2063 if (cname)
2064 *cname = xstrprintf ("%d", index
2065 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
2066
2067 if (cvalue && value)
2068 {
2069 int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
2070 struct value *indval =
2071 value_from_longest (builtin_type_int, (LONGEST) real_index);
2072 gdb_value_subscript (value, indval, cvalue);
2073 }
2074
2075 if (ctype)
2076 *ctype = get_target_type (type);
2077
02142340
VP
2078 if (cfull_expression)
2079 *cfull_expression = xstrprintf ("(%s)[%d]", parent_expression,
2080 index
2081 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
2082
2083
8b93c638
JM
2084 break;
2085
2086 case TYPE_CODE_STRUCT:
2087 case TYPE_CODE_UNION:
bbec2603
VP
2088 if (cname)
2089 {
2090 char *string = TYPE_FIELD_NAME (type, index);
2091 *cname = savestring (string, strlen (string));
2092 }
2093
2094 if (cvalue && value)
2095 {
2096 /* For C, varobj index is the same as type index. */
2097 *cvalue = value_struct_element_index (value, index);
2098 }
2099
2100 if (ctype)
2101 *ctype = TYPE_FIELD_TYPE (type, index);
2102
02142340
VP
2103 if (cfull_expression)
2104 {
2105 char *join = was_ptr ? "->" : ".";
2106 *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression, join,
2107 TYPE_FIELD_NAME (type, index));
2108 }
2109
8b93c638
JM
2110 break;
2111
2112 case TYPE_CODE_PTR:
bbec2603
VP
2113 if (cname)
2114 *cname = xstrprintf ("*%s", parent->name);
8b93c638 2115
bbec2603
VP
2116 if (cvalue && value)
2117 gdb_value_ind (value, cvalue);
2118
2024f65a
VP
2119 /* Don't use get_target_type because it calls
2120 check_typedef and here, we want to show the true
2121 declared type of the variable. */
bbec2603 2122 if (ctype)
2024f65a 2123 *ctype = TYPE_TARGET_TYPE (type);
02142340
VP
2124
2125 if (cfull_expression)
2126 *cfull_expression = xstrprintf ("*(%s)", parent_expression);
bbec2603 2127
8b93c638
JM
2128 break;
2129
2130 default:
2131 /* This should not happen */
bbec2603
VP
2132 if (cname)
2133 *cname = xstrdup ("???");
02142340
VP
2134 if (cfull_expression)
2135 *cfull_expression = xstrdup ("???");
bbec2603 2136 /* Don't set value and type, we don't know then. */
8b93c638 2137 }
bbec2603 2138}
8b93c638 2139
bbec2603
VP
2140static char *
2141c_name_of_child (struct varobj *parent, int index)
2142{
2143 char *name;
02142340 2144 c_describe_child (parent, index, &name, NULL, NULL, NULL);
8b93c638
JM
2145 return name;
2146}
2147
02142340
VP
2148static char *
2149c_path_expr_of_child (struct varobj *child)
2150{
2151 c_describe_child (child->parent, child->index, NULL, NULL, NULL,
2152 &child->path_expr);
2153 return child->path_expr;
2154}
2155
30b28db1 2156static struct value *
fba45db2 2157c_value_of_root (struct varobj **var_handle)
8b93c638 2158{
5e572bb4 2159 struct value *new_val = NULL;
73a93a32 2160 struct varobj *var = *var_handle;
8b93c638
JM
2161 struct frame_info *fi;
2162 int within_scope;
2163
73a93a32 2164 /* Only root variables can be updated... */
b2c2bd75 2165 if (!is_root_p (var))
73a93a32
JI
2166 /* Not a root var */
2167 return NULL;
2168
72330bd6 2169
8b93c638 2170 /* Determine whether the variable is still around. */
b20d8971 2171 if (var->root->valid_block == NULL || var->root->use_selected_frame)
8b93c638
JM
2172 within_scope = 1;
2173 else
2174 {
e64d9b3d 2175 fi = frame_find_by_id (var->root->frame);
8b93c638
JM
2176 within_scope = fi != NULL;
2177 /* FIXME: select_frame could fail */
d2353924
NR
2178 if (fi)
2179 {
2180 CORE_ADDR pc = get_frame_pc (fi);
2181 if (pc < BLOCK_START (var->root->valid_block) ||
2182 pc >= BLOCK_END (var->root->valid_block))
2183 within_scope = 0;
2d43bda2
NR
2184 else
2185 select_frame (fi);
d2353924 2186 }
8b93c638 2187 }
72330bd6 2188
8b93c638
JM
2189 if (within_scope)
2190 {
73a93a32 2191 /* We need to catch errors here, because if evaluate
85d93f1d
VP
2192 expression fails we want to just return NULL. */
2193 gdb_evaluate_expression (var->root->exp, &new_val);
8b93c638
JM
2194 return new_val;
2195 }
2196
2197 return NULL;
2198}
2199
30b28db1 2200static struct value *
fba45db2 2201c_value_of_child (struct varobj *parent, int index)
8b93c638 2202{
bbec2603 2203 struct value *value = NULL;
02142340 2204 c_describe_child (parent, index, NULL, &value, NULL, NULL);
8b93c638
JM
2205
2206 return value;
2207}
2208
2209static struct type *
fba45db2 2210c_type_of_child (struct varobj *parent, int index)
8b93c638 2211{
bbec2603 2212 struct type *type = NULL;
02142340 2213 c_describe_child (parent, index, NULL, NULL, &type, NULL);
8b93c638
JM
2214 return type;
2215}
2216
8b93c638 2217static char *
fba45db2 2218c_value_of_variable (struct varobj *var)
8b93c638 2219{
14b3d9c9
JB
2220 /* BOGUS: if val_print sees a struct/class, or a reference to one,
2221 it will print out its children instead of "{...}". So we need to
2222 catch that case explicitly. */
2223 struct type *type = get_type (var);
e64d9b3d 2224
14b3d9c9
JB
2225 /* Strip top-level references. */
2226 while (TYPE_CODE (type) == TYPE_CODE_REF)
2227 type = check_typedef (TYPE_TARGET_TYPE (type));
2228
2229 switch (TYPE_CODE (type))
8b93c638
JM
2230 {
2231 case TYPE_CODE_STRUCT:
2232 case TYPE_CODE_UNION:
2233 return xstrdup ("{...}");
2234 /* break; */
2235
2236 case TYPE_CODE_ARRAY:
2237 {
e64d9b3d 2238 char *number;
b435e160 2239 number = xstrprintf ("[%d]", var->num_children);
e64d9b3d 2240 return (number);
8b93c638
JM
2241 }
2242 /* break; */
2243
2244 default:
2245 {
575bbeb6
KS
2246 if (var->value == NULL)
2247 {
2248 /* This can happen if we attempt to get the value of a struct
2249 member when the parent is an invalid pointer. This is an
2250 error condition, so we should tell the caller. */
2251 return NULL;
2252 }
2253 else
2254 {
25d5ea92
VP
2255 if (var->not_fetched && value_lazy (var->value))
2256 /* Frozen variable and no value yet. We don't
2257 implicitly fetch the value. MI response will
2258 use empty string for the value, which is OK. */
2259 return NULL;
2260
b2c2bd75 2261 gdb_assert (varobj_value_is_changeable_p (var));
acd65feb 2262 gdb_assert (!value_lazy (var->value));
85265413
NR
2263 return value_get_print_value (var->value, var->format);
2264 }
e64d9b3d 2265 }
8b93c638
JM
2266 }
2267}
2268\f
2269
2270/* C++ */
2271
2272static int
fba45db2 2273cplus_number_of_children (struct varobj *var)
8b93c638
JM
2274{
2275 struct type *type;
2276 int children, dont_know;
2277
2278 dont_know = 1;
2279 children = 0;
2280
2281 if (!CPLUS_FAKE_CHILD (var))
2282 {
2024f65a 2283 type = get_value_type (var);
02142340 2284 adjust_value_for_child_access (NULL, &type, NULL);
8b93c638
JM
2285
2286 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
72330bd6 2287 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
8b93c638
JM
2288 {
2289 int kids[3];
2290
2291 cplus_class_num_children (type, kids);
2292 if (kids[v_public] != 0)
2293 children++;
2294 if (kids[v_private] != 0)
2295 children++;
2296 if (kids[v_protected] != 0)
2297 children++;
2298
2299 /* Add any baseclasses */
2300 children += TYPE_N_BASECLASSES (type);
2301 dont_know = 0;
2302
2303 /* FIXME: save children in var */
2304 }
2305 }
2306 else
2307 {
2308 int kids[3];
2309
2024f65a 2310 type = get_value_type (var->parent);
02142340 2311 adjust_value_for_child_access (NULL, &type, NULL);
8b93c638
JM
2312
2313 cplus_class_num_children (type, kids);
6e382aa3 2314 if (strcmp (var->name, "public") == 0)
8b93c638 2315 children = kids[v_public];
6e382aa3 2316 else if (strcmp (var->name, "private") == 0)
8b93c638
JM
2317 children = kids[v_private];
2318 else
2319 children = kids[v_protected];
2320 dont_know = 0;
2321 }
2322
2323 if (dont_know)
2324 children = c_number_of_children (var);
2325
2326 return children;
2327}
2328
2329/* Compute # of public, private, and protected variables in this class.
2330 That means we need to descend into all baseclasses and find out
2331 how many are there, too. */
2332static void
1669605f 2333cplus_class_num_children (struct type *type, int children[3])
8b93c638
JM
2334{
2335 int i;
2336
2337 children[v_public] = 0;
2338 children[v_private] = 0;
2339 children[v_protected] = 0;
2340
2341 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
2342 {
2343 /* If we have a virtual table pointer, omit it. */
72330bd6 2344 if (TYPE_VPTR_BASETYPE (type) == type && TYPE_VPTR_FIELDNO (type) == i)
8b93c638
JM
2345 continue;
2346
2347 if (TYPE_FIELD_PROTECTED (type, i))
2348 children[v_protected]++;
2349 else if (TYPE_FIELD_PRIVATE (type, i))
2350 children[v_private]++;
2351 else
2352 children[v_public]++;
2353 }
2354}
2355
2356static char *
fba45db2 2357cplus_name_of_variable (struct varobj *parent)
8b93c638
JM
2358{
2359 return c_name_of_variable (parent);
2360}
2361
2024f65a
VP
2362enum accessibility { private_field, protected_field, public_field };
2363
2364/* Check if field INDEX of TYPE has the specified accessibility.
2365 Return 0 if so and 1 otherwise. */
2366static int
2367match_accessibility (struct type *type, int index, enum accessibility acc)
8b93c638 2368{
2024f65a
VP
2369 if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
2370 return 1;
2371 else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
2372 return 1;
2373 else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
2374 && !TYPE_FIELD_PROTECTED (type, index))
2375 return 1;
2376 else
2377 return 0;
2378}
2379
2380static void
2381cplus_describe_child (struct varobj *parent, int index,
02142340
VP
2382 char **cname, struct value **cvalue, struct type **ctype,
2383 char **cfull_expression)
2024f65a 2384{
348144ba 2385 char *name = NULL;
2024f65a 2386 struct value *value;
8b93c638 2387 struct type *type;
02142340
VP
2388 int was_ptr;
2389 char *parent_expression = NULL;
8b93c638 2390
2024f65a
VP
2391 if (cname)
2392 *cname = NULL;
2393 if (cvalue)
2394 *cvalue = NULL;
2395 if (ctype)
2396 *ctype = NULL;
02142340
VP
2397 if (cfull_expression)
2398 *cfull_expression = NULL;
2024f65a 2399
8b93c638
JM
2400 if (CPLUS_FAKE_CHILD (parent))
2401 {
2024f65a
VP
2402 value = parent->parent->value;
2403 type = get_value_type (parent->parent);
02142340
VP
2404 if (cfull_expression)
2405 parent_expression = varobj_get_path_expr (parent->parent);
8b93c638
JM
2406 }
2407 else
2024f65a
VP
2408 {
2409 value = parent->value;
2410 type = get_value_type (parent);
02142340
VP
2411 if (cfull_expression)
2412 parent_expression = varobj_get_path_expr (parent);
2024f65a 2413 }
8b93c638 2414
02142340 2415 adjust_value_for_child_access (&value, &type, &was_ptr);
2024f65a
VP
2416
2417 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2418 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
8b93c638 2419 {
02142340 2420 char *join = was_ptr ? "->" : ".";
8b93c638
JM
2421 if (CPLUS_FAKE_CHILD (parent))
2422 {
6e382aa3
JJ
2423 /* The fields of the class type are ordered as they
2424 appear in the class. We are given an index for a
2425 particular access control type ("public","protected",
2426 or "private"). We must skip over fields that don't
2427 have the access control we are looking for to properly
2428 find the indexed field. */
2429 int type_index = TYPE_N_BASECLASSES (type);
2024f65a 2430 enum accessibility acc = public_field;
6e382aa3 2431 if (strcmp (parent->name, "private") == 0)
2024f65a 2432 acc = private_field;
6e382aa3 2433 else if (strcmp (parent->name, "protected") == 0)
2024f65a
VP
2434 acc = protected_field;
2435
2436 while (index >= 0)
6e382aa3 2437 {
2024f65a
VP
2438 if (TYPE_VPTR_BASETYPE (type) == type
2439 && type_index == TYPE_VPTR_FIELDNO (type))
2440 ; /* ignore vptr */
2441 else if (match_accessibility (type, type_index, acc))
6e382aa3
JJ
2442 --index;
2443 ++type_index;
6e382aa3 2444 }
2024f65a
VP
2445 --type_index;
2446
2447 if (cname)
2448 *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
2449
2450 if (cvalue && value)
2451 *cvalue = value_struct_element_index (value, type_index);
2452
2453 if (ctype)
2454 *ctype = TYPE_FIELD_TYPE (type, type_index);
02142340
VP
2455
2456 if (cfull_expression)
2457 *cfull_expression = xstrprintf ("((%s)%s%s)", parent_expression,
2458 join,
2459 TYPE_FIELD_NAME (type, type_index));
2024f65a
VP
2460 }
2461 else if (index < TYPE_N_BASECLASSES (type))
2462 {
2463 /* This is a baseclass. */
2464 if (cname)
2465 *cname = xstrdup (TYPE_FIELD_NAME (type, index));
2466
2467 if (cvalue && value)
6e382aa3 2468 {
2024f65a 2469 *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
02142340 2470 release_value (*cvalue);
6e382aa3
JJ
2471 }
2472
2024f65a
VP
2473 if (ctype)
2474 {
2475 *ctype = TYPE_FIELD_TYPE (type, index);
2476 }
02142340
VP
2477
2478 if (cfull_expression)
2479 {
2480 char *ptr = was_ptr ? "*" : "";
2481 /* Cast the parent to the base' type. Note that in gdb,
2482 expression like
2483 (Base1)d
2484 will create an lvalue, for all appearences, so we don't
2485 need to use more fancy:
2486 *(Base1*)(&d)
2487 construct. */
2488 *cfull_expression = xstrprintf ("(%s(%s%s) %s)",
2489 ptr,
2490 TYPE_FIELD_NAME (type, index),
2491 ptr,
2492 parent_expression);
2493 }
8b93c638 2494 }
8b93c638
JM
2495 else
2496 {
348144ba 2497 char *access = NULL;
6e382aa3 2498 int children[3];
2024f65a 2499 cplus_class_num_children (type, children);
6e382aa3 2500
8b93c638 2501 /* Everything beyond the baseclasses can
6e382aa3
JJ
2502 only be "public", "private", or "protected"
2503
2504 The special "fake" children are always output by varobj in
2505 this order. So if INDEX == 2, it MUST be "protected". */
8b93c638
JM
2506 index -= TYPE_N_BASECLASSES (type);
2507 switch (index)
2508 {
2509 case 0:
6e382aa3 2510 if (children[v_public] > 0)
2024f65a 2511 access = "public";
6e382aa3 2512 else if (children[v_private] > 0)
2024f65a 2513 access = "private";
6e382aa3 2514 else
2024f65a 2515 access = "protected";
6e382aa3 2516 break;
8b93c638 2517 case 1:
6e382aa3 2518 if (children[v_public] > 0)
8b93c638 2519 {
6e382aa3 2520 if (children[v_private] > 0)
2024f65a 2521 access = "private";
6e382aa3 2522 else
2024f65a 2523 access = "protected";
8b93c638 2524 }
6e382aa3 2525 else if (children[v_private] > 0)
2024f65a 2526 access = "protected";
6e382aa3 2527 break;
8b93c638 2528 case 2:
6e382aa3 2529 /* Must be protected */
2024f65a 2530 access = "protected";
6e382aa3 2531 break;
8b93c638
JM
2532 default:
2533 /* error! */
2534 break;
2535 }
348144ba
MS
2536
2537 gdb_assert (access);
2024f65a
VP
2538 if (cname)
2539 *cname = xstrdup (access);
8b93c638 2540
02142340 2541 /* Value and type and full expression are null here. */
2024f65a 2542 }
8b93c638 2543 }
8b93c638
JM
2544 else
2545 {
02142340 2546 c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
2024f65a
VP
2547 }
2548}
8b93c638 2549
2024f65a
VP
2550static char *
2551cplus_name_of_child (struct varobj *parent, int index)
2552{
2553 char *name = NULL;
02142340 2554 cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
8b93c638
JM
2555 return name;
2556}
2557
02142340
VP
2558static char *
2559cplus_path_expr_of_child (struct varobj *child)
2560{
2561 cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
2562 &child->path_expr);
2563 return child->path_expr;
2564}
2565
30b28db1 2566static struct value *
fba45db2 2567cplus_value_of_root (struct varobj **var_handle)
8b93c638 2568{
73a93a32 2569 return c_value_of_root (var_handle);
8b93c638
JM
2570}
2571
30b28db1 2572static struct value *
fba45db2 2573cplus_value_of_child (struct varobj *parent, int index)
8b93c638 2574{
2024f65a 2575 struct value *value = NULL;
02142340 2576 cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
8b93c638
JM
2577 return value;
2578}
2579
2580static struct type *
fba45db2 2581cplus_type_of_child (struct varobj *parent, int index)
8b93c638 2582{
2024f65a 2583 struct type *type = NULL;
02142340 2584 cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
8b93c638
JM
2585 return type;
2586}
2587
8b93c638 2588static char *
fba45db2 2589cplus_value_of_variable (struct varobj *var)
8b93c638
JM
2590{
2591
2592 /* If we have one of our special types, don't print out
2593 any value. */
2594 if (CPLUS_FAKE_CHILD (var))
2595 return xstrdup ("");
2596
2597 return c_value_of_variable (var);
2598}
2599\f
2600/* Java */
2601
2602static int
fba45db2 2603java_number_of_children (struct varobj *var)
8b93c638
JM
2604{
2605 return cplus_number_of_children (var);
2606}
2607
2608static char *
fba45db2 2609java_name_of_variable (struct varobj *parent)
8b93c638
JM
2610{
2611 char *p, *name;
2612
2613 name = cplus_name_of_variable (parent);
2614 /* If the name has "-" in it, it is because we
2615 needed to escape periods in the name... */
2616 p = name;
2617
2618 while (*p != '\000')
2619 {
2620 if (*p == '-')
2621 *p = '.';
2622 p++;
2623 }
2624
2625 return name;
2626}
2627
2628static char *
fba45db2 2629java_name_of_child (struct varobj *parent, int index)
8b93c638
JM
2630{
2631 char *name, *p;
2632
2633 name = cplus_name_of_child (parent, index);
2634 /* Escape any periods in the name... */
2635 p = name;
2636
2637 while (*p != '\000')
2638 {
2639 if (*p == '.')
2640 *p = '-';
2641 p++;
2642 }
2643
2644 return name;
2645}
2646
02142340
VP
2647static char *
2648java_path_expr_of_child (struct varobj *child)
2649{
2650 return NULL;
2651}
2652
30b28db1 2653static struct value *
fba45db2 2654java_value_of_root (struct varobj **var_handle)
8b93c638 2655{
73a93a32 2656 return cplus_value_of_root (var_handle);
8b93c638
JM
2657}
2658
30b28db1 2659static struct value *
fba45db2 2660java_value_of_child (struct varobj *parent, int index)
8b93c638
JM
2661{
2662 return cplus_value_of_child (parent, index);
2663}
2664
2665static struct type *
fba45db2 2666java_type_of_child (struct varobj *parent, int index)
8b93c638
JM
2667{
2668 return cplus_type_of_child (parent, index);
2669}
2670
8b93c638 2671static char *
fba45db2 2672java_value_of_variable (struct varobj *var)
8b93c638
JM
2673{
2674 return cplus_value_of_variable (var);
2675}
2676\f
2677extern void _initialize_varobj (void);
2678void
2679_initialize_varobj (void)
2680{
2681 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
2682
2683 varobj_table = xmalloc (sizeof_table);
2684 memset (varobj_table, 0, sizeof_table);
2685
85c07804
AC
2686 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
2687 &varobjdebug, _("\
2688Set varobj debugging."), _("\
2689Show varobj debugging."), _("\
2690When non-zero, varobj debugging is enabled."),
2691 NULL,
920d2a44 2692 show_varobjdebug,
85c07804 2693 &setlist, &showlist);
8b93c638 2694}
8756216b
DP
2695
2696/* Invalidate the varobjs that are tied to locals and re-create the ones that
2697 are defined on globals.
2698 Invalidated varobjs will be always printed in_scope="invalid". */
2699void
2700varobj_invalidate (void)
2701{
2702 struct varobj **all_rootvarobj;
2703 struct varobj **varp;
2704
2705 if (varobj_list (&all_rootvarobj) > 0)
2706 {
2707 varp = all_rootvarobj;
2708 while (*varp != NULL)
2709 {
2710 /* global var must be re-evaluated. */
2711 if ((*varp)->root->valid_block == NULL)
2712 {
2713 struct varobj *tmp_var;
2714
2715 /* Try to create a varobj with same expression. If we succeed replace
2716 the old varobj, otherwise invalidate it. */
2717 tmp_var = varobj_create (NULL, (*varp)->name, (CORE_ADDR) 0, USE_CURRENT_FRAME);
2718 if (tmp_var != NULL)
2719 {
2720 tmp_var->obj_name = xstrdup ((*varp)->obj_name);
2721 varobj_delete (*varp, NULL, 0);
2722 install_variable (tmp_var);
2723 }
2724 else
2725 (*varp)->root->is_valid = 0;
2726 }
2727 else /* locals must be invalidated. */
2728 (*varp)->root->is_valid = 0;
2729
2730 varp++;
2731 }
2732 xfree (all_rootvarobj);
2733 }
2734 return;
2735}