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