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